| 1 | /* Remote utility routines for the remote server for GDB. |
| 2 | Copyright (C) 1986-2015 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 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 |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 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. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include "server.h" |
| 20 | #include "terminal.h" |
| 21 | #include "target.h" |
| 22 | #include "gdbthread.h" |
| 23 | #include "tdesc.h" |
| 24 | #include "dll.h" |
| 25 | #include "rsp-low.h" |
| 26 | #include <ctype.h> |
| 27 | #if HAVE_SYS_IOCTL_H |
| 28 | #include <sys/ioctl.h> |
| 29 | #endif |
| 30 | #if HAVE_SYS_FILE_H |
| 31 | #include <sys/file.h> |
| 32 | #endif |
| 33 | #if HAVE_NETINET_IN_H |
| 34 | #include <netinet/in.h> |
| 35 | #endif |
| 36 | #if HAVE_SYS_SOCKET_H |
| 37 | #include <sys/socket.h> |
| 38 | #endif |
| 39 | #if HAVE_NETDB_H |
| 40 | #include <netdb.h> |
| 41 | #endif |
| 42 | #if HAVE_NETINET_TCP_H |
| 43 | #include <netinet/tcp.h> |
| 44 | #endif |
| 45 | #if HAVE_SYS_IOCTL_H |
| 46 | #include <sys/ioctl.h> |
| 47 | #endif |
| 48 | #if HAVE_SIGNAL_H |
| 49 | #include <signal.h> |
| 50 | #endif |
| 51 | #if HAVE_FCNTL_H |
| 52 | #include <fcntl.h> |
| 53 | #endif |
| 54 | #include "gdb_sys_time.h" |
| 55 | #include <unistd.h> |
| 56 | #if HAVE_ARPA_INET_H |
| 57 | #include <arpa/inet.h> |
| 58 | #endif |
| 59 | #include <sys/stat.h> |
| 60 | |
| 61 | #if USE_WIN32API |
| 62 | #include <winsock2.h> |
| 63 | #endif |
| 64 | |
| 65 | #if __QNX__ |
| 66 | #include <sys/iomgr.h> |
| 67 | #endif /* __QNX__ */ |
| 68 | |
| 69 | #ifndef HAVE_SOCKLEN_T |
| 70 | typedef int socklen_t; |
| 71 | #endif |
| 72 | |
| 73 | #ifndef IN_PROCESS_AGENT |
| 74 | |
| 75 | #if USE_WIN32API |
| 76 | # define INVALID_DESCRIPTOR INVALID_SOCKET |
| 77 | #else |
| 78 | # define INVALID_DESCRIPTOR -1 |
| 79 | #endif |
| 80 | |
| 81 | /* Extra value for readchar_callback. */ |
| 82 | enum { |
| 83 | /* The callback is currently not scheduled. */ |
| 84 | NOT_SCHEDULED = -1 |
| 85 | }; |
| 86 | |
| 87 | /* Status of the readchar callback. |
| 88 | Either NOT_SCHEDULED or the callback id. */ |
| 89 | static int readchar_callback = NOT_SCHEDULED; |
| 90 | |
| 91 | static int readchar (void); |
| 92 | static void reset_readchar (void); |
| 93 | static void reschedule (void); |
| 94 | |
| 95 | /* A cache entry for a successfully looked-up symbol. */ |
| 96 | struct sym_cache |
| 97 | { |
| 98 | char *name; |
| 99 | CORE_ADDR addr; |
| 100 | struct sym_cache *next; |
| 101 | }; |
| 102 | |
| 103 | int remote_debug = 0; |
| 104 | struct ui_file *gdb_stdlog; |
| 105 | |
| 106 | static int remote_is_stdio = 0; |
| 107 | |
| 108 | static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR; |
| 109 | static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR; |
| 110 | |
| 111 | /* FIXME headerize? */ |
| 112 | extern int using_threads; |
| 113 | extern int debug_threads; |
| 114 | |
| 115 | /* If true, then GDB has requested noack mode. */ |
| 116 | int noack_mode = 0; |
| 117 | /* If true, then we tell GDB to use noack mode by default. */ |
| 118 | int transport_is_reliable = 0; |
| 119 | |
| 120 | #ifdef USE_WIN32API |
| 121 | # define read(fd, buf, len) recv (fd, (char *) buf, len, 0) |
| 122 | # define write(fd, buf, len) send (fd, (char *) buf, len, 0) |
| 123 | #endif |
| 124 | |
| 125 | int |
| 126 | gdb_connected (void) |
| 127 | { |
| 128 | return remote_desc != INVALID_DESCRIPTOR; |
| 129 | } |
| 130 | |
| 131 | /* Return true if the remote connection is over stdio. */ |
| 132 | |
| 133 | int |
| 134 | remote_connection_is_stdio (void) |
| 135 | { |
| 136 | return remote_is_stdio; |
| 137 | } |
| 138 | |
| 139 | static void |
| 140 | enable_async_notification (int fd) |
| 141 | { |
| 142 | #if defined(F_SETFL) && defined (FASYNC) |
| 143 | int save_fcntl_flags; |
| 144 | |
| 145 | save_fcntl_flags = fcntl (fd, F_GETFL, 0); |
| 146 | fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC); |
| 147 | #if defined (F_SETOWN) |
| 148 | fcntl (fd, F_SETOWN, getpid ()); |
| 149 | #endif |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | static int |
| 154 | handle_accept_event (int err, gdb_client_data client_data) |
| 155 | { |
| 156 | struct sockaddr_in sockaddr; |
| 157 | socklen_t tmp; |
| 158 | |
| 159 | if (debug_threads) |
| 160 | debug_printf ("handling possible accept event\n"); |
| 161 | |
| 162 | tmp = sizeof (sockaddr); |
| 163 | remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp); |
| 164 | if (remote_desc == -1) |
| 165 | perror_with_name ("Accept failed"); |
| 166 | |
| 167 | /* Enable TCP keep alive process. */ |
| 168 | tmp = 1; |
| 169 | setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, |
| 170 | (char *) &tmp, sizeof (tmp)); |
| 171 | |
| 172 | /* Tell TCP not to delay small packets. This greatly speeds up |
| 173 | interactive response. */ |
| 174 | tmp = 1; |
| 175 | setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY, |
| 176 | (char *) &tmp, sizeof (tmp)); |
| 177 | |
| 178 | #ifndef USE_WIN32API |
| 179 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply |
| 180 | exits when the remote side dies. */ |
| 181 | #endif |
| 182 | |
| 183 | if (run_once) |
| 184 | { |
| 185 | #ifndef USE_WIN32API |
| 186 | close (listen_desc); /* No longer need this */ |
| 187 | #else |
| 188 | closesocket (listen_desc); /* No longer need this */ |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | /* Even if !RUN_ONCE no longer notice new connections. Still keep the |
| 193 | descriptor open for add_file_handler to wait for a new connection. */ |
| 194 | delete_file_handler (listen_desc); |
| 195 | |
| 196 | /* Convert IP address to string. */ |
| 197 | fprintf (stderr, "Remote debugging from host %s\n", |
| 198 | inet_ntoa (sockaddr.sin_addr)); |
| 199 | |
| 200 | enable_async_notification (remote_desc); |
| 201 | |
| 202 | /* Register the event loop handler. */ |
| 203 | add_file_handler (remote_desc, handle_serial_event, NULL); |
| 204 | |
| 205 | /* We have a new GDB connection now. If we were disconnected |
| 206 | tracing, there's a window where the target could report a stop |
| 207 | event to the event loop, and since we have a connection now, we'd |
| 208 | try to send vStopped notifications to GDB. But, don't do that |
| 209 | until GDB as selected all-stop/non-stop, and has queried the |
| 210 | threads' status ('?'). */ |
| 211 | target_async (0); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* Prepare for a later connection to a remote debugger. |
| 217 | NAME is the filename used for communication. */ |
| 218 | |
| 219 | void |
| 220 | remote_prepare (char *name) |
| 221 | { |
| 222 | char *port_str; |
| 223 | #ifdef USE_WIN32API |
| 224 | static int winsock_initialized; |
| 225 | #endif |
| 226 | int port; |
| 227 | struct sockaddr_in sockaddr; |
| 228 | socklen_t tmp; |
| 229 | char *port_end; |
| 230 | |
| 231 | remote_is_stdio = 0; |
| 232 | if (strcmp (name, STDIO_CONNECTION_NAME) == 0) |
| 233 | { |
| 234 | /* We need to record fact that we're using stdio sooner than the |
| 235 | call to remote_open so start_inferior knows the connection is |
| 236 | via stdio. */ |
| 237 | remote_is_stdio = 1; |
| 238 | transport_is_reliable = 1; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | port_str = strchr (name, ':'); |
| 243 | if (port_str == NULL) |
| 244 | { |
| 245 | transport_is_reliable = 0; |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | port = strtoul (port_str + 1, &port_end, 10); |
| 250 | if (port_str[1] == '\0' || *port_end != '\0') |
| 251 | error ("Bad port argument: %s", name); |
| 252 | |
| 253 | #ifdef USE_WIN32API |
| 254 | if (!winsock_initialized) |
| 255 | { |
| 256 | WSADATA wsad; |
| 257 | |
| 258 | WSAStartup (MAKEWORD (1, 0), &wsad); |
| 259 | winsock_initialized = 1; |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 264 | if (listen_desc == -1) |
| 265 | perror_with_name ("Can't open socket"); |
| 266 | |
| 267 | /* Allow rapid reuse of this port. */ |
| 268 | tmp = 1; |
| 269 | setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
| 270 | sizeof (tmp)); |
| 271 | |
| 272 | sockaddr.sin_family = PF_INET; |
| 273 | sockaddr.sin_port = htons (port); |
| 274 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
| 275 | |
| 276 | if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
| 277 | || listen (listen_desc, 1)) |
| 278 | perror_with_name ("Can't bind address"); |
| 279 | |
| 280 | transport_is_reliable = 1; |
| 281 | } |
| 282 | |
| 283 | /* Open a connection to a remote debugger. |
| 284 | NAME is the filename used for communication. */ |
| 285 | |
| 286 | void |
| 287 | remote_open (char *name) |
| 288 | { |
| 289 | char *port_str; |
| 290 | |
| 291 | port_str = strchr (name, ':'); |
| 292 | #ifdef USE_WIN32API |
| 293 | if (port_str == NULL) |
| 294 | error ("Only <host>:<port> is supported on this platform."); |
| 295 | #endif |
| 296 | |
| 297 | if (strcmp (name, STDIO_CONNECTION_NAME) == 0) |
| 298 | { |
| 299 | fprintf (stderr, "Remote debugging using stdio\n"); |
| 300 | |
| 301 | /* Use stdin as the handle of the connection. |
| 302 | We only select on reads, for example. */ |
| 303 | remote_desc = fileno (stdin); |
| 304 | |
| 305 | enable_async_notification (remote_desc); |
| 306 | |
| 307 | /* Register the event loop handler. */ |
| 308 | add_file_handler (remote_desc, handle_serial_event, NULL); |
| 309 | } |
| 310 | #ifndef USE_WIN32API |
| 311 | else if (port_str == NULL) |
| 312 | { |
| 313 | struct stat statbuf; |
| 314 | |
| 315 | if (stat (name, &statbuf) == 0 |
| 316 | && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) |
| 317 | remote_desc = open (name, O_RDWR); |
| 318 | else |
| 319 | { |
| 320 | errno = EINVAL; |
| 321 | remote_desc = -1; |
| 322 | } |
| 323 | |
| 324 | if (remote_desc < 0) |
| 325 | perror_with_name ("Could not open remote device"); |
| 326 | |
| 327 | #ifdef HAVE_TERMIOS |
| 328 | { |
| 329 | struct termios termios; |
| 330 | tcgetattr (remote_desc, &termios); |
| 331 | |
| 332 | termios.c_iflag = 0; |
| 333 | termios.c_oflag = 0; |
| 334 | termios.c_lflag = 0; |
| 335 | termios.c_cflag &= ~(CSIZE | PARENB); |
| 336 | termios.c_cflag |= CLOCAL | CS8; |
| 337 | termios.c_cc[VMIN] = 1; |
| 338 | termios.c_cc[VTIME] = 0; |
| 339 | |
| 340 | tcsetattr (remote_desc, TCSANOW, &termios); |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | #ifdef HAVE_TERMIO |
| 345 | { |
| 346 | struct termio termio; |
| 347 | ioctl (remote_desc, TCGETA, &termio); |
| 348 | |
| 349 | termio.c_iflag = 0; |
| 350 | termio.c_oflag = 0; |
| 351 | termio.c_lflag = 0; |
| 352 | termio.c_cflag &= ~(CSIZE | PARENB); |
| 353 | termio.c_cflag |= CLOCAL | CS8; |
| 354 | termio.c_cc[VMIN] = 1; |
| 355 | termio.c_cc[VTIME] = 0; |
| 356 | |
| 357 | ioctl (remote_desc, TCSETA, &termio); |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | #ifdef HAVE_SGTTY |
| 362 | { |
| 363 | struct sgttyb sg; |
| 364 | |
| 365 | ioctl (remote_desc, TIOCGETP, &sg); |
| 366 | sg.sg_flags = RAW; |
| 367 | ioctl (remote_desc, TIOCSETP, &sg); |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | fprintf (stderr, "Remote debugging using %s\n", name); |
| 372 | |
| 373 | enable_async_notification (remote_desc); |
| 374 | |
| 375 | /* Register the event loop handler. */ |
| 376 | add_file_handler (remote_desc, handle_serial_event, NULL); |
| 377 | } |
| 378 | #endif /* USE_WIN32API */ |
| 379 | else |
| 380 | { |
| 381 | int port; |
| 382 | socklen_t len; |
| 383 | struct sockaddr_in sockaddr; |
| 384 | |
| 385 | len = sizeof (sockaddr); |
| 386 | if (getsockname (listen_desc, |
| 387 | (struct sockaddr *) &sockaddr, &len) < 0 |
| 388 | || len < sizeof (sockaddr)) |
| 389 | perror_with_name ("Can't determine port"); |
| 390 | port = ntohs (sockaddr.sin_port); |
| 391 | |
| 392 | fprintf (stderr, "Listening on port %d\n", port); |
| 393 | fflush (stderr); |
| 394 | |
| 395 | /* Register the event loop handler. */ |
| 396 | add_file_handler (listen_desc, handle_accept_event, NULL); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | remote_close (void) |
| 402 | { |
| 403 | delete_file_handler (remote_desc); |
| 404 | |
| 405 | #ifdef USE_WIN32API |
| 406 | closesocket (remote_desc); |
| 407 | #else |
| 408 | if (! remote_connection_is_stdio ()) |
| 409 | close (remote_desc); |
| 410 | #endif |
| 411 | remote_desc = INVALID_DESCRIPTOR; |
| 412 | |
| 413 | reset_readchar (); |
| 414 | } |
| 415 | |
| 416 | #endif |
| 417 | |
| 418 | #ifndef IN_PROCESS_AGENT |
| 419 | |
| 420 | void |
| 421 | decode_address (CORE_ADDR *addrp, const char *start, int len) |
| 422 | { |
| 423 | CORE_ADDR addr; |
| 424 | char ch; |
| 425 | int i; |
| 426 | |
| 427 | addr = 0; |
| 428 | for (i = 0; i < len; i++) |
| 429 | { |
| 430 | ch = start[i]; |
| 431 | addr = addr << 4; |
| 432 | addr = addr | (fromhex (ch) & 0x0f); |
| 433 | } |
| 434 | *addrp = addr; |
| 435 | } |
| 436 | |
| 437 | const char * |
| 438 | decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) |
| 439 | { |
| 440 | const char *end; |
| 441 | |
| 442 | end = start; |
| 443 | while (*end != '\0' && *end != ';') |
| 444 | end++; |
| 445 | |
| 446 | decode_address (addrp, start, end - start); |
| 447 | |
| 448 | if (*end == ';') |
| 449 | end++; |
| 450 | return end; |
| 451 | } |
| 452 | |
| 453 | #endif |
| 454 | |
| 455 | #ifndef IN_PROCESS_AGENT |
| 456 | |
| 457 | /* Look for a sequence of characters which can be run-length encoded. |
| 458 | If there are any, update *CSUM and *P. Otherwise, output the |
| 459 | single character. Return the number of characters consumed. */ |
| 460 | |
| 461 | static int |
| 462 | try_rle (char *buf, int remaining, unsigned char *csum, char **p) |
| 463 | { |
| 464 | int n; |
| 465 | |
| 466 | /* Always output the character. */ |
| 467 | *csum += buf[0]; |
| 468 | *(*p)++ = buf[0]; |
| 469 | |
| 470 | /* Don't go past '~'. */ |
| 471 | if (remaining > 97) |
| 472 | remaining = 97; |
| 473 | |
| 474 | for (n = 1; n < remaining; n++) |
| 475 | if (buf[n] != buf[0]) |
| 476 | break; |
| 477 | |
| 478 | /* N is the index of the first character not the same as buf[0]. |
| 479 | buf[0] is counted twice, so by decrementing N, we get the number |
| 480 | of characters the RLE sequence will replace. */ |
| 481 | n--; |
| 482 | |
| 483 | if (n < 3) |
| 484 | return 1; |
| 485 | |
| 486 | /* Skip the frame characters. The manual says to skip '+' and '-' |
| 487 | also, but there's no reason to. Unfortunately these two unusable |
| 488 | characters double the encoded length of a four byte zero |
| 489 | value. */ |
| 490 | while (n + 29 == '$' || n + 29 == '#') |
| 491 | n--; |
| 492 | |
| 493 | *csum += '*'; |
| 494 | *(*p)++ = '*'; |
| 495 | *csum += n + 29; |
| 496 | *(*p)++ = n + 29; |
| 497 | |
| 498 | return n + 1; |
| 499 | } |
| 500 | |
| 501 | #endif |
| 502 | |
| 503 | #ifndef IN_PROCESS_AGENT |
| 504 | |
| 505 | /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */ |
| 506 | |
| 507 | char * |
| 508 | write_ptid (char *buf, ptid_t ptid) |
| 509 | { |
| 510 | int pid, tid; |
| 511 | |
| 512 | if (multi_process) |
| 513 | { |
| 514 | pid = ptid_get_pid (ptid); |
| 515 | if (pid < 0) |
| 516 | buf += sprintf (buf, "p-%x.", -pid); |
| 517 | else |
| 518 | buf += sprintf (buf, "p%x.", pid); |
| 519 | } |
| 520 | tid = ptid_get_lwp (ptid); |
| 521 | if (tid < 0) |
| 522 | buf += sprintf (buf, "-%x", -tid); |
| 523 | else |
| 524 | buf += sprintf (buf, "%x", tid); |
| 525 | |
| 526 | return buf; |
| 527 | } |
| 528 | |
| 529 | ULONGEST |
| 530 | hex_or_minus_one (char *buf, char **obuf) |
| 531 | { |
| 532 | ULONGEST ret; |
| 533 | |
| 534 | if (startswith (buf, "-1")) |
| 535 | { |
| 536 | ret = (ULONGEST) -1; |
| 537 | buf += 2; |
| 538 | } |
| 539 | else |
| 540 | buf = unpack_varlen_hex (buf, &ret); |
| 541 | |
| 542 | if (obuf) |
| 543 | *obuf = buf; |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /* Extract a PTID from BUF. If non-null, OBUF is set to the to one |
| 549 | passed the last parsed char. Returns null_ptid on error. */ |
| 550 | ptid_t |
| 551 | read_ptid (char *buf, char **obuf) |
| 552 | { |
| 553 | char *p = buf; |
| 554 | char *pp; |
| 555 | ULONGEST pid = 0, tid = 0; |
| 556 | |
| 557 | if (*p == 'p') |
| 558 | { |
| 559 | /* Multi-process ptid. */ |
| 560 | pp = unpack_varlen_hex (p + 1, &pid); |
| 561 | if (*pp != '.') |
| 562 | error ("invalid remote ptid: %s\n", p); |
| 563 | |
| 564 | p = pp + 1; |
| 565 | |
| 566 | tid = hex_or_minus_one (p, &pp); |
| 567 | |
| 568 | if (obuf) |
| 569 | *obuf = pp; |
| 570 | return ptid_build (pid, tid, 0); |
| 571 | } |
| 572 | |
| 573 | /* No multi-process. Just a tid. */ |
| 574 | tid = hex_or_minus_one (p, &pp); |
| 575 | |
| 576 | /* Since the stub is not sending a process id, then default to |
| 577 | what's in the current inferior. */ |
| 578 | pid = ptid_get_pid (current_ptid); |
| 579 | |
| 580 | if (obuf) |
| 581 | *obuf = pp; |
| 582 | return ptid_build (pid, tid, 0); |
| 583 | } |
| 584 | |
| 585 | /* Write COUNT bytes in BUF to the client. |
| 586 | The result is the number of bytes written or -1 if error. |
| 587 | This may return less than COUNT. */ |
| 588 | |
| 589 | static int |
| 590 | write_prim (const void *buf, int count) |
| 591 | { |
| 592 | if (remote_connection_is_stdio ()) |
| 593 | return write (fileno (stdout), buf, count); |
| 594 | else |
| 595 | return write (remote_desc, buf, count); |
| 596 | } |
| 597 | |
| 598 | /* Read COUNT bytes from the client and store in BUF. |
| 599 | The result is the number of bytes read or -1 if error. |
| 600 | This may return less than COUNT. */ |
| 601 | |
| 602 | static int |
| 603 | read_prim (void *buf, int count) |
| 604 | { |
| 605 | if (remote_connection_is_stdio ()) |
| 606 | return read (fileno (stdin), buf, count); |
| 607 | else |
| 608 | return read (remote_desc, buf, count); |
| 609 | } |
| 610 | |
| 611 | /* Send a packet to the remote machine, with error checking. |
| 612 | The data of the packet is in BUF, and the length of the |
| 613 | packet is in CNT. Returns >= 0 on success, -1 otherwise. */ |
| 614 | |
| 615 | static int |
| 616 | putpkt_binary_1 (char *buf, int cnt, int is_notif) |
| 617 | { |
| 618 | int i; |
| 619 | unsigned char csum = 0; |
| 620 | char *buf2; |
| 621 | char *p; |
| 622 | int cc; |
| 623 | |
| 624 | buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1); |
| 625 | |
| 626 | /* Copy the packet into buffer BUF2, encapsulating it |
| 627 | and giving it a checksum. */ |
| 628 | |
| 629 | p = buf2; |
| 630 | if (is_notif) |
| 631 | *p++ = '%'; |
| 632 | else |
| 633 | *p++ = '$'; |
| 634 | |
| 635 | for (i = 0; i < cnt;) |
| 636 | i += try_rle (buf + i, cnt - i, &csum, &p); |
| 637 | |
| 638 | *p++ = '#'; |
| 639 | *p++ = tohex ((csum >> 4) & 0xf); |
| 640 | *p++ = tohex (csum & 0xf); |
| 641 | |
| 642 | *p = '\0'; |
| 643 | |
| 644 | /* Send it over and over until we get a positive ack. */ |
| 645 | |
| 646 | do |
| 647 | { |
| 648 | if (write_prim (buf2, p - buf2) != p - buf2) |
| 649 | { |
| 650 | perror ("putpkt(write)"); |
| 651 | free (buf2); |
| 652 | return -1; |
| 653 | } |
| 654 | |
| 655 | if (noack_mode || is_notif) |
| 656 | { |
| 657 | /* Don't expect an ack then. */ |
| 658 | if (remote_debug) |
| 659 | { |
| 660 | if (is_notif) |
| 661 | fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2); |
| 662 | else |
| 663 | fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2); |
| 664 | fflush (stderr); |
| 665 | } |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | if (remote_debug) |
| 670 | { |
| 671 | fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2); |
| 672 | fflush (stderr); |
| 673 | } |
| 674 | |
| 675 | cc = readchar (); |
| 676 | |
| 677 | if (cc < 0) |
| 678 | { |
| 679 | free (buf2); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | if (remote_debug) |
| 684 | { |
| 685 | fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc); |
| 686 | fflush (stderr); |
| 687 | } |
| 688 | |
| 689 | /* Check for an input interrupt while we're here. */ |
| 690 | if (cc == '\003' && current_thread != NULL) |
| 691 | (*the_target->request_interrupt) (); |
| 692 | } |
| 693 | while (cc != '+'); |
| 694 | |
| 695 | free (buf2); |
| 696 | return 1; /* Success! */ |
| 697 | } |
| 698 | |
| 699 | int |
| 700 | putpkt_binary (char *buf, int cnt) |
| 701 | { |
| 702 | return putpkt_binary_1 (buf, cnt, 0); |
| 703 | } |
| 704 | |
| 705 | /* Send a packet to the remote machine, with error checking. The data |
| 706 | of the packet is in BUF, and the packet should be a NUL-terminated |
| 707 | string. Returns >= 0 on success, -1 otherwise. */ |
| 708 | |
| 709 | int |
| 710 | putpkt (char *buf) |
| 711 | { |
| 712 | return putpkt_binary (buf, strlen (buf)); |
| 713 | } |
| 714 | |
| 715 | int |
| 716 | putpkt_notif (char *buf) |
| 717 | { |
| 718 | return putpkt_binary_1 (buf, strlen (buf), 1); |
| 719 | } |
| 720 | |
| 721 | /* Come here when we get an input interrupt from the remote side. This |
| 722 | interrupt should only be active while we are waiting for the child to do |
| 723 | something. Thus this assumes readchar:bufcnt is 0. |
| 724 | About the only thing that should come through is a ^C, which |
| 725 | will cause us to request child interruption. */ |
| 726 | |
| 727 | static void |
| 728 | input_interrupt (int unused) |
| 729 | { |
| 730 | fd_set readset; |
| 731 | struct timeval immediate = { 0, 0 }; |
| 732 | |
| 733 | /* Protect against spurious interrupts. This has been observed to |
| 734 | be a problem under NetBSD 1.4 and 1.5. */ |
| 735 | |
| 736 | FD_ZERO (&readset); |
| 737 | FD_SET (remote_desc, &readset); |
| 738 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) |
| 739 | { |
| 740 | int cc; |
| 741 | char c = 0; |
| 742 | |
| 743 | cc = read_prim (&c, 1); |
| 744 | |
| 745 | if (cc == 0) |
| 746 | { |
| 747 | fprintf (stderr, "client connection closed\n"); |
| 748 | return; |
| 749 | } |
| 750 | else if (cc != 1 || c != '\003') |
| 751 | { |
| 752 | fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c); |
| 753 | if (isprint (c)) |
| 754 | fprintf (stderr, "('%c')\n", c); |
| 755 | else |
| 756 | fprintf (stderr, "('\\x%02x')\n", c & 0xff); |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | (*the_target->request_interrupt) (); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* Check if the remote side sent us an interrupt request (^C). */ |
| 765 | void |
| 766 | check_remote_input_interrupt_request (void) |
| 767 | { |
| 768 | /* This function may be called before establishing communications, |
| 769 | therefore we need to validate the remote descriptor. */ |
| 770 | |
| 771 | if (remote_desc == INVALID_DESCRIPTOR) |
| 772 | return; |
| 773 | |
| 774 | input_interrupt (0); |
| 775 | } |
| 776 | |
| 777 | /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to |
| 778 | accept Control-C from the client, and must be disabled when talking to |
| 779 | the client. */ |
| 780 | |
| 781 | static void |
| 782 | unblock_async_io (void) |
| 783 | { |
| 784 | #ifndef USE_WIN32API |
| 785 | sigset_t sigio_set; |
| 786 | |
| 787 | sigemptyset (&sigio_set); |
| 788 | sigaddset (&sigio_set, SIGIO); |
| 789 | sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); |
| 790 | #endif |
| 791 | } |
| 792 | |
| 793 | #ifdef __QNX__ |
| 794 | static void |
| 795 | nto_comctrl (int enable) |
| 796 | { |
| 797 | struct sigevent event; |
| 798 | |
| 799 | if (enable) |
| 800 | { |
| 801 | event.sigev_notify = SIGEV_SIGNAL_THREAD; |
| 802 | event.sigev_signo = SIGIO; |
| 803 | event.sigev_code = 0; |
| 804 | event.sigev_value.sival_ptr = NULL; |
| 805 | event.sigev_priority = -1; |
| 806 | ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT, |
| 807 | &event); |
| 808 | } |
| 809 | else |
| 810 | ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL); |
| 811 | } |
| 812 | #endif /* __QNX__ */ |
| 813 | |
| 814 | |
| 815 | /* Current state of asynchronous I/O. */ |
| 816 | static int async_io_enabled; |
| 817 | |
| 818 | /* Enable asynchronous I/O. */ |
| 819 | void |
| 820 | enable_async_io (void) |
| 821 | { |
| 822 | if (async_io_enabled) |
| 823 | return; |
| 824 | |
| 825 | #ifndef USE_WIN32API |
| 826 | signal (SIGIO, input_interrupt); |
| 827 | #endif |
| 828 | async_io_enabled = 1; |
| 829 | #ifdef __QNX__ |
| 830 | nto_comctrl (1); |
| 831 | #endif /* __QNX__ */ |
| 832 | } |
| 833 | |
| 834 | /* Disable asynchronous I/O. */ |
| 835 | void |
| 836 | disable_async_io (void) |
| 837 | { |
| 838 | if (!async_io_enabled) |
| 839 | return; |
| 840 | |
| 841 | #ifndef USE_WIN32API |
| 842 | signal (SIGIO, SIG_IGN); |
| 843 | #endif |
| 844 | async_io_enabled = 0; |
| 845 | #ifdef __QNX__ |
| 846 | nto_comctrl (0); |
| 847 | #endif /* __QNX__ */ |
| 848 | |
| 849 | } |
| 850 | |
| 851 | void |
| 852 | initialize_async_io (void) |
| 853 | { |
| 854 | /* Make sure that async I/O starts disabled. */ |
| 855 | async_io_enabled = 1; |
| 856 | disable_async_io (); |
| 857 | |
| 858 | /* Make sure the signal is unblocked. */ |
| 859 | unblock_async_io (); |
| 860 | } |
| 861 | |
| 862 | /* Internal buffer used by readchar. |
| 863 | These are global to readchar because reschedule_remote needs to be |
| 864 | able to tell whether the buffer is empty. */ |
| 865 | |
| 866 | static unsigned char readchar_buf[BUFSIZ]; |
| 867 | static int readchar_bufcnt = 0; |
| 868 | static unsigned char *readchar_bufp; |
| 869 | |
| 870 | /* Returns next char from remote GDB. -1 if error. */ |
| 871 | |
| 872 | static int |
| 873 | readchar (void) |
| 874 | { |
| 875 | int ch; |
| 876 | |
| 877 | if (readchar_bufcnt == 0) |
| 878 | { |
| 879 | readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf)); |
| 880 | |
| 881 | if (readchar_bufcnt <= 0) |
| 882 | { |
| 883 | if (readchar_bufcnt == 0) |
| 884 | fprintf (stderr, "readchar: Got EOF\n"); |
| 885 | else |
| 886 | perror ("readchar"); |
| 887 | |
| 888 | return -1; |
| 889 | } |
| 890 | |
| 891 | readchar_bufp = readchar_buf; |
| 892 | } |
| 893 | |
| 894 | readchar_bufcnt--; |
| 895 | ch = *readchar_bufp++; |
| 896 | reschedule (); |
| 897 | return ch; |
| 898 | } |
| 899 | |
| 900 | /* Reset the readchar state machine. */ |
| 901 | |
| 902 | static void |
| 903 | reset_readchar (void) |
| 904 | { |
| 905 | readchar_bufcnt = 0; |
| 906 | if (readchar_callback != NOT_SCHEDULED) |
| 907 | { |
| 908 | delete_callback_event (readchar_callback); |
| 909 | readchar_callback = NOT_SCHEDULED; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /* Process remaining data in readchar_buf. */ |
| 914 | |
| 915 | static int |
| 916 | process_remaining (void *context) |
| 917 | { |
| 918 | int res; |
| 919 | |
| 920 | /* This is a one-shot event. */ |
| 921 | readchar_callback = NOT_SCHEDULED; |
| 922 | |
| 923 | if (readchar_bufcnt > 0) |
| 924 | res = handle_serial_event (0, NULL); |
| 925 | else |
| 926 | res = 0; |
| 927 | |
| 928 | return res; |
| 929 | } |
| 930 | |
| 931 | /* If there is still data in the buffer, queue another event to process it, |
| 932 | we can't sleep in select yet. */ |
| 933 | |
| 934 | static void |
| 935 | reschedule (void) |
| 936 | { |
| 937 | if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED) |
| 938 | readchar_callback = append_callback_event (process_remaining, NULL); |
| 939 | } |
| 940 | |
| 941 | /* Read a packet from the remote machine, with error checking, |
| 942 | and store it in BUF. Returns length of packet, or negative if error. */ |
| 943 | |
| 944 | int |
| 945 | getpkt (char *buf) |
| 946 | { |
| 947 | char *bp; |
| 948 | unsigned char csum, c1, c2; |
| 949 | int c; |
| 950 | |
| 951 | while (1) |
| 952 | { |
| 953 | csum = 0; |
| 954 | |
| 955 | while (1) |
| 956 | { |
| 957 | c = readchar (); |
| 958 | if (c == '$') |
| 959 | break; |
| 960 | if (remote_debug) |
| 961 | { |
| 962 | fprintf (stderr, "[getpkt: discarding char '%c']\n", c); |
| 963 | fflush (stderr); |
| 964 | } |
| 965 | |
| 966 | if (c < 0) |
| 967 | return -1; |
| 968 | } |
| 969 | |
| 970 | bp = buf; |
| 971 | while (1) |
| 972 | { |
| 973 | c = readchar (); |
| 974 | if (c < 0) |
| 975 | return -1; |
| 976 | if (c == '#') |
| 977 | break; |
| 978 | *bp++ = c; |
| 979 | csum += c; |
| 980 | } |
| 981 | *bp = 0; |
| 982 | |
| 983 | c1 = fromhex (readchar ()); |
| 984 | c2 = fromhex (readchar ()); |
| 985 | |
| 986 | if (csum == (c1 << 4) + c2) |
| 987 | break; |
| 988 | |
| 989 | if (noack_mode) |
| 990 | { |
| 991 | fprintf (stderr, |
| 992 | "Bad checksum, sentsum=0x%x, csum=0x%x, " |
| 993 | "buf=%s [no-ack-mode, Bad medium?]\n", |
| 994 | (c1 << 4) + c2, csum, buf); |
| 995 | /* Not much we can do, GDB wasn't expecting an ack/nac. */ |
| 996 | break; |
| 997 | } |
| 998 | |
| 999 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
| 1000 | (c1 << 4) + c2, csum, buf); |
| 1001 | if (write_prim ("-", 1) != 1) |
| 1002 | return -1; |
| 1003 | } |
| 1004 | |
| 1005 | if (!noack_mode) |
| 1006 | { |
| 1007 | if (remote_debug) |
| 1008 | { |
| 1009 | fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); |
| 1010 | fflush (stderr); |
| 1011 | } |
| 1012 | |
| 1013 | if (write_prim ("+", 1) != 1) |
| 1014 | return -1; |
| 1015 | |
| 1016 | if (remote_debug) |
| 1017 | { |
| 1018 | fprintf (stderr, "[sent ack]\n"); |
| 1019 | fflush (stderr); |
| 1020 | } |
| 1021 | } |
| 1022 | else |
| 1023 | { |
| 1024 | if (remote_debug) |
| 1025 | { |
| 1026 | fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf); |
| 1027 | fflush (stderr); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | return bp - buf; |
| 1032 | } |
| 1033 | |
| 1034 | void |
| 1035 | write_ok (char *buf) |
| 1036 | { |
| 1037 | buf[0] = 'O'; |
| 1038 | buf[1] = 'K'; |
| 1039 | buf[2] = '\0'; |
| 1040 | } |
| 1041 | |
| 1042 | void |
| 1043 | write_enn (char *buf) |
| 1044 | { |
| 1045 | /* Some day, we should define the meanings of the error codes... */ |
| 1046 | buf[0] = 'E'; |
| 1047 | buf[1] = '0'; |
| 1048 | buf[2] = '1'; |
| 1049 | buf[3] = '\0'; |
| 1050 | } |
| 1051 | |
| 1052 | #endif |
| 1053 | |
| 1054 | #ifndef IN_PROCESS_AGENT |
| 1055 | |
| 1056 | static char * |
| 1057 | outreg (struct regcache *regcache, int regno, char *buf) |
| 1058 | { |
| 1059 | if ((regno >> 12) != 0) |
| 1060 | *buf++ = tohex ((regno >> 12) & 0xf); |
| 1061 | if ((regno >> 8) != 0) |
| 1062 | *buf++ = tohex ((regno >> 8) & 0xf); |
| 1063 | *buf++ = tohex ((regno >> 4) & 0xf); |
| 1064 | *buf++ = tohex (regno & 0xf); |
| 1065 | *buf++ = ':'; |
| 1066 | collect_register_as_string (regcache, regno, buf); |
| 1067 | buf += 2 * register_size (regcache->tdesc, regno); |
| 1068 | *buf++ = ';'; |
| 1069 | |
| 1070 | return buf; |
| 1071 | } |
| 1072 | |
| 1073 | void |
| 1074 | new_thread_notify (int id) |
| 1075 | { |
| 1076 | char own_buf[256]; |
| 1077 | |
| 1078 | /* The `n' response is not yet part of the remote protocol. Do nothing. */ |
| 1079 | if (1) |
| 1080 | return; |
| 1081 | |
| 1082 | if (server_waiting == 0) |
| 1083 | return; |
| 1084 | |
| 1085 | sprintf (own_buf, "n%x", id); |
| 1086 | disable_async_io (); |
| 1087 | putpkt (own_buf); |
| 1088 | enable_async_io (); |
| 1089 | } |
| 1090 | |
| 1091 | void |
| 1092 | dead_thread_notify (int id) |
| 1093 | { |
| 1094 | char own_buf[256]; |
| 1095 | |
| 1096 | /* The `x' response is not yet part of the remote protocol. Do nothing. */ |
| 1097 | if (1) |
| 1098 | return; |
| 1099 | |
| 1100 | sprintf (own_buf, "x%x", id); |
| 1101 | disable_async_io (); |
| 1102 | putpkt (own_buf); |
| 1103 | enable_async_io (); |
| 1104 | } |
| 1105 | |
| 1106 | void |
| 1107 | prepare_resume_reply (char *buf, ptid_t ptid, |
| 1108 | struct target_waitstatus *status) |
| 1109 | { |
| 1110 | if (debug_threads) |
| 1111 | debug_printf ("Writing resume reply for %s:%d\n", |
| 1112 | target_pid_to_str (ptid), status->kind); |
| 1113 | |
| 1114 | switch (status->kind) |
| 1115 | { |
| 1116 | case TARGET_WAITKIND_STOPPED: |
| 1117 | case TARGET_WAITKIND_FORKED: |
| 1118 | case TARGET_WAITKIND_VFORKED: |
| 1119 | { |
| 1120 | struct thread_info *saved_thread; |
| 1121 | const char **regp; |
| 1122 | struct regcache *regcache; |
| 1123 | |
| 1124 | if ((status->kind == TARGET_WAITKIND_FORKED && report_fork_events) |
| 1125 | || (status->kind == TARGET_WAITKIND_VFORKED && report_vfork_events)) |
| 1126 | { |
| 1127 | enum gdb_signal signal = GDB_SIGNAL_TRAP; |
| 1128 | const char *event = (status->kind == TARGET_WAITKIND_FORKED |
| 1129 | ? "fork" : "vfork"); |
| 1130 | |
| 1131 | sprintf (buf, "T%02x%s:", signal, event); |
| 1132 | buf += strlen (buf); |
| 1133 | buf = write_ptid (buf, status->value.related_pid); |
| 1134 | strcat (buf, ";"); |
| 1135 | } |
| 1136 | else |
| 1137 | sprintf (buf, "T%02x", status->value.sig); |
| 1138 | |
| 1139 | buf += strlen (buf); |
| 1140 | |
| 1141 | saved_thread = current_thread; |
| 1142 | |
| 1143 | current_thread = find_thread_ptid (ptid); |
| 1144 | |
| 1145 | regp = current_target_desc ()->expedite_regs; |
| 1146 | |
| 1147 | regcache = get_thread_regcache (current_thread, 1); |
| 1148 | |
| 1149 | if (the_target->stopped_by_watchpoint != NULL |
| 1150 | && (*the_target->stopped_by_watchpoint) ()) |
| 1151 | { |
| 1152 | CORE_ADDR addr; |
| 1153 | int i; |
| 1154 | |
| 1155 | strncpy (buf, "watch:", 6); |
| 1156 | buf += 6; |
| 1157 | |
| 1158 | addr = (*the_target->stopped_data_address) (); |
| 1159 | |
| 1160 | /* Convert each byte of the address into two hexadecimal |
| 1161 | chars. Note that we take sizeof (void *) instead of |
| 1162 | sizeof (addr); this is to avoid sending a 64-bit |
| 1163 | address to a 32-bit GDB. */ |
| 1164 | for (i = sizeof (void *) * 2; i > 0; i--) |
| 1165 | *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); |
| 1166 | *buf++ = ';'; |
| 1167 | } |
| 1168 | else if (swbreak_feature && target_stopped_by_sw_breakpoint ()) |
| 1169 | { |
| 1170 | sprintf (buf, "swbreak:;"); |
| 1171 | buf += strlen (buf); |
| 1172 | } |
| 1173 | else if (hwbreak_feature && target_stopped_by_hw_breakpoint ()) |
| 1174 | { |
| 1175 | sprintf (buf, "hwbreak:;"); |
| 1176 | buf += strlen (buf); |
| 1177 | } |
| 1178 | |
| 1179 | while (*regp) |
| 1180 | { |
| 1181 | buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf); |
| 1182 | regp ++; |
| 1183 | } |
| 1184 | *buf = '\0'; |
| 1185 | |
| 1186 | /* Formerly, if the debugger had not used any thread features |
| 1187 | we would not burden it with a thread status response. This |
| 1188 | was for the benefit of GDB 4.13 and older. However, in |
| 1189 | recent GDB versions the check (``if (cont_thread != 0)'') |
| 1190 | does not have the desired effect because of sillyness in |
| 1191 | the way that the remote protocol handles specifying a |
| 1192 | thread. Since thread support relies on qSymbol support |
| 1193 | anyway, assume GDB can handle threads. */ |
| 1194 | |
| 1195 | if (using_threads && !disable_packet_Tthread) |
| 1196 | { |
| 1197 | /* This if (1) ought to be unnecessary. But remote_wait |
| 1198 | in GDB will claim this event belongs to inferior_ptid |
| 1199 | if we do not specify a thread, and there's no way for |
| 1200 | gdbserver to know what inferior_ptid is. */ |
| 1201 | if (1 || !ptid_equal (general_thread, ptid)) |
| 1202 | { |
| 1203 | int core = -1; |
| 1204 | /* In non-stop, don't change the general thread behind |
| 1205 | GDB's back. */ |
| 1206 | if (!non_stop) |
| 1207 | general_thread = ptid; |
| 1208 | sprintf (buf, "thread:"); |
| 1209 | buf += strlen (buf); |
| 1210 | buf = write_ptid (buf, ptid); |
| 1211 | strcat (buf, ";"); |
| 1212 | buf += strlen (buf); |
| 1213 | |
| 1214 | core = target_core_of_thread (ptid); |
| 1215 | |
| 1216 | if (core != -1) |
| 1217 | { |
| 1218 | sprintf (buf, "core:"); |
| 1219 | buf += strlen (buf); |
| 1220 | sprintf (buf, "%x", core); |
| 1221 | strcat (buf, ";"); |
| 1222 | buf += strlen (buf); |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (dlls_changed) |
| 1228 | { |
| 1229 | strcpy (buf, "library:;"); |
| 1230 | buf += strlen (buf); |
| 1231 | dlls_changed = 0; |
| 1232 | } |
| 1233 | |
| 1234 | current_thread = saved_thread; |
| 1235 | } |
| 1236 | break; |
| 1237 | case TARGET_WAITKIND_EXITED: |
| 1238 | if (multi_process) |
| 1239 | sprintf (buf, "W%x;process:%x", |
| 1240 | status->value.integer, ptid_get_pid (ptid)); |
| 1241 | else |
| 1242 | sprintf (buf, "W%02x", status->value.integer); |
| 1243 | break; |
| 1244 | case TARGET_WAITKIND_SIGNALLED: |
| 1245 | if (multi_process) |
| 1246 | sprintf (buf, "X%x;process:%x", |
| 1247 | status->value.sig, ptid_get_pid (ptid)); |
| 1248 | else |
| 1249 | sprintf (buf, "X%02x", status->value.sig); |
| 1250 | break; |
| 1251 | case TARGET_WAITKIND_VFORK_DONE: |
| 1252 | if (report_vfork_events) |
| 1253 | { |
| 1254 | enum gdb_signal signal = GDB_SIGNAL_TRAP; |
| 1255 | |
| 1256 | sprintf (buf, "T%02xvforkdone:;", signal); |
| 1257 | } |
| 1258 | else |
| 1259 | sprintf (buf, "T%02x", GDB_SIGNAL_0); |
| 1260 | break; |
| 1261 | default: |
| 1262 | error ("unhandled waitkind"); |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | void |
| 1268 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
| 1269 | { |
| 1270 | int i = 0, j = 0; |
| 1271 | char ch; |
| 1272 | *mem_addr_ptr = *len_ptr = 0; |
| 1273 | |
| 1274 | while ((ch = from[i++]) != ',') |
| 1275 | { |
| 1276 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 1277 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 1278 | } |
| 1279 | |
| 1280 | for (j = 0; j < 4; j++) |
| 1281 | { |
| 1282 | if ((ch = from[i++]) == 0) |
| 1283 | break; |
| 1284 | *len_ptr = *len_ptr << 4; |
| 1285 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | void |
| 1290 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
| 1291 | unsigned char **to_p) |
| 1292 | { |
| 1293 | int i = 0; |
| 1294 | char ch; |
| 1295 | *mem_addr_ptr = *len_ptr = 0; |
| 1296 | |
| 1297 | while ((ch = from[i++]) != ',') |
| 1298 | { |
| 1299 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 1300 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 1301 | } |
| 1302 | |
| 1303 | while ((ch = from[i++]) != ':') |
| 1304 | { |
| 1305 | *len_ptr = *len_ptr << 4; |
| 1306 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1307 | } |
| 1308 | |
| 1309 | if (*to_p == NULL) |
| 1310 | *to_p = xmalloc (*len_ptr); |
| 1311 | |
| 1312 | hex2bin (&from[i++], *to_p, *len_ptr); |
| 1313 | } |
| 1314 | |
| 1315 | int |
| 1316 | decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, |
| 1317 | unsigned int *len_ptr, unsigned char **to_p) |
| 1318 | { |
| 1319 | int i = 0; |
| 1320 | char ch; |
| 1321 | *mem_addr_ptr = *len_ptr = 0; |
| 1322 | |
| 1323 | while ((ch = from[i++]) != ',') |
| 1324 | { |
| 1325 | *mem_addr_ptr = *mem_addr_ptr << 4; |
| 1326 | *mem_addr_ptr |= fromhex (ch) & 0x0f; |
| 1327 | } |
| 1328 | |
| 1329 | while ((ch = from[i++]) != ':') |
| 1330 | { |
| 1331 | *len_ptr = *len_ptr << 4; |
| 1332 | *len_ptr |= fromhex (ch) & 0x0f; |
| 1333 | } |
| 1334 | |
| 1335 | if (*to_p == NULL) |
| 1336 | *to_p = xmalloc (*len_ptr); |
| 1337 | |
| 1338 | if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, |
| 1339 | *to_p, *len_ptr) != *len_ptr) |
| 1340 | return -1; |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | /* Decode a qXfer write request. */ |
| 1346 | |
| 1347 | int |
| 1348 | decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset, |
| 1349 | unsigned int *len, unsigned char *data) |
| 1350 | { |
| 1351 | char ch; |
| 1352 | char *b = buf; |
| 1353 | |
| 1354 | /* Extract the offset. */ |
| 1355 | *offset = 0; |
| 1356 | while ((ch = *buf++) != ':') |
| 1357 | { |
| 1358 | *offset = *offset << 4; |
| 1359 | *offset |= fromhex (ch) & 0x0f; |
| 1360 | } |
| 1361 | |
| 1362 | /* Get encoded data. */ |
| 1363 | packet_len -= buf - b; |
| 1364 | *len = remote_unescape_input ((const gdb_byte *) buf, packet_len, |
| 1365 | data, packet_len); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | /* Decode the parameters of a qSearch:memory packet. */ |
| 1370 | |
| 1371 | int |
| 1372 | decode_search_memory_packet (const char *buf, int packet_len, |
| 1373 | CORE_ADDR *start_addrp, |
| 1374 | CORE_ADDR *search_space_lenp, |
| 1375 | gdb_byte *pattern, unsigned int *pattern_lenp) |
| 1376 | { |
| 1377 | const char *p = buf; |
| 1378 | |
| 1379 | p = decode_address_to_semicolon (start_addrp, p); |
| 1380 | p = decode_address_to_semicolon (search_space_lenp, p); |
| 1381 | packet_len -= p - buf; |
| 1382 | *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len, |
| 1383 | pattern, packet_len); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | static void |
| 1388 | free_sym_cache (struct sym_cache *sym) |
| 1389 | { |
| 1390 | if (sym != NULL) |
| 1391 | { |
| 1392 | free (sym->name); |
| 1393 | free (sym); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | void |
| 1398 | clear_symbol_cache (struct sym_cache **symcache_p) |
| 1399 | { |
| 1400 | struct sym_cache *sym, *next; |
| 1401 | |
| 1402 | /* Check the cache first. */ |
| 1403 | for (sym = *symcache_p; sym; sym = next) |
| 1404 | { |
| 1405 | next = sym->next; |
| 1406 | free_sym_cache (sym); |
| 1407 | } |
| 1408 | |
| 1409 | *symcache_p = NULL; |
| 1410 | } |
| 1411 | |
| 1412 | /* Get the address of NAME, and return it in ADDRP if found. if |
| 1413 | MAY_ASK_GDB is false, assume symbol cache misses are failures. |
| 1414 | Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ |
| 1415 | |
| 1416 | int |
| 1417 | look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb) |
| 1418 | { |
| 1419 | char own_buf[266], *p, *q; |
| 1420 | int len; |
| 1421 | struct sym_cache *sym; |
| 1422 | struct process_info *proc; |
| 1423 | |
| 1424 | proc = current_process (); |
| 1425 | |
| 1426 | /* Check the cache first. */ |
| 1427 | for (sym = proc->symbol_cache; sym; sym = sym->next) |
| 1428 | if (strcmp (name, sym->name) == 0) |
| 1429 | { |
| 1430 | *addrp = sym->addr; |
| 1431 | return 1; |
| 1432 | } |
| 1433 | |
| 1434 | /* It might not be an appropriate time to look up a symbol, |
| 1435 | e.g. while we're trying to fetch registers. */ |
| 1436 | if (!may_ask_gdb) |
| 1437 | return 0; |
| 1438 | |
| 1439 | /* Send the request. */ |
| 1440 | strcpy (own_buf, "qSymbol:"); |
| 1441 | bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"), |
| 1442 | strlen (name)); |
| 1443 | if (putpkt (own_buf) < 0) |
| 1444 | return -1; |
| 1445 | |
| 1446 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ |
| 1447 | len = getpkt (own_buf); |
| 1448 | if (len < 0) |
| 1449 | return -1; |
| 1450 | |
| 1451 | /* We ought to handle pretty much any packet at this point while we |
| 1452 | wait for the qSymbol "response". That requires re-entering the |
| 1453 | main loop. For now, this is an adequate approximation; allow |
| 1454 | GDB to read from memory while it figures out the address of the |
| 1455 | symbol. */ |
| 1456 | while (own_buf[0] == 'm') |
| 1457 | { |
| 1458 | CORE_ADDR mem_addr; |
| 1459 | unsigned char *mem_buf; |
| 1460 | unsigned int mem_len; |
| 1461 | |
| 1462 | decode_m_packet (&own_buf[1], &mem_addr, &mem_len); |
| 1463 | mem_buf = xmalloc (mem_len); |
| 1464 | if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) |
| 1465 | bin2hex (mem_buf, own_buf, mem_len); |
| 1466 | else |
| 1467 | write_enn (own_buf); |
| 1468 | free (mem_buf); |
| 1469 | if (putpkt (own_buf) < 0) |
| 1470 | return -1; |
| 1471 | len = getpkt (own_buf); |
| 1472 | if (len < 0) |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | if (!startswith (own_buf, "qSymbol:")) |
| 1477 | { |
| 1478 | warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | |
| 1482 | p = own_buf + strlen ("qSymbol:"); |
| 1483 | q = p; |
| 1484 | while (*q && *q != ':') |
| 1485 | q++; |
| 1486 | |
| 1487 | /* Make sure we found a value for the symbol. */ |
| 1488 | if (p == q || *q == '\0') |
| 1489 | return 0; |
| 1490 | |
| 1491 | decode_address (addrp, p, q - p); |
| 1492 | |
| 1493 | /* Save the symbol in our cache. */ |
| 1494 | sym = xmalloc (sizeof (*sym)); |
| 1495 | sym->name = xstrdup (name); |
| 1496 | sym->addr = *addrp; |
| 1497 | sym->next = proc->symbol_cache; |
| 1498 | proc->symbol_cache = sym; |
| 1499 | |
| 1500 | return 1; |
| 1501 | } |
| 1502 | |
| 1503 | /* Relocate an instruction to execute at a different address. OLDLOC |
| 1504 | is the address in the inferior memory where the instruction to |
| 1505 | relocate is currently at. On input, TO points to the destination |
| 1506 | where we want the instruction to be copied (and possibly adjusted) |
| 1507 | to. On output, it points to one past the end of the resulting |
| 1508 | instruction(s). The effect of executing the instruction at TO |
| 1509 | shall be the same as if executing it at OLDLOC. For example, call |
| 1510 | instructions that implicitly push the return address on the stack |
| 1511 | should be adjusted to return to the instruction after OLDLOC; |
| 1512 | relative branches, and other PC-relative instructions need the |
| 1513 | offset adjusted; etc. Returns 0 on success, -1 on failure. */ |
| 1514 | |
| 1515 | int |
| 1516 | relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc) |
| 1517 | { |
| 1518 | char own_buf[266]; |
| 1519 | int len; |
| 1520 | ULONGEST written = 0; |
| 1521 | |
| 1522 | /* Send the request. */ |
| 1523 | strcpy (own_buf, "qRelocInsn:"); |
| 1524 | sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc), |
| 1525 | paddress (*to)); |
| 1526 | if (putpkt (own_buf) < 0) |
| 1527 | return -1; |
| 1528 | |
| 1529 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ |
| 1530 | len = getpkt (own_buf); |
| 1531 | if (len < 0) |
| 1532 | return -1; |
| 1533 | |
| 1534 | /* We ought to handle pretty much any packet at this point while we |
| 1535 | wait for the qRelocInsn "response". That requires re-entering |
| 1536 | the main loop. For now, this is an adequate approximation; allow |
| 1537 | GDB to access memory. */ |
| 1538 | while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X') |
| 1539 | { |
| 1540 | CORE_ADDR mem_addr; |
| 1541 | unsigned char *mem_buf = NULL; |
| 1542 | unsigned int mem_len; |
| 1543 | |
| 1544 | if (own_buf[0] == 'm') |
| 1545 | { |
| 1546 | decode_m_packet (&own_buf[1], &mem_addr, &mem_len); |
| 1547 | mem_buf = xmalloc (mem_len); |
| 1548 | if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) |
| 1549 | bin2hex (mem_buf, own_buf, mem_len); |
| 1550 | else |
| 1551 | write_enn (own_buf); |
| 1552 | } |
| 1553 | else if (own_buf[0] == 'X') |
| 1554 | { |
| 1555 | if (decode_X_packet (&own_buf[1], len - 1, &mem_addr, |
| 1556 | &mem_len, &mem_buf) < 0 |
| 1557 | || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0) |
| 1558 | write_enn (own_buf); |
| 1559 | else |
| 1560 | write_ok (own_buf); |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf); |
| 1565 | if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0) |
| 1566 | write_ok (own_buf); |
| 1567 | else |
| 1568 | write_enn (own_buf); |
| 1569 | } |
| 1570 | free (mem_buf); |
| 1571 | if (putpkt (own_buf) < 0) |
| 1572 | return -1; |
| 1573 | len = getpkt (own_buf); |
| 1574 | if (len < 0) |
| 1575 | return -1; |
| 1576 | } |
| 1577 | |
| 1578 | if (own_buf[0] == 'E') |
| 1579 | { |
| 1580 | warning ("An error occurred while relocating an instruction: %s\n", |
| 1581 | own_buf); |
| 1582 | return -1; |
| 1583 | } |
| 1584 | |
| 1585 | if (!startswith (own_buf, "qRelocInsn:")) |
| 1586 | { |
| 1587 | warning ("Malformed response to qRelocInsn, ignoring: %s\n", |
| 1588 | own_buf); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | |
| 1592 | unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written); |
| 1593 | |
| 1594 | *to += written; |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | void |
| 1599 | monitor_output (const char *msg) |
| 1600 | { |
| 1601 | int len = strlen (msg); |
| 1602 | char *buf = xmalloc (len * 2 + 2); |
| 1603 | |
| 1604 | buf[0] = 'O'; |
| 1605 | bin2hex ((const gdb_byte *) msg, buf + 1, len); |
| 1606 | |
| 1607 | putpkt (buf); |
| 1608 | free (buf); |
| 1609 | } |
| 1610 | |
| 1611 | #endif |