Commit | Line | Data |
---|---|---|
c906108c | 1 | /* General utility routines for the remote server for GDB. |
28e7fd62 | 2 | Copyright (C) 1986-2013 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
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 | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 9 | (at your option) any later version. |
c906108c | 10 | |
c5aa993b JM |
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. | |
c906108c | 15 | |
c5aa993b | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
18 | |
19 | #include "server.h" | |
20 | #include <stdio.h> | |
21 | #include <string.h> | |
68070c10 PA |
22 | #include <stdlib.h> |
23 | #if HAVE_ERRNO_H | |
24 | #include <errno.h> | |
25 | #endif | |
c906108c | 26 | |
fa593d66 PA |
27 | #ifdef IN_PROCESS_AGENT |
28 | # define PREFIX "ipa: " | |
29 | # define TOOLNAME "GDBserver in-process agent" | |
30 | #else | |
31 | # define PREFIX "gdbserver: " | |
32 | # define TOOLNAME "GDBserver" | |
33 | #endif | |
34 | ||
c906108c SS |
35 | /* Generally useful subroutines used throughout the program. */ |
36 | ||
d26e3629 KY |
37 | void |
38 | malloc_failure (long size) | |
bca929d3 | 39 | { |
493e2a69 MS |
40 | fprintf (stderr, |
41 | PREFIX "ran out of memory while trying to allocate %lu bytes\n", | |
bca929d3 DE |
42 | (unsigned long) size); |
43 | exit (1); | |
44 | } | |
45 | ||
bca929d3 DE |
46 | /* Copy a string into a memory buffer. |
47 | If malloc fails, this will print a message to stderr and exit. */ | |
48 | ||
49 | char * | |
50 | xstrdup (const char *s) | |
51 | { | |
52 | char *ret = strdup (s); | |
53 | if (ret == NULL) | |
54 | malloc_failure (strlen (s) + 1); | |
55 | return ret; | |
56 | } | |
57 | ||
fa593d66 PA |
58 | #ifndef IN_PROCESS_AGENT |
59 | ||
aef93bd7 DE |
60 | /* Free a standard argv vector. */ |
61 | ||
62 | void | |
63 | freeargv (char **vector) | |
64 | { | |
65 | char **scan; | |
66 | ||
67 | if (vector != NULL) | |
68 | { | |
69 | for (scan = vector; *scan != NULL; scan++) | |
70 | { | |
71 | free (*scan); | |
72 | } | |
73 | free (vector); | |
74 | } | |
75 | } | |
76 | ||
fa593d66 PA |
77 | #endif |
78 | ||
c906108c SS |
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. */ | |
82 | ||
83 | void | |
54363045 | 84 | perror_with_name (const char *string) |
c906108c | 85 | { |
5c44784c | 86 | const char *err; |
c906108c SS |
87 | char *combined; |
88 | ||
43d5792c DJ |
89 | err = strerror (errno); |
90 | if (err == NULL) | |
c906108c SS |
91 | err = "unknown error"; |
92 | ||
93 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
94 | strcpy (combined, string); | |
95 | strcat (combined, ": "); | |
96 | strcat (combined, err); | |
97 | ||
98 | error ("%s.", combined); | |
99 | } | |
100 | ||
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. */ | |
104 | ||
0729219d | 105 | void |
c5aa993b | 106 | error (const char *string,...) |
c906108c | 107 | { |
fa593d66 | 108 | #ifndef IN_PROCESS_AGENT |
c906108c | 109 | extern jmp_buf toplevel; |
fa593d66 | 110 | #endif |
c906108c | 111 | va_list args; |
c906108c | 112 | va_start (args, string); |
c906108c | 113 | fflush (stdout); |
c906108c | 114 | vfprintf (stderr, string, args); |
c906108c | 115 | fprintf (stderr, "\n"); |
fa593d66 | 116 | #ifndef IN_PROCESS_AGENT |
c5aa993b | 117 | longjmp (toplevel, 1); |
fa593d66 PA |
118 | #else |
119 | exit (1); | |
120 | #endif | |
c906108c SS |
121 | } |
122 | ||
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. */ | |
126 | ||
127 | /* VARARGS */ | |
0729219d | 128 | void |
0a30fbc4 | 129 | fatal (const char *string,...) |
c906108c SS |
130 | { |
131 | va_list args; | |
c906108c | 132 | va_start (args, string); |
fa593d66 | 133 | fprintf (stderr, PREFIX); |
c906108c SS |
134 | vfprintf (stderr, string, args); |
135 | fprintf (stderr, "\n"); | |
136 | va_end (args); | |
137 | exit (1); | |
138 | } | |
0a30fbc4 DJ |
139 | |
140 | /* VARARGS */ | |
141 | void | |
142 | warning (const char *string,...) | |
143 | { | |
144 | va_list args; | |
145 | va_start (args, string); | |
fa593d66 | 146 | fprintf (stderr, PREFIX); |
0a30fbc4 DJ |
147 | vfprintf (stderr, string, args); |
148 | fprintf (stderr, "\n"); | |
149 | va_end (args); | |
150 | } | |
aa5ca48f | 151 | |
e92d13d5 PA |
152 | /* Report a problem internal to GDBserver, and exit. */ |
153 | ||
154 | void | |
155 | internal_error (const char *file, int line, const char *fmt, ...) | |
156 | { | |
157 | va_list args; | |
158 | va_start (args, fmt); | |
159 | ||
160 | fprintf (stderr, "\ | |
fa593d66 | 161 | %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); |
e92d13d5 PA |
162 | vfprintf (stderr, fmt, args); |
163 | fprintf (stderr, "\n"); | |
164 | va_end (args); | |
165 | exit (1); | |
166 | } | |
167 | ||
aa5ca48f | 168 | /* Temporary storage using circular buffer. */ |
fa593d66 | 169 | #define NUMCELLS 10 |
aa5ca48f DE |
170 | #define CELLSIZE 50 |
171 | ||
172 | /* Return the next entry in the circular buffer. */ | |
173 | ||
174 | static char * | |
175 | get_cell (void) | |
176 | { | |
177 | static char buf[NUMCELLS][CELLSIZE]; | |
178 | static int cell = 0; | |
179 | if (++cell >= NUMCELLS) | |
180 | cell = 0; | |
181 | return buf[cell]; | |
182 | } | |
183 | ||
219f2f23 | 184 | static char * |
238f1c74 | 185 | decimal2str (char *sign, ULONGEST addr) |
219f2f23 PA |
186 | { |
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 (); | |
219f2f23 | 191 | int i = 0; |
9f72fee2 | 192 | int width = 9; |
238f1c74 | 193 | |
219f2f23 PA |
194 | do |
195 | { | |
196 | temp[i] = addr % (1000 * 1000 * 1000); | |
197 | addr /= (1000 * 1000 * 1000); | |
198 | i++; | |
219f2f23 PA |
199 | } |
200 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
201 | ||
219f2f23 PA |
202 | switch (i) |
203 | { | |
204 | case 1: | |
205 | xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]); | |
206 | break; | |
207 | case 2: | |
208 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width, | |
209 | temp[1], temp[0]); | |
210 | break; | |
211 | case 3: | |
212 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width, | |
213 | temp[2], temp[1], temp[0]); | |
214 | break; | |
215 | default: | |
216 | internal_error (__FILE__, __LINE__, | |
217 | "failed internal consistency check"); | |
218 | } | |
219 | ||
220 | return str; | |
221 | } | |
222 | ||
223 | /* %u for ULONGEST. The result is stored in a circular static buffer, | |
224 | NUMCELLS deep. */ | |
225 | ||
226 | char * | |
227 | pulongest (ULONGEST u) | |
228 | { | |
238f1c74 | 229 | return decimal2str ("", u); |
219f2f23 PA |
230 | } |
231 | ||
232 | /* %d for LONGEST. The result is stored in a circular static buffer, | |
233 | NUMCELLS deep. */ | |
234 | ||
235 | char * | |
236 | plongest (LONGEST l) | |
237 | { | |
238 | if (l < 0) | |
238f1c74 | 239 | return decimal2str ("-", -l); |
219f2f23 | 240 | else |
238f1c74 | 241 | return decimal2str ("", l); |
219f2f23 PA |
242 | } |
243 | ||
244 | /* Eliminate warning from compiler on 32-bit systems. */ | |
245 | static int thirty_two = 32; | |
246 | ||
247 | /* Convert a ULONGEST into a HEX string, like %lx. The result is | |
248 | stored in a circular static buffer, NUMCELLS deep. */ | |
249 | ||
250 | char * | |
251 | phex_nz (ULONGEST l, int sizeof_l) | |
252 | { | |
253 | char *str; | |
254 | ||
255 | switch (sizeof_l) | |
256 | { | |
257 | case 8: | |
258 | { | |
259 | unsigned long high = (unsigned long) (l >> thirty_two); | |
260 | str = get_cell (); | |
261 | if (high == 0) | |
262 | xsnprintf (str, CELLSIZE, "%lx", | |
263 | (unsigned long) (l & 0xffffffff)); | |
264 | else | |
265 | xsnprintf (str, CELLSIZE, "%lx%08lx", high, | |
266 | (unsigned long) (l & 0xffffffff)); | |
267 | break; | |
268 | } | |
269 | case 4: | |
270 | str = get_cell (); | |
271 | xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l); | |
272 | break; | |
273 | case 2: | |
274 | str = get_cell (); | |
275 | xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff)); | |
276 | break; | |
277 | default: | |
278 | str = phex_nz (l, sizeof (l)); | |
279 | break; | |
280 | } | |
281 | ||
282 | return str; | |
283 | } | |
5c3216e2 OS |
284 | |
285 | /* Convert a CORE_ADDR into a HEX string, like %lx. | |
286 | The result is stored in a circular static buffer, NUMCELLS deep. */ | |
287 | ||
288 | char * | |
289 | paddress (CORE_ADDR addr) | |
290 | { | |
291 | return phex_nz (addr, sizeof (CORE_ADDR)); | |
292 | } | |
ec48365d PA |
293 | |
294 | /* Convert a file descriptor into a printable string. */ | |
295 | ||
296 | char * | |
297 | pfildes (gdb_fildes_t fd) | |
298 | { | |
299 | #if USE_WIN32API | |
300 | return phex_nz (fd, sizeof (gdb_fildes_t)); | |
301 | #else | |
302 | return plongest (fd); | |
303 | #endif | |
304 | } |