This commit was generated by cvs2svn to track changes on a CVS vendor
[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, 1994, 1995, 1996
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "obstack.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29 #include "demangle.h"
30 #include "annotate.h"
31 #include "gdb_string.h"
32 #include "c-lang.h"
33 #include "target.h"
34
35 int vtblprint; /* Controls printing of vtbl's */
36 int objectprint; /* Controls looking up an object's derived type
37 using what we find in its vtables. */
38 int static_field_print; /* Controls printing of static fields. */
39
40 static struct obstack dont_print_vb_obstack;
41 static struct obstack dont_print_statmem_obstack;
42
43 static void
44 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
45 enum val_prettyprint));
46
47 static void
48 cp_print_value PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
49 int, int, enum val_prettyprint, struct type **));
50
51 void
52 cp_print_class_method (valaddr, type, stream)
53 char *valaddr;
54 struct type *type;
55 GDB_FILE *stream;
56 {
57 struct type *domain;
58 struct fn_field *f = NULL;
59 int j = 0;
60 int len2;
61 int offset;
62 char *kind = "";
63 CORE_ADDR addr;
64 struct symbol *sym;
65 unsigned len;
66 unsigned int i;
67 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
68
69 domain = TYPE_DOMAIN_TYPE (target_type);
70 if (domain == (struct type *)NULL)
71 {
72 fprintf_filtered (stream, "<unknown>");
73 return;
74 }
75 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
76 if (METHOD_PTR_IS_VIRTUAL (addr))
77 {
78 offset = METHOD_PTR_TO_VOFFSET (addr);
79 len = TYPE_NFN_FIELDS (domain);
80 for (i = 0; i < len; i++)
81 {
82 f = TYPE_FN_FIELDLIST1 (domain, i);
83 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
84
85 for (j = 0; j < len2; j++)
86 {
87 QUIT;
88 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
89 {
90 if (TYPE_FN_FIELD_STUB (f, j))
91 check_stub_method (domain, i, j);
92 kind = "virtual ";
93 goto common;
94 }
95 }
96 }
97 }
98 else
99 {
100 sym = find_pc_function (addr);
101 if (sym == 0)
102 {
103 error ("invalid pointer to member function");
104 }
105 len = TYPE_NFN_FIELDS (domain);
106 for (i = 0; i < len; i++)
107 {
108 f = TYPE_FN_FIELDLIST1 (domain, i);
109 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
110
111 for (j = 0; j < len2; j++)
112 {
113 QUIT;
114 if (TYPE_FN_FIELD_STUB (f, j))
115 check_stub_method (domain, i, j);
116 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
117 {
118 goto common;
119 }
120 }
121 }
122 }
123 common:
124 if (i < len)
125 {
126 char *demangled_name;
127
128 fprintf_filtered (stream, "&");
129 fprintf_filtered (stream, kind);
130 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
131 DMGL_ANSI | DMGL_PARAMS);
132 if (demangled_name == NULL)
133 fprintf_filtered (stream, "<badly mangled name %s>",
134 TYPE_FN_FIELD_PHYSNAME (f, j));
135 else
136 {
137 fputs_filtered (demangled_name, stream);
138 free (demangled_name);
139 }
140 }
141 else
142 {
143 fprintf_filtered (stream, "(");
144 type_print (type, "", stream, -1);
145 fprintf_filtered (stream, ") %d", (int) addr >> 3);
146 }
147 }
148
149 /* This was what it was for gcc 2.4.5 and earlier. */
150 static const char vtbl_ptr_name_old[] =
151 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
152 /* It was changed to this after 2.4.5. */
153 const char vtbl_ptr_name[] =
154 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
155
156 /* Return truth value for assertion that TYPE is of the type
157 "pointer to virtual function". */
158
159 int
160 cp_is_vtbl_ptr_type(type)
161 struct type *type;
162 {
163 char *typename = type_name_no_tag (type);
164
165 return (typename != NULL
166 && (STREQ (typename, vtbl_ptr_name)
167 || STREQ (typename, vtbl_ptr_name_old)));
168 }
169
170 /* Return truth value for the assertion that TYPE is of the type
171 "pointer to virtual function table". */
172
173 int
174 cp_is_vtbl_member(type)
175 struct type *type;
176 {
177 if (TYPE_CODE (type) == TYPE_CODE_PTR)
178 {
179 type = TYPE_TARGET_TYPE (type);
180 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
181 {
182 type = TYPE_TARGET_TYPE (type);
183 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
184 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
185 {
186 /* Virtual functions tables are full of pointers
187 to virtual functions. */
188 return cp_is_vtbl_ptr_type (type);
189 }
190 }
191 }
192 return 0;
193 }
194
195 /* Mutually recursive subroutines of cp_print_value and c_val_print to
196 print out a structure's fields: cp_print_value_fields and cp_print_value.
197
198 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
199 same meanings as in cp_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, address, stream, format, recurse, pretty,
206 dont_print_vb, dont_print_statmem)
207 struct type *type;
208 char *valaddr;
209 CORE_ADDR address;
210 GDB_FILE *stream;
211 int format;
212 int recurse;
213 enum val_prettyprint pretty;
214 struct type **dont_print_vb;
215 int dont_print_statmem;
216 {
217 int i, len, n_baseclasses;
218 struct obstack tmp_obstack;
219 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
220
221 CHECK_TYPEDEF (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 cp_print_value (type, valaddr, address, stream,
231 format, recurse+1, pretty, dont_print_vb);
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 if (dont_print_statmem == 0)
241 {
242 /* If we're at top level, carve out a completely fresh
243 chunk of the obstack and use that until this particular
244 invocation returns. */
245 tmp_obstack = dont_print_statmem_obstack;
246 obstack_finish (&dont_print_statmem_obstack);
247 }
248
249 for (i = n_baseclasses; i < len; i++)
250 {
251 /* If requested, skip printing of static fields. */
252 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
253 continue;
254 if (fields_seen)
255 fprintf_filtered (stream, ", ");
256 else if (n_baseclasses > 0)
257 {
258 if (pretty)
259 {
260 fprintf_filtered (stream, "\n");
261 print_spaces_filtered (2 + 2 * recurse, stream);
262 fputs_filtered ("members of ", stream);
263 fputs_filtered (type_name_no_tag (type), stream);
264 fputs_filtered (": ", stream);
265 }
266 }
267 fields_seen = 1;
268
269 if (pretty)
270 {
271 fprintf_filtered (stream, "\n");
272 print_spaces_filtered (2 + 2 * recurse, stream);
273 }
274 else
275 {
276 wrap_here (n_spaces (2 + 2 * recurse));
277 }
278 if (inspect_it)
279 {
280 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
281 fputs_filtered ("\"( ptr \"", stream);
282 else
283 fputs_filtered ("\"( nodef \"", stream);
284 if (TYPE_FIELD_STATIC (type, i))
285 fputs_filtered ("static ", stream);
286 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
287 language_cplus,
288 DMGL_PARAMS | DMGL_ANSI);
289 fputs_filtered ("\" \"", stream);
290 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
291 language_cplus,
292 DMGL_PARAMS | DMGL_ANSI);
293 fputs_filtered ("\") \"", stream);
294 }
295 else
296 {
297 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
298
299 if (TYPE_FIELD_STATIC (type, i))
300 fputs_filtered ("static ", stream);
301 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
302 language_cplus,
303 DMGL_PARAMS | DMGL_ANSI);
304 annotate_field_name_end ();
305 fputs_filtered (" = ", stream);
306 annotate_field_value ();
307 }
308
309 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
310 {
311 value_ptr v;
312
313 /* Bitfields require special handling, especially due to byte
314 order problems. */
315 if (TYPE_FIELD_IGNORE (type, i))
316 {
317 fputs_filtered ("<optimized out or zero length>", stream);
318 }
319 else
320 {
321 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
322 unpack_field_as_long (type, valaddr, i));
323
324 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
325 stream, format, 0, recurse + 1, pretty);
326 }
327 }
328 else
329 {
330 if (TYPE_FIELD_IGNORE (type, i))
331 {
332 fputs_filtered ("<optimized out or zero length>", stream);
333 }
334 else if (TYPE_FIELD_STATIC (type, i))
335 {
336 value_ptr v = value_static_field (type, i);
337 if (v == NULL)
338 fputs_filtered ("<optimized out>", stream);
339 else
340 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
341 stream, format, recurse + 1,
342 pretty);
343 }
344 else
345 {
346 val_print (TYPE_FIELD_TYPE (type, i),
347 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
348 address + TYPE_FIELD_BITPOS (type, i) / 8,
349 stream, format, 0, recurse + 1, pretty);
350 }
351 }
352 annotate_field_end ();
353 }
354
355 if (dont_print_statmem == 0)
356 {
357 /* Free the space used to deal with the printing
358 of the members from top level. */
359 obstack_free (&dont_print_statmem_obstack, last_dont_print);
360 dont_print_statmem_obstack = tmp_obstack;
361 }
362
363 if (pretty)
364 {
365 fprintf_filtered (stream, "\n");
366 print_spaces_filtered (2 * recurse, stream);
367 }
368 }
369 fprintf_filtered (stream, "}");
370 }
371
372 /* Special val_print routine to avoid printing multiple copies of virtual
373 baseclasses. */
374
375 static void
376 cp_print_value (type, valaddr, address, stream, format, recurse, pretty,
377 dont_print_vb)
378 struct type *type;
379 char *valaddr;
380 CORE_ADDR address;
381 GDB_FILE *stream;
382 int format;
383 int recurse;
384 enum val_prettyprint pretty;
385 struct type **dont_print_vb;
386 {
387 struct obstack tmp_obstack;
388 struct type **last_dont_print
389 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
390 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
391
392 if (dont_print_vb == 0)
393 {
394 /* If we're at top level, carve out a completely fresh
395 chunk of the obstack and use that until this particular
396 invocation returns. */
397 tmp_obstack = dont_print_vb_obstack;
398 /* Bump up the high-water mark. Now alpha is omega. */
399 obstack_finish (&dont_print_vb_obstack);
400 }
401
402 for (i = 0; i < n_baseclasses; i++)
403 {
404 int boffset;
405 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
406 char *basename = TYPE_NAME (baseclass);
407 char *base_valaddr;
408
409 if (BASETYPE_VIA_VIRTUAL (type, i))
410 {
411 struct type **first_dont_print
412 = (struct type **)obstack_base (&dont_print_vb_obstack);
413
414 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
415 - first_dont_print;
416
417 while (--j >= 0)
418 if (baseclass == first_dont_print[j])
419 goto flush_it;
420
421 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
422 }
423
424 boffset = baseclass_offset (type, i , valaddr, address);
425
426 if (pretty)
427 {
428 fprintf_filtered (stream, "\n");
429 print_spaces_filtered (2 * recurse, stream);
430 }
431 fputs_filtered ("<", stream);
432 /* Not sure what the best notation is in the case where there is no
433 baseclass name. */
434 fputs_filtered (basename ? basename : "", stream);
435 fputs_filtered ("> = ", stream);
436
437 /* The virtual base class pointer might have been clobbered by the
438 user program. Make sure that it still points to a valid memory
439 location. */
440
441 if (boffset != -1 && (boffset < 0 || boffset >= TYPE_LENGTH (type)))
442 {
443 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
444 if (target_read_memory (address + boffset, base_valaddr,
445 TYPE_LENGTH (baseclass)) != 0)
446 boffset = -1;
447 }
448 else
449 base_valaddr = valaddr + boffset;
450
451 if (boffset == -1)
452 fprintf_filtered (stream, "<invalid address>");
453 else
454 cp_print_value_fields (baseclass, base_valaddr, address + boffset,
455 stream, format, recurse, pretty,
456 (struct type **) obstack_base (&dont_print_vb_obstack),
457 0);
458 fputs_filtered (", ", stream);
459
460 flush_it:
461 ;
462 }
463
464 if (dont_print_vb == 0)
465 {
466 /* Free the space used to deal with the printing
467 of this type from top level. */
468 obstack_free (&dont_print_vb_obstack, last_dont_print);
469 /* Reset watermark so that we can continue protecting
470 ourselves from whatever we were protecting ourselves. */
471 dont_print_vb_obstack = tmp_obstack;
472 }
473 }
474
475 /* Print value of a static member.
476 To avoid infinite recursion when printing a class that contains
477 a static instance of the class, we keep the addresses of all printed
478 static member classes in an obstack and refuse to print them more
479 than once.
480
481 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
482 have the same meanings as in c_val_print. */
483
484 static void
485 cp_print_static_field (type, val, stream, format, recurse, pretty)
486 struct type *type;
487 value_ptr val;
488 GDB_FILE *stream;
489 int format;
490 int recurse;
491 enum val_prettyprint pretty;
492 {
493 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
494 {
495 CORE_ADDR *first_dont_print;
496 int i;
497
498 first_dont_print
499 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
500 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
501 - first_dont_print;
502
503 while (--i >= 0)
504 {
505 if (VALUE_ADDRESS (val) == first_dont_print[i])
506 {
507 fputs_filtered ("<same as static member of an already seen type>",
508 stream);
509 return;
510 }
511 }
512
513 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
514 sizeof (CORE_ADDR));
515
516 CHECK_TYPEDEF (type);
517 cp_print_value_fields (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
518 stream, format, recurse, pretty, NULL, 1);
519 return;
520 }
521 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
522 stream, format, 0, recurse, pretty);
523 }
524
525 void
526 cp_print_class_member (valaddr, domain, stream, prefix)
527 char *valaddr;
528 struct type *domain;
529 GDB_FILE *stream;
530 char *prefix;
531 {
532
533 /* VAL is a byte offset into the structure type DOMAIN.
534 Find the name of the field for that offset and
535 print it. */
536 int extra = 0;
537 int bits = 0;
538 register unsigned int i;
539 unsigned len = TYPE_NFIELDS (domain);
540 /* @@ Make VAL into bit offset */
541 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
542 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
543 {
544 int bitpos = TYPE_FIELD_BITPOS (domain, i);
545 QUIT;
546 if (val == bitpos)
547 break;
548 if (val < bitpos && i != 0)
549 {
550 /* Somehow pointing into a field. */
551 i -= 1;
552 extra = (val - TYPE_FIELD_BITPOS (domain, i));
553 if (extra & 0x7)
554 bits = 1;
555 else
556 extra >>= 3;
557 break;
558 }
559 }
560 if (i < len)
561 {
562 char *name;
563 fprintf_filtered (stream, prefix);
564 name = type_name_no_tag (domain);
565 if (name)
566 fputs_filtered (name, stream);
567 else
568 c_type_print_base (domain, stream, 0, 0);
569 fprintf_filtered (stream, "::");
570 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
571 if (extra)
572 fprintf_filtered (stream, " + %d bytes", extra);
573 if (bits)
574 fprintf_filtered (stream, " (offset in bits)");
575 }
576 else
577 fprintf_filtered (stream, "%d", val >> 3);
578 }
579
580 void
581 _initialize_cp_valprint ()
582 {
583 add_show_from_set
584 (add_set_cmd ("static-members", class_support, var_boolean,
585 (char *)&static_field_print,
586 "Set printing of C++ static members.",
587 &setprintlist),
588 &showprintlist);
589 /* Turn on printing of static fields. */
590 static_field_print = 1;
591
592 add_show_from_set
593 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
594 "Set printing of C++ virtual function tables.",
595 &setprintlist),
596 &showprintlist);
597
598 add_show_from_set
599 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
600 "Set printing of object's derived type based on vtable info.",
601 &setprintlist),
602 &showprintlist);
603
604 /* Give people the defaults which they are used to. */
605 objectprint = 0;
606 vtblprint = 0;
607 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
608 obstack_specify_allocation (&dont_print_statmem_obstack,
609 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
610 xmalloc, free);
611 }
This page took 0.042059 seconds and 5 git commands to generate.