mi_load_progress: Use unique_ptr to manage ui_out lifetime
[deliverable/binutils-gdb.git] / gdb / mi / mi-main.c
index 9a876574201a73de048edd8f2752e5389291992d..0359dc123fb138a390d4066dd708c727eacc2fc7 100644 (file)
 #include "extension.h"
 #include "gdbcmd.h"
 #include "observer.h"
+#include "common/gdb_optional.h"
+#include "common/byte-vector.h"
 
 #include <ctype.h>
 #include "run-time-clock.h"
 #include <chrono>
+#include "progspace-and-thread.h"
+#include "common/rsp-low.h"
 
 enum
   {
@@ -84,12 +88,11 @@ int running_result_record_printed = 1;
    command was issued.  */
 int mi_proceeded;
 
-extern void _initialize_mi_main (void);
 static void mi_cmd_execute (struct mi_parse *parse);
 
 static void mi_execute_cli_command (const char *cmd, int args_p,
                                    const char *args);
-static void mi_execute_async_cli_command (char *cli_command,
+static void mi_execute_async_cli_command (const char *cli_command,
                                          char **argv, int argc);
 static int register_changed_p (int regnum, struct regcache *,
                               struct regcache *);
@@ -273,8 +276,8 @@ exec_continue (char **argv, int argc)
         See comment on infcmd.c:proceed_thread_callback for rationale.  */
       if (current_context->all || current_context->thread_group != -1)
        {
+         scoped_restore_current_thread restore_thread;
          int pid = 0;
-         struct cleanup *back_to = make_cleanup_restore_current_thread ();
 
          if (!current_context->all)
            {
@@ -284,7 +287,6 @@ exec_continue (char **argv, int argc)
              pid = inf->pid;
            }
          iterate_over_threads (proceed_thread_callback, &pid);
-         do_cleanups (back_to);
        }
       else
        {
@@ -311,17 +313,10 @@ exec_continue (char **argv, int argc)
     }
 }
 
-static void
-exec_direction_forward (void *notused)
-{
-  execution_direction = EXEC_FORWARD;
-}
-
 static void
 exec_reverse_continue (char **argv, int argc)
 {
   enum exec_direction_kind dir = execution_direction;
-  struct cleanup *old_chain;
 
   if (dir == EXEC_REVERSE)
     error (_("Already in reverse mode."));
@@ -329,10 +324,9 @@ exec_reverse_continue (char **argv, int argc)
   if (!target_can_execute_reverse)
     error (_("Target %s does not support this command."), target_shortname);
 
-  old_chain = make_cleanup (exec_direction_forward, NULL);
-  execution_direction = EXEC_REVERSE;
+  scoped_restore save_exec_dir = make_scoped_restore (&execution_direction,
+                                                     EXEC_REVERSE);
   exec_continue (argv, argc);
-  do_cleanups (old_chain);
 }
 
 void
@@ -475,10 +469,9 @@ mi_cmd_exec_run (const char *command, char **argv, int argc)
 
   if (current_context->all)
     {
-      struct cleanup *back_to = save_current_space_and_thread ();
+      scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
       iterate_over_inferiors (run_one_inferior, &start_p);
-      do_cleanups (back_to);
     }
   else
     {
@@ -667,8 +660,7 @@ print_one_inferior (struct inferior *inferior, void *xdata)
                  compare_positive_ints))
     {
       struct collect_cores_data data;
-      struct cleanup *back_to
-       = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+      ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
       uiout->field_fmt ("id", "i%d", inferior->num);
       uiout->field_string ("type", "process");
@@ -694,8 +686,7 @@ print_one_inferior (struct inferior *inferior, void *xdata)
       if (!VEC_empty (int, data.cores))
        {
          int *b, *e;
-         struct cleanup *back_to_2 =
-           make_cleanup_ui_out_list_begin_end (uiout, "cores");
+         ui_out_emit_list list_emitter (uiout, "cores");
 
          qsort (VEC_address (int, data.cores),
                 VEC_length (int, data.cores), sizeof (int),
@@ -707,14 +698,10 @@ print_one_inferior (struct inferior *inferior, void *xdata)
 
          for (; b != e; ++b)
            uiout->field_int (NULL, *b);
-
-         do_cleanups (back_to_2);
        }
 
       if (top_data->recurse)
        print_thread_info (uiout, NULL, inferior->pid);
-
-      do_cleanups (back_to);
     }
 
   return 0;
@@ -727,17 +714,12 @@ print_one_inferior (struct inferior *inferior, void *xdata)
 static void
 output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
 {
-  struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
-                                                               field_name);
-  char *cores = xstrdup (xcores);
-  char *p = cores;
-
-  make_cleanup (xfree, cores);
+  ui_out_emit_list list_emitter (uiout, field_name);
+  gdb::unique_xmalloc_ptr<char> cores (xstrdup (xcores));
+  char *p = cores.get ();
 
   for (p = strtok (p, ","); p;  p = strtok (NULL, ","))
     uiout->field_string (NULL, p);
-
-  do_cleanups (back_to);
 }
 
 static void
