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