1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
25 #include "event-top.h"
26 #include "event-loop.h"
34 #include "mi-console.h"
38 /* MI's output channels */
43 struct ui_file *event_channel;
45 /* This is the interpreter for the mi... */
46 struct interp *mi2_interp;
47 struct interp *mi1_interp;
48 struct interp *mi_interp;
51 /* These are the interpreter setup, etc. functions for the MI interpreter */
52 static void mi_execute_command_wrapper (char *cmd);
53 static void mi_command_loop (int mi_version);
54 static char *mi_input (char *);
56 /* These are hooks that we put in place while doing interpreter_exec
57 so we can report interesting things that happened "behind the mi's
58 back" in this command */
59 static int mi_interp_query_hook (const char *ctlstr, va_list ap);
60 static char *mi_interp_read_one_line_hook (char *prompt, int repeat,
63 static void mi2_command_loop (void);
64 static void mi1_command_loop (void);
66 static void mi_insert_notify_hooks (void);
67 static void mi_remove_notify_hooks (void);
70 mi_interpreter_init (void)
72 struct mi_interp *mi = XMALLOC (struct mi_interp);
74 /* Why is this a part of the mi architecture? */
76 mi_setup_architecture_data ();
78 /* HACK: We need to force stdout/stderr to point at the console. This avoids
79 any potential side effects caused by legacy code that is still
80 using the TUI / fputs_unfiltered_hook. So we set up output channels for
81 this now, and swap them in when we are run. */
83 raw_stdout = stdio_fileopen (stdout);
85 /* Create MI channels */
86 mi->out = mi_console_file_new (raw_stdout, "~", '"');
87 mi->err = mi_console_file_new (raw_stdout, "&", '"');
89 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
90 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
96 mi_interpreter_resume (void *data)
98 struct mi_interp *mi = data;
99 /* As per hack note in mi_interpreter_init, swap in the output channels... */
101 gdb_setup_readline ();
105 /* These overwrite some of the initialization done in
106 _intialize_event_loop. */
107 call_readline = gdb_readline2;
108 input_handler = mi_execute_command_wrapper;
109 add_file_handler (input_fd, stdin_event_handler, 0);
110 async_command_editing_p = 0;
111 /* FIXME: This is a total hack for now. PB's use of the MI implicitly
112 relies on a bug in the async support which allows asynchronous
113 commands to leak through the commmand loop. The bug involves
114 (but is not limited to) the fact that sync_execution was
115 erroneously initialized to 0. Duplicate by initializing it
120 gdb_stdout = mi->out;
121 /* Route error and log output through the MI */
122 gdb_stderr = mi->err;
123 gdb_stdlog = mi->log;
124 /* Route target output through the MI. */
125 gdb_stdtarg = mi->targ;
127 /* Replace all the hooks that we know about. There really needs to
128 be a better way of doing this... */
129 clear_interpreter_hooks ();
131 show_load_progress = mi_load_progress;
133 /* If we're _the_ interpreter, take control. */
134 if (current_interp_named_p (INTERP_MI1))
135 command_loop_hook = mi1_command_loop;
136 else if (current_interp_named_p (INTERP_MI))
137 command_loop_hook = mi2_command_loop;
145 mi_interpreter_suspend (void *data)
147 gdb_disable_readline ();
152 mi_interpreter_exec (void *data, const char *command)
154 char *tmp = alloca (strlen (command) + 1);
155 strcpy (tmp, command);
156 mi_execute_command_wrapper (tmp);
160 /* Never display the default gdb prompt in mi case. */
162 mi_interpreter_prompt_p (void *data)
168 mi_interpreter_exec_continuation (struct continuation_arg *arg)
170 bpstat_do_actions (&stop_bpstat);
171 if (!target_executing)
173 fputs_unfiltered ("*stopped", raw_stdout);
174 mi_out_put (uiout, raw_stdout);
175 fputs_unfiltered ("\n", raw_stdout);
176 fputs_unfiltered ("(gdb) \n", raw_stdout);
177 gdb_flush (raw_stdout);
178 do_exec_cleanups (ALL_CLEANUPS);
180 else if (target_can_async_p ())
182 add_continuation (mi_interpreter_exec_continuation, NULL);
187 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
189 struct interp *interp_to_use;
190 enum mi_cmd_result result = MI_CMD_DONE;
192 struct interp_procs *procs;
196 xasprintf (&mi_error_message,
197 "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
201 interp_to_use = interp_lookup (argv[0]);
202 if (interp_to_use == NULL)
204 xasprintf (&mi_error_message,
205 "mi_cmd_interpreter_exec: could not find interpreter \"%s\"",
210 if (!interp_exec_p (interp_to_use))
212 xasprintf (&mi_error_message,
213 "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
218 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
220 /* KRS: We shouldn't need this... Events should be installed and they should
221 just ALWAYS fire something out down the MI channel... */
222 mi_insert_notify_hooks ();
224 /* Now run the code... */
226 for (i = 1; i < argc; i++)
229 /* Do this in a cleaner way... We want to force execution to be
230 asynchronous for commands that run the target. */
231 if (target_can_async_p () && (strcmp (argv[0], "console") == 0))
233 int len = strlen (argv[i]);
234 buff = xmalloc (len + 2);
235 memcpy (buff, argv[i], len);
237 buff[len + 1] = '\0';
240 /* We had to set sync_execution = 0 for the mi (well really for Project
241 Builder's use of the mi - particularly so interrupting would work.
242 But for console commands to work, we need to initialize it to 1 -
243 since that is what the cli expects - before running the command,
244 and then set it back to 0 when we are done. */
246 if (interp_exec (interp_to_use, argv[i]) < 0)
248 mi_error_last_message ();
249 result = MI_CMD_ERROR;
253 do_exec_error_cleanups (ALL_CLEANUPS);
257 mi_remove_notify_hooks ();
259 /* Okay, now let's see if the command set the inferior going...
260 Tricky point - have to do this AFTER resetting the interpreter, since
261 changing the interpreter will clear out all the continuations for
262 that interpreter... */
264 if (target_can_async_p () && target_executing)
266 fputs_unfiltered ("^running\n", raw_stdout);
267 add_continuation (mi_interpreter_exec_continuation, NULL);
274 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
275 * async-notify ("=") MI messages while running commands in another interpreter
276 * using mi_interpreter_exec. The canonical use for this is to allow access to
277 * the gdb CLI interpreter from within the MI, while still producing MI style output
278 * when actions in the CLI command change gdb's state.
282 mi_insert_notify_hooks (void)
284 query_hook = mi_interp_query_hook;
288 mi_remove_notify_hooks ()
294 mi_interp_query_hook (const char *ctlstr, va_list ap)
300 mi_interp_read_one_line_hook (char *prompt, int repeat, char *anno)
302 static char buff[256];
303 printf_unfiltered ("=read-one-line,prompt=\"%s\"\n", prompt);
304 gdb_flush (gdb_stdout);
305 (void) fgets (buff, sizeof (buff), stdin);
306 buff[(strlen (buff) - 1)] = 0;
311 output_control_change_notification (char *notification)
313 printf_unfiltered ("^");
314 printf_unfiltered ("%s\n", notification);
315 gdb_flush (gdb_stdout);
319 mi_execute_command_wrapper (char *cmd)
321 mi_execute_command (cmd, stdin == instream);
325 mi1_command_loop (void)
331 mi2_command_loop (void)
337 mi_command_loop (int mi_version)
340 /* HACK: Force stdout/stderr to point at the console. This avoids
341 any potential side effects caused by legacy code that is still
342 using the TUI / fputs_unfiltered_hook */
343 raw_stdout = stdio_fileopen (stdout);
344 /* Route normal output through the MIx */
345 gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
346 /* Route error and log output through the MI */
347 gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
348 gdb_stdlog = gdb_stderr;
349 /* Route target output through the MI. */
350 gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
351 /* HACK: Poke the ui_out table directly. Should we be creating a
352 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
353 uiout = mi_out_new (mi_version);
354 /* HACK: Override any other interpreter hooks. We need to create a
355 real event table and pass in that. */
357 /* command_loop_hook = 0; */
358 print_frame_info_listing_hook = 0;
361 create_breakpoint_hook = 0;
362 delete_breakpoint_hook = 0;
363 modify_breakpoint_hook = 0;
364 interactive_hook = 0;
365 registers_changed_hook = 0;
366 readline_begin_hook = 0;
368 readline_end_hook = 0;
369 register_changed_hook = 0;
370 memory_changed_hook = 0;
372 target_wait_hook = 0;
373 call_command_hook = 0;
375 error_begin_hook = 0;
376 show_load_progress = mi_load_progress;
378 /* Turn off 8 bit strings in quoted output. Any character with the
379 high bit set is printed using C's octal format. */
380 sevenbit_strings = 1;
381 /* Tell the world that we're alive */
382 fputs_unfiltered ("(gdb) \n", raw_stdout);
383 gdb_flush (raw_stdout);
385 simplified_command_loop (mi_input, mi_execute_command);
393 return gdb_readline (NULL);
397 _initialize_mi_interp (void)
399 static const struct interp_procs procs =
401 mi_interpreter_init, /* init_proc */
402 mi_interpreter_resume, /* resume_proc */
403 mi_interpreter_suspend, /* suspend_proc */
404 mi_interpreter_exec, /* exec_proc */
405 mi_interpreter_prompt_p /* prompt_proc_p */
408 /* Create MI1 interpreter */
409 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
411 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (3), &procs));