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