Remove IRIX 5 <sys/proc.h> _KMEMUSER workaround
[deliverable/binutils-gdb.git] / gdb / valprint.c
index 720942b85e74d55561c98dba0ed344d5f26e57f4..ca30a7f945c3dc055964f069ca0f3f68db5baaca 100644 (file)
@@ -36,6 +36,7 @@
 #include "charset.h"
 #include "typeprint.h"
 #include <ctype.h>
+#include <algorithm>
 
 /* Maximum number of wchars returned from wchar_iterate.  */
 #define MAX_WCHARS 4
@@ -303,7 +304,7 @@ val_print_scalar_type_p (struct type *type)
 int
 valprint_check_validity (struct ui_file *stream,
                         struct type *type,
-                        int embedded_offset,
+                        LONGEST embedded_offset,
                         const struct value *val)
 {
   type = check_typedef (type);
@@ -335,8 +336,22 @@ valprint_check_validity (struct ui_file *stream,
       if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
                                        TARGET_CHAR_BIT * TYPE_LENGTH (type)))
        {
-         fputs_filtered (_("<synthetic pointer>"), stream);
-         return 0;
+         const int is_ref = TYPE_CODE (type) == TYPE_CODE_REF;
+         int ref_is_addressable = 0;
+
+         if (is_ref)
+           {
+             const struct value *deref_val = coerce_ref_if_computed (val);
+
+             if (deref_val != NULL)
+               ref_is_addressable = value_lval_const (deref_val) == lval_memory;
+           }
+
+         if (!is_ref || !ref_is_addressable)
+           fputs_filtered (_("<synthetic pointer>"), stream);
+
+         /* C++ references should be valid even if they're synthetic.  */
+         return is_ref;
        }
 
       if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
@@ -407,10 +422,12 @@ print_unpacked_pointer (struct type *type, struct type *elttype,
 
 static void
 generic_val_print_array (struct type *type, const gdb_byte *valaddr,
-                  int embedded_offset, CORE_ADDR address,
-                  struct ui_file *stream, int recurse,
-                  const struct value *original_value,
-                  const struct value_print_options *options)
+                        int embedded_offset, CORE_ADDR address,
+                        struct ui_file *stream, int recurse,
+                        const struct value *original_value,
+                        const struct value_print_options *options,
+                        const struct
+                            generic_val_print_decorations *decorations)
 {
   struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
   struct type *elttype = check_typedef (unresolved_elttype);
@@ -427,11 +444,11 @@ generic_val_print_array (struct type *type, const gdb_byte *valaddr,
          print_spaces_filtered (2 + 2 * recurse, stream);
        }
 
-      fprintf_filtered (stream, "{");
+      fputs_filtered (decorations->array_start, stream);
       val_print_array_elements (type, valaddr, embedded_offset,
                                address, stream,
                                recurse, original_value, options, 0);
-      fprintf_filtered (stream, "}");
+      fputs_filtered (decorations->array_end, stream);
     }
   else
     {
@@ -482,6 +499,42 @@ generic_val_print_memberptr (struct type *type, const gdb_byte *valaddr,
                              original_value, options, 0, stream);
 }
 
+/* Print '@' followed by the address contained in ADDRESS_BUFFER.  */
+
+static void
+print_ref_address (struct type *type, const gdb_byte *address_buffer,
+                 int embedded_offset, struct ui_file *stream)
+{
+  struct gdbarch *gdbarch = get_type_arch (type);
+
+  if (address_buffer != NULL)
+    {
+      CORE_ADDR address
+       = extract_typed_address (address_buffer + embedded_offset, type);
+
+      fprintf_filtered (stream, "@");
+      fputs_filtered (paddress (gdbarch, address), stream);
+    }
+  /* Else: we have a non-addressable value, such as a DW_AT_const_value.  */
+}
+
+/* If VAL is addressable, return the value contents buffer of a value that
+   represents a pointer to VAL.  Otherwise return NULL.  */
+
+static const gdb_byte *
+get_value_addr_contents (struct value *deref_val)
+{
+  gdb_assert (deref_val != NULL);
+
+  if (value_lval_const (deref_val) == lval_memory)
+    return value_contents_for_printing_const (value_addr (deref_val));
+  else
+    {
+      /* We have a non-addressable value, such as a DW_AT_const_value.  */
+      return NULL;
+    }
+}
+
 /* generic_val_print helper for TYPE_CODE_REF.  */
 
 static void
