1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2015 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"
28 #include <sys/ioctl.h>
34 #include <netinet/in.h>
37 #include <sys/socket.h>
42 #if HAVE_NETINET_TCP_H
43 #include <netinet/tcp.h>
46 #include <sys/ioctl.h>
54 #include "gdb_sys_time.h"
57 #include <arpa/inet.h>
66 #include <sys/iomgr.h>
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t
;
73 #ifndef IN_PROCESS_AGENT
76 # define INVALID_DESCRIPTOR INVALID_SOCKET
78 # define INVALID_DESCRIPTOR -1
81 /* Extra value for readchar_callback. */
83 /* The callback is currently not scheduled. */
87 /* Status of the readchar callback.
88 Either NOT_SCHEDULED or the callback id. */
89 static int readchar_callback
= NOT_SCHEDULED
;
91 static int readchar (void);
92 static void reset_readchar (void);
93 static void reschedule (void);
95 /* A cache entry for a successfully looked-up symbol. */
100 struct sym_cache
*next
;
103 int remote_debug
= 0;
104 struct ui_file
*gdb_stdlog
;
106 static int remote_is_stdio
= 0;
108 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
109 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
111 /* FIXME headerize? */
112 extern int using_threads
;
113 extern int debug_threads
;
115 /* If true, then GDB has requested noack mode. */
117 /* If true, then we tell GDB to use noack mode by default. */
118 int transport_is_reliable
= 0;
121 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
122 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
128 return remote_desc
!= INVALID_DESCRIPTOR
;
131 /* Return true if the remote connection is over stdio. */
134 remote_connection_is_stdio (void)
136 return remote_is_stdio
;
140 enable_async_notification (int fd
)
142 #if defined(F_SETFL) && defined (FASYNC)
143 int save_fcntl_flags
;
145 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
146 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
147 #if defined (F_SETOWN)
148 fcntl (fd
, F_SETOWN
, getpid ());
154 handle_accept_event (int err
, gdb_client_data client_data
)
156 struct sockaddr_in sockaddr
;
160 debug_printf ("handling possible accept event\n");
162 tmp
= sizeof (sockaddr
);
163 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
164 if (remote_desc
== -1)
165 perror_with_name ("Accept failed");
167 /* Enable TCP keep alive process. */
169 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
170 (char *) &tmp
, sizeof (tmp
));
172 /* Tell TCP not to delay small packets. This greatly speeds up
173 interactive response. */
175 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
176 (char *) &tmp
, sizeof (tmp
));
179 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
180 exits when the remote side dies. */
186 close (listen_desc
); /* No longer need this */
188 closesocket (listen_desc
); /* No longer need this */
192 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
193 descriptor open for add_file_handler to wait for a new connection. */
194 delete_file_handler (listen_desc
);
196 /* Convert IP address to string. */
197 fprintf (stderr
, "Remote debugging from host %s\n",
198 inet_ntoa (sockaddr
.sin_addr
));
200 enable_async_notification (remote_desc
);
202 /* Register the event loop handler. */
203 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
205 /* We have a new GDB connection now. If we were disconnected
206 tracing, there's a window where the target could report a stop
207 event to the event loop, and since we have a connection now, we'd
208 try to send vStopped notifications to GDB. But, don't do that
209 until GDB as selected all-stop/non-stop, and has queried the
210 threads' status ('?'). */
216 /* Prepare for a later connection to a remote debugger.
217 NAME is the filename used for communication. */
220 remote_prepare (char *name
)
224 static int winsock_initialized
;
227 struct sockaddr_in sockaddr
;
232 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
234 /* We need to record fact that we're using stdio sooner than the
235 call to remote_open so start_inferior knows the connection is
238 transport_is_reliable
= 1;
242 port_str
= strchr (name
, ':');
243 if (port_str
== NULL
)
245 transport_is_reliable
= 0;
249 port
= strtoul (port_str
+ 1, &port_end
, 10);
250 if (port_str
[1] == '\0' || *port_end
!= '\0')
251 error ("Bad port argument: %s", name
);
254 if (!winsock_initialized
)
258 WSAStartup (MAKEWORD (1, 0), &wsad
);
259 winsock_initialized
= 1;
263 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
264 if (listen_desc
== -1)
265 perror_with_name ("Can't open socket");
267 /* Allow rapid reuse of this port. */
269 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
272 sockaddr
.sin_family
= PF_INET
;
273 sockaddr
.sin_port
= htons (port
);
274 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
276 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
277 || listen (listen_desc
, 1))
278 perror_with_name ("Can't bind address");
280 transport_is_reliable
= 1;
283 /* Open a connection to a remote debugger.
284 NAME is the filename used for communication. */
287 remote_open (char *name
)
291 port_str
= strchr (name
, ':');
293 if (port_str
== NULL
)
294 error ("Only <host>:<port> is supported on this platform.");
297 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
299 fprintf (stderr
, "Remote debugging using stdio\n");
301 /* Use stdin as the handle of the connection.
302 We only select on reads, for example. */
303 remote_desc
= fileno (stdin
);
305 enable_async_notification (remote_desc
);
307 /* Register the event loop handler. */
308 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
311 else if (port_str
== NULL
)
315 if (stat (name
, &statbuf
) == 0
316 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
317 remote_desc
= open (name
, O_RDWR
);
325 perror_with_name ("Could not open remote device");
329 struct termios termios
;
330 tcgetattr (remote_desc
, &termios
);
335 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
336 termios
.c_cflag
|= CLOCAL
| CS8
;
337 termios
.c_cc
[VMIN
] = 1;
338 termios
.c_cc
[VTIME
] = 0;
340 tcsetattr (remote_desc
, TCSANOW
, &termios
);
346 struct termio termio
;
347 ioctl (remote_desc
, TCGETA
, &termio
);
352 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
353 termio
.c_cflag
|= CLOCAL
| CS8
;
354 termio
.c_cc
[VMIN
] = 1;
355 termio
.c_cc
[VTIME
] = 0;
357 ioctl (remote_desc
, TCSETA
, &termio
);
365 ioctl (remote_desc
, TIOCGETP
, &sg
);
367 ioctl (remote_desc
, TIOCSETP
, &sg
);
371 fprintf (stderr
, "Remote debugging using %s\n", name
);
373 enable_async_notification (remote_desc
);
375 /* Register the event loop handler. */
376 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
378 #endif /* USE_WIN32API */
383 struct sockaddr_in sockaddr
;
385 len
= sizeof (sockaddr
);
386 if (getsockname (listen_desc
,
387 (struct sockaddr
*) &sockaddr
, &len
) < 0
388 || len
< sizeof (sockaddr
))
389 perror_with_name ("Can't determine port");
390 port
= ntohs (sockaddr
.sin_port
);
392 fprintf (stderr
, "Listening on port %d\n", port
);
395 /* Register the event loop handler. */
396 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
403 delete_file_handler (remote_desc
);
406 closesocket (remote_desc
);
408 if (! remote_connection_is_stdio ())
411 remote_desc
= INVALID_DESCRIPTOR
;
418 #ifndef IN_PROCESS_AGENT
421 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
428 for (i
= 0; i
< len
; i
++)
432 addr
= addr
| (fromhex (ch
) & 0x0f);
438 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
443 while (*end
!= '\0' && *end
!= ';')
446 decode_address (addrp
, start
, end
- start
);
455 #ifndef IN_PROCESS_AGENT
457 /* Look for a sequence of characters which can be run-length encoded.
458 If there are any, update *CSUM and *P. Otherwise, output the
459 single character. Return the number of characters consumed. */
462 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
466 /* Always output the character. */
470 /* Don't go past '~'. */
474 for (n
= 1; n
< remaining
; n
++)
475 if (buf
[n
] != buf
[0])
478 /* N is the index of the first character not the same as buf[0].
479 buf[0] is counted twice, so by decrementing N, we get the number
480 of characters the RLE sequence will replace. */
486 /* Skip the frame characters. The manual says to skip '+' and '-'
487 also, but there's no reason to. Unfortunately these two unusable
488 characters double the encoded length of a four byte zero
490 while (n
+ 29 == '$' || n
+ 29 == '#')
503 #ifndef IN_PROCESS_AGENT
505 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
508 write_ptid (char *buf
, ptid_t ptid
)
514 pid
= ptid_get_pid (ptid
);
516 buf
+= sprintf (buf
, "p-%x.", -pid
);
518 buf
+= sprintf (buf
, "p%x.", pid
);
520 tid
= ptid_get_lwp (ptid
);
522 buf
+= sprintf (buf
, "-%x", -tid
);
524 buf
+= sprintf (buf
, "%x", tid
);
530 hex_or_minus_one (char *buf
, char **obuf
)
534 if (startswith (buf
, "-1"))
540 buf
= unpack_varlen_hex (buf
, &ret
);
548 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
549 passed the last parsed char. Returns null_ptid on error. */
551 read_ptid (char *buf
, char **obuf
)
555 ULONGEST pid
= 0, tid
= 0;
559 /* Multi-process ptid. */
560 pp
= unpack_varlen_hex (p
+ 1, &pid
);
562 error ("invalid remote ptid: %s\n", p
);
566 tid
= hex_or_minus_one (p
, &pp
);
570 return ptid_build (pid
, tid
, 0);
573 /* No multi-process. Just a tid. */
574 tid
= hex_or_minus_one (p
, &pp
);
576 /* Since GDB is not sending a process id (multi-process extensions
577 are off), then there's only one process. Default to the first in
579 pid
= pid_of (get_first_process ());
583 return ptid_build (pid
, tid
, 0);
586 /* Write COUNT bytes in BUF to the client.
587 The result is the number of bytes written or -1 if error.
588 This may return less than COUNT. */
591 write_prim (const void *buf
, int count
)
593 if (remote_connection_is_stdio ())
594 return write (fileno (stdout
), buf
, count
);
596 return write (remote_desc
, buf
, count
);
599 /* Read COUNT bytes from the client and store in BUF.
600 The result is the number of bytes read or -1 if error.
601 This may return less than COUNT. */
604 read_prim (void *buf
, int count
)
606 if (remote_connection_is_stdio ())
607 return read (fileno (stdin
), buf
, count
);
609 return read (remote_desc
, buf
, count
);
612 /* Send a packet to the remote machine, with error checking.
613 The data of the packet is in BUF, and the length of the
614 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
617 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
620 unsigned char csum
= 0;
625 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
627 /* Copy the packet into buffer BUF2, encapsulating it
628 and giving it a checksum. */
636 for (i
= 0; i
< cnt
;)
637 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
640 *p
++ = tohex ((csum
>> 4) & 0xf);
641 *p
++ = tohex (csum
& 0xf);
645 /* Send it over and over until we get a positive ack. */
649 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
651 perror ("putpkt(write)");
656 if (noack_mode
|| is_notif
)
658 /* Don't expect an ack then. */
662 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
664 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
672 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
686 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
690 /* Check for an input interrupt while we're here. */
691 if (cc
== '\003' && current_thread
!= NULL
)
692 (*the_target
->request_interrupt
) ();
697 return 1; /* Success! */
701 putpkt_binary (char *buf
, int cnt
)
703 return putpkt_binary_1 (buf
, cnt
, 0);
706 /* Send a packet to the remote machine, with error checking. The data
707 of the packet is in BUF, and the packet should be a NUL-terminated
708 string. Returns >= 0 on success, -1 otherwise. */
713 return putpkt_binary (buf
, strlen (buf
));
717 putpkt_notif (char *buf
)
719 return putpkt_binary_1 (buf
, strlen (buf
), 1);
722 /* Come here when we get an input interrupt from the remote side. This
723 interrupt should only be active while we are waiting for the child to do
724 something. Thus this assumes readchar:bufcnt is 0.
725 About the only thing that should come through is a ^C, which
726 will cause us to request child interruption. */
729 input_interrupt (int unused
)
732 struct timeval immediate
= { 0, 0 };
734 /* Protect against spurious interrupts. This has been observed to
735 be a problem under NetBSD 1.4 and 1.5. */
738 FD_SET (remote_desc
, &readset
);
739 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
744 cc
= read_prim (&c
, 1);
748 fprintf (stderr
, "client connection closed\n");
751 else if (cc
!= 1 || c
!= '\003')
753 fprintf (stderr
, "input_interrupt, count = %d c = %d ", cc
, c
);
755 fprintf (stderr
, "('%c')\n", c
);
757 fprintf (stderr
, "('\\x%02x')\n", c
& 0xff);
761 (*the_target
->request_interrupt
) ();
765 /* Check if the remote side sent us an interrupt request (^C). */
767 check_remote_input_interrupt_request (void)
769 /* This function may be called before establishing communications,
770 therefore we need to validate the remote descriptor. */
772 if (remote_desc
== INVALID_DESCRIPTOR
)
778 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
779 accept Control-C from the client, and must be disabled when talking to
783 unblock_async_io (void)
788 sigemptyset (&sigio_set
);
789 sigaddset (&sigio_set
, SIGIO
);
790 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
796 nto_comctrl (int enable
)
798 struct sigevent event
;
802 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
803 event
.sigev_signo
= SIGIO
;
804 event
.sigev_code
= 0;
805 event
.sigev_value
.sival_ptr
= NULL
;
806 event
.sigev_priority
= -1;
807 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
811 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
816 /* Current state of asynchronous I/O. */
817 static int async_io_enabled
;
819 /* Enable asynchronous I/O. */
821 enable_async_io (void)
823 if (async_io_enabled
)
827 signal (SIGIO
, input_interrupt
);
829 async_io_enabled
= 1;
835 /* Disable asynchronous I/O. */
837 disable_async_io (void)
839 if (!async_io_enabled
)
843 signal (SIGIO
, SIG_IGN
);
845 async_io_enabled
= 0;
853 initialize_async_io (void)
855 /* Make sure that async I/O starts disabled. */
856 async_io_enabled
= 1;
859 /* Make sure the signal is unblocked. */
863 /* Internal buffer used by readchar.
864 These are global to readchar because reschedule_remote needs to be
865 able to tell whether the buffer is empty. */
867 static unsigned char readchar_buf
[BUFSIZ
];
868 static int readchar_bufcnt
= 0;
869 static unsigned char *readchar_bufp
;
871 /* Returns next char from remote GDB. -1 if error. */
878 if (readchar_bufcnt
== 0)
880 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
882 if (readchar_bufcnt
<= 0)
884 if (readchar_bufcnt
== 0)
885 fprintf (stderr
, "readchar: Got EOF\n");
892 readchar_bufp
= readchar_buf
;
896 ch
= *readchar_bufp
++;
901 /* Reset the readchar state machine. */
904 reset_readchar (void)
907 if (readchar_callback
!= NOT_SCHEDULED
)
909 delete_callback_event (readchar_callback
);
910 readchar_callback
= NOT_SCHEDULED
;
914 /* Process remaining data in readchar_buf. */
917 process_remaining (void *context
)
921 /* This is a one-shot event. */
922 readchar_callback
= NOT_SCHEDULED
;
924 if (readchar_bufcnt
> 0)
925 res
= handle_serial_event (0, NULL
);
932 /* If there is still data in the buffer, queue another event to process it,
933 we can't sleep in select yet. */
938 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
939 readchar_callback
= append_callback_event (process_remaining
, NULL
);
942 /* Read a packet from the remote machine, with error checking,
943 and store it in BUF. Returns length of packet, or negative if error. */
949 unsigned char csum
, c1
, c2
;
963 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
984 c1
= fromhex (readchar ());
985 c2
= fromhex (readchar ());
987 if (csum
== (c1
<< 4) + c2
)
993 "Bad checksum, sentsum=0x%x, csum=0x%x, "
994 "buf=%s [no-ack-mode, Bad medium?]\n",
995 (c1
<< 4) + c2
, csum
, buf
);
996 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1000 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1001 (c1
<< 4) + c2
, csum
, buf
);
1002 if (write_prim ("-", 1) != 1)
1010 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1014 if (write_prim ("+", 1) != 1)
1019 fprintf (stderr
, "[sent ack]\n");
1027 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1036 write_ok (char *buf
)
1044 write_enn (char *buf
)
1046 /* Some day, we should define the meanings of the error codes... */
1055 #ifndef IN_PROCESS_AGENT
1058 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1060 if ((regno
>> 12) != 0)
1061 *buf
++ = tohex ((regno
>> 12) & 0xf);
1062 if ((regno
>> 8) != 0)
1063 *buf
++ = tohex ((regno
>> 8) & 0xf);
1064 *buf
++ = tohex ((regno
>> 4) & 0xf);
1065 *buf
++ = tohex (regno
& 0xf);
1067 collect_register_as_string (regcache
, regno
, buf
);
1068 buf
+= 2 * register_size (regcache
->tdesc
, regno
);
1075 new_thread_notify (int id
)
1079 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1083 if (server_waiting
== 0)
1086 sprintf (own_buf
, "n%x", id
);
1087 disable_async_io ();
1093 dead_thread_notify (int id
)
1097 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1101 sprintf (own_buf
, "x%x", id
);
1102 disable_async_io ();
1108 prepare_resume_reply (char *buf
, ptid_t ptid
,
1109 struct target_waitstatus
*status
)
1112 debug_printf ("Writing resume reply for %s:%d\n",
1113 target_pid_to_str (ptid
), status
->kind
);
1115 switch (status
->kind
)
1117 case TARGET_WAITKIND_STOPPED
:
1118 case TARGET_WAITKIND_FORKED
:
1119 case TARGET_WAITKIND_VFORKED
:
1120 case TARGET_WAITKIND_EXECD
:
1122 struct thread_info
*saved_thread
;
1124 struct regcache
*regcache
;
1126 if ((status
->kind
== TARGET_WAITKIND_FORKED
&& report_fork_events
)
1127 || (status
->kind
== TARGET_WAITKIND_VFORKED
&& report_vfork_events
))
1129 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1130 const char *event
= (status
->kind
== TARGET_WAITKIND_FORKED
1131 ? "fork" : "vfork");
1133 sprintf (buf
, "T%02x%s:", signal
, event
);
1134 buf
+= strlen (buf
);
1135 buf
= write_ptid (buf
, status
->value
.related_pid
);
1138 else if (status
->kind
== TARGET_WAITKIND_EXECD
&& multi_process
)
1140 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1141 const char *event
= "exec";
1142 char hexified_pathname
[PATH_MAX
* 2];
1144 sprintf (buf
, "T%02x%s:", signal
, event
);
1145 buf
+= strlen (buf
);
1147 /* Encode pathname to hexified format. */
1148 bin2hex ((const gdb_byte
*) status
->value
.execd_pathname
,
1150 strlen (status
->value
.execd_pathname
));
1152 sprintf (buf
, "%s;", hexified_pathname
);
1153 xfree (status
->value
.execd_pathname
);
1154 status
->value
.execd_pathname
= NULL
;
1155 buf
+= strlen (buf
);
1158 sprintf (buf
, "T%02x", status
->value
.sig
);
1160 buf
+= strlen (buf
);
1162 saved_thread
= current_thread
;
1164 current_thread
= find_thread_ptid (ptid
);
1166 regp
= current_target_desc ()->expedite_regs
;
1168 regcache
= get_thread_regcache (current_thread
, 1);
1170 if (the_target
->stopped_by_watchpoint
!= NULL
1171 && (*the_target
->stopped_by_watchpoint
) ())
1176 strncpy (buf
, "watch:", 6);
1179 addr
= (*the_target
->stopped_data_address
) ();
1181 /* Convert each byte of the address into two hexadecimal
1182 chars. Note that we take sizeof (void *) instead of
1183 sizeof (addr); this is to avoid sending a 64-bit
1184 address to a 32-bit GDB. */
1185 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1186 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1189 else if (swbreak_feature
&& target_stopped_by_sw_breakpoint ())
1191 sprintf (buf
, "swbreak:;");
1192 buf
+= strlen (buf
);
1194 else if (hwbreak_feature
&& target_stopped_by_hw_breakpoint ())
1196 sprintf (buf
, "hwbreak:;");
1197 buf
+= strlen (buf
);
1202 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1207 /* Formerly, if the debugger had not used any thread features
1208 we would not burden it with a thread status response. This
1209 was for the benefit of GDB 4.13 and older. However, in
1210 recent GDB versions the check (``if (cont_thread != 0)'')
1211 does not have the desired effect because of sillyness in
1212 the way that the remote protocol handles specifying a
1213 thread. Since thread support relies on qSymbol support
1214 anyway, assume GDB can handle threads. */
1216 if (using_threads
&& !disable_packet_Tthread
)
1218 /* This if (1) ought to be unnecessary. But remote_wait
1219 in GDB will claim this event belongs to inferior_ptid
1220 if we do not specify a thread, and there's no way for
1221 gdbserver to know what inferior_ptid is. */
1222 if (1 || !ptid_equal (general_thread
, ptid
))
1225 /* In non-stop, don't change the general thread behind
1228 general_thread
= ptid
;
1229 sprintf (buf
, "thread:");
1230 buf
+= strlen (buf
);
1231 buf
= write_ptid (buf
, ptid
);
1233 buf
+= strlen (buf
);
1235 core
= target_core_of_thread (ptid
);
1239 sprintf (buf
, "core:");
1240 buf
+= strlen (buf
);
1241 sprintf (buf
, "%x", core
);
1243 buf
+= strlen (buf
);
1250 strcpy (buf
, "library:;");
1251 buf
+= strlen (buf
);
1255 current_thread
= saved_thread
;
1258 case TARGET_WAITKIND_EXITED
:
1260 sprintf (buf
, "W%x;process:%x",
1261 status
->value
.integer
, ptid_get_pid (ptid
));
1263 sprintf (buf
, "W%02x", status
->value
.integer
);
1265 case TARGET_WAITKIND_SIGNALLED
:
1267 sprintf (buf
, "X%x;process:%x",
1268 status
->value
.sig
, ptid_get_pid (ptid
));
1270 sprintf (buf
, "X%02x", status
->value
.sig
);
1272 case TARGET_WAITKIND_VFORK_DONE
:
1273 if (report_vfork_events
)
1275 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1277 sprintf (buf
, "T%02xvforkdone:;", signal
);
1280 sprintf (buf
, "T%02x", GDB_SIGNAL_0
);
1283 error ("unhandled waitkind");
1289 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1293 *mem_addr_ptr
= *len_ptr
= 0;
1295 while ((ch
= from
[i
++]) != ',')
1297 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1298 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1301 for (j
= 0; j
< 4; j
++)
1303 if ((ch
= from
[i
++]) == 0)
1305 *len_ptr
= *len_ptr
<< 4;
1306 *len_ptr
|= fromhex (ch
) & 0x0f;
1311 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1312 unsigned char **to_p
)
1316 *mem_addr_ptr
= *len_ptr
= 0;
1318 while ((ch
= from
[i
++]) != ',')
1320 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1321 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1324 while ((ch
= from
[i
++]) != ':')
1326 *len_ptr
= *len_ptr
<< 4;
1327 *len_ptr
|= fromhex (ch
) & 0x0f;
1331 *to_p
= xmalloc (*len_ptr
);
1333 hex2bin (&from
[i
++], *to_p
, *len_ptr
);
1337 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1338 unsigned int *len_ptr
, unsigned char **to_p
)
1342 *mem_addr_ptr
= *len_ptr
= 0;
1344 while ((ch
= from
[i
++]) != ',')
1346 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1347 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1350 while ((ch
= from
[i
++]) != ':')
1352 *len_ptr
= *len_ptr
<< 4;
1353 *len_ptr
|= fromhex (ch
) & 0x0f;
1357 *to_p
= xmalloc (*len_ptr
);
1359 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1360 *to_p
, *len_ptr
) != *len_ptr
)
1366 /* Decode a qXfer write request. */
1369 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1370 unsigned int *len
, unsigned char *data
)
1375 /* Extract the offset. */
1377 while ((ch
= *buf
++) != ':')
1379 *offset
= *offset
<< 4;
1380 *offset
|= fromhex (ch
) & 0x0f;
1383 /* Get encoded data. */
1384 packet_len
-= buf
- b
;
1385 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1390 /* Decode the parameters of a qSearch:memory packet. */
1393 decode_search_memory_packet (const char *buf
, int packet_len
,
1394 CORE_ADDR
*start_addrp
,
1395 CORE_ADDR
*search_space_lenp
,
1396 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1398 const char *p
= buf
;
1400 p
= decode_address_to_semicolon (start_addrp
, p
);
1401 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1402 packet_len
-= p
- buf
;
1403 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1404 pattern
, packet_len
);
1409 free_sym_cache (struct sym_cache
*sym
)
1419 clear_symbol_cache (struct sym_cache
**symcache_p
)
1421 struct sym_cache
*sym
, *next
;
1423 /* Check the cache first. */
1424 for (sym
= *symcache_p
; sym
; sym
= next
)
1427 free_sym_cache (sym
);
1433 /* Get the address of NAME, and return it in ADDRP if found. if
1434 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1435 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1438 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1440 char own_buf
[266], *p
, *q
;
1442 struct sym_cache
*sym
;
1443 struct process_info
*proc
;
1445 proc
= current_process ();
1447 /* Check the cache first. */
1448 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1449 if (strcmp (name
, sym
->name
) == 0)
1455 /* It might not be an appropriate time to look up a symbol,
1456 e.g. while we're trying to fetch registers. */
1460 /* Send the request. */
1461 strcpy (own_buf
, "qSymbol:");
1462 bin2hex ((const gdb_byte
*) name
, own_buf
+ strlen ("qSymbol:"),
1464 if (putpkt (own_buf
) < 0)
1467 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1468 len
= getpkt (own_buf
);
1472 /* We ought to handle pretty much any packet at this point while we
1473 wait for the qSymbol "response". That requires re-entering the
1474 main loop. For now, this is an adequate approximation; allow
1475 GDB to read from memory while it figures out the address of the
1477 while (own_buf
[0] == 'm')
1480 unsigned char *mem_buf
;
1481 unsigned int mem_len
;
1483 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1484 mem_buf
= xmalloc (mem_len
);
1485 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1486 bin2hex (mem_buf
, own_buf
, mem_len
);
1488 write_enn (own_buf
);
1490 if (putpkt (own_buf
) < 0)
1492 len
= getpkt (own_buf
);
1497 if (!startswith (own_buf
, "qSymbol:"))
1499 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1503 p
= own_buf
+ strlen ("qSymbol:");
1505 while (*q
&& *q
!= ':')
1508 /* Make sure we found a value for the symbol. */
1509 if (p
== q
|| *q
== '\0')
1512 decode_address (addrp
, p
, q
- p
);
1514 /* Save the symbol in our cache. */
1515 sym
= XNEW (struct sym_cache
);
1516 sym
->name
= xstrdup (name
);
1518 sym
->next
= proc
->symbol_cache
;
1519 proc
->symbol_cache
= sym
;
1524 /* Relocate an instruction to execute at a different address. OLDLOC
1525 is the address in the inferior memory where the instruction to
1526 relocate is currently at. On input, TO points to the destination
1527 where we want the instruction to be copied (and possibly adjusted)
1528 to. On output, it points to one past the end of the resulting
1529 instruction(s). The effect of executing the instruction at TO
1530 shall be the same as if executing it at OLDLOC. For example, call
1531 instructions that implicitly push the return address on the stack
1532 should be adjusted to return to the instruction after OLDLOC;
1533 relative branches, and other PC-relative instructions need the
1534 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1537 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1541 ULONGEST written
= 0;
1543 /* Send the request. */
1544 strcpy (own_buf
, "qRelocInsn:");
1545 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1547 if (putpkt (own_buf
) < 0)
1550 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1551 len
= getpkt (own_buf
);
1555 /* We ought to handle pretty much any packet at this point while we
1556 wait for the qRelocInsn "response". That requires re-entering
1557 the main loop. For now, this is an adequate approximation; allow
1558 GDB to access memory. */
1559 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1562 unsigned char *mem_buf
= NULL
;
1563 unsigned int mem_len
;
1565 if (own_buf
[0] == 'm')
1567 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1568 mem_buf
= xmalloc (mem_len
);
1569 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1570 bin2hex (mem_buf
, own_buf
, mem_len
);
1572 write_enn (own_buf
);
1574 else if (own_buf
[0] == 'X')
1576 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1577 &mem_len
, &mem_buf
) < 0
1578 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1579 write_enn (own_buf
);
1585 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1586 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1589 write_enn (own_buf
);
1592 if (putpkt (own_buf
) < 0)
1594 len
= getpkt (own_buf
);
1599 if (own_buf
[0] == 'E')
1601 warning ("An error occurred while relocating an instruction: %s\n",
1606 if (!startswith (own_buf
, "qRelocInsn:"))
1608 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1613 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1620 monitor_output (const char *msg
)
1622 int len
= strlen (msg
);
1623 char *buf
= xmalloc (len
* 2 + 2);
1626 bin2hex ((const gdb_byte
*) msg
, buf
+ 1, len
);