2007-08-14 Michael Snyder <msnyder@access-company.com>
[external/binutils.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3    Copyright (C) 2002, 2003, 2004, 2005, 2007 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., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, 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 #include "exceptions.h"
31 #include "mi-main.h"
32 #include "mi-cmds.h"
33 #include "mi-out.h"
34 #include "mi-console.h"
35
36 struct 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 */
52 static void mi_execute_command_wrapper (char *cmd);
53 static void mi_command_loop (int mi_version);
54
55 /* These are hooks that we put in place while doing interpreter_exec
56    so we can report interesting things that happened "behind the mi's
57    back" in this command */
58 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
59      ATTR_FORMAT (printf, 1, 0);
60
61 static void mi3_command_loop (void);
62 static void mi2_command_loop (void);
63 static void mi1_command_loop (void);
64
65 static void mi_insert_notify_hooks (void);
66 static void mi_remove_notify_hooks (void);
67
68 static void *
69 mi_interpreter_init (void)
70 {
71   struct mi_interp *mi = XMALLOC (struct mi_interp);
72
73   /* HACK: We need to force stdout/stderr to point at the console.  This avoids
74      any potential side effects caused by legacy code that is still
75      using the TUI / fputs_unfiltered_hook.  So we set up output channels for
76      this now, and swap them in when we are run. */
77
78   raw_stdout = stdio_fileopen (stdout);
79
80   /* Create MI channels */
81   mi->out = mi_console_file_new (raw_stdout, "~", '"');
82   mi->err = mi_console_file_new (raw_stdout, "&", '"');
83   mi->log = mi->err;
84   mi->targ = mi_console_file_new (raw_stdout, "@", '"');
85   mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
86
87   return mi;
88 }
89
90 static int
91 mi_interpreter_resume (void *data)
92 {
93   struct mi_interp *mi = data;
94   /* As per hack note in mi_interpreter_init, swap in the output channels... */
95
96   gdb_setup_readline ();
97
98   /* These overwrite some of the initialization done in
99      _intialize_event_loop.  */
100   call_readline = gdb_readline2;
101   input_handler = mi_execute_command_wrapper;
102   add_file_handler (input_fd, stdin_event_handler, 0);
103   async_command_editing_p = 0;
104   /* FIXME: This is a total hack for now.  PB's use of the MI
105      implicitly relies on a bug in the async support which allows
106      asynchronous commands to leak through the commmand loop.  The bug
107      involves (but is not limited to) the fact that sync_execution was
108      erroneously initialized to 0.  Duplicate by initializing it thus
109      here...  */
110   sync_execution = 0;
111
112   gdb_stdout = mi->out;
113   /* Route error and log output through the MI */
114   gdb_stderr = mi->err;
115   gdb_stdlog = mi->log;
116   /* Route target output through the MI. */
117   gdb_stdtarg = mi->targ;
118   /* Route target error through the MI as well. */
119   gdb_stdtargerr = mi->targ;
120
121   /* Replace all the hooks that we know about.  There really needs to
122      be a better way of doing this... */
123   clear_interpreter_hooks ();
124
125   deprecated_show_load_progress = mi_load_progress;
126
127   /* If we're _the_ interpreter, take control. */
128   if (current_interp_named_p (INTERP_MI1))
129     deprecated_command_loop_hook = mi1_command_loop;
130   else if (current_interp_named_p (INTERP_MI2))
131     deprecated_command_loop_hook = mi2_command_loop;
132   else if (current_interp_named_p (INTERP_MI3))
133     deprecated_command_loop_hook = mi3_command_loop;
134   else
135     deprecated_command_loop_hook = mi2_command_loop;
136
137   return 1;
138 }
139
140 static int
141 mi_interpreter_suspend (void *data)
142 {
143   gdb_disable_readline ();
144   return 1;
145 }
146
147 static struct gdb_exception
148 mi_interpreter_exec (void *data, const char *command)
149 {
150   static struct gdb_exception ok;
151   char *tmp = alloca (strlen (command) + 1);
152   strcpy (tmp, command);
153   mi_execute_command_wrapper (tmp);
154   return exception_none;
155 }
156
157 /* Never display the default gdb prompt in mi case.  */
158 static int
159 mi_interpreter_prompt_p (void *data)
160 {
161   return 0;
162 }
163
164 static void
165 mi_interpreter_exec_continuation (struct continuation_arg *arg)
166 {
167   bpstat_do_actions (&stop_bpstat);
168   if (!target_executing)
169     {
170       fputs_unfiltered ("*stopped", raw_stdout);
171       mi_out_put (uiout, raw_stdout);
172       fputs_unfiltered ("\n", raw_stdout);
173       fputs_unfiltered ("(gdb) \n", raw_stdout);
174       gdb_flush (raw_stdout);
175       do_exec_cleanups (ALL_CLEANUPS);
176     }
177   else if (target_can_async_p ())
178     {
179       add_continuation (mi_interpreter_exec_continuation, NULL);
180     }
181 }
182
183 enum mi_cmd_result
184 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
185 {
186   struct interp *interp_to_use;
187   enum mi_cmd_result result = MI_CMD_DONE;
188   int i;
189   struct interp_procs *procs;
190
191   if (argc < 2)
192     {
193       mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
194       return MI_CMD_ERROR;
195     }
196
197   interp_to_use = interp_lookup (argv[0]);
198   if (interp_to_use == NULL)
199     {
200       mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
201       return MI_CMD_ERROR;
202     }
203
204   if (!interp_exec_p (interp_to_use))
205     {
206       mi_error_message = xstrprintf ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
207                                      argv[0]);
208       return MI_CMD_ERROR;
209     }
210
211   /* Insert the MI out hooks, making sure to also call the interpreter's hooks
212      if it has any. */
213   /* KRS: We shouldn't need this... Events should be installed and they should
214      just ALWAYS fire something out down the MI channel... */
215   mi_insert_notify_hooks ();
216
217   /* Now run the code... */
218
219   for (i = 1; i < argc; i++)
220     {
221       /* We had to set sync_execution = 0 for the mi (well really for Project
222          Builder's use of the mi - particularly so interrupting would work.
223          But for console commands to work, we need to initialize it to 1 -
224          since that is what the cli expects - before running the command,
225          and then set it back to 0 when we are done. */
226       sync_execution = 1;
227       {
228         struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
229         if (e.reason < 0)
230           {
231             mi_error_message = xstrdup (e.message);
232             result = MI_CMD_ERROR;
233             break;
234           }
235       }
236       do_exec_error_cleanups (ALL_CLEANUPS);
237       sync_execution = 0;
238     }
239
240   mi_remove_notify_hooks ();
241
242   /* Okay, now let's see if the command set the inferior going...
243      Tricky point - have to do this AFTER resetting the interpreter, since
244      changing the interpreter will clear out all the continuations for
245      that interpreter... */
246
247   if (target_can_async_p () && target_executing)
248     {
249       fputs_unfiltered ("^running\n", raw_stdout);
250       add_continuation (mi_interpreter_exec_continuation, NULL);
251     }
252
253   return result;
254 }
255
256 /*
257  * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
258  * async-notify ("=") MI messages while running commands in another interpreter
259  * using mi_interpreter_exec.  The canonical use for this is to allow access to
260  * the gdb CLI interpreter from within the MI, while still producing MI style output
261  * when actions in the CLI command change gdb's state.
262 */
263
264 static void
265 mi_insert_notify_hooks (void)
266 {
267   deprecated_query_hook = mi_interp_query_hook;
268 }
269
270 static void
271 mi_remove_notify_hooks (void)
272 {
273   deprecated_query_hook = NULL;
274 }
275
276 static int
277 mi_interp_query_hook (const char *ctlstr, va_list ap)
278 {
279   return 1;
280 }
281
282 static void
283 mi_execute_command_wrapper (char *cmd)
284 {
285   mi_execute_command (cmd, stdin == instream);
286 }
287
288 static void
289 mi1_command_loop (void)
290 {
291   mi_command_loop (1);
292 }
293
294 static void
295 mi2_command_loop (void)
296 {
297   mi_command_loop (2);
298 }
299
300 static void
301 mi3_command_loop (void)
302 {
303   mi_command_loop (3);
304 }
305
306 static void
307 mi_command_loop (int mi_version)
308 {
309 #if 0
310   /* HACK: Force stdout/stderr to point at the console.  This avoids
311      any potential side effects caused by legacy code that is still
312      using the TUI / fputs_unfiltered_hook */
313   raw_stdout = stdio_fileopen (stdout);
314   /* Route normal output through the MIx */
315   gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
316   /* Route error and log output through the MI */
317   gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
318   gdb_stdlog = gdb_stderr;
319   /* Route target output through the MI. */
320   gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
321   /* HACK: Poke the ui_out table directly.  Should we be creating a
322      mi_out object wired up to the above gdb_stdout / gdb_stderr? */
323   uiout = mi_out_new (mi_version);
324   /* HACK: Override any other interpreter hooks.  We need to create a
325      real event table and pass in that. */
326   deprecated_init_ui_hook = 0;
327   /* deprecated_command_loop_hook = 0; */
328   deprecated_print_frame_info_listing_hook = 0;
329   deprecated_query_hook = 0;
330   deprecated_warning_hook = 0;
331   deprecated_create_breakpoint_hook = 0;
332   deprecated_delete_breakpoint_hook = 0;
333   deprecated_modify_breakpoint_hook = 0;
334   deprecated_interactive_hook = 0;
335   deprecated_readline_begin_hook = 0;
336   deprecated_readline_hook = 0;
337   deprecated_readline_end_hook = 0;
338   deprecated_register_changed_hook = 0;
339   deprecated_memory_changed_hook = 0;
340   deprecated_context_hook = 0;
341   deprecated_target_wait_hook = 0;
342   deprecated_call_command_hook = 0;
343   deprecated_error_hook = 0;
344   deprecated_error_begin_hook = 0;
345   deprecated_show_load_progress = mi_load_progress;
346 #endif
347   /* Turn off 8 bit strings in quoted output.  Any character with the
348      high bit set is printed using C's octal format. */
349   sevenbit_strings = 1;
350   /* Tell the world that we're alive */
351   fputs_unfiltered ("(gdb) \n", raw_stdout);
352   gdb_flush (raw_stdout);
353   start_event_loop ();
354 }
355
356 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
357
358 void
359 _initialize_mi_interp (void)
360 {
361   static const struct interp_procs procs =
362   {
363     mi_interpreter_init,        /* init_proc */
364     mi_interpreter_resume,      /* resume_proc */
365     mi_interpreter_suspend,     /* suspend_proc */
366     mi_interpreter_exec,        /* exec_proc */
367     mi_interpreter_prompt_p     /* prompt_proc_p */
368   };
369
370   /* The various interpreter levels.  */
371   interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
372   interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
373   interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
374
375   /* "mi" selects the most recent released version.  "mi2" was
376      released as part of GDB 6.0.  */
377   interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
378 }