m2-valprint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* Support for printing Modula 2 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 "typeprint.h"
  22. #include "c-lang.h"
  23. #include "m2-lang.h"
  24. #include "target.h"
  25. #include "cli/cli-style.h"
  26. static int print_unpacked_pointer (struct type *type,
  27. CORE_ADDR address, CORE_ADDR addr,
  28. const struct value_print_options *options,
  29. struct ui_file *stream);
  30. static void
  31. m2_print_array_contents (struct value *val,
  32. struct ui_file *stream, int recurse,
  33. const struct value_print_options *options,
  34. int len);
  35. /* get_long_set_bounds - assigns the bounds of the long set to low and
  36. high. */
  37. int
  38. get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
  39. {
  40. int len, i;
  41. if (type->code () == TYPE_CODE_STRUCT)
  42. {
  43. len = type->num_fields ();
  44. i = TYPE_N_BASECLASSES (type);
  45. if (len == 0)
  46. return 0;
  47. *low = type->field (i).type ()->bounds ()->low.const_val ();
  48. *high = type->field (len - 1).type ()->bounds ()->high.const_val ();
  49. return 1;
  50. }
  51. error (_("expecting long_set"));
  52. return 0;
  53. }
  54. static void
  55. m2_print_long_set (struct type *type, const gdb_byte *valaddr,
  56. int embedded_offset, CORE_ADDR address,
  57. struct ui_file *stream)
  58. {
  59. int empty_set = 1;
  60. int element_seen = 0;
  61. LONGEST previous_low = 0;
  62. LONGEST previous_high= 0;
  63. LONGEST i, low_bound, high_bound;
  64. LONGEST field_low, field_high;
  65. struct type *range;
  66. int len, field;
  67. struct type *target;
  68. int bitval;
  69. type = check_typedef (type);
  70. gdb_printf (stream, "{");
  71. len = type->num_fields ();
  72. if (get_long_set_bounds (type, &low_bound, &high_bound))
  73. {
  74. field = TYPE_N_BASECLASSES (type);
  75. range = type->field (field).type ()->index_type ();
  76. }
  77. else
  78. {
  79. fprintf_styled (stream, metadata_style.style (),
  80. " %s }", _("<unknown bounds of set>"));
  81. return;
  82. }
  83. target = TYPE_TARGET_TYPE (range);
  84. if (get_discrete_bounds (range, &field_low, &field_high))
  85. {
  86. for (i = low_bound; i <= high_bound; i++)
  87. {
  88. bitval = value_bit_index (type->field (field).type (),
  89. (type->field (field).loc_bitpos () / 8) +
  90. valaddr + embedded_offset, i);
  91. if (bitval < 0)
  92. error (_("bit test is out of range"));
  93. else if (bitval > 0)
  94. {
  95. previous_high = i;
  96. if (! element_seen)
  97. {
  98. if (! empty_set)
  99. gdb_printf (stream, ", ");
  100. print_type_scalar (target, i, stream);
  101. empty_set = 0;
  102. element_seen = 1;
  103. previous_low = i;
  104. }
  105. }
  106. else
  107. {
  108. /* bit is not set */
  109. if (element_seen)
  110. {
  111. if (previous_low+1 < previous_high)
  112. gdb_printf (stream, "..");
  113. if (previous_low+1 < previous_high)
  114. print_type_scalar (target, previous_high, stream);
  115. element_seen = 0;
  116. }
  117. }
  118. if (i == field_high)
  119. {
  120. field++;
  121. if (field == len)
  122. break;
  123. range = type->field (field).type ()->index_type ();
  124. if (!get_discrete_bounds (range, &field_low, &field_high))
  125. break;
  126. target = TYPE_TARGET_TYPE (range);
  127. }
  128. }
  129. if (element_seen)
  130. {
  131. if (previous_low+1 < previous_high)
  132. {
  133. gdb_printf (stream, "..");
  134. print_type_scalar (target, previous_high, stream);
  135. }
  136. element_seen = 0;
  137. }
  138. gdb_printf (stream, "}");
  139. }
  140. }
  141. static void
  142. m2_print_unbounded_array (struct value *value,
  143. struct ui_file *stream, int recurse,
  144. const struct value_print_options *options)
  145. {
  146. CORE_ADDR addr;
  147. LONGEST len;
  148. struct value *val;
  149. struct type *type = check_typedef (value_type (value));
  150. const gdb_byte *valaddr = value_contents_for_printing (value).data ();
  151. addr = unpack_pointer (type->field (0).type (),
  152. (type->field (0).loc_bitpos () / 8) +
  153. valaddr);
  154. val = value_at_lazy (TYPE_TARGET_TYPE (type->field (0).type ()),
  155. addr);
  156. len = unpack_field_as_long (type, valaddr, 1);
  157. gdb_printf (stream, "{");
  158. m2_print_array_contents (val, stream, recurse, options, len);
  159. gdb_printf (stream, ", HIGH = %d}", (int) len);
  160. }
  161. static int
  162. print_unpacked_pointer (struct type *type,
  163. CORE_ADDR address, CORE_ADDR addr,
  164. const struct value_print_options *options,
  165. struct ui_file *stream)
  166. {
  167. struct gdbarch *gdbarch = type->arch ();
  168. struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  169. int want_space = 0;
  170. if (elttype->code () == TYPE_CODE_FUNC)
  171. {
  172. /* Try to print what function it points to. */
  173. print_function_pointer_address (options, gdbarch, addr, stream);
  174. /* Return value is irrelevant except for string pointers. */
  175. return 0;
  176. }
  177. if (options->addressprint && options->format != 's')
  178. {
  179. gdb_puts (paddress (gdbarch, address), stream);
  180. want_space = 1;
  181. }
  182. /* For a pointer to char or unsigned char, also print the string
  183. pointed to, unless pointer is null. */
  184. if (TYPE_LENGTH (elttype) == 1
  185. && elttype->code () == TYPE_CODE_INT
  186. && (options->format == 0 || options->format == 's')
  187. && addr != 0)
  188. {
  189. if (want_space)
  190. gdb_puts (" ", stream);
  191. return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
  192. stream, options);
  193. }
  194. return 0;
  195. }
  196. static void
  197. print_variable_at_address (struct type *type,
  198. const gdb_byte *valaddr,
  199. struct ui_file *stream,
  200. int recurse,
  201. const struct value_print_options *options)
  202. {
  203. struct gdbarch *gdbarch = type->arch ();
  204. CORE_ADDR addr = unpack_pointer (type, valaddr);
  205. struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
  206. gdb_printf (stream, "[");
  207. gdb_puts (paddress (gdbarch, addr), stream);
  208. gdb_printf (stream, "] : ");
  209. if (elttype->code () != TYPE_CODE_UNDEF)
  210. {
  211. struct value *deref_val =
  212. value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
  213. common_val_print (deref_val, stream, recurse, options, current_language);
  214. }
  215. else
  216. gdb_puts ("???", stream);
  217. }
  218. /* m2_print_array_contents - prints out the contents of an
  219. array up to a max_print values.
  220. It prints arrays of char as a string
  221. and all other data types as comma
  222. separated values. */
  223. static void
  224. m2_print_array_contents (struct value *val,
  225. struct ui_file *stream, int recurse,
  226. const struct value_print_options *options,
  227. int len)
  228. {
  229. struct type *type = check_typedef (value_type (val));
  230. if (TYPE_LENGTH (type) > 0)
  231. {
  232. /* For an array of chars, print with string syntax. */
  233. if (TYPE_LENGTH (type) == 1 &&
  234. ((type->code () == TYPE_CODE_INT)
  235. || ((current_language->la_language == language_m2)
  236. && (type->code () == TYPE_CODE_CHAR)))
  237. && (options->format == 0 || options->format == 's'))
  238. val_print_string (type, NULL, value_address (val), len+1, stream,
  239. options);
  240. else
  241. {
  242. gdb_printf (stream, "{");
  243. value_print_array_elements (val, stream, recurse, options, 0);
  244. gdb_printf (stream, "}");
  245. }
  246. }
  247. }
  248. /* Decorations for Modula 2. */
  249. static const struct generic_val_print_decorations m2_decorations =
  250. {
  251. "",
  252. " + ",
  253. " * I",
  254. "TRUE",
  255. "FALSE",
  256. "void",
  257. "{",
  258. "}"
  259. };
  260. /* See m2-lang.h. */
  261. void
  262. m2_language::value_print_inner (struct value *val, struct ui_file *stream,
  263. int recurse,
  264. const struct value_print_options *options) const
  265. {
  266. unsigned len;
  267. struct type *elttype;
  268. CORE_ADDR addr;
  269. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  270. const CORE_ADDR address = value_address (val);
  271. struct type *type = check_typedef (value_type (val));
  272. switch (type->code ())
  273. {
  274. case TYPE_CODE_ARRAY:
  275. if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
  276. {
  277. elttype = check_typedef (TYPE_TARGET_TYPE (type));
  278. len = TYPE_LENGTH (type) / TYPE_LENGTH (elttype);
  279. /* For an array of chars, print with string syntax. */
  280. if (TYPE_LENGTH (elttype) == 1 &&
  281. ((elttype->code () == TYPE_CODE_INT)
  282. || ((current_language->la_language == language_m2)
  283. && (elttype->code () == TYPE_CODE_CHAR)))
  284. && (options->format == 0 || options->format == 's'))
  285. {
  286. /* If requested, look for the first null char and only print
  287. elements up to it. */
  288. if (options->stop_print_at_null)
  289. {
  290. unsigned int temp_len;
  291. /* Look for a NULL char. */
  292. for (temp_len = 0;
  293. (valaddr[temp_len]
  294. && temp_len < len && temp_len < options->print_max);
  295. temp_len++);
  296. len = temp_len;
  297. }
  298. printstr (stream, TYPE_TARGET_TYPE (type), valaddr, len,
  299. NULL, 0, options);
  300. }
  301. else
  302. {
  303. gdb_printf (stream, "{");
  304. value_print_array_elements (val, stream, recurse,
  305. options, 0);
  306. gdb_printf (stream, "}");
  307. }
  308. break;
  309. }
  310. /* Array of unspecified length: treat like pointer to first elt. */
  311. print_unpacked_pointer (type, address, address, options, stream);
  312. break;
  313. case TYPE_CODE_PTR:
  314. if (TYPE_CONST (type))
  315. print_variable_at_address (type, valaddr, stream, recurse, options);
  316. else if (options->format && options->format != 's')
  317. value_print_scalar_formatted (val, options, 0, stream);
  318. else
  319. {
  320. addr = unpack_pointer (type, valaddr);
  321. print_unpacked_pointer (type, addr, address, options, stream);
  322. }
  323. break;
  324. case TYPE_CODE_UNION:
  325. if (recurse && !options->unionprint)
  326. {
  327. gdb_printf (stream, "{...}");
  328. break;
  329. }
  330. /* Fall through. */
  331. case TYPE_CODE_STRUCT:
  332. if (m2_is_long_set (type))
  333. m2_print_long_set (type, valaddr, 0, address, stream);
  334. else if (m2_is_unbounded_array (type))
  335. m2_print_unbounded_array (val, stream, recurse, options);
  336. else
  337. cp_print_value_fields (val, stream, recurse, options, NULL, 0);
  338. break;
  339. case TYPE_CODE_SET:
  340. elttype = type->index_type ();
  341. elttype = check_typedef (elttype);
  342. if (elttype->is_stub ())
  343. {
  344. fprintf_styled (stream, metadata_style.style (),
  345. _("<incomplete type>"));
  346. break;
  347. }
  348. else
  349. {
  350. struct type *range = elttype;
  351. LONGEST low_bound, high_bound;
  352. int i;
  353. int need_comma = 0;
  354. gdb_puts ("{", stream);
  355. i = get_discrete_bounds (range, &low_bound, &high_bound) ? 0 : -1;
  356. maybe_bad_bstring:
  357. if (i < 0)
  358. {
  359. fputs_styled (_("<error value>"), metadata_style.style (),
  360. stream);
  361. goto done;
  362. }
  363. for (i = low_bound; i <= high_bound; i++)
  364. {
  365. int element = value_bit_index (type, valaddr, i);
  366. if (element < 0)
  367. {
  368. i = element;
  369. goto maybe_bad_bstring;
  370. }
  371. if (element)
  372. {
  373. if (need_comma)
  374. gdb_puts (", ", stream);
  375. print_type_scalar (range, i, stream);
  376. need_comma = 1;
  377. if (i + 1 <= high_bound
  378. && value_bit_index (type, valaddr, ++i))
  379. {
  380. int j = i;
  381. gdb_puts ("..", stream);
  382. while (i + 1 <= high_bound
  383. && value_bit_index (type, valaddr, ++i))
  384. j = i;
  385. print_type_scalar (range, j, stream);
  386. }
  387. }
  388. }
  389. done:
  390. gdb_puts ("}", stream);
  391. }
  392. break;
  393. case TYPE_CODE_RANGE:
  394. if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
  395. {
  396. struct value *v = value_cast (TYPE_TARGET_TYPE (type), val);
  397. value_print_inner (v, stream, recurse, options);
  398. break;
  399. }
  400. /* FALLTHROUGH */
  401. case TYPE_CODE_REF:
  402. case TYPE_CODE_ENUM:
  403. case TYPE_CODE_FUNC:
  404. case TYPE_CODE_INT:
  405. case TYPE_CODE_FLT:
  406. case TYPE_CODE_METHOD:
  407. case TYPE_CODE_VOID:
  408. case TYPE_CODE_ERROR:
  409. case TYPE_CODE_UNDEF:
  410. case TYPE_CODE_BOOL:
  411. case TYPE_CODE_CHAR:
  412. default:
  413. generic_value_print (val, stream, recurse, options, &m2_decorations);
  414. break;
  415. }
  416. }