Return std::string from memory_error_message
[deliverable/binutils-gdb.git] / gdb / valprint.c
index aa34b68c83338079458112cc30ac16bfac0467b4..5b02e2facea6f569b29b86b1cee8f7be8089ceda 100644 (file)
@@ -37,6 +37,7 @@
 #include "typeprint.h"
 #include <ctype.h>
 #include <algorithm>
+#include "common/byte-vector.h"
 
 /* Maximum number of wchars returned from wchar_iterate.  */
 #define MAX_WCHARS 4
@@ -781,25 +782,12 @@ generic_val_print_int (struct type *type,
                       struct value *original_value,
                       const struct value_print_options *options)
 {
-  struct gdbarch *gdbarch = get_type_arch (type);
-  int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
-
-  if (options->format || options->output_format)
-    {
-      struct value_print_options opts = *options;
-
-      opts.format = (options->format ? options->format
-                    : options->output_format);
-      val_print_scalar_formatted (type, embedded_offset,
-                                 original_value, &opts, 0, stream);
-    }
-  else
-    {
-      const gdb_byte *valaddr = value_contents_for_printing (original_value);
+  struct value_print_options opts = *options;
 
-      val_print_type_code_int (type, valaddr + embedded_offset * unit_size,
-                              stream);
-    }
+  opts.format = (options->format ? options->format
+                : options->output_format);
+  val_print_scalar_formatted (type, embedded_offset,
+                             original_value, &opts, 0, stream);
 }
 
 /* generic_val_print helper for TYPE_CODE_CHAR.  */
@@ -1237,43 +1225,6 @@ value_print (struct value *val, struct ui_file *stream,
   LA_VALUE_PRINT (val, stream, options);
 }
 
-/* Called by various <lang>_val_print routines to print
-   TYPE_CODE_INT's.  TYPE is the type.  VALADDR is the address of the
-   value.  STREAM is where to print the value.  */
-
-void
-val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
-                        struct ui_file *stream)
-{
-  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
-
-  if (TYPE_LENGTH (type) > sizeof (LONGEST))
-    {
-      LONGEST val;
-
-      if (TYPE_UNSIGNED (type)
-         && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
-                                           byte_order, &val))
-       {
-         print_longest (stream, 'u', 0, val);
-       }
-      else
-       {
-         /* Signed, or we couldn't turn an unsigned value into a
-            LONGEST.  For signed values, one could assume two's
-            complement (a reasonable assumption, I think) and do
-            better than this.  */
-         print_hex_chars (stream, (unsigned char *) valaddr,
-                          TYPE_LENGTH (type), byte_order, false);
-       }
-    }
-  else
-    {
-      print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
-                    unpack_long (type, valaddr));
-    }
-}
-
 static void
 val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
                           struct ui_file *stream)
