public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 06/25] Thread options & clone events (core + remote)
Date: Mon, 20 Jun 2022 23:54:00 +0100	[thread overview]
Message-ID: <20220620225419.382221-7-pedro@palves.net> (raw)
In-Reply-To: <20220620225419.382221-1-pedro@palves.net>

A previous patch taught GDB about a new TARGET_WAITKIND_THREAD_CLONED
event kind, and made the Linux target report clone events.

A following patch will teach Linux GDBserver to do the same thing.

However, for remote debugging, it wouldn't be ideal for GDBserver to
report every clone event to GDB, when GDB only cares about such events
in some specific situations.  Reporting clone events all the time
would be potentially chatty.  We don't enable thread create/exit
events all the time for the same reason.  Instead we have the
QThreadEvents packet.  QThreadEvents is target-wide, though.

This patch makes GDB instead explicitly request that the target
reports clone events or not, on a per-thread basis.

In order to be able to do that with GDBserver, we need a new remote
protocol feature.  Since a following patch will want to enable thread
exit events on per-thread basis too, the packet introduced here is
more generic than just for clone events.  It lets you enable/disable a
set of options at once, modelled on Linux ptrace's PTRACE_SETOPTIONS.

IOW, this commit introduces a new QThreadOptions packet, that lets you
specify a set of per-thread event options you want to enable.  The
packet accepts a list of options/thread-id pairs, similarly to vCont,
processed left to right, with the options field being a number
interpreted as a bit mask of options.  The only option defined in this
commit is GDB_TO_CLONE (0x1), which ask the remote target to report
clone events.  Another patch later in the series will introduce
another option.

For example, this packet sets option "1" (clone events) on thread
p1000.2345:

  QThreadOptions;1:p1000.2345

and this clears options for all threads of process 1000, and then sets
option "1" (clone events) on thread p1000.2345:

  QThreadOptions;0:p1000.-1;1:p1000.2345

This clears options of all threads of all processes:

  QThreadOptions;0

The target reports the set of supported options by including
"QThreadOptions=<supported options>" in its qSupported response.

infrun is then tweaked to enable GDB_TO_CLONE when stepping over a
breakpoint.

Similarly to PTRACE_SETOPTIONS, fork/vfork/clone children inherit
their parent's thread options.

Documentation for this new remote protocol feature is included in a
documentation patch later in the series.

Change-Id: If271f20320d864f074d8ac0d531cc1a323da847f
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19675
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27830
---
 gdb/gdbthread.h              |  19 ++++++
 gdb/infrun.c                 |  60 +++++++++++++++++
 gdb/process-stratum-target.c |   4 +-
 gdb/remote.c                 | 119 ++++++++++++++++++++++++++++++++-
 gdb/target-debug.h           |   2 +
 gdb/target-delegates.c       |  54 +++++++++++++++
 gdb/target.c                 |  32 +++++++++
 gdb/target.h                 |  15 +++++
 gdb/target/target.h          |  10 +++
 gdbserver/gdbthread.h        |   3 +
 gdbserver/server.cc          | 124 +++++++++++++++++++++++++++++++++++
 gdbserver/target.cc          |   6 ++
 gdbserver/target.h           |   9 +++
 13 files changed, 454 insertions(+), 3 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 1a33eb61221..a73d3c4eda0 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -28,6 +28,7 @@ struct symtab;
 #include "ui-out.h"
 #include "btrace.h"
 #include "target/waitstatus.h"
+#include "target/target.h"
 #include "cli/cli-utils.h"
 #include "gdbsupport/refcounted-object.h"
 #include "gdbsupport/common-gdbthread.h"
@@ -470,6 +471,20 @@ class thread_info : public refcounted_object,
     m_thread_fsm = std::move (fsm);
   }
 
+  /* Record the thread options last set for this thread.  */
+
+  void set_thread_options (gdb_thread_options thread_options)
+  {
+    m_thread_options = thread_options;
+  }
+
+  /* Get the thread options last set for this thread.  */
+
+  gdb_thread_options thread_options () const
+  {
+    return m_thread_options;
+  }
+
   int current_line = 0;
   struct symtab *current_symtab = NULL;
 
