From 550194db388374be9aa8ca3a998523807c8080ae Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 9 Jun 2023 10:27:18 -0600 Subject: [PATCH] Use std::string for MI token This changes the MI "token" to be a std::string, removing some manual memory management. It also makes current_token 'const' in order to support this change. Reviewed-by: Keith Seitz --- gdb/mi/mi-main.c | 21 ++++++++++----------- gdb/mi/mi-main.h | 2 +- gdb/mi/mi-parse.c | 9 +++------ gdb/mi/mi-parse.h | 11 +++++------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 7503ffd2f18..7d671657a44 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -78,7 +78,7 @@ static struct mi_timestamp *current_command_ts; static int do_timings = 0; -char *current_token; +const char *current_token; /* Few commands would like to know if options like --thread-group were explicitly specified. This variable keeps the current parsed command including all option, and make it possible. */ @@ -1810,7 +1810,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) current_command_ts = context->cmd_start; scoped_restore save_token = make_scoped_restore (¤t_token, - context->token); + context->token.c_str ()); mi->running_result_record_printed = 0; mi->mi_proceeded = 0; @@ -1821,7 +1821,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) if (mi_debug_p) gdb_printf (gdb_stdlog, " token=`%s' command=`%s' args=`%s'\n", - context->token, context->command, context->args ()); + context->token.c_str (), context->command, context->args ()); mi_cmd_execute (context); @@ -1833,7 +1833,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) uiout will most likely crash in the mi_out_* routines. */ if (!mi->running_result_record_printed) { - gdb_puts (context->token, mi->raw_stdout); + gdb_puts (context->token.c_str (), mi->raw_stdout); /* There's no particularly good reason why target-connect results in not ^done. Should kill ^connected for MI3. */ gdb_puts (strcmp (context->command, "target-select") == 0 @@ -1872,7 +1872,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) { if (!mi->running_result_record_printed) { - gdb_puts (context->token, mi->raw_stdout); + gdb_puts (context->token.c_str (), mi->raw_stdout); gdb_puts ("^done", mi->raw_stdout); mi_out_put (uiout, mi->raw_stdout); mi_out_rewind (uiout); @@ -1915,7 +1915,7 @@ mi_print_exception (const char *token, const struct gdb_exception &exception) void mi_execute_command (const char *cmd, int from_tty) { - char *token; + std::string token; std::unique_ptr command; /* This is to handle EOF (^D). We just quit gdb. */ @@ -1931,13 +1931,12 @@ mi_execute_command (const char *cmd, int from_tty) } catch (const gdb_exception &exception) { - mi_print_exception (token, exception); - xfree (token); + mi_print_exception (token.c_str (), exception); } if (command != NULL) { - command->token = token; + command->token = std::move (token); if (do_timings) { @@ -1960,7 +1959,7 @@ mi_execute_command (const char *cmd, int from_tty) /* The command execution failed and error() was called somewhere. */ - mi_print_exception (command->token, result); + mi_print_exception (command->token.c_str (), result); mi_out_rewind (current_uiout); /* Throw to a higher level catch for SIGTERM sent to GDB. */ @@ -1982,7 +1981,7 @@ mi_execute_command (mi_parse *context) error (_("Command is not an MI command")); scoped_restore save_token = make_scoped_restore (¤t_token, - context->token); + context->token.c_str ()); scoped_restore save_debug = make_scoped_restore (&mi_debug_p, 0); mi_cmd_execute (context); diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h index 1741f48b3ee..cb17921c150 100644 --- a/gdb/mi/mi-main.h +++ b/gdb/mi/mi-main.h @@ -34,7 +34,7 @@ extern void mi_print_timing_maybe (struct ui_file *file); extern int mi_async_p (void); -extern char *current_token; +extern const char *current_token; struct mi_suppress_notification { diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index a113d4d48da..aceecad6db6 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -216,7 +216,6 @@ mi_parse::parse_argv () mi_parse::~mi_parse () { xfree (command); - xfree (token); freeargv (argv); } @@ -290,7 +289,7 @@ mi_parse::set_language (const char *arg, const char **endp) } std::unique_ptr -mi_parse::make (const char *cmd, char **token) +mi_parse::make (const char *cmd, std::string *token) { const char *chp; @@ -302,9 +301,7 @@ mi_parse::make (const char *cmd, char **token) /* Find/skip any token and then extract it. */ for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++) ; - *token = (char *) xmalloc (chp - cmd + 1); - memcpy (*token, cmd, (chp - cmd)); - (*token)[chp - cmd] = '\0'; + *token = std::string (cmd, chp - cmd); /* This wasn't a real MI command. Return it as a CLI_COMMAND. */ if (*chp != '-') @@ -422,7 +419,7 @@ mi_parse::make (gdb::unique_xmalloc_ptr command, std::unique_ptr parse (new struct mi_parse); parse->command = command.release (); - parse->token = xstrdup (""); + parse->token = ""; if (parse->command[0] != '-') throw_error (UNDEFINED_COMMAND_ERROR, diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 6373543529b..78fb414b4f0 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -44,13 +44,12 @@ struct mi_parse /* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is invalid, an exception is thrown. For an MI_COMMAND COMMAND, ARGS and OP are initialized. Un-initialized fields are zero. *TOKEN is - set to the token, even if an exception is thrown. It is allocated - with xmalloc; it must either be freed with xfree, or assigned to - the TOKEN field of the resultant mi_parse object, to be freed by - mi_parse_free. */ + set to the token, even if an exception is thrown. It can be + assigned to the TOKEN field of the resultant mi_parse object, + to be freed by mi_parse_free. */ static std::unique_ptr make (const char *cmd, - char **token); + std::string *token); /* Create an mi_parse object given the command name and a vector of arguments. Unlike with the other constructor, here the @@ -74,7 +73,7 @@ struct mi_parse enum mi_command_type op = MI_COMMAND; char *command = nullptr; - char *token = nullptr; + std::string token; const struct mi_command *cmd = nullptr; struct mi_timestamp *cmd_start = nullptr; char **argv = nullptr; -- 2.30.2