]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/valprint.c
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / gdb / valprint.c
index 507aeb587b8324fc0ca8c3434d7264f2d85989c7..b8fdcaabbd12e44594af471fe46037cabcd54cc6 100644 (file)
@@ -1,6 +1,6 @@
 /* Print values for GDB, the GNU debugger.
 
-   Copyright (C) 1986, 1988-2012 Free Software Foundation, Inc.
+   Copyright (C) 1986-2013 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 
 #include <errno.h>
 
+/* Maximum number of wchars returned from wchar_iterate.  */
+#define MAX_WCHARS 4
+
+/* A convenience macro to compute the size of a wchar_t buffer containing X
+   characters.  */
+#define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
+
+/* Character buffer size saved while iterating over wchars.  */
+#define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
+
+/* A structure to encapsulate state information from iterated
+   character conversions.  */
+struct converted_character
+{
+  /* The number of characters converted.  */
+  int num_chars;
+
+  /* The result of the conversion.  See charset.h for more.  */
+  enum wchar_iterate_result result;
+
+  /* The (saved) converted character(s).  */
+  gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
+
+  /* The first converted target byte.  */
+  const gdb_byte *buf;
+
+  /* The number of bytes converted.  */
+  size_t buflen;
+
+  /* How many times this character(s) is repeated.  */
+  int repeat_count;
+};
+
+typedef struct converted_character converted_character_d;
+DEF_VEC_O (converted_character_d);
+
+
 /* Prototypes for local functions */
 
 static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
@@ -85,7 +122,8 @@ struct value_print_options user_print_options =
   1,                           /* static_field_print */
   1,                           /* pascal_static_field_print */
   0,                           /* raw */
-  0                            /* summary */
+  0,                           /* summary */
+  1                            /* symbol_print */
 };
 
 /* Initialize *OPTS to be a copy of the user print options.  */
@@ -219,6 +257,16 @@ show_addressprint (struct ui_file *file, int from_tty,
 {
   fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
 }
+
+static void
+show_symbol_print (struct ui_file *file, int from_tty,
+                  struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file,
+                   _("Printing of symbols when printing pointers is %s.\n"),
+                   value);
+}
+
 \f
 
 /* A helper function for val_print.  When printing in "summary" mode,
@@ -241,7 +289,6 @@ scalar_type_p (struct type *type)
     case TYPE_CODE_UNION:
     case TYPE_CODE_SET:
     case TYPE_CODE_STRING:
-    case TYPE_CODE_BITSTRING:
       return 0;
     default:
       return 1;
@@ -329,7 +376,6 @@ generic_val_print (struct type *type, const gdb_byte *valaddr,
   unsigned len;
   struct type *elttype, *unresolved_elttype;
   struct type *unresolved_type = type;
-  unsigned eltlen;
   LONGEST val;
   CORE_ADDR addr;
 
@@ -388,7 +434,9 @@ generic_val_print (struct type *type, const gdb_byte *valaddr,
              return;
            }
 
-         if (options->addressprint)
+         if (options->symbol_print)
+           print_address_demangle (options, gdbarch, addr, stream, demangle);
+         else if (options->addressprint)
            fputs_filtered (paddress (gdbarch, addr), stream);
        }
       break;
@@ -1926,9 +1974,10 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig,
                /* If the value fits in 3 octal digits, print it that
                   way.  Otherwise, print it as a hex escape.  */
                if (value <= 0777)
-                 sprintf (octal, "\\%.3o", (int) (value & 0777));
+                 xsnprintf (octal, sizeof (octal), "\\%.3o",
+                            (int) (value & 0777));
                else
-                 sprintf (octal, "\\x%lx", (long) value);
+                 xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
                append_string_as_wide (octal, output);
              }
            /* If we somehow have extra bytes, print them now.  */
@@ -1936,7 +1985,7 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig,
              {
                char octal[5];
 
-               sprintf (octal, "\\%.3o", orig[i] & 0xff);
+               xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
                append_string_as_wide (octal, output);
                ++i;
              }
