Skip to content

Commit

Permalink
gdbserver: Hide and don't detach pending clone children
Browse files Browse the repository at this point in the history
This commit extends the logic added by these two commits from a while
ago:

 #1  7b96196  (gdbserver: hide fork child threads from GDB),
 #2  df5ad10  (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.

Reviewed-By: Andrew Burgess <aburgess@redhat.com>
Change-Id: I7f5901d07efda576a2522d03e183994e071b8ffc
  • Loading branch information
palves committed Nov 13, 2023
1 parent 393a6b5 commit faf44a3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
5 changes: 3 additions & 2 deletions gdbserver/linux-low.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6951,9 +6951,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;
Expand Down
15 changes: 10 additions & 5 deletions gdbserver/linux-low.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ class linux_process_target : public process_stratum_target
#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;

Expand Down Expand Up @@ -756,13 +757,15 @@ struct lwp_info
const target_waitstatus &ws
= this->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->relative; }

/* 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 () const
reported to GDB yet, return that child and fill in KIND with the
matching waitkind, otherwise nullptr. */
lwp_info *pending_child (target_waitkind *kind) const
{
if (this->relative == nullptr)
return nullptr;
Expand All @@ -781,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->relative;
}

Expand Down
12 changes: 7 additions & 5 deletions gdbserver/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1349,8 +1349,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);
Expand Down Expand Up @@ -1771,9 +1772,10 @@ handle_qxfer_threads_worker (thread_info *thread, std::string *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;

Expand Down
3 changes: 2 additions & 1 deletion gdbserver/target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,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;
}
Expand Down
16 changes: 9 additions & 7 deletions gdbserver/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,15 @@ class process_stratum_target
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 and fill in KIND with the
matching waitkind, otherwise 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 ();
Expand Down Expand Up @@ -700,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);
}

/* Read LEN bytes from MEMADDR in the buffer MYADDR. Return 0 if the read
Expand Down

0 comments on commit faf44a3

Please sign in to comment.