* [PATCH 1/3] gdb: store internalvars in an std::vector
@ 2023-02-14 4:21 Simon Marchi
2023-02-14 4:21 ` [PATCH 2/3] gdb: use std::string for internalvar::name Simon Marchi
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Simon Marchi @ 2023-02-14 4:21 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
Change the storage of internalvars to an std::vector of unique pointers
to internalval. This helps automate memory management, and will help
keep internalvars sorted in a subsequent patch.
I initially tried to use an std::vector<internalval> initially, but some
parts of the code need for the addresses of internalvars to be stable.
Change-Id: I1fca7e7877cc984a3a3432c7639d45e68d437241
---
gdb/value.c | 32 +++++++++++++-------------------
1 file changed, 13 insertions(+), 19 deletions(-)
diff --git a/gdb/value.c b/gdb/value.c
index 7873aeb9558e..e884913abe5a 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1829,7 +1829,6 @@ union internalvar_data
struct internalvar
{
- struct internalvar *next;
char *name;
/* We support various different kinds of content of an internal variable.
@@ -1841,7 +1840,9 @@ struct internalvar
union internalvar_data u;
};
-static struct internalvar *internalvars;
+using internalvar_up = std::unique_ptr<internalvar>;
+
+static std::vector<internalvar_up> internalvars;
/* If the variable does not already exist create it and give it the
value given. If no value is given then the default is zero. */
@@ -1891,11 +1892,9 @@ init_if_undefined_command (const char* args, int from_tty)
struct internalvar *
lookup_only_internalvar (const char *name)
{
- struct internalvar *var;
-
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
if (strcmp (var->name, name) == 0)
- return var;
+ return var.get ();
return NULL;
}
@@ -1906,12 +1905,11 @@ lookup_only_internalvar (const char *name)
void
complete_internalvar (completion_tracker &tracker, const char *name)
{
- struct internalvar *var;
int len;
len = strlen (name);
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
if (strncmp (var->name, name, len) == 0)
tracker.add_completion (make_unique_xstrdup (var->name));
}
@@ -1922,12 +1920,12 @@ complete_internalvar (completion_tracker &tracker, const char *name)
struct internalvar *
create_internalvar (const char *name)
{
- struct internalvar *var = XNEW (struct internalvar);
+ internalvars.emplace_back (new internalvar);
+ internalvar *var = internalvars.back ().get ();
var->name = xstrdup (name);
var->kind = INTERNALVAR_VOID;
- var->next = internalvars;
- internalvars = var;
+
return var;
}
@@ -2412,8 +2410,6 @@ preserve_one_varobj (struct varobj *varobj, struct objfile *objfile,
void
preserve_values (struct objfile *objfile)
{
- struct internalvar *var;
-
/* Create the hash table. We allocate on the objfile's obstack, since
it is soon to be deleted. */
htab_up copied_types = create_copied_types_hash ();
@@ -2421,8 +2417,8 @@ preserve_values (struct objfile *objfile)
for (const value_ref_ptr &item : value_history)
item->preserve (objfile, copied_types.get ());
- for (var = internalvars; var; var = var->next)
- preserve_one_internalvar (var, objfile, copied_types.get ());
+ for (internalvar_up &var : internalvars)
+ preserve_one_internalvar (var.get (), objfile, copied_types.get ());
/* For the remaining varobj, check that none has type owned by OBJFILE. */
all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj)
@@ -2438,14 +2434,12 @@ static void
show_convenience (const char *ignore, int from_tty)
{
struct gdbarch *gdbarch = get_current_arch ();
- struct internalvar *var;
int varseen = 0;
struct value_print_options opts;
get_user_print_options (&opts);
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
{
-
if (!varseen)
{
varseen = 1;
@@ -2456,7 +2450,7 @@ show_convenience (const char *ignore, int from_tty)
{
struct value *val;
- val = value_of_internalvar (gdbarch, var);
+ val = value_of_internalvar (gdbarch, var.get ());
value_print (val, gdb_stdout, &opts);
}
catch (const gdb_exception_error &ex)
--
2.39.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/3] gdb: use std::string for internalvar::name
2023-02-14 4:21 [PATCH 1/3] gdb: store internalvars in an std::vector Simon Marchi
@ 2023-02-14 4:21 ` Simon Marchi
2023-02-14 4:21 ` [PATCH 3/3] gdb: keep internalvars sorted Simon Marchi
2023-02-14 10:54 ` [PATCH 1/3] gdb: store internalvars in an std::vector Lancelot SIX
2 siblings, 0 replies; 5+ messages in thread
From: Simon Marchi @ 2023-02-14 4:21 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
Change internalvar::name to std::string, automating memory management.
Change-Id: I814d61361663e7becb8f3fb5f58c0180cdc414bc
---
gdb/value.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/gdb/value.c b/gdb/value.c
index e884913abe5a..68499896af8c 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1829,7 +1829,7 @@ union internalvar_data
struct internalvar
{
- char *name;
+ std::string name;
/* We support various different kinds of content of an internal variable.
enum internalvar_kind specifies the kind, and union internalvar_data
@@ -1893,7 +1893,7 @@ struct internalvar *
lookup_only_internalvar (const char *name)
{
for (internalvar_up &var : internalvars)
- if (strcmp (var->name, name) == 0)
+ if (var->name == name)
return var.get ();
return NULL;
@@ -1910,8 +1910,8 @@ complete_internalvar (completion_tracker &tracker, const char *name)
len = strlen (name);
for (internalvar_up &var : internalvars)
- if (strncmp (var->name, name, len) == 0)
- tracker.add_completion (make_unique_xstrdup (var->name));
+ if (strncmp (var->name.c_str (), name, len) == 0)
+ tracker.add_completion (make_unique_xstrdup (var->name.c_str ()));
}
/* Create an internal variable with name NAME and with a void value.
@@ -1923,7 +1923,7 @@ create_internalvar (const char *name)
internalvars.emplace_back (new internalvar);
internalvar *var = internalvars.back ().get ();
- var->name = xstrdup (name);
+ var->name = name;
var->kind = INTERNALVAR_VOID;
return var;
@@ -1994,7 +1994,7 @@ value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
/* If there is a trace state variable of the same name, assume that
is what we really want to see. */
- tsv = find_trace_state_variable (var->name);
+ tsv = find_trace_state_variable (var->name.c_str ());
if (tsv)
{
tsv->value_known = target_get_trace_state_variable_value (tsv->number,
@@ -2147,7 +2147,7 @@ set_internalvar (struct internalvar *var, struct value *val)
union internalvar_data new_data = { 0 };
if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
- error (_("Cannot overwrite convenience function %s"), var->name);
+ error (_("Cannot overwrite convenience function %s"), var->name.c_str ());
/* Prepare new contents. */
switch (check_typedef (val->type ())->code ())
@@ -2259,7 +2259,7 @@ clear_internalvar (struct internalvar *var)
const char *
internalvar_name (const struct internalvar *var)
{
- return var->name;
+ return var->name.c_str ();
}
static struct internal_function *
@@ -2444,7 +2444,7 @@ show_convenience (const char *ignore, int from_tty)
{
varseen = 1;
}
- gdb_printf (("$%s = "), var->name);
+ gdb_printf (("$%s = "), var->name.c_str ());
try
{
--
2.39.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/3] gdb: keep internalvars sorted
2023-02-14 4:21 [PATCH 1/3] gdb: store internalvars in an std::vector Simon Marchi
2023-02-14 4:21 ` [PATCH 2/3] gdb: use std::string for internalvar::name Simon Marchi
@ 2023-02-14 4:21 ` Simon Marchi
2023-02-14 10:54 ` [PATCH 1/3] gdb: store internalvars in an std::vector Lancelot SIX
2 siblings, 0 replies; 5+ messages in thread
From: Simon Marchi @ 2023-02-14 4:21 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
In a test (downstream in ROCgdb), there was a test case failing when
GDB_REVERSE_INIT_FUNCTIONS was set. The test was assuming a particular
order in the output of "show convenience". And the order changed with
GDB_REVERSE_INIT_FUNCTIONS.
I think that a nice way to fix it is to make the output of "show
convenience" sorted, and therefore stable. Ideally, I think that the
the user-visible behavior of GDB should not change when using
GDB_REVERSE_INIT_FUNCTIONS. Plus, it makes the output of "show
convenience" look nice, not that it's really important.
So, change create_internalvar to keep the internalvars vector sorted.
Change-Id: I3a916a641e0d50ff698f5d097ef0cf10637ab8de
---
gdb/value.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/gdb/value.c b/gdb/value.c
index 68499896af8c..2c37f94cba99 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1920,8 +1920,15 @@ complete_internalvar (completion_tracker &tracker, const char *name)
struct internalvar *
create_internalvar (const char *name)
{
- internalvars.emplace_back (new internalvar);
- internalvar *var = internalvars.back ().get ();
+ auto it = std::lower_bound (internalvars.begin (),
+ internalvars.end (),
+ name,
+ [] (const internalvar_up &var, const char *name_)
+ {
+ return var->name < name_;
+ });
+
+ internalvar *var = internalvars.emplace (it, new internalvar)->get ();
var->name = name;
var->kind = INTERNALVAR_VOID;
--
2.39.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] gdb: store internalvars in an std::vector
2023-02-14 4:21 [PATCH 1/3] gdb: store internalvars in an std::vector Simon Marchi
2023-02-14 4:21 ` [PATCH 2/3] gdb: use std::string for internalvar::name Simon Marchi
2023-02-14 4:21 ` [PATCH 3/3] gdb: keep internalvars sorted Simon Marchi
@ 2023-02-14 10:54 ` Lancelot SIX
2023-02-14 18:41 ` Simon Marchi
2 siblings, 1 reply; 5+ messages in thread
From: Lancelot SIX @ 2023-02-14 10:54 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
On Mon, Feb 13, 2023 at 11:21:37PM -0500, Simon Marchi via Gdb-patches wrote:
> Change the storage of internalvars to an std::vector of unique pointers
> to internalval. This helps automate memory management, and will help
> keep internalvars sorted in a subsequent patch.
>
> I initially tried to use an std::vector<internalval> initially, but some
> parts of the code need for the addresses of internalvars to be stable.
>
Hi Simon,
As the end-goal is to have an order, did you consider using a container
which enforces it like a std::map? For small number of objects (for
some definition of small…) vector is usually more efficient but as here
you hold pointers, I am not sure the vector brings a huge benefit.
You could use a std::map<std::string, internalvar> as the internalvar’s
address will remain stable. No need to have a unique_ptr.
One downside of the std::map is that you might end up with the name
stored twice (once as key, and maybe still once in the internalvar
object). std::set can also be used, but with other downsides.
The main difference with vector can come if we had 2 internal variables
with the same name. The current code does not prevent this AFAICT, but
as one of the vars would effectively shadow the other, I am not sure
this is a case we want to support anyway.
Best,
Lancelot.
> Change-Id: I1fca7e7877cc984a3a3432c7639d45e68d437241
> ---
> gdb/value.c | 32 +++++++++++++-------------------
> 1 file changed, 13 insertions(+), 19 deletions(-)
>
> diff --git a/gdb/value.c b/gdb/value.c
> index 7873aeb9558e..e884913abe5a 100644
> --- a/gdb/value.c
> +++ b/gdb/value.c
> @@ -1829,7 +1829,6 @@ union internalvar_data
>
> struct internalvar
> {
> - struct internalvar *next;
> char *name;
>
> /* We support various different kinds of content of an internal variable.
> @@ -1841,7 +1840,9 @@ struct internalvar
> union internalvar_data u;
> };
>
> -static struct internalvar *internalvars;
> +using internalvar_up = std::unique_ptr<internalvar>;
> +
> +static std::vector<internalvar_up> internalvars;
>
> /* If the variable does not already exist create it and give it the
> value given. If no value is given then the default is zero. */
> @@ -1891,11 +1892,9 @@ init_if_undefined_command (const char* args, int from_tty)
> struct internalvar *
> lookup_only_internalvar (const char *name)
> {
> - struct internalvar *var;
> -
> - for (var = internalvars; var; var = var->next)
> + for (internalvar_up &var : internalvars)
> if (strcmp (var->name, name) == 0)
> - return var;
> + return var.get ();
>
> return NULL;
> }
> @@ -1906,12 +1905,11 @@ lookup_only_internalvar (const char *name)
> void
> complete_internalvar (completion_tracker &tracker, const char *name)
> {
> - struct internalvar *var;
> int len;
>
> len = strlen (name);
>
> - for (var = internalvars; var; var = var->next)
> + for (internalvar_up &var : internalvars)
> if (strncmp (var->name, name, len) == 0)
> tracker.add_completion (make_unique_xstrdup (var->name));
> }
> @@ -1922,12 +1920,12 @@ complete_internalvar (completion_tracker &tracker, const char *name)
> struct internalvar *
> create_internalvar (const char *name)
> {
> - struct internalvar *var = XNEW (struct internalvar);
> + internalvars.emplace_back (new internalvar);
> + internalvar *var = internalvars.back ().get ();
>
> var->name = xstrdup (name);
> var->kind = INTERNALVAR_VOID;
> - var->next = internalvars;
> - internalvars = var;
> +
> return var;
> }
>
> @@ -2412,8 +2410,6 @@ preserve_one_varobj (struct varobj *varobj, struct objfile *objfile,
> void
> preserve_values (struct objfile *objfile)
> {
> - struct internalvar *var;
> -
> /* Create the hash table. We allocate on the objfile's obstack, since
> it is soon to be deleted. */
> htab_up copied_types = create_copied_types_hash ();
> @@ -2421,8 +2417,8 @@ preserve_values (struct objfile *objfile)
> for (const value_ref_ptr &item : value_history)
> item->preserve (objfile, copied_types.get ());
>
> - for (var = internalvars; var; var = var->next)
> - preserve_one_internalvar (var, objfile, copied_types.get ());
> + for (internalvar_up &var : internalvars)
> + preserve_one_internalvar (var.get (), objfile, copied_types.get ());
>
> /* For the remaining varobj, check that none has type owned by OBJFILE. */
> all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj)
> @@ -2438,14 +2434,12 @@ static void
> show_convenience (const char *ignore, int from_tty)
> {
> struct gdbarch *gdbarch = get_current_arch ();
> - struct internalvar *var;
> int varseen = 0;
> struct value_print_options opts;
>
> get_user_print_options (&opts);
> - for (var = internalvars; var; var = var->next)
> + for (internalvar_up &var : internalvars)
> {
> -
> if (!varseen)
> {
> varseen = 1;
> @@ -2456,7 +2450,7 @@ show_convenience (const char *ignore, int from_tty)
> {
> struct value *val;
>
> - val = value_of_internalvar (gdbarch, var);
> + val = value_of_internalvar (gdbarch, var.get ());
> value_print (val, gdb_stdout, &opts);
> }
> catch (const gdb_exception_error &ex)
> --
> 2.39.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] gdb: store internalvars in an std::vector
2023-02-14 10:54 ` [PATCH 1/3] gdb: store internalvars in an std::vector Lancelot SIX
@ 2023-02-14 18:41 ` Simon Marchi
0 siblings, 0 replies; 5+ messages in thread
From: Simon Marchi @ 2023-02-14 18:41 UTC (permalink / raw)
To: Lancelot SIX, Simon Marchi; +Cc: gdb-patches
On 2/14/23 05:54, Lancelot SIX via Gdb-patches wrote:
> On Mon, Feb 13, 2023 at 11:21:37PM -0500, Simon Marchi via Gdb-patches wrote:
>> Change the storage of internalvars to an std::vector of unique pointers
>> to internalval. This helps automate memory management, and will help
>> keep internalvars sorted in a subsequent patch.
>>
>> I initially tried to use an std::vector<internalval> initially, but some
>> parts of the code need for the addresses of internalvars to be stable.
>>
>
> Hi Simon,
>
> As the end-goal is to have an order, did you consider using a container
> which enforces it like a std::map? For small number of objects (for
> some definition of small…) vector is usually more efficient but as here
> you hold pointers, I am not sure the vector brings a huge benefit.
>
> You could use a std::map<std::string, internalvar> as the internalvar’s
> address will remain stable. No need to have a unique_ptr.
>
> One downside of the std::map is that you might end up with the name
> stored twice (once as key, and maybe still once in the internalvar
> object). std::set can also be used, but with other downsides.
I had not thought of that, but I think it's a good idea. Having the
name stored twice is not an issue, I think.
> The main difference with vector can come if we had 2 internal variables
> with the same name. The current code does not prevent this AFAICT, but
> as one of the vars would effectively shadow the other, I am not sure
> this is a case we want to support anyway.
I don't think it can really happen today anyway.
create_internalvar_type_lazy is only used to create variable known in
advance, for which we know there are no clashes. And through
lookup_internalvar, create_internalvar is only called after seeing that
there is no variable by that name. So, I think we'll be fine with a
map.
Simon
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-02-14 18:41 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-14 4:21 [PATCH 1/3] gdb: store internalvars in an std::vector Simon Marchi
2023-02-14 4:21 ` [PATCH 2/3] gdb: use std::string for internalvar::name Simon Marchi
2023-02-14 4:21 ` [PATCH 3/3] gdb: keep internalvars sorted Simon Marchi
2023-02-14 10:54 ` [PATCH 1/3] gdb: store internalvars in an std::vector Lancelot SIX
2023-02-14 18:41 ` Simon Marchi
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).