* ldlang.c (dprint_statement): Stop printing at end of list.
[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,
354 0, stream, format, 0, recurse + 1, pretty);
024f65b1 355 }
a8a69e63 356 }
1c95d7ab 357 annotate_field_end ();
a8a69e63 358 }
96f7edbd 359
a1a0d974
PS
360 if (dont_print_statmem == 0)
361 {
362 /* Free the space used to deal with the printing
363 of the members from top level. */
364 obstack_free (&dont_print_statmem_obstack, last_dont_print);
365 dont_print_statmem_obstack = tmp_obstack;
366 }
367
a8a69e63
FF
368 if (pretty)
369 {
370 fprintf_filtered (stream, "\n");
371 print_spaces_filtered (2 * recurse, stream);
372 }
373 }
374 fprintf_filtered (stream, "}");
375}
376
377/* Special val_print routine to avoid printing multiple copies of virtual
378 baseclasses. */
379
380static void
5e548861
PB
381cp_print_value (type, valaddr, address, stream, format, recurse, pretty,
382 dont_print_vb)
a8a69e63
FF
383 struct type *type;
384 char *valaddr;
5e548861 385 CORE_ADDR address;
199b2450 386 GDB_FILE *stream;
a8a69e63
FF
387 int format;
388 int recurse;
389 enum val_prettyprint pretty;
a1a0d974 390 struct type **dont_print_vb;
a8a69e63
FF
391{
392 struct obstack tmp_obstack;
393 struct type **last_dont_print
a1a0d974 394 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
a8a69e63
FF
395 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
396
a1a0d974 397 if (dont_print_vb == 0)
a8a69e63
FF
398 {
399 /* If we're at top level, carve out a completely fresh
400 chunk of the obstack and use that until this particular
401 invocation returns. */
a1a0d974 402 tmp_obstack = dont_print_vb_obstack;
a8a69e63 403 /* Bump up the high-water mark. Now alpha is omega. */
a1a0d974 404 obstack_finish (&dont_print_vb_obstack);
a8a69e63
FF
405 }
406
407 for (i = 0; i < n_baseclasses; i++)
408 {
5e548861
PB
409 int boffset;
410 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
411 char *basename = TYPE_NAME (baseclass);
a8a69e63
FF
412
413 if (BASETYPE_VIA_VIRTUAL (type, i))
414 {
415 struct type **first_dont_print
a1a0d974 416 = (struct type **)obstack_base (&dont_print_vb_obstack);
a8a69e63 417
a1a0d974 418 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
a8a69e63
FF
419 - first_dont_print;
420
421 while (--j >= 0)
5e548861 422 if (baseclass == first_dont_print[j])
a8a69e63
FF
423 goto flush_it;
424
5e548861 425 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
a8a69e63
FF
426 }
427
5e548861 428 boffset = baseclass_offset (type, i , valaddr, address);
a8a69e63
FF
429
430 if (pretty)
431 {
432 fprintf_filtered (stream, "\n");
433 print_spaces_filtered (2 * recurse, stream);
434 }
435 fputs_filtered ("<", stream);
1410f5f1
JK
436 /* Not sure what the best notation is in the case where there is no
437 baseclass name. */
438 fputs_filtered (basename ? basename : "", stream);
a8a69e63 439 fputs_filtered ("> = ", stream);
5e548861
PB
440 if (boffset == -1)
441 fprintf_filtered (stream, "<invalid address>");
a8a69e63 442 else
5e548861
PB
443 cp_print_value_fields (baseclass, valaddr + boffset, address + boffset,
444 stream, format, recurse, pretty,
a1a0d974
PS
445 (struct type **) obstack_base (&dont_print_vb_obstack),
446 0);
a8a69e63
FF
447 fputs_filtered (", ", stream);
448
449 flush_it:
450 ;
451 }
452
a1a0d974 453 if (dont_print_vb == 0)
a8a69e63
FF
454 {
455 /* Free the space used to deal with the printing
456 of this type from top level. */
a1a0d974 457 obstack_free (&dont_print_vb_obstack, last_dont_print);
a8a69e63
FF
458 /* Reset watermark so that we can continue protecting
459 ourselves from whatever we were protecting ourselves. */
a1a0d974
PS
460 dont_print_vb_obstack = tmp_obstack;
461 }
462}
463
464/* Print value of a static member.
465 To avoid infinite recursion when printing a class that contains
466 a static instance of the class, we keep the addresses of all printed
467 static member classes in an obstack and refuse to print them more
468 than once.
469
470 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
471 have the same meanings as in c_val_print. */
472
473static void
474cp_print_static_field (type, val, stream, format, recurse, pretty)
475 struct type *type;
476 value_ptr val;
477 GDB_FILE *stream;
478 int format;
479 int recurse;
480 enum val_prettyprint pretty;
481{
482 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
483 {
484 CORE_ADDR *first_dont_print;
485 int i;
486
487 first_dont_print
488 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
489 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
490 - first_dont_print;
491
492 while (--i >= 0)
493 {
494 if (VALUE_ADDRESS (val) == first_dont_print[i])
495 {
496 fputs_filtered ("<same as static member of an already seen type>",
497 stream);
498 return;
499 }
500 }
501
5579919f 502 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
a1a0d974
PS
503 sizeof (CORE_ADDR));
504
5e548861
PB
505 CHECK_TYPEDEF (type);
506 cp_print_value_fields (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
507 stream, format, recurse, pretty, NULL, 1);
a1a0d974 508 return;
a8a69e63 509 }
a1a0d974
PS
510 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
511 stream, format, 0, recurse, pretty);
a8a69e63
FF
512}
513
514void
515cp_print_class_member (valaddr, domain, stream, prefix)
516 char *valaddr;
517 struct type *domain;
199b2450 518 GDB_FILE *stream;
a8a69e63
FF
519 char *prefix;
520{
521
522 /* VAL is a byte offset into the structure type DOMAIN.
523 Find the name of the field for that offset and
524 print it. */
525 int extra = 0;
526 int bits = 0;
527 register unsigned int i;
528 unsigned len = TYPE_NFIELDS (domain);
529 /* @@ Make VAL into bit offset */
530 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
531 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
532 {
533 int bitpos = TYPE_FIELD_BITPOS (domain, i);
534 QUIT;
535 if (val == bitpos)
536 break;
537 if (val < bitpos && i != 0)
538 {
539 /* Somehow pointing into a field. */
540 i -= 1;
541 extra = (val - TYPE_FIELD_BITPOS (domain, i));
542 if (extra & 0x7)
543 bits = 1;
544 else
545 extra >>= 3;
546 break;
547 }
548 }
549 if (i < len)
550 {
551 char *name;
552 fprintf_filtered (stream, prefix);
553 name = type_name_no_tag (domain);
554 if (name)
555 fputs_filtered (name, stream);
556 else
557 c_type_print_base (domain, stream, 0, 0);
558 fprintf_filtered (stream, "::");
559 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
560 if (extra)
561 fprintf_filtered (stream, " + %d bytes", extra);
562 if (bits)
563 fprintf_filtered (stream, " (offset in bits)");
564 }
565 else
566 fprintf_filtered (stream, "%d", val >> 3);
567}
568
569void
570_initialize_cp_valprint ()
571{
4c664b8d
PS
572 add_show_from_set
573 (add_set_cmd ("static-members", class_support, var_boolean,
574 (char *)&static_field_print,
575 "Set printing of C++ static members.",
576 &setprintlist),
577 &showprintlist);
578 /* Turn on printing of static fields. */
579 static_field_print = 1;
580
a8a69e63
FF
581 add_show_from_set
582 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
583 "Set printing of C++ virtual function tables.",
584 &setprintlist),
585 &showprintlist);
586
587 add_show_from_set
588 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
589 "Set printing of object's derived type based on vtable info.",
590 &setprintlist),
591 &showprintlist);
592
593 /* Give people the defaults which they are used to. */
594 objectprint = 0;
595 vtblprint = 0;
a1a0d974
PS
596 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
597 obstack_specify_allocation (&dont_print_statmem_obstack,
598 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
599 xmalloc, free);
a8a69e63 600}
This page took 0.196807 seconds and 4 git commands to generate.