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: 88972 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF54F384475B for ; Wed, 24 Apr 2024 23:16:37 +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-Google-Smtp-Source: AGHT+IG3GyAZa4uGJFVueES90Q8/0EPgrW91tu7wSmeUon8j+168/czKchpTHShbiDUKiqpR08ywLg== 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+patchwork=sourceware.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