c-valprint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "value.h"
  19. #include "valprint.h"
  20. #include "language.h"
  21. #include "c-lang.h"
  22. #include "cp-abi.h"
  23. #include "target.h"
  24. #include "objfiles.h"
  25. /* A helper for c_textual_element_type. This checks the name of the
  26. typedef. This is bogus but it isn't apparent that the compiler
  27. provides us the help we may need. */
  28. static int
  29. textual_name (const char *name)
  30. {
  31. return (!strcmp (name, "wchar_t")
  32. || !strcmp (name, "char16_t")
  33. || !strcmp (name, "char32_t"));
  34. }
  35. /* Apply a heuristic to decide whether an array of TYPE or a pointer
  36. to TYPE should be printed as a textual string. Return non-zero if
  37. it should, or zero if it should be treated as an array of integers
  38. or pointer to integers. FORMAT is the current format letter, or 0
  39. if none.
  40. We guess that "char" is a character. Explicitly signed and
  41. unsigned character types are also characters. Integer data from
  42. vector types is not. The user can override this by using the /s
  43. format letter. */
  44. int
  45. c_textual_element_type (struct type *type, char format)
  46. {
  47. struct type *true_type, *iter_type;
  48. if (format != 0 && format != 's')
  49. return 0;
  50. /* We also rely on this for its side effect of setting up all the
  51. typedef pointers. */
  52. true_type = check_typedef (type);
  53. /* TYPE_CODE_CHAR is always textual. */
  54. if (true_type->code () == TYPE_CODE_CHAR)
  55. return 1;
  56. /* Any other character-like types must be integral. */
  57. if (true_type->code () != TYPE_CODE_INT)
  58. return 0;
  59. /* We peel typedefs one by one, looking for a match. */
  60. iter_type = type;
  61. while (iter_type)
  62. {
  63. /* Check the name of the type. */
  64. if (iter_type->name () && textual_name (iter_type->name ()))
  65. return 1;
  66. if (iter_type->code () != TYPE_CODE_TYPEDEF)
  67. break;
  68. /* Peel a single typedef. If the typedef doesn't have a target
  69. type, we use check_typedef and hope the result is ok -- it
  70. might be for C++, where wchar_t is a built-in type. */
  71. if (TYPE_TARGET_TYPE (iter_type))
  72. iter_type = TYPE_TARGET_TYPE (iter_type);
  73. else
  74. iter_type = check_typedef (iter_type);
  75. }
  76. if (format == 's')
  77. {
  78. /* Print this as a string if we can manage it. For now, no wide
  79. character support. */
  80. if (true_type->code () == TYPE_CODE_INT
  81. && TYPE_LENGTH (true_type) == 1)
  82. return 1;
  83. }
  84. else
  85. {
  86. /* If a one-byte TYPE_CODE_INT is missing the not-a-character
  87. flag, then we treat it as text; otherwise, we assume it's
  88. being used as data. */
  89. if (true_type->code () == TYPE_CODE_INT
  90. && TYPE_LENGTH (true_type) == 1
  91. && !TYPE_NOTTEXT (true_type))
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. /* Decorations for C. */
  97. static const struct generic_val_print_decorations c_decorations =
  98. {
  99. "",
  100. " + ",
  101. "i",
  102. "true",
  103. "false",
  104. "void",
  105. "{",
  106. "}"
  107. };
  108. /* Print a pointer based on the type of its target.
  109. Arguments to this functions are roughly the same as those in c_val_print.
  110. A difference is that ADDRESS is the address to print, with embedded_offset
  111. already added. UNRESOLVED_ELTTYPE and ELTTYPE represent the pointed type,
  112. respectively before and after check_typedef. */
  113. static void
  114. print_unpacked_pointer (struct type *type, struct type *elttype,
  115. struct type *unresolved_elttype,
  116. const gdb_byte *valaddr, int embedded_offset,
  117. CORE_ADDR address, struct ui_file *stream, int recurse,
  118. const struct value_print_options *options)
  119. {
  120. int want_space = 0;
  121. struct gdbarch *gdbarch = type->arch ();
  122. if (elttype->code () == TYPE_CODE_FUNC)
  123. {
  124. /* Try to print what function it points to. */
  125. print_function_pointer_address (options, gdbarch, address, stream);
  126. return;
  127. }
  128. if (options->symbol_print)
  129. want_space = print_address_demangle (options, gdbarch, address, stream,
  130. demangle);
  131. else if (options->addressprint)
  132. {
  133. gdb_puts (paddress (gdbarch, address), stream);
  134. want_space = 1;
  135. }
  136. /* For a pointer to a textual type, also print the string
  137. pointed to, unless pointer is null. */
  138. if (c_textual_element_type (unresolved_elttype, options->format)
  139. && address != 0)
  140. {
  141. if (want_space)
  142. gdb_puts (" ", stream);
  143. val_print_string (unresolved_elttype, NULL, address, -1, stream, options);
  144. }
  145. else if (cp_is_vtbl_member (type))
  146. {
  147. /* Print vtbl's nicely. */
  148. CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
  149. struct bound_minimal_symbol msymbol =
  150. lookup_minimal_symbol_by_pc (vt_address);
  151. /* If 'symbol_print' is set, we did the work above. */
  152. if (!options->symbol_print
  153. && (msymbol.minsym != NULL)
  154. && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
  155. {
  156. if (want_space)
  157. gdb_puts (" ", stream);
  158. gdb_puts (" <", stream);
  159. gdb_puts (msymbol.minsym->print_name (), stream);
  160. gdb_puts (">", stream);
  161. want_space = 1;
  162. }
  163. if (vt_address && options->vtblprint)
  164. {
  165. struct value *vt_val;
  166. struct symbol *wsym = NULL;
  167. struct type *wtype;
  168. if (want_space)
  169. gdb_puts (" ", stream);
  170. if (msymbol.minsym != NULL)
  171. {
  172. const char *search_name = msymbol.minsym->search_name ();
  173. wsym = lookup_symbol_search_name (search_name, NULL,
  174. VAR_DOMAIN).symbol;
  175. }
  176. if (wsym)
  177. {
  178. wtype = wsym->type ();
  179. }
  180. else
  181. {
  182. wtype = unresolved_elttype;
  183. }
  184. vt_val = value_at (wtype, vt_address);
  185. common_val_print (vt_val, stream, recurse + 1, options,
  186. current_language);
  187. if (options->prettyformat)
  188. {
  189. gdb_printf (stream, "\n");
  190. print_spaces (2 + 2 * recurse, stream);
  191. }
  192. }
  193. }
  194. }
  195. /* c_value_print helper for TYPE_CODE_ARRAY. */
  196. static void
  197. c_value_print_array (struct value *val,
  198. struct ui_file *stream, int recurse,
  199. const struct value_print_options *options)
  200. {
  201. struct type *type = check_typedef (value_type (val));
  202. CORE_ADDR address = value_address (val);
  203. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  204. struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
  205. struct type *elttype = check_typedef (unresolved_elttype);
  206. if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
  207. {
  208. LONGEST low_bound, high_bound;
  209. int eltlen, len;
  210. enum bfd_endian byte_order = type_byte_order (type);
  211. if (!get_array_bounds (type, &low_bound, &high_bound))
  212. error (_("Could not determine the array high bound"));
  213. eltlen = TYPE_LENGTH (elttype);
  214. len = high_bound - low_bound + 1;
  215. /* Print arrays of textual chars with a string syntax, as
  216. long as the entire array is valid. */
  217. if (c_textual_element_type (unresolved_elttype,
  218. options->format)
  219. && value_bytes_available (val, 0, TYPE_LENGTH (type))
  220. && !value_bits_any_optimized_out (val, 0,
  221. TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  222. {
  223. int force_ellipses = 0;
  224. /* If requested, look for the first null char and only
  225. print elements up to it. */
  226. if (options->stop_print_at_null)
  227. {
  228. unsigned int temp_len;
  229. for (temp_len = 0;
  230. (temp_len < len
  231. && temp_len < options->print_max
  232. && extract_unsigned_integer (valaddr + temp_len * eltlen,
  233. eltlen, byte_order) != 0);
  234. ++temp_len)
  235. ;
  236. /* Force printstr to print ellipses if
  237. we've printed the maximum characters and
  238. the next character is not \000. */
  239. if (temp_len == options->print_max && temp_len < len)
  240. {
  241. ULONGEST ival
  242. = extract_unsigned_integer (valaddr + temp_len * eltlen,
  243. eltlen, byte_order);
  244. if (ival != 0)
  245. force_ellipses = 1;
  246. }
  247. len = temp_len;
  248. }
  249. current_language->printstr (stream, unresolved_elttype, valaddr, len,
  250. NULL, force_ellipses, options);
  251. }
  252. else
  253. {
  254. unsigned int i = 0;
  255. gdb_printf (stream, "{");
  256. /* If this is a virtual function table, print the 0th
  257. entry specially, and the rest of the members
  258. normally. */
  259. if (cp_is_vtbl_ptr_type (elttype))
  260. {
  261. i = 1;
  262. gdb_printf (stream, _("%d vtable entries"),
  263. len - 1);
  264. }
  265. value_print_array_elements (val, stream, recurse, options, i);
  266. gdb_printf (stream, "}");
  267. }
  268. }
  269. else
  270. {
  271. /* Array of unspecified length: treat like pointer to first elt. */
  272. print_unpacked_pointer (type, elttype, unresolved_elttype, valaddr,
  273. 0, address, stream, recurse, options);
  274. }
  275. }
  276. /* c_value_print_inner helper for TYPE_CODE_PTR. */
  277. static void
  278. c_value_print_ptr (struct value *val, struct ui_file *stream, int recurse,
  279. const struct value_print_options *options)
  280. {
  281. if (options->format && options->format != 's')
  282. {
  283. value_print_scalar_formatted (val, options, 0, stream);
  284. return;
  285. }
  286. struct type *type = check_typedef (value_type (val));
  287. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  288. if (options->vtblprint && cp_is_vtbl_ptr_type (type))
  289. {
  290. /* Print the unmangled name if desired. */
  291. /* Print vtable entry - we only get here if we ARE using
  292. -fvtable_thunks. (Otherwise, look under
  293. TYPE_CODE_STRUCT.) */
  294. CORE_ADDR addr = extract_typed_address (valaddr, type);
  295. print_function_pointer_address (options, type->arch (), addr, stream);
  296. }
  297. else
  298. {
  299. struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
  300. struct type *elttype = check_typedef (unresolved_elttype);
  301. CORE_ADDR addr = unpack_pointer (type, valaddr);
  302. print_unpacked_pointer (type, elttype, unresolved_elttype, valaddr,
  303. 0, addr, stream, recurse, options);
  304. }
  305. }
  306. /* c_value_print helper for TYPE_CODE_STRUCT and TYPE_CODE_UNION. */
  307. static void
  308. c_value_print_struct (struct value *val, struct ui_file *stream, int recurse,
  309. const struct value_print_options *options)
  310. {
  311. struct type *type = check_typedef (value_type (val));
  312. if (type->code () == TYPE_CODE_UNION && recurse && !options->unionprint)
  313. gdb_printf (stream, "{...}");
  314. else if (options->vtblprint && cp_is_vtbl_ptr_type (type))
  315. {
  316. /* Print the unmangled name if desired. */
  317. /* Print vtable entry - we only get here if NOT using
  318. -fvtable_thunks. (Otherwise, look under
  319. TYPE_CODE_PTR.) */
  320. int offset = type->field (VTBL_FNADDR_OFFSET).loc_bitpos () / 8;
  321. struct type *field_type = type->field (VTBL_FNADDR_OFFSET).type ();
  322. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  323. CORE_ADDR addr = extract_typed_address (valaddr + offset, field_type);
  324. print_function_pointer_address (options, type->arch (), addr, stream);
  325. }
  326. else
  327. cp_print_value_fields (val, stream, recurse, options, NULL, 0);
  328. }
  329. /* c_value_print helper for TYPE_CODE_INT. */
  330. static void
  331. c_value_print_int (struct value *val, struct ui_file *stream,
  332. const struct value_print_options *options)
  333. {
  334. if (options->format || options->output_format)
  335. {
  336. struct value_print_options opts = *options;
  337. opts.format = (options->format ? options->format
  338. : options->output_format);
  339. value_print_scalar_formatted (val, &opts, 0, stream);
  340. }
  341. else
  342. {
  343. value_print_scalar_formatted (val, options, 0, stream);
  344. /* C and C++ has no single byte int type, char is used
  345. instead. Since we don't know whether the value is really
  346. intended to be used as an integer or a character, print
  347. the character equivalent as well. */
  348. struct type *type = value_type (val);
  349. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  350. if (c_textual_element_type (type, options->format))
  351. {
  352. gdb_puts (" ", stream);
  353. current_language->printchar (unpack_long (type, valaddr), type,
  354. stream);
  355. }
  356. }
  357. }
  358. /* See c-lang.h. */
  359. void
  360. c_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
  361. const struct value_print_options *options)
  362. {
  363. struct type *type = value_type (val);
  364. type = check_typedef (type);
  365. switch (type->code ())
  366. {
  367. case TYPE_CODE_ARRAY:
  368. c_value_print_array (val, stream, recurse, options);
  369. break;
  370. case TYPE_CODE_PTR:
  371. c_value_print_ptr (val, stream, recurse, options);
  372. break;
  373. case TYPE_CODE_UNION:
  374. case TYPE_CODE_STRUCT:
  375. c_value_print_struct (val, stream, recurse, options);
  376. break;
  377. case TYPE_CODE_CHAR:
  378. case TYPE_CODE_INT:
  379. c_value_print_int (val, stream, options);
  380. break;
  381. case TYPE_CODE_METHODPTR:
  382. case TYPE_CODE_MEMBERPTR:
  383. case TYPE_CODE_REF:
  384. case TYPE_CODE_RVALUE_REF:
  385. case TYPE_CODE_ENUM:
  386. case TYPE_CODE_FLAGS:
  387. case TYPE_CODE_FUNC:
  388. case TYPE_CODE_METHOD:
  389. case TYPE_CODE_BOOL:
  390. case TYPE_CODE_RANGE:
  391. case TYPE_CODE_FLT:
  392. case TYPE_CODE_DECFLOAT:
  393. case TYPE_CODE_VOID:
  394. case TYPE_CODE_ERROR:
  395. case TYPE_CODE_UNDEF:
  396. case TYPE_CODE_COMPLEX:
  397. default:
  398. generic_value_print (val, stream, recurse, options, &c_decorations);
  399. break;
  400. }
  401. }
  402. void
  403. c_value_print (struct value *val, struct ui_file *stream,
  404. const struct value_print_options *options)
  405. {
  406. struct type *type, *real_type;
  407. int full, using_enc;
  408. LONGEST top;
  409. struct value_print_options opts = *options;
  410. opts.deref_ref = 1;
  411. /* If it is a pointer, indicate what it points to.
  412. Print type also if it is a reference.
  413. C++: if it is a member pointer, we will take care
  414. of that when we print it. */
  415. type = check_typedef (value_type (val));
  416. if (type->is_pointer_or_reference ())
  417. {
  418. struct type *original_type = value_type (val);
  419. /* Hack: remove (char *) for char strings. Their
  420. type is indicated by the quoted string anyway.
  421. (Don't use c_textual_element_type here; quoted strings
  422. are always exactly (char *), (wchar_t *), or the like. */
  423. if (original_type->code () == TYPE_CODE_PTR
  424. && original_type->name () == NULL
  425. && TYPE_TARGET_TYPE (original_type)->name () != NULL
  426. && (strcmp (TYPE_TARGET_TYPE (original_type)->name (),
  427. "char") == 0
  428. || textual_name (TYPE_TARGET_TYPE (original_type)->name ())))
  429. {
  430. /* Print nothing. */
  431. }
  432. else if (options->objectprint
  433. && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT))
  434. {
  435. int is_ref = TYPE_IS_REFERENCE (type);
  436. enum type_code refcode = TYPE_CODE_UNDEF;
  437. if (is_ref)
  438. {
  439. val = value_addr (val);
  440. refcode = type->code ();
  441. }
  442. /* Pointer to class, check real type of object. */
  443. gdb_printf (stream, "(");
  444. if (value_entirely_available (val))
  445. {
  446. real_type = value_rtti_indirect_type (val, &full, &top,
  447. &using_enc);
  448. if (real_type)
  449. {
  450. /* RTTI entry found. */
  451. /* Need to adjust pointer value. */
  452. val = value_from_pointer (real_type,
  453. value_as_address (val) - top);
  454. /* Note: When we look up RTTI entries, we don't get
  455. any information on const or volatile
  456. attributes. */
  457. }
  458. }
  459. if (is_ref)
  460. val = value_ref (value_ind (val), refcode);
  461. type = value_type (val);
  462. type_print (type, "", stream, -1);
  463. gdb_printf (stream, ") ");
  464. }
  465. else
  466. {
  467. /* normal case */
  468. gdb_printf (stream, "(");
  469. type_print (value_type (val), "", stream, -1);
  470. gdb_printf (stream, ") ");
  471. }
  472. }
  473. if (!value_initialized (val))
  474. gdb_printf (stream, " [uninitialized] ");
  475. if (options->objectprint && (type->code () == TYPE_CODE_STRUCT))
  476. {
  477. /* Attempt to determine real type of object. */
  478. real_type = value_rtti_type (val, &full, &top, &using_enc);
  479. if (real_type)
  480. {
  481. /* We have RTTI information, so use it. */
  482. val = value_full_object (val, real_type,
  483. full, top, using_enc);
  484. /* In a destructor we might see a real type that is a
  485. superclass of the object's type. In this case it is
  486. better to leave the object as-is. */
  487. if (!(full
  488. && (TYPE_LENGTH (real_type)
  489. < TYPE_LENGTH (value_enclosing_type (val)))))
  490. val = value_cast (real_type, val);
  491. gdb_printf (stream, "(%s%s) ",
  492. real_type->name (),
  493. full ? "" : _(" [incomplete object]"));
  494. }
  495. else if (type != check_typedef (value_enclosing_type (val)))
  496. {
  497. /* No RTTI information, so let's do our best. */
  498. gdb_printf (stream, "(%s ?) ",
  499. value_enclosing_type (val)->name ());
  500. val = value_cast (value_enclosing_type (val), val);
  501. }
  502. }
  503. common_val_print (val, stream, 0, &opts, current_language);
  504. }