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

Message ID 20230906150201.83216-1-simon.marchi@efficios.com
State New
Headers
Series [v2] gdb/mi: make current_token a field of mi_interp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Simon Marchi Sept. 6, 2023, 3:02 p.m. UTC
  This is v2 of this single patch:

  https://inbox.sourceware.org/gdb-patches/20230502205011.132151-3-simon.marchi@efficios.com/

I did not push this patch as part of the original series, because there
were non-trivial conflicts with master a the time, and the patch wasn't
really necessary for the rest of the series.

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
---
 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(-)


base-commit: e061219f5d600af5b33418553f192e0cb9fc9ca9
  

Comments

Tom Tromey Sept. 7, 2023, 2:07 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> Following the commit f818c32ba459 ("gdb/mi: fix ^running record with
Simon> multiple MI interpreters"), I thought it would make sense to make
Simon> current_token a field of mi_interp.

Looks good to me.
Approved-By: Tom Tromey <tom@tromey.com>

I wonder if the token is redundant with the current_context and could
just be extracted from there.

Tom
  
Simon Marchi Sept. 7, 2023, 2:41 p.m. UTC | #2
On 9/7/23 10:07, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> Following the commit f818c32ba459 ("gdb/mi: fix ^running record with
> Simon> multiple MI interpreters"), I thought it would make sense to make
> Simon> current_token a field of mi_interp.
> 
> Looks good to me.
> Approved-By: Tom Tromey <tom@tromey.com>
> 
> I wonder if the token is redundant with the current_context and could
> just be extracted from there.

Hmm maybe, I'll push this patch and look if additional simplifications
can be done.

Simon
  

Patch

diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 1eff4c286ea3..b3b0f5bb1f51 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 bc975d75ad81..f9af61f0a571 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -102,6 +102,8 @@  class mi_interp final : public interp
   /* 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 7a8a9ce94c29..8ea0d35f18a4 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 b35544baef10..44f77022bdd9 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?  */