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