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