@@ -1539,9 +1490,6 @@ void
 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
                    unsigned len, enum bfd_endian byte_order, bool zero_pad)
 {
-
-#define BITS_IN_BYTES 8
-
   const gdb_byte *p;
   unsigned int i;
   int b;
@@ -1561,7 +1509,7 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
          /* Every byte has 8 binary characters; peel off
             and print from the MSB end.  */
 
-         for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
+         for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
            {
              if (*p & (mask >> i))
                b = '1';
@@ -1581,7 +1529,7 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
           p >= valaddr;
           p--)
        {
-         for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
+         for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
            {
              if (*p & (mask >> i))
                b = '1';
@@ -1642,20 +1590,26 @@ print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
    */
 #define BITS_IN_OCTAL 3
 #define HIGH_ZERO     0340
-#define LOW_ZERO      0016
+#define LOW_ZERO      0034
 #define CARRY_ZERO    0003
+  static_assert (HIGH_ZERO + LOW_ZERO + CARRY_ZERO == 0xff,
+                "cycle zero constants are wrong");
 #define HIGH_ONE      0200
 #define MID_ONE       0160
 #define LOW_ONE       0016
 #define CARRY_ONE     0001
+  static_assert (HIGH_ONE + MID_ONE + LOW_ONE + CARRY_ONE == 0xff,
+                "cycle one constants are wrong");
 #define HIGH_TWO      0300
 #define MID_TWO       0070
 #define LOW_TWO       0007
+  static_assert (HIGH_TWO + MID_TWO + LOW_TWO == 0xff,
+                "cycle two constants are wrong");
 
   /* For 32 we start in cycle 2, with two bits and one bit carry;
      for 64 in cycle in cycle 1, with one bit and a two bit carry.  */
 
-  cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
+  cycle = (len * HOST_CHAR_BIT) % BITS_IN_OCTAL;
   carry = 0;
 
   fputs_filtered ("0", stream);
@@ -1763,12 +1717,58 @@ print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
 
 }
 
+/* Possibly negate the integer represented by BYTES.  It contains LEN
+   bytes in the specified byte order.  If the integer is negative,
+   copy it into OUT_VEC, negate it, and return true.  Otherwise, do
+   nothing and return false.  */
+
+static bool
+maybe_negate_by_bytes (const gdb_byte *bytes, unsigned len,
+                      enum bfd_endian byte_order,
+                      gdb::byte_vector *out_vec)
+{
+  gdb_byte sign_byte;
+  if (byte_order == BFD_ENDIAN_BIG)
+    sign_byte = bytes[0];
+  else
+    sign_byte = bytes[len - 1];
+  if ((sign_byte & 0x80) == 0)
+    return false;
+
+  out_vec->resize (len);
+
+  /* Compute -x == 1 + ~x.  */
+  if (byte_order == BFD_ENDIAN_LITTLE)
+    {
+      unsigned carry = 1;
+      for (unsigned i = 0; i < len; ++i)
+       {
+         unsigned tem = (0xff & ~bytes[i]) + carry;
+         (*out_vec)[i] = tem & 0xff;
+         carry = tem / 256;
+       }
+    }
+  else
+    {
+      unsigned carry = 1;
+      for (unsigned i = len; i > 0; --i)
+       {
+         unsigned tem = (0xff & ~bytes[i - 1]) + carry;
+         (*out_vec)[i - 1] = tem & 0xff;
+         carry = tem / 256;
+       }
+    }
+
+  return true;
+}
+
 /* VALADDR points to an integer of LEN bytes.
    Print it in decimal on stream or format it in buf.  */
 
 void
 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
-                    unsigned len, enum bfd_endian byte_order)
+                    unsigned len, bool is_signed,
+                    enum bfd_endian byte_order)
 {
 #define TEN             10
 #define CARRY_OUT(  x ) ((x) / TEN)    /* extend char to int */
@@ -1784,6 +1784,14 @@ print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
   int dummy;
   int flip;
 
+  gdb::byte_vector negated_bytes;
+  if (is_signed
+      && maybe_negate_by_bytes (valaddr, len, byte_order, &negated_bytes))
+    {
+      fputs_filtered ("-", stream);
+      valaddr = negated_bytes.data ();
+    }
+
   /* Base-ten number is less than twice as many digits
      as the base 16 number, which is 2 digits per byte.  */
 
@@ -2479,8 +2487,6 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
 {
   enum bfd_endian byte_order
     = gdbarch_byte_order (get_type_arch (type));
-  struct obstack wchar_buf, output;
-  struct cleanup *cleanups;
   gdb_byte *buf;
   int need_escape = 0;
 
@@ -2490,8 +2496,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
   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);
-  cleanups = make_cleanup_obstack_free (&wchar_buf);
+  auto_obstack wchar_buf;
 
   while (1)
     {
@@ -2538,8 +2543,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
     }
 
   /* The output in the host encoding.  */
-  obstack_init (&output);
-  make_cleanup_obstack_free (&output);
+  auto_obstack output;
 
   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
                             (gdb_byte *) obstack_base (&wchar_buf),
@@ -2548,8 +2552,6 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
   obstack_1grow (&output, '\0');
 
   fputs_filtered ((const char *) obstack_base (&output), stream);
-
-  do_cleanups (cleanups);
 }
 
 /* Return the repeat count of the next character/byte in ITER,
@@ -2808,7 +2810,6 @@ generic_printstr (struct ui_file *stream, struct type *type,
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   unsigned int i;
   int width = TYPE_LENGTH (type);
-  struct obstack wchar_buf, output;
   struct cleanup *cleanup;
   int finished = 0;
   struct converted_character *last;
@@ -2880,8 +2881,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
 
   /* WCHAR_BUF is the obstack we use to represent the string in
      wchar_t form.  */
-  obstack_init (&wchar_buf);
-  make_cleanup_obstack_free (&wchar_buf);
+  auto_obstack wchar_buf;
 
   /* Print the output string to the obstack.  */
   print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
@@ -2891,8 +2891,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
     obstack_grow_wstr (&wchar_buf, LCST ("..."));
 
   /* OUTPUT is where we collect `char's for printing.  */
-  obstack_init (&output);
-  make_cleanup_obstack_free (&output);
+  auto_obstack output;
 
   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
                             (gdb_byte *) obstack_base (&wchar_buf),
@@ -2989,13 +2988,10 @@ val_print_string (struct type *elttype, const char *encoding,
 
   if (err != 0)
     {
-      char *str;
-
-      str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
-      make_cleanup (xfree, str);
+      std::string str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
 
       fprintf_filtered (stream, "<error: ");
-      fputs_filtered (str, stream);
+      fputs_filtered (str.c_str (), stream);
       fprintf_filtered (stream, ">");
     }
 
This page took 0.027793 seconds and 4 git commands to generate.