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, 2011 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/>. */
28 #ifdef IN_PROCESS_AGENT
29 # define PREFIX "ipa: "
30 # define TOOLNAME "GDBserver in-process agent"
32 # define PREFIX "gdbserver: "
33 # define TOOLNAME "GDBserver"
36 /* Generally useful subroutines used throughout the program. */
38 static void malloc_failure (size_t size
) ATTR_NORETURN
;
41 malloc_failure (size_t size
)
44 PREFIX
"ran out of memory while trying to allocate %lu bytes\n",
45 (unsigned long) size
);
49 /* Allocate memory without fail.
50 If malloc fails, this will print a message to stderr and exit. */
59 newmem
= malloc (size
);
61 malloc_failure (size
);
66 /* Reallocate memory without fail. This works like xmalloc. */
69 xrealloc (void *ptr
, size_t size
)
77 val
= realloc (ptr
, size
); /* OK: realloc */
79 val
= malloc (size
); /* OK: malloc */
81 malloc_failure (size
);
86 /* Allocate memory without fail and set it to zero.
87 If malloc fails, this will print a message to stderr and exit. */
90 xcalloc (size_t nelem
, size_t elsize
)
94 if (nelem
== 0 || elsize
== 0)
97 newmem
= calloc (nelem
, elsize
);
99 malloc_failure (nelem
* elsize
);
104 /* Copy a string into a memory buffer.
105 If malloc fails, this will print a message to stderr and exit. */
108 xstrdup (const char *s
)
110 char *ret
= strdup (s
);
112 malloc_failure (strlen (s
) + 1);
116 #ifndef IN_PROCESS_AGENT
118 /* Free a standard argv vector. */
121 freeargv (char **vector
)
127 for (scan
= vector
; *scan
!= NULL
; scan
++)
137 /* Print the system error message for errno, and also mention STRING
138 as the file name for which the error was encountered.
139 Then return to command level. */
142 perror_with_name (const char *string
)
147 err
= strerror (errno
);
149 err
= "unknown error";
151 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
152 strcpy (combined
, string
);
153 strcat (combined
, ": ");
154 strcat (combined
, err
);
156 error ("%s.", combined
);
159 /* Print an error message and return to command level.
160 STRING is the error message, used as a fprintf string,
161 and ARG is passed as an argument to it. */
164 error (const char *string
,...)
166 #ifndef IN_PROCESS_AGENT
167 extern jmp_buf toplevel
;
170 va_start (args
, string
);
172 vfprintf (stderr
, string
, args
);
173 fprintf (stderr
, "\n");
174 #ifndef IN_PROCESS_AGENT
175 longjmp (toplevel
, 1);
181 /* Print an error message and exit reporting failure.
182 This is for a error that we cannot continue from.
183 STRING and ARG are passed to fprintf. */
187 fatal (const char *string
,...)
190 va_start (args
, string
);
191 fprintf (stderr
, PREFIX
);
192 vfprintf (stderr
, string
, args
);
193 fprintf (stderr
, "\n");
200 warning (const char *string
,...)
203 va_start (args
, string
);
204 fprintf (stderr
, PREFIX
);
205 vfprintf (stderr
, string
, args
);
206 fprintf (stderr
, "\n");
210 /* Report a problem internal to GDBserver, and exit. */
213 internal_error (const char *file
, int line
, const char *fmt
, ...)
216 va_start (args
, fmt
);
219 %s:%d: A problem internal to " TOOLNAME
" has been detected.\n", file
, line
);
220 vfprintf (stderr
, fmt
, args
);
221 fprintf (stderr
, "\n");
226 /* Temporary storage using circular buffer. */
230 /* Return the next entry in the circular buffer. */
235 static char buf
[NUMCELLS
][CELLSIZE
];
237 if (++cell
>= NUMCELLS
)
242 /* Stdarg wrapper around vsnprintf.
243 SIZE is the size of the buffer pointed to by STR. */
246 xsnprintf (char *str
, size_t size
, const char *format
, ...)
251 va_start (args
, format
);
252 ret
= vsnprintf (str
, size
, format
, args
);
259 decimal2str (char *sign
, ULONGEST addr
)
261 /* Steal code from valprint.c:print_decimal(). Should this worry
262 about the real size of addr as the above does? */
263 unsigned long temp
[3];
264 char *str
= get_cell ();
270 temp
[i
] = addr
% (1000 * 1000 * 1000);
271 addr
/= (1000 * 1000 * 1000);
274 while (addr
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
279 xsnprintf (str
, CELLSIZE
, "%s%0*lu", sign
, width
, temp
[0]);
282 xsnprintf (str
, CELLSIZE
, "%s%0*lu%09lu", sign
, width
,
286 xsnprintf (str
, CELLSIZE
, "%s%0*lu%09lu%09lu", sign
, width
,
287 temp
[2], temp
[1], temp
[0]);
290 internal_error (__FILE__
, __LINE__
,
291 "failed internal consistency check");
297 /* %u for ULONGEST. The result is stored in a circular static buffer,
301 pulongest (ULONGEST u
)
303 return decimal2str ("", u
);
306 /* %d for LONGEST. The result is stored in a circular static buffer,
313 return decimal2str ("-", -l
);
315 return decimal2str ("", l
);
318 /* Eliminate warning from compiler on 32-bit systems. */
319 static int thirty_two
= 32;
321 /* Convert a ULONGEST into a HEX string, like %lx. The result is
322 stored in a circular static buffer, NUMCELLS deep. */
325 phex_nz (ULONGEST l
, int sizeof_l
)
333 unsigned long high
= (unsigned long) (l
>> thirty_two
);
336 xsnprintf (str
, CELLSIZE
, "%lx",
337 (unsigned long) (l
& 0xffffffff));
339 xsnprintf (str
, CELLSIZE
, "%lx%08lx", high
,
340 (unsigned long) (l
& 0xffffffff));
345 xsnprintf (str
, CELLSIZE
, "%lx", (unsigned long) l
);
349 xsnprintf (str
, CELLSIZE
, "%x", (unsigned short) (l
& 0xffff));
352 str
= phex_nz (l
, sizeof (l
));
359 /* Convert a CORE_ADDR into a HEX string, like %lx.
360 The result is stored in a circular static buffer, NUMCELLS deep. */
363 paddress (CORE_ADDR addr
)
365 return phex_nz (addr
, sizeof (CORE_ADDR
));
368 /* Convert a file descriptor into a printable string. */
371 pfildes (gdb_fildes_t fd
)
374 return phex_nz (fd
, sizeof (gdb_fildes_t
));
376 return plongest (fd
);
This page took 0.039407 seconds and 5 git commands to generate.