]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Implement GDB_TO_EXIT support for Linux GDBserver
authorPedro Alves <pedro@palves.net>
Thu, 17 Mar 2022 19:25:03 +0000 (19:25 +0000)
committerPedro Alves <pedro@palves.net>
Mon, 18 Jul 2022 16:33:31 +0000 (17:33 +0100)
This implements support for the new GDB_TO_EXIT thread option for
Linux GDBserver.

Change-Id: I96b719fdf7fee94709e98bb3a90751d8134f3a38
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27338

gdbserver/linux-low.cc
gdbserver/linux-low.h

index 2568fa10ad4d4983908194ca6b0dcd564200f196..54ab0a7d8f5a77dffb26d667abd5edc59aa16e3e 100644 (file)
@@ -144,6 +144,18 @@ is_leader (thread_info *thread)
   return ptid.pid () == ptid.lwp ();
 }
 
+/* Return true if we should report thread exit events to GDB, for
+   THR.  */
+
+static bool
+report_exit_events_for (thread_info *thr)
+{
+  client_state &cs = get_client_state ();
+
+  return (cs.report_thread_events
+         || (thr->thread_options & GDB_TO_EXIT) != 0);
+}
+
 /* LWP accessors.  */
 
 /* See nat/linux-nat.h.  */
@@ -2231,7 +2243,6 @@ linux_low_ptrace_options (int attached)
 void
 linux_process_target::filter_event (int lwpid, int wstat)
 {
-  client_state &cs = get_client_state ();
   struct lwp_info *child;
   struct thread_info *thread;
   int have_stop_pc = 0;
@@ -2318,7 +2329,7 @@ linux_process_target::filter_event (int lwpid, int wstat)
       /* If this is not the leader LWP, then the exit signal was not
         the end of the debugged application and should be ignored,
         unless GDB wants to hear about thread exits.  */
-      if (cs.report_thread_events || is_leader (thread))
+      if (report_exit_events_for (thread) || is_leader (thread))
        {
          /* Since events are serialized to GDB core, and we can't
             report this one right now.  Leave the status pending for
@@ -2881,13 +2892,20 @@ ptid_t
 linux_process_target::filter_exit_event (lwp_info *event_child,
                                         target_waitstatus *ourstatus)
 {
-  client_state &cs = get_client_state ();
   struct thread_info *thread = get_lwp_thread (event_child);
   ptid_t ptid = ptid_of (thread);
 
+  /* Note we must filter TARGET_WAITKIND_SIGNALLED as well, otherwise
+     if a non-leader thread exits with a signal, we'd report it to the
+     core which would interpret it as the whole-process exiting.
+     There is no TARGET_WAITKIND_THREAD_SIGNALLED event kind.  */
+  if (ourstatus->kind () != TARGET_WAITKIND_EXITED
+      && ourstatus->kind () != TARGET_WAITKIND_SIGNALLED)
+    return ptid;
+
   if (!is_leader (thread))
     {
-      if (cs.report_thread_events)
+      if (report_exit_events_for (thread))
        ourstatus->set_thread_exited (0);
       else
        ourstatus->set_ignore ();
@@ -3030,10 +3048,7 @@ linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
             WTERMSIG (w));
        }
 
-      if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
-       return filter_exit_event (event_child, ourstatus);
-
-      return ptid_of (current_thread);
+      return filter_exit_event (event_child, ourstatus);
     }
 
   /* If step-over executes a breakpoint instruction, in the case of a
@@ -3602,10 +3617,7 @@ linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
                        target_pid_to_str (ptid_of (current_thread)).c_str (),
                        ourstatus->to_string ().c_str ());
 
-  if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
-    return filter_exit_event (event_child, ourstatus);
-
-  return ptid_of (current_thread);
+  return filter_exit_event (event_child, ourstatus);
 }
 
 /* Get rid of any pending event in the pipe.  */
@@ -5915,7 +5927,7 @@ bool
 linux_process_target::supports_set_thread_options
   (gdb_thread_options *supported_options)
 {
-  *supported_options = GDB_TO_CLONE;
+  *supported_options = GDB_TO_CLONE | GDB_TO_EXIT;
   return true;
 }
 
index e7237bba3b3175fbd2f753740e01a39a0b3c313f..051a7196ef765d433b3732982558c10021754b91 100644 (file)
@@ -573,10 +573,11 @@ private: /* Back to private.  */
      exited.  */
   void check_zombie_leaders ();
 
-  /* Convenience function that is called when the kernel reports an exit
-     event.  This decides whether to report the event to GDB as a
-     process exit event, a thread exit event, or to suppress the
-     event.  */
+  /* Convenience function that is called when we're about to return an
+     event to the core.  If the event is an exit or signalled event,
+     then this decides whether to report it as process-wide event, as
+     a thread exit event, or to suppress it.  All other event kinds
+     are passed through unmodified.  */
   ptid_t filter_exit_event (lwp_info *event_child,
                            target_waitstatus *ourstatus);