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