| 1 | /* Remote utility routines for the remote server for GDB. |
| 2 | Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 2 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, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. */ |
| 21 | |
| 22 | #include "server.h" |
| 23 | #include "terminal.h" |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #if HAVE_SYS_IOCTL_H |
| 27 | #include <sys/ioctl.h> |
| 28 | #endif |
| 29 | #if HAVE_SYS_FILE_H |
| 30 | #include <sys/file.h> |
| 31 | #endif |
| 32 | #if HAVE_NETINET_IN_H |
| 33 | #include <netinet/in.h> |
| 34 | #endif |
| 35 | #if HAVE_SYS_SOCKET_H |
| 36 | #include <sys/socket.h> |
| 37 | #endif |
| 38 | #if HAVE_NETDB_H |
| 39 | #include <netdb.h> |
| 40 | #endif |
| 41 | #if HAVE_NETINET_TCP_H |
| 42 | #include <netinet/tcp.h> |
| 43 | #endif |
| 44 | #if HAVE_SYS_IOCTL_H |
| 45 | #include <sys/ioctl.h> |
| 46 | #endif |
| 47 | #if HAVE_SIGNAL_H |
| 48 | #include <signal.h> |
| 49 | #endif |
| 50 | #if HAVE_FCNTL_H |
| 51 | #include <fcntl.h> |
| 52 | #endif |
| 53 | #include <sys/time.h> |
| 54 | #if HAVE_UNISTD_H |
| 55 | #include <unistd.h> |
| 56 | #endif |
| 57 | #if HAVE_ARPA_INET_H |
| 58 | #include <arpa/inet.h> |
| 59 | #endif |
| 60 | #include <sys/stat.h> |
| 61 | #if HAVE_ERRNO_H |
| 62 | #include <errno.h> |
| 63 | #endif |
| 64 | |
| 65 | #if USE_WIN32API |
| 66 | #include <winsock.h> |
| 67 | #endif |
| 68 | |
| 69 | #ifndef HAVE_SOCKLEN_T |
| 70 | typedef int socklen_t; |
| 71 | #endif |
| 72 | |
| 73 | #if USE_WIN32API |
| 74 | # define INVALID_DESCRIPTOR INVALID_SOCKET |
| 75 | #else |
| 76 | # define INVALID_DESCRIPTOR -1 |
| 77 | #endif |
| 78 | |
| 79 | /* A cache entry for a successfully looked-up symbol. */ |
| 80 | struct sym_cache |
| 81 | { |
| 82 | const char *name; |
| 83 | CORE_ADDR addr; |
| 84 | struct sym_cache *next; |
| 85 | }; |
| 86 | |
| 87 | /* The symbol cache. */ |
| 88 | static struct sym_cache *symbol_cache; |
| 89 | |
| 90 | /* If this flag has been set, assume cache misses are |
| 91 | failures. */ |
| 92 | int all_symbols_looked_up; |
| 93 | |
| 94 | int remote_debug = 0; |
| 95 | struct ui_file *gdb_stdlog; |
| 96 | |
| 97 | static int remote_desc = INVALID_DESCRIPTOR; |
| 98 | |
| 99 | /* FIXME headerize? */ |
| 100 | extern int using_threads; |
| 101 | extern int debug_threads; |
| 102 | |
| 103 | #ifdef USE_WIN32API |
| 104 | # define read(fd, buf, len) recv (fd, (char *) buf, len, 0) |
| 105 | # define write(fd, buf, len) send (fd, (char *) buf, len, 0) |
| 106 | #endif |
| 107 | |
| 108 | /* Open a connection to a remote debugger. |
| 109 | NAME is the filename used for communication. */ |
| 110 | |
| 111 | void |
| 112 | remote_open (char *name) |
| 113 | { |
| 114 | #if defined(F_SETFL) && defined (FASYNC) |
| 115 | int save_fcntl_flags; |
| 116 | #endif |
| 117 | char *port_str; |
| 118 | |
| 119 | port_str = strchr (name, ':'); |
| 120 | if (port_str == NULL) |
| 121 | { |
| 122 | #ifdef USE_WIN32API |
| 123 | error ("Only <host>:<port> is supported on this platform."); |
| 124 | #else |
| 125 | struct stat statbuf; |
| 126 | |
| 127 | if (stat (name, &statbuf) == 0 |
| 128 | && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) |
| 129 | remote_desc = open (name, O_RDWR); |
| 130 | else |
| 131 | { |
| 132 | errno = EINVAL; |
| 133 | remote_desc = -1; |
| 134 | } |
| 135 | |
| 136 | if (remote_desc < 0) |
| 137 | perror_with_name ("Could not open remote device"); |
| 138 | |
| 139 | #ifdef HAVE_TERMIOS |
| 140 | { |
| 141 | struct termios termios; |
| 142 | tcgetattr (remote_desc, &termios); |
| 143 | |
| 144 | termios.c_iflag = 0; |
| 145 | termios.c_oflag = 0; |
| 146 | termios.c_lflag = 0; |
| 147 | termios.c_cflag &= ~(CSIZE | PARENB); |
| 148 | termios.c_cflag |= CLOCAL | CS8; |
| 149 | termios.c_cc[VMIN] = 1; |
| 150 | termios.c_cc[VTIME] = 0; |
| 151 | |
| 152 | tcsetattr (remote_desc, TCSANOW, &termios); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | #ifdef HAVE_TERMIO |
| 157 | { |
| 158 | struct termio termio; |
| 159 | ioctl (remote_desc, TCGETA, &termio); |
| 160 | |
| 161 | termio.c_iflag = 0; |
| 162 | termio.c_oflag = 0; |
| 163 | termio.c_lflag = 0; |
| 164 | termio.c_cflag &= ~(CSIZE | PARENB); |
| 165 | termio.c_cflag |= CLOCAL | CS8; |
| 166 | termio.c_cc[VMIN] = 1; |
| 167 | termio.c_cc[VTIME] = 0; |
| 168 | |
| 169 | ioctl (remote_desc, TCSETA, &termio); |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | #ifdef HAVE_SGTTY |
| 174 | { |
| 175 | struct sgttyb sg; |
| 176 | |
| 177 | ioctl (remote_desc, TIOCGETP, &sg); |
| 178 | sg.sg_flags = RAW; |
| 179 | ioctl (remote_desc, TIOCSETP, &sg); |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | fprintf (stderr, "Remote debugging using %s\n", name); |
| 184 | #endif /* USE_WIN32API */ |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | #ifdef USE_WIN32API |
| 189 | static int winsock_initialized; |
| 190 | #endif |
| 191 | char *port_str; |
| 192 | int port; |
| 193 | struct sockaddr_in sockaddr; |
| 194 | socklen_t tmp; |
| 195 | int tmp_desc; |
| 196 | |
| 197 | port_str = strchr (name, ':'); |
| 198 | |
| 199 | port = atoi (port_str + 1); |
| 200 | |
| 201 | #ifdef USE_WIN32API |
| 202 | if (!winsock_initialized) |
| 203 | { |
| 204 | WSADATA wsad; |
| 205 | |
| 206 | WSAStartup (MAKEWORD (1, 0), &wsad); |
| 207 | winsock_initialized = 1; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 212 | if (tmp_desc < 0) |
| 213 | perror_with_name ("Can't open socket"); |
| 214 | |
| 215 | /* Allow rapid reuse of this port. */ |
| 216 | tmp = 1; |
| 217 | setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
| 218 | sizeof (tmp)); |
| 219 | |
| 220 | sockaddr.sin_family = PF_INET; |
| 221 | sockaddr.sin_port = htons (port); |
| 222 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
| 223 | |
| 224 | if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
| 225 | || listen (tmp_desc, 1)) |
| 226 | perror_with_name ("Can't bind address"); |
| 227 | |
| 228 | /* If port is zero, a random port will be selected, and the |
| 229 | fprintf below needs to know what port was selected. */ |
| 230 | if (port == 0) |
| 231 | { |
| 232 | socklen_t len = sizeof (sockaddr); |
| 233 | if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0 |
| 234 | || len < sizeof (sockaddr)) |
| 235 | perror_with_name ("Can't determine port"); |
| 236 | port = ntohs (sockaddr.sin_port); |
| 237 | } |
| 238 | |
| 239 | fprintf (stderr, "Listening on port %d\n", port); |
| 240 | fflush (stderr); |
| 241 | |
| 242 | tmp = sizeof (sockaddr); |
| 243 | remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp); |
| 244 | if (remote_desc == -1) |
| 245 | perror_with_name ("Accept failed"); |
| 246 | |
| 247 | /* Enable TCP keep alive process. */ |
| 248 | tmp = 1; |
| 249 | setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, |
| 250 | (char *) &tmp, sizeof (tmp)); |
| 251 | |
| 252 | /* Tell TCP not to delay small packets. This greatly speeds up |
| 253 | interactive response. */ |
| 254 | tmp = 1; |
| 255 | setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY, |
| 256 | (char *) &tmp, sizeof (tmp)); |
| 257 | |
| 258 | |
| 259 | #ifndef USE_WIN32API |
| 260 | close (tmp_desc); /* No longer need this */ |
| 261 | |
| 262 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply |
| 263 | exits when the remote side dies. */ |
| 264 | #else |
| 265 | closesocket (tmp_desc); /* No longer need this */ |
| 266 | #endif |
| 267 | |
| 268 | /* Convert IP address to string. */ |
| 269 | fprintf (stderr, "Remote debugging from host %s\n", |
| 270 | inet_ntoa (sockaddr.sin_addr)); |
| 271 | } |
| 272 | |
| 273 | #if defined(F_SETFL) && defined (FASYNC) |
| 274 | save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0); |
| 275 | fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC); |
| 276 | #if defined (F_SETOWN) |
| 277 | fcntl (remote_desc, F_SETOWN, getpid ()); |
| 278 | #endif |
| 279 | #endif |
| 280 | disable_async_io (); |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | remote_close (void) |
| 285 | { |
| 286 | #ifdef USE_WIN32API |
| 287 | closesocket (remote_desc); |
| 288 | #else |
| 289 | close (remote_desc); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | /* Convert hex digit A to a number. */ |
| 294 | |
| 295 | static int |
| 296 | fromhex (int a) |
| 297 | { |
| 298 | if (a >= '0' && a <= '9') |
| 299 | return a - '0'; |
| 300 | else if (a >= 'a' && a <= 'f') |
| 301 | return a - 'a' + 10; |
| 302 | else |
| 303 | error ("Reply contains invalid hex digit"); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int |
| 308 | unhexify (char *bin, const char *hex, int count) |
| 309 | { |
| 310 | int i; |
| 311 | |
| 312 | for (i = 0; i < count; i++) |
| 313 | { |
| 314 | if (hex[0] == 0 || hex[1] == 0) |
| 315 | { |
| 316 | /* Hex string is short, or of uneven length. |
| 317 | Return the count that has been converted so far. */ |
| 318 | return i; |
| 319 | } |
| 320 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); |
| 321 | hex += 2; |
| 322 | } |
| 323 | return i; |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | decode_address (CORE_ADDR *addrp, const char *start, int len) |
| 328 | { |
| 329 | CORE_ADDR addr; |
| 330 | char ch; |
| 331 | int i; |
| 332 | |
| 333 | addr = 0; |
| 334 | for (i = 0; i < len; i++) |
| 335 | { |
| 336 | ch = start[i]; |
| 337 | addr = addr << 4; |
| 338 | addr = addr | (fromhex (ch) & 0x0f); |
| 339 | } |
| 340 | *addrp = addr; |
| 341 | } |
| 342 | |
| 343 | const char * |
| 344 | decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) |
| 345 | { |
| 346 | const char *end; |
| 347 | |
| 348 | end = start; |
| 349 | while (*end != '\0' && *end != ';') |
| 350 | end++; |
| 351 | |
| 352 | decode_address (addrp, start, end - start); |
| 353 | |
| 354 | if (*end == ';') |
| 355 | end++; |
| 356 | return end; |
| 357 | } |
| 358 | |
| 359 | /* Convert number NIB to a hex digit. */ |
| 360 | |
| 361 | static int |
| 362 | tohex (int nib) |
| 363 | { |
| 364 | if (nib < 10) |
| 365 | return '0' + nib; |
| 366 | else |
| 367 | return 'a' + nib - 10; |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | hexify (char *hex, const char *bin, int count) |
| 372 | { |
| 373 | int i; |
| 374 | |
| 375 | /* May use a length, or a nul-terminated string as input. */ |
| 376 | if (count == 0) |
| 377 | count = strlen (bin); |
| 378 | |
| 379 | for (i = 0; i < count; i++) |
| 380 | { |
| 381 | *hex++ = tohex ((*bin >> 4) & 0xf); |
| 382 | *hex++ = tohex (*bin++ & 0xf); |
| 383 | } |
| 384 | *hex = 0; |
| 385 | return i; |
| 386 | } |
| 387 | |
| 388 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
| 389 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data |
| 390 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF |
| 391 | (which may be more than *OUT_LEN due to escape characters). The |
| 392 | total number of bytes in the output buffer will be at most |
| 393 | OUT_MAXLEN. */ |
| 394 | |
| 395 | int |
| 396 | remote_escape_output (const gdb_byte *buffer, int len, |
| 397 | gdb_byte *out_buf, int *out_len, |
| 398 | int out_maxlen) |
| 399 | { |
| 400 | int input_index, output_index; |
| 401 | |
| 402 | output_index = 0; |
| 403 | for (input_index = 0; input_index < len; input_index++) |
| 404 | { |
| 405 | gdb_byte b = buffer[input_index]; |
| 406 | |
| 407 | if (b == '$' || b == '#' || b == '}' || b == '*') |
| 408 | { |
| 409 | /* These must be escaped. */ |
| 410 | if (output_index + 2 > out_maxlen) |
| 411 | break; |
| 412 | out_buf[output_index++] = '}'; |
| 413 | out_buf[output_index++] = b ^ 0x20; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | if (output_index + 1 > out_maxlen) |
| 418 | break; |
| 419 | out_buf[output_index++] = b; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | *out_len = input_index; |
| 424 | return output_index; |
| 425 | } |
| 426 | |
| 427 | /* Convert BUFFER, escaped data LEN bytes long, into binary data |
| 428 | in OUT_BUF. Return the number of bytes written to OUT_BUF. |
| 429 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. |
| 430 | |
| 431 | This function reverses remote_escape_output. It allows more |
| 432 | escaped characters than that function does, in particular because |
| 433 | '*' must be escaped to avoid the run-length encoding processing |
| 434 | in reading packets. */ |
| 435 | |
| 436 | static int |
| 437 | remote_unescape_input (const gdb_byte *buffer, int len, |
| 438 | gdb_byte *out_buf, int out_maxlen) |
| 439 | { |
| 440 | int input_index, output_index; |
| 441 | int escaped; |
| 442 | |
| 443 | output_index = 0; |
| 444 | escaped = 0; |
| 445 | for (input_index = 0; input_index < len; input_index++) |
| 446 | { |
| 447 | gdb_byte b = buffer[input_index]; |
| 448 | |
| 449 | if (output_index + 1 > out_maxlen) |
| 450 | error ("Received too much data from the target."); |
| 451 | |
| 452 | if (escaped) |
| 453 | { |
| 454 | out_buf[output_index++] = b ^ 0x20; |
| 455 | escaped = 0; |
| 456 | } |
| 457 | else if (b == '}') |
| 458 | escaped = 1; |
| 459 | else |
| 460 | out_buf[output_index++] = b; |
| 461 | } |
| 462 | |
| 463 | if (escaped) |
| 464 | error ("Unmatched escape character in target response."); |
| 465 | |
| 466 | return output_index; |
| 467 | } |
| 468 | |
| 469 | /* Look for a sequence of characters which can be run-length encoded. |
| 470 | If there are any, update *CSUM and *P. Otherwise, output the |
| 471 | single character. Return the number of characters consumed. */ |
| 472 | |
| 473 | static int |
| 474 | try_rle (char *buf, int remaining, unsigned char *csum, char **p) |
| 475 | { |
| 476 | int n; |
| 477 | |
| 478 | /* Always output the character. */ |
| 479 | *csum += buf[0]; |
| 480 | *(*p)++ = buf[0]; |
| 481 | |
| 482 | /* Don't go past '~'. */ |
| 483 | if (remaining > 97) |
| 484 | remaining = 97; |
| 485 | |
| 486 | for (n = 1; n < remaining; n++) |
| 487 | if (buf[n] != buf[0]) |
| 488 | break; |
| 489 | |
| 490 | /* N is the index of the first character not the same as buf[0]. |
| 491 | buf[0] is counted twice, so by decrementing N, we get the number |
| 492 | of characters the RLE sequence will replace. */ |
| 493 | n--; |
| 494 | |
| 495 | if (n < 3) |
| 496 | return 1; |
| 497 | |
| 498 | /* Skip the frame characters. The manual says to skip '+' and '-' |
| 499 | also, but there's no reason to. Unfortunately these two unusable |
| 500 | characters double the encoded length of a four byte zero |
| 501 | value. */ |
| 502 | while (n + 29 == '$' || n + 29 == '#') |
| 503 | n--; |
| 504 | |
| 505 | *csum += '*'; |
| 506 | *(*p)++ = '*'; |
| 507 | *csum += n + 29; |
| 508 | *(*p)++ = n + 29; |
| 509 | |
| 510 | return n + 1; |
| 511 | } |
| 512 | |
| 513 | /* Send a packet to the remote machine, with error checking. |
| 514 | The data of the packet is in BUF, and the length of the |
| 515 | packet is in CNT. Returns >= 0 on success, -1 otherwise. */ |
| 516 | |
| 517 | int |
| 518 | putpkt_binary (char *buf, int cnt) |
| 519 | { |
| 520 | int i; |
| 521 | unsigned char csum = 0; |
| 522 | char *buf2; |
| 523 | char buf3[1]; |
| 524 | char *p; |
| 525 | |
| 526 | buf2 = malloc (PBUFSIZ); |
| 527 | |
| 528 | /* Copy the packet into buffer BUF2, encapsulating it |
| 529 | and giving it a checksum. */ |
| 530 | |
| 531 | p = buf2; |
| 532 | *p++ = '$'; |
| 533 | |
| 534 | for (i = 0; i < cnt;) |
| 535 | i += try_rle (buf + i, cnt - i, &csum, &p); |
| 536 | |
| 537 | *p++ = '#'; |
| 538 | *p++ = tohex ((csum >> 4) & 0xf); |
| 539 | *p++ = tohex (csum & 0xf); |
| 540 | |
| 541 | *p = '\0'; |
| 542 | |
| 543 | /* Send it over and over until we get a positive ack. */ |
| 544 | |
| 545 | do |
| 546 | { |
| 547 | int cc; |
| 548 | |
| 549 | if (write (remote_desc, buf2, p - buf2) != p - buf2) |
| 550 | { |
| 551 | perror ("putpkt(write)"); |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | if (remote_debug) |
| 556 | { |
| 557 | fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2); |
| 558 | fflush (stderr); |
| 559 | } |
| 560 | cc = read (remote_desc, buf3, 1); |
| 561 | if (remote_debug) |
| 562 | { |
| 563 | fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]); |
| 564 | fflush (stderr); |
| 565 | } |
| 566 | |
| 567 | if (cc <= 0) |
| 568 | { |
| 569 | if (cc == 0) |
| 570 | fprintf (stderr, "putpkt(read): Got EOF\n"); |
| 571 | else |
| 572 | perror ("putpkt(read)"); |
| 573 | |
| 574 | free (buf2); |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | /* Check for an input interrupt while we're here. */ |
| 579 | if (buf3[0] == '\003') |
| 580 | (*the_target->request_interrupt) (); |
| 581 | } |
| 582 | while (buf3[0] != '+'); |
| 583 | |
| 584 | free (buf2); |
| 585 | return 1; /* Success! */ |
| 586 | } |
| 587 | |
| 588 | /* Send a packet to the remote machine, with error checking. The data |
| 589 | of the packet is in BUF, and the packet should be a NUL-terminated |
| 590 | string. Returns >= 0 on success, -1 otherwise. */ |
| 591 | |
| 592 | int |
| 593 | putpkt (char *buf) |
| 594 | { |
| 595 | return putpkt_binary (buf, strlen (buf)); |
| 596 | } |
| 597 | |
| 598 | /* Come here when we get an input interrupt from the remote side. This |
| 599 | interrupt should only be active while we are waiting for the child to do |
| 600 | something. About the only thing that should come through is a ^C, which |
| 601 | will cause us to request child interruption. */ |
| 602 | |
| 603 | static void |
| 604 | input_interrupt (int unused) |
| 605 | { |
| 606 | fd_set readset; |
| 607 | struct timeval immediate = { 0, 0 }; |
| 608 | |
| 609 | /* Protect against spurious interrupts. This has been observed to |
| 610 | be a problem under NetBSD 1.4 and 1.5. */ |
| 611 | |
| 612 | FD_ZERO (&readset); |
| 613 | FD_SET (remote_desc, &readset); |
| 614 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) |
| 615 | { |
| 616 | int cc; |
| 617 | char c = 0; |
| 618 | |
| 619 | cc = read (remote_desc, &c, 1); |
| 620 | |
| 621 | if (cc != 1 || c != '\003') |
| 622 | { |
| 623 | fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n", |
| 624 | cc, c, c); |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | (*the_target->request_interrupt) (); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /* Check if the remote side sent us an interrupt request (^C). */ |
| 633 | void |
| 634 | check_remote_input_interrupt_request (void) |
| 635 | { |
| 636 | /* This function may be called before establishing communications, |
| 637 | therefore we need to validate the remote descriptor. */ |
| 638 | |
| 639 | if (remote_desc == INVALID_DESCRIPTOR) |
| 640 | return; |
| 641 | |
| 642 | input_interrupt (0); |
| 643 | } |
| 644 | |
| 645 | /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to |
| 646 | accept Control-C from the client, and must be disabled when talking to |
| 647 | the client. */ |
| 648 | |
| 649 | void |
| 650 | block_async_io (void) |
| 651 | { |
| 652 | #ifndef USE_WIN32API |
| 653 | sigset_t sigio_set; |
| 654 | sigemptyset (&sigio_set); |
| 655 | sigaddset (&sigio_set, SIGIO); |
| 656 | sigprocmask (SIG_BLOCK, &sigio_set, NULL); |
| 657 | #endif |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | unblock_async_io (void) |
| 662 | { |
| 663 | #ifndef USE_WIN32API |
| 664 | sigset_t sigio_set; |
| 665 | sigemptyset (&sigio_set); |
| 666 | sigaddset (&sigio_set, SIGIO); |
| 667 | sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | /* Current state of asynchronous I/O. */ |
| 672 | static int async_io_enabled; |
| 673 | |
| 674 | /* Enable asynchronous I/O. */ |
| 675 | void |
| 676 | enable_async_io (void) |
| 677 | { |
| 678 | if (async_io_enabled) |
| 679 | return; |
| 680 | |
| 681 | #ifndef USE_WIN32API |
| 682 | signal (SIGIO, input_interrupt); |
| 683 | #endif |
| 684 | async_io_enabled = 1; |
| 685 | } |
| 686 | |
| 687 | /* Disable asynchronous I/O. */ |
| 688 | void |
| 689 | disable_async_io (void) |
| 690 | { |
| 691 | if (!async_io_enabled) |
| 692 | return; |
| 693 | |
| 694 | #ifndef USE_WIN32API |
| 695 | signal (SIGIO, SIG_IGN); |
| 696 | #endif |
| 697 | async_io_enabled = 0; |
| 698 | } |
| 699 | |
| 700 | /* Returns next char from remote GDB. -1 if error. */ |
| 701 | |
| 702 | static int |
| 703 | readchar (void) |
| 704 | { |
| 705 | static unsigned char buf[BUFSIZ]; |
| 706 | static int bufcnt = 0; |
| 707 | static unsigned char *bufp; |
| 708 | |
| 709 | if (bufcnt-- > 0) |
| 710 | return *bufp++; |
| 711 | |
| 712 | bufcnt = read (remote_desc, buf, sizeof (buf)); |
| 713 | |
| 714 | if (bufcnt <= 0) |
| 715 | { |
| 716 | if (bufcnt == 0) |
| 717 | fprintf (stderr, "readchar: Got EOF\n"); |
| 718 | else |
| 719 | perror ("readchar"); |
| 720 | |
| 721 | return -1; |
| 722 | } |
| 723 | |
| 724 | bufp = buf; |
| 725 | bufcnt--; |
| 726 | return *bufp++ & 0x7f; |
| 727 | } |
| 728 | |
| 729 | /* Read a packet from the remote machine, with error checking, |
| 730 | and store it in BUF. Returns length of packet, or negative if error. */ |
| 731 | |
| 732 | int |
| 733 | getpkt (char *buf) |
| 734 | { |
| 735 | char *bp; |
| 736 | unsigned char csum, c1, c2; |
| 737 | int c; |
| 738 | |
| 739 | while (1) |
| 740 | { |
| 741 | csum = 0; |
| 742 | |
| 743 | while (1) |
| 744 | { |
| 745 | c = readchar (); |
| 746 | if (c == '$') |
| 747 | break; |
| 748 | if (remote_debug) |
| 749 | { |
| 750 | fprintf (stderr, "[getpkt: discarding char '%c']\n", c); |
| 751 | fflush (stderr); |
| 752 | } |
| 753 | |
| 754 | if (c < 0) |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | bp = buf; |
| 759 | while (1) |
| 760 | { |
| 761 | c = readchar (); |
| 762 | if (c < 0) |
| 763 | return -1; |
| 764 | if (c == '#') |
| 765 | break; |
| 766 | *bp++ = c; |
| 767 | csum += c; |
| 768 | } |
| 769 | *bp = 0; |
| 770 | |
| 771 | c1 = fromhex (readchar ()); |
| 772 | c2 = fromhex (readchar ()); |
| 773 | |
| 774 | if (csum == (c1 << 4) + c2) |
| 775 | break; |
| 776 | |
| 777 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
| 778 | (c1 << 4) + c2, csum, buf); |
| 779 | write (remote_desc, "-", 1); |
| 780 | } |
| 781 | |
| 782 | if (remote_debug) |
| 783 | { |
| 784 | fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); |
| 785 | fflush (stderr); |
| 786 | } |
| 787 | |
| 788 | write (remote_desc, "+", 1); |
| 789 | |
| 790 | if (remote_debug) |
| 791 | { |
| 792 | fprintf (stderr, "[sent ack]\n"); |
| 793 | fflush (stderr); |
| 794 | } |
| 795 | |
| 796 | return bp - buf; |
| 797 | } |
| 798 | |
| 799 | void |
| 800 | write_ok (char *buf) |
| 801 | { |
| 802 | buf[0] = 'O'; |
| 803 | buf[1] = 'K'; |
| 804 | buf[2] = '\0'; |
| 805 | } |
| 806 | |
| 807 | void |
| 808 | write_enn (char *buf) |
| 809 | { |
| 810 | /* Some day, we should define the meanings of the error codes... */ |
| 811 | buf[0] = 'E'; |
| 812 | buf[1] = '0'; |
| 813 | buf[2] = '1'; |
| 814 | buf[3] = '\0'; |
| 815 | } |
| 816 | |
| 817 | void |
| 818 | convert_int_to_ascii (unsigned char *from, char *to, int n) |
| 819 | { |
| 820 | int nib; |
| 821 | int ch; |
| 822 | while (n--) |
| 823 | { |
| 824 | ch = *from++; |
| 825 | nib = ((ch & 0xf0) >> 4) & 0x0f; |
| 826 | *to++ = tohex (nib); |
| 827 | nib = ch & 0x0f; |
| 828 | *to++ = tohex (nib); |
| 829 | } |
| 830 | *to++ = 0; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | void |
| 835 | convert_ascii_to_int (char *from, unsigned char *to, int n) |
| 836 | { |
| 837 | int nib1, nib2; |
| 838 | while (n--) |
| 839 | { |
| 840 | nib1 = fromhex (*from++); |
| 841 | nib2 = fromhex (*from++); |
| 842 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static char * |
| 847 | outreg (int regno, char *buf) |
| 848 | { |
| 849 | if ((regno >> 12) != 0) |
| 850 | *buf++ = tohex ((regno >> 12) & 0xf); |
| 851 | if ((regno >> 8) != 0) |
| 852 | *buf++ = tohex ((regno >> 8) & 0xf); |
| 853 | *buf++ = tohex ((regno >> 4) & 0xf); |
| 854 | *buf++ = tohex (regno & 0xf); |
| 855 | *buf++ = ':'; |
| 856 | collect_register_as_string (regno, buf); |
| 857 | buf += 2 * register_size (regno); |
| 858 | *buf++ = ';'; |
| 859 | |
| 860 | return buf; |
| 861 | } |
| 862 | |
| 863 | void |
| 864 | new_thread_notify (int id) |
| 865 | { |
| 866 | char own_buf[256]; |
| 867 | |
| 868 | /* The `n' response is not yet part of the remote protocol. Do nothing. */ |
| 869 | if (1) |
| 870 | return; |
| 871 | |
| 872 | if (server_waiting == 0) |
| 873 | return; |
| 874 | |
| 875 | sprintf (own_buf, "n%x", id); |
| 876 | disable_async_io (); |
| 877 | putpkt (own_buf); |
| 878 | enable_async_io (); |
| 879 | } |
| 880 | |
| 881 | void |
| 882 | dead_thread_notify (int id) |
| 883 | { |
| 884 | char own_buf[256]; |
| 885 | |
| 886 | /* The `x' response is not yet part of the remote protocol. Do nothing. */ |
| 887 | if (1) |
| 888 | return; |
| 889 | |
| 890 | sprintf (own_buf, "x%x", id); |
| 891 | disable_async_io (); |
| 892 | putpkt (own_buf); |
| 893 | enable_async_io (); |
| 894 | } |
| 895 | |
| 896 | void |
| 897 | prepare_resume_reply (char *buf, char status, unsigned char sig) |
| 898 | { |
| 899 | int nib; |
| 900 | |
| 901 | *buf++ = status; |
| 902 | |
| 903 | nib = ((sig & 0xf0) >> 4); |
| 904 | *buf++ = tohex (nib); |
| 905 | nib = sig & 0x0f; |
| 906 | *buf++ = tohex (nib); |
| 907 | |
| 908 | if (status == 'T') |
| 909 | { |
| 910 | const char **regp = gdbserver_expedite_regs; |
| 911 | |
| 912 | if (the_target->stopped_by_watchpoint != NULL |
| 913 | && (*the_target->stopped_by_watchpoint) ()) |
| 914 | { |
| 915 | CORE_ADDR addr; |
| 916 | int i; |
| 917 | |
| 918 | strncpy (buf, "watch:", 6); |
| 919 | buf += 6; |
| 920 | |
| 921 | addr = (*the_target->stopped_data_address) (); |
| 922 | |
| 923 | /* Convert each byte of the address into two hexadecimal chars. |
| 924 | Note that we take sizeof (void *) instead of sizeof (addr); |
| 925 | this is to avoid sending a 64-bit address to a 32-bit GDB. */ |
| 926 | for (i = sizeof (void *) * 2; i > 0; i--) |
| 927 | { |
| 928 | *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); |
| 929 | } |
| 930 | *buf++ = ';'; |
| 931 | } |
| 932 | |
| 933 | while (*regp) |
| 934 | { |
| 935 | buf = outreg (find_regno (*regp), buf); |
| 936 | regp ++; |
| 937 | } |
| 938 | |
| 939 | /* Formerly, if the debugger had not used any thread features we would not |
| 940 | burden it with a thread status response. This was for the benefit of |
| 941 | GDB 4.13 and older. However, in recent GDB versions the check |
| 942 | (``if (cont_thread != 0)'') does not have the desired effect because of |
| 943 | sillyness in the way that the remote protocol handles specifying a thread. |
| 944 | Since thread support relies on qSymbol support anyway, assume GDB can handle |
| 945 | threads. */ |
| 946 | |
| 947 | if (using_threads) |
| 948 | { |
| 949 | unsigned int gdb_id_from_wait; |
| 950 | |
| 951 | /* FIXME right place to set this? */ |
| 952 | thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id; |
| 953 | gdb_id_from_wait = thread_to_gdb_id (current_inferior); |
| 954 | |
| 955 | if (debug_threads) |
| 956 | fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait); |
| 957 | /* This if (1) ought to be unnecessary. But remote_wait in GDB |
| 958 | will claim this event belongs to inferior_ptid if we do not |
| 959 | specify a thread, and there's no way for gdbserver to know |
| 960 | what inferior_ptid is. */ |
| 961 | if (1 || old_thread_from_wait != thread_from_wait) |
| 962 | { |
| 963 | general_thread = thread_from_wait; |
| 964 | sprintf (buf, "thread:%x;", gdb_id_from_wait); |
| 965 | buf += strlen (buf); |
| 966 | old_thread_from_wait = thread_from_wait; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (dlls_changed) |
| 971 | { |
| 972 | strcpy (buf, "library:;"); |
| 973 | buf += strlen (buf); |
| 974 | dlls_changed = 0; |
| 975 | } |
| 976 | } |
| 977 | /* For W and X, we're done. */ |
| 978 | *buf++ = 0; |
| 979 | } |
| 980 | |
| 981 | void |
| 982 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
| 983 | { |
| 984 | int i = 0, j = 0; |
| 985 | char ch; |
| 986 | *mem_addr_ptr = *len_ptr = 0; |
| 987 | |
| 988 | while ((ch = from[i++]) != ',') |
| 989 | { |
| 990 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 991 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 992 | } |
| 993 | |
| 994 | for (j = 0; j < 4; j++) |
| 995 | { |
| 996 | if ((ch = from[i++]) == 0) |
| 997 | break; |
| 998 | *len_ptr = *len_ptr << 4; |
| 999 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | void |
| 1004 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
| 1005 | unsigned char *to) |
| 1006 | { |
| 1007 | int i = 0; |
| 1008 | char ch; |
| 1009 | *mem_addr_ptr = *len_ptr = 0; |
| 1010 | |
| 1011 | while ((ch = from[i++]) != ',') |
| 1012 | { |
| 1013 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 1014 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 1015 | } |
| 1016 | |
| 1017 | while ((ch = from[i++]) != ':') |
| 1018 | { |
| 1019 | *len_ptr = *len_ptr << 4; |
| 1020 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1021 | } |
| 1022 | |
| 1023 | convert_ascii_to_int (&from[i++], to, *len_ptr); |
| 1024 | } |
| 1025 | |
| 1026 | int |
| 1027 | decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, |
| 1028 | unsigned int *len_ptr, unsigned char *to) |
| 1029 | { |
| 1030 | int i = 0; |
| 1031 | char ch; |
| 1032 | *mem_addr_ptr = *len_ptr = 0; |
| 1033 | |
| 1034 | while ((ch = from[i++]) != ',') |
| 1035 | { |
| 1036 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 1037 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 1038 | } |
| 1039 | |
| 1040 | while ((ch = from[i++]) != ':') |
| 1041 | { |
| 1042 | *len_ptr = *len_ptr << 4; |
| 1043 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1044 | } |
| 1045 | |
| 1046 | if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, |
| 1047 | to, *len_ptr) != *len_ptr) |
| 1048 | return -1; |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | /* Decode a qXfer write request. */ |
| 1054 | int |
| 1055 | decode_xfer_write (char *buf, int packet_len, char **annex, CORE_ADDR *offset, |
| 1056 | unsigned int *len, unsigned char *data) |
| 1057 | { |
| 1058 | char ch; |
| 1059 | |
| 1060 | /* Extract and NUL-terminate the annex. */ |
| 1061 | *annex = buf; |
| 1062 | while (*buf && *buf != ':') |
| 1063 | buf++; |
| 1064 | if (*buf == '\0') |
| 1065 | return -1; |
| 1066 | *buf++ = 0; |
| 1067 | |
| 1068 | /* Extract the offset. */ |
| 1069 | *offset = 0; |
| 1070 | while ((ch = *buf++) != ':') |
| 1071 | { |
| 1072 | *offset = *offset << 4; |
| 1073 | *offset |= fromhex (ch) & 0x0f; |
| 1074 | } |
| 1075 | |
| 1076 | /* Get encoded data. */ |
| 1077 | packet_len -= buf - *annex; |
| 1078 | *len = remote_unescape_input ((const gdb_byte *) buf, packet_len, |
| 1079 | data, packet_len); |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | /* Ask GDB for the address of NAME, and return it in ADDRP if found. |
| 1084 | Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ |
| 1085 | |
| 1086 | int |
| 1087 | look_up_one_symbol (const char *name, CORE_ADDR *addrp) |
| 1088 | { |
| 1089 | char own_buf[266], *p, *q; |
| 1090 | int len; |
| 1091 | struct sym_cache *sym; |
| 1092 | |
| 1093 | /* Check the cache first. */ |
| 1094 | for (sym = symbol_cache; sym; sym = sym->next) |
| 1095 | if (strcmp (name, sym->name) == 0) |
| 1096 | { |
| 1097 | *addrp = sym->addr; |
| 1098 | return 1; |
| 1099 | } |
| 1100 | |
| 1101 | /* If we've passed the call to thread_db_look_up_symbols, then |
| 1102 | anything not in the cache must not exist; we're not interested |
| 1103 | in any libraries loaded after that point, only in symbols in |
| 1104 | libpthread.so. It might not be an appropriate time to look |
| 1105 | up a symbol, e.g. while we're trying to fetch registers. */ |
| 1106 | if (all_symbols_looked_up) |
| 1107 | return 0; |
| 1108 | |
| 1109 | /* Send the request. */ |
| 1110 | strcpy (own_buf, "qSymbol:"); |
| 1111 | hexify (own_buf + strlen ("qSymbol:"), name, strlen (name)); |
| 1112 | if (putpkt (own_buf) < 0) |
| 1113 | return -1; |
| 1114 | |
| 1115 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ |
| 1116 | len = getpkt (own_buf); |
| 1117 | if (len < 0) |
| 1118 | return -1; |
| 1119 | |
| 1120 | /* We ought to handle pretty much any packet at this point while we |
| 1121 | wait for the qSymbol "response". That requires re-entering the |
| 1122 | main loop. For now, this is an adequate approximation; allow |
| 1123 | GDB to read from memory while it figures out the address of the |
| 1124 | symbol. */ |
| 1125 | while (own_buf[0] == 'm') |
| 1126 | { |
| 1127 | CORE_ADDR mem_addr; |
| 1128 | unsigned char *mem_buf; |
| 1129 | unsigned int mem_len; |
| 1130 | |
| 1131 | decode_m_packet (&own_buf[1], &mem_addr, &mem_len); |
| 1132 | mem_buf = malloc (mem_len); |
| 1133 | if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) |
| 1134 | convert_int_to_ascii (mem_buf, own_buf, mem_len); |
| 1135 | else |
| 1136 | write_enn (own_buf); |
| 1137 | free (mem_buf); |
| 1138 | if (putpkt (own_buf) < 0) |
| 1139 | return -1; |
| 1140 | len = getpkt (own_buf); |
| 1141 | if (len < 0) |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0) |
| 1146 | { |
| 1147 | warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf); |
| 1148 | return -1; |
| 1149 | } |
| 1150 | |
| 1151 | p = own_buf + strlen ("qSymbol:"); |
| 1152 | q = p; |
| 1153 | while (*q && *q != ':') |
| 1154 | q++; |
| 1155 | |
| 1156 | /* Make sure we found a value for the symbol. */ |
| 1157 | if (p == q || *q == '\0') |
| 1158 | return 0; |
| 1159 | |
| 1160 | decode_address (addrp, p, q - p); |
| 1161 | |
| 1162 | /* Save the symbol in our cache. */ |
| 1163 | sym = malloc (sizeof (*sym)); |
| 1164 | sym->name = strdup (name); |
| 1165 | sym->addr = *addrp; |
| 1166 | sym->next = symbol_cache; |
| 1167 | symbol_cache = sym; |
| 1168 | |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
| 1172 | void |
| 1173 | monitor_output (const char *msg) |
| 1174 | { |
| 1175 | char *buf = malloc (strlen (msg) * 2 + 2); |
| 1176 | |
| 1177 | buf[0] = 'O'; |
| 1178 | hexify (buf + 1, msg, 0); |
| 1179 | |
| 1180 | putpkt (buf); |
| 1181 | free (buf); |
| 1182 | } |
| 1183 | |
| 1184 | /* Return a malloc allocated string with special characters from TEXT |
| 1185 | replaced by entity references. */ |
| 1186 | |
| 1187 | char * |
| 1188 | xml_escape_text (const char *text) |
| 1189 | { |
| 1190 | char *result; |
| 1191 | int i, special; |
| 1192 | |
| 1193 | /* Compute the length of the result. */ |
| 1194 | for (i = 0, special = 0; text[i] != '\0'; i++) |
| 1195 | switch (text[i]) |
| 1196 | { |
| 1197 | case '\'': |
| 1198 | case '\"': |
| 1199 | special += 5; |
| 1200 | break; |
| 1201 | case '&': |
| 1202 | special += 4; |
| 1203 | break; |
| 1204 | case '<': |
| 1205 | case '>': |
| 1206 | special += 3; |
| 1207 | break; |
| 1208 | default: |
| 1209 | break; |
| 1210 | } |
| 1211 | |
| 1212 | /* Expand the result. */ |
| 1213 | result = malloc (i + special + 1); |
| 1214 | for (i = 0, special = 0; text[i] != '\0'; i++) |
| 1215 | switch (text[i]) |
| 1216 | { |
| 1217 | case '\'': |
| 1218 | strcpy (result + i + special, "'"); |
| 1219 | special += 5; |
| 1220 | break; |
| 1221 | case '\"': |
| 1222 | strcpy (result + i + special, """); |
| 1223 | special += 5; |
| 1224 | break; |
| 1225 | case '&': |
| 1226 | strcpy (result + i + special, "&"); |
| 1227 | special += 4; |
| 1228 | break; |
| 1229 | case '<': |
| 1230 | strcpy (result + i + special, "<"); |
| 1231 | special += 3; |
| 1232 | break; |
| 1233 | case '>': |
| 1234 | strcpy (result + i + special, ">"); |
| 1235 | special += 3; |
| 1236 | break; |
| 1237 | default: |
| 1238 | result[i + special] = text[i]; |
| 1239 | break; |
| 1240 | } |
| 1241 | result[i + special] = '\0'; |
| 1242 | |
| 1243 | return result; |
| 1244 | } |