From: John Baldwin <jhb@FreeBSD.org>
To: gdb-patches@sourceware.org
Subject: [PATCH 9/9] fbsd-nat: Stop a process if it is running before killing it.
Date: Tue, 28 Feb 2023 10:18:45 -0800 [thread overview]
Message-ID: <20230228181845.99936-10-jhb@FreeBSD.org> (raw)
In-Reply-To: <20230228181845.99936-1-jhb@FreeBSD.org>
In addition, detach from any child processes implicitly attached to by
the kernel due to fork following that have not yet been processed by
GDB's core.
---
gdb/fbsd-nat.c | 93 +++++++++++++++++++++++++++++++++++++++++---------
gdb/fbsd-nat.h | 2 ++
2 files changed, 78 insertions(+), 17 deletions(-)
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 32dd482ec3c..42c69875b0f 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -1158,6 +1158,31 @@ fbsd_is_child_pending (pid_t pid)
return null_ptid;
}
+/* Wait for a child of a fork to report its stop. Returns the PTID of
+ the new child process. */
+
+static ptid_t
+fbsd_wait_for_fork_child (pid_t pid)
+{
+ ptid_t ptid = fbsd_is_child_pending (pid);
+ if (ptid != null_ptid)
+ return ptid;
+
+ int status;
+ pid_t wpid = waitpid (pid, &status, 0);
+ if (wpid == -1)
+ perror_with_name (("waitpid"));
+
+ gdb_assert (wpid == pid);
+
+ struct ptrace_lwpinfo pl;
+ if (ptrace (PT_LWPINFO, wpid, (caddr_t) &pl, sizeof pl) == -1)
+ perror_with_name (("ptrace (PT_LWPINFO)"));
+
+ gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
+ return ptid_t (wpid, pl.pl_lwpid);
+}
+
#ifndef PTRACE_VFORK
/* Record a pending vfork done event. */
@@ -1467,23 +1492,7 @@ fbsd_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
#endif
/* Make sure the other end of the fork is stopped too. */
- child_ptid = fbsd_is_child_pending (child);
- if (child_ptid == null_ptid)
- {
- int status;
-
- pid = waitpid (child, &status, 0);
- if (pid == -1)
- perror_with_name (("waitpid"));
-
- gdb_assert (pid == child);
-
- if (ptrace (PT_LWPINFO, child, (caddr_t) &pl, sizeof pl) == -1)
- perror_with_name (("ptrace (PT_LWPINFO)"));
-
- gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
- child_ptid = ptid_t (child, pl.pl_lwpid);
- }
+ child_ptid = fbsd_wait_for_fork_child (child);
/* Enable additional events on the child process. */
fbsd_enable_proc_events (child_ptid.pid ());
@@ -2115,6 +2124,56 @@ fbsd_nat_target::detach (inferior *inf, int from_tty)
detach_success (inf);
}
+/* Implement the "kill" target method. */
+
+void
+fbsd_nat_target::kill ()
+{
+ pid_t pid = inferior_ptid.pid ();
+ if (pid == 0)
+ return;
+
+ inferior *inf = current_inferior ();
+ stop_process (inf);
+
+ if (detach_fork_children (this, inf)) {
+ /* No need to kill now. */
+ target_mourn_inferior (inferior_ptid);
+
+ return;
+ }
+
+#ifdef TDP_RFPPWAIT
+ /* If there are any threads that have forked a new child but not yet
+ reported it because other threads reported events first, detach
+ from the children before killing the parent. */
+ auto lambda = [] (const struct ptrace_lwpinfo &pl)
+ {
+ if (pl.pl_flags & PL_FLAG_FORKED)
+ {
+ pid_t child = pl.pl_child_pid;
+
+ /* If the child hasn't reported its stop yet, wait for it to
+ stop. */
+ fbsd_wait_for_fork_child (child);
+
+ /* Detach from the child. */
+ (void) ptrace (PT_DETACH, child, (caddr_t) 1, 0);
+ }
+ return false;
+ };
+ iterate_other_ptrace_events (pid, gdb::make_function_view (lambda));
+#endif
+
+ if (ptrace (PT_KILL, pid, NULL, 0) == -1)
+ perror_with_name (("ptrace (PT_KILL)"));
+
+ int status;
+ waitpid (pid, &status, 0);
+
+ target_mourn_inferior (inferior_ptid);
+}
+
void
fbsd_nat_target::mourn_inferior ()
{
diff --git a/gdb/fbsd-nat.h b/gdb/fbsd-nat.h
index 3dc22ce1cca..8096c53bc03 100644
--- a/gdb/fbsd-nat.h
+++ b/gdb/fbsd-nat.h
@@ -80,6 +80,8 @@ class fbsd_nat_target : public inf_ptrace_target
void detach (inferior *, int) override;
+ void kill () override;
+
void mourn_inferior () override;
void resume (ptid_t, int, enum gdb_signal) override;
--
2.38.1
next prev parent reply other threads:[~2023-02-28 18:19 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-28 18:18 [PATCH 0/9] Fixes for multiprocess for FreeBSD's native target John Baldwin
2023-02-28 18:18 ` [PATCH 1/9] fbsd-nat: Add missing spaces John Baldwin
2023-03-20 17:43 ` Simon Marchi
2023-03-20 18:08 ` John Baldwin
2023-02-28 18:18 ` [PATCH 2/9] fbsd-nat: Avoid a direct write to target_waitstatus::kind John Baldwin
2023-03-20 17:45 ` Simon Marchi
2023-02-28 18:18 ` [PATCH 3/9] fbsd-nat: Use correct constant for target_waitstatus::sig John Baldwin
2023-03-20 17:47 ` Simon Marchi
2023-02-28 18:18 ` [PATCH 4/9] fbsd-nat: Add a list of pending events John Baldwin
2023-03-20 18:35 ` Simon Marchi
2023-03-27 20:24 ` John Baldwin
2023-03-27 20:57 ` Simon Marchi
2023-03-27 21:00 ` John Baldwin
2023-02-28 18:18 ` [PATCH 5/9] fbsd-nat: Defer any ineligible events reported by wait John Baldwin
2023-03-20 19:09 ` Simon Marchi
2023-03-27 20:49 ` John Baldwin
2023-03-27 21:04 ` Simon Marchi
2023-02-28 18:18 ` [PATCH 6/9] fbsd-nat: Fix resuming and waiting with multiple processes John Baldwin
2023-03-20 19:55 ` Simon Marchi
2023-03-27 21:13 ` John Baldwin
2023-02-28 18:18 ` [PATCH 7/9] fbsd-nat: Fix several issues with detaching John Baldwin
2023-02-28 18:18 ` [PATCH 8/9] fbsd-nat: Fix thread_alive against a running thread John Baldwin
2023-02-28 18:18 ` John Baldwin [this message]
2023-03-14 18:21 ` [PING] [PATCH 0/9] Fixes for multiprocess for FreeBSD's native target John Baldwin
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=20230228181845.99936-10-jhb@FreeBSD.org \
--to=jhb@freebsd.org \
--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).