Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Kamil Rytarowski" <n54@gmx.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Add support for NetBSD thread events (create, exit)
Date: Thu, 30 Apr 2020 15:59:43 +0200	[thread overview]
Message-ID: <trinity-a9d47dff-0aff-4cc6-b8bc-19887120bba8-1588255183159@3c-app-mailcom-bs10> (raw)

[-- Attachment #1: Type: text/plain, Size: 946 bytes --]

Hello,

Please review.

commit 5446f5e7983ad9383e3bc0469327c94baa5e0587 (HEAD -> netbsd-3)
Author: Kamil Rytarowski <n54@gmx.com>
Date:   Thu Apr 30 16:13:33 2020 +0200

    Add support for NetBSD thread events (create, exit)

    Report LWP CREATE and LWP EXIT events and setup this on post_attach()
    and post_startup_inferior().

    Stop reinitializing the list of recognized threads in update_thread_list().

    Handle LWP CREATE and EXIT events in nbsd_nat_target::wait().

    gdb/ChangeLog:

            * nbsd-nat.c (nbsd_enable_proc_events)
            (nbsd_nat_target::post_startup_inferior): Add.
            (nbsd_nat_target::post_attach): Call `nbsd_enable_proc_events'.
            (nbsd_nat_target::update_thread_list): Rewrite.
            (nbsd_nat_target::wait): Handle "PTRACE_LWP_EXIT" and
            "PTRACE_LWP_CREATE".
            * nbsd-nat.h (nbsd_nat_target::post_startup_inferior): Add.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-support-for-NetBSD-thread-events-create-exit.patch --]
[-- Type: text/x-patch, Size: 5651 bytes --]

From 5446f5e7983ad9383e3bc0469327c94baa5e0587 Mon Sep 17 00:00:00 2001
From: Kamil Rytarowski <n54@gmx.com>
Date: Thu, 30 Apr 2020 16:13:33 +0200
Subject: [PATCH] Add support for NetBSD thread events (create, exit)

Report LWP CREATE and LWP EXIT events and setup this on post_attach()
and post_startup_inferior().

Stop reinitializing the list of recognized threads in update_thread_list().

Handle LWP CREATE and EXIT events in nbsd_nat_target::wait().

gdb/ChangeLog:

        * nbsd-nat.c (nbsd_enable_proc_events)
        (nbsd_nat_target::post_startup_inferior): Add.
        (nbsd_nat_target::post_attach): Call `nbsd_enable_proc_events'.
        (nbsd_nat_target::update_thread_list): Rewrite.
        (nbsd_nat_target::wait): Handle "PTRACE_LWP_EXIT" and
        "PTRACE_LWP_CREATE".
        * nbsd-nat.h (nbsd_nat_target::post_startup_inferior): Add.
---
 gdb/ChangeLog  | 10 ++++++
 gdb/nbsd-nat.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++--
 gdb/nbsd-nat.h |  1 +
 3 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 830bc30979..22099db259 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@
+2020-04-30  Kamil Rytarowski  <n54@gmx.com>
+
+	* nbsd-nat.c (nbsd_enable_proc_events)
+	(nbsd_nat_target::post_startup_inferior): Add.
+	(nbsd_nat_target::post_attach): Call `nbsd_enable_proc_events'.
+	(nbsd_nat_target::update_thread_list): Rewrite.
+	(nbsd_nat_target::wait): Handle "PTRACE_LWP_EXIT" and
+	"PTRACE_LWP_CREATE".
+	* nbsd-nat.h (nbsd_nat_target::post_startup_inferior): Add.
+
 2020-04-29  Simon Marchi  <simon.marchi@efficios.com>

 	* gdbarch.sh (do_read): Add shellcheck disable directive for
diff --git a/gdb/nbsd-nat.c b/gdb/nbsd-nat.c
index b04e634b54..f8c917076b 100644
--- a/gdb/nbsd-nat.c
+++ b/gdb/nbsd-nat.c
@@ -222,11 +222,37 @@ nbsd_add_threads (nbsd_nat_target *target, pid_t pid)
   nbsd_thread_lister (pid, fn);
 }

+/* Enable additional event reporting on new processes.  */
+
+static void
+nbsd_enable_proc_events (pid_t pid)
+{
+  int events;
+
+  if (ptrace (PT_GET_EVENT_MASK, pid, &events, sizeof (events)) == -1)
+    perror_with_name (("ptrace"));
+
+  events |= PTRACE_LWP_CREATE;
+  events |= PTRACE_LWP_EXIT;
+
+  if (ptrace (PT_SET_EVENT_MASK, pid, &events, sizeof (events)) == -1)
+    perror_with_name (("ptrace"));
+}
+
+/* Implement the "post_startup_inferior" target_ops method.  */
+
+void
+nbsd_nat_target::post_startup_inferior (ptid_t ptid)
+{
+  nbsd_enable_proc_events (ptid.pid ());
+}
+
 /* Implement the "post_attach" target_ops method.  */

 void
 nbsd_nat_target::post_attach (int pid)
 {
+  nbsd_enable_proc_events (pid);
   nbsd_add_threads (this, pid);
 }

@@ -235,9 +261,7 @@ nbsd_nat_target::post_attach (int pid)
 void
 nbsd_nat_target::update_thread_list ()
 {
-  prune_threads ();
-
-  nbsd_add_threads (this, inferior_ptid.pid ());
+  delete_exited_threads ();
 }

 /* Convert PTID to a string.  */
@@ -686,9 +710,61 @@ nbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
   if (code <= SI_USER || code == SI_NOINFO)
     return wptid;

+  /* Process state for threading events */
+  ptrace_state_t pst = {};
+  if (code == TRAP_LWP)
+    {
+      if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) == -1)
+	perror_with_name (("ptrace"));
+    }
+
+  if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_EXIT)
+    {
+      /* If GDB attaches to a multi-threaded process, exiting
+	 threads might be skipped during post_attach that
+	 have not yet reported their PTRACE_LWP_EXIT event.
+	 Ignore exited events for an unknown LWP.  */
+      thread_info *thr = find_thread_ptid (this, wptid);
+      if (thr == nullptr)
+	  ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+      else
+	{
+	  ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
+	  /* NetBSD does not store an LWP exit status.  */
+	  ourstatus->value.integer = 0;
+
+	  if (print_thread_events)
+	    printf_unfiltered (_("[%s exited]\n"),
+                           target_pid_to_str (wptid).c_str ());
+	  delete_thread (thr);
+	}
+
+      /* The GDB core expects that the rest of the threads are running.  */
+      if (ptrace (PT_CONTINUE, pid, (void *) 1, 0) == -1)
+	perror_with_name (("ptrace"));
+
+      return wptid;
+    }
+
   if (in_thread_list (this, ptid_t (pid)))
       thread_change_ptid (this, ptid_t (pid), wptid);

+  if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_CREATE)
+    {
+      /* If GDB attaches to a multi-threaded process, newborn
+	 threads might be added by nbsd_add_threads that have
+	 not yet reported their PTRACE_LWP_CREATE event.  Ignore
+	 born events for an already-known LWP.  */
+      if (in_thread_list (this, wptid))
+	  ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+      else
+	{
+	  add_thread (this, wptid);
+	  ourstatus->kind = TARGET_WAITKIND_THREAD_CREATED;
+	}
+      return wptid;
+    }
+
   if (code == TRAP_EXEC)
     {
       ourstatus->kind = TARGET_WAITKIND_EXECD;
diff --git a/gdb/nbsd-nat.h b/gdb/nbsd-nat.h
index 6e14cbb889..4a8b96026e 100644
--- a/gdb/nbsd-nat.h
+++ b/gdb/nbsd-nat.h
@@ -32,6 +32,7 @@ struct nbsd_nat_target : public inf_ptrace_target

   bool thread_alive (ptid_t ptid) override;
   const char *thread_name (struct thread_info *thr) override;
+  void post_startup_inferior (ptid_t ptid) override;
   void post_attach (int pid) override;
   void update_thread_list () override;
   std::string pid_to_str (ptid_t ptid) override;
--
2.25.2


             reply	other threads:[~2020-04-30 13:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-30 13:59 Kamil Rytarowski [this message]
2020-04-30 18:28 ` Simon Marchi

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=trinity-a9d47dff-0aff-4cc6-b8bc-19887120bba8-1588255183159@3c-app-mailcom-bs10 \
    --to=n54@gmx.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