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