Make command line editing (use of readline) be per UI
[deliverable/binutils-gdb.git] / gdb / mi / mi-interp.c
CommitLineData
4a8f6654
AC
1/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 2002-2016 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"
683f2885
VP
33#include "observer.h"
34#include "gdbthread.h"
c86cf029 35#include "solist.h"
8d3788bd 36#include "gdb.h"
8de0566d 37#include "objfiles.h"
134a2066 38#include "tracepoint.h"
17b2616c 39#include "cli-out.h"
243a9253 40#include "thread-fsm.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);
e837f12a 46static void mi_execute_command_input_handler (char *cmd);
1af12a7d 47static void mi_command_loop (void *data);
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
73ab01a0
PA
92/* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
93 returns NULL otherwise. */
94
95static struct mi_interp *
96as_mi_interp (struct interp *interp)
97{
98 if (ui_out_is_mi_like_p (interp_ui_out (interp)))
99 return (struct mi_interp *) interp_data (interp);
100 return NULL;
101}
102
4a8f6654 103static void *
4801a9a3 104mi_interpreter_init (struct interp *interp, int top_level)
4a8f6654 105{
70ba0933 106 struct mi_interp *mi = XNEW (struct mi_interp);
4801a9a3
PA
107 const char *name;
108 int mi_version;
4a8f6654 109
bf6309af
SS
110 /* Assign the output channel created at startup to its own global,
111 so that we can create a console channel that encapsulates and
112 prefixes all gdb_output-type bits coming from the rest of the
113 debugger. */
4a8f6654 114
bf6309af 115 raw_stdout = gdb_stdout;
4a8f6654 116
2b03b41d
SS
117 /* Create MI console channels, each with a different prefix so they
118 can be distinguished. */
4a8f6654
AC
119 mi->out = mi_console_file_new (raw_stdout, "~", '"');
120 mi->err = mi_console_file_new (raw_stdout, "&", '"');
121 mi->log = mi->err;
122 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
123 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
124
4801a9a3
PA
125 name = interp_name (interp);
126 /* INTERP_MI selects the most recent released version. "mi2" was
127 released as part of GDB 6.0. */
128 if (strcmp (name, INTERP_MI) == 0)
129 mi_version = 2;
130 else if (strcmp (name, INTERP_MI1) == 0)
131 mi_version = 1;
132 else if (strcmp (name, INTERP_MI2) == 0)
133 mi_version = 2;
134 else if (strcmp (name, INTERP_MI3) == 0)
135 mi_version = 3;
136 else
137 gdb_assert_not_reached ("unhandled MI version");
138
fd664c91
PA
139 mi->mi_uiout = mi_out_new (mi_version);
140 mi->cli_uiout = cli_out_new (mi->out);
141
683f2885 142 if (top_level)
063bfe2e 143 {
2b03b41d
SS
144 /* The initial inferior is created before this function is
145 called, so we need to report it explicitly. Use iteration in
146 case future version of GDB creates more than one inferior
147 up-front. */
a79b8f6e 148 iterate_over_inferiors (report_initial_inferior, mi);
063bfe2e 149 }
683f2885 150
4a8f6654
AC
151 return mi;
152}
153
154static int
155mi_interpreter_resume (void *data)
156{
19ba03f4 157 struct mi_interp *mi = (struct mi_interp *) data;
a74e1786 158 struct ui *ui = current_ui;
4a8f6654 159
2b03b41d
SS
160 /* As per hack note in mi_interpreter_init, swap in the output
161 channels... */
3c216924 162 gdb_setup_readline (0);
4a8f6654 163
a74e1786
PA
164 ui->call_readline = gdb_readline_no_editing_callback;
165 ui->input_handler = mi_execute_command_input_handler;
362646f5
AC
166 /* FIXME: This is a total hack for now. PB's use of the MI
167 implicitly relies on a bug in the async support which allows
168 asynchronous commands to leak through the commmand loop. The bug
169 involves (but is not limited to) the fact that sync_execution was
170 erroneously initialized to 0. Duplicate by initializing it thus
171 here... */
172 sync_execution = 0;
4a8f6654
AC
173
174 gdb_stdout = mi->out;
2b03b41d 175 /* Route error and log output through the MI. */
4a8f6654
AC
176 gdb_stderr = mi->err;
177 gdb_stdlog = mi->log;
2b03b41d 178 /* Route target output through the MI. */
4a8f6654 179 gdb_stdtarg = mi->targ;
2b03b41d 180 /* Route target error through the MI as well. */
1f20321b 181 gdb_stdtargerr = mi->targ;
4a8f6654
AC
182
183 /* Replace all the hooks that we know about. There really needs to
184 be a better way of doing this... */
185 clear_interpreter_hooks ();
186
9a4105ab 187 deprecated_show_load_progress = mi_load_progress;
4a8f6654 188
4a8f6654
AC
189 return 1;
190}
191
192static int
193mi_interpreter_suspend (void *data)
194{
195 gdb_disable_readline ();
196 return 1;
197}
198
71fff37b 199static struct gdb_exception
4a8f6654
AC
200mi_interpreter_exec (void *data, const char *command)
201{
ee047554 202 mi_execute_command_wrapper (command);
c1043fc2 203 return exception_none;
4a8f6654
AC
204}
205
ce8f13f8 206void
4a8f6654
AC
207mi_cmd_interpreter_exec (char *command, char **argv, int argc)
208{
209 struct interp *interp_to_use;
4a8f6654 210 int i;
a13e061a
PA
211 char *mi_error_message = NULL;
212 struct cleanup *old_chain;
4a8f6654
AC
213
214 if (argc < 2)
1b05df00 215 error (_("-interpreter-exec: "
9b20d036 216 "Usage: -interpreter-exec interp command"));
4a8f6654 217
8322445e 218 interp_to_use = interp_lookup (current_ui, argv[0]);
4a8f6654 219 if (interp_to_use == NULL)
1b05df00 220 error (_("-interpreter-exec: could not find interpreter \"%s\""),
9a2b4c1b 221 argv[0]);
4a8f6654 222
17b2616c
PA
223 /* Note that unlike the CLI version of this command, we don't
224 actually set INTERP_TO_USE as the current interpreter, as we
225 still want gdb_stdout, etc. to point at MI streams. */
226
2b03b41d
SS
227 /* Insert the MI out hooks, making sure to also call the
228 interpreter's hooks if it has any. */
229 /* KRS: We shouldn't need this... Events should be installed and
230 they should just ALWAYS fire something out down the MI
231 channel. */
4a8f6654
AC
232 mi_insert_notify_hooks ();
233
2b03b41d 234 /* Now run the code. */
4a8f6654 235
a13e061a 236 old_chain = make_cleanup (null_cleanup, 0);
4a8f6654
AC
237 for (i = 1; i < argc; i++)
238 {
32c1e744 239 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
102040f0 240
32c1e744
VP
241 if (e.reason < 0)
242 {
243 mi_error_message = xstrdup (e.message);
a13e061a 244 make_cleanup (xfree, mi_error_message);
32c1e744
VP
245 break;
246 }
4a8f6654
AC
247 }
248
249 mi_remove_notify_hooks ();
250
a13e061a
PA
251 if (mi_error_message != NULL)
252 error ("%s", mi_error_message);
253 do_cleanups (old_chain);
4a8f6654
AC
254}
255
2b03b41d
SS
256/* This inserts a number of hooks that are meant to produce
257 async-notify ("=") MI messages while running commands in another
258 interpreter using mi_interpreter_exec. The canonical use for this
259 is to allow access to the gdb CLI interpreter from within the MI,
260 while still producing MI style output when actions in the CLI
261 command change GDB's state. */
4a8f6654
AC
262
263static void
264mi_insert_notify_hooks (void)
265{
9a4105ab 266 deprecated_query_hook = mi_interp_query_hook;
4a8f6654
AC
267}
268
269static void
11308a41 270mi_remove_notify_hooks (void)
4a8f6654 271{
9a4105ab 272 deprecated_query_hook = NULL;
4a8f6654
AC
273}
274
275static int
276mi_interp_query_hook (const char *ctlstr, va_list ap)
277{
278 return 1;
279}
280
4a8f6654 281static void
ee047554 282mi_execute_command_wrapper (const char *cmd)
4a8f6654 283{
f38d3ad1
PA
284 struct ui *ui = current_ui;
285
286 mi_execute_command (cmd, stdin == ui->instream);
4a8f6654
AC
287}
288
329ea579
PA
289/* Observer for the synchronous_command_done notification. */
290
291static void
292mi_on_sync_execution_done (void)
293{
73ab01a0
PA
294 struct ui *ui = current_ui;
295 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
296
297 if (mi == NULL)
298 return;
299
0b333c5e
PA
300 /* If MI is sync, then output the MI prompt now, indicating we're
301 ready for further input. */
329ea579
PA
302 if (!mi_async_p ())
303 {
304 fputs_unfiltered ("(gdb) \n", raw_stdout);
305 gdb_flush (raw_stdout);
306 }
307}
308
e837f12a
JK
309/* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER. */
310
311static void
312mi_execute_command_input_handler (char *cmd)
313{
314 mi_execute_command_wrapper (cmd);
315
0b333c5e
PA
316 /* Print a prompt, indicating we're ready for further input, unless
317 we just started a synchronous command. In that case, we're about
318 to go back to the event loop and will output the prompt in the
319 'synchronous_command_done' observer when the target next
320 stops. */
321 if (!sync_execution)
329ea579
PA
322 {
323 fputs_unfiltered ("(gdb) \n", raw_stdout);
324 gdb_flush (raw_stdout);
325 }
e837f12a
JK
326}
327
4a8f6654 328static void
1af12a7d 329mi_command_loop (void *data)
4a8f6654 330{
4a8f6654 331 /* Turn off 8 bit strings in quoted output. Any character with the
2b03b41d 332 high bit set is printed using C's octal format. */
4a8f6654 333 sevenbit_strings = 1;
2b03b41d
SS
334
335 /* Tell the world that we're alive. */
4a8f6654
AC
336 fputs_unfiltered ("(gdb) \n", raw_stdout);
337 gdb_flush (raw_stdout);
2b03b41d 338
362646f5 339 start_event_loop ();
4a8f6654
AC
340}
341
683f2885
VP
342static void
343mi_new_thread (struct thread_info *t)
344{
c9657e70 345 struct inferior *inf = find_inferior_ptid (t->ptid);
73ab01a0 346 struct switch_thru_all_uis state;
a79b8f6e
VP
347
348 gdb_assert (inf);
683f2885 349
73ab01a0
PA
350 SWITCH_THRU_ALL_UIS (state)
351 {
352 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
353 struct cleanup *old_chain;
5fe96654 354
73ab01a0
PA
355 if (mi == NULL)
356 continue;
5fe96654 357
73ab01a0
PA
358 old_chain = make_cleanup_restore_target_terminal ();
359 target_terminal_ours_for_output ();
360
361 fprintf_unfiltered (mi->event_channel,
362 "thread-created,id=\"%d\",group-id=\"i%d\"",
363 t->global_num, inf->num);
364 gdb_flush (mi->event_channel);
365
366 do_cleanups (old_chain);
367 }
683f2885
VP
368}
369
063bfe2e 370static void
a07daef3 371mi_thread_exit (struct thread_info *t, int silent)
063bfe2e 372{
73ab01a0 373 struct switch_thru_all_uis state;
a07daef3
PA
374
375 if (silent)
376 return;
377
73ab01a0
PA
378 SWITCH_THRU_ALL_UIS (state)
379 {
380 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
381 struct cleanup *old_chain;
a79b8f6e 382
73ab01a0
PA
383 if (mi == NULL)
384 continue;
5fe96654 385
73ab01a0
PA
386 old_chain = make_cleanup_restore_target_terminal ();
387 target_terminal_ours_for_output ();
388 fprintf_unfiltered (mi->event_channel,
389 "thread-exited,id=\"%d\",group-id=\"i%d\"",
390 t->global_num, t->inf->num);
391 gdb_flush (mi->event_channel);
1abf3a14 392
73ab01a0
PA
393 do_cleanups (old_chain);
394 }
063bfe2e
VP
395}
396
82a90ccf
YQ
397/* Emit notification on changing the state of record. */
398
399static void
38b022b4
SM
400mi_record_changed (struct inferior *inferior, int started, const char *method,
401 const char *format)
82a90ccf 402{
73ab01a0 403 struct switch_thru_all_uis state;
82a90ccf 404
73ab01a0 405 SWITCH_THRU_ALL_UIS (state)
38b022b4 406 {
73ab01a0
PA
407 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
408 struct cleanup *old_chain;
409
410 if (mi == NULL)
411 continue;
412
413 old_chain = make_cleanup_restore_target_terminal ();
414 target_terminal_ours_for_output ();
415
416 if (started)
1aec0b6a 417 {
73ab01a0
PA
418 if (format != NULL)
419 {
420 fprintf_unfiltered (mi->event_channel,
421 "record-started,thread-group=\"i%d\","
422 "method=\"%s\",format=\"%s\"",
423 inferior->num, method, format);
424 }
425 else
426 {
427 fprintf_unfiltered (mi->event_channel,
428 "record-started,thread-group=\"i%d\","
429 "method=\"%s\"",
430 inferior->num, method);
431 }
1aec0b6a 432 }
38b022b4 433 else
1aec0b6a 434 {
73ab01a0
PA
435 fprintf_unfiltered (mi->event_channel,
436 "record-stopped,thread-group=\"i%d\"",
437 inferior->num);
1aec0b6a 438 }
38b022b4 439
73ab01a0 440 gdb_flush (mi->event_channel);
82a90ccf 441
73ab01a0
PA
442 do_cleanups (old_chain);
443 }
82a90ccf
YQ
444}
445
a79b8f6e
VP
446static void
447mi_inferior_added (struct inferior *inf)
448{
73ab01a0 449 struct switch_thru_all_uis state;
5fe96654 450
73ab01a0
PA
451 SWITCH_THRU_ALL_UIS (state)
452 {
453 struct interp *interp;
454 struct mi_interp *mi;
455 struct cleanup *old_chain;
102040f0 456
73ab01a0
PA
457 /* We'll be called once for the initial inferior, before the top
458 level interpreter is set. */
459 interp = top_level_interpreter ();
460 if (interp == NULL)
461 continue;
5fe96654 462
73ab01a0
PA
463 mi = as_mi_interp (interp);
464 if (mi == NULL)
465 continue;
466
467 old_chain = make_cleanup_restore_target_terminal ();
468 target_terminal_ours_for_output ();
469
470 fprintf_unfiltered (mi->event_channel,
471 "thread-group-added,id=\"i%d\"",
472 inf->num);
473 gdb_flush (mi->event_channel);
474
475 do_cleanups (old_chain);
476 }
a79b8f6e
VP
477}
478
479static void
480mi_inferior_appeared (struct inferior *inf)
4a92f99b 481{
73ab01a0 482 struct switch_thru_all_uis state;
5fe96654 483
73ab01a0
PA
484 SWITCH_THRU_ALL_UIS (state)
485 {
486 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
487 struct cleanup *old_chain;
102040f0 488
73ab01a0
PA
489 if (mi == NULL)
490 continue;
5fe96654 491
73ab01a0
PA
492 old_chain = make_cleanup_restore_target_terminal ();
493 target_terminal_ours_for_output ();
494
495 fprintf_unfiltered (mi->event_channel,
496 "thread-group-started,id=\"i%d\",pid=\"%d\"",
497 inf->num, inf->pid);
498 gdb_flush (mi->event_channel);
499 do_cleanups (old_chain);
500 }
4a92f99b
VP
501}
502
503static void
a79b8f6e 504mi_inferior_exit (struct inferior *inf)
4a92f99b 505{
73ab01a0 506 struct switch_thru_all_uis state;
5fe96654 507
73ab01a0
PA
508 SWITCH_THRU_ALL_UIS (state)
509 {
510 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
511 struct cleanup *old_chain;
102040f0 512
73ab01a0
PA
513 if (mi == NULL)
514 continue;
8cf64490 515
73ab01a0
PA
516 old_chain = make_cleanup_restore_target_terminal ();
517 target_terminal_ours_for_output ();
518
519 if (inf->has_exit_code)
520 fprintf_unfiltered (mi->event_channel,
521 "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
522 inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
523 else
524 fprintf_unfiltered (mi->event_channel,
525 "thread-group-exited,id=\"i%d\"", inf->num);
526
527 gdb_flush (mi->event_channel);
528 do_cleanups (old_chain);
529 }
4a92f99b
VP
530}
531
a79b8f6e
VP
532static void
533mi_inferior_removed (struct inferior *inf)
534{
73ab01a0 535 struct switch_thru_all_uis state;
5fe96654 536
73ab01a0
PA
537 SWITCH_THRU_ALL_UIS (state)
538 {
539 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
540 struct cleanup *old_chain;
102040f0 541
73ab01a0
PA
542 if (mi == NULL)
543 continue;
5fe96654 544
73ab01a0
PA
545 old_chain = make_cleanup_restore_target_terminal ();
546 target_terminal_ours_for_output ();
547
548 fprintf_unfiltered (mi->event_channel,
549 "thread-group-removed,id=\"i%d\"",
550 inf->num);
551 gdb_flush (mi->event_channel);
552
553 do_cleanups (old_chain);
554 }
a79b8f6e
VP
555}
556
fd664c91
PA
557/* Return the MI interpreter, if it is active -- either because it's
558 the top-level interpreter or the interpreter executing the current
559 command. Returns NULL if the MI interpreter is not being used. */
560
73ab01a0
PA
561static struct mi_interp *
562find_mi_interp (void)
fd664c91 563{
73ab01a0 564 struct mi_interp *mi;
fd664c91 565
73ab01a0
PA
566 mi = as_mi_interp (top_level_interpreter ());
567 if (mi != NULL)
568 return mi;
fd664c91 569
73ab01a0
PA
570 mi = as_mi_interp (command_interp ());
571 if (mi != NULL)
572 return mi;
fd664c91 573
fd664c91
PA
574 return NULL;
575}
576
577/* Observers for several run control events that print why the
578 inferior has stopped to both the the MI event channel and to the MI
579 console. If the MI interpreter is not active, print nothing. */
580
581/* Observer for the signal_received notification. */
582
583static void
584mi_on_signal_received (enum gdb_signal siggnal)
585{
73ab01a0 586 struct switch_thru_all_uis state;
fd664c91 587
73ab01a0
PA
588 SWITCH_THRU_ALL_UIS (state)
589 {
590 struct mi_interp *mi = find_mi_interp ();
591
592 if (mi == NULL)
593 continue;
fd664c91 594
73ab01a0
PA
595 print_signal_received_reason (mi->mi_uiout, siggnal);
596 print_signal_received_reason (mi->cli_uiout, siggnal);
597 }
fd664c91
PA
598}
599
600/* Observer for the end_stepping_range notification. */
601
602static void
603mi_on_end_stepping_range (void)
604{
73ab01a0 605 struct switch_thru_all_uis state;
fd664c91 606
73ab01a0
PA
607 SWITCH_THRU_ALL_UIS (state)
608 {
609 struct mi_interp *mi = find_mi_interp ();
610
611 if (mi == NULL)
612 continue;
fd664c91 613
73ab01a0
PA
614 print_end_stepping_range_reason (mi->mi_uiout);
615 print_end_stepping_range_reason (mi->cli_uiout);
616 }
fd664c91
PA
617}
618
619/* Observer for the signal_exited notification. */
17b2616c
PA
620
621static void
fd664c91 622mi_on_signal_exited (enum gdb_signal siggnal)
17b2616c 623{
73ab01a0 624 struct switch_thru_all_uis state;
17b2616c 625
73ab01a0
PA
626 SWITCH_THRU_ALL_UIS (state)
627 {
628 struct mi_interp *mi = find_mi_interp ();
629
630 if (mi == NULL)
631 continue;
fd664c91 632
73ab01a0
PA
633 print_signal_exited_reason (mi->mi_uiout, siggnal);
634 print_signal_exited_reason (mi->cli_uiout, siggnal);
635 }
fd664c91
PA
636}
637
638/* Observer for the exited notification. */
639
640static void
641mi_on_exited (int exitstatus)
642{
73ab01a0 643 struct switch_thru_all_uis state;
fd664c91 644
73ab01a0
PA
645 SWITCH_THRU_ALL_UIS (state)
646 {
647 struct mi_interp *mi = find_mi_interp ();
648
649 if (mi == NULL)
650 continue;
fd664c91 651
73ab01a0
PA
652 print_exited_reason (mi->mi_uiout, exitstatus);
653 print_exited_reason (mi->cli_uiout, exitstatus);
654 }
fd664c91
PA
655}
656
657/* Observer for the no_history notification. */
658
659static void
660mi_on_no_history (void)
661{
73ab01a0 662 struct switch_thru_all_uis state;
fd664c91 663
73ab01a0
PA
664 SWITCH_THRU_ALL_UIS (state)
665 {
666 struct mi_interp *mi = find_mi_interp ();
fd664c91 667
73ab01a0
PA
668 if (mi == NULL)
669 continue;
670
671 print_no_history_reason (mi->mi_uiout);
672 print_no_history_reason (mi->cli_uiout);
673 }
17b2616c
PA
674}
675
f7f9a841 676static void
73ab01a0 677mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
f7f9a841
VP
678{
679 /* Since this can be called when CLI command is executing,
680 using cli interpreter, be sure to use MI uiout for output,
681 not the current one. */
1d33d6ba 682 struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
f7f9a841 683
1d33d6ba
VP
684 if (print_frame)
685 {
243a9253 686 struct thread_info *tp;
dc146f7c 687 int core;
102040f0 688
243a9253 689 tp = inferior_thread ();
36dfb11c 690
243a9253
PA
691 if (tp->thread_fsm != NULL
692 && thread_fsm_finished_p (tp->thread_fsm))
693 {
694 enum async_reply_reason reason;
36dfb11c 695
243a9253
PA
696 reason = thread_fsm_async_reply_reason (tp->thread_fsm);
697 ui_out_field_string (mi_uiout, "reason",
698 async_reason_lookup (reason));
1d33d6ba 699 }
243a9253
PA
700 print_stop_event (mi_uiout);
701
702 /* Breakpoint hits should always be mirrored to the console.
703 Deciding what to mirror to the console wrt to breakpoints and
704 random stops gets messy real fast. E.g., say "s" trips on a
705 breakpoint. We'd clearly want to mirror the event to the
706 console in this case. But what about more complicated cases
707 like "s&; thread n; s&", and one of those steps spawning a
708 new thread, and that thread hitting a breakpoint? It's
709 impossible in general to track whether the thread had any
710 relation to the commands that had been executed. So we just
711 simplify and always mirror breakpoints and random events to
712 the console.
713
714 OTOH, we should print the source line to the console when
715 stepping or other similar commands, iff the step was started
716 by a console command, but not if it was started with
717 -exec-step or similar. */
718 if ((bpstat_what (tp->control.stop_bpstat).main_action
719 == BPSTAT_WHAT_STOP_NOISY)
720 || !(tp->thread_fsm != NULL
721 && thread_fsm_finished_p (tp->thread_fsm))
722 || (tp->control.command_interp != NULL
723 && tp->control.command_interp != top_level_interpreter ()))
17b2616c 724 {
19ba03f4
SM
725 struct mi_interp *mi
726 = (struct mi_interp *) top_level_interpreter_data ();
243a9253
PA
727
728 print_stop_event (mi->cli_uiout);
17b2616c 729 }
1d33d6ba 730
5d5658a1
PA
731 tp = inferior_thread ();
732 ui_out_field_int (mi_uiout, "thread-id", tp->global_num);
1d33d6ba
VP
733 if (non_stop)
734 {
735 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
736 (mi_uiout, "stopped-threads");
102040f0 737
5d5658a1 738 ui_out_field_int (mi_uiout, NULL, tp->global_num);
1d33d6ba
VP
739 do_cleanups (back_to);
740 }
741 else
742 ui_out_field_string (mi_uiout, "stopped-threads", "all");
dc146f7c
VP
743
744 core = target_core_of_thread (inferior_ptid);
745 if (core != -1)
746 ui_out_field_int (mi_uiout, "core", core);
1d33d6ba
VP
747 }
748
f7f9a841 749 fputs_unfiltered ("*stopped", raw_stdout);
1d33d6ba
VP
750 mi_out_put (mi_uiout, raw_stdout);
751 mi_out_rewind (mi_uiout);
4333ada3 752 mi_print_timing_maybe ();
f7f9a841
VP
753 fputs_unfiltered ("\n", raw_stdout);
754 gdb_flush (raw_stdout);
755}
756
73ab01a0
PA
757static void
758mi_on_normal_stop (struct bpstats *bs, int print_frame)
759{
760 struct switch_thru_all_uis state;
761
762 SWITCH_THRU_ALL_UIS (state)
763 {
764 if (as_mi_interp (top_level_interpreter ()) == NULL)
765 continue;
766
767 mi_on_normal_stop_1 (bs, print_frame);
768 }
769}
770
f3b1572e
PA
771static void
772mi_about_to_proceed (void)
773{
774 /* Suppress output while calling an inferior function. */
775
776 if (!ptid_equal (inferior_ptid, null_ptid))
777 {
778 struct thread_info *tp = inferior_thread ();
102040f0 779
16c381f0 780 if (tp->control.in_infcall)
f3b1572e
PA
781 return;
782 }
783
784 mi_proceeded = 1;
785}
786
5b9afe8a
YQ
787/* When the element is non-zero, no MI notifications will be emitted in
788 response to the corresponding observers. */
2b03b41d 789
5b9afe8a
YQ
790struct mi_suppress_notification mi_suppress_notification =
791 {
792 0,
793 0,
201b4506 794 0,
5b9afe8a 795 };
8d3788bd 796
201b4506
YQ
797/* Emit notification on changing a traceframe. */
798
799static void
800mi_traceframe_changed (int tfnum, int tpnum)
801{
73ab01a0 802 struct switch_thru_all_uis state;
201b4506
YQ
803
804 if (mi_suppress_notification.traceframe)
805 return;
806
73ab01a0
PA
807 SWITCH_THRU_ALL_UIS (state)
808 {
809 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
810 struct cleanup *old_chain;
201b4506 811
73ab01a0
PA
812 if (mi == NULL)
813 continue;
201b4506 814
73ab01a0
PA
815 old_chain = make_cleanup_restore_target_terminal ();
816 target_terminal_ours_for_output ();
5fe96654 817
73ab01a0
PA
818 if (tfnum >= 0)
819 fprintf_unfiltered (mi->event_channel, "traceframe-changed,"
820 "num=\"%d\",tracepoint=\"%d\"\n",
821 tfnum, tpnum);
822 else
823 fprintf_unfiltered (mi->event_channel, "traceframe-changed,end");
824
825 gdb_flush (mi->event_channel);
826
827 do_cleanups (old_chain);
828 }
201b4506
YQ
829}
830
bb25a15c
YQ
831/* Emit notification on creating a trace state variable. */
832
833static void
134a2066 834mi_tsv_created (const struct trace_state_variable *tsv)
bb25a15c 835{
73ab01a0 836 struct switch_thru_all_uis state;
bb25a15c 837
73ab01a0
PA
838 SWITCH_THRU_ALL_UIS (state)
839 {
840 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
841 struct cleanup *old_chain;
bb25a15c 842
73ab01a0
PA
843 if (mi == NULL)
844 continue;
bb25a15c 845
73ab01a0
PA
846 old_chain = make_cleanup_restore_target_terminal ();
847 target_terminal_ours_for_output ();
5fe96654 848
73ab01a0
PA
849 fprintf_unfiltered (mi->event_channel, "tsv-created,"
850 "name=\"%s\",initial=\"%s\"\n",
851 tsv->name, plongest (tsv->initial_value));
852
853 gdb_flush (mi->event_channel);
854
855 do_cleanups (old_chain);
856 }
bb25a15c
YQ
857}
858
859/* Emit notification on deleting a trace state variable. */
860
861static void
134a2066 862mi_tsv_deleted (const struct trace_state_variable *tsv)
bb25a15c 863{
73ab01a0 864 struct switch_thru_all_uis state;
bb25a15c 865
73ab01a0
PA
866 SWITCH_THRU_ALL_UIS (state)
867 {
868 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
869 struct cleanup *old_chain;
bb25a15c 870
73ab01a0
PA
871 if (mi == NULL)
872 continue;
bb25a15c 873
73ab01a0
PA
874 old_chain = make_cleanup_restore_target_terminal ();
875 target_terminal_ours_for_output ();
5fe96654 876
73ab01a0
PA
877 if (tsv != NULL)
878 fprintf_unfiltered (mi->event_channel, "tsv-deleted,"
879 "name=\"%s\"\n", tsv->name);
880 else
881 fprintf_unfiltered (mi->event_channel, "tsv-deleted\n");
882
883 gdb_flush (mi->event_channel);
884
885 do_cleanups (old_chain);
886 }
bb25a15c
YQ
887}
888
134a2066
YQ
889/* Emit notification on modifying a trace state variable. */
890
891static void
892mi_tsv_modified (const struct trace_state_variable *tsv)
893{
73ab01a0 894 struct switch_thru_all_uis state;
134a2066 895
73ab01a0
PA
896 SWITCH_THRU_ALL_UIS (state)
897 {
898 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
899 struct ui_out *mi_uiout;
900 struct cleanup *old_chain;
134a2066 901
73ab01a0
PA
902 if (mi == NULL)
903 continue;
134a2066 904
73ab01a0 905 mi_uiout = interp_ui_out (top_level_interpreter ());
134a2066 906
73ab01a0
PA
907 old_chain = make_cleanup_restore_target_terminal ();
908 target_terminal_ours_for_output ();
134a2066 909
73ab01a0
PA
910 fprintf_unfiltered (mi->event_channel,
911 "tsv-modified");
134a2066 912
73ab01a0 913 ui_out_redirect (mi_uiout, mi->event_channel);
5fe96654 914
73ab01a0
PA
915 ui_out_field_string (mi_uiout, "name", tsv->name);
916 ui_out_field_string (mi_uiout, "initial",
917 plongest (tsv->initial_value));
918 if (tsv->value_known)
919 ui_out_field_string (mi_uiout, "current", plongest (tsv->value));
920
921 ui_out_redirect (mi_uiout, NULL);
922
923 gdb_flush (mi->event_channel);
924
925 do_cleanups (old_chain);
926 }
134a2066
YQ
927}
928
8d3788bd 929/* Emit notification about a created breakpoint. */
2b03b41d 930
8d3788bd
VP
931static void
932mi_breakpoint_created (struct breakpoint *b)
933{
73ab01a0 934 struct switch_thru_all_uis state;
8d3788bd 935
5b9afe8a 936 if (mi_suppress_notification.breakpoint)
8d3788bd
VP
937 return;
938
939 if (b->number <= 0)
940 return;
941
73ab01a0 942 SWITCH_THRU_ALL_UIS (state)
492d29ea 943 {
73ab01a0
PA
944 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
945 struct ui_out *mi_uiout;
946 struct cleanup *old_chain;
492d29ea 947
73ab01a0
PA
948 if (mi == NULL)
949 continue;
8d3788bd 950
73ab01a0 951 mi_uiout = interp_ui_out (top_level_interpreter ());
5fe96654 952
73ab01a0
PA
953 old_chain = make_cleanup_restore_target_terminal ();
954 target_terminal_ours_for_output ();
955
956 fprintf_unfiltered (mi->event_channel,
957 "breakpoint-created");
958 /* We want the output from gdb_breakpoint_query to go to
959 mi->event_channel. One approach would be to just call
960 gdb_breakpoint_query, and then use mi_out_put to send the current
961 content of mi_outout into mi->event_channel. However, that will
962 break if anything is output to mi_uiout prior to calling the
963 breakpoint_created notifications. So, we use
964 ui_out_redirect. */
965 ui_out_redirect (mi_uiout, mi->event_channel);
966 TRY
967 {
968 gdb_breakpoint_query (mi_uiout, b->number, NULL);
969 }
970 CATCH (e, RETURN_MASK_ERROR)
971 {
972 }
973 END_CATCH
974
975 ui_out_redirect (mi_uiout, NULL);
976
977 gdb_flush (mi->event_channel);
978
979 do_cleanups (old_chain);
980 }
8d3788bd
VP
981}
982
983/* Emit notification about deleted breakpoint. */
2b03b41d 984
8d3788bd
VP
985static void
986mi_breakpoint_deleted (struct breakpoint *b)
987{
73ab01a0 988 struct switch_thru_all_uis state;
8d3788bd 989
5b9afe8a 990 if (mi_suppress_notification.breakpoint)
8d3788bd
VP
991 return;
992
993 if (b->number <= 0)
994 return;
995
73ab01a0
PA
996 SWITCH_THRU_ALL_UIS (state)
997 {
998 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
999 struct cleanup *old_chain;
8d3788bd 1000
73ab01a0
PA
1001 if (mi == NULL)
1002 continue;
8d3788bd 1003
73ab01a0
PA
1004 old_chain = make_cleanup_restore_target_terminal ();
1005 target_terminal_ours_for_output ();
5fe96654 1006
73ab01a0
PA
1007 fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
1008 b->number);
1009
1010 gdb_flush (mi->event_channel);
1011
1012 do_cleanups (old_chain);
1013 }
8d3788bd
VP
1014}
1015
1016/* Emit notification about modified breakpoint. */
2b03b41d 1017
8d3788bd
VP
1018static void
1019mi_breakpoint_modified (struct breakpoint *b)
1020{
73ab01a0 1021 struct switch_thru_all_uis state;
8d3788bd 1022
5b9afe8a 1023 if (mi_suppress_notification.breakpoint)
8d3788bd
VP
1024 return;
1025
1026 if (b->number <= 0)
1027 return;
1028
73ab01a0 1029 SWITCH_THRU_ALL_UIS (state)
492d29ea 1030 {
73ab01a0
PA
1031 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1032 struct cleanup *old_chain;
492d29ea 1033
73ab01a0
PA
1034 if (mi == NULL)
1035 continue;
8d3788bd 1036
73ab01a0
PA
1037 old_chain = make_cleanup_restore_target_terminal ();
1038 target_terminal_ours_for_output ();
1039 fprintf_unfiltered (mi->event_channel,
1040 "breakpoint-modified");
1041 /* We want the output from gdb_breakpoint_query to go to
1042 mi->event_channel. One approach would be to just call
1043 gdb_breakpoint_query, and then use mi_out_put to send the current
1044 content of mi_outout into mi->event_channel. However, that will
1045 break if anything is output to mi_uiout prior to calling the
1046 breakpoint_created notifications. So, we use
1047 ui_out_redirect. */
1048 ui_out_redirect (mi->mi_uiout, mi->event_channel);
1049 TRY
1050 {
1051 gdb_breakpoint_query (mi->mi_uiout, b->number, NULL);
1052 }
1053 CATCH (e, RETURN_MASK_ERROR)
1054 {
1055 }
1056 END_CATCH
5fe96654 1057
73ab01a0
PA
1058 ui_out_redirect (mi->mi_uiout, NULL);
1059
1060 gdb_flush (mi->event_channel);
1061
1062 do_cleanups (old_chain);
1063 }
8d3788bd
VP
1064}
1065
d90e17a7
PA
1066static int
1067mi_output_running_pid (struct thread_info *info, void *arg)
1068{
19ba03f4 1069 ptid_t *ptid = (ptid_t *) arg;
73ab01a0 1070 struct switch_thru_all_uis state;
d90e17a7 1071
73ab01a0
PA
1072 SWITCH_THRU_ALL_UIS (state)
1073 {
1074 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1075
1076 if (mi == NULL)
1077 continue;
1078
1079 if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
1080 fprintf_unfiltered (raw_stdout,
1081 "*running,thread-id=\"%d\"\n",
1082 info->global_num);
1083 }
d90e17a7
PA
1084
1085 return 0;
1086}
1087
1088static int
1089mi_inferior_count (struct inferior *inf, void *arg)
1090{
1091 if (inf->pid != 0)
1092 {
19ba03f4 1093 int *count_p = (int *) arg;
d90e17a7
PA
1094 (*count_p)++;
1095 }
1096
1097 return 0;
1098}
1099
e1ac3328 1100static void
73ab01a0 1101mi_on_resume_1 (ptid_t ptid)
e1ac3328 1102{
a2840c35
VP
1103 /* To cater for older frontends, emit ^running, but do it only once
1104 per each command. We do it here, since at this point we know
1105 that the target was successfully resumed, and in non-async mode,
1106 we won't return back to MI interpreter code until the target
1107 is done running, so delaying the output of "^running" until then
1108 will make it impossible for frontend to know what's going on.
1109
1110 In future (MI3), we'll be outputting "^done" here. */
f3b1572e 1111 if (!running_result_record_printed && mi_proceeded)
a2840c35 1112 {
c271b6e2
VP
1113 fprintf_unfiltered (raw_stdout, "%s^running\n",
1114 current_token ? current_token : "");
a2840c35
VP
1115 }
1116
dfd4cc63 1117 if (ptid_get_pid (ptid) == -1)
e1ac3328 1118 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
d90e17a7 1119 else if (ptid_is_pid (ptid))
bb599c81 1120 {
ab730e72 1121 int count = 0;
d90e17a7
PA
1122
1123 /* Backwards compatibility. If there's only one inferior,
1124 output "all", otherwise, output each resumed thread
1125 individually. */
1126 iterate_over_inferiors (mi_inferior_count, &count);
1127
1128 if (count == 1)
1129 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
1130 else
1131 iterate_over_threads (mi_output_running_pid, &ptid);
bb599c81 1132 }
e1ac3328
VP
1133 else
1134 {
e09875d4 1135 struct thread_info *ti = find_thread_ptid (ptid);
102040f0 1136
e1ac3328 1137 gdb_assert (ti);
5d5658a1
PA
1138 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n",
1139 ti->global_num);
e1ac3328 1140 }
a2840c35 1141
f3b1572e 1142 if (!running_result_record_printed && mi_proceeded)
a2840c35
VP
1143 {
1144 running_result_record_printed = 1;
1145 /* This is what gdb used to do historically -- printing prompt even if
1146 it cannot actually accept any input. This will be surely removed
329ea579
PA
1147 for MI3, and may be removed even earlier. SYNC_EXECUTION is
1148 checked here because we only need to emit a prompt if a
1149 synchronous command was issued when the target is async. */
28addb40 1150 if (!target_can_async_p () || sync_execution)
a2840c35
VP
1151 fputs_unfiltered ("(gdb) \n", raw_stdout);
1152 }
c1828f25 1153 gdb_flush (raw_stdout);
e1ac3328
VP
1154}
1155
c86cf029 1156static void
73ab01a0 1157mi_on_resume (ptid_t ptid)
c86cf029 1158{
73ab01a0
PA
1159 struct thread_info *tp = NULL;
1160 struct switch_thru_all_uis state;
6ef284bd 1161
73ab01a0
PA
1162 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
1163 tp = inferior_thread ();
1164 else
1165 tp = find_thread_ptid (ptid);
6ef284bd 1166
73ab01a0
PA
1167 /* Suppress output while calling an inferior function. */
1168 if (tp->control.in_infcall)
1169 return;
6ef284bd 1170
73ab01a0 1171 SWITCH_THRU_ALL_UIS (state)
6ef284bd 1172 {
73ab01a0
PA
1173 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1174 struct cleanup *old_chain;
1175
1176 if (mi == NULL)
1177 continue;
1178
1179 old_chain = make_cleanup_restore_target_terminal ();
1180 target_terminal_ours_for_output ();
1181
1182 mi_on_resume_1 (ptid);
1183
1184 do_cleanups (old_chain);
6ef284bd 1185 }
73ab01a0 1186}
6ef284bd 1187
73ab01a0
PA
1188static void
1189mi_solib_loaded (struct so_list *solib)
1190{
1191 struct switch_thru_all_uis state;
a79b8f6e 1192
73ab01a0
PA
1193 SWITCH_THRU_ALL_UIS (state)
1194 {
1195 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1196 struct ui_out *uiout;
1197 struct cleanup *old_chain;
5fe96654 1198
73ab01a0
PA
1199 if (mi == NULL)
1200 continue;
1201
1202 uiout = interp_ui_out (top_level_interpreter ());
1203
1204 old_chain = make_cleanup_restore_target_terminal ();
1205 target_terminal_ours_for_output ();
1206
1207 fprintf_unfiltered (mi->event_channel, "library-loaded");
1208
1209 ui_out_redirect (uiout, mi->event_channel);
1210
1211 ui_out_field_string (uiout, "id", solib->so_original_name);
1212 ui_out_field_string (uiout, "target-name", solib->so_original_name);
1213 ui_out_field_string (uiout, "host-name", solib->so_name);
1214 ui_out_field_int (uiout, "symbols-loaded", solib->symbols_loaded);
1215 if (!gdbarch_has_global_solist (target_gdbarch ()))
1216 {
1217 ui_out_field_fmt (uiout, "thread-group", "i%d",
1218 current_inferior ()->num);
1219 }
1220
1221 ui_out_redirect (uiout, NULL);
1222
1223 gdb_flush (mi->event_channel);
1224
1225 do_cleanups (old_chain);
1226 }
c86cf029
VP
1227}
1228
1229static void
1230mi_solib_unloaded (struct so_list *solib)
1231{
73ab01a0 1232 struct switch_thru_all_uis state;
102040f0 1233
73ab01a0
PA
1234 SWITCH_THRU_ALL_UIS (state)
1235 {
1236 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1237 struct ui_out *uiout;
1238 struct cleanup *old_chain;
6ef284bd 1239
73ab01a0
PA
1240 if (mi == NULL)
1241 continue;
6ef284bd 1242
73ab01a0 1243 uiout = interp_ui_out (top_level_interpreter ());
6ef284bd 1244
73ab01a0
PA
1245 old_chain = make_cleanup_restore_target_terminal ();
1246 target_terminal_ours_for_output ();
6ef284bd 1247
73ab01a0 1248 fprintf_unfiltered (mi->event_channel, "library-unloaded");
a79b8f6e 1249
73ab01a0 1250 ui_out_redirect (uiout, mi->event_channel);
5fe96654 1251
73ab01a0
PA
1252 ui_out_field_string (uiout, "id", solib->so_original_name);
1253 ui_out_field_string (uiout, "target-name", solib->so_original_name);
1254 ui_out_field_string (uiout, "host-name", solib->so_name);
1255 if (!gdbarch_has_global_solist (target_gdbarch ()))
1256 {
1257 ui_out_field_fmt (uiout, "thread-group", "i%d",
1258 current_inferior ()->num);
1259 }
1260
1261 ui_out_redirect (uiout, NULL);
1262
1263 gdb_flush (mi->event_channel);
1264
1265 do_cleanups (old_chain);
1266 }
c86cf029
VP
1267}
1268
5b9afe8a
YQ
1269/* Emit notification about the command parameter change. */
1270
1271static void
1272mi_command_param_changed (const char *param, const char *value)
1273{
73ab01a0 1274 struct switch_thru_all_uis state;
5b9afe8a
YQ
1275
1276 if (mi_suppress_notification.cmd_param_changed)
1277 return;
1278
73ab01a0
PA
1279 SWITCH_THRU_ALL_UIS (state)
1280 {
1281 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1282 struct ui_out *mi_uiout;
1283 struct cleanup *old_chain;
5b9afe8a 1284
73ab01a0
PA
1285 if (mi == NULL)
1286 continue;
5b9afe8a 1287
73ab01a0 1288 mi_uiout = interp_ui_out (top_level_interpreter ());
5b9afe8a 1289
73ab01a0
PA
1290 old_chain = make_cleanup_restore_target_terminal ();
1291 target_terminal_ours_for_output ();
5b9afe8a 1292
73ab01a0 1293 fprintf_unfiltered (mi->event_channel, "cmd-param-changed");
5b9afe8a 1294
73ab01a0 1295 ui_out_redirect (mi_uiout, mi->event_channel);
5fe96654 1296
73ab01a0
PA
1297 ui_out_field_string (mi_uiout, "param", param);
1298 ui_out_field_string (mi_uiout, "value", value);
1299
1300 ui_out_redirect (mi_uiout, NULL);
1301
1302 gdb_flush (mi->event_channel);
1303
1304 do_cleanups (old_chain);
1305 }
5b9afe8a
YQ
1306}
1307
8de0566d
YQ
1308/* Emit notification about the target memory change. */
1309
1310static void
1311mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
1312 ssize_t len, const bfd_byte *myaddr)
1313{
73ab01a0 1314 struct switch_thru_all_uis state;
8de0566d
YQ
1315
1316 if (mi_suppress_notification.memory)
1317 return;
1318
73ab01a0
PA
1319 SWITCH_THRU_ALL_UIS (state)
1320 {
1321 struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1322 struct ui_out *mi_uiout;
1323 struct obj_section *sec;
1324 struct cleanup *old_chain;
8de0566d 1325
73ab01a0
PA
1326 if (mi == NULL)
1327 continue;
8de0566d 1328
73ab01a0 1329 mi_uiout = interp_ui_out (top_level_interpreter ());
8de0566d 1330
73ab01a0
PA
1331 old_chain = make_cleanup_restore_target_terminal ();
1332 target_terminal_ours_for_output ();
8de0566d 1333
73ab01a0 1334 fprintf_unfiltered (mi->event_channel, "memory-changed");
8de0566d 1335
73ab01a0 1336 ui_out_redirect (mi_uiout, mi->event_channel);
8de0566d 1337
73ab01a0
PA
1338 ui_out_field_fmt (mi_uiout, "thread-group", "i%d", inferior->num);
1339 ui_out_field_core_addr (mi_uiout, "addr", target_gdbarch (), memaddr);
1340 ui_out_field_fmt (mi_uiout, "len", "%s", hex_string (len));
8de0566d 1341
73ab01a0
PA
1342 /* Append 'type=code' into notification if MEMADDR falls in the range of
1343 sections contain code. */
1344 sec = find_pc_section (memaddr);
1345 if (sec != NULL && sec->objfile != NULL)
1346 {
1347 flagword flags = bfd_get_section_flags (sec->objfile->obfd,
1348 sec->the_bfd_section);
5fe96654 1349
73ab01a0
PA
1350 if (flags & SEC_CODE)
1351 ui_out_field_string (mi_uiout, "type", "code");
1352 }
1353
1354 ui_out_redirect (mi_uiout, NULL);
1355
1356 gdb_flush (mi->event_channel);
1357
1358 do_cleanups (old_chain);
1359 }
8de0566d
YQ
1360}
1361
a79b8f6e
VP
1362static int
1363report_initial_inferior (struct inferior *inf, void *closure)
1364{
73ab01a0 1365 /* This function is called from mi_interpreter_init, and since
a79b8f6e
VP
1366 mi_inferior_added assumes that inferior is fully initialized
1367 and top_level_interpreter_data is set, we cannot call
1368 it here. */
19ba03f4 1369 struct mi_interp *mi = (struct mi_interp *) closure;
5fe96654
PA
1370 struct cleanup *old_chain;
1371
1372 old_chain = make_cleanup_restore_target_terminal ();
1373 target_terminal_ours_for_output ();
102040f0 1374
a79b8f6e
VP
1375 fprintf_unfiltered (mi->event_channel,
1376 "thread-group-added,id=\"i%d\"",
1377 inf->num);
1378 gdb_flush (mi->event_channel);
5fe96654
PA
1379
1380 do_cleanups (old_chain);
a79b8f6e
VP
1381 return 0;
1382}
c86cf029 1383
4801a9a3
PA
1384static struct ui_out *
1385mi_ui_out (struct interp *interp)
1386{
19ba03f4 1387 struct mi_interp *mi = (struct mi_interp *) interp_data (interp);
4801a9a3 1388
fd664c91 1389 return mi->mi_uiout;
4801a9a3
PA
1390}
1391
37ce89eb
SS
1392/* Save the original value of raw_stdout here when logging, so we can
1393 restore correctly when done. */
1394
1395static struct ui_file *saved_raw_stdout;
1396
1397/* Do MI-specific logging actions; save raw_stdout, and change all
1398 the consoles to use the supplied ui-file(s). */
1399
1400static int
1401mi_set_logging (struct interp *interp, int start_log,
1402 struct ui_file *out, struct ui_file *logfile)
1403{
19ba03f4 1404 struct mi_interp *mi = (struct mi_interp *) interp_data (interp);
37ce89eb
SS
1405
1406 if (!mi)
1407 return 0;
1408
1409 if (start_log)
1410 {
1411 /* The tee created already is based on gdb_stdout, which for MI
1412 is a console and so we end up in an infinite loop of console
1413 writing to ui_file writing to console etc. So discard the
1414 existing tee (it hasn't been used yet, and MI won't ever use
1415 it), and create one based on raw_stdout instead. */
1416 if (logfile)
1417 {
1418 ui_file_delete (out);
1419 out = tee_file_new (raw_stdout, 0, logfile, 0);
1420 }
1421
1422 saved_raw_stdout = raw_stdout;
1423 raw_stdout = out;
1424 }
1425 else
1426 {
1427 raw_stdout = saved_raw_stdout;
1428 saved_raw_stdout = NULL;
1429 }
1430
1431 mi_console_set_raw (mi->out, raw_stdout);
1432 mi_console_set_raw (mi->err, raw_stdout);
1433 mi_console_set_raw (mi->log, raw_stdout);
1434 mi_console_set_raw (mi->targ, raw_stdout);
1435 mi_console_set_raw (mi->event_channel, raw_stdout);
1436
1437 return 1;
1438}
1439
8322445e
PA
1440/* The MI interpreter's vtable. */
1441
1442static const struct interp_procs mi_interp_procs =
1443{
1444 mi_interpreter_init, /* init_proc */
1445 mi_interpreter_resume, /* resume_proc */
1446 mi_interpreter_suspend, /* suspend_proc */
1447 mi_interpreter_exec, /* exec_proc */
1448 mi_ui_out, /* ui_out_proc */
1449 mi_set_logging, /* set_logging_proc */
1450 mi_command_loop /* command_loop_proc */
1451};
1452
1453/* Factory for MI interpreters. */
1454
1455static struct interp *
1456mi_interp_factory (const char *name)
1457{
1458 return interp_new (name, &mi_interp_procs, NULL);
1459}
1460
b9362cc7
AC
1461extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
1462
4a8f6654
AC
1463void
1464_initialize_mi_interp (void)
1465{
2fcf52f0 1466 /* The various interpreter levels. */
8322445e
PA
1467 interp_factory_register (INTERP_MI1, mi_interp_factory);
1468 interp_factory_register (INTERP_MI2, mi_interp_factory);
1469 interp_factory_register (INTERP_MI3, mi_interp_factory);
1470 interp_factory_register (INTERP_MI, mi_interp_factory);
73ab01a0
PA
1471
1472 observer_attach_signal_received (mi_on_signal_received);
1473 observer_attach_end_stepping_range (mi_on_end_stepping_range);
1474 observer_attach_signal_exited (mi_on_signal_exited);
1475 observer_attach_exited (mi_on_exited);
1476 observer_attach_no_history (mi_on_no_history);
1477 observer_attach_new_thread (mi_new_thread);
1478 observer_attach_thread_exit (mi_thread_exit);
1479 observer_attach_inferior_added (mi_inferior_added);
1480 observer_attach_inferior_appeared (mi_inferior_appeared);
1481 observer_attach_inferior_exit (mi_inferior_exit);
1482 observer_attach_inferior_removed (mi_inferior_removed);
1483 observer_attach_record_changed (mi_record_changed);
1484 observer_attach_normal_stop (mi_on_normal_stop);
1485 observer_attach_target_resumed (mi_on_resume);
1486 observer_attach_solib_loaded (mi_solib_loaded);
1487 observer_attach_solib_unloaded (mi_solib_unloaded);
1488 observer_attach_about_to_proceed (mi_about_to_proceed);
1489 observer_attach_traceframe_changed (mi_traceframe_changed);
1490 observer_attach_tsv_created (mi_tsv_created);
1491 observer_attach_tsv_deleted (mi_tsv_deleted);
1492 observer_attach_tsv_modified (mi_tsv_modified);
1493 observer_attach_breakpoint_created (mi_breakpoint_created);
1494 observer_attach_breakpoint_deleted (mi_breakpoint_deleted);
1495 observer_attach_breakpoint_modified (mi_breakpoint_modified);
1496 observer_attach_command_param_changed (mi_command_param_changed);
1497 observer_attach_memory_changed (mi_memory_changed);
1498 observer_attach_sync_execution_done (mi_on_sync_execution_done);
4a8f6654 1499}
This page took 1.246836 seconds and 4 git commands to generate.