1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
26 #include <sys/ioctl.h>
32 #include <netinet/in.h>
35 #include <sys/socket.h>
40 #if HAVE_NETINET_TCP_H
41 #include <netinet/tcp.h>
44 #include <sys/ioctl.h>
57 #include <arpa/inet.h>
69 #include <sys/iomgr.h>
72 #ifndef HAVE_SOCKLEN_T
73 typedef int socklen_t
;
76 #ifndef IN_PROCESS_AGENT
79 # define INVALID_DESCRIPTOR INVALID_SOCKET
81 # define INVALID_DESCRIPTOR -1
84 /* Extra value for readchar_callback. */
86 /* The callback is currently not scheduled. */
90 /* Status of the readchar callback.
91 Either NOT_SCHEDULED or the callback id. */
92 static int readchar_callback
= NOT_SCHEDULED
;
94 static int readchar (void);
95 static void reset_readchar (void);
96 static void reschedule (void);
98 /* A cache entry for a successfully looked-up symbol. */
103 struct sym_cache
*next
;
106 int remote_debug
= 0;
107 struct ui_file
*gdb_stdlog
;
109 static int remote_is_stdio
= 0;
111 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
112 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
114 /* FIXME headerize? */
115 extern int using_threads
;
116 extern int debug_threads
;
118 /* If true, then GDB has requested noack mode. */
120 /* If true, then we tell GDB to use noack mode by default. */
121 int transport_is_reliable
= 0;
124 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
125 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
131 return remote_desc
!= INVALID_DESCRIPTOR
;
134 /* Return true if the remote connection is over stdio. */
137 remote_connection_is_stdio (void)
139 return remote_is_stdio
;
143 enable_async_notification (int fd
)
145 #if defined(F_SETFL) && defined (FASYNC)
146 int save_fcntl_flags
;
148 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
149 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
150 #if defined (F_SETOWN)
151 fcntl (fd
, F_SETOWN
, getpid ());
157 handle_accept_event (int err
, gdb_client_data client_data
)
159 struct sockaddr_in sockaddr
;
163 fprintf (stderr
, "handling possible accept event\n");
165 tmp
= sizeof (sockaddr
);
166 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
167 if (remote_desc
== -1)
168 perror_with_name ("Accept failed");
170 /* Enable TCP keep alive process. */
172 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
173 (char *) &tmp
, sizeof (tmp
));
175 /* Tell TCP not to delay small packets. This greatly speeds up
176 interactive response. */
178 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
179 (char *) &tmp
, sizeof (tmp
));
182 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
183 exits when the remote side dies. */
189 close (listen_desc
); /* No longer need this */
191 closesocket (listen_desc
); /* No longer need this */
195 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
196 descriptor open for add_file_handler to wait for a new connection. */
197 delete_file_handler (listen_desc
);
199 /* Convert IP address to string. */
200 fprintf (stderr
, "Remote debugging from host %s\n",
201 inet_ntoa (sockaddr
.sin_addr
));
203 enable_async_notification (remote_desc
);
205 /* Register the event loop handler. */
206 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
208 /* We have a new GDB connection now. If we were disconnected
209 tracing, there's a window where the target could report a stop
210 event to the event loop, and since we have a connection now, we'd
211 try to send vStopped notifications to GDB. But, don't do that
212 until GDB as selected all-stop/non-stop, and has queried the
213 threads' status ('?'). */
219 /* Prepare for a later connection to a remote debugger.
220 NAME is the filename used for communication. */
223 remote_prepare (char *name
)
227 static int winsock_initialized
;
230 struct sockaddr_in sockaddr
;
235 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
237 /* We need to record fact that we're using stdio sooner than the
238 call to remote_open so start_inferior knows the connection is
241 transport_is_reliable
= 1;
245 port_str
= strchr (name
, ':');
246 if (port_str
== NULL
)
248 transport_is_reliable
= 0;
252 port
= strtoul (port_str
+ 1, &port_end
, 10);
253 if (port_str
[1] == '\0' || *port_end
!= '\0')
254 fatal ("Bad port argument: %s", name
);
257 if (!winsock_initialized
)
261 WSAStartup (MAKEWORD (1, 0), &wsad
);
262 winsock_initialized
= 1;
266 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
267 if (listen_desc
== -1)
268 perror_with_name ("Can't open socket");
270 /* Allow rapid reuse of this port. */
272 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
275 sockaddr
.sin_family
= PF_INET
;
276 sockaddr
.sin_port
= htons (port
);
277 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
279 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
280 || listen (listen_desc
, 1))
281 perror_with_name ("Can't bind address");
283 transport_is_reliable
= 1;
286 /* Open a connection to a remote debugger.
287 NAME is the filename used for communication. */
290 remote_open (char *name
)
294 port_str
= strchr (name
, ':');
296 if (port_str
== NULL
)
297 error ("Only <host>:<port> is supported on this platform.");
300 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
302 fprintf (stderr
, "Remote debugging using stdio\n");
304 /* Use stdin as the handle of the connection.
305 We only select on reads, for example. */
306 remote_desc
= fileno (stdin
);
308 enable_async_notification (remote_desc
);
310 /* Register the event loop handler. */
311 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
314 else if (port_str
== NULL
)
318 if (stat (name
, &statbuf
) == 0
319 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
320 remote_desc
= open (name
, O_RDWR
);
328 perror_with_name ("Could not open remote device");
332 struct termios termios
;
333 tcgetattr (remote_desc
, &termios
);
338 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
339 termios
.c_cflag
|= CLOCAL
| CS8
;
340 termios
.c_cc
[VMIN
] = 1;
341 termios
.c_cc
[VTIME
] = 0;
343 tcsetattr (remote_desc
, TCSANOW
, &termios
);
349 struct termio termio
;
350 ioctl (remote_desc
, TCGETA
, &termio
);
355 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
356 termio
.c_cflag
|= CLOCAL
| CS8
;
357 termio
.c_cc
[VMIN
] = 1;
358 termio
.c_cc
[VTIME
] = 0;
360 ioctl (remote_desc
, TCSETA
, &termio
);
368 ioctl (remote_desc
, TIOCGETP
, &sg
);
370 ioctl (remote_desc
, TIOCSETP
, &sg
);
374 fprintf (stderr
, "Remote debugging using %s\n", name
);
376 enable_async_notification (remote_desc
);
378 /* Register the event loop handler. */
379 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
381 #endif /* USE_WIN32API */
386 struct sockaddr_in sockaddr
;
388 len
= sizeof (sockaddr
);
389 if (getsockname (listen_desc
,
390 (struct sockaddr
*) &sockaddr
, &len
) < 0
391 || len
< sizeof (sockaddr
))
392 perror_with_name ("Can't determine port");
393 port
= ntohs (sockaddr
.sin_port
);
395 fprintf (stderr
, "Listening on port %d\n", port
);
398 /* Register the event loop handler. */
399 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
406 delete_file_handler (remote_desc
);
409 closesocket (remote_desc
);
411 if (! remote_connection_is_stdio ())
414 remote_desc
= INVALID_DESCRIPTOR
;
419 /* Convert hex digit A to a number. */
424 if (a
>= '0' && a
<= '9')
426 else if (a
>= 'a' && a
<= 'f')
429 error ("Reply contains invalid hex digit");
435 static const char hexchars
[] = "0123456789abcdef";
438 ishex (int ch
, int *val
)
440 if ((ch
>= 'a') && (ch
<= 'f'))
442 *val
= ch
- 'a' + 10;
445 if ((ch
>= 'A') && (ch
<= 'F'))
447 *val
= ch
- 'A' + 10;
450 if ((ch
>= '0') && (ch
<= '9'))
458 #ifndef IN_PROCESS_AGENT
461 unhexify (char *bin
, const char *hex
, int count
)
465 for (i
= 0; i
< count
; i
++)
467 if (hex
[0] == 0 || hex
[1] == 0)
469 /* Hex string is short, or of uneven length.
470 Return the count that has been converted so far. */
473 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
480 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
487 for (i
= 0; i
< len
; i
++)
491 addr
= addr
| (fromhex (ch
) & 0x0f);
497 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
502 while (*end
!= '\0' && *end
!= ';')
505 decode_address (addrp
, start
, end
- start
);
514 /* Convert number NIB to a hex digit. */
522 return 'a' + nib
- 10;
525 #ifndef IN_PROCESS_AGENT
528 hexify (char *hex
, const char *bin
, int count
)
532 /* May use a length, or a nul-terminated string as input. */
534 count
= strlen (bin
);
536 for (i
= 0; i
< count
; i
++)
538 *hex
++ = tohex ((*bin
>> 4) & 0xf);
539 *hex
++ = tohex (*bin
++ & 0xf);
545 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
546 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
547 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
548 (which may be more than *OUT_LEN due to escape characters). The
549 total number of bytes in the output buffer will be at most
553 remote_escape_output (const gdb_byte
*buffer
, int len
,
554 gdb_byte
*out_buf
, int *out_len
,
557 int input_index
, output_index
;
560 for (input_index
= 0; input_index
< len
; input_index
++)
562 gdb_byte b
= buffer
[input_index
];
564 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
566 /* These must be escaped. */
567 if (output_index
+ 2 > out_maxlen
)
569 out_buf
[output_index
++] = '}';
570 out_buf
[output_index
++] = b
^ 0x20;
574 if (output_index
+ 1 > out_maxlen
)
576 out_buf
[output_index
++] = b
;
580 *out_len
= input_index
;
584 /* Convert BUFFER, escaped data LEN bytes long, into binary data
585 in OUT_BUF. Return the number of bytes written to OUT_BUF.
586 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
588 This function reverses remote_escape_output. It allows more
589 escaped characters than that function does, in particular because
590 '*' must be escaped to avoid the run-length encoding processing
591 in reading packets. */
594 remote_unescape_input (const gdb_byte
*buffer
, int len
,
595 gdb_byte
*out_buf
, int out_maxlen
)
597 int input_index
, output_index
;
602 for (input_index
= 0; input_index
< len
; input_index
++)
604 gdb_byte b
= buffer
[input_index
];
606 if (output_index
+ 1 > out_maxlen
)
607 error ("Received too much data from the target.");
611 out_buf
[output_index
++] = b
^ 0x20;
617 out_buf
[output_index
++] = b
;
621 error ("Unmatched escape character in target response.");
626 /* Look for a sequence of characters which can be run-length encoded.
627 If there are any, update *CSUM and *P. Otherwise, output the
628 single character. Return the number of characters consumed. */
631 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
635 /* Always output the character. */
639 /* Don't go past '~'. */
643 for (n
= 1; n
< remaining
; n
++)
644 if (buf
[n
] != buf
[0])
647 /* N is the index of the first character not the same as buf[0].
648 buf[0] is counted twice, so by decrementing N, we get the number
649 of characters the RLE sequence will replace. */
655 /* Skip the frame characters. The manual says to skip '+' and '-'
656 also, but there's no reason to. Unfortunately these two unusable
657 characters double the encoded length of a four byte zero
659 while (n
+ 29 == '$' || n
+ 29 == '#')
673 unpack_varlen_hex (char *buff
, /* packet to parse */
679 while (ishex (*buff
, &nibble
))
682 retval
= retval
<< 4;
683 retval
|= nibble
& 0x0f;
689 #ifndef IN_PROCESS_AGENT
691 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
694 write_ptid (char *buf
, ptid_t ptid
)
700 pid
= ptid_get_pid (ptid
);
702 buf
+= sprintf (buf
, "p-%x.", -pid
);
704 buf
+= sprintf (buf
, "p%x.", pid
);
706 tid
= ptid_get_lwp (ptid
);
708 buf
+= sprintf (buf
, "-%x", -tid
);
710 buf
+= sprintf (buf
, "%x", tid
);
716 hex_or_minus_one (char *buf
, char **obuf
)
720 if (strncmp (buf
, "-1", 2) == 0)
726 buf
= unpack_varlen_hex (buf
, &ret
);
734 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
735 passed the last parsed char. Returns null_ptid on error. */
737 read_ptid (char *buf
, char **obuf
)
741 ULONGEST pid
= 0, tid
= 0;
745 /* Multi-process ptid. */
746 pp
= unpack_varlen_hex (p
+ 1, &pid
);
748 error ("invalid remote ptid: %s\n", p
);
752 tid
= hex_or_minus_one (p
, &pp
);
756 return ptid_build (pid
, tid
, 0);
759 /* No multi-process. Just a tid. */
760 tid
= hex_or_minus_one (p
, &pp
);
762 /* Since the stub is not sending a process id, then default to
763 what's in the current inferior. */
764 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
768 return ptid_build (pid
, tid
, 0);
771 /* Write COUNT bytes in BUF to the client.
772 The result is the number of bytes written or -1 if error.
773 This may return less than COUNT. */
776 write_prim (const void *buf
, int count
)
778 if (remote_connection_is_stdio ())
779 return write (fileno (stdout
), buf
, count
);
781 return write (remote_desc
, buf
, count
);
784 /* Read COUNT bytes from the client and store in BUF.
785 The result is the number of bytes read or -1 if error.
786 This may return less than COUNT. */
789 read_prim (void *buf
, int count
)
791 if (remote_connection_is_stdio ())
792 return read (fileno (stdin
), buf
, count
);
794 return read (remote_desc
, buf
, count
);
797 /* Send a packet to the remote machine, with error checking.
798 The data of the packet is in BUF, and the length of the
799 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
802 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
805 unsigned char csum
= 0;
810 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
812 /* Copy the packet into buffer BUF2, encapsulating it
813 and giving it a checksum. */
821 for (i
= 0; i
< cnt
;)
822 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
825 *p
++ = tohex ((csum
>> 4) & 0xf);
826 *p
++ = tohex (csum
& 0xf);
830 /* Send it over and over until we get a positive ack. */
834 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
836 perror ("putpkt(write)");
841 if (noack_mode
|| is_notif
)
843 /* Don't expect an ack then. */
847 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
849 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
857 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
871 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
875 /* Check for an input interrupt while we're here. */
876 if (cc
== '\003' && current_inferior
!= NULL
)
877 (*the_target
->request_interrupt
) ();
882 return 1; /* Success! */
886 putpkt_binary (char *buf
, int cnt
)
888 return putpkt_binary_1 (buf
, cnt
, 0);
891 /* Send a packet to the remote machine, with error checking. The data
892 of the packet is in BUF, and the packet should be a NUL-terminated
893 string. Returns >= 0 on success, -1 otherwise. */
898 return putpkt_binary (buf
, strlen (buf
));
902 putpkt_notif (char *buf
)
904 return putpkt_binary_1 (buf
, strlen (buf
), 1);
907 /* Come here when we get an input interrupt from the remote side. This
908 interrupt should only be active while we are waiting for the child to do
909 something. Thus this assumes readchar:bufcnt is 0.
910 About the only thing that should come through is a ^C, which
911 will cause us to request child interruption. */
914 input_interrupt (int unused
)
917 struct timeval immediate
= { 0, 0 };
919 /* Protect against spurious interrupts. This has been observed to
920 be a problem under NetBSD 1.4 and 1.5. */
923 FD_SET (remote_desc
, &readset
);
924 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
929 cc
= read_prim (&c
, 1);
931 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
933 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
938 (*the_target
->request_interrupt
) ();
942 /* Check if the remote side sent us an interrupt request (^C). */
944 check_remote_input_interrupt_request (void)
946 /* This function may be called before establishing communications,
947 therefore we need to validate the remote descriptor. */
949 if (remote_desc
== INVALID_DESCRIPTOR
)
955 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
956 accept Control-C from the client, and must be disabled when talking to
960 unblock_async_io (void)
965 sigemptyset (&sigio_set
);
966 sigaddset (&sigio_set
, SIGIO
);
967 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
973 nto_comctrl (int enable
)
975 struct sigevent event
;
979 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
980 event
.sigev_signo
= SIGIO
;
981 event
.sigev_code
= 0;
982 event
.sigev_value
.sival_ptr
= NULL
;
983 event
.sigev_priority
= -1;
984 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
988 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
993 /* Current state of asynchronous I/O. */
994 static int async_io_enabled
;
996 /* Enable asynchronous I/O. */
998 enable_async_io (void)
1000 if (async_io_enabled
)
1003 #ifndef USE_WIN32API
1004 signal (SIGIO
, input_interrupt
);
1006 async_io_enabled
= 1;
1009 #endif /* __QNX__ */
1012 /* Disable asynchronous I/O. */
1014 disable_async_io (void)
1016 if (!async_io_enabled
)
1019 #ifndef USE_WIN32API
1020 signal (SIGIO
, SIG_IGN
);
1022 async_io_enabled
= 0;
1025 #endif /* __QNX__ */
1030 initialize_async_io (void)
1032 /* Make sure that async I/O starts disabled. */
1033 async_io_enabled
= 1;
1034 disable_async_io ();
1036 /* Make sure the signal is unblocked. */
1037 unblock_async_io ();
1040 /* Internal buffer used by readchar.
1041 These are global to readchar because reschedule_remote needs to be
1042 able to tell whether the buffer is empty. */
1044 static unsigned char readchar_buf
[BUFSIZ
];
1045 static int readchar_bufcnt
= 0;
1046 static unsigned char *readchar_bufp
;
1048 /* Returns next char from remote GDB. -1 if error. */
1055 if (readchar_bufcnt
== 0)
1057 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
1059 if (readchar_bufcnt
<= 0)
1061 if (readchar_bufcnt
== 0)
1062 fprintf (stderr
, "readchar: Got EOF\n");
1064 perror ("readchar");
1069 readchar_bufp
= readchar_buf
;
1073 ch
= *readchar_bufp
++;
1078 /* Reset the readchar state machine. */
1081 reset_readchar (void)
1083 readchar_bufcnt
= 0;
1084 if (readchar_callback
!= NOT_SCHEDULED
)
1086 delete_callback_event (readchar_callback
);
1087 readchar_callback
= NOT_SCHEDULED
;
1091 /* Process remaining data in readchar_buf. */
1094 process_remaining (void *context
)
1098 /* This is a one-shot event. */
1099 readchar_callback
= NOT_SCHEDULED
;
1101 if (readchar_bufcnt
> 0)
1102 res
= handle_serial_event (0, NULL
);
1109 /* If there is still data in the buffer, queue another event to process it,
1110 we can't sleep in select yet. */
1115 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
1116 readchar_callback
= append_callback_event (process_remaining
, NULL
);
1119 /* Read a packet from the remote machine, with error checking,
1120 and store it in BUF. Returns length of packet, or negative if error. */
1126 unsigned char csum
, c1
, c2
;
1140 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1161 c1
= fromhex (readchar ());
1162 c2
= fromhex (readchar ());
1164 if (csum
== (c1
<< 4) + c2
)
1170 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1171 "buf=%s [no-ack-mode, Bad medium?]\n",
1172 (c1
<< 4) + c2
, csum
, buf
);
1173 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1177 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1178 (c1
<< 4) + c2
, csum
, buf
);
1179 if (write_prim ("-", 1) != 1)
1187 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1191 if (write_prim ("+", 1) != 1)
1196 fprintf (stderr
, "[sent ack]\n");
1204 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1213 write_ok (char *buf
)
1221 write_enn (char *buf
)
1223 /* Some day, we should define the meanings of the error codes... */
1233 convert_int_to_ascii (const unsigned char *from
, char *to
, int n
)
1240 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1241 *to
++ = tohex (nib
);
1243 *to
++ = tohex (nib
);
1248 #ifndef IN_PROCESS_AGENT
1251 convert_ascii_to_int (const char *from
, unsigned char *to
, int n
)
1256 nib1
= fromhex (*from
++);
1257 nib2
= fromhex (*from
++);
1258 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1263 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1265 if ((regno
>> 12) != 0)
1266 *buf
++ = tohex ((regno
>> 12) & 0xf);
1267 if ((regno
>> 8) != 0)
1268 *buf
++ = tohex ((regno
>> 8) & 0xf);
1269 *buf
++ = tohex ((regno
>> 4) & 0xf);
1270 *buf
++ = tohex (regno
& 0xf);
1272 collect_register_as_string (regcache
, regno
, buf
);
1273 buf
+= 2 * register_size (regno
);
1280 new_thread_notify (int id
)
1284 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1288 if (server_waiting
== 0)
1291 sprintf (own_buf
, "n%x", id
);
1292 disable_async_io ();
1298 dead_thread_notify (int id
)
1302 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1306 sprintf (own_buf
, "x%x", id
);
1307 disable_async_io ();
1313 prepare_resume_reply (char *buf
, ptid_t ptid
,
1314 struct target_waitstatus
*status
)
1317 fprintf (stderr
, "Writing resume reply for %s:%d\n",
1318 target_pid_to_str (ptid
), status
->kind
);
1320 switch (status
->kind
)
1322 case TARGET_WAITKIND_STOPPED
:
1324 struct thread_info
*saved_inferior
;
1326 struct regcache
*regcache
;
1328 sprintf (buf
, "T%02x", status
->value
.sig
);
1329 buf
+= strlen (buf
);
1331 regp
= gdbserver_expedite_regs
;
1333 saved_inferior
= current_inferior
;
1335 current_inferior
= find_thread_ptid (ptid
);
1337 regcache
= get_thread_regcache (current_inferior
, 1);
1339 if (the_target
->stopped_by_watchpoint
!= NULL
1340 && (*the_target
->stopped_by_watchpoint
) ())
1345 strncpy (buf
, "watch:", 6);
1348 addr
= (*the_target
->stopped_data_address
) ();
1350 /* Convert each byte of the address into two hexadecimal
1351 chars. Note that we take sizeof (void *) instead of
1352 sizeof (addr); this is to avoid sending a 64-bit
1353 address to a 32-bit GDB. */
1354 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1355 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1361 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1366 /* Formerly, if the debugger had not used any thread features
1367 we would not burden it with a thread status response. This
1368 was for the benefit of GDB 4.13 and older. However, in
1369 recent GDB versions the check (``if (cont_thread != 0)'')
1370 does not have the desired effect because of sillyness in
1371 the way that the remote protocol handles specifying a
1372 thread. Since thread support relies on qSymbol support
1373 anyway, assume GDB can handle threads. */
1375 if (using_threads
&& !disable_packet_Tthread
)
1377 /* This if (1) ought to be unnecessary. But remote_wait
1378 in GDB will claim this event belongs to inferior_ptid
1379 if we do not specify a thread, and there's no way for
1380 gdbserver to know what inferior_ptid is. */
1381 if (1 || !ptid_equal (general_thread
, ptid
))
1384 /* In non-stop, don't change the general thread behind
1387 general_thread
= ptid
;
1388 sprintf (buf
, "thread:");
1389 buf
+= strlen (buf
);
1390 buf
= write_ptid (buf
, ptid
);
1392 buf
+= strlen (buf
);
1394 core
= target_core_of_thread (ptid
);
1398 sprintf (buf
, "core:");
1399 buf
+= strlen (buf
);
1400 sprintf (buf
, "%x", core
);
1402 buf
+= strlen (buf
);
1409 strcpy (buf
, "library:;");
1410 buf
+= strlen (buf
);
1414 current_inferior
= saved_inferior
;
1417 case TARGET_WAITKIND_EXITED
:
1419 sprintf (buf
, "W%x;process:%x",
1420 status
->value
.integer
, ptid_get_pid (ptid
));
1422 sprintf (buf
, "W%02x", status
->value
.integer
);
1424 case TARGET_WAITKIND_SIGNALLED
:
1426 sprintf (buf
, "X%x;process:%x",
1427 status
->value
.sig
, ptid_get_pid (ptid
));
1429 sprintf (buf
, "X%02x", status
->value
.sig
);
1432 error ("unhandled waitkind");
1438 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1442 *mem_addr_ptr
= *len_ptr
= 0;
1444 while ((ch
= from
[i
++]) != ',')
1446 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1447 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1450 for (j
= 0; j
< 4; j
++)
1452 if ((ch
= from
[i
++]) == 0)
1454 *len_ptr
= *len_ptr
<< 4;
1455 *len_ptr
|= fromhex (ch
) & 0x0f;
1460 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1461 unsigned char **to_p
)
1465 *mem_addr_ptr
= *len_ptr
= 0;
1467 while ((ch
= from
[i
++]) != ',')
1469 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1470 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1473 while ((ch
= from
[i
++]) != ':')
1475 *len_ptr
= *len_ptr
<< 4;
1476 *len_ptr
|= fromhex (ch
) & 0x0f;
1480 *to_p
= xmalloc (*len_ptr
);
1482 convert_ascii_to_int (&from
[i
++], *to_p
, *len_ptr
);
1486 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1487 unsigned int *len_ptr
, unsigned char **to_p
)
1491 *mem_addr_ptr
= *len_ptr
= 0;
1493 while ((ch
= from
[i
++]) != ',')
1495 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1496 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1499 while ((ch
= from
[i
++]) != ':')
1501 *len_ptr
= *len_ptr
<< 4;
1502 *len_ptr
|= fromhex (ch
) & 0x0f;
1506 *to_p
= xmalloc (*len_ptr
);
1508 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1509 *to_p
, *len_ptr
) != *len_ptr
)
1515 /* Decode a qXfer write request. */
1518 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1519 unsigned int *len
, unsigned char *data
)
1524 /* Extract the offset. */
1526 while ((ch
= *buf
++) != ':')
1528 *offset
= *offset
<< 4;
1529 *offset
|= fromhex (ch
) & 0x0f;
1532 /* Get encoded data. */
1533 packet_len
-= buf
- b
;
1534 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1539 /* Decode the parameters of a qSearch:memory packet. */
1542 decode_search_memory_packet (const char *buf
, int packet_len
,
1543 CORE_ADDR
*start_addrp
,
1544 CORE_ADDR
*search_space_lenp
,
1545 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1547 const char *p
= buf
;
1549 p
= decode_address_to_semicolon (start_addrp
, p
);
1550 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1551 packet_len
-= p
- buf
;
1552 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1553 pattern
, packet_len
);
1558 free_sym_cache (struct sym_cache
*sym
)
1568 clear_symbol_cache (struct sym_cache
**symcache_p
)
1570 struct sym_cache
*sym
, *next
;
1572 /* Check the cache first. */
1573 for (sym
= *symcache_p
; sym
; sym
= next
)
1576 free_sym_cache (sym
);
1582 /* Get the address of NAME, and return it in ADDRP if found. if
1583 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1584 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1587 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1589 char own_buf
[266], *p
, *q
;
1591 struct sym_cache
*sym
;
1592 struct process_info
*proc
;
1594 proc
= current_process ();
1596 /* Check the cache first. */
1597 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1598 if (strcmp (name
, sym
->name
) == 0)
1604 /* It might not be an appropriate time to look up a symbol,
1605 e.g. while we're trying to fetch registers. */
1609 /* Send the request. */
1610 strcpy (own_buf
, "qSymbol:");
1611 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1612 if (putpkt (own_buf
) < 0)
1615 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1616 len
= getpkt (own_buf
);
1620 /* We ought to handle pretty much any packet at this point while we
1621 wait for the qSymbol "response". That requires re-entering the
1622 main loop. For now, this is an adequate approximation; allow
1623 GDB to read from memory while it figures out the address of the
1625 while (own_buf
[0] == 'm')
1628 unsigned char *mem_buf
;
1629 unsigned int mem_len
;
1631 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1632 mem_buf
= xmalloc (mem_len
);
1633 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1634 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1636 write_enn (own_buf
);
1638 if (putpkt (own_buf
) < 0)
1640 len
= getpkt (own_buf
);
1645 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1647 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1651 p
= own_buf
+ strlen ("qSymbol:");
1653 while (*q
&& *q
!= ':')
1656 /* Make sure we found a value for the symbol. */
1657 if (p
== q
|| *q
== '\0')
1660 decode_address (addrp
, p
, q
- p
);
1662 /* Save the symbol in our cache. */
1663 sym
= xmalloc (sizeof (*sym
));
1664 sym
->name
= xstrdup (name
);
1666 sym
->next
= proc
->symbol_cache
;
1667 proc
->symbol_cache
= sym
;
1672 /* Relocate an instruction to execute at a different address. OLDLOC
1673 is the address in the inferior memory where the instruction to
1674 relocate is currently at. On input, TO points to the destination
1675 where we want the instruction to be copied (and possibly adjusted)
1676 to. On output, it points to one past the end of the resulting
1677 instruction(s). The effect of executing the instruction at TO
1678 shall be the same as if executing it at OLDLOC. For example, call
1679 instructions that implicitly push the return address on the stack
1680 should be adjusted to return to the instruction after OLDLOC;
1681 relative branches, and other PC-relative instructions need the
1682 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1685 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1689 ULONGEST written
= 0;
1691 /* Send the request. */
1692 strcpy (own_buf
, "qRelocInsn:");
1693 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1695 if (putpkt (own_buf
) < 0)
1698 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1699 len
= getpkt (own_buf
);
1703 /* We ought to handle pretty much any packet at this point while we
1704 wait for the qRelocInsn "response". That requires re-entering
1705 the main loop. For now, this is an adequate approximation; allow
1706 GDB to access memory. */
1707 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1710 unsigned char *mem_buf
= NULL
;
1711 unsigned int mem_len
;
1713 if (own_buf
[0] == 'm')
1715 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1716 mem_buf
= xmalloc (mem_len
);
1717 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1718 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1720 write_enn (own_buf
);
1722 else if (own_buf
[0] == 'X')
1724 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1725 &mem_len
, &mem_buf
) < 0
1726 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1727 write_enn (own_buf
);
1733 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1734 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1737 write_enn (own_buf
);
1740 if (putpkt (own_buf
) < 0)
1742 len
= getpkt (own_buf
);
1747 if (own_buf
[0] == 'E')
1749 warning ("An error occurred while relocating an instruction: %s\n",
1754 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1756 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1761 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1768 monitor_output (const char *msg
)
1770 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1773 hexify (buf
+ 1, msg
, 0);