1129470Spjd/* MI Interpreter Definitions and Commands for GDB, the GNU debugger. 2129470Spjd 3129470Spjd Copyright 2002, 2003, 2003 Free Software Foundation, Inc. 4129470Spjd 5129470Spjd This file is part of GDB. 6129470Spjd 7129470Spjd This program is free software; you can redistribute it and/or modify 8129470Spjd it under the terms of the GNU General Public License as published by 9129470Spjd the Free Software Foundation; either version 2 of the License, or 10129470Spjd (at your option) any later version. 11129470Spjd 12129470Spjd This program is distributed in the hope that it will be useful, 13155175Spjd but WITHOUT ANY WARRANTY; without even the implied warranty of 14129470Spjd MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15129470Spjd GNU General Public License for more details. 16129470Spjd 17129470Spjd You should have received a copy of the GNU General Public License 18129470Spjd along with this program; if not, write to the Free Software 19129470Spjd Foundation, Inc., 59 Temple Place - Suite 330, 20129470Spjd Boston, MA 02111-1307, USA. */ 21129470Spjd 22129470Spjd#include "defs.h" 23129470Spjd#include "gdb_string.h" 24129470Spjd#include "interps.h" 25129470Spjd#include "event-top.h" 26129470Spjd#include "event-loop.h" 27129470Spjd#include "inferior.h" 28129470Spjd#include "ui-out.h" 29129470Spjd#include "top.h" 30129470Spjd 31129470Spjd#include "mi-main.h" 32129470Spjd#include "mi-cmds.h" 33129470Spjd#include "mi-out.h" 34129470Spjd#include "mi-console.h" 35129470Spjd 36129470Spjdstruct mi_interp 37129470Spjd{ 38129470Spjd /* MI's output channels */ 39129470Spjd struct ui_file *out; 40129470Spjd struct ui_file *err; 41129470Spjd struct ui_file *log; 42129470Spjd struct ui_file *targ; 43129470Spjd struct ui_file *event_channel; 44129470Spjd 45129470Spjd /* This is the interpreter for the mi... */ 46129470Spjd struct interp *mi2_interp; 47129470Spjd struct interp *mi1_interp; 48129470Spjd struct interp *mi_interp; 49129470Spjd}; 50129470Spjd 51129470Spjd/* These are the interpreter setup, etc. functions for the MI interpreter */ 52129470Spjdstatic void mi_execute_command_wrapper (char *cmd); 53129470Spjdstatic void mi_command_loop (int mi_version); 54129470Spjdstatic char *mi_input (char *); 55129470Spjd 56129470Spjd/* These are hooks that we put in place while doing interpreter_exec 57129470Spjd so we can report interesting things that happened "behind the mi's 58129470Spjd back" in this command */ 59129470Spjdstatic int mi_interp_query_hook (const char *ctlstr, va_list ap); 60129470Spjd 61129470Spjdstatic void mi3_command_loop (void); 62129470Spjdstatic void mi2_command_loop (void); 63129470Spjdstatic void mi1_command_loop (void); 64129470Spjd 65129470Spjdstatic void mi_insert_notify_hooks (void); 66129470Spjdstatic void mi_remove_notify_hooks (void); 67129470Spjd 68129470Spjdstatic void * 69129470Spjdmi_interpreter_init (void) 70129470Spjd{ 71129470Spjd struct mi_interp *mi = XMALLOC (struct mi_interp); 72130591Spjd 73130591Spjd /* Why is this a part of the mi architecture? */ 74130591Spjd 75130591Spjd mi_setup_architecture_data (); 76130591Spjd 77130591Spjd /* HACK: We need to force stdout/stderr to point at the console. This avoids 78130591Spjd any potential side effects caused by legacy code that is still 79130591Spjd using the TUI / fputs_unfiltered_hook. So we set up output channels for 80130591Spjd this now, and swap them in when we are run. */ 81130591Spjd 82130591Spjd raw_stdout = stdio_fileopen (stdout); 83130591Spjd 84130591Spjd /* Create MI channels */ 85130591Spjd mi->out = mi_console_file_new (raw_stdout, "~", '"'); 86130591Spjd mi->err = mi_console_file_new (raw_stdout, "&", '"'); 87130591Spjd mi->log = mi->err; 88130591Spjd mi->targ = mi_console_file_new (raw_stdout, "@", '"'); 89130591Spjd mi->event_channel = mi_console_file_new (raw_stdout, "=", 0); 90130591Spjd 91130591Spjd return mi; 92130591Spjd} 93130591Spjd 94130591Spjdstatic int 95130591Spjdmi_interpreter_resume (void *data) 96130591Spjd{ 97130591Spjd struct mi_interp *mi = data; 98149302Spjd /* As per hack note in mi_interpreter_init, swap in the output channels... */ 99149302Spjd 100149302Spjd gdb_setup_readline (); 101149302Spjd 102149302Spjd if (event_loop_p) 103149302Spjd { 104149302Spjd /* These overwrite some of the initialization done in 105149302Spjd _intialize_event_loop. */ 106149302Spjd call_readline = gdb_readline2; 107149302Spjd input_handler = mi_execute_command_wrapper; 108149302Spjd add_file_handler (input_fd, stdin_event_handler, 0); 109149302Spjd async_command_editing_p = 0; 110130591Spjd /* FIXME: This is a total hack for now. PB's use of the MI implicitly 111130591Spjd relies on a bug in the async support which allows asynchronous 112130591Spjd commands to leak through the commmand loop. The bug involves 113130591Spjd (but is not limited to) the fact that sync_execution was 114130591Spjd erroneously initialized to 0. Duplicate by initializing it 115130591Spjd thus here... */ 116130591Spjd sync_execution = 0; 117130591Spjd } 118130591Spjd 119130591Spjd gdb_stdout = mi->out; 120130591Spjd /* Route error and log output through the MI */ 121130591Spjd gdb_stderr = mi->err; 122130591Spjd gdb_stdlog = mi->log; 123130591Spjd /* Route target output through the MI. */ 124130591Spjd gdb_stdtarg = mi->targ; 125130591Spjd 126130591Spjd /* Replace all the hooks that we know about. There really needs to 127130591Spjd be a better way of doing this... */ 128130591Spjd clear_interpreter_hooks (); 129130591Spjd 130130591Spjd show_load_progress = mi_load_progress; 131130591Spjd 132130591Spjd /* If we're _the_ interpreter, take control. */ 133130591Spjd if (current_interp_named_p (INTERP_MI1)) 134130591Spjd command_loop_hook = mi1_command_loop; 135130591Spjd else if (current_interp_named_p (INTERP_MI2)) 136130591Spjd command_loop_hook = mi2_command_loop; 137130591Spjd else if (current_interp_named_p (INTERP_MI3)) 138130591Spjd command_loop_hook = mi3_command_loop; 139130591Spjd else 140130591Spjd command_loop_hook = mi2_command_loop; 141130591Spjd 142130591Spjd return 1; 143130591Spjd} 144130591Spjd 145130591Spjdstatic int 146130591Spjdmi_interpreter_suspend (void *data) 147130591Spjd{ 148129470Spjd gdb_disable_readline (); 149131603Spjd return 1; 150131603Spjd} 151131603Spjd 152131603Spjdstatic int 153131603Spjdmi_interpreter_exec (void *data, const char *command) 154131603Spjd{ 155131603Spjd char *tmp = alloca (strlen (command) + 1); 156131603Spjd strcpy (tmp, command); 157131603Spjd mi_execute_command_wrapper (tmp); 158131603Spjd return 1; 159131603Spjd} 160131603Spjd 161131603Spjd/* Never display the default gdb prompt in mi case. */ 162131603Spjdstatic int 163131603Spjdmi_interpreter_prompt_p (void *data) 164131603Spjd{ 165131603Spjd return 0; 166131603Spjd} 167131603Spjd 168131603Spjdstatic void 169131603Spjdmi_interpreter_exec_continuation (struct continuation_arg *arg) 170131603Spjd{ 171131603Spjd bpstat_do_actions (&stop_bpstat); 172131603Spjd if (!target_executing) 173131603Spjd { 174131603Spjd fputs_unfiltered ("*stopped", raw_stdout); 175131603Spjd mi_out_put (uiout, raw_stdout); 176131603Spjd fputs_unfiltered ("\n", raw_stdout); 177131603Spjd fputs_unfiltered ("(gdb) \n", raw_stdout); 178131603Spjd gdb_flush (raw_stdout); 179131603Spjd do_exec_cleanups (ALL_CLEANUPS); 180131603Spjd } 181131603Spjd else if (target_can_async_p ()) 182131603Spjd { 183131603Spjd add_continuation (mi_interpreter_exec_continuation, NULL); 184131603Spjd } 185131603Spjd} 186131603Spjd 187131603Spjdenum mi_cmd_result 188131603Spjdmi_cmd_interpreter_exec (char *command, char **argv, int argc) 189131603Spjd{ 190131603Spjd struct interp *interp_to_use; 191131603Spjd enum mi_cmd_result result = MI_CMD_DONE; 192131603Spjd int i; 193131603Spjd struct interp_procs *procs; 194131603Spjd 195131603Spjd if (argc < 2) 196131603Spjd { 197131603Spjd xasprintf (&mi_error_message, 198131603Spjd "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command"); 199131603Spjd return MI_CMD_ERROR; 200131603Spjd } 201131603Spjd 202129470Spjd interp_to_use = interp_lookup (argv[0]); 203129470Spjd if (interp_to_use == NULL) 204129470Spjd { 205129470Spjd xasprintf (&mi_error_message, 206129470Spjd "mi_cmd_interpreter_exec: could not find interpreter \"%s\"", 207129470Spjd argv[0]); 208129470Spjd return MI_CMD_ERROR; 209129470Spjd } 210129470Spjd 211129470Spjd if (!interp_exec_p (interp_to_use)) 212129470Spjd { 213129470Spjd xasprintf (&mi_error_message, 214129470Spjd "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution", 215129470Spjd argv[0]); 216129470Spjd return MI_CMD_ERROR; 217130591Spjd } 218130591Spjd 219129470Spjd /* Insert the MI out hooks, making sure to also call the interpreter's hooks 220129470Spjd if it has any. */ 221129470Spjd /* KRS: We shouldn't need this... Events should be installed and they should 222130591Spjd just ALWAYS fire something out down the MI channel... */ 223130591Spjd mi_insert_notify_hooks (); 224129470Spjd 225129470Spjd /* Now run the code... */ 226129470Spjd 227129470Spjd for (i = 1; i < argc; i++) 228129470Spjd { 229129470Spjd char *buff = NULL; 230129470Spjd /* Do this in a cleaner way... We want to force execution to be 231129470Spjd asynchronous for commands that run the target. */ 232129470Spjd if (target_can_async_p () && (strcmp (argv[0], "console") == 0)) 233129470Spjd { 234129470Spjd int len = strlen (argv[i]); 235129470Spjd buff = xmalloc (len + 2); 236129470Spjd memcpy (buff, argv[i], len); 237129470Spjd buff[len] = '&'; 238129470Spjd buff[len + 1] = '\0'; 239129470Spjd } 240129470Spjd 241129470Spjd /* We had to set sync_execution = 0 for the mi (well really for Project 242129470Spjd Builder's use of the mi - particularly so interrupting would work. 243129470Spjd But for console commands to work, we need to initialize it to 1 - 244129470Spjd since that is what the cli expects - before running the command, 245129470Spjd and then set it back to 0 when we are done. */ 246129470Spjd sync_execution = 1; 247129470Spjd if (interp_exec (interp_to_use, argv[i]) < 0) 248129470Spjd { 249129470Spjd mi_error_last_message (); 250129470Spjd result = MI_CMD_ERROR; 251129470Spjd break; 252129470Spjd } 253129470Spjd xfree (buff); 254129470Spjd do_exec_error_cleanups (ALL_CLEANUPS); 255129470Spjd sync_execution = 0; 256129470Spjd } 257129470Spjd 258129470Spjd mi_remove_notify_hooks (); 259129470Spjd 260129470Spjd /* Okay, now let's see if the command set the inferior going... 261129470Spjd Tricky point - have to do this AFTER resetting the interpreter, since 262129470Spjd changing the interpreter will clear out all the continuations for 263130591Spjd that interpreter... */ 264130591Spjd 265129470Spjd if (target_can_async_p () && target_executing) 266129470Spjd { 267129470Spjd fputs_unfiltered ("^running\n", raw_stdout); 268130591Spjd add_continuation (mi_interpreter_exec_continuation, NULL); 269130591Spjd } 270129470Spjd 271129470Spjd return result; 272129470Spjd} 273129470Spjd 274129470Spjd/* 275129470Spjd * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce 276129470Spjd * async-notify ("=") MI messages while running commands in another interpreter 277129470Spjd * using mi_interpreter_exec. The canonical use for this is to allow access to 278129470Spjd * the gdb CLI interpreter from within the MI, while still producing MI style output 279129470Spjd * when actions in the CLI command change gdb's state. 280129470Spjd*/ 281129470Spjd 282129470Spjdstatic void 283129470Spjdmi_insert_notify_hooks (void) 284129470Spjd{ 285129470Spjd query_hook = mi_interp_query_hook; 286129470Spjd} 287129470Spjd 288129470Spjdstatic void 289129470Spjdmi_remove_notify_hooks (void) 290129470Spjd{ 291129470Spjd query_hook = NULL; 292129470Spjd} 293129470Spjd 294129470Spjdstatic int 295129470Spjdmi_interp_query_hook (const char *ctlstr, va_list ap) 296129470Spjd{ 297129470Spjd return 1; 298129470Spjd} 299129470Spjd 300129470Spjdstatic void 301129470Spjdmi_execute_command_wrapper (char *cmd) 302129470Spjd{ 303129470Spjd mi_execute_command (cmd, stdin == instream); 304129470Spjd} 305129470Spjd 306129470Spjdstatic void 307129470Spjdmi1_command_loop (void) 308129470Spjd{ 309129470Spjd mi_command_loop (1); 310129470Spjd} 311129470Spjd 312129470Spjdstatic void 313129470Spjdmi2_command_loop (void) 314129470Spjd{ 315129470Spjd mi_command_loop (2); 316129470Spjd} 317129470Spjd 318153190Spjdstatic void 319153190Spjdmi3_command_loop (void) 320129470Spjd{ 321153190Spjd mi_command_loop (3); 322153190Spjd} 323153190Spjd 324129470Spjdstatic void 325129470Spjdmi_command_loop (int mi_version) 326153190Spjd{ 327129470Spjd#if 0 328153190Spjd /* HACK: Force stdout/stderr to point at the console. This avoids 329153190Spjd any potential side effects caused by legacy code that is still 330129470Spjd using the TUI / fputs_unfiltered_hook */ 331153190Spjd raw_stdout = stdio_fileopen (stdout); 332129470Spjd /* Route normal output through the MIx */ 333153190Spjd gdb_stdout = mi_console_file_new (raw_stdout, "~", '"'); 334153190Spjd /* Route error and log output through the MI */ 335153190Spjd gdb_stderr = mi_console_file_new (raw_stdout, "&", '"'); 336153190Spjd gdb_stdlog = gdb_stderr; 337153190Spjd /* Route target output through the MI. */ 338153190Spjd gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"'); 339153190Spjd /* HACK: Poke the ui_out table directly. Should we be creating a 340153190Spjd mi_out object wired up to the above gdb_stdout / gdb_stderr? */ 341153190Spjd uiout = mi_out_new (mi_version); 342153190Spjd /* HACK: Override any other interpreter hooks. We need to create a 343153190Spjd real event table and pass in that. */ 344153190Spjd init_ui_hook = 0; 345153190Spjd /* command_loop_hook = 0; */ 346153190Spjd print_frame_info_listing_hook = 0; 347153190Spjd query_hook = 0; 348153190Spjd warning_hook = 0; 349153190Spjd create_breakpoint_hook = 0; 350129470Spjd delete_breakpoint_hook = 0; 351129470Spjd modify_breakpoint_hook = 0; 352153190Spjd interactive_hook = 0; 353153190Spjd registers_changed_hook = 0; 354129470Spjd readline_begin_hook = 0; 355129470Spjd readline_hook = 0; 356153190Spjd readline_end_hook = 0; 357153190Spjd register_changed_hook = 0; 358129470Spjd memory_changed_hook = 0; 359153190Spjd context_hook = 0; 360153190Spjd target_wait_hook = 0; 361129470Spjd call_command_hook = 0; 362153190Spjd error_hook = 0; 363153190Spjd error_begin_hook = 0; 364153190Spjd show_load_progress = mi_load_progress; 365153190Spjd#endif 366129470Spjd /* Turn off 8 bit strings in quoted output. Any character with the 367129470Spjd high bit set is printed using C's octal format. */ 368153190Spjd sevenbit_strings = 1; 369153190Spjd /* Tell the world that we're alive */ 370129470Spjd fputs_unfiltered ("(gdb) \n", raw_stdout); 371153190Spjd gdb_flush (raw_stdout); 372153190Spjd if (!event_loop_p) 373129470Spjd simplified_command_loop (mi_input, mi_execute_command); 374153190Spjd else 375153190Spjd start_event_loop (); 376153190Spjd} 377153190Spjd 378153190Spjdstatic char * 379153190Spjdmi_input (char *buf) 380153190Spjd{ 381153190Spjd return gdb_readline (NULL); 382153190Spjd} 383153190Spjd 384153190Spjdextern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */ 385153190Spjd 386153190Spjdvoid 387153190Spjd_initialize_mi_interp (void) 388153190Spjd{ 389129470Spjd static const struct interp_procs procs = 390129470Spjd { 391166215Spjd mi_interpreter_init, /* init_proc */ 392166215Spjd mi_interpreter_resume, /* resume_proc */ 393166215Spjd mi_interpreter_suspend, /* suspend_proc */ 394166215Spjd mi_interpreter_exec, /* exec_proc */ 395166215Spjd mi_interpreter_prompt_p /* prompt_proc_p */ 396166215Spjd }; 397166215Spjd 398166215Spjd /* The various interpreter levels. */ 399166215Spjd interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs)); 400166215Spjd interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs)); 401166215Spjd interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs)); 402166215Spjd 403166215Spjd /* "mi" selects the most recent released version. "mi2" was 404166215Spjd released as part of GDB 6.0. */ 405166215Spjd interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs)); 406166215Spjd} 407166215Spjd