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-utils.h"
21 #include "host-defs.h"
22 #include "gdbsupport/gdb-safe-ctype.h"
23 #include "gdbsupport/gdb-xfree.h"
28 return xcalloc (1, size
);
31 /* Like asprintf/vasprintf but get an internal_error if the call
34 gdb::unique_xmalloc_ptr
<char>
35 xstrprintf (const char *format
, ...)
39 va_start (args
, format
);
40 gdb::unique_xmalloc_ptr
<char> ret
= xstrvprintf (format
, args
);
45 gdb::unique_xmalloc_ptr
<char>
46 xstrvprintf (const char *format
, va_list ap
)
49 int status
= vasprintf (&ret
, format
, ap
);
51 /* NULL is returned when there was a memory allocation problem, or
52 any other error (for instance, a bad format string). A negative
53 status (the printed length) with a non-NULL buffer should never
54 happen, but just to be sure. */
55 if (ret
== NULL
|| status
< 0)
56 internal_error (_("vasprintf call failed"));
57 return gdb::unique_xmalloc_ptr
<char> (ret
);
61 xsnprintf (char *str
, size_t size
, const char *format
, ...)
66 va_start (args
, format
);
67 ret
= vsnprintf (str
, size
, format
, args
);
68 gdb_assert (ret
< size
);
74 /* See documentation in common-utils.h. */
77 string_printf (const char* fmt
, ...)
83 size
= vsnprintf (NULL
, 0, fmt
, vp
);
86 std::string
str (size
, '\0');
88 /* C++11 and later guarantee std::string uses contiguous memory and
89 always includes the terminating '\0'. */
91 vsprintf (&str
[0], fmt
, vp
); /* ARI: vsprintf */
97 /* See documentation in common-utils.h. */
100 string_vprintf (const char* fmt
, va_list args
)
106 size
= vsnprintf (NULL
, 0, fmt
, vp
);
109 std::string
str (size
, '\0');
111 /* C++11 and later guarantee std::string uses contiguous memory and
112 always includes the terminating '\0'. */
113 vsprintf (&str
[0], fmt
, args
); /* ARI: vsprintf */
119 /* See documentation in common-utils.h. */
122 string_appendf (std::string
&str
, const char *fmt
, ...)
127 string_vappendf (str
, fmt
, vp
);
134 /* See documentation in common-utils.h. */
137 string_vappendf (std::string
&str
, const char *fmt
, va_list args
)
143 grow_size
= vsnprintf (NULL
, 0, fmt
, vp
);
146 size_t curr_size
= str
.size ();
147 str
.resize (curr_size
+ grow_size
);
149 /* C++11 and later guarantee std::string uses contiguous memory and
150 always includes the terminating '\0'. */
151 vsprintf (&str
[curr_size
], fmt
, args
); /* ARI: vsprintf */
157 savestring (const char *ptr
, size_t len
)
159 char *p
= (char *) xmalloc (len
+ 1);
161 memcpy (p
, ptr
, len
);
166 /* See documentation in common-utils.h. */
169 extract_string_maybe_quoted (const char **arg
)
173 bool bsquote
= false;
175 const char *p
= *arg
;
177 /* Find the start of the argument. */
180 /* Parse p similarly to gdb_argv buildargv function. */
183 if (ISSPACE (*p
) && !squote
&& !dquote
&& !bsquote
)
225 /* See gdbsupport/common-utils.h. */
228 make_quoted_string (const char *str
)
230 gdb_assert (str
!= nullptr);
234 for (; *str
!= '\0'; ++str
)
236 const char ch
= *str
;
238 if (strchr ("\"' \t\n", ch
) != nullptr)
239 result
.push_back ('\\');
240 result
.push_back (ch
);
246 /* The bit offset of the highest byte in a ULONGEST, for overflow
249 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
251 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
252 where 2 <= BASE <= 36. */
255 is_digit_in_base (unsigned char digit
, int base
)
257 if (!ISALNUM (digit
))
260 return (ISDIGIT (digit
) && digit
< base
+ '0');
262 return (ISDIGIT (digit
) || TOLOWER (digit
) < base
- 10 + 'a');
266 digit_to_int (unsigned char c
)
271 return TOLOWER (c
) - 'a' + 10;
274 /* As for strtoul, but for ULONGEST results. */
277 strtoulst (const char *num
, const char **trailer
, int base
)
279 unsigned int high_part
;
284 /* Skip leading whitespace. */
285 while (ISSPACE (num
[i
]))
288 /* Handle prefixes. */
291 else if (num
[i
] == '-')
297 if (base
== 0 || base
== 16)
299 if (num
[i
] == '0' && (num
[i
+ 1] == 'x' || num
[i
+ 1] == 'X'))
307 if (base
== 0 && num
[i
] == '0')
313 if (base
< 2 || base
> 36)
319 result
= high_part
= 0;
320 for (; is_digit_in_base (num
[i
], base
); i
+= 1)
322 result
= result
* base
+ digit_to_int (num
[i
]);
323 high_part
= high_part
* base
+ (unsigned int) (result
>> HIGH_BYTE_POSN
);
324 result
&= ((ULONGEST
) 1 << HIGH_BYTE_POSN
) - 1;
325 if (high_part
> 0xff)
328 result
= ~ (ULONGEST
) 0;
338 result
= result
+ ((ULONGEST
) high_part
<< HIGH_BYTE_POSN
);
345 /* See documentation in common-utils.h. */
348 skip_spaces (char *chp
)
352 while (*chp
&& ISSPACE (*chp
))
357 /* A const-correct version of the above. */
360 skip_spaces (const char *chp
)
364 while (*chp
&& ISSPACE (*chp
))
369 /* See documentation in common-utils.h. */
372 skip_to_space (const char *chp
)
376 while (*chp
&& !ISSPACE (*chp
))
381 /* See documentation in common-utils.h. */
384 skip_to_space (char *chp
)
386 return (char *) skip_to_space ((const char *) chp
);
389 /* See gdbsupport/common-utils.h. */
392 free_vector_argv (std::vector
<char *> &v
)
400 /* See gdbsupport/common-utils.h. */
403 align_up (ULONGEST v
, int n
)
405 /* Check that N is really a power of two. */
406 gdb_assert (n
&& (n
& (n
-1)) == 0);
407 return (v
+ n
- 1) & -n
;
410 /* See gdbsupport/common-utils.h. */
413 align_down (ULONGEST v
, int n
)
415 /* Check that N is really a power of two. */
416 gdb_assert (n
&& (n
& (n
-1)) == 0);
420 /* See gdbsupport/common-utils.h. */
425 if (a
>= '0' && a
<= '9')
427 else if (a
>= 'a' && a
<= 'f')
429 else if (a
>= 'A' && a
<= 'F')
432 error (_("Invalid hex digit %d"), a
);
435 /* See gdbsupport/common-utils.h. */
438 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
442 for (i
= 0; i
< count
; i
++)
444 if (hex
[0] == 0 || hex
[1] == 0)
446 /* Hex string is short, or of uneven length.
447 Return the count that has been converted so far. */
450 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
456 /* See gdbsupport/common-utils.h. */
459 hex2bin (const char *hex
)
461 size_t bin_len
= strlen (hex
) / 2;
462 gdb::byte_vector
bin (bin_len
);
464 hex2bin (hex
, bin
.data (), bin_len
);
469 /* See gdbsupport/common-utils.h. */
472 bytes_to_string (gdb::array_view
<const gdb_byte
> bytes
)
476 for (size_t i
= 0; i
< bytes
.size (); i
++)
479 ret
+= string_printf ("%02x", bytes
[i
]);
481 ret
+= string_printf (" %02x", bytes
[i
]);