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. */
29 #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>
52 #include <arpa/inet.h>
61 #ifndef HAVE_SOCKLEN_T
62 typedef int socklen_t
;
65 /* A cache entry for a successfully looked-up symbol. */
70 struct sym_cache
*next
;
73 /* The symbol cache. */
74 static struct sym_cache
*symbol_cache
;
76 /* If this flag has been set, assume cache misses are
78 int all_symbols_looked_up
;
81 struct ui_file
*gdb_stdlog
;
83 static int remote_desc
;
85 /* FIXME headerize? */
86 extern int using_threads
;
87 extern int debug_threads
;
90 # define read(fd, buf, len) recv (fd, buf, len, 0)
91 # define write(fd, buf, len) send (fd, buf, len, 0)
94 /* Open a connection to a remote debugger.
95 NAME is the filename used for communication. */
98 remote_open (char *name
)
100 #if defined(F_SETFL) && defined (FASYNC)
101 int save_fcntl_flags
;
105 port_str
= strchr (name
, ':');
106 if (port_str
== NULL
)
109 error ("Only <host>:<port> is supported on this platform.");
113 if (stat (name
, &statbuf
) == 0
114 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
115 remote_desc
= open (name
, O_RDWR
);
123 perror_with_name ("Could not open remote device");
127 struct termios termios
;
128 tcgetattr (remote_desc
, &termios
);
133 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
134 termios
.c_cflag
|= CLOCAL
| CS8
;
135 termios
.c_cc
[VMIN
] = 1;
136 termios
.c_cc
[VTIME
] = 0;
138 tcsetattr (remote_desc
, TCSANOW
, &termios
);
144 struct termio termio
;
145 ioctl (remote_desc
, TCGETA
, &termio
);
150 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
151 termio
.c_cflag
|= CLOCAL
| CS8
;
152 termio
.c_cc
[VMIN
] = 1;
153 termio
.c_cc
[VTIME
] = 0;
155 ioctl (remote_desc
, TCSETA
, &termio
);
163 ioctl (remote_desc
, TIOCGETP
, &sg
);
165 ioctl (remote_desc
, TIOCSETP
, &sg
);
169 fprintf (stderr
, "Remote debugging using %s\n", name
);
170 #endif /* USE_WIN32API */
175 static int winsock_initialized
;
179 struct sockaddr_in sockaddr
;
183 port_str
= strchr (name
, ':');
185 port
= atoi (port_str
+ 1);
188 if (!winsock_initialized
)
192 WSAStartup (MAKEWORD (1, 0), &wsad
);
193 winsock_initialized
= 1;
197 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
199 perror_with_name ("Can't open socket");
201 /* Allow rapid reuse of this port. */
203 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
206 sockaddr
.sin_family
= PF_INET
;
207 sockaddr
.sin_port
= htons (port
);
208 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
210 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
211 || listen (tmp_desc
, 1))
212 perror_with_name ("Can't bind address");
214 fprintf (stderr
, "Listening on port %d\n", port
);
217 tmp
= sizeof (sockaddr
);
218 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
219 if (remote_desc
== -1)
220 perror_with_name ("Accept failed");
222 /* Enable TCP keep alive process. */
224 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
226 /* Tell TCP not to delay small packets. This greatly speeds up
227 interactive response. */
229 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
230 (char *) &tmp
, sizeof (tmp
));
234 close (tmp_desc
); /* No longer need this */
236 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
237 exits when the remote side dies. */
239 closesocket (tmp_desc
); /* No longer need this */
242 /* Convert IP address to string. */
243 fprintf (stderr
, "Remote debugging from host %s\n",
244 inet_ntoa (sockaddr
.sin_addr
));
247 #if defined(F_SETFL) && defined (FASYNC)
248 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
249 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
250 #if defined (F_SETOWN)
251 fcntl (remote_desc
, F_SETOWN
, getpid ());
261 closesocket (remote_desc
);
267 /* Convert hex digit A to a number. */
272 if (a
>= '0' && a
<= '9')
274 else if (a
>= 'a' && a
<= 'f')
277 error ("Reply contains invalid hex digit");
282 unhexify (char *bin
, const char *hex
, int count
)
286 for (i
= 0; i
< count
; i
++)
288 if (hex
[0] == 0 || hex
[1] == 0)
290 /* Hex string is short, or of uneven length.
291 Return the count that has been converted so far. */
294 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
301 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
308 for (i
= 0; i
< len
; i
++)
312 addr
= addr
| (fromhex (ch
) & 0x0f);
318 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
323 while (*end
!= '\0' && *end
!= ';')
326 decode_address (addrp
, start
, end
- start
);
333 /* Convert number NIB to a hex digit. */
341 return 'a' + nib
- 10;
345 hexify (char *hex
, const char *bin
, int count
)
349 /* May use a length, or a nul-terminated string as input. */
351 count
= strlen (bin
);
353 for (i
= 0; i
< count
; i
++)
355 *hex
++ = tohex ((*bin
>> 4) & 0xf);
356 *hex
++ = tohex (*bin
++ & 0xf);
362 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
363 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
364 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
365 (which may be more than *OUT_LEN due to escape characters). The
366 total number of bytes in the output buffer will be at most
370 remote_escape_output (const gdb_byte
*buffer
, int len
,
371 gdb_byte
*out_buf
, int *out_len
,
374 int input_index
, output_index
;
377 for (input_index
= 0; input_index
< len
; input_index
++)
379 gdb_byte b
= buffer
[input_index
];
381 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
383 /* These must be escaped. */
384 if (output_index
+ 2 > out_maxlen
)
386 out_buf
[output_index
++] = '}';
387 out_buf
[output_index
++] = b
^ 0x20;
391 if (output_index
+ 1 > out_maxlen
)
393 out_buf
[output_index
++] = b
;
397 *out_len
= input_index
;
401 /* Convert BUFFER, escaped data LEN bytes long, into binary data
402 in OUT_BUF. Return the number of bytes written to OUT_BUF.
403 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
405 This function reverses remote_escape_output. It allows more
406 escaped characters than that function does, in particular because
407 '*' must be escaped to avoid the run-length encoding processing
408 in reading packets. */
411 remote_unescape_input (const gdb_byte
*buffer
, int len
,
412 gdb_byte
*out_buf
, int out_maxlen
)
414 int input_index
, output_index
;
419 for (input_index
= 0; input_index
< len
; input_index
++)
421 gdb_byte b
= buffer
[input_index
];
423 if (output_index
+ 1 > out_maxlen
)
424 error ("Received too much data from the target.");
428 out_buf
[output_index
++] = b
^ 0x20;
434 out_buf
[output_index
++] = b
;
438 error ("Unmatched escape character in target response.");
443 /* Look for a sequence of characters which can be run-length encoded.
444 If there are any, update *CSUM and *P. Otherwise, output the
445 single character. Return the number of characters consumed. */
448 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
452 /* Always output the character. */
456 /* Don't go past '~'. */
460 for (n
= 1; n
< remaining
; n
++)
461 if (buf
[n
] != buf
[0])
464 /* N is the index of the first character not the same as buf[0].
465 buf[0] is counted twice, so by decrementing N, we get the number
466 of characters the RLE sequence will replace. */
472 /* Skip the frame characters. The manual says to skip '+' and '-'
473 also, but there's no reason to. Unfortunately these two unusable
474 characters double the encoded length of a four byte zero
476 while (n
+ 29 == '$' || n
+ 29 == '#')
487 /* Send a packet to the remote machine, with error checking.
488 The data of the packet is in BUF, and the length of the
489 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
492 putpkt_binary (char *buf
, int cnt
)
495 unsigned char csum
= 0;
500 buf2
= malloc (PBUFSIZ
);
502 /* Copy the packet into buffer BUF2, encapsulating it
503 and giving it a checksum. */
508 for (i
= 0; i
< cnt
;)
509 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
512 *p
++ = tohex ((csum
>> 4) & 0xf);
513 *p
++ = tohex (csum
& 0xf);
517 /* Send it over and over until we get a positive ack. */
523 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
525 perror ("putpkt(write)");
531 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
534 cc
= read (remote_desc
, buf3
, 1);
537 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
544 fprintf (stderr
, "putpkt(read): Got EOF\n");
546 perror ("putpkt(read)");
552 /* Check for an input interrupt while we're here. */
553 if (buf3
[0] == '\003')
554 (*the_target
->send_signal
) (SIGINT
);
556 while (buf3
[0] != '+');
559 return 1; /* Success! */
562 /* Send a packet to the remote machine, with error checking. The data
563 of the packet is in BUF, and the packet should be a NUL-terminated
564 string. Returns >= 0 on success, -1 otherwise. */
569 return putpkt_binary (buf
, strlen (buf
));
574 /* Come here when we get an input interrupt from the remote side. This
575 interrupt should only be active while we are waiting for the child to do
576 something. About the only thing that should come through is a ^C, which
577 will cause us to send a SIGINT to the child. */
580 input_interrupt (int unused
)
583 struct timeval immediate
= { 0, 0 };
585 /* Protect against spurious interrupts. This has been observed to
586 be a problem under NetBSD 1.4 and 1.5. */
589 FD_SET (remote_desc
, &readset
);
590 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
595 cc
= read (remote_desc
, &c
, 1);
597 if (cc
!= 1 || c
!= '\003')
599 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
604 (*the_target
->send_signal
) (SIGINT
);
609 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
610 accept Control-C from the client, and must be disabled when talking to
614 block_async_io (void)
618 sigemptyset (&sigio_set
);
619 sigaddset (&sigio_set
, SIGIO
);
620 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
625 unblock_async_io (void)
629 sigemptyset (&sigio_set
);
630 sigaddset (&sigio_set
, SIGIO
);
631 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
635 /* Current state of asynchronous I/O. */
636 static int async_io_enabled
;
638 /* Enable asynchronous I/O. */
640 enable_async_io (void)
642 if (async_io_enabled
)
646 signal (SIGIO
, input_interrupt
);
648 async_io_enabled
= 1;
651 /* Disable asynchronous I/O. */
653 disable_async_io (void)
655 if (!async_io_enabled
)
659 signal (SIGIO
, SIG_IGN
);
661 async_io_enabled
= 0;
664 /* Returns next char from remote GDB. -1 if error. */
669 static unsigned char buf
[BUFSIZ
];
670 static int bufcnt
= 0;
671 static unsigned char *bufp
;
676 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
681 fprintf (stderr
, "readchar: Got EOF\n");
690 return *bufp
++ & 0x7f;
693 /* Read a packet from the remote machine, with error checking,
694 and store it in BUF. Returns length of packet, or negative if error. */
700 unsigned char csum
, c1
, c2
;
714 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
735 c1
= fromhex (readchar ());
736 c2
= fromhex (readchar ());
738 if (csum
== (c1
<< 4) + c2
)
741 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
742 (c1
<< 4) + c2
, csum
, buf
);
743 write (remote_desc
, "-", 1);
748 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
752 write (remote_desc
, "+", 1);
756 fprintf (stderr
, "[sent ack]\n");
772 write_enn (char *buf
)
774 /* Some day, we should define the meanings of the error codes... */
782 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
789 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
799 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
804 nib1
= fromhex (*from
++);
805 nib2
= fromhex (*from
++);
806 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
811 outreg (int regno
, char *buf
)
813 if ((regno
>> 12) != 0)
814 *buf
++ = tohex ((regno
>> 12) & 0xf);
815 if ((regno
>> 8) != 0)
816 *buf
++ = tohex ((regno
>> 8) & 0xf);
817 *buf
++ = tohex ((regno
>> 4) & 0xf);
818 *buf
++ = tohex (regno
& 0xf);
820 collect_register_as_string (regno
, buf
);
821 buf
+= 2 * register_size (regno
);
828 new_thread_notify (int id
)
832 /* The `n' response is not yet part of the remote protocol. Do nothing. */
836 if (server_waiting
== 0)
839 sprintf (own_buf
, "n%x", id
);
846 dead_thread_notify (int id
)
850 /* The `x' response is not yet part of the remote protocol. Do nothing. */
854 sprintf (own_buf
, "x%x", id
);
861 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
867 nib
= ((sig
& 0xf0) >> 4);
868 *buf
++ = tohex (nib
);
870 *buf
++ = tohex (nib
);
874 const char **regp
= gdbserver_expedite_regs
;
876 if (the_target
->stopped_by_watchpoint
!= NULL
877 && (*the_target
->stopped_by_watchpoint
) ())
882 strncpy (buf
, "watch:", 6);
885 addr
= (*the_target
->stopped_data_address
) ();
887 /* Convert each byte of the address into two hexadecimal chars.
888 Note that we take sizeof (void *) instead of sizeof (addr);
889 this is to avoid sending a 64-bit address to a 32-bit GDB. */
890 for (i
= sizeof (void *) * 2; i
> 0; i
--)
892 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
899 buf
= outreg (find_regno (*regp
), buf
);
903 /* Formerly, if the debugger had not used any thread features we would not
904 burden it with a thread status response. This was for the benefit of
905 GDB 4.13 and older. However, in recent GDB versions the check
906 (``if (cont_thread != 0)'') does not have the desired effect because of
907 sillyness in the way that the remote protocol handles specifying a thread.
908 Since thread support relies on qSymbol support anyway, assume GDB can handle
913 unsigned int gdb_id_from_wait
;
915 /* FIXME right place to set this? */
916 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
917 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
920 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
921 /* This if (1) ought to be unnecessary. But remote_wait in GDB
922 will claim this event belongs to inferior_ptid if we do not
923 specify a thread, and there's no way for gdbserver to know
924 what inferior_ptid is. */
925 if (1 || old_thread_from_wait
!= thread_from_wait
)
927 general_thread
= thread_from_wait
;
928 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
930 old_thread_from_wait
= thread_from_wait
;
934 /* For W and X, we're done. */
939 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
943 *mem_addr_ptr
= *len_ptr
= 0;
945 while ((ch
= from
[i
++]) != ',')
947 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
948 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
951 for (j
= 0; j
< 4; j
++)
953 if ((ch
= from
[i
++]) == 0)
955 *len_ptr
= *len_ptr
<< 4;
956 *len_ptr
|= fromhex (ch
) & 0x0f;
961 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
966 *mem_addr_ptr
= *len_ptr
= 0;
968 while ((ch
= from
[i
++]) != ',')
970 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
971 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
974 while ((ch
= from
[i
++]) != ':')
976 *len_ptr
= *len_ptr
<< 4;
977 *len_ptr
|= fromhex (ch
) & 0x0f;
980 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
984 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
985 unsigned int *len_ptr
, unsigned char *to
)
989 *mem_addr_ptr
= *len_ptr
= 0;
991 while ((ch
= from
[i
++]) != ',')
993 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
994 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
997 while ((ch
= from
[i
++]) != ':')
999 *len_ptr
= *len_ptr
<< 4;
1000 *len_ptr
|= fromhex (ch
) & 0x0f;
1003 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1004 to
, *len_ptr
) != *len_ptr
)
1010 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1011 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1014 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1016 char own_buf
[266], *p
, *q
;
1018 struct sym_cache
*sym
;
1020 /* Check the cache first. */
1021 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
1022 if (strcmp (name
, sym
->name
) == 0)
1028 /* If we've passed the call to thread_db_look_up_symbols, then
1029 anything not in the cache must not exist; we're not interested
1030 in any libraries loaded after that point, only in symbols in
1031 libpthread.so. It might not be an appropriate time to look
1032 up a symbol, e.g. while we're trying to fetch registers. */
1033 if (all_symbols_looked_up
)
1036 /* Send the request. */
1037 strcpy (own_buf
, "qSymbol:");
1038 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1039 if (putpkt (own_buf
) < 0)
1042 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1043 len
= getpkt (own_buf
);
1047 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1049 /* Malformed response. */
1052 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
1059 p
= own_buf
+ strlen ("qSymbol:");
1061 while (*q
&& *q
!= ':')
1064 /* Make sure we found a value for the symbol. */
1065 if (p
== q
|| *q
== '\0')
1068 decode_address (addrp
, p
, q
- p
);
1070 /* Save the symbol in our cache. */
1071 sym
= malloc (sizeof (*sym
));
1072 sym
->name
= strdup (name
);
1074 sym
->next
= symbol_cache
;
This page took 0.062863 seconds and 5 git commands to generate.