From patchwork Thu Mar 21 23:11:48 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: 781596 Delivered-To: patch@linaro.org Received: by 2002:adf:cf01:0:b0:33e:7753:30bd with SMTP id o1csp1077849wrj; Thu, 21 Mar 2024 16:12:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURKmDo2+ewbLr+5tRnoeTF0J3gbbqZ+Lw/BrBfd9pnFa44GEf/Tc+ZaKoYMgCwJ5i6X3lyquAACFWSBssQV80z X-Google-Smtp-Source: AGHT+IFO4F4mqktj+ujzcBoqe2Y/FR7i3koyS9wHCxVpOmuSKv2Wv4aKukNC4eFnN/BgpFLm1rro X-Received: by 2002:ac8:59d4:0:b0:42e:cc3d:a257 with SMTP id f20-20020ac859d4000000b0042ecc3da257mr676747qtf.53.1711062764132; Thu, 21 Mar 2024 16:12:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711062764; cv=pass; d=google.com; s=arc-20160816; b=m5VqvXEdx7UK+B8uT8VNWyFLYy/0zl8ATckvljo5+oYvVWx3rrmFEw5L4ovUE/fMg+ 5a4ypD7exrW/MmRFrixumXvr8Ku3Wnvj3VkYtRHDRJn2RVn0L4J3AYO+5ODojGqQxIWh BZHzf7ZM0zc6mRCQS9iL8faJMX4h5lhDRUK8e+62abxh93rtq3Bba6viApZEb4AjNnrs DHM1PrBwsSVAozdBY3Ss8Us1BdAUmXN73dRhJulZaS43jAxOZqL7Pzr/rgUOt0QKQaTC ip1UTcKICBr+wG7DYWH/wrdTNZTP94ZUquG/KSdbL+nD0a76d5XUneBNCRM0W22VUboa wvbg== 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:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=7aFAglohWo67xvqz/cY8xqU1zU3/C+glppEcfODGlXc=; fh=72kqq0iQhigvR9Vv/oqX5ebs3Yyyw7XhzWxOOEPdupI=; b=ufEJSW6tSK+YEGz7jzEYCR25O56qfDAZJ744cjZV2xWWp1zMdnRSfrUzHZuEEtez6X CZQWCEDCVELIWjsTCFnPVGeEgNFCXI+1e8pX0ahw55rMpN4jHKaUA4kP55+v+RrR+e24 rY5Gd20sRtUZGrKsx0r3CTQck3MT/PwycMtMxG+OefhKnkt9oUbXhgtGGO5Fns1HKesZ AfQu/ietURVoLtCCosydqf/o/WzH99L1TH5m0Z9c++nwEW0r/mcpLYOKhld+I440szZe wBmXtr9cHtJHopc8AJx5FgJvqPFQ6zc5ZRfWmCvKzWYtWsRbSXvwGr4/+CY4eVV3qI56 Yd7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Jhjk6SaN; 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 d15-20020a05622a100f00b0042f4b96c4f9si811733qte.302.2024.03.21.16.12.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 16:12:44 -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=Jhjk6SaN; 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 A5CF13858C24 for ; Thu, 21 Mar 2024 23:12:43 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by sourceware.org (Postfix) with ESMTPS id B43EC3858C5E for ; Thu, 21 Mar 2024 23:11:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B43EC3858C5E 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 B43EC3858C5E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711062722; cv=none; b=fyIJfqRcN6D/7Z6CjRX/a9V8LOexjQdAKW1L51XFZ0TMCJm42zW8ikdUactRqdaUdYat/+f1Oo2D5ASOFFQ/M9SStK3Bes8ZAo2dghlDi3xj8nWL3cPZzSnzHqz9HI0iMDNOplkA8Alx+Cl3hSTvdP1YGX6x3CQNSXItHih24PU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711062722; c=relaxed/simple; bh=ygPiGSWnZRknJbrN0UT4y8y1c4WDxffc49lMAMaSVlQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fej2Oxi/Y8QTXc2tbmVaMF0djWZslo6ovY6sYNSZNsQ6KTLVegEYFzd6BZpv6tEkGjQpEv9tKjWRTDN4DsAnee/gtC3OqfrEuiLkiF3IJKDvFl7OjlflT6AtNX+sEIQD9ACAvojBd/JlZnS/7lpyj66ZKyjGrxdHoV3txDLf3U8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-5a480985625so853885eaf.0 for ; Thu, 21 Mar 2024 16:11:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1711062719; x=1711667519; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=7aFAglohWo67xvqz/cY8xqU1zU3/C+glppEcfODGlXc=; b=Jhjk6SaNEnRqiKfPNsUkHjeCJ5pJsFdtDxaBjpcqhI64e9BOIDyrCp0SRvKrSLhVQx O3YE4/qsDJfgwSZP+9dvJCYtN55u8I91vzsz0WsYd2I/5JIk7gXZB8EsiGeHrGgr65GH X+DfpHGXoSOYGVyjEnp211/IJ5wBABrePt+P+bSQ9TA1C7g6yD+fSmz/oZsk80dQjTnK WjhoZ3EUyNCiP+OuRBkb7QE2tbMhSrM84jze9Ru7QUh8C6+cGP5QKYf67pBNlCTd5pFv sq0j0+/Qj1DYlI/DDuJFmHtRv/hcmiuwvtykjnhzEnsaZBHKm7trbAe07S7x9ZAf/lN6 9vAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711062719; x=1711667519; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7aFAglohWo67xvqz/cY8xqU1zU3/C+glppEcfODGlXc=; b=DJFFkKnTeW86cLDx3GZ4bxoXkYJIVNhhXJtFLSsMbme6ZbfaG8E8arbl+UNd7IEZpm OtG6E707BPiKdyH0f0xG68rNbS/jmP55h1z3+lXuuirXANibNUUAe50W8k4uT0PDgGT5 oIbCpfNr8QLBgM2Bnx0VkAgsRWWiDx+DOyYlyyJYlESnMTs2/5T3EVeowhgFO2t6KlPR isJzMq+NZrLdZSKJVzl3wRV/uAbSFZmpY3yngjeNJMyHEAC+ciJg3jvaMQUSwViOh+rh 0phb2b1rG1FHKpfZGKBKSvSiIEr49CpnZbqL8EPPwntXvRNJofdaYoDZqwWe8yJHvgha ONPg== X-Gm-Message-State: AOJu0YwHzWBcC/NGjx4AL1dbI0mA6vhlz74DngUMpOkbV+bguXp4WrYB MPVTXTshc6fpOmHFW5up6aQtZzaEVC3pDxilINUuZHowkYEjbzXqvMjCeKzq3lff0q9Dh2in5TN 7 X-Received: by 2002:a05:6870:8086:b0:222:7404:f3e4 with SMTP id q6-20020a056870808600b002227404f3e4mr776304oab.29.1711062718896; Thu, 21 Mar 2024 16:11:58 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8470:e28:f7d5:8d63:c544]) by smtp.gmail.com with ESMTPSA id n4-20020a62e504000000b006e8f75d3b07sm367592pff.181.2024.03.21.16.11.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 16:11:58 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Subject: [RFC PATCH 2/3] gdb/nat: Factor linux_find_proc_stat_field out of linux_common_core_of_thread Date: Thu, 21 Mar 2024 20:11:48 -0300 Message-ID: <20240321231149.519549-3-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240321231149.519549-1-thiago.bauermann@linaro.org> References: <20240321231149.519549-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 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. 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 | 43 ++++++++++++++++++++++++++++-------------- gdb/nat/linux-osdata.h | 3 +++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 172fea5cea85..c254f2e4f05b 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -53,32 +53,31 @@ typedef long long TIME_T; #define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1) -/* Returns the CPU core that thread PTID is currently running on. */ +/* Returns FIELD (as numbered in procfs(5) man page) of + /proc/PID/task/LWP/stat file. */ -/* Compute and return the processor core of a given thread. */ - -int -linux_common_core_of_thread (ptid_t ptid) +static std::optional +linux_find_proc_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 >= 3); + char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN]; - 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 ()) - return -1; + 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 -1; + return {}; - /* 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 = 3; i <= 39; ++i) + for (int i = 3; i <= field; ++i) { /* Find separator. */ pos = content->find_first_of (' ', pos); @@ -91,8 +90,24 @@ linux_common_core_of_thread (ptid_t ptid) return {}; } - if (sscanf (&(*content)[pos], "%d", &core) == 0) - core = -1; + /* 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-osdata.h. */ + +int +linux_common_core_of_thread (ptid_t ptid) +{ + std::optional field = linux_find_proc_stat_field (ptid, 39); + int core; + + if (!field.has_value () || sscanf (field->c_str (), "%d", &core) == 0) + return -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);