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