@@ -831,15 +813,13 @@ list_available_thread_groups (VEC (int) *ids, int recurse)
        }
     }
 
-  make_cleanup_ui_out_list_begin_end (uiout, "groups");
+  ui_out_emit_list list_emitter (uiout, "groups");
 
   for (ix_items = 0;
        VEC_iterate (osdata_item_s, data->items,
                    ix_items, item);
        ix_items++)
     {
-      struct cleanup *back_to;
-
       const char *pid = get_osdata_column (item, "pid");
       const char *cmd = get_osdata_column (item, "command");
       const char *user = get_osdata_column (item, "user");
@@ -856,7 +836,7 @@ list_available_thread_groups (VEC (int) *ids, int recurse)
        continue;
 
 
-      back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+      ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
       uiout->field_fmt ("id", "%s", pid);
       uiout->field_string ("type", "process");
@@ -876,27 +856,22 @@ list_available_thread_groups (VEC (int) *ids, int recurse)
              struct osdata_item *child;
              int ix_child;
 
-             make_cleanup_ui_out_list_begin_end (uiout, "threads");
+             ui_out_emit_list thread_list_emitter (uiout, "threads");
 
              for (ix_child = 0;
                   VEC_iterate (osdata_item_s, children, ix_child, child);
                   ++ix_child)
                {
-                 struct cleanup *back_to_2 =
-                   make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+                 ui_out_emit_tuple tuple_emitter (uiout, NULL);
                  const char *tid = get_osdata_column (child, "tid");
                  const char *tcore = get_osdata_column (child, "core");
 
                  uiout->field_string ("id", tid);
                  if (tcore)
                    uiout->field_string ("core", tcore);
-
-                 do_cleanups (back_to_2);
                }
            }
        }
-
-      do_cleanups (back_to);
     }
 
   do_cleanups (cleanup);
@@ -996,7 +971,7 @@ mi_cmd_list_thread_groups (const char *command, char **argv, int argc)
         print everything, or several explicit ids.  In both cases,
         we print more than one group, and have to use 'groups'
         as the top-level element.  */
-      make_cleanup_ui_out_list_begin_end (uiout, "groups");
+      ui_out_emit_list list_emitter (uiout, "groups");
       update_thread_list ();
       iterate_over_inferiors (print_one_inferior, &data);
     }
@@ -1011,7 +986,6 @@ mi_cmd_data_list_register_names (const char *command, char **argv, int argc)
   struct ui_out *uiout = current_uiout;
   int regnum, numregs;
   int i;
-  struct cleanup *cleanup;
 
   /* Note that the test for a valid register must include checking the
      gdbarch_register_name because gdbarch_num_regs may be allocated
@@ -1023,7 +997,7 @@ mi_cmd_data_list_register_names (const char *command, char **argv, int argc)
   gdbarch = get_current_arch ();
   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
 
-  cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
+  ui_out_emit_list list_emitter (uiout, "register-names");
 
   if (argc == 0)               /* No args, just do all the regs.  */
     {
@@ -1052,7 +1026,6 @@ mi_cmd_data_list_register_names (const char *command, char **argv, int argc)
       else
        uiout->field_string (NULL, gdbarch_register_name (gdbarch, regnum));
     }
