]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/remote-utils.c
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
;
89 /* Open a connection to a remote debugger.
90 NAME is the filename used for communication. */
93 remote_open (char *name
)
95 #if defined(F_SETFL) && defined (FASYNC)
100 port_str
= strchr (name
, ':');
101 if (port_str
== NULL
)
104 error ("Only <host>:<port> is supported on this platform.");
108 if (stat (name
, &statbuf
) == 0
109 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
110 remote_desc
= open (name
, O_RDWR
);
118 perror_with_name ("Could not open remote device");
122 struct termios termios
;
123 tcgetattr (remote_desc
, &termios
);
128 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
129 termios
.c_cflag
|= CLOCAL
| CS8
;
130 termios
.c_cc
[VMIN
] = 1;
131 termios
.c_cc
[VTIME
] = 0;
133 tcsetattr (remote_desc
, TCSANOW
, &termios
);
139 struct termio termio
;
140 ioctl (remote_desc
, TCGETA
, &termio
);
145 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
146 termio
.c_cflag
|= CLOCAL
| CS8
;
147 termio
.c_cc
[VMIN
] = 1;
148 termio
.c_cc
[VTIME
] = 0;
150 ioctl (remote_desc
, TCSETA
, &termio
);
158 ioctl (remote_desc
, TIOCGETP
, &sg
);
160 ioctl (remote_desc
, TIOCSETP
, &sg
);
164 fprintf (stderr
, "Remote debugging using %s\n", name
);
165 #endif /* USE_WIN32API */
170 static int winsock_initialized
;
174 struct sockaddr_in sockaddr
;
178 port_str
= strchr (name
, ':');
180 port
= atoi (port_str
+ 1);
183 if (!winsock_initialized
)
187 WSAStartup (MAKEWORD (1, 0), &wsad
);
188 winsock_initialized
= 1;
192 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
194 perror_with_name ("Can't open socket");
196 /* Allow rapid reuse of this port. */
198 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
201 sockaddr
.sin_family
= PF_INET
;
202 sockaddr
.sin_port
= htons (port
);
203 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
205 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
206 || listen (tmp_desc
, 1))
207 perror_with_name ("Can't bind address");
209 fprintf (stderr
, "Listening on port %d\n", port
);
212 tmp
= sizeof (sockaddr
);
213 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
214 if (remote_desc
== -1)
215 perror_with_name ("Accept failed");
217 /* Enable TCP keep alive process. */
219 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
221 /* Tell TCP not to delay small packets. This greatly speeds up
222 interactive response. */
224 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
225 (char *) &tmp
, sizeof (tmp
));
229 close (tmp_desc
); /* No longer need this */
231 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
232 exits when the remote side dies. */
234 closesocket (tmp_desc
); /* No longer need this */
237 /* Convert IP address to string. */
238 fprintf (stderr
, "Remote debugging from host %s\n",
239 inet_ntoa (sockaddr
.sin_addr
));
242 #if defined(F_SETFL) && defined (FASYNC)
243 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
244 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
245 #if defined (F_SETOWN)
246 fcntl (remote_desc
, F_SETOWN
, getpid ());
256 closesocket (remote_desc
);
262 /* Convert hex digit A to a number. */
267 if (a
>= '0' && a
<= '9')
269 else if (a
>= 'a' && a
<= 'f')
272 error ("Reply contains invalid hex digit");
277 unhexify (char *bin
, const char *hex
, int count
)
281 for (i
= 0; i
< count
; i
++)
283 if (hex
[0] == 0 || hex
[1] == 0)
285 /* Hex string is short, or of uneven length.
286 Return the count that has been converted so far. */
289 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
296 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
303 for (i
= 0; i
< len
; i
++)
307 addr
= addr
| (fromhex (ch
) & 0x0f);
313 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
318 while (*end
!= '\0' && *end
!= ';')
321 decode_address (addrp
, start
, end
- start
);
328 /* Convert number NIB to a hex digit. */
336 return 'a' + nib
- 10;
340 hexify (char *hex
, const char *bin
, int count
)
344 /* May use a length, or a nul-terminated string as input. */
346 count
= strlen (bin
);
348 for (i
= 0; i
< count
; i
++)
350 *hex
++ = tohex ((*bin
>> 4) & 0xf);
351 *hex
++ = tohex (*bin
++ & 0xf);
357 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
358 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
359 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
360 (which may be more than *OUT_LEN due to escape characters). The
361 total number of bytes in the output buffer will be at most
365 remote_escape_output (const gdb_byte
*buffer
, int len
,
366 gdb_byte
*out_buf
, int *out_len
,
369 int input_index
, output_index
;
372 for (input_index
= 0; input_index
< len
; input_index
++)
374 gdb_byte b
= buffer
[input_index
];
376 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
378 /* These must be escaped. */
379 if (output_index
+ 2 > out_maxlen
)
381 out_buf
[output_index
++] = '}';
382 out_buf
[output_index
++] = b
^ 0x20;
386 if (output_index
+ 1 > out_maxlen
)
388 out_buf
[output_index
++] = b
;
392 *out_len
= input_index
;
396 /* Convert BUFFER, escaped data LEN bytes long, into binary data
397 in OUT_BUF. Return the number of bytes written to OUT_BUF.
398 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
400 This function reverses remote_escape_output. It allows more
401 escaped characters than that function does, in particular because
402 '*' must be escaped to avoid the run-length encoding processing
403 in reading packets. */
406 remote_unescape_input (const gdb_byte
*buffer
, int len
,
407 gdb_byte
*out_buf
, int out_maxlen
)
409 int input_index
, output_index
;
414 for (input_index
= 0; input_index
< len
; input_index
++)
416 gdb_byte b
= buffer
[input_index
];
418 if (output_index
+ 1 > out_maxlen
)
419 error ("Received too much data from the target.");
423 out_buf
[output_index
++] = b
^ 0x20;
429 out_buf
[output_index
++] = b
;
433 error ("Unmatched escape character in target response.");
438 /* Look for a sequence of characters which can be run-length encoded.
439 If there are any, update *CSUM and *P. Otherwise, output the
440 single character. Return the number of characters consumed. */
443 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
447 /* Always output the character. */
451 /* Don't go past '~'. */
455 for (n
= 1; n
< remaining
; n
++)
456 if (buf
[n
] != buf
[0])
459 /* N is the index of the first character not the same as buf[0].
460 buf[0] is counted twice, so by decrementing N, we get the number
461 of characters the RLE sequence will replace. */
467 /* Skip the frame characters. The manual says to skip '+' and '-'
468 also, but there's no reason to. Unfortunately these two unusable
469 characters double the encoded length of a four byte zero
471 while (n
+ 29 == '$' || n
+ 29 == '#')
482 /* Send a packet to the remote machine, with error checking.
483 The data of the packet is in BUF, and the length of the
484 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
487 putpkt_binary (char *buf
, int cnt
)
490 unsigned char csum
= 0;
495 buf2
= malloc (PBUFSIZ
);
497 /* Copy the packet into buffer BUF2, encapsulating it
498 and giving it a checksum. */
503 for (i
= 0; i
< cnt
;)
504 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
507 *p
++ = tohex ((csum
>> 4) & 0xf);
508 *p
++ = tohex (csum
& 0xf);
512 /* Send it over and over until we get a positive ack. */
518 if (send (remote_desc
, buf2
, p
- buf2
, 0) != p
- buf2
)
520 perror ("putpkt(write)");
526 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
529 cc
= recv (remote_desc
, buf3
, 1, 0);
532 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
539 fprintf (stderr
, "putpkt(read): Got EOF\n");
541 perror ("putpkt(read)");
547 /* Check for an input interrupt while we're here. */
548 if (buf3
[0] == '\003')
549 (*the_target
->send_signal
) (SIGINT
);
551 while (buf3
[0] != '+');
554 return 1; /* Success! */
557 /* Send a packet to the remote machine, with error checking. The data
558 of the packet is in BUF, and the packet should be a NUL-terminated
559 string. Returns >= 0 on success, -1 otherwise. */
564 return putpkt_binary (buf
, strlen (buf
));
569 /* Come here when we get an input interrupt from the remote side. This
570 interrupt should only be active while we are waiting for the child to do
571 something. About the only thing that should come through is a ^C, which
572 will cause us to send a SIGINT to the child. */
575 input_interrupt (int unused
)
578 struct timeval immediate
= { 0, 0 };
580 /* Protect against spurious interrupts. This has been observed to
581 be a problem under NetBSD 1.4 and 1.5. */
584 FD_SET (remote_desc
, &readset
);
585 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
590 cc
= recv (remote_desc
, &c
, 1, 0);
592 if (cc
!= 1 || c
!= '\003')
594 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
599 (*the_target
->send_signal
) (SIGINT
);
604 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
605 accept Control-C from the client, and must be disabled when talking to
609 block_async_io (void)
613 sigemptyset (&sigio_set
);
614 sigaddset (&sigio_set
, SIGIO
);
615 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
620 unblock_async_io (void)
624 sigemptyset (&sigio_set
);
625 sigaddset (&sigio_set
, SIGIO
);
626 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
630 /* Current state of asynchronous I/O. */
631 static int async_io_enabled
;
633 /* Enable asynchronous I/O. */
635 enable_async_io (void)
637 if (async_io_enabled
)
641 signal (SIGIO
, input_interrupt
);
643 async_io_enabled
= 1;
646 /* Disable asynchronous I/O. */
648 disable_async_io (void)
650 if (!async_io_enabled
)
654 signal (SIGIO
, SIG_IGN
);
656 async_io_enabled
= 0;
659 /* Returns next char from remote GDB. -1 if error. */
664 static unsigned char buf
[BUFSIZ
];
665 static int bufcnt
= 0;
666 static unsigned char *bufp
;
671 bufcnt
= recv (remote_desc
, buf
, sizeof (buf
), 0);
676 fprintf (stderr
, "readchar: Got EOF\n");
685 return *bufp
++ & 0x7f;
688 /* Read a packet from the remote machine, with error checking,
689 and store it in BUF. Returns length of packet, or negative if error. */
695 unsigned char csum
, c1
, c2
;
709 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
730 c1
= fromhex (readchar ());
731 c2
= fromhex (readchar ());
733 if (csum
== (c1
<< 4) + c2
)
736 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
737 (c1
<< 4) + c2
, csum
, buf
);
738 send (remote_desc
, "-", 1, 0);
743 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
747 send (remote_desc
, "+", 1, 0);
751 fprintf (stderr
, "[sent ack]\n");
767 write_enn (char *buf
)
769 /* Some day, we should define the meanings of the error codes... */
777 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
784 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
794 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
799 nib1
= fromhex (*from
++);
800 nib2
= fromhex (*from
++);
801 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
806 outreg (int regno
, char *buf
)
808 if ((regno
>> 12) != 0)
809 *buf
++ = tohex ((regno
>> 12) & 0xf);
810 if ((regno
>> 8) != 0)
811 *buf
++ = tohex ((regno
>> 8) & 0xf);
812 *buf
++ = tohex ((regno
>> 4) & 0xf);
813 *buf
++ = tohex (regno
& 0xf);
815 collect_register_as_string (regno
, buf
);
816 buf
+= 2 * register_size (regno
);
823 new_thread_notify (int id
)
827 /* The `n' response is not yet part of the remote protocol. Do nothing. */
831 if (server_waiting
== 0)
834 sprintf (own_buf
, "n%x", id
);
841 dead_thread_notify (int id
)
845 /* The `x' response is not yet part of the remote protocol. Do nothing. */
849 sprintf (own_buf
, "x%x", id
);
856 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
862 nib
= ((sig
& 0xf0) >> 4);
863 *buf
++ = tohex (nib
);
865 *buf
++ = tohex (nib
);
869 const char **regp
= gdbserver_expedite_regs
;
871 if (the_target
->stopped_by_watchpoint
!= NULL
872 && (*the_target
->stopped_by_watchpoint
) ())
877 strncpy (buf
, "watch:", 6);
880 addr
= (*the_target
->stopped_data_address
) ();
882 /* Convert each byte of the address into two hexadecimal chars.
883 Note that we take sizeof (void *) instead of sizeof (addr);
884 this is to avoid sending a 64-bit address to a 32-bit GDB. */
885 for (i
= sizeof (void *) * 2; i
> 0; i
--)
887 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
894 buf
= outreg (find_regno (*regp
), buf
);
898 /* Formerly, if the debugger had not used any thread features we would not
899 burden it with a thread status response. This was for the benefit of
900 GDB 4.13 and older. However, in recent GDB versions the check
901 (``if (cont_thread != 0)'') does not have the desired effect because of
902 sillyness in the way that the remote protocol handles specifying a thread.
903 Since thread support relies on qSymbol support anyway, assume GDB can handle
908 unsigned int gdb_id_from_wait
;
910 /* FIXME right place to set this? */
911 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
912 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
915 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
916 /* This if (1) ought to be unnecessary. But remote_wait in GDB
917 will claim this event belongs to inferior_ptid if we do not
918 specify a thread, and there's no way for gdbserver to know
919 what inferior_ptid is. */
920 if (1 || old_thread_from_wait
!= thread_from_wait
)
922 general_thread
= thread_from_wait
;
923 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
925 old_thread_from_wait
= thread_from_wait
;
929 /* For W and X, we're done. */
934 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
938 *mem_addr_ptr
= *len_ptr
= 0;
940 while ((ch
= from
[i
++]) != ',')
942 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
943 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
946 for (j
= 0; j
< 4; j
++)
948 if ((ch
= from
[i
++]) == 0)
950 *len_ptr
= *len_ptr
<< 4;
951 *len_ptr
|= fromhex (ch
) & 0x0f;
956 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
961 *mem_addr_ptr
= *len_ptr
= 0;
963 while ((ch
= from
[i
++]) != ',')
965 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
966 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
969 while ((ch
= from
[i
++]) != ':')
971 *len_ptr
= *len_ptr
<< 4;
972 *len_ptr
|= fromhex (ch
) & 0x0f;
975 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
979 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
980 unsigned int *len_ptr
, unsigned char *to
)
984 *mem_addr_ptr
= *len_ptr
= 0;
986 while ((ch
= from
[i
++]) != ',')
988 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
989 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
992 while ((ch
= from
[i
++]) != ':')
994 *len_ptr
= *len_ptr
<< 4;
995 *len_ptr
|= fromhex (ch
) & 0x0f;
998 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
999 to
, *len_ptr
) != *len_ptr
)
1005 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1006 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1009 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1011 char own_buf
[266], *p
, *q
;
1013 struct sym_cache
*sym
;
1015 /* Check the cache first. */
1016 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
1017 if (strcmp (name
, sym
->name
) == 0)
1023 /* If we've passed the call to thread_db_look_up_symbols, then
1024 anything not in the cache must not exist; we're not interested
1025 in any libraries loaded after that point, only in symbols in
1026 libpthread.so. It might not be an appropriate time to look
1027 up a symbol, e.g. while we're trying to fetch registers. */
1028 if (all_symbols_looked_up
)
1031 /* Send the request. */
1032 strcpy (own_buf
, "qSymbol:");
1033 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1034 if (putpkt (own_buf
) < 0)
1037 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1038 len
= getpkt (own_buf
);
1042 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1044 /* Malformed response. */
1047 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
1054 p
= own_buf
+ strlen ("qSymbol:");
1056 while (*q
&& *q
!= ':')
1059 /* Make sure we found a value for the symbol. */
1060 if (p
== q
|| *q
== '\0')
1063 decode_address (addrp
, p
, q
- p
);
1065 /* Save the symbol in our cache. */
1066 sym
= malloc (sizeof (*sym
));
1067 sym
->name
= strdup (name
);
1069 sym
->next
= symbol_cache
;