public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
To: gdb-patches@sourceware.org
Cc: Christophe Lyon <christophe.lyon@linaro.org>,
	Luis Machado <luis.machado@arm.com>,
	Pedro Alves <pedro@palves.net>
Subject: [PATCH v3 1/3] gdb/nat: Use procfs(5) indexes in linux_common_core_of_thread
Date: Wed, 24 Apr 2024 20:15:49 -0300	[thread overview]
Message-ID: <20240424231551.1577518-2-thiago.bauermann@linaro.org> (raw)
In-Reply-To: <20240424231551.1577518-1-thiago.bauermann@linaro.org>

The code and comment reference stat fields by made-up indexes.  The
procfs(5) man page, which describes the /proc/PID/stat file, has a numbered
list of these fields so it's more convenient to use those numbers instead.

This is currently an implementation detail inside the function so it's
not really relevant with the code as-is, but a future patch will do some
refactoring which will make the index more prominent.

Therefore, make this change in a separate patch so that it's simpler to
review.

Reviewed-By: Luis Machado <luis.machado@arm.com>
---
 gdb/nat/linux-osdata.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Changes in v3:
- Use macro names in comment, and clarify why we use those fields
  (suggested by Pedro).

Changes in v2:
- Added macros for field indexes in /proc/PID/stat (Suggested by Luis).

diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 6ffabe90aa7d..4812bc735e86 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -52,6 +52,10 @@ 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.  */
@@ -74,10 +78,9 @@ linux_common_core_of_thread (ptid_t ptid)
   if (pos == std::string::npos)
     return -1;
 
-  /* If the first field after program name has index 0, then core number is
-     the field with index 36 (so, the 37th).  There's no constant for that
-     anywhere.  */
-  for (int i = 0; i < 37; ++i)
+  /* 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);

  reply	other threads:[~2024-04-24 23:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-24 23:15 [PATCH v3 0/3] Fix attaching to process when it has zombie threads Thiago Jung Bauermann
2024-04-24 23:15 ` Thiago Jung Bauermann [this message]
2024-04-24 23:15 ` [PATCH v3 2/3] gdb/nat: Factor linux_proc_get_stat_field out of linux_common_core_of_thread Thiago Jung Bauermann
2024-04-25  8:59   ` Luis Machado
2024-04-25  9:04     ` Luis Machado
2024-04-25 12:48       ` Thiago Jung Bauermann
2024-04-24 23:15 ` [PATCH v3 3/3] gdb/nat/linux: Fix attaching to process when it has zombie threads Thiago Jung Bauermann
2024-04-26 16:23 ` [PATCH v3 0/3] " Pedro Alves
2024-04-30  2:39   ` Thiago Jung Bauermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240424231551.1577518-2-thiago.bauermann@linaro.org \
    --to=thiago.bauermann@linaro.org \
    --cc=christophe.lyon@linaro.org \
    --cc=gdb-patches@sourceware.org \
    --cc=luis.machado@arm.com \
    --cc=pedro@palves.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).