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