gdb/mi/mi-main.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* MI Command Set.

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

  3.    Contributed by Cygnus Solutions (a Red Hat company).

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "target.h"
  18. #include "inferior.h"
  19. #include "infrun.h"
  20. #include "top.h"
  21. #include "gdbthread.h"
  22. #include "mi-cmds.h"
  23. #include "mi-parse.h"
  24. #include "mi-getopt.h"
  25. #include "mi-console.h"
  26. #include "ui-out.h"
  27. #include "mi-out.h"
  28. #include "interps.h"
  29. #include "event-loop.h"
  30. #include "event-top.h"
  31. #include "gdbcore.h"                /* For write_memory().  */
  32. #include "value.h"
  33. #include "regcache.h"
  34. #include "gdb.h"
  35. #include "frame.h"
  36. #include "mi-main.h"
  37. #include "mi-common.h"
  38. #include "language.h"
  39. #include "valprint.h"
  40. #include "inferior.h"
  41. #include "osdata.h"
  42. #include "splay-tree.h"
  43. #include "tracepoint.h"
  44. #include "ctf.h"
  45. #include "ada-lang.h"
  46. #include "linespec.h"
  47. #include "extension.h"
  48. #include "gdbcmd.h"

  49. #include <ctype.h>
  50. #include <sys/time.h>

  51. #if defined HAVE_SYS_RESOURCE_H
  52. #include <sys/resource.h>
  53. #endif

  54. #ifdef HAVE_GETRUSAGE
  55. struct rusage rusage;
  56. #endif

  57. enum
  58.   {
  59.     FROM_TTY = 0
  60.   };

  61. int mi_debug_p;

  62. struct ui_file *raw_stdout;

  63. /* This is used to pass the current command timestamp down to
  64.    continuation routines.  */
  65. static struct mi_timestamp *current_command_ts;

  66. static int do_timings = 0;

  67. char *current_token;
  68. /* Few commands would like to know if options like --thread-group were
  69.    explicitly specified.  This variable keeps the current parsed
  70.    command including all option, and make it possible.  */
  71. static struct mi_parse *current_context;

  72. int running_result_record_printed = 1;

  73. /* Flag indicating that the target has proceeded since the last
  74.    command was issued.  */
  75. int mi_proceeded;

  76. extern void _initialize_mi_main (void);
  77. static void mi_cmd_execute (struct mi_parse *parse);

  78. static void mi_execute_cli_command (const char *cmd, int args_p,
  79.                                     const char *args);
  80. static void mi_execute_async_cli_command (char *cli_command,
  81.                                           char **argv, int argc);
  82. static int register_changed_p (int regnum, struct regcache *,
  83.                                struct regcache *);
  84. static void output_register (struct frame_info *, int regnum, int format,
  85.                              int skip_unavailable);

  86. /* Controls whether the frontend wants MI in async mode.  */
  87. static int mi_async = 0;

  88. /* The set command writes to this variable.  If the inferior is
  89.    executing, mi_async is *not* updated.  */
  90. static int mi_async_1 = 0;

  91. static void
  92. set_mi_async_command (char *args, int from_tty,
  93.                       struct cmd_list_element *c)
  94. {
  95.   if (have_live_inferiors ())
  96.     {
  97.       mi_async_1 = mi_async;
  98.       error (_("Cannot change this setting while the inferior is running."));
  99.     }

  100.   mi_async = mi_async_1;
  101. }

  102. static void
  103. show_mi_async_command (struct ui_file *file, int from_tty,
  104.                        struct cmd_list_element *c,
  105.                        const char *value)
  106. {
  107.   fprintf_filtered (file,
  108.                     _("Whether MI is in asynchronous mode is %s.\n"),
  109.                     value);
  110. }

  111. /* A wrapper for target_can_async_p that takes the MI setting into
  112.    account.  */

  113. int
  114. mi_async_p (void)
  115. {
  116.   return mi_async && target_can_async_p ();
  117. }

  118. /* Command implementations.  FIXME: Is this libgdb?  No.  This is the MI
  119.    layer that calls libgdb.  Any operation used in the below should be
  120.    formalized.  */

  121. static void timestamp (struct mi_timestamp *tv);

  122. static void print_diff_now (struct mi_timestamp *start);
  123. static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);

  124. void
  125. mi_cmd_gdb_exit (char *command, char **argv, int argc)
  126. {
  127.   /* We have to print everything right here because we never return.  */
  128.   if (current_token)
  129.     fputs_unfiltered (current_token, raw_stdout);
  130.   fputs_unfiltered ("^exit\n", raw_stdout);
  131.   mi_out_put (current_uiout, raw_stdout);
  132.   gdb_flush (raw_stdout);
  133.   /* FIXME: The function called is not yet a formal libgdb function.  */
  134.   quit_force (NULL, FROM_TTY);
  135. }

  136. void
  137. mi_cmd_exec_next (char *command, char **argv, int argc)
  138. {
  139.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  140.   if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
  141.     mi_execute_async_cli_command ("reverse-next", argv + 1, argc - 1);
  142.   else
  143.     mi_execute_async_cli_command ("next", argv, argc);
  144. }

  145. void
  146. mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
  147. {
  148.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  149.   if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
  150.     mi_execute_async_cli_command ("reverse-nexti", argv + 1, argc - 1);
  151.   else
  152.     mi_execute_async_cli_command ("nexti", argv, argc);
  153. }

  154. void
  155. mi_cmd_exec_step (char *command, char **argv, int argc)
  156. {
  157.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  158.   if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
  159.     mi_execute_async_cli_command ("reverse-step", argv + 1, argc - 1);
  160.   else
  161.     mi_execute_async_cli_command ("step", argv, argc);
  162. }

  163. void
  164. mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
  165. {
  166.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  167.   if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
  168.     mi_execute_async_cli_command ("reverse-stepi", argv + 1, argc - 1);
  169.   else
  170.     mi_execute_async_cli_command ("stepi", argv, argc);
  171. }

  172. void
  173. mi_cmd_exec_finish (char *command, char **argv, int argc)
  174. {
  175.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  176.   if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
  177.     mi_execute_async_cli_command ("reverse-finish", argv + 1, argc - 1);
  178.   else
  179.     mi_execute_async_cli_command ("finish", argv, argc);
  180. }

  181. void
  182. mi_cmd_exec_return (char *command, char **argv, int argc)
  183. {
  184.   /* This command doesn't really execute the target, it just pops the
  185.      specified number of frames.  */
  186.   if (argc)
  187.     /* Call return_command with from_tty argument equal to 0 so as to
  188.        avoid being queried.  */
  189.     return_command (*argv, 0);
  190.   else
  191.     /* Call return_command with from_tty argument equal to 0 so as to
  192.        avoid being queried.  */
  193.     return_command (NULL, 0);

  194.   /* Because we have called return_command with from_tty = 0, we need
  195.      to print the frame here.  */
  196.   print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 1);
  197. }

  198. void
  199. mi_cmd_exec_jump (char *args, char **argv, int argc)
  200. {
  201.   /* FIXME: Should call a libgdb function, not a cli wrapper.  */
  202.   mi_execute_async_cli_command ("jump", argv, argc);
  203. }

  204. static void
  205. proceed_thread (struct thread_info *thread, int pid)
  206. {
  207.   if (!is_stopped (thread->ptid))
  208.     return;

  209.   if (pid != 0 && ptid_get_pid (thread->ptid) != pid)
  210.     return;

  211.   switch_to_thread (thread->ptid);
  212.   clear_proceed_status (0);
  213.   proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0);
  214. }

  215. static int
  216. proceed_thread_callback (struct thread_info *thread, void *arg)
  217. {
  218.   int pid = *(int *)arg;

  219.   proceed_thread (thread, pid);
  220.   return 0;
  221. }

  222. static void
  223. exec_continue (char **argv, int argc)
  224. {
  225.   prepare_execution_command (&current_target, mi_async_p ());

  226.   if (non_stop)
  227.     {
  228.       /* In non-stop mode, 'resume' always resumes a single thread.
  229.          Therefore, to resume all threads of the current inferior, or
  230.          all threads in all inferiors, we need to iterate over
  231.          threads.

  232.          See comment on infcmd.c:proceed_thread_callback for rationale.  */
  233.       if (current_context->all || current_context->thread_group != -1)
  234.         {
  235.           int pid = 0;
  236.           struct cleanup *back_to = make_cleanup_restore_current_thread ();

  237.           if (!current_context->all)
  238.             {
  239.               struct inferior *inf
  240.                 = find_inferior_id (current_context->thread_group);

  241.               pid = inf->pid;
  242.             }
  243.           iterate_over_threads (proceed_thread_callback, &pid);
  244.           do_cleanups (back_to);
  245.         }
  246.       else
  247.         {
  248.           continue_1 (0);
  249.         }
  250.     }
  251.   else
  252.     {
  253.       struct cleanup *back_to = make_cleanup_restore_integer (&sched_multi);

  254.       if (current_context->all)
  255.         {
  256.           sched_multi = 1;
  257.           continue_1 (0);
  258.         }
  259.       else
  260.         {
  261.           /* In all-stop mode, -exec-continue traditionally resumed
  262.              either all threads, or one thread, depending on the
  263.              'scheduler-locking' variable.  Let's continue to do the
  264.              same.  */
  265.           continue_1 (1);
  266.         }
  267.       do_cleanups (back_to);
  268.     }
  269. }

  270. static void
  271. exec_direction_forward (void *notused)
  272. {
  273.   execution_direction = EXEC_FORWARD;
  274. }

  275. static void
  276. exec_reverse_continue (char **argv, int argc)
  277. {
  278.   enum exec_direction_kind dir = execution_direction;
  279.   struct cleanup *old_chain;

  280.   if (dir == EXEC_REVERSE)
  281.     error (_("Already in reverse mode."));

  282.   if (!target_can_execute_reverse)
  283.     error (_("Target %s does not support this command."), target_shortname);

  284.   old_chain = make_cleanup (exec_direction_forward, NULL);
  285.   execution_direction = EXEC_REVERSE;
  286.   exec_continue (argv, argc);
  287.   do_cleanups (old_chain);
  288. }

  289. void
  290. mi_cmd_exec_continue (char *command, char **argv, int argc)
  291. {
  292.   if (argc > 0 && strcmp (argv[0], "--reverse") == 0)
  293.     exec_reverse_continue (argv + 1, argc - 1);
  294.   else
  295.     exec_continue (argv, argc);
  296. }

  297. static int
  298. interrupt_thread_callback (struct thread_info *thread, void *arg)
  299. {
  300.   int pid = *(int *)arg;

  301.   if (!is_running (thread->ptid))
  302.     return 0;

  303.   if (ptid_get_pid (thread->ptid) != pid)
  304.     return 0;

  305.   target_stop (thread->ptid);
  306.   return 0;
  307. }

  308. /* Interrupt the execution of the target.  Note how we must play
  309.    around with the token variables, in order to display the current
  310.    token in the result of the interrupt command, and the previous
  311.    execution token when the target finally stops.  See comments in
  312.    mi_cmd_execute.  */

  313. void
  314. mi_cmd_exec_interrupt (char *command, char **argv, int argc)
  315. {
  316.   /* In all-stop mode, everything stops, so we don't need to try
  317.      anything specific.  */
  318.   if (!non_stop)
  319.     {
  320.       interrupt_target_1 (0);
  321.       return;
  322.     }

  323.   if (current_context->all)
  324.     {
  325.       /* This will interrupt all threads in all inferiors.  */
  326.       interrupt_target_1 (1);
  327.     }
  328.   else if (current_context->thread_group != -1)
  329.     {
  330.       struct inferior *inf = find_inferior_id (current_context->thread_group);

  331.       iterate_over_threads (interrupt_thread_callback, &inf->pid);
  332.     }
  333.   else
  334.     {
  335.       /* Interrupt just the current thread -- either explicitly
  336.          specified via --thread or whatever was current before
  337.          MI command was sent.  */
  338.       interrupt_target_1 (0);
  339.     }
  340. }

  341. /* Callback for iterate_over_inferiors which starts the execution
  342.    of the given inferior.

  343.    ARG is a pointer to an integer whose value, if non-zero, indicates
  344.    that the program should be stopped when reaching the main subprogram
  345.    (similar to what the CLI "start" command does).  */

  346. static int
  347. run_one_inferior (struct inferior *inf, void *arg)
  348. {
  349.   int start_p = *(int *) arg;
  350.   const char *run_cmd = start_p ? "start" : "run";

  351.   if (inf->pid != 0)
  352.     {
  353.       if (inf->pid != ptid_get_pid (inferior_ptid))
  354.         {
  355.           struct thread_info *tp;

  356.           tp = any_thread_of_process (inf->pid);
  357.           if (!tp)
  358.             error (_("Inferior has no threads."));

  359.           switch_to_thread (tp->ptid);
  360.         }
  361.     }
  362.   else
  363.     {
  364.       set_current_inferior (inf);
  365.       switch_to_thread (null_ptid);
  366.       set_current_program_space (inf->pspace);
  367.     }
  368.   mi_execute_cli_command (run_cmd, mi_async_p (),
  369.                           mi_async_p () ? "&" : NULL);
  370.   return 0;
  371. }

  372. void
  373. mi_cmd_exec_run (char *command, char **argv, int argc)
  374. {
  375.   int i;
  376.   int start_p = 0;

  377.   /* Parse the command options.  */
  378.   enum opt
  379.     {
  380.       START_OPT,
  381.     };
  382.   static const struct mi_opt opts[] =
  383.     {
  384.         {"-start", START_OPT, 0},
  385.         {NULL, 0, 0},
  386.     };

  387.   int oind = 0;
  388.   char *oarg;

  389.   while (1)
  390.     {
  391.       int opt = mi_getopt ("-exec-run", argc, argv, opts, &oind, &oarg);

  392.       if (opt < 0)
  393.         break;
  394.       switch ((enum opt) opt)
  395.         {
  396.         case START_OPT:
  397.           start_p = 1;
  398.           break;
  399.         }
  400.     }

  401.   /* This command does not accept any argument.  Make sure the user
  402.      did not provide any.  */
  403.   if (oind != argc)
  404.     error (_("Invalid argument: %s"), argv[oind]);

  405.   if (current_context->all)
  406.     {
  407.       struct cleanup *back_to = save_current_space_and_thread ();

  408.       iterate_over_inferiors (run_one_inferior, &start_p);
  409.       do_cleanups (back_to);
  410.     }
  411.   else
  412.     {
  413.       const char *run_cmd = start_p ? "start" : "run";

  414.       mi_execute_cli_command (run_cmd, mi_async_p (),
  415.                               mi_async_p () ? "&" : NULL);
  416.     }
  417. }


  418. static int
  419. find_thread_of_process (struct thread_info *ti, void *p)
  420. {
  421.   int pid = *(int *)p;

  422.   if (ptid_get_pid (ti->ptid) == pid && !is_exited (ti->ptid))
  423.     return 1;

  424.   return 0;
  425. }

  426. void
  427. mi_cmd_target_detach (char *command, char **argv, int argc)
  428. {
  429.   if (argc != 0 && argc != 1)
  430.     error (_("Usage: -target-detach [pid | thread-group]"));

  431.   if (argc == 1)
  432.     {
  433.       struct thread_info *tp;
  434.       char *end = argv[0];
  435.       int pid;

  436.       /* First see if we are dealing with a thread-group id.  */
  437.       if (*argv[0] == 'i')
  438.         {
  439.           struct inferior *inf;
  440.           int id = strtoul (argv[0] + 1, &end, 0);

  441.           if (*end != '\0')
  442.             error (_("Invalid syntax of thread-group id '%s'"), argv[0]);

  443.           inf = find_inferior_id (id);
  444.           if (!inf)
  445.             error (_("Non-existent thread-group id '%d'"), id);

  446.           pid = inf->pid;
  447.         }
  448.       else
  449.         {
  450.           /* We must be dealing with a pid.  */
  451.           pid = strtol (argv[0], &end, 10);

  452.           if (*end != '\0')
  453.             error (_("Invalid identifier '%s'"), argv[0]);
  454.         }

  455.       /* Pick any thread in the desired process.  Current
  456.          target_detach detaches from the parent of inferior_ptid.  */
  457.       tp = iterate_over_threads (find_thread_of_process, &pid);
  458.       if (!tp)
  459.         error (_("Thread group is empty"));

  460.       switch_to_thread (tp->ptid);
  461.     }

  462.   detach_command (NULL, 0);
  463. }

  464. void
  465. mi_cmd_thread_select (char *command, char **argv, int argc)
  466. {
  467.   enum gdb_rc rc;
  468.   char *mi_error_message;

  469.   if (argc != 1)
  470.     error (_("-thread-select: USAGE: threadnum."));

  471.   rc = gdb_thread_select (current_uiout, argv[0], &mi_error_message);

  472.   if (rc == GDB_RC_FAIL)
  473.     {
  474.       make_cleanup (xfree, mi_error_message);
  475.       error ("%s", mi_error_message);
  476.     }
  477. }

  478. void
  479. mi_cmd_thread_list_ids (char *command, char **argv, int argc)
  480. {
  481.   enum gdb_rc rc;
  482.   char *mi_error_message;

  483.   if (argc != 0)
  484.     error (_("-thread-list-ids: No arguments required."));

  485.   rc = gdb_list_thread_ids (current_uiout, &mi_error_message);

  486.   if (rc == GDB_RC_FAIL)
  487.     {
  488.       make_cleanup (xfree, mi_error_message);
  489.       error ("%s", mi_error_message);
  490.     }
  491. }

  492. void
  493. mi_cmd_thread_info (char *command, char **argv, int argc)
  494. {
  495.   if (argc != 0 && argc != 1)
  496.     error (_("Invalid MI command"));

  497.   print_thread_info (current_uiout, argv[0], -1);
  498. }

  499. struct collect_cores_data
  500. {
  501.   int pid;

  502.   VEC (int) *cores;
  503. };

  504. static int
  505. collect_cores (struct thread_info *ti, void *xdata)
  506. {
  507.   struct collect_cores_data *data = xdata;

  508.   if (ptid_get_pid (ti->ptid) == data->pid)
  509.     {
  510.       int core = target_core_of_thread (ti->ptid);

  511.       if (core != -1)
  512.         VEC_safe_push (int, data->cores, core);
  513.     }

  514.   return 0;
  515. }

  516. static int *
  517. unique (int *b, int *e)
  518. {
  519.   int *d = b;

  520.   while (++b != e)
  521.     if (*d != *b)
  522.       *++d = *b;
  523.   return ++d;
  524. }

  525. struct print_one_inferior_data
  526. {
  527.   int recurse;
  528.   VEC (int) *inferiors;
  529. };

  530. static int
  531. print_one_inferior (struct inferior *inferior, void *xdata)
  532. {
  533.   struct print_one_inferior_data *top_data = xdata;
  534.   struct ui_out *uiout = current_uiout;

  535.   if (VEC_empty (int, top_data->inferiors)
  536.       || bsearch (&(inferior->pid), VEC_address (int, top_data->inferiors),
  537.                   VEC_length (int, top_data->inferiors), sizeof (int),
  538.                   compare_positive_ints))
  539.     {
  540.       struct collect_cores_data data;
  541.       struct cleanup *back_to
  542.         = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  543.       ui_out_field_fmt (uiout, "id", "i%d", inferior->num);
  544.       ui_out_field_string (uiout, "type", "process");
  545.       if (inferior->has_exit_code)
  546.         ui_out_field_string (uiout, "exit-code",
  547.                              int_string (inferior->exit_code, 8, 0, 0, 1));
  548.       if (inferior->pid != 0)
  549.         ui_out_field_int (uiout, "pid", inferior->pid);

  550.       if (inferior->pspace->pspace_exec_filename != NULL)
  551.         {
  552.           ui_out_field_string (uiout, "executable",
  553.                                inferior->pspace->pspace_exec_filename);
  554.         }

  555.       data.cores = 0;
  556.       if (inferior->pid != 0)
  557.         {
  558.           data.pid = inferior->pid;
  559.           iterate_over_threads (collect_cores, &data);
  560.         }

  561.       if (!VEC_empty (int, data.cores))
  562.         {
  563.           int *b, *e;
  564.           struct cleanup *back_to_2 =
  565.             make_cleanup_ui_out_list_begin_end (uiout, "cores");

  566.           qsort (VEC_address (int, data.cores),
  567.                  VEC_length (int, data.cores), sizeof (int),
  568.                  compare_positive_ints);

  569.           b = VEC_address (int, data.cores);
  570.           e = b + VEC_length (int, data.cores);
  571.           e = unique (b, e);

  572.           for (; b != e; ++b)
  573.             ui_out_field_int (uiout, NULL, *b);

  574.           do_cleanups (back_to_2);
  575.         }

  576.       if (top_data->recurse)
  577.         print_thread_info (uiout, NULL, inferior->pid);

  578.       do_cleanups (back_to);
  579.     }

  580.   return 0;
  581. }

  582. /* Output a field named 'cores' with a list as the value.  The
  583.    elements of the list are obtained by splitting 'cores' on
  584.    comma.  */

  585. static void
  586. output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
  587. {
  588.   struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
  589.                                                                 field_name);
  590.   char *cores = xstrdup (xcores);
  591.   char *p = cores;

  592.   make_cleanup (xfree, cores);

  593.   for (p = strtok (p, ","); pp = strtok (NULL, ","))
  594.     ui_out_field_string (uiout, NULL, p);

  595.   do_cleanups (back_to);
  596. }

  597. static void
  598. free_vector_of_ints (void *xvector)
  599. {
  600.   VEC (int) **vector = xvector;

  601.   VEC_free (int, *vector);
  602. }

  603. static void
  604. do_nothing (splay_tree_key k)
  605. {
  606. }

  607. static void
  608. free_vector_of_osdata_items (splay_tree_value xvalue)
  609. {
  610.   VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue;

  611.   /* We don't free the items itself, it will be done separately.  */
  612.   VEC_free (osdata_item_s, value);
  613. }

  614. static int
  615. splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb)
  616. {
  617.   int a = xa;
  618.   int b = xb;

  619.   return a - b;
  620. }

  621. static void
  622. free_splay_tree (void *xt)
  623. {
  624.   splay_tree t = xt;
  625.   splay_tree_delete (t);
  626. }

  627. static void
  628. list_available_thread_groups (VEC (int) *ids, int recurse)
  629. {
  630.   struct osdata *data;
  631.   struct osdata_item *item;
  632.   int ix_items;
  633.   struct ui_out *uiout = current_uiout;
  634.   struct cleanup *cleanup;

  635.   /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
  636.      The vector contains information about all threads for the given pid.
  637.      This is assigned an initial value to avoid "may be used uninitialized"
  638.      warning from gcc.  */
  639.   splay_tree tree = NULL;

  640.   /* get_osdata will throw if it cannot return data.  */
  641.   data = get_osdata ("processes");
  642.   cleanup = make_cleanup_osdata_free (data);

  643.   if (recurse)
  644.     {
  645.       struct osdata *threads = get_osdata ("threads");

  646.       make_cleanup_osdata_free (threads);
  647.       tree = splay_tree_new (splay_tree_int_comparator,
  648.                              do_nothing,
  649.                              free_vector_of_osdata_items);
  650.       make_cleanup (free_splay_tree, tree);

  651.       for (ix_items = 0;
  652.            VEC_iterate (osdata_item_s, threads->items,
  653.                         ix_items, item);
  654.            ix_items++)
  655.         {
  656.           const char *pid = get_osdata_column (item, "pid");
  657.           int pid_i = strtoul (pid, NULL, 0);
  658.           VEC (osdata_item_s) *vec = 0;

  659.           splay_tree_node n = splay_tree_lookup (tree, pid_i);
  660.           if (!n)
  661.             {
  662.               VEC_safe_push (osdata_item_s, vec, item);
  663.               splay_tree_insert (tree, pid_i, (splay_tree_value)vec);
  664.             }
  665.           else
  666.             {
  667.               vec = (VEC (osdata_item_s) *) n->value;
  668.               VEC_safe_push (osdata_item_s, vec, item);
  669.               n->value = (splay_tree_value) vec;
  670.             }
  671.         }
  672.     }

  673.   make_cleanup_ui_out_list_begin_end (uiout, "groups");

  674.   for (ix_items = 0;
  675.        VEC_iterate (osdata_item_s, data->items,
  676.                     ix_items, item);
  677.        ix_items++)
  678.     {
  679.       struct cleanup *back_to;

  680.       const char *pid = get_osdata_column (item, "pid");
  681.       const char *cmd = get_osdata_column (item, "command");
  682.       const char *user = get_osdata_column (item, "user");
  683.       const char *cores = get_osdata_column (item, "cores");

  684.       int pid_i = strtoul (pid, NULL, 0);

  685.       /* At present, the target will return all available processes
  686.          and if information about specific ones was required, we filter
  687.          undesired processes here.  */
  688.       if (ids && bsearch (&pid_i, VEC_address (int, ids),
  689.                           VEC_length (int, ids),
  690.                           sizeof (int), compare_positive_ints) == NULL)
  691.         continue;


  692.       back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  693.       ui_out_field_fmt (uiout, "id", "%s", pid);
  694.       ui_out_field_string (uiout, "type", "process");
  695.       if (cmd)
  696.         ui_out_field_string (uiout, "description", cmd);
  697.       if (user)
  698.         ui_out_field_string (uiout, "user", user);
  699.       if (cores)
  700.         output_cores (uiout, "cores", cores);

  701.       if (recurse)
  702.         {
  703.           splay_tree_node n = splay_tree_lookup (tree, pid_i);
  704.           if (n)
  705.             {
  706.               VEC (osdata_item_s) *children = (VEC (osdata_item_s) *) n->value;
  707.               struct osdata_item *child;
  708.               int ix_child;

  709.               make_cleanup_ui_out_list_begin_end (uiout, "threads");

  710.               for (ix_child = 0;
  711.                    VEC_iterate (osdata_item_s, children, ix_child, child);
  712.                    ++ix_child)
  713.                 {
  714.                   struct cleanup *back_to_2 =
  715.                     make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  716.                   const char *tid = get_osdata_column (child, "tid");
  717.                   const char *tcore = get_osdata_column (child, "core");

  718.                   ui_out_field_string (uiout, "id", tid);
  719.                   if (tcore)
  720.                     ui_out_field_string (uiout, "core", tcore);

  721.                   do_cleanups (back_to_2);
  722.                 }
  723.             }
  724.         }

  725.       do_cleanups (back_to);
  726.     }

  727.   do_cleanups (cleanup);
  728. }

  729. void
  730. mi_cmd_list_thread_groups (char *command, char **argv, int argc)
  731. {
  732.   struct ui_out *uiout = current_uiout;
  733.   struct cleanup *back_to;
  734.   int available = 0;
  735.   int recurse = 0;
  736.   VEC (int) *ids = 0;

  737.   enum opt
  738.   {
  739.     AVAILABLE_OPT, RECURSE_OPT
  740.   };
  741.   static const struct mi_opt opts[] =
  742.     {
  743.       {"-available", AVAILABLE_OPT, 0},
  744.       {"-recurse", RECURSE_OPT, 1},
  745.       { 0, 0, 0 }
  746.     };

  747.   int oind = 0;
  748.   char *oarg;

  749.   while (1)
  750.     {
  751.       int opt = mi_getopt ("-list-thread-groups", argc, argv, opts,
  752.                            &oind, &oarg);

  753.       if (opt < 0)
  754.         break;
  755.       switch ((enum opt) opt)
  756.         {
  757.         case AVAILABLE_OPT:
  758.           available = 1;
  759.           break;
  760.         case RECURSE_OPT:
  761.           if (strcmp (oarg, "0") == 0)
  762.             ;
  763.           else if (strcmp (oarg, "1") == 0)
  764.             recurse = 1;
  765.           else
  766.             error (_("only '0' and '1' are valid values "
  767.                      "for the '--recurse' option"));
  768.           break;
  769.         }
  770.     }

  771.   for (; oind < argc; ++oind)
  772.     {
  773.       char *end;
  774.       int inf;

  775.       if (*(argv[oind]) != 'i')
  776.         error (_("invalid syntax of group id '%s'"), argv[oind]);

  777.       inf = strtoul (argv[oind] + 1, &end, 0);

  778.       if (*end != '\0')
  779.         error (_("invalid syntax of group id '%s'"), argv[oind]);
  780.       VEC_safe_push (int, ids, inf);
  781.     }
  782.   if (VEC_length (int, ids) > 1)
  783.     qsort (VEC_address (int, ids),
  784.            VEC_length (int, ids),
  785.            sizeof (int), compare_positive_ints);

  786.   back_to = make_cleanup (free_vector_of_ints, &ids);

  787.   if (available)
  788.     {
  789.       list_available_thread_groups (ids, recurse);
  790.     }
  791.   else if (VEC_length (int, ids) == 1)
  792.     {
  793.       /* Local thread groups, single id.  */
  794.       int id = *VEC_address (int, ids);
  795.       struct inferior *inf = find_inferior_id (id);

  796.       if (!inf)
  797.         error (_("Non-existent thread group id '%d'"), id);

  798.       print_thread_info (uiout, NULL, inf->pid);
  799.     }
  800.   else
  801.     {
  802.       struct print_one_inferior_data data;

  803.       data.recurse = recurse;
  804.       data.inferiors = ids;

  805.       /* Local thread groups.  Either no explicit ids -- and we
  806.          print everything, or several explicit ids.  In both cases,
  807.          we print more than one group, and have to use 'groups'
  808.          as the top-level element.  */
  809.       make_cleanup_ui_out_list_begin_end (uiout, "groups");
  810.       update_thread_list ();
  811.       iterate_over_inferiors (print_one_inferior, &data);
  812.     }

  813.   do_cleanups (back_to);
  814. }

  815. void
  816. mi_cmd_data_list_register_names (char *command, char **argv, int argc)
  817. {
  818.   struct gdbarch *gdbarch;
  819.   struct ui_out *uiout = current_uiout;
  820.   int regnum, numregs;
  821.   int i;
  822.   struct cleanup *cleanup;

  823.   /* Note that the test for a valid register must include checking the
  824.      gdbarch_register_name because gdbarch_num_regs may be allocated
  825.      for the union of the register sets within a family of related
  826.      processors.  In this case, some entries of gdbarch_register_name
  827.      will change depending upon the particular processor being
  828.      debugged.  */

  829.   gdbarch = get_current_arch ();
  830.   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  831.   cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");

  832.   if (argc == 0)                /* No args, just do all the regs.  */
  833.     {
  834.       for (regnum = 0;
  835.            regnum < numregs;
  836.            regnum++)
  837.         {
  838.           if (gdbarch_register_name (gdbarch, regnum) == NULL
  839.               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  840.             ui_out_field_string (uiout, NULL, "");
  841.           else
  842.             ui_out_field_string (uiout, NULL,
  843.                                  gdbarch_register_name (gdbarch, regnum));
  844.         }
  845.     }

  846.   /* Else, list of register #s, just do listed regs.  */
  847.   for (i = 0; i < argc; i++)
  848.     {
  849.       regnum = atoi (argv[i]);
  850.       if (regnum < 0 || regnum >= numregs)
  851.         error (_("bad register number"));

  852.       if (gdbarch_register_name (gdbarch, regnum) == NULL
  853.           || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  854.         ui_out_field_string (uiout, NULL, "");
  855.       else
  856.         ui_out_field_string (uiout, NULL,
  857.                              gdbarch_register_name (gdbarch, regnum));
  858.     }
  859.   do_cleanups (cleanup);
  860. }

  861. void
  862. mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
  863. {
  864.   static struct regcache *this_regs = NULL;
  865.   struct ui_out *uiout = current_uiout;
  866.   struct regcache *prev_regs;
  867.   struct gdbarch *gdbarch;
  868.   int regnum, numregs, changed;
  869.   int i;
  870.   struct cleanup *cleanup;

  871.   /* The last time we visited this function, the current frame's
  872.      register contents were saved in THIS_REGS.  Move THIS_REGS over
  873.      to PREV_REGS, and refresh THIS_REGS with the now-current register
  874.      contents.  */

  875.   prev_regs = this_regs;
  876.   this_regs = frame_save_as_regcache (get_selected_frame (NULL));
  877.   cleanup = make_cleanup_regcache_xfree (prev_regs);

  878.   /* Note that the test for a valid register must include checking the
  879.      gdbarch_register_name because gdbarch_num_regs may be allocated
  880.      for the union of the register sets within a family of related
  881.      processors.  In this case, some entries of gdbarch_register_name
  882.      will change depending upon the particular processor being
  883.      debugged.  */

  884.   gdbarch = get_regcache_arch (this_regs);
  885.   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  886.   make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");

  887.   if (argc == 0)
  888.     {
  889.       /* No args, just do all the regs.  */
  890.       for (regnum = 0;
  891.            regnum < numregs;
  892.            regnum++)
  893.         {
  894.           if (gdbarch_register_name (gdbarch, regnum) == NULL
  895.               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  896.             continue;
  897.           changed = register_changed_p (regnum, prev_regs, this_regs);
  898.           if (changed < 0)
  899.             error (_("-data-list-changed-registers: "
  900.                      "Unable to read register contents."));
  901.           else if (changed)
  902.             ui_out_field_int (uiout, NULL, regnum);
  903.         }
  904.     }

  905.   /* Else, list of register #s, just do listed regs.  */
  906.   for (i = 0; i < argc; i++)
  907.     {
  908.       regnum = atoi (argv[i]);

  909.       if (regnum >= 0
  910.           && regnum < numregs
  911.           && gdbarch_register_name (gdbarch, regnum) != NULL
  912.           && *gdbarch_register_name (gdbarch, regnum) != '\000')
  913.         {
  914.           changed = register_changed_p (regnum, prev_regs, this_regs);
  915.           if (changed < 0)
  916.             error (_("-data-list-changed-registers: "
  917.                      "Unable to read register contents."));
  918.           else if (changed)
  919.             ui_out_field_int (uiout, NULL, regnum);
  920.         }
  921.       else
  922.         error (_("bad register number"));
  923.     }
  924.   do_cleanups (cleanup);
  925. }

  926. static int
  927. register_changed_p (int regnum, struct regcache *prev_regs,
  928.                     struct regcache *this_regs)
  929. {
  930.   struct gdbarch *gdbarch = get_regcache_arch (this_regs);
  931.   gdb_byte prev_buffer[MAX_REGISTER_SIZE];
  932.   gdb_byte this_buffer[MAX_REGISTER_SIZE];
  933.   enum register_status prev_status;
  934.   enum register_status this_status;

  935.   /* First time through or after gdbarch change consider all registers
  936.      as changed.  */
  937.   if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch)
  938.     return 1;

  939.   /* Get register contents and compare.  */
  940.   prev_status = regcache_cooked_read (prev_regs, regnum, prev_buffer);
  941.   this_status = regcache_cooked_read (this_regs, regnum, this_buffer);

  942.   if (this_status != prev_status)
  943.     return 1;
  944.   else if (this_status == REG_VALID)
  945.     return memcmp (prev_buffer, this_buffer,
  946.                    register_size (gdbarch, regnum)) != 0;
  947.   else
  948.     return 0;
  949. }

  950. /* Return a list of register number and value pairs.  The valid
  951.    arguments expected are: a letter indicating the format in which to
  952.    display the registers contents.  This can be one of: x
  953.    (hexadecimal), d (decimal), N (natural), t (binary), o (octal), r
  954.    (raw).  After the format argument there can be a sequence of
  955.    numbers, indicating which registers to fetch the content of.  If
  956.    the format is the only argument, a list of all the registers with
  957.    their values is returned.  */

  958. void
  959. mi_cmd_data_list_register_values (char *command, char **argv, int argc)
  960. {
  961.   struct ui_out *uiout = current_uiout;
  962.   struct frame_info *frame;
  963.   struct gdbarch *gdbarch;
  964.   int regnum, numregs, format;
  965.   int i;
  966.   struct cleanup *list_cleanup;
  967.   int skip_unavailable = 0;
  968.   int oind = 0;
  969.   enum opt
  970.   {
  971.     SKIP_UNAVAILABLE,
  972.   };
  973.   static const struct mi_opt opts[] =
  974.     {
  975.       {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
  976.       { 0, 0, 0 }
  977.     };

  978.   /* Note that the test for a valid register must include checking the
  979.      gdbarch_register_name because gdbarch_num_regs may be allocated
  980.      for the union of the register sets within a family of related
  981.      processors.  In this case, some entries of gdbarch_register_name
  982.      will change depending upon the particular processor being
  983.      debugged.  */

  984.   while (1)
  985.     {
  986.       char *oarg;
  987.       int opt = mi_getopt ("-data-list-register-values", argc, argv,
  988.                            opts, &oind, &oarg);

  989.       if (opt < 0)
  990.         break;
  991.       switch ((enum opt) opt)
  992.         {
  993.         case SKIP_UNAVAILABLE:
  994.           skip_unavailable = 1;
  995.           break;
  996.         }
  997.     }

  998.   if (argc - oind < 1)
  999.     error (_("-data-list-register-values: Usage: "
  1000.              "-data-list-register-values [--skip-unavailable] <format>"
  1001.              " [<regnum1>...<regnumN>]"));

  1002.   format = (int) argv[oind][0];

  1003.   frame = get_selected_frame (NULL);
  1004.   gdbarch = get_frame_arch (frame);
  1005.   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  1006.   list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");

  1007.   if (argc - oind == 1)
  1008.     {
  1009.       /* No args, beside the format: do all the regs.  */
  1010.       for (regnum = 0;
  1011.            regnum < numregs;
  1012.            regnum++)
  1013.         {
  1014.           if (gdbarch_register_name (gdbarch, regnum) == NULL
  1015.               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1016.             continue;

  1017.           output_register (frame, regnum, format, skip_unavailable);
  1018.         }
  1019.     }

  1020.   /* Else, list of register #s, just do listed regs.  */
  1021.   for (i = 1 + oind; i < argc; i++)
  1022.     {
  1023.       regnum = atoi (argv[i]);

  1024.       if (regnum >= 0
  1025.           && regnum < numregs
  1026.           && gdbarch_register_name (gdbarch, regnum) != NULL
  1027.           && *gdbarch_register_name (gdbarch, regnum) != '\000')
  1028.         output_register (frame, regnum, format, skip_unavailable);
  1029.       else
  1030.         error (_("bad register number"));
  1031.     }
  1032.   do_cleanups (list_cleanup);
  1033. }

  1034. /* Output one register REGNUM's contents in the desired FORMAT.  If
  1035.    SKIP_UNAVAILABLE is true, skip the register if it is
  1036.    unavailable.  */

  1037. static void
  1038. output_register (struct frame_info *frame, int regnum, int format,
  1039.                  int skip_unavailable)
  1040. {
  1041.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1042.   struct ui_out *uiout = current_uiout;
  1043.   struct value *val = value_of_register (regnum, frame);
  1044.   struct cleanup *tuple_cleanup;
  1045.   struct value_print_options opts;
  1046.   struct ui_file *stb;

  1047.   if (skip_unavailable && !value_entirely_available (val))
  1048.     return;

  1049.   tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  1050.   ui_out_field_int (uiout, "number", regnum);

  1051.   if (format == 'N')
  1052.     format = 0;

  1053.   if (format == 'r')
  1054.     format = 'z';

  1055.   stb = mem_fileopen ();
  1056.   make_cleanup_ui_file_delete (stb);

  1057.   get_formatted_print_options (&opts, format);
  1058.   opts.deref_ref = 1;
  1059.   val_print (value_type (val),
  1060.              value_contents_for_printing (val),
  1061.              value_embedded_offset (val), 0,
  1062.              stb, 0, val, &opts, current_language);
  1063.   ui_out_field_stream (uiout, "value", stb);

  1064.   do_cleanups (tuple_cleanup);
  1065. }

  1066. /* Write given values into registers. The registers and values are
  1067.    given as pairs.  The corresponding MI command is
  1068.    -data-write-register-values <format>
  1069.                                [<regnum1> <value1>...<regnumN> <valueN>] */
  1070. void
  1071. mi_cmd_data_write_register_values (char *command, char **argv, int argc)
  1072. {
  1073.   struct regcache *regcache;
  1074.   struct gdbarch *gdbarch;
  1075.   int numregs, i;

  1076.   /* Note that the test for a valid register must include checking the
  1077.      gdbarch_register_name because gdbarch_num_regs may be allocated
  1078.      for the union of the register sets within a family of related
  1079.      processors.  In this case, some entries of gdbarch_register_name
  1080.      will change depending upon the particular processor being
  1081.      debugged.  */

  1082.   regcache = get_current_regcache ();
  1083.   gdbarch = get_regcache_arch (regcache);
  1084.   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  1085.   if (argc == 0)
  1086.     error (_("-data-write-register-values: Usage: -data-write-register-"
  1087.              "values <format> [<regnum1> <value1>...<regnumN> <valueN>]"));

  1088.   if (!target_has_registers)
  1089.     error (_("-data-write-register-values: No registers."));

  1090.   if (!(argc - 1))
  1091.     error (_("-data-write-register-values: No regs and values specified."));

  1092.   if ((argc - 1) % 2)
  1093.     error (_("-data-write-register-values: "
  1094.              "Regs and vals are not in pairs."));

  1095.   for (i = 1; i < argc; i = i + 2)
  1096.     {
  1097.       int regnum = atoi (argv[i]);

  1098.       if (regnum >= 0 && regnum < numregs
  1099.           && gdbarch_register_name (gdbarch, regnum)
  1100.           && *gdbarch_register_name (gdbarch, regnum))
  1101.         {
  1102.           LONGEST value;

  1103.           /* Get the value as a number.  */
  1104.           value = parse_and_eval_address (argv[i + 1]);

  1105.           /* Write it down.  */
  1106.           regcache_cooked_write_signed (regcache, regnum, value);
  1107.         }
  1108.       else
  1109.         error (_("bad register number"));
  1110.     }
  1111. }

  1112. /* Evaluate the value of the argument.  The argument is an
  1113.    expression. If the expression contains spaces it needs to be
  1114.    included in double quotes.  */

  1115. void
  1116. mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
  1117. {
  1118.   struct expression *expr;
  1119.   struct cleanup *old_chain;
  1120.   struct value *val;
  1121.   struct ui_file *stb;
  1122.   struct value_print_options opts;
  1123.   struct ui_out *uiout = current_uiout;

  1124.   stb = mem_fileopen ();
  1125.   old_chain = make_cleanup_ui_file_delete (stb);

  1126.   if (argc != 1)
  1127.     error (_("-data-evaluate-expression: "
  1128.              "Usage: -data-evaluate-expression expression"));

  1129.   expr = parse_expression (argv[0]);

  1130.   make_cleanup (free_current_contents, &expr);

  1131.   val = evaluate_expression (expr);

  1132.   /* Print the result of the expression evaluation.  */
  1133.   get_user_print_options (&opts);
  1134.   opts.deref_ref = 0;
  1135.   common_val_print (val, stb, 0, &opts, current_language);

  1136.   ui_out_field_stream (uiout, "value", stb);

  1137.   do_cleanups (old_chain);
  1138. }

  1139. /* This is the -data-read-memory command.

  1140.    ADDR: start address of data to be dumped.
  1141.    WORD-FORMAT: a char indicating format for the ``word''.  See
  1142.    the ``x'' command.
  1143.    WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
  1144.    NR_ROW: Number of rows.
  1145.    NR_COL: The number of colums (words per row).
  1146.    ASCHAR: (OPTIONAL) Append an ascii character dump to each row.  Use
  1147.    ASCHAR for unprintable characters.

  1148.    Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
  1149.    displayes them.  Returns:

  1150.    {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}

  1151.    Returns:
  1152.    The number of bytes read is SIZE*ROW*COL.  */

  1153. void
  1154. mi_cmd_data_read_memory (char *command, char **argv, int argc)
  1155. {
  1156.   struct gdbarch *gdbarch = get_current_arch ();
  1157.   struct ui_out *uiout = current_uiout;
  1158.   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
  1159.   CORE_ADDR addr;
  1160.   long total_bytes, nr_cols, nr_rows;
  1161.   char word_format;
  1162.   struct type *word_type;
  1163.   long word_size;
  1164.   char word_asize;
  1165.   char aschar;
  1166.   gdb_byte *mbuf;
  1167.   int nr_bytes;
  1168.   long offset = 0;
  1169.   int oind = 0;
  1170.   char *oarg;
  1171.   enum opt
  1172.   {
  1173.     OFFSET_OPT
  1174.   };
  1175.   static const struct mi_opt opts[] =
  1176.     {
  1177.       {"o", OFFSET_OPT, 1},
  1178.       { 0, 0, 0 }
  1179.     };

  1180.   while (1)
  1181.     {
  1182.       int opt = mi_getopt ("-data-read-memory", argc, argv, opts,
  1183.                            &oind, &oarg);

  1184.       if (opt < 0)
  1185.         break;
  1186.       switch ((enum opt) opt)
  1187.         {
  1188.         case OFFSET_OPT:
  1189.           offset = atol (oarg);
  1190.           break;
  1191.         }
  1192.     }
  1193.   argv += oind;
  1194.   argc -= oind;

  1195.   if (argc < 5 || argc > 6)
  1196.     error (_("-data-read-memory: Usage: "
  1197.              "ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."));

  1198.   /* Extract all the arguments. */

  1199.   /* Start address of the memory dump.  */
  1200.   addr = parse_and_eval_address (argv[0]) + offset;
  1201.   /* The format character to use when displaying a memory word.  See
  1202.      the ``x'' command.  */
  1203.   word_format = argv[1][0];
  1204.   /* The size of the memory word.  */
  1205.   word_size = atol (argv[2]);
  1206.   switch (word_size)
  1207.     {
  1208.     case 1:
  1209.       word_type = builtin_type (gdbarch)->builtin_int8;
  1210.       word_asize = 'b';
  1211.       break;
  1212.     case 2:
  1213.       word_type = builtin_type (gdbarch)->builtin_int16;
  1214.       word_asize = 'h';
  1215.       break;
  1216.     case 4:
  1217.       word_type = builtin_type (gdbarch)->builtin_int32;
  1218.       word_asize = 'w';
  1219.       break;
  1220.     case 8:
  1221.       word_type = builtin_type (gdbarch)->builtin_int64;
  1222.       word_asize = 'g';
  1223.       break;
  1224.     default:
  1225.       word_type = builtin_type (gdbarch)->builtin_int8;
  1226.       word_asize = 'b';
  1227.     }
  1228.   /* The number of rows.  */
  1229.   nr_rows = atol (argv[3]);
  1230.   if (nr_rows <= 0)
  1231.     error (_("-data-read-memory: invalid number of rows."));

  1232.   /* Number of bytes per row.  */
  1233.   nr_cols = atol (argv[4]);
  1234.   if (nr_cols <= 0)
  1235.     error (_("-data-read-memory: invalid number of columns."));

  1236.   /* The un-printable character when printing ascii.  */
  1237.   if (argc == 6)
  1238.     aschar = *argv[5];
  1239.   else
  1240.     aschar = 0;

  1241.   /* Create a buffer and read it in.  */
  1242.   total_bytes = word_size * nr_rows * nr_cols;
  1243.   mbuf = xcalloc (total_bytes, 1);
  1244.   make_cleanup (xfree, mbuf);

  1245.   /* Dispatch memory reads to the topmost target, not the flattened
  1246.      current_target.  */
  1247.   nr_bytes = target_read (current_target.beneath,
  1248.                           TARGET_OBJECT_MEMORY, NULL, mbuf,
  1249.                           addr, total_bytes);
  1250.   if (nr_bytes <= 0)
  1251.     error (_("Unable to read memory."));

  1252.   /* Output the header information.  */
  1253.   ui_out_field_core_addr (uiout, "addr", gdbarch, addr);
  1254.   ui_out_field_int (uiout, "nr-bytes", nr_bytes);
  1255.   ui_out_field_int (uiout, "total-bytes", total_bytes);
  1256.   ui_out_field_core_addr (uiout, "next-row",
  1257.                           gdbarch, addr + word_size * nr_cols);
  1258.   ui_out_field_core_addr (uiout, "prev-row",
  1259.                           gdbarch, addr - word_size * nr_cols);
  1260.   ui_out_field_core_addr (uiout, "next-page", gdbarch, addr + total_bytes);
  1261.   ui_out_field_core_addr (uiout, "prev-page", gdbarch, addr - total_bytes);

  1262.   /* Build the result as a two dimentional table.  */
  1263.   {
  1264.     struct ui_file *stream;
  1265.     struct cleanup *cleanup_stream;
  1266.     int row;
  1267.     int row_byte;

  1268.     stream = mem_fileopen ();
  1269.     cleanup_stream = make_cleanup_ui_file_delete (stream);

  1270.     make_cleanup_ui_out_list_begin_end (uiout, "memory");
  1271.     for (row = 0, row_byte = 0;
  1272.          row < nr_rows;
  1273.          row++, row_byte += nr_cols * word_size)
  1274.       {
  1275.         int col;
  1276.         int col_byte;
  1277.         struct cleanup *cleanup_tuple;
  1278.         struct cleanup *cleanup_list_data;
  1279.         struct value_print_options opts;

  1280.         cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  1281.         ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte);
  1282.         /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
  1283.            row_byte); */
  1284.         cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
  1285.         get_formatted_print_options (&opts, word_format);
  1286.         for (col = 0, col_byte = row_byte;
  1287.              col < nr_cols;
  1288.              col++, col_byte += word_size)
  1289.           {
  1290.             if (col_byte + word_size > nr_bytes)
  1291.               {
  1292.                 ui_out_field_string (uiout, NULL, "N/A");
  1293.               }
  1294.             else
  1295.               {
  1296.                 ui_file_rewind (stream);
  1297.                 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
  1298.                                         word_asize, stream);
  1299.                 ui_out_field_stream (uiout, NULL, stream);
  1300.               }
  1301.           }
  1302.         do_cleanups (cleanup_list_data);
  1303.         if (aschar)
  1304.           {
  1305.             int byte;

  1306.             ui_file_rewind (stream);
  1307.             for (byte = row_byte;
  1308.                  byte < row_byte + word_size * nr_cols; byte++)
  1309.               {
  1310.                 if (byte >= nr_bytes)
  1311.                   fputc_unfiltered ('X', stream);
  1312.                 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
  1313.                   fputc_unfiltered (aschar, stream);
  1314.                 else
  1315.                   fputc_unfiltered (mbuf[byte], stream);
  1316.               }
  1317.             ui_out_field_stream (uiout, "ascii", stream);
  1318.           }
  1319.         do_cleanups (cleanup_tuple);
  1320.       }
  1321.     do_cleanups (cleanup_stream);
  1322.   }
  1323.   do_cleanups (cleanups);
  1324. }

  1325. void
  1326. mi_cmd_data_read_memory_bytes (char *command, char **argv, int argc)
  1327. {
  1328.   struct gdbarch *gdbarch = get_current_arch ();
  1329.   struct ui_out *uiout = current_uiout;
  1330.   struct cleanup *cleanups;
  1331.   CORE_ADDR addr;
  1332.   LONGEST length;
  1333.   memory_read_result_s *read_result;
  1334.   int ix;
  1335.   VEC(memory_read_result_s) *result;
  1336.   long offset = 0;
  1337.   int oind = 0;
  1338.   char *oarg;
  1339.   enum opt
  1340.   {
  1341.     OFFSET_OPT
  1342.   };
  1343.   static const struct mi_opt opts[] =
  1344.     {
  1345.       {"o", OFFSET_OPT, 1},
  1346.       { 0, 0, 0 }
  1347.     };

  1348.   while (1)
  1349.     {
  1350.       int opt = mi_getopt ("-data-read-memory-bytes", argc, argv, opts,
  1351.                            &oind, &oarg);
  1352.       if (opt < 0)
  1353.         break;
  1354.       switch ((enum opt) opt)
  1355.         {
  1356.         case OFFSET_OPT:
  1357.           offset = atol (oarg);
  1358.           break;
  1359.         }
  1360.     }
  1361.   argv += oind;
  1362.   argc -= oind;

  1363.   if (argc != 2)
  1364.     error (_("Usage: [ -o OFFSET ] ADDR LENGTH."));

  1365.   addr = parse_and_eval_address (argv[0]) + offset;
  1366.   length = atol (argv[1]);

  1367.   result = read_memory_robust (current_target.beneath, addr, length);

  1368.   cleanups = make_cleanup (free_memory_read_result_vector, result);

  1369.   if (VEC_length (memory_read_result_s, result) == 0)
  1370.     error (_("Unable to read memory."));

  1371.   make_cleanup_ui_out_list_begin_end (uiout, "memory");
  1372.   for (ix = 0;
  1373.        VEC_iterate (memory_read_result_s, result, ix, read_result);
  1374.        ++ix)
  1375.     {
  1376.       struct cleanup *t = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  1377.       char *data, *p;
  1378.       int i;

  1379.       ui_out_field_core_addr (uiout, "begin", gdbarch, read_result->begin);
  1380.       ui_out_field_core_addr (uiout, "offset", gdbarch, read_result->begin
  1381.                               - addr);
  1382.       ui_out_field_core_addr (uiout, "end", gdbarch, read_result->end);

  1383.       data = xmalloc ((read_result->end - read_result->begin) * 2 + 1);

  1384.       for (i = 0, p = data;
  1385.            i < (read_result->end - read_result->begin);
  1386.            ++i, p += 2)
  1387.         {
  1388.           sprintf (p, "%02x", read_result->data[i]);
  1389.         }
  1390.       ui_out_field_string (uiout, "contents", data);
  1391.       xfree (data);
  1392.       do_cleanups (t);
  1393.     }
  1394.   do_cleanups (cleanups);
  1395. }

  1396. /* Implementation of the -data-write_memory command.

  1397.    COLUMN_OFFSET: optional argument. Must be preceded by '-o'. The
  1398.    offset from the beginning of the memory grid row where the cell to
  1399.    be written is.
  1400.    ADDR: start address of the row in the memory grid where the memory
  1401.    cell is, if OFFSET_COLUMN is specified.  Otherwise, the address of
  1402.    the location to write to.
  1403.    FORMAT: a char indicating format for the ``word''.  See
  1404.    the ``x'' command.
  1405.    WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
  1406.    VALUE: value to be written into the memory address.

  1407.    Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).

  1408.    Prints nothing.  */

  1409. void
  1410. mi_cmd_data_write_memory (char *command, char **argv, int argc)
  1411. {
  1412.   struct gdbarch *gdbarch = get_current_arch ();
  1413.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1414.   CORE_ADDR addr;
  1415.   long word_size;
  1416.   /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
  1417.      enough when using a compiler other than GCC.  */
  1418.   LONGEST value;
  1419.   void *buffer;
  1420.   struct cleanup *old_chain;
  1421.   long offset = 0;
  1422.   int oind = 0;
  1423.   char *oarg;
  1424.   enum opt
  1425.   {
  1426.     OFFSET_OPT
  1427.   };
  1428.   static const struct mi_opt opts[] =
  1429.     {
  1430.       {"o", OFFSET_OPT, 1},
  1431.       { 0, 0, 0 }
  1432.     };

  1433.   while (1)
  1434.     {
  1435.       int opt = mi_getopt ("-data-write-memory", argc, argv, opts,
  1436.                            &oind, &oarg);

  1437.       if (opt < 0)
  1438.         break;
  1439.       switch ((enum opt) opt)
  1440.         {
  1441.         case OFFSET_OPT:
  1442.           offset = atol (oarg);
  1443.           break;
  1444.         }
  1445.     }
  1446.   argv += oind;
  1447.   argc -= oind;

  1448.   if (argc != 4)
  1449.     error (_("-data-write-memory: Usage: "
  1450.              "[-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."));

  1451.   /* Extract all the arguments.  */
  1452.   /* Start address of the memory dump.  */
  1453.   addr = parse_and_eval_address (argv[0]);
  1454.   /* The size of the memory word.  */
  1455.   word_size = atol (argv[2]);

  1456.   /* Calculate the real address of the write destination.  */
  1457.   addr += (offset * word_size);

  1458.   /* Get the value as a number.  */
  1459.   value = parse_and_eval_address (argv[3]);
  1460.   /* Get the value into an array.  */
  1461.   buffer = xmalloc (word_size);
  1462.   old_chain = make_cleanup (xfree, buffer);
  1463.   store_signed_integer (buffer, word_size, byte_order, value);
  1464.   /* Write it down to memory.  */
  1465.   write_memory_with_notification (addr, buffer, word_size);
  1466.   /* Free the buffer.  */
  1467.   do_cleanups (old_chain);
  1468. }

  1469. /* Implementation of the -data-write-memory-bytes command.

  1470.    ADDR: start address
  1471.    DATA: string of bytes to write at that address
  1472.    COUNT: number of bytes to be filled (decimal integer).  */

  1473. void
  1474. mi_cmd_data_write_memory_bytes (char *command, char **argv, int argc)
  1475. {
  1476.   CORE_ADDR addr;
  1477.   char *cdata;
  1478.   gdb_byte *data;
  1479.   gdb_byte *databuf;
  1480.   size_t len, i, steps, remainder;
  1481.   long int count, j;
  1482.   struct cleanup *back_to;

  1483.   if (argc != 2 && argc != 3)
  1484.     error (_("Usage: ADDR DATA [COUNT]."));

  1485.   addr = parse_and_eval_address (argv[0]);
  1486.   cdata = argv[1];
  1487.   if (strlen (cdata) % 2)
  1488.     error (_("Hex-encoded '%s' must have an even number of characters."),
  1489.            cdata);

  1490.   len = strlen (cdata)/2;
  1491.   if (argc == 3)
  1492.     count = strtoul (argv[2], NULL, 10);
  1493.   else
  1494.     count = len;

  1495.   databuf = xmalloc (len * sizeof (gdb_byte));
  1496.   back_to = make_cleanup (xfree, databuf);

  1497.   for (i = 0; i < len; ++i)
  1498.     {
  1499.       int x;
  1500.       if (sscanf (cdata + i * 2, "%02x", &x) != 1)
  1501.         error (_("Invalid argument"));
  1502.       databuf[i] = (gdb_byte) x;
  1503.     }

  1504.   if (len < count)
  1505.     {
  1506.       /* Pattern is made of less bytes than count:
  1507.          repeat pattern to fill memory.  */
  1508.       data = xmalloc (count);
  1509.       make_cleanup (xfree, data);

  1510.       steps = count / len;
  1511.       remainder = count % len;
  1512.       for (j = 0; j < steps; j++)
  1513.         memcpy (data + j * len, databuf, len);

  1514.       if (remainder > 0)
  1515.         memcpy (data + steps * len, databuf, remainder);
  1516.     }
  1517.   else
  1518.     {
  1519.       /* Pattern is longer than or equal to count:
  1520.          just copy count bytes.  */
  1521.       data = databuf;
  1522.     }

  1523.   write_memory_with_notification (addr, data, count);

  1524.   do_cleanups (back_to);
  1525. }

  1526. void
  1527. mi_cmd_enable_timings (char *command, char **argv, int argc)
  1528. {
  1529.   if (argc == 0)
  1530.     do_timings = 1;
  1531.   else if (argc == 1)
  1532.     {
  1533.       if (strcmp (argv[0], "yes") == 0)
  1534.         do_timings = 1;
  1535.       else if (strcmp (argv[0], "no") == 0)
  1536.         do_timings = 0;
  1537.       else
  1538.         goto usage_error;
  1539.     }
  1540.   else
  1541.     goto usage_error;

  1542.   return;

  1543. usage_error:
  1544.   error (_("-enable-timings: Usage: %s {yes|no}"), command);
  1545. }

  1546. void
  1547. mi_cmd_list_features (char *command, char **argv, int argc)
  1548. {
  1549.   if (argc == 0)
  1550.     {
  1551.       struct cleanup *cleanup = NULL;
  1552.       struct ui_out *uiout = current_uiout;

  1553.       cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
  1554.       ui_out_field_string (uiout, NULL, "frozen-varobjs");
  1555.       ui_out_field_string (uiout, NULL, "pending-breakpoints");
  1556.       ui_out_field_string (uiout, NULL, "thread-info");
  1557.       ui_out_field_string (uiout, NULL, "data-read-memory-bytes");
  1558.       ui_out_field_string (uiout, NULL, "breakpoint-notifications");
  1559.       ui_out_field_string (uiout, NULL, "ada-task-info");
  1560.       ui_out_field_string (uiout, NULL, "language-option");
  1561.       ui_out_field_string (uiout, NULL, "info-gdb-mi-command");
  1562.       ui_out_field_string (uiout, NULL, "undefined-command-error-code");
  1563.       ui_out_field_string (uiout, NULL, "exec-run-start-option");

  1564.       if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON)))
  1565.         ui_out_field_string (uiout, NULL, "python");

  1566.       do_cleanups (cleanup);
  1567.       return;
  1568.     }

  1569.   error (_("-list-features should be passed no arguments"));
  1570. }

  1571. void
  1572. mi_cmd_list_target_features (char *command, char **argv, int argc)
  1573. {
  1574.   if (argc == 0)
  1575.     {
  1576.       struct cleanup *cleanup = NULL;
  1577.       struct ui_out *uiout = current_uiout;

  1578.       cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
  1579.       if (mi_async_p ())
  1580.         ui_out_field_string (uiout, NULL, "async");
  1581.       if (target_can_execute_reverse)
  1582.         ui_out_field_string (uiout, NULL, "reverse");
  1583.       do_cleanups (cleanup);
  1584.       return;
  1585.     }

  1586.   error (_("-list-target-features should be passed no arguments"));
  1587. }

  1588. void
  1589. mi_cmd_add_inferior (char *command, char **argv, int argc)
  1590. {
  1591.   struct inferior *inf;

  1592.   if (argc != 0)
  1593.     error (_("-add-inferior should be passed no arguments"));

  1594.   inf = add_inferior_with_spaces ();

  1595.   ui_out_field_fmt (current_uiout, "inferior", "i%d", inf->num);
  1596. }

  1597. /* Callback used to find the first inferior other than the current
  1598.    one.  */

  1599. static int
  1600. get_other_inferior (struct inferior *inf, void *arg)
  1601. {
  1602.   if (inf == current_inferior ())
  1603.     return 0;

  1604.   return 1;
  1605. }

  1606. void
  1607. mi_cmd_remove_inferior (char *command, char **argv, int argc)
  1608. {
  1609.   int id;
  1610.   struct inferior *inf;

  1611.   if (argc != 1)
  1612.     error (_("-remove-inferior should be passed a single argument"));

  1613.   if (sscanf (argv[0], "i%d", &id) != 1)
  1614.     error (_("the thread group id is syntactically invalid"));

  1615.   inf = find_inferior_id (id);
  1616.   if (!inf)
  1617.     error (_("the specified thread group does not exist"));

  1618.   if (inf->pid != 0)
  1619.     error (_("cannot remove an active inferior"));

  1620.   if (inf == current_inferior ())
  1621.     {
  1622.       struct thread_info *tp = 0;
  1623.       struct inferior *new_inferior
  1624.         = iterate_over_inferiors (get_other_inferior, NULL);

  1625.       if (new_inferior == NULL)
  1626.         error (_("Cannot remove last inferior"));

  1627.       set_current_inferior (new_inferior);
  1628.       if (new_inferior->pid != 0)
  1629.         tp = any_thread_of_process (new_inferior->pid);
  1630.       switch_to_thread (tp ? tp->ptid : null_ptid);
  1631.       set_current_program_space (new_inferior->pspace);
  1632.     }

  1633.   delete_inferior_1 (inf, 1 /* silent */);
  1634. }



  1635. /* Execute a command within a safe environment.
  1636.    Return <0 for error; >=0 for ok.

  1637.    args->action will tell mi_execute_command what action
  1638.    to perfrom after the given command has executed (display/suppress
  1639.    prompt, display error).  */

  1640. static void
  1641. captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
  1642. {
  1643.   struct cleanup *cleanup;

  1644.   if (do_timings)
  1645.     current_command_ts = context->cmd_start;

  1646.   current_token = xstrdup (context->token);
  1647.   cleanup = make_cleanup (free_current_contents, &current_token);

  1648.   running_result_record_printed = 0;
  1649.   mi_proceeded = 0;
  1650.   switch (context->op)
  1651.     {
  1652.     case MI_COMMAND:
  1653.       /* A MI command was read from the input stream.  */
  1654.       if (mi_debug_p)
  1655.         /* FIXME: gdb_???? */
  1656.         fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
  1657.                             context->token, context->command, context->args);

  1658.       mi_cmd_execute (context);

  1659.       /* Print the result if there were no errors.

  1660.          Remember that on the way out of executing a command, you have
  1661.          to directly use the mi_interp's uiout, since the command
  1662.          could have reset the interpreter, in which case the current
  1663.          uiout will most likely crash in the mi_out_* routines.  */
  1664.       if (!running_result_record_printed)
  1665.         {
  1666.           fputs_unfiltered (context->token, raw_stdout);
  1667.           /* There's no particularly good reason why target-connect results
  1668.              in not ^done.  Should kill ^connected for MI3.  */
  1669.           fputs_unfiltered (strcmp (context->command, "target-select") == 0
  1670.                             ? "^connected" : "^done", raw_stdout);
  1671.           mi_out_put (uiout, raw_stdout);
  1672.           mi_out_rewind (uiout);
  1673.           mi_print_timing_maybe ();
  1674.           fputs_unfiltered ("\n", raw_stdout);
  1675.         }
  1676.       else
  1677.         /* The command does not want anything to be printed.  In that
  1678.            case, the command probably should not have written anything
  1679.            to uiout, but in case it has written something, discard it.  */
  1680.         mi_out_rewind (uiout);
  1681.       break;

  1682.     case CLI_COMMAND:
  1683.       {
  1684.         char *argv[2];

  1685.         /* A CLI command was read from the input stream.  */
  1686.         /* This "feature" will be removed as soon as we have a
  1687.            complete set of mi commands.  */
  1688.         /* Echo the command on the console.  */
  1689.         fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
  1690.         /* Call the "console" interpreter.  */
  1691.         argv[0] = "console";
  1692.         argv[1] = context->command;
  1693.         mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);

  1694.         /* If we changed interpreters, DON'T print out anything.  */
  1695.         if (current_interp_named_p (INTERP_MI)
  1696.             || current_interp_named_p (INTERP_MI1)
  1697.             || current_interp_named_p (INTERP_MI2)
  1698.             || current_interp_named_p (INTERP_MI3))
  1699.           {
  1700.             if (!running_result_record_printed)
  1701.               {
  1702.                 fputs_unfiltered (context->token, raw_stdout);
  1703.                 fputs_unfiltered ("^done", raw_stdout);
  1704.                 mi_out_put (uiout, raw_stdout);
  1705.                 mi_out_rewind (uiout);
  1706.                 mi_print_timing_maybe ();
  1707.                 fputs_unfiltered ("\n", raw_stdout);
  1708.               }
  1709.             else
  1710.               mi_out_rewind (uiout);
  1711.           }
  1712.         break;
  1713.       }
  1714.     }

  1715.   do_cleanups (cleanup);
  1716. }

  1717. /* Print a gdb exception to the MI output stream.  */

  1718. static void
  1719. mi_print_exception (const char *token, struct gdb_exception exception)
  1720. {
  1721.   fputs_unfiltered (token, raw_stdout);
  1722.   fputs_unfiltered ("^error,msg=\"", raw_stdout);
  1723.   if (exception.message == NULL)
  1724.     fputs_unfiltered ("unknown error", raw_stdout);
  1725.   else
  1726.     fputstr_unfiltered (exception.message, '"', raw_stdout);
  1727.   fputs_unfiltered ("\"", raw_stdout);

  1728.   switch (exception.error)
  1729.     {
  1730.       case UNDEFINED_COMMAND_ERROR:
  1731.         fputs_unfiltered (",code=\"undefined-command\"", raw_stdout);
  1732.         break;
  1733.     }

  1734.   fputs_unfiltered ("\n", raw_stdout);
  1735. }

  1736. void
  1737. mi_execute_command (const char *cmd, int from_tty)
  1738. {
  1739.   char *token;
  1740.   struct mi_parse *command = NULL;
  1741.   volatile struct gdb_exception exception;

  1742.   /* This is to handle EOF (^D). We just quit gdb.  */
  1743.   /* FIXME: we should call some API function here.  */
  1744.   if (cmd == 0)
  1745.     quit_force (NULL, from_tty);

  1746.   target_log_command (cmd);

  1747.   TRY_CATCH (exception, RETURN_MASK_ALL)
  1748.     {
  1749.       command = mi_parse (cmd, &token);
  1750.     }
  1751.   if (exception.reason < 0)
  1752.     {
  1753.       mi_print_exception (token, exception);
  1754.       xfree (token);
  1755.     }
  1756.   else
  1757.     {
  1758.       volatile struct gdb_exception result;
  1759.       ptid_t previous_ptid = inferior_ptid;

  1760.       command->token = token;

  1761.       if (do_timings)
  1762.         {
  1763.           command->cmd_start = (struct mi_timestamp *)
  1764.             xmalloc (sizeof (struct mi_timestamp));
  1765.           timestamp (command->cmd_start);
  1766.         }

  1767.       TRY_CATCH (result, RETURN_MASK_ALL)
  1768.         {
  1769.           captured_mi_execute_command (current_uiout, command);
  1770.         }
  1771.       if (result.reason < 0)
  1772.         {
  1773.           /* The command execution failed and error() was called
  1774.              somewhere.  */
  1775.           mi_print_exception (command->token, result);
  1776.           mi_out_rewind (current_uiout);
  1777.         }

  1778.       bpstat_do_actions ();

  1779.       if (/* The notifications are only output when the top-level
  1780.              interpreter (specified on the command line) is MI.  */
  1781.           ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
  1782.           /* Don't try report anything if there are no threads --
  1783.              the program is dead.  */
  1784.           && thread_count () != 0
  1785.           /* -thread-select explicitly changes thread. If frontend uses that
  1786.              internally, we don't want to emit =thread-selected, since
  1787.              =thread-selected is supposed to indicate user's intentions.  */
  1788.           && strcmp (command->command, "thread-select") != 0)
  1789.         {
  1790.           struct mi_interp *mi = top_level_interpreter_data ();
  1791.           int report_change = 0;

  1792.           if (command->thread == -1)
  1793.             {
  1794.               report_change = (!ptid_equal (previous_ptid, null_ptid)
  1795.                                && !ptid_equal (inferior_ptid, previous_ptid)
  1796.                                && !ptid_equal (inferior_ptid, null_ptid));
  1797.             }
  1798.           else if (!ptid_equal (inferior_ptid, null_ptid))
  1799.             {
  1800.               struct thread_info *ti = inferior_thread ();

  1801.               report_change = (ti->num != command->thread);
  1802.             }

  1803.           if (report_change)
  1804.             {
  1805.               struct thread_info *ti = inferior_thread ();

  1806.               target_terminal_ours ();
  1807.               fprintf_unfiltered (mi->event_channel,
  1808.                                   "thread-selected,id=\"%d\"",
  1809.                                   ti->num);
  1810.               gdb_flush (mi->event_channel);
  1811.             }
  1812.         }

  1813.       mi_parse_free (command);
  1814.     }
  1815. }

  1816. static void
  1817. mi_cmd_execute (struct mi_parse *parse)
  1818. {
  1819.   struct cleanup *cleanup;
  1820.   enum language saved_language;

  1821.   cleanup = prepare_execute_command ();

  1822.   if (parse->all && parse->thread_group != -1)
  1823.     error (_("Cannot specify --thread-group together with --all"));

  1824.   if (parse->all && parse->thread != -1)
  1825.     error (_("Cannot specify --thread together with --all"));

  1826.   if (parse->thread_group != -1 && parse->thread != -1)
  1827.     error (_("Cannot specify --thread together with --thread-group"));

  1828.   if (parse->frame != -1 && parse->thread == -1)
  1829.     error (_("Cannot specify --frame without --thread"));

  1830.   if (parse->thread_group != -1)
  1831.     {
  1832.       struct inferior *inf = find_inferior_id (parse->thread_group);
  1833.       struct thread_info *tp = 0;

  1834.       if (!inf)
  1835.         error (_("Invalid thread group for the --thread-group option"));

  1836.       set_current_inferior (inf);
  1837.       /* This behaviour means that if --thread-group option identifies
  1838.          an inferior with multiple threads, then a random one will be
  1839.          picked.  This is not a problem -- frontend should always
  1840.          provide --thread if it wishes to operate on a specific
  1841.          thread.  */
  1842.       if (inf->pid != 0)
  1843.         tp = any_live_thread_of_process (inf->pid);
  1844.       switch_to_thread (tp ? tp->ptid : null_ptid);
  1845.       set_current_program_space (inf->pspace);
  1846.     }

  1847.   if (parse->thread != -1)
  1848.     {
  1849.       struct thread_info *tp = find_thread_id (parse->thread);

  1850.       if (!tp)
  1851.         error (_("Invalid thread id: %d"), parse->thread);

  1852.       if (is_exited (tp->ptid))
  1853.         error (_("Thread id: %d has terminated"), parse->thread);

  1854.       switch_to_thread (tp->ptid);
  1855.     }

  1856.   if (parse->frame != -1)
  1857.     {
  1858.       struct frame_info *fid;
  1859.       int frame = parse->frame;

  1860.       fid = find_relative_frame (get_current_frame (), &frame);
  1861.       if (frame == 0)
  1862.         /* find_relative_frame was successful */
  1863.         select_frame (fid);
  1864.       else
  1865.         error (_("Invalid frame id: %d"), frame);
  1866.     }

  1867.   if (parse->language != language_unknown)
  1868.     {
  1869.       make_cleanup_restore_current_language ();
  1870.       set_language (parse->language);
  1871.     }

  1872.   current_context = parse;

  1873.   if (parse->cmd->suppress_notification != NULL)
  1874.     {
  1875.       make_cleanup_restore_integer (parse->cmd->suppress_notification);
  1876.       *parse->cmd->suppress_notification = 1;
  1877.     }

  1878.   if (parse->cmd->argv_func != NULL)
  1879.     {
  1880.       parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
  1881.     }
  1882.   else if (parse->cmd->cli.cmd != 0)
  1883.     {
  1884.       /* FIXME: DELETE THIS. */
  1885.       /* The operation is still implemented by a cli command.  */
  1886.       /* Must be a synchronous one.  */
  1887.       mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
  1888.                               parse->args);
  1889.     }
  1890.   else
  1891.     {
  1892.       /* FIXME: DELETE THIS.  */
  1893.       struct ui_file *stb;

  1894.       stb = mem_fileopen ();

  1895.       fputs_unfiltered ("Undefined mi command: ", stb);
  1896.       fputstr_unfiltered (parse->command, '"', stb);
  1897.       fputs_unfiltered (" (missing implementation)", stb);

  1898.       make_cleanup_ui_file_delete (stb);
  1899.       error_stream (stb);
  1900.     }
  1901.   do_cleanups (cleanup);
  1902. }

  1903. /* FIXME: This is just a hack so we can get some extra commands going.
  1904.    We don't want to channel things through the CLI, but call libgdb directly.
  1905.    Use only for synchronous commands.  */

  1906. void
  1907. mi_execute_cli_command (const char *cmd, int args_p, const char *args)
  1908. {
  1909.   if (cmd != 0)
  1910.     {
  1911.       struct cleanup *old_cleanups;
  1912.       char *run;

  1913.       if (args_p)
  1914.         run = xstrprintf ("%s %s", cmd, args);
  1915.       else
  1916.         run = xstrdup (cmd);
  1917.       if (mi_debug_p)
  1918.         /* FIXME: gdb_???? */
  1919.         fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
  1920.                             cmd, run);
  1921.       old_cleanups = make_cleanup (xfree, run);
  1922.       execute_command (run, 0 /* from_tty */ );
  1923.       do_cleanups (old_cleanups);
  1924.       return;
  1925.     }
  1926. }

  1927. void
  1928. mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
  1929. {
  1930.   struct cleanup *old_cleanups;
  1931.   char *run;

  1932.   if (mi_async_p ())
  1933.     run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
  1934.   else
  1935.     run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
  1936.   old_cleanups = make_cleanup (xfree, run);

  1937.   execute_command (run, 0 /* from_tty */ );

  1938.   /* Do this before doing any printing.  It would appear that some
  1939.      print code leaves garbage around in the buffer.  */
  1940.   do_cleanups (old_cleanups);
  1941. }

  1942. void
  1943. mi_load_progress (const char *section_name,
  1944.                   unsigned long sent_so_far,
  1945.                   unsigned long total_section,
  1946.                   unsigned long total_sent,
  1947.                   unsigned long grand_total)
  1948. {
  1949.   struct timeval time_now, delta, update_threshold;
  1950.   static struct timeval last_update;
  1951.   static char *previous_sect_name = NULL;
  1952.   int new_section;
  1953.   struct ui_out *saved_uiout;
  1954.   struct ui_out *uiout;

  1955.   /* This function is called through deprecated_show_load_progress
  1956.      which means uiout may not be correct.  Fix it for the duration
  1957.      of this function.  */
  1958.   saved_uiout = current_uiout;

  1959.   if (current_interp_named_p (INTERP_MI)
  1960.       || current_interp_named_p (INTERP_MI2))
  1961.     current_uiout = mi_out_new (2);
  1962.   else if (current_interp_named_p (INTERP_MI1))
  1963.     current_uiout = mi_out_new (1);
  1964.   else if (current_interp_named_p (INTERP_MI3))
  1965.     current_uiout = mi_out_new (3);
  1966.   else
  1967.     return;

  1968.   uiout = current_uiout;

  1969.   update_threshold.tv_sec = 0;
  1970.   update_threshold.tv_usec = 500000;
  1971.   gettimeofday (&time_now, NULL);

  1972.   delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
  1973.   delta.tv_sec = time_now.tv_sec - last_update.tv_sec;

  1974.   if (delta.tv_usec < 0)
  1975.     {
  1976.       delta.tv_sec -= 1;
  1977.       delta.tv_usec += 1000000L;
  1978.     }

  1979.   new_section = (previous_sect_name ?
  1980.                  strcmp (previous_sect_name, section_name) : 1);
  1981.   if (new_section)
  1982.     {
  1983.       struct cleanup *cleanup_tuple;

  1984.       xfree (previous_sect_name);
  1985.       previous_sect_name = xstrdup (section_name);

  1986.       if (current_token)
  1987.         fputs_unfiltered (current_token, raw_stdout);
  1988.       fputs_unfiltered ("+download", raw_stdout);
  1989.       cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  1990.       ui_out_field_string (uiout, "section", section_name);
  1991.       ui_out_field_int (uiout, "section-size", total_section);
  1992.       ui_out_field_int (uiout, "total-size", grand_total);
  1993.       do_cleanups (cleanup_tuple);
  1994.       mi_out_put (uiout, raw_stdout);
  1995.       fputs_unfiltered ("\n", raw_stdout);
  1996.       gdb_flush (raw_stdout);
  1997.     }

  1998.   if (delta.tv_sec >= update_threshold.tv_sec &&
  1999.       delta.tv_usec >= update_threshold.tv_usec)
  2000.     {
  2001.       struct cleanup *cleanup_tuple;

  2002.       last_update.tv_sec = time_now.tv_sec;
  2003.       last_update.tv_usec = time_now.tv_usec;
  2004.       if (current_token)
  2005.         fputs_unfiltered (current_token, raw_stdout);
  2006.       fputs_unfiltered ("+download", raw_stdout);
  2007.       cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  2008.       ui_out_field_string (uiout, "section", section_name);
  2009.       ui_out_field_int (uiout, "section-sent", sent_so_far);
  2010.       ui_out_field_int (uiout, "section-size", total_section);
  2011.       ui_out_field_int (uiout, "total-sent", total_sent);
  2012.       ui_out_field_int (uiout, "total-size", grand_total);
  2013.       do_cleanups (cleanup_tuple);
  2014.       mi_out_put (uiout, raw_stdout);
  2015.       fputs_unfiltered ("\n", raw_stdout);
  2016.       gdb_flush (raw_stdout);
  2017.     }

  2018.   xfree (uiout);
  2019.   current_uiout = saved_uiout;
  2020. }

  2021. static void
  2022. timestamp (struct mi_timestamp *tv)
  2023. {
  2024.   gettimeofday (&tv->wallclock, NULL);
  2025. #ifdef HAVE_GETRUSAGE
  2026.   getrusage (RUSAGE_SELF, &rusage);
  2027.   tv->utime.tv_sec = rusage.ru_utime.tv_sec;
  2028.   tv->utime.tv_usec = rusage.ru_utime.tv_usec;
  2029.   tv->stime.tv_sec = rusage.ru_stime.tv_sec;
  2030.   tv->stime.tv_usec = rusage.ru_stime.tv_usec;
  2031. #else
  2032.   {
  2033.     long usec = get_run_time ();

  2034.     tv->utime.tv_sec = usec/1000000L;
  2035.     tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
  2036.     tv->stime.tv_sec = 0;
  2037.     tv->stime.tv_usec = 0;
  2038.   }
  2039. #endif
  2040. }

  2041. static void
  2042. print_diff_now (struct mi_timestamp *start)
  2043. {
  2044.   struct mi_timestamp now;

  2045.   timestamp (&now);
  2046.   print_diff (start, &now);
  2047. }

  2048. void
  2049. mi_print_timing_maybe (void)
  2050. {
  2051.   /* If the command is -enable-timing then do_timings may be true
  2052.      whilst current_command_ts is not initialized.  */
  2053.   if (do_timings && current_command_ts)
  2054.     print_diff_now (current_command_ts);
  2055. }

  2056. static long
  2057. timeval_diff (struct timeval start, struct timeval end)
  2058. {
  2059.   return ((end.tv_sec - start.tv_sec) * 1000000L)
  2060.     + (end.tv_usec - start.tv_usec);
  2061. }

  2062. static void
  2063. print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
  2064. {
  2065.   fprintf_unfiltered
  2066.     (raw_stdout,
  2067.      ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
  2068.      timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
  2069.      timeval_diff (start->utime, end->utime) / 1000000.0,
  2070.      timeval_diff (start->stime, end->stime) / 1000000.0);
  2071. }

  2072. void
  2073. mi_cmd_trace_define_variable (char *command, char **argv, int argc)
  2074. {
  2075.   struct expression *expr;
  2076.   LONGEST initval = 0;
  2077.   struct trace_state_variable *tsv;
  2078.   char *name = 0;

  2079.   if (argc != 1 && argc != 2)
  2080.     error (_("Usage: -trace-define-variable VARIABLE [VALUE]"));

  2081.   name = argv[0];
  2082.   if (*name++ != '$')
  2083.     error (_("Name of trace variable should start with '$'"));

  2084.   validate_trace_state_variable_name (name);

  2085.   tsv = find_trace_state_variable (name);
  2086.   if (!tsv)
  2087.     tsv = create_trace_state_variable (name);

  2088.   if (argc == 2)
  2089.     initval = value_as_long (parse_and_eval (argv[1]));

  2090.   tsv->initial_value = initval;
  2091. }

  2092. void
  2093. mi_cmd_trace_list_variables (char *command, char **argv, int argc)
  2094. {
  2095.   if (argc != 0)
  2096.     error (_("-trace-list-variables: no arguments allowed"));

  2097.   tvariables_info_1 ();
  2098. }

  2099. void
  2100. mi_cmd_trace_find (char *command, char **argv, int argc)
  2101. {
  2102.   char *mode;

  2103.   if (argc == 0)
  2104.     error (_("trace selection mode is required"));

  2105.   mode = argv[0];

  2106.   if (strcmp (mode, "none") == 0)
  2107.     {
  2108.       tfind_1 (tfind_number, -1, 0, 0, 0);
  2109.       return;
  2110.     }

  2111.   check_trace_running (current_trace_status ());

  2112.   if (strcmp (mode, "frame-number") == 0)
  2113.     {
  2114.       if (argc != 2)
  2115.         error (_("frame number is required"));
  2116.       tfind_1 (tfind_number, atoi (argv[1]), 0, 0, 0);
  2117.     }
  2118.   else if (strcmp (mode, "tracepoint-number") == 0)
  2119.     {
  2120.       if (argc != 2)
  2121.         error (_("tracepoint number is required"));
  2122.       tfind_1 (tfind_tp, atoi (argv[1]), 0, 0, 0);
  2123.     }
  2124.   else if (strcmp (mode, "pc") == 0)
  2125.     {
  2126.       if (argc != 2)
  2127.         error (_("PC is required"));
  2128.       tfind_1 (tfind_pc, 0, parse_and_eval_address (argv[1]), 0, 0);
  2129.     }
  2130.   else if (strcmp (mode, "pc-inside-range") == 0)
  2131.     {
  2132.       if (argc != 3)
  2133.         error (_("Start and end PC are required"));
  2134.       tfind_1 (tfind_range, 0, parse_and_eval_address (argv[1]),
  2135.                parse_and_eval_address (argv[2]), 0);
  2136.     }
  2137.   else if (strcmp (mode, "pc-outside-range") == 0)
  2138.     {
  2139.       if (argc != 3)
  2140.         error (_("Start and end PC are required"));
  2141.       tfind_1 (tfind_outside, 0, parse_and_eval_address (argv[1]),
  2142.                parse_and_eval_address (argv[2]), 0);
  2143.     }
  2144.   else if (strcmp (mode, "line") == 0)
  2145.     {
  2146.       struct symtabs_and_lines sals;
  2147.       struct symtab_and_line sal;
  2148.       static CORE_ADDR start_pc, end_pc;
  2149.       struct cleanup *back_to;

  2150.       if (argc != 2)
  2151.         error (_("Line is required"));

  2152.       sals = decode_line_with_current_source (argv[1],
  2153.                                               DECODE_LINE_FUNFIRSTLINE);
  2154.       back_to = make_cleanup (xfree, sals.sals);

  2155.       sal = sals.sals[0];

  2156.       if (sal.symtab == 0)
  2157.         error (_("Could not find the specified line"));

  2158.       if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
  2159.         tfind_1 (tfind_range, 0, start_pc, end_pc - 1, 0);
  2160.       else
  2161.         error (_("Could not find the specified line"));

  2162.       do_cleanups (back_to);
  2163.     }
  2164.   else
  2165.     error (_("Invalid mode '%s'"), mode);

  2166.   if (has_stack_frames () || get_traceframe_number () >= 0)
  2167.     print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 1);
  2168. }

  2169. void
  2170. mi_cmd_trace_save (char *command, char **argv, int argc)
  2171. {
  2172.   int target_saves = 0;
  2173.   int generate_ctf = 0;
  2174.   char *filename;
  2175.   int oind = 0;
  2176.   char *oarg;

  2177.   enum opt
  2178.   {
  2179.     TARGET_SAVE_OPT, CTF_OPT
  2180.   };
  2181.   static const struct mi_opt opts[] =
  2182.     {
  2183.       {"r", TARGET_SAVE_OPT, 0},
  2184.       {"ctf", CTF_OPT, 0},
  2185.       { 0, 0, 0 }
  2186.     };

  2187.   while (1)
  2188.     {
  2189.       int opt = mi_getopt ("-trace-save", argc, argv, opts,
  2190.                            &oind, &oarg);

  2191.       if (opt < 0)
  2192.         break;
  2193.       switch ((enum opt) opt)
  2194.         {
  2195.         case TARGET_SAVE_OPT:
  2196.           target_saves = 1;
  2197.           break;
  2198.         case CTF_OPT:
  2199.           generate_ctf = 1;
  2200.           break;
  2201.         }
  2202.     }
  2203.   filename = argv[oind];

  2204.   if (generate_ctf)
  2205.     trace_save_ctf (filename, target_saves);
  2206.   else
  2207.     trace_save_tfile (filename, target_saves);
  2208. }

  2209. void
  2210. mi_cmd_trace_start (char *command, char **argv, int argc)
  2211. {
  2212.   start_tracing (NULL);
  2213. }

  2214. void
  2215. mi_cmd_trace_status (char *command, char **argv, int argc)
  2216. {
  2217.   trace_status_mi (0);
  2218. }

  2219. void
  2220. mi_cmd_trace_stop (char *command, char **argv, int argc)
  2221. {
  2222.   stop_tracing (NULL);
  2223.   trace_status_mi (1);
  2224. }

  2225. /* Implement the "-ada-task-info" command.  */

  2226. void
  2227. mi_cmd_ada_task_info (char *command, char **argv, int argc)
  2228. {
  2229.   if (argc != 0 && argc != 1)
  2230.     error (_("Invalid MI command"));

  2231.   print_ada_task_info (current_uiout, argv[0], current_inferior ());
  2232. }

  2233. /* Print EXPRESSION according to VALUES.  */

  2234. static void
  2235. print_variable_or_computed (char *expression, enum print_values values)
  2236. {
  2237.   struct expression *expr;
  2238.   struct cleanup *old_chain;
  2239.   struct value *val;
  2240.   struct ui_file *stb;
  2241.   struct value_print_options opts;
  2242.   struct type *type;
  2243.   struct ui_out *uiout = current_uiout;

  2244.   stb = mem_fileopen ();
  2245.   old_chain = make_cleanup_ui_file_delete (stb);

  2246.   expr = parse_expression (expression);

  2247.   make_cleanup (free_current_contents, &expr);

  2248.   if (values == PRINT_SIMPLE_VALUES)
  2249.     val = evaluate_type (expr);
  2250.   else
  2251.     val = evaluate_expression (expr);

  2252.   if (values != PRINT_NO_VALUES)
  2253.     make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  2254.   ui_out_field_string (uiout, "name", expression);

  2255.   switch (values)
  2256.     {
  2257.     case PRINT_SIMPLE_VALUES:
  2258.       type = check_typedef (value_type (val));
  2259.       type_print (value_type (val), "", stb, -1);
  2260.       ui_out_field_stream (uiout, "type", stb);
  2261.       if (TYPE_CODE (type) != TYPE_CODE_ARRAY
  2262.           && TYPE_CODE (type) != TYPE_CODE_STRUCT
  2263.           && TYPE_CODE (type) != TYPE_CODE_UNION)
  2264.         {
  2265.           struct value_print_options opts;

  2266.           get_no_prettyformat_print_options (&opts);
  2267.           opts.deref_ref = 1;
  2268.           common_val_print (val, stb, 0, &opts, current_language);
  2269.           ui_out_field_stream (uiout, "value", stb);
  2270.         }
  2271.       break;
  2272.     case PRINT_ALL_VALUES:
  2273.       {
  2274.         struct value_print_options opts;

  2275.         get_no_prettyformat_print_options (&opts);
  2276.         opts.deref_ref = 1;
  2277.         common_val_print (val, stb, 0, &opts, current_language);
  2278.         ui_out_field_stream (uiout, "value", stb);
  2279.       }
  2280.       break;
  2281.     }

  2282.   do_cleanups (old_chain);
  2283. }

  2284. /* Implement the "-trace-frame-collected" command.  */

  2285. void
  2286. mi_cmd_trace_frame_collected (char *command, char **argv, int argc)
  2287. {
  2288.   struct cleanup *old_chain;
  2289.   struct bp_location *tloc;
  2290.   int stepping_frame;
  2291.   struct collection_list *clist;
  2292.   struct collection_list tracepoint_list, stepping_list;
  2293.   struct traceframe_info *tinfo;
  2294.   int oind = 0;
  2295.   int var_print_values = PRINT_ALL_VALUES;
  2296.   int comp_print_values = PRINT_ALL_VALUES;
  2297.   int registers_format = 'x';
  2298.   int memory_contents = 0;
  2299.   struct ui_out *uiout = current_uiout;
  2300.   enum opt
  2301.   {
  2302.     VAR_PRINT_VALUES,
  2303.     COMP_PRINT_VALUES,
  2304.     REGISTERS_FORMAT,
  2305.     MEMORY_CONTENTS,
  2306.   };
  2307.   static const struct mi_opt opts[] =
  2308.     {
  2309.       {"-var-print-values", VAR_PRINT_VALUES, 1},
  2310.       {"-comp-print-values", COMP_PRINT_VALUES, 1},
  2311.       {"-registers-format", REGISTERS_FORMAT, 1},
  2312.       {"-memory-contents", MEMORY_CONTENTS, 0},
  2313.       { 0, 0, 0 }
  2314.     };

  2315.   while (1)
  2316.     {
  2317.       char *oarg;
  2318.       int opt = mi_getopt ("-trace-frame-collected", argc, argv, opts,
  2319.                            &oind, &oarg);
  2320.       if (opt < 0)
  2321.         break;
  2322.       switch ((enum opt) opt)
  2323.         {
  2324.         case VAR_PRINT_VALUES:
  2325.           var_print_values = mi_parse_print_values (oarg);
  2326.           break;
  2327.         case COMP_PRINT_VALUES:
  2328.           comp_print_values = mi_parse_print_values (oarg);
  2329.           break;
  2330.         case REGISTERS_FORMAT:
  2331.           registers_format = oarg[0];
  2332.         case MEMORY_CONTENTS:
  2333.           memory_contents = 1;
  2334.           break;
  2335.         }
  2336.     }

  2337.   if (oind != argc)
  2338.     error (_("Usage: -trace-frame-collected "
  2339.              "[--var-print-values PRINT_VALUES] "
  2340.              "[--comp-print-values PRINT_VALUES] "
  2341.              "[--registers-format FORMAT]"
  2342.              "[--memory-contents]"));

  2343.   /* This throws an error is not inspecting a trace frame.  */
  2344.   tloc = get_traceframe_location (&stepping_frame);

  2345.   /* This command only makes sense for the current frame, not the
  2346.      selected frame.  */
  2347.   old_chain = make_cleanup_restore_current_thread ();
  2348.   select_frame (get_current_frame ());

  2349.   encode_actions_and_make_cleanup (tloc, &tracepoint_list,
  2350.                                    &stepping_list);

  2351.   if (stepping_frame)
  2352.     clist = &stepping_list;
  2353.   else
  2354.     clist = &tracepoint_list;

  2355.   tinfo = get_traceframe_info ();

  2356.   /* Explicitly wholly collected variables.  */
  2357.   {
  2358.     struct cleanup *list_cleanup;
  2359.     char *p;
  2360.     int i;

  2361.     list_cleanup = make_cleanup_ui_out_list_begin_end (uiout,
  2362.                                                        "explicit-variables");
  2363.     for (i = 0; VEC_iterate (char_ptr, clist->wholly_collected, i, p); i++)
  2364.       print_variable_or_computed (p, var_print_values);
  2365.     do_cleanups (list_cleanup);
  2366.   }

  2367.   /* Computed expressions.  */
  2368.   {
  2369.     struct cleanup *list_cleanup;
  2370.     char *p;
  2371.     int i;

  2372.     list_cleanup
  2373.       = make_cleanup_ui_out_list_begin_end (uiout,
  2374.                                             "computed-expressions");
  2375.     for (i = 0; VEC_iterate (char_ptr, clist->computed, i, p); i++)
  2376.       print_variable_or_computed (p, comp_print_values);
  2377.     do_cleanups (list_cleanup);
  2378.   }

  2379.   /* Registers.  Given pseudo-registers, and that some architectures
  2380.      (like MIPS) actually hide the raw registers, we don't go through
  2381.      the trace frame info, but instead consult the register cache for
  2382.      register availability.  */
  2383.   {
  2384.     struct cleanup *list_cleanup;
  2385.     struct frame_info *frame;
  2386.     struct gdbarch *gdbarch;
  2387.     int regnum;
  2388.     int numregs;

  2389.     list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "registers");

  2390.     frame = get_selected_frame (NULL);
  2391.     gdbarch = get_frame_arch (frame);
  2392.     numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  2393.     for (regnum = 0; regnum < numregs; regnum++)
  2394.       {
  2395.         if (gdbarch_register_name (gdbarch, regnum) == NULL
  2396.             || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  2397.           continue;

  2398.         output_register (frame, regnum, registers_format, 1);
  2399.       }

  2400.     do_cleanups (list_cleanup);
  2401.   }

  2402.   /* Trace state variables.  */
  2403.   {
  2404.     struct cleanup *list_cleanup;
  2405.     int tvar;
  2406.     char *tsvname;
  2407.     int i;

  2408.     list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "tvars");

  2409.     tsvname = NULL;
  2410.     make_cleanup (free_current_contents, &tsvname);

  2411.     for (i = 0; VEC_iterate (int, tinfo->tvars, i, tvar); i++)
  2412.       {
  2413.         struct cleanup *cleanup_child;
  2414.         struct trace_state_variable *tsv;

  2415.         tsv = find_trace_state_variable_by_number (tvar);

  2416.         cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  2417.         if (tsv != NULL)
  2418.           {
  2419.             tsvname = xrealloc (tsvname, strlen (tsv->name) + 2);
  2420.             tsvname[0] = '$';
  2421.             strcpy (tsvname + 1, tsv->name);
  2422.             ui_out_field_string (uiout, "name", tsvname);

  2423.             tsv->value_known = target_get_trace_state_variable_value (tsv->number,
  2424.                                                                       &tsv->value);
  2425.             ui_out_field_int (uiout, "current", tsv->value);
  2426.           }
  2427.         else
  2428.           {
  2429.             ui_out_field_skip (uiout, "name");
  2430.             ui_out_field_skip (uiout, "current");
  2431.           }

  2432.         do_cleanups (cleanup_child);
  2433.       }

  2434.     do_cleanups (list_cleanup);
  2435.   }

  2436.   /* Memory.  */
  2437.   {
  2438.     struct cleanup *list_cleanup;
  2439.     VEC(mem_range_s) *available_memory = NULL;
  2440.     struct mem_range *r;
  2441.     int i;

  2442.     traceframe_available_memory (&available_memory, 0, ULONGEST_MAX);
  2443.     make_cleanup (VEC_cleanup(mem_range_s), &available_memory);

  2444.     list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "memory");

  2445.     for (i = 0; VEC_iterate (mem_range_s, available_memory, i, r); i++)
  2446.       {
  2447.         struct cleanup *cleanup_child;
  2448.         gdb_byte *data;
  2449.         struct gdbarch *gdbarch = target_gdbarch ();

  2450.         cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  2451.         ui_out_field_core_addr (uiout, "address", gdbarch, r->start);
  2452.         ui_out_field_int (uiout, "length", r->length);

  2453.         data = xmalloc (r->length);
  2454.         make_cleanup (xfree, data);

  2455.         if (memory_contents)
  2456.           {
  2457.             if (target_read_memory (r->start, data, r->length) == 0)
  2458.               {
  2459.                 int m;
  2460.                 char *data_str, *p;

  2461.                 data_str = xmalloc (r->length * 2 + 1);
  2462.                 make_cleanup (xfree, data_str);

  2463.                 for (m = 0, p = data_str; m < r->length; ++m, p += 2)
  2464.                   sprintf (p, "%02x", data[m]);
  2465.                 ui_out_field_string (uiout, "contents", data_str);
  2466.               }
  2467.             else
  2468.               ui_out_field_skip (uiout, "contents");
  2469.           }
  2470.         do_cleanups (cleanup_child);
  2471.       }

  2472.     do_cleanups (list_cleanup);
  2473.   }

  2474.   do_cleanups (old_chain);
  2475. }

  2476. void
  2477. _initialize_mi_main (void)
  2478. {
  2479.   struct cmd_list_element *c;

  2480.   add_setshow_boolean_cmd ("mi-async", class_run,
  2481.                            &mi_async_1, _("\
  2482. Set whether MI asynchronous mode is enabled."), _("\
  2483. Show whether MI asynchronous mode is enabled."), _("\
  2484. Tells GDB whether MI should be in asynchronous mode."),
  2485.                            set_mi_async_command,
  2486.                            show_mi_async_command,
  2487.                            &setlist,
  2488.                            &showlist);

  2489.   /* Alias old "target-async" to "mi-async".  */
  2490.   c = add_alias_cmd ("target-async", "mi-async", class_run, 0, &setlist);
  2491.   deprecate_cmd (c, "set mi-async");
  2492.   c = add_alias_cmd ("target-async", "mi-async", class_run, 0, &showlist);
  2493.   deprecate_cmd (c, "show mi-async");
  2494. }