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