1 /* Support for printing Ada values for GDB, the GNU debugger.
2 
3    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1997, 2001,
4    2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 
6 This file is part of GDB.
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21 
22 #include <ctype.h>
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "demangle.h"
30 #include "valprint.h"
31 #include "language.h"
32 #include "annotate.h"
33 #include "ada-lang.h"
34 #include "c-lang.h"
35 #include "infcall.h"
36 #include "exceptions.h"
37 
38 /* Encapsulates arguments to ada_val_print.  */
39 struct ada_val_print_args
40 {
41   struct type *type;
42   const gdb_byte *valaddr0;
43   int embedded_offset;
44   CORE_ADDR address;
45   struct ui_file *stream;
46   int format;
47   int deref_ref;
48   int recurse;
49   enum val_prettyprint pretty;
50 };
51 
52 static void print_record (struct type *, const gdb_byte *, struct ui_file *,
53 			  int, int, enum val_prettyprint);
54 
55 static int print_field_values (struct type *, const gdb_byte *,
56 			       struct ui_file *, int, int,
57 			       enum val_prettyprint, int, struct type *,
58 			       const gdb_byte *);
59 
60 static void adjust_type_signedness (struct type *);
61 
62 static int ada_val_print_stub (void *args0);
63 
64 static int ada_val_print_1 (struct type *, const gdb_byte *, int, CORE_ADDR,
65 			    struct ui_file *, int, int, int,
66 			    enum val_prettyprint);
67 
68 
69 /* Make TYPE unsigned if its range of values includes no negatives.  */
70 static void
adjust_type_signedness(struct type * type)71 adjust_type_signedness (struct type *type)
72 {
73   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
74       && TYPE_LOW_BOUND (type) >= 0)
75     TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
76 }
77 
78 /* Assuming TYPE is a simple, non-empty array type, prints its lower bound
79    on STREAM, if non-standard (i.e., other than 1 for numbers, other
80    than lower bound of index type for enumerated type).  Returns 1
81    if something printed, otherwise 0.  */
82 
83 static int
print_optional_low_bound(struct ui_file * stream,struct type * type)84 print_optional_low_bound (struct ui_file *stream, struct type *type)
85 {
86   struct type *index_type;
87   long low_bound;
88 
89   index_type = TYPE_INDEX_TYPE (type);
90   low_bound = 0;
91 
92   if (index_type == NULL)
93     return 0;
94   if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
95     {
96       low_bound = TYPE_LOW_BOUND (index_type);
97       if (low_bound > TYPE_HIGH_BOUND (index_type))
98 	return 0;
99       index_type = TYPE_TARGET_TYPE (index_type);
100     }
101   else
102     return 0;
103 
104   switch (TYPE_CODE (index_type))
105     {
106     case TYPE_CODE_ENUM:
107       if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
108 	return 0;
109       break;
110     case TYPE_CODE_UNDEF:
111       index_type = builtin_type_long;
112       /* FALL THROUGH */
113     default:
114       if (low_bound == 1)
115 	return 0;
116       break;
117     }
118 
119   ada_print_scalar (index_type, (LONGEST) low_bound, stream);
120   fprintf_filtered (stream, " => ");
121   return 1;
122 }
123 
124 /*  Version of val_print_array_elements for GNAT-style packed arrays.
125     Prints elements of packed array of type TYPE at bit offset
126     BITOFFSET from VALADDR on STREAM.  Formats according to FORMAT and
127     separates with commas.  RECURSE is the recursion (nesting) level.
128     If PRETTY, uses "prettier" format.  TYPE must have been decoded (as
129     by ada_coerce_to_simple_array).  */
130 
131 static void
val_print_packed_array_elements(struct type * type,const gdb_byte * valaddr,int bitoffset,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)132 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
133 				 int bitoffset, struct ui_file *stream,
134 				 int format, int recurse,
135 				 enum val_prettyprint pretty)
136 {
137   unsigned int i;
138   unsigned int things_printed = 0;
139   unsigned len;
140   struct type *elttype;
141   unsigned eltlen;
142   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
143   struct value *mark = value_mark ();
144 
145   elttype = TYPE_TARGET_TYPE (type);
146   eltlen = TYPE_LENGTH (check_typedef (elttype));
147 
148   {
149     LONGEST low, high;
150     if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
151       len = 1;
152     else
153       len = high - low + 1;
154   }
155 
156   i = 0;
157   annotate_array_section_begin (i, elttype);
158 
159   while (i < len && things_printed < print_max)
160     {
161       struct value *v0, *v1;
162       int i0;
163 
164       if (i != 0)
165 	{
166 	  if (prettyprint_arrays)
167 	    {
168 	      fprintf_filtered (stream, ",\n");
169 	      print_spaces_filtered (2 + 2 * recurse, stream);
170 	    }
171 	  else
172 	    {
173 	      fprintf_filtered (stream, ", ");
174 	    }
175 	}
176       wrap_here (n_spaces (2 + 2 * recurse));
177 
178       i0 = i;
179       v0 = ada_value_primitive_packed_val (NULL, valaddr,
180 					   (i0 * bitsize) / HOST_CHAR_BIT,
181 					   (i0 * bitsize) % HOST_CHAR_BIT,
182 					   bitsize, elttype);
183       while (1)
184 	{
185 	  i += 1;
186 	  if (i >= len)
187 	    break;
188 	  v1 = ada_value_primitive_packed_val (NULL, valaddr,
189 					       (i * bitsize) / HOST_CHAR_BIT,
190 					       (i * bitsize) % HOST_CHAR_BIT,
191 					       bitsize, elttype);
192 	  if (memcmp (value_contents (v0), value_contents (v1), eltlen) != 0)
193 	    break;
194 	}
195 
196       if (i - i0 > repeat_count_threshold)
197 	{
198 	  val_print (elttype, value_contents (v0), 0, 0, stream, format,
199 		     0, recurse + 1, pretty);
200 	  annotate_elt_rep (i - i0);
201 	  fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
202 	  annotate_elt_rep_end ();
203 
204 	}
205       else
206 	{
207 	  int j;
208 	  for (j = i0; j < i; j += 1)
209 	    {
210 	      if (j > i0)
211 		{
212 		  if (prettyprint_arrays)
213 		    {
214 		      fprintf_filtered (stream, ",\n");
215 		      print_spaces_filtered (2 + 2 * recurse, stream);
216 		    }
217 		  else
218 		    {
219 		      fprintf_filtered (stream, ", ");
220 		    }
221 		  wrap_here (n_spaces (2 + 2 * recurse));
222 		}
223 	      val_print (elttype, value_contents (v0), 0, 0, stream, format,
224 			 0, recurse + 1, pretty);
225 	      annotate_elt ();
226 	    }
227 	}
228       things_printed += i - i0;
229     }
230   annotate_array_section_end ();
231   if (i < len)
232     {
233       fprintf_filtered (stream, "...");
234     }
235 
236   value_free_to_mark (mark);
237 }
238 
239 static struct type *
printable_val_type(struct type * type,const gdb_byte * valaddr)240 printable_val_type (struct type *type, const gdb_byte *valaddr)
241 {
242   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL);
243 }
244 
245 /* Print the character C on STREAM as part of the contents of a literal
246    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
247    (1 or 2) of the character.  */
248 
249 void
ada_emit_char(int c,struct ui_file * stream,int quoter,int type_len)250 ada_emit_char (int c, struct ui_file *stream, int quoter, int type_len)
251 {
252   if (type_len != 2)
253     type_len = 1;
254 
255   c &= (1 << (type_len * TARGET_CHAR_BIT)) - 1;
256 
257   if (isascii (c) && isprint (c))
258     {
259       if (c == quoter && c == '"')
260 	fprintf_filtered (stream, "[\"%c\"]", quoter);
261       else
262 	fprintf_filtered (stream, "%c", c);
263     }
264   else
265     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
266 }
267 
268 /* Character #I of STRING, given that TYPE_LEN is the size in bytes (1
269    or 2) of a character.  */
270 
271 static int
char_at(const gdb_byte * string,int i,int type_len)272 char_at (const gdb_byte *string, int i, int type_len)
273 {
274   if (type_len == 1)
275     return string[i];
276   else
277     return (int) extract_unsigned_integer (string + 2 * i, 2);
278 }
279 
280 /* Wrapper around memcpy to make it legal argument to ui_file_put */
281 static void
ui_memcpy(void * dest,const char * buffer,long len)282 ui_memcpy (void *dest, const char *buffer, long len)
283 {
284   memcpy (dest, buffer, (size_t) len);
285   ((char *) dest)[len] = '\0';
286 }
287 
288 /* Print a floating-point value of type TYPE, pointed to in GDB by
289    VALADDR, on STREAM.  Use Ada formatting conventions: there must be
290    a decimal point, and at least one digit before and after the
291    point.  We use GNAT format for NaNs and infinities.  */
292 static void
ada_print_floating(const gdb_byte * valaddr,struct type * type,struct ui_file * stream)293 ada_print_floating (const gdb_byte *valaddr, struct type *type,
294 		    struct ui_file *stream)
295 {
296   char buffer[64];
297   char *s, *result;
298   int len;
299   struct ui_file *tmp_stream = mem_fileopen ();
300   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
301 
302   print_floating (valaddr, type, tmp_stream);
303   ui_file_put (tmp_stream, ui_memcpy, buffer);
304   do_cleanups (cleanups);
305 
306   result = buffer;
307   len = strlen (result);
308 
309   /* Modify for Ada rules.  */
310 
311   s = strstr (result, "inf");
312   if (s == NULL)
313     s = strstr (result, "Inf");
314   if (s == NULL)
315     s = strstr (result, "INF");
316   if (s != NULL)
317     strcpy (s, "Inf");
318 
319   if (s == NULL)
320     {
321       s = strstr (result, "nan");
322       if (s == NULL)
323 	s = strstr (result, "NaN");
324       if (s == NULL)
325 	s = strstr (result, "Nan");
326       if (s != NULL)
327 	{
328 	  s[0] = s[2] = 'N';
329 	  if (result[0] == '-')
330 	    result += 1;
331 	}
332     }
333 
334   if (s == NULL && strchr (result, '.') == NULL)
335     {
336       s = strchr (result, 'e');
337       if (s == NULL)
338 	fprintf_filtered (stream, "%s.0", result);
339       else
340 	fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
341       return;
342     }
343   fprintf_filtered (stream, "%s", result);
344 }
345 
346 void
ada_printchar(int c,struct ui_file * stream)347 ada_printchar (int c, struct ui_file *stream)
348 {
349   fputs_filtered ("'", stream);
350   ada_emit_char (c, stream, '\'', 1);
351   fputs_filtered ("'", stream);
352 }
353 
354 /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
355    form appropriate for TYPE.  */
356 
357 void
ada_print_scalar(struct type * type,LONGEST val,struct ui_file * stream)358 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
359 {
360   unsigned int i;
361   unsigned len;
362 
363   type = ada_check_typedef (type);
364 
365   switch (TYPE_CODE (type))
366     {
367 
368     case TYPE_CODE_ENUM:
369       len = TYPE_NFIELDS (type);
370       for (i = 0; i < len; i++)
371 	{
372 	  if (TYPE_FIELD_BITPOS (type, i) == val)
373 	    {
374 	      break;
375 	    }
376 	}
377       if (i < len)
378 	{
379 	  fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
380 	}
381       else
382 	{
383 	  print_longest (stream, 'd', 0, val);
384 	}
385       break;
386 
387     case TYPE_CODE_INT:
388       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
389       break;
390 
391     case TYPE_CODE_CHAR:
392       LA_PRINT_CHAR ((unsigned char) val, stream);
393       break;
394 
395     case TYPE_CODE_BOOL:
396       fprintf_filtered (stream, val ? "true" : "false");
397       break;
398 
399     case TYPE_CODE_RANGE:
400       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
401       return;
402 
403     case TYPE_CODE_UNDEF:
404     case TYPE_CODE_PTR:
405     case TYPE_CODE_ARRAY:
406     case TYPE_CODE_STRUCT:
407     case TYPE_CODE_UNION:
408     case TYPE_CODE_FUNC:
409     case TYPE_CODE_FLT:
410     case TYPE_CODE_VOID:
411     case TYPE_CODE_SET:
412     case TYPE_CODE_STRING:
413     case TYPE_CODE_ERROR:
414     case TYPE_CODE_MEMBER:
415     case TYPE_CODE_METHOD:
416     case TYPE_CODE_REF:
417       warning (_("internal error: unhandled type in ada_print_scalar"));
418       break;
419 
420     default:
421       error (_("Invalid type code in symbol table."));
422     }
423   gdb_flush (stream);
424 }
425 
426 /* Print the character string STRING, printing at most LENGTH characters.
427    Printing stops early if the number hits print_max; repeat counts
428    are printed as appropriate.  Print ellipses at the end if we
429    had to stop before printing LENGTH characters, or if
430    FORCE_ELLIPSES.   TYPE_LEN is the length (1 or 2) of the character type.
431  */
432 
433 static void
printstr(struct ui_file * stream,const gdb_byte * string,unsigned int length,int force_ellipses,int type_len)434 printstr (struct ui_file *stream, const gdb_byte *string,
435 	  unsigned int length, int force_ellipses, int type_len)
436 {
437   unsigned int i;
438   unsigned int things_printed = 0;
439   int in_quotes = 0;
440   int need_comma = 0;
441 
442   if (length == 0)
443     {
444       fputs_filtered ("\"\"", stream);
445       return;
446     }
447 
448   for (i = 0; i < length && things_printed < print_max; i += 1)
449     {
450       /* Position of the character we are examining
451          to see whether it is repeated.  */
452       unsigned int rep1;
453       /* Number of repetitions we have detected so far.  */
454       unsigned int reps;
455 
456       QUIT;
457 
458       if (need_comma)
459 	{
460 	  fputs_filtered (", ", stream);
461 	  need_comma = 0;
462 	}
463 
464       rep1 = i + 1;
465       reps = 1;
466       while (rep1 < length
467 	     && char_at (string, rep1, type_len) == char_at (string, i,
468 							     type_len))
469 	{
470 	  rep1 += 1;
471 	  reps += 1;
472 	}
473 
474       if (reps > repeat_count_threshold)
475 	{
476 	  if (in_quotes)
477 	    {
478 	      if (inspect_it)
479 		fputs_filtered ("\\\", ", stream);
480 	      else
481 		fputs_filtered ("\", ", stream);
482 	      in_quotes = 0;
483 	    }
484 	  fputs_filtered ("'", stream);
485 	  ada_emit_char (char_at (string, i, type_len), stream, '\'',
486 			 type_len);
487 	  fputs_filtered ("'", stream);
488 	  fprintf_filtered (stream, _(" <repeats %u times>"), reps);
489 	  i = rep1 - 1;
490 	  things_printed += repeat_count_threshold;
491 	  need_comma = 1;
492 	}
493       else
494 	{
495 	  if (!in_quotes)
496 	    {
497 	      if (inspect_it)
498 		fputs_filtered ("\\\"", stream);
499 	      else
500 		fputs_filtered ("\"", stream);
501 	      in_quotes = 1;
502 	    }
503 	  ada_emit_char (char_at (string, i, type_len), stream, '"',
504 			 type_len);
505 	  things_printed += 1;
506 	}
507     }
508 
509   /* Terminate the quotes if necessary.  */
510   if (in_quotes)
511     {
512       if (inspect_it)
513 	fputs_filtered ("\\\"", stream);
514       else
515 	fputs_filtered ("\"", stream);
516     }
517 
518   if (force_ellipses || i < length)
519     fputs_filtered ("...", stream);
520 }
521 
522 void
ada_printstr(struct ui_file * stream,const gdb_byte * string,unsigned int length,int width,int force_ellipses)523 ada_printstr (struct ui_file *stream, const gdb_byte *string,
524 	      unsigned int length, int width, int force_ellipses)
525 {
526   printstr (stream, string, length, force_ellipses, width);
527 }
528 
529 
530 /* Print data of type TYPE located at VALADDR (within GDB), which came from
531    the inferior at address ADDRESS, onto stdio stream STREAM according to
532    FORMAT (a letter as for the printf % codes or 0 for natural format).
533    The data at VALADDR is in target byte order.
534 
535    If the data is printed as a string, returns the number of string characters
536    printed.
537 
538    If DEREF_REF is nonzero, then dereference references, otherwise just print
539    them like pointers.
540 
541    RECURSE indicates the amount of indentation to supply before
542    continuation lines; this amount is roughly twice the value of RECURSE.
543 
544    When PRETTY is non-zero, prints record fields on separate lines.
545    (For some reason, the current version of gdb instead uses a global
546    variable---prettyprint_arrays--- to causes a similar effect on
547    arrays.)  */
548 
549 int
ada_val_print(struct type * type,const gdb_byte * valaddr0,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int format,int deref_ref,int recurse,enum val_prettyprint pretty)550 ada_val_print (struct type *type, const gdb_byte *valaddr0,
551 	       int embedded_offset, CORE_ADDR address,
552 	       struct ui_file *stream, int format, int deref_ref,
553 	       int recurse, enum val_prettyprint pretty)
554 {
555   struct ada_val_print_args args;
556   args.type = type;
557   args.valaddr0 = valaddr0;
558   args.embedded_offset = embedded_offset;
559   args.address = address;
560   args.stream = stream;
561   args.format = format;
562   args.deref_ref = deref_ref;
563   args.recurse = recurse;
564   args.pretty = pretty;
565 
566   return catch_errors (ada_val_print_stub, &args, NULL, RETURN_MASK_ALL);
567 }
568 
569 /* Helper for ada_val_print; used as argument to catch_errors to
570    unmarshal the arguments to ada_val_print_1, which does the work.  */
571 static int
ada_val_print_stub(void * args0)572 ada_val_print_stub (void *args0)
573 {
574   struct ada_val_print_args *argsp = (struct ada_val_print_args *) args0;
575   return ada_val_print_1 (argsp->type, argsp->valaddr0,
576 			  argsp->embedded_offset, argsp->address,
577 			  argsp->stream, argsp->format, argsp->deref_ref,
578 			  argsp->recurse, argsp->pretty);
579 }
580 
581 /* See the comment on ada_val_print.  This function differs in that it
582  * does not catch evaluation errors (leaving that to ada_val_print).  */
583 
584 static int
ada_val_print_1(struct type * type,const gdb_byte * valaddr0,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int format,int deref_ref,int recurse,enum val_prettyprint pretty)585 ada_val_print_1 (struct type *type, const gdb_byte *valaddr0,
586 		 int embedded_offset, CORE_ADDR address,
587 		 struct ui_file *stream, int format,
588 		 int deref_ref, int recurse, enum val_prettyprint pretty)
589 {
590   unsigned int len;
591   int i;
592   struct type *elttype;
593   unsigned int eltlen;
594   LONGEST val;
595   const gdb_byte *valaddr = valaddr0 + embedded_offset;
596 
597   type = ada_check_typedef (type);
598 
599   if (ada_is_array_descriptor_type (type) || ada_is_packed_array_type (type))
600     {
601       int retn;
602       struct value *mark = value_mark ();
603       struct value *val;
604       val = value_from_contents_and_address (type, valaddr, address);
605       val = ada_coerce_to_simple_array_ptr (val);
606       if (val == NULL)
607 	{
608 	  fprintf_filtered (stream, "(null)");
609 	  retn = 0;
610 	}
611       else
612 	retn = ada_val_print_1 (value_type (val), value_contents (val), 0,
613 				VALUE_ADDRESS (val), stream, format,
614 				deref_ref, recurse, pretty);
615       value_free_to_mark (mark);
616       return retn;
617     }
618 
619   valaddr = ada_aligned_value_addr (type, valaddr);
620   embedded_offset -= valaddr - valaddr0 - embedded_offset;
621   type = printable_val_type (type, valaddr);
622 
623   switch (TYPE_CODE (type))
624     {
625     default:
626       return c_val_print (type, valaddr0, embedded_offset, address, stream,
627 			  format, deref_ref, recurse, pretty);
628 
629     case TYPE_CODE_PTR:
630       {
631 	int ret = c_val_print (type, valaddr0, embedded_offset, address,
632 			       stream, format, deref_ref, recurse, pretty);
633 	if (ada_is_tag_type (type))
634 	  {
635 	    struct value *val =
636 	      value_from_contents_and_address (type, valaddr, address);
637 	    const char *name = ada_tag_name (val);
638 	    if (name != NULL)
639 	      fprintf_filtered (stream, " (%s)", name);
640 	    return 0;
641 	}
642 	return ret;
643       }
644 
645     case TYPE_CODE_INT:
646     case TYPE_CODE_RANGE:
647       if (ada_is_fixed_point_type (type))
648 	{
649 	  LONGEST v = unpack_long (type, valaddr);
650 	  int len = TYPE_LENGTH (type);
651 
652 	  fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
653 			    (double) ada_fixed_to_float (type, v));
654 	  return 0;
655 	}
656       else if (ada_is_vax_floating_type (type))
657 	{
658 	  struct value *val =
659 	    value_from_contents_and_address (type, valaddr, address);
660 	  struct value *func = ada_vax_float_print_function (type);
661 	  if (func != 0)
662 	    {
663 	      static struct type *parray_of_char = NULL;
664 	      struct value *printable_val;
665 
666 	      if (parray_of_char == NULL)
667 		parray_of_char =
668 		  make_pointer_type
669 		  (create_array_type
670 		   (NULL, builtin_type_char,
671 		    create_range_type (NULL, builtin_type_int, 0, 32)), NULL);
672 
673 	      printable_val =
674 		value_ind (value_cast (parray_of_char,
675 				       call_function_by_hand (func, 1,
676 							      &val)));
677 
678 	      fprintf_filtered (stream, "%s", value_contents (printable_val));
679 	      return 0;
680 	    }
681 	  /* No special printing function.  Do as best we can.  */
682 	}
683       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
684 	{
685 	  struct type *target_type = TYPE_TARGET_TYPE (type);
686 	  if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
687 	    {
688 	      /* Obscure case of range type that has different length from
689 	         its base type.  Perform a conversion, or we will get a
690 	         nonsense value.  Actually, we could use the same
691 	         code regardless of lengths; I'm just avoiding a cast.  */
692 	      struct value *v = value_cast (target_type,
693 					    value_from_contents_and_address
694 					    (type, valaddr, 0));
695 	      return ada_val_print_1 (target_type, value_contents (v), 0, 0,
696 				      stream, format, 0, recurse + 1, pretty);
697 	    }
698 	  else
699 	    return ada_val_print_1 (TYPE_TARGET_TYPE (type),
700 				    valaddr0, embedded_offset,
701 				    address, stream, format, deref_ref,
702 				    recurse, pretty);
703 	}
704       else
705 	{
706 	  format = format ? format : output_format;
707 	  if (format)
708 	    {
709 	      print_scalar_formatted (valaddr, type, format, 0, stream);
710 	    }
711           else if (ada_is_system_address_type (type))
712             {
713               /* FIXME: We want to print System.Address variables using
714                  the same format as for any access type.  But for some
715                  reason GNAT encodes the System.Address type as an int,
716                  so we have to work-around this deficiency by handling
717                  System.Address values as a special case.  */
718               fprintf_filtered (stream, "(");
719               type_print (type, "", stream, -1);
720               fprintf_filtered (stream, ") ");
721               deprecated_print_address_numeric
722 		(extract_typed_address (valaddr, builtin_type_void_data_ptr),
723                  1, stream);
724             }
725 	  else
726 	    {
727 	      val_print_type_code_int (type, valaddr, stream);
728 	      if (ada_is_character_type (type))
729 		{
730 		  fputs_filtered (" ", stream);
731 		  ada_printchar ((unsigned char) unpack_long (type, valaddr),
732 				 stream);
733 		}
734 	    }
735 	  return 0;
736 	}
737 
738     case TYPE_CODE_ENUM:
739       if (format)
740 	{
741 	  print_scalar_formatted (valaddr, type, format, 0, stream);
742 	  break;
743 	}
744       len = TYPE_NFIELDS (type);
745       val = unpack_long (type, valaddr);
746       for (i = 0; i < len; i++)
747 	{
748 	  QUIT;
749 	  if (val == TYPE_FIELD_BITPOS (type, i))
750 	    {
751 	      break;
752 	    }
753 	}
754       if (i < len)
755 	{
756 	  const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
757 	  if (name[0] == '\'')
758 	    fprintf_filtered (stream, "%ld %s", (long) val, name);
759 	  else
760 	    fputs_filtered (name, stream);
761 	}
762       else
763 	{
764 	  print_longest (stream, 'd', 0, val);
765 	}
766       break;
767 
768     case TYPE_CODE_FLT:
769       if (format)
770 	return c_val_print (type, valaddr0, embedded_offset, address, stream,
771 			    format, deref_ref, recurse, pretty);
772       else
773 	ada_print_floating (valaddr0 + embedded_offset, type, stream);
774       break;
775 
776     case TYPE_CODE_UNION:
777     case TYPE_CODE_STRUCT:
778       if (ada_is_bogus_array_descriptor (type))
779 	{
780 	  fprintf_filtered (stream, "(...?)");
781 	  return 0;
782 	}
783       else
784 	{
785 	  print_record (type, valaddr, stream, format, recurse, pretty);
786 	  return 0;
787 	}
788 
789     case TYPE_CODE_ARRAY:
790       elttype = TYPE_TARGET_TYPE (type);
791       if (elttype == NULL)
792 	eltlen = 0;
793       else
794 	eltlen = TYPE_LENGTH (elttype);
795       /* FIXME: This doesn't deal with non-empty arrays of
796 	 0-length items (not a typical case!) */
797       if (eltlen == 0)
798 	len = 0;
799       else
800 	len = TYPE_LENGTH (type) / eltlen;
801 
802 	  /* For an array of chars, print with string syntax.  */
803       if (ada_is_string_type (type) && (format == 0 || format == 's'))
804 	{
805 	  if (prettyprint_arrays)
806 	    {
807 	      print_spaces_filtered (2 + 2 * recurse, stream);
808 	    }
809 	  /* If requested, look for the first null char and only print
810 	     elements up to it.  */
811 	  if (stop_print_at_null)
812 	    {
813 	      int temp_len;
814 
815 	      /* Look for a NULL char.  */
816 	      for (temp_len = 0;
817 		   temp_len < len && temp_len < print_max
818 		     && char_at (valaddr, temp_len, eltlen) != 0;
819 		   temp_len += 1);
820 	      len = temp_len;
821 	    }
822 
823 	  printstr (stream, valaddr, len, 0, eltlen);
824 	}
825       else
826 	{
827 	  len = 0;
828 	  fprintf_filtered (stream, "(");
829 	  print_optional_low_bound (stream, type);
830 	  if (TYPE_FIELD_BITSIZE (type, 0) > 0)
831 	    val_print_packed_array_elements (type, valaddr, 0, stream,
832 					     format, recurse, pretty);
833 	  else
834 	    val_print_array_elements (type, valaddr, address, stream,
835 				      format, deref_ref, recurse,
836 				      pretty, 0);
837 	  fprintf_filtered (stream, ")");
838 	}
839       gdb_flush (stream);
840       return len;
841 
842     case TYPE_CODE_REF:
843       elttype = check_typedef (TYPE_TARGET_TYPE (type));
844       /* De-reference the reference */
845       if (deref_ref)
846 	{
847 	  if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
848 	    {
849 	      LONGEST deref_val_int = (LONGEST)
850 		unpack_pointer (lookup_pointer_type (builtin_type_void),
851 				valaddr);
852 	      if (deref_val_int != 0)
853 		{
854 		  struct value *deref_val =
855 		    ada_value_ind (value_from_longest
856 				   (lookup_pointer_type (elttype),
857 				    deref_val_int));
858 		  val_print (value_type (deref_val),
859 			     value_contents (deref_val), 0,
860 			     VALUE_ADDRESS (deref_val), stream, format,
861 			     deref_ref, recurse + 1, pretty);
862 		}
863 	      else
864 		fputs_filtered ("(null)", stream);
865 	    }
866 	  else
867 	    fputs_filtered ("???", stream);
868 	}
869       break;
870     }
871   gdb_flush (stream);
872   return 0;
873 }
874 
875 static int
print_variant_part(struct type * type,int field_num,const gdb_byte * valaddr,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,int comma_needed,struct type * outer_type,const gdb_byte * outer_valaddr)876 print_variant_part (struct type *type, int field_num, const gdb_byte *valaddr,
877 		    struct ui_file *stream, int format, int recurse,
878 		    enum val_prettyprint pretty, int comma_needed,
879 		    struct type *outer_type, const gdb_byte *outer_valaddr)
880 {
881   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
882   int which = ada_which_variant_applies (var_type, outer_type, outer_valaddr);
883 
884   if (which < 0)
885     return 0;
886   else
887     return print_field_values
888       (TYPE_FIELD_TYPE (var_type, which),
889        valaddr + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
890        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
891        stream, format, recurse, pretty,
892        comma_needed, outer_type, outer_valaddr);
893 }
894 
895 int
ada_value_print(struct value * val0,struct ui_file * stream,int format,enum val_prettyprint pretty)896 ada_value_print (struct value *val0, struct ui_file *stream, int format,
897 		 enum val_prettyprint pretty)
898 {
899   const gdb_byte *valaddr = value_contents (val0);
900   CORE_ADDR address = VALUE_ADDRESS (val0) + value_offset (val0);
901   struct type *type =
902     ada_to_fixed_type (value_type (val0), valaddr, address, NULL);
903   struct value *val =
904     value_from_contents_and_address (type, valaddr, address);
905 
906   /* If it is a pointer, indicate what it points to.  */
907   if (TYPE_CODE (type) == TYPE_CODE_PTR)
908     {
909       /* Hack:  don't print (char *) for char strings.  Their
910          type is indicated by the quoted string anyway.  */
911       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
912 	  || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT
913 	  || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
914 	{
915 	  fprintf_filtered (stream, "(");
916 	  type_print (type, "", stream, -1);
917 	  fprintf_filtered (stream, ") ");
918 	}
919     }
920   else if (ada_is_array_descriptor_type (type))
921     {
922       fprintf_filtered (stream, "(");
923       type_print (type, "", stream, -1);
924       fprintf_filtered (stream, ") ");
925     }
926   else if (ada_is_bogus_array_descriptor (type))
927     {
928       fprintf_filtered (stream, "(");
929       type_print (type, "", stream, -1);
930       fprintf_filtered (stream, ") (...?)");
931       return 0;
932     }
933 
934   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
935       && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == 0
936       && TYPE_CODE (TYPE_INDEX_TYPE (type)) == TYPE_CODE_RANGE)
937     {
938       /* This is an array of zero-length elements, that is an array
939          of null records.  This array needs to be printed by hand,
940          as the standard routine to print arrays relies on the size of
941          the array elements to be nonzero.  This is because it computes
942          the number of elements in the array by dividing the array size
943          by the array element size.  */
944       fprintf_filtered (stream, "(%d .. %d => ())",
945                         TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
946                         TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)));
947       return 0;
948     }
949 
950   return (val_print (type, value_contents (val), 0, address,
951 		     stream, format, 1, 0, pretty));
952 }
953 
954 static void
print_record(struct type * type,const gdb_byte * valaddr,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)955 print_record (struct type *type, const gdb_byte *valaddr,
956 	      struct ui_file *stream, int format, int recurse,
957 	      enum val_prettyprint pretty)
958 {
959   type = ada_check_typedef (type);
960 
961   fprintf_filtered (stream, "(");
962 
963   if (print_field_values (type, valaddr, stream, format, recurse, pretty,
964 			  0, type, valaddr) != 0 && pretty)
965     {
966       fprintf_filtered (stream, "\n");
967       print_spaces_filtered (2 * recurse, stream);
968     }
969 
970   fprintf_filtered (stream, ")");
971 }
972 
973 /* Print out fields of value at VALADDR having structure type TYPE.
974 
975    TYPE, VALADDR, STREAM, FORMAT, RECURSE, and PRETTY have the
976    same meanings as in ada_print_value and ada_val_print.
977 
978    OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
979    (used to get discriminant values when printing variant parts).
980 
981    COMMA_NEEDED is 1 if fields have been printed at the current recursion
982    level, so that a comma is needed before any field printed by this
983    call.
984 
985    Returns 1 if COMMA_NEEDED or any fields were printed.  */
986 
987 static int
print_field_values(struct type * type,const gdb_byte * valaddr,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,int comma_needed,struct type * outer_type,const gdb_byte * outer_valaddr)988 print_field_values (struct type *type, const gdb_byte *valaddr,
989 		    struct ui_file *stream, int format, int recurse,
990 		    enum val_prettyprint pretty, int comma_needed,
991 		    struct type *outer_type, const gdb_byte *outer_valaddr)
992 {
993   int i, len;
994 
995   len = TYPE_NFIELDS (type);
996 
997   for (i = 0; i < len; i += 1)
998     {
999       if (ada_is_ignored_field (type, i))
1000 	continue;
1001 
1002       if (ada_is_wrapper_field (type, i))
1003 	{
1004 	  comma_needed =
1005 	    print_field_values (TYPE_FIELD_TYPE (type, i),
1006 				valaddr
1007 				+ TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1008 				stream, format, recurse, pretty,
1009 				comma_needed, type, valaddr);
1010 	  continue;
1011 	}
1012       else if (ada_is_variant_part (type, i))
1013 	{
1014 	  comma_needed =
1015 	    print_variant_part (type, i, valaddr,
1016 				stream, format, recurse, pretty, comma_needed,
1017 				outer_type, outer_valaddr);
1018 	  continue;
1019 	}
1020 
1021       if (comma_needed)
1022 	fprintf_filtered (stream, ", ");
1023       comma_needed = 1;
1024 
1025       if (pretty)
1026 	{
1027 	  fprintf_filtered (stream, "\n");
1028 	  print_spaces_filtered (2 + 2 * recurse, stream);
1029 	}
1030       else
1031 	{
1032 	  wrap_here (n_spaces (2 + 2 * recurse));
1033 	}
1034       if (inspect_it)
1035 	{
1036 	  if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1037 	    fputs_filtered ("\"( ptr \"", stream);
1038 	  else
1039 	    fputs_filtered ("\"( nodef \"", stream);
1040 	  fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1041 				   language_cplus, DMGL_NO_OPTS);
1042 	  fputs_filtered ("\" \"", stream);
1043 	  fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1044 				   language_cplus, DMGL_NO_OPTS);
1045 	  fputs_filtered ("\") \"", stream);
1046 	}
1047       else
1048 	{
1049 	  annotate_field_begin (TYPE_FIELD_TYPE (type, i));
1050 	  fprintf_filtered (stream, "%.*s",
1051 			    ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1052 			    TYPE_FIELD_NAME (type, i));
1053 	  annotate_field_name_end ();
1054 	  fputs_filtered (" => ", stream);
1055 	  annotate_field_value ();
1056 	}
1057 
1058       if (TYPE_FIELD_PACKED (type, i))
1059 	{
1060 	  struct value *v;
1061 
1062 	  /* Bitfields require special handling, especially due to byte
1063 	     order problems.  */
1064 	  if (TYPE_CPLUS_SPECIFIC (type) != NULL
1065 	      && TYPE_FIELD_IGNORE (type, i))
1066 	    {
1067 	      fputs_filtered (_("<optimized out or zero length>"), stream);
1068 	    }
1069 	  else
1070 	    {
1071 	      int bit_pos = TYPE_FIELD_BITPOS (type, i);
1072 	      int bit_size = TYPE_FIELD_BITSIZE (type, i);
1073 
1074 	      adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
1075 	      v = ada_value_primitive_packed_val (NULL, valaddr,
1076 						  bit_pos / HOST_CHAR_BIT,
1077 						  bit_pos % HOST_CHAR_BIT,
1078 						  bit_size,
1079 						  TYPE_FIELD_TYPE (type, i));
1080 	      val_print (TYPE_FIELD_TYPE (type, i), value_contents (v), 0, 0,
1081 			 stream, format, 0, recurse + 1, pretty);
1082 	    }
1083 	}
1084       else
1085 	ada_val_print (TYPE_FIELD_TYPE (type, i),
1086 		       valaddr + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1087 		       0, 0, stream, format, 0, recurse + 1, pretty);
1088       annotate_field_end ();
1089     }
1090 
1091   return comma_needed;
1092 }
1093