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, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "server.h"
23 #include "terminal.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 #if HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 #if HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60 #include <sys/stat.h>
61 #if HAVE_ERRNO_H
62 #include <errno.h>
63 #endif
64
65 #if USE_WIN32API
66 #include <winsock.h>
67 #endif
68
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t;
71 #endif
72
73 /* A cache entry for a successfully looked-up symbol. */
74 struct sym_cache
75 {
76 const char *name;
77 CORE_ADDR addr;
78 struct sym_cache *next;
79 };
80
81 /* The symbol cache. */
82 static struct sym_cache *symbol_cache;
83
84 /* If this flag has been set, assume cache misses are
85 failures. */
86 int all_symbols_looked_up;
87
88 int remote_debug = 0;
89 struct ui_file *gdb_stdlog;
90
91 static int remote_desc;
92
93 /* FIXME headerize? */
94 extern int using_threads;
95 extern int debug_threads;
96
97 #ifdef USE_WIN32API
98 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
99 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
100 #endif
101
102 /* Open a connection to a remote debugger.
103 NAME is the filename used for communication. */
104
105 void
106 remote_open (char *name)
107 {
108 #if defined(F_SETFL) && defined (FASYNC)
109 int save_fcntl_flags;
110 #endif
111 char *port_str;
112
113 port_str = strchr (name, ':');
114 if (port_str == NULL)
115 {
116 #ifdef USE_WIN32API
117 error ("Only <host>:<port> is supported on this platform.");
118 #else
119 struct stat statbuf;
120
121 if (stat (name, &statbuf) == 0
122 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
123 remote_desc = open (name, O_RDWR);
124 else
125 {
126 errno = EINVAL;
127 remote_desc = -1;
128 }
129
130 if (remote_desc < 0)
131 perror_with_name ("Could not open remote device");
132
133 #ifdef HAVE_TERMIOS
134 {
135 struct termios termios;
136 tcgetattr (remote_desc, &termios);
137
138 termios.c_iflag = 0;
139 termios.c_oflag = 0;
140 termios.c_lflag = 0;
141 termios.c_cflag &= ~(CSIZE | PARENB);
142 termios.c_cflag |= CLOCAL | CS8;
143 termios.c_cc[VMIN] = 1;
144 termios.c_cc[VTIME] = 0;
145
146 tcsetattr (remote_desc, TCSANOW, &termios);
147 }
148 #endif
149
150 #ifdef HAVE_TERMIO
151 {
152 struct termio termio;
153 ioctl (remote_desc, TCGETA, &termio);
154
155 termio.c_iflag = 0;
156 termio.c_oflag = 0;
157 termio.c_lflag = 0;
158 termio.c_cflag &= ~(CSIZE | PARENB);
159 termio.c_cflag |= CLOCAL | CS8;
160 termio.c_cc[VMIN] = 1;
161 termio.c_cc[VTIME] = 0;
162
163 ioctl (remote_desc, TCSETA, &termio);
164 }
165 #endif
166
167 #ifdef HAVE_SGTTY
168 {
169 struct sgttyb sg;
170
171 ioctl (remote_desc, TIOCGETP, &sg);
172 sg.sg_flags = RAW;
173 ioctl (remote_desc, TIOCSETP, &sg);
174 }
175 #endif
176
177 fprintf (stderr, "Remote debugging using %s\n", name);
178 #endif /* USE_WIN32API */
179 }
180 else
181 {
182 #ifdef USE_WIN32API
183 static int winsock_initialized;
184 #endif
185 char *port_str;
186 int port;
187 struct sockaddr_in sockaddr;
188 socklen_t tmp;
189 int tmp_desc;
190
191 port_str = strchr (name, ':');
192
193 port = atoi (port_str + 1);
194
195 #ifdef USE_WIN32API
196 if (!winsock_initialized)
197 {
198 WSADATA wsad;
199
200 WSAStartup (MAKEWORD (1, 0), &wsad);
201 winsock_initialized = 1;
202 }
203 #endif
204
205 tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
206 if (tmp_desc < 0)
207 perror_with_name ("Can't open socket");
208
209 /* Allow rapid reuse of this port. */
210 tmp = 1;
211 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
212 sizeof (tmp));
213
214 sockaddr.sin_family = PF_INET;
215 sockaddr.sin_port = htons (port);
216 sockaddr.sin_addr.s_addr = INADDR_ANY;
217
218 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
219 || listen (tmp_desc, 1))
220 perror_with_name ("Can't bind address");
221
222 /* If port is zero, a random port will be selected, and the
223 fprintf below needs to know what port was selected. */
224 if (port == 0)
225 {
226 socklen_t len = sizeof (sockaddr);
227 if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0
228 || len < sizeof (sockaddr))
229 perror_with_name ("Can't determine port");
230 port = ntohs (sockaddr.sin_port);
231 }
232
233 fprintf (stderr, "Listening on port %d\n", port);
234 fflush (stderr);
235
236 tmp = sizeof (sockaddr);
237 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
238 if (remote_desc == -1)
239 perror_with_name ("Accept failed");
240
241 /* Enable TCP keep alive process. */
242 tmp = 1;
243 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
244
245 /* Tell TCP not to delay small packets. This greatly speeds up
246 interactive response. */
247 tmp = 1;
248 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
249 (char *) &tmp, sizeof (tmp));
250
251
252 #ifndef USE_WIN32API
253 close (tmp_desc); /* No longer need this */
254
255 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
256 exits when the remote side dies. */
257 #else
258 closesocket (tmp_desc); /* No longer need this */
259 #endif
260
261 /* Convert IP address to string. */
262 fprintf (stderr, "Remote debugging from host %s\n",
263 inet_ntoa (sockaddr.sin_addr));
264 }
265
266 #if defined(F_SETFL) && defined (FASYNC)
267 save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
268 fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
269 #if defined (F_SETOWN)
270 fcntl (remote_desc, F_SETOWN, getpid ());
271 #endif
272 #endif
273 disable_async_io ();
274 }
275
276 void
277 remote_close (void)
278 {
279 #ifdef USE_WIN32API
280 closesocket (remote_desc);
281 #else
282 close (remote_desc);
283 #endif
284 }
285
286 /* Convert hex digit A to a number. */
287
288 static int
289 fromhex (int a)
290 {
291 if (a >= '0' && a <= '9')
292 return a - '0';
293 else if (a >= 'a' && a <= 'f')
294 return a - 'a' + 10;
295 else
296 error ("Reply contains invalid hex digit");
297 return 0;
298 }
299
300 int
301 unhexify (char *bin, const char *hex, int count)
302 {
303 int i;
304
305 for (i = 0; i < count; i++)
306 {
307 if (hex[0] == 0 || hex[1] == 0)
308 {
309 /* Hex string is short, or of uneven length.
310 Return the count that has been converted so far. */
311 return i;
312 }
313 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
314 hex += 2;
315 }
316 return i;
317 }
318
319 void
320 decode_address (CORE_ADDR *addrp, const char *start, int len)
321 {
322 CORE_ADDR addr;
323 char ch;
324 int i;
325
326 addr = 0;
327 for (i = 0; i < len; i++)
328 {
329 ch = start[i];
330 addr = addr << 4;
331 addr = addr | (fromhex (ch) & 0x0f);
332 }
333 *addrp = addr;
334 }
335
336 const char *
337 decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
338 {
339 const char *end;
340
341 end = start;
342 while (*end != '\0' && *end != ';')
343 end++;
344
345 decode_address (addrp, start, end - start);
346
347 if (*end == ';')
348 end++;
349 return end;
350 }
351
352 /* Convert number NIB to a hex digit. */
353
354 static int
355 tohex (int nib)
356 {
357 if (nib < 10)
358 return '0' + nib;
359 else
360 return 'a' + nib - 10;
361 }
362
363 int
364 hexify (char *hex, const char *bin, int count)
365 {
366 int i;
367
368 /* May use a length, or a nul-terminated string as input. */
369 if (count == 0)
370 count = strlen (bin);
371
372 for (i = 0; i < count; i++)
373 {
374 *hex++ = tohex ((*bin >> 4) & 0xf);
375 *hex++ = tohex (*bin++ & 0xf);
376 }
377 *hex = 0;
378 return i;
379 }
380
381 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
382 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
383 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
384 (which may be more than *OUT_LEN due to escape characters). The
385 total number of bytes in the output buffer will be at most
386 OUT_MAXLEN. */
387
388 int
389 remote_escape_output (const gdb_byte *buffer, int len,
390 gdb_byte *out_buf, int *out_len,
391 int out_maxlen)
392 {
393 int input_index, output_index;
394
395 output_index = 0;
396 for (input_index = 0; input_index < len; input_index++)
397 {
398 gdb_byte b = buffer[input_index];
399
400 if (b == '$' || b == '#' || b == '}' || b == '*')
401 {
402 /* These must be escaped. */
403 if (output_index + 2 > out_maxlen)
404 break;
405 out_buf[output_index++] = '}';
406 out_buf[output_index++] = b ^ 0x20;
407 }
408 else
409 {
410 if (output_index + 1 > out_maxlen)
411 break;
412 out_buf[output_index++] = b;
413 }
414 }
415
416 *out_len = input_index;
417 return output_index;
418 }
419
420 /* Convert BUFFER, escaped data LEN bytes long, into binary data
421 in OUT_BUF. Return the number of bytes written to OUT_BUF.
422 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
423
424 This function reverses remote_escape_output. It allows more
425 escaped characters than that function does, in particular because
426 '*' must be escaped to avoid the run-length encoding processing
427 in reading packets. */
428
429 static int
430 remote_unescape_input (const gdb_byte *buffer, int len,
431 gdb_byte *out_buf, int out_maxlen)
432 {
433 int input_index, output_index;
434 int escaped;
435
436 output_index = 0;
437 escaped = 0;
438 for (input_index = 0; input_index < len; input_index++)
439 {
440 gdb_byte b = buffer[input_index];
441
442 if (output_index + 1 > out_maxlen)
443 error ("Received too much data from the target.");
444
445 if (escaped)
446 {
447 out_buf[output_index++] = b ^ 0x20;
448 escaped = 0;
449 }
450 else if (b == '}')
451 escaped = 1;
452 else
453 out_buf[output_index++] = b;
454 }
455
456 if (escaped)
457 error ("Unmatched escape character in target response.");
458
459 return output_index;
460 }
461
462 /* Look for a sequence of characters which can be run-length encoded.
463 If there are any, update *CSUM and *P. Otherwise, output the
464 single character. Return the number of characters consumed. */
465
466 static int
467 try_rle (char *buf, int remaining, unsigned char *csum, char **p)
468 {
469 int n;
470
471 /* Always output the character. */
472 *csum += buf[0];
473 *(*p)++ = buf[0];
474
475 /* Don't go past '~'. */
476 if (remaining > 97)
477 remaining = 97;
478
479 for (n = 1; n < remaining; n++)
480 if (buf[n] != buf[0])
481 break;
482
483 /* N is the index of the first character not the same as buf[0].
484 buf[0] is counted twice, so by decrementing N, we get the number
485 of characters the RLE sequence will replace. */
486 n--;
487
488 if (n < 3)
489 return 1;
490
491 /* Skip the frame characters. The manual says to skip '+' and '-'
492 also, but there's no reason to. Unfortunately these two unusable
493 characters double the encoded length of a four byte zero
494 value. */
495 while (n + 29 == '$' || n + 29 == '#')
496 n--;
497
498 *csum += '*';
499 *(*p)++ = '*';
500 *csum += n + 29;
501 *(*p)++ = n + 29;
502
503 return n + 1;
504 }
505
506 /* Send a packet to the remote machine, with error checking.
507 The data of the packet is in BUF, and the length of the
508 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
509
510 int
511 putpkt_binary (char *buf, int cnt)
512 {
513 int i;
514 unsigned char csum = 0;
515 char *buf2;
516 char buf3[1];
517 char *p;
518
519 buf2 = malloc (PBUFSIZ);
520
521 /* Copy the packet into buffer BUF2, encapsulating it
522 and giving it a checksum. */
523
524 p = buf2;
525 *p++ = '$';
526
527 for (i = 0; i < cnt;)
528 i += try_rle (buf + i, cnt - i, &csum, &p);
529
530 *p++ = '#';
531 *p++ = tohex ((csum >> 4) & 0xf);
532 *p++ = tohex (csum & 0xf);
533
534 *p = '\0';
535
536 /* Send it over and over until we get a positive ack. */
537
538 do
539 {
540 int cc;
541
542 if (write (remote_desc, buf2, p - buf2) != p - buf2)
543 {
544 perror ("putpkt(write)");
545 return -1;
546 }
547
548 if (remote_debug)
549 {
550 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
551 fflush (stderr);
552 }
553 cc = read (remote_desc, buf3, 1);
554 if (remote_debug)
555 {
556 fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
557 fflush (stderr);
558 }
559
560 if (cc <= 0)
561 {
562 if (cc == 0)
563 fprintf (stderr, "putpkt(read): Got EOF\n");
564 else
565 perror ("putpkt(read)");
566
567 free (buf2);
568 return -1;
569 }
570
571 /* Check for an input interrupt while we're here. */
572 if (buf3[0] == '\003')
573 (*the_target->request_interrupt) ();
574 }
575 while (buf3[0] != '+');
576
577 free (buf2);
578 return 1; /* Success! */
579 }
580
581 /* Send a packet to the remote machine, with error checking. The data
582 of the packet is in BUF, and the packet should be a NUL-terminated
583 string. Returns >= 0 on success, -1 otherwise. */
584
585 int
586 putpkt (char *buf)
587 {
588 return putpkt_binary (buf, strlen (buf));
589 }
590
591 #ifndef USE_WIN32API
592
593 /* Come here when we get an input interrupt from the remote side. This
594 interrupt should only be active while we are waiting for the child to do
595 something. About the only thing that should come through is a ^C, which
596 will cause us to request child interruption. */
597
598 static void
599 input_interrupt (int unused)
600 {
601 fd_set readset;
602 struct timeval immediate = { 0, 0 };
603
604 /* Protect against spurious interrupts. This has been observed to
605 be a problem under NetBSD 1.4 and 1.5. */
606
607 FD_ZERO (&readset);
608 FD_SET (remote_desc, &readset);
609 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
610 {
611 int cc;
612 char c = 0;
613
614 cc = read (remote_desc, &c, 1);
615
616 if (cc != 1 || c != '\003')
617 {
618 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
619 cc, c, c);
620 return;
621 }
622
623 (*the_target->request_interrupt) ();
624 }
625 }
626 #endif
627
628 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
629 accept Control-C from the client, and must be disabled when talking to
630 the client. */
631
632 void
633 block_async_io (void)
634 {
635 #ifndef USE_WIN32API
636 sigset_t sigio_set;
637 sigemptyset (&sigio_set);
638 sigaddset (&sigio_set, SIGIO);
639 sigprocmask (SIG_BLOCK, &sigio_set, NULL);
640 #endif
641 }
642
643 void
644 unblock_async_io (void)
645 {
646 #ifndef USE_WIN32API
647 sigset_t sigio_set;
648 sigemptyset (&sigio_set);
649 sigaddset (&sigio_set, SIGIO);
650 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
651 #endif
652 }
653
654 /* Current state of asynchronous I/O. */
655 static int async_io_enabled;
656
657 /* Enable asynchronous I/O. */
658 void
659 enable_async_io (void)
660 {
661 if (async_io_enabled)
662 return;
663
664 #ifndef USE_WIN32API
665 signal (SIGIO, input_interrupt);
666 #endif
667 async_io_enabled = 1;
668 }
669
670 /* Disable asynchronous I/O. */
671 void
672 disable_async_io (void)
673 {
674 if (!async_io_enabled)
675 return;
676
677 #ifndef USE_WIN32API
678 signal (SIGIO, SIG_IGN);
679 #endif
680 async_io_enabled = 0;
681 }
682
683 /* Returns next char from remote GDB. -1 if error. */
684
685 static int
686 readchar (void)
687 {
688 static unsigned char buf[BUFSIZ];
689 static int bufcnt = 0;
690 static unsigned char *bufp;
691
692 if (bufcnt-- > 0)
693 return *bufp++;
694
695 bufcnt = read (remote_desc, buf, sizeof (buf));
696
697 if (bufcnt <= 0)
698 {
699 if (bufcnt == 0)
700 fprintf (stderr, "readchar: Got EOF\n");
701 else
702 perror ("readchar");
703
704 return -1;
705 }
706
707 bufp = buf;
708 bufcnt--;
709 return *bufp++ & 0x7f;
710 }
711
712 /* Read a packet from the remote machine, with error checking,
713 and store it in BUF. Returns length of packet, or negative if error. */
714
715 int
716 getpkt (char *buf)
717 {
718 char *bp;
719 unsigned char csum, c1, c2;
720 int c;
721
722 while (1)
723 {
724 csum = 0;
725
726 while (1)
727 {
728 c = readchar ();
729 if (c == '$')
730 break;
731 if (remote_debug)
732 {
733 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
734 fflush (stderr);
735 }
736
737 if (c < 0)
738 return -1;
739 }
740
741 bp = buf;
742 while (1)
743 {
744 c = readchar ();
745 if (c < 0)
746 return -1;
747 if (c == '#')
748 break;
749 *bp++ = c;
750 csum += c;
751 }
752 *bp = 0;
753
754 c1 = fromhex (readchar ());
755 c2 = fromhex (readchar ());
756
757 if (csum == (c1 << 4) + c2)
758 break;
759
760 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
761 (c1 << 4) + c2, csum, buf);
762 write (remote_desc, "-", 1);
763 }
764
765 if (remote_debug)
766 {
767 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
768 fflush (stderr);
769 }
770
771 write (remote_desc, "+", 1);
772
773 if (remote_debug)
774 {
775 fprintf (stderr, "[sent ack]\n");
776 fflush (stderr);
777 }
778
779 return bp - buf;
780 }
781
782 void
783 write_ok (char *buf)
784 {
785 buf[0] = 'O';
786 buf[1] = 'K';
787 buf[2] = '\0';
788 }
789
790 void
791 write_enn (char *buf)
792 {
793 /* Some day, we should define the meanings of the error codes... */
794 buf[0] = 'E';
795 buf[1] = '0';
796 buf[2] = '1';
797 buf[3] = '\0';
798 }
799
800 void
801 convert_int_to_ascii (unsigned char *from, char *to, int n)
802 {
803 int nib;
804 int ch;
805 while (n--)
806 {
807 ch = *from++;
808 nib = ((ch & 0xf0) >> 4) & 0x0f;
809 *to++ = tohex (nib);
810 nib = ch & 0x0f;
811 *to++ = tohex (nib);
812 }
813 *to++ = 0;
814 }
815
816
817 void
818 convert_ascii_to_int (char *from, unsigned char *to, int n)
819 {
820 int nib1, nib2;
821 while (n--)
822 {
823 nib1 = fromhex (*from++);
824 nib2 = fromhex (*from++);
825 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
826 }
827 }
828
829 static char *
830 outreg (int regno, char *buf)
831 {
832 if ((regno >> 12) != 0)
833 *buf++ = tohex ((regno >> 12) & 0xf);
834 if ((regno >> 8) != 0)
835 *buf++ = tohex ((regno >> 8) & 0xf);
836 *buf++ = tohex ((regno >> 4) & 0xf);
837 *buf++ = tohex (regno & 0xf);
838 *buf++ = ':';
839 collect_register_as_string (regno, buf);
840 buf += 2 * register_size (regno);
841 *buf++ = ';';
842
843 return buf;
844 }
845
846 void
847 new_thread_notify (int id)
848 {
849 char own_buf[256];
850
851 /* The `n' response is not yet part of the remote protocol. Do nothing. */
852 if (1)
853 return;
854
855 if (server_waiting == 0)
856 return;
857
858 sprintf (own_buf, "n%x", id);
859 disable_async_io ();
860 putpkt (own_buf);
861 enable_async_io ();
862 }
863
864 void
865 dead_thread_notify (int id)
866 {
867 char own_buf[256];
868
869 /* The `x' response is not yet part of the remote protocol. Do nothing. */
870 if (1)
871 return;
872
873 sprintf (own_buf, "x%x", id);
874 disable_async_io ();
875 putpkt (own_buf);
876 enable_async_io ();
877 }
878
879 void
880 prepare_resume_reply (char *buf, char status, unsigned char sig)
881 {
882 int nib;
883
884 *buf++ = status;
885
886 nib = ((sig & 0xf0) >> 4);
887 *buf++ = tohex (nib);
888 nib = sig & 0x0f;
889 *buf++ = tohex (nib);
890
891 if (status == 'T')
892 {
893 const char **regp = gdbserver_expedite_regs;
894
895 if (the_target->stopped_by_watchpoint != NULL
896 && (*the_target->stopped_by_watchpoint) ())
897 {
898 CORE_ADDR addr;
899 int i;
900
901 strncpy (buf, "watch:", 6);
902 buf += 6;
903
904 addr = (*the_target->stopped_data_address) ();
905
906 /* Convert each byte of the address into two hexadecimal chars.
907 Note that we take sizeof (void *) instead of sizeof (addr);
908 this is to avoid sending a 64-bit address to a 32-bit GDB. */
909 for (i = sizeof (void *) * 2; i > 0; i--)
910 {
911 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
912 }
913 *buf++ = ';';
914 }
915
916 while (*regp)
917 {
918 buf = outreg (find_regno (*regp), buf);
919 regp ++;
920 }
921
922 /* Formerly, if the debugger had not used any thread features we would not
923 burden it with a thread status response. This was for the benefit of
924 GDB 4.13 and older. However, in recent GDB versions the check
925 (``if (cont_thread != 0)'') does not have the desired effect because of
926 sillyness in the way that the remote protocol handles specifying a thread.
927 Since thread support relies on qSymbol support anyway, assume GDB can handle
928 threads. */
929
930 if (using_threads)
931 {
932 unsigned int gdb_id_from_wait;
933
934 /* FIXME right place to set this? */
935 thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id;
936 gdb_id_from_wait = thread_to_gdb_id (current_inferior);
937
938 if (debug_threads)
939 fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait);
940 /* This if (1) ought to be unnecessary. But remote_wait in GDB
941 will claim this event belongs to inferior_ptid if we do not
942 specify a thread, and there's no way for gdbserver to know
943 what inferior_ptid is. */
944 if (1 || old_thread_from_wait != thread_from_wait)
945 {
946 general_thread = thread_from_wait;
947 sprintf (buf, "thread:%x;", gdb_id_from_wait);
948 buf += strlen (buf);
949 old_thread_from_wait = thread_from_wait;
950 }
951 }
952 }
953 /* For W and X, we're done. */
954 *buf++ = 0;
955 }
956
957 void
958 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
959 {
960 int i = 0, j = 0;
961 char ch;
962 *mem_addr_ptr = *len_ptr = 0;
963
964 while ((ch = from[i++]) != ',')
965 {
966 *mem_addr_ptr = *mem_addr_ptr << 4;
967 *mem_addr_ptr |= fromhex (ch) & 0x0f;
968 }
969
970 for (j = 0; j < 4; j++)
971 {
972 if ((ch = from[i++]) == 0)
973 break;
974 *len_ptr = *len_ptr << 4;
975 *len_ptr |= fromhex (ch) & 0x0f;
976 }
977 }
978
979 void
980 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
981 unsigned char *to)
982 {
983 int i = 0;
984 char ch;
985 *mem_addr_ptr = *len_ptr = 0;
986
987 while ((ch = from[i++]) != ',')
988 {
989 *mem_addr_ptr = *mem_addr_ptr << 4;
990 *mem_addr_ptr |= fromhex (ch) & 0x0f;
991 }
992
993 while ((ch = from[i++]) != ':')
994 {
995 *len_ptr = *len_ptr << 4;
996 *len_ptr |= fromhex (ch) & 0x0f;
997 }
998
999 convert_ascii_to_int (&from[i++], to, *len_ptr);
1000 }
1001
1002 int
1003 decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1004 unsigned int *len_ptr, unsigned char *to)
1005 {
1006 int i = 0;
1007 char ch;
1008 *mem_addr_ptr = *len_ptr = 0;
1009
1010 while ((ch = from[i++]) != ',')
1011 {
1012 *mem_addr_ptr = *mem_addr_ptr << 4;
1013 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1014 }
1015
1016 while ((ch = from[i++]) != ':')
1017 {
1018 *len_ptr = *len_ptr << 4;
1019 *len_ptr |= fromhex (ch) & 0x0f;
1020 }
1021
1022 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1023 to, *len_ptr) != *len_ptr)
1024 return -1;
1025
1026 return 0;
1027 }
1028
1029 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1030 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1031
1032 int
1033 look_up_one_symbol (const char *name, CORE_ADDR *addrp)
1034 {
1035 char own_buf[266], *p, *q;
1036 int len;
1037 struct sym_cache *sym;
1038
1039 /* Check the cache first. */
1040 for (sym = symbol_cache; sym; sym = sym->next)
1041 if (strcmp (name, sym->name) == 0)
1042 {
1043 *addrp = sym->addr;
1044 return 1;
1045 }
1046
1047 /* If we've passed the call to thread_db_look_up_symbols, then
1048 anything not in the cache must not exist; we're not interested
1049 in any libraries loaded after that point, only in symbols in
1050 libpthread.so. It might not be an appropriate time to look
1051 up a symbol, e.g. while we're trying to fetch registers. */
1052 if (all_symbols_looked_up)
1053 return 0;
1054
1055 /* Send the request. */
1056 strcpy (own_buf, "qSymbol:");
1057 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
1058 if (putpkt (own_buf) < 0)
1059 return -1;
1060
1061 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1062 len = getpkt (own_buf);
1063 if (len < 0)
1064 return -1;
1065
1066 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
1067 {
1068 /* Malformed response. */
1069 if (remote_debug)
1070 {
1071 fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
1072 fflush (stderr);
1073 }
1074
1075 return -1;
1076 }
1077
1078 p = own_buf + strlen ("qSymbol:");
1079 q = p;
1080 while (*q && *q != ':')
1081 q++;
1082
1083 /* Make sure we found a value for the symbol. */
1084 if (p == q || *q == '\0')
1085 return 0;
1086
1087 decode_address (addrp, p, q - p);
1088
1089 /* Save the symbol in our cache. */
1090 sym = malloc (sizeof (*sym));
1091 sym->name = strdup (name);
1092 sym->addr = *addrp;
1093 sym->next = symbol_cache;
1094 symbol_cache = sym;
1095
1096 return 1;
1097 }
1098
1099 void
1100 monitor_output (const char *msg)
1101 {
1102 char *buf = malloc (strlen (msg) * 2 + 2);
1103
1104 buf[0] = 'O';
1105 hexify (buf + 1, msg, 0);
1106
1107 putpkt (buf);
1108 free (buf);
1109 }
This page took 0.073442 seconds and 5 git commands to generate.