| 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, 2011 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 | |
| 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 | |
| 36 | /* Generally useful subroutines used throughout the program. */ |
| 37 | |
| 38 | void |
| 39 | malloc_failure (long size) |
| 40 | { |
| 41 | fprintf (stderr, |
| 42 | PREFIX "ran out of memory while trying to allocate %lu bytes\n", |
| 43 | (unsigned long) size); |
| 44 | exit (1); |
| 45 | } |
| 46 | |
| 47 | /* Copy a string into a memory buffer. |
| 48 | If malloc fails, this will print a message to stderr and exit. */ |
| 49 | |
| 50 | char * |
| 51 | xstrdup (const char *s) |
| 52 | { |
| 53 | char *ret = strdup (s); |
| 54 | if (ret == NULL) |
| 55 | malloc_failure (strlen (s) + 1); |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | #ifndef IN_PROCESS_AGENT |
| 60 | |
| 61 | /* Free a standard argv vector. */ |
| 62 | |
| 63 | void |
| 64 | freeargv (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 | |
| 78 | #endif |
| 79 | |
| 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 | |
| 84 | void |
| 85 | perror_with_name (const char *string) |
| 86 | { |
| 87 | const char *err; |
| 88 | char *combined; |
| 89 | |
| 90 | err = strerror (errno); |
| 91 | if (err == NULL) |
| 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 | |
| 106 | void |
| 107 | error (const char *string,...) |
| 108 | { |
| 109 | #ifndef IN_PROCESS_AGENT |
| 110 | extern jmp_buf toplevel; |
| 111 | #endif |
| 112 | va_list args; |
| 113 | va_start (args, string); |
| 114 | fflush (stdout); |
| 115 | vfprintf (stderr, string, args); |
| 116 | fprintf (stderr, "\n"); |
| 117 | #ifndef IN_PROCESS_AGENT |
| 118 | longjmp (toplevel, 1); |
| 119 | #else |
| 120 | exit (1); |
| 121 | #endif |
| 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 */ |
| 129 | void |
| 130 | fatal (const char *string,...) |
| 131 | { |
| 132 | va_list args; |
| 133 | va_start (args, string); |
| 134 | fprintf (stderr, PREFIX); |
| 135 | vfprintf (stderr, string, args); |
| 136 | fprintf (stderr, "\n"); |
| 137 | va_end (args); |
| 138 | exit (1); |
| 139 | } |
| 140 | |
| 141 | /* VARARGS */ |
| 142 | void |
| 143 | warning (const char *string,...) |
| 144 | { |
| 145 | va_list args; |
| 146 | va_start (args, string); |
| 147 | fprintf (stderr, PREFIX); |
| 148 | vfprintf (stderr, string, args); |
| 149 | fprintf (stderr, "\n"); |
| 150 | va_end (args); |
| 151 | } |
| 152 | |
| 153 | /* Report a problem internal to GDBserver, and exit. */ |
| 154 | |
| 155 | void |
| 156 | internal_error (const char *file, int line, const char *fmt, ...) |
| 157 | { |
| 158 | va_list args; |
| 159 | va_start (args, fmt); |
| 160 | |
| 161 | fprintf (stderr, "\ |
| 162 | %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); |
| 163 | vfprintf (stderr, fmt, args); |
| 164 | fprintf (stderr, "\n"); |
| 165 | va_end (args); |
| 166 | exit (1); |
| 167 | } |
| 168 | |
| 169 | /* Temporary storage using circular buffer. */ |
| 170 | #define NUMCELLS 10 |
| 171 | #define CELLSIZE 50 |
| 172 | |
| 173 | /* Return the next entry in the circular buffer. */ |
| 174 | |
| 175 | static char * |
| 176 | get_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 | |
| 185 | static char * |
| 186 | decimal2str (char *sign, ULONGEST addr) |
| 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 (); |
| 192 | int i = 0; |
| 193 | int width = 9; |
| 194 | |
| 195 | do |
| 196 | { |
| 197 | temp[i] = addr % (1000 * 1000 * 1000); |
| 198 | addr /= (1000 * 1000 * 1000); |
| 199 | i++; |
| 200 | } |
| 201 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); |
| 202 | |
| 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 | |
| 227 | char * |
| 228 | pulongest (ULONGEST u) |
| 229 | { |
| 230 | return decimal2str ("", u); |
| 231 | } |
| 232 | |
| 233 | /* %d for LONGEST. The result is stored in a circular static buffer, |
| 234 | NUMCELLS deep. */ |
| 235 | |
| 236 | char * |
| 237 | plongest (LONGEST l) |
| 238 | { |
| 239 | if (l < 0) |
| 240 | return decimal2str ("-", -l); |
| 241 | else |
| 242 | return decimal2str ("", l); |
| 243 | } |
| 244 | |
| 245 | /* Eliminate warning from compiler on 32-bit systems. */ |
| 246 | static 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 | |
| 251 | char * |
| 252 | phex_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 | } |
| 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 | |
| 289 | char * |
| 290 | paddress (CORE_ADDR addr) |
| 291 | { |
| 292 | return phex_nz (addr, sizeof (CORE_ADDR)); |
| 293 | } |
| 294 | |
| 295 | /* Convert a file descriptor into a printable string. */ |
| 296 | |
| 297 | char * |
| 298 | pfildes (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 | } |