]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/mi/mi-interp.c
gdb: rename struct shobj -> struct solib
[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-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include "mi-interp.h"
23
24 #include "interps.h"
25 #include "event-top.h"
26 #include "gdbsupport/event-loop.h"
27 #include "inferior.h"
28 #include "infrun.h"
29 #include "ui-out.h"
30 #include "ui.h"
31 #include "mi-main.h"
32 #include "mi-cmds.h"
33 #include "mi-out.h"
34 #include "mi-console.h"
35 #include "mi-common.h"
36 #include "observable.h"
37 #include "gdbthread.h"
38 #include "solist.h"
39 #include "objfiles.h"
40 #include "tracepoint.h"
41 #include "cli-out.h"
42 #include "thread-fsm.h"
43 #include "cli/cli-interp.h"
44 #include "gdbsupport/scope-exit.h"
45
46 /* These are the interpreter setup, etc. functions for the MI
47 interpreter. */
48
49 static void mi_execute_command_wrapper (const char *cmd);
50 static void mi_execute_command_input_handler
51 (gdb::unique_xmalloc_ptr<char> &&cmd);
52
53 /* These are hooks that we put in place while doing interpreter_exec
54 so we can report interesting things that happened "behind the MI's
55 back" in this command. */
56
57 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
58 ATTRIBUTE_PRINTF (1, 0);
59
60 static void mi_insert_notify_hooks (void);
61 static void mi_remove_notify_hooks (void);
62
63 /* Display the MI prompt. */
64
65 static void
66 display_mi_prompt (struct mi_interp *mi)
67 {
68 struct ui *ui = current_ui;
69
70 gdb_puts ("(gdb) \n", mi->raw_stdout);
71 gdb_flush (mi->raw_stdout);
72 ui->prompt_state = PROMPTED;
73 }
74
75 void
76 mi_interp::on_command_error ()
77 {
78 display_mi_prompt (this);
79 }
80
81 void
82 mi_interp::init (bool top_level)
83 {
84 mi_interp *mi = this;
85
86 /* Store the current output channel, so that we can create a console
87 channel that encapsulates and prefixes all gdb_output-type bits
88 coming from the rest of the debugger. */
89 mi->raw_stdout = gdb_stdout;
90
91 /* Create MI console channels, each with a different prefix so they
92 can be distinguished. */
93 mi->out = new mi_console_file (mi->raw_stdout, "~", '"');
94 mi->err = new mi_console_file (mi->raw_stdout, "&", '"');
95 mi->log = mi->err;
96 mi->targ = new mi_console_file (mi->raw_stdout, "@", '"');
97 mi->event_channel = new mi_console_file (mi->raw_stdout, "=", 0);
98 mi->mi_uiout = mi_out_new (name ()).release ();
99 gdb_assert (mi->mi_uiout != nullptr);
100 mi->cli_uiout = new cli_ui_out (mi->out);
101
102 if (top_level)
103 {
104 /* The initial inferior is created before this function is called, so we
105 need to report it explicitly when initializing the top-level MI
106 interpreter.
107
108 This is also called when additional MI interpreters are added (using
109 the new-ui command), when multiple inferiors possibly exist, so we need
110 to use iteration to report all the inferiors. */
111
112 for (inferior *inf : all_inferiors ())
113 mi->on_inferior_added (inf);
114 }
115 }
116
117 void
118 mi_interp::resume ()
119 {
120 struct mi_interp *mi = this;
121 struct ui *ui = current_ui;
122
123 /* As per hack note in mi_interpreter_init, swap in the output
124 channels... */
125 gdb_setup_readline (0);
126
127 ui->call_readline = gdb_readline_no_editing_callback;
128 ui->input_handler = mi_execute_command_input_handler;
129
130 gdb_stdout = mi->out;
131 /* Route error and log output through the MI. */
132 gdb_stderr = mi->err;
133 gdb_stdlog = mi->log;
134 /* Route target output through the MI. */
135 gdb_stdtarg = mi->targ;
136 /* Route target error through the MI as well. */
137 gdb_stdtargerr = mi->targ;
138
139 deprecated_show_load_progress = mi_load_progress;
140 }
141
142 void
143 mi_interp::suspend ()
144 {
145 gdb_disable_readline ();
146 }
147
148 void
149 mi_interp::exec (const char *command)
150 {
151 mi_execute_command_wrapper (command);
152 }
153
154 void
155 mi_cmd_interpreter_exec (const char *command, const char *const *argv,
156 int argc)
157 {
158 struct interp *interp_to_use;
159 int i;
160
161 if (argc < 2)
162 error (_("-interpreter-exec: "
163 "Usage: -interpreter-exec interp command"));
164
165 interp_to_use = interp_lookup (current_ui, argv[0]);
166 if (interp_to_use == NULL)
167 error (_("-interpreter-exec: could not find interpreter \"%s\""),
168 argv[0]);
169
170 /* Note that unlike the CLI version of this command, we don't
171 actually set INTERP_TO_USE as the current interpreter, as we
172 still want gdb_stdout, etc. to point at MI streams. */
173
174 /* Insert the MI out hooks, making sure to also call the
175 interpreter's hooks if it has any. */
176 /* KRS: We shouldn't need this... Events should be installed and
177 they should just ALWAYS fire something out down the MI
178 channel. */
179 mi_insert_notify_hooks ();
180
181 /* Now run the code. */
182
183 SCOPE_EXIT
184 {
185 mi_remove_notify_hooks ();
186 };
187
188 for (i = 1; i < argc; i++)
189 interp_exec (interp_to_use, argv[i]);
190 }
191
192 /* This inserts a number of hooks that are meant to produce
193 async-notify ("=") MI messages while running commands in another
194 interpreter using mi_interpreter_exec. The canonical use for this
195 is to allow access to the gdb CLI interpreter from within the MI,
196 while still producing MI style output when actions in the CLI
197 command change GDB's state. */
198
199 static void
200 mi_insert_notify_hooks (void)
201 {
202 deprecated_query_hook = mi_interp_query_hook;
203 }
204
205 static void
206 mi_remove_notify_hooks (void)
207 {
208 deprecated_query_hook = NULL;
209 }
210
211 static int
212 mi_interp_query_hook (const char *ctlstr, va_list ap)
213 {
214 return 1;
215 }
216
217 static void
218 mi_execute_command_wrapper (const char *cmd)
219 {
220 struct ui *ui = current_ui;
221
222 mi_execute_command (cmd, ui->instream == ui->stdin_stream);
223 }
224
225 void
226 mi_interp::on_sync_execution_done ()
227 {
228 /* If MI is sync, then output the MI prompt now, indicating we're
229 ready for further input. */
230 if (!mi_async_p ())
231 display_mi_prompt (this);
232 }
233
234 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
235
236 static void
237 mi_execute_command_input_handler (gdb::unique_xmalloc_ptr<char> &&cmd)
238 {
239 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
240 struct ui *ui = current_ui;
241
242 ui->prompt_state = PROMPT_NEEDED;
243
244 mi_execute_command_wrapper (cmd.get ());
245
246 /* Print a prompt, indicating we're ready for further input, unless
247 we just started a synchronous command. In that case, we're about
248 to go back to the event loop and will output the prompt in the
249 'synchronous_command_done' observer when the target next
250 stops. */
251 if (ui->prompt_state == PROMPT_NEEDED)
252 display_mi_prompt (mi);
253 }
254
255 void
256 mi_interp::pre_command_loop ()
257 {
258 struct mi_interp *mi = this;
259
260 /* Turn off 8 bit strings in quoted output. Any character with the
261 high bit set is printed using C's octal format. */
262 sevenbit_strings = 1;
263
264 /* Tell the world that we're alive. */
265 display_mi_prompt (mi);
266 }
267
268 void
269 mi_interp::on_new_thread (thread_info *t)
270 {
271 target_terminal::scoped_restore_terminal_state term_state;
272 target_terminal::ours_for_output ();
273
274 gdb_printf (this->event_channel, "thread-created,id=\"%d\",group-id=\"i%d\"",
275 t->global_num, t->inf->num);
276 gdb_flush (this->event_channel);
277 }
278
279 void
280 mi_interp::on_thread_exited (thread_info *t,
281 std::optional<ULONGEST> /* exit_code */,
282 int /* silent */)
283 {
284 target_terminal::scoped_restore_terminal_state term_state;
285 target_terminal::ours_for_output ();
286 gdb_printf (this->event_channel, "thread-exited,id=\"%d\",group-id=\"i%d\"",
287 t->global_num, t->inf->num);
288 gdb_flush (this->event_channel);
289 }
290
291 void
292 mi_interp::on_record_changed (inferior *inferior, int started,
293 const char *method, const char *format)
294 {
295 target_terminal::scoped_restore_terminal_state term_state;
296 target_terminal::ours_for_output ();
297
298 if (started)
299 {
300 if (format != NULL)
301 gdb_printf (this->event_channel,
302 "record-started,thread-group=\"i%d\","
303 "method=\"%s\",format=\"%s\"",
304 inferior->num, method, format);
305 else
306 gdb_printf (this->event_channel,
307 "record-started,thread-group=\"i%d\","
308 "method=\"%s\"",
309 inferior->num, method);
310 }
311 else
312 gdb_printf (this->event_channel,
313 "record-stopped,thread-group=\"i%d\"",
314 inferior->num);
315
316 gdb_flush (this->event_channel);
317 }
318
319 void
320 mi_interp::on_inferior_added (inferior *inf)
321 {
322 target_terminal::scoped_restore_terminal_state term_state;
323 target_terminal::ours_for_output ();
324
325 gdb_printf (this->event_channel, "thread-group-added,id=\"i%d\"", inf->num);
326 gdb_flush (this->event_channel);
327 }
328
329 void
330 mi_interp::on_inferior_appeared (inferior *inf)
331 {
332 target_terminal::scoped_restore_terminal_state term_state;
333 target_terminal::ours_for_output ();
334
335 gdb_printf (this->event_channel, "thread-group-started,id=\"i%d\",pid=\"%d\"",
336 inf->num, inf->pid);
337 gdb_flush (this->event_channel);
338 }
339
340 void
341 mi_interp::on_inferior_disappeared (inferior *inf)
342 {
343 target_terminal::scoped_restore_terminal_state term_state;
344 target_terminal::ours_for_output ();
345
346 if (inf->has_exit_code)
347 gdb_printf (this->event_channel,
348 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
349 inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
350 else
351 gdb_printf (this->event_channel,
352 "thread-group-exited,id=\"i%d\"", inf->num);
353
354 gdb_flush (this->event_channel);
355 }
356
357 void
358 mi_interp::on_inferior_removed (inferior *inf)
359 {
360 target_terminal::scoped_restore_terminal_state term_state;
361 target_terminal::ours_for_output ();
362
363 gdb_printf (this->event_channel, "thread-group-removed,id=\"i%d\"", inf->num);
364 gdb_flush (this->event_channel);
365 }
366
367 /* Observers for several run control events that print why the
368 inferior has stopped to both the MI event channel and to the MI
369 console. If the MI interpreter is not active, print nothing. */
370
371 void
372 mi_interp::on_signal_received (enum gdb_signal siggnal)
373 {
374 print_signal_received_reason (this->mi_uiout, siggnal);
375 print_signal_received_reason (this->cli_uiout, siggnal);
376 }
377
378 void
379 mi_interp::on_signal_exited (gdb_signal sig)
380 {
381 print_signal_exited_reason (this->mi_uiout, sig);
382 print_signal_exited_reason (this->cli_uiout, sig);
383 }
384
385 void
386 mi_interp::on_exited (int status)
387 {
388 print_exited_reason (this->mi_uiout, status);
389 print_exited_reason (this->cli_uiout, status);
390 }
391
392 void
393 mi_interp::on_no_history ()
394 {
395 print_no_history_reason (this->mi_uiout);
396 print_no_history_reason (this->cli_uiout);
397 }
398
399 void
400 mi_interp::on_normal_stop (struct bpstat *bs, int print_frame)
401 {
402 /* Since this can be called when CLI command is executing,
403 using cli interpreter, be sure to use MI uiout for output,
404 not the current one. */
405 ui_out *mi_uiout = this->interp_ui_out ();
406
407 if (print_frame)
408 {
409 thread_info *tp = inferior_thread ();
410
411 if (tp->thread_fsm () != nullptr
412 && tp->thread_fsm ()->finished_p ())
413 {
414 async_reply_reason reason
415 = tp->thread_fsm ()->async_reply_reason ();
416 mi_uiout->field_string ("reason", async_reason_lookup (reason));
417 }
418
419 interp *console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
420
421 /* We only want to print the displays once, and we want it to
422 look just how it would on the console, so we use this to
423 decide whether the MI stop should include them. */
424 bool console_print = should_print_stop_to_console (console_interp, tp);
425 print_stop_event (mi_uiout, !console_print);
426
427 if (console_print)
428 print_stop_event (this->cli_uiout);
429
430 mi_uiout->field_signed ("thread-id", tp->global_num);
431 if (non_stop)
432 {
433 ui_out_emit_list list_emitter (mi_uiout, "stopped-threads");
434
435 mi_uiout->field_signed (NULL, tp->global_num);
436 }
437 else
438 mi_uiout->field_string ("stopped-threads", "all");
439
440 int core = target_core_of_thread (tp->ptid);
441 if (core != -1)
442 mi_uiout->field_signed ("core", core);
443 }
444
445 gdb_puts ("*stopped", this->raw_stdout);
446 mi_out_put (mi_uiout, this->raw_stdout);
447 mi_out_rewind (mi_uiout);
448 mi_print_timing_maybe (this->raw_stdout);
449 gdb_puts ("\n", this->raw_stdout);
450 gdb_flush (this->raw_stdout);
451 }
452
453 void
454 mi_interp::on_about_to_proceed ()
455 {
456 /* Suppress output while calling an inferior function. */
457
458 if (inferior_ptid != null_ptid)
459 {
460 struct thread_info *tp = inferior_thread ();
461
462 if (tp->control.in_infcall)
463 return;
464 }
465
466 this->mi_proceeded = 1;
467 }
468
469 /* When the element is non-zero, no MI notifications will be emitted in
470 response to the corresponding observers. */
471
472 struct mi_suppress_notification mi_suppress_notification =
473 {
474 0,
475 0,
476 0,
477 0,
478 };
479
480 void
481 mi_interp::on_traceframe_changed (int tfnum, int tpnum)
482 {
483 if (mi_suppress_notification.traceframe)
484 return;
485
486 target_terminal::scoped_restore_terminal_state term_state;
487 target_terminal::ours_for_output ();
488
489 if (tfnum >= 0)
490 gdb_printf (this->event_channel, "traceframe-changed,"
491 "num=\"%d\",tracepoint=\"%d\"",
492 tfnum, tpnum);
493 else
494 gdb_printf (this->event_channel, "traceframe-changed,end");
495
496 gdb_flush (this->event_channel);
497 }
498
499 void
500 mi_interp::on_tsv_created (const trace_state_variable *tsv)
501 {
502 target_terminal::scoped_restore_terminal_state term_state;
503 target_terminal::ours_for_output ();
504
505 gdb_printf (this->event_channel, "tsv-created,"
506 "name=\"%s\",initial=\"%s\"",
507 tsv->name.c_str (), plongest (tsv->initial_value));
508
509 gdb_flush (this->event_channel);
510 }
511
512 void
513 mi_interp::on_tsv_deleted (const trace_state_variable *tsv)
514 {
515 target_terminal::scoped_restore_terminal_state term_state;
516 target_terminal::ours_for_output ();
517
518 if (tsv != nullptr)
519 gdb_printf (this->event_channel, "tsv-deleted,name=\"%s\"",
520 tsv->name.c_str ());
521 else
522 gdb_printf (this->event_channel, "tsv-deleted");
523
524 gdb_flush (this->event_channel);
525 }
526
527 void
528 mi_interp::on_tsv_modified (const trace_state_variable *tsv)
529 {
530 ui_out *mi_uiout = this->interp_ui_out ();
531
532 target_terminal::scoped_restore_terminal_state term_state;
533 target_terminal::ours_for_output ();
534
535 gdb_printf (this->event_channel,
536 "tsv-modified");
537
538 ui_out_redirect_pop redir (mi_uiout, this->event_channel);
539
540 mi_uiout->field_string ("name", tsv->name);
541 mi_uiout->field_string ("initial",
542 plongest (tsv->initial_value));
543 if (tsv->value_known)
544 mi_uiout->field_string ("current", plongest (tsv->value));
545
546 gdb_flush (this->event_channel);
547 }
548
549 /* Print breakpoint BP on MI's event channel. */
550
551 static void
552 mi_print_breakpoint_for_event (struct mi_interp *mi, breakpoint *bp)
553 {
554 ui_out *mi_uiout = mi->interp_ui_out ();
555
556 /* We want the output from print_breakpoint to go to
557 mi->event_channel. One approach would be to just call
558 print_breakpoint, and then use mi_out_put to send the current
559 content of mi_uiout into mi->event_channel. However, that will
560 break if anything is output to mi_uiout prior to calling the
561 breakpoint_created notifications. So, we use
562 ui_out_redirect. */
563 ui_out_redirect_pop redir (mi_uiout, mi->event_channel);
564
565 try
566 {
567 scoped_restore restore_uiout
568 = make_scoped_restore (&current_uiout, mi_uiout);
569
570 print_breakpoint (bp);
571 }
572 catch (const gdb_exception_error &ex)
573 {
574 exception_print (gdb_stderr, ex);
575 }
576 }
577
578 void
579 mi_interp::on_breakpoint_created (breakpoint *b)
580 {
581 if (mi_suppress_notification.breakpoint)
582 return;
583
584 if (b->number <= 0)
585 return;
586
587 target_terminal::scoped_restore_terminal_state term_state;
588 target_terminal::ours_for_output ();
589
590 gdb_printf (this->event_channel, "breakpoint-created");
591 mi_print_breakpoint_for_event (this, b);
592
593 gdb_flush (this->event_channel);
594 }
595
596 void
597 mi_interp::on_breakpoint_deleted (breakpoint *b)
598 {
599 if (mi_suppress_notification.breakpoint)
600 return;
601
602 if (b->number <= 0)
603 return;
604
605 target_terminal::scoped_restore_terminal_state term_state;
606 target_terminal::ours_for_output ();
607
608 gdb_printf (this->event_channel, "breakpoint-deleted,id=\"%d\"", b->number);
609 gdb_flush (this->event_channel);
610 }
611
612 void
613 mi_interp::on_breakpoint_modified (breakpoint *b)
614 {
615 if (mi_suppress_notification.breakpoint)
616 return;
617
618 if (b->number <= 0)
619 return;
620
621 target_terminal::scoped_restore_terminal_state term_state;
622 target_terminal::ours_for_output ();
623 gdb_printf (this->event_channel, "breakpoint-modified");
624 mi_print_breakpoint_for_event (this, b);
625
626 gdb_flush (this->event_channel);
627 }
628
629 static void
630 mi_output_running (struct thread_info *thread)
631 {
632 SWITCH_THRU_ALL_UIS ()
633 {
634 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
635
636 if (mi == NULL)
637 continue;
638
639 gdb_printf (mi->raw_stdout,
640 "*running,thread-id=\"%d\"\n",
641 thread->global_num);
642 }
643 }
644
645 /* Return true if there are multiple inferiors loaded. This is used
646 for backwards compatibility -- if there's only one inferior, output
647 "all", otherwise, output each resumed thread individually. */
648
649 static bool
650 multiple_inferiors_p ()
651 {
652 int count = 0;
653 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
654 {
655 count++;
656 if (count > 1)
657 return true;
658 }
659
660 return false;
661 }
662
663 static void
664 mi_on_resume_1 (struct mi_interp *mi,
665 process_stratum_target *targ, ptid_t ptid)
666 {
667 /* To cater for older frontends, emit ^running, but do it only once
668 per each command. We do it here, since at this point we know
669 that the target was successfully resumed, and in non-async mode,
670 we won't return back to MI interpreter code until the target
671 is done running, so delaying the output of "^running" until then
672 will make it impossible for frontend to know what's going on.
673
674 In future (MI3), we'll be outputting "^done" here. */
675 if (!mi->running_result_record_printed && mi->mi_proceeded)
676 {
677 gdb_printf (mi->raw_stdout, "%s^running\n",
678 mi->current_token ? mi->current_token : "");
679 }
680
681 /* Backwards compatibility. If doing a wildcard resume and there's
682 only one inferior, output "all", otherwise, output each resumed
683 thread individually. */
684 if ((ptid == minus_one_ptid || ptid.is_pid ())
685 && !multiple_inferiors_p ())
686 gdb_printf (mi->raw_stdout, "*running,thread-id=\"all\"\n");
687 else
688 for (thread_info *tp : all_non_exited_threads (targ, ptid))
689 mi_output_running (tp);
690
691 if (!mi->running_result_record_printed && mi->mi_proceeded)
692 {
693 mi->running_result_record_printed = 1;
694 /* This is what gdb used to do historically -- printing prompt
695 even if it cannot actually accept any input. This will be
696 surely removed for MI3, and may be removed even earlier. */
697 if (current_ui->prompt_state == PROMPT_BLOCKED)
698 gdb_puts ("(gdb) \n", mi->raw_stdout);
699 }
700 gdb_flush (mi->raw_stdout);
701 }
702
703 void
704 mi_interp::on_target_resumed (ptid_t ptid)
705 {
706 struct thread_info *tp = NULL;
707
708 process_stratum_target *target = current_inferior ()->process_target ();
709 if (ptid == minus_one_ptid || ptid.is_pid ())
710 tp = inferior_thread ();
711 else
712 tp = target->find_thread (ptid);
713
714 /* Suppress output while calling an inferior function. */
715 if (tp->control.in_infcall)
716 return;
717
718 target_terminal::scoped_restore_terminal_state term_state;
719 target_terminal::ours_for_output ();
720
721 mi_on_resume_1 (this, target, ptid);
722 }
723
724 /* See mi-interp.h. */
725
726 void
727 mi_output_solib_attribs (ui_out *uiout, const solib &solib)
728 {
729 gdbarch *gdbarch = current_inferior ()->arch ();
730
731 uiout->field_string ("id", solib.so_original_name);
732 uiout->field_string ("target-name", solib.so_original_name);
733 uiout->field_string ("host-name", solib.so_name);
734 uiout->field_signed ("symbols-loaded", solib.symbols_loaded);
735 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
736 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
737
738 ui_out_emit_list list_emitter (uiout, "ranges");
739 ui_out_emit_tuple tuple_emitter (uiout, NULL);
740 if (solib.addr_high != 0)
741 {
742 uiout->field_core_addr ("from", gdbarch, solib.addr_low);
743 uiout->field_core_addr ("to", gdbarch, solib.addr_high);
744 }
745 }
746
747 void
748 mi_interp::on_solib_loaded (const solib &solib)
749 {
750 ui_out *uiout = this->interp_ui_out ();
751
752 target_terminal::scoped_restore_terminal_state term_state;
753 target_terminal::ours_for_output ();
754
755 gdb_printf (this->event_channel, "library-loaded");
756
757 ui_out_redirect_pop redir (uiout, this->event_channel);
758
759 mi_output_solib_attribs (uiout, solib);
760
761 gdb_flush (this->event_channel);
762 }
763
764 void
765 mi_interp::on_solib_unloaded (const solib &solib)
766 {
767 ui_out *uiout = this->interp_ui_out ();
768
769 target_terminal::scoped_restore_terminal_state term_state;
770 target_terminal::ours_for_output ();
771
772 gdb_printf (this->event_channel, "library-unloaded");
773
774 ui_out_redirect_pop redir (uiout, this->event_channel);
775
776 uiout->field_string ("id", solib.so_original_name);
777 uiout->field_string ("target-name", solib.so_original_name);
778 uiout->field_string ("host-name", solib.so_name);
779 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
780 uiout->field_fmt ("thread-group", "i%d", current_inferior ()->num);
781
782 gdb_flush (this->event_channel);
783 }
784
785 void
786 mi_interp::on_param_changed (const char *param, const char *value)
787 {
788 if (mi_suppress_notification.cmd_param_changed)
789 return;
790
791 ui_out *mi_uiout = this->interp_ui_out ();
792
793 target_terminal::scoped_restore_terminal_state term_state;
794 target_terminal::ours_for_output ();
795
796 gdb_printf (this->event_channel, "cmd-param-changed");
797
798 ui_out_redirect_pop redir (mi_uiout, this->event_channel);
799
800 mi_uiout->field_string ("param", param);
801 mi_uiout->field_string ("value", value);
802
803 gdb_flush (this->event_channel);
804 }
805
806 void
807 mi_interp::on_memory_changed (inferior *inferior, CORE_ADDR memaddr,
808 ssize_t len, const bfd_byte *myaddr)
809 {
810 if (mi_suppress_notification.memory)
811 return;
812
813
814 ui_out *mi_uiout = this->interp_ui_out ();
815
816 target_terminal::scoped_restore_terminal_state term_state;
817 target_terminal::ours_for_output ();
818
819 gdb_printf (this->event_channel, "memory-changed");
820
821 ui_out_redirect_pop redir (mi_uiout, this->event_channel);
822
823 mi_uiout->field_fmt ("thread-group", "i%d", inferior->num);
824 mi_uiout->field_core_addr ("addr", current_inferior ()->arch (), memaddr);
825 mi_uiout->field_string ("len", hex_string (len));
826
827 /* Append 'type=code' into notification if MEMADDR falls in the range of
828 sections contain code. */
829 obj_section *sec = find_pc_section (memaddr);
830 if (sec != nullptr && sec->objfile != nullptr)
831 {
832 flagword flags = bfd_section_flags (sec->the_bfd_section);
833
834 if (flags & SEC_CODE)
835 mi_uiout->field_string ("type", "code");
836 }
837
838 gdb_flush (this->event_channel);
839 }
840
841 void
842 mi_interp::on_user_selected_context_changed (user_selected_what selection)
843 {
844 /* Don't send an event if we're responding to an MI command. */
845 if (mi_suppress_notification.user_selected_context)
846 return;
847
848 thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : nullptr;
849 ui_out *mi_uiout = this->interp_ui_out ();
850 ui_out_redirect_pop redirect_popper (mi_uiout, this->event_channel);
851
852 target_terminal::scoped_restore_terminal_state term_state;
853 target_terminal::ours_for_output ();
854
855 if (selection & USER_SELECTED_INFERIOR)
856 print_selected_inferior (this->cli_uiout);
857
858 if (tp != NULL
859 && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
860 {
861 print_selected_thread_frame (this->cli_uiout, selection);
862
863 gdb_printf (this->event_channel, "thread-selected,id=\"%d\"",
864 tp->global_num);
865
866 if (tp->state != THREAD_RUNNING)
867 {
868 if (has_stack_frames ())
869 print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
870 1, SRC_AND_LOC, 1);
871 }
872 }
873
874 gdb_flush (this->event_channel);
875 }
876
877 ui_out *
878 mi_interp::interp_ui_out ()
879 {
880 return this->mi_uiout;
881 }
882
883 /* Do MI-specific logging actions; save raw_stdout, and change all
884 the consoles to use the supplied ui-file(s). */
885
886 void
887 mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
888 bool debug_redirect)
889 {
890 struct mi_interp *mi = this;
891
892 if (logfile != NULL)
893 {
894 mi->saved_raw_stdout = mi->raw_stdout;
895
896 ui_file *logfile_p = logfile.get ();
897 mi->logfile_holder = std::move (logfile);
898
899 /* If something is not being redirected, then a tee containing both the
900 logfile and stdout. */
901 ui_file *tee = nullptr;
902 if (!logging_redirect || !debug_redirect)
903 {
904 tee = new tee_file (mi->raw_stdout, logfile_p);
905 mi->stdout_holder.reset (tee);
906 }
907
908 mi->raw_stdout = logging_redirect ? logfile_p : tee;
909 }
910 else
911 {
912 mi->logfile_holder.reset ();
913 mi->stdout_holder.reset ();
914 mi->raw_stdout = mi->saved_raw_stdout;
915 mi->saved_raw_stdout = nullptr;
916 }
917
918 mi->out->set_raw (mi->raw_stdout);
919 mi->err->set_raw (mi->raw_stdout);
920 mi->log->set_raw (mi->raw_stdout);
921 mi->targ->set_raw (mi->raw_stdout);
922 mi->event_channel->set_raw (mi->raw_stdout);
923 }
924
925 /* Factory for MI interpreters. */
926
927 static struct interp *
928 mi_interp_factory (const char *name)
929 {
930 return new mi_interp (name);
931 }
932
933 void _initialize_mi_interp ();
934 void
935 _initialize_mi_interp ()
936 {
937 /* The various interpreter levels. */
938 interp_factory_register (INTERP_MI2, mi_interp_factory);
939 interp_factory_register (INTERP_MI3, mi_interp_factory);
940 interp_factory_register (INTERP_MI4, mi_interp_factory);
941 interp_factory_register (INTERP_MI, mi_interp_factory);
942 }