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 08/10] Implement stepped-thread-exited feature on native+gdbserver Linux
Date: Fri,  2 Jul 2021 14:01:52 +0100	[thread overview]
Message-ID: <20210702130154.1947858-9-pedro@palves.net> (raw)
In-Reply-To: <20210702130154.1947858-1-pedro@palves.net>

From: Simon Marchi <simon.marchi@efficios.com>

gdb/ChangeLog:
yyyy-mm-dd  Simon Marchi  <simon.marchi@efficios.com>
	    Pedro Alves  <pedro@palves.net>

	PR gdb/27338
	* linux-nat.c (exit_lwp): Add del_thread parameter and handle it.
	(wait_lwp): Adjust.
	(linux_nat_filter_event): Report thread exit event if thread was
	stepping.
	(check_zombie_leaders): Adjust.
	(filter_exit_event): Report thread exit event if thread was
	stepping.  If so, don't delete thread_info associated to thread.
	(linux_nat_wait_1): Pass last_resume_kind to filter_exit_event.

gdbserver/ChangeLog:
yyyy-mm-dd  Simon Marchi  <simon.marchi@efficios.com>
	    Pedro Alves  <pedro@palves.net>

	PR gdb/27338
	* linux-low.cc (linux_process_target::filter_event): Report thread
	exit event if thread was stepping from gdb's perspective.
	(linux_process_target::supports_stepped_thread_exited): New.
	* linux-low.h
	(linux_process_target::supports_stepped_thread_exited): Declare.

Change-Id: Id9ea2d512631972017bb89e21e447cfcd04f7017
Co-authored-by: Pedro Alves <pedro@palves.net>
---
 gdb/linux-nat.c        | 41 ++++++++++++++++++++++++++++-------------
 gdbserver/linux-low.cc | 10 +++++++++-
 gdbserver/linux-low.h  |  2 ++
 3 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index f206b874929..0da0b6200b9 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -1001,10 +1001,13 @@ linux_nat_switch_fork (ptid_t new_ptid)
   registers_changed ();
 }
 
-/* Handle the exit of a single thread LP.  */
+/* Handle the exit of a single thread LP.
+
+   If DEL_THREAD is true, delete the thread_info associated to LP, if
+   it exists.  */
 
 static void
-exit_lwp (struct lwp_info *lp)
+exit_lwp (struct lwp_info *lp, bool del_thread)
 {
   struct thread_info *th = find_thread_ptid (linux_target, lp->ptid);
 
@@ -1014,7 +1017,8 @@ exit_lwp (struct lwp_info *lp)
 	printf_unfiltered (_("[%s exited]\n"),
 			   target_pid_to_str (lp->ptid).c_str ());
 
-      delete_thread (th);
+      if (del_thread)
+	delete_thread (th);
     }
 
   delete_lwp (lp->ptid);
@@ -2188,7 +2192,7 @@ wait_lwp (struct lwp_info *lp)
 
   if (thread_dead)
     {
-      exit_lwp (lp);
+      exit_lwp (lp, true);
       return 0;
     }
 
@@ -2925,6 +2929,7 @@ linux_nat_filter_event (int lwpid, int status)
   if (WIFEXITED (status) || WIFSIGNALED (status))
     {
       if (!report_thread_events
+	  && lp->last_resume_kind != resume_step
 	  && num_lwps (lp->ptid.pid ()) > 1)
 	{
 	  linux_nat_debug_printf ("%s exited.",
@@ -2933,7 +2938,7 @@ linux_nat_filter_event (int lwpid, int status)
 	  /* If there is at least one more LWP, then the exit signal
 	     was not the end of the debugged application and should be
 	     ignored.  */
-	  exit_lwp (lp);
+	  exit_lwp (lp, true);
 	  return;
 	}
 
@@ -3100,7 +3105,7 @@ check_zombie_leaders (void)
 	     thread execs).  */
 
 	  linux_nat_debug_printf ("Thread group leader %d vanished.", inf->pid);
-	  exit_lwp (leader_lp);
+	  exit_lwp (leader_lp, true);
 	}
     }
 }
