cp-valprint.c 22 KB

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