daily update
[deliverable/binutils-gdb.git] / gdb / mi / mi-interp.c
CommitLineData
4a8f6654
AC
1/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
9b254dd1
DJ
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
4a8f6654
AC
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
4a8f6654
AC
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4a8f6654
AC
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"
c1043fc2 29#include "exceptions.h"
4a8f6654
AC
30#include "mi-main.h"
31#include "mi-cmds.h"
32#include "mi-out.h"
33#include "mi-console.h"
683f2885
VP
34#include "observer.h"
35#include "gdbthread.h"
4a8f6654
AC
36
37struct mi_interp
38{
39 /* MI's output channels */
40 struct ui_file *out;
41 struct ui_file *err;
42 struct ui_file *log;
43 struct ui_file *targ;
44 struct ui_file *event_channel;
45
46 /* This is the interpreter for the mi... */
47 struct interp *mi2_interp;
48 struct interp *mi1_interp;
49 struct interp *mi_interp;
50};
51
52/* These are the interpreter setup, etc. functions for the MI interpreter */
53static void mi_execute_command_wrapper (char *cmd);
54static void mi_command_loop (int mi_version);
4a8f6654
AC
55
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 */
bee0189a
DJ
59static int mi_interp_query_hook (const char *ctlstr, va_list ap)
60 ATTR_FORMAT (printf, 1, 0);
4a8f6654 61
f786f615 62static void mi3_command_loop (void);
4a8f6654
AC
63static void mi2_command_loop (void);
64static void mi1_command_loop (void);
65
66static void mi_insert_notify_hooks (void);
67static void mi_remove_notify_hooks (void);
68
683f2885 69static void mi_new_thread (struct thread_info *t);
063bfe2e 70static void mi_thread_exit (struct thread_info *t);
683f2885 71
4a8f6654 72static void *
683f2885 73mi_interpreter_init (int top_level)
4a8f6654
AC
74{
75 struct mi_interp *mi = XMALLOC (struct mi_interp);
76
4a8f6654
AC
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
683f2885 91 if (top_level)
063bfe2e
VP
92 {
93 observer_attach_new_thread (mi_new_thread);
94 observer_attach_thread_exit (mi_thread_exit);
95 }
683f2885 96
4a8f6654
AC
97 return mi;
98}
99
100static int
101mi_interpreter_resume (void *data)
102{
103 struct mi_interp *mi = data;
104 /* As per hack note in mi_interpreter_init, swap in the output channels... */
105
106 gdb_setup_readline ();
107
362646f5
AC
108 /* These overwrite some of the initialization done in
109 _intialize_event_loop. */
110 call_readline = gdb_readline2;
111 input_handler = mi_execute_command_wrapper;
112 add_file_handler (input_fd, stdin_event_handler, 0);
113 async_command_editing_p = 0;
114 /* FIXME: This is a total hack for now. PB's use of the MI
115 implicitly relies on a bug in the async support which allows
116 asynchronous commands to leak through the commmand loop. The bug
117 involves (but is not limited to) the fact that sync_execution was
118 erroneously initialized to 0. Duplicate by initializing it thus
119 here... */
120 sync_execution = 0;
4a8f6654
AC
121
122 gdb_stdout = mi->out;
123 /* Route error and log output through the MI */
124 gdb_stderr = mi->err;
125 gdb_stdlog = mi->log;
126 /* Route target output through the MI. */
127 gdb_stdtarg = mi->targ;
1f20321b
FR
128 /* Route target error through the MI as well. */
129 gdb_stdtargerr = mi->targ;
4a8f6654
AC
130
131 /* Replace all the hooks that we know about. There really needs to
132 be a better way of doing this... */
133 clear_interpreter_hooks ();
134
9a4105ab 135 deprecated_show_load_progress = mi_load_progress;
4a8f6654
AC
136
137 /* If we're _the_ interpreter, take control. */
138 if (current_interp_named_p (INTERP_MI1))
9a4105ab 139 deprecated_command_loop_hook = mi1_command_loop;
f786f615 140 else if (current_interp_named_p (INTERP_MI2))
9a4105ab 141 deprecated_command_loop_hook = mi2_command_loop;
f786f615 142 else if (current_interp_named_p (INTERP_MI3))
9a4105ab 143 deprecated_command_loop_hook = mi3_command_loop;
4a8f6654 144 else
9a4105ab 145 deprecated_command_loop_hook = mi2_command_loop;
4a8f6654
AC
146
147 return 1;
148}
149
150static int
151mi_interpreter_suspend (void *data)
152{
153 gdb_disable_readline ();
154 return 1;
155}
156
71fff37b 157static struct gdb_exception
4a8f6654
AC
158mi_interpreter_exec (void *data, const char *command)
159{
71fff37b 160 static struct gdb_exception ok;
4a8f6654
AC
161 char *tmp = alloca (strlen (command) + 1);
162 strcpy (tmp, command);
163 mi_execute_command_wrapper (tmp);
c1043fc2 164 return exception_none;
4a8f6654
AC
165}
166
167/* Never display the default gdb prompt in mi case. */
168static int
169mi_interpreter_prompt_p (void *data)
170{
171 return 0;
172}
173
174static void
f107f563 175mi_interpreter_exec_continuation (struct continuation_arg *arg, int error_p)
4a8f6654
AC
176{
177 bpstat_do_actions (&stop_bpstat);
f107f563
VP
178 /* It's not clear what to do in the case of errror -- should we assume that
179 the target is stopped, or that it still runs? */
4a8f6654
AC
180 if (!target_executing)
181 {
182 fputs_unfiltered ("*stopped", raw_stdout);
183 mi_out_put (uiout, raw_stdout);
184 fputs_unfiltered ("\n", raw_stdout);
185 fputs_unfiltered ("(gdb) \n", raw_stdout);
186 gdb_flush (raw_stdout);
4a8f6654
AC
187 }
188 else if (target_can_async_p ())
189 {
190 add_continuation (mi_interpreter_exec_continuation, NULL);
191 }
192}
193
194enum mi_cmd_result
195mi_cmd_interpreter_exec (char *command, char **argv, int argc)
196{
197 struct interp *interp_to_use;
4a8f6654
AC
198 int i;
199 struct interp_procs *procs;
a13e061a
PA
200 char *mi_error_message = NULL;
201 struct cleanup *old_chain;
4a8f6654
AC
202
203 if (argc < 2)
a13e061a 204 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
4a8f6654
AC
205
206 interp_to_use = interp_lookup (argv[0]);
207 if (interp_to_use == NULL)
a13e061a 208 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
4a8f6654
AC
209
210 if (!interp_exec_p (interp_to_use))
a13e061a
PA
211 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
212 argv[0]);
4a8f6654
AC
213
214 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
215 if it has any. */
216 /* KRS: We shouldn't need this... Events should be installed and they should
217 just ALWAYS fire something out down the MI channel... */
218 mi_insert_notify_hooks ();
219
220 /* Now run the code... */
221
a13e061a 222 old_chain = make_cleanup (null_cleanup, 0);
4a8f6654
AC
223 for (i = 1; i < argc; i++)
224 {
32c1e744
VP
225 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
226 if (e.reason < 0)
227 {
228 mi_error_message = xstrdup (e.message);
a13e061a 229 make_cleanup (xfree, mi_error_message);
32c1e744
VP
230 break;
231 }
4a8f6654
AC
232 }
233
234 mi_remove_notify_hooks ();
235
236 /* Okay, now let's see if the command set the inferior going...
237 Tricky point - have to do this AFTER resetting the interpreter, since
238 changing the interpreter will clear out all the continuations for
239 that interpreter... */
240
241 if (target_can_async_p () && target_executing)
242 {
243 fputs_unfiltered ("^running\n", raw_stdout);
244 add_continuation (mi_interpreter_exec_continuation, NULL);
245 }
246
a13e061a
PA
247 if (mi_error_message != NULL)
248 error ("%s", mi_error_message);
249 do_cleanups (old_chain);
250 return MI_CMD_DONE;
4a8f6654
AC
251}
252
253/*
254 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
255 * async-notify ("=") MI messages while running commands in another interpreter
256 * using mi_interpreter_exec. The canonical use for this is to allow access to
257 * the gdb CLI interpreter from within the MI, while still producing MI style output
258 * when actions in the CLI command change gdb's state.
259*/
260
261static void
262mi_insert_notify_hooks (void)
263{
9a4105ab 264 deprecated_query_hook = mi_interp_query_hook;
4a8f6654
AC
265}
266
267static void
11308a41 268mi_remove_notify_hooks (void)
4a8f6654 269{
9a4105ab 270 deprecated_query_hook = NULL;
4a8f6654
AC
271}
272
273static int
274mi_interp_query_hook (const char *ctlstr, va_list ap)
275{
276 return 1;
277}
278
4a8f6654
AC
279static void
280mi_execute_command_wrapper (char *cmd)
281{
282 mi_execute_command (cmd, stdin == instream);
283}
284
285static void
286mi1_command_loop (void)
287{
288 mi_command_loop (1);
289}
290
291static void
292mi2_command_loop (void)
293{
294 mi_command_loop (2);
295}
296
f786f615
AC
297static void
298mi3_command_loop (void)
299{
300 mi_command_loop (3);
301}
302
4a8f6654
AC
303static void
304mi_command_loop (int mi_version)
305{
4a8f6654
AC
306 /* Turn off 8 bit strings in quoted output. Any character with the
307 high bit set is printed using C's octal format. */
308 sevenbit_strings = 1;
309 /* Tell the world that we're alive */
310 fputs_unfiltered ("(gdb) \n", raw_stdout);
311 gdb_flush (raw_stdout);
362646f5 312 start_event_loop ();
4a8f6654
AC
313}
314
683f2885
VP
315static void
316mi_new_thread (struct thread_info *t)
317{
318 struct mi_interp *mi = top_level_interpreter_data ();
319
c6446539 320 fprintf_unfiltered (mi->event_channel, "thread-created,id=\"%d\"", t->num);
683f2885
VP
321 gdb_flush (mi->event_channel);
322}
323
063bfe2e
VP
324static void
325mi_thread_exit (struct thread_info *t)
326{
327 struct mi_interp *mi = top_level_interpreter_data ();
328
329 target_terminal_ours ();
330 fprintf_unfiltered (mi->event_channel, "thread-exited,id=\"%d\"", t->num);
331 gdb_flush (mi->event_channel);
332}
333
b9362cc7
AC
334extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
335
4a8f6654
AC
336void
337_initialize_mi_interp (void)
338{
339 static const struct interp_procs procs =
340 {
341 mi_interpreter_init, /* init_proc */
342 mi_interpreter_resume, /* resume_proc */
343 mi_interpreter_suspend, /* suspend_proc */
344 mi_interpreter_exec, /* exec_proc */
345 mi_interpreter_prompt_p /* prompt_proc_p */
346 };
347
2fcf52f0 348 /* The various interpreter levels. */
4a8f6654 349 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
2fcf52f0
AC
350 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
351 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
4a8f6654 352
2fcf52f0
AC
353 /* "mi" selects the most recent released version. "mi2" was
354 released as part of GDB 6.0. */
355 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
4a8f6654 356}
This page took 0.432464 seconds and 4 git commands to generate.