| 1 | /* General utility routines for the remote server for GDB. |
| 2 | Copyright (C) 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
| 3 | 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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 |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 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. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "server.h" |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 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 |
| 30 | |
| 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 | |
| 39 | /* Generally useful subroutines used throughout the program. */ |
| 40 | |
| 41 | static void malloc_failure (size_t size) ATTR_NORETURN; |
| 42 | |
| 43 | static void |
| 44 | malloc_failure (size_t size) |
| 45 | { |
| 46 | fprintf (stderr, PREFIX "ran out of memory while trying to allocate %lu bytes\n", |
| 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 | |
| 54 | void * |
| 55 | xmalloc (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 | |
| 68 | /* Reallocate memory without fail. This works like xmalloc. */ |
| 69 | |
| 70 | void * |
| 71 | xrealloc (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 | |
| 88 | /* Allocate memory without fail and set it to zero. |
| 89 | If malloc fails, this will print a message to stderr and exit. */ |
| 90 | |
| 91 | void * |
| 92 | xcalloc (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 | |
| 109 | char * |
| 110 | xstrdup (const char *s) |
| 111 | { |
| 112 | char *ret = strdup (s); |
| 113 | if (ret == NULL) |
| 114 | malloc_failure (strlen (s) + 1); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | #ifndef IN_PROCESS_AGENT |
| 119 | |
| 120 | /* Free a standard argv vector. */ |
| 121 | |
| 122 | void |
| 123 | freeargv (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 | |
| 137 | #endif |
| 138 | |
| 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 | |
| 143 | void |
| 144 | perror_with_name (const char *string) |
| 145 | { |
| 146 | const char *err; |
| 147 | char *combined; |
| 148 | |
| 149 | err = strerror (errno); |
| 150 | if (err == NULL) |
| 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 | |
| 165 | void |
| 166 | error (const char *string,...) |
| 167 | { |
| 168 | #ifndef IN_PROCESS_AGENT |
| 169 | extern jmp_buf toplevel; |
| 170 | #endif |
| 171 | va_list args; |
| 172 | va_start (args, string); |
| 173 | fflush (stdout); |
| 174 | vfprintf (stderr, string, args); |
| 175 | fprintf (stderr, "\n"); |
| 176 | #ifndef IN_PROCESS_AGENT |
| 177 | longjmp (toplevel, 1); |
| 178 | #else |
| 179 | exit (1); |
| 180 | #endif |
| 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 */ |
| 188 | void |
| 189 | fatal (const char *string,...) |
| 190 | { |
| 191 | va_list args; |
| 192 | va_start (args, string); |
| 193 | fprintf (stderr, PREFIX); |
| 194 | vfprintf (stderr, string, args); |
| 195 | fprintf (stderr, "\n"); |
| 196 | va_end (args); |
| 197 | exit (1); |
| 198 | } |
| 199 | |
| 200 | /* VARARGS */ |
| 201 | void |
| 202 | warning (const char *string,...) |
| 203 | { |
| 204 | va_list args; |
| 205 | va_start (args, string); |
| 206 | fprintf (stderr, PREFIX); |
| 207 | vfprintf (stderr, string, args); |
| 208 | fprintf (stderr, "\n"); |
| 209 | va_end (args); |
| 210 | } |
| 211 | |
| 212 | /* Report a problem internal to GDBserver, and exit. */ |
| 213 | |
| 214 | void |
| 215 | internal_error (const char *file, int line, const char *fmt, ...) |
| 216 | { |
| 217 | va_list args; |
| 218 | va_start (args, fmt); |
| 219 | |
| 220 | fprintf (stderr, "\ |
| 221 | %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); |
| 222 | vfprintf (stderr, fmt, args); |
| 223 | fprintf (stderr, "\n"); |
| 224 | va_end (args); |
| 225 | exit (1); |
| 226 | } |
| 227 | |
| 228 | /* Temporary storage using circular buffer. */ |
| 229 | #define NUMCELLS 10 |
| 230 | #define CELLSIZE 50 |
| 231 | |
| 232 | /* Return the next entry in the circular buffer. */ |
| 233 | |
| 234 | static char * |
| 235 | get_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 | |
| 247 | static int |
| 248 | xsnprintf (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 | |
| 263 | char * |
| 264 | paddress (CORE_ADDR addr) |
| 265 | { |
| 266 | char *str = get_cell (); |
| 267 | xsnprintf (str, CELLSIZE, "%lx", (long) addr); |
| 268 | return str; |
| 269 | } |
| 270 | |
| 271 | static char * |
| 272 | decimal2str (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 | |
| 317 | char * |
| 318 | pulongest (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 | |
| 326 | char * |
| 327 | plongest (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. */ |
| 336 | static 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 | |
| 341 | char * |
| 342 | phex_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 | } |