]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
a2bd3dcd | 2 | |
213516ef | 3 | Copyright (C) 1986-2023 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" | |
bf31fd38 | 21 | #include "gdbsupport/gdb_obstack.h" |
4de283e4 TT |
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" | |
c906108c | 29 | #include "annotate.h" |
c906108c | 30 | #include "c-lang.h" |
4de283e4 | 31 | #include "target.h" |
b9d652ac | 32 | #include "cp-abi.h" |
4de283e4 | 33 | #include "valprint.h" |
d3cbe7ef | 34 | #include "cp-support.h" |
d55e5aa6 | 35 | #include "language.h" |
4de283e4 | 36 | #include "extension.h" |
79d43c61 | 37 | #include "typeprint.h" |
268a13a5 | 38 | #include "gdbsupport/byte-vector.h" |
0d12e84c | 39 | #include "gdbarch.h" |
7f6aba03 | 40 | #include "cli/cli-style.h" |
91254b91 SV |
41 | #include "gdbsupport/selftest.h" |
42 | #include "selftest-arch.h" | |
c906108c | 43 | |
c906108c SS |
44 | static struct obstack dont_print_vb_obstack; |
45 | static struct obstack dont_print_statmem_obstack; | |
ec31cde5 | 46 | static struct obstack dont_print_stat_array_obstack; |
c906108c | 47 | |
6943961c | 48 | static void cp_print_static_field (struct type *, struct value *, |
79a45b7d TT |
49 | struct ui_file *, int, |
50 | const struct value_print_options *); | |
c906108c | 51 | |
fbf54e75 TT |
52 | static void cp_print_value (struct value *, struct ui_file *, |
53 | int, const struct value_print_options *, | |
54 | struct type **); | |
55 | ||
c906108c | 56 | |
8343f86c | 57 | /* GCC versions after 2.4.5 use this. */ |
bad5c026 | 58 | const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
c906108c | 59 | |
c906108c SS |
60 | /* Return truth value for assertion that TYPE is of the type |
61 | "pointer to virtual function". */ | |
62 | ||
63 | int | |
fba45db2 | 64 | cp_is_vtbl_ptr_type (struct type *type) |
c906108c | 65 | { |
7d93a1e0 | 66 | const char *type_name = type->name (); |
c906108c | 67 | |
fe978cb0 | 68 | return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name)); |
c906108c SS |
69 | } |
70 | ||
71 | /* Return truth value for the assertion that TYPE is of the type | |
72 | "pointer to virtual function table". */ | |
73 | ||
74 | int | |
fba45db2 | 75 | cp_is_vtbl_member (struct type *type) |
c906108c | 76 | { |
aff410f1 MS |
77 | /* With older versions of g++, the vtbl field pointed to an array of |
78 | structures. Nowadays it points directly to the structure. */ | |
78134374 | 79 | if (type->code () == TYPE_CODE_PTR) |
c906108c | 80 | { |
27710edb | 81 | type = type->target_type (); |
78134374 | 82 | if (type->code () == TYPE_CODE_ARRAY) |
c906108c | 83 | { |
27710edb | 84 | type = type->target_type (); |
78134374 SM |
85 | if (type->code () == TYPE_CODE_STRUCT /* if not using thunks */ |
86 | || type->code () == TYPE_CODE_PTR) /* if using thunks */ | |
c906108c SS |
87 | { |
88 | /* Virtual functions tables are full of pointers | |
dda83cd7 | 89 | to virtual functions. */ |
c906108c SS |
90 | return cp_is_vtbl_ptr_type (type); |
91 | } | |
92 | } | |
78134374 | 93 | else if (type->code () == TYPE_CODE_STRUCT) /* if not using thunks */ |
0e5e3ea6 PS |
94 | { |
95 | return cp_is_vtbl_ptr_type (type); | |
96 | } | |
78134374 | 97 | else if (type->code () == TYPE_CODE_PTR) /* if using thunks */ |
0e5e3ea6 | 98 | { |
aff410f1 MS |
99 | /* The type name of the thunk pointer is NULL when using |
100 | dwarf2. We could test for a pointer to a function, but | |
101 | there is no type info for the virtual table either, so it | |
102 | wont help. */ | |
0e5e3ea6 PS |
103 | return cp_is_vtbl_ptr_type (type); |
104 | } | |
c906108c SS |
105 | } |
106 | return 0; | |
107 | } | |
108 | ||
109 | /* Mutually recursive subroutines of cp_print_value and c_val_print to | |
aff410f1 MS |
110 | print out a structure's fields: cp_print_value_fields and |
111 | cp_print_value. | |
c5aa993b | 112 | |
aff410f1 MS |
113 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same |
114 | meanings as in cp_print_value and c_val_print. | |
c906108c | 115 | |
aff410f1 MS |
116 | 2nd argument REAL_TYPE is used to carry over the type of the |
117 | derived class across the recursion to base classes. | |
c906108c | 118 | |
aff410f1 MS |
119 | DONT_PRINT is an array of baseclass types that we should not print, |
120 | or zero if called from top level. */ | |
c906108c | 121 | |
64b653ca TT |
122 | void |
123 | cp_print_value_fields (struct value *val, struct ui_file *stream, | |
124 | int recurse, const struct value_print_options *options, | |
125 | struct type **dont_print_vb, | |
126 | int dont_print_statmem) | |
127 | { | |
128 | int i, len, n_baseclasses; | |
129 | int fields_seen = 0; | |
130 | static int last_set_recurse = -1; | |
131 | ||
132 | struct type *type = check_typedef (value_type (val)); | |
64b653ca TT |
133 | |
134 | if (recurse == 0) | |
135 | { | |
136 | /* Any object can be left on obstacks only during an unexpected | |
137 | error. */ | |
138 | ||
139 | if (obstack_object_size (&dont_print_statmem_obstack) > 0) | |
140 | { | |
141 | obstack_free (&dont_print_statmem_obstack, NULL); | |
142 | obstack_begin (&dont_print_statmem_obstack, | |
143 | 32 * sizeof (CORE_ADDR)); | |
144 | } | |
145 | if (obstack_object_size (&dont_print_stat_array_obstack) > 0) | |
146 | { | |
147 | obstack_free (&dont_print_stat_array_obstack, NULL); | |
148 | obstack_begin (&dont_print_stat_array_obstack, | |
149 | 32 * sizeof (struct type *)); | |
150 | } | |
151 | } | |
152 | ||
6cb06a8c | 153 | gdb_printf (stream, "{"); |
1f704f76 | 154 | len = type->num_fields (); |
64b653ca TT |
155 | n_baseclasses = TYPE_N_BASECLASSES (type); |
156 | ||
157 | /* First, print out baseclasses such that we don't print | |
158 | duplicates of virtual baseclasses. */ | |
159 | ||
160 | if (n_baseclasses > 0) | |
fbf54e75 | 161 | cp_print_value (val, stream, recurse + 1, options, dont_print_vb); |
64b653ca TT |
162 | |
163 | /* Second, print out data fields */ | |
164 | ||
165 | /* If there are no data fields, skip this part */ | |
166 | if (len == n_baseclasses || !len) | |
167 | fprintf_styled (stream, metadata_style.style (), "<No data fields>"); | |
168 | else | |
169 | { | |
170 | size_t statmem_obstack_initial_size = 0; | |
171 | size_t stat_array_obstack_initial_size = 0; | |
172 | struct type *vptr_basetype = NULL; | |
173 | int vptr_fieldno; | |
174 | ||
175 | if (dont_print_statmem == 0) | |
176 | { | |
177 | statmem_obstack_initial_size = | |
178 | obstack_object_size (&dont_print_statmem_obstack); | |
179 | ||
180 | if (last_set_recurse != recurse) | |
181 | { | |
182 | stat_array_obstack_initial_size = | |
183 | obstack_object_size (&dont_print_stat_array_obstack); | |
184 | ||
185 | last_set_recurse = recurse; | |
186 | } | |
187 | } | |
188 | ||
189 | vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype); | |
190 | for (i = n_baseclasses; i < len; i++) | |
191 | { | |
50888e42 | 192 | const gdb_byte *valaddr = value_contents_for_printing (val).data (); |
64b653ca TT |
193 | |
194 | /* If requested, skip printing of static fields. */ | |
195 | if (!options->static_field_print | |
ceacbf6e | 196 | && field_is_static (&type->field (i))) |
64b653ca TT |
197 | continue; |
198 | ||
199 | if (fields_seen) | |
200 | { | |
0426ad51 | 201 | gdb_puts (",", stream); |
64b653ca | 202 | if (!options->prettyformat) |
0426ad51 | 203 | gdb_puts (" ", stream); |
64b653ca TT |
204 | } |
205 | else if (n_baseclasses > 0) | |
206 | { | |
207 | if (options->prettyformat) | |
208 | { | |
6cb06a8c | 209 | gdb_printf (stream, "\n"); |
d0b1020b | 210 | print_spaces (2 + 2 * recurse, stream); |
0426ad51 TT |
211 | gdb_puts ("members of ", stream); |
212 | gdb_puts (type->name (), stream); | |
213 | gdb_puts (":", stream); | |
64b653ca TT |
214 | } |
215 | } | |
216 | fields_seen = 1; | |
217 | ||
218 | if (options->prettyformat) | |
219 | { | |
6cb06a8c | 220 | gdb_printf (stream, "\n"); |
d0b1020b | 221 | print_spaces (2 + 2 * recurse, stream); |
64b653ca TT |
222 | } |
223 | else | |
224 | { | |
1285ce86 | 225 | stream->wrap_here (2 + 2 * recurse); |
64b653ca TT |
226 | } |
227 | ||
940da03e | 228 | annotate_field_begin (type->field (i).type ()); |
64b653ca | 229 | |
ceacbf6e | 230 | if (field_is_static (&type->field (i))) |
64b653ca | 231 | { |
0426ad51 | 232 | gdb_puts ("static ", stream); |
bed009b9 TT |
233 | fprintf_symbol (stream, |
234 | type->field (i).name (), | |
235 | current_language->la_language, | |
236 | DMGL_PARAMS | DMGL_ANSI); | |
64b653ca TT |
237 | } |
238 | else | |
33d16dd9 | 239 | fputs_styled (type->field (i).name (), |
64b653ca TT |
240 | variable_name_style.style (), stream); |
241 | annotate_field_name_end (); | |
242 | ||
243 | /* We tweak various options in a few cases below. */ | |
244 | value_print_options options_copy = *options; | |
245 | value_print_options *opts = &options_copy; | |
246 | ||
247 | /* Do not print leading '=' in case of anonymous | |
248 | unions. */ | |
33d16dd9 | 249 | if (strcmp (type->field (i).name (), "")) |
0426ad51 | 250 | gdb_puts (" = ", stream); |
64b653ca TT |
251 | else |
252 | { | |
253 | /* If this is an anonymous field then we want to consider it | |
254 | as though it is at its parent's depth when it comes to the | |
255 | max print depth. */ | |
256 | if (opts->max_depth != -1 && opts->max_depth < INT_MAX) | |
257 | ++opts->max_depth; | |
258 | } | |
259 | annotate_field_value (); | |
260 | ||
ceacbf6e | 261 | if (!field_is_static (&type->field (i)) |
64b653ca TT |
262 | && TYPE_FIELD_PACKED (type, i)) |
263 | { | |
264 | struct value *v; | |
265 | ||
266 | /* Bitfields require special handling, especially due to | |
dda83cd7 | 267 | byte order problems. */ |
64b653ca TT |
268 | if (TYPE_FIELD_IGNORE (type, i)) |
269 | { | |
270 | fputs_styled ("<optimized out or zero length>", | |
271 | metadata_style.style (), stream); | |
272 | } | |
b610c045 SM |
273 | else if (value_bits_synthetic_pointer |
274 | (val, type->field (i).loc_bitpos (), | |
275 | TYPE_FIELD_BITSIZE (type, i))) | |
64b653ca TT |
276 | { |
277 | fputs_styled (_("<synthetic pointer>"), | |
278 | metadata_style.style (), stream); | |
279 | } | |
280 | else | |
281 | { | |
dad6b350 | 282 | opts->deref_ref = false; |
64b653ca TT |
283 | |
284 | v = value_field_bitfield (type, i, valaddr, | |
285 | value_embedded_offset (val), val); | |
286 | ||
287 | common_val_print (v, stream, recurse + 1, | |
288 | opts, current_language); | |
289 | } | |
290 | } | |
291 | else | |
292 | { | |
293 | if (TYPE_FIELD_IGNORE (type, i)) | |
294 | { | |
295 | fputs_styled ("<optimized out or zero length>", | |
296 | metadata_style.style (), stream); | |
297 | } | |
ceacbf6e | 298 | else if (field_is_static (&type->field (i))) |
64b653ca TT |
299 | { |
300 | try | |
301 | { | |
302 | struct value *v = value_static_field (type, i); | |
303 | ||
940da03e | 304 | cp_print_static_field (type->field (i).type (), |
64b653ca TT |
305 | v, stream, recurse + 1, |
306 | opts); | |
307 | } | |
308 | catch (const gdb_exception_error &ex) | |
309 | { | |
310 | fprintf_styled (stream, metadata_style.style (), | |
311 | _("<error reading variable: %s>"), | |
312 | ex.what ()); | |
313 | } | |
314 | } | |
315 | else if (i == vptr_fieldno && type == vptr_basetype) | |
316 | { | |
b610c045 | 317 | int i_offset = type->field (i).loc_bitpos () / 8; |
940da03e | 318 | struct type *i_type = type->field (i).type (); |
64b653ca TT |
319 | |
320 | if (valprint_check_validity (stream, i_type, i_offset, val)) | |
321 | { | |
322 | CORE_ADDR addr; | |
323 | ||
fbf54e75 | 324 | i_offset += value_embedded_offset (val); |
64b653ca TT |
325 | addr = extract_typed_address (valaddr + i_offset, i_type); |
326 | print_function_pointer_address (opts, | |
8ee511af | 327 | type->arch (), |
64b653ca TT |
328 | addr, stream); |
329 | } | |
330 | } | |
331 | else | |
332 | { | |
333 | struct value *v = value_primitive_field (val, 0, i, type); | |
dad6b350 | 334 | opts->deref_ref = false; |
64b653ca TT |
335 | common_val_print (v, stream, recurse + 1, opts, |
336 | current_language); | |
337 | } | |
338 | } | |
339 | annotate_field_end (); | |
340 | } | |
341 | ||
342 | if (dont_print_statmem == 0) | |
343 | { | |
344 | size_t obstack_final_size = | |
dda83cd7 | 345 | obstack_object_size (&dont_print_statmem_obstack); |
64b653ca TT |
346 | |
347 | if (obstack_final_size > statmem_obstack_initial_size) | |
348 | { | |
349 | /* In effect, a pop of the printed-statics stack. */ | |
350 | size_t shrink_bytes | |
351 | = statmem_obstack_initial_size - obstack_final_size; | |
352 | obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes); | |
353 | } | |
354 | ||
355 | if (last_set_recurse != recurse) | |
356 | { | |
357 | obstack_final_size = | |
358 | obstack_object_size (&dont_print_stat_array_obstack); | |
359 | ||
360 | if (obstack_final_size > stat_array_obstack_initial_size) | |
361 | { | |
362 | void *free_to_ptr = | |
363 | (char *) obstack_next_free (&dont_print_stat_array_obstack) | |
364 | - (obstack_final_size | |
365 | - stat_array_obstack_initial_size); | |
366 | ||
367 | obstack_free (&dont_print_stat_array_obstack, | |
368 | free_to_ptr); | |
369 | } | |
370 | last_set_recurse = -1; | |
371 | } | |
372 | } | |
373 | ||
374 | if (options->prettyformat) | |
375 | { | |
6cb06a8c | 376 | gdb_printf (stream, "\n"); |
d0b1020b | 377 | print_spaces (2 * recurse, stream); |
64b653ca TT |
378 | } |
379 | } /* if there are data fields */ | |
380 | ||
6cb06a8c | 381 | gdb_printf (stream, "}"); |
64b653ca TT |
382 | } |
383 | ||
fbf54e75 TT |
384 | /* Special val_print routine to avoid printing multiple copies of |
385 | virtual baseclasses. */ | |
386 | ||
387 | static void | |
388 | cp_print_value (struct value *val, struct ui_file *stream, | |
389 | int recurse, const struct value_print_options *options, | |
390 | struct type **dont_print_vb) | |
391 | { | |
392 | struct type *type = check_typedef (value_type (val)); | |
393 | CORE_ADDR address = value_address (val); | |
394 | struct type **last_dont_print | |
395 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); | |
396 | struct obstack tmp_obstack = dont_print_vb_obstack; | |
397 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
50888e42 | 398 | const gdb_byte *valaddr = value_contents_for_printing (val).data (); |
fbf54e75 TT |
399 | |
400 | if (dont_print_vb == 0) | |
401 | { | |
402 | /* If we're at top level, carve out a completely fresh chunk of | |
dda83cd7 SM |
403 | the obstack and use that until this particular invocation |
404 | returns. */ | |
fbf54e75 TT |
405 | /* Bump up the high-water mark. Now alpha is omega. */ |
406 | obstack_finish (&dont_print_vb_obstack); | |
407 | } | |
408 | ||
409 | for (i = 0; i < n_baseclasses; i++) | |
410 | { | |
411 | LONGEST boffset = 0; | |
412 | int skip = 0; | |
413 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
7d93a1e0 | 414 | const char *basename = baseclass->name (); |
fbf54e75 TT |
415 | struct value *base_val = NULL; |
416 | ||
417 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
418 | { | |
419 | struct type **first_dont_print | |
420 | = (struct type **) obstack_base (&dont_print_vb_obstack); | |
421 | ||
422 | int j = (struct type **) | |
423 | obstack_next_free (&dont_print_vb_obstack) - first_dont_print; | |
424 | ||
425 | while (--j >= 0) | |
426 | if (baseclass == first_dont_print[j]) | |
427 | goto flush_it; | |
428 | ||
429 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
430 | } | |
431 | ||
432 | try | |
433 | { | |
434 | boffset = baseclass_offset (type, i, valaddr, | |
435 | value_embedded_offset (val), | |
436 | address, val); | |
437 | } | |
438 | catch (const gdb_exception_error &ex) | |
439 | { | |
440 | if (ex.error == NOT_AVAILABLE_ERROR) | |
441 | skip = -1; | |
442 | else | |
443 | skip = 1; | |
444 | } | |
445 | ||
446 | if (skip == 0) | |
447 | { | |
448 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
449 | { | |
450 | /* The virtual base class pointer might have been | |
451 | clobbered by the user program. Make sure that it | |
452 | still points to a valid memory location. */ | |
453 | ||
df86565b | 454 | if (boffset < 0 || boffset >= type->length ()) |
fbf54e75 | 455 | { |
df86565b | 456 | gdb::byte_vector buf (baseclass->length ()); |
fbf54e75 TT |
457 | |
458 | if (target_read_memory (address + boffset, buf.data (), | |
df86565b | 459 | baseclass->length ()) != 0) |
fbf54e75 TT |
460 | skip = 1; |
461 | base_val = value_from_contents_and_address (baseclass, | |
462 | buf.data (), | |
463 | address + boffset); | |
464 | baseclass = value_type (base_val); | |
465 | boffset = 0; | |
466 | } | |
467 | else | |
468 | { | |
469 | base_val = val; | |
470 | } | |
471 | } | |
472 | else | |
473 | { | |
474 | base_val = val; | |
475 | } | |
476 | } | |
477 | ||
478 | /* Now do the printing. */ | |
479 | if (options->prettyformat) | |
480 | { | |
6cb06a8c | 481 | gdb_printf (stream, "\n"); |
d0b1020b | 482 | print_spaces (2 * recurse, stream); |
fbf54e75 | 483 | } |
0426ad51 | 484 | gdb_puts ("<", stream); |
fbf54e75 | 485 | /* Not sure what the best notation is in the case where there is |
dda83cd7 | 486 | no baseclass name. */ |
0426ad51 TT |
487 | gdb_puts (basename ? basename : "", stream); |
488 | gdb_puts ("> = ", stream); | |
fbf54e75 TT |
489 | |
490 | if (skip < 0) | |
491 | val_print_unavailable (stream); | |
492 | else if (skip > 0) | |
493 | val_print_invalid_address (stream); | |
494 | else | |
495 | { | |
496 | int result = 0; | |
497 | ||
ecf25064 KC |
498 | if (!val_print_check_max_depth (stream, recurse, options, |
499 | current_language)) | |
fbf54e75 | 500 | { |
42331a1e TT |
501 | struct value *baseclass_val = value_primitive_field (val, 0, |
502 | i, type); | |
503 | ||
fbf54e75 TT |
504 | /* Attempt to run an extension language pretty-printer on the |
505 | baseclass if possible. */ | |
506 | if (!options->raw) | |
507 | result | |
42331a1e TT |
508 | = apply_ext_lang_val_pretty_printer (baseclass_val, stream, |
509 | recurse, options, | |
fbf54e75 TT |
510 | current_language); |
511 | ||
512 | if (!result) | |
42331a1e | 513 | cp_print_value_fields (baseclass_val, stream, recurse, options, |
fbf54e75 TT |
514 | ((struct type **) |
515 | obstack_base (&dont_print_vb_obstack)), | |
516 | 0); | |
517 | } | |
518 | } | |
0426ad51 | 519 | gdb_puts (", ", stream); |
fbf54e75 TT |
520 | |
521 | flush_it: | |
522 | ; | |
523 | } | |
524 | ||
525 | if (dont_print_vb == 0) | |
526 | { | |
527 | /* Free the space used to deal with the printing | |
dda83cd7 | 528 | of this type from top level. */ |
fbf54e75 TT |
529 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
530 | /* Reset watermark so that we can continue protecting | |
dda83cd7 | 531 | ourselves from whatever we were protecting ourselves. */ |
fbf54e75 TT |
532 | dont_print_vb_obstack = tmp_obstack; |
533 | } | |
534 | } | |
535 | ||
aff410f1 MS |
536 | /* Print value of a static member. To avoid infinite recursion when |
537 | printing a class that contains a static instance of the class, we | |
538 | keep the addresses of all printed static member classes in an | |
539 | obstack and refuse to print them more than once. | |
c906108c | 540 | |
79a45b7d | 541 | VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS |
c906108c SS |
542 | have the same meanings as in c_val_print. */ |
543 | ||
544 | static void | |
2c63a960 | 545 | cp_print_static_field (struct type *type, |
6943961c | 546 | struct value *val, |
2c63a960 | 547 | struct ui_file *stream, |
2c63a960 | 548 | int recurse, |
79a45b7d | 549 | const struct value_print_options *options) |
c906108c | 550 | { |
79a45b7d | 551 | struct value_print_options opts; |
686d4def PA |
552 | |
553 | if (value_entirely_optimized_out (val)) | |
554 | { | |
555 | val_print_optimized_out (val, stream); | |
556 | return; | |
557 | } | |
558 | ||
79f18731 | 559 | struct type *real_type = check_typedef (type); |
78134374 | 560 | if (real_type->code () == TYPE_CODE_STRUCT) |
c906108c SS |
561 | { |
562 | CORE_ADDR *first_dont_print; | |
426a9c18 | 563 | CORE_ADDR addr = value_address (val); |
c906108c SS |
564 | int i; |
565 | ||
566 | first_dont_print | |
c5aa993b | 567 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
99903ae3 CM |
568 | i = obstack_object_size (&dont_print_statmem_obstack) |
569 | / sizeof (CORE_ADDR); | |
c906108c SS |
570 | |
571 | while (--i >= 0) | |
572 | { | |
426a9c18 | 573 | if (addr == first_dont_print[i]) |
c906108c | 574 | { |
2dbc041e TT |
575 | fputs_styled (_("<same as static member of an already" |
576 | " seen type>"), | |
577 | metadata_style.style (), stream); | |
c906108c SS |
578 | return; |
579 | } | |
580 | } | |
581 | ||
42ae5230 | 582 | obstack_grow (&dont_print_statmem_obstack, (char *) &addr, |
c906108c | 583 | sizeof (CORE_ADDR)); |
426a9c18 | 584 | cp_print_value_fields (val, stream, recurse, options, NULL, 1); |
c906108c SS |
585 | return; |
586 | } | |
79a45b7d | 587 | |
78134374 | 588 | if (real_type->code () == TYPE_CODE_ARRAY) |
ec31cde5 CM |
589 | { |
590 | struct type **first_dont_print; | |
591 | int i; | |
27710edb | 592 | struct type *target_type = type->target_type (); |
ec31cde5 CM |
593 | |
594 | first_dont_print | |
595 | = (struct type **) obstack_base (&dont_print_stat_array_obstack); | |
596 | i = obstack_object_size (&dont_print_stat_array_obstack) | |
1e9beacb | 597 | / sizeof (struct type *); |
ec31cde5 CM |
598 | |
599 | while (--i >= 0) | |
600 | { | |
601 | if (target_type == first_dont_print[i]) | |
602 | { | |
2dbc041e TT |
603 | fputs_styled (_("<same as static member of an already" |
604 | " seen type>"), | |
605 | metadata_style.style (), stream); | |
ec31cde5 CM |
606 | return; |
607 | } | |
608 | } | |
609 | ||
aff410f1 MS |
610 | obstack_grow (&dont_print_stat_array_obstack, |
611 | (char *) &target_type, | |
ec31cde5 CM |
612 | sizeof (struct type *)); |
613 | } | |
614 | ||
79a45b7d | 615 | opts = *options; |
dad6b350 | 616 | opts.deref_ref = false; |
410cf315 | 617 | common_val_print (val, stream, recurse, &opts, current_language); |
c906108c SS |
618 | } |
619 | ||
09e2d7c7 DE |
620 | /* Find the field in *SELF, or its non-virtual base classes, with |
621 | bit offset OFFSET. Set *SELF to the containing type and *FIELDNO | |
aff410f1 | 622 | to the containing field number. If OFFSET is not exactly at the |
09e2d7c7 | 623 | start of some field, set *SELF to NULL. */ |
0d5de010 | 624 | |
2c0b251b | 625 | static void |
09e2d7c7 | 626 | cp_find_class_member (struct type **self_p, int *fieldno, |
0d5de010 DJ |
627 | LONGEST offset) |
628 | { | |
09e2d7c7 | 629 | struct type *self; |
0d5de010 DJ |
630 | unsigned int i; |
631 | unsigned len; | |
632 | ||
09e2d7c7 DE |
633 | *self_p = check_typedef (*self_p); |
634 | self = *self_p; | |
1f704f76 | 635 | len = self->num_fields (); |
0d5de010 | 636 | |
09e2d7c7 | 637 | for (i = TYPE_N_BASECLASSES (self); i < len; i++) |
0d5de010 | 638 | { |
22f8e2e7 TV |
639 | field &f = self->field (i); |
640 | if (field_is_static (&f)) | |
641 | continue; | |
642 | LONGEST bitpos = f.loc_bitpos (); | |
0d5de010 DJ |
643 | |
644 | QUIT; | |
645 | if (offset == bitpos) | |
646 | { | |
647 | *fieldno = i; | |
648 | return; | |
649 | } | |
650 | } | |
651 | ||
09e2d7c7 | 652 | for (i = 0; i < TYPE_N_BASECLASSES (self); i++) |
0d5de010 | 653 | { |
b610c045 | 654 | LONGEST bitpos = self->field (i).loc_bitpos (); |
df86565b | 655 | LONGEST bitsize = 8 * self->field (i).type ()->length (); |
0d5de010 DJ |
656 | |
657 | if (offset >= bitpos && offset < bitpos + bitsize) | |
658 | { | |
940da03e | 659 | *self_p = self->field (i).type (); |
09e2d7c7 | 660 | cp_find_class_member (self_p, fieldno, offset - bitpos); |
0d5de010 DJ |
661 | return; |
662 | } | |
663 | } | |
664 | ||
09e2d7c7 | 665 | *self_p = NULL; |
0d5de010 DJ |
666 | } |
667 | ||
c906108c | 668 | void |
ad4820ab | 669 | cp_print_class_member (const gdb_byte *valaddr, struct type *type, |
a121b7c1 | 670 | struct ui_file *stream, const char *prefix) |
c906108c | 671 | { |
34877895 | 672 | enum bfd_endian byte_order = type_byte_order (type); |
e17a4113 | 673 | |
09e2d7c7 | 674 | /* VAL is a byte offset into the structure type SELF_TYPE. |
c906108c SS |
675 | Find the name of the field for that offset and |
676 | print it. */ | |
09e2d7c7 | 677 | struct type *self_type = TYPE_SELF_TYPE (type); |
e17a4113 | 678 | LONGEST val; |
9f8afa72 | 679 | int fieldno; |
c906108c | 680 | |
aff410f1 | 681 | val = extract_signed_integer (valaddr, |
df86565b | 682 | type->length (), |
aff410f1 | 683 | byte_order); |
e17a4113 | 684 | |
0d5de010 DJ |
685 | /* Pointers to data members are usually byte offsets into an object. |
686 | Because a data member can have offset zero, and a NULL pointer to | |
687 | member must be distinct from any valid non-NULL pointer to | |
688 | member, either the value is biased or the NULL value has a | |
689 | special representation; both are permitted by ISO C++. HP aCC | |
690 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x | |
691 | and other compilers which use the Itanium ABI use -1 as the NULL | |
692 | value. GDB only supports that last form; to add support for | |
693 | another form, make this into a cp-abi hook. */ | |
c906108c | 694 | |
0d5de010 | 695 | if (val == -1) |
c906108c | 696 | { |
6cb06a8c | 697 | gdb_printf (stream, "NULL"); |
0d5de010 | 698 | return; |
c906108c | 699 | } |
0d5de010 | 700 | |
09e2d7c7 | 701 | cp_find_class_member (&self_type, &fieldno, val << 3); |
0d5de010 | 702 | |
09e2d7c7 | 703 | if (self_type != NULL) |
c906108c | 704 | { |
0d5cff50 | 705 | const char *name; |
c5504eaf | 706 | |
0426ad51 | 707 | gdb_puts (prefix, stream); |
7d93a1e0 | 708 | name = self_type->name (); |
c906108c | 709 | if (name) |
0426ad51 | 710 | gdb_puts (name, stream); |
c906108c | 711 | else |
09e2d7c7 | 712 | c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options); |
6cb06a8c | 713 | gdb_printf (stream, "::"); |
33d16dd9 | 714 | fputs_styled (self_type->field (fieldno).name (), |
3f0cbb04 | 715 | variable_name_style.style (), stream); |
c906108c SS |
716 | } |
717 | else | |
6cb06a8c | 718 | gdb_printf (stream, "%ld", (long) val); |
c906108c SS |
719 | } |
720 | ||
91254b91 SV |
721 | #if GDB_SELF_TEST |
722 | ||
723 | /* Test printing of TYPE_CODE_STRUCT values. */ | |
724 | ||
725 | static void | |
726 | test_print_fields (gdbarch *arch) | |
727 | { | |
728 | struct field *f; | |
729 | type *uint8_type = builtin_type (arch)->builtin_uint8; | |
730 | type *bool_type = builtin_type (arch)->builtin_bool; | |
731 | type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT); | |
b6cdbc9a | 732 | the_struct->set_length (4); |
91254b91 SV |
733 | |
734 | /* Value: 1110 1001 | |
735 | Fields: C-BB B-A- */ | |
736 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE) | |
737 | { | |
738 | f = append_composite_type_field_raw (the_struct, "A", bool_type); | |
cd3f655c | 739 | f->set_loc_bitpos (1); |
91254b91 SV |
740 | FIELD_BITSIZE (*f) = 1; |
741 | f = append_composite_type_field_raw (the_struct, "B", uint8_type); | |
cd3f655c | 742 | f->set_loc_bitpos (3); |
91254b91 SV |
743 | FIELD_BITSIZE (*f) = 3; |
744 | f = append_composite_type_field_raw (the_struct, "C", bool_type); | |
cd3f655c | 745 | f->set_loc_bitpos (7); |
91254b91 SV |
746 | FIELD_BITSIZE (*f) = 1; |
747 | } | |
748 | /* According to the logic commented in "make_gdb_type_struct ()" of | |
749 | * target-descriptions.c, bit positions are numbered differently for | |
750 | * little and big endians. */ | |
751 | else | |
752 | { | |
753 | f = append_composite_type_field_raw (the_struct, "A", bool_type); | |
cd3f655c | 754 | f->set_loc_bitpos (30); |
91254b91 SV |
755 | FIELD_BITSIZE (*f) = 1; |
756 | f = append_composite_type_field_raw (the_struct, "B", uint8_type); | |
cd3f655c | 757 | f->set_loc_bitpos (26); |
91254b91 SV |
758 | FIELD_BITSIZE (*f) = 3; |
759 | f = append_composite_type_field_raw (the_struct, "C", bool_type); | |
cd3f655c | 760 | f->set_loc_bitpos (24); |
91254b91 SV |
761 | FIELD_BITSIZE (*f) = 1; |
762 | } | |
763 | ||
764 | value *val = allocate_value (the_struct); | |
50888e42 | 765 | gdb_byte *contents = value_contents_writeable (val).data (); |
df86565b | 766 | store_unsigned_integer (contents, value_enclosing_type (val)->length (), |
91254b91 SV |
767 | gdbarch_byte_order (arch), 0xe9); |
768 | ||
769 | string_file out; | |
770 | struct value_print_options opts; | |
771 | get_no_prettyformat_print_options (&opts); | |
772 | cp_print_value_fields(val, &out, 0, &opts, NULL, 0); | |
773 | SELF_CHECK (out.string () == "{A = false, B = 5, C = true}"); | |
774 | ||
775 | out.clear(); | |
776 | opts.format = 'x'; | |
777 | cp_print_value_fields(val, &out, 0, &opts, NULL, 0); | |
778 | SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}"); | |
779 | } | |
780 | ||
781 | #endif | |
782 | ||
c906108c | 783 | |
6c265988 | 784 | void _initialize_cp_valprint (); |
c906108c | 785 | void |
6c265988 | 786 | _initialize_cp_valprint () |
c906108c | 787 | { |
91254b91 SV |
788 | #if GDB_SELF_TEST |
789 | selftests::register_test_foreach_arch ("print-fields", test_print_fields); | |
790 | #endif | |
791 | ||
aff410f1 MS |
792 | obstack_begin (&dont_print_stat_array_obstack, |
793 | 32 * sizeof (struct type *)); | |
794 | obstack_begin (&dont_print_statmem_obstack, | |
795 | 32 * sizeof (CORE_ADDR)); | |
796 | obstack_begin (&dont_print_vb_obstack, | |
797 | 32 * sizeof (struct type *)); | |
c906108c | 798 | } |