gdb/
[external/binutils.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3    Copyright (C) 2002-2005, 2007-2012 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 "gdb_string.h"
22 #include "interps.h"
23 #include "event-top.h"
24 #include "event-loop.h"
25 #include "inferior.h"
26 #include "ui-out.h"
27 #include "top.h"
28 #include "exceptions.h"
29 #include "mi-main.h"
30 #include "mi-cmds.h"
31 #include "mi-out.h"
32 #include "mi-console.h"
33 #include "mi-common.h"
34 #include "observer.h"
35 #include "gdbthread.h"
36 #include "solist.h"
37 #include "gdb.h"
38
39 /* These are the interpreter setup, etc. functions for the MI
40    interpreter.  */
41
42 static void mi_execute_command_wrapper (char *cmd);
43 static void mi_execute_command_input_handler (char *cmd);
44 static void mi_command_loop (int mi_version);
45
46 /* These are hooks that we put in place while doing interpreter_exec
47    so we can report interesting things that happened "behind the MI's
48    back" in this command.  */
49
50 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
51   ATTRIBUTE_PRINTF (1, 0);
52
53 static void mi3_command_loop (void);
54 static void mi2_command_loop (void);
55 static void mi1_command_loop (void);
56
57 static void mi_insert_notify_hooks (void);
58 static void mi_remove_notify_hooks (void);
59 static void mi_on_normal_stop (struct bpstats *bs, int print_frame);
60
61 static void mi_new_thread (struct thread_info *t);
62 static void mi_thread_exit (struct thread_info *t, int silent);
63 static void mi_inferior_added (struct inferior *inf);
64 static void mi_inferior_appeared (struct inferior *inf);
65 static void mi_inferior_exit (struct inferior *inf);
66 static void mi_inferior_removed (struct inferior *inf);
67 static void mi_on_resume (ptid_t ptid);
68 static void mi_solib_loaded (struct so_list *solib);
69 static void mi_solib_unloaded (struct so_list *solib);
70 static void mi_about_to_proceed (void);
71 static void mi_breakpoint_created (struct breakpoint *b);
72 static void mi_breakpoint_deleted (struct breakpoint *b);
73 static void mi_breakpoint_modified (struct breakpoint *b);
74 static void mi_command_param_changed (const char *param, const char *value);
75
76 static int report_initial_inferior (struct inferior *inf, void *closure);
77
78 static void *
79 mi_interpreter_init (struct interp *interp, int top_level)
80 {
81   struct mi_interp *mi = XMALLOC (struct mi_interp);
82   const char *name;
83   int mi_version;
84
85   /* Assign the output channel created at startup to its own global,
86      so that we can create a console channel that encapsulates and
87      prefixes all gdb_output-type bits coming from the rest of the
88      debugger.  */
89
90   raw_stdout = gdb_stdout;
91
92   /* Create MI console channels, each with a different prefix so they
93      can be distinguished.  */
94   mi->out = mi_console_file_new (raw_stdout, "~", '"');
95   mi->err = mi_console_file_new (raw_stdout, "&", '"');
96   mi->log = mi->err;
97   mi->targ = mi_console_file_new (raw_stdout, "@", '"');
98   mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
99
100   name = interp_name (interp);
101   /* INTERP_MI selects the most recent released version.  "mi2" was
102      released as part of GDB 6.0.  */
103   if (strcmp (name, INTERP_MI) == 0)
104     mi_version = 2;
105   else if (strcmp (name, INTERP_MI1) == 0)
106     mi_version = 1;
107   else if (strcmp (name, INTERP_MI2) == 0)
108     mi_version = 2;
109   else if (strcmp (name, INTERP_MI3) == 0)
110     mi_version = 3;
111   else
112     gdb_assert_not_reached ("unhandled MI version");
113
114   mi->uiout = mi_out_new (mi_version);
115
116   if (top_level)
117     {
118       observer_attach_new_thread (mi_new_thread);
119       observer_attach_thread_exit (mi_thread_exit);
120       observer_attach_inferior_added (mi_inferior_added);
121       observer_attach_inferior_appeared (mi_inferior_appeared);
122       observer_attach_inferior_exit (mi_inferior_exit);
123       observer_attach_inferior_removed (mi_inferior_removed);
124       observer_attach_normal_stop (mi_on_normal_stop);
125       observer_attach_target_resumed (mi_on_resume);
126       observer_attach_solib_loaded (mi_solib_loaded);
127       observer_attach_solib_unloaded (mi_solib_unloaded);
128       observer_attach_about_to_proceed (mi_about_to_proceed);
129       observer_attach_breakpoint_created (mi_breakpoint_created);
130       observer_attach_breakpoint_deleted (mi_breakpoint_deleted);
131       observer_attach_breakpoint_modified (mi_breakpoint_modified);
132       observer_attach_command_param_changed (mi_command_param_changed);
133
134       /* The initial inferior is created before this function is
135          called, so we need to report it explicitly.  Use iteration in
136          case future version of GDB creates more than one inferior
137          up-front.  */
138       iterate_over_inferiors (report_initial_inferior, mi);
139     }
140
141   return mi;
142 }
143
144 static int
145 mi_interpreter_resume (void *data)
146 {
147   struct mi_interp *mi = data;
148
149   /* As per hack note in mi_interpreter_init, swap in the output
150      channels... */
151   gdb_setup_readline ();
152
153   /* These overwrite some of the initialization done in
154      _intialize_event_loop.  */
155   call_readline = gdb_readline2;
156   input_handler = mi_execute_command_input_handler;
157   add_file_handler (input_fd, stdin_event_handler, 0);
158   async_command_editing_p = 0;
159   /* FIXME: This is a total hack for now.  PB's use of the MI
160      implicitly relies on a bug in the async support which allows
161      asynchronous commands to leak through the commmand loop.  The bug
162      involves (but is not limited to) the fact that sync_execution was
163      erroneously initialized to 0.  Duplicate by initializing it thus
164      here...  */
165   sync_execution = 0;
166
167   gdb_stdout = mi->out;
168   /* Route error and log output through the MI.  */
169   gdb_stderr = mi->err;
170   gdb_stdlog = mi->log;
171   /* Route target output through the MI.  */
172   gdb_stdtarg = mi->targ;
173   /* Route target error through the MI as well.  */
174   gdb_stdtargerr = mi->targ;
175
176   /* Replace all the hooks that we know about.  There really needs to
177      be a better way of doing this... */
178   clear_interpreter_hooks ();
179
180   deprecated_show_load_progress = mi_load_progress;
181
182   /* If we're _the_ interpreter, take control.  */
183   if (current_interp_named_p (INTERP_MI1))
184     deprecated_command_loop_hook = mi1_command_loop;
185   else if (current_interp_named_p (INTERP_MI2))
186     deprecated_command_loop_hook = mi2_command_loop;
187   else if (current_interp_named_p (INTERP_MI3))
188     deprecated_command_loop_hook = mi3_command_loop;
189   else
190     deprecated_command_loop_hook = mi2_command_loop;
191
192   return 1;
193 }
194
195 static int
196 mi_interpreter_suspend (void *data)
197 {
198   gdb_disable_readline ();
199   return 1;
200 }
201
202 static struct gdb_exception
203 mi_interpreter_exec (void *data, const char *command)
204 {
205   char *tmp = alloca (strlen (command) + 1);
206
207   strcpy (tmp, command);
208   mi_execute_command_wrapper (tmp);
209   return exception_none;
210 }
211
212 /* Never display the default GDB prompt in MI case.  */
213
214 static int
215 mi_interpreter_prompt_p (void *data)
216 {
217   return 0;
218 }
219
220 void
221 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
222 {
223   struct interp *interp_to_use;
224   int i;
225   char *mi_error_message = NULL;
226   struct cleanup *old_chain;
227
228   if (argc < 2)
229     error (_("-interpreter-exec: "
230              "Usage: -interpreter-exec interp command"));
231
232   interp_to_use = interp_lookup (argv[0]);
233   if (interp_to_use == NULL)
234     error (_("-interpreter-exec: could not find interpreter \"%s\""),
235            argv[0]);
236
237   if (!interp_exec_p (interp_to_use))
238     error (_("-interpreter-exec: interpreter \"%s\" "
239              "does not support command execution"),
240               argv[0]);
241
242   /* Insert the MI out hooks, making sure to also call the
243      interpreter's hooks if it has any.  */
244   /* KRS: We shouldn't need this... Events should be installed and
245      they should just ALWAYS fire something out down the MI
246      channel.  */
247   mi_insert_notify_hooks ();
248
249   /* Now run the code.  */
250
251   old_chain = make_cleanup (null_cleanup, 0);
252   for (i = 1; i < argc; i++)
253     {
254       struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
255
256       if (e.reason < 0)
257         {
258           mi_error_message = xstrdup (e.message);
259           make_cleanup (xfree, mi_error_message);
260           break;
261         }
262     }
263
264   mi_remove_notify_hooks ();
265
266   if (mi_error_message != NULL)
267     error ("%s", mi_error_message);
268   do_cleanups (old_chain);
269 }
270
271 /* This inserts a number of hooks that are meant to produce
272    async-notify ("=") MI messages while running commands in another
273    interpreter using mi_interpreter_exec.  The canonical use for this
274    is to allow access to the gdb CLI interpreter from within the MI,
275    while still producing MI style output when actions in the CLI
276    command change GDB's state.  */
277
278 static void
279 mi_insert_notify_hooks (void)
280 {
281   deprecated_query_hook = mi_interp_query_hook;
282 }
283
284 static void
285 mi_remove_notify_hooks (void)
286 {
287   deprecated_query_hook = NULL;
288 }
289
290 static int
291 mi_interp_query_hook (const char *ctlstr, va_list ap)
292 {
293   return 1;
294 }
295
296 static void
297 mi_execute_command_wrapper (char *cmd)
298 {
299   mi_execute_command (cmd, stdin == instream);
300 }
301
302 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER.  */
303
304 static void
305 mi_execute_command_input_handler (char *cmd)
306 {
307   mi_execute_command_wrapper (cmd);
308
309   fputs_unfiltered ("(gdb) \n", raw_stdout);
310   gdb_flush (raw_stdout);
311 }
312
313 static void
314 mi1_command_loop (void)
315 {
316   mi_command_loop (1);
317 }
318
319 static void
320 mi2_command_loop (void)
321 {
322   mi_command_loop (2);
323 }
324
325 static void
326 mi3_command_loop (void)
327 {
328   mi_command_loop (3);
329 }
330
331 static void
332 mi_command_loop (int mi_version)
333 {
334   /* Turn off 8 bit strings in quoted output.  Any character with the
335      high bit set is printed using C's octal format.  */
336   sevenbit_strings = 1;
337
338   /* Tell the world that we're alive.  */
339   fputs_unfiltered ("(gdb) \n", raw_stdout);
340   gdb_flush (raw_stdout);
341
342   start_event_loop ();
343 }
344
345 static void
346 mi_new_thread (struct thread_info *t)
347 {
348   struct mi_interp *mi = top_level_interpreter_data ();
349   struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid));
350
351   gdb_assert (inf);
352
353   fprintf_unfiltered (mi->event_channel, 
354                       "thread-created,id=\"%d\",group-id=\"i%d\"",
355                       t->num, inf->num);
356   gdb_flush (mi->event_channel);
357 }
358
359 static void
360 mi_thread_exit (struct thread_info *t, int silent)
361 {
362   struct mi_interp *mi;
363   struct inferior *inf;
364
365   if (silent)
366     return;
367
368   inf = find_inferior_pid (ptid_get_pid (t->ptid));
369
370   mi = top_level_interpreter_data ();
371   target_terminal_ours ();
372   fprintf_unfiltered (mi->event_channel, 
373                       "thread-exited,id=\"%d\",group-id=\"i%d\"",
374                       t->num, inf->num);
375   gdb_flush (mi->event_channel);
376 }
377
378 static void
379 mi_inferior_added (struct inferior *inf)
380 {
381   struct mi_interp *mi = top_level_interpreter_data ();
382
383   target_terminal_ours ();
384   fprintf_unfiltered (mi->event_channel,
385                       "thread-group-added,id=\"i%d\"",
386                       inf->num);
387   gdb_flush (mi->event_channel);
388 }
389
390 static void
391 mi_inferior_appeared (struct inferior *inf)
392 {
393   struct mi_interp *mi = top_level_interpreter_data ();
394
395   target_terminal_ours ();
396   fprintf_unfiltered (mi->event_channel,
397                       "thread-group-started,id=\"i%d\",pid=\"%d\"",
398                       inf->num, inf->pid);
399   gdb_flush (mi->event_channel);
400 }
401
402 static void
403 mi_inferior_exit (struct inferior *inf)
404 {
405   struct mi_interp *mi = top_level_interpreter_data ();
406
407   target_terminal_ours ();
408   if (inf->has_exit_code)
409     fprintf_unfiltered (mi->event_channel,
410                         "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
411                         inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
412   else
413     fprintf_unfiltered (mi->event_channel,
414                         "thread-group-exited,id=\"i%d\"", inf->num);
415
416   gdb_flush (mi->event_channel);  
417 }
418
419 static void
420 mi_inferior_removed (struct inferior *inf)
421 {
422   struct mi_interp *mi = top_level_interpreter_data ();
423
424   target_terminal_ours ();
425   fprintf_unfiltered (mi->event_channel,
426                       "thread-group-removed,id=\"i%d\"",
427                       inf->num);
428   gdb_flush (mi->event_channel);
429 }
430
431 static void
432 mi_on_normal_stop (struct bpstats *bs, int print_frame)
433 {
434   /* Since this can be called when CLI command is executing,
435      using cli interpreter, be sure to use MI uiout for output,
436      not the current one.  */
437   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
438
439   if (print_frame)
440     {
441       int core;
442
443       if (current_uiout != mi_uiout)
444         {
445           /* The normal_stop function has printed frame information
446              into CLI uiout, or some other non-MI uiout.  There's no
447              way we can extract proper fields from random uiout
448              object, so we print the frame again.  In practice, this
449              can only happen when running a CLI command in MI.  */
450           struct ui_out *saved_uiout = current_uiout;
451           struct target_waitstatus last;
452           ptid_t last_ptid;
453
454           current_uiout = mi_uiout;
455
456           get_last_target_status (&last_ptid, &last);
457           bpstat_print (bs, last.kind);
458
459           print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
460           current_uiout = saved_uiout;
461         }
462
463       ui_out_field_int (mi_uiout, "thread-id",
464                         pid_to_thread_id (inferior_ptid));
465       if (non_stop)
466         {
467           struct cleanup *back_to = make_cleanup_ui_out_list_begin_end 
468             (mi_uiout, "stopped-threads");
469
470           ui_out_field_int (mi_uiout, NULL,
471                             pid_to_thread_id (inferior_ptid));
472           do_cleanups (back_to);
473         }
474       else
475         ui_out_field_string (mi_uiout, "stopped-threads", "all");
476
477       core = target_core_of_thread (inferior_ptid);
478       if (core != -1)
479         ui_out_field_int (mi_uiout, "core", core);
480     }
481   
482   fputs_unfiltered ("*stopped", raw_stdout);
483   mi_out_put (mi_uiout, raw_stdout);
484   mi_out_rewind (mi_uiout);
485   mi_print_timing_maybe ();
486   fputs_unfiltered ("\n", raw_stdout);
487   gdb_flush (raw_stdout);
488 }
489
490 static void
491 mi_about_to_proceed (void)
492 {
493   /* Suppress output while calling an inferior function.  */
494
495   if (!ptid_equal (inferior_ptid, null_ptid))
496     {
497       struct thread_info *tp = inferior_thread ();
498
499       if (tp->control.in_infcall)
500         return;
501     }
502
503   mi_proceeded = 1;
504 }
505
506 /* When the element is non-zero, no MI notifications will be emitted in
507    response to the corresponding observers.  */
508
509 struct mi_suppress_notification mi_suppress_notification =
510   {
511     0,
512     0,
513   };
514
515 /* Emit notification about a created breakpoint.  */
516
517 static void
518 mi_breakpoint_created (struct breakpoint *b)
519 {
520   struct mi_interp *mi = top_level_interpreter_data ();
521   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
522   volatile struct gdb_exception e;
523
524   if (mi_suppress_notification.breakpoint)
525     return;
526
527   if (b->number <= 0)
528     return;
529
530   target_terminal_ours ();
531   fprintf_unfiltered (mi->event_channel,
532                       "breakpoint-created");
533   /* We want the output from gdb_breakpoint_query to go to
534      mi->event_channel.  One approach would be to just call
535      gdb_breakpoint_query, and then use mi_out_put to send the current
536      content of mi_outout into mi->event_channel.  However, that will
537      break if anything is output to mi_uiout prior to calling the
538      breakpoint_created notifications.  So, we use
539      ui_out_redirect.  */
540   ui_out_redirect (mi_uiout, mi->event_channel);
541   TRY_CATCH (e, RETURN_MASK_ERROR)
542     gdb_breakpoint_query (mi_uiout, b->number, NULL);
543   ui_out_redirect (mi_uiout, NULL);
544
545   gdb_flush (mi->event_channel);
546 }
547
548 /* Emit notification about deleted breakpoint.  */
549
550 static void
551 mi_breakpoint_deleted (struct breakpoint *b)
552 {
553   struct mi_interp *mi = top_level_interpreter_data ();
554
555   if (mi_suppress_notification.breakpoint)
556     return;
557
558   if (b->number <= 0)
559     return;
560
561   target_terminal_ours ();
562
563   fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
564                       b->number);
565
566   gdb_flush (mi->event_channel);
567 }
568
569 /* Emit notification about modified breakpoint.  */
570
571 static void
572 mi_breakpoint_modified (struct breakpoint *b)
573 {
574   struct mi_interp *mi = top_level_interpreter_data ();
575   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
576   volatile struct gdb_exception e;
577
578   if (mi_suppress_notification.breakpoint)
579     return;
580
581   if (b->number <= 0)
582     return;
583
584   target_terminal_ours ();
585   fprintf_unfiltered (mi->event_channel,
586                       "breakpoint-modified");
587   /* We want the output from gdb_breakpoint_query to go to
588      mi->event_channel.  One approach would be to just call
589      gdb_breakpoint_query, and then use mi_out_put to send the current
590      content of mi_outout into mi->event_channel.  However, that will
591      break if anything is output to mi_uiout prior to calling the
592      breakpoint_created notifications.  So, we use
593      ui_out_redirect.  */
594   ui_out_redirect (mi_uiout, mi->event_channel);
595   TRY_CATCH (e, RETURN_MASK_ERROR)
596     gdb_breakpoint_query (mi_uiout, b->number, NULL);
597   ui_out_redirect (mi_uiout, NULL);
598
599   gdb_flush (mi->event_channel);
600 }
601
602 static int
603 mi_output_running_pid (struct thread_info *info, void *arg)
604 {
605   ptid_t *ptid = arg;
606
607   if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
608     fprintf_unfiltered (raw_stdout,
609                         "*running,thread-id=\"%d\"\n",
610                         info->num);
611
612   return 0;
613 }
614
615 static int
616 mi_inferior_count (struct inferior *inf, void *arg)
617 {
618   if (inf->pid != 0)
619     {
620       int *count_p = arg;
621       (*count_p)++;
622     }
623
624   return 0;
625 }
626
627 static void
628 mi_on_resume (ptid_t ptid)
629 {
630   struct thread_info *tp = NULL;
631
632   if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
633     tp = inferior_thread ();
634   else
635     tp = find_thread_ptid (ptid);
636
637   /* Suppress output while calling an inferior function.  */
638   if (tp->control.in_infcall)
639     return;
640
641   /* To cater for older frontends, emit ^running, but do it only once
642      per each command.  We do it here, since at this point we know
643      that the target was successfully resumed, and in non-async mode,
644      we won't return back to MI interpreter code until the target
645      is done running, so delaying the output of "^running" until then
646      will make it impossible for frontend to know what's going on.
647
648      In future (MI3), we'll be outputting "^done" here.  */
649   if (!running_result_record_printed && mi_proceeded)
650     {
651       fprintf_unfiltered (raw_stdout, "%s^running\n",
652                           current_token ? current_token : "");
653     }
654
655   if (PIDGET (ptid) == -1)
656     fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
657   else if (ptid_is_pid (ptid))
658     {
659       int count = 0;
660
661       /* Backwards compatibility.  If there's only one inferior,
662          output "all", otherwise, output each resumed thread
663          individually.  */
664       iterate_over_inferiors (mi_inferior_count, &count);
665
666       if (count == 1)
667         fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
668       else
669         iterate_over_threads (mi_output_running_pid, &ptid);
670     }
671   else
672     {
673       struct thread_info *ti = find_thread_ptid (ptid);
674
675       gdb_assert (ti);
676       fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
677     }
678
679   if (!running_result_record_printed && mi_proceeded)
680     {
681       running_result_record_printed = 1;
682       /* This is what gdb used to do historically -- printing prompt even if
683          it cannot actually accept any input.  This will be surely removed
684          for MI3, and may be removed even earler.  */
685       /* FIXME: review the use of target_is_async_p here -- is that
686          what we want? */
687       if (!target_is_async_p ())
688         fputs_unfiltered ("(gdb) \n", raw_stdout);
689     }
690   gdb_flush (raw_stdout);
691 }
692
693 static void
694 mi_solib_loaded (struct so_list *solib)
695 {
696   struct mi_interp *mi = top_level_interpreter_data ();
697
698   target_terminal_ours ();
699   if (gdbarch_has_global_solist (target_gdbarch))
700     fprintf_unfiltered (mi->event_channel,
701                         "library-loaded,id=\"%s\",target-name=\"%s\","
702                         "host-name=\"%s\",symbols-loaded=\"%d\"",
703                         solib->so_original_name, solib->so_original_name,
704                         solib->so_name, solib->symbols_loaded);
705   else
706     fprintf_unfiltered (mi->event_channel,
707                         "library-loaded,id=\"%s\",target-name=\"%s\","
708                         "host-name=\"%s\",symbols-loaded=\"%d\","
709                         "thread-group=\"i%d\"",
710                         solib->so_original_name, solib->so_original_name,
711                         solib->so_name, solib->symbols_loaded,
712                         current_inferior ()->num);
713
714   gdb_flush (mi->event_channel);
715 }
716
717 static void
718 mi_solib_unloaded (struct so_list *solib)
719 {
720   struct mi_interp *mi = top_level_interpreter_data ();
721
722   target_terminal_ours ();
723   if (gdbarch_has_global_solist (target_gdbarch))
724     fprintf_unfiltered (mi->event_channel,
725                         "library-unloaded,id=\"%s\",target-name=\"%s\","
726                         "host-name=\"%s\"",
727                         solib->so_original_name, solib->so_original_name,
728                         solib->so_name);
729   else
730     fprintf_unfiltered (mi->event_channel,
731                         "library-unloaded,id=\"%s\",target-name=\"%s\","
732                         "host-name=\"%s\",thread-group=\"i%d\"",
733                         solib->so_original_name, solib->so_original_name,
734                         solib->so_name, current_inferior ()->num);
735
736   gdb_flush (mi->event_channel);
737 }
738
739 /* Emit notification about the command parameter change.  */
740
741 static void
742 mi_command_param_changed (const char *param, const char *value)
743 {
744   struct mi_interp *mi = top_level_interpreter_data ();
745   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
746
747   if (mi_suppress_notification.cmd_param_changed)
748     return;
749
750   target_terminal_ours ();
751
752   fprintf_unfiltered (mi->event_channel,
753                       "cmd-param-changed");
754
755   ui_out_redirect (mi_uiout, mi->event_channel);
756
757   ui_out_field_string (mi_uiout, "param", param);
758   ui_out_field_string (mi_uiout, "value", value);
759
760   ui_out_redirect (mi_uiout, NULL);
761
762   gdb_flush (mi->event_channel);
763 }
764
765 static int
766 report_initial_inferior (struct inferior *inf, void *closure)
767 {
768   /* This function is called from mi_intepreter_init, and since
769      mi_inferior_added assumes that inferior is fully initialized
770      and top_level_interpreter_data is set, we cannot call
771      it here.  */
772   struct mi_interp *mi = closure;
773
774   target_terminal_ours ();
775   fprintf_unfiltered (mi->event_channel,
776                       "thread-group-added,id=\"i%d\"",
777                       inf->num);
778   gdb_flush (mi->event_channel);
779   return 0;
780 }
781
782 static struct ui_out *
783 mi_ui_out (struct interp *interp)
784 {
785   struct mi_interp *mi = interp_data (interp);
786
787   return mi->uiout;
788 }
789
790 /* Save the original value of raw_stdout here when logging, so we can
791    restore correctly when done.  */
792
793 static struct ui_file *saved_raw_stdout;
794
795 /* Do MI-specific logging actions; save raw_stdout, and change all
796    the consoles to use the supplied ui-file(s).  */
797
798 static int
799 mi_set_logging (struct interp *interp, int start_log,
800                 struct ui_file *out, struct ui_file *logfile)
801 {
802   struct mi_interp *mi = interp_data (interp);
803
804   if (!mi)
805     return 0;
806
807   if (start_log)
808     {
809       /* The tee created already is based on gdb_stdout, which for MI
810          is a console and so we end up in an infinite loop of console
811          writing to ui_file writing to console etc.  So discard the
812          existing tee (it hasn't been used yet, and MI won't ever use
813          it), and create one based on raw_stdout instead.  */
814       if (logfile)
815         {
816           ui_file_delete (out);
817           out = tee_file_new (raw_stdout, 0, logfile, 0);
818         }
819
820       saved_raw_stdout = raw_stdout;
821       raw_stdout = out;
822     }
823   else
824     {
825       raw_stdout = saved_raw_stdout;
826       saved_raw_stdout = NULL;
827     }
828   
829   mi_console_set_raw (mi->out, raw_stdout);
830   mi_console_set_raw (mi->err, raw_stdout);
831   mi_console_set_raw (mi->log, raw_stdout);
832   mi_console_set_raw (mi->targ, raw_stdout);
833   mi_console_set_raw (mi->event_channel, raw_stdout);
834
835   return 1;
836 }
837
838 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
839
840 void
841 _initialize_mi_interp (void)
842 {
843   static const struct interp_procs procs =
844     {
845       mi_interpreter_init,      /* init_proc */
846       mi_interpreter_resume,    /* resume_proc */
847       mi_interpreter_suspend,   /* suspend_proc */
848       mi_interpreter_exec,      /* exec_proc */
849       mi_interpreter_prompt_p,  /* prompt_proc_p */
850       mi_ui_out,                /* ui_out_proc */
851       mi_set_logging            /* set_logging_proc */
852     };
853
854   /* The various interpreter levels.  */
855   interp_add (interp_new (INTERP_MI1, &procs));
856   interp_add (interp_new (INTERP_MI2, &procs));
857   interp_add (interp_new (INTERP_MI3, &procs));
858   interp_add (interp_new (INTERP_MI, &procs));
859 }