]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/remote-utils.c
gas/
[thirdparty/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
c906108c
SS
299/* Convert number NIB to a hex digit. */
300
301static int
fba45db2 302tohex (int nib)
c906108c
SS
303{
304 if (nib < 10)
305 return '0' + nib;
306 else
307 return 'a' + nib - 10;
308}
309
ce3a066d
DJ
310int
311hexify (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
01f9e8fa
DJ
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
335int
336remote_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
376static int
377remote_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
5ffff7c1
DJ
409/* Look for a sequence of characters which can be run-length encoded.
410 If there are any, update *CSUM and *P. Otherwise, output the
411 single character. Return the number of characters consumed. */
412
413static int
414try_rle (char *buf, int remaining, unsigned char *csum, char **p)
415{
416 int n;
417
418 /* Always output the character. */
419 *csum += buf[0];
420 *(*p)++ = buf[0];
421
422 /* Don't go past '~'. */
423 if (remaining > 97)
424 remaining = 97;
425
426 for (n = 1; n < remaining; n++)
427 if (buf[n] != buf[0])
428 break;
429
430 /* N is the index of the first character not the same as buf[0].
431 buf[0] is counted twice, so by decrementing N, we get the number
432 of characters the RLE sequence will replace. */
433 n--;
434
435 if (n < 3)
436 return 1;
437
438 /* Skip the frame characters. The manual says to skip '+' and '-'
439 also, but there's no reason to. Unfortunately these two unusable
440 characters double the encoded length of a four byte zero
441 value. */
442 while (n + 29 == '$' || n + 29 == '#')
443 n--;
444
445 *csum += '*';
446 *(*p)++ = '*';
447 *csum += n + 29;
448 *(*p)++ = n + 29;
449
450 return n + 1;
451}
452
c906108c 453/* Send a packet to the remote machine, with error checking.
01f9e8fa
DJ
454 The data of the packet is in BUF, and the length of the
455 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
c906108c
SS
456
457int
01f9e8fa 458putpkt_binary (char *buf, int cnt)
c906108c
SS
459{
460 int i;
461 unsigned char csum = 0;
0a30fbc4 462 char *buf2;
c906108c 463 char buf3[1];
c906108c
SS
464 char *p;
465
0a30fbc4
DJ
466 buf2 = malloc (PBUFSIZ);
467
c906108c
SS
468 /* Copy the packet into buffer BUF2, encapsulating it
469 and giving it a checksum. */
470
471 p = buf2;
472 *p++ = '$';
473
5ffff7c1
DJ
474 for (i = 0; i < cnt;)
475 i += try_rle (buf + i, cnt - i, &csum, &p);
476
c906108c
SS
477 *p++ = '#';
478 *p++ = tohex ((csum >> 4) & 0xf);
479 *p++ = tohex (csum & 0xf);
480
481 *p = '\0';
482
483 /* Send it over and over until we get a positive ack. */
484
485 do
486 {
487 int cc;
488
b80864fb 489 if (send (remote_desc, buf2, p - buf2, 0) != p - buf2)
c906108c
SS
490 {
491 perror ("putpkt(write)");
492 return -1;
493 }
494
495 if (remote_debug)
0d62e5e8
DJ
496 {
497 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
498 fflush (stderr);
499 }
b80864fb 500 cc = recv (remote_desc, buf3, 1, 0);
c906108c 501 if (remote_debug)
0d62e5e8
DJ
502 {
503 fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
504 fflush (stderr);
505 }
506
c906108c
SS
507 if (cc <= 0)
508 {
509 if (cc == 0)
510 fprintf (stderr, "putpkt(read): Got EOF\n");
511 else
512 perror ("putpkt(read)");
513
0a30fbc4 514 free (buf2);
c906108c
SS
515 return -1;
516 }
0d62e5e8
DJ
517
518 /* Check for an input interrupt while we're here. */
519 if (buf3[0] == '\003')
e5379b03 520 (*the_target->send_signal) (SIGINT);
c906108c
SS
521 }
522 while (buf3[0] != '+');
523
0a30fbc4 524 free (buf2);
c906108c
SS
525 return 1; /* Success! */
526}
527
01f9e8fa
DJ
528/* Send a packet to the remote machine, with error checking. The data
529 of the packet is in BUF, and the packet should be a NUL-terminated
530 string. Returns >= 0 on success, -1 otherwise. */
531
532int
533putpkt (char *buf)
534{
535 return putpkt_binary (buf, strlen (buf));
536}
537
b80864fb 538#ifndef USE_WIN32API
01f9e8fa 539
c906108c
SS
540/* Come here when we get an input interrupt from the remote side. This
541 interrupt should only be active while we are waiting for the child to do
542 something. About the only thing that should come through is a ^C, which
543 will cause us to send a SIGINT to the child. */
544
545static void
0a30fbc4 546input_interrupt (int unused)
c906108c 547{
cf30a8e1
C
548 fd_set readset;
549 struct timeval immediate = { 0, 0 };
c906108c 550
cf30a8e1
C
551 /* Protect against spurious interrupts. This has been observed to
552 be a problem under NetBSD 1.4 and 1.5. */
c906108c 553
cf30a8e1
C
554 FD_ZERO (&readset);
555 FD_SET (remote_desc, &readset);
556 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 557 {
cf30a8e1 558 int cc;
fd500816 559 char c = 0;
cf30a8e1 560
b80864fb 561 cc = recv (remote_desc, &c, 1, 0);
c906108c 562
cf30a8e1
C
563 if (cc != 1 || c != '\003')
564 {
fd500816
DJ
565 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
566 cc, c, c);
cf30a8e1
C
567 return;
568 }
569
e5379b03 570 (*the_target->send_signal) (SIGINT);
cf30a8e1 571 }
c906108c 572}
b80864fb
DJ
573#endif
574
575/* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
576 accept Control-C from the client, and must be disabled when talking to
577 the client. */
c906108c 578
62ea82f5
DJ
579void
580block_async_io (void)
581{
b80864fb 582#ifndef USE_WIN32API
62ea82f5
DJ
583 sigset_t sigio_set;
584 sigemptyset (&sigio_set);
585 sigaddset (&sigio_set, SIGIO);
586 sigprocmask (SIG_BLOCK, &sigio_set, NULL);
b80864fb 587#endif
62ea82f5
DJ
588}
589
590void
591unblock_async_io (void)
592{
b80864fb 593#ifndef USE_WIN32API
62ea82f5
DJ
594 sigset_t sigio_set;
595 sigemptyset (&sigio_set);
596 sigaddset (&sigio_set, SIGIO);
597 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
b80864fb 598#endif
62ea82f5
DJ
599}
600
fd500816
DJ
601/* Current state of asynchronous I/O. */
602static int async_io_enabled;
603
604/* Enable asynchronous I/O. */
c906108c 605void
fba45db2 606enable_async_io (void)
c906108c 607{
fd500816
DJ
608 if (async_io_enabled)
609 return;
610
b80864fb 611#ifndef USE_WIN32API
c906108c 612 signal (SIGIO, input_interrupt);
b80864fb 613#endif
fd500816 614 async_io_enabled = 1;
c906108c
SS
615}
616
fd500816 617/* Disable asynchronous I/O. */
c906108c 618void
fba45db2 619disable_async_io (void)
c906108c 620{
fd500816
DJ
621 if (!async_io_enabled)
622 return;
623
b80864fb 624#ifndef USE_WIN32API
c906108c 625 signal (SIGIO, SIG_IGN);
b80864fb 626#endif
fd500816 627 async_io_enabled = 0;
c906108c
SS
628}
629
630/* Returns next char from remote GDB. -1 if error. */
631
632static int
fba45db2 633readchar (void)
c906108c 634{
01f9e8fa 635 static unsigned char buf[BUFSIZ];
c906108c 636 static int bufcnt = 0;
01f9e8fa 637 static unsigned char *bufp;
c906108c
SS
638
639 if (bufcnt-- > 0)
01f9e8fa 640 return *bufp++;
c906108c 641
b80864fb 642 bufcnt = recv (remote_desc, buf, sizeof (buf), 0);
c906108c
SS
643
644 if (bufcnt <= 0)
645 {
646 if (bufcnt == 0)
647 fprintf (stderr, "readchar: Got EOF\n");
648 else
649 perror ("readchar");
650
651 return -1;
652 }
653
654 bufp = buf;
655 bufcnt--;
656 return *bufp++ & 0x7f;
657}
658
659/* Read a packet from the remote machine, with error checking,
660 and store it in BUF. Returns length of packet, or negative if error. */
661
662int
fba45db2 663getpkt (char *buf)
c906108c
SS
664{
665 char *bp;
666 unsigned char csum, c1, c2;
667 int c;
668
669 while (1)
670 {
671 csum = 0;
672
673 while (1)
674 {
675 c = readchar ();
676 if (c == '$')
677 break;
678 if (remote_debug)
0d62e5e8
DJ
679 {
680 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
681 fflush (stderr);
682 }
683
c906108c
SS
684 if (c < 0)
685 return -1;
686 }
687
688 bp = buf;
689 while (1)
690 {
691 c = readchar ();
692 if (c < 0)
693 return -1;
694 if (c == '#')
695 break;
696 *bp++ = c;
697 csum += c;
698 }
699 *bp = 0;
700
701 c1 = fromhex (readchar ());
702 c2 = fromhex (readchar ());
c5aa993b 703
c906108c
SS
704 if (csum == (c1 << 4) + c2)
705 break;
706
707 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
708 (c1 << 4) + c2, csum, buf);
b80864fb 709 send (remote_desc, "-", 1, 0);
c906108c
SS
710 }
711
712 if (remote_debug)
0d62e5e8
DJ
713 {
714 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
715 fflush (stderr);
716 }
c906108c 717
b80864fb 718 send (remote_desc, "+", 1, 0);
c906108c
SS
719
720 if (remote_debug)
0d62e5e8
DJ
721 {
722 fprintf (stderr, "[sent ack]\n");
723 fflush (stderr);
724 }
725
c906108c
SS
726 return bp - buf;
727}
728
729void
fba45db2 730write_ok (char *buf)
c906108c
SS
731{
732 buf[0] = 'O';
733 buf[1] = 'K';
734 buf[2] = '\0';
735}
736
737void
fba45db2 738write_enn (char *buf)
c906108c 739{
c89dc5d4 740 /* Some day, we should define the meanings of the error codes... */
c906108c 741 buf[0] = 'E';
c89dc5d4
DJ
742 buf[1] = '0';
743 buf[2] = '1';
c906108c
SS
744 buf[3] = '\0';
745}
746
747void
f450004a 748convert_int_to_ascii (unsigned char *from, char *to, int n)
c906108c
SS
749{
750 int nib;
f450004a 751 int ch;
c906108c
SS
752 while (n--)
753 {
754 ch = *from++;
755 nib = ((ch & 0xf0) >> 4) & 0x0f;
756 *to++ = tohex (nib);
757 nib = ch & 0x0f;
758 *to++ = tohex (nib);
759 }
760 *to++ = 0;
761}
762
763
764void
f450004a 765convert_ascii_to_int (char *from, unsigned char *to, int n)
c906108c
SS
766{
767 int nib1, nib2;
768 while (n--)
769 {
770 nib1 = fromhex (*from++);
771 nib2 = fromhex (*from++);
772 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
773 }
774}
775
776static char *
fba45db2 777outreg (int regno, char *buf)
c906108c 778{
5c44784c
JM
779 if ((regno >> 12) != 0)
780 *buf++ = tohex ((regno >> 12) & 0xf);
781 if ((regno >> 8) != 0)
782 *buf++ = tohex ((regno >> 8) & 0xf);
783 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
784 *buf++ = tohex (regno & 0xf);
785 *buf++ = ':';
0d62e5e8
DJ
786 collect_register_as_string (regno, buf);
787 buf += 2 * register_size (regno);
c906108c
SS
788 *buf++ = ';';
789
790 return buf;
791}
792
0d62e5e8
DJ
793void
794new_thread_notify (int id)
795{
796 char own_buf[256];
797
798 /* The `n' response is not yet part of the remote protocol. Do nothing. */
799 if (1)
800 return;
801
802 if (server_waiting == 0)
803 return;
804
805 sprintf (own_buf, "n%x", id);
806 disable_async_io ();
807 putpkt (own_buf);
808 enable_async_io ();
809}
810
811void
812dead_thread_notify (int id)
813{
814 char own_buf[256];
815
816 /* The `x' response is not yet part of the remote protocol. Do nothing. */
817 if (1)
818 return;
819
820 sprintf (own_buf, "x%x", id);
821 disable_async_io ();
822 putpkt (own_buf);
823 enable_async_io ();
824}
825
c906108c 826void
b80864fb 827prepare_resume_reply (char *buf, char status, unsigned char sig)
c906108c 828{
b80864fb 829 int nib;
c906108c
SS
830
831 *buf++ = status;
832
0e98d0a7 833 nib = ((sig & 0xf0) >> 4);
c906108c 834 *buf++ = tohex (nib);
0e98d0a7 835 nib = sig & 0x0f;
c906108c
SS
836 *buf++ = tohex (nib);
837
838 if (status == 'T')
839 {
0a30fbc4 840 const char **regp = gdbserver_expedite_regs;
e013ee27
OF
841
842 if (the_target->stopped_by_watchpoint != NULL
843 && (*the_target->stopped_by_watchpoint) ())
844 {
845 CORE_ADDR addr;
846 int i;
847
848 strncpy (buf, "watch:", 6);
849 buf += 6;
850
851 addr = (*the_target->stopped_data_address) ();
852
853 /* Convert each byte of the address into two hexadecimal chars.
854 Note that we take sizeof (void *) instead of sizeof (addr);
855 this is to avoid sending a 64-bit address to a 32-bit GDB. */
856 for (i = sizeof (void *) * 2; i > 0; i--)
857 {
858 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
859 }
860 *buf++ = ';';
861 }
862
0a30fbc4 863 while (*regp)
5c44784c 864 {
0a30fbc4
DJ
865 buf = outreg (find_regno (*regp), buf);
866 regp ++;
5c44784c 867 }
c906108c 868
0d62e5e8
DJ
869 /* Formerly, if the debugger had not used any thread features we would not
870 burden it with a thread status response. This was for the benefit of
871 GDB 4.13 and older. However, in recent GDB versions the check
872 (``if (cont_thread != 0)'') does not have the desired effect because of
873 sillyness in the way that the remote protocol handles specifying a thread.
874 Since thread support relies on qSymbol support anyway, assume GDB can handle
875 threads. */
876
877 if (using_threads)
c906108c 878 {
b92a518e
DJ
879 unsigned int gdb_id_from_wait;
880
0d62e5e8
DJ
881 /* FIXME right place to set this? */
882 thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id;
b92a518e 883 gdb_id_from_wait = thread_to_gdb_id (current_inferior);
a06660f7 884
0d62e5e8 885 if (debug_threads)
a1928bad 886 fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait);
89a208da
DJ
887 /* This if (1) ought to be unnecessary. But remote_wait in GDB
888 will claim this event belongs to inferior_ptid if we do not
889 specify a thread, and there's no way for gdbserver to know
890 what inferior_ptid is. */
891 if (1 || old_thread_from_wait != thread_from_wait)
c906108c 892 {
0d62e5e8 893 general_thread = thread_from_wait;
a06660f7 894 sprintf (buf, "thread:%x;", gdb_id_from_wait);
c906108c
SS
895 buf += strlen (buf);
896 old_thread_from_wait = thread_from_wait;
897 }
898 }
899 }
900 /* For W and X, we're done. */
901 *buf++ = 0;
902}
903
904void
fba45db2 905decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
906{
907 int i = 0, j = 0;
908 char ch;
909 *mem_addr_ptr = *len_ptr = 0;
910
911 while ((ch = from[i++]) != ',')
912 {
913 *mem_addr_ptr = *mem_addr_ptr << 4;
914 *mem_addr_ptr |= fromhex (ch) & 0x0f;
915 }
916
917 for (j = 0; j < 4; j++)
918 {
919 if ((ch = from[i++]) == 0)
920 break;
921 *len_ptr = *len_ptr << 4;
922 *len_ptr |= fromhex (ch) & 0x0f;
923 }
924}
925
926void
fba45db2 927decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
f450004a 928 unsigned char *to)
c906108c
SS
929{
930 int i = 0;
931 char ch;
932 *mem_addr_ptr = *len_ptr = 0;
933
934 while ((ch = from[i++]) != ',')
935 {
936 *mem_addr_ptr = *mem_addr_ptr << 4;
937 *mem_addr_ptr |= fromhex (ch) & 0x0f;
938 }
939
940 while ((ch = from[i++]) != ':')
941 {
942 *len_ptr = *len_ptr << 4;
943 *len_ptr |= fromhex (ch) & 0x0f;
944 }
945
946 convert_ascii_to_int (&from[i++], to, *len_ptr);
947}
2f2893d9 948
01f9e8fa
DJ
949int
950decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
951 unsigned int *len_ptr, unsigned char *to)
952{
953 int i = 0;
954 char ch;
955 *mem_addr_ptr = *len_ptr = 0;
956
957 while ((ch = from[i++]) != ',')
958 {
959 *mem_addr_ptr = *mem_addr_ptr << 4;
960 *mem_addr_ptr |= fromhex (ch) & 0x0f;
961 }
962
963 while ((ch = from[i++]) != ':')
964 {
965 *len_ptr = *len_ptr << 4;
966 *len_ptr |= fromhex (ch) & 0x0f;
967 }
968
969 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
970 to, *len_ptr) != *len_ptr)
971 return -1;
972
973 return 0;
974}
975
fd500816
DJ
976/* Ask GDB for the address of NAME, and return it in ADDRP if found.
977 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
978
2f2893d9
DJ
979int
980look_up_one_symbol (const char *name, CORE_ADDR *addrp)
981{
982 char own_buf[266], *p, *q;
983 int len;
fd500816
DJ
984 struct sym_cache *sym;
985
986 /* Check the cache first. */
987 for (sym = symbol_cache; sym; sym = sym->next)
988 if (strcmp (name, sym->name) == 0)
989 {
990 *addrp = sym->addr;
991 return 1;
992 }
2f2893d9 993
ea025f5f
DJ
994 /* If we've passed the call to thread_db_look_up_symbols, then
995 anything not in the cache must not exist; we're not interested
996 in any libraries loaded after that point, only in symbols in
997 libpthread.so. It might not be an appropriate time to look
998 up a symbol, e.g. while we're trying to fetch registers. */
999 if (all_symbols_looked_up)
1000 return 0;
1001
2f2893d9
DJ
1002 /* Send the request. */
1003 strcpy (own_buf, "qSymbol:");
1004 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
1005 if (putpkt (own_buf) < 0)
1006 return -1;
1007
1008 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1009 len = getpkt (own_buf);
1010 if (len < 0)
1011 return -1;
1012
1013 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
1014 {
1015 /* Malformed response. */
1016 if (remote_debug)
0d62e5e8
DJ
1017 {
1018 fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
1019 fflush (stderr);
1020 }
1021
2f2893d9
DJ
1022 return -1;
1023 }
1024
1025 p = own_buf + strlen ("qSymbol:");
1026 q = p;
1027 while (*q && *q != ':')
1028 q++;
1029
1030 /* Make sure we found a value for the symbol. */
1031 if (p == q || *q == '\0')
1032 return 0;
1033
1034 decode_address (addrp, p, q - p);
fd500816
DJ
1035
1036 /* Save the symbol in our cache. */
1037 sym = malloc (sizeof (*sym));
1038 sym->name = strdup (name);
1039 sym->addr = *addrp;
1040 sym->next = symbol_cache;
1041 symbol_cache = sym;
1042
2f2893d9
DJ
1043 return 1;
1044}