1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
27 #include <sys/ioctl.h>
33 #include <netinet/in.h>
36 #include <sys/socket.h>
41 #if HAVE_NETINET_TCP_H
42 #include <netinet/tcp.h>
45 #include <sys/ioctl.h>
58 #include <arpa/inet.h>
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t
;
73 /* A cache entry for a successfully looked-up symbol. */
78 struct sym_cache
*next
;
81 /* The symbol cache. */
82 static struct sym_cache
*symbol_cache
;
84 /* If this flag has been set, assume cache misses are
86 int all_symbols_looked_up
;
89 struct ui_file
*gdb_stdlog
;
91 static int remote_desc
;
93 /* FIXME headerize? */
94 extern int using_threads
;
95 extern int debug_threads
;
98 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
99 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
102 /* Open a connection to a remote debugger.
103 NAME is the filename used for communication. */
106 remote_open (char *name
)
108 #if defined(F_SETFL) && defined (FASYNC)
109 int save_fcntl_flags
;
113 port_str
= strchr (name
, ':');
114 if (port_str
== NULL
)
117 error ("Only <host>:<port> is supported on this platform.");
121 if (stat (name
, &statbuf
) == 0
122 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
123 remote_desc
= open (name
, O_RDWR
);
131 perror_with_name ("Could not open remote device");
135 struct termios termios
;
136 tcgetattr (remote_desc
, &termios
);
141 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
142 termios
.c_cflag
|= CLOCAL
| CS8
;
143 termios
.c_cc
[VMIN
] = 1;
144 termios
.c_cc
[VTIME
] = 0;
146 tcsetattr (remote_desc
, TCSANOW
, &termios
);
152 struct termio termio
;
153 ioctl (remote_desc
, TCGETA
, &termio
);
158 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
159 termio
.c_cflag
|= CLOCAL
| CS8
;
160 termio
.c_cc
[VMIN
] = 1;
161 termio
.c_cc
[VTIME
] = 0;
163 ioctl (remote_desc
, TCSETA
, &termio
);
171 ioctl (remote_desc
, TIOCGETP
, &sg
);
173 ioctl (remote_desc
, TIOCSETP
, &sg
);
177 fprintf (stderr
, "Remote debugging using %s\n", name
);
178 #endif /* USE_WIN32API */
183 static int winsock_initialized
;
187 struct sockaddr_in sockaddr
;
191 port_str
= strchr (name
, ':');
193 port
= atoi (port_str
+ 1);
196 if (!winsock_initialized
)
200 WSAStartup (MAKEWORD (1, 0), &wsad
);
201 winsock_initialized
= 1;
205 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
207 perror_with_name ("Can't open socket");
209 /* Allow rapid reuse of this port. */
211 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
214 sockaddr
.sin_family
= PF_INET
;
215 sockaddr
.sin_port
= htons (port
);
216 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
218 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
219 || listen (tmp_desc
, 1))
220 perror_with_name ("Can't bind address");
222 /* If port is zero, a random port will be selected, and the
223 fprintf below needs to know what port was selected. */
226 socklen_t len
= sizeof (sockaddr
);
227 if (getsockname (tmp_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
228 || len
< sizeof (sockaddr
))
229 perror_with_name ("Can't determine port");
230 port
= ntohs (sockaddr
.sin_port
);
233 fprintf (stderr
, "Listening on port %d\n", port
);
236 tmp
= sizeof (sockaddr
);
237 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
238 if (remote_desc
== -1)
239 perror_with_name ("Accept failed");
241 /* Enable TCP keep alive process. */
243 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
245 /* Tell TCP not to delay small packets. This greatly speeds up
246 interactive response. */
248 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
249 (char *) &tmp
, sizeof (tmp
));
253 close (tmp_desc
); /* No longer need this */
255 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
256 exits when the remote side dies. */
258 closesocket (tmp_desc
); /* No longer need this */
261 /* Convert IP address to string. */
262 fprintf (stderr
, "Remote debugging from host %s\n",
263 inet_ntoa (sockaddr
.sin_addr
));
266 #if defined(F_SETFL) && defined (FASYNC)
267 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
268 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
269 #if defined (F_SETOWN)
270 fcntl (remote_desc
, F_SETOWN
, getpid ());
280 closesocket (remote_desc
);
286 /* Convert hex digit A to a number. */
291 if (a
>= '0' && a
<= '9')
293 else if (a
>= 'a' && a
<= 'f')
296 error ("Reply contains invalid hex digit");
301 unhexify (char *bin
, const char *hex
, int count
)
305 for (i
= 0; i
< count
; i
++)
307 if (hex
[0] == 0 || hex
[1] == 0)
309 /* Hex string is short, or of uneven length.
310 Return the count that has been converted so far. */
313 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
320 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
327 for (i
= 0; i
< len
; i
++)
331 addr
= addr
| (fromhex (ch
) & 0x0f);
337 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
342 while (*end
!= '\0' && *end
!= ';')
345 decode_address (addrp
, start
, end
- start
);
352 /* Convert number NIB to a hex digit. */
360 return 'a' + nib
- 10;
364 hexify (char *hex
, const char *bin
, int count
)
368 /* May use a length, or a nul-terminated string as input. */
370 count
= strlen (bin
);
372 for (i
= 0; i
< count
; i
++)
374 *hex
++ = tohex ((*bin
>> 4) & 0xf);
375 *hex
++ = tohex (*bin
++ & 0xf);
381 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
382 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
383 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
384 (which may be more than *OUT_LEN due to escape characters). The
385 total number of bytes in the output buffer will be at most
389 remote_escape_output (const gdb_byte
*buffer
, int len
,
390 gdb_byte
*out_buf
, int *out_len
,
393 int input_index
, output_index
;
396 for (input_index
= 0; input_index
< len
; input_index
++)
398 gdb_byte b
= buffer
[input_index
];
400 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
402 /* These must be escaped. */
403 if (output_index
+ 2 > out_maxlen
)
405 out_buf
[output_index
++] = '}';
406 out_buf
[output_index
++] = b
^ 0x20;
410 if (output_index
+ 1 > out_maxlen
)
412 out_buf
[output_index
++] = b
;
416 *out_len
= input_index
;
420 /* Convert BUFFER, escaped data LEN bytes long, into binary data
421 in OUT_BUF. Return the number of bytes written to OUT_BUF.
422 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
424 This function reverses remote_escape_output. It allows more
425 escaped characters than that function does, in particular because
426 '*' must be escaped to avoid the run-length encoding processing
427 in reading packets. */
430 remote_unescape_input (const gdb_byte
*buffer
, int len
,
431 gdb_byte
*out_buf
, int out_maxlen
)
433 int input_index
, output_index
;
438 for (input_index
= 0; input_index
< len
; input_index
++)
440 gdb_byte b
= buffer
[input_index
];
442 if (output_index
+ 1 > out_maxlen
)
443 error ("Received too much data from the target.");
447 out_buf
[output_index
++] = b
^ 0x20;
453 out_buf
[output_index
++] = b
;
457 error ("Unmatched escape character in target response.");
462 /* Look for a sequence of characters which can be run-length encoded.
463 If there are any, update *CSUM and *P. Otherwise, output the
464 single character. Return the number of characters consumed. */
467 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
471 /* Always output the character. */
475 /* Don't go past '~'. */
479 for (n
= 1; n
< remaining
; n
++)
480 if (buf
[n
] != buf
[0])
483 /* N is the index of the first character not the same as buf[0].
484 buf[0] is counted twice, so by decrementing N, we get the number
485 of characters the RLE sequence will replace. */
491 /* Skip the frame characters. The manual says to skip '+' and '-'
492 also, but there's no reason to. Unfortunately these two unusable
493 characters double the encoded length of a four byte zero
495 while (n
+ 29 == '$' || n
+ 29 == '#')
506 /* Send a packet to the remote machine, with error checking.
507 The data of the packet is in BUF, and the length of the
508 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
511 putpkt_binary (char *buf
, int cnt
)
514 unsigned char csum
= 0;
519 buf2
= malloc (PBUFSIZ
);
521 /* Copy the packet into buffer BUF2, encapsulating it
522 and giving it a checksum. */
527 for (i
= 0; i
< cnt
;)
528 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
531 *p
++ = tohex ((csum
>> 4) & 0xf);
532 *p
++ = tohex (csum
& 0xf);
536 /* Send it over and over until we get a positive ack. */
542 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
544 perror ("putpkt(write)");
550 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
553 cc
= read (remote_desc
, buf3
, 1);
556 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
563 fprintf (stderr
, "putpkt(read): Got EOF\n");
565 perror ("putpkt(read)");
571 /* Check for an input interrupt while we're here. */
572 if (buf3
[0] == '\003')
573 (*the_target
->request_interrupt
) ();
575 while (buf3
[0] != '+');
578 return 1; /* Success! */
581 /* Send a packet to the remote machine, with error checking. The data
582 of the packet is in BUF, and the packet should be a NUL-terminated
583 string. Returns >= 0 on success, -1 otherwise. */
588 return putpkt_binary (buf
, strlen (buf
));
593 /* Come here when we get an input interrupt from the remote side. This
594 interrupt should only be active while we are waiting for the child to do
595 something. About the only thing that should come through is a ^C, which
596 will cause us to request child interruption. */
599 input_interrupt (int unused
)
602 struct timeval immediate
= { 0, 0 };
604 /* Protect against spurious interrupts. This has been observed to
605 be a problem under NetBSD 1.4 and 1.5. */
608 FD_SET (remote_desc
, &readset
);
609 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
614 cc
= read (remote_desc
, &c
, 1);
616 if (cc
!= 1 || c
!= '\003')
618 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
623 (*the_target
->request_interrupt
) ();
628 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
629 accept Control-C from the client, and must be disabled when talking to
633 block_async_io (void)
637 sigemptyset (&sigio_set
);
638 sigaddset (&sigio_set
, SIGIO
);
639 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
644 unblock_async_io (void)
648 sigemptyset (&sigio_set
);
649 sigaddset (&sigio_set
, SIGIO
);
650 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
654 /* Current state of asynchronous I/O. */
655 static int async_io_enabled
;
657 /* Enable asynchronous I/O. */
659 enable_async_io (void)
661 if (async_io_enabled
)
665 signal (SIGIO
, input_interrupt
);
667 async_io_enabled
= 1;
670 /* Disable asynchronous I/O. */
672 disable_async_io (void)
674 if (!async_io_enabled
)
678 signal (SIGIO
, SIG_IGN
);
680 async_io_enabled
= 0;
683 /* Returns next char from remote GDB. -1 if error. */
688 static unsigned char buf
[BUFSIZ
];
689 static int bufcnt
= 0;
690 static unsigned char *bufp
;
695 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
700 fprintf (stderr
, "readchar: Got EOF\n");
709 return *bufp
++ & 0x7f;
712 /* Read a packet from the remote machine, with error checking,
713 and store it in BUF. Returns length of packet, or negative if error. */
719 unsigned char csum
, c1
, c2
;
733 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
754 c1
= fromhex (readchar ());
755 c2
= fromhex (readchar ());
757 if (csum
== (c1
<< 4) + c2
)
760 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
761 (c1
<< 4) + c2
, csum
, buf
);
762 write (remote_desc
, "-", 1);
767 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
771 write (remote_desc
, "+", 1);
775 fprintf (stderr
, "[sent ack]\n");
791 write_enn (char *buf
)
793 /* Some day, we should define the meanings of the error codes... */
801 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
808 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
818 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
823 nib1
= fromhex (*from
++);
824 nib2
= fromhex (*from
++);
825 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
830 outreg (int regno
, char *buf
)
832 if ((regno
>> 12) != 0)
833 *buf
++ = tohex ((regno
>> 12) & 0xf);
834 if ((regno
>> 8) != 0)
835 *buf
++ = tohex ((regno
>> 8) & 0xf);
836 *buf
++ = tohex ((regno
>> 4) & 0xf);
837 *buf
++ = tohex (regno
& 0xf);
839 collect_register_as_string (regno
, buf
);
840 buf
+= 2 * register_size (regno
);
847 new_thread_notify (int id
)
851 /* The `n' response is not yet part of the remote protocol. Do nothing. */
855 if (server_waiting
== 0)
858 sprintf (own_buf
, "n%x", id
);
865 dead_thread_notify (int id
)
869 /* The `x' response is not yet part of the remote protocol. Do nothing. */
873 sprintf (own_buf
, "x%x", id
);
880 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
886 nib
= ((sig
& 0xf0) >> 4);
887 *buf
++ = tohex (nib
);
889 *buf
++ = tohex (nib
);
893 const char **regp
= gdbserver_expedite_regs
;
895 if (the_target
->stopped_by_watchpoint
!= NULL
896 && (*the_target
->stopped_by_watchpoint
) ())
901 strncpy (buf
, "watch:", 6);
904 addr
= (*the_target
->stopped_data_address
) ();
906 /* Convert each byte of the address into two hexadecimal chars.
907 Note that we take sizeof (void *) instead of sizeof (addr);
908 this is to avoid sending a 64-bit address to a 32-bit GDB. */
909 for (i
= sizeof (void *) * 2; i
> 0; i
--)
911 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
918 buf
= outreg (find_regno (*regp
), buf
);
922 /* Formerly, if the debugger had not used any thread features we would not
923 burden it with a thread status response. This was for the benefit of
924 GDB 4.13 and older. However, in recent GDB versions the check
925 (``if (cont_thread != 0)'') does not have the desired effect because of
926 sillyness in the way that the remote protocol handles specifying a thread.
927 Since thread support relies on qSymbol support anyway, assume GDB can handle
932 unsigned int gdb_id_from_wait
;
934 /* FIXME right place to set this? */
935 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
936 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
939 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
940 /* This if (1) ought to be unnecessary. But remote_wait in GDB
941 will claim this event belongs to inferior_ptid if we do not
942 specify a thread, and there's no way for gdbserver to know
943 what inferior_ptid is. */
944 if (1 || old_thread_from_wait
!= thread_from_wait
)
946 general_thread
= thread_from_wait
;
947 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
949 old_thread_from_wait
= thread_from_wait
;
953 /* For W and X, we're done. */
958 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
962 *mem_addr_ptr
= *len_ptr
= 0;
964 while ((ch
= from
[i
++]) != ',')
966 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
967 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
970 for (j
= 0; j
< 4; j
++)
972 if ((ch
= from
[i
++]) == 0)
974 *len_ptr
= *len_ptr
<< 4;
975 *len_ptr
|= fromhex (ch
) & 0x0f;
980 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
985 *mem_addr_ptr
= *len_ptr
= 0;
987 while ((ch
= from
[i
++]) != ',')
989 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
990 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
993 while ((ch
= from
[i
++]) != ':')
995 *len_ptr
= *len_ptr
<< 4;
996 *len_ptr
|= fromhex (ch
) & 0x0f;
999 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1003 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1004 unsigned int *len_ptr
, unsigned char *to
)
1008 *mem_addr_ptr
= *len_ptr
= 0;
1010 while ((ch
= from
[i
++]) != ',')
1012 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1013 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1016 while ((ch
= from
[i
++]) != ':')
1018 *len_ptr
= *len_ptr
<< 4;
1019 *len_ptr
|= fromhex (ch
) & 0x0f;
1022 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1023 to
, *len_ptr
) != *len_ptr
)
1029 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1030 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1033 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1035 char own_buf
[266], *p
, *q
;
1037 struct sym_cache
*sym
;
1039 /* Check the cache first. */
1040 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
1041 if (strcmp (name
, sym
->name
) == 0)
1047 /* If we've passed the call to thread_db_look_up_symbols, then
1048 anything not in the cache must not exist; we're not interested
1049 in any libraries loaded after that point, only in symbols in
1050 libpthread.so. It might not be an appropriate time to look
1051 up a symbol, e.g. while we're trying to fetch registers. */
1052 if (all_symbols_looked_up
)
1055 /* Send the request. */
1056 strcpy (own_buf
, "qSymbol:");
1057 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1058 if (putpkt (own_buf
) < 0)
1061 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1062 len
= getpkt (own_buf
);
1066 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1068 /* Malformed response. */
1071 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
1078 p
= own_buf
+ strlen ("qSymbol:");
1080 while (*q
&& *q
!= ':')
1083 /* Make sure we found a value for the symbol. */
1084 if (p
== q
|| *q
== '\0')
1087 decode_address (addrp
, p
, q
- p
);
1089 /* Save the symbol in our cache. */
1090 sym
= malloc (sizeof (*sym
));
1091 sym
->name
= strdup (name
);
1093 sym
->next
= symbol_cache
;
1100 monitor_output (const char *msg
)
1102 char *buf
= malloc (strlen (msg
) * 2 + 2);
1105 hexify (buf
+ 1, msg
, 0);
This page took 0.073442 seconds and 5 git commands to generate.