[gdb/testsuite] Call ldd --version in gdb.testsuite/dump-system-info.exp
[binutils-gdb.git] / gdbsupport / common-utils.cc
blob99b9cb8609bbf4e0b6822122a259b5fa7248793f
1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "common-utils.h"
22 #include "host-defs.h"
23 #include "gdbsupport/gdb-safe-ctype.h"
24 #include "gdbsupport/gdb-xfree.h"
26 void *
27 xzalloc (size_t size)
29 return xcalloc (1, size);
32 /* Like asprintf/vasprintf but get an internal_error if the call
33 fails. */
35 gdb::unique_xmalloc_ptr<char>
36 xstrprintf (const char *format, ...)
38 va_list args;
40 va_start (args, format);
41 gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
42 va_end (args);
43 return ret;
46 gdb::unique_xmalloc_ptr<char>
47 xstrvprintf (const char *format, va_list ap)
49 char *ret = NULL;
50 int status = vasprintf (&ret, format, ap);
52 /* NULL is returned when there was a memory allocation problem, or
53 any other error (for instance, a bad format string). A negative
54 status (the printed length) with a non-NULL buffer should never
55 happen, but just to be sure. */
56 if (ret == NULL || status < 0)
57 internal_error (_("vasprintf call failed"));
58 return gdb::unique_xmalloc_ptr<char> (ret);
61 int
62 xsnprintf (char *str, size_t size, const char *format, ...)
64 va_list args;
65 int ret;
67 va_start (args, format);
68 ret = vsnprintf (str, size, format, args);
69 gdb_assert (ret < size);
70 va_end (args);
72 return ret;
75 /* See documentation in common-utils.h. */
77 std::string
78 string_printf (const char* fmt, ...)
80 va_list vp;
81 int size;
83 va_start (vp, fmt);
84 size = vsnprintf (NULL, 0, fmt, vp);
85 va_end (vp);
87 std::string str (size, '\0');
89 /* C++11 and later guarantee std::string uses contiguous memory and
90 always includes the terminating '\0'. */
91 va_start (vp, fmt);
92 vsprintf (&str[0], fmt, vp); /* ARI: vsprintf */
93 va_end (vp);
95 return str;
98 /* See documentation in common-utils.h. */
100 std::string
101 string_vprintf (const char* fmt, va_list args)
103 va_list vp;
104 size_t size;
106 va_copy (vp, args);
107 size = vsnprintf (NULL, 0, fmt, vp);
108 va_end (vp);
110 std::string str (size, '\0');
112 /* C++11 and later guarantee std::string uses contiguous memory and
113 always includes the terminating '\0'. */
114 vsprintf (&str[0], fmt, args); /* ARI: vsprintf */
116 return str;
120 /* See documentation in common-utils.h. */
122 std::string &
123 string_appendf (std::string &str, const char *fmt, ...)
125 va_list vp;
127 va_start (vp, fmt);
128 string_vappendf (str, fmt, vp);
129 va_end (vp);
131 return str;
135 /* See documentation in common-utils.h. */
137 std::string &
138 string_vappendf (std::string &str, const char *fmt, va_list args)
140 va_list vp;
141 int grow_size;
143 va_copy (vp, args);
144 grow_size = vsnprintf (NULL, 0, fmt, vp);
145 va_end (vp);
147 size_t curr_size = str.size ();
148 str.resize (curr_size + grow_size);
150 /* C++11 and later guarantee std::string uses contiguous memory and
151 always includes the terminating '\0'. */
152 vsprintf (&str[curr_size], fmt, args); /* ARI: vsprintf */
154 return str;
157 char *
158 savestring (const char *ptr, size_t len)
160 char *p = (char *) xmalloc (len + 1);
162 memcpy (p, ptr, len);
163 p[len] = 0;
164 return p;
167 /* See documentation in common-utils.h. */
169 std::string
170 extract_string_maybe_quoted (const char **arg)
172 bool squote = false;
173 bool dquote = false;
174 bool bsquote = false;
175 std::string result;
176 const char *p = *arg;
178 /* Find the start of the argument. */
179 p = skip_spaces (p);
181 /* Parse p similarly to gdb_argv buildargv function. */
182 while (*p != '\0')
184 if (ISSPACE (*p) && !squote && !dquote && !bsquote)
185 break;
186 else
188 if (bsquote)
190 bsquote = false;
191 result += *p;
193 else if (*p == '\\')
194 bsquote = true;
195 else if (squote)
197 if (*p == '\'')
198 squote = false;
199 else
200 result += *p;
202 else if (dquote)
204 if (*p == '"')
205 dquote = false;
206 else
207 result += *p;
209 else
211 if (*p == '\'')
212 squote = true;
213 else if (*p == '"')
214 dquote = true;
215 else
216 result += *p;
218 p++;
222 *arg = p;
223 return result;
226 /* The bit offset of the highest byte in a ULONGEST, for overflow
227 checking. */
229 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
231 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
232 where 2 <= BASE <= 36. */
234 static int
235 is_digit_in_base (unsigned char digit, int base)
237 if (!ISALNUM (digit))
238 return 0;
239 if (base <= 10)
240 return (ISDIGIT (digit) && digit < base + '0');
241 else
242 return (ISDIGIT (digit) || TOLOWER (digit) < base - 10 + 'a');
245 static int
246 digit_to_int (unsigned char c)
248 if (ISDIGIT (c))
249 return c - '0';
250 else
251 return TOLOWER (c) - 'a' + 10;
254 /* As for strtoul, but for ULONGEST results. */
256 ULONGEST
257 strtoulst (const char *num, const char **trailer, int base)
259 unsigned int high_part;
260 ULONGEST result;
261 int minus = 0;
262 int i = 0;
264 /* Skip leading whitespace. */
265 while (ISSPACE (num[i]))
266 i++;
268 /* Handle prefixes. */
269 if (num[i] == '+')
270 i++;
271 else if (num[i] == '-')
273 minus = 1;
274 i++;
277 if (base == 0 || base == 16)
279 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
281 i += 2;
282 if (base == 0)
283 base = 16;
287 if (base == 0 && num[i] == '0')
288 base = 8;
290 if (base == 0)
291 base = 10;
293 if (base < 2 || base > 36)
295 errno = EINVAL;
296 return 0;
299 result = high_part = 0;
300 for (; is_digit_in_base (num[i], base); i += 1)
302 result = result * base + digit_to_int (num[i]);
303 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
304 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
305 if (high_part > 0xff)
307 errno = ERANGE;
308 result = ~ (ULONGEST) 0;
309 high_part = 0;
310 minus = 0;
311 break;
315 if (trailer != NULL)
316 *trailer = &num[i];
318 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
319 if (minus)
320 return -result;
321 else
322 return result;
325 /* See documentation in common-utils.h. */
327 char *
328 skip_spaces (char *chp)
330 if (chp == NULL)
331 return NULL;
332 while (*chp && ISSPACE (*chp))
333 chp++;
334 return chp;
337 /* A const-correct version of the above. */
339 const char *
340 skip_spaces (const char *chp)
342 if (chp == NULL)
343 return NULL;
344 while (*chp && ISSPACE (*chp))
345 chp++;
346 return chp;
349 /* See documentation in common-utils.h. */
351 const char *
352 skip_to_space (const char *chp)
354 if (chp == NULL)
355 return NULL;
356 while (*chp && !ISSPACE (*chp))
357 chp++;
358 return chp;
361 /* See documentation in common-utils.h. */
363 char *
364 skip_to_space (char *chp)
366 return (char *) skip_to_space ((const char *) chp);
369 /* See gdbsupport/common-utils.h. */
371 void
372 free_vector_argv (std::vector<char *> &v)
374 for (char *el : v)
375 xfree (el);
377 v.clear ();
380 /* See gdbsupport/common-utils.h. */
382 ULONGEST
383 align_up (ULONGEST v, int n)
385 /* Check that N is really a power of two. */
386 gdb_assert (n && (n & (n-1)) == 0);
387 return (v + n - 1) & -n;
390 /* See gdbsupport/common-utils.h. */
392 ULONGEST
393 align_down (ULONGEST v, int n)
395 /* Check that N is really a power of two. */
396 gdb_assert (n && (n & (n-1)) == 0);
397 return (v & -n);
400 /* See gdbsupport/common-utils.h. */
403 fromhex (int a)
405 if (a >= '0' && a <= '9')
406 return a - '0';
407 else if (a >= 'a' && a <= 'f')
408 return a - 'a' + 10;
409 else if (a >= 'A' && a <= 'F')
410 return a - 'A' + 10;
411 else
412 error (_("Invalid hex digit %d"), a);
415 /* See gdbsupport/common-utils.h. */
418 hex2bin (const char *hex, gdb_byte *bin, int count)
420 int i;
422 for (i = 0; i < count; i++)
424 if (hex[0] == 0 || hex[1] == 0)
426 /* Hex string is short, or of uneven length.
427 Return the count that has been converted so far. */
428 return i;
430 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
431 hex += 2;
433 return i;
436 /* See gdbsupport/common-utils.h. */
438 gdb::byte_vector
439 hex2bin (const char *hex)
441 size_t bin_len = strlen (hex) / 2;
442 gdb::byte_vector bin (bin_len);
444 hex2bin (hex, bin.data (), bin_len);
446 return bin;
449 /* See gdbsupport/common-utils.h. */
451 std::string
452 bytes_to_string (gdb::array_view<const gdb_byte> bytes)
454 std::string ret;
456 for (size_t i = 0; i < bytes.size (); i++)
458 if (i == 0)
459 ret += string_printf ("%02x", bytes[i]);
460 else
461 ret += string_printf (" %02x", bytes[i]);
464 return ret;