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