2012-06-04 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / utils.c
CommitLineData
c906108c 1/* General utility routines for the remote server for GDB.
0b302171
JB
2 Copyright (C) 1986, 1989, 1993, 1995-1997, 1999-2000, 2002-2003,
3 2007-2012 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
c906108c 27
fa593d66
PA
28#ifdef IN_PROCESS_AGENT
29# define PREFIX "ipa: "
30# define TOOLNAME "GDBserver in-process agent"
31#else
32# define PREFIX "gdbserver: "
33# define TOOLNAME "GDBserver"
34#endif
35
c906108c
SS
36/* Generally useful subroutines used throughout the program. */
37
d26e3629
KY
38void
39malloc_failure (long size)
bca929d3 40{
493e2a69
MS
41 fprintf (stderr,
42 PREFIX "ran out of memory while trying to allocate %lu bytes\n",
bca929d3
DE
43 (unsigned long) size);
44 exit (1);
45}
46
bca929d3
DE
47/* Copy a string into a memory buffer.
48 If malloc fails, this will print a message to stderr and exit. */
49
50char *
51xstrdup (const char *s)
52{
53 char *ret = strdup (s);
54 if (ret == NULL)
55 malloc_failure (strlen (s) + 1);
56 return ret;
57}
58
fa593d66
PA
59#ifndef IN_PROCESS_AGENT
60
aef93bd7
DE
61/* Free a standard argv vector. */
62
63void
64freeargv (char **vector)
65{
66 char **scan;
67
68 if (vector != NULL)
69 {
70 for (scan = vector; *scan != NULL; scan++)
71 {
72 free (*scan);
73 }
74 free (vector);
75 }
76}
77
fa593d66
PA
78#endif
79
c906108c
SS
80/* Print the system error message for errno, and also mention STRING
81 as the file name for which the error was encountered.
82 Then return to command level. */
83
84void
54363045 85perror_with_name (const char *string)
c906108c 86{
5c44784c 87 const char *err;
c906108c
SS
88 char *combined;
89
43d5792c
DJ
90 err = strerror (errno);
91 if (err == NULL)
c906108c
SS
92 err = "unknown error";
93
94 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
95 strcpy (combined, string);
96 strcat (combined, ": ");
97 strcat (combined, err);
98
99 error ("%s.", combined);
100}
101
102/* Print an error message and return to command level.
103 STRING is the error message, used as a fprintf string,
104 and ARG is passed as an argument to it. */
105
0729219d 106void
c5aa993b 107error (const char *string,...)
c906108c 108{
fa593d66 109#ifndef IN_PROCESS_AGENT
c906108c 110 extern jmp_buf toplevel;
fa593d66 111#endif
c906108c 112 va_list args;
c906108c 113 va_start (args, string);
c906108c 114 fflush (stdout);
c906108c 115 vfprintf (stderr, string, args);
c906108c 116 fprintf (stderr, "\n");
fa593d66 117#ifndef IN_PROCESS_AGENT
c5aa993b 118 longjmp (toplevel, 1);
fa593d66
PA
119#else
120 exit (1);
121#endif
c906108c
SS
122}
123
124/* Print an error message and exit reporting failure.
125 This is for a error that we cannot continue from.
126 STRING and ARG are passed to fprintf. */
127
128/* VARARGS */
0729219d 129void
0a30fbc4 130fatal (const char *string,...)
c906108c
SS
131{
132 va_list args;
c906108c 133 va_start (args, string);
fa593d66 134 fprintf (stderr, PREFIX);
c906108c
SS
135 vfprintf (stderr, string, args);
136 fprintf (stderr, "\n");
137 va_end (args);
138 exit (1);
139}
0a30fbc4
DJ
140
141/* VARARGS */
142void
143warning (const char *string,...)
144{
145 va_list args;
146 va_start (args, string);
fa593d66 147 fprintf (stderr, PREFIX);
0a30fbc4
DJ
148 vfprintf (stderr, string, args);
149 fprintf (stderr, "\n");
150 va_end (args);
151}
aa5ca48f 152
e92d13d5
PA
153/* Report a problem internal to GDBserver, and exit. */
154
155void
156internal_error (const char *file, int line, const char *fmt, ...)
157{
158 va_list args;
159 va_start (args, fmt);
160
161 fprintf (stderr, "\
fa593d66 162%s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
e92d13d5
PA
163 vfprintf (stderr, fmt, args);
164 fprintf (stderr, "\n");
165 va_end (args);
166 exit (1);
167}
168
aa5ca48f 169/* Temporary storage using circular buffer. */
fa593d66 170#define NUMCELLS 10
aa5ca48f
DE
171#define CELLSIZE 50
172
173/* Return the next entry in the circular buffer. */
174
175static char *
176get_cell (void)
177{
178 static char buf[NUMCELLS][CELLSIZE];
179 static int cell = 0;
180 if (++cell >= NUMCELLS)
181 cell = 0;
182 return buf[cell];
183}
184
219f2f23 185static char *
238f1c74 186decimal2str (char *sign, ULONGEST addr)
219f2f23
PA
187{
188 /* Steal code from valprint.c:print_decimal(). Should this worry
189 about the real size of addr as the above does? */
190 unsigned long temp[3];
191 char *str = get_cell ();
219f2f23 192 int i = 0;
9f72fee2 193 int width = 9;
238f1c74 194
219f2f23
PA
195 do
196 {
197 temp[i] = addr % (1000 * 1000 * 1000);
198 addr /= (1000 * 1000 * 1000);
199 i++;
219f2f23
PA
200 }
201 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
202
219f2f23
PA
203 switch (i)
204 {
205 case 1:
206 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
207 break;
208 case 2:
209 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
210 temp[1], temp[0]);
211 break;
212 case 3:
213 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
214 temp[2], temp[1], temp[0]);
215 break;
216 default:
217 internal_error (__FILE__, __LINE__,
218 "failed internal consistency check");
219 }
220
221 return str;
222}
223
224/* %u for ULONGEST. The result is stored in a circular static buffer,
225 NUMCELLS deep. */
226
227char *
228pulongest (ULONGEST u)
229{
238f1c74 230 return decimal2str ("", u);
219f2f23
PA
231}
232
233/* %d for LONGEST. The result is stored in a circular static buffer,
234 NUMCELLS deep. */
235
236char *
237plongest (LONGEST l)
238{
239 if (l < 0)
238f1c74 240 return decimal2str ("-", -l);
219f2f23 241 else
238f1c74 242 return decimal2str ("", l);
219f2f23
PA
243}
244
245/* Eliminate warning from compiler on 32-bit systems. */
246static int thirty_two = 32;
247
248/* Convert a ULONGEST into a HEX string, like %lx. The result is
249 stored in a circular static buffer, NUMCELLS deep. */
250
251char *
252phex_nz (ULONGEST l, int sizeof_l)
253{
254 char *str;
255
256 switch (sizeof_l)
257 {
258 case 8:
259 {
260 unsigned long high = (unsigned long) (l >> thirty_two);
261 str = get_cell ();
262 if (high == 0)
263 xsnprintf (str, CELLSIZE, "%lx",
264 (unsigned long) (l & 0xffffffff));
265 else
266 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
267 (unsigned long) (l & 0xffffffff));
268 break;
269 }
270 case 4:
271 str = get_cell ();
272 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
273 break;
274 case 2:
275 str = get_cell ();
276 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
277 break;
278 default:
279 str = phex_nz (l, sizeof (l));
280 break;
281 }
282
283 return str;
284}
5c3216e2
OS
285
286/* Convert a CORE_ADDR into a HEX string, like %lx.
287 The result is stored in a circular static buffer, NUMCELLS deep. */
288
289char *
290paddress (CORE_ADDR addr)
291{
292 return phex_nz (addr, sizeof (CORE_ADDR));
293}
ec48365d
PA
294
295/* Convert a file descriptor into a printable string. */
296
297char *
298pfildes (gdb_fildes_t fd)
299{
300#if USE_WIN32API
301 return phex_nz (fd, sizeof (gdb_fildes_t));
302#else
303 return plongest (fd);
304#endif
305}
This page took 0.981163 seconds and 4 git commands to generate.