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