Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Matthieu Longo <matthieu.longo@arm.com>, gdb-patches@sourceware.org
Cc: Simon Marchi <simark@simark.ca>,
	Thiago Jung Bauermann <thiago.bauermann@linaro.org>,
	Luis Machado <luis.machado@amd.com>,
	Luis Machado <luis.machado.foss@gmail.com>,
	Christina Joos <christina.joos@intel.com>,
	Kevin Buettner <kevinb@redhat.com>,
	Matthieu Longo <matthieu.longo@arm.com>
Subject: Re: [PATCH v2 4/6] gdb/linux-tdep: migrate linux_info_proc to file_reader_t
Date: Thu, 10 Sep 2026 17:31:29 +0100	[thread overview]
Message-ID: <87a4ppdq5a.fsf@redhat.com> (raw)
In-Reply-To: <20260825100912.514232-5-matthieu.longo@arm.com>

Matthieu Longo <matthieu.longo@arm.com> writes:

> 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.
>
> Reviewed-By: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
> ---
>  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 84614bc91a0..e2c5b2d8815 100644
> --- a/gdb/linux-tdep.c
> +++ b/gdb/linux-tdep.c
> @@ -866,6 +866,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,

The gdbsupport/array-view.h header says that gdb::array_view objects
should usually be passed by value.  Is there a reason why this needs to
be passed by reference here?



> @@ -914,25 +947,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');

We shouldn't assert on data from an outside source.  This should be
either an error, or a warning if GDB is able to handle this and push
on.



> @@ -997,12 +1025,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';

If the buffer contains two consecutive '\n' characters then this will
overwrite the wrong one I think, e.g. "abc\n\ndef\n". IT will point at
the 'd', and this will overwrite the second '\n', not the first, which I
think is what you want.

Also, extract_string_view_from_buffer handles a missing final '\n', so
if the buffer is "abc\bdef" then IT will point to the character after
'f', and the above will overwrite 'f'.

Given that extract_string_view_from_buffer handles these cases, I think
this code should also handle them, or throw an error if we see data in a
form that you don't expect.


Thanks,
Andrew


  reply	other threads:[~2026-09-10 16:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25 10:09 [PATCH v2 0/6] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-08-25 10:09 ` [PATCH v2 1/6] target_fileio_read_stralloc: add an optional length parameter Matthieu Longo
2026-09-10 10:55   ` Andrew Burgess
2026-08-25 10:09 ` [PATCH v2 2/6] gdb support: add gdb::ranges::replace algorithm Matthieu Longo
2026-09-10 11:07   ` Andrew Burgess
2026-09-10 11:08     ` Andrew Burgess
2026-08-25 10:09 ` [PATCH v2 3/6] gdb: introduce helper class file_reader_t Matthieu Longo
2026-09-10 15:35   ` Andrew Burgess
2026-08-25 10:09 ` [PATCH v2 4/6] gdb/linux-tdep: migrate linux_info_proc to file_reader_t Matthieu Longo
2026-09-10 16:31   ` Andrew Burgess [this message]
2026-08-25 10:09 ` [PATCH v2 5/6] gdb/linux-tdep: migrate linux_find_memory_regions_full " Matthieu Longo
2026-09-11  8:34   ` Andrew Burgess
2026-08-25 10:09 ` [PATCH v2 6/6] gdb/linux-tdep: remove legacy parse_smaps_data overload Matthieu Longo
2026-09-11  8:41   ` Andrew Burgess
2026-09-16 10:55     ` Matthieu Longo
2026-09-03 22:58 ` [PATCH v2 0/6] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-09-08 10:27 ` 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=87a4ppdq5a.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=christina.joos@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=kevinb@redhat.com \
    --cc=luis.machado.foss@gmail.com \
    --cc=luis.machado@amd.com \
    --cc=matthieu.longo@arm.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