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, 2008, 2009, 2010, 2011
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 3 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, see <http://www.gnu.org/licenses/>. */
27 #include <sys/ioctl.h>
33 #include <netinet/in.h>
36 #include <sys/socket.h>
41 #if HAVE_NETINET_TCP_H
42 #include <netinet/tcp.h>
45 #include <sys/ioctl.h>
58 #include <arpa/inet.h>
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 gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
111 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
113 /* FIXME headerize? */
114 extern int using_threads
;
115 extern int debug_threads
;
117 /* If true, then GDB has requested noack mode. */
119 /* If true, then we tell GDB to use noack mode by default. */
120 int transport_is_reliable
= 0;
123 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
124 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
130 return remote_desc
!= INVALID_DESCRIPTOR
;
134 enable_async_notification (int fd
)
136 #if defined(F_SETFL) && defined (FASYNC)
137 int save_fcntl_flags
;
139 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
140 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
141 #if defined (F_SETOWN)
142 fcntl (fd
, F_SETOWN
, getpid ());
148 handle_accept_event (int err
, gdb_client_data client_data
)
150 struct sockaddr_in sockaddr
;
154 fprintf (stderr
, "handling possible accept event\n");
156 tmp
= sizeof (sockaddr
);
157 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
158 if (remote_desc
== -1)
159 perror_with_name ("Accept failed");
161 /* Enable TCP keep alive process. */
163 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
164 (char *) &tmp
, sizeof (tmp
));
166 /* Tell TCP not to delay small packets. This greatly speeds up
167 interactive response. */
169 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
170 (char *) &tmp
, sizeof (tmp
));
173 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
174 exits when the remote side dies. */
180 close (listen_desc
); /* No longer need this */
182 closesocket (listen_desc
); /* No longer need this */
186 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
187 descriptor open for add_file_handler to wait for a new connection. */
188 delete_file_handler (listen_desc
);
190 /* Convert IP address to string. */
191 fprintf (stderr
, "Remote debugging from host %s\n",
192 inet_ntoa (sockaddr
.sin_addr
));
194 enable_async_notification (remote_desc
);
196 /* Register the event loop handler. */
197 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
199 /* We have a new GDB connection now. If we were disconnected
200 tracing, there's a window where the target could report a stop
201 event to the event loop, and since we have a connection now, we'd
202 try to send vStopped notifications to GDB. But, don't do that
203 until GDB as selected all-stop/non-stop, and has queried the
204 threads' status ('?'). */
210 /* Prepare for a later connection to a remote debugger.
211 NAME is the filename used for communication. */
214 remote_prepare (char *name
)
218 static int winsock_initialized
;
221 struct sockaddr_in sockaddr
;
225 port_str
= strchr (name
, ':');
226 if (port_str
== NULL
)
228 transport_is_reliable
= 0;
232 port
= strtoul (port_str
+ 1, &port_end
, 10);
233 if (port_str
[1] == '\0' || *port_end
!= '\0')
234 fatal ("Bad port argument: %s", name
);
237 if (!winsock_initialized
)
241 WSAStartup (MAKEWORD (1, 0), &wsad
);
242 winsock_initialized
= 1;
246 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
247 if (listen_desc
== -1)
248 perror_with_name ("Can't open socket");
250 /* Allow rapid reuse of this port. */
252 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
255 sockaddr
.sin_family
= PF_INET
;
256 sockaddr
.sin_port
= htons (port
);
257 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
259 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
260 || listen (listen_desc
, 1))
261 perror_with_name ("Can't bind address");
263 transport_is_reliable
= 1;
266 /* Open a connection to a remote debugger.
267 NAME is the filename used for communication. */
270 remote_open (char *name
)
274 port_str
= strchr (name
, ':');
275 if (port_str
== NULL
)
278 error ("Only <host>:<port> is supported on this platform.");
282 if (stat (name
, &statbuf
) == 0
283 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
284 remote_desc
= open (name
, O_RDWR
);
292 perror_with_name ("Could not open remote device");
296 struct termios termios
;
297 tcgetattr (remote_desc
, &termios
);
302 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
303 termios
.c_cflag
|= CLOCAL
| CS8
;
304 termios
.c_cc
[VMIN
] = 1;
305 termios
.c_cc
[VTIME
] = 0;
307 tcsetattr (remote_desc
, TCSANOW
, &termios
);
313 struct termio termio
;
314 ioctl (remote_desc
, TCGETA
, &termio
);
319 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
320 termio
.c_cflag
|= CLOCAL
| CS8
;
321 termio
.c_cc
[VMIN
] = 1;
322 termio
.c_cc
[VTIME
] = 0;
324 ioctl (remote_desc
, TCSETA
, &termio
);
332 ioctl (remote_desc
, TIOCGETP
, &sg
);
334 ioctl (remote_desc
, TIOCSETP
, &sg
);
338 fprintf (stderr
, "Remote debugging using %s\n", name
);
340 enable_async_notification (remote_desc
);
342 /* Register the event loop handler. */
343 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
344 #endif /* USE_WIN32API */
350 struct sockaddr_in sockaddr
;
352 len
= sizeof (sockaddr
);
353 if (getsockname (listen_desc
,
354 (struct sockaddr
*) &sockaddr
, &len
) < 0
355 || len
< sizeof (sockaddr
))
356 perror_with_name ("Can't determine port");
357 port
= ntohs (sockaddr
.sin_port
);
359 fprintf (stderr
, "Listening on port %d\n", port
);
362 /* Register the event loop handler. */
363 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
370 delete_file_handler (remote_desc
);
373 closesocket (remote_desc
);
377 remote_desc
= INVALID_DESCRIPTOR
;
382 /* Convert hex digit A to a number. */
387 if (a
>= '0' && a
<= '9')
389 else if (a
>= 'a' && a
<= 'f')
392 error ("Reply contains invalid hex digit");
398 static const char hexchars
[] = "0123456789abcdef";
401 ishex (int ch
, int *val
)
403 if ((ch
>= 'a') && (ch
<= 'f'))
405 *val
= ch
- 'a' + 10;
408 if ((ch
>= 'A') && (ch
<= 'F'))
410 *val
= ch
- 'A' + 10;
413 if ((ch
>= '0') && (ch
<= '9'))
421 #ifndef IN_PROCESS_AGENT
424 unhexify (char *bin
, const char *hex
, int count
)
428 for (i
= 0; i
< count
; i
++)
430 if (hex
[0] == 0 || hex
[1] == 0)
432 /* Hex string is short, or of uneven length.
433 Return the count that has been converted so far. */
436 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
443 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
450 for (i
= 0; i
< len
; i
++)
454 addr
= addr
| (fromhex (ch
) & 0x0f);
460 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
465 while (*end
!= '\0' && *end
!= ';')
468 decode_address (addrp
, start
, end
- start
);
477 /* Convert number NIB to a hex digit. */
485 return 'a' + nib
- 10;
488 #ifndef IN_PROCESS_AGENT
491 hexify (char *hex
, const char *bin
, int count
)
495 /* May use a length, or a nul-terminated string as input. */
497 count
= strlen (bin
);
499 for (i
= 0; i
< count
; i
++)
501 *hex
++ = tohex ((*bin
>> 4) & 0xf);
502 *hex
++ = tohex (*bin
++ & 0xf);
508 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
509 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
510 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
511 (which may be more than *OUT_LEN due to escape characters). The
512 total number of bytes in the output buffer will be at most
516 remote_escape_output (const gdb_byte
*buffer
, int len
,
517 gdb_byte
*out_buf
, int *out_len
,
520 int input_index
, output_index
;
523 for (input_index
= 0; input_index
< len
; input_index
++)
525 gdb_byte b
= buffer
[input_index
];
527 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
529 /* These must be escaped. */
530 if (output_index
+ 2 > out_maxlen
)
532 out_buf
[output_index
++] = '}';
533 out_buf
[output_index
++] = b
^ 0x20;
537 if (output_index
+ 1 > out_maxlen
)
539 out_buf
[output_index
++] = b
;
543 *out_len
= input_index
;
547 /* Convert BUFFER, escaped data LEN bytes long, into binary data
548 in OUT_BUF. Return the number of bytes written to OUT_BUF.
549 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
551 This function reverses remote_escape_output. It allows more
552 escaped characters than that function does, in particular because
553 '*' must be escaped to avoid the run-length encoding processing
554 in reading packets. */
557 remote_unescape_input (const gdb_byte
*buffer
, int len
,
558 gdb_byte
*out_buf
, int out_maxlen
)
560 int input_index
, output_index
;
565 for (input_index
= 0; input_index
< len
; input_index
++)
567 gdb_byte b
= buffer
[input_index
];
569 if (output_index
+ 1 > out_maxlen
)
570 error ("Received too much data from the target.");
574 out_buf
[output_index
++] = b
^ 0x20;
580 out_buf
[output_index
++] = b
;
584 error ("Unmatched escape character in target response.");
589 /* Look for a sequence of characters which can be run-length encoded.
590 If there are any, update *CSUM and *P. Otherwise, output the
591 single character. Return the number of characters consumed. */
594 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
598 /* Always output the character. */
602 /* Don't go past '~'. */
606 for (n
= 1; n
< remaining
; n
++)
607 if (buf
[n
] != buf
[0])
610 /* N is the index of the first character not the same as buf[0].
611 buf[0] is counted twice, so by decrementing N, we get the number
612 of characters the RLE sequence will replace. */
618 /* Skip the frame characters. The manual says to skip '+' and '-'
619 also, but there's no reason to. Unfortunately these two unusable
620 characters double the encoded length of a four byte zero
622 while (n
+ 29 == '$' || n
+ 29 == '#')
636 unpack_varlen_hex (char *buff
, /* packet to parse */
642 while (ishex (*buff
, &nibble
))
645 retval
= retval
<< 4;
646 retval
|= nibble
& 0x0f;
652 #ifndef IN_PROCESS_AGENT
654 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
657 write_ptid (char *buf
, ptid_t ptid
)
663 pid
= ptid_get_pid (ptid
);
665 buf
+= sprintf (buf
, "p-%x.", -pid
);
667 buf
+= sprintf (buf
, "p%x.", pid
);
669 tid
= ptid_get_lwp (ptid
);
671 buf
+= sprintf (buf
, "-%x", -tid
);
673 buf
+= sprintf (buf
, "%x", tid
);
679 hex_or_minus_one (char *buf
, char **obuf
)
683 if (strncmp (buf
, "-1", 2) == 0)
689 buf
= unpack_varlen_hex (buf
, &ret
);
697 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
698 passed the last parsed char. Returns null_ptid on error. */
700 read_ptid (char *buf
, char **obuf
)
704 ULONGEST pid
= 0, tid
= 0;
708 /* Multi-process ptid. */
709 pp
= unpack_varlen_hex (p
+ 1, &pid
);
711 error ("invalid remote ptid: %s\n", p
);
715 tid
= hex_or_minus_one (p
, &pp
);
719 return ptid_build (pid
, tid
, 0);
722 /* No multi-process. Just a tid. */
723 tid
= hex_or_minus_one (p
, &pp
);
725 /* Since the stub is not sending a process id, then default to
726 what's in the current inferior. */
727 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
731 return ptid_build (pid
, tid
, 0);
734 /* Send a packet to the remote machine, with error checking.
735 The data of the packet is in BUF, and the length of the
736 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
739 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
742 unsigned char csum
= 0;
747 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
749 /* Copy the packet into buffer BUF2, encapsulating it
750 and giving it a checksum. */
758 for (i
= 0; i
< cnt
;)
759 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
762 *p
++ = tohex ((csum
>> 4) & 0xf);
763 *p
++ = tohex (csum
& 0xf);
767 /* Send it over and over until we get a positive ack. */
771 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
773 perror ("putpkt(write)");
778 if (noack_mode
|| is_notif
)
780 /* Don't expect an ack then. */
784 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
786 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
794 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
808 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
812 /* Check for an input interrupt while we're here. */
813 if (cc
== '\003' && current_inferior
!= NULL
)
814 (*the_target
->request_interrupt
) ();
819 return 1; /* Success! */
823 putpkt_binary (char *buf
, int cnt
)
825 return putpkt_binary_1 (buf
, cnt
, 0);
828 /* Send a packet to the remote machine, with error checking. The data
829 of the packet is in BUF, and the packet should be a NUL-terminated
830 string. Returns >= 0 on success, -1 otherwise. */
835 return putpkt_binary (buf
, strlen (buf
));
839 putpkt_notif (char *buf
)
841 return putpkt_binary_1 (buf
, strlen (buf
), 1);
844 /* Come here when we get an input interrupt from the remote side. This
845 interrupt should only be active while we are waiting for the child to do
846 something. Thus this assumes readchar:bufcnt is 0.
847 About the only thing that should come through is a ^C, which
848 will cause us to request child interruption. */
851 input_interrupt (int unused
)
854 struct timeval immediate
= { 0, 0 };
856 /* Protect against spurious interrupts. This has been observed to
857 be a problem under NetBSD 1.4 and 1.5. */
860 FD_SET (remote_desc
, &readset
);
861 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
866 cc
= read (remote_desc
, &c
, 1);
868 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
870 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
875 (*the_target
->request_interrupt
) ();
879 /* Check if the remote side sent us an interrupt request (^C). */
881 check_remote_input_interrupt_request (void)
883 /* This function may be called before establishing communications,
884 therefore we need to validate the remote descriptor. */
886 if (remote_desc
== INVALID_DESCRIPTOR
)
892 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
893 accept Control-C from the client, and must be disabled when talking to
897 unblock_async_io (void)
902 sigemptyset (&sigio_set
);
903 sigaddset (&sigio_set
, SIGIO
);
904 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
910 nto_comctrl (int enable
)
912 struct sigevent event
;
916 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
917 event
.sigev_signo
= SIGIO
;
918 event
.sigev_code
= 0;
919 event
.sigev_value
.sival_ptr
= NULL
;
920 event
.sigev_priority
= -1;
921 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
925 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
930 /* Current state of asynchronous I/O. */
931 static int async_io_enabled
;
933 /* Enable asynchronous I/O. */
935 enable_async_io (void)
937 if (async_io_enabled
)
941 signal (SIGIO
, input_interrupt
);
943 async_io_enabled
= 1;
949 /* Disable asynchronous I/O. */
951 disable_async_io (void)
953 if (!async_io_enabled
)
957 signal (SIGIO
, SIG_IGN
);
959 async_io_enabled
= 0;
967 initialize_async_io (void)
969 /* Make sure that async I/O starts disabled. */
970 async_io_enabled
= 1;
973 /* Make sure the signal is unblocked. */
977 /* Internal buffer used by readchar.
978 These are global to readchar because reschedule_remote needs to be
979 able to tell whether the buffer is empty. */
981 static unsigned char readchar_buf
[BUFSIZ
];
982 static int readchar_bufcnt
= 0;
983 static unsigned char *readchar_bufp
;
985 /* Returns next char from remote GDB. -1 if error. */
992 if (readchar_bufcnt
== 0)
994 readchar_bufcnt
= read (remote_desc
, readchar_buf
,
995 sizeof (readchar_buf
));
997 if (readchar_bufcnt
<= 0)
999 if (readchar_bufcnt
== 0)
1000 fprintf (stderr
, "readchar: Got EOF\n");
1002 perror ("readchar");
1007 readchar_bufp
= readchar_buf
;
1011 ch
= *readchar_bufp
++;
1016 /* Reset the readchar state machine. */
1019 reset_readchar (void)
1021 readchar_bufcnt
= 0;
1022 if (readchar_callback
!= NOT_SCHEDULED
)
1024 delete_callback_event (readchar_callback
);
1025 readchar_callback
= NOT_SCHEDULED
;
1029 /* Process remaining data in readchar_buf. */
1032 process_remaining (void *context
)
1036 /* This is a one-shot event. */
1037 readchar_callback
= NOT_SCHEDULED
;
1039 if (readchar_bufcnt
> 0)
1040 res
= handle_serial_event (0, NULL
);
1047 /* If there is still data in the buffer, queue another event to process it,
1048 we can't sleep in select yet. */
1053 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
1054 readchar_callback
= append_callback_event (process_remaining
, NULL
);
1057 /* Read a packet from the remote machine, with error checking,
1058 and store it in BUF. Returns length of packet, or negative if error. */
1064 unsigned char csum
, c1
, c2
;
1078 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1099 c1
= fromhex (readchar ());
1100 c2
= fromhex (readchar ());
1102 if (csum
== (c1
<< 4) + c2
)
1108 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1109 "buf=%s [no-ack-mode, Bad medium?]\n",
1110 (c1
<< 4) + c2
, csum
, buf
);
1111 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1115 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1116 (c1
<< 4) + c2
, csum
, buf
);
1117 if (write (remote_desc
, "-", 1) != 1)
1125 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1129 if (write (remote_desc
, "+", 1) != 1)
1134 fprintf (stderr
, "[sent ack]\n");
1142 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1151 write_ok (char *buf
)
1159 write_enn (char *buf
)
1161 /* Some day, we should define the meanings of the error codes... */
1171 convert_int_to_ascii (const unsigned char *from
, char *to
, int n
)
1178 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1179 *to
++ = tohex (nib
);
1181 *to
++ = tohex (nib
);
1186 #ifndef IN_PROCESS_AGENT
1189 convert_ascii_to_int (const char *from
, unsigned char *to
, int n
)
1194 nib1
= fromhex (*from
++);
1195 nib2
= fromhex (*from
++);
1196 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1201 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1203 if ((regno
>> 12) != 0)
1204 *buf
++ = tohex ((regno
>> 12) & 0xf);
1205 if ((regno
>> 8) != 0)
1206 *buf
++ = tohex ((regno
>> 8) & 0xf);
1207 *buf
++ = tohex ((regno
>> 4) & 0xf);
1208 *buf
++ = tohex (regno
& 0xf);
1210 collect_register_as_string (regcache
, regno
, buf
);
1211 buf
+= 2 * register_size (regno
);
1218 new_thread_notify (int id
)
1222 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1226 if (server_waiting
== 0)
1229 sprintf (own_buf
, "n%x", id
);
1230 disable_async_io ();
1236 dead_thread_notify (int id
)
1240 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1244 sprintf (own_buf
, "x%x", id
);
1245 disable_async_io ();
1251 prepare_resume_reply (char *buf
, ptid_t ptid
,
1252 struct target_waitstatus
*status
)
1255 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1256 target_pid_to_str (ptid
), status
->kind
);
1258 switch (status
->kind
)
1260 case TARGET_WAITKIND_STOPPED
:
1262 struct thread_info
*saved_inferior
;
1264 struct regcache
*regcache
;
1266 sprintf (buf
, "T%02x", status
->value
.sig
);
1267 buf
+= strlen (buf
);
1269 regp
= gdbserver_expedite_regs
;
1271 saved_inferior
= current_inferior
;
1273 current_inferior
= find_thread_ptid (ptid
);
1275 regcache
= get_thread_regcache (current_inferior
, 1);
1277 if (the_target
->stopped_by_watchpoint
!= NULL
1278 && (*the_target
->stopped_by_watchpoint
) ())
1283 strncpy (buf
, "watch:", 6);
1286 addr
= (*the_target
->stopped_data_address
) ();
1288 /* Convert each byte of the address into two hexadecimal
1289 chars. Note that we take sizeof (void *) instead of
1290 sizeof (addr); this is to avoid sending a 64-bit
1291 address to a 32-bit GDB. */
1292 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1293 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1299 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1304 /* Formerly, if the debugger had not used any thread features
1305 we would not burden it with a thread status response. This
1306 was for the benefit of GDB 4.13 and older. However, in
1307 recent GDB versions the check (``if (cont_thread != 0)'')
1308 does not have the desired effect because of sillyness in
1309 the way that the remote protocol handles specifying a
1310 thread. Since thread support relies on qSymbol support
1311 anyway, assume GDB can handle threads. */
1313 if (using_threads
&& !disable_packet_Tthread
)
1315 /* This if (1) ought to be unnecessary. But remote_wait
1316 in GDB will claim this event belongs to inferior_ptid
1317 if we do not specify a thread, and there's no way for
1318 gdbserver to know what inferior_ptid is. */
1319 if (1 || !ptid_equal (general_thread
, ptid
))
1322 /* In non-stop, don't change the general thread behind
1325 general_thread
= ptid
;
1326 sprintf (buf
, "thread:");
1327 buf
+= strlen (buf
);
1328 buf
= write_ptid (buf
, ptid
);
1330 buf
+= strlen (buf
);
1332 if (the_target
->core_of_thread
)
1333 core
= (*the_target
->core_of_thread
) (ptid
);
1336 sprintf (buf
, "core:");
1337 buf
+= strlen (buf
);
1338 sprintf (buf
, "%x", core
);
1340 buf
+= strlen (buf
);
1347 strcpy (buf
, "library:;");
1348 buf
+= strlen (buf
);
1352 current_inferior
= saved_inferior
;
1355 case TARGET_WAITKIND_EXITED
:
1357 sprintf (buf
, "W%x;process:%x",
1358 status
->value
.integer
, ptid_get_pid (ptid
));
1360 sprintf (buf
, "W%02x", status
->value
.integer
);
1362 case TARGET_WAITKIND_SIGNALLED
:
1364 sprintf (buf
, "X%x;process:%x",
1365 status
->value
.sig
, ptid_get_pid (ptid
));
1367 sprintf (buf
, "X%02x", status
->value
.sig
);
1370 error ("unhandled waitkind");
1376 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1380 *mem_addr_ptr
= *len_ptr
= 0;
1382 while ((ch
= from
[i
++]) != ',')
1384 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1385 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1388 for (j
= 0; j
< 4; j
++)
1390 if ((ch
= from
[i
++]) == 0)
1392 *len_ptr
= *len_ptr
<< 4;
1393 *len_ptr
|= fromhex (ch
) & 0x0f;
1398 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1399 unsigned char **to_p
)
1403 *mem_addr_ptr
= *len_ptr
= 0;
1405 while ((ch
= from
[i
++]) != ',')
1407 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1408 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1411 while ((ch
= from
[i
++]) != ':')
1413 *len_ptr
= *len_ptr
<< 4;
1414 *len_ptr
|= fromhex (ch
) & 0x0f;
1418 *to_p
= xmalloc (*len_ptr
);
1420 convert_ascii_to_int (&from
[i
++], *to_p
, *len_ptr
);
1424 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1425 unsigned int *len_ptr
, unsigned char **to_p
)
1429 *mem_addr_ptr
= *len_ptr
= 0;
1431 while ((ch
= from
[i
++]) != ',')
1433 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1434 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1437 while ((ch
= from
[i
++]) != ':')
1439 *len_ptr
= *len_ptr
<< 4;
1440 *len_ptr
|= fromhex (ch
) & 0x0f;
1444 *to_p
= xmalloc (*len_ptr
);
1446 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1447 *to_p
, *len_ptr
) != *len_ptr
)
1453 /* Decode a qXfer write request. */
1456 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1457 unsigned int *len
, unsigned char *data
)
1462 /* Extract the offset. */
1464 while ((ch
= *buf
++) != ':')
1466 *offset
= *offset
<< 4;
1467 *offset
|= fromhex (ch
) & 0x0f;
1470 /* Get encoded data. */
1471 packet_len
-= buf
- b
;
1472 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1477 /* Decode the parameters of a qSearch:memory packet. */
1480 decode_search_memory_packet (const char *buf
, int packet_len
,
1481 CORE_ADDR
*start_addrp
,
1482 CORE_ADDR
*search_space_lenp
,
1483 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1485 const char *p
= buf
;
1487 p
= decode_address_to_semicolon (start_addrp
, p
);
1488 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1489 packet_len
-= p
- buf
;
1490 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1491 pattern
, packet_len
);
1496 free_sym_cache (struct sym_cache
*sym
)
1506 clear_symbol_cache (struct sym_cache
**symcache_p
)
1508 struct sym_cache
*sym
, *next
;
1510 /* Check the cache first. */
1511 for (sym
= *symcache_p
; sym
; sym
= next
)
1514 free_sym_cache (sym
);
1520 /* Get the address of NAME, and return it in ADDRP if found. if
1521 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1522 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1525 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1527 char own_buf
[266], *p
, *q
;
1529 struct sym_cache
*sym
;
1530 struct process_info
*proc
;
1532 proc
= current_process ();
1534 /* Check the cache first. */
1535 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1536 if (strcmp (name
, sym
->name
) == 0)
1542 /* It might not be an appropriate time to look up a symbol,
1543 e.g. while we're trying to fetch registers. */
1547 /* Send the request. */
1548 strcpy (own_buf
, "qSymbol:");
1549 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1550 if (putpkt (own_buf
) < 0)
1553 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1554 len
= getpkt (own_buf
);
1558 /* We ought to handle pretty much any packet at this point while we
1559 wait for the qSymbol "response". That requires re-entering the
1560 main loop. For now, this is an adequate approximation; allow
1561 GDB to read from memory while it figures out the address of the
1563 while (own_buf
[0] == 'm')
1566 unsigned char *mem_buf
;
1567 unsigned int mem_len
;
1569 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1570 mem_buf
= xmalloc (mem_len
);
1571 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1572 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1574 write_enn (own_buf
);
1576 if (putpkt (own_buf
) < 0)
1578 len
= getpkt (own_buf
);
1583 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1585 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1589 p
= own_buf
+ strlen ("qSymbol:");
1591 while (*q
&& *q
!= ':')
1594 /* Make sure we found a value for the symbol. */
1595 if (p
== q
|| *q
== '\0')
1598 decode_address (addrp
, p
, q
- p
);
1600 /* Save the symbol in our cache. */
1601 sym
= xmalloc (sizeof (*sym
));
1602 sym
->name
= xstrdup (name
);
1604 sym
->next
= proc
->symbol_cache
;
1605 proc
->symbol_cache
= sym
;
1610 /* Relocate an instruction to execute at a different address. OLDLOC
1611 is the address in the inferior memory where the instruction to
1612 relocate is currently at. On input, TO points to the destination
1613 where we want the instruction to be copied (and possibly adjusted)
1614 to. On output, it points to one past the end of the resulting
1615 instruction(s). The effect of executing the instruction at TO
1616 shall be the same as if executing it at FROM. For example, call
1617 instructions that implicitly push the return address on the stack
1618 should be adjusted to return to the instruction after OLDLOC;
1619 relative branches, and other PC-relative instructions need the
1620 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1623 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1627 ULONGEST written
= 0;
1629 /* Send the request. */
1630 strcpy (own_buf
, "qRelocInsn:");
1631 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1633 if (putpkt (own_buf
) < 0)
1636 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1637 len
= getpkt (own_buf
);
1641 /* We ought to handle pretty much any packet at this point while we
1642 wait for the qRelocInsn "response". That requires re-entering
1643 the main loop. For now, this is an adequate approximation; allow
1644 GDB to access memory. */
1645 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1648 unsigned char *mem_buf
= NULL
;
1649 unsigned int mem_len
;
1651 if (own_buf
[0] == 'm')
1653 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1654 mem_buf
= xmalloc (mem_len
);
1655 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1656 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1658 write_enn (own_buf
);
1660 else if (own_buf
[0] == 'X')
1662 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1663 &mem_len
, &mem_buf
) < 0
1664 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1665 write_enn (own_buf
);
1671 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1672 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1675 write_enn (own_buf
);
1678 if (putpkt (own_buf
) < 0)
1680 len
= getpkt (own_buf
);
1685 if (own_buf
[0] == 'E')
1687 warning ("An error occurred while relocating an instruction: %s\n",
1692 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1694 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1699 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1706 monitor_output (const char *msg
)
1708 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1711 hexify (buf
+ 1, msg
, 0);