p-valprint.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /* Support for printing Pascal values for GDB, the GNU debugger.
  2. Copyright (C) 2000-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. /* This file is derived from c-valprint.c */
  15. #include "defs.h"
  16. #include "gdbsupport/gdb_obstack.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "value.h"
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "gdbcore.h"
  24. #include "demangle.h"
  25. #include "valprint.h"
  26. #include "typeprint.h"
  27. #include "language.h"
  28. #include "target.h"
  29. #include "annotate.h"
  30. #include "p-lang.h"
  31. #include "cp-abi.h"
  32. #include "cp-support.h"
  33. #include "objfiles.h"
  34. #include "gdbsupport/byte-vector.h"
  35. #include "cli/cli-style.h"
  36. static void pascal_object_print_value_fields (struct value *, struct ui_file *,
  37. int,
  38. const struct value_print_options *,
  39. struct type **, int);
  40. /* Decorations for Pascal. */
  41. static const struct generic_val_print_decorations p_decorations =
  42. {
  43. "",
  44. " + ",
  45. " * I",
  46. "true",
  47. "false",
  48. "void",
  49. "{",
  50. "}"
  51. };
  52. /* See p-lang.h. */
  53. void
  54. pascal_language::value_print_inner (struct value *val,
  55. struct ui_file *stream, int recurse,
  56. const struct value_print_options *options) const
  57. {
  58. struct type *type = check_typedef (value_type (val));
  59. struct gdbarch *gdbarch = type->arch ();
  60. enum bfd_endian byte_order = type_byte_order (type);
  61. unsigned int i = 0; /* Number of characters printed */
  62. unsigned len;
  63. struct type *elttype;
  64. unsigned eltlen;
  65. int length_pos, length_size, string_pos;
  66. struct type *char_type;
  67. CORE_ADDR addr;
  68. int want_space = 0;
  69. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  70. switch (type->code ())
  71. {
  72. case TYPE_CODE_ARRAY:
  73. {
  74. LONGEST low_bound, high_bound;
  75. if (get_array_bounds (type, &low_bound, &high_bound))
  76. {
  77. len = high_bound - low_bound + 1;
  78. elttype = check_typedef (TYPE_TARGET_TYPE (type));
  79. eltlen = TYPE_LENGTH (elttype);
  80. /* If 's' format is used, try to print out as string.
  81. If no format is given, print as string if element type
  82. is of TYPE_CODE_CHAR and element size is 1,2 or 4. */
  83. if (options->format == 's'
  84. || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
  85. && elttype->code () == TYPE_CODE_CHAR
  86. && options->format == 0))
  87. {
  88. /* If requested, look for the first null char and only print
  89. elements up to it. */
  90. if (options->stop_print_at_null)
  91. {
  92. unsigned int temp_len;
  93. /* Look for a NULL char. */
  94. for (temp_len = 0;
  95. extract_unsigned_integer (valaddr + temp_len * eltlen,
  96. eltlen, byte_order)
  97. && temp_len < len && temp_len < options->print_max;
  98. temp_len++);
  99. len = temp_len;
  100. }
  101. printstr (stream, TYPE_TARGET_TYPE (type), valaddr, len,
  102. NULL, 0, options);
  103. i = len;
  104. }
  105. else
  106. {
  107. gdb_printf (stream, "{");
  108. /* If this is a virtual function table, print the 0th
  109. entry specially, and the rest of the members normally. */
  110. if (pascal_object_is_vtbl_ptr_type (elttype))
  111. {
  112. i = 1;
  113. gdb_printf (stream, "%d vtable entries", len - 1);
  114. }
  115. else
  116. {
  117. i = 0;
  118. }
  119. value_print_array_elements (val, stream, recurse, options, i);
  120. gdb_printf (stream, "}");
  121. }
  122. break;
  123. }
  124. /* Array of unspecified length: treat like pointer to first elt. */
  125. addr = value_address (val);
  126. }
  127. goto print_unpacked_pointer;
  128. case TYPE_CODE_PTR:
  129. if (options->format && options->format != 's')
  130. {
  131. value_print_scalar_formatted (val, options, 0, stream);
  132. break;
  133. }
  134. if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
  135. {
  136. /* Print the unmangled name if desired. */
  137. /* Print vtable entry - we only get here if we ARE using
  138. -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
  139. /* Extract the address, assume that it is unsigned. */
  140. addr = extract_unsigned_integer (valaddr,
  141. TYPE_LENGTH (type), byte_order);
  142. print_address_demangle (options, gdbarch, addr, stream, demangle);
  143. break;
  144. }
  145. check_typedef (TYPE_TARGET_TYPE (type));
  146. addr = unpack_pointer (type, valaddr);
  147. print_unpacked_pointer:
  148. elttype = check_typedef (TYPE_TARGET_TYPE (type));
  149. if (elttype->code () == TYPE_CODE_FUNC)
  150. {
  151. /* Try to print what function it points to. */
  152. print_address_demangle (options, gdbarch, addr, stream, demangle);
  153. return;
  154. }
  155. if (options->addressprint && options->format != 's')
  156. {
  157. gdb_puts (paddress (gdbarch, addr), stream);
  158. want_space = 1;
  159. }
  160. /* For a pointer to char or unsigned char, also print the string
  161. pointed to, unless pointer is null. */
  162. if (((TYPE_LENGTH (elttype) == 1
  163. && (elttype->code () == TYPE_CODE_INT
  164. || elttype->code () == TYPE_CODE_CHAR))
  165. || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
  166. && elttype->code () == TYPE_CODE_CHAR))
  167. && (options->format == 0 || options->format == 's')
  168. && addr != 0)
  169. {
  170. if (want_space)
  171. gdb_puts (" ", stream);
  172. /* No wide string yet. */
  173. i = val_print_string (elttype, NULL, addr, -1, stream, options);
  174. }
  175. /* Also for pointers to pascal strings. */
  176. /* Note: this is Free Pascal specific:
  177. as GDB does not recognize stabs pascal strings
  178. Pascal strings are mapped to records
  179. with lowercase names PM. */
  180. if (pascal_is_string_type (elttype, &length_pos, &length_size,
  181. &string_pos, &char_type, NULL) > 0
  182. && addr != 0)
  183. {
  184. ULONGEST string_length;
  185. gdb_byte *buffer;
  186. if (want_space)
  187. gdb_puts (" ", stream);
  188. buffer = (gdb_byte *) xmalloc (length_size);
  189. read_memory (addr + length_pos, buffer, length_size);
  190. string_length = extract_unsigned_integer (buffer, length_size,
  191. byte_order);
  192. xfree (buffer);
  193. i = val_print_string (char_type, NULL,
  194. addr + string_pos, string_length,
  195. stream, options);
  196. }
  197. else if (pascal_object_is_vtbl_member (type))
  198. {
  199. /* Print vtbl's nicely. */
  200. CORE_ADDR vt_address = unpack_pointer (type, valaddr);
  201. struct bound_minimal_symbol msymbol =
  202. lookup_minimal_symbol_by_pc (vt_address);
  203. /* If 'symbol_print' is set, we did the work above. */
  204. if (!options->symbol_print
  205. && (msymbol.minsym != NULL)
  206. && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
  207. {
  208. if (want_space)
  209. gdb_puts (" ", stream);
  210. gdb_puts ("<", stream);
  211. gdb_puts (msymbol.minsym->print_name (), stream);
  212. gdb_puts (">", stream);
  213. want_space = 1;
  214. }
  215. if (vt_address && options->vtblprint)
  216. {
  217. struct value *vt_val;
  218. struct symbol *wsym = NULL;
  219. struct type *wtype;
  220. if (want_space)
  221. gdb_puts (" ", stream);
  222. if (msymbol.minsym != NULL)
  223. {
  224. const char *search_name = msymbol.minsym->search_name ();
  225. wsym = lookup_symbol_search_name (search_name, NULL,
  226. VAR_DOMAIN).symbol;
  227. }
  228. if (wsym)
  229. {
  230. wtype = wsym->type ();
  231. }
  232. else
  233. {
  234. wtype = TYPE_TARGET_TYPE (type);
  235. }
  236. vt_val = value_at (wtype, vt_address);
  237. common_val_print (vt_val, stream, recurse + 1, options,
  238. current_language);
  239. if (options->prettyformat)
  240. {
  241. gdb_printf (stream, "\n");
  242. print_spaces (2 + 2 * recurse, stream);
  243. }
  244. }
  245. }
  246. return;
  247. case TYPE_CODE_REF:
  248. case TYPE_CODE_ENUM:
  249. case TYPE_CODE_FLAGS:
  250. case TYPE_CODE_FUNC:
  251. case TYPE_CODE_RANGE:
  252. case TYPE_CODE_INT:
  253. case TYPE_CODE_FLT:
  254. case TYPE_CODE_VOID:
  255. case TYPE_CODE_ERROR:
  256. case TYPE_CODE_UNDEF:
  257. case TYPE_CODE_BOOL:
  258. case TYPE_CODE_CHAR:
  259. generic_value_print (val, stream, recurse, options, &p_decorations);
  260. break;
  261. case TYPE_CODE_UNION:
  262. if (recurse && !options->unionprint)
  263. {
  264. gdb_printf (stream, "{...}");
  265. break;
  266. }
  267. /* Fall through. */
  268. case TYPE_CODE_STRUCT:
  269. if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
  270. {
  271. /* Print the unmangled name if desired. */
  272. /* Print vtable entry - we only get here if NOT using
  273. -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
  274. /* Extract the address, assume that it is unsigned. */
  275. print_address_demangle
  276. (options, gdbarch,
  277. extract_unsigned_integer
  278. (valaddr + type->field (VTBL_FNADDR_OFFSET).loc_bitpos () / 8,
  279. TYPE_LENGTH (type->field (VTBL_FNADDR_OFFSET).type ()),
  280. byte_order),
  281. stream, demangle);
  282. }
  283. else
  284. {
  285. if (pascal_is_string_type (type, &length_pos, &length_size,
  286. &string_pos, &char_type, NULL) > 0)
  287. {
  288. len = extract_unsigned_integer (valaddr + length_pos,
  289. length_size, byte_order);
  290. printstr (stream, char_type, valaddr + string_pos, len,
  291. NULL, 0, options);
  292. }
  293. else
  294. pascal_object_print_value_fields (val, stream, recurse,
  295. options, NULL, 0);
  296. }
  297. break;
  298. case TYPE_CODE_SET:
  299. elttype = type->index_type ();
  300. elttype = check_typedef (elttype);
  301. if (elttype->is_stub ())
  302. {
  303. fprintf_styled (stream, metadata_style.style (), "<incomplete type>");
  304. break;
  305. }
  306. else
  307. {
  308. struct type *range = elttype;
  309. LONGEST low_bound, high_bound;
  310. int need_comma = 0;
  311. gdb_puts ("[", stream);
  312. int bound_info = (get_discrete_bounds (range, &low_bound, &high_bound)
  313. ? 0 : -1);
  314. if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
  315. {
  316. /* If we know the size of the set type, we can figure out the
  317. maximum value. */
  318. bound_info = 0;
  319. high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
  320. range->bounds ()->high.set_const_val (high_bound);
  321. }
  322. maybe_bad_bstring:
  323. if (bound_info < 0)
  324. {
  325. fputs_styled ("<error value>", metadata_style.style (), stream);
  326. goto done;
  327. }
  328. for (i = low_bound; i <= high_bound; i++)
  329. {
  330. int element = value_bit_index (type, valaddr, i);
  331. if (element < 0)
  332. {
  333. i = element;
  334. goto maybe_bad_bstring;
  335. }
  336. if (element)
  337. {
  338. if (need_comma)
  339. gdb_puts (", ", stream);
  340. print_type_scalar (range, i, stream);
  341. need_comma = 1;
  342. if (i + 1 <= high_bound
  343. && value_bit_index (type, valaddr, ++i))
  344. {
  345. int j = i;
  346. gdb_puts ("..", stream);
  347. while (i + 1 <= high_bound
  348. && value_bit_index (type, valaddr, ++i))
  349. j = i;
  350. print_type_scalar (range, j, stream);
  351. }
  352. }
  353. }
  354. done:
  355. gdb_puts ("]", stream);
  356. }
  357. break;
  358. default:
  359. error (_("Invalid pascal type code %d in symbol table."),
  360. type->code ());
  361. }
  362. }
  363. void
  364. pascal_language::value_print (struct value *val, struct ui_file *stream,
  365. const struct value_print_options *options) const
  366. {
  367. struct type *type = value_type (val);
  368. struct value_print_options opts = *options;
  369. opts.deref_ref = 1;
  370. /* If it is a pointer, indicate what it points to.
  371. Print type also if it is a reference.
  372. Object pascal: if it is a member pointer, we will take care
  373. of that when we print it. */
  374. if (type->code () == TYPE_CODE_PTR
  375. || type->code () == TYPE_CODE_REF)
  376. {
  377. /* Hack: remove (char *) for char strings. Their
  378. type is indicated by the quoted string anyway. */
  379. if (type->code () == TYPE_CODE_PTR
  380. && type->name () == NULL
  381. && TYPE_TARGET_TYPE (type)->name () != NULL
  382. && strcmp (TYPE_TARGET_TYPE (type)->name (), "char") == 0)
  383. {
  384. /* Print nothing. */
  385. }
  386. else
  387. {
  388. gdb_printf (stream, "(");
  389. type_print (type, "", stream, -1);
  390. gdb_printf (stream, ") ");
  391. }
  392. }
  393. common_val_print (val, stream, 0, &opts, current_language);
  394. }
  395. static void
  396. show_pascal_static_field_print (struct ui_file *file, int from_tty,
  397. struct cmd_list_element *c, const char *value)
  398. {
  399. gdb_printf (file, _("Printing of pascal static members is %s.\n"),
  400. value);
  401. }
  402. static struct obstack dont_print_vb_obstack;
  403. static struct obstack dont_print_statmem_obstack;
  404. static void pascal_object_print_static_field (struct value *,
  405. struct ui_file *, int,
  406. const struct value_print_options *);
  407. static void pascal_object_print_value (struct value *, struct ui_file *, int,
  408. const struct value_print_options *,
  409. struct type **);
  410. /* It was changed to this after 2.4.5. */
  411. const char pascal_vtbl_ptr_name[] =
  412. {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
  413. /* Return truth value for assertion that TYPE is of the type
  414. "pointer to virtual function". */
  415. int
  416. pascal_object_is_vtbl_ptr_type (struct type *type)
  417. {
  418. const char *type_name = type->name ();
  419. return (type_name != NULL
  420. && strcmp (type_name, pascal_vtbl_ptr_name) == 0);
  421. }
  422. /* Return truth value for the assertion that TYPE is of the type
  423. "pointer to virtual function table". */
  424. int
  425. pascal_object_is_vtbl_member (struct type *type)
  426. {
  427. if (type->code () == TYPE_CODE_PTR)
  428. {
  429. type = TYPE_TARGET_TYPE (type);
  430. if (type->code () == TYPE_CODE_ARRAY)
  431. {
  432. type = TYPE_TARGET_TYPE (type);
  433. if (type->code () == TYPE_CODE_STRUCT /* If not using
  434. thunks. */
  435. || type->code () == TYPE_CODE_PTR) /* If using thunks. */
  436. {
  437. /* Virtual functions tables are full of pointers
  438. to virtual functions. */
  439. return pascal_object_is_vtbl_ptr_type (type);
  440. }
  441. }
  442. }
  443. return 0;
  444. }
  445. /* Helper function for print pascal objects.
  446. VAL, STREAM, RECURSE, and OPTIONS have the same meanings as in
  447. pascal_object_print_value and c_value_print.
  448. DONT_PRINT is an array of baseclass types that we
  449. should not print, or zero if called from top level. */
  450. static void
  451. pascal_object_print_value_fields (struct value *val, struct ui_file *stream,
  452. int recurse,
  453. const struct value_print_options *options,
  454. struct type **dont_print_vb,
  455. int dont_print_statmem)
  456. {
  457. int i, len, n_baseclasses;
  458. char *last_dont_print
  459. = (char *) obstack_next_free (&dont_print_statmem_obstack);
  460. struct type *type = check_typedef (value_type (val));
  461. gdb_printf (stream, "{");
  462. len = type->num_fields ();
  463. n_baseclasses = TYPE_N_BASECLASSES (type);
  464. /* Print out baseclasses such that we don't print
  465. duplicates of virtual baseclasses. */
  466. if (n_baseclasses > 0)
  467. pascal_object_print_value (val, stream, recurse + 1,
  468. options, dont_print_vb);
  469. if (!len && n_baseclasses == 1)
  470. fprintf_styled (stream, metadata_style.style (), "<No data fields>");
  471. else
  472. {
  473. struct obstack tmp_obstack = dont_print_statmem_obstack;
  474. int fields_seen = 0;
  475. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  476. if (dont_print_statmem == 0)
  477. {
  478. /* If we're at top level, carve out a completely fresh
  479. chunk of the obstack and use that until this particular
  480. invocation returns. */
  481. obstack_finish (&dont_print_statmem_obstack);
  482. }
  483. for (i = n_baseclasses; i < len; i++)
  484. {
  485. /* If requested, skip printing of static fields. */
  486. if (!options->pascal_static_field_print
  487. && field_is_static (&type->field (i)))
  488. continue;
  489. if (fields_seen)
  490. gdb_printf (stream, ", ");
  491. else if (n_baseclasses > 0)
  492. {
  493. if (options->prettyformat)
  494. {
  495. gdb_printf (stream, "\n");
  496. print_spaces (2 + 2 * recurse, stream);
  497. gdb_puts ("members of ", stream);
  498. gdb_puts (type->name (), stream);
  499. gdb_puts (": ", stream);
  500. }
  501. }
  502. fields_seen = 1;
  503. if (options->prettyformat)
  504. {
  505. gdb_printf (stream, "\n");
  506. print_spaces (2 + 2 * recurse, stream);
  507. }
  508. else
  509. {
  510. stream->wrap_here (2 + 2 * recurse);
  511. }
  512. annotate_field_begin (type->field (i).type ());
  513. if (field_is_static (&type->field (i)))
  514. {
  515. gdb_puts ("static ", stream);
  516. fprintf_symbol (stream,
  517. type->field (i).name (),
  518. current_language->la_language,
  519. DMGL_PARAMS | DMGL_ANSI);
  520. }
  521. else
  522. fputs_styled (type->field (i).name (),
  523. variable_name_style.style (), stream);
  524. annotate_field_name_end ();
  525. gdb_puts (" = ", stream);
  526. annotate_field_value ();
  527. if (!field_is_static (&type->field (i))
  528. && TYPE_FIELD_PACKED (type, i))
  529. {
  530. struct value *v;
  531. /* Bitfields require special handling, especially due to byte
  532. order problems. */
  533. if (TYPE_FIELD_IGNORE (type, i))
  534. {
  535. fputs_styled ("<optimized out or zero length>",
  536. metadata_style.style (), stream);
  537. }
  538. else if (value_bits_synthetic_pointer
  539. (val, type->field (i).loc_bitpos (),
  540. TYPE_FIELD_BITSIZE (type, i)))
  541. {
  542. fputs_styled (_("<synthetic pointer>"),
  543. metadata_style.style (), stream);
  544. }
  545. else
  546. {
  547. struct value_print_options opts = *options;
  548. v = value_field_bitfield (type, i, valaddr, 0, val);
  549. opts.deref_ref = 0;
  550. common_val_print (v, stream, recurse + 1, &opts,
  551. current_language);
  552. }
  553. }
  554. else
  555. {
  556. if (TYPE_FIELD_IGNORE (type, i))
  557. {
  558. fputs_styled ("<optimized out or zero length>",
  559. metadata_style.style (), stream);
  560. }
  561. else if (field_is_static (&type->field (i)))
  562. {
  563. /* struct value *v = value_static_field (type, i);
  564. v4.17 specific. */
  565. struct value *v;
  566. v = value_field_bitfield (type, i, valaddr, 0, val);
  567. if (v == NULL)
  568. val_print_optimized_out (NULL, stream);
  569. else
  570. pascal_object_print_static_field (v, stream, recurse + 1,
  571. options);
  572. }
  573. else
  574. {
  575. struct value_print_options opts = *options;
  576. opts.deref_ref = 0;
  577. struct value *v = value_primitive_field (val, 0, i,
  578. value_type (val));
  579. common_val_print (v, stream, recurse + 1, &opts,
  580. current_language);
  581. }
  582. }
  583. annotate_field_end ();
  584. }
  585. if (dont_print_statmem == 0)
  586. {
  587. /* Free the space used to deal with the printing
  588. of the members from top level. */
  589. obstack_free (&dont_print_statmem_obstack, last_dont_print);
  590. dont_print_statmem_obstack = tmp_obstack;
  591. }
  592. if (options->prettyformat)
  593. {
  594. gdb_printf (stream, "\n");
  595. print_spaces (2 * recurse, stream);
  596. }
  597. }
  598. gdb_printf (stream, "}");
  599. }
  600. /* Special val_print routine to avoid printing multiple copies of virtual
  601. baseclasses. */
  602. static void
  603. pascal_object_print_value (struct value *val, struct ui_file *stream,
  604. int recurse,
  605. const struct value_print_options *options,
  606. struct type **dont_print_vb)
  607. {
  608. struct type **last_dont_print
  609. = (struct type **) obstack_next_free (&dont_print_vb_obstack);
  610. struct obstack tmp_obstack = dont_print_vb_obstack;
  611. struct type *type = check_typedef (value_type (val));
  612. int i, n_baseclasses = TYPE_N_BASECLASSES (type);
  613. if (dont_print_vb == 0)
  614. {
  615. /* If we're at top level, carve out a completely fresh
  616. chunk of the obstack and use that until this particular
  617. invocation returns. */
  618. /* Bump up the high-water mark. Now alpha is omega. */
  619. obstack_finish (&dont_print_vb_obstack);
  620. }
  621. for (i = 0; i < n_baseclasses; i++)
  622. {
  623. LONGEST boffset = 0;
  624. struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  625. const char *basename = baseclass->name ();
  626. int skip = 0;
  627. if (BASETYPE_VIA_VIRTUAL (type, i))
  628. {
  629. struct type **first_dont_print
  630. = (struct type **) obstack_base (&dont_print_vb_obstack);
  631. int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
  632. - first_dont_print;
  633. while (--j >= 0)
  634. if (baseclass == first_dont_print[j])
  635. goto flush_it;
  636. obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
  637. }
  638. struct value *base_value;
  639. try
  640. {
  641. base_value = value_primitive_field (val, 0, i, type);
  642. }
  643. catch (const gdb_exception_error &ex)
  644. {
  645. base_value = nullptr;
  646. if (ex.error == NOT_AVAILABLE_ERROR)
  647. skip = -1;
  648. else
  649. skip = 1;
  650. }
  651. if (skip == 0)
  652. {
  653. /* The virtual base class pointer might have been clobbered by the
  654. user program. Make sure that it still points to a valid memory
  655. location. */
  656. if (boffset < 0 || boffset >= TYPE_LENGTH (type))
  657. {
  658. CORE_ADDR address= value_address (val);
  659. gdb::byte_vector buf (TYPE_LENGTH (baseclass));
  660. if (target_read_memory (address + boffset, buf.data (),
  661. TYPE_LENGTH (baseclass)) != 0)
  662. skip = 1;
  663. base_value = value_from_contents_and_address (baseclass,
  664. buf.data (),
  665. address + boffset);
  666. baseclass = value_type (base_value);
  667. boffset = 0;
  668. }
  669. }
  670. if (options->prettyformat)
  671. {
  672. gdb_printf (stream, "\n");
  673. print_spaces (2 * recurse, stream);
  674. }
  675. gdb_puts ("<", stream);
  676. /* Not sure what the best notation is in the case where there is no
  677. baseclass name. */
  678. gdb_puts (basename ? basename : "", stream);
  679. gdb_puts ("> = ", stream);
  680. if (skip < 0)
  681. val_print_unavailable (stream);
  682. else if (skip > 0)
  683. val_print_invalid_address (stream);
  684. else
  685. pascal_object_print_value_fields
  686. (base_value, stream, recurse, options,
  687. (struct type **) obstack_base (&dont_print_vb_obstack),
  688. 0);
  689. gdb_puts (", ", stream);
  690. flush_it:
  691. ;
  692. }
  693. if (dont_print_vb == 0)
  694. {
  695. /* Free the space used to deal with the printing
  696. of this type from top level. */
  697. obstack_free (&dont_print_vb_obstack, last_dont_print);
  698. /* Reset watermark so that we can continue protecting
  699. ourselves from whatever we were protecting ourselves. */
  700. dont_print_vb_obstack = tmp_obstack;
  701. }
  702. }
  703. /* Print value of a static member.
  704. To avoid infinite recursion when printing a class that contains
  705. a static instance of the class, we keep the addresses of all printed
  706. static member classes in an obstack and refuse to print them more
  707. than once.
  708. VAL contains the value to print, STREAM, RECURSE, and OPTIONS
  709. have the same meanings as in c_val_print. */
  710. static void
  711. pascal_object_print_static_field (struct value *val,
  712. struct ui_file *stream,
  713. int recurse,
  714. const struct value_print_options *options)
  715. {
  716. struct type *type = value_type (val);
  717. struct value_print_options opts;
  718. if (value_entirely_optimized_out (val))
  719. {
  720. val_print_optimized_out (val, stream);
  721. return;
  722. }
  723. if (type->code () == TYPE_CODE_STRUCT)
  724. {
  725. CORE_ADDR *first_dont_print, addr;
  726. int i;
  727. first_dont_print
  728. = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
  729. i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
  730. - first_dont_print;
  731. while (--i >= 0)
  732. {
  733. if (value_address (val) == first_dont_print[i])
  734. {
  735. fputs_styled (_("\
  736. <same as static member of an already seen type>"),
  737. metadata_style.style (), stream);
  738. return;
  739. }
  740. }
  741. addr = value_address (val);
  742. obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
  743. sizeof (CORE_ADDR));
  744. type = check_typedef (type);
  745. pascal_object_print_value_fields (val, stream, recurse,
  746. options, NULL, 1);
  747. return;
  748. }
  749. opts = *options;
  750. opts.deref_ref = 0;
  751. common_val_print (val, stream, recurse, &opts, current_language);
  752. }
  753. void _initialize_pascal_valprint ();
  754. void
  755. _initialize_pascal_valprint ()
  756. {
  757. add_setshow_boolean_cmd ("pascal_static-members", class_support,
  758. &user_print_options.pascal_static_field_print, _("\
  759. Set printing of pascal static members."), _("\
  760. Show printing of pascal static members."), NULL,
  761. NULL,
  762. show_pascal_static_field_print,
  763. &setprintlist, &showprintlist);
  764. }