print-utils.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Cell-based print utility routines 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 "common-defs.h"
  15. #include "print-utils.h"
  16. /* Temporary storage using circular buffer. */
  17. /* Number of cells in the circular buffer. */
  18. #define NUMCELLS 16
  19. /* Return the next entry in the circular buffer. */
  20. char *
  21. get_print_cell (void)
  22. {
  23. static char buf[NUMCELLS][PRINT_CELL_SIZE];
  24. static int cell = 0;
  25. if (++cell >= NUMCELLS)
  26. cell = 0;
  27. return buf[cell];
  28. }
  29. static char *
  30. decimal2str (const char *sign, ULONGEST addr, int width)
  31. {
  32. /* Steal code from valprint.c:print_decimal(). Should this worry
  33. about the real size of addr as the above does? */
  34. unsigned long temp[3];
  35. char *str = get_print_cell ();
  36. int i = 0;
  37. do
  38. {
  39. temp[i] = addr % (1000 * 1000 * 1000);
  40. addr /= (1000 * 1000 * 1000);
  41. i++;
  42. width -= 9;
  43. }
  44. while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
  45. width += 9;
  46. if (width < 0)
  47. width = 0;
  48. switch (i)
  49. {
  50. case 1:
  51. xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu", sign, width, temp[0]);
  52. break;
  53. case 2:
  54. xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu", sign, width,
  55. temp[1], temp[0]);
  56. break;
  57. case 3:
  58. xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu%09lu", sign, width,
  59. temp[2], temp[1], temp[0]);
  60. break;
  61. default:
  62. internal_error (__FILE__, __LINE__,
  63. _("failed internal consistency check"));
  64. }
  65. return str;
  66. }
  67. static char *
  68. octal2str (ULONGEST addr, int width)
  69. {
  70. unsigned long temp[3];
  71. char *str = get_print_cell ();
  72. int i = 0;
  73. do
  74. {
  75. temp[i] = addr % (0100000 * 0100000);
  76. addr /= (0100000 * 0100000);
  77. i++;
  78. width -= 10;
  79. }
  80. while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
  81. width += 10;
  82. if (width < 0)
  83. width = 0;
  84. switch (i)
  85. {
  86. case 1:
  87. if (temp[0] == 0)
  88. xsnprintf (str, PRINT_CELL_SIZE, "%*o", width, 0);
  89. else
  90. xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo", width, temp[0]);
  91. break;
  92. case 2:
  93. xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
  94. break;
  95. case 3:
  96. xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo%010lo", width,
  97. temp[2], temp[1], temp[0]);
  98. break;
  99. default:
  100. internal_error (__FILE__, __LINE__,
  101. _("failed internal consistency check"));
  102. }
  103. return str;
  104. }
  105. /* See print-utils.h. */
  106. char *
  107. pulongest (ULONGEST u)
  108. {
  109. return decimal2str ("", u, 0);
  110. }
  111. /* See print-utils.h. */
  112. char *
  113. plongest (LONGEST l)
  114. {
  115. if (l < 0)
  116. return decimal2str ("-", -l, 0);
  117. else
  118. return decimal2str ("", l, 0);
  119. }
  120. /* Eliminate warning from compiler on 32-bit systems. */
  121. static int thirty_two = 32;
  122. /* See print-utils.h. */
  123. char *
  124. phex (ULONGEST l, int sizeof_l)
  125. {
  126. char *str;
  127. switch (sizeof_l)
  128. {
  129. case 8:
  130. str = get_print_cell ();
  131. xsnprintf (str, PRINT_CELL_SIZE, "%08lx%08lx",
  132. (unsigned long) (l >> thirty_two),
  133. (unsigned long) (l & 0xffffffff));
  134. break;
  135. case 4:
  136. str = get_print_cell ();
  137. xsnprintf (str, PRINT_CELL_SIZE, "%08lx", (unsigned long) l);
  138. break;
  139. case 2:
  140. str = get_print_cell ();
  141. xsnprintf (str, PRINT_CELL_SIZE, "%04x", (unsigned short) (l & 0xffff));
  142. break;
  143. case 1:
  144. str = get_print_cell ();
  145. xsnprintf (str, PRINT_CELL_SIZE, "%02x", (unsigned short) (l & 0xff));
  146. break;
  147. default:
  148. str = phex (l, sizeof (l));
  149. break;
  150. }
  151. return str;
  152. }
  153. /* See print-utils.h. */
  154. char *
  155. phex_nz (ULONGEST l, int sizeof_l)
  156. {
  157. char *str;
  158. switch (sizeof_l)
  159. {
  160. case 8:
  161. {
  162. unsigned long high = (unsigned long) (l >> thirty_two);
  163. str = get_print_cell ();
  164. if (high == 0)
  165. xsnprintf (str, PRINT_CELL_SIZE, "%lx",
  166. (unsigned long) (l & 0xffffffff));
  167. else
  168. xsnprintf (str, PRINT_CELL_SIZE, "%lx%08lx", high,
  169. (unsigned long) (l & 0xffffffff));
  170. break;
  171. }
  172. case 4:
  173. str = get_print_cell ();
  174. xsnprintf (str, PRINT_CELL_SIZE, "%lx", (unsigned long) l);
  175. break;
  176. case 2:
  177. str = get_print_cell ();
  178. xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xffff));
  179. break;
  180. case 1:
  181. str = get_print_cell ();
  182. xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xff));
  183. break;
  184. default:
  185. str = phex_nz (l, sizeof (l));
  186. break;
  187. }
  188. return str;
  189. }
  190. /* See print-utils.h. */
  191. char *
  192. hex_string (LONGEST num)
  193. {
  194. char *result = get_print_cell ();
  195. xsnprintf (result, PRINT_CELL_SIZE, "0x%s", phex_nz (num, sizeof (num)));
  196. return result;
  197. }
  198. /* See print-utils.h. */
  199. char *
  200. hex_string_custom (LONGEST num, int width)
  201. {
  202. char *result = get_print_cell ();
  203. char *result_end = result + PRINT_CELL_SIZE - 1;
  204. const char *hex = phex_nz (num, sizeof (num));
  205. int hex_len = strlen (hex);
  206. if (hex_len > width)
  207. width = hex_len;
  208. if (width + 2 >= PRINT_CELL_SIZE)
  209. internal_error (__FILE__, __LINE__, _("\
  210. hex_string_custom: insufficient space to store result"));
  211. strcpy (result_end - width - 2, "0x");
  212. memset (result_end - width, '0', width);
  213. strcpy (result_end - hex_len, hex);
  214. return result_end - width - 2;
  215. }
  216. /* See print-utils.h. */
  217. char *
  218. int_string (LONGEST val, int radix, int is_signed, int width,
  219. int use_c_format)
  220. {
  221. switch (radix)
  222. {
  223. case 16:
  224. {
  225. char *result;
  226. if (width == 0)
  227. result = hex_string (val);
  228. else
  229. result = hex_string_custom (val, width);
  230. if (! use_c_format)
  231. result += 2;
  232. return result;
  233. }
  234. case 10:
  235. {
  236. if (is_signed && val < 0)
  237. return decimal2str ("-", -val, width);
  238. else
  239. return decimal2str ("", val, width);
  240. }
  241. case 8:
  242. {
  243. char *result = octal2str (val, width);
  244. if (use_c_format || val == 0)
  245. return result;
  246. else
  247. return result + 1;
  248. }
  249. default:
  250. internal_error (__FILE__, __LINE__,
  251. _("failed internal consistency check"));
  252. }
  253. }
  254. /* See print-utils.h. */
  255. const char *
  256. core_addr_to_string (const CORE_ADDR addr)
  257. {
  258. char *str = get_print_cell ();
  259. strcpy (str, "0x");
  260. strcat (str, phex (addr, sizeof (addr)));
  261. return str;
  262. }
  263. /* See print-utils.h. */
  264. const char *
  265. core_addr_to_string_nz (const CORE_ADDR addr)
  266. {
  267. char *str = get_print_cell ();
  268. strcpy (str, "0x");
  269. strcat (str, phex_nz (addr, sizeof (addr)));
  270. return str;
  271. }
  272. /* See print-utils.h. */
  273. const char *
  274. host_address_to_string_1 (const void *addr)
  275. {
  276. char *str = get_print_cell ();
  277. xsnprintf (str, PRINT_CELL_SIZE, "0x%s",
  278. phex_nz ((uintptr_t) addr, sizeof (addr)));
  279. return str;
  280. }