@@ -577,6 +592,10 @@ class thread_info : public refcounted_object,
      left to do for the thread's execution command after the target
      stops.  Several execution commands use it.  */
   std::unique_ptr<struct thread_fsm> m_thread_fsm;
+
+  /* The thread options as last set with a call to
+     target_set_thread_options.  */
+  gdb_thread_options m_thread_options;
 };
 
 using thread_info_resumed_with_pending_wait_status_node
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 9f09373c1db..78ba81f530f 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1512,6 +1512,16 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_finish for details.  */
 
+/* Return true if THREAD is doing a displaced step.  */
+
+static bool
+displaced_step_in_progress_thread (thread_info *thread)
+{
+  gdb_assert (thread != NULL);
+
+  return thread->displaced_step_state.in_progress ();
+}
+
 /* Return true if INF has a thread doing a displaced step.  */
 
 static bool
@@ -1806,6 +1816,28 @@ displaced_step_prepare (thread_info *thread)
   return status;
 }
 
+/* Maybe disable thread-{cloned,created,exited} event reporting after
+   a step-over (either in-line or displaced) finishes.  */
+
+static void
+update_thread_events_after_step_over (thread_info *event_thread)
+{
+  if (target_supports_set_thread_options (0))
+    {
+      /* We can control per-thread options.  Disable events for the
+	 event thread.  */
+      target_set_thread_options (event_thread, 0);
+    }
+  else
+    {
+      /* We can only control the target-wide target_thread_events
+	 setting.  Disable it, but only if other threads don't need it
+	 enabled.  */
+      if (!displaced_step_in_progress_any_thread ())
+	target_thread_events (false);
+    }
+}
+
 /* If we displaced stepped an instruction successfully, adjust registers and
    memory to yield the same effect the instruction would have had if we had
    executed it at its original address, and return
@@ -1850,6 +1882,8 @@ displaced_step_finish (thread_info *event_thread,
   if (!displaced->in_progress ())
     return DISPLACED_STEP_FINISH_STATUS_OK;
 
+  update_thread_events_after_step_over (event_thread);
+
   gdb_assert (event_thread->inf->displaced_step_state.in_progress_count > 0);
   event_thread->inf->displaced_step_state.in_progress_count--;
 
@@ -2342,6 +2376,30 @@ do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
   else
     target_pass_signals (signal_pass);
 
+  /* Request that the target report thread-{created,cloned} events in
+     the following situations:
+
+     - If we are performing an in-line step-over-breakpoint, then we
+       will remove a breakpoint from the target and only run the
+       current thread.  We don't want any new thread (spawned by the
+       step) to start running, as it might miss the breakpoint.
+
+     - If we are stepping over a breakpoint out of line (displaced
+       stepping) then we won't remove a breakpoint from the target,
+       but, if the step spawns a new clone thread, then we will need
+       to fixup the $pc address in the clone child too, so we need it
+       to start stopped.
+  */
+  if (step_over_info_valid_p ()
+      || displaced_step_in_progress_thread (tp))
+    {
+      gdb_thread_options options = GDB_TO_CLONE;
+      if (target_supports_set_thread_options (options))
+	target_set_thread_options (tp, options);
+      else
+	target_thread_events (true);
+    }
+
   infrun_debug_printf ("resume_ptid=%s, step=%d, sig=%s",
 		       resume_ptid.to_string ().c_str (),
 		       step, gdb_signal_to_symbol_string (sig));
@@ -6011,6 +6069,8 @@ finish_step_over (struct execution_control_state *ecs)
 	 back an event.  */
       gdb_assert (ecs->event_thread->control.trap_expected);
 
+      update_thread_events_after_step_over (ecs->event_thread);
+
       clear_step_over_info ();
     }
 
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index 50bb13e4b83..fddb8dbdb87 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -101,6 +101,7 @@ process_stratum_target::follow_exec (inferior *follow_inf, ptid_t ptid,
 	 after that, at its discretion.  */
       follow_inf->push_target (orig_inf->process_target ());
       thread_info *t = add_thread (follow_inf->process_target (), ptid);
+      t->set_thread_options (inferior_thread ()->thread_options ());
 
       /* Leave the new inferior / thread as the current inferior / thread.  */
       switch_to_thread (t);
@@ -118,7 +119,8 @@ process_stratum_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
   if (child_inf != nullptr)
     {
       child_inf->push_target (this);
-      add_thread_silent (this, child_ptid);
+      thread_info *child = add_thread_silent (this, child_ptid);
+      child->set_thread_options (inferior_thread ()->thread_options ());
     }
 }
 
