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