[2/3] gdb: make packet_command function available outside remote.c

Message ID 7bd1464bc602f1238273293b1ef60d619380814f.1631375869.git.andrew.burgess@embecosm.com
State New
Headers show
  • Python API for target connections, and packet sending
Related show

Commit Message

Andrew Burgess Sept. 11, 2021, 4:03 p.m.
In a later commit I will add a Python API to access the 'maint packet'
functionality, that is, sending a user specified packet to the target.

To make implementing this easier, this commit refactors how this
command is currently implemented so that the packet_command function
is now global.

The new global send_remote_packet function takes an object that is an
implementation of an abstract interface.  Two functions within this
interface are then called, one just before a packet is sent to the
remote target, and one when the reply has been received from the
remote target.  Using an interface object in this way allows (1) for
the error checking to be done before the first callback is made, this
means we only print out what packet it being sent once we know we are
going to actually send it, and (2) we don't need to make a copy of the
reply if all we want to do is print it.

The only user visible changes after this commit are the error
messages, which I've changed to be less 'maint packet' command
focused, this will make them (I hope) better for when
send_remote_packet can be called from Python code.

So:      "command can only be used with remote target"
Becomes: "packets can only be sent to a remote target"

And:     "remote-packet command requires packet text as argument"
Becomes: "a remote packet must not be empty"
 gdb/remote.c | 67 +++++++++++++++++++++++++++++++++++-----------------
 gdb/remote.h | 34 ++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 22 deletions(-)



diff --git a/gdb/remote.c b/gdb/remote.c
index b6da6b086a2..21a7ff18442 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -956,8 +956,6 @@  class remote_target : public process_stratum_target
   bool vcont_r_supported ();
-  void packet_command (const char *args, int from_tty);
 private: /* data fields */
   /* The remote state.  Don't reference this directly.  Use the
@@ -11605,34 +11603,59 @@  remote_target::memory_map ()
   return result;
-static void
-packet_command (const char *args, int from_tty)
+/* Set of callbacks used to implement the 'maint packet' command.  */
+struct cli_packet_command_callbacks : public send_remote_packet_callbacks
-  remote_target *remote = get_current_remote_target ();
+  /* Called before the packet is sent.  PACKET_STR is the packet content
+     before the protocol specific prefix, suffix, and escaping is added.  */
-  if (remote == nullptr)
-    error (_("command can only be used with remote target"));
+  void sending (const char *packet_str) override
+  {
+    puts_filtered ("sending: ");
+    print_packet (packet_str);
+    puts_filtered ("\n");
+  }
-  remote->packet_command (args, from_tty);
+  /* Called with BUF, the reply from the remote target.  */
+  void received (const gdb::char_vector &buf) override
+  {
+    puts_filtered ("received: ");
+    print_packet (buf.data ());
+    puts_filtered ("\n");
+  }
+/* See remote.h.  */
-remote_target::packet_command (const char *args, int from_tty)
+send_remote_packet (const char *packet_str,
+		    send_remote_packet_callbacks *callbacks)
-  if (!args)
-    error (_("remote-packet command requires packet text as argument"));
+  if (packet_str == nullptr || *packet_str == '\0')
+    error (_("a remote packet must not be empty"));
-  puts_filtered ("sending: ");
-  print_packet (args);
-  puts_filtered ("\n");
-  putpkt (args);
+  remote_target *remote = get_current_remote_target ();
+  if (remote == nullptr)
+    error (_("packets can only be sent to a remote target"));
-  remote_state *rs = get_remote_state ();
+  callbacks->sending (packet_str);
-  getpkt (&rs->buf, 0);
-  puts_filtered ("received: ");
-  print_packet (rs->buf.data ());
-  puts_filtered ("\n");
+  remote->putpkt (packet_str);
+  remote_state *rs = remote->get_remote_state ();
+  remote->getpkt (&rs->buf, 0);
+  callbacks->received (rs->buf);
+/* Entry point for the 'maint packet' command.  */
+static void
+cli_packet_command (const char *args, int from_tty)
+  cli_packet_command_callbacks cb;
+  send_remote_packet (args, &cb);
 #if 0
@@ -14911,7 +14934,7 @@  Argument is a single section name (default: all loaded sections).\n\
 To compare only read-only loaded sections, specify the -r option."),
-  add_cmd ("packet", class_maintenance, packet_command, _("\
+  add_cmd ("packet", class_maintenance, cli_packet_command, _("\
 Send an arbitrary packet to a remote target.\n\
    maintenance packet TEXT\n\
 If GDB is talking to an inferior via the GDB serial protocol, then\n\
diff --git a/gdb/remote.h b/gdb/remote.h
index 46bfa01fc79..cd91be8decb 100644
--- a/gdb/remote.h
+++ b/gdb/remote.h
@@ -78,4 +78,38 @@  extern int remote_register_number_and_offset (struct gdbarch *gdbarch,
 extern void remote_notif_get_pending_events (remote_target *remote,
 					     struct notif_client *np);
 extern bool remote_target_is_non_stop_p (remote_target *t);
+/* An abstract class that represents the set of callbacks that are made
+   from the send_remote_packet function (declared below).  */
+struct send_remote_packet_callbacks
+  /* The SENDING callback is called once send_remote_packet has performed
+     its error checking and setup, just before the packet is sent to the
+     remote target.  PACKET_STR is the content of the packet that will be
+     sent (before any of the protocol specific prefix, suffix, or escaping
+     is applied).  */
+  virtual void sending (const char *packet_str) = 0;
+  /* The RECEIVED callback is called once a reply has been received from
+     the remote target.  The content of the reply is in BUF which can't be
+     modified, and which is not guaranteed to remain valid after the
+     RECEIVED call has returned.  If you need to preserve the contents of
+     BUF then a copy should be taken.  */
+  virtual void received (const gdb::char_vector &buf) = 0;
+/* Send PACKET_STR the current remote target.  If PACKET_STR is nullptr, or
+   is the empty string, then an error is thrown.  If the current target is
+   not a remote target then an error is thrown.
+   Calls CALLBACKS->sending() just before the packet is sent to the remote
+   target, and calls CALLBACKS->received() with the reply once this is
+   received from the remote target.  */
+extern void send_remote_packet (const char *packet_str,
+				send_remote_packet_callbacks *callbacks);