Commit | Line | Data |
---|---|---|
c906108c | 1 | /* General utility routines for the remote server for GDB. |
6aba47ca | 2 | Copyright (C) 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
4c38e0a4 | 3 | 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "server.h" | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
68070c10 PA |
23 | #include <stdlib.h> |
24 | #if HAVE_ERRNO_H | |
25 | #include <errno.h> | |
26 | #endif | |
27 | #if HAVE_MALLOC_H | |
28 | #include <malloc.h> | |
29 | #endif | |
c906108c SS |
30 | |
31 | /* Generally useful subroutines used throughout the program. */ | |
32 | ||
bca929d3 DE |
33 | static void malloc_failure (size_t size) ATTR_NORETURN; |
34 | ||
35 | static void | |
36 | malloc_failure (size_t size) | |
37 | { | |
38 | fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n", | |
39 | (unsigned long) size); | |
40 | exit (1); | |
41 | } | |
42 | ||
43 | /* Allocate memory without fail. | |
44 | If malloc fails, this will print a message to stderr and exit. */ | |
45 | ||
46 | void * | |
47 | xmalloc (size_t size) | |
48 | { | |
49 | void *newmem; | |
50 | ||
51 | if (size == 0) | |
52 | size = 1; | |
53 | newmem = malloc (size); | |
54 | if (!newmem) | |
55 | malloc_failure (size); | |
56 | ||
57 | return newmem; | |
58 | } | |
59 | ||
219f2f23 PA |
60 | /* Reallocate memory without fail. This works like xmalloc. */ |
61 | ||
62 | void * | |
63 | xrealloc (void *ptr, size_t size) | |
64 | { | |
65 | void *val; | |
66 | ||
67 | if (size == 0) | |
68 | size = 1; | |
69 | ||
70 | if (ptr != NULL) | |
71 | val = realloc (ptr, size); /* OK: realloc */ | |
72 | else | |
73 | val = malloc (size); /* OK: malloc */ | |
74 | if (val == NULL) | |
75 | malloc_failure (size); | |
76 | ||
77 | return val; | |
78 | } | |
79 | ||
bca929d3 DE |
80 | /* Allocate memory without fail and set it to zero. |
81 | If malloc fails, this will print a message to stderr and exit. */ | |
82 | ||
83 | void * | |
84 | xcalloc (size_t nelem, size_t elsize) | |
85 | { | |
86 | void *newmem; | |
87 | ||
88 | if (nelem == 0 || elsize == 0) | |
89 | nelem = elsize = 1; | |
90 | ||
91 | newmem = calloc (nelem, elsize); | |
92 | if (!newmem) | |
93 | malloc_failure (nelem * elsize); | |
94 | ||
95 | return newmem; | |
96 | } | |
97 | ||
98 | /* Copy a string into a memory buffer. | |
99 | If malloc fails, this will print a message to stderr and exit. */ | |
100 | ||
101 | char * | |
102 | xstrdup (const char *s) | |
103 | { | |
104 | char *ret = strdup (s); | |
105 | if (ret == NULL) | |
106 | malloc_failure (strlen (s) + 1); | |
107 | return ret; | |
108 | } | |
109 | ||
aef93bd7 DE |
110 | /* Free a standard argv vector. */ |
111 | ||
112 | void | |
113 | freeargv (char **vector) | |
114 | { | |
115 | char **scan; | |
116 | ||
117 | if (vector != NULL) | |
118 | { | |
119 | for (scan = vector; *scan != NULL; scan++) | |
120 | { | |
121 | free (*scan); | |
122 | } | |
123 | free (vector); | |
124 | } | |
125 | } | |
126 | ||
c906108c SS |
127 | /* Print the system error message for errno, and also mention STRING |
128 | as the file name for which the error was encountered. | |
129 | Then return to command level. */ | |
130 | ||
131 | void | |
54363045 | 132 | perror_with_name (const char *string) |
c906108c | 133 | { |
5c44784c | 134 | const char *err; |
c906108c SS |
135 | char *combined; |
136 | ||
43d5792c DJ |
137 | err = strerror (errno); |
138 | if (err == NULL) | |
c906108c SS |
139 | err = "unknown error"; |
140 | ||
141 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
142 | strcpy (combined, string); | |
143 | strcat (combined, ": "); | |
144 | strcat (combined, err); | |
145 | ||
146 | error ("%s.", combined); | |
147 | } | |
148 | ||
149 | /* Print an error message and return to command level. | |
150 | STRING is the error message, used as a fprintf string, | |
151 | and ARG is passed as an argument to it. */ | |
152 | ||
0729219d | 153 | void |
c5aa993b | 154 | error (const char *string,...) |
c906108c SS |
155 | { |
156 | extern jmp_buf toplevel; | |
157 | va_list args; | |
c906108c | 158 | va_start (args, string); |
c906108c | 159 | fflush (stdout); |
c906108c | 160 | vfprintf (stderr, string, args); |
c906108c | 161 | fprintf (stderr, "\n"); |
c5aa993b | 162 | longjmp (toplevel, 1); |
c906108c SS |
163 | } |
164 | ||
165 | /* Print an error message and exit reporting failure. | |
166 | This is for a error that we cannot continue from. | |
167 | STRING and ARG are passed to fprintf. */ | |
168 | ||
169 | /* VARARGS */ | |
0729219d | 170 | void |
0a30fbc4 | 171 | fatal (const char *string,...) |
c906108c SS |
172 | { |
173 | va_list args; | |
c906108c | 174 | va_start (args, string); |
24a09b5f | 175 | fprintf (stderr, "gdbserver: "); |
c906108c SS |
176 | vfprintf (stderr, string, args); |
177 | fprintf (stderr, "\n"); | |
178 | va_end (args); | |
179 | exit (1); | |
180 | } | |
0a30fbc4 DJ |
181 | |
182 | /* VARARGS */ | |
183 | void | |
184 | warning (const char *string,...) | |
185 | { | |
186 | va_list args; | |
187 | va_start (args, string); | |
24a09b5f | 188 | fprintf (stderr, "gdbserver: "); |
0a30fbc4 DJ |
189 | vfprintf (stderr, string, args); |
190 | fprintf (stderr, "\n"); | |
191 | va_end (args); | |
192 | } | |
aa5ca48f | 193 | |
e92d13d5 PA |
194 | /* Report a problem internal to GDBserver, and exit. */ |
195 | ||
196 | void | |
197 | internal_error (const char *file, int line, const char *fmt, ...) | |
198 | { | |
199 | va_list args; | |
200 | va_start (args, fmt); | |
201 | ||
202 | fprintf (stderr, "\ | |
203 | %s:%d: A problem internal to GDBserver has been detected.\n", file, line); | |
204 | vfprintf (stderr, fmt, args); | |
205 | fprintf (stderr, "\n"); | |
206 | va_end (args); | |
207 | exit (1); | |
208 | } | |
209 | ||
aa5ca48f DE |
210 | /* Temporary storage using circular buffer. */ |
211 | #define NUMCELLS 4 | |
212 | #define CELLSIZE 50 | |
213 | ||
214 | /* Return the next entry in the circular buffer. */ | |
215 | ||
216 | static char * | |
217 | get_cell (void) | |
218 | { | |
219 | static char buf[NUMCELLS][CELLSIZE]; | |
220 | static int cell = 0; | |
221 | if (++cell >= NUMCELLS) | |
222 | cell = 0; | |
223 | return buf[cell]; | |
224 | } | |
225 | ||
226 | /* Stdarg wrapper around vsnprintf. | |
227 | SIZE is the size of the buffer pointed to by STR. */ | |
228 | ||
229 | static int | |
230 | xsnprintf (char *str, size_t size, const char *format, ...) | |
231 | { | |
232 | va_list args; | |
233 | int ret; | |
234 | ||
235 | va_start (args, format); | |
236 | ret = vsnprintf (str, size, format, args); | |
237 | va_end (args); | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
242 | /* Convert a CORE_ADDR into a HEX string, like %lx. | |
243 | The result is stored in a circular static buffer, NUMCELLS deep. */ | |
244 | ||
245 | char * | |
246 | paddress (CORE_ADDR addr) | |
247 | { | |
248 | char *str = get_cell (); | |
249 | xsnprintf (str, CELLSIZE, "%lx", (long) addr); | |
250 | return str; | |
251 | } | |
219f2f23 PA |
252 | |
253 | static char * | |
254 | decimal2str (char *sign, ULONGEST addr, int width) | |
255 | { | |
256 | /* Steal code from valprint.c:print_decimal(). Should this worry | |
257 | about the real size of addr as the above does? */ | |
258 | unsigned long temp[3]; | |
259 | char *str = get_cell (); | |
260 | ||
261 | int i = 0; | |
262 | do | |
263 | { | |
264 | temp[i] = addr % (1000 * 1000 * 1000); | |
265 | addr /= (1000 * 1000 * 1000); | |
266 | i++; | |
267 | width -= 9; | |
268 | } | |
269 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
270 | ||
271 | width = 9; | |
272 | if (width < 0) | |
273 | width = 0; | |
274 | ||
275 | switch (i) | |
276 | { | |
277 | case 1: | |
278 | xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]); | |
279 | break; | |
280 | case 2: | |
281 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width, | |
282 | temp[1], temp[0]); | |
283 | break; | |
284 | case 3: | |
285 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width, | |
286 | temp[2], temp[1], temp[0]); | |
287 | break; | |
288 | default: | |
289 | internal_error (__FILE__, __LINE__, | |
290 | "failed internal consistency check"); | |
291 | } | |
292 | ||
293 | return str; | |
294 | } | |
295 | ||
296 | /* %u for ULONGEST. The result is stored in a circular static buffer, | |
297 | NUMCELLS deep. */ | |
298 | ||
299 | char * | |
300 | pulongest (ULONGEST u) | |
301 | { | |
302 | return decimal2str ("", u, 0); | |
303 | } | |
304 | ||
305 | /* %d for LONGEST. The result is stored in a circular static buffer, | |
306 | NUMCELLS deep. */ | |
307 | ||
308 | char * | |
309 | plongest (LONGEST l) | |
310 | { | |
311 | if (l < 0) | |
312 | return decimal2str ("-", -l, 0); | |
313 | else | |
314 | return decimal2str ("", l, 0); | |
315 | } | |
316 | ||
317 | /* Eliminate warning from compiler on 32-bit systems. */ | |
318 | static int thirty_two = 32; | |
319 | ||
320 | /* Convert a ULONGEST into a HEX string, like %lx. The result is | |
321 | stored in a circular static buffer, NUMCELLS deep. */ | |
322 | ||
323 | char * | |
324 | phex_nz (ULONGEST l, int sizeof_l) | |
325 | { | |
326 | char *str; | |
327 | ||
328 | switch (sizeof_l) | |
329 | { | |
330 | case 8: | |
331 | { | |
332 | unsigned long high = (unsigned long) (l >> thirty_two); | |
333 | str = get_cell (); | |
334 | if (high == 0) | |
335 | xsnprintf (str, CELLSIZE, "%lx", | |
336 | (unsigned long) (l & 0xffffffff)); | |
337 | else | |
338 | xsnprintf (str, CELLSIZE, "%lx%08lx", high, | |
339 | (unsigned long) (l & 0xffffffff)); | |
340 | break; | |
341 | } | |
342 | case 4: | |
343 | str = get_cell (); | |
344 | xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l); | |
345 | break; | |
346 | case 2: | |
347 | str = get_cell (); | |
348 | xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff)); | |
349 | break; | |
350 | default: | |
351 | str = phex_nz (l, sizeof (l)); | |
352 | break; | |
353 | } | |
354 | ||
355 | return str; | |
356 | } |