1*56bb7041Schristos /* Remote utility routines for the remote server for GDB.
2*56bb7041Schristos    Copyright (C) 1986-2020 Free Software Foundation, Inc.
3*56bb7041Schristos 
4*56bb7041Schristos    This file is part of GDB.
5*56bb7041Schristos 
6*56bb7041Schristos    This program is free software; you can redistribute it and/or modify
7*56bb7041Schristos    it under the terms of the GNU General Public License as published by
8*56bb7041Schristos    the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos    (at your option) any later version.
10*56bb7041Schristos 
11*56bb7041Schristos    This program is distributed in the hope that it will be useful,
12*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*56bb7041Schristos    GNU General Public License for more details.
15*56bb7041Schristos 
16*56bb7041Schristos    You should have received a copy of the GNU General Public License
17*56bb7041Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18*56bb7041Schristos 
19*56bb7041Schristos #include "server.h"
20*56bb7041Schristos #if HAVE_TERMIOS_H
21*56bb7041Schristos #include <termios.h>
22*56bb7041Schristos #endif
23*56bb7041Schristos #include "target.h"
24*56bb7041Schristos #include "gdbthread.h"
25*56bb7041Schristos #include "tdesc.h"
26*56bb7041Schristos #include "debug.h"
27*56bb7041Schristos #include "dll.h"
28*56bb7041Schristos #include "gdbsupport/rsp-low.h"
29*56bb7041Schristos #include "gdbsupport/netstuff.h"
30*56bb7041Schristos #include "gdbsupport/filestuff.h"
31*56bb7041Schristos #include "gdbsupport/gdb-sigmask.h"
32*56bb7041Schristos #include <ctype.h>
33*56bb7041Schristos #if HAVE_SYS_IOCTL_H
34*56bb7041Schristos #include <sys/ioctl.h>
35*56bb7041Schristos #endif
36*56bb7041Schristos #if HAVE_SYS_FILE_H
37*56bb7041Schristos #include <sys/file.h>
38*56bb7041Schristos #endif
39*56bb7041Schristos #if HAVE_NETINET_IN_H
40*56bb7041Schristos #include <netinet/in.h>
41*56bb7041Schristos #endif
42*56bb7041Schristos #if HAVE_SYS_SOCKET_H
43*56bb7041Schristos #include <sys/socket.h>
44*56bb7041Schristos #endif
45*56bb7041Schristos #if HAVE_NETDB_H
46*56bb7041Schristos #include <netdb.h>
47*56bb7041Schristos #endif
48*56bb7041Schristos #if HAVE_NETINET_TCP_H
49*56bb7041Schristos #include <netinet/tcp.h>
50*56bb7041Schristos #endif
51*56bb7041Schristos #if HAVE_SYS_IOCTL_H
52*56bb7041Schristos #include <sys/ioctl.h>
53*56bb7041Schristos #endif
54*56bb7041Schristos #if HAVE_SIGNAL_H
55*56bb7041Schristos #include <signal.h>
56*56bb7041Schristos #endif
57*56bb7041Schristos #if HAVE_FCNTL_H
58*56bb7041Schristos #include <fcntl.h>
59*56bb7041Schristos #endif
60*56bb7041Schristos #include "gdbsupport/gdb_sys_time.h"
61*56bb7041Schristos #include <unistd.h>
62*56bb7041Schristos #if HAVE_ARPA_INET_H
63*56bb7041Schristos #include <arpa/inet.h>
64*56bb7041Schristos #endif
65*56bb7041Schristos #include <sys/stat.h>
66*56bb7041Schristos 
67*56bb7041Schristos #if USE_WIN32API
68*56bb7041Schristos #include <ws2tcpip.h>
69*56bb7041Schristos #endif
70*56bb7041Schristos 
71*56bb7041Schristos #ifndef HAVE_SOCKLEN_T
72*56bb7041Schristos typedef int socklen_t;
73*56bb7041Schristos #endif
74*56bb7041Schristos 
75*56bb7041Schristos #ifndef IN_PROCESS_AGENT
76*56bb7041Schristos 
77*56bb7041Schristos /* Extra value for readchar_callback.  */
78*56bb7041Schristos enum {
79*56bb7041Schristos   /* The callback is currently not scheduled.  */
80*56bb7041Schristos   NOT_SCHEDULED = -1
81*56bb7041Schristos };
82*56bb7041Schristos 
83*56bb7041Schristos /* Status of the readchar callback.
84*56bb7041Schristos    Either NOT_SCHEDULED or the callback id.  */
85*56bb7041Schristos static int readchar_callback = NOT_SCHEDULED;
86*56bb7041Schristos 
87*56bb7041Schristos static int readchar (void);
88*56bb7041Schristos static void reset_readchar (void);
89*56bb7041Schristos static void reschedule (void);
90*56bb7041Schristos 
91*56bb7041Schristos /* A cache entry for a successfully looked-up symbol.  */
92*56bb7041Schristos struct sym_cache
93*56bb7041Schristos {
94*56bb7041Schristos   char *name;
95*56bb7041Schristos   CORE_ADDR addr;
96*56bb7041Schristos   struct sym_cache *next;
97*56bb7041Schristos };
98*56bb7041Schristos 
99*56bb7041Schristos static int remote_is_stdio = 0;
100*56bb7041Schristos 
101*56bb7041Schristos static int remote_desc = -1;
102*56bb7041Schristos static int listen_desc = -1;
103*56bb7041Schristos 
104*56bb7041Schristos #ifdef USE_WIN32API
105*56bb7041Schristos # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
106*56bb7041Schristos # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
107*56bb7041Schristos #endif
108*56bb7041Schristos 
109*56bb7041Schristos int
gdb_connected(void)110*56bb7041Schristos gdb_connected (void)
111*56bb7041Schristos {
112*56bb7041Schristos   return remote_desc != -1;
113*56bb7041Schristos }
114*56bb7041Schristos 
115*56bb7041Schristos /* Return true if the remote connection is over stdio.  */
116*56bb7041Schristos 
117*56bb7041Schristos int
remote_connection_is_stdio(void)118*56bb7041Schristos remote_connection_is_stdio (void)
119*56bb7041Schristos {
120*56bb7041Schristos   return remote_is_stdio;
121*56bb7041Schristos }
122*56bb7041Schristos 
123*56bb7041Schristos static void
enable_async_notification(int fd)124*56bb7041Schristos enable_async_notification (int fd)
125*56bb7041Schristos {
126*56bb7041Schristos #if defined(F_SETFL) && defined (FASYNC)
127*56bb7041Schristos   int save_fcntl_flags;
128*56bb7041Schristos 
129*56bb7041Schristos   save_fcntl_flags = fcntl (fd, F_GETFL, 0);
130*56bb7041Schristos   fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
131*56bb7041Schristos #if defined (F_SETOWN)
132*56bb7041Schristos   fcntl (fd, F_SETOWN, getpid ());
133*56bb7041Schristos #endif
134*56bb7041Schristos #endif
135*56bb7041Schristos }
136*56bb7041Schristos 
137*56bb7041Schristos static void
handle_accept_event(int err,gdb_client_data client_data)138*56bb7041Schristos handle_accept_event (int err, gdb_client_data client_data)
139*56bb7041Schristos {
140*56bb7041Schristos   struct sockaddr_storage sockaddr;
141*56bb7041Schristos   socklen_t len = sizeof (sockaddr);
142*56bb7041Schristos 
143*56bb7041Schristos   if (debug_threads)
144*56bb7041Schristos     debug_printf ("handling possible accept event\n");
145*56bb7041Schristos 
146*56bb7041Schristos   remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &len);
147*56bb7041Schristos   if (remote_desc == -1)
148*56bb7041Schristos     perror_with_name ("Accept failed");
149*56bb7041Schristos 
150*56bb7041Schristos   /* Enable TCP keep alive process. */
151*56bb7041Schristos   socklen_t tmp = 1;
152*56bb7041Schristos   setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
153*56bb7041Schristos 	      (char *) &tmp, sizeof (tmp));
154*56bb7041Schristos 
155*56bb7041Schristos   /* Tell TCP not to delay small packets.  This greatly speeds up
156*56bb7041Schristos      interactive response. */
157*56bb7041Schristos   tmp = 1;
158*56bb7041Schristos   setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
159*56bb7041Schristos 	      (char *) &tmp, sizeof (tmp));
160*56bb7041Schristos 
161*56bb7041Schristos #ifndef USE_WIN32API
162*56bb7041Schristos   signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbserver simply
163*56bb7041Schristos 				   exits when the remote side dies.  */
164*56bb7041Schristos #endif
165*56bb7041Schristos 
166*56bb7041Schristos   if (run_once)
167*56bb7041Schristos     {
168*56bb7041Schristos #ifndef USE_WIN32API
169*56bb7041Schristos       close (listen_desc);		/* No longer need this */
170*56bb7041Schristos #else
171*56bb7041Schristos       closesocket (listen_desc);	/* No longer need this */
172*56bb7041Schristos #endif
173*56bb7041Schristos     }
174*56bb7041Schristos 
175*56bb7041Schristos   /* Even if !RUN_ONCE no longer notice new connections.  Still keep the
176*56bb7041Schristos      descriptor open for add_file_handler to wait for a new connection.  */
177*56bb7041Schristos   delete_file_handler (listen_desc);
178*56bb7041Schristos 
179*56bb7041Schristos   /* Convert IP address to string.  */
180*56bb7041Schristos   char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
181*56bb7041Schristos 
182*56bb7041Schristos   int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
183*56bb7041Schristos 		       orig_host, sizeof (orig_host),
184*56bb7041Schristos 		       orig_port, sizeof (orig_port),
185*56bb7041Schristos 		       NI_NUMERICHOST | NI_NUMERICSERV);
186*56bb7041Schristos 
187*56bb7041Schristos   if (r != 0)
188*56bb7041Schristos     fprintf (stderr, _("Could not obtain remote address: %s\n"),
189*56bb7041Schristos 	     gai_strerror (r));
190*56bb7041Schristos   else
191*56bb7041Schristos     fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
192*56bb7041Schristos 	     orig_host, orig_port);
193*56bb7041Schristos 
194*56bb7041Schristos   enable_async_notification (remote_desc);
195*56bb7041Schristos 
196*56bb7041Schristos   /* Register the event loop handler.  */
197*56bb7041Schristos   add_file_handler (remote_desc, handle_serial_event, NULL);
198*56bb7041Schristos 
199*56bb7041Schristos   /* We have a new GDB connection now.  If we were disconnected
200*56bb7041Schristos      tracing, there's a window where the target could report a stop
201*56bb7041Schristos      event to the event loop, and since we have a connection now, we'd
202*56bb7041Schristos      try to send vStopped notifications to GDB.  But, don't do that
203*56bb7041Schristos      until GDB as selected all-stop/non-stop, and has queried the
204*56bb7041Schristos      threads' status ('?').  */
205*56bb7041Schristos   target_async (0);
206*56bb7041Schristos }
207*56bb7041Schristos 
208*56bb7041Schristos /* Prepare for a later connection to a remote debugger.
209*56bb7041Schristos    NAME is the filename used for communication.  */
210*56bb7041Schristos 
211*56bb7041Schristos void
remote_prepare(const char * name)212*56bb7041Schristos remote_prepare (const char *name)
213*56bb7041Schristos {
214*56bb7041Schristos   client_state &cs = get_client_state ();
215*56bb7041Schristos #ifdef USE_WIN32API
216*56bb7041Schristos   static int winsock_initialized;
217*56bb7041Schristos #endif
218*56bb7041Schristos   socklen_t tmp;
219*56bb7041Schristos 
220*56bb7041Schristos   remote_is_stdio = 0;
221*56bb7041Schristos   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
222*56bb7041Schristos     {
223*56bb7041Schristos       /* We need to record fact that we're using stdio sooner than the
224*56bb7041Schristos 	 call to remote_open so start_inferior knows the connection is
225*56bb7041Schristos 	 via stdio.  */
226*56bb7041Schristos       remote_is_stdio = 1;
227*56bb7041Schristos       cs.transport_is_reliable = 1;
228*56bb7041Schristos       return;
229*56bb7041Schristos     }
230*56bb7041Schristos 
231*56bb7041Schristos   struct addrinfo hint;
232*56bb7041Schristos   struct addrinfo *ainfo;
233*56bb7041Schristos 
234*56bb7041Schristos   memset (&hint, 0, sizeof (hint));
235*56bb7041Schristos   /* Assume no prefix will be passed, therefore we should use
236*56bb7041Schristos      AF_UNSPEC.  */
237*56bb7041Schristos   hint.ai_family = AF_UNSPEC;
238*56bb7041Schristos   hint.ai_socktype = SOCK_STREAM;
239*56bb7041Schristos   hint.ai_protocol = IPPROTO_TCP;
240*56bb7041Schristos 
241*56bb7041Schristos   parsed_connection_spec parsed
242*56bb7041Schristos     = parse_connection_spec_without_prefix (name, &hint);
243*56bb7041Schristos 
244*56bb7041Schristos   if (parsed.port_str.empty ())
245*56bb7041Schristos     {
246*56bb7041Schristos       cs.transport_is_reliable = 0;
247*56bb7041Schristos       return;
248*56bb7041Schristos     }
249*56bb7041Schristos 
250*56bb7041Schristos #ifdef USE_WIN32API
251*56bb7041Schristos   if (!winsock_initialized)
252*56bb7041Schristos     {
253*56bb7041Schristos       WSADATA wsad;
254*56bb7041Schristos 
255*56bb7041Schristos       WSAStartup (MAKEWORD (1, 0), &wsad);
256*56bb7041Schristos       winsock_initialized = 1;
257*56bb7041Schristos     }
258*56bb7041Schristos #endif
259*56bb7041Schristos 
260*56bb7041Schristos   int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
261*56bb7041Schristos 		       &hint, &ainfo);
262*56bb7041Schristos 
263*56bb7041Schristos   if (r != 0)
264*56bb7041Schristos     error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
265*56bb7041Schristos 
266*56bb7041Schristos   scoped_free_addrinfo freeaddrinfo (ainfo);
267*56bb7041Schristos 
268*56bb7041Schristos   struct addrinfo *iter;
269*56bb7041Schristos 
270*56bb7041Schristos   for (iter = ainfo; iter != NULL; iter = iter->ai_next)
271*56bb7041Schristos     {
272*56bb7041Schristos       listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
273*56bb7041Schristos 					iter->ai_protocol);
274*56bb7041Schristos 
275*56bb7041Schristos       if (listen_desc >= 0)
276*56bb7041Schristos 	break;
277*56bb7041Schristos     }
278*56bb7041Schristos 
279*56bb7041Schristos   if (iter == NULL)
280*56bb7041Schristos     perror_with_name ("Can't open socket");
281*56bb7041Schristos 
282*56bb7041Schristos   /* Allow rapid reuse of this port. */
283*56bb7041Schristos   tmp = 1;
284*56bb7041Schristos   setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
285*56bb7041Schristos 	      sizeof (tmp));
286*56bb7041Schristos 
287*56bb7041Schristos   switch (iter->ai_family)
288*56bb7041Schristos     {
289*56bb7041Schristos     case AF_INET:
290*56bb7041Schristos       ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
291*56bb7041Schristos       break;
292*56bb7041Schristos     case AF_INET6:
293*56bb7041Schristos       ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
294*56bb7041Schristos       break;
295*56bb7041Schristos     default:
296*56bb7041Schristos       internal_error (__FILE__, __LINE__,
297*56bb7041Schristos 		      _("Invalid 'ai_family' %d\n"), iter->ai_family);
298*56bb7041Schristos     }
299*56bb7041Schristos 
300*56bb7041Schristos   if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
301*56bb7041Schristos     perror_with_name ("Can't bind address");
302*56bb7041Schristos 
303*56bb7041Schristos   if (listen (listen_desc, 1) != 0)
304*56bb7041Schristos     perror_with_name ("Can't listen on socket");
305*56bb7041Schristos 
306*56bb7041Schristos   cs.transport_is_reliable = 1;
307*56bb7041Schristos }
308*56bb7041Schristos 
309*56bb7041Schristos /* Open a connection to a remote debugger.
310*56bb7041Schristos    NAME is the filename used for communication.  */
311*56bb7041Schristos 
312*56bb7041Schristos void
remote_open(const char * name)313*56bb7041Schristos remote_open (const char *name)
314*56bb7041Schristos {
315*56bb7041Schristos   const char *port_str;
316*56bb7041Schristos 
317*56bb7041Schristos   port_str = strchr (name, ':');
318*56bb7041Schristos #ifdef USE_WIN32API
319*56bb7041Schristos   if (port_str == NULL)
320*56bb7041Schristos     error ("Only HOST:PORT is supported on this platform.");
321*56bb7041Schristos #endif
322*56bb7041Schristos 
323*56bb7041Schristos   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
324*56bb7041Schristos     {
325*56bb7041Schristos       fprintf (stderr, "Remote debugging using stdio\n");
326*56bb7041Schristos 
327*56bb7041Schristos       /* Use stdin as the handle of the connection.
328*56bb7041Schristos 	 We only select on reads, for example.  */
329*56bb7041Schristos       remote_desc = fileno (stdin);
330*56bb7041Schristos 
331*56bb7041Schristos       enable_async_notification (remote_desc);
332*56bb7041Schristos 
333*56bb7041Schristos       /* Register the event loop handler.  */
334*56bb7041Schristos       add_file_handler (remote_desc, handle_serial_event, NULL);
335*56bb7041Schristos     }
336*56bb7041Schristos #ifndef USE_WIN32API
337*56bb7041Schristos   else if (port_str == NULL)
338*56bb7041Schristos     {
339*56bb7041Schristos       struct stat statbuf;
340*56bb7041Schristos 
341*56bb7041Schristos       if (stat (name, &statbuf) == 0
342*56bb7041Schristos 	  && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
343*56bb7041Schristos 	remote_desc = open (name, O_RDWR);
344*56bb7041Schristos       else
345*56bb7041Schristos 	{
346*56bb7041Schristos 	  errno = EINVAL;
347*56bb7041Schristos 	  remote_desc = -1;
348*56bb7041Schristos 	}
349*56bb7041Schristos 
350*56bb7041Schristos       if (remote_desc < 0)
351*56bb7041Schristos 	perror_with_name ("Could not open remote device");
352*56bb7041Schristos 
353*56bb7041Schristos #if HAVE_TERMIOS_H
354*56bb7041Schristos       {
355*56bb7041Schristos 	struct termios termios;
356*56bb7041Schristos 	tcgetattr (remote_desc, &termios);
357*56bb7041Schristos 
358*56bb7041Schristos 	termios.c_iflag = 0;
359*56bb7041Schristos 	termios.c_oflag = 0;
360*56bb7041Schristos 	termios.c_lflag = 0;
361*56bb7041Schristos 	termios.c_cflag &= ~(CSIZE | PARENB);
362*56bb7041Schristos 	termios.c_cflag |= CLOCAL | CS8;
363*56bb7041Schristos 	termios.c_cc[VMIN] = 1;
364*56bb7041Schristos 	termios.c_cc[VTIME] = 0;
365*56bb7041Schristos 
366*56bb7041Schristos 	tcsetattr (remote_desc, TCSANOW, &termios);
367*56bb7041Schristos       }
368*56bb7041Schristos #endif
369*56bb7041Schristos 
370*56bb7041Schristos       fprintf (stderr, "Remote debugging using %s\n", name);
371*56bb7041Schristos 
372*56bb7041Schristos       enable_async_notification (remote_desc);
373*56bb7041Schristos 
374*56bb7041Schristos       /* Register the event loop handler.  */
375*56bb7041Schristos       add_file_handler (remote_desc, handle_serial_event, NULL);
376*56bb7041Schristos     }
377*56bb7041Schristos #endif /* USE_WIN32API */
378*56bb7041Schristos   else
379*56bb7041Schristos     {
380*56bb7041Schristos       char listen_port[GDB_NI_MAX_PORT];
381*56bb7041Schristos       struct sockaddr_storage sockaddr;
382*56bb7041Schristos       socklen_t len = sizeof (sockaddr);
383*56bb7041Schristos 
384*56bb7041Schristos       if (getsockname (listen_desc, (struct sockaddr *) &sockaddr, &len) < 0)
385*56bb7041Schristos 	perror_with_name ("Can't determine port");
386*56bb7041Schristos 
387*56bb7041Schristos       int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
388*56bb7041Schristos 			   NULL, 0,
389*56bb7041Schristos 			   listen_port, sizeof (listen_port),
390*56bb7041Schristos 			   NI_NUMERICSERV);
391*56bb7041Schristos 
392*56bb7041Schristos       if (r != 0)
393*56bb7041Schristos 	fprintf (stderr, _("Can't obtain port where we are listening: %s"),
394*56bb7041Schristos 		 gai_strerror (r));
395*56bb7041Schristos       else
396*56bb7041Schristos 	fprintf (stderr, _("Listening on port %s\n"), listen_port);
397*56bb7041Schristos 
398*56bb7041Schristos       fflush (stderr);
399*56bb7041Schristos 
400*56bb7041Schristos       /* Register the event loop handler.  */
401*56bb7041Schristos       add_file_handler (listen_desc, handle_accept_event, NULL);
402*56bb7041Schristos     }
403*56bb7041Schristos }
404*56bb7041Schristos 
405*56bb7041Schristos void
remote_close(void)406*56bb7041Schristos remote_close (void)
407*56bb7041Schristos {
408*56bb7041Schristos   delete_file_handler (remote_desc);
409*56bb7041Schristos 
410*56bb7041Schristos   disable_async_io ();
411*56bb7041Schristos 
412*56bb7041Schristos #ifdef USE_WIN32API
413*56bb7041Schristos   closesocket (remote_desc);
414*56bb7041Schristos #else
415*56bb7041Schristos   if (! remote_connection_is_stdio ())
416*56bb7041Schristos     close (remote_desc);
417*56bb7041Schristos #endif
418*56bb7041Schristos   remote_desc = -1;
419*56bb7041Schristos 
420*56bb7041Schristos   reset_readchar ();
421*56bb7041Schristos }
422*56bb7041Schristos 
423*56bb7041Schristos #endif
424*56bb7041Schristos 
425*56bb7041Schristos #ifndef IN_PROCESS_AGENT
426*56bb7041Schristos 
427*56bb7041Schristos void
decode_address(CORE_ADDR * addrp,const char * start,int len)428*56bb7041Schristos decode_address (CORE_ADDR *addrp, const char *start, int len)
429*56bb7041Schristos {
430*56bb7041Schristos   CORE_ADDR addr;
431*56bb7041Schristos   char ch;
432*56bb7041Schristos   int i;
433*56bb7041Schristos 
434*56bb7041Schristos   addr = 0;
435*56bb7041Schristos   for (i = 0; i < len; i++)
436*56bb7041Schristos     {
437*56bb7041Schristos       ch = start[i];
438*56bb7041Schristos       addr = addr << 4;
439*56bb7041Schristos       addr = addr | (fromhex (ch) & 0x0f);
440*56bb7041Schristos     }
441*56bb7041Schristos   *addrp = addr;
442*56bb7041Schristos }
443*56bb7041Schristos 
444*56bb7041Schristos const char *
decode_address_to_semicolon(CORE_ADDR * addrp,const char * start)445*56bb7041Schristos decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
446*56bb7041Schristos {
447*56bb7041Schristos   const char *end;
448*56bb7041Schristos 
449*56bb7041Schristos   end = start;
450*56bb7041Schristos   while (*end != '\0' && *end != ';')
451*56bb7041Schristos     end++;
452*56bb7041Schristos 
453*56bb7041Schristos   decode_address (addrp, start, end - start);
454*56bb7041Schristos 
455*56bb7041Schristos   if (*end == ';')
456*56bb7041Schristos     end++;
457*56bb7041Schristos   return end;
458*56bb7041Schristos }
459*56bb7041Schristos 
460*56bb7041Schristos #endif
461*56bb7041Schristos 
462*56bb7041Schristos #ifndef IN_PROCESS_AGENT
463*56bb7041Schristos 
464*56bb7041Schristos /* Look for a sequence of characters which can be run-length encoded.
465*56bb7041Schristos    If there are any, update *CSUM and *P.  Otherwise, output the
466*56bb7041Schristos    single character.  Return the number of characters consumed.  */
467*56bb7041Schristos 
468*56bb7041Schristos static int
try_rle(char * buf,int remaining,unsigned char * csum,char ** p)469*56bb7041Schristos try_rle (char *buf, int remaining, unsigned char *csum, char **p)
470*56bb7041Schristos {
471*56bb7041Schristos   int n;
472*56bb7041Schristos 
473*56bb7041Schristos   /* Always output the character.  */
474*56bb7041Schristos   *csum += buf[0];
475*56bb7041Schristos   *(*p)++ = buf[0];
476*56bb7041Schristos 
477*56bb7041Schristos   /* Don't go past '~'.  */
478*56bb7041Schristos   if (remaining > 97)
479*56bb7041Schristos     remaining = 97;
480*56bb7041Schristos 
481*56bb7041Schristos   for (n = 1; n < remaining; n++)
482*56bb7041Schristos     if (buf[n] != buf[0])
483*56bb7041Schristos       break;
484*56bb7041Schristos 
485*56bb7041Schristos   /* N is the index of the first character not the same as buf[0].
486*56bb7041Schristos      buf[0] is counted twice, so by decrementing N, we get the number
487*56bb7041Schristos      of characters the RLE sequence will replace.  */
488*56bb7041Schristos   n--;
489*56bb7041Schristos 
490*56bb7041Schristos   if (n < 3)
491*56bb7041Schristos     return 1;
492*56bb7041Schristos 
493*56bb7041Schristos   /* Skip the frame characters.  The manual says to skip '+' and '-'
494*56bb7041Schristos      also, but there's no reason to.  Unfortunately these two unusable
495*56bb7041Schristos      characters double the encoded length of a four byte zero
496*56bb7041Schristos      value.  */
497*56bb7041Schristos   while (n + 29 == '$' || n + 29 == '#')
498*56bb7041Schristos     n--;
499*56bb7041Schristos 
500*56bb7041Schristos   *csum += '*';
501*56bb7041Schristos   *(*p)++ = '*';
502*56bb7041Schristos   *csum += n + 29;
503*56bb7041Schristos   *(*p)++ = n + 29;
504*56bb7041Schristos 
505*56bb7041Schristos   return n + 1;
506*56bb7041Schristos }
507*56bb7041Schristos 
508*56bb7041Schristos #endif
509*56bb7041Schristos 
510*56bb7041Schristos #ifndef IN_PROCESS_AGENT
511*56bb7041Schristos 
512*56bb7041Schristos /* Write a PTID to BUF.  Returns BUF+CHARACTERS_WRITTEN.  */
513*56bb7041Schristos 
514*56bb7041Schristos char *
write_ptid(char * buf,ptid_t ptid)515*56bb7041Schristos write_ptid (char *buf, ptid_t ptid)
516*56bb7041Schristos {
517*56bb7041Schristos   client_state &cs = get_client_state ();
518*56bb7041Schristos   int pid, tid;
519*56bb7041Schristos 
520*56bb7041Schristos   if (cs.multi_process)
521*56bb7041Schristos     {
522*56bb7041Schristos       pid = ptid.pid ();
523*56bb7041Schristos       if (pid < 0)
524*56bb7041Schristos 	buf += sprintf (buf, "p-%x.", -pid);
525*56bb7041Schristos       else
526*56bb7041Schristos 	buf += sprintf (buf, "p%x.", pid);
527*56bb7041Schristos     }
528*56bb7041Schristos   tid = ptid.lwp ();
529*56bb7041Schristos   if (tid < 0)
530*56bb7041Schristos     buf += sprintf (buf, "-%x", -tid);
531*56bb7041Schristos   else
532*56bb7041Schristos     buf += sprintf (buf, "%x", tid);
533*56bb7041Schristos 
534*56bb7041Schristos   return buf;
535*56bb7041Schristos }
536*56bb7041Schristos 
537*56bb7041Schristos static ULONGEST
hex_or_minus_one(const char * buf,const char ** obuf)538*56bb7041Schristos hex_or_minus_one (const char *buf, const char **obuf)
539*56bb7041Schristos {
540*56bb7041Schristos   ULONGEST ret;
541*56bb7041Schristos 
542*56bb7041Schristos   if (startswith (buf, "-1"))
543*56bb7041Schristos     {
544*56bb7041Schristos       ret = (ULONGEST) -1;
545*56bb7041Schristos       buf += 2;
546*56bb7041Schristos     }
547*56bb7041Schristos   else
548*56bb7041Schristos     buf = unpack_varlen_hex (buf, &ret);
549*56bb7041Schristos 
550*56bb7041Schristos   if (obuf)
551*56bb7041Schristos     *obuf = buf;
552*56bb7041Schristos 
553*56bb7041Schristos   return ret;
554*56bb7041Schristos }
555*56bb7041Schristos 
556*56bb7041Schristos /* Extract a PTID from BUF.  If non-null, OBUF is set to the to one
557*56bb7041Schristos    passed the last parsed char.  Returns null_ptid on error.  */
558*56bb7041Schristos ptid_t
read_ptid(const char * buf,const char ** obuf)559*56bb7041Schristos read_ptid (const char *buf, const char **obuf)
560*56bb7041Schristos {
561*56bb7041Schristos   const char *p = buf;
562*56bb7041Schristos   const char *pp;
563*56bb7041Schristos   ULONGEST pid = 0, tid = 0;
564*56bb7041Schristos 
565*56bb7041Schristos   if (*p == 'p')
566*56bb7041Schristos     {
567*56bb7041Schristos       /* Multi-process ptid.  */
568*56bb7041Schristos       pp = unpack_varlen_hex (p + 1, &pid);
569*56bb7041Schristos       if (*pp != '.')
570*56bb7041Schristos 	error ("invalid remote ptid: %s\n", p);
571*56bb7041Schristos 
572*56bb7041Schristos       p = pp + 1;
573*56bb7041Schristos 
574*56bb7041Schristos       tid = hex_or_minus_one (p, &pp);
575*56bb7041Schristos 
576*56bb7041Schristos       if (obuf)
577*56bb7041Schristos 	*obuf = pp;
578*56bb7041Schristos       return ptid_t (pid, tid, 0);
579*56bb7041Schristos     }
580*56bb7041Schristos 
581*56bb7041Schristos   /* No multi-process.  Just a tid.  */
582*56bb7041Schristos   tid = hex_or_minus_one (p, &pp);
583*56bb7041Schristos 
584*56bb7041Schristos   /* Since GDB is not sending a process id (multi-process extensions
585*56bb7041Schristos      are off), then there's only one process.  Default to the first in
586*56bb7041Schristos      the list.  */
587*56bb7041Schristos   pid = pid_of (get_first_process ());
588*56bb7041Schristos 
589*56bb7041Schristos   if (obuf)
590*56bb7041Schristos     *obuf = pp;
591*56bb7041Schristos   return ptid_t (pid, tid, 0);
592*56bb7041Schristos }
593*56bb7041Schristos 
594*56bb7041Schristos /* Write COUNT bytes in BUF to the client.
595*56bb7041Schristos    The result is the number of bytes written or -1 if error.
596*56bb7041Schristos    This may return less than COUNT.  */
597*56bb7041Schristos 
598*56bb7041Schristos static int
write_prim(const void * buf,int count)599*56bb7041Schristos write_prim (const void *buf, int count)
600*56bb7041Schristos {
601*56bb7041Schristos   if (remote_connection_is_stdio ())
602*56bb7041Schristos     return write (fileno (stdout), buf, count);
603*56bb7041Schristos   else
604*56bb7041Schristos     return write (remote_desc, buf, count);
605*56bb7041Schristos }
606*56bb7041Schristos 
607*56bb7041Schristos /* Read COUNT bytes from the client and store in BUF.
608*56bb7041Schristos    The result is the number of bytes read or -1 if error.
609*56bb7041Schristos    This may return less than COUNT.  */
610*56bb7041Schristos 
611*56bb7041Schristos static int
read_prim(void * buf,int count)612*56bb7041Schristos read_prim (void *buf, int count)
613*56bb7041Schristos {
614*56bb7041Schristos   if (remote_connection_is_stdio ())
615*56bb7041Schristos     return read (fileno (stdin), buf, count);
616*56bb7041Schristos   else
617*56bb7041Schristos     return read (remote_desc, buf, count);
618*56bb7041Schristos }
619*56bb7041Schristos 
620*56bb7041Schristos /* Send a packet to the remote machine, with error checking.
621*56bb7041Schristos    The data of the packet is in BUF, and the length of the
622*56bb7041Schristos    packet is in CNT.  Returns >= 0 on success, -1 otherwise.  */
623*56bb7041Schristos 
624*56bb7041Schristos static int
putpkt_binary_1(char * buf,int cnt,int is_notif)625*56bb7041Schristos putpkt_binary_1 (char *buf, int cnt, int is_notif)
626*56bb7041Schristos {
627*56bb7041Schristos   client_state &cs = get_client_state ();
628*56bb7041Schristos   int i;
629*56bb7041Schristos   unsigned char csum = 0;
630*56bb7041Schristos   char *buf2;
631*56bb7041Schristos   char *p;
632*56bb7041Schristos   int cc;
633*56bb7041Schristos 
634*56bb7041Schristos   buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
635*56bb7041Schristos 
636*56bb7041Schristos   /* Copy the packet into buffer BUF2, encapsulating it
637*56bb7041Schristos      and giving it a checksum.  */
638*56bb7041Schristos 
639*56bb7041Schristos   p = buf2;
640*56bb7041Schristos   if (is_notif)
641*56bb7041Schristos     *p++ = '%';
642*56bb7041Schristos   else
643*56bb7041Schristos     *p++ = '$';
644*56bb7041Schristos 
645*56bb7041Schristos   for (i = 0; i < cnt;)
646*56bb7041Schristos     i += try_rle (buf + i, cnt - i, &csum, &p);
647*56bb7041Schristos 
648*56bb7041Schristos   *p++ = '#';
649*56bb7041Schristos   *p++ = tohex ((csum >> 4) & 0xf);
650*56bb7041Schristos   *p++ = tohex (csum & 0xf);
651*56bb7041Schristos 
652*56bb7041Schristos   *p = '\0';
653*56bb7041Schristos 
654*56bb7041Schristos   /* Send it over and over until we get a positive ack.  */
655*56bb7041Schristos 
656*56bb7041Schristos   do
657*56bb7041Schristos     {
658*56bb7041Schristos       if (write_prim (buf2, p - buf2) != p - buf2)
659*56bb7041Schristos 	{
660*56bb7041Schristos 	  perror ("putpkt(write)");
661*56bb7041Schristos 	  free (buf2);
662*56bb7041Schristos 	  return -1;
663*56bb7041Schristos 	}
664*56bb7041Schristos 
665*56bb7041Schristos       if (cs.noack_mode || is_notif)
666*56bb7041Schristos 	{
667*56bb7041Schristos 	  /* Don't expect an ack then.  */
668*56bb7041Schristos 	  if (remote_debug)
669*56bb7041Schristos 	    {
670*56bb7041Schristos 	      if (is_notif)
671*56bb7041Schristos 		debug_printf ("putpkt (\"%s\"); [notif]\n", buf2);
672*56bb7041Schristos 	      else
673*56bb7041Schristos 		debug_printf ("putpkt (\"%s\"); [noack mode]\n", buf2);
674*56bb7041Schristos 	      debug_flush ();
675*56bb7041Schristos 	    }
676*56bb7041Schristos 	  break;
677*56bb7041Schristos 	}
678*56bb7041Schristos 
679*56bb7041Schristos       if (remote_debug)
680*56bb7041Schristos 	{
681*56bb7041Schristos 	  debug_printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
682*56bb7041Schristos 	  debug_flush ();
683*56bb7041Schristos 	}
684*56bb7041Schristos 
685*56bb7041Schristos       cc = readchar ();
686*56bb7041Schristos 
687*56bb7041Schristos       if (cc < 0)
688*56bb7041Schristos 	{
689*56bb7041Schristos 	  free (buf2);
690*56bb7041Schristos 	  return -1;
691*56bb7041Schristos 	}
692*56bb7041Schristos 
693*56bb7041Schristos       if (remote_debug)
694*56bb7041Schristos 	{
695*56bb7041Schristos 	  debug_printf ("[received '%c' (0x%x)]\n", cc, cc);
696*56bb7041Schristos 	  debug_flush ();
697*56bb7041Schristos 	}
698*56bb7041Schristos 
699*56bb7041Schristos       /* Check for an input interrupt while we're here.  */
700*56bb7041Schristos       if (cc == '\003' && current_thread != NULL)
701*56bb7041Schristos 	the_target->request_interrupt ();
702*56bb7041Schristos     }
703*56bb7041Schristos   while (cc != '+');
704*56bb7041Schristos 
705*56bb7041Schristos   free (buf2);
706*56bb7041Schristos   return 1;			/* Success! */
707*56bb7041Schristos }
708*56bb7041Schristos 
709*56bb7041Schristos int
putpkt_binary(char * buf,int cnt)710*56bb7041Schristos putpkt_binary (char *buf, int cnt)
711*56bb7041Schristos {
712*56bb7041Schristos   return putpkt_binary_1 (buf, cnt, 0);
713*56bb7041Schristos }
714*56bb7041Schristos 
715*56bb7041Schristos /* Send a packet to the remote machine, with error checking.  The data
716*56bb7041Schristos    of the packet is in BUF, and the packet should be a NUL-terminated
717*56bb7041Schristos    string.  Returns >= 0 on success, -1 otherwise.  */
718*56bb7041Schristos 
719*56bb7041Schristos int
putpkt(char * buf)720*56bb7041Schristos putpkt (char *buf)
721*56bb7041Schristos {
722*56bb7041Schristos   return putpkt_binary (buf, strlen (buf));
723*56bb7041Schristos }
724*56bb7041Schristos 
725*56bb7041Schristos int
putpkt_notif(char * buf)726*56bb7041Schristos putpkt_notif (char *buf)
727*56bb7041Schristos {
728*56bb7041Schristos   return putpkt_binary_1 (buf, strlen (buf), 1);
729*56bb7041Schristos }
730*56bb7041Schristos 
731*56bb7041Schristos /* Come here when we get an input interrupt from the remote side.  This
732*56bb7041Schristos    interrupt should only be active while we are waiting for the child to do
733*56bb7041Schristos    something.  Thus this assumes readchar:bufcnt is 0.
734*56bb7041Schristos    About the only thing that should come through is a ^C, which
735*56bb7041Schristos    will cause us to request child interruption.  */
736*56bb7041Schristos 
737*56bb7041Schristos static void
input_interrupt(int unused)738*56bb7041Schristos input_interrupt (int unused)
739*56bb7041Schristos {
740*56bb7041Schristos   fd_set readset;
741*56bb7041Schristos   struct timeval immediate = { 0, 0 };
742*56bb7041Schristos 
743*56bb7041Schristos   /* Protect against spurious interrupts.  This has been observed to
744*56bb7041Schristos      be a problem under NetBSD 1.4 and 1.5.  */
745*56bb7041Schristos 
746*56bb7041Schristos   FD_ZERO (&readset);
747*56bb7041Schristos   FD_SET (remote_desc, &readset);
748*56bb7041Schristos   if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
749*56bb7041Schristos     {
750*56bb7041Schristos       int cc;
751*56bb7041Schristos       char c = 0;
752*56bb7041Schristos 
753*56bb7041Schristos       cc = read_prim (&c, 1);
754*56bb7041Schristos 
755*56bb7041Schristos       if (cc == 0)
756*56bb7041Schristos 	{
757*56bb7041Schristos 	  fprintf (stderr, "client connection closed\n");
758*56bb7041Schristos 	  return;
759*56bb7041Schristos 	}
760*56bb7041Schristos       else if (cc != 1 || c != '\003')
761*56bb7041Schristos 	{
762*56bb7041Schristos 	  fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
763*56bb7041Schristos 	  if (isprint (c))
764*56bb7041Schristos 	    fprintf (stderr, "('%c')\n", c);
765*56bb7041Schristos 	  else
766*56bb7041Schristos 	    fprintf (stderr, "('\\x%02x')\n", c & 0xff);
767*56bb7041Schristos 	  return;
768*56bb7041Schristos 	}
769*56bb7041Schristos 
770*56bb7041Schristos       the_target->request_interrupt ();
771*56bb7041Schristos     }
772*56bb7041Schristos }
773*56bb7041Schristos 
774*56bb7041Schristos /* Check if the remote side sent us an interrupt request (^C).  */
775*56bb7041Schristos void
check_remote_input_interrupt_request(void)776*56bb7041Schristos check_remote_input_interrupt_request (void)
777*56bb7041Schristos {
778*56bb7041Schristos   /* This function may be called before establishing communications,
779*56bb7041Schristos      therefore we need to validate the remote descriptor.  */
780*56bb7041Schristos 
781*56bb7041Schristos   if (remote_desc == -1)
782*56bb7041Schristos     return;
783*56bb7041Schristos 
784*56bb7041Schristos   input_interrupt (0);
785*56bb7041Schristos }
786*56bb7041Schristos 
787*56bb7041Schristos /* Asynchronous I/O support.  SIGIO must be unblocked when waiting,
788*56bb7041Schristos    in order to accept Control-C from the client, and must be blocked
789*56bb7041Schristos    when talking to the client.  */
790*56bb7041Schristos 
791*56bb7041Schristos static void
block_unblock_async_io(int block)792*56bb7041Schristos block_unblock_async_io (int block)
793*56bb7041Schristos {
794*56bb7041Schristos #ifndef USE_WIN32API
795*56bb7041Schristos   sigset_t sigio_set;
796*56bb7041Schristos 
797*56bb7041Schristos   sigemptyset (&sigio_set);
798*56bb7041Schristos   sigaddset (&sigio_set, SIGIO);
799*56bb7041Schristos   gdb_sigmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
800*56bb7041Schristos #endif
801*56bb7041Schristos }
802*56bb7041Schristos 
803*56bb7041Schristos /* Current state of asynchronous I/O.  */
804*56bb7041Schristos static int async_io_enabled;
805*56bb7041Schristos 
806*56bb7041Schristos /* Enable asynchronous I/O.  */
807*56bb7041Schristos void
enable_async_io(void)808*56bb7041Schristos enable_async_io (void)
809*56bb7041Schristos {
810*56bb7041Schristos   if (async_io_enabled)
811*56bb7041Schristos     return;
812*56bb7041Schristos 
813*56bb7041Schristos   block_unblock_async_io (0);
814*56bb7041Schristos 
815*56bb7041Schristos   async_io_enabled = 1;
816*56bb7041Schristos }
817*56bb7041Schristos 
818*56bb7041Schristos /* Disable asynchronous I/O.  */
819*56bb7041Schristos void
disable_async_io(void)820*56bb7041Schristos disable_async_io (void)
821*56bb7041Schristos {
822*56bb7041Schristos   if (!async_io_enabled)
823*56bb7041Schristos     return;
824*56bb7041Schristos 
825*56bb7041Schristos   block_unblock_async_io (1);
826*56bb7041Schristos 
827*56bb7041Schristos   async_io_enabled = 0;
828*56bb7041Schristos }
829*56bb7041Schristos 
830*56bb7041Schristos void
initialize_async_io(void)831*56bb7041Schristos initialize_async_io (void)
832*56bb7041Schristos {
833*56bb7041Schristos   /* Make sure that async I/O starts blocked.  */
834*56bb7041Schristos   async_io_enabled = 1;
835*56bb7041Schristos   disable_async_io ();
836*56bb7041Schristos 
837*56bb7041Schristos   /* Install the signal handler.  */
838*56bb7041Schristos #ifndef USE_WIN32API
839*56bb7041Schristos   signal (SIGIO, input_interrupt);
840*56bb7041Schristos #endif
841*56bb7041Schristos }
842*56bb7041Schristos 
843*56bb7041Schristos /* Internal buffer used by readchar.
844*56bb7041Schristos    These are global to readchar because reschedule_remote needs to be
845*56bb7041Schristos    able to tell whether the buffer is empty.  */
846*56bb7041Schristos 
847*56bb7041Schristos static unsigned char readchar_buf[BUFSIZ];
848*56bb7041Schristos static int readchar_bufcnt = 0;
849*56bb7041Schristos static unsigned char *readchar_bufp;
850*56bb7041Schristos 
851*56bb7041Schristos /* Returns next char from remote GDB.  -1 if error.  */
852*56bb7041Schristos 
853*56bb7041Schristos static int
readchar(void)854*56bb7041Schristos readchar (void)
855*56bb7041Schristos {
856*56bb7041Schristos   int ch;
857*56bb7041Schristos 
858*56bb7041Schristos   if (readchar_bufcnt == 0)
859*56bb7041Schristos     {
860*56bb7041Schristos       readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
861*56bb7041Schristos 
862*56bb7041Schristos       if (readchar_bufcnt <= 0)
863*56bb7041Schristos 	{
864*56bb7041Schristos 	  if (readchar_bufcnt == 0)
865*56bb7041Schristos 	    {
866*56bb7041Schristos 	      if (remote_debug)
867*56bb7041Schristos 		debug_printf ("readchar: Got EOF\n");
868*56bb7041Schristos 	    }
869*56bb7041Schristos 	  else
870*56bb7041Schristos 	    perror ("readchar");
871*56bb7041Schristos 
872*56bb7041Schristos 	  return -1;
873*56bb7041Schristos 	}
874*56bb7041Schristos 
875*56bb7041Schristos       readchar_bufp = readchar_buf;
876*56bb7041Schristos     }
877*56bb7041Schristos 
878*56bb7041Schristos   readchar_bufcnt--;
879*56bb7041Schristos   ch = *readchar_bufp++;
880*56bb7041Schristos   reschedule ();
881*56bb7041Schristos   return ch;
882*56bb7041Schristos }
883*56bb7041Schristos 
884*56bb7041Schristos /* Reset the readchar state machine.  */
885*56bb7041Schristos 
886*56bb7041Schristos static void
reset_readchar(void)887*56bb7041Schristos reset_readchar (void)
888*56bb7041Schristos {
889*56bb7041Schristos   readchar_bufcnt = 0;
890*56bb7041Schristos   if (readchar_callback != NOT_SCHEDULED)
891*56bb7041Schristos     {
892*56bb7041Schristos       delete_timer (readchar_callback);
893*56bb7041Schristos       readchar_callback = NOT_SCHEDULED;
894*56bb7041Schristos     }
895*56bb7041Schristos }
896*56bb7041Schristos 
897*56bb7041Schristos /* Process remaining data in readchar_buf.  */
898*56bb7041Schristos 
899*56bb7041Schristos static void
process_remaining(void * context)900*56bb7041Schristos process_remaining (void *context)
901*56bb7041Schristos {
902*56bb7041Schristos   /* This is a one-shot event.  */
903*56bb7041Schristos   readchar_callback = NOT_SCHEDULED;
904*56bb7041Schristos 
905*56bb7041Schristos   if (readchar_bufcnt > 0)
906*56bb7041Schristos     handle_serial_event (0, NULL);
907*56bb7041Schristos }
908*56bb7041Schristos 
909*56bb7041Schristos /* If there is still data in the buffer, queue another event to process it,
910*56bb7041Schristos    we can't sleep in select yet.  */
911*56bb7041Schristos 
912*56bb7041Schristos static void
reschedule(void)913*56bb7041Schristos reschedule (void)
914*56bb7041Schristos {
915*56bb7041Schristos   if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
916*56bb7041Schristos     readchar_callback = create_timer (0, process_remaining, NULL);
917*56bb7041Schristos }
918*56bb7041Schristos 
919*56bb7041Schristos /* Read a packet from the remote machine, with error checking,
920*56bb7041Schristos    and store it in BUF.  Returns length of packet, or negative if error. */
921*56bb7041Schristos 
922*56bb7041Schristos int
getpkt(char * buf)923*56bb7041Schristos getpkt (char *buf)
924*56bb7041Schristos {
925*56bb7041Schristos   client_state &cs = get_client_state ();
926*56bb7041Schristos   char *bp;
927*56bb7041Schristos   unsigned char csum, c1, c2;
928*56bb7041Schristos   int c;
929*56bb7041Schristos 
930*56bb7041Schristos   while (1)
931*56bb7041Schristos     {
932*56bb7041Schristos       csum = 0;
933*56bb7041Schristos 
934*56bb7041Schristos       while (1)
935*56bb7041Schristos 	{
936*56bb7041Schristos 	  c = readchar ();
937*56bb7041Schristos 
938*56bb7041Schristos 	  /* The '\003' may appear before or after each packet, so
939*56bb7041Schristos 	     check for an input interrupt.  */
940*56bb7041Schristos 	  if (c == '\003')
941*56bb7041Schristos 	    {
942*56bb7041Schristos 	      the_target->request_interrupt ();
943*56bb7041Schristos 	      continue;
944*56bb7041Schristos 	    }
945*56bb7041Schristos 
946*56bb7041Schristos 	  if (c == '$')
947*56bb7041Schristos 	    break;
948*56bb7041Schristos 	  if (remote_debug)
949*56bb7041Schristos 	    {
950*56bb7041Schristos 	      debug_printf ("[getpkt: discarding char '%c']\n", c);
951*56bb7041Schristos 	      debug_flush ();
952*56bb7041Schristos 	    }
953*56bb7041Schristos 
954*56bb7041Schristos 	  if (c < 0)
955*56bb7041Schristos 	    return -1;
956*56bb7041Schristos 	}
957*56bb7041Schristos 
958*56bb7041Schristos       bp = buf;
959*56bb7041Schristos       while (1)
960*56bb7041Schristos 	{
961*56bb7041Schristos 	  c = readchar ();
962*56bb7041Schristos 	  if (c < 0)
963*56bb7041Schristos 	    return -1;
964*56bb7041Schristos 	  if (c == '#')
965*56bb7041Schristos 	    break;
966*56bb7041Schristos 	  *bp++ = c;
967*56bb7041Schristos 	  csum += c;
968*56bb7041Schristos 	}
969*56bb7041Schristos       *bp = 0;
970*56bb7041Schristos 
971*56bb7041Schristos       c1 = fromhex (readchar ());
972*56bb7041Schristos       c2 = fromhex (readchar ());
973*56bb7041Schristos 
974*56bb7041Schristos       if (csum == (c1 << 4) + c2)
975*56bb7041Schristos 	break;
976*56bb7041Schristos 
977*56bb7041Schristos       if (cs.noack_mode)
978*56bb7041Schristos 	{
979*56bb7041Schristos 	  fprintf (stderr,
980*56bb7041Schristos 		   "Bad checksum, sentsum=0x%x, csum=0x%x, "
981*56bb7041Schristos 		   "buf=%s [no-ack-mode, Bad medium?]\n",
982*56bb7041Schristos 		   (c1 << 4) + c2, csum, buf);
983*56bb7041Schristos 	  /* Not much we can do, GDB wasn't expecting an ack/nac.  */
984*56bb7041Schristos 	  break;
985*56bb7041Schristos 	}
986*56bb7041Schristos 
987*56bb7041Schristos       fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
988*56bb7041Schristos 	       (c1 << 4) + c2, csum, buf);
989*56bb7041Schristos       if (write_prim ("-", 1) != 1)
990*56bb7041Schristos 	return -1;
991*56bb7041Schristos     }
992*56bb7041Schristos 
993*56bb7041Schristos   if (!cs.noack_mode)
994*56bb7041Schristos     {
995*56bb7041Schristos       if (remote_debug)
996*56bb7041Schristos 	{
997*56bb7041Schristos 	  debug_printf ("getpkt (\"%s\");  [sending ack] \n", buf);
998*56bb7041Schristos 	  debug_flush ();
999*56bb7041Schristos 	}
1000*56bb7041Schristos 
1001*56bb7041Schristos       if (write_prim ("+", 1) != 1)
1002*56bb7041Schristos 	return -1;
1003*56bb7041Schristos 
1004*56bb7041Schristos       if (remote_debug)
1005*56bb7041Schristos 	{
1006*56bb7041Schristos 	  debug_printf ("[sent ack]\n");
1007*56bb7041Schristos 	  debug_flush ();
1008*56bb7041Schristos 	}
1009*56bb7041Schristos     }
1010*56bb7041Schristos   else
1011*56bb7041Schristos     {
1012*56bb7041Schristos       if (remote_debug)
1013*56bb7041Schristos 	{
1014*56bb7041Schristos 	  debug_printf ("getpkt (\"%s\");  [no ack sent] \n", buf);
1015*56bb7041Schristos 	  debug_flush ();
1016*56bb7041Schristos 	}
1017*56bb7041Schristos     }
1018*56bb7041Schristos 
1019*56bb7041Schristos   /* The readchar above may have already read a '\003' out of the socket
1020*56bb7041Schristos      and moved it to the local buffer.  For example, when GDB sends
1021*56bb7041Schristos      vCont;c immediately followed by interrupt (see
1022*56bb7041Schristos      gdb.base/interrupt-noterm.exp).  As soon as we see the vCont;c, we'll
1023*56bb7041Schristos      resume the inferior and wait.  Since we've already moved the '\003'
1024*56bb7041Schristos      to the local buffer, SIGIO won't help.  In that case, if we don't
1025*56bb7041Schristos      check for interrupt after the vCont;c packet, the interrupt character
1026*56bb7041Schristos      would stay in the buffer unattended until after the next (unrelated)
1027*56bb7041Schristos      stop.  */
1028*56bb7041Schristos   while (readchar_bufcnt > 0 && *readchar_bufp == '\003')
1029*56bb7041Schristos     {
1030*56bb7041Schristos       /* Consume the interrupt character in the buffer.  */
1031*56bb7041Schristos       readchar ();
1032*56bb7041Schristos       the_target->request_interrupt ();
1033*56bb7041Schristos     }
1034*56bb7041Schristos 
1035*56bb7041Schristos   return bp - buf;
1036*56bb7041Schristos }
1037*56bb7041Schristos 
1038*56bb7041Schristos void
write_ok(char * buf)1039*56bb7041Schristos write_ok (char *buf)
1040*56bb7041Schristos {
1041*56bb7041Schristos   buf[0] = 'O';
1042*56bb7041Schristos   buf[1] = 'K';
1043*56bb7041Schristos   buf[2] = '\0';
1044*56bb7041Schristos }
1045*56bb7041Schristos 
1046*56bb7041Schristos void
write_enn(char * buf)1047*56bb7041Schristos write_enn (char *buf)
1048*56bb7041Schristos {
1049*56bb7041Schristos   /* Some day, we should define the meanings of the error codes... */
1050*56bb7041Schristos   buf[0] = 'E';
1051*56bb7041Schristos   buf[1] = '0';
1052*56bb7041Schristos   buf[2] = '1';
1053*56bb7041Schristos   buf[3] = '\0';
1054*56bb7041Schristos }
1055*56bb7041Schristos 
1056*56bb7041Schristos #endif
1057*56bb7041Schristos 
1058*56bb7041Schristos #ifndef IN_PROCESS_AGENT
1059*56bb7041Schristos 
1060*56bb7041Schristos static char *
outreg(struct regcache * regcache,int regno,char * buf)1061*56bb7041Schristos outreg (struct regcache *regcache, int regno, char *buf)
1062*56bb7041Schristos {
1063*56bb7041Schristos   if ((regno >> 12) != 0)
1064*56bb7041Schristos     *buf++ = tohex ((regno >> 12) & 0xf);
1065*56bb7041Schristos   if ((regno >> 8) != 0)
1066*56bb7041Schristos     *buf++ = tohex ((regno >> 8) & 0xf);
1067*56bb7041Schristos   *buf++ = tohex ((regno >> 4) & 0xf);
1068*56bb7041Schristos   *buf++ = tohex (regno & 0xf);
1069*56bb7041Schristos   *buf++ = ':';
1070*56bb7041Schristos   collect_register_as_string (regcache, regno, buf);
1071*56bb7041Schristos   buf += 2 * register_size (regcache->tdesc, regno);
1072*56bb7041Schristos   *buf++ = ';';
1073*56bb7041Schristos 
1074*56bb7041Schristos   return buf;
1075*56bb7041Schristos }
1076*56bb7041Schristos 
1077*56bb7041Schristos void
prepare_resume_reply(char * buf,ptid_t ptid,struct target_waitstatus * status)1078*56bb7041Schristos prepare_resume_reply (char *buf, ptid_t ptid,
1079*56bb7041Schristos 		      struct target_waitstatus *status)
1080*56bb7041Schristos {
1081*56bb7041Schristos   client_state &cs = get_client_state ();
1082*56bb7041Schristos   if (debug_threads)
1083*56bb7041Schristos     debug_printf ("Writing resume reply for %s:%d\n",
1084*56bb7041Schristos 		  target_pid_to_str (ptid), status->kind);
1085*56bb7041Schristos 
1086*56bb7041Schristos   switch (status->kind)
1087*56bb7041Schristos     {
1088*56bb7041Schristos     case TARGET_WAITKIND_STOPPED:
1089*56bb7041Schristos     case TARGET_WAITKIND_FORKED:
1090*56bb7041Schristos     case TARGET_WAITKIND_VFORKED:
1091*56bb7041Schristos     case TARGET_WAITKIND_VFORK_DONE:
1092*56bb7041Schristos     case TARGET_WAITKIND_EXECD:
1093*56bb7041Schristos     case TARGET_WAITKIND_THREAD_CREATED:
1094*56bb7041Schristos     case TARGET_WAITKIND_SYSCALL_ENTRY:
1095*56bb7041Schristos     case TARGET_WAITKIND_SYSCALL_RETURN:
1096*56bb7041Schristos       {
1097*56bb7041Schristos 	struct thread_info *saved_thread;
1098*56bb7041Schristos 	const char **regp;
1099*56bb7041Schristos 	struct regcache *regcache;
1100*56bb7041Schristos 
1101*56bb7041Schristos 	if ((status->kind == TARGET_WAITKIND_FORKED && cs.report_fork_events)
1102*56bb7041Schristos 	    || (status->kind == TARGET_WAITKIND_VFORKED
1103*56bb7041Schristos 		&& cs.report_vfork_events))
1104*56bb7041Schristos 	  {
1105*56bb7041Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1106*56bb7041Schristos 	    const char *event = (status->kind == TARGET_WAITKIND_FORKED
1107*56bb7041Schristos 				 ? "fork" : "vfork");
1108*56bb7041Schristos 
1109*56bb7041Schristos 	    sprintf (buf, "T%02x%s:", signal, event);
1110*56bb7041Schristos 	    buf += strlen (buf);
1111*56bb7041Schristos 	    buf = write_ptid (buf, status->value.related_pid);
1112*56bb7041Schristos 	    strcat (buf, ";");
1113*56bb7041Schristos 	  }
1114*56bb7041Schristos 	else if (status->kind == TARGET_WAITKIND_VFORK_DONE
1115*56bb7041Schristos 		 && cs.report_vfork_events)
1116*56bb7041Schristos 	  {
1117*56bb7041Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1118*56bb7041Schristos 
1119*56bb7041Schristos 	    sprintf (buf, "T%02xvforkdone:;", signal);
1120*56bb7041Schristos 	  }
1121*56bb7041Schristos 	else if (status->kind == TARGET_WAITKIND_EXECD && cs.report_exec_events)
1122*56bb7041Schristos 	  {
1123*56bb7041Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1124*56bb7041Schristos 	    const char *event = "exec";
1125*56bb7041Schristos 	    char hexified_pathname[PATH_MAX * 2];
1126*56bb7041Schristos 
1127*56bb7041Schristos 	    sprintf (buf, "T%02x%s:", signal, event);
1128*56bb7041Schristos 	    buf += strlen (buf);
1129*56bb7041Schristos 
1130*56bb7041Schristos 	    /* Encode pathname to hexified format.  */
1131*56bb7041Schristos 	    bin2hex ((const gdb_byte *) status->value.execd_pathname,
1132*56bb7041Schristos 		     hexified_pathname,
1133*56bb7041Schristos 		     strlen (status->value.execd_pathname));
1134*56bb7041Schristos 
1135*56bb7041Schristos 	    sprintf (buf, "%s;", hexified_pathname);
1136*56bb7041Schristos 	    xfree (status->value.execd_pathname);
1137*56bb7041Schristos 	    status->value.execd_pathname = NULL;
1138*56bb7041Schristos 	    buf += strlen (buf);
1139*56bb7041Schristos 	  }
1140*56bb7041Schristos 	else if (status->kind == TARGET_WAITKIND_THREAD_CREATED
1141*56bb7041Schristos 		 && cs.report_thread_events)
1142*56bb7041Schristos 	  {
1143*56bb7041Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1144*56bb7041Schristos 
1145*56bb7041Schristos 	    sprintf (buf, "T%02xcreate:;", signal);
1146*56bb7041Schristos 	  }
1147*56bb7041Schristos 	else if (status->kind == TARGET_WAITKIND_SYSCALL_ENTRY
1148*56bb7041Schristos 		 || status->kind == TARGET_WAITKIND_SYSCALL_RETURN)
1149*56bb7041Schristos 	  {
1150*56bb7041Schristos 	    enum gdb_signal signal = GDB_SIGNAL_TRAP;
1151*56bb7041Schristos 	    const char *event = (status->kind == TARGET_WAITKIND_SYSCALL_ENTRY
1152*56bb7041Schristos 				 ? "syscall_entry" : "syscall_return");
1153*56bb7041Schristos 
1154*56bb7041Schristos 	    sprintf (buf, "T%02x%s:%x;", signal, event,
1155*56bb7041Schristos 		     status->value.syscall_number);
1156*56bb7041Schristos 	  }
1157*56bb7041Schristos 	else
1158*56bb7041Schristos 	  sprintf (buf, "T%02x", status->value.sig);
1159*56bb7041Schristos 
1160*56bb7041Schristos 	if (disable_packet_T)
1161*56bb7041Schristos 	  {
1162*56bb7041Schristos 	    /* This is a bit (OK, a lot) of a kludge, however, this isn't
1163*56bb7041Schristos 	       really a user feature, but exists only so GDB can use the
1164*56bb7041Schristos 	       gdbserver to test handling of the 'S' stop reply packet, so
1165*56bb7041Schristos 	       we would rather this code be as simple as possible.
1166*56bb7041Schristos 
1167*56bb7041Schristos 	       By this point we've started to build the 'T' stop packet,
1168*56bb7041Schristos 	       and it should look like 'Txx....' where 'x' is a hex digit.
1169*56bb7041Schristos 	       An 'S' stop packet always looks like 'Sxx', so all we do
1170*56bb7041Schristos 	       here is convert the buffer from a T packet to an S packet
1171*56bb7041Schristos 	       and the avoid adding any extra content by breaking out.  */
1172*56bb7041Schristos 	    gdb_assert (*buf == 'T');
1173*56bb7041Schristos 	    gdb_assert (isxdigit (*(buf + 1)));
1174*56bb7041Schristos 	    gdb_assert (isxdigit (*(buf + 2)));
1175*56bb7041Schristos 	    *buf = 'S';
1176*56bb7041Schristos 	    *(buf + 3) = '\0';
1177*56bb7041Schristos 	    break;
1178*56bb7041Schristos 	  }
1179*56bb7041Schristos 
1180*56bb7041Schristos 	buf += strlen (buf);
1181*56bb7041Schristos 
1182*56bb7041Schristos 	saved_thread = current_thread;
1183*56bb7041Schristos 
1184*56bb7041Schristos 	switch_to_thread (the_target, ptid);
1185*56bb7041Schristos 
1186*56bb7041Schristos 	regp = current_target_desc ()->expedite_regs;
1187*56bb7041Schristos 
1188*56bb7041Schristos 	regcache = get_thread_regcache (current_thread, 1);
1189*56bb7041Schristos 
1190*56bb7041Schristos 	if (the_target->stopped_by_watchpoint ())
1191*56bb7041Schristos 	  {
1192*56bb7041Schristos 	    CORE_ADDR addr;
1193*56bb7041Schristos 	    int i;
1194*56bb7041Schristos 
1195*56bb7041Schristos 	    memcpy (buf, "watch:", 6);
1196*56bb7041Schristos 	    buf += 6;
1197*56bb7041Schristos 
1198*56bb7041Schristos 	    addr = the_target->stopped_data_address ();
1199*56bb7041Schristos 
1200*56bb7041Schristos 	    /* Convert each byte of the address into two hexadecimal
1201*56bb7041Schristos 	       chars.  Note that we take sizeof (void *) instead of
1202*56bb7041Schristos 	       sizeof (addr); this is to avoid sending a 64-bit
1203*56bb7041Schristos 	       address to a 32-bit GDB.  */
1204*56bb7041Schristos 	    for (i = sizeof (void *) * 2; i > 0; i--)
1205*56bb7041Schristos 	      *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1206*56bb7041Schristos 	    *buf++ = ';';
1207*56bb7041Schristos 	  }
1208*56bb7041Schristos 	else if (cs.swbreak_feature && target_stopped_by_sw_breakpoint ())
1209*56bb7041Schristos 	  {
1210*56bb7041Schristos 	    sprintf (buf, "swbreak:;");
1211*56bb7041Schristos 	    buf += strlen (buf);
1212*56bb7041Schristos 	  }
1213*56bb7041Schristos 	else if (cs.hwbreak_feature && target_stopped_by_hw_breakpoint ())
1214*56bb7041Schristos 	  {
1215*56bb7041Schristos 	    sprintf (buf, "hwbreak:;");
1216*56bb7041Schristos 	    buf += strlen (buf);
1217*56bb7041Schristos 	  }
1218*56bb7041Schristos 
1219*56bb7041Schristos 	while (*regp)
1220*56bb7041Schristos 	  {
1221*56bb7041Schristos 	    buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
1222*56bb7041Schristos 	    regp ++;
1223*56bb7041Schristos 	  }
1224*56bb7041Schristos 	*buf = '\0';
1225*56bb7041Schristos 
1226*56bb7041Schristos 	/* Formerly, if the debugger had not used any thread features
1227*56bb7041Schristos 	   we would not burden it with a thread status response.  This
1228*56bb7041Schristos 	   was for the benefit of GDB 4.13 and older.  However, in
1229*56bb7041Schristos 	   recent GDB versions the check (``if (cont_thread != 0)'')
1230*56bb7041Schristos 	   does not have the desired effect because of sillyness in
1231*56bb7041Schristos 	   the way that the remote protocol handles specifying a
1232*56bb7041Schristos 	   thread.  Since thread support relies on qSymbol support
1233*56bb7041Schristos 	   anyway, assume GDB can handle threads.  */
1234*56bb7041Schristos 
1235*56bb7041Schristos 	if (using_threads && !disable_packet_Tthread)
1236*56bb7041Schristos 	  {
1237*56bb7041Schristos 	    /* This if (1) ought to be unnecessary.  But remote_wait
1238*56bb7041Schristos 	       in GDB will claim this event belongs to inferior_ptid
1239*56bb7041Schristos 	       if we do not specify a thread, and there's no way for
1240*56bb7041Schristos 	       gdbserver to know what inferior_ptid is.  */
1241*56bb7041Schristos 	    if (1 || cs.general_thread != ptid)
1242*56bb7041Schristos 	      {
1243*56bb7041Schristos 		int core = -1;
1244*56bb7041Schristos 		/* In non-stop, don't change the general thread behind
1245*56bb7041Schristos 		   GDB's back.  */
1246*56bb7041Schristos 		if (!non_stop)
1247*56bb7041Schristos 		  cs.general_thread = ptid;
1248*56bb7041Schristos 		sprintf (buf, "thread:");
1249*56bb7041Schristos 		buf += strlen (buf);
1250*56bb7041Schristos 		buf = write_ptid (buf, ptid);
1251*56bb7041Schristos 		strcat (buf, ";");
1252*56bb7041Schristos 		buf += strlen (buf);
1253*56bb7041Schristos 
1254*56bb7041Schristos 		core = target_core_of_thread (ptid);
1255*56bb7041Schristos 
1256*56bb7041Schristos 		if (core != -1)
1257*56bb7041Schristos 		  {
1258*56bb7041Schristos 		    sprintf (buf, "core:");
1259*56bb7041Schristos 		    buf += strlen (buf);
1260*56bb7041Schristos 		    sprintf (buf, "%x", core);
1261*56bb7041Schristos 		    strcat (buf, ";");
1262*56bb7041Schristos 		    buf += strlen (buf);
1263*56bb7041Schristos 		  }
1264*56bb7041Schristos 	      }
1265*56bb7041Schristos 	  }
1266*56bb7041Schristos 
1267*56bb7041Schristos 	if (dlls_changed)
1268*56bb7041Schristos 	  {
1269*56bb7041Schristos 	    strcpy (buf, "library:;");
1270*56bb7041Schristos 	    buf += strlen (buf);
1271*56bb7041Schristos 	    dlls_changed = 0;
1272*56bb7041Schristos 	  }
1273*56bb7041Schristos 
1274*56bb7041Schristos 	current_thread = saved_thread;
1275*56bb7041Schristos       }
1276*56bb7041Schristos       break;
1277*56bb7041Schristos     case TARGET_WAITKIND_EXITED:
1278*56bb7041Schristos       if (cs.multi_process)
1279*56bb7041Schristos 	sprintf (buf, "W%x;process:%x",
1280*56bb7041Schristos 		 status->value.integer, ptid.pid ());
1281*56bb7041Schristos       else
1282*56bb7041Schristos 	sprintf (buf, "W%02x", status->value.integer);
1283*56bb7041Schristos       break;
1284*56bb7041Schristos     case TARGET_WAITKIND_SIGNALLED:
1285*56bb7041Schristos       if (cs.multi_process)
1286*56bb7041Schristos 	sprintf (buf, "X%x;process:%x",
1287*56bb7041Schristos 		 status->value.sig, ptid.pid ());
1288*56bb7041Schristos       else
1289*56bb7041Schristos 	sprintf (buf, "X%02x", status->value.sig);
1290*56bb7041Schristos       break;
1291*56bb7041Schristos     case TARGET_WAITKIND_THREAD_EXITED:
1292*56bb7041Schristos       sprintf (buf, "w%x;", status->value.integer);
1293*56bb7041Schristos       buf += strlen (buf);
1294*56bb7041Schristos       buf = write_ptid (buf, ptid);
1295*56bb7041Schristos       break;
1296*56bb7041Schristos     case TARGET_WAITKIND_NO_RESUMED:
1297*56bb7041Schristos       sprintf (buf, "N");
1298*56bb7041Schristos       break;
1299*56bb7041Schristos     default:
1300*56bb7041Schristos       error ("unhandled waitkind");
1301*56bb7041Schristos       break;
1302*56bb7041Schristos     }
1303*56bb7041Schristos }
1304*56bb7041Schristos 
1305*56bb7041Schristos void
decode_m_packet(char * from,CORE_ADDR * mem_addr_ptr,unsigned int * len_ptr)1306*56bb7041Schristos decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
1307*56bb7041Schristos {
1308*56bb7041Schristos   int i = 0, j = 0;
1309*56bb7041Schristos   char ch;
1310*56bb7041Schristos   *mem_addr_ptr = *len_ptr = 0;
1311*56bb7041Schristos 
1312*56bb7041Schristos   while ((ch = from[i++]) != ',')
1313*56bb7041Schristos     {
1314*56bb7041Schristos       *mem_addr_ptr = *mem_addr_ptr << 4;
1315*56bb7041Schristos       *mem_addr_ptr |= fromhex (ch) & 0x0f;
1316*56bb7041Schristos     }
1317*56bb7041Schristos 
1318*56bb7041Schristos   for (j = 0; j < 4; j++)
1319*56bb7041Schristos     {
1320*56bb7041Schristos       if ((ch = from[i++]) == 0)
1321*56bb7041Schristos 	break;
1322*56bb7041Schristos       *len_ptr = *len_ptr << 4;
1323*56bb7041Schristos       *len_ptr |= fromhex (ch) & 0x0f;
1324*56bb7041Schristos     }
1325*56bb7041Schristos }
1326*56bb7041Schristos 
1327*56bb7041Schristos void
decode_M_packet(char * from,CORE_ADDR * mem_addr_ptr,unsigned int * len_ptr,unsigned char ** to_p)1328*56bb7041Schristos decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
1329*56bb7041Schristos 		 unsigned char **to_p)
1330*56bb7041Schristos {
1331*56bb7041Schristos   int i = 0;
1332*56bb7041Schristos   char ch;
1333*56bb7041Schristos   *mem_addr_ptr = *len_ptr = 0;
1334*56bb7041Schristos 
1335*56bb7041Schristos   while ((ch = from[i++]) != ',')
1336*56bb7041Schristos     {
1337*56bb7041Schristos       *mem_addr_ptr = *mem_addr_ptr << 4;
1338*56bb7041Schristos       *mem_addr_ptr |= fromhex (ch) & 0x0f;
1339*56bb7041Schristos     }
1340*56bb7041Schristos 
1341*56bb7041Schristos   while ((ch = from[i++]) != ':')
1342*56bb7041Schristos     {
1343*56bb7041Schristos       *len_ptr = *len_ptr << 4;
1344*56bb7041Schristos       *len_ptr |= fromhex (ch) & 0x0f;
1345*56bb7041Schristos     }
1346*56bb7041Schristos 
1347*56bb7041Schristos   if (*to_p == NULL)
1348*56bb7041Schristos     *to_p = (unsigned char *) xmalloc (*len_ptr);
1349*56bb7041Schristos 
1350*56bb7041Schristos   hex2bin (&from[i++], *to_p, *len_ptr);
1351*56bb7041Schristos }
1352*56bb7041Schristos 
1353*56bb7041Schristos int
decode_X_packet(char * from,int packet_len,CORE_ADDR * mem_addr_ptr,unsigned int * len_ptr,unsigned char ** to_p)1354*56bb7041Schristos decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1355*56bb7041Schristos 		 unsigned int *len_ptr, unsigned char **to_p)
1356*56bb7041Schristos {
1357*56bb7041Schristos   int i = 0;
1358*56bb7041Schristos   char ch;
1359*56bb7041Schristos   *mem_addr_ptr = *len_ptr = 0;
1360*56bb7041Schristos 
1361*56bb7041Schristos   while ((ch = from[i++]) != ',')
1362*56bb7041Schristos     {
1363*56bb7041Schristos       *mem_addr_ptr = *mem_addr_ptr << 4;
1364*56bb7041Schristos       *mem_addr_ptr |= fromhex (ch) & 0x0f;
1365*56bb7041Schristos     }
1366*56bb7041Schristos 
1367*56bb7041Schristos   while ((ch = from[i++]) != ':')
1368*56bb7041Schristos     {
1369*56bb7041Schristos       *len_ptr = *len_ptr << 4;
1370*56bb7041Schristos       *len_ptr |= fromhex (ch) & 0x0f;
1371*56bb7041Schristos     }
1372*56bb7041Schristos 
1373*56bb7041Schristos   if (*to_p == NULL)
1374*56bb7041Schristos     *to_p = (unsigned char *) xmalloc (*len_ptr);
1375*56bb7041Schristos 
1376*56bb7041Schristos   if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1377*56bb7041Schristos 			     *to_p, *len_ptr) != *len_ptr)
1378*56bb7041Schristos     return -1;
1379*56bb7041Schristos 
1380*56bb7041Schristos   return 0;
1381*56bb7041Schristos }
1382*56bb7041Schristos 
1383*56bb7041Schristos /* Decode a qXfer write request.  */
1384*56bb7041Schristos 
1385*56bb7041Schristos int
decode_xfer_write(char * buf,int packet_len,CORE_ADDR * offset,unsigned int * len,unsigned char * data)1386*56bb7041Schristos decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
1387*56bb7041Schristos 		   unsigned int *len, unsigned char *data)
1388*56bb7041Schristos {
1389*56bb7041Schristos   char ch;
1390*56bb7041Schristos   char *b = buf;
1391*56bb7041Schristos 
1392*56bb7041Schristos   /* Extract the offset.  */
1393*56bb7041Schristos   *offset = 0;
1394*56bb7041Schristos   while ((ch = *buf++) != ':')
1395*56bb7041Schristos     {
1396*56bb7041Schristos       *offset = *offset << 4;
1397*56bb7041Schristos       *offset |= fromhex (ch) & 0x0f;
1398*56bb7041Schristos     }
1399*56bb7041Schristos 
1400*56bb7041Schristos   /* Get encoded data.  */
1401*56bb7041Schristos   packet_len -= buf - b;
1402*56bb7041Schristos   *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1403*56bb7041Schristos 				data, packet_len);
1404*56bb7041Schristos   return 0;
1405*56bb7041Schristos }
1406*56bb7041Schristos 
1407*56bb7041Schristos /* Decode the parameters of a qSearch:memory packet.  */
1408*56bb7041Schristos 
1409*56bb7041Schristos int
decode_search_memory_packet(const char * buf,int packet_len,CORE_ADDR * start_addrp,CORE_ADDR * search_space_lenp,gdb_byte * pattern,unsigned int * pattern_lenp)1410*56bb7041Schristos decode_search_memory_packet (const char *buf, int packet_len,
1411*56bb7041Schristos 			     CORE_ADDR *start_addrp,
1412*56bb7041Schristos 			     CORE_ADDR *search_space_lenp,
1413*56bb7041Schristos 			     gdb_byte *pattern, unsigned int *pattern_lenp)
1414*56bb7041Schristos {
1415*56bb7041Schristos   const char *p = buf;
1416*56bb7041Schristos 
1417*56bb7041Schristos   p = decode_address_to_semicolon (start_addrp, p);
1418*56bb7041Schristos   p = decode_address_to_semicolon (search_space_lenp, p);
1419*56bb7041Schristos   packet_len -= p - buf;
1420*56bb7041Schristos   *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1421*56bb7041Schristos 					 pattern, packet_len);
1422*56bb7041Schristos   return 0;
1423*56bb7041Schristos }
1424*56bb7041Schristos 
1425*56bb7041Schristos static void
free_sym_cache(struct sym_cache * sym)1426*56bb7041Schristos free_sym_cache (struct sym_cache *sym)
1427*56bb7041Schristos {
1428*56bb7041Schristos   if (sym != NULL)
1429*56bb7041Schristos     {
1430*56bb7041Schristos       free (sym->name);
1431*56bb7041Schristos       free (sym);
1432*56bb7041Schristos     }
1433*56bb7041Schristos }
1434*56bb7041Schristos 
1435*56bb7041Schristos void
clear_symbol_cache(struct sym_cache ** symcache_p)1436*56bb7041Schristos clear_symbol_cache (struct sym_cache **symcache_p)
1437*56bb7041Schristos {
1438*56bb7041Schristos   struct sym_cache *sym, *next;
1439*56bb7041Schristos 
1440*56bb7041Schristos   /* Check the cache first.  */
1441*56bb7041Schristos   for (sym = *symcache_p; sym; sym = next)
1442*56bb7041Schristos     {
1443*56bb7041Schristos       next = sym->next;
1444*56bb7041Schristos       free_sym_cache (sym);
1445*56bb7041Schristos     }
1446*56bb7041Schristos 
1447*56bb7041Schristos   *symcache_p = NULL;
1448*56bb7041Schristos }
1449*56bb7041Schristos 
1450*56bb7041Schristos /* Get the address of NAME, and return it in ADDRP if found.  if
1451*56bb7041Schristos    MAY_ASK_GDB is false, assume symbol cache misses are failures.
1452*56bb7041Schristos    Returns 1 if the symbol is found, 0 if it is not, -1 on error.  */
1453*56bb7041Schristos 
1454*56bb7041Schristos int
look_up_one_symbol(const char * name,CORE_ADDR * addrp,int may_ask_gdb)1455*56bb7041Schristos look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
1456*56bb7041Schristos {
1457*56bb7041Schristos   client_state &cs = get_client_state ();
1458*56bb7041Schristos   char *p, *q;
1459*56bb7041Schristos   int len;
1460*56bb7041Schristos   struct sym_cache *sym;
1461*56bb7041Schristos   struct process_info *proc;
1462*56bb7041Schristos 
1463*56bb7041Schristos   proc = current_process ();
1464*56bb7041Schristos 
1465*56bb7041Schristos   /* Check the cache first.  */
1466*56bb7041Schristos   for (sym = proc->symbol_cache; sym; sym = sym->next)
1467*56bb7041Schristos     if (strcmp (name, sym->name) == 0)
1468*56bb7041Schristos       {
1469*56bb7041Schristos 	*addrp = sym->addr;
1470*56bb7041Schristos 	return 1;
1471*56bb7041Schristos       }
1472*56bb7041Schristos 
1473*56bb7041Schristos   /* It might not be an appropriate time to look up a symbol,
1474*56bb7041Schristos      e.g. while we're trying to fetch registers.  */
1475*56bb7041Schristos   if (!may_ask_gdb)
1476*56bb7041Schristos     return 0;
1477*56bb7041Schristos 
1478*56bb7041Schristos   /* Send the request.  */
1479*56bb7041Schristos   strcpy (cs.own_buf, "qSymbol:");
1480*56bb7041Schristos   bin2hex ((const gdb_byte *) name, cs.own_buf + strlen ("qSymbol:"),
1481*56bb7041Schristos 	  strlen (name));
1482*56bb7041Schristos   if (putpkt (cs.own_buf) < 0)
1483*56bb7041Schristos     return -1;
1484*56bb7041Schristos 
1485*56bb7041Schristos   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
1486*56bb7041Schristos   len = getpkt (cs.own_buf);
1487*56bb7041Schristos   if (len < 0)
1488*56bb7041Schristos     return -1;
1489*56bb7041Schristos 
1490*56bb7041Schristos   /* We ought to handle pretty much any packet at this point while we
1491*56bb7041Schristos      wait for the qSymbol "response".  That requires re-entering the
1492*56bb7041Schristos      main loop.  For now, this is an adequate approximation; allow
1493*56bb7041Schristos      GDB to read from memory and handle 'v' packets (for vFile transfers)
1494*56bb7041Schristos      while it figures out the address of the symbol.  */
1495*56bb7041Schristos   while (1)
1496*56bb7041Schristos     {
1497*56bb7041Schristos       if (cs.own_buf[0] == 'm')
1498*56bb7041Schristos 	{
1499*56bb7041Schristos 	  CORE_ADDR mem_addr;
1500*56bb7041Schristos 	  unsigned char *mem_buf;
1501*56bb7041Schristos 	  unsigned int mem_len;
1502*56bb7041Schristos 
1503*56bb7041Schristos 	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1504*56bb7041Schristos 	  mem_buf = (unsigned char *) xmalloc (mem_len);
1505*56bb7041Schristos 	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1506*56bb7041Schristos 	    bin2hex (mem_buf, cs.own_buf, mem_len);
1507*56bb7041Schristos 	  else
1508*56bb7041Schristos 	    write_enn (cs.own_buf);
1509*56bb7041Schristos 	  free (mem_buf);
1510*56bb7041Schristos 	  if (putpkt (cs.own_buf) < 0)
1511*56bb7041Schristos 	    return -1;
1512*56bb7041Schristos 	}
1513*56bb7041Schristos       else if (cs.own_buf[0] == 'v')
1514*56bb7041Schristos 	{
1515*56bb7041Schristos 	  int new_len = -1;
1516*56bb7041Schristos 	  handle_v_requests (cs.own_buf, len, &new_len);
1517*56bb7041Schristos 	  if (new_len != -1)
1518*56bb7041Schristos 	    putpkt_binary (cs.own_buf, new_len);
1519*56bb7041Schristos 	  else
1520*56bb7041Schristos 	    putpkt (cs.own_buf);
1521*56bb7041Schristos 	}
1522*56bb7041Schristos       else
1523*56bb7041Schristos 	break;
1524*56bb7041Schristos       len = getpkt (cs.own_buf);
1525*56bb7041Schristos       if (len < 0)
1526*56bb7041Schristos 	return -1;
1527*56bb7041Schristos     }
1528*56bb7041Schristos 
1529*56bb7041Schristos   if (!startswith (cs.own_buf, "qSymbol:"))
1530*56bb7041Schristos     {
1531*56bb7041Schristos       warning ("Malformed response to qSymbol, ignoring: %s", cs.own_buf);
1532*56bb7041Schristos       return -1;
1533*56bb7041Schristos     }
1534*56bb7041Schristos 
1535*56bb7041Schristos   p = cs.own_buf + strlen ("qSymbol:");
1536*56bb7041Schristos   q = p;
1537*56bb7041Schristos   while (*q && *q != ':')
1538*56bb7041Schristos     q++;
1539*56bb7041Schristos 
1540*56bb7041Schristos   /* Make sure we found a value for the symbol.  */
1541*56bb7041Schristos   if (p == q || *q == '\0')
1542*56bb7041Schristos     return 0;
1543*56bb7041Schristos 
1544*56bb7041Schristos   decode_address (addrp, p, q - p);
1545*56bb7041Schristos 
1546*56bb7041Schristos   /* Save the symbol in our cache.  */
1547*56bb7041Schristos   sym = XNEW (struct sym_cache);
1548*56bb7041Schristos   sym->name = xstrdup (name);
1549*56bb7041Schristos   sym->addr = *addrp;
1550*56bb7041Schristos   sym->next = proc->symbol_cache;
1551*56bb7041Schristos   proc->symbol_cache = sym;
1552*56bb7041Schristos 
1553*56bb7041Schristos   return 1;
1554*56bb7041Schristos }
1555*56bb7041Schristos 
1556*56bb7041Schristos /* Relocate an instruction to execute at a different address.  OLDLOC
1557*56bb7041Schristos    is the address in the inferior memory where the instruction to
1558*56bb7041Schristos    relocate is currently at.  On input, TO points to the destination
1559*56bb7041Schristos    where we want the instruction to be copied (and possibly adjusted)
1560*56bb7041Schristos    to.  On output, it points to one past the end of the resulting
1561*56bb7041Schristos    instruction(s).  The effect of executing the instruction at TO
1562*56bb7041Schristos    shall be the same as if executing it at OLDLOC.  For example, call
1563*56bb7041Schristos    instructions that implicitly push the return address on the stack
1564*56bb7041Schristos    should be adjusted to return to the instruction after OLDLOC;
1565*56bb7041Schristos    relative branches, and other PC-relative instructions need the
1566*56bb7041Schristos    offset adjusted; etc.  Returns 0 on success, -1 on failure.  */
1567*56bb7041Schristos 
1568*56bb7041Schristos int
relocate_instruction(CORE_ADDR * to,CORE_ADDR oldloc)1569*56bb7041Schristos relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1570*56bb7041Schristos {
1571*56bb7041Schristos   client_state &cs = get_client_state ();
1572*56bb7041Schristos   int len;
1573*56bb7041Schristos   ULONGEST written = 0;
1574*56bb7041Schristos 
1575*56bb7041Schristos   /* Send the request.  */
1576*56bb7041Schristos   sprintf (cs.own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1577*56bb7041Schristos 	   paddress (*to));
1578*56bb7041Schristos   if (putpkt (cs.own_buf) < 0)
1579*56bb7041Schristos     return -1;
1580*56bb7041Schristos 
1581*56bb7041Schristos   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
1582*56bb7041Schristos   len = getpkt (cs.own_buf);
1583*56bb7041Schristos   if (len < 0)
1584*56bb7041Schristos     return -1;
1585*56bb7041Schristos 
1586*56bb7041Schristos   /* We ought to handle pretty much any packet at this point while we
1587*56bb7041Schristos      wait for the qRelocInsn "response".  That requires re-entering
1588*56bb7041Schristos      the main loop.  For now, this is an adequate approximation; allow
1589*56bb7041Schristos      GDB to access memory.  */
1590*56bb7041Schristos   while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M' || cs.own_buf[0] == 'X')
1591*56bb7041Schristos     {
1592*56bb7041Schristos       CORE_ADDR mem_addr;
1593*56bb7041Schristos       unsigned char *mem_buf = NULL;
1594*56bb7041Schristos       unsigned int mem_len;
1595*56bb7041Schristos 
1596*56bb7041Schristos       if (cs.own_buf[0] == 'm')
1597*56bb7041Schristos 	{
1598*56bb7041Schristos 	  decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1599*56bb7041Schristos 	  mem_buf = (unsigned char *) xmalloc (mem_len);
1600*56bb7041Schristos 	  if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1601*56bb7041Schristos 	    bin2hex (mem_buf, cs.own_buf, mem_len);
1602*56bb7041Schristos 	  else
1603*56bb7041Schristos 	    write_enn (cs.own_buf);
1604*56bb7041Schristos 	}
1605*56bb7041Schristos       else if (cs.own_buf[0] == 'X')
1606*56bb7041Schristos 	{
1607*56bb7041Schristos 	  if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
1608*56bb7041Schristos 			       &mem_len, &mem_buf) < 0
1609*56bb7041Schristos 	      || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
1610*56bb7041Schristos 	    write_enn (cs.own_buf);
1611*56bb7041Schristos 	  else
1612*56bb7041Schristos 	    write_ok (cs.own_buf);
1613*56bb7041Schristos 	}
1614*56bb7041Schristos       else
1615*56bb7041Schristos 	{
1616*56bb7041Schristos 	  decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
1617*56bb7041Schristos 	  if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
1618*56bb7041Schristos 	    write_ok (cs.own_buf);
1619*56bb7041Schristos 	  else
1620*56bb7041Schristos 	    write_enn (cs.own_buf);
1621*56bb7041Schristos 	}
1622*56bb7041Schristos       free (mem_buf);
1623*56bb7041Schristos       if (putpkt (cs.own_buf) < 0)
1624*56bb7041Schristos 	return -1;
1625*56bb7041Schristos       len = getpkt (cs.own_buf);
1626*56bb7041Schristos       if (len < 0)
1627*56bb7041Schristos 	return -1;
1628*56bb7041Schristos     }
1629*56bb7041Schristos 
1630*56bb7041Schristos   if (cs.own_buf[0] == 'E')
1631*56bb7041Schristos     {
1632*56bb7041Schristos       warning ("An error occurred while relocating an instruction: %s",
1633*56bb7041Schristos 	       cs.own_buf);
1634*56bb7041Schristos       return -1;
1635*56bb7041Schristos     }
1636*56bb7041Schristos 
1637*56bb7041Schristos   if (!startswith (cs.own_buf, "qRelocInsn:"))
1638*56bb7041Schristos     {
1639*56bb7041Schristos       warning ("Malformed response to qRelocInsn, ignoring: %s",
1640*56bb7041Schristos 	       cs.own_buf);
1641*56bb7041Schristos       return -1;
1642*56bb7041Schristos     }
1643*56bb7041Schristos 
1644*56bb7041Schristos   unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
1645*56bb7041Schristos 
1646*56bb7041Schristos   *to += written;
1647*56bb7041Schristos   return 0;
1648*56bb7041Schristos }
1649*56bb7041Schristos 
1650*56bb7041Schristos void
monitor_output(const char * msg)1651*56bb7041Schristos monitor_output (const char *msg)
1652*56bb7041Schristos {
1653*56bb7041Schristos   int len = strlen (msg);
1654*56bb7041Schristos   char *buf = (char *) xmalloc (len * 2 + 2);
1655*56bb7041Schristos 
1656*56bb7041Schristos   buf[0] = 'O';
1657*56bb7041Schristos   bin2hex ((const gdb_byte *) msg, buf + 1, len);
1658*56bb7041Schristos 
1659*56bb7041Schristos   putpkt (buf);
1660*56bb7041Schristos   free (buf);
1661*56bb7041Schristos }
1662*56bb7041Schristos 
1663*56bb7041Schristos #endif
1664