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