Change extension language pretty-printers to use value API
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
1 /* Support for printing C++ values for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2020 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 "gdb_obstack.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "demangle.h"
29 #include "annotate.h"
30 #include "c-lang.h"
31 #include "target.h"
32 #include "cp-abi.h"
33 #include "valprint.h"
34 #include "cp-support.h"
35 #include "language.h"
36 #include "extension.h"
37 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
39 #include "gdbarch.h"
40 #include "cli/cli-style.h"
41
42 static struct obstack dont_print_vb_obstack;
43 static struct obstack dont_print_statmem_obstack;
44 static struct obstack dont_print_stat_array_obstack;
45
46 static void cp_print_static_field (struct type *, struct value *,
47 struct ui_file *, int,
48 const struct value_print_options *);
49
50 static void cp_print_value (struct type *, struct type *,
51 LONGEST,
52 CORE_ADDR, struct ui_file *,
53 int, struct value *,
54 const struct value_print_options *,
55 struct type **);
56
57 static void cp_print_value (struct value *, struct ui_file *,
58 int, const struct value_print_options *,
59 struct type **);
60
61
62 /* GCC versions after 2.4.5 use this. */
63 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
64
65 /* Return truth value for assertion that TYPE is of the type
66 "pointer to virtual function". */
67
68 int
69 cp_is_vtbl_ptr_type (struct type *type)
70 {
71 const char *type_name = TYPE_NAME (type);
72
73 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
74 }
75
76 /* Return truth value for the assertion that TYPE is of the type
77 "pointer to virtual function table". */
78
79 int
80 cp_is_vtbl_member (struct type *type)
81 {
82 /* With older versions of g++, the vtbl field pointed to an array of
83 structures. Nowadays it points directly to the structure. */
84 if (TYPE_CODE (type) == TYPE_CODE_PTR)
85 {
86 type = TYPE_TARGET_TYPE (type);
87 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
88 {
89 type = TYPE_TARGET_TYPE (type);
90 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
91 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
92 {
93 /* Virtual functions tables are full of pointers
94 to virtual functions. */
95 return cp_is_vtbl_ptr_type (type);
96 }
97 }
98 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
99 {
100 return cp_is_vtbl_ptr_type (type);
101 }
102 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
103 {
104 /* The type name of the thunk pointer is NULL when using
105 dwarf2. We could test for a pointer to a function, but
106 there is no type info for the virtual table either, so it
107 wont help. */
108 return cp_is_vtbl_ptr_type (type);
109 }
110 }
111 return 0;
112 }
113
114 /* Mutually recursive subroutines of cp_print_value and c_val_print to
115 print out a structure's fields: cp_print_value_fields and
116 cp_print_value.
117
118 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
119 meanings as in cp_print_value and c_val_print.
120
121 2nd argument REAL_TYPE is used to carry over the type of the
122 derived class across the recursion to base classes.
123
124 DONT_PRINT is an array of baseclass types that we should not print,
125 or zero if called from top level. */
126
127 void
128 cp_print_value_fields (struct type *type, struct type *real_type,
129 LONGEST offset,
130 CORE_ADDR address, struct ui_file *stream,
131 int recurse, struct value *val,
132 const struct value_print_options *options,
133 struct type **dont_print_vb,
134 int dont_print_statmem)
135 {
136 int i, len, n_baseclasses;
137 int fields_seen = 0;
138 static int last_set_recurse = -1;
139
140 type = check_typedef (type);
141
142 if (recurse == 0)
143 {
144 /* Any object can be left on obstacks only during an unexpected
145 error. */
146
147 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
148 {
149 obstack_free (&dont_print_statmem_obstack, NULL);
150 obstack_begin (&dont_print_statmem_obstack,
151 32 * sizeof (CORE_ADDR));
152 }
153 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
154 {
155 obstack_free (&dont_print_stat_array_obstack, NULL);
156 obstack_begin (&dont_print_stat_array_obstack,
157 32 * sizeof (struct type *));
158 }
159 }
160
161 fprintf_filtered (stream, "{");
162 len = TYPE_NFIELDS (type);
163 n_baseclasses = TYPE_N_BASECLASSES (type);
164
165 /* First, print out baseclasses such that we don't print
166 duplicates of virtual baseclasses. */
167
168 if (n_baseclasses > 0)
169 cp_print_value (type, real_type,
170 offset, address, stream,
171 recurse + 1, val, options,
172 dont_print_vb);
173
174 /* Second, print out data fields */
175
176 /* If there are no data fields, skip this part */
177 if (len == n_baseclasses || !len)
178 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
179 else
180 {
181 size_t statmem_obstack_initial_size = 0;
182 size_t stat_array_obstack_initial_size = 0;
183 struct type *vptr_basetype = NULL;
184 int vptr_fieldno;
185
186 if (dont_print_statmem == 0)
187 {
188 statmem_obstack_initial_size =
189 obstack_object_size (&dont_print_statmem_obstack);
190
191 if (last_set_recurse != recurse)
192 {
193 stat_array_obstack_initial_size =
194 obstack_object_size (&dont_print_stat_array_obstack);
195
196 last_set_recurse = recurse;
197 }
198 }
199
200 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
201 for (i = n_baseclasses; i < len; i++)
202 {
203 const gdb_byte *valaddr = value_contents_for_printing (val);
204
205 /* If requested, skip printing of static fields. */
206 if (!options->static_field_print
207 && field_is_static (&TYPE_FIELD (type, i)))
208 continue;
209
210 if (fields_seen)
211 {
212 fputs_filtered (",", stream);
213 if (!options->prettyformat)
214 fputs_filtered (" ", stream);
215 }
216 else if (n_baseclasses > 0)
217 {
218 if (options->prettyformat)
219 {
220 fprintf_filtered (stream, "\n");
221 print_spaces_filtered (2 + 2 * recurse, stream);
222 fputs_filtered ("members of ", stream);
223 fputs_filtered (TYPE_NAME (type), stream);
224 fputs_filtered (":", stream);
225 }
226 }
227 fields_seen = 1;
228
229 if (options->prettyformat)
230 {
231 fprintf_filtered (stream, "\n");
232 print_spaces_filtered (2 + 2 * recurse, stream);
233 }
234 else
235 {
236 wrap_here (n_spaces (2 + 2 * recurse));
237 }
238
239 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
240
241 if (field_is_static (&TYPE_FIELD (type, i)))
242 {
243 fputs_filtered ("static ", stream);
244 fprintf_symbol_filtered (stream,
245 TYPE_FIELD_NAME (type, i),
246 current_language->la_language,
247 DMGL_PARAMS | DMGL_ANSI);
248 }
249 else
250 fputs_styled (TYPE_FIELD_NAME (type, i),
251 variable_name_style.style (), stream);
252 annotate_field_name_end ();
253
254 /* We tweak various options in a few cases below. */
255 value_print_options options_copy = *options;
256 value_print_options *opts = &options_copy;
257
258 /* Do not print leading '=' in case of anonymous
259 unions. */
260 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
261 fputs_filtered (" = ", stream);
262 else
263 {
264 /* If this is an anonymous field then we want to consider it
265 as though it is at its parent's depth when it comes to the
266 max print depth. */
267 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
268 ++opts->max_depth;
269 }
270 annotate_field_value ();
271
272 if (!field_is_static (&TYPE_FIELD (type, i))
273 && TYPE_FIELD_PACKED (type, i))
274 {
275 struct value *v;
276
277 /* Bitfields require special handling, especially due to
278 byte order problems. */
279 if (TYPE_FIELD_IGNORE (type, i))
280 {
281 fputs_styled ("<optimized out or zero length>",
282 metadata_style.style (), stream);
283 }
284 else if (value_bits_synthetic_pointer (val,
285 TYPE_FIELD_BITPOS (type,
286 i),
287 TYPE_FIELD_BITSIZE (type,
288 i)))
289 {
290 fputs_styled (_("<synthetic pointer>"),
291 metadata_style.style (), stream);
292 }
293 else
294 {
295 opts->deref_ref = 0;
296
297 v = value_field_bitfield (type, i, valaddr, offset, val);
298
299 common_val_print (v, stream, recurse + 1,
300 opts, current_language);
301 }
302 }
303 else
304 {
305 if (TYPE_FIELD_IGNORE (type, i))
306 {
307 fputs_styled ("<optimized out or zero length>",
308 metadata_style.style (), stream);
309 }
310 else if (field_is_static (&TYPE_FIELD (type, i)))
311 {
312 try
313 {
314 struct value *v = value_static_field (type, i);
315
316 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
317 v, stream, recurse + 1,
318 opts);
319 }
320 catch (const gdb_exception_error &ex)
321 {
322 fprintf_styled (stream, metadata_style.style (),
323 _("<error reading variable: %s>"),
324 ex.what ());
325 }
326 }
327 else if (i == vptr_fieldno && type == vptr_basetype)
328 {
329 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
330 struct type *i_type = TYPE_FIELD_TYPE (type, i);
331
332 if (valprint_check_validity (stream, i_type, i_offset, val))
333 {
334 CORE_ADDR addr;
335
336 i_offset += value_embedded_offset (val);
337 addr = extract_typed_address (valaddr + i_offset, i_type);
338 print_function_pointer_address (opts,
339 get_type_arch (type),
340 addr, stream);
341 }
342 }
343 else
344 {
345 opts->deref_ref = 0;
346 val_print (TYPE_FIELD_TYPE (type, i),
347 offset + TYPE_FIELD_BITPOS (type, i) / 8,
348 address,
349 stream, recurse + 1, val, opts,
350 current_language);
351 }
352 }
353 annotate_field_end ();
354 }
355
356 if (dont_print_statmem == 0)
357 {
358 size_t obstack_final_size =
359 obstack_object_size (&dont_print_statmem_obstack);
360
361 if (obstack_final_size > statmem_obstack_initial_size)
362 {
363 /* In effect, a pop of the printed-statics stack. */
364 size_t shrink_bytes
365 = statmem_obstack_initial_size - obstack_final_size;
366 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
367 }
368
369 if (last_set_recurse != recurse)
370 {
371 obstack_final_size =
372 obstack_object_size (&dont_print_stat_array_obstack);
373
374 if (obstack_final_size > stat_array_obstack_initial_size)
375 {
376 void *free_to_ptr =
377 (char *) obstack_next_free (&dont_print_stat_array_obstack)
378 - (obstack_final_size
379 - stat_array_obstack_initial_size);
380
381 obstack_free (&dont_print_stat_array_obstack,
382 free_to_ptr);
383 }
384 last_set_recurse = -1;
385 }
386 }
387
388 if (options->prettyformat)
389 {
390 fprintf_filtered (stream, "\n");
391 print_spaces_filtered (2 * recurse, stream);
392 }
393 } /* if there are data fields */
394
395 fprintf_filtered (stream, "}");
396 }
397
398 /* Mutually recursive subroutines of cp_print_value and c_value_print
399 to print out a structure's fields: cp_print_value_fields and
400 cp_print_value.
401
402 VAL, ADDRESS, STREAM, RECURSE, and OPTIONS have the same meanings
403 as in cp_print_value and c_value_print.
404
405 DONT_PRINT is an array of baseclass types that we should not print,
406 or zero if called from top level. */
407
408 void
409 cp_print_value_fields (struct value *val, struct ui_file *stream,
410 int recurse, const struct value_print_options *options,
411 struct type **dont_print_vb,
412 int dont_print_statmem)
413 {
414 int i, len, n_baseclasses;
415 int fields_seen = 0;
416 static int last_set_recurse = -1;
417
418 struct type *type = check_typedef (value_type (val));
419
420 if (recurse == 0)
421 {
422 /* Any object can be left on obstacks only during an unexpected
423 error. */
424
425 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
426 {
427 obstack_free (&dont_print_statmem_obstack, NULL);
428 obstack_begin (&dont_print_statmem_obstack,
429 32 * sizeof (CORE_ADDR));
430 }
431 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
432 {
433 obstack_free (&dont_print_stat_array_obstack, NULL);
434 obstack_begin (&dont_print_stat_array_obstack,
435 32 * sizeof (struct type *));
436 }
437 }
438
439 fprintf_filtered (stream, "{");
440 len = TYPE_NFIELDS (type);
441 n_baseclasses = TYPE_N_BASECLASSES (type);
442
443 /* First, print out baseclasses such that we don't print
444 duplicates of virtual baseclasses. */
445
446 if (n_baseclasses > 0)
447 cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
448
449 /* Second, print out data fields */
450
451 /* If there are no data fields, skip this part */
452 if (len == n_baseclasses || !len)
453 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
454 else
455 {
456 size_t statmem_obstack_initial_size = 0;
457 size_t stat_array_obstack_initial_size = 0;
458 struct type *vptr_basetype = NULL;
459 int vptr_fieldno;
460
461 if (dont_print_statmem == 0)
462 {
463 statmem_obstack_initial_size =
464 obstack_object_size (&dont_print_statmem_obstack);
465
466 if (last_set_recurse != recurse)
467 {
468 stat_array_obstack_initial_size =
469 obstack_object_size (&dont_print_stat_array_obstack);
470
471 last_set_recurse = recurse;
472 }
473 }
474
475 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
476 for (i = n_baseclasses; i < len; i++)
477 {
478 const gdb_byte *valaddr = value_contents_for_printing (val);
479
480 /* If requested, skip printing of static fields. */
481 if (!options->static_field_print
482 && field_is_static (&TYPE_FIELD (type, i)))
483 continue;
484
485 if (fields_seen)
486 {
487 fputs_filtered (",", stream);
488 if (!options->prettyformat)
489 fputs_filtered (" ", stream);
490 }
491 else if (n_baseclasses > 0)
492 {
493 if (options->prettyformat)
494 {
495 fprintf_filtered (stream, "\n");
496 print_spaces_filtered (2 + 2 * recurse, stream);
497 fputs_filtered ("members of ", stream);
498 fputs_filtered (TYPE_NAME (type), stream);
499 fputs_filtered (":", stream);
500 }
501 }
502 fields_seen = 1;
503
504 if (options->prettyformat)
505 {
506 fprintf_filtered (stream, "\n");
507 print_spaces_filtered (2 + 2 * recurse, stream);
508 }
509 else
510 {
511 wrap_here (n_spaces (2 + 2 * recurse));
512 }
513
514 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
515
516 if (field_is_static (&TYPE_FIELD (type, i)))
517 {
518 fputs_filtered ("static ", stream);
519 fprintf_symbol_filtered (stream,
520 TYPE_FIELD_NAME (type, i),
521 current_language->la_language,
522 DMGL_PARAMS | DMGL_ANSI);
523 }
524 else
525 fputs_styled (TYPE_FIELD_NAME (type, i),
526 variable_name_style.style (), stream);
527 annotate_field_name_end ();
528
529 /* We tweak various options in a few cases below. */
530 value_print_options options_copy = *options;
531 value_print_options *opts = &options_copy;
532
533 /* Do not print leading '=' in case of anonymous
534 unions. */
535 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
536 fputs_filtered (" = ", stream);
537 else
538 {
539 /* If this is an anonymous field then we want to consider it
540 as though it is at its parent's depth when it comes to the
541 max print depth. */
542 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
543 ++opts->max_depth;
544 }
545 annotate_field_value ();
546
547 if (!field_is_static (&TYPE_FIELD (type, i))
548 && TYPE_FIELD_PACKED (type, i))
549 {
550 struct value *v;
551
552 /* Bitfields require special handling, especially due to
553 byte order problems. */
554 if (TYPE_FIELD_IGNORE (type, i))
555 {
556 fputs_styled ("<optimized out or zero length>",
557 metadata_style.style (), stream);
558 }
559 else if (value_bits_synthetic_pointer (val,
560 TYPE_FIELD_BITPOS (type,
561 i),
562 TYPE_FIELD_BITSIZE (type,
563 i)))
564 {
565 fputs_styled (_("<synthetic pointer>"),
566 metadata_style.style (), stream);
567 }
568 else
569 {
570 opts->deref_ref = 0;
571
572 v = value_field_bitfield (type, i, valaddr,
573 value_embedded_offset (val), val);
574
575 common_val_print (v, stream, recurse + 1,
576 opts, current_language);
577 }
578 }
579 else
580 {
581 if (TYPE_FIELD_IGNORE (type, i))
582 {
583 fputs_styled ("<optimized out or zero length>",
584 metadata_style.style (), stream);
585 }
586 else if (field_is_static (&TYPE_FIELD (type, i)))
587 {
588 try
589 {
590 struct value *v = value_static_field (type, i);
591
592 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
593 v, stream, recurse + 1,
594 opts);
595 }
596 catch (const gdb_exception_error &ex)
597 {
598 fprintf_styled (stream, metadata_style.style (),
599 _("<error reading variable: %s>"),
600 ex.what ());
601 }
602 }
603 else if (i == vptr_fieldno && type == vptr_basetype)
604 {
605 int i_offset = TYPE_FIELD_BITPOS (type, i) / 8;
606 struct type *i_type = TYPE_FIELD_TYPE (type, i);
607
608 if (valprint_check_validity (stream, i_type, i_offset, val))
609 {
610 CORE_ADDR addr;
611
612 i_offset += value_embedded_offset (val);
613 addr = extract_typed_address (valaddr + i_offset, i_type);
614 print_function_pointer_address (opts,
615 get_type_arch (type),
616 addr, stream);
617 }
618 }
619 else
620 {
621 struct value *v = value_primitive_field (val, 0, i, type);
622 opts->deref_ref = 0;
623 common_val_print (v, stream, recurse + 1, opts,
624 current_language);
625 }
626 }
627 annotate_field_end ();
628 }
629
630 if (dont_print_statmem == 0)
631 {
632 size_t obstack_final_size =
633 obstack_object_size (&dont_print_statmem_obstack);
634
635 if (obstack_final_size > statmem_obstack_initial_size)
636 {
637 /* In effect, a pop of the printed-statics stack. */
638 size_t shrink_bytes
639 = statmem_obstack_initial_size - obstack_final_size;
640 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
641 }
642
643 if (last_set_recurse != recurse)
644 {
645 obstack_final_size =
646 obstack_object_size (&dont_print_stat_array_obstack);
647
648 if (obstack_final_size > stat_array_obstack_initial_size)
649 {
650 void *free_to_ptr =
651 (char *) obstack_next_free (&dont_print_stat_array_obstack)
652 - (obstack_final_size
653 - stat_array_obstack_initial_size);
654
655 obstack_free (&dont_print_stat_array_obstack,
656 free_to_ptr);
657 }
658 last_set_recurse = -1;
659 }
660 }
661
662 if (options->prettyformat)
663 {
664 fprintf_filtered (stream, "\n");
665 print_spaces_filtered (2 * recurse, stream);
666 }
667 } /* if there are data fields */
668
669 fprintf_filtered (stream, "}");
670 }
671
672 /* Like cp_print_value_fields, but find the runtime type of the object
673 and pass it as the `real_type' argument to cp_print_value_fields.
674 This function is a hack to work around the fact that
675 common_val_print passes the embedded offset to val_print, but not
676 the enclosing type. */
677
678 void
679 cp_print_value_fields_rtti (struct type *type,
680 const gdb_byte *valaddr, LONGEST offset,
681 CORE_ADDR address,
682 struct ui_file *stream, int recurse,
683 struct value *val,
684 const struct value_print_options *options,
685 struct type **dont_print_vb,
686 int dont_print_statmem)
687 {
688 struct type *real_type = NULL;
689
690 /* We require all bits to be valid in order to attempt a
691 conversion. */
692 if (!value_bits_any_optimized_out (val,
693 TARGET_CHAR_BIT * offset,
694 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
695 {
696 struct value *value;
697 int full, using_enc;
698 LONGEST top;
699
700 /* Ugh, we have to convert back to a value here. */
701 value = value_from_contents_and_address (type, valaddr + offset,
702 address + offset);
703 type = value_type (value);
704 /* We don't actually care about most of the result here -- just
705 the type. We already have the correct offset, due to how
706 val_print was initially called. */
707 real_type = value_rtti_type (value, &full, &top, &using_enc);
708 }
709
710 if (!real_type)
711 real_type = type;
712
713 cp_print_value_fields (type, real_type, offset,
714 address, stream, recurse, val, options,
715 dont_print_vb, dont_print_statmem);
716 }
717
718 /* Special value_print routine to avoid printing multiple copies of
719 virtual baseclasses. */
720
721 static void
722 cp_print_value (struct type *type, struct type *real_type,
723 LONGEST offset,
724 CORE_ADDR address, struct ui_file *stream,
725 int recurse, struct value *val,
726 const struct value_print_options *options,
727 struct type **dont_print_vb)
728 {
729 struct type **last_dont_print
730 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
731 struct obstack tmp_obstack = dont_print_vb_obstack;
732 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
733 LONGEST thisoffset;
734 struct type *thistype;
735 const gdb_byte *valaddr = value_contents_for_printing (val);
736
737 if (dont_print_vb == 0)
738 {
739 /* If we're at top level, carve out a completely fresh chunk of
740 the obstack and use that until this particular invocation
741 returns. */
742 /* Bump up the high-water mark. Now alpha is omega. */
743 obstack_finish (&dont_print_vb_obstack);
744 }
745
746 for (i = 0; i < n_baseclasses; i++)
747 {
748 LONGEST boffset = 0;
749 int skip = 0;
750 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
751 const char *basename = TYPE_NAME (baseclass);
752 struct value *base_val = NULL;
753
754 if (BASETYPE_VIA_VIRTUAL (type, i))
755 {
756 struct type **first_dont_print
757 = (struct type **) obstack_base (&dont_print_vb_obstack);
758
759 int j = (struct type **)
760 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
761
762 while (--j >= 0)
763 if (baseclass == first_dont_print[j])
764 goto flush_it;
765
766 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
767 }
768
769 thisoffset = offset;
770 thistype = real_type;
771
772 try
773 {
774 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
775 }
776 catch (const gdb_exception_error &ex)
777 {
778 if (ex.error == NOT_AVAILABLE_ERROR)
779 skip = -1;
780 else
781 skip = 1;
782 }
783
784 if (skip == 0)
785 {
786 if (BASETYPE_VIA_VIRTUAL (type, i))
787 {
788 /* The virtual base class pointer might have been
789 clobbered by the user program. Make sure that it
790 still points to a valid memory location. */
791
792 if ((boffset + offset) < 0
793 || (boffset + offset) >= TYPE_LENGTH (real_type))
794 {
795 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
796
797 if (target_read_memory (address + boffset, buf.data (),
798 TYPE_LENGTH (baseclass)) != 0)
799 skip = 1;
800 base_val = value_from_contents_and_address (baseclass,
801 buf.data (),
802 address + boffset);
803 baseclass = value_type (base_val);
804 thisoffset = 0;
805 boffset = 0;
806 thistype = baseclass;
807 }
808 else
809 {
810 base_val = val;
811 }
812 }
813 else
814 {
815 base_val = val;
816 }
817 }
818
819 /* Now do the printing. */
820 if (options->prettyformat)
821 {
822 fprintf_filtered (stream, "\n");
823 print_spaces_filtered (2 * recurse, stream);
824 }
825 fputs_filtered ("<", stream);
826 /* Not sure what the best notation is in the case where there is
827 no baseclass name. */
828 fputs_filtered (basename ? basename : "", stream);
829 fputs_filtered ("> = ", stream);
830
831 if (skip < 0)
832 val_print_unavailable (stream);
833 else if (skip > 0)
834 val_print_invalid_address (stream);
835 else
836 {
837 int result = 0;
838
839 if (options->max_depth > -1
840 && recurse >= options->max_depth)
841 {
842 const struct language_defn *language = current_language;
843 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
844 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
845 }
846 else
847 {
848 /* Attempt to run an extension language pretty-printer on the
849 baseclass if possible. */
850 if (!options->raw)
851 {
852 struct value *v
853 = value_from_component (base_val, baseclass,
854 thisoffset + boffset);
855 result
856 = apply_ext_lang_val_pretty_printer (v, stream, recurse,
857 options,
858 current_language);
859 }
860
861 if (!result)
862 cp_print_value_fields (baseclass, thistype,
863 thisoffset + boffset,
864 value_address (base_val),
865 stream, recurse, base_val, options,
866 ((struct type **)
867 obstack_base (&dont_print_vb_obstack)),
868 0);
869 }
870 }
871 fputs_filtered (", ", stream);
872
873 flush_it:
874 ;
875 }
876
877 if (dont_print_vb == 0)
878 {
879 /* Free the space used to deal with the printing
880 of this type from top level. */
881 obstack_free (&dont_print_vb_obstack, last_dont_print);
882 /* Reset watermark so that we can continue protecting
883 ourselves from whatever we were protecting ourselves. */
884 dont_print_vb_obstack = tmp_obstack;
885 }
886 }
887
888 /* Special val_print routine to avoid printing multiple copies of
889 virtual baseclasses. */
890
891 static void
892 cp_print_value (struct value *val, struct ui_file *stream,
893 int recurse, const struct value_print_options *options,
894 struct type **dont_print_vb)
895 {
896 struct type *type = check_typedef (value_type (val));
897 CORE_ADDR address = value_address (val);
898 struct type **last_dont_print
899 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
900 struct obstack tmp_obstack = dont_print_vb_obstack;
901 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
902 const gdb_byte *valaddr = value_contents_for_printing (val);
903
904 if (dont_print_vb == 0)
905 {
906 /* If we're at top level, carve out a completely fresh chunk of
907 the obstack and use that until this particular invocation
908 returns. */
909 /* Bump up the high-water mark. Now alpha is omega. */
910 obstack_finish (&dont_print_vb_obstack);
911 }
912
913 for (i = 0; i < n_baseclasses; i++)
914 {
915 LONGEST boffset = 0;
916 int skip = 0;
917 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
918 const char *basename = TYPE_NAME (baseclass);
919 struct value *base_val = NULL;
920
921 if (BASETYPE_VIA_VIRTUAL (type, i))
922 {
923 struct type **first_dont_print
924 = (struct type **) obstack_base (&dont_print_vb_obstack);
925
926 int j = (struct type **)
927 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
928
929 while (--j >= 0)
930 if (baseclass == first_dont_print[j])
931 goto flush_it;
932
933 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
934 }
935
936 try
937 {
938 boffset = baseclass_offset (type, i, valaddr,
939 value_embedded_offset (val),
940 address, val);
941 }
942 catch (const gdb_exception_error &ex)
943 {
944 if (ex.error == NOT_AVAILABLE_ERROR)
945 skip = -1;
946 else
947 skip = 1;
948 }
949
950 if (skip == 0)
951 {
952 if (BASETYPE_VIA_VIRTUAL (type, i))
953 {
954 /* The virtual base class pointer might have been
955 clobbered by the user program. Make sure that it
956 still points to a valid memory location. */
957
958 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
959 {
960 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
961
962 if (target_read_memory (address + boffset, buf.data (),
963 TYPE_LENGTH (baseclass)) != 0)
964 skip = 1;
965 base_val = value_from_contents_and_address (baseclass,
966 buf.data (),
967 address + boffset);
968 baseclass = value_type (base_val);
969 boffset = 0;
970 }
971 else
972 {
973 base_val = val;
974 }
975 }
976 else
977 {
978 base_val = val;
979 }
980 }
981
982 /* Now do the printing. */
983 if (options->prettyformat)
984 {
985 fprintf_filtered (stream, "\n");
986 print_spaces_filtered (2 * recurse, stream);
987 }
988 fputs_filtered ("<", stream);
989 /* Not sure what the best notation is in the case where there is
990 no baseclass name. */
991 fputs_filtered (basename ? basename : "", stream);
992 fputs_filtered ("> = ", stream);
993
994 if (skip < 0)
995 val_print_unavailable (stream);
996 else if (skip > 0)
997 val_print_invalid_address (stream);
998 else
999 {
1000 int result = 0;
1001
1002 if (options->max_depth > -1
1003 && recurse >= options->max_depth)
1004 {
1005 const struct language_defn *language = current_language;
1006 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
1007 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
1008 }
1009 else
1010 {
1011 struct value *baseclass_val = value_primitive_field (val, 0,
1012 i, type);
1013
1014 /* Attempt to run an extension language pretty-printer on the
1015 baseclass if possible. */
1016 if (!options->raw)
1017 result
1018 = apply_ext_lang_val_pretty_printer (baseclass_val, stream,
1019 recurse, options,
1020 current_language);
1021
1022 if (!result)
1023 cp_print_value_fields (baseclass_val, stream, recurse, options,
1024 ((struct type **)
1025 obstack_base (&dont_print_vb_obstack)),
1026 0);
1027 }
1028 }
1029 fputs_filtered (", ", stream);
1030
1031 flush_it:
1032 ;
1033 }
1034
1035 if (dont_print_vb == 0)
1036 {
1037 /* Free the space used to deal with the printing
1038 of this type from top level. */
1039 obstack_free (&dont_print_vb_obstack, last_dont_print);
1040 /* Reset watermark so that we can continue protecting
1041 ourselves from whatever we were protecting ourselves. */
1042 dont_print_vb_obstack = tmp_obstack;
1043 }
1044 }
1045
1046 /* Print value of a static member. To avoid infinite recursion when
1047 printing a class that contains a static instance of the class, we
1048 keep the addresses of all printed static member classes in an
1049 obstack and refuse to print them more than once.
1050
1051 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
1052 have the same meanings as in c_val_print. */
1053
1054 static void
1055 cp_print_static_field (struct type *type,
1056 struct value *val,
1057 struct ui_file *stream,
1058 int recurse,
1059 const struct value_print_options *options)
1060 {
1061 struct value_print_options opts;
1062
1063 if (value_entirely_optimized_out (val))
1064 {
1065 val_print_optimized_out (val, stream);
1066 return;
1067 }
1068
1069 struct type *real_type = check_typedef (type);
1070 if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
1071 {
1072 CORE_ADDR *first_dont_print;
1073 CORE_ADDR addr;
1074 int i;
1075
1076 first_dont_print
1077 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
1078 i = obstack_object_size (&dont_print_statmem_obstack)
1079 / sizeof (CORE_ADDR);
1080
1081 while (--i >= 0)
1082 {
1083 if (value_address (val) == first_dont_print[i])
1084 {
1085 fputs_styled (_("<same as static member of an already"
1086 " seen type>"),
1087 metadata_style.style (), stream);
1088 return;
1089 }
1090 }
1091
1092 addr = value_address (val);
1093 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
1094 sizeof (CORE_ADDR));
1095 cp_print_value_fields (type, value_enclosing_type (val),
1096 value_embedded_offset (val), addr,
1097 stream, recurse, val,
1098 options, NULL, 1);
1099 return;
1100 }
1101
1102 if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
1103 {
1104 struct type **first_dont_print;
1105 int i;
1106 struct type *target_type = TYPE_TARGET_TYPE (type);
1107
1108 first_dont_print
1109 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
1110 i = obstack_object_size (&dont_print_stat_array_obstack)
1111 / sizeof (struct type *);
1112
1113 while (--i >= 0)
1114 {
1115 if (target_type == first_dont_print[i])
1116 {
1117 fputs_styled (_("<same as static member of an already"
1118 " seen type>"),
1119 metadata_style.style (), stream);
1120 return;
1121 }
1122 }
1123
1124 obstack_grow (&dont_print_stat_array_obstack,
1125 (char *) &target_type,
1126 sizeof (struct type *));
1127 }
1128
1129 opts = *options;
1130 opts.deref_ref = 0;
1131 common_val_print (val, stream, recurse, &opts, current_language);
1132 }
1133
1134 /* Find the field in *SELF, or its non-virtual base classes, with
1135 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
1136 to the containing field number. If OFFSET is not exactly at the
1137 start of some field, set *SELF to NULL. */
1138
1139 static void
1140 cp_find_class_member (struct type **self_p, int *fieldno,
1141 LONGEST offset)
1142 {
1143 struct type *self;
1144 unsigned int i;
1145 unsigned len;
1146
1147 *self_p = check_typedef (*self_p);
1148 self = *self_p;
1149 len = TYPE_NFIELDS (self);
1150
1151 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
1152 {
1153 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
1154
1155 QUIT;
1156 if (offset == bitpos)
1157 {
1158 *fieldno = i;
1159 return;
1160 }
1161 }
1162
1163 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
1164 {
1165 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
1166 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
1167
1168 if (offset >= bitpos && offset < bitpos + bitsize)
1169 {
1170 *self_p = TYPE_FIELD_TYPE (self, i);
1171 cp_find_class_member (self_p, fieldno, offset - bitpos);
1172 return;
1173 }
1174 }
1175
1176 *self_p = NULL;
1177 }
1178
1179 void
1180 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
1181 struct ui_file *stream, const char *prefix)
1182 {
1183 enum bfd_endian byte_order = type_byte_order (type);
1184
1185 /* VAL is a byte offset into the structure type SELF_TYPE.
1186 Find the name of the field for that offset and
1187 print it. */
1188 struct type *self_type = TYPE_SELF_TYPE (type);
1189 LONGEST val;
1190 int fieldno;
1191
1192 val = extract_signed_integer (valaddr,
1193 TYPE_LENGTH (type),
1194 byte_order);
1195
1196 /* Pointers to data members are usually byte offsets into an object.
1197 Because a data member can have offset zero, and a NULL pointer to
1198 member must be distinct from any valid non-NULL pointer to
1199 member, either the value is biased or the NULL value has a
1200 special representation; both are permitted by ISO C++. HP aCC
1201 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
1202 and other compilers which use the Itanium ABI use -1 as the NULL
1203 value. GDB only supports that last form; to add support for
1204 another form, make this into a cp-abi hook. */
1205
1206 if (val == -1)
1207 {
1208 fprintf_filtered (stream, "NULL");
1209 return;
1210 }
1211
1212 cp_find_class_member (&self_type, &fieldno, val << 3);
1213
1214 if (self_type != NULL)
1215 {
1216 const char *name;
1217
1218 fputs_filtered (prefix, stream);
1219 name = TYPE_NAME (self_type);
1220 if (name)
1221 fputs_filtered (name, stream);
1222 else
1223 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
1224 fprintf_filtered (stream, "::");
1225 fputs_styled (TYPE_FIELD_NAME (self_type, fieldno),
1226 variable_name_style.style (), stream);
1227 }
1228 else
1229 fprintf_filtered (stream, "%ld", (long) val);
1230 }
1231
1232
1233 void _initialize_cp_valprint ();
1234 void
1235 _initialize_cp_valprint ()
1236 {
1237 obstack_begin (&dont_print_stat_array_obstack,
1238 32 * sizeof (struct type *));
1239 obstack_begin (&dont_print_statmem_obstack,
1240 32 * sizeof (CORE_ADDR));
1241 obstack_begin (&dont_print_vb_obstack,
1242 32 * sizeof (struct type *));
1243 }
This page took 0.065577 seconds and 5 git commands to generate.