]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
a2bd3dcd | 2 | |
3666a048 | 3 | Copyright (C) 1986-2021 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" | |
4de283e4 TT |
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" | |
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 SS |
80 | { |
81 | type = TYPE_TARGET_TYPE (type); | |
78134374 | 82 | if (type->code () == TYPE_CODE_ARRAY) |
c906108c SS |
83 | { |
84 | type = TYPE_TARGET_TYPE (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 | ||
153 | fprintf_filtered (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 | { | |
201 | fputs_filtered (",", stream); | |
202 | if (!options->prettyformat) | |
203 | fputs_filtered (" ", stream); | |
204 | } | |
205 | else if (n_baseclasses > 0) | |
206 | { | |
207 | if (options->prettyformat) | |
208 | { | |
209 | fprintf_filtered (stream, "\n"); | |
210 | print_spaces_filtered (2 + 2 * recurse, stream); | |
211 | fputs_filtered ("members of ", stream); | |
7d93a1e0 | 212 | fputs_filtered (type->name (), stream); |
64b653ca TT |
213 | fputs_filtered (":", stream); |
214 | } | |
215 | } | |
216 | fields_seen = 1; | |
217 | ||
218 | if (options->prettyformat) | |
219 | { | |
220 | fprintf_filtered (stream, "\n"); | |
221 | print_spaces_filtered (2 + 2 * recurse, stream); | |
222 | } | |
223 | else | |
224 | { | |
225 | wrap_here (n_spaces (2 + 2 * recurse)); | |
226 | } | |
227 | ||
940da03e | 228 | annotate_field_begin (type->field (i).type ()); |
64b653ca | 229 | |
ceacbf6e | 230 | if (field_is_static (&type->field (i))) |
64b653ca TT |
231 | { |
232 | fputs_filtered ("static ", stream); | |
233 | fprintf_symbol_filtered (stream, | |
33d16dd9 | 234 | type->field (i).name (), |
64b653ca TT |
235 | current_language->la_language, |
236 | DMGL_PARAMS | DMGL_ANSI); | |
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 (), "")) |
64b653ca TT |
250 | fputs_filtered (" = ", stream); |
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 | } | |
273 | else if (value_bits_synthetic_pointer (val, | |
274 | TYPE_FIELD_BITPOS (type, | |
275 | i), | |
276 | TYPE_FIELD_BITSIZE (type, | |
277 | i))) | |
278 | { | |
279 | fputs_styled (_("<synthetic pointer>"), | |
280 | metadata_style.style (), stream); | |
281 | } | |
282 | else | |
283 | { | |
284 | opts->deref_ref = 0; | |
285 | ||
286 | v = value_field_bitfield (type, i, valaddr, | |
287 | value_embedded_offset (val), val); | |
288 | ||
289 | common_val_print (v, stream, recurse + 1, | |
290 | opts, current_language); | |
291 | } | |
292 | } | |
293 | else | |
294 | { | |
295 | if (TYPE_FIELD_IGNORE (type, i)) | |
296 | { | |
297 | fputs_styled ("<optimized out or zero length>", | |
298 | metadata_style.style (), stream); | |
299 | } | |
ceacbf6e | 300 | else if (field_is_static (&type->field (i))) |
64b653ca TT |
301 | { |
302 | try | |
303 | { | |
304 | struct value *v = value_static_field (type, i); | |
305 | ||
940da03e | 306 | cp_print_static_field (type->field (i).type (), |
64b653ca TT |
307 | v, stream, recurse + 1, |
308 | opts); | |
309 | } | |
310 | catch (const gdb_exception_error &ex) | |
311 | { | |
312 | fprintf_styled (stream, metadata_style.style (), | |
313 | _("<error reading variable: %s>"), | |
314 | ex.what ()); | |
315 | } | |
316 | } | |
317 | else if (i == vptr_fieldno && type == vptr_basetype) | |
318 | { | |
319 | int i_offset = TYPE_FIELD_BITPOS (type, i) / 8; | |
940da03e | 320 | struct type *i_type = type->field (i).type (); |
64b653ca TT |
321 | |
322 | if (valprint_check_validity (stream, i_type, i_offset, val)) | |
323 | { | |
324 | CORE_ADDR addr; | |
325 | ||
fbf54e75 | 326 | i_offset += value_embedded_offset (val); |
64b653ca TT |
327 | addr = extract_typed_address (valaddr + i_offset, i_type); |
328 | print_function_pointer_address (opts, | |
8ee511af | 329 | type->arch (), |
64b653ca TT |
330 | addr, stream); |
331 | } | |
332 | } | |
333 | else | |
334 | { | |
335 | struct value *v = value_primitive_field (val, 0, i, type); | |
336 | opts->deref_ref = 0; | |
337 | common_val_print (v, stream, recurse + 1, opts, | |
338 | current_language); | |
339 | } | |
340 | } | |
341 | annotate_field_end (); | |
342 | } | |
343 | ||
344 | if (dont_print_statmem == 0) | |
345 | { | |
346 | size_t obstack_final_size = | |
dda83cd7 | 347 | obstack_object_size (&dont_print_statmem_obstack); |
64b653ca TT |
348 | |
349 | if (obstack_final_size > statmem_obstack_initial_size) | |
350 | { | |
351 | /* In effect, a pop of the printed-statics stack. */ | |
352 | size_t shrink_bytes | |
353 | = statmem_obstack_initial_size - obstack_final_size; | |
354 | obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes); | |
355 | } | |
356 | ||
357 | if (last_set_recurse != recurse) | |
358 | { | |
359 | obstack_final_size = | |
360 | obstack_object_size (&dont_print_stat_array_obstack); | |
361 | ||
362 | if (obstack_final_size > stat_array_obstack_initial_size) | |
363 | { | |
364 | void *free_to_ptr = | |
365 | (char *) obstack_next_free (&dont_print_stat_array_obstack) | |
366 | - (obstack_final_size | |
367 | - stat_array_obstack_initial_size); | |
368 | ||
369 | obstack_free (&dont_print_stat_array_obstack, | |
370 | free_to_ptr); | |
371 | } | |
372 | last_set_recurse = -1; | |
373 | } | |
374 | } | |
375 | ||
376 | if (options->prettyformat) | |
377 | { | |
378 | fprintf_filtered (stream, "\n"); | |
379 | print_spaces_filtered (2 * recurse, stream); | |
380 | } | |
381 | } /* if there are data fields */ | |
382 | ||
383 | fprintf_filtered (stream, "}"); | |
384 | } | |
385 | ||
fbf54e75 TT |
386 | /* Special val_print routine to avoid printing multiple copies of |
387 | virtual baseclasses. */ | |
388 | ||
389 | static void | |
390 | cp_print_value (struct value *val, struct ui_file *stream, | |
391 | int recurse, const struct value_print_options *options, | |
392 | struct type **dont_print_vb) | |
393 | { | |
394 | struct type *type = check_typedef (value_type (val)); | |
395 | CORE_ADDR address = value_address (val); | |
396 | struct type **last_dont_print | |
397 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); | |
398 | struct obstack tmp_obstack = dont_print_vb_obstack; | |
399 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
50888e42 | 400 | const gdb_byte *valaddr = value_contents_for_printing (val).data (); |
fbf54e75 TT |
401 | |
402 | if (dont_print_vb == 0) | |
403 | { | |
404 | /* If we're at top level, carve out a completely fresh chunk of | |
dda83cd7 SM |
405 | the obstack and use that until this particular invocation |
406 | returns. */ | |
fbf54e75 TT |
407 | /* Bump up the high-water mark. Now alpha is omega. */ |
408 | obstack_finish (&dont_print_vb_obstack); | |
409 | } | |
410 | ||
411 | for (i = 0; i < n_baseclasses; i++) | |
412 | { | |
413 | LONGEST boffset = 0; | |
414 | int skip = 0; | |
415 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
7d93a1e0 | 416 | const char *basename = baseclass->name (); |
fbf54e75 TT |
417 | struct value *base_val = NULL; |
418 | ||
419 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
420 | { | |
421 | struct type **first_dont_print | |
422 | = (struct type **) obstack_base (&dont_print_vb_obstack); | |
423 | ||
424 | int j = (struct type **) | |
425 | obstack_next_free (&dont_print_vb_obstack) - first_dont_print; | |
426 | ||
427 | while (--j >= 0) | |
428 | if (baseclass == first_dont_print[j]) | |
429 | goto flush_it; | |
430 | ||
431 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
432 | } | |
433 | ||
434 | try | |
435 | { | |
436 | boffset = baseclass_offset (type, i, valaddr, | |
437 | value_embedded_offset (val), | |
438 | address, val); | |
439 | } | |
440 | catch (const gdb_exception_error &ex) | |
441 | { | |
442 | if (ex.error == NOT_AVAILABLE_ERROR) | |
443 | skip = -1; | |
444 | else | |
445 | skip = 1; | |
446 | } | |
447 | ||
448 | if (skip == 0) | |
449 | { | |
450 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
451 | { | |
452 | /* The virtual base class pointer might have been | |
453 | clobbered by the user program. Make sure that it | |
454 | still points to a valid memory location. */ | |
455 | ||
456 | if (boffset < 0 || boffset >= TYPE_LENGTH (type)) | |
457 | { | |
458 | gdb::byte_vector buf (TYPE_LENGTH (baseclass)); | |
459 | ||
460 | if (target_read_memory (address + boffset, buf.data (), | |
461 | TYPE_LENGTH (baseclass)) != 0) | |
462 | skip = 1; | |
463 | base_val = value_from_contents_and_address (baseclass, | |
464 | buf.data (), | |
465 | address + boffset); | |
466 | baseclass = value_type (base_val); | |
467 | boffset = 0; | |
468 | } | |
469 | else | |
470 | { | |
471 | base_val = val; | |
472 | } | |
473 | } | |
474 | else | |
475 | { | |
476 | base_val = val; | |
477 | } | |
478 | } | |
479 | ||
480 | /* Now do the printing. */ | |
481 | if (options->prettyformat) | |
482 | { | |
483 | fprintf_filtered (stream, "\n"); | |
484 | print_spaces_filtered (2 * recurse, stream); | |
485 | } | |
486 | fputs_filtered ("<", stream); | |
487 | /* Not sure what the best notation is in the case where there is | |
dda83cd7 | 488 | no baseclass name. */ |
fbf54e75 TT |
489 | fputs_filtered (basename ? basename : "", stream); |
490 | fputs_filtered ("> = ", stream); | |
491 | ||
492 | if (skip < 0) | |
493 | val_print_unavailable (stream); | |
494 | else if (skip > 0) | |
495 | val_print_invalid_address (stream); | |
496 | else | |
497 | { | |
498 | int result = 0; | |
499 | ||
ecf25064 KC |
500 | if (!val_print_check_max_depth (stream, recurse, options, |
501 | current_language)) | |
fbf54e75 | 502 | { |
42331a1e TT |
503 | struct value *baseclass_val = value_primitive_field (val, 0, |
504 | i, type); | |
505 | ||
fbf54e75 TT |
506 | /* Attempt to run an extension language pretty-printer on the |
507 | baseclass if possible. */ | |
508 | if (!options->raw) | |
509 | result | |
42331a1e TT |
510 | = apply_ext_lang_val_pretty_printer (baseclass_val, stream, |
511 | recurse, options, | |
fbf54e75 TT |
512 | current_language); |
513 | ||
514 | if (!result) | |
42331a1e | 515 | cp_print_value_fields (baseclass_val, stream, recurse, options, |
fbf54e75 TT |
516 | ((struct type **) |
517 | obstack_base (&dont_print_vb_obstack)), | |
518 | 0); | |
519 | } | |
520 | } | |
521 | fputs_filtered (", ", stream); | |
522 | ||
523 | flush_it: | |
524 | ; | |
525 | } | |
526 | ||
527 | if (dont_print_vb == 0) | |
528 | { | |
529 | /* Free the space used to deal with the printing | |
dda83cd7 | 530 | of this type from top level. */ |
fbf54e75 TT |
531 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
532 | /* Reset watermark so that we can continue protecting | |
dda83cd7 | 533 | ourselves from whatever we were protecting ourselves. */ |
fbf54e75 TT |
534 | dont_print_vb_obstack = tmp_obstack; |
535 | } | |
536 | } | |
537 | ||
aff410f1 MS |
538 | /* Print value of a static member. To avoid infinite recursion when |
539 | printing a class that contains a static instance of the class, we | |
540 | keep the addresses of all printed static member classes in an | |
541 | obstack and refuse to print them more than once. | |
c906108c | 542 | |
79a45b7d | 543 | VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS |
c906108c SS |
544 | have the same meanings as in c_val_print. */ |
545 | ||
546 | static void | |
2c63a960 | 547 | cp_print_static_field (struct type *type, |
6943961c | 548 | struct value *val, |
2c63a960 | 549 | struct ui_file *stream, |
2c63a960 | 550 | int recurse, |
79a45b7d | 551 | const struct value_print_options *options) |
c906108c | 552 | { |
79a45b7d | 553 | struct value_print_options opts; |
686d4def PA |
554 | |
555 | if (value_entirely_optimized_out (val)) | |
556 | { | |
557 | val_print_optimized_out (val, stream); | |
558 | return; | |
559 | } | |
560 | ||
79f18731 | 561 | struct type *real_type = check_typedef (type); |
78134374 | 562 | if (real_type->code () == TYPE_CODE_STRUCT) |
c906108c SS |
563 | { |
564 | CORE_ADDR *first_dont_print; | |
426a9c18 | 565 | CORE_ADDR addr = value_address (val); |
c906108c SS |
566 | int i; |
567 | ||
568 | first_dont_print | |
c5aa993b | 569 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
99903ae3 CM |
570 | i = obstack_object_size (&dont_print_statmem_obstack) |
571 | / sizeof (CORE_ADDR); | |
c906108c SS |
572 | |
573 | while (--i >= 0) | |
574 | { | |
426a9c18 | 575 | if (addr == first_dont_print[i]) |
c906108c | 576 | { |
2dbc041e TT |
577 | fputs_styled (_("<same as static member of an already" |
578 | " seen type>"), | |
579 | metadata_style.style (), stream); | |
c906108c SS |
580 | return; |
581 | } | |
582 | } | |
583 | ||
42ae5230 | 584 | obstack_grow (&dont_print_statmem_obstack, (char *) &addr, |
c906108c | 585 | sizeof (CORE_ADDR)); |
426a9c18 | 586 | cp_print_value_fields (val, stream, recurse, options, NULL, 1); |
c906108c SS |
587 | return; |
588 | } | |
79a45b7d | 589 | |
78134374 | 590 | if (real_type->code () == TYPE_CODE_ARRAY) |
ec31cde5 CM |
591 | { |
592 | struct type **first_dont_print; | |
593 | int i; | |
594 | struct type *target_type = TYPE_TARGET_TYPE (type); | |
595 | ||
596 | first_dont_print | |
597 | = (struct type **) obstack_base (&dont_print_stat_array_obstack); | |
598 | i = obstack_object_size (&dont_print_stat_array_obstack) | |
1e9beacb | 599 | / sizeof (struct type *); |
ec31cde5 CM |
600 | |
601 | while (--i >= 0) | |
602 | { | |
603 | if (target_type == first_dont_print[i]) | |
604 | { | |
2dbc041e TT |
605 | fputs_styled (_("<same as static member of an already" |
606 | " seen type>"), | |
607 | metadata_style.style (), stream); | |
ec31cde5 CM |
608 | return; |
609 | } | |
610 | } | |
611 | ||
aff410f1 MS |
612 | obstack_grow (&dont_print_stat_array_obstack, |
613 | (char *) &target_type, | |
ec31cde5 CM |
614 | sizeof (struct type *)); |
615 | } | |
616 | ||
79a45b7d TT |
617 | opts = *options; |
618 | opts.deref_ref = 0; | |
410cf315 | 619 | common_val_print (val, stream, recurse, &opts, current_language); |
c906108c SS |
620 | } |
621 | ||
09e2d7c7 DE |
622 | /* Find the field in *SELF, or its non-virtual base classes, with |
623 | bit offset OFFSET. Set *SELF to the containing type and *FIELDNO | |
aff410f1 | 624 | to the containing field number. If OFFSET is not exactly at the |
09e2d7c7 | 625 | start of some field, set *SELF to NULL. */ |
0d5de010 | 626 | |
2c0b251b | 627 | static void |
09e2d7c7 | 628 | cp_find_class_member (struct type **self_p, int *fieldno, |
0d5de010 DJ |
629 | LONGEST offset) |
630 | { | |
09e2d7c7 | 631 | struct type *self; |
0d5de010 DJ |
632 | unsigned int i; |
633 | unsigned len; | |
634 | ||
09e2d7c7 DE |
635 | *self_p = check_typedef (*self_p); |
636 | self = *self_p; | |
1f704f76 | 637 | len = self->num_fields (); |
0d5de010 | 638 | |
09e2d7c7 | 639 | for (i = TYPE_N_BASECLASSES (self); i < len; i++) |
0d5de010 | 640 | { |
09e2d7c7 | 641 | LONGEST bitpos = TYPE_FIELD_BITPOS (self, i); |
0d5de010 DJ |
642 | |
643 | QUIT; | |
644 | if (offset == bitpos) | |
645 | { | |
646 | *fieldno = i; | |
647 | return; | |
648 | } | |
649 | } | |
650 | ||
09e2d7c7 | 651 | for (i = 0; i < TYPE_N_BASECLASSES (self); i++) |
0d5de010 | 652 | { |
09e2d7c7 | 653 | LONGEST bitpos = TYPE_FIELD_BITPOS (self, i); |
940da03e | 654 | LONGEST bitsize = 8 * TYPE_LENGTH (self->field (i).type ()); |
0d5de010 DJ |
655 | |
656 | if (offset >= bitpos && offset < bitpos + bitsize) | |
657 | { | |
940da03e | 658 | *self_p = self->field (i).type (); |
09e2d7c7 | 659 | cp_find_class_member (self_p, fieldno, offset - bitpos); |
0d5de010 DJ |
660 | return; |
661 | } | |
662 | } | |
663 | ||
09e2d7c7 | 664 | *self_p = NULL; |
0d5de010 DJ |
665 | } |
666 | ||
c906108c | 667 | void |
ad4820ab | 668 | cp_print_class_member (const gdb_byte *valaddr, struct type *type, |
a121b7c1 | 669 | struct ui_file *stream, const char *prefix) |
c906108c | 670 | { |
34877895 | 671 | enum bfd_endian byte_order = type_byte_order (type); |
e17a4113 | 672 | |
09e2d7c7 | 673 | /* VAL is a byte offset into the structure type SELF_TYPE. |
c906108c SS |
674 | Find the name of the field for that offset and |
675 | print it. */ | |
09e2d7c7 | 676 | struct type *self_type = TYPE_SELF_TYPE (type); |
e17a4113 | 677 | LONGEST val; |
9f8afa72 | 678 | int fieldno; |
c906108c | 679 | |
aff410f1 MS |
680 | val = extract_signed_integer (valaddr, |
681 | TYPE_LENGTH (type), | |
682 | byte_order); | |
e17a4113 | 683 | |
0d5de010 DJ |
684 | /* Pointers to data members are usually byte offsets into an object. |
685 | Because a data member can have offset zero, and a NULL pointer to | |
686 | member must be distinct from any valid non-NULL pointer to | |
687 | member, either the value is biased or the NULL value has a | |
688 | special representation; both are permitted by ISO C++. HP aCC | |
689 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x | |
690 | and other compilers which use the Itanium ABI use -1 as the NULL | |
691 | value. GDB only supports that last form; to add support for | |
692 | another form, make this into a cp-abi hook. */ | |
c906108c | 693 | |
0d5de010 | 694 | if (val == -1) |
c906108c | 695 | { |
0d5de010 DJ |
696 | fprintf_filtered (stream, "NULL"); |
697 | return; | |
c906108c | 698 | } |
0d5de010 | 699 | |
09e2d7c7 | 700 | cp_find_class_member (&self_type, &fieldno, val << 3); |
0d5de010 | 701 | |
09e2d7c7 | 702 | if (self_type != NULL) |
c906108c | 703 | { |
0d5cff50 | 704 | const char *name; |
c5504eaf | 705 | |
306d9ac5 | 706 | fputs_filtered (prefix, stream); |
7d93a1e0 | 707 | name = self_type->name (); |
c906108c | 708 | if (name) |
c5aa993b | 709 | fputs_filtered (name, stream); |
c906108c | 710 | else |
09e2d7c7 | 711 | c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options); |
c906108c | 712 | fprintf_filtered (stream, "::"); |
33d16dd9 | 713 | fputs_styled (self_type->field (fieldno).name (), |
3f0cbb04 | 714 | variable_name_style.style (), stream); |
c906108c SS |
715 | } |
716 | else | |
0d5de010 | 717 | fprintf_filtered (stream, "%ld", (long) val); |
c906108c SS |
718 | } |
719 | ||
91254b91 SV |
720 | #if GDB_SELF_TEST |
721 | ||
722 | /* Test printing of TYPE_CODE_STRUCT values. */ | |
723 | ||
724 | static void | |
725 | test_print_fields (gdbarch *arch) | |
726 | { | |
727 | struct field *f; | |
728 | type *uint8_type = builtin_type (arch)->builtin_uint8; | |
729 | type *bool_type = builtin_type (arch)->builtin_bool; | |
730 | type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT); | |
731 | TYPE_LENGTH (the_struct) = 4; | |
732 | ||
733 | /* Value: 1110 1001 | |
734 | Fields: C-BB B-A- */ | |
735 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE) | |
736 | { | |
737 | f = append_composite_type_field_raw (the_struct, "A", bool_type); | |
cd3f655c | 738 | f->set_loc_bitpos (1); |
91254b91 SV |
739 | FIELD_BITSIZE (*f) = 1; |
740 | f = append_composite_type_field_raw (the_struct, "B", uint8_type); | |
cd3f655c | 741 | f->set_loc_bitpos (3); |
91254b91 SV |
742 | FIELD_BITSIZE (*f) = 3; |
743 | f = append_composite_type_field_raw (the_struct, "C", bool_type); | |
cd3f655c | 744 | f->set_loc_bitpos (7); |
91254b91 SV |
745 | FIELD_BITSIZE (*f) = 1; |
746 | } | |
747 | /* According to the logic commented in "make_gdb_type_struct ()" of | |
748 | * target-descriptions.c, bit positions are numbered differently for | |
749 | * little and big endians. */ | |
750 | else | |
751 | { | |
752 | f = append_composite_type_field_raw (the_struct, "A", bool_type); | |
cd3f655c | 753 | f->set_loc_bitpos (30); |
91254b91 SV |
754 | FIELD_BITSIZE (*f) = 1; |
755 | f = append_composite_type_field_raw (the_struct, "B", uint8_type); | |
cd3f655c | 756 | f->set_loc_bitpos (26); |
91254b91 SV |
757 | FIELD_BITSIZE (*f) = 3; |
758 | f = append_composite_type_field_raw (the_struct, "C", bool_type); | |
cd3f655c | 759 | f->set_loc_bitpos (24); |
91254b91 SV |
760 | FIELD_BITSIZE (*f) = 1; |
761 | } | |
762 | ||
763 | value *val = allocate_value (the_struct); | |
50888e42 | 764 | gdb_byte *contents = value_contents_writeable (val).data (); |
91254b91 SV |
765 | store_unsigned_integer (contents, TYPE_LENGTH (value_enclosing_type (val)), |
766 | gdbarch_byte_order (arch), 0xe9); | |
767 | ||
768 | string_file out; | |
769 | struct value_print_options opts; | |
770 | get_no_prettyformat_print_options (&opts); | |
771 | cp_print_value_fields(val, &out, 0, &opts, NULL, 0); | |
772 | SELF_CHECK (out.string () == "{A = false, B = 5, C = true}"); | |
773 | ||
774 | out.clear(); | |
775 | opts.format = 'x'; | |
776 | cp_print_value_fields(val, &out, 0, &opts, NULL, 0); | |
777 | SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}"); | |
778 | } | |
779 | ||
780 | #endif | |
781 | ||
c906108c | 782 | |
6c265988 | 783 | void _initialize_cp_valprint (); |
c906108c | 784 | void |
6c265988 | 785 | _initialize_cp_valprint () |
c906108c | 786 | { |
91254b91 SV |
787 | #if GDB_SELF_TEST |
788 | selftests::register_test_foreach_arch ("print-fields", test_print_fields); | |
789 | #endif | |
790 | ||
aff410f1 MS |
791 | obstack_begin (&dont_print_stat_array_obstack, |
792 | 32 * sizeof (struct type *)); | |
793 | obstack_begin (&dont_print_statmem_obstack, | |
794 | 32 * sizeof (CORE_ADDR)); | |
795 | obstack_begin (&dont_print_vb_obstack, | |
796 | 32 * sizeof (struct type *)); | |
c906108c | 797 | } |