1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009
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_new_inferior (int pid);
60 static void mi_inferior_exit (int pid);
61 static void mi_on_resume (ptid_t ptid);
62 static void mi_solib_loaded (struct so_list *solib);
63 static void mi_solib_unloaded (struct so_list *solib);
64 static void mi_about_to_proceed (void);
67 mi_interpreter_init (int top_level)
69 struct mi_interp *mi = XMALLOC (struct mi_interp);
71 /* HACK: We need to force stdout/stderr to point at the console. This avoids
72 any potential side effects caused by legacy code that is still
73 using the TUI / fputs_unfiltered_hook. So we set up output channels for
74 this now, and swap them in when we are run. */
76 raw_stdout = stdio_fileopen (stdout);
78 /* Create MI channels */
79 mi->out = mi_console_file_new (raw_stdout, "~", '"');
80 mi->err = mi_console_file_new (raw_stdout, "&", '"');
82 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
83 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
87 observer_attach_new_thread (mi_new_thread);
88 observer_attach_thread_exit (mi_thread_exit);
89 observer_attach_new_inferior (mi_new_inferior);
90 observer_attach_inferior_exit (mi_inferior_exit);
91 observer_attach_normal_stop (mi_on_normal_stop);
92 observer_attach_target_resumed (mi_on_resume);
93 observer_attach_solib_loaded (mi_solib_loaded);
94 observer_attach_solib_unloaded (mi_solib_unloaded);
95 observer_attach_about_to_proceed (mi_about_to_proceed);
102 mi_interpreter_resume (void *data)
104 struct mi_interp *mi = data;
105 /* As per hack note in mi_interpreter_init, swap in the output channels... */
107 gdb_setup_readline ();
109 /* These overwrite some of the initialization done in
110 _intialize_event_loop. */
111 call_readline = gdb_readline2;
112 input_handler = mi_execute_command_wrapper;
113 add_file_handler (input_fd, stdin_event_handler, 0);
114 async_command_editing_p = 0;
115 /* FIXME: This is a total hack for now. PB's use of the MI
116 implicitly relies on a bug in the async support which allows
117 asynchronous commands to leak through the commmand loop. The bug
118 involves (but is not limited to) the fact that sync_execution was
119 erroneously initialized to 0. Duplicate by initializing it thus
123 gdb_stdout = mi->out;
124 /* Route error and log output through the MI */
125 gdb_stderr = mi->err;
126 gdb_stdlog = mi->log;
127 /* Route target output through the MI. */
128 gdb_stdtarg = mi->targ;
129 /* Route target error through the MI as well. */
130 gdb_stdtargerr = mi->targ;
132 /* Replace all the hooks that we know about. There really needs to
133 be a better way of doing this... */
134 clear_interpreter_hooks ();
136 deprecated_show_load_progress = mi_load_progress;
138 /* If we're _the_ interpreter, take control. */
139 if (current_interp_named_p (INTERP_MI1))
140 deprecated_command_loop_hook = mi1_command_loop;
141 else if (current_interp_named_p (INTERP_MI2))
142 deprecated_command_loop_hook = mi2_command_loop;
143 else if (current_interp_named_p (INTERP_MI3))
144 deprecated_command_loop_hook = mi3_command_loop;
146 deprecated_command_loop_hook = mi2_command_loop;
152 mi_interpreter_suspend (void *data)
154 gdb_disable_readline ();
158 static struct gdb_exception
159 mi_interpreter_exec (void *data, const char *command)
161 static struct gdb_exception ok;
162 char *tmp = alloca (strlen (command) + 1);
163 strcpy (tmp, command);
164 mi_execute_command_wrapper (tmp);
165 return exception_none;
168 /* Never display the default gdb prompt in mi case. */
170 mi_interpreter_prompt_p (void *data)
176 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
178 struct interp *interp_to_use;
180 struct interp_procs *procs;
181 char *mi_error_message = NULL;
182 struct cleanup *old_chain;
185 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
187 interp_to_use = interp_lookup (argv[0]);
188 if (interp_to_use == NULL)
189 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
191 if (!interp_exec_p (interp_to_use))
192 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
195 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
197 /* KRS: We shouldn't need this... Events should be installed and they should
198 just ALWAYS fire something out down the MI channel... */
199 mi_insert_notify_hooks ();
201 /* Now run the code... */
203 old_chain = make_cleanup (null_cleanup, 0);
204 for (i = 1; i < argc; i++)
206 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
209 mi_error_message = xstrdup (e.message);
210 make_cleanup (xfree, mi_error_message);
215 mi_remove_notify_hooks ();
217 if (mi_error_message != NULL)
218 error ("%s", mi_error_message);
219 do_cleanups (old_chain);
223 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
224 * async-notify ("=") MI messages while running commands in another interpreter
225 * using mi_interpreter_exec. The canonical use for this is to allow access to
226 * the gdb CLI interpreter from within the MI, while still producing MI style output
227 * when actions in the CLI command change gdb's state.
231 mi_insert_notify_hooks (void)
233 deprecated_query_hook = mi_interp_query_hook;
237 mi_remove_notify_hooks (void)
239 deprecated_query_hook = NULL;
243 mi_interp_query_hook (const char *ctlstr, va_list ap)
249 mi_execute_command_wrapper (char *cmd)
251 mi_execute_command (cmd, stdin == instream);
255 mi1_command_loop (void)
261 mi2_command_loop (void)
267 mi3_command_loop (void)
273 mi_command_loop (int mi_version)
275 /* Turn off 8 bit strings in quoted output. Any character with the
276 high bit set is printed using C's octal format. */
277 sevenbit_strings = 1;
278 /* Tell the world that we're alive */
279 fputs_unfiltered ("(gdb) \n", raw_stdout);
280 gdb_flush (raw_stdout);
285 mi_new_thread (struct thread_info *t)
287 struct mi_interp *mi = top_level_interpreter_data ();
289 fprintf_unfiltered (mi->event_channel,
290 "thread-created,id=\"%d\",group-id=\"%d\"",
291 t->num, t->ptid.pid);
292 gdb_flush (mi->event_channel);
296 mi_thread_exit (struct thread_info *t, int silent)
298 struct mi_interp *mi;
303 mi = top_level_interpreter_data ();
304 target_terminal_ours ();
305 fprintf_unfiltered (mi->event_channel,
306 "thread-exited,id=\"%d\",group-id=\"%d\"",
308 gdb_flush (mi->event_channel);
312 mi_new_inferior (int pid)
314 struct mi_interp *mi = top_level_interpreter_data ();
315 target_terminal_ours ();
316 fprintf_unfiltered (mi->event_channel, "thread-group-created,id=\"%d\"",
318 gdb_flush (mi->event_channel);
322 mi_inferior_exit (int pid)
324 struct mi_interp *mi = top_level_interpreter_data ();
325 target_terminal_ours ();
326 fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"%d\"",
328 gdb_flush (mi->event_channel);
332 mi_on_normal_stop (struct bpstats *bs, int print_frame)
334 /* Since this can be called when CLI command is executing,
335 using cli interpreter, be sure to use MI uiout for output,
336 not the current one. */
337 struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
338 struct mi_interp *mi = top_level_interpreter_data ();
342 if (uiout != mi_uiout)
344 /* The normal_stop function has printed frame information into
345 CLI uiout, or some other non-MI uiout. There's no way we
346 can extract proper fields from random uiout object, so we print
347 the frame again. In practice, this can only happen when running
348 a CLI command in MI. */
349 struct ui_out *saved_uiout = uiout;
351 print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
355 ui_out_field_int (mi_uiout, "thread-id",
356 pid_to_thread_id (inferior_ptid));
359 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
360 (mi_uiout, "stopped-threads");
361 ui_out_field_int (mi_uiout, NULL,
362 pid_to_thread_id (inferior_ptid));
363 do_cleanups (back_to);
366 ui_out_field_string (mi_uiout, "stopped-threads", "all");
369 fputs_unfiltered ("*stopped", raw_stdout);
370 mi_out_put (mi_uiout, raw_stdout);
371 mi_out_rewind (mi_uiout);
372 mi_print_timing_maybe ();
373 fputs_unfiltered ("\n", raw_stdout);
374 gdb_flush (raw_stdout);
378 mi_about_to_proceed (void)
380 /* Suppress output while calling an inferior function. */
382 if (!ptid_equal (inferior_ptid, null_ptid))
384 struct thread_info *tp = inferior_thread ();
393 mi_on_resume (ptid_t ptid)
395 struct thread_info *tp = NULL;
397 if (ptid_equal (ptid, minus_one_ptid))
398 tp = inferior_thread ();
400 tp = find_thread_pid (ptid);
402 /* Suppress output while calling an inferior function. */
406 /* To cater for older frontends, emit ^running, but do it only once
407 per each command. We do it here, since at this point we know
408 that the target was successfully resumed, and in non-async mode,
409 we won't return back to MI interpreter code until the target
410 is done running, so delaying the output of "^running" until then
411 will make it impossible for frontend to know what's going on.
413 In future (MI3), we'll be outputting "^done" here. */
414 if (!running_result_record_printed && mi_proceeded)
417 fputs_unfiltered (current_token, raw_stdout);
418 fputs_unfiltered ("^running\n", raw_stdout);
421 if (PIDGET (ptid) == -1)
422 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
423 else if (thread_count () == 0)
425 /* This is a target where for single-threaded programs the thread
426 table has zero threads. Don't print any thread-id field. */
427 fprintf_unfiltered (raw_stdout, "*running\n");
431 struct thread_info *ti = find_thread_pid (ptid);
433 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
436 if (!running_result_record_printed && mi_proceeded)
438 running_result_record_printed = 1;
439 /* This is what gdb used to do historically -- printing prompt even if
440 it cannot actually accept any input. This will be surely removed
441 for MI3, and may be removed even earler. */
442 /* FIXME: review the use of target_is_async_p here -- is that
444 if (!target_is_async_p ())
445 fputs_unfiltered ("(gdb) \n", raw_stdout);
447 gdb_flush (raw_stdout);
451 mi_solib_loaded (struct so_list *solib)
453 struct mi_interp *mi = top_level_interpreter_data ();
454 target_terminal_ours ();
455 fprintf_unfiltered (mi->event_channel,
456 "library-loaded,id=\"%s\",target-name=\"%s\",host-name=\"%s\",symbols-loaded=\"%d\"",
457 solib->so_original_name, solib->so_original_name,
458 solib->so_name, solib->symbols_loaded);
459 gdb_flush (mi->event_channel);
463 mi_solib_unloaded (struct so_list *solib)
465 struct mi_interp *mi = top_level_interpreter_data ();
466 target_terminal_ours ();
467 fprintf_unfiltered (mi->event_channel,
468 "library-unloaded,id=\"%s\",target-name=\"%s\",host-name=\"%s\"",
469 solib->so_original_name, solib->so_original_name,
471 gdb_flush (mi->event_channel);
475 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
478 _initialize_mi_interp (void)
480 static const struct interp_procs procs =
482 mi_interpreter_init, /* init_proc */
483 mi_interpreter_resume, /* resume_proc */
484 mi_interpreter_suspend, /* suspend_proc */
485 mi_interpreter_exec, /* exec_proc */
486 mi_interpreter_prompt_p /* prompt_proc_p */
489 /* The various interpreter levels. */
490 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
491 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
492 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
494 /* "mi" selects the most recent released version. "mi2" was
495 released as part of GDB 6.0. */
496 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));