From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by sourceware.org (Postfix) with ESMTPS id ECDF53858D20 for ; Sat, 6 Jan 2024 02:45:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ECDF53858D20 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 ECDF53858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1030 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704509141; cv=none; b=VDkcyGhyMOWI38OOL2IN7echs1MVL4LPgiwbX7bEAxeS+hZgN4Px3PsodkkKoaNrco8OH80aclLavqHoQSwNF3SzjLkpDuZ9l/HCuu8v+KdT8Bt1vUZ1bKNlQDIMCylg+R/j+S+Y/+Z/YKwaD78bVl9ZT1vGZnTTdB+nwhzbijA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704509141; c=relaxed/simple; bh=/wXsQvbr3b4Tpts9FbvgymceYhsfdVM5JI7Z3fYq/OA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=p5jNNENe3AWcKd/a8k+TqAQjUdMprtetCzVkIm8RqahVaF/S/ImhewqtWHHQ8kR6pgkK3hxsn0xnvLjW6iexyWH9iFyt2/ZjFV9Pr2gIXB3xfCXN/wbXER+WyKtHFoNcWOnih//midN9PBkEjKDKsghWUe4RWkgZu6IIy8pBy+A= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-28cb3bc3fe7so129608a91.1 for ; Fri, 05 Jan 2024 18:45:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704509137; x=1705113937; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=1HPyhetbJUsW28xcx5/Zezh+0uX47CJqcoMut321tN8=; b=CaLYY7ADsXCAiyzoK9OrUXog3EQYBui4K503eu/PMj+y66PdGxvJJIrDrM08PEcSaK hRbjyRjHVGZnEVwsvYqXGMd2TKZGMwGMdxzClaMknj1LEgrNkLSDpJqN5aw9b0XUsuoX bkROQGRf8w7VYgrt4HdQo26p1eNRjnSqAOeJtblDelGwC30dOI76GiSXetYRW/4Yqcih EwxBc2TZv4d4kOba16FO3G5tPFXVQ/r6mTqRWl0qAEGm5y6ZYVSN7SIT1FgDI9lV2qDR DKv1C0D88ZxxtMYV6gCA55SxJ0XgLGRw7N7ASLD73hN0VP2aTDbp79bf8qQTl8lRI2BU Bg6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704509137; x=1705113937; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1HPyhetbJUsW28xcx5/Zezh+0uX47CJqcoMut321tN8=; b=c7k1Vo/Lahfdxoqf+9GRAU/sQyBJ29GXNIKv+5VYzpZtQs2Mv4sKLLdh2S/KRMu+Dr uvTVjdML2+fjwqPYeAvp7ukcKsHY1XJh4xVKqkRVRD3vK3mgNCxtK/dpZ33i7yfh7z1D 91vmp2Uf5tUj3XkYLm33BoqRtwxyCUCVkOg+kD/Kpo4Ri6R8+Uz4moI2/Xu/r36cy4zx 207OiOvJkMPT7ODI5e0xze7/Y42LFWwWa5bJN5i2dfw2qOUfsPYlo41bnbh9anwzVRm4 CWCF2Ur1ZtAJJv5NQv5QwEZZaaY1IPWJiWgSCU0HMk8OGCyc1Pe3fgTWTvNEQ+quVxE1 H+Nw== X-Gm-Message-State: AOJu0Yxqu5ISkAZdfi+u8FO1qF4mQLItgbFFJ4Q6KaS7/WxbKaes/+lq KEb0EAz+3qKge6Dpeaabt2kFRkAKCA0= X-Google-Smtp-Source: AGHT+IHTujDR5rJ9ipqYDRlAKs6lx8jLTWGxiSdLayiEMBoebzsK3Sx5EAq3cwYdTcu3eyicS9p9pw== X-Received: by 2002:a17:90a:7503:b0:28c:815e:e0f0 with SMTP id q3-20020a17090a750300b0028c815ee0f0mr546814pjk.37.1704509136578; Fri, 05 Jan 2024 18:45:36 -0800 (PST) Received: from emerald.. ([2804:10f8:433d:9800:cb68:becf:dd34:e487]) by smtp.gmail.com with ESMTPSA id 29-20020a17090a19dd00b0028d2741085csm409855pjj.41.2024.01.05.18.45.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 18:45:36 -0800 (PST) From: Matheus Branco Borella To: gdb-patches@sourceware.org Cc: Matheus Branco Borella Subject: [PATCH] Make `linux_info_proc` prefer using the LWP over the PID Date: Fri, 5 Jan 2024 23:45:13 -0300 Message-Id: <20240106024512.14270-1-dark.ryu.550@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.9 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: Fixes: https://sourceware.org/bugzilla/show_bug.cgi?id=31207 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 --- gdb/linux-tdep.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 82e8bc3db3c..2c91e298d45 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -840,7 +840,14 @@ 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 = inferior_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