diff --git a/gdb/remote.c b/gdb/remote.c
index c0bd075e9e5..20f2b7f399d 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -381,6 +381,10 @@ class remote_state
      this can go away.  */
   int wait_forever_enabled_p = 1;
 
+  /* The set of thread options the target reported it supports, via
+     qSupported.  */
+  gdb_thread_options supported_thread_options = 0;
+
 private:
   /* Mapping of remote protocol data for each gdbarch.  Usually there
      is only one entry here, though we may see more with stubs that
@@ -543,6 +547,9 @@ class remote_target : public process_stratum_target
 
   void thread_events (int) override;
 
+  bool supports_set_thread_options (gdb_thread_options) override;
+  void set_thread_options (thread_info *, gdb_thread_options) override;
+
   int can_do_single_step () override;
 
   void terminal_inferior () override;
@@ -833,6 +840,9 @@ class remote_target : public process_stratum_target
 
   void remote_packet_size (const protocol_feature *feature,
 			   packet_support support, const char *value);
+  void remote_supported_thread_options (const protocol_feature *feature,
+					enum packet_support support,
+					const char *value);
 
   void remote_serial_quit_handler ();
 
@@ -2167,6 +2177,9 @@ enum {
   /* Support for the QThreadEvents packet.  */
   PACKET_QThreadEvents,
 
+  /* Support for the QThreadOptions packet.  */
+  PACKET_QThreadOptions,
+
   /* Support for multi-process extensions.  */
   PACKET_multiprocess_feature,
 