-  do_cleanups (cleanup);
 }
 
 void
@@ -1085,7 +1058,7 @@ mi_cmd_data_list_changed_registers (const char *command, char **argv, int argc)
   gdbarch = get_regcache_arch (this_regs);
   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
 
-  make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
+  ui_out_emit_list list_emitter (uiout, "changed-registers");
 
   if (argc == 0)
     {
@@ -1134,10 +1107,8 @@ register_changed_p (int regnum, struct regcache *prev_regs,
                    struct regcache *this_regs)
 {
   struct gdbarch *gdbarch = get_regcache_arch (this_regs);
-  gdb_byte prev_buffer[MAX_REGISTER_SIZE];
-  gdb_byte this_buffer[MAX_REGISTER_SIZE];
-  enum register_status prev_status;
-  enum register_status this_status;
+  struct value *prev_value, *this_value;
+  int ret;
 
   /* First time through or after gdbarch change consider all registers
      as changed.  */
@@ -1145,16 +1116,19 @@ register_changed_p (int regnum, struct regcache *prev_regs,
     return 1;
 
   /* Get register contents and compare.  */
-  prev_status = regcache_cooked_read (prev_regs, regnum, prev_buffer);
-  this_status = regcache_cooked_read (this_regs, regnum, this_buffer);
+  prev_value = prev_regs->cooked_read_value (regnum);
+  this_value = this_regs->cooked_read_value (regnum);
+  gdb_assert (prev_value != NULL);
+  gdb_assert (this_value != NULL);
 
-  if (this_status != prev_status)
-    return 1;
-  else if (this_status == REG_VALID)
-    return memcmp (prev_buffer, this_buffer,
-                  register_size (gdbarch, regnum)) != 0;
-  else
-    return 0;
+  ret = value_contents_eq (prev_value, 0, this_value, 0,
+                          register_size (gdbarch, regnum)) == 0;
+
+  release_value (prev_value);
+  release_value (this_value);
+  value_free (prev_value);
+  value_free (this_value);
+  return ret;
 }
 
 /* Return a list of register number and value pairs.  The valid
@@ -1174,7 +1148,6 @@ mi_cmd_data_list_register_values (const char *command, char **argv, int argc)
   struct gdbarch *gdbarch;
   int regnum, numregs, format;
   int i;
-  struct cleanup *list_cleanup;
   int skip_unavailable = 0;
   int oind = 0;
   enum opt
@@ -1221,7 +1194,7 @@ mi_cmd_data_list_register_values (const char *command, char **argv, int argc)
   gdbarch = get_frame_arch (frame);
   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
 
-  list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
+  ui_out_emit_list list_emitter (uiout, "register-values");
 
   if (argc - oind == 1)
     {
@@ -1251,7 +1224,6 @@ mi_cmd_data_list_register_values (const char *command, char **argv, int argc)
       else
        error (_("bad register number"));
     }
-  do_cleanups (list_cleanup);
 }
 
 /* Output one register REGNUM's contents in the desired FORMAT.  If
@@ -1264,13 +1236,12 @@ output_register (struct frame_info *frame, int regnum, int format,
 {
   struct ui_out *uiout = current_uiout;
   struct value *val = value_of_register (regnum, frame);
-  struct cleanup *tuple_cleanup;
   struct value_print_options opts;
 
   if (skip_unavailable && !value_entirely_available (val))
     return;
 
-  tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+  ui_out_emit_tuple tuple_emitter (uiout, NULL);
   uiout->field_int ("number", regnum);
 
   if (format == 'N')
@@ -1287,8 +1258,6 @@ output_register (struct frame_info *frame, int regnum, int format,
             value_embedded_offset (val), 0,
             &stb, 0, val, &opts, current_language);
   uiout->field_stream ("value", stb);
-
-  do_cleanups (tuple_cleanup);
 }
 
 /* Write given values into registers. The registers and values are
@@ -1493,12 +1462,12 @@ mi_cmd_data_read_memory (const char *command, char **argv, int argc)
   /* Create a buffer and read it in.  */
   total_bytes = word_size * nr_rows * nr_cols;
 
-  std::unique_ptr<gdb_byte[]> mbuf (new gdb_byte[total_bytes]);
+  gdb::byte_vector mbuf (total_bytes);
 
   /* Dispatch memory reads to the topmost target, not the flattened
      current_target.  */
   nr_bytes = target_read (current_target.beneath,
-                         TARGET_OBJECT_MEMORY, NULL, mbuf.get (),
+                         TARGET_OBJECT_MEMORY, NULL, mbuf.data (),
                          addr, total_bytes);
   if (nr_bytes <= 0)
     error (_("Unable to read memory."));
@@ -1516,44 +1485,43 @@ mi_cmd_data_read_memory (const char *command, char **argv, int argc)
   {
     int row;
     int row_byte;
-    struct cleanup *cleanup_list;
 
     string_file stream;
 
-    cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, "memory");
+    ui_out_emit_list list_emitter (uiout, "memory");
     for (row = 0, row_byte = 0;
         row < nr_rows;
         row++, row_byte += nr_cols * word_size)
       {
        int col;
        int col_byte;
-       struct cleanup *cleanup_tuple;
-       struct cleanup *cleanup_list_data;
        struct value_print_options opts;
 
-       cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+       ui_out_emit_tuple tuple_emitter (uiout, NULL);
        uiout->field_core_addr ("addr", gdbarch, addr + row_byte);
        /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
           row_byte); */
-       cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
-       get_formatted_print_options (&opts, word_format);
-       for (col = 0, col_byte = row_byte;
-            col < nr_cols;
-            col++, col_byte += word_size)
-         {
-           if (col_byte + word_size > nr_bytes)
-             {
-               uiout->field_string (NULL, "N/A");
-             }
-           else
-             {
-               stream.clear ();
-               print_scalar_formatted (&mbuf[col_byte], word_type, &opts,
-                                       word_asize, &stream);
-               uiout->field_stream (NULL, stream);
-             }
-         }
-       do_cleanups (cleanup_list_data);
+       {
+         ui_out_emit_list list_data_emitter (uiout, "data");
+         get_formatted_print_options (&opts, word_format);
+         for (col = 0, col_byte = row_byte;
+              col < nr_cols;
+              col++, col_byte += word_size)
+           {
+             if (col_byte + word_size > nr_bytes)
+               {
+                 uiout->field_string (NULL, "N/A");
+               }
+             else
+               {
+                 stream.clear ();
+                 print_scalar_formatted (&mbuf[col_byte], word_type, &opts,
+                                         word_asize, &stream);
+                 uiout->field_stream (NULL, stream);
+               }
+           }
+       }
+
        if (aschar)
          {
            int byte;
@@ -1571,9 +1539,7 @@ mi_cmd_data_read_memory (const char *command, char **argv, int argc)
              }
            uiout->field_stream ("ascii", stream);
          }
-       do_cleanups (cleanup_tuple);
       }
