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