From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.baldwin.cx (bigwig.baldwin.cx [IPv6:2607:f138:0:13::2]) by sourceware.org (Postfix) with ESMTPS id DEEE73839C76 for ; Tue, 27 Jul 2021 17:42:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DEEE73839C76 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=FreeBSD.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=FreeBSD.org Received: from ralph.com (ralph.baldwin.cx [66.234.199.215]) by mail.baldwin.cx (Postfix) with ESMTPSA id 4DB711A84C5E for ; Tue, 27 Jul 2021 13:42:51 -0400 (EDT) From: John Baldwin To: gdb-patches@sourceware.org Subject: [PATCH v2 4/5] obsd-nat: Various fixes for fork following. Date: Tue, 27 Jul 2021 10:41:09 -0700 Message-Id: <20210727174110.62480-5-jhb@FreeBSD.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210727174110.62480-1-jhb@FreeBSD.org> References: <20210727174110.62480-1-jhb@FreeBSD.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.4 (mail.baldwin.cx [0.0.0.0]); Tue, 27 Jul 2021 13:42:51 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.103.1 at mail.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_STATUS, KHOP_HELO_FCRDNS, SPF_HELO_PASS, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Jul 2021 17:42:54 -0000 - Don't use #ifdef's on ptrace ops. obsd-nat.h didn't include , so the virtual methods weren't always overridden causing the fork following to not work. In addition, the thread and fork code is intertwined in ::wait and and the lack of #ifdef's there already assumed both were present. - Move duplicated code to enable PTRACE_FORK event reporting to a single function and invoke it on new child processes reported via PTRACE_FORK. - Don't return early from PTRACE_FORK handling, but instead reset wptid to the correct ptid if the child reports its event before the parent. This allows the ptid fixup code to add thread IDs if the first event for a process is a PTRACE_FORK event. This also properly returns ptid's with thread IDs when reporting PTRACE_FORK events. - Handle detach_fork by skipping the PT_DETACH. --- gdb/obsd-nat.c | 51 +++++++++++++++++++++++--------------------------- gdb/obsd-nat.h | 2 -- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/gdb/obsd-nat.c b/gdb/obsd-nat.c index 8549c6ea66b..0edaa66cf1b 100644 --- a/gdb/obsd-nat.c +++ b/gdb/obsd-nat.c @@ -33,8 +33,6 @@ that maps userland threads directly onto kernel threads in a 1:1 fashion. */ -#ifdef PT_GET_THREAD_FIRST - std::string obsd_nat_target::pid_to_str (ptid_t ptid) { @@ -72,6 +70,19 @@ obsd_nat_target::update_thread_list () } } +static void +obsd_enable_proc_events (pid_t pid) +{ + ptrace_event_t pe; + + /* Set the initial event mask. */ + memset (&pe, 0, sizeof pe); + pe.pe_set_event |= PTRACE_FORK; + if (ptrace (PT_SET_EVENT_MASK, pid, + (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) + perror_with_name (("ptrace")); +} + ptid_t obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, target_wait_flags options) @@ -87,6 +98,8 @@ obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1) perror_with_name (("ptrace")); + wptid = ptid_t (pid, pe.pe_tid, 0); + switch (pe.pe_report_event) { case PTRACE_FORK: @@ -107,13 +120,15 @@ obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, if (find_inferior_pid (this, fpid) != nullptr) { ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); - return ptid_t (fpid); + wptid = ptid_t (fpid, pe.pe_tid, 0); } - return ptid_t (pid); + obsd_enable_proc_events (ourstatus->value.related_pid.pid ()); + break; } - wptid = ptid_t (pid, pe.pe_tid, 0); + /* Ensure the ptid is updated with an LWP id on the first stop + of a process. */ if (!in_thread_list (this, wptid)) { if (in_thread_list (this, ptid_t (pid))) @@ -125,34 +140,16 @@ obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, return wptid; } -#endif /* PT_GET_THREAD_FIRST */ - -#ifdef PT_GET_PROCESS_STATE - void obsd_nat_target::post_attach (int pid) { - ptrace_event_t pe; - - /* Set the initial event mask. */ - memset (&pe, 0, sizeof pe); - pe.pe_set_event |= PTRACE_FORK; - if (ptrace (PT_SET_EVENT_MASK, pid, - (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) - perror_with_name (("ptrace")); + obsd_enable_proc_events (pid); } void obsd_nat_target::post_startup_inferior (ptid_t pid) { - ptrace_event_t pe; - - /* Set the initial event mask. */ - memset (&pe, 0, sizeof pe); - pe.pe_set_event |= PTRACE_FORK; - if (ptrace (PT_SET_EVENT_MASK, pid.pid (), - (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1) - perror_with_name (("ptrace")); + obsd_enable_proc_events (pid.pid ()); } /* Target hook for follow_fork. On entry and at return inferior_ptid is @@ -162,7 +159,7 @@ void obsd_nat_target::follow_fork (ptid_t child_ptid, target_waitkind fork_kind, bool follow_child, bool detach_fork) { - if (!follow_child) + if (!follow_child && detach_fork) { /* Breakpoints have already been detached from the child by infrun.c. */ @@ -183,5 +180,3 @@ obsd_nat_target::remove_fork_catchpoint (int pid) { return 0; } - -#endif /* PT_GET_PROCESS_STATE */ diff --git a/gdb/obsd-nat.h b/gdb/obsd-nat.h index ddd4baf7761..43a793e0a31 100644 --- a/gdb/obsd-nat.h +++ b/gdb/obsd-nat.h @@ -29,7 +29,6 @@ class obsd_nat_target : public inf_ptrace_target void update_thread_list () override; ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; -#ifdef PT_GET_PROCESS_STATE void follow_fork (ptid_t, target_waitkind, bool, bool) override; int insert_fork_catchpoint (int) override; @@ -39,7 +38,6 @@ class obsd_nat_target : public inf_ptrace_target void post_startup_inferior (ptid_t) override; void post_attach (int) override; -#endif }; #endif /* obsd-nat.h */ -- 2.31.1