]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
linux-nat: Factor out get_detach_signal code to common code
authorPedro Alves <pedro@palves.net>
Fri, 17 May 2024 19:09:18 +0000 (20:09 +0100)
committerPedro Alves <pedro@palves.net>
Mon, 9 Jun 2025 17:09:16 +0000 (18:09 +0100)
The Windows target backend will want to do most of what the
get_detach_signal function in gdb/linux-nat.c does, except for the
Linux-specific bits.  This commit moves the code that is shareable to
infrun.c, so that other targets can use it too.

Change-Id: Ifaa96b4a41bb83d868079af4d47633715c0e1940

gdb/infrun.c
gdb/infrun.h
gdb/linux-nat.c

index 52e526361e43abd7f85d17ec355a7c132b199761..4076aa560e002da5d31f7a620dacc8129d86de00 100644 (file)
@@ -366,6 +366,43 @@ update_signals_program_target (void)
   target_program_signals (signal_program);
 }
 
+/* See infrun.h.  */
+
+gdb_signal
+get_detach_signal (process_stratum_target *proc_target, ptid_t ptid)
+{
+  thread_info *tp = proc_target->find_thread (ptid);
+  gdb_signal signo = GDB_SIGNAL_0;
+
+  if (target_is_non_stop_p ()
+      && tp->internal_state () != THREAD_INT_RUNNING)
+    {
+      if (tp->has_pending_waitstatus ())
+       {
+         /* If the thread has a pending event, and it was stopped
+            with a signal, use that signal to resume it.  If it has a
+            pending event of another kind, it was not stopped with a
+            signal, so resume it without a signal.  */
+         if (tp->pending_waitstatus ().kind () == TARGET_WAITKIND_STOPPED)
+           signo = tp->pending_waitstatus ().sig ();
+       }
+      else
+       signo = tp->stop_signal ();
+    }
+  else if (!target_is_non_stop_p ())
+    {
+      ptid_t last_ptid;
+      process_stratum_target *last_target;
+
+      get_last_target_status (&last_target, &last_ptid, nullptr);
+
+      if (last_target == proc_target && ptid == last_ptid)
+       signo = tp->stop_signal ();
+    }
+
+  return signo;
+}
+
 /* Value to pass to target_resume() to cause all threads to resume.  */
 
 #define RESUME_ALL minus_one_ptid
index b09e7c1df8a561330934df13d851b02d037768f2..869ae2a67a97e676cb6b1a4064f494d0a8a453e0 100644 (file)
@@ -319,6 +319,12 @@ extern void all_uis_on_sync_execution_starting (void);
    detach.  */
 extern void restart_after_all_stop_detach (process_stratum_target *proc_target);
 
+/* While detaching, return the signal PTID was supposed to be resumed
+   with, if it were resumed, so we can pass it down to PTID while
+   detaching.  */
+extern gdb_signal get_detach_signal (process_stratum_target *proc_target,
+                                    ptid_t ptid);
+
 /* RAII object to temporarily disable the requirement for target
    stacks to commit their resumed threads.
 
index 4de4cc83cfa14e3296864ecd793f303511dad9dc..94f3f6c70759b149cb34f5f0ecb39e435e133015 100644 (file)
@@ -1292,13 +1292,13 @@ detach_one_pid (int pid, int signo)
                            pid, strsignal (signo));
 }
 
-/* Get pending signal of THREAD as a host signal number, for detaching
+/* Get pending signal of LP as a host signal number, for detaching
    purposes.  This is the signal the thread last stopped for, which we
    need to deliver to the thread when detaching, otherwise, it'd be
    suppressed/lost.  */
 
 static int
-get_detach_signal (struct lwp_info *lp)
+get_lwp_detach_signal (struct lwp_info *lp)
 {
   enum gdb_signal signo = GDB_SIGNAL_0;
 
@@ -1328,38 +1328,7 @@ get_detach_signal (struct lwp_info *lp)
   else if (lp->status)
     signo = gdb_signal_from_host (WSTOPSIG (lp->status));
   else
-    {
-      thread_info *tp = linux_target->find_thread (lp->ptid);
-
-      if (target_is_non_stop_p ()
-         && tp->internal_state () != THREAD_INT_RUNNING)
-       {
-         if (tp->has_pending_waitstatus ())
-           {
-             /* If the thread has a pending event, and it was stopped with a
-                signal, use that signal to resume it.  If it has a pending
-                event of another kind, it was not stopped with a signal, so
-                resume it without a signal.  */
-             if (tp->pending_waitstatus ().kind () == TARGET_WAITKIND_STOPPED)
-               signo = tp->pending_waitstatus ().sig ();
-             else
-               signo = GDB_SIGNAL_0;
-           }
-         else
-           signo = tp->stop_signal ();
-       }
-      else if (!target_is_non_stop_p ())
-       {
-         ptid_t last_ptid;
-         process_stratum_target *last_target;
-
-         get_last_target_status (&last_target, &last_ptid, nullptr);
-
-         if (last_target == linux_target
-             && lp->ptid.lwp () == last_ptid.lwp ())
-           signo = tp->stop_signal ();
-       }
-    }
+    signo = get_detach_signal (linux_target, lp->ptid);
 
   if (signo == GDB_SIGNAL_0)
     {
@@ -1489,7 +1458,7 @@ detach_one_lwp (struct lwp_info *lp, int *signo_p)
   if (signo_p == NULL)
     {
       /* Pass on any pending signal for this LWP.  */
-      signo = get_detach_signal (lp);
+      signo = get_lwp_detach_signal (lp);
     }
   else
     signo = *signo_p;
@@ -1577,7 +1546,7 @@ linux_nat_target::detach (inferior *inf, int from_tty)
       if (main_lwp != nullptr)
        {
          /* Pass on any pending signal for the last LWP.  */
-         int signo = get_detach_signal (main_lwp);
+         int signo = get_lwp_detach_signal (main_lwp);
 
          detach_one_lwp (main_lwp, &signo);
        }