gdb/m2-valprint.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Support for printing Modula 2 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 "typeprint.h"
  22. #include "c-lang.h"
  23. #include "m2-lang.h"
  24. #include "target.h"

  25. static int print_unpacked_pointer (struct type *type,
  26.                                    CORE_ADDR address, CORE_ADDR addr,
  27.                                    const struct value_print_options *options,
  28.                                    struct ui_file *stream);
  29. static void
  30. m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
  31.                          int embedded_offset, CORE_ADDR address,
  32.                          struct ui_file *stream, int recurse,
  33.                          const struct value *val,
  34.                          const struct value_print_options *options,
  35.                          int len);


  36. /* get_long_set_bounds - assigns the bounds of the long set to low and
  37.                          high.  */

  38. int
  39. get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
  40. {
  41.   int len, i;

  42.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  43.     {
  44.       len = TYPE_NFIELDS (type);
  45.       i = TYPE_N_BASECLASSES (type);
  46.       if (len == 0)
  47.         return 0;
  48.       *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
  49.       *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
  50.                                                                  len-1)));
  51.       return 1;
  52.     }
  53.   error (_("expecting long_set"));
  54.   return 0;
  55. }

  56. static void
  57. m2_print_long_set (struct type *type, const gdb_byte *valaddr,
  58.                    int embedded_offset, CORE_ADDR address,
  59.                    struct ui_file *stream)
  60. {
  61.   int empty_set        = 1;
  62.   int element_seen     = 0;
  63.   LONGEST previous_low = 0;
  64.   LONGEST previous_high= 0;
  65.   LONGEST i, low_bound, high_bound;
  66.   LONGEST field_low, field_high;
  67.   struct type *range;
  68.   int len, field;
  69.   struct type *target;
  70.   int bitval;

  71.   CHECK_TYPEDEF (type);

  72.   fprintf_filtered (stream, "{");
  73.   len = TYPE_NFIELDS (type);
  74.   if (get_long_set_bounds (type, &low_bound, &high_bound))
  75.     {
  76.       field = TYPE_N_BASECLASSES (type);
  77.       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
  78.     }
  79.   else
  80.     {
  81.       fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
  82.       return;
  83.     }

  84.   target = TYPE_TARGET_TYPE (range);

  85.   if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
  86.     {
  87.       for (i = low_bound; i <= high_bound; i++)
  88.         {
  89.           bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
  90.                                     (TYPE_FIELD_BITPOS (type, field) / 8) +
  91.                                     valaddr + embedded_offset, i);
  92.           if (bitval < 0)
  93.             error (_("bit test is out of range"));
  94.           else if (bitval > 0)
  95.             {
  96.               previous_high = i;
  97.               if (! element_seen)
  98.                 {
  99.                   if (! empty_set)
  100.                     fprintf_filtered (stream, ", ");
  101.                   print_type_scalar (target, i, stream);
  102.                   empty_set    = 0;
  103.                   element_seen = 1;
  104.                   previous_low = i;
  105.                 }
  106.             }
  107.           else
  108.             {
  109.               /* bit is not set */
  110.               if (element_seen)
  111.                 {
  112.                   if (previous_low+1 < previous_high)
  113.                     fprintf_filtered (stream, "..");
  114.                   if (previous_low+1 < previous_high)
  115.                     print_type_scalar (target, previous_high, stream);
  116.                   element_seen = 0;
  117.                 }
  118.             }
  119.           if (i == field_high)
  120.             {
  121.               field++;
  122.               if (field == len)
  123.                 break;
  124.               range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
  125.               if (get_discrete_bounds (range, &field_low, &field_high) < 0)
  126.                 break;
  127.               target = TYPE_TARGET_TYPE (range);
  128.             }
  129.         }
  130.       if (element_seen)
  131.         {
  132.           if (previous_low+1 < previous_high)
  133.             {
  134.               fprintf_filtered (stream, "..");
  135.               print_type_scalar (target, previous_high, stream);
  136.             }
  137.           element_seen = 0;
  138.         }
  139.       fprintf_filtered (stream, "}");
  140.     }
  141. }

  142. static void
  143. m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
  144.                           int embedded_offset, CORE_ADDR address,
  145.                           struct ui_file *stream, int recurse,
  146.                           const struct value_print_options *options)
  147. {
  148.   struct type *content_type;
  149.   CORE_ADDR addr;
  150.   LONGEST len;
  151.   struct value *val;

  152.   CHECK_TYPEDEF (type);
  153.   content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));

  154.   addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
  155.                          (TYPE_FIELD_BITPOS (type, 0) / 8) +
  156.                          valaddr + embedded_offset);

  157.   val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
  158.                        addr);
  159.   len = unpack_field_as_long (type, valaddr + embedded_offset, 1);

  160.   fprintf_filtered (stream, "{");
  161.   m2_print_array_contents (value_type (val),
  162.                            value_contents_for_printing (val),
  163.                            value_embedded_offset (val), addr, stream,
  164.                            recurse, val, options, len);
  165.   fprintf_filtered (stream, ", HIGH = %d}", (int) len);
  166. }

  167. static int
  168. print_unpacked_pointer (struct type *type,
  169.                         CORE_ADDR address, CORE_ADDR addr,
  170.                         const struct value_print_options *options,
  171.                         struct ui_file *stream)
  172. {
  173.   struct gdbarch *gdbarch = get_type_arch (type);
  174.   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  175.   int want_space = 0;

  176.   if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  177.     {
  178.       /* Try to print what function it points to.  */
  179.       print_function_pointer_address (options, gdbarch, addr, stream);
  180.       /* Return value is irrelevant except for string pointers.  */
  181.       return 0;
  182.     }

  183.   if (options->addressprint && options->format != 's')
  184.     {
  185.       fputs_filtered (paddress (gdbarch, address), stream);
  186.       want_space = 1;
  187.     }

  188.   /* For a pointer to char or unsigned char, also print the string
  189.      pointed to, unless pointer is null.  */

  190.   if (TYPE_LENGTH (elttype) == 1
  191.       && TYPE_CODE (elttype) == TYPE_CODE_INT
  192.       && (options->format == 0 || options->format == 's')
  193.       && addr != 0)
  194.     {
  195.       if (want_space)
  196.         fputs_filtered (" ", stream);
  197.       return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
  198.                                stream, options);
  199.     }

  200.   return 0;
  201. }

  202. static void
  203. print_variable_at_address (struct type *type,
  204.                            const gdb_byte *valaddr,
  205.                            struct ui_file *stream,
  206.                            int recurse,
  207.                            const struct value_print_options *options)
  208. {
  209.   struct gdbarch *gdbarch = get_type_arch (type);
  210.   CORE_ADDR addr = unpack_pointer (type, valaddr);
  211.   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));

  212.   fprintf_filtered (stream, "[");
  213.   fputs_filtered (paddress (gdbarch, addr), stream);
  214.   fprintf_filtered (stream, "] : ");

  215.   if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
  216.     {
  217.       struct value *deref_val =
  218.         value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));

  219.       common_val_print (deref_val, stream, recurse, options, current_language);
  220.     }
  221.   else
  222.     fputs_filtered ("???", stream);
  223. }


  224. /* m2_print_array_contents - prints out the contents of an
  225.                              array up to a max_print values.
  226.                              It prints arrays of char as a string
  227.                              and all other data types as comma
  228.                              separated values.  */

  229. static void
  230. m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
  231.                          int embedded_offset, CORE_ADDR address,
  232.                          struct ui_file *stream, int recurse,
  233.                          const struct value *val,
  234.                          const struct value_print_options *options,
  235.                          int len)
  236. {
  237.   CHECK_TYPEDEF (type);

  238.   if (TYPE_LENGTH (type) > 0)
  239.     {
  240.       if (options->prettyformat_arrays)
  241.         print_spaces_filtered (2 + 2 * recurse, stream);
  242.       /* For an array of chars, print with string syntax.  */
  243.       if (TYPE_LENGTH (type) == 1 &&
  244.           ((TYPE_CODE (type) == TYPE_CODE_INT)
  245.            || ((current_language->la_language == language_m2)
  246.                && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
  247.           && (options->format == 0 || options->format == 's'))
  248.         val_print_string (type, NULL, address, len+1, stream, options);
  249.       else
  250.         {
  251.           fprintf_filtered (stream, "{");
  252.           val_print_array_elements (type, valaddr, embedded_offset,
  253.                                     address, stream, recurse, val,
  254.                                     options, 0);
  255.           fprintf_filtered (stream, "}");
  256.         }
  257.     }
  258. }

  259. /* Decorations for Modula 2.  */

  260. static const struct generic_val_print_decorations m2_decorations =
  261. {
  262.   "",
  263.   " + ",
  264.   " * I",
  265.   "TRUE",
  266.   "FALSE",
  267.   "void"
  268. };

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

  271. void
  272. m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
  273.               CORE_ADDR address, struct ui_file *stream, int recurse,
  274.               const struct value *original_value,
  275.               const struct value_print_options *options)
  276. {
  277.   struct gdbarch *gdbarch = get_type_arch (type);
  278.   unsigned int i = 0;        /* Number of characters printed.  */
  279.   unsigned len;
  280.   struct type *elttype;
  281.   CORE_ADDR addr;

  282.   CHECK_TYPEDEF (type);
  283.   switch (TYPE_CODE (type))
  284.     {
  285.     case TYPE_CODE_ARRAY:
  286.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
  287.         {
  288.           elttype = check_typedef (TYPE_TARGET_TYPE (type));
  289.           len = TYPE_LENGTH (type) / TYPE_LENGTH (elttype);
  290.           if (options->prettyformat_arrays)
  291.             print_spaces_filtered (2 + 2 * recurse, stream);
  292.           /* For an array of chars, print with string syntax.  */
  293.           if (TYPE_LENGTH (elttype) == 1 &&
  294.               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
  295.                || ((current_language->la_language == language_m2)
  296.                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
  297.               && (options->format == 0 || options->format == 's'))
  298.             {
  299.               /* If requested, look for the first null char and only print
  300.                  elements up to it.  */
  301.               if (options->stop_print_at_null)
  302.                 {
  303.                   unsigned int temp_len;

  304.                   /* Look for a NULL char.  */
  305.                   for (temp_len = 0;
  306.                        (valaddr + embedded_offset)[temp_len]
  307.                          && temp_len < len && temp_len < options->print_max;
  308.                        temp_len++);
  309.                   len = temp_len;
  310.                 }

  311.               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
  312.                                valaddr + embedded_offset, len, NULL,
  313.                                0, options);
  314.               i = len;
  315.             }
  316.           else
  317.             {
  318.               fprintf_filtered (stream, "{");
  319.               val_print_array_elements (type, valaddr, embedded_offset,
  320.                                         address, stream,
  321.                                         recurse, original_value,
  322.                                         options, 0);
  323.               fprintf_filtered (stream, "}");
  324.             }
  325.           break;
  326.         }
  327.       /* Array of unspecified length: treat like pointer to first elt.  */
  328.       print_unpacked_pointer (type, address, address, options, stream);
  329.       break;

  330.     case TYPE_CODE_PTR:
  331.       if (TYPE_CONST (type))
  332.         print_variable_at_address (type, valaddr + embedded_offset,
  333.                                    stream, recurse, options);
  334.       else if (options->format && options->format != 's')
  335.         val_print_scalar_formatted (type, valaddr, embedded_offset,
  336.                                     original_value, options, 0, stream);
  337.       else
  338.         {
  339.           addr = unpack_pointer (type, valaddr + embedded_offset);
  340.           print_unpacked_pointer (type, addr, address, options, stream);
  341.         }
  342.       break;

  343.     case TYPE_CODE_UNION:
  344.       if (recurse && !options->unionprint)
  345.         {
  346.           fprintf_filtered (stream, "{...}");
  347.           break;
  348.         }
  349.       /* Fall through.  */
  350.     case TYPE_CODE_STRUCT:
  351.       if (m2_is_long_set (type))
  352.         m2_print_long_set (type, valaddr, embedded_offset, address,
  353.                            stream);
  354.       else if (m2_is_unbounded_array (type))
  355.         m2_print_unbounded_array (type, valaddr, embedded_offset,
  356.                                   address, stream, recurse, options);
  357.       else
  358.         cp_print_value_fields (type, type, valaddr, embedded_offset,
  359.                                address, stream, recurse, original_value,
  360.                                options, NULL, 0);
  361.       break;

  362.     case TYPE_CODE_SET:
  363.       elttype = TYPE_INDEX_TYPE (type);
  364.       CHECK_TYPEDEF (elttype);
  365.       if (TYPE_STUB (elttype))
  366.         {
  367.           fprintf_filtered (stream, _("<incomplete type>"));
  368.           gdb_flush (stream);
  369.           break;
  370.         }
  371.       else
  372.         {
  373.           struct type *range = elttype;
  374.           LONGEST low_bound, high_bound;
  375.           int i;
  376.           int need_comma = 0;

  377.           fputs_filtered ("{", stream);

  378.           i = get_discrete_bounds (range, &low_bound, &high_bound);
  379.         maybe_bad_bstring:
  380.           if (i < 0)
  381.             {
  382.               fputs_filtered (_("<error value>"), stream);
  383.               goto done;
  384.             }

  385.           for (i = low_bound; i <= high_bound; i++)
  386.             {
  387.               int element = value_bit_index (type, valaddr + embedded_offset,
  388.                                              i);

  389.               if (element < 0)
  390.                 {
  391.                   i = element;
  392.                   goto maybe_bad_bstring;
  393.                 }
  394.               if (element)
  395.                 {
  396.                   if (need_comma)
  397.                     fputs_filtered (", ", stream);
  398.                   print_type_scalar (range, i, stream);
  399.                   need_comma = 1;

  400.                   if (i + 1 <= high_bound
  401.                       && value_bit_index (type, valaddr + embedded_offset,
  402.                                           ++i))
  403.                     {
  404.                       int j = i;

  405.                       fputs_filtered ("..", stream);
  406.                       while (i + 1 <= high_bound
  407.                              && value_bit_index (type,
  408.                                                  valaddr + embedded_offset,
  409.                                                  ++i))
  410.                         j = i;
  411.                       print_type_scalar (range, j, stream);
  412.                     }
  413.                 }
  414.             }
  415.         done:
  416.           fputs_filtered ("}", stream);
  417.         }
  418.       break;

  419.     case TYPE_CODE_RANGE:
  420.       if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
  421.         {
  422.           m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
  423.                         address, stream, recurse, original_value, options);
  424.           break;
  425.         }
  426.       /* FIXME: create_static_range_type does not set the unsigned bit in a
  427.          range type (I think it probably should copy it from the target
  428.          type), so we won't print values which are too large to
  429.          fit in a signed integer correctly.  */
  430.       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
  431.          print with the target type, though, because the size of our type
  432.          and the target type might differ).  */
  433.       /* FALLTHROUGH */

  434.     case TYPE_CODE_REF:
  435.     case TYPE_CODE_ENUM:
  436.     case TYPE_CODE_FUNC:
  437.     case TYPE_CODE_INT:
  438.     case TYPE_CODE_FLT:
  439.     case TYPE_CODE_METHOD:
  440.     case TYPE_CODE_VOID:
  441.     case TYPE_CODE_ERROR:
  442.     case TYPE_CODE_UNDEF:
  443.     case TYPE_CODE_BOOL:
  444.     case TYPE_CODE_CHAR:
  445.     default:
  446.       generic_val_print (type, valaddr, embedded_offset, address,
  447.                          stream, recurse, original_value, options,
  448.                          &m2_decorations);
  449.       break;
  450.     }
  451.   gdb_flush (stream);
  452. }