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