[binutils-gdb] gdb/mi: make current_token a field of mi_interp

Simon Marchi simark@sourceware.org
Thu Sep 7 14:43:01 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=def2803789208a617c429b5dcf2026decb25ce0c

commit def2803789208a617c429b5dcf2026decb25ce0c
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Wed Sep 6 11:02:00 2023 -0400

    gdb/mi: make current_token a field of mi_interp
    
    Following the commit f818c32ba459 ("gdb/mi: fix ^running record with
    multiple MI interpreters"), I thought it would make sense to make
    current_token a field of mi_interp.  This variable contains the token of
    the currently handled MI command, like the 222 in:
    
        222-exec-continue
    
    I didn't find any bug related to that, it's just a "that seems nicer"
    cleanup, since the current token is a fundamentally per-interp thing.
    
    mi_execute_command needs a check similar to what we already have in
    mi_cmd_gdb_exit: when invoked from Python's gdb.execute_mi, the current
    interpreter is not an mi_interp.  When using the Python gdb.execute_mi
    function, there is no such concept of token, so we can just skip that.
    
    There should be no user-visible change.
    
    Change-Id: Ib52b3c0cba4b7c9d805b432c809692a86e4945ad
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/mi/mi-interp.c |  2 +-
 gdb/mi/mi-interp.h |  2 ++
 gdb/mi/mi-main.c   | 27 ++++++++++++++++-----------
 gdb/mi/mi-main.h   |  2 --
 4 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 1eff4c286ea..b3b0f5bb1f5 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -675,7 +675,7 @@ mi_on_resume_1 (struct mi_interp *mi,
   if (!mi->running_result_record_printed && mi->mi_proceeded)
     {
       gdb_printf (mi->raw_stdout, "%s^running\n",
-		  current_token ? current_token : "");
+		  mi->current_token ? mi->current_token : "");
     }
 
   /* Backwards compatibility.  If doing a wildcard resume and there's
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index bc975d75ad8..f9af61f0a57 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -102,6 +102,8 @@ public:
   /* Flag indicating that the target has proceeded since the last
      command was issued.  */
   int mi_proceeded;
+
+  const char *current_token;
 };
 
 /* Output the shared object attributes to UIOUT.  */
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 7a8a9ce94c2..8ea0d35f18a 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -78,7 +78,6 @@ static struct mi_timestamp *current_command_ts;
 
 static int do_timings = 0;
 
-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.  */
@@ -153,8 +152,8 @@ mi_cmd_gdb_exit (const char *command, const char *const *argv, int argc)
   if (mi != nullptr)
     {
       /* We have to print everything right here because we never return.  */
-      if (current_token)
-	gdb_puts (current_token, mi->raw_stdout);
+      if (mi->current_token)
+	gdb_puts (mi->current_token, mi->raw_stdout);
       gdb_puts ("^exit\n", mi->raw_stdout);
       mi_out_put (current_uiout, mi->raw_stdout);
       gdb_flush (mi->raw_stdout);
@@ -1814,8 +1813,8 @@ captured_mi_execute_command (struct mi_interp *mi, struct ui_out *uiout,
   if (do_timings)
     current_command_ts = context->cmd_start;
 
-  scoped_restore save_token = make_scoped_restore (&current_token,
-						   context->token.c_str ());
+  scoped_restore save_token
+    = make_scoped_restore (&mi->current_token, context->token.c_str ());
 
   mi->running_result_record_printed = 0;
   mi->mi_proceeded = 0;
@@ -1987,8 +1986,14 @@ mi_execute_command (mi_parse *context)
   if (context->op != MI_COMMAND)
     error (_("Command is not an MI command"));
 
-  scoped_restore save_token = make_scoped_restore (&current_token,
-						   context->token.c_str ());
+  mi_interp *mi = as_mi_interp (current_interpreter ());
+
+  /* The current interpreter may not be MI, for instance when using
+     the Python gdb.execute_mi function.  */
+  if (mi != nullptr)
+    scoped_restore save_token = make_scoped_restore (&mi->current_token,
+						     context->token.c_str ());
+
   scoped_restore save_debug = make_scoped_restore (&mi_debug_p, 0);
 
   mi_cmd_execute (context);
@@ -2220,8 +2225,8 @@ mi_load_progress (const char *section_name,
       xfree (previous_sect_name);
       previous_sect_name = xstrdup (section_name);
 
-      if (current_token)
-	gdb_puts (current_token, mi->raw_stdout);
+      if (mi->current_token)
+	gdb_puts (mi->current_token, mi->raw_stdout);
       gdb_puts ("+download", mi->raw_stdout);
       {
 	ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
@@ -2238,8 +2243,8 @@ mi_load_progress (const char *section_name,
   if (time_now - last_update > milliseconds (500))
     {
       last_update = time_now;
-      if (current_token)
-	gdb_puts (current_token, mi->raw_stdout);
+      if (mi->current_token)
+	gdb_puts (mi->current_token, mi->raw_stdout);
       gdb_puts ("+download", mi->raw_stdout);
       {
 	ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h
index b35544baef1..44f77022bdd 100644
--- a/gdb/mi/mi-main.h
+++ b/gdb/mi/mi-main.h
@@ -34,8 +34,6 @@ extern void mi_print_timing_maybe (struct ui_file *file);
 
 extern int mi_async_p (void);
 
-extern const char *current_token;
-
 struct mi_suppress_notification
 {
   /* Breakpoint notification suppressed?  */


More information about the Gdb-cvs mailing list