Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
a2bd3dcd | 2 | |
6aba47ca DJ |
3 | Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
4 | 2000, 2001, 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
04ea0df1 | 22 | #include "gdb_obstack.h" |
c906108c SS |
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" | |
30 | #include "annotate.h" | |
31 | #include "gdb_string.h" | |
32 | #include "c-lang.h" | |
33 | #include "target.h" | |
b9d652ac | 34 | #include "cp-abi.h" |
973177d3 | 35 | #include "valprint.h" |
d3cbe7ef | 36 | #include "cp-support.h" |
cf309262 | 37 | #include "language.h" |
c906108c | 38 | |
920d2a44 AC |
39 | /* Controls printing of vtbl's */ |
40 | int vtblprint; | |
41 | static void | |
42 | show_vtblprint (struct ui_file *file, int from_tty, | |
43 | struct cmd_list_element *c, const char *value) | |
44 | { | |
45 | fprintf_filtered (file, _("\ | |
46 | Printing of C++ virtual function tables is %s.\n"), | |
47 | value); | |
48 | } | |
49 | ||
50 | /* Controls looking up an object's derived type using what we find in | |
51 | its vtables. */ | |
52 | int objectprint; | |
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 | ||
c5aa993b | 63 | int static_field_print; /* Controls printing of static fields. */ |
920d2a44 AC |
64 | static void |
65 | show_static_field_print (struct ui_file *file, int from_tty, | |
66 | struct cmd_list_element *c, const char *value) | |
67 | { | |
68 | fprintf_filtered (file, _("Printing of C++ static members is %s.\n"), | |
69 | value); | |
70 | } | |
71 | ||
c906108c SS |
72 | |
73 | static struct obstack dont_print_vb_obstack; | |
74 | static struct obstack dont_print_statmem_obstack; | |
75 | ||
a14ed312 | 76 | extern void _initialize_cp_valprint (void); |
392a587b | 77 | |
6943961c | 78 | static void cp_print_static_field (struct type *, struct value *, |
d9fcf2fb JM |
79 | struct ui_file *, int, int, |
80 | enum val_prettyprint); | |
c906108c | 81 | |
fc1a4b47 | 82 | static void cp_print_value (struct type *, struct type *, const gdb_byte *, |
a2bd3dcd | 83 | int, CORE_ADDR, struct ui_file *, int, int, |
d9fcf2fb | 84 | enum val_prettyprint, struct type **); |
c906108c | 85 | |
d9fcf2fb | 86 | static void cp_print_hpacc_virtual_table_entries (struct type *, int *, |
6943961c | 87 | struct value *, |
d9fcf2fb JM |
88 | struct ui_file *, int, |
89 | int, | |
90 | enum val_prettyprint); | |
c906108c SS |
91 | |
92 | ||
8343f86c | 93 | /* GCC versions after 2.4.5 use this. */ |
2c63a960 | 94 | const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
c906108c | 95 | |
8343f86c | 96 | /* HP aCC uses different names. */ |
2c63a960 JB |
97 | const char hpacc_vtbl_ptr_name[] = "__vfp"; |
98 | const char hpacc_vtbl_ptr_type_name[] = "__vftyp"; | |
c906108c | 99 | |
c906108c SS |
100 | /* Return truth value for assertion that TYPE is of the type |
101 | "pointer to virtual function". */ | |
102 | ||
103 | int | |
fba45db2 | 104 | cp_is_vtbl_ptr_type (struct type *type) |
c906108c SS |
105 | { |
106 | char *typename = type_name_no_tag (type); | |
107 | ||
8343f86c | 108 | return (typename != NULL && !strcmp (typename, vtbl_ptr_name)); |
c906108c SS |
109 | } |
110 | ||
111 | /* Return truth value for the assertion that TYPE is of the type | |
112 | "pointer to virtual function table". */ | |
113 | ||
114 | int | |
fba45db2 | 115 | cp_is_vtbl_member (struct type *type) |
c906108c | 116 | { |
0e5e3ea6 PS |
117 | /* With older versions of g++, the vtbl field pointed to an array |
118 | of structures. Nowadays it points directly to the structure. */ | |
c906108c SS |
119 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
120 | { | |
121 | type = TYPE_TARGET_TYPE (type); | |
122 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
123 | { | |
124 | type = TYPE_TARGET_TYPE (type); | |
c5aa993b JM |
125 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ |
126 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
c906108c SS |
127 | { |
128 | /* Virtual functions tables are full of pointers | |
c5aa993b | 129 | to virtual functions. */ |
c906108c SS |
130 | return cp_is_vtbl_ptr_type (type); |
131 | } | |
132 | } | |
0e5e3ea6 PS |
133 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ |
134 | { | |
135 | return cp_is_vtbl_ptr_type (type); | |
136 | } | |
137 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ | |
138 | { | |
139 | /* The type name of the thunk pointer is NULL when using dwarf2. | |
140 | We could test for a pointer to a function, but there is | |
141 | no type info for the virtual table either, so it wont help. */ | |
142 | return cp_is_vtbl_ptr_type (type); | |
143 | } | |
c906108c SS |
144 | } |
145 | return 0; | |
146 | } | |
147 | ||
148 | /* Mutually recursive subroutines of cp_print_value and c_val_print to | |
149 | print out a structure's fields: cp_print_value_fields and cp_print_value. | |
c5aa993b | 150 | |
c906108c SS |
151 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the |
152 | same meanings as in cp_print_value and c_val_print. | |
153 | ||
154 | 2nd argument REAL_TYPE is used to carry over the type of the derived | |
155 | class across the recursion to base classes. | |
156 | ||
157 | DONT_PRINT is an array of baseclass types that we | |
158 | should not print, or zero if called from top level. */ | |
159 | ||
160 | void | |
a2bd3dcd | 161 | cp_print_value_fields (struct type *type, struct type *real_type, |
fc1a4b47 | 162 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
a2bd3dcd AC |
163 | struct ui_file *stream, int format, int recurse, |
164 | enum val_prettyprint pretty, | |
165 | struct type **dont_print_vb,int dont_print_statmem) | |
c906108c SS |
166 | { |
167 | int i, len, n_baseclasses; | |
c906108c SS |
168 | char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack); |
169 | int fields_seen = 0; | |
170 | ||
171 | CHECK_TYPEDEF (type); | |
172 | ||
173 | fprintf_filtered (stream, "{"); | |
174 | len = TYPE_NFIELDS (type); | |
175 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
176 | ||
177 | /* First, print out baseclasses such that we don't print | |
178 | duplicates of virtual baseclasses. */ | |
179 | ||
180 | if (n_baseclasses > 0) | |
181 | cp_print_value (type, real_type, valaddr, offset, address, stream, | |
c5aa993b | 182 | format, recurse + 1, pretty, dont_print_vb); |
c906108c SS |
183 | |
184 | /* Second, print out data fields */ | |
185 | ||
186 | /* If there are no data fields, or if the only field is the | |
c5aa993b | 187 | * vtbl pointer, skip this part */ |
2c63a960 JB |
188 | if ((len == n_baseclasses) |
189 | || ((len - n_baseclasses == 1) | |
190 | && TYPE_HAS_VTABLE (type) | |
5cb316ef AC |
191 | && strncmp (TYPE_FIELD_NAME (type, n_baseclasses), |
192 | hpacc_vtbl_ptr_name, 5) == 0) | |
2c63a960 | 193 | || !len) |
c906108c SS |
194 | fprintf_filtered (stream, "<No data fields>"); |
195 | else | |
196 | { | |
c1b6e682 DJ |
197 | struct obstack tmp_obstack = dont_print_statmem_obstack; |
198 | ||
c906108c SS |
199 | if (dont_print_statmem == 0) |
200 | { | |
201 | /* If we're at top level, carve out a completely fresh | |
202 | chunk of the obstack and use that until this particular | |
203 | invocation returns. */ | |
c906108c SS |
204 | obstack_finish (&dont_print_statmem_obstack); |
205 | } | |
206 | ||
207 | for (i = n_baseclasses; i < len; i++) | |
208 | { | |
209 | /* If requested, skip printing of static fields. */ | |
210 | if (!static_field_print && TYPE_FIELD_STATIC (type, i)) | |
211 | continue; | |
212 | ||
c5aa993b | 213 | /* If a vtable pointer appears, we'll print it out later */ |
2c63a960 | 214 | if (TYPE_HAS_VTABLE (type) |
5cb316ef AC |
215 | && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, |
216 | 5) == 0) | |
c5aa993b JM |
217 | continue; |
218 | ||
c906108c SS |
219 | if (fields_seen) |
220 | fprintf_filtered (stream, ", "); | |
221 | else if (n_baseclasses > 0) | |
222 | { | |
223 | if (pretty) | |
224 | { | |
225 | fprintf_filtered (stream, "\n"); | |
226 | print_spaces_filtered (2 + 2 * recurse, stream); | |
227 | fputs_filtered ("members of ", stream); | |
228 | fputs_filtered (type_name_no_tag (type), stream); | |
229 | fputs_filtered (": ", stream); | |
230 | } | |
231 | } | |
232 | fields_seen = 1; | |
233 | ||
234 | if (pretty) | |
235 | { | |
236 | fprintf_filtered (stream, "\n"); | |
237 | print_spaces_filtered (2 + 2 * recurse, stream); | |
238 | } | |
c5aa993b | 239 | else |
c906108c SS |
240 | { |
241 | wrap_here (n_spaces (2 + 2 * recurse)); | |
242 | } | |
243 | if (inspect_it) | |
244 | { | |
245 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
246 | fputs_filtered ("\"( ptr \"", stream); | |
247 | else | |
248 | fputs_filtered ("\"( nodef \"", stream); | |
249 | if (TYPE_FIELD_STATIC (type, i)) | |
250 | fputs_filtered ("static ", stream); | |
251 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 252 | current_language->la_language, |
c906108c SS |
253 | DMGL_PARAMS | DMGL_ANSI); |
254 | fputs_filtered ("\" \"", stream); | |
255 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 256 | current_language->la_language, |
c906108c SS |
257 | DMGL_PARAMS | DMGL_ANSI); |
258 | fputs_filtered ("\") \"", stream); | |
259 | } | |
260 | else | |
261 | { | |
262 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); | |
263 | ||
264 | if (TYPE_FIELD_STATIC (type, i)) | |
265 | fputs_filtered ("static ", stream); | |
266 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), | |
cf309262 | 267 | current_language->la_language, |
c906108c SS |
268 | DMGL_PARAMS | DMGL_ANSI); |
269 | annotate_field_name_end (); | |
270 | /* do not print leading '=' in case of anonymous unions */ | |
271 | if (strcmp (TYPE_FIELD_NAME (type, i), "")) | |
272 | fputs_filtered (" = ", stream); | |
273 | annotate_field_value (); | |
274 | } | |
275 | ||
276 | if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i)) | |
277 | { | |
6943961c | 278 | struct value *v; |
c906108c SS |
279 | |
280 | /* Bitfields require special handling, especially due to byte | |
c5aa993b | 281 | order problems. */ |
c906108c SS |
282 | if (TYPE_FIELD_IGNORE (type, i)) |
283 | { | |
c5aa993b | 284 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
285 | } |
286 | else | |
287 | { | |
2c63a960 JB |
288 | v = value_from_longest |
289 | (TYPE_FIELD_TYPE (type, i), | |
290 | unpack_field_as_long (type, valaddr + offset, i)); | |
c906108c | 291 | |
806048c6 | 292 | common_val_print (v, stream, format, 0, recurse + 1, pretty); |
c906108c SS |
293 | } |
294 | } | |
295 | else | |
296 | { | |
297 | if (TYPE_FIELD_IGNORE (type, i)) | |
298 | { | |
c5aa993b | 299 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
300 | } |
301 | else if (TYPE_FIELD_STATIC (type, i)) | |
302 | { | |
6943961c | 303 | struct value *v = value_static_field (type, i); |
c906108c SS |
304 | if (v == NULL) |
305 | fputs_filtered ("<optimized out>", stream); | |
306 | else | |
307 | cp_print_static_field (TYPE_FIELD_TYPE (type, i), v, | |
308 | stream, format, recurse + 1, | |
309 | pretty); | |
310 | } | |
311 | else | |
312 | { | |
c5aa993b | 313 | val_print (TYPE_FIELD_TYPE (type, i), |
2c63a960 | 314 | valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8, |
c5aa993b JM |
315 | address + TYPE_FIELD_BITPOS (type, i) / 8, |
316 | stream, format, 0, recurse + 1, pretty); | |
c906108c SS |
317 | } |
318 | } | |
319 | annotate_field_end (); | |
320 | } | |
321 | ||
322 | if (dont_print_statmem == 0) | |
323 | { | |
324 | /* Free the space used to deal with the printing | |
325 | of the members from top level. */ | |
326 | obstack_free (&dont_print_statmem_obstack, last_dont_print); | |
327 | dont_print_statmem_obstack = tmp_obstack; | |
328 | } | |
329 | ||
330 | if (pretty) | |
331 | { | |
332 | fprintf_filtered (stream, "\n"); | |
333 | print_spaces_filtered (2 * recurse, stream); | |
334 | } | |
c5aa993b JM |
335 | } /* if there are data fields */ |
336 | /* Now print out the virtual table pointer if there is one */ | |
2c63a960 | 337 | if (TYPE_HAS_VTABLE (type) |
5cb316ef AC |
338 | && strncmp (TYPE_FIELD_NAME (type, n_baseclasses), |
339 | hpacc_vtbl_ptr_name, 5) == 0) | |
c906108c | 340 | { |
6943961c | 341 | struct value *v; |
c5aa993b | 342 | /* First get the virtual table pointer and print it out */ |
c906108c SS |
343 | |
344 | #if 0 | |
345 | fputs_filtered ("__vfp = ", stream); | |
346 | #endif | |
347 | ||
348 | fputs_filtered (", Virtual table at ", stream); | |
349 | ||
350 | /* pai: FIXME 32x64 problem? */ | |
351 | /* Not sure what the best notation is in the case where there is no | |
352 | baseclass name. */ | |
4478b372 | 353 | v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long), |
c5aa993b | 354 | *(unsigned long *) (valaddr + offset)); |
c906108c | 355 | |
806048c6 | 356 | common_val_print (v, stream, format, 0, recurse + 1, pretty); |
c906108c SS |
357 | fields_seen = 1; |
358 | ||
359 | if (vtblprint) | |
c5aa993b JM |
360 | { |
361 | /* Print out function pointers in vtable. */ | |
c906108c | 362 | |
c5aa993b JM |
363 | /* FIXME: then-clause is for non-RRBC layout of virtual |
364 | * table. The RRBC case in the else-clause is yet to be | |
365 | * implemented. The if (1) below should be changed to a | |
366 | * test for whether the executable we have was compiled | |
367 | * with a version of HP aCC that doesn't have RRBC | |
368 | * support. */ | |
c906108c | 369 | |
c5aa993b JM |
370 | if (1) |
371 | { | |
2c63a960 JB |
372 | /* no RRBC support; function pointers embedded directly |
373 | in vtable */ | |
c906108c | 374 | |
c5aa993b | 375 | int vfuncs = count_virtual_fns (real_type); |
c906108c | 376 | |
c5aa993b | 377 | fputs_filtered (" {", stream); |
c906108c | 378 | |
c5aa993b | 379 | /* FIXME : doesn't work at present */ |
c906108c | 380 | #if 0 |
2c63a960 JB |
381 | fprintf_filtered (stream, "%d entr%s: ", vfuncs, |
382 | vfuncs == 1 ? "y" : "ies"); | |
c906108c | 383 | #else |
c5aa993b | 384 | fputs_filtered ("not implemented", stream); |
c906108c SS |
385 | |
386 | ||
387 | #endif | |
388 | ||
c5aa993b | 389 | /* recursive function that prints all virtual function entries */ |
c906108c | 390 | #if 0 |
2c63a960 JB |
391 | cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, |
392 | stream, format, recurse, | |
393 | pretty); | |
c906108c | 394 | #endif |
c5aa993b JM |
395 | fputs_filtered ("}", stream); |
396 | } /* non-RRBC case */ | |
397 | else | |
398 | { | |
4d33f415 | 399 | /* FIXME -- see comments above */ |
c5aa993b JM |
400 | /* RRBC support present; function pointers are found |
401 | * by indirection through the class segment entries. */ | |
402 | ||
403 | ||
404 | } /* RRBC case */ | |
405 | } /* if vtblprint */ | |
c906108c SS |
406 | |
407 | if (pretty) | |
408 | { | |
409 | fprintf_filtered (stream, "\n"); | |
410 | print_spaces_filtered (2 * recurse, stream); | |
411 | } | |
412 | ||
c5aa993b JM |
413 | } /* if vtable exists */ |
414 | ||
c906108c SS |
415 | fprintf_filtered (stream, "}"); |
416 | } | |
417 | ||
418 | /* Special val_print routine to avoid printing multiple copies of virtual | |
419 | baseclasses. */ | |
420 | ||
421 | static void | |
a2bd3dcd | 422 | cp_print_value (struct type *type, struct type *real_type, |
fc1a4b47 | 423 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
a2bd3dcd AC |
424 | struct ui_file *stream, int format, int recurse, |
425 | enum val_prettyprint pretty, struct type **dont_print_vb) | |
c906108c | 426 | { |
c906108c | 427 | struct type **last_dont_print |
2c63a960 | 428 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
c1b6e682 | 429 | struct obstack tmp_obstack = dont_print_vb_obstack; |
c906108c | 430 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
b9d652ac DJ |
431 | int thisoffset; |
432 | struct type *thistype; | |
c906108c SS |
433 | |
434 | if (dont_print_vb == 0) | |
435 | { | |
436 | /* If we're at top level, carve out a completely fresh | |
c5aa993b JM |
437 | chunk of the obstack and use that until this particular |
438 | invocation returns. */ | |
c906108c SS |
439 | /* Bump up the high-water mark. Now alpha is omega. */ |
440 | obstack_finish (&dont_print_vb_obstack); | |
441 | } | |
442 | ||
443 | for (i = 0; i < n_baseclasses; i++) | |
444 | { | |
445 | int boffset; | |
446 | int skip; | |
447 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
448 | char *basename = TYPE_NAME (baseclass); | |
fc1a4b47 | 449 | const gdb_byte *base_valaddr; |
c906108c SS |
450 | |
451 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
452 | { | |
453 | struct type **first_dont_print | |
2c63a960 | 454 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
c906108c | 455 | |
c5aa993b | 456 | int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) |
2c63a960 | 457 | - first_dont_print; |
c906108c SS |
458 | |
459 | while (--j >= 0) | |
460 | if (baseclass == first_dont_print[j]) | |
461 | goto flush_it; | |
462 | ||
463 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); | |
464 | } | |
465 | ||
b9d652ac DJ |
466 | thisoffset = offset; |
467 | thistype = real_type; | |
c906108c | 468 | if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i)) |
c5aa993b JM |
469 | { |
470 | /* Assume HP/Taligent runtime convention */ | |
471 | find_rt_vbase_offset (type, TYPE_BASECLASS (type, i), | |
472 | valaddr, offset, &boffset, &skip); | |
473 | if (skip >= 0) | |
8a3fe4f8 AC |
474 | error (_("Virtual base class offset not found from vtable while" |
475 | " printing")); | |
c5aa993b JM |
476 | base_valaddr = valaddr; |
477 | } | |
c906108c | 478 | else |
c5aa993b | 479 | { |
2c63a960 JB |
480 | boffset = baseclass_offset (type, i, |
481 | valaddr + offset, | |
9e14d721 | 482 | address); |
c5aa993b | 483 | skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1; |
c906108c | 484 | |
c5aa993b JM |
485 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
486 | { | |
2c63a960 JB |
487 | /* The virtual base class pointer might have been |
488 | clobbered by the user program. Make sure that it | |
489 | still points to a valid memory location. */ | |
c906108c | 490 | |
2c63a960 JB |
491 | if (boffset != -1 |
492 | && ((boffset + offset) < 0 | |
493 | || (boffset + offset) >= TYPE_LENGTH (type))) | |
c5aa993b | 494 | { |
34c0bd93 | 495 | /* FIXME (alloca): unsafe if baseclass is really really large. */ |
fc1a4b47 | 496 | gdb_byte *buf = alloca (TYPE_LENGTH (baseclass)); |
a2bd3dcd AC |
497 | base_valaddr = buf; |
498 | if (target_read_memory (address + boffset, buf, | |
c5aa993b JM |
499 | TYPE_LENGTH (baseclass)) != 0) |
500 | skip = 1; | |
9e14d721 | 501 | address = address + boffset; |
b9d652ac DJ |
502 | thisoffset = 0; |
503 | boffset = 0; | |
504 | thistype = baseclass; | |
c5aa993b JM |
505 | } |
506 | else | |
507 | base_valaddr = valaddr; | |
508 | } | |
509 | else | |
510 | base_valaddr = valaddr; | |
c906108c SS |
511 | } |
512 | ||
513 | /* now do the printing */ | |
514 | if (pretty) | |
515 | { | |
516 | fprintf_filtered (stream, "\n"); | |
517 | print_spaces_filtered (2 * recurse, stream); | |
518 | } | |
519 | fputs_filtered ("<", stream); | |
520 | /* Not sure what the best notation is in the case where there is no | |
c5aa993b | 521 | baseclass name. */ |
c906108c SS |
522 | fputs_filtered (basename ? basename : "", stream); |
523 | fputs_filtered ("> = ", stream); | |
524 | ||
525 | ||
526 | if (skip >= 1) | |
527 | fprintf_filtered (stream, "<invalid address>"); | |
528 | else | |
b9d652ac | 529 | cp_print_value_fields (baseclass, thistype, base_valaddr, |
9e14d721 DJ |
530 | thisoffset + boffset, address + boffset, |
531 | stream, format, | |
2c63a960 JB |
532 | recurse, pretty, |
533 | ((struct type **) | |
534 | obstack_base (&dont_print_vb_obstack)), | |
c906108c SS |
535 | 0); |
536 | fputs_filtered (", ", stream); | |
537 | ||
538 | flush_it: | |
539 | ; | |
540 | } | |
541 | ||
542 | if (dont_print_vb == 0) | |
543 | { | |
544 | /* Free the space used to deal with the printing | |
c5aa993b | 545 | of this type from top level. */ |
c906108c SS |
546 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
547 | /* Reset watermark so that we can continue protecting | |
c5aa993b | 548 | ourselves from whatever we were protecting ourselves. */ |
c906108c SS |
549 | dont_print_vb_obstack = tmp_obstack; |
550 | } | |
551 | } | |
552 | ||
553 | /* Print value of a static member. | |
554 | To avoid infinite recursion when printing a class that contains | |
555 | a static instance of the class, we keep the addresses of all printed | |
556 | static member classes in an obstack and refuse to print them more | |
557 | than once. | |
558 | ||
559 | VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY | |
560 | have the same meanings as in c_val_print. */ | |
561 | ||
562 | static void | |
2c63a960 | 563 | cp_print_static_field (struct type *type, |
6943961c | 564 | struct value *val, |
2c63a960 JB |
565 | struct ui_file *stream, |
566 | int format, | |
567 | int recurse, | |
568 | enum val_prettyprint pretty) | |
c906108c SS |
569 | { |
570 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
571 | { | |
572 | CORE_ADDR *first_dont_print; | |
573 | int i; | |
574 | ||
575 | first_dont_print | |
c5aa993b JM |
576 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
577 | i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack) | |
c906108c SS |
578 | - first_dont_print; |
579 | ||
580 | while (--i >= 0) | |
581 | { | |
582 | if (VALUE_ADDRESS (val) == first_dont_print[i]) | |
583 | { | |
2c63a960 JB |
584 | fputs_filtered ("<same as static member of an already" |
585 | " seen type>", | |
c906108c SS |
586 | stream); |
587 | return; | |
588 | } | |
589 | } | |
590 | ||
591 | obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val), | |
592 | sizeof (CORE_ADDR)); | |
593 | ||
594 | CHECK_TYPEDEF (type); | |
46615f07 | 595 | cp_print_value_fields (type, type, value_contents_all (val), |
13c3b5f5 | 596 | value_embedded_offset (val), VALUE_ADDRESS (val), |
c906108c SS |
597 | stream, format, recurse, pretty, NULL, 1); |
598 | return; | |
599 | } | |
46615f07 | 600 | val_print (type, value_contents_all (val), |
13c3b5f5 | 601 | value_embedded_offset (val), VALUE_ADDRESS (val), |
c906108c SS |
602 | stream, format, 0, recurse, pretty); |
603 | } | |
604 | ||
0d5de010 DJ |
605 | |
606 | /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset | |
607 | OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing | |
608 | field number. If OFFSET is not exactly at the start of some field, set | |
609 | *DOMAIN to NULL. */ | |
610 | ||
611 | void | |
612 | cp_find_class_member (struct type **domain_p, int *fieldno, | |
613 | LONGEST offset) | |
614 | { | |
615 | struct type *domain; | |
616 | unsigned int i; | |
617 | unsigned len; | |
618 | ||
619 | *domain_p = check_typedef (*domain_p); | |
620 | domain = *domain_p; | |
621 | len = TYPE_NFIELDS (domain); | |
622 | ||
623 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
624 | { | |
625 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); | |
626 | ||
627 | QUIT; | |
628 | if (offset == bitpos) | |
629 | { | |
630 | *fieldno = i; | |
631 | return; | |
632 | } | |
633 | } | |
634 | ||
635 | for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) | |
636 | { | |
637 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); | |
638 | LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i)); | |
639 | ||
640 | if (offset >= bitpos && offset < bitpos + bitsize) | |
641 | { | |
642 | *domain_p = TYPE_FIELD_TYPE (domain, i); | |
643 | cp_find_class_member (domain_p, fieldno, offset - bitpos); | |
644 | return; | |
645 | } | |
646 | } | |
647 | ||
648 | *domain_p = NULL; | |
649 | } | |
650 | ||
c906108c | 651 | void |
fc1a4b47 | 652 | cp_print_class_member (const gdb_byte *valaddr, struct type *domain, |
fba45db2 | 653 | struct ui_file *stream, char *prefix) |
c906108c | 654 | { |
c906108c SS |
655 | /* VAL is a byte offset into the structure type DOMAIN. |
656 | Find the name of the field for that offset and | |
657 | print it. */ | |
0d5de010 | 658 | unsigned int fieldno; |
c906108c | 659 | |
0d5de010 | 660 | LONGEST val = unpack_long (builtin_type_long, valaddr); |
c906108c | 661 | |
0d5de010 DJ |
662 | /* Pointers to data members are usually byte offsets into an object. |
663 | Because a data member can have offset zero, and a NULL pointer to | |
664 | member must be distinct from any valid non-NULL pointer to | |
665 | member, either the value is biased or the NULL value has a | |
666 | special representation; both are permitted by ISO C++. HP aCC | |
667 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x | |
668 | and other compilers which use the Itanium ABI use -1 as the NULL | |
669 | value. GDB only supports that last form; to add support for | |
670 | another form, make this into a cp-abi hook. */ | |
c906108c | 671 | |
0d5de010 | 672 | if (val == -1) |
c906108c | 673 | { |
0d5de010 DJ |
674 | fprintf_filtered (stream, "NULL"); |
675 | return; | |
c906108c | 676 | } |
0d5de010 DJ |
677 | |
678 | cp_find_class_member (&domain, &fieldno, val << 3); | |
679 | ||
680 | if (domain != NULL) | |
c906108c SS |
681 | { |
682 | char *name; | |
306d9ac5 | 683 | fputs_filtered (prefix, stream); |
c906108c SS |
684 | name = type_name_no_tag (domain); |
685 | if (name) | |
c5aa993b | 686 | fputs_filtered (name, stream); |
c906108c SS |
687 | else |
688 | c_type_print_base (domain, stream, 0, 0); | |
689 | fprintf_filtered (stream, "::"); | |
0d5de010 | 690 | fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream); |
c906108c SS |
691 | } |
692 | else | |
0d5de010 | 693 | fprintf_filtered (stream, "%ld", (long) val); |
c906108c SS |
694 | } |
695 | ||
696 | ||
697 | /* This function prints out virtual table entries for a class; it | |
698 | * recurses on the base classes to find all virtual functions | |
699 | * available in a class. | |
700 | * | |
701 | * pai/1997-05-21 Note: As the name suggests, it's currently | |
702 | * implemented for HP aCC runtime only. g++ objects are handled | |
703 | * differently and I have made no attempt to fold that logic in | |
704 | * here. The runtime layout is different for the two cases. Also, | |
705 | * this currently has only the code for non-RRBC layouts generated by | |
706 | * the HP aCC compiler; RRBC code is stubbed out and will have to be | |
707 | * added later. */ | |
c5aa993b | 708 | |
c906108c SS |
709 | |
710 | static void | |
fba45db2 | 711 | cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs, |
6943961c | 712 | struct value *v, struct ui_file *stream, |
fba45db2 KB |
713 | int format, int recurse, |
714 | enum val_prettyprint pretty) | |
c906108c SS |
715 | { |
716 | int fn, oi; | |
717 | ||
718 | /* pai: FIXME this function doesn't work. It should handle a given | |
719 | * virtual function only once (latest redefinition in class hierarchy) | |
720 | */ | |
721 | ||
c5aa993b JM |
722 | /* Recursion on other classes that can share the same vtable */ |
723 | struct type *pbc = primary_base_class (type); | |
c906108c | 724 | if (pbc) |
2c63a960 JB |
725 | cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, |
726 | recurse, pretty); | |
c5aa993b | 727 | |
c906108c SS |
728 | /* Now deal with vfuncs declared in this class */ |
729 | for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++) | |
730 | for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++) | |
731 | if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi)) | |
c5aa993b JM |
732 | { |
733 | char *vf_name; | |
2c63a960 | 734 | const char *field_physname; |
c5aa993b JM |
735 | |
736 | /* virtual function offset */ | |
2c63a960 JB |
737 | int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) |
738 | - 1); | |
c5aa993b JM |
739 | |
740 | /* Get the address of the vfunction entry */ | |
6943961c | 741 | struct value *vf = value_copy (v); |
d69fe07e | 742 | if (value_lazy (vf)) |
c5aa993b | 743 | (void) value_fetch_lazy (vf); |
2c63a960 | 744 | /* adjust by offset */ |
5086187c AC |
745 | /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ |
746 | value_contents_writeable (vf)[0] += 4 * (HP_ACC_VFUNC_START + vx); | |
c5aa993b | 747 | vf = value_ind (vf); /* get the entry */ |
04624583 AC |
748 | /* make it a pointer */ |
749 | deprecated_set_value_type (vf, value_type (v)); | |
c5aa993b JM |
750 | |
751 | /* print out the entry */ | |
806048c6 | 752 | common_val_print (vf, stream, format, 0, recurse + 1, pretty); |
2c63a960 JB |
753 | field_physname |
754 | = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi); | |
755 | /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */ | |
756 | vf_name = cplus_demangle (field_physname, DMGL_ARM); | |
c5aa993b | 757 | fprintf_filtered (stream, " %s", vf_name); |
34e2dfde | 758 | xfree (vf_name); |
c5aa993b JM |
759 | if (--(*vfuncs) > 0) |
760 | fputs_filtered (", ", stream); | |
761 | } | |
c906108c SS |
762 | } |
763 | ||
764 | ||
765 | ||
766 | void | |
fba45db2 | 767 | _initialize_cp_valprint (void) |
c906108c | 768 | { |
5bf193a2 AC |
769 | add_setshow_boolean_cmd ("static-members", class_support, |
770 | &static_field_print, _("\ | |
771 | Set printing of C++ static members."), _("\ | |
772 | Show printing of C++ static members."), NULL, | |
773 | NULL, | |
920d2a44 | 774 | show_static_field_print, |
5bf193a2 | 775 | &setprintlist, &showprintlist); |
c906108c SS |
776 | /* Turn on printing of static fields. */ |
777 | static_field_print = 1; | |
778 | ||
5bf193a2 AC |
779 | add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\ |
780 | Set printing of C++ virtual function tables."), _("\ | |
781 | Show printing of C++ virtual function tables."), NULL, | |
782 | NULL, | |
920d2a44 | 783 | show_vtblprint, |
5bf193a2 AC |
784 | &setprintlist, &showprintlist); |
785 | ||
786 | add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\ | |
787 | Set printing of object's derived type based on vtable info."), _("\ | |
788 | Show printing of object's derived type based on vtable info."), NULL, | |
789 | NULL, | |
920d2a44 | 790 | show_objectprint, |
5bf193a2 | 791 | &setprintlist, &showprintlist); |
c906108c SS |
792 | |
793 | /* Give people the defaults which they are used to. */ | |
794 | objectprint = 0; | |
795 | vtblprint = 0; | |
796 | obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *)); | |
797 | obstack_specify_allocation (&dont_print_statmem_obstack, | |
798 | 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR), | |
b8c9b27d | 799 | xmalloc, xfree); |
c906108c | 800 | } |