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