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
;
74 # define INVALID_DESCRIPTOR INVALID_SOCKET
76 # define INVALID_DESCRIPTOR -1
79 /* A cache entry for a successfully looked-up symbol. */
84 struct sym_cache
*next
;
87 /* The symbol cache. */
88 static struct sym_cache
*symbol_cache
;
90 /* If this flag has been set, assume cache misses are
92 int all_symbols_looked_up
;
95 struct ui_file
*gdb_stdlog
;
97 static int remote_desc
= INVALID_DESCRIPTOR
;
99 /* FIXME headerize? */
100 extern int using_threads
;
101 extern int debug_threads
;
104 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
105 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
108 /* Open a connection to a remote debugger.
109 NAME is the filename used for communication. */
112 remote_open (char *name
)
114 #if defined(F_SETFL) && defined (FASYNC)
115 int save_fcntl_flags
;
119 port_str
= strchr (name
, ':');
120 if (port_str
== NULL
)
123 error ("Only <host>:<port> is supported on this platform.");
127 if (stat (name
, &statbuf
) == 0
128 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
129 remote_desc
= open (name
, O_RDWR
);
137 perror_with_name ("Could not open remote device");
141 struct termios termios
;
142 tcgetattr (remote_desc
, &termios
);
147 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
148 termios
.c_cflag
|= CLOCAL
| CS8
;
149 termios
.c_cc
[VMIN
] = 1;
150 termios
.c_cc
[VTIME
] = 0;
152 tcsetattr (remote_desc
, TCSANOW
, &termios
);
158 struct termio termio
;
159 ioctl (remote_desc
, TCGETA
, &termio
);
164 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
165 termio
.c_cflag
|= CLOCAL
| CS8
;
166 termio
.c_cc
[VMIN
] = 1;
167 termio
.c_cc
[VTIME
] = 0;
169 ioctl (remote_desc
, TCSETA
, &termio
);
177 ioctl (remote_desc
, TIOCGETP
, &sg
);
179 ioctl (remote_desc
, TIOCSETP
, &sg
);
183 fprintf (stderr
, "Remote debugging using %s\n", name
);
184 #endif /* USE_WIN32API */
189 static int winsock_initialized
;
193 struct sockaddr_in sockaddr
;
197 port_str
= strchr (name
, ':');
199 port
= atoi (port_str
+ 1);
202 if (!winsock_initialized
)
206 WSAStartup (MAKEWORD (1, 0), &wsad
);
207 winsock_initialized
= 1;
211 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
213 perror_with_name ("Can't open socket");
215 /* Allow rapid reuse of this port. */
217 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
220 sockaddr
.sin_family
= PF_INET
;
221 sockaddr
.sin_port
= htons (port
);
222 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
224 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
225 || listen (tmp_desc
, 1))
226 perror_with_name ("Can't bind address");
228 /* If port is zero, a random port will be selected, and the
229 fprintf below needs to know what port was selected. */
232 socklen_t len
= sizeof (sockaddr
);
233 if (getsockname (tmp_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
234 || len
< sizeof (sockaddr
))
235 perror_with_name ("Can't determine port");
236 port
= ntohs (sockaddr
.sin_port
);
239 fprintf (stderr
, "Listening on port %d\n", port
);
242 tmp
= sizeof (sockaddr
);
243 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
244 if (remote_desc
== -1)
245 perror_with_name ("Accept failed");
247 /* Enable TCP keep alive process. */
249 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
251 /* Tell TCP not to delay small packets. This greatly speeds up
252 interactive response. */
254 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
255 (char *) &tmp
, sizeof (tmp
));
259 close (tmp_desc
); /* No longer need this */
261 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
262 exits when the remote side dies. */
264 closesocket (tmp_desc
); /* No longer need this */
267 /* Convert IP address to string. */
268 fprintf (stderr
, "Remote debugging from host %s\n",
269 inet_ntoa (sockaddr
.sin_addr
));
272 #if defined(F_SETFL) && defined (FASYNC)
273 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
274 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
275 #if defined (F_SETOWN)
276 fcntl (remote_desc
, F_SETOWN
, getpid ());
286 closesocket (remote_desc
);
292 /* Convert hex digit A to a number. */
297 if (a
>= '0' && a
<= '9')
299 else if (a
>= 'a' && a
<= 'f')
302 error ("Reply contains invalid hex digit");
307 unhexify (char *bin
, const char *hex
, int count
)
311 for (i
= 0; i
< count
; i
++)
313 if (hex
[0] == 0 || hex
[1] == 0)
315 /* Hex string is short, or of uneven length.
316 Return the count that has been converted so far. */
319 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
326 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
333 for (i
= 0; i
< len
; i
++)
337 addr
= addr
| (fromhex (ch
) & 0x0f);
343 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
348 while (*end
!= '\0' && *end
!= ';')
351 decode_address (addrp
, start
, end
- start
);
358 /* Convert number NIB to a hex digit. */
366 return 'a' + nib
- 10;
370 hexify (char *hex
, const char *bin
, int count
)
374 /* May use a length, or a nul-terminated string as input. */
376 count
= strlen (bin
);
378 for (i
= 0; i
< count
; i
++)
380 *hex
++ = tohex ((*bin
>> 4) & 0xf);
381 *hex
++ = tohex (*bin
++ & 0xf);
387 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
388 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
389 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
390 (which may be more than *OUT_LEN due to escape characters). The
391 total number of bytes in the output buffer will be at most
395 remote_escape_output (const gdb_byte
*buffer
, int len
,
396 gdb_byte
*out_buf
, int *out_len
,
399 int input_index
, output_index
;
402 for (input_index
= 0; input_index
< len
; input_index
++)
404 gdb_byte b
= buffer
[input_index
];
406 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
408 /* These must be escaped. */
409 if (output_index
+ 2 > out_maxlen
)
411 out_buf
[output_index
++] = '}';
412 out_buf
[output_index
++] = b
^ 0x20;
416 if (output_index
+ 1 > out_maxlen
)
418 out_buf
[output_index
++] = b
;
422 *out_len
= input_index
;
426 /* Convert BUFFER, escaped data LEN bytes long, into binary data
427 in OUT_BUF. Return the number of bytes written to OUT_BUF.
428 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
430 This function reverses remote_escape_output. It allows more
431 escaped characters than that function does, in particular because
432 '*' must be escaped to avoid the run-length encoding processing
433 in reading packets. */
436 remote_unescape_input (const gdb_byte
*buffer
, int len
,
437 gdb_byte
*out_buf
, int out_maxlen
)
439 int input_index
, output_index
;
444 for (input_index
= 0; input_index
< len
; input_index
++)
446 gdb_byte b
= buffer
[input_index
];
448 if (output_index
+ 1 > out_maxlen
)
449 error ("Received too much data from the target.");
453 out_buf
[output_index
++] = b
^ 0x20;
459 out_buf
[output_index
++] = b
;
463 error ("Unmatched escape character in target response.");
468 /* Look for a sequence of characters which can be run-length encoded.
469 If there are any, update *CSUM and *P. Otherwise, output the
470 single character. Return the number of characters consumed. */
473 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
477 /* Always output the character. */
481 /* Don't go past '~'. */
485 for (n
= 1; n
< remaining
; n
++)
486 if (buf
[n
] != buf
[0])
489 /* N is the index of the first character not the same as buf[0].
490 buf[0] is counted twice, so by decrementing N, we get the number
491 of characters the RLE sequence will replace. */
497 /* Skip the frame characters. The manual says to skip '+' and '-'
498 also, but there's no reason to. Unfortunately these two unusable
499 characters double the encoded length of a four byte zero
501 while (n
+ 29 == '$' || n
+ 29 == '#')
512 /* Send a packet to the remote machine, with error checking.
513 The data of the packet is in BUF, and the length of the
514 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
517 putpkt_binary (char *buf
, int cnt
)
520 unsigned char csum
= 0;
525 buf2
= malloc (PBUFSIZ
);
527 /* Copy the packet into buffer BUF2, encapsulating it
528 and giving it a checksum. */
533 for (i
= 0; i
< cnt
;)
534 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
537 *p
++ = tohex ((csum
>> 4) & 0xf);
538 *p
++ = tohex (csum
& 0xf);
542 /* Send it over and over until we get a positive ack. */
548 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
550 perror ("putpkt(write)");
556 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
559 cc
= read (remote_desc
, buf3
, 1);
562 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
569 fprintf (stderr
, "putpkt(read): Got EOF\n");
571 perror ("putpkt(read)");
577 /* Check for an input interrupt while we're here. */
578 if (buf3
[0] == '\003')
579 (*the_target
->request_interrupt
) ();
581 while (buf3
[0] != '+');
584 return 1; /* Success! */
587 /* Send a packet to the remote machine, with error checking. The data
588 of the packet is in BUF, and the packet should be a NUL-terminated
589 string. Returns >= 0 on success, -1 otherwise. */
594 return putpkt_binary (buf
, strlen (buf
));
597 /* Come here when we get an input interrupt from the remote side. This
598 interrupt should only be active while we are waiting for the child to do
599 something. About the only thing that should come through is a ^C, which
600 will cause us to request child interruption. */
603 input_interrupt (int unused
)
606 struct timeval immediate
= { 0, 0 };
608 /* Protect against spurious interrupts. This has been observed to
609 be a problem under NetBSD 1.4 and 1.5. */
612 FD_SET (remote_desc
, &readset
);
613 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
618 cc
= read (remote_desc
, &c
, 1);
620 if (cc
!= 1 || c
!= '\003')
622 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
627 (*the_target
->request_interrupt
) ();
631 /* Check if the remote side sent us an interrupt request (^C). */
633 check_remote_input_interrupt_request (void)
635 /* This function may be called before establishing communications,
636 therefore we need to validate the remote descriptor. */
638 if (remote_desc
== INVALID_DESCRIPTOR
)
644 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
645 accept Control-C from the client, and must be disabled when talking to
649 block_async_io (void)
653 sigemptyset (&sigio_set
);
654 sigaddset (&sigio_set
, SIGIO
);
655 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
660 unblock_async_io (void)
664 sigemptyset (&sigio_set
);
665 sigaddset (&sigio_set
, SIGIO
);
666 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
670 /* Current state of asynchronous I/O. */
671 static int async_io_enabled
;
673 /* Enable asynchronous I/O. */
675 enable_async_io (void)
677 if (async_io_enabled
)
681 signal (SIGIO
, input_interrupt
);
683 async_io_enabled
= 1;
686 /* Disable asynchronous I/O. */
688 disable_async_io (void)
690 if (!async_io_enabled
)
694 signal (SIGIO
, SIG_IGN
);
696 async_io_enabled
= 0;
699 /* Returns next char from remote GDB. -1 if error. */
704 static unsigned char buf
[BUFSIZ
];
705 static int bufcnt
= 0;
706 static unsigned char *bufp
;
711 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
716 fprintf (stderr
, "readchar: Got EOF\n");
725 return *bufp
++ & 0x7f;
728 /* Read a packet from the remote machine, with error checking,
729 and store it in BUF. Returns length of packet, or negative if error. */
735 unsigned char csum
, c1
, c2
;
749 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
770 c1
= fromhex (readchar ());
771 c2
= fromhex (readchar ());
773 if (csum
== (c1
<< 4) + c2
)
776 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
777 (c1
<< 4) + c2
, csum
, buf
);
778 write (remote_desc
, "-", 1);
783 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
787 write (remote_desc
, "+", 1);
791 fprintf (stderr
, "[sent ack]\n");
807 write_enn (char *buf
)
809 /* Some day, we should define the meanings of the error codes... */
817 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
824 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
834 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
839 nib1
= fromhex (*from
++);
840 nib2
= fromhex (*from
++);
841 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
846 outreg (int regno
, char *buf
)
848 if ((regno
>> 12) != 0)
849 *buf
++ = tohex ((regno
>> 12) & 0xf);
850 if ((regno
>> 8) != 0)
851 *buf
++ = tohex ((regno
>> 8) & 0xf);
852 *buf
++ = tohex ((regno
>> 4) & 0xf);
853 *buf
++ = tohex (regno
& 0xf);
855 collect_register_as_string (regno
, buf
);
856 buf
+= 2 * register_size (regno
);
863 new_thread_notify (int id
)
867 /* The `n' response is not yet part of the remote protocol. Do nothing. */
871 if (server_waiting
== 0)
874 sprintf (own_buf
, "n%x", id
);
881 dead_thread_notify (int id
)
885 /* The `x' response is not yet part of the remote protocol. Do nothing. */
889 sprintf (own_buf
, "x%x", id
);
896 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
902 nib
= ((sig
& 0xf0) >> 4);
903 *buf
++ = tohex (nib
);
905 *buf
++ = tohex (nib
);
909 const char **regp
= gdbserver_expedite_regs
;
911 if (the_target
->stopped_by_watchpoint
!= NULL
912 && (*the_target
->stopped_by_watchpoint
) ())
917 strncpy (buf
, "watch:", 6);
920 addr
= (*the_target
->stopped_data_address
) ();
922 /* Convert each byte of the address into two hexadecimal chars.
923 Note that we take sizeof (void *) instead of sizeof (addr);
924 this is to avoid sending a 64-bit address to a 32-bit GDB. */
925 for (i
= sizeof (void *) * 2; i
> 0; i
--)
927 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
934 buf
= outreg (find_regno (*regp
), buf
);
938 /* Formerly, if the debugger had not used any thread features we would not
939 burden it with a thread status response. This was for the benefit of
940 GDB 4.13 and older. However, in recent GDB versions the check
941 (``if (cont_thread != 0)'') does not have the desired effect because of
942 sillyness in the way that the remote protocol handles specifying a thread.
943 Since thread support relies on qSymbol support anyway, assume GDB can handle
948 unsigned int gdb_id_from_wait
;
950 /* FIXME right place to set this? */
951 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
952 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
955 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
956 /* This if (1) ought to be unnecessary. But remote_wait in GDB
957 will claim this event belongs to inferior_ptid if we do not
958 specify a thread, and there's no way for gdbserver to know
959 what inferior_ptid is. */
960 if (1 || old_thread_from_wait
!= thread_from_wait
)
962 general_thread
= thread_from_wait
;
963 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
965 old_thread_from_wait
= thread_from_wait
;
969 /* For W and X, we're done. */
974 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
978 *mem_addr_ptr
= *len_ptr
= 0;
980 while ((ch
= from
[i
++]) != ',')
982 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
983 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
986 for (j
= 0; j
< 4; j
++)
988 if ((ch
= from
[i
++]) == 0)
990 *len_ptr
= *len_ptr
<< 4;
991 *len_ptr
|= fromhex (ch
) & 0x0f;
996 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1001 *mem_addr_ptr
= *len_ptr
= 0;
1003 while ((ch
= from
[i
++]) != ',')
1005 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1006 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1009 while ((ch
= from
[i
++]) != ':')
1011 *len_ptr
= *len_ptr
<< 4;
1012 *len_ptr
|= fromhex (ch
) & 0x0f;
1015 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1019 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1020 unsigned int *len_ptr
, unsigned char *to
)
1024 *mem_addr_ptr
= *len_ptr
= 0;
1026 while ((ch
= from
[i
++]) != ',')
1028 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1029 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1032 while ((ch
= from
[i
++]) != ':')
1034 *len_ptr
= *len_ptr
<< 4;
1035 *len_ptr
|= fromhex (ch
) & 0x0f;
1038 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1039 to
, *len_ptr
) != *len_ptr
)
1045 /* Decode a qXfer write request. */
1047 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1048 unsigned int *len
, unsigned char *data
)
1052 /* Extract and NUL-terminate the annex. */
1054 while (*buf
&& *buf
!= ':')
1060 /* Extract the offset. */
1062 while ((ch
= *buf
++) != ':')
1064 *offset
= *offset
<< 4;
1065 *offset
|= fromhex (ch
) & 0x0f;
1068 /* Get encoded data. */
1069 packet_len
-= buf
- *annex
;
1070 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1075 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1076 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1079 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1081 char own_buf
[266], *p
, *q
;
1083 struct sym_cache
*sym
;
1085 /* Check the cache first. */
1086 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
1087 if (strcmp (name
, sym
->name
) == 0)
1093 /* If we've passed the call to thread_db_look_up_symbols, then
1094 anything not in the cache must not exist; we're not interested
1095 in any libraries loaded after that point, only in symbols in
1096 libpthread.so. It might not be an appropriate time to look
1097 up a symbol, e.g. while we're trying to fetch registers. */
1098 if (all_symbols_looked_up
)
1101 /* Send the request. */
1102 strcpy (own_buf
, "qSymbol:");
1103 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1104 if (putpkt (own_buf
) < 0)
1107 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1108 len
= getpkt (own_buf
);
1112 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1114 /* Malformed response. */
1117 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
1124 p
= own_buf
+ strlen ("qSymbol:");
1126 while (*q
&& *q
!= ':')
1129 /* Make sure we found a value for the symbol. */
1130 if (p
== q
|| *q
== '\0')
1133 decode_address (addrp
, p
, q
- p
);
1135 /* Save the symbol in our cache. */
1136 sym
= malloc (sizeof (*sym
));
1137 sym
->name
= strdup (name
);
1139 sym
->next
= symbol_cache
;
1146 monitor_output (const char *msg
)
1148 char *buf
= malloc (strlen (msg
) * 2 + 2);
1151 hexify (buf
+ 1, msg
, 0);
This page took 0.053573 seconds and 4 git commands to generate.