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