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 | ||
63 | /* Return truth value for assertion that TYPE is of the type | |
64 | "pointer to virtual function". */ | |
65 | ||
66 | int | |
67 | cp_is_vtbl_ptr_type(type) | |
68 | struct type *type; | |
69 | { | |
70 | char *typename = type_name_no_tag (type); | |
71 | static const char vtbl_ptr_name[] = | |
72 | { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 }; | |
73 | ||
2e4964ad | 74 | return (typename != NULL && STREQ(typename, vtbl_ptr_name)); |
a8a69e63 FF |
75 | } |
76 | ||
77 | /* Return truth value for the assertion that TYPE is of the type | |
78 | "pointer to virtual function table". */ | |
79 | ||
80 | int | |
81 | cp_is_vtbl_member(type) | |
82 | struct type *type; | |
83 | { | |
84 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
85 | type = TYPE_TARGET_TYPE (type); | |
86 | else | |
87 | return 0; | |
88 | ||
89 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY | |
90 | && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT) | |
91 | /* Virtual functions tables are full of pointers to virtual functions. */ | |
92 | return cp_is_vtbl_ptr_type (TYPE_TARGET_TYPE (type)); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | /* Mutually recursive subroutines of cplus_print_value and c_val_print to | |
97 | print out a structure's fields: cp_print_value_fields and cplus_print_value. | |
98 | ||
99 | TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the | |
100 | same meanings as in cplus_print_value and c_val_print. | |
101 | ||
102 | DONT_PRINT is an array of baseclass types that we | |
103 | should not print, or zero if called from top level. */ | |
104 | ||
105 | void | |
106 | cp_print_value_fields (type, valaddr, stream, format, recurse, pretty, | |
107 | dont_print) | |
108 | struct type *type; | |
109 | char *valaddr; | |
110 | FILE *stream; | |
111 | int format; | |
112 | int recurse; | |
113 | enum val_prettyprint pretty; | |
114 | struct type **dont_print; | |
115 | { | |
116 | int i, len, n_baseclasses; | |
117 | ||
118 | check_stub_type (type); | |
119 | ||
120 | fprintf_filtered (stream, "{"); | |
121 | len = TYPE_NFIELDS (type); | |
122 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
123 | ||
124 | /* Print out baseclasses such that we don't print | |
125 | duplicates of virtual baseclasses. */ | |
126 | if (n_baseclasses > 0) | |
127 | cplus_print_value (type, valaddr, stream, format, recurse+1, pretty, | |
128 | dont_print); | |
129 | ||
130 | if (!len && n_baseclasses == 1) | |
131 | fprintf_filtered (stream, "<No data fields>"); | |
132 | else | |
133 | { | |
134 | extern int inspect_it; | |
135 | int fields_seen = 0; | |
136 | ||
137 | for (i = n_baseclasses; i < len; i++) | |
138 | { | |
139 | /* Check if static field */ | |
140 | if (TYPE_FIELD_STATIC (type, i)) | |
141 | continue; | |
142 | if (fields_seen) | |
143 | fprintf_filtered (stream, ", "); | |
144 | else if (n_baseclasses > 0) | |
145 | { | |
146 | if (pretty) | |
147 | { | |
148 | fprintf_filtered (stream, "\n"); | |
149 | print_spaces_filtered (2 + 2 * recurse, stream); | |
150 | fputs_filtered ("members of ", stream); | |
151 | fputs_filtered (type_name_no_tag (type), stream); | |
152 | fputs_filtered (": ", stream); | |
153 | } | |
154 | } | |
155 | fields_seen = 1; | |
156 | ||
157 | if (pretty) | |
158 | { | |
159 | fprintf_filtered (stream, "\n"); | |
160 | print_spaces_filtered (2 + 2 * recurse, stream); | |
161 | } | |
162 | else | |
163 | { | |
164 | wrap_here (n_spaces (2 + 2 * recurse)); | |
165 | } | |
166 | if (inspect_it) | |
167 | { | |
168 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
169 | fputs_filtered ("\"( ptr \"", stream); | |
170 | else | |
171 | fputs_filtered ("\"( nodef \"", stream); | |
172 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
173 | fputs_filtered ("\" \"", stream); | |
174 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
175 | fputs_filtered ("\") \"", stream); | |
176 | } | |
177 | else | |
178 | { | |
179 | fprint_symbol (stream, TYPE_FIELD_NAME (type, i)); | |
180 | fputs_filtered (" = ", stream); | |
181 | } | |
182 | if (TYPE_FIELD_PACKED (type, i)) | |
183 | { | |
184 | value v; | |
185 | ||
186 | /* Bitfields require special handling, especially due to byte | |
187 | order problems. */ | |
188 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), | |
189 | unpack_field_as_long (type, valaddr, i)); | |
190 | ||
191 | c_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, | |
192 | stream, format, 0, recurse + 1, pretty); | |
193 | } | |
194 | else | |
195 | { | |
196 | c_val_print (TYPE_FIELD_TYPE (type, i), | |
197 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, | |
198 | 0, stream, format, 0, recurse + 1, pretty); | |
199 | } | |
200 | } | |
201 | if (pretty) | |
202 | { | |
203 | fprintf_filtered (stream, "\n"); | |
204 | print_spaces_filtered (2 * recurse, stream); | |
205 | } | |
206 | } | |
207 | fprintf_filtered (stream, "}"); | |
208 | } | |
209 | ||
210 | /* Special val_print routine to avoid printing multiple copies of virtual | |
211 | baseclasses. */ | |
212 | ||
213 | static void | |
214 | cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print) | |
215 | struct type *type; | |
216 | char *valaddr; | |
217 | FILE *stream; | |
218 | int format; | |
219 | int recurse; | |
220 | enum val_prettyprint pretty; | |
221 | struct type **dont_print; | |
222 | { | |
223 | struct obstack tmp_obstack; | |
224 | struct type **last_dont_print | |
225 | = (struct type **)obstack_next_free (&dont_print_obstack); | |
226 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
227 | ||
228 | if (dont_print == 0) | |
229 | { | |
230 | /* If we're at top level, carve out a completely fresh | |
231 | chunk of the obstack and use that until this particular | |
232 | invocation returns. */ | |
233 | tmp_obstack = dont_print_obstack; | |
234 | /* Bump up the high-water mark. Now alpha is omega. */ | |
235 | obstack_finish (&dont_print_obstack); | |
236 | } | |
237 | ||
238 | for (i = 0; i < n_baseclasses; i++) | |
239 | { | |
240 | char *baddr; | |
241 | int err; | |
242 | ||
243 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
244 | { | |
245 | struct type **first_dont_print | |
246 | = (struct type **)obstack_base (&dont_print_obstack); | |
247 | ||
248 | int j = (struct type **)obstack_next_free (&dont_print_obstack) | |
249 | - first_dont_print; | |
250 | ||
251 | while (--j >= 0) | |
252 | if (TYPE_BASECLASS (type, i) == first_dont_print[j]) | |
253 | goto flush_it; | |
254 | ||
255 | obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i)); | |
256 | } | |
257 | ||
258 | /* Fix to use baseclass_offset instead. FIXME */ | |
259 | baddr = baseclass_addr (type, i, valaddr, 0, &err); | |
260 | if (err == 0 && baddr == 0) | |
261 | error ("could not find virtual baseclass `%s'\n", | |
262 | type_name_no_tag (TYPE_BASECLASS (type, i))); | |
263 | ||
264 | if (pretty) | |
265 | { | |
266 | fprintf_filtered (stream, "\n"); | |
267 | print_spaces_filtered (2 * recurse, stream); | |
268 | } | |
269 | fputs_filtered ("<", stream); | |
270 | fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream); | |
271 | fputs_filtered ("> = ", stream); | |
272 | if (err != 0) | |
273 | fprintf_filtered (stream, "<invalid address 0x%x>", baddr); | |
274 | else | |
275 | cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format, | |
276 | recurse, pretty, | |
277 | (struct type **) obstack_base (&dont_print_obstack)); | |
278 | fputs_filtered (", ", stream); | |
279 | ||
280 | flush_it: | |
281 | ; | |
282 | } | |
283 | ||
284 | if (dont_print == 0) | |
285 | { | |
286 | /* Free the space used to deal with the printing | |
287 | of this type from top level. */ | |
288 | obstack_free (&dont_print_obstack, last_dont_print); | |
289 | /* Reset watermark so that we can continue protecting | |
290 | ourselves from whatever we were protecting ourselves. */ | |
291 | dont_print_obstack = tmp_obstack; | |
292 | } | |
293 | } | |
294 | ||
295 | void | |
296 | cp_print_class_member (valaddr, domain, stream, prefix) | |
297 | char *valaddr; | |
298 | struct type *domain; | |
299 | FILE *stream; | |
300 | char *prefix; | |
301 | { | |
302 | ||
303 | /* VAL is a byte offset into the structure type DOMAIN. | |
304 | Find the name of the field for that offset and | |
305 | print it. */ | |
306 | int extra = 0; | |
307 | int bits = 0; | |
308 | register unsigned int i; | |
309 | unsigned len = TYPE_NFIELDS (domain); | |
310 | /* @@ Make VAL into bit offset */ | |
311 | LONGEST val = unpack_long (builtin_type_int, valaddr) << 3; | |
312 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
313 | { | |
314 | int bitpos = TYPE_FIELD_BITPOS (domain, i); | |
315 | QUIT; | |
316 | if (val == bitpos) | |
317 | break; | |
318 | if (val < bitpos && i != 0) | |
319 | { | |
320 | /* Somehow pointing into a field. */ | |
321 | i -= 1; | |
322 | extra = (val - TYPE_FIELD_BITPOS (domain, i)); | |
323 | if (extra & 0x7) | |
324 | bits = 1; | |
325 | else | |
326 | extra >>= 3; | |
327 | break; | |
328 | } | |
329 | } | |
330 | if (i < len) | |
331 | { | |
332 | char *name; | |
333 | fprintf_filtered (stream, prefix); | |
334 | name = type_name_no_tag (domain); | |
335 | if (name) | |
336 | fputs_filtered (name, stream); | |
337 | else | |
338 | c_type_print_base (domain, stream, 0, 0); | |
339 | fprintf_filtered (stream, "::"); | |
340 | fputs_filtered (TYPE_FIELD_NAME (domain, i), stream); | |
341 | if (extra) | |
342 | fprintf_filtered (stream, " + %d bytes", extra); | |
343 | if (bits) | |
344 | fprintf_filtered (stream, " (offset in bits)"); | |
345 | } | |
346 | else | |
347 | fprintf_filtered (stream, "%d", val >> 3); | |
348 | } | |
349 | ||
350 | void | |
351 | _initialize_cp_valprint () | |
352 | { | |
353 | add_show_from_set | |
354 | (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint, | |
355 | "Set printing of C++ virtual function tables.", | |
356 | &setprintlist), | |
357 | &showprintlist); | |
358 | ||
359 | add_show_from_set | |
360 | (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint, | |
361 | "Set printing of object's derived type based on vtable info.", | |
362 | &setprintlist), | |
363 | &showprintlist); | |
364 | ||
365 | /* Give people the defaults which they are used to. */ | |
366 | objectprint = 0; | |
367 | vtblprint = 0; | |
368 | obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *)); | |
369 | } |