From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id tOHJEF4uAGFNfgAAWB0awg (envelope-from ) for ; Tue, 27 Jul 2021 12:03:42 -0400 Received: by simark.ca (Postfix, from userid 112) id 34E7F1EDFB; Tue, 27 Jul 2021 12:03:42 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3C9B61E813 for ; Tue, 27 Jul 2021 12:03:40 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8DBD383F41E for ; Tue, 27 Jul 2021 16:03:39 +0000 (GMT) Received: from mx2.freebsd.org (mx2.freebsd.org [96.47.72.81]) by sourceware.org (Postfix) with ESMTPS id 97D5E385801A for ; Tue, 27 Jul 2021 16:03:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 97D5E385801A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=FreeBSD.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits)) (Client CN "mx1.freebsd.org", Issuer "R3" (verified OK)) by mx2.freebsd.org (Postfix) with ESMTPS id 0C840857DD; Tue, 27 Jul 2021 16:03:28 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GZ1mb6m9Bz4gL2; Tue, 27 Jul 2021 16:03:27 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from John-Baldwins-MacBook-Pro.local (ralph.baldwin.cx [66.234.199.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: jhb) by smtp.freebsd.org (Postfix) with ESMTPSA id 7E02A27E48; Tue, 27 Jul 2021 16:03:27 +0000 (UTC) (envelope-from jhb@FreeBSD.org) To: Simon Marchi , gdb-patches@sourceware.org References: <20210727002421.18947-1-jhb@FreeBSD.org> <20210727002421.18947-4-jhb@FreeBSD.org> <268f5540-4418-103b-1cb2-84026af99606@polymtl.ca> From: John Baldwin Subject: Re: [PATCH 3/3] obsd-nat: Various fixes to obsd_nat_target::wait. Message-ID: <15f826bb-0fe0-87cb-bf01-19c373bfd3f8@FreeBSD.org> Date: Tue, 27 Jul 2021 09:03:26 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <268f5540-4418-103b-1cb2-84026af99606@polymtl.ca> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit 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: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" On 7/26/21 6:54 PM, Simon Marchi wrote: > > > On 2021-07-26 8:24 p.m., John Baldwin wrote: >> - Call inf_ptrace_target::wait instead of duplicating the code. >> Replace a check for WIFSTOPPED on the returned status from waitpid >> by checking for TARGET_WAITKIND_STOPPED in the parsed status as is >> done in fbsd_nat_target::wait. >> >> - Don't use inferior_ptid when deciding if a new process is a child vs >> parent of the fork. Instead, use find_inferior_pid and assume that >> if an inferior already exists, the pid in question is the parent; >> otherwise, the pid is the child. >> >> - Don't use inferior_ptid when deciding if the ptid of the process >> needs to be updated with an LWP ID, or if this is a new thread. >> Instead, use the approach from fbsd-nat which is to check if a ptid >> without an LWP exists and if so update the ptid of that thread >> instead of adding a new thread. >> --- >> gdb/obsd-nat.c | 61 +++++++++++--------------------------------------- >> 1 file changed, 13 insertions(+), 48 deletions(-) >> >> diff --git a/gdb/obsd-nat.c b/gdb/obsd-nat.c >> index 46fdc0676e..a6612a982b 100644 >> --- a/gdb/obsd-nat.c >> +++ b/gdb/obsd-nat.c >> @@ -26,7 +26,7 @@ >> #include >> #include "gdbsupport/gdb_wait.h" >> >> -#include "inf-child.h" >> +#include "inf-ptrace.h" >> #include "obsd-nat.h" >> >> /* OpenBSD 5.2 and later include rthreads which uses a thread model >> @@ -76,47 +76,14 @@ ptid_t >> obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, >> target_wait_flags options) >> { >> - pid_t pid; >> - int status, save_errno; >> - >> - do >> - { >> - set_sigint_trap (); >> - >> - do >> - { >> - pid = waitpid (ptid.pid (), &status, 0); >> - save_errno = errno; >> - } >> - while (pid == -1 && errno == EINTR); >> - >> - clear_sigint_trap (); >> - >> - if (pid == -1) >> - { >> - fprintf_unfiltered (gdb_stderr, >> - _("Child process unexpectedly missing: %s.\n"), >> - safe_strerror (save_errno)); >> - >> - /* Claim it exited with unknown signal. */ >> - ourstatus->kind = TARGET_WAITKIND_SIGNALLED; >> - ourstatus->value.sig = GDB_SIGNAL_UNKNOWN; >> - return inferior_ptid; >> - } >> - >> - /* Ignore terminated detached child processes. */ >> - if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ()) >> - pid = -1; >> - } >> - while (pid == -1); >> - >> - ptid = ptid_t (pid); >> - >> - if (WIFSTOPPED (status)) >> + ptid_t wptid = inf_ptrace_target::wait (ptid, ourstatus, options); >> + if (ourstatus->kind == TARGET_WAITKIND_STOPPED) >> { >> ptrace_state_t pe; >> - pid_t fpid; >> + pid_t fpid, pid; >> + int status; >> >> + pid = wptid.pid (); >> if (ptrace (PT_GET_PROCESS_STATE, pid, (caddr_t)&pe, sizeof pe) == -1) >> perror_with_name (("ptrace")); >> >> @@ -137,7 +104,7 @@ obsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, >> >> gdb_assert (pe.pe_report_event == PTRACE_FORK); >> gdb_assert (pe.pe_other_pid == pid); >> - if (fpid == inferior_ptid.pid ()) >> + if (find_inferior_pid (this, pid) != nullptr) >> { >> ourstatus->value.related_pid = ptid_t (pe.pe_other_pid); >> return ptid_t (fpid); > > I don't really have comments on this, since I am not familiar with the > OpenBSD specifics. But I don't understand this code above. fpid refers > to the fork child pid? And pe.pe_other_pid as well? So it's as if we > return that the child has forked? What am I getting wrong? My reading of the code is that when a process forks, both the parent and child report identical PTRACE_FORK events where pe_other_pid is the pid of the other process. For TARGET_WAITKIND_FORKED, both processes need to have reported their events (which is then reported as a single logical event to the core), so when ::wait sees the first PTRACE_FORK event, it immediately waits for the other process to report an event (and asserts it is also a PTRACE_FORK). fpid is the pid of the second process to report an event, and pid is the pid of the original process that reported an event. The find_inferior_pid check is used to determine which of 'pid' or 'fpid' is the parent. If find_inferior_pid (this, pid) finds an existing inferior, then 'pid' is the parent and 'fpid' is the child. It does seem though that both the existing code (that was checking inferior_ptid) and the new version both return a ptid for the child process. fbsd-nat.c returns the ptid of the parent with the child pid set in the ourstatus->value.related_pid. Oh, I see what I did wrong, the old code was comparing 'fpid' against inferior_ptid and I changed this to 'pid'. -- John Baldwin