xref: /dragonfly/contrib/gdb-7/gdb/mi/mi-interp.c (revision 2020c8fe)
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2 
3    Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "interps.h"
24 #include "event-top.h"
25 #include "event-loop.h"
26 #include "inferior.h"
27 #include "ui-out.h"
28 #include "top.h"
29 #include "exceptions.h"
30 #include "mi-main.h"
31 #include "mi-cmds.h"
32 #include "mi-out.h"
33 #include "mi-console.h"
34 #include "mi-common.h"
35 #include "observer.h"
36 #include "gdbthread.h"
37 #include "solist.h"
38 
39 /* These are the interpreter setup, etc. functions for the MI interpreter */
40 static void mi_execute_command_wrapper (char *cmd);
41 static void mi_command_loop (int mi_version);
42 
43 /* These are hooks that we put in place while doing interpreter_exec
44    so we can report interesting things that happened "behind the mi's
45    back" in this command */
46 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
47      ATTRIBUTE_PRINTF (1, 0);
48 
49 static void mi3_command_loop (void);
50 static void mi2_command_loop (void);
51 static void mi1_command_loop (void);
52 
53 static void mi_insert_notify_hooks (void);
54 static void mi_remove_notify_hooks (void);
55 static void mi_on_normal_stop (struct bpstats *bs, int print_frame);
56 
57 static void mi_new_thread (struct thread_info *t);
58 static void mi_thread_exit (struct thread_info *t, int silent);
59 static void mi_inferior_added (struct inferior *inf);
60 static void mi_inferior_appeared (struct inferior *inf);
61 static void mi_inferior_exit (struct inferior *inf);
62 static void mi_inferior_removed (struct inferior *inf);
63 static void mi_on_resume (ptid_t ptid);
64 static void mi_solib_loaded (struct so_list *solib);
65 static void mi_solib_unloaded (struct so_list *solib);
66 static void mi_about_to_proceed (void);
67 
68 static int report_initial_inferior (struct inferior *inf, void *closure);
69 
70 static void *
71 mi_interpreter_init (int top_level)
72 {
73   struct mi_interp *mi = XMALLOC (struct mi_interp);
74 
75   /* HACK: We need to force stdout/stderr to point at the console.  This avoids
76      any potential side effects caused by legacy code that is still
77      using the TUI / fputs_unfiltered_hook.  So we set up output channels for
78      this now, and swap them in when we are run. */
79 
80   raw_stdout = stdio_fileopen (stdout);
81 
82   /* Create MI channels */
83   mi->out = mi_console_file_new (raw_stdout, "~", '"');
84   mi->err = mi_console_file_new (raw_stdout, "&", '"');
85   mi->log = mi->err;
86   mi->targ = mi_console_file_new (raw_stdout, "@", '"');
87   mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
88 
89   if (top_level)
90     {
91       observer_attach_new_thread (mi_new_thread);
92       observer_attach_thread_exit (mi_thread_exit);
93       observer_attach_inferior_added (mi_inferior_added);
94       observer_attach_inferior_appeared (mi_inferior_appeared);
95       observer_attach_inferior_exit (mi_inferior_exit);
96       observer_attach_inferior_removed (mi_inferior_removed);
97       observer_attach_normal_stop (mi_on_normal_stop);
98       observer_attach_target_resumed (mi_on_resume);
99       observer_attach_solib_loaded (mi_solib_loaded);
100       observer_attach_solib_unloaded (mi_solib_unloaded);
101       observer_attach_about_to_proceed (mi_about_to_proceed);
102 
103       /* The initial inferior is created before this function is called, so we
104 	 need to report it explicitly.  Use iteration in case future version
105 	 of GDB creates more than one inferior up-front.  */
106       iterate_over_inferiors (report_initial_inferior, mi);
107     }
108 
109   return mi;
110 }
111 
112 static int
113 mi_interpreter_resume (void *data)
114 {
115   struct mi_interp *mi = data;
116 
117   /* As per hack note in mi_interpreter_init, swap in the output channels... */
118   gdb_setup_readline ();
119 
120   /* These overwrite some of the initialization done in
121      _intialize_event_loop.  */
122   call_readline = gdb_readline2;
123   input_handler = mi_execute_command_wrapper;
124   add_file_handler (input_fd, stdin_event_handler, 0);
125   async_command_editing_p = 0;
126   /* FIXME: This is a total hack for now.  PB's use of the MI
127      implicitly relies on a bug in the async support which allows
128      asynchronous commands to leak through the commmand loop.  The bug
129      involves (but is not limited to) the fact that sync_execution was
130      erroneously initialized to 0.  Duplicate by initializing it thus
131      here...  */
132   sync_execution = 0;
133 
134   gdb_stdout = mi->out;
135   /* Route error and log output through the MI */
136   gdb_stderr = mi->err;
137   gdb_stdlog = mi->log;
138   /* Route target output through the MI. */
139   gdb_stdtarg = mi->targ;
140   /* Route target error through the MI as well. */
141   gdb_stdtargerr = mi->targ;
142 
143   /* Replace all the hooks that we know about.  There really needs to
144      be a better way of doing this... */
145   clear_interpreter_hooks ();
146 
147   deprecated_show_load_progress = mi_load_progress;
148 
149   /* If we're _the_ interpreter, take control. */
150   if (current_interp_named_p (INTERP_MI1))
151     deprecated_command_loop_hook = mi1_command_loop;
152   else if (current_interp_named_p (INTERP_MI2))
153     deprecated_command_loop_hook = mi2_command_loop;
154   else if (current_interp_named_p (INTERP_MI3))
155     deprecated_command_loop_hook = mi3_command_loop;
156   else
157     deprecated_command_loop_hook = mi2_command_loop;
158 
159   return 1;
160 }
161 
162 static int
163 mi_interpreter_suspend (void *data)
164 {
165   gdb_disable_readline ();
166   return 1;
167 }
168 
169 static struct gdb_exception
170 mi_interpreter_exec (void *data, const char *command)
171 {
172   char *tmp = alloca (strlen (command) + 1);
173 
174   strcpy (tmp, command);
175   mi_execute_command_wrapper (tmp);
176   return exception_none;
177 }
178 
179 /* Never display the default gdb prompt in mi case.  */
180 static int
181 mi_interpreter_prompt_p (void *data)
182 {
183   return 0;
184 }
185 
186 void
187 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
188 {
189   struct interp *interp_to_use;
190   int i;
191   char *mi_error_message = NULL;
192   struct cleanup *old_chain;
193 
194   if (argc < 2)
195     error (_("-interpreter-exec: "
196 	     "Usage: -interpreter-exec interp command"));
197 
198   interp_to_use = interp_lookup (argv[0]);
199   if (interp_to_use == NULL)
200     error (_("-interpreter-exec: could not find interpreter \"%s\""),
201 	   argv[0]);
202 
203   if (!interp_exec_p (interp_to_use))
204     error (_("-interpreter-exec: interpreter \"%s\" "
205 	     "does not support command execution"),
206 	      argv[0]);
207 
208   /* Insert the MI out hooks, making sure to also call the interpreter's hooks
209      if it has any. */
210   /* KRS: We shouldn't need this... Events should be installed and they should
211      just ALWAYS fire something out down the MI channel... */
212   mi_insert_notify_hooks ();
213 
214   /* Now run the code... */
215 
216   old_chain = make_cleanup (null_cleanup, 0);
217   for (i = 1; i < argc; i++)
218     {
219       struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
220 
221       if (e.reason < 0)
222 	{
223 	  mi_error_message = xstrdup (e.message);
224 	  make_cleanup (xfree, mi_error_message);
225 	  break;
226 	}
227     }
228 
229   mi_remove_notify_hooks ();
230 
231   if (mi_error_message != NULL)
232     error ("%s", mi_error_message);
233   do_cleanups (old_chain);
234 }
235 
236 /*
237  * mi_insert_notify_hooks - This inserts a number of hooks that are
238  * meant to produce async-notify ("=") MI messages while running
239  * commands in another interpreter using mi_interpreter_exec.  The
240  * canonical use for this is to allow access to the gdb CLI
241  * interpreter from within the MI, while still producing MI style
242  * output when actions in the CLI command change gdb's state.
243 */
244 
245 static void
246 mi_insert_notify_hooks (void)
247 {
248   deprecated_query_hook = mi_interp_query_hook;
249 }
250 
251 static void
252 mi_remove_notify_hooks (void)
253 {
254   deprecated_query_hook = NULL;
255 }
256 
257 static int
258 mi_interp_query_hook (const char *ctlstr, va_list ap)
259 {
260   return 1;
261 }
262 
263 static void
264 mi_execute_command_wrapper (char *cmd)
265 {
266   mi_execute_command (cmd, stdin == instream);
267 }
268 
269 static void
270 mi1_command_loop (void)
271 {
272   mi_command_loop (1);
273 }
274 
275 static void
276 mi2_command_loop (void)
277 {
278   mi_command_loop (2);
279 }
280 
281 static void
282 mi3_command_loop (void)
283 {
284   mi_command_loop (3);
285 }
286 
287 static void
288 mi_command_loop (int mi_version)
289 {
290   /* Turn off 8 bit strings in quoted output.  Any character with the
291      high bit set is printed using C's octal format. */
292   sevenbit_strings = 1;
293   /* Tell the world that we're alive */
294   fputs_unfiltered ("(gdb) \n", raw_stdout);
295   gdb_flush (raw_stdout);
296   start_event_loop ();
297 }
298 
299 static void
300 mi_new_thread (struct thread_info *t)
301 {
302   struct mi_interp *mi = top_level_interpreter_data ();
303   struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid));
304 
305   gdb_assert (inf);
306 
307   fprintf_unfiltered (mi->event_channel,
308 		      "thread-created,id=\"%d\",group-id=\"i%d\"",
309 		      t->num, inf->num);
310   gdb_flush (mi->event_channel);
311 }
312 
313 static void
314 mi_thread_exit (struct thread_info *t, int silent)
315 {
316   struct mi_interp *mi;
317   struct inferior *inf;
318 
319   if (silent)
320     return;
321 
322   inf = find_inferior_pid (ptid_get_pid (t->ptid));
323 
324   mi = top_level_interpreter_data ();
325   target_terminal_ours ();
326   fprintf_unfiltered (mi->event_channel,
327 		      "thread-exited,id=\"%d\",group-id=\"i%d\"",
328 		      t->num, inf->num);
329   gdb_flush (mi->event_channel);
330 }
331 
332 static void
333 mi_inferior_added (struct inferior *inf)
334 {
335   struct mi_interp *mi = top_level_interpreter_data ();
336 
337   target_terminal_ours ();
338   fprintf_unfiltered (mi->event_channel,
339 		      "thread-group-added,id=\"i%d\"",
340 		      inf->num);
341   gdb_flush (mi->event_channel);
342 }
343 
344 static void
345 mi_inferior_appeared (struct inferior *inf)
346 {
347   struct mi_interp *mi = top_level_interpreter_data ();
348 
349   target_terminal_ours ();
350   fprintf_unfiltered (mi->event_channel,
351 		      "thread-group-started,id=\"i%d\",pid=\"%d\"",
352 		      inf->num, inf->pid);
353   gdb_flush (mi->event_channel);
354 }
355 
356 static void
357 mi_inferior_exit (struct inferior *inf)
358 {
359   struct mi_interp *mi = top_level_interpreter_data ();
360 
361   target_terminal_ours ();
362   if (inf->has_exit_code)
363     fprintf_unfiltered (mi->event_channel,
364 			"thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
365 			inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
366   else
367     fprintf_unfiltered (mi->event_channel,
368 			"thread-group-exited,id=\"i%d\"", inf->num);
369 
370   gdb_flush (mi->event_channel);
371 }
372 
373 static void
374 mi_inferior_removed (struct inferior *inf)
375 {
376   struct mi_interp *mi = top_level_interpreter_data ();
377 
378   target_terminal_ours ();
379   fprintf_unfiltered (mi->event_channel,
380 		      "thread-group-removed,id=\"i%d\"",
381 		      inf->num);
382   gdb_flush (mi->event_channel);
383 }
384 
385 static void
386 mi_on_normal_stop (struct bpstats *bs, int print_frame)
387 {
388   /* Since this can be called when CLI command is executing,
389      using cli interpreter, be sure to use MI uiout for output,
390      not the current one.  */
391   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
392 
393   if (print_frame)
394     {
395       int core;
396 
397       if (uiout != mi_uiout)
398 	{
399 	  /* The normal_stop function has printed frame information into
400 	     CLI uiout, or some other non-MI uiout.  There's no way we
401 	     can extract proper fields from random uiout object, so we print
402 	     the frame again.  In practice, this can only happen when running
403 	     a CLI command in MI.  */
404 	  struct ui_out *saved_uiout = uiout;
405 
406 	  uiout = mi_uiout;
407 	  print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
408 	  uiout = saved_uiout;
409 	}
410 
411       ui_out_field_int (mi_uiout, "thread-id",
412 			pid_to_thread_id (inferior_ptid));
413       if (non_stop)
414 	{
415 	  struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
416 	    (mi_uiout, "stopped-threads");
417 
418 	  ui_out_field_int (mi_uiout, NULL,
419 			    pid_to_thread_id (inferior_ptid));
420 	  do_cleanups (back_to);
421 	}
422       else
423 	ui_out_field_string (mi_uiout, "stopped-threads", "all");
424 
425       core = target_core_of_thread (inferior_ptid);
426       if (core != -1)
427 	ui_out_field_int (mi_uiout, "core", core);
428     }
429 
430   fputs_unfiltered ("*stopped", raw_stdout);
431   mi_out_put (mi_uiout, raw_stdout);
432   mi_out_rewind (mi_uiout);
433   mi_print_timing_maybe ();
434   fputs_unfiltered ("\n", raw_stdout);
435   gdb_flush (raw_stdout);
436 }
437 
438 static void
439 mi_about_to_proceed (void)
440 {
441   /* Suppress output while calling an inferior function.  */
442 
443   if (!ptid_equal (inferior_ptid, null_ptid))
444     {
445       struct thread_info *tp = inferior_thread ();
446 
447       if (tp->control.in_infcall)
448 	return;
449     }
450 
451   mi_proceeded = 1;
452 }
453 
454 static int
455 mi_output_running_pid (struct thread_info *info, void *arg)
456 {
457   ptid_t *ptid = arg;
458 
459   if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
460     fprintf_unfiltered (raw_stdout,
461 			"*running,thread-id=\"%d\"\n",
462 			info->num);
463 
464   return 0;
465 }
466 
467 static int
468 mi_inferior_count (struct inferior *inf, void *arg)
469 {
470   if (inf->pid != FAKE_PROCESS_ID)
471     {
472       int *count_p = arg;
473       (*count_p)++;
474     }
475 
476   return 0;
477 }
478 
479 static void
480 mi_on_resume (ptid_t ptid)
481 {
482   struct thread_info *tp = NULL;
483 
484   if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
485     tp = inferior_thread ();
486   else
487     tp = find_thread_ptid (ptid);
488 
489   /* Suppress output while calling an inferior function.  */
490   if (tp->control.in_infcall)
491     return;
492 
493   /* To cater for older frontends, emit ^running, but do it only once
494      per each command.  We do it here, since at this point we know
495      that the target was successfully resumed, and in non-async mode,
496      we won't return back to MI interpreter code until the target
497      is done running, so delaying the output of "^running" until then
498      will make it impossible for frontend to know what's going on.
499 
500      In future (MI3), we'll be outputting "^done" here.  */
501   if (!running_result_record_printed && mi_proceeded)
502     {
503       fprintf_unfiltered (raw_stdout, "%s^running\n",
504 			  current_token ? current_token : "");
505     }
506 
507   if (PIDGET (ptid) == -1)
508     fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
509   else if (ptid_is_pid (ptid))
510     {
511       int count = 0;
512 
513       /* Backwards compatibility.  If there's only one inferior,
514 	 output "all", otherwise, output each resumed thread
515 	 individually.  */
516       iterate_over_inferiors (mi_inferior_count, &count);
517 
518       if (count == 1)
519 	fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
520       else
521 	iterate_over_threads (mi_output_running_pid, &ptid);
522     }
523   else
524     {
525       struct thread_info *ti = find_thread_ptid (ptid);
526 
527       gdb_assert (ti);
528       fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
529     }
530 
531   if (!running_result_record_printed && mi_proceeded)
532     {
533       running_result_record_printed = 1;
534       /* This is what gdb used to do historically -- printing prompt even if
535 	 it cannot actually accept any input.  This will be surely removed
536 	 for MI3, and may be removed even earler.  */
537       /* FIXME: review the use of target_is_async_p here -- is that
538 	 what we want? */
539       if (!target_is_async_p ())
540 	fputs_unfiltered ("(gdb) \n", raw_stdout);
541     }
542   gdb_flush (raw_stdout);
543 }
544 
545 static void
546 mi_solib_loaded (struct so_list *solib)
547 {
548   struct mi_interp *mi = top_level_interpreter_data ();
549 
550   target_terminal_ours ();
551   if (gdbarch_has_global_solist (target_gdbarch))
552     fprintf_unfiltered (mi->event_channel,
553 			"library-loaded,id=\"%s\",target-name=\"%s\","
554 			"host-name=\"%s\",symbols-loaded=\"%d\"",
555 			solib->so_original_name, solib->so_original_name,
556 			solib->so_name, solib->symbols_loaded);
557   else
558     fprintf_unfiltered (mi->event_channel,
559 			"library-loaded,id=\"%s\",target-name=\"%s\","
560 			"host-name=\"%s\",symbols-loaded=\"%d\","
561 			"thread-group=\"i%d\"",
562 			solib->so_original_name, solib->so_original_name,
563 			solib->so_name, solib->symbols_loaded,
564 			current_inferior ()->num);
565 
566   gdb_flush (mi->event_channel);
567 }
568 
569 static void
570 mi_solib_unloaded (struct so_list *solib)
571 {
572   struct mi_interp *mi = top_level_interpreter_data ();
573 
574   target_terminal_ours ();
575   if (gdbarch_has_global_solist (target_gdbarch))
576     fprintf_unfiltered (mi->event_channel,
577 			"library-unloaded,id=\"%s\",target-name=\"%s\","
578 			"host-name=\"%s\"",
579 			solib->so_original_name, solib->so_original_name,
580 			solib->so_name);
581   else
582     fprintf_unfiltered (mi->event_channel,
583 			"library-unloaded,id=\"%s\",target-name=\"%s\","
584 			"host-name=\"%s\",thread-group=\"i%d\"",
585 			solib->so_original_name, solib->so_original_name,
586 			solib->so_name, current_inferior ()->num);
587 
588   gdb_flush (mi->event_channel);
589 }
590 
591 static int
592 report_initial_inferior (struct inferior *inf, void *closure)
593 {
594   /* This function is called from mi_intepreter_init, and since
595      mi_inferior_added assumes that inferior is fully initialized
596      and top_level_interpreter_data is set, we cannot call
597      it here.  */
598   struct mi_interp *mi = closure;
599 
600   target_terminal_ours ();
601   fprintf_unfiltered (mi->event_channel,
602 		      "thread-group-added,id=\"i%d\"",
603 		      inf->num);
604   gdb_flush (mi->event_channel);
605   return 0;
606 }
607 
608 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
609 
610 void
611 _initialize_mi_interp (void)
612 {
613   static const struct interp_procs procs =
614   {
615     mi_interpreter_init,	/* init_proc */
616     mi_interpreter_resume,	/* resume_proc */
617     mi_interpreter_suspend,	/* suspend_proc */
618     mi_interpreter_exec,	/* exec_proc */
619     mi_interpreter_prompt_p	/* prompt_proc_p */
620   };
621 
622   /* The various interpreter levels.  */
623   interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
624   interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
625   interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
626 
627   /* "mi" selects the most recent released version.  "mi2" was
628      released as part of GDB 6.0.  */
629   interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
630 }
631