2010-06-11 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / c-lang.c
index 4ba81ba86aa637bfa643d0da31766e4dce44da65..40c417256bf9f475b64f17079cdf9b0433aae647 100644 (file)
@@ -1,7 +1,7 @@
 /* C language support routines for GDB, the GNU debugger.
 
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003,
-   2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
+   2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -43,26 +43,26 @@ extern void _initialize_c_language (void);
 
 static const char *
 charset_for_string_type (enum c_string_type str_type,
-                        enum bfd_endian byte_order)
+                        struct gdbarch *gdbarch)
 {
   switch (str_type & ~C_CHAR)
     {
     case C_STRING:
-      return target_charset ();
+      return target_charset (gdbarch);
     case C_WIDE_STRING:
-      return target_wide_charset (byte_order);
+      return target_wide_charset (gdbarch);
     case C_STRING_16:
-      /* FIXME: UCS-2 is not always correct.  */
-      if (byte_order == BFD_ENDIAN_BIG)
-       return "UCS-2BE";
+      /* FIXME: UTF-16 is not always correct.  */
+      if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
+       return "UTF-16BE";
       else
-       return "UCS-2LE";
+       return "UTF-16LE";
     case C_STRING_32:
-      /* FIXME: UCS-4 is not always correct.  */
-      if (byte_order == BFD_ENDIAN_BIG)
-       return "UCS-4BE";
+      /* FIXME: UTF-32 is not always correct.  */
+      if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
+       return "UTF-32BE";
       else
-       return "UCS-4LE";
+       return "UTF-32LE";
     }
   internal_error (__FILE__, __LINE__, "unhandled c_string_type");
 }
