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