* event-loop.c (event_handle_func): Adjust to use gdb_fildes_t.
[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
219f2f23
PA
260static char *
261decimal2str (char *sign, ULONGEST addr, int width)
262{
263 /* Steal code from valprint.c:print_decimal(). Should this worry
264 about the real size of addr as the above does? */
265 unsigned long temp[3];
266 char *str = get_cell ();
267
268 int i = 0;
269 do
270 {
271 temp[i] = addr % (1000 * 1000 * 1000);
272 addr /= (1000 * 1000 * 1000);
273 i++;
274 width -= 9;
275 }
276 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
277
278 width = 9;
279 if (width < 0)
280 width = 0;
281
282 switch (i)
283 {
284 case 1:
285 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
286 break;
287 case 2:
288 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
289 temp[1], temp[0]);
290 break;
291 case 3:
292 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
293 temp[2], temp[1], temp[0]);
294 break;
295 default:
296 internal_error (__FILE__, __LINE__,
297 "failed internal consistency check");
298 }
299
300 return str;
301}
302
303/* %u for ULONGEST. The result is stored in a circular static buffer,
304 NUMCELLS deep. */
305
306char *
307pulongest (ULONGEST u)
308{
309 return decimal2str ("", u, 0);
310}
311
312/* %d for LONGEST. The result is stored in a circular static buffer,
313 NUMCELLS deep. */
314
315char *
316plongest (LONGEST l)
317{
318 if (l < 0)
319 return decimal2str ("-", -l, 0);
320 else
321 return decimal2str ("", l, 0);
322}
323
324/* Eliminate warning from compiler on 32-bit systems. */
325static int thirty_two = 32;
326
327/* Convert a ULONGEST into a HEX string, like %lx. The result is
328 stored in a circular static buffer, NUMCELLS deep. */
329
330char *
331phex_nz (ULONGEST l, int sizeof_l)
332{
333 char *str;
334
335 switch (sizeof_l)
336 {
337 case 8:
338 {
339 unsigned long high = (unsigned long) (l >> thirty_two);
340 str = get_cell ();
341 if (high == 0)
342 xsnprintf (str, CELLSIZE, "%lx",
343 (unsigned long) (l & 0xffffffff));
344 else
345 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
346 (unsigned long) (l & 0xffffffff));
347 break;
348 }
349 case 4:
350 str = get_cell ();
351 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
352 break;
353 case 2:
354 str = get_cell ();
355 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
356 break;
357 default:
358 str = phex_nz (l, sizeof (l));
359 break;
360 }
361
362 return str;
363}
5c3216e2
OS
364
365/* Convert a CORE_ADDR into a HEX string, like %lx.
366 The result is stored in a circular static buffer, NUMCELLS deep. */
367
368char *
369paddress (CORE_ADDR addr)
370{
371 return phex_nz (addr, sizeof (CORE_ADDR));
372}
ec48365d
PA
373
374/* Convert a file descriptor into a printable string. */
375
376char *
377pfildes (gdb_fildes_t fd)
378{
379#if USE_WIN32API
380 return phex_nz (fd, sizeof (gdb_fildes_t));
381#else
382 return plongest (fd);
383#endif
384}
This page took 0.724463 seconds and 4 git commands to generate.