1 /* Support for printing C++ values for GDB, the GNU debugger.
2 
3    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4    1997, 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22 
23 #include "defs.h"
24 #include "gdb_obstack.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "demangle.h"
32 #include "annotate.h"
33 #include "gdb_string.h"
34 #include "c-lang.h"
35 #include "target.h"
36 #include "cp-abi.h"
37 #include "valprint.h"
38 #include "cp-support.h"
39 #include "language.h"
40 
41 /* Controls printing of vtbl's */
42 int vtblprint;
43 static void
show_vtblprint(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)44 show_vtblprint (struct ui_file *file, int from_tty,
45 		struct cmd_list_element *c, const char *value)
46 {
47   fprintf_filtered (file, _("\
48 Printing of C++ virtual function tables is %s.\n"),
49 		    value);
50 }
51 
52 /* Controls looking up an object's derived type using what we find in
53    its vtables.  */
54 int objectprint;
55 static void
show_objectprint(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)56 show_objectprint (struct ui_file *file, int from_tty,
57 		  struct cmd_list_element *c,
58 		  const char *value)
59 {
60   fprintf_filtered (file, _("\
61 Printing of object's derived type based on vtable info is %s.\n"),
62 		    value);
63 }
64 
65 int static_field_print;		/* Controls printing of static fields. */
66 static void
show_static_field_print(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)67 show_static_field_print (struct ui_file *file, int from_tty,
68 			 struct cmd_list_element *c, const char *value)
69 {
70   fprintf_filtered (file, _("Printing of C++ static members is %s.\n"),
71 		    value);
72 }
73 
74 
75 static struct obstack dont_print_vb_obstack;
76 static struct obstack dont_print_statmem_obstack;
77 
78 extern void _initialize_cp_valprint (void);
79 
80 static void cp_print_static_field (struct type *, struct value *,
81 				   struct ui_file *, int, int,
82 				   enum val_prettyprint);
83 
84 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
85 			    int, CORE_ADDR, struct ui_file *, int, int,
86 			    enum val_prettyprint, struct type **);
87 
88 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
89 						  struct value *,
90 						  struct ui_file *, int,
91 						  int,
92 						  enum val_prettyprint);
93 
94 
95 void
cp_print_class_method(const gdb_byte * valaddr,struct type * type,struct ui_file * stream)96 cp_print_class_method (const gdb_byte *valaddr,
97 		       struct type *type,
98 		       struct ui_file *stream)
99 {
100   struct type *domain;
101   struct fn_field *f = NULL;
102   int j = 0;
103   int len2;
104   int offset;
105   char *kind = "";
106   CORE_ADDR addr;
107   struct symbol *sym;
108   unsigned len;
109   unsigned int i;
110   struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
111 
112   domain = TYPE_DOMAIN_TYPE (target_type);
113   if (domain == (struct type *) NULL)
114     {
115       fprintf_filtered (stream, "<unknown>");
116       return;
117     }
118   addr = unpack_pointer (type, valaddr);
119   if (METHOD_PTR_IS_VIRTUAL (addr))
120     {
121       offset = METHOD_PTR_TO_VOFFSET (addr);
122       len = TYPE_NFN_FIELDS (domain);
123       for (i = 0; i < len; i++)
124 	{
125 	  f = TYPE_FN_FIELDLIST1 (domain, i);
126 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
127 
128 	  check_stub_method_group (domain, i);
129 	  for (j = 0; j < len2; j++)
130 	    {
131 	      if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
132 		{
133 		  kind = "virtual ";
134 		  goto common;
135 		}
136 	    }
137 	}
138     }
139   else
140     {
141       sym = find_pc_function (addr);
142       if (sym == 0)
143 	{
144 	  /* 1997-08-01 Currently unsupported with HP aCC */
145 	  if (deprecated_hp_som_som_object_present)
146 	    {
147 	      fputs_filtered ("?? <not supported with HP aCC>", stream);
148 	      return;
149 	    }
150 	  error (_("invalid pointer to member function"));
151 	}
152       len = TYPE_NFN_FIELDS (domain);
153       for (i = 0; i < len; i++)
154 	{
155 	  f = TYPE_FN_FIELDLIST1 (domain, i);
156 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
157 
158 	  check_stub_method_group (domain, i);
159 	  for (j = 0; j < len2; j++)
160 	    {
161 	      if (strcmp (DEPRECATED_SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))
162 		  == 0)
163 		goto common;
164 	    }
165 	}
166     }
167  common:
168   if (i < len)
169     {
170       char *demangled_name;
171 
172       fprintf_filtered (stream, "&");
173       fputs_filtered (kind, stream);
174       demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
175 				       DMGL_ANSI | DMGL_PARAMS);
176       if (demangled_name == NULL)
177 	fprintf_filtered (stream, "<badly mangled name %s>",
178 			  TYPE_FN_FIELD_PHYSNAME (f, j));
179       else
180 	{
181 	  fputs_filtered (demangled_name, stream);
182 	  xfree (demangled_name);
183 	}
184     }
185   else
186     {
187       fprintf_filtered (stream, "(");
188       type_print (type, "", stream, -1);
189       fprintf_filtered (stream, ") %d", (int) addr >> 3);
190     }
191 }
192 
193 /* GCC versions after 2.4.5 use this.  */
194 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
195 
196 /* HP aCC uses different names.  */
197 const char hpacc_vtbl_ptr_name[] = "__vfp";
198 const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
199 
200 /* Return truth value for assertion that TYPE is of the type
201    "pointer to virtual function".  */
202 
203 int
cp_is_vtbl_ptr_type(struct type * type)204 cp_is_vtbl_ptr_type (struct type *type)
205 {
206   char *typename = type_name_no_tag (type);
207 
208   return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
209 }
210 
211 /* Return truth value for the assertion that TYPE is of the type
212    "pointer to virtual function table".  */
213 
214 int
cp_is_vtbl_member(struct type * type)215 cp_is_vtbl_member (struct type *type)
216 {
217   /* With older versions of g++, the vtbl field pointed to an array
218      of structures.  Nowadays it points directly to the structure. */
219   if (TYPE_CODE (type) == TYPE_CODE_PTR)
220     {
221       type = TYPE_TARGET_TYPE (type);
222       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
223 	{
224 	  type = TYPE_TARGET_TYPE (type);
225 	  if (TYPE_CODE (type) == TYPE_CODE_STRUCT	/* if not using thunks */
226 	      || TYPE_CODE (type) == TYPE_CODE_PTR)	/* if using thunks */
227 	    {
228 	      /* Virtual functions tables are full of pointers
229 	         to virtual functions. */
230 	      return cp_is_vtbl_ptr_type (type);
231 	    }
232 	}
233       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)  /* if not using thunks */
234 	{
235 	  return cp_is_vtbl_ptr_type (type);
236 	}
237       else if (TYPE_CODE (type) == TYPE_CODE_PTR)     /* if using thunks */
238 	{
239 	  /* The type name of the thunk pointer is NULL when using dwarf2.
240 	     We could test for a pointer to a function, but there is
241 	     no type info for the virtual table either, so it wont help.  */
242 	  return cp_is_vtbl_ptr_type (type);
243 	}
244     }
245   return 0;
246 }
247 
248 /* Mutually recursive subroutines of cp_print_value and c_val_print to
249    print out a structure's fields: cp_print_value_fields and cp_print_value.
250 
251    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
252    same meanings as in cp_print_value and c_val_print.
253 
254    2nd argument REAL_TYPE is used to carry over the type of the derived
255    class across the recursion to base classes.
256 
257    DONT_PRINT is an array of baseclass types that we
258    should not print, or zero if called from top level.  */
259 
260 void
cp_print_value_fields(struct type * type,struct type * real_type,const gdb_byte * valaddr,int offset,CORE_ADDR address,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,struct type ** dont_print_vb,int dont_print_statmem)261 cp_print_value_fields (struct type *type, struct type *real_type,
262 		       const gdb_byte *valaddr, int offset, CORE_ADDR address,
263 		       struct ui_file *stream, int format, int recurse,
264 		       enum val_prettyprint pretty,
265 		       struct type **dont_print_vb,int dont_print_statmem)
266 {
267   int i, len, n_baseclasses;
268   struct obstack tmp_obstack;
269   char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
270   int fields_seen = 0;
271 
272   CHECK_TYPEDEF (type);
273 
274   fprintf_filtered (stream, "{");
275   len = TYPE_NFIELDS (type);
276   n_baseclasses = TYPE_N_BASECLASSES (type);
277 
278   /* First, print out baseclasses such that we don't print
279      duplicates of virtual baseclasses.  */
280 
281   if (n_baseclasses > 0)
282     cp_print_value (type, real_type, valaddr, offset, address, stream,
283 		    format, recurse + 1, pretty, dont_print_vb);
284 
285   /* Second, print out data fields */
286 
287   /* If there are no data fields, or if the only field is the
288    * vtbl pointer, skip this part */
289   if ((len == n_baseclasses)
290       || ((len - n_baseclasses == 1)
291 	  && TYPE_HAS_VTABLE (type)
292 	  && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
293 		      hpacc_vtbl_ptr_name, 5) == 0)
294       || !len)
295     fprintf_filtered (stream, "<No data fields>");
296   else
297     {
298       if (dont_print_statmem == 0)
299 	{
300 	  /* If we're at top level, carve out a completely fresh
301 	     chunk of the obstack and use that until this particular
302 	     invocation returns.  */
303 	  tmp_obstack = dont_print_statmem_obstack;
304 	  obstack_finish (&dont_print_statmem_obstack);
305 	}
306 
307       for (i = n_baseclasses; i < len; i++)
308 	{
309 	  /* If requested, skip printing of static fields.  */
310 	  if (!static_field_print && TYPE_FIELD_STATIC (type, i))
311 	    continue;
312 
313 	  /* If a vtable pointer appears, we'll print it out later */
314 	  if (TYPE_HAS_VTABLE (type)
315 	      && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
316 			  5) == 0)
317 	    continue;
318 
319 	  if (fields_seen)
320 	    fprintf_filtered (stream, ", ");
321 	  else if (n_baseclasses > 0)
322 	    {
323 	      if (pretty)
324 		{
325 		  fprintf_filtered (stream, "\n");
326 		  print_spaces_filtered (2 + 2 * recurse, stream);
327 		  fputs_filtered ("members of ", stream);
328 		  fputs_filtered (type_name_no_tag (type), stream);
329 		  fputs_filtered (": ", stream);
330 		}
331 	    }
332 	  fields_seen = 1;
333 
334 	  if (pretty)
335 	    {
336 	      fprintf_filtered (stream, "\n");
337 	      print_spaces_filtered (2 + 2 * recurse, stream);
338 	    }
339 	  else
340 	    {
341 	      wrap_here (n_spaces (2 + 2 * recurse));
342 	    }
343 	  if (inspect_it)
344 	    {
345 	      if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
346 		fputs_filtered ("\"( ptr \"", stream);
347 	      else
348 		fputs_filtered ("\"( nodef \"", stream);
349 	      if (TYPE_FIELD_STATIC (type, i))
350 		fputs_filtered ("static ", stream);
351 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
352 				       current_language->la_language,
353 				       DMGL_PARAMS | DMGL_ANSI);
354 	      fputs_filtered ("\" \"", stream);
355 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
356 				       current_language->la_language,
357 				       DMGL_PARAMS | DMGL_ANSI);
358 	      fputs_filtered ("\") \"", stream);
359 	    }
360 	  else
361 	    {
362 	      annotate_field_begin (TYPE_FIELD_TYPE (type, i));
363 
364 	      if (TYPE_FIELD_STATIC (type, i))
365 		fputs_filtered ("static ", stream);
366 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
367 				       current_language->la_language,
368 				       DMGL_PARAMS | DMGL_ANSI);
369 	      annotate_field_name_end ();
370 	      /* do not print leading '=' in case of anonymous unions */
371 	      if (strcmp (TYPE_FIELD_NAME (type, i), ""))
372 		fputs_filtered (" = ", stream);
373 	      annotate_field_value ();
374 	    }
375 
376 	  if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
377 	    {
378 	      struct value *v;
379 
380 	      /* Bitfields require special handling, especially due to byte
381 	         order problems.  */
382 	      if (TYPE_FIELD_IGNORE (type, i))
383 		{
384 		  fputs_filtered ("<optimized out or zero length>", stream);
385 		}
386 	      else
387 		{
388 		  v = value_from_longest
389 		    (TYPE_FIELD_TYPE (type, i),
390 		     unpack_field_as_long (type, valaddr + offset, i));
391 
392 		  common_val_print (v, stream, format, 0, recurse + 1, pretty);
393 		}
394 	    }
395 	  else
396 	    {
397 	      if (TYPE_FIELD_IGNORE (type, i))
398 		{
399 		  fputs_filtered ("<optimized out or zero length>", stream);
400 		}
401 	      else if (TYPE_FIELD_STATIC (type, i))
402 		{
403 		  struct value *v = value_static_field (type, i);
404 		  if (v == NULL)
405 		    fputs_filtered ("<optimized out>", stream);
406 		  else
407 		    cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
408 					   stream, format, recurse + 1,
409 					   pretty);
410 		}
411 	      else
412 		{
413 		  val_print (TYPE_FIELD_TYPE (type, i),
414 			     valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
415 			     address + TYPE_FIELD_BITPOS (type, i) / 8,
416 			     stream, format, 0, recurse + 1, pretty);
417 		}
418 	    }
419 	  annotate_field_end ();
420 	}
421 
422       if (dont_print_statmem == 0)
423 	{
424 	  /* Free the space used to deal with the printing
425 	     of the members from top level.  */
426 	  obstack_free (&dont_print_statmem_obstack, last_dont_print);
427 	  dont_print_statmem_obstack = tmp_obstack;
428 	}
429 
430       if (pretty)
431 	{
432 	  fprintf_filtered (stream, "\n");
433 	  print_spaces_filtered (2 * recurse, stream);
434 	}
435     }				/* if there are data fields */
436   /* Now print out the virtual table pointer if there is one */
437   if (TYPE_HAS_VTABLE (type)
438       && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
439 		  hpacc_vtbl_ptr_name, 5) == 0)
440     {
441       struct value *v;
442       /* First get the virtual table pointer and print it out */
443 
444 #if 0
445       fputs_filtered ("__vfp = ", stream);
446 #endif
447 
448       fputs_filtered (", Virtual table at ", stream);
449 
450       /* pai: FIXME 32x64 problem? */
451       /* Not sure what the best notation is in the case where there is no
452          baseclass name.  */
453       v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
454 			      *(unsigned long *) (valaddr + offset));
455 
456       common_val_print (v, stream, format, 0, recurse + 1, pretty);
457       fields_seen = 1;
458 
459       if (vtblprint)
460 	{
461 	  /* Print out function pointers in vtable. */
462 
463 	  /* FIXME: then-clause is for non-RRBC layout of virtual
464 	   * table.  The RRBC case in the else-clause is yet to be
465 	   * implemented.  The if (1) below should be changed to a
466 	   * test for whether the executable we have was compiled
467 	   * with a version of HP aCC that doesn't have RRBC
468 	   * support. */
469 
470 	  if (1)
471 	    {
472 	      /* no RRBC support; function pointers embedded directly
473                  in vtable */
474 
475 	      int vfuncs = count_virtual_fns (real_type);
476 
477 	      fputs_filtered (" {", stream);
478 
479 	      /* FIXME : doesn't work at present */
480 #if 0
481 	      fprintf_filtered (stream, "%d entr%s: ", vfuncs,
482 				vfuncs == 1 ? "y" : "ies");
483 #else
484 	      fputs_filtered ("not implemented", stream);
485 
486 
487 #endif
488 
489 	      /* recursive function that prints all virtual function entries */
490 #if 0
491 	      cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
492 						    stream, format, recurse,
493 						    pretty);
494 #endif
495 	      fputs_filtered ("}", stream);
496 	    }			/* non-RRBC case */
497 	  else
498 	    {
499 	      /* FIXME -- see comments above */
500 	      /* RRBC support present; function pointers are found
501 	       * by indirection through the class segment entries. */
502 
503 
504 	    }			/* RRBC case */
505 	}			/* if vtblprint */
506 
507       if (pretty)
508 	{
509 	  fprintf_filtered (stream, "\n");
510 	  print_spaces_filtered (2 * recurse, stream);
511 	}
512 
513     }				/* if vtable exists */
514 
515   fprintf_filtered (stream, "}");
516 }
517 
518 /* Special val_print routine to avoid printing multiple copies of virtual
519    baseclasses.  */
520 
521 static void
cp_print_value(struct type * type,struct type * real_type,const gdb_byte * valaddr,int offset,CORE_ADDR address,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,struct type ** dont_print_vb)522 cp_print_value (struct type *type, struct type *real_type,
523 		const gdb_byte *valaddr, int offset, CORE_ADDR address,
524 		struct ui_file *stream, int format, int recurse,
525 		enum val_prettyprint pretty, struct type **dont_print_vb)
526 {
527   struct obstack tmp_obstack;
528   struct type **last_dont_print
529     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
530   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
531   int thisoffset;
532   struct type *thistype;
533 
534   if (dont_print_vb == 0)
535     {
536       /* If we're at top level, carve out a completely fresh
537          chunk of the obstack and use that until this particular
538          invocation returns.  */
539       tmp_obstack = dont_print_vb_obstack;
540       /* Bump up the high-water mark.  Now alpha is omega.  */
541       obstack_finish (&dont_print_vb_obstack);
542     }
543 
544   for (i = 0; i < n_baseclasses; i++)
545     {
546       int boffset;
547       int skip;
548       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
549       char *basename = TYPE_NAME (baseclass);
550       const gdb_byte *base_valaddr;
551 
552       if (BASETYPE_VIA_VIRTUAL (type, i))
553 	{
554 	  struct type **first_dont_print
555 	    = (struct type **) obstack_base (&dont_print_vb_obstack);
556 
557 	  int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
558 	    - first_dont_print;
559 
560 	  while (--j >= 0)
561 	    if (baseclass == first_dont_print[j])
562 	      goto flush_it;
563 
564 	  obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
565 	}
566 
567       thisoffset = offset;
568       thistype = real_type;
569       if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
570 	{
571 	  /* Assume HP/Taligent runtime convention */
572 	  find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
573 				valaddr, offset, &boffset, &skip);
574 	  if (skip >= 0)
575 	    error (_("Virtual base class offset not found from vtable while"
576 		   " printing"));
577 	  base_valaddr = valaddr;
578 	}
579       else
580 	{
581 	  boffset = baseclass_offset (type, i,
582 				      valaddr + offset,
583 				      address);
584 	  skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
585 
586 	  if (BASETYPE_VIA_VIRTUAL (type, i))
587 	    {
588 	      /* The virtual base class pointer might have been
589 	         clobbered by the user program. Make sure that it
590 	         still points to a valid memory location.  */
591 
592 	      if (boffset != -1
593 		  && ((boffset + offset) < 0
594 		      || (boffset + offset) >= TYPE_LENGTH (type)))
595 		{
596 		  /* FIXME (alloca): unsafe if baseclass is really really large. */
597 		  gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
598 		  base_valaddr = buf;
599 		  if (target_read_memory (address + boffset, buf,
600 					  TYPE_LENGTH (baseclass)) != 0)
601 		    skip = 1;
602 		  address = address + boffset;
603 		  thisoffset = 0;
604 		  boffset = 0;
605 		  thistype = baseclass;
606 		}
607 	      else
608 		base_valaddr = valaddr;
609 	    }
610 	  else
611 	    base_valaddr = valaddr;
612 	}
613 
614       /* now do the printing */
615       if (pretty)
616 	{
617 	  fprintf_filtered (stream, "\n");
618 	  print_spaces_filtered (2 * recurse, stream);
619 	}
620       fputs_filtered ("<", stream);
621       /* Not sure what the best notation is in the case where there is no
622          baseclass name.  */
623       fputs_filtered (basename ? basename : "", stream);
624       fputs_filtered ("> = ", stream);
625 
626 
627       if (skip >= 1)
628 	fprintf_filtered (stream, "<invalid address>");
629       else
630 	cp_print_value_fields (baseclass, thistype, base_valaddr,
631 			       thisoffset + boffset, address + boffset,
632 			       stream, format,
633 			       recurse, pretty,
634 			       ((struct type **)
635 				obstack_base (&dont_print_vb_obstack)),
636 			       0);
637       fputs_filtered (", ", stream);
638 
639     flush_it:
640       ;
641     }
642 
643   if (dont_print_vb == 0)
644     {
645       /* Free the space used to deal with the printing
646          of this type from top level.  */
647       obstack_free (&dont_print_vb_obstack, last_dont_print);
648       /* Reset watermark so that we can continue protecting
649          ourselves from whatever we were protecting ourselves.  */
650       dont_print_vb_obstack = tmp_obstack;
651     }
652 }
653 
654 /* Print value of a static member.
655    To avoid infinite recursion when printing a class that contains
656    a static instance of the class, we keep the addresses of all printed
657    static member classes in an obstack and refuse to print them more
658    than once.
659 
660    VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
661    have the same meanings as in c_val_print.  */
662 
663 static void
cp_print_static_field(struct type * type,struct value * val,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)664 cp_print_static_field (struct type *type,
665 		       struct value *val,
666 		       struct ui_file *stream,
667 		       int format,
668 		       int recurse,
669 		       enum val_prettyprint pretty)
670 {
671   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
672     {
673       CORE_ADDR *first_dont_print;
674       int i;
675 
676       first_dont_print
677 	= (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
678       i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
679 	- first_dont_print;
680 
681       while (--i >= 0)
682 	{
683 	  if (VALUE_ADDRESS (val) == first_dont_print[i])
684 	    {
685 	      fputs_filtered ("<same as static member of an already"
686 			      " seen type>",
687 			      stream);
688 	      return;
689 	    }
690 	}
691 
692       obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
693 		    sizeof (CORE_ADDR));
694 
695       CHECK_TYPEDEF (type);
696       cp_print_value_fields (type, type, value_contents_all (val),
697 			     value_embedded_offset (val), VALUE_ADDRESS (val),
698 			     stream, format, recurse, pretty, NULL, 1);
699       return;
700     }
701   val_print (type, value_contents_all (val),
702 	     value_embedded_offset (val), VALUE_ADDRESS (val),
703 	     stream, format, 0, recurse, pretty);
704 }
705 
706 void
cp_print_class_member(const gdb_byte * valaddr,struct type * domain,struct ui_file * stream,char * prefix)707 cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
708 		       struct ui_file *stream, char *prefix)
709 {
710 
711   /* VAL is a byte offset into the structure type DOMAIN.
712      Find the name of the field for that offset and
713      print it.  */
714   int extra = 0;
715   int bits = 0;
716   unsigned int i;
717   unsigned len = TYPE_NFIELDS (domain);
718 
719   /* @@ Make VAL into bit offset */
720 
721   /* Note: HP aCC generates offsets that are the real byte offsets added
722      to a constant bias 0x20000000 (1 << 29).  This constant bias gets
723      shifted out in the code below -- joyous happenstance! */
724 
725   /* Note: HP cfront uses a constant bias of 1; if we support this
726      compiler ever, we will have to adjust the computation below */
727 
728   LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
729   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
730     {
731       int bitpos = TYPE_FIELD_BITPOS (domain, i);
732       QUIT;
733       if (val == bitpos)
734 	break;
735       if (val < bitpos && i != 0)
736 	{
737 	  /* Somehow pointing into a field.  */
738 	  i -= 1;
739 	  extra = (val - TYPE_FIELD_BITPOS (domain, i));
740 	  if (extra & 0x7)
741 	    bits = 1;
742 	  else
743 	    extra >>= 3;
744 	  break;
745 	}
746     }
747   if (i < len)
748     {
749       char *name;
750       fputs_filtered (prefix, stream);
751       name = type_name_no_tag (domain);
752       if (name)
753 	fputs_filtered (name, stream);
754       else
755 	c_type_print_base (domain, stream, 0, 0);
756       fprintf_filtered (stream, "::");
757       fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
758       if (extra)
759 	fprintf_filtered (stream, " + %d bytes", extra);
760       if (bits)
761 	fprintf_filtered (stream, " (offset in bits)");
762     }
763   else
764     fprintf_filtered (stream, "%ld", (long) (val >> 3));
765 }
766 
767 
768 /* This function prints out virtual table entries for a class; it
769  * recurses on the base classes to find all virtual functions
770  * available in a class.
771  *
772  * pai/1997-05-21 Note: As the name suggests, it's currently
773  * implemented for HP aCC runtime only. g++ objects are handled
774  * differently and I have made no attempt to fold that logic in
775  * here. The runtime layout is different for the two cases.  Also,
776  * this currently has only the code for non-RRBC layouts generated by
777  * the HP aCC compiler; RRBC code is stubbed out and will have to be
778  * added later. */
779 
780 
781 static void
cp_print_hpacc_virtual_table_entries(struct type * type,int * vfuncs,struct value * v,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)782 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
783 				      struct value *v, struct ui_file *stream,
784 				      int format, int recurse,
785 				      enum val_prettyprint pretty)
786 {
787   int fn, oi;
788 
789   /* pai: FIXME this function doesn't work. It should handle a given
790    * virtual function only once (latest redefinition in class hierarchy)
791    */
792 
793   /* Recursion on other classes that can share the same vtable */
794   struct type *pbc = primary_base_class (type);
795   if (pbc)
796     cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format,
797 					  recurse, pretty);
798 
799   /* Now deal with vfuncs declared in this class */
800   for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
801     for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
802       if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
803 	{
804 	  char *vf_name;
805 	  const char *field_physname;
806 
807 	  /* virtual function offset */
808 	  int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi)
809 		    - 1);
810 
811 	  /* Get the address of the vfunction entry */
812 	  struct value *vf = value_copy (v);
813 	  if (value_lazy (vf))
814 	    (void) value_fetch_lazy (vf);
815 	  /* adjust by offset */
816 	  /* NOTE: cagney/2005-01-02: THIS IS BOGUS.  */
817 	  value_contents_writeable (vf)[0] += 4 * (HP_ACC_VFUNC_START + vx);
818 	  vf = value_ind (vf);	/* get the entry */
819 	  /* make it a pointer */
820 	  deprecated_set_value_type (vf, value_type (v));
821 
822 	  /* print out the entry */
823 	  common_val_print (vf, stream, format, 0, recurse + 1, pretty);
824 	  field_physname
825 	    = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
826 	  /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
827 	  vf_name = cplus_demangle (field_physname, DMGL_ARM);
828 	  fprintf_filtered (stream, " %s", vf_name);
829 	  if (--(*vfuncs) > 0)
830 	    fputs_filtered (", ", stream);
831 	}
832 }
833 
834 
835 
836 void
_initialize_cp_valprint(void)837 _initialize_cp_valprint (void)
838 {
839   add_setshow_boolean_cmd ("static-members", class_support,
840 			   &static_field_print, _("\
841 Set printing of C++ static members."), _("\
842 Show printing of C++ static members."), NULL,
843 			   NULL,
844 			   show_static_field_print,
845 			   &setprintlist, &showprintlist);
846   /* Turn on printing of static fields.  */
847   static_field_print = 1;
848 
849   add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
850 Set printing of C++ virtual function tables."), _("\
851 Show printing of C++ virtual function tables."), NULL,
852 			   NULL,
853 			   show_vtblprint,
854 			   &setprintlist, &showprintlist);
855 
856   add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
857 Set printing of object's derived type based on vtable info."), _("\
858 Show printing of object's derived type based on vtable info."), NULL,
859 			   NULL,
860 			   show_objectprint,
861 			   &setprintlist, &showprintlist);
862 
863   /* Give people the defaults which they are used to.  */
864   objectprint = 0;
865   vtblprint = 0;
866   obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
867   obstack_specify_allocation (&dont_print_statmem_obstack,
868 			      32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
869 			      xmalloc, xfree);
870 }
871