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_received (enum gdb_signal siggnal
);
64 static void mi_on_end_stepping_range (void);
65 static void mi_on_signal_exited (enum gdb_signal siggnal
);
66 static void mi_on_exited (int exitstatus
);
67 static void mi_on_normal_stop (struct bpstat
*bs
, int print_frame
);
68 static void mi_on_no_history (void);
70 static void mi_new_thread (struct thread_info
*t
);
71 static void mi_thread_exit (struct thread_info
*t
, int silent
);
72 static void mi_record_changed (struct inferior
*, int, const char *,
74 static void mi_inferior_added (struct inferior
*inf
);
75 static void mi_inferior_appeared (struct inferior
*inf
);
76 static void mi_inferior_exit (struct inferior
*inf
);
77 static void mi_inferior_removed (struct inferior
*inf
);
78 static void mi_on_resume (ptid_t ptid
);
79 static void mi_solib_loaded (struct so_list
*solib
);
80 static void mi_solib_unloaded (struct so_list
*solib
);
81 static void mi_about_to_proceed (void);
82 static void mi_traceframe_changed (int tfnum
, int tpnum
);
83 static void mi_tsv_created (const struct trace_state_variable
*tsv
);
84 static void mi_tsv_deleted (const struct trace_state_variable
*tsv
);
85 static void mi_tsv_modified (const struct trace_state_variable
*tsv
);
86 static void mi_breakpoint_created (struct breakpoint
*b
);
87 static void mi_breakpoint_deleted (struct breakpoint
*b
);
88 static void mi_breakpoint_modified (struct breakpoint
*b
);
89 static void mi_command_param_changed (const char *param
, const char *value
);
90 static void mi_memory_changed (struct inferior
*inf
, CORE_ADDR memaddr
,
91 ssize_t len
, const bfd_byte
*myaddr
);
92 static void mi_on_sync_execution_done (void);
94 /* Display the MI prompt. */
97 display_mi_prompt (struct mi_interp
*mi
)
99 struct ui
*ui
= current_ui
;
101 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
102 gdb_flush (mi
->raw_stdout
);
103 ui
->prompt_state
= PROMPTED
;
106 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
107 returns NULL otherwise. */
109 static struct mi_interp
*
110 as_mi_interp (struct interp
*interp
)
112 return dynamic_cast<mi_interp
*> (interp
);
115 /* Observer for the command_error notification. */
118 mi_on_command_error ()
120 mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
122 display_mi_prompt (mi
);
126 mi_interp::init (bool top_level
)
128 mi_interp
*mi
= this;
130 /* Store the current output channel, so that we can create a console
131 channel that encapsulates and prefixes all gdb_output-type bits
132 coming from the rest of the debugger. */
133 mi
->raw_stdout
= gdb_stdout
;
135 /* Create MI console channels, each with a different prefix so they
136 can be distinguished. */
137 mi
->out
= new mi_console_file (mi
->raw_stdout
, "~", '"');
138 mi
->err
= new mi_console_file (mi
->raw_stdout
, "&", '"');
140 mi
->targ
= new mi_console_file (mi
->raw_stdout
, "@", '"');
141 mi
->event_channel
= new mi_console_file (mi
->raw_stdout
, "=", 0);
142 mi
->mi_uiout
= mi_out_new (name ());
143 gdb_assert (mi
->mi_uiout
!= nullptr);
144 mi
->cli_uiout
= new cli_ui_out (mi
->out
);
148 /* The initial inferior is created before this function is called, so we
149 need to report it explicitly when initializing the top-level MI
152 This is also called when additional MI interpreters are added (using
153 the new-ui command), when multiple inferiors possibly exist, so we need
154 to use iteration to report all the inferiors. mi_inferior_added can't
155 be used, because it would print the event on all the other MI UIs. */
157 for (inferior
*inf
: all_inferiors ())
159 target_terminal::scoped_restore_terminal_state term_state
;
160 target_terminal::ours_for_output ();
162 gdb_printf (mi
->event_channel
,
163 "thread-group-added,id=\"i%d\"",
166 gdb_flush (mi
->event_channel
);
174 struct mi_interp
*mi
= this;
175 struct ui
*ui
= current_ui
;
177 /* As per hack note in mi_interpreter_init, swap in the output
179 gdb_setup_readline (0);
181 ui
->call_readline
= gdb_readline_no_editing_callback
;
182 ui
->input_handler
= mi_execute_command_input_handler
;
184 gdb_stdout
= mi
->out
;
185 /* Route error and log output through the MI. */
186 gdb_stderr
= mi
->err
;
187 gdb_stdlog
= mi
->log
;
188 /* Route target output through the MI. */
189 gdb_stdtarg
= mi
->targ
;
190 /* Route target error through the MI as well. */
191 gdb_stdtargerr
= mi
->targ
;
193 deprecated_show_load_progress
= mi_load_progress
;
197 mi_interp::suspend ()
199 gdb_disable_readline ();
203 mi_interp::exec (const char *command
)
205 mi_execute_command_wrapper (command
);
209 mi_cmd_interpreter_exec (const char *command
, char **argv
, int argc
)
211 struct interp
*interp_to_use
;
215 error (_("-interpreter-exec: "
216 "Usage: -interpreter-exec interp command"));
218 interp_to_use
= interp_lookup (current_ui
, argv
[0]);
219 if (interp_to_use
== NULL
)
220 error (_("-interpreter-exec: could not find interpreter \"%s\""),
223 /* Note that unlike the CLI version of this command, we don't
224 actually set INTERP_TO_USE as the current interpreter, as we
225 still want gdb_stdout, etc. to point at MI streams. */
227 /* Insert the MI out hooks, making sure to also call the
228 interpreter's hooks if it has any. */
229 /* KRS: We shouldn't need this... Events should be installed and
230 they should just ALWAYS fire something out down the MI
232 mi_insert_notify_hooks ();
234 /* Now run the code. */
238 mi_remove_notify_hooks ();
241 for (i
= 1; i
< argc
; i
++)
242 interp_exec (interp_to_use
, argv
[i
]);
245 /* This inserts a number of hooks that are meant to produce
246 async-notify ("=") MI messages while running commands in another
247 interpreter using mi_interpreter_exec. The canonical use for this
248 is to allow access to the gdb CLI interpreter from within the MI,
249 while still producing MI style output when actions in the CLI
250 command change GDB's state. */
253 mi_insert_notify_hooks (void)
255 deprecated_query_hook
= mi_interp_query_hook
;
259 mi_remove_notify_hooks (void)
261 deprecated_query_hook
= NULL
;
265 mi_interp_query_hook (const char *ctlstr
, va_list ap
)
271 mi_execute_command_wrapper (const char *cmd
)
273 struct ui
*ui
= current_ui
;
275 mi_execute_command (cmd
, ui
->instream
== ui
->stdin_stream
);
278 /* Observer for the synchronous_command_done notification. */
281 mi_on_sync_execution_done (void)
283 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
288 /* If MI is sync, then output the MI prompt now, indicating we're
289 ready for further input. */
291 display_mi_prompt (mi
);
294 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
297 mi_execute_command_input_handler (gdb::unique_xmalloc_ptr
<char> &&cmd
)
299 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
300 struct ui
*ui
= current_ui
;
302 ui
->prompt_state
= PROMPT_NEEDED
;
304 mi_execute_command_wrapper (cmd
.get ());
306 /* Print a prompt, indicating we're ready for further input, unless
307 we just started a synchronous command. In that case, we're about
308 to go back to the event loop and will output the prompt in the
309 'synchronous_command_done' observer when the target next
311 if (ui
->prompt_state
== PROMPT_NEEDED
)
312 display_mi_prompt (mi
);
316 mi_interp::pre_command_loop ()
318 struct mi_interp
*mi
= this;
320 /* Turn off 8 bit strings in quoted output. Any character with the
321 high bit set is printed using C's octal format. */
322 sevenbit_strings
= 1;
324 /* Tell the world that we're alive. */
325 display_mi_prompt (mi
);
329 mi_new_thread (struct thread_info
*t
)
331 SWITCH_THRU_ALL_UIS ()
333 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
338 target_terminal::scoped_restore_terminal_state term_state
;
339 target_terminal::ours_for_output ();
341 gdb_printf (mi
->event_channel
,
342 "thread-created,id=\"%d\",group-id=\"i%d\"",
343 t
->global_num
, t
->inf
->num
);
344 gdb_flush (mi
->event_channel
);
349 mi_thread_exit (struct thread_info
*t
, int silent
)
351 SWITCH_THRU_ALL_UIS ()
353 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
358 target_terminal::scoped_restore_terminal_state term_state
;
359 target_terminal::ours_for_output ();
360 gdb_printf (mi
->event_channel
,
361 "thread-exited,id=\"%d\",group-id=\"i%d\"",
362 t
->global_num
, t
->inf
->num
);
363 gdb_flush (mi
->event_channel
);
367 /* Emit notification on changing the state of record. */
370 mi_record_changed (struct inferior
*inferior
, int started
, const char *method
,
373 SWITCH_THRU_ALL_UIS ()
375 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
380 target_terminal::scoped_restore_terminal_state term_state
;
381 target_terminal::ours_for_output ();
387 gdb_printf (mi
->event_channel
,
388 "record-started,thread-group=\"i%d\","
389 "method=\"%s\",format=\"%s\"",
390 inferior
->num
, method
, format
);
394 gdb_printf (mi
->event_channel
,
395 "record-started,thread-group=\"i%d\","
397 inferior
->num
, method
);
402 gdb_printf (mi
->event_channel
,
403 "record-stopped,thread-group=\"i%d\"",
407 gdb_flush (mi
->event_channel
);
412 mi_inferior_added (struct inferior
*inf
)
414 SWITCH_THRU_ALL_UIS ()
416 struct interp
*interp
;
417 struct mi_interp
*mi
;
419 /* We'll be called once for the initial inferior, before the top
420 level interpreter is set. */
421 interp
= top_level_interpreter ();
425 mi
= as_mi_interp (interp
);
429 target_terminal::scoped_restore_terminal_state term_state
;
430 target_terminal::ours_for_output ();
432 gdb_printf (mi
->event_channel
,
433 "thread-group-added,id=\"i%d\"",
435 gdb_flush (mi
->event_channel
);
440 mi_inferior_appeared (struct inferior
*inf
)
442 SWITCH_THRU_ALL_UIS ()
444 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
449 target_terminal::scoped_restore_terminal_state term_state
;
450 target_terminal::ours_for_output ();
452 gdb_printf (mi
->event_channel
,
453 "thread-group-started,id=\"i%d\",pid=\"%d\"",
455 gdb_flush (mi
->event_channel
);
460 mi_inferior_exit (struct inferior
*inf
)
462 SWITCH_THRU_ALL_UIS ()
464 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
469 target_terminal::scoped_restore_terminal_state term_state
;
470 target_terminal::ours_for_output ();
472 if (inf
->has_exit_code
)
473 gdb_printf (mi
->event_channel
,
474 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
475 inf
->num
, int_string (inf
->exit_code
, 8, 0, 0, 1));
477 gdb_printf (mi
->event_channel
,
478 "thread-group-exited,id=\"i%d\"", inf
->num
);
480 gdb_flush (mi
->event_channel
);
485 mi_inferior_removed (struct inferior
*inf
)
487 SWITCH_THRU_ALL_UIS ()
489 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
494 target_terminal::scoped_restore_terminal_state term_state
;
495 target_terminal::ours_for_output ();
497 gdb_printf (mi
->event_channel
,
498 "thread-group-removed,id=\"i%d\"",
500 gdb_flush (mi
->event_channel
);
504 /* Return the MI interpreter, if it is active -- either because it's
505 the top-level interpreter or the interpreter executing the current
506 command. Returns NULL if the MI interpreter is not being used. */
508 static struct mi_interp
*
509 find_mi_interp (void)
511 struct mi_interp
*mi
;
513 mi
= as_mi_interp (top_level_interpreter ());
517 mi
= as_mi_interp (command_interp ());
524 /* Observers for several run control events that print why the
525 inferior has stopped to both the MI event channel and to the MI
526 console. If the MI interpreter is not active, print nothing. */
528 /* Observer for the signal_received notification. */
531 mi_on_signal_received (enum gdb_signal siggnal
)
533 SWITCH_THRU_ALL_UIS ()
535 struct mi_interp
*mi
= find_mi_interp ();
540 print_signal_received_reason (mi
->mi_uiout
, siggnal
);
541 print_signal_received_reason (mi
->cli_uiout
, siggnal
);
545 /* Observer for the end_stepping_range notification. */
548 mi_on_end_stepping_range (void)
550 SWITCH_THRU_ALL_UIS ()
552 struct mi_interp
*mi
= find_mi_interp ();
557 print_end_stepping_range_reason (mi
->mi_uiout
);
558 print_end_stepping_range_reason (mi
->cli_uiout
);
562 /* Observer for the signal_exited notification. */
565 mi_on_signal_exited (enum gdb_signal siggnal
)
567 SWITCH_THRU_ALL_UIS ()
569 struct mi_interp
*mi
= find_mi_interp ();
574 print_signal_exited_reason (mi
->mi_uiout
, siggnal
);
575 print_signal_exited_reason (mi
->cli_uiout
, siggnal
);
579 /* Observer for the exited notification. */
582 mi_on_exited (int exitstatus
)
584 SWITCH_THRU_ALL_UIS ()
586 struct mi_interp
*mi
= find_mi_interp ();
591 print_exited_reason (mi
->mi_uiout
, exitstatus
);
592 print_exited_reason (mi
->cli_uiout
, exitstatus
);
596 /* Observer for the no_history notification. */
599 mi_on_no_history (void)
601 SWITCH_THRU_ALL_UIS ()
603 struct mi_interp
*mi
= find_mi_interp ();
608 print_no_history_reason (mi
->mi_uiout
);
609 print_no_history_reason (mi
->cli_uiout
);
614 mi_on_normal_stop_1 (struct bpstat
*bs
, int print_frame
)
616 /* Since this can be called when CLI command is executing,
617 using cli interpreter, be sure to use MI uiout for output,
618 not the current one. */
619 struct ui_out
*mi_uiout
= top_level_interpreter ()->interp_ui_out ();
620 struct mi_interp
*mi
= (struct mi_interp
*) top_level_interpreter ();
624 struct thread_info
*tp
;
626 struct interp
*console_interp
;
628 tp
= inferior_thread ();
630 if (tp
->thread_fsm () != nullptr
631 && tp
->thread_fsm ()->finished_p ())
633 enum async_reply_reason reason
;
635 reason
= tp
->thread_fsm ()->async_reply_reason ();
636 mi_uiout
->field_string ("reason", async_reason_lookup (reason
));
639 console_interp
= interp_lookup (current_ui
, INTERP_CONSOLE
);
640 /* We only want to print the displays once, and we want it to
641 look just how it would on the console, so we use this to
642 decide whether the MI stop should include them. */
643 bool console_print
= should_print_stop_to_console (console_interp
, tp
);
644 print_stop_event (mi_uiout
, !console_print
);
647 print_stop_event (mi
->cli_uiout
);
649 mi_uiout
->field_signed ("thread-id", tp
->global_num
);
652 ui_out_emit_list
list_emitter (mi_uiout
, "stopped-threads");
654 mi_uiout
->field_signed (NULL
, tp
->global_num
);
657 mi_uiout
->field_string ("stopped-threads", "all");
659 core
= target_core_of_thread (tp
->ptid
);
661 mi_uiout
->field_signed ("core", core
);
664 gdb_puts ("*stopped", mi
->raw_stdout
);
665 mi_out_put (mi_uiout
, mi
->raw_stdout
);
666 mi_out_rewind (mi_uiout
);
667 mi_print_timing_maybe (mi
->raw_stdout
);
668 gdb_puts ("\n", mi
->raw_stdout
);
669 gdb_flush (mi
->raw_stdout
);
673 mi_on_normal_stop (struct bpstat
*bs
, int print_frame
)
675 SWITCH_THRU_ALL_UIS ()
677 if (as_mi_interp (top_level_interpreter ()) == NULL
)
680 mi_on_normal_stop_1 (bs
, print_frame
);
685 mi_about_to_proceed (void)
687 /* Suppress output while calling an inferior function. */
689 if (inferior_ptid
!= null_ptid
)
691 struct thread_info
*tp
= inferior_thread ();
693 if (tp
->control
.in_infcall
)
700 /* When the element is non-zero, no MI notifications will be emitted in
701 response to the corresponding observers. */
703 struct mi_suppress_notification mi_suppress_notification
=
711 /* Emit notification on changing a traceframe. */
714 mi_traceframe_changed (int tfnum
, int tpnum
)
716 if (mi_suppress_notification
.traceframe
)
719 SWITCH_THRU_ALL_UIS ()
721 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
726 target_terminal::scoped_restore_terminal_state term_state
;
727 target_terminal::ours_for_output ();
730 gdb_printf (mi
->event_channel
, "traceframe-changed,"
731 "num=\"%d\",tracepoint=\"%d\"",
734 gdb_printf (mi
->event_channel
, "traceframe-changed,end");
736 gdb_flush (mi
->event_channel
);
740 /* Emit notification on creating a trace state variable. */
743 mi_tsv_created (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 ();
755 gdb_printf (mi
->event_channel
, "tsv-created,"
756 "name=\"%s\",initial=\"%s\"",
757 tsv
->name
.c_str (), plongest (tsv
->initial_value
));
759 gdb_flush (mi
->event_channel
);
763 /* Emit notification on deleting a trace state variable. */
766 mi_tsv_deleted (const struct trace_state_variable
*tsv
)
768 SWITCH_THRU_ALL_UIS ()
770 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
775 target_terminal::scoped_restore_terminal_state term_state
;
776 target_terminal::ours_for_output ();
779 gdb_printf (mi
->event_channel
, "tsv-deleted,"
780 "name=\"%s\"", tsv
->name
.c_str ());
782 gdb_printf (mi
->event_channel
, "tsv-deleted");
784 gdb_flush (mi
->event_channel
);
788 /* Emit notification on modifying a trace state variable. */
791 mi_tsv_modified (const struct trace_state_variable
*tsv
)
793 SWITCH_THRU_ALL_UIS ()
795 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
796 struct ui_out
*mi_uiout
;
801 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
803 target_terminal::scoped_restore_terminal_state term_state
;
804 target_terminal::ours_for_output ();
806 gdb_printf (mi
->event_channel
,
809 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
811 mi_uiout
->field_string ("name", tsv
->name
);
812 mi_uiout
->field_string ("initial",
813 plongest (tsv
->initial_value
));
814 if (tsv
->value_known
)
815 mi_uiout
->field_string ("current", plongest (tsv
->value
));
817 gdb_flush (mi
->event_channel
);
821 /* Print breakpoint BP on MI's event channel. */
824 mi_print_breakpoint_for_event (struct mi_interp
*mi
, breakpoint
*bp
)
826 ui_out
*mi_uiout
= mi
->interp_ui_out ();
828 /* We want the output from print_breakpoint to go to
829 mi->event_channel. One approach would be to just call
830 print_breakpoint, and then use mi_out_put to send the current
831 content of mi_uiout into mi->event_channel. However, that will
832 break if anything is output to mi_uiout prior to calling the
833 breakpoint_created notifications. So, we use
835 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
839 scoped_restore restore_uiout
840 = make_scoped_restore (¤t_uiout
, mi_uiout
);
842 print_breakpoint (bp
);
844 catch (const gdb_exception_error
&ex
)
846 exception_print (gdb_stderr
, ex
);
850 /* Emit notification about a created breakpoint. */
853 mi_breakpoint_created (struct breakpoint
*b
)
855 if (mi_suppress_notification
.breakpoint
)
861 SWITCH_THRU_ALL_UIS ()
863 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
868 target_terminal::scoped_restore_terminal_state term_state
;
869 target_terminal::ours_for_output ();
871 gdb_printf (mi
->event_channel
,
872 "breakpoint-created");
873 mi_print_breakpoint_for_event (mi
, b
);
875 gdb_flush (mi
->event_channel
);
879 /* Emit notification about deleted breakpoint. */
882 mi_breakpoint_deleted (struct breakpoint
*b
)
884 if (mi_suppress_notification
.breakpoint
)
890 SWITCH_THRU_ALL_UIS ()
892 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
897 target_terminal::scoped_restore_terminal_state term_state
;
898 target_terminal::ours_for_output ();
900 gdb_printf (mi
->event_channel
, "breakpoint-deleted,id=\"%d\"",
903 gdb_flush (mi
->event_channel
);
907 /* Emit notification about modified breakpoint. */
910 mi_breakpoint_modified (struct breakpoint
*b
)
912 if (mi_suppress_notification
.breakpoint
)
918 SWITCH_THRU_ALL_UIS ()
920 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
925 target_terminal::scoped_restore_terminal_state term_state
;
926 target_terminal::ours_for_output ();
927 gdb_printf (mi
->event_channel
,
928 "breakpoint-modified");
929 mi_print_breakpoint_for_event (mi
, b
);
931 gdb_flush (mi
->event_channel
);
936 mi_output_running (struct thread_info
*thread
)
938 SWITCH_THRU_ALL_UIS ()
940 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
945 gdb_printf (mi
->raw_stdout
,
946 "*running,thread-id=\"%d\"\n",
951 /* Return true if there are multiple inferiors loaded. This is used
952 for backwards compatibility -- if there's only one inferior, output
953 "all", otherwise, output each resumed thread individually. */
956 multiple_inferiors_p ()
959 for (inferior
*inf ATTRIBUTE_UNUSED
: all_non_exited_inferiors ())
970 mi_on_resume_1 (struct mi_interp
*mi
,
971 process_stratum_target
*targ
, ptid_t ptid
)
973 /* To cater for older frontends, emit ^running, but do it only once
974 per each command. We do it here, since at this point we know
975 that the target was successfully resumed, and in non-async mode,
976 we won't return back to MI interpreter code until the target
977 is done running, so delaying the output of "^running" until then
978 will make it impossible for frontend to know what's going on.
980 In future (MI3), we'll be outputting "^done" here. */
981 if (!running_result_record_printed
&& mi_proceeded
)
983 gdb_printf (mi
->raw_stdout
, "%s^running\n",
984 current_token
? current_token
: "");
987 /* Backwards compatibility. If doing a wildcard resume and there's
988 only one inferior, output "all", otherwise, output each resumed
989 thread individually. */
990 if ((ptid
== minus_one_ptid
|| ptid
.is_pid ())
991 && !multiple_inferiors_p ())
992 gdb_printf (mi
->raw_stdout
, "*running,thread-id=\"all\"\n");
994 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
995 mi_output_running (tp
);
997 if (!running_result_record_printed
&& mi_proceeded
)
999 running_result_record_printed
= 1;
1000 /* This is what gdb used to do historically -- printing prompt
1001 even if it cannot actually accept any input. This will be
1002 surely removed for MI3, and may be removed even earlier. */
1003 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
1004 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
1006 gdb_flush (mi
->raw_stdout
);
1010 mi_on_resume (ptid_t ptid
)
1012 struct thread_info
*tp
= NULL
;
1014 process_stratum_target
*target
= current_inferior ()->process_target ();
1015 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
1016 tp
= inferior_thread ();
1018 tp
= target
->find_thread (ptid
);
1020 /* Suppress output while calling an inferior function. */
1021 if (tp
->control
.in_infcall
)
1024 SWITCH_THRU_ALL_UIS ()
1026 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1031 target_terminal::scoped_restore_terminal_state term_state
;
1032 target_terminal::ours_for_output ();
1034 mi_on_resume_1 (mi
, target
, ptid
);
1038 /* See mi-interp.h. */
1041 mi_output_solib_attribs (ui_out
*uiout
, struct so_list
*solib
)
1043 struct gdbarch
*gdbarch
= target_gdbarch ();
1045 uiout
->field_string ("id", solib
->so_original_name
);
1046 uiout
->field_string ("target-name", solib
->so_original_name
);
1047 uiout
->field_string ("host-name", solib
->so_name
);
1048 uiout
->field_signed ("symbols-loaded", solib
->symbols_loaded
);
1049 if (!gdbarch_has_global_solist (target_gdbarch ()))
1050 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1052 ui_out_emit_list
list_emitter (uiout
, "ranges");
1053 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1054 if (solib
->addr_high
!= 0)
1056 uiout
->field_core_addr ("from", gdbarch
, solib
->addr_low
);
1057 uiout
->field_core_addr ("to", gdbarch
, solib
->addr_high
);
1062 mi_solib_loaded (struct so_list
*solib
)
1064 SWITCH_THRU_ALL_UIS ()
1066 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1067 struct ui_out
*uiout
;
1072 uiout
= top_level_interpreter ()->interp_ui_out ();
1074 target_terminal::scoped_restore_terminal_state term_state
;
1075 target_terminal::ours_for_output ();
1077 gdb_printf (mi
->event_channel
, "library-loaded");
1079 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1081 mi_output_solib_attribs (uiout
, solib
);
1083 gdb_flush (mi
->event_channel
);
1088 mi_solib_unloaded (struct so_list
*solib
)
1090 SWITCH_THRU_ALL_UIS ()
1092 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1093 struct ui_out
*uiout
;
1098 uiout
= top_level_interpreter ()->interp_ui_out ();
1100 target_terminal::scoped_restore_terminal_state term_state
;
1101 target_terminal::ours_for_output ();
1103 gdb_printf (mi
->event_channel
, "library-unloaded");
1105 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1107 uiout
->field_string ("id", solib
->so_original_name
);
1108 uiout
->field_string ("target-name", solib
->so_original_name
);
1109 uiout
->field_string ("host-name", solib
->so_name
);
1110 if (!gdbarch_has_global_solist (target_gdbarch ()))
1112 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1115 gdb_flush (mi
->event_channel
);
1119 /* Emit notification about the command parameter change. */
1122 mi_command_param_changed (const char *param
, const char *value
)
1124 if (mi_suppress_notification
.cmd_param_changed
)
1127 SWITCH_THRU_ALL_UIS ()
1129 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1130 struct ui_out
*mi_uiout
;
1135 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1137 target_terminal::scoped_restore_terminal_state term_state
;
1138 target_terminal::ours_for_output ();
1140 gdb_printf (mi
->event_channel
, "cmd-param-changed");
1142 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1144 mi_uiout
->field_string ("param", param
);
1145 mi_uiout
->field_string ("value", value
);
1147 gdb_flush (mi
->event_channel
);
1151 /* Emit notification about the target memory change. */
1154 mi_memory_changed (struct inferior
*inferior
, CORE_ADDR memaddr
,
1155 ssize_t len
, const bfd_byte
*myaddr
)
1157 if (mi_suppress_notification
.memory
)
1160 SWITCH_THRU_ALL_UIS ()
1162 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1163 struct ui_out
*mi_uiout
;
1164 struct obj_section
*sec
;
1169 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1171 target_terminal::scoped_restore_terminal_state term_state
;
1172 target_terminal::ours_for_output ();
1174 gdb_printf (mi
->event_channel
, "memory-changed");
1176 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1178 mi_uiout
->field_fmt ("thread-group", "i%d", inferior
->num
);
1179 mi_uiout
->field_core_addr ("addr", target_gdbarch (), memaddr
);
1180 mi_uiout
->field_string ("len", hex_string (len
));
1182 /* Append 'type=code' into notification if MEMADDR falls in the range of
1183 sections contain code. */
1184 sec
= find_pc_section (memaddr
);
1185 if (sec
!= NULL
&& sec
->objfile
!= NULL
)
1187 flagword flags
= bfd_section_flags (sec
->the_bfd_section
);
1189 if (flags
& SEC_CODE
)
1190 mi_uiout
->field_string ("type", "code");
1193 gdb_flush (mi
->event_channel
);
1197 /* Emit an event when the selection context (inferior, thread, frame)
1201 mi_user_selected_context_changed (user_selected_what selection
)
1203 struct thread_info
*tp
;
1205 /* Don't send an event if we're responding to an MI command. */
1206 if (mi_suppress_notification
.user_selected_context
)
1209 if (inferior_ptid
!= null_ptid
)
1210 tp
= inferior_thread ();
1214 SWITCH_THRU_ALL_UIS ()
1216 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1217 struct ui_out
*mi_uiout
;
1222 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1224 ui_out_redirect_pop
redirect_popper (mi_uiout
, mi
->event_channel
);
1226 target_terminal::scoped_restore_terminal_state term_state
;
1227 target_terminal::ours_for_output ();
1229 if (selection
& USER_SELECTED_INFERIOR
)
1230 print_selected_inferior (mi
->cli_uiout
);
1233 && (selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
)))
1235 print_selected_thread_frame (mi
->cli_uiout
, selection
);
1237 gdb_printf (mi
->event_channel
,
1238 "thread-selected,id=\"%d\"",
1241 if (tp
->state
!= THREAD_RUNNING
)
1243 if (has_stack_frames ())
1244 print_stack_frame_to_uiout (mi_uiout
, get_selected_frame (NULL
),
1249 gdb_flush (mi
->event_channel
);
1254 mi_interp::interp_ui_out ()
1256 return this->mi_uiout
;
1259 /* Do MI-specific logging actions; save raw_stdout, and change all
1260 the consoles to use the supplied ui-file(s). */
1263 mi_interp::set_logging (ui_file_up logfile
, bool logging_redirect
,
1264 bool debug_redirect
)
1266 struct mi_interp
*mi
= this;
1268 if (logfile
!= NULL
)
1270 mi
->saved_raw_stdout
= mi
->raw_stdout
;
1272 ui_file
*logfile_p
= logfile
.get ();
1273 mi
->logfile_holder
= std::move (logfile
);
1275 /* If something is not being redirected, then a tee containing both the
1276 logfile and stdout. */
1277 ui_file
*tee
= nullptr;
1278 if (!logging_redirect
|| !debug_redirect
)
1280 tee
= new tee_file (mi
->raw_stdout
, logfile_p
);
1281 mi
->stdout_holder
.reset (tee
);
1284 mi
->raw_stdout
= logging_redirect
? logfile_p
: tee
;
1288 mi
->logfile_holder
.reset ();
1289 mi
->stdout_holder
.reset ();
1290 mi
->raw_stdout
= mi
->saved_raw_stdout
;
1291 mi
->saved_raw_stdout
= nullptr;
1294 mi
->out
->set_raw (mi
->raw_stdout
);
1295 mi
->err
->set_raw (mi
->raw_stdout
);
1296 mi
->log
->set_raw (mi
->raw_stdout
);
1297 mi
->targ
->set_raw (mi
->raw_stdout
);
1298 mi
->event_channel
->set_raw (mi
->raw_stdout
);
1301 /* Factory for MI interpreters. */
1303 static struct interp
*
1304 mi_interp_factory (const char *name
)
1306 return new mi_interp (name
);
1309 void _initialize_mi_interp ();
1311 _initialize_mi_interp ()
1313 /* The various interpreter levels. */
1314 interp_factory_register (INTERP_MI2
, mi_interp_factory
);
1315 interp_factory_register (INTERP_MI3
, mi_interp_factory
);
1316 interp_factory_register (INTERP_MI4
, mi_interp_factory
);
1317 interp_factory_register (INTERP_MI
, mi_interp_factory
);
1319 gdb::observers::signal_received
.attach (mi_on_signal_received
, "mi-interp");
1320 gdb::observers::end_stepping_range
.attach (mi_on_end_stepping_range
,
1322 gdb::observers::signal_exited
.attach (mi_on_signal_exited
, "mi-interp");
1323 gdb::observers::exited
.attach (mi_on_exited
, "mi-interp");
1324 gdb::observers::no_history
.attach (mi_on_no_history
, "mi-interp");
1325 gdb::observers::new_thread
.attach (mi_new_thread
, "mi-interp");
1326 gdb::observers::thread_exit
.attach (mi_thread_exit
, "mi-interp");
1327 gdb::observers::inferior_added
.attach (mi_inferior_added
, "mi-interp");
1328 gdb::observers::inferior_appeared
.attach (mi_inferior_appeared
, "mi-interp");
1329 gdb::observers::inferior_exit
.attach (mi_inferior_exit
, "mi-interp");
1330 gdb::observers::inferior_removed
.attach (mi_inferior_removed
, "mi-interp");
1331 gdb::observers::record_changed
.attach (mi_record_changed
, "mi-interp");
1332 gdb::observers::normal_stop
.attach (mi_on_normal_stop
, "mi-interp");
1333 gdb::observers::target_resumed
.attach (mi_on_resume
, "mi-interp");
1334 gdb::observers::solib_loaded
.attach (mi_solib_loaded
, "mi-interp");
1335 gdb::observers::solib_unloaded
.attach (mi_solib_unloaded
, "mi-interp");
1336 gdb::observers::about_to_proceed
.attach (mi_about_to_proceed
, "mi-interp");
1337 gdb::observers::traceframe_changed
.attach (mi_traceframe_changed
,
1339 gdb::observers::tsv_created
.attach (mi_tsv_created
, "mi-interp");
1340 gdb::observers::tsv_deleted
.attach (mi_tsv_deleted
, "mi-interp");
1341 gdb::observers::tsv_modified
.attach (mi_tsv_modified
, "mi-interp");
1342 gdb::observers::breakpoint_created
.attach (mi_breakpoint_created
,
1344 gdb::observers::breakpoint_deleted
.attach (mi_breakpoint_deleted
,
1346 gdb::observers::breakpoint_modified
.attach (mi_breakpoint_modified
,
1348 gdb::observers::command_param_changed
.attach (mi_command_param_changed
,
1350 gdb::observers::command_error
.attach (mi_on_command_error
, "mi-interp");
1351 gdb::observers::memory_changed
.attach (mi_memory_changed
, "mi-interp");
1352 gdb::observers::sync_execution_done
.attach (mi_on_sync_execution_done
,
1354 gdb::observers::user_selected_context_changed
.attach
1355 (mi_user_selected_context_changed
, "mi-interp");