public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 3/3] gdbserver: allow for general 'monitor set debug COMPONENT VALUE' use
Date: Tue,  7 Nov 2023 18:03:23 +0000	[thread overview]
Message-ID: <9257bb42888e44e8d7d63f562babdd404fd766b0.1699379375.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1699379375.git.aburgess@redhat.com>

Building on the last commit, which added a general --debug=COMPONENT
option to the gdbserver command line, this commit extends the monitor
command to allow for general:

  (gdb) monitor set debug COMPONENT 0|1

style commands.  Just like with the previous commit, the COMPONENT can
be any one of threads, remote, event-loop, and correspond to the same
set of global debug flags.

While on the command line it is possible to do:

  --debug=remote,event-loop,threads

the components have to be entered one at a time with the monitor
command, and also, the 'all' component (see previous commit) is not
supported.  This makes the monitor command usage similar to how GDB's
normal 'set debug ...'  commands work.

I've retained the existing monitor command for backwards
compatibility, though I have removed them from the monitor help
output.
---
 gdb/doc/gdb.texinfo                     | 16 ++++-
 gdb/testsuite/gdb.server/server-mon.exp |  6 ++
 gdbserver/server.cc                     | 79 +++++++++++++++++++++++--
 3 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d3f088f6505..87993ac924a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -23804,11 +23804,23 @@
 @itemx monitor set debug 1
 Disable or enable general debugging messages.
 
-@item monitor set remote-debug 0
-@itemx monitor set remote-debug 1
+@item monitor set debug threads 0
+@itemx monitor set debug threads 1
+Disable or enable specific debugging messages associated thread
+handling in gdbserver.  Currently this category also includes
+additional debug output not specifically related to thread handling,
+this could change in future releases of gdbserver.
+
+@item monitor set debug remote 0
+@itemx monitor set debug remote 1
 Disable or enable specific debugging messages associated with the remote
 protocol (@pxref{Remote Protocol}).
 
+@item monitor set debug event-loop 0
+@itemx monitor set debug event-loop 1
+Disable or enable specific debugging messages associated with
+gdbserver's event-loop.
+
 @item monitor set debug-file filename
 @itemx monitor set debug-file
 Send any debug output to the given file, or to stderr.
diff --git a/gdb/testsuite/gdb.server/server-mon.exp b/gdb/testsuite/gdb.server/server-mon.exp
index 728cc84a9e0..b2ad07254be 100644
--- a/gdb/testsuite/gdb.server/server-mon.exp
+++ b/gdb/testsuite/gdb.server/server-mon.exp
@@ -46,6 +46,12 @@ gdb_test "monitor" "Unknown monitor command.*Protocol error.*"
 
 gdb_test "monitor set debug 1" "Debug output enabled\\."
 gdb_test "monitor set debug 0" "Debug output disabled\\."
+gdb_test "monitor set debug remote 1" "Debug output for 'remote' enabled\\."
+gdb_test "monitor set debug remote 0" "Debug output for 'remote' disabled\\."
+gdb_test "monitor set debug event-loop 1" "Debug output for 'event-loop' enabled\\."
+gdb_test "monitor set debug event-loop 0" "Debug output for 'event-loop' disabled\\."
+gdb_test "monitor set debug threads 1" "Debug output for 'threads' enabled\\."
+gdb_test "monitor set debug threads 0" "Debug output for 'threads' disabled\\."
 gdb_test "monitor set remote-debug 1" "Protocol debug output enabled\\."
 gdb_test "monitor set remote-debug 0" "Protocol debug output disabled\\."
 gdb_test "monitor set debug-format all" \
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index b954507dd6b..46625ce5556 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -1024,12 +1024,11 @@ monitor_show_help (void)
   monitor_output ("The following monitor commands are supported:\n");
   monitor_output ("  set debug <0|1>\n");
   monitor_output ("    Enable general debugging messages\n");
+  monitor_output ("  set debug COMPONENT <0|1>\n");
+  monitor_output ("    Enable debugging messages for COMPONENT, which is\n");
+  monitor_output ("    one of: threads, remote, event-loop.\n");
   monitor_output ("  set debug-hw-points <0|1>\n");
   monitor_output ("    Enable h/w breakpoint/watchpoint debugging messages\n");
-  monitor_output ("  set remote-debug <0|1>\n");
-  monitor_output ("    Enable remote protocol debugging messages\n");
-  monitor_output ("  set event-loop-debug <0|1>\n");
-  monitor_output ("    Enable event loop debugging messages\n");
   monitor_output ("  set debug-format option1[,option2,...]\n");
   monitor_output ("    Add additional information to debugging messages\n");
   monitor_output ("    Options: all, none, timestamp\n");
@@ -1395,6 +1394,66 @@ parse_debug_options (const char *options)
     }
 }
 
