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