From: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
To: Matthieu Longo <matthieu.longo@arm.com>
Cc: <gdb-patches@sourceware.org>,
Luis Machado <luis.machado@amd.com>,
Luis Machado <luis.machado.foss@gmail.com>,
Andrew Burgess <aburgess@redhat.com>,
"Yury Khrustalev" <yury.khrustalev@arm.com>,
Pedro Alves <pedro@palves.net>, "Tom Tromey" <tom@tromey.com>
Subject: Re: [PATCH v1 05/10] gdb: introduce helper class file_reader_t
Date: Thu, 09 Jul 2026 06:33:23 +0000 [thread overview]
Message-ID: <87bjcg1xl8.fsf@linaro.org> (raw)
In-Reply-To: <20260707154900.94542-6-matthieu.longo@arm.com> (Matthieu Longo's message of "Tue, 7 Jul 2026 16:48:55 +0100")
Matthieu Longo <matthieu.longo@arm.com> writes:
> Wrap all the boilerplate code required to read a file in a new helper
> class: file_reader_t. The class owns the file contents together with
> the file path, and provides convenient accessors for the data, size and
> typed views. It supports both null-terminated text files and binary files.
>
> This helper eliminates repeated calls to target_fileio_read_stralloc
> and target_fileio_read_alloc, remove explicit memory management with
> gdb::unique_xmalloc_ptr, and simplifies the casting logic when working
> with binary data.
>
> The patch converts some of the existing Linux, AMD64, and SPARC code that
> reads files from /proc to use file_reader_t.
> ---
> gdb/amd64-linux-tdep.c | 12 ++---
> gdb/linux-tdep.c | 102 +++++++++++++++++------------------------
> gdb/sparc64-tdep.c | 12 ++---
> gdb/target.h | 65 ++++++++++++++++++++++++++
> 4 files changed, 117 insertions(+), 74 deletions(-)
With the changes I suggested below:
Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
> @@ -2285,23 +2280,24 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
>
> /* Obtaining PID and filename. */
> pid = inferior_ptid.pid ();
> - xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
> - /* The full name of the program which generated the corefile. */
> - gdb_byte *buf = nullptr;
> - LONGEST buf_len = target_fileio_read_alloc (nullptr, filename, &buf);
> - gdb::unique_xmalloc_ptr<char> fname ((char *)buf);
> + file_reader_t<gdb_byte> cmdline_freader
> + (string_printf ("/proc/%d/cmdline", pid));
I was scratching my head for a bit trying to understand why not use
file_reader_t<char> here and avoid having to use cast_view<char>
below. I think it's worth a comment mentioning that it's because cmdline
has '\0' separating the arguments.
> + if (!cmdline_freader)
> + return false;
>
> - if (buf_len < 1 || fname.get () == nullptr || fname.get ()[0] == '\0')
> + /* The full name of the program which generated the corefile. */
> + gdb::array_view<char> cmdline = cmdline_freader.cast_view<char> ();
> + if (cmdline.size () < 1 || cmdline[0] == '\0')
> {
> /* No program name was read, so we won't be able to retrieve more
> information about the process. */
> return false;
> }
> - if (fname.get ()[buf_len - 1] != '\0')
> + if (cmdline[cmdline.size () - 1] != '\0')
> {
> warning (_("target file %s "
> "does not contain a trailing null character"),
> - filename);
> + cmdline_freader.c_filepath ());
> return false;
> }
>
⋮
> diff --git a/gdb/sparc64-tdep.c b/gdb/sparc64-tdep.c
> index 93db3417a2a..811b76f27d8 100644
> --- a/gdb/sparc64-tdep.c
> +++ b/gdb/sparc64-tdep.c
> @@ -305,14 +305,13 @@ adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt)
> size_t i = 0;
>
> pid_t pid = inferior_ptid.pid ();
> - snprintf (filename, sizeof filename, "/proc/%ld/adi/maps", (long) pid);
> - gdb::unique_xmalloc_ptr<char> data
> - = target_fileio_read_stralloc (NULL, filename);
This change leaves filename unused and I get a build error when using
"configure --enable-targets=all":
CXX sparc64-tdep.o
/home/bauermann/src/binutils-gdb-wt-2/gdb/sparc64-tdep.c: In function 'bool adi_is_addr_mapped(CORE_ADDR, size_t)':
/home/bauermann/src/binutils-gdb-wt-2/gdb/sparc64-tdep.c:304:8: error: unused variable 'filename' [-Werror=unused-variable]
304 | char filename[MAX_PROC_NAME_SIZE];
| ^~~~~~~~
cc1plus: all warnings being treated as errors
make[2]: *** [Makefile:2100: sparc64-tdep.o] Error 1
> - if (data)
> + file_reader_t<char> adi_maps_freader
> + (string_printf ("/proc/%d/adi/maps", pid));
I'll just note that this patch changes the printf pattern from "%ld" and
"(long) pid" to "%d" and "pid". I think the change is for the better and
I can't think of why it was done the other way before.
> + if (adi_maps_freader)
> {
> adi_stat_t adi_stat = get_adi_info (pid);
> char *saveptr;
> - for (char *line = strtok_r (data.get (), "\n", &saveptr);
> + for (char *line = strtok_r (adi_maps_freader.data (), "\n", &saveptr);
> line;
> line = strtok_r (NULL, "\n", &saveptr))
> {
--
Thiago
(he/him)
next prev parent reply other threads:[~2026-07-09 6:34 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-07 15:48 [PATCH v1 00/10] gdb: bugfix 31207 and various refactoring in linux-tdep Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 01/10] gdb/linux-tdep: change linux_fill_prpsinfo to return bool Matthieu Longo
2026-07-09 6:26 ` Thiago Jung Bauermann
2026-07-09 12:23 ` Simon Marchi
2026-07-27 14:38 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 02/10] gdb: rely on the first alive thread TPID when reading Linux procfs files Matthieu Longo
2026-07-09 6:29 ` Thiago Jung Bauermann
2026-07-13 9:11 ` Matthieu Longo
2026-07-09 14:24 ` Simon Marchi
2026-07-14 15:47 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 03/10] target_fileio_read_stralloc: add an optional length parameter Matthieu Longo
2026-07-09 6:30 ` Thiago Jung Bauermann
2026-07-13 15:26 ` Matthieu Longo
2026-07-24 2:50 ` Thiago Jung Bauermann
2026-07-27 14:52 ` Matthieu Longo
2026-07-29 1:57 ` Thiago Jung Bauermann
2026-07-21 21:28 ` Luis
2026-07-27 14:48 ` Matthieu Longo
2026-07-27 14:53 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 04/10] gdb support: add gdb::replace algorithm for iterators and ranges Matthieu Longo
2026-07-09 6:30 ` Thiago Jung Bauermann
2026-07-10 21:21 ` Kevin Buettner
2026-07-13 15:51 ` Matthieu Longo
2026-07-21 21:33 ` Luis
2026-07-27 14:58 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 05/10] gdb: introduce helper class file_reader_t Matthieu Longo
2026-07-09 6:33 ` Thiago Jung Bauermann [this message]
2026-07-13 17:17 ` Matthieu Longo
2026-07-10 21:43 ` Kevin Buettner
2026-07-13 16:31 ` Matthieu Longo
2026-07-13 15:50 ` Schimpe, Christina
2026-07-13 17:12 ` Matthieu Longo
2026-07-22 16:36 ` Joos, Christina
2026-07-27 15:13 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 06/10] gdb/linux-tdep: migrate linux_info_proc to file_reader_t Matthieu Longo
2026-07-09 6:35 ` Thiago Jung Bauermann
2026-07-13 17:20 ` Matthieu Longo
2026-07-21 21:43 ` Luis
2026-07-27 17:11 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 07/10] gdb/linux-tdep: migrate linux_find_memory_regions_full " Matthieu Longo
2026-07-09 6:36 ` Thiago Jung Bauermann
2026-07-14 8:40 ` Matthieu Longo
2026-07-24 2:51 ` Thiago Jung Bauermann
2026-07-21 21:47 ` Luis
2026-07-27 17:14 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 08/10] gdb/linux-tdep: migrate parse_smaps_data " Matthieu Longo
2026-07-09 6:41 ` Thiago Jung Bauermann
2026-07-14 8:49 ` Matthieu Longo
2026-07-24 2:52 ` Thiago Jung Bauermann
2026-07-21 21:49 ` Luis
2026-07-27 17:17 ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 09/10] gdb/linux-tdep: parse ProtectionKey in /proc/PID/smaps Matthieu Longo
2026-07-09 6:42 ` Thiago Jung Bauermann
2026-07-14 9:12 ` Matthieu Longo
2026-07-14 9:29 ` Matthieu Longo
2026-07-24 2:58 ` Thiago Jung Bauermann
2026-07-24 10:27 ` Yury Khrustalev
2026-07-25 6:40 ` Thiago Jung Bauermann
2026-07-27 8:22 ` Yury Khrustalev
2026-07-29 1:10 ` Thiago Jung Bauermann
2026-07-21 21:57 ` Luis
2026-07-27 17:54 ` Matthieu Longo
2026-07-07 15:49 ` [PATCH v1 10/10] gdb/linux: add helpers to read AT_HWCAP3 and AT_HWCAP4 Matthieu Longo
2026-07-09 6:44 ` Thiago Jung Bauermann
2026-07-21 21:58 ` Luis
2026-07-27 17:32 ` Matthieu Longo
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=87bjcg1xl8.fsf@linaro.org \
--to=thiago.bauermann@linaro.org \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=luis.machado.foss@gmail.com \
--cc=luis.machado@amd.com \
--cc=matthieu.longo@arm.com \
--cc=pedro@palves.net \
--cc=tom@tromey.com \
--cc=yury.khrustalev@arm.com \
/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