* config/arm/embed.mt (SIM_OBS, SIM): Remove.
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
1 /* Support for printing C++ values for GDB, the GNU debugger.
2
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.
5
6 This file is part of GDB.
7
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdb_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 #include "cp-abi.h"
35 #include "valprint.h"
36 #include "cp-support.h"
37 #include "language.h"
38
39 /* Controls printing of vtbl's */
40 int vtblprint;
41 static void
42 show_vtblprint (struct ui_file *file, int from_tty,
43 struct cmd_list_element *c, const char *value)
44 {
45 fprintf_filtered (file, _("\
46 Printing 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. */
52 int objectprint;
53 static void
54 show_objectprint (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c,
56 const char *value)
57 {
58 fprintf_filtered (file, _("\
59 Printing of object's derived type based on vtable info is %s.\n"),
60 value);
61 }
62
63 int static_field_print; /* Controls printing of static fields. */
64 static void
65 show_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
72
73 static struct obstack dont_print_vb_obstack;
74 static struct obstack dont_print_statmem_obstack;
75
76 extern void _initialize_cp_valprint (void);
77
78 static void cp_print_static_field (struct type *, struct value *,
79 struct ui_file *, int, int,
80 enum val_prettyprint);
81
82 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
83 int, CORE_ADDR, struct ui_file *, int, int,
84 enum val_prettyprint, struct type **);
85
86
87 /* GCC versions after 2.4.5 use this. */
88 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
89
90 /* Return truth value for assertion that TYPE is of the type
91 "pointer to virtual function". */
92
93 int
94 cp_is_vtbl_ptr_type (struct type *type)
95 {
96 char *typename = type_name_no_tag (type);
97
98 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
99 }
100
101 /* Return truth value for the assertion that TYPE is of the type
102 "pointer to virtual function table". */
103
104 int
105 cp_is_vtbl_member (struct type *type)
106 {
107 /* With older versions of g++, the vtbl field pointed to an array
108 of structures. Nowadays it points directly to the structure. */
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);
115 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
116 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
117 {
118 /* Virtual functions tables are full of pointers
119 to virtual functions. */
120 return cp_is_vtbl_ptr_type (type);
121 }
122 }
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 }
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.
140
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
150 void
151 cp_print_value_fields (struct type *type, struct type *real_type,
152 const gdb_byte *valaddr, int offset, CORE_ADDR address,
153 struct ui_file *stream, int format, int recurse,
154 enum val_prettyprint pretty,
155 struct type **dont_print_vb,int dont_print_statmem)
156 {
157 int i, len, n_baseclasses;
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,
172 format, recurse + 1, pretty, dont_print_vb);
173
174 /* Second, print out data fields */
175
176 /* If there are no data fields, skip this part */
177 if (len == n_baseclasses || !len)
178 fprintf_filtered (stream, "<No data fields>");
179 else
180 {
181 struct obstack tmp_obstack = dont_print_statmem_obstack;
182
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. */
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
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 }
217 else
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),
230 current_language->la_language,
231 DMGL_PARAMS | DMGL_ANSI);
232 fputs_filtered ("\" \"", stream);
233 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
234 current_language->la_language,
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),
245 current_language->la_language,
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 {
256 struct value *v;
257
258 /* Bitfields require special handling, especially due to byte
259 order problems. */
260 if (TYPE_FIELD_IGNORE (type, i))
261 {
262 fputs_filtered ("<optimized out or zero length>", stream);
263 }
264 else
265 {
266 v = value_from_longest
267 (TYPE_FIELD_TYPE (type, i),
268 unpack_field_as_long (type, valaddr + offset, i));
269
270 common_val_print (v, stream, format, 0, recurse + 1, pretty);
271 }
272 }
273 else
274 {
275 if (TYPE_FIELD_IGNORE (type, i))
276 {
277 fputs_filtered ("<optimized out or zero length>", stream);
278 }
279 else if (TYPE_FIELD_STATIC (type, i))
280 {
281 struct value *v = value_static_field (type, i);
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 {
291 val_print (TYPE_FIELD_TYPE (type, i),
292 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
293 address + TYPE_FIELD_BITPOS (type, i) / 8,
294 stream, format, 0, recurse + 1, pretty);
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 }
313 } /* if there are data fields */
314
315 fprintf_filtered (stream, "}");
316 }
317
318 /* Special val_print routine to avoid printing multiple copies of virtual
319 baseclasses. */
320
321 static void
322 cp_print_value (struct type *type, struct type *real_type,
323 const gdb_byte *valaddr, int offset, CORE_ADDR address,
324 struct ui_file *stream, int format, int recurse,
325 enum val_prettyprint pretty, struct type **dont_print_vb)
326 {
327 struct type **last_dont_print
328 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
329 struct obstack tmp_obstack = dont_print_vb_obstack;
330 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
331 int thisoffset;
332 struct type *thistype;
333
334 if (dont_print_vb == 0)
335 {
336 /* If we're at top level, carve out a completely fresh
337 chunk of the obstack and use that until this particular
338 invocation returns. */
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);
349 const gdb_byte *base_valaddr;
350
351 if (BASETYPE_VIA_VIRTUAL (type, i))
352 {
353 struct type **first_dont_print
354 = (struct type **) obstack_base (&dont_print_vb_obstack);
355
356 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
357 - first_dont_print;
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
366 thisoffset = offset;
367 thistype = real_type;
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))
373 {
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. */
377
378 if (boffset != -1
379 && ((boffset + offset) < 0
380 || (boffset + offset) >= TYPE_LENGTH (type)))
381 {
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;
392 }
393 else
394 base_valaddr = valaddr;
395 }
396 else
397 base_valaddr = valaddr;
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
407 baseclass name. */
408 fputs_filtered (basename ? basename : "", stream);
409 fputs_filtered ("> = ", stream);
410
411
412 if (skip >= 1)
413 fprintf_filtered (stream, "<invalid address>");
414 else
415 cp_print_value_fields (baseclass, thistype, base_valaddr,
416 thisoffset + boffset, address + boffset,
417 stream, format,
418 recurse, pretty,
419 ((struct type **)
420 obstack_base (&dont_print_vb_obstack)),
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
431 of this type from top level. */
432 obstack_free (&dont_print_vb_obstack, last_dont_print);
433 /* Reset watermark so that we can continue protecting
434 ourselves from whatever we were protecting ourselves. */
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
448 static void
449 cp_print_static_field (struct type *type,
450 struct value *val,
451 struct ui_file *stream,
452 int format,
453 int recurse,
454 enum val_prettyprint pretty)
455 {
456 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
457 {
458 CORE_ADDR *first_dont_print;
459 int i;
460
461 first_dont_print
462 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
463 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
464 - first_dont_print;
465
466 while (--i >= 0)
467 {
468 if (VALUE_ADDRESS (val) == first_dont_print[i])
469 {
470 fputs_filtered ("<same as static member of an already"
471 " seen type>",
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);
481 cp_print_value_fields (type, type, value_contents_all (val),
482 value_embedded_offset (val), VALUE_ADDRESS (val),
483 stream, format, recurse, pretty, NULL, 1);
484 return;
485 }
486 val_print (type, value_contents_all (val),
487 value_embedded_offset (val), VALUE_ADDRESS (val),
488 stream, format, 0, recurse, pretty);
489 }
490
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
497 void
498 cp_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
537 void
538 cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
539 struct ui_file *stream, char *prefix)
540 {
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. */
544 unsigned int fieldno;
545
546 LONGEST val = unpack_long (builtin_type_long, valaddr);
547
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. */
557
558 if (val == -1)
559 {
560 fprintf_filtered (stream, "NULL");
561 return;
562 }
563
564 cp_find_class_member (&domain, &fieldno, val << 3);
565
566 if (domain != NULL)
567 {
568 char *name;
569 fputs_filtered (prefix, stream);
570 name = type_name_no_tag (domain);
571 if (name)
572 fputs_filtered (name, stream);
573 else
574 c_type_print_base (domain, stream, 0, 0);
575 fprintf_filtered (stream, "::");
576 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
577 }
578 else
579 fprintf_filtered (stream, "%ld", (long) val);
580 }
581
582
583 void
584 _initialize_cp_valprint (void)
585 {
586 add_setshow_boolean_cmd ("static-members", class_support,
587 &static_field_print, _("\
588 Set printing of C++ static members."), _("\
589 Show printing of C++ static members."), NULL,
590 NULL,
591 show_static_field_print,
592 &setprintlist, &showprintlist);
593 /* Turn on printing of static fields. */
594 static_field_print = 1;
595
596 add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
597 Set printing of C++ virtual function tables."), _("\
598 Show printing of C++ virtual function tables."), NULL,
599 NULL,
600 show_vtblprint,
601 &setprintlist, &showprintlist);
602
603 add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
604 Set printing of object's derived type based on vtable info."), _("\
605 Show printing of object's derived type based on vtable info."), NULL,
606 NULL,
607 show_objectprint,
608 &setprintlist, &showprintlist);
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),
616 xmalloc, xfree);
617 }
This page took 0.047926 seconds and 4 git commands to generate.