Use amd64_target_description to get tdesc_amd64
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
c906108c 1/* Support for printing C++ values for GDB, the GNU debugger.
a2bd3dcd 2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
04ea0df1 21#include "gdb_obstack.h"
c906108c
SS
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"
c906108c
SS
30#include "c-lang.h"
31#include "target.h"
b9d652ac 32#include "cp-abi.h"
973177d3 33#include "valprint.h"
d3cbe7ef 34#include "cp-support.h"
cf309262 35#include "language.h"
6dddc817 36#include "extension.h"
79d43c61 37#include "typeprint.h"
26fcd5d7 38#include "byte-vector.h"
c906108c 39
aff410f1 40/* Controls printing of vtbl's. */
920d2a44
AC
41static void
42show_vtblprint (struct ui_file *file, int from_tty,
43 struct cmd_list_element *c, const char *value)
44{
45 fprintf_filtered (file, _("\
46Printing of C++ virtual function tables is %s.\n"),
47 value);
48}
49
50/* Controls looking up an object's derived type using what we find in
51 its vtables. */
920d2a44
AC
52static void
53show_objectprint (struct ui_file *file, int from_tty,
54 struct cmd_list_element *c,
55 const char *value)
56{
57 fprintf_filtered (file, _("\
58Printing of object's derived type based on vtable info is %s.\n"),
59 value);
60}
61
920d2a44
AC
62static void
63show_static_field_print (struct ui_file *file, int from_tty,
aff410f1
MS
64 struct cmd_list_element *c,
65 const char *value)
920d2a44 66{
aff410f1
MS
67 fprintf_filtered (file,
68 _("Printing of C++ static members is %s.\n"),
920d2a44
AC
69 value);
70}
71
c906108c
SS
72
73static struct obstack dont_print_vb_obstack;
74static struct obstack dont_print_statmem_obstack;
ec31cde5 75static struct obstack dont_print_stat_array_obstack;
c906108c 76
a14ed312 77extern void _initialize_cp_valprint (void);
392a587b 78
6943961c 79static void cp_print_static_field (struct type *, struct value *,
79a45b7d
TT
80 struct ui_file *, int,
81 const struct value_print_options *);
c906108c 82
aff410f1 83static void cp_print_value (struct type *, struct type *,
65408fa6 84 LONGEST,
aff410f1 85 CORE_ADDR, struct ui_file *,
e8b24d9f 86 int, struct value *,
aff410f1
MS
87 const struct value_print_options *,
88 struct type **);
c906108c 89
c906108c 90
8343f86c 91/* GCC versions after 2.4.5 use this. */
ddb6d633 92extern const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 93
c906108c
SS
94/* Return truth value for assertion that TYPE is of the type
95 "pointer to virtual function". */
96
97int
fba45db2 98cp_is_vtbl_ptr_type (struct type *type)
c906108c 99{
fe978cb0 100 const char *type_name = type_name_no_tag (type);
c906108c 101
fe978cb0 102 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
c906108c
SS
103}
104
105/* Return truth value for the assertion that TYPE is of the type
106 "pointer to virtual function table". */
107
108int
fba45db2 109cp_is_vtbl_member (struct type *type)
c906108c 110{
aff410f1
MS
111 /* With older versions of g++, the vtbl field pointed to an array of
112 structures. Nowadays it points directly to the structure. */
c906108c
SS
113 if (TYPE_CODE (type) == TYPE_CODE_PTR)
114 {
115 type = TYPE_TARGET_TYPE (type);
116 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
117 {
118 type = TYPE_TARGET_TYPE (type);
aff410f1
MS
119 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
120 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
121 {
122 /* Virtual functions tables are full of pointers
aff410f1 123 to virtual functions. */
c906108c
SS
124 return cp_is_vtbl_ptr_type (type);
125 }
126 }
0e5e3ea6
PS
127 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
128 {
129 return cp_is_vtbl_ptr_type (type);
130 }
131 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
132 {
aff410f1
MS
133 /* The type name of the thunk pointer is NULL when using
134 dwarf2. We could test for a pointer to a function, but
135 there is no type info for the virtual table either, so it
136 wont help. */
0e5e3ea6
PS
137 return cp_is_vtbl_ptr_type (type);
138 }
c906108c
SS
139 }
140 return 0;
141}
142
143/* Mutually recursive subroutines of cp_print_value and c_val_print to
aff410f1
MS
144 print out a structure's fields: cp_print_value_fields and
145 cp_print_value.
c5aa993b 146
aff410f1
MS
147 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
148 meanings as in cp_print_value and c_val_print.
c906108c 149
aff410f1
MS
150 2nd argument REAL_TYPE is used to carry over the type of the
151 derived class across the recursion to base classes.
c906108c 152
aff410f1
MS
153 DONT_PRINT is an array of baseclass types that we should not print,
154 or zero if called from top level. */
c906108c
SS
155
156void
a2bd3dcd 157cp_print_value_fields (struct type *type, struct type *real_type,
65408fa6 158 LONGEST offset,
aff410f1 159 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 160 int recurse, struct value *val,
79a45b7d 161 const struct value_print_options *options,
aff410f1
MS
162 struct type **dont_print_vb,
163 int dont_print_statmem)
c906108c
SS
164{
165 int i, len, n_baseclasses;
c906108c 166 int fields_seen = 0;
ec31cde5 167 static int last_set_recurse = -1;
c906108c 168
f168693b 169 type = check_typedef (type);
99903ae3 170
ec31cde5
CM
171 if (recurse == 0)
172 {
aff410f1
MS
173 /* Any object can be left on obstacks only during an unexpected
174 error. */
6036c742 175
ec31cde5 176 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
6036c742
JK
177 {
178 obstack_free (&dont_print_statmem_obstack, NULL);
aff410f1
MS
179 obstack_begin (&dont_print_statmem_obstack,
180 32 * sizeof (CORE_ADDR));
6036c742 181 }
ec31cde5 182 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
6036c742
JK
183 {
184 obstack_free (&dont_print_stat_array_obstack, NULL);
185 obstack_begin (&dont_print_stat_array_obstack,
186 32 * sizeof (struct type *));
187 }
ec31cde5 188 }
c906108c
SS
189
190 fprintf_filtered (stream, "{");
191 len = TYPE_NFIELDS (type);
192 n_baseclasses = TYPE_N_BASECLASSES (type);
193
194 /* First, print out baseclasses such that we don't print
195 duplicates of virtual baseclasses. */
196
197 if (n_baseclasses > 0)
65408fa6 198 cp_print_value (type, real_type,
aff410f1
MS
199 offset, address, stream,
200 recurse + 1, val, options,
201 dont_print_vb);
c906108c
SS
202
203 /* Second, print out data fields */
204
086280be
UW
205 /* If there are no data fields, skip this part */
206 if (len == n_baseclasses || !len)
c906108c
SS
207 fprintf_filtered (stream, "<No data fields>");
208 else
209 {
241fd515
AM
210 size_t statmem_obstack_initial_size = 0;
211 size_t stat_array_obstack_initial_size = 0;
7977e5d2
TT
212 struct type *vptr_basetype = NULL;
213 int vptr_fieldno;
214
c906108c
SS
215 if (dont_print_statmem == 0)
216 {
f56dcb88 217 statmem_obstack_initial_size =
0b66f317 218 obstack_object_size (&dont_print_statmem_obstack);
ec31cde5
CM
219
220 if (last_set_recurse != recurse)
221 {
f56dcb88
CM
222 stat_array_obstack_initial_size =
223 obstack_object_size (&dont_print_stat_array_obstack);
c5504eaf 224
ec31cde5
CM
225 last_set_recurse = recurse;
226 }
c906108c
SS
227 }
228
7977e5d2 229 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
c906108c
SS
230 for (i = n_baseclasses; i < len; i++)
231 {
65408fa6
YQ
232 const gdb_byte *valaddr = value_contents_for_printing (val);
233
c906108c 234 /* If requested, skip printing of static fields. */
79a45b7d 235 if (!options->static_field_print
d6a843b5 236 && field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
237 continue;
238
c906108c
SS
239 if (fields_seen)
240 fprintf_filtered (stream, ", ");
241 else if (n_baseclasses > 0)
242 {
2a998fc0 243 if (options->prettyformat)
c906108c
SS
244 {
245 fprintf_filtered (stream, "\n");
246 print_spaces_filtered (2 + 2 * recurse, stream);
247 fputs_filtered ("members of ", stream);
248 fputs_filtered (type_name_no_tag (type), stream);
249 fputs_filtered (": ", stream);
250 }
251 }
252 fields_seen = 1;
253
2a998fc0 254 if (options->prettyformat)
c906108c
SS
255 {
256 fprintf_filtered (stream, "\n");
257 print_spaces_filtered (2 + 2 * recurse, stream);
258 }
c5aa993b 259 else
c906108c
SS
260 {
261 wrap_here (n_spaces (2 + 2 * recurse));
262 }
e93a8774
TT
263
264 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
265
266 if (field_is_static (&TYPE_FIELD (type, i)))
267 fputs_filtered ("static ", stream);
268 fprintf_symbol_filtered (stream,
269 TYPE_FIELD_NAME (type, i),
270 current_language->la_language,
271 DMGL_PARAMS | DMGL_ANSI);
272 annotate_field_name_end ();
273 /* Do not print leading '=' in case of anonymous
274 unions. */
275 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
276 fputs_filtered (" = ", stream);
277 annotate_field_value ();
c906108c 278
d6a843b5
JK
279 if (!field_is_static (&TYPE_FIELD (type, i))
280 && TYPE_FIELD_PACKED (type, i))
c906108c 281 {
6943961c 282 struct value *v;
c906108c 283
aff410f1
MS
284 /* Bitfields require special handling, especially due to
285 byte order problems. */
c906108c
SS
286 if (TYPE_FIELD_IGNORE (type, i))
287 {
c5aa993b 288 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 289 }
8cf6f0b1
TT
290 else if (value_bits_synthetic_pointer (val,
291 TYPE_FIELD_BITPOS (type,
292 i),
293 TYPE_FIELD_BITSIZE (type,
294 i)))
295 {
296 fputs_filtered (_("<synthetic pointer>"), stream);
297 }
c906108c
SS
298 else
299 {
79a45b7d 300 struct value_print_options opts = *options;
c5504eaf 301
79a45b7d 302 opts.deref_ref = 0;
5467c6c8
PA
303
304 v = value_field_bitfield (type, i, valaddr, offset, val);
c906108c 305
79a45b7d 306 common_val_print (v, stream, recurse + 1, &opts,
d8ca156b 307 current_language);
c906108c
SS
308 }
309 }
310 else
311 {
312 if (TYPE_FIELD_IGNORE (type, i))
313 {
aff410f1
MS
314 fputs_filtered ("<optimized out or zero length>",
315 stream);
c906108c 316 }
d6a843b5 317 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 318 {
ee86786c
TT
319 struct value *v = NULL;
320
492d29ea 321 TRY
ee86786c
TT
322 {
323 v = value_static_field (type, i);
324 }
325
492d29ea
PA
326 CATCH (ex, RETURN_MASK_ERROR)
327 {
328 fprintf_filtered (stream,
329 _("<error reading variable: %s>"),
330 ex.message);
331 }
332 END_CATCH
333
686d4def
PA
334 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
335 v, stream, recurse + 1,
336 options);
c906108c 337 }
7977e5d2 338 else if (i == vptr_fieldno && type == vptr_basetype)
410528f0 339 {
a72c8f6a
JK
340 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
341 struct type *i_type = TYPE_FIELD_TYPE (type, i);
342
343 if (valprint_check_validity (stream, i_type, i_offset, val))
344 {
345 CORE_ADDR addr;
346
347 addr = extract_typed_address (valaddr + i_offset, i_type);
edf0c1b7
TT
348 print_function_pointer_address (options,
349 get_type_arch (type),
350 addr, stream);
a72c8f6a 351 }
410528f0 352 }
c906108c
SS
353 else
354 {
79a45b7d 355 struct value_print_options opts = *options;
c5504eaf 356
79a45b7d 357 opts.deref_ref = 0;
c5aa993b 358 val_print (TYPE_FIELD_TYPE (type, i),
aff410f1 359 offset + TYPE_FIELD_BITPOS (type, i) / 8,
edf3d5f3 360 address,
0e03807e 361 stream, recurse + 1, val, &opts,
d8ca156b 362 current_language);
c906108c
SS
363 }
364 }
365 annotate_field_end ();
366 }
367
368 if (dont_print_statmem == 0)
369 {
241fd515 370 size_t obstack_final_size =
0b66f317
CM
371 obstack_object_size (&dont_print_statmem_obstack);
372
aff410f1
MS
373 if (obstack_final_size > statmem_obstack_initial_size)
374 {
375 /* In effect, a pop of the printed-statics stack. */
0b66f317 376
aff410f1 377 void *free_to_ptr =
1ae1b8cc 378 (char *) obstack_next_free (&dont_print_statmem_obstack) -
aff410f1 379 (obstack_final_size - statmem_obstack_initial_size);
0b66f317 380
aff410f1
MS
381 obstack_free (&dont_print_statmem_obstack,
382 free_to_ptr);
383 }
ec31cde5
CM
384
385 if (last_set_recurse != recurse)
386 {
241fd515 387 size_t obstack_final_size =
f56dcb88
CM
388 obstack_object_size (&dont_print_stat_array_obstack);
389
390 if (obstack_final_size > stat_array_obstack_initial_size)
391 {
392 void *free_to_ptr =
1ae1b8cc 393 (char *) obstack_next_free (&dont_print_stat_array_obstack)
aff410f1
MS
394 - (obstack_final_size
395 - stat_array_obstack_initial_size);
f56dcb88
CM
396
397 obstack_free (&dont_print_stat_array_obstack,
398 free_to_ptr);
399 }
ec31cde5
CM
400 last_set_recurse = -1;
401 }
c906108c
SS
402 }
403
2a998fc0 404 if (options->prettyformat)
c906108c
SS
405 {
406 fprintf_filtered (stream, "\n");
407 print_spaces_filtered (2 * recurse, stream);
408 }
c5aa993b 409 } /* if there are data fields */
c5aa993b 410
c906108c
SS
411 fprintf_filtered (stream, "}");
412}
413
edf3d5f3
TT
414/* Like cp_print_value_fields, but find the runtime type of the object
415 and pass it as the `real_type' argument to cp_print_value_fields.
416 This function is a hack to work around the fact that
417 common_val_print passes the embedded offset to val_print, but not
418 the enclosing type. */
419
420void
421cp_print_value_fields_rtti (struct type *type,
6b850546 422 const gdb_byte *valaddr, LONGEST offset,
edf3d5f3
TT
423 CORE_ADDR address,
424 struct ui_file *stream, int recurse,
e8b24d9f 425 struct value *val,
edf3d5f3 426 const struct value_print_options *options,
c5504eaf
MS
427 struct type **dont_print_vb,
428 int dont_print_statmem)
edf3d5f3 429{
0e03807e
TT
430 struct type *real_type = NULL;
431
432 /* We require all bits to be valid in order to attempt a
433 conversion. */
9a0dc9e3
PA
434 if (!value_bits_any_optimized_out (val,
435 TARGET_CHAR_BIT * offset,
436 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
0e03807e
TT
437 {
438 struct value *value;
6b850546
DT
439 int full, using_enc;
440 LONGEST top;
0e03807e
TT
441
442 /* Ugh, we have to convert back to a value here. */
443 value = value_from_contents_and_address (type, valaddr + offset,
444 address + offset);
9f1f738a 445 type = value_type (value);
aff410f1
MS
446 /* We don't actually care about most of the result here -- just
447 the type. We already have the correct offset, due to how
448 val_print was initially called. */
0e03807e
TT
449 real_type = value_rtti_type (value, &full, &top, &using_enc);
450 }
451
edf3d5f3
TT
452 if (!real_type)
453 real_type = type;
454
65408fa6 455 cp_print_value_fields (type, real_type, offset,
0e03807e 456 address, stream, recurse, val, options,
edf3d5f3
TT
457 dont_print_vb, dont_print_statmem);
458}
459
aff410f1
MS
460/* Special val_print routine to avoid printing multiple copies of
461 virtual baseclasses. */
c906108c
SS
462
463static void
a2bd3dcd 464cp_print_value (struct type *type, struct type *real_type,
65408fa6 465 LONGEST offset,
aff410f1 466 CORE_ADDR address, struct ui_file *stream,
e8b24d9f 467 int recurse, struct value *val,
79a45b7d
TT
468 const struct value_print_options *options,
469 struct type **dont_print_vb)
c906108c 470{
c906108c 471 struct type **last_dont_print
2c63a960 472 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 473 struct obstack tmp_obstack = dont_print_vb_obstack;
c906108c 474 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
6b850546 475 LONGEST thisoffset;
b9d652ac 476 struct type *thistype;
65408fa6 477 const gdb_byte *valaddr = value_contents_for_printing (val);
c906108c
SS
478
479 if (dont_print_vb == 0)
480 {
aff410f1
MS
481 /* If we're at top level, carve out a completely fresh chunk of
482 the obstack and use that until this particular invocation
483 returns. */
c906108c
SS
484 /* Bump up the high-water mark. Now alpha is omega. */
485 obstack_finish (&dont_print_vb_obstack);
486 }
487
488 for (i = 0; i < n_baseclasses; i++)
489 {
6b850546 490 LONGEST boffset = 0;
7556d4a4 491 int skip = 0;
c906108c 492 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
0d5cff50 493 const char *basename = TYPE_NAME (baseclass);
e8b24d9f 494 struct value *base_val = NULL;
c906108c
SS
495
496 if (BASETYPE_VIA_VIRTUAL (type, i))
497 {
498 struct type **first_dont_print
2c63a960 499 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 500
aff410f1
MS
501 int j = (struct type **)
502 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
c906108c
SS
503
504 while (--j >= 0)
505 if (baseclass == first_dont_print[j])
506 goto flush_it;
507
508 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
509 }
510
b9d652ac
DJ
511 thisoffset = offset;
512 thistype = real_type;
086280be 513
492d29ea 514 TRY
c5aa993b 515 {
8af8e3bc
PA
516 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
517 }
492d29ea 518 CATCH (ex, RETURN_MASK_ERROR)
7556d4a4
PA
519 {
520 if (ex.error == NOT_AVAILABLE_ERROR)
521 skip = -1;
522 else
523 skip = 1;
524 }
492d29ea 525 END_CATCH
c906108c 526
7556d4a4
PA
527 if (skip == 0)
528 {
8af8e3bc 529 if (BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b 530 {
8af8e3bc
PA
531 /* The virtual base class pointer might have been
532 clobbered by the user program. Make sure that it
533 still points to a valid memory location. */
534
535 if ((boffset + offset) < 0
536 || (boffset + offset) >= TYPE_LENGTH (real_type))
537 {
26fcd5d7 538 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
d5161074 539
26fcd5d7 540 if (target_read_memory (address + boffset, buf.data (),
8af8e3bc
PA
541 TYPE_LENGTH (baseclass)) != 0)
542 skip = 1;
543 base_val = value_from_contents_and_address (baseclass,
26fcd5d7 544 buf.data (),
8af8e3bc 545 address + boffset);
9f1f738a 546 baseclass = value_type (base_val);
8af8e3bc
PA
547 thisoffset = 0;
548 boffset = 0;
549 thistype = baseclass;
8af8e3bc
PA
550 }
551 else
552 {
8af8e3bc
PA
553 base_val = val;
554 }
c5aa993b
JM
555 }
556 else
de4127a3 557 {
de4127a3
PA
558 base_val = val;
559 }
c906108c
SS
560 }
561
aff410f1 562 /* Now do the printing. */
2a998fc0 563 if (options->prettyformat)
c906108c
SS
564 {
565 fprintf_filtered (stream, "\n");
566 print_spaces_filtered (2 * recurse, stream);
567 }
568 fputs_filtered ("<", stream);
aff410f1
MS
569 /* Not sure what the best notation is in the case where there is
570 no baseclass name. */
c906108c
SS
571 fputs_filtered (basename ? basename : "", stream);
572 fputs_filtered ("> = ", stream);
573
8af8e3bc
PA
574 if (skip < 0)
575 val_print_unavailable (stream);
576 else if (skip > 0)
577 val_print_invalid_address (stream);
c906108c 578 else
a6bac58e
TT
579 {
580 int result = 0;
581
6dddc817 582 /* Attempt to run an extension language pretty-printer on the
a6bac58e
TT
583 baseclass if possible. */
584 if (!options->raw)
6dddc817 585 result
668e1674 586 = apply_ext_lang_val_pretty_printer (baseclass,
6dddc817
DE
587 thisoffset + boffset,
588 value_address (base_val),
589 stream, recurse,
590 base_val, options,
591 current_language);
de4127a3 592
a6bac58e 593 if (!result)
65408fa6 594 cp_print_value_fields (baseclass, thistype,
de4127a3
PA
595 thisoffset + boffset,
596 value_address (base_val),
597 stream, recurse, base_val, options,
a6bac58e
TT
598 ((struct type **)
599 obstack_base (&dont_print_vb_obstack)),
600 0);
601 }
c906108c
SS
602 fputs_filtered (", ", stream);
603
604 flush_it:
605 ;
606 }
607
608 if (dont_print_vb == 0)
609 {
610 /* Free the space used to deal with the printing
c5aa993b 611 of this type from top level. */
c906108c
SS
612 obstack_free (&dont_print_vb_obstack, last_dont_print);
613 /* Reset watermark so that we can continue protecting
c5aa993b 614 ourselves from whatever we were protecting ourselves. */
c906108c
SS
615 dont_print_vb_obstack = tmp_obstack;
616 }
617}
618
aff410f1
MS
619/* Print value of a static member. To avoid infinite recursion when
620 printing a class that contains a static instance of the class, we
621 keep the addresses of all printed static member classes in an
622 obstack and refuse to print them more than once.
c906108c 623
79a45b7d 624 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
c906108c
SS
625 have the same meanings as in c_val_print. */
626
627static void
2c63a960 628cp_print_static_field (struct type *type,
6943961c 629 struct value *val,
2c63a960 630 struct ui_file *stream,
2c63a960 631 int recurse,
79a45b7d 632 const struct value_print_options *options)
c906108c 633{
79a45b7d 634 struct value_print_options opts;
686d4def
PA
635
636 if (value_entirely_optimized_out (val))
637 {
638 val_print_optimized_out (val, stream);
639 return;
640 }
641
c906108c
SS
642 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
643 {
644 CORE_ADDR *first_dont_print;
42ae5230 645 CORE_ADDR addr;
c906108c
SS
646 int i;
647
648 first_dont_print
c5aa993b 649 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
99903ae3
CM
650 i = obstack_object_size (&dont_print_statmem_obstack)
651 / sizeof (CORE_ADDR);
c906108c
SS
652
653 while (--i >= 0)
654 {
42ae5230 655 if (value_address (val) == first_dont_print[i])
c906108c 656 {
2c63a960
JB
657 fputs_filtered ("<same as static member of an already"
658 " seen type>",
c906108c
SS
659 stream);
660 return;
661 }
662 }
663
42ae5230
TT
664 addr = value_address (val);
665 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
c906108c 666 sizeof (CORE_ADDR));
f168693b 667 type = check_typedef (type);
edf3d5f3 668 cp_print_value_fields (type, value_enclosing_type (val),
42ae5230 669 value_embedded_offset (val), addr,
aff410f1
MS
670 stream, recurse, val,
671 options, NULL, 1);
c906108c
SS
672 return;
673 }
79a45b7d 674
ec31cde5
CM
675 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
676 {
677 struct type **first_dont_print;
678 int i;
679 struct type *target_type = TYPE_TARGET_TYPE (type);
680
681 first_dont_print
682 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
683 i = obstack_object_size (&dont_print_stat_array_obstack)
1e9beacb 684 / sizeof (struct type *);
ec31cde5
CM
685
686 while (--i >= 0)
687 {
688 if (target_type == first_dont_print[i])
689 {
690 fputs_filtered ("<same as static member of an already"
691 " seen type>",
692 stream);
693 return;
694 }
695 }
696
aff410f1
MS
697 obstack_grow (&dont_print_stat_array_obstack,
698 (char *) &target_type,
ec31cde5
CM
699 sizeof (struct type *));
700 }
701
79a45b7d
TT
702 opts = *options;
703 opts.deref_ref = 0;
e8b24d9f 704 val_print (type,
aff410f1
MS
705 value_embedded_offset (val),
706 value_address (val),
707 stream, recurse, val,
708 &opts, current_language);
c906108c
SS
709}
710
09e2d7c7
DE
711/* Find the field in *SELF, or its non-virtual base classes, with
712 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
aff410f1 713 to the containing field number. If OFFSET is not exactly at the
09e2d7c7 714 start of some field, set *SELF to NULL. */
0d5de010 715
2c0b251b 716static void
09e2d7c7 717cp_find_class_member (struct type **self_p, int *fieldno,
0d5de010
DJ
718 LONGEST offset)
719{
09e2d7c7 720 struct type *self;
0d5de010
DJ
721 unsigned int i;
722 unsigned len;
723
09e2d7c7
DE
724 *self_p = check_typedef (*self_p);
725 self = *self_p;
726 len = TYPE_NFIELDS (self);
0d5de010 727
09e2d7c7 728 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
0d5de010 729 {
09e2d7c7 730 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
0d5de010
DJ
731
732 QUIT;
733 if (offset == bitpos)
734 {
735 *fieldno = i;
736 return;
737 }
738 }
739
09e2d7c7 740 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
0d5de010 741 {
09e2d7c7
DE
742 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
743 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
0d5de010
DJ
744
745 if (offset >= bitpos && offset < bitpos + bitsize)
746 {
09e2d7c7
DE
747 *self_p = TYPE_FIELD_TYPE (self, i);
748 cp_find_class_member (self_p, fieldno, offset - bitpos);
0d5de010
DJ
749 return;
750 }
751 }
752
09e2d7c7 753 *self_p = NULL;
0d5de010
DJ
754}
755
c906108c 756void
ad4820ab 757cp_print_class_member (const gdb_byte *valaddr, struct type *type,
a121b7c1 758 struct ui_file *stream, const char *prefix)
c906108c 759{
e17a4113
UW
760 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
761
09e2d7c7 762 /* VAL is a byte offset into the structure type SELF_TYPE.
c906108c
SS
763 Find the name of the field for that offset and
764 print it. */
09e2d7c7 765 struct type *self_type = TYPE_SELF_TYPE (type);
e17a4113 766 LONGEST val;
9f8afa72 767 int fieldno;
c906108c 768
aff410f1
MS
769 val = extract_signed_integer (valaddr,
770 TYPE_LENGTH (type),
771 byte_order);
e17a4113 772
0d5de010
DJ
773 /* Pointers to data members are usually byte offsets into an object.
774 Because a data member can have offset zero, and a NULL pointer to
775 member must be distinct from any valid non-NULL pointer to
776 member, either the value is biased or the NULL value has a
777 special representation; both are permitted by ISO C++. HP aCC
778 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
779 and other compilers which use the Itanium ABI use -1 as the NULL
780 value. GDB only supports that last form; to add support for
781 another form, make this into a cp-abi hook. */
c906108c 782
0d5de010 783 if (val == -1)
c906108c 784 {
0d5de010
DJ
785 fprintf_filtered (stream, "NULL");
786 return;
c906108c 787 }
0d5de010 788
09e2d7c7 789 cp_find_class_member (&self_type, &fieldno, val << 3);
0d5de010 790
09e2d7c7 791 if (self_type != NULL)
c906108c 792 {
0d5cff50 793 const char *name;
c5504eaf 794
306d9ac5 795 fputs_filtered (prefix, stream);
09e2d7c7 796 name = type_name_no_tag (self_type);
c906108c 797 if (name)
c5aa993b 798 fputs_filtered (name, stream);
c906108c 799 else
09e2d7c7 800 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
c906108c 801 fprintf_filtered (stream, "::");
09e2d7c7 802 fputs_filtered (TYPE_FIELD_NAME (self_type, fieldno), stream);
c906108c
SS
803 }
804 else
0d5de010 805 fprintf_filtered (stream, "%ld", (long) val);
c906108c
SS
806}
807
808
c906108c 809void
fba45db2 810_initialize_cp_valprint (void)
c906108c 811{
5bf193a2 812 add_setshow_boolean_cmd ("static-members", class_support,
79a45b7d 813 &user_print_options.static_field_print, _("\
5bf193a2
AC
814Set printing of C++ static members."), _("\
815Show printing of C++ static members."), NULL,
816 NULL,
920d2a44 817 show_static_field_print,
5bf193a2 818 &setprintlist, &showprintlist);
c906108c 819
79a45b7d
TT
820 add_setshow_boolean_cmd ("vtbl", class_support,
821 &user_print_options.vtblprint, _("\
5bf193a2
AC
822Set printing of C++ virtual function tables."), _("\
823Show printing of C++ virtual function tables."), NULL,
824 NULL,
920d2a44 825 show_vtblprint,
5bf193a2
AC
826 &setprintlist, &showprintlist);
827
79a45b7d
TT
828 add_setshow_boolean_cmd ("object", class_support,
829 &user_print_options.objectprint, _("\
5bf193a2
AC
830Set printing of object's derived type based on vtable info."), _("\
831Show printing of object's derived type based on vtable info."), NULL,
832 NULL,
920d2a44 833 show_objectprint,
5bf193a2 834 &setprintlist, &showprintlist);
c906108c 835
aff410f1
MS
836 obstack_begin (&dont_print_stat_array_obstack,
837 32 * sizeof (struct type *));
838 obstack_begin (&dont_print_statmem_obstack,
839 32 * sizeof (CORE_ADDR));
840 obstack_begin (&dont_print_vb_obstack,
841 32 * sizeof (struct type *));
c906108c 842}
This page took 1.196236 seconds and 4 git commands to generate.