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/cli | |
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/cli')
-rw-r--r-- | gdb/cli/cli-cmds.c | 49 | ||||
-rw-r--r-- | gdb/cli/cli-decode.c | 38 | ||||
-rw-r--r-- | gdb/cli/cli-logging.c | 23 | ||||
-rw-r--r-- | gdb/cli/cli-option.c | 9 | ||||
-rw-r--r-- | gdb/cli/cli-option.h | 4 | ||||
-rw-r--r-- | gdb/cli/cli-setshow.c | 59 |
6 files changed, 88 insertions, 94 deletions
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index ecbe5a4..f8f0133 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -650,7 +650,7 @@ find_and_open_script (const char *script_file, int search_path) /* Search for and open 'file' on the search path used for source files. Put the full location in *FULL_PATHP. */ gdb::unique_xmalloc_ptr<char> full_path; - fd = openp (source_path, search_flags, + fd = openp (source_path.c_str (), search_flags, file.get (), O_RDONLY, &full_path); if (fd == -1) @@ -1042,12 +1042,7 @@ edit_command (const char *arg, int from_tty) struct pipe_cmd_opts { /* For "-d". */ - char *delimiter = nullptr; - - ~pipe_cmd_opts () - { - xfree (delimiter); - } + std::string delimiter; }; static const gdb::option::option_def pipe_cmd_option_defs[] = { @@ -1084,8 +1079,8 @@ pipe_command (const char *arg, int from_tty) (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp); const char *delim = "|"; - if (opts.delimiter != nullptr) - delim = opts.delimiter; + if (!opts.delimiter.empty ()) + delim = opts.delimiter.c_str (); const char *command = arg; if (command == nullptr) @@ -1148,8 +1143,8 @@ pipe_command_completer (struct cmd_list_element *ignore, return; const char *delimiter = "|"; - if (opts.delimiter != nullptr) - delimiter = opts.delimiter; + if (!opts.delimiter.empty ()) + delimiter = opts.delimiter.c_str (); /* Check if we're past option values already. */ if (text > org_text && !isspace (text[-1])) @@ -2152,13 +2147,21 @@ value_from_setting (const setting &var, struct gdbarch *gdbarch) case var_enum: { const char *value; + size_t len; if (var.type () == var_enum) - value = var.get<const char *> (); + { + value = var.get<const char *> (); + len = strlen (value); + } else - value = var.get<char *> (); + { + const std::string &st = var.get<std::string> (); + value = st.c_str (); + len = st.length (); + } - if (value != nullptr) - return value_cstring (value, strlen (value), + if (len > 0) + return value_cstring (value, len, builtin_type (gdbarch)->builtin_char); else return value_cstring ("", 1, @@ -2231,13 +2234,21 @@ str_value_from_setting (const setting &var, struct gdbarch *gdbarch) similarly to the value_from_setting code for these casevar. */ { const char *value; + size_t len; if (var.type () == var_enum) - value = var.get<const char *> (); + { + value = var.get<const char *> (); + len = strlen (value); + } else - value = var.get<char *> (); + { + const std::string &st = var.get<std::string> (); + value = st.c_str (); + len = st.length (); + } - if (value != nullptr) - return value_cstring (value, strlen (value), + if (len > 0) + return value_cstring (value, len, builtin_type (gdbarch)->builtin_char); else return value_cstring ("", 1, diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c index 56befc9..b3bf627 100644 --- a/gdb/cli/cli-decode.c +++ b/gdb/cli/cli-decode.c @@ -655,7 +655,7 @@ add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *va set_show_commands add_setshow_filename_cmd (const char *name, enum command_class theclass, - char **var, + std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, @@ -664,10 +664,10 @@ add_setshow_filename_cmd (const char *name, enum command_class theclass, struct cmd_list_element **show_list) { set_show_commands commands - = add_setshow_cmd_full<char *> (name, theclass, var_filename, var, - set_doc, show_doc, help_doc, - set_func, show_func, - set_list, show_list); + = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var, + set_doc, show_doc, help_doc, + set_func, show_func, + set_list, show_list); set_cmd_completer (commands.set, filename_completer); @@ -679,7 +679,7 @@ add_setshow_filename_cmd (const char *name, enum command_class theclass, set_show_commands add_setshow_string_cmd (const char *name, enum command_class theclass, - char **var, + std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, @@ -688,10 +688,10 @@ add_setshow_string_cmd (const char *name, enum command_class theclass, struct cmd_list_element **show_list) { set_show_commands commands - = add_setshow_cmd_full<char *> (name, theclass, var_string, var, - set_doc, show_doc, help_doc, - set_func, show_func, - set_list, show_list); + = add_setshow_cmd_full<std::string> (name, theclass, var_string, var, + set_doc, show_doc, help_doc, + set_func, show_func, + set_list, show_list); /* Disable the default symbol completer. */ set_cmd_completer (commands.set, nullptr); @@ -704,7 +704,7 @@ add_setshow_string_cmd (const char *name, enum command_class theclass, set_show_commands add_setshow_string_noescape_cmd (const char *name, enum command_class theclass, - char **var, + std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, @@ -713,9 +713,10 @@ add_setshow_string_noescape_cmd (const char *name, enum command_class theclass, struct cmd_list_element **show_list) { set_show_commands commands - = add_setshow_cmd_full<char *> (name, theclass, var_string_noescape, var, - set_doc, show_doc, help_doc, set_func, - show_func, set_list, show_list); + = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape, + var, set_doc, show_doc, help_doc, + set_func, show_func, set_list, + show_list); /* Disable the default symbol completer. */ set_cmd_completer (commands.set, nullptr); @@ -728,7 +729,7 @@ add_setshow_string_noescape_cmd (const char *name, enum command_class theclass, set_show_commands add_setshow_optional_filename_cmd (const char *name, enum command_class theclass, - char **var, + std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, @@ -737,9 +738,10 @@ add_setshow_optional_filename_cmd (const char *name, enum command_class theclass struct cmd_list_element **show_list) { set_show_commands commands - = add_setshow_cmd_full<char *> (name, theclass, var_optional_filename, - var, set_doc, show_doc, help_doc, - set_func, show_func, set_list, show_list); + = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename, + var, set_doc, show_doc, help_doc, + set_func, show_func, set_list, + show_list); set_cmd_completer (commands.set, filename_completer); diff --git a/gdb/cli/cli-logging.c b/gdb/cli/cli-logging.c index dfedd75..c909352 100644 --- a/gdb/cli/cli-logging.c +++ b/gdb/cli/cli-logging.c @@ -25,7 +25,7 @@ static char *saved_filename; -static char *logging_filename; +static std::string logging_filename = "gdb.txt"; static void show_logging_filename (struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value) @@ -102,7 +102,7 @@ handle_redirections (int from_tty) } stdio_file_up log (new no_terminal_escape_file ()); - if (!log->open (logging_filename, logging_overwrite ? "w" : "a")) + if (!log->open (logging_filename.c_str (), logging_overwrite ? "w" : "a")) perror_with_name (_("set logging")); /* Redirects everything to gdb_stdout while this is running. */ @@ -110,20 +110,20 @@ handle_redirections (int from_tty) { if (!logging_redirect) fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n", - logging_filename); + logging_filename.c_str ()); else fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n", - logging_filename); + logging_filename.c_str ()); if (!debug_redirect) fprintf_unfiltered (gdb_stdout, "Copying debug output to %s.\n", - logging_filename); + logging_filename.c_str ()); else fprintf_unfiltered (gdb_stdout, "Redirecting debug output to %s.\n", - logging_filename); + logging_filename.c_str ()); } - saved_filename = xstrdup (logging_filename); + saved_filename = xstrdup (logging_filename.c_str ()); /* Let the interpreter do anything it needs. */ current_interp_set_logging (std::move (log), logging_redirect, @@ -145,10 +145,8 @@ set_logging_on (const char *args, int from_tty) const char *rest = args; if (rest && *rest) - { - xfree (logging_filename); - logging_filename = xstrdup (rest); - } + logging_filename = rest; + handle_redirections (from_tty); } @@ -201,6 +199,7 @@ If debug redirect is on, debug will go only to the log file."), set_logging_redirect, show_logging_redirect, &set_logging_cmdlist, &show_logging_cmdlist); + add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\ Set the current logfile."), _("\ Show the current logfile."), _("\ @@ -212,6 +211,4 @@ The logfile is used when directing GDB's output."), _("Enable logging."), &set_logging_cmdlist); add_cmd ("off", class_support, set_logging_off, _("Disable logging."), &set_logging_cmdlist); - - logging_filename = xstrdup ("gdb.txt"); } diff --git a/gdb/cli/cli-option.c b/gdb/cli/cli-option.c index ab76f19..846b819 100644 --- a/gdb/cli/cli-option.c +++ b/gdb/cli/cli-option.c @@ -45,7 +45,7 @@ union option_value const char *enumeration; /* For var_string options. This is malloc-allocated. */ - char *string; + std::string *string; }; /* Holds an options definition and its value. */ @@ -87,7 +87,7 @@ struct option_def_and_value if (value.has_value ()) { if (option.type == var_string) - xfree (value->string); + delete value->string; } } @@ -439,7 +439,7 @@ parse_option (gdb::array_view<const option_def_group> options_group, error (_("-%s requires an argument"), match->name); option_value val; - val.string = xstrdup (str.c_str ()); + val.string = new std::string (std::move (str)); return option_def_and_value {*match, match_ctx, val}; } @@ -603,8 +603,7 @@ save_option_value_in_ctx (gdb::optional<option_def_and_value> &ov) break; case var_string: *ov->option.var_address.string (ov->option, ov->ctx) - = ov->value->string; - ov->value->string = nullptr; + = std::move (*ov->value->string); break; default: gdb_assert_not_reached ("unhandled option type"); diff --git a/gdb/cli/cli-option.h b/gdb/cli/cli-option.h index aa2ccbe..b7ede45 100644 --- a/gdb/cli/cli-option.h +++ b/gdb/cli/cli-option.h @@ -86,7 +86,7 @@ public: unsigned int *(*uinteger) (const option_def &, void *ctx); int *(*integer) (const option_def &, void *ctx); const char **(*enumeration) (const option_def &, void *ctx); - char **(*string) (const option_def &, void *ctx); + std::string *(*string) (const option_def &, void *ctx); } var_address; @@ -268,7 +268,7 @@ template<typename Context> struct string_option_def : option_def { string_option_def (const char *long_option_, - char **(*get_var_address_cb_) (Context *), + std::string *(*get_var_address_cb_) (Context *), show_value_ftype *show_cmd_cb_, const char *set_doc_, const char *show_doc_ = nullptr, diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c index 86ab553..8d29c0c 100644 --- a/gdb/cli/cli-setshow.c +++ b/gdb/cli/cli-setshow.c @@ -360,27 +360,22 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c) *q++ = '\0'; newobj = (char *) xrealloc (newobj, q - newobj); - char * const var = c->var->get<char *> (); - if (var == nullptr - || strcmp (var, newobj) != 0) + const std::string &cur_val = c->var->get<std::string> (); + if (strcmp (cur_val.c_str(), newobj) != 0) { - xfree (var); - c->var->set<char *> (newobj); + c->var->set<std::string> (std::string (newobj)); option_changed = true; } - else - xfree (newobj); + xfree (newobj); } break; case var_string_noescape: { - char * const var = c->var->get<char *> (); - if (var == nullptr - || strcmp (var, arg) != 0) + const std::string &cur_val = c->var->get<std::string> (); + if (strcmp (cur_val.c_str (), arg) != 0) { - xfree (var); - c->var->set<char *> (xstrdup (arg)); + c->var->set<std::string> (std::string (arg)); option_changed = true; } @@ -410,17 +405,14 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c) else val = xstrdup (""); - char * const var = c->var->get<char *> (); - if (var == nullptr - || strcmp (var, val) != 0) + const std::string &cur_val = c->var->get<std::string> (); + if (strcmp (cur_val.c_str (), val) != 0) { - xfree (var); - c->var->set<char *> (val); + c->var->set<std::string> (std::string (val)); option_changed = true; } - else - xfree (val); + xfree (val); } break; case var_boolean: @@ -598,15 +590,12 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c) case var_string_noescape: case var_filename: case var_optional_filename: + gdb::observers::command_param_changed.notify + (name, c->var->get<std::string> ().c_str ()); + break; case var_enum: - { - const char *var; - if (c->var->type () == var_enum) - var = c->var->get<const char *> (); - else - var = c->var->get<char *> (); - gdb::observers::command_param_changed.notify (name, var); - } + gdb::observers::command_param_changed.notify + (name, c->var->get<const char *> ()); break; case var_boolean: { @@ -658,23 +647,19 @@ get_setshow_command_value_string (const setting &var) { case var_string: { - char *value = var.get<char *> (); - - if (value != nullptr) - stb.putstr (value, '"'); + std::string value = var.get<std::string> (); + if (!value.empty ()) + stb.putstr (value.c_str (), '"'); } break; case var_string_noescape: case var_optional_filename: case var_filename: + stb.puts (var.get<std::string> ().c_str ()); + break; case var_enum: { - const char *value; - if (var.type () == var_enum) - value = var.get<const char *> (); - else - value = var.get<char *> (); - + const char *value = var.get<const char *> (); if (value != nullptr) stb.puts (value); } |