common-utils.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Shared general 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 "common-utils.h"
  16. #include "host-defs.h"
  17. #include "safe-ctype.h"
  18. #include "gdbsupport/gdb-xfree.h"
  19. void *
  20. xzalloc (size_t size)
  21. {
  22. return xcalloc (1, size);
  23. }
  24. /* Like asprintf/vasprintf but get an internal_error if the call
  25. fails. */
  26. gdb::unique_xmalloc_ptr<char>
  27. xstrprintf (const char *format, ...)
  28. {
  29. va_list args;
  30. va_start (args, format);
  31. gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
  32. va_end (args);
  33. return ret;
  34. }
  35. gdb::unique_xmalloc_ptr<char>
  36. xstrvprintf (const char *format, va_list ap)
  37. {
  38. char *ret = NULL;
  39. int status = vasprintf (&ret, format, ap);
  40. /* NULL is returned when there was a memory allocation problem, or
  41. any other error (for instance, a bad format string). A negative
  42. status (the printed length) with a non-NULL buffer should never
  43. happen, but just to be sure. */
  44. if (ret == NULL || status < 0)
  45. internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
  46. return gdb::unique_xmalloc_ptr<char> (ret);
  47. }
  48. int
  49. xsnprintf (char *str, size_t size, const char *format, ...)
  50. {
  51. va_list args;
  52. int ret;
  53. va_start (args, format);
  54. ret = vsnprintf (str, size, format, args);
  55. gdb_assert (ret < size);
  56. va_end (args);
  57. return ret;
  58. }
  59. /* See documentation in common-utils.h. */
  60. std::string
  61. string_printf (const char* fmt, ...)
  62. {
  63. va_list vp;
  64. int size;
  65. va_start (vp, fmt);
  66. size = vsnprintf (NULL, 0, fmt, vp);
  67. va_end (vp);
  68. std::string str (size, '\0');
  69. /* C++11 and later guarantee std::string uses contiguous memory and
  70. always includes the terminating '\0'. */
  71. va_start (vp, fmt);
  72. vsprintf (&str[0], fmt, vp); /* ARI: vsprintf */
  73. va_end (vp);
  74. return str;
  75. }
  76. /* See documentation in common-utils.h. */
  77. std::string
  78. string_vprintf (const char* fmt, va_list args)
  79. {
  80. va_list vp;
  81. size_t size;
  82. va_copy (vp, args);
  83. size = vsnprintf (NULL, 0, fmt, vp);
  84. va_end (vp);
  85. std::string str (size, '\0');
  86. /* C++11 and later guarantee std::string uses contiguous memory and
  87. always includes the terminating '\0'. */
  88. vsprintf (&str[0], fmt, args); /* ARI: vsprintf */
  89. return str;
  90. }
  91. /* See documentation in common-utils.h. */
  92. std::string &
  93. string_appendf (std::string &str, const char *fmt, ...)
  94. {
  95. va_list vp;
  96. va_start (vp, fmt);
  97. string_vappendf (str, fmt, vp);
  98. va_end (vp);
  99. return str;
  100. }
  101. /* See documentation in common-utils.h. */
  102. std::string &
  103. string_vappendf (std::string &str, const char *fmt, va_list args)
  104. {
  105. va_list vp;
  106. int grow_size;
  107. va_copy (vp, args);
  108. grow_size = vsnprintf (NULL, 0, fmt, vp);
  109. va_end (vp);
  110. size_t curr_size = str.size ();
  111. str.resize (curr_size + grow_size);
  112. /* C++11 and later guarantee std::string uses contiguous memory and
  113. always includes the terminating '\0'. */
  114. vsprintf (&str[curr_size], fmt, args); /* ARI: vsprintf */
  115. return str;
  116. }
  117. char *
  118. savestring (const char *ptr, size_t len)
  119. {
  120. char *p = (char *) xmalloc (len + 1);
  121. memcpy (p, ptr, len);
  122. p[len] = 0;
  123. return p;
  124. }
  125. /* See documentation in common-utils.h. */
  126. std::string
  127. extract_string_maybe_quoted (const char **arg)
  128. {
  129. bool squote = false;
  130. bool dquote = false;
  131. bool bsquote = false;
  132. std::string result;
  133. const char *p = *arg;
  134. /* Find the start of the argument. */
  135. p = skip_spaces (p);
  136. /* Parse p similarly to gdb_argv buildargv function. */
  137. while (*p != '\0')
  138. {
  139. if (ISSPACE (*p) && !squote && !dquote && !bsquote)
  140. break;
  141. else
  142. {
  143. if (bsquote)
  144. {
  145. bsquote = false;
  146. result += *p;
  147. }
  148. else if (*p == '\\')
  149. bsquote = true;
  150. else if (squote)
  151. {
  152. if (*p == '\'')
  153. squote = false;
  154. else
  155. result += *p;
  156. }
  157. else if (dquote)
  158. {
  159. if (*p == '"')
  160. dquote = false;
  161. else
  162. result += *p;
  163. }
  164. else
  165. {
  166. if (*p == '\'')
  167. squote = true;
  168. else if (*p == '"')
  169. dquote = true;
  170. else
  171. result += *p;
  172. }
  173. p++;
  174. }
  175. }
  176. *arg = p;
  177. return result;
  178. }
  179. /* The bit offset of the highest byte in a ULONGEST, for overflow
  180. checking. */
  181. #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
  182. /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
  183. where 2 <= BASE <= 36. */
  184. static int
  185. is_digit_in_base (unsigned char digit, int base)
  186. {
  187. if (!ISALNUM (digit))
  188. return 0;
  189. if (base <= 10)
  190. return (ISDIGIT (digit) && digit < base + '0');
  191. else
  192. return (ISDIGIT (digit) || TOLOWER (digit) < base - 10 + 'a');
  193. }
  194. static int
  195. digit_to_int (unsigned char c)
  196. {
  197. if (ISDIGIT (c))
  198. return c - '0';
  199. else
  200. return TOLOWER (c) - 'a' + 10;
  201. }
  202. /* As for strtoul, but for ULONGEST results. */
  203. ULONGEST
  204. strtoulst (const char *num, const char **trailer, int base)
  205. {
  206. unsigned int high_part;
  207. ULONGEST result;
  208. int minus = 0;
  209. int i = 0;
  210. /* Skip leading whitespace. */
  211. while (ISSPACE (num[i]))
  212. i++;
  213. /* Handle prefixes. */
  214. if (num[i] == '+')
  215. i++;
  216. else if (num[i] == '-')
  217. {
  218. minus = 1;
  219. i++;
  220. }
  221. if (base == 0 || base == 16)
  222. {
  223. if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
  224. {
  225. i += 2;
  226. if (base == 0)
  227. base = 16;
  228. }
  229. }
  230. if (base == 0 && num[i] == '0')
  231. base = 8;
  232. if (base == 0)
  233. base = 10;
  234. if (base < 2 || base > 36)
  235. {
  236. errno = EINVAL;
  237. return 0;
  238. }
  239. result = high_part = 0;
  240. for (; is_digit_in_base (num[i], base); i += 1)
  241. {
  242. result = result * base + digit_to_int (num[i]);
  243. high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
  244. result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
  245. if (high_part > 0xff)
  246. {
  247. errno = ERANGE;
  248. result = ~ (ULONGEST) 0;
  249. high_part = 0;
  250. minus = 0;
  251. break;
  252. }
  253. }
  254. if (trailer != NULL)
  255. *trailer = &num[i];
  256. result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
  257. if (minus)
  258. return -result;
  259. else
  260. return result;
  261. }
  262. /* See documentation in common-utils.h. */
  263. char *
  264. skip_spaces (char *chp)
  265. {
  266. if (chp == NULL)
  267. return NULL;
  268. while (*chp && ISSPACE (*chp))
  269. chp++;
  270. return chp;
  271. }
  272. /* A const-correct version of the above. */
  273. const char *
  274. skip_spaces (const char *chp)
  275. {
  276. if (chp == NULL)
  277. return NULL;
  278. while (*chp && ISSPACE (*chp))
  279. chp++;
  280. return chp;
  281. }
  282. /* See documentation in common-utils.h. */
  283. const char *
  284. skip_to_space (const char *chp)
  285. {
  286. if (chp == NULL)
  287. return NULL;
  288. while (*chp && !ISSPACE (*chp))
  289. chp++;
  290. return chp;
  291. }
  292. /* See documentation in common-utils.h. */
  293. char *
  294. skip_to_space (char *chp)
  295. {
  296. return (char *) skip_to_space ((const char *) chp);
  297. }
  298. /* See gdbsupport/common-utils.h. */
  299. void
  300. free_vector_argv (std::vector<char *> &v)
  301. {
  302. for (char *el : v)
  303. xfree (el);
  304. v.clear ();
  305. }
  306. /* See gdbsupport/common-utils.h. */
  307. ULONGEST
  308. align_up (ULONGEST v, int n)
  309. {
  310. /* Check that N is really a power of two. */
  311. gdb_assert (n && (n & (n-1)) == 0);
  312. return (v + n - 1) & -n;
  313. }
  314. /* See gdbsupport/common-utils.h. */
  315. ULONGEST
  316. align_down (ULONGEST v, int n)
  317. {
  318. /* Check that N is really a power of two. */
  319. gdb_assert (n && (n & (n-1)) == 0);
  320. return (v & -n);
  321. }
  322. /* See gdbsupport/common-utils.h. */
  323. int
  324. fromhex (int a)
  325. {
  326. if (a >= '0' && a <= '9')
  327. return a - '0';
  328. else if (a >= 'a' && a <= 'f')
  329. return a - 'a' + 10;
  330. else if (a >= 'A' && a <= 'F')
  331. return a - 'A' + 10;
  332. else
  333. error (_("Invalid hex digit %d"), a);
  334. }
  335. /* See gdbsupport/common-utils.h. */
  336. int
  337. hex2bin (const char *hex, gdb_byte *bin, int count)
  338. {
  339. int i;
  340. for (i = 0; i < count; i++)
  341. {
  342. if (hex[0] == 0 || hex[1] == 0)
  343. {
  344. /* Hex string is short, or of uneven length.
  345. Return the count that has been converted so far. */
  346. return i;
  347. }
  348. *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
  349. hex += 2;
  350. }
  351. return i;
  352. }
  353. /* See gdbsupport/common-utils.h. */
  354. gdb::byte_vector
  355. hex2bin (const char *hex)
  356. {
  357. size_t bin_len = strlen (hex) / 2;
  358. gdb::byte_vector bin (bin_len);
  359. hex2bin (hex, bin.data (), bin_len);
  360. return bin;
  361. }