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