]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-interp.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / mi / mi-interp.c
CommitLineData
4a8f6654
AC
1/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
6aba47ca 3 Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4a8f6654
AC
4
5 This file is part of GDB.
6
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.
11
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.
16
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
1caae165
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
4a8f6654
AC
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "interps.h"
25#include "event-top.h"
26#include "event-loop.h"
27#include "inferior.h"
28#include "ui-out.h"
29#include "top.h"
c1043fc2 30#include "exceptions.h"
4a8f6654
AC
31#include "mi-main.h"
32#include "mi-cmds.h"
33#include "mi-out.h"
34#include "mi-console.h"
35
36struct mi_interp
37{
38 /* MI's output channels */
39 struct ui_file *out;
40 struct ui_file *err;
41 struct ui_file *log;
42 struct ui_file *targ;
43 struct ui_file *event_channel;
44
45 /* This is the interpreter for the mi... */
46 struct interp *mi2_interp;
47 struct interp *mi1_interp;
48 struct interp *mi_interp;
49};
50
51/* These are the interpreter setup, etc. functions for the MI interpreter */
52static void mi_execute_command_wrapper (char *cmd);
53static void mi_command_loop (int mi_version);
4a8f6654
AC
54
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 */
bee0189a
DJ
58static int mi_interp_query_hook (const char *ctlstr, va_list ap)
59 ATTR_FORMAT (printf, 1, 0);
4a8f6654 60
f786f615 61static void mi3_command_loop (void);
4a8f6654
AC
62static void mi2_command_loop (void);
63static void mi1_command_loop (void);
64
65static void mi_insert_notify_hooks (void);
66static void mi_remove_notify_hooks (void);
67
68static void *
69mi_interpreter_init (void)
70{
71 struct mi_interp *mi = XMALLOC (struct mi_interp);
72
73 /* Why is this a part of the mi architecture? */
74
75 mi_setup_architecture_data ();
76
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. */
81
82 raw_stdout = stdio_fileopen (stdout);
83
84 /* Create MI channels */
85 mi->out = mi_console_file_new (raw_stdout, "~", '"');
86 mi->err = mi_console_file_new (raw_stdout, "&", '"');
87 mi->log = mi->err;
88 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
89 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
90
91 return mi;
92}
93
94static int
95mi_interpreter_resume (void *data)
96{
97 struct mi_interp *mi = data;
98 /* As per hack note in mi_interpreter_init, swap in the output channels... */
99
100 gdb_setup_readline ();
101
362646f5
AC
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
113 here... */
114 sync_execution = 0;
4a8f6654
AC
115
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;
1f20321b
FR
122 /* Route target error through the MI as well. */
123 gdb_stdtargerr = mi->targ;
4a8f6654
AC
124
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 ();
128
9a4105ab 129 deprecated_show_load_progress = mi_load_progress;
4a8f6654
AC
130
131 /* If we're _the_ interpreter, take control. */
132 if (current_interp_named_p (INTERP_MI1))
9a4105ab 133 deprecated_command_loop_hook = mi1_command_loop;
f786f615 134 else if (current_interp_named_p (INTERP_MI2))
9a4105ab 135 deprecated_command_loop_hook = mi2_command_loop;
f786f615 136 else if (current_interp_named_p (INTERP_MI3))
9a4105ab 137 deprecated_command_loop_hook = mi3_command_loop;
4a8f6654 138 else
9a4105ab 139 deprecated_command_loop_hook = mi2_command_loop;
4a8f6654
AC
140
141 return 1;
142}
143
144static int
145mi_interpreter_suspend (void *data)
146{
147 gdb_disable_readline ();
148 return 1;
149}
150
71fff37b 151static struct gdb_exception
4a8f6654
AC
152mi_interpreter_exec (void *data, const char *command)
153{
71fff37b 154 static struct gdb_exception ok;
4a8f6654
AC
155 char *tmp = alloca (strlen (command) + 1);
156 strcpy (tmp, command);
157 mi_execute_command_wrapper (tmp);
c1043fc2 158 return exception_none;
4a8f6654
AC
159}
160
161/* Never display the default gdb prompt in mi case. */
162static int
163mi_interpreter_prompt_p (void *data)
164{
165 return 0;
166}
167
168static void
169mi_interpreter_exec_continuation (struct continuation_arg *arg)
170{
171 bpstat_do_actions (&stop_bpstat);
172 if (!target_executing)
173 {
174 fputs_unfiltered ("*stopped", raw_stdout);
175 mi_out_put (uiout, raw_stdout);
176 fputs_unfiltered ("\n", raw_stdout);
177 fputs_unfiltered ("(gdb) \n", raw_stdout);
178 gdb_flush (raw_stdout);
179 do_exec_cleanups (ALL_CLEANUPS);
180 }
181 else if (target_can_async_p ())
182 {
183 add_continuation (mi_interpreter_exec_continuation, NULL);
184 }
185}
186
187enum mi_cmd_result
188mi_cmd_interpreter_exec (char *command, char **argv, int argc)
189{
190 struct interp *interp_to_use;
191 enum mi_cmd_result result = MI_CMD_DONE;
192 int i;
193 struct interp_procs *procs;
194
195 if (argc < 2)
196 {
c6902d46 197 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
4a8f6654
AC
198 return MI_CMD_ERROR;
199 }
200
201 interp_to_use = interp_lookup (argv[0]);
202 if (interp_to_use == NULL)
203 {
c6902d46 204 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
4a8f6654
AC
205 return MI_CMD_ERROR;
206 }
207
208 if (!interp_exec_p (interp_to_use))
209 {
c6902d46
AC
210 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
211 argv[0]);
4a8f6654
AC
212 return MI_CMD_ERROR;
213 }
214
215 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
216 if it has any. */
217 /* KRS: We shouldn't need this... Events should be installed and they should
218 just ALWAYS fire something out down the MI channel... */
219 mi_insert_notify_hooks ();
220
221 /* Now run the code... */
222
223 for (i = 1; i < argc; i++)
224 {
225 char *buff = NULL;
226 /* Do this in a cleaner way... We want to force execution to be
227 asynchronous for commands that run the target. */
228 if (target_can_async_p () && (strcmp (argv[0], "console") == 0))
229 {
230 int len = strlen (argv[i]);
231 buff = xmalloc (len + 2);
232 memcpy (buff, argv[i], len);
233 buff[len] = '&';
234 buff[len + 1] = '\0';
235 }
236
237 /* We had to set sync_execution = 0 for the mi (well really for Project
238 Builder's use of the mi - particularly so interrupting would work.
239 But for console commands to work, we need to initialize it to 1 -
240 since that is what the cli expects - before running the command,
241 and then set it back to 0 when we are done. */
242 sync_execution = 1;
c1043fc2 243 {
71fff37b 244 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
c1043fc2
AC
245 if (e.reason < 0)
246 {
6b1b7650 247 mi_error_message = xstrdup (e.message);
c1043fc2
AC
248 result = MI_CMD_ERROR;
249 break;
250 }
251 }
4a8f6654
AC
252 xfree (buff);
253 do_exec_error_cleanups (ALL_CLEANUPS);
254 sync_execution = 0;
255 }
256
257 mi_remove_notify_hooks ();
258
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... */
263
264 if (target_can_async_p () && target_executing)
265 {
266 fputs_unfiltered ("^running\n", raw_stdout);
267 add_continuation (mi_interpreter_exec_continuation, NULL);
268 }
269
270 return result;
271}
272
273/*
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.
279*/
280
281static void
282mi_insert_notify_hooks (void)
283{
9a4105ab 284 deprecated_query_hook = mi_interp_query_hook;
4a8f6654
AC
285}
286
287static void
11308a41 288mi_remove_notify_hooks (void)
4a8f6654 289{
9a4105ab 290 deprecated_query_hook = NULL;
4a8f6654
AC
291}
292
293static int
294mi_interp_query_hook (const char *ctlstr, va_list ap)
295{
296 return 1;
297}
298
4a8f6654
AC
299static void
300mi_execute_command_wrapper (char *cmd)
301{
302 mi_execute_command (cmd, stdin == instream);
303}
304
305static void
306mi1_command_loop (void)
307{
308 mi_command_loop (1);
309}
310
311static void
312mi2_command_loop (void)
313{
314 mi_command_loop (2);
315}
316
f786f615
AC
317static void
318mi3_command_loop (void)
319{
320 mi_command_loop (3);
321}
322
4a8f6654
AC
323static void
324mi_command_loop (int mi_version)
325{
326#if 0
327 /* HACK: Force stdout/stderr to point at the console. This avoids
328 any potential side effects caused by legacy code that is still
329 using the TUI / fputs_unfiltered_hook */
330 raw_stdout = stdio_fileopen (stdout);
331 /* Route normal output through the MIx */
332 gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
333 /* Route error and log output through the MI */
334 gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
335 gdb_stdlog = gdb_stderr;
336 /* Route target output through the MI. */
337 gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
338 /* HACK: Poke the ui_out table directly. Should we be creating a
339 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
340 uiout = mi_out_new (mi_version);
341 /* HACK: Override any other interpreter hooks. We need to create a
342 real event table and pass in that. */
9a4105ab
AC
343 deprecated_init_ui_hook = 0;
344 /* deprecated_command_loop_hook = 0; */
345 deprecated_print_frame_info_listing_hook = 0;
346 deprecated_query_hook = 0;
347 deprecated_warning_hook = 0;
348 deprecated_create_breakpoint_hook = 0;
349 deprecated_delete_breakpoint_hook = 0;
350 deprecated_modify_breakpoint_hook = 0;
351 deprecated_interactive_hook = 0;
352 deprecated_registers_changed_hook = 0;
353 deprecated_readline_begin_hook = 0;
354 deprecated_readline_hook = 0;
355 deprecated_readline_end_hook = 0;
356 deprecated_register_changed_hook = 0;
357 deprecated_memory_changed_hook = 0;
358 deprecated_context_hook = 0;
359 deprecated_target_wait_hook = 0;
360 deprecated_call_command_hook = 0;
361 deprecated_error_hook = 0;
362 deprecated_error_begin_hook = 0;
363 deprecated_show_load_progress = mi_load_progress;
4a8f6654
AC
364#endif
365 /* Turn off 8 bit strings in quoted output. Any character with the
366 high bit set is printed using C's octal format. */
367 sevenbit_strings = 1;
368 /* Tell the world that we're alive */
369 fputs_unfiltered ("(gdb) \n", raw_stdout);
370 gdb_flush (raw_stdout);
362646f5 371 start_event_loop ();
4a8f6654
AC
372}
373
b9362cc7
AC
374extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
375
4a8f6654
AC
376void
377_initialize_mi_interp (void)
378{
379 static const struct interp_procs procs =
380 {
381 mi_interpreter_init, /* init_proc */
382 mi_interpreter_resume, /* resume_proc */
383 mi_interpreter_suspend, /* suspend_proc */
384 mi_interpreter_exec, /* exec_proc */
385 mi_interpreter_prompt_p /* prompt_proc_p */
386 };
387
2fcf52f0 388 /* The various interpreter levels. */
4a8f6654 389 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
2fcf52f0
AC
390 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
391 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
4a8f6654 392
2fcf52f0
AC
393 /* "mi" selects the most recent released version. "mi2" was
394 released as part of GDB 6.0. */
395 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
4a8f6654 396}