1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
3 Copyright 2002, 2003, 2004, 2005 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"
30 #include "exceptions.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
);
55 /* These are hooks that we put in place while doing interpreter_exec
56 so we can report interesting things that happened "behind the mi's
57 back" in this command */
58 static int mi_interp_query_hook (const char *ctlstr
, va_list ap
)
59 ATTR_FORMAT (printf
, 1, 0);
61 static void mi3_command_loop (void);
62 static void mi2_command_loop (void);
63 static void mi1_command_loop (void);
65 static void mi_insert_notify_hooks (void);
66 static void mi_remove_notify_hooks (void);
69 mi_interpreter_init (void)
71 struct mi_interp
*mi
= XMALLOC (struct mi_interp
);
73 /* Why is this a part of the mi architecture? */
75 mi_setup_architecture_data ();
77 /* HACK: We need to force stdout/stderr to point at the console. This avoids
78 any potential side effects caused by legacy code that is still
79 using the TUI / fputs_unfiltered_hook. So we set up output channels for
80 this now, and swap them in when we are run. */
82 raw_stdout
= stdio_fileopen (stdout
);
84 /* Create MI channels */
85 mi
->out
= mi_console_file_new (raw_stdout
, "~", '"');
86 mi
->err
= mi_console_file_new (raw_stdout
, "&", '"');
88 mi
->targ
= mi_console_file_new (raw_stdout
, "@", '"');
89 mi
->event_channel
= mi_console_file_new (raw_stdout
, "=", 0);
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
;
123 /* Replace all the hooks that we know about. There really needs to
124 be a better way of doing this... */
125 clear_interpreter_hooks ();
127 deprecated_show_load_progress
= mi_load_progress
;
129 /* If we're _the_ interpreter, take control. */
130 if (current_interp_named_p (INTERP_MI1
))
131 deprecated_command_loop_hook
= mi1_command_loop
;
132 else if (current_interp_named_p (INTERP_MI2
))
133 deprecated_command_loop_hook
= mi2_command_loop
;
134 else if (current_interp_named_p (INTERP_MI3
))
135 deprecated_command_loop_hook
= mi3_command_loop
;
137 deprecated_command_loop_hook
= mi2_command_loop
;
143 mi_interpreter_suspend (void *data
)
145 gdb_disable_readline ();
149 static struct gdb_exception
150 mi_interpreter_exec (void *data
, const char *command
)
152 static struct gdb_exception ok
;
153 char *tmp
= alloca (strlen (command
) + 1);
154 strcpy (tmp
, command
);
155 mi_execute_command_wrapper (tmp
);
156 return exception_none
;
159 /* Never display the default gdb prompt in mi case. */
161 mi_interpreter_prompt_p (void *data
)
167 mi_interpreter_exec_continuation (struct continuation_arg
*arg
)
169 bpstat_do_actions (&stop_bpstat
);
170 if (!target_executing
)
172 fputs_unfiltered ("*stopped", raw_stdout
);
173 mi_out_put (uiout
, raw_stdout
);
174 fputs_unfiltered ("\n", raw_stdout
);
175 fputs_unfiltered ("(gdb) \n", raw_stdout
);
176 gdb_flush (raw_stdout
);
177 do_exec_cleanups (ALL_CLEANUPS
);
179 else if (target_can_async_p ())
181 add_continuation (mi_interpreter_exec_continuation
, NULL
);
186 mi_cmd_interpreter_exec (char *command
, char **argv
, int argc
)
188 struct interp
*interp_to_use
;
189 enum mi_cmd_result result
= MI_CMD_DONE
;
191 struct interp_procs
*procs
;
195 mi_error_message
= xstrprintf ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
199 interp_to_use
= interp_lookup (argv
[0]);
200 if (interp_to_use
== NULL
)
202 mi_error_message
= xstrprintf ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv
[0]);
206 if (!interp_exec_p (interp_to_use
))
208 mi_error_message
= xstrprintf ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
213 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
215 /* KRS: We shouldn't need this... Events should be installed and they should
216 just ALWAYS fire something out down the MI channel... */
217 mi_insert_notify_hooks ();
219 /* Now run the code... */
221 for (i
= 1; i
< argc
; i
++)
224 /* Do this in a cleaner way... We want to force execution to be
225 asynchronous for commands that run the target. */
226 if (target_can_async_p () && (strcmp (argv
[0], "console") == 0))
228 int len
= strlen (argv
[i
]);
229 buff
= xmalloc (len
+ 2);
230 memcpy (buff
, argv
[i
], len
);
232 buff
[len
+ 1] = '\0';
235 /* We had to set sync_execution = 0 for the mi (well really for Project
236 Builder's use of the mi - particularly so interrupting would work.
237 But for console commands to work, we need to initialize it to 1 -
238 since that is what the cli expects - before running the command,
239 and then set it back to 0 when we are done. */
242 struct gdb_exception e
= interp_exec (interp_to_use
, argv
[i
]);
245 mi_error_message
= xstrdup (e
.message
);
246 result
= MI_CMD_ERROR
;
251 do_exec_error_cleanups (ALL_CLEANUPS
);
255 mi_remove_notify_hooks ();
257 /* Okay, now let's see if the command set the inferior going...
258 Tricky point - have to do this AFTER resetting the interpreter, since
259 changing the interpreter will clear out all the continuations for
260 that interpreter... */
262 if (target_can_async_p () && target_executing
)
264 fputs_unfiltered ("^running\n", raw_stdout
);
265 add_continuation (mi_interpreter_exec_continuation
, NULL
);
272 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
273 * async-notify ("=") MI messages while running commands in another interpreter
274 * using mi_interpreter_exec. The canonical use for this is to allow access to
275 * the gdb CLI interpreter from within the MI, while still producing MI style output
276 * when actions in the CLI command change gdb's state.
280 mi_insert_notify_hooks (void)
282 deprecated_query_hook
= mi_interp_query_hook
;
286 mi_remove_notify_hooks (void)
288 deprecated_query_hook
= NULL
;
292 mi_interp_query_hook (const char *ctlstr
, va_list ap
)
298 mi_execute_command_wrapper (char *cmd
)
300 mi_execute_command (cmd
, stdin
== instream
);
304 mi1_command_loop (void)
310 mi2_command_loop (void)
316 mi3_command_loop (void)
322 mi_command_loop (int mi_version
)
325 /* HACK: Force stdout/stderr to point at the console. This avoids
326 any potential side effects caused by legacy code that is still
327 using the TUI / fputs_unfiltered_hook */
328 raw_stdout
= stdio_fileopen (stdout
);
329 /* Route normal output through the MIx */
330 gdb_stdout
= mi_console_file_new (raw_stdout
, "~", '"');
331 /* Route error and log output through the MI */
332 gdb_stderr
= mi_console_file_new (raw_stdout
, "&", '"');
333 gdb_stdlog
= gdb_stderr
;
334 /* Route target output through the MI. */
335 gdb_stdtarg
= mi_console_file_new (raw_stdout
, "@", '"');
336 /* HACK: Poke the ui_out table directly. Should we be creating a
337 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
338 uiout
= mi_out_new (mi_version
);
339 /* HACK: Override any other interpreter hooks. We need to create a
340 real event table and pass in that. */
341 deprecated_init_ui_hook
= 0;
342 /* deprecated_command_loop_hook = 0; */
343 deprecated_print_frame_info_listing_hook
= 0;
344 deprecated_query_hook
= 0;
345 deprecated_warning_hook
= 0;
346 deprecated_create_breakpoint_hook
= 0;
347 deprecated_delete_breakpoint_hook
= 0;
348 deprecated_modify_breakpoint_hook
= 0;
349 deprecated_interactive_hook
= 0;
350 deprecated_registers_changed_hook
= 0;
351 deprecated_readline_begin_hook
= 0;
352 deprecated_readline_hook
= 0;
353 deprecated_readline_end_hook
= 0;
354 deprecated_register_changed_hook
= 0;
355 deprecated_memory_changed_hook
= 0;
356 deprecated_context_hook
= 0;
357 deprecated_target_wait_hook
= 0;
358 deprecated_call_command_hook
= 0;
359 deprecated_error_hook
= 0;
360 deprecated_error_begin_hook
= 0;
361 deprecated_show_load_progress
= mi_load_progress
;
363 /* Turn off 8 bit strings in quoted output. Any character with the
364 high bit set is printed using C's octal format. */
365 sevenbit_strings
= 1;
366 /* Tell the world that we're alive */
367 fputs_unfiltered ("(gdb) \n", raw_stdout
);
368 gdb_flush (raw_stdout
);
372 extern initialize_file_ftype _initialize_mi_interp
; /* -Wmissing-prototypes */
375 _initialize_mi_interp (void)
377 static const struct interp_procs procs
=
379 mi_interpreter_init
, /* init_proc */
380 mi_interpreter_resume
, /* resume_proc */
381 mi_interpreter_suspend
, /* suspend_proc */
382 mi_interpreter_exec
, /* exec_proc */
383 mi_interpreter_prompt_p
/* prompt_proc_p */
386 /* The various interpreter levels. */
387 interp_add (interp_new (INTERP_MI1
, NULL
, mi_out_new (1), &procs
));
388 interp_add (interp_new (INTERP_MI2
, NULL
, mi_out_new (2), &procs
));
389 interp_add (interp_new (INTERP_MI3
, NULL
, mi_out_new (3), &procs
));
391 /* "mi" selects the most recent released version. "mi2" was
392 released as part of GDB 6.0. */
393 interp_add (interp_new (INTERP_MI
, NULL
, mi_out_new (2), &procs
));