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