[PATCH v3 18/34] Make raw_stdout be per MI instance

Pedro Alves
2016-05-06T12:52:59Z

Each MI instance should obviously have its own raw output channel,
along with save_raw_stdout.

yyyy-mm-dd Pedro Alves <palves@redhat.com>

* interps.c (current_interpreter): New function.
* interps.h (current_interpreter): New declaration.
* mi/mi-cmds.h (raw_stdout): Delete declaration.
* mi/mi-common.h (struct mi_interp) <raw_stdout,
saved_raw_stdout>: New field.
* mi/mi-interp.c (display_mi_prompt): New parameter 'mi'. Adjust
to per-UI raw_stdout.
(mi_interpreter_init): Adjust to per-UI raw_stdout.
(mi_on_sync_execution_done, mi_execute_command_input_handler)
(mi_command_loop): Pass MI instance to display_mi_prompt.
(mi_on_normal_stop_1, mi_output_running_pid, mi_on_resume_1)
(mi_on_resume): Adjust to per-UI raw_stdout.
(saved_raw_stdout): Delete.
(mi_set_logging): Adjust to per-UI raw_stdout and
saved_raw_stdout.
* mi/mi-main.c (raw_stdout): Delete.
(mi_cmd_gdb_exit, captured_mi_execute_command)
(mi_print_exception, mi_load_progress): Adjust to per-UI
raw_stdout.
(print_diff_now, mi_print_timing_maybe): New ui_file parameter.
Pass it along.
(print_diff): New ui_file parameter. Send output there instead of
raw_stdout.
* mi/mi-main.h (struct ui_file): Forward declare.
(mi_print_timing_maybe): Add ui_file parameter.
---
gdb/interps.c | 10 ++++++
gdb/interps.h | 3 ++
gdb/mi/mi-cmds.h | 3 --
gdb/mi/mi-common.h | 7 ++++
gdb/mi/mi-interp.c | 95 +++++++++++++++++++++++++-----------------------------
gdb/mi/mi-main.c | 93 ++++++++++++++++++++++++++++------------------------
gdb/mi/mi-main.h | 4 ++-
7 files changed, 118 insertions(+), 97 deletions(-)

diff --git a/gdb/interps.c b/gdb/interps.c
index 536630a..36e0422 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -606,6 +606,16 @@ top_level_interpreter_data (void)
return interp->data;
}

+/* See interps.h. */
+
+struct interp *
+current_interpreter (void)
+{
+ struct ui_interp_info *ui_interp = get_current_interp_info ();
+
+ return ui_interp->current_interpreter;
+}
+
/* This just adds the "interpreter-exec" command. */
void
_initialize_interpreter (void)
diff --git a/gdb/interps.h b/gdb/interps.h
index 4922544..45f9bf2 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -117,6 +117,9 @@ extern int current_interp_set_logging (int start_log, struct ui_file *out,
extern void *top_level_interpreter_data (void);
extern struct interp *top_level_interpreter (void);

+/* Return the current UI's current interpreter. */
+extern struct interp *current_interpreter (void);
+
extern struct interp *command_interp (void);

extern void clear_interpreter_hooks (void);
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index c3bc777..69472a9 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -157,9 +157,6 @@ extern struct mi_cmd *mi_lookup (const char *command);
/* Debug flag */
extern int mi_debug_p;

-/* Raw console output - FIXME: should this be a parameter? */
-extern struct ui_file *raw_stdout;
-
extern void mi_execute_command (const char *cmd, int from_tty);

#endif
diff --git a/gdb/mi/mi-common.h b/gdb/mi/mi-common.h
index 7bb706c..c7e4855 100644
--- a/gdb/mi/mi-common.h
+++ b/gdb/mi/mi-common.h
@@ -57,6 +57,13 @@ struct mi_interp
struct ui_file *targ;
struct ui_file *event_channel;

+ /* Raw console output. */
+ struct ui_file *raw_stdout;
+
+ /* Save the original value of raw_stdout here when logging, so we
+ can restore correctly when done. */
+ struct ui_file *saved_raw_stdout;
+
/* MI's builder. */
struct ui_out *mi_uiout;

diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index b4d498d..d4c89f7 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -91,10 +91,10 @@ static int report_initial_inferior (struct inferior *inf, void *closure);
/* Display the MI prompt. */

static void
-display_mi_prompt (void)
+display_mi_prompt (struct mi_interp *mi)
{
- fputs_unfiltered ("(gdb) \n", raw_stdout);
- gdb_flush (raw_stdout);
+ fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
+ gdb_flush (mi->raw_stdout);
}

/* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
@@ -115,20 +115,18 @@ mi_interpreter_init (struct interp *interp, int top_level)
const char *name;
int mi_version;

- /* Assign the output channel created at startup to its own global,
- so that we can create a console channel that encapsulates and
- prefixes all gdb_output-type bits coming from the rest of the
- debugger. */
-
- raw_stdout = gdb_stdout;
+ /* Store the current output channel, so that we can create a console
+ channel that encapsulates and prefixes all gdb_output-type bits
+ coming from the rest of the debugger. */
+ mi->raw_stdout = gdb_stdout;

/* Create MI console channels, each with a different prefix so they
can be distinguished. */
- mi->out = mi_console_file_new (raw_stdout, "~", '"');
- mi->err = mi_console_file_new (raw_stdout, "&", '"');
+ mi->out = mi_console_file_new (mi->raw_stdout, "~", '"');
+ mi->err = mi_console_file_new (mi->raw_stdout, "&", '"');
mi->log = mi->err;
- mi->targ = mi_console_file_new (raw_stdout, "@", '"');
- mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
+ mi->targ = mi_console_file_new (mi->raw_stdout, "@", '"');
+ mi->event_channel = mi_console_file_new (mi->raw_stdout, "=", 0);

name = interp_name (interp);
/* INTERP_MI selects the most recent released version. "mi2" was
@@ -308,7 +306,7 @@ mi_on_sync_execution_done (void)
/* If MI is sync, then output the MI prompt now, indicating we're
ready for further input. */
if (!mi_async_p ())
- display_mi_prompt ();
+ display_mi_prompt (mi);
}

/* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
@@ -316,6 +314,8 @@ mi_on_sync_execution_done (void)
static void
mi_execute_command_input_handler (char *cmd)
{
+ struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
+
mi_execute_command_wrapper (cmd);

/* Print a prompt, indicating we're ready for further input, unless
@@ -324,18 +324,20 @@ mi_execute_command_input_handler (char *cmd)
'synchronous_command_done' observer when the target next
stops. */
if (!sync_execution)
- display_mi_prompt ();
+ display_mi_prompt (mi);
}

static void
mi_command_loop (void *data)
{
+ struct mi_interp *mi = (struct mi_interp *) data;
+
/* Turn off 8 bit strings in quoted output. Any character with the
high bit set is printed using C's octal format. */
sevenbit_strings = 1;

/* Tell the world that we're alive. */
- display_mi_prompt ();
+ display_mi_prompt (mi);

start_event_loop ();
}
@@ -658,6 +660,7 @@ mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
using cli interpreter, be sure to use MI uiout for output,
not the current one. */
struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
+ struct mi_interp *mi = (struct mi_interp *) top_level_interpreter_data ();

if (print_frame)
{
@@ -699,12 +702,7 @@ mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
&& thread_fsm_finished_p (tp->thread_fsm))
|| (tp->control.command_interp != NULL
&& tp->control.command_interp != top_level_interpreter ()))
- {
- struct mi_interp *mi
- = (struct mi_interp *) top_level_interpreter_data ();
-
- print_stop_event (mi->cli_uiout);
- }
+ print_stop_event (mi->cli_uiout);

tp = inferior_thread ();
ui_out_field_int (mi_uiout, "thread-id", tp->global_num);
@@ -724,12 +722,12 @@ mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
ui_out_field_int (mi_uiout, "core", core);
}

- fputs_unfiltered ("*stopped", raw_stdout);
- mi_out_put (mi_uiout, raw_stdout);
+ fputs_unfiltered ("*stopped", mi->raw_stdout);
+ mi_out_put (mi_uiout, mi->raw_stdout);
mi_out_rewind (mi_uiout);
- mi_print_timing_maybe ();
- fputs_unfiltered ("\n", raw_stdout);
- gdb_flush (raw_stdout);
+ mi_print_timing_maybe (mi->raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
+ gdb_flush (mi->raw_stdout);
}

static void
@@ -1055,7 +1053,7 @@ mi_output_running_pid (struct thread_info *info, void *arg)
continue;

