gdb: change single_displaced_buffer_manager into multiple_displaced_buffer_manager
[deliverable/binutils-gdb.git] / gdb / printcmd.c
index 645ac4da9a4785025738adc5d1ea6d2d9d3f2b8b..388a0b2c6d414450f6c54c5f463d538167b29a9b 100644 (file)
@@ -116,13 +116,27 @@ show_print_symbol_filename (struct ui_file *file, int from_tty,
 
 static int current_display_number;
 
+/* Last allocated display number.  */
+
+static int display_number;
+
 struct display
   {
-    /* Chain link to next auto-display item.  */
-    struct display *next;
+    display (const char *exp_string_, expression_up &&exp_,
+            const struct format_data &format_, struct program_space *pspace_,
+            const struct block *block_)
+      : exp_string (exp_string_),
+       exp (std::move (exp_)),
+       number (++display_number),
+       format (format_),
+       pspace (pspace_),
+       block (block_),
+       enabled_p (true)
+    {
+    }
 
     /* The expression as the user typed it.  */
-    char *exp_string;
+    std::string exp_string;
 
     /* Expression to be evaluated and displayed.  */
     expression_up exp;
@@ -140,27 +154,13 @@ struct display
     const struct block *block;
 
     /* Status of this display (enabled or disabled).  */
-    int enabled_p;
+    bool enabled_p;
   };
 
-/* Chain of expressions whose values should be displayed
-   automatically each time the program stops.  */
-
-static struct display *display_chain;
+/* Expressions whose values should be displayed automatically each
+   time the program stops.  */
 
-static int display_number;
-
-/* Walk the following statement or block through all displays.
-   ALL_DISPLAYS_SAFE does so even if the statement deletes the current
-   display.  */
-
-#define ALL_DISPLAYS(B)                                \
-  for (B = display_chain; B; B = B->next)
-
-#define ALL_DISPLAYS_SAFE(B,TMP)               \
-  for (B = display_chain;                      \
-       B ? (TMP = B->next, 1): 0;              \
-       B = TMP)
+static std::vector<std::unique_ptr<struct display>> all_displays;
 
 /* Prototypes for local functions.  */
 
@@ -311,12 +311,12 @@ print_formatted (struct value *val, int size,
     }
 
   if (options->format == 0 || options->format == 's'
-      || TYPE_CODE (type) == TYPE_CODE_REF
-      || TYPE_CODE (type) == TYPE_CODE_ARRAY
-      || TYPE_CODE (type) == TYPE_CODE_STRING
-      || TYPE_CODE (type) == TYPE_CODE_STRUCT
-      || TYPE_CODE (type) == TYPE_CODE_UNION
-      || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
+      || type->code () == TYPE_CODE_REF
+      || type->code () == TYPE_CODE_ARRAY
+      || type->code () == TYPE_CODE_STRING
+      || type->code () == TYPE_CODE_STRUCT
+      || type->code () == TYPE_CODE_UNION
+      || type->code () == TYPE_CODE_NAMESPACE)
     value_print (val, stream, options);
   else
     /* User specified format, so don't look to the type to tell us
@@ -361,7 +361,7 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
   /* If the value is a pointer, and pointers and addresses are not the
      same, then at this point, the value's length (in target bytes) is
      gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type).  */
-  if (TYPE_CODE (type) == TYPE_CODE_PTR)
+  if (type->code () == TYPE_CODE_PTR)
     len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
 
   /* If we are printing it as unsigned, truncate it in case it is actually
@@ -411,14 +411,14 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
      range case, we want to avoid this, so we store the unpacked value
      here for possible use later.  */
   gdb::optional<LONGEST> val_long;
-  if ((TYPE_CODE (type) == TYPE_CODE_FLT
+  if ((type->code () == TYPE_CODE_FLT
        && (options->format == 'o'
           || options->format == 'x'
           || options->format == 't'
           || options->format == 'z'
           || options->format == 'd'
           || options->format == 'u'))
-      || (TYPE_CODE (type) == TYPE_CODE_RANGE
+      || (type->code () == TYPE_CODE_RANGE
          && TYPE_RANGE_DATA (type)->bias != 0))
     {
       val_long.emplace (unpack_long (type, valaddr));
@@ -432,10 +432,10 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
      of a floating-point type of the same length, if that exists.  Otherwise,
      the data is printed as integer.  */
   char format = options->format;
-  if (format == 'f' && TYPE_CODE (type) != TYPE_CODE_FLT)
+  if (format == 'f' && type->code () != TYPE_CODE_FLT)
     {
       type = float_type_from_length (type);
-      if (TYPE_CODE (type) != TYPE_CODE_FLT)
+      if (type->code () != TYPE_CODE_FLT)
         format = 0;
     }
 
@@ -451,7 +451,7 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
       print_decimal_chars (stream, valaddr, len, false, byte_order);
       break;
     case 0:
-      if (TYPE_CODE (type) != TYPE_CODE_FLT)
+      if (type->code () != TYPE_CODE_FLT)
        {
          print_decimal_chars (stream, valaddr, len, !TYPE_UNSIGNED (type),
                               byte_order);
@@ -1218,7 +1218,7 @@ print_command_1 (const char *args, int voidprint)
     val = access_value_history (0);
 
   if (voidprint || (val && value_type (val) &&
-                   TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
+                   value_type (val)->code () != TYPE_CODE_VOID))
     print_value (val, print_opts);
 }
 
@@ -1442,7 +1442,7 @@ info_address_command (const char *exp, int from_tty)
        {
          struct objfile *objfile = msymbol.objfile;
 
-         gdbarch = get_objfile_arch (objfile);
+         gdbarch = objfile->arch ();
          load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
 
          printf_filtered ("Symbol \"");
@@ -1681,8 +1681,7 @@ x_command (const char *exp, int from_tty)
        val = coerce_ref (val);
       /* In rvalue contexts, such as this, functions are coerced into
          pointers to functions.  This makes "x/i main" work.  */
-      if (/* last_format == 'i'  && */ 
-         TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
+      if (value_type (val)->code () == TYPE_CODE_FUNC
           && VALUE_LVAL (val) == lval_memory)
        next_address = value_address (val);
       else
@@ -1763,27 +1762,9 @@ display_command (const char *arg, int from_tty)
   innermost_block_tracker tracker;
   expression_up expr = parse_expression (exp, &tracker);
 
-  newobj = new display ();
-
-  newobj->exp_string = xstrdup (exp);
-  newobj->exp = std::move (expr);
-  newobj->block = tracker.block ();
-  newobj->pspace = current_program_space;
-  newobj->number = ++display_number;
-  newobj->format = fmt;
-  newobj->enabled_p = 1;
-  newobj->next = NULL;
-
-  if (display_chain == NULL)
-    display_chain = newobj;
-  else
-    {
-      struct display *last;
-
-      for (last = display_chain; last->next != NULL; last = last->next)
-       ;
-      last->next = newobj;
-    }
+  newobj = new display (exp, std::move (expr), fmt,
+                       current_program_space, tracker.block ());
+  all_displays.emplace_back (newobj);
 
   if (from_tty)
     do_one_display (newobj);
@@ -1791,26 +1772,13 @@ display_command (const char *arg, int from_tty)
   dont_repeat ();
 }
 
-static void
-free_display (struct display *d)
-{
-  xfree (d->exp_string);
-  delete d;
-}
-
 /* Clear out the display_chain.  Done when new symtabs are loaded,
    since this invalidates the types stored in many expressions.  */
 
 void
-clear_displays (void)
+clear_displays ()
 {
-  struct display *d;
-
-  while ((d = display_chain) != NULL)
-    {
-      display_chain = d->next;
-      free_display (d);
-    }
+  all_displays.clear ();
 }
 
 /* Delete the auto-display DISPLAY.  */
@@ -1818,21 +1786,16 @@ clear_displays (void)
 static void
 delete_display (struct display *display)
 {
-  struct display *d;
-
   gdb_assert (display != NULL);
 
-  if (display_chain == display)
-    display_chain = display->next;
-
-  ALL_DISPLAYS (d)
-    if (d->next == display)
-      {
-       d->next = display->next;
-       break;
-      }
-
-  free_display (display);
+  auto iter = std::find_if (all_displays.begin (),
+                           all_displays.end (),
+                           [=] (const std::unique_ptr<struct display> &item)
+                           {
+                             return item.get () == display;
+                           });
+  gdb_assert (iter != all_displays.end ());
+  all_displays.erase (iter);
 }
 
 /* Call FUNCTION on each of the displays whose numbers are given in
@@ -1840,9 +1803,7 @@ delete_display (struct display *display)
 
 static void
 map_display_numbers (const char *args,
-                    void (*function) (struct display *,
-                                      void *),
-                    void *data)
+                    gdb::function_view<void (struct display *)> function)
 {
   int num;
 
@@ -1860,27 +1821,20 @@ map_display_numbers (const char *args,
        warning (_("bad display number at or near '%s'"), p);
       else
        {
-         struct display *d, *tmp;
-
-         ALL_DISPLAYS_SAFE (d, tmp)
-           if (d->number == num)
-             break;
-         if (d == NULL)
+         auto iter = std::find_if (all_displays.begin (),
+                                   all_displays.end (),
+                                   [=] (const std::unique_ptr<display> &item)
+                                   {
+                                     return item->number == num;
+                                   });
+         if (iter == all_displays.end ())
            printf_unfiltered (_("No display number %d.\n"), num);
          else
-           function (d, data);
+           function (iter->get ());
        }
     }
 }
 
-/* Callback for map_display_numbers, that deletes a display.  */
-
-static void
-do_delete_display (struct display *d, void *data)
-{
-  delete_display (d);
-}
-
 /* "undisplay" command.  */
 
 static void
@@ -1894,7 +1848,7 @@ undisplay_command (const char *args, int from_tty)
       return;
     }
 
-  map_display_numbers (args, do_delete_display, NULL);
+  map_display_numbers (args, delete_display);
   dont_repeat ();
 }
 
@@ -1907,7 +1861,7 @@ do_one_display (struct display *d)
 {
   int within_current_scope;
 
-  if (d->enabled_p == 0)
+  if (!d->enabled_p)
     return;
 
   /* The expression carries the architecture that was used at parse time.
@@ -1929,15 +1883,15 @@ do_one_display (struct display *d)
       try
        {
          innermost_block_tracker tracker;
-         d->exp = parse_expression (d->exp_string, &tracker);
+         d->exp = parse_expression (d->exp_string.c_str (), &tracker);
          d->block = tracker.block ();
        }
       catch (const gdb_exception &ex)
        {
          /* Can't re-parse the expression.  Disable this display item.  */
-         d->enabled_p = 0;
+         d->enabled_p = false;
          warning (_("Unable to display \"%s\": %s"),
-                  d->exp_string, ex.what ());
+                  d->exp_string.c_str (), ex.what ());
          return;
        }
     }
@@ -1977,7 +1931,7 @@ do_one_display (struct display *d)
 
       annotate_display_expression ();
 
-      puts_filtered (d->exp_string);
+      puts_filtered (d->exp_string.c_str ());
       annotate_display_expression_end ();
 
       if (d->format.count != 1 || d->format.format == 'i')
@@ -2016,7 +1970,7 @@ do_one_display (struct display *d)
 
       annotate_display_expression ();
 
-      puts_filtered (d->exp_string);
+      puts_filtered (d->exp_string.c_str ());
       annotate_display_expression_end ();
 
       printf_filtered (" = ");
@@ -2053,10 +2007,8 @@ do_one_display (struct display *d)
 void
 do_displays (void)
 {
-  struct display *d;
-
-  for (d = display_chain; d; d = d->next)
-    do_one_display (d);
+  for (auto &d : all_displays)
+    do_one_display (d.get ());
 }
 
 /* Delete the auto-display which we were in the process of displaying.
@@ -2065,12 +2017,10 @@ do_displays (void)
 void
 disable_display (int num)
 {
-  struct display *d;
-
-  for (d = display_chain; d; d = d->next)
+  for (auto &d : all_displays)
     if (d->number == num)
       {
-       d->enabled_p = 0;
+       d->enabled_p = false;
        return;
       }
   printf_unfiltered (_("No display number %d.\n"), num);
@@ -2093,15 +2043,13 @@ disable_current_display (void)
 static void
 info_display_command (const char *ignore, int from_tty)
 {
-  struct display *d;
-
-  if (!display_chain)
+  if (all_displays.empty ())
     printf_unfiltered (_("There are no auto-display expressions now.\n"));
   else
     printf_filtered (_("Auto-display expressions now in effect:\n\
 Num Enb Expression\n"));
 
-  for (d = display_chain; d; d = d->next)
+  for (auto &d : all_displays)
     {
       printf_filtered ("%d:   %c  ", d->number, "ny"[(int) d->enabled_p]);
       if (d->format.size)
@@ -2109,38 +2057,31 @@ Num Enb Expression\n"));
                         d->format.format);
       else if (d->format.format)
        printf_filtered ("/%c ", d->format.format);
-      puts_filtered (d->exp_string);
+      puts_filtered (d->exp_string.c_str ());
       if (d->block && !contained_in (get_selected_block (0), d->block, true))
        printf_filtered (_(" (cannot be evaluated in the current context)"));
       printf_filtered ("\n");
     }
 }
 
-/* Callback fo map_display_numbers, that enables or disables the
-   passed in display D.  */
-
-static void
-do_enable_disable_display (struct display *d, void *data)
-{
-  d->enabled_p = *(int *) data;
-}
-
 /* Implementation of both the "disable display" and "enable display"
    commands.  ENABLE decides what to do.  */
 
 static void
-enable_disable_display_command (const char *args, int from_tty, int enable)
+enable_disable_display_command (const char *args, int from_tty, bool enable)
 {
   if (args == NULL)
     {
-      struct display *d;
-
-      ALL_DISPLAYS (d)
+      for (auto &d : all_displays)
        d->enabled_p = enable;
       return;
     }
 
-  map_display_numbers (args, do_enable_disable_display, &enable);
+  map_display_numbers (args,
+                      [=] (struct display *d)
+                      {
+                        d->enabled_p = enable;
+                      });
 }
 
 /* The "enable display" command.  */
@@ -2148,7 +2089,7 @@ enable_disable_display_command (const char *args, int from_tty, int enable)
 static void
 enable_display_command (const char *args, int from_tty)
 {
-  enable_disable_display_command (args, from_tty, 1);
+  enable_disable_display_command (args, from_tty, true);
 }
 
 /* The "disable display" command.  */
@@ -2156,7 +2097,7 @@ enable_display_command (const char *args, int from_tty)
 static void
 disable_display_command (const char *args, int from_tty)
 {
-  enable_disable_display_command (args, from_tty, 0);
+  enable_disable_display_command (args, from_tty, false);
 }
 
 /* display_chain items point to blocks and expressions.  Some expressions in
@@ -2170,7 +2111,6 @@ disable_display_command (const char *args, int from_tty)
 static void
 clear_dangling_display_expressions (struct objfile *objfile)
 {
-  struct display *d;
   struct program_space *pspace;
 
   /* With no symbol file we cannot have a block or expression from it.  */
@@ -2183,17 +2123,25 @@ clear_dangling_display_expressions (struct objfile *objfile)
       gdb_assert (objfile->pspace == pspace);
     }
 
-  for (d = display_chain; d != NULL; d = d->next)
+  for (auto &d : all_displays)
     {
       if (d->pspace != pspace)
        continue;
 
-      if (lookup_objfile_from_block (d->block) == objfile
+      struct objfile *bl_objf = nullptr;
+      if (d->block != nullptr)
+       {
+         bl_objf = block_objfile (d->block);
+         if (bl_objf->separate_debug_objfile_backlink != nullptr)
+           bl_objf = bl_objf->separate_debug_objfile_backlink;
+       }
+
+      if (bl_objf == objfile
          || (d->exp != NULL && exp_uses_objfile (d->exp.get (), objfile)))
-      {
-       d->exp.reset ();
-       d->block = NULL;
-      }
+       {
+         d->exp.reset ();
+         d->block = NULL;
+       }
     }
 }
 \f
@@ -2257,7 +2205,7 @@ printf_c_string (struct ui_file *stream, const char *format,
 {
   const gdb_byte *str;
 
-  if (TYPE_CODE (value_type (value)) != TYPE_CODE_PTR
+  if (value_type (value)->code () != TYPE_CODE_PTR
       && VALUE_LVAL (value) == lval_internalvar
       && c_is_string_type_p (value_type (value)))
     {
@@ -2438,7 +2386,7 @@ printf_floating (struct ui_file *stream, const char *format,
      In either case, the result of the conversion is a byte buffer
      formatted in the target format for the target type.  */
 
-  if (TYPE_CODE (fmt_type) == TYPE_CODE_FLT)
+  if (fmt_type->code () == TYPE_CODE_FLT)
     {
       param_type = float_type_from_length (param_type);
       if (param_type != value_type (value))
@@ -2608,7 +2556,7 @@ ui_printf (const char *arg, struct ui_file *stream)
 
              valtype = value_type (val_args[i]);
              if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
-                 || TYPE_CODE (valtype) != TYPE_CODE_INT)
+                 || valtype->code () != TYPE_CODE_INT)
                error (_("expected wchar_t argument for %%lc"));
 
              bytes = value_contents (val_args[i]);
This page took 0.031014 seconds and 4 git commands to generate.