public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Subject: [PATCHv5 2/4] gdb: make packet_command function available outside remote.c
Date: Fri, 22 Oct 2021 18:10:09 +0100	[thread overview]
Message-ID: <e625e35060c3011b37057d80ad58fb1e2827253f.1634922528.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1634922528.git.andrew.burgess@embecosm.com>

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 0fb42753596..57f35892527 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
@@ -11593,34 +11591,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.  */
 
 void
-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
@@ -14899,7 +14922,7 @@ Argument is a single section name (default: all loaded sections).\n\
 To compare only read-only loaded sections, specify the -r option."),
 	   &cmdlist);
 
-  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);
+
 #endif
-- 
2.25.4


  parent reply	other threads:[~2021-10-22 17:10 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11 16:03 [PATCH 0/3] Python API for target connections, and packet sending Andrew Burgess
2021-09-11 16:03 ` [PATCH 1/3] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-09-11 16:19   ` Eli Zaretskii
2021-09-11 16:03 ` [PATCH 2/3] gdb: make packet_command function available outside remote.c Andrew Burgess
2021-09-11 16:03 ` [PATCH 3/3] gdb/python: add TargetConnection.send_remote_packet method Andrew Burgess
2021-09-11 16:10   ` Eli Zaretskii
2021-10-18  9:45 ` [PATCHv2 0/3] Python API for target connections, and packet sending Andrew Burgess
2021-10-18  9:45   ` [PATCHv2 1/3] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-10-18 12:44     ` Eli Zaretskii
2021-10-18 15:53     ` Simon Marchi
2021-10-18  9:45   ` [PATCHv2 2/3] gdb: make packet_command function available outside remote.c Andrew Burgess
2021-10-18  9:45   ` [PATCHv2 3/3] gdb/python: add TargetConnection.send_remote_packet method Andrew Burgess
2021-10-18 12:57     ` Eli Zaretskii
2021-10-18 15:46     ` Simon Marchi
2021-10-19 10:17   ` [PATCHv3 0/3] Python API for target connections, and packet sending Andrew Burgess
2021-10-19 10:17     ` [PATCHv3 1/3] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-10-19 12:26       ` Eli Zaretskii
2021-10-20 22:33       ` Lancelot SIX
2021-10-21  2:00       ` Simon Marchi
2021-10-19 10:17     ` [PATCHv3 2/3] gdb: make packet_command function available outside remote.c Andrew Burgess
2021-10-21  2:23       ` Simon Marchi
2021-10-19 10:17     ` [PATCHv3 3/3] gdb/python: add gdb.RemoteTargetConnection.send_packet Andrew Burgess
2021-10-19 12:28       ` Eli Zaretskii
2021-10-21  2:43       ` Simon Marchi
2021-10-22 11:08         ` Andrew Burgess
2021-10-22 11:18           ` Simon Marchi
2021-10-22 17:11             ` Andrew Burgess
2021-10-22 10:58     ` [PATCHv4 0/4] Python API for target connections, and packet sending Andrew Burgess
2021-10-22 10:58       ` [PATCHv4 1/4] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-10-22 10:58       ` [PATCHv4 2/4] gdb: make packet_command function available outside remote.c Andrew Burgess
2021-10-22 10:58       ` [PATCHv4 3/4] gdb/python: add gdb.RemoteTargetConnection.send_packet Andrew Burgess
2021-10-22 10:58       ` [PATCHv4 4/4] gdb: handle binary data in 'maint packet' and RemoteTargetConnection.send_packet Andrew Burgess
2021-10-22 17:10       ` [PATCHv5 0/4] Python API for target connections, and packet sending Andrew Burgess
2021-10-22 17:10         ` [PATCHv5 1/4] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-10-22 17:10         ` Andrew Burgess [this message]
2021-10-22 17:10         ` [PATCHv5 3/4] gdb/python: add gdb.RemoteTargetConnection.send_packet Andrew Burgess
2021-11-15  2:08           ` Simon Marchi
2021-11-15  9:25             ` Andrew Burgess
2021-11-15 13:16               ` Simon Marchi
2021-10-22 17:10         ` [PATCHv5 4/4] gdb: handle binary data in 'maint packet' and RemoteTargetConnection.send_packet Andrew Burgess
2021-11-15  2:44           ` Simon Marchi
2021-11-09 10:04         ` [PATCHv5 0/4] Python API for target connections, and packet sending Andrew Burgess
2021-11-15 17:40         ` [PATCHv6 0/3] " Andrew Burgess
2021-11-15 17:40           ` [PATCHv6 1/3] gdb/python: introduce gdb.TargetConnection object type Andrew Burgess
2021-11-15 17:40           ` [PATCHv6 2/3] gdb: make packet_command function available outside remote.c Andrew Burgess
2021-11-15 17:40           ` [PATCHv6 3/3] gdb/python: add gdb.RemoteTargetConnection.send_packet Andrew Burgess
2021-11-15 18:42             ` Eli Zaretskii
2021-11-15 19:38               ` Simon Marchi
2021-11-15 19:29             ` Simon Marchi
2021-11-16 12:48             ` Andrew Burgess
2021-11-16 15:10               ` Simon Marchi
2021-11-30 12:15                 ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e625e35060c3011b37057d80ad58fb1e2827253f.1634922528.git.andrew.burgess@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).