From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id CDE213858D28 for ; Wed, 24 Nov 2021 20:04:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CDE213858D28 X-ASG-Debug-ID: 1637784285-0c856e2e462ae790001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id sHuursrAPh5vOmv4 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 24 Nov 2021 15:04:46 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@efficios.com X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from smarchi-efficios.internal.efficios.com (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by smtp.ebox.ca (Postfix) with ESMTP id C4652441D65; Wed, 24 Nov 2021 15:04:45 -0500 (EST) From: Simon Marchi X-Barracuda-RBL-IP: 192.222.180.24 X-Barracuda-Effective-Source-IP: 192-222-180-24.qc.cable.ebox.net[192.222.180.24] X-Barracuda-Apparent-Source-IP: 192.222.180.24 To: gdb-patches@sourceware.org Cc: Simon Marchi 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 X-ASG-Orig-Subj: [PATCH 2/3] gdb/linux-nat: factor ptrace-detach code to new detach_one_pid function Message-Id: <20211124200444.614978-3-simon.marchi@efficios.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20211124200444.614978-1-simon.marchi@efficios.com> References: <20211029203332.69894-1-simon.marchi@polymtl.ca> <20211124200444.614978-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1637784286 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 3216 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.94175 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Spam-Status: No, score=-19.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, 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: Wed, 24 Nov 2021 20:04:50 -0000 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