1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdb_string.h"
24 #include "event-top.h"
25 #include "event-loop.h"
29 #include "exceptions.h"
33 #include "mi-console.h"
34 #include "mi-common.h"
36 #include "gdbthread.h"
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);
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 ATTR_FORMAT (printf, 1, 0);
49 static void mi3_command_loop (void);
50 static void mi2_command_loop (void);
51 static void mi1_command_loop (void);
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);
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);
68 static int report_initial_inferior (struct inferior *inf, void *closure);
71 mi_interpreter_init (int top_level)
73 struct mi_interp *mi = XMALLOC (struct mi_interp);
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. */
80 raw_stdout = stdio_fileopen (stdout);
82 /* Create MI channels */
83 mi->out = mi_console_file_new (raw_stdout, "~", '"');
84 mi->err = mi_console_file_new (raw_stdout, "&", '"');
86 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
87 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
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);
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);
113 mi_interpreter_resume (void *data)
115 struct mi_interp *mi = data;
116 /* As per hack note in mi_interpreter_init, swap in the output channels... */
118 gdb_setup_readline ();
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
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;
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 ();
147 deprecated_show_load_progress = mi_load_progress;
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;
157 deprecated_command_loop_hook = mi2_command_loop;
163 mi_interpreter_suspend (void *data)
165 gdb_disable_readline ();
169 static struct gdb_exception
170 mi_interpreter_exec (void *data, const char *command)
172 static struct gdb_exception ok;
173 char *tmp = alloca (strlen (command) + 1);
174 strcpy (tmp, command);
175 mi_execute_command_wrapper (tmp);
176 return exception_none;
179 /* Never display the default gdb prompt in mi case. */
181 mi_interpreter_prompt_p (void *data)
187 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
189 struct interp *interp_to_use;
191 struct interp_procs *procs;
192 char *mi_error_message = NULL;
193 struct cleanup *old_chain;
196 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
198 interp_to_use = interp_lookup (argv[0]);
199 if (interp_to_use == NULL)
200 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
202 if (!interp_exec_p (interp_to_use))
203 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
206 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
208 /* KRS: We shouldn't need this... Events should be installed and they should
209 just ALWAYS fire something out down the MI channel... */
210 mi_insert_notify_hooks ();
212 /* Now run the code... */
214 old_chain = make_cleanup (null_cleanup, 0);
215 for (i = 1; i < argc; i++)
217 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
220 mi_error_message = xstrdup (e.message);
221 make_cleanup (xfree, mi_error_message);
226 mi_remove_notify_hooks ();
228 if (mi_error_message != NULL)
229 error ("%s", mi_error_message);
230 do_cleanups (old_chain);
234 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
235 * async-notify ("=") MI messages while running commands in another interpreter
236 * using mi_interpreter_exec. The canonical use for this is to allow access to
237 * the gdb CLI interpreter from within the MI, while still producing MI style output
238 * when actions in the CLI command change gdb's state.
242 mi_insert_notify_hooks (void)
244 deprecated_query_hook = mi_interp_query_hook;
248 mi_remove_notify_hooks (void)
250 deprecated_query_hook = NULL;
254 mi_interp_query_hook (const char *ctlstr, va_list ap)
260 mi_execute_command_wrapper (char *cmd)
262 mi_execute_command (cmd, stdin == instream);
266 mi1_command_loop (void)
272 mi2_command_loop (void)
278 mi3_command_loop (void)
284 mi_command_loop (int mi_version)
286 /* Turn off 8 bit strings in quoted output. Any character with the
287 high bit set is printed using C's octal format. */
288 sevenbit_strings = 1;
289 /* Tell the world that we're alive */
290 fputs_unfiltered ("(gdb) \n", raw_stdout);
291 gdb_flush (raw_stdout);
296 mi_new_thread (struct thread_info *t)
298 struct mi_interp *mi = top_level_interpreter_data ();
299 struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid));
303 fprintf_unfiltered (mi->event_channel,
304 "thread-created,id=\"%d\",group-id=\"i%d\"",
306 gdb_flush (mi->event_channel);
310 mi_thread_exit (struct thread_info *t, int silent)
312 struct mi_interp *mi;
313 struct inferior *inf;
318 inf = find_inferior_pid (ptid_get_pid (t->ptid));
320 mi = top_level_interpreter_data ();
321 target_terminal_ours ();
322 fprintf_unfiltered (mi->event_channel,
323 "thread-exited,id=\"%d\",group-id=\"i%d\"",
325 gdb_flush (mi->event_channel);
329 mi_inferior_added (struct inferior *inf)
331 struct mi_interp *mi = top_level_interpreter_data ();
332 target_terminal_ours ();
333 fprintf_unfiltered (mi->event_channel,
334 "thread-group-added,id=\"i%d\"",
336 gdb_flush (mi->event_channel);
340 mi_inferior_appeared (struct inferior *inf)
342 struct mi_interp *mi = top_level_interpreter_data ();
343 target_terminal_ours ();
344 fprintf_unfiltered (mi->event_channel,
345 "thread-group-started,id=\"i%d\",pid=\"%d\"",
347 gdb_flush (mi->event_channel);
351 mi_inferior_exit (struct inferior *inf)
353 struct mi_interp *mi = top_level_interpreter_data ();
354 target_terminal_ours ();
355 fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"i%d\"",
357 gdb_flush (mi->event_channel);
361 mi_inferior_removed (struct inferior *inf)
363 struct mi_interp *mi = top_level_interpreter_data ();
364 target_terminal_ours ();
365 fprintf_unfiltered (mi->event_channel,
366 "thread-group-removed,id=\"i%d\"",
368 gdb_flush (mi->event_channel);
372 mi_on_normal_stop (struct bpstats *bs, int print_frame)
374 /* Since this can be called when CLI command is executing,
375 using cli interpreter, be sure to use MI uiout for output,
376 not the current one. */
377 struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
378 struct mi_interp *mi = top_level_interpreter_data ();
383 if (uiout != mi_uiout)
385 /* The normal_stop function has printed frame information into
386 CLI uiout, or some other non-MI uiout. There's no way we
387 can extract proper fields from random uiout object, so we print
388 the frame again. In practice, this can only happen when running
389 a CLI command in MI. */
390 struct ui_out *saved_uiout = uiout;
392 print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
396 ui_out_field_int (mi_uiout, "thread-id",
397 pid_to_thread_id (inferior_ptid));
400 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
401 (mi_uiout, "stopped-threads");
402 ui_out_field_int (mi_uiout, NULL,
403 pid_to_thread_id (inferior_ptid));
404 do_cleanups (back_to);
407 ui_out_field_string (mi_uiout, "stopped-threads", "all");
409 core = target_core_of_thread (inferior_ptid);
411 ui_out_field_int (mi_uiout, "core", core);
414 fputs_unfiltered ("*stopped", raw_stdout);
415 mi_out_put (mi_uiout, raw_stdout);
416 mi_out_rewind (mi_uiout);
417 mi_print_timing_maybe ();
418 fputs_unfiltered ("\n", raw_stdout);
419 gdb_flush (raw_stdout);
423 mi_about_to_proceed (void)
425 /* Suppress output while calling an inferior function. */
427 if (!ptid_equal (inferior_ptid, null_ptid))
429 struct thread_info *tp = inferior_thread ();
438 mi_output_running_pid (struct thread_info *info, void *arg)
442 if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
443 fprintf_unfiltered (raw_stdout,
444 "*running,thread-id=\"%d\"\n",
451 mi_inferior_count (struct inferior *inf, void *arg)
463 mi_on_resume (ptid_t ptid)
465 struct thread_info *tp = NULL;
467 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
468 tp = inferior_thread ();
470 tp = find_thread_ptid (ptid);
472 /* Suppress output while calling an inferior function. */
476 /* To cater for older frontends, emit ^running, but do it only once
477 per each command. We do it here, since at this point we know
478 that the target was successfully resumed, and in non-async mode,
479 we won't return back to MI interpreter code until the target
480 is done running, so delaying the output of "^running" until then
481 will make it impossible for frontend to know what's going on.
483 In future (MI3), we'll be outputting "^done" here. */
484 if (!running_result_record_printed && mi_proceeded)
486 fprintf_unfiltered (raw_stdout, "%s^running\n",
487 current_token ? current_token : "");
490 if (PIDGET (ptid) == -1)
491 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
492 else if (ptid_is_pid (ptid))
496 /* Backwards compatibility. If there's only one inferior,
497 output "all", otherwise, output each resumed thread
499 iterate_over_inferiors (mi_inferior_count, &count);
502 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
504 iterate_over_threads (mi_output_running_pid, &ptid);
508 struct thread_info *ti = find_thread_ptid (ptid);
510 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
513 if (!running_result_record_printed && mi_proceeded)
515 running_result_record_printed = 1;
516 /* This is what gdb used to do historically -- printing prompt even if
517 it cannot actually accept any input. This will be surely removed
518 for MI3, and may be removed even earler. */
519 /* FIXME: review the use of target_is_async_p here -- is that
521 if (!target_is_async_p ())
522 fputs_unfiltered ("(gdb) \n", raw_stdout);
524 gdb_flush (raw_stdout);
528 mi_solib_loaded (struct so_list *solib)
530 struct mi_interp *mi = top_level_interpreter_data ();
531 target_terminal_ours ();
532 if (gdbarch_has_global_solist (target_gdbarch))
533 fprintf_unfiltered (mi->event_channel,
534 "library-loaded,id=\"%s\",target-name=\"%s\","
535 "host-name=\"%s\",symbols-loaded=\"%d\"",
536 solib->so_original_name, solib->so_original_name,
537 solib->so_name, solib->symbols_loaded);
539 fprintf_unfiltered (mi->event_channel,
540 "library-loaded,id=\"%s\",target-name=\"%s\","
541 "host-name=\"%s\",symbols-loaded=\"%d\","
542 "thread-group=\"i%d\"",
543 solib->so_original_name, solib->so_original_name,
544 solib->so_name, solib->symbols_loaded,
545 current_inferior ()->num);
547 gdb_flush (mi->event_channel);
551 mi_solib_unloaded (struct so_list *solib)
553 struct mi_interp *mi = top_level_interpreter_data ();
554 target_terminal_ours ();
555 if (gdbarch_has_global_solist (target_gdbarch))
556 fprintf_unfiltered (mi->event_channel,
557 "library-unloaded,id=\"%s\",target-name=\"%s\","
559 solib->so_original_name, solib->so_original_name,
562 fprintf_unfiltered (mi->event_channel,
563 "library-unloaded,id=\"%s\",target-name=\"%s\","
564 "host-name=\"%s\",thread-group=\"i%d\"",
565 solib->so_original_name, solib->so_original_name,
566 solib->so_name, current_inferior ()->num);
568 gdb_flush (mi->event_channel);
572 report_initial_inferior (struct inferior *inf, void *closure)
574 /* This function is called from mi_intepreter_init, and since
575 mi_inferior_added assumes that inferior is fully initialized
576 and top_level_interpreter_data is set, we cannot call
578 struct mi_interp *mi = closure;
579 target_terminal_ours ();
580 fprintf_unfiltered (mi->event_channel,
581 "thread-group-added,id=\"i%d\"",
583 gdb_flush (mi->event_channel);
587 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
590 _initialize_mi_interp (void)
592 static const struct interp_procs procs =
594 mi_interpreter_init, /* init_proc */
595 mi_interpreter_resume, /* resume_proc */
596 mi_interpreter_suspend, /* suspend_proc */
597 mi_interpreter_exec, /* exec_proc */
598 mi_interpreter_prompt_p /* prompt_proc_p */
601 /* The various interpreter levels. */
602 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
603 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
604 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
606 /* "mi" selects the most recent released version. "mi2" was
607 released as part of GDB 6.0. */
608 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));