+/* Called from the 'monitor' command handler, to handle general 'set debug'
+   commands with the format: 'set debug COMPONENT VALUE' where VALUE is
+   either '0' or '1', and COMPONENT is the name of a supported debug
+   component, see parse_debug_options for the supported components.  MON
+   should point to the 'COMPONENT VALUE' part of the string, the 'set
+   debug' should already have been stripped off.
+
+   Return a string containing an error message if something goes wrong,
+   this error can be returned as part of the monitor command output.  If
+   everything goes correctly then the debug global will have been updated,
+   and an empty string is returned.  */
+
+static std::string
+handle_general_monitor_debug (const char *mon)
+{
+  mon = skip_spaces (mon);
+
+  if (*mon == '\0')
+    return "No debug component name found.\n";
+
+  /* Skip to next space.  */
+  const char *end = skip_to_space (mon);
+  std::string component (mon, end - mon);
+  if (component == "all")
+    return "Component 'all' is not supported.\n";
+  if (component.find (',') != component.npos || component[0] == '-'
+      || component[0] == '+')
+    return "Invalid character found in debug component name.\n";
+
+  /* Skip any spaces.  */
+  const char *value = skip_spaces (end);
+
+  /* Check next character is either '0' or '1'.  */
+  if (*value != '0' && *value != '1')
+    return "Invalid value for set debug.\n";
+  bool enable = *value == '1';
+  const char *after_value = skip_spaces (value + 1);
+  if (*after_value != '\0')
+    return "junk found at end of set debug command";
+
+  /* Build a string with either a '+' or '-' prefix and pass this to
+     parse_debug_options.  */
+  std::string action_str = std::string (enable ? "+" : "-") + component;
+
+  try
+    {
+      parse_debug_options (action_str.c_str ());
+      std::string msg = string_printf ("Debug output for '%s' %s.\n",
+				       component.c_str (),
+				       enable ? "enabled" : "disabled");
+      monitor_output (msg.c_str ());
+    }
+  catch (const gdb_exception_error &exception)
+    {
+      return string_printf ("Error: %s\n", exception.what ());
+    }
+
+  return {};
+}
+
 /* Handle monitor commands not handled by target-specific handlers.  */
 
 static void
@@ -1410,6 +1469,18 @@ handle_monitor_command (char *mon, char *own_buf)
       debug_threads = false;
       monitor_output ("Debug output disabled.\n");
     }
+  else if (startswith (mon, "set debug "))
+    {
+      std::string error_msg
+	= handle_general_monitor_debug (mon + sizeof ("set debug ") - 1);
+
+      if (!error_msg.empty ())
+	{
+	  monitor_output (error_msg.c_str ());
+	  monitor_show_help ();
+	  write_enn (own_buf);
+	}
+    }
   else if (strcmp (mon, "set debug-hw-points 1") == 0)
     {
       show_debug_regs = 1;
-- 
2.25.4


  parent reply	other threads:[~2023-11-07 18:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07 18:03 [PATCH 0/3] Improve debug output support in gdbserver Andrew Burgess
2023-11-07 18:03 ` [PATCH 1/3] gdbserver: cleanup monitor_show_help Andrew Burgess
2023-11-22 15:21   ` Andrew Burgess
2023-11-07 18:03 ` [PATCH 2/3] gdbserver: allow the --debug command line option to take a value Andrew Burgess
2023-11-07 19:41   ` Eli Zaretskii
2023-11-30 18:31     ` Andrew Burgess
2023-11-30 19:20       ` Eli Zaretskii
2023-12-04 15:57         ` Andrew Burgess
2023-12-04 16:21           ` Eli Zaretskii
2023-12-05 10:17             ` Andrew Burgess
2023-12-05 13:06               ` Eli Zaretskii
2023-11-07 18:03 ` Andrew Burgess [this message]
2023-11-17 14:43 ` [PATCH 0/3] Improve debug output support in gdbserver Tom Tromey
2023-11-17 14:55 ` Tom Tromey
2023-11-30 18:44 ` [PATCHv2 " Andrew Burgess
2023-11-30 18:44   ` [PATCHv2 1/3] gdb: fix GDB_DEBUG and GDBSERVER_DEBUG Makefile variables Andrew Burgess
2023-11-30 18:44   ` [PATCHv2 2/3] gdbserver: allow the --debug command line option to take a value Andrew Burgess
2023-11-30 18:44   ` [PATCHv2 3/3] gdbserver: allow for general 'monitor set debug COMPONENT VALUE' use Andrew Burgess
2023-11-30 19:30     ` Eli Zaretskii
2023-12-01 18:02   ` [PATCHv2 0/3] Improve debug output support in gdbserver Tom Tromey
2023-12-08 18:03     ` 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=9257bb42888e44e8d7d63f562babdd404fd766b0.1699379375.git.aburgess@redhat.com \
    --to=aburgess@redhat.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).