1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #ifdef IN_PROCESS_AGENT
28 # define PREFIX "ipa: "
29 # define TOOLNAME "GDBserver in-process agent"
31 # define PREFIX "gdbserver: "
32 # define TOOLNAME "GDBserver"
35 /* Generally useful subroutines used throughout the program. */
38 malloc_failure (long size)
41 PREFIX "ran out of memory while trying to allocate %lu bytes\n",
42 (unsigned long) size);
46 /* Copy a string into a memory buffer.
47 If malloc fails, this will print a message to stderr and exit. */
50 xstrdup (const char *s)
52 char *ret = strdup (s);
54 malloc_failure (strlen (s) + 1);
58 #ifndef IN_PROCESS_AGENT
60 /* Free a standard argv vector. */
63 freeargv (char **vector)
69 for (scan = vector; *scan != NULL; scan++)
79 /* Print the system error message for errno, and also mention STRING
80 as the file name for which the error was encountered.
81 Then return to command level. */
84 perror_with_name (const char *string)
89 err = strerror (errno);
91 err = "unknown error";
93 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
94 strcpy (combined, string);
95 strcat (combined, ": ");
96 strcat (combined, err);
98 error ("%s.", combined);
101 /* Print an error message and return to command level.
102 STRING is the error message, used as a fprintf string,
103 and ARG is passed as an argument to it. */
106 error (const char *string,...)
108 #ifndef IN_PROCESS_AGENT
109 extern jmp_buf toplevel;
112 va_start (args, string);
114 vfprintf (stderr, string, args);
115 fprintf (stderr, "\n");
116 #ifndef IN_PROCESS_AGENT
117 longjmp (toplevel, 1);
123 /* Print an error message and exit reporting failure.
124 This is for a error that we cannot continue from.
125 STRING and ARG are passed to fprintf. */
129 fatal (const char *string,...)
132 va_start (args, string);
133 fprintf (stderr, PREFIX);
134 vfprintf (stderr, string, args);
135 fprintf (stderr, "\n");
142 warning (const char *string,...)
145 va_start (args, string);
146 fprintf (stderr, PREFIX);
147 vfprintf (stderr, string, args);
148 fprintf (stderr, "\n");
152 /* Report a problem internal to GDBserver, and exit. */
155 internal_error (const char *file, int line, const char *fmt, ...)
158 va_start (args, fmt);
161 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
162 vfprintf (stderr, fmt, args);
163 fprintf (stderr, "\n");
168 /* Temporary storage using circular buffer. */
172 /* Return the next entry in the circular buffer. */
177 static char buf[NUMCELLS][CELLSIZE];
179 if (++cell >= NUMCELLS)
185 decimal2str (char *sign, ULONGEST addr)
187 /* Steal code from valprint.c:print_decimal(). Should this worry
188 about the real size of addr as the above does? */
189 unsigned long temp[3];
190 char *str = get_cell ();
196 temp[i] = addr % (1000 * 1000 * 1000);
197 addr /= (1000 * 1000 * 1000);
200 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
205 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
208 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
212 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
213 temp[2], temp[1], temp[0]);
216 internal_error (__FILE__, __LINE__,
217 "failed internal consistency check");
223 /* %u for ULONGEST. The result is stored in a circular static buffer,
227 pulongest (ULONGEST u)
229 return decimal2str ("", u);
232 /* %d for LONGEST. The result is stored in a circular static buffer,
239 return decimal2str ("-", -l);
241 return decimal2str ("", l);
244 /* Eliminate warning from compiler on 32-bit systems. */
245 static int thirty_two = 32;
247 /* Convert a ULONGEST into a HEX string, like %lx. The result is
248 stored in a circular static buffer, NUMCELLS deep. */
251 phex_nz (ULONGEST l, int sizeof_l)
259 unsigned long high = (unsigned long) (l >> thirty_two);
262 xsnprintf (str, CELLSIZE, "%lx",
263 (unsigned long) (l & 0xffffffff));
265 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
266 (unsigned long) (l & 0xffffffff));
271 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
275 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
278 str = phex_nz (l, sizeof (l));
285 /* Convert a CORE_ADDR into a HEX string, like %lx.
286 The result is stored in a circular static buffer, NUMCELLS deep. */
289 paddress (CORE_ADDR addr)
291 return phex_nz (addr, sizeof (CORE_ADDR));
294 /* Convert a file descriptor into a printable string. */
297 pfildes (gdb_fildes_t fd)
300 return phex_nz (fd, sizeof (gdb_fildes_t));
302 return plongest (fd);