1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2019 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/>. */
24 #include "gdbthread.h"
27 #include "common/rsp-low.h"
28 #include "common/netstuff.h"
29 #include "common/filestuff.h"
32 #include <sys/ioctl.h>
38 #include <netinet/in.h>
41 #include <sys/socket.h>
46 #if HAVE_NETINET_TCP_H
47 #include <netinet/tcp.h>
50 #include <sys/ioctl.h>
58 #include "common/gdb_sys_time.h"
61 #include <arpa/inet.h>
66 #if _WIN32_WINNT < 0x0501
68 # define _WIN32_WINNT 0x0501
74 #include <sys/iomgr.h>
77 #ifndef HAVE_SOCKLEN_T
78 typedef int socklen_t
;
81 #ifndef IN_PROCESS_AGENT
84 # define INVALID_DESCRIPTOR INVALID_SOCKET
86 # define INVALID_DESCRIPTOR -1
89 /* Extra value for readchar_callback. */
91 /* The callback is currently not scheduled. */
95 /* Status of the readchar callback.
96 Either NOT_SCHEDULED or the callback id. */
97 static int readchar_callback
= NOT_SCHEDULED
;
99 static int readchar (void);
100 static void reset_readchar (void);
101 static void reschedule (void);
103 /* A cache entry for a successfully looked-up symbol. */
108 struct sym_cache
*next
;
111 static int remote_is_stdio
= 0;
113 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
114 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
116 /* FIXME headerize? */
117 extern int using_threads
;
118 extern int debug_threads
;
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_storage sockaddr
;
157 socklen_t len
= sizeof (sockaddr
);
160 debug_printf ("handling possible accept event\n");
162 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
);
163 if (remote_desc
== -1)
164 perror_with_name ("Accept failed");
166 /* Enable TCP keep alive process. */
168 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
169 (char *) &tmp
, sizeof (tmp
));
171 /* Tell TCP not to delay small packets. This greatly speeds up
172 interactive response. */
174 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
175 (char *) &tmp
, sizeof (tmp
));
178 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
179 exits when the remote side dies. */
185 close (listen_desc
); /* No longer need this */
187 closesocket (listen_desc
); /* No longer need this */
191 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
192 descriptor open for add_file_handler to wait for a new connection. */
193 delete_file_handler (listen_desc
);
195 /* Convert IP address to string. */
196 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
198 int r
= getnameinfo ((struct sockaddr
*) &sockaddr
, len
,
199 orig_host
, sizeof (orig_host
),
200 orig_port
, sizeof (orig_port
),
201 NI_NUMERICHOST
| NI_NUMERICSERV
);
204 fprintf (stderr
, _("Could not obtain remote address: %s\n"),
207 fprintf (stderr
, _("Remote debugging from host %s, port %s\n"),
208 orig_host
, orig_port
);
210 enable_async_notification (remote_desc
);
212 /* Register the event loop handler. */
213 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
215 /* We have a new GDB connection now. If we were disconnected
216 tracing, there's a window where the target could report a stop
217 event to the event loop, and since we have a connection now, we'd
218 try to send vStopped notifications to GDB. But, don't do that
219 until GDB as selected all-stop/non-stop, and has queried the
220 threads' status ('?'). */
226 /* Prepare for a later connection to a remote debugger.
227 NAME is the filename used for communication. */
230 remote_prepare (const char *name
)
232 client_state
&cs
= get_client_state ();
234 static int winsock_initialized
;
239 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
241 /* We need to record fact that we're using stdio sooner than the
242 call to remote_open so start_inferior knows the connection is
245 cs
.transport_is_reliable
= 1;
249 struct addrinfo hint
;
250 struct addrinfo
*ainfo
;
252 memset (&hint
, 0, sizeof (hint
));
253 /* Assume no prefix will be passed, therefore we should use
255 hint
.ai_family
= AF_UNSPEC
;
256 hint
.ai_socktype
= SOCK_STREAM
;
257 hint
.ai_protocol
= IPPROTO_TCP
;
259 parsed_connection_spec parsed
260 = parse_connection_spec_without_prefix (name
, &hint
);
262 if (parsed
.port_str
.empty ())
264 cs
.transport_is_reliable
= 0;
269 if (!winsock_initialized
)
273 WSAStartup (MAKEWORD (1, 0), &wsad
);
274 winsock_initialized
= 1;
278 int r
= getaddrinfo (parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
282 error (_("%s: cannot resolve name: %s"), name
, gai_strerror (r
));
284 scoped_free_addrinfo
freeaddrinfo (ainfo
);
286 struct addrinfo
*iter
;
288 for (iter
= ainfo
; iter
!= NULL
; iter
= iter
->ai_next
)
290 listen_desc
= gdb_socket_cloexec (iter
->ai_family
, iter
->ai_socktype
,
293 if (listen_desc
>= 0)
298 perror_with_name ("Can't open socket");
300 /* Allow rapid reuse of this port. */
302 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
305 switch (iter
->ai_family
)
308 ((struct sockaddr_in
*) iter
->ai_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
311 ((struct sockaddr_in6
*) iter
->ai_addr
)->sin6_addr
= in6addr_any
;
314 internal_error (__FILE__
, __LINE__
,
315 _("Invalid 'ai_family' %d\n"), iter
->ai_family
);
318 if (bind (listen_desc
, iter
->ai_addr
, iter
->ai_addrlen
) != 0)
319 perror_with_name ("Can't bind address");
321 if (listen (listen_desc
, 1) != 0)
322 perror_with_name ("Can't listen on socket");
324 cs
.transport_is_reliable
= 1;
327 /* Open a connection to a remote debugger.
328 NAME is the filename used for communication. */
331 remote_open (const char *name
)
333 const char *port_str
;
335 port_str
= strchr (name
, ':');
337 if (port_str
== NULL
)
338 error ("Only HOST:PORT is supported on this platform.");
341 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
343 fprintf (stderr
, "Remote debugging using stdio\n");
345 /* Use stdin as the handle of the connection.
346 We only select on reads, for example. */
347 remote_desc
= fileno (stdin
);
349 enable_async_notification (remote_desc
);
351 /* Register the event loop handler. */
352 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
355 else if (port_str
== NULL
)
359 if (stat (name
, &statbuf
) == 0
360 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
361 remote_desc
= open (name
, O_RDWR
);
369 perror_with_name ("Could not open remote device");
373 struct termios termios
;
374 tcgetattr (remote_desc
, &termios
);
379 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
380 termios
.c_cflag
|= CLOCAL
| CS8
;
381 termios
.c_cc
[VMIN
] = 1;
382 termios
.c_cc
[VTIME
] = 0;
384 tcsetattr (remote_desc
, TCSANOW
, &termios
);
388 fprintf (stderr
, "Remote debugging using %s\n", name
);
390 enable_async_notification (remote_desc
);
392 /* Register the event loop handler. */
393 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
395 #endif /* USE_WIN32API */
398 char listen_port
[GDB_NI_MAX_PORT
];
399 struct sockaddr_storage sockaddr
;
400 socklen_t len
= sizeof (sockaddr
);
402 if (getsockname (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0)
403 perror_with_name ("Can't determine port");
405 int r
= getnameinfo ((struct sockaddr
*) &sockaddr
, len
,
407 listen_port
, sizeof (listen_port
),
411 fprintf (stderr
, _("Can't obtain port where we are listening: %s"),
414 fprintf (stderr
, _("Listening on port %s\n"), listen_port
);
418 /* Register the event loop handler. */
419 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
426 delete_file_handler (remote_desc
);
431 closesocket (remote_desc
);
433 if (! remote_connection_is_stdio ())
436 remote_desc
= INVALID_DESCRIPTOR
;
443 #ifndef IN_PROCESS_AGENT
446 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
453 for (i
= 0; i
< len
; i
++)
457 addr
= addr
| (fromhex (ch
) & 0x0f);
463 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
468 while (*end
!= '\0' && *end
!= ';')
471 decode_address (addrp
, start
, end
- start
);
480 #ifndef IN_PROCESS_AGENT
482 /* Look for a sequence of characters which can be run-length encoded.
483 If there are any, update *CSUM and *P. Otherwise, output the
484 single character. Return the number of characters consumed. */
487 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
491 /* Always output the character. */
495 /* Don't go past '~'. */
499 for (n
= 1; n
< remaining
; n
++)
500 if (buf
[n
] != buf
[0])
503 /* N is the index of the first character not the same as buf[0].
504 buf[0] is counted twice, so by decrementing N, we get the number
505 of characters the RLE sequence will replace. */
511 /* Skip the frame characters. The manual says to skip '+' and '-'
512 also, but there's no reason to. Unfortunately these two unusable
513 characters double the encoded length of a four byte zero
515 while (n
+ 29 == '$' || n
+ 29 == '#')
528 #ifndef IN_PROCESS_AGENT
530 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
533 write_ptid (char *buf
, ptid_t ptid
)
535 client_state
&cs
= get_client_state ();
538 if (cs
.multi_process
)
542 buf
+= sprintf (buf
, "p-%x.", -pid
);
544 buf
+= sprintf (buf
, "p%x.", pid
);
548 buf
+= sprintf (buf
, "-%x", -tid
);
550 buf
+= sprintf (buf
, "%x", tid
);
556 hex_or_minus_one (const char *buf
, const char **obuf
)
560 if (startswith (buf
, "-1"))
566 buf
= unpack_varlen_hex (buf
, &ret
);
574 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
575 passed the last parsed char. Returns null_ptid on error. */
577 read_ptid (const char *buf
, const char **obuf
)
581 ULONGEST pid
= 0, tid
= 0;
585 /* Multi-process ptid. */
586 pp
= unpack_varlen_hex (p
+ 1, &pid
);
588 error ("invalid remote ptid: %s\n", p
);
592 tid
= hex_or_minus_one (p
, &pp
);
596 return ptid_t (pid
, tid
, 0);
599 /* No multi-process. Just a tid. */
600 tid
= hex_or_minus_one (p
, &pp
);
602 /* Since GDB is not sending a process id (multi-process extensions
603 are off), then there's only one process. Default to the first in
605 pid
= pid_of (get_first_process ());
609 return ptid_t (pid
, tid
, 0);
612 /* Write COUNT bytes in BUF to the client.
613 The result is the number of bytes written or -1 if error.
614 This may return less than COUNT. */
617 write_prim (const void *buf
, int count
)
619 if (remote_connection_is_stdio ())
620 return write (fileno (stdout
), buf
, count
);
622 return write (remote_desc
, buf
, count
);
625 /* Read COUNT bytes from the client and store in BUF.
626 The result is the number of bytes read or -1 if error.
627 This may return less than COUNT. */
630 read_prim (void *buf
, int count
)
632 if (remote_connection_is_stdio ())
633 return read (fileno (stdin
), buf
, count
);
635 return read (remote_desc
, buf
, count
);
638 /* Send a packet to the remote machine, with error checking.
639 The data of the packet is in BUF, and the length of the
640 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
643 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
645 client_state
&cs
= get_client_state ();
647 unsigned char csum
= 0;
652 buf2
= (char *) xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
654 /* Copy the packet into buffer BUF2, encapsulating it
655 and giving it a checksum. */
663 for (i
= 0; i
< cnt
;)
664 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
667 *p
++ = tohex ((csum
>> 4) & 0xf);
668 *p
++ = tohex (csum
& 0xf);
672 /* Send it over and over until we get a positive ack. */
676 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
678 perror ("putpkt(write)");
683 if (cs
.noack_mode
|| is_notif
)
685 /* Don't expect an ack then. */
689 debug_printf ("putpkt (\"%s\"); [notif]\n", buf2
);
691 debug_printf ("putpkt (\"%s\"); [noack mode]\n", buf2
);
699 debug_printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
713 debug_printf ("[received '%c' (0x%x)]\n", cc
, cc
);
717 /* Check for an input interrupt while we're here. */
718 if (cc
== '\003' && current_thread
!= NULL
)
719 (*the_target
->request_interrupt
) ();
724 return 1; /* Success! */
728 putpkt_binary (char *buf
, int cnt
)
730 return putpkt_binary_1 (buf
, cnt
, 0);
733 /* Send a packet to the remote machine, with error checking. The data
734 of the packet is in BUF, and the packet should be a NUL-terminated
735 string. Returns >= 0 on success, -1 otherwise. */
740 return putpkt_binary (buf
, strlen (buf
));
744 putpkt_notif (char *buf
)
746 return putpkt_binary_1 (buf
, strlen (buf
), 1);
749 /* Come here when we get an input interrupt from the remote side. This
750 interrupt should only be active while we are waiting for the child to do
751 something. Thus this assumes readchar:bufcnt is 0.
752 About the only thing that should come through is a ^C, which
753 will cause us to request child interruption. */
756 input_interrupt (int unused
)
759 struct timeval immediate
= { 0, 0 };
761 /* Protect against spurious interrupts. This has been observed to
762 be a problem under NetBSD 1.4 and 1.5. */
765 FD_SET (remote_desc
, &readset
);
766 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
771 cc
= read_prim (&c
, 1);
775 fprintf (stderr
, "client connection closed\n");
778 else if (cc
!= 1 || c
!= '\003')
780 fprintf (stderr
, "input_interrupt, count = %d c = %d ", cc
, c
);
782 fprintf (stderr
, "('%c')\n", c
);
784 fprintf (stderr
, "('\\x%02x')\n", c
& 0xff);
788 (*the_target
->request_interrupt
) ();
792 /* Check if the remote side sent us an interrupt request (^C). */
794 check_remote_input_interrupt_request (void)
796 /* This function may be called before establishing communications,
797 therefore we need to validate the remote descriptor. */
799 if (remote_desc
== INVALID_DESCRIPTOR
)
805 /* Asynchronous I/O support. SIGIO must be unblocked when waiting,
806 in order to accept Control-C from the client, and must be blocked
807 when talking to the client. */
810 block_unblock_async_io (int block
)
815 sigemptyset (&sigio_set
);
816 sigaddset (&sigio_set
, SIGIO
);
817 sigprocmask (block
? SIG_BLOCK
: SIG_UNBLOCK
, &sigio_set
, NULL
);
823 nto_comctrl (int enable
)
825 struct sigevent event
;
829 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
830 event
.sigev_signo
= SIGIO
;
831 event
.sigev_code
= 0;
832 event
.sigev_value
.sival_ptr
= NULL
;
833 event
.sigev_priority
= -1;
834 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
838 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
843 /* Current state of asynchronous I/O. */
844 static int async_io_enabled
;
846 /* Enable asynchronous I/O. */
848 enable_async_io (void)
850 if (async_io_enabled
)
853 block_unblock_async_io (0);
855 async_io_enabled
= 1;
861 /* Disable asynchronous I/O. */
863 disable_async_io (void)
865 if (!async_io_enabled
)
868 block_unblock_async_io (1);
870 async_io_enabled
= 0;
878 initialize_async_io (void)
880 /* Make sure that async I/O starts blocked. */
881 async_io_enabled
= 1;
884 /* Install the signal handler. */
886 signal (SIGIO
, input_interrupt
);
890 /* Internal buffer used by readchar.
891 These are global to readchar because reschedule_remote needs to be
892 able to tell whether the buffer is empty. */
894 static unsigned char readchar_buf
[BUFSIZ
];
895 static int readchar_bufcnt
= 0;
896 static unsigned char *readchar_bufp
;
898 /* Returns next char from remote GDB. -1 if error. */
905 if (readchar_bufcnt
== 0)
907 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
909 if (readchar_bufcnt
<= 0)
911 if (readchar_bufcnt
== 0)
914 debug_printf ("readchar: Got EOF\n");
922 readchar_bufp
= readchar_buf
;
926 ch
= *readchar_bufp
++;
931 /* Reset the readchar state machine. */
934 reset_readchar (void)
937 if (readchar_callback
!= NOT_SCHEDULED
)
939 delete_callback_event (readchar_callback
);
940 readchar_callback
= NOT_SCHEDULED
;
944 /* Process remaining data in readchar_buf. */
947 process_remaining (void *context
)
951 /* This is a one-shot event. */
952 readchar_callback
= NOT_SCHEDULED
;
954 if (readchar_bufcnt
> 0)
955 res
= handle_serial_event (0, NULL
);
962 /* If there is still data in the buffer, queue another event to process it,
963 we can't sleep in select yet. */
968 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
969 readchar_callback
= append_callback_event (process_remaining
, NULL
);
972 /* Read a packet from the remote machine, with error checking,
973 and store it in BUF. Returns length of packet, or negative if error. */
978 client_state
&cs
= get_client_state ();
980 unsigned char csum
, c1
, c2
;
991 /* The '\003' may appear before or after each packet, so
992 check for an input interrupt. */
995 (*the_target
->request_interrupt
) ();
1003 debug_printf ("[getpkt: discarding char '%c']\n", c
);
1024 c1
= fromhex (readchar ());
1025 c2
= fromhex (readchar ());
1027 if (csum
== (c1
<< 4) + c2
)
1033 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1034 "buf=%s [no-ack-mode, Bad medium?]\n",
1035 (c1
<< 4) + c2
, csum
, buf
);
1036 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1040 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1041 (c1
<< 4) + c2
, csum
, buf
);
1042 if (write_prim ("-", 1) != 1)
1050 debug_printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
1054 if (write_prim ("+", 1) != 1)
1059 debug_printf ("[sent ack]\n");
1067 debug_printf ("getpkt (\"%s\"); [no ack sent] \n", buf
);
1072 /* The readchar above may have already read a '\003' out of the socket
1073 and moved it to the local buffer. For example, when GDB sends
1074 vCont;c immediately followed by interrupt (see
1075 gdb.base/interrupt-noterm.exp). As soon as we see the vCont;c, we'll
1076 resume the inferior and wait. Since we've already moved the '\003'
1077 to the local buffer, SIGIO won't help. In that case, if we don't
1078 check for interrupt after the vCont;c packet, the interrupt character
1079 would stay in the buffer unattended until after the next (unrelated)
1081 while (readchar_bufcnt
> 0 && *readchar_bufp
== '\003')
1083 /* Consume the interrupt character in the buffer. */
1085 (*the_target
->request_interrupt
) ();
1092 write_ok (char *buf
)
1100 write_enn (char *buf
)
1102 /* Some day, we should define the meanings of the error codes... */
1111 #ifndef IN_PROCESS_AGENT
1114 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1116 if ((regno
>> 12) != 0)
1117 *buf
++ = tohex ((regno
>> 12) & 0xf);
1118 if ((regno
>> 8) != 0)
1119 *buf
++ = tohex ((regno
>> 8) & 0xf);
1120 *buf
++ = tohex ((regno
>> 4) & 0xf);
1121 *buf
++ = tohex (regno
& 0xf);
1123 collect_register_as_string (regcache
, regno
, buf
);
1124 buf
+= 2 * register_size (regcache
->tdesc
, regno
);
1131 prepare_resume_reply (char *buf
, ptid_t ptid
,
1132 struct target_waitstatus
*status
)
1134 client_state
&cs
= get_client_state ();
1136 debug_printf ("Writing resume reply for %s:%d\n",
1137 target_pid_to_str (ptid
), status
->kind
);
1139 switch (status
->kind
)
1141 case TARGET_WAITKIND_STOPPED
:
1142 case TARGET_WAITKIND_FORKED
:
1143 case TARGET_WAITKIND_VFORKED
:
1144 case TARGET_WAITKIND_VFORK_DONE
:
1145 case TARGET_WAITKIND_EXECD
:
1146 case TARGET_WAITKIND_THREAD_CREATED
:
1147 case TARGET_WAITKIND_SYSCALL_ENTRY
:
1148 case TARGET_WAITKIND_SYSCALL_RETURN
:
1150 struct thread_info
*saved_thread
;
1152 struct regcache
*regcache
;
1154 if ((status
->kind
== TARGET_WAITKIND_FORKED
&& cs
.report_fork_events
)
1155 || (status
->kind
== TARGET_WAITKIND_VFORKED
1156 && cs
.report_vfork_events
))
1158 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1159 const char *event
= (status
->kind
== TARGET_WAITKIND_FORKED
1160 ? "fork" : "vfork");
1162 sprintf (buf
, "T%02x%s:", signal
, event
);
1163 buf
+= strlen (buf
);
1164 buf
= write_ptid (buf
, status
->value
.related_pid
);
1167 else if (status
->kind
== TARGET_WAITKIND_VFORK_DONE
1168 && cs
.report_vfork_events
)
1170 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1172 sprintf (buf
, "T%02xvforkdone:;", signal
);
1174 else if (status
->kind
== TARGET_WAITKIND_EXECD
&& cs
.report_exec_events
)
1176 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1177 const char *event
= "exec";
1178 char hexified_pathname
[PATH_MAX
* 2];
1180 sprintf (buf
, "T%02x%s:", signal
, event
);
1181 buf
+= strlen (buf
);
1183 /* Encode pathname to hexified format. */
1184 bin2hex ((const gdb_byte
*) status
->value
.execd_pathname
,
1186 strlen (status
->value
.execd_pathname
));
1188 sprintf (buf
, "%s;", hexified_pathname
);
1189 xfree (status
->value
.execd_pathname
);
1190 status
->value
.execd_pathname
= NULL
;
1191 buf
+= strlen (buf
);
1193 else if (status
->kind
== TARGET_WAITKIND_THREAD_CREATED
1194 && cs
.report_thread_events
)
1196 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1198 sprintf (buf
, "T%02xcreate:;", signal
);
1200 else if (status
->kind
== TARGET_WAITKIND_SYSCALL_ENTRY
1201 || status
->kind
== TARGET_WAITKIND_SYSCALL_RETURN
)
1203 enum gdb_signal signal
= GDB_SIGNAL_TRAP
;
1204 const char *event
= (status
->kind
== TARGET_WAITKIND_SYSCALL_ENTRY
1205 ? "syscall_entry" : "syscall_return");
1207 sprintf (buf
, "T%02x%s:%x;", signal
, event
,
1208 status
->value
.syscall_number
);
1211 sprintf (buf
, "T%02x", status
->value
.sig
);
1213 buf
+= strlen (buf
);
1215 saved_thread
= current_thread
;
1217 switch_to_thread (ptid
);
1219 regp
= current_target_desc ()->expedite_regs
;
1221 regcache
= get_thread_regcache (current_thread
, 1);
1223 if (the_target
->stopped_by_watchpoint
!= NULL
1224 && (*the_target
->stopped_by_watchpoint
) ())
1229 memcpy (buf
, "watch:", 6);
1232 addr
= (*the_target
->stopped_data_address
) ();
1234 /* Convert each byte of the address into two hexadecimal
1235 chars. Note that we take sizeof (void *) instead of
1236 sizeof (addr); this is to avoid sending a 64-bit
1237 address to a 32-bit GDB. */
1238 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1239 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1242 else if (cs
.swbreak_feature
&& target_stopped_by_sw_breakpoint ())
1244 sprintf (buf
, "swbreak:;");
1245 buf
+= strlen (buf
);
1247 else if (cs
.hwbreak_feature
&& target_stopped_by_hw_breakpoint ())
1249 sprintf (buf
, "hwbreak:;");
1250 buf
+= strlen (buf
);
1255 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1260 /* Formerly, if the debugger had not used any thread features
1261 we would not burden it with a thread status response. This
1262 was for the benefit of GDB 4.13 and older. However, in
1263 recent GDB versions the check (``if (cont_thread != 0)'')
1264 does not have the desired effect because of sillyness in
1265 the way that the remote protocol handles specifying a
1266 thread. Since thread support relies on qSymbol support
1267 anyway, assume GDB can handle threads. */
1269 if (using_threads
&& !disable_packet_Tthread
)
1271 /* This if (1) ought to be unnecessary. But remote_wait
1272 in GDB will claim this event belongs to inferior_ptid
1273 if we do not specify a thread, and there's no way for
1274 gdbserver to know what inferior_ptid is. */
1275 if (1 || cs
.general_thread
!= ptid
)
1278 /* In non-stop, don't change the general thread behind
1281 cs
.general_thread
= ptid
;
1282 sprintf (buf
, "thread:");
1283 buf
+= strlen (buf
);
1284 buf
= write_ptid (buf
, ptid
);
1286 buf
+= strlen (buf
);
1288 core
= target_core_of_thread (ptid
);
1292 sprintf (buf
, "core:");
1293 buf
+= strlen (buf
);
1294 sprintf (buf
, "%x", core
);
1296 buf
+= strlen (buf
);
1303 strcpy (buf
, "library:;");
1304 buf
+= strlen (buf
);
1308 current_thread
= saved_thread
;
1311 case TARGET_WAITKIND_EXITED
:
1312 if (cs
.multi_process
)
1313 sprintf (buf
, "W%x;process:%x",
1314 status
->value
.integer
, ptid
.pid ());
1316 sprintf (buf
, "W%02x", status
->value
.integer
);
1318 case TARGET_WAITKIND_SIGNALLED
:
1319 if (cs
.multi_process
)
1320 sprintf (buf
, "X%x;process:%x",
1321 status
->value
.sig
, ptid
.pid ());
1323 sprintf (buf
, "X%02x", status
->value
.sig
);
1325 case TARGET_WAITKIND_THREAD_EXITED
:
1326 sprintf (buf
, "w%x;", status
->value
.integer
);
1327 buf
+= strlen (buf
);
1328 buf
= write_ptid (buf
, ptid
);
1330 case TARGET_WAITKIND_NO_RESUMED
:
1334 error ("unhandled waitkind");
1340 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1344 *mem_addr_ptr
= *len_ptr
= 0;
1346 while ((ch
= from
[i
++]) != ',')
1348 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1349 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1352 for (j
= 0; j
< 4; j
++)
1354 if ((ch
= from
[i
++]) == 0)
1356 *len_ptr
= *len_ptr
<< 4;
1357 *len_ptr
|= fromhex (ch
) & 0x0f;
1362 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1363 unsigned char **to_p
)
1367 *mem_addr_ptr
= *len_ptr
= 0;
1369 while ((ch
= from
[i
++]) != ',')
1371 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1372 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1375 while ((ch
= from
[i
++]) != ':')
1377 *len_ptr
= *len_ptr
<< 4;
1378 *len_ptr
|= fromhex (ch
) & 0x0f;
1382 *to_p
= (unsigned char *) xmalloc (*len_ptr
);
1384 hex2bin (&from
[i
++], *to_p
, *len_ptr
);
1388 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1389 unsigned int *len_ptr
, unsigned char **to_p
)
1393 *mem_addr_ptr
= *len_ptr
= 0;
1395 while ((ch
= from
[i
++]) != ',')
1397 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1398 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1401 while ((ch
= from
[i
++]) != ':')
1403 *len_ptr
= *len_ptr
<< 4;
1404 *len_ptr
|= fromhex (ch
) & 0x0f;
1408 *to_p
= (unsigned char *) xmalloc (*len_ptr
);
1410 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1411 *to_p
, *len_ptr
) != *len_ptr
)
1417 /* Decode a qXfer write request. */
1420 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1421 unsigned int *len
, unsigned char *data
)
1426 /* Extract the offset. */
1428 while ((ch
= *buf
++) != ':')
1430 *offset
= *offset
<< 4;
1431 *offset
|= fromhex (ch
) & 0x0f;
1434 /* Get encoded data. */
1435 packet_len
-= buf
- b
;
1436 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1441 /* Decode the parameters of a qSearch:memory packet. */
1444 decode_search_memory_packet (const char *buf
, int packet_len
,
1445 CORE_ADDR
*start_addrp
,
1446 CORE_ADDR
*search_space_lenp
,
1447 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1449 const char *p
= buf
;
1451 p
= decode_address_to_semicolon (start_addrp
, p
);
1452 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1453 packet_len
-= p
- buf
;
1454 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1455 pattern
, packet_len
);
1460 free_sym_cache (struct sym_cache
*sym
)
1470 clear_symbol_cache (struct sym_cache
**symcache_p
)
1472 struct sym_cache
*sym
, *next
;
1474 /* Check the cache first. */
1475 for (sym
= *symcache_p
; sym
; sym
= next
)
1478 free_sym_cache (sym
);
1484 /* Get the address of NAME, and return it in ADDRP if found. if
1485 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1486 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1489 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1491 client_state
&cs
= get_client_state ();
1494 struct sym_cache
*sym
;
1495 struct process_info
*proc
;
1497 proc
= current_process ();
1499 /* Check the cache first. */
1500 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1501 if (strcmp (name
, sym
->name
) == 0)
1507 /* It might not be an appropriate time to look up a symbol,
1508 e.g. while we're trying to fetch registers. */
1512 /* Send the request. */
1513 strcpy (cs
.own_buf
, "qSymbol:");
1514 bin2hex ((const gdb_byte
*) name
, cs
.own_buf
+ strlen ("qSymbol:"),
1516 if (putpkt (cs
.own_buf
) < 0)
1519 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1520 len
= getpkt (cs
.own_buf
);
1524 /* We ought to handle pretty much any packet at this point while we
1525 wait for the qSymbol "response". That requires re-entering the
1526 main loop. For now, this is an adequate approximation; allow
1527 GDB to read from memory and handle 'v' packets (for vFile transfers)
1528 while it figures out the address of the symbol. */
1531 if (cs
.own_buf
[0] == 'm')
1534 unsigned char *mem_buf
;
1535 unsigned int mem_len
;
1537 decode_m_packet (&cs
.own_buf
[1], &mem_addr
, &mem_len
);
1538 mem_buf
= (unsigned char *) xmalloc (mem_len
);
1539 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1540 bin2hex (mem_buf
, cs
.own_buf
, mem_len
);
1542 write_enn (cs
.own_buf
);
1544 if (putpkt (cs
.own_buf
) < 0)
1547 else if (cs
.own_buf
[0] == 'v')
1550 handle_v_requests (cs
.own_buf
, len
, &new_len
);
1552 putpkt_binary (cs
.own_buf
, new_len
);
1554 putpkt (cs
.own_buf
);
1558 len
= getpkt (cs
.own_buf
);
1563 if (!startswith (cs
.own_buf
, "qSymbol:"))
1565 warning ("Malformed response to qSymbol, ignoring: %s\n", cs
.own_buf
);
1569 p
= cs
.own_buf
+ strlen ("qSymbol:");
1571 while (*q
&& *q
!= ':')
1574 /* Make sure we found a value for the symbol. */
1575 if (p
== q
|| *q
== '\0')
1578 decode_address (addrp
, p
, q
- p
);
1580 /* Save the symbol in our cache. */
1581 sym
= XNEW (struct sym_cache
);
1582 sym
->name
= xstrdup (name
);
1584 sym
->next
= proc
->symbol_cache
;
1585 proc
->symbol_cache
= sym
;
1590 /* Relocate an instruction to execute at a different address. OLDLOC
1591 is the address in the inferior memory where the instruction to
1592 relocate is currently at. On input, TO points to the destination
1593 where we want the instruction to be copied (and possibly adjusted)
1594 to. On output, it points to one past the end of the resulting
1595 instruction(s). The effect of executing the instruction at TO
1596 shall be the same as if executing it at OLDLOC. For example, call
1597 instructions that implicitly push the return address on the stack
1598 should be adjusted to return to the instruction after OLDLOC;
1599 relative branches, and other PC-relative instructions need the
1600 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1603 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1605 client_state
&cs
= get_client_state ();
1607 ULONGEST written
= 0;
1609 /* Send the request. */
1610 sprintf (cs
.own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1612 if (putpkt (cs
.own_buf
) < 0)
1615 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1616 len
= getpkt (cs
.own_buf
);
1620 /* We ought to handle pretty much any packet at this point while we
1621 wait for the qRelocInsn "response". That requires re-entering
1622 the main loop. For now, this is an adequate approximation; allow
1623 GDB to access memory. */
1624 while (cs
.own_buf
[0] == 'm' || cs
.own_buf
[0] == 'M' || cs
.own_buf
[0] == 'X')
1627 unsigned char *mem_buf
= NULL
;
1628 unsigned int mem_len
;
1630 if (cs
.own_buf
[0] == 'm')
1632 decode_m_packet (&cs
.own_buf
[1], &mem_addr
, &mem_len
);
1633 mem_buf
= (unsigned char *) xmalloc (mem_len
);
1634 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1635 bin2hex (mem_buf
, cs
.own_buf
, mem_len
);
1637 write_enn (cs
.own_buf
);
1639 else if (cs
.own_buf
[0] == 'X')
1641 if (decode_X_packet (&cs
.own_buf
[1], len
- 1, &mem_addr
,
1642 &mem_len
, &mem_buf
) < 0
1643 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1644 write_enn (cs
.own_buf
);
1646 write_ok (cs
.own_buf
);
1650 decode_M_packet (&cs
.own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1651 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1652 write_ok (cs
.own_buf
);
1654 write_enn (cs
.own_buf
);
1657 if (putpkt (cs
.own_buf
) < 0)
1659 len
= getpkt (cs
.own_buf
);
1664 if (cs
.own_buf
[0] == 'E')
1666 warning ("An error occurred while relocating an instruction: %s\n",
1671 if (!startswith (cs
.own_buf
, "qRelocInsn:"))
1673 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1678 unpack_varlen_hex (cs
.own_buf
+ strlen ("qRelocInsn:"), &written
);
1685 monitor_output (const char *msg
)
1687 int len
= strlen (msg
);
1688 char *buf
= (char *) xmalloc (len
* 2 + 2);
1691 bin2hex ((const gdb_byte
*) msg
, buf
+ 1, len
);