typeprint.c 24 KB

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