@@ -2026,7 +2075,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
                             obstack_base (&wchar_buf),
                             obstack_object_size (&wchar_buf),
-                            1, &output, translit_char);
+                            sizeof (gdb_wchar_t), &output, translit_char);
   obstack_1grow (&output, '\0');
 
   fputs_filtered (obstack_base (&output), stream);
@@ -2034,6 +2083,253 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream,
   do_cleanups (cleanups);
 }
 
+/* Return the repeat count of the next character/byte in ITER,
+   storing the result in VEC.  */
+
+static int
+count_next_character (struct wchar_iterator *iter,
+                     VEC (converted_character_d) **vec)
+{
+  struct converted_character *current;
+
+  if (VEC_empty (converted_character_d, *vec))
+    {
+      struct converted_character tmp;
+      gdb_wchar_t *chars;
+
+      tmp.num_chars
+       = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
+      if (tmp.num_chars > 0)
+       {
+         gdb_assert (tmp.num_chars < MAX_WCHARS);
+         memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
+       }
+      VEC_safe_push (converted_character_d, *vec, &tmp);
+    }
+
+  current = VEC_last (converted_character_d, *vec);
+
+  /* Count repeated characters or bytes.  */
+  current->repeat_count = 1;
+  if (current->num_chars == -1)
+    {
+      /* EOF  */
+      return -1;
+    }
+  else
+    {
+      gdb_wchar_t *chars;
+      struct converted_character d;
+      int repeat;
+
+      d.repeat_count = 0;
+
+      while (1)
+       {
+         /* Get the next character.  */
+         d.num_chars
+           = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
+
+         /* If a character was successfully converted, save the character
+            into the converted character.  */
+         if (d.num_chars > 0)
+           {
+             gdb_assert (d.num_chars < MAX_WCHARS);
+             memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
+           }
+
+         /* Determine if the current character is the same as this
+            new character.  */
+         if (d.num_chars == current->num_chars && d.result == current->result)
+           {
+             /* There are two cases to consider:
+
+                1) Equality of converted character (num_chars > 0)
+                2) Equality of non-converted character (num_chars == 0)  */
+             if ((current->num_chars > 0
+                  && memcmp (current->chars, d.chars,
+                             WCHAR_BUFLEN (current->num_chars)) == 0)
+                 || (current->num_chars == 0
+                     && current->buflen == d.buflen
+                     && memcmp (current->buf, d.buf, current->buflen) == 0))
+               ++current->repeat_count;
+             else
+               break;
+           }
+         else
+           break;
+       }
+
+      /* Push this next converted character onto the result vector.  */
+      repeat = current->repeat_count;
+      VEC_safe_push (converted_character_d, *vec, &d);
+      return repeat;
+    }
+}
+
+/* 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 the target byte order.  OPTIONS
+   is the user's print options.  */
+
+static void
+print_converted_chars_to_obstack (struct obstack *obstack,
+                                 VEC (converted_character_d) *chars,
+                                 int quote_char, int width,
+                                 enum bfd_endian byte_order,
+                                 const struct value_print_options *options)
+{
+  unsigned int idx;
+  struct converted_character *elem;
+  enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
+  gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
+  int need_escape = 0;
+
+  /* Set the start state.  */
+  idx = 0;
+  last = state = START;
+  elem = NULL;
+
+  while (1)
+    {
+      switch (state)
+       {
+       case START:
+         /* Nothing to do.  */
+         break;
+
+       case SINGLE:
+         {
+           int j;
+
+           /* We are outputting a single character
+              (< options->repeat_count_threshold).  */
+
+           if (last != SINGLE)
+             {
+               /* We were outputting some other type of content, so we
+                  must output and a comma and a quote.  */
+               if (last != START)
+                 obstack_grow_wstr (obstack, LCST (", "));
+               if (options->inspect_it)
+                 obstack_grow_wstr (obstack, LCST ("\\"));
+               obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
+             }
+           /* Output the character.  */
+           for (j = 0; j < elem->repeat_count; ++j)
+             {
+               if (elem->result == wchar_iterate_ok)
+                 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
+                              byte_order, obstack, quote_char, &need_escape);
+               else
+                 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
+                              byte_order, obstack, quote_char, &need_escape);
+             }
+         }
+         break;
+
+       case REPEAT:
+         {
+           int j;
+           char *s;
+
+           /* We are outputting a character with a repeat count
+              greater than options->repeat_count_threshold.  */
+
+           if (last == SINGLE)
+             {
+               /* We were outputting a single string.  Terminate the
+                  string.  */
+               if (options->inspect_it)
+                 obstack_grow_wstr (obstack, LCST ("\\"));
+               obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
+             }
+           if (last != START)
+             obstack_grow_wstr (obstack, LCST (", "));
+
+           /* Output the character and repeat string.  */
+           obstack_grow_wstr (obstack, LCST ("'"));
+           if (elem->result == wchar_iterate_ok)
+             print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
+                          byte_order, obstack, quote_char, &need_escape);
+           else
+             print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
+                          byte_order, obstack, quote_char, &need_escape);
+           obstack_grow_wstr (obstack, LCST ("'"));
+           s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
+           for (j = 0; s[j]; ++j)
+             {
+               gdb_wchar_t w = gdb_btowc (s[j]);
+               obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
+             }
+           xfree (s);
+         }
+         break;
+
+       case INCOMPLETE:
+         /* We are outputting an incomplete sequence.  */
+         if (last == SINGLE)
+           {
+             /* If we were outputting a string of SINGLE characters,
+                terminate the quote.  */
+             if (options->inspect_it)
+               obstack_grow_wstr (obstack, LCST ("\\"));
+             obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
+           }
+         if (last != START)
+           obstack_grow_wstr (obstack, LCST (", "));
+
+         /* Output the incomplete sequence string.  */
+         obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
+         print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
+                      obstack, 0, &need_escape);
+         obstack_grow_wstr (obstack, LCST (">"));
+
+         /* We do not attempt to outupt anything after this.  */
+         state = FINISH;
+         break;
+
+       case FINISH:
+         /* All done.  If we were outputting a string of SINGLE
+            characters, the string must be terminated.  Otherwise,
+            REPEAT and INCOMPLETE are always left properly terminated.  */
+         if (last == SINGLE)
+           {
+             if (options->inspect_it)
+               obstack_grow_wstr (obstack, LCST ("\\"));
+             obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
+           }
+
+         return;
+       }
+
+      /* Get the next element and state.  */
+      last = state;
+      if (state != FINISH)
+       {
+         elem = VEC_index (converted_character_d, chars, idx++);
+         switch (elem->result)
+           {
+           case wchar_iterate_ok:
+           case wchar_iterate_invalid:
+             if (elem->repeat_count > options->repeat_count_threshold)
+               state = REPEAT;
+             else
+               state = SINGLE;
+             break;
+
+           case wchar_iterate_incomplete:
+             state = INCOMPLETE;
+             break;
+
+           case wchar_iterate_eof:
+             state = FINISH;
+             break;
+           }
+       }
+    }
+}
+
 /* Print the character string STRING, printing at most LENGTH
    characters.  LENGTH is -1 if the string is nul terminated.  TYPE is
    the type of each character.  OPTIONS holds the printing options;
@@ -2053,16 +2349,13 @@ generic_printstr (struct ui_file *stream, struct type *type,
 {
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   unsigned int i;
-  unsigned int things_printed = 0;
-  int in_quotes = 0;
-  int need_comma = 0;
   int width = TYPE_LENGTH (type);
   struct obstack wchar_buf, output;
   struct cleanup *cleanup;
   struct wchar_iterator *iter;
   int finished = 0;
-  int need_escape = 0;
-  gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
+  struct converted_character *last;
+  VEC (converted_character_d) *converted_chars;
 
   if (length == -1)
     {
@@ -2096,166 +2389,46 @@ 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);
+  converted_chars = NULL;
+  make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
 
-  /* 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);
-
-  while (!finished && things_printed < options->print_max)
+  /* Convert characters until the string is over or the maximum
+     number of printed characters has been reached.  */
+  i = 0;
+  while (i < options->print_max)
     {
-      int num_chars;
-      enum wchar_iterate_result result;
-      gdb_wchar_t *chars;
-      const gdb_byte *buf;
-      size_t buflen;
+      int r;
 
       QUIT;
 
-      if (need_comma)
-       {
-         obstack_grow_wstr (&wchar_buf, LCST (", "));
-         need_comma = 0;
-       }
-
-      num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
-      /* We only look at repetitions when we were able to convert a
-        single character in isolation.  This makes the code simpler
-        and probably does the sensible thing in the majority of
-        cases.  */
-      while (num_chars == 1 && things_printed < options->print_max)
-       {
-         /* Count the number of repetitions.  */
-         unsigned int reps = 0;
-         gdb_wchar_t current_char = chars[0];
-         const gdb_byte *orig_buf = buf;
-         int orig_len = buflen;
-
-         if (need_comma)
-           {
-             obstack_grow_wstr (&wchar_buf, LCST (", "));
-             need_comma = 0;
-           }
-
-         while (num_chars == 1 && current_char == chars[0])
-           {
-             num_chars = wchar_iterate (iter, &result, &chars,
-                                        &buf, &buflen);
-             ++reps;
-           }
+      /* Grab the next character and repeat count.  */
+      r = count_next_character (iter, &converted_chars);
 
-         /* Emit CURRENT_CHAR according to the repetition count and
-            options.  */
-         if (reps > options->repeat_count_threshold)
-           {
-             if (in_quotes)
-               {
-                 if (options->inspect_it)
-                   obstack_grow_wstr (&wchar_buf, LCST ("\\"));
-                 obstack_grow (&wchar_buf, &wide_quote_char,
-                               sizeof (gdb_wchar_t));
-                 obstack_grow_wstr (&wchar_buf, LCST (", "));
-                 in_quotes = 0;
-               }
-             obstack_grow_wstr (&wchar_buf, LCST ("'"));
-             need_escape = 0;
-             print_wchar (current_char, orig_buf, orig_len, width,
-                          byte_order, &wchar_buf, '\'', &need_escape);
-             obstack_grow_wstr (&wchar_buf, LCST ("'"));
-             {
-               /* Painful gyrations.  */
-               int j;
-               char *s = xstrprintf (_(" <repeats %u times>"), reps);
-
-               for (j = 0; s[j]; ++j)
-                 {
-                   gdb_wchar_t w = gdb_btowc (s[j]);
-                   obstack_grow (&wchar_buf, &w, sizeof (gdb_wchar_t));
-                 }
-               xfree (s);
-             }
-             things_printed += options->repeat_count_threshold;
-             need_comma = 1;
-           }
-         else
-           {
-             /* Saw the character one or more times, but fewer than
-                the repetition threshold.  */
-             if (!in_quotes)
-               {
-                 if (options->inspect_it)
-                   obstack_grow_wstr (&wchar_buf, LCST ("\\"));
-                 obstack_grow (&wchar_buf, &wide_quote_char,
-                               sizeof (gdb_wchar_t));
-                 in_quotes = 1;
-                 need_escape = 0;
-               }
+      /* If less than zero, the end of the input string was reached.  */
+      if (r < 0)
+       break;
 
-             while (reps-- > 0)
-               {
-                 print_wchar (current_char, orig_buf,
-                              orig_len, width,
-                              byte_order, &wchar_buf,
-                              quote_char, &need_escape);
-                 ++things_printed;
-               }
-           }
-       }
+      /* Otherwise, add the count to the total print count and get
+        the next character.  */
+      i += r;
+    }
 