-    do_cleanups (cleanup_list);
   }
 }
 
@@ -1631,12 +1597,12 @@ mi_cmd_data_read_memory_bytes (const char *command, char **argv, int argc)
   if (VEC_length (memory_read_result_s, result) == 0)
     error (_("Unable to read memory."));
 
-  make_cleanup_ui_out_list_begin_end (uiout, "memory");
+  ui_out_emit_list list_emitter (uiout, "memory");
   for (ix = 0;
        VEC_iterate (memory_read_result_s, result, ix, read_result);
        ++ix)
     {
-      struct cleanup *t = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+      ui_out_emit_tuple tuple_emitter (uiout, NULL);
       char *data, *p;
       int i;
       int alloc_len;
@@ -1656,7 +1622,6 @@ mi_cmd_data_read_memory_bytes (const char *command, char **argv, int argc)
        }
       uiout->field_string ("contents", data);
       xfree (data);
-      do_cleanups (t);
     }
   do_cleanups (cleanups);
 }
@@ -1688,8 +1653,6 @@ mi_cmd_data_write_memory (const char *command, char **argv, int argc)
   /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
      enough when using a compiler other than GCC.  */
   LONGEST value;
-  gdb_byte *buffer;
-  struct cleanup *old_chain;
   long offset = 0;
   int oind = 0;
   char *oarg;
