* valprint.c (print_longest): Fix a syntax error in #ifdef
[deliverable/binutils-gdb.git] / gdb / cp-valprint.c
1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1994, 1995
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 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"
29 #include "demangle.h"
30 #include "annotate.h"
31
32 int vtblprint; /* Controls printing of vtbl's */
33 int objectprint; /* Controls looking up an object's derived type
34 using what we find in its vtables. */
35 static int static_field_print; /* Controls printing of static fields. */
36
37 static struct obstack dont_print_vb_obstack;
38 static struct obstack dont_print_statmem_obstack;
39
40 static void
41 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
42 enum val_prettyprint));
43
44 static void
45 cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
46 enum val_prettyprint, struct type **));
47
48 /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
49
50 extern void
51 c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
52
53 extern void
54 c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
55
56 extern void
57 cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
58 GDB_FILE *));
59
60 /* END-FIXME */
61
62 void
63 cp_print_class_method (valaddr, type, stream)
64 char *valaddr;
65 struct type *type;
66 GDB_FILE *stream;
67 {
68 struct type *domain;
69 struct fn_field *f = NULL;
70 int j = 0;
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
79 check_stub_type (TYPE_TARGET_TYPE (type));
80 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
81 if (domain == (struct type *)NULL)
82 {
83 fprintf_filtered (stream, "<unknown>");
84 return;
85 }
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;
123 if (TYPE_FN_FIELD_STUB (f, j))
124 check_stub_method (domain, i, j);
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);
137 fprintf_unfiltered (stream, kind);
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
160 /* This was what it was for gcc 2.4.5 and earlier. */
161 static 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. */
164 const char vtbl_ptr_name[] =
165 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
166
167 /* Return truth value for assertion that TYPE is of the type
168 "pointer to virtual function". */
169
170 int
171 cp_is_vtbl_ptr_type(type)
172 struct type *type;
173 {
174 char *typename = type_name_no_tag (type);
175
176 return (typename != NULL
177 && (STREQ (typename, vtbl_ptr_name)
178 || STREQ (typename, vtbl_ptr_name_old)));
179 }
180
181 /* Return truth value for the assertion that TYPE is of the type
182 "pointer to virtual function table". */
183
184 int
185 cp_is_vtbl_member(type)
186 struct type *type;
187 {
188 if (TYPE_CODE (type) == TYPE_CODE_PTR)
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 }
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
215 void
216 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
217 dont_print_vb, dont_print_statmem)
218 struct type *type;
219 char *valaddr;
220 GDB_FILE *stream;
221 int format;
222 int recurse;
223 enum val_prettyprint pretty;
224 struct type **dont_print_vb;
225 int dont_print_statmem;
226 {
227 int i, len, n_baseclasses;
228 struct obstack tmp_obstack;
229 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
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,
241 dont_print_vb);
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
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
259 for (i = n_baseclasses; i < len; i++)
260 {
261 /* If requested, skip printing of static fields. */
262 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
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);
294 if (TYPE_FIELD_STATIC (type, i))
295 fputs_filtered ("static ", stream);
296 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
297 language_cplus,
298 DMGL_PARAMS | DMGL_ANSI);
299 fputs_filtered ("\" \"", stream);
300 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
301 language_cplus,
302 DMGL_PARAMS | DMGL_ANSI);
303 fputs_filtered ("\") \"", stream);
304 }
305 else
306 {
307 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
308
309 if (TYPE_FIELD_STATIC (type, i))
310 fputs_filtered ("static ", stream);
311 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
312 language_cplus,
313 DMGL_PARAMS | DMGL_ANSI);
314 annotate_field_name_end ();
315 fputs_filtered (" = ", stream);
316 annotate_field_value ();
317 }
318
319 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
320 {
321 value_ptr v;
322
323 /* Bitfields require special handling, especially due to byte
324 order problems. */
325 if (TYPE_FIELD_IGNORE (type, i))
326 {
327 fputs_filtered ("<optimized out or zero length>", stream);
328 }
329 else
330 {
331 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
332 unpack_field_as_long (type, valaddr, i));
333
334 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
335 stream, format, 0, recurse + 1, pretty);
336 }
337 }
338 else
339 {
340 if (TYPE_FIELD_IGNORE (type, i))
341 {
342 fputs_filtered ("<optimized out or zero length>", stream);
343 }
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));
356 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
357 stream, format, recurse + 1,
358 pretty);
359 }
360 }
361 else
362 {
363 val_print (TYPE_FIELD_TYPE (type, i),
364 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
365 0, stream, format, 0, recurse + 1, pretty);
366 }
367 }
368 annotate_field_end ();
369 }
370
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
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
391 static void
392 cplus_print_value (type, valaddr, stream, format, recurse, pretty,
393 dont_print_vb)
394 struct type *type;
395 char *valaddr;
396 GDB_FILE *stream;
397 int format;
398 int recurse;
399 enum val_prettyprint pretty;
400 struct type **dont_print_vb;
401 {
402 struct obstack tmp_obstack;
403 struct type **last_dont_print
404 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
405 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
406
407 if (dont_print_vb == 0)
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. */
412 tmp_obstack = dont_print_vb_obstack;
413 /* Bump up the high-water mark. Now alpha is omega. */
414 obstack_finish (&dont_print_vb_obstack);
415 }
416
417 for (i = 0; i < n_baseclasses; i++)
418 {
419 /* FIXME-32x64--assumes that a target pointer can fit in a char *.
420 Fix it by nuking baseclass_addr. */
421 char *baddr;
422 int err;
423 char *basename;
424
425 check_stub_type (TYPE_BASECLASS (type, i));
426 basename = TYPE_NAME (TYPE_BASECLASS (type, i));
427
428 if (BASETYPE_VIA_VIRTUAL (type, i))
429 {
430 struct type **first_dont_print
431 = (struct type **)obstack_base (&dont_print_vb_obstack);
432
433 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
434 - first_dont_print;
435
436 while (--j >= 0)
437 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
438 goto flush_it;
439
440 obstack_ptr_grow (&dont_print_vb_obstack, TYPE_BASECLASS (type, i));
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)
446 error ("could not find virtual baseclass %s\n",
447 basename ? basename : "");
448
449 if (pretty)
450 {
451 fprintf_filtered (stream, "\n");
452 print_spaces_filtered (2 * recurse, stream);
453 }
454 fputs_filtered ("<", stream);
455 /* Not sure what the best notation is in the case where there is no
456 baseclass name. */
457 fputs_filtered (basename ? basename : "", stream);
458 fputs_filtered ("> = ", stream);
459 if (err != 0)
460 {
461 fprintf_filtered (stream, "<invalid address ");
462 print_address_numeric ((CORE_ADDR) baddr, 1, stream);
463 fprintf_filtered (stream, ">");
464 }
465 else
466 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
467 recurse, pretty,
468 (struct type **) obstack_base (&dont_print_vb_obstack),
469 0);
470 fputs_filtered (", ", stream);
471
472 flush_it:
473 ;
474 }
475
476 if (dont_print_vb == 0)
477 {
478 /* Free the space used to deal with the printing
479 of this type from top level. */
480 obstack_free (&dont_print_vb_obstack, last_dont_print);
481 /* Reset watermark so that we can continue protecting
482 ourselves from whatever we were protecting ourselves. */
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
496 static void
497 cp_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;
533 }
534 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
535 stream, format, 0, recurse, pretty);
536 }
537
538 void
539 cp_print_class_member (valaddr, domain, stream, prefix)
540 char *valaddr;
541 struct type *domain;
542 GDB_FILE *stream;
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
593 void
594 _initialize_cp_valprint ()
595 {
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
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;
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);
624 }
This page took 0.082949 seconds and 4 git commands to generate.