nm: Add --quiet to suppress "no symbols" diagnostic
[binutils-gdb.git] / gdb / mi / mi-interp.c
blob4c5612ee9ee0d3377dc9d1526b09c2c775007e57
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright (C) 2002-2021 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/>. */
20 #include "defs.h"
22 #include "mi-interp.h"
24 #include "interps.h"
25 #include "event-top.h"
26 #include "gdbsupport/event-loop.h"
27 #include "inferior.h"
28 #include "infrun.h"
29 #include "ui-out.h"
30 #include "top.h"
31 #include "mi-main.h"
32 #include "mi-cmds.h"
33 #include "mi-out.h"
34 #include "mi-console.h"
35 #include "mi-common.h"
36 #include "observable.h"
37 #include "gdbthread.h"
38 #include "solist.h"
39 #include "objfiles.h"
40 #include "tracepoint.h"
41 #include "cli-out.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
47 interpreter. */
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 bpstats *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 *,
73 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. */
96 static void
97 display_mi_prompt (struct mi_interp *mi)
99 struct ui *ui = current_ui;
101 fputs_unfiltered ("(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 void
116 mi_interp::init (bool top_level)
118 mi_interp *mi = this;
120 /* Store the current output channel, so that we can create a console
121 channel that encapsulates and prefixes all gdb_output-type bits
122 coming from the rest of the debugger. */
123 mi->raw_stdout = gdb_stdout;
125 /* Create MI console channels, each with a different prefix so they
126 can be distinguished. */
127 mi->out = new mi_console_file (mi->raw_stdout, "~", '"');
128 mi->err = new mi_console_file (mi->raw_stdout, "&", '"');
129 mi->log = mi->err;
130 mi->targ = new mi_console_file (mi->raw_stdout, "@", '"');
131 mi->event_channel = new mi_console_file (mi->raw_stdout, "=", 0);
132 mi->mi_uiout = mi_out_new (name ());
133 gdb_assert (mi->mi_uiout != nullptr);
134 mi->cli_uiout = cli_out_new (mi->out);
136 if (top_level)
138 /* The initial inferior is created before this function is called, so we
139 need to report it explicitly when initializing the top-level MI
140 interpreter.
142 This is also called when additional MI interpreters are added (using
143 the new-ui command), when multiple inferiors possibly exist, so we need
144 to use iteration to report all the inferiors. mi_inferior_added can't
145 be used, because it would print the event on all the other MI UIs. */
147 for (inferior *inf : all_inferiors ())
149 target_terminal::scoped_restore_terminal_state term_state;
150 target_terminal::ours_for_output ();
152 fprintf_unfiltered (mi->event_channel,
153 "thread-group-added,id=\"i%d\"",
154 inf->num);
156 gdb_flush (mi->event_channel);
161 void
162 mi_interp::resume ()
164 struct mi_interp *mi = this;
165 struct ui *ui = current_ui;
167 /* As per hack note in mi_interpreter_init, swap in the output
168 channels... */
169 gdb_setup_readline (0);
171 ui->call_readline = gdb_readline_no_editing_callback;
172 ui->input_handler = mi_execute_command_input_handler;
174 gdb_stdout = mi->out;
175 /* Route error and log output through the MI. */
176 gdb_stderr = mi->err;
177 gdb_stdlog = mi->log;
178 /* Route target output through the MI. */
179 gdb_stdtarg = mi->targ;
180 /* Route target error through the MI as well. */
181 gdb_stdtargerr = mi->targ;
183 /* Replace all the hooks that we know about. There really needs to
184 be a better way of doing this... */
185 clear_interpreter_hooks ();
187 deprecated_show_load_progress = mi_load_progress;
190 void
191 mi_interp::suspend ()
193 gdb_disable_readline ();
196 gdb_exception
197 mi_interp::exec (const char *command)
199 mi_execute_command_wrapper (command);
200 return gdb_exception ();
203 void
204 mi_cmd_interpreter_exec (const char *command, char **argv, int argc)
206 struct interp *interp_to_use;
207 int i;
209 if (argc < 2)
210 error (_("-interpreter-exec: "
211 "Usage: -interpreter-exec interp command"));
213 interp_to_use = interp_lookup (current_ui, argv[0]);
214 if (interp_to_use == NULL)
215 error (_("-interpreter-exec: could not find interpreter \"%s\""),
216 argv[0]);
218 /* Note that unlike the CLI version of this command, we don't
219 actually set INTERP_TO_USE as the current interpreter, as we
220 still want gdb_stdout, etc. to point at MI streams. */
222 /* Insert the MI out hooks, making sure to also call the
223 interpreter's hooks if it has any. */
224 /* KRS: We shouldn't need this... Events should be installed and
225 they should just ALWAYS fire something out down the MI
226 channel. */
227 mi_insert_notify_hooks ();
229 /* Now run the code. */
231 SCOPE_EXIT
233 mi_remove_notify_hooks ();
236 for (i = 1; i < argc; i++)
238 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
240 if (e.reason < 0)
241 error ("%s", e.what ());
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. */
252 static void
253 mi_insert_notify_hooks (void)
255 deprecated_query_hook = mi_interp_query_hook;
258 static void
259 mi_remove_notify_hooks (void)
261 deprecated_query_hook = NULL;
264 static int
265 mi_interp_query_hook (const char *ctlstr, va_list ap)
267 return 1;
270 static void
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. */
280 static void
281 mi_on_sync_execution_done (void)
283 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
285 if (mi == NULL)
286 return;
288 /* If MI is sync, then output the MI prompt now, indicating we're
289 ready for further input. */
290 if (!mi_async_p ())
291 display_mi_prompt (mi);
294 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
296 static void
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
310 stops. */
311 if (ui->prompt_state == PROMPT_NEEDED)
312 display_mi_prompt (mi);
315 void
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);
328 static void
329 mi_new_thread (struct thread_info *t)
331 SWITCH_THRU_ALL_UIS ()
333 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
335 if (mi == NULL)
336 continue;
338 target_terminal::scoped_restore_terminal_state term_state;
339 target_terminal::ours_for_output ();
341 fprintf_unfiltered (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);
348 static void
349 mi_thread_exit (struct thread_info *t, int silent)
351 if (silent)
352 return;
354 SWITCH_THRU_ALL_UIS ()
356 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
358 if (mi == NULL)
359 continue;
361 target_terminal::scoped_restore_terminal_state term_state;
362 target_terminal::ours_for_output ();
363 fprintf_unfiltered (mi->event_channel,
364 "thread-exited,id=\"%d\",group-id=\"i%d\"",
365 t->global_num, t->inf->num);
366 gdb_flush (mi->event_channel);
370 /* Emit notification on changing the state of record. */
372 static void
373 mi_record_changed (struct inferior *inferior, int started, const char *method,
374 const char *format)
376 SWITCH_THRU_ALL_UIS ()
378 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
380 if (mi == NULL)
381 continue;
383 target_terminal::scoped_restore_terminal_state term_state;
384 target_terminal::ours_for_output ();
386 if (started)
388 if (format != NULL)
390 fprintf_unfiltered (mi->event_channel,
391 "record-started,thread-group=\"i%d\","
392 "method=\"%s\",format=\"%s\"",
393 inferior->num, method, format);
395 else
397 fprintf_unfiltered (mi->event_channel,
398 "record-started,thread-group=\"i%d\","
399 "method=\"%s\"",
400 inferior->num, method);
403 else
405 fprintf_unfiltered (mi->event_channel,
406 "record-stopped,thread-group=\"i%d\"",
407 inferior->num);
410 gdb_flush (mi->event_channel);
414 static void
415 mi_inferior_added (struct inferior *inf)
417 SWITCH_THRU_ALL_UIS ()
419 struct interp *interp;
420 struct mi_interp *mi;
422 /* We'll be called once for the initial inferior, before the top
423 level interpreter is set. */
424 interp = top_level_interpreter ();
425 if (interp == NULL)
426 continue;
428 mi = as_mi_interp (interp);
429 if (mi == NULL)
430 continue;
432 target_terminal::scoped_restore_terminal_state term_state;
433 target_terminal::ours_for_output ();
435 fprintf_unfiltered (mi->event_channel,
436 "thread-group-added,id=\"i%d\"",
437 inf->num);
438 gdb_flush (mi->event_channel);
442 static void
443 mi_inferior_appeared (struct inferior *inf)
445 SWITCH_THRU_ALL_UIS ()
447 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
449 if (mi == NULL)
450 continue;
452 target_terminal::scoped_restore_terminal_state term_state;
453 target_terminal::ours_for_output ();
455 fprintf_unfiltered (mi->event_channel,
456 "thread-group-started,id=\"i%d\",pid=\"%d\"",
457 inf->num, inf->pid);
458 gdb_flush (mi->event_channel);
462 static void
463 mi_inferior_exit (struct inferior *inf)
465 SWITCH_THRU_ALL_UIS ()
467 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
469 if (mi == NULL)
470 continue;
472 target_terminal::scoped_restore_terminal_state term_state;
473 target_terminal::ours_for_output ();
475 if (inf->has_exit_code)
476 fprintf_unfiltered (mi->event_channel,
477 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
478 inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
479 else
480 fprintf_unfiltered (mi->event_channel,
481 "thread-group-exited,id=\"i%d\"", inf->num);
483 gdb_flush (mi->event_channel);
487 static void
488 mi_inferior_removed (struct inferior *inf)
490 SWITCH_THRU_ALL_UIS ()
492 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
494 if (mi == NULL)
495 continue;
497 target_terminal::scoped_restore_terminal_state term_state;
498 target_terminal::ours_for_output ();
500 fprintf_unfiltered (mi->event_channel,
501 "thread-group-removed,id=\"i%d\"",
502 inf->num);
503 gdb_flush (mi->event_channel);
507 /* Return the MI interpreter, if it is active -- either because it's
508 the top-level interpreter or the interpreter executing the current
509 command. Returns NULL if the MI interpreter is not being used. */
511 static struct mi_interp *
512 find_mi_interp (void)
514 struct mi_interp *mi;
516 mi = as_mi_interp (top_level_interpreter ());
517 if (mi != NULL)
518 return mi;
520 mi = as_mi_interp (command_interp ());
521 if (mi != NULL)
522 return mi;
524 return NULL;
527 /* Observers for several run control events that print why the
528 inferior has stopped to both the MI event channel and to the MI
529 console. If the MI interpreter is not active, print nothing. */
531 /* Observer for the signal_received notification. */
533 static void
534 mi_on_signal_received (enum gdb_signal siggnal)
536 SWITCH_THRU_ALL_UIS ()
538 struct mi_interp *mi = find_mi_interp ();
540 if (mi == NULL)
541 continue;
543 print_signal_received_reason (mi->mi_uiout, siggnal);
544 print_signal_received_reason (mi->cli_uiout, siggnal);
548 /* Observer for the end_stepping_range notification. */
550 static void
551 mi_on_end_stepping_range (void)
553 SWITCH_THRU_ALL_UIS ()
555 struct mi_interp *mi = find_mi_interp ();
557 if (mi == NULL)
558 continue;
560 print_end_stepping_range_reason (mi->mi_uiout);
561 print_end_stepping_range_reason (mi->cli_uiout);
565 /* Observer for the signal_exited notification. */
567 static void
568 mi_on_signal_exited (enum gdb_signal siggnal)
570 SWITCH_THRU_ALL_UIS ()
572 struct mi_interp *mi = find_mi_interp ();
574 if (mi == NULL)
575 continue;
577 print_signal_exited_reason (mi->mi_uiout, siggnal);
578 print_signal_exited_reason (mi->cli_uiout, siggnal);
582 /* Observer for the exited notification. */
584 static void
585 mi_on_exited (int exitstatus)
587 SWITCH_THRU_ALL_UIS ()
589 struct mi_interp *mi = find_mi_interp ();
591 if (mi == NULL)
592 continue;
594 print_exited_reason (mi->mi_uiout, exitstatus);
595 print_exited_reason (mi->cli_uiout, exitstatus);
599 /* Observer for the no_history notification. */
601 static void
602 mi_on_no_history (void)
604 SWITCH_THRU_ALL_UIS ()
606 struct mi_interp *mi = find_mi_interp ();
608 if (mi == NULL)
609 continue;
611 print_no_history_reason (mi->mi_uiout);
612 print_no_history_reason (mi->cli_uiout);
616 static void
617 mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
619 /* Since this can be called when CLI command is executing,
620 using cli interpreter, be sure to use MI uiout for output,
621 not the current one. */
622 struct ui_out *mi_uiout = top_level_interpreter ()->interp_ui_out ();
623 struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
625 if (print_frame)
627 struct thread_info *tp;
628 int core;
629 struct interp *console_interp;
631 tp = inferior_thread ();
633 if (tp->thread_fsm != NULL
634 && tp->thread_fsm->finished_p ())
636 enum async_reply_reason reason;
638 reason = tp->thread_fsm->async_reply_reason ();
639 mi_uiout->field_string ("reason", async_reason_lookup (reason));
642 console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
643 /* We only want to print the displays once, and we want it to
644 look just how it would on the console, so we use this to
645 decide whether the MI stop should include them. */
646 bool console_print = should_print_stop_to_console (console_interp, tp);
647 print_stop_event (mi_uiout, !console_print);
649 if (console_print)
650 print_stop_event (mi->cli_uiout);
652 mi_uiout->field_signed ("thread-id", tp->global_num);
653 if (non_stop)
655 ui_out_emit_list list_emitter (mi_uiout, "stopped-threads");
657 mi_uiout->field_signed (NULL, tp->global_num);
659 else
660 mi_uiout->field_string ("stopped-threads", "all");
662 core = target_core_of_thread (tp->ptid);
663 if (core != -1)
664 mi_uiout->field_signed ("core", core);
667 fputs_unfiltered ("*stopped", mi->raw_stdout);
668 mi_out_put (mi_uiout, mi->raw_stdout);
669 mi_out_rewind (mi_uiout);
670 mi_print_timing_maybe (mi->raw_stdout);
671 fputs_unfiltered ("\n", mi->raw_stdout);
672 gdb_flush (mi->raw_stdout);
675 static void
676 mi_on_normal_stop (struct bpstats *bs, int print_frame)
678 SWITCH_THRU_ALL_UIS ()
680 if (as_mi_interp (top_level_interpreter ()) == NULL)
681 continue;
683 mi_on_normal_stop_1 (bs, print_frame);
687 static void
688 mi_about_to_proceed (void)
690 /* Suppress output while calling an inferior function. */
692 if (inferior_ptid != null_ptid)
694 struct thread_info *tp = inferior_thread ();
696 if (tp->control.in_infcall)
697 return;
700 mi_proceeded = 1;
703 /* When the element is non-zero, no MI notifications will be emitted in
704 response to the corresponding observers. */
706 struct mi_suppress_notification mi_suppress_notification =
714 /* Emit notification on changing a traceframe. */
716 static void
717 mi_traceframe_changed (int tfnum, int tpnum)
719 if (mi_suppress_notification.traceframe)
720 return;
722 SWITCH_THRU_ALL_UIS ()
724 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
726 if (mi == NULL)
727 continue;
729 target_terminal::scoped_restore_terminal_state term_state;
730 target_terminal::ours_for_output ();
732 if (tfnum >= 0)
733 fprintf_unfiltered (mi->event_channel, "traceframe-changed,"
734 "num=\"%d\",tracepoint=\"%d\"",
735 tfnum, tpnum);
736 else
737 fprintf_unfiltered (mi->event_channel, "traceframe-changed,end");
739 gdb_flush (mi->event_channel);
743 /* Emit notification on creating a trace state variable. */
745 static void
746 mi_tsv_created (const struct trace_state_variable *tsv)
748 SWITCH_THRU_ALL_UIS ()
750 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
752 if (mi == NULL)
753 continue;
755 target_terminal::scoped_restore_terminal_state term_state;
756 target_terminal::ours_for_output ();
758 fprintf_unfiltered (mi->event_channel, "tsv-created,"
759 "name=\"%s\",initial=\"%s\"",
760 tsv->name.c_str (), plongest (tsv->initial_value));
762 gdb_flush (mi->event_channel);
766 /* Emit notification on deleting a trace state variable. */
768 static void
769 mi_tsv_deleted (const struct trace_state_variable *tsv)
771 SWITCH_THRU_ALL_UIS ()
773 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
775 if (mi == NULL)
776 continue;
778 target_terminal::scoped_restore_terminal_state term_state;
779 target_terminal::ours_for_output ();
781 if (tsv != NULL)
782 fprintf_unfiltered (mi->event_channel, "tsv-deleted,"
783 "name=\"%s\"", tsv->name.c_str ());
784 else
785 fprintf_unfiltered (mi->event_channel, "tsv-deleted");
787 gdb_flush (mi->event_channel);
791 /* Emit notification on modifying a trace state variable. */
793 static void
794 mi_tsv_modified (const struct trace_state_variable *tsv)
796 SWITCH_THRU_ALL_UIS ()
798 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
799 struct ui_out *mi_uiout;
801 if (mi == NULL)
802 continue;
804 mi_uiout = top_level_interpreter ()->interp_ui_out ();
806 target_terminal::scoped_restore_terminal_state term_state;
807 target_terminal::ours_for_output ();
809 fprintf_unfiltered (mi->event_channel,
810 "tsv-modified");
812 mi_uiout->redirect (mi->event_channel);
814 mi_uiout->field_string ("name", tsv->name);
815 mi_uiout->field_string ("initial",
816 plongest (tsv->initial_value));
817 if (tsv->value_known)
818 mi_uiout->field_string ("current", plongest (tsv->value));
820 mi_uiout->redirect (NULL);
822 gdb_flush (mi->event_channel);
826 /* Print breakpoint BP on MI's event channel. */
828 static void
829 mi_print_breakpoint_for_event (struct mi_interp *mi, breakpoint *bp)
831 ui_out *mi_uiout = mi->interp_ui_out ();
833 /* We want the output from print_breakpoint to go to
834 mi->event_channel. One approach would be to just call
835 print_breakpoint, and then use mi_out_put to send the current
836 content of mi_uiout into mi->event_channel. However, that will
837 break if anything is output to mi_uiout prior to calling the
838 breakpoint_created notifications. So, we use
839 ui_out_redirect. */
840 mi_uiout->redirect (mi->event_channel);
844 scoped_restore restore_uiout
845 = make_scoped_restore (&current_uiout, mi_uiout);
847 print_breakpoint (bp);
849 catch (const gdb_exception &ex)
851 exception_print (gdb_stderr, ex);
854 mi_uiout->redirect (NULL);
857 /* Emit notification about a created breakpoint. */
859 static void
860 mi_breakpoint_created (struct breakpoint *b)
862 if (mi_suppress_notification.breakpoint)
863 return;
865 if (b->number <= 0)
866 return;
868 SWITCH_THRU_ALL_UIS ()
870 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
872 if (mi == NULL)
873 continue;
875 target_terminal::scoped_restore_terminal_state term_state;
876 target_terminal::ours_for_output ();
878 fprintf_unfiltered (mi->event_channel,
879 "breakpoint-created");
880 mi_print_breakpoint_for_event (mi, b);
882 gdb_flush (mi->event_channel);
886 /* Emit notification about deleted breakpoint. */
888 static void
889 mi_breakpoint_deleted (struct breakpoint *b)
891 if (mi_suppress_notification.breakpoint)
892 return;
894 if (b->number <= 0)
895 return;
897 SWITCH_THRU_ALL_UIS ()
899 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
901 if (mi == NULL)
902 continue;
904 target_terminal::scoped_restore_terminal_state term_state;
905 target_terminal::ours_for_output ();
907 fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
908 b->number);
910 gdb_flush (mi->event_channel);
914 /* Emit notification about modified breakpoint. */
916 static void
917 mi_breakpoint_modified (struct breakpoint *b)
919 if (mi_suppress_notification.breakpoint)
920 return;
922 if (b->number <= 0)
923 return;
925 SWITCH_THRU_ALL_UIS ()
927 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
929 if (mi == NULL)
930 continue;
932 target_terminal::scoped_restore_terminal_state term_state;
933 target_terminal::ours_for_output ();
934 fprintf_unfiltered (mi->event_channel,
935 "breakpoint-modified");
936 mi_print_breakpoint_for_event (mi, b);
938 gdb_flush (mi->event_channel);
942 static void
943 mi_output_running (struct thread_info *thread)
945 SWITCH_THRU_ALL_UIS ()
947 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
949 if (mi == NULL)
950 continue;
952 fprintf_unfiltered (mi->raw_stdout,
953 "*running,thread-id=\"%d\"\n",
954 thread->global_num);
958 /* Return true if there are multiple inferiors loaded. This is used
959 for backwards compatibility -- if there's only one inferior, output
960 "all", otherwise, output each resumed thread individually. */
962 static bool
963 multiple_inferiors_p ()
965 int count = 0;
966 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
968 count++;
969 if (count > 1)
970 return true;
973 return false;
976 static void
977 mi_on_resume_1 (struct mi_interp *mi,
978 process_stratum_target *targ, ptid_t ptid)
980 /* To cater for older frontends, emit ^running, but do it only once
981 per each command. We do it here, since at this point we know
982 that the target was successfully resumed, and in non-async mode,
983 we won't return back to MI interpreter code until the target
984 is done running, so delaying the output of "^running" until then
985 will make it impossible for frontend to know what's going on.
987 In future (MI3), we'll be outputting "^done" here. */
988 if (!running_result_record_printed && mi_proceeded)
990 fprintf_unfiltered (mi->raw_stdout, "%s^running\n",
991 current_token ? current_token : "");
994 /* Backwards compatibility. If doing a wildcard resume and there's
995 only one inferior, output "all", otherwise, output each resumed
996 thread individually. */
997 if ((ptid == minus_one_ptid || ptid.is_pid ())
998 && !multiple_inferiors_p ())
999 fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
1000 else
1001 for (thread_info *tp : all_non_exited_threads (targ, ptid))
1002 mi_output_running (tp);
1004 if (!running_result_record_printed && mi_proceeded)
1006 running_result_record_printed = 1;
1007 /* This is what gdb used to do historically -- printing prompt
1008 even if it cannot actually accept any input. This will be
1009 surely removed for MI3, and may be removed even earlier. */
1010 if (current_ui->prompt_state == PROMPT_BLOCKED)
1011 fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
1013 gdb_flush (mi->raw_stdout);
1016 static void
1017 mi_on_resume (ptid_t ptid)
1019 struct thread_info *tp = NULL;
1021 process_stratum_target *target = current_inferior ()->process_target ();
1022 if (ptid == minus_one_ptid || ptid.is_pid ())
1023 tp = inferior_thread ();
1024 else
1025 tp = find_thread_ptid (target, ptid);
1027 /* Suppress output while calling an inferior function. */
1028 if (tp->control.in_infcall)
1029 return;
1031 SWITCH_THRU_ALL_UIS ()
1033 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1035 if (mi == NULL)
1036 continue;
1038 target_terminal::scoped_restore_terminal_state term_state;
1039 target_terminal::ours_for_output ();
1041 mi_on_resume_1 (mi, target, ptid);
1045 /* See mi-interp.h. */
1047 void
1048 mi_output_solib_attribs (ui_out *uiout, struct so_list *solib)
1050 struct gdbarch *gdbarch = target_gdbarch ();
1052 uiout->field_string ("id", solib->so_original_name);
1053 uiout->field_string ("target-name", solib->so_original_name);
1054 uiout->field_string ("host-name", solib->so_name);
1055 uiout->field_signed ("symbols-loaded", solib->symbols_loaded);
1056 if (!gdbarch_has_global_solist (target_gdbarch ()))
1057 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
1059 ui_out_emit_list list_emitter (uiout, "ranges");
1060 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1061 if (solib->addr_high != 0)
1063 uiout->field_core_addr ("from", gdbarch, solib->addr_low);
1064 uiout->field_core_addr ("to", gdbarch, solib->addr_high);
1068 static void
1069 mi_solib_loaded (struct so_list *solib)
1071 SWITCH_THRU_ALL_UIS ()
1073 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1074 struct ui_out *uiout;
1076 if (mi == NULL)
1077 continue;
1079 uiout = top_level_interpreter ()->interp_ui_out ();
1081 target_terminal::scoped_restore_terminal_state term_state;
1082 target_terminal::ours_for_output ();
1084 fprintf_unfiltered (mi->event_channel, "library-loaded");
1086 uiout->redirect (mi->event_channel);
1088 mi_output_solib_attribs (uiout, solib);
1090 uiout->redirect (NULL);
1092 gdb_flush (mi->event_channel);
1096 static void
1097 mi_solib_unloaded (struct so_list *solib)
1099 SWITCH_THRU_ALL_UIS ()
1101 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1102 struct ui_out *uiout;
1104 if (mi == NULL)
1105 continue;
1107 uiout = top_level_interpreter ()->interp_ui_out ();
1109 target_terminal::scoped_restore_terminal_state term_state;
1110 target_terminal::ours_for_output ();
1112 fprintf_unfiltered (mi->event_channel, "library-unloaded");
1114 uiout->redirect (mi->event_channel);
1116 uiout->field_string ("id", solib->so_original_name);
1117 uiout->field_string ("target-name", solib->so_original_name);
1118 uiout->field_string ("host-name", solib->so_name);
1119 if (!gdbarch_has_global_solist (target_gdbarch ()))
1121 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
1124 uiout->redirect (NULL);
1126 gdb_flush (mi->event_channel);
1130 /* Emit notification about the command parameter change. */
1132 static void
1133 mi_command_param_changed (const char *param, const char *value)
1135 if (mi_suppress_notification.cmd_param_changed)
1136 return;
1138 SWITCH_THRU_ALL_UIS ()
1140 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1141 struct ui_out *mi_uiout;
1143 if (mi == NULL)
1144 continue;
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 fprintf_unfiltered (mi->event_channel, "cmd-param-changed");
1153 mi_uiout->redirect (mi->event_channel);
1155 mi_uiout->field_string ("param", param);
1156 mi_uiout->field_string ("value", value);
1158 mi_uiout->redirect (NULL);
1160 gdb_flush (mi->event_channel);
1164 /* Emit notification about the target memory change. */
1166 static void
1167 mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
1168 ssize_t len, const bfd_byte *myaddr)
1170 if (mi_suppress_notification.memory)
1171 return;
1173 SWITCH_THRU_ALL_UIS ()
1175 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1176 struct ui_out *mi_uiout;
1177 struct obj_section *sec;
1179 if (mi == NULL)
1180 continue;
1182 mi_uiout = top_level_interpreter ()->interp_ui_out ();
1184 target_terminal::scoped_restore_terminal_state term_state;
1185 target_terminal::ours_for_output ();
1187 fprintf_unfiltered (mi->event_channel, "memory-changed");
1189 mi_uiout->redirect (mi->event_channel);
1191 mi_uiout->field_fmt ("thread-group", "i%d", inferior->num);
1192 mi_uiout->field_core_addr ("addr", target_gdbarch (), memaddr);
1193 mi_uiout->field_string ("len", hex_string (len));
1195 /* Append 'type=code' into notification if MEMADDR falls in the range of
1196 sections contain code. */
1197 sec = find_pc_section (memaddr);
1198 if (sec != NULL && sec->objfile != NULL)
1200 flagword flags = bfd_section_flags (sec->the_bfd_section);
1202 if (flags & SEC_CODE)
1203 mi_uiout->field_string ("type", "code");
1206 mi_uiout->redirect (NULL);
1208 gdb_flush (mi->event_channel);
1212 /* Emit an event when the selection context (inferior, thread, frame)
1213 changed. */
1215 static void
1216 mi_user_selected_context_changed (user_selected_what selection)
1218 struct thread_info *tp;
1220 /* Don't send an event if we're responding to an MI command. */
1221 if (mi_suppress_notification.user_selected_context)
1222 return;
1224 if (inferior_ptid != null_ptid)
1225 tp = inferior_thread ();
1226 else
1227 tp = NULL;
1229 SWITCH_THRU_ALL_UIS ()
1231 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1232 struct ui_out *mi_uiout;
1234 if (mi == NULL)
1235 continue;
1237 mi_uiout = top_level_interpreter ()->interp_ui_out ();
1239 mi_uiout->redirect (mi->event_channel);
1240 ui_out_redirect_pop redirect_popper (mi_uiout);
1242 target_terminal::scoped_restore_terminal_state term_state;
1243 target_terminal::ours_for_output ();
1245 if (selection & USER_SELECTED_INFERIOR)
1246 print_selected_inferior (mi->cli_uiout);
1248 if (tp != NULL
1249 && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
1251 print_selected_thread_frame (mi->cli_uiout, selection);
1253 fprintf_unfiltered (mi->event_channel,
1254 "thread-selected,id=\"%d\"",
1255 tp->global_num);
1257 if (tp->state != THREAD_RUNNING)
1259 if (has_stack_frames ())
1260 print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
1261 1, SRC_AND_LOC, 1);
1265 gdb_flush (mi->event_channel);
1269 ui_out *
1270 mi_interp::interp_ui_out ()
1272 return this->mi_uiout;
1275 /* Do MI-specific logging actions; save raw_stdout, and change all
1276 the consoles to use the supplied ui-file(s). */
1278 void
1279 mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
1280 bool debug_redirect)
1282 struct mi_interp *mi = this;
1284 if (logfile != NULL)
1286 mi->saved_raw_stdout = mi->raw_stdout;
1288 /* If something is being redirected, then grab logfile. */
1289 ui_file *logfile_p = nullptr;
1290 if (logging_redirect || debug_redirect)
1292 logfile_p = logfile.get ();
1293 mi->saved_raw_file_to_delete = logfile_p;
1296 /* If something is not being redirected, then a tee containing both the
1297 logfile and stdout. */
1298 ui_file *tee = nullptr;
1299 if (!logging_redirect || !debug_redirect)
1301 tee = new tee_file (mi->raw_stdout, std::move (logfile));
1302 mi->saved_raw_file_to_delete = tee;
1305 mi->raw_stdout = logging_redirect ? logfile_p : tee;
1306 mi->raw_stdlog = debug_redirect ? logfile_p : tee;
1308 else
1310 delete mi->saved_raw_file_to_delete;
1311 mi->raw_stdout = mi->saved_raw_stdout;
1312 mi->saved_raw_stdout = nullptr;
1313 mi->saved_raw_file_to_delete = nullptr;
1316 mi->out->set_raw (mi->raw_stdout);
1317 mi->err->set_raw (mi->raw_stdout);
1318 mi->log->set_raw (mi->raw_stdout);
1319 mi->targ->set_raw (mi->raw_stdout);
1320 mi->event_channel->set_raw (mi->raw_stdout);
1323 /* Factory for MI interpreters. */
1325 static struct interp *
1326 mi_interp_factory (const char *name)
1328 return new mi_interp (name);
1331 void _initialize_mi_interp ();
1332 void
1333 _initialize_mi_interp ()
1335 /* The various interpreter levels. */
1336 interp_factory_register (INTERP_MI1, mi_interp_factory);
1337 interp_factory_register (INTERP_MI2, mi_interp_factory);
1338 interp_factory_register (INTERP_MI3, mi_interp_factory);
1339 interp_factory_register (INTERP_MI, mi_interp_factory);
1341 gdb::observers::signal_received.attach (mi_on_signal_received);
1342 gdb::observers::end_stepping_range.attach (mi_on_end_stepping_range);
1343 gdb::observers::signal_exited.attach (mi_on_signal_exited);
1344 gdb::observers::exited.attach (mi_on_exited);
1345 gdb::observers::no_history.attach (mi_on_no_history);
1346 gdb::observers::new_thread.attach (mi_new_thread);
1347 gdb::observers::thread_exit.attach (mi_thread_exit);
1348 gdb::observers::inferior_added.attach (mi_inferior_added);
1349 gdb::observers::inferior_appeared.attach (mi_inferior_appeared);
1350 gdb::observers::inferior_exit.attach (mi_inferior_exit);
1351 gdb::observers::inferior_removed.attach (mi_inferior_removed);
1352 gdb::observers::record_changed.attach (mi_record_changed);
1353 gdb::observers::normal_stop.attach (mi_on_normal_stop);
1354 gdb::observers::target_resumed.attach (mi_on_resume);
1355 gdb::observers::solib_loaded.attach (mi_solib_loaded);
1356 gdb::observers::solib_unloaded.attach (mi_solib_unloaded);
1357 gdb::observers::about_to_proceed.attach (mi_about_to_proceed);
1358 gdb::observers::traceframe_changed.attach (mi_traceframe_changed);
1359 gdb::observers::tsv_created.attach (mi_tsv_created);
1360 gdb::observers::tsv_deleted.attach (mi_tsv_deleted);
1361 gdb::observers::tsv_modified.attach (mi_tsv_modified);
1362 gdb::observers::breakpoint_created.attach (mi_breakpoint_created);
1363 gdb::observers::breakpoint_deleted.attach (mi_breakpoint_deleted);
1364 gdb::observers::breakpoint_modified.attach (mi_breakpoint_modified);
1365 gdb::observers::command_param_changed.attach (mi_command_param_changed);
1366 gdb::observers::memory_changed.attach (mi_memory_changed);
1367 gdb::observers::sync_execution_done.attach (mi_on_sync_execution_done);
1368 gdb::observers::user_selected_context_changed.attach
1369 (mi_user_selected_context_changed);