@@ -73,10 +73,9 @@ charset_for_string_type (enum c_string_type str_type,
    characters of this type in target BYTE_ORDER to the host character set.  */
 
 static enum c_string_type
-classify_type (struct type *elttype, enum bfd_endian byte_order,
+classify_type (struct type *elttype, struct gdbarch *gdbarch,
               const char **encoding)
 {
-  struct type *saved_type;
   enum c_string_type result;
 
   /* We loop because ELTTYPE may be a typedef, and we want to
@@ -134,7 +133,7 @@ classify_type (struct type *elttype, enum bfd_endian byte_order,
 
  done:
   if (encoding)
-    *encoding = charset_for_string_type (result, byte_order);
+    *encoding = charset_for_string_type (result, gdbarch);
 
   return result;
 }
@@ -180,6 +179,7 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, int orig_len,
             int quoter, int *need_escapep)
 {
   int need_escape = *need_escapep;
+
   *need_escapep = 0;
   if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w)
                                            && w != LCST ('8')
@@ -224,6 +224,7 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, int orig_len,
              {
                char octal[30];
                ULONGEST value;
+
                value = extract_unsigned_integer (&orig[i], width, byte_order);
                /* If the value fits in 3 octal digits, print it that
                   way.  Otherwise, print it as a hex escape.  */
@@ -237,6 +238,7 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, int orig_len,
            while (i < orig_len)
              {
                char octal[5];
+
                sprintf (octal, "\\%.3o", orig[i] & 0xff);
                append_string_as_wide (octal, output);
                ++i;
@@ -253,8 +255,9 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, int orig_len,
    string whose delimiter is QUOTER.  Note that that format for printing
    characters and strings is language specific. */
 
-static void
-c_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
+void
+c_emit_char (int c, struct type *type,
+            struct ui_file *stream, int quoter)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
   struct obstack wchar_buf, output;
@@ -264,7 +267,7 @@ c_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
   struct wchar_iterator *iter;
   int need_escape = 0;
 
-  classify_type (type, byte_order, &encoding);
+  classify_type (type, get_type_arch (type), &encoding);
 
   buf = alloca (TYPE_LENGTH (type));
   pack_long (buf, type, c);
@@ -340,7 +343,7 @@ c_printchar (int c, struct type *type, struct ui_file *stream)
 {
   enum c_string_type str_type;
 
-  str_type = classify_type (type, BFD_ENDIAN_UNKNOWN, NULL);
+  str_type = classify_type (type, get_type_arch (type), NULL);
   switch (str_type)
     {
     case C_CHAR:
@@ -369,7 +372,7 @@ c_printchar (int c, struct type *type, struct ui_file *stream)
 
 void
 c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
-           unsigned int length, int force_ellipses,
+           unsigned int length, const char *user_encoding, int force_ellipses,
            const struct value_print_options *options)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
@@ -381,11 +384,25 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
   struct obstack wchar_buf, output;
   struct cleanup *cleanup;
   enum c_string_type str_type;
+  const char *type_encoding;
   const char *encoding;
   struct wchar_iterator *iter;
   int finished = 0;
   int need_escape = 0;
 
+  if (length == -1)
+    {
+      unsigned long current_char = 1;
+
+      for (i = 0; current_char; ++i)
+       {
+         QUIT;
+         current_char = extract_unsigned_integer (string + i * width,
+                                                  width, byte_order);
+       }
+      length = i;
+    }
+
   /* If the string was not truncated due to `set print elements', and
      the last byte of it is a null, we don't print that, in traditional C
      style.  */
@@ -395,7 +412,8 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
                                    width, byte_order) == 0))
     length--;
 
-  str_type = classify_type (type, byte_order, &encoding) & ~C_CHAR;
+  str_type = (classify_type (type, get_type_arch (type), &type_encoding)
+             & ~C_CHAR);
   switch (str_type)
     {
     case C_STRING:
@@ -411,24 +429,14 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
       break;
     }
 
+  encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
+
   if (length == 0)
     {
       fputs_filtered ("\"\"", stream);
       return;
     }
 
-  if (length == -1)
-    {
-      unsigned long current_char = 1;
-      for (i = 0; current_char; ++i)
-       {
-         QUIT;
-         current_char = extract_unsigned_integer (string + i * width,
-                                                  width, byte_order);
-       }
-      length = i;
-    }
-
   /* 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);
@@ -459,7 +467,7 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
         single character in isolation.  This makes the code simpler
         and probably does the sensible thing in the majority of
         cases.  */
-      while (num_chars == 1)
+      while (num_chars == 1 && things_printed < options->print_max)
        {
          /* Count the number of repetitions.  */
          unsigned int reps = 0;
@@ -500,6 +508,7 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
                /* 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]);
@@ -618,7 +627,7 @@ c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
 
 void
 c_get_string (struct value *value, gdb_byte **buffer, int *length,
-             const char **charset)
+             struct type **char_type, const char **charset)
 {
   int err, width;
   unsigned int fetchlimit;
@@ -626,6 +635,7 @@ c_get_string (struct value *value, gdb_byte **buffer, int *length,
   struct type *element_type = TYPE_TARGET_TYPE (type);
   int req_length = *length;
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
+  enum c_string_type kind;
 
   if (element_type == NULL)
     goto error;
@@ -652,13 +662,11 @@ c_get_string (struct value *value, gdb_byte **buffer, int *length,
     /* We work only with arrays and pointers.  */
     goto error;
 
-  element_type = check_typedef (element_type);
-  if (TYPE_CODE (element_type) != TYPE_CODE_INT
-      && TYPE_CODE (element_type) != TYPE_CODE_CHAR)
-    /* If the elements are not integers or characters, we don't consider it
-       a string.  */
+  if (! c_textual_element_type (element_type, 0))
     goto error;
-
+  kind = classify_type (element_type,
+                       get_type_arch (element_type),
+                       charset);
   width = TYPE_LENGTH (element_type);
 
   /* If the string lives in GDB's memory instead of the inferior's, then we
@@ -717,7 +725,7 @@ c_get_string (struct value *value, gdb_byte **buffer, int *length,
   if (*length != 0)
      *length = *length / width;
 
-  *charset = target_charset ();
+  *char_type = element_type;
 
   return;
 
@@ -763,7 +771,7 @@ convert_ucn (char *p, char *limit, const char *dest_charset,
       result >>= 8;
     }
 
-  convert_between_encodings ("UCS-4BE", dest_charset, data, 4, 4, output,
+  convert_between_encodings ("UTF-32BE", dest_charset, data, 4, 4, output,
                             translit_none);
 
   return p;
@@ -878,6 +886,7 @@ convert_escape (struct type *type, const char *dest_charset,
     case 'U':
       {
        int length = *p == 'u' ? 4 : 8;
+
        ADVANCE;
        if (!isxdigit (*p))
          error (_("\\u used with no following hex digits"));
@@ -905,6 +914,7 @@ parse_one_string (struct obstack *output, char *data, int len,
   while (data < limit)
     {
       char *p = data;
+
       /* Look for next escape, or the end of the input.  */
       while (p < limit && *p != '\\')
        ++p;
@@ -940,7 +950,6 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,
        struct value *result;
        enum c_string_type dest_type;
        const char *dest_charset;
-       enum bfd_endian byte_order;
 
        obstack_init (&output);
        cleanup = make_cleanup_obstack_free (&output);
@@ -977,8 +986,7 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,
        /* Ensure TYPE_LENGTH is valid for TYPE.  */
        check_typedef (type);
 
-       byte_order = gdbarch_byte_order (exp->gdbarch);
-       dest_charset = charset_for_string_type (dest_type, byte_order);
+       dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
 
        ++*pos;
        while (*pos < limit)
@@ -1020,6 +1028,7 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,
        else
          {
            int i;
+
            /* Write the terminating character.  */
            for (i = 0; i < TYPE_LENGTH (type); ++i)
              obstack_1grow (&output, 0);
@@ -1106,6 +1115,7 @@ c_language_arch_info (struct gdbarch *gdbarch,
                      struct language_arch_info *lai)
 {
   const struct builtin_type *builtin = builtin_type (gdbarch);
+
   lai->string_char_type = builtin->builtin_char;
   lai->primitive_type_vector
     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
@@ -1134,10 +1144,11 @@ c_language_arch_info (struct gdbarch *gdbarch,
   lai->bool_type_default = builtin->builtin_int;
 }
 
-static const struct exp_descriptor exp_descriptor_c = 
+const struct exp_descriptor exp_descriptor_c = 
 {
   print_subexp_standard,
   operator_length_standard,
+  operator_check_standard,
   op_name_standard,
   dump_subexp_body_standard,
   evaluate_subexp_c
@@ -1211,6 +1222,7 @@ cplus_language_arch_info (struct gdbarch *gdbarch,
                          struct language_arch_info *lai)
 {
   const struct builtin_type *builtin = builtin_type (gdbarch);
+
   lai->string_char_type = builtin->builtin_char;
   lai->primitive_type_vector
     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
This page took 0.028782 seconds and 4 git commands to generate.