public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/2] Use std::string for MI token
Date: Fri, 09 Jun 2023 10:46:25 -0600	[thread overview]
Message-ID: <20230609-mi-xfree-v1-1-04e9c8d9a679@adacore.com> (raw)
In-Reply-To: <20230609-mi-xfree-v1-0-04e9c8d9a679@adacore.com>

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.
---
 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 (&current_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<struct mi_parse> 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 (&current_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<struct mi_parse>
-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<char> command,
   std::unique_ptr<struct mi_parse> 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<struct mi_parse> 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.40.1


  reply	other threads:[~2023-06-09 16:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09 16:46 [PATCH 0/2] Remove some use of xfree from MI Tom Tromey
2023-06-09 16:46 ` Tom Tromey [this message]
2023-06-09 16:46 ` [PATCH 2/2] Use unique_xmalloc_ptr for mi_parse::command Tom Tromey
2023-06-16 18:03 ` [PATCH 0/2] Remove some use of xfree from MI Keith Seitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230609-mi-xfree-v1-1-04e9c8d9a679@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).