From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id C93B33858433 for ; Fri, 19 Jan 2024 16:50:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C93B33858433 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C93B33858433 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705683032; cv=none; b=Dl3h+kvgucVIwYzpD6hVpo2iNj3M+TCo/4ag9P2tCqwipU3SVSkgz2qRZb1QL8Q9qJ77C9WeHBcrz5ZjqZPIFKqJsn/faqVXCGzpiqtlPd/U/ya5v9qjevPFhGH0cMuM1uiedCSPpI3Xx72FNypohwr5YOLxB/PcYfYX9R6FKeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705683032; c=relaxed/simple; bh=6frp2+hxrQb9Z5xm4eauA0fl1EqvuCLgs5x2HCgVlpw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=UzLqpLVVHI/MiOJhUe+leIvPPgzK2qABOXFA+6KqpeYp7ZAsw8M+JNk+bkIvrrHuTXnmePFwOejXvB+nx4ZySrEX2WyKNkOh8efZjmLocr6pCwD3tR0tFXK7FtfTPVJD70IH0Pd3E7Q7Rn0Adba2gRpuefScrtL/BV0RM6wgh9U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1d6ff29293dso8539395ad.0 for ; Fri, 19 Jan 2024 08:50:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705683029; x=1706287829; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uoM74j/+uOvpGk/6FxEfCGBbvs5N2RHSbeBz7nW7Pi0=; b=DueMWrdgbZ0p9SPoAEC4edYEG54vrCNNuhjWQpXUCjmXjDX8YeoT010clfxrWt6kzj 2hNTC3etlXWdlV2J0Y4DbaThoCChqkNxB57TSpja460xx0bh3SBS7rpS0Kbk9gMKhNSY ibarsYllxaICEcbILe4+Ap2rEQJgKOPYe7W9IopUiQOQ8+WaS8crpUBXb5/GL9VLTRyZ cgZWzcs28/zXsgfUAG1+dYXC6DXzS17uR4Tf5YEjALqRfxHxVPiXSe28qGOWuZ8w3aRm yjCCK0wUA/nSZf75q2T4oi2pp1QP01b76Xa3DThXEX2TJGlnPWveChFxGCqwoVioCjbV tMzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705683029; x=1706287829; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uoM74j/+uOvpGk/6FxEfCGBbvs5N2RHSbeBz7nW7Pi0=; b=E6MOOnzoxBH87DckcH6o5XTRNB0RQUNdbbGPIhgVquo/RPDjr9v7eNtlz3HR2Ux6GE n5co/HztbQntrAnLmGJtFAaeTT2kqKFghxTlFg0BOEGHQXzzM3m5SB8k6RQrFs71V4f7 5d5oCvyPbuH4CtB2WPvCNugpdd8iBdJkHLzohTDlVy3yrG8Dv2T5kQzudRyEJ8eFzbH2 2yi5/MUo2qliExn/yUIPSg+l0N9BIa0aWIjyiemYa+nfoNaqjt8AAmxkPRHJLBuAhkur 40xPLp8JeA8zVP0bP1auwuquY/OXhslXTRP7FznLdcK/wIuzh/aV1aO1pdccpRiLjapg 5UAQ== X-Gm-Message-State: AOJu0YzJ5Vj0IOTyd6M8ydzoeaC6KyiFP9VFKXJ8jMR1Nk4MqGSCRI6M YmEuIAWAPyMvACEA2AX66Ct0GoanVvESG+50YADbDMFgyBiYGTPBvIX0SER4 X-Google-Smtp-Source: AGHT+IEs0bnxsQZzP0CeEHotl1XUoP9bBidMxEVWyPhpvxRXWwuXyfa47L4CMmkMRcXx0ikJZbh4FQ== X-Received: by 2002:a17:903:124b:b0:1d4:fd84:bee2 with SMTP id u11-20020a170903124b00b001d4fd84bee2mr214109plh.7.1705683029551; Fri, 19 Jan 2024 08:50:29 -0800 (PST) Received: from emerald.. ([2804:10f8:433d:9800:5f61:49b3:47cd:689f]) by smtp.gmail.com with ESMTPSA id r6-20020a170902be0600b001cfc4d8ede5sm3215830pls.301.2024.01.19.08.50.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 08:50:29 -0800 (PST) From: Matheus Branco Borella To: gdb-patches@sourceware.org Cc: simark@simark.ca, Matheus Branco Borella Subject: [PATCH v2] Make `linux_info_proc` prefer using the LWP over the PID Date: Fri, 19 Jan 2024 13:49:22 -0300 Message-Id: <20240119164921.62544-1-dark.ryu.550@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240106024512.14270-1-dark.ryu.550@gmail.com> References: <20240106024512.14270-1-dark.ryu.550@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Normally, `linux_info_proc` would use the PID to determine which subfolder in `/proc` to read information from. While this is usually fine, it breaks down after the main thread exits, at which point the information in `/proc/$pid` becomes become unreliable, if it is available at all. While it is the case that most programs terminate after their main thread exits, some may continue running from detached threads, in which case `info proc` will start misbehaving. This patch addresses this by making it so that the LWP - the Lightweight Process ID, that, in the case of GNU/Linux is the number of the process backing up the thread[1] - is prefered over the PID. By doing this, `linux_info_proc` will always access valid procfs information, even after the main thread exits. [1]: https://man7.org/linux/man-pages/man2/clone.2.html Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31207 --- gdb/linux-tdep.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 82e8bc3db3c..4fa7a98adde 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -840,7 +840,17 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, if (current_inferior ()->fake_pid_p) error (_("Can't determine the current process's PID: you must name one.")); - pid = current_inferior ()->pid; + /* Seeing as, when the main thread exits, the information in /proc/$pid + * becomes unreliable, we should prefer using the current TID, whenever + * possible. */ + pid = 0; + struct thread_info *info = any_live_thread_of_inferior (current_inferior ()); + if (info != nullptr) + pid = info->ptid.lwp (); + + /* And fall back to the actual PID only when the TID is not available. */ + if (pid == 0) + pid = current_inferior ()->pid; } args = skip_spaces (args); -- 2.40.1