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