diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2021-12-01 09:40:02 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2021-12-08 21:00:39 -0500 |
commit | 4a3ee32a402c865d2abec9bd6b708bc315a0fff8 (patch) | |
tree | 1be2a8cdd12643d297c2c0bc5730f50da0caf7bd /gdb/linux-nat.c | |
parent | 7b961964f86618773218c067bfff066b2bff8328 (diff) | |
download | gdb-4a3ee32a402c865d2abec9bd6b708bc315a0fff8.zip gdb-4a3ee32a402c865d2abec9bd6b708bc315a0fff8.tar.gz gdb-4a3ee32a402c865d2abec9bd6b708bc315a0fff8.tar.bz2 |
gdb/linux-nat: factor ptrace-detach code to new detach_one_pid function
The following patch will add some code paths that need to ptrace-detach
a given PID. Factor out the code that does this and put it in its own
function, so that it can be re-used.
Change-Id: Ie65ca0d89893b41aea0a23d9fc6ffbed042a9705
Diffstat (limited to 'gdb/linux-nat.c')
-rw-r--r-- | gdb/linux-nat.c | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 656a097..2f8cf49 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -1234,6 +1234,45 @@ linux_nat_target::attach (const char *args, int from_tty) target_async (1); } +/* Ptrace-detach the thread with pid PID. */ + +static void +detach_one_pid (int pid, int signo) +{ + if (ptrace (PTRACE_DETACH, pid, 0, signo) < 0) + { + int save_errno = errno; + + /* We know the thread exists, so ESRCH must mean the lwp is + zombie. This can happen if one of the already-detached + threads exits the whole thread group. In that case we're + still attached, and must reap the lwp. */ + if (save_errno == ESRCH) + { + int ret, status; + + ret = my_waitpid (pid, &status, __WALL); + if (ret == -1) + { + warning (_("Couldn't reap LWP %d while detaching: %s"), + pid, safe_strerror (errno)); + } + else if (!WIFEXITED (status) && !WIFSIGNALED (status)) + { + warning (_("Reaping LWP %d while detaching " + "returned unexpected status 0x%x"), + pid, status); + } + } + else + error (_("Can't detach %d: %s"), + pid, safe_strerror (save_errno)); + } + else + linux_nat_debug_printf ("PTRACE_DETACH (%d, %s, 0) (OK)", + pid, strsignal (signo)); +} + /* Get pending signal of THREAD 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 @@ -1364,42 +1403,7 @@ detach_one_lwp (struct lwp_info *lp, int *signo_p) throw; } - if (ptrace (PTRACE_DETACH, lwpid, 0, signo) < 0) - { - int save_errno = errno; - - /* We know the thread exists, so ESRCH must mean the lwp is - zombie. This can happen if one of the already-detached - threads exits the whole thread group. In that case we're - still attached, and must reap the lwp. */ - if (save_errno == ESRCH) - { - int ret, status; - - ret = my_waitpid (lwpid, &status, __WALL); - if (ret == -1) - { - warning (_("Couldn't reap LWP %d while detaching: %s"), - lwpid, safe_strerror (errno)); - } - else if (!WIFEXITED (status) && !WIFSIGNALED (status)) - { - warning (_("Reaping LWP %d while detaching " - "returned unexpected status 0x%x"), - lwpid, status); - } - } - else - { - error (_("Can't detach %s: %s"), - target_pid_to_str (lp->ptid).c_str (), - safe_strerror (save_errno)); - } - } - else - linux_nat_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)", - target_pid_to_str (lp->ptid).c_str (), - strsignal (signo)); + detach_one_pid (lwpid, signo); delete_lwp (lp->ptid); } |