From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Luis Machado <luis.machado@amd.com>,
Luis Machado <luis.machado.foss@gmail.com>,
Thiago Jung Bauermann <thiago.bauermann@linaro.org>,
Simon Marchi <simark@simark.ca>,
"Kevin Buettner" <kevinb@redhat.com>,
Christina Schimpe <christina.schimpe@intel.com>,
Christina Joos <christina.joos@intel.com>,
Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v1 4/6] gdb/linux-tdep: migrate linux_info_proc to file_reader_t
Date: Tue, 28 Jul 2026 16:16:58 +0100 [thread overview]
Message-ID: <20260728151700.253720-5-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260728151700.253720-1-matthieu.longo@arm.com>
The patch migrates the code of linux_info_proc to use file_reader_t to
read the procfs files.
The availability of array_views allows to also simplify the logic in
several places, where null-terminating characters are replaced by spaces,
or where the file content is iterated line by line.
In the last case, a new helper function, extract_string_view_from_buffer,
encapsulates the logic for such iterations where string are separated by
tokens.
---
gdb/linux-tdep.c | 135 +++++++++++++++++++++++++++++------------------
1 file changed, 84 insertions(+), 51 deletions(-)
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 11f0a6952ea..a8dab0d6d5e 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -874,6 +874,39 @@ dump_note_entry_p (filter_flags filterflags, const smaps_data &map)
return true;
}
+/* Extract a string view from BUFFER starting at START and ending at the
+ first occurrence of SEPARATOR.
+ Return the extracted view together with an iterator to the beginning of
+ the next entry, skipping any successive separators. If no separator
+ is found, return the remainder of BUFFER starting at START. If there is
+ no following entry, the returned iterator is BUFFER.end (). */
+
+static std::pair<gdb::array_view<char>, gdb::array_view<char>::iterator>
+extract_string_view_from_buffer (gdb::array_view<char> &buffer,
+ gdb::array_view<char>::iterator start,
+ char separator = '\0')
+{
+ auto next_start = buffer.end ();
+
+ /* Reject a START iterator that does not point into BUFFER. */
+ if (start < buffer.begin () || start >= buffer.end ())
+ return {gdb::array_view<char> (), next_start};
+
+ auto it = std::find (start, buffer.end (), separator);
+
+ /* If no separator is found, the remainder of BUFFER is the final string. */
+ if (it != buffer.end ())
+ {
+ /* Otherwise, skip successive separators so that NEXT_START points to
+ the beginning of the next string, if any. */
+ for (next_start = std::next (it);
+ next_start != buffer.end () && *next_start == separator;
+ next_start = std::next (next_start));
+ }
+
+ return {gdb::array_view<char> (start, it), next_start};
+}
+
/* Implement the "info proc" command. */
static void
@@ -915,25 +948,23 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
if (cmdline_f)
{
- xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", ptid.lwp ());
- gdb_byte *buffer;
- LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer);
-
- if (len > 0)
+ file_reader_t<gdb_byte> cmdline_freader
+ (string_printf ("/proc/%ld/cmdline", ptid.lwp ()));
+ if (cmdline_freader)
{
- gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
- ssize_t pos;
-
- for (pos = 0; pos < len - 1; pos++)
- {
- if (buffer[pos] == '\0')
- buffer[pos] = ' ';
- }
- buffer[len - 1] = '\0';
- gdb_printf ("cmdline = '%s'\n", buffer);
+ /* /proc/<pid>/cmdline stores the command-line arguments as a
+ sequence of NUL-separated strings. */
+ gdb::array_view<char> cmdline = cmdline_freader.cast_view<char> ();
+ gdb_assert (cmdline[cmdline.size () - 1] == '\0');
+ /* Replace null characters splitting the arguments in the command
+ line by spaces, except for the last one. */
+ gdb::ranges::replace
+ (cmdline.slice (0, cmdline.size () - 1), '\0', ' ');
+ gdb_printf ("cmdline = '%s'\n", cmdline.data ());
}
else
- warning (_("unable to open /proc file '%s'"), filename);
+ warning (_("unable to open /proc file '%s'"),
+ cmdline_freader.c_filepath ());
}
if (cwd_f)
{
@@ -947,27 +978,25 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
}
if (environ_f)
{
- xsnprintf (filename, sizeof filename, "/proc/%ld/environ", ptid.lwp ());
- gdb_byte *buffer;
- LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer);
-
- if (len > 0)
+ file_reader_t<gdb_byte> environ_freader
+ (string_printf ("/proc/%ld/environ", ptid.lwp ()));
+ if (environ_freader)
{
- gdb::unique_xmalloc_ptr<char> dealloc ((char *) buffer);
gdb_printf (_("Environment variables:\n\n"));
-
+ gdb::array_view<char> buffer = environ_freader.cast_view<char> ();
/* Entries are separated by the null character.
Print each environment variable, line by line. */
- gdb_byte *buffer_end = buffer + len;
- while (buffer < buffer_end)
+ for (auto it = buffer.begin (); it != buffer.end ();)
{
- gdb_printf (" %s\n", buffer);
- /* +1 for the null character. */
- buffer += strlen ((char *) buffer) + 1;
+ auto [ntbs, next_start]
+ = extract_string_view_from_buffer (buffer, it, '\0');
+ gdb_printf (" %s\n", ntbs.data ());
+ it = next_start;
}
}
else
- warning (_("unable to open /proc file '%s'"), filename);
+ warning (_("unable to open /proc file '%s'"),
+ environ_freader.c_filepath ());
}
if (exe_f)
{
@@ -981,10 +1010,9 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
}
if (mappings_f)
{
- xsnprintf (filename, sizeof filename, "/proc/%ld/maps", ptid.lwp ());
- gdb::unique_xmalloc_ptr<char> map
- = target_fileio_read_stralloc (NULL, filename);
- if (map != NULL)
+ file_reader_t<char> map_freader
+ (string_printf ("/proc/%ld/maps", ptid.lwp ()));
+ if (map_freader)
{
gdb_printf (_("Mapped address spaces:\n\n"));
ui_out_emit_table emitter (current_uiout, 6, -1, "ProcMappings");
@@ -998,12 +1026,16 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
current_uiout->table_header (0, ui_left, "objfile", "File");
current_uiout->table_body ();
- char *saveptr;
- for (const char *line = strtok_r (map.get (), "\n", &saveptr);
- line != nullptr;
- line = strtok_r (nullptr, "\n", &saveptr))
+ auto content = map_freader.view ();
+ for (auto it = content.begin (); it != content.end ();)
{
- struct mapping m = read_mapping (line);
+ auto [line, next_line_begin]
+ = extract_string_view_from_buffer (content, it, '\n');
+ it = next_line_begin;
+
+ /* read_mapping() expects a null-terminated string. */
+ *std::prev (it) = '\0';
+ struct mapping m = read_mapping (line.data ());
ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
current_uiout->field_core_addr ("start", gdbarch, m.addr);
@@ -1022,26 +1054,26 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
}
}
else
- warning (_("unable to open /proc file '%s'"), filename);
+ warning (_("unable to open /proc file '%s'"),
+ map_freader.c_filepath ());
}
if (status_f)
{
- xsnprintf (filename, sizeof filename, "/proc/%ld/status", ptid.lwp ());
- gdb::unique_xmalloc_ptr<char> status
- = target_fileio_read_stralloc (NULL, filename);
- if (status)
- gdb_puts (status.get ());
+ file_reader_t<char> status_freader
+ (string_printf ("/proc/%ld/status", ptid.lwp ()));
+ if (status_freader)
+ gdb_puts (status_freader.data ());
else
- warning (_("unable to open /proc file '%s'"), filename);
+ warning (_("unable to open /proc file '%s'"),
+ status_freader.c_filepath ());
}
if (stat_f)
{
- xsnprintf (filename, sizeof filename, "/proc/%ld/stat", ptid.lwp ());
- gdb::unique_xmalloc_ptr<char> statstr
- = target_fileio_read_stralloc (NULL, filename);
- if (statstr)
+ file_reader_t<char> stat_freader
+ (string_printf ("/proc/%ld/stat", ptid.lwp ()));
+ if (stat_freader)
{
- const char *p = statstr.get ();
+ const char *p = stat_freader.data ();
gdb_printf (_("Process: %s\n"),
pulongest (strtoulst (p, &p, 10)));
@@ -1168,7 +1200,8 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
#endif
}
else
- warning (_("unable to open /proc file '%s'"), filename);
+ warning (_("unable to open /proc file '%s'"),
+ stat_freader.c_filepath ());
}
}
--
2.55.0
next prev parent reply other threads:[~2026-07-28 15:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 15:16 [PATCH v1 0/6] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-07-28 15:16 ` [PATCH v1 1/6] target_fileio_read_stralloc: add an optional length parameter Matthieu Longo
2026-07-28 15:16 ` [PATCH v1 2/6] gdb support: add gdb::ranges::replace algorithm Matthieu Longo
2026-07-28 15:16 ` [PATCH v1 3/6] gdb: introduce helper class file_reader_t Matthieu Longo
2026-07-28 15:16 ` Matthieu Longo [this message]
2026-07-28 15:16 ` [PATCH v1 5/6] gdb/linux-tdep: migrate linux_find_memory_regions_full to file_reader_t Matthieu Longo
2026-07-28 15:17 ` [PATCH v1 6/6] gdb/linux-tdep: remove legacy parse_smaps_data overload Matthieu Longo
2026-07-28 15:43 ` [PATCH v1 0/6] gdb: introduce file_reader_t to read procfs files Joos, Christina
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=20260728151700.253720-5-matthieu.longo@arm.com \
--to=matthieu.longo@arm.com \
--cc=christina.joos@intel.com \
--cc=christina.schimpe@intel.com \
--cc=gdb-patches@sourceware.org \
--cc=kevinb@redhat.com \
--cc=luis.machado.foss@gmail.com \
--cc=luis.machado@amd.com \
--cc=simark@simark.ca \
--cc=thiago.bauermann@linaro.org \
/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