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"
29 return xcalloc (1, size
);
32 /* Like asprintf/vasprintf but get an internal_error if the call
35 gdb::unique_xmalloc_ptr
<char>
36 xstrprintf (const char *format
, ...)
40 va_start (args
, format
);
41 gdb::unique_xmalloc_ptr
<char> ret
= xstrvprintf (format
, args
);
46 gdb::unique_xmalloc_ptr
<char>
47 xstrvprintf (const char *format
, va_list ap
)
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
);
62 xsnprintf (char *str
, size_t size
, const char *format
, ...)
67 va_start (args
, format
);
68 ret
= vsnprintf (str
, size
, format
, args
);
69 gdb_assert (ret
< size
);
75 /* See documentation in common-utils.h. */
78 string_printf (const char* fmt
, ...)
84 size
= vsnprintf (NULL
, 0, fmt
, 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'. */
92 vsprintf (&str
[0], fmt
, vp
); /* ARI: vsprintf */
98 /* See documentation in common-utils.h. */
101 string_vprintf (const char* fmt
, va_list args
)
107 size
= vsnprintf (NULL
, 0, fmt
, 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 */
120 /* See documentation in common-utils.h. */
123 string_appendf (std::string
&str
, const char *fmt
, ...)
128 string_vappendf (str
, fmt
, vp
);
135 /* See documentation in common-utils.h. */
138 string_vappendf (std::string
&str
, const char *fmt
, va_list args
)
144 grow_size
= vsnprintf (NULL
, 0, fmt
, 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 */
158 savestring (const char *ptr
, size_t len
)
160 char *p
= (char *) xmalloc (len
+ 1);
162 memcpy (p
, ptr
, len
);
167 /* See documentation in common-utils.h. */
170 extract_string_maybe_quoted (const char **arg
)
174 bool bsquote
= false;
176 const char *p
= *arg
;
178 /* Find the start of the argument. */
181 /* Parse p similarly to gdb_argv buildargv function. */
184 if (ISSPACE (*p
) && !squote
&& !dquote
&& !bsquote
)
226 /* The bit offset of the highest byte in a ULONGEST, for overflow
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. */
235 is_digit_in_base (unsigned char digit
, int base
)
237 if (!ISALNUM (digit
))
240 return (ISDIGIT (digit
) && digit
< base
+ '0');
242 return (ISDIGIT (digit
) || TOLOWER (digit
) < base
- 10 + 'a');
246 digit_to_int (unsigned char c
)
251 return TOLOWER (c
) - 'a' + 10;
254 /* As for strtoul, but for ULONGEST results. */
257 strtoulst (const char *num
, const char **trailer
, int base
)
259 unsigned int high_part
;
264 /* Skip leading whitespace. */
265 while (ISSPACE (num
[i
]))
268 /* Handle prefixes. */
271 else if (num
[i
] == '-')
277 if (base
== 0 || base
== 16)
279 if (num
[i
] == '0' && (num
[i
+ 1] == 'x' || num
[i
+ 1] == 'X'))
287 if (base
== 0 && num
[i
] == '0')
293 if (base
< 2 || base
> 36)
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)
308 result
= ~ (ULONGEST
) 0;
318 result
= result
+ ((ULONGEST
) high_part
<< HIGH_BYTE_POSN
);
325 /* See documentation in common-utils.h. */
328 skip_spaces (char *chp
)
332 while (*chp
&& ISSPACE (*chp
))
337 /* A const-correct version of the above. */
340 skip_spaces (const char *chp
)
344 while (*chp
&& ISSPACE (*chp
))
349 /* See documentation in common-utils.h. */
352 skip_to_space (const char *chp
)
356 while (*chp
&& !ISSPACE (*chp
))
361 /* See documentation in common-utils.h. */
364 skip_to_space (char *chp
)
366 return (char *) skip_to_space ((const char *) chp
);
369 /* See gdbsupport/common-utils.h. */
372 free_vector_argv (std::vector
<char *> &v
)
380 /* See gdbsupport/common-utils.h. */
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. */
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);
400 /* See gdbsupport/common-utils.h. */
405 if (a
>= '0' && a
<= '9')
407 else if (a
>= 'a' && a
<= 'f')
409 else if (a
>= 'A' && a
<= 'F')
412 error (_("Invalid hex digit %d"), a
);
415 /* See gdbsupport/common-utils.h. */
418 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
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. */
430 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
436 /* See gdbsupport/common-utils.h. */
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
);
449 /* See gdbsupport/common-utils.h. */
452 bytes_to_string (gdb::array_view
<const gdb_byte
> bytes
)
456 for (size_t i
= 0; i
< bytes
.size (); i
++)
459 ret
+= string_printf ("%02x", bytes
[i
]);
461 ret
+= string_printf (" %02x", bytes
[i
]);