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