GDBserver
|
00001 /* Remote utility routines for the remote server for GDB. 00002 Copyright (C) 1986-2013 Free Software Foundation, Inc. 00003 00004 This file is part of GDB. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00018 00019 #include "server.h" 00020 #include "terminal.h" 00021 #include "target.h" 00022 #include "gdbthread.h" 00023 #include "tdesc.h" 00024 #include "dll.h" 00025 00026 #include <stdio.h> 00027 #include <string.h> 00028 #if HAVE_SYS_IOCTL_H 00029 #include <sys/ioctl.h> 00030 #endif 00031 #if HAVE_SYS_FILE_H 00032 #include <sys/file.h> 00033 #endif 00034 #if HAVE_NETINET_IN_H 00035 #include <netinet/in.h> 00036 #endif 00037 #if HAVE_SYS_SOCKET_H 00038 #include <sys/socket.h> 00039 #endif 00040 #if HAVE_NETDB_H 00041 #include <netdb.h> 00042 #endif 00043 #if HAVE_NETINET_TCP_H 00044 #include <netinet/tcp.h> 00045 #endif 00046 #if HAVE_SYS_IOCTL_H 00047 #include <sys/ioctl.h> 00048 #endif 00049 #if HAVE_SIGNAL_H 00050 #include <signal.h> 00051 #endif 00052 #if HAVE_FCNTL_H 00053 #include <fcntl.h> 00054 #endif 00055 #include <sys/time.h> 00056 #include <unistd.h> 00057 #if HAVE_ARPA_INET_H 00058 #include <arpa/inet.h> 00059 #endif 00060 #include "gdb_stat.h" 00061 #if HAVE_ERRNO_H 00062 #include <errno.h> 00063 #endif 00064 00065 #if USE_WIN32API 00066 #include <winsock2.h> 00067 #endif 00068 00069 #if __QNX__ 00070 #include <sys/iomgr.h> 00071 #endif /* __QNX__ */ 00072 00073 #ifndef HAVE_SOCKLEN_T 00074 typedef int socklen_t; 00075 #endif 00076 00077 #ifndef IN_PROCESS_AGENT 00078 00079 #if USE_WIN32API 00080 # define INVALID_DESCRIPTOR INVALID_SOCKET 00081 #else 00082 # define INVALID_DESCRIPTOR -1 00083 #endif 00084 00085 /* Extra value for readchar_callback. */ 00086 enum { 00087 /* The callback is currently not scheduled. */ 00088 NOT_SCHEDULED = -1 00089 }; 00090 00091 /* Status of the readchar callback. 00092 Either NOT_SCHEDULED or the callback id. */ 00093 static int readchar_callback = NOT_SCHEDULED; 00094 00095 static int readchar (void); 00096 static void reset_readchar (void); 00097 static void reschedule (void); 00098 00099 /* A cache entry for a successfully looked-up symbol. */ 00100 struct sym_cache 00101 { 00102 char *name; 00103 CORE_ADDR addr; 00104 struct sym_cache *next; 00105 }; 00106 00107 int remote_debug = 0; 00108 struct ui_file *gdb_stdlog; 00109 00110 static int remote_is_stdio = 0; 00111 00112 static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR; 00113 static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR; 00114 00115 /* FIXME headerize? */ 00116 extern int using_threads; 00117 extern int debug_threads; 00118 00119 /* If true, then GDB has requested noack mode. */ 00120 int noack_mode = 0; 00121 /* If true, then we tell GDB to use noack mode by default. */ 00122 int transport_is_reliable = 0; 00123 00124 #ifdef USE_WIN32API 00125 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0) 00126 # define write(fd, buf, len) send (fd, (char *) buf, len, 0) 00127 #endif 00128 00129 int 00130 gdb_connected (void) 00131 { 00132 return remote_desc != INVALID_DESCRIPTOR; 00133 } 00134 00135 /* Return true if the remote connection is over stdio. */ 00136 00137 int 00138 remote_connection_is_stdio (void) 00139 { 00140 return remote_is_stdio; 00141 } 00142 00143 static void 00144 enable_async_notification (int fd) 00145 { 00146 #if defined(F_SETFL) && defined (FASYNC) 00147 int save_fcntl_flags; 00148 00149 save_fcntl_flags = fcntl (fd, F_GETFL, 0); 00150 fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC); 00151 #if defined (F_SETOWN) 00152 fcntl (fd, F_SETOWN, getpid ()); 00153 #endif 00154 #endif 00155 } 00156 00157 static int 00158 handle_accept_event (int err, gdb_client_data client_data) 00159 { 00160 struct sockaddr_in sockaddr; 00161 socklen_t tmp; 00162 00163 if (debug_threads) 00164 fprintf (stderr, "handling possible accept event\n"); 00165 00166 tmp = sizeof (sockaddr); 00167 remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp); 00168 if (remote_desc == -1) 00169 perror_with_name ("Accept failed"); 00170 00171 /* Enable TCP keep alive process. */ 00172 tmp = 1; 00173 setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, 00174 (char *) &tmp, sizeof (tmp)); 00175 00176 /* Tell TCP not to delay small packets. This greatly speeds up 00177 interactive response. */ 00178 tmp = 1; 00179 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY, 00180 (char *) &tmp, sizeof (tmp)); 00181 00182 #ifndef USE_WIN32API 00183 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply 00184 exits when the remote side dies. */ 00185 #endif 00186 00187 if (run_once) 00188 { 00189 #ifndef USE_WIN32API 00190 close (listen_desc); /* No longer need this */ 00191 #else 00192 closesocket (listen_desc); /* No longer need this */ 00193 #endif 00194 } 00195 00196 /* Even if !RUN_ONCE no longer notice new connections. Still keep the 00197 descriptor open for add_file_handler to wait for a new connection. */ 00198 delete_file_handler (listen_desc); 00199 00200 /* Convert IP address to string. */ 00201 fprintf (stderr, "Remote debugging from host %s\n", 00202 inet_ntoa (sockaddr.sin_addr)); 00203 00204 enable_async_notification (remote_desc); 00205 00206 /* Register the event loop handler. */ 00207 add_file_handler (remote_desc, handle_serial_event, NULL); 00208 00209 /* We have a new GDB connection now. If we were disconnected 00210 tracing, there's a window where the target could report a stop 00211 event to the event loop, and since we have a connection now, we'd 00212 try to send vStopped notifications to GDB. But, don't do that 00213 until GDB as selected all-stop/non-stop, and has queried the 00214 threads' status ('?'). */ 00215 target_async (0); 00216 00217 return 0; 00218 } 00219 00220 /* Prepare for a later connection to a remote debugger. 00221 NAME is the filename used for communication. */ 00222 00223 void 00224 remote_prepare (char *name) 00225 { 00226 char *port_str; 00227 #ifdef USE_WIN32API 00228 static int winsock_initialized; 00229 #endif 00230 int port; 00231 struct sockaddr_in sockaddr; 00232 socklen_t tmp; 00233 char *port_end; 00234 00235 remote_is_stdio = 0; 00236 if (strcmp (name, STDIO_CONNECTION_NAME) == 0) 00237 { 00238 /* We need to record fact that we're using stdio sooner than the 00239 call to remote_open so start_inferior knows the connection is 00240 via stdio. */ 00241 remote_is_stdio = 1; 00242 transport_is_reliable = 1; 00243 return; 00244 } 00245 00246 port_str = strchr (name, ':'); 00247 if (port_str == NULL) 00248 { 00249 transport_is_reliable = 0; 00250 return; 00251 } 00252 00253 port = strtoul (port_str + 1, &port_end, 10); 00254 if (port_str[1] == '\0' || *port_end != '\0') 00255 fatal ("Bad port argument: %s", name); 00256 00257 #ifdef USE_WIN32API 00258 if (!winsock_initialized) 00259 { 00260 WSADATA wsad; 00261 00262 WSAStartup (MAKEWORD (1, 0), &wsad); 00263 winsock_initialized = 1; 00264 } 00265 #endif 00266 00267 listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 00268 if (listen_desc == -1) 00269 perror_with_name ("Can't open socket"); 00270 00271 /* Allow rapid reuse of this port. */ 00272 tmp = 1; 00273 setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, 00274 sizeof (tmp)); 00275 00276 sockaddr.sin_family = PF_INET; 00277 sockaddr.sin_port = htons (port); 00278 sockaddr.sin_addr.s_addr = INADDR_ANY; 00279 00280 if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) 00281 || listen (listen_desc, 1)) 00282 perror_with_name ("Can't bind address"); 00283 00284 transport_is_reliable = 1; 00285 } 00286 00287 /* Open a connection to a remote debugger. 00288 NAME is the filename used for communication. */ 00289 00290 void 00291 remote_open (char *name) 00292 { 00293 char *port_str; 00294 00295 port_str = strchr (name, ':'); 00296 #ifdef USE_WIN32API 00297 if (port_str == NULL) 00298 error ("Only <host>:<port> is supported on this platform."); 00299 #endif 00300 00301 if (strcmp (name, STDIO_CONNECTION_NAME) == 0) 00302 { 00303 fprintf (stderr, "Remote debugging using stdio\n"); 00304 00305 /* Use stdin as the handle of the connection. 00306 We only select on reads, for example. */ 00307 remote_desc = fileno (stdin); 00308 00309 enable_async_notification (remote_desc); 00310 00311 /* Register the event loop handler. */ 00312 add_file_handler (remote_desc, handle_serial_event, NULL); 00313 } 00314 #ifndef USE_WIN32API 00315 else if (port_str == NULL) 00316 { 00317 struct stat statbuf; 00318 00319 if (stat (name, &statbuf) == 0 00320 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) 00321 remote_desc = open (name, O_RDWR); 00322 else 00323 { 00324 errno = EINVAL; 00325 remote_desc = -1; 00326 } 00327 00328 if (remote_desc < 0) 00329 perror_with_name ("Could not open remote device"); 00330 00331 #ifdef HAVE_TERMIOS 00332 { 00333 struct termios termios; 00334 tcgetattr (remote_desc, &termios); 00335 00336 termios.c_iflag = 0; 00337 termios.c_oflag = 0; 00338 termios.c_lflag = 0; 00339 termios.c_cflag &= ~(CSIZE | PARENB); 00340 termios.c_cflag |= CLOCAL | CS8; 00341 termios.c_cc[VMIN] = 1; 00342 termios.c_cc[VTIME] = 0; 00343 00344 tcsetattr (remote_desc, TCSANOW, &termios); 00345 } 00346 #endif 00347 00348 #ifdef HAVE_TERMIO 00349 { 00350 struct termio termio; 00351 ioctl (remote_desc, TCGETA, &termio); 00352 00353 termio.c_iflag = 0; 00354 termio.c_oflag = 0; 00355 termio.c_lflag = 0; 00356 termio.c_cflag &= ~(CSIZE | PARENB); 00357 termio.c_cflag |= CLOCAL | CS8; 00358 termio.c_cc[VMIN] = 1; 00359 termio.c_cc[VTIME] = 0; 00360 00361 ioctl (remote_desc, TCSETA, &termio); 00362 } 00363 #endif 00364 00365 #ifdef HAVE_SGTTY 00366 { 00367 struct sgttyb sg; 00368 00369 ioctl (remote_desc, TIOCGETP, &sg); 00370 sg.sg_flags = RAW; 00371 ioctl (remote_desc, TIOCSETP, &sg); 00372 } 00373 #endif 00374 00375 fprintf (stderr, "Remote debugging using %s\n", name); 00376 00377 enable_async_notification (remote_desc); 00378 00379 /* Register the event loop handler. */ 00380 add_file_handler (remote_desc, handle_serial_event, NULL); 00381 } 00382 #endif /* USE_WIN32API */ 00383 else 00384 { 00385 int port; 00386 socklen_t len; 00387 struct sockaddr_in sockaddr; 00388 00389 len = sizeof (sockaddr); 00390 if (getsockname (listen_desc, 00391 (struct sockaddr *) &sockaddr, &len) < 0 00392 || len < sizeof (sockaddr)) 00393 perror_with_name ("Can't determine port"); 00394 port = ntohs (sockaddr.sin_port); 00395 00396 fprintf (stderr, "Listening on port %d\n", port); 00397 fflush (stderr); 00398 00399 /* Register the event loop handler. */ 00400 add_file_handler (listen_desc, handle_accept_event, NULL); 00401 } 00402 } 00403 00404 void 00405 remote_close (void) 00406 { 00407 delete_file_handler (remote_desc); 00408 00409 #ifdef USE_WIN32API 00410 closesocket (remote_desc); 00411 #else 00412 if (! remote_connection_is_stdio ()) 00413 close (remote_desc); 00414 #endif 00415 remote_desc = INVALID_DESCRIPTOR; 00416 00417 reset_readchar (); 00418 } 00419 00420 /* Convert hex digit A to a number. */ 00421 00422 static int 00423 fromhex (int a) 00424 { 00425 if (a >= '0' && a <= '9') 00426 return a - '0'; 00427 else if (a >= 'a' && a <= 'f') 00428 return a - 'a' + 10; 00429 else 00430 error ("Reply contains invalid hex digit"); 00431 return 0; 00432 } 00433 00434 #endif 00435 00436 static const char hexchars[] = "0123456789abcdef"; 00437 00438 static int 00439 ishex (int ch, int *val) 00440 { 00441 if ((ch >= 'a') && (ch <= 'f')) 00442 { 00443 *val = ch - 'a' + 10; 00444 return 1; 00445 } 00446 if ((ch >= 'A') && (ch <= 'F')) 00447 { 00448 *val = ch - 'A' + 10; 00449 return 1; 00450 } 00451 if ((ch >= '0') && (ch <= '9')) 00452 { 00453 *val = ch - '0'; 00454 return 1; 00455 } 00456 return 0; 00457 } 00458 00459 #ifndef IN_PROCESS_AGENT 00460 00461 int 00462 unhexify (char *bin, const char *hex, int count) 00463 { 00464 int i; 00465 00466 for (i = 0; i < count; i++) 00467 { 00468 if (hex[0] == 0 || hex[1] == 0) 00469 { 00470 /* Hex string is short, or of uneven length. 00471 Return the count that has been converted so far. */ 00472 return i; 00473 } 00474 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); 00475 hex += 2; 00476 } 00477 return i; 00478 } 00479 00480 void 00481 decode_address (CORE_ADDR *addrp, const char *start, int len) 00482 { 00483 CORE_ADDR addr; 00484 char ch; 00485 int i; 00486 00487 addr = 0; 00488 for (i = 0; i < len; i++) 00489 { 00490 ch = start[i]; 00491 addr = addr << 4; 00492 addr = addr | (fromhex (ch) & 0x0f); 00493 } 00494 *addrp = addr; 00495 } 00496 00497 const char * 00498 decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) 00499 { 00500 const char *end; 00501 00502 end = start; 00503 while (*end != '\0' && *end != ';') 00504 end++; 00505 00506 decode_address (addrp, start, end - start); 00507 00508 if (*end == ';') 00509 end++; 00510 return end; 00511 } 00512 00513 #endif 00514 00515 /* Convert number NIB to a hex digit. */ 00516 00517 static int 00518 tohex (int nib) 00519 { 00520 if (nib < 10) 00521 return '0' + nib; 00522 else 00523 return 'a' + nib - 10; 00524 } 00525 00526 #ifndef IN_PROCESS_AGENT 00527 00528 int 00529 hexify (char *hex, const char *bin, int count) 00530 { 00531 int i; 00532 00533 /* May use a length, or a nul-terminated string as input. */ 00534 if (count == 0) 00535 count = strlen (bin); 00536 00537 for (i = 0; i < count; i++) 00538 { 00539 *hex++ = tohex ((*bin >> 4) & 0xf); 00540 *hex++ = tohex (*bin++ & 0xf); 00541 } 00542 *hex = 0; 00543 return i; 00544 } 00545 00546 /* Convert BUFFER, binary data at least LEN bytes long, into escaped 00547 binary data in OUT_BUF. Set *OUT_LEN to the length of the data 00548 encoded in OUT_BUF, and return the number of bytes in OUT_BUF 00549 (which may be more than *OUT_LEN due to escape characters). The 00550 total number of bytes in the output buffer will be at most 00551 OUT_MAXLEN. */ 00552 00553 int 00554 remote_escape_output (const gdb_byte *buffer, int len, 00555 gdb_byte *out_buf, int *out_len, 00556 int out_maxlen) 00557 { 00558 int input_index, output_index; 00559 00560 output_index = 0; 00561 for (input_index = 0; input_index < len; input_index++) 00562 { 00563 gdb_byte b = buffer[input_index]; 00564 00565 if (b == '$' || b == '#' || b == '}' || b == '*') 00566 { 00567 /* These must be escaped. */ 00568 if (output_index + 2 > out_maxlen) 00569 break; 00570 out_buf[output_index++] = '}'; 00571 out_buf[output_index++] = b ^ 0x20; 00572 } 00573 else 00574 { 00575 if (output_index + 1 > out_maxlen) 00576 break; 00577 out_buf[output_index++] = b; 00578 } 00579 } 00580 00581 *out_len = input_index; 00582 return output_index; 00583 } 00584 00585 /* Convert BUFFER, escaped data LEN bytes long, into binary data 00586 in OUT_BUF. Return the number of bytes written to OUT_BUF. 00587 Raise an error if the total number of bytes exceeds OUT_MAXLEN. 00588 00589 This function reverses remote_escape_output. It allows more 00590 escaped characters than that function does, in particular because 00591 '*' must be escaped to avoid the run-length encoding processing 00592 in reading packets. */ 00593 00594 static int 00595 remote_unescape_input (const gdb_byte *buffer, int len, 00596 gdb_byte *out_buf, int out_maxlen) 00597 { 00598 int input_index, output_index; 00599 int escaped; 00600 00601 output_index = 0; 00602 escaped = 0; 00603 for (input_index = 0; input_index < len; input_index++) 00604 { 00605 gdb_byte b = buffer[input_index]; 00606 00607 if (output_index + 1 > out_maxlen) 00608 error ("Received too much data from the target."); 00609 00610 if (escaped) 00611 { 00612 out_buf[output_index++] = b ^ 0x20; 00613 escaped = 0; 00614 } 00615 else if (b == '}') 00616 escaped = 1; 00617 else 00618 out_buf[output_index++] = b; 00619 } 00620 00621 if (escaped) 00622 error ("Unmatched escape character in target response."); 00623 00624 return output_index; 00625 } 00626 00627 /* Look for a sequence of characters which can be run-length encoded. 00628 If there are any, update *CSUM and *P. Otherwise, output the 00629 single character. Return the number of characters consumed. */ 00630 00631 static int 00632 try_rle (char *buf, int remaining, unsigned char *csum, char **p) 00633 { 00634 int n; 00635 00636 /* Always output the character. */ 00637 *csum += buf[0]; 00638 *(*p)++ = buf[0]; 00639 00640 /* Don't go past '~'. */ 00641 if (remaining > 97) 00642 remaining = 97; 00643 00644 for (n = 1; n < remaining; n++) 00645 if (buf[n] != buf[0]) 00646 break; 00647 00648 /* N is the index of the first character not the same as buf[0]. 00649 buf[0] is counted twice, so by decrementing N, we get the number 00650 of characters the RLE sequence will replace. */ 00651 n--; 00652 00653 if (n < 3) 00654 return 1; 00655 00656 /* Skip the frame characters. The manual says to skip '+' and '-' 00657 also, but there's no reason to. Unfortunately these two unusable 00658 characters double the encoded length of a four byte zero 00659 value. */ 00660 while (n + 29 == '$' || n + 29 == '#') 00661 n--; 00662 00663 *csum += '*'; 00664 *(*p)++ = '*'; 00665 *csum += n + 29; 00666 *(*p)++ = n + 29; 00667 00668 return n + 1; 00669 } 00670 00671 #endif 00672 00673 char * 00674 unpack_varlen_hex (char *buff, /* packet to parse */ 00675 ULONGEST *result) 00676 { 00677 int nibble; 00678 ULONGEST retval = 0; 00679 00680 while (ishex (*buff, &nibble)) 00681 { 00682 buff++; 00683 retval = retval << 4; 00684 retval |= nibble & 0x0f; 00685 } 00686 *result = retval; 00687 return buff; 00688 } 00689 00690 #ifndef IN_PROCESS_AGENT 00691 00692 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */ 00693 00694 char * 00695 write_ptid (char *buf, ptid_t ptid) 00696 { 00697 int pid, tid; 00698 00699 if (multi_process) 00700 { 00701 pid = ptid_get_pid (ptid); 00702 if (pid < 0) 00703 buf += sprintf (buf, "p-%x.", -pid); 00704 else 00705 buf += sprintf (buf, "p%x.", pid); 00706 } 00707 tid = ptid_get_lwp (ptid); 00708 if (tid < 0) 00709 buf += sprintf (buf, "-%x", -tid); 00710 else 00711 buf += sprintf (buf, "%x", tid); 00712 00713 return buf; 00714 } 00715 00716 ULONGEST 00717 hex_or_minus_one (char *buf, char **obuf) 00718 { 00719 ULONGEST ret; 00720 00721 if (strncmp (buf, "-1", 2) == 0) 00722 { 00723 ret = (ULONGEST) -1; 00724 buf += 2; 00725 } 00726 else 00727 buf = unpack_varlen_hex (buf, &ret); 00728 00729 if (obuf) 00730 *obuf = buf; 00731 00732 return ret; 00733 } 00734 00735 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one 00736 passed the last parsed char. Returns null_ptid on error. */ 00737 ptid_t 00738 read_ptid (char *buf, char **obuf) 00739 { 00740 char *p = buf; 00741 char *pp; 00742 ULONGEST pid = 0, tid = 0; 00743 00744 if (*p == 'p') 00745 { 00746 /* Multi-process ptid. */ 00747 pp = unpack_varlen_hex (p + 1, &pid); 00748 if (*pp != '.') 00749 error ("invalid remote ptid: %s\n", p); 00750 00751 p = pp + 1; 00752 00753 tid = hex_or_minus_one (p, &pp); 00754 00755 if (obuf) 00756 *obuf = pp; 00757 return ptid_build (pid, tid, 0); 00758 } 00759 00760 /* No multi-process. Just a tid. */ 00761 tid = hex_or_minus_one (p, &pp); 00762 00763 /* Since the stub is not sending a process id, then default to 00764 what's in the current inferior. */ 00765 pid = ptid_get_pid (current_ptid); 00766 00767 if (obuf) 00768 *obuf = pp; 00769 return ptid_build (pid, tid, 0); 00770 } 00771 00772 /* Write COUNT bytes in BUF to the client. 00773 The result is the number of bytes written or -1 if error. 00774 This may return less than COUNT. */ 00775 00776 static int 00777 write_prim (const void *buf, int count) 00778 { 00779 if (remote_connection_is_stdio ()) 00780 return write (fileno (stdout), buf, count); 00781 else 00782 return write (remote_desc, buf, count); 00783 } 00784 00785 /* Read COUNT bytes from the client and store in BUF. 00786 The result is the number of bytes read or -1 if error. 00787 This may return less than COUNT. */ 00788 00789 static int 00790 read_prim (void *buf, int count) 00791 { 00792 if (remote_connection_is_stdio ()) 00793 return read (fileno (stdin), buf, count); 00794 else 00795 return read (remote_desc, buf, count); 00796 } 00797 00798 /* Send a packet to the remote machine, with error checking. 00799 The data of the packet is in BUF, and the length of the 00800 packet is in CNT. Returns >= 0 on success, -1 otherwise. */ 00801 00802 static int 00803 putpkt_binary_1 (char *buf, int cnt, int is_notif) 00804 { 00805 int i; 00806 unsigned char csum = 0; 00807 char *buf2; 00808 char *p; 00809 int cc; 00810 00811 buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1); 00812 00813 /* Copy the packet into buffer BUF2, encapsulating it 00814 and giving it a checksum. */ 00815 00816 p = buf2; 00817 if (is_notif) 00818 *p++ = '%'; 00819 else 00820 *p++ = '$'; 00821 00822 for (i = 0; i < cnt;) 00823 i += try_rle (buf + i, cnt - i, &csum, &p); 00824 00825 *p++ = '#'; 00826 *p++ = tohex ((csum >> 4) & 0xf); 00827 *p++ = tohex (csum & 0xf); 00828 00829 *p = '\0'; 00830 00831 /* Send it over and over until we get a positive ack. */ 00832 00833 do 00834 { 00835 if (write_prim (buf2, p - buf2) != p - buf2) 00836 { 00837 perror ("putpkt(write)"); 00838 free (buf2); 00839 return -1; 00840 } 00841 00842 if (noack_mode || is_notif) 00843 { 00844 /* Don't expect an ack then. */ 00845 if (remote_debug) 00846 { 00847 if (is_notif) 00848 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2); 00849 else 00850 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2); 00851 fflush (stderr); 00852 } 00853 break; 00854 } 00855 00856 if (remote_debug) 00857 { 00858 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2); 00859 fflush (stderr); 00860 } 00861 00862 cc = readchar (); 00863 00864 if (cc < 0) 00865 { 00866 free (buf2); 00867 return -1; 00868 } 00869 00870 if (remote_debug) 00871 { 00872 fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc); 00873 fflush (stderr); 00874 } 00875 00876 /* Check for an input interrupt while we're here. */ 00877 if (cc == '\003' && current_inferior != NULL) 00878 (*the_target->request_interrupt) (); 00879 } 00880 while (cc != '+'); 00881 00882 free (buf2); 00883 return 1; /* Success! */ 00884 } 00885 00886 int 00887 putpkt_binary (char *buf, int cnt) 00888 { 00889 return putpkt_binary_1 (buf, cnt, 0); 00890 } 00891 00892 /* Send a packet to the remote machine, with error checking. The data 00893 of the packet is in BUF, and the packet should be a NUL-terminated 00894 string. Returns >= 0 on success, -1 otherwise. */ 00895 00896 int 00897 putpkt (char *buf) 00898 { 00899 return putpkt_binary (buf, strlen (buf)); 00900 } 00901 00902 int 00903 putpkt_notif (char *buf) 00904 { 00905 return putpkt_binary_1 (buf, strlen (buf), 1); 00906 } 00907 00908 /* Come here when we get an input interrupt from the remote side. This 00909 interrupt should only be active while we are waiting for the child to do 00910 something. Thus this assumes readchar:bufcnt is 0. 00911 About the only thing that should come through is a ^C, which 00912 will cause us to request child interruption. */ 00913 00914 static void 00915 input_interrupt (int unused) 00916 { 00917 fd_set readset; 00918 struct timeval immediate = { 0, 0 }; 00919 00920 /* Protect against spurious interrupts. This has been observed to 00921 be a problem under NetBSD 1.4 and 1.5. */ 00922 00923 FD_ZERO (&readset); 00924 FD_SET (remote_desc, &readset); 00925 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) 00926 { 00927 int cc; 00928 char c = 0; 00929 00930 cc = read_prim (&c, 1); 00931 00932 if (cc != 1 || c != '\003' || current_inferior == NULL) 00933 { 00934 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n", 00935 cc, c, c); 00936 return; 00937 } 00938 00939 (*the_target->request_interrupt) (); 00940 } 00941 } 00942 00943 /* Check if the remote side sent us an interrupt request (^C). */ 00944 void 00945 check_remote_input_interrupt_request (void) 00946 { 00947 /* This function may be called before establishing communications, 00948 therefore we need to validate the remote descriptor. */ 00949 00950 if (remote_desc == INVALID_DESCRIPTOR) 00951 return; 00952 00953 input_interrupt (0); 00954 } 00955 00956 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to 00957 accept Control-C from the client, and must be disabled when talking to 00958 the client. */ 00959 00960 static void 00961 unblock_async_io (void) 00962 { 00963 #ifndef USE_WIN32API 00964 sigset_t sigio_set; 00965 00966 sigemptyset (&sigio_set); 00967 sigaddset (&sigio_set, SIGIO); 00968 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); 00969 #endif 00970 } 00971 00972 #ifdef __QNX__ 00973 static void 00974 nto_comctrl (int enable) 00975 { 00976 struct sigevent event; 00977 00978 if (enable) 00979 { 00980 event.sigev_notify = SIGEV_SIGNAL_THREAD; 00981 event.sigev_signo = SIGIO; 00982 event.sigev_code = 0; 00983 event.sigev_value.sival_ptr = NULL; 00984 event.sigev_priority = -1; 00985 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT, 00986 &event); 00987 } 00988 else 00989 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL); 00990 } 00991 #endif /* __QNX__ */ 00992 00993 00994 /* Current state of asynchronous I/O. */ 00995 static int async_io_enabled; 00996 00997 /* Enable asynchronous I/O. */ 00998 void 00999 enable_async_io (void) 01000 { 01001 if (async_io_enabled) 01002 return; 01003 01004 #ifndef USE_WIN32API 01005 signal (SIGIO, input_interrupt); 01006 #endif 01007 async_io_enabled = 1; 01008 #ifdef __QNX__ 01009 nto_comctrl (1); 01010 #endif /* __QNX__ */ 01011 } 01012 01013 /* Disable asynchronous I/O. */ 01014 void 01015 disable_async_io (void) 01016 { 01017 if (!async_io_enabled) 01018 return; 01019 01020 #ifndef USE_WIN32API 01021 signal (SIGIO, SIG_IGN); 01022 #endif 01023 async_io_enabled = 0; 01024 #ifdef __QNX__ 01025 nto_comctrl (0); 01026 #endif /* __QNX__ */ 01027 01028 } 01029 01030 void 01031 initialize_async_io (void) 01032 { 01033 /* Make sure that async I/O starts disabled. */ 01034 async_io_enabled = 1; 01035 disable_async_io (); 01036 01037 /* Make sure the signal is unblocked. */ 01038 unblock_async_io (); 01039 } 01040 01041 /* Internal buffer used by readchar. 01042 These are global to readchar because reschedule_remote needs to be 01043 able to tell whether the buffer is empty. */ 01044 01045 static unsigned char readchar_buf[BUFSIZ]; 01046 static int readchar_bufcnt = 0; 01047 static unsigned char *readchar_bufp; 01048 01049 /* Returns next char from remote GDB. -1 if error. */ 01050 01051 static int 01052 readchar (void) 01053 { 01054 int ch; 01055 01056 if (readchar_bufcnt == 0) 01057 { 01058 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf)); 01059 01060 if (readchar_bufcnt <= 0) 01061 { 01062 if (readchar_bufcnt == 0) 01063 fprintf (stderr, "readchar: Got EOF\n"); 01064 else 01065 perror ("readchar"); 01066 01067 return -1; 01068 } 01069 01070 readchar_bufp = readchar_buf; 01071 } 01072 01073 readchar_bufcnt--; 01074 ch = *readchar_bufp++; 01075 reschedule (); 01076 return ch; 01077 } 01078 01079 /* Reset the readchar state machine. */ 01080 01081 static void 01082 reset_readchar (void) 01083 { 01084 readchar_bufcnt = 0; 01085 if (readchar_callback != NOT_SCHEDULED) 01086 { 01087 delete_callback_event (readchar_callback); 01088 readchar_callback = NOT_SCHEDULED; 01089 } 01090 } 01091 01092 /* Process remaining data in readchar_buf. */ 01093 01094 static int 01095 process_remaining (void *context) 01096 { 01097 int res; 01098 01099 /* This is a one-shot event. */ 01100 readchar_callback = NOT_SCHEDULED; 01101 01102 if (readchar_bufcnt > 0) 01103 res = handle_serial_event (0, NULL); 01104 else 01105 res = 0; 01106 01107 return res; 01108 } 01109 01110 /* If there is still data in the buffer, queue another event to process it, 01111 we can't sleep in select yet. */ 01112 01113 static void 01114 reschedule (void) 01115 { 01116 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED) 01117 readchar_callback = append_callback_event (process_remaining, NULL); 01118 } 01119 01120 /* Read a packet from the remote machine, with error checking, 01121 and store it in BUF. Returns length of packet, or negative if error. */ 01122 01123 int 01124 getpkt (char *buf) 01125 { 01126 char *bp; 01127 unsigned char csum, c1, c2; 01128 int c; 01129 01130 while (1) 01131 { 01132 csum = 0; 01133 01134 while (1) 01135 { 01136 c = readchar (); 01137 if (c == '$') 01138 break; 01139 if (remote_debug) 01140 { 01141 fprintf (stderr, "[getpkt: discarding char '%c']\n", c); 01142 fflush (stderr); 01143 } 01144 01145 if (c < 0) 01146 return -1; 01147 } 01148 01149 bp = buf; 01150 while (1) 01151 { 01152 c = readchar (); 01153 if (c < 0) 01154 return -1; 01155 if (c == '#') 01156 break; 01157 *bp++ = c; 01158 csum += c; 01159 } 01160 *bp = 0; 01161 01162 c1 = fromhex (readchar ()); 01163 c2 = fromhex (readchar ()); 01164 01165 if (csum == (c1 << 4) + c2) 01166 break; 01167 01168 if (noack_mode) 01169 { 01170 fprintf (stderr, 01171 "Bad checksum, sentsum=0x%x, csum=0x%x, " 01172 "buf=%s [no-ack-mode, Bad medium?]\n", 01173 (c1 << 4) + c2, csum, buf); 01174 /* Not much we can do, GDB wasn't expecting an ack/nac. */ 01175 break; 01176 } 01177 01178 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", 01179 (c1 << 4) + c2, csum, buf); 01180 if (write_prim ("-", 1) != 1) 01181 return -1; 01182 } 01183 01184 if (!noack_mode) 01185 { 01186 if (remote_debug) 01187 { 01188 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); 01189 fflush (stderr); 01190 } 01191 01192 if (write_prim ("+", 1) != 1) 01193 return -1; 01194 01195 if (remote_debug) 01196 { 01197 fprintf (stderr, "[sent ack]\n"); 01198 fflush (stderr); 01199 } 01200 } 01201 else 01202 { 01203 if (remote_debug) 01204 { 01205 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf); 01206 fflush (stderr); 01207 } 01208 } 01209 01210 return bp - buf; 01211 } 01212 01213 void 01214 write_ok (char *buf) 01215 { 01216 buf[0] = 'O'; 01217 buf[1] = 'K'; 01218 buf[2] = '\0'; 01219 } 01220 01221 void 01222 write_enn (char *buf) 01223 { 01224 /* Some day, we should define the meanings of the error codes... */ 01225 buf[0] = 'E'; 01226 buf[1] = '0'; 01227 buf[2] = '1'; 01228 buf[3] = '\0'; 01229 } 01230 01231 #endif 01232 01233 void 01234 convert_int_to_ascii (const unsigned char *from, char *to, int n) 01235 { 01236 int nib; 01237 int ch; 01238 while (n--) 01239 { 01240 ch = *from++; 01241 nib = ((ch & 0xf0) >> 4) & 0x0f; 01242 *to++ = tohex (nib); 01243 nib = ch & 0x0f; 01244 *to++ = tohex (nib); 01245 } 01246 *to++ = 0; 01247 } 01248 01249 #ifndef IN_PROCESS_AGENT 01250 01251 void 01252 convert_ascii_to_int (const char *from, unsigned char *to, int n) 01253 { 01254 int nib1, nib2; 01255 while (n--) 01256 { 01257 nib1 = fromhex (*from++); 01258 nib2 = fromhex (*from++); 01259 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); 01260 } 01261 } 01262 01263 static char * 01264 outreg (struct regcache *regcache, int regno, char *buf) 01265 { 01266 if ((regno >> 12) != 0) 01267 *buf++ = tohex ((regno >> 12) & 0xf); 01268 if ((regno >> 8) != 0) 01269 *buf++ = tohex ((regno >> 8) & 0xf); 01270 *buf++ = tohex ((regno >> 4) & 0xf); 01271 *buf++ = tohex (regno & 0xf); 01272 *buf++ = ':'; 01273 collect_register_as_string (regcache, regno, buf); 01274 buf += 2 * register_size (regcache->tdesc, regno); 01275 *buf++ = ';'; 01276 01277 return buf; 01278 } 01279 01280 void 01281 new_thread_notify (int id) 01282 { 01283 char own_buf[256]; 01284 01285 /* The `n' response is not yet part of the remote protocol. Do nothing. */ 01286 if (1) 01287 return; 01288 01289 if (server_waiting == 0) 01290 return; 01291 01292 sprintf (own_buf, "n%x", id); 01293 disable_async_io (); 01294 putpkt (own_buf); 01295 enable_async_io (); 01296 } 01297 01298 void 01299 dead_thread_notify (int id) 01300 { 01301 char own_buf[256]; 01302 01303 /* The `x' response is not yet part of the remote protocol. Do nothing. */ 01304 if (1) 01305 return; 01306 01307 sprintf (own_buf, "x%x", id); 01308 disable_async_io (); 01309 putpkt (own_buf); 01310 enable_async_io (); 01311 } 01312 01313 void 01314 prepare_resume_reply (char *buf, ptid_t ptid, 01315 struct target_waitstatus *status) 01316 { 01317 if (debug_threads) 01318 fprintf (stderr, "Writing resume reply for %s:%d\n", 01319 target_pid_to_str (ptid), status->kind); 01320 01321 switch (status->kind) 01322 { 01323 case TARGET_WAITKIND_STOPPED: 01324 { 01325 struct thread_info *saved_inferior; 01326 const char **regp; 01327 struct regcache *regcache; 01328 01329 sprintf (buf, "T%02x", status->value.sig); 01330 buf += strlen (buf); 01331 01332 saved_inferior = current_inferior; 01333 01334 current_inferior = find_thread_ptid (ptid); 01335 01336 regp = current_target_desc ()->expedite_regs; 01337 01338 regcache = get_thread_regcache (current_inferior, 1); 01339 01340 if (the_target->stopped_by_watchpoint != NULL 01341 && (*the_target->stopped_by_watchpoint) ()) 01342 { 01343 CORE_ADDR addr; 01344 int i; 01345 01346 strncpy (buf, "watch:", 6); 01347 buf += 6; 01348 01349 addr = (*the_target->stopped_data_address) (); 01350 01351 /* Convert each byte of the address into two hexadecimal 01352 chars. Note that we take sizeof (void *) instead of 01353 sizeof (addr); this is to avoid sending a 64-bit 01354 address to a 32-bit GDB. */ 01355 for (i = sizeof (void *) * 2; i > 0; i--) 01356 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); 01357 *buf++ = ';'; 01358 } 01359 01360 while (*regp) 01361 { 01362 buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf); 01363 regp ++; 01364 } 01365 *buf = '\0'; 01366 01367 /* Formerly, if the debugger had not used any thread features 01368 we would not burden it with a thread status response. This 01369 was for the benefit of GDB 4.13 and older. However, in 01370 recent GDB versions the check (``if (cont_thread != 0)'') 01371 does not have the desired effect because of sillyness in 01372 the way that the remote protocol handles specifying a 01373 thread. Since thread support relies on qSymbol support 01374 anyway, assume GDB can handle threads. */ 01375 01376 if (using_threads && !disable_packet_Tthread) 01377 { 01378 /* This if (1) ought to be unnecessary. But remote_wait 01379 in GDB will claim this event belongs to inferior_ptid 01380 if we do not specify a thread, and there's no way for 01381 gdbserver to know what inferior_ptid is. */ 01382 if (1 || !ptid_equal (general_thread, ptid)) 01383 { 01384 int core = -1; 01385 /* In non-stop, don't change the general thread behind 01386 GDB's back. */ 01387 if (!non_stop) 01388 general_thread = ptid; 01389 sprintf (buf, "thread:"); 01390 buf += strlen (buf); 01391 buf = write_ptid (buf, ptid); 01392 strcat (buf, ";"); 01393 buf += strlen (buf); 01394 01395 core = target_core_of_thread (ptid); 01396 01397 if (core != -1) 01398 { 01399 sprintf (buf, "core:"); 01400 buf += strlen (buf); 01401 sprintf (buf, "%x", core); 01402 strcat (buf, ";"); 01403 buf += strlen (buf); 01404 } 01405 } 01406 } 01407 01408 if (dlls_changed) 01409 { 01410 strcpy (buf, "library:;"); 01411 buf += strlen (buf); 01412 dlls_changed = 0; 01413 } 01414 01415 current_inferior = saved_inferior; 01416 } 01417 break; 01418 case TARGET_WAITKIND_EXITED: 01419 if (multi_process) 01420 sprintf (buf, "W%x;process:%x", 01421 status->value.integer, ptid_get_pid (ptid)); 01422 else 01423 sprintf (buf, "W%02x", status->value.integer); 01424 break; 01425 case TARGET_WAITKIND_SIGNALLED: 01426 if (multi_process) 01427 sprintf (buf, "X%x;process:%x", 01428 status->value.sig, ptid_get_pid (ptid)); 01429 else 01430 sprintf (buf, "X%02x", status->value.sig); 01431 break; 01432 default: 01433 error ("unhandled waitkind"); 01434 break; 01435 } 01436 } 01437 01438 void 01439 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) 01440 { 01441 int i = 0, j = 0; 01442 char ch; 01443 *mem_addr_ptr = *len_ptr = 0; 01444 01445 while ((ch = from[i++]) != ',') 01446 { 01447 *mem_addr_ptr = *mem_addr_ptr << 4; 01448 *mem_addr_ptr |= fromhex (ch) & 0x0f; 01449 } 01450 01451 for (j = 0; j < 4; j++) 01452 { 01453 if ((ch = from[i++]) == 0) 01454 break; 01455 *len_ptr = *len_ptr << 4; 01456 *len_ptr |= fromhex (ch) & 0x0f; 01457 } 01458 } 01459 01460 void 01461 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, 01462 unsigned char **to_p) 01463 { 01464 int i = 0; 01465 char ch; 01466 *mem_addr_ptr = *len_ptr = 0; 01467 01468 while ((ch = from[i++]) != ',') 01469 { 01470 *mem_addr_ptr = *mem_addr_ptr << 4; 01471 *mem_addr_ptr |= fromhex (ch) & 0x0f; 01472 } 01473 01474 while ((ch = from[i++]) != ':') 01475 { 01476 *len_ptr = *len_ptr << 4; 01477 *len_ptr |= fromhex (ch) & 0x0f; 01478 } 01479 01480 if (*to_p == NULL) 01481 *to_p = xmalloc (*len_ptr); 01482 01483 convert_ascii_to_int (&from[i++], *to_p, *len_ptr); 01484 } 01485 01486 int 01487 decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, 01488 unsigned int *len_ptr, unsigned char **to_p) 01489 { 01490 int i = 0; 01491 char ch; 01492 *mem_addr_ptr = *len_ptr = 0; 01493 01494 while ((ch = from[i++]) != ',') 01495 { 01496 *mem_addr_ptr = *mem_addr_ptr << 4; 01497 *mem_addr_ptr |= fromhex (ch) & 0x0f; 01498 } 01499 01500 while ((ch = from[i++]) != ':') 01501 { 01502 *len_ptr = *len_ptr << 4; 01503 *len_ptr |= fromhex (ch) & 0x0f; 01504 } 01505 01506 if (*to_p == NULL) 01507 *to_p = xmalloc (*len_ptr); 01508 01509 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, 01510 *to_p, *len_ptr) != *len_ptr) 01511 return -1; 01512 01513 return 0; 01514 } 01515 01516 /* Decode a qXfer write request. */ 01517 01518 int 01519 decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset, 01520 unsigned int *len, unsigned char *data) 01521 { 01522 char ch; 01523 char *b = buf; 01524 01525 /* Extract the offset. */ 01526 *offset = 0; 01527 while ((ch = *buf++) != ':') 01528 { 01529 *offset = *offset << 4; 01530 *offset |= fromhex (ch) & 0x0f; 01531 } 01532 01533 /* Get encoded data. */ 01534 packet_len -= buf - b; 01535 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len, 01536 data, packet_len); 01537 return 0; 01538 } 01539 01540 /* Decode the parameters of a qSearch:memory packet. */ 01541 01542 int 01543 decode_search_memory_packet (const char *buf, int packet_len, 01544 CORE_ADDR *start_addrp, 01545 CORE_ADDR *search_space_lenp, 01546 gdb_byte *pattern, unsigned int *pattern_lenp) 01547 { 01548 const char *p = buf; 01549 01550 p = decode_address_to_semicolon (start_addrp, p); 01551 p = decode_address_to_semicolon (search_space_lenp, p); 01552 packet_len -= p - buf; 01553 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len, 01554 pattern, packet_len); 01555 return 0; 01556 } 01557 01558 static void 01559 free_sym_cache (struct sym_cache *sym) 01560 { 01561 if (sym != NULL) 01562 { 01563 free (sym->name); 01564 free (sym); 01565 } 01566 } 01567 01568 void 01569 clear_symbol_cache (struct sym_cache **symcache_p) 01570 { 01571 struct sym_cache *sym, *next; 01572 01573 /* Check the cache first. */ 01574 for (sym = *symcache_p; sym; sym = next) 01575 { 01576 next = sym->next; 01577 free_sym_cache (sym); 01578 } 01579 01580 *symcache_p = NULL; 01581 } 01582 01583 /* Get the address of NAME, and return it in ADDRP if found. if 01584 MAY_ASK_GDB is false, assume symbol cache misses are failures. 01585 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ 01586 01587 int 01588 look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb) 01589 { 01590 char own_buf[266], *p, *q; 01591 int len; 01592 struct sym_cache *sym; 01593 struct process_info *proc; 01594 01595 proc = current_process (); 01596 01597 /* Check the cache first. */ 01598 for (sym = proc->symbol_cache; sym; sym = sym->next) 01599 if (strcmp (name, sym->name) == 0) 01600 { 01601 *addrp = sym->addr; 01602 return 1; 01603 } 01604 01605 /* It might not be an appropriate time to look up a symbol, 01606 e.g. while we're trying to fetch registers. */ 01607 if (!may_ask_gdb) 01608 return 0; 01609 01610 /* Send the request. */ 01611 strcpy (own_buf, "qSymbol:"); 01612 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name)); 01613 if (putpkt (own_buf) < 0) 01614 return -1; 01615 01616 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ 01617 len = getpkt (own_buf); 01618 if (len < 0) 01619 return -1; 01620 01621 /* We ought to handle pretty much any packet at this point while we 01622 wait for the qSymbol "response". That requires re-entering the 01623 main loop. For now, this is an adequate approximation; allow 01624 GDB to read from memory while it figures out the address of the 01625 symbol. */ 01626 while (own_buf[0] == 'm') 01627 { 01628 CORE_ADDR mem_addr; 01629 unsigned char *mem_buf; 01630 unsigned int mem_len; 01631 01632 decode_m_packet (&own_buf[1], &mem_addr, &mem_len); 01633 mem_buf = xmalloc (mem_len); 01634 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) 01635 convert_int_to_ascii (mem_buf, own_buf, mem_len); 01636 else 01637 write_enn (own_buf); 01638 free (mem_buf); 01639 if (putpkt (own_buf) < 0) 01640 return -1; 01641 len = getpkt (own_buf); 01642 if (len < 0) 01643 return -1; 01644 } 01645 01646 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0) 01647 { 01648 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf); 01649 return -1; 01650 } 01651 01652 p = own_buf + strlen ("qSymbol:"); 01653 q = p; 01654 while (*q && *q != ':') 01655 q++; 01656 01657 /* Make sure we found a value for the symbol. */ 01658 if (p == q || *q == '\0') 01659 return 0; 01660 01661 decode_address (addrp, p, q - p); 01662 01663 /* Save the symbol in our cache. */ 01664 sym = xmalloc (sizeof (*sym)); 01665 sym->name = xstrdup (name); 01666 sym->addr = *addrp; 01667 sym->next = proc->symbol_cache; 01668 proc->symbol_cache = sym; 01669 01670 return 1; 01671 } 01672 01673 /* Relocate an instruction to execute at a different address. OLDLOC 01674 is the address in the inferior memory where the instruction to 01675 relocate is currently at. On input, TO points to the destination 01676 where we want the instruction to be copied (and possibly adjusted) 01677 to. On output, it points to one past the end of the resulting 01678 instruction(s). The effect of executing the instruction at TO 01679 shall be the same as if executing it at OLDLOC. For example, call 01680 instructions that implicitly push the return address on the stack 01681 should be adjusted to return to the instruction after OLDLOC; 01682 relative branches, and other PC-relative instructions need the 01683 offset adjusted; etc. Returns 0 on success, -1 on failure. */ 01684 01685 int 01686 relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc) 01687 { 01688 char own_buf[266]; 01689 int len; 01690 ULONGEST written = 0; 01691 01692 /* Send the request. */ 01693 strcpy (own_buf, "qRelocInsn:"); 01694 sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc), 01695 paddress (*to)); 01696 if (putpkt (own_buf) < 0) 01697 return -1; 01698 01699 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ 01700 len = getpkt (own_buf); 01701 if (len < 0) 01702 return -1; 01703 01704 /* We ought to handle pretty much any packet at this point while we 01705 wait for the qRelocInsn "response". That requires re-entering 01706 the main loop. For now, this is an adequate approximation; allow 01707 GDB to access memory. */ 01708 while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X') 01709 { 01710 CORE_ADDR mem_addr; 01711 unsigned char *mem_buf = NULL; 01712 unsigned int mem_len; 01713 01714 if (own_buf[0] == 'm') 01715 { 01716 decode_m_packet (&own_buf[1], &mem_addr, &mem_len); 01717 mem_buf = xmalloc (mem_len); 01718 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) 01719 convert_int_to_ascii (mem_buf, own_buf, mem_len); 01720 else 01721 write_enn (own_buf); 01722 } 01723 else if (own_buf[0] == 'X') 01724 { 01725 if (decode_X_packet (&own_buf[1], len - 1, &mem_addr, 01726 &mem_len, &mem_buf) < 0 01727 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0) 01728 write_enn (own_buf); 01729 else 01730 write_ok (own_buf); 01731 } 01732 else 01733 { 01734 decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf); 01735 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0) 01736 write_ok (own_buf); 01737 else 01738 write_enn (own_buf); 01739 } 01740 free (mem_buf); 01741 if (putpkt (own_buf) < 0) 01742 return -1; 01743 len = getpkt (own_buf); 01744 if (len < 0) 01745 return -1; 01746 } 01747 01748 if (own_buf[0] == 'E') 01749 { 01750 warning ("An error occurred while relocating an instruction: %s\n", 01751 own_buf); 01752 return -1; 01753 } 01754 01755 if (strncmp (own_buf, "qRelocInsn:", strlen ("qRelocInsn:")) != 0) 01756 { 01757 warning ("Malformed response to qRelocInsn, ignoring: %s\n", 01758 own_buf); 01759 return -1; 01760 } 01761 01762 unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written); 01763 01764 *to += written; 01765 return 0; 01766 } 01767 01768 void 01769 monitor_output (const char *msg) 01770 { 01771 char *buf = xmalloc (strlen (msg) * 2 + 2); 01772 01773 buf[0] = 'O'; 01774 hexify (buf + 1, msg, 0); 01775 01776 putpkt (buf); 01777 free (buf); 01778 } 01779 01780 #endif