1 /* Language independent support for printing types for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2021 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 "gdb_obstack.h"
22 #include "bfd.h"		/* Binary File Description */
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "target.h"
31 #include "language.h"
32 #include "cp-abi.h"
33 #include "typeprint.h"
34 #include "valprint.h"
35 #include <ctype.h>
36 #include "cli/cli-utils.h"
37 #include "extension.h"
38 #include "completer.h"
39 #include "cli/cli-style.h"
40 
41 const struct type_print_options type_print_raw_options =
42 {
43   1,				/* raw */
44   1,				/* print_methods */
45   1,				/* print_typedefs */
46   0,				/* print_offsets */
47   0,				/* print_in_hex */
48   0,				/* print_nested_type_limit  */
49   NULL,				/* local_typedefs */
50   NULL,				/* global_table */
51   NULL				/* global_printers */
52 };
53 
54 /* The default flags for 'ptype' and 'whatis'.  */
55 
56 static struct type_print_options default_ptype_flags =
57 {
58   0,				/* raw */
59   1,				/* print_methods */
60   1,				/* print_typedefs */
61   0,				/* print_offsets */
62   0,				/* print_in_hex */
63   0,				/* print_nested_type_limit  */
64   NULL,				/* local_typedefs */
65   NULL,				/* global_table */
66   NULL				/* global_printers */
67 };
68 
69 
70 
71 /* See typeprint.h.  */
72 
73 const int print_offset_data::indentation = 27;
74 
75 /* See typeprint.h.  */
76 
print_offset_data(const struct type_print_options * flags)77 print_offset_data::print_offset_data (const struct type_print_options *flags)
78 {
79   if (flags != nullptr)
80     print_in_hex = flags->print_in_hex;
81 }
82 
83 /* See typeprint.h.  */
84 
85 void
maybe_print_hole(struct ui_file * stream,unsigned int bitpos,const char * for_what)86 print_offset_data::maybe_print_hole (struct ui_file *stream,
87 				     unsigned int bitpos,
88 				     const char *for_what)
89 {
90   /* We check for END_BITPOS > 0 because there is a specific
91      scenario when END_BITPOS can be zero and BITPOS can be >
92      0: when we are dealing with a struct/class with a virtual method.
93      Because of the vtable, the first field of the struct/class will
94      have an offset of sizeof (void *) (the size of the vtable).  If
95      we do not check for END_BITPOS > 0 here, GDB will report
96      a hole before the first field, which is not accurate.  */
97   if (end_bitpos > 0 && end_bitpos < bitpos)
98     {
99       /* If END_BITPOS is smaller than the current type's
100 	 bitpos, it means there's a hole in the struct, so we report
101 	 it here.  */
102       unsigned int hole = bitpos - end_bitpos;
103       unsigned int hole_byte = hole / TARGET_CHAR_BIT;
104       unsigned int hole_bit = hole % TARGET_CHAR_BIT;
105 
106       if (hole_bit > 0)
107 	fprintf_filtered (stream, "/* XXX %2u-bit %-7s    */\n", hole_bit,
108 			  for_what);
109 
110       if (hole_byte > 0)
111 	fprintf_filtered (stream, "/* XXX %2u-byte %-7s   */\n", hole_byte,
112 			  for_what);
113     }
114 }
115 
116 /* See typeprint.h.  */
117 
118 void
update(struct type * type,unsigned int field_idx,struct ui_file * stream)119 print_offset_data::update (struct type *type, unsigned int field_idx,
120 			   struct ui_file *stream)
121 {
122   if (field_is_static (&type->field (field_idx)))
123     {
124       print_spaces_filtered (indentation, stream);
125       return;
126     }
127 
128   struct type *ftype = check_typedef (type->field (field_idx).type ());
129   if (type->code () == TYPE_CODE_UNION)
130     {
131       /* Since union fields don't have the concept of offsets, we just
132 	 print their sizes.  */
133       fprintf_filtered (stream, "/*                %6s */",
134 			(print_in_hex ?
135 			 hex_string_custom (TYPE_LENGTH (ftype), 4) :
136 			 pulongest (TYPE_LENGTH (ftype))));
137       return;
138     }
139 
140   unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx);
141   unsigned int fieldsize_byte = TYPE_LENGTH (ftype);
142   unsigned int fieldsize_bit = fieldsize_byte * TARGET_CHAR_BIT;
143 
144   maybe_print_hole (stream, bitpos, "hole");
145 
146   if (TYPE_FIELD_PACKED (type, field_idx)
147       || offset_bitpos % TARGET_CHAR_BIT != 0)
148     {
149       /* We're dealing with a bitfield.  Print the bit offset.  */
150       fieldsize_bit = TYPE_FIELD_BITSIZE (type, field_idx);
151 
152       unsigned real_bitpos = bitpos + offset_bitpos;
153 
154       fprintf_filtered (stream,
155 			(print_in_hex ? "/* 0x%04x: 0x%x" : "/* %6u:%2u  "),
156 			real_bitpos / TARGET_CHAR_BIT,
157 			real_bitpos % TARGET_CHAR_BIT);
158     }
159   else
160     {
161       /* The position of the field, relative to the beginning of the
162 	 struct.  */
163       fprintf_filtered (stream, (print_in_hex ?  "/* 0x%04x" : "/* %6u"),
164 			(bitpos + offset_bitpos) / TARGET_CHAR_BIT);
165 
166       fprintf_filtered (stream, "     ");
167     }
168 
169   fprintf_filtered (stream, (print_in_hex ? " |  0x%04x */" : " |  %6u */"),
170 		    fieldsize_byte);
171 
172   end_bitpos = bitpos + fieldsize_bit;
173 }
174 
175 /* See typeprint.h.  */
176 
177 void
finish(struct type * type,int level,struct ui_file * stream)178 print_offset_data::finish (struct type *type, int level,
179 			   struct ui_file *stream)
180 {
181   unsigned int bitpos = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
182   maybe_print_hole (stream, bitpos, "padding");
183 
184   fputs_filtered ("\n", stream);
185   print_spaces_filtered (level + 4 + print_offset_data::indentation, stream);
186   fprintf_filtered (stream, "/* total size (bytes): %4s */\n",
187 		    pulongest (TYPE_LENGTH (type)));
188 }
189 
190 
191 
192 /* A hash function for a typedef_field.  */
193 
194 static hashval_t
hash_typedef_field(const void * p)195 hash_typedef_field (const void *p)
196 {
197   const struct decl_field *tf = (const struct decl_field *) p;
198   struct type *t = check_typedef (tf->type);
199 
200   return htab_hash_string (TYPE_SAFE_NAME (t));
201 }
202 
203 /* An equality function for a typedef field.  */
204 
205 static int
eq_typedef_field(const void * a,const void * b)206 eq_typedef_field (const void *a, const void *b)
207 {
208   const struct decl_field *tfa = (const struct decl_field *) a;
209   const struct decl_field *tfb = (const struct decl_field *) b;
210 
211   return types_equal (tfa->type, tfb->type);
212 }
213 
214 /* See typeprint.h.  */
215 
216 void
recursively_update(struct type * t)217 typedef_hash_table::recursively_update (struct type *t)
218 {
219   int i;
220 
221   for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
222     {
223       struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
224       void **slot;
225 
226       slot = htab_find_slot (m_table.get (), tdef, INSERT);
227       /* Only add a given typedef name once.  Really this shouldn't
228 	 happen; but it is safe enough to do the updates breadth-first
229 	 and thus use the most specific typedef.  */
230       if (*slot == NULL)
231 	*slot = tdef;
232     }
233 
234   /* Recurse into superclasses.  */
235   for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
236     recursively_update (TYPE_BASECLASS (t, i));
237 }
238 
239 /* See typeprint.h.  */
240 
241 void
add_template_parameters(struct type * t)242 typedef_hash_table::add_template_parameters (struct type *t)
243 {
244   int i;
245 
246   for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
247     {
248       struct decl_field *tf;
249       void **slot;
250 
251       /* We only want type-valued template parameters in the hash.  */
252       if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
253 	continue;
254 
255       tf = XOBNEW (&m_storage, struct decl_field);
256       tf->name = TYPE_TEMPLATE_ARGUMENT (t, i)->linkage_name ();
257       tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i));
258 
259       slot = htab_find_slot (m_table.get (), tf, INSERT);
260       if (*slot == NULL)
261 	*slot = tf;
262     }
263 }
264 
265 /* See typeprint.h.  */
266 
typedef_hash_table()267 typedef_hash_table::typedef_hash_table ()
268   : m_table (htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
269 				NULL, xcalloc, xfree))
270 {
271 }
272 
273 /* Helper function for typedef_hash_table::copy.  */
274 
275 static int
copy_typedef_hash_element(void ** slot,void * nt)276 copy_typedef_hash_element (void **slot, void *nt)
277 {
278   htab_t new_table = (htab_t) nt;
279   void **new_slot;
280 
281   new_slot = htab_find_slot (new_table, *slot, INSERT);
282   if (*new_slot == NULL)
283     *new_slot = *slot;
284 
285   return 1;
286 }
287 
288 /* See typeprint.h.  */
289 
typedef_hash_table(const typedef_hash_table & table)290 typedef_hash_table::typedef_hash_table (const typedef_hash_table &table)
291 {
292   m_table.reset (htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
293 				    NULL, xcalloc, xfree));
294   htab_traverse_noresize (table.m_table.get (), copy_typedef_hash_element,
295 			  m_table.get ());
296 }
297 
298 /* Look up the type T in the global typedef hash.  If it is found,
299    return the typedef name.  If it is not found, apply the
300    type-printers, if any, given by start_script_type_printers and return the
301    result.  A NULL return means that the name was not found.  */
302 
303 const char *
find_global_typedef(const struct type_print_options * flags,struct type * t)304 typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
305 					 struct type *t)
306 {
307   char *applied;
308   void **slot;
309   struct decl_field tf, *new_tf;
310 
311   if (flags->global_typedefs == NULL)
312     return NULL;
313 
314   tf.name = NULL;
315   tf.type = t;
316 
317   slot = htab_find_slot (flags->global_typedefs->m_table.get (), &tf, INSERT);
318   if (*slot != NULL)
319     {
320       new_tf = (struct decl_field *) *slot;
321       return new_tf->name;
322     }
323 
324   /* Put an entry into the hash table now, in case
325      apply_ext_lang_type_printers recurses.  */
326   new_tf = XOBNEW (&flags->global_typedefs->m_storage, struct decl_field);
327   new_tf->name = NULL;
328   new_tf->type = t;
329 
330   *slot = new_tf;
331 
332   applied = apply_ext_lang_type_printers (flags->global_printers, t);
333 
334   if (applied != NULL)
335     {
336       new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage,
337 				     applied);
338       xfree (applied);
339     }
340 
341   return new_tf->name;
342 }
343 
344 /* See typeprint.h.  */
345 
346 const char *
find_typedef(const struct type_print_options * flags,struct type * t)347 typedef_hash_table::find_typedef (const struct type_print_options *flags,
348 				  struct type *t)
349 {
350   if (flags->local_typedefs != NULL)
351     {
352       struct decl_field tf, *found;
353 
354       tf.name = NULL;
355       tf.type = t;
356       htab_t table = flags->local_typedefs->m_table.get ();
357       found = (struct decl_field *) htab_find (table, &tf);
358 
359       if (found != NULL)
360 	return found->name;
361     }
362 
363   return find_global_typedef (flags, t);
364 }
365 
366 
367 
368 /* Print a description of a type in the format of a
369    typedef for the current language.
370    NEW is the new name for a type TYPE.  */
371 
372 void
typedef_print(struct type * type,struct symbol * newobj,struct ui_file * stream)373 typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream)
374 {
375   current_language->print_typedef (type, newobj, stream);
376 }
377 
378 /* Print a description of a type TYPE in the form of a declaration of a
379    variable named VARSTRING.  (VARSTRING is demangled if necessary.)
380    Output goes to STREAM (via stdio).
381    If SHOW is positive, we show the contents of the outermost level
382    of structure even if there is a type name that could be used instead.
383    If SHOW is negative, we never show the details of elements' types.  */
384 
385 void
type_print(struct type * type,const char * varstring,struct ui_file * stream,int show)386 type_print (struct type *type, const char *varstring, struct ui_file *stream,
387 	    int show)
388 {
389   LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
390 }
391 
392 /* Print TYPE to a string, returning it.  The caller is responsible for
393    freeing the string.  */
394 
395 std::string
type_to_string(struct type * type)396 type_to_string (struct type *type)
397 {
398   try
399     {
400       string_file stb;
401 
402       type_print (type, "", &stb, -1);
403       return std::move (stb.string ());
404     }
405   catch (const gdb_exception &except)
406     {
407     }
408 
409   return {};
410 }
411 
412 /* See typeprint.h.  */
413 
414 void
type_print_unknown_return_type(struct ui_file * stream)415 type_print_unknown_return_type (struct ui_file *stream)
416 {
417   fprintf_styled (stream, metadata_style.style (),
418 		  _("<unknown return type>"));
419 }
420 
421 /* See typeprint.h.  */
422 
423 void
error_unknown_type(const char * sym_print_name)424 error_unknown_type (const char *sym_print_name)
425 {
426   error (_("'%s' has unknown type; cast it to its declared type"),
427 	 sym_print_name);
428 }
429 
430 /* Print type of EXP, or last thing in value history if EXP == NULL.
431    show is passed to type_print.  */
432 
433 static void
whatis_exp(const char * exp,int show)434 whatis_exp (const char *exp, int show)
435 {
436   struct value *val;
437   struct type *real_type = NULL;
438   struct type *type;
439   int full = 0;
440   LONGEST top = -1;
441   int using_enc = 0;
442   struct value_print_options opts;
443   struct type_print_options flags = default_ptype_flags;
444 
445   if (exp)
446     {
447       if (*exp == '/')
448 	{
449 	  int seen_one = 0;
450 
451 	  for (++exp; *exp && !isspace (*exp); ++exp)
452 	    {
453 	      switch (*exp)
454 		{
455 		case 'r':
456 		  flags.raw = 1;
457 		  break;
458 		case 'm':
459 		  flags.print_methods = 0;
460 		  break;
461 		case 'M':
462 		  flags.print_methods = 1;
463 		  break;
464 		case 't':
465 		  flags.print_typedefs = 0;
466 		  break;
467 		case 'T':
468 		  flags.print_typedefs = 1;
469 		  break;
470 		case 'o':
471 		  {
472 		    /* Filter out languages which don't implement the
473 		       feature.  */
474 		    if (show > 0
475 			&& (current_language->la_language == language_c
476 			    || current_language->la_language == language_cplus
477 			    || current_language->la_language == language_rust))
478 		      {
479 			flags.print_offsets = 1;
480 			flags.print_typedefs = 0;
481 			flags.print_methods = 0;
482 		      }
483 		    break;
484 		  }
485 		case 'x':
486 		  flags.print_in_hex = 1;
487 		  break;
488 		case 'd':
489 		  flags.print_in_hex = 0;
490 		  break;
491 		default:
492 		  error (_("unrecognized flag '%c'"), *exp);
493 		}
494 	      seen_one = 1;
495 	    }
496 
497 	  if (!*exp && !seen_one)
498 	    error (_("flag expected"));
499 	  if (!isspace (*exp))
500 	    error (_("expected space after format"));
501 	  exp = skip_spaces (exp);
502 	}
503 
504       expression_up expr = parse_expression (exp);
505 
506       /* The behavior of "whatis" depends on whether the user
507 	 expression names a type directly, or a language expression
508 	 (including variable names).  If the former, then "whatis"
509 	 strips one level of typedefs, only.  If an expression,
510 	 "whatis" prints the type of the expression without stripping
511 	 any typedef level.  "ptype" always strips all levels of
512 	 typedefs.  */
513       val = evaluate_type (expr.get ());
514       type = value_type (val);
515 
516       if (show == -1 && expr->first_opcode () == OP_TYPE)
517 	{
518 	  /* The user expression names a type directly.  */
519 
520 	  /* If this is a typedef, then find its immediate target.
521 	     Use check_typedef to resolve stubs, but ignore its result
522 	     because we do not want to dig past all typedefs.  */
523 	  check_typedef (type);
524 	  if (type->code () == TYPE_CODE_TYPEDEF)
525 	    type = TYPE_TARGET_TYPE (type);
526 
527 	  /* If the expression is actually a type, then there's no
528 	     value to fetch the dynamic type from.  */
529 	  val = NULL;
530 	}
531     }
532   else
533     {
534       val = access_value_history (0);
535       type = value_type (val);
536     }
537 
538   get_user_print_options (&opts);
539   if (val != NULL && opts.objectprint)
540     {
541       if (((type->code () == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type))
542 	  && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT))
543 	real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
544       else if (type->code () == TYPE_CODE_STRUCT)
545 	real_type = value_rtti_type (val, &full, &top, &using_enc);
546     }
547 
548   if (flags.print_offsets
549       && (type->code () == TYPE_CODE_STRUCT
550 	  || type->code () == TYPE_CODE_UNION))
551     fprintf_filtered (gdb_stdout, "/* offset      |    size */  ");
552 
553   printf_filtered ("type = ");
554 
555   std::unique_ptr<typedef_hash_table> table_holder;
556   std::unique_ptr<ext_lang_type_printers> printer_holder;
557   if (!flags.raw)
558     {
559       table_holder.reset (new typedef_hash_table);
560       flags.global_typedefs = table_holder.get ();
561 
562       printer_holder.reset (new ext_lang_type_printers);
563       flags.global_printers = printer_holder.get ();
564     }
565 
566   if (real_type)
567     {
568       printf_filtered ("/* real type = ");
569       type_print (real_type, "", gdb_stdout, -1);
570       if (! full)
571 	printf_filtered (" (incomplete object)");
572       printf_filtered (" */\n");
573     }
574 
575   LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
576   printf_filtered ("\n");
577 }
578 
579 static void
whatis_command(const char * exp,int from_tty)580 whatis_command (const char *exp, int from_tty)
581 {
582   /* Most of the time users do not want to see all the fields
583      in a structure.  If they do they can use the "ptype" command.
584      Hence the "-1" below.  */
585   whatis_exp (exp, -1);
586 }
587 
588 /* TYPENAME is either the name of a type, or an expression.  */
589 
590 static void
ptype_command(const char * type_name,int from_tty)591 ptype_command (const char *type_name, int from_tty)
592 {
593   whatis_exp (type_name, 1);
594 }
595 
596 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
597    Used to print data from type structures in a specified type.  For example,
598    array bounds may be characters or booleans in some languages, and this
599    allows the ranges to be printed in their "natural" form rather than as
600    decimal integer values.
601 
602    FIXME:  This is here simply because only the type printing routines
603    currently use it, and it wasn't clear if it really belonged somewhere
604    else (like printcmd.c).  There are a lot of other gdb routines that do
605    something similar, but they are generally concerned with printing values
606    that come from the inferior in target byte order and target size.  */
607 
608 void
print_type_scalar(struct type * type,LONGEST val,struct ui_file * stream)609 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
610 {
611   unsigned int i;
612   unsigned len;
613 
614   type = check_typedef (type);
615 
616   switch (type->code ())
617     {
618 
619     case TYPE_CODE_ENUM:
620       len = type->num_fields ();
621       for (i = 0; i < len; i++)
622 	{
623 	  if (TYPE_FIELD_ENUMVAL (type, i) == val)
624 	    {
625 	      break;
626 	    }
627 	}
628       if (i < len)
629 	{
630 	  fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
631 	}
632       else
633 	{
634 	  print_longest (stream, 'd', 0, val);
635 	}
636       break;
637 
638     case TYPE_CODE_INT:
639       print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val);
640       break;
641 
642     case TYPE_CODE_CHAR:
643       LA_PRINT_CHAR ((unsigned char) val, type, stream);
644       break;
645 
646     case TYPE_CODE_BOOL:
647       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
648       break;
649 
650     case TYPE_CODE_RANGE:
651       print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
652       return;
653 
654     case TYPE_CODE_FIXED_POINT:
655       print_type_fixed_point (type, stream);
656       break;
657 
658     case TYPE_CODE_UNDEF:
659     case TYPE_CODE_PTR:
660     case TYPE_CODE_ARRAY:
661     case TYPE_CODE_STRUCT:
662     case TYPE_CODE_UNION:
663     case TYPE_CODE_FUNC:
664     case TYPE_CODE_FLT:
665     case TYPE_CODE_VOID:
666     case TYPE_CODE_SET:
667     case TYPE_CODE_STRING:
668     case TYPE_CODE_ERROR:
669     case TYPE_CODE_MEMBERPTR:
670     case TYPE_CODE_METHODPTR:
671     case TYPE_CODE_METHOD:
672     case TYPE_CODE_REF:
673     case TYPE_CODE_RVALUE_REF:
674     case TYPE_CODE_NAMESPACE:
675       error (_("internal error: unhandled type in print_type_scalar"));
676       break;
677 
678     default:
679       error (_("Invalid type code in symbol table."));
680     }
681 }
682 
683 /* See typeprint.h.  */
684 
685 void
print_type_fixed_point(struct type * type,struct ui_file * stream)686 print_type_fixed_point (struct type *type, struct ui_file *stream)
687 {
688   std::string small_img = type->fixed_point_scaling_factor ().str ();
689 
690   fprintf_filtered (stream, "%s-byte fixed point (small = %s)",
691 		    pulongest (TYPE_LENGTH (type)), small_img.c_str ());
692 }
693 
694 /* Dump details of a type specified either directly or indirectly.
695    Uses the same sort of type lookup mechanism as ptype_command()
696    and whatis_command().  */
697 
698 void
maintenance_print_type(const char * type_name,int from_tty)699 maintenance_print_type (const char *type_name, int from_tty)
700 {
701   if (type_name != NULL)
702     {
703       expression_up expr = parse_expression (type_name);
704       struct value *val = evaluate_type (expr.get ());
705       struct type *type = value_type (val);
706 
707       if (type != nullptr)
708 	recursive_dump_type (type, 0);
709     }
710 }
711 
712 
713 struct cmd_list_element *setprinttypelist;
714 
715 struct cmd_list_element *showprinttypelist;
716 
717 static bool print_methods = true;
718 
719 static void
set_print_type_methods(const char * args,int from_tty,struct cmd_list_element * c)720 set_print_type_methods (const char *args,
721 			int from_tty, struct cmd_list_element *c)
722 {
723   default_ptype_flags.print_methods = print_methods;
724 }
725 
726 static void
show_print_type_methods(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)727 show_print_type_methods (struct ui_file *file, int from_tty,
728 			 struct cmd_list_element *c, const char *value)
729 {
730   fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
731 		    value);
732 }
733 
734 static bool print_typedefs = true;
735 
736 static void
set_print_type_typedefs(const char * args,int from_tty,struct cmd_list_element * c)737 set_print_type_typedefs (const char *args,
738 			 int from_tty, struct cmd_list_element *c)
739 {
740   default_ptype_flags.print_typedefs = print_typedefs;
741 }
742 
743 static void
show_print_type_typedefs(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)744 show_print_type_typedefs (struct ui_file *file, int from_tty,
745 			 struct cmd_list_element *c, const char *value)
746 {
747   fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
748 		    value);
749 }
750 
751 /* Limit on the number of nested type definitions to print or -1 to print
752    all nested type definitions in a class.  By default, we do not print
753    nested definitions.  */
754 
755 static int print_nested_type_limit = 0;
756 
757 /* Set how many nested type definitions should be printed by the type
758    printer.  */
759 
760 static void
set_print_type_nested_types(const char * args,int from_tty,struct cmd_list_element * c)761 set_print_type_nested_types (const char *args, int from_tty,
762 			     struct cmd_list_element *c)
763 {
764   default_ptype_flags.print_nested_type_limit = print_nested_type_limit;
765 }
766 
767 /* Show how many nested type definitions the type printer will print.  */
768 
769 static void
show_print_type_nested_types(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)770 show_print_type_nested_types  (struct ui_file *file, int from_tty,
771 			       struct cmd_list_element *c, const char *value)
772 {
773   if (*value == '0')
774     {
775       fprintf_filtered (file,
776 			_("Will not print nested types defined in a class\n"));
777     }
778   else
779     {
780       fprintf_filtered (file,
781 			_("Will print %s nested types defined in a class\n"),
782 			value);
783     }
784 }
785 
786 /* When printing structs, offsets and sizes of members can be displayed using
787    decimal notation or hexadecimal notation.  By default, Decimal notation is
788    used.  */
789 
790 static bool print_offsets_and_sizes_in_hex = false;
791 
792 /* Set the flags that instructs if sizes and offsets of struct members are
793    displayed in hexadecimal or decimal notation.  */
794 
795 static void
set_print_offsets_and_sizes_in_hex(const char * args,int from_tty,struct cmd_list_element * c)796 set_print_offsets_and_sizes_in_hex (const char *args,
797 				    int from_tty, struct cmd_list_element *c)
798 {
799   default_ptype_flags.print_in_hex = print_offsets_and_sizes_in_hex;
800 }
801 
802 /* Display whether struct members sizes and offsets are printed
803    using decimal or hexadecimal notation.  */
804 
805 static void
show_print_offsets_and_sizes_in_hex(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)806 show_print_offsets_and_sizes_in_hex (struct ui_file *file, int from_tty,
807 				     struct cmd_list_element *c,
808 				     const char *value)
809 {
810   fprintf_filtered (file, _("\
811 Display of struct members offsets and sizes in hexadecimal is %s\n"),
812 		    value);
813 }
814 
815 void _initialize_typeprint ();
816 void
_initialize_typeprint()817 _initialize_typeprint ()
818 {
819   struct cmd_list_element *c;
820 
821   c = add_com ("ptype", class_vars, ptype_command, _("\
822 Print definition of type TYPE.\n\
823 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
824 Argument may be any type (for example a type name defined by typedef,\n\
825 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
826 or \"enum ENUM-TAG\") or an expression.\n\
827 The selected stack frame's lexical context is used to look up the name.\n\
828 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
829 \n\
830 Available FLAGS are:\n\
831   /r    print in \"raw\" form; do not substitute typedefs\n\
832   /m    do not print methods defined in a class\n\
833   /M    print methods defined in a class\n\
834   /t    do not print typedefs defined in a class\n\
835   /T    print typedefs defined in a class\n\
836   /o    print offsets and sizes of fields in a struct (like pahole)\n\
837   /x    use hexadecimal notation when displaying sizes and offsets\n\
838         of struct members\n\
839   /d    use decimal notation when displaying sizes and offsets\n\
840         of struct members "));
841   set_cmd_completer (c, expression_completer);
842 
843   c = add_com ("whatis", class_vars, whatis_command,
844 	       _("Print data type of expression EXP.\n\
845 Only one level of typedefs is unrolled.  See also \"ptype\"."));
846   set_cmd_completer (c, expression_completer);
847 
848   add_show_prefix_cmd ("type", no_class,
849 		       _("Generic command for showing type-printing settings."),
850 		       &showprinttypelist, 0, &showprintlist);
851   add_basic_prefix_cmd ("type", no_class,
852 			_("Generic command for setting how types print."),
853 			&setprinttypelist, 0, &setprintlist);
854 
855   add_setshow_boolean_cmd ("methods", no_class, &print_methods,
856 			   _("\
857 Set printing of methods defined in classes."), _("\
858 Show printing of methods defined in classes."), NULL,
859 			   set_print_type_methods,
860 			   show_print_type_methods,
861 			   &setprinttypelist, &showprinttypelist);
862   add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs,
863 			   _("\
864 Set printing of typedefs defined in classes."), _("\
865 Show printing of typedefs defined in classes."), NULL,
866 			   set_print_type_typedefs,
867 			   show_print_type_typedefs,
868 			   &setprinttypelist, &showprinttypelist);
869 
870   add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class,
871 				       &print_nested_type_limit,
872 				       _("\
873 Set the number of recursive nested type definitions to print \
874 (\"unlimited\" or -1 to show all)."), _("\
875 Show the number of recursive nested type definitions to print."), NULL,
876 				       set_print_type_nested_types,
877 				       show_print_type_nested_types,
878 				       &setprinttypelist, &showprinttypelist);
879 
880   add_setshow_boolean_cmd ("hex", no_class, &print_offsets_and_sizes_in_hex,
881 			   _("\
882 Set printing of struct members sizes and offsets using hex notation."), _("\
883 Show whether sizes and offsets of struct members are printed using hex \
884 notation."), nullptr, set_print_offsets_and_sizes_in_hex,
885 			   show_print_offsets_and_sizes_in_hex,
886 			   &setprinttypelist, &showprinttypelist);
887 }
888 
889 /* Print <not allocated> status to stream STREAM.  */
890 
891 void
val_print_not_allocated(struct ui_file * stream)892 val_print_not_allocated (struct ui_file *stream)
893 {
894   fprintf_styled (stream, metadata_style.style (), _("<not allocated>"));
895 }
896 
897 /* Print <not associated> status to stream STREAM.  */
898 
899 void
val_print_not_associated(struct ui_file * stream)900 val_print_not_associated (struct ui_file *stream)
901 {
902   fprintf_styled (stream, metadata_style.style (), _("<not associated>"));
903 }
904