@@ -1736,13 +1699,10 @@ mi_cmd_data_write_memory (const char *command, char **argv, int argc)
   /* Get the value as a number.  */
   value = parse_and_eval_address (argv[3]);
   /* Get the value into an array.  */
-  buffer = (gdb_byte *) xmalloc (word_size);
-  old_chain = make_cleanup (xfree, buffer);
-  store_signed_integer (buffer, word_size, byte_order, value);
+  gdb::byte_vector buffer (word_size);
+  store_signed_integer (buffer.data (), word_size, byte_order, value);
   /* Write it down to memory.  */
-  write_memory_with_notification (addr, buffer, word_size);
-  /* Free the buffer.  */
-  do_cleanups (old_chain);
+  write_memory_with_notification (addr, buffer.data (), word_size);
 }
 
 /* Implementation of the -data-write-memory-bytes command.
@@ -1853,10 +1813,9 @@ mi_cmd_list_features (const char *command, char **argv, int argc)
 {
   if (argc == 0)
     {
-      struct cleanup *cleanup = NULL;
       struct ui_out *uiout = current_uiout;
 
-      cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
+      ui_out_emit_list list_emitter (uiout, "features");
       uiout->field_string (NULL, "frozen-varobjs");
       uiout->field_string (NULL, "pending-breakpoints");
       uiout->field_string (NULL, "thread-info");
@@ -1871,7 +1830,6 @@ mi_cmd_list_features (const char *command, char **argv, int argc)
       if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON)))
        uiout->field_string (NULL, "python");
 
-      do_cleanups (cleanup);
       return;
     }
 
@@ -1883,15 +1841,13 @@ mi_cmd_list_target_features (const char *command, char **argv, int argc)
 {
   if (argc == 0)
     {
-      struct cleanup *cleanup = NULL;
       struct ui_out *uiout = current_uiout;
 
-      cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
+      ui_out_emit_list list_emitter (uiout, "features");
       if (mi_async_p ())
        uiout->field_string (NULL, "async");
       if (target_can_execute_reverse)
        uiout->field_string (NULL, "reverse");
-      do_cleanups (cleanup);
       return;
     }
 
@@ -2031,7 +1987,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
        /* Echo the command on the console.  */
        fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
        /* Call the "console" interpreter.  */
-       argv[0] = INTERP_CONSOLE;
+       argv[0] = (char *) INTERP_CONSOLE;
        argv[1] = context->command;
        mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
 
@@ -2117,7 +2073,7 @@ void
 mi_execute_command (const char *cmd, int from_tty)
 {
   char *token;
-  struct mi_parse *command = NULL;
+  std::unique_ptr<struct mi_parse> command;
 
   /* This is to handle EOF (^D). We just quit gdb.  */
   /* FIXME: we should call some API function here.  */
@@ -2140,15 +2096,13 @@ mi_execute_command (const char *cmd, int from_tty)
   if (command != NULL)
     {
       ptid_t previous_ptid = inferior_ptid;
-      struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
 
-      command->token = token;
+      gdb::optional<scoped_restore_tmpl<int>> restore_suppress;
 
       if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
-        {
-          make_cleanup_restore_integer (command->cmd->suppress_notification);
-          *command->cmd->suppress_notification = 1;
-        }
+       restore_suppress.emplace (command->cmd->suppress_notification, 1);
+
+      command->token = token;
 
       if (do_timings)
        {
@@ -2158,7 +2112,7 @@ mi_execute_command (const char *cmd, int from_tty)
 
       TRY
        {
-         captured_mi_execute_command (current_uiout, command);
+         captured_mi_execute_command (current_uiout, command.get ());
        }
       CATCH (result, RETURN_MASK_ALL)
        {
@@ -2186,7 +2140,7 @@ mi_execute_command (const char *cmd, int from_tty)
          && thread_count () != 0
          /* If the command already reports the thread change, no need to do it
             again.  */
-         && !command_notifies_uscc_observer (command))
+         && !command_notifies_uscc_observer (command.get ()))
        {
          struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
          int report_change = 0;
@@ -2210,10 +2164,6 @@ mi_execute_command (const char *cmd, int from_tty)
                  (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
            }
        }
-
-      mi_parse_free (command);
-
-      do_cleanups (cleanup);
     }
 }
 