@@ -490,41 +543,58 @@ generic_val_print_ref (struct type *type, const gdb_byte *valaddr,
                       const struct value *original_value,
                       const struct value_print_options *options)
 {
-  struct gdbarch *gdbarch = get_type_arch (type);
   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (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 (type));
+  const int must_coerce_ref = ((options->addressprint && value_is_synthetic)
+                              || options->deref_ref);
+  const int type_is_defined = TYPE_CODE (elttype) != TYPE_CODE_UNDEF;
+
+  if (must_coerce_ref && type_is_defined)
+    {
+      deref_val = coerce_ref_if_computed (original_value);
+
+      if (deref_val != NULL)
+       {
+         /* More complicated computed references are not supported.  */
+         gdb_assert (embedded_offset == 0);
+       }
+      else
+       deref_val = value_at (TYPE_TARGET_TYPE (type),
+                             unpack_pointer (type, valaddr + embedded_offset));
+    }
+  /* Else, original_value isn't a synthetic reference or we don't have to print
+     the reference's contents.
+
+     Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
+     cause original_value to be a not_lval instead of an lval_computed,
+     which will make value_bits_synthetic_pointer return false.
+     This happens because if options->objectprint is true, c_value_print will
+     overwrite original_value's contents with the result of coercing
+     the reference through value_addr, and then set its type back to
+     TYPE_CODE_REF.  In that case we don't have to coerce the reference again;
+     we can simply treat it as non-synthetic and move on.  */
 
   if (options->addressprint)
     {
-      CORE_ADDR addr
-       = extract_typed_address (valaddr + embedded_offset, type);
+      const gdb_byte *address = (value_is_synthetic && type_is_defined
+                                ? get_value_addr_contents (deref_val)
+                                : valaddr);
+
+      print_ref_address (type, address, embedded_offset, stream);
 
-      fprintf_filtered (stream, "@");
-      fputs_filtered (paddress (gdbarch, addr), stream);
       if (options->deref_ref)
        fputs_filtered (": ", stream);
     }
-  /* De-reference the reference.  */
+
   if (options->deref_ref)
     {
-      if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
-       {
-         struct value *deref_val;
-
-         deref_val = coerce_ref_if_computed (original_value);
-         if (deref_val != NULL)
-           {
-             /* More complicated computed references are not supported.  */
-             gdb_assert (embedded_offset == 0);
-           }
-         else
-           deref_val = value_at (TYPE_TARGET_TYPE (type),
-                                 unpack_pointer (type,
-                                                 (valaddr
-                                                  + embedded_offset)));
-
-         common_val_print (deref_val, stream, recurse, options,
-                           current_language);
-       }
+      if (type_is_defined)
+       common_val_print (deref_val, stream, recurse, options,
+                         current_language);
       else
        fputs_filtered ("???", stream);
     }
@@ -851,7 +921,7 @@ generic_val_print (struct type *type, const gdb_byte *valaddr,
     {
     case TYPE_CODE_ARRAY:
       generic_val_print_array (type, valaddr, embedded_offset, address, stream,
-                              recurse, original_value, options);
+                              recurse, original_value, options, decorations);
       break;
 
     case TYPE_CODE_MEMBERPTR:
@@ -930,10 +1000,9 @@ generic_val_print (struct type *type, const gdb_byte *valaddr,
       break;
 
     case TYPE_CODE_UNDEF:
-      /* This happens (without TYPE_FLAG_STUB set) on systems which
-         don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
-         "struct foo *bar" and no complete type for struct foo in that
-         file.  */
+      /* This happens (without TYPE_STUB set) on systems which don't use
+         dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
+         and no complete type for struct foo in that file.  */
       fprintf_filtered (stream, _("<incomplete type>"));
       break;
 
@@ -973,7 +1042,7 @@ generic_val_print (struct type *type, const gdb_byte *valaddr,
    RECURSE.  */
 
 void
-val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
+val_print (struct type *type, const gdb_byte *valaddr, LONGEST embedded_offset,
           CORE_ADDR address, struct ui_file *stream, int recurse,
           const struct value *val,
           const struct value_print_options *options,
@@ -1234,7 +1303,7 @@ val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
 
 void
 val_print_scalar_formatted (struct type *type,
-                           const gdb_byte *valaddr, int embedded_offset,
+                           const gdb_byte *valaddr, LONGEST embedded_offset,
                            const struct value *val,
                            const struct value_print_options *options,
                            int size,
@@ -1896,7 +1965,7 @@ maybe_print_array_index (struct type *index_type, LONGEST index,
 
 void
 val_print_array_elements (struct type *type,
-                         const gdb_byte *valaddr, int embedded_offset,
+                         const gdb_byte *valaddr, LONGEST embedded_offset,
                          CORE_ADDR address, struct ui_file *stream,
                          int recurse,
                          const struct value *val,
@@ -2110,7 +2179,7 @@ read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
     {
       /* We want fetchlimit chars, so we might as well read them all in
         one operation.  */
-      unsigned int fetchlen = min (len, fetchlimit);
+      unsigned int fetchlen = std::min ((unsigned) len, fetchlimit);
 
       *buffer = (gdb_byte *) xmalloc (fetchlen * width);
       bufptr = *buffer;
@@ -2134,12 +2203,12 @@ read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
         So we choose the minimum of 8 and fetchlimit.  We used to use 200
         instead of 8 but 200 is way too big for remote debugging over a
          serial line.  */
-      chunksize = min (8, fetchlimit);
+      chunksize = std::min (8u, fetchlimit);
 
       do
        {
          QUIT;
-         nfetch = min (chunksize, fetchlimit - bufsize);
+         nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize);
 
          if (*buffer == NULL)
            *buffer = (gdb_byte *) xmalloc (nfetch * width);
@@ -2335,19 +2404,16 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
   struct obstack wchar_buf, output;
   struct cleanup *cleanups;
   gdb_byte *buf;
-  struct wchar_iterator *iter;
   int need_escape = 0;
 
   buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
   pack_long (buf, type, c);
 
-  iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
-                             encoding, TYPE_LENGTH (type));
-  cleanups = make_cleanup_wchar_iterator (iter);
+  wchar_iterator iter (buf, TYPE_LENGTH (type), encoding, TYPE_LENGTH (type));
 
   /* This holds the printable form of the wchar_t data.  */
   obstack_init (&wchar_buf);
-  make_cleanup_obstack_free (&wchar_buf);
+  cleanups = make_cleanup_obstack_free (&wchar_buf);
 
   while (1)
     {
@@ -2358,7 +2424,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
       int print_escape = 1;
       enum wchar_iterate_result result;
 
-      num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
+      num_chars = iter.iterate (&result, &chars, &buf, &buflen);
       if (num_chars < 0)
        break;
       if (num_chars > 0)
@@ -2412,7 +2478,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
    storing the result in VEC.  */
 
 static int
-count_next_character (struct wchar_iterator *iter,
+count_next_character (wchar_iterator *iter,
                      VEC (converted_character_d) **vec)
 {
   struct converted_character *current;
@@ -2423,7 +2489,7 @@ count_next_character (struct wchar_iterator *iter,
       gdb_wchar_t *chars;
 
       tmp.num_chars
-       = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
+       = iter->iterate (&tmp.result, &chars, &tmp.buf, &tmp.buflen);
       if (tmp.num_chars > 0)
        {
          gdb_assert (tmp.num_chars < MAX_WCHARS);
@@ -2452,8 +2518,7 @@ count_next_character (struct wchar_iterator *iter,
       while (1)
        {
          /* Get the next character.  */
-         d.num_chars
-           = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
+         d.num_chars = iter->iterate (&d.result, &chars, &d.buf, &d.buflen);
 
          /* If a character was successfully converted, save the character
             into the converted character.  */
@@ -2667,7 +2732,6 @@ generic_printstr (struct ui_file *stream, struct type *type,
   int width = TYPE_LENGTH (type);
   struct obstack wchar_buf, output;
   struct cleanup *cleanup;
-  struct wchar_iterator *iter;
   int finished = 0;
   struct converted_character *last;
   VEC (converted_character_d) *converted_chars;
@@ -2702,10 +2766,10 @@ generic_printstr (struct ui_file *stream, struct type *type,
     }
 
   /* Arrange to iterate over the characters, in wchar_t form.  */
-  iter = make_wchar_iterator (string, length * width, encoding, width);
-  cleanup = make_cleanup_wchar_iterator (iter);
+  wchar_iterator iter (string, length * width, encoding, width);
   converted_chars = NULL;
-  make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
+  cleanup = make_cleanup (VEC_cleanup (converted_character_d),
+                         &converted_chars);
 
   /* Convert characters until the string is over or the maximum
      number of printed characters has been reached.  */
@@ -2717,7 +2781,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
       QUIT;
 
       /* Grab the next character and repeat count.  */
-      r = count_next_character (iter, &converted_chars);
+      r = count_next_character (&iter, &converted_chars);
 
       /* If less than zero, the end of the input string was reached.  */
       if (r < 0)
@@ -2796,8 +2860,8 @@ val_print_string (struct type *elttype, const char *encoding,
      because finding the null byte (or available memory) is what actually
      limits the fetch.  */
 
-  fetchlimit = (len == -1 ? options->print_max : min (len,
-                                                     options->print_max));
+  fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len,
+                                                          options->print_max));
 
   err = read_string (addr, len, width, fetchlimit, byte_order,
                     &buffer, &bytes_read);
This page took 0.027892 seconds and 4 git commands to generate.