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