if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
- fprintf_unfiltered (raw_stdout,
+ fprintf_unfiltered (mi->raw_stdout,
"*running,thread-id=\"%d\"\n",
info->global_num);
}
@@ -1076,7 +1074,7 @@ mi_inferior_count (struct inferior *inf, void *arg)
}

static void
-mi_on_resume_1 (ptid_t ptid)
+mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
{
/* To cater for older frontends, emit ^running, but do it only once
per each command. We do it here, since at this point we know
@@ -1088,12 +1086,12 @@ mi_on_resume_1 (ptid_t ptid)
In future (MI3), we'll be outputting "^done" here. */
if (!running_result_record_printed && mi_proceeded)
{
- fprintf_unfiltered (raw_stdout, "%s^running\n",
+ fprintf_unfiltered (mi->raw_stdout, "%s^running\n",
current_token ? current_token : "");
}

if (ptid_get_pid (ptid) == -1)
- fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
+ fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
else if (ptid_is_pid (ptid))
{
int count = 0;
@@ -1104,7 +1102,7 @@ mi_on_resume_1 (ptid_t ptid)
iterate_over_inferiors (mi_inferior_count, &count);

if (count == 1)
- fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
+ fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
else
iterate_over_threads (mi_output_running_pid, &ptid);
}
@@ -1113,7 +1111,7 @@ mi_on_resume_1 (ptid_t ptid)
struct thread_info *ti = find_thread_ptid (ptid);

gdb_assert (ti);
- fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n",
+ fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"%d\"\n",
ti->global_num);
}

@@ -1126,9 +1124,9 @@ mi_on_resume_1 (ptid_t ptid)
checked here because we only need to emit a prompt if a
synchronous command was issued when the target is async. */
if (!target_is_async_p () || sync_execution)
- fputs_unfiltered ("(gdb) \n", raw_stdout);
+ fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
}
- gdb_flush (raw_stdout);
+ gdb_flush (mi->raw_stdout);
}

static void
@@ -1157,7 +1155,7 @@ mi_on_resume (ptid_t ptid)
old_chain = make_cleanup_restore_target_terminal ();
target_terminal_ours_for_output ();

- mi_on_resume_1 (ptid);
+ mi_on_resume_1 (mi, ptid);

do_cleanups (old_chain);
}
@@ -1367,11 +1365,6 @@ mi_ui_out (struct interp *interp)
return mi->mi_uiout;
}

-/* Save the original value of raw_stdout here when logging, so we can
- restore correctly when done. */
-
-static struct ui_file *saved_raw_stdout;
-
/* Do MI-specific logging actions; save raw_stdout, and change all
the consoles to use the supplied ui-file(s). */

@@ -1394,23 +1387,23 @@ mi_set_logging (struct interp *interp, int start_log,
if (logfile)
{
ui_file_delete (out);
- out = tee_file_new (raw_stdout, 0, logfile, 0);
+ out = tee_file_new (mi->raw_stdout, 0, logfile, 0);
}

- saved_raw_stdout = raw_stdout;
- raw_stdout = out;
+ mi->saved_raw_stdout = mi->raw_stdout;
+ mi->raw_stdout = out;
}
else
{
- raw_stdout = saved_raw_stdout;
- saved_raw_stdout = NULL;
+ mi->raw_stdout = mi->saved_raw_stdout;
+ mi->saved_raw_stdout = NULL;
}

- mi_console_set_raw (mi->out, raw_stdout);
- mi_console_set_raw (mi->err, raw_stdout);
- mi_console_set_raw (mi->log, raw_stdout);
- mi_console_set_raw (mi->targ, raw_stdout);
- mi_console_set_raw (mi->event_channel, raw_stdout);
+ mi_console_set_raw (mi->out, mi->raw_stdout);
+ mi_console_set_raw (mi->err, mi->raw_stdout);
+ mi_console_set_raw (mi->log, mi->raw_stdout);
+ mi_console_set_raw (mi->targ, mi->raw_stdout);
+ mi_console_set_raw (mi->event_channel, mi->raw_stdout);

return 1;
}
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 7cb7bf5..a3b55a8746 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -72,8 +72,6 @@ enum

int mi_debug_p;

-struct ui_file *raw_stdout;
-
/* This is used to pass the current command timestamp down to
continuation routines. */
static struct mi_timestamp *current_command_ts;
@@ -149,18 +147,21 @@ mi_async_p (void)

