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