@@ -3112,18 +3117,28 @@ check_zombie_leaders (void)
 
 static ptid_t
 filter_exit_event (struct lwp_info *event_child,
-		   struct target_waitstatus *ourstatus)
+		   struct target_waitstatus *ourstatus,
+		   resume_kind last_resume_kind)
 {
   ptid_t ptid = event_child->ptid;
 
   if (num_lwps (ptid.pid ()) > 1)
     {
-      if (report_thread_events)
-	ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
+      /* If we decide to report the thread exited event to the core, we must
+         not delete the thread_info structure ourselves.   */
+      if (report_thread_events
+	  || last_resume_kind == resume_step)
+	{
+	  ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
+	  /* Delete lwp, but not thread_info, infrun will need it to process
+	     the event.  */
+	  exit_lwp (event_child, false);
+	}
       else
-	ourstatus->kind = TARGET_WAITKIND_IGNORE;
-
-      exit_lwp (event_child);
+	{
+	  ourstatus->kind = TARGET_WAITKIND_IGNORE;
+	  exit_lwp (event_child, true);
+	}
     }
 
   return ptid;
@@ -3346,7 +3361,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
     lp->core = linux_common_core_of_thread (lp->ptid);
 
   if (ourstatus->kind == TARGET_WAITKIND_EXITED)
-    return filter_exit_event (lp, ourstatus);
+    return filter_exit_event (lp, ourstatus, last_resume_kind);
 
   return lp->ptid;
 }
diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
index 26693e2dc48..4f08bb7a109 100644
--- a/gdbserver/linux-low.cc
+++ b/gdbserver/linux-low.cc
@@ -2312,6 +2312,7 @@ linux_process_target::filter_event (int lwpid, int wstat)
 	 not the end of the debugged application and should be
 	 ignored, unless GDB wants to hear about thread exits.  */
       if (cs.report_thread_events
+	  || thread->last_resume_kind == resume_step
 	  || last_thread_of_process_p (pid_of (thread)))
 	{
 	  /* Since events are serialized to GDB core, and we can't
@@ -2895,7 +2896,8 @@ linux_process_target::filter_exit_event (lwp_info *event_child,
 
   if (!last_thread_of_process_p (pid_of (thread)))
     {
-      if (cs.report_thread_events)
+      if (cs.report_thread_events
+	  || thread->last_resume_kind == resume_step)
 	ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
       else
 	ourstatus->kind = TARGET_WAITKIND_IGNORE;
@@ -7125,6 +7127,12 @@ linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
 }
 #endif
 
+bool
+linux_process_target::supports_stepped_thread_exited ()
+{
+  return true;
+}
+
 /* Default implementation of linux_target_ops method "set_pc" for
    32-bit pc register which is literally named "pc".  */
 
diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h
index d59ad386bd5..712d273223e 100644
--- a/gdbserver/linux-low.h
+++ b/gdbserver/linux-low.h
@@ -317,6 +317,8 @@ class linux_process_target : public process_stratum_target
      visibility because proc-service uses it.  */
   virtual const regs_info *get_regs_info () = 0;
 
+  bool supports_stepped_thread_exited () override;
+
 private:
 
   /* Handle a GNU/Linux extended wait response.  If we see a clone,
-- 
2.26.2


  parent reply	other threads:[~2021-07-02 13:02 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-02 13:01 [PATCH 00/10] Step over thread exit (PR gdb/27338) Pedro Alves
2021-07-02 13:01 ` [PATCH 01/10] displaced step: pass down target_waitstatus instead of gdb_signal Pedro Alves
2021-07-05 22:19   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 02/10] gdb: clear step over information on thread exit (PR gdb/27338) Pedro Alves
2021-07-05 18:34   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 03/10] remote+gdbserver: stepped-thread-exited feature Pedro Alves
2021-07-02 17:13   ` Eli Zaretskii
2021-07-05 19:27   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 04/10] struct resumed_pending_vcont_info -> struct resume_info Pedro Alves
2021-07-05 19:46   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 05/10] remote_target::update_thread_list: Don't delete stepping threads Pedro Alves
2021-07-05 19:55   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 06/10] stop_all_threads: (re-)enable async before waiting for stops Pedro Alves
2021-07-05 21:32   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 07/10] Tweak gdbserver's no-resumed handling Pedro Alves
2021-07-05 21:51   ` Simon Marchi
2021-07-02 13:01 ` Pedro Alves [this message]
2021-07-02 13:01 ` [PATCH 09/10] lib/my-syscalls.S: Refactor new SYSCALL macro Pedro Alves
2021-07-05 22:11   ` Simon Marchi
2021-07-02 13:01 ` [PATCH 10/10] Testcases for stepping over thread exit syscall (PR gdb/27338) Pedro Alves
2021-07-05  8:42 ` [PATCH 00/10] Step over thread exit " Andrew Burgess
2021-07-05 11:43   ` Pedro Alves
2021-07-13 15:08     ` Pedro Alves

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=20210702130154.1947858-9-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).