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_signal_exited (enum gdb_signal siggnal
);
65 static void mi_on_exited (int exitstatus
);
66 static void mi_on_normal_stop (struct bpstat
*bs
, int print_frame
);
67 static void mi_on_no_history (void);
69 static void mi_new_thread (struct thread_info
*t
);
70 static void mi_thread_exit (struct thread_info
*t
, int silent
);
71 static void mi_record_changed (struct inferior
*, int, const char *,
73 static void mi_inferior_added (struct inferior
*inf
);
74 static void mi_inferior_appeared (struct inferior
*inf
);
75 static void mi_inferior_exit (struct inferior
*inf
);
76 static void mi_inferior_removed (struct inferior
*inf
);
77 static void mi_on_resume (ptid_t ptid
);
78 static void mi_solib_loaded (struct so_list
*solib
);
79 static void mi_solib_unloaded (struct so_list
*solib
);
80 static void mi_about_to_proceed (void);
81 static void mi_traceframe_changed (int tfnum
, int tpnum
);
82 static void mi_tsv_created (const struct trace_state_variable
*tsv
);
83 static void mi_tsv_deleted (const struct trace_state_variable
*tsv
);
84 static void mi_tsv_modified (const struct trace_state_variable
*tsv
);
85 static void mi_breakpoint_created (struct breakpoint
*b
);
86 static void mi_breakpoint_deleted (struct breakpoint
*b
);
87 static void mi_breakpoint_modified (struct breakpoint
*b
);
88 static void mi_command_param_changed (const char *param
, const char *value
);
89 static void mi_memory_changed (struct inferior
*inf
, CORE_ADDR memaddr
,
90 ssize_t len
, const bfd_byte
*myaddr
);
91 static void mi_on_sync_execution_done (void);
93 /* Display the MI prompt. */
96 display_mi_prompt (struct mi_interp
*mi
)
98 struct ui
*ui
= current_ui
;
100 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
101 gdb_flush (mi
->raw_stdout
);
102 ui
->prompt_state
= PROMPTED
;
105 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
106 returns NULL otherwise. */
108 static struct mi_interp
*
109 as_mi_interp (struct interp
*interp
)
111 return dynamic_cast<mi_interp
*> (interp
);
114 /* Observer for the command_error notification. */
117 mi_on_command_error ()
119 mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
121 display_mi_prompt (mi
);
125 mi_interp::init (bool top_level
)
127 mi_interp
*mi
= this;
129 /* Store the current output channel, so that we can create a console
130 channel that encapsulates and prefixes all gdb_output-type bits
131 coming from the rest of the debugger. */
132 mi
->raw_stdout
= gdb_stdout
;
134 /* Create MI console channels, each with a different prefix so they
135 can be distinguished. */
136 mi
->out
= new mi_console_file (mi
->raw_stdout
, "~", '"');
137 mi
->err
= new mi_console_file (mi
->raw_stdout
, "&", '"');
139 mi
->targ
= new mi_console_file (mi
->raw_stdout
, "@", '"');
140 mi
->event_channel
= new mi_console_file (mi
->raw_stdout
, "=", 0);
141 mi
->mi_uiout
= mi_out_new (name ());
142 gdb_assert (mi
->mi_uiout
!= nullptr);
143 mi
->cli_uiout
= new cli_ui_out (mi
->out
);
147 /* The initial inferior is created before this function is called, so we
148 need to report it explicitly when initializing the top-level MI
151 This is also called when additional MI interpreters are added (using
152 the new-ui command), when multiple inferiors possibly exist, so we need
153 to use iteration to report all the inferiors. mi_inferior_added can't
154 be used, because it would print the event on all the other MI UIs. */
156 for (inferior
*inf
: all_inferiors ())
158 target_terminal::scoped_restore_terminal_state term_state
;
159 target_terminal::ours_for_output ();
161 gdb_printf (mi
->event_channel
,
162 "thread-group-added,id=\"i%d\"",
165 gdb_flush (mi
->event_channel
);
173 struct mi_interp
*mi
= this;
174 struct ui
*ui
= current_ui
;
176 /* As per hack note in mi_interpreter_init, swap in the output
178 gdb_setup_readline (0);
180 ui
->call_readline
= gdb_readline_no_editing_callback
;
181 ui
->input_handler
= mi_execute_command_input_handler
;
183 gdb_stdout
= mi
->out
;
184 /* Route error and log output through the MI. */
185 gdb_stderr
= mi
->err
;
186 gdb_stdlog
= mi
->log
;
187 /* Route target output through the MI. */
188 gdb_stdtarg
= mi
->targ
;
189 /* Route target error through the MI as well. */
190 gdb_stdtargerr
= mi
->targ
;
192 deprecated_show_load_progress
= mi_load_progress
;
196 mi_interp::suspend ()
198 gdb_disable_readline ();
202 mi_interp::exec (const char *command
)
204 mi_execute_command_wrapper (command
);
208 mi_cmd_interpreter_exec (const char *command
, char **argv
, int argc
)
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. */
527 /* Observer for the signal_received notification. */
530 mi_on_signal_received (enum gdb_signal siggnal
)
532 SWITCH_THRU_ALL_UIS ()
534 struct mi_interp
*mi
= find_mi_interp ();
539 print_signal_received_reason (mi
->mi_uiout
, siggnal
);
540 print_signal_received_reason (mi
->cli_uiout
, siggnal
);
544 /* Observer for the signal_exited notification. */
547 mi_on_signal_exited (enum gdb_signal siggnal
)
549 SWITCH_THRU_ALL_UIS ()
551 struct mi_interp
*mi
= find_mi_interp ();
556 print_signal_exited_reason (mi
->mi_uiout
, siggnal
);
557 print_signal_exited_reason (mi
->cli_uiout
, siggnal
);
561 /* Observer for the exited notification. */
564 mi_on_exited (int exitstatus
)
566 SWITCH_THRU_ALL_UIS ()
568 struct mi_interp
*mi
= find_mi_interp ();
573 print_exited_reason (mi
->mi_uiout
, exitstatus
);
574 print_exited_reason (mi
->cli_uiout
, exitstatus
);
578 /* Observer for the no_history notification. */
581 mi_on_no_history (void)
583 SWITCH_THRU_ALL_UIS ()
585 struct mi_interp
*mi
= find_mi_interp ();
590 print_no_history_reason (mi
->mi_uiout
);
591 print_no_history_reason (mi
->cli_uiout
);
596 mi_on_normal_stop_1 (struct bpstat
*bs
, int print_frame
)
598 /* Since this can be called when CLI command is executing,
599 using cli interpreter, be sure to use MI uiout for output,
600 not the current one. */
601 struct ui_out
*mi_uiout
= top_level_interpreter ()->interp_ui_out ();
602 struct mi_interp
*mi
= (struct mi_interp
*) top_level_interpreter ();
606 struct thread_info
*tp
;
608 struct interp
*console_interp
;
610 tp
= inferior_thread ();
612 if (tp
->thread_fsm () != nullptr
613 && tp
->thread_fsm ()->finished_p ())
615 enum async_reply_reason reason
;
617 reason
= tp
->thread_fsm ()->async_reply_reason ();
618 mi_uiout
->field_string ("reason", async_reason_lookup (reason
));
621 console_interp
= interp_lookup (current_ui
, INTERP_CONSOLE
);
622 /* We only want to print the displays once, and we want it to
623 look just how it would on the console, so we use this to
624 decide whether the MI stop should include them. */
625 bool console_print
= should_print_stop_to_console (console_interp
, tp
);
626 print_stop_event (mi_uiout
, !console_print
);
629 print_stop_event (mi
->cli_uiout
);
631 mi_uiout
->field_signed ("thread-id", tp
->global_num
);
634 ui_out_emit_list
list_emitter (mi_uiout
, "stopped-threads");
636 mi_uiout
->field_signed (NULL
, tp
->global_num
);
639 mi_uiout
->field_string ("stopped-threads", "all");
641 core
= target_core_of_thread (tp
->ptid
);
643 mi_uiout
->field_signed ("core", core
);
646 gdb_puts ("*stopped", mi
->raw_stdout
);
647 mi_out_put (mi_uiout
, mi
->raw_stdout
);
648 mi_out_rewind (mi_uiout
);
649 mi_print_timing_maybe (mi
->raw_stdout
);
650 gdb_puts ("\n", mi
->raw_stdout
);
651 gdb_flush (mi
->raw_stdout
);
655 mi_on_normal_stop (struct bpstat
*bs
, int print_frame
)
657 SWITCH_THRU_ALL_UIS ()
659 if (as_mi_interp (top_level_interpreter ()) == NULL
)
662 mi_on_normal_stop_1 (bs
, print_frame
);
667 mi_about_to_proceed (void)
669 /* Suppress output while calling an inferior function. */
671 if (inferior_ptid
!= null_ptid
)
673 struct thread_info
*tp
= inferior_thread ();
675 if (tp
->control
.in_infcall
)
682 /* When the element is non-zero, no MI notifications will be emitted in
683 response to the corresponding observers. */
685 struct mi_suppress_notification mi_suppress_notification
=
693 /* Emit notification on changing a traceframe. */
696 mi_traceframe_changed (int tfnum
, int tpnum
)
698 if (mi_suppress_notification
.traceframe
)
701 SWITCH_THRU_ALL_UIS ()
703 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
708 target_terminal::scoped_restore_terminal_state term_state
;
709 target_terminal::ours_for_output ();
712 gdb_printf (mi
->event_channel
, "traceframe-changed,"
713 "num=\"%d\",tracepoint=\"%d\"",
716 gdb_printf (mi
->event_channel
, "traceframe-changed,end");
718 gdb_flush (mi
->event_channel
);
722 /* Emit notification on creating a trace state variable. */
725 mi_tsv_created (const struct trace_state_variable
*tsv
)
727 SWITCH_THRU_ALL_UIS ()
729 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
734 target_terminal::scoped_restore_terminal_state term_state
;
735 target_terminal::ours_for_output ();
737 gdb_printf (mi
->event_channel
, "tsv-created,"
738 "name=\"%s\",initial=\"%s\"",
739 tsv
->name
.c_str (), plongest (tsv
->initial_value
));
741 gdb_flush (mi
->event_channel
);
745 /* Emit notification on deleting a trace state variable. */
748 mi_tsv_deleted (const struct trace_state_variable
*tsv
)
750 SWITCH_THRU_ALL_UIS ()
752 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
757 target_terminal::scoped_restore_terminal_state term_state
;
758 target_terminal::ours_for_output ();
761 gdb_printf (mi
->event_channel
, "tsv-deleted,"
762 "name=\"%s\"", tsv
->name
.c_str ());
764 gdb_printf (mi
->event_channel
, "tsv-deleted");
766 gdb_flush (mi
->event_channel
);
770 /* Emit notification on modifying a trace state variable. */
773 mi_tsv_modified (const struct trace_state_variable
*tsv
)
775 SWITCH_THRU_ALL_UIS ()
777 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
778 struct ui_out
*mi_uiout
;
783 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
785 target_terminal::scoped_restore_terminal_state term_state
;
786 target_terminal::ours_for_output ();
788 gdb_printf (mi
->event_channel
,
791 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
793 mi_uiout
->field_string ("name", tsv
->name
);
794 mi_uiout
->field_string ("initial",
795 plongest (tsv
->initial_value
));
796 if (tsv
->value_known
)
797 mi_uiout
->field_string ("current", plongest (tsv
->value
));
799 gdb_flush (mi
->event_channel
);
803 /* Print breakpoint BP on MI's event channel. */
806 mi_print_breakpoint_for_event (struct mi_interp
*mi
, breakpoint
*bp
)
808 ui_out
*mi_uiout
= mi
->interp_ui_out ();
810 /* We want the output from print_breakpoint to go to
811 mi->event_channel. One approach would be to just call
812 print_breakpoint, and then use mi_out_put to send the current
813 content of mi_uiout into mi->event_channel. However, that will
814 break if anything is output to mi_uiout prior to calling the
815 breakpoint_created notifications. So, we use
817 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
821 scoped_restore restore_uiout
822 = make_scoped_restore (¤t_uiout
, mi_uiout
);
824 print_breakpoint (bp
);
826 catch (const gdb_exception_error
&ex
)
828 exception_print (gdb_stderr
, ex
);
832 /* Emit notification about a created breakpoint. */
835 mi_breakpoint_created (struct breakpoint
*b
)
837 if (mi_suppress_notification
.breakpoint
)
843 SWITCH_THRU_ALL_UIS ()
845 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
850 target_terminal::scoped_restore_terminal_state term_state
;
851 target_terminal::ours_for_output ();
853 gdb_printf (mi
->event_channel
,
854 "breakpoint-created");
855 mi_print_breakpoint_for_event (mi
, b
);
857 gdb_flush (mi
->event_channel
);
861 /* Emit notification about deleted breakpoint. */
864 mi_breakpoint_deleted (struct breakpoint
*b
)
866 if (mi_suppress_notification
.breakpoint
)
872 SWITCH_THRU_ALL_UIS ()
874 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
879 target_terminal::scoped_restore_terminal_state term_state
;
880 target_terminal::ours_for_output ();
882 gdb_printf (mi
->event_channel
, "breakpoint-deleted,id=\"%d\"",
885 gdb_flush (mi
->event_channel
);
889 /* Emit notification about modified breakpoint. */
892 mi_breakpoint_modified (struct breakpoint
*b
)
894 if (mi_suppress_notification
.breakpoint
)
900 SWITCH_THRU_ALL_UIS ()
902 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
907 target_terminal::scoped_restore_terminal_state term_state
;
908 target_terminal::ours_for_output ();
909 gdb_printf (mi
->event_channel
,
910 "breakpoint-modified");
911 mi_print_breakpoint_for_event (mi
, b
);
913 gdb_flush (mi
->event_channel
);
918 mi_output_running (struct thread_info
*thread
)
920 SWITCH_THRU_ALL_UIS ()
922 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
927 gdb_printf (mi
->raw_stdout
,
928 "*running,thread-id=\"%d\"\n",
933 /* Return true if there are multiple inferiors loaded. This is used
934 for backwards compatibility -- if there's only one inferior, output
935 "all", otherwise, output each resumed thread individually. */
938 multiple_inferiors_p ()
941 for (inferior
*inf ATTRIBUTE_UNUSED
: all_non_exited_inferiors ())
952 mi_on_resume_1 (struct mi_interp
*mi
,
953 process_stratum_target
*targ
, ptid_t ptid
)
955 /* To cater for older frontends, emit ^running, but do it only once
956 per each command. We do it here, since at this point we know
957 that the target was successfully resumed, and in non-async mode,
958 we won't return back to MI interpreter code until the target
959 is done running, so delaying the output of "^running" until then
960 will make it impossible for frontend to know what's going on.
962 In future (MI3), we'll be outputting "^done" here. */
963 if (!running_result_record_printed
&& mi_proceeded
)
965 gdb_printf (mi
->raw_stdout
, "%s^running\n",
966 current_token
? current_token
: "");
969 /* Backwards compatibility. If doing a wildcard resume and there's
970 only one inferior, output "all", otherwise, output each resumed
971 thread individually. */
972 if ((ptid
== minus_one_ptid
|| ptid
.is_pid ())
973 && !multiple_inferiors_p ())
974 gdb_printf (mi
->raw_stdout
, "*running,thread-id=\"all\"\n");
976 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
977 mi_output_running (tp
);
979 if (!running_result_record_printed
&& mi_proceeded
)
981 running_result_record_printed
= 1;
982 /* This is what gdb used to do historically -- printing prompt
983 even if it cannot actually accept any input. This will be
984 surely removed for MI3, and may be removed even earlier. */
985 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
986 gdb_puts ("(gdb) \n", mi
->raw_stdout
);
988 gdb_flush (mi
->raw_stdout
);
992 mi_on_resume (ptid_t ptid
)
994 struct thread_info
*tp
= NULL
;
996 process_stratum_target
*target
= current_inferior ()->process_target ();
997 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
998 tp
= inferior_thread ();
1000 tp
= target
->find_thread (ptid
);
1002 /* Suppress output while calling an inferior function. */
1003 if (tp
->control
.in_infcall
)
1006 SWITCH_THRU_ALL_UIS ()
1008 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1013 target_terminal::scoped_restore_terminal_state term_state
;
1014 target_terminal::ours_for_output ();
1016 mi_on_resume_1 (mi
, target
, ptid
);
1020 /* See mi-interp.h. */
1023 mi_output_solib_attribs (ui_out
*uiout
, struct so_list
*solib
)
1025 struct gdbarch
*gdbarch
= target_gdbarch ();
1027 uiout
->field_string ("id", solib
->so_original_name
);
1028 uiout
->field_string ("target-name", solib
->so_original_name
);
1029 uiout
->field_string ("host-name", solib
->so_name
);
1030 uiout
->field_signed ("symbols-loaded", solib
->symbols_loaded
);
1031 if (!gdbarch_has_global_solist (target_gdbarch ()))
1032 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1034 ui_out_emit_list
list_emitter (uiout
, "ranges");
1035 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1036 if (solib
->addr_high
!= 0)
1038 uiout
->field_core_addr ("from", gdbarch
, solib
->addr_low
);
1039 uiout
->field_core_addr ("to", gdbarch
, solib
->addr_high
);
1044 mi_solib_loaded (struct so_list
*solib
)
1046 SWITCH_THRU_ALL_UIS ()
1048 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1049 struct ui_out
*uiout
;
1054 uiout
= top_level_interpreter ()->interp_ui_out ();
1056 target_terminal::scoped_restore_terminal_state term_state
;
1057 target_terminal::ours_for_output ();
1059 gdb_printf (mi
->event_channel
, "library-loaded");
1061 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1063 mi_output_solib_attribs (uiout
, solib
);
1065 gdb_flush (mi
->event_channel
);
1070 mi_solib_unloaded (struct so_list
*solib
)
1072 SWITCH_THRU_ALL_UIS ()
1074 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1075 struct ui_out
*uiout
;
1080 uiout
= top_level_interpreter ()->interp_ui_out ();
1082 target_terminal::scoped_restore_terminal_state term_state
;
1083 target_terminal::ours_for_output ();
1085 gdb_printf (mi
->event_channel
, "library-unloaded");
1087 ui_out_redirect_pop
redir (uiout
, mi
->event_channel
);
1089 uiout
->field_string ("id", solib
->so_original_name
);
1090 uiout
->field_string ("target-name", solib
->so_original_name
);
1091 uiout
->field_string ("host-name", solib
->so_name
);
1092 if (!gdbarch_has_global_solist (target_gdbarch ()))
1094 uiout
->field_fmt ("thread-group", "i%d", current_inferior ()->num
);
1097 gdb_flush (mi
->event_channel
);
1101 /* Emit notification about the command parameter change. */
1104 mi_command_param_changed (const char *param
, const char *value
)
1106 if (mi_suppress_notification
.cmd_param_changed
)
1109 SWITCH_THRU_ALL_UIS ()
1111 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1112 struct ui_out
*mi_uiout
;
1117 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1119 target_terminal::scoped_restore_terminal_state term_state
;
1120 target_terminal::ours_for_output ();
1122 gdb_printf (mi
->event_channel
, "cmd-param-changed");
1124 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1126 mi_uiout
->field_string ("param", param
);
1127 mi_uiout
->field_string ("value", value
);
1129 gdb_flush (mi
->event_channel
);
1133 /* Emit notification about the target memory change. */
1136 mi_memory_changed (struct inferior
*inferior
, CORE_ADDR memaddr
,
1137 ssize_t len
, const bfd_byte
*myaddr
)
1139 if (mi_suppress_notification
.memory
)
1142 SWITCH_THRU_ALL_UIS ()
1144 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1145 struct ui_out
*mi_uiout
;
1146 struct obj_section
*sec
;
1151 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1153 target_terminal::scoped_restore_terminal_state term_state
;
1154 target_terminal::ours_for_output ();
1156 gdb_printf (mi
->event_channel
, "memory-changed");
1158 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
1160 mi_uiout
->field_fmt ("thread-group", "i%d", inferior
->num
);
1161 mi_uiout
->field_core_addr ("addr", target_gdbarch (), memaddr
);
1162 mi_uiout
->field_string ("len", hex_string (len
));
1164 /* Append 'type=code' into notification if MEMADDR falls in the range of
1165 sections contain code. */
1166 sec
= find_pc_section (memaddr
);
1167 if (sec
!= NULL
&& sec
->objfile
!= NULL
)
1169 flagword flags
= bfd_section_flags (sec
->the_bfd_section
);
1171 if (flags
& SEC_CODE
)
1172 mi_uiout
->field_string ("type", "code");
1175 gdb_flush (mi
->event_channel
);
1179 /* Emit an event when the selection context (inferior, thread, frame)
1183 mi_user_selected_context_changed (user_selected_what selection
)
1185 struct thread_info
*tp
;
1187 /* Don't send an event if we're responding to an MI command. */
1188 if (mi_suppress_notification
.user_selected_context
)
1191 if (inferior_ptid
!= null_ptid
)
1192 tp
= inferior_thread ();
1196 SWITCH_THRU_ALL_UIS ()
1198 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
1199 struct ui_out
*mi_uiout
;
1204 mi_uiout
= top_level_interpreter ()->interp_ui_out ();
1206 ui_out_redirect_pop
redirect_popper (mi_uiout
, mi
->event_channel
);
1208 target_terminal::scoped_restore_terminal_state term_state
;
1209 target_terminal::ours_for_output ();
1211 if (selection
& USER_SELECTED_INFERIOR
)
1212 print_selected_inferior (mi
->cli_uiout
);
1215 && (selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
)))
1217 print_selected_thread_frame (mi
->cli_uiout
, selection
);
1219 gdb_printf (mi
->event_channel
,
1220 "thread-selected,id=\"%d\"",
1223 if (tp
->state
!= THREAD_RUNNING
)
1225 if (has_stack_frames ())
1226 print_stack_frame_to_uiout (mi_uiout
, get_selected_frame (NULL
),
1231 gdb_flush (mi
->event_channel
);
1236 mi_interp::interp_ui_out ()
1238 return this->mi_uiout
;
1241 /* Do MI-specific logging actions; save raw_stdout, and change all
1242 the consoles to use the supplied ui-file(s). */
1245 mi_interp::set_logging (ui_file_up logfile
, bool logging_redirect
,
1246 bool debug_redirect
)
1248 struct mi_interp
*mi
= this;
1250 if (logfile
!= NULL
)
1252 mi
->saved_raw_stdout
= mi
->raw_stdout
;
1254 ui_file
*logfile_p
= logfile
.get ();
1255 mi
->logfile_holder
= std::move (logfile
);
1257 /* If something is not being redirected, then a tee containing both the
1258 logfile and stdout. */
1259 ui_file
*tee
= nullptr;
1260 if (!logging_redirect
|| !debug_redirect
)
1262 tee
= new tee_file (mi
->raw_stdout
, logfile_p
);
1263 mi
->stdout_holder
.reset (tee
);
1266 mi
->raw_stdout
= logging_redirect
? logfile_p
: tee
;
1270 mi
->logfile_holder
.reset ();
1271 mi
->stdout_holder
.reset ();
1272 mi
->raw_stdout
= mi
->saved_raw_stdout
;
1273 mi
->saved_raw_stdout
= nullptr;
1276 mi
->out
->set_raw (mi
->raw_stdout
);
1277 mi
->err
->set_raw (mi
->raw_stdout
);
1278 mi
->log
->set_raw (mi
->raw_stdout
);
1279 mi
->targ
->set_raw (mi
->raw_stdout
);
1280 mi
->event_channel
->set_raw (mi
->raw_stdout
);
1283 /* Factory for MI interpreters. */
1285 static struct interp
*
1286 mi_interp_factory (const char *name
)
1288 return new mi_interp (name
);
1291 void _initialize_mi_interp ();
1293 _initialize_mi_interp ()
1295 /* The various interpreter levels. */
1296 interp_factory_register (INTERP_MI2
, mi_interp_factory
);
1297 interp_factory_register (INTERP_MI3
, mi_interp_factory
);
1298 interp_factory_register (INTERP_MI4
, mi_interp_factory
);
1299 interp_factory_register (INTERP_MI
, mi_interp_factory
);
1301 gdb::observers::signal_received
.attach (mi_on_signal_received
, "mi-interp");
1302 gdb::observers::signal_exited
.attach (mi_on_signal_exited
, "mi-interp");
1303 gdb::observers::exited
.attach (mi_on_exited
, "mi-interp");
1304 gdb::observers::no_history
.attach (mi_on_no_history
, "mi-interp");
1305 gdb::observers::new_thread
.attach (mi_new_thread
, "mi-interp");
1306 gdb::observers::thread_exit
.attach (mi_thread_exit
, "mi-interp");
1307 gdb::observers::inferior_added
.attach (mi_inferior_added
, "mi-interp");
1308 gdb::observers::inferior_appeared
.attach (mi_inferior_appeared
, "mi-interp");
1309 gdb::observers::inferior_exit
.attach (mi_inferior_exit
, "mi-interp");
1310 gdb::observers::inferior_removed
.attach (mi_inferior_removed
, "mi-interp");
1311 gdb::observers::record_changed
.attach (mi_record_changed
, "mi-interp");
1312 gdb::observers::normal_stop
.attach (mi_on_normal_stop
, "mi-interp");
1313 gdb::observers::target_resumed
.attach (mi_on_resume
, "mi-interp");
1314 gdb::observers::solib_loaded
.attach (mi_solib_loaded
, "mi-interp");
1315 gdb::observers::solib_unloaded
.attach (mi_solib_unloaded
, "mi-interp");
1316 gdb::observers::about_to_proceed
.attach (mi_about_to_proceed
, "mi-interp");
1317 gdb::observers::traceframe_changed
.attach (mi_traceframe_changed
,
1319 gdb::observers::tsv_created
.attach (mi_tsv_created
, "mi-interp");
1320 gdb::observers::tsv_deleted
.attach (mi_tsv_deleted
, "mi-interp");
1321 gdb::observers::tsv_modified
.attach (mi_tsv_modified
, "mi-interp");
1322 gdb::observers::breakpoint_created
.attach (mi_breakpoint_created
,
1324 gdb::observers::breakpoint_deleted
.attach (mi_breakpoint_deleted
,
1326 gdb::observers::breakpoint_modified
.attach (mi_breakpoint_modified
,
1328 gdb::observers::command_param_changed
.attach (mi_command_param_changed
,
1330 gdb::observers::command_error
.attach (mi_on_command_error
, "mi-interp");
1331 gdb::observers::memory_changed
.attach (mi_memory_changed
, "mi-interp");
1332 gdb::observers::sync_execution_done
.attach (mi_on_sync_execution_done
,
1334 gdb::observers::user_selected_context_changed
.attach
1335 (mi_user_selected_context_changed
, "mi-interp");