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