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