@@ -2282,9 +2232,10 @@ mi_cmd_execute (struct mi_parse *parse)
        error (_("Invalid frame id: %d"), frame);
     }
 
+  gdb::optional<scoped_restore_current_language> lang_saver;
   if (parse->language != language_unknown)
     {
-      make_cleanup_restore_current_language ();
+      lang_saver.emplace ();
       set_language (parse->language);
     }
 
@@ -2344,7 +2295,7 @@ mi_execute_cli_command (const char *cmd, int args_p, const char *args)
 }
 
 void
-mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
+mi_execute_async_cli_command (const char *cli_command, char **argv, int argc)
 {
   struct cleanup *old_cleanups;
   char *run;
@@ -2374,7 +2325,6 @@ mi_load_progress (const char *section_name,
   static char *previous_sect_name = NULL;
   int new_section;
   struct ui_out *saved_uiout;
-  struct ui_out *uiout;
   struct mi_interp *mi = (struct mi_interp *) current_interpreter ();
 
   /* This function is called through deprecated_show_load_progress
@@ -2382,36 +2332,37 @@ mi_load_progress (const char *section_name,
      of this function.  */
   saved_uiout = current_uiout;
 
+  std::unique_ptr<ui_out> uiout;
+
   if (current_interp_named_p (INTERP_MI)
       || current_interp_named_p (INTERP_MI2))
-    current_uiout = mi_out_new (2);
+    uiout.reset (mi_out_new (2));
   else if (current_interp_named_p (INTERP_MI1))
-    current_uiout = mi_out_new (1);
+    uiout.reset (mi_out_new (1));
   else if (current_interp_named_p (INTERP_MI3))
-    current_uiout = mi_out_new (3);
+    uiout.reset (mi_out_new (3));
   else
     return;
 
-  uiout = current_uiout;
+  current_uiout = uiout.get ();
 
   new_section = (previous_sect_name ?
                 strcmp (previous_sect_name, section_name) : 1);
   if (new_section)
     {
-      struct cleanup *cleanup_tuple;
-
       xfree (previous_sect_name);
       previous_sect_name = xstrdup (section_name);
 
       if (current_token)
        fputs_unfiltered (current_token, mi->raw_stdout);
       fputs_unfiltered ("+download", mi->raw_stdout);
-      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
-      uiout->field_string ("section", section_name);
-      uiout->field_int ("section-size", total_section);
-      uiout->field_int ("total-size", grand_total);
-      do_cleanups (cleanup_tuple);
-      mi_out_put (uiout, mi->raw_stdout);
+      {
+       ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
+       uiout->field_string ("section", section_name);
+       uiout->field_int ("section-size", total_section);
+       uiout->field_int ("total-size", grand_total);
+      }
+      mi_out_put (uiout.get (), mi->raw_stdout);
       fputs_unfiltered ("\n", mi->raw_stdout);
       gdb_flush (mi->raw_stdout);
     }
@@ -2419,25 +2370,23 @@ mi_load_progress (const char *section_name,
   steady_clock::time_point time_now = steady_clock::now ();
   if (time_now - last_update > milliseconds (500))
     {
-      struct cleanup *cleanup_tuple;
-
       last_update = time_now;
       if (current_token)
        fputs_unfiltered (current_token, mi->raw_stdout);
       fputs_unfiltered ("+download", mi->raw_stdout);
-      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
-      uiout->field_string ("section", section_name);
-      uiout->field_int ("section-sent", sent_so_far);
-      uiout->field_int ("section-size", total_section);
-      uiout->field_int ("total-sent", total_sent);
-      uiout->field_int ("total-size", grand_total);
-      do_cleanups (cleanup_tuple);
-      mi_out_put (uiout, mi->raw_stdout);
+      {
+       ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
+       uiout->field_string ("section", section_name);
+       uiout->field_int ("section-sent", sent_so_far);
+       uiout->field_int ("section-size", total_section);
+       uiout->field_int ("total-sent", total_sent);
+       uiout->field_int ("total-size", grand_total);
+      }
+      mi_out_put (uiout.get (), mi->raw_stdout);
       fputs_unfiltered ("\n", mi->raw_stdout);
       gdb_flush (mi->raw_stdout);
     }
 
-  xfree (uiout);
   current_uiout = saved_uiout;
 }
 
@@ -2571,29 +2520,22 @@ mi_cmd_trace_find (const char *command, char **argv, int argc)
     }
   else if (strcmp (mode, "line") == 0)
     {
-      struct symtabs_and_lines sals;
-      struct symtab_and_line sal;
-      static CORE_ADDR start_pc, end_pc;
-      struct cleanup *back_to;
-
       if (argc != 2)
        error (_("Line is required"));
 
-      sals = decode_line_with_current_source (argv[1],
-                                             DECODE_LINE_FUNFIRSTLINE);
-      back_to = make_cleanup (xfree, sals.sals);
-
-      sal = sals.sals[0];
+      std::vector<symtab_and_line> sals
+       = decode_line_with_current_source (argv[1],
+                                          DECODE_LINE_FUNFIRSTLINE);
+      const symtab_and_line &sal = sals[0];
 
       if (sal.symtab == 0)
        error (_("Could not find the specified line"));
 
+      CORE_ADDR start_pc, end_pc;
       if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
        tfind_1 (tfind_range, 0, start_pc, end_pc - 1, 0);
       else
        error (_("Could not find the specified line"));
-
-      do_cleanups (back_to);
     }
   else
     error (_("Invalid mode '%s'"), mode);
@@ -2687,7 +2629,6 @@ mi_cmd_ada_task_info (const char *command, char **argv, int argc)
 static void
 print_variable_or_computed (const char *expression, enum print_values values)
 {
-  struct cleanup *old_chain;
   struct value *val;
   struct type *type;
   struct ui_out *uiout = current_uiout;
@@ -2701,9 +2642,9 @@ print_variable_or_computed (const char *expression, enum print_values values)
   else
     val = evaluate_expression (expr.get ());
 
-  old_chain = make_cleanup (null_cleanup, NULL);
+  gdb::optional<ui_out_emit_tuple> tuple_emitter;
   if (values != PRINT_NO_VALUES)
-    make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+    tuple_emitter.emplace (uiout, nullptr);
   uiout->field_string ("name", expression);
 
   switch (values)
@@ -2735,8 +2676,6 @@ print_variable_or_computed (const char *expression, enum print_values values)
       }
       break;
     }
