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