gdb/mi/mi-interp.c - gdb

Global variables defined

Functions defined

Source code

  1. /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.

  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "interps.h"
  16. #include "event-top.h"
  17. #include "event-loop.h"
  18. #include "inferior.h"
  19. #include "infrun.h"
  20. #include "ui-out.h"
  21. #include "top.h"
  22. #include "mi-main.h"
  23. #include "mi-cmds.h"
  24. #include "mi-out.h"
  25. #include "mi-console.h"
  26. #include "mi-common.h"
  27. #include "observer.h"
  28. #include "gdbthread.h"
  29. #include "solist.h"
  30. #include "gdb.h"
  31. #include "objfiles.h"
  32. #include "tracepoint.h"
  33. #include "cli-out.h"

  34. /* These are the interpreter setup, etc. functions for the MI
  35.    interpreter.  */

  36. static void mi_execute_command_wrapper (const char *cmd);
  37. static void mi_execute_command_input_handler (char *cmd);
  38. static void mi_command_loop (void *data);

  39. /* These are hooks that we put in place while doing interpreter_exec
  40.    so we can report interesting things that happened "behind the MI's
  41.    back" in this command.  */

  42. static int mi_interp_query_hook (const char *ctlstr, va_list ap)
  43.   ATTRIBUTE_PRINTF (1, 0);

  44. static void mi_insert_notify_hooks (void);
  45. static void mi_remove_notify_hooks (void);

  46. static void mi_on_signal_received (enum gdb_signal siggnal);
  47. static void mi_on_end_stepping_range (void);
  48. static void mi_on_signal_exited (enum gdb_signal siggnal);
  49. static void mi_on_exited (int exitstatus);
  50. static void mi_on_normal_stop (struct bpstats *bs, int print_frame);
  51. static void mi_on_no_history (void);

  52. static void mi_new_thread (struct thread_info *t);
  53. static void mi_thread_exit (struct thread_info *t, int silent);
  54. static void mi_record_changed (struct inferior*, int);
  55. static void mi_inferior_added (struct inferior *inf);
  56. static void mi_inferior_appeared (struct inferior *inf);
  57. static void mi_inferior_exit (struct inferior *inf);
  58. static void mi_inferior_removed (struct inferior *inf);
  59. static void mi_on_resume (ptid_t ptid);
  60. static void mi_solib_loaded (struct so_list *solib);
  61. static void mi_solib_unloaded (struct so_list *solib);
  62. static void mi_about_to_proceed (void);
  63. static void mi_traceframe_changed (int tfnum, int tpnum);
  64. static void mi_tsv_created (const struct trace_state_variable *tsv);
  65. static void mi_tsv_deleted (const struct trace_state_variable *tsv);
  66. static void mi_tsv_modified (const struct trace_state_variable *tsv);
  67. static void mi_breakpoint_created (struct breakpoint *b);
  68. static void mi_breakpoint_deleted (struct breakpoint *b);
  69. static void mi_breakpoint_modified (struct breakpoint *b);
  70. static void mi_command_param_changed (const char *param, const char *value);
  71. static void mi_memory_changed (struct inferior *inf, CORE_ADDR memaddr,
  72.                                ssize_t len, const bfd_byte *myaddr);
  73. static void mi_on_sync_execution_done (void);

  74. static int report_initial_inferior (struct inferior *inf, void *closure);

  75. static void *
  76. mi_interpreter_init (struct interp *interp, int top_level)
  77. {
  78.   struct mi_interp *mi = XNEW (struct mi_interp);
  79.   const char *name;
  80.   int mi_version;

  81.   /* Assign the output channel created at startup to its own global,
  82.      so that we can create a console channel that encapsulates and
  83.      prefixes all gdb_output-type bits coming from the rest of the
  84.      debugger.  */

  85.   raw_stdout = gdb_stdout;

  86.   /* Create MI console channels, each with a different prefix so they
  87.      can be distinguished.  */
  88.   mi->out = mi_console_file_new (raw_stdout, "~", '"');
  89.   mi->err = mi_console_file_new (raw_stdout, "&", '"');
  90.   mi->log = mi->err;
  91.   mi->targ = mi_console_file_new (raw_stdout, "@", '"');
  92.   mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);

  93.   name = interp_name (interp);
  94.   /* INTERP_MI selects the most recent released version.  "mi2" was
  95.      released as part of GDB 6.0.  */
  96.   if (strcmp (name, INTERP_MI) == 0)
  97.     mi_version = 2;
  98.   else if (strcmp (name, INTERP_MI1) == 0)
  99.     mi_version = 1;
  100.   else if (strcmp (name, INTERP_MI2) == 0)
  101.     mi_version = 2;
  102.   else if (strcmp (name, INTERP_MI3) == 0)
  103.     mi_version = 3;
  104.   else
  105.     gdb_assert_not_reached ("unhandled MI version");

  106.   mi->mi_uiout = mi_out_new (mi_version);
  107.   mi->cli_uiout = cli_out_new (mi->out);

  108.   /* There are installed even if MI is not the top level interpreter.
  109.      The callbacks themselves decide whether to be skipped.  */
  110.   observer_attach_signal_received (mi_on_signal_received);
  111.   observer_attach_end_stepping_range (mi_on_end_stepping_range);
  112.   observer_attach_signal_exited (mi_on_signal_exited);
  113.   observer_attach_exited (mi_on_exited);
  114.   observer_attach_no_history (mi_on_no_history);

  115.   if (top_level)
  116.     {
  117.       observer_attach_new_thread (mi_new_thread);
  118.       observer_attach_thread_exit (mi_thread_exit);
  119.       observer_attach_inferior_added (mi_inferior_added);
  120.       observer_attach_inferior_appeared (mi_inferior_appeared);
  121.       observer_attach_inferior_exit (mi_inferior_exit);
  122.       observer_attach_inferior_removed (mi_inferior_removed);
  123.       observer_attach_record_changed (mi_record_changed);
  124.       observer_attach_normal_stop (mi_on_normal_stop);
  125.       observer_attach_target_resumed (mi_on_resume);
  126.       observer_attach_solib_loaded (mi_solib_loaded);
  127.       observer_attach_solib_unloaded (mi_solib_unloaded);
  128.       observer_attach_about_to_proceed (mi_about_to_proceed);
  129.       observer_attach_traceframe_changed (mi_traceframe_changed);
  130.       observer_attach_tsv_created (mi_tsv_created);
  131.       observer_attach_tsv_deleted (mi_tsv_deleted);
  132.       observer_attach_tsv_modified (mi_tsv_modified);
  133.       observer_attach_breakpoint_created (mi_breakpoint_created);
  134.       observer_attach_breakpoint_deleted (mi_breakpoint_deleted);
  135.       observer_attach_breakpoint_modified (mi_breakpoint_modified);
  136.       observer_attach_command_param_changed (mi_command_param_changed);
  137.       observer_attach_memory_changed (mi_memory_changed);
  138.       observer_attach_sync_execution_done (mi_on_sync_execution_done);

  139.       /* The initial inferior is created before this function is
  140.          called, so we need to report it explicitly.  Use iteration in
  141.          case future version of GDB creates more than one inferior
  142.          up-front.  */
  143.       iterate_over_inferiors (report_initial_inferior, mi);
  144.     }

  145.   return mi;
  146. }

  147. static int
  148. mi_interpreter_resume (void *data)
  149. {
  150.   struct mi_interp *mi = data;

  151.   /* As per hack note in mi_interpreter_init, swap in the output
  152.      channels... */
  153.   gdb_setup_readline ();

  154.   /* These overwrite some of the initialization done in
  155.      _intialize_event_loop.  */
  156.   call_readline = gdb_readline2;
  157.   input_handler = mi_execute_command_input_handler;
  158.   async_command_editing_p = 0;
  159.   /* FIXME: This is a total hack for now.  PB's use of the MI
  160.      implicitly relies on a bug in the async support which allows
  161.      asynchronous commands to leak through the commmand loop.  The bug
  162.      involves (but is not limited to) the fact that sync_execution was
  163.      erroneously initialized to 0.  Duplicate by initializing it thus
  164.      here...  */
  165.   sync_execution = 0;

  166.   gdb_stdout = mi->out;
  167.   /* Route error and log output through the MI.  */
  168.   gdb_stderr = mi->err;
  169.   gdb_stdlog = mi->log;
  170.   /* Route target output through the MI.  */
  171.   gdb_stdtarg = mi->targ;
  172.   /* Route target error through the MI as well.  */
  173.   gdb_stdtargerr = mi->targ;

  174.   /* Replace all the hooks that we know about.  There really needs to
  175.      be a better way of doing this... */
  176.   clear_interpreter_hooks ();

  177.   deprecated_show_load_progress = mi_load_progress;

  178.   return 1;
  179. }

  180. static int
  181. mi_interpreter_suspend (void *data)
  182. {
  183.   gdb_disable_readline ();
  184.   return 1;
  185. }

  186. static struct gdb_exception
  187. mi_interpreter_exec (void *data, const char *command)
  188. {
  189.   mi_execute_command_wrapper (command);
  190.   return exception_none;
  191. }

  192. void
  193. mi_cmd_interpreter_exec (char *command, char **argv, int argc)
  194. {
  195.   struct interp *interp_to_use;
  196.   int i;
  197.   char *mi_error_message = NULL;
  198.   struct cleanup *old_chain;

  199.   if (argc < 2)
  200.     error (_("-interpreter-exec: "
  201.              "Usage: -interpreter-exec interp command"));

  202.   interp_to_use = interp_lookup (argv[0]);
  203.   if (interp_to_use == NULL)
  204.     error (_("-interpreter-exec: could not find interpreter \"%s\""),
  205.            argv[0]);

  206.   /* Note that unlike the CLI version of this command, we don't
  207.      actually set INTERP_TO_USE as the current interpreter, as we
  208.      still want gdb_stdout, etc. to point at MI streams.  */

  209.   /* Insert the MI out hooks, making sure to also call the
  210.      interpreter's hooks if it has any.  */
  211.   /* KRS: We shouldn't need this... Events should be installed and
  212.      they should just ALWAYS fire something out down the MI
  213.      channel.  */
  214.   mi_insert_notify_hooks ();

  215.   /* Now run the code.  */

  216.   old_chain = make_cleanup (null_cleanup, 0);
  217.   for (i = 1; i < argc; i++)
  218.     {
  219.       struct gdb_exception e = interp_exec (interp_to_use, argv[i]);

  220.       if (e.reason < 0)
  221.         {
  222.           mi_error_message = xstrdup (e.message);
  223.           make_cleanup (xfree, mi_error_message);
  224.           break;
  225.         }
  226.     }

  227.   mi_remove_notify_hooks ();

  228.   if (mi_error_message != NULL)
  229.     error ("%s", mi_error_message);
  230.   do_cleanups (old_chain);
  231. }

  232. /* This inserts a number of hooks that are meant to produce
  233.    async-notify ("=") MI messages while running commands in another
  234.    interpreter using mi_interpreter_exec.  The canonical use for this
  235.    is to allow access to the gdb CLI interpreter from within the MI,
  236.    while still producing MI style output when actions in the CLI
  237.    command change GDB's state.  */

  238. static void
  239. mi_insert_notify_hooks (void)
  240. {
  241.   deprecated_query_hook = mi_interp_query_hook;
  242. }

  243. static void
  244. mi_remove_notify_hooks (void)
  245. {
  246.   deprecated_query_hook = NULL;
  247. }

  248. static int
  249. mi_interp_query_hook (const char *ctlstr, va_list ap)
  250. {
  251.   return 1;
  252. }

  253. static void
  254. mi_execute_command_wrapper (const char *cmd)
  255. {
  256.   mi_execute_command (cmd, stdin == instream);
  257. }

  258. /* Observer for the synchronous_command_done notification.  */

  259. static void
  260. mi_on_sync_execution_done (void)
  261. {
  262.   /* MI generally prints a prompt after a command, indicating it's
  263.      ready for further input.  However, due to an historical wart, if
  264.      MI async, and a (CLI) synchronous command was issued, then we
  265.      will print the prompt right after printing "^running", even if we
  266.      cannot actually accept any input until the target stops.  See
  267.      mi_on_resume.  However, if the target is async but MI is sync,
  268.      then we need to output the MI prompt now, to replicate gdb's
  269.      behavior when neither the target nor MI are async.  (Note this
  270.      observer is only called by the asynchronous target event handling
  271.      code.)  */
  272.   if (!mi_async_p ())
  273.     {
  274.       fputs_unfiltered ("(gdb) \n", raw_stdout);
  275.       gdb_flush (raw_stdout);
  276.     }
  277. }

  278. /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER.  */

  279. static void
  280. mi_execute_command_input_handler (char *cmd)
  281. {
  282.   mi_execute_command_wrapper (cmd);

  283.   /* MI generally prints a prompt after a command, indicating it's
  284.      ready for further input.  However, due to an historical wart, if
  285.      MI is async, and a synchronous command was issued, then we will
  286.      print the prompt right after printing "^running", even if we
  287.      cannot actually accept any input until the target stops.  See
  288.      mi_on_resume.

  289.      If MI is not async, then we print the prompt when the command
  290.      finishes.  If the target is sync, that means output the prompt
  291.      now, as in that case executing a command doesn't return until the
  292.      command is done.  However, if the target is async, we go back to
  293.      the event loop and output the prompt in the
  294.      'synchronous_command_done' observer.  */
  295.   if (!target_is_async_p () || !sync_execution)
  296.     {
  297.       fputs_unfiltered ("(gdb) \n", raw_stdout);
  298.       gdb_flush (raw_stdout);
  299.     }
  300. }

  301. static void
  302. mi_command_loop (void *data)
  303. {
  304.   /* Turn off 8 bit strings in quoted output.  Any character with the
  305.      high bit set is printed using C's octal format.  */
  306.   sevenbit_strings = 1;

  307.   /* Tell the world that we're alive.  */
  308.   fputs_unfiltered ("(gdb) \n", raw_stdout);
  309.   gdb_flush (raw_stdout);

  310.   start_event_loop ();
  311. }

  312. static void
  313. mi_new_thread (struct thread_info *t)
  314. {
  315.   struct mi_interp *mi = top_level_interpreter_data ();
  316.   struct inferior *inf = find_inferior_ptid (t->ptid);

  317.   gdb_assert (inf);

  318.   fprintf_unfiltered (mi->event_channel,
  319.                       "thread-created,id=\"%d\",group-id=\"i%d\"",
  320.                       t->num, inf->num);
  321.   gdb_flush (mi->event_channel);
  322. }

  323. static void
  324. mi_thread_exit (struct thread_info *t, int silent)
  325. {
  326.   struct mi_interp *mi;
  327.   struct inferior *inf;
  328.   struct cleanup *old_chain;

  329.   if (silent)
  330.     return;

  331.   inf = find_inferior_ptid (t->ptid);

  332.   mi = top_level_interpreter_data ();
  333.   old_chain = make_cleanup_restore_target_terminal ();
  334.   target_terminal_ours ();
  335.   fprintf_unfiltered (mi->event_channel,
  336.                       "thread-exited,id=\"%d\",group-id=\"i%d\"",
  337.                       t->num, inf->num);
  338.   gdb_flush (mi->event_channel);

  339.   do_cleanups (old_chain);
  340. }

  341. /* Emit notification on changing the state of record.  */

  342. static void
  343. mi_record_changed (struct inferior *inferior, int started)
  344. {
  345.   struct mi_interp *mi = top_level_interpreter_data ();

  346.   fprintf_unfiltered (mi->event_channel,  "record-%s,thread-group=\"i%d\"",
  347.                       started ? "started" : "stopped", inferior->num);

  348.   gdb_flush (mi->event_channel);
  349. }

  350. static void
  351. mi_inferior_added (struct inferior *inf)
  352. {
  353.   struct mi_interp *mi = top_level_interpreter_data ();

  354.   target_terminal_ours ();
  355.   fprintf_unfiltered (mi->event_channel,
  356.                       "thread-group-added,id=\"i%d\"",
  357.                       inf->num);
  358.   gdb_flush (mi->event_channel);
  359. }

  360. static void
  361. mi_inferior_appeared (struct inferior *inf)
  362. {
  363.   struct mi_interp *mi = top_level_interpreter_data ();

  364.   target_terminal_ours ();
  365.   fprintf_unfiltered (mi->event_channel,
  366.                       "thread-group-started,id=\"i%d\",pid=\"%d\"",
  367.                       inf->num, inf->pid);
  368.   gdb_flush (mi->event_channel);
  369. }

  370. static void
  371. mi_inferior_exit (struct inferior *inf)
  372. {
  373.   struct mi_interp *mi = top_level_interpreter_data ();

  374.   target_terminal_ours ();
  375.   if (inf->has_exit_code)
  376.     fprintf_unfiltered (mi->event_channel,
  377.                         "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
  378.                         inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
  379.   else
  380.     fprintf_unfiltered (mi->event_channel,
  381.                         "thread-group-exited,id=\"i%d\"", inf->num);

  382.   gdb_flush (mi->event_channel);
  383. }

  384. static void
  385. mi_inferior_removed (struct inferior *inf)
  386. {
  387.   struct mi_interp *mi = top_level_interpreter_data ();

  388.   target_terminal_ours ();
  389.   fprintf_unfiltered (mi->event_channel,
  390.                       "thread-group-removed,id=\"i%d\"",
  391.                       inf->num);
  392.   gdb_flush (mi->event_channel);
  393. }

  394. /* Cleanup that restores a previous current uiout.  */

  395. static void
  396. restore_current_uiout_cleanup (void *arg)
  397. {
  398.   struct ui_out *saved_uiout = arg;

  399.   current_uiout = saved_uiout;
  400. }

  401. /* Return the MI interpreter, if it is active -- either because it's
  402.    the top-level interpreter or the interpreter executing the current
  403.    command.  Returns NULL if the MI interpreter is not being used.  */

  404. static struct interp *
  405. find_mi_interpreter (void)
  406. {
  407.   struct interp *interp;

  408.   interp = top_level_interpreter ();
  409.   if (ui_out_is_mi_like_p (interp_ui_out (interp)))
  410.     return interp;

  411.   interp = command_interp ();
  412.   if (ui_out_is_mi_like_p (interp_ui_out (interp)))
  413.     return interp;

  414.   return NULL;
  415. }

  416. /* Return the MI_INTERP structure of the active MI interpreter.
  417.    Returns NULL if MI is not active.  */

  418. static struct mi_interp *
  419. mi_interp_data (void)
  420. {
  421.   struct interp *interp = find_mi_interpreter ();

  422.   if (interp != NULL)
  423.     return interp_data (interp);
  424.   return NULL;
  425. }

  426. /* Observers for several run control events that print why the
  427.    inferior has stopped to both the the MI event channel and to the MI
  428.    console.  If the MI interpreter is not active, print nothing.  */

  429. /* Observer for the signal_received notification.  */

  430. static void
  431. mi_on_signal_received (enum gdb_signal siggnal)
  432. {
  433.   struct mi_interp *mi = mi_interp_data ();

  434.   if (mi == NULL)
  435.     return;

  436.   print_signal_received_reason (mi->mi_uiout, siggnal);
  437.   print_signal_received_reason (mi->cli_uiout, siggnal);
  438. }

  439. /* Observer for the end_stepping_range notification.  */

  440. static void
  441. mi_on_end_stepping_range (void)
  442. {
  443.   struct mi_interp *mi = mi_interp_data ();

  444.   if (mi == NULL)
  445.     return;

  446.   print_end_stepping_range_reason (mi->mi_uiout);
  447.   print_end_stepping_range_reason (mi->cli_uiout);
  448. }

  449. /* Observer for the signal_exited notification.  */

  450. static void
  451. mi_on_signal_exited (enum gdb_signal siggnal)
  452. {
  453.   struct mi_interp *mi = mi_interp_data ();

  454.   if (mi == NULL)
  455.     return;

  456.   print_signal_exited_reason (mi->mi_uiout, siggnal);
  457.   print_signal_exited_reason (mi->cli_uiout, siggnal);
  458. }

  459. /* Observer for the exited notification.  */

  460. static void
  461. mi_on_exited (int exitstatus)
  462. {
  463.   struct mi_interp *mi = mi_interp_data ();

  464.   if (mi == NULL)
  465.     return;

  466.   print_exited_reason (mi->mi_uiout, exitstatus);
  467.   print_exited_reason (mi->cli_uiout, exitstatus);
  468. }

  469. /* Observer for the no_history notification.  */

  470. static void
  471. mi_on_no_history (void)
  472. {
  473.   struct mi_interp *mi = mi_interp_data ();

  474.   if (mi == NULL)
  475.     return;

  476.   print_no_history_reason (mi->mi_uiout);
  477.   print_no_history_reason (mi->cli_uiout);
  478. }

  479. static void
  480. mi_on_normal_stop (struct bpstats *bs, int print_frame)
  481. {
  482.   /* Since this can be called when CLI command is executing,
  483.      using cli interpreter, be sure to use MI uiout for output,
  484.      not the current one.  */
  485.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());

  486.   if (print_frame)
  487.     {
  488.       int core;

  489.       if (current_uiout != mi_uiout)
  490.         {
  491.           /* The normal_stop function has printed frame information
  492.              into CLI uiout, or some other non-MI uiout.  There's no
  493.              way we can extract proper fields from random uiout
  494.              object, so we print the frame again.  In practice, this
  495.              can only happen when running a CLI command in MI.  */
  496.           struct ui_out *saved_uiout = current_uiout;
  497.           struct target_waitstatus last;
  498.           ptid_t last_ptid;

  499.           current_uiout = mi_uiout;

  500.           get_last_target_status (&last_ptid, &last);
  501.           print_stop_event (&last);

  502.           current_uiout = saved_uiout;
  503.         }
  504.       /* Otherwise, frame information has already been printed by
  505.          normal_stop.  */
  506.       else
  507.         {
  508.           /* Breakpoint hits should always be mirrored to the console.
  509.              Deciding what to mirror to the console wrt to breakpoints
  510.              and random stops gets messy real fast.  E.g., say "s"
  511.              trips on a breakpoint.  We'd clearly want to mirror the
  512.              event to the console in this case.  But what about more
  513.              complicated cases like "s&; thread n; s&", and one of
  514.              those steps spawning a new thread, and that thread
  515.              hitting a breakpoint?  It's impossible in general to
  516.              track whether the thread had any relation to the commands
  517.              that had been executed.  So we just simplify and always
  518.              mirror breakpoints and random events to the console.

  519.              Also, CLI execution commands (-interpreter-exec console
  520.              "next", for example) in async mode have the opposite
  521.              issue as described in the "then" branch above --
  522.              normal_stop has already printed frame information to MI
  523.              uiout, but nothing has printed the same information to
  524.              the CLI channel.  We should print the source line to the
  525.              console when stepping or other similar commands, iff the
  526.              step was started by a console command (but not if it was
  527.              started with -exec-step or similar).  */
  528.           struct thread_info *tp = inferior_thread ();

  529.           if ((!tp->control.stop_step
  530.                   && !tp->control.proceed_to_finish)
  531.               || (tp->control.command_interp != NULL
  532.                   && tp->control.command_interp != top_level_interpreter ()))
  533.             {
  534.               struct mi_interp *mi = top_level_interpreter_data ();
  535.               struct target_waitstatus last;
  536.               ptid_t last_ptid;
  537.               struct cleanup *old_chain;

  538.               /* Set the current uiout to CLI uiout temporarily.  */
  539.               old_chain = make_cleanup (restore_current_uiout_cleanup,
  540.                                         current_uiout);
  541.               current_uiout = mi->cli_uiout;

  542.               get_last_target_status (&last_ptid, &last);
  543.               print_stop_event (&last);

  544.               do_cleanups (old_chain);
  545.             }
  546.         }

  547.       ui_out_field_int (mi_uiout, "thread-id",
  548.                         pid_to_thread_id (inferior_ptid));
  549.       if (non_stop)
  550.         {
  551.           struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
  552.             (mi_uiout, "stopped-threads");

  553.           ui_out_field_int (mi_uiout, NULL,
  554.                             pid_to_thread_id (inferior_ptid));
  555.           do_cleanups (back_to);
  556.         }
  557.       else
  558.         ui_out_field_string (mi_uiout, "stopped-threads", "all");

  559.       core = target_core_of_thread (inferior_ptid);
  560.       if (core != -1)
  561.         ui_out_field_int (mi_uiout, "core", core);
  562.     }

  563.   fputs_unfiltered ("*stopped", raw_stdout);
  564.   mi_out_put (mi_uiout, raw_stdout);
  565.   mi_out_rewind (mi_uiout);
  566.   mi_print_timing_maybe ();
  567.   fputs_unfiltered ("\n", raw_stdout);
  568.   gdb_flush (raw_stdout);
  569. }

  570. static void
  571. mi_about_to_proceed (void)
  572. {
  573.   /* Suppress output while calling an inferior function.  */

  574.   if (!ptid_equal (inferior_ptid, null_ptid))
  575.     {
  576.       struct thread_info *tp = inferior_thread ();

  577.       if (tp->control.in_infcall)
  578.         return;
  579.     }

  580.   mi_proceeded = 1;
  581. }

  582. /* When the element is non-zero, no MI notifications will be emitted in
  583.    response to the corresponding observers.  */

  584. struct mi_suppress_notification mi_suppress_notification =
  585.   {
  586.     0,
  587.     0,
  588.     0,
  589.   };

  590. /* Emit notification on changing a traceframe.  */

  591. static void
  592. mi_traceframe_changed (int tfnum, int tpnum)
  593. {
  594.   struct mi_interp *mi = top_level_interpreter_data ();

  595.   if (mi_suppress_notification.traceframe)
  596.     return;

  597.   target_terminal_ours ();

  598.   if (tfnum >= 0)
  599.     fprintf_unfiltered (mi->event_channel, "traceframe-changed,"
  600.                         "num=\"%d\",tracepoint=\"%d\"\n",
  601.                         tfnum, tpnum);
  602.   else
  603.     fprintf_unfiltered (mi->event_channel, "traceframe-changed,end");

  604.   gdb_flush (mi->event_channel);
  605. }

  606. /* Emit notification on creating a trace state variable.  */

  607. static void
  608. mi_tsv_created (const struct trace_state_variable *tsv)
  609. {
  610.   struct mi_interp *mi = top_level_interpreter_data ();

  611.   target_terminal_ours ();

  612.   fprintf_unfiltered (mi->event_channel, "tsv-created,"
  613.                       "name=\"%s\",initial=\"%s\"\n",
  614.                       tsv->name, plongest (tsv->initial_value));

  615.   gdb_flush (mi->event_channel);
  616. }

  617. /* Emit notification on deleting a trace state variable.  */

  618. static void
  619. mi_tsv_deleted (const struct trace_state_variable *tsv)
  620. {
  621.   struct mi_interp *mi = top_level_interpreter_data ();

  622.   target_terminal_ours ();

  623.   if (tsv != NULL)
  624.     fprintf_unfiltered (mi->event_channel, "tsv-deleted,"
  625.                         "name=\"%s\"\n", tsv->name);
  626.   else
  627.     fprintf_unfiltered (mi->event_channel, "tsv-deleted\n");

  628.   gdb_flush (mi->event_channel);
  629. }

  630. /* Emit notification on modifying a trace state variable.  */

  631. static void
  632. mi_tsv_modified (const struct trace_state_variable *tsv)
  633. {
  634.   struct mi_interp *mi = top_level_interpreter_data ();
  635.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());

  636.   target_terminal_ours ();

  637.   fprintf_unfiltered (mi->event_channel,
  638.                       "tsv-modified");

  639.   ui_out_redirect (mi_uiout, mi->event_channel);

  640.   ui_out_field_string (mi_uiout, "name", tsv->name);
  641.   ui_out_field_string (mi_uiout, "initial",
  642.                        plongest (tsv->initial_value));
  643.   if (tsv->value_known)
  644.     ui_out_field_string (mi_uiout, "current", plongest (tsv->value));

  645.   ui_out_redirect (mi_uiout, NULL);

  646.   gdb_flush (mi->event_channel);
  647. }

  648. /* Emit notification about a created breakpoint.  */

  649. static void
  650. mi_breakpoint_created (struct breakpoint *b)
  651. {
  652.   struct mi_interp *mi = top_level_interpreter_data ();
  653.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
  654.   volatile struct gdb_exception e;

  655.   if (mi_suppress_notification.breakpoint)
  656.     return;

  657.   if (b->number <= 0)
  658.     return;

  659.   target_terminal_ours ();
  660.   fprintf_unfiltered (mi->event_channel,
  661.                       "breakpoint-created");
  662.   /* We want the output from gdb_breakpoint_query to go to
  663.      mi->event_channel.  One approach would be to just call
  664.      gdb_breakpoint_query, and then use mi_out_put to send the current
  665.      content of mi_outout into mi->event_channel.  However, that will
  666.      break if anything is output to mi_uiout prior to calling the
  667.      breakpoint_created notifications.  So, we use
  668.      ui_out_redirect.  */
  669.   ui_out_redirect (mi_uiout, mi->event_channel);
  670.   TRY_CATCH (e, RETURN_MASK_ERROR)
  671.     gdb_breakpoint_query (mi_uiout, b->number, NULL);
  672.   ui_out_redirect (mi_uiout, NULL);

  673.   gdb_flush (mi->event_channel);
  674. }

  675. /* Emit notification about deleted breakpoint.  */

  676. static void
  677. mi_breakpoint_deleted (struct breakpoint *b)
  678. {
  679.   struct mi_interp *mi = top_level_interpreter_data ();

  680.   if (mi_suppress_notification.breakpoint)
  681.     return;

  682.   if (b->number <= 0)
  683.     return;

  684.   target_terminal_ours ();

  685.   fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
  686.                       b->number);

  687.   gdb_flush (mi->event_channel);
  688. }

  689. /* Emit notification about modified breakpoint.  */

  690. static void
  691. mi_breakpoint_modified (struct breakpoint *b)
  692. {
  693.   struct mi_interp *mi = top_level_interpreter_data ();
  694.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
  695.   volatile struct gdb_exception e;

  696.   if (mi_suppress_notification.breakpoint)
  697.     return;

  698.   if (b->number <= 0)
  699.     return;

  700.   target_terminal_ours ();
  701.   fprintf_unfiltered (mi->event_channel,
  702.                       "breakpoint-modified");
  703.   /* We want the output from gdb_breakpoint_query to go to
  704.      mi->event_channel.  One approach would be to just call
  705.      gdb_breakpoint_query, and then use mi_out_put to send the current
  706.      content of mi_outout into mi->event_channel.  However, that will
  707.      break if anything is output to mi_uiout prior to calling the
  708.      breakpoint_created notifications.  So, we use
  709.      ui_out_redirect.  */
  710.   ui_out_redirect (mi_uiout, mi->event_channel);
  711.   TRY_CATCH (e, RETURN_MASK_ERROR)
  712.     gdb_breakpoint_query (mi_uiout, b->number, NULL);
  713.   ui_out_redirect (mi_uiout, NULL);

  714.   gdb_flush (mi->event_channel);
  715. }

  716. static int
  717. mi_output_running_pid (struct thread_info *info, void *arg)
  718. {
  719.   ptid_t *ptid = arg;

  720.   if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
  721.     fprintf_unfiltered (raw_stdout,
  722.                         "*running,thread-id=\"%d\"\n",
  723.                         info->num);

  724.   return 0;
  725. }

  726. static int
  727. mi_inferior_count (struct inferior *inf, void *arg)
  728. {
  729.   if (inf->pid != 0)
  730.     {
  731.       int *count_p = arg;
  732.       (*count_p)++;
  733.     }

  734.   return 0;
  735. }

  736. static void
  737. mi_on_resume (ptid_t ptid)
  738. {
  739.   struct thread_info *tp = NULL;

  740.   if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
  741.     tp = inferior_thread ();
  742.   else
  743.     tp = find_thread_ptid (ptid);

  744.   /* Suppress output while calling an inferior function.  */
  745.   if (tp->control.in_infcall)
  746.     return;

  747.   /* To cater for older frontends, emit ^running, but do it only once
  748.      per each command.  We do it here, since at this point we know
  749.      that the target was successfully resumed, and in non-async mode,
  750.      we won't return back to MI interpreter code until the target
  751.      is done running, so delaying the output of "^running" until then
  752.      will make it impossible for frontend to know what's going on.

  753.      In future (MI3), we'll be outputting "^done" here.  */
  754.   if (!running_result_record_printed && mi_proceeded)
  755.     {
  756.       fprintf_unfiltered (raw_stdout, "%s^running\n",
  757.                           current_token ? current_token : "");
  758.     }

  759.   if (ptid_get_pid (ptid) == -1)
  760.     fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
  761.   else if (ptid_is_pid (ptid))
  762.     {
  763.       int count = 0;

  764.       /* Backwards compatibility.  If there's only one inferior,
  765.          output "all", otherwise, output each resumed thread
  766.          individually.  */
  767.       iterate_over_inferiors (mi_inferior_count, &count);

  768.       if (count == 1)
  769.         fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
  770.       else
  771.         iterate_over_threads (mi_output_running_pid, &ptid);
  772.     }
  773.   else
  774.     {
  775.       struct thread_info *ti = find_thread_ptid (ptid);

  776.       gdb_assert (ti);
  777.       fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
  778.     }

  779.   if (!running_result_record_printed && mi_proceeded)
  780.     {
  781.       running_result_record_printed = 1;
  782.       /* This is what gdb used to do historically -- printing prompt even if
  783.          it cannot actually accept any input.  This will be surely removed
  784.          for MI3, and may be removed even earlier.  SYNC_EXECUTION is
  785.          checked here because we only need to emit a prompt if a
  786.          synchronous command was issued when the target is async.  */
  787.       if (!target_is_async_p () || sync_execution)
  788.         fputs_unfiltered ("(gdb) \n", raw_stdout);
  789.     }
  790.   gdb_flush (raw_stdout);
  791. }

  792. static void
  793. mi_solib_loaded (struct so_list *solib)
  794. {
  795.   struct mi_interp *mi = top_level_interpreter_data ();
  796.   struct ui_out *uiout = interp_ui_out (top_level_interpreter ());

  797.   target_terminal_ours ();

  798.   fprintf_unfiltered (mi->event_channel, "library-loaded");

  799.   ui_out_redirect (uiout, mi->event_channel);

  800.   ui_out_field_string (uiout, "id", solib->so_original_name);
  801.   ui_out_field_string (uiout, "target-name", solib->so_original_name);
  802.   ui_out_field_string (uiout, "host-name", solib->so_name);
  803.   ui_out_field_int (uiout, "symbols-loaded", solib->symbols_loaded);
  804.   if (!gdbarch_has_global_solist (target_gdbarch ()))
  805.     {
  806.       ui_out_field_fmt (uiout, "thread-group", "i%d", current_inferior ()->num);
  807.     }

  808.   ui_out_redirect (uiout, NULL);

  809.   gdb_flush (mi->event_channel);
  810. }

  811. static void
  812. mi_solib_unloaded (struct so_list *solib)
  813. {
  814.   struct mi_interp *mi = top_level_interpreter_data ();
  815.   struct ui_out *uiout = interp_ui_out (top_level_interpreter ());

  816.   target_terminal_ours ();

  817.   fprintf_unfiltered (mi->event_channel, "library-unloaded");

  818.   ui_out_redirect (uiout, mi->event_channel);

  819.   ui_out_field_string (uiout, "id", solib->so_original_name);
  820.   ui_out_field_string (uiout, "target-name", solib->so_original_name);
  821.   ui_out_field_string (uiout, "host-name", solib->so_name);
  822.   if (!gdbarch_has_global_solist (target_gdbarch ()))
  823.     {
  824.       ui_out_field_fmt (uiout, "thread-group", "i%d", current_inferior ()->num);
  825.     }

  826.   ui_out_redirect (uiout, NULL);

  827.   gdb_flush (mi->event_channel);
  828. }

  829. /* Emit notification about the command parameter change.  */

  830. static void
  831. mi_command_param_changed (const char *param, const char *value)
  832. {
  833.   struct mi_interp *mi = top_level_interpreter_data ();
  834.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());

  835.   if (mi_suppress_notification.cmd_param_changed)
  836.     return;

  837.   target_terminal_ours ();

  838.   fprintf_unfiltered (mi->event_channel,
  839.                       "cmd-param-changed");

  840.   ui_out_redirect (mi_uiout, mi->event_channel);

  841.   ui_out_field_string (mi_uiout, "param", param);
  842.   ui_out_field_string (mi_uiout, "value", value);

  843.   ui_out_redirect (mi_uiout, NULL);

  844.   gdb_flush (mi->event_channel);
  845. }

  846. /* Emit notification about the target memory change.  */

  847. static void
  848. mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
  849.                    ssize_t len, const bfd_byte *myaddr)
  850. {
  851.   struct mi_interp *mi = top_level_interpreter_data ();
  852.   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
  853.   struct obj_section *sec;

  854.   if (mi_suppress_notification.memory)
  855.     return;

  856.   target_terminal_ours ();

  857.   fprintf_unfiltered (mi->event_channel,
  858.                       "memory-changed");

  859.   ui_out_redirect (mi_uiout, mi->event_channel);

  860.   ui_out_field_fmt (mi_uiout, "thread-group", "i%d", inferior->num);
  861.   ui_out_field_core_addr (mi_uiout, "addr", target_gdbarch (), memaddr);
  862.   ui_out_field_fmt (mi_uiout, "len", "%s", hex_string (len));

  863.   /* Append 'type=code' into notification if MEMADDR falls in the range of
  864.      sections contain code.  */
  865.   sec = find_pc_section (memaddr);
  866.   if (sec != NULL && sec->objfile != NULL)
  867.     {
  868.       flagword flags = bfd_get_section_flags (sec->objfile->obfd,
  869.                                               sec->the_bfd_section);

  870.       if (flags & SEC_CODE)
  871.         ui_out_field_string (mi_uiout, "type", "code");
  872.     }

  873.   ui_out_redirect (mi_uiout, NULL);

  874.   gdb_flush (mi->event_channel);
  875. }

  876. static int
  877. report_initial_inferior (struct inferior *inf, void *closure)
  878. {
  879.   /* This function is called from mi_intepreter_init, and since
  880.      mi_inferior_added assumes that inferior is fully initialized
  881.      and top_level_interpreter_data is set, we cannot call
  882.      it here.  */
  883.   struct mi_interp *mi = closure;

  884.   target_terminal_ours ();
  885.   fprintf_unfiltered (mi->event_channel,
  886.                       "thread-group-added,id=\"i%d\"",
  887.                       inf->num);
  888.   gdb_flush (mi->event_channel);
  889.   return 0;
  890. }

  891. static struct ui_out *
  892. mi_ui_out (struct interp *interp)
  893. {
  894.   struct mi_interp *mi = interp_data (interp);

  895.   return mi->mi_uiout;
  896. }

  897. /* Save the original value of raw_stdout here when logging, so we can
  898.    restore correctly when done.  */

  899. static struct ui_file *saved_raw_stdout;

  900. /* Do MI-specific logging actions; save raw_stdout, and change all
  901.    the consoles to use the supplied ui-file(s).  */

  902. static int
  903. mi_set_logging (struct interp *interp, int start_log,
  904.                 struct ui_file *out, struct ui_file *logfile)
  905. {
  906.   struct mi_interp *mi = interp_data (interp);

  907.   if (!mi)
  908.     return 0;

  909.   if (start_log)
  910.     {
  911.       /* The tee created already is based on gdb_stdout, which for MI
  912.          is a console and so we end up in an infinite loop of console
  913.          writing to ui_file writing to console etc.  So discard the
  914.          existing tee (it hasn't been used yet, and MI won't ever use
  915.          it), and create one based on raw_stdout instead.  */
  916.       if (logfile)
  917.         {
  918.           ui_file_delete (out);
  919.           out = tee_file_new (raw_stdout, 0, logfile, 0);
  920.         }

  921.       saved_raw_stdout = raw_stdout;
  922.       raw_stdout = out;
  923.     }
  924.   else
  925.     {
  926.       raw_stdout = saved_raw_stdout;
  927.       saved_raw_stdout = NULL;
  928.     }

  929.   mi_console_set_raw (mi->out, raw_stdout);
  930.   mi_console_set_raw (mi->err, raw_stdout);
  931.   mi_console_set_raw (mi->log, raw_stdout);
  932.   mi_console_set_raw (mi->targ, raw_stdout);
  933.   mi_console_set_raw (mi->event_channel, raw_stdout);

  934.   return 1;
  935. }

  936. extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */

  937. void
  938. _initialize_mi_interp (void)
  939. {
  940.   static const struct interp_procs procs =
  941.     {
  942.       mi_interpreter_init,        /* init_proc */
  943.       mi_interpreter_resume,        /* resume_proc */
  944.       mi_interpreter_suspend,        /* suspend_proc */
  945.       mi_interpreter_exec,        /* exec_proc */
  946.       mi_ui_out,                 /* ui_out_proc */
  947.       mi_set_logging,                /* set_logging_proc */
  948.       mi_command_loop                /* command_loop_proc */
  949.     };

  950.   /* The various interpreter levels.  */
  951.   interp_add (interp_new (INTERP_MI1, &procs));
  952.   interp_add (interp_new (INTERP_MI2, &procs));
  953.   interp_add (interp_new (INTERP_MI3, &procs));
  954.   interp_add (interp_new (INTERP_MI, &procs));
  955. }