Commit | Line | Data |
---|---|---|
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, |
0fb0cc75 | 3 | 2007, 2008, 2009 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 SS |
30 | |
31 | /* Generally useful subroutines used throughout the program. */ | |
32 | ||
bca929d3 DE |
33 | static void malloc_failure (size_t size) ATTR_NORETURN; |
34 | ||
35 | static void | |
36 | malloc_failure (size_t size) | |
37 | { | |
38 | fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n", | |
39 | (unsigned long) size); | |
40 | exit (1); | |
41 | } | |
42 | ||
43 | /* Allocate memory without fail. | |
44 | If malloc fails, this will print a message to stderr and exit. */ | |
45 | ||
46 | void * | |
47 | xmalloc (size_t size) | |
48 | { | |
49 | void *newmem; | |
50 | ||
51 | if (size == 0) | |
52 | size = 1; | |
53 | newmem = malloc (size); | |
54 | if (!newmem) | |
55 | malloc_failure (size); | |
56 | ||
57 | return newmem; | |
58 | } | |
59 | ||
60 | /* Allocate memory without fail and set it to zero. | |
61 | If malloc fails, this will print a message to stderr and exit. */ | |
62 | ||
63 | void * | |
64 | xcalloc (size_t nelem, size_t elsize) | |
65 | { | |
66 | void *newmem; | |
67 | ||
68 | if (nelem == 0 || elsize == 0) | |
69 | nelem = elsize = 1; | |
70 | ||
71 | newmem = calloc (nelem, elsize); | |
72 | if (!newmem) | |
73 | malloc_failure (nelem * elsize); | |
74 | ||
75 | return newmem; | |
76 | } | |
77 | ||
78 | /* Copy a string into a memory buffer. | |
79 | If malloc fails, this will print a message to stderr and exit. */ | |
80 | ||
81 | char * | |
82 | xstrdup (const char *s) | |
83 | { | |
84 | char *ret = strdup (s); | |
85 | if (ret == NULL) | |
86 | malloc_failure (strlen (s) + 1); | |
87 | return ret; | |
88 | } | |
89 | ||
aef93bd7 DE |
90 | /* Free a standard argv vector. */ |
91 | ||
92 | void | |
93 | freeargv (char **vector) | |
94 | { | |
95 | char **scan; | |
96 | ||
97 | if (vector != NULL) | |
98 | { | |
99 | for (scan = vector; *scan != NULL; scan++) | |
100 | { | |
101 | free (*scan); | |
102 | } | |
103 | free (vector); | |
104 | } | |
105 | } | |
106 | ||
c906108c SS |
107 | /* Print the system error message for errno, and also mention STRING |
108 | as the file name for which the error was encountered. | |
109 | Then return to command level. */ | |
110 | ||
111 | void | |
54363045 | 112 | perror_with_name (const char *string) |
c906108c | 113 | { |
5c44784c | 114 | const char *err; |
c906108c SS |
115 | char *combined; |
116 | ||
43d5792c DJ |
117 | err = strerror (errno); |
118 | if (err == NULL) | |
c906108c SS |
119 | err = "unknown error"; |
120 | ||
121 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
122 | strcpy (combined, string); | |
123 | strcat (combined, ": "); | |
124 | strcat (combined, err); | |
125 | ||
126 | error ("%s.", combined); | |
127 | } | |
128 | ||
129 | /* Print an error message and return to command level. | |
130 | STRING is the error message, used as a fprintf string, | |
131 | and ARG is passed as an argument to it. */ | |
132 | ||
0729219d | 133 | void |
c5aa993b | 134 | error (const char *string,...) |
c906108c SS |
135 | { |
136 | extern jmp_buf toplevel; | |
137 | va_list args; | |
c906108c | 138 | va_start (args, string); |
c906108c | 139 | fflush (stdout); |
c906108c | 140 | vfprintf (stderr, string, args); |
c906108c | 141 | fprintf (stderr, "\n"); |
c5aa993b | 142 | longjmp (toplevel, 1); |
c906108c SS |
143 | } |
144 | ||
145 | /* Print an error message and exit reporting failure. | |
146 | This is for a error that we cannot continue from. | |
147 | STRING and ARG are passed to fprintf. */ | |
148 | ||
149 | /* VARARGS */ | |
0729219d | 150 | void |
0a30fbc4 | 151 | fatal (const char *string,...) |
c906108c SS |
152 | { |
153 | va_list args; | |
c906108c | 154 | va_start (args, string); |
24a09b5f | 155 | fprintf (stderr, "gdbserver: "); |
c906108c SS |
156 | vfprintf (stderr, string, args); |
157 | fprintf (stderr, "\n"); | |
158 | va_end (args); | |
159 | exit (1); | |
160 | } | |
0a30fbc4 DJ |
161 | |
162 | /* VARARGS */ | |
163 | void | |
164 | warning (const char *string,...) | |
165 | { | |
166 | va_list args; | |
167 | va_start (args, string); | |
24a09b5f | 168 | fprintf (stderr, "gdbserver: "); |
0a30fbc4 DJ |
169 | vfprintf (stderr, string, args); |
170 | fprintf (stderr, "\n"); | |
171 | va_end (args); | |
172 | } | |
aa5ca48f DE |
173 | |
174 | /* Temporary storage using circular buffer. */ | |
175 | #define NUMCELLS 4 | |
176 | #define CELLSIZE 50 | |
177 | ||
178 | /* Return the next entry in the circular buffer. */ | |
179 | ||
180 | static char * | |
181 | get_cell (void) | |
182 | { | |
183 | static char buf[NUMCELLS][CELLSIZE]; | |
184 | static int cell = 0; | |
185 | if (++cell >= NUMCELLS) | |
186 | cell = 0; | |
187 | return buf[cell]; | |
188 | } | |
189 | ||
190 | /* Stdarg wrapper around vsnprintf. | |
191 | SIZE is the size of the buffer pointed to by STR. */ | |
192 | ||
193 | static int | |
194 | xsnprintf (char *str, size_t size, const char *format, ...) | |
195 | { | |
196 | va_list args; | |
197 | int ret; | |
198 | ||
199 | va_start (args, format); | |
200 | ret = vsnprintf (str, size, format, args); | |
201 | va_end (args); | |
202 | ||
203 | return ret; | |
204 | } | |
205 | ||
206 | /* Convert a CORE_ADDR into a HEX string, like %lx. | |
207 | The result is stored in a circular static buffer, NUMCELLS deep. */ | |
208 | ||
209 | char * | |
210 | paddress (CORE_ADDR addr) | |
211 | { | |
212 | char *str = get_cell (); | |
213 | xsnprintf (str, CELLSIZE, "%lx", (long) addr); | |
214 | return str; | |
215 | } |