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