-
-  do_cleanups (old_chain);
 }
 
 /* Implement the "-trace-frame-collected" command.  */
@@ -2744,7 +2683,6 @@ print_variable_or_computed (const char *expression, enum print_values values)
 void
 mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
 {
-  struct cleanup *old_chain;
   struct bp_location *tloc;
   int stepping_frame;
   struct collection_list *clist;
@@ -2807,7 +2745,7 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
 
   /* This command only makes sense for the current frame, not the
      selected frame.  */
-  old_chain = make_cleanup_restore_current_thread ();
+  scoped_restore_current_thread restore_thread;
   select_frame (get_current_frame ());
 
   encode_actions (tloc, &tracepoint_list, &stepping_list);
@@ -2821,12 +2759,9 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
 
   /* Explicitly wholly collected variables.  */
   {
-    struct cleanup *list_cleanup;
     int i;
 
-    list_cleanup = make_cleanup_ui_out_list_begin_end (uiout,
-                                                      "explicit-variables");
-
+    ui_out_emit_list list_emitter (uiout, "explicit-variables");
     const std::vector<std::string> &wholly_collected
       = clist->wholly_collected ();
     for (size_t i = 0; i < wholly_collected.size (); i++)
@@ -2834,19 +2769,14 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
        const std::string &str = wholly_collected[i];
        print_variable_or_computed (str.c_str (), var_print_values);
       }
-
-    do_cleanups (list_cleanup);
   }
 
   /* Computed expressions.  */
   {
-    struct cleanup *list_cleanup;
     char *p;
     int i;
 
-    list_cleanup
-      = make_cleanup_ui_out_list_begin_end (uiout,
-                                           "computed-expressions");
+    ui_out_emit_list list_emitter (uiout, "computed-expressions");
 
     const std::vector<std::string> &computed = clist->computed ();
     for (size_t i = 0; i < computed.size (); i++)
@@ -2854,8 +2784,6 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
        const std::string &str = computed[i];
        print_variable_or_computed (str.c_str (), comp_print_values);
       }
