valprint.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /* Print values for GNU debugger gdb.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <stdio.h>
  18. #include "defs.h"
  19. #include "initialize.h"
  20. #include "symtab.h"
  21. #include "value.h"
  22. /* Maximum number of chars to print for a string pointer value
  23. or vector contents. */
  24. static int print_max;
  25. static void type_print_varspec_suffix ();
  26. static void type_print_varspec_prefix ();
  27. static void type_print_base ();
  28. START_FILE
  29. char **unsigned_type_table;
  30. char **signed_type_table;
  31. char **float_type_table;
  32. /* Print the value VAL in C-ish syntax on stream STREAM.
  33. If the object printed is a string pointer, returns
  34. the number of string bytes printed. */
  35. value_print (val, stream)
  36. value val;
  37. FILE *stream;
  38. {
  39. register int i, n, typelen;
  40. /* A "repeated" value really contains several values in a row.
  41. They are made by the @ operator.
  42. Print such values as if they were arrays. */
  43. if (VALUE_REPEATED (val))
  44. {
  45. n = VALUE_REPETITIONS (val);
  46. typelen = TYPE_LENGTH (VALUE_TYPE (val));
  47. fputc ('{', stream);
  48. /* Print arrays of characters using string syntax. */
  49. if (VALUE_TYPE (val) == builtin_type_char
  50. || VALUE_TYPE (val) == builtin_type_unsigned_char)
  51. {
  52. fputc ('"', stream);
  53. for (i = 0; i < n && i < print_max; i++)
  54. {
  55. QUIT;
  56. printchar (VALUE_CONTENTS (val)[i], stream);
  57. }
  58. if (i < n)
  59. fprintf (stream, "...");
  60. fputc ('"', stream);
  61. }
  62. else
  63. {
  64. for (i = 0; i < n && i < print_max; i++)
  65. {
  66. if (i)
  67. fprintf (stream, ", ");
  68. val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
  69. VALUE_ADDRESS (val) + typelen * i, stream);
  70. }
  71. if (i < n)
  72. fprintf (stream, "...");
  73. }
  74. fputc ('}', stream);
  75. }
  76. else
  77. {
  78. /* A simple (nonrepeated) value */
  79. /* If it is a pointer, indicate what it points to. */
  80. if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_PTR)
  81. {
  82. fprintf (stream, "(");
  83. type_print (VALUE_TYPE (val), "", stream, -1);
  84. fprintf (stream, ") ");
  85. }
  86. return val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
  87. VALUE_ADDRESS (val), stream);
  88. }
  89. }
  90. /* Print on STREAM data stored in debugger at address VALADDR
  91. according to the format of type TYPE.
  92. ADDRESS is the location in the inferior that the data
  93. is supposed to have come from.
  94. If the data are a string pointer, returns the number of
  95. sting characters printed. */
  96. int
  97. val_print (type, valaddr, address, stream)
  98. struct type *type;
  99. char *valaddr;
  100. CORE_ADDR address;
  101. FILE *stream;
  102. {
  103. register int i;
  104. int len;
  105. struct type *elttype;
  106. int eltlen;
  107. int val;
  108. unsigned char c;
  109. QUIT;
  110. switch (TYPE_CODE (type))
  111. {
  112. case TYPE_CODE_ARRAY:
  113. if (TYPE_LENGTH (type) >= 0)
  114. {
  115. elttype = TYPE_TARGET_TYPE (type);
  116. eltlen = TYPE_LENGTH (elttype);
  117. len = TYPE_LENGTH (type) / eltlen;
  118. fprintf (stream, "{");
  119. /* For an array of chars, print with string syntax. */
  120. if (elttype == builtin_type_char
  121. || elttype == builtin_type_unsigned_char)
  122. {
  123. fputc ('"', stream);
  124. for (i = 0; i < len && i < print_max; i++)
  125. {
  126. QUIT;
  127. printchar (valaddr[i], stream);
  128. }
  129. if (i < len)
  130. fprintf (stream, "...");
  131. fputc ('"', stream);
  132. }
  133. else
  134. {
  135. for (i = 0; i < len && i < print_max; i++)
  136. {
  137. if (i) fprintf (stream, ", ");
  138. val_print (elttype, valaddr + i * eltlen,
  139. 0, stream);
  140. }
  141. if (i < len)
  142. fprintf (stream, "...");
  143. }
  144. fprintf (stream, "}");
  145. break;
  146. }
  147. /* Array of unspecified length: treat like pointer. */
  148. case TYPE_CODE_PTR:
  149. fprintf (stream, "0x%x", * (int *) valaddr);
  150. /* For a pointer to char or unsigned char,
  151. also print the string pointed to, unless pointer is null. */
  152. if ((TYPE_TARGET_TYPE (type) == builtin_type_char
  153. || TYPE_TARGET_TYPE (type) == builtin_type_unsigned_char)
  154. && unpack_long (type, valaddr) != 0)
  155. {
  156. fputc (' ', stream);
  157. fputc ('"', stream);
  158. for (i = 0; i < print_max; i++)
  159. {
  160. QUIT;
  161. read_memory (unpack_long (type, valaddr) + i, &c, 1);
  162. if (c == 0)
  163. break;
  164. printchar (c, stream);
  165. }
  166. fputc ('"', stream);
  167. if (i == print_max)
  168. fprintf (stream, "...");
  169. fflush (stream);
  170. /* Return number of characters printed, plus one for the
  171. terminating null if we have "reached the end". */
  172. return i + (i != print_max);
  173. }
  174. break;
  175. case TYPE_CODE_STRUCT:
  176. case TYPE_CODE_UNION:
  177. fprintf (stream, "{");
  178. len = TYPE_NFIELDS (type);
  179. for (i = 0; i < len; i++)
  180. {
  181. if (i) fprintf (stream, ", ");
  182. fprintf (stream, "%s = ", TYPE_FIELD_NAME (type, i));
  183. if (TYPE_FIELD_PACKED (type, i))
  184. {
  185. val = unpack_field_as_long (type, valaddr, i);
  186. val_print (TYPE_FIELD_TYPE (type, i), &val, 0, stream);
  187. }
  188. else
  189. val_print (TYPE_FIELD_TYPE (type, i),
  190. valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
  191. 0, stream);
  192. }
  193. fprintf (stream, "}");
  194. break;
  195. case TYPE_CODE_ENUM:
  196. len = TYPE_NFIELDS (type);
  197. val = unpack_long (builtin_type_int, valaddr);
  198. for (i = 0; i < len; i++)
  199. {
  200. QUIT;
  201. if (val == TYPE_FIELD_VALUE (type, i))
  202. break;
  203. }
  204. if (i < len)
  205. fprintf (stream, "%s", TYPE_FIELD_NAME (type, i));
  206. else
  207. fprintf (stream, "%d", val);
  208. break;
  209. case TYPE_CODE_FUNC:
  210. fprintf (stream, "{");
  211. type_print (type, "", stream, -1);
  212. fprintf (stream, "} ");
  213. fprintf (stream, "0x%x", address);
  214. break;
  215. case TYPE_CODE_INT:
  216. fprintf (stream,
  217. TYPE_UNSIGNED (type) ? "%u" : "%d",
  218. unpack_long (type, valaddr));
  219. if (type == builtin_type_char
  220. || type == builtin_type_unsigned_char)
  221. {
  222. fprintf (stream, " '");
  223. printchar (unpack_long (type, valaddr), stream);
  224. fputc ('\'', stream);
  225. }
  226. break;
  227. case TYPE_CODE_FLT:
  228. fprintf (stream, "%g", unpack_double (type, valaddr));
  229. break;
  230. case TYPE_CODE_VOID:
  231. fprintf (stream, "void");
  232. break;
  233. default:
  234. error ("Invalid type code in symbol table.");
  235. }
  236. fflush (stream);
  237. }
  238. /* Print a description of a type TYPE
  239. in the form of a declaration of a variable named VARSTRING.
  240. Output goes to STREAM (via stdio).
  241. If SHOW is positive, we show the contents of the outermost level
  242. of structure even if there is a type name that could be used instead.
  243. If SHOW is negative, we never show the details of elements' types. */
  244. type_print (type, varstring, stream, show)
  245. struct type *type;
  246. char *varstring;
  247. FILE *stream;
  248. int show;
  249. {
  250. type_print_1 (type, varstring, stream, show, 0);
  251. }
  252. /* LEVEL is the depth to indent lines by. */
  253. type_print_1 (type, varstring, stream, show, level)
  254. struct type *type;
  255. char *varstring;
  256. FILE *stream;
  257. int show;
  258. int level;
  259. {
  260. register enum type_code code;
  261. type_print_base (type, stream, show, level);
  262. code = TYPE_CODE (type);
  263. if ((varstring && *varstring)
  264. ||
  265. /* Need a space if going to print stars or brackets;
  266. but not if we will print just a type name. */
  267. ((show > 0 || TYPE_NAME (type) == 0)
  268. &&
  269. (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
  270. || code == TYPE_CODE_ARRAY)))
  271. fprintf (stream, " ");
  272. type_print_varspec_prefix (type, stream, show, 0);
  273. fprintf (stream, "%s", varstring);
  274. type_print_varspec_suffix (type, stream, show, 0);
  275. }
  276. /* Print any asterisks or open-parentheses needed before the
  277. variable name (to describe its type).
  278. On outermost call, pass 0 for PASSED_A_PTR.
  279. On outermost call, SHOW > 0 means should ignore
  280. any typename for TYPE and show its details.
  281. SHOW is always zero on recursive calls. */
  282. static void
  283. type_print_varspec_prefix (type, stream, show, passed_a_ptr)
  284. struct type *type;
  285. FILE *stream;
  286. int show;
  287. int passed_a_ptr;
  288. {
  289. if (TYPE_NAME (type) && show <= 0)
  290. return;
  291. QUIT;
  292. switch (TYPE_CODE (type))
  293. {
  294. case TYPE_CODE_PTR:
  295. type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
  296. fputc ('*', stream);
  297. break;
  298. case TYPE_CODE_FUNC:
  299. type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
  300. passed_a_ptr);
  301. if (passed_a_ptr)
  302. fputc ('(', stream);
  303. break;
  304. case TYPE_CODE_ARRAY:
  305. type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
  306. passed_a_ptr);
  307. }
  308. }
  309. /* Print any array sizes, function arguments or close parentheses
  310. needed after the variable name (to describe its type).
  311. Args work like type_print_varspec_prefix. */
  312. static void
  313. type_print_varspec_suffix (type, stream, show, passed_a_ptr)
  314. struct type *type;
  315. FILE *stream;
  316. int show;
  317. int passed_a_ptr;
  318. {
  319. if (TYPE_NAME (type) && show <= 0)
  320. return;
  321. QUIT;
  322. switch (TYPE_CODE (type))
  323. {
  324. case TYPE_CODE_ARRAY:
  325. type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
  326. passed_a_ptr);
  327. fprintf (stream, "[");
  328. if (TYPE_LENGTH (type) >= 0)
  329. fprintf (stream, "%d",
  330. TYPE_LENGTH (type) / TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
  331. fprintf (stream, "]");
  332. break;
  333. case TYPE_CODE_PTR:
  334. type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
  335. break;
  336. case TYPE_CODE_FUNC:
  337. type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
  338. passed_a_ptr);
  339. if (passed_a_ptr)
  340. fprintf (stream, ")");
  341. fprintf (stream, "()");
  342. break;
  343. }
  344. }
  345. /* Print the name of the type (or the ultimate pointer target,
  346. function value or array element), or the description of a
  347. structure or union.
  348. SHOW nonzero means don't print this type as just its name;
  349. show its real definition even if it has a name.
  350. SHOW zero means print just typename or struct tag if there is one
  351. SHOW negative means abbreviate structure elements.
  352. SHOW is decremented for printing of structure elements.
  353. LEVEL is the depth to indent by.
  354. We increase it for some recursive calls. */
  355. static void
  356. type_print_base (type, stream, show, level)
  357. struct type *type;
  358. FILE *stream;
  359. int show;
  360. int level;
  361. {
  362. char *name;
  363. register int i;
  364. register int len;
  365. register int lastval;
  366. QUIT;
  367. if (TYPE_NAME (type) && show <= 0)
  368. {
  369. fprintf (stream, TYPE_NAME (type));
  370. return;
  371. }
  372. switch (TYPE_CODE (type))
  373. {
  374. case TYPE_CODE_ARRAY:
  375. case TYPE_CODE_PTR:
  376. case TYPE_CODE_FUNC:
  377. type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
  378. break;
  379. case TYPE_CODE_STRUCT:
  380. fprintf (stream, "struct ");
  381. goto struct_union;
  382. case TYPE_CODE_UNION:
  383. fprintf (stream, "union ");
  384. struct_union:
  385. if (TYPE_NAME (type) && (name = TYPE_NAME (type)))
  386. {
  387. while (*name != ' ') name++;
  388. fprintf (stream, "%s ", name + 1);
  389. }
  390. if (show < 0)
  391. fprintf (stream, "{...}");
  392. else
  393. {
  394. fprintf (stream, "{");
  395. len = TYPE_NFIELDS (type);
  396. fprintf (stream, "\n");
  397. for (i = 0; i < len; i++)
  398. {
  399. QUIT;
  400. print_spaces (level + 4, stream);
  401. type_print_1 (TYPE_FIELD_TYPE (type, i),
  402. TYPE_FIELD_NAME (type, i),
  403. stream, show - 1, level + 4);
  404. if (TYPE_FIELD_PACKED (type, i))
  405. {
  406. /* ??? don't know what to put here ??? */;
  407. }
  408. fprintf (stream, ";\n");
  409. }
  410. print_spaces (level, stream);
  411. fputc ('}', stream);
  412. }
  413. break;
  414. case TYPE_CODE_ENUM:
  415. fprintf (stream, "enum ");
  416. if (TYPE_NAME (type))
  417. {
  418. name = TYPE_NAME (type);
  419. while (*name != ' ') name++;
  420. fprintf (stream, "%s ", name + 1);
  421. }
  422. if (show < 0)
  423. fprintf (stream, "{...}");
  424. else
  425. {
  426. fprintf (stream, "{");
  427. len = TYPE_NFIELDS (type);
  428. lastval = 0;
  429. for (i = 0; i < len; i++)
  430. {
  431. QUIT;
  432. if (i) fprintf (stream, ", ");
  433. fprintf (stream, "%s", TYPE_FIELD_NAME (type, i));
  434. if (lastval != TYPE_FIELD_VALUE (type, i))
  435. {
  436. fprintf (stream, " : %d", TYPE_FIELD_VALUE (type, i));
  437. lastval = TYPE_FIELD_VALUE (type, i);
  438. }
  439. lastval++;
  440. }
  441. fprintf (stream, "}");
  442. }
  443. break;
  444. case TYPE_CODE_INT:
  445. if (TYPE_UNSIGNED (type))
  446. name = unsigned_type_table[TYPE_LENGTH (type)];
  447. else
  448. name = signed_type_table[TYPE_LENGTH (type)];
  449. fprintf (stream, "%s", name);
  450. break;
  451. case TYPE_CODE_FLT:
  452. name = float_type_table[TYPE_LENGTH (type)];
  453. fprintf (stream, "%s", name);
  454. break;
  455. case TYPE_CODE_VOID:
  456. fprintf (stream, "void");
  457. break;
  458. case 0:
  459. fprintf (stream, "struct unknown");
  460. break;
  461. default:
  462. error ("Invalid type code in symbol table.");
  463. }
  464. }
  465. static void
  466. set_maximum_command (arg)
  467. char *arg;
  468. {
  469. if (!arg) error_no_arg ("value for maximum elements to print");
  470. print_max = atoi (arg);
  471. }
  472. static
  473. initialize ()
  474. {
  475. add_com ("set-maximum", class_vars, set_maximum_command,
  476. "Set NUMBER as limit on string chars or array elements to print.");
  477. print_max = 200;
  478. unsigned_type_table
  479. = (char **) xmalloc ((1 + sizeof (unsigned long)) * sizeof (char *));
  480. bzero (unsigned_type_table, (1 + sizeof (unsigned long)));
  481. unsigned_type_table[sizeof (unsigned char)] = "unsigned char";
  482. unsigned_type_table[sizeof (unsigned short)] = "unsigned short";
  483. unsigned_type_table[sizeof (unsigned long)] = "unsigned long";
  484. unsigned_type_table[sizeof (unsigned int)] = "unsigned int";
  485. signed_type_table
  486. = (char **) xmalloc ((1 + sizeof (long)) * sizeof (char *));
  487. bzero (signed_type_table, (1 + sizeof (long)));
  488. signed_type_table[sizeof (char)] = "char";
  489. signed_type_table[sizeof (short)] = "short";
  490. signed_type_table[sizeof (long)] = "long";
  491. signed_type_table[sizeof (int)] = "int";
  492. float_type_table
  493. = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
  494. bzero (float_type_table, (1 + sizeof (double)));
  495. float_type_table[sizeof (float)] = "float";
  496. float_type_table[sizeof (double)] = "double";
  497. }
  498. END_FILE