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