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