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