From patchwork Wed Apr 24 23:15:49 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: 791576 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:346:15ad:a2a with SMTP id g4csp1100982wrq; Wed, 24 Apr 2024 16:16:50 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUO4LVUus58kWJgdqhde5b05wAQKBVBhEiFyXOLUcnlYnhM05VXq+XtSpy3V/KGjChax50c1N82lhROduBoznH7 X-Google-Smtp-Source: AGHT+IGOxM4UOL3fVx0NuVHLzMUx34WaqIYrLTOHHeuqDDvJLrBW+mBOve7xOyYKaSzbd9bxWBys X-Received: by 2002:a54:481a:0:b0:3c8:42f5:4b5a with SMTP id j26-20020a54481a000000b003c842f54b5amr3757206oij.13.1714000609883; Wed, 24 Apr 2024 16:16:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714000609; cv=pass; d=google.com; s=arc-20160816; b=ksIjvVlm6M0/SncO153L9q+UKRTm+wrt381rkw7JOYtpKqkl5Of6o6gEFwi1i/QH6J TtNZPrdlRcasKlWUDAlRm9bh+iyeY36JfsWt/rdtHzfAVkXmk0ROclJh0XPY42cNzH16 7AV+foeMJreASTH2z3NIc4ezxgc89dg+w+5Yr+9nl8zGtqWDdtS+yHLTWLJKdUkqDfn/ u2n7/4qOfrB3KUf7vwgckWswt4Qy15R98toWdcWOwkqAaA2O+2vY8QIx5PyPNy5LY+4F q3DLW1Ky1HaYkL3PieqssS9ADuHXn3kqRMBdFomy3V3DpC6QhXuoR8XPRZyfDiXM94dI JCew== 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=fYBh+n9IwUhNo/7khRr/3J4ptTWU7xn/Zet84InQcgE=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=yETMFO6yVTqn6LWL9g+uPKXIkHQgx/0zEqLIV/F1K6pWLLqv0/vzNzfpGJaqeLmPUI wvbtUmHMf03EYezhWsgDOaZ7XdNo2324fjbQHKzTYY4E8SxVBLU1V9K5jG8D5EZtTUET JezFoYARLkzLj08JU1l356eUNhvWJIiG/ITzzgkKQVWHaaE+P6SvOMc5viW3AbZJxgUp J2kZlXIyJ2VRiaYG7J9wslnPvGoIOw8en3/cKjefSSmMRUvQpBcM7i0SDZaL9ph9CPrd s3QRxwaBBvO934ABkrkoy2DzA3p6zT3ZaXkPy1Q2jUxa9qTGhjs77mGgx5WH2qwM6w/o BXng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oWKvCDbP; 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 y20-20020a37e314000000b0078ecf9c3480si15191311qki.555.2024.04.24.16.16.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:16:49 -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=oWKvCDbP; 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 80D9D38449C1 for ; Wed, 24 Apr 2024 23:16:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 388D43846410 for ; Wed, 24 Apr 2024 23:15:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 388D43846410 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 388D43846410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000561; cv=none; b=Wg7zGwfTH8g67XGBBHjZ32XOLrm5tOMdZLAbcPU9mFXEt/cZ/53H20+1u2GhgaaBHd4mYfU6rAwBgSS16V4n1rySeTLpNmqBg3c5aYaZr040FgrS88hKZyigPJgAcyDtunVwf7+CG4dAzRfDr3qSewztBavbnpcP5er4UQvzYi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000561; c=relaxed/simple; bh=1xGhkGDmn9yKghDG/UQUQESMBKjS89m7wdcPxf2ymNU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GKTx8WyEL8BnY7yPX+qGHNpZ9GyZ/qQif6PzBaSfhzIp4/HZVGZTJyJq+ZbcqOatFA1elrxRdBEe8PB0p+ltnO/ap+SGmYUz8NWfVgoOusx+D1/+KNBHwd+b+AkegXjZmDCBEx3ItFV8U4AcI0u54qLIAoq4itPawl/l66mLQ4o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1e83a2a4f2cso2662075ad.1 for ; Wed, 24 Apr 2024 16:15:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714000558; x=1714605358; 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=fYBh+n9IwUhNo/7khRr/3J4ptTWU7xn/Zet84InQcgE=; b=oWKvCDbPCBVy2CtMX3UDKmg5ePZckvhOi3uEXXkLs9I0VsH51DFstv+PY9FReUrjuG LFiHtu19bGWPZz6OTrUVxqscmAf/Fn3SxHapmXKnSSEVtuVH/jAmxOLBM41wl0xjazXJ nuqfs7tP6Wxs7fGjAZ12c5nhbJg8/AykThsvVNOzAZYwnetfWUrJMI+ctw+cXoJke/rp ZC05Hl7HEOt1TKu3zf5GM+C6QP0TXphSQbVzFHBC/12gZ5LnIfZRCaPTzG29R0+Gl10J 4dir2Q5vmfqoY6wEMttqQ1umCw08HKVGzt2AXvEx8Km8ojRukuJsqZY4NwPpvyAefoRl BWHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714000558; x=1714605358; 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=fYBh+n9IwUhNo/7khRr/3J4ptTWU7xn/Zet84InQcgE=; b=eEmsHRozE77mMZDV/zWFdXD3OESByOtkqCU1fPoHnzOROd5goAnqroVcIlWUEvXMiz gNwdLWUFBa0Zc/AVRAgKr6hYbSzbefsON+iAIE6hbOluShrhDsg7yLNQfX2zSB5PJuWf RF5dGeNCEEcvovXejzcOZmjF3UqOjd1MBGavhK5ptPEKBz1xA32btmJHn74ZzIsQy+Jd jd5zEXxOHZwDc7FtLG/UICIKLHUKYN5W/f2S3UvjqiJg5VIU97liJ8qtaDGLq65Rawcu WgTU8ZuYlSBKjh8F7txiNlbWXgt5iCnPNNwJw5+vm52PUhZ3P8+Pr+bKUEgdSCQ0Nw0S bc1w== X-Gm-Message-State: AOJu0YxvoZ+ecaxQWtTqW+ncIZTazqK4z3tB3ISPBZzGi09jzgUqejPC cWDg8s7CJwAIIZjqQIdHvTMCNuM0nuO6cO4arWWHHaF/cGyAQ7MjamBm14Wh/RVCQsK4QuF1kSg k X-Received: by 2002:a17:902:7488:b0:1e2:a91c:9836 with SMTP id h8-20020a170902748800b001e2a91c9836mr3893078pll.33.1714000558122; Wed, 24 Apr 2024 16:15:58 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:b65d:315b:9fcb:d747]) by smtp.gmail.com with ESMTPSA id ba5-20020a170902720500b001e2b4f513e1sm12433555plb.106.2024.04.24.16.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:15:57 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v3 1/3] gdb/nat: Use procfs(5) indexes in linux_common_core_of_thread Date: Wed, 24 Apr 2024 20:15:49 -0300 Message-ID: <20240424231551.1577518-2-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240424231551.1577518-1-thiago.bauermann@linaro.org> References: <20240424231551.1577518-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 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) Changes in v3: - Use macro names in comment, and clarify why we use those fields (suggested by Pedro). 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..4812bc735e86 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,9 @@ 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) + /* The first field after program name is LINUX_PROC_STAT_STATE, and we are + interested in field LINUX_PROC_STAT_PROCESSOR. */ + for (int i = LINUX_PROC_STAT_STATE; i <= LINUX_PROC_STAT_PROCESSOR; ++i) { /* Find separator. */ pos = content->find_first_of (' ', pos); From patchwork Wed Apr 24 23:15: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: 791575 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:346:15ad:a2a with SMTP id g4csp1100901wrq; Wed, 24 Apr 2024 16:16:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUEu/pBVuHocdM1RdDQuDoyyrF5+v4U+Ym5qNF4ClULJfrW1uAmy5kdFWf8gOxmXYijUeR5S7Hc5G+kQHIKlmZ9 X-Google-Smtp-Source: AGHT+IHACoNywHKx510fxaKIa4dDwDA5hJgFeNWumjj1PWfujz+PR+xXIakQKaiJ/DEjZS5tSEp0 X-Received: by 2002:ac8:7d12:0:b0:435:c390:6d4b with SMTP id g18-20020ac87d12000000b00435c3906d4bmr4610469qtb.64.1714000591885; Wed, 24 Apr 2024 16:16:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714000591; cv=pass; d=google.com; s=arc-20160816; b=TFFKAjbLKpPBl7RHUxzj18CZ91XkEWf3ntVL0a1rUr7QvFu2tHj6VzBH8663MQS8Ps jmiWoia4Dj5ILbo4LJ/yQE5FZwJY29VK54acQbec5Rbtx7DGtP5pCINTwDS9atvJlSW5 wNJxWNAr/lNnnwh3FB4Gwl4RRp5cU/AqLsAwUs1E3bTZTSoZ/b2fbSn5RE95oo4QpNnb 6MeVB6qFiExQhObqy2KFSJUyhgmsF8rRNyAcFlJpcfqPwdOvm5NXaqCKtIYISHFXcFlL tK9PdpF8xSdloDZWK7hrZDWrn2Pwbk6PdJua6DK2fA1ANlUvMXRQ0KIDD+p7Ma4sdNiI 9KFg== 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=iyQzvNa/vGaO35v2yRTaCVofAf3pR6w4MAitotHzsOw=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=PFcjtTz4o874fUAXdvRdXQu+ZlQrkhWIq19PsfShOLV74I0wAPSoRUw+nwTgln/vOT eLmY6kXPvK0GEXrAeVyM0H6Pk2/5kahj63H2yVbWvByu+3N1HWy3jXKUMIMJ5NSS4nPW u7zV4umbEjxOT43LSGUKNyC5XYsz0fm5FO4rc3lYPY6cqGnyFQ29lgTmyxYg91P9Fv58 408dyO7/5qLi6jKQO/WJspaJz9LulJlwhQqHz6dgiDNptF3OO6EGhA50qczmyI7MccpD UmbkxpHMWYNsTFpyLN1TAKPkrWWU2yzeemfT0JloTOEcmWnHuPZhrYHYjcOHa0OV8lXw 0X4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AD4eGgQ+; 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 m12-20020a05622a054c00b00437b491bd8dsi15575143qtx.61.2024.04.24.16.16.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:16:31 -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=AD4eGgQ+; 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 7EB143846079 for ; Wed, 24 Apr 2024 23:16:31 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 0B1943846420 for ; Wed, 24 Apr 2024 23:16:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B1943846420 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 0B1943846420 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::433 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000565; cv=none; b=XD5PkuCMWOPYAnw2lntyjDMdQcsP7t6llktWEddf4ArvNZhP9Q6nEtudIpn0VVTFaEf+xcG6Ez6ZuC3/zvz/7+Qt7E8sI5Mts8lAXTOhx0JB2ItUB72vkLjRVavTm3nHsxa4tSmBcsiIPocfBCL91+AzYTtG7oYRBQnn1FEyj7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000565; c=relaxed/simple; bh=x7CyEDOCd32hepKt+J7iAkmKZwz6/DgU01oLs67HLuc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Lt/k7o+CadTtV9lvI2YHXbHUqkWDjq+7fBrVpMNwxvIcdgaerUQGA8RqhnLuaUQSPrgXfuXsolO/nzbmILcyYcO4dKAJLsKAHXwVTuZY0hqnO7PA7dSNXCq6wZTSN0fdT4bSfW+z/ZyE958QMzKFeZ19Kp36KYZhHqPU74GDG70= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6f043f9e6d7so462299b3a.3 for ; Wed, 24 Apr 2024 16:16:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714000562; x=1714605362; 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=iyQzvNa/vGaO35v2yRTaCVofAf3pR6w4MAitotHzsOw=; b=AD4eGgQ+LMbciEzujX+jF2IldvD7IkmucI/eg10JOMJJmwV2dwBHcJK72pQ6EQyss2 eOlicoYzl8ST0aAe74735A8JzAam2tnQHYnB6q/eHt7oqjNSZ0WGG6S4zLLIXtsmfSah n+f7zwMcCnqStUuo8iAOXoHfOkd1msQJeXm8/MUG4x8jKSIxgOI8jCZdbWm3Qd1Wd0s7 I7ZMUa+6mACJDia3niF9cnzxAM4fjLoedTxXCs7JfM3NT5JBkoYixn3Ov7vZ5ddEUm3q pK3syEqVtXm8E8UIAgPKHwbjLYe0KfuR+6cPws8oZDljUr+6PJr8UwZm6YUs+JouQDE4 nwGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714000562; x=1714605362; 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=iyQzvNa/vGaO35v2yRTaCVofAf3pR6w4MAitotHzsOw=; b=BKvT9pvZ7kHDYapOwlvXJMXWY97taV+4tKn1mxIpjYz2BIKGPtH7QLfu5+VfuIP/Vq DdTN9uMT0M/DTPnunk37qMclOAR8WPMm3RDZ4LkNthKYW7k65PqxGUWw6otex5d9ncgS c/4K7Ym3Xd1MGSJqwiEKsMgFTIGY9dVQECUx+CS7120FZxp7Tr6MDBe8nV3/3kB2eD9Y w+jXlNAXP5maAMvJA7DmrEjZ6yHlcp4eP/ajLYUPqq5CcpdwD4tph8W7gUMXWmy9RNot dXiNvHwAq0C/A9VAu5Bl/xKfPX0FVKraLUHkkiVOkrN80oGjyRewkIB8KpgWOnpg2O/q GLrA== X-Gm-Message-State: AOJu0Ywg9Pq24mhTN7r7F6ovpFS/FuAsLXfe2g2LkvyWkTyT+9NGhhfT vSVie0m3ssZWu8AE2DdRLxfa5FKCsmE/rWrvhRrc4OMRPsgVQGAqgsSXzIvnV1UtOk4GeERRtaB 6 X-Received: by 2002:a05:6a00:891:b0:6e6:8df5:f77a with SMTP id q17-20020a056a00089100b006e68df5f77amr4931652pfj.31.1714000561903; Wed, 24 Apr 2024 16:16:01 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:b65d:315b:9fcb:d747]) by smtp.gmail.com with ESMTPSA id fd16-20020a056a002e9000b006e5808b472esm12332271pfb.95.2024.04.24.16.16.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:16:01 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v3 2/3] gdb/nat: Factor linux_proc_get_stat_field out of linux_common_core_of_thread Date: Wed, 24 Apr 2024 20:15:50 -0300 Message-ID: <20240424231551.1577518-3-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240424231551.1577518-1-thiago.bauermann@linaro.org> References: <20240424231551.1577518-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 | 41 +++++---------------------------------- gdb/nat/linux-osdata.h | 3 +++ gdb/nat/linux-procfs.c | 44 ++++++++++++++++++++++++++++++++++++++++++ gdb/nat/linux-procfs.h | 10 ++++++++++ 4 files changed, 62 insertions(+), 36 deletions(-) Changes in v3: - Preserve comment mentioning why we use that field in the for loop in linux_proc_get_stat_field. 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 4812bc735e86..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,50 +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; - - /* The first field after program name is LINUX_PROC_STAT_STATE, and we are - interested in field LINUX_PROC_STAT_PROCESSOR. */ - 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..c11eaf3cc6fd 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -230,6 +230,50 @@ 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 {}; + + /* The first field after program name is LINUX_PROC_STAT_STATE. */ + 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 Wed Apr 24 23:15: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: 791577 Delivered-To: patch@linaro.org Received: by 2002:a5d:4884:0:b0:346:15ad:a2a with SMTP id g4csp1100980wrq; Wed, 24 Apr 2024 16:16:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXUrhz+O6OetCbsLeUFa0yV0AKumF4OolGTUX9r4aGyyccNk5qFnPOLYsiYhMk8Gih1l0Jz1mEjogWpncVR6eDm X-Google-Smtp-Source: AGHT+IEYx2dXhf+z8FWbSeKs//RlHM6rprlmXzW8EI1FxcNd160fgOeT4Ii059l5qZtZ5tB7DfP2 X-Received: by 2002:a05:620a:84e:b0:78e:eb60:182e with SMTP id u14-20020a05620a084e00b0078eeb60182emr4350401qku.10.1714000609739; Wed, 24 Apr 2024 16:16:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714000609; cv=pass; d=google.com; s=arc-20160816; b=o7hwCj7yUUGxjTc9qGftAtXBopAjaSHeKYokehwEb7Rz7vM+uOmwHKp2PJeNRTQmBq Ec1q8RRdAleKvbmoUPBi0SIxWuCTQ/5POZyghZuN9p/5AKqHeMA+YHIyxYX2OsJGsHDO yvukJzLDIo1bZWhKa3KGUXcUziy/zvULl90gNUm1hu0HPNqBuqDUkVSJqCqoUm101obI OnqHVL89D3OslnofVT8kqPVTEyyI4WgYrynBL4OkieOPpPGfvqjtjxyYEQxHGVa3ZQrk Lo3UbgzZpUw89ib2osNet1/o56SmU9poM01XqPX0qHhu4/4jFa8TK1/HQnke9hfsyW1f LNWA== 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=i5pt7XC+TzhbjOWWMlUUUNEFVK61cUXDAXivRzBEg0M=; fh=hiz3ehk7igAf1jDTyW+iD8dgiuxOgEsxZsLHCOrAJYo=; b=TE0fzSS/R91QiRsD85LFCqBb9o9jeZtfMagJztEYH0WQhcP/fzTQItiE0lPz0e+7Uj v5L30qW6k44t7aI3LfKTc7ST1zGPIeeLpxTgED14aFbLVIFrX0md/dFNtxA5ASiKbbNV bla/T38T0GoPxDGfoWSc8sD6NKI+DMqjO0zXA2zumEOjXvlC1EVQtniOKDej1xZbZFUO IYXH3WSD9uTFIQ5JaEEnoUYzjQWytNM/W05v4k+gfTagTvMJfXFcCvJpEsG0adh8irC5 doNn7tGWufkK2qp3+H0mLmJR4s1H6PQUBUikNKBgN0aIHDbYBN2RSpx0HVKunQ9Yi7IV kjpA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U01w523C; 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 q24-20020a05620a0c9800b007906e1cba30si9340927qki.415.2024.04.24.16.16.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:16:49 -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=U01w523C; 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 5C70738460A3 for ; Wed, 24 Apr 2024 23:16:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 9583F384640E for ; Wed, 24 Apr 2024 23:16:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9583F384640E 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 9583F384640E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000572; cv=none; b=IbJzgghi7qIwrqf8nMHVmUzPXU3MwvZ/+is/Ziyjt6wJVDMh6SrB5T7x0gUlE3VwDkw4H9tipxgnElr9BbGfo4SZxdDi73zJNXMzVWUNs8qIOy9ZN4QqGwOf/vWq0YGdd98vpGmqWJ89D4UqkcjrrbroDPHVNQCW7Vgay4ZPZGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714000572; c=relaxed/simple; bh=uSaE2rirjP12c+qOjNIFTrVxAa5XRQPleP/K3lKZSVI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AD+NmfzZ6aBLXsfo3uGgHn0KJEA65TmtPTtnadp+f/NY0AFlUFpGBMwGKy/CkyewiD6JCcvMwX8tEfBCXM6ZPWC/5+Q8ysjjmoXfRKEsLYrx8lEiTotH1yam8RGf2gtMjr/GHNspVLQpbtTxDzbU9W8pj+AhRbugaxB+6jMOO7s= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6ed20fb620fso408800b3a.2 for ; Wed, 24 Apr 2024 16:16:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714000565; x=1714605365; 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=i5pt7XC+TzhbjOWWMlUUUNEFVK61cUXDAXivRzBEg0M=; b=U01w523Ct1kLKiEu8EQMriWKMklU3inQNvw9g0s0oenrvqzebhpsQCLKQxXEplUHqU 0pB6G/QBm1UQEbJy+UFFzpFGKRDoZ/RSceWYuEDLb4hq0yXgLaAJlkdaL56FmYEpWDzl QyynH71pI16Fd/qKj6WX9mefu0Zi3COIyo1jvPns5A0/m1+b+efwFSlUQHN0qcS6jWsL a0+YNepZkexsSCHC+KJR9kb/mlWxEct2s3/v3/psGQs89cKuPanhJStlJT9cEYXfCaj5 LDsieqnyvhjAra0rMF05F/xyedDJXyZ7Uor+PF41ckrDXIup8xr4VKrKVkNT0ZFCM19H h96w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714000565; x=1714605365; 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=i5pt7XC+TzhbjOWWMlUUUNEFVK61cUXDAXivRzBEg0M=; b=UABFhq85Jjs28IJDqmuWvUVgzKHAEiBomhzMvU2YPAiM8wQf0D338Ml26JJTsEjJNU caYly5SF0h3EI+VuOw5tokYS28NnOPlr2rOCbsu9uEGJlq3AHMdt1Jo1//Aw9Cwxf0EL 3+OPjbfIHTcbemquBNz8bVnc0C/5f+/4MUHqfZtHAWu4R20ADkZf/3QETtsok3NsOV6x znP51oPt6MMSChOGHuZBhygyUbbp47pBLemEWGcWcCcjaIpJsZ6Lwe2dFcrgB9acWb97 0Tb9MEMgUTXkxNg3laFaPXRlBwRCGGXKPt8ndX0N+4nuxkHyf+UM9qUKu15ucFBH0O9g TmcQ== X-Gm-Message-State: AOJu0YwjajJXrLVFoCOU2JnHqItGz4LqEUneZI7rtcZppDHK1ejYhEWd CwQrB3HjfzRzV/nT4FMKAMq0fB8r1sV4ClDF+yjSk8s8AN7mNlSeXUyA99LPu5rGxcE7KZ+Sswh t X-Received: by 2002:a05:6a00:189b:b0:6ea:d740:62a4 with SMTP id x27-20020a056a00189b00b006ead74062a4mr5226385pfh.25.1714000565396; Wed, 24 Apr 2024 16:16:05 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:b65d:315b:9fcb:d747]) by smtp.gmail.com with ESMTPSA id gv2-20020a056a004e8200b006ea6f189976sm11967077pfb.163.2024.04.24.16.16.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 16:16:05 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Christophe Lyon , Luis Machado , Pedro Alves Subject: [PATCH v3 3/3] gdb/nat/linux: Fix attaching to process when it has zombie threads Date: Wed, 24 Apr 2024 20:15:51 -0300 Message-ID: <20240424231551.1577518-4-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240424231551.1577518-1-thiago.bauermann@linaro.org> References: <20240424231551.1577518-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 Approved-By: Pedro Alves --- gdb/nat/linux-procfs.c | 53 ++++++++++++++++++++++++++++++++++++++++++ gdb/nat/linux-procfs.h | 1 + 2 files changed, 54 insertions(+) No change in v3. 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 c11eaf3cc6fd..ba6263d79e5f 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. */ @@ -272,6 +274,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 * @@ -333,6 +358,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 @@ -351,6 +391,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