Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2019 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
22 /* Local non-gdb includes. */
23 #include "c-lang.h"
24 #include "cp-abi.h"
25 #include "expression.h"
26 #include "gdbtypes.h"
27 #include "language.h"
28 #include "objfiles.h"
29 #include "symtab.h"
30 #include "target.h"
31 #include "valprint.h"
32 #include "value.h"
33
34 \f
35
36 /* A helper for c_textual_element_type. This checks the name of the
37 typedef. This is bogus but it isn't apparent that the compiler
38 provides us the help we may need. */
39
40 static int
41 textual_name (const char *name)
42 {
43 return (!strcmp (name, "wchar_t")
44 || !strcmp (name, "char16_t")
45 || !strcmp (name, "char32_t"));
46 }
47
48 /* Apply a heuristic to decide whether an array of TYPE or a pointer
49 to TYPE should be printed as a textual string. Return non-zero if
50 it should, or zero if it should be treated as an array of integers
51 or pointer to integers. FORMAT is the current format letter, or 0
52 if none.
53
54 We guess that "char" is a character. Explicitly signed and
55 unsigned character types are also characters. Integer data from
56 vector types is not. The user can override this by using the /s
57 format letter. */
58
59 int
60 c_textual_element_type (struct type *type, char format)
61 {
62 struct type *true_type, *iter_type;
63
64 if (format != 0 && format != 's')
65 return 0;
66
67 /* We also rely on this for its side effect of setting up all the
68 typedef pointers. */
69 true_type = check_typedef (type);
70
71 /* TYPE_CODE_CHAR is always textual. */
72 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
73 return 1;
74
75 /* Any other character-like types must be integral. */
76 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
77 return 0;
78
79 /* We peel typedefs one by one, looking for a match. */
80 iter_type = type;
81 while (iter_type)
82 {
83 /* Check the name of the type. */
84 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
85 return 1;
86
87 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
88 break;
89
90 /* Peel a single typedef. If the typedef doesn't have a target
91 type, we use check_typedef and hope the result is ok -- it
92 might be for C++, where wchar_t is a built-in type. */
93 if (TYPE_TARGET_TYPE (iter_type))
94 iter_type = TYPE_TARGET_TYPE (iter_type);
95 else
96 iter_type = check_typedef (iter_type);
97 }
98
99 if (format == 's')
100 {
101 /* Print this as a string if we can manage it. For now, no wide
102 character support. */
103 if (TYPE_CODE (true_type) == TYPE_CODE_INT
104 && TYPE_LENGTH (true_type) == 1)
105 return 1;
106 }
107 else
108 {
109 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
110 flag, then we treat it as text; otherwise, we assume it's
111 being used as data. */
112 if (TYPE_CODE (true_type) == TYPE_CODE_INT
113 && TYPE_LENGTH (true_type) == 1
114 && !TYPE_NOTTEXT (true_type))
115 return 1;
116 }
117
118 return 0;
119 }
120
121 /* Decorations for C. */
122
123 static const struct generic_val_print_decorations c_decorations =
124 {
125 "",
126 " + ",
127 " * I",
128 "true",
129 "false",
130 "void",
131 "{",
132 "}"
133 };
134
135 /* Print a pointer based on the type of its target.
136
137 Arguments to this functions are roughly the same as those in c_val_print.
138 A difference is that ADDRESS is the address to print, with embedded_offset
139 already added. UNRESOLVED_ELTTYPE and ELTTYPE represent the pointed type,
140 respectively before and after check_typedef. */
141
142 static void
143 print_unpacked_pointer (struct type *type, struct type *elttype,
144 struct type *unresolved_elttype,
145 const gdb_byte *valaddr, int embedded_offset,
146 CORE_ADDR address, struct ui_file *stream, int recurse,
147 const struct value_print_options *options)
148 {
149 int want_space = 0;
150 struct gdbarch *gdbarch = get_type_arch (type);
151
152 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
153 {
154 /* Try to print what function it points to. */
155 print_function_pointer_address (options, gdbarch, address, stream);
156 return;
157 }
158
159 if (options->symbol_print)
160 want_space = print_address_demangle (options, gdbarch, address, stream,
161 demangle);
162 else if (options->addressprint)
163 {
164 fputs_filtered (paddress (gdbarch, address), stream);
165 want_space = 1;
166 }
167
168 /* For a pointer to a textual type, also print the string
169 pointed to, unless pointer is null. */
170
171 if (c_textual_element_type (unresolved_elttype, options->format)
172 && address != 0)
173 {
174 if (want_space)
175 fputs_filtered (" ", stream);
176 val_print_string (unresolved_elttype, NULL, address, -1, stream, options);
177 }
178 else if (cp_is_vtbl_member (type))
179 {
180 /* Print vtbl's nicely. */
181 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
182 struct bound_minimal_symbol msymbol =
183 lookup_minimal_symbol_by_pc (vt_address);
184
185 /* If 'symbol_print' is set, we did the work above. */
186 if (!options->symbol_print
187 && (msymbol.minsym != NULL)
188 && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
189 {
190 if (want_space)
191 fputs_filtered (" ", stream);
192 fputs_filtered (" <", stream);
193 fputs_filtered (MSYMBOL_PRINT_NAME (msymbol.minsym), stream);
194 fputs_filtered (">", stream);
195 want_space = 1;
196 }
197
198 if (vt_address && options->vtblprint)
199 {
200 struct value *vt_val;
201 struct symbol *wsym = NULL;
202 struct type *wtype;
203
204 if (want_space)
205 fputs_filtered (" ", stream);
206
207 if (msymbol.minsym != NULL)
208 {
209 const char *search_name
210 = MSYMBOL_SEARCH_NAME (msymbol.minsym);
211 wsym = lookup_symbol_search_name (search_name, NULL,
212 VAR_DOMAIN).symbol;
213 }
214
215 if (wsym)
216 {
217 wtype = SYMBOL_TYPE (wsym);
218 }
219 else
220 {
221 wtype = unresolved_elttype;
222 }
223 vt_val = value_at (wtype, vt_address);
224 common_val_print (vt_val, stream, recurse + 1, options,
225 current_language);
226 if (options->prettyformat)
227 {
228 fprintf_filtered (stream, "\n");
229 print_spaces_filtered (2 + 2 * recurse, stream);
230 }
231 }
232 }
233 }
234
235 /* c_val_print helper for TYPE_CODE_ARRAY. */
236
237 static void
238 c_val_print_array (struct type *type, const gdb_byte *valaddr,
239 int embedded_offset, CORE_ADDR address,
240 struct ui_file *stream, int recurse,
241 struct value *original_value,
242 const struct value_print_options *options)
243 {
244 struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
245 struct type *elttype = check_typedef (unresolved_elttype);
246 struct gdbarch *arch = get_type_arch (type);
247 int unit_size = gdbarch_addressable_memory_unit_size (arch);
248
249 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
250 {
251 LONGEST low_bound, high_bound;
252 int eltlen, len;
253 struct gdbarch *gdbarch = get_type_arch (type);
254 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
255 unsigned int i = 0; /* Number of characters printed. */
256
257 if (!get_array_bounds (type, &low_bound, &high_bound))
258 error (_("Could not determine the array high bound"));
259
260 eltlen = TYPE_LENGTH (elttype);
261 len = high_bound - low_bound + 1;
262 if (options->prettyformat_arrays)
263 {
264 print_spaces_filtered (2 + 2 * recurse, stream);
265 }
266
267 /* Print arrays of textual chars with a string syntax, as
268 long as the entire array is valid. */
269 if (c_textual_element_type (unresolved_elttype,
270 options->format)
271 && value_bytes_available (original_value, embedded_offset,
272 TYPE_LENGTH (type))
273 && !value_bits_any_optimized_out (original_value,
274 TARGET_CHAR_BIT * embedded_offset,
275 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
276 {
277 int force_ellipses = 0;
278
279 /* If requested, look for the first null char and only
280 print elements up to it. */
281 if (options->stop_print_at_null)
282 {
283 unsigned int temp_len;
284
285 for (temp_len = 0;
286 (temp_len < len
287 && temp_len < options->print_max
288 && extract_unsigned_integer (valaddr
289 + embedded_offset * unit_size
290 + temp_len * eltlen,
291 eltlen, byte_order) != 0);
292 ++temp_len)
293 ;
294
295 /* Force LA_PRINT_STRING to print ellipses if
296 we've printed the maximum characters and
297 the next character is not \000. */
298 if (temp_len == options->print_max && temp_len < len)
299 {
300 ULONGEST val
301 = extract_unsigned_integer (valaddr
302 + embedded_offset * unit_size
303 + temp_len * eltlen,
304 eltlen, byte_order);
305 if (val != 0)
306 force_ellipses = 1;
307 }
308
309 len = temp_len;
310 }
311
312 LA_PRINT_STRING (stream, unresolved_elttype,
313 valaddr + embedded_offset * unit_size, len,
314 NULL, force_ellipses, options);
315 i = len;
316 }
317 else
318 {
319 fprintf_filtered (stream, "{");
320 /* If this is a virtual function table, print the 0th
321 entry specially, and the rest of the members
322 normally. */
323 if (cp_is_vtbl_ptr_type (elttype))
324 {
325 i = 1;
326 fprintf_filtered (stream, _("%d vtable entries"),
327 len - 1);
328 }
329 else
330 {
331 i = 0;
332 }
333 val_print_array_elements (type, embedded_offset,
334 address, stream,
335 recurse, original_value, options, i);
336 fprintf_filtered (stream, "}");
337 }
338 }
339 else
340 {
341 /* Array of unspecified length: treat like pointer to first elt. */
342 print_unpacked_pointer (type, elttype, unresolved_elttype, valaddr,
343 embedded_offset, address + embedded_offset,
344 stream, recurse, options);
345 }
346 }
347
348 /* c_val_print helper for TYPE_CODE_PTR. */
349
350 static void
351 c_val_print_ptr (struct type *type, const gdb_byte *valaddr,
352 int embedded_offset, struct ui_file *stream, int recurse,
353 struct value *original_value,
354 const struct value_print_options *options)
355 {
356 struct gdbarch *arch = get_type_arch (type);
357 int unit_size = gdbarch_addressable_memory_unit_size (arch);
358
359 if (options->format && options->format != 's')
360 {
361 val_print_scalar_formatted (type, embedded_offset,
362 original_value, options, 0, stream);
363 }
364 else if (options->vtblprint && cp_is_vtbl_ptr_type (type))
365 {
366 /* Print the unmangled name if desired. */
367 /* Print vtable entry - we only get here if we ARE using
368 -fvtable_thunks. (Otherwise, look under
369 TYPE_CODE_STRUCT.) */
370 CORE_ADDR addr
371 = extract_typed_address (valaddr + embedded_offset, type);
372 struct gdbarch *gdbarch = get_type_arch (type);
373
374 print_function_pointer_address (options, gdbarch, addr, stream);
375 }
376 else
377 {
378 struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
379 struct type *elttype = check_typedef (unresolved_elttype);
380 CORE_ADDR addr = unpack_pointer (type,
381 valaddr + embedded_offset * unit_size);
382
383 print_unpacked_pointer (type, elttype, unresolved_elttype, valaddr,
384 embedded_offset, addr, stream, recurse, options);
385 }
386 }
387
388 /* c_val_print helper for TYPE_CODE_STRUCT. */
389
390 static void
391 c_val_print_struct (struct type *type, const gdb_byte *valaddr,
392 int embedded_offset, CORE_ADDR address,
393 struct ui_file *stream, int recurse,
394 struct value *original_value,
395 const struct value_print_options *options)
396 {
397 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
398 {
399 /* Print the unmangled name if desired. */
400 /* Print vtable entry - we only get here if NOT using
401 -fvtable_thunks. (Otherwise, look under
402 TYPE_CODE_PTR.) */
403 struct gdbarch *gdbarch = get_type_arch (type);
404 int offset = (embedded_offset
405 + TYPE_FIELD_BITPOS (type,
406 VTBL_FNADDR_OFFSET) / 8);
407 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
408 CORE_ADDR addr = extract_typed_address (valaddr + offset, field_type);
409
410 print_function_pointer_address (options, gdbarch, addr, stream);
411 }
412 else
413 cp_print_value_fields_rtti (type, valaddr,
414 embedded_offset, address,
415 stream, recurse,
416 original_value, options,
417 NULL, 0);
418 }
419
420 /* c_val_print helper for TYPE_CODE_UNION. */
421
422 static void
423 c_val_print_union (struct type *type, const gdb_byte *valaddr,
424 int embedded_offset, CORE_ADDR address,
425 struct ui_file *stream, int recurse,
426 struct value *original_value,
427 const struct value_print_options *options)
428 {
429 if (recurse && !options->unionprint)
430 {
431 fprintf_filtered (stream, "{...}");
432 }
433 else
434 {
435 c_val_print_struct (type, valaddr, embedded_offset, address, stream,
436 recurse, original_value, options);
437 }
438 }
439
440 /* c_val_print helper for TYPE_CODE_INT. */
441
442 static void
443 c_val_print_int (struct type *type, struct type *unresolved_type,
444 const gdb_byte *valaddr, int embedded_offset,
445 struct ui_file *stream, struct value *original_value,
446 const struct value_print_options *options)
447 {
448 struct gdbarch *arch = get_type_arch (type);
449 int unit_size = gdbarch_addressable_memory_unit_size (arch);
450
451 if (options->format || options->output_format)
452 {
453 struct value_print_options opts = *options;
454
455 opts.format = (options->format ? options->format
456 : options->output_format);
457 val_print_scalar_formatted (type, embedded_offset,
458 original_value, &opts, 0, stream);
459 }
460 else
461 {
462 val_print_scalar_formatted (type, embedded_offset,
463 original_value, options, 0, stream);
464 /* C and C++ has no single byte int type, char is used
465 instead. Since we don't know whether the value is really
466 intended to be used as an integer or a character, print
467 the character equivalent as well. */
468 if (c_textual_element_type (unresolved_type, options->format))
469 {
470 fputs_filtered (" ", stream);
471 LA_PRINT_CHAR (unpack_long (type,
472 valaddr + embedded_offset * unit_size),
473 unresolved_type, stream);
474 }
475 }
476 }
477
478 /* c_val_print helper for TYPE_CODE_MEMBERPTR. */
479
480 static void
481 c_val_print_memberptr (struct type *type, const gdb_byte *valaddr,
482 int embedded_offset, CORE_ADDR address,
483 struct ui_file *stream, int recurse,
484 struct value *original_value,
485 const struct value_print_options *options)
486 {
487 if (!options->format)
488 {
489 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
490 }
491 else
492 {
493 generic_val_print (type, embedded_offset, address, stream,
494 recurse, original_value, options, &c_decorations);
495 }
496 }
497
498 /* See val_print for a description of the various parameters of this
499 function; they are identical. */
500
501 void
502 c_val_print (struct type *type,
503 int embedded_offset, CORE_ADDR address,
504 struct ui_file *stream, int recurse,
505 struct value *original_value,
506 const struct value_print_options *options)
507 {
508 struct type *unresolved_type = type;
509 const gdb_byte *valaddr = value_contents_for_printing (original_value);
510
511 type = check_typedef (type);
512 switch (TYPE_CODE (type))
513 {
514 case TYPE_CODE_ARRAY:
515 c_val_print_array (type, valaddr, embedded_offset, address, stream,
516 recurse, original_value, options);
517 break;
518
519 case TYPE_CODE_METHODPTR:
520 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
521 break;
522
523 case TYPE_CODE_PTR:
524 c_val_print_ptr (type, valaddr, embedded_offset, stream, recurse,
525 original_value, options);
526 break;
527
528 case TYPE_CODE_UNION:
529 c_val_print_union (type, valaddr, embedded_offset, address, stream,
530 recurse, original_value, options);
531 break;
532
533 case TYPE_CODE_STRUCT:
534 c_val_print_struct (type, valaddr, embedded_offset, address, stream,
535 recurse, original_value, options);
536 break;
537
538 case TYPE_CODE_INT:
539 c_val_print_int (type, unresolved_type, valaddr, embedded_offset, stream,
540 original_value, options);
541 break;
542
543 case TYPE_CODE_MEMBERPTR:
544 c_val_print_memberptr (type, valaddr, embedded_offset, address, stream,
545 recurse, original_value, options);
546 break;
547
548 case TYPE_CODE_REF:
549 case TYPE_CODE_RVALUE_REF:
550 case TYPE_CODE_ENUM:
551 case TYPE_CODE_FLAGS:
552 case TYPE_CODE_FUNC:
553 case TYPE_CODE_METHOD:
554 case TYPE_CODE_BOOL:
555 case TYPE_CODE_RANGE:
556 case TYPE_CODE_FLT:
557 case TYPE_CODE_DECFLOAT:
558 case TYPE_CODE_VOID:
559 case TYPE_CODE_ERROR:
560 case TYPE_CODE_UNDEF:
561 case TYPE_CODE_COMPLEX:
562 case TYPE_CODE_CHAR:
563 default:
564 generic_val_print (type, embedded_offset, address,
565 stream, recurse, original_value, options,
566 &c_decorations);
567 break;
568 }
569 }
570 \f
571 void
572 c_value_print (struct value *val, struct ui_file *stream,
573 const struct value_print_options *options)
574 {
575 struct type *type, *real_type, *val_type;
576 int full, using_enc;
577 LONGEST top;
578 struct value_print_options opts = *options;
579
580 opts.deref_ref = 1;
581
582 /* If it is a pointer, indicate what it points to.
583
584 Print type also if it is a reference.
585
586 C++: if it is a member pointer, we will take care
587 of that when we print it. */
588
589 /* Preserve the original type before stripping typedefs. We prefer
590 to pass down the original type when possible, but for local
591 checks it is better to look past the typedefs. */
592 val_type = value_type (val);
593 type = check_typedef (val_type);
594
595 if (TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
596 {
597 /* Hack: remove (char *) for char strings. Their
598 type is indicated by the quoted string anyway.
599 (Don't use c_textual_element_type here; quoted strings
600 are always exactly (char *), (wchar_t *), or the like. */
601 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
602 && TYPE_NAME (val_type) == NULL
603 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
604 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
605 "char") == 0
606 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
607 {
608 /* Print nothing. */
609 }
610 else if (options->objectprint
611 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
612 {
613 int is_ref = TYPE_IS_REFERENCE (type);
614 enum type_code refcode = TYPE_CODE_UNDEF;
615
616 if (is_ref)
617 {
618 val = value_addr (val);
619 refcode = TYPE_CODE (type);
620 }
621
622 /* Pointer to class, check real type of object. */
623 fprintf_filtered (stream, "(");
624
625 if (value_entirely_available (val))
626 {
627 real_type = value_rtti_indirect_type (val, &full, &top,
628 &using_enc);
629 if (real_type)
630 {
631 /* RTTI entry found. */
632 type = real_type;
633
634 /* Need to adjust pointer value. */
635 val = value_from_pointer (real_type,
636 value_as_address (val) - top);
637
638 /* Note: When we look up RTTI entries, we don't get
639 any information on const or volatile
640 attributes. */
641 }
642 }
643
644 if (is_ref)
645 {
646 val = value_ref (value_ind (val), refcode);
647 type = value_type (val);
648 }
649
650 type_print (type, "", stream, -1);
651 fprintf_filtered (stream, ") ");
652 val_type = type;
653 }
654 else
655 {
656 /* normal case */
657 fprintf_filtered (stream, "(");
658 type_print (value_type (val), "", stream, -1);
659 fprintf_filtered (stream, ") ");
660 }
661 }
662
663 if (!value_initialized (val))
664 fprintf_filtered (stream, " [uninitialized] ");
665
666 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_STRUCT))
667 {
668 /* Attempt to determine real type of object. */
669 real_type = value_rtti_type (val, &full, &top, &using_enc);
670 if (real_type)
671 {
672 /* We have RTTI information, so use it. */
673 val = value_full_object (val, real_type,
674 full, top, using_enc);
675 fprintf_filtered (stream, "(%s%s) ",
676 TYPE_NAME (real_type),
677 full ? "" : _(" [incomplete object]"));
678 /* Print out object: enclosing type is same as real_type if
679 full. */
680 val_print (value_enclosing_type (val),
681 0,
682 value_address (val), stream, 0,
683 val, &opts, current_language);
684 return;
685 /* Note: When we look up RTTI entries, we don't get any
686 information on const or volatile attributes. */
687 }
688 else if (type != check_typedef (value_enclosing_type (val)))
689 {
690 /* No RTTI information, so let's do our best. */
691 fprintf_filtered (stream, "(%s ?) ",
692 TYPE_NAME (value_enclosing_type (val)));
693 val_print (value_enclosing_type (val),
694 0,
695 value_address (val), stream, 0,
696 val, &opts, current_language);
697 return;
698 }
699 /* Otherwise, we end up at the return outside this "if". */
700 }
701
702 val_print (val_type,
703 value_embedded_offset (val),
704 value_address (val),
705 stream, 0,
706 val, &opts, current_language);
707 }
This page took 0.045608 seconds and 5 git commands to generate.