gdb/c-valprint.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Support for printing C values for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "value.h"
  19. #include "valprint.h"
  20. #include "language.h"
  21. #include "c-lang.h"
  22. #include "cp-abi.h"
  23. #include "target.h"
  24. #include "objfiles.h"


  25. /* A helper for c_textual_element_type.  This checks the name of the
  26.    typedef.  This is bogus but it isn't apparent that the compiler
  27.    provides us the help we may need.  */

  28. static int
  29. textual_name (const char *name)
  30. {
  31.   return (!strcmp (name, "wchar_t")
  32.           || !strcmp (name, "char16_t")
  33.           || !strcmp (name, "char32_t"));
  34. }

  35. /* Apply a heuristic to decide whether an array of TYPE or a pointer
  36.    to TYPE should be printed as a textual string.  Return non-zero if
  37.    it should, or zero if it should be treated as an array of integers
  38.    or pointer to integers.  FORMAT is the current format letter, or 0
  39.    if none.

  40.    We guess that "char" is a character.  Explicitly signed and
  41.    unsigned character types are also characters.  Integer data from
  42.    vector types is not.  The user can override this by using the /s
  43.    format letter.  */

  44. int
  45. c_textual_element_type (struct type *type, char format)
  46. {
  47.   struct type *true_type, *iter_type;

  48.   if (format != 0 && format != 's')
  49.     return 0;

  50.   /* We also rely on this for its side effect of setting up all the
  51.      typedef pointers.  */
  52.   true_type = check_typedef (type);

  53.   /* TYPE_CODE_CHAR is always textual.  */
  54.   if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
  55.     return 1;

  56.   /* Any other character-like types must be integral.  */
  57.   if (TYPE_CODE (true_type) != TYPE_CODE_INT)
  58.     return 0;

  59.   /* We peel typedefs one by one, looking for a match.  */
  60.   iter_type = type;
  61.   while (iter_type)
  62.     {
  63.       /* Check the name of the type.  */
  64.       if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
  65.         return 1;

  66.       if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
  67.         break;

  68.       /* Peel a single typedef.  If the typedef doesn't have a target
  69.          type, we use check_typedef and hope the result is ok -- it
  70.          might be for C++, where wchar_t is a built-in type.  */
  71.       if (TYPE_TARGET_TYPE (iter_type))
  72.         iter_type = TYPE_TARGET_TYPE (iter_type);
  73.       else
  74.         iter_type = check_typedef (iter_type);
  75.     }

  76.   if (format == 's')
  77.     {
  78.       /* Print this as a string if we can manage it.  For now, no wide
  79.          character support.  */
  80.       if (TYPE_CODE (true_type) == TYPE_CODE_INT
  81.           && TYPE_LENGTH (true_type) == 1)
  82.         return 1;
  83.     }
  84.   else
  85.     {
  86.       /* If a one-byte TYPE_CODE_INT is missing the not-a-character
  87.          flag, then we treat it as text; otherwise, we assume it's
  88.          being used as data.  */
  89.       if (TYPE_CODE (true_type) == TYPE_CODE_INT
  90.           && TYPE_LENGTH (true_type) == 1
  91.           && !TYPE_NOTTEXT (true_type))
  92.         return 1;
  93.     }

  94.   return 0;
  95. }

  96. /* Decorations for C.  */

  97. static const struct generic_val_print_decorations c_decorations =
  98. {
  99.   "",
  100.   " + ",
  101.   " * I",
  102.   "true",
  103.   "false",
  104.   "void"
  105. };

  106. /* See val_print for a description of the various parameters of this
  107.    function; they are identical.  */

  108. void
  109. c_val_print (struct type *type, const gdb_byte *valaddr,
  110.              int embedded_offset, CORE_ADDR address,
  111.              struct ui_file *stream, int recurse,
  112.              const struct value *original_value,
  113.              const struct value_print_options *options)
  114. {
  115.   struct gdbarch *gdbarch = get_type_arch (type);
  116.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  117.   unsigned int i = 0;        /* Number of characters printed.  */
  118.   unsigned len;
  119.   struct type *elttype, *unresolved_elttype;
  120.   struct type *unresolved_type = type;
  121.   unsigned eltlen;
  122.   CORE_ADDR addr;

  123.   CHECK_TYPEDEF (type);
  124.   switch (TYPE_CODE (type))
  125.     {
  126.     case TYPE_CODE_ARRAY:
  127.       unresolved_elttype = TYPE_TARGET_TYPE (type);
  128.       elttype = check_typedef (unresolved_elttype);
  129.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
  130.         {
  131.           LONGEST low_bound, high_bound;

  132.           if (!get_array_bounds (type, &low_bound, &high_bound))
  133.             error (_("Could not determine the array high bound"));

  134.           eltlen = TYPE_LENGTH (elttype);
  135.           len = high_bound - low_bound + 1;
  136.           if (options->prettyformat_arrays)
  137.             {
  138.               print_spaces_filtered (2 + 2 * recurse, stream);
  139.             }

  140.           /* Print arrays of textual chars with a string syntax, as
  141.              long as the entire array is valid.  */
  142.           if (c_textual_element_type (unresolved_elttype,
  143.                                       options->format)
  144.               && value_bytes_available (original_value, embedded_offset,
  145.                                         TYPE_LENGTH (type))
  146.               && !value_bits_any_optimized_out (original_value,
  147.                                                 TARGET_CHAR_BIT * embedded_offset,
  148.                                                 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  149.             {
  150.               int force_ellipses = 0;

  151.               /* If requested, look for the first null char and only
  152.                  print elements up to it.  */
  153.               if (options->stop_print_at_null)
  154.                 {
  155.                   unsigned int temp_len;

  156.                   for (temp_len = 0;
  157.                        (temp_len < len
  158.                         && temp_len < options->print_max
  159.                         && extract_unsigned_integer (valaddr + embedded_offset
  160.                                                      + temp_len * eltlen,
  161.                                                      eltlen, byte_order) != 0);
  162.                        ++temp_len)
  163.                     ;

  164.                   /* Force LA_PRINT_STRING to print ellipses if
  165.                      we've printed the maximum characters and
  166.                      the next character is not \000.  */
  167.                   if (temp_len == options->print_max && temp_len < len)
  168.                     {
  169.                       ULONGEST val
  170.                         = extract_unsigned_integer (valaddr + embedded_offset
  171.                                                     + temp_len * eltlen,
  172.                                                     eltlen, byte_order);
  173.                       if (val != 0)
  174.                         force_ellipses = 1;
  175.                     }

  176.                   len = temp_len;
  177.                 }

  178.               LA_PRINT_STRING (stream, unresolved_elttype,
  179.                                valaddr + embedded_offset, len,
  180.                                NULL, force_ellipses, options);
  181.               i = len;
  182.             }
  183.           else
  184.             {
  185.               fprintf_filtered (stream, "{");
  186.               /* If this is a virtual function table, print the 0th
  187.                  entry specially, and the rest of the members
  188.                  normally.  */
  189.               if (cp_is_vtbl_ptr_type (elttype))
  190.                 {
  191.                   i = 1;
  192.                   fprintf_filtered (stream, _("%d vtable entries"),
  193.                                     len - 1);
  194.                 }
  195.               else
  196.                 {
  197.                   i = 0;
  198.                 }
  199.               val_print_array_elements (type, valaddr, embedded_offset,
  200.                                         address, stream,
  201.                                         recurse, original_value, options, i);
  202.               fprintf_filtered (stream, "}");
  203.             }
  204.           break;
  205.         }
  206.       /* Array of unspecified length: treat like pointer to first
  207.          elt.  */
  208.       addr = address + embedded_offset;
  209.       goto print_unpacked_pointer;

  210.     case TYPE_CODE_METHODPTR:
  211.       cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
  212.       break;

  213.     case TYPE_CODE_PTR:
  214.       if (options->format && options->format != 's')
  215.         {
  216.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  217.                                       original_value, options, 0, stream);
  218.           break;
  219.         }
  220.       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
  221.         {
  222.           /* Print the unmangled name if desired.  */
  223.           /* Print vtable entry - we only get here if we ARE using
  224.              -fvtable_thunks.  (Otherwise, look under
  225.              TYPE_CODE_STRUCT.)  */
  226.           CORE_ADDR addr
  227.             = extract_typed_address (valaddr + embedded_offset, type);

  228.           print_function_pointer_address (options, gdbarch, addr, stream);
  229.           break;
  230.         }
  231.       unresolved_elttype = TYPE_TARGET_TYPE (type);
  232.       elttype = check_typedef (unresolved_elttype);
  233.         {
  234.           int want_space;

  235.           addr = unpack_pointer (type, valaddr + embedded_offset);
  236.         print_unpacked_pointer:

  237.           want_space = 0;

  238.           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  239.             {
  240.               /* Try to print what function it points to.  */
  241.               print_function_pointer_address (options, gdbarch, addr, stream);
  242.               return;
  243.             }

  244.           if (options->symbol_print)
  245.             want_space = print_address_demangle (options, gdbarch, addr,
  246.                                                  stream, demangle);
  247.           else if (options->addressprint)
  248.             {
  249.               fputs_filtered (paddress (gdbarch, addr), stream);
  250.               want_space = 1;
  251.             }

  252.           /* For a pointer to a textual type, also print the string
  253.              pointed to, unless pointer is null.  */

  254.           if (c_textual_element_type (unresolved_elttype,
  255.                                       options->format)
  256.               && addr != 0)
  257.             {
  258.               if (want_space)
  259.                 fputs_filtered (" ", stream);
  260.               i = val_print_string (unresolved_elttype, NULL,
  261.                                     addr, -1,
  262.                                     stream, options);
  263.             }
  264.           else if (cp_is_vtbl_member (type))
  265.             {
  266.               /* Print vtbl's nicely.  */
  267.               CORE_ADDR vt_address = unpack_pointer (type,
  268.                                                      valaddr
  269.                                                      + embedded_offset);
  270.               struct bound_minimal_symbol msymbol =
  271.                 lookup_minimal_symbol_by_pc (vt_address);

  272.               /* If 'symbol_print' is set, we did the work above.  */
  273.               if (!options->symbol_print
  274.                   && (msymbol.minsym != NULL)
  275.                   && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
  276.                 {
  277.                   if (want_space)
  278.                     fputs_filtered (" ", stream);
  279.                   fputs_filtered (" <", stream);
  280.                   fputs_filtered (MSYMBOL_PRINT_NAME (msymbol.minsym), stream);
  281.                   fputs_filtered (">", stream);
  282.                   want_space = 1;
  283.                 }

  284.               if (vt_address && options->vtblprint)
  285.                 {
  286.                   struct value *vt_val;
  287.                   struct symbol *wsym = (struct symbol *) NULL;
  288.                   struct type *wtype;
  289.                   struct block *block = (struct block *) NULL;
  290.                   struct field_of_this_result is_this_fld;

  291.                   if (want_space)
  292.                     fputs_filtered (" ", stream);

  293.                   if (msymbol.minsym != NULL)
  294.                     wsym = lookup_symbol (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
  295.                                           block, VAR_DOMAIN,
  296.                                           &is_this_fld);

  297.                   if (wsym)
  298.                     {
  299.                       wtype = SYMBOL_TYPE (wsym);
  300.                     }
  301.                   else
  302.                     {
  303.                       wtype = unresolved_elttype;
  304.                     }
  305.                   vt_val = value_at (wtype, vt_address);
  306.                   common_val_print (vt_val, stream, recurse + 1,
  307.                                     options, current_language);
  308.                   if (options->prettyformat)
  309.                     {
  310.                       fprintf_filtered (stream, "\n");
  311.                       print_spaces_filtered (2 + 2 * recurse, stream);
  312.                     }
  313.                 }
  314.             }
  315.           return;
  316.         }
  317.       break;

  318.     case TYPE_CODE_UNION:
  319.       if (recurse && !options->unionprint)
  320.         {
  321.           fprintf_filtered (stream, "{...}");
  322.           break;
  323.         }
  324.       /* Fall through.  */
  325.     case TYPE_CODE_STRUCT:
  326.       /*FIXME: Abstract this away.  */
  327.       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
  328.         {
  329.           /* Print the unmangled name if desired.  */
  330.           /* Print vtable entry - we only get here if NOT using
  331.              -fvtable_thunks.  (Otherwise, look under
  332.              TYPE_CODE_PTR.)  */
  333.           int offset = (embedded_offset
  334.                         + TYPE_FIELD_BITPOS (type,
  335.                                              VTBL_FNADDR_OFFSET) / 8);
  336.           struct type *field_type = TYPE_FIELD_TYPE (type,
  337.                                                      VTBL_FNADDR_OFFSET);
  338.           CORE_ADDR addr
  339.             = extract_typed_address (valaddr + offset, field_type);

  340.           print_function_pointer_address (options, gdbarch, addr, stream);
  341.         }
  342.       else
  343.         cp_print_value_fields_rtti (type, valaddr,
  344.                                     embedded_offset, address,
  345.                                     stream, recurse,
  346.                                     original_value, options,
  347.                                     NULL, 0);
  348.       break;

  349.     case TYPE_CODE_INT:
  350.       if (options->format || options->output_format)
  351.         {
  352.           struct value_print_options opts = *options;

  353.           opts.format = (options->format ? options->format
  354.                          : options->output_format);
  355.           val_print_scalar_formatted (type, valaddr, embedded_offset,
  356.                                       original_value, &opts, 0, stream);
  357.         }
  358.       else
  359.         {
  360.           val_print_type_code_int (type, valaddr + embedded_offset,
  361.                                    stream);
  362.           /* C and C++ has no single byte int type, char is used
  363.              instead.  Since we don't know whether the value is really
  364.              intended to be used as an integer or a character, print
  365.              the character equivalent as well.  */
  366.           if (c_textual_element_type (unresolved_type, options->format))
  367.             {
  368.               fputs_filtered (" ", stream);
  369.               LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset),
  370.                              unresolved_type, stream);
  371.             }
  372.         }
  373.       break;

  374.     case TYPE_CODE_MEMBERPTR:
  375.       if (!options->format)
  376.         {
  377.           cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
  378.           break;
  379.         }
  380.       /* FALLTHROUGH */

  381.     case TYPE_CODE_REF:
  382.     case TYPE_CODE_ENUM:
  383.     case TYPE_CODE_FLAGS:
  384.     case TYPE_CODE_FUNC:
  385.     case TYPE_CODE_METHOD:
  386.     case TYPE_CODE_BOOL:
  387.     case TYPE_CODE_RANGE:
  388.     case TYPE_CODE_FLT:
  389.     case TYPE_CODE_DECFLOAT:
  390.     case TYPE_CODE_VOID:
  391.     case TYPE_CODE_ERROR:
  392.     case TYPE_CODE_UNDEF:
  393.     case TYPE_CODE_COMPLEX:
  394.     case TYPE_CODE_CHAR:
  395.     default:
  396.       generic_val_print (type, valaddr, embedded_offset, address,
  397.                          stream, recurse, original_value, options,
  398.                          &c_decorations);
  399.       break;
  400.     }
  401.   gdb_flush (stream);
  402. }

  403. void
  404. c_value_print (struct value *val, struct ui_file *stream,
  405.                const struct value_print_options *options)
  406. {
  407.   struct type *type, *real_type, *val_type;
  408.   int full, top, using_enc;
  409.   struct value_print_options opts = *options;

  410.   opts.deref_ref = 1;

  411.   /* If it is a pointer, indicate what it points to.

  412.      Print type also if it is a reference.

  413.      C++: if it is a member pointer, we will take care
  414.      of that when we print it.  */

  415.   /* Preserve the original type before stripping typedefs.  We prefer
  416.      to pass down the original type when possible, but for local
  417.      checks it is better to look past the typedefs.  */
  418.   val_type = value_type (val);
  419.   type = check_typedef (val_type);

  420.   if (TYPE_CODE (type) == TYPE_CODE_PTR
  421.       || TYPE_CODE (type) == TYPE_CODE_REF)
  422.     {
  423.       /* Hack:  remove (char *) for char strings.  Their
  424.          type is indicated by the quoted string anyway.
  425.          (Don't use c_textual_element_type here; quoted strings
  426.          are always exactly (char *), (wchar_t *), or the like.  */
  427.       if (TYPE_CODE (val_type) == TYPE_CODE_PTR
  428.           && TYPE_NAME (val_type) == NULL
  429.           && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
  430.           && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
  431.                       "char") == 0
  432.               || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
  433.         {
  434.           /* Print nothing.  */
  435.         }
  436.       else if (options->objectprint
  437.                && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
  438.         {
  439.           int is_ref = TYPE_CODE (type) == TYPE_CODE_REF;

  440.           if (is_ref)
  441.             val = value_addr (val);

  442.           /* Pointer to class, check real type of object.  */
  443.           fprintf_filtered (stream, "(");

  444.           if (value_entirely_available (val))
  445.              {
  446.               real_type = value_rtti_indirect_type (val, &full, &top,
  447.                                                     &using_enc);
  448.               if (real_type)
  449.                 {
  450.                   /* RTTI entry found.  */
  451.                   type = real_type;

  452.                   /* Need to adjust pointer value.  */
  453.                   val = value_from_pointer (real_type,
  454.                                             value_as_address (val) - top);

  455.                   if (is_ref)
  456.                     {
  457.                       val = value_ref (value_ind (val));
  458.                       type = value_type (val);
  459.                     }

  460.                   /* Note: When we look up RTTI entries, we don't get
  461.                      any information on const or volatile
  462.                      attributes.  */
  463.                 }
  464.             }
  465.           type_print (type, "", stream, -1);
  466.           fprintf_filtered (stream, ") ");
  467.           val_type = type;
  468.         }
  469.       else
  470.         {
  471.           /* normal case */
  472.           fprintf_filtered (stream, "(");
  473.           type_print (value_type (val), "", stream, -1);
  474.           fprintf_filtered (stream, ") ");
  475.         }
  476.     }

  477.   if (!value_initialized (val))
  478.     fprintf_filtered (stream, " [uninitialized] ");

  479.   if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_STRUCT))
  480.     {
  481.       /* Attempt to determine real type of object.  */
  482.       real_type = value_rtti_type (val, &full, &top, &using_enc);
  483.       if (real_type)
  484.         {
  485.           /* We have RTTI information, so use it.  */
  486.           val = value_full_object (val, real_type,
  487.                                    full, top, using_enc);
  488.           fprintf_filtered (stream, "(%s%s) ",
  489.                             TYPE_NAME (real_type),
  490.                             full ? "" : _(" [incomplete object]"));
  491.           /* Print out object: enclosing type is same as real_type if
  492.              full.  */
  493.           val_print (value_enclosing_type (val),
  494.                      value_contents_for_printing (val), 0,
  495.                      value_address (val), stream, 0,
  496.                      val, &opts, current_language);
  497.           return;
  498.           /* Note: When we look up RTTI entries, we don't get any
  499.              information on const or volatile attributes.  */
  500.         }
  501.       else if (type != check_typedef (value_enclosing_type (val)))
  502.         {
  503.           /* No RTTI information, so let's do our best.  */
  504.           fprintf_filtered (stream, "(%s ?) ",
  505.                             TYPE_NAME (value_enclosing_type (val)));
  506.           val_print (value_enclosing_type (val),
  507.                      value_contents_for_printing (val), 0,
  508.                      value_address (val), stream, 0,
  509.                      val, &opts, current_language);
  510.           return;
  511.         }
  512.       /* Otherwise, we end up at the return outside this "if".  */
  513.     }

  514.   val_print (val_type, value_contents_for_printing (val),
  515.              value_embedded_offset (val),
  516.              value_address (val),
  517.              stream, 0,
  518.              val, &opts, current_language);
  519. }