xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/cp-valprint.c (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Support for printing C++ values for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2023 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 3 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, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "gdbsupport/gdb_obstack.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "demangle.h"
29 #include "annotate.h"
30 #include "c-lang.h"
31 #include "target.h"
32 #include "cp-abi.h"
33 #include "valprint.h"
34 #include "cp-support.h"
35 #include "language.h"
36 #include "extension.h"
37 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
39 #include "gdbarch.h"
40 #include "cli/cli-style.h"
41 #include "gdbsupport/selftest.h"
42 #include "selftest-arch.h"
43 
44 static struct obstack dont_print_vb_obstack;
45 static struct obstack dont_print_statmem_obstack;
46 static struct obstack dont_print_stat_array_obstack;
47 
48 static void cp_print_static_field (struct type *, struct value *,
49 				   struct ui_file *, int,
50 				   const struct value_print_options *);
51 
52 static void cp_print_value (struct value *, struct ui_file *,
53 			    int, const struct value_print_options *,
54 			    struct type **);
55 
56 
57 /* GCC versions after 2.4.5 use this.  */
58 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
59 
60 /* Return truth value for assertion that TYPE is of the type
61    "pointer to virtual function".  */
62 
63 int
64 cp_is_vtbl_ptr_type (struct type *type)
65 {
66   const char *type_name = type->name ();
67 
68   return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
69 }
70 
71 /* Return truth value for the assertion that TYPE is of the type
72    "pointer to virtual function table".  */
73 
74 int
75 cp_is_vtbl_member (struct type *type)
76 {
77   /* With older versions of g++, the vtbl field pointed to an array of
78      structures.  Nowadays it points directly to the structure.  */
79   if (type->code () == TYPE_CODE_PTR)
80     {
81       type = type->target_type ();
82       if (type->code () == TYPE_CODE_ARRAY)
83 	{
84 	  type = type->target_type ();
85 	  if (type->code () == TYPE_CODE_STRUCT    /* if not using thunks */
86 	      || type->code () == TYPE_CODE_PTR)   /* if using thunks */
87 	    {
88 	      /* Virtual functions tables are full of pointers
89 		 to virtual functions.  */
90 	      return cp_is_vtbl_ptr_type (type);
91 	    }
92 	}
93       else if (type->code () == TYPE_CODE_STRUCT)  /* if not using thunks */
94 	{
95 	  return cp_is_vtbl_ptr_type (type);
96 	}
97       else if (type->code () == TYPE_CODE_PTR)     /* if using thunks */
98 	{
99 	  /* The type name of the thunk pointer is NULL when using
100 	     dwarf2.  We could test for a pointer to a function, but
101 	     there is no type info for the virtual table either, so it
102 	     wont help.  */
103 	  return cp_is_vtbl_ptr_type (type);
104 	}
105     }
106   return 0;
107 }
108 
109 /* Mutually recursive subroutines of cp_print_value and c_val_print to
110    print out a structure's fields: cp_print_value_fields and
111    cp_print_value.
112 
113    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
114    meanings as in cp_print_value and c_val_print.
115 
116    2nd argument REAL_TYPE is used to carry over the type of the
117    derived class across the recursion to base classes.
118 
119    DONT_PRINT is an array of baseclass types that we should not print,
120    or zero if called from top level.  */
121 
122 void
123 cp_print_value_fields (struct value *val, struct ui_file *stream,
124 		       int recurse, const struct value_print_options *options,
125 		       struct type **dont_print_vb,
126 		       int dont_print_statmem)
127 {
128   int i, len, n_baseclasses;
129   int fields_seen = 0;
130   static int last_set_recurse = -1;
131 
132   struct type *type = check_typedef (value_type (val));
133 
134   if (recurse == 0)
135     {
136       /* Any object can be left on obstacks only during an unexpected
137 	 error.  */
138 
139       if (obstack_object_size (&dont_print_statmem_obstack) > 0)
140 	{
141 	  obstack_free (&dont_print_statmem_obstack, NULL);
142 	  obstack_begin (&dont_print_statmem_obstack,
143 			 32 * sizeof (CORE_ADDR));
144 	}
145       if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
146 	{
147 	  obstack_free (&dont_print_stat_array_obstack, NULL);
148 	  obstack_begin (&dont_print_stat_array_obstack,
149 			 32 * sizeof (struct type *));
150 	}
151     }
152 
153   gdb_printf (stream, "{");
154   len = type->num_fields ();
155   n_baseclasses = TYPE_N_BASECLASSES (type);
156 
157   /* First, print out baseclasses such that we don't print
158      duplicates of virtual baseclasses.  */
159 
160   if (n_baseclasses > 0)
161     cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
162 
163   /* Second, print out data fields */
164 
165   /* If there are no data fields, skip this part */
166   if (len == n_baseclasses || !len)
167     fprintf_styled (stream, metadata_style.style (), "<No data fields>");
168   else
169     {
170       size_t statmem_obstack_initial_size = 0;
171       size_t stat_array_obstack_initial_size = 0;
172       struct type *vptr_basetype = NULL;
173       int vptr_fieldno;
174 
175       if (dont_print_statmem == 0)
176 	{
177 	  statmem_obstack_initial_size =
178 	    obstack_object_size (&dont_print_statmem_obstack);
179 
180 	  if (last_set_recurse != recurse)
181 	    {
182 	      stat_array_obstack_initial_size =
183 		obstack_object_size (&dont_print_stat_array_obstack);
184 
185 	      last_set_recurse = recurse;
186 	    }
187 	}
188 
189       vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
190       for (i = n_baseclasses; i < len; i++)
191 	{
192 	  const gdb_byte *valaddr = value_contents_for_printing (val).data ();
193 
194 	  /* If requested, skip printing of static fields.  */
195 	  if (!options->static_field_print
196 	      && field_is_static (&type->field (i)))
197 	    continue;
198 
199 	  if (fields_seen)
200 	    {
201 	      gdb_puts (",", stream);
202 	      if (!options->prettyformat)
203 		gdb_puts (" ", stream);
204 	    }
205 	  else if (n_baseclasses > 0)
206 	    {
207 	      if (options->prettyformat)
208 		{
209 		  gdb_printf (stream, "\n");
210 		  print_spaces (2 + 2 * recurse, stream);
211 		  gdb_puts ("members of ", stream);
212 		  gdb_puts (type->name (), stream);
213 		  gdb_puts (":", stream);
214 		}
215 	    }
216 	  fields_seen = 1;
217 
218 	  if (options->prettyformat)
219 	    {
220 	      gdb_printf (stream, "\n");
221 	      print_spaces (2 + 2 * recurse, stream);
222 	    }
223 	  else
224 	    {
225 	      stream->wrap_here (2 + 2 * recurse);
226 	    }
227 
228 	  annotate_field_begin (type->field (i).type ());
229 
230 	  if (field_is_static (&type->field (i)))
231 	    {
232 	      gdb_puts ("static ", stream);
233 	      fprintf_symbol (stream,
234 			      type->field (i).name (),
235 			      current_language->la_language,
236 			      DMGL_PARAMS | DMGL_ANSI);
237 	    }
238 	  else
239 	    fputs_styled (type->field (i).name (),
240 			  variable_name_style.style (), stream);
241 	  annotate_field_name_end ();
242 
243 	  /* We tweak various options in a few cases below.  */
244 	  value_print_options options_copy = *options;
245 	  value_print_options *opts = &options_copy;
246 
247 	  /* Do not print leading '=' in case of anonymous
248 	     unions.  */
249 	  if (strcmp (type->field (i).name (), ""))
250 	    gdb_puts (" = ", stream);
251 	  else
252 	    {
253 	      /* If this is an anonymous field then we want to consider it
254 		 as though it is at its parent's depth when it comes to the
255 		 max print depth.  */
256 	      if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
257 		++opts->max_depth;
258 	    }
259 	  annotate_field_value ();
260 
261 	  if (!field_is_static (&type->field (i))
262 	      && TYPE_FIELD_PACKED (type, i))
263 	    {
264 	      struct value *v;
265 
266 	      /* Bitfields require special handling, especially due to
267 		 byte order problems.  */
268 	      if (TYPE_FIELD_IGNORE (type, i))
269 		{
270 		  fputs_styled ("<optimized out or zero length>",
271 				metadata_style.style (), stream);
272 		}
273 	      else if (value_bits_synthetic_pointer
274 			 (val, type->field (i).loc_bitpos (),
275 			  TYPE_FIELD_BITSIZE (type, i)))
276 		{
277 		  fputs_styled (_("<synthetic pointer>"),
278 				metadata_style.style (), stream);
279 		}
280 	      else
281 		{
282 		  opts->deref_ref = 0;
283 
284 		  v = value_field_bitfield (type, i, valaddr,
285 					    value_embedded_offset (val), val);
286 
287 		  common_val_print (v, stream, recurse + 1,
288 				    opts, current_language);
289 		}
290 	    }
291 	  else
292 	    {
293 	      if (TYPE_FIELD_IGNORE (type, i))
294 		{
295 		  fputs_styled ("<optimized out or zero length>",
296 				metadata_style.style (), stream);
297 		}
298 	      else if (field_is_static (&type->field (i)))
299 		{
300 		  try
301 		    {
302 		      struct value *v = value_static_field (type, i);
303 
304 		      cp_print_static_field (type->field (i).type (),
305 					     v, stream, recurse + 1,
306 					     opts);
307 		    }
308 		  catch (const gdb_exception_error &ex)
309 		    {
310 		      fprintf_styled (stream, metadata_style.style (),
311 				      _("<error reading variable: %s>"),
312 				      ex.what ());
313 		    }
314 		}
315 	      else if (i == vptr_fieldno && type == vptr_basetype)
316 		{
317 		  int i_offset = type->field (i).loc_bitpos () / 8;
318 		  struct type *i_type = type->field (i).type ();
319 
320 		  if (valprint_check_validity (stream, i_type, i_offset, val))
321 		    {
322 		      CORE_ADDR addr;
323 
324 		      i_offset += value_embedded_offset (val);
325 		      addr = extract_typed_address (valaddr + i_offset, i_type);
326 		      print_function_pointer_address (opts,
327 						      type->arch (),
328 						      addr, stream);
329 		    }
330 		}
331 	      else
332 		{
333 		  struct value *v = value_primitive_field (val, 0, i, type);
334 		  opts->deref_ref = 0;
335 		  common_val_print (v, stream, recurse + 1, opts,
336 				    current_language);
337 		}
338 	    }
339 	  annotate_field_end ();
340 	}
341 
342       if (dont_print_statmem == 0)
343 	{
344 	  size_t obstack_final_size =
345 	   obstack_object_size (&dont_print_statmem_obstack);
346 
347 	  if (obstack_final_size > statmem_obstack_initial_size)
348 	    {
349 	      /* In effect, a pop of the printed-statics stack.  */
350 	      size_t shrink_bytes
351 		= statmem_obstack_initial_size - obstack_final_size;
352 	      obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
353 	    }
354 
355 	  if (last_set_recurse != recurse)
356 	    {
357 	      obstack_final_size =
358 		obstack_object_size (&dont_print_stat_array_obstack);
359 
360 	      if (obstack_final_size > stat_array_obstack_initial_size)
361 		{
362 		  void *free_to_ptr =
363 		    (char *) obstack_next_free (&dont_print_stat_array_obstack)
364 		    - (obstack_final_size
365 		       - stat_array_obstack_initial_size);
366 
367 		  obstack_free (&dont_print_stat_array_obstack,
368 				free_to_ptr);
369 		}
370 	      last_set_recurse = -1;
371 	    }
372 	}
373 
374       if (options->prettyformat)
375 	{
376 	  gdb_printf (stream, "\n");
377 	  print_spaces (2 * recurse, stream);
378 	}
379     }				/* if there are data fields */
380 
381   gdb_printf (stream, "}");
382 }
383 
384 /* Special val_print routine to avoid printing multiple copies of
385    virtual baseclasses.  */
386 
387 static void
388 cp_print_value (struct value *val, struct ui_file *stream,
389 		int recurse, const struct value_print_options *options,
390 		struct type **dont_print_vb)
391 {
392   struct type *type = check_typedef (value_type (val));
393   CORE_ADDR address = value_address (val);
394   struct type **last_dont_print
395     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
396   struct obstack tmp_obstack = dont_print_vb_obstack;
397   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
398   const gdb_byte *valaddr = value_contents_for_printing (val).data ();
399 
400   if (dont_print_vb == 0)
401     {
402       /* If we're at top level, carve out a completely fresh chunk of
403 	 the obstack and use that until this particular invocation
404 	 returns.  */
405       /* Bump up the high-water mark.  Now alpha is omega.  */
406       obstack_finish (&dont_print_vb_obstack);
407     }
408 
409   for (i = 0; i < n_baseclasses; i++)
410     {
411       LONGEST boffset = 0;
412       int skip = 0;
413       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
414       const char *basename = baseclass->name ();
415       struct value *base_val = NULL;
416 
417       if (BASETYPE_VIA_VIRTUAL (type, i))
418 	{
419 	  struct type **first_dont_print
420 	    = (struct type **) obstack_base (&dont_print_vb_obstack);
421 
422 	  int j = (struct type **)
423 	    obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
424 
425 	  while (--j >= 0)
426 	    if (baseclass == first_dont_print[j])
427 	      goto flush_it;
428 
429 	  obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
430 	}
431 
432       try
433 	{
434 	  boffset = baseclass_offset (type, i, valaddr,
435 				      value_embedded_offset (val),
436 				      address, val);
437 	}
438       catch (const gdb_exception_error &ex)
439 	{
440 	  if (ex.error == NOT_AVAILABLE_ERROR)
441 	    skip = -1;
442 	  else
443 	    skip = 1;
444 	}
445 
446       if (skip == 0)
447 	{
448 	  if (BASETYPE_VIA_VIRTUAL (type, i))
449 	    {
450 	      /* The virtual base class pointer might have been
451 		 clobbered by the user program. Make sure that it
452 		 still points to a valid memory location.  */
453 
454 	      if (boffset < 0 || boffset >= type->length ())
455 		{
456 		  gdb::byte_vector buf (baseclass->length ());
457 
458 		  if (target_read_memory (address + boffset, buf.data (),
459 					  baseclass->length ()) != 0)
460 		    skip = 1;
461 		  base_val = value_from_contents_and_address (baseclass,
462 							      buf.data (),
463 							      address + boffset);
464 		  baseclass = value_type (base_val);
465 		  boffset = 0;
466 		}
467 	      else
468 		{
469 		  base_val = val;
470 		}
471 	    }
472 	  else
473 	    {
474 	      base_val = val;
475 	    }
476 	}
477 
478       /* Now do the printing.  */
479       if (options->prettyformat)
480 	{
481 	  gdb_printf (stream, "\n");
482 	  print_spaces (2 * recurse, stream);
483 	}
484       gdb_puts ("<", stream);
485       /* Not sure what the best notation is in the case where there is
486 	 no baseclass name.  */
487       gdb_puts (basename ? basename : "", stream);
488       gdb_puts ("> = ", stream);
489 
490       if (skip < 0)
491 	val_print_unavailable (stream);
492       else if (skip > 0)
493 	val_print_invalid_address (stream);
494       else
495 	{
496 	  int result = 0;
497 
498 	  if (!val_print_check_max_depth (stream, recurse, options,
499 					  current_language))
500 	    {
501 	      struct value *baseclass_val = value_primitive_field (val, 0,
502 								   i, type);
503 
504 	      /* Attempt to run an extension language pretty-printer on the
505 		 baseclass if possible.  */
506 	      if (!options->raw)
507 		result
508 		  = apply_ext_lang_val_pretty_printer (baseclass_val, stream,
509 						       recurse, options,
510 						       current_language);
511 
512 	      if (!result)
513 		cp_print_value_fields (baseclass_val, stream, recurse, options,
514 				       ((struct type **)
515 					obstack_base (&dont_print_vb_obstack)),
516 				       0);
517 	    }
518 	}
519       gdb_puts (", ", stream);
520 
521     flush_it:
522       ;
523     }
524 
525   if (dont_print_vb == 0)
526     {
527       /* Free the space used to deal with the printing
528 	 of this type from top level.  */
529       obstack_free (&dont_print_vb_obstack, last_dont_print);
530       /* Reset watermark so that we can continue protecting
531 	 ourselves from whatever we were protecting ourselves.  */
532       dont_print_vb_obstack = tmp_obstack;
533     }
534 }
535 
536 /* Print value of a static member.  To avoid infinite recursion when
537    printing a class that contains a static instance of the class, we
538    keep the addresses of all printed static member classes in an
539    obstack and refuse to print them more than once.
540 
541    VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
542    have the same meanings as in c_val_print.  */
543 
544 static void
545 cp_print_static_field (struct type *type,
546 		       struct value *val,
547 		       struct ui_file *stream,
548 		       int recurse,
549 		       const struct value_print_options *options)
550 {
551   struct value_print_options opts;
552 
553   if (value_entirely_optimized_out (val))
554     {
555       val_print_optimized_out (val, stream);
556       return;
557     }
558 
559   struct type *real_type = check_typedef (type);
560   if (real_type->code () == TYPE_CODE_STRUCT)
561     {
562       CORE_ADDR *first_dont_print;
563       CORE_ADDR addr = value_address (val);
564       int i;
565 
566       first_dont_print
567 	= (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
568       i = obstack_object_size (&dont_print_statmem_obstack)
569 	/ sizeof (CORE_ADDR);
570 
571       while (--i >= 0)
572 	{
573 	  if (addr == first_dont_print[i])
574 	    {
575 	      fputs_styled (_("<same as static member of an already"
576 			      " seen type>"),
577 			    metadata_style.style (), stream);
578 	      return;
579 	    }
580 	}
581 
582       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
583 		    sizeof (CORE_ADDR));
584       cp_print_value_fields (val, stream, recurse, options, NULL, 1);
585       return;
586     }
587 
588   if (real_type->code () == TYPE_CODE_ARRAY)
589     {
590       struct type **first_dont_print;
591       int i;
592       struct type *target_type = type->target_type ();
593 
594       first_dont_print
595 	= (struct type **) obstack_base (&dont_print_stat_array_obstack);
596       i = obstack_object_size (&dont_print_stat_array_obstack)
597 	/ sizeof (struct type *);
598 
599       while (--i >= 0)
600 	{
601 	  if (target_type == first_dont_print[i])
602 	    {
603 	      fputs_styled (_("<same as static member of an already"
604 			      " seen type>"),
605 			    metadata_style.style (), stream);
606 	      return;
607 	    }
608 	}
609 
610       obstack_grow (&dont_print_stat_array_obstack,
611 		    (char *) &target_type,
612 		    sizeof (struct type *));
613     }
614 
615   opts = *options;
616   opts.deref_ref = 0;
617   common_val_print (val, stream, recurse, &opts, current_language);
618 }
619 
620 /* Find the field in *SELF, or its non-virtual base classes, with
621    bit offset OFFSET.  Set *SELF to the containing type and *FIELDNO
622    to the containing field number.  If OFFSET is not exactly at the
623    start of some field, set *SELF to NULL.  */
624 
625 static void
626 cp_find_class_member (struct type **self_p, int *fieldno,
627 		      LONGEST offset)
628 {
629   struct type *self;
630   unsigned int i;
631   unsigned len;
632 
633   *self_p = check_typedef (*self_p);
634   self = *self_p;
635   len = self->num_fields ();
636 
637   for (i = TYPE_N_BASECLASSES (self); i < len; i++)
638     {
639       field &f = self->field (i);
640       if (field_is_static (&f))
641 	continue;
642       LONGEST bitpos = f.loc_bitpos ();
643 
644       QUIT;
645       if (offset == bitpos)
646 	{
647 	  *fieldno = i;
648 	  return;
649 	}
650     }
651 
652   for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
653     {
654       LONGEST bitpos = self->field (i).loc_bitpos ();
655       LONGEST bitsize = 8 * self->field (i).type ()->length ();
656 
657       if (offset >= bitpos && offset < bitpos + bitsize)
658 	{
659 	  *self_p = self->field (i).type ();
660 	  cp_find_class_member (self_p, fieldno, offset - bitpos);
661 	  return;
662 	}
663     }
664 
665   *self_p = NULL;
666 }
667 
668 void
669 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
670 		       struct ui_file *stream, const char *prefix)
671 {
672   enum bfd_endian byte_order = type_byte_order (type);
673 
674   /* VAL is a byte offset into the structure type SELF_TYPE.
675      Find the name of the field for that offset and
676      print it.  */
677   struct type *self_type = TYPE_SELF_TYPE (type);
678   LONGEST val;
679   int fieldno;
680 
681   val = extract_signed_integer (valaddr,
682 				type->length (),
683 				byte_order);
684 
685   /* Pointers to data members are usually byte offsets into an object.
686      Because a data member can have offset zero, and a NULL pointer to
687      member must be distinct from any valid non-NULL pointer to
688      member, either the value is biased or the NULL value has a
689      special representation; both are permitted by ISO C++.  HP aCC
690      used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
691      and other compilers which use the Itanium ABI use -1 as the NULL
692      value.  GDB only supports that last form; to add support for
693      another form, make this into a cp-abi hook.  */
694 
695   if (val == -1)
696     {
697       gdb_printf (stream, "NULL");
698       return;
699     }
700 
701   cp_find_class_member (&self_type, &fieldno, val << 3);
702 
703   if (self_type != NULL)
704     {
705       const char *name;
706 
707       gdb_puts (prefix, stream);
708       name = self_type->name ();
709       if (name)
710 	gdb_puts (name, stream);
711       else
712 	c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
713       gdb_printf (stream, "::");
714       fputs_styled (self_type->field (fieldno).name (),
715 		    variable_name_style.style (), stream);
716     }
717   else
718     gdb_printf (stream, "%ld", (long) val);
719 }
720 
721 #if GDB_SELF_TEST
722 
723 /* Test printing of TYPE_CODE_STRUCT values.  */
724 
725 static void
726 test_print_fields (gdbarch *arch)
727 {
728   struct field *f;
729   type *uint8_type = builtin_type (arch)->builtin_uint8;
730   type *bool_type = builtin_type (arch)->builtin_bool;
731   type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT);
732   the_struct->set_length (4);
733 
734   /* Value:  1110 1001
735      Fields: C-BB B-A- */
736   if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE)
737     {
738       f = append_composite_type_field_raw (the_struct, "A", bool_type);
739       f->set_loc_bitpos (1);
740       FIELD_BITSIZE (*f) = 1;
741       f = append_composite_type_field_raw (the_struct, "B", uint8_type);
742       f->set_loc_bitpos (3);
743       FIELD_BITSIZE (*f) = 3;
744       f = append_composite_type_field_raw (the_struct, "C", bool_type);
745       f->set_loc_bitpos (7);
746       FIELD_BITSIZE (*f) = 1;
747     }
748   /* According to the logic commented in "make_gdb_type_struct ()" of
749    * target-descriptions.c, bit positions are numbered differently for
750    * little and big endians.  */
751   else
752     {
753       f = append_composite_type_field_raw (the_struct, "A", bool_type);
754       f->set_loc_bitpos (30);
755       FIELD_BITSIZE (*f) = 1;
756       f = append_composite_type_field_raw (the_struct, "B", uint8_type);
757       f->set_loc_bitpos (26);
758       FIELD_BITSIZE (*f) = 3;
759       f = append_composite_type_field_raw (the_struct, "C", bool_type);
760       f->set_loc_bitpos (24);
761       FIELD_BITSIZE (*f) = 1;
762     }
763 
764   value *val = allocate_value (the_struct);
765   gdb_byte *contents = value_contents_writeable (val).data ();
766   store_unsigned_integer (contents, value_enclosing_type (val)->length (),
767 			  gdbarch_byte_order (arch), 0xe9);
768 
769   string_file out;
770   struct value_print_options opts;
771   get_no_prettyformat_print_options (&opts);
772   cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
773   SELF_CHECK (out.string () == "{A = false, B = 5, C = true}");
774 
775   out.clear();
776   opts.format = 'x';
777   cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
778   SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}");
779 }
780 
781 #endif
782 
783 
784 void _initialize_cp_valprint ();
785 void
786 _initialize_cp_valprint ()
787 {
788 #if GDB_SELF_TEST
789   selftests::register_test_foreach_arch ("print-fields", test_print_fields);
790 #endif
791 
792   obstack_begin (&dont_print_stat_array_obstack,
793 		 32 * sizeof (struct type *));
794   obstack_begin (&dont_print_statmem_obstack,
795 		 32 * sizeof (CORE_ADDR));
796   obstack_begin (&dont_print_vb_obstack,
797 		 32 * sizeof (struct type *));
798 }
799