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