xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/remote-utils.cc (revision 64f917f5a88990e32dd65fcd4348042fa7f852b9)
18dffb485Schristos /* Remote utility routines for the remote server for GDB.
2*64f917f5Schristos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of GDB.
58dffb485Schristos 
68dffb485Schristos    This program is free software; you can redistribute it and/or modify
78dffb485Schristos    it under the terms of the GNU General Public License as published by
88dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
98dffb485Schristos    (at your option) any later version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148dffb485Schristos    GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos #if HAVE_TERMIOS_H
208dffb485Schristos #include <termios.h>
218dffb485Schristos #endif
228dffb485Schristos #include "target.h"
238dffb485Schristos #include "gdbthread.h"
248dffb485Schristos #include "tdesc.h"
258dffb485Schristos #include "debug.h"
268dffb485Schristos #include "dll.h"
278dffb485Schristos #include "gdbsupport/rsp-low.h"
288dffb485Schristos #include "gdbsupport/netstuff.h"
298dffb485Schristos #include "gdbsupport/filestuff.h"
308dffb485Schristos #include "gdbsupport/gdb-sigmask.h"
318dffb485Schristos #include <ctype.h>
328dffb485Schristos #if HAVE_SYS_IOCTL_H
338dffb485Schristos #include <sys/ioctl.h>
348dffb485Schristos #endif
358dffb485Schristos #if HAVE_SYS_FILE_H
368dffb485Schristos #include <sys/file.h>
378dffb485Schristos #endif
388dffb485Schristos #if HAVE_NETINET_IN_H
398dffb485Schristos #include <netinet/in.h>
408dffb485Schristos #endif
418dffb485Schristos #if HAVE_SYS_SOCKET_H
428dffb485Schristos #include <sys/socket.h>
438dffb485Schristos #endif
448dffb485Schristos #if HAVE_NETDB_H
458dffb485Schristos #include <netdb.h>
468dffb485Schristos #endif
478dffb485Schristos #if HAVE_NETINET_TCP_H
488dffb485Schristos #include <netinet/tcp.h>
498dffb485Schristos #endif
508dffb485Schristos #if HAVE_SYS_IOCTL_H
518dffb485Schristos #include <sys/ioctl.h>
528dffb485Schristos #endif
538dffb485Schristos #if HAVE_SIGNAL_H
548dffb485Schristos #include <signal.h>
558dffb485Schristos #endif
568dffb485Schristos #if HAVE_FCNTL_H
578dffb485Schristos #include <fcntl.h>
588dffb485Schristos #endif
598dffb485Schristos #include "gdbsupport/gdb_sys_time.h"
608dffb485Schristos #include <unistd.h>
618dffb485Schristos #if HAVE_ARPA_INET_H
628dffb485Schristos #include <arpa/inet.h>
638dffb485Schristos #endif
648dffb485Schristos #include <sys/stat.h>
658dffb485Schristos 
668dffb485Schristos #if USE_WIN32API
678dffb485Schristos #include <ws2tcpip.h>
688dffb485Schristos #endif
698dffb485Schristos 
708dffb485Schristos #ifndef HAVE_SOCKLEN_T
718dffb485Schristos typedef int socklen_t;
728dffb485Schristos #endif
738dffb485Schristos 
748dffb485Schristos #ifndef IN_PROCESS_AGENT
758dffb485Schristos 
768dffb485Schristos /* Extra value for readchar_callback.  */
778dffb485Schristos enum {
788dffb485Schristos   /* The callback is currently not scheduled.  */
798dffb485Schristos   NOT_SCHEDULED = -1
808dffb485Schristos };
818dffb485Schristos 
828dffb485Schristos /* Status of the readchar callback.
838dffb485Schristos    Either NOT_SCHEDULED or the callback id.  */
848dffb485Schristos static int readchar_callback = NOT_SCHEDULED;
858dffb485Schristos 
868dffb485Schristos static int readchar (void);
878dffb485Schristos static void reset_readchar (void);
888dffb485Schristos static void reschedule (void);
898dffb485Schristos 
908dffb485Schristos /* A cache entry for a successfully looked-up symbol.  */
918dffb485Schristos struct sym_cache
928dffb485Schristos {
938dffb485Schristos   char *name;
948dffb485Schristos   CORE_ADDR addr;
958dffb485Schristos   struct sym_cache *next;
968dffb485Schristos };
978dffb485Schristos 
988dffb485Schristos static int remote_is_stdio = 0;
998dffb485Schristos 
1008dffb485Schristos static int remote_desc = -1;
1018dffb485Schristos static int listen_desc = -1;
1028dffb485Schristos 
1038dffb485Schristos #ifdef USE_WIN32API
1044b169a6bSchristos /* gnulib wraps these as macros, undo them.  */
1054b169a6bSchristos # undef read
1064b169a6bSchristos # undef write
1074b169a6bSchristos 
1088dffb485Schristos # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
1098dffb485Schristos # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
1108dffb485Schristos #endif
1118dffb485Schristos 
1128dffb485Schristos int
1138dffb485Schristos gdb_connected (void)
1148dffb485Schristos {
1158dffb485Schristos   return remote_desc != -1;
1168dffb485Schristos }
1178dffb485Schristos 
1188dffb485Schristos /* Return true if the remote connection is over stdio.  */
1198dffb485Schristos 
1208dffb485Schristos int
1218dffb485Schristos remote_connection_is_stdio (void)
1228dffb485Schristos {
1238dffb485Schristos   return remote_is_stdio;
1248dffb485Schristos }
1258dffb485Schristos 
1268dffb485Schristos static void
1278dffb485Schristos enable_async_notification (int fd)
1288dffb485Schristos {
1298dffb485Schristos #if defined(F_SETFL) && defined (FASYNC)
1308dffb485Schristos   int save_fcntl_flags;
1318dffb485Schristos 
1328dffb485Schristos   save_fcntl_flags = fcntl (fd, F_GETFL, 0);
1338dffb485Schristos   fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
1348dffb485Schristos #if defined (F_SETOWN)
1358dffb485Schristos   fcntl (fd, F_SETOWN, getpid ());
1368dffb485Schristos #endif
1378dffb485Schristos #endif
1388dffb485Schristos }
1398dffb485Schristos 
1408dffb485Schristos static void
1418dffb485Schristos handle_accept_event (int err, gdb_client_data client_data)
1428dffb485Schristos {
1438dffb485Schristos   struct sockaddr_storage sockaddr;
1448dffb485Schristos   socklen_t len = sizeof (sockaddr);
1458dffb485Schristos 
1464b169a6bSchristos   threads_debug_printf ("handling possible accept event");
1478dffb485Schristos 
1488dffb485Schristos   remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &len);
1498dffb485Schristos   if (remote_desc == -1)
1508dffb485Schristos     perror_with_name ("Accept failed");
1518dffb485Schristos 
1528dffb485Schristos   /* Enable TCP keep alive process. */
1538dffb485Schristos   socklen_t tmp = 1;
1548dffb485Schristos   setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
1558dffb485Schristos 	      (char *) &tmp, sizeof (tmp));
1568dffb485Schristos 
1578dffb485Schristos   /* Tell TCP not to delay small packets.  This greatly speeds up
1588dffb485Schristos      interactive response. */
1598dffb485Schristos   tmp = 1;
1608dffb485Schristos   setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
1618dffb485Schristos 	      (char *) &tmp, sizeof (tmp));
1628dffb485Schristos 
1638dffb485Schristos #ifndef USE_WIN32API
1648dffb485Schristos   signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbserver simply
1658dffb485Schristos 				   exits when the remote side dies.  */
1668dffb485Schristos #endif
1678dffb485Schristos 
1688dffb485Schristos   if (run_once)
1698dffb485Schristos     {
1708dffb485Schristos #ifndef USE_WIN32API
1718dffb485Schristos       close (listen_desc);		/* No longer need this */
1728dffb485Schristos #else
1738dffb485Schristos       closesocket (listen_desc);	/* No longer need this */
1748dffb485Schristos #endif
1758dffb485Schristos     }
1768dffb485Schristos 
1778dffb485Schristos   /* Even if !RUN_ONCE no longer notice new connections.  Still keep the
1788dffb485Schristos      descriptor open for add_file_handler to wait for a new connection.  */
1798dffb485Schristos   delete_file_handler (listen_desc);
1808dffb485Schristos 
1818dffb485Schristos   /* Convert IP address to string.  */
1828dffb485Schristos   char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
1838dffb485Schristos 
1848dffb485Schristos   int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
1858dffb485Schristos 		       orig_host, sizeof (orig_host),
1868dffb485Schristos 		       orig_port, sizeof (orig_port),
1878dffb485Schristos 		       NI_NUMERICHOST | NI_NUMERICSERV);
1888dffb485Schristos 
1898dffb485Schristos   if (r != 0)
1908dffb485Schristos     fprintf (stderr, _("Could not obtain remote address: %s\n"),
1918dffb485Schristos 	     gai_strerror (r));
1928dffb485Schristos   else
1938dffb485Schristos     fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
1948dffb485Schristos 	     orig_host, orig_port);
1958dffb485Schristos 
1968dffb485Schristos   enable_async_notification (remote_desc);
1978dffb485Schristos 
1988dffb485Schristos   /* Register the event loop handler.  */
1994b169a6bSchristos   add_file_handler (remote_desc, handle_serial_event, NULL, "remote-net");
2008dffb485Schristos 
2018dffb485Schristos   /* We have a new GDB connection now.  If we were disconnected
2028dffb485Schristos      tracing, there's a window where the target could report a stop
2038dffb485Schristos      event to the event loop, and since we have a connection now, we'd
2048dffb485Schristos      try to send vStopped notifications to GDB.  But, don't do that
2058dffb485Schristos      until GDB as selected all-stop/non-stop, and has queried the
2068dffb485Schristos      threads' status ('?').  */
2078dffb485Schristos   target_async (0);
2088dffb485Schristos }
2098dffb485Schristos 
2108dffb485Schristos /* Prepare for a later connection to a remote debugger.
2118dffb485Schristos    NAME is the filename used for communication.  */
2128dffb485Schristos 
2138dffb485Schristos void
2148dffb485Schristos remote_prepare (const char *name)
2158dffb485Schristos {
2168dffb485Schristos   client_state &cs = get_client_state ();
2178dffb485Schristos #ifdef USE_WIN32API
2188dffb485Schristos   static int winsock_initialized;
2198dffb485Schristos #endif
2208dffb485Schristos   socklen_t tmp;
2218dffb485Schristos 
2228dffb485Schristos   remote_is_stdio = 0;
2238dffb485Schristos   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
2248dffb485Schristos     {
2258dffb485Schristos       /* We need to record fact that we're using stdio sooner than the
2268dffb485Schristos 	 call to remote_open so start_inferior knows the connection is
2278dffb485Schristos 	 via stdio.  */
2288dffb485Schristos       remote_is_stdio = 1;
2298dffb485Schristos       cs.transport_is_reliable = 1;
2308dffb485Schristos       return;
2318dffb485Schristos     }
2328dffb485Schristos 
2338dffb485Schristos   struct addrinfo hint;
2348dffb485Schristos   struct addrinfo *ainfo;
2358dffb485Schristos 
2368dffb485Schristos   memset (&hint, 0, sizeof (hint));
2378dffb485Schristos   /* Assume no prefix will be passed, therefore we should use
2388dffb485Schristos      AF_UNSPEC.  */
2398dffb485Schristos   hint.ai_family = AF_UNSPEC;
2408dffb485Schristos   hint.ai_socktype = SOCK_STREAM;
2418dffb485Schristos   hint.ai_protocol = IPPROTO_TCP;
2428dffb485Schristos 
2438dffb485Schristos   parsed_connection_spec parsed
2448dffb485Schristos     = parse_connection_spec_without_prefix (name, &hint);
2458dffb485Schristos 
2468dffb485Schristos   if (parsed.port_str.empty ())
2478dffb485Schristos     {
2488dffb485Schristos       cs.transport_is_reliable = 0;
2498dffb485Schristos       return;
2508dffb485Schristos     }
2518dffb485Schristos 
2528dffb485Schristos #ifdef USE_WIN32API
2538dffb485Schristos   if (!winsock_initialized)
2548dffb485Schristos     {
2558dffb485Schristos       WSADATA wsad;
2568dffb485Schristos 
2578dffb485Schristos       WSAStartup (MAKEWORD (1, 0), &wsad);
2588dffb485Schristos       winsock_initialized = 1;
2598dffb485Schristos     }
2608dffb485Schristos #endif
2618dffb485Schristos 
2628dffb485Schristos   int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
2638dffb485Schristos 		       &hint, &ainfo);
2648dffb485Schristos 
2658dffb485Schristos   if (r != 0)
2668dffb485Schristos     error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
2678dffb485Schristos 
2688dffb485Schristos   scoped_free_addrinfo freeaddrinfo (ainfo);
2698dffb485Schristos 
2708dffb485Schristos   struct addrinfo *iter;
2718dffb485Schristos 
2728dffb485Schristos   for (iter = ainfo; iter != NULL; iter = iter->ai_next)
2738dffb485Schristos     {
2748dffb485Schristos       listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
2758dffb485Schristos 					iter->ai_protocol);
2768dffb485Schristos 
2778dffb485Schristos       if (listen_desc >= 0)
2788dffb485Schristos 	break;
2798dffb485Schristos     }
2808dffb485Schristos 
2818dffb485Schristos   if (iter == NULL)
2828dffb485Schristos     perror_with_name ("Can't open socket");
2838dffb485Schristos 
2848dffb485Schristos   /* Allow rapid reuse of this port. */
2858dffb485Schristos   tmp = 1;
2868dffb485Schristos   setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
2878dffb485Schristos 	      sizeof (tmp));
2888dffb485Schristos 
2898dffb485Schristos   switch (iter->ai_family)
2908dffb485Schristos     {
2918dffb485Schristos     case AF_INET:
2928dffb485Schristos       ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
2938dffb485Schristos       break;
2948dffb485Schristos     case AF_INET6:
2958dffb485Schristos       ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
2968dffb485Schristos       break;
2978dffb485Schristos     default:
2984b169a6bSchristos       internal_error (_("Invalid 'ai_family' %d\n"), iter->ai_family);
2998dffb485Schristos     }
3008dffb485Schristos 
3018dffb485Schristos   if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
3028dffb485Schristos     perror_with_name ("Can't bind address");
3038dffb485Schristos 
3048dffb485Schristos   if (listen (listen_desc, 1) != 0)
3058dffb485Schristos     perror_with_name ("Can't listen on socket");
3068dffb485Schristos 
3078dffb485Schristos   cs.transport_is_reliable = 1;
3088dffb485Schristos }
3098dffb485Schristos 
3108dffb485Schristos /* Open a connection to a remote debugger.
3118dffb485Schristos    NAME is the filename used for communication.  */
3128dffb485Schristos 
3138dffb485Schristos void
3148dffb485Schristos remote_open (const char *name)
3158dffb485Schristos {
3168dffb485Schristos   const char *port_str;
3178dffb485Schristos 
3188dffb485Schristos   port_str = strchr (name, ':');
3198dffb485Schristos #ifdef USE_WIN32API
3208dffb485Schristos   if (port_str == NULL)
3218dffb485Schristos     error ("Only HOST:PORT is supported on this platform.");
3228dffb485Schristos #endif
3238dffb485Schristos 
3248dffb485Schristos   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
3258dffb485Schristos     {
3268dffb485Schristos       fprintf (stderr, "Remote debugging using stdio\n");
3278dffb485Schristos 
3288dffb485Schristos       /* Use stdin as the handle of the connection.
3298dffb485Schristos 	 We only select on reads, for example.  */
3308dffb485Schristos       remote_desc = fileno (stdin);
3318dffb485Schristos 
3328dffb485Schristos       enable_async_notification (remote_desc);
3338dffb485Schristos 
3348dffb485Schristos       /* Register the event loop handler.  */
3354b169a6bSchristos       add_file_handler (remote_desc, handle_serial_event, NULL, "remote-stdio");
3368dffb485Schristos     }
3378dffb485Schristos #ifndef USE_WIN32API
3388dffb485Schristos   else if (port_str == NULL)
3398dffb485Schristos     {
3408dffb485Schristos       struct stat statbuf;
3418dffb485Schristos 
3428dffb485Schristos       if (stat (name, &statbuf) == 0
3438dffb485Schristos 	  && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
3448dffb485Schristos 	remote_desc = open (name, O_RDWR);
3458dffb485Schristos       else
3468dffb485Schristos 	{
3478dffb485Schristos 	  errno = EINVAL;
3488dffb485Schristos 	  remote_desc = -1;
3498dffb485Schristos 	}
3508dffb485Schristos 
3518dffb485Schristos       if (remote_desc < 0)
3528dffb485Schristos 	perror_with_name ("Could not open remote device");
3538dffb485Schristos 
3548dffb485Schristos #if HAVE_TERMIOS_H
3558dffb485Schristos       {
3568dffb485Schristos 	struct termios termios;
3578dffb485Schristos 	tcgetattr (remote_desc, &termios);
3588dffb485Schristos 
3598dffb485Schristos 	termios.c_iflag = 0;
3608dffb485Schristos 	termios.c_oflag = 0;
3618dffb485Schristos 	termios.c_lflag = 0;
3628dffb485Schristos 	termios.c_cflag &= ~(CSIZE | PARENB);
3638dffb485Schristos 	termios.c_cflag |= CLOCAL | CS8;
3648dffb485Schristos 	termios.c_cc[VMIN] = 1;
3658dffb485Schristos 	termios.c_cc[VTIME] = 0;
3668dffb485Schristos 
3678dffb485Schristos 	tcsetattr (remote_desc, TCSANOW, &termios);
3688dffb485Schristos       }
3698dffb485Schristos #endif
3708dffb485Schristos 
3718dffb485Schristos       fprintf (stderr, "Remote debugging using %s\n", name);
3728dffb485Schristos 
3738dffb485Schristos       enable_async_notification (remote_desc);
3748dffb485Schristos 
3758dffb485Schristos       /* Register the event loop handler.  */
3764b169a6bSchristos       add_file_handler (remote_desc, handle_serial_event, NULL,
3774b169a6bSchristos 			"remote-device");
3788dffb485Schristos     }
3798dffb485Schristos #endif /* USE_WIN32API */
3808dffb485Schristos   else
3818dffb485Schristos     {
3828dffb485Schristos       char listen_port[GDB_NI_MAX_PORT];
3838dffb485Schristos       struct sockaddr_storage sockaddr;
3848dffb485Schristos       socklen_t len = sizeof (sockaddr);
3858dffb485Schristos 
3868dffb485Schristos       if (getsockname (listen_desc, (struct sockaddr *) &sockaddr, &len) < 0)
3878dffb485Schristos 	perror_with_name ("Can't determine port");
3888dffb485Schristos 
3898dffb485Schristos       int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
3908dffb485Schristos 			   NULL, 0,
3918dffb485Schristos 			   listen_port, sizeof (listen_port),
3928dffb485Schristos 			   NI_NUMERICSERV);
3938dffb485Schristos 
3948dffb485Schristos       if (r != 0)
3958dffb485Schristos 	fprintf (stderr, _("Can't obtain port where we are listening: %s"),
3968dffb485Schristos 		 gai_strerror (r));
3978dffb485Schristos       else
3988dffb485Schristos 	fprintf (stderr, _("Listening on port %s\n"), listen_port);
3998dffb485Schristos 
4008dffb485Schristos       fflush (stderr);
4018dffb485Schristos 
4028dffb485Schristos       /* Register the event loop handler.  */
4034b169a6bSchristos       add_file_handler (listen_desc, handle_accept_event, NULL,
4044b169a6bSchristos 			"remote-listen");
4058dffb485Schristos     }
4068dffb485Schristos }
4078dffb485Schristos 
4088dffb485Schristos void
4098dffb485Schristos remote_close (void)
4108dffb485Schristos {
4118dffb485Schristos   delete_file_handler (remote_desc);
4128dffb485Schristos 
4138dffb485Schristos   disable_async_io ();
4148dffb485Schristos 
4158dffb485Schristos #ifdef USE_WIN32API
4168dffb485Schristos   closesocket (remote_desc);
4178dffb485Schristos #else
4188dffb485Schristos   if (! remote_connection_is_stdio ())
4198dffb485Schristos     close (remote_desc);
4208dffb485Schristos #endif
4218dffb485Schristos   remote_desc = -1;
4228dffb485Schristos 
4238dffb485Schristos   reset_readchar ();
4248dffb485Schristos }
4258dffb485Schristos 
4268dffb485Schristos #endif
4278dffb485Schristos 
4288dffb485Schristos #ifndef IN_PROCESS_AGENT
4298dffb485Schristos 
4308dffb485Schristos void
4318dffb485Schristos decode_address (CORE_ADDR *addrp, const char *start, int len)
4328dffb485Schristos {
4338dffb485Schristos   CORE_ADDR addr;
4348dffb485Schristos   char ch;
4358dffb485Schristos   int i;
4368dffb485Schristos 
4378dffb485Schristos   addr = 0;
4388dffb485Schristos   for (i = 0; i < len; i++)
4398dffb485Schristos     {
4408dffb485Schristos       ch = start[i];
4418dffb485Schristos       addr = addr << 4;
4428dffb485Schristos       addr = addr | (fromhex (ch) & 0x0f);
4438dffb485Schristos     }
4448dffb485Schristos   *addrp = addr;
4458dffb485Schristos }
4468dffb485Schristos 
4478dffb485Schristos const char *
4488dffb485Schristos decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
4498dffb485Schristos {
4508dffb485Schristos   const char *end;
4518dffb485Schristos 
4528dffb485Schristos   end = start;
4538dffb485Schristos   while (*end != '\0' && *end != ';')
4548dffb485Schristos     end++;
4558dffb485Schristos 
4568dffb485Schristos   decode_address (addrp, start, end - start);
4578dffb485Schristos 
4588dffb485Schristos   if (*end == ';')
4598dffb485Schristos     end++;
4608dffb485Schristos   return end;
4618dffb485Schristos }
4628dffb485Schristos 
4638dffb485Schristos #endif
4648dffb485Schristos 
4658dffb485Schristos #ifndef IN_PROCESS_AGENT
4668dffb485Schristos 
4678dffb485Schristos /* Look for a sequence of characters which can be run-length encoded.
4688dffb485Schristos    If there are any, update *CSUM and *P.  Otherwise, output the
4698dffb485Schristos    single character.  Return the number of characters consumed.  */
4708dffb485Schristos 
4718dffb485Schristos static int
4728dffb485Schristos try_rle (char *buf, int remaining, unsigned char *csum, char **p)
4738dffb485Schristos {
4748dffb485Schristos   int n;
4758dffb485Schristos 
4768dffb485Schristos   /* Always output the character.  */
4778dffb485Schristos   *csum += buf[0];
4788dffb485Schristos   *(*p)++ = buf[0];
4798dffb485Schristos 
4808dffb485Schristos   /* Don't go past '~'.  */
4818dffb485Schristos   if (remaining > 97)
4828dffb485Schristos     remaining = 97;
4838dffb485Schristos 
4848dffb485Schristos   for (n = 1; n < remaining; n++)
4858dffb485Schristos     if (buf[n] != buf[0])
4868dffb485Schristos       break;
4878dffb485Schristos 
4888dffb485Schristos   /* N is the index of the first character not the same as buf[0].
4898dffb485Schristos      buf[0] is counted twice, so by decrementing N, we get the number
4908dffb485Schristos      of characters the RLE sequence will replace.  */
4918dffb485Schristos   n--;
4928dffb485Schristos 
4938dffb485Schristos   if (n < 3)
4948dffb485Schristos     return 1;
4958dffb485Schristos 
4968dffb485Schristos   /* Skip the frame characters.  The manual says to skip '+' and '-'
4978dffb485Schristos      also, but there's no reason to.  Unfortunately these two unusable
4988dffb485Schristos      characters double the encoded length of a four byte zero
4998dffb485Schristos      value.  */
5008dffb485Schristos   while (n + 29 == '$' || n + 29 == '#')
5018dffb485Schristos     n--;
5028dffb485Schristos 
5038dffb485Schristos   *csum += '*';
5048dffb485Schristos   *(*p)++ = '*';
5058dffb485Schristos   *csum += n + 29;
5068dffb485Schristos   *(*p)++ = n + 29;
5078dffb485Schristos 
5088dffb485Schristos   return n + 1;
5098dffb485Schristos }
5108dffb485Schristos 
5118dffb485Schristos #endif
5128dffb485Schristos 
5138dffb485Schristos #ifndef IN_PROCESS_AGENT
5148dffb485Schristos 
5158dffb485Schristos /* Write a PTID to BUF.  Returns BUF+CHARACTERS_WRITTEN.  */
5168dffb485Schristos 
5178dffb485Schristos char *
5188dffb485Schristos write_ptid (char *buf, ptid_t ptid)
5198dffb485Schristos {
5208dffb485Schristos   client_state &cs = get_client_state ();
5218dffb485Schristos   int pid, tid;
5228dffb485Schristos 
5238dffb485Schristos   if (cs.multi_process)
5248dffb485Schristos     {
5258dffb485Schristos       pid = ptid.pid ();
5268dffb485Schristos       if (pid < 0)
5278dffb485Schristos 	buf += sprintf (buf, "p-%x.", -pid);
5288dffb485Schristos       else
5298dffb485Schristos 	buf += sprintf (buf, "p%x.", pid);
5308dffb485Schristos     }
5318dffb485Schristos   tid = ptid.lwp ();
5328dffb485Schristos   if (tid < 0)
5338dffb485Schristos     buf += sprintf (buf, "-%x", -tid);
5348dffb485Schristos   else
5358dffb485Schristos     buf += sprintf (buf, "%x", tid);
5368dffb485Schristos 
5378dffb485Schristos   return buf;
5388dffb485Schristos }
5398dffb485Schristos 
5408dffb485Schristos static ULONGEST
5418dffb485Schristos hex_or_minus_one (const char *buf, const char **obuf)
5428dffb485Schristos {
5438dffb485Schristos   ULONGEST ret;
5448dffb485Schristos 
5458dffb485Schristos   if (startswith (buf, "-1"))
5468dffb485Schristos     {
5478dffb485Schristos       ret = (ULONGEST) -1;
5488dffb485Schristos       buf += 2;
5498dffb485Schristos     }
5508dffb485Schristos   else
5518dffb485Schristos     buf = unpack_varlen_hex (buf, &ret);
5528dffb485Schristos 
5538dffb485Schristos   if (obuf)
5548dffb485Schristos     *obuf = buf;
5558dffb485Schristos 
5568dffb485Schristos   return ret;
5578dffb485Schristos }
5588dffb485Schristos 
5598dffb485Schristos /* Extract a PTID from BUF.  If non-null, OBUF is set to the to one
5608dffb485Schristos    passed the last parsed char.  Returns null_ptid on error.  */
5618dffb485Schristos ptid_t
5628dffb485Schristos read_ptid (const char *buf, const char **obuf)
5638dffb485Schristos {
5648dffb485Schristos   const char *p = buf;
5658dffb485Schristos   const char *pp;
5668dffb485Schristos   ULONGEST pid = 0, tid = 0;
5678dffb485Schristos 
5688dffb485Schristos   if (*p == 'p')
5698dffb485Schristos     {
5708dffb485Schristos       /* Multi-process ptid.  */
5718dffb485Schristos       pp = unpack_varlen_hex (p + 1, &pid);
5728dffb485Schristos       if (*pp != '.')
5738dffb485Schristos 	error ("invalid remote ptid: %s\n", p);
5748dffb485Schristos 
5758dffb485Schristos       p = pp + 1;
5768dffb485Schristos 
5778dffb485Schristos       tid = hex_or_minus_one (p, &pp);
5788dffb485Schristos 
5798dffb485Schristos       if (obuf)
5808dffb485Schristos 	*obuf = pp;
5814b169a6bSchristos       return ptid_t (pid, tid);
5828dffb485Schristos     }
5838dffb485Schristos 
5848dffb485Schristos   /* No multi-process.  Just a tid.  */
5858dffb485Schristos   tid = hex_or_minus_one (p, &pp);
5868dffb485Schristos 
5878dffb485Schristos   /* Since GDB is not sending a process id (multi-process extensions
5888dffb485Schristos      are off), then there's only one process.  Default to the first in
5898dffb485Schristos      the list.  */
5908dffb485Schristos   pid = pid_of (get_first_process ());
5918dffb485Schristos 
5928dffb485Schristos   if (obuf)
5938dffb485Schristos     *obuf = pp;
5944b169a6bSchristos   return ptid_t (pid, tid);
5958dffb485Schristos }
5968dffb485Schristos 
5978dffb485Schristos /* Write COUNT bytes in BUF to the client.
5988dffb485Schristos    The result is the number of bytes written or -1 if error.
5998dffb485Schristos    This may return less than COUNT.  */
6008dffb485Schristos 
6018dffb485Schristos static int
6028dffb485Schristos write_prim (const void *buf, int count)
6038dffb485Schristos {
6048dffb485Schristos   if (remote_connection_is_stdio ())
6058dffb485Schristos     return write (fileno (stdout), buf, count);
6068dffb485Schristos   else
6078dffb485Schristos     return write (remote_desc, buf, count);
6088dffb485Schristos }
6098dffb485Schristos 
6108dffb485Schristos /* Read COUNT bytes from the client and store in BUF.
6118dffb485Schristos    The result is the number of bytes read or -1 if error.
6128dffb485Schristos    This may return less than COUNT.  */
6138dffb485Schristos 
6148dffb485Schristos static int
6158dffb485Schristos read_prim (void *buf, int count)
6168dffb485Schristos {
6178dffb485Schristos   if (remote_connection_is_stdio ())
6188dffb485Schristos     return read (fileno (stdin), buf, count);
6198dffb485Schristos   else
6208dffb485Schristos     return read (remote_desc, buf, count);
6218dffb485Schristos }
6228dffb485Schristos 
6238dffb485Schristos /* Send a packet to the remote machine, with error checking.
6248dffb485Schristos    The data of the packet is in BUF, and the length of the
6258dffb485Schristos    packet is in CNT.  Returns >= 0 on success, -1 otherwise.  */
6268dffb485Schristos 
6278dffb485Schristos static int
6288dffb485Schristos putpkt_binary_1 (char *buf, int cnt, int is_notif)
6298dffb485Schristos {
6308dffb485Schristos   client_state &cs = get_client_state ();
6318dffb485Schristos   int i;
6328dffb485Schristos   unsigned char csum = 0;
6338dffb485Schristos   char *buf2;
6348dffb485Schristos   char *p;
6358dffb485Schristos   int cc;
6368dffb485Schristos 
6378dffb485Schristos   buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
6388dffb485Schristos 
6398dffb485Schristos   /* Copy the packet into buffer BUF2, encapsulating it
6408dffb485Schristos      and giving it a checksum.  */
6418dffb485Schristos 
6428dffb485Schristos   p = buf2;
6438dffb485Schristos   if (is_notif)
6448dffb485Schristos     *p++ = '%';
6458dffb485Schristos   else
6468dffb485Schristos     *p++ = '$';
6478dffb485Schristos 
6488dffb485Schristos   for (i = 0; i < cnt;)
6498dffb485Schristos     i += try_rle (buf + i, cnt - i, &csum, &p);
6508dffb485Schristos 
6518dffb485Schristos   *p++ = '#';
6528dffb485Schristos   *p++ = tohex ((csum >> 4) & 0xf);
6538dffb485Schristos   *p++ = tohex (csum & 0xf);
6548dffb485Schristos 
6558dffb485Schristos   *p = '\0';
6568dffb485Schristos 
6578dffb485Schristos   /* Send it over and over until we get a positive ack.  */
6588dffb485Schristos 
6598dffb485Schristos   do
6608dffb485Schristos     {
6618dffb485Schristos       if (write_prim (buf2, p - buf2) != p - buf2)
6628dffb485Schristos 	{
6638dffb485Schristos 	  perror ("putpkt(write)");
6648dffb485Schristos 	  free (buf2);
6658dffb485Schristos 	  return -1;
6668dffb485Schristos 	}
6678dffb485Schristos 
6688dffb485Schristos       if (cs.noack_mode || is_notif)
6698dffb485Schristos 	{
6708dffb485Schristos 	  /* Don't expect an ack then.  */
6718dffb485Schristos 	  if (is_notif)
6724b169a6bSchristos 	    remote_debug_printf ("putpkt (\"%s\"); [notif]", buf2);
6738dffb485Schristos 	  else
6744b169a6bSchristos 	    remote_debug_printf ("putpkt (\"%s\"); [noack mode]", buf2);
6754b169a6bSchristos 
6768dffb485Schristos 	  break;
6778dffb485Schristos 	}
6788dffb485Schristos 
6794b169a6bSchristos       remote_debug_printf ("putpkt (\"%s\"); [looking for ack]", buf2);
6808dffb485Schristos 
6818dffb485Schristos       cc = readchar ();
6828dffb485Schristos 
6838dffb485Schristos       if (cc < 0)
6848dffb485Schristos 	{
6858dffb485Schristos 	  free (buf2);
6868dffb485Schristos 	  return -1;
6878dffb485Schristos 	}
6888dffb485Schristos 
6894b169a6bSchristos       remote_debug_printf ("[received '%c' (0x%x)]", cc, cc);
6908dffb485Schristos 
6918dffb485Schristos       /* Check for an input interrupt while we're here.  */
6928dffb485Schristos       if (cc == '\003' && current_thread != NULL)
6938dffb485Schristos 	the_target->request_interrupt ();
6948dffb485Schristos     }
6958dffb485Schristos   while (cc != '+');
6968dffb485Schristos 
6978dffb485Schristos   free (buf2);
6988dffb485Schristos   return 1;			/* Success! */
6998dffb485Schristos }
7008dffb485Schristos 
7018dffb485Schristos int
7028dffb485Schristos putpkt_binary (char *buf, int cnt)
7038dffb485Schristos {
7048dffb485Schristos   return putpkt_binary_1 (buf, cnt, 0);
7058dffb485Schristos }
7068dffb485Schristos 
7078dffb485Schristos /* Send a packet to the remote machine, with error checking.  The data
7088dffb485Schristos    of the packet is in BUF, and the packet should be a NUL-terminated
7098dffb485Schristos    string.  Returns >= 0 on success, -1 otherwise.  */
7108dffb485Schristos 
7118dffb485Schristos int
7128dffb485Schristos putpkt (char *buf)
7138dffb485Schristos {
7148dffb485Schristos   return putpkt_binary (buf, strlen (buf));
7158dffb485Schristos }
7168dffb485Schristos 
7178dffb485Schristos int
7188dffb485Schristos putpkt_notif (char *buf)
7198dffb485Schristos {
7208dffb485Schristos   return putpkt_binary_1 (buf, strlen (buf), 1);
7218dffb485Schristos }
7228dffb485Schristos 
7238dffb485Schristos /* Come here when we get an input interrupt from the remote side.  This
7248dffb485Schristos    interrupt should only be active while we are waiting for the child to do
7258dffb485Schristos    something.  Thus this assumes readchar:bufcnt is 0.
7268dffb485Schristos    About the only thing that should come through is a ^C, which
7278dffb485Schristos    will cause us to request child interruption.  */
7288dffb485Schristos 
7298dffb485Schristos static void
7308dffb485Schristos input_interrupt (int unused)
7318dffb485Schristos {
7328dffb485Schristos   fd_set readset;
7338dffb485Schristos   struct timeval immediate = { 0, 0 };
7348dffb485Schristos 
7358dffb485Schristos   /* Protect against spurious interrupts.  This has been observed to
7368dffb485Schristos      be a problem under NetBSD 1.4 and 1.5.  */
7378dffb485Schristos 
7388dffb485Schristos   FD_ZERO (&readset);
7398dffb485Schristos   FD_SET (remote_desc, &readset);
7408dffb485Schristos   if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
7418dffb485Schristos     {
7428dffb485Schristos       int cc;
7438dffb485Schristos       char c = 0;
7448dffb485Schristos 
7458dffb485Schristos       cc = read_prim (&c, 1);
7468dffb485Schristos 
7478dffb485Schristos       if (cc == 0)
7488dffb485Schristos 	{
7498dffb485Schristos 	  fprintf (stderr, "client connection closed\n");
7508dffb485Schristos 	  return;
7518dffb485Schristos 	}
7528dffb485Schristos       else if (cc != 1 || c != '\003')
7538dffb485Schristos 	{
7548dffb485Schristos 	  fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
7558dffb485Schristos 	  if (isprint (c))
7568dffb485Schristos 	    fprintf (stderr, "('%c')\n", c);
7578dffb485Schristos 	  else
7588dffb485Schristos 	    fprintf (stderr, "('\\x%02x')\n", c & 0xff);
7598dffb485Schristos 	  return;
7608dffb485Schristos 	}
7618dffb485Schristos 
7628dffb485Schristos       the_target->request_interrupt ();
7638dffb485Schristos     }
7648dffb485Schristos }
7658dffb485Schristos 
7668dffb485Schristos /* Check if the remote side sent us an interrupt request (^C).  */
7678dffb485Schristos void
7688dffb485Schristos check_remote_input_interrupt_request (void)
7698dffb485Schristos {
7708dffb485Schristos   /* This function may be called before establishing communications,
7718dffb485Schristos      therefore we need to validate the remote descriptor.  */
7728dffb485Schristos 
7738dffb485Schristos   if (remote_desc == -1)
7748dffb485Schristos     return;
7758dffb485Schristos 
7768dffb485Schristos   input_interrupt (0);
7778dffb485Schristos }
7788dffb485Schristos 
7798dffb485Schristos /* Asynchronous I/O support.  SIGIO must be unblocked when waiting,
7808dffb485Schristos    in order to accept Control-C from the client, and must be blocked
7818dffb485Schristos    when talking to the client.  */
7828dffb485Schristos 
7838dffb485Schristos static void
7848dffb485Schristos block_unblock_async_io (int block)
7858dffb485Schristos {
7868dffb485Schristos #ifndef USE_WIN32API
7878dffb485Schristos   sigset_t sigio_set;
7888dffb485Schristos 
7898dffb485Schristos   sigemptyset (&sigio_set);
7908dffb485Schristos   sigaddset (&sigio_set, SIGIO);
7918dffb485Schristos   gdb_sigmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
7928dffb485Schristos #endif
7938dffb485Schristos }
7948dffb485Schristos 
7958dffb485Schristos /* Current state of asynchronous I/O.  */
7968dffb485Schristos static int async_io_enabled;
7978dffb485Schristos 
7988dffb485Schristos /* Enable asynchronous I/O.  */
7998dffb485Schristos void
8008dffb485Schristos enable_async_io (void)
8018dffb485Schristos {
8028dffb485Schristos   if (async_io_enabled)
8038dffb485Schristos     return;
8048dffb485Schristos 
8058dffb485Schristos   block_unblock_async_io (0);
8068dffb485Schristos 
8078dffb485Schristos   async_io_enabled = 1;
8088dffb485Schristos }
8098dffb485Schristos 
8108dffb485Schristos /* Disable asynchronous I/O.  */
8118dffb485Schristos void
8128dffb485Schristos disable_async_io (void)
8138dffb485Schristos {
8148dffb485Schristos   if (!async_io_enabled)
8158dffb485Schristos     return;
8168dffb485Schristos 
8178dffb485Schristos   block_unblock_async_io (1);
8188dffb485Schristos 
8198dffb485Schristos   async_io_enabled = 0;
8208dffb485Schristos }
8218dffb485Schristos 
8228dffb485Schristos void
8238dffb485Schristos initialize_async_io (void)
8248dffb485Schristos {
8258dffb485Schristos   /* Make sure that async I/O starts blocked.  */
8268dffb485Schristos   async_io_enabled = 1;
8278dffb485Schristos   disable_async_io ();
8288dffb485Schristos 
8298dffb485Schristos   /* Install the signal handler.  */
8308dffb485Schristos #ifndef USE_WIN32API
8318dffb485Schristos   signal (SIGIO, input_interrupt);
8328dffb485Schristos #endif
8338dffb485Schristos }
8348dffb485Schristos 
8358dffb485Schristos /* Internal buffer used by readchar.
8368dffb485Schristos    These are global to readchar because reschedule_remote needs to be
8378dffb485Schristos    able to tell whether the buffer is empty.  */
8388dffb485Schristos 
8398dffb485Schristos static unsigned char readchar_buf[BUFSIZ];
8408dffb485Schristos static int readchar_bufcnt = 0;
8418dffb485Schristos static unsigned char *readchar_bufp;
8428dffb485Schristos 
8438dffb485Schristos /* Returns next char from remote GDB.  -1 if error.  */
8448dffb485Schristos 
8458dffb485Schristos static int
8468dffb485Schristos readchar (void)
8478dffb485Schristos {
8488dffb485Schristos   int ch;
8498dffb485Schristos 
8508dffb485Schristos   if (readchar_bufcnt == 0)
8518dffb485Schristos     {
8528dffb485Schristos       readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
8538dffb485Schristos 
8548dffb485Schristos       if (readchar_bufcnt <= 0)
8558dffb485Schristos 	{
8568dffb485Schristos 	  if (readchar_bufcnt == 0)
8578dffb485Schristos 	    {
8584b169a6bSchristos 	      remote_debug_printf ("readchar: Got EOF");
8598dffb485Schristos 	    }
8608dffb485Schristos 	  else
8618dffb485Schristos 	    perror ("readchar");
8628dffb485Schristos 
8638dffb485Schristos 	  return -1;
8648dffb485Schristos 	}
8658dffb485Schristos 
8668dffb485Schristos       readchar_bufp = readchar_buf;
8678dffb485Schristos     }
8688dffb485Schristos 
8698dffb485Schristos   readchar_bufcnt--;
8708dffb485Schristos   ch = *readchar_bufp++;
8718dffb485Schristos   reschedule ();
8728dffb485Schristos   return ch;
8738dffb485Schristos }
8748dffb485Schristos 
8758dffb485Schristos /* Reset the readchar state machine.  */
8768dffb485Schristos 
8778dffb485Schristos static void
8788dffb485Schristos reset_readchar (void)
8798dffb485Schristos {
8808dffb485Schristos   readchar_bufcnt = 0;
8818dffb485Schristos   if (readchar_callback != NOT_SCHEDULED)
8828dffb485Schristos     {
8838dffb485Schristos       delete_timer (readchar_callback);
8848dffb485Schristos       readchar_callback = NOT_SCHEDULED;
8858dffb485Schristos     }
8868dffb485Schristos }
8878dffb485Schristos 
8888dffb485Schristos /* Process remaining data in readchar_buf.  */
8898dffb485Schristos 
8908dffb485Schristos static void
8918dffb485Schristos process_remaining (void *context)
8928dffb485Schristos {
8938dffb485Schristos   /* This is a one-shot event.  */
8948dffb485Schristos   readchar_callback = NOT_SCHEDULED;
8958dffb485Schristos 
8968dffb485Schristos   if (readchar_bufcnt > 0)
8978dffb485Schristos     handle_serial_event (0, NULL);
8988dffb485Schristos }
8998dffb485Schristos 
9008dffb485Schristos /* If there is still data in the buffer, queue another event to process it,
9018dffb485Schristos    we can't sleep in select yet.  */
9028dffb485Schristos 
9038dffb485Schristos static void
9048dffb485Schristos reschedule (void)
9058dffb485Schristos {
9068dffb485Schristos   if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
9078dffb485Schristos     readchar_callback = create_timer (0, process_remaining, NULL);
9088dffb485Schristos }
9098dffb485Schristos 
9108dffb485Schristos /* Read a packet from the remote machine, with error checking,
9118dffb485Schristos    and store it in BUF.  Returns length of packet, or negative if error. */
9128dffb485Schristos 
9138dffb485Schristos int
9148dffb485Schristos getpkt (char *buf)
9158dffb485Schristos {
9168dffb485Schristos   client_state &cs = get_client_state ();
9178dffb485Schristos   char *bp;
9188dffb485Schristos   unsigned char csum, c1, c2;
9198dffb485Schristos   int c;
9208dffb485Schristos 
9218dffb485Schristos   while (1)
9228dffb485Schristos     {
9238dffb485Schristos       csum = 0;
9248dffb485Schristos 
9258dffb485Schristos       while (1)
9268dffb485Schristos 	{
9278dffb485Schristos 	  c = readchar ();
9288dffb485Schristos 
9298dffb485Schristos 	  /* The '\003' may appear before or after each packet, so
9308dffb485Schristos 	     check for an input interrupt.  */
9318dffb485Schristos 	  if (c == '\003')
9328dffb485Schristos 	    {
9338dffb485Schristos 	      the_target->request_interrupt ();
9348dffb485Schristos 	      continue;
9358dffb485Schristos 	    }
9368dffb485Schristos 
9378dffb485Schristos 	  if (c == '$')
9388dffb485Schristos 	    break;
9394b169a6bSchristos 
9404b169a6bSchristos 	  remote_debug_printf ("[getpkt: discarding char '%c']", c);
9418dffb485Schristos 
9428dffb485Schristos 	  if (c < 0)
9438dffb485Schristos 	    return -1;
9448dffb485Schristos 	}
9458dffb485Schristos 
9468dffb485Schristos       bp = buf;
9478dffb485Schristos       while (1)
9488dffb485Schristos 	{
9498dffb485Schristos 	  c = readchar ();
9508dffb485Schristos 	  if (c < 0)
9518dffb485Schristos 	    return -1;
9528dffb485Schristos 	  if (c == '#')
9538dffb485Schristos 	    break;
9548dffb485Schristos 	  *bp++ = c;
9558dffb485Schristos 	  csum += c;
9568dffb485Schristos 	}
9578dffb485Schristos       *bp = 0;
9588dffb485Schristos 
9598dffb485Schristos       c1 = fromhex (readchar ());
9608dffb485Schristos       c2 = fromhex (readchar ());
9618dffb485Schristos 
9628dffb485Schristos       if (csum == (c1 << 4) + c2)
9638dffb485Schristos 	break;
9648dffb485Schristos 
9658dffb485Schristos       if (cs.noack_mode)
9668dffb485Schristos 	{
9678dffb485Schristos 	  fprintf (stderr,
9688dffb485Schristos 		   "Bad checksum, sentsum=0x%x, csum=0x%x, "
9698dffb485Schristos 		   "buf=%s [no-ack-mode, Bad medium?]\n",
9708dffb485Schristos 		   (c1 << 4) + c2, csum, buf);
9718dffb485Schristos 	  /* Not much we can do, GDB wasn't expecting an ack/nac.  */
9728dffb485Schristos 	  break;
9738dffb485Schristos 	}
9748dffb485Schristos 
9758dffb485Schristos       fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
9768dffb485Schristos 	       (c1 << 4) + c2, csum, buf);
9778dffb485Schristos       if (write_prim ("-", 1) != 1)
9788dffb485Schristos 	return -1;
9798dffb485Schristos     }
9808dffb485Schristos 
9818dffb485Schristos   if (!cs.noack_mode)
9828dffb485Schristos     {
9834b169a6bSchristos       remote_debug_printf ("getpkt (\"%s\");  [sending ack]", buf);
9848dffb485Schristos 
9858dffb485Schristos       if (write_prim ("+", 1) != 1)
9868dffb485Schristos 	return -1;
9878dffb485Schristos 
9884b169a6bSchristos       remote_debug_printf ("[sent ack]");
9898dffb485Schristos     }
9908dffb485Schristos   else
9914b169a6bSchristos     remote_debug_printf ("getpkt (\"%s\");  [no ack sent]", buf);
9928dffb485Schristos 
9938dffb485Schristos   /* The readchar above may have already read a '\003' out of the socket
9948dffb485Schristos      and moved it to the local buffer.  For example, when GDB sends
9958dffb485Schristos      vCont;c immediately followed by interrupt (see
9968dffb485Schristos      gdb.base/interrupt-noterm.exp).  As soon as we see the vCont;c, we'll
9978dffb485Schristos      resume the inferior and wait.  Since we've already moved the '\003'
9988dffb485Schristos      to the local buffer, SIGIO won't help.  In that case, if we don't
9998dffb485Schristos      check for interrupt after the vCont;c packet, the interrupt character
10008dffb485Schristos      would stay in the buffer unattended until after the next (unrelated)
10018dffb485Schristos      stop.  */
10028dffb485Schristos   while (readchar_bufcnt > 0 && *readchar_bufp == '\003')
10038dffb485Schristos     {
10048dffb485Schristos       /* Consume the interrupt character in the buffer.  */
10058dffb485Schristos       readchar ();
10068dffb485Schristos       the_target->request_interrupt ();
10078dffb485Schristos     }
10088dffb485Schristos 
10098dffb485Schristos   return bp - buf;
10108dffb485Schristos }
10118dffb485Schristos 
10128dffb485Schristos void
10138dffb485Schristos write_ok (char *buf)
10148dffb485Schristos {
10158dffb485Schristos   buf[0] = 'O';
10168dffb485Schristos   buf[1] = 'K';
10178dffb485Schristos   buf[2] = '\0';
10188dffb485Schristos }
10198dffb485Schristos 
10208dffb485Schristos void
10218dffb485Schristos write_enn (char *buf)
10228dffb485Schristos {
10238dffb485Schristos   /* Some day, we should define the meanings of the error codes... */
10248dffb485Schristos   buf[0] = 'E';
10258dffb485Schristos   buf[1] = '0';
10268dffb485Schristos   buf[2] = '1';
10278dffb485Schristos   buf[3] = '\0';
10288dffb485Schristos }
10298dffb485Schristos 
10308dffb485Schristos #endif
10318dffb485Schristos 
10328dffb485Schristos #ifndef IN_PROCESS_AGENT
10338dffb485Schristos 
10348dffb485Schristos static char *
10358dffb485Schristos outreg (struct regcache *regcache, int regno, char *buf)
10368dffb485Schristos {
10378dffb485Schristos   if ((regno >> 12) != 0)
10388dffb485Schristos     *buf++ = tohex ((regno >> 12) & 0xf);
10398dffb485Schristos   if ((regno >> 8) != 0)
10408dffb485Schristos     *buf++ = tohex ((regno >> 8) & 0xf);
10418dffb485Schristos   *buf++ = tohex ((regno >> 4) & 0xf);
10428dffb485Schristos   *buf++ = tohex (regno & 0xf);
10438dffb485Schristos   *buf++ = ':';
10448dffb485Schristos   collect_register_as_string (regcache, regno, buf);
10458dffb485Schristos   buf += 2 * register_size (regcache->tdesc, regno);
10468dffb485Schristos   *buf++ = ';';
10478dffb485Schristos 
10488dffb485Schristos   return buf;
10498dffb485Schristos }
10508dffb485Schristos 
10518dffb485Schristos void
10524b169a6bSchristos prepare_resume_reply (char *buf, ptid_t ptid, const target_waitstatus &status)
10538dffb485Schristos {
10548dffb485Schristos   client_state &cs = get_client_state ();
1055*64f917f5Schristos   threads_debug_printf ("Writing resume reply for %s: %s",
1056*64f917f5Schristos 			target_pid_to_str (ptid).c_str (),
1057*64f917f5Schristos 			status.to_string ().c_str ());
10588dffb485Schristos 
10594b169a6bSchristos   switch (status.kind ())
10608dffb485Schristos     {
10618dffb485Schristos     case TARGET_WAITKIND_STOPPED:
10628dffb485Schristos     case TARGET_WAITKIND_FORKED:
10638dffb485Schristos     case TARGET_WAITKIND_VFORKED:
10648dffb485Schristos     case TARGET_WAITKIND_VFORK_DONE:
1065*64f917f5Schristos     case TARGET_WAITKIND_THREAD_CLONED:
10668dffb485Schristos     case TARGET_WAITKIND_EXECD:
10678dffb485Schristos     case TARGET_WAITKIND_THREAD_CREATED:
10688dffb485Schristos     case TARGET_WAITKIND_SYSCALL_ENTRY:
10698dffb485Schristos     case TARGET_WAITKIND_SYSCALL_RETURN:
10708dffb485Schristos       {
10718dffb485Schristos 	struct regcache *regcache;
10724b169a6bSchristos 	char *buf_start = buf;
10738dffb485Schristos 
1074*64f917f5Schristos 	if ((status.kind () == TARGET_WAITKIND_FORKED
1075*64f917f5Schristos 	     && cs.report_fork_events)
10764b169a6bSchristos 	    || (status.kind () == TARGET_WAITKIND_VFORKED
1077*64f917f5Schristos 		&& cs.report_vfork_events)
1078*64f917f5Schristos 	    || status.kind () == TARGET_WAITKIND_THREAD_CLONED)
10798dffb485Schristos 	  {
10808dffb485Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1081*64f917f5Schristos 
1082*64f917f5Schristos 	    auto kind_remote_str = [] (target_waitkind kind)
1083*64f917f5Schristos 	    {
1084*64f917f5Schristos 	      switch (kind)
1085*64f917f5Schristos 		{
1086*64f917f5Schristos 		case TARGET_WAITKIND_FORKED:
1087*64f917f5Schristos 		  return "fork";
1088*64f917f5Schristos 		case TARGET_WAITKIND_VFORKED:
1089*64f917f5Schristos 		  return "vfork";
1090*64f917f5Schristos 		case TARGET_WAITKIND_THREAD_CLONED:
1091*64f917f5Schristos 		  return "clone";
1092*64f917f5Schristos 		default:
1093*64f917f5Schristos 		  gdb_assert_not_reached ("unhandled kind");
1094*64f917f5Schristos 		}
1095*64f917f5Schristos 	    };
1096*64f917f5Schristos 
1097*64f917f5Schristos 	    const char *event = kind_remote_str (status.kind ());
10988dffb485Schristos 
10998dffb485Schristos 	    sprintf (buf, "T%02x%s:", signal, event);
11008dffb485Schristos 	    buf += strlen (buf);
11014b169a6bSchristos 	    buf = write_ptid (buf, status.child_ptid ());
11028dffb485Schristos 	    strcat (buf, ";");
11038dffb485Schristos 	  }
11044b169a6bSchristos 	else if (status.kind () == TARGET_WAITKIND_VFORK_DONE
11058dffb485Schristos 		 && cs.report_vfork_events)
11068dffb485Schristos 	  {
11078dffb485Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
11088dffb485Schristos 
11098dffb485Schristos 	    sprintf (buf, "T%02xvforkdone:;", signal);
11108dffb485Schristos 	  }
11114b169a6bSchristos 	else if (status.kind () == TARGET_WAITKIND_EXECD && cs.report_exec_events)
11128dffb485Schristos 	  {
11138dffb485Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
11148dffb485Schristos 	    const char *event = "exec";
11158dffb485Schristos 	    char hexified_pathname[PATH_MAX * 2];
11168dffb485Schristos 
11178dffb485Schristos 	    sprintf (buf, "T%02x%s:", signal, event);
11188dffb485Schristos 	    buf += strlen (buf);
11198dffb485Schristos 
11208dffb485Schristos 	    /* Encode pathname to hexified format.  */
11214b169a6bSchristos 	    bin2hex ((const gdb_byte *) status.execd_pathname (),
11228dffb485Schristos 		     hexified_pathname,
11234b169a6bSchristos 		     strlen (status.execd_pathname ()));
11248dffb485Schristos 
11258dffb485Schristos 	    sprintf (buf, "%s;", hexified_pathname);
11268dffb485Schristos 	    buf += strlen (buf);
11278dffb485Schristos 	  }
11284b169a6bSchristos 	else if (status.kind () == TARGET_WAITKIND_THREAD_CREATED
11298dffb485Schristos 		 && cs.report_thread_events)
11308dffb485Schristos 	  {
11318dffb485Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
11328dffb485Schristos 
11338dffb485Schristos 	    sprintf (buf, "T%02xcreate:;", signal);
11348dffb485Schristos 	  }
11354b169a6bSchristos 	else if (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
11364b169a6bSchristos 		 || status.kind () == TARGET_WAITKIND_SYSCALL_RETURN)
11378dffb485Schristos 	  {
11388dffb485Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
11394b169a6bSchristos 	    const char *event = (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
11408dffb485Schristos 				 ? "syscall_entry" : "syscall_return");
11418dffb485Schristos 
11428dffb485Schristos 	    sprintf (buf, "T%02x%s:%x;", signal, event,
11434b169a6bSchristos 		     status.syscall_number ());
11448dffb485Schristos 	  }
11458dffb485Schristos 	else
11464b169a6bSchristos 	  sprintf (buf, "T%02x", status.sig ());
11478dffb485Schristos 
11488dffb485Schristos 	if (disable_packet_T)
11498dffb485Schristos 	  {
11508dffb485Schristos 	    /* This is a bit (OK, a lot) of a kludge, however, this isn't
11518dffb485Schristos 	       really a user feature, but exists only so GDB can use the
11528dffb485Schristos 	       gdbserver to test handling of the 'S' stop reply packet, so
11538dffb485Schristos 	       we would rather this code be as simple as possible.
11548dffb485Schristos 
11558dffb485Schristos 	       By this point we've started to build the 'T' stop packet,
11568dffb485Schristos 	       and it should look like 'Txx....' where 'x' is a hex digit.
11578dffb485Schristos 	       An 'S' stop packet always looks like 'Sxx', so all we do
11588dffb485Schristos 	       here is convert the buffer from a T packet to an S packet
11598dffb485Schristos 	       and the avoid adding any extra content by breaking out.  */
11604b169a6bSchristos 	    gdb_assert (buf_start[0] == 'T');
11614b169a6bSchristos 	    gdb_assert (isxdigit (buf_start[1]));
11624b169a6bSchristos 	    gdb_assert (isxdigit (buf_start[2]));
11634b169a6bSchristos 	    buf_start[0] = 'S';
11644b169a6bSchristos 	    buf_start[3] = '\0';
11658dffb485Schristos 	    break;
11668dffb485Schristos 	  }
11678dffb485Schristos 
11688dffb485Schristos 	buf += strlen (buf);
11698dffb485Schristos 
11704b169a6bSchristos 	scoped_restore_current_thread restore_thread;
11718dffb485Schristos 
11728dffb485Schristos 	switch_to_thread (the_target, ptid);
11738dffb485Schristos 
11748dffb485Schristos 	regcache = get_thread_regcache (current_thread, 1);
11758dffb485Schristos 
11768dffb485Schristos 	if (the_target->stopped_by_watchpoint ())
11778dffb485Schristos 	  {
11788dffb485Schristos 	    CORE_ADDR addr;
11798dffb485Schristos 	    int i;
11808dffb485Schristos 
11818dffb485Schristos 	    memcpy (buf, "watch:", 6);
11828dffb485Schristos 	    buf += 6;
11838dffb485Schristos 
11848dffb485Schristos 	    addr = the_target->stopped_data_address ();
11858dffb485Schristos 
11868dffb485Schristos 	    /* Convert each byte of the address into two hexadecimal
11878dffb485Schristos 	       chars.  Note that we take sizeof (void *) instead of
11888dffb485Schristos 	       sizeof (addr); this is to avoid sending a 64-bit
11898dffb485Schristos 	       address to a 32-bit GDB.  */
11908dffb485Schristos 	    for (i = sizeof (void *) * 2; i > 0; i--)
11918dffb485Schristos 	      *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
11928dffb485Schristos 	    *buf++ = ';';
11938dffb485Schristos 	  }
11948dffb485Schristos 	else if (cs.swbreak_feature && target_stopped_by_sw_breakpoint ())
11958dffb485Schristos 	  {
11968dffb485Schristos 	    sprintf (buf, "swbreak:;");
11978dffb485Schristos 	    buf += strlen (buf);
11988dffb485Schristos 	  }
11998dffb485Schristos 	else if (cs.hwbreak_feature && target_stopped_by_hw_breakpoint ())
12008dffb485Schristos 	  {
12018dffb485Schristos 	    sprintf (buf, "hwbreak:;");
12028dffb485Schristos 	    buf += strlen (buf);
12038dffb485Schristos 	  }
12048dffb485Schristos 
1205*64f917f5Schristos 	/* Handle the expedited registers.  */
1206*64f917f5Schristos 	for (const std::string &expedited_reg :
1207*64f917f5Schristos 	     current_target_desc ()->expedite_regs)
1208*64f917f5Schristos 	  buf = outreg (regcache, find_regno (regcache->tdesc,
1209*64f917f5Schristos 					      expedited_reg.c_str ()), buf);
12108dffb485Schristos 	*buf = '\0';
12118dffb485Schristos 
12128dffb485Schristos 	/* Formerly, if the debugger had not used any thread features
12138dffb485Schristos 	   we would not burden it with a thread status response.  This
12148dffb485Schristos 	   was for the benefit of GDB 4.13 and older.  However, in
12158dffb485Schristos 	   recent GDB versions the check (``if (cont_thread != 0)'')
1216*64f917f5Schristos 	   does not have the desired effect because of silliness in
12178dffb485Schristos 	   the way that the remote protocol handles specifying a
12188dffb485Schristos 	   thread.  Since thread support relies on qSymbol support
12198dffb485Schristos 	   anyway, assume GDB can handle threads.  */
12208dffb485Schristos 
12218dffb485Schristos 	if (using_threads && !disable_packet_Tthread)
12228dffb485Schristos 	  {
12238dffb485Schristos 	    /* This if (1) ought to be unnecessary.  But remote_wait
12248dffb485Schristos 	       in GDB will claim this event belongs to inferior_ptid
12258dffb485Schristos 	       if we do not specify a thread, and there's no way for
12268dffb485Schristos 	       gdbserver to know what inferior_ptid is.  */
12278dffb485Schristos 	    if (1 || cs.general_thread != ptid)
12288dffb485Schristos 	      {
12298dffb485Schristos 		int core = -1;
12308dffb485Schristos 		/* In non-stop, don't change the general thread behind
12318dffb485Schristos 		   GDB's back.  */
12328dffb485Schristos 		if (!non_stop)
12338dffb485Schristos 		  cs.general_thread = ptid;
12348dffb485Schristos 		sprintf (buf, "thread:");
12358dffb485Schristos 		buf += strlen (buf);
12368dffb485Schristos 		buf = write_ptid (buf, ptid);
12378dffb485Schristos 		strcat (buf, ";");
12388dffb485Schristos 		buf += strlen (buf);
12398dffb485Schristos 
12408dffb485Schristos 		core = target_core_of_thread (ptid);
12418dffb485Schristos 
12428dffb485Schristos 		if (core != -1)
12438dffb485Schristos 		  {
12448dffb485Schristos 		    sprintf (buf, "core:");
12458dffb485Schristos 		    buf += strlen (buf);
12468dffb485Schristos 		    sprintf (buf, "%x", core);
12478dffb485Schristos 		    strcat (buf, ";");
12488dffb485Schristos 		    buf += strlen (buf);
12498dffb485Schristos 		  }
12508dffb485Schristos 	      }
12518dffb485Schristos 	  }
12528dffb485Schristos 
12534b169a6bSchristos 	if (current_process ()->dlls_changed)
12548dffb485Schristos 	  {
12558dffb485Schristos 	    strcpy (buf, "library:;");
12568dffb485Schristos 	    buf += strlen (buf);
12574b169a6bSchristos 	    current_process ()->dlls_changed = false;
12588dffb485Schristos 	  }
12598dffb485Schristos       }
12608dffb485Schristos       break;
12618dffb485Schristos     case TARGET_WAITKIND_EXITED:
12628dffb485Schristos       if (cs.multi_process)
12638dffb485Schristos 	sprintf (buf, "W%x;process:%x",
12644b169a6bSchristos 		 status.exit_status (), ptid.pid ());
12658dffb485Schristos       else
12664b169a6bSchristos 	sprintf (buf, "W%02x", status.exit_status ());
12678dffb485Schristos       break;
12688dffb485Schristos     case TARGET_WAITKIND_SIGNALLED:
12698dffb485Schristos       if (cs.multi_process)
12708dffb485Schristos 	sprintf (buf, "X%x;process:%x",
12714b169a6bSchristos 		 status.sig (), ptid.pid ());
12728dffb485Schristos       else
12734b169a6bSchristos 	sprintf (buf, "X%02x", status.sig ());
12748dffb485Schristos       break;
12758dffb485Schristos     case TARGET_WAITKIND_THREAD_EXITED:
12764b169a6bSchristos       sprintf (buf, "w%x;", status.exit_status ());
12778dffb485Schristos       buf += strlen (buf);
12788dffb485Schristos       buf = write_ptid (buf, ptid);
12798dffb485Schristos       break;
12808dffb485Schristos     case TARGET_WAITKIND_NO_RESUMED:
12818dffb485Schristos       sprintf (buf, "N");
12828dffb485Schristos       break;
12838dffb485Schristos     default:
12848dffb485Schristos       error ("unhandled waitkind");
12858dffb485Schristos       break;
12868dffb485Schristos     }
12878dffb485Schristos }
12888dffb485Schristos 
12894b169a6bSchristos /* See remote-utils.h.  */
12908dffb485Schristos 
12914b169a6bSchristos const char *
12924b169a6bSchristos decode_m_packet_params (const char *from, CORE_ADDR *mem_addr_ptr,
12934b169a6bSchristos 			unsigned int *len_ptr, const char end_marker)
12948dffb485Schristos {
12958dffb485Schristos   int i = 0;
12968dffb485Schristos   char ch;
12978dffb485Schristos   *mem_addr_ptr = *len_ptr = 0;
12988dffb485Schristos 
12998dffb485Schristos   while ((ch = from[i++]) != ',')
13008dffb485Schristos     {
13018dffb485Schristos       *mem_addr_ptr = *mem_addr_ptr << 4;
13028dffb485Schristos       *mem_addr_ptr |= fromhex (ch) & 0x0f;
13038dffb485Schristos     }
13048dffb485Schristos 
13054b169a6bSchristos   while ((ch = from[i++]) != end_marker)
13068dffb485Schristos     {
13078dffb485Schristos       *len_ptr = *len_ptr << 4;
13088dffb485Schristos       *len_ptr |= fromhex (ch) & 0x0f;
13098dffb485Schristos     }
13108dffb485Schristos 
13114b169a6bSchristos   return from + i;
13124b169a6bSchristos }
13134b169a6bSchristos 
13144b169a6bSchristos void
13154b169a6bSchristos decode_m_packet (const char *from, CORE_ADDR *mem_addr_ptr,
13164b169a6bSchristos 		 unsigned int *len_ptr)
13174b169a6bSchristos {
13184b169a6bSchristos   decode_m_packet_params (from, mem_addr_ptr, len_ptr, '\0');
13194b169a6bSchristos }
13204b169a6bSchristos 
13214b169a6bSchristos void
13224b169a6bSchristos decode_M_packet (const char *from, CORE_ADDR *mem_addr_ptr,
13234b169a6bSchristos 		 unsigned int *len_ptr, unsigned char **to_p)
13244b169a6bSchristos {
13254b169a6bSchristos   from = decode_m_packet_params (from, mem_addr_ptr, len_ptr, ':');
13264b169a6bSchristos 
13278dffb485Schristos   if (*to_p == NULL)
13288dffb485Schristos     *to_p = (unsigned char *) xmalloc (*len_ptr);
13298dffb485Schristos 
13304b169a6bSchristos   hex2bin (from, *to_p, *len_ptr);
13318dffb485Schristos }
13328dffb485Schristos 
13338dffb485Schristos int
13348dffb485Schristos decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
13358dffb485Schristos 		 unsigned int *len_ptr, unsigned char **to_p)
13368dffb485Schristos {
13378dffb485Schristos   int i = 0;
13388dffb485Schristos   char ch;
13398dffb485Schristos   *mem_addr_ptr = *len_ptr = 0;
13408dffb485Schristos 
13418dffb485Schristos   while ((ch = from[i++]) != ',')
13428dffb485Schristos     {
13438dffb485Schristos       *mem_addr_ptr = *mem_addr_ptr << 4;
13448dffb485Schristos       *mem_addr_ptr |= fromhex (ch) & 0x0f;
13458dffb485Schristos     }
13468dffb485Schristos 
13478dffb485Schristos   while ((ch = from[i++]) != ':')
13488dffb485Schristos     {
13498dffb485Schristos       *len_ptr = *len_ptr << 4;
13508dffb485Schristos       *len_ptr |= fromhex (ch) & 0x0f;
13518dffb485Schristos     }
13528dffb485Schristos 
13538dffb485Schristos   if (*to_p == NULL)
13548dffb485Schristos     *to_p = (unsigned char *) xmalloc (*len_ptr);
13558dffb485Schristos 
13568dffb485Schristos   if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
13578dffb485Schristos 			     *to_p, *len_ptr) != *len_ptr)
13588dffb485Schristos     return -1;
13598dffb485Schristos 
13608dffb485Schristos   return 0;
13618dffb485Schristos }
13628dffb485Schristos 
13638dffb485Schristos /* Decode a qXfer write request.  */
13648dffb485Schristos 
13658dffb485Schristos int
13668dffb485Schristos decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
13678dffb485Schristos 		   unsigned int *len, unsigned char *data)
13688dffb485Schristos {
13698dffb485Schristos   char ch;
13708dffb485Schristos   char *b = buf;
13718dffb485Schristos 
13728dffb485Schristos   /* Extract the offset.  */
13738dffb485Schristos   *offset = 0;
13748dffb485Schristos   while ((ch = *buf++) != ':')
13758dffb485Schristos     {
13768dffb485Schristos       *offset = *offset << 4;
13778dffb485Schristos       *offset |= fromhex (ch) & 0x0f;
13788dffb485Schristos     }
13798dffb485Schristos 
13808dffb485Schristos   /* Get encoded data.  */
13818dffb485Schristos   packet_len -= buf - b;
13828dffb485Schristos   *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
13838dffb485Schristos 				data, packet_len);
13848dffb485Schristos   return 0;
13858dffb485Schristos }
13868dffb485Schristos 
13878dffb485Schristos /* Decode the parameters of a qSearch:memory packet.  */
13888dffb485Schristos 
13898dffb485Schristos int
13908dffb485Schristos decode_search_memory_packet (const char *buf, int packet_len,
13918dffb485Schristos 			     CORE_ADDR *start_addrp,
13928dffb485Schristos 			     CORE_ADDR *search_space_lenp,
13938dffb485Schristos 			     gdb_byte *pattern, unsigned int *pattern_lenp)
13948dffb485Schristos {
13958dffb485Schristos   const char *p = buf;
13968dffb485Schristos 
13978dffb485Schristos   p = decode_address_to_semicolon (start_addrp, p);
13988dffb485Schristos   p = decode_address_to_semicolon (search_space_lenp, p);
13998dffb485Schristos   packet_len -= p - buf;
14008dffb485Schristos   *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
14018dffb485Schristos 					 pattern, packet_len);
14028dffb485Schristos   return 0;
14038dffb485Schristos }
14048dffb485Schristos 
14058dffb485Schristos static void
14068dffb485Schristos free_sym_cache (struct sym_cache *sym)
14078dffb485Schristos {
14088dffb485Schristos   if (sym != NULL)
14098dffb485Schristos     {
14108dffb485Schristos       free (sym->name);
14118dffb485Schristos       free (sym);
14128dffb485Schristos     }
14138dffb485Schristos }
14148dffb485Schristos 
14158dffb485Schristos void
14168dffb485Schristos clear_symbol_cache (struct sym_cache **symcache_p)
14178dffb485Schristos {
14188dffb485Schristos   struct sym_cache *sym, *next;
14198dffb485Schristos 
14208dffb485Schristos   /* Check the cache first.  */
14218dffb485Schristos   for (sym = *symcache_p; sym; sym = next)
14228dffb485Schristos     {
14238dffb485Schristos       next = sym->next;
14248dffb485Schristos       free_sym_cache (sym);
14258dffb485Schristos     }
14268dffb485Schristos 
14278dffb485Schristos   *symcache_p = NULL;
14288dffb485Schristos }
14298dffb485Schristos 
14308dffb485Schristos /* Get the address of NAME, and return it in ADDRP if found.  if
14318dffb485Schristos    MAY_ASK_GDB is false, assume symbol cache misses are failures.
14328dffb485Schristos    Returns 1 if the symbol is found, 0 if it is not, -1 on error.  */
14338dffb485Schristos 
14348dffb485Schristos int
14358dffb485Schristos look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
14368dffb485Schristos {
14378dffb485Schristos   client_state &cs = get_client_state ();
14388dffb485Schristos   char *p, *q;
14398dffb485Schristos   int len;
14408dffb485Schristos   struct sym_cache *sym;
14418dffb485Schristos   struct process_info *proc;
14428dffb485Schristos 
14438dffb485Schristos   proc = current_process ();
14448dffb485Schristos 
14458dffb485Schristos   /* Check the cache first.  */
14468dffb485Schristos   for (sym = proc->symbol_cache; sym; sym = sym->next)
14478dffb485Schristos     if (strcmp (name, sym->name) == 0)
14488dffb485Schristos       {
14498dffb485Schristos 	*addrp = sym->addr;
14508dffb485Schristos 	return 1;
14518dffb485Schristos       }
14528dffb485Schristos 
14538dffb485Schristos   /* It might not be an appropriate time to look up a symbol,
14548dffb485Schristos      e.g. while we're trying to fetch registers.  */
14558dffb485Schristos   if (!may_ask_gdb)
14568dffb485Schristos     return 0;
14578dffb485Schristos 
14588dffb485Schristos   /* Send the request.  */
14598dffb485Schristos   strcpy (cs.own_buf, "qSymbol:");
14608dffb485Schristos   bin2hex ((const gdb_byte *) name, cs.own_buf + strlen ("qSymbol:"),
14618dffb485Schristos 	  strlen (name));
14628dffb485Schristos   if (putpkt (cs.own_buf) < 0)
14638dffb485Schristos     return -1;
14648dffb485Schristos 
14658dffb485Schristos   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
14668dffb485Schristos   len = getpkt (cs.own_buf);
14678dffb485Schristos   if (len < 0)
14688dffb485Schristos     return -1;
14698dffb485Schristos 
14708dffb485Schristos   /* We ought to handle pretty much any packet at this point while we
14718dffb485Schristos      wait for the qSymbol "response".  That requires re-entering the
14728dffb485Schristos      main loop.  For now, this is an adequate approximation; allow
14738dffb485Schristos      GDB to read from memory and handle 'v' packets (for vFile transfers)
14748dffb485Schristos      while it figures out the address of the symbol.  */
14758dffb485Schristos   while (1)
14768dffb485Schristos     {
14778dffb485Schristos       if (cs.own_buf[0] == 'm')
14788dffb485Schristos 	{
14798dffb485Schristos 	  CORE_ADDR mem_addr;
14808dffb485Schristos 	  unsigned char *mem_buf;
14818dffb485Schristos 	  unsigned int mem_len;
14828dffb485Schristos 
14838dffb485Schristos 	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
14848dffb485Schristos 	  mem_buf = (unsigned char *) xmalloc (mem_len);
14858dffb485Schristos 	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
14868dffb485Schristos 	    bin2hex (mem_buf, cs.own_buf, mem_len);
14878dffb485Schristos 	  else
14888dffb485Schristos 	    write_enn (cs.own_buf);
14898dffb485Schristos 	  free (mem_buf);
14908dffb485Schristos 	  if (putpkt (cs.own_buf) < 0)
14918dffb485Schristos 	    return -1;
14928dffb485Schristos 	}
14938dffb485Schristos       else if (cs.own_buf[0] == 'v')
14948dffb485Schristos 	{
14958dffb485Schristos 	  int new_len = -1;
14968dffb485Schristos 	  handle_v_requests (cs.own_buf, len, &new_len);
14978dffb485Schristos 	  if (new_len != -1)
14988dffb485Schristos 	    putpkt_binary (cs.own_buf, new_len);
14998dffb485Schristos 	  else
15008dffb485Schristos 	    putpkt (cs.own_buf);
15018dffb485Schristos 	}
15028dffb485Schristos       else
15038dffb485Schristos 	break;
15048dffb485Schristos       len = getpkt (cs.own_buf);
15058dffb485Schristos       if (len < 0)
15068dffb485Schristos 	return -1;
15078dffb485Schristos     }
15088dffb485Schristos 
15098dffb485Schristos   if (!startswith (cs.own_buf, "qSymbol:"))
15108dffb485Schristos     {
15118dffb485Schristos       warning ("Malformed response to qSymbol, ignoring: %s", cs.own_buf);
15128dffb485Schristos       return -1;
15138dffb485Schristos     }
15148dffb485Schristos 
15158dffb485Schristos   p = cs.own_buf + strlen ("qSymbol:");
15168dffb485Schristos   q = p;
15178dffb485Schristos   while (*q && *q != ':')
15188dffb485Schristos     q++;
15198dffb485Schristos 
15208dffb485Schristos   /* Make sure we found a value for the symbol.  */
15218dffb485Schristos   if (p == q || *q == '\0')
15228dffb485Schristos     return 0;
15238dffb485Schristos 
15248dffb485Schristos   decode_address (addrp, p, q - p);
15258dffb485Schristos 
15268dffb485Schristos   /* Save the symbol in our cache.  */
15278dffb485Schristos   sym = XNEW (struct sym_cache);
15288dffb485Schristos   sym->name = xstrdup (name);
15298dffb485Schristos   sym->addr = *addrp;
15308dffb485Schristos   sym->next = proc->symbol_cache;
15318dffb485Schristos   proc->symbol_cache = sym;
15328dffb485Schristos 
15338dffb485Schristos   return 1;
15348dffb485Schristos }
15358dffb485Schristos 
15368dffb485Schristos /* Relocate an instruction to execute at a different address.  OLDLOC
15378dffb485Schristos    is the address in the inferior memory where the instruction to
15388dffb485Schristos    relocate is currently at.  On input, TO points to the destination
15398dffb485Schristos    where we want the instruction to be copied (and possibly adjusted)
15408dffb485Schristos    to.  On output, it points to one past the end of the resulting
15418dffb485Schristos    instruction(s).  The effect of executing the instruction at TO
15428dffb485Schristos    shall be the same as if executing it at OLDLOC.  For example, call
15438dffb485Schristos    instructions that implicitly push the return address on the stack
15448dffb485Schristos    should be adjusted to return to the instruction after OLDLOC;
15458dffb485Schristos    relative branches, and other PC-relative instructions need the
15468dffb485Schristos    offset adjusted; etc.  Returns 0 on success, -1 on failure.  */
15478dffb485Schristos 
15488dffb485Schristos int
15498dffb485Schristos relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
15508dffb485Schristos {
15518dffb485Schristos   client_state &cs = get_client_state ();
15528dffb485Schristos   int len;
15538dffb485Schristos   ULONGEST written = 0;
15548dffb485Schristos 
15558dffb485Schristos   /* Send the request.  */
15568dffb485Schristos   sprintf (cs.own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
15578dffb485Schristos 	   paddress (*to));
15588dffb485Schristos   if (putpkt (cs.own_buf) < 0)
15598dffb485Schristos     return -1;
15608dffb485Schristos 
15618dffb485Schristos   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
15628dffb485Schristos   len = getpkt (cs.own_buf);
15638dffb485Schristos   if (len < 0)
15648dffb485Schristos     return -1;
15658dffb485Schristos 
15668dffb485Schristos   /* We ought to handle pretty much any packet at this point while we
15678dffb485Schristos      wait for the qRelocInsn "response".  That requires re-entering
15688dffb485Schristos      the main loop.  For now, this is an adequate approximation; allow
15698dffb485Schristos      GDB to access memory.  */
15708dffb485Schristos   while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M' || cs.own_buf[0] == 'X')
15718dffb485Schristos     {
15728dffb485Schristos       CORE_ADDR mem_addr;
15738dffb485Schristos       unsigned char *mem_buf = NULL;
15748dffb485Schristos       unsigned int mem_len;
15758dffb485Schristos 
15768dffb485Schristos       if (cs.own_buf[0] == 'm')
15778dffb485Schristos 	{
15788dffb485Schristos 	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
15798dffb485Schristos 	  mem_buf = (unsigned char *) xmalloc (mem_len);
15808dffb485Schristos 	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
15818dffb485Schristos 	    bin2hex (mem_buf, cs.own_buf, mem_len);
15828dffb485Schristos 	  else
15838dffb485Schristos 	    write_enn (cs.own_buf);
15848dffb485Schristos 	}
15858dffb485Schristos       else if (cs.own_buf[0] == 'X')
15868dffb485Schristos 	{
15878dffb485Schristos 	  if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
15888dffb485Schristos 			       &mem_len, &mem_buf) < 0
15898dffb485Schristos 	      || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
15908dffb485Schristos 	    write_enn (cs.own_buf);
15918dffb485Schristos 	  else
15928dffb485Schristos 	    write_ok (cs.own_buf);
15938dffb485Schristos 	}
15948dffb485Schristos       else
15958dffb485Schristos 	{
15968dffb485Schristos 	  decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
15978dffb485Schristos 	  if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
15988dffb485Schristos 	    write_ok (cs.own_buf);
15998dffb485Schristos 	  else
16008dffb485Schristos 	    write_enn (cs.own_buf);
16018dffb485Schristos 	}
16028dffb485Schristos       free (mem_buf);
16038dffb485Schristos       if (putpkt (cs.own_buf) < 0)
16048dffb485Schristos 	return -1;
16058dffb485Schristos       len = getpkt (cs.own_buf);
16068dffb485Schristos       if (len < 0)
16078dffb485Schristos 	return -1;
16088dffb485Schristos     }
16098dffb485Schristos 
16108dffb485Schristos   if (cs.own_buf[0] == 'E')
16118dffb485Schristos     {
16128dffb485Schristos       warning ("An error occurred while relocating an instruction: %s",
16138dffb485Schristos 	       cs.own_buf);
16148dffb485Schristos       return -1;
16158dffb485Schristos     }
16168dffb485Schristos 
16178dffb485Schristos   if (!startswith (cs.own_buf, "qRelocInsn:"))
16188dffb485Schristos     {
16198dffb485Schristos       warning ("Malformed response to qRelocInsn, ignoring: %s",
16208dffb485Schristos 	       cs.own_buf);
16218dffb485Schristos       return -1;
16228dffb485Schristos     }
16238dffb485Schristos 
16248dffb485Schristos   unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
16258dffb485Schristos 
16268dffb485Schristos   *to += written;
16278dffb485Schristos   return 0;
16288dffb485Schristos }
16298dffb485Schristos 
16308dffb485Schristos void
16318dffb485Schristos monitor_output (const char *msg)
16328dffb485Schristos {
16338dffb485Schristos   int len = strlen (msg);
16348dffb485Schristos   char *buf = (char *) xmalloc (len * 2 + 2);
16358dffb485Schristos 
16368dffb485Schristos   buf[0] = 'O';
16378dffb485Schristos   bin2hex ((const gdb_byte *) msg, buf + 1, len);
16388dffb485Schristos 
16398dffb485Schristos   putpkt (buf);
16408dffb485Schristos   free (buf);
16418dffb485Schristos }
16428dffb485Schristos 
16438dffb485Schristos #endif
1644