Home | History | Annotate | Line # | Download | only in gdb
ada-valprint.c revision 1.1.1.1
      1 /* Support for printing Ada values for GDB, the GNU debugger.
      2 
      3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include <ctype.h>
     22 #include <string.h>
     23 #include "symtab.h"
     24 #include "gdbtypes.h"
     25 #include "expression.h"
     26 #include "value.h"
     27 #include "demangle.h"
     28 #include "valprint.h"
     29 #include "language.h"
     30 #include "annotate.h"
     31 #include "ada-lang.h"
     32 #include "c-lang.h"
     33 #include "infcall.h"
     34 #include "exceptions.h"
     35 #include "objfiles.h"
     36 
     37 static int print_field_values (struct type *, const gdb_byte *,
     38 			       int,
     39 			       struct ui_file *, int,
     40 			       const struct value *,
     41 			       const struct value_print_options *,
     42 			       int, struct type *, int,
     43 			       const struct language_defn *);
     44 
     45 
     47 /* Make TYPE unsigned if its range of values includes no negatives.  */
     48 static void
     49 adjust_type_signedness (struct type *type)
     50 {
     51   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
     52       && TYPE_LOW_BOUND (type) >= 0)
     53     TYPE_UNSIGNED (type) = 1;
     54 }
     55 
     56 /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
     57    if non-standard (i.e., other than 1 for numbers, other than lower bound
     58    of index type for enumerated type).  Returns 1 if something printed,
     59    otherwise 0.  */
     60 
     61 static int
     62 print_optional_low_bound (struct ui_file *stream, struct type *type,
     63 			  const struct value_print_options *options)
     64 {
     65   struct type *index_type;
     66   LONGEST low_bound;
     67   LONGEST high_bound;
     68 
     69   if (options->print_array_indexes)
     70     return 0;
     71 
     72   if (!get_array_bounds (type, &low_bound, &high_bound))
     73     return 0;
     74 
     75   /* If this is an empty array, then don't print the lower bound.
     76      That would be confusing, because we would print the lower bound,
     77      followed by... nothing!  */
     78   if (low_bound > high_bound)
     79     return 0;
     80 
     81   index_type = TYPE_INDEX_TYPE (type);
     82 
     83   if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
     84     {
     85       /* We need to know what the base type is, in order to do the
     86          appropriate check below.  Otherwise, if this is a subrange
     87          of an enumerated type, where the underlying value of the
     88          first element is typically 0, we might test the low bound
     89          against the wrong value.  */
     90       index_type = TYPE_TARGET_TYPE (index_type);
     91     }
     92 
     93   switch (TYPE_CODE (index_type))
     94     {
     95     case TYPE_CODE_BOOL:
     96       if (low_bound == 0)
     97 	return 0;
     98       break;
     99     case TYPE_CODE_ENUM:
    100       if (low_bound == TYPE_FIELD_ENUMVAL (index_type, 0))
    101 	return 0;
    102       break;
    103     case TYPE_CODE_UNDEF:
    104       index_type = NULL;
    105       /* FALL THROUGH */
    106     default:
    107       if (low_bound == 1)
    108 	return 0;
    109       break;
    110     }
    111 
    112   ada_print_scalar (index_type, low_bound, stream);
    113   fprintf_filtered (stream, " => ");
    114   return 1;
    115 }
    116 
    117 /*  Version of val_print_array_elements for GNAT-style packed arrays.
    118     Prints elements of packed array of type TYPE at bit offset
    119     BITOFFSET from VALADDR on STREAM.  Formats according to OPTIONS and
    120     separates with commas.  RECURSE is the recursion (nesting) level.
    121     TYPE must have been decoded (as by ada_coerce_to_simple_array).  */
    122 
    123 static void
    124 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
    125 				 int offset,
    126 				 int bitoffset, struct ui_file *stream,
    127 				 int recurse,
    128 				 const struct value *val,
    129 				 const struct value_print_options *options)
    130 {
    131   unsigned int i;
    132   unsigned int things_printed = 0;
    133   unsigned len;
    134   struct type *elttype, *index_type;
    135   unsigned eltlen;
    136   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
    137   struct value *mark = value_mark ();
    138   LONGEST low = 0;
    139 
    140   elttype = TYPE_TARGET_TYPE (type);
    141   eltlen = TYPE_LENGTH (check_typedef (elttype));
    142   index_type = TYPE_INDEX_TYPE (type);
    143 
    144   {
    145     LONGEST high;
    146 
    147     if (get_discrete_bounds (index_type, &low, &high) < 0)
    148       len = 1;
    149     else
    150       len = high - low + 1;
    151   }
    152 
    153   i = 0;
    154   annotate_array_section_begin (i, elttype);
    155 
    156   while (i < len && things_printed < options->print_max)
    157     {
    158       struct value *v0, *v1;
    159       int i0;
    160 
    161       if (i != 0)
    162 	{
    163 	  if (options->prettyformat_arrays)
    164 	    {
    165 	      fprintf_filtered (stream, ",\n");
    166 	      print_spaces_filtered (2 + 2 * recurse, stream);
    167 	    }
    168 	  else
    169 	    {
    170 	      fprintf_filtered (stream, ", ");
    171 	    }
    172 	}
    173       wrap_here (n_spaces (2 + 2 * recurse));
    174       maybe_print_array_index (index_type, i + low, stream, options);
    175 
    176       i0 = i;
    177       v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
    178 					   (i0 * bitsize) / HOST_CHAR_BIT,
    179 					   (i0 * bitsize) % HOST_CHAR_BIT,
    180 					   bitsize, elttype);
    181       while (1)
    182 	{
    183 	  i += 1;
    184 	  if (i >= len)
    185 	    break;
    186 	  v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
    187 					       (i * bitsize) / HOST_CHAR_BIT,
    188 					       (i * bitsize) % HOST_CHAR_BIT,
    189 					       bitsize, elttype);
    190 	  if (!value_available_contents_eq (v0, value_embedded_offset (v0),
    191 					    v1, value_embedded_offset (v1),
    192 					    eltlen))
    193 	    break;
    194 	}
    195 
    196       if (i - i0 > options->repeat_count_threshold)
    197 	{
    198 	  struct value_print_options opts = *options;
    199 
    200 	  opts.deref_ref = 0;
    201 	  val_print (elttype, value_contents_for_printing (v0),
    202 		     value_embedded_offset (v0), 0, stream,
    203 		     recurse + 1, v0, &opts, current_language);
    204 	  annotate_elt_rep (i - i0);
    205 	  fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
    206 	  annotate_elt_rep_end ();
    207 
    208 	}
    209       else
    210 	{
    211 	  int j;
    212 	  struct value_print_options opts = *options;
    213 
    214 	  opts.deref_ref = 0;
    215 	  for (j = i0; j < i; j += 1)
    216 	    {
    217 	      if (j > i0)
    218 		{
    219 		  if (options->prettyformat_arrays)
    220 		    {
    221 		      fprintf_filtered (stream, ",\n");
    222 		      print_spaces_filtered (2 + 2 * recurse, stream);
    223 		    }
    224 		  else
    225 		    {
    226 		      fprintf_filtered (stream, ", ");
    227 		    }
    228 		  wrap_here (n_spaces (2 + 2 * recurse));
    229 		  maybe_print_array_index (index_type, j + low,
    230 					   stream, options);
    231 		}
    232 	      val_print (elttype, value_contents_for_printing (v0),
    233 			 value_embedded_offset (v0), 0, stream,
    234 			 recurse + 1, v0, &opts, current_language);
    235 	      annotate_elt ();
    236 	    }
    237 	}
    238       things_printed += i - i0;
    239     }
    240   annotate_array_section_end ();
    241   if (i < len)
    242     {
    243       fprintf_filtered (stream, "...");
    244     }
    245 
    246   value_free_to_mark (mark);
    247 }
    248 
    249 static struct type *
    250 printable_val_type (struct type *type, const gdb_byte *valaddr)
    251 {
    252   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL, 1);
    253 }
    254 
    255 /* Print the character C on STREAM as part of the contents of a literal
    256    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
    257    of the character.  */
    258 
    259 void
    260 ada_emit_char (int c, struct type *type, struct ui_file *stream,
    261 	       int quoter, int type_len)
    262 {
    263   /* If this character fits in the normal ASCII range, and is
    264      a printable character, then print the character as if it was
    265      an ASCII character, even if this is a wide character.
    266      The UCHAR_MAX check is necessary because the isascii function
    267      requires that its argument have a value of an unsigned char,
    268      or EOF (EOF is obviously not printable).  */
    269   if (c <= UCHAR_MAX && isascii (c) && isprint (c))
    270     {
    271       if (c == quoter && c == '"')
    272 	fprintf_filtered (stream, "\"\"");
    273       else
    274 	fprintf_filtered (stream, "%c", c);
    275     }
    276   else
    277     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
    278 }
    279 
    280 /* Character #I of STRING, given that TYPE_LEN is the size in bytes
    281    of a character.  */
    282 
    283 static int
    284 char_at (const gdb_byte *string, int i, int type_len,
    285 	 enum bfd_endian byte_order)
    286 {
    287   if (type_len == 1)
    288     return string[i];
    289   else
    290     return (int) extract_unsigned_integer (string + type_len * i,
    291                                            type_len, byte_order);
    292 }
    293 
    294 /* Print a floating-point value of type TYPE, pointed to in GDB by
    295    VALADDR, on STREAM.  Use Ada formatting conventions: there must be
    296    a decimal point, and at least one digit before and after the
    297    point.  We use the GNAT format for NaNs and infinities.  */
    298 
    299 static void
    300 ada_print_floating (const gdb_byte *valaddr, struct type *type,
    301 		    struct ui_file *stream)
    302 {
    303   char *s, *result;
    304   struct ui_file *tmp_stream = mem_fileopen ();
    305   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
    306 
    307   print_floating (valaddr, type, tmp_stream);
    308   result = ui_file_xstrdup (tmp_stream, NULL);
    309   make_cleanup (xfree, result);
    310 
    311   /* Modify for Ada rules.  */
    312 
    313   s = strstr (result, "inf");
    314   if (s == NULL)
    315     s = strstr (result, "Inf");
    316   if (s == NULL)
    317     s = strstr (result, "INF");
    318   if (s != NULL)
    319     strcpy (s, "Inf");
    320 
    321   if (s == NULL)
    322     {
    323       s = strstr (result, "nan");
    324       if (s == NULL)
    325 	s = strstr (result, "NaN");
    326       if (s == NULL)
    327 	s = strstr (result, "Nan");
    328       if (s != NULL)
    329 	{
    330 	  s[0] = s[2] = 'N';
    331 	  if (result[0] == '-')
    332 	    result += 1;
    333 	}
    334     }
    335 
    336   if (s == NULL && strchr (result, '.') == NULL)
    337     {
    338       s = strchr (result, 'e');
    339       if (s == NULL)
    340 	fprintf_filtered (stream, "%s.0", result);
    341       else
    342 	fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
    343     }
    344   else
    345     fprintf_filtered (stream, "%s", result);
    346 
    347   do_cleanups (cleanups);
    348 }
    349 
    350 void
    351 ada_printchar (int c, struct type *type, struct ui_file *stream)
    352 {
    353   fputs_filtered ("'", stream);
    354   ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
    355   fputs_filtered ("'", stream);
    356 }
    357 
    358 /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
    359    form appropriate for TYPE, if non-NULL.  If TYPE is NULL, print VAL
    360    like a default signed integer.  */
    361 
    362 void
    363 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
    364 {
    365   unsigned int i;
    366   unsigned len;
    367 
    368   if (!type)
    369     {
    370       print_longest (stream, 'd', 0, val);
    371       return;
    372     }
    373 
    374   type = ada_check_typedef (type);
    375 
    376   switch (TYPE_CODE (type))
    377     {
    378 
    379     case TYPE_CODE_ENUM:
    380       len = TYPE_NFIELDS (type);
    381       for (i = 0; i < len; i++)
    382 	{
    383 	  if (TYPE_FIELD_ENUMVAL (type, i) == val)
    384 	    {
    385 	      break;
    386 	    }
    387 	}
    388       if (i < len)
    389 	{
    390 	  fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
    391 	}
    392       else
    393 	{
    394 	  print_longest (stream, 'd', 0, val);
    395 	}
    396       break;
    397 
    398     case TYPE_CODE_INT:
    399       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
    400       break;
    401 
    402     case TYPE_CODE_CHAR:
    403       LA_PRINT_CHAR (val, type, stream);
    404       break;
    405 
    406     case TYPE_CODE_BOOL:
    407       fprintf_filtered (stream, val ? "true" : "false");
    408       break;
    409 
    410     case TYPE_CODE_RANGE:
    411       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
    412       return;
    413 
    414     case TYPE_CODE_UNDEF:
    415     case TYPE_CODE_PTR:
    416     case TYPE_CODE_ARRAY:
    417     case TYPE_CODE_STRUCT:
    418     case TYPE_CODE_UNION:
    419     case TYPE_CODE_FUNC:
    420     case TYPE_CODE_FLT:
    421     case TYPE_CODE_VOID:
    422     case TYPE_CODE_SET:
    423     case TYPE_CODE_STRING:
    424     case TYPE_CODE_ERROR:
    425     case TYPE_CODE_MEMBERPTR:
    426     case TYPE_CODE_METHODPTR:
    427     case TYPE_CODE_METHOD:
    428     case TYPE_CODE_REF:
    429       warning (_("internal error: unhandled type in ada_print_scalar"));
    430       break;
    431 
    432     default:
    433       error (_("Invalid type code in symbol table."));
    434     }
    435   gdb_flush (stream);
    436 }
    437 
    438 /* Print the character string STRING, printing at most LENGTH characters.
    439    Printing stops early if the number hits print_max; repeat counts
    440    are printed as appropriate.  Print ellipses at the end if we
    441    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
    442    TYPE_LEN is the length (1 or 2) of the character type.  */
    443 
    444 static void
    445 printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
    446 	  unsigned int length, int force_ellipses, int type_len,
    447 	  const struct value_print_options *options)
    448 {
    449   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
    450   unsigned int i;
    451   unsigned int things_printed = 0;
    452   int in_quotes = 0;
    453   int need_comma = 0;
    454 
    455   if (length == 0)
    456     {
    457       fputs_filtered ("\"\"", stream);
    458       return;
    459     }
    460 
    461   for (i = 0; i < length && things_printed < options->print_max; i += 1)
    462     {
    463       /* Position of the character we are examining
    464          to see whether it is repeated.  */
    465       unsigned int rep1;
    466       /* Number of repetitions we have detected so far.  */
    467       unsigned int reps;
    468 
    469       QUIT;
    470 
    471       if (need_comma)
    472 	{
    473 	  fputs_filtered (", ", stream);
    474 	  need_comma = 0;
    475 	}
    476 
    477       rep1 = i + 1;
    478       reps = 1;
    479       while (rep1 < length
    480 	     && char_at (string, rep1, type_len, byte_order)
    481 		== char_at (string, i, type_len, byte_order))
    482 	{
    483 	  rep1 += 1;
    484 	  reps += 1;
    485 	}
    486 
    487       if (reps > options->repeat_count_threshold)
    488 	{
    489 	  if (in_quotes)
    490 	    {
    491 	      fputs_filtered ("\", ", stream);
    492 	      in_quotes = 0;
    493 	    }
    494 	  fputs_filtered ("'", stream);
    495 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    496 			 elttype, stream, '\'', type_len);
    497 	  fputs_filtered ("'", stream);
    498 	  fprintf_filtered (stream, _(" <repeats %u times>"), reps);
    499 	  i = rep1 - 1;
    500 	  things_printed += options->repeat_count_threshold;
    501 	  need_comma = 1;
    502 	}
    503       else
    504 	{
    505 	  if (!in_quotes)
    506 	    {
    507 	      fputs_filtered ("\"", stream);
    508 	      in_quotes = 1;
    509 	    }
    510 	  ada_emit_char (char_at (string, i, type_len, byte_order),
    511 			 elttype, stream, '"', type_len);
    512 	  things_printed += 1;
    513 	}
    514     }
    515 
    516   /* Terminate the quotes if necessary.  */
    517   if (in_quotes)
    518     fputs_filtered ("\"", stream);
    519 
    520   if (force_ellipses || i < length)
    521     fputs_filtered ("...", stream);
    522 }
    523 
    524 void
    525 ada_printstr (struct ui_file *stream, struct type *type,
    526 	      const gdb_byte *string, unsigned int length,
    527 	      const char *encoding, int force_ellipses,
    528 	      const struct value_print_options *options)
    529 {
    530   printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
    531 	    options);
    532 }
    533 
    534 static int
    535 print_variant_part (struct type *type, int field_num,
    536 		    const gdb_byte *valaddr, int offset,
    537 		    struct ui_file *stream, int recurse,
    538 		    const struct value *val,
    539 		    const struct value_print_options *options,
    540 		    int comma_needed,
    541 		    struct type *outer_type, int outer_offset,
    542 		    const struct language_defn *language)
    543 {
    544   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
    545   int which = ada_which_variant_applies (var_type, outer_type,
    546 					 valaddr + outer_offset);
    547 
    548   if (which < 0)
    549     return 0;
    550   else
    551     return print_field_values
    552       (TYPE_FIELD_TYPE (var_type, which),
    553        valaddr,
    554        offset + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
    555        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
    556        stream, recurse, val, options,
    557        comma_needed, outer_type, outer_offset, language);
    558 }
    559 
    560 /* Print out fields of value at VALADDR + OFFSET having structure type TYPE.
    561 
    562    TYPE, VALADDR, OFFSET, STREAM, RECURSE, and OPTIONS have the same
    563    meanings as in ada_print_value and ada_val_print.
    564 
    565    OUTER_TYPE and OUTER_OFFSET give type and address of enclosing
    566    record (used to get discriminant values when printing variant
    567    parts).
    568 
    569    COMMA_NEEDED is 1 if fields have been printed at the current recursion
    570    level, so that a comma is needed before any field printed by this
    571    call.
    572 
    573    Returns 1 if COMMA_NEEDED or any fields were printed.  */
    574 
    575 static int
    576 print_field_values (struct type *type, const gdb_byte *valaddr,
    577 		    int offset, struct ui_file *stream, int recurse,
    578 		    const struct value *val,
    579 		    const struct value_print_options *options,
    580 		    int comma_needed,
    581 		    struct type *outer_type, int outer_offset,
    582 		    const struct language_defn *language)
    583 {
    584   int i, len;
    585 
    586   len = TYPE_NFIELDS (type);
    587 
    588   for (i = 0; i < len; i += 1)
    589     {
    590       if (ada_is_ignored_field (type, i))
    591 	continue;
    592 
    593       if (ada_is_wrapper_field (type, i))
    594 	{
    595 	  comma_needed =
    596 	    print_field_values (TYPE_FIELD_TYPE (type, i),
    597 				valaddr,
    598 				(offset
    599 				 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
    600 				stream, recurse, val, options,
    601 				comma_needed, type, offset, language);
    602 	  continue;
    603 	}
    604       else if (ada_is_variant_part (type, i))
    605 	{
    606 	  comma_needed =
    607 	    print_variant_part (type, i, valaddr,
    608 				offset, stream, recurse, val,
    609 				options, comma_needed,
    610 				outer_type, outer_offset, language);
    611 	  continue;
    612 	}
    613 
    614       if (comma_needed)
    615 	fprintf_filtered (stream, ", ");
    616       comma_needed = 1;
    617 
    618       if (options->prettyformat)
    619 	{
    620 	  fprintf_filtered (stream, "\n");
    621 	  print_spaces_filtered (2 + 2 * recurse, stream);
    622 	}
    623       else
    624 	{
    625 	  wrap_here (n_spaces (2 + 2 * recurse));
    626 	}
    627 
    628       annotate_field_begin (TYPE_FIELD_TYPE (type, i));
    629       fprintf_filtered (stream, "%.*s",
    630 			ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
    631 			TYPE_FIELD_NAME (type, i));
    632       annotate_field_name_end ();
    633       fputs_filtered (" => ", stream);
    634       annotate_field_value ();
    635 
    636       if (TYPE_FIELD_PACKED (type, i))
    637 	{
    638 	  struct value *v;
    639 
    640 	  /* Bitfields require special handling, especially due to byte
    641 	     order problems.  */
    642 	  if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
    643 	    {
    644 	      fputs_filtered (_("<optimized out or zero length>"), stream);
    645 	    }
    646 	  else
    647 	    {
    648 	      int bit_pos = TYPE_FIELD_BITPOS (type, i);
    649 	      int bit_size = TYPE_FIELD_BITSIZE (type, i);
    650 	      struct value_print_options opts;
    651 
    652 	      adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
    653 	      v = ada_value_primitive_packed_val
    654 		    (NULL, valaddr,
    655 		     offset + bit_pos / HOST_CHAR_BIT,
    656 		     bit_pos % HOST_CHAR_BIT,
    657 		     bit_size, TYPE_FIELD_TYPE (type, i));
    658 	      opts = *options;
    659 	      opts.deref_ref = 0;
    660 	      val_print (TYPE_FIELD_TYPE (type, i),
    661 			 value_contents_for_printing (v),
    662 			 value_embedded_offset (v), 0,
    663 			 stream, recurse + 1, v,
    664 			 &opts, language);
    665 	    }
    666 	}
    667       else
    668 	{
    669 	  struct value_print_options opts = *options;
    670 
    671 	  opts.deref_ref = 0;
    672 	  val_print (TYPE_FIELD_TYPE (type, i), valaddr,
    673 		     (offset + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT),
    674 		     0, stream, recurse + 1, val, &opts, language);
    675 	}
    676       annotate_field_end ();
    677     }
    678 
    679   return comma_needed;
    680 }
    681 
    682 /* Implement Ada val_print'ing for the case where TYPE is
    683    a TYPE_CODE_ARRAY of characters.  */
    684 
    685 static void
    686 ada_val_print_string (struct type *type, const gdb_byte *valaddr,
    687 		      int offset, int offset_aligned, CORE_ADDR address,
    688 		      struct ui_file *stream, int recurse,
    689 		      const struct value *original_value,
    690 		      const struct value_print_options *options)
    691 {
    692   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
    693   struct type *elttype = TYPE_TARGET_TYPE (type);
    694   unsigned int eltlen;
    695   unsigned int len;
    696 
    697   /* We know that ELTTYPE cannot possibly be null, because we assume
    698      that we're called only when TYPE is a string-like type.
    699      Similarly, the size of ELTTYPE should also be non-null, since
    700      it's a character-like type.  */
    701   gdb_assert (elttype != NULL);
    702   gdb_assert (TYPE_LENGTH (elttype) != 0);
    703 
    704   eltlen = TYPE_LENGTH (elttype);
    705   len = TYPE_LENGTH (type) / eltlen;
    706 
    707   if (options->prettyformat_arrays)
    708     print_spaces_filtered (2 + 2 * recurse, stream);
    709 
    710   /* If requested, look for the first null char and only print
    711      elements up to it.  */
    712   if (options->stop_print_at_null)
    713     {
    714       int temp_len;
    715 
    716       /* Look for a NULL char.  */
    717       for (temp_len = 0;
    718 	   (temp_len < len
    719 	    && temp_len < options->print_max
    720 	    && char_at (valaddr + offset_aligned,
    721 			temp_len, eltlen, byte_order) != 0);
    722 	   temp_len += 1);
    723       len = temp_len;
    724     }
    725 
    726   printstr (stream, elttype, valaddr + offset_aligned, len, 0,
    727 	    eltlen, options);
    728 }
    729 
    730 /* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
    731    thin pointers, etc).  */
    732 
    733 static void
    734 ada_val_print_gnat_array (struct type *type, const gdb_byte *valaddr,
    735 			  int offset, CORE_ADDR address,
    736 			  struct ui_file *stream, int recurse,
    737 			  const struct value *original_value,
    738 			  const struct value_print_options *options,
    739 			  const struct language_defn *language)
    740 {
    741   struct value *mark = value_mark ();
    742   struct value *val;
    743 
    744   val = value_from_contents_and_address (type, valaddr + offset, address);
    745   /* If this is a reference, coerce it now.  This helps taking care
    746      of the case where ADDRESS is meaningless because original_value
    747      was not an lval.  */
    748   val = coerce_ref (val);
    749   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)  /* array access type.  */
    750     val = ada_coerce_to_simple_array_ptr (val);
    751   else
    752     val = ada_coerce_to_simple_array (val);
    753   if (val == NULL)
    754     {
    755       gdb_assert (TYPE_CODE (type) == TYPE_CODE_TYPEDEF);
    756       fprintf_filtered (stream, "0x0");
    757     }
    758   else
    759     val_print (value_type (val), value_contents_for_printing (val),
    760 	       value_embedded_offset (val), value_address (val),
    761 	       stream, recurse, val, options, language);
    762   value_free_to_mark (mark);
    763 }
    764 
    765 /* Implement Ada val_print'ing for the case where TYPE is
    766    a TYPE_CODE_PTR.  */
    767 
    768 static void
    769 ada_val_print_ptr (struct type *type, const gdb_byte *valaddr,
    770 		   int offset, int offset_aligned, CORE_ADDR address,
    771 		   struct ui_file *stream, int recurse,
    772 		   const struct value *original_value,
    773 		   const struct value_print_options *options,
    774 		   const struct language_defn *language)
    775 {
    776   val_print (type, valaddr, offset, address, stream, recurse,
    777 	     original_value, options, language_def (language_c));
    778 
    779   if (ada_is_tag_type (type))
    780     {
    781       struct value *val =
    782 	value_from_contents_and_address (type,
    783 					 valaddr + offset_aligned,
    784 					 address + offset_aligned);
    785       const char *name = ada_tag_name (val);
    786 
    787       if (name != NULL)
    788 	fprintf_filtered (stream, " (%s)", name);
    789     }
    790 }
    791 
    792 /* Implement Ada val_print'ing for the case where TYPE is
    793    a TYPE_CODE_INT or TYPE_CODE_RANGE.  */
    794 
    795 static void
    796 ada_val_print_num (struct type *type, const gdb_byte *valaddr,
    797 		   int offset, int offset_aligned, CORE_ADDR address,
    798 		   struct ui_file *stream, int recurse,
    799 		   const struct value *original_value,
    800 		   const struct value_print_options *options,
    801 		   const struct language_defn *language)
    802 {
    803   if (ada_is_fixed_point_type (type))
    804     {
    805       LONGEST v = unpack_long (type, valaddr + offset_aligned);
    806 
    807       fprintf_filtered (stream, TYPE_LENGTH (type) < 4 ? "%.11g" : "%.17g",
    808 			(double) ada_fixed_to_float (type, v));
    809       return;
    810     }
    811   else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
    812     {
    813       struct type *target_type = TYPE_TARGET_TYPE (type);
    814 
    815       if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
    816 	{
    817 	  /* Obscure case of range type that has different length from
    818 	     its base type.  Perform a conversion, or we will get a
    819 	     nonsense value.  Actually, we could use the same
    820 	     code regardless of lengths; I'm just avoiding a cast.  */
    821 	  struct value *v1
    822 	    = value_from_contents_and_address (type, valaddr + offset, 0);
    823 	  struct value *v = value_cast (target_type, v1);
    824 
    825 	  val_print (target_type, value_contents_for_printing (v),
    826 		     value_embedded_offset (v), 0, stream,
    827 		     recurse + 1, v, options, language);
    828 	}
    829       else
    830 	val_print (TYPE_TARGET_TYPE (type), valaddr, offset,
    831 		   address, stream, recurse, original_value,
    832 		   options, language);
    833       return;
    834     }
    835   else
    836     {
    837       int format = (options->format ? options->format
    838 		    : options->output_format);
    839 
    840       if (format)
    841 	{
    842 	  struct value_print_options opts = *options;
    843 
    844 	  opts.format = format;
    845 	  val_print_scalar_formatted (type, valaddr, offset_aligned,
    846 				      original_value, &opts, 0, stream);
    847 	}
    848       else if (ada_is_system_address_type (type))
    849 	{
    850 	  /* FIXME: We want to print System.Address variables using
    851 	     the same format as for any access type.  But for some
    852 	     reason GNAT encodes the System.Address type as an int,
    853 	     so we have to work-around this deficiency by handling
    854 	     System.Address values as a special case.  */
    855 
    856 	  struct gdbarch *gdbarch = get_type_arch (type);
    857 	  struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
    858 	  CORE_ADDR addr = extract_typed_address (valaddr + offset_aligned,
    859 						  ptr_type);
    860 
    861 	  fprintf_filtered (stream, "(");
    862 	  type_print (type, "", stream, -1);
    863 	  fprintf_filtered (stream, ") ");
    864 	  fputs_filtered (paddress (gdbarch, addr), stream);
    865 	}
    866       else
    867 	{
    868 	  val_print_type_code_int (type, valaddr + offset_aligned, stream);
    869 	  if (ada_is_character_type (type))
    870 	    {
    871 	      LONGEST c;
    872 
    873 	      fputs_filtered (" ", stream);
    874 	      c = unpack_long (type, valaddr + offset_aligned);
    875 	      ada_printchar (c, type, stream);
    876 	    }
    877 	}
    878       return;
    879     }
    880 }
    881 
    882 /* Implement Ada val_print'ing for the case where TYPE is
    883    a TYPE_CODE_ENUM.  */
    884 
    885 static void
    886 ada_val_print_enum (struct type *type, const gdb_byte *valaddr,
    887 		    int offset, int offset_aligned, CORE_ADDR address,
    888 		    struct ui_file *stream, int recurse,
    889 		    const struct value *original_value,
    890 		    const struct value_print_options *options,
    891 		    const struct language_defn *language)
    892 {
    893   int i;
    894   unsigned int len;
    895   LONGEST val;
    896 
    897   if (options->format)
    898     {
    899       val_print_scalar_formatted (type, valaddr, offset_aligned,
    900 				  original_value, options, 0, stream);
    901       return;
    902     }
    903 
    904   len = TYPE_NFIELDS (type);
    905   val = unpack_long (type, valaddr + offset_aligned);
    906   for (i = 0; i < len; i++)
    907     {
    908       QUIT;
    909       if (val == TYPE_FIELD_ENUMVAL (type, i))
    910 	break;
    911     }
    912 
    913   if (i < len)
    914     {
    915       const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
    916 
    917       if (name[0] == '\'')
    918 	fprintf_filtered (stream, "%ld %s", (long) val, name);
    919       else
    920 	fputs_filtered (name, stream);
    921     }
    922   else
    923     print_longest (stream, 'd', 0, val);
    924 }
    925 
    926 /* Implement Ada val_print'ing for the case where TYPE is
    927    a TYPE_CODE_FLT.  */
    928 
    929 static void
    930 ada_val_print_flt (struct type *type, const gdb_byte *valaddr,
    931 		   int offset, int offset_aligned, CORE_ADDR address,
    932 		   struct ui_file *stream, int recurse,
    933 		   const struct value *original_value,
    934 		   const struct value_print_options *options,
    935 		   const struct language_defn *language)
    936 {
    937   if (options->format)
    938     {
    939       val_print (type, valaddr, offset, address, stream, recurse,
    940 		 original_value, options, language_def (language_c));
    941       return;
    942     }
    943 
    944   ada_print_floating (valaddr + offset, type, stream);
    945 }
    946 
    947 /* Implement Ada val_print'ing for the case where TYPE is
    948    a TYPE_CODE_STRUCT or TYPE_CODE_UNION.  */
    949 
    950 static void
    951 ada_val_print_struct_union
    952   (struct type *type, const gdb_byte *valaddr, int offset,
    953    int offset_aligned, CORE_ADDR address, struct ui_file *stream,
    954    int recurse, const struct value *original_value,
    955    const struct value_print_options *options,
    956    const struct language_defn *language)
    957 {
    958   if (ada_is_bogus_array_descriptor (type))
    959     {
    960       fprintf_filtered (stream, "(...?)");
    961       return;
    962     }
    963 
    964   fprintf_filtered (stream, "(");
    965 
    966   if (print_field_values (type, valaddr, offset_aligned,
    967 			  stream, recurse, original_value, options,
    968 			  0, type, offset_aligned, language) != 0
    969       && options->prettyformat)
    970     {
    971       fprintf_filtered (stream, "\n");
    972       print_spaces_filtered (2 * recurse, stream);
    973     }
    974 
    975   fprintf_filtered (stream, ")");
    976 }
    977 
    978 /* Implement Ada val_print'ing for the case where TYPE is
    979    a TYPE_CODE_ARRAY.  */
    980 
    981 static void
    982 ada_val_print_array (struct type *type, const gdb_byte *valaddr,
    983 		     int offset, int offset_aligned, CORE_ADDR address,
    984 		     struct ui_file *stream, int recurse,
    985 		     const struct value *original_value,
    986 		     const struct value_print_options *options)
    987 {
    988   /* For an array of characters, print with string syntax.  */
    989   if (ada_is_string_type (type)
    990       && (options->format == 0 || options->format == 's'))
    991     {
    992       ada_val_print_string (type, valaddr, offset, offset_aligned,
    993 			    address, stream, recurse, original_value,
    994 			    options);
    995       return;
    996     }
    997 
    998   fprintf_filtered (stream, "(");
    999   print_optional_low_bound (stream, type, options);
   1000   if (TYPE_FIELD_BITSIZE (type, 0) > 0)
   1001     val_print_packed_array_elements (type, valaddr, offset_aligned,
   1002 				     0, stream, recurse,
   1003 				     original_value, options);
   1004   else
   1005     val_print_array_elements (type, valaddr, offset_aligned, address,
   1006 			      stream, recurse, original_value,
   1007 			      options, 0);
   1008   fprintf_filtered (stream, ")");
   1009 }
   1010 
   1011 /* Implement Ada val_print'ing for the case where TYPE is
   1012    a TYPE_CODE_REF.  */
   1013 
   1014 static void
   1015 ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
   1016 		   int offset, int offset_aligned, CORE_ADDR address,
   1017 		   struct ui_file *stream, int recurse,
   1018 		   const struct value *original_value,
   1019 		   const struct value_print_options *options,
   1020 		   const struct language_defn *language)
   1021 {
   1022   /* For references, the debugger is expected to print the value as
   1023      an address if DEREF_REF is null.  But printing an address in place
   1024      of the object value would be confusing to an Ada programmer.
   1025      So, for Ada values, we print the actual dereferenced value
   1026      regardless.  */
   1027   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
   1028   struct value *deref_val;
   1029   CORE_ADDR deref_val_int;
   1030 
   1031   if (TYPE_CODE (elttype) == TYPE_CODE_UNDEF)
   1032     {
   1033       fputs_filtered ("<ref to undefined type>", stream);
   1034       return;
   1035     }
   1036 
   1037   deref_val = coerce_ref_if_computed (original_value);
   1038   if (deref_val)
   1039     {
   1040       if (ada_is_tagged_type (value_type (deref_val), 1))
   1041 	deref_val = ada_tag_value_at_base_address (deref_val);
   1042 
   1043       common_val_print (deref_val, stream, recurse + 1, options,
   1044 			language);
   1045       return;
   1046     }
   1047 
   1048   deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
   1049   if (deref_val_int == 0)
   1050     {
   1051       fputs_filtered ("(null)", stream);
   1052       return;
   1053     }
   1054 
   1055   deref_val
   1056     = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
   1057 					 deref_val_int));
   1058   if (ada_is_tagged_type (value_type (deref_val), 1))
   1059     deref_val = ada_tag_value_at_base_address (deref_val);
   1060 
   1061   val_print (value_type (deref_val),
   1062 	     value_contents_for_printing (deref_val),
   1063 	     value_embedded_offset (deref_val),
   1064 	     value_address (deref_val), stream, recurse + 1,
   1065 	     deref_val, options, language);
   1066 }
   1067 
   1068 /* See the comment on ada_val_print.  This function differs in that it
   1069    does not catch evaluation errors (leaving that to ada_val_print).  */
   1070 
   1071 static void
   1072 ada_val_print_1 (struct type *type, const gdb_byte *valaddr,
   1073 		 int offset, CORE_ADDR address,
   1074 		 struct ui_file *stream, int recurse,
   1075 		 const struct value *original_value,
   1076 		 const struct value_print_options *options,
   1077 		 const struct language_defn *language)
   1078 {
   1079   int offset_aligned;
   1080 
   1081   type = ada_check_typedef (type);
   1082 
   1083   if (ada_is_array_descriptor_type (type)
   1084       || (ada_is_constrained_packed_array_type (type)
   1085 	  && TYPE_CODE (type) != TYPE_CODE_PTR))
   1086     {
   1087       ada_val_print_gnat_array (type, valaddr, offset, address,
   1088 				stream, recurse, original_value,
   1089 				options, language);
   1090       return;
   1091     }
   1092 
   1093   offset_aligned = offset + ada_aligned_value_addr (type, valaddr) - valaddr;
   1094   type = printable_val_type (type, valaddr + offset_aligned);
   1095 
   1096   switch (TYPE_CODE (type))
   1097     {
   1098     default:
   1099       val_print (type, valaddr, offset, address, stream, recurse,
   1100 		 original_value, options, language_def (language_c));
   1101       break;
   1102 
   1103     case TYPE_CODE_PTR:
   1104       ada_val_print_ptr (type, valaddr, offset, offset_aligned,
   1105 			 address, stream, recurse, original_value,
   1106 			 options, language);
   1107       break;
   1108 
   1109     case TYPE_CODE_INT:
   1110     case TYPE_CODE_RANGE:
   1111       ada_val_print_num (type, valaddr, offset, offset_aligned,
   1112 			 address, stream, recurse, original_value,
   1113 			 options, language);
   1114       break;
   1115 
   1116     case TYPE_CODE_ENUM:
   1117       ada_val_print_enum (type, valaddr, offset, offset_aligned,
   1118 			  address, stream, recurse, original_value,
   1119 			  options, language);
   1120       break;
   1121 
   1122     case TYPE_CODE_FLT:
   1123       ada_val_print_flt (type, valaddr, offset, offset_aligned,
   1124 			 address, stream, recurse, original_value,
   1125 			 options, language);
   1126       break;
   1127 
   1128     case TYPE_CODE_UNION:
   1129     case TYPE_CODE_STRUCT:
   1130       ada_val_print_struct_union (type, valaddr, offset, offset_aligned,
   1131 				  address, stream, recurse,
   1132 				  original_value, options, language);
   1133       break;
   1134 
   1135     case TYPE_CODE_ARRAY:
   1136       ada_val_print_array (type, valaddr, offset, offset_aligned,
   1137 			   address, stream, recurse, original_value,
   1138 			   options);
   1139       return;
   1140 
   1141     case TYPE_CODE_REF:
   1142       ada_val_print_ref (type, valaddr, offset, offset_aligned,
   1143 			 address, stream, recurse, original_value,
   1144 			 options, language);
   1145       break;
   1146     }
   1147 }
   1148 
   1149 /* See val_print for a description of the various parameters of this
   1150    function; they are identical.  */
   1151 
   1152 void
   1153 ada_val_print (struct type *type, const gdb_byte *valaddr,
   1154 	       int embedded_offset, CORE_ADDR address,
   1155 	       struct ui_file *stream, int recurse,
   1156 	       const struct value *val,
   1157 	       const struct value_print_options *options)
   1158 {
   1159   volatile struct gdb_exception except;
   1160 
   1161   /* XXX: this catches QUIT/ctrl-c as well.  Isn't that busted?  */
   1162   TRY_CATCH (except, RETURN_MASK_ALL)
   1163     {
   1164       ada_val_print_1 (type, valaddr, embedded_offset, address,
   1165 		       stream, recurse, val, options,
   1166 		       current_language);
   1167     }
   1168 }
   1169 
   1170 void
   1171 ada_value_print (struct value *val0, struct ui_file *stream,
   1172 		 const struct value_print_options *options)
   1173 {
   1174   struct value *val = ada_to_fixed_value (val0);
   1175   CORE_ADDR address = value_address (val);
   1176   struct type *type = ada_check_typedef (value_type (val));
   1177   struct value_print_options opts;
   1178 
   1179   /* If it is a pointer, indicate what it points to.  */
   1180   if (TYPE_CODE (type) == TYPE_CODE_PTR)
   1181     {
   1182       /* Hack:  don't print (char *) for char strings.  Their
   1183          type is indicated by the quoted string anyway.  */
   1184       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
   1185 	  || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
   1186 	  || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
   1187 	{
   1188 	  fprintf_filtered (stream, "(");
   1189 	  type_print (type, "", stream, -1);
   1190 	  fprintf_filtered (stream, ") ");
   1191 	}
   1192     }
   1193   else if (ada_is_array_descriptor_type (type))
   1194     {
   1195       /* We do not print the type description unless TYPE is an array
   1196 	 access type (this is encoded by the compiler as a typedef to
   1197 	 a fat pointer - hence the check against TYPE_CODE_TYPEDEF).  */
   1198       if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
   1199         {
   1200 	  fprintf_filtered (stream, "(");
   1201 	  type_print (type, "", stream, -1);
   1202 	  fprintf_filtered (stream, ") ");
   1203 	}
   1204     }
   1205   else if (ada_is_bogus_array_descriptor (type))
   1206     {
   1207       fprintf_filtered (stream, "(");
   1208       type_print (type, "", stream, -1);
   1209       fprintf_filtered (stream, ") (...?)");
   1210       return;
   1211     }
   1212 
   1213   opts = *options;
   1214   opts.deref_ref = 1;
   1215   val_print (type, value_contents_for_printing (val),
   1216 	     value_embedded_offset (val), address,
   1217 	     stream, 0, val, &opts, current_language);
   1218 }
   1219