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