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 #ifdef IN_PROCESS_AGENT
32 # define PREFIX "ipa: "
33 # define TOOLNAME "GDBserver in-process agent"
35 # define PREFIX "gdbserver: "
36 # define TOOLNAME "GDBserver"
39 /* Generally useful subroutines used throughout the program. */
41 static void malloc_failure (size_t size) ATTR_NORETURN;
44 malloc_failure (size_t size)
46 fprintf (stderr, PREFIX "ran out of memory while trying to allocate %lu bytes\n",
47 (unsigned long) size);
51 /* Allocate memory without fail.
52 If malloc fails, this will print a message to stderr and exit. */
61 newmem = malloc (size);
63 malloc_failure (size);
68 /* Reallocate memory without fail. This works like xmalloc. */
71 xrealloc (void *ptr, size_t size)
79 val = realloc (ptr, size); /* OK: realloc */
81 val = malloc (size); /* OK: malloc */
83 malloc_failure (size);
88 /* Allocate memory without fail and set it to zero.
89 If malloc fails, this will print a message to stderr and exit. */
92 xcalloc (size_t nelem, size_t elsize)
96 if (nelem == 0 || elsize == 0)
99 newmem = calloc (nelem, elsize);
101 malloc_failure (nelem * elsize);
106 /* Copy a string into a memory buffer.
107 If malloc fails, this will print a message to stderr and exit. */
110 xstrdup (const char *s)
112 char *ret = strdup (s);
114 malloc_failure (strlen (s) + 1);
118 #ifndef IN_PROCESS_AGENT
120 /* Free a standard argv vector. */
123 freeargv (char **vector)
129 for (scan = vector; *scan != NULL; scan++)
139 /* Print the system error message for errno, and also mention STRING
140 as the file name for which the error was encountered.
141 Then return to command level. */
144 perror_with_name (const char *string)
149 err = strerror (errno);
151 err = "unknown error";
153 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
154 strcpy (combined, string);
155 strcat (combined, ": ");
156 strcat (combined, err);
158 error ("%s.", combined);
161 /* Print an error message and return to command level.
162 STRING is the error message, used as a fprintf string,
163 and ARG is passed as an argument to it. */
166 error (const char *string,...)
168 #ifndef IN_PROCESS_AGENT
169 extern jmp_buf toplevel;
172 va_start (args, string);
174 vfprintf (stderr, string, args);
175 fprintf (stderr, "\n");
176 #ifndef IN_PROCESS_AGENT
177 longjmp (toplevel, 1);
183 /* Print an error message and exit reporting failure.
184 This is for a error that we cannot continue from.
185 STRING and ARG are passed to fprintf. */
189 fatal (const char *string,...)
192 va_start (args, string);
193 fprintf (stderr, PREFIX);
194 vfprintf (stderr, string, args);
195 fprintf (stderr, "\n");
202 warning (const char *string,...)
205 va_start (args, string);
206 fprintf (stderr, PREFIX);
207 vfprintf (stderr, string, args);
208 fprintf (stderr, "\n");
212 /* Report a problem internal to GDBserver, and exit. */
215 internal_error (const char *file, int line, const char *fmt, ...)
218 va_start (args, fmt);
221 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
222 vfprintf (stderr, fmt, args);
223 fprintf (stderr, "\n");
228 /* Temporary storage using circular buffer. */
232 /* Return the next entry in the circular buffer. */
237 static char buf[NUMCELLS][CELLSIZE];
239 if (++cell >= NUMCELLS)
244 /* Stdarg wrapper around vsnprintf.
245 SIZE is the size of the buffer pointed to by STR. */
248 xsnprintf (char *str, size_t size, const char *format, ...)
253 va_start (args, format);
254 ret = vsnprintf (str, size, format, args);
260 /* Convert a CORE_ADDR into a HEX string, like %lx.
261 The result is stored in a circular static buffer, NUMCELLS deep. */
264 paddress (CORE_ADDR addr)
266 char *str = get_cell ();
267 xsnprintf (str, CELLSIZE, "%lx", (long) addr);
272 decimal2str (char *sign, ULONGEST addr, int width)
274 /* Steal code from valprint.c:print_decimal(). Should this worry
275 about the real size of addr as the above does? */
276 unsigned long temp[3];
277 char *str = get_cell ();
282 temp[i] = addr % (1000 * 1000 * 1000);
283 addr /= (1000 * 1000 * 1000);
287 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
296 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
299 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
303 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
304 temp[2], temp[1], temp[0]);
307 internal_error (__FILE__, __LINE__,
308 "failed internal consistency check");
314 /* %u for ULONGEST. The result is stored in a circular static buffer,
318 pulongest (ULONGEST u)
320 return decimal2str ("", u, 0);
323 /* %d for LONGEST. The result is stored in a circular static buffer,
330 return decimal2str ("-", -l, 0);
332 return decimal2str ("", l, 0);
335 /* Eliminate warning from compiler on 32-bit systems. */
336 static int thirty_two = 32;
338 /* Convert a ULONGEST into a HEX string, like %lx. The result is
339 stored in a circular static buffer, NUMCELLS deep. */
342 phex_nz (ULONGEST l, int sizeof_l)
350 unsigned long high = (unsigned long) (l >> thirty_two);
353 xsnprintf (str, CELLSIZE, "%lx",
354 (unsigned long) (l & 0xffffffff));
356 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
357 (unsigned long) (l & 0xffffffff));
362 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
366 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
369 str = phex_nz (l, sizeof (l));