From patchwork Sat Apr 20 05:56:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 790522 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp1416484wrm; Fri, 19 Apr 2024 22:57:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUIrTOm3pCVBr5NgR/4Mq+gUoCpqCZirkJFcStQn2JYjUQX6qkPHVzXWYb5nQIvkv8ghnNW8Xg8J0UjObejjWyQ X-Google-Smtp-Source: AGHT+IGhNDNUkxNNe5aoqZG5maoN6GW7KJBmDvuru+BcCQNf2VMS5Zz8h9LIDJ39aXygjvN50zDp X-Received: by 2002:a05:620a:807:b0:78f:ec9:387f with SMTP id s7-20020a05620a080700b0078f0ec9387fmr4297793qks.56.1713592648376; Fri, 19 Apr 2024 22:57:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713592648; cv=pass; d=google.com; s=arc-20160816; b=j6ECJtk0VJbXrJyktqXBvG5mjip8LfkhPt+UjuX2ZJmL64oyYJRQPvPsIgAPovAiiD 6uFlxXeGlvmquPxm5LioysawbEGeFbtVaRcjWq62UWBaiXm1FKccfC5m8sDAWTO4SzbO slFcgcgh0OgBHfl0cqXc4ziwjMH883PZ1YWJ8dzxw3dy3CFTUP9xJ+r/pkT6jBlq+ffm xdK/M9739mJd/DoOUBzGuSGNTSkLnmwZdwo5EhKliETaCEXjCRrOO4SwQlJk88PWJbEQ mq6TdnRR7zjKOTni+WhlKv7kkuaa6q3tb3Iu/D2GEYa8AXfIY9L9GPgqQ8ZAf3E/Gf0Y fa1Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=IxqndCGpyWwGCj7KkX0XOLriud63z/VjYBlBWwzA58Y=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=W6k/mCAYNUsYKB0x9z1wsV7s09qe2cTVKEKHj9y7qTd7HkFspjkNNVRsIe5Td4DTqV gkO50+JTJbKDGtNBLrUaEFD87QJ98ThP8g3DMcyM+u/fvmVL7mfS8/V0QXgsKvj+6oQ0 DUvdrF7IKgRzufrEBW6Nd2AUCvrS+2JnvU5GGiQhewCaJIyZdF6tCAV1S9igCQEaeo3L 4Mn9yc0tqIssvLfw0qhRztlbnrf39pKBI+jiFa2KIry86vUrX+Km6/dm0saIZZ7xX9wq DzFxzel4GN1HpT1YpbdkxIo4fntx2XFu0d+GRYNMbuVfPFgvaivc2lJxZc3Vn2vENUTO VOKQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b7VY4AiZ; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j25-20020a05620a147900b0078a51670468si5459247qkl.438.2024.04.19.22.57.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:57:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b7VY4AiZ; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 772DA385840C for ; Sat, 20 Apr 2024 05:57:27 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id 2E80B3858D37 for ; Sat, 20 Apr 2024 05:57:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E80B3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2E80B3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::330 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592623; cv=none; b=FnokwpVlYXXeAM4LXZiQDG0B0XCLnWVqnIjlHeWraAbfRiBg1FIKGgoJKyqeMmdQ/vhgsw5Sedl1aMAKHLILLr/Vu2boABlDT1w5Xq9Uxwd16u5hC1/W06Q91jEKCxbJT8MxkfJSMYRR0nbk1A7HTfCW2TUexZRtP17OsH2YLsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592623; c=relaxed/simple; bh=XopwI2VdvcVvsEo0tP95k00O4qdDbyDhuc041TnMrME=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QxJpSwCdpu+DdO0lQQFYhzOEC2PcyxQ5yj2CYnE9840njMjMJH9AKXeIHKlIeAvEZnn3lsPUI8v4hW3wXYVZirPLypPnz3iwo9Vb1MYoG57bD9vgJGLHxTfarcC2p2CkGZOkLRgpyWtdZIKsPn0Oao/bQ6MQDwFXeGaBEYeaUus= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6eb658ca1ceso1934371a34.2 for ; Fri, 19 Apr 2024 22:57:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713592621; x=1714197421; 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=IxqndCGpyWwGCj7KkX0XOLriud63z/VjYBlBWwzA58Y=; b=b7VY4AiZafF4TBjBN5RWSnasNnBv9g8Yb3IKMBWMpLsKEKKGdWqIDwzoGju4+yZ4YV jo7oGNbbSlUhLu7v1frvU+5n2VypfoUJT9cdn0MnQIDUTAhkctIm9nhHX6jKun4brM/l 4e4re0RYgeN9T/8SdBTJLBiYnZqCXK3izjGe8lW9DC3e/Diz1ggasVj6t/qNbpcjPPO0 I5Rtl+4mKP5uXvXX5w+BcfDtCt274wmo+kgJbIvBMjUZBd1DV+l5cGt3kbdSl02dxUXO lh8CC/C5Pb7GLn0mf/bQDlFq1HhWrgO4z6bpf6ZASEM+QrS8cVOIRT3bn56daKwwzFZi EAfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713592621; x=1714197421; 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=IxqndCGpyWwGCj7KkX0XOLriud63z/VjYBlBWwzA58Y=; b=PKS5TZBlyATYMMGcQ5SxKTh+LKTNSgAok0Jv6RtkxWac/DWytd4Df+QnXHwF71cD1c YTJ6cSpfbYBHGDtarVBqf/6ExUHNpg+BA/+hDDKglZ2d7LmnrtxA8tFrp1csXYt+HUjD NXT/+hN13h3i+/e0XU2WBYtq1NjaxdYe+v3Wh8dM4SamGN/yRSSUvbFVR+16gKyj7ZSe HC/3OpgRMbfxRfx0kNPmnkQFcurukOL6XKY1PMTrFKIalqSt2F9YvHzoLhzOCzLQMzpj 9e0KcFSaooO/aBVNz9qdykBwovZvXv1oYjLBujQEUBcPDbp4m5Pv3m7EYCNUCZThQf1+ XEGw== X-Gm-Message-State: AOJu0Yz2HTHkNz4uCmr87t2RD6R46lL7O45k3ManFvMs+nufoZFTrAut UYIjwWCMfHYlrpwon9qE0VwWu3M6Zid/gJ8DvehrXklLAFwUZ2SZiFMu6DCj6PPp1GDpaS9TI4E A X-Received: by 2002:a9d:590c:0:b0:6eb:7685:b00 with SMTP id t12-20020a9d590c000000b006eb76850b00mr4857947oth.28.1713592621165; Fri, 19 Apr 2024 22:57:01 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:1348:72c4:7c65:61e7]) by smtp.gmail.com with ESMTPSA id x19-20020a634853000000b005dc5129ba9dsm4081164pgk.72.2024.04.19.22.57.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:57:00 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v2 1/3] gdb/nat: Use procfs(5) indexes in linux_common_core_of_thread Date: Sat, 20 Apr 2024 02:56:50 -0300 Message-ID: <20240420055652.819024-2-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240420055652.819024-1-thiago.bauermann@linaro.org> References: <20240420055652.819024-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patch=linaro.org@sourceware.org The code and comment reference stat fields by made-up indexes. The procfs(5) man page, which describes the /proc/PID/stat file, has a numbered list of these fields so it's more convenient to use those numbers instead. This is currently an implementation detail inside the function so it's not really relevant with the code as-is, but a future patch will do some refactoring which will make the index more prominent. Therefore, make this change in a separate patch so that it's simpler to review. Reviewed-By: Luis Machado --- gdb/nat/linux-osdata.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) Changes in v2: - Added macros for field indexes in /proc/PID/stat (Suggested by Luis). diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 6ffabe90aa7d..079f9ea20c37 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -52,6 +52,10 @@ typedef long long TIME_T; #define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1) +/* Index of fields of interest in /proc/PID/stat, from procfs(5) man page. */ +#define LINUX_PROC_STAT_STATE 3 +#define LINUX_PROC_STAT_PROCESSOR 39 + /* Returns the CPU core that thread PTID is currently running on. */ /* Compute and return the processor core of a given thread. */ @@ -74,10 +78,10 @@ linux_common_core_of_thread (ptid_t ptid) if (pos == std::string::npos) return -1; - /* If the first field after program name has index 0, then core number is - the field with index 36 (so, the 37th). There's no constant for that - anywhere. */ - for (int i = 0; i < 37; ++i) + /* If the first field after program name has index 3, then core number is + the field with index 39. These are the indexes shown in the procfs(5) + man page. */ + for (int i = LINUX_PROC_STAT_STATE; i <= LINUX_PROC_STAT_PROCESSOR; ++i) { /* Find separator. */ pos = content->find_first_of (' ', pos); From patchwork Sat Apr 20 05:56:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 790524 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp1416600wrm; Fri, 19 Apr 2024 22:57:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUQSqYTWnhzLG8Y7XcZb9dSPOgSVj1dpr29AIttLTShqgGdP5nIeUl6R9eGq+j1fw720g7xz+s1utR8n8JH7YtZ X-Google-Smtp-Source: AGHT+IHdW+SPXIh31mfeJu5vMSixZpWTeOBqbn23x3r6j0mptA5155DMsC/sXnKk6YacLrGijGuL X-Received: by 2002:a05:620a:5587:b0:78f:5f0:8e1a with SMTP id vq7-20020a05620a558700b0078f05f08e1amr4285458qkn.20.1713592673493; Fri, 19 Apr 2024 22:57:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713592673; cv=pass; d=google.com; s=arc-20160816; b=ddiyBdceyUMPE1icqn9cxcxZidTUV7rAjoZ7ZWlC/ydNO30yXFNf8DFO0Q8DK2vHDZ s27TVMk+RBv/wyf1YQpxHsJ2UVau+GeTYTMiHe/Qgw9x7zSOIwrA1vL2wDQvau2idCbW x0EmL2TLRicRXpWlUny7dbsbXaZO5JgPT5PfTDhsPRSBPq5b5Y9FfxSVg5ffGr0fmzME I7Xg28BtVfUk8MWmorabGz1Q/EXj+6e2qRuxY4EYO3cND/kUUnH9rNWws2D6mxMWueAj DRodBZSAoT6bFhbcsQjtA80rcnYNBEDOWy1dFvz0a2D1Nvi2LMP/cqOdQrje/rdA7rN7 GhgQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=Nw+SS1K8jhfBwIt0ZxqCGbJ2g3vFlsBr0EteLCo3p5o=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=jwPeB79YDX4bpRXeam3pSV8w42Nlc8m+zkXgDGSoAy3HBc3oj2ZI/yk6iB709Vsl2u 5rsiG9tJ2MvX0gArVL0cD+1KxQfjmcNWMoCK4F8vYnXU2SRl5JKlyGEWlCE90yNjN687 wD+iW8fsE/S0CHNyMz8xB0/G7o9xIlq2nuyJufVL61DrcednglwlO7XCX+LC/Un2v1fk Y9SqXiZ+G6ZHdKJk3xo4wVDo3gtlYOrFs0PI3KRZkfPg6NumkCwG06z5XL5x+UmATY+k s21rGmjoVlWJdeJIYWMENraXb9lWkUL96dKf9od1t6iAkDJXNSlTcZOrRVsSA7H6QhCG dzGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XzV1RN0l; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pi32-20020a05620a37a000b0078ec0e16362si5457888qkn.105.2024.04.19.22.57.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:57:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XzV1RN0l; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24E453858401 for ; Sat, 20 Apr 2024 05:57:53 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) by sourceware.org (Postfix) with ESMTPS id EB2F83858D38 for ; Sat, 20 Apr 2024 05:57:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB2F83858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EB2F83858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c2f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592632; cv=none; b=fdMtvdI5qp1wdESn0S/1xKlxeAStJtMXBSfGQKXJeJfJ0zx99cdh72izQ1+PJZzN8r3b7o7HnWVz79aVE8pJG6JPHwRtSjykqErAWORHUwgUg7d7B0YBmJVyiUOsqlaPC2r15qnlu75B+sQjgsQ4FZUTyeP0skAE3vEc73/0Tdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592632; c=relaxed/simple; bh=/K7scmfapdNXQYku+FhwNif4mYWy2e/kBjxt8KOyy+g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DFFB1/kRBJYekn/LPOhU0+xcRGFTu04TqKPXP06qOI2Kies4jJWATho/jSpUgUky2fQo8iRX6RymvWhTi4JpzedWgPq9O76thuVOciophXz8GlbWZnex8VSnh0y2T6cwbIsIECmMfJOs4hyncGxwDFbNO8q1Sr5KGZHxSI5DQE0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-5aa2bd6f651so1761168eaf.0 for ; Fri, 19 Apr 2024 22:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713592624; x=1714197424; 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=Nw+SS1K8jhfBwIt0ZxqCGbJ2g3vFlsBr0EteLCo3p5o=; b=XzV1RN0lqbPzsmiLyw1DOYn2YTLEJQY0oew6YNfSAzPkjYed92pee6prUmDBeUqezd gpvsv3oPvq63B7ynuJlW/nNFM0i0vRLLo1WTnQnfatPOpu7+DssLfD+uhRkfDwdUN7WD QZd+eqdDUpAhzySq8306TOHfduu30xCzcQ/KmhrP+khDQn9zJSJg5bBttYc40zee90bb ujwNVgDMGRmgJKjTH1rsa9cUenMutPQwgvPBBrguhEKjNq9LmgPdYvNrMI0woCYkzlhG xsYXze4fMEr0LbJJJP+miXZC1WAggOmptksy/6GtQkZ4SeODi+duHoAc9OkTWHmitB9b 4Wqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713592624; x=1714197424; 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=Nw+SS1K8jhfBwIt0ZxqCGbJ2g3vFlsBr0EteLCo3p5o=; b=Itp1xbJWHnEzpLc9SorLSyw0da5bfJN2rCvQ44d5SdHjpXEAr2PmD9Aw4llBMEx9tl VVpe14tsYBMY9fHR6SqyQYzeO6HmsWVsFT8xNNfPuxaIj8hO/2peIdW1+i7Lxe2R8HFE AhAoRJmlXYdPfK9+T6+rrTKC1tE7539djN26ynHKbqKFE53yWzZN5o1r4Yaq9HsL+83C jnOknq1xdAkXG+gAy3ptrHb7ovnfr6B23fqQtjosWj4Ra4F2kJTEAa7eKQGNdqURcVNM LjrFi+oVsF0OMswHX7r4RyDuxLnVtLwvSfv6hd21g6Zmd1w9fdz/6iuuVDcIcVOrDkbp jt6A== X-Gm-Message-State: AOJu0YzEYh4zzUxOD1G2Z9kZBUik8ELFhoIyYk2zB8TsIRRvmOWXPvPP YuKjrjlDisVyKgcLAv9os+lYqfU7soMsegZ4tB9PlIdUYAwQIzNz9bVs2TM+bVgPiC8KFj2ssCu J X-Received: by 2002:a05:6358:793:b0:186:16ef:444 with SMTP id n19-20020a056358079300b0018616ef0444mr4514555rwj.17.1713592624074; Fri, 19 Apr 2024 22:57:04 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:1348:72c4:7c65:61e7]) by smtp.gmail.com with ESMTPSA id fw9-20020a17090b128900b00299101c1341sm4117622pjb.18.2024.04.19.22.57.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:57:03 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v2 2/3] gdb/nat: Factor linux_proc_get_stat_field out of linux_common_core_of_thread Date: Sat, 20 Apr 2024 02:56:51 -0300 Message-ID: <20240420055652.819024-3-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240420055652.819024-1-thiago.bauermann@linaro.org> References: <20240420055652.819024-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patch=linaro.org@sourceware.org The new function will be used in a subsequent patch to read a different stat field. The new code is believed to be equivalent to the old code, so there should be no change in GDB behaviour. The only material change was to use std::string and string_printf rather than a fixed char array to build the path to the stat file. Also, take the opportunity to move the function's documentation comment to the header file, to conform with GDB practice. Reviewed-By: Luis Machado --- gdb/nat/linux-osdata.c | 42 +++++------------------------------------ gdb/nat/linux-osdata.h | 3 +++ gdb/nat/linux-procfs.c | 43 ++++++++++++++++++++++++++++++++++++++++++ gdb/nat/linux-procfs.h | 10 ++++++++++ 4 files changed, 61 insertions(+), 37 deletions(-) Changes in v2: - Added macros for field indexes in /proc/PID/stat (Suggested by Luis). - Moved linux_find_proc_stat_field from linux-osdata.c to linux-procfs.c and changed prefix to linux_proc (Suggested by Pedro). - Use string_printf in linux_proc_get_stat_field to build path to stat file, to avoid having to copy the PID_T and MAX_PID_T_STRLEN macros. diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 079f9ea20c37..3a6215015f12 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -36,6 +36,7 @@ #include #include "gdbsupport/filestuff.h" #include +#include "linux-procfs.h" #define NAMELEN(dirent) strlen ((dirent)->d_name) @@ -52,51 +53,18 @@ typedef long long TIME_T; #define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1) -/* Index of fields of interest in /proc/PID/stat, from procfs(5) man page. */ -#define LINUX_PROC_STAT_STATE 3 -#define LINUX_PROC_STAT_PROCESSOR 39 - -/* Returns the CPU core that thread PTID is currently running on. */ - -/* Compute and return the processor core of a given thread. */ +/* See linux-osdata.h. */ int linux_common_core_of_thread (ptid_t ptid) { - char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN]; + std::optional field + = linux_proc_get_stat_field (ptid, LINUX_PROC_STAT_PROCESSOR); int core; - sprintf (filename, "/proc/%lld/task/%lld/stat", - (PID_T) ptid.pid (), (PID_T) ptid.lwp ()); - - std::optional content = read_text_file_to_string (filename); - if (!content.has_value ()) + if (!field.has_value () || sscanf (field->c_str (), "%d", &core) == 0) return -1; - /* ps command also relies on no trailing fields ever contain ')'. */ - std::string::size_type pos = content->find_last_of (')'); - if (pos == std::string::npos) - return -1; - - /* If the first field after program name has index 3, then core number is - the field with index 39. These are the indexes shown in the procfs(5) - man page. */ - for (int i = LINUX_PROC_STAT_STATE; i <= LINUX_PROC_STAT_PROCESSOR; ++i) - { - /* Find separator. */ - pos = content->find_first_of (' ', pos); - if (pos == std::string::npos) - return {}; - - /* Find beginning of field. */ - pos = content->find_first_not_of (' ', pos); - if (pos == std::string::npos) - return {}; - } - - if (sscanf (&(*content)[pos], "%d", &core) == 0) - core = -1; - return core; } diff --git a/gdb/nat/linux-osdata.h b/gdb/nat/linux-osdata.h index 833915cdb2fd..a82fb08b998e 100644 --- a/gdb/nat/linux-osdata.h +++ b/gdb/nat/linux-osdata.h @@ -20,7 +20,10 @@ #ifndef NAT_LINUX_OSDATA_H #define NAT_LINUX_OSDATA_H +/* Returns the CPU core that thread PTID is currently running on. */ + extern int linux_common_core_of_thread (ptid_t ptid); + extern LONGEST linux_common_xfer_osdata (const char *annex, gdb_byte *readbuf, ULONGEST offset, ULONGEST len); diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c index e2086952ce6b..23231c301a3f 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -230,6 +230,49 @@ linux_proc_pid_is_zombie (pid_t pid) /* See linux-procfs.h. */ +std::optional +linux_proc_get_stat_field (ptid_t ptid, int field) +{ + /* We never need to read PID from the stat file, and there's + command_from_pid to read the comm field. */ + gdb_assert (field >= LINUX_PROC_STAT_STATE); + + std::string filename = string_printf ("/proc/%ld/task/%ld/stat", + (long) ptid.pid (), (long) ptid.lwp ()); + + std::optional content + = read_text_file_to_string (filename.c_str ()); + if (!content.has_value ()) + return {}; + + /* ps command also relies on no trailing fields ever contain ')'. */ + std::string::size_type pos = content->find_last_of (')'); + if (pos == std::string::npos) + return {}; + + for (int i = LINUX_PROC_STAT_STATE; i <= field; ++i) + { + /* Find separator. */ + pos = content->find_first_of (' ', pos); + if (pos == std::string::npos) + return {}; + + /* Find beginning of field. */ + pos = content->find_first_not_of (' ', pos); + if (pos == std::string::npos) + return {}; + } + + /* Find end of field. */ + std::string::size_type end_pos = content->find_first_of (' ', pos); + if (end_pos == std::string::npos) + return content->substr (pos); + else + return content->substr (pos, end_pos - pos); +} + +/* See linux-procfs.h. */ + const char * linux_proc_tid_get_name (ptid_t ptid) { diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h index 880afbcdd615..ec1f37651fbf 100644 --- a/gdb/nat/linux-procfs.h +++ b/gdb/nat/linux-procfs.h @@ -54,6 +54,16 @@ extern int linux_proc_pid_is_zombie_nowarn (pid_t pid); extern int linux_proc_pid_is_gone (pid_t pid); +/* Index of fields of interest in /proc/PID/stat, from procfs(5) man page. */ +#define LINUX_PROC_STAT_STATE 3 +#define LINUX_PROC_STAT_PROCESSOR 39 + +/* Returns FIELD (as numbered in procfs(5) man page) of + /proc/PID/task/LWP/stat file. */ + +extern std::optional linux_proc_get_stat_field (ptid_t ptid, + int field); + /* Return a string giving the thread's name or NULL if the information is unavailable. The returned value points to a statically allocated buffer. The value therefore becomes invalid at the next From patchwork Sat Apr 20 05:56:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 790525 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp1416709wrm; Fri, 19 Apr 2024 22:58:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVgIqj7vKM+qglTpzBLfYS+qKZXyNdVkrNOcYnjgVkLuv0x216mUaMMf4sBA1hFhf4DlnWnzQ/fmmMKRPkgI93G X-Google-Smtp-Source: AGHT+IHQU0FMZBLwGWqCb6ou32a3L4IW33nIojNR4MEGtN2zGWYx5egHo/zfZIM3uT5/H7Xq3HD7 X-Received: by 2002:ac8:57cd:0:b0:439:73c:3e5e with SMTP id w13-20020ac857cd000000b00439073c3e5emr3741915qta.29.1713592699213; Fri, 19 Apr 2024 22:58:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713592699; cv=pass; d=google.com; s=arc-20160816; b=0l7j5WmJlF+vxRJmCdf74MmM5GFSskSLgR1i1sPNzvzlVqeCYOAvyXVHqhgspvunUW HMFl8qhKCy5sAXJwcu1TMeHQyJ7185SMBZ7mu3SvuuyO8eFplK7mXALA0P1bLEocptoq XqNL+TulHtOPXY4OD81erCdtQ+p4N4Px+MyKqCvhVA9EKDYqMR75F1789MA7J5eEok3O S0GNfndISco8C6AvXsj8oBSrxkpwJ3cq7mO+bF1z2ZATCZ11zf29/aSXS3vAjW0CpzKO AZGqGGRmPfeHBXCjuulAGR1mj1rHbroQ1ZA9FYQvu3U2hiWp1oJBi4Vs0CAsbgp54XkT MKvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=olz/BBLKUX6Bt8KDb3wGVUfMhXAacqz1CjV8L0Y2bx4=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=GewHP5zjhK3saaAFPGDoGOzdMhc1FQiSCN77lZOd6jf9uHA327dPaRILn5sxGoTct2 RgoZXJsjcTXodmrwwOuWVwCqcoKapdpqI5vYPx1IOxvjwB7K5wrvDQcgrRFr22Qj2yIf AHfAfUXpQE5YIGRMz22yC8Z0sDBNQDoqpvqb/hE0WaKVlB0Ed7U7PD4IHAXTOUoK2amB NXRUuo4cb5VTJ19bjJfB5KyhwK2GXc7ENxOFv+FSD+jhL193n/a2p6L3YvTmwIwIvPUW 669K7FteAs1VhSGnsI/56CYajiLWgBnHh9iOXVRqNNAHIXLT0pooV4P+zFcWH4cescV/ n69g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="azU/kMp8"; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v26-20020a05620a0a9a00b0078ebe617b73si5390380qkg.761.2024.04.19.22.58.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:58:19 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="azU/kMp8"; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D188B3858D3C for ; Sat, 20 Apr 2024 05:58:18 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id D1CE9385840F for ; Sat, 20 Apr 2024 05:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1CE9385840F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D1CE9385840F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::532 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592632; cv=none; b=PUD/JgS0ivin41oA9sHdmfdJlHB7cN/NX35uoIqbVED/SXfsB70cqhpsLXiiTOwpgBQ8U7zdyl6e9VZ+Ih5CJSzVOb5Z9RW3qo0hjYgIqN9IFVstTajIrE2YOCd7j1ChnqjKxXM5fngsrBz9r9MmNe9Qc3+ixloKp+PhMWp2YOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713592632; c=relaxed/simple; bh=nkIrD9kMcrZgWYmoP0aHY6NlTxWTe49hs3AuNxu6LEg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=V25bW1KaXDS7PydHMEf8DdK3I6Z97V0k0ND2nKiCcPwFMCU/pTQJA2ErhuSWsXqG8+ctEntK3RCe1WX3KnoIAeGuGCNytlfbEyFfpNsPYYf7/ebCu+hG7xPbD3AJcI7JMb8MOXhBdeee+XEk3aVGn0K2wd6k8dCO5J+Kf+bcDho= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-5f415fd71f8so2092009a12.3 for ; Fri, 19 Apr 2024 22:57:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713592628; x=1714197428; 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=olz/BBLKUX6Bt8KDb3wGVUfMhXAacqz1CjV8L0Y2bx4=; b=azU/kMp8Y9cVF7TaelqtqT/tmFGQaXPid/YFpMEP6wgg/AQCj5qBFytVsnCXPsm7Qo x6Z2o9sybzdyZEdUibqB0BEhAJo9MDDVmUYhnzv4vfLIxC4PMH8y7zg+XufMqO50/N1P AuSGpznBRRr1z1+At5bBMrXhXd88DUu7Ru1Fd0iW/M6DV6S0mPdfv8Dbuz5OX9Dm6YLf 4iMHGNbuB50pPejycpyQE9eKuOvpOIj/rpC5ib9tV7lG8VTSdsqR3l8+evDpRzfts9t9 M34WziMagCoieJuPnI5j4K5oyRQu7m12vcMdgvujNj4FTY6cF6E7p5Ys/PZgouY/9qjH LS1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713592628; x=1714197428; 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=olz/BBLKUX6Bt8KDb3wGVUfMhXAacqz1CjV8L0Y2bx4=; b=afyHL0e7YyKviPyyGxT701GLPF06P3IBiRQqYhz+0nT81qNf9xCq2TDOXr03a7U8ix kRkPptmFuU1SxOehJl2lMBSGuZAg9On0CgEsTQJFvxZ0et0+LbC01oYzzgmRTjY3gfEc OOeOo2PzAwrN2JE9hNDBjUZnAeXFW0UXhhnkxUQWk3Xz68pHWXFStwpohvQAxGdsVgGg g9EyNftvHcOyBUGwOWf9soc+3/5xx5rQmSucRNUYY33iX5kAe/F+LrU61+ESUZClscaC Rym9qih67sgL0WiGHWjy4MBD6X06Ulk574SwrM/5nJ9U43wGDaSDjDzVW9TafN5hw3ah GnyQ== X-Gm-Message-State: AOJu0YxkN78Y6yBbI15G5aEe6t9DYidi0F/LpzEPhzAfgMuzTiH6ORGW efUkrogJSI5ybEOv7bddNosQ9kE1Xc69cfhfwWgV8QZSYfqdXIEG1ONsZbOlEojxaX/LXO4h0Ge o X-Received: by 2002:a17:902:e807:b0:1e5:556:60e2 with SMTP id u7-20020a170902e80700b001e5055660e2mr5319481plg.5.1713592627740; Fri, 19 Apr 2024 22:57:07 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:1348:72c4:7c65:61e7]) by smtp.gmail.com with ESMTPSA id t20-20020a170902b21400b001e3e081dea1sm4337377plr.0.2024.04.19.22.57.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 22:57:07 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v2 3/3] gdb/nat/linux: Fix attaching to process when it has zombie threads Date: Sat, 20 Apr 2024 02:56:52 -0300 Message-ID: <20240420055652.819024-4-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240420055652.819024-1-thiago.bauermann@linaro.org> References: <20240420055652.819024-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patch=linaro.org@sourceware.org When GDB attaches to a multi-threaded process, it calls linux_proc_attach_tgid_threads () to go through all threads found in /proc/PID/task/ and call attach_proc_task_lwp_callback () on each of them. If it does that twice without the callback reporting that a new thread was found, then it considers that all inferior threads have been found and returns. The problem is that the callback considers any thread that it hasn't attached to yet as new. This causes problems if the process has one or more zombie threads, because GDB can't attach to it and the loop will always "find" a new thread (the zombie one), and get stuck in an infinite loop. This is easy to trigger (at least on aarch64-linux and powerpc64le-linux) with the gdb.threads/attach-many-short-lived-threads.exp testcase, because its test program constantly creates and finishes joinable threads so the chance of having zombie threads is high. This problem causes the following failures: FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: attach (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: no new threads (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: set breakpoint always-inserted on (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: break break_fn (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: break at break_fn: 1 (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: break at break_fn: 2 (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: break at break_fn: 3 (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: reset timer in the inferior (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: print seconds_left (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: detach (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: set breakpoint always-inserted off (timeout) FAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: delete all breakpoints, watchpoints, tracepoints, and catchpoints in delete_breakpoints (timeout) ERROR: breakpoints not deleted The iteration number is random, and all tests in the subsequent iterations fail too, because GDB is stuck in the attach command at the beginning of the iteration. The solution is to make linux_proc_attach_tgid_threads () remember when it has already processed a given LWP and skip it in the subsequent iterations. PR testsuite/31312 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31312 Reviewed-By: Luis Machado --- gdb/nat/linux-procfs.c | 53 ++++++++++++++++++++++++++++++++++++++++++ gdb/nat/linux-procfs.h | 1 + 2 files changed, 54 insertions(+) Changes in v2: - Added macro for field index in /proc/PID/stat (Suggested by Luis). - Moved linux_get_starttime to linux-procfs.c and changed its prefix to linux_proc (Suggested by Pedro). - Changed visited_lwps from std::set to std::unordered_set. Had to add a hash function (Suggested by Pedro). diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c index 23231c301a3f..2ed7b36ecd55 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -20,6 +20,8 @@ #include "gdbsupport/filestuff.h" #include #include +#include +#include /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not found. */ @@ -271,6 +273,29 @@ linux_proc_get_stat_field (ptid_t ptid, int field) return content->substr (pos, end_pos - pos); } +/* Get the start time of thread PTID. */ + +static std::optional +linux_proc_get_starttime (ptid_t ptid) +{ + std::optional field + = linux_proc_get_stat_field (ptid, LINUX_PROC_STAT_STARTTIME); + + if (!field.has_value ()) + return {}; + + errno = 0; + const char *trailer; + ULONGEST starttime = strtoulst (field->c_str (), &trailer, 10); + if (starttime == ULONGEST_MAX && errno == ERANGE) + return {}; + else if (*trailer != '\0') + /* There were unexpected characters. */ + return {}; + + return starttime; +} + /* See linux-procfs.h. */ const char * @@ -332,6 +357,21 @@ linux_proc_attach_tgid_threads (pid_t pid, return; } + /* Callable object to hash elements in visited_lpws. */ + struct pair_hash + { + std::size_t operator() (const std::pair &v) const + { + return (std::hash() (v.first) + ^ std::hash() (v.second)); + } + }; + + /* Keeps track of the LWPs we have already visited in /proc, + identified by their PID and starttime to detect PID reuse. */ + std::unordered_set, + pair_hash> visited_lwps; + /* Scan the task list for existing threads. While we go through the threads, new threads may be spawned. Cycle through the list of threads until we have done two iterations without finding new @@ -350,6 +390,19 @@ linux_proc_attach_tgid_threads (pid_t pid, if (lwp != 0) { ptid_t ptid = ptid_t (pid, lwp); + std::optional starttime + = linux_proc_get_starttime (ptid); + + if (starttime.has_value ()) + { + std::pair key (lwp, *starttime); + + /* If we already visited this LWP, skip it this time. */ + if (visited_lwps.find (key) != visited_lwps.cend ()) + continue; + + visited_lwps.insert (key); + } if (attach_lwp (ptid)) new_threads_found = 1; diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h index ec1f37651fbf..64224801c8f2 100644 --- a/gdb/nat/linux-procfs.h +++ b/gdb/nat/linux-procfs.h @@ -56,6 +56,7 @@ extern int linux_proc_pid_is_gone (pid_t pid); /* Index of fields of interest in /proc/PID/stat, from procfs(5) man page. */ #define LINUX_PROC_STAT_STATE 3 +#define LINUX_PROC_STAT_STARTTIME 22 #define LINUX_PROC_STAT_PROCESSOR 39 /* Returns FIELD (as numbered in procfs(5) man page) of