static void timestamp (struct mi_timestamp *tv);

-static void print_diff_now (struct mi_timestamp *start);
-static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
+static void print_diff (struct ui_file *file, struct mi_timestamp *start,
+ struct mi_timestamp *end);

void
mi_cmd_gdb_exit (char *command, char **argv, int argc)
{
+ struct mi_interp *mi
+ = (struct mi_interp *) interp_data (current_interpreter ());
+
/* We have to print everything right here because we never return. */
if (current_token)
- fputs_unfiltered (current_token, raw_stdout);
- fputs_unfiltered ("^exit\n", raw_stdout);
- mi_out_put (current_uiout, raw_stdout);
- gdb_flush (raw_stdout);
+ fputs_unfiltered (current_token, mi->raw_stdout);
+ fputs_unfiltered ("^exit\n", mi->raw_stdout);
+ mi_out_put (current_uiout, mi->raw_stdout);
+ gdb_flush (mi->raw_stdout);
/* FIXME: The function called is not yet a formal libgdb function. */
quit_force (NULL, FROM_TTY);
}
@@ -1981,6 +1982,7 @@ mi_cmd_remove_inferior (char *command, char **argv, int argc)
static void
captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
{
+ struct mi_interp *mi = (struct mi_interp *) interp_data (command_interp ());
struct cleanup *cleanup;

if (do_timings)
@@ -1997,7 +1999,8 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
/* A MI command was read from the input stream. */
if (mi_debug_p)
/* FIXME: gdb_???? */
- fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
+ fprintf_unfiltered (mi->raw_stdout,
+ " token=`%s' command=`%s' args=`%s'\n",
context->token, context->command, context->args);

mi_cmd_execute (context);
@@ -2010,15 +2013,15 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
uiout will most likely crash in the mi_out_* routines. */
if (!running_result_record_printed)
{
- fputs_unfiltered (context->token, raw_stdout);
+ fputs_unfiltered (context->token, mi->raw_stdout);
/* There's no particularly good reason why target-connect results
in not ^done. Should kill ^connected for MI3. */
fputs_unfiltered (strcmp (context->command, "target-select") == 0
- ? "^connected" : "^done", raw_stdout);
- mi_out_put (uiout, raw_stdout);
+ ? "^connected" : "^done", mi->raw_stdout);
+ mi_out_put (uiout, mi->raw_stdout);
mi_out_rewind (uiout);
- mi_print_timing_maybe ();
- fputs_unfiltered ("\n", raw_stdout);
+ mi_print_timing_maybe (mi->raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
}
else
/* The command does not want anything to be printed. In that
@@ -2049,12 +2052,12 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
{
if (!running_result_record_printed)
{
- fputs_unfiltered (context->token, raw_stdout);
- fputs_unfiltered ("^done", raw_stdout);
- mi_out_put (uiout, raw_stdout);
+ fputs_unfiltered (context->token, mi->raw_stdout);
+ fputs_unfiltered ("^done", mi->raw_stdout);
+ mi_out_put (uiout, mi->raw_stdout);
mi_out_rewind (uiout);
- mi_print_timing_maybe ();
- fputs_unfiltered ("\n", raw_stdout);
+ mi_print_timing_maybe (mi->raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
}
else
mi_out_rewind (uiout);
@@ -2071,22 +2074,25 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
static void
mi_print_exception (const char *token, struct gdb_exception exception)
{
- fputs_unfiltered (token, raw_stdout);
- fputs_unfiltered ("^error,msg=\"", raw_stdout);
+ struct mi_interp *mi
+ = (struct mi_interp *) interp_data (current_interpreter ());
+
+ fputs_unfiltered (token, mi->raw_stdout);
+ fputs_unfiltered ("^error,msg=\"", mi->raw_stdout);
if (exception.message == NULL)
- fputs_unfiltered ("unknown error", raw_stdout);
+ fputs_unfiltered ("unknown error", mi->raw_stdout);
else
- fputstr_unfiltered (exception.message, '"', raw_stdout);
- fputs_unfiltered ("\"", raw_stdout);
+ fputstr_unfiltered (exception.message, '"', mi->raw_stdout);
+ fputs_unfiltered ("\"", mi->raw_stdout);

switch (exception.error)
{
case UNDEFINED_COMMAND_ERROR:
- fputs_unfiltered (",code=\"undefined-command\"", raw_stdout);
+ fputs_unfiltered (",code=\"undefined-command\"", mi->raw_stdout);
break;
}

- fputs_unfiltered ("\n", raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
}

void
@@ -2357,6 +2363,8 @@ mi_load_progress (const char *section_name,
int new_section;
struct ui_out *saved_uiout;
struct ui_out *uiout;
+ struct mi_interp *mi
+ = (struct mi_interp *) interp_data (current_interpreter ());

/* This function is called through deprecated_show_load_progress
which means uiout may not be correct. Fix it for the duration
@@ -2398,16 +2406,16 @@ mi_load_progress (const char *section_name,
previous_sect_name = xstrdup (section_name);

if (current_token)
- fputs_unfiltered (current_token, raw_stdout);
- fputs_unfiltered ("+download", raw_stdout);
+ fputs_unfiltered (current_token, mi->raw_stdout);
+ fputs_unfiltered ("+download", mi->raw_stdout);
cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
ui_out_field_string (uiout, "section", section_name);
ui_out_field_int (uiout, "section-size", total_section);
ui_out_field_int (uiout, "total-size", grand_total);
do_cleanups (cleanup_tuple);
- mi_out_put (uiout, raw_stdout);
- fputs_unfiltered ("\n", raw_stdout);
- gdb_flush (raw_stdout);
+ mi_out_put (uiout, mi->raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
+ gdb_flush (mi->raw_stdout);
}

if (delta.tv_sec >= update_threshold.tv_sec &&
@@ -2418,8 +2426,8 @@ mi_load_progress (const char *section_name,
last_update.tv_sec = time_now.tv_sec;
last_update.tv_usec = time_now.tv_usec;
if (current_token)
- fputs_unfiltered (current_token, raw_stdout);
- fputs_unfiltered ("+download", raw_stdout);
+ fputs_unfiltered (current_token, mi->raw_stdout);
+ fputs_unfiltered ("+download", mi->raw_stdout);
cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
ui_out_field_string (uiout, "section", section_name);
ui_out_field_int (uiout, "section-sent", sent_so_far);
@@ -2427,9 +2435,9 @@ mi_load_progress (const char *section_name,
ui_out_field_int (uiout, "total-sent", total_sent);
ui_out_field_int (uiout, "total-size", grand_total);
do_cleanups (cleanup_tuple);
- mi_out_put (uiout, raw_stdout);
- fputs_unfiltered ("\n", raw_stdout);
- gdb_flush (raw_stdout);
+ mi_out_put (uiout, mi->raw_stdout);
+ fputs_unfiltered ("\n", mi->raw_stdout);
+ gdb_flush (mi->raw_stdout);
}

xfree (uiout);
@@ -2459,21 +2467,21 @@ timestamp (struct mi_timestamp *tv)
}

static void
-print_diff_now (struct mi_timestamp *start)
+print_diff_now (struct ui_file *file, struct mi_timestamp *start)
{
struct mi_timestamp now;

timestamp (&now);
- print_diff (start, &now);
+ print_diff (file, start, &now);
}

void
-mi_print_timing_maybe (void)
+mi_print_timing_maybe (struct ui_file *file)
{
/* If the command is -enable-timing then do_timings may be true
whilst current_command_ts is not initialized. */
if (do_timings && current_command_ts)
- print_diff_now (current_command_ts);
+ print_diff_now (file, current_command_ts);
}

static long
@@ -2484,10 +2492,11 @@ timeval_diff (struct timeval start, struct timeval end)
}

static void
-print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
+print_diff (struct ui_file *file, struct mi_timestamp *start,
+ struct mi_timestamp *end)
{
fprintf_unfiltered
- (raw_stdout,
+ (file,
",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
timeval_diff (start->utime, end->utime) / 1000000.0,
diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h
index b2face1..18000cf 100644
--- a/gdb/mi/mi-main.h
+++ b/gdb/mi/mi-main.h
@@ -20,13 +20,15 @@
#ifndef MI_MAIN_H
#define MI_MAIN_H

+struct ui_file;
+
extern void mi_load_progress (const char *section_name,
unsigned long sent_so_far,
unsigned long total_section,
unsigned long total_sent,
unsigned long grand_total);

-extern void mi_print_timing_maybe (void);
+extern void mi_print_timing_maybe (struct ui_file *file);

/* Whether MI is in async mode. */

--
2.5.5