gdb/ada-valprint.c - gdb

Functions defined

Source code

  1. /* Support for printing Ada 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 <ctype.h>
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "value.h"
  20. #include "demangle.h"
  21. #include "valprint.h"
  22. #include "language.h"
  23. #include "annotate.h"
  24. #include "ada-lang.h"
  25. #include "c-lang.h"
  26. #include "infcall.h"
  27. #include "objfiles.h"

  28. static int print_field_values (struct type *, const gdb_byte *,
  29.                                int,
  30.                                struct ui_file *, int,
  31.                                const struct value *,
  32.                                const struct value_print_options *,
  33.                                int, struct type *, int,
  34.                                const struct language_defn *);


  35. /* Make TYPE unsigned if its range of values includes no negatives.  */
  36. static void
  37. adjust_type_signedness (struct type *type)
  38. {
  39.   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
  40.       && TYPE_LOW_BOUND (type) >= 0)
  41.     TYPE_UNSIGNED (type) = 1;
  42. }

  43. /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
  44.    if non-standard (i.e., other than 1 for numbers, other than lower bound
  45.    of index type for enumerated type).  Returns 1 if something printed,
  46.    otherwise 0.  */

  47. static int
  48. print_optional_low_bound (struct ui_file *stream, struct type *type,
  49.                           const struct value_print_options *options)
  50. {
  51.   struct type *index_type;
  52.   LONGEST low_bound;
  53.   LONGEST high_bound;

  54.   if (options->print_array_indexes)
  55.     return 0;

  56.   if (!get_array_bounds (type, &low_bound, &high_bound))
  57.     return 0;

  58.   /* If this is an empty array, then don't print the lower bound.
  59.      That would be confusing, because we would print the lower bound,
  60.      followed by... nothing!  */
  61.   if (low_bound > high_bound)
  62.     return 0;

  63.   index_type = TYPE_INDEX_TYPE (type);

  64.   while (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
  65.     {
  66.       /* We need to know what the base type is, in order to do the
  67.          appropriate check below.  Otherwise, if this is a subrange
  68.          of an enumerated type, where the underlying value of the
  69.          first element is typically 0, we might test the low bound
  70.          against the wrong value.  */
  71.       index_type = TYPE_TARGET_TYPE (index_type);
  72.     }

  73.   switch (TYPE_CODE (index_type))
  74.     {
  75.     case TYPE_CODE_BOOL:
  76.       if (low_bound == 0)
  77.         return 0;
  78.       break;
  79.     case TYPE_CODE_ENUM:
  80.       if (low_bound == TYPE_FIELD_ENUMVAL (index_type, 0))
  81.         return 0;
  82.       break;
  83.     case TYPE_CODE_UNDEF:
  84.       index_type = NULL;
  85.       /* FALL THROUGH */
  86.     default:
  87.       if (low_bound == 1)
  88.         return 0;
  89.       break;
  90.     }

  91.   ada_print_scalar (index_type, low_bound, stream);
  92.   fprintf_filtered (stream, " => ");
  93.   return 1;
  94. }

  95. /*  Version of val_print_array_elements for GNAT-style packed arrays.
  96.     Prints elements of packed array of type TYPE at bit offset
  97.     BITOFFSET from VALADDR on STREAM.  Formats according to OPTIONS and
  98.     separates with commas.  RECURSE is the recursion (nesting) level.
  99.     TYPE must have been decoded (as by ada_coerce_to_simple_array).  */

  100. static void
  101. val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
  102.                                  int offset,
  103.                                  int bitoffset, struct ui_file *stream,
  104.                                  int recurse,
  105.                                  const struct value *val,
  106.                                  const struct value_print_options *options)
  107. {
  108.   unsigned int i;
  109.   unsigned int things_printed = 0;
  110.   unsigned len;
  111.   struct type *elttype, *index_type;
  112.   unsigned eltlen;
  113.   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
  114.   struct value *mark = value_mark ();
  115.   LONGEST low = 0;

  116.   elttype = TYPE_TARGET_TYPE (type);
  117.   eltlen = TYPE_LENGTH (check_typedef (elttype));
  118.   index_type = TYPE_INDEX_TYPE (type);

  119.   {
  120.     LONGEST high;

  121.     if (get_discrete_bounds (index_type, &low, &high) < 0)
  122.       len = 1;
  123.     else
  124.       len = high - low + 1;
  125.   }

  126.   i = 0;
  127.   annotate_array_section_begin (i, elttype);

  128.   while (i < len && things_printed < options->print_max)
  129.     {
  130.       struct value *v0, *v1;
  131.       int i0;

  132.       if (i != 0)
  133.         {
  134.           if (options->prettyformat_arrays)
  135.             {
  136.               fprintf_filtered (stream, ",\n");
  137.               print_spaces_filtered (2 + 2 * recurse, stream);
  138.             }
  139.           else
  140.             {
  141.               fprintf_filtered (stream, ", ");
  142.             }
  143.         }
  144.       wrap_here (n_spaces (2 + 2 * recurse));
  145.       maybe_print_array_index (index_type, i + low, stream, options);

  146.       i0 = i;
  147.       v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
  148.                                            (i0 * bitsize) / HOST_CHAR_BIT,
  149.                                            (i0 * bitsize) % HOST_CHAR_BIT,
  150.                                            bitsize, elttype);
  151.       while (1)
  152.         {
  153.           i += 1;
  154.           if (i >= len)
  155.             break;
  156.           v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
  157.                                                (i * bitsize) / HOST_CHAR_BIT,
  158.                                                (i * bitsize) % HOST_CHAR_BIT,
  159.                                                bitsize, elttype);
  160.           if (!value_contents_eq (v0, value_embedded_offset (v0),
  161.                                   v1, value_embedded_offset (v1),
  162.                                   eltlen))
  163.             break;
  164.         }

  165.       if (i - i0 > options->repeat_count_threshold)
  166.         {
  167.           struct value_print_options opts = *options;

  168.           opts.deref_ref = 0;
  169.           val_print (elttype, value_contents_for_printing (v0),
  170.                      value_embedded_offset (v0), 0, stream,
  171.                      recurse + 1, v0, &opts, current_language);
  172.           annotate_elt_rep (i - i0);
  173.           fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
  174.           annotate_elt_rep_end ();

  175.         }
  176.       else
  177.         {
  178.           int j;
  179.           struct value_print_options opts = *options;

  180.           opts.deref_ref = 0;
  181.           for (j = i0; j < i; j += 1)
  182.             {
  183.               if (j > i0)
  184.                 {
  185.                   if (options->prettyformat_arrays)
  186.                     {
  187.                       fprintf_filtered (stream, ",\n");
  188.                       print_spaces_filtered (2 + 2 * recurse, stream);
  189.                     }
  190.                   else
  191.                     {
  192.                       fprintf_filtered (stream, ", ");
  193.                     }
  194.                   wrap_here (n_spaces (2 + 2 * recurse));
  195.                   maybe_print_array_index (index_type, j + low,
  196.                                            stream, options);
  197.                 }
  198.               val_print (elttype, value_contents_for_printing (v0),
  199.                          value_embedded_offset (v0), 0, stream,
  200.                          recurse + 1, v0, &opts, current_language);
  201.               annotate_elt ();
  202.             }
  203.         }
  204.       things_printed += i - i0;
  205.     }
  206.   annotate_array_section_end ();
  207.   if (i < len)
  208.     {
  209.       fprintf_filtered (stream, "...");
  210.     }

  211.   value_free_to_mark (mark);
  212. }

  213. static struct type *
  214. printable_val_type (struct type *type, const gdb_byte *valaddr)
  215. {
  216.   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
  217. }

  218. /* Print the character C on STREAM as part of the contents of a literal
  219.    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
  220.    of the character.  */

  221. void
  222. ada_emit_char (int c, struct type *type, struct ui_file *stream,
  223.                int quoter, int type_len)
  224. {
  225.   /* If this character fits in the normal ASCII range, and is
  226.      a printable character, then print the character as if it was
  227.      an ASCII character, even if this is a wide character.
  228.      The UCHAR_MAX check is necessary because the isascii function
  229.      requires that its argument have a value of an unsigned char,
  230.      or EOF (EOF is obviously not printable).  */
  231.   if (c <= UCHAR_MAX && isascii (c) && isprint (c))
  232.     {
  233.       if (c == quoter && c == '"')
  234.         fprintf_filtered (stream, "\"\"");
  235.       else
  236.         fprintf_filtered (stream, "%c", c);
  237.     }
  238.   else
  239.     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
  240. }

  241. /* Character #I of STRING, given that TYPE_LEN is the size in bytes
  242.    of a character.  */

  243. static int
  244. char_at (const gdb_byte *string, int i, int type_len,
  245.          enum bfd_endian byte_order)
  246. {
  247.   if (type_len == 1)
  248.     return string[i];
  249.   else
  250.     return (int) extract_unsigned_integer (string + type_len * i,
  251.                                            type_len, byte_order);
  252. }

  253. /* Print a floating-point value of type TYPE, pointed to in GDB by
  254.    VALADDR, on STREAM.  Use Ada formatting conventions: there must be
  255.    a decimal point, and at least one digit before and after the
  256.    point.  We use the GNAT format for NaNs and infinities.  */

  257. static void
  258. ada_print_floating (const gdb_byte *valaddr, struct type *type,
  259.                     struct ui_file *stream)
  260. {
  261.   char *s, *result;
  262.   struct ui_file *tmp_stream = mem_fileopen ();
  263.   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);

  264.   print_floating (valaddr, type, tmp_stream);
  265.   result = ui_file_xstrdup (tmp_stream, NULL);
  266.   make_cleanup (xfree, result);

  267.   /* Modify for Ada rules.  */

  268.   s = strstr (result, "inf");
  269.   if (s == NULL)
  270.     s = strstr (result, "Inf");
  271.   if (s == NULL)
  272.     s = strstr (result, "INF");
  273.   if (s != NULL)
  274.     strcpy (s, "Inf");

  275.   if (s == NULL)
  276.     {
  277.       s = strstr (result, "nan");
  278.       if (s == NULL)
  279.         s = strstr (result, "NaN");
  280.       if (s == NULL)
  281.         s = strstr (result, "Nan");
  282.       if (s != NULL)
  283.         {
  284.           s[0] = s[2] = 'N';
  285.           if (result[0] == '-')
  286.             result += 1;
  287.         }
  288.     }

  289.   if (s == NULL && strchr (result, '.') == NULL)
  290.     {
  291.       s = strchr (result, 'e');
  292.       if (s == NULL)
  293.         fprintf_filtered (stream, "%s.0", result);
  294.       else
  295.         fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
  296.     }
  297.   else
  298.     fprintf_filtered (stream, "%s", result);

  299.   do_cleanups (cleanups);
  300. }

  301. void
  302. ada_printchar (int c, struct type *type, struct ui_file *stream)
  303. {
  304.   fputs_filtered ("'", stream);
  305.   ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
  306.   fputs_filtered ("'", stream);
  307. }

  308. /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
  309.    form appropriate for TYPE, if non-NULL.  If TYPE is NULL, print VAL
  310.    like a default signed integer.  */

  311. void
  312. ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
  313. {
  314.   unsigned int i;
  315.   unsigned len;

  316.   if (!type)
  317.     {
  318.       print_longest (stream, 'd', 0, val);
  319.       return;
  320.     }

  321.   type = ada_check_typedef (type);

  322.   switch (TYPE_CODE (type))
  323.     {

  324.     case TYPE_CODE_ENUM:
  325.       len = TYPE_NFIELDS (type);
  326.       for (i = 0; i < len; i++)
  327.         {
  328.           if (TYPE_FIELD_ENUMVAL (type, i) == val)
  329.             {
  330.               break;
  331.             }
  332.         }
  333.       if (i < len)
  334.         {
  335.           fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
  336.         }
  337.       else
  338.         {
  339.           print_longest (stream, 'd', 0, val);
  340.         }
  341.       break;

  342.     case TYPE_CODE_INT:
  343.       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
  344.       break;

  345.     case TYPE_CODE_CHAR:
  346.       LA_PRINT_CHAR (val, type, stream);
  347.       break;

  348.     case TYPE_CODE_BOOL:
  349.       fprintf_filtered (stream, val ? "true" : "false");
  350.       break;

  351.     case TYPE_CODE_RANGE:
  352.       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
  353.       return;

  354.     case TYPE_CODE_UNDEF:
  355.     case TYPE_CODE_PTR:
  356.     case TYPE_CODE_ARRAY:
  357.     case TYPE_CODE_STRUCT:
  358.     case TYPE_CODE_UNION:
  359.     case TYPE_CODE_FUNC:
  360.     case TYPE_CODE_FLT:
  361.     case TYPE_CODE_VOID:
  362.     case TYPE_CODE_SET:
  363.     case TYPE_CODE_STRING:
  364.     case TYPE_CODE_ERROR:
  365.     case TYPE_CODE_MEMBERPTR:
  366.     case TYPE_CODE_METHODPTR:
  367.     case TYPE_CODE_METHOD:
  368.     case TYPE_CODE_REF:
  369.       warning (_("internal error: unhandled type in ada_print_scalar"));
  370.       break;

  371.     default:
  372.       error (_("Invalid type code in symbol table."));
  373.     }
  374.   gdb_flush (stream);
  375. }

  376. /* Print the character string STRING, printing at most LENGTH characters.
  377.    Printing stops early if the number hits print_max; repeat counts
  378.    are printed as appropriate.  Print ellipses at the end if we
  379.    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
  380.    TYPE_LEN is the length (1 or 2) of the character type.  */

  381. static void
  382. printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
  383.           unsigned int length, int force_ellipses, int type_len,
  384.           const struct value_print_options *options)
  385. {
  386.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
  387.   unsigned int i;
  388.   unsigned int things_printed = 0;
  389.   int in_quotes = 0;
  390.   int need_comma = 0;

  391.   if (length == 0)
  392.     {
  393.       fputs_filtered ("\"\"", stream);
  394.       return;
  395.     }

  396.   for (i = 0; i < length && things_printed < options->print_max; i += 1)
  397.     {
  398.       /* Position of the character we are examining
  399.          to see whether it is repeated.  */
  400.       unsigned int rep1;
  401.       /* Number of repetitions we have detected so far.  */
  402.       unsigned int reps;

  403.       QUIT;

  404.       if (need_comma)
  405.         {
  406.           fputs_filtered (", ", stream);
  407.           need_comma = 0;
  408.         }

  409.       rep1 = i + 1;
  410.       reps = 1;
  411.       while (rep1 < length
  412.              && char_at (string, rep1, type_len, byte_order)
  413.                 == char_at (string, i, type_len, byte_order))
  414.         {
  415.           rep1 += 1;
  416.           reps += 1;
  417.         }

  418.       if (reps > options->repeat_count_threshold)
  419.         {
  420.           if (in_quotes)
  421.             {
  422.               fputs_filtered ("\", ", stream);
  423.               in_quotes = 0;
  424.             }
  425.           fputs_filtered ("'", stream);
  426.           ada_emit_char (char_at (string, i, type_len, byte_order),
  427.                          elttype, stream, '\'', type_len);
  428.           fputs_filtered ("'", stream);
  429.           fprintf_filtered (stream, _(" <repeats %u times>"), reps);
  430.           i = rep1 - 1;
  431.           things_printed += options->repeat_count_threshold;
  432.           need_comma = 1;
  433.         }
  434.       else
  435.         {
  436.           if (!in_quotes)
  437.             {
  438.               fputs_filtered ("\"", stream);
  439.               in_quotes = 1;
  440.             }
  441.           ada_emit_char (char_at (string, i, type_len, byte_order),
  442.                          elttype, stream, '"', type_len);
  443.           things_printed += 1;
  444.         }
  445.     }

  446.   /* Terminate the quotes if necessary.  */
  447.   if (in_quotes)
  448.     fputs_filtered ("\"", stream);

  449.   if (force_ellipses || i < length)
  450.     fputs_filtered ("...", stream);
  451. }

  452. void
  453. ada_printstr (struct ui_file *stream, struct type *type,
  454.               const gdb_byte *string, unsigned int length,
  455.               const char *encoding, int force_ellipses,
  456.               const struct value_print_options *options)
  457. {
  458.   printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
  459.             options);
  460. }

  461. static int
  462. print_variant_part (struct type *type, int field_num,
  463.                     const gdb_byte *valaddr, int offset,
  464.                     struct ui_file *stream, int recurse,
  465.                     const struct value *val,
  466.                     const struct value_print_options *options,
  467.                     int comma_needed,
  468.                     struct type *outer_type, int outer_offset,
  469.                     const struct language_defn *language)
  470. {
  471.   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
  472.   int which = ada_which_variant_applies (var_type, outer_type,
  473.                                          valaddr + outer_offset);

  474.   if (which < 0)
  475.     return 0;
  476.   else
  477.     return print_field_values
  478.       (TYPE_FIELD_TYPE (var_type, which),
  479.        valaddr,
  480.        offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
  481.        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
  482.        stream, recurse, val, options,
  483.        comma_needed, outer_type, outer_offset, language);
  484. }

  485. /* Print out fields of value at VALADDR + OFFSET having structure type TYPE.

  486.    TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
  487.    meanings as in ada_print_value and ada_val_print.

  488.    OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
  489.    record (used to get discriminant values when printing variant
  490.    parts).

  491.    COMMA_NEEDED is 1 if fields have been printed at the current recursion
  492.    level, so that a comma is needed before any field printed by this
  493.    call.

  494.    Returns 1 if COMMA_NEEDED or any fields were printed.  */

  495. static int
  496. print_field_values (struct type *type, const gdb_byte *valaddr,
  497.                     int offset, struct ui_file *stream, int recurse,
  498.                     const struct value *val,
  499.                     const struct value_print_options *options,
  500.                     int comma_needed,
  501.                     struct type *outer_type, int outer_offset,
  502.                     const struct language_defn *language)
  503. {
  504.   int i, len;

  505.   len = TYPE_NFIELDS (type);

  506.   for (i = 0; i < len; i += 1)
  507.     {
  508.       if (ada_is_ignored_field (type, i))
  509.         continue;

  510.       if (ada_is_wrapper_field (type, i))
  511.         {
  512.           comma_needed =
  513.             print_field_values (TYPE_FIELD_TYPE (type, i),
  514.                                 valaddr,
  515.                                 (offset
  516.                                  + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
  517.                                 stream, recurse, val, options,
  518.                                 comma_needed, type, offset, language);
  519.           continue;
  520.         }
  521.       else if (ada_is_variant_part (type, i))
  522.         {
  523.           comma_needed =
  524.             print_variant_part (type, i, valaddr,
  525.                                 offset, stream, recurse, val,
  526.                                 options, comma_needed,
  527.                                 outer_type, outer_offset, language);
  528.           continue;
  529.         }

  530.       if (comma_needed)
  531.         fprintf_filtered (stream, ", ");
  532.       comma_needed = 1;

  533.       if (options->prettyformat)
  534.         {
  535.           fprintf_filtered (stream, "\n");
  536.           print_spaces_filtered (2 + 2 * recurse, stream);
  537.         }
  538.       else
  539.         {
  540.           wrap_here (n_spaces (2 + 2 * recurse));
  541.         }

  542.       annotate_field_begin (TYPE_FIELD_TYPE (type, i));
  543.       fprintf_filtered (stream, "%.*s",
  544.                         ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
  545.                         TYPE_FIELD_NAME (type, i));
  546.       annotate_field_name_end ();
  547.       fputs_filtered (" => ", stream);
  548.       annotate_field_value ();

  549.       if (TYPE_FIELD_PACKED (type, i))
  550.         {
  551.           struct value *v;

  552.           /* Bitfields require special handling, especially due to byte
  553.              order problems.  */
  554.           if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
  555.             {
  556.               fputs_filtered (_("<optimized out or zero length>"), stream);
  557.             }
  558.           else
  559.             {
  560.               int bit_pos = TYPE_FIELD_BITPOS (type, i);
  561.               int bit_size = TYPE_FIELD_BITSIZE (type, i);
  562.               struct value_print_options opts;

  563.               adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
  564.               v = ada_value_primitive_packed_val
  565.                     (NULL, valaddr,
  566.                      offset + bit_pos / HOST_CHAR_BIT,
  567.                      bit_pos % HOST_CHAR_BIT,
  568.                      bit_size, TYPE_FIELD_TYPE (type, i));
  569.               opts = *options;
  570.               opts.deref_ref = 0;
  571.               val_print (TYPE_FIELD_TYPE (type, i),
  572.                          value_contents_for_printing (v),
  573.                          value_embedded_offset (v), 0,
  574.                          stream, recurse + 1, v,
  575.                          &opts, language);
  576.             }
  577.         }
  578.       else
  579.         {
  580.           struct value_print_options opts = *options;

  581.           opts.deref_ref = 0;
  582.           val_print (TYPE_FIELD_TYPE (type, i), valaddr,
  583.                      (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
  584.                      0, stream, recurse + 1, val, &opts, language);
  585.         }
  586.       annotate_field_end ();
  587.     }

  588.   return comma_needed;
  589. }

  590. /* Implement Ada val_print'ing for the case where TYPE is
  591.    a TYPE_CODE_ARRAY of characters.  */

  592. static void
  593. ada_val_print_string (struct type *type, const gdb_byte *valaddr,
  594.                       int offset, int offset_aligned, CORE_ADDR address,
  595.                       struct ui_file *stream, int recurse,
  596.                       const struct value *original_value,
  597.                       const struct value_print_options *options)
  598. {
  599.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  600.   struct type *elttype = TYPE_TARGET_TYPE (type);
  601.   unsigned int eltlen;
  602.   unsigned int len;

  603.   /* We know that ELTTYPE cannot possibly be null, because we assume
  604.      that we're called only when TYPE is a string-like type.
  605.      Similarly, the size of ELTTYPE should also be non-null, since
  606.      it's a character-like type.  */
  607.   gdb_assert (elttype != NULL);
  608.   gdb_assert (TYPE_LENGTH (elttype) != 0);

  609.   eltlen = TYPE_LENGTH (elttype);
  610.   len = TYPE_LENGTH (type) / eltlen;

  611.   if (options->prettyformat_arrays)
  612.     print_spaces_filtered (2 + 2 * recurse, stream);

  613.   /* If requested, look for the first null char and only print
  614.      elements up to it.  */
  615.   if (options->stop_print_at_null)
  616.     {
  617.       int temp_len;

  618.       /* Look for a NULL char.  */
  619.       for (temp_len = 0;
  620.            (temp_len < len
  621.             && temp_len < options->print_max
  622.             && char_at (valaddr + offset_aligned,
  623.                         temp_len, eltlen, byte_order) != 0);
  624.            temp_len += 1);
  625.       len = temp_len;
  626.     }

  627.   printstr (stream, elttype, valaddr + offset_aligned, len, 0,
  628.             eltlen, options);
  629. }

  630. /* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
  631.    thin pointers, etc).  */

  632. static void
  633. ada_val_print_gnat_array (struct type *type, const gdb_byte *valaddr,
  634.                           int offset, CORE_ADDR address,
  635.                           struct ui_file *stream, int recurse,
  636.                           const struct value *original_value,
  637.                           const struct value_print_options *options,
  638.                           const struct language_defn *language)
  639. {
  640.   struct value *mark = value_mark ();
  641.   struct value *val;

  642.   val = value_from_contents_and_address (type, valaddr + offset, address);
  643.   /* If this is a reference, coerce it now.  This helps taking care
  644.      of the case where ADDRESS is meaningless because original_value
  645.      was not an lval.  */
  646.   val = coerce_ref (val);
  647.   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)  /* array access type.  */
  648.     val = ada_coerce_to_simple_array_ptr (val);
  649.   else
  650.     val = ada_coerce_to_simple_array (val);
  651.   if (val == NULL)
  652.     {
  653.       gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
  654.       fprintf_filtered (stream, "0x0");
  655.     }
  656.   else
  657.     val_print (value_type (val), value_contents_for_printing (val),
  658.                value_embedded_offset (val), value_address (val),
  659.                stream, recurse, val, options, language);
  660.   value_free_to_mark (mark);
  661. }

  662. /* Implement Ada val_print'ing for the case where TYPE is
  663.    a TYPE_CODE_PTR.  */

  664. static void
  665. ada_val_print_ptr (struct type *type, const gdb_byte *valaddr,
  666.                    int offset, int offset_aligned, CORE_ADDR address,
  667.                    struct ui_file *stream, int recurse,
  668.                    const struct value *original_value,
  669.                    const struct value_print_options *options,
  670.                    const struct language_defn *language)
  671. {
  672.   val_print (type, valaddr, offset, address, stream, recurse,
  673.              original_value, options, language_def (language_c));

  674.   if (ada_is_tag_type (type))
  675.     {
  676.       struct value *val =
  677.         value_from_contents_and_address (type,
  678.                                          valaddr + offset_aligned,
  679.                                          address + offset_aligned);
  680.       const char *name = ada_tag_name (val);

  681.       if (name != NULL)
  682.         fprintf_filtered (stream, " (%s)", name);
  683.     }
  684. }

  685. /* Implement Ada val_print'ing for the case where TYPE is
  686.    a TYPE_CODE_INT or TYPE_CODE_RANGE.  */

  687. static void
  688. ada_val_print_num (struct type *type, const gdb_byte *valaddr,
  689.                    int offset, int offset_aligned, CORE_ADDR address,
  690.                    struct ui_file *stream, int recurse,
  691.                    const struct value *original_value,
  692.                    const struct value_print_options *options,
  693.                    const struct language_defn *language)
  694. {
  695.   if (ada_is_fixed_point_type (type))
  696.     {
  697.       LONGEST v = unpack_long (type, valaddr + offset_aligned);

  698.       fprintf_filtered (stream, TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g",
  699.                         (double) ada_fixed_to_float (type, v));
  700.       return;
  701.     }
  702.   else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
  703.     {
  704.       struct type *target_type = TYPE_TARGET_TYPE (type);

  705.       if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
  706.         {
  707.           /* Obscure case of range type that has different length from
  708.              its base type.  Perform a conversion, or we will get a
  709.              nonsense value.  Actually, we could use the same
  710.              code regardless of lengths; I'm just avoiding a cast.  */
  711.           struct value *v1
  712.             = value_from_contents_and_address (type, valaddr + offset, 0);
  713.           struct value *v = value_cast (target_type, v1);

  714.           val_print (target_type, value_contents_for_printing (v),
  715.                      value_embedded_offset (v), 0, stream,
  716.                      recurse + 1, v, options, language);
  717.         }
  718.       else
  719.         val_print (TYPE_TARGET_TYPE (type), valaddr, offset,
  720.                    address, stream, recurse, original_value,
  721.                    options, language);
  722.       return;
  723.     }
  724.   else
  725.     {
  726.       int format = (options->format ? options->format
  727.                     : options->output_format);

  728.       if (format)
  729.         {
  730.           struct value_print_options opts = *options;

  731.           opts.format = format;
  732.           val_print_scalar_formatted (type, valaddr, offset_aligned,
  733.                                       original_value, &opts, 0, stream);
  734.         }
  735.       else if (ada_is_system_address_type (type))
  736.         {
  737.           /* FIXME: We want to print System.Address variables using
  738.              the same format as for any access type.  But for some
  739.              reason GNAT encodes the System.Address type as an int,
  740.              so we have to work-around this deficiency by handling
  741.              System.Address values as a special case.  */

  742.           struct gdbarch *gdbarch = get_type_arch (type);
  743.           struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  744.           CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
  745.                                                   ptr_type);

  746.           fprintf_filtered (stream, "(");
  747.           type_print (type, "", stream, -1);
  748.           fprintf_filtered (stream, ") ");
  749.           fputs_filtered (paddress (gdbarch, addr), stream);
  750.         }
  751.       else
  752.         {
  753.           val_print_type_code_int (type, valaddr + offset_aligned, stream);
  754.           if (ada_is_character_type (type))
  755.             {
  756.               LONGEST c;

  757.               fputs_filtered (" ", stream);
  758.               c = unpack_long (type, valaddr + offset_aligned);
  759.               ada_printchar (c, type, stream);
  760.             }
  761.         }
  762.       return;
  763.     }
  764. }

  765. /* Implement Ada val_print'ing for the case where TYPE is
  766.    a TYPE_CODE_ENUM.  */

  767. static void
  768. ada_val_print_enum (struct type *type, const gdb_byte *valaddr,
  769.                     int offset, int offset_aligned, CORE_ADDR address,
  770.                     struct ui_file *stream, int recurse,
  771.                     const struct value *original_value,
  772.                     const struct value_print_options *options,
  773.                     const struct language_defn *language)
  774. {
  775.   int i;
  776.   unsigned int len;
  777.   LONGEST val;

  778.   if (options->format)
  779.     {
  780.       val_print_scalar_formatted (type, valaddr, offset_aligned,
  781.                                   original_value, options, 0, stream);
  782.       return;
  783.     }

  784.   len = TYPE_NFIELDS (type);
  785.   val = unpack_long (type, valaddr + offset_aligned);
  786.   for (i = 0; i < len; i++)
  787.     {
  788.       QUIT;
  789.       if (val == TYPE_FIELD_ENUMVAL (type, i))
  790.         break;
  791.     }

  792.   if (i < len)
  793.     {
  794.       const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));

  795.       if (name[0] == '\'')
  796.         fprintf_filtered (stream, "%ld %s", (long) val, name);
  797.       else
  798.         fputs_filtered (name, stream);
  799.     }
  800.   else
  801.     print_longest (stream, 'd', 0, val);
  802. }

  803. /* Implement Ada val_print'ing for the case where TYPE is
  804.    a TYPE_CODE_FLT.  */

  805. static void
  806. ada_val_print_flt (struct type *type, const gdb_byte *valaddr,
  807.                    int offset, int offset_aligned, CORE_ADDR address,
  808.                    struct ui_file *stream, int recurse,
  809.                    const struct value *original_value,
  810.                    const struct value_print_options *options,
  811.                    const struct language_defn *language)
  812. {
  813.   if (options->format)
  814.     {
  815.       val_print (type, valaddr, offset, address, stream, recurse,
  816.                  original_value, options, language_def (language_c));
  817.       return;
  818.     }

  819.   ada_print_floating (valaddr + offset, type, stream);
  820. }

  821. /* Implement Ada val_print'ing for the case where TYPE is
  822.    a TYPE_CODE_STRUCT or TYPE_CODE_UNION.  */

  823. static void
  824. ada_val_print_struct_union
  825.   (struct type *type, const gdb_byte *valaddr, int offset,
  826.    int offset_aligned, CORE_ADDR address, struct ui_file *stream,
  827.    int recurse, const struct value *original_value,
  828.    const struct value_print_options *options,
  829.    const struct language_defn *language)
  830. {
  831.   if (ada_is_bogus_array_descriptor (type))
  832.     {
  833.       fprintf_filtered (stream, "(...?)");
  834.       return;
  835.     }

  836.   fprintf_filtered (stream, "(");

  837.   if (print_field_values (type, valaddr, offset_aligned,
  838.                           stream, recurse, original_value, options,
  839.                           0, type, offset_aligned, language) != 0
  840.       && options->prettyformat)
  841.     {
  842.       fprintf_filtered (stream, "\n");
  843.       print_spaces_filtered (2 * recurse, stream);
  844.     }

  845.   fprintf_filtered (stream, ")");
  846. }

  847. /* Implement Ada val_print'ing for the case where TYPE is
  848.    a TYPE_CODE_ARRAY.  */

  849. static void
  850. ada_val_print_array (struct type *type, const gdb_byte *valaddr,
  851.                      int offset, int offset_aligned, CORE_ADDR address,
  852.                      struct ui_file *stream, int recurse,
  853.                      const struct value *original_value,
  854.                      const struct value_print_options *options)
  855. {
  856.   /* For an array of characters, print with string syntax.  */
  857.   if (ada_is_string_type (type)
  858.       && (options->format == 0 || options->format == 's'))
  859.     {
  860.       ada_val_print_string (type, valaddr, offset, offset_aligned,
  861.                             address, stream, recurse, original_value,
  862.                             options);
  863.       return;
  864.     }

  865.   fprintf_filtered (stream, "(");
  866.   print_optional_low_bound (stream, type, options);
  867.   if (TYPE_FIELD_BITSIZE (type, 0) > 0)
  868.     val_print_packed_array_elements (type, valaddr, offset_aligned,
  869.                                      0, stream, recurse,
  870.                                      original_value, options);
  871.   else
  872.     val_print_array_elements (type, valaddr, offset_aligned, address,
  873.                               stream, recurse, original_value,
  874.                               options, 0);
  875.   fprintf_filtered (stream, ")");
  876. }

  877. /* Implement Ada val_print'ing for the case where TYPE is
  878.    a TYPE_CODE_REF.  */

  879. static void
  880. ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
  881.                    int offset, int offset_aligned, CORE_ADDR address,
  882.                    struct ui_file *stream, int recurse,
  883.                    const struct value *original_value,
  884.                    const struct value_print_options *options,
  885.                    const struct language_defn *language)
  886. {
  887.   /* For references, the debugger is expected to print the value as
  888.      an address if DEREF_REF is null.  But printing an address in place
  889.      of the object value would be confusing to an Ada programmer.
  890.      So, for Ada values, we print the actual dereferenced value
  891.      regardless.  */
  892.   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  893.   struct value *deref_val;
  894.   CORE_ADDR deref_val_int;

  895.   if (TYPE_CODE (elttype) == TYPE_CODE_UNDEF)
  896.     {
  897.       fputs_filtered ("<ref to undefined type>", stream);
  898.       return;
  899.     }

  900.   deref_val = coerce_ref_if_computed (original_value);
  901.   if (deref_val)
  902.     {
  903.       if (ada_is_tagged_type (value_type (deref_val), 1))
  904.         deref_val = ada_tag_value_at_base_address (deref_val);

  905.       common_val_print (deref_val, stream, recurse + 1, options,
  906.                         language);
  907.       return;
  908.     }

  909.   deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
  910.   if (deref_val_int == 0)
  911.     {
  912.       fputs_filtered ("(null)", stream);
  913.       return;
  914.     }

  915.   deref_val
  916.     = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
  917.                                          deref_val_int));
  918.   if (ada_is_tagged_type (value_type (deref_val), 1))
  919.     deref_val = ada_tag_value_at_base_address (deref_val);

  920.   /* Make sure that the object does not have an unreasonable size
  921.      before trying to print it.  This can happen for instance with
  922.      references to dynamic objects whose contents is uninitialized
  923.      (Eg: an array whose bounds are not set yet).  */
  924.   ada_ensure_varsize_limit (value_type (deref_val));

  925.   val_print (value_type (deref_val),
  926.              value_contents_for_printing (deref_val),
  927.              value_embedded_offset (deref_val),
  928.              value_address (deref_val), stream, recurse + 1,
  929.              deref_val, options, language);
  930. }

  931. /* See the comment on ada_val_print.  This function differs in that it
  932.    does not catch evaluation errors (leaving that to ada_val_print).  */

  933. static void
  934. ada_val_print_1 (struct type *type, const gdb_byte *valaddr,
  935.                  int offset, CORE_ADDR address,
  936.                  struct ui_file *stream, int recurse,
  937.                  const struct value *original_value,
  938.                  const struct value_print_options *options,
  939.                  const struct language_defn *language)
  940. {
  941.   int offset_aligned;

  942.   type = ada_check_typedef (type);

  943.   if (ada_is_array_descriptor_type (type)
  944.       || (ada_is_constrained_packed_array_type (type)
  945.           && TYPE_CODE (type) != TYPE_CODE_PTR))
  946.     {
  947.       ada_val_print_gnat_array (type, valaddr, offset, address,
  948.                                 stream, recurse, original_value,
  949.                                 options, language);
  950.       return;
  951.     }

  952.   offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
  953.   type = printable_val_type (type, valaddr + offset_aligned);

  954.   switch (TYPE_CODE (type))
  955.     {
  956.     default:
  957.       val_print (type, valaddr, offset, address, stream, recurse,
  958.                  original_value, options, language_def (language_c));
  959.       break;

  960.     case TYPE_CODE_PTR:
  961.       ada_val_print_ptr (type, valaddr, offset, offset_aligned,
  962.                          address, stream, recurse, original_value,
  963.                          options, language);
  964.       break;

  965.     case TYPE_CODE_INT:
  966.     case TYPE_CODE_RANGE:
  967.       ada_val_print_num (type, valaddr, offset, offset_aligned,
  968.                          address, stream, recurse, original_value,
  969.                          options, language);
  970.       break;

  971.     case TYPE_CODE_ENUM:
  972.       ada_val_print_enum (type, valaddr, offset, offset_aligned,
  973.                           address, stream, recurse, original_value,
  974.                           options, language);
  975.       break;

  976.     case TYPE_CODE_FLT:
  977.       ada_val_print_flt (type, valaddr, offset, offset_aligned,
  978.                          address, stream, recurse, original_value,
  979.                          options, language);
  980.       break;

  981.     case TYPE_CODE_UNION:
  982.     case TYPE_CODE_STRUCT:
  983.       ada_val_print_struct_union (type, valaddr, offset, offset_aligned,
  984.                                   address, stream, recurse,
  985.                                   original_value, options, language);
  986.       break;

  987.     case TYPE_CODE_ARRAY:
  988.       ada_val_print_array (type, valaddr, offset, offset_aligned,
  989.                            address, stream, recurse, original_value,
  990.                            options);
  991.       return;

  992.     case TYPE_CODE_REF:
  993.       ada_val_print_ref (type, valaddr, offset, offset_aligned,
  994.                          address, stream, recurse, original_value,
  995.                          options, language);
  996.       break;
  997.     }
  998. }

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

  1001. void
  1002. ada_val_print (struct type *type, const gdb_byte *valaddr,
  1003.                int embedded_offset, CORE_ADDR address,
  1004.                struct ui_file *stream, int recurse,
  1005.                const struct value *val,
  1006.                const struct value_print_options *options)
  1007. {
  1008.   volatile struct gdb_exception except;

  1009.   /* XXX: this catches QUIT/ctrl-c as well.  Isn't that busted?  */
  1010.   TRY_CATCH (except, RETURN_MASK_ALL)
  1011.     {
  1012.       ada_val_print_1 (type, valaddr, embedded_offset, address,
  1013.                        stream, recurse, val, options,
  1014.                        current_language);
  1015.     }
  1016. }

  1017. void
  1018. ada_value_print (struct value *val0, struct ui_file *stream,
  1019.                  const struct value_print_options *options)
  1020. {
  1021.   struct value *val = ada_to_fixed_value (val0);
  1022.   CORE_ADDR address = value_address (val);
  1023.   struct type *type = ada_check_typedef (value_enclosing_type (val));
  1024.   struct value_print_options opts;

  1025.   /* If it is a pointer, indicate what it points to.  */
  1026.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  1027.     {
  1028.       /* Hack:  don't print (char *) for char strings.  Their
  1029.          type is indicated by the quoted string anyway.  */
  1030.       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
  1031.           || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
  1032.           || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
  1033.         {
  1034.           fprintf_filtered (stream, "(");
  1035.           type_print (type, "", stream, -1);
  1036.           fprintf_filtered (stream, ") ");
  1037.         }
  1038.     }
  1039.   else if (ada_is_array_descriptor_type (type))
  1040.     {
  1041.       /* We do not print the type description unless TYPE is an array
  1042.          access type (this is encoded by the compiler as a typedef to
  1043.          a fat pointer - hence the check against TYPE_CODE_TYPEDEF).  */
  1044.       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  1045.         {
  1046.           fprintf_filtered (stream, "(");
  1047.           type_print (type, "", stream, -1);
  1048.           fprintf_filtered (stream, ") ");
  1049.         }
  1050.     }
  1051.   else if (ada_is_bogus_array_descriptor (type))
  1052.     {
  1053.       fprintf_filtered (stream, "(");
  1054.       type_print (type, "", stream, -1);
  1055.       fprintf_filtered (stream, ") (...?)");
  1056.       return;
  1057.     }

  1058.   opts = *options;
  1059.   opts.deref_ref = 1;
  1060.   val_print (type, value_contents_for_printing (val),
  1061.              value_embedded_offset (val), address,
  1062.              stream, 0, val, &opts, current_language);
  1063. }