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