@@ -5289,7 +5302,8 @@ remote_supported_packet (remote_target *remote,
 
 void
 remote_target::remote_packet_size (const protocol_feature *feature,
-				   enum packet_support support, const char *value)
+				   enum packet_support support,
+				   const char *value)
 {
   struct remote_state *rs = get_remote_state ();
 
@@ -5326,6 +5340,49 @@ remote_packet_size (remote_target *remote, const protocol_feature *feature,
   remote->remote_packet_size (feature, support, value);
 }
 
+void
+remote_target::remote_supported_thread_options (const protocol_feature *feature,
+						enum packet_support support,
+						const char *value)
+{
+  struct remote_state *rs = get_remote_state ();
+
+  remote_protocol_packets[feature->packet].support = support;
+
+  if (support != PACKET_ENABLE)
+    return;
+
+  if (value == nullptr || *value == '\0')
+    {
+      warning (_("Remote target reported \"%s\" without supported options."),
+	       feature->name);
+      return;
+    }
+
+  ULONGEST options = 0;
+  const char *p = unpack_varlen_hex (value, &options);
+
+  if (*p != '\0')
+    {
+      warning (_("Remote target reported \"%s\" with "
+		 "bad thread options: \"%s\"."),
+	       feature->name, value);
+      return;
+    }
+
+  /* Record the set of supported options.  */
+  rs->supported_thread_options = (gdb_thread_option) options;
+}
+
+static void
+remote_supported_thread_options (remote_target *remote,
+				 const protocol_feature *feature,
+				 enum packet_support support,
+				 const char *value)
+{
+  remote->remote_supported_thread_options (feature, support, value);
+}
+
 static const struct protocol_feature remote_protocol_features[] = {
   { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
   { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
@@ -5428,6 +5485,8 @@ static const struct protocol_feature remote_protocol_features[] = {
     PACKET_Qbtrace_conf_pt_size },
   { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
   { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
+  { "QThreadOptions", PACKET_DISABLE, remote_supported_thread_options,
+    PACKET_QThreadOptions },
   { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
   { "memory-tagging", PACKET_DISABLE, remote_supported_packet,
     PACKET_memory_tagging_feature },
@@ -5522,6 +5581,9 @@ remote_target::remote_query_supported ()
       if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
 	remote_query_supported_append (&q, "QThreadEvents+");
 
+      if (packet_set_cmd_state (PACKET_QThreadOptions) != AUTO_BOOLEAN_FALSE)
+	remote_query_supported_append (&q, "QThreadOptions+");
+
       if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
 	remote_query_supported_append (&q, "no-resumed+");
 
@@ -6119,7 +6181,8 @@ remote_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
 void
 remote_target::follow_clone (ptid_t child_ptid)
 {
-  remote_add_thread (child_ptid, false, false, false);
+  thread_info *child = remote_add_thread (child_ptid, false, false, false);
+  child->set_thread_options (inferior_thread ()->thread_options ());
 }
 
 /* Target follow-exec function for remote targets.  Save EXECD_PATHNAME
@@ -14603,6 +14666,55 @@ remote_target::thread_events (int enable)
     }
 }
 
+/* Implementation of the supports_set_thread_options target
+   method.  */
+
+bool
+remote_target::supports_set_thread_options (gdb_thread_options options)
+{
+  remote_state *rs = get_remote_state ();
+  return (packet_support (PACKET_QThreadOptions) == PACKET_ENABLE
+	  && (rs->supported_thread_options & options) == options);
+}
+
+/* Implementation of the set_thread_options target method.  */
+
+void
+remote_target::set_thread_options (thread_info *tp, gdb_thread_options options)
+{
+  struct remote_state *rs = get_remote_state ();
+
+  char *p = rs->buf.data ();
+  char *endp = p + get_remote_packet_size ();
+
+  strcpy (p, "QThreadOptions;");
+  p += strlen (p);
+  p += xsnprintf (p, endp - p, "%s", phex_nz (options, sizeof (options)));
+  if (tp->ptid != magic_null_ptid)
+    {
+      *p++ = ':';
+      p = write_ptid (p, endp, tp->ptid);
+    }
+  *p++ = '\0';
+
+  putpkt (rs->buf);
+  getpkt (&rs->buf, 0);
+
+  switch (packet_ok (rs->buf,
+		     &remote_protocol_packets[PACKET_QThreadOptions]))
+    {
+    case PACKET_OK:
+      if (strcmp (rs->buf.data (), "OK") != 0)
+	error (_("Remote refused setting thread options: %s"), rs->buf.data ());
+      break;
+    case PACKET_ERROR:
+      error (_("Remote failure reply: %s"), rs->buf.data ());
+    case PACKET_UNKNOWN:
+      gdb_assert_not_reached ("PACKET_UNKNOWN");
+      break;
+    }
+}
+
 static void
 show_remote_cmd (const char *args, int from_tty)
 {
@@ -15386,6 +15498,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
 			 "QThreadEvents", "thread-events", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadOptions],
+			 "QThreadOptions", "thread-options", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
 			 "N stop reply", "no-resumed-stop-reply", 0);
 
diff --git a/gdb/target-debug.h b/gdb/target-debug.h
index c2b1db1ce8e..0fe5e5e6022 100644
--- a/gdb/target-debug.h
+++ b/gdb/target-debug.h
@@ -176,6 +176,8 @@
   target_debug_do_print (X.get ())
 #define target_debug_print_target_waitkind(X) \
   target_debug_do_print (pulongest (X))
+#define target_debug_print_gdb_thread_options(X) \
+  target_debug_do_print (pulongest (X))
 
 static void
 target_debug_print_struct_target_waitstatus_p (struct target_waitstatus *status)
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index f58fbe44094..c09e6eba922 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -106,6 +106,8 @@ struct dummy_target : public target_ops
   int async_wait_fd () override;
   bool has_pending_events () override;
   void thread_events (int arg0) override;
+  bool supports_set_thread_options (gdb_thread_options arg0) override;
+  void set_thread_options (thread_info *arg0, gdb_thread_options arg1) override;
   bool supports_non_stop () override;
   bool always_non_stop_p () override;
   int find_memory_regions (find_memory_region_ftype arg0, void *arg1) override;
@@ -281,6 +283,8 @@ struct debug_target : public target_ops
   int async_wait_fd () override;
   bool has_pending_events () override;
   void thread_events (int arg0) override;
+  bool supports_set_thread_options (gdb_thread_options arg0) override;
+  void set_thread_options (thread_info *arg0, gdb_thread_options arg1) override;
   bool supports_non_stop () override;
   bool always_non_stop_p () override;
   int find_memory_regions (find_memory_region_ftype arg0, void *arg1) override;
@@ -2272,6 +2276,56 @@ debug_target::thread_events (int arg0)
   gdb_puts (")\n", gdb_stdlog);
 }
 
+bool
+target_ops::supports_set_thread_options (gdb_thread_options arg0)
+{
+  return this->beneath ()->supports_set_thread_options (arg0);
+}
+
+bool
+dummy_target::supports_set_thread_options (gdb_thread_options arg0)
+{
+  return false;
+}
+
+bool
+debug_target::supports_set_thread_options (gdb_thread_options arg0)
+{
+  bool result;
+  gdb_printf (gdb_stdlog, "-> %s->supports_set_thread_options (...)\n", this->beneath ()->shortname ());
+  result = this->beneath ()->supports_set_thread_options (arg0);
+  gdb_printf (gdb_stdlog, "<- %s->supports_set_thread_options (", this->beneath ()->shortname ());
+  target_debug_print_gdb_thread_options (arg0);
+  gdb_puts (") = ", gdb_stdlog);
+  target_debug_print_bool (result);
+  gdb_puts ("\n", gdb_stdlog);
+  return result;
+}
+
+void
+target_ops::set_thread_options (thread_info *arg0, gdb_thread_options arg1)
+{
+  this->beneath ()->set_thread_options (arg0, arg1);
+}
+
+void
+dummy_target::set_thread_options (thread_info *arg0, gdb_thread_options arg1)
+{
+  tcomplain ();
+}
+
+void
+debug_target::set_thread_options (thread_info *arg0, gdb_thread_options arg1)
+{
+  gdb_printf (gdb_stdlog, "-> %s->set_thread_options (...)\n", this->beneath ()->shortname ());
+  this->beneath ()->set_thread_options (arg0, arg1);
+  gdb_printf (gdb_stdlog, "<- %s->set_thread_options (", this->beneath ()->shortname ());
+  target_debug_print_thread_info_p (arg0);
+  gdb_puts (", ", gdb_stdlog);
+  target_debug_print_gdb_thread_options (arg1);
+  gdb_puts (")\n", gdb_stdlog);
+}
+
 bool
 target_ops::supports_non_stop ()
 {
diff --git a/gdb/target.c b/gdb/target.c
index d1ba229189f..52eee4b8140 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -4377,6 +4377,38 @@ target_thread_events (int enable)
   current_inferior ()->top_target ()->thread_events (enable);
 }
 
+/* See target.h.  */
+
+bool
+target_supports_set_thread_options (gdb_thread_options options)
+{
+  inferior *inf = current_inferior ();
+  return inf->top_target ()->supports_set_thread_options (options);
+}
+
+/* See target.h.  */
+
+void
+target_set_thread_options (thread_info *thr, gdb_thread_options options)
+{
+  gdb_assert (thr->inf->process_target ()
+	      == current_inferior ()->process_target ());
+
+  if (thr->thread_options () == options)
+    {
+      /* Avoid propagating the new options to the target if nothing
+	 would change.  This avoids redundant remote protocol
+	 packets.  */
+      return;
+    }
+
+  thr->set_thread_options (options);
+  current_inferior ()->top_target ()->set_thread_options (thr, options);
+  infrun_debug_printf ("[options for %s are now 0x%x]",
+		       target_pid_to_str (thr->ptid).c_str (),
+		       (unsigned) options);
+}
+
 /* Controls if targets can report that they can/are async.  This is
    just for maintainers to use when debugging gdb.  */
 bool target_async_permitted = true;
diff --git a/gdb/target.h b/gdb/target.h
index 1cab47147e3..b1447cc908c 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -730,6 +730,13 @@ struct target_ops
       TARGET_DEFAULT_RETURN (false);
     virtual void thread_events (int)
       TARGET_DEFAULT_IGNORE ();
+    /* Returns true if the target supports setting thread options
+       OPTIONS, false otherwise.  */
+    virtual bool supports_set_thread_options (gdb_thread_options options)
+      TARGET_DEFAULT_RETURN (false);
+    /* Set thread options for the specified thread.  */
+    virtual void set_thread_options (thread_info *, gdb_thread_options)
+      TARGET_DEFAULT_NORETURN (tcomplain ());
     /* This method must be implemented in some situations.  See the
        comment on 'can_run'.  */
     virtual bool supports_non_stop ()
@@ -1893,6 +1900,14 @@ extern void target_async (int enable);
 /* Enables/disables thread create and exit events.  */
 extern void target_thread_events (int enable);
 
+/* Returns true if the target supports setting thread options
+   OPTIONS.  */
+extern bool target_supports_set_thread_options (gdb_thread_options options);
+
+/* Sets TP's options.  */
+extern void target_set_thread_options (thread_info *tp,
+				       gdb_thread_options options);
+
 /* Whether support for controlling the target backends always in
    non-stop mode is enabled.  */
 extern enum auto_boolean target_non_stop_enabled;
diff --git a/gdb/target/target.h b/gdb/target/target.h
index a5b0dd3ed1a..cba98114b5d 100644
--- a/gdb/target/target.h
+++ b/gdb/target/target.h
@@ -22,9 +22,19 @@
 
 #include "target/waitstatus.h"
 #include "target/wait.h"
+#include "gdbsupport/enum-flags.h"
 
 /* This header is a stopgap until more code is shared.  */
 
+enum gdb_thread_option : unsigned
+{
+  /* Tell the target to report TARGET_WAITKIND_THREAD_CLONED events
+     for the thread.  */
+  GDB_TO_CLONE = 1 << 0,
+};
+
+DEF_ENUM_FLAGS_TYPE (enum gdb_thread_option, gdb_thread_options);
+
 /* Read LEN bytes of target memory at address MEMADDR, placing the
    results in GDB's memory at MYADDR.  Return zero for success,
    nonzero if any error occurs.  This function must be provided by
diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h
index 8b897e73d33..30040e8afb6 100644
--- a/gdbserver/gdbthread.h
+++ b/gdbserver/gdbthread.h
@@ -80,6 +80,9 @@ struct thread_info
 
   /* Branch trace target information for this thread.  */
   struct btrace_target_info *btrace = nullptr;
+
+  /* Thread options GDB requested with QThreadOptions.  */
+  gdb_thread_options thread_options = 0;
 };
 
 extern std::list<thread_info *> all_threads;
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 30a1ccbb367..2b78ba7292d 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -611,6 +611,17 @@ parse_store_memtags_request (char *request, CORE_ADDR *addr, size_t *len,
   return true;
 }
 
+/* Parse thread options starting at *P and return them.  On exit,
+   advance *P past the options.  */
+
+static gdb_thread_options
+parse_gdb_thread_options (const char **p)
+{
+  ULONGEST options = 0;
+  *p = unpack_varlen_hex (*p, &options);
+  return (gdb_thread_option) options;
+}
+
 /* Handle all of the extended 'Q' packets.  */
 
 static void
@@ -892,6 +903,109 @@ handle_general_set (char *own_buf)
       return;
     }
 
+  if (startswith (own_buf, "QThreadOptions;"))
+    {
+      const char *p = own_buf + strlen ("QThreadOptions");
+
+      gdb_thread_options supported_options;
+      if (!target_supports_set_thread_options (&supported_options))
+	{
+	  /* Something went wrong -- we don't support options, but GDB
+	     sent the packet anyway.  */
+	  write_enn (own_buf);
+	  return;
+	}
+
+      auto set_thread_options = [] (thread_info *thread,
+				    gdb_thread_options options)
+        {
+	  thread->thread_options = options;
+
+	  if (debug_threads)
+	    {
+	      debug_printf ("[options for %s are now 0x%x]\n",
+			    target_pid_to_str (ptid_of (thread)).c_str (),
+			    (unsigned) options);
+	    }
+	};
+
+      while (*p != '\0')
+	{
+	  p++;
+
+	  /* Read the options.  */
+
+	  gdb_thread_options options = parse_gdb_thread_options (&p);
+
+	  if ((options & ~supported_options) != 0)
+	    {
+	      /* GDB asked for an unknown or unsupported option, so
+		 error out.  */
+	      std::string err
+		= string_printf ("E.Unknown option requested: %s\n",
+				 hex_string (options));
+	      strcpy (own_buf, err.c_str ());
+	      return;
+	    }
+
+	  ptid_t ptid;
+
+	  if (p[0] == '\0')
+	    ptid = minus_one_ptid;
+	  else if (p[0] == ':')
+	    {
+	      const char *q;
+
+	      ptid = read_ptid (p + 1, &q);
+
+	      if (p == q)
+		{
+		  write_enn (own_buf);
+		  return;
+		}
+	      p = q;
+	      if (p[0] != ';' && p[0] != '\0')
+		{
+		  write_enn (own_buf);
+		  return;
+		}
+	    }
+	  else
+	    {
+	      write_enn (own_buf);
+	      return;
+	    }
+
+	  if (ptid != minus_one_ptid && ptid.lwp () != -1)
+	    {
+	      thread_info *thread = find_thread_ptid (ptid);
+	      if (thread == nullptr)
+		{
+		  std::string err
+		    = string_printf ("E.No such thread %s",
+				     ptid.to_string ().c_str ());
+		  strcpy (own_buf, err.c_str ());
+		  return;
+		}
+
+	      set_thread_options (thread, options);
+	    }
+	  else
+	    {
+	      for_each_thread ([&] (thread_info *thread)
+	        {
+		  if (!ptid.matches (ptid_of (thread)))
+		    return;
+
+		  set_thread_options (thread, options);
+		});
+	    }
+	}
+
+      write_ok (own_buf);
+      return;
+    }
+
   if (startswith (own_buf, "QStartupWithShell:"))
     {
       const char *value = own_buf + strlen ("QStartupWithShell:");
@@ -2364,6 +2478,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
 		cs.vCont_supported = 1;
 	      else if (feature == "QThreadEvents+")
 		;
+	      else if (feature == "QThreadOptions+")
+		;
 	      else if (feature == "no-resumed+")
 		{
 		  /* GDB supports and wants TARGET_WAITKIND_NO_RESUMED
@@ -2489,6 +2605,14 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
 
       strcat (own_buf, ";vContSupported+");
 
+      gdb_thread_options supported_options;
+      if (target_supports_set_thread_options (&supported_options))
+	{
+	  char *end_buf = own_buf + strlen (own_buf);
+	  sprintf (end_buf, ";QThreadOptions=%s",
+		   phex_nz (supported_options, sizeof (supported_options)));
+	}
+
       strcat (own_buf, ";QThreadEvents+");
 
       strcat (own_buf, ";no-resumed+");
diff --git a/gdbserver/target.cc b/gdbserver/target.cc
index adcfe6e7bcc..70fec6d2078 100644
--- a/gdbserver/target.cc
+++ b/gdbserver/target.cc
@@ -530,6 +530,12 @@ process_stratum_target::supports_vfork_events ()
   return false;
 }
 
+bool
+process_stratum_target::supports_set_thread_options (gdb_thread_options *)
+{
+  return false;
+}
+
 bool
 process_stratum_target::supports_exec_events ()
 {
diff --git a/gdbserver/target.h b/gdbserver/target.h
index 6c536a30778..33142363a02 100644
--- a/gdbserver/target.h
+++ b/gdbserver/target.h
@@ -277,6 +277,12 @@ class process_stratum_target
   /* Returns true if vfork events are supported.  */
   virtual bool supports_vfork_events ();
 
+  /* Returns true if the target supports setting thread options.  If
+     options are supported, write into SUPPORTED_OPTIONS the set of
+     supported options.  */
+  virtual bool supports_set_thread_options
+    (gdb_thread_options *supported_options);
+
   /* Returns true if exec events are supported.  */
   virtual bool supports_exec_events ();
 
@@ -529,6 +535,9 @@ int kill_inferior (process_info *proc);
 #define target_supports_vfork_events() \
   the_target->supports_vfork_events ()
 
+#define target_supports_set_thread_options(options) \
+  the_target->supports_set_thread_options (options)
+
 #define target_supports_exec_events() \
   the_target->supports_exec_events ()
 
-- 
2.36.0


  parent reply	other threads:[~2022-06-20 22:54 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-20 22:53 [PATCH 00/25] Step over thread clone and thread exit Pedro Alves
2022-06-20 22:53 ` [PATCH 01/25] Don't use pthread_mutex_t in gdb.base/step-over-clone.c Pedro Alves
2022-07-13 21:35   ` Pedro Alves
2022-06-20 22:53 ` [PATCH 02/25] displaced step: pass down target_waitstatus instead of gdb_signal Pedro Alves
2022-06-20 22:53 ` [PATCH 03/25] linux-nat: introduce pending_status_str Pedro Alves
2022-06-20 22:53 ` [PATCH 04/25] Step over clone syscall w/ breakpoint, TARGET_WAITKIND_THREAD_CLONED Pedro Alves
2022-06-20 22:53 ` [PATCH 05/25] Support clone events in the remote protocol Pedro Alves
2022-06-20 22:54 ` Pedro Alves [this message]
2022-06-20 22:54 ` [PATCH 07/25] Thread options & clone events (native Linux) Pedro Alves
2022-06-20 22:54 ` [PATCH 08/25] Thread options & clone events (Linux GDBserver) Pedro Alves
2022-06-20 22:54 ` [PATCH 09/25] gdbserver: Hide and don't detach pending clone children Pedro Alves
2022-06-20 22:54 ` [PATCH 10/25] Remove gdb/19675 kfails (displaced stepping + clone) Pedro Alves
2022-06-20 22:54 ` [PATCH 11/25] Add test for stepping over clone syscall Pedro Alves
2022-06-20 22:54 ` [PATCH 12/25] all-stop/synchronous RSP support thread-exit events Pedro Alves
2022-06-20 22:54 ` [PATCH 13/25] Introduce GDB_TO_EXIT thread option, fix step-over-thread-exit Pedro Alves
2022-06-20 22:54 ` [PATCH 14/25] Implement GDB_TO_EXIT support for Linux GDBserver Pedro Alves
2022-06-20 22:54 ` [PATCH 15/25] Implement GDB_TO_EXIT support for native Linux Pedro Alves
2022-06-20 22:54 ` [PATCH 16/25] gdb: clear step over information on thread exit (PR gdb/27338) Pedro Alves
2022-06-20 22:54 ` [PATCH 17/25] stop_all_threads: (re-)enable async before waiting for stops Pedro Alves
2022-06-20 22:54 ` [PATCH 18/25] gdbserver: Queue no-resumed event after thread exit Pedro Alves
2022-06-20 22:54 ` [PATCH 19/25] Don't resume new threads if scheduler-locking is in effect Pedro Alves
2022-06-21 11:07   ` Eli Zaretskii
2022-07-11 14:20     ` Pedro Alves
2022-07-11 15:44       ` Eli Zaretskii
2022-07-11 16:09         ` Pedro Alves
2022-07-11 16:30           ` Eli Zaretskii
2022-07-11 16:38             ` Pedro Alves
2022-07-11 17:00               ` Eli Zaretskii
2022-07-11 17:48                 ` Pedro Alves
2022-07-11 17:50                   ` Eli Zaretskii
2022-07-11 18:18                     ` Pedro Alves
2022-07-11 18:29                       ` Eli Zaretskii
2022-07-11 19:39                         ` Pedro Alves
2022-07-12 16:08                           ` Eli Zaretskii
2022-07-12 17:14                             ` Pedro Alves
2022-06-20 22:54 ` [PATCH 20/25] Tighten gdb.threads/no-unwaited-for-left.exp regexps Pedro Alves
2022-07-13 21:32   ` Pedro Alves
2022-06-20 22:54 ` [PATCH 21/25] Report thread exit event for leader if reporting thread exit events Pedro Alves
2022-06-20 22:54 ` [PATCH 22/25] Ignore failure to read PC when resuming Pedro Alves
2022-06-20 22:54 ` [PATCH 23/25] gdb/testsuite/lib/my-syscalls.S: Refactor new SYSCALL macro Pedro Alves
2022-06-20 22:54 ` [PATCH 24/25] Testcases for stepping over thread exit syscall (PR gdb/27338) Pedro Alves
2022-06-20 22:54 ` [PATCH 25/25] Document remote clone events, and QThreadOptions packet Pedro Alves
2022-06-21 12:07   ` Eli Zaretskii
2022-07-11 15:19     ` Pedro Alves
2022-07-11 16:09       ` Eli Zaretskii
2022-07-11 16:54         ` Pedro Alves
2022-07-11 17:02           ` Eli Zaretskii

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=20220620225419.382221-7-pedro@palves.net \
    --to=pedro@palves.net \
    --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).