From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id 3EA593858000; Wed, 15 Feb 2023 17:00:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EA593858000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676480425; bh=vwV3+Tcj2ydGrhZlUEBNakX90ZrS1b2UlTcQQSrLfDk=; h=From:To:Subject:Date:From; b=jGk4qIWFi+x392ORIjm41sqwP2HG5qKzlBsyWCcp8TmnhCLxMYqba77Vj2Grly1d6 cCy8DydLHBnX9Hbu/uq+VcIDY2qcFdhEJb9W0PLgoTJ9KcWXLbDhj7gd4rziGAv9m8 83iJUCJGSzLIRDO39N6HBfvxs/1cNKHDAgliG2PE= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: store internalvars in an std::map X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: dbca589b8d72316a5884321747b6ab92c6b34dbc X-Git-Newrev: 11470e70ea0d692db00285422efa474224727487 Message-Id: <20230215170025.3EA593858000@sourceware.org> Date: Wed, 15 Feb 2023 17:00:25 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D11470e70ea0d= 692db00285422efa474224727487 commit 11470e70ea0d692db00285422efa474224727487 Author: Simon Marchi Date: Tue Feb 14 14:23:27 2023 -0500 gdb: store internalvars in an std::map =20 In a test downstream in ROCgdb, we had 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 changes when running with GDB_REVERSE_INIT_FUNCTIONS. =20 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. =20 Implement this by storing the internal vars in an std::map, which is a sorted container. =20 Change-Id: I1fca7e7877cc984a3a3432c7639d45e68d437241 Approved-By: Tom Tromey Diff: --- gdb/value.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/gdb/value.c b/gdb/value.c index 1b74a6ecc4f..34a282b9746 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -41,6 +41,7 @@ #include "user-regs.h" #include #include +#include #include #include #include "completer.h" @@ -1833,7 +1834,6 @@ struct internalvar : name (std::move (name)) {} =20 - struct internalvar *next =3D nullptr; std::string name; =20 /* We support various different kinds of content of an internal variable. @@ -1845,7 +1845,10 @@ struct internalvar union internalvar_data u; }; =20 -static struct internalvar *internalvars; +/* Use std::map, a sorted container, to make the order of iteration (and + therefore the output of "show convenience" stable). */ + +static std::map internalvars; =20 /* 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. */ @@ -1895,13 +1898,11 @@ init_if_undefined_command (const char* args, int fr= om_tty) struct internalvar * lookup_only_internalvar (const char *name) { - struct internalvar *var; - - for (var =3D internalvars; var; var =3D var->next) - if (var->name =3D=3D name) - return var; + auto it =3D internalvars.find (name); + if (it =3D=3D internalvars.end ()) + return nullptr; =20 - return NULL; + return &it->second; } =20 /* Complete NAME by comparing it to the names of internal @@ -1910,27 +1911,29 @@ lookup_only_internalvar (const char *name) void complete_internalvar (completion_tracker &tracker, const char *name) { - struct internalvar *var; - int len; + int len =3D strlen (name); =20 - len =3D strlen (name); + for (auto &pair : internalvars) + { + const internalvar &var =3D pair.second; =20 - for (var =3D internalvars; var; var =3D var->next) - if (var->name.compare (0, len, name) =3D=3D 0) - tracker.add_completion (make_unique_xstrdup (var->name.c_str ())); + if (var.name.compare (0, len, name) =3D=3D 0) + tracker.add_completion (make_unique_xstrdup (var.name.c_str ())); + } } =20 /* Create an internal variable with name NAME and with a void value. - NAME should not normally include a dollar sign. */ + NAME should not normally include a dollar sign. + + An internal variable with that name must not exist already. */ =20 struct internalvar * create_internalvar (const char *name) { - internalvar *var =3D new internalvar (name); + auto pair =3D internalvars.emplace (std::make_pair (name, internalvar (n= ame))); + gdb_assert (pair.second); =20 - var->next =3D internalvars; - internalvars =3D var; - return var; + return &pair.first->second; } =20 /* Create an internal variable with name NAME and register FUN as the @@ -2417,8 +2420,6 @@ preserve_one_varobj (struct varobj *varobj, struct ob= jfile *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 =3D create_copied_types_hash (); @@ -2426,8 +2427,8 @@ preserve_values (struct objfile *objfile) for (const value_ref_ptr &item : value_history) item->preserve (objfile, copied_types.get ()); =20 - for (var =3D internalvars; var; var =3D var->next) - preserve_one_internalvar (var, objfile, copied_types.get ()); + for (auto &pair : internalvars) + preserve_one_internalvar (&pair.second, objfile, copied_types.get ()); =20 /* For the remaining varobj, check that none has type owned by OBJFILE. = */ all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj) @@ -2443,25 +2444,25 @@ static void show_convenience (const char *ignore, int from_tty) { struct gdbarch *gdbarch =3D get_current_arch (); - struct internalvar *var; int varseen =3D 0; struct value_print_options opts; =20 get_user_print_options (&opts); - for (var =3D internalvars; var; var =3D var->next) + for (auto &pair : internalvars) { + internalvar &var =3D pair.second; =20 if (!varseen) { varseen =3D 1; } - gdb_printf (("$%s =3D "), var->name.c_str ()); + gdb_printf (("$%s =3D "), var.name.c_str ()); =20 try { struct value *val; =20 - val =3D value_of_internalvar (gdbarch, var); + val =3D value_of_internalvar (gdbarch, &var); value_print (val, gdb_stdout, &opts); } catch (const gdb_exception_error &ex)