Commit | Line | Data |
---|---|---|
d26e3629 KY |
1 | /* Shared general utility routines for GDB, the GNU debugger. |
2 | ||
e2882c85 | 3 | Copyright (C) 1986-2018 Free Software Foundation, Inc. |
d26e3629 KY |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
727605ca | 20 | #include "common-defs.h" |
2090129c | 21 | #include "common-utils.h" |
03aef70f | 22 | #include "host-defs.h" |
25e3c82c | 23 | #include <sys/stat.h> |
03aef70f | 24 | #include <ctype.h> |
d26e3629 | 25 | |
d26e3629 KY |
26 | /* The xmalloc() (libiberty.h) family of memory management routines. |
27 | ||
28 | These are like the ISO-C malloc() family except that they implement | |
29 | consistent semantics and guard against typical memory management | |
30 | problems. */ | |
31 | ||
32 | /* NOTE: These are declared using PTR to ensure consistency with | |
33 | "libiberty.h". xfree() is GDB local. */ | |
34 | ||
35 | PTR /* ARI: PTR */ | |
36 | xmalloc (size_t size) | |
37 | { | |
38 | void *val; | |
39 | ||
40 | /* See libiberty/xmalloc.c. This function need's to match that's | |
41 | semantics. It never returns NULL. */ | |
42 | if (size == 0) | |
43 | size = 1; | |
44 | ||
45 | val = malloc (size); /* ARI: malloc */ | |
46 | if (val == NULL) | |
47 | malloc_failure (size); | |
48 | ||
49 | return val; | |
50 | } | |
51 | ||
52 | PTR /* ARI: PTR */ | |
53 | xrealloc (PTR ptr, size_t size) /* ARI: PTR */ | |
54 | { | |
55 | void *val; | |
56 | ||
57 | /* See libiberty/xmalloc.c. This function need's to match that's | |
58 | semantics. It never returns NULL. */ | |
59 | if (size == 0) | |
60 | size = 1; | |
61 | ||
62 | if (ptr != NULL) | |
63 | val = realloc (ptr, size); /* ARI: realloc */ | |
64 | else | |
65 | val = malloc (size); /* ARI: malloc */ | |
66 | if (val == NULL) | |
67 | malloc_failure (size); | |
68 | ||
69 | return val; | |
70 | } | |
71 | ||
72 | PTR /* ARI: PTR */ | |
73 | xcalloc (size_t number, size_t size) | |
74 | { | |
75 | void *mem; | |
76 | ||
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) | |
80 | { | |
81 | number = 1; | |
82 | size = 1; | |
83 | } | |
84 | ||
85 | mem = calloc (number, size); /* ARI: xcalloc */ | |
86 | if (mem == NULL) | |
87 | malloc_failure (number * size); | |
88 | ||
89 | return mem; | |
90 | } | |
91 | ||
92 | void * | |
93 | xzalloc (size_t size) | |
94 | { | |
95 | return xcalloc (1, size); | |
96 | } | |
97 | ||
51403f74 NC |
98 | void |
99 | xmalloc_failed (size_t size) | |
100 | { | |
101 | malloc_failure (size); | |
102 | } | |
103 | ||
d26e3629 KY |
104 | /* Like asprintf/vasprintf but get an internal_error if the call |
105 | fails. */ | |
106 | ||
107 | char * | |
108 | xstrprintf (const char *format, ...) | |
109 | { | |
110 | char *ret; | |
111 | va_list args; | |
112 | ||
113 | va_start (args, format); | |
114 | ret = xstrvprintf (format, args); | |
115 | va_end (args); | |
116 | return ret; | |
117 | } | |
118 | ||
119 | char * | |
120 | xstrvprintf (const char *format, va_list ap) | |
121 | { | |
122 | char *ret = NULL; | |
123 | int status = vasprintf (&ret, format, ap); | |
124 | ||
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")); | |
131 | return ret; | |
132 | } | |
133 | ||
d26e3629 KY |
134 | int |
135 | xsnprintf (char *str, size_t size, const char *format, ...) | |
136 | { | |
137 | va_list args; | |
138 | int ret; | |
139 | ||
140 | va_start (args, format); | |
141 | ret = vsnprintf (str, size, format, args); | |
142 | gdb_assert (ret < size); | |
143 | va_end (args); | |
144 | ||
145 | return ret; | |
146 | } | |
baea0dae | 147 | |
d4081a38 PA |
148 | /* See documentation in common-utils.h. */ |
149 | ||
150 | std::string | |
151 | string_printf (const char* fmt, ...) | |
152 | { | |
153 | va_list vp; | |
154 | int size; | |
155 | ||
156 | va_start (vp, fmt); | |
157 | size = vsnprintf (NULL, 0, fmt, vp); | |
158 | va_end (vp); | |
159 | ||
160 | std::string str (size, '\0'); | |
161 | ||
162 | /* C++11 and later guarantee std::string uses contiguous memory and | |
163 | always includes the terminating '\0'. */ | |
164 | va_start (vp, fmt); | |
165 | vsprintf (&str[0], fmt, vp); | |
166 | va_end (vp); | |
167 | ||
168 | return str; | |
169 | } | |
170 | ||
bd413795 TT |
171 | /* See documentation in common-utils.h. */ |
172 | ||
173 | std::string | |
174 | string_vprintf (const char* fmt, va_list args) | |
175 | { | |
176 | va_list vp; | |
177 | size_t size; | |
178 | ||
179 | va_copy (vp, args); | |
180 | size = vsnprintf (NULL, 0, fmt, vp); | |
181 | va_end (vp); | |
182 | ||
183 | std::string str (size, '\0'); | |
184 | ||
185 | /* C++11 and later guarantee std::string uses contiguous memory and | |
186 | always includes the terminating '\0'. */ | |
187 | vsprintf (&str[0], fmt, args); | |
188 | ||
189 | return str; | |
190 | } | |
191 | ||
31b833b3 PA |
192 | |
193 | /* See documentation in common-utils.h. */ | |
194 | ||
195 | void | |
196 | string_appendf (std::string &str, const char *fmt, ...) | |
197 | { | |
198 | va_list vp; | |
199 | ||
200 | va_start (vp, fmt); | |
201 | string_vappendf (str, fmt, vp); | |
202 | va_end (vp); | |
203 | } | |
204 | ||
205 | ||
206 | /* See documentation in common-utils.h. */ | |
207 | ||
208 | void | |
209 | string_vappendf (std::string &str, const char *fmt, va_list args) | |
210 | { | |
211 | va_list vp; | |
212 | int grow_size; | |
213 | ||
214 | va_copy (vp, args); | |
215 | grow_size = vsnprintf (NULL, 0, fmt, vp); | |
216 | va_end (vp); | |
217 | ||
218 | size_t curr_size = str.size (); | |
219 | str.resize (curr_size + grow_size); | |
220 | ||
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); | |
224 | } | |
225 | ||
baea0dae PA |
226 | char * |
227 | savestring (const char *ptr, size_t len) | |
228 | { | |
229 | char *p = (char *) xmalloc (len + 1); | |
230 | ||
231 | memcpy (p, ptr, len); | |
232 | p[len] = 0; | |
233 | return p; | |
234 | } | |
03aef70f JK |
235 | |
236 | /* The bit offset of the highest byte in a ULONGEST, for overflow | |
237 | checking. */ | |
238 | ||
239 | #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT) | |
240 | ||
241 | /* True (non-zero) iff DIGIT is a valid digit in radix BASE, | |
242 | where 2 <= BASE <= 36. */ | |
243 | ||
244 | static int | |
245 | is_digit_in_base (unsigned char digit, int base) | |
246 | { | |
247 | if (!isalnum (digit)) | |
248 | return 0; | |
249 | if (base <= 10) | |
250 | return (isdigit (digit) && digit < base + '0'); | |
251 | else | |
252 | return (isdigit (digit) || tolower (digit) < base - 10 + 'a'); | |
253 | } | |
254 | ||
255 | static int | |
256 | digit_to_int (unsigned char c) | |
257 | { | |
258 | if (isdigit (c)) | |
259 | return c - '0'; | |
260 | else | |
261 | return tolower (c) - 'a' + 10; | |
262 | } | |
263 | ||
264 | /* As for strtoul, but for ULONGEST results. */ | |
265 | ||
266 | ULONGEST | |
267 | strtoulst (const char *num, const char **trailer, int base) | |
268 | { | |
269 | unsigned int high_part; | |
270 | ULONGEST result; | |
271 | int minus = 0; | |
272 | int i = 0; | |
273 | ||
274 | /* Skip leading whitespace. */ | |
275 | while (isspace (num[i])) | |
276 | i++; | |
277 | ||
278 | /* Handle prefixes. */ | |
279 | if (num[i] == '+') | |
280 | i++; | |
281 | else if (num[i] == '-') | |
282 | { | |
283 | minus = 1; | |
284 | i++; | |
285 | } | |
286 | ||
287 | if (base == 0 || base == 16) | |
288 | { | |
289 | if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X')) | |
290 | { | |
291 | i += 2; | |
292 | if (base == 0) | |
293 | base = 16; | |
294 | } | |
295 | } | |
296 | ||
297 | if (base == 0 && num[i] == '0') | |
298 | base = 8; | |
299 | ||
300 | if (base == 0) | |
301 | base = 10; | |
302 | ||
303 | if (base < 2 || base > 36) | |
304 | { | |
305 | errno = EINVAL; | |
306 | return 0; | |
307 | } | |
308 | ||
309 | result = high_part = 0; | |
310 | for (; is_digit_in_base (num[i], base); i += 1) | |
311 | { | |
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) | |
316 | { | |
317 | errno = ERANGE; | |
318 | result = ~ (ULONGEST) 0; | |
319 | high_part = 0; | |
320 | minus = 0; | |
321 | break; | |
322 | } | |
323 | } | |
324 | ||
325 | if (trailer != NULL) | |
326 | *trailer = &num[i]; | |
327 | ||
328 | result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN); | |
329 | if (minus) | |
330 | return -result; | |
331 | else | |
332 | return result; | |
333 | } | |
334 | ||
41548caa | 335 | /* See documentation in common-utils.h. */ |
03aef70f JK |
336 | |
337 | char * | |
338 | skip_spaces (char *chp) | |
339 | { | |
340 | if (chp == NULL) | |
341 | return NULL; | |
342 | while (*chp && isspace (*chp)) | |
343 | chp++; | |
344 | return chp; | |
345 | } | |
346 | ||
347 | /* A const-correct version of the above. */ | |
348 | ||
349 | const char * | |
f1735a53 | 350 | skip_spaces (const char *chp) |
03aef70f JK |
351 | { |
352 | if (chp == NULL) | |
353 | return NULL; | |
354 | while (*chp && isspace (*chp)) | |
355 | chp++; | |
356 | return chp; | |
357 | } | |
358 | ||
41548caa | 359 | /* See documentation in common-utils.h. */ |
03aef70f JK |
360 | |
361 | const char * | |
f1735a53 | 362 | skip_to_space (const char *chp) |
03aef70f JK |
363 | { |
364 | if (chp == NULL) | |
365 | return NULL; | |
366 | while (*chp && !isspace (*chp)) | |
367 | chp++; | |
368 | return chp; | |
369 | } | |
7c5ded6a | 370 | |
f1735a53 TT |
371 | /* See documentation in common-utils.h. */ |
372 | ||
373 | char * | |
374 | skip_to_space (char *chp) | |
375 | { | |
376 | return (char *) skip_to_space ((const char *) chp); | |
377 | } | |
378 | ||
7c5ded6a SDJ |
379 | /* See common/common-utils.h. */ |
380 | ||
381 | void | |
382 | free_vector_argv (std::vector<char *> &v) | |
383 | { | |
384 | for (char *el : v) | |
385 | xfree (el); | |
386 | ||
387 | v.clear (); | |
388 | } | |
2090129c SDJ |
389 | |
390 | /* See common/common-utils.h. */ | |
391 | ||
392 | std::string | |
393 | stringify_argv (const std::vector<char *> &args) | |
394 | { | |
395 | std::string ret; | |
396 | ||
2f91880f | 397 | if (!args.empty () && args[0] != NULL) |
2090129c SDJ |
398 | { |
399 | for (auto s : args) | |
400 | if (s != NULL) | |
401 | { | |
402 | ret += s; | |
403 | ret += ' '; | |
404 | } | |
405 | ||
406 | /* Erase the last whitespace. */ | |
407 | ret.erase (ret.end () - 1); | |
408 | } | |
409 | ||
410 | return ret; | |
411 | } | |
25e3c82c SDJ |
412 | |
413 | /* See common/common-utils.h. */ | |
414 | ||
415 | bool | |
416 | is_regular_file (const char *name, int *errno_ptr) | |
417 | { | |
418 | struct stat st; | |
419 | const int status = stat (name, &st); | |
420 | ||
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. */ | |
425 | ||
426 | if (status != 0) | |
427 | { | |
428 | if (errno != ENOENT) | |
429 | return true; | |
430 | *errno_ptr = ENOENT; | |
431 | return false; | |
432 | } | |
433 | ||
434 | if (S_ISREG (st.st_mode)) | |
435 | return true; | |
436 | ||
437 | if (S_ISDIR (st.st_mode)) | |
438 | *errno_ptr = EISDIR; | |
439 | else | |
440 | *errno_ptr = EINVAL; | |
441 | return false; | |
442 | } | |
a3b60e45 JK |
443 | |
444 | /* See common/common-utils.h. */ | |
445 | ||
446 | ULONGEST | |
447 | align_up (ULONGEST v, int n) | |
448 | { | |
449 | /* Check that N is really a power of two. */ | |
450 | gdb_assert (n && (n & (n-1)) == 0); | |
451 | return (v + n - 1) & -n; | |
452 | } | |
453 | ||
454 | /* See common/common-utils.h. */ | |
455 | ||
456 | ULONGEST | |
457 | align_down (ULONGEST v, int n) | |
458 | { | |
459 | /* Check that N is really a power of two. */ | |
460 | gdb_assert (n && (n & (n-1)) == 0); | |
461 | return (v & -n); | |
462 | } |