1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2013 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"
29 #include <sys/ioctl.h>
35 #include <netinet/in.h>
38 #include <sys/socket.h>
43 #if HAVE_NETINET_TCP_H
44 #include <netinet/tcp.h>
47 #include <sys/ioctl.h>
58 #include <arpa/inet.h>
70 #include <sys/iomgr.h>
73 #ifndef HAVE_SOCKLEN_T
74 typedef int socklen_t
;
77 #ifndef IN_PROCESS_AGENT
80 # define INVALID_DESCRIPTOR INVALID_SOCKET
82 # define INVALID_DESCRIPTOR -1
85 /* Extra value for readchar_callback. */
87 /* The callback is currently not scheduled. */
91 /* Status of the readchar callback.
92 Either NOT_SCHEDULED or the callback id. */
93 static int readchar_callback
= NOT_SCHEDULED
;
95 static int readchar (void);
96 static void reset_readchar (void);
97 static void reschedule (void);
99 /* A cache entry for a successfully looked-up symbol. */
104 struct sym_cache
*next
;
107 int remote_debug
= 0;
108 struct ui_file
*gdb_stdlog
;
110 static int remote_is_stdio
= 0;
112 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
113 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
115 /* FIXME headerize? */
116 extern int using_threads
;
117 extern int debug_threads
;
119 /* If true, then GDB has requested noack mode. */
121 /* If true, then we tell GDB to use noack mode by default. */
122 int transport_is_reliable
= 0;
125 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
126 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
132 return remote_desc
!= INVALID_DESCRIPTOR
;
135 /* Return true if the remote connection is over stdio. */
138 remote_connection_is_stdio (void)
140 return remote_is_stdio
;
144 enable_async_notification (int fd
)
146 #if defined(F_SETFL) && defined (FASYNC)
147 int save_fcntl_flags
;
149 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
150 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
151 #if defined (F_SETOWN)
152 fcntl (fd
, F_SETOWN
, getpid ());
158 handle_accept_event (int err
, gdb_client_data client_data
)
160 struct sockaddr_in sockaddr
;
164 fprintf (stderr
, "handling possible accept event\n");
166 tmp
= sizeof (sockaddr
);
167 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
168 if (remote_desc
== -1)
169 perror_with_name ("Accept failed");
171 /* Enable TCP keep alive process. */
173 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
174 (char *) &tmp
, sizeof (tmp
));
176 /* Tell TCP not to delay small packets. This greatly speeds up
177 interactive response. */
179 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
180 (char *) &tmp
, sizeof (tmp
));
183 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
184 exits when the remote side dies. */
190 close (listen_desc
); /* No longer need this */
192 closesocket (listen_desc
); /* No longer need this */
196 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
197 descriptor open for add_file_handler to wait for a new connection. */
198 delete_file_handler (listen_desc
);
200 /* Convert IP address to string. */
201 fprintf (stderr
, "Remote debugging from host %s\n",
202 inet_ntoa (sockaddr
.sin_addr
));
204 enable_async_notification (remote_desc
);
206 /* Register the event loop handler. */
207 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
209 /* We have a new GDB connection now. If we were disconnected
210 tracing, there's a window where the target could report a stop
211 event to the event loop, and since we have a connection now, we'd
212 try to send vStopped notifications to GDB. But, don't do that
213 until GDB as selected all-stop/non-stop, and has queried the
214 threads' status ('?'). */
220 /* Prepare for a later connection to a remote debugger.
221 NAME is the filename used for communication. */
224 remote_prepare (char *name
)
228 static int winsock_initialized
;
231 struct sockaddr_in sockaddr
;
236 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
238 /* We need to record fact that we're using stdio sooner than the
239 call to remote_open so start_inferior knows the connection is
242 transport_is_reliable
= 1;
246 port_str
= strchr (name
, ':');
247 if (port_str
== NULL
)
249 transport_is_reliable
= 0;
253 port
= strtoul (port_str
+ 1, &port_end
, 10);
254 if (port_str
[1] == '\0' || *port_end
!= '\0')
255 fatal ("Bad port argument: %s", name
);
258 if (!winsock_initialized
)
262 WSAStartup (MAKEWORD (1, 0), &wsad
);
263 winsock_initialized
= 1;
267 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
268 if (listen_desc
== -1)
269 perror_with_name ("Can't open socket");
271 /* Allow rapid reuse of this port. */
273 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
276 sockaddr
.sin_family
= PF_INET
;
277 sockaddr
.sin_port
= htons (port
);
278 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
280 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
281 || listen (listen_desc
, 1))
282 perror_with_name ("Can't bind address");
284 transport_is_reliable
= 1;
287 /* Open a connection to a remote debugger.
288 NAME is the filename used for communication. */
291 remote_open (char *name
)
295 port_str
= strchr (name
, ':');
297 if (port_str
== NULL
)
298 error ("Only <host>:<port> is supported on this platform.");
301 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
303 fprintf (stderr
, "Remote debugging using stdio\n");
305 /* Use stdin as the handle of the connection.
306 We only select on reads, for example. */
307 remote_desc
= fileno (stdin
);
309 enable_async_notification (remote_desc
);
311 /* Register the event loop handler. */
312 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
315 else if (port_str
== NULL
)
319 if (stat (name
, &statbuf
) == 0
320 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
321 remote_desc
= open (name
, O_RDWR
);
329 perror_with_name ("Could not open remote device");
333 struct termios termios
;
334 tcgetattr (remote_desc
, &termios
);
339 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
340 termios
.c_cflag
|= CLOCAL
| CS8
;
341 termios
.c_cc
[VMIN
] = 1;
342 termios
.c_cc
[VTIME
] = 0;
344 tcsetattr (remote_desc
, TCSANOW
, &termios
);
350 struct termio termio
;
351 ioctl (remote_desc
, TCGETA
, &termio
);
356 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
357 termio
.c_cflag
|= CLOCAL
| CS8
;
358 termio
.c_cc
[VMIN
] = 1;
359 termio
.c_cc
[VTIME
] = 0;
361 ioctl (remote_desc
, TCSETA
, &termio
);
369 ioctl (remote_desc
, TIOCGETP
, &sg
);
371 ioctl (remote_desc
, TIOCSETP
, &sg
);
375 fprintf (stderr
, "Remote debugging using %s\n", name
);
377 enable_async_notification (remote_desc
);
379 /* Register the event loop handler. */
380 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
382 #endif /* USE_WIN32API */
387 struct sockaddr_in sockaddr
;
389 len
= sizeof (sockaddr
);
390 if (getsockname (listen_desc
,
391 (struct sockaddr
*) &sockaddr
, &len
) < 0
392 || len
< sizeof (sockaddr
))
393 perror_with_name ("Can't determine port");
394 port
= ntohs (sockaddr
.sin_port
);
396 fprintf (stderr
, "Listening on port %d\n", port
);
399 /* Register the event loop handler. */
400 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
407 delete_file_handler (remote_desc
);
410 closesocket (remote_desc
);
412 if (! remote_connection_is_stdio ())
415 remote_desc
= INVALID_DESCRIPTOR
;
420 /* Convert hex digit A to a number. */
425 if (a
>= '0' && a
<= '9')
427 else if (a
>= 'a' && a
<= 'f')
430 error ("Reply contains invalid hex digit");
436 static const char hexchars
[] = "0123456789abcdef";
439 ishex (int ch
, int *val
)
441 if ((ch
>= 'a') && (ch
<= 'f'))
443 *val
= ch
- 'a' + 10;
446 if ((ch
>= 'A') && (ch
<= 'F'))
448 *val
= ch
- 'A' + 10;
451 if ((ch
>= '0') && (ch
<= '9'))
459 #ifndef IN_PROCESS_AGENT
462 unhexify (char *bin
, const char *hex
, int count
)
466 for (i
= 0; i
< count
; i
++)
468 if (hex
[0] == 0 || hex
[1] == 0)
470 /* Hex string is short, or of uneven length.
471 Return the count that has been converted so far. */
474 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
481 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
488 for (i
= 0; i
< len
; i
++)
492 addr
= addr
| (fromhex (ch
) & 0x0f);
498 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
503 while (*end
!= '\0' && *end
!= ';')
506 decode_address (addrp
, start
, end
- start
);
515 /* Convert number NIB to a hex digit. */
523 return 'a' + nib
- 10;
526 #ifndef IN_PROCESS_AGENT
529 hexify (char *hex
, const char *bin
, int count
)
533 /* May use a length, or a nul-terminated string as input. */
535 count
= strlen (bin
);
537 for (i
= 0; i
< count
; i
++)
539 *hex
++ = tohex ((*bin
>> 4) & 0xf);
540 *hex
++ = tohex (*bin
++ & 0xf);
546 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
547 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
548 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
549 (which may be more than *OUT_LEN due to escape characters). The
550 total number of bytes in the output buffer will be at most
554 remote_escape_output (const gdb_byte
*buffer
, int len
,
555 gdb_byte
*out_buf
, int *out_len
,
558 int input_index
, output_index
;
561 for (input_index
= 0; input_index
< len
; input_index
++)
563 gdb_byte b
= buffer
[input_index
];
565 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
567 /* These must be escaped. */
568 if (output_index
+ 2 > out_maxlen
)
570 out_buf
[output_index
++] = '}';
571 out_buf
[output_index
++] = b
^ 0x20;
575 if (output_index
+ 1 > out_maxlen
)
577 out_buf
[output_index
++] = b
;
581 *out_len
= input_index
;
585 /* Convert BUFFER, escaped data LEN bytes long, into binary data
586 in OUT_BUF. Return the number of bytes written to OUT_BUF.
587 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
589 This function reverses remote_escape_output. It allows more
590 escaped characters than that function does, in particular because
591 '*' must be escaped to avoid the run-length encoding processing
592 in reading packets. */
595 remote_unescape_input (const gdb_byte
*buffer
, int len
,
596 gdb_byte
*out_buf
, int out_maxlen
)
598 int input_index
, output_index
;
603 for (input_index
= 0; input_index
< len
; input_index
++)
605 gdb_byte b
= buffer
[input_index
];
607 if (output_index
+ 1 > out_maxlen
)
608 error ("Received too much data from the target.");
612 out_buf
[output_index
++] = b
^ 0x20;
618 out_buf
[output_index
++] = b
;
622 error ("Unmatched escape character in target response.");
627 /* Look for a sequence of characters which can be run-length encoded.
628 If there are any, update *CSUM and *P. Otherwise, output the
629 single character. Return the number of characters consumed. */
632 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
636 /* Always output the character. */
640 /* Don't go past '~'. */
644 for (n
= 1; n
< remaining
; n
++)
645 if (buf
[n
] != buf
[0])
648 /* N is the index of the first character not the same as buf[0].
649 buf[0] is counted twice, so by decrementing N, we get the number
650 of characters the RLE sequence will replace. */
656 /* Skip the frame characters. The manual says to skip '+' and '-'
657 also, but there's no reason to. Unfortunately these two unusable
658 characters double the encoded length of a four byte zero
660 while (n
+ 29 == '$' || n
+ 29 == '#')
674 unpack_varlen_hex (char *buff
, /* packet to parse */
680 while (ishex (*buff
, &nibble
))
683 retval
= retval
<< 4;
684 retval
|= nibble
& 0x0f;
690 #ifndef IN_PROCESS_AGENT
692 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
695 write_ptid (char *buf
, ptid_t ptid
)
701 pid
= ptid_get_pid (ptid
);
703 buf
+= sprintf (buf
, "p-%x.", -pid
);
705 buf
+= sprintf (buf
, "p%x.", pid
);
707 tid
= ptid_get_lwp (ptid
);
709 buf
+= sprintf (buf
, "-%x", -tid
);
711 buf
+= sprintf (buf
, "%x", tid
);
717 hex_or_minus_one (char *buf
, char **obuf
)
721 if (strncmp (buf
, "-1", 2) == 0)
727 buf
= unpack_varlen_hex (buf
, &ret
);
735 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
736 passed the last parsed char. Returns null_ptid on error. */
738 read_ptid (char *buf
, char **obuf
)
742 ULONGEST pid
= 0, tid
= 0;
746 /* Multi-process ptid. */
747 pp
= unpack_varlen_hex (p
+ 1, &pid
);
749 error ("invalid remote ptid: %s\n", p
);
753 tid
= hex_or_minus_one (p
, &pp
);
757 return ptid_build (pid
, tid
, 0);
760 /* No multi-process. Just a tid. */
761 tid
= hex_or_minus_one (p
, &pp
);
763 /* Since the stub is not sending a process id, then default to
764 what's in the current inferior. */
765 pid
= ptid_get_pid (current_ptid
);
769 return ptid_build (pid
, tid
, 0);
772 /* Write COUNT bytes in BUF to the client.
773 The result is the number of bytes written or -1 if error.
774 This may return less than COUNT. */
777 write_prim (const void *buf
, int count
)
779 if (remote_connection_is_stdio ())
780 return write (fileno (stdout
), buf
, count
);
782 return write (remote_desc
, buf
, count
);
785 /* Read COUNT bytes from the client and store in BUF.
786 The result is the number of bytes read or -1 if error.
787 This may return less than COUNT. */
790 read_prim (void *buf
, int count
)
792 if (remote_connection_is_stdio ())
793 return read (fileno (stdin
), buf
, count
);
795 return read (remote_desc
, buf
, count
);
798 /* Send a packet to the remote machine, with error checking.
799 The data of the packet is in BUF, and the length of the
800 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
803 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
806 unsigned char csum
= 0;
811 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
813 /* Copy the packet into buffer BUF2, encapsulating it
814 and giving it a checksum. */
822 for (i
= 0; i
< cnt
;)
823 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
826 *p
++ = tohex ((csum
>> 4) & 0xf);
827 *p
++ = tohex (csum
& 0xf);
831 /* Send it over and over until we get a positive ack. */
835 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
837 perror ("putpkt(write)");
842 if (noack_mode
|| is_notif
)
844 /* Don't expect an ack then. */
848 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
850 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
858 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
872 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
876 /* Check for an input interrupt while we're here. */
877 if (cc
== '\003' && current_inferior
!= NULL
)
878 (*the_target
->request_interrupt
) ();
883 return 1; /* Success! */
887 putpkt_binary (char *buf
, int cnt
)
889 return putpkt_binary_1 (buf
, cnt
, 0);
892 /* Send a packet to the remote machine, with error checking. The data
893 of the packet is in BUF, and the packet should be a NUL-terminated
894 string. Returns >= 0 on success, -1 otherwise. */
899 return putpkt_binary (buf
, strlen (buf
));
903 putpkt_notif (char *buf
)
905 return putpkt_binary_1 (buf
, strlen (buf
), 1);
908 /* Come here when we get an input interrupt from the remote side. This
909 interrupt should only be active while we are waiting for the child to do
910 something. Thus this assumes readchar:bufcnt is 0.
911 About the only thing that should come through is a ^C, which
912 will cause us to request child interruption. */
915 input_interrupt (int unused
)
918 struct timeval immediate
= { 0, 0 };
920 /* Protect against spurious interrupts. This has been observed to
921 be a problem under NetBSD 1.4 and 1.5. */
924 FD_SET (remote_desc
, &readset
);
925 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
930 cc
= read_prim (&c
, 1);
932 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
934 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
939 (*the_target
->request_interrupt
) ();
943 /* Check if the remote side sent us an interrupt request (^C). */
945 check_remote_input_interrupt_request (void)
947 /* This function may be called before establishing communications,
948 therefore we need to validate the remote descriptor. */
950 if (remote_desc
== INVALID_DESCRIPTOR
)
956 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
957 accept Control-C from the client, and must be disabled when talking to
961 unblock_async_io (void)
966 sigemptyset (&sigio_set
);
967 sigaddset (&sigio_set
, SIGIO
);
968 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
974 nto_comctrl (int enable
)
976 struct sigevent event
;
980 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
981 event
.sigev_signo
= SIGIO
;
982 event
.sigev_code
= 0;
983 event
.sigev_value
.sival_ptr
= NULL
;
984 event
.sigev_priority
= -1;
985 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
989 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
994 /* Current state of asynchronous I/O. */
995 static int async_io_enabled
;
997 /* Enable asynchronous I/O. */
999 enable_async_io (void)
1001 if (async_io_enabled
)
1004 #ifndef USE_WIN32API
1005 signal (SIGIO
, input_interrupt
);
1007 async_io_enabled
= 1;
1010 #endif /* __QNX__ */
1013 /* Disable asynchronous I/O. */
1015 disable_async_io (void)
1017 if (!async_io_enabled
)
1020 #ifndef USE_WIN32API
1021 signal (SIGIO
, SIG_IGN
);
1023 async_io_enabled
= 0;
1026 #endif /* __QNX__ */
1031 initialize_async_io (void)
1033 /* Make sure that async I/O starts disabled. */
1034 async_io_enabled
= 1;
1035 disable_async_io ();
1037 /* Make sure the signal is unblocked. */
1038 unblock_async_io ();
1041 /* Internal buffer used by readchar.
1042 These are global to readchar because reschedule_remote needs to be
1043 able to tell whether the buffer is empty. */
1045 static unsigned char readchar_buf
[BUFSIZ
];
1046 static int readchar_bufcnt
= 0;
1047 static unsigned char *readchar_bufp
;
1049 /* Returns next char from remote GDB. -1 if error. */
1056 if (readchar_bufcnt
== 0)
1058 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
1060 if (readchar_bufcnt
<= 0)
1062 if (readchar_bufcnt
== 0)
1063 fprintf (stderr
, "readchar: Got EOF\n");
1065 perror ("readchar");
1070 readchar_bufp
= readchar_buf
;
1074 ch
= *readchar_bufp
++;
1079 /* Reset the readchar state machine. */
1082 reset_readchar (void)
1084 readchar_bufcnt
= 0;
1085 if (readchar_callback
!= NOT_SCHEDULED
)
1087 delete_callback_event (readchar_callback
);
1088 readchar_callback
= NOT_SCHEDULED
;
1092 /* Process remaining data in readchar_buf. */
1095 process_remaining (void *context
)
1099 /* This is a one-shot event. */
1100 readchar_callback
= NOT_SCHEDULED
;
1102 if (readchar_bufcnt
> 0)
1103 res
= handle_serial_event (0, NULL
);
1110 /* If there is still data in the buffer, queue another event to process it,
1111 we can't sleep in select yet. */
1116 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
1117 readchar_callback
= append_callback_event (process_remaining
, NULL
);
1120 /* Read a packet from the remote machine, with error checking,
1121 and store it in BUF. Returns length of packet, or negative if error. */
1127 unsigned char csum
, c1
, c2
;
1141 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1162 c1
= fromhex (readchar ());
1163 c2
= fromhex (readchar ());
1165 if (csum
== (c1
<< 4) + c2
)
1171 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1172 "buf=%s [no-ack-mode, Bad medium?]\n",
1173 (c1
<< 4) + c2
, csum
, buf
);
1174 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1178 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1179 (c1
<< 4) + c2
, csum
, buf
);
1180 if (write_prim ("-", 1) != 1)
1188 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1192 if (write_prim ("+", 1) != 1)
1197 fprintf (stderr
, "[sent ack]\n");
1205 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1214 write_ok (char *buf
)
1222 write_enn (char *buf
)
1224 /* Some day, we should define the meanings of the error codes... */
1234 convert_int_to_ascii (const unsigned char *from
, char *to
, int n
)
1241 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1242 *to
++ = tohex (nib
);
1244 *to
++ = tohex (nib
);
1249 #ifndef IN_PROCESS_AGENT
1252 convert_ascii_to_int (const char *from
, unsigned char *to
, int n
)
1257 nib1
= fromhex (*from
++);
1258 nib2
= fromhex (*from
++);
1259 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1264 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1266 if ((regno
>> 12) != 0)
1267 *buf
++ = tohex ((regno
>> 12) & 0xf);
1268 if ((regno
>> 8) != 0)
1269 *buf
++ = tohex ((regno
>> 8) & 0xf);
1270 *buf
++ = tohex ((regno
>> 4) & 0xf);
1271 *buf
++ = tohex (regno
& 0xf);
1273 collect_register_as_string (regcache
, regno
, buf
);
1274 buf
+= 2 * register_size (regcache
->tdesc
, regno
);
1281 new_thread_notify (int id
)
1285 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1289 if (server_waiting
== 0)
1292 sprintf (own_buf
, "n%x", id
);
1293 disable_async_io ();
1299 dead_thread_notify (int id
)
1303 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1307 sprintf (own_buf
, "x%x", id
);
1308 disable_async_io ();
1314 prepare_resume_reply (char *buf
, ptid_t ptid
,
1315 struct target_waitstatus
*status
)
1318 fprintf (stderr
, "Writing resume reply for %s:%d\n",
1319 target_pid_to_str (ptid
), status
->kind
);
1321 switch (status
->kind
)
1323 case TARGET_WAITKIND_STOPPED
:
1325 struct thread_info
*saved_inferior
;
1327 struct regcache
*regcache
;
1329 sprintf (buf
, "T%02x", status
->value
.sig
);
1330 buf
+= strlen (buf
);
1332 saved_inferior
= current_inferior
;
1334 current_inferior
= find_thread_ptid (ptid
);
1336 regp
= current_target_desc ()->expedite_regs
;
1338 regcache
= get_thread_regcache (current_inferior
, 1);
1340 if (the_target
->stopped_by_watchpoint
!= NULL
1341 && (*the_target
->stopped_by_watchpoint
) ())
1346 strncpy (buf
, "watch:", 6);
1349 addr
= (*the_target
->stopped_data_address
) ();
1351 /* Convert each byte of the address into two hexadecimal
1352 chars. Note that we take sizeof (void *) instead of
1353 sizeof (addr); this is to avoid sending a 64-bit
1354 address to a 32-bit GDB. */
1355 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1356 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1362 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1367 /* Formerly, if the debugger had not used any thread features
1368 we would not burden it with a thread status response. This
1369 was for the benefit of GDB 4.13 and older. However, in
1370 recent GDB versions the check (``if (cont_thread != 0)'')
1371 does not have the desired effect because of sillyness in
1372 the way that the remote protocol handles specifying a
1373 thread. Since thread support relies on qSymbol support
1374 anyway, assume GDB can handle threads. */
1376 if (using_threads
&& !disable_packet_Tthread
)
1378 /* This if (1) ought to be unnecessary. But remote_wait
1379 in GDB will claim this event belongs to inferior_ptid
1380 if we do not specify a thread, and there's no way for
1381 gdbserver to know what inferior_ptid is. */
1382 if (1 || !ptid_equal (general_thread
, ptid
))
1385 /* In non-stop, don't change the general thread behind
1388 general_thread
= ptid
;
1389 sprintf (buf
, "thread:");
1390 buf
+= strlen (buf
);
1391 buf
= write_ptid (buf
, ptid
);
1393 buf
+= strlen (buf
);
1395 core
= target_core_of_thread (ptid
);
1399 sprintf (buf
, "core:");
1400 buf
+= strlen (buf
);
1401 sprintf (buf
, "%x", core
);
1403 buf
+= strlen (buf
);
1410 strcpy (buf
, "library:;");
1411 buf
+= strlen (buf
);
1415 current_inferior
= saved_inferior
;
1418 case TARGET_WAITKIND_EXITED
:
1420 sprintf (buf
, "W%x;process:%x",
1421 status
->value
.integer
, ptid_get_pid (ptid
));
1423 sprintf (buf
, "W%02x", status
->value
.integer
);
1425 case TARGET_WAITKIND_SIGNALLED
:
1427 sprintf (buf
, "X%x;process:%x",
1428 status
->value
.sig
, ptid_get_pid (ptid
));
1430 sprintf (buf
, "X%02x", status
->value
.sig
);
1433 error ("unhandled waitkind");
1439 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1443 *mem_addr_ptr
= *len_ptr
= 0;
1445 while ((ch
= from
[i
++]) != ',')
1447 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1448 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1451 for (j
= 0; j
< 4; j
++)
1453 if ((ch
= from
[i
++]) == 0)
1455 *len_ptr
= *len_ptr
<< 4;
1456 *len_ptr
|= fromhex (ch
) & 0x0f;
1461 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1462 unsigned char **to_p
)
1466 *mem_addr_ptr
= *len_ptr
= 0;
1468 while ((ch
= from
[i
++]) != ',')
1470 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1471 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1474 while ((ch
= from
[i
++]) != ':')
1476 *len_ptr
= *len_ptr
<< 4;
1477 *len_ptr
|= fromhex (ch
) & 0x0f;
1481 *to_p
= xmalloc (*len_ptr
);
1483 convert_ascii_to_int (&from
[i
++], *to_p
, *len_ptr
);
1487 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1488 unsigned int *len_ptr
, unsigned char **to_p
)
1492 *mem_addr_ptr
= *len_ptr
= 0;
1494 while ((ch
= from
[i
++]) != ',')
1496 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1497 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1500 while ((ch
= from
[i
++]) != ':')
1502 *len_ptr
= *len_ptr
<< 4;
1503 *len_ptr
|= fromhex (ch
) & 0x0f;
1507 *to_p
= xmalloc (*len_ptr
);
1509 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1510 *to_p
, *len_ptr
) != *len_ptr
)
1516 /* Decode a qXfer write request. */
1519 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1520 unsigned int *len
, unsigned char *data
)
1525 /* Extract the offset. */
1527 while ((ch
= *buf
++) != ':')
1529 *offset
= *offset
<< 4;
1530 *offset
|= fromhex (ch
) & 0x0f;
1533 /* Get encoded data. */
1534 packet_len
-= buf
- b
;
1535 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1540 /* Decode the parameters of a qSearch:memory packet. */
1543 decode_search_memory_packet (const char *buf
, int packet_len
,
1544 CORE_ADDR
*start_addrp
,
1545 CORE_ADDR
*search_space_lenp
,
1546 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1548 const char *p
= buf
;
1550 p
= decode_address_to_semicolon (start_addrp
, p
);
1551 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1552 packet_len
-= p
- buf
;
1553 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1554 pattern
, packet_len
);
1559 free_sym_cache (struct sym_cache
*sym
)
1569 clear_symbol_cache (struct sym_cache
**symcache_p
)
1571 struct sym_cache
*sym
, *next
;
1573 /* Check the cache first. */
1574 for (sym
= *symcache_p
; sym
; sym
= next
)
1577 free_sym_cache (sym
);
1583 /* Get the address of NAME, and return it in ADDRP if found. if
1584 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1585 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1588 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1590 char own_buf
[266], *p
, *q
;
1592 struct sym_cache
*sym
;
1593 struct process_info
*proc
;
1595 proc
= current_process ();
1597 /* Check the cache first. */
1598 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1599 if (strcmp (name
, sym
->name
) == 0)
1605 /* It might not be an appropriate time to look up a symbol,
1606 e.g. while we're trying to fetch registers. */
1610 /* Send the request. */
1611 strcpy (own_buf
, "qSymbol:");
1612 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1613 if (putpkt (own_buf
) < 0)
1616 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1617 len
= getpkt (own_buf
);
1621 /* We ought to handle pretty much any packet at this point while we
1622 wait for the qSymbol "response". That requires re-entering the
1623 main loop. For now, this is an adequate approximation; allow
1624 GDB to read from memory while it figures out the address of the
1626 while (own_buf
[0] == 'm')
1629 unsigned char *mem_buf
;
1630 unsigned int mem_len
;
1632 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1633 mem_buf
= xmalloc (mem_len
);
1634 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1635 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1637 write_enn (own_buf
);
1639 if (putpkt (own_buf
) < 0)
1641 len
= getpkt (own_buf
);
1646 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1648 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1652 p
= own_buf
+ strlen ("qSymbol:");
1654 while (*q
&& *q
!= ':')
1657 /* Make sure we found a value for the symbol. */
1658 if (p
== q
|| *q
== '\0')
1661 decode_address (addrp
, p
, q
- p
);
1663 /* Save the symbol in our cache. */
1664 sym
= xmalloc (sizeof (*sym
));
1665 sym
->name
= xstrdup (name
);
1667 sym
->next
= proc
->symbol_cache
;
1668 proc
->symbol_cache
= sym
;
1673 /* Relocate an instruction to execute at a different address. OLDLOC
1674 is the address in the inferior memory where the instruction to
1675 relocate is currently at. On input, TO points to the destination
1676 where we want the instruction to be copied (and possibly adjusted)
1677 to. On output, it points to one past the end of the resulting
1678 instruction(s). The effect of executing the instruction at TO
1679 shall be the same as if executing it at OLDLOC. For example, call
1680 instructions that implicitly push the return address on the stack
1681 should be adjusted to return to the instruction after OLDLOC;
1682 relative branches, and other PC-relative instructions need the
1683 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1686 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1690 ULONGEST written
= 0;
1692 /* Send the request. */
1693 strcpy (own_buf
, "qRelocInsn:");
1694 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1696 if (putpkt (own_buf
) < 0)
1699 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1700 len
= getpkt (own_buf
);
1704 /* We ought to handle pretty much any packet at this point while we
1705 wait for the qRelocInsn "response". That requires re-entering
1706 the main loop. For now, this is an adequate approximation; allow
1707 GDB to access memory. */
1708 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1711 unsigned char *mem_buf
= NULL
;
1712 unsigned int mem_len
;
1714 if (own_buf
[0] == 'm')
1716 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1717 mem_buf
= xmalloc (mem_len
);
1718 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1719 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1721 write_enn (own_buf
);
1723 else if (own_buf
[0] == 'X')
1725 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1726 &mem_len
, &mem_buf
) < 0
1727 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1728 write_enn (own_buf
);
1734 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1735 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1738 write_enn (own_buf
);
1741 if (putpkt (own_buf
) < 0)
1743 len
= getpkt (own_buf
);
1748 if (own_buf
[0] == 'E')
1750 warning ("An error occurred while relocating an instruction: %s\n",
1755 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1757 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1762 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1769 monitor_output (const char *msg
)
1771 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1774 hexify (buf
+ 1, msg
, 0);