Fix crash with DW_FORM_implicit_const
[binutils-gdb.git] / gdb / valprint.c
index 578a2294dfedb7c83fe026aee2e580c143a86a65..9489662168086d4ab8e71871ce1b96b365fa42c7 100644 (file)
@@ -373,16 +373,15 @@ valprint_check_validity (struct ui_file *stream,
       && type->code () != TYPE_CODE_STRUCT
       && type->code () != TYPE_CODE_ARRAY)
     {
-      if (value_bits_any_optimized_out (val,
-                                       TARGET_CHAR_BIT * embedded_offset,
-                                       TARGET_CHAR_BIT * type->length ()))
+      if (val->bits_any_optimized_out (TARGET_CHAR_BIT * embedded_offset,
+                                      TARGET_CHAR_BIT * type->length ()))
        {
          val_print_optimized_out (val, stream);
          return 0;
        }
 
-      if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
-                                       TARGET_CHAR_BIT * type->length ()))
+      if (val->bits_synthetic_pointer (TARGET_CHAR_BIT * embedded_offset,
+                                      TARGET_CHAR_BIT * type->length ()))
        {
          const int is_ref = type->code () == TYPE_CODE_REF;
          int ref_is_addressable = 0;
@@ -403,7 +402,7 @@ valprint_check_validity (struct ui_file *stream,
          return is_ref;
        }
 
-      if (!value_bytes_available (val, embedded_offset, type->length ()))
+      if (!val->bytes_available (embedded_offset, type->length ()))
        {
          val_print_unavailable (stream);
          return 0;
@@ -513,7 +512,7 @@ generic_value_print_ptr (struct value *val, struct ui_file *stream,
     {
       struct type *type = check_typedef (val->type ());
       struct type *elttype = check_typedef (type->target_type ());
-      const gdb_byte *valaddr = value_contents_for_printing (val).data ();
+      const gdb_byte *valaddr = val->contents_for_printing ().data ();
       CORE_ADDR addr = unpack_pointer (type, valaddr);
 
       print_unpacked_pointer (type, elttype, addr, stream, options);
@@ -549,7 +548,7 @@ get_value_addr_contents (struct value *deref_val)
   gdb_assert (deref_val != NULL);
 
   if (deref_val->lval () == lval_memory)
-    return value_contents_for_printing_const (value_addr (deref_val)).data ();
+    return value_addr (deref_val)->contents_for_printing ().data ();
   else
     {
       /* We have a non-addressable value, such as a DW_AT_const_value.  */
@@ -567,14 +566,13 @@ generic_val_print_ref (struct type *type,
 {
   struct type *elttype = check_typedef (type->target_type ());
   struct value *deref_val = NULL;
-  const int value_is_synthetic
-    = value_bits_synthetic_pointer (original_value,
-                                   TARGET_CHAR_BIT * embedded_offset,
-                                   TARGET_CHAR_BIT * type->length ());
+  const bool value_is_synthetic
+    = original_value->bits_synthetic_pointer (TARGET_CHAR_BIT * embedded_offset,
+                                             TARGET_CHAR_BIT * type->length ());
   const int must_coerce_ref = ((options->addressprint && value_is_synthetic)
                               || options->deref_ref);
   const int type_is_defined = elttype->code () != TYPE_CODE_UNDEF;
-  const gdb_byte *valaddr = value_contents_for_printing (original_value).data ();
+  const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
 
   if (must_coerce_ref && type_is_defined)
     {
@@ -722,7 +720,7 @@ generic_val_print_enum (struct type *type,
 
   gdb_assert (!options->format);
 
-  const gdb_byte *valaddr = value_contents_for_printing (original_value).data ();
+  const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
 
   val = unpack_long (type, valaddr + embedded_offset * unit_size);
 
@@ -769,7 +767,7 @@ generic_value_print_bool
     }
   else
     {
-      const gdb_byte *valaddr = value_contents_for_printing (value).data ();
+      const gdb_byte *valaddr = value->contents_for_printing ().data ();
       struct type *type = check_typedef (value->type ());
       LONGEST val = unpack_long (type, valaddr);
       if (val == 0)
@@ -812,7 +810,7 @@ generic_value_print_char (struct value *value, struct ui_file *stream,
     {
       struct type *unresolved_type = value->type ();
       struct type *type = check_typedef (unresolved_type);
-      const gdb_byte *valaddr = value_contents_for_printing (value).data ();
+      const gdb_byte *valaddr = value->contents_for_printing ().data ();
 
       LONGEST val = unpack_long (type, valaddr);
       if (type->is_unsigned ())
@@ -833,7 +831,7 @@ generic_val_print_float (struct type *type, struct ui_file *stream,
 {
   gdb_assert (!options->format);
 
-  const gdb_byte *valaddr = value_contents_for_printing (original_value).data ();
+  const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
 
   print_floating (valaddr, type, stream);
 }
@@ -850,7 +848,7 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream,
     {
       struct type *type = val->type ();
 
-      const gdb_byte *valaddr = value_contents_for_printing (val).data ();
+      const gdb_byte *valaddr = val->contents_for_printing ().data ();
       gdb_mpf f;
 
       f.read_fixed_point (gdb::make_array_view (valaddr, type->length ()),
@@ -858,7 +856,7 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream,
                          type->fixed_point_scaling_factor ());
 
       const char *fmt = type->length () < 4 ? "%.11Fg" : "%.17Fg";
-      std::string str = gmp_string_printf (fmt, f.val);
+      std::string str = f.str (fmt);
       gdb_printf (stream, "%s", str.c_str ());
     }
 }
@@ -896,7 +894,7 @@ generic_value_print_memberptr
       /* Member pointers are essentially specific to C++, and so if we
         encounter one, we should print it according to C++ rules.  */
       struct type *type = check_typedef (val->type ());
-      const gdb_byte *valaddr = value_contents_for_printing (val).data ();
+      const gdb_byte *valaddr = val->contents_for_printing ().data ();
       cp_print_class_member (valaddr, type, stream, "&");
     }
   else
@@ -1013,7 +1011,7 @@ generic_value_print (struct value *val, struct ui_file *stream, int recurse,
       break;
 
     case TYPE_CODE_METHODPTR:
-      cplus_print_method_ptr (value_contents_for_printing (val).data (), type,
+      cplus_print_method_ptr (val->contents_for_printing ().data (), type,
                              stream);
       break;
 
@@ -1044,7 +1042,7 @@ common_val_print (struct value *value, struct ui_file *stream, int recurse,
     value = ada_to_fixed_value (value);
 
   if (value->lazy ())
-    value_fetch_lazy (value);
+    value->fetch_lazy ();
 
   struct value_print_options local_opts = *options;
   struct type *type = value->type ();
@@ -1132,7 +1130,7 @@ value_check_printable (struct value *val, struct ui_file *stream,
       return 0;
     }
 
-  if (value_entirely_optimized_out (val))
+  if (val->entirely_optimized_out ())
     {
       if (options->summary && !val_print_scalar_type_p (val->type ()))
        gdb_printf (stream, "...");
@@ -1141,7 +1139,7 @@ value_check_printable (struct value *val, struct ui_file *stream,
       return 0;
     }
 
-  if (value_entirely_unavailable (val))
+  if (val->entirely_unavailable ())
     {
       if (options->summary && !val_print_scalar_type_p (val->type ()))
        gdb_printf (stream, "...");
@@ -1227,7 +1225,7 @@ static void
 val_print_type_code_flags (struct type *type, struct value *original_value,
                           int embedded_offset, struct ui_file *stream)
 {
-  const gdb_byte *valaddr = (value_contents_for_printing (original_value).data ()
+  const gdb_byte *valaddr = (original_value->contents_for_printing ().data ()
                             + embedded_offset);
   ULONGEST val = unpack_long (type, valaddr);
   int field, nfields = type->num_fields ();
@@ -1301,14 +1299,14 @@ value_print_scalar_formatted (struct value *val,
   /* value_contents_for_printing fetches all VAL's contents.  They are
      needed to check whether VAL is optimized-out or unavailable
      below.  */
-  const gdb_byte *valaddr = value_contents_for_printing (val).data ();
+  const gdb_byte *valaddr = val->contents_for_printing ().data ();
 
   /* A scalar object that does not have all bits available can't be
      printed, because all bits contribute to its representation.  */
-  if (value_bits_any_optimized_out (val, 0,
-                                   TARGET_CHAR_BIT * type->length ()))
+  if (val->bits_any_optimized_out (0,
+                                  TARGET_CHAR_BIT * type->length ()))
     val_print_optimized_out (val, stream);
-  else if (!value_bytes_available (val, 0, type->length ()))
+  else if (!val->bytes_available (0, type->length ()))
     val_print_unavailable (stream);
   else
     print_scalar_formatted (valaddr, type, options, size, stream);
@@ -2009,29 +2007,29 @@ value_print_array_elements (struct value *val, struct ui_file *stream,
       maybe_print_array_index (index_type, i + low_bound,
                               stream, options);
 
-      struct value *element = value_from_component_bitsize (val, elttype,
-                                                           bit_stride * i,
-                                                           bit_stride);
+      struct value *element = val->from_component_bitsize (elttype,
+                                                          bit_stride * i,
+                                                          bit_stride);
       rep1 = i + 1;
       reps = 1;
       /* Only check for reps if repeat_count_threshold is not set to
         UINT_MAX (unlimited).  */
       if (options->repeat_count_threshold < UINT_MAX)
        {
-         bool unavailable = value_entirely_unavailable (element);
-         bool available = value_entirely_available (element);
+         bool unavailable = element->entirely_unavailable ();
+         bool available = element->entirely_available ();
 
          while (rep1 < len)
            {
              struct value *rep_elt
-               = value_from_component_bitsize (val, elttype,
-                                               rep1 * bit_stride,
-                                               bit_stride);
+               = val->from_component_bitsize (elttype,
+                                              rep1 * bit_stride,
+                                              bit_stride);
              bool repeated = ((available
-                               && value_entirely_available (rep_elt)
-                               && value_contents_eq (element, rep_elt))
+                               && rep_elt->entirely_available ()
+                               && element->contents_eq (rep_elt))
                               || (unavailable
-                                  && value_entirely_unavailable (rep_elt)));
+                                  && rep_elt->entirely_unavailable ()));
              if (!repeated)
                break;
              ++reps;
@@ -2358,23 +2356,27 @@ count_next_character (wchar_iterator *iter,
 /* Print the characters in CHARS to the OBSTACK.  QUOTE_CHAR is the quote
    character to use with string output.  WIDTH is the size of the output
    character type.  BYTE_ORDER is the target byte order.  OPTIONS
-   is the user's print options.  */
+   is the user's print options.  *FINISHED is set to 0 if we didn't print
+   all the elements in CHARS.  */
 
 static void
 print_converted_chars_to_obstack (struct obstack *obstack,
                                  const std::vector<converted_character> &chars,
                                  int quote_char, int width,
                                  enum bfd_endian byte_order,
-                                 const struct value_print_options *options)
+                                 const struct value_print_options *options,
+                                 int *finished)
 {
-  unsigned int idx;
+  unsigned int idx, num_elements;
   const converted_character *elem;
   enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
   gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
   bool need_escape = false;
+  const int print_max = options->print_max_chars > 0
+      ? options->print_max_chars : options->print_max;
 
   /* Set the start state.  */
-  idx = 0;
+  idx = num_elements = 0;
   last = state = START;
   elem = NULL;
 
@@ -2402,7 +2404,13 @@ print_converted_chars_to_obstack (struct obstack *obstack,
                obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
              }
            /* Output the character.  */
-           for (j = 0; j < elem->repeat_count; ++j)
+           int repeat_count = elem->repeat_count;
+           if (print_max < repeat_count + num_elements)
+             {
+               repeat_count = print_max - num_elements;
+               *finished = 0;
+             }
+           for (j = 0; j < repeat_count; ++j)
              {
                if (elem->result == wchar_iterate_ok)
                  print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
@@ -2410,6 +2418,7 @@ print_converted_chars_to_obstack (struct obstack *obstack,
                else
                  print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
                               byte_order, obstack, quote_char, &need_escape);
+               num_elements += 1;
              }
          }
          break;
@@ -2441,6 +2450,7 @@ print_converted_chars_to_obstack (struct obstack *obstack,
            obstack_grow_wstr (obstack, LCST ("'"));
            std::string s = string_printf (_(" <repeats %u times>"),
                                           elem->repeat_count);
+           num_elements += elem->repeat_count;
            for (j = 0; s[j]; ++j)
              {
                gdb_wchar_t w = gdb_btowc (s[j]);
@@ -2465,6 +2475,7 @@ print_converted_chars_to_obstack (struct obstack *obstack,
          print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
                       obstack, 0, &need_escape);
          obstack_grow_wstr (obstack, LCST (">"));
+         num_elements += 1;
 
          /* We do not attempt to output anything after this.  */
          state = FINISH;
@@ -2599,7 +2610,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
 
   /* Print the output string to the obstack.  */
   print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
-                                   width, byte_order, options);
+                                   width, byte_order, options, &finished);
 
   if (force_ellipses || !finished)
     obstack_grow_wstr (&wchar_buf, LCST ("..."));
@@ -3100,7 +3111,7 @@ test_print_flags (gdbarch *arch)
   append_flags_type_field (flags_type, 5, 3, field_type, "C");
 
   value *val = value::allocate (flags_type);
-  gdb_byte *contents = value_contents_writeable (val).data ();
+  gdb_byte *contents = val->contents_writeable ().data ();
   store_unsigned_integer (contents, 4, gdbarch_byte_order (arch), 0xaa);
 
   string_file out;