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