Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
a2bd3dcd | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
4c38e0a4 | 4 | 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010 |
9b254dd1 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
04ea0df1 | 23 | #include "gdb_obstack.h" |
c906108c SS |
24 | #include "symtab.h" |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "value.h" | |
28 | #include "command.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "demangle.h" | |
31 | #include "annotate.h" | |
32 | #include "gdb_string.h" | |
33 | #include "c-lang.h" | |
34 | #include "target.h" | |
b9d652ac | 35 | #include "cp-abi.h" |
973177d3 | 36 | #include "valprint.h" |
d3cbe7ef | 37 | #include "cp-support.h" |
cf309262 | 38 | #include "language.h" |
a6bac58e | 39 | #include "python/python.h" |
c906108c | 40 | |
920d2a44 | 41 | /* Controls printing of vtbl's */ |
920d2a44 AC |
42 | static void |
43 | show_vtblprint (struct ui_file *file, int from_tty, | |
44 | struct cmd_list_element *c, const char *value) | |
45 | { | |
46 | fprintf_filtered (file, _("\ | |
47 | Printing of C++ virtual function tables is %s.\n"), | |
48 | value); | |
49 | } | |
50 | ||
51 | /* Controls looking up an object's derived type using what we find in | |
52 | its vtables. */ | |
920d2a44 AC |
53 | static void |
54 | show_objectprint (struct ui_file *file, int from_tty, | |
55 | struct cmd_list_element *c, | |
56 | const char *value) | |
57 | { | |
58 | fprintf_filtered (file, _("\ | |
59 | Printing of object's derived type based on vtable info is %s.\n"), | |
60 | value); | |
61 | } | |
62 | ||
920d2a44 AC |
63 | static void |
64 | show_static_field_print (struct ui_file *file, int from_tty, | |
65 | struct cmd_list_element *c, const char *value) | |
66 | { | |
67 | fprintf_filtered (file, _("Printing of C++ static members is %s.\n"), | |
68 | value); | |
69 | } | |
70 | ||
c906108c SS |
71 | |
72 | static struct obstack dont_print_vb_obstack; | |
73 | static struct obstack dont_print_statmem_obstack; | |
74 | ||
a14ed312 | 75 | extern void _initialize_cp_valprint (void); |
392a587b | 76 | |
6943961c | 77 | static void cp_print_static_field (struct type *, struct value *, |
79a45b7d TT |
78 | struct ui_file *, int, |
79 | const struct value_print_options *); | |
c906108c | 80 | |
fc1a4b47 | 81 | static void cp_print_value (struct type *, struct type *, const gdb_byte *, |
79a45b7d TT |
82 | int, CORE_ADDR, struct ui_file *, int, |
83 | const struct value_print_options *, struct type **); | |
c906108c | 84 | |
c906108c | 85 | |
8343f86c | 86 | /* GCC versions after 2.4.5 use this. */ |
2c63a960 | 87 | const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
c906108c | 88 | |
c906108c SS |
89 | /* Return truth value for assertion that TYPE is of the type |
90 | "pointer to virtual function". */ | |
91 | ||
92 | int | |
fba45db2 | 93 | cp_is_vtbl_ptr_type (struct type *type) |
c906108c SS |
94 | { |
95 | char *typename = type_name_no_tag (type); | |
96 | ||
8343f86c | 97 | return (typename != NULL && !strcmp (typename, vtbl_ptr_name)); |
c906108c SS |
98 | } |
99 | ||
100 | /* Return truth value for the assertion that TYPE is of the type | |
101 | "pointer to virtual function table". */ | |
102 | ||
103 | int | |
fba45db2 | 104 | cp_is_vtbl_member (struct type *type) |
c906108c | 105 | { |
0e5e3ea6 PS |
106 | /* With older versions of g++, the vtbl field pointed to an array |
107 | of structures. Nowadays it points directly to the structure. */ | |
c906108c SS |
108 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
109 | { | |
110 | type = TYPE_TARGET_TYPE (type); | |
111 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
112 | { | |
113 | type = TYPE_TARGET_TYPE (type); | |
c5aa993b JM |
114 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ |
115 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
c906108c SS |
116 | { |
117 | /* Virtual functions tables are full of pointers | |
c5aa993b | 118 | to virtual functions. */ |
c906108c SS |
119 | return cp_is_vtbl_ptr_type (type); |
120 | } | |
121 | } | |
0e5e3ea6 PS |
122 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ |
123 | { | |
124 | return cp_is_vtbl_ptr_type (type); | |
125 | } | |
126 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
127 | { | |
128 | /* The type name of the thunk pointer is NULL when using dwarf2. | |
129 | We could test for a pointer to a function, but there is | |
130 | no type info for the virtual table either, so it wont help. */ | |
131 | return cp_is_vtbl_ptr_type (type); | |
132 | } | |
c906108c SS |
133 | } |
134 | return 0; | |
135 | } | |
136 | ||
137 | /* Mutually recursive subroutines of cp_print_value and c_val_print to | |
138 | print out a structure's fields: cp_print_value_fields and cp_print_value. | |
c5aa993b | 139 | |
79a45b7d | 140 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the |
c906108c SS |
141 | same meanings as in cp_print_value and c_val_print. |
142 | ||
143 | 2nd argument REAL_TYPE is used to carry over the type of the derived | |
144 | class across the recursion to base classes. | |
145 | ||
146 | DONT_PRINT is an array of baseclass types that we | |
147 | should not print, or zero if called from top level. */ | |
148 | ||
149 | void | |
a2bd3dcd | 150 | cp_print_value_fields (struct type *type, struct type *real_type, |
fc1a4b47 | 151 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
79a45b7d TT |
152 | struct ui_file *stream, int recurse, |
153 | const struct value_print_options *options, | |
154 | struct type **dont_print_vb, int dont_print_statmem) | |
c906108c SS |
155 | { |
156 | int i, len, n_baseclasses; | |
c906108c SS |
157 | int fields_seen = 0; |
158 | ||
159 | CHECK_TYPEDEF (type); | |
99903ae3 CM |
160 | |
161 | if (recurse == 0 | |
162 | && obstack_object_size (&dont_print_statmem_obstack) > 0) | |
163 | obstack_free (&dont_print_statmem_obstack, NULL); | |
c906108c SS |
164 | |
165 | fprintf_filtered (stream, "{"); | |
166 | len = TYPE_NFIELDS (type); | |
167 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
168 | ||
169 | /* First, print out baseclasses such that we don't print | |
170 | duplicates of virtual baseclasses. */ | |
171 | ||
172 | if (n_baseclasses > 0) | |
173 | cp_print_value (type, real_type, valaddr, offset, address, stream, | |
79a45b7d | 174 | recurse + 1, options, dont_print_vb); |
c906108c SS |
175 | |
176 | /* Second, print out data fields */ | |
177 | ||
086280be UW |
178 | /* If there are no data fields, skip this part */ |
179 | if (len == n_baseclasses || !len) | |
c906108c SS |
180 | fprintf_filtered (stream, "<No data fields>"); |
181 | else | |
182 | { | |
99903ae3 CM |
183 | void *statmem_obstack_top = NULL; |
184 | ||
c906108c SS |
185 | if (dont_print_statmem == 0) |
186 | { | |
99903ae3 CM |
187 | /* Set the current printed-statics stack top. */ |
188 | statmem_obstack_top | |
189 | = obstack_next_free (&dont_print_statmem_obstack); | |
c906108c SS |
190 | } |
191 | ||
192 | for (i = n_baseclasses; i < len; i++) | |
193 | { | |
194 | /* If requested, skip printing of static fields. */ | |
79a45b7d | 195 | if (!options->static_field_print |
d6a843b5 | 196 | && field_is_static (&TYPE_FIELD (type, i))) |
c906108c SS |
197 | continue; |
198 | ||
c906108c SS |
199 | if (fields_seen) |
200 | fprintf_filtered (stream, ", "); | |
201 | else if (n_baseclasses > 0) | |
202 | { | |
79a45b7d | 203 | if (options->pretty) |
c906108c SS |
204 | { |
205 | fprintf_filtered (stream, "\n"); | |
206 | print_spaces_filtered (2 + 2 * recurse, stream); | |
207 | fputs_filtered ("members of ", stream); | |
208 | fputs_filtered (type_name_no_tag (type), stream); | |
209 | fputs_filtered (": ", stream); | |
210 | } | |
211 | } | |
212 | fields_seen = 1; | |
213 | ||
79a45b7d | 214 | if (options->pretty) |
c906108c SS |
215 | { |
216 | fprintf_filtered (stream, "\n"); | |
217 | print_spaces_filtered (2 + 2 * recurse, stream); | |
218 | } | |
c5aa993b | 219 | else |
c906108c SS |
220 | { |
221 | wrap_here (n_spaces (2 + 2 * recurse)); | |
222 | } | |
79a45b7d | 223 | if (options->inspect_it) |
c906108c SS |
224 | { |
225 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
226 | fputs_filtered ("\"( ptr \"", stream); | |
227 | else | |
228 | fputs_filtered ("\"( nodef \"", stream); | |
d6a843b5 | 229 | if (field_is_static (&TYPE_FIELD (type, i))) |
c906108c SS |
230 | fputs_filtered ("static ", stream); |
231 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 232 | current_language->la_language, |
c906108c SS |
233 | DMGL_PARAMS | DMGL_ANSI); |
234 | fputs_filtered ("\" \"", stream); | |
235 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 236 | current_language->la_language, |
c906108c SS |
237 | DMGL_PARAMS | DMGL_ANSI); |
238 | fputs_filtered ("\") \"", stream); | |
239 | } | |
240 | else | |
241 | { | |
242 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); | |
243 | ||
d6a843b5 | 244 | if (field_is_static (&TYPE_FIELD (type, i))) |
c906108c SS |
245 | fputs_filtered ("static ", stream); |
246 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 247 | current_language->la_language, |
c906108c SS |
248 | DMGL_PARAMS | DMGL_ANSI); |
249 | annotate_field_name_end (); | |
250 | /* do not print leading '=' in case of anonymous unions */ | |
251 | if (strcmp (TYPE_FIELD_NAME (type, i), "")) | |
252 | fputs_filtered (" = ", stream); | |
253 | annotate_field_value (); | |
254 | } | |
255 | ||
d6a843b5 JK |
256 | if (!field_is_static (&TYPE_FIELD (type, i)) |
257 | && TYPE_FIELD_PACKED (type, i)) | |
c906108c | 258 | { |
6943961c | 259 | struct value *v; |
c906108c SS |
260 | |
261 | /* Bitfields require special handling, especially due to byte | |
c5aa993b | 262 | order problems. */ |
c906108c SS |
263 | if (TYPE_FIELD_IGNORE (type, i)) |
264 | { | |
c5aa993b | 265 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
266 | } |
267 | else | |
268 | { | |
79a45b7d TT |
269 | struct value_print_options opts = *options; |
270 | opts.deref_ref = 0; | |
2c63a960 JB |
271 | v = value_from_longest |
272 | (TYPE_FIELD_TYPE (type, i), | |
273 | unpack_field_as_long (type, valaddr + offset, i)); | |
c906108c | 274 | |
79a45b7d | 275 | common_val_print (v, stream, recurse + 1, &opts, |
d8ca156b | 276 | current_language); |
c906108c SS |
277 | } |
278 | } | |
279 | else | |
280 | { | |
281 | if (TYPE_FIELD_IGNORE (type, i)) | |
282 | { | |
c5aa993b | 283 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c | 284 | } |
d6a843b5 | 285 | else if (field_is_static (&TYPE_FIELD (type, i))) |
c906108c | 286 | { |
6943961c | 287 | struct value *v = value_static_field (type, i); |
c906108c SS |
288 | if (v == NULL) |
289 | fputs_filtered ("<optimized out>", stream); | |
290 | else | |
291 | cp_print_static_field (TYPE_FIELD_TYPE (type, i), v, | |
79a45b7d | 292 | stream, recurse + 1, options); |
c906108c SS |
293 | } |
294 | else | |
295 | { | |
79a45b7d TT |
296 | struct value_print_options opts = *options; |
297 | opts.deref_ref = 0; | |
c5aa993b | 298 | val_print (TYPE_FIELD_TYPE (type, i), |
2c63a960 | 299 | valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8, |
edf3d5f3 | 300 | address, |
79a45b7d | 301 | stream, recurse + 1, &opts, |
d8ca156b | 302 | current_language); |
c906108c SS |
303 | } |
304 | } | |
305 | annotate_field_end (); | |
306 | } | |
307 | ||
308 | if (dont_print_statmem == 0) | |
309 | { | |
99903ae3 CM |
310 | /* In effect, a pop of the printed-statics stack. */ |
311 | if (obstack_object_size (&dont_print_statmem_obstack) > 0) | |
312 | obstack_free (&dont_print_statmem_obstack, statmem_obstack_top); | |
c906108c SS |
313 | } |
314 | ||
79a45b7d | 315 | if (options->pretty) |
c906108c SS |
316 | { |
317 | fprintf_filtered (stream, "\n"); | |
318 | print_spaces_filtered (2 * recurse, stream); | |
319 | } | |
c5aa993b | 320 | } /* if there are data fields */ |
c5aa993b | 321 | |
c906108c SS |
322 | fprintf_filtered (stream, "}"); |
323 | } | |
324 | ||
edf3d5f3 TT |
325 | /* Like cp_print_value_fields, but find the runtime type of the object |
326 | and pass it as the `real_type' argument to cp_print_value_fields. | |
327 | This function is a hack to work around the fact that | |
328 | common_val_print passes the embedded offset to val_print, but not | |
329 | the enclosing type. */ | |
330 | ||
331 | void | |
332 | cp_print_value_fields_rtti (struct type *type, | |
333 | const gdb_byte *valaddr, int offset, | |
334 | CORE_ADDR address, | |
335 | struct ui_file *stream, int recurse, | |
336 | const struct value_print_options *options, | |
337 | struct type **dont_print_vb, int dont_print_statmem) | |
338 | { | |
339 | struct value *value; | |
340 | int full, top, using_enc; | |
341 | struct type *real_type; | |
342 | ||
343 | /* Ugh, we have to convert back to a value here. */ | |
344 | value = value_from_contents_and_address (type, valaddr + offset, | |
345 | address + offset); | |
346 | /* We don't actually care about most of the result here -- just the | |
347 | type. We already have the correct offset, due to how val_print | |
348 | was initially called. */ | |
349 | real_type = value_rtti_type (value, &full, &top, &using_enc); | |
350 | if (!real_type) | |
351 | real_type = type; | |
352 | ||
353 | cp_print_value_fields (type, real_type, valaddr, offset, | |
354 | address, stream, recurse, options, | |
355 | dont_print_vb, dont_print_statmem); | |
356 | } | |
357 | ||
c906108c SS |
358 | /* Special val_print routine to avoid printing multiple copies of virtual |
359 | baseclasses. */ | |
360 | ||
361 | static void | |
a2bd3dcd | 362 | cp_print_value (struct type *type, struct type *real_type, |
fc1a4b47 | 363 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
79a45b7d TT |
364 | struct ui_file *stream, int recurse, |
365 | const struct value_print_options *options, | |
366 | struct type **dont_print_vb) | |
c906108c | 367 | { |
c906108c | 368 | struct type **last_dont_print |
2c63a960 | 369 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
c1b6e682 | 370 | struct obstack tmp_obstack = dont_print_vb_obstack; |
c906108c | 371 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
b9d652ac DJ |
372 | int thisoffset; |
373 | struct type *thistype; | |
c906108c SS |
374 | |
375 | if (dont_print_vb == 0) | |
376 | { | |
377 | /* If we're at top level, carve out a completely fresh | |
c5aa993b JM |
378 | chunk of the obstack and use that until this particular |
379 | invocation returns. */ | |
c906108c SS |
380 | /* Bump up the high-water mark. Now alpha is omega. */ |
381 | obstack_finish (&dont_print_vb_obstack); | |
382 | } | |
383 | ||
384 | for (i = 0; i < n_baseclasses; i++) | |
385 | { | |
386 | int boffset; | |
387 | int skip; | |
388 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
389 | char *basename = TYPE_NAME (baseclass); | |
fc1a4b47 | 390 | const gdb_byte *base_valaddr; |
c906108c SS |
391 | |
392 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
393 | { | |
394 | struct type **first_dont_print | |
2c63a960 | 395 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
c906108c | 396 | |
c5aa993b | 397 | int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) |
2c63a960 | 398 | - first_dont_print; |
c906108c SS |
399 | |
400 | while (--j >= 0) | |
401 | if (baseclass == first_dont_print[j]) | |
402 | goto flush_it; | |
403 | ||
404 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
405 | } | |
406 | ||
b9d652ac DJ |
407 | thisoffset = offset; |
408 | thistype = real_type; | |
086280be | 409 | |
edf3d5f3 | 410 | boffset = baseclass_offset (type, i, valaddr + offset, address + offset); |
086280be UW |
411 | skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1; |
412 | ||
413 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
c5aa993b | 414 | { |
086280be UW |
415 | /* The virtual base class pointer might have been |
416 | clobbered by the user program. Make sure that it | |
417 | still points to a valid memory location. */ | |
c906108c | 418 | |
086280be UW |
419 | if (boffset != -1 |
420 | && ((boffset + offset) < 0 | |
edf3d5f3 | 421 | || (boffset + offset) >= TYPE_LENGTH (real_type))) |
c5aa993b | 422 | { |
086280be UW |
423 | /* FIXME (alloca): unsafe if baseclass is really really large. */ |
424 | gdb_byte *buf = alloca (TYPE_LENGTH (baseclass)); | |
425 | base_valaddr = buf; | |
426 | if (target_read_memory (address + boffset, buf, | |
427 | TYPE_LENGTH (baseclass)) != 0) | |
428 | skip = 1; | |
429 | address = address + boffset; | |
430 | thisoffset = 0; | |
431 | boffset = 0; | |
432 | thistype = baseclass; | |
c5aa993b JM |
433 | } |
434 | else | |
435 | base_valaddr = valaddr; | |
c906108c | 436 | } |
086280be UW |
437 | else |
438 | base_valaddr = valaddr; | |
c906108c SS |
439 | |
440 | /* now do the printing */ | |
79a45b7d | 441 | if (options->pretty) |
c906108c SS |
442 | { |
443 | fprintf_filtered (stream, "\n"); | |
444 | print_spaces_filtered (2 * recurse, stream); | |
445 | } | |
446 | fputs_filtered ("<", stream); | |
447 | /* Not sure what the best notation is in the case where there is no | |
c5aa993b | 448 | baseclass name. */ |
c906108c SS |
449 | fputs_filtered (basename ? basename : "", stream); |
450 | fputs_filtered ("> = ", stream); | |
451 | ||
452 | ||
453 | if (skip >= 1) | |
454 | fprintf_filtered (stream, "<invalid address>"); | |
455 | else | |
a6bac58e TT |
456 | { |
457 | int result = 0; | |
458 | ||
459 | /* Attempt to run the Python pretty-printers on the | |
460 | baseclass if possible. */ | |
461 | if (!options->raw) | |
462 | result = apply_val_pretty_printer (baseclass, base_valaddr, | |
463 | thisoffset + boffset, | |
edf3d5f3 | 464 | address, |
a6bac58e TT |
465 | stream, recurse, |
466 | options, | |
467 | current_language); | |
468 | ||
469 | if (!result) | |
470 | cp_print_value_fields (baseclass, thistype, base_valaddr, | |
edf3d5f3 | 471 | thisoffset + boffset, address, |
a6bac58e TT |
472 | stream, recurse, options, |
473 | ((struct type **) | |
474 | obstack_base (&dont_print_vb_obstack)), | |
475 | 0); | |
476 | } | |
c906108c SS |
477 | fputs_filtered (", ", stream); |
478 | ||
479 | flush_it: | |
480 | ; | |
481 | } | |
482 | ||
483 | if (dont_print_vb == 0) | |
484 | { | |
485 | /* Free the space used to deal with the printing | |
c5aa993b | 486 | of this type from top level. */ |
c906108c SS |
487 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
488 | /* Reset watermark so that we can continue protecting | |
c5aa993b | 489 | ourselves from whatever we were protecting ourselves. */ |
c906108c SS |
490 | dont_print_vb_obstack = tmp_obstack; |
491 | } | |
492 | } | |
493 | ||
494 | /* Print value of a static member. | |
495 | To avoid infinite recursion when printing a class that contains | |
496 | a static instance of the class, we keep the addresses of all printed | |
497 | static member classes in an obstack and refuse to print them more | |
498 | than once. | |
499 | ||
79a45b7d | 500 | VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS |
c906108c SS |
501 | have the same meanings as in c_val_print. */ |
502 | ||
503 | static void | |
2c63a960 | 504 | cp_print_static_field (struct type *type, |
6943961c | 505 | struct value *val, |
2c63a960 | 506 | struct ui_file *stream, |
2c63a960 | 507 | int recurse, |
79a45b7d | 508 | const struct value_print_options *options) |
c906108c | 509 | { |
79a45b7d | 510 | struct value_print_options opts; |
c906108c SS |
511 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
512 | { | |
513 | CORE_ADDR *first_dont_print; | |
42ae5230 | 514 | CORE_ADDR addr; |
c906108c SS |
515 | int i; |
516 | ||
517 | first_dont_print | |
c5aa993b | 518 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
99903ae3 CM |
519 | i = obstack_object_size (&dont_print_statmem_obstack) |
520 | / sizeof (CORE_ADDR); | |
c906108c SS |
521 | |
522 | while (--i >= 0) | |
523 | { | |
42ae5230 | 524 | if (value_address (val) == first_dont_print[i]) |
c906108c | 525 | { |
2c63a960 JB |
526 | fputs_filtered ("<same as static member of an already" |
527 | " seen type>", | |
c906108c SS |
528 | stream); |
529 | return; | |
530 | } | |
531 | } | |
532 | ||
42ae5230 TT |
533 | addr = value_address (val); |
534 | obstack_grow (&dont_print_statmem_obstack, (char *) &addr, | |
c906108c SS |
535 | sizeof (CORE_ADDR)); |
536 | ||
537 | CHECK_TYPEDEF (type); | |
edf3d5f3 TT |
538 | cp_print_value_fields (type, value_enclosing_type (val), |
539 | value_contents_all (val), | |
42ae5230 | 540 | value_embedded_offset (val), addr, |
79a45b7d | 541 | stream, recurse, options, NULL, 1); |
c906108c SS |
542 | return; |
543 | } | |
79a45b7d TT |
544 | |
545 | opts = *options; | |
546 | opts.deref_ref = 0; | |
46615f07 | 547 | val_print (type, value_contents_all (val), |
42ae5230 | 548 | value_embedded_offset (val), value_address (val), |
79a45b7d | 549 | stream, recurse, &opts, current_language); |
c906108c SS |
550 | } |
551 | ||
0d5de010 DJ |
552 | |
553 | /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset | |
554 | OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing | |
555 | field number. If OFFSET is not exactly at the start of some field, set | |
556 | *DOMAIN to NULL. */ | |
557 | ||
2c0b251b | 558 | static void |
0d5de010 DJ |
559 | cp_find_class_member (struct type **domain_p, int *fieldno, |
560 | LONGEST offset) | |
561 | { | |
562 | struct type *domain; | |
563 | unsigned int i; | |
564 | unsigned len; | |
565 | ||
566 | *domain_p = check_typedef (*domain_p); | |
567 | domain = *domain_p; | |
568 | len = TYPE_NFIELDS (domain); | |
569 | ||
570 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
571 | { | |
572 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); | |
573 | ||
574 | QUIT; | |
575 | if (offset == bitpos) | |
576 | { | |
577 | *fieldno = i; | |
578 | return; | |
579 | } | |
580 | } | |
581 | ||
582 | for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) | |
583 | { | |
584 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); | |
585 | LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i)); | |
586 | ||
587 | if (offset >= bitpos && offset < bitpos + bitsize) | |
588 | { | |
589 | *domain_p = TYPE_FIELD_TYPE (domain, i); | |
590 | cp_find_class_member (domain_p, fieldno, offset - bitpos); | |
591 | return; | |
592 | } | |
593 | } | |
594 | ||
595 | *domain_p = NULL; | |
596 | } | |
597 | ||
c906108c | 598 | void |
ad4820ab | 599 | cp_print_class_member (const gdb_byte *valaddr, struct type *type, |
fba45db2 | 600 | struct ui_file *stream, char *prefix) |
c906108c | 601 | { |
e17a4113 UW |
602 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
603 | ||
c906108c SS |
604 | /* VAL is a byte offset into the structure type DOMAIN. |
605 | Find the name of the field for that offset and | |
606 | print it. */ | |
ad4820ab | 607 | struct type *domain = TYPE_DOMAIN_TYPE (type); |
e17a4113 | 608 | LONGEST val; |
0d5de010 | 609 | unsigned int fieldno; |
c906108c | 610 | |
e17a4113 UW |
611 | val = extract_signed_integer (valaddr, TYPE_LENGTH (type), byte_order); |
612 | ||
0d5de010 DJ |
613 | /* Pointers to data members are usually byte offsets into an object. |
614 | Because a data member can have offset zero, and a NULL pointer to | |
615 | member must be distinct from any valid non-NULL pointer to | |
616 | member, either the value is biased or the NULL value has a | |
617 | special representation; both are permitted by ISO C++. HP aCC | |
618 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x | |
619 | and other compilers which use the Itanium ABI use -1 as the NULL | |
620 | value. GDB only supports that last form; to add support for | |
621 | another form, make this into a cp-abi hook. */ | |
c906108c | 622 | |
0d5de010 | 623 | if (val == -1) |
c906108c | 624 | { |
0d5de010 DJ |
625 | fprintf_filtered (stream, "NULL"); |
626 | return; | |
c906108c | 627 | } |
0d5de010 DJ |
628 | |
629 | cp_find_class_member (&domain, &fieldno, val << 3); | |
630 | ||
631 | if (domain != NULL) | |
c906108c SS |
632 | { |
633 | char *name; | |
306d9ac5 | 634 | fputs_filtered (prefix, stream); |
c906108c SS |
635 | name = type_name_no_tag (domain); |
636 | if (name) | |
c5aa993b | 637 | fputs_filtered (name, stream); |
c906108c SS |
638 | else |
639 | c_type_print_base (domain, stream, 0, 0); | |
640 | fprintf_filtered (stream, "::"); | |
0d5de010 | 641 | fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream); |
c906108c SS |
642 | } |
643 | else | |
0d5de010 | 644 | fprintf_filtered (stream, "%ld", (long) val); |
c906108c SS |
645 | } |
646 | ||
647 | ||
c906108c | 648 | void |
fba45db2 | 649 | _initialize_cp_valprint (void) |
c906108c | 650 | { |
5bf193a2 | 651 | add_setshow_boolean_cmd ("static-members", class_support, |
79a45b7d | 652 | &user_print_options.static_field_print, _("\ |
5bf193a2 AC |
653 | Set printing of C++ static members."), _("\ |
654 | Show printing of C++ static members."), NULL, | |
655 | NULL, | |
920d2a44 | 656 | show_static_field_print, |
5bf193a2 | 657 | &setprintlist, &showprintlist); |
c906108c | 658 | |
79a45b7d TT |
659 | add_setshow_boolean_cmd ("vtbl", class_support, |
660 | &user_print_options.vtblprint, _("\ | |
5bf193a2 AC |
661 | Set printing of C++ virtual function tables."), _("\ |
662 | Show printing of C++ virtual function tables."), NULL, | |
663 | NULL, | |
920d2a44 | 664 | show_vtblprint, |
5bf193a2 AC |
665 | &setprintlist, &showprintlist); |
666 | ||
79a45b7d TT |
667 | add_setshow_boolean_cmd ("object", class_support, |
668 | &user_print_options.objectprint, _("\ | |
5bf193a2 AC |
669 | Set printing of object's derived type based on vtable info."), _("\ |
670 | Show printing of object's derived type based on vtable info."), NULL, | |
671 | NULL, | |
920d2a44 | 672 | show_objectprint, |
5bf193a2 | 673 | &setprintlist, &showprintlist); |
c906108c | 674 | |
99903ae3 | 675 | obstack_begin (&dont_print_statmem_obstack, 32 * sizeof (CORE_ADDR)); |
c906108c | 676 | obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *)); |
c906108c | 677 | } |