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