15796c8dcSSimon Schubert /* MI Interpreter Definitions and Commands for GDB, the GNU debugger. 25796c8dcSSimon Schubert 3*cf7f2e2dSJohn Marino Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 45796c8dcSSimon Schubert Free Software Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of GDB. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 115796c8dcSSimon Schubert (at your option) any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165796c8dcSSimon Schubert GNU General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "gdb_string.h" 235796c8dcSSimon Schubert #include "interps.h" 245796c8dcSSimon Schubert #include "event-top.h" 255796c8dcSSimon Schubert #include "event-loop.h" 265796c8dcSSimon Schubert #include "inferior.h" 275796c8dcSSimon Schubert #include "ui-out.h" 285796c8dcSSimon Schubert #include "top.h" 295796c8dcSSimon Schubert #include "exceptions.h" 305796c8dcSSimon Schubert #include "mi-main.h" 315796c8dcSSimon Schubert #include "mi-cmds.h" 325796c8dcSSimon Schubert #include "mi-out.h" 335796c8dcSSimon Schubert #include "mi-console.h" 345796c8dcSSimon Schubert #include "mi-common.h" 355796c8dcSSimon Schubert #include "observer.h" 365796c8dcSSimon Schubert #include "gdbthread.h" 375796c8dcSSimon Schubert #include "solist.h" 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert /* These are the interpreter setup, etc. functions for the MI interpreter */ 405796c8dcSSimon Schubert static void mi_execute_command_wrapper (char *cmd); 415796c8dcSSimon Schubert static void mi_command_loop (int mi_version); 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert /* These are hooks that we put in place while doing interpreter_exec 445796c8dcSSimon Schubert so we can report interesting things that happened "behind the mi's 455796c8dcSSimon Schubert back" in this command */ 465796c8dcSSimon Schubert static int mi_interp_query_hook (const char *ctlstr, va_list ap) 47*cf7f2e2dSJohn Marino ATTRIBUTE_PRINTF (1, 0); 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert static void mi3_command_loop (void); 505796c8dcSSimon Schubert static void mi2_command_loop (void); 515796c8dcSSimon Schubert static void mi1_command_loop (void); 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert static void mi_insert_notify_hooks (void); 545796c8dcSSimon Schubert static void mi_remove_notify_hooks (void); 555796c8dcSSimon Schubert static void mi_on_normal_stop (struct bpstats *bs, int print_frame); 565796c8dcSSimon Schubert 575796c8dcSSimon Schubert static void mi_new_thread (struct thread_info *t); 585796c8dcSSimon Schubert static void mi_thread_exit (struct thread_info *t, int silent); 59*cf7f2e2dSJohn Marino static void mi_inferior_added (struct inferior *inf); 60*cf7f2e2dSJohn Marino static void mi_inferior_appeared (struct inferior *inf); 61*cf7f2e2dSJohn Marino static void mi_inferior_exit (struct inferior *inf); 62*cf7f2e2dSJohn Marino static void mi_inferior_removed (struct inferior *inf); 635796c8dcSSimon Schubert static void mi_on_resume (ptid_t ptid); 645796c8dcSSimon Schubert static void mi_solib_loaded (struct so_list *solib); 655796c8dcSSimon Schubert static void mi_solib_unloaded (struct so_list *solib); 665796c8dcSSimon Schubert static void mi_about_to_proceed (void); 675796c8dcSSimon Schubert 68*cf7f2e2dSJohn Marino static int report_initial_inferior (struct inferior *inf, void *closure); 69*cf7f2e2dSJohn Marino 705796c8dcSSimon Schubert static void * 715796c8dcSSimon Schubert mi_interpreter_init (int top_level) 725796c8dcSSimon Schubert { 735796c8dcSSimon Schubert struct mi_interp *mi = XMALLOC (struct mi_interp); 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert /* HACK: We need to force stdout/stderr to point at the console. This avoids 765796c8dcSSimon Schubert any potential side effects caused by legacy code that is still 775796c8dcSSimon Schubert using the TUI / fputs_unfiltered_hook. So we set up output channels for 785796c8dcSSimon Schubert this now, and swap them in when we are run. */ 795796c8dcSSimon Schubert 805796c8dcSSimon Schubert raw_stdout = stdio_fileopen (stdout); 815796c8dcSSimon Schubert 825796c8dcSSimon Schubert /* Create MI channels */ 835796c8dcSSimon Schubert mi->out = mi_console_file_new (raw_stdout, "~", '"'); 845796c8dcSSimon Schubert mi->err = mi_console_file_new (raw_stdout, "&", '"'); 855796c8dcSSimon Schubert mi->log = mi->err; 865796c8dcSSimon Schubert mi->targ = mi_console_file_new (raw_stdout, "@", '"'); 875796c8dcSSimon Schubert mi->event_channel = mi_console_file_new (raw_stdout, "=", 0); 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert if (top_level) 905796c8dcSSimon Schubert { 915796c8dcSSimon Schubert observer_attach_new_thread (mi_new_thread); 925796c8dcSSimon Schubert observer_attach_thread_exit (mi_thread_exit); 93*cf7f2e2dSJohn Marino observer_attach_inferior_added (mi_inferior_added); 94*cf7f2e2dSJohn Marino observer_attach_inferior_appeared (mi_inferior_appeared); 955796c8dcSSimon Schubert observer_attach_inferior_exit (mi_inferior_exit); 96*cf7f2e2dSJohn Marino observer_attach_inferior_removed (mi_inferior_removed); 975796c8dcSSimon Schubert observer_attach_normal_stop (mi_on_normal_stop); 985796c8dcSSimon Schubert observer_attach_target_resumed (mi_on_resume); 995796c8dcSSimon Schubert observer_attach_solib_loaded (mi_solib_loaded); 1005796c8dcSSimon Schubert observer_attach_solib_unloaded (mi_solib_unloaded); 1015796c8dcSSimon Schubert observer_attach_about_to_proceed (mi_about_to_proceed); 102*cf7f2e2dSJohn Marino 103*cf7f2e2dSJohn Marino /* The initial inferior is created before this function is called, so we 104*cf7f2e2dSJohn Marino need to report it explicitly. Use iteration in case future version 105*cf7f2e2dSJohn Marino of GDB creates more than one inferior up-front. */ 106*cf7f2e2dSJohn Marino iterate_over_inferiors (report_initial_inferior, mi); 1075796c8dcSSimon Schubert } 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert return mi; 1105796c8dcSSimon Schubert } 1115796c8dcSSimon Schubert 1125796c8dcSSimon Schubert static int 1135796c8dcSSimon Schubert mi_interpreter_resume (void *data) 1145796c8dcSSimon Schubert { 1155796c8dcSSimon Schubert struct mi_interp *mi = data; 1165796c8dcSSimon Schubert 117*cf7f2e2dSJohn Marino /* As per hack note in mi_interpreter_init, swap in the output channels... */ 1185796c8dcSSimon Schubert gdb_setup_readline (); 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert /* These overwrite some of the initialization done in 1215796c8dcSSimon Schubert _intialize_event_loop. */ 1225796c8dcSSimon Schubert call_readline = gdb_readline2; 1235796c8dcSSimon Schubert input_handler = mi_execute_command_wrapper; 1245796c8dcSSimon Schubert add_file_handler (input_fd, stdin_event_handler, 0); 1255796c8dcSSimon Schubert async_command_editing_p = 0; 1265796c8dcSSimon Schubert /* FIXME: This is a total hack for now. PB's use of the MI 1275796c8dcSSimon Schubert implicitly relies on a bug in the async support which allows 1285796c8dcSSimon Schubert asynchronous commands to leak through the commmand loop. The bug 1295796c8dcSSimon Schubert involves (but is not limited to) the fact that sync_execution was 1305796c8dcSSimon Schubert erroneously initialized to 0. Duplicate by initializing it thus 1315796c8dcSSimon Schubert here... */ 1325796c8dcSSimon Schubert sync_execution = 0; 1335796c8dcSSimon Schubert 1345796c8dcSSimon Schubert gdb_stdout = mi->out; 1355796c8dcSSimon Schubert /* Route error and log output through the MI */ 1365796c8dcSSimon Schubert gdb_stderr = mi->err; 1375796c8dcSSimon Schubert gdb_stdlog = mi->log; 1385796c8dcSSimon Schubert /* Route target output through the MI. */ 1395796c8dcSSimon Schubert gdb_stdtarg = mi->targ; 1405796c8dcSSimon Schubert /* Route target error through the MI as well. */ 1415796c8dcSSimon Schubert gdb_stdtargerr = mi->targ; 1425796c8dcSSimon Schubert 1435796c8dcSSimon Schubert /* Replace all the hooks that we know about. There really needs to 1445796c8dcSSimon Schubert be a better way of doing this... */ 1455796c8dcSSimon Schubert clear_interpreter_hooks (); 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert deprecated_show_load_progress = mi_load_progress; 1485796c8dcSSimon Schubert 1495796c8dcSSimon Schubert /* If we're _the_ interpreter, take control. */ 1505796c8dcSSimon Schubert if (current_interp_named_p (INTERP_MI1)) 1515796c8dcSSimon Schubert deprecated_command_loop_hook = mi1_command_loop; 1525796c8dcSSimon Schubert else if (current_interp_named_p (INTERP_MI2)) 1535796c8dcSSimon Schubert deprecated_command_loop_hook = mi2_command_loop; 1545796c8dcSSimon Schubert else if (current_interp_named_p (INTERP_MI3)) 1555796c8dcSSimon Schubert deprecated_command_loop_hook = mi3_command_loop; 1565796c8dcSSimon Schubert else 1575796c8dcSSimon Schubert deprecated_command_loop_hook = mi2_command_loop; 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert return 1; 1605796c8dcSSimon Schubert } 1615796c8dcSSimon Schubert 1625796c8dcSSimon Schubert static int 1635796c8dcSSimon Schubert mi_interpreter_suspend (void *data) 1645796c8dcSSimon Schubert { 1655796c8dcSSimon Schubert gdb_disable_readline (); 1665796c8dcSSimon Schubert return 1; 1675796c8dcSSimon Schubert } 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert static struct gdb_exception 1705796c8dcSSimon Schubert mi_interpreter_exec (void *data, const char *command) 1715796c8dcSSimon Schubert { 1725796c8dcSSimon Schubert char *tmp = alloca (strlen (command) + 1); 173*cf7f2e2dSJohn Marino 1745796c8dcSSimon Schubert strcpy (tmp, command); 1755796c8dcSSimon Schubert mi_execute_command_wrapper (tmp); 1765796c8dcSSimon Schubert return exception_none; 1775796c8dcSSimon Schubert } 1785796c8dcSSimon Schubert 1795796c8dcSSimon Schubert /* Never display the default gdb prompt in mi case. */ 1805796c8dcSSimon Schubert static int 1815796c8dcSSimon Schubert mi_interpreter_prompt_p (void *data) 1825796c8dcSSimon Schubert { 1835796c8dcSSimon Schubert return 0; 1845796c8dcSSimon Schubert } 1855796c8dcSSimon Schubert 1865796c8dcSSimon Schubert void 1875796c8dcSSimon Schubert mi_cmd_interpreter_exec (char *command, char **argv, int argc) 1885796c8dcSSimon Schubert { 1895796c8dcSSimon Schubert struct interp *interp_to_use; 1905796c8dcSSimon Schubert int i; 1915796c8dcSSimon Schubert char *mi_error_message = NULL; 1925796c8dcSSimon Schubert struct cleanup *old_chain; 1935796c8dcSSimon Schubert 1945796c8dcSSimon Schubert if (argc < 2) 1955796c8dcSSimon Schubert error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command"); 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert interp_to_use = interp_lookup (argv[0]); 1985796c8dcSSimon Schubert if (interp_to_use == NULL) 1995796c8dcSSimon Schubert error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]); 2005796c8dcSSimon Schubert 2015796c8dcSSimon Schubert if (!interp_exec_p (interp_to_use)) 2025796c8dcSSimon Schubert error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution", 2035796c8dcSSimon Schubert argv[0]); 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert /* Insert the MI out hooks, making sure to also call the interpreter's hooks 2065796c8dcSSimon Schubert if it has any. */ 2075796c8dcSSimon Schubert /* KRS: We shouldn't need this... Events should be installed and they should 2085796c8dcSSimon Schubert just ALWAYS fire something out down the MI channel... */ 2095796c8dcSSimon Schubert mi_insert_notify_hooks (); 2105796c8dcSSimon Schubert 2115796c8dcSSimon Schubert /* Now run the code... */ 2125796c8dcSSimon Schubert 2135796c8dcSSimon Schubert old_chain = make_cleanup (null_cleanup, 0); 2145796c8dcSSimon Schubert for (i = 1; i < argc; i++) 2155796c8dcSSimon Schubert { 2165796c8dcSSimon Schubert struct gdb_exception e = interp_exec (interp_to_use, argv[i]); 217*cf7f2e2dSJohn Marino 2185796c8dcSSimon Schubert if (e.reason < 0) 2195796c8dcSSimon Schubert { 2205796c8dcSSimon Schubert mi_error_message = xstrdup (e.message); 2215796c8dcSSimon Schubert make_cleanup (xfree, mi_error_message); 2225796c8dcSSimon Schubert break; 2235796c8dcSSimon Schubert } 2245796c8dcSSimon Schubert } 2255796c8dcSSimon Schubert 2265796c8dcSSimon Schubert mi_remove_notify_hooks (); 2275796c8dcSSimon Schubert 2285796c8dcSSimon Schubert if (mi_error_message != NULL) 2295796c8dcSSimon Schubert error ("%s", mi_error_message); 2305796c8dcSSimon Schubert do_cleanups (old_chain); 2315796c8dcSSimon Schubert } 2325796c8dcSSimon Schubert 2335796c8dcSSimon Schubert /* 2345796c8dcSSimon Schubert * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce 2355796c8dcSSimon Schubert * async-notify ("=") MI messages while running commands in another interpreter 2365796c8dcSSimon Schubert * using mi_interpreter_exec. The canonical use for this is to allow access to 2375796c8dcSSimon Schubert * the gdb CLI interpreter from within the MI, while still producing MI style output 2385796c8dcSSimon Schubert * when actions in the CLI command change gdb's state. 2395796c8dcSSimon Schubert */ 2405796c8dcSSimon Schubert 2415796c8dcSSimon Schubert static void 2425796c8dcSSimon Schubert mi_insert_notify_hooks (void) 2435796c8dcSSimon Schubert { 2445796c8dcSSimon Schubert deprecated_query_hook = mi_interp_query_hook; 2455796c8dcSSimon Schubert } 2465796c8dcSSimon Schubert 2475796c8dcSSimon Schubert static void 2485796c8dcSSimon Schubert mi_remove_notify_hooks (void) 2495796c8dcSSimon Schubert { 2505796c8dcSSimon Schubert deprecated_query_hook = NULL; 2515796c8dcSSimon Schubert } 2525796c8dcSSimon Schubert 2535796c8dcSSimon Schubert static int 2545796c8dcSSimon Schubert mi_interp_query_hook (const char *ctlstr, va_list ap) 2555796c8dcSSimon Schubert { 2565796c8dcSSimon Schubert return 1; 2575796c8dcSSimon Schubert } 2585796c8dcSSimon Schubert 2595796c8dcSSimon Schubert static void 2605796c8dcSSimon Schubert mi_execute_command_wrapper (char *cmd) 2615796c8dcSSimon Schubert { 2625796c8dcSSimon Schubert mi_execute_command (cmd, stdin == instream); 2635796c8dcSSimon Schubert } 2645796c8dcSSimon Schubert 2655796c8dcSSimon Schubert static void 2665796c8dcSSimon Schubert mi1_command_loop (void) 2675796c8dcSSimon Schubert { 2685796c8dcSSimon Schubert mi_command_loop (1); 2695796c8dcSSimon Schubert } 2705796c8dcSSimon Schubert 2715796c8dcSSimon Schubert static void 2725796c8dcSSimon Schubert mi2_command_loop (void) 2735796c8dcSSimon Schubert { 2745796c8dcSSimon Schubert mi_command_loop (2); 2755796c8dcSSimon Schubert } 2765796c8dcSSimon Schubert 2775796c8dcSSimon Schubert static void 2785796c8dcSSimon Schubert mi3_command_loop (void) 2795796c8dcSSimon Schubert { 2805796c8dcSSimon Schubert mi_command_loop (3); 2815796c8dcSSimon Schubert } 2825796c8dcSSimon Schubert 2835796c8dcSSimon Schubert static void 2845796c8dcSSimon Schubert mi_command_loop (int mi_version) 2855796c8dcSSimon Schubert { 2865796c8dcSSimon Schubert /* Turn off 8 bit strings in quoted output. Any character with the 2875796c8dcSSimon Schubert high bit set is printed using C's octal format. */ 2885796c8dcSSimon Schubert sevenbit_strings = 1; 2895796c8dcSSimon Schubert /* Tell the world that we're alive */ 2905796c8dcSSimon Schubert fputs_unfiltered ("(gdb) \n", raw_stdout); 2915796c8dcSSimon Schubert gdb_flush (raw_stdout); 2925796c8dcSSimon Schubert start_event_loop (); 2935796c8dcSSimon Schubert } 2945796c8dcSSimon Schubert 2955796c8dcSSimon Schubert static void 2965796c8dcSSimon Schubert mi_new_thread (struct thread_info *t) 2975796c8dcSSimon Schubert { 2985796c8dcSSimon Schubert struct mi_interp *mi = top_level_interpreter_data (); 299*cf7f2e2dSJohn Marino struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid)); 300*cf7f2e2dSJohn Marino 301*cf7f2e2dSJohn Marino gdb_assert (inf); 3025796c8dcSSimon Schubert 3035796c8dcSSimon Schubert fprintf_unfiltered (mi->event_channel, 304*cf7f2e2dSJohn Marino "thread-created,id=\"%d\",group-id=\"i%d\"", 305*cf7f2e2dSJohn Marino t->num, inf->num); 3065796c8dcSSimon Schubert gdb_flush (mi->event_channel); 3075796c8dcSSimon Schubert } 3085796c8dcSSimon Schubert 3095796c8dcSSimon Schubert static void 3105796c8dcSSimon Schubert mi_thread_exit (struct thread_info *t, int silent) 3115796c8dcSSimon Schubert { 3125796c8dcSSimon Schubert struct mi_interp *mi; 313*cf7f2e2dSJohn Marino struct inferior *inf; 3145796c8dcSSimon Schubert 3155796c8dcSSimon Schubert if (silent) 3165796c8dcSSimon Schubert return; 3175796c8dcSSimon Schubert 318*cf7f2e2dSJohn Marino inf = find_inferior_pid (ptid_get_pid (t->ptid)); 319*cf7f2e2dSJohn Marino 3205796c8dcSSimon Schubert mi = top_level_interpreter_data (); 3215796c8dcSSimon Schubert target_terminal_ours (); 3225796c8dcSSimon Schubert fprintf_unfiltered (mi->event_channel, 323*cf7f2e2dSJohn Marino "thread-exited,id=\"%d\",group-id=\"i%d\"", 324*cf7f2e2dSJohn Marino t->num, inf->num); 3255796c8dcSSimon Schubert gdb_flush (mi->event_channel); 3265796c8dcSSimon Schubert } 3275796c8dcSSimon Schubert 3285796c8dcSSimon Schubert static void 329*cf7f2e2dSJohn Marino mi_inferior_added (struct inferior *inf) 3305796c8dcSSimon Schubert { 3315796c8dcSSimon Schubert struct mi_interp *mi = top_level_interpreter_data (); 332*cf7f2e2dSJohn Marino 3335796c8dcSSimon Schubert target_terminal_ours (); 334*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 335*cf7f2e2dSJohn Marino "thread-group-added,id=\"i%d\"", 336*cf7f2e2dSJohn Marino inf->num); 3375796c8dcSSimon Schubert gdb_flush (mi->event_channel); 3385796c8dcSSimon Schubert } 3395796c8dcSSimon Schubert 3405796c8dcSSimon Schubert static void 341*cf7f2e2dSJohn Marino mi_inferior_appeared (struct inferior *inf) 3425796c8dcSSimon Schubert { 3435796c8dcSSimon Schubert struct mi_interp *mi = top_level_interpreter_data (); 344*cf7f2e2dSJohn Marino 3455796c8dcSSimon Schubert target_terminal_ours (); 346*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 347*cf7f2e2dSJohn Marino "thread-group-started,id=\"i%d\",pid=\"%d\"", 348*cf7f2e2dSJohn Marino inf->num, inf->pid); 349*cf7f2e2dSJohn Marino gdb_flush (mi->event_channel); 350*cf7f2e2dSJohn Marino } 351*cf7f2e2dSJohn Marino 352*cf7f2e2dSJohn Marino static void 353*cf7f2e2dSJohn Marino mi_inferior_exit (struct inferior *inf) 354*cf7f2e2dSJohn Marino { 355*cf7f2e2dSJohn Marino struct mi_interp *mi = top_level_interpreter_data (); 356*cf7f2e2dSJohn Marino 357*cf7f2e2dSJohn Marino target_terminal_ours (); 358*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"i%d\"", 359*cf7f2e2dSJohn Marino inf->num); 360*cf7f2e2dSJohn Marino gdb_flush (mi->event_channel); 361*cf7f2e2dSJohn Marino } 362*cf7f2e2dSJohn Marino 363*cf7f2e2dSJohn Marino static void 364*cf7f2e2dSJohn Marino mi_inferior_removed (struct inferior *inf) 365*cf7f2e2dSJohn Marino { 366*cf7f2e2dSJohn Marino struct mi_interp *mi = top_level_interpreter_data (); 367*cf7f2e2dSJohn Marino 368*cf7f2e2dSJohn Marino target_terminal_ours (); 369*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 370*cf7f2e2dSJohn Marino "thread-group-removed,id=\"i%d\"", 371*cf7f2e2dSJohn Marino inf->num); 3725796c8dcSSimon Schubert gdb_flush (mi->event_channel); 3735796c8dcSSimon Schubert } 3745796c8dcSSimon Schubert 3755796c8dcSSimon Schubert static void 3765796c8dcSSimon Schubert mi_on_normal_stop (struct bpstats *bs, int print_frame) 3775796c8dcSSimon Schubert { 3785796c8dcSSimon Schubert /* Since this can be called when CLI command is executing, 3795796c8dcSSimon Schubert using cli interpreter, be sure to use MI uiout for output, 3805796c8dcSSimon Schubert not the current one. */ 3815796c8dcSSimon Schubert struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ()); 3825796c8dcSSimon Schubert 3835796c8dcSSimon Schubert if (print_frame) 3845796c8dcSSimon Schubert { 385*cf7f2e2dSJohn Marino int core; 386*cf7f2e2dSJohn Marino 3875796c8dcSSimon Schubert if (uiout != mi_uiout) 3885796c8dcSSimon Schubert { 3895796c8dcSSimon Schubert /* The normal_stop function has printed frame information into 3905796c8dcSSimon Schubert CLI uiout, or some other non-MI uiout. There's no way we 3915796c8dcSSimon Schubert can extract proper fields from random uiout object, so we print 3925796c8dcSSimon Schubert the frame again. In practice, this can only happen when running 3935796c8dcSSimon Schubert a CLI command in MI. */ 3945796c8dcSSimon Schubert struct ui_out *saved_uiout = uiout; 395*cf7f2e2dSJohn Marino 3965796c8dcSSimon Schubert uiout = mi_uiout; 3975796c8dcSSimon Schubert print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC); 3985796c8dcSSimon Schubert uiout = saved_uiout; 3995796c8dcSSimon Schubert } 4005796c8dcSSimon Schubert 4015796c8dcSSimon Schubert ui_out_field_int (mi_uiout, "thread-id", 4025796c8dcSSimon Schubert pid_to_thread_id (inferior_ptid)); 4035796c8dcSSimon Schubert if (non_stop) 4045796c8dcSSimon Schubert { 4055796c8dcSSimon Schubert struct cleanup *back_to = make_cleanup_ui_out_list_begin_end 4065796c8dcSSimon Schubert (mi_uiout, "stopped-threads"); 407*cf7f2e2dSJohn Marino 4085796c8dcSSimon Schubert ui_out_field_int (mi_uiout, NULL, 4095796c8dcSSimon Schubert pid_to_thread_id (inferior_ptid)); 4105796c8dcSSimon Schubert do_cleanups (back_to); 4115796c8dcSSimon Schubert } 4125796c8dcSSimon Schubert else 4135796c8dcSSimon Schubert ui_out_field_string (mi_uiout, "stopped-threads", "all"); 414*cf7f2e2dSJohn Marino 415*cf7f2e2dSJohn Marino core = target_core_of_thread (inferior_ptid); 416*cf7f2e2dSJohn Marino if (core != -1) 417*cf7f2e2dSJohn Marino ui_out_field_int (mi_uiout, "core", core); 4185796c8dcSSimon Schubert } 4195796c8dcSSimon Schubert 4205796c8dcSSimon Schubert fputs_unfiltered ("*stopped", raw_stdout); 4215796c8dcSSimon Schubert mi_out_put (mi_uiout, raw_stdout); 4225796c8dcSSimon Schubert mi_out_rewind (mi_uiout); 4235796c8dcSSimon Schubert mi_print_timing_maybe (); 4245796c8dcSSimon Schubert fputs_unfiltered ("\n", raw_stdout); 4255796c8dcSSimon Schubert gdb_flush (raw_stdout); 4265796c8dcSSimon Schubert } 4275796c8dcSSimon Schubert 4285796c8dcSSimon Schubert static void 4295796c8dcSSimon Schubert mi_about_to_proceed (void) 4305796c8dcSSimon Schubert { 4315796c8dcSSimon Schubert /* Suppress output while calling an inferior function. */ 4325796c8dcSSimon Schubert 4335796c8dcSSimon Schubert if (!ptid_equal (inferior_ptid, null_ptid)) 4345796c8dcSSimon Schubert { 4355796c8dcSSimon Schubert struct thread_info *tp = inferior_thread (); 436*cf7f2e2dSJohn Marino 4375796c8dcSSimon Schubert if (tp->in_infcall) 4385796c8dcSSimon Schubert return; 4395796c8dcSSimon Schubert } 4405796c8dcSSimon Schubert 4415796c8dcSSimon Schubert mi_proceeded = 1; 4425796c8dcSSimon Schubert } 4435796c8dcSSimon Schubert 4445796c8dcSSimon Schubert static int 4455796c8dcSSimon Schubert mi_output_running_pid (struct thread_info *info, void *arg) 4465796c8dcSSimon Schubert { 4475796c8dcSSimon Schubert ptid_t *ptid = arg; 4485796c8dcSSimon Schubert 4495796c8dcSSimon Schubert if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid)) 4505796c8dcSSimon Schubert fprintf_unfiltered (raw_stdout, 4515796c8dcSSimon Schubert "*running,thread-id=\"%d\"\n", 4525796c8dcSSimon Schubert info->num); 4535796c8dcSSimon Schubert 4545796c8dcSSimon Schubert return 0; 4555796c8dcSSimon Schubert } 4565796c8dcSSimon Schubert 4575796c8dcSSimon Schubert static int 4585796c8dcSSimon Schubert mi_inferior_count (struct inferior *inf, void *arg) 4595796c8dcSSimon Schubert { 4605796c8dcSSimon Schubert if (inf->pid != 0) 4615796c8dcSSimon Schubert { 4625796c8dcSSimon Schubert int *count_p = arg; 4635796c8dcSSimon Schubert (*count_p)++; 4645796c8dcSSimon Schubert } 4655796c8dcSSimon Schubert 4665796c8dcSSimon Schubert return 0; 4675796c8dcSSimon Schubert } 4685796c8dcSSimon Schubert 4695796c8dcSSimon Schubert static void 4705796c8dcSSimon Schubert mi_on_resume (ptid_t ptid) 4715796c8dcSSimon Schubert { 4725796c8dcSSimon Schubert struct thread_info *tp = NULL; 4735796c8dcSSimon Schubert 4745796c8dcSSimon Schubert if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid)) 4755796c8dcSSimon Schubert tp = inferior_thread (); 4765796c8dcSSimon Schubert else 4775796c8dcSSimon Schubert tp = find_thread_ptid (ptid); 4785796c8dcSSimon Schubert 4795796c8dcSSimon Schubert /* Suppress output while calling an inferior function. */ 4805796c8dcSSimon Schubert if (tp->in_infcall) 4815796c8dcSSimon Schubert return; 4825796c8dcSSimon Schubert 4835796c8dcSSimon Schubert /* To cater for older frontends, emit ^running, but do it only once 4845796c8dcSSimon Schubert per each command. We do it here, since at this point we know 4855796c8dcSSimon Schubert that the target was successfully resumed, and in non-async mode, 4865796c8dcSSimon Schubert we won't return back to MI interpreter code until the target 4875796c8dcSSimon Schubert is done running, so delaying the output of "^running" until then 4885796c8dcSSimon Schubert will make it impossible for frontend to know what's going on. 4895796c8dcSSimon Schubert 4905796c8dcSSimon Schubert In future (MI3), we'll be outputting "^done" here. */ 4915796c8dcSSimon Schubert if (!running_result_record_printed && mi_proceeded) 4925796c8dcSSimon Schubert { 493*cf7f2e2dSJohn Marino fprintf_unfiltered (raw_stdout, "%s^running\n", 494*cf7f2e2dSJohn Marino current_token ? current_token : ""); 4955796c8dcSSimon Schubert } 4965796c8dcSSimon Schubert 4975796c8dcSSimon Schubert if (PIDGET (ptid) == -1) 4985796c8dcSSimon Schubert fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n"); 4995796c8dcSSimon Schubert else if (ptid_is_pid (ptid)) 5005796c8dcSSimon Schubert { 5015796c8dcSSimon Schubert int count = 0; 5025796c8dcSSimon Schubert 5035796c8dcSSimon Schubert /* Backwards compatibility. If there's only one inferior, 5045796c8dcSSimon Schubert output "all", otherwise, output each resumed thread 5055796c8dcSSimon Schubert individually. */ 5065796c8dcSSimon Schubert iterate_over_inferiors (mi_inferior_count, &count); 5075796c8dcSSimon Schubert 5085796c8dcSSimon Schubert if (count == 1) 5095796c8dcSSimon Schubert fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n"); 5105796c8dcSSimon Schubert else 5115796c8dcSSimon Schubert iterate_over_threads (mi_output_running_pid, &ptid); 5125796c8dcSSimon Schubert } 5135796c8dcSSimon Schubert else 5145796c8dcSSimon Schubert { 5155796c8dcSSimon Schubert struct thread_info *ti = find_thread_ptid (ptid); 516*cf7f2e2dSJohn Marino 5175796c8dcSSimon Schubert gdb_assert (ti); 5185796c8dcSSimon Schubert fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num); 5195796c8dcSSimon Schubert } 5205796c8dcSSimon Schubert 5215796c8dcSSimon Schubert if (!running_result_record_printed && mi_proceeded) 5225796c8dcSSimon Schubert { 5235796c8dcSSimon Schubert running_result_record_printed = 1; 5245796c8dcSSimon Schubert /* This is what gdb used to do historically -- printing prompt even if 5255796c8dcSSimon Schubert it cannot actually accept any input. This will be surely removed 5265796c8dcSSimon Schubert for MI3, and may be removed even earler. */ 5275796c8dcSSimon Schubert /* FIXME: review the use of target_is_async_p here -- is that 5285796c8dcSSimon Schubert what we want? */ 5295796c8dcSSimon Schubert if (!target_is_async_p ()) 5305796c8dcSSimon Schubert fputs_unfiltered ("(gdb) \n", raw_stdout); 5315796c8dcSSimon Schubert } 5325796c8dcSSimon Schubert gdb_flush (raw_stdout); 5335796c8dcSSimon Schubert } 5345796c8dcSSimon Schubert 5355796c8dcSSimon Schubert static void 5365796c8dcSSimon Schubert mi_solib_loaded (struct so_list *solib) 5375796c8dcSSimon Schubert { 5385796c8dcSSimon Schubert struct mi_interp *mi = top_level_interpreter_data (); 539*cf7f2e2dSJohn Marino 5405796c8dcSSimon Schubert target_terminal_ours (); 541*cf7f2e2dSJohn Marino if (gdbarch_has_global_solist (target_gdbarch)) 5425796c8dcSSimon Schubert fprintf_unfiltered (mi->event_channel, 543*cf7f2e2dSJohn Marino "library-loaded,id=\"%s\",target-name=\"%s\"," 544*cf7f2e2dSJohn Marino "host-name=\"%s\",symbols-loaded=\"%d\"", 5455796c8dcSSimon Schubert solib->so_original_name, solib->so_original_name, 5465796c8dcSSimon Schubert solib->so_name, solib->symbols_loaded); 547*cf7f2e2dSJohn Marino else 548*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 549*cf7f2e2dSJohn Marino "library-loaded,id=\"%s\",target-name=\"%s\"," 550*cf7f2e2dSJohn Marino "host-name=\"%s\",symbols-loaded=\"%d\"," 551*cf7f2e2dSJohn Marino "thread-group=\"i%d\"", 552*cf7f2e2dSJohn Marino solib->so_original_name, solib->so_original_name, 553*cf7f2e2dSJohn Marino solib->so_name, solib->symbols_loaded, 554*cf7f2e2dSJohn Marino current_inferior ()->num); 555*cf7f2e2dSJohn Marino 5565796c8dcSSimon Schubert gdb_flush (mi->event_channel); 5575796c8dcSSimon Schubert } 5585796c8dcSSimon Schubert 5595796c8dcSSimon Schubert static void 5605796c8dcSSimon Schubert mi_solib_unloaded (struct so_list *solib) 5615796c8dcSSimon Schubert { 5625796c8dcSSimon Schubert struct mi_interp *mi = top_level_interpreter_data (); 563*cf7f2e2dSJohn Marino 5645796c8dcSSimon Schubert target_terminal_ours (); 565*cf7f2e2dSJohn Marino if (gdbarch_has_global_solist (target_gdbarch)) 5665796c8dcSSimon Schubert fprintf_unfiltered (mi->event_channel, 567*cf7f2e2dSJohn Marino "library-unloaded,id=\"%s\",target-name=\"%s\"," 568*cf7f2e2dSJohn Marino "host-name=\"%s\"", 5695796c8dcSSimon Schubert solib->so_original_name, solib->so_original_name, 5705796c8dcSSimon Schubert solib->so_name); 571*cf7f2e2dSJohn Marino else 572*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 573*cf7f2e2dSJohn Marino "library-unloaded,id=\"%s\",target-name=\"%s\"," 574*cf7f2e2dSJohn Marino "host-name=\"%s\",thread-group=\"i%d\"", 575*cf7f2e2dSJohn Marino solib->so_original_name, solib->so_original_name, 576*cf7f2e2dSJohn Marino solib->so_name, current_inferior ()->num); 577*cf7f2e2dSJohn Marino 5785796c8dcSSimon Schubert gdb_flush (mi->event_channel); 5795796c8dcSSimon Schubert } 5805796c8dcSSimon Schubert 581*cf7f2e2dSJohn Marino static int 582*cf7f2e2dSJohn Marino report_initial_inferior (struct inferior *inf, void *closure) 583*cf7f2e2dSJohn Marino { 584*cf7f2e2dSJohn Marino /* This function is called from mi_intepreter_init, and since 585*cf7f2e2dSJohn Marino mi_inferior_added assumes that inferior is fully initialized 586*cf7f2e2dSJohn Marino and top_level_interpreter_data is set, we cannot call 587*cf7f2e2dSJohn Marino it here. */ 588*cf7f2e2dSJohn Marino struct mi_interp *mi = closure; 589*cf7f2e2dSJohn Marino 590*cf7f2e2dSJohn Marino target_terminal_ours (); 591*cf7f2e2dSJohn Marino fprintf_unfiltered (mi->event_channel, 592*cf7f2e2dSJohn Marino "thread-group-added,id=\"i%d\"", 593*cf7f2e2dSJohn Marino inf->num); 594*cf7f2e2dSJohn Marino gdb_flush (mi->event_channel); 595*cf7f2e2dSJohn Marino return 0; 596*cf7f2e2dSJohn Marino } 5975796c8dcSSimon Schubert 5985796c8dcSSimon Schubert extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */ 5995796c8dcSSimon Schubert 6005796c8dcSSimon Schubert void 6015796c8dcSSimon Schubert _initialize_mi_interp (void) 6025796c8dcSSimon Schubert { 6035796c8dcSSimon Schubert static const struct interp_procs procs = 6045796c8dcSSimon Schubert { 6055796c8dcSSimon Schubert mi_interpreter_init, /* init_proc */ 6065796c8dcSSimon Schubert mi_interpreter_resume, /* resume_proc */ 6075796c8dcSSimon Schubert mi_interpreter_suspend, /* suspend_proc */ 6085796c8dcSSimon Schubert mi_interpreter_exec, /* exec_proc */ 6095796c8dcSSimon Schubert mi_interpreter_prompt_p /* prompt_proc_p */ 6105796c8dcSSimon Schubert }; 6115796c8dcSSimon Schubert 6125796c8dcSSimon Schubert /* The various interpreter levels. */ 6135796c8dcSSimon Schubert interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs)); 6145796c8dcSSimon Schubert interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs)); 6155796c8dcSSimon Schubert interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs)); 6165796c8dcSSimon Schubert 6175796c8dcSSimon Schubert /* "mi" selects the most recent released version. "mi2" was 6185796c8dcSSimon Schubert released as part of GDB 6.0. */ 6195796c8dcSSimon Schubert interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs)); 6205796c8dcSSimon Schubert } 621