1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
30 #include <sys/ioctl.h>
34 #include <netinet/in.h>
37 #include <sys/socket.h>
42 #if HAVE_NETINET_TCP_H
43 #include <netinet/tcp.h>
46 #include <sys/ioctl.h>
53 #include <arpa/inet.h>
60 #ifndef HAVE_SOCKLEN_T
61 typedef int socklen_t
;
64 /* A cache entry for a successfully looked-up symbol. */
69 struct sym_cache
*next
;
72 /* The symbol cache. */
73 static struct sym_cache
*symbol_cache
;
75 /* If this flag has been set, assume cache misses are
77 int all_symbols_looked_up
;
80 struct ui_file
*gdb_stdlog
;
82 static int remote_desc
;
84 /* FIXME headerize? */
85 extern int using_threads
;
86 extern int debug_threads
;
88 /* Open a connection to a remote debugger.
89 NAME is the filename used for communication. */
92 remote_open (char *name
)
94 #if defined(F_SETFL) && defined (FASYNC)
98 if (!strchr (name
, ':'))
101 error ("Only <host>:<port> is supported on this platform.");
103 remote_desc
= open (name
, O_RDWR
);
105 perror_with_name ("Could not open remote device");
109 struct termios termios
;
110 tcgetattr (remote_desc
, &termios
);
115 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
116 termios
.c_cflag
|= CLOCAL
| CS8
;
117 termios
.c_cc
[VMIN
] = 1;
118 termios
.c_cc
[VTIME
] = 0;
120 tcsetattr (remote_desc
, TCSANOW
, &termios
);
126 struct termio termio
;
127 ioctl (remote_desc
, TCGETA
, &termio
);
132 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
133 termio
.c_cflag
|= CLOCAL
| CS8
;
134 termio
.c_cc
[VMIN
] = 1;
135 termio
.c_cc
[VTIME
] = 0;
137 ioctl (remote_desc
, TCSETA
, &termio
);
145 ioctl (remote_desc
, TIOCGETP
, &sg
);
147 ioctl (remote_desc
, TIOCSETP
, &sg
);
151 fprintf (stderr
, "Remote debugging using %s\n", name
);
152 #endif /* USE_WIN32API */
157 static int winsock_initialized
;
161 struct sockaddr_in sockaddr
;
165 port_str
= strchr (name
, ':');
167 port
= atoi (port_str
+ 1);
170 if (!winsock_initialized
)
174 WSAStartup (MAKEWORD (1, 0), &wsad
);
175 winsock_initialized
= 1;
179 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
181 perror_with_name ("Can't open socket");
183 /* Allow rapid reuse of this port. */
185 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
188 sockaddr
.sin_family
= PF_INET
;
189 sockaddr
.sin_port
= htons (port
);
190 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
192 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
193 || listen (tmp_desc
, 1))
194 perror_with_name ("Can't bind address");
196 fprintf (stderr
, "Listening on port %d\n", port
);
199 tmp
= sizeof (sockaddr
);
200 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
201 if (remote_desc
== -1)
202 perror_with_name ("Accept failed");
204 /* Enable TCP keep alive process. */
206 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
208 /* Tell TCP not to delay small packets. This greatly speeds up
209 interactive response. */
211 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
212 (char *) &tmp
, sizeof (tmp
));
216 close (tmp_desc
); /* No longer need this */
218 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
219 exits when the remote side dies. */
221 closesocket (tmp_desc
); /* No longer need this */
224 /* Convert IP address to string. */
225 fprintf (stderr
, "Remote debugging from host %s\n",
226 inet_ntoa (sockaddr
.sin_addr
));
229 #if defined(F_SETFL) && defined (FASYNC)
230 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
231 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
232 #if defined (F_SETOWN)
233 fcntl (remote_desc
, F_SETOWN
, getpid ());
243 closesocket (remote_desc
);
249 /* Convert hex digit A to a number. */
254 if (a
>= '0' && a
<= '9')
256 else if (a
>= 'a' && a
<= 'f')
259 error ("Reply contains invalid hex digit");
264 unhexify (char *bin
, const char *hex
, int count
)
268 for (i
= 0; i
< count
; i
++)
270 if (hex
[0] == 0 || hex
[1] == 0)
272 /* Hex string is short, or of uneven length.
273 Return the count that has been converted so far. */
276 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
283 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
290 for (i
= 0; i
< len
; i
++)
294 addr
= addr
| (fromhex (ch
) & 0x0f);
299 /* Convert number NIB to a hex digit. */
307 return 'a' + nib
- 10;
311 hexify (char *hex
, const char *bin
, int count
)
315 /* May use a length, or a nul-terminated string as input. */
317 count
= strlen (bin
);
319 for (i
= 0; i
< count
; i
++)
321 *hex
++ = tohex ((*bin
>> 4) & 0xf);
322 *hex
++ = tohex (*bin
++ & 0xf);
328 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
329 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
330 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
331 (which may be more than *OUT_LEN due to escape characters). The
332 total number of bytes in the output buffer will be at most
336 remote_escape_output (const gdb_byte
*buffer
, int len
,
337 gdb_byte
*out_buf
, int *out_len
,
340 int input_index
, output_index
;
343 for (input_index
= 0; input_index
< len
; input_index
++)
345 gdb_byte b
= buffer
[input_index
];
347 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
349 /* These must be escaped. */
350 if (output_index
+ 2 > out_maxlen
)
352 out_buf
[output_index
++] = '}';
353 out_buf
[output_index
++] = b
^ 0x20;
357 if (output_index
+ 1 > out_maxlen
)
359 out_buf
[output_index
++] = b
;
363 *out_len
= input_index
;
367 /* Convert BUFFER, escaped data LEN bytes long, into binary data
368 in OUT_BUF. Return the number of bytes written to OUT_BUF.
369 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
371 This function reverses remote_escape_output. It allows more
372 escaped characters than that function does, in particular because
373 '*' must be escaped to avoid the run-length encoding processing
374 in reading packets. */
377 remote_unescape_input (const gdb_byte
*buffer
, int len
,
378 gdb_byte
*out_buf
, int out_maxlen
)
380 int input_index
, output_index
;
385 for (input_index
= 0; input_index
< len
; input_index
++)
387 gdb_byte b
= buffer
[input_index
];
389 if (output_index
+ 1 > out_maxlen
)
390 error ("Received too much data from the target.");
394 out_buf
[output_index
++] = b
^ 0x20;
400 out_buf
[output_index
++] = b
;
404 error ("Unmatched escape character in target response.");
409 /* Look for a sequence of characters which can be run-length encoded.
410 If there are any, update *CSUM and *P. Otherwise, output the
411 single character. Return the number of characters consumed. */
414 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
418 /* Always output the character. */
422 /* Don't go past '~'. */
426 for (n
= 1; n
< remaining
; n
++)
427 if (buf
[n
] != buf
[0])
430 /* N is the index of the first character not the same as buf[0].
431 buf[0] is counted twice, so by decrementing N, we get the number
432 of characters the RLE sequence will replace. */
438 /* Skip the frame characters. The manual says to skip '+' and '-'
439 also, but there's no reason to. Unfortunately these two unusable
440 characters double the encoded length of a four byte zero
442 while (n
+ 29 == '$' || n
+ 29 == '#')
453 /* Send a packet to the remote machine, with error checking.
454 The data of the packet is in BUF, and the length of the
455 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
458 putpkt_binary (char *buf
, int cnt
)
461 unsigned char csum
= 0;
466 buf2
= malloc (PBUFSIZ
);
468 /* Copy the packet into buffer BUF2, encapsulating it
469 and giving it a checksum. */
474 for (i
= 0; i
< cnt
;)
475 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
478 *p
++ = tohex ((csum
>> 4) & 0xf);
479 *p
++ = tohex (csum
& 0xf);
483 /* Send it over and over until we get a positive ack. */
489 if (send (remote_desc
, buf2
, p
- buf2
, 0) != p
- buf2
)
491 perror ("putpkt(write)");
497 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
500 cc
= recv (remote_desc
, buf3
, 1, 0);
503 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
510 fprintf (stderr
, "putpkt(read): Got EOF\n");
512 perror ("putpkt(read)");
518 /* Check for an input interrupt while we're here. */
519 if (buf3
[0] == '\003')
520 (*the_target
->send_signal
) (SIGINT
);
522 while (buf3
[0] != '+');
525 return 1; /* Success! */
528 /* Send a packet to the remote machine, with error checking. The data
529 of the packet is in BUF, and the packet should be a NUL-terminated
530 string. Returns >= 0 on success, -1 otherwise. */
535 return putpkt_binary (buf
, strlen (buf
));
540 /* Come here when we get an input interrupt from the remote side. This
541 interrupt should only be active while we are waiting for the child to do
542 something. About the only thing that should come through is a ^C, which
543 will cause us to send a SIGINT to the child. */
546 input_interrupt (int unused
)
549 struct timeval immediate
= { 0, 0 };
551 /* Protect against spurious interrupts. This has been observed to
552 be a problem under NetBSD 1.4 and 1.5. */
555 FD_SET (remote_desc
, &readset
);
556 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
561 cc
= recv (remote_desc
, &c
, 1, 0);
563 if (cc
!= 1 || c
!= '\003')
565 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
570 (*the_target
->send_signal
) (SIGINT
);
575 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
576 accept Control-C from the client, and must be disabled when talking to
580 block_async_io (void)
584 sigemptyset (&sigio_set
);
585 sigaddset (&sigio_set
, SIGIO
);
586 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
591 unblock_async_io (void)
595 sigemptyset (&sigio_set
);
596 sigaddset (&sigio_set
, SIGIO
);
597 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
601 /* Current state of asynchronous I/O. */
602 static int async_io_enabled
;
604 /* Enable asynchronous I/O. */
606 enable_async_io (void)
608 if (async_io_enabled
)
612 signal (SIGIO
, input_interrupt
);
614 async_io_enabled
= 1;
617 /* Disable asynchronous I/O. */
619 disable_async_io (void)
621 if (!async_io_enabled
)
625 signal (SIGIO
, SIG_IGN
);
627 async_io_enabled
= 0;
630 /* Returns next char from remote GDB. -1 if error. */
635 static unsigned char buf
[BUFSIZ
];
636 static int bufcnt
= 0;
637 static unsigned char *bufp
;
642 bufcnt
= recv (remote_desc
, buf
, sizeof (buf
), 0);
647 fprintf (stderr
, "readchar: Got EOF\n");
656 return *bufp
++ & 0x7f;
659 /* Read a packet from the remote machine, with error checking,
660 and store it in BUF. Returns length of packet, or negative if error. */
666 unsigned char csum
, c1
, c2
;
680 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
701 c1
= fromhex (readchar ());
702 c2
= fromhex (readchar ());
704 if (csum
== (c1
<< 4) + c2
)
707 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
708 (c1
<< 4) + c2
, csum
, buf
);
709 send (remote_desc
, "-", 1, 0);
714 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
718 send (remote_desc
, "+", 1, 0);
722 fprintf (stderr
, "[sent ack]\n");
738 write_enn (char *buf
)
740 /* Some day, we should define the meanings of the error codes... */
748 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
755 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
765 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
770 nib1
= fromhex (*from
++);
771 nib2
= fromhex (*from
++);
772 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
777 outreg (int regno
, char *buf
)
779 if ((regno
>> 12) != 0)
780 *buf
++ = tohex ((regno
>> 12) & 0xf);
781 if ((regno
>> 8) != 0)
782 *buf
++ = tohex ((regno
>> 8) & 0xf);
783 *buf
++ = tohex ((regno
>> 4) & 0xf);
784 *buf
++ = tohex (regno
& 0xf);
786 collect_register_as_string (regno
, buf
);
787 buf
+= 2 * register_size (regno
);
794 new_thread_notify (int id
)
798 /* The `n' response is not yet part of the remote protocol. Do nothing. */
802 if (server_waiting
== 0)
805 sprintf (own_buf
, "n%x", id
);
812 dead_thread_notify (int id
)
816 /* The `x' response is not yet part of the remote protocol. Do nothing. */
820 sprintf (own_buf
, "x%x", id
);
827 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
833 nib
= ((sig
& 0xf0) >> 4);
834 *buf
++ = tohex (nib
);
836 *buf
++ = tohex (nib
);
840 const char **regp
= gdbserver_expedite_regs
;
842 if (the_target
->stopped_by_watchpoint
!= NULL
843 && (*the_target
->stopped_by_watchpoint
) ())
848 strncpy (buf
, "watch:", 6);
851 addr
= (*the_target
->stopped_data_address
) ();
853 /* Convert each byte of the address into two hexadecimal chars.
854 Note that we take sizeof (void *) instead of sizeof (addr);
855 this is to avoid sending a 64-bit address to a 32-bit GDB. */
856 for (i
= sizeof (void *) * 2; i
> 0; i
--)
858 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
865 buf
= outreg (find_regno (*regp
), buf
);
869 /* Formerly, if the debugger had not used any thread features we would not
870 burden it with a thread status response. This was for the benefit of
871 GDB 4.13 and older. However, in recent GDB versions the check
872 (``if (cont_thread != 0)'') does not have the desired effect because of
873 sillyness in the way that the remote protocol handles specifying a thread.
874 Since thread support relies on qSymbol support anyway, assume GDB can handle
879 unsigned int gdb_id_from_wait
;
881 /* FIXME right place to set this? */
882 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
883 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
886 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
887 /* This if (1) ought to be unnecessary. But remote_wait in GDB
888 will claim this event belongs to inferior_ptid if we do not
889 specify a thread, and there's no way for gdbserver to know
890 what inferior_ptid is. */
891 if (1 || old_thread_from_wait
!= thread_from_wait
)
893 general_thread
= thread_from_wait
;
894 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
896 old_thread_from_wait
= thread_from_wait
;
900 /* For W and X, we're done. */
905 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
909 *mem_addr_ptr
= *len_ptr
= 0;
911 while ((ch
= from
[i
++]) != ',')
913 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
914 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
917 for (j
= 0; j
< 4; j
++)
919 if ((ch
= from
[i
++]) == 0)
921 *len_ptr
= *len_ptr
<< 4;
922 *len_ptr
|= fromhex (ch
) & 0x0f;
927 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
932 *mem_addr_ptr
= *len_ptr
= 0;
934 while ((ch
= from
[i
++]) != ',')
936 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
937 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
940 while ((ch
= from
[i
++]) != ':')
942 *len_ptr
= *len_ptr
<< 4;
943 *len_ptr
|= fromhex (ch
) & 0x0f;
946 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
950 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
951 unsigned int *len_ptr
, unsigned char *to
)
955 *mem_addr_ptr
= *len_ptr
= 0;
957 while ((ch
= from
[i
++]) != ',')
959 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
960 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
963 while ((ch
= from
[i
++]) != ':')
965 *len_ptr
= *len_ptr
<< 4;
966 *len_ptr
|= fromhex (ch
) & 0x0f;
969 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
970 to
, *len_ptr
) != *len_ptr
)
976 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
977 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
980 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
982 char own_buf
[266], *p
, *q
;
984 struct sym_cache
*sym
;
986 /* Check the cache first. */
987 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
988 if (strcmp (name
, sym
->name
) == 0)
994 /* If we've passed the call to thread_db_look_up_symbols, then
995 anything not in the cache must not exist; we're not interested
996 in any libraries loaded after that point, only in symbols in
997 libpthread.so. It might not be an appropriate time to look
998 up a symbol, e.g. while we're trying to fetch registers. */
999 if (all_symbols_looked_up
)
1002 /* Send the request. */
1003 strcpy (own_buf
, "qSymbol:");
1004 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1005 if (putpkt (own_buf
) < 0)
1008 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1009 len
= getpkt (own_buf
);
1013 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1015 /* Malformed response. */
1018 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
1025 p
= own_buf
+ strlen ("qSymbol:");
1027 while (*q
&& *q
!= ':')
1030 /* Make sure we found a value for the symbol. */
1031 if (p
== q
|| *q
== '\0')
1034 decode_address (addrp
, p
, q
- p
);
1036 /* Save the symbol in our cache. */
1037 sym
= malloc (sizeof (*sym
));
1038 sym
->name
= strdup (name
);
1040 sym
->next
= symbol_cache
;
This page took 0.069953 seconds and 5 git commands to generate.