]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/mi/mi-interp.c
gdb/mi: fix breakpoint script field output
[thirdparty/binutils-gdb.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21
22 #include "mi-interp.h"
23
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"
45
46 /* These are the interpreter setup, etc. functions for the MI
47 interpreter. */
48
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);
52
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. */
56
57 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
58 ATTRIBUTE_PRINTF (1, 0);
59
60 static void mi_insert_notify_hooks (void);
61 static void mi_remove_notify_hooks (void);
62
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);
69
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);
93
94 /* Display the MI prompt. */
95
96 static void
97 display_mi_prompt (struct mi_interp *mi)
98 {
99 struct ui *ui = current_ui;
100
101 gdb_puts ("(gdb) \n", mi->raw_stdout);
102 gdb_flush (mi->raw_stdout);
103 ui->prompt_state = PROMPTED;
104 }
105
106 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
107 returns NULL otherwise. */
108
109 static struct mi_interp *
110 as_mi_interp (struct interp *interp)
111 {
112 return dynamic_cast<mi_interp *> (interp);
113 }
114
115 /* Observer for the command_error notification. */
116
117 static void
118 mi_on_command_error ()
119 {
120 mi_interp *mi = as_mi_interp (top_level_interpreter ());
121 if (mi != nullptr)
122 display_mi_prompt (mi);
123 }
124
125 void
126 mi_interp::init (bool top_level)
127 {
128 mi_interp *mi = this;
129
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;
134
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, "&", '"');
139 mi->log = mi->err;
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);
145
146 if (top_level)
147 {
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
150 interpreter.
151
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. */
156
157 for (inferior *inf : all_inferiors ())
158 {
159 target_terminal::scoped_restore_terminal_state term_state;
160 target_terminal::ours_for_output ();
161
162 gdb_printf (mi->event_channel,
163 "thread-group-added,id=\"i%d\"",
164 inf->num);
165
166 gdb_flush (mi->event_channel);
167 }
168 }
169 }
170
171 void
172 mi_interp::resume ()
173 {
174 struct mi_interp *mi = this;
175 struct ui *ui = current_ui;
176
177 /* As per hack note in mi_interpreter_init, swap in the output
178 channels... */
179 gdb_setup_readline (0);
180
181 ui->call_readline = gdb_readline_no_editing_callback;
182 ui->input_handler = mi_execute_command_input_handler;
183
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;
192
193 /* Replace all the hooks that we know about. There really needs to
194 be a better way of doing this... */
195 clear_interpreter_hooks ();
196
197 deprecated_show_load_progress = mi_load_progress;
198 }
199
200 void
201 mi_interp::suspend ()
202 {
203 gdb_disable_readline ();
204 }
205
206 gdb_exception
207 mi_interp::exec (const char *command)
208 {
209 mi_execute_command_wrapper (command);
210 return gdb_exception ();
211 }
212
213 void
214 mi_cmd_interpreter_exec (const char *command, char **argv, int argc)
215 {
216 struct interp *interp_to_use;
217 int i;
218
219 if (argc < 2)
220 error (_("-interpreter-exec: "
221 "Usage: -interpreter-exec interp command"));
222
223 interp_to_use = interp_lookup (current_ui, argv[0]);
224 if (interp_to_use == NULL)
225 error (_("-interpreter-exec: could not find interpreter \"%s\""),
226 argv[0]);
227
228 /* Note that unlike the CLI version of this command, we don't
229 actually set INTERP_TO_USE as the current interpreter, as we
230 still want gdb_stdout, etc. to point at MI streams. */
231
232 /* Insert the MI out hooks, making sure to also call the
233 interpreter's hooks if it has any. */
234 /* KRS: We shouldn't need this... Events should be installed and
235 they should just ALWAYS fire something out down the MI
236 channel. */
237 mi_insert_notify_hooks ();
238
239 /* Now run the code. */
240
241 SCOPE_EXIT
242 {
243 mi_remove_notify_hooks ();
244 };
245
246 for (i = 1; i < argc; i++)
247 {
248 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
249
250 if (e.reason < 0)
251 error ("%s", e.what ());
252 }
253 }
254
255 /* This inserts a number of hooks that are meant to produce
256 async-notify ("=") MI messages while running commands in another
257 interpreter using mi_interpreter_exec. The canonical use for this
258 is to allow access to the gdb CLI interpreter from within the MI,
259 while still producing MI style output when actions in the CLI
260 command change GDB's state. */
261
262 static void
263 mi_insert_notify_hooks (void)
264 {
265 deprecated_query_hook = mi_interp_query_hook;
266 }
267
268 static void
269 mi_remove_notify_hooks (void)
270 {
271 deprecated_query_hook = NULL;
272 }
273
274 static int
275 mi_interp_query_hook (const char *ctlstr, va_list ap)
276 {
277 return 1;
278 }
279
280 static void
281 mi_execute_command_wrapper (const char *cmd)
282 {
283 struct ui *ui = current_ui;
284
285 mi_execute_command (cmd, ui->instream == ui->stdin_stream);
286 }
287
288 /* Observer for the synchronous_command_done notification. */
289
290 static void
291 mi_on_sync_execution_done (void)
292 {
293 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
294
295 if (mi == NULL)
296 return;
297
298 /* If MI is sync, then output the MI prompt now, indicating we're
299 ready for further input. */
300 if (!mi_async_p ())
301 display_mi_prompt (mi);
302 }
303
304 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
305
306 static void
307 mi_execute_command_input_handler (gdb::unique_xmalloc_ptr<char> &&cmd)
308 {
309 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
310 struct ui *ui = current_ui;
311
312 ui->prompt_state = PROMPT_NEEDED;
313
314 mi_execute_command_wrapper (cmd.get ());
315
316 /* Print a prompt, indicating we're ready for further input, unless
317 we just started a synchronous command. In that case, we're about
318 to go back to the event loop and will output the prompt in the
319 'synchronous_command_done' observer when the target next
320 stops. */
321 if (ui->prompt_state == PROMPT_NEEDED)
322 display_mi_prompt (mi);
323 }
324
325 void
326 mi_interp::pre_command_loop ()
327 {
328 struct mi_interp *mi = this;
329
330 /* Turn off 8 bit strings in quoted output. Any character with the
331 high bit set is printed using C's octal format. */
332 sevenbit_strings = 1;
333
334 /* Tell the world that we're alive. */
335 display_mi_prompt (mi);
336 }
337
338 static void
339 mi_new_thread (struct thread_info *t)
340 {
341 SWITCH_THRU_ALL_UIS ()
342 {
343 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
344
345 if (mi == NULL)
346 continue;
347
348 target_terminal::scoped_restore_terminal_state term_state;
349 target_terminal::ours_for_output ();
350
351 gdb_printf (mi->event_channel,
352 "thread-created,id=\"%d\",group-id=\"i%d\"",
353 t->global_num, t->inf->num);
354 gdb_flush (mi->event_channel);
355 }
356 }
357
358 static void
359 mi_thread_exit (struct thread_info *t, int silent)
360 {
361 SWITCH_THRU_ALL_UIS ()
362 {
363 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
364
365 if (mi == NULL)
366 continue;
367
368 target_terminal::scoped_restore_terminal_state term_state;
369 target_terminal::ours_for_output ();
370 gdb_printf (mi->event_channel,
371 "thread-exited,id=\"%d\",group-id=\"i%d\"",
372 t->global_num, t->inf->num);
373 gdb_flush (mi->event_channel);
374 }
375 }
376
377 /* Emit notification on changing the state of record. */
378
379 static void
380 mi_record_changed (struct inferior *inferior, int started, const char *method,
381 const char *format)
382 {
383 SWITCH_THRU_ALL_UIS ()
384 {
385 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
386
387 if (mi == NULL)
388 continue;
389
390 target_terminal::scoped_restore_terminal_state term_state;
391 target_terminal::ours_for_output ();
392
393 if (started)
394 {
395 if (format != NULL)
396 {
397 gdb_printf (mi->event_channel,
398 "record-started,thread-group=\"i%d\","
399 "method=\"%s\",format=\"%s\"",
400 inferior->num, method, format);
401 }
402 else
403 {
404 gdb_printf (mi->event_channel,
405 "record-started,thread-group=\"i%d\","
406 "method=\"%s\"",
407 inferior->num, method);
408 }
409 }
410 else
411 {
412 gdb_printf (mi->event_channel,
413 "record-stopped,thread-group=\"i%d\"",
414 inferior->num);
415 }
416
417 gdb_flush (mi->event_channel);
418 }
419 }
420
421 static void
422 mi_inferior_added (struct inferior *inf)
423 {
424 SWITCH_THRU_ALL_UIS ()
425 {
426 struct interp *interp;
427 struct mi_interp *mi;
428
429 /* We'll be called once for the initial inferior, before the top
430 level interpreter is set. */
431 interp = top_level_interpreter ();
432 if (interp == NULL)
433 continue;
434
435 mi = as_mi_interp (interp);
436 if (mi == NULL)
437 continue;
438
439 target_terminal::scoped_restore_terminal_state term_state;
440 target_terminal::ours_for_output ();
441
442 gdb_printf (mi->event_channel,
443 "thread-group-added,id=\"i%d\"",
444 inf->num);
445 gdb_flush (mi->event_channel);
446 }
447 }
448
449 static void
450 mi_inferior_appeared (struct inferior *inf)
451 {
452 SWITCH_THRU_ALL_UIS ()
453 {
454 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
455
456 if (mi == NULL)
457 continue;
458
459 target_terminal::scoped_restore_terminal_state term_state;
460 target_terminal::ours_for_output ();
461
462 gdb_printf (mi->event_channel,
463 "thread-group-started,id=\"i%d\",pid=\"%d\"",
464 inf->num, inf->pid);
465 gdb_flush (mi->event_channel);
466 }
467 }
468
469 static void
470 mi_inferior_exit (struct inferior *inf)
471 {
472 SWITCH_THRU_ALL_UIS ()
473 {
474 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
475
476 if (mi == NULL)
477 continue;
478
479 target_terminal::scoped_restore_terminal_state term_state;
480 target_terminal::ours_for_output ();
481
482 if (inf->has_exit_code)
483 gdb_printf (mi->event_channel,
484 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
485 inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
486 else
487 gdb_printf (mi->event_channel,
488 "thread-group-exited,id=\"i%d\"", inf->num);
489
490 gdb_flush (mi->event_channel);
491 }
492 }
493
494 static void
495 mi_inferior_removed (struct inferior *inf)
496 {
497 SWITCH_THRU_ALL_UIS ()
498 {
499 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
500
501 if (mi == NULL)
502 continue;
503
504 target_terminal::scoped_restore_terminal_state term_state;
505 target_terminal::ours_for_output ();
506
507 gdb_printf (mi->event_channel,
508 "thread-group-removed,id=\"i%d\"",
509 inf->num);
510 gdb_flush (mi->event_channel);
511 }
512 }
513
514 /* Return the MI interpreter, if it is active -- either because it's
515 the top-level interpreter or the interpreter executing the current
516 command. Returns NULL if the MI interpreter is not being used. */
517
518 static struct mi_interp *
519 find_mi_interp (void)
520 {
521 struct mi_interp *mi;
522
523 mi = as_mi_interp (top_level_interpreter ());
524 if (mi != NULL)
525 return mi;
526
527 mi = as_mi_interp (command_interp ());
528 if (mi != NULL)
529 return mi;
530
531 return NULL;
532 }
533
534 /* Observers for several run control events that print why the
535 inferior has stopped to both the MI event channel and to the MI
536 console. If the MI interpreter is not active, print nothing. */
537
538 /* Observer for the signal_received notification. */
539
540 static void
541 mi_on_signal_received (enum gdb_signal siggnal)
542 {
543 SWITCH_THRU_ALL_UIS ()
544 {
545 struct mi_interp *mi = find_mi_interp ();
546
547 if (mi == NULL)
548 continue;
549
550 print_signal_received_reason (mi->mi_uiout, siggnal);
551 print_signal_received_reason (mi->cli_uiout, siggnal);
552 }
553 }
554
555 /* Observer for the end_stepping_range notification. */
556
557 static void
558 mi_on_end_stepping_range (void)
559 {
560 SWITCH_THRU_ALL_UIS ()
561 {
562 struct mi_interp *mi = find_mi_interp ();
563
564 if (mi == NULL)
565 continue;
566
567 print_end_stepping_range_reason (mi->mi_uiout);
568 print_end_stepping_range_reason (mi->cli_uiout);
569 }
570 }
571
572 /* Observer for the signal_exited notification. */
573
574 static void
575 mi_on_signal_exited (enum gdb_signal siggnal)
576 {
577 SWITCH_THRU_ALL_UIS ()
578 {
579 struct mi_interp *mi = find_mi_interp ();
580
581 if (mi == NULL)
582 continue;
583
584 print_signal_exited_reason (mi->mi_uiout, siggnal);
585 print_signal_exited_reason (mi->cli_uiout, siggnal);
586 }
587 }
588
589 /* Observer for the exited notification. */
590
591 static void
592 mi_on_exited (int exitstatus)
593 {
594 SWITCH_THRU_ALL_UIS ()
595 {
596 struct mi_interp *mi = find_mi_interp ();
597
598 if (mi == NULL)
599 continue;
600
601 print_exited_reason (mi->mi_uiout, exitstatus);
602 print_exited_reason (mi->cli_uiout, exitstatus);
603 }
604 }
605
606 /* Observer for the no_history notification. */
607
608 static void
609 mi_on_no_history (void)
610 {
611 SWITCH_THRU_ALL_UIS ()
612 {
613 struct mi_interp *mi = find_mi_interp ();
614
615 if (mi == NULL)
616 continue;
617
618 print_no_history_reason (mi->mi_uiout);
619 print_no_history_reason (mi->cli_uiout);
620 }
621 }
622
623 static void
624 mi_on_normal_stop_1 (struct bpstat *bs, int print_frame)
625 {
626 /* Since this can be called when CLI command is executing,
627 using cli interpreter, be sure to use MI uiout for output,
628 not the current one. */
629 struct ui_out *mi_uiout = top_level_interpreter ()->interp_ui_out ();
630 struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
631
632 if (print_frame)
633 {
634 struct thread_info *tp;
635 int core;
636 struct interp *console_interp;
637
638 tp = inferior_thread ();
639
640 if (tp->thread_fsm () != nullptr
641 && tp->thread_fsm ()->finished_p ())
642 {
643 enum async_reply_reason reason;
644
645 reason = tp->thread_fsm ()->async_reply_reason ();
646 mi_uiout->field_string ("reason", async_reason_lookup (reason));
647 }
648
649 console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
650 /* We only want to print the displays once, and we want it to
651 look just how it would on the console, so we use this to
652 decide whether the MI stop should include them. */
653 bool console_print = should_print_stop_to_console (console_interp, tp);
654 print_stop_event (mi_uiout, !console_print);
655
656 if (console_print)
657 print_stop_event (mi->cli_uiout);
658
659 mi_uiout->field_signed ("thread-id", tp->global_num);
660 if (non_stop)
661 {
662 ui_out_emit_list list_emitter (mi_uiout, "stopped-threads");
663
664 mi_uiout->field_signed (NULL, tp->global_num);
665 }
666 else
667 mi_uiout->field_string ("stopped-threads", "all");
668
669 core = target_core_of_thread (tp->ptid);
670 if (core != -1)
671 mi_uiout->field_signed ("core", core);
672 }
673
674 gdb_puts ("*stopped", mi->raw_stdout);
675 mi_out_put (mi_uiout, mi->raw_stdout);
676 mi_out_rewind (mi_uiout);
677 mi_print_timing_maybe (mi->raw_stdout);
678 gdb_puts ("\n", mi->raw_stdout);
679 gdb_flush (mi->raw_stdout);
680 }
681
682 static void
683 mi_on_normal_stop (struct bpstat *bs, int print_frame)
684 {
685 SWITCH_THRU_ALL_UIS ()
686 {
687 if (as_mi_interp (top_level_interpreter ()) == NULL)
688 continue;
689
690 mi_on_normal_stop_1 (bs, print_frame);
691 }
692 }
693
694 static void
695 mi_about_to_proceed (void)
696 {
697 /* Suppress output while calling an inferior function. */
698
699 if (inferior_ptid != null_ptid)
700 {
701 struct thread_info *tp = inferior_thread ();
702
703 if (tp->control.in_infcall)
704 return;
705 }
706
707 mi_proceeded = 1;
708 }
709
710 /* When the element is non-zero, no MI notifications will be emitted in
711 response to the corresponding observers. */
712
713 struct mi_suppress_notification mi_suppress_notification =
714 {
715 0,
716 0,
717 0,
718 0,
719 };
720
721 /* Emit notification on changing a traceframe. */
722
723 static void
724 mi_traceframe_changed (int tfnum, int tpnum)
725 {
726 if (mi_suppress_notification.traceframe)
727 return;
728
729 SWITCH_THRU_ALL_UIS ()
730 {
731 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
732
733 if (mi == NULL)
734 continue;
735
736 target_terminal::scoped_restore_terminal_state term_state;
737 target_terminal::ours_for_output ();
738
739 if (tfnum >= 0)
740 gdb_printf (mi->event_channel, "traceframe-changed,"
741 "num=\"%d\",tracepoint=\"%d\"",
742 tfnum, tpnum);
743 else
744 gdb_printf (mi->event_channel, "traceframe-changed,end");
745
746 gdb_flush (mi->event_channel);
747 }
748 }
749
750 /* Emit notification on creating a trace state variable. */
751
752 static void
753 mi_tsv_created (const struct trace_state_variable *tsv)
754 {
755 SWITCH_THRU_ALL_UIS ()
756 {
757 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
758
759 if (mi == NULL)
760 continue;
761
762 target_terminal::scoped_restore_terminal_state term_state;
763 target_terminal::ours_for_output ();
764
765 gdb_printf (mi->event_channel, "tsv-created,"
766 "name=\"%s\",initial=\"%s\"",
767 tsv->name.c_str (), plongest (tsv->initial_value));
768
769 gdb_flush (mi->event_channel);
770 }
771 }
772
773 /* Emit notification on deleting a trace state variable. */
774
775 static void
776 mi_tsv_deleted (const struct trace_state_variable *tsv)
777 {
778 SWITCH_THRU_ALL_UIS ()
779 {
780 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
781
782 if (mi == NULL)
783 continue;
784
785 target_terminal::scoped_restore_terminal_state term_state;
786 target_terminal::ours_for_output ();
787
788 if (tsv != NULL)
789 gdb_printf (mi->event_channel, "tsv-deleted,"
790 "name=\"%s\"", tsv->name.c_str ());
791 else
792 gdb_printf (mi->event_channel, "tsv-deleted");
793
794 gdb_flush (mi->event_channel);
795 }
796 }
797
798 /* Emit notification on modifying a trace state variable. */
799
800 static void
801 mi_tsv_modified (const struct trace_state_variable *tsv)
802 {
803 SWITCH_THRU_ALL_UIS ()
804 {
805 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
806 struct ui_out *mi_uiout;
807
808 if (mi == NULL)
809 continue;
810
811 mi_uiout = top_level_interpreter ()->interp_ui_out ();
812
813 target_terminal::scoped_restore_terminal_state term_state;
814 target_terminal::ours_for_output ();
815
816 gdb_printf (mi->event_channel,
817 "tsv-modified");
818
819 mi_uiout->redirect (mi->event_channel);
820
821 mi_uiout->field_string ("name", tsv->name);
822 mi_uiout->field_string ("initial",
823 plongest (tsv->initial_value));
824 if (tsv->value_known)
825 mi_uiout->field_string ("current", plongest (tsv->value));
826
827 mi_uiout->redirect (NULL);
828
829 gdb_flush (mi->event_channel);
830 }
831 }
832
833 /* Print breakpoint BP on MI's event channel. */
834
835 static void
836 mi_print_breakpoint_for_event (struct mi_interp *mi, breakpoint *bp)
837 {
838 ui_out *mi_uiout = mi->interp_ui_out ();
839
840 /* We want the output from print_breakpoint to go to
841 mi->event_channel. One approach would be to just call
842 print_breakpoint, and then use mi_out_put to send the current
843 content of mi_uiout into mi->event_channel. However, that will
844 break if anything is output to mi_uiout prior to calling the
845 breakpoint_created notifications. So, we use
846 ui_out_redirect. */
847 mi_uiout->redirect (mi->event_channel);
848
849 try
850 {
851 scoped_restore restore_uiout
852 = make_scoped_restore (&current_uiout, mi_uiout);
853
854 print_breakpoint (bp);
855 }
856 catch (const gdb_exception &ex)
857 {
858 exception_print (gdb_stderr, ex);
859 }
860
861 mi_uiout->redirect (NULL);
862 }
863
864 /* Emit notification about a created breakpoint. */
865
866 static void
867 mi_breakpoint_created (struct breakpoint *b)
868 {
869 if (mi_suppress_notification.breakpoint)
870 return;
871
872 if (b->number <= 0)
873 return;
874
875 SWITCH_THRU_ALL_UIS ()
876 {
877 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
878
879 if (mi == NULL)
880 continue;
881
882 target_terminal::scoped_restore_terminal_state term_state;
883 target_terminal::ours_for_output ();
884
885 gdb_printf (mi->event_channel,
886 "breakpoint-created");
887 mi_print_breakpoint_for_event (mi, b);
888
889 gdb_flush (mi->event_channel);
890 }
891 }
892
893 /* Emit notification about deleted breakpoint. */
894
895 static void
896 mi_breakpoint_deleted (struct breakpoint *b)
897 {
898 if (mi_suppress_notification.breakpoint)
899 return;
900
901 if (b->number <= 0)
902 return;
903
904 SWITCH_THRU_ALL_UIS ()
905 {
906 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
907
908 if (mi == NULL)
909 continue;
910
911 target_terminal::scoped_restore_terminal_state term_state;
912 target_terminal::ours_for_output ();
913
914 gdb_printf (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
915 b->number);
916
917 gdb_flush (mi->event_channel);
918 }
919 }
920
921 /* Emit notification about modified breakpoint. */
922
923 static void
924 mi_breakpoint_modified (struct breakpoint *b)
925 {
926 if (mi_suppress_notification.breakpoint)
927 return;
928
929 if (b->number <= 0)
930 return;
931
932 SWITCH_THRU_ALL_UIS ()
933 {
934 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
935
936 if (mi == NULL)
937 continue;
938
939 target_terminal::scoped_restore_terminal_state term_state;
940 target_terminal::ours_for_output ();
941 gdb_printf (mi->event_channel,
942 "breakpoint-modified");
943 mi_print_breakpoint_for_event (mi, b);
944
945 gdb_flush (mi->event_channel);
946 }
947 }
948
949 static void
950 mi_output_running (struct thread_info *thread)
951 {
952 SWITCH_THRU_ALL_UIS ()
953 {
954 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
955
956 if (mi == NULL)
957 continue;
958
959 gdb_printf (mi->raw_stdout,
960 "*running,thread-id=\"%d\"\n",
961 thread->global_num);
962 }
963 }
964
965 /* Return true if there are multiple inferiors loaded. This is used
966 for backwards compatibility -- if there's only one inferior, output
967 "all", otherwise, output each resumed thread individually. */
968
969 static bool
970 multiple_inferiors_p ()
971 {
972 int count = 0;
973 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
974 {
975 count++;
976 if (count > 1)
977 return true;
978 }
979
980 return false;
981 }
982
983 static void
984 mi_on_resume_1 (struct mi_interp *mi,
985 process_stratum_target *targ, ptid_t ptid)
986 {
987 /* To cater for older frontends, emit ^running, but do it only once
988 per each command. We do it here, since at this point we know
989 that the target was successfully resumed, and in non-async mode,
990 we won't return back to MI interpreter code until the target
991 is done running, so delaying the output of "^running" until then
992 will make it impossible for frontend to know what's going on.
993
994 In future (MI3), we'll be outputting "^done" here. */
995 if (!running_result_record_printed && mi_proceeded)
996 {
997 gdb_printf (mi->raw_stdout, "%s^running\n",
998 current_token ? current_token : "");
999 }
1000
1001 /* Backwards compatibility. If doing a wildcard resume and there's
1002 only one inferior, output "all", otherwise, output each resumed
1003 thread individually. */
1004 if ((ptid == minus_one_ptid || ptid.is_pid ())
1005 && !multiple_inferiors_p ())
1006 gdb_printf (mi->raw_stdout, "*running,thread-id=\"all\"\n");
1007 else
1008 for (thread_info *tp : all_non_exited_threads (targ, ptid))
1009 mi_output_running (tp);
1010
1011 if (!running_result_record_printed && mi_proceeded)
1012 {
1013 running_result_record_printed = 1;
1014 /* This is what gdb used to do historically -- printing prompt
1015 even if it cannot actually accept any input. This will be
1016 surely removed for MI3, and may be removed even earlier. */
1017 if (current_ui->prompt_state == PROMPT_BLOCKED)
1018 gdb_puts ("(gdb) \n", mi->raw_stdout);
1019 }
1020 gdb_flush (mi->raw_stdout);
1021 }
1022
1023 static void
1024 mi_on_resume (ptid_t ptid)
1025 {
1026 struct thread_info *tp = NULL;
1027
1028 process_stratum_target *target = current_inferior ()->process_target ();
1029 if (ptid == minus_one_ptid || ptid.is_pid ())
1030 tp = inferior_thread ();
1031 else
1032 tp = find_thread_ptid (target, ptid);
1033
1034 /* Suppress output while calling an inferior function. */
1035 if (tp->control.in_infcall)
1036 return;
1037
1038 SWITCH_THRU_ALL_UIS ()
1039 {
1040 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1041
1042 if (mi == NULL)
1043 continue;
1044
1045 target_terminal::scoped_restore_terminal_state term_state;
1046 target_terminal::ours_for_output ();
1047
1048 mi_on_resume_1 (mi, target, ptid);
1049 }
1050 }
1051
1052 /* See mi-interp.h. */
1053
1054 void
1055 mi_output_solib_attribs (ui_out *uiout, struct so_list *solib)
1056 {
1057 struct gdbarch *gdbarch = target_gdbarch ();
1058
1059 uiout->field_string ("id", solib->so_original_name);
1060 uiout->field_string ("target-name", solib->so_original_name);
1061 uiout->field_string ("host-name", solib->so_name);
1062 uiout->field_signed ("symbols-loaded", solib->symbols_loaded);
1063 if (!gdbarch_has_global_solist (target_gdbarch ()))
1064 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
1065
1066 ui_out_emit_list list_emitter (uiout, "ranges");
1067 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1068 if (solib->addr_high != 0)
1069 {
1070 uiout->field_core_addr ("from", gdbarch, solib->addr_low);
1071 uiout->field_core_addr ("to", gdbarch, solib->addr_high);
1072 }
1073 }
1074
1075 static void
1076 mi_solib_loaded (struct so_list *solib)
1077 {
1078 SWITCH_THRU_ALL_UIS ()
1079 {
1080 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1081 struct ui_out *uiout;
1082
1083 if (mi == NULL)
1084 continue;
1085
1086 uiout = top_level_interpreter ()->interp_ui_out ();
1087
1088 target_terminal::scoped_restore_terminal_state term_state;
1089 target_terminal::ours_for_output ();
1090
1091 gdb_printf (mi->event_channel, "library-loaded");
1092
1093 uiout->redirect (mi->event_channel);
1094
1095 mi_output_solib_attribs (uiout, solib);
1096
1097 uiout->redirect (NULL);
1098
1099 gdb_flush (mi->event_channel);
1100 }
1101 }
1102
1103 static void
1104 mi_solib_unloaded (struct so_list *solib)
1105 {
1106 SWITCH_THRU_ALL_UIS ()
1107 {
1108 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1109 struct ui_out *uiout;
1110
1111 if (mi == NULL)
1112 continue;
1113
1114 uiout = top_level_interpreter ()->interp_ui_out ();
1115
1116 target_terminal::scoped_restore_terminal_state term_state;
1117 target_terminal::ours_for_output ();
1118
1119 gdb_printf (mi->event_channel, "library-unloaded");
1120
1121 uiout->redirect (mi->event_channel);
1122
1123 uiout->field_string ("id", solib->so_original_name);
1124 uiout->field_string ("target-name", solib->so_original_name);
1125 uiout->field_string ("host-name", solib->so_name);
1126 if (!gdbarch_has_global_solist (target_gdbarch ()))
1127 {
1128 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
1129 }
1130
1131 uiout->redirect (NULL);
1132
1133 gdb_flush (mi->event_channel);
1134 }
1135 }
1136
1137 /* Emit notification about the command parameter change. */
1138
1139 static void
1140 mi_command_param_changed (const char *param, const char *value)
1141 {
1142 if (mi_suppress_notification.cmd_param_changed)
1143 return;
1144
1145 SWITCH_THRU_ALL_UIS ()
1146 {
1147 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1148 struct ui_out *mi_uiout;
1149
1150 if (mi == NULL)
1151 continue;
1152
1153 mi_uiout = top_level_interpreter ()->interp_ui_out ();
1154
1155 target_terminal::scoped_restore_terminal_state term_state;
1156 target_terminal::ours_for_output ();
1157
1158 gdb_printf (mi->event_channel, "cmd-param-changed");
1159
1160 mi_uiout->redirect (mi->event_channel);
1161
1162 mi_uiout->field_string ("param", param);
1163 mi_uiout->field_string ("value", value);
1164
1165 mi_uiout->redirect (NULL);
1166
1167 gdb_flush (mi->event_channel);
1168 }
1169 }
1170
1171 /* Emit notification about the target memory change. */
1172
1173 static void
1174 mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
1175 ssize_t len, const bfd_byte *myaddr)
1176 {
1177 if (mi_suppress_notification.memory)
1178 return;
1179
1180 SWITCH_THRU_ALL_UIS ()
1181 {
1182 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1183 struct ui_out *mi_uiout;
1184 struct obj_section *sec;
1185
1186 if (mi == NULL)
1187 continue;
1188
1189 mi_uiout = top_level_interpreter ()->interp_ui_out ();
1190
1191 target_terminal::scoped_restore_terminal_state term_state;
1192 target_terminal::ours_for_output ();
1193
1194 gdb_printf (mi->event_channel, "memory-changed");
1195
1196 mi_uiout->redirect (mi->event_channel);
1197
1198 mi_uiout->field_fmt ("thread-group", "i%d", inferior->num);
1199 mi_uiout->field_core_addr ("addr", target_gdbarch (), memaddr);
1200 mi_uiout->field_string ("len", hex_string (len));
1201
1202 /* Append 'type=code' into notification if MEMADDR falls in the range of
1203 sections contain code. */
1204 sec = find_pc_section (memaddr);
1205 if (sec != NULL && sec->objfile != NULL)
1206 {
1207 flagword flags = bfd_section_flags (sec->the_bfd_section);
1208
1209 if (flags & SEC_CODE)
1210 mi_uiout->field_string ("type", "code");
1211 }
1212
1213 mi_uiout->redirect (NULL);
1214
1215 gdb_flush (mi->event_channel);
1216 }
1217 }
1218
1219 /* Emit an event when the selection context (inferior, thread, frame)
1220 changed. */
1221
1222 static void
1223 mi_user_selected_context_changed (user_selected_what selection)
1224 {
1225 struct thread_info *tp;
1226
1227 /* Don't send an event if we're responding to an MI command. */
1228 if (mi_suppress_notification.user_selected_context)
1229 return;
1230
1231 if (inferior_ptid != null_ptid)
1232 tp = inferior_thread ();
1233 else
1234 tp = NULL;
1235
1236 SWITCH_THRU_ALL_UIS ()
1237 {
1238 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1239 struct ui_out *mi_uiout;
1240
1241 if (mi == NULL)
1242 continue;
1243
1244 mi_uiout = top_level_interpreter ()->interp_ui_out ();
1245
1246 mi_uiout->redirect (mi->event_channel);
1247 ui_out_redirect_pop redirect_popper (mi_uiout);
1248
1249 target_terminal::scoped_restore_terminal_state term_state;
1250 target_terminal::ours_for_output ();
1251
1252 if (selection & USER_SELECTED_INFERIOR)
1253 print_selected_inferior (mi->cli_uiout);
1254
1255 if (tp != NULL
1256 && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
1257 {
1258 print_selected_thread_frame (mi->cli_uiout, selection);
1259
1260 gdb_printf (mi->event_channel,
1261 "thread-selected,id=\"%d\"",
1262 tp->global_num);
1263
1264 if (tp->state != THREAD_RUNNING)
1265 {
1266 if (has_stack_frames ())
1267 print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
1268 1, SRC_AND_LOC, 1);
1269 }
1270 }
1271
1272 gdb_flush (mi->event_channel);
1273 }
1274 }
1275
1276 ui_out *
1277 mi_interp::interp_ui_out ()
1278 {
1279 return this->mi_uiout;
1280 }
1281
1282 /* Do MI-specific logging actions; save raw_stdout, and change all
1283 the consoles to use the supplied ui-file(s). */
1284
1285 void
1286 mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
1287 bool debug_redirect)
1288 {
1289 struct mi_interp *mi = this;
1290
1291 if (logfile != NULL)
1292 {
1293 mi->saved_raw_stdout = mi->raw_stdout;
1294
1295 /* If something is being redirected, then grab logfile. */
1296 ui_file *logfile_p = nullptr;
1297 if (logging_redirect || debug_redirect)
1298 {
1299 logfile_p = logfile.get ();
1300 mi->saved_raw_file_to_delete = logfile_p;
1301 }
1302
1303 /* If something is not being redirected, then a tee containing both the
1304 logfile and stdout. */
1305 ui_file *tee = nullptr;
1306 if (!logging_redirect || !debug_redirect)
1307 {
1308 tee = new tee_file (mi->raw_stdout, std::move (logfile));
1309 mi->saved_raw_file_to_delete = tee;
1310 }
1311
1312 mi->raw_stdout = logging_redirect ? logfile_p : tee;
1313 mi->raw_stdlog = debug_redirect ? logfile_p : tee;
1314 }
1315 else
1316 {
1317 delete mi->saved_raw_file_to_delete;
1318 mi->raw_stdout = mi->saved_raw_stdout;
1319 mi->saved_raw_stdout = nullptr;
1320 mi->saved_raw_file_to_delete = nullptr;
1321 }
1322
1323 mi->out->set_raw (mi->raw_stdout);
1324 mi->err->set_raw (mi->raw_stdout);
1325 mi->log->set_raw (mi->raw_stdout);
1326 mi->targ->set_raw (mi->raw_stdout);
1327 mi->event_channel->set_raw (mi->raw_stdout);
1328 }
1329
1330 /* Factory for MI interpreters. */
1331
1332 static struct interp *
1333 mi_interp_factory (const char *name)
1334 {
1335 return new mi_interp (name);
1336 }
1337
1338 void _initialize_mi_interp ();
1339 void
1340 _initialize_mi_interp ()
1341 {
1342 /* The various interpreter levels. */
1343 interp_factory_register (INTERP_MI1, mi_interp_factory);
1344 interp_factory_register (INTERP_MI2, mi_interp_factory);
1345 interp_factory_register (INTERP_MI3, mi_interp_factory);
1346 interp_factory_register (INTERP_MI4, mi_interp_factory);
1347 interp_factory_register (INTERP_MI, mi_interp_factory);
1348
1349 gdb::observers::signal_received.attach (mi_on_signal_received, "mi-interp");
1350 gdb::observers::end_stepping_range.attach (mi_on_end_stepping_range,
1351 "mi-interp");
1352 gdb::observers::signal_exited.attach (mi_on_signal_exited, "mi-interp");
1353 gdb::observers::exited.attach (mi_on_exited, "mi-interp");
1354 gdb::observers::no_history.attach (mi_on_no_history, "mi-interp");
1355 gdb::observers::new_thread.attach (mi_new_thread, "mi-interp");
1356 gdb::observers::thread_exit.attach (mi_thread_exit, "mi-interp");
1357 gdb::observers::inferior_added.attach (mi_inferior_added, "mi-interp");
1358 gdb::observers::inferior_appeared.attach (mi_inferior_appeared, "mi-interp");
1359 gdb::observers::inferior_exit.attach (mi_inferior_exit, "mi-interp");
1360 gdb::observers::inferior_removed.attach (mi_inferior_removed, "mi-interp");
1361 gdb::observers::record_changed.attach (mi_record_changed, "mi-interp");
1362 gdb::observers::normal_stop.attach (mi_on_normal_stop, "mi-interp");
1363 gdb::observers::target_resumed.attach (mi_on_resume, "mi-interp");
1364 gdb::observers::solib_loaded.attach (mi_solib_loaded, "mi-interp");
1365 gdb::observers::solib_unloaded.attach (mi_solib_unloaded, "mi-interp");
1366 gdb::observers::about_to_proceed.attach (mi_about_to_proceed, "mi-interp");
1367 gdb::observers::traceframe_changed.attach (mi_traceframe_changed,
1368 "mi-interp");
1369 gdb::observers::tsv_created.attach (mi_tsv_created, "mi-interp");
1370 gdb::observers::tsv_deleted.attach (mi_tsv_deleted, "mi-interp");
1371 gdb::observers::tsv_modified.attach (mi_tsv_modified, "mi-interp");
1372 gdb::observers::breakpoint_created.attach (mi_breakpoint_created,
1373 "mi-interp");
1374 gdb::observers::breakpoint_deleted.attach (mi_breakpoint_deleted,
1375 "mi-interp");
1376 gdb::observers::breakpoint_modified.attach (mi_breakpoint_modified,
1377 "mi-interp");
1378 gdb::observers::command_param_changed.attach (mi_command_param_changed,
1379 "mi-interp");
1380 gdb::observers::command_error.attach (mi_on_command_error, "mi-interp");
1381 gdb::observers::memory_changed.attach (mi_memory_changed, "mi-interp");
1382 gdb::observers::sync_execution_done.attach (mi_on_sync_execution_done,
1383 "mi-interp");
1384 gdb::observers::user_selected_context_changed.attach
1385 (mi_user_selected_context_changed, "mi-interp");
1386 }