]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdbserver: Hide and don't detach pending clone children
authorPedro Alves <pedro@palves.net>
Fri, 3 Dec 2021 22:10:05 +0000 (22:10 +0000)
committerPedro Alves <pedro@palves.net>
Mon, 12 Dec 2022 19:04:15 +0000 (19:04 +0000)
This commit extends the logic added by these two commits from a while
ago:

 #1  7b961964f866  (gdbserver: hide fork child threads from GDB),
 #2  df5ad102009c  (gdb, gdbserver: detach fork child when detaching from fork parent)

... to handle thread clone events, which are very similar to (v)fork
events.

For #1, we want to hide clone children as well, so just update the
comments.

For #2, unlike (v)fork children, pending clone children aren't full
processes, they're just threads, so don't detach them in
handle_detach.  linux-low.cc will take care of detaching them along
with all other threads of the process, there's nothing special that
needs to be done.

Change-Id: I7f5901d07efda576a2522d03e183994e071b8ffc

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

index d755cda0e44a47740760a47915a2ccfca3635901..a7c310260ca419223dbd649463729b5506cf7bd3 100644 (file)
@@ -6940,9 +6940,10 @@ linux_process_target::thread_pending_parent (thread_info *thread)
 }
 
 thread_info *
-linux_process_target::thread_pending_child (thread_info *thread)
+linux_process_target::thread_pending_child (thread_info *thread,
+                                           target_waitkind *kind)
 {
-  lwp_info *child = get_thread_lwp (thread)->pending_child ();
+  lwp_info *child = get_thread_lwp (thread)->pending_child (kind);
 
   if (child == nullptr)
     return nullptr;
index 69a34fb96fcfbead24a05ce3f4837fca9dd94c89..c9f9db71e0992b02fdaf6cd8d61fe64590dba71f 100644 (file)
@@ -315,7 +315,8 @@ public:
 #endif
 
   thread_info *thread_pending_parent (thread_info *thread) override;
-  thread_info *thread_pending_child (thread_info *thread) override;
+  thread_info *thread_pending_child (thread_info *thread,
+                                    target_waitkind *kind) override;
 
   bool supports_catch_syscall () override;
 
@@ -734,8 +735,8 @@ struct pending_signal
 
 struct lwp_info
 {
-  /* If this LWP is a fork child that wasn't reported to GDB yet, return
-     its parent, else nullptr.  */
+  /* If this LWP is a fork/vfork/clone child that wasn't reported to
+     GDB yet, return its parent, else nullptr.  */
   lwp_info *pending_parent () const
   {
     if (this->fork_relative == nullptr)
@@ -743,10 +744,10 @@ struct lwp_info
 
     gdb_assert (this->fork_relative->fork_relative == this);
 
-    /* In a fork parent/child relationship, the parent has a status pending and
-       the child does not, and a thread can only be in one such relationship
-       at most.  So we can recognize who is the parent based on which one has
-       a pending status.  */
+    /* In a parent/child relationship, the parent has a status pending
+       and the child does not, and a thread can only be in one such
+       relationship at most.  So we can recognize who is the parent
+       based on which one has a pending status.  */
     gdb_assert (!!this->status_pending_p
                != !!this->fork_relative->status_pending_p);
 
@@ -756,24 +757,25 @@ struct lwp_info
     const target_waitstatus &ws
       = this->fork_relative->waitstatus;
     gdb_assert (ws.kind () == TARGET_WAITKIND_FORKED
-               || ws.kind () == TARGET_WAITKIND_VFORKED);
+               || ws.kind () == TARGET_WAITKIND_VFORKED
+               || ws.kind () == TARGET_WAITKIND_THREAD_CLONED);
 
     return this->fork_relative;
   }
 
-  /* If this LWP is the parent of a fork child we haven't reported to GDB yet,
-     return that child, else nullptr.  */
-  lwp_info *pending_child () const
+  /* If this LWP is the parent of a fork/vfork/clone child we haven't
+     reported to GDB yet, return that child, else nullptr.  */
+  lwp_info *pending_child (target_waitkind *kind) const
   {
     if (this->fork_relative == nullptr)
       return nullptr;
 
     gdb_assert (this->fork_relative->fork_relative == this);
 
-    /* In a fork parent/child relationship, the parent has a status pending and
-       the child does not, and a thread can only be in one such relationship
-       at most.  So we can recognize who is the parent based on which one has
-       a pending status.  */
+    /* In a parent/child relationship, the parent has a status pending
+       and the child does not, and a thread can only be in one such
+       relationship at most.  So we can recognize who is the parent
+       based on which one has a pending status.  */
     gdb_assert (!!this->status_pending_p
                != !!this->fork_relative->status_pending_p);
 
@@ -782,8 +784,10 @@ struct lwp_info
 
     const target_waitstatus &ws = this->waitstatus;
     gdb_assert (ws.kind () == TARGET_WAITKIND_FORKED
-               || ws.kind () == TARGET_WAITKIND_VFORKED);
+               || ws.kind () == TARGET_WAITKIND_VFORKED
+               || ws.kind () == TARGET_WAITKIND_THREAD_CLONED);
 
+    *kind = ws.kind ();
     return this->fork_relative;
   }
 
