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 mi3_command_loop (void);
64 static void mi2_command_loop (void);
65 static void mi1_command_loop (void);
67 static void mi_insert_notify_hooks (void);
68 static void mi_remove_notify_hooks (void);
71 mi_interpreter_init (void)
73 struct mi_interp
*mi
= XMALLOC (struct mi_interp
);
75 /* Why is this a part of the mi architecture? */
77 mi_setup_architecture_data ();
79 /* HACK: We need to force stdout/stderr to point at the console. This avoids
80 any potential side effects caused by legacy code that is still
81 using the TUI / fputs_unfiltered_hook. So we set up output channels for
82 this now, and swap them in when we are run. */
84 raw_stdout
= stdio_fileopen (stdout
);
86 /* Create MI channels */
87 mi
->out
= mi_console_file_new (raw_stdout
, "~", '"');
88 mi
->err
= mi_console_file_new (raw_stdout
, "&", '"');
90 mi
->targ
= mi_console_file_new (raw_stdout
, "@", '"');
91 mi
->event_channel
= mi_console_file_new (raw_stdout
, "=", 0);
97 mi_interpreter_resume (void *data
)
99 struct mi_interp
*mi
= data
;
100 /* As per hack note in mi_interpreter_init, swap in the output channels... */
102 gdb_setup_readline ();
106 /* These overwrite some of the initialization done in
107 _intialize_event_loop. */
108 call_readline
= gdb_readline2
;
109 input_handler
= mi_execute_command_wrapper
;
110 add_file_handler (input_fd
, stdin_event_handler
, 0);
111 async_command_editing_p
= 0;
112 /* FIXME: This is a total hack for now. PB's use of the MI implicitly
113 relies on a bug in the async support which allows asynchronous
114 commands to leak through the commmand loop. The bug involves
115 (but is not limited to) the fact that sync_execution was
116 erroneously initialized to 0. Duplicate by initializing it
121 gdb_stdout
= mi
->out
;
122 /* Route error and log output through the MI */
123 gdb_stderr
= mi
->err
;
124 gdb_stdlog
= mi
->log
;
125 /* Route target output through the MI. */
126 gdb_stdtarg
= mi
->targ
;
128 /* Replace all the hooks that we know about. There really needs to
129 be a better way of doing this... */
130 clear_interpreter_hooks ();
132 show_load_progress
= mi_load_progress
;
134 /* If we're _the_ interpreter, take control. */
135 if (current_interp_named_p (INTERP_MI1
))
136 command_loop_hook
= mi1_command_loop
;
137 else if (current_interp_named_p (INTERP_MI2
))
138 command_loop_hook
= mi2_command_loop
;
139 else if (current_interp_named_p (INTERP_MI3
))
140 command_loop_hook
= mi3_command_loop
;
142 command_loop_hook
= mi2_command_loop
;
148 mi_interpreter_suspend (void *data
)
150 gdb_disable_readline ();
155 mi_interpreter_exec (void *data
, const char *command
)
157 char *tmp
= alloca (strlen (command
) + 1);
158 strcpy (tmp
, command
);
159 mi_execute_command_wrapper (tmp
);
163 /* Never display the default gdb prompt in mi case. */
165 mi_interpreter_prompt_p (void *data
)
171 mi_interpreter_exec_continuation (struct continuation_arg
*arg
)
173 bpstat_do_actions (&stop_bpstat
);
174 if (!target_executing
)
176 fputs_unfiltered ("*stopped", raw_stdout
);
177 mi_out_put (uiout
, raw_stdout
);
178 fputs_unfiltered ("\n", raw_stdout
);
179 fputs_unfiltered ("(gdb) \n", raw_stdout
);
180 gdb_flush (raw_stdout
);
181 do_exec_cleanups (ALL_CLEANUPS
);
183 else if (target_can_async_p ())
185 add_continuation (mi_interpreter_exec_continuation
, NULL
);
190 mi_cmd_interpreter_exec (char *command
, char **argv
, int argc
)
192 struct interp
*interp_to_use
;
193 enum mi_cmd_result result
= MI_CMD_DONE
;
195 struct interp_procs
*procs
;
199 xasprintf (&mi_error_message
,
200 "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
204 interp_to_use
= interp_lookup (argv
[0]);
205 if (interp_to_use
== NULL
)
207 xasprintf (&mi_error_message
,
208 "mi_cmd_interpreter_exec: could not find interpreter \"%s\"",
213 if (!interp_exec_p (interp_to_use
))
215 xasprintf (&mi_error_message
,
216 "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
221 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
223 /* KRS: We shouldn't need this... Events should be installed and they should
224 just ALWAYS fire something out down the MI channel... */
225 mi_insert_notify_hooks ();
227 /* Now run the code... */
229 for (i
= 1; i
< argc
; i
++)
232 /* Do this in a cleaner way... We want to force execution to be
233 asynchronous for commands that run the target. */
234 if (target_can_async_p () && (strcmp (argv
[0], "console") == 0))
236 int len
= strlen (argv
[i
]);
237 buff
= xmalloc (len
+ 2);
238 memcpy (buff
, argv
[i
], len
);
240 buff
[len
+ 1] = '\0';
243 /* We had to set sync_execution = 0 for the mi (well really for Project
244 Builder's use of the mi - particularly so interrupting would work.
245 But for console commands to work, we need to initialize it to 1 -
246 since that is what the cli expects - before running the command,
247 and then set it back to 0 when we are done. */
249 if (interp_exec (interp_to_use
, argv
[i
]) < 0)
251 mi_error_last_message ();
252 result
= MI_CMD_ERROR
;
256 do_exec_error_cleanups (ALL_CLEANUPS
);
260 mi_remove_notify_hooks ();
262 /* Okay, now let's see if the command set the inferior going...
263 Tricky point - have to do this AFTER resetting the interpreter, since
264 changing the interpreter will clear out all the continuations for
265 that interpreter... */
267 if (target_can_async_p () && target_executing
)
269 fputs_unfiltered ("^running\n", raw_stdout
);
270 add_continuation (mi_interpreter_exec_continuation
, NULL
);
277 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
278 * async-notify ("=") MI messages while running commands in another interpreter
279 * using mi_interpreter_exec. The canonical use for this is to allow access to
280 * the gdb CLI interpreter from within the MI, while still producing MI style output
281 * when actions in the CLI command change gdb's state.
285 mi_insert_notify_hooks (void)
287 query_hook
= mi_interp_query_hook
;
291 mi_remove_notify_hooks (void)
297 mi_interp_query_hook (const char *ctlstr
, va_list ap
)
303 mi_interp_read_one_line_hook (char *prompt
, int repeat
, char *anno
)
305 static char buff
[256];
306 printf_unfiltered ("=read-one-line,prompt=\"%s\"\n", prompt
);
307 gdb_flush (gdb_stdout
);
308 (void) fgets (buff
, sizeof (buff
), stdin
);
309 buff
[(strlen (buff
) - 1)] = 0;
314 output_control_change_notification (char *notification
)
316 printf_unfiltered ("^");
317 printf_unfiltered ("%s\n", notification
);
318 gdb_flush (gdb_stdout
);
322 mi_execute_command_wrapper (char *cmd
)
324 mi_execute_command (cmd
, stdin
== instream
);
328 mi1_command_loop (void)
334 mi2_command_loop (void)
340 mi3_command_loop (void)
346 mi_command_loop (int mi_version
)
349 /* HACK: Force stdout/stderr to point at the console. This avoids
350 any potential side effects caused by legacy code that is still
351 using the TUI / fputs_unfiltered_hook */
352 raw_stdout
= stdio_fileopen (stdout
);
353 /* Route normal output through the MIx */
354 gdb_stdout
= mi_console_file_new (raw_stdout
, "~", '"');
355 /* Route error and log output through the MI */
356 gdb_stderr
= mi_console_file_new (raw_stdout
, "&", '"');
357 gdb_stdlog
= gdb_stderr
;
358 /* Route target output through the MI. */
359 gdb_stdtarg
= mi_console_file_new (raw_stdout
, "@", '"');
360 /* HACK: Poke the ui_out table directly. Should we be creating a
361 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
362 uiout
= mi_out_new (mi_version
);
363 /* HACK: Override any other interpreter hooks. We need to create a
364 real event table and pass in that. */
366 /* command_loop_hook = 0; */
367 print_frame_info_listing_hook
= 0;
370 create_breakpoint_hook
= 0;
371 delete_breakpoint_hook
= 0;
372 modify_breakpoint_hook
= 0;
373 interactive_hook
= 0;
374 registers_changed_hook
= 0;
375 readline_begin_hook
= 0;
377 readline_end_hook
= 0;
378 register_changed_hook
= 0;
379 memory_changed_hook
= 0;
381 target_wait_hook
= 0;
382 call_command_hook
= 0;
384 error_begin_hook
= 0;
385 show_load_progress
= mi_load_progress
;
387 /* Turn off 8 bit strings in quoted output. Any character with the
388 high bit set is printed using C's octal format. */
389 sevenbit_strings
= 1;
390 /* Tell the world that we're alive */
391 fputs_unfiltered ("(gdb) \n", raw_stdout
);
392 gdb_flush (raw_stdout
);
394 simplified_command_loop (mi_input
, mi_execute_command
);
402 return gdb_readline (NULL
);
405 extern initialize_file_ftype _initialize_mi_interp
; /* -Wmissing-prototypes */
408 _initialize_mi_interp (void)
410 static const struct interp_procs procs
=
412 mi_interpreter_init
, /* init_proc */
413 mi_interpreter_resume
, /* resume_proc */
414 mi_interpreter_suspend
, /* suspend_proc */
415 mi_interpreter_exec
, /* exec_proc */
416 mi_interpreter_prompt_p
/* prompt_proc_p */
419 /* The various interpreter levels. */
420 interp_add (interp_new (INTERP_MI1
, NULL
, mi_out_new (1), &procs
));
421 interp_add (interp_new (INTERP_MI2
, NULL
, mi_out_new (2), &procs
));
422 interp_add (interp_new (INTERP_MI3
, NULL
, mi_out_new (3), &procs
));
424 /* "mi" selects the most recent released version. "mi2" was
425 released as part of GDB 6.0. */
426 interp_add (interp_new (INTERP_MI
, NULL
, mi_out_new (2), &procs
));