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.
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/>. */
31 /* Generally useful subroutines used throughout the program. */
33 static void malloc_failure (size_t size) ATTR_NORETURN;
36 malloc_failure (size_t size)
38 fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n",
39 (unsigned long) size);
43 /* Allocate memory without fail.
44 If malloc fails, this will print a message to stderr and exit. */
53 newmem = malloc (size);
55 malloc_failure (size);
60 /* Reallocate memory without fail. This works like xmalloc. */
63 xrealloc (void *ptr, size_t size)
71 val = realloc (ptr, size); /* OK: realloc */
73 val = malloc (size); /* OK: malloc */
75 malloc_failure (size);
80 /* Allocate memory without fail and set it to zero.
81 If malloc fails, this will print a message to stderr and exit. */
84 xcalloc (size_t nelem, size_t elsize)
88 if (nelem == 0 || elsize == 0)
91 newmem = calloc (nelem, elsize);
93 malloc_failure (nelem * elsize);
98 /* Copy a string into a memory buffer.
99 If malloc fails, this will print a message to stderr and exit. */
102 xstrdup (const char *s)
104 char *ret = strdup (s);
106 malloc_failure (strlen (s) + 1);
110 /* Free a standard argv vector. */
113 freeargv (char **vector)
119 for (scan = vector; *scan != NULL; scan++)
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. */
132 perror_with_name (const char *string)
137 err = strerror (errno);
139 err = "unknown error";
141 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
142 strcpy (combined, string);
143 strcat (combined, ": ");
144 strcat (combined, err);
146 error ("%s.", combined);
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. */
154 error (const char *string,...)
156 extern jmp_buf toplevel;
158 va_start (args, string);
160 vfprintf (stderr, string, args);
161 fprintf (stderr, "\n");
162 longjmp (toplevel, 1);
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. */
171 fatal (const char *string,...)
174 va_start (args, string);
175 fprintf (stderr, "gdbserver: ");
176 vfprintf (stderr, string, args);
177 fprintf (stderr, "\n");
184 warning (const char *string,...)
187 va_start (args, string);
188 fprintf (stderr, "gdbserver: ");
189 vfprintf (stderr, string, args);
190 fprintf (stderr, "\n");
194 /* Report a problem internal to GDBserver, and exit. */
197 internal_error (const char *file, int line, const char *fmt, ...)
200 va_start (args, fmt);
203 %s:%d: A problem internal to GDBserver has been detected.\n", file, line);
204 vfprintf (stderr, fmt, args);
205 fprintf (stderr, "\n");
210 /* Temporary storage using circular buffer. */
214 /* Return the next entry in the circular buffer. */
219 static char buf[NUMCELLS][CELLSIZE];
221 if (++cell >= NUMCELLS)
226 /* Stdarg wrapper around vsnprintf.
227 SIZE is the size of the buffer pointed to by STR. */
230 xsnprintf (char *str, size_t size, const char *format, ...)
235 va_start (args, format);
236 ret = vsnprintf (str, size, format, args);
242 /* Convert a CORE_ADDR into a HEX string, like %lx.
243 The result is stored in a circular static buffer, NUMCELLS deep. */
246 paddress (CORE_ADDR addr)
248 char *str = get_cell ();
249 xsnprintf (str, CELLSIZE, "%lx", (long) addr);
254 decimal2str (char *sign, ULONGEST addr, int width)
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 ();
264 temp[i] = addr % (1000 * 1000 * 1000);
265 addr /= (1000 * 1000 * 1000);
269 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
278 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
281 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
285 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
286 temp[2], temp[1], temp[0]);
289 internal_error (__FILE__, __LINE__,
290 "failed internal consistency check");
296 /* %u for ULONGEST. The result is stored in a circular static buffer,
300 pulongest (ULONGEST u)
302 return decimal2str ("", u, 0);
305 /* %d for LONGEST. The result is stored in a circular static buffer,
312 return decimal2str ("-", -l, 0);
314 return decimal2str ("", l, 0);
317 /* Eliminate warning from compiler on 32-bit systems. */
318 static int thirty_two = 32;
320 /* Convert a ULONGEST into a HEX string, like %lx. The result is
321 stored in a circular static buffer, NUMCELLS deep. */
324 phex_nz (ULONGEST l, int sizeof_l)
332 unsigned long high = (unsigned long) (l >> thirty_two);
335 xsnprintf (str, CELLSIZE, "%lx",
336 (unsigned long) (l & 0xffffffff));
338 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
339 (unsigned long) (l & 0xffffffff));
344 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
348 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
351 str = phex_nz (l, sizeof (l));