1130803Smarcel/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2130803Smarcel
3130803Smarcel   Copyright 2002, 2003, 2003 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "gdb_string.h"
24130803Smarcel#include "interps.h"
25130803Smarcel#include "event-top.h"
26130803Smarcel#include "event-loop.h"
27130803Smarcel#include "inferior.h"
28130803Smarcel#include "ui-out.h"
29130803Smarcel#include "top.h"
30130803Smarcel
31130803Smarcel#include "mi-main.h"
32130803Smarcel#include "mi-cmds.h"
33130803Smarcel#include "mi-out.h"
34130803Smarcel#include "mi-console.h"
35130803Smarcel
36130803Smarcelstruct mi_interp
37130803Smarcel{
38130803Smarcel  /* MI's output channels */
39130803Smarcel  struct ui_file *out;
40130803Smarcel  struct ui_file *err;
41130803Smarcel  struct ui_file *log;
42130803Smarcel  struct ui_file *targ;
43130803Smarcel  struct ui_file *event_channel;
44130803Smarcel
45130803Smarcel  /* This is the interpreter for the mi... */
46130803Smarcel  struct interp *mi2_interp;
47130803Smarcel  struct interp *mi1_interp;
48130803Smarcel  struct interp *mi_interp;
49130803Smarcel};
50130803Smarcel
51130803Smarcel/* These are the interpreter setup, etc. functions for the MI interpreter */
52130803Smarcelstatic void mi_execute_command_wrapper (char *cmd);
53130803Smarcelstatic void mi_command_loop (int mi_version);
54130803Smarcelstatic char *mi_input (char *);
55130803Smarcel
56130803Smarcel/* These are hooks that we put in place while doing interpreter_exec
57130803Smarcel   so we can report interesting things that happened "behind the mi's
58130803Smarcel   back" in this command */
59130803Smarcelstatic int mi_interp_query_hook (const char *ctlstr, va_list ap);
60130803Smarcel
61130803Smarcelstatic void mi3_command_loop (void);
62130803Smarcelstatic void mi2_command_loop (void);
63130803Smarcelstatic void mi1_command_loop (void);
64130803Smarcel
65130803Smarcelstatic void mi_insert_notify_hooks (void);
66130803Smarcelstatic void mi_remove_notify_hooks (void);
67130803Smarcel
68130803Smarcelstatic void *
69130803Smarcelmi_interpreter_init (void)
70130803Smarcel{
71130803Smarcel  struct mi_interp *mi = XMALLOC (struct mi_interp);
72130803Smarcel
73130803Smarcel  /* Why is this a part of the mi architecture? */
74130803Smarcel
75130803Smarcel  mi_setup_architecture_data ();
76130803Smarcel
77130803Smarcel  /* HACK: We need to force stdout/stderr to point at the console.  This avoids
78130803Smarcel     any potential side effects caused by legacy code that is still
79130803Smarcel     using the TUI / fputs_unfiltered_hook.  So we set up output channels for
80130803Smarcel     this now, and swap them in when we are run. */
81130803Smarcel
82130803Smarcel  raw_stdout = stdio_fileopen (stdout);
83130803Smarcel
84130803Smarcel  /* Create MI channels */
85130803Smarcel  mi->out = mi_console_file_new (raw_stdout, "~", '"');
86130803Smarcel  mi->err = mi_console_file_new (raw_stdout, "&", '"');
87130803Smarcel  mi->log = mi->err;
88130803Smarcel  mi->targ = mi_console_file_new (raw_stdout, "@", '"');
89130803Smarcel  mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
90130803Smarcel
91130803Smarcel  return mi;
92130803Smarcel}
93130803Smarcel
94130803Smarcelstatic int
95130803Smarcelmi_interpreter_resume (void *data)
96130803Smarcel{
97130803Smarcel  struct mi_interp *mi = data;
98130803Smarcel  /* As per hack note in mi_interpreter_init, swap in the output channels... */
99130803Smarcel
100130803Smarcel  gdb_setup_readline ();
101130803Smarcel
102130803Smarcel  if (event_loop_p)
103130803Smarcel    {
104130803Smarcel      /* These overwrite some of the initialization done in
105130803Smarcel         _intialize_event_loop. */
106130803Smarcel      call_readline = gdb_readline2;
107130803Smarcel      input_handler = mi_execute_command_wrapper;
108130803Smarcel      add_file_handler (input_fd, stdin_event_handler, 0);
109130803Smarcel      async_command_editing_p = 0;
110130803Smarcel      /* FIXME: This is a total hack for now.  PB's use of the MI implicitly
111130803Smarcel         relies on a bug in the async support which allows asynchronous
112130803Smarcel         commands to leak through the commmand loop.  The bug involves
113130803Smarcel         (but is not limited to) the fact that sync_execution was
114130803Smarcel         erroneously initialized to 0.  Duplicate by initializing it
115130803Smarcel         thus here... */
116130803Smarcel      sync_execution = 0;
117130803Smarcel    }
118130803Smarcel
119130803Smarcel  gdb_stdout = mi->out;
120130803Smarcel  /* Route error and log output through the MI */
121130803Smarcel  gdb_stderr = mi->err;
122130803Smarcel  gdb_stdlog = mi->log;
123130803Smarcel  /* Route target output through the MI. */
124130803Smarcel  gdb_stdtarg = mi->targ;
125130803Smarcel
126130803Smarcel  /* Replace all the hooks that we know about.  There really needs to
127130803Smarcel     be a better way of doing this... */
128130803Smarcel  clear_interpreter_hooks ();
129130803Smarcel
130130803Smarcel  show_load_progress = mi_load_progress;
131130803Smarcel
132130803Smarcel  /* If we're _the_ interpreter, take control. */
133130803Smarcel  if (current_interp_named_p (INTERP_MI1))
134130803Smarcel    command_loop_hook = mi1_command_loop;
135130803Smarcel  else if (current_interp_named_p (INTERP_MI2))
136130803Smarcel    command_loop_hook = mi2_command_loop;
137130803Smarcel  else if (current_interp_named_p (INTERP_MI3))
138130803Smarcel    command_loop_hook = mi3_command_loop;
139130803Smarcel  else
140130803Smarcel    command_loop_hook = mi2_command_loop;
141130803Smarcel
142130803Smarcel  return 1;
143130803Smarcel}
144130803Smarcel
145130803Smarcelstatic int
146130803Smarcelmi_interpreter_suspend (void *data)
147130803Smarcel{
148130803Smarcel  gdb_disable_readline ();
149130803Smarcel  return 1;
150130803Smarcel}
151130803Smarcel
152130803Smarcelstatic int
153130803Smarcelmi_interpreter_exec (void *data, const char *command)
154130803Smarcel{
155130803Smarcel  char *tmp = alloca (strlen (command) + 1);
156130803Smarcel  strcpy (tmp, command);
157130803Smarcel  mi_execute_command_wrapper (tmp);
158130803Smarcel  return 1;
159130803Smarcel}
160130803Smarcel
161130803Smarcel/* Never display the default gdb prompt in mi case.  */
162130803Smarcelstatic int
163130803Smarcelmi_interpreter_prompt_p (void *data)
164130803Smarcel{
165130803Smarcel  return 0;
166130803Smarcel}
167130803Smarcel
168130803Smarcelstatic void
169130803Smarcelmi_interpreter_exec_continuation (struct continuation_arg *arg)
170130803Smarcel{
171130803Smarcel  bpstat_do_actions (&stop_bpstat);
172130803Smarcel  if (!target_executing)
173130803Smarcel    {
174130803Smarcel      fputs_unfiltered ("*stopped", raw_stdout);
175130803Smarcel      mi_out_put (uiout, raw_stdout);
176130803Smarcel      fputs_unfiltered ("\n", raw_stdout);
177130803Smarcel      fputs_unfiltered ("(gdb) \n", raw_stdout);
178130803Smarcel      gdb_flush (raw_stdout);
179130803Smarcel      do_exec_cleanups (ALL_CLEANUPS);
180130803Smarcel    }
181130803Smarcel  else if (target_can_async_p ())
182130803Smarcel    {
183130803Smarcel      add_continuation (mi_interpreter_exec_continuation, NULL);
184130803Smarcel    }
185130803Smarcel}
186130803Smarcel
187130803Smarcelenum mi_cmd_result
188130803Smarcelmi_cmd_interpreter_exec (char *command, char **argv, int argc)
189130803Smarcel{
190130803Smarcel  struct interp *interp_to_use;
191130803Smarcel  enum mi_cmd_result result = MI_CMD_DONE;
192130803Smarcel  int i;
193130803Smarcel  struct interp_procs *procs;
194130803Smarcel
195130803Smarcel  if (argc < 2)
196130803Smarcel    {
197130803Smarcel      xasprintf (&mi_error_message,
198130803Smarcel		 "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
199130803Smarcel      return MI_CMD_ERROR;
200130803Smarcel    }
201130803Smarcel
202130803Smarcel  interp_to_use = interp_lookup (argv[0]);
203130803Smarcel  if (interp_to_use == NULL)
204130803Smarcel    {
205130803Smarcel      xasprintf (&mi_error_message,
206130803Smarcel		 "mi_cmd_interpreter_exec: could not find interpreter \"%s\"",
207130803Smarcel		 argv[0]);
208130803Smarcel      return MI_CMD_ERROR;
209130803Smarcel    }
210130803Smarcel
211130803Smarcel  if (!interp_exec_p (interp_to_use))
212130803Smarcel    {
213130803Smarcel      xasprintf (&mi_error_message,
214130803Smarcel		 "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
215130803Smarcel		 argv[0]);
216130803Smarcel      return MI_CMD_ERROR;
217130803Smarcel    }
218130803Smarcel
219130803Smarcel  /* Insert the MI out hooks, making sure to also call the interpreter's hooks
220130803Smarcel     if it has any. */
221130803Smarcel  /* KRS: We shouldn't need this... Events should be installed and they should
222130803Smarcel     just ALWAYS fire something out down the MI channel... */
223130803Smarcel  mi_insert_notify_hooks ();
224130803Smarcel
225130803Smarcel  /* Now run the code... */
226130803Smarcel
227130803Smarcel  for (i = 1; i < argc; i++)
228130803Smarcel    {
229130803Smarcel      char *buff = NULL;
230130803Smarcel      /* Do this in a cleaner way...  We want to force execution to be
231130803Smarcel         asynchronous for commands that run the target.  */
232130803Smarcel      if (target_can_async_p () && (strcmp (argv[0], "console") == 0))
233130803Smarcel	{
234130803Smarcel	  int len = strlen (argv[i]);
235130803Smarcel	  buff = xmalloc (len + 2);
236130803Smarcel	  memcpy (buff, argv[i], len);
237130803Smarcel	  buff[len] = '&';
238130803Smarcel	  buff[len + 1] = '\0';
239130803Smarcel	}
240130803Smarcel
241130803Smarcel      /* We had to set sync_execution = 0 for the mi (well really for Project
242130803Smarcel         Builder's use of the mi - particularly so interrupting would work.
243130803Smarcel         But for console commands to work, we need to initialize it to 1 -
244130803Smarcel         since that is what the cli expects - before running the command,
245130803Smarcel         and then set it back to 0 when we are done. */
246130803Smarcel      sync_execution = 1;
247130803Smarcel      if (interp_exec (interp_to_use, argv[i]) < 0)
248130803Smarcel	{
249130803Smarcel	  mi_error_last_message ();
250130803Smarcel	  result = MI_CMD_ERROR;
251130803Smarcel	  break;
252130803Smarcel	}
253130803Smarcel      xfree (buff);
254130803Smarcel      do_exec_error_cleanups (ALL_CLEANUPS);
255130803Smarcel      sync_execution = 0;
256130803Smarcel    }
257130803Smarcel
258130803Smarcel  mi_remove_notify_hooks ();
259130803Smarcel
260130803Smarcel  /* Okay, now let's see if the command set the inferior going...
261130803Smarcel     Tricky point - have to do this AFTER resetting the interpreter, since
262130803Smarcel     changing the interpreter will clear out all the continuations for
263130803Smarcel     that interpreter... */
264130803Smarcel
265130803Smarcel  if (target_can_async_p () && target_executing)
266130803Smarcel    {
267130803Smarcel      fputs_unfiltered ("^running\n", raw_stdout);
268130803Smarcel      add_continuation (mi_interpreter_exec_continuation, NULL);
269130803Smarcel    }
270130803Smarcel
271130803Smarcel  return result;
272130803Smarcel}
273130803Smarcel
274130803Smarcel/*
275130803Smarcel * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
276130803Smarcel * async-notify ("=") MI messages while running commands in another interpreter
277130803Smarcel * using mi_interpreter_exec.  The canonical use for this is to allow access to
278130803Smarcel * the gdb CLI interpreter from within the MI, while still producing MI style output
279130803Smarcel * when actions in the CLI command change gdb's state.
280130803Smarcel*/
281130803Smarcel
282130803Smarcelstatic void
283130803Smarcelmi_insert_notify_hooks (void)
284130803Smarcel{
285130803Smarcel  query_hook = mi_interp_query_hook;
286130803Smarcel}
287130803Smarcel
288130803Smarcelstatic void
289130803Smarcelmi_remove_notify_hooks (void)
290130803Smarcel{
291130803Smarcel  query_hook = NULL;
292130803Smarcel}
293130803Smarcel
294130803Smarcelstatic int
295130803Smarcelmi_interp_query_hook (const char *ctlstr, va_list ap)
296130803Smarcel{
297130803Smarcel  return 1;
298130803Smarcel}
299130803Smarcel
300130803Smarcelstatic void
301130803Smarcelmi_execute_command_wrapper (char *cmd)
302130803Smarcel{
303130803Smarcel  mi_execute_command (cmd, stdin == instream);
304130803Smarcel}
305130803Smarcel
306130803Smarcelstatic void
307130803Smarcelmi1_command_loop (void)
308130803Smarcel{
309130803Smarcel  mi_command_loop (1);
310130803Smarcel}
311130803Smarcel
312130803Smarcelstatic void
313130803Smarcelmi2_command_loop (void)
314130803Smarcel{
315130803Smarcel  mi_command_loop (2);
316130803Smarcel}
317130803Smarcel
318130803Smarcelstatic void
319130803Smarcelmi3_command_loop (void)
320130803Smarcel{
321130803Smarcel  mi_command_loop (3);
322130803Smarcel}
323130803Smarcel
324130803Smarcelstatic void
325130803Smarcelmi_command_loop (int mi_version)
326130803Smarcel{
327130803Smarcel#if 0
328130803Smarcel  /* HACK: Force stdout/stderr to point at the console.  This avoids
329130803Smarcel     any potential side effects caused by legacy code that is still
330130803Smarcel     using the TUI / fputs_unfiltered_hook */
331130803Smarcel  raw_stdout = stdio_fileopen (stdout);
332130803Smarcel  /* Route normal output through the MIx */
333130803Smarcel  gdb_stdout = mi_console_file_new (raw_stdout, "~", '"');
334130803Smarcel  /* Route error and log output through the MI */
335130803Smarcel  gdb_stderr = mi_console_file_new (raw_stdout, "&", '"');
336130803Smarcel  gdb_stdlog = gdb_stderr;
337130803Smarcel  /* Route target output through the MI. */
338130803Smarcel  gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"');
339130803Smarcel  /* HACK: Poke the ui_out table directly.  Should we be creating a
340130803Smarcel     mi_out object wired up to the above gdb_stdout / gdb_stderr? */
341130803Smarcel  uiout = mi_out_new (mi_version);
342130803Smarcel  /* HACK: Override any other interpreter hooks.  We need to create a
343130803Smarcel     real event table and pass in that. */
344130803Smarcel  init_ui_hook = 0;
345130803Smarcel  /* command_loop_hook = 0; */
346130803Smarcel  print_frame_info_listing_hook = 0;
347130803Smarcel  query_hook = 0;
348130803Smarcel  warning_hook = 0;
349130803Smarcel  create_breakpoint_hook = 0;
350130803Smarcel  delete_breakpoint_hook = 0;
351130803Smarcel  modify_breakpoint_hook = 0;
352130803Smarcel  interactive_hook = 0;
353130803Smarcel  registers_changed_hook = 0;
354130803Smarcel  readline_begin_hook = 0;
355130803Smarcel  readline_hook = 0;
356130803Smarcel  readline_end_hook = 0;
357130803Smarcel  register_changed_hook = 0;
358130803Smarcel  memory_changed_hook = 0;
359130803Smarcel  context_hook = 0;
360130803Smarcel  target_wait_hook = 0;
361130803Smarcel  call_command_hook = 0;
362130803Smarcel  error_hook = 0;
363130803Smarcel  error_begin_hook = 0;
364130803Smarcel  show_load_progress = mi_load_progress;
365130803Smarcel#endif
366130803Smarcel  /* Turn off 8 bit strings in quoted output.  Any character with the
367130803Smarcel     high bit set is printed using C's octal format. */
368130803Smarcel  sevenbit_strings = 1;
369130803Smarcel  /* Tell the world that we're alive */
370130803Smarcel  fputs_unfiltered ("(gdb) \n", raw_stdout);
371130803Smarcel  gdb_flush (raw_stdout);
372130803Smarcel  if (!event_loop_p)
373130803Smarcel    simplified_command_loop (mi_input, mi_execute_command);
374130803Smarcel  else
375130803Smarcel    start_event_loop ();
376130803Smarcel}
377130803Smarcel
378130803Smarcelstatic char *
379130803Smarcelmi_input (char *buf)
380130803Smarcel{
381130803Smarcel  return gdb_readline (NULL);
382130803Smarcel}
383130803Smarcel
384130803Smarcelextern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
385130803Smarcel
386130803Smarcelvoid
387130803Smarcel_initialize_mi_interp (void)
388130803Smarcel{
389130803Smarcel  static const struct interp_procs procs =
390130803Smarcel  {
391130803Smarcel    mi_interpreter_init,	/* init_proc */
392130803Smarcel    mi_interpreter_resume,	/* resume_proc */
393130803Smarcel    mi_interpreter_suspend,	/* suspend_proc */
394130803Smarcel    mi_interpreter_exec,	/* exec_proc */
395130803Smarcel    mi_interpreter_prompt_p	/* prompt_proc_p */
396130803Smarcel  };
397130803Smarcel
398130803Smarcel  /* The various interpreter levels.  */
399130803Smarcel  interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
400130803Smarcel  interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
401130803Smarcel  interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
402130803Smarcel
403130803Smarcel  /* "mi" selects the most recent released version.  "mi2" was
404130803Smarcel     released as part of GDB 6.0.  */
405130803Smarcel  interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
406130803Smarcel}
407