index 5b07c4e438894c725240da256528a586d3fb8f7d..07a3319d114bf4a91eb9ce03f027a954d023344b 100644 (file)
@@ -1344,8 +1344,9 @@ handle_detach (char *own_buf)
        continue;
 
       /* Only threads that have a pending fork event.  */
-      thread_info *child = target_thread_pending_child (thread);
-      if (child == nullptr)
+      target_waitkind kind;
+      thread_info *child = target_thread_pending_child (thread, &kind);
+      if (child == nullptr || kind == TARGET_WAITKIND_THREAD_CLONED)
        continue;
 
       process_info *fork_child_process = get_thread_process (child);
@@ -1765,9 +1766,10 @@ handle_qxfer_threads_worker (thread_info *thread, struct buffer *buffer)
   gdb_byte *handle;
   bool handle_status = target_thread_handle (ptid, &handle, &handle_len);
 
-  /* If this is a fork or vfork child (has a fork parent), GDB does not yet
-     know about this process, and must not know about it until it gets the
-     corresponding (v)fork event.  Exclude this thread from the list.  */
+  /* If this is a (v)fork/clone child (has a (v)fork/clone parent),
+     GDB does not yet know about this thread, and must not know about
+     it until it gets the corresponding (v)fork/clone event.  Exclude
+     this thread from the list.  */
   if (target_thread_pending_parent (thread) != nullptr)
     return;
 
index 168b843d2ecbf570da089198889d724c1f917dcb..4584e9b3a8ea2194fbdbe3a91ce2420015fe88f9 100644 (file)
@@ -814,7 +814,8 @@ process_stratum_target::thread_pending_parent (thread_info *thread)
 }
 
 thread_info *
-process_stratum_target::thread_pending_child (thread_info *thread)
+process_stratum_target::thread_pending_child (thread_info *thread,
+                                             target_waitkind *kind)
 {
   return nullptr;
 }
index 8a0d9f42f7ddb1d16c1d81d2e35a636f61d943f0..e2e818b130b7456bf9da52522bac8b149497c687 100644 (file)
@@ -479,13 +479,14 @@ public:
   virtual bool thread_handle (ptid_t ptid, gdb_byte **handle,
                              int *handle_len);
 
-  /* If THREAD is a fork child that was not reported to GDB, return its parent
-     else nullptr.  */
+  /* If THREAD is a fork/vfork/clone child that was not reported to
+     GDB, return its parent else nullptr.  */
   virtual thread_info *thread_pending_parent (thread_info *thread);
 
-  /* If THREAD is the parent of a fork child that was not reported to GDB,
-     return this child, else nullptr.  */
-  virtual thread_info *thread_pending_child (thread_info *thread);
+  /* If THREAD is the parent of a fork/vfork/clone child that was not
+     reported to GDB, return this child, else nullptr.  */
+  virtual thread_info *thread_pending_child (thread_info *thread,
+                                            target_waitkind *kind);
 
   /* Returns true if the target can software single step.  */
   virtual bool supports_software_single_step ();
@@ -701,9 +702,9 @@ target_thread_pending_parent (thread_info *thread)
 }
 
 static inline thread_info *
-target_thread_pending_child (thread_info *thread)
+target_thread_pending_child (thread_info *thread, target_waitkind *kind)
 {
-  return the_target->thread_pending_child (thread);
+  return the_target->thread_pending_child (thread, kind);
 }
 
 int read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len);