1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 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"
26 /* The xmalloc() (libiberty.h) family of memory management routines.
28 These are like the ISO-C malloc() family except that they implement
29 consistent semantics and guard against typical memory management
32 /* NOTE: These are declared using PTR to ensure consistency with
33 "libiberty.h". xfree() is GDB local. */
40 /* See libiberty/xmalloc.c. This function need's to match that's
41 semantics. It never returns NULL. */
45 val
= malloc (size
); /* ARI: malloc */
47 malloc_failure (size
);
53 xrealloc (PTR ptr
, size_t size
) /* ARI: PTR */
57 /* See libiberty/xmalloc.c. This function need's to match that's
58 semantics. It never returns NULL. */
63 val
= realloc (ptr
, size
); /* ARI: realloc */
65 val
= malloc (size
); /* ARI: malloc */
67 malloc_failure (size
);
73 xcalloc (size_t number
, size_t size
)
77 /* See libiberty/xmalloc.c. This function need's to match that's
78 semantics. It never returns NULL. */
79 if (number
== 0 || size
== 0)
85 mem
= calloc (number
, size
); /* ARI: xcalloc */
87 malloc_failure (number
* size
);
95 return xcalloc (1, size
);
99 xmalloc_failed (size_t size
)
101 malloc_failure (size
);
104 /* Like asprintf/vasprintf but get an internal_error if the call
108 xstrprintf (const char *format
, ...)
113 va_start (args
, format
);
114 ret
= xstrvprintf (format
, args
);
120 xstrvprintf (const char *format
, va_list ap
)
123 int status
= vasprintf (&ret
, format
, ap
);
125 /* NULL is returned when there was a memory allocation problem, or
126 any other error (for instance, a bad format string). A negative
127 status (the printed length) with a non-NULL buffer should never
128 happen, but just to be sure. */
129 if (ret
== NULL
|| status
< 0)
130 internal_error (__FILE__
, __LINE__
, _("vasprintf call failed"));
135 xsnprintf (char *str
, size_t size
, const char *format
, ...)
140 va_start (args
, format
);
141 ret
= vsnprintf (str
, size
, format
, args
);
142 gdb_assert (ret
< size
);
148 /* See documentation in common-utils.h. */
151 string_printf (const char* fmt
, ...)
157 size
= vsnprintf (NULL
, 0, fmt
, vp
);
160 std::string
str (size
, '\0');
162 /* C++11 and later guarantee std::string uses contiguous memory and
163 always includes the terminating '\0'. */
165 vsprintf (&str
[0], fmt
, vp
);
171 /* See documentation in common-utils.h. */
174 string_vprintf (const char* fmt
, va_list args
)
180 size
= vsnprintf (NULL
, 0, fmt
, vp
);
183 std::string
str (size
, '\0');
185 /* C++11 and later guarantee std::string uses contiguous memory and
186 always includes the terminating '\0'. */
187 vsprintf (&str
[0], fmt
, args
);
193 /* See documentation in common-utils.h. */
196 string_appendf (std::string
&str
, const char *fmt
, ...)
201 string_vappendf (str
, fmt
, vp
);
206 /* See documentation in common-utils.h. */
209 string_vappendf (std::string
&str
, const char *fmt
, va_list args
)
215 grow_size
= vsnprintf (NULL
, 0, fmt
, vp
);
218 size_t curr_size
= str
.size ();
219 str
.resize (curr_size
+ grow_size
);
221 /* C++11 and later guarantee std::string uses contiguous memory and
222 always includes the terminating '\0'. */
223 vsprintf (&str
[curr_size
], fmt
, args
);
227 savestring (const char *ptr
, size_t len
)
229 char *p
= (char *) xmalloc (len
+ 1);
231 memcpy (p
, ptr
, len
);
236 /* The bit offset of the highest byte in a ULONGEST, for overflow
239 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
241 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
242 where 2 <= BASE <= 36. */
245 is_digit_in_base (unsigned char digit
, int base
)
247 if (!isalnum (digit
))
250 return (isdigit (digit
) && digit
< base
+ '0');
252 return (isdigit (digit
) || tolower (digit
) < base
- 10 + 'a');
256 digit_to_int (unsigned char c
)
261 return tolower (c
) - 'a' + 10;
264 /* As for strtoul, but for ULONGEST results. */
267 strtoulst (const char *num
, const char **trailer
, int base
)
269 unsigned int high_part
;
274 /* Skip leading whitespace. */
275 while (isspace (num
[i
]))
278 /* Handle prefixes. */
281 else if (num
[i
] == '-')
287 if (base
== 0 || base
== 16)
289 if (num
[i
] == '0' && (num
[i
+ 1] == 'x' || num
[i
+ 1] == 'X'))
297 if (base
== 0 && num
[i
] == '0')
303 if (base
< 2 || base
> 36)
309 result
= high_part
= 0;
310 for (; is_digit_in_base (num
[i
], base
); i
+= 1)
312 result
= result
* base
+ digit_to_int (num
[i
]);
313 high_part
= high_part
* base
+ (unsigned int) (result
>> HIGH_BYTE_POSN
);
314 result
&= ((ULONGEST
) 1 << HIGH_BYTE_POSN
) - 1;
315 if (high_part
> 0xff)
318 result
= ~ (ULONGEST
) 0;
328 result
= result
+ ((ULONGEST
) high_part
<< HIGH_BYTE_POSN
);
335 /* See documentation in common-utils.h. */
338 skip_spaces (char *chp
)
342 while (*chp
&& isspace (*chp
))
347 /* A const-correct version of the above. */
350 skip_spaces (const char *chp
)
354 while (*chp
&& isspace (*chp
))
359 /* See documentation in common-utils.h. */
362 skip_to_space (const char *chp
)
366 while (*chp
&& !isspace (*chp
))
371 /* See documentation in common-utils.h. */
374 skip_to_space (char *chp
)
376 return (char *) skip_to_space ((const char *) chp
);
379 /* See common/common-utils.h. */
382 free_vector_argv (std::vector
<char *> &v
)
390 /* See common/common-utils.h. */
393 stringify_argv (const std::vector
<char *> &args
)
397 if (!args
.empty () && args
[0] != NULL
)
406 /* Erase the last whitespace. */
407 ret
.erase (ret
.end () - 1);
413 /* See common/common-utils.h. */
416 is_regular_file (const char *name
, int *errno_ptr
)
419 const int status
= stat (name
, &st
);
421 /* Stat should never fail except when the file does not exist.
422 If stat fails, analyze the source of error and return true
423 unless the file does not exist, to avoid returning false results
424 on obscure systems where stat does not work as expected. */
434 if (S_ISREG (st
.st_mode
))
437 if (S_ISDIR (st
.st_mode
))
This page took 0.045396 seconds and 4 git commands to generate.