Switch the license of all .c files to GPLv3.
[binutils-gdb.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdb_string.h"
22 #include "interps.h"
23 #include "event-top.h"
24 #include "event-loop.h"
25 #include "inferior.h"
26 #include "ui-out.h"
27 #include "top.h"
28 #include "exceptions.h"
29 #include "mi-main.h"
30 #include "mi-cmds.h"
31 #include "mi-out.h"
32 #include "mi-console.h"
33
34 struct mi_interp
35 {
36 /* MI's output channels */
37 struct ui_file *out;
38 struct ui_file *err;
39 struct ui_file *log;
40 struct ui_file *targ;
41 struct ui_file *event_channel;
42
43 /* This is the interpreter for the mi... */
44 struct interp *mi2_interp;
45 struct interp *mi1_interp;
46 struct interp *mi_interp;
47 };
48
49 /* These are the interpreter setup, etc. functions for the MI interpreter */
50 static void mi_execute_command_wrapper (char *cmd);
51 static void mi_command_loop (int mi_version);
52
53 /* These are hooks that we put in place while doing interpreter_exec
54 so we can report interesting things that happened "behind the mi's
55 back" in this command */
56 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
57 ATTR_FORMAT (printf, 1, 0);
58
59 static void mi3_command_loop (void);
60 static void mi2_command_loop (void);
61 static void mi1_command_loop (void);
62
63 static void mi_insert_notify_hooks (void);
64 static void mi_remove_notify_hooks (void);
65
66 static void *
67 mi_interpreter_init (void)
68 {
69 struct mi_interp *mi = XMALLOC (struct mi_interp);
70
71 /* HACK: We need to force stdout/stderr to point at the console. This avoids
72 any potential side effects caused by legacy code that is still
73 using the TUI / fputs_unfiltered_hook. So we set up output channels for
74 this now, and swap them in when we are run. */
75
76 raw_stdout = stdio_fileopen (stdout);
77
78 /* Create MI channels */
79 mi->out = mi_console_file_new (raw_stdout, "~", '"');
80 mi->err = mi_console_file_new (raw_stdout, "&", '"');
81 mi->log = mi->err;
82 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
83 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
84
85 return mi;
86 }
87
88 static int
89 mi_interpreter_resume (void *data)
90 {
91 struct mi_interp *mi = data;
92 /* As per hack note in mi_interpreter_init, swap in the output channels... */
93
94 gdb_setup_readline ();
95
96 /* These overwrite some of the initialization done in
97 _intialize_event_loop. */
98 call_readline = gdb_readline2;
99 input_handler = mi_execute_command_wrapper;
100 add_file_handler (input_fd, stdin_event_handler, 0);
101 async_command_editing_p = 0;
102 /* FIXME: This is a total hack for now. PB's use of the MI
103 implicitly relies on a bug in the async support which allows
104 asynchronous commands to leak through the commmand loop. The bug
105 involves (but is not limited to) the fact that sync_execution was
106 erroneously initialized to 0. Duplicate by initializing it thus
107 here... */
108 sync_execution = 0;
109
110 gdb_stdout = mi->out;
111 /* Route error and log output through the MI */
112 gdb_stderr = mi->err;
113 gdb_stdlog = mi->log;
114 /* Route target output through the MI. */
115 gdb_stdtarg = mi->targ;
116 /* Route target error through the MI as well. */
117 gdb_stdtargerr = mi->targ;
118
119 /* Replace all the hooks that we know about. There really needs to
120 be a better way of doing this... */
121 clear_interpreter_hooks ();
122
123 deprecated_show_load_progress = mi_load_progress;
124
125 /* If we're _the_ interpreter, take control. */
126 if (current_interp_named_p (INTERP_MI1))
127 deprecated_command_loop_hook = mi1_command_loop;
128 else if (current_interp_named_p (INTERP_MI2))
129 deprecated_command_loop_hook = mi2_command_loop;
130 else if (current_interp_named_p (INTERP_MI3))
131 deprecated_command_loop_hook = mi3_command_loop;
132 else
133 deprecated_command_loop_hook = mi2_command_loop;
134
135 return 1;
136 }
137
138 static int
139 mi_interpreter_suspend (void *data)
140 {
141 gdb_disable_readline ();
142 return 1;
143 }
144
145 static struct gdb_exception
146 mi_interpreter_exec (void *data, const char *command)
147 {
148 static struct gdb_exception ok;
149 char *tmp = alloca (strlen (command) + 1);
150 strcpy (tmp, command);
151 mi_execute_command_wrapper (tmp);
152 return exception_none;
153 }
154
155 /* Never display the default gdb prompt in mi case. */
156 static int
157 mi_interpreter_prompt_p (void *data)
158 {
159 return 0;
160 }
161
162 static void
163 mi_interpreter_exec_continuation (struct continuation_arg *arg)
164 {
165 bpstat_do_actions (&stop_bpstat);
166 if (!target_executing)
167 {
168 fputs_unfiltered ("*stopped", raw_stdout);
169 mi_out_put (uiout, raw_stdout);
170 fputs_unfiltered ("\n", raw_stdout);
171 fputs_unfiltered ("(gdb) \n", raw_stdout);
172 gdb_flush (raw_stdout);
173 do_exec_cleanups (ALL_CLEANUPS);
174 }
175 else if (target_can_async_p ())
176 {
177 add_continuation (mi_interpreter_exec_continuation, NULL);
178 }
179 }
180
181 enum mi_cmd_result
182 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
183 {
184 struct interp *interp_to_use;
185 enum mi_cmd_result result = MI_CMD_DONE;
186 int i;
187 struct interp_procs *procs;
188
189 if (argc < 2)
190 {
191 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
192 return MI_CMD_ERROR;
193 }
194
195 interp_to_use = interp_lookup (argv[0]);
196 if (interp_to_use == NULL)
197 {
198 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
199 return MI_CMD_ERROR;
200 }
201
202 if (!interp_exec_p (interp_to_use))
203 {
204 mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
205 argv[0]);
206 return MI_CMD_ERROR;
207 }
208
209 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
210 if it has any. */
211 /* KRS: We shouldn't need this... Events should be installed and they should
212 just ALWAYS fire something out down the MI channel... */
213 mi_insert_notify_hooks ();
214
215 /* Now run the code... */
216
217 for (i = 1; i < argc; i++)
218 {
219 /* We had to set sync_execution = 0 for the mi (well really for Project
220 Builder's use of the mi - particularly so interrupting would work.
221 But for console commands to work, we need to initialize it to 1 -
222 since that is what the cli expects - before running the command,
223 and then set it back to 0 when we are done. */
224 sync_execution = 1;
225 {
226 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
227 if (e.reason < 0)
228 {
229 mi_error_message = xstrdup (e.message);
230 result = MI_CMD_ERROR;
231 break;
232 }
233 }
234 do_exec_error_cleanups (ALL_CLEANUPS);
235 sync_execution = 0;
236 }
237
238 mi_remove_notify_hooks ();
239
240 /* Okay, now let's see if the command set the inferior going...
241 Tricky point - have to do this AFTER resetting the interpreter, since
242 changing the interpreter will clear out all the continuations for
243 that interpreter... */
244
245 if (target_can_async_p () && target_executing)
246 {
247 fputs_unfiltered ("^running\n", raw_stdout);
248 add_continuation (mi_interpreter_exec_continuation, NULL);
249 }
250
251 return result;
252 }
253
254 /*
255 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
256 * async-notify ("=") MI messages while running commands in another interpreter
257 * using mi_interpreter_exec. The canonical use for this is to allow access to
258 * the gdb CLI interpreter from within the MI, while still producing MI style output
259 * when actions in the CLI command change gdb's state.
260 */
261
262 static void
263 mi_insert_notify_hooks (void)
264 {
265 deprecated_query_hook = mi_interp_query_hook;
266 }
267
268 static void
269 mi_remove_notify_hooks (void)
270 {
271 deprecated_query_hook = NULL;
272 }
273
274 static int
275 mi_interp_query_hook (const char *ctlstr, va_list ap)
276 {
277 return 1;
278 }
279
280 static void
281 mi_execute_command_wrapper (char *cmd)
282 {
283 mi_execute_command (cmd, stdin == instream);
284 }
285
286 static void
287 mi1_command_loop (void)
288 {
289 mi_command_loop (1);
290 }
291
292 static void
293 mi2_command_loop (void)
294 {
295 mi_command_loop (2);
296 }
297
298 static void
299 mi3_command_loop (void)
300 {
301 mi_command_loop (3);
302 }
303
304 static void
305 mi_command_loop (int mi_version)
306 {
307 #if 0
308 /* HACK: Force stdout/stderr to point at the console. This avoids
309 any potential side effects caused by legacy code that is still
310 using the TUI / fputs_unfiltered_hook */
311 raw_stdout = stdio_fileopen (stdout);
312 /* Route normal output through the MIx */
313 gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
314 /* Route error and log output through the MI */
315 gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
316 gdb_stdlog = gdb_stderr;
317 /* Route target output through the MI. */
318 gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
319 /* HACK: Poke the ui_out table directly. Should we be creating a
320 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
321 uiout = mi_out_new (mi_version);
322 /* HACK: Override any other interpreter hooks. We need to create a
323 real event table and pass in that. */
324 deprecated_init_ui_hook = 0;
325 /* deprecated_command_loop_hook = 0; */
326 deprecated_print_frame_info_listing_hook = 0;
327 deprecated_query_hook = 0;
328 deprecated_warning_hook = 0;
329 deprecated_create_breakpoint_hook = 0;
330 deprecated_delete_breakpoint_hook = 0;
331 deprecated_modify_breakpoint_hook = 0;
332 deprecated_interactive_hook = 0;
333 deprecated_readline_begin_hook = 0;
334 deprecated_readline_hook = 0;
335 deprecated_readline_end_hook = 0;
336 deprecated_register_changed_hook = 0;
337 deprecated_memory_changed_hook = 0;
338 deprecated_context_hook = 0;
339 deprecated_target_wait_hook = 0;
340 deprecated_call_command_hook = 0;
341 deprecated_error_hook = 0;
342 deprecated_error_begin_hook = 0;
343 deprecated_show_load_progress = mi_load_progress;
344 #endif
345 /* Turn off 8 bit strings in quoted output. Any character with the
346 high bit set is printed using C's octal format. */
347 sevenbit_strings = 1;
348 /* Tell the world that we're alive */
349 fputs_unfiltered ("(gdb) \n", raw_stdout);
350 gdb_flush (raw_stdout);
351 start_event_loop ();
352 }
353
354 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
355
356 void
357 _initialize_mi_interp (void)
358 {
359 static const struct interp_procs procs =
360 {
361 mi_interpreter_init, /* init_proc */
362 mi_interpreter_resume, /* resume_proc */
363 mi_interpreter_suspend, /* suspend_proc */
364 mi_interpreter_exec, /* exec_proc */
365 mi_interpreter_prompt_p /* prompt_proc_p */
366 };
367
368 /* The various interpreter levels. */
369 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
370 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
371 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
372
373 /* "mi" selects the most recent released version. "mi2" was
374 released as part of GDB 6.0. */
375 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
376 }