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