Commit | Line | Data |
---|---|---|
d26e3629 KY |
1 | /* Shared general utility routines for GDB, the GNU debugger. |
2 | ||
61baf725 | 3 | Copyright (C) 1986-2017 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 JK |
22 | #include "host-defs.h" |
23 | #include <ctype.h> | |
d26e3629 | 24 | |
d26e3629 KY |
25 | /* The xmalloc() (libiberty.h) family of memory management routines. |
26 | ||
27 | These are like the ISO-C malloc() family except that they implement | |
28 | consistent semantics and guard against typical memory management | |
29 | problems. */ | |
30 | ||
31 | /* NOTE: These are declared using PTR to ensure consistency with | |
32 | "libiberty.h". xfree() is GDB local. */ | |
33 | ||
34 | PTR /* ARI: PTR */ | |
35 | xmalloc (size_t size) | |
36 | { | |
37 | void *val; | |
38 | ||
39 | /* See libiberty/xmalloc.c. This function need's to match that's | |
40 | semantics. It never returns NULL. */ | |
41 | if (size == 0) | |
42 | size = 1; | |
43 | ||
44 | val = malloc (size); /* ARI: malloc */ | |
45 | if (val == NULL) | |
46 | malloc_failure (size); | |
47 | ||
48 | return val; | |
49 | } | |
50 | ||
51 | PTR /* ARI: PTR */ | |
52 | xrealloc (PTR ptr, size_t size) /* ARI: PTR */ | |
53 | { | |
54 | void *val; | |
55 | ||
56 | /* See libiberty/xmalloc.c. This function need's to match that's | |
57 | semantics. It never returns NULL. */ | |
58 | if (size == 0) | |
59 | size = 1; | |
60 | ||
61 | if (ptr != NULL) | |
62 | val = realloc (ptr, size); /* ARI: realloc */ | |
63 | else | |
64 | val = malloc (size); /* ARI: malloc */ | |
65 | if (val == NULL) | |
66 | malloc_failure (size); | |
67 | ||
68 | return val; | |
69 | } | |
70 | ||
71 | PTR /* ARI: PTR */ | |
72 | xcalloc (size_t number, size_t size) | |
73 | { | |
74 | void *mem; | |
75 | ||
76 | /* See libiberty/xmalloc.c. This function need's to match that's | |
77 | semantics. It never returns NULL. */ | |
78 | if (number == 0 || size == 0) | |
79 | { | |
80 | number = 1; | |
81 | size = 1; | |
82 | } | |
83 | ||
84 | mem = calloc (number, size); /* ARI: xcalloc */ | |
85 | if (mem == NULL) | |
86 | malloc_failure (number * size); | |
87 | ||
88 | return mem; | |
89 | } | |
90 | ||
91 | void * | |
92 | xzalloc (size_t size) | |
93 | { | |
94 | return xcalloc (1, size); | |
95 | } | |
96 | ||
97 | void | |
98 | xfree (void *ptr) | |
99 | { | |
100 | if (ptr != NULL) | |
101 | free (ptr); /* ARI: free */ | |
102 | } | |
103 | ||
51403f74 NC |
104 | void |
105 | xmalloc_failed (size_t size) | |
106 | { | |
107 | malloc_failure (size); | |
108 | } | |
109 | ||
d26e3629 KY |
110 | /* Like asprintf/vasprintf but get an internal_error if the call |
111 | fails. */ | |
112 | ||
113 | char * | |
114 | xstrprintf (const char *format, ...) | |
115 | { | |
116 | char *ret; | |
117 | va_list args; | |
118 | ||
119 | va_start (args, format); | |
120 | ret = xstrvprintf (format, args); | |
121 | va_end (args); | |
122 | return ret; | |
123 | } | |
124 | ||
125 | char * | |
126 | xstrvprintf (const char *format, va_list ap) | |
127 | { | |
128 | char *ret = NULL; | |
129 | int status = vasprintf (&ret, format, ap); | |
130 | ||
131 | /* NULL is returned when there was a memory allocation problem, or | |
132 | any other error (for instance, a bad format string). A negative | |
133 | status (the printed length) with a non-NULL buffer should never | |
134 | happen, but just to be sure. */ | |
135 | if (ret == NULL || status < 0) | |
136 | internal_error (__FILE__, __LINE__, _("vasprintf call failed")); | |
137 | return ret; | |
138 | } | |
139 | ||
d26e3629 KY |
140 | int |
141 | xsnprintf (char *str, size_t size, const char *format, ...) | |
142 | { | |
143 | va_list args; | |
144 | int ret; | |
145 | ||
146 | va_start (args, format); | |
147 | ret = vsnprintf (str, size, format, args); | |
148 | gdb_assert (ret < size); | |
149 | va_end (args); | |
150 | ||
151 | return ret; | |
152 | } | |
baea0dae | 153 | |
d4081a38 PA |
154 | /* See documentation in common-utils.h. */ |
155 | ||
156 | std::string | |
157 | string_printf (const char* fmt, ...) | |
158 | { | |
159 | va_list vp; | |
160 | int size; | |
161 | ||
162 | va_start (vp, fmt); | |
163 | size = vsnprintf (NULL, 0, fmt, vp); | |
164 | va_end (vp); | |
165 | ||
166 | std::string str (size, '\0'); | |
167 | ||
168 | /* C++11 and later guarantee std::string uses contiguous memory and | |
169 | always includes the terminating '\0'. */ | |
170 | va_start (vp, fmt); | |
171 | vsprintf (&str[0], fmt, vp); | |
172 | va_end (vp); | |
173 | ||
174 | return str; | |
175 | } | |
176 | ||
baea0dae PA |
177 | char * |
178 | savestring (const char *ptr, size_t len) | |
179 | { | |
180 | char *p = (char *) xmalloc (len + 1); | |
181 | ||
182 | memcpy (p, ptr, len); | |
183 | p[len] = 0; | |
184 | return p; | |
185 | } | |
03aef70f JK |
186 | |
187 | /* The bit offset of the highest byte in a ULONGEST, for overflow | |
188 | checking. */ | |
189 | ||
190 | #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT) | |
191 | ||
192 | /* True (non-zero) iff DIGIT is a valid digit in radix BASE, | |
193 | where 2 <= BASE <= 36. */ | |
194 | ||
195 | static int | |
196 | is_digit_in_base (unsigned char digit, int base) | |
197 | { | |
198 | if (!isalnum (digit)) | |
199 | return 0; | |
200 | if (base <= 10) | |
201 | return (isdigit (digit) && digit < base + '0'); | |
202 | else | |
203 | return (isdigit (digit) || tolower (digit) < base - 10 + 'a'); | |
204 | } | |
205 | ||
206 | static int | |
207 | digit_to_int (unsigned char c) | |
208 | { | |
209 | if (isdigit (c)) | |
210 | return c - '0'; | |
211 | else | |
212 | return tolower (c) - 'a' + 10; | |
213 | } | |
214 | ||
215 | /* As for strtoul, but for ULONGEST results. */ | |
216 | ||
217 | ULONGEST | |
218 | strtoulst (const char *num, const char **trailer, int base) | |
219 | { | |
220 | unsigned int high_part; | |
221 | ULONGEST result; | |
222 | int minus = 0; | |
223 | int i = 0; | |
224 | ||
225 | /* Skip leading whitespace. */ | |
226 | while (isspace (num[i])) | |
227 | i++; | |
228 | ||
229 | /* Handle prefixes. */ | |
230 | if (num[i] == '+') | |
231 | i++; | |
232 | else if (num[i] == '-') | |
233 | { | |
234 | minus = 1; | |
235 | i++; | |
236 | } | |
237 | ||
238 | if (base == 0 || base == 16) | |
239 | { | |
240 | if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X')) | |
241 | { | |
242 | i += 2; | |
243 | if (base == 0) | |
244 | base = 16; | |
245 | } | |
246 | } | |
247 | ||
248 | if (base == 0 && num[i] == '0') | |
249 | base = 8; | |
250 | ||
251 | if (base == 0) | |
252 | base = 10; | |
253 | ||
254 | if (base < 2 || base > 36) | |
255 | { | |
256 | errno = EINVAL; | |
257 | return 0; | |
258 | } | |
259 | ||
260 | result = high_part = 0; | |
261 | for (; is_digit_in_base (num[i], base); i += 1) | |
262 | { | |
263 | result = result * base + digit_to_int (num[i]); | |
264 | high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN); | |
265 | result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1; | |
266 | if (high_part > 0xff) | |
267 | { | |
268 | errno = ERANGE; | |
269 | result = ~ (ULONGEST) 0; | |
270 | high_part = 0; | |
271 | minus = 0; | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | if (trailer != NULL) | |
277 | *trailer = &num[i]; | |
278 | ||
279 | result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN); | |
280 | if (minus) | |
281 | return -result; | |
282 | else | |
283 | return result; | |
284 | } | |
285 | ||
41548caa | 286 | /* See documentation in common-utils.h. */ |
03aef70f JK |
287 | |
288 | char * | |
289 | skip_spaces (char *chp) | |
290 | { | |
291 | if (chp == NULL) | |
292 | return NULL; | |
293 | while (*chp && isspace (*chp)) | |
294 | chp++; | |
295 | return chp; | |
296 | } | |
297 | ||
298 | /* A const-correct version of the above. */ | |
299 | ||
300 | const char * | |
f1735a53 | 301 | skip_spaces (const char *chp) |
03aef70f JK |
302 | { |
303 | if (chp == NULL) | |
304 | return NULL; | |
305 | while (*chp && isspace (*chp)) | |
306 | chp++; | |
307 | return chp; | |
308 | } | |
309 | ||
41548caa | 310 | /* See documentation in common-utils.h. */ |
03aef70f JK |
311 | |
312 | const char * | |
f1735a53 | 313 | skip_to_space (const char *chp) |
03aef70f JK |
314 | { |
315 | if (chp == NULL) | |
316 | return NULL; | |
317 | while (*chp && !isspace (*chp)) | |
318 | chp++; | |
319 | return chp; | |
320 | } | |
7c5ded6a | 321 | |
f1735a53 TT |
322 | /* See documentation in common-utils.h. */ |
323 | ||
324 | char * | |
325 | skip_to_space (char *chp) | |
326 | { | |
327 | return (char *) skip_to_space ((const char *) chp); | |
328 | } | |
329 | ||
7c5ded6a SDJ |
330 | /* See common/common-utils.h. */ |
331 | ||
332 | void | |
333 | free_vector_argv (std::vector<char *> &v) | |
334 | { | |
335 | for (char *el : v) | |
336 | xfree (el); | |
337 | ||
338 | v.clear (); | |
339 | } | |
2090129c SDJ |
340 | |
341 | /* See common/common-utils.h. */ | |
342 | ||
343 | std::string | |
344 | stringify_argv (const std::vector<char *> &args) | |
345 | { | |
346 | std::string ret; | |
347 | ||
2f91880f | 348 | if (!args.empty () && args[0] != NULL) |
2090129c SDJ |
349 | { |
350 | for (auto s : args) | |
351 | if (s != NULL) | |
352 | { | |
353 | ret += s; | |
354 | ret += ' '; | |
355 | } | |
356 | ||
357 | /* Erase the last whitespace. */ | |
358 | ret.erase (ret.end () - 1); | |
359 | } | |
360 | ||
361 | return ret; | |
362 | } |