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