From: Matthieu Longo <matthieu.longo@arm.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org, Andrew Burgess <aburgess@redhat.com>
Subject: Re: [PATCH v3 5/8] gdb/linux: refactor /proc mapping parsing to use array_view
Date: Wed, 11 Nov 2026 10:41:54 +0000 [thread overview]
Message-ID: <bfd8cfa7-a2f3-453f-9fab-05b5b3c78689@arm.com> (raw)
In-Reply-To: <87wlqrvvkr.fsf@tromey.com>
On 05/11/2026 21:04, Tom Tromey wrote:
>>>>>> Matthieu Longo <matthieu.longo@arm.com> writes:
>
>> Refactor read_mapping and smaps parsing to operate on gdb::array_view<char>
>> instead of relying on NUL-terminated strings and strtok_r to split mapping
>> headers.
>
>> Track the amount of data returned by target_fileio_read_stralloc and use
>> extract_next_field to iterate over mapping lines.
>
>> Update read_mapping to parse fields using bounded iterators and diagnose
>> malformed addresses, separators, offsets, and inode values.
>
> Thanks. I had a couple questions about this.
>
>> static mapping
>> -read_mapping (const char *line)
>> +read_mapping (gdb::array_view<char> line)
>> {
>> struct mapping mapping;
>> - const char *p = line;
>
>> - mapping.addr = strtoulst (p, &p, 16);
>> - if (*p == '-')
>> - p++;
>> - mapping.endaddr = strtoulst (p, &p, 16);
>> + decltype(line)::const_iterator it = line.begin ();
>> + decltype(line)::const_iterator it_next;
>
> Not your problem but if/when gdb::span lands, this will be an issue,
> because std::span doesn't have const iterators.
>
> But, this function could probably take an array_view<const char>.
>
> I do wonder why array_view and not string_view.
>
See the before last line of the function:
/* Ensure that the line ends with '0'. */
*line.end () = '\0';
This is a pity. Some code here and there has some assumption that the object file name has null-byte
terminator. For instance, here:
current_uiout->field_string ("objfile", m.filename,
file_name_style.style ());
One solution can consist in adding a new method field_string_view to ui_out.
I gave a quick shot at it, and I think that, if you want to go this way, I will have to publish an
additional patch adding the method before this patch.
See below the draft to give you a rough idea.
Please note that my approach duplicates a lot of code. I think that adding a size parameter to the
existing do_field_string would do better than adding a do_field_string_view. Do you see any reason
not to do this ?
diff --git a/gdb/cli-out.c b/gdb/cli-out.c
index 0c339d0e819..8af3899f251 100644
--- a/gdb/cli-out.c
+++ b/gdb/cli-out.c
@@ -187,6 +187,58 @@ cli_ui_out::do_field_string (int fldno, int width, ui_align align,
field_separator ();
}
+void
+cli_ui_out::do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style)
+{
+ int before = 0;
+ int after = 0;
+
+ if (m_suppress_output)
+ return;
+
+ if ((align != ui_noalign) && s)
+ {
+ before = width - n;
+ if (before <= 0)
+ before = 0;
+ else
+ {
+ if (align == ui_right)
+ after = 0;
+ else if (align == ui_left)
+ {
+ after = before;
+ before = 0;
+ }
+ else
+ /* ui_center */
+ {
+ after = before / 2;
+ before -= after;
+ }
+ }
+ }
+
+ if (before)
+ spaces (before);
+
+ if (s)
+ {
+ ui_file *stream = m_streams.back ();
+ stream->emit_style_escape (style);
+ stream->write (s, n);
+ stream->emit_style_escape (ui_file_style ());
+ }
+
+ if (after)
+ spaces (after);
+
+ if (align != ui_noalign)
+ field_separator ();
+}
+
void
cli_ui_out::do_spaces (int numspaces)
{
diff --git a/gdb/cli-out.h b/gdb/cli-out.h
index 4c7ae6e656e..2baa26de7b5 100644
--- a/gdb/cli-out.h
+++ b/gdb/cli-out.h
@@ -65,6 +65,10 @@ class cli_ui_out : public ui_out
const char *fldname,
const char *string,
const ui_file_style &style) override;
+ virtual void do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style)
+ override;
virtual void do_spaces (int numspaces) override;
virtual void do_text (const char *string) override;
virtual void do_message (ui_file_style ¤t_style,
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 50583797604..bda5992f443 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -502,21 +502,18 @@ struct mapping
ULONGEST offset;
std::string_view device;
ULONGEST inode;
-
- /* This field is guaranteed to be NULL-terminated, hence it is not a
- std::string_view. */
- const char *filename;
+ std::string_view filename;
};
/* Service function for corefiles and info proc. */
static mapping
-read_mapping (gdb::array_view<char> line)
+read_mapping (std::string_view line)
{
struct mapping mapping;
- decltype(line)::const_iterator it = line.begin ();
- decltype(line)::const_iterator it_next;
+ decltype(line)::iterator it = line.begin ();
+ decltype(line)::iterator it_next;
mapping.addr = strtoulst (it, &it_next, 16);
if (it == it_next)
@@ -534,9 +531,9 @@ read_mapping (gdb::array_view<char> line)
it = it_next;
/* Skip spaces. */
- it = std::find_if_not (it, line.cend (), c_isspace);
+ it = std::find_if_not (it, line.end (), c_isspace);
- it_next = std::find_if (it, line.cend (), c_isspace);
+ it_next = std::find_if (it, line.end (), c_isspace);
mapping.permissions = std::string (it, it_next);
it = it_next;
@@ -547,9 +544,9 @@ read_mapping (gdb::array_view<char> line)
it = it_next;
/* Skip spaces. */
- it = std::find_if_not (it, line.cend (), c_isspace);
+ it = std::find_if_not (it, line.end (), c_isspace);
- it_next = std::find_if (it, line.cend (), c_isspace);
+ it_next = std::find_if (it, line.end (), c_isspace);
mapping.device = std::string (it, it_next);
it = it_next;
@@ -560,11 +557,9 @@ read_mapping (gdb::array_view<char> line)
it = it_next;
/* Skip spaces. */
- it = std::find_if_not (it, line.cend (), c_isspace);
+ it = std::find_if_not (it, line.end (), c_isspace);
- mapping.filename = it;
- /* Ensure that the line ends with '0'. */
- *line.end () = '\0';
+ mapping.filename = std::string_view (it, std::distance(it, line.end ()));
return mapping;
}
@@ -656,7 +651,7 @@ struct mapping_regexes
Then FILENAME will be "/path/to/file". */
static int
-mapping_is_anonymous_p (const char *filename)
+mapping_is_anonymous_p (const std::string_view &filename)
{
static std::optional<mapping_regexes> regexes;
static int init_regex_p = 0;
@@ -675,9 +670,9 @@ mapping_is_anonymous_p (const char *filename)
if (init_regex_p == -1)
{
- const char deleted[] = " (deleted)";
- size_t del_len = sizeof (deleted) - 1;
- size_t filename_len = strlen (filename);
+ std::string deleted = " (deleted)";
+ size_t del_len = deleted.size ();
+ size_t filename_len = filename.size ();
/* There was an error while compiling the regex'es above. In
order to try to give some reliable information to the caller,
@@ -685,13 +680,14 @@ mapping_is_anonymous_p (const char *filename)
If we managed to find it, then we assume the mapping is
anonymous. */
return (filename_len >= del_len
- && streq (filename + filename_len - del_len, deleted));
+ && filename.substr (filename_len - del_len) == deleted);
}
- if (*filename == '\0'
- || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
- || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
- || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
+ const std::string filename_nbs (filename);
+ if (!filename_nbs.empty ()
+ || regexes->dev_zero.exec (filename_nbs.c_str (), 0, NULL, 0) == 0
+ || regexes->shmem_file.exec (filename_nbs.c_str (), 0, NULL, 0) == 0
+ || regexes->file_deleted.exec (filename_nbs.c_str (), 0, NULL, 0) == 0)
return 1;
return 0;
@@ -1034,7 +1030,8 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
auto [line, next_line_begin]
= extract_next_field (content, it, '\n');
it = next_line_begin;
- mapping m = read_mapping (line);
+ mapping m
+ = read_mapping (std::string_view (line.data (), line.size ()));
ui_out_emit_tuple tuple_emitter (current_uiout);
current_uiout->field_core_addr ("start", gdbarch, m.addr);
@@ -1047,8 +1044,8 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
current_uiout->field_string ("offset",
paddress (gdbarch, m.offset));
current_uiout->field_string ("perms", m.permissions);
- current_uiout->field_string ("objfile", m.filename,
- file_name_style.style ());
+ current_uiout->field_string_view ("objfile", m.filename,
+ file_name_style.style ());
current_uiout->text ("\n");
}
}
@@ -1599,7 +1596,9 @@ parse_smaps_data (gdb::array_view<char> data,
it = next_line_begin;
/* Parse a region's header. */
- mapping m = read_mapping (region_header_line);
+ mapping m
+ = read_mapping (std::string_view(region_header_line.data (),
+ region_header_line.size ()));
struct smaps_vmflags v;
memset (&v, 0, sizeof (v));
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index cba6acbbab5..66ffc94d2a6 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -141,6 +141,22 @@ mi_ui_out::do_field_string (int fldno, int width, ui_align align,
gdb_printf (stream, "\"");
}
+void
+mi_ui_out::do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style)
+{
+ ui_file *stream = m_streams.back ();
+ field_separator ();
+
+ if (fldname)
+ gdb_printf (stream, "%s=", fldname);
+ gdb_printf (stream, "\"");
+ if (s)
+ stream->putstrn (s, n, '"');
+ gdb_printf (stream, "\"");
+}
+
void
mi_ui_out::do_spaces (int numspaces)
{
diff --git a/gdb/mi/mi-out.h b/gdb/mi/mi-out.h
index ce1e5dadc4c..53fc6a2ee94 100644
--- a/gdb/mi/mi-out.h
+++ b/gdb/mi/mi-out.h
@@ -72,6 +72,10 @@ class mi_ui_out : public ui_out
virtual void do_field_string (int fldno, int width, ui_align align,
const char *fldname, const char *string,
const ui_file_style &style) override;
+ virtual void do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style)
+ override;
virtual void do_spaces (int numspaces) override;
virtual void do_text (const char *string) override;
virtual void do_message (ui_file_style ¤t_style,
diff --git a/gdb/python/py-mi.c b/gdb/python/py-mi.c
index 7e8105fdade..739f60ecaf2 100644
--- a/gdb/python/py-mi.c
+++ b/gdb/python/py-mi.c
@@ -119,6 +119,18 @@ py_ui_out::do_field_string (int fldno, int width, ui_align align,
add_field (fldname, val);
}
+void
+py_ui_out::do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s, size_t n,
+ const ui_file_style &style)
+{
+ if (m_error.has_value ())
+ return;
+
+ gdbpy_ref<> val = host_string_to_python_string (std::string_view (s, n));
+ add_field (fldname, val);
+}
+
/* Implementation of the gdb.execute_mi command. */
PyObject *
diff --git a/gdb/python/py-uiout.h b/gdb/python/py-uiout.h
index 5a1ee9c09b3..3bccb7a03c4 100644
--- a/gdb/python/py-uiout.h
+++ b/gdb/python/py-uiout.h
@@ -99,6 +99,10 @@ class py_ui_out : public ui_out
const char *fldname, const char *string,
const ui_file_style &style) override;
+ void do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style) override;
+
void do_spaces (int numspaces) override
{ }
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 45bb1d821a1..396587134e7 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -522,6 +522,20 @@ ui_out::field_string (const char *fldname, const char *string,
do_field_string (fldno, width, align, fldname, string, style);
}
+void
+ui_out::field_string_view (const char *fldname, std::string_view s,
+ const ui_file_style &style)
+{
+ int fldno;
+ int width;
+ ui_align align;
+
+ verify_field (&fldno, &width, &align);
+
+ do_field_string_view (fldno, width, align, fldname,
+ s.data (), s.size (), style);
+}
+
/* VARARGS */
void
ui_out::field_fmt (const char *fldname, const char *format, ...)
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 26d4a6b7d0e..4ad859a410c 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -197,6 +197,10 @@ class ui_out
{
field_string (fldname, string.c_str (), style);
}
+
+ void field_string_view (const char *fldname, std::string_view string,
+ const ui_file_style &style = ui_file_style ());
+
void field_stream (const char *fldname, string_file &stream,
const ui_file_style &style = ui_file_style ());
void field_skip (const char *fldname);
@@ -360,6 +364,9 @@ class ui_out
virtual void do_field_string (int fldno, int width, ui_align align,
const char *fldname, const char *string,
const ui_file_style &style) = 0;
+ virtual void do_field_string_view (int fldno, int width, ui_align align,
+ const char *fldname, const char *s,
+ size_t n, const ui_file_style &style) = 0;
virtual void do_spaces (int numspaces) = 0;
virtual void do_text (const char *string) = 0;
>> + LONGEST len = 0;
>> gdb::unique_xmalloc_ptr<char> map
>> - = target_fileio_read_stralloc (NULL, filename);
>> + = target_fileio_read_stralloc (NULL, filename, &len);
>
> I think target_fileio_read_stralloc guarantees \0-termination.
>
> So with string_view you wouldn't even need the length stuff I think?
>
> But maybe there's some later reason.
>
The next patch introducing target_file_reader tries to mutualize the code to read text and binary
data files. Hence my choice of using gdb::array_view.
>> struct smaps_vmflags v;
>> + memset (&v, 0, sizeof (v));
>
> Would `smaps_vmflags v {}' work?
>
>> + map.mapping_anon_p = mapping_anon_p? true : false;
>> + map.mapping_file_p = mapping_file_p? true : false;
>
> Missing spaces before the "?".
Oups, this was old code. I just indented it. Fixed in the next revision.
>
> Tom
Matthieu
next prev parent reply other threads:[~2026-11-11 10:43 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 13:50 [PATCH v3 0/8] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 1/8] gdb support: add gdb::ranges::replace algorithm Matthieu Longo
2026-11-05 20:39 ` Tom Tromey
2026-09-22 13:50 ` [PATCH v3 2/8] gdb/linux: remove redundant struct prefixes from smaps_data Matthieu Longo
2026-11-05 20:40 ` Tom Tromey
2026-09-22 13:50 ` [PATCH v3 3/8] gdb/linux: style filenames in /proc warning messages Matthieu Longo
2026-11-05 20:41 ` Tom Tromey
2026-09-22 13:50 ` [PATCH v3 4/8] gdbsupport: add extract_next_field helper Matthieu Longo
2026-11-05 20:51 ` Tom Tromey
2026-11-10 12:51 ` Matthieu Longo
2026-11-13 18:20 ` Tom Tromey
2026-11-16 11:11 ` Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 5/8] gdb/linux: refactor /proc mapping parsing to use array_view Matthieu Longo
2026-11-05 21:04 ` Tom Tromey
2026-11-11 10:41 ` Matthieu Longo [this message]
2026-11-13 18:23 ` Tom Tromey
2026-11-11 10:56 ` Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 6/8] gdb: introduce helper class target_file_reader Matthieu Longo
2026-11-05 21:16 ` Tom Tromey
2026-11-11 11:24 ` Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 7/8] gdb/linux-tdep: use target_file_reader for procfs parsing Matthieu Longo
2026-11-05 21:27 ` Tom Tromey
2026-11-11 12:08 ` Matthieu Longo
2026-09-22 13:50 ` [PATCH v3 8/8] gdb/linux-tdep: remove legacy parse_smaps_data overload Matthieu Longo
2026-11-05 21:28 ` Tom Tromey
2026-10-09 14:59 ` [PATCH v3 0/8] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-10-26 10:18 ` 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=bfd8cfa7-a2f3-453f-9fab-05b5b3c78689@arm.com \
--to=matthieu.longo@arm.com \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.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