1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright (C) 2002-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "mi-interp.h"
25 #include "event-top.h"
26 #include "gdbsupport/event-loop.h"
34 #include "mi-console.h"
35 #include "mi-common.h"
36 #include "observable.h"
37 #include "gdbthread.h"
40 #include "tracepoint.h"
42 #include "thread-fsm.h"
43 #include "cli/cli-interp.h"
44 #include "gdbsupport/scope-exit.h"
46 /* These are the interpreter setup, etc. functions for the MI
49 static void mi_execute_command_wrapper (const char *cmd
);
50 static void mi_execute_command_input_handler
51 (gdb::unique_xmalloc_ptr
<char> &&cmd
);
53 /* These are hooks that we put in place while doing interpreter_exec
54 so we can report interesting things that happened "behind the MI's
55 back" in this command. */
57 static int mi_interp_query_hook (const char *ctlstr
, va_list ap
)
58 ATTRIBUTE_PRINTF (1, 0);
60 static void mi_insert_notify_hooks (void);
61 static void mi_remove_notify_hooks (void);
63 static void mi_on_signal_exited (enum gdb_signal siggnal
);
64 static void mi_on_exited (int exitstatus
);
65 static void mi_on_normal_stop (struct bpstat
*bs
, int print_frame
);
66 static void mi_on_no_history (void);
68 static void mi_new_thread (struct thread_info
*t
);
69 static void mi_thread_exit (struct thread_info
*t
, int silent
);
70 static void mi_record_changed (struct inferior
*, int, const char *,
72 static void mi_inferior_added (struct inferior
*inf
);
73 static void mi_inferior_appeared (struct inferior
*inf
);
74 static void mi_inferior_exit (struct inferior
*inf
);
75 static void mi_inferior_removed (struct inferior
*inf
);
76 static void mi_on_resume (ptid_t ptid
);
77 static void mi_solib_loaded (struct so_list
*solib
);
78 static void mi_solib_unloaded (struct so_list
*solib
);
79 static void mi_about_to_proceed (void);
80 static void mi_traceframe_changed (int tfnum
, int tpnum
);
81 static void mi_tsv_created (const struct trace_state_variable
*tsv
);
82 static void mi_tsv_deleted (const struct trace_state_variable
*tsv
);
83 static void mi_tsv_modified (const struct trace_state_variable
*tsv
);
84 static void mi_breakpoint_created (struct breakpoint
*b
);
85 static void mi_breakpoint_deleted (struct breakpoint
*b
);
86 static void mi_breakpoint_modified (struct breakpoint
*b
);
87 static void mi_command_param_changed (const char *param
, const char *value
);
88 static void mi_memory_changed (struct inferior
*inf
, CORE_ADDR memaddr
,
89 ssize_t len
, const bfd_byte
*myaddr
);
90 static void mi_on_sync_execution_done (void);
92 /* Display the MI prompt. */
95 display_mi_prompt (struct mi_interp
*mi
)
97 struct ui
*ui
= current_ui
;
99 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
100 gdb_flush (mi
->raw_stdout
);
101 ui
->prompt_state
= PROMPTED
;
104 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
105 returns NULL otherwise. */
107 static struct mi_interp
*
108 as_mi_interp (struct interp
*interp
)
110 return dynamic_cast<mi_interp
*> (interp
);
113 /* Observer for the command_error notification. */
116 mi_on_command_error ()
118 mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
120 display_mi_prompt (mi
);
124 mi_interp::init (bool top_level
)
126 mi_interp
*mi
= this;
128 /* Store the current output channel, so that we can create a console
129 channel that encapsulates and prefixes all gdb_output-type bits
130 coming from the rest of the debugger. */
131 mi
->raw_stdout
= gdb_stdout
;
133 /* Create MI console channels, each with a different prefix so they
134 can be distinguished. */
135 mi
->out
= new mi_console_file (mi
->raw_stdout
, "~", '"');
136 mi
->err
= new mi_console_file (mi
->raw_stdout
, "&", '"');
138 mi
->targ
= new mi_console_file (mi
->raw_stdout
, "@", '"');
139 mi
->event_channel
= new mi_console_file (mi
->raw_stdout
, "=", 0);
140 mi
->mi_uiout
= mi_out_new (name ());
141 gdb_assert (mi
->mi_uiout
!= nullptr);
142 mi
->cli_uiout
= new cli_ui_out (mi
->out
);
146 /* The initial inferior is created before this function is called, so we
147 need to report it explicitly when initializing the top-level MI
150 This is also called when additional MI interpreters are added (using
151 the new-ui command), when multiple inferiors possibly exist, so we need
152 to use iteration to report all the inferiors. mi_inferior_added can't
153 be used, because it would print the event on all the other MI UIs. */
155 for (inferior
*inf
: all_inferiors ())
157 target_terminal::scoped_restore_terminal_state term_state
;
158 target_terminal::ours_for_output ();
160 gdb_printf (mi
->event_channel
,
161 "thread-group-added,id=\"i%d\"",
164 gdb_flush (mi
->event_channel
);
172 struct mi_interp
*mi
= this;
173 struct ui
*ui
= current_ui
;
175 /* As per hack note in mi_interpreter_init, swap in the output
177 gdb_setup_readline (0);
179 ui
->call_readline
= gdb_readline_no_editing_callback
;
180 ui
->input_handler
= mi_execute_command_input_handler
;
182 gdb_stdout
= mi
->out
;
183 /* Route error and log output through the MI. */
184 gdb_stderr
= mi
->err
;
185 gdb_stdlog
= mi
->log
;
186 /* Route target output through the MI. */
187 gdb_stdtarg
= mi
->targ
;
188 /* Route target error through the MI as well. */
189 gdb_stdtargerr
= mi
->targ
;
191 deprecated_show_load_progress
= mi_load_progress
;
195 mi_interp::suspend ()
197 gdb_disable_readline ();
201 mi_interp::exec (const char *command
)
203 mi_execute_command_wrapper (command
);
207 mi_cmd_interpreter_exec (const char *command
, const char *const *argv
,
210 struct interp
*interp_to_use
;
214 error (_("-interpreter-exec: "
215 "Usage: -interpreter-exec interp command"));
217 interp_to_use
= interp_lookup (current_ui
, argv
[0]);
218 if (interp_to_use
== NULL
)
219 error (_("-interpreter-exec: could not find interpreter \"%s\""),
222 /* Note that unlike the CLI version of this command, we don't
223 actually set INTERP_TO_USE as the current interpreter, as we
224 still want gdb_stdout, etc. to point at MI streams. */
226 /* Insert the MI out hooks, making sure to also call the
227 interpreter's hooks if it has any. */
228 /* KRS: We shouldn't need this... Events should be installed and
229 they should just ALWAYS fire something out down the MI
231 mi_insert_notify_hooks ();
233 /* Now run the code. */
237 mi_remove_notify_hooks ();
240 for (i
= 1; i
< argc
; i
++)
241 interp_exec (interp_to_use
, argv
[i
]);
244 /* This inserts a number of hooks that are meant to produce
245 async-notify ("=") MI messages while running commands in another
246 interpreter using mi_interpreter_exec. The canonical use for this
247 is to allow access to the gdb CLI interpreter from within the MI,
248 while still producing MI style output when actions in the CLI
249 command change GDB's state. */
252 mi_insert_notify_hooks (void)
254 deprecated_query_hook
= mi_interp_query_hook
;
258 mi_remove_notify_hooks (void)
260 deprecated_query_hook
= NULL
;
264 mi_interp_query_hook (const char *ctlstr
, va_list ap
)
270 mi_execute_command_wrapper (const char *cmd
)
272 struct ui
*ui
= current_ui
;
274 mi_execute_command (cmd
, ui
->instream
== ui
->stdin_stream
);
277 /* Observer for the synchronous_command_done notification. */
280 mi_on_sync_execution_done (void)
282 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
287 /* If MI is sync, then output the MI prompt now, indicating we're
288 ready for further input. */
290 display_mi_prompt (mi
);
293 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
296 mi_execute_command_input_handler (gdb::unique_xmalloc_ptr
<char> &&cmd
)
298 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
299 struct ui
*ui
= current_ui
;
301 ui
->prompt_state
= PROMPT_NEEDED
;
303 mi_execute_command_wrapper (cmd
.get ());
305 /* Print a prompt, indicating we're ready for further input, unless
306 we just started a synchronous command. In that case, we're about
307 to go back to the event loop and will output the prompt in the
308 'synchronous_command_done' observer when the target next
310 if (ui
->prompt_state
== PROMPT_NEEDED
)
311 display_mi_prompt (mi
);
315 mi_interp::pre_command_loop ()
317 struct mi_interp
*mi
= this;
319 /* Turn off 8 bit strings in quoted output. Any character with the
320 high bit set is printed using C's octal format. */
321 sevenbit_strings
= 1;
323 /* Tell the world that we're alive. */
324 display_mi_prompt (mi
);
328 mi_new_thread (struct thread_info
*t
)
330 SWITCH_THRU_ALL_UIS ()
332 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
337 target_terminal::scoped_restore_terminal_state term_state
;
338 target_terminal::ours_for_output ();
340 gdb_printf (mi
->event_channel
,
341 "thread-created,id=\"%d\",group-id=\"i%d\"",
342 t
->global_num
, t
->inf
->num
);
343 gdb_flush (mi
->event_channel
);
348 mi_thread_exit (struct thread_info
*t
, int silent
)
350 SWITCH_THRU_ALL_UIS ()
352 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
357 target_terminal::scoped_restore_terminal_state term_state
;
358 target_terminal::ours_for_output ();
359 gdb_printf (mi
->event_channel
,
360 "thread-exited,id=\"%d\",group-id=\"i%d\"",
361 t
->global_num
, t
->inf
->num
);
362 gdb_flush (mi
->event_channel
);
366 /* Emit notification on changing the state of record. */
369 mi_record_changed (struct inferior
*inferior
, int started
, const char *method
,
372 SWITCH_THRU_ALL_UIS ()
374 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
379 target_terminal::scoped_restore_terminal_state term_state
;
380 target_terminal::ours_for_output ();
386 gdb_printf (mi
->event_channel
,
387 "record-started,thread-group=\"i%d\","
388 "method=\"%s\",format=\"%s\"",
389 inferior
->num
, method
, format
);
393 gdb_printf (mi
->event_channel
,
394 "record-started,thread-group=\"i%d\","
396 inferior
->num
, method
);
401 gdb_printf (mi
->event_channel
,
402 "record-stopped,thread-group=\"i%d\"",
406 gdb_flush (mi
->event_channel
);
411 mi_inferior_added (struct inferior
*inf
)
413 SWITCH_THRU_ALL_UIS ()
415 struct interp
*interp
;
416 struct mi_interp
*mi
;
418 /* We'll be called once for the initial inferior, before the top
419 level interpreter is set. */
420 interp
= top_level_interpreter ();
424 mi
= as_mi_interp (interp
);
428 target_terminal::scoped_restore_terminal_state term_state
;
429 target_terminal::ours_for_output ();
431 gdb_printf (mi
->event_channel
,
432 "thread-group-added,id=\"i%d\"",
434 gdb_flush (mi
->event_channel
);
439 mi_inferior_appeared (struct inferior
*inf
)
441 SWITCH_THRU_ALL_UIS ()
443 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
448 target_terminal::scoped_restore_terminal_state term_state
;
449 target_terminal::ours_for_output ();
451 gdb_printf (mi
->event_channel
,
452 "thread-group-started,id=\"i%d\",pid=\"%d\"",
454 gdb_flush (mi
->event_channel
);
459 mi_inferior_exit (struct inferior
*inf
)
461 SWITCH_THRU_ALL_UIS ()
463 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
468 target_terminal::scoped_restore_terminal_state term_state
;
469 target_terminal::ours_for_output ();
471 if (inf
->has_exit_code
)
472 gdb_printf (mi
->event_channel
,
473 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
474 inf
->num
, int_string (inf
->exit_code
, 8, 0, 0, 1));
476 gdb_printf (mi
->event_channel
,
477 "thread-group-exited,id=\"i%d\"", inf
->num
);
479 gdb_flush (mi
->event_channel
);
484 mi_inferior_removed (struct inferior
*inf
)
486 SWITCH_THRU_ALL_UIS ()
488 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
493 target_terminal::scoped_restore_terminal_state term_state
;
494 target_terminal::ours_for_output ();
496 gdb_printf (mi
->event_channel
,
497 "thread-group-removed,id=\"i%d\"",
499 gdb_flush (mi
->event_channel
);
503 /* Return the MI interpreter, if it is active -- either because it's
504 the top-level interpreter or the interpreter executing the current
505 command. Returns NULL if the MI interpreter is not being used. */
507 static struct mi_interp
*
508 find_mi_interp (void)
510 struct mi_interp
*mi
;
512 mi
= as_mi_interp (top_level_interpreter ());
516 mi
= as_mi_interp (command_interp ());
523 /* Observers for several run control events that print why the
524 inferior has stopped to both the MI event channel and to the MI
525 console. If the MI interpreter is not active, print nothing. */
528 mi_interp::on_signal_received (enum gdb_signal siggnal
)
530 print_signal_received_reason (this->mi_uiout
, siggnal
);
531 print_signal_received_reason (this->cli_uiout
, siggnal
);
534 /* Observer for the signal_exited notification. */
537 mi_on_signal_exited (enum gdb_signal siggnal
)
539 SWITCH_THRU_ALL_UIS ()
541 struct mi_interp
*mi
= find_mi_interp ();
546 print_signal_exited_reason (mi
->mi_uiout
, siggnal
);
547 print_signal_exited_reason (mi
->cli_uiout
, siggnal
);
551 /* Observer for the exited notification. */
554 mi_on_exited (int exitstatus
)
556 SWITCH_THRU_ALL_UIS ()
558 struct mi_interp
*mi
= find_mi_interp ();
563 print_exited_reason (mi
->mi_uiout
, exitstatus
);
564 print_exited_reason (mi
->cli_uiout
, exitstatus
);
568 /* Observer for the no_history notification. */
571 mi_on_no_history (void)
573 SWITCH_THRU_ALL_UIS ()
575 struct mi_interp
*mi
= find_mi_interp ();
580 print_no_history_reason (mi
->mi_uiout
);
581 print_no_history_reason (mi
->cli_uiout
);
586 mi_on_normal_stop_1 (struct bpstat
*bs
, int print_frame
)
588 /* Since this can be called when CLI command is executing,
589 using cli interpreter, be sure to use MI uiout for output,
590 not the current one. */
591 struct ui_out
*mi_uiout
= top_level_interpreter ()->interp_ui_out ();
592 struct mi_interp
*mi
= (struct mi_interp
*) top_level_interpreter ();
596 struct thread_info
*tp
;
598 struct interp
*console_interp
;
600 tp
= inferior_thread ();
602 if (tp
->thread_fsm () != nullptr
603 && tp
->thread_fsm ()->finished_p ())
605 enum async_reply_reason reason
;
607 reason
= tp
->thread_fsm ()->async_reply_reason ();
608 mi_uiout
->field_string ("reason", async_reason_lookup (reason
));
611 console_interp
= interp_lookup (current_ui
, INTERP_CONSOLE
);
612 /* We only want to print the displays once, and we want it to
613 look just how it would on the console, so we use this to
614 decide whether the MI stop should include them. */
615 bool console_print
= should_print_stop_to_console (console_interp
, tp
);
616 print_stop_event (mi_uiout
, !console_print
);
619 print_stop_event (mi
->cli_uiout
);
621 mi_uiout
->field_signed ("thread-id", tp
->global_num
);
624 ui_out_emit_list
list_emitter (mi_uiout
, "stopped-threads");
626 mi_uiout
->field_signed (NULL
, tp
->global_num
);
629 mi_uiout
->field_string ("stopped-threads", "all");
631 core
= target_core_of_thread (tp
->ptid
);
633 mi_uiout
->field_signed ("core", core
);
636 gdb_puts ("*stopped", mi
->raw_stdout
);
637 mi_out_put (mi_uiout
, mi
->raw_stdout
);
638 mi_out_rewind (mi_uiout
);
639 mi_print_timing_maybe (mi
->raw_stdout
);
640 gdb_puts ("\n", mi
->raw_stdout
);
641 gdb_flush (mi
->raw_stdout
);
645 mi_on_normal_stop (struct bpstat
*bs
, int print_frame
)
647 SWITCH_THRU_ALL_UIS ()
649 if (as_mi_interp (top_level_interpreter ()) == NULL
)
652 mi_on_normal_stop_1 (bs
, print_frame
);
657 mi_about_to_proceed (void)
659 /* Suppress output while calling an inferior function. */
661 if (inferior_ptid
!= null_ptid
)
663 struct thread_info
*tp
= inferior_thread ();
665 if (tp
->control
.in_infcall
)
669 mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
674 mi
->mi_proceeded
= 1;
677 /* When the element is non-zero, no MI notifications will be emitted in
678 response to the corresponding observers. */
680 struct mi_suppress_notification mi_suppress_notification
=
688 /* Emit notification on changing a traceframe. */
691 mi_traceframe_changed (int tfnum
, int tpnum
)
693 if (mi_suppress_notification
.traceframe
)
696 SWITCH_THRU_ALL_UIS ()
698 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
703 target_terminal::scoped_restore_terminal_state term_state
;
704 target_terminal::ours_for_output ();
707 gdb_printf (mi
->event_channel
, "traceframe-changed,"
708 "num=\"%d\",tracepoint=\"%d\"",
711 gdb_printf (mi
->event_channel
, "traceframe-changed,end");
713 gdb_flush (mi
->event_channel
);
717 /* Emit notification on creating a trace state variable. */
720 mi_tsv_created (const struct trace_state_variable
*tsv
)
722 SWITCH_THRU_ALL_UIS ()
724 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
729 target_terminal::scoped_restore_terminal_state term_state
;
730 target_terminal::ours_for_output ();
732 gdb_printf (mi
->event_channel
, "tsv-created,"
733 "name=\"%s\",initial=\"%s\"",
734 tsv
->name
.c_str (), plongest (tsv
->initial_value
));
736 gdb_flush (mi
->event_channel
);
740 /* Emit notification on deleting a trace state variable. */
743 mi_tsv_deleted (const struct trace_state_variable
*tsv
)
745 SWITCH_THRU_ALL_UIS ()
747 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
752 target_terminal::scoped_restore_terminal_state term_state
;
753 target_terminal::ours_for_output ();
756 gdb_printf (mi
->event_channel
, "tsv-deleted,"
757 "name=\"%s\"", tsv
->name
.c_str ());
759 gdb_printf (mi
->event_channel
, "tsv-deleted");
761 gdb_flush (mi
->event_channel
);
765 /* Emit notification on modifying a trace state variable. */
768 mi_tsv_modified (const struct trace_state_variable
*tsv
)
770 SWITCH_THRU_ALL_UIS ()
772 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
773 struct ui_out
*mi_uiout
;
778 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
780 target_terminal::scoped_restore_terminal_state term_state
;
781 target_terminal::ours_for_output ();
783 gdb_printf (mi
->event_channel
,
786 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
788 mi_uiout
->field_string ("name", tsv
->name
);
789 mi_uiout
->field_string ("initial",
790 plongest (tsv
->initial_value
));
791 if (tsv
->value_known
)
792 mi_uiout
->field_string ("current", plongest (tsv
->value
));
794 gdb_flush (mi
->event_channel
);
798 /* Print breakpoint BP on MI's event channel. */
801 mi_print_breakpoint_for_event (struct mi_interp
*mi
, breakpoint
*bp
)
803 ui_out
*mi_uiout
= mi
->interp_ui_out ();
805 /* We want the output from print_breakpoint to go to
806 mi->event_channel. One approach would be to just call
807 print_breakpoint, and then use mi_out_put to send the current
808 content of mi_uiout into mi->event_channel. However, that will
809 break if anything is output to mi_uiout prior to calling the
810 breakpoint_created notifications. So, we use
812 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
816 scoped_restore restore_uiout
817 = make_scoped_restore (¤t_uiout
, mi_uiout
);
819 print_breakpoint (bp
);
821 catch (const gdb_exception_error
&ex
)
823 exception_print (gdb_stderr
, ex
);
827 /* Emit notification about a created breakpoint. */
830 mi_breakpoint_created (struct breakpoint
*b
)
832 if (mi_suppress_notification
.breakpoint
)
838 SWITCH_THRU_ALL_UIS ()
840 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
845 target_terminal::scoped_restore_terminal_state term_state
;
846 target_terminal::ours_for_output ();
848 gdb_printf (mi
->event_channel
,
849 "breakpoint-created");
850 mi_print_breakpoint_for_event (mi
, b
);
852 gdb_flush (mi
->event_channel
);
856 /* Emit notification about deleted breakpoint. */
859 mi_breakpoint_deleted (struct breakpoint
*b
)
861 if (mi_suppress_notification
.breakpoint
)
867 SWITCH_THRU_ALL_UIS ()
869 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
874 target_terminal::scoped_restore_terminal_state term_state
;
875 target_terminal::ours_for_output ();
877 gdb_printf (mi
->event_channel
, "breakpoint-deleted,id=\"%d\"",
880 gdb_flush (mi
->event_channel
);
884 /* Emit notification about modified breakpoint. */
887 mi_breakpoint_modified (struct breakpoint
*b
)
889 if (mi_suppress_notification
.breakpoint
)
895 SWITCH_THRU_ALL_UIS ()
897 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
902 target_terminal::scoped_restore_terminal_state term_state
;
903 target_terminal::ours_for_output ();
904 gdb_printf (mi
->event_channel
,
905 "breakpoint-modified");
906 mi_print_breakpoint_for_event (mi
, b
);
908 gdb_flush (mi
->event_channel
);
913 mi_output_running (struct thread_info
*thread
)
915 SWITCH_THRU_ALL_UIS ()
917 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
922 gdb_printf (mi
->raw_stdout
,
923 "*running,thread-id=\"%d\"\n",
928 /* Return true if there are multiple inferiors loaded. This is used
929 for backwards compatibility -- if there's only one inferior, output
930 "all", otherwise, output each resumed thread individually. */
933 multiple_inferiors_p ()
936 for (inferior
*inf ATTRIBUTE_UNUSED
: all_non_exited_inferiors ())
947 mi_on_resume_1 (struct mi_interp
*mi
,
948 process_stratum_target
*targ
, ptid_t ptid
)
950 /* To cater for older frontends, emit ^running, but do it only once
951 per each command. We do it here, since at this point we know
952 that the target was successfully resumed, and in non-async mode,
953 we won't return back to MI interpreter code until the target
954 is done running, so delaying the output of "^running" until then
955 will make it impossible for frontend to know what's going on.
957 In future (MI3), we'll be outputting "^done" here. */
958 if (!mi
->running_result_record_printed
&& mi
->mi_proceeded
)
960 gdb_printf (mi
->raw_stdout
, "%s^running\n",
961 current_token
? current_token
: "");
964 /* Backwards compatibility. If doing a wildcard resume and there's
965 only one inferior, output "all", otherwise, output each resumed
966 thread individually. */
967 if ((ptid
== minus_one_ptid
|| ptid
.is_pid ())
968 && !multiple_inferiors_p ())
969 gdb_printf (mi
->raw_stdout
, "*running,thread-id=\"all\"\n");
971 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
972 mi_output_running (tp
);
974 if (!mi
->running_result_record_printed
&& mi
->mi_proceeded
)
976 mi
->running_result_record_printed
= 1;
977 /* This is what gdb used to do historically -- printing prompt
978 even if it cannot actually accept any input. This will be
979 surely removed for MI3, and may be removed even earlier. */
980 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
981 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
983 gdb_flush (mi
->raw_stdout
);
987 mi_on_resume (ptid_t ptid
)
989 struct thread_info
*tp
= NULL
;
991 process_stratum_target
*target
= current_inferior ()->process_target ();
992 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
993 tp
= inferior_thread ();
995 tp
= target
->find_thread (ptid
);
997 /* Suppress output while calling an inferior function. */
998 if (tp
->control
.in_infcall
)
1001 SWITCH_THRU_ALL_UIS ()
1003 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1008 target_terminal::scoped_restore_terminal_state term_state
;
1009 target_terminal::ours_for_output ();
1011 mi_on_resume_1 (mi
, target
, ptid
);
1015 /* See mi-interp.h. */
1018 mi_output_solib_attribs (ui_out
*uiout
, struct so_list
*solib
)
1020 struct gdbarch
*gdbarch
= target_gdbarch ();
1022 uiout
->field_string ("id", solib
->so_original_name
);
1023 uiout
->field_string ("target-name", solib
->so_original_name
);
1024 uiout
->field_string ("host-name", solib
->so_name
);
1025 uiout
->field_signed ("symbols-loaded", solib
->symbols_loaded
);
1026 if (!gdbarch_has_global_solist (target_gdbarch ()))
1027 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1029 ui_out_emit_list
list_emitter (uiout
, "ranges");
1030 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1031 if (solib
->addr_high
!= 0)
1033 uiout
->field_core_addr ("from", gdbarch
, solib
->addr_low
);
1034 uiout
->field_core_addr ("to", gdbarch
, solib
->addr_high
);
1039 mi_solib_loaded (struct so_list
*solib
)
1041 SWITCH_THRU_ALL_UIS ()
1043 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1044 struct ui_out
*uiout
;
1049 uiout
= top_level_interpreter ()->interp_ui_out ();
1051 target_terminal::scoped_restore_terminal_state term_state
;
1052 target_terminal::ours_for_output ();
1054 gdb_printf (mi
->event_channel
, "library-loaded");
1056 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1058 mi_output_solib_attribs (uiout
, solib
);
1060 gdb_flush (mi
->event_channel
);
1065 mi_solib_unloaded (struct so_list
*solib
)
1067 SWITCH_THRU_ALL_UIS ()
1069 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1070 struct ui_out
*uiout
;
1075 uiout
= top_level_interpreter ()->interp_ui_out ();
1077 target_terminal::scoped_restore_terminal_state term_state
;
1078 target_terminal::ours_for_output ();
1080 gdb_printf (mi
->event_channel
, "library-unloaded");
1082 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1084 uiout
->field_string ("id", solib
->so_original_name
);
1085 uiout
->field_string ("target-name", solib
->so_original_name
);
1086 uiout
->field_string ("host-name", solib
->so_name
);
1087 if (!gdbarch_has_global_solist (target_gdbarch ()))
1089 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1092 gdb_flush (mi
->event_channel
);
1096 /* Emit notification about the command parameter change. */
1099 mi_command_param_changed (const char *param
, const char *value
)
1101 if (mi_suppress_notification
.cmd_param_changed
)
1104 SWITCH_THRU_ALL_UIS ()
1106 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1107 struct ui_out
*mi_uiout
;
1112 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1114 target_terminal::scoped_restore_terminal_state term_state
;
1115 target_terminal::ours_for_output ();
1117 gdb_printf (mi
->event_channel
, "cmd-param-changed");
1119 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1121 mi_uiout
->field_string ("param", param
);
1122 mi_uiout
->field_string ("value", value
);
1124 gdb_flush (mi
->event_channel
);
1128 /* Emit notification about the target memory change. */
1131 mi_memory_changed (struct inferior
*inferior
, CORE_ADDR memaddr
,
1132 ssize_t len
, const bfd_byte
*myaddr
)
1134 if (mi_suppress_notification
.memory
)
1137 SWITCH_THRU_ALL_UIS ()
1139 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1140 struct ui_out
*mi_uiout
;
1141 struct obj_section
*sec
;
1146 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1148 target_terminal::scoped_restore_terminal_state term_state
;
1149 target_terminal::ours_for_output ();
1151 gdb_printf (mi
->event_channel
, "memory-changed");
1153 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1155 mi_uiout
->field_fmt ("thread-group", "i%d", inferior
->num
);
1156 mi_uiout
->field_core_addr ("addr", target_gdbarch (), memaddr
);
1157 mi_uiout
->field_string ("len", hex_string (len
));
1159 /* Append 'type=code' into notification if MEMADDR falls in the range of
1160 sections contain code. */
1161 sec
= find_pc_section (memaddr
);
1162 if (sec
!= NULL
&& sec
->objfile
!= NULL
)
1164 flagword flags
= bfd_section_flags (sec
->the_bfd_section
);
1166 if (flags
& SEC_CODE
)
1167 mi_uiout
->field_string ("type", "code");
1170 gdb_flush (mi
->event_channel
);
1174 /* Emit an event when the selection context (inferior, thread, frame)
1178 mi_user_selected_context_changed (user_selected_what selection
)
1180 struct thread_info
*tp
;
1182 /* Don't send an event if we're responding to an MI command. */
1183 if (mi_suppress_notification
.user_selected_context
)
1186 if (inferior_ptid
!= null_ptid
)
1187 tp
= inferior_thread ();
1191 SWITCH_THRU_ALL_UIS ()
1193 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1194 struct ui_out
*mi_uiout
;
1199 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1201 ui_out_redirect_pop
redirect_popper (mi_uiout
, mi
->event_channel
);
1203 target_terminal::scoped_restore_terminal_state term_state
;
1204 target_terminal::ours_for_output ();
1206 if (selection
& USER_SELECTED_INFERIOR
)
1207 print_selected_inferior (mi
->cli_uiout
);
1210 && (selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
)))
1212 print_selected_thread_frame (mi
->cli_uiout
, selection
);
1214 gdb_printf (mi
->event_channel
,
1215 "thread-selected,id=\"%d\"",
1218 if (tp
->state
!= THREAD_RUNNING
)
1220 if (has_stack_frames ())
1221 print_stack_frame_to_uiout (mi_uiout
, get_selected_frame (NULL
),
1226 gdb_flush (mi
->event_channel
);
1231 mi_interp::interp_ui_out ()
1233 return this->mi_uiout
;
1236 /* Do MI-specific logging actions; save raw_stdout, and change all
1237 the consoles to use the supplied ui-file(s). */
1240 mi_interp::set_logging (ui_file_up logfile
, bool logging_redirect
,
1241 bool debug_redirect
)
1243 struct mi_interp
*mi
= this;
1245 if (logfile
!= NULL
)
1247 mi
->saved_raw_stdout
= mi
->raw_stdout
;
1249 ui_file
*logfile_p
= logfile
.get ();
1250 mi
->logfile_holder
= std::move (logfile
);
1252 /* If something is not being redirected, then a tee containing both the
1253 logfile and stdout. */
1254 ui_file
*tee
= nullptr;
1255 if (!logging_redirect
|| !debug_redirect
)
1257 tee
= new tee_file (mi
->raw_stdout
, logfile_p
);
1258 mi
->stdout_holder
.reset (tee
);
1261 mi
->raw_stdout
= logging_redirect
? logfile_p
: tee
;
1265 mi
->logfile_holder
.reset ();
1266 mi
->stdout_holder
.reset ();
1267 mi
->raw_stdout
= mi
->saved_raw_stdout
;
1268 mi
->saved_raw_stdout
= nullptr;
1271 mi
->out
->set_raw (mi
->raw_stdout
);
1272 mi
->err
->set_raw (mi
->raw_stdout
);
1273 mi
->log
->set_raw (mi
->raw_stdout
);
1274 mi
->targ
->set_raw (mi
->raw_stdout
);
1275 mi
->event_channel
->set_raw (mi
->raw_stdout
);
1278 /* Factory for MI interpreters. */
1280 static struct interp
*
1281 mi_interp_factory (const char *name
)
1283 return new mi_interp (name
);
1286 void _initialize_mi_interp ();
1288 _initialize_mi_interp ()
1290 /* The various interpreter levels. */
1291 interp_factory_register (INTERP_MI2
, mi_interp_factory
);
1292 interp_factory_register (INTERP_MI3
, mi_interp_factory
);
1293 interp_factory_register (INTERP_MI4
, mi_interp_factory
);
1294 interp_factory_register (INTERP_MI
, mi_interp_factory
);
1296 gdb::observers::signal_exited
.attach (mi_on_signal_exited
, "mi-interp");
1297 gdb::observers::exited
.attach (mi_on_exited
, "mi-interp");
1298 gdb::observers::no_history
.attach (mi_on_no_history
, "mi-interp");
1299 gdb::observers::new_thread
.attach (mi_new_thread
, "mi-interp");
1300 gdb::observers::thread_exit
.attach (mi_thread_exit
, "mi-interp");
1301 gdb::observers::inferior_added
.attach (mi_inferior_added
, "mi-interp");
1302 gdb::observers::inferior_appeared
.attach (mi_inferior_appeared
, "mi-interp");
1303 gdb::observers::inferior_exit
.attach (mi_inferior_exit
, "mi-interp");
1304 gdb::observers::inferior_removed
.attach (mi_inferior_removed
, "mi-interp");
1305 gdb::observers::record_changed
.attach (mi_record_changed
, "mi-interp");
1306 gdb::observers::normal_stop
.attach (mi_on_normal_stop
, "mi-interp");
1307 gdb::observers::target_resumed
.attach (mi_on_resume
, "mi-interp");
1308 gdb::observers::solib_loaded
.attach (mi_solib_loaded
, "mi-interp");
1309 gdb::observers::solib_unloaded
.attach (mi_solib_unloaded
, "mi-interp");
1310 gdb::observers::about_to_proceed
.attach (mi_about_to_proceed
, "mi-interp");
1311 gdb::observers::traceframe_changed
.attach (mi_traceframe_changed
,
1313 gdb::observers::tsv_created
.attach (mi_tsv_created
, "mi-interp");
1314 gdb::observers::tsv_deleted
.attach (mi_tsv_deleted
, "mi-interp");
1315 gdb::observers::tsv_modified
.attach (mi_tsv_modified
, "mi-interp");
1316 gdb::observers::breakpoint_created
.attach (mi_breakpoint_created
,
1318 gdb::observers::breakpoint_deleted
.attach (mi_breakpoint_deleted
,
1320 gdb::observers::breakpoint_modified
.attach (mi_breakpoint_modified
,
1322 gdb::observers::command_param_changed
.attach (mi_command_param_changed
,
1324 gdb::observers::command_error
.attach (mi_on_command_error
, "mi-interp");
1325 gdb::observers::memory_changed
.attach (mi_memory_changed
, "mi-interp");
1326 gdb::observers::sync_execution_done
.attach (mi_on_sync_execution_done
,
1328 gdb::observers::user_selected_context_changed
.attach
1329 (mi_user_selected_context_changed
, "mi-interp");