2007-11-07 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
CommitLineData
c906108c 1/* Support for printing C++ values for GDB, the GNU debugger.
a2bd3dcd 2
6aba47ca
DJ
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 2000, 2001, 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
04ea0df1 22#include "gdb_obstack.h"
c906108c
SS
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"
b9d652ac 34#include "cp-abi.h"
973177d3 35#include "valprint.h"
d3cbe7ef 36#include "cp-support.h"
cf309262 37#include "language.h"
c906108c 38
920d2a44
AC
39/* Controls printing of vtbl's */
40int vtblprint;
41static void
42show_vtblprint (struct ui_file *file, int from_tty,
43 struct cmd_list_element *c, const char *value)
44{
45 fprintf_filtered (file, _("\
46Printing of C++ virtual function tables is %s.\n"),
47 value);
48}
49
50/* Controls looking up an object's derived type using what we find in
51 its vtables. */
52int objectprint;
53static void
54show_objectprint (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c,
56 const char *value)
57{
58 fprintf_filtered (file, _("\
59Printing of object's derived type based on vtable info is %s.\n"),
60 value);
61}
62
c5aa993b 63int static_field_print; /* Controls printing of static fields. */
920d2a44
AC
64static void
65show_static_field_print (struct ui_file *file, int from_tty,
66 struct cmd_list_element *c, const char *value)
67{
68 fprintf_filtered (file, _("Printing of C++ static members is %s.\n"),
69 value);
70}
71
c906108c
SS
72
73static struct obstack dont_print_vb_obstack;
74static struct obstack dont_print_statmem_obstack;
75
a14ed312 76extern void _initialize_cp_valprint (void);
392a587b 77
6943961c 78static void cp_print_static_field (struct type *, struct value *,
d9fcf2fb
JM
79 struct ui_file *, int, int,
80 enum val_prettyprint);
c906108c 81
fc1a4b47 82static void cp_print_value (struct type *, struct type *, const gdb_byte *,
a2bd3dcd 83 int, CORE_ADDR, struct ui_file *, int, int,
d9fcf2fb 84 enum val_prettyprint, struct type **);
c906108c 85
c906108c 86
8343f86c 87/* GCC versions after 2.4.5 use this. */
2c63a960 88const char vtbl_ptr_name[] = "__vtbl_ptr_type";
c906108c 89
c906108c
SS
90/* Return truth value for assertion that TYPE is of the type
91 "pointer to virtual function". */
92
93int
fba45db2 94cp_is_vtbl_ptr_type (struct type *type)
c906108c
SS
95{
96 char *typename = type_name_no_tag (type);
97
8343f86c 98 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
c906108c
SS
99}
100
101/* Return truth value for the assertion that TYPE is of the type
102 "pointer to virtual function table". */
103
104int
fba45db2 105cp_is_vtbl_member (struct type *type)
c906108c 106{
0e5e3ea6
PS
107 /* With older versions of g++, the vtbl field pointed to an array
108 of structures. Nowadays it points directly to the structure. */
c906108c
SS
109 if (TYPE_CODE (type) == TYPE_CODE_PTR)
110 {
111 type = TYPE_TARGET_TYPE (type);
112 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
113 {
114 type = TYPE_TARGET_TYPE (type);
c5aa993b
JM
115 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
116 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
c906108c
SS
117 {
118 /* Virtual functions tables are full of pointers
c5aa993b 119 to virtual functions. */
c906108c
SS
120 return cp_is_vtbl_ptr_type (type);
121 }
122 }
0e5e3ea6
PS
123 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
124 {
125 return cp_is_vtbl_ptr_type (type);
126 }
127 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
128 {
129 /* The type name of the thunk pointer is NULL when using dwarf2.
130 We could test for a pointer to a function, but there is
131 no type info for the virtual table either, so it wont help. */
132 return cp_is_vtbl_ptr_type (type);
133 }
c906108c
SS
134 }
135 return 0;
136}
137
138/* Mutually recursive subroutines of cp_print_value and c_val_print to
139 print out a structure's fields: cp_print_value_fields and cp_print_value.
c5aa993b 140
c906108c
SS
141 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
142 same meanings as in cp_print_value and c_val_print.
143
144 2nd argument REAL_TYPE is used to carry over the type of the derived
145 class across the recursion to base classes.
146
147 DONT_PRINT is an array of baseclass types that we
148 should not print, or zero if called from top level. */
149
150void
a2bd3dcd 151cp_print_value_fields (struct type *type, struct type *real_type,
fc1a4b47 152 const gdb_byte *valaddr, int offset, CORE_ADDR address,
a2bd3dcd
AC
153 struct ui_file *stream, int format, int recurse,
154 enum val_prettyprint pretty,
155 struct type **dont_print_vb,int dont_print_statmem)
c906108c
SS
156{
157 int i, len, n_baseclasses;
c906108c
SS
158 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
159 int fields_seen = 0;
160
161 CHECK_TYPEDEF (type);
162
163 fprintf_filtered (stream, "{");
164 len = TYPE_NFIELDS (type);
165 n_baseclasses = TYPE_N_BASECLASSES (type);
166
167 /* First, print out baseclasses such that we don't print
168 duplicates of virtual baseclasses. */
169
170 if (n_baseclasses > 0)
171 cp_print_value (type, real_type, valaddr, offset, address, stream,
c5aa993b 172 format, recurse + 1, pretty, dont_print_vb);
c906108c
SS
173
174 /* Second, print out data fields */
175
086280be
UW
176 /* If there are no data fields, skip this part */
177 if (len == n_baseclasses || !len)
c906108c
SS
178 fprintf_filtered (stream, "<No data fields>");
179 else
180 {
c1b6e682
DJ
181 struct obstack tmp_obstack = dont_print_statmem_obstack;
182
c906108c
SS
183 if (dont_print_statmem == 0)
184 {
185 /* If we're at top level, carve out a completely fresh
186 chunk of the obstack and use that until this particular
187 invocation returns. */
c906108c
SS
188 obstack_finish (&dont_print_statmem_obstack);
189 }
190
191 for (i = n_baseclasses; i < len; i++)
192 {
193 /* If requested, skip printing of static fields. */
194 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
195 continue;
196
c906108c
SS
197 if (fields_seen)
198 fprintf_filtered (stream, ", ");
199 else if (n_baseclasses > 0)
200 {
201 if (pretty)
202 {
203 fprintf_filtered (stream, "\n");
204 print_spaces_filtered (2 + 2 * recurse, stream);
205 fputs_filtered ("members of ", stream);
206 fputs_filtered (type_name_no_tag (type), stream);
207 fputs_filtered (": ", stream);
208 }
209 }
210 fields_seen = 1;
211
212 if (pretty)
213 {
214 fprintf_filtered (stream, "\n");
215 print_spaces_filtered (2 + 2 * recurse, stream);
216 }
c5aa993b 217 else
c906108c
SS
218 {
219 wrap_here (n_spaces (2 + 2 * recurse));
220 }
221 if (inspect_it)
222 {
223 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
224 fputs_filtered ("\"( ptr \"", stream);
225 else
226 fputs_filtered ("\"( nodef \"", stream);
227 if (TYPE_FIELD_STATIC (type, i))
228 fputs_filtered ("static ", stream);
229 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
cf309262 230 current_language->la_language,
c906108c
SS
231 DMGL_PARAMS | DMGL_ANSI);
232 fputs_filtered ("\" \"", stream);
233 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
cf309262 234 current_language->la_language,
c906108c
SS
235 DMGL_PARAMS | DMGL_ANSI);
236 fputs_filtered ("\") \"", stream);
237 }
238 else
239 {
240 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
241
242 if (TYPE_FIELD_STATIC (type, i))
243 fputs_filtered ("static ", stream);
244 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
cf309262 245 current_language->la_language,
c906108c
SS
246 DMGL_PARAMS | DMGL_ANSI);
247 annotate_field_name_end ();
248 /* do not print leading '=' in case of anonymous unions */
249 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
250 fputs_filtered (" = ", stream);
251 annotate_field_value ();
252 }
253
254 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
255 {
6943961c 256 struct value *v;
c906108c
SS
257
258 /* Bitfields require special handling, especially due to byte
c5aa993b 259 order problems. */
c906108c
SS
260 if (TYPE_FIELD_IGNORE (type, i))
261 {
c5aa993b 262 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
263 }
264 else
265 {
2c63a960
JB
266 v = value_from_longest
267 (TYPE_FIELD_TYPE (type, i),
268 unpack_field_as_long (type, valaddr + offset, i));
c906108c 269
806048c6 270 common_val_print (v, stream, format, 0, recurse + 1, pretty);
c906108c
SS
271 }
272 }
273 else
274 {
275 if (TYPE_FIELD_IGNORE (type, i))
276 {
c5aa993b 277 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
278 }
279 else if (TYPE_FIELD_STATIC (type, i))
280 {
6943961c 281 struct value *v = value_static_field (type, i);
c906108c
SS
282 if (v == NULL)
283 fputs_filtered ("<optimized out>", stream);
284 else
285 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
286 stream, format, recurse + 1,
287 pretty);
288 }
289 else
290 {
c5aa993b 291 val_print (TYPE_FIELD_TYPE (type, i),
2c63a960 292 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
c5aa993b
JM
293 address + TYPE_FIELD_BITPOS (type, i) / 8,
294 stream, format, 0, recurse + 1, pretty);
c906108c
SS
295 }
296 }
297 annotate_field_end ();
298 }
299
300 if (dont_print_statmem == 0)
301 {
302 /* Free the space used to deal with the printing
303 of the members from top level. */
304 obstack_free (&dont_print_statmem_obstack, last_dont_print);
305 dont_print_statmem_obstack = tmp_obstack;
306 }
307
308 if (pretty)
309 {
310 fprintf_filtered (stream, "\n");
311 print_spaces_filtered (2 * recurse, stream);
312 }
c5aa993b 313 } /* if there are data fields */
c5aa993b 314
c906108c
SS
315 fprintf_filtered (stream, "}");
316}
317
318/* Special val_print routine to avoid printing multiple copies of virtual
319 baseclasses. */
320
321static void
a2bd3dcd 322cp_print_value (struct type *type, struct type *real_type,
fc1a4b47 323 const gdb_byte *valaddr, int offset, CORE_ADDR address,
a2bd3dcd
AC
324 struct ui_file *stream, int format, int recurse,
325 enum val_prettyprint pretty, struct type **dont_print_vb)
c906108c 326{
c906108c 327 struct type **last_dont_print
2c63a960 328 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 329 struct obstack tmp_obstack = dont_print_vb_obstack;
c906108c 330 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
b9d652ac
DJ
331 int thisoffset;
332 struct type *thistype;
c906108c
SS
333
334 if (dont_print_vb == 0)
335 {
336 /* If we're at top level, carve out a completely fresh
c5aa993b
JM
337 chunk of the obstack and use that until this particular
338 invocation returns. */
c906108c
SS
339 /* Bump up the high-water mark. Now alpha is omega. */
340 obstack_finish (&dont_print_vb_obstack);
341 }
342
343 for (i = 0; i < n_baseclasses; i++)
344 {
345 int boffset;
346 int skip;
347 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
348 char *basename = TYPE_NAME (baseclass);
fc1a4b47 349 const gdb_byte *base_valaddr;
c906108c
SS
350
351 if (BASETYPE_VIA_VIRTUAL (type, i))
352 {
353 struct type **first_dont_print
2c63a960 354 = (struct type **) obstack_base (&dont_print_vb_obstack);
c906108c 355
c5aa993b 356 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
2c63a960 357 - first_dont_print;
c906108c
SS
358
359 while (--j >= 0)
360 if (baseclass == first_dont_print[j])
361 goto flush_it;
362
363 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
364 }
365
b9d652ac
DJ
366 thisoffset = offset;
367 thistype = real_type;
086280be
UW
368
369 boffset = baseclass_offset (type, i, valaddr + offset, address);
370 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
371
372 if (BASETYPE_VIA_VIRTUAL (type, i))
c5aa993b 373 {
086280be
UW
374 /* The virtual base class pointer might have been
375 clobbered by the user program. Make sure that it
376 still points to a valid memory location. */
c906108c 377
086280be
UW
378 if (boffset != -1
379 && ((boffset + offset) < 0
380 || (boffset + offset) >= TYPE_LENGTH (type)))
c5aa993b 381 {
086280be
UW
382 /* FIXME (alloca): unsafe if baseclass is really really large. */
383 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
384 base_valaddr = buf;
385 if (target_read_memory (address + boffset, buf,
386 TYPE_LENGTH (baseclass)) != 0)
387 skip = 1;
388 address = address + boffset;
389 thisoffset = 0;
390 boffset = 0;
391 thistype = baseclass;
c5aa993b
JM
392 }
393 else
394 base_valaddr = valaddr;
c906108c 395 }
086280be
UW
396 else
397 base_valaddr = valaddr;
c906108c
SS
398
399 /* now do the printing */
400 if (pretty)
401 {
402 fprintf_filtered (stream, "\n");
403 print_spaces_filtered (2 * recurse, stream);
404 }
405 fputs_filtered ("<", stream);
406 /* Not sure what the best notation is in the case where there is no
c5aa993b 407 baseclass name. */
c906108c
SS
408 fputs_filtered (basename ? basename : "", stream);
409 fputs_filtered ("> = ", stream);
410
411
412 if (skip >= 1)
413 fprintf_filtered (stream, "<invalid address>");
414 else
b9d652ac 415 cp_print_value_fields (baseclass, thistype, base_valaddr,
9e14d721
DJ
416 thisoffset + boffset, address + boffset,
417 stream, format,
2c63a960
JB
418 recurse, pretty,
419 ((struct type **)
420 obstack_base (&dont_print_vb_obstack)),
c906108c
SS
421 0);
422 fputs_filtered (", ", stream);
423
424 flush_it:
425 ;
426 }
427
428 if (dont_print_vb == 0)
429 {
430 /* Free the space used to deal with the printing
c5aa993b 431 of this type from top level. */
c906108c
SS
432 obstack_free (&dont_print_vb_obstack, last_dont_print);
433 /* Reset watermark so that we can continue protecting
c5aa993b 434 ourselves from whatever we were protecting ourselves. */
c906108c
SS
435 dont_print_vb_obstack = tmp_obstack;
436 }
437}
438
439/* Print value of a static member.
440 To avoid infinite recursion when printing a class that contains
441 a static instance of the class, we keep the addresses of all printed
442 static member classes in an obstack and refuse to print them more
443 than once.
444
445 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
446 have the same meanings as in c_val_print. */
447
448static void
2c63a960 449cp_print_static_field (struct type *type,
6943961c 450 struct value *val,
2c63a960
JB
451 struct ui_file *stream,
452 int format,
453 int recurse,
454 enum val_prettyprint pretty)
c906108c
SS
455{
456 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
457 {
458 CORE_ADDR *first_dont_print;
459 int i;
460
461 first_dont_print
c5aa993b
JM
462 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
463 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
c906108c
SS
464 - first_dont_print;
465
466 while (--i >= 0)
467 {
468 if (VALUE_ADDRESS (val) == first_dont_print[i])
469 {
2c63a960
JB
470 fputs_filtered ("<same as static member of an already"
471 " seen type>",
c906108c
SS
472 stream);
473 return;
474 }
475 }
476
477 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
478 sizeof (CORE_ADDR));
479
480 CHECK_TYPEDEF (type);
46615f07 481 cp_print_value_fields (type, type, value_contents_all (val),
13c3b5f5 482 value_embedded_offset (val), VALUE_ADDRESS (val),
c906108c
SS
483 stream, format, recurse, pretty, NULL, 1);
484 return;
485 }
46615f07 486 val_print (type, value_contents_all (val),
13c3b5f5 487 value_embedded_offset (val), VALUE_ADDRESS (val),
c906108c
SS
488 stream, format, 0, recurse, pretty);
489}
490
0d5de010
DJ
491
492/* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
493 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
494 field number. If OFFSET is not exactly at the start of some field, set
495 *DOMAIN to NULL. */
496
497void
498cp_find_class_member (struct type **domain_p, int *fieldno,
499 LONGEST offset)
500{
501 struct type *domain;
502 unsigned int i;
503 unsigned len;
504
505 *domain_p = check_typedef (*domain_p);
506 domain = *domain_p;
507 len = TYPE_NFIELDS (domain);
508
509 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
510 {
511 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
512
513 QUIT;
514 if (offset == bitpos)
515 {
516 *fieldno = i;
517 return;
518 }
519 }
520
521 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
522 {
523 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
524 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
525
526 if (offset >= bitpos && offset < bitpos + bitsize)
527 {
528 *domain_p = TYPE_FIELD_TYPE (domain, i);
529 cp_find_class_member (domain_p, fieldno, offset - bitpos);
530 return;
531 }
532 }
533
534 *domain_p = NULL;
535}
536
c906108c 537void
fc1a4b47 538cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
fba45db2 539 struct ui_file *stream, char *prefix)
c906108c 540{
c906108c
SS
541 /* VAL is a byte offset into the structure type DOMAIN.
542 Find the name of the field for that offset and
543 print it. */
0d5de010 544 unsigned int fieldno;
c906108c 545
0d5de010 546 LONGEST val = unpack_long (builtin_type_long, valaddr);
c906108c 547
0d5de010
DJ
548 /* Pointers to data members are usually byte offsets into an object.
549 Because a data member can have offset zero, and a NULL pointer to
550 member must be distinct from any valid non-NULL pointer to
551 member, either the value is biased or the NULL value has a
552 special representation; both are permitted by ISO C++. HP aCC
553 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
554 and other compilers which use the Itanium ABI use -1 as the NULL
555 value. GDB only supports that last form; to add support for
556 another form, make this into a cp-abi hook. */
c906108c 557
0d5de010 558 if (val == -1)
c906108c 559 {
0d5de010
DJ
560 fprintf_filtered (stream, "NULL");
561 return;
c906108c 562 }
0d5de010
DJ
563
564 cp_find_class_member (&domain, &fieldno, val << 3);
565
566 if (domain != NULL)
c906108c
SS
567 {
568 char *name;
306d9ac5 569 fputs_filtered (prefix, stream);
c906108c
SS
570 name = type_name_no_tag (domain);
571 if (name)
c5aa993b 572 fputs_filtered (name, stream);
c906108c
SS
573 else
574 c_type_print_base (domain, stream, 0, 0);
575 fprintf_filtered (stream, "::");
0d5de010 576 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
c906108c
SS
577 }
578 else
0d5de010 579 fprintf_filtered (stream, "%ld", (long) val);
c906108c
SS
580}
581
582
c906108c 583void
fba45db2 584_initialize_cp_valprint (void)
c906108c 585{
5bf193a2
AC
586 add_setshow_boolean_cmd ("static-members", class_support,
587 &static_field_print, _("\
588Set printing of C++ static members."), _("\
589Show printing of C++ static members."), NULL,
590 NULL,
920d2a44 591 show_static_field_print,
5bf193a2 592 &setprintlist, &showprintlist);
c906108c
SS
593 /* Turn on printing of static fields. */
594 static_field_print = 1;
595
5bf193a2
AC
596 add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
597Set printing of C++ virtual function tables."), _("\
598Show printing of C++ virtual function tables."), NULL,
599 NULL,
920d2a44 600 show_vtblprint,
5bf193a2
AC
601 &setprintlist, &showprintlist);
602
603 add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
604Set printing of object's derived type based on vtable info."), _("\
605Show printing of object's derived type based on vtable info."), NULL,
606 NULL,
920d2a44 607 show_objectprint,
5bf193a2 608 &setprintlist, &showprintlist);
c906108c
SS
609
610 /* Give people the defaults which they are used to. */
611 objectprint = 0;
612 vtblprint = 0;
613 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
614 obstack_specify_allocation (&dont_print_statmem_obstack,
615 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
b8c9b27d 616 xmalloc, xfree);
c906108c 617}
This page took 0.64179 seconds and 4 git commands to generate.