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