1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
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"
38 /* These are the interpreter setup, etc. functions for the MI interpreter */
39 static void mi_execute_command_wrapper (char *cmd);
40 static void mi_command_loop (int mi_version);
42 /* These are hooks that we put in place while doing interpreter_exec
43 so we can report interesting things that happened "behind the mi's
44 back" in this command */
45 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
46 ATTR_FORMAT (printf, 1, 0);
48 static void mi3_command_loop (void);
49 static void mi2_command_loop (void);
50 static void mi1_command_loop (void);
52 static void mi_insert_notify_hooks (void);
53 static void mi_remove_notify_hooks (void);
54 static void mi_on_normal_stop (struct bpstats *bs);
56 static void mi_new_thread (struct thread_info *t);
57 static void mi_thread_exit (struct thread_info *t);
58 static void mi_new_inferior (int pid);
59 static void mi_inferior_exit (int pid);
60 static void mi_on_resume (ptid_t ptid);
63 mi_interpreter_init (int top_level)
65 struct mi_interp *mi = XMALLOC (struct mi_interp);
67 /* HACK: We need to force stdout/stderr to point at the console. This avoids
68 any potential side effects caused by legacy code that is still
69 using the TUI / fputs_unfiltered_hook. So we set up output channels for
70 this now, and swap them in when we are run. */
72 raw_stdout = stdio_fileopen (stdout);
74 /* Create MI channels */
75 mi->out = mi_console_file_new (raw_stdout, "~", '"');
76 mi->err = mi_console_file_new (raw_stdout, "&", '"');
78 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
79 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
83 observer_attach_new_thread (mi_new_thread);
84 observer_attach_thread_exit (mi_thread_exit);
85 observer_attach_new_inferior (mi_new_inferior);
86 observer_attach_inferior_exit (mi_inferior_exit);
87 observer_attach_normal_stop (mi_on_normal_stop);
88 observer_attach_target_resumed (mi_on_resume);
95 mi_interpreter_resume (void *data)
97 struct mi_interp *mi = data;
98 /* As per hack note in mi_interpreter_init, swap in the output channels... */
100 gdb_setup_readline ();
102 /* These overwrite some of the initialization done in
103 _intialize_event_loop. */
104 call_readline = gdb_readline2;
105 input_handler = mi_execute_command_wrapper;
106 add_file_handler (input_fd, stdin_event_handler, 0);
107 async_command_editing_p = 0;
108 /* FIXME: This is a total hack for now. PB's use of the MI
109 implicitly relies on a bug in the async support which allows
110 asynchronous commands to leak through the commmand loop. The bug
111 involves (but is not limited to) the fact that sync_execution was
112 erroneously initialized to 0. Duplicate by initializing it thus
116 gdb_stdout = mi->out;
117 /* Route error and log output through the MI */
118 gdb_stderr = mi->err;
119 gdb_stdlog = mi->log;
120 /* Route target output through the MI. */
121 gdb_stdtarg = mi->targ;
122 /* Route target error through the MI as well. */
123 gdb_stdtargerr = mi->targ;
125 /* Replace all the hooks that we know about. There really needs to
126 be a better way of doing this... */
127 clear_interpreter_hooks ();
129 deprecated_show_load_progress = mi_load_progress;
131 /* If we're _the_ interpreter, take control. */
132 if (current_interp_named_p (INTERP_MI1))
133 deprecated_command_loop_hook = mi1_command_loop;
134 else if (current_interp_named_p (INTERP_MI2))
135 deprecated_command_loop_hook = mi2_command_loop;
136 else if (current_interp_named_p (INTERP_MI3))
137 deprecated_command_loop_hook = mi3_command_loop;
139 deprecated_command_loop_hook = mi2_command_loop;
145 mi_interpreter_suspend (void *data)
147 gdb_disable_readline ();
151 static struct gdb_exception
152 mi_interpreter_exec (void *data, const char *command)
154 static struct gdb_exception ok;
155 char *tmp = alloca (strlen (command) + 1);
156 strcpy (tmp, command);
157 mi_execute_command_wrapper (tmp);
158 return exception_none;
161 /* Never display the default gdb prompt in mi case. */
163 mi_interpreter_prompt_p (void *data)
169 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
171 struct interp *interp_to_use;
173 struct interp_procs *procs;
174 char *mi_error_message = NULL;
175 struct cleanup *old_chain;
178 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
180 interp_to_use = interp_lookup (argv[0]);
181 if (interp_to_use == NULL)
182 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
184 if (!interp_exec_p (interp_to_use))
185 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
188 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
190 /* KRS: We shouldn't need this... Events should be installed and they should
191 just ALWAYS fire something out down the MI channel... */
192 mi_insert_notify_hooks ();
194 /* Now run the code... */
196 old_chain = make_cleanup (null_cleanup, 0);
197 for (i = 1; i < argc; i++)
199 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
202 mi_error_message = xstrdup (e.message);
203 make_cleanup (xfree, mi_error_message);
208 mi_remove_notify_hooks ();
210 if (mi_error_message != NULL)
211 error ("%s", mi_error_message);
212 do_cleanups (old_chain);
216 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
217 * async-notify ("=") MI messages while running commands in another interpreter
218 * using mi_interpreter_exec. The canonical use for this is to allow access to
219 * the gdb CLI interpreter from within the MI, while still producing MI style output
220 * when actions in the CLI command change gdb's state.
224 mi_insert_notify_hooks (void)
226 deprecated_query_hook = mi_interp_query_hook;
230 mi_remove_notify_hooks (void)
232 deprecated_query_hook = NULL;
236 mi_interp_query_hook (const char *ctlstr, va_list ap)
242 mi_execute_command_wrapper (char *cmd)
244 mi_execute_command (cmd, stdin == instream);
248 mi1_command_loop (void)
254 mi2_command_loop (void)
260 mi3_command_loop (void)
266 mi_command_loop (int mi_version)
268 /* Turn off 8 bit strings in quoted output. Any character with the
269 high bit set is printed using C's octal format. */
270 sevenbit_strings = 1;
271 /* Tell the world that we're alive */
272 fputs_unfiltered ("(gdb) \n", raw_stdout);
273 gdb_flush (raw_stdout);
278 mi_new_thread (struct thread_info *t)
280 struct mi_interp *mi = top_level_interpreter_data ();
282 fprintf_unfiltered (mi->event_channel,
283 "thread-created,id=\"%d\",group-id=\"%d\"",
284 t->num, t->ptid.pid);
285 gdb_flush (mi->event_channel);
289 mi_thread_exit (struct thread_info *t)
291 struct mi_interp *mi = top_level_interpreter_data ();
292 target_terminal_ours ();
293 fprintf_unfiltered (mi->event_channel,
294 "thread-exited,id=\"%d\",group-id=\"%d\"",
296 gdb_flush (mi->event_channel);
300 mi_new_inferior (int pid)
302 struct mi_interp *mi = top_level_interpreter_data ();
303 target_terminal_ours ();
304 fprintf_unfiltered (mi->event_channel, "thread-group-created,id=\"%d\"",
306 gdb_flush (mi->event_channel);
310 mi_inferior_exit (int pid)
312 struct mi_interp *mi = top_level_interpreter_data ();
313 target_terminal_ours ();
314 fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"%d\"",
316 gdb_flush (mi->event_channel);
320 mi_on_normal_stop (struct bpstats *bs)
322 /* Since this can be called when CLI command is executing,
323 using cli interpreter, be sure to use MI uiout for output,
324 not the current one. */
325 struct ui_out *uiout = interp_ui_out (top_level_interpreter ());
326 struct mi_interp *mi = top_level_interpreter_data ();
328 fputs_unfiltered ("*stopped", raw_stdout);
329 mi_out_put (uiout, raw_stdout);
330 mi_out_rewind (uiout);
331 fputs_unfiltered ("\n", raw_stdout);
332 gdb_flush (raw_stdout);
336 mi_on_resume (ptid_t ptid)
338 /* To cater for older frontends, emit ^running, but do it only once
339 per each command. We do it here, since at this point we know
340 that the target was successfully resumed, and in non-async mode,
341 we won't return back to MI interpreter code until the target
342 is done running, so delaying the output of "^running" until then
343 will make it impossible for frontend to know what's going on.
345 In future (MI3), we'll be outputting "^done" here. */
346 if (!running_result_record_printed)
349 fputs_unfiltered (current_token, raw_stdout);
350 fputs_unfiltered ("^running\n", raw_stdout);
353 if (PIDGET (ptid) == -1)
354 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
355 else if (thread_count () == 0)
357 /* This is a target where for single-threaded programs the thread
358 table has zero threads. Don't print any thread-id field. */
359 fprintf_unfiltered (raw_stdout, "*running\n");
363 struct thread_info *ti = find_thread_pid (ptid);
365 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
368 if (!running_result_record_printed)
370 running_result_record_printed = 1;
371 /* This is what gdb used to do historically -- printing prompt even if
372 it cannot actually accept any input. This will be surely removed
373 for MI3, and may be removed even earler. */
374 /* FIXME: review the use of target_is_async_p here -- is that
376 if (!target_is_async_p ())
377 fputs_unfiltered ("(gdb) \n", raw_stdout);
379 gdb_flush (raw_stdout);
382 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
385 _initialize_mi_interp (void)
387 static const struct interp_procs procs =
389 mi_interpreter_init, /* init_proc */
390 mi_interpreter_resume, /* resume_proc */
391 mi_interpreter_suspend, /* suspend_proc */
392 mi_interpreter_exec, /* exec_proc */
393 mi_interpreter_prompt_p /* prompt_proc_p */
396 /* The various interpreter levels. */
397 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
398 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
399 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
401 /* "mi" selects the most recent released version. "mi2" was
402 released as part of GDB 6.0. */
403 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));