Commit | Line | Data |
---|---|---|
a8a69e63 FF |
1 | /* Support for printing C++ values for GDB, the GNU debugger. |
2 | Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "obstack.h" | |
22 | #include "symtab.h" | |
23 | #include "gdbtypes.h" | |
24 | #include "expression.h" | |
25 | #include "value.h" | |
26 | #include "command.h" | |
27 | #include "gdbcmd.h" | |
28 | ||
29 | int vtblprint; /* Controls printing of vtbl's */ | |
30 | int objectprint; /* Controls looking up an object's derived type | |
31 | using what we find in its vtables. */ | |
32 | struct obstack dont_print_obstack; | |
33 | ||
34 | static void | |
35 | cplus_print_value PARAMS ((struct type *, char *, FILE *, int, int, | |
36 | enum val_prettyprint, struct type **)); | |
37 | ||
38 | /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */ | |
39 | ||
40 | extern void | |
41 | c_type_print_base PARAMS ((struct type *, FILE *, int, int)); | |
42 | ||
43 | extern void | |
44 | c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int)); | |
45 | ||
46 | extern void | |
47 | cp_type_print_method_args PARAMS ((struct type **, char *, char *, int, | |
48 | FILE *)); | |
49 | ||
50 | extern struct obstack dont_print_obstack; | |
51 | ||
52 | /* END-FIXME */ | |
53 | ||
54 | ||
55 | /* BEGIN-FIXME: Hooks into c-valprint.c */ | |
56 | ||
57 | extern int | |
58 | c_val_print PARAMS ((struct type *, char *, CORE_ADDR, FILE *, int, int, int, | |
59 | enum val_prettyprint)); | |
60 | /* END-FIXME */ | |
61 | ||
62 | ||
c7da3ed3 FF |
63 | void |
64 | cp_print_class_method (valaddr, type, stream) | |
65 | char *valaddr; | |
66 | struct type *type; | |
67 | FILE *stream; | |
68 | { | |
69 | struct type *domain; | |
70 | struct fn_field *f; | |
71 | int j; | |
72 | int len2; | |
73 | int offset; | |
74 | char *kind = ""; | |
75 | CORE_ADDR addr; | |
76 | struct symbol *sym; | |
77 | unsigned len; | |
78 | unsigned int i; | |
79 | ||
80 | domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)); | |
81 | addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr); | |
82 | if (METHOD_PTR_IS_VIRTUAL (addr)) | |
83 | { | |
84 | offset = METHOD_PTR_TO_VOFFSET (addr); | |
85 | len = TYPE_NFN_FIELDS (domain); | |
86 | for (i = 0; i < len; i++) | |
87 | { | |
88 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
89 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
90 | ||
91 | for (j = 0; j < len2; j++) | |
92 | { | |
93 | QUIT; | |
94 | if (TYPE_FN_FIELD_VOFFSET (f, j) == offset) | |
95 | { | |
96 | kind = "virtual "; | |
97 | goto common; | |
98 | } | |
99 | } | |
100 | } | |
101 | } | |
102 | else | |
103 | { | |
104 | sym = find_pc_function (addr); | |
105 | if (sym == 0) | |
106 | { | |
107 | error ("invalid pointer to member function"); | |
108 | } | |
109 | len = TYPE_NFN_FIELDS (domain); | |
110 | for (i = 0; i < len; i++) | |
111 | { | |
112 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
113 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
114 | ||
115 | for (j = 0; j < len2; j++) | |
116 | { | |
117 | QUIT; | |
118 | if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))) | |
119 | { | |
120 | goto common; | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | common: | |
126 | if (i < len) | |
127 | { | |
128 | fprintf_filtered (stream, "&"); | |
129 | c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0); | |
130 | fprintf (stream, kind); | |
131 | if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_' | |
132 | && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER) | |
133 | { | |
134 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~", | |
135 | TYPE_FN_FIELDLIST_NAME (domain, i), | |
136 | 0, stream); | |
137 | } | |
138 | else | |
139 | { | |
140 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "", | |
141 | TYPE_FN_FIELDLIST_NAME (domain, i), | |
142 | 0, stream); | |
143 | } | |
144 | } | |
145 | else | |
146 | { | |
147 | fprintf_filtered (stream, "("); | |
148 | type_print (type, "", stream, -1); | |
149 | fprintf_filtered (stream, ") %d", (int) addr >> 3); | |
150 | } | |
151 | } | |
152 | ||
a8a69e63 FF |
153 | /* Return truth value for assertion that TYPE is of the type |
154 | "pointer to virtual function". */ | |
155 | ||
156 | int | |
157 | cp_is_vtbl_ptr_type(type) | |
158 | struct type *type; | |
159 | { | |
160 | char *typename = type_name_no_tag (type); | |
161 | static const char vtbl_ptr_name[] = | |
162 | { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 }; | |
163 | ||
2e4964ad | 164 | return (typename != NULL && STREQ(typename, vtbl_ptr_name)); |
a8a69e63 FF |
165 | } |
166 | ||
167 | /* Return truth value for the assertion that TYPE is of the type | |
168 | "pointer to virtual function table". */ | |
169 | ||
170 | int | |
171 | cp_is_vtbl_member(type) | |
172 | struct type *type; | |
173 | { | |
174 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
175 | type = TYPE_TARGET_TYPE (type); | |
176 | else | |
177 | return 0; | |
178 | ||
179 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY | |
180 | && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT) | |
181 | /* Virtual functions tables are full of pointers to virtual functions. */ | |
182 | return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type)); | |
183 | return 0; | |
184 | } | |
185 | ||
186 | /* Mutually recursive subroutines of cplus_print_value and c_val_print to | |
187 | print out a structure's fields: cp_print_value_fields and cplus_print_value. | |
188 | ||
189 | TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the | |
190 | same meanings as in cplus_print_value and c_val_print. | |
191 | ||
192 | DONT_PRINT is an array of baseclass types that we | |
193 | should not print, or zero if called from top level. */ | |
194 | ||
195 | void | |
196 | cp_print_value_fields (type, valaddr, stream, format, recurse, pretty, | |
197 | dont_print) | |
198 | struct type *type; | |
199 | char *valaddr; | |
200 | FILE *stream; | |
201 | int format; | |
202 | int recurse; | |
203 | enum val_prettyprint pretty; | |
204 | struct type **dont_print; | |
205 | { | |
206 | int i, len, n_baseclasses; | |
207 | ||
208 | check_stub_type (type); | |
209 | ||
210 | fprintf_filtered (stream, "{"); | |
211 | len = TYPE_NFIELDS (type); | |
212 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
213 | ||
214 | /* Print out baseclasses such that we don't print | |
215 | duplicates of virtual baseclasses. */ | |
216 | if (n_baseclasses > 0) | |
217 | cplus_print_value (type, valaddr, stream, format, recurse+1, pretty, | |
218 | dont_print); | |
219 | ||
220 | if (!len && n_baseclasses == 1) | |
221 | fprintf_filtered (stream, "<No data fields>"); | |
222 | else | |
223 | { | |
224 | extern int inspect_it; | |
225 | int fields_seen = 0; | |
226 | ||
227 | for (i = n_baseclasses; i < len; i++) | |
228 | { | |
229 | /* Check if static field */ | |
230 | if (TYPE_FIELD_STATIC (type, i)) | |
231 | continue; | |
232 | if (fields_seen) | |
233 | fprintf_filtered (stream, ", "); | |
234 | else if (n_baseclasses > 0) | |
235 | { | |
236 | if (pretty) | |
237 | { | |
238 | fprintf_filtered (stream, "\n"); | |
239 | print_spaces_filtered (2 + 2 * recurse, stream); | |
240 | fputs_filtered ("members of ", stream); | |
241 | fputs_filtered (type_name_no_tag (type), stream); | |
242 | fputs_filtered (": ", stream); | |
243 | } | |
244 | } | |
245 | fields_seen = 1; | |
246 | ||
247 | if (pretty) | |
248 | { | |
249 | fprintf_filtered (stream, "\n"); | |
250 | print_spaces_filtered (2 + 2 * recurse, stream); | |
251 | } | |
252 | else | |
253 | { | |
254 | wrap_here (n_spaces (2 + 2 * recurse)); | |
255 | } | |
256 | if (inspect_it) | |
257 | { | |
258 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
259 | fputs_filtered ("\"( ptr \"", stream); | |
260 | else | |
261 | fputs_filtered ("\"( nodef \"", stream); | |
262 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
263 | fputs_filtered ("\" \"", stream); | |
264 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
265 | fputs_filtered ("\") \"", stream); | |
266 | } | |
267 | else | |
268 | { | |
269 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
270 | fputs_filtered (" = ", stream); | |
271 | } | |
272 | if (TYPE_FIELD_PACKED (type, i)) | |
273 | { | |
274 | value v; | |
275 | ||
276 | /* Bitfields require special handling, especially due to byte | |
277 | order problems. */ | |
278 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), | |
279 | unpack_field_as_long (type, valaddr, i)); | |
280 | ||
281 | c_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, | |
282 | stream, format, 0, recurse + 1, pretty); | |
283 | } | |
284 | else | |
285 | { | |
286 | c_val_print (TYPE_FIELD_TYPE (type, i), | |
287 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, | |
288 | 0, stream, format, 0, recurse + 1, pretty); | |
289 | } | |
290 | } | |
291 | if (pretty) | |
292 | { | |
293 | fprintf_filtered (stream, "\n"); | |
294 | print_spaces_filtered (2 * recurse, stream); | |
295 | } | |
296 | } | |
297 | fprintf_filtered (stream, "}"); | |
298 | } | |
299 | ||
300 | /* Special val_print routine to avoid printing multiple copies of virtual | |
301 | baseclasses. */ | |
302 | ||
303 | static void | |
304 | cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print) | |
305 | struct type *type; | |
306 | char *valaddr; | |
307 | FILE *stream; | |
308 | int format; | |
309 | int recurse; | |
310 | enum val_prettyprint pretty; | |
311 | struct type **dont_print; | |
312 | { | |
313 | struct obstack tmp_obstack; | |
314 | struct type **last_dont_print | |
315 | = (struct type **)obstack_next_free (&dont_print_obstack); | |
316 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
317 | ||
318 | if (dont_print == 0) | |
319 | { | |
320 | /* If we're at top level, carve out a completely fresh | |
321 | chunk of the obstack and use that until this particular | |
322 | invocation returns. */ | |
323 | tmp_obstack = dont_print_obstack; | |
324 | /* Bump up the high-water mark. Now alpha is omega. */ | |
325 | obstack_finish (&dont_print_obstack); | |
326 | } | |
327 | ||
328 | for (i = 0; i < n_baseclasses; i++) | |
329 | { | |
330 | char *baddr; | |
331 | int err; | |
332 | ||
333 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
334 | { | |
335 | struct type **first_dont_print | |
336 | = (struct type **)obstack_base (&dont_print_obstack); | |
337 | ||
338 | int j = (struct type **)obstack_next_free (&dont_print_obstack) | |
339 | - first_dont_print; | |
340 | ||
341 | while (--j >= 0) | |
342 | if (TYPE_BASECLASS (type, i) == first_dont_print[j]) | |
343 | goto flush_it; | |
344 | ||
345 | obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i)); | |
346 | } | |
347 | ||
348 | /* Fix to use baseclass_offset instead. FIXME */ | |
349 | baddr = baseclass_addr (type, i, valaddr, 0, &err); | |
350 | if (err == 0 && baddr == 0) | |
351 | error ("could not find virtual baseclass `%s'\n", | |
352 | type_name_no_tag (TYPE_BASECLASS (type, i))); | |
353 | ||
354 | if (pretty) | |
355 | { | |
356 | fprintf_filtered (stream, "\n"); | |
357 | print_spaces_filtered (2 * recurse, stream); | |
358 | } | |
359 | fputs_filtered ("<", stream); | |
360 | fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream); | |
361 | fputs_filtered ("> = ", stream); | |
362 | if (err != 0) | |
363 | fprintf_filtered (stream, "<invalid address 0x%x>", baddr); | |
364 | else | |
365 | cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format, | |
366 | recurse, pretty, | |
367 | (struct type **) obstack_base (&dont_print_obstack)); | |
368 | fputs_filtered (", ", stream); | |
369 | ||
370 | flush_it: | |
371 | ; | |
372 | } | |
373 | ||
374 | if (dont_print == 0) | |
375 | { | |
376 | /* Free the space used to deal with the printing | |
377 | of this type from top level. */ | |
378 | obstack_free (&dont_print_obstack, last_dont_print); | |
379 | /* Reset watermark so that we can continue protecting | |
380 | ourselves from whatever we were protecting ourselves. */ | |
381 | dont_print_obstack = tmp_obstack; | |
382 | } | |
383 | } | |
384 | ||
385 | void | |
386 | cp_print_class_member (valaddr, domain, stream, prefix) | |
387 | char *valaddr; | |
388 | struct type *domain; | |
389 | FILE *stream; | |
390 | char *prefix; | |
391 | { | |
392 | ||
393 | /* VAL is a byte offset into the structure type DOMAIN. | |
394 | Find the name of the field for that offset and | |
395 | print it. */ | |
396 | int extra = 0; | |
397 | int bits = 0; | |
398 | register unsigned int i; | |
399 | unsigned len = TYPE_NFIELDS (domain); | |
400 | /* @@ Make VAL into bit offset */ | |
401 | LONGEST val = unpack_long (builtin_type_int, valaddr) << 3; | |
402 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
403 | { | |
404 | int bitpos = TYPE_FIELD_BITPOS (domain, i); | |
405 | QUIT; | |
406 | if (val == bitpos) | |
407 | break; | |
408 | if (val < bitpos && i != 0) | |
409 | { | |
410 | /* Somehow pointing into a field. */ | |
411 | i -= 1; | |
412 | extra = (val - TYPE_FIELD_BITPOS (domain, i)); | |
413 | if (extra & 0x7) | |
414 | bits = 1; | |
415 | else | |
416 | extra >>= 3; | |
417 | break; | |
418 | } | |
419 | } | |
420 | if (i < len) | |
421 | { | |
422 | char *name; | |
423 | fprintf_filtered (stream, prefix); | |
424 | name = type_name_no_tag (domain); | |
425 | if (name) | |
426 | fputs_filtered (name, stream); | |
427 | else | |
428 | c_type_print_base (domain, stream, 0, 0); | |
429 | fprintf_filtered (stream, "::"); | |
430 | fputs_filtered (TYPE_FIELD_NAME (domain, i), stream); | |
431 | if (extra) | |
432 | fprintf_filtered (stream, " + %d bytes", extra); | |
433 | if (bits) | |
434 | fprintf_filtered (stream, " (offset in bits)"); | |
435 | } | |
436 | else | |
437 | fprintf_filtered (stream, "%d", val >> 3); | |
438 | } | |
439 | ||
440 | void | |
441 | _initialize_cp_valprint () | |
442 | { | |
443 | add_show_from_set | |
444 | (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint, | |
445 | "Set printing of C++ virtual function tables.", | |
446 | &setprintlist), | |
447 | &showprintlist); | |
448 | ||
449 | add_show_from_set | |
450 | (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint, | |
451 | "Set printing of object's derived type based on vtable info.", | |
452 | &setprintlist), | |
453 | &showprintlist); | |
454 | ||
455 | /* Give people the defaults which they are used to. */ | |
456 | objectprint = 0; | |
457 | vtblprint = 0; | |
458 | obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *)); | |
459 | } |