]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/remote-utils.c
Implement IPv6 support for GDB/gdbserver
[thirdparty/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "server.h"
20 #if HAVE_TERMIOS_H
21 #include <termios.h>
22 #endif
23 #include "target.h"
24 #include "gdbthread.h"
25 #include "tdesc.h"
26 #include "dll.h"
27 #include "rsp-low.h"
28 #include "gdbthread.h"
29 #include "netstuff.h"
30 #include "filestuff.h"
31 #include <ctype.h>
32 #if HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h>
34 #endif
35 #if HAVE_SYS_FILE_H
36 #include <sys/file.h>
37 #endif
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
44 #if HAVE_NETDB_H
45 #include <netdb.h>
46 #endif
47 #if HAVE_NETINET_TCP_H
48 #include <netinet/tcp.h>
49 #endif
50 #if HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
52 #endif
53 #if HAVE_SIGNAL_H
54 #include <signal.h>
55 #endif
56 #if HAVE_FCNTL_H
57 #include <fcntl.h>
58 #endif
59 #include "gdb_sys_time.h"
60 #include <unistd.h>
61 #if HAVE_ARPA_INET_H
62 #include <arpa/inet.h>
63 #endif
64 #include <sys/stat.h>
65
66 #if USE_WIN32API
67 #include <winsock2.h>
68 #include <wspiapi.h>
69 #endif
70
71 #if __QNX__
72 #include <sys/iomgr.h>
73 #endif /* __QNX__ */
74
75 #ifndef HAVE_SOCKLEN_T
76 typedef int socklen_t;
77 #endif
78
79 #ifndef IN_PROCESS_AGENT
80
81 #if USE_WIN32API
82 # define INVALID_DESCRIPTOR INVALID_SOCKET
83 #else
84 # define INVALID_DESCRIPTOR -1
85 #endif
86
87 /* Extra value for readchar_callback. */
88 enum {
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. */
95 static int readchar_callback = NOT_SCHEDULED;
96
97 static int readchar (void);
98 static void reset_readchar (void);
99 static void reschedule (void);
100
101 /* A cache entry for a successfully looked-up symbol. */
102 struct sym_cache
103 {
104 char *name;
105 CORE_ADDR addr;
106 struct sym_cache *next;
107 };
108
109 int remote_debug = 0;
110 struct ui_file *gdb_stdlog;
111
112 static int remote_is_stdio = 0;
113
114 static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR;
115 static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR;
116
117 /* FIXME headerize? */
118 extern int using_threads;
119 extern int debug_threads;
120
121 #ifdef USE_WIN32API
122 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
123 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
124 #endif
125
126 int
127 gdb_connected (void)
128 {
129 return remote_desc != INVALID_DESCRIPTOR;
130 }
131
132 /* Return true if the remote connection is over stdio. */
133
134 int
135 remote_connection_is_stdio (void)
136 {
137 return remote_is_stdio;
138 }
139
140 static void
141 enable_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
154 static int
155 handle_accept_event (int err, gdb_client_data client_data)
156 {
157 struct sockaddr_storage sockaddr;
158 socklen_t len = sizeof (sockaddr);
159
160 if (debug_threads)
161 debug_printf ("handling possible accept event\n");
162
163 remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &len);
164 if (remote_desc == -1)
165 perror_with_name ("Accept failed");
166
167 /* Enable TCP keep alive process. */
168 socklen_t tmp = 1;
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
179 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
180 exits when the remote side dies. */
181 #endif
182
183 if (run_once)
184 {
185 #ifndef USE_WIN32API
186 close (listen_desc); /* No longer need this */
187 #else
188 closesocket (listen_desc); /* No longer need this */
189 #endif
190 }
191
192 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
193 descriptor open for add_file_handler to wait for a new connection. */
194 delete_file_handler (listen_desc);
195
196 /* Convert IP address to string. */
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);
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
227 /* Prepare for a later connection to a remote debugger.
228 NAME is the filename used for communication. */
229
230 void
231 remote_prepare (const char *name)
232 {
233 client_state &cs = get_client_state ();
234 const char *port_str;
235 #ifdef USE_WIN32API
236 static int winsock_initialized;
237 #endif
238 socklen_t tmp;
239
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;
247 cs.transport_is_reliable = 1;
248 return;
249 }
250
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 ())
265 {
266 cs.transport_is_reliable = 0;
267 return;
268 }
269
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
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)
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
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 }
319
320 if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
321 perror_with_name ("Can't bind address");
322
323 if (listen (listen_desc, 1) != 0)
324 perror_with_name ("Can't listen on socket");
325
326 cs.transport_is_reliable = 1;
327 }
328
329 /* Open a connection to a remote debugger.
330 NAME is the filename used for communication. */
331
332 void
333 remote_open (const char *name)
334 {
335 const char *port_str;
336
337 port_str = strchr (name, ':');
338 #ifdef USE_WIN32API
339 if (port_str == NULL)
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)
358 {
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
370 if (remote_desc < 0)
371 perror_with_name ("Could not open remote device");
372
373 #if HAVE_TERMIOS_H
374 {
375 struct termios termios;
376 tcgetattr (remote_desc, &termios);
377
378 termios.c_iflag = 0;
379 termios.c_oflag = 0;
380 termios.c_lflag = 0;
381 termios.c_cflag &= ~(CSIZE | PARENB);
382 termios.c_cflag |= CLOCAL | CS8;
383 termios.c_cc[VMIN] = 1;
384 termios.c_cc[VTIME] = 0;
385
386 tcsetattr (remote_desc, TCSANOW, &termios);
387 }
388 #endif
389
390 fprintf (stderr, "Remote debugging using %s\n", name);
391
392 enable_async_notification (remote_desc);
393
394 /* Register the event loop handler. */
395 add_file_handler (remote_desc, handle_serial_event, NULL);
396 }
397 #endif /* USE_WIN32API */
398 else
399 {
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)
405 perror_with_name ("Can't determine port");
406
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
418 fflush (stderr);
419
420 /* Register the event loop handler. */
421 add_file_handler (listen_desc, handle_accept_event, NULL);
422 }
423 }
424
425 void
426 remote_close (void)
427 {
428 delete_file_handler (remote_desc);
429
430 disable_async_io ();
431
432 #ifdef USE_WIN32API
433 closesocket (remote_desc);
434 #else
435 if (! remote_connection_is_stdio ())
436 close (remote_desc);
437 #endif
438 remote_desc = INVALID_DESCRIPTOR;
439
440 reset_readchar ();
441 }
442
443 #endif
444
445 #ifndef IN_PROCESS_AGENT
446
447 void
448 decode_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
464 const char *
465 decode_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
480 #endif
481
482 #ifndef IN_PROCESS_AGENT
483
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
488 static int
489 try_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
528 #endif
529
530 #ifndef IN_PROCESS_AGENT
531
532 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
533
534 char *
535 write_ptid (char *buf, ptid_t ptid)
536 {
537 client_state &cs = get_client_state ();
538 int pid, tid;
539
540 if (cs.multi_process)
541 {
542 pid = ptid.pid ();
543 if (pid < 0)
544 buf += sprintf (buf, "p-%x.", -pid);
545 else
546 buf += sprintf (buf, "p%x.", pid);
547 }
548 tid = ptid.lwp ();
549 if (tid < 0)
550 buf += sprintf (buf, "-%x", -tid);
551 else
552 buf += sprintf (buf, "%x", tid);
553
554 return buf;
555 }
556
557 static ULONGEST
558 hex_or_minus_one (const char *buf, const char **obuf)
559 {
560 ULONGEST ret;
561
562 if (startswith (buf, "-1"))
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. */
578 ptid_t
579 read_ptid (const char *buf, const char **obuf)
580 {
581 const char *p = buf;
582 const char *pp;
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;
598 return ptid_t (pid, tid, 0);
599 }
600
601 /* No multi-process. Just a tid. */
602 tid = hex_or_minus_one (p, &pp);
603
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 ());
608
609 if (obuf)
610 *obuf = pp;
611 return ptid_t (pid, tid, 0);
612 }
613
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
618 static int
619 write_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
631 static int
632 read_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
640 /* Send a packet to the remote machine, with error checking.
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. */
643
644 static int
645 putpkt_binary_1 (char *buf, int cnt, int is_notif)
646 {
647 client_state &cs = get_client_state ();
648 int i;
649 unsigned char csum = 0;
650 char *buf2;
651 char *p;
652 int cc;
653
654 buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
655
656 /* Copy the packet into buffer BUF2, encapsulating it
657 and giving it a checksum. */
658
659 p = buf2;
660 if (is_notif)
661 *p++ = '%';
662 else
663 *p++ = '$';
664
665 for (i = 0; i < cnt;)
666 i += try_rle (buf + i, cnt - i, &csum, &p);
667
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 {
678 if (write_prim (buf2, p - buf2) != p - buf2)
679 {
680 perror ("putpkt(write)");
681 free (buf2);
682 return -1;
683 }
684
685 if (cs.noack_mode || is_notif)
686 {
687 /* Don't expect an ack then. */
688 if (remote_debug)
689 {
690 if (is_notif)
691 debug_printf ("putpkt (\"%s\"); [notif]\n", buf2);
692 else
693 debug_printf ("putpkt (\"%s\"); [noack mode]\n", buf2);
694 debug_flush ();
695 }
696 break;
697 }
698
699 if (remote_debug)
700 {
701 debug_printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
702 debug_flush ();
703 }
704
705 cc = readchar ();
706
707 if (cc < 0)
708 {
709 free (buf2);
710 return -1;
711 }
712
713 if (remote_debug)
714 {
715 debug_printf ("[received '%c' (0x%x)]\n", cc, cc);
716 debug_flush ();
717 }
718
719 /* Check for an input interrupt while we're here. */
720 if (cc == '\003' && current_thread != NULL)
721 (*the_target->request_interrupt) ();
722 }
723 while (cc != '+');
724
725 free (buf2);
726 return 1; /* Success! */
727 }
728
729 int
730 putpkt_binary (char *buf, int cnt)
731 {
732 return putpkt_binary_1 (buf, cnt, 0);
733 }
734
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
739 int
740 putpkt (char *buf)
741 {
742 return putpkt_binary (buf, strlen (buf));
743 }
744
745 int
746 putpkt_notif (char *buf)
747 {
748 return putpkt_binary_1 (buf, strlen (buf), 1);
749 }
750
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
753 something. Thus this assumes readchar:bufcnt is 0.
754 About the only thing that should come through is a ^C, which
755 will cause us to request child interruption. */
756
757 static void
758 input_interrupt (int unused)
759 {
760 fd_set readset;
761 struct timeval immediate = { 0, 0 };
762
763 /* Protect against spurious interrupts. This has been observed to
764 be a problem under NetBSD 1.4 and 1.5. */
765
766 FD_ZERO (&readset);
767 FD_SET (remote_desc, &readset);
768 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
769 {
770 int cc;
771 char c = 0;
772
773 cc = read_prim (&c, 1);
774
775 if (cc == 0)
776 {
777 fprintf (stderr, "client connection closed\n");
778 return;
779 }
780 else if (cc != 1 || c != '\003')
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);
787 return;
788 }
789
790 (*the_target->request_interrupt) ();
791 }
792 }
793
794 /* Check if the remote side sent us an interrupt request (^C). */
795 void
796 check_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 }
806
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. */
810
811 static void
812 block_unblock_async_io (int block)
813 {
814 #ifndef USE_WIN32API
815 sigset_t sigio_set;
816
817 sigemptyset (&sigio_set);
818 sigaddset (&sigio_set, SIGIO);
819 sigprocmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
820 #endif
821 }
822
823 #ifdef __QNX__
824 static void
825 nto_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
845 /* Current state of asynchronous I/O. */
846 static int async_io_enabled;
847
848 /* Enable asynchronous I/O. */
849 void
850 enable_async_io (void)
851 {
852 if (async_io_enabled)
853 return;
854
855 block_unblock_async_io (0);
856
857 async_io_enabled = 1;
858 #ifdef __QNX__
859 nto_comctrl (1);
860 #endif /* __QNX__ */
861 }
862
863 /* Disable asynchronous I/O. */
864 void
865 disable_async_io (void)
866 {
867 if (!async_io_enabled)
868 return;
869
870 block_unblock_async_io (1);
871
872 async_io_enabled = 0;
873 #ifdef __QNX__
874 nto_comctrl (0);
875 #endif /* __QNX__ */
876
877 }
878
879 void
880 initialize_async_io (void)
881 {
882 /* Make sure that async I/O starts blocked. */
883 async_io_enabled = 1;
884 disable_async_io ();
885
886 /* Install the signal handler. */
887 #ifndef USE_WIN32API
888 signal (SIGIO, input_interrupt);
889 #endif
890 }
891
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
896 static unsigned char readchar_buf[BUFSIZ];
897 static int readchar_bufcnt = 0;
898 static unsigned char *readchar_bufp;
899
900 /* Returns next char from remote GDB. -1 if error. */
901
902 static int
903 readchar (void)
904 {
905 client_state &cs = get_client_state ();
906 int ch;
907
908 if (readchar_bufcnt == 0)
909 {
910 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
911
912 if (readchar_bufcnt <= 0)
913 {
914 if (readchar_bufcnt == 0)
915 {
916 if (remote_debug)
917 debug_printf ("readchar: Got EOF\n");
918 }
919 else
920 perror ("readchar");
921
922 return -1;
923 }
924
925 readchar_bufp = readchar_buf;
926 }
927
928 readchar_bufcnt--;
929 ch = *readchar_bufp++;
930 reschedule ();
931 return ch;
932 }
933
934 /* Reset the readchar state machine. */
935
936 static void
937 reset_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
949 static int
950 process_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
968 static void
969 reschedule (void)
970 {
971 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
972 readchar_callback = append_callback_event (process_remaining, NULL);
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
978 int
979 getpkt (char *buf)
980 {
981 client_state &cs = get_client_state ();
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 ();
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
1002 if (c == '$')
1003 break;
1004 if (remote_debug)
1005 {
1006 debug_printf ("[getpkt: discarding char '%c']\n", c);
1007 debug_flush ();
1008 }
1009
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 ());
1029
1030 if (csum == (c1 << 4) + c2)
1031 break;
1032
1033 if (cs.noack_mode)
1034 {
1035 fprintf (stderr,
1036 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1037 "buf=%s [no-ack-mode, Bad medium?]\n",
1038 (c1 << 4) + c2, csum, buf);
1039 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1040 break;
1041 }
1042
1043 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1044 (c1 << 4) + c2, csum, buf);
1045 if (write_prim ("-", 1) != 1)
1046 return -1;
1047 }
1048
1049 if (!cs.noack_mode)
1050 {
1051 if (remote_debug)
1052 {
1053 debug_printf ("getpkt (\"%s\"); [sending ack] \n", buf);
1054 debug_flush ();
1055 }
1056
1057 if (write_prim ("+", 1) != 1)
1058 return -1;
1059
1060 if (remote_debug)
1061 {
1062 debug_printf ("[sent ack]\n");
1063 debug_flush ();
1064 }
1065 }
1066 else
1067 {
1068 if (remote_debug)
1069 {
1070 debug_printf ("getpkt (\"%s\"); [no ack sent] \n", buf);
1071 debug_flush ();
1072 }
1073 }
1074
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
1091 return bp - buf;
1092 }
1093
1094 void
1095 write_ok (char *buf)
1096 {
1097 buf[0] = 'O';
1098 buf[1] = 'K';
1099 buf[2] = '\0';
1100 }
1101
1102 void
1103 write_enn (char *buf)
1104 {
1105 /* Some day, we should define the meanings of the error codes... */
1106 buf[0] = 'E';
1107 buf[1] = '0';
1108 buf[2] = '1';
1109 buf[3] = '\0';
1110 }
1111
1112 #endif
1113
1114 #ifndef IN_PROCESS_AGENT
1115
1116 static char *
1117 outreg (struct regcache *regcache, int regno, char *buf)
1118 {
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);
1124 *buf++ = tohex (regno & 0xf);
1125 *buf++ = ':';
1126 collect_register_as_string (regcache, regno, buf);
1127 buf += 2 * register_size (regcache->tdesc, regno);
1128 *buf++ = ';';
1129
1130 return buf;
1131 }
1132
1133 void
1134 prepare_resume_reply (char *buf, ptid_t ptid,
1135 struct target_waitstatus *status)
1136 {
1137 client_state &cs = get_client_state ();
1138 if (debug_threads)
1139 debug_printf ("Writing resume reply for %s:%d\n",
1140 target_pid_to_str (ptid), status->kind);
1141
1142 switch (status->kind)
1143 {
1144 case TARGET_WAITKIND_STOPPED:
1145 case TARGET_WAITKIND_FORKED:
1146 case TARGET_WAITKIND_VFORKED:
1147 case TARGET_WAITKIND_VFORK_DONE:
1148 case TARGET_WAITKIND_EXECD:
1149 case TARGET_WAITKIND_THREAD_CREATED:
1150 case TARGET_WAITKIND_SYSCALL_ENTRY:
1151 case TARGET_WAITKIND_SYSCALL_RETURN:
1152 {
1153 struct thread_info *saved_thread;
1154 const char **regp;
1155 struct regcache *regcache;
1156
1157 if ((status->kind == TARGET_WAITKIND_FORKED && cs.report_fork_events)
1158 || (status->kind == TARGET_WAITKIND_VFORKED
1159 && cs.report_vfork_events))
1160 {
1161 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1162 const char *event = (status->kind == TARGET_WAITKIND_FORKED
1163 ? "fork" : "vfork");
1164
1165 sprintf (buf, "T%02x%s:", signal, event);
1166 buf += strlen (buf);
1167 buf = write_ptid (buf, status->value.related_pid);
1168 strcat (buf, ";");
1169 }
1170 else if (status->kind == TARGET_WAITKIND_VFORK_DONE
1171 && cs.report_vfork_events)
1172 {
1173 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1174
1175 sprintf (buf, "T%02xvforkdone:;", signal);
1176 }
1177 else if (status->kind == TARGET_WAITKIND_EXECD && cs.report_exec_events)
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 }
1196 else if (status->kind == TARGET_WAITKIND_THREAD_CREATED
1197 && cs.report_thread_events)
1198 {
1199 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1200
1201 sprintf (buf, "T%02xcreate:;", signal);
1202 }
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 }
1213 else
1214 sprintf (buf, "T%02x", status->value.sig);
1215
1216 buf += strlen (buf);
1217
1218 saved_thread = current_thread;
1219
1220 switch_to_thread (ptid);
1221
1222 regp = current_target_desc ()->expedite_regs;
1223
1224 regcache = get_thread_regcache (current_thread, 1);
1225
1226 if (the_target->stopped_by_watchpoint != NULL
1227 && (*the_target->stopped_by_watchpoint) ())
1228 {
1229 CORE_ADDR addr;
1230 int i;
1231
1232 memcpy (buf, "watch:", 6);
1233 buf += 6;
1234
1235 addr = (*the_target->stopped_data_address) ();
1236
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 }
1245 else if (cs.swbreak_feature && target_stopped_by_sw_breakpoint ())
1246 {
1247 sprintf (buf, "swbreak:;");
1248 buf += strlen (buf);
1249 }
1250 else if (cs.hwbreak_feature && target_stopped_by_hw_breakpoint ())
1251 {
1252 sprintf (buf, "hwbreak:;");
1253 buf += strlen (buf);
1254 }
1255
1256 while (*regp)
1257 {
1258 buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
1259 regp ++;
1260 }
1261 *buf = '\0';
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. */
1278 if (1 || cs.general_thread != ptid)
1279 {
1280 int core = -1;
1281 /* In non-stop, don't change the general thread behind
1282 GDB's back. */
1283 if (!non_stop)
1284 cs.general_thread = ptid;
1285 sprintf (buf, "thread:");
1286 buf += strlen (buf);
1287 buf = write_ptid (buf, ptid);
1288 strcat (buf, ";");
1289 buf += strlen (buf);
1290
1291 core = target_core_of_thread (ptid);
1292
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 }
1301 }
1302 }
1303
1304 if (dlls_changed)
1305 {
1306 strcpy (buf, "library:;");
1307 buf += strlen (buf);
1308 dlls_changed = 0;
1309 }
1310
1311 current_thread = saved_thread;
1312 }
1313 break;
1314 case TARGET_WAITKIND_EXITED:
1315 if (cs.multi_process)
1316 sprintf (buf, "W%x;process:%x",
1317 status->value.integer, ptid.pid ());
1318 else
1319 sprintf (buf, "W%02x", status->value.integer);
1320 break;
1321 case TARGET_WAITKIND_SIGNALLED:
1322 if (cs.multi_process)
1323 sprintf (buf, "X%x;process:%x",
1324 status->value.sig, ptid.pid ());
1325 else
1326 sprintf (buf, "X%02x", status->value.sig);
1327 break;
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;
1333 case TARGET_WAITKIND_NO_RESUMED:
1334 sprintf (buf, "N");
1335 break;
1336 default:
1337 error ("unhandled waitkind");
1338 break;
1339 }
1340 }
1341
1342 void
1343 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
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
1364 void
1365 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
1366 unsigned char **to_p)
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
1384 if (*to_p == NULL)
1385 *to_p = (unsigned char *) xmalloc (*len_ptr);
1386
1387 hex2bin (&from[i++], *to_p, *len_ptr);
1388 }
1389
1390 int
1391 decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1392 unsigned int *len_ptr, unsigned char **to_p)
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
1410 if (*to_p == NULL)
1411 *to_p = (unsigned char *) xmalloc (*len_ptr);
1412
1413 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1414 *to_p, *len_ptr) != *len_ptr)
1415 return -1;
1416
1417 return 0;
1418 }
1419
1420 /* Decode a qXfer write request. */
1421
1422 int
1423 decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
1424 unsigned int *len, unsigned char *data)
1425 {
1426 char ch;
1427 char *b = buf;
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. */
1438 packet_len -= buf - b;
1439 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1440 data, packet_len);
1441 return 0;
1442 }
1443
1444 /* Decode the parameters of a qSearch:memory packet. */
1445
1446 int
1447 decode_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
1462 static void
1463 free_sym_cache (struct sym_cache *sym)
1464 {
1465 if (sym != NULL)
1466 {
1467 free (sym->name);
1468 free (sym);
1469 }
1470 }
1471
1472 void
1473 clear_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
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.
1489 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1490
1491 int
1492 look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
1493 {
1494 client_state &cs = get_client_state ();
1495 char *p, *q;
1496 int len;
1497 struct sym_cache *sym;
1498 struct process_info *proc;
1499
1500 proc = current_process ();
1501
1502 /* Check the cache first. */
1503 for (sym = proc->symbol_cache; sym; sym = sym->next)
1504 if (strcmp (name, sym->name) == 0)
1505 {
1506 *addrp = sym->addr;
1507 return 1;
1508 }
1509
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)
1513 return 0;
1514
1515 /* Send the request. */
1516 strcpy (cs.own_buf, "qSymbol:");
1517 bin2hex ((const gdb_byte *) name, cs.own_buf + strlen ("qSymbol:"),
1518 strlen (name));
1519 if (putpkt (cs.own_buf) < 0)
1520 return -1;
1521
1522 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1523 len = getpkt (cs.own_buf);
1524 if (len < 0)
1525 return -1;
1526
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
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)
1533 {
1534 if (cs.own_buf[0] == 'm')
1535 {
1536 CORE_ADDR mem_addr;
1537 unsigned char *mem_buf;
1538 unsigned int mem_len;
1539
1540 decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1541 mem_buf = (unsigned char *) xmalloc (mem_len);
1542 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1543 bin2hex (mem_buf, cs.own_buf, mem_len);
1544 else
1545 write_enn (cs.own_buf);
1546 free (mem_buf);
1547 if (putpkt (cs.own_buf) < 0)
1548 return -1;
1549 }
1550 else if (cs.own_buf[0] == 'v')
1551 {
1552 int new_len = -1;
1553 handle_v_requests (cs.own_buf, len, &new_len);
1554 if (new_len != -1)
1555 putpkt_binary (cs.own_buf, new_len);
1556 else
1557 putpkt (cs.own_buf);
1558 }
1559 else
1560 break;
1561 len = getpkt (cs.own_buf);
1562 if (len < 0)
1563 return -1;
1564 }
1565
1566 if (!startswith (cs.own_buf, "qSymbol:"))
1567 {
1568 warning ("Malformed response to qSymbol, ignoring: %s\n", cs.own_buf);
1569 return -1;
1570 }
1571
1572 p = cs.own_buf + strlen ("qSymbol:");
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);
1582
1583 /* Save the symbol in our cache. */
1584 sym = XNEW (struct sym_cache);
1585 sym->name = xstrdup (name);
1586 sym->addr = *addrp;
1587 sym->next = proc->symbol_cache;
1588 proc->symbol_cache = sym;
1589
1590 return 1;
1591 }
1592
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
1599 shall be the same as if executing it at OLDLOC. For example, call
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
1605 int
1606 relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1607 {
1608 client_state &cs = get_client_state ();
1609 int len;
1610 ULONGEST written = 0;
1611
1612 /* Send the request. */
1613 sprintf (cs.own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1614 paddress (*to));
1615 if (putpkt (cs.own_buf) < 0)
1616 return -1;
1617
1618 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1619 len = getpkt (cs.own_buf);
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. */
1627 while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M' || cs.own_buf[0] == 'X')
1628 {
1629 CORE_ADDR mem_addr;
1630 unsigned char *mem_buf = NULL;
1631 unsigned int mem_len;
1632
1633 if (cs.own_buf[0] == 'm')
1634 {
1635 decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1636 mem_buf = (unsigned char *) xmalloc (mem_len);
1637 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1638 bin2hex (mem_buf, cs.own_buf, mem_len);
1639 else
1640 write_enn (cs.own_buf);
1641 }
1642 else if (cs.own_buf[0] == 'X')
1643 {
1644 if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
1645 &mem_len, &mem_buf) < 0
1646 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
1647 write_enn (cs.own_buf);
1648 else
1649 write_ok (cs.own_buf);
1650 }
1651 else
1652 {
1653 decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
1654 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1655 write_ok (cs.own_buf);
1656 else
1657 write_enn (cs.own_buf);
1658 }
1659 free (mem_buf);
1660 if (putpkt (cs.own_buf) < 0)
1661 return -1;
1662 len = getpkt (cs.own_buf);
1663 if (len < 0)
1664 return -1;
1665 }
1666
1667 if (cs.own_buf[0] == 'E')
1668 {
1669 warning ("An error occurred while relocating an instruction: %s\n",
1670 cs.own_buf);
1671 return -1;
1672 }
1673
1674 if (!startswith (cs.own_buf, "qRelocInsn:"))
1675 {
1676 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1677 cs.own_buf);
1678 return -1;
1679 }
1680
1681 unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
1682
1683 *to += written;
1684 return 0;
1685 }
1686
1687 void
1688 monitor_output (const char *msg)
1689 {
1690 int len = strlen (msg);
1691 char *buf = (char *) xmalloc (len * 2 + 2);
1692
1693 buf[0] = 'O';
1694 bin2hex ((const gdb_byte *) msg, buf + 1, len);
1695
1696 putpkt (buf);
1697 free (buf);
1698 }
1699
1700 #endif