ada-valprint.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /* Support for printing Ada 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 <ctype.h>
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "value.h"
  19. #include "valprint.h"
  20. #include "language.h"
  21. #include "annotate.h"
  22. #include "ada-lang.h"
  23. #include "target-float.h"
  24. #include "cli/cli-style.h"
  25. #include "gdbarch.h"
  26. static int print_field_values (struct value *, struct value *,
  27. struct ui_file *, int,
  28. const struct value_print_options *,
  29. int, const struct language_defn *);
  30. /* Make TYPE unsigned if its range of values includes no negatives. */
  31. static void
  32. adjust_type_signedness (struct type *type)
  33. {
  34. if (type != NULL && type->code () == TYPE_CODE_RANGE
  35. && type->bounds ()->low.const_val () >= 0)
  36. type->set_is_unsigned (true);
  37. }
  38. /* Assuming TYPE is a simple array type, prints its lower bound on STREAM,
  39. if non-standard (i.e., other than 1 for numbers, other than lower bound
  40. of index type for enumerated type). Returns 1 if something printed,
  41. otherwise 0. */
  42. static int
  43. print_optional_low_bound (struct ui_file *stream, struct type *type,
  44. const struct value_print_options *options)
  45. {
  46. struct type *index_type;
  47. LONGEST low_bound;
  48. LONGEST high_bound;
  49. if (options->print_array_indexes)
  50. return 0;
  51. if (!get_array_bounds (type, &low_bound, &high_bound))
  52. return 0;
  53. /* If this is an empty array, then don't print the lower bound.
  54. That would be confusing, because we would print the lower bound,
  55. followed by... nothing! */
  56. if (low_bound > high_bound)
  57. return 0;
  58. index_type = type->index_type ();
  59. while (index_type->code () == TYPE_CODE_RANGE)
  60. {
  61. /* We need to know what the base type is, in order to do the
  62. appropriate check below. Otherwise, if this is a subrange
  63. of an enumerated type, where the underlying value of the
  64. first element is typically 0, we might test the low bound
  65. against the wrong value. */
  66. index_type = TYPE_TARGET_TYPE (index_type);
  67. }
  68. /* Don't print the lower bound if it's the default one. */
  69. switch (index_type->code ())
  70. {
  71. case TYPE_CODE_BOOL:
  72. case TYPE_CODE_CHAR:
  73. if (low_bound == 0)
  74. return 0;
  75. break;
  76. case TYPE_CODE_ENUM:
  77. if (low_bound == 0)
  78. return 0;
  79. low_bound = index_type->field (low_bound).loc_enumval ();
  80. break;
  81. case TYPE_CODE_UNDEF:
  82. index_type = NULL;
  83. /* FALL THROUGH */
  84. default:
  85. if (low_bound == 1)
  86. return 0;
  87. break;
  88. }
  89. ada_print_scalar (index_type, low_bound, stream);
  90. gdb_printf (stream, " => ");
  91. return 1;
  92. }
  93. /* Version of val_print_array_elements for GNAT-style packed arrays.
  94. Prints elements of packed array of type TYPE from VALADDR on
  95. STREAM. Formats according to OPTIONS and separates with commas.
  96. RECURSE is the recursion (nesting) level. TYPE must have been
  97. decoded (as by ada_coerce_to_simple_array). */
  98. static void
  99. val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
  100. int offset, struct ui_file *stream,
  101. int recurse,
  102. const struct value_print_options *options)
  103. {
  104. unsigned int i;
  105. unsigned int things_printed = 0;
  106. unsigned len;
  107. struct type *elttype, *index_type;
  108. unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
  109. struct value *mark = value_mark ();
  110. LONGEST low = 0;
  111. elttype = TYPE_TARGET_TYPE (type);
  112. index_type = type->index_type ();
  113. {
  114. LONGEST high;
  115. if (!get_discrete_bounds (index_type, &low, &high))
  116. len = 1;
  117. else if (low > high)
  118. {
  119. /* The array length should normally be HIGH_POS - LOW_POS + 1.
  120. But in Ada we allow LOW_POS to be greater than HIGH_POS for
  121. empty arrays. In that situation, the array length is just zero,
  122. not negative! */
  123. len = 0;
  124. }
  125. else
  126. len = high - low + 1;
  127. }
  128. if (index_type->code () == TYPE_CODE_RANGE)
  129. index_type = TYPE_TARGET_TYPE (index_type);
  130. i = 0;
  131. annotate_array_section_begin (i, elttype);
  132. while (i < len && things_printed < options->print_max)
  133. {
  134. struct value *v0, *v1;
  135. int i0;
  136. if (i != 0)
  137. {
  138. if (options->prettyformat_arrays)
  139. {
  140. gdb_printf (stream, ",\n");
  141. print_spaces (2 + 2 * recurse, stream);
  142. }
  143. else
  144. {
  145. gdb_printf (stream, ", ");
  146. }
  147. }
  148. else if (options->prettyformat_arrays)
  149. {
  150. gdb_printf (stream, "\n");
  151. print_spaces (2 + 2 * recurse, stream);
  152. }
  153. stream->wrap_here (2 + 2 * recurse);
  154. maybe_print_array_index (index_type, i + low, stream, options);
  155. i0 = i;
  156. v0 = ada_value_primitive_packed_val (NULL, valaddr + offset,
  157. (i0 * bitsize) / HOST_CHAR_BIT,
  158. (i0 * bitsize) % HOST_CHAR_BIT,
  159. bitsize, elttype);
  160. while (1)
  161. {
  162. i += 1;
  163. if (i >= len)
  164. break;
  165. v1 = ada_value_primitive_packed_val (NULL, valaddr + offset,
  166. (i * bitsize) / HOST_CHAR_BIT,
  167. (i * bitsize) % HOST_CHAR_BIT,
  168. bitsize, elttype);
  169. if (TYPE_LENGTH (check_typedef (value_type (v0)))
  170. != TYPE_LENGTH (check_typedef (value_type (v1))))
  171. break;
  172. if (!value_contents_eq (v0, value_embedded_offset (v0),
  173. v1, value_embedded_offset (v1),
  174. TYPE_LENGTH (check_typedef (value_type (v0)))))
  175. break;
  176. }
  177. if (i - i0 > options->repeat_count_threshold)
  178. {
  179. struct value_print_options opts = *options;
  180. opts.deref_ref = 0;
  181. common_val_print (v0, stream, recurse + 1, &opts, current_language);
  182. annotate_elt_rep (i - i0);
  183. gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
  184. metadata_style.style ().ptr (), i - i0, nullptr);
  185. annotate_elt_rep_end ();
  186. }
  187. else
  188. {
  189. int j;
  190. struct value_print_options opts = *options;
  191. opts.deref_ref = 0;
  192. for (j = i0; j < i; j += 1)
  193. {
  194. if (j > i0)
  195. {
  196. if (options->prettyformat_arrays)
  197. {
  198. gdb_printf (stream, ",\n");
  199. print_spaces (2 + 2 * recurse, stream);
  200. }
  201. else
  202. {
  203. gdb_printf (stream, ", ");
  204. }
  205. stream->wrap_here (2 + 2 * recurse);
  206. maybe_print_array_index (index_type, j + low,
  207. stream, options);
  208. }
  209. common_val_print (v0, stream, recurse + 1, &opts,
  210. current_language);
  211. annotate_elt ();
  212. }
  213. }
  214. things_printed += i - i0;
  215. }
  216. annotate_array_section_end ();
  217. if (i < len)
  218. {
  219. gdb_printf (stream, "...");
  220. }
  221. value_free_to_mark (mark);
  222. }
  223. /* Print the character C on STREAM as part of the contents of a literal
  224. string whose delimiter is QUOTER. TYPE_LEN is the length in bytes
  225. of the character. */
  226. void
  227. ada_emit_char (int c, struct type *type, struct ui_file *stream,
  228. int quoter, int type_len)
  229. {
  230. /* If this character fits in the normal ASCII range, and is
  231. a printable character, then print the character as if it was
  232. an ASCII character, even if this is a wide character.
  233. The UCHAR_MAX check is necessary because the isascii function
  234. requires that its argument have a value of an unsigned char,
  235. or EOF (EOF is obviously not printable). */
  236. if (c <= UCHAR_MAX && isascii (c) && isprint (c))
  237. {
  238. if (c == quoter && c == '"')
  239. gdb_printf (stream, "\"\"");
  240. else
  241. gdb_printf (stream, "%c", c);
  242. }
  243. else
  244. {
  245. /* Follow GNAT's lead here and only use 6 digits for
  246. wide_wide_character. */
  247. gdb_printf (stream, "[\"%0*x\"]", std::min (6, type_len * 2), c);
  248. }
  249. }
  250. /* Character #I of STRING, given that TYPE_LEN is the size in bytes
  251. of a character. */
  252. static int
  253. char_at (const gdb_byte *string, int i, int type_len,
  254. enum bfd_endian byte_order)
  255. {
  256. if (type_len == 1)
  257. return string[i];
  258. else
  259. return (int) extract_unsigned_integer (string + type_len * i,
  260. type_len, byte_order);
  261. }
  262. /* Print a floating-point value of type TYPE, pointed to in GDB by
  263. VALADDR, on STREAM. Use Ada formatting conventions: there must be
  264. a decimal point, and at least one digit before and after the
  265. point. We use the GNAT format for NaNs and infinities. */
  266. static void
  267. ada_print_floating (const gdb_byte *valaddr, struct type *type,
  268. struct ui_file *stream)
  269. {
  270. string_file tmp_stream;
  271. print_floating (valaddr, type, &tmp_stream);
  272. std::string s = tmp_stream.release ();
  273. size_t skip_count = 0;
  274. /* Don't try to modify a result representing an error. */
  275. if (s[0] == '<')
  276. {
  277. gdb_puts (s.c_str (), stream);
  278. return;
  279. }
  280. /* Modify for Ada rules. */
  281. size_t pos = s.find ("inf");
  282. if (pos == std::string::npos)
  283. pos = s.find ("Inf");
  284. if (pos == std::string::npos)
  285. pos = s.find ("INF");
  286. if (pos != std::string::npos)
  287. s.replace (pos, 3, "Inf");
  288. if (pos == std::string::npos)
  289. {
  290. pos = s.find ("nan");
  291. if (pos == std::string::npos)
  292. pos = s.find ("NaN");
  293. if (pos == std::string::npos)
  294. pos = s.find ("Nan");
  295. if (pos != std::string::npos)
  296. {
  297. s[pos] = s[pos + 2] = 'N';
  298. if (s[0] == '-')
  299. skip_count = 1;
  300. }
  301. }
  302. if (pos == std::string::npos
  303. && s.find ('.') == std::string::npos)
  304. {
  305. pos = s.find ('e');
  306. if (pos == std::string::npos)
  307. gdb_printf (stream, "%s.0", s.c_str ());
  308. else
  309. gdb_printf (stream, "%.*s.0%s", (int) pos, s.c_str (), &s[pos]);
  310. }
  311. else
  312. gdb_printf (stream, "%s", &s[skip_count]);
  313. }
  314. void
  315. ada_printchar (int c, struct type *type, struct ui_file *stream)
  316. {
  317. gdb_puts ("'", stream);
  318. ada_emit_char (c, type, stream, '\'', TYPE_LENGTH (type));
  319. gdb_puts ("'", stream);
  320. }
  321. /* [From print_type_scalar in typeprint.c]. Print VAL on STREAM in a
  322. form appropriate for TYPE, if non-NULL. If TYPE is NULL, print VAL
  323. like a default signed integer. */
  324. void
  325. ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
  326. {
  327. unsigned int i;
  328. unsigned len;
  329. if (!type)
  330. {
  331. print_longest (stream, 'd', 0, val);
  332. return;
  333. }
  334. type = ada_check_typedef (type);
  335. switch (type->code ())
  336. {
  337. case TYPE_CODE_ENUM:
  338. len = type->num_fields ();
  339. for (i = 0; i < len; i++)
  340. {
  341. if (type->field (i).loc_enumval () == val)
  342. {
  343. break;
  344. }
  345. }
  346. if (i < len)
  347. {
  348. fputs_styled (ada_enum_name (type->field (i).name ()),
  349. variable_name_style.style (), stream);
  350. }
  351. else
  352. {
  353. print_longest (stream, 'd', 0, val);
  354. }
  355. break;
  356. case TYPE_CODE_INT:
  357. print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val);
  358. break;
  359. case TYPE_CODE_CHAR:
  360. current_language->printchar (val, type, stream);
  361. break;
  362. case TYPE_CODE_BOOL:
  363. gdb_printf (stream, val ? "true" : "false");
  364. break;
  365. case TYPE_CODE_RANGE:
  366. ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
  367. return;
  368. case TYPE_CODE_UNDEF:
  369. case TYPE_CODE_PTR:
  370. case TYPE_CODE_ARRAY:
  371. case TYPE_CODE_STRUCT:
  372. case TYPE_CODE_UNION:
  373. case TYPE_CODE_FUNC:
  374. case TYPE_CODE_FLT:
  375. case TYPE_CODE_VOID:
  376. case TYPE_CODE_SET:
  377. case TYPE_CODE_STRING:
  378. case TYPE_CODE_ERROR:
  379. case TYPE_CODE_MEMBERPTR:
  380. case TYPE_CODE_METHODPTR:
  381. case TYPE_CODE_METHOD:
  382. case TYPE_CODE_REF:
  383. warning (_("internal error: unhandled type in ada_print_scalar"));
  384. break;
  385. default:
  386. error (_("Invalid type code in symbol table."));
  387. }
  388. }
  389. /* Print the character string STRING, printing at most LENGTH characters.
  390. Printing stops early if the number hits print_max; repeat counts
  391. are printed as appropriate. Print ellipses at the end if we
  392. had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
  393. TYPE_LEN is the length (1 or 2) of the character type. */
  394. static void
  395. printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
  396. unsigned int length, int force_ellipses, int type_len,
  397. const struct value_print_options *options)
  398. {
  399. enum bfd_endian byte_order = type_byte_order (elttype);
  400. unsigned int i;
  401. unsigned int things_printed = 0;
  402. int in_quotes = 0;
  403. int need_comma = 0;
  404. if (length == 0)
  405. {
  406. gdb_puts ("\"\"", stream);
  407. return;
  408. }
  409. for (i = 0; i < length && things_printed < options->print_max; i += 1)
  410. {
  411. /* Position of the character we are examining
  412. to see whether it is repeated. */
  413. unsigned int rep1;
  414. /* Number of repetitions we have detected so far. */
  415. unsigned int reps;
  416. QUIT;
  417. if (need_comma)
  418. {
  419. gdb_puts (", ", stream);
  420. need_comma = 0;
  421. }
  422. rep1 = i + 1;
  423. reps = 1;
  424. while (rep1 < length
  425. && char_at (string, rep1, type_len, byte_order)
  426. == char_at (string, i, type_len, byte_order))
  427. {
  428. rep1 += 1;
  429. reps += 1;
  430. }
  431. if (reps > options->repeat_count_threshold)
  432. {
  433. if (in_quotes)
  434. {
  435. gdb_puts ("\", ", stream);
  436. in_quotes = 0;
  437. }
  438. gdb_puts ("'", stream);
  439. ada_emit_char (char_at (string, i, type_len, byte_order),
  440. elttype, stream, '\'', type_len);
  441. gdb_puts ("'", stream);
  442. gdb_printf (stream, _(" %p[<repeats %u times>%p]"),
  443. metadata_style.style ().ptr (), reps, nullptr);
  444. i = rep1 - 1;
  445. things_printed += options->repeat_count_threshold;
  446. need_comma = 1;
  447. }
  448. else
  449. {
  450. if (!in_quotes)
  451. {
  452. gdb_puts ("\"", stream);
  453. in_quotes = 1;
  454. }
  455. ada_emit_char (char_at (string, i, type_len, byte_order),
  456. elttype, stream, '"', type_len);
  457. things_printed += 1;
  458. }
  459. }
  460. /* Terminate the quotes if necessary. */
  461. if (in_quotes)
  462. gdb_puts ("\"", stream);
  463. if (force_ellipses || i < length)
  464. gdb_puts ("...", stream);
  465. }
  466. void
  467. ada_printstr (struct ui_file *stream, struct type *type,
  468. const gdb_byte *string, unsigned int length,
  469. const char *encoding, int force_ellipses,
  470. const struct value_print_options *options)
  471. {
  472. printstr (stream, type, string, length, force_ellipses, TYPE_LENGTH (type),
  473. options);
  474. }
  475. static int
  476. print_variant_part (struct value *value, int field_num,
  477. struct value *outer_value,
  478. struct ui_file *stream, int recurse,
  479. const struct value_print_options *options,
  480. int comma_needed,
  481. const struct language_defn *language)
  482. {
  483. struct type *type = value_type (value);
  484. struct type *var_type = type->field (field_num).type ();
  485. int which = ada_which_variant_applies (var_type, outer_value);
  486. if (which < 0)
  487. return 0;
  488. struct value *variant_field = value_field (value, field_num);
  489. struct value *active_component = value_field (variant_field, which);
  490. return print_field_values (active_component, outer_value, stream, recurse,
  491. options, comma_needed, language);
  492. }
  493. /* Print out fields of VALUE.
  494. STREAM, RECURSE, and OPTIONS have the same meanings as in
  495. ada_print_value and ada_value_print.
  496. OUTER_VALUE gives the enclosing record (used to get discriminant
  497. values when printing variant parts).
  498. COMMA_NEEDED is 1 if fields have been printed at the current recursion
  499. level, so that a comma is needed before any field printed by this
  500. call.
  501. Returns 1 if COMMA_NEEDED or any fields were printed. */
  502. static int
  503. print_field_values (struct value *value, struct value *outer_value,
  504. struct ui_file *stream, int recurse,
  505. const struct value_print_options *options,
  506. int comma_needed,
  507. const struct language_defn *language)
  508. {
  509. int i, len;
  510. struct type *type = value_type (value);
  511. len = type->num_fields ();
  512. for (i = 0; i < len; i += 1)
  513. {
  514. if (ada_is_ignored_field (type, i))
  515. continue;
  516. if (ada_is_wrapper_field (type, i))
  517. {
  518. struct value *field_val = ada_value_primitive_field (value, 0,
  519. i, type);
  520. comma_needed =
  521. print_field_values (field_val, field_val,
  522. stream, recurse, options,
  523. comma_needed, language);
  524. continue;
  525. }
  526. else if (ada_is_variant_part (type, i))
  527. {
  528. comma_needed =
  529. print_variant_part (value, i, outer_value, stream, recurse,
  530. options, comma_needed, language);
  531. continue;
  532. }
  533. if (comma_needed)
  534. gdb_printf (stream, ", ");
  535. comma_needed = 1;
  536. if (options->prettyformat)
  537. {
  538. gdb_printf (stream, "\n");
  539. print_spaces (2 + 2 * recurse, stream);
  540. }
  541. else
  542. {
  543. stream->wrap_here (2 + 2 * recurse);
  544. }
  545. annotate_field_begin (type->field (i).type ());
  546. gdb_printf (stream, "%.*s",
  547. ada_name_prefix_len (type->field (i).name ()),
  548. type->field (i).name ());
  549. annotate_field_name_end ();
  550. gdb_puts (" => ", stream);
  551. annotate_field_value ();
  552. if (TYPE_FIELD_PACKED (type, i))
  553. {
  554. /* Bitfields require special handling, especially due to byte
  555. order problems. */
  556. if (HAVE_CPLUS_STRUCT (type) && TYPE_FIELD_IGNORE (type, i))
  557. {
  558. fputs_styled (_("<optimized out or zero length>"),
  559. metadata_style.style (), stream);
  560. }
  561. else
  562. {
  563. struct value *v;
  564. int bit_pos = type->field (i).loc_bitpos ();
  565. int bit_size = TYPE_FIELD_BITSIZE (type, i);
  566. struct value_print_options opts;
  567. adjust_type_signedness (type->field (i).type ());
  568. v = ada_value_primitive_packed_val
  569. (value, nullptr,
  570. bit_pos / HOST_CHAR_BIT,
  571. bit_pos % HOST_CHAR_BIT,
  572. bit_size, type->field (i).type ());
  573. opts = *options;
  574. opts.deref_ref = 0;
  575. common_val_print (v, stream, recurse + 1, &opts, language);
  576. }
  577. }
  578. else
  579. {
  580. struct value_print_options opts = *options;
  581. opts.deref_ref = 0;
  582. struct value *v = value_field (value, i);
  583. common_val_print (v, stream, recurse + 1, &opts, language);
  584. }
  585. annotate_field_end ();
  586. }
  587. return comma_needed;
  588. }
  589. /* Implement Ada val_print'ing for the case where TYPE is
  590. a TYPE_CODE_ARRAY of characters. */
  591. static void
  592. ada_val_print_string (struct type *type, const gdb_byte *valaddr,
  593. int offset_aligned,
  594. struct ui_file *stream, int recurse,
  595. const struct value_print_options *options)
  596. {
  597. enum bfd_endian byte_order = type_byte_order (type);
  598. struct type *elttype = TYPE_TARGET_TYPE (type);
  599. unsigned int eltlen;
  600. unsigned int len;
  601. /* We know that ELTTYPE cannot possibly be null, because we assume
  602. that we're called only when TYPE is a string-like type.
  603. Similarly, the size of ELTTYPE should also be non-null, since
  604. it's a character-like type. */
  605. gdb_assert (elttype != NULL);
  606. gdb_assert (TYPE_LENGTH (elttype) != 0);
  607. eltlen = TYPE_LENGTH (elttype);
  608. len = TYPE_LENGTH (type) / eltlen;
  609. /* If requested, look for the first null char and only print
  610. elements up to it. */
  611. if (options->stop_print_at_null)
  612. {
  613. int temp_len;
  614. /* Look for a NULL char. */
  615. for (temp_len = 0;
  616. (temp_len < len
  617. && temp_len < options->print_max
  618. && char_at (valaddr + offset_aligned,
  619. temp_len, eltlen, byte_order) != 0);
  620. temp_len += 1);
  621. len = temp_len;
  622. }
  623. printstr (stream, elttype, valaddr + offset_aligned, len, 0,
  624. eltlen, options);
  625. }
  626. /* Implement Ada value_print'ing for the case where TYPE is a
  627. TYPE_CODE_PTR. */
  628. static void
  629. ada_value_print_ptr (struct value *val,
  630. struct ui_file *stream, int recurse,
  631. const struct value_print_options *options)
  632. {
  633. if (!options->format
  634. && TYPE_TARGET_TYPE (value_type (val))->code () == TYPE_CODE_INT
  635. && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))) == 0)
  636. {
  637. gdb_puts ("null", stream);
  638. return;
  639. }
  640. common_val_print (val, stream, recurse, options, language_def (language_c));
  641. struct type *type = ada_check_typedef (value_type (val));
  642. if (ada_is_tag_type (type))
  643. {
  644. gdb::unique_xmalloc_ptr<char> name = ada_tag_name (val);
  645. if (name != NULL)
  646. gdb_printf (stream, " (%s)", name.get ());
  647. }
  648. }
  649. /* Implement Ada val_print'ing for the case where TYPE is
  650. a TYPE_CODE_INT or TYPE_CODE_RANGE. */
  651. static void
  652. ada_value_print_num (struct value *val, struct ui_file *stream, int recurse,
  653. const struct value_print_options *options)
  654. {
  655. struct type *type = ada_check_typedef (value_type (val));
  656. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  657. if (type->code () == TYPE_CODE_RANGE
  658. && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM
  659. || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_BOOL
  660. || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_CHAR))
  661. {
  662. /* For enum-valued ranges, we want to recurse, because we'll end
  663. up printing the constant's name rather than its numeric
  664. value. Character and fixed-point types are also printed
  665. differently, so recuse for those as well. */
  666. struct type *target_type = TYPE_TARGET_TYPE (type);
  667. val = value_cast (target_type, val);
  668. common_val_print (val, stream, recurse + 1, options,
  669. language_def (language_ada));
  670. return;
  671. }
  672. else
  673. {
  674. int format = (options->format ? options->format
  675. : options->output_format);
  676. if (format)
  677. {
  678. struct value_print_options opts = *options;
  679. opts.format = format;
  680. value_print_scalar_formatted (val, &opts, 0, stream);
  681. }
  682. else if (ada_is_system_address_type (type))
  683. {
  684. /* FIXME: We want to print System.Address variables using
  685. the same format as for any access type. But for some
  686. reason GNAT encodes the System.Address type as an int,
  687. so we have to work-around this deficiency by handling
  688. System.Address values as a special case. */
  689. struct gdbarch *gdbarch = type->arch ();
  690. struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  691. CORE_ADDR addr = extract_typed_address (valaddr, ptr_type);
  692. gdb_printf (stream, "(");
  693. type_print (type, "", stream, -1);
  694. gdb_printf (stream, ") ");
  695. gdb_puts (paddress (gdbarch, addr), stream);
  696. }
  697. else
  698. {
  699. value_print_scalar_formatted (val, options, 0, stream);
  700. if (ada_is_character_type (type))
  701. {
  702. LONGEST c;
  703. gdb_puts (" ", stream);
  704. c = unpack_long (type, valaddr);
  705. ada_printchar (c, type, stream);
  706. }
  707. }
  708. return;
  709. }
  710. }
  711. /* Implement Ada val_print'ing for the case where TYPE is
  712. a TYPE_CODE_ENUM. */
  713. static void
  714. ada_val_print_enum (struct value *value, struct ui_file *stream, int recurse,
  715. const struct value_print_options *options)
  716. {
  717. int i;
  718. unsigned int len;
  719. LONGEST val;
  720. if (options->format)
  721. {
  722. value_print_scalar_formatted (value, options, 0, stream);
  723. return;
  724. }
  725. struct type *type = ada_check_typedef (value_type (value));
  726. const gdb_byte *valaddr = value_contents_for_printing (value).data ();
  727. int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
  728. len = type->num_fields ();
  729. val = unpack_long (type, valaddr + offset_aligned);
  730. for (i = 0; i < len; i++)
  731. {
  732. QUIT;
  733. if (val == type->field (i).loc_enumval ())
  734. break;
  735. }
  736. if (i < len)
  737. {
  738. const char *name = ada_enum_name (type->field (i).name ());
  739. if (name[0] == '\'')
  740. gdb_printf (stream, "%ld %ps", (long) val,
  741. styled_string (variable_name_style.style (),
  742. name));
  743. else
  744. fputs_styled (name, variable_name_style.style (), stream);
  745. }
  746. else
  747. print_longest (stream, 'd', 0, val);
  748. }
  749. /* Implement Ada val_print'ing for the case where the type is
  750. TYPE_CODE_STRUCT or TYPE_CODE_UNION. */
  751. static void
  752. ada_val_print_struct_union (struct value *value,
  753. struct ui_file *stream,
  754. int recurse,
  755. const struct value_print_options *options)
  756. {
  757. if (ada_is_bogus_array_descriptor (value_type (value)))
  758. {
  759. gdb_printf (stream, "(...?)");
  760. return;
  761. }
  762. gdb_printf (stream, "(");
  763. if (print_field_values (value, value, stream, recurse, options,
  764. 0, language_def (language_ada)) != 0
  765. && options->prettyformat)
  766. {
  767. gdb_printf (stream, "\n");
  768. print_spaces (2 * recurse, stream);
  769. }
  770. gdb_printf (stream, ")");
  771. }
  772. /* Implement Ada value_print'ing for the case where TYPE is a
  773. TYPE_CODE_ARRAY. */
  774. static void
  775. ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
  776. const struct value_print_options *options)
  777. {
  778. struct type *type = ada_check_typedef (value_type (val));
  779. /* For an array of characters, print with string syntax. */
  780. if (ada_is_string_type (type)
  781. && (options->format == 0 || options->format == 's'))
  782. {
  783. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  784. int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
  785. ada_val_print_string (type, valaddr, offset_aligned, stream, recurse,
  786. options);
  787. return;
  788. }
  789. gdb_printf (stream, "(");
  790. print_optional_low_bound (stream, type, options);
  791. if (value_entirely_optimized_out (val))
  792. val_print_optimized_out (val, stream);
  793. else if (TYPE_FIELD_BITSIZE (type, 0) > 0)
  794. {
  795. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  796. int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
  797. val_print_packed_array_elements (type, valaddr, offset_aligned,
  798. stream, recurse, options);
  799. }
  800. else
  801. value_print_array_elements (val, stream, recurse, options, 0);
  802. gdb_printf (stream, ")");
  803. }
  804. /* Implement Ada val_print'ing for the case where TYPE is
  805. a TYPE_CODE_REF. */
  806. static void
  807. ada_val_print_ref (struct type *type, const gdb_byte *valaddr,
  808. int offset, int offset_aligned, CORE_ADDR address,
  809. struct ui_file *stream, int recurse,
  810. struct value *original_value,
  811. const struct value_print_options *options)
  812. {
  813. /* For references, the debugger is expected to print the value as
  814. an address if DEREF_REF is null. But printing an address in place
  815. of the object value would be confusing to an Ada programmer.
  816. So, for Ada values, we print the actual dereferenced value
  817. regardless. */
  818. struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  819. struct value *deref_val;
  820. CORE_ADDR deref_val_int;
  821. if (elttype->code () == TYPE_CODE_UNDEF)
  822. {
  823. fputs_styled ("<ref to undefined type>", metadata_style.style (),
  824. stream);
  825. return;
  826. }
  827. deref_val = coerce_ref_if_computed (original_value);
  828. if (deref_val)
  829. {
  830. if (ada_is_tagged_type (value_type (deref_val), 1))
  831. deref_val = ada_tag_value_at_base_address (deref_val);
  832. common_val_print (deref_val, stream, recurse + 1, options,
  833. language_def (language_ada));
  834. return;
  835. }
  836. deref_val_int = unpack_pointer (type, valaddr + offset_aligned);
  837. if (deref_val_int == 0)
  838. {
  839. gdb_puts ("(null)", stream);
  840. return;
  841. }
  842. deref_val
  843. = ada_value_ind (value_from_pointer (lookup_pointer_type (elttype),
  844. deref_val_int));
  845. if (ada_is_tagged_type (value_type (deref_val), 1))
  846. deref_val = ada_tag_value_at_base_address (deref_val);
  847. if (value_lazy (deref_val))
  848. value_fetch_lazy (deref_val);
  849. common_val_print (deref_val, stream, recurse + 1,
  850. options, language_def (language_ada));
  851. }
  852. /* See the comment on ada_value_print. This function differs in that
  853. it does not catch evaluation errors (leaving that to its
  854. caller). */
  855. void
  856. ada_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
  857. const struct value_print_options *options)
  858. {
  859. struct type *type = ada_check_typedef (value_type (val));
  860. if (ada_is_array_descriptor_type (type)
  861. || (ada_is_constrained_packed_array_type (type)
  862. && type->code () != TYPE_CODE_PTR))
  863. {
  864. /* If this is a reference, coerce it now. This helps taking
  865. care of the case where ADDRESS is meaningless because
  866. original_value was not an lval. */
  867. val = coerce_ref (val);
  868. val = ada_get_decoded_value (val);
  869. if (val == nullptr)
  870. {
  871. gdb_assert (type->code () == TYPE_CODE_TYPEDEF);
  872. gdb_printf (stream, "0x0");
  873. return;
  874. }
  875. }
  876. else
  877. val = ada_to_fixed_value (val);
  878. type = value_type (val);
  879. struct type *saved_type = type;
  880. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  881. CORE_ADDR address = value_address (val);
  882. gdb::array_view<const gdb_byte> view
  883. = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
  884. type = ada_check_typedef (resolve_dynamic_type (type, view, address));
  885. if (type != saved_type)
  886. {
  887. val = value_copy (val);
  888. deprecated_set_value_type (val, type);
  889. }
  890. if (is_fixed_point_type (type))
  891. type = type->fixed_point_type_base_type ();
  892. switch (type->code ())
  893. {
  894. default:
  895. common_val_print (val, stream, recurse, options,
  896. language_def (language_c));
  897. break;
  898. case TYPE_CODE_PTR:
  899. ada_value_print_ptr (val, stream, recurse, options);
  900. break;
  901. case TYPE_CODE_INT:
  902. case TYPE_CODE_RANGE:
  903. ada_value_print_num (val, stream, recurse, options);
  904. break;
  905. case TYPE_CODE_ENUM:
  906. ada_val_print_enum (val, stream, recurse, options);
  907. break;
  908. case TYPE_CODE_FLT:
  909. if (options->format)
  910. {
  911. common_val_print (val, stream, recurse, options,
  912. language_def (language_c));
  913. break;
  914. }
  915. ada_print_floating (valaddr, type, stream);
  916. break;
  917. case TYPE_CODE_UNION:
  918. case TYPE_CODE_STRUCT:
  919. ada_val_print_struct_union (val, stream, recurse, options);
  920. break;
  921. case TYPE_CODE_ARRAY:
  922. ada_value_print_array (val, stream, recurse, options);
  923. return;
  924. case TYPE_CODE_REF:
  925. ada_val_print_ref (type, valaddr, 0, 0,
  926. address, stream, recurse, val,
  927. options);
  928. break;
  929. }
  930. }
  931. void
  932. ada_value_print (struct value *val0, struct ui_file *stream,
  933. const struct value_print_options *options)
  934. {
  935. struct value *val = ada_to_fixed_value (val0);
  936. struct type *type = ada_check_typedef (value_type (val));
  937. struct value_print_options opts;
  938. /* If it is a pointer, indicate what it points to; but not for
  939. "void *" pointers. */
  940. if (type->code () == TYPE_CODE_PTR
  941. && !(TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_INT
  942. && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == 0))
  943. {
  944. /* Hack: don't print (char *) for char strings. Their
  945. type is indicated by the quoted string anyway. */
  946. if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
  947. || TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_INT
  948. || TYPE_TARGET_TYPE (type)->is_unsigned ())
  949. {
  950. gdb_printf (stream, "(");
  951. type_print (type, "", stream, -1);
  952. gdb_printf (stream, ") ");
  953. }
  954. }
  955. else if (ada_is_array_descriptor_type (type))
  956. {
  957. /* We do not print the type description unless TYPE is an array
  958. access type (this is encoded by the compiler as a typedef to
  959. a fat pointer - hence the check against TYPE_CODE_TYPEDEF). */
  960. if (type->code () == TYPE_CODE_TYPEDEF)
  961. {
  962. gdb_printf (stream, "(");
  963. type_print (type, "", stream, -1);
  964. gdb_printf (stream, ") ");
  965. }
  966. }
  967. else if (ada_is_bogus_array_descriptor (type))
  968. {
  969. gdb_printf (stream, "(");
  970. type_print (type, "", stream, -1);
  971. gdb_printf (stream, ") (...?)");
  972. return;
  973. }
  974. opts = *options;
  975. opts.deref_ref = 1;
  976. common_val_print (val, stream, 0, &opts, current_language);
  977. }