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