1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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"
28 return xcalloc (1, size);
31 /* Like asprintf/vasprintf but get an internal_error if the call
35 xstrprintf (const char *format, ...)
40 va_start (args, format);
41 ret = xstrvprintf (format, args);
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 (__FILE__, __LINE__, _("vasprintf call failed"));
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);
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);
120 /* See documentation in common-utils.h. */
123 string_appendf (std::string &str, const char *fmt, ...)
128 string_vappendf (str, fmt, vp);
133 /* See documentation in common-utils.h. */
136 string_vappendf (std::string &str, const char *fmt, va_list args)
142 grow_size = vsnprintf (NULL, 0, fmt, vp);
145 size_t curr_size = str.size ();
146 str.resize (curr_size + grow_size);
148 /* C++11 and later guarantee std::string uses contiguous memory and
149 always includes the terminating '\0'. */
150 vsprintf (&str[curr_size], fmt, args);
154 savestring (const char *ptr, size_t len)
156 char *p = (char *) xmalloc (len + 1);
158 memcpy (p, ptr, len);
163 /* See documentation in common-utils.h. */
166 extract_string_maybe_quoted (const char **arg)
170 bool bsquote = false;
172 const char *p = *arg;
174 /* Find the start of the argument. */
177 /* Parse p similarly to gdb_argv buildargv function. */
180 if (isspace (*p) && !squote && !dquote && !bsquote)
222 /* The bit offset of the highest byte in a ULONGEST, for overflow
225 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
227 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
228 where 2 <= BASE <= 36. */
231 is_digit_in_base (unsigned char digit, int base)
233 if (!isalnum (digit))
236 return (isdigit (digit) && digit < base + '0');
238 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
242 digit_to_int (unsigned char c)
247 return tolower (c) - 'a' + 10;
250 /* As for strtoul, but for ULONGEST results. */
253 strtoulst (const char *num, const char **trailer, int base)
255 unsigned int high_part;
260 /* Skip leading whitespace. */
261 while (isspace (num[i]))
264 /* Handle prefixes. */
267 else if (num[i] == '-')
273 if (base == 0 || base == 16)
275 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
283 if (base == 0 && num[i] == '0')
289 if (base < 2 || base > 36)
295 result = high_part = 0;
296 for (; is_digit_in_base (num[i], base); i += 1)
298 result = result * base + digit_to_int (num[i]);
299 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
300 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
301 if (high_part > 0xff)
304 result = ~ (ULONGEST) 0;
314 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
321 /* See documentation in common-utils.h. */
324 skip_spaces (char *chp)
328 while (*chp && isspace (*chp))
333 /* A const-correct version of the above. */
336 skip_spaces (const char *chp)
340 while (*chp && isspace (*chp))
345 /* See documentation in common-utils.h. */
348 skip_to_space (const char *chp)
352 while (*chp && !isspace (*chp))
357 /* See documentation in common-utils.h. */
360 skip_to_space (char *chp)
362 return (char *) skip_to_space ((const char *) chp);
365 /* See gdbsupport/common-utils.h. */
368 free_vector_argv (std::vector<char *> &v)
376 /* See gdbsupport/common-utils.h. */
379 stringify_argv (const std::vector<char *> &args)
383 if (!args.empty () && args[0] != NULL)
392 /* Erase the last whitespace. */
393 ret.erase (ret.end () - 1);
399 /* See gdbsupport/common-utils.h. */
402 align_up (ULONGEST v, int n)
404 /* Check that N is really a power of two. */
405 gdb_assert (n && (n & (n-1)) == 0);
406 return (v + n - 1) & -n;
409 /* See gdbsupport/common-utils.h. */
412 align_down (ULONGEST v, int n)
414 /* Check that N is really a power of two. */
415 gdb_assert (n && (n & (n-1)) == 0);