From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 2/3] gdb/linux-nat: factor ptrace-detach code to new detach_one_pid function
Date: Wed, 24 Nov 2021 15:04:43 -0500 [thread overview]
Message-ID: <20211124200444.614978-3-simon.marchi@efficios.com> (raw)
In-Reply-To: <20211124200444.614978-1-simon.marchi@efficios.com>
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
---
gdb/linux-nat.c | 76 ++++++++++++++++++++++++++-----------------------
1 file changed, 40 insertions(+), 36 deletions(-)
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index f8f728481ea7..85ae68d8a6fa 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);
}
--
2.26.2
next prev parent reply other threads:[~2021-11-24 20:04 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-29 20:33 [PATCH 1/2] gdbserver: hide fork child threads from GDB Simon Marchi
2021-10-29 20:33 ` [PATCH 2/2] gdb, gdbserver: detach fork child when detaching from fork parent Simon Marchi
2021-11-12 20:54 ` [PATCH 1/2] gdbserver: hide fork child threads from GDB (ping) Simon Marchi
2021-11-24 20:04 ` [PATCH 0/3] Fix handling of pending fork events Simon Marchi
2021-11-24 20:04 ` [PATCH 1/3] gdbserver: hide fork child threads from GDB Simon Marchi
2021-11-26 22:51 ` Pedro Alves
2021-11-29 12:46 ` Simon Marchi
2021-11-29 15:37 ` Pedro Alves
2021-11-24 20:04 ` Simon Marchi [this message]
2021-11-26 22:51 ` [PATCH 2/3] gdb/linux-nat: factor ptrace-detach code to new detach_one_pid function Pedro Alves
2021-11-24 20:04 ` [PATCH 3/3] gdb, gdbserver: detach fork child when detaching from fork parent Simon Marchi
2021-11-26 22:54 ` Pedro Alves
2021-11-29 18:27 ` Simon Marchi
2021-11-24 20:56 ` [PATCH 0/3] Fix handling of pending fork events Simon Marchi
2021-11-26 22:50 ` 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=20211124200444.614978-3-simon.marchi@efficios.com \
--to=simon.marchi@efficios.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).