-
-    do_cleanups (list_cleanup);
   }
 
   /* Registers.  Given pseudo-registers, and that some architectures
@@ -2863,13 +2791,12 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
      the trace frame info, but instead consult the register cache for
      register availability.  */
   {
-    struct cleanup *list_cleanup;
     struct frame_info *frame;
     struct gdbarch *gdbarch;
     int regnum;
     int numregs;
 
-    list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "registers");
+    ui_out_emit_list list_emitter (uiout, "registers");
 
     frame = get_selected_frame (NULL);
     gdbarch = get_frame_arch (frame);
@@ -2883,30 +2810,27 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
 
        output_register (frame, regnum, registers_format, 1);
       }
-
-    do_cleanups (list_cleanup);
   }
 
   /* Trace state variables.  */
   {
-    struct cleanup *list_cleanup;
+    struct cleanup *cleanups;
     int tvar;
     char *tsvname;
     int i;
 
-    list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "tvars");
+    ui_out_emit_list list_emitter (uiout, "tvars");
 
     tsvname = NULL;
-    make_cleanup (free_current_contents, &tsvname);
+    cleanups = make_cleanup (free_current_contents, &tsvname);
 
     for (i = 0; VEC_iterate (int, tinfo->tvars, i, tvar); i++)
       {
-       struct cleanup *cleanup_child;
        struct trace_state_variable *tsv;
 
        tsv = find_trace_state_variable_by_number (tvar);
 
-       cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+       ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
        if (tsv != NULL)
          {
@@ -2924,63 +2848,48 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
            uiout->field_skip ("name");
            uiout->field_skip ("current");
          }
-
-       do_cleanups (cleanup_child);
       }
 
-    do_cleanups (list_cleanup);
+    do_cleanups (cleanups);
   }
 
   /* Memory.  */
   {
-    struct cleanup *list_cleanup;
+    struct cleanup *cleanups;
     VEC(mem_range_s) *available_memory = NULL;
     struct mem_range *r;
     int i;
 
     traceframe_available_memory (&available_memory, 0, ULONGEST_MAX);
-    make_cleanup (VEC_cleanup(mem_range_s), &available_memory);
+    cleanups = make_cleanup (VEC_cleanup(mem_range_s), &available_memory);
 
-    list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "memory");
+    ui_out_emit_list list_emitter (uiout, "memory");
 
     for (i = 0; VEC_iterate (mem_range_s, available_memory, i, r); i++)
       {
-       struct cleanup *cleanup_child;
-       gdb_byte *data;
        struct gdbarch *gdbarch = target_gdbarch ();
 
-       cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+       ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
        uiout->field_core_addr ("address", gdbarch, r->start);
        uiout->field_int ("length", r->length);
 
-       data = (gdb_byte *) xmalloc (r->length);
-       make_cleanup (xfree, data);
+       gdb::byte_vector data (r->length);
 
        if (memory_contents)
          {
-           if (target_read_memory (r->start, data, r->length) == 0)
+           if (target_read_memory (r->start, data.data (), r->length) == 0)
              {
-               int m;
-               char *data_str, *p;
-
-               data_str = (char *) xmalloc (r->length * 2 + 1);
-               make_cleanup (xfree, data_str);
-
-               for (m = 0, p = data_str; m < r->length; ++m, p += 2)
-                 sprintf (p, "%02x", data[m]);
-               uiout->field_string ("contents", data_str);
+               std::string data_str = bin2hex (data.data (), r->length);
+               uiout->field_string ("contents", data_str.c_str ());
              }
            else
              uiout->field_skip ("contents");
          }
-       do_cleanups (cleanup_child);
       }
 
-    do_cleanups (list_cleanup);
+    do_cleanups (cleanups);
   }
-
-  do_cleanups (old_chain);
 }
 
 void
This page took 0.056546 seconds and 4 git commands to generate.