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