-      /* NUM_CHARS and the other outputs from wchar_iterate are valid
-        here regardless of which branch was taken above.  */
-      if (num_chars < 0)
-       {
-         /* Hit EOF.  */
-         finished = 1;
-         break;
-       }
+  /* Get the last element and determine if the entire string was
+     processed.  */
+  last = VEC_last (converted_character_d, converted_chars);
+  finished = (last->result == wchar_iterate_eof);
 
-      switch (result)
-       {
-       case wchar_iterate_invalid:
-         if (!in_quotes)
-           {
-             if (options->inspect_it)
-               obstack_grow_wstr (&wchar_buf, LCST ("\\"));
-             obstack_grow (&wchar_buf, &wide_quote_char,
-                           sizeof (gdb_wchar_t));
-             in_quotes = 1;
-           }
-         need_escape = 0;
-         print_wchar (gdb_WEOF, buf, buflen, width, byte_order,
-                      &wchar_buf, quote_char, &need_escape);
-         break;
+  /* Ensure that CONVERTED_CHARS is terminated.  */
+  last->result = wchar_iterate_eof;
 
-       case wchar_iterate_incomplete:
-         if (in_quotes)
-           {
-             if (options->inspect_it)
-               obstack_grow_wstr (&wchar_buf, LCST ("\\"));
-             obstack_grow (&wchar_buf, &wide_quote_char,
-                           sizeof (gdb_wchar_t));
-             obstack_grow_wstr (&wchar_buf, LCST (","));
-             in_quotes = 0;
-           }
-         obstack_grow_wstr (&wchar_buf,
-                            LCST (" <incomplete sequence "));
-         print_wchar (gdb_WEOF, buf, buflen, width,
-                      byte_order, &wchar_buf,
-                      0, &need_escape);
-         obstack_grow_wstr (&wchar_buf, LCST (">"));
-         finished = 1;
-         break;
-       }
-    }
+  /* 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);
 
-  /* Terminate the quotes if necessary.  */
-  if (in_quotes)
-    {
-      if (options->inspect_it)
-       obstack_grow_wstr (&wchar_buf, LCST ("\\"));
-      obstack_grow (&wchar_buf, &wide_quote_char,
-                   sizeof (gdb_wchar_t));
-    }
+  /* Print the output string to the obstack.  */
+  print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
+                                   width, byte_order, options);
 
   if (force_ellipses || !finished)
     obstack_grow_wstr (&wchar_buf, LCST ("..."));
@@ -2267,7 +2440,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
   convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
                             obstack_base (&wchar_buf),
                             obstack_object_size (&wchar_buf),
-                            1, &output, translit_char);
+                            sizeof (gdb_wchar_t), &output, translit_char);
   obstack_1grow (&output, '\0');
 
   fputs_filtered (obstack_base (&output), stream);
@@ -2351,10 +2524,6 @@ val_print_string (struct type *elttype, const char *encoding,
      and then the error message.  */
   if (errcode == 0 || bytes_read > 0)
     {
-      if (options->addressprint)
-       {
-         fputs_filtered (" ", stream);
-       }
       LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
                       encoding, force_ellipsis, options);
     }
@@ -2363,13 +2532,13 @@ val_print_string (struct type *elttype, const char *encoding,
     {
       if (errcode == EIO)
        {
-         fprintf_filtered (stream, " <Address ");
+         fprintf_filtered (stream, "<Address ");
          fputs_filtered (paddress (gdbarch, addr), stream);
          fprintf_filtered (stream, " out of bounds>");
        }
       else
        {
-         fprintf_filtered (stream, " <Error reading address ");
+         fprintf_filtered (stream, "<Error reading address ");
          fputs_filtered (paddress (gdbarch, addr), stream);
          fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
        }
@@ -2604,6 +2773,14 @@ Show printing of addresses."), NULL,
                           show_addressprint,
                           &setprintlist, &showprintlist);
 
+  add_setshow_boolean_cmd ("symbol", class_support,
+                          &user_print_options.symbol_print, _("\
+Set printing of symbol names when printing pointers."), _("\
+Show printing of symbol names when printing pointers."),
+                          NULL, NULL,
+                          show_symbol_print,
+                          &setprintlist, &showprintlist);
+
   add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
                             _("\
 Set default input radix for entering numbers."), _("\