diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2021-09-10 17:10:13 -0400 |
---|---|---|
committer | Lancelot SIX <lsix@lancelotsix.com> | 2021-10-03 17:53:16 +0100 |
commit | e0700ba44c5695d07f4cc9841315adc91ca18bf5 (patch) | |
tree | b8ba80e26fb783ab67094df1722733c9d1ff101b /gdb/auto-load.c | |
parent | 1d7fe7f01b93ecaeb3e481ed09d3deac7890a97f (diff) | |
download | gdb-e0700ba44c5695d07f4cc9841315adc91ca18bf5.zip gdb-e0700ba44c5695d07f4cc9841315adc91ca18bf5.tar.gz gdb-e0700ba44c5695d07f4cc9841315adc91ca18bf5.tar.bz2 |
gdb: make string-like set show commands use std::string variable
String-like settings (var_string, var_filename, var_optional_filename,
var_string_noescape) currently take a pointer to a `char *` storage
variable (typically global) that holds the setting's value. I'd like to
"mordernize" this by changing them to use an std::string for storage.
An obvious reason is that string operations on std::string are often
easier to write than with C strings. And they avoid having to do any
manual memory management.
Another interesting reason is that, with `char *`, nullptr and an empty
string often both have the same meaning of "no value". String settings
are initially nullptr (unless initialized otherwise). But when doing
"set foo" (where `foo` is a string setting), the setting now points to
an empty string. For example, solib_search_path is nullptr at startup,
but points to an empty string after doing "set solib-search-path". This
leads to some code that needs to check for both to check for "no value".
Or some code that converts back and forth between NULL and "" when
getting or setting the value. I find this very error-prone, because it
is very easy to forget one or the other. With std::string, we at least
know that the variable is not "NULL". There is only one way of
representing an empty string setting, that is with an empty string.
I was wondering whether the distinction between NULL and "" would be
important for some setting, but it doesn't seem so. If that ever
happens, it would be more C++-y and self-descriptive to use
optional<string> anyway.
Actually, there's one spot where this distinction mattered, it's in
init_history, for the test gdb.base/gdbinit-history.exp. init_history
sets the history filename to the default ".gdb_history" if it sees that
the setting was never set - if history_filename is nullptr. If
history_filename is an empty string, it means the setting was explicitly
cleared, so it leaves it as-is. With the change to std::string, this
distinction doesn't exist anymore. This can be fixed by moving the code
that chooses a good default value for history_filename to
_initialize_top. This is ran before -ex commands are processed, so an
-ex command can then clear that value if needed (what
gdb.base/gdbinit-history.exp tests).
Another small improvement, in my opinion is that we can now easily
give string parameters initial values, by simply initializing the global
variables, instead of xstrdup-ing it in the _initialize function.
In Python and Guile, when registering a string-like parameter, we
allocate (with new) an std::string that is owned by the param_smob (in
Guile) and the parmpy_object (in Python) objects.
This patch started by changing all relevant add_setshow_* commands to
take an `std::string *` instead of a `char **` and fixing everything
that failed to build. That includes of course all string setting
variable and their uses.
string_option_def now uses an std::string also, because there's a
connection between options and settings (see
add_setshow_cmds_for_options).
The add_path function in source.c is really complex and twisted, I'd
rather not try to change it to work on an std::string right now.
Instead, I added an overload that copies the std:string to a `char *`
and back. This means more copying, but this is not used in a hot path
at all, so I think it is acceptable.
Change-Id: I92c50a1bdd8307141cdbacb388248e4e4fc08c93
Co-authored-by: Lancelot SIX <lsix@lancelotsix.com>
Diffstat (limited to 'gdb/auto-load.c')
-rw-r--r-- | gdb/auto-load.c | 48 |
1 files changed, 17 insertions, 31 deletions
diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 7f0bb74..36d8725 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -137,7 +137,7 @@ show_auto_load_local_gdbinit (struct ui_file *file, int from_tty, /* Directory list from which to load auto-loaded scripts. It is not checked for absolute paths but they are strongly recommended. It is initialized by _initialize_auto_load. */ -static char *auto_load_dir; +static std::string auto_load_dir = AUTO_LOAD_DIR; /* "set" command for the auto_load_dir configuration variable. */ @@ -145,11 +145,8 @@ static void set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c) { /* Setting the variable to "" resets it to the compile time defaults. */ - if (auto_load_dir[0] == '\0') - { - xfree (auto_load_dir); - auto_load_dir = xstrdup (AUTO_LOAD_DIR); - } + if (auto_load_dir.empty ()) + auto_load_dir = AUTO_LOAD_DIR; } /* "show" command for the auto_load_dir configuration variable. */ @@ -166,7 +163,7 @@ show_auto_load_dir (struct ui_file *file, int from_tty, /* Directory list safe to hold auto-loaded files. It is not checked for absolute paths but they are strongly recommended. It is initialized by _initialize_auto_load. */ -static char *auto_load_safe_path; +static std::string auto_load_safe_path = AUTO_LOAD_SAFE_PATH; /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized by tilde_expand and possibly each entries has added its gdb_realpath @@ -181,7 +178,7 @@ auto_load_expand_dir_vars (const char *string) { char *s = xstrdup (string); substitute_path_component (&s, "$datadir", gdb_datadir.c_str ()); - substitute_path_component (&s, "$debugdir", debug_file_directory); + substitute_path_component (&s, "$debugdir", debug_file_directory.c_str ()); if (debug_auto_load && strcmp (s, string) != 0) auto_load_debug_printf ("Expanded $-variables to \"%s\".", s); @@ -199,9 +196,10 @@ static void auto_load_safe_path_vec_update (void) { auto_load_debug_printf ("Updating directories of \"%s\".", - auto_load_safe_path); + auto_load_safe_path.c_str ()); - auto_load_safe_path_vec = auto_load_expand_dir_vars (auto_load_safe_path); + auto_load_safe_path_vec + = auto_load_expand_dir_vars (auto_load_safe_path.c_str ()); size_t len = auto_load_safe_path_vec.size (); /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC @@ -253,11 +251,8 @@ set_auto_load_safe_path (const char *args, int from_tty, struct cmd_list_element *c) { /* Setting the variable to "" resets it to the compile time defaults. */ - if (auto_load_safe_path[0] == '\0') - { - xfree (auto_load_safe_path); - auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); - } + if (auto_load_safe_path.empty ()) + auto_load_safe_path = AUTO_LOAD_SAFE_PATH; auto_load_safe_path_vec_update (); } @@ -291,17 +286,14 @@ show_auto_load_safe_path (struct ui_file *file, int from_tty, static void add_auto_load_safe_path (const char *args, int from_tty) { - char *s; - if (args == NULL || *args == 0) error (_("\ Directory argument required.\n\ Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\ ")); - s = xstrprintf ("%s%c%s", auto_load_safe_path, DIRNAME_SEPARATOR, args); - xfree (auto_load_safe_path); - auto_load_safe_path = s; + auto_load_safe_path = string_printf ("%s%c%s", auto_load_safe_path.c_str (), + DIRNAME_SEPARATOR, args); auto_load_safe_path_vec_update (); } @@ -312,14 +304,11 @@ Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\ static void add_auto_load_dir (const char *args, int from_tty) { - char *s; - if (args == NULL || *args == 0) error (_("Directory argument required.")); - s = xstrprintf ("%s%c%s", auto_load_dir, DIRNAME_SEPARATOR, args); - xfree (auto_load_dir); - auto_load_dir = s; + auto_load_dir = string_printf ("%s%c%s", auto_load_dir.c_str (), + DIRNAME_SEPARATOR, args); } /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME @@ -459,7 +448,7 @@ file_is_auto_load_safe (const char *filename) warning (_("File \"%ps\" auto-loading has been declined by your " "`auto-load safe-path' set to \"%s\"."), styled_string (file_name_style.style (), filename_real.get ()), - auto_load_safe_path); + auto_load_safe_path.c_str ()); if (!advice_printed) { @@ -749,11 +738,11 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, directory. */ std::vector<gdb::unique_xmalloc_ptr<char>> vec - = auto_load_expand_dir_vars (auto_load_dir); + = auto_load_expand_dir_vars (auto_load_dir.c_str ()); auto_load_debug_printf ("Searching 'set auto-load scripts-directory' path \"%s\".", - auto_load_dir); + auto_load_dir.c_str ()); /* Convert Windows file name from c:/dir/file to /c/dir/file. */ if (HAS_DRIVE_SPEC (debugfile)) @@ -1542,8 +1531,6 @@ This option has security implications for untrusted inferiors."), Usage: info auto-load local-gdbinit"), auto_load_info_cmdlist_get ()); - auto_load_dir = xstrdup (AUTO_LOAD_DIR); - suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB)); gdb_name_help = xstrprintf (_("\ @@ -1595,7 +1582,6 @@ Show the list of directories from which to load auto-loaded scripts."), xfree (gdb_name_help); xfree (guile_name_help); - auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH); auto_load_safe_path_vec_update (); add_setshow_optional_filename_cmd ("safe-path", class_support, &auto_load_safe_path, _("\ |