* valprint.c (val_print): Add new language parameter and use it
[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, 2008
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "gdb_obstack.h"
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"
35 #include "cp-abi.h"
36 #include "valprint.h"
37 #include "cp-support.h"
38 #include "language.h"
39
40 /* Controls printing of vtbl's */
41 int vtblprint;
42 static void
43 show_vtblprint (struct ui_file *file, int from_tty,
44 struct cmd_list_element *c, const char *value)
45 {
46 fprintf_filtered (file, _("\
47 Printing 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. */
53 int objectprint;
54 static void
55 show_objectprint (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c,
57 const char *value)
58 {
59 fprintf_filtered (file, _("\
60 Printing of object's derived type based on vtable info is %s.\n"),
61 value);
62 }
63
64 int static_field_print; /* Controls printing of static fields. */
65 static void
66 show_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
73
74 static struct obstack dont_print_vb_obstack;
75 static struct obstack dont_print_statmem_obstack;
76
77 extern void _initialize_cp_valprint (void);
78
79 static void cp_print_static_field (struct type *, struct value *,
80 struct ui_file *, int, int,
81 enum val_prettyprint);
82
83 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
84 int, CORE_ADDR, struct ui_file *, int, int,
85 enum val_prettyprint, struct type **);
86
87
88 /* GCC versions after 2.4.5 use this. */
89 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
90
91 /* Return truth value for assertion that TYPE is of the type
92 "pointer to virtual function". */
93
94 int
95 cp_is_vtbl_ptr_type (struct type *type)
96 {
97 char *typename = type_name_no_tag (type);
98
99 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
100 }
101
102 /* Return truth value for the assertion that TYPE is of the type
103 "pointer to virtual function table". */
104
105 int
106 cp_is_vtbl_member (struct type *type)
107 {
108 /* With older versions of g++, the vtbl field pointed to an array
109 of structures. Nowadays it points directly to the structure. */
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);
116 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
117 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
118 {
119 /* Virtual functions tables are full of pointers
120 to virtual functions. */
121 return cp_is_vtbl_ptr_type (type);
122 }
123 }
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 }
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.
141
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
151 void
152 cp_print_value_fields (struct type *type, struct type *real_type,
153 const gdb_byte *valaddr, int offset, CORE_ADDR address,
154 struct ui_file *stream, int format, int recurse,
155 enum val_prettyprint pretty,
156 struct type **dont_print_vb,int dont_print_statmem)
157 {
158 int i, len, n_baseclasses;
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,
173 format, recurse + 1, pretty, dont_print_vb);
174
175 /* Second, print out data fields */
176
177 /* If there are no data fields, skip this part */
178 if (len == n_baseclasses || !len)
179 fprintf_filtered (stream, "<No data fields>");
180 else
181 {
182 struct obstack tmp_obstack = dont_print_statmem_obstack;
183
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. */
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
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 }
218 else
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),
231 current_language->la_language,
232 DMGL_PARAMS | DMGL_ANSI);
233 fputs_filtered ("\" \"", stream);
234 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
235 current_language->la_language,
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),
246 current_language->la_language,
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 {
257 struct value *v;
258
259 /* Bitfields require special handling, especially due to byte
260 order problems. */
261 if (TYPE_FIELD_IGNORE (type, i))
262 {
263 fputs_filtered ("<optimized out or zero length>", stream);
264 }
265 else
266 {
267 v = value_from_longest
268 (TYPE_FIELD_TYPE (type, i),
269 unpack_field_as_long (type, valaddr + offset, i));
270
271 common_val_print (v, stream, format, 0, recurse + 1, pretty,
272 current_language);
273 }
274 }
275 else
276 {
277 if (TYPE_FIELD_IGNORE (type, i))
278 {
279 fputs_filtered ("<optimized out or zero length>", stream);
280 }
281 else if (TYPE_FIELD_STATIC (type, i))
282 {
283 struct value *v = value_static_field (type, i);
284 if (v == NULL)
285 fputs_filtered ("<optimized out>", stream);
286 else
287 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
288 stream, format, recurse + 1,
289 pretty);
290 }
291 else
292 {
293 val_print (TYPE_FIELD_TYPE (type, i),
294 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
295 address + TYPE_FIELD_BITPOS (type, i) / 8,
296 stream, format, 0, recurse + 1, pretty,
297 current_language);
298 }
299 }
300 annotate_field_end ();
301 }
302
303 if (dont_print_statmem == 0)
304 {
305 /* Free the space used to deal with the printing
306 of the members from top level. */
307 obstack_free (&dont_print_statmem_obstack, last_dont_print);
308 dont_print_statmem_obstack = tmp_obstack;
309 }
310
311 if (pretty)
312 {
313 fprintf_filtered (stream, "\n");
314 print_spaces_filtered (2 * recurse, stream);
315 }
316 } /* if there are data fields */
317
318 fprintf_filtered (stream, "}");
319 }
320
321 /* Special val_print routine to avoid printing multiple copies of virtual
322 baseclasses. */
323
324 static void
325 cp_print_value (struct type *type, struct type *real_type,
326 const gdb_byte *valaddr, int offset, CORE_ADDR address,
327 struct ui_file *stream, int format, int recurse,
328 enum val_prettyprint pretty, struct type **dont_print_vb)
329 {
330 struct type **last_dont_print
331 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
332 struct obstack tmp_obstack = dont_print_vb_obstack;
333 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
334 int thisoffset;
335 struct type *thistype;
336
337 if (dont_print_vb == 0)
338 {
339 /* If we're at top level, carve out a completely fresh
340 chunk of the obstack and use that until this particular
341 invocation returns. */
342 /* Bump up the high-water mark. Now alpha is omega. */
343 obstack_finish (&dont_print_vb_obstack);
344 }
345
346 for (i = 0; i < n_baseclasses; i++)
347 {
348 int boffset;
349 int skip;
350 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
351 char *basename = TYPE_NAME (baseclass);
352 const gdb_byte *base_valaddr;
353
354 if (BASETYPE_VIA_VIRTUAL (type, i))
355 {
356 struct type **first_dont_print
357 = (struct type **) obstack_base (&dont_print_vb_obstack);
358
359 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
360 - first_dont_print;
361
362 while (--j >= 0)
363 if (baseclass == first_dont_print[j])
364 goto flush_it;
365
366 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
367 }
368
369 thisoffset = offset;
370 thistype = real_type;
371
372 boffset = baseclass_offset (type, i, valaddr + offset, address);
373 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
374
375 if (BASETYPE_VIA_VIRTUAL (type, i))
376 {
377 /* The virtual base class pointer might have been
378 clobbered by the user program. Make sure that it
379 still points to a valid memory location. */
380
381 if (boffset != -1
382 && ((boffset + offset) < 0
383 || (boffset + offset) >= TYPE_LENGTH (type)))
384 {
385 /* FIXME (alloca): unsafe if baseclass is really really large. */
386 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
387 base_valaddr = buf;
388 if (target_read_memory (address + boffset, buf,
389 TYPE_LENGTH (baseclass)) != 0)
390 skip = 1;
391 address = address + boffset;
392 thisoffset = 0;
393 boffset = 0;
394 thistype = baseclass;
395 }
396 else
397 base_valaddr = valaddr;
398 }
399 else
400 base_valaddr = valaddr;
401
402 /* now do the printing */
403 if (pretty)
404 {
405 fprintf_filtered (stream, "\n");
406 print_spaces_filtered (2 * recurse, stream);
407 }
408 fputs_filtered ("<", stream);
409 /* Not sure what the best notation is in the case where there is no
410 baseclass name. */
411 fputs_filtered (basename ? basename : "", stream);
412 fputs_filtered ("> = ", stream);
413
414
415 if (skip >= 1)
416 fprintf_filtered (stream, "<invalid address>");
417 else
418 cp_print_value_fields (baseclass, thistype, base_valaddr,
419 thisoffset + boffset, address + boffset,
420 stream, format,
421 recurse, pretty,
422 ((struct type **)
423 obstack_base (&dont_print_vb_obstack)),
424 0);
425 fputs_filtered (", ", stream);
426
427 flush_it:
428 ;
429 }
430
431 if (dont_print_vb == 0)
432 {
433 /* Free the space used to deal with the printing
434 of this type from top level. */
435 obstack_free (&dont_print_vb_obstack, last_dont_print);
436 /* Reset watermark so that we can continue protecting
437 ourselves from whatever we were protecting ourselves. */
438 dont_print_vb_obstack = tmp_obstack;
439 }
440 }
441
442 /* Print value of a static member.
443 To avoid infinite recursion when printing a class that contains
444 a static instance of the class, we keep the addresses of all printed
445 static member classes in an obstack and refuse to print them more
446 than once.
447
448 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
449 have the same meanings as in c_val_print. */
450
451 static void
452 cp_print_static_field (struct type *type,
453 struct value *val,
454 struct ui_file *stream,
455 int format,
456 int recurse,
457 enum val_prettyprint pretty)
458 {
459 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
460 {
461 CORE_ADDR *first_dont_print;
462 int i;
463
464 first_dont_print
465 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
466 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
467 - first_dont_print;
468
469 while (--i >= 0)
470 {
471 if (VALUE_ADDRESS (val) == first_dont_print[i])
472 {
473 fputs_filtered ("<same as static member of an already"
474 " seen type>",
475 stream);
476 return;
477 }
478 }
479
480 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
481 sizeof (CORE_ADDR));
482
483 CHECK_TYPEDEF (type);
484 cp_print_value_fields (type, type, value_contents_all (val),
485 value_embedded_offset (val), VALUE_ADDRESS (val),
486 stream, format, recurse, pretty, NULL, 1);
487 return;
488 }
489 val_print (type, value_contents_all (val),
490 value_embedded_offset (val), VALUE_ADDRESS (val),
491 stream, format, 0, recurse, pretty, current_language);
492 }
493
494
495 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
496 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
497 field number. If OFFSET is not exactly at the start of some field, set
498 *DOMAIN to NULL. */
499
500 void
501 cp_find_class_member (struct type **domain_p, int *fieldno,
502 LONGEST offset)
503 {
504 struct type *domain;
505 unsigned int i;
506 unsigned len;
507
508 *domain_p = check_typedef (*domain_p);
509 domain = *domain_p;
510 len = TYPE_NFIELDS (domain);
511
512 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
513 {
514 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
515
516 QUIT;
517 if (offset == bitpos)
518 {
519 *fieldno = i;
520 return;
521 }
522 }
523
524 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
525 {
526 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
527 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
528
529 if (offset >= bitpos && offset < bitpos + bitsize)
530 {
531 *domain_p = TYPE_FIELD_TYPE (domain, i);
532 cp_find_class_member (domain_p, fieldno, offset - bitpos);
533 return;
534 }
535 }
536
537 *domain_p = NULL;
538 }
539
540 void
541 cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
542 struct ui_file *stream, char *prefix)
543 {
544 /* VAL is a byte offset into the structure type DOMAIN.
545 Find the name of the field for that offset and
546 print it. */
547 unsigned int fieldno;
548
549 LONGEST val = unpack_long (builtin_type_long, valaddr);
550
551 /* Pointers to data members are usually byte offsets into an object.
552 Because a data member can have offset zero, and a NULL pointer to
553 member must be distinct from any valid non-NULL pointer to
554 member, either the value is biased or the NULL value has a
555 special representation; both are permitted by ISO C++. HP aCC
556 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
557 and other compilers which use the Itanium ABI use -1 as the NULL
558 value. GDB only supports that last form; to add support for
559 another form, make this into a cp-abi hook. */
560
561 if (val == -1)
562 {
563 fprintf_filtered (stream, "NULL");
564 return;
565 }
566
567 cp_find_class_member (&domain, &fieldno, val << 3);
568
569 if (domain != NULL)
570 {
571 char *name;
572 fputs_filtered (prefix, stream);
573 name = type_name_no_tag (domain);
574 if (name)
575 fputs_filtered (name, stream);
576 else
577 c_type_print_base (domain, stream, 0, 0);
578 fprintf_filtered (stream, "::");
579 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
580 }
581 else
582 fprintf_filtered (stream, "%ld", (long) val);
583 }
584
585
586 void
587 _initialize_cp_valprint (void)
588 {
589 add_setshow_boolean_cmd ("static-members", class_support,
590 &static_field_print, _("\
591 Set printing of C++ static members."), _("\
592 Show printing of C++ static members."), NULL,
593 NULL,
594 show_static_field_print,
595 &setprintlist, &showprintlist);
596 /* Turn on printing of static fields. */
597 static_field_print = 1;
598
599 add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
600 Set printing of C++ virtual function tables."), _("\
601 Show printing of C++ virtual function tables."), NULL,
602 NULL,
603 show_vtblprint,
604 &setprintlist, &showprintlist);
605
606 add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
607 Set printing of object's derived type based on vtable info."), _("\
608 Show printing of object's derived type based on vtable info."), NULL,
609 NULL,
610 show_objectprint,
611 &setprintlist, &showprintlist);
612
613 /* Give people the defaults which they are used to. */
614 objectprint = 0;
615 vtblprint = 0;
616 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
617 obstack_specify_allocation (&dont_print_statmem_obstack,
618 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
619 xmalloc, xfree);
620 }
This page took 0.055429 seconds and 4 git commands to generate.