2004-01-17 Andrew Cagney <cagney@redhat.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
3 Copyright 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "interps.h"
25#include "event-top.h"
26#include "event-loop.h"
27#include "inferior.h"
28#include "ui-out.h"
29#include "top.h"
30
31#include "mi-main.h"
32#include "mi-cmds.h"
33#include "mi-out.h"
34#include "mi-console.h"
35
36struct mi_interp
37{
38 /* MI's output channels */
39 struct ui_file *out;
40 struct ui_file *err;
41 struct ui_file *log;
42 struct ui_file *targ;
43 struct ui_file *event_channel;
44
45 /* This is the interpreter for the mi... */
46 struct interp *mi2_interp;
47 struct interp *mi1_interp;
48 struct interp *mi_interp;
49};
50
51/* These are the interpreter setup, etc. functions for the MI interpreter */
52static void mi_execute_command_wrapper (char *cmd);
53static void mi_command_loop (int mi_version);
54static char *mi_input (char *);
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 */
59static int mi_interp_query_hook (const char *ctlstr, va_list ap);
60static char *mi_interp_read_one_line_hook (char *prompt, int repeat,
61 char *anno);
62
f786f615 63static void mi3_command_loop (void);
4a8f6654
AC
64static void mi2_command_loop (void);
65static void mi1_command_loop (void);
66
67static void mi_insert_notify_hooks (void);
68static void mi_remove_notify_hooks (void);
69
70static void *
71mi_interpreter_init (void)
72{
73 struct mi_interp *mi = XMALLOC (struct mi_interp);
74
75 /* Why is this a part of the mi architecture? */
76
77 mi_setup_architecture_data ();
78
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
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
104 if (event_loop_p)
105 {
106 /* These overwrite some of the initialization done in
107 _intialize_event_loop. */
108 call_readline = gdb_readline2;
109 input_handler = mi_execute_command_wrapper;
110 add_file_handler (input_fd, stdin_event_handler, 0);
111 async_command_editing_p = 0;
112 /* FIXME: This is a total hack for now. PB's use of the MI implicitly
113 relies on a bug in the async support which allows asynchronous
114 commands to leak through the commmand loop. The bug involves
115 (but is not limited to) the fact that sync_execution was
116 erroneously initialized to 0. Duplicate by initializing it
117 thus here... */
118 sync_execution = 0;
119 }
120
121 gdb_stdout = mi->out;
122 /* Route error and log output through the MI */
123 gdb_stderr = mi->err;
124 gdb_stdlog = mi->log;
125 /* Route target output through the MI. */
126 gdb_stdtarg = mi->targ;
127
128 /* Replace all the hooks that we know about. There really needs to
129 be a better way of doing this... */
130 clear_interpreter_hooks ();
131
132 show_load_progress = mi_load_progress;
133
134 /* If we're _the_ interpreter, take control. */
135 if (current_interp_named_p (INTERP_MI1))
136 command_loop_hook = mi1_command_loop;
f786f615 137 else if (current_interp_named_p (INTERP_MI2))
4a8f6654 138 command_loop_hook = mi2_command_loop;
f786f615
AC
139 else if (current_interp_named_p (INTERP_MI3))
140 command_loop_hook = mi3_command_loop;
4a8f6654 141 else
f786f615 142 command_loop_hook = mi2_command_loop;
4a8f6654
AC
143
144 return 1;
145}
146
147static int
148mi_interpreter_suspend (void *data)
149{
150 gdb_disable_readline ();
151 return 1;
152}
153
154static int
155mi_interpreter_exec (void *data, const char *command)
156{
157 char *tmp = alloca (strlen (command) + 1);
158 strcpy (tmp, command);
159 mi_execute_command_wrapper (tmp);
160 return 1;
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 {
199 xasprintf (&mi_error_message,
200 "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
201 return MI_CMD_ERROR;
202 }
203
204 interp_to_use = interp_lookup (argv[0]);
205 if (interp_to_use == NULL)
206 {
207 xasprintf (&mi_error_message,
208 "mi_cmd_interpreter_exec: could not find interpreter \"%s\"",
209 argv[0]);
210 return MI_CMD_ERROR;
211 }
212
213 if (!interp_exec_p (interp_to_use))
214 {
215 xasprintf (&mi_error_message,
216 "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
217 argv[0]);
218 return MI_CMD_ERROR;
219 }
220
221 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
222 if it has any. */
223 /* KRS: We shouldn't need this... Events should be installed and they should
224 just ALWAYS fire something out down the MI channel... */
225 mi_insert_notify_hooks ();
226
227 /* Now run the code... */
228
229 for (i = 1; i < argc; i++)
230 {
231 char *buff = NULL;
232 /* Do this in a cleaner way... We want to force execution to be
233 asynchronous for commands that run the target. */
234 if (target_can_async_p () && (strcmp (argv[0], "console") == 0))
235 {
236 int len = strlen (argv[i]);
237 buff = xmalloc (len + 2);
238 memcpy (buff, argv[i], len);
239 buff[len] = '&';
240 buff[len + 1] = '\0';
241 }
242
243 /* We had to set sync_execution = 0 for the mi (well really for Project
244 Builder's use of the mi - particularly so interrupting would work.
245 But for console commands to work, we need to initialize it to 1 -
246 since that is what the cli expects - before running the command,
247 and then set it back to 0 when we are done. */
248 sync_execution = 1;
249 if (interp_exec (interp_to_use, argv[i]) < 0)
250 {
251 mi_error_last_message ();
252 result = MI_CMD_ERROR;
253 break;
254 }
255 xfree (buff);
256 do_exec_error_cleanups (ALL_CLEANUPS);
257 sync_execution = 0;
258 }
259
260 mi_remove_notify_hooks ();
261
262 /* Okay, now let's see if the command set the inferior going...
263 Tricky point - have to do this AFTER resetting the interpreter, since
264 changing the interpreter will clear out all the continuations for
265 that interpreter... */
266
267 if (target_can_async_p () && target_executing)
268 {
269 fputs_unfiltered ("^running\n", raw_stdout);
270 add_continuation (mi_interpreter_exec_continuation, NULL);
271 }
272
273 return result;
274}
275
276/*
277 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
278 * async-notify ("=") MI messages while running commands in another interpreter
279 * using mi_interpreter_exec. The canonical use for this is to allow access to
280 * the gdb CLI interpreter from within the MI, while still producing MI style output
281 * when actions in the CLI command change gdb's state.
282*/
283
284static void
285mi_insert_notify_hooks (void)
286{
287 query_hook = mi_interp_query_hook;
288}
289
290static void
11308a41 291mi_remove_notify_hooks (void)
4a8f6654
AC
292{
293 query_hook = NULL;
294}
295
296static int
297mi_interp_query_hook (const char *ctlstr, va_list ap)
298{
299 return 1;
300}
301
302static char *
303mi_interp_read_one_line_hook (char *prompt, int repeat, char *anno)
304{
305 static char buff[256];
306 printf_unfiltered ("=read-one-line,prompt=\"%s\"\n", prompt);
307 gdb_flush (gdb_stdout);
308 (void) fgets (buff, sizeof (buff), stdin);
309 buff[(strlen (buff) - 1)] = 0;
310 return buff;
311}
312
313static void
314output_control_change_notification (char *notification)
315{
316 printf_unfiltered ("^");
317 printf_unfiltered ("%s\n", notification);
318 gdb_flush (gdb_stdout);
319}
320
321static void
322mi_execute_command_wrapper (char *cmd)
323{
324 mi_execute_command (cmd, stdin == instream);
325}
326
327static void
328mi1_command_loop (void)
329{
330 mi_command_loop (1);
331}
332
333static void
334mi2_command_loop (void)
335{
336 mi_command_loop (2);
337}
338
f786f615
AC
339static void
340mi3_command_loop (void)
341{
342 mi_command_loop (3);
343}
344
4a8f6654
AC
345static void
346mi_command_loop (int mi_version)
347{
348#if 0
349 /* HACK: Force stdout/stderr to point at the console. This avoids
350 any potential side effects caused by legacy code that is still
351 using the TUI / fputs_unfiltered_hook */
352 raw_stdout = stdio_fileopen (stdout);
353 /* Route normal output through the MIx */
354 gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
355 /* Route error and log output through the MI */
356 gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
357 gdb_stdlog = gdb_stderr;
358 /* Route target output through the MI. */
359 gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
360 /* HACK: Poke the ui_out table directly. Should we be creating a
361 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
362 uiout = mi_out_new (mi_version);
363 /* HACK: Override any other interpreter hooks. We need to create a
364 real event table and pass in that. */
365 init_ui_hook = 0;
366 /* command_loop_hook = 0; */
367 print_frame_info_listing_hook = 0;
368 query_hook = 0;
369 warning_hook = 0;
370 create_breakpoint_hook = 0;
371 delete_breakpoint_hook = 0;
372 modify_breakpoint_hook = 0;
373 interactive_hook = 0;
374 registers_changed_hook = 0;
375 readline_begin_hook = 0;
376 readline_hook = 0;
377 readline_end_hook = 0;
378 register_changed_hook = 0;
379 memory_changed_hook = 0;
380 context_hook = 0;
381 target_wait_hook = 0;
382 call_command_hook = 0;
383 error_hook = 0;
384 error_begin_hook = 0;
385 show_load_progress = mi_load_progress;
386#endif
387 /* Turn off 8 bit strings in quoted output. Any character with the
388 high bit set is printed using C's octal format. */
389 sevenbit_strings = 1;
390 /* Tell the world that we're alive */
391 fputs_unfiltered ("(gdb) \n", raw_stdout);
392 gdb_flush (raw_stdout);
393 if (!event_loop_p)
394 simplified_command_loop (mi_input, mi_execute_command);
395 else
396 start_event_loop ();
397}
398
399static char *
400mi_input (char *buf)
401{
402 return gdb_readline (NULL);
403}
404
b9362cc7
AC
405extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
406
4a8f6654
AC
407void
408_initialize_mi_interp (void)
409{
410 static const struct interp_procs procs =
411 {
412 mi_interpreter_init, /* init_proc */
413 mi_interpreter_resume, /* resume_proc */
414 mi_interpreter_suspend, /* suspend_proc */
415 mi_interpreter_exec, /* exec_proc */
416 mi_interpreter_prompt_p /* prompt_proc_p */
417 };
418
2fcf52f0 419 /* The various interpreter levels. */
4a8f6654 420 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
2fcf52f0
AC
421 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
422 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
4a8f6654 423
2fcf52f0
AC
424 /* "mi" selects the most recent released version. "mi2" was
425 released as part of GDB 6.0. */
426 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
4a8f6654 427}
This page took 0.124231 seconds and 4 git commands to generate.