* gdb.base/macscp.exp (objfile): Move it to ${objdir}/${subdir}/.
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
CommitLineData
c906108c 1/* Remote utility routines for the remote server for GDB.
6aba47ca 2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
0fb0cc75 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
9b254dd1 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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "server.h"
22#include "terminal.h"
23#include <stdio.h>
24#include <string.h>
b80864fb 25#if HAVE_SYS_IOCTL_H
c906108c 26#include <sys/ioctl.h>
b80864fb 27#endif
68070c10 28#if HAVE_SYS_FILE_H
c906108c 29#include <sys/file.h>
68070c10 30#endif
b80864fb 31#if HAVE_NETINET_IN_H
c906108c 32#include <netinet/in.h>
b80864fb
DJ
33#endif
34#if HAVE_SYS_SOCKET_H
c906108c 35#include <sys/socket.h>
b80864fb
DJ
36#endif
37#if HAVE_NETDB_H
c906108c 38#include <netdb.h>
b80864fb
DJ
39#endif
40#if HAVE_NETINET_TCP_H
c906108c 41#include <netinet/tcp.h>
b80864fb
DJ
42#endif
43#if HAVE_SYS_IOCTL_H
c906108c 44#include <sys/ioctl.h>
b80864fb 45#endif
68070c10 46#if HAVE_SIGNAL_H
c906108c 47#include <signal.h>
68070c10
PA
48#endif
49#if HAVE_FCNTL_H
c906108c 50#include <fcntl.h>
68070c10 51#endif
cf30a8e1 52#include <sys/time.h>
68070c10 53#if HAVE_UNISTD_H
cf30a8e1 54#include <unistd.h>
68070c10 55#endif
b80864fb 56#if HAVE_ARPA_INET_H
0729219d 57#include <arpa/inet.h>
b80864fb 58#endif
8264bb58 59#include <sys/stat.h>
68070c10 60#if HAVE_ERRNO_H
8264bb58 61#include <errno.h>
68070c10 62#endif
b80864fb
DJ
63
64#if USE_WIN32API
65#include <winsock.h>
66#endif
c906108c 67
f450004a
DJ
68#ifndef HAVE_SOCKLEN_T
69typedef int socklen_t;
70#endif
71
7390519e
PA
72#if USE_WIN32API
73# define INVALID_DESCRIPTOR INVALID_SOCKET
74#else
75# define INVALID_DESCRIPTOR -1
76#endif
77
fd500816
DJ
78/* A cache entry for a successfully looked-up symbol. */
79struct sym_cache
80{
81 const char *name;
82 CORE_ADDR addr;
83 struct sym_cache *next;
84};
85
86/* The symbol cache. */
87static struct sym_cache *symbol_cache;
88
ea025f5f
DJ
89/* If this flag has been set, assume cache misses are
90 failures. */
91int all_symbols_looked_up;
92
c906108c 93int remote_debug = 0;
03863182 94struct ui_file *gdb_stdlog;
c906108c 95
7390519e 96static int remote_desc = INVALID_DESCRIPTOR;
c906108c 97
0d62e5e8
DJ
98/* FIXME headerize? */
99extern int using_threads;
100extern int debug_threads;
101
a6f3e723
SL
102/* If true, then GDB has requested noack mode. */
103int noack_mode = 0;
104/* If true, then we tell GDB to use noack mode by default. */
105int transport_is_reliable = 0;
106
0f48aa01 107#ifdef USE_WIN32API
68070c10
PA
108# define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
109# define write(fd, buf, len) send (fd, (char *) buf, len, 0)
0f48aa01
DJ
110#endif
111
c906108c
SS
112/* Open a connection to a remote debugger.
113 NAME is the filename used for communication. */
114
115void
fba45db2 116remote_open (char *name)
c906108c 117{
b80864fb 118#if defined(F_SETFL) && defined (FASYNC)
c906108c 119 int save_fcntl_flags;
b80864fb 120#endif
8264bb58
DJ
121 char *port_str;
122
123 port_str = strchr (name, ':');
124 if (port_str == NULL)
c906108c 125 {
b80864fb
DJ
126#ifdef USE_WIN32API
127 error ("Only <host>:<port> is supported on this platform.");
128#else
8264bb58
DJ
129 struct stat statbuf;
130
131 if (stat (name, &statbuf) == 0
132 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
133 remote_desc = open (name, O_RDWR);
134 else
135 {
136 errno = EINVAL;
137 remote_desc = -1;
138 }
139
c906108c
SS
140 if (remote_desc < 0)
141 perror_with_name ("Could not open remote device");
142
143#ifdef HAVE_TERMIOS
144 {
145 struct termios termios;
c5aa993b 146 tcgetattr (remote_desc, &termios);
c906108c
SS
147
148 termios.c_iflag = 0;
149 termios.c_oflag = 0;
150 termios.c_lflag = 0;
c5aa993b 151 termios.c_cflag &= ~(CSIZE | PARENB);
c906108c 152 termios.c_cflag |= CLOCAL | CS8;
d0608e50 153 termios.c_cc[VMIN] = 1;
c906108c
SS
154 termios.c_cc[VTIME] = 0;
155
c5aa993b 156 tcsetattr (remote_desc, TCSANOW, &termios);
c906108c
SS
157 }
158#endif
159
160#ifdef HAVE_TERMIO
161 {
162 struct termio termio;
163 ioctl (remote_desc, TCGETA, &termio);
164
165 termio.c_iflag = 0;
166 termio.c_oflag = 0;
167 termio.c_lflag = 0;
c5aa993b 168 termio.c_cflag &= ~(CSIZE | PARENB);
c906108c 169 termio.c_cflag |= CLOCAL | CS8;
d0608e50 170 termio.c_cc[VMIN] = 1;
c906108c
SS
171 termio.c_cc[VTIME] = 0;
172
173 ioctl (remote_desc, TCSETA, &termio);
174 }
175#endif
176
177#ifdef HAVE_SGTTY
178 {
179 struct sgttyb sg;
180
181 ioctl (remote_desc, TIOCGETP, &sg);
182 sg.sg_flags = RAW;
183 ioctl (remote_desc, TIOCSETP, &sg);
184 }
185#endif
186
e641a1ca 187 fprintf (stderr, "Remote debugging using %s\n", name);
b80864fb 188#endif /* USE_WIN32API */
a6f3e723
SL
189
190 transport_is_reliable = 0;
c906108c
SS
191 }
192 else
193 {
b80864fb
DJ
194#ifdef USE_WIN32API
195 static int winsock_initialized;
196#endif
c906108c
SS
197 int port;
198 struct sockaddr_in sockaddr;
f450004a 199 socklen_t tmp;
c906108c 200 int tmp_desc;
2d717e4f 201 char *port_end;
c906108c 202
2d717e4f
DJ
203 port = strtoul (port_str + 1, &port_end, 10);
204 if (port_str[1] == '\0' || *port_end != '\0')
205 fatal ("Bad port argument: %s", name);
c906108c 206
b80864fb
DJ
207#ifdef USE_WIN32API
208 if (!winsock_initialized)
209 {
210 WSADATA wsad;
211
212 WSAStartup (MAKEWORD (1, 0), &wsad);
213 winsock_initialized = 1;
214 }
215#endif
216
217 tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
c906108c
SS
218 if (tmp_desc < 0)
219 perror_with_name ("Can't open socket");
220
221 /* Allow rapid reuse of this port. */
222 tmp = 1;
c5aa993b
JM
223 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
224 sizeof (tmp));
c906108c
SS
225
226 sockaddr.sin_family = PF_INET;
c5aa993b 227 sockaddr.sin_port = htons (port);
c906108c
SS
228 sockaddr.sin_addr.s_addr = INADDR_ANY;
229
c5aa993b 230 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
c906108c
SS
231 || listen (tmp_desc, 1))
232 perror_with_name ("Can't bind address");
233
6f8486da
DJ
234 /* If port is zero, a random port will be selected, and the
235 fprintf below needs to know what port was selected. */
236 if (port == 0)
237 {
238 socklen_t len = sizeof (sockaddr);
239 if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0
240 || len < sizeof (sockaddr))
241 perror_with_name ("Can't determine port");
242 port = ntohs (sockaddr.sin_port);
243 }
244
6910d122 245 fprintf (stderr, "Listening on port %d\n", port);
b80864fb 246 fflush (stderr);
6910d122 247
c906108c 248 tmp = sizeof (sockaddr);
c5aa993b 249 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
c906108c
SS
250 if (remote_desc == -1)
251 perror_with_name ("Accept failed");
252
c906108c
SS
253 /* Enable TCP keep alive process. */
254 tmp = 1;
aa0403d9
PA
255 setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
256 (char *) &tmp, sizeof (tmp));
c906108c
SS
257
258 /* Tell TCP not to delay small packets. This greatly speeds up
c5aa993b 259 interactive response. */
c906108c 260 tmp = 1;
373fe97f 261 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
c5aa993b 262 (char *) &tmp, sizeof (tmp));
c906108c 263
b80864fb
DJ
264
265#ifndef USE_WIN32API
c906108c
SS
266 close (tmp_desc); /* No longer need this */
267
c5aa993b
JM
268 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
269 exits when the remote side dies. */
b80864fb
DJ
270#else
271 closesocket (tmp_desc); /* No longer need this */
272#endif
e641a1ca
ML
273
274 /* Convert IP address to string. */
275 fprintf (stderr, "Remote debugging from host %s\n",
276 inet_ntoa (sockaddr.sin_addr));
a6f3e723
SL
277
278 transport_is_reliable = 1;
c906108c
SS
279 }
280
281#if defined(F_SETFL) && defined (FASYNC)
282 save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
283 fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
cf30a8e1
C
284#if defined (F_SETOWN)
285 fcntl (remote_desc, F_SETOWN, getpid ());
94dfea5d 286#endif
cf30a8e1 287#endif
c906108c
SS
288}
289
290void
fba45db2 291remote_close (void)
c906108c 292{
b80864fb
DJ
293#ifdef USE_WIN32API
294 closesocket (remote_desc);
295#else
c906108c 296 close (remote_desc);
b80864fb 297#endif
c906108c
SS
298}
299
300/* Convert hex digit A to a number. */
301
302static int
fba45db2 303fromhex (int a)
c906108c
SS
304{
305 if (a >= '0' && a <= '9')
306 return a - '0';
307 else if (a >= 'a' && a <= 'f')
308 return a - 'a' + 10;
309 else
310 error ("Reply contains invalid hex digit");
0a30fbc4 311 return 0;
c906108c
SS
312}
313
ce3a066d
DJ
314int
315unhexify (char *bin, const char *hex, int count)
316{
317 int i;
318
319 for (i = 0; i < count; i++)
320 {
321 if (hex[0] == 0 || hex[1] == 0)
322 {
323 /* Hex string is short, or of uneven length.
324 Return the count that has been converted so far. */
325 return i;
326 }
327 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
328 hex += 2;
329 }
330 return i;
331}
332
dae5f5cf 333void
2f2893d9
DJ
334decode_address (CORE_ADDR *addrp, const char *start, int len)
335{
336 CORE_ADDR addr;
337 char ch;
338 int i;
339
340 addr = 0;
341 for (i = 0; i < len; i++)
342 {
343 ch = start[i];
344 addr = addr << 4;
345 addr = addr | (fromhex (ch) & 0x0f);
346 }
347 *addrp = addr;
348}
349
89be2091
DJ
350const char *
351decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
352{
353 const char *end;
354
355 end = start;
356 while (*end != '\0' && *end != ';')
357 end++;
358
359 decode_address (addrp, start, end - start);
360
361 if (*end == ';')
362 end++;
363 return end;
364}
365
c906108c
SS
366/* Convert number NIB to a hex digit. */
367
368static int
fba45db2 369tohex (int nib)
c906108c
SS
370{
371 if (nib < 10)
372 return '0' + nib;
373 else
374 return 'a' + nib - 10;
375}
376
ce3a066d
DJ
377int
378hexify (char *hex, const char *bin, int count)
379{
380 int i;
381
382 /* May use a length, or a nul-terminated string as input. */
383 if (count == 0)
384 count = strlen (bin);
385
386 for (i = 0; i < count; i++)
387 {
388 *hex++ = tohex ((*bin >> 4) & 0xf);
389 *hex++ = tohex (*bin++ & 0xf);
390 }
391 *hex = 0;
392 return i;
393}
394
01f9e8fa
DJ
395/* Convert BUFFER, binary data at least LEN bytes long, into escaped
396 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
397 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
398 (which may be more than *OUT_LEN due to escape characters). The
399 total number of bytes in the output buffer will be at most
400 OUT_MAXLEN. */
401
402int
403remote_escape_output (const gdb_byte *buffer, int len,
404 gdb_byte *out_buf, int *out_len,
405 int out_maxlen)
406{
407 int input_index, output_index;
408
409 output_index = 0;
410 for (input_index = 0; input_index < len; input_index++)
411 {
412 gdb_byte b = buffer[input_index];
413
414 if (b == '$' || b == '#' || b == '}' || b == '*')
415 {
416 /* These must be escaped. */
417 if (output_index + 2 > out_maxlen)
418 break;
419 out_buf[output_index++] = '}';
420 out_buf[output_index++] = b ^ 0x20;
421 }
422 else
423 {
424 if (output_index + 1 > out_maxlen)
425 break;
426 out_buf[output_index++] = b;
427 }
428 }
429
430 *out_len = input_index;
431 return output_index;
432}
433
434/* Convert BUFFER, escaped data LEN bytes long, into binary data
435 in OUT_BUF. Return the number of bytes written to OUT_BUF.
436 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
437
438 This function reverses remote_escape_output. It allows more
439 escaped characters than that function does, in particular because
440 '*' must be escaped to avoid the run-length encoding processing
441 in reading packets. */
442
443static int
444remote_unescape_input (const gdb_byte *buffer, int len,
445 gdb_byte *out_buf, int out_maxlen)
446{
447 int input_index, output_index;
448 int escaped;
449
450 output_index = 0;
451 escaped = 0;
452 for (input_index = 0; input_index < len; input_index++)
453 {
454 gdb_byte b = buffer[input_index];
455
456 if (output_index + 1 > out_maxlen)
457 error ("Received too much data from the target.");
458
459 if (escaped)
460 {
461 out_buf[output_index++] = b ^ 0x20;
462 escaped = 0;
463 }
464 else if (b == '}')
465 escaped = 1;
466 else
467 out_buf[output_index++] = b;
468 }
469
470 if (escaped)
471 error ("Unmatched escape character in target response.");
472
473 return output_index;
474}
475
5ffff7c1
DJ
476/* Look for a sequence of characters which can be run-length encoded.
477 If there are any, update *CSUM and *P. Otherwise, output the
478 single character. Return the number of characters consumed. */
479
480static int
481try_rle (char *buf, int remaining, unsigned char *csum, char **p)
482{
483 int n;
484
485 /* Always output the character. */
486 *csum += buf[0];
487 *(*p)++ = buf[0];
488
489 /* Don't go past '~'. */
490 if (remaining > 97)
491 remaining = 97;
492
493 for (n = 1; n < remaining; n++)
494 if (buf[n] != buf[0])
495 break;
496
497 /* N is the index of the first character not the same as buf[0].
498 buf[0] is counted twice, so by decrementing N, we get the number
499 of characters the RLE sequence will replace. */
500 n--;
501
502 if (n < 3)
503 return 1;
504
505 /* Skip the frame characters. The manual says to skip '+' and '-'
506 also, but there's no reason to. Unfortunately these two unusable
507 characters double the encoded length of a four byte zero
508 value. */
509 while (n + 29 == '$' || n + 29 == '#')
510 n--;
511
512 *csum += '*';
513 *(*p)++ = '*';
514 *csum += n + 29;
515 *(*p)++ = n + 29;
516
517 return n + 1;
518}
519
c906108c 520/* Send a packet to the remote machine, with error checking.
01f9e8fa
DJ
521 The data of the packet is in BUF, and the length of the
522 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
c906108c
SS
523
524int
01f9e8fa 525putpkt_binary (char *buf, int cnt)
c906108c
SS
526{
527 int i;
528 unsigned char csum = 0;
0a30fbc4 529 char *buf2;
c906108c 530 char buf3[1];
c906108c
SS
531 char *p;
532
bca929d3 533 buf2 = xmalloc (PBUFSIZ);
0a30fbc4 534
c906108c
SS
535 /* Copy the packet into buffer BUF2, encapsulating it
536 and giving it a checksum. */
537
538 p = buf2;
539 *p++ = '$';
540
5ffff7c1
DJ
541 for (i = 0; i < cnt;)
542 i += try_rle (buf + i, cnt - i, &csum, &p);
543
c906108c
SS
544 *p++ = '#';
545 *p++ = tohex ((csum >> 4) & 0xf);
546 *p++ = tohex (csum & 0xf);
547
548 *p = '\0';
549
550 /* Send it over and over until we get a positive ack. */
551
552 do
553 {
554 int cc;
555
0f48aa01 556 if (write (remote_desc, buf2, p - buf2) != p - buf2)
c906108c
SS
557 {
558 perror ("putpkt(write)");
f88c79e6 559 free (buf2);
c906108c
SS
560 return -1;
561 }
562
a6f3e723
SL
563 if (noack_mode)
564 {
565 /* Don't expect an ack then. */
566 if (remote_debug)
567 {
568 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
569 fflush (stderr);
570 }
571 break;
572 }
573
c906108c 574 if (remote_debug)
0d62e5e8
DJ
575 {
576 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
577 fflush (stderr);
578 }
0f48aa01 579 cc = read (remote_desc, buf3, 1);
c906108c 580 if (remote_debug)
0d62e5e8
DJ
581 {
582 fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
583 fflush (stderr);
584 }
585
c906108c
SS
586 if (cc <= 0)
587 {
588 if (cc == 0)
589 fprintf (stderr, "putpkt(read): Got EOF\n");
590 else
591 perror ("putpkt(read)");
592
0a30fbc4 593 free (buf2);
c906108c
SS
594 return -1;
595 }
0d62e5e8
DJ
596
597 /* Check for an input interrupt while we're here. */
2d717e4f 598 if (buf3[0] == '\003' && current_inferior != NULL)
ef57601b 599 (*the_target->request_interrupt) ();
c906108c
SS
600 }
601 while (buf3[0] != '+');
602
0a30fbc4 603 free (buf2);
c906108c
SS
604 return 1; /* Success! */
605}
606
01f9e8fa
DJ
607/* Send a packet to the remote machine, with error checking. The data
608 of the packet is in BUF, and the packet should be a NUL-terminated
609 string. Returns >= 0 on success, -1 otherwise. */
610
611int
612putpkt (char *buf)
613{
614 return putpkt_binary (buf, strlen (buf));
615}
616
c906108c
SS
617/* Come here when we get an input interrupt from the remote side. This
618 interrupt should only be active while we are waiting for the child to do
619 something. About the only thing that should come through is a ^C, which
ef57601b 620 will cause us to request child interruption. */
c906108c
SS
621
622static void
0a30fbc4 623input_interrupt (int unused)
c906108c 624{
cf30a8e1
C
625 fd_set readset;
626 struct timeval immediate = { 0, 0 };
c906108c 627
cf30a8e1
C
628 /* Protect against spurious interrupts. This has been observed to
629 be a problem under NetBSD 1.4 and 1.5. */
c906108c 630
cf30a8e1
C
631 FD_ZERO (&readset);
632 FD_SET (remote_desc, &readset);
633 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 634 {
cf30a8e1 635 int cc;
fd500816 636 char c = 0;
7390519e 637
0f48aa01 638 cc = read (remote_desc, &c, 1);
c906108c 639
2d717e4f 640 if (cc != 1 || c != '\003' || current_inferior == NULL)
cf30a8e1 641 {
fd500816
DJ
642 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
643 cc, c, c);
cf30a8e1
C
644 return;
645 }
7390519e 646
ef57601b 647 (*the_target->request_interrupt) ();
cf30a8e1 648 }
c906108c 649}
7390519e
PA
650
651/* Check if the remote side sent us an interrupt request (^C). */
652void
653check_remote_input_interrupt_request (void)
654{
655 /* This function may be called before establishing communications,
656 therefore we need to validate the remote descriptor. */
657
658 if (remote_desc == INVALID_DESCRIPTOR)
659 return;
660
661 input_interrupt (0);
662}
b80864fb
DJ
663
664/* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
665 accept Control-C from the client, and must be disabled when talking to
666 the client. */
c906108c 667
a20d5e98 668static void
62ea82f5
DJ
669unblock_async_io (void)
670{
b80864fb 671#ifndef USE_WIN32API
62ea82f5 672 sigset_t sigio_set;
a20d5e98 673
62ea82f5
DJ
674 sigemptyset (&sigio_set);
675 sigaddset (&sigio_set, SIGIO);
676 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
b80864fb 677#endif
62ea82f5
DJ
678}
679
fd500816
DJ
680/* Current state of asynchronous I/O. */
681static int async_io_enabled;
682
683/* Enable asynchronous I/O. */
c906108c 684void
fba45db2 685enable_async_io (void)
c906108c 686{
fd500816
DJ
687 if (async_io_enabled)
688 return;
689
b80864fb 690#ifndef USE_WIN32API
c906108c 691 signal (SIGIO, input_interrupt);
b80864fb 692#endif
fd500816 693 async_io_enabled = 1;
c906108c
SS
694}
695
fd500816 696/* Disable asynchronous I/O. */
c906108c 697void
fba45db2 698disable_async_io (void)
c906108c 699{
fd500816
DJ
700 if (!async_io_enabled)
701 return;
702
b80864fb 703#ifndef USE_WIN32API
c906108c 704 signal (SIGIO, SIG_IGN);
b80864fb 705#endif
fd500816 706 async_io_enabled = 0;
c906108c
SS
707}
708
a20d5e98
DJ
709void
710initialize_async_io (void)
711{
712 /* Make sure that async I/O starts disabled. */
713 async_io_enabled = 1;
714 disable_async_io ();
715
716 /* Make sure the signal is unblocked. */
717 unblock_async_io ();
718}
719
c906108c
SS
720/* Returns next char from remote GDB. -1 if error. */
721
722static int
fba45db2 723readchar (void)
c906108c 724{
01f9e8fa 725 static unsigned char buf[BUFSIZ];
c906108c 726 static int bufcnt = 0;
01f9e8fa 727 static unsigned char *bufp;
c906108c
SS
728
729 if (bufcnt-- > 0)
01f9e8fa 730 return *bufp++;
c906108c 731
0f48aa01 732 bufcnt = read (remote_desc, buf, sizeof (buf));
c906108c
SS
733
734 if (bufcnt <= 0)
735 {
736 if (bufcnt == 0)
737 fprintf (stderr, "readchar: Got EOF\n");
738 else
739 perror ("readchar");
740
741 return -1;
742 }
743
744 bufp = buf;
745 bufcnt--;
b79d787e 746 return *bufp++;
c906108c
SS
747}
748
749/* Read a packet from the remote machine, with error checking,
750 and store it in BUF. Returns length of packet, or negative if error. */
751
752int
fba45db2 753getpkt (char *buf)
c906108c
SS
754{
755 char *bp;
756 unsigned char csum, c1, c2;
757 int c;
758
759 while (1)
760 {
761 csum = 0;
762
763 while (1)
764 {
765 c = readchar ();
766 if (c == '$')
767 break;
768 if (remote_debug)
0d62e5e8
DJ
769 {
770 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
771 fflush (stderr);
772 }
773
c906108c
SS
774 if (c < 0)
775 return -1;
776 }
777
778 bp = buf;
779 while (1)
780 {
781 c = readchar ();
782 if (c < 0)
783 return -1;
784 if (c == '#')
785 break;
786 *bp++ = c;
787 csum += c;
788 }
789 *bp = 0;
790
791 c1 = fromhex (readchar ());
792 c2 = fromhex (readchar ());
c5aa993b 793
c906108c
SS
794 if (csum == (c1 << 4) + c2)
795 break;
796
a6f3e723
SL
797 if (noack_mode)
798 {
799 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
800 (c1 << 4) + c2, csum, buf);
801 /* Not much we can do, GDB wasn't expecting an ack/nac. */
802 break;
803 }
804
c906108c
SS
805 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
806 (c1 << 4) + c2, csum, buf);
0f48aa01 807 write (remote_desc, "-", 1);
c906108c
SS
808 }
809
a6f3e723 810 if (!noack_mode)
0d62e5e8 811 {
a6f3e723
SL
812 if (remote_debug)
813 {
814 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
815 fflush (stderr);
816 }
c906108c 817
a6f3e723 818 write (remote_desc, "+", 1);
c906108c 819
a6f3e723
SL
820 if (remote_debug)
821 {
822 fprintf (stderr, "[sent ack]\n");
823 fflush (stderr);
824 }
0d62e5e8 825 }
86b1f9c5
PM
826 else
827 {
828 if (remote_debug)
829 {
830 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf);
831 fflush (stderr);
832 }
833 }
0d62e5e8 834
c906108c
SS
835 return bp - buf;
836}
837
838void
fba45db2 839write_ok (char *buf)
c906108c
SS
840{
841 buf[0] = 'O';
842 buf[1] = 'K';
843 buf[2] = '\0';
844}
845
846void
fba45db2 847write_enn (char *buf)
c906108c 848{
c89dc5d4 849 /* Some day, we should define the meanings of the error codes... */
c906108c 850 buf[0] = 'E';
c89dc5d4
DJ
851 buf[1] = '0';
852 buf[2] = '1';
c906108c
SS
853 buf[3] = '\0';
854}
855
856void
f450004a 857convert_int_to_ascii (unsigned char *from, char *to, int n)
c906108c
SS
858{
859 int nib;
f450004a 860 int ch;
c906108c
SS
861 while (n--)
862 {
863 ch = *from++;
864 nib = ((ch & 0xf0) >> 4) & 0x0f;
865 *to++ = tohex (nib);
866 nib = ch & 0x0f;
867 *to++ = tohex (nib);
868 }
869 *to++ = 0;
870}
871
872
873void
f450004a 874convert_ascii_to_int (char *from, unsigned char *to, int n)
c906108c
SS
875{
876 int nib1, nib2;
877 while (n--)
878 {
879 nib1 = fromhex (*from++);
880 nib2 = fromhex (*from++);
881 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
882 }
883}
884
885static char *
fba45db2 886outreg (int regno, char *buf)
c906108c 887{
5c44784c
JM
888 if ((regno >> 12) != 0)
889 *buf++ = tohex ((regno >> 12) & 0xf);
890 if ((regno >> 8) != 0)
891 *buf++ = tohex ((regno >> 8) & 0xf);
892 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
893 *buf++ = tohex (regno & 0xf);
894 *buf++ = ':';
0d62e5e8
DJ
895 collect_register_as_string (regno, buf);
896 buf += 2 * register_size (regno);
c906108c
SS
897 *buf++ = ';';
898
899 return buf;
900}
901
0d62e5e8
DJ
902void
903new_thread_notify (int id)
904{
905 char own_buf[256];
906
907 /* The `n' response is not yet part of the remote protocol. Do nothing. */
908 if (1)
909 return;
910
911 if (server_waiting == 0)
912 return;
913
914 sprintf (own_buf, "n%x", id);
915 disable_async_io ();
916 putpkt (own_buf);
917 enable_async_io ();
918}
919
920void
921dead_thread_notify (int id)
922{
923 char own_buf[256];
924
925 /* The `x' response is not yet part of the remote protocol. Do nothing. */
926 if (1)
927 return;
928
929 sprintf (own_buf, "x%x", id);
930 disable_async_io ();
931 putpkt (own_buf);
932 enable_async_io ();
933}
934
c906108c 935void
b80864fb 936prepare_resume_reply (char *buf, char status, unsigned char sig)
c906108c 937{
b80864fb 938 int nib;
c906108c
SS
939
940 *buf++ = status;
941
0e98d0a7 942 nib = ((sig & 0xf0) >> 4);
c906108c 943 *buf++ = tohex (nib);
0e98d0a7 944 nib = sig & 0x0f;
c906108c
SS
945 *buf++ = tohex (nib);
946
947 if (status == 'T')
948 {
0a30fbc4 949 const char **regp = gdbserver_expedite_regs;
e013ee27
OF
950
951 if (the_target->stopped_by_watchpoint != NULL
952 && (*the_target->stopped_by_watchpoint) ())
953 {
954 CORE_ADDR addr;
955 int i;
956
957 strncpy (buf, "watch:", 6);
958 buf += 6;
959
960 addr = (*the_target->stopped_data_address) ();
961
962 /* Convert each byte of the address into two hexadecimal chars.
963 Note that we take sizeof (void *) instead of sizeof (addr);
964 this is to avoid sending a 64-bit address to a 32-bit GDB. */
965 for (i = sizeof (void *) * 2; i > 0; i--)
966 {
967 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
968 }
969 *buf++ = ';';
970 }
971
0a30fbc4 972 while (*regp)
5c44784c 973 {
0a30fbc4
DJ
974 buf = outreg (find_regno (*regp), buf);
975 regp ++;
5c44784c 976 }
c906108c 977
0d62e5e8
DJ
978 /* Formerly, if the debugger had not used any thread features we would not
979 burden it with a thread status response. This was for the benefit of
980 GDB 4.13 and older. However, in recent GDB versions the check
981 (``if (cont_thread != 0)'') does not have the desired effect because of
982 sillyness in the way that the remote protocol handles specifying a thread.
983 Since thread support relies on qSymbol support anyway, assume GDB can handle
984 threads. */
985
db42f210 986 if (using_threads && !disable_packet_Tthread)
c906108c 987 {
b92a518e
DJ
988 unsigned int gdb_id_from_wait;
989
0d62e5e8
DJ
990 /* FIXME right place to set this? */
991 thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id;
b92a518e 992 gdb_id_from_wait = thread_to_gdb_id (current_inferior);
a06660f7 993
0d62e5e8 994 if (debug_threads)
a1928bad 995 fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait);
89a208da
DJ
996 /* This if (1) ought to be unnecessary. But remote_wait in GDB
997 will claim this event belongs to inferior_ptid if we do not
998 specify a thread, and there's no way for gdbserver to know
999 what inferior_ptid is. */
1000 if (1 || old_thread_from_wait != thread_from_wait)
c906108c 1001 {
0d62e5e8 1002 general_thread = thread_from_wait;
a06660f7 1003 sprintf (buf, "thread:%x;", gdb_id_from_wait);
c906108c
SS
1004 buf += strlen (buf);
1005 old_thread_from_wait = thread_from_wait;
1006 }
1007 }
255e7678
DJ
1008
1009 if (dlls_changed)
1010 {
1011 strcpy (buf, "library:;");
1012 buf += strlen (buf);
1013 dlls_changed = 0;
1014 }
c906108c
SS
1015 }
1016 /* For W and X, we're done. */
1017 *buf++ = 0;
1018}
1019
1020void
fba45db2 1021decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
1022{
1023 int i = 0, j = 0;
1024 char ch;
1025 *mem_addr_ptr = *len_ptr = 0;
1026
1027 while ((ch = from[i++]) != ',')
1028 {
1029 *mem_addr_ptr = *mem_addr_ptr << 4;
1030 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1031 }
1032
1033 for (j = 0; j < 4; j++)
1034 {
1035 if ((ch = from[i++]) == 0)
1036 break;
1037 *len_ptr = *len_ptr << 4;
1038 *len_ptr |= fromhex (ch) & 0x0f;
1039 }
1040}
1041
1042void
fba45db2 1043decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
f450004a 1044 unsigned char *to)
c906108c
SS
1045{
1046 int i = 0;
1047 char ch;
1048 *mem_addr_ptr = *len_ptr = 0;
1049
1050 while ((ch = from[i++]) != ',')
1051 {
1052 *mem_addr_ptr = *mem_addr_ptr << 4;
1053 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1054 }
1055
1056 while ((ch = from[i++]) != ':')
1057 {
1058 *len_ptr = *len_ptr << 4;
1059 *len_ptr |= fromhex (ch) & 0x0f;
1060 }
1061
1062 convert_ascii_to_int (&from[i++], to, *len_ptr);
1063}
2f2893d9 1064
01f9e8fa
DJ
1065int
1066decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1067 unsigned int *len_ptr, unsigned char *to)
1068{
1069 int i = 0;
1070 char ch;
1071 *mem_addr_ptr = *len_ptr = 0;
1072
1073 while ((ch = from[i++]) != ',')
1074 {
1075 *mem_addr_ptr = *mem_addr_ptr << 4;
1076 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1077 }
1078
1079 while ((ch = from[i++]) != ':')
1080 {
1081 *len_ptr = *len_ptr << 4;
1082 *len_ptr |= fromhex (ch) & 0x0f;
1083 }
1084
1085 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1086 to, *len_ptr) != *len_ptr)
1087 return -1;
1088
1089 return 0;
1090}
1091
0e7f50da
UW
1092/* Decode a qXfer write request. */
1093int
1094decode_xfer_write (char *buf, int packet_len, char **annex, CORE_ADDR *offset,
1095 unsigned int *len, unsigned char *data)
1096{
1097 char ch;
1098
1099 /* Extract and NUL-terminate the annex. */
1100 *annex = buf;
1101 while (*buf && *buf != ':')
1102 buf++;
1103 if (*buf == '\0')
1104 return -1;
1105 *buf++ = 0;
1106
1107 /* Extract the offset. */
1108 *offset = 0;
1109 while ((ch = *buf++) != ':')
1110 {
1111 *offset = *offset << 4;
1112 *offset |= fromhex (ch) & 0x0f;
1113 }
1114
1115 /* Get encoded data. */
1116 packet_len -= buf - *annex;
1117 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1118 data, packet_len);
1119 return 0;
1120}
1121
08388c79
DE
1122/* Decode the parameters of a qSearch:memory packet. */
1123
1124int
1125decode_search_memory_packet (const char *buf, int packet_len,
1126 CORE_ADDR *start_addrp,
1127 CORE_ADDR *search_space_lenp,
1128 gdb_byte *pattern, unsigned int *pattern_lenp)
1129{
1130 const char *p = buf;
1131
1132 p = decode_address_to_semicolon (start_addrp, p);
1133 p = decode_address_to_semicolon (search_space_lenp, p);
1134 packet_len -= p - buf;
1135 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1136 pattern, packet_len);
1137 return 0;
1138}
1139
fd500816
DJ
1140/* Ask GDB for the address of NAME, and return it in ADDRP if found.
1141 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1142
2f2893d9
DJ
1143int
1144look_up_one_symbol (const char *name, CORE_ADDR *addrp)
1145{
1146 char own_buf[266], *p, *q;
1147 int len;
fd500816
DJ
1148 struct sym_cache *sym;
1149
1150 /* Check the cache first. */
1151 for (sym = symbol_cache; sym; sym = sym->next)
1152 if (strcmp (name, sym->name) == 0)
1153 {
1154 *addrp = sym->addr;
1155 return 1;
1156 }
2f2893d9 1157
ea025f5f
DJ
1158 /* If we've passed the call to thread_db_look_up_symbols, then
1159 anything not in the cache must not exist; we're not interested
1160 in any libraries loaded after that point, only in symbols in
1161 libpthread.so. It might not be an appropriate time to look
1162 up a symbol, e.g. while we're trying to fetch registers. */
1163 if (all_symbols_looked_up)
1164 return 0;
1165
2f2893d9
DJ
1166 /* Send the request. */
1167 strcpy (own_buf, "qSymbol:");
1168 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
1169 if (putpkt (own_buf) < 0)
1170 return -1;
1171
1172 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1173 len = getpkt (own_buf);
1174 if (len < 0)
1175 return -1;
1176
2bbe3cc1
DJ
1177 /* We ought to handle pretty much any packet at this point while we
1178 wait for the qSymbol "response". That requires re-entering the
1179 main loop. For now, this is an adequate approximation; allow
1180 GDB to read from memory while it figures out the address of the
1181 symbol. */
1182 while (own_buf[0] == 'm')
1183 {
1184 CORE_ADDR mem_addr;
1185 unsigned char *mem_buf;
1186 unsigned int mem_len;
1187
1188 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
bca929d3 1189 mem_buf = xmalloc (mem_len);
2bbe3cc1
DJ
1190 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1191 convert_int_to_ascii (mem_buf, own_buf, mem_len);
1192 else
1193 write_enn (own_buf);
1194 free (mem_buf);
1195 if (putpkt (own_buf) < 0)
1196 return -1;
1197 len = getpkt (own_buf);
1198 if (len < 0)
1199 return -1;
1200 }
1201
2f2893d9
DJ
1202 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
1203 {
2bbe3cc1 1204 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
2f2893d9
DJ
1205 return -1;
1206 }
1207
1208 p = own_buf + strlen ("qSymbol:");
1209 q = p;
1210 while (*q && *q != ':')
1211 q++;
1212
1213 /* Make sure we found a value for the symbol. */
1214 if (p == q || *q == '\0')
1215 return 0;
1216
1217 decode_address (addrp, p, q - p);
fd500816
DJ
1218
1219 /* Save the symbol in our cache. */
bca929d3
DE
1220 sym = xmalloc (sizeof (*sym));
1221 sym->name = xstrdup (name);
fd500816
DJ
1222 sym->addr = *addrp;
1223 sym->next = symbol_cache;
1224 symbol_cache = sym;
1225
2f2893d9
DJ
1226 return 1;
1227}
c74d0ad8
DJ
1228
1229void
bce7165d 1230monitor_output (const char *msg)
c74d0ad8 1231{
bca929d3 1232 char *buf = xmalloc (strlen (msg) * 2 + 2);
c74d0ad8
DJ
1233
1234 buf[0] = 'O';
1235 hexify (buf + 1, msg, 0);
1236
1237 putpkt (buf);
1238 free (buf);
1239}
255e7678
DJ
1240
1241/* Return a malloc allocated string with special characters from TEXT
1242 replaced by entity references. */
1243
1244char *
1245xml_escape_text (const char *text)
1246{
1247 char *result;
1248 int i, special;
1249
1250 /* Compute the length of the result. */
1251 for (i = 0, special = 0; text[i] != '\0'; i++)
1252 switch (text[i])
1253 {
1254 case '\'':
1255 case '\"':
1256 special += 5;
1257 break;
1258 case '&':
1259 special += 4;
1260 break;
1261 case '<':
1262 case '>':
1263 special += 3;
1264 break;
1265 default:
1266 break;
1267 }
1268
1269 /* Expand the result. */
bca929d3 1270 result = xmalloc (i + special + 1);
255e7678
DJ
1271 for (i = 0, special = 0; text[i] != '\0'; i++)
1272 switch (text[i])
1273 {
1274 case '\'':
1275 strcpy (result + i + special, "&apos;");
1276 special += 5;
1277 break;
1278 case '\"':
1279 strcpy (result + i + special, "&quot;");
1280 special += 5;
1281 break;
1282 case '&':
1283 strcpy (result + i + special, "&amp;");
1284 special += 4;
1285 break;
1286 case '<':
1287 strcpy (result + i + special, "&lt;");
1288 special += 3;
1289 break;
1290 case '>':
1291 strcpy (result + i + special, "&gt;");
1292 special += 3;
1293 break;
1294 default:
1295 result[i + special] = text[i];
1296 break;
1297 }
1298 result[i + special] = '\0';
1299
1300 return result;
1301}
07e059b5
VP
1302
1303void
1304buffer_grow (struct buffer *buffer, const char *data, size_t size)
1305{
1306 char *new_buffer;
1307 size_t new_buffer_size;
1308
1309 if (size == 0)
1310 return;
1311
1312 new_buffer_size = buffer->buffer_size;
1313
1314 if (new_buffer_size == 0)
1315 new_buffer_size = 1;
1316
1317 while (buffer->used_size + size > new_buffer_size)
1318 new_buffer_size *= 2;
1319 new_buffer = realloc (buffer->buffer, new_buffer_size);
1320 if (!new_buffer)
1321 abort ();
1322 memcpy (new_buffer + buffer->used_size, data, size);
1323 buffer->buffer = new_buffer;
1324 buffer->buffer_size = new_buffer_size;
1325 buffer->used_size += size;
1326}
1327
1328void
1329buffer_free (struct buffer *buffer)
1330{
1331 if (!buffer)
1332 return;
1333
1334 free (buffer->buffer);
1335 buffer->buffer = NULL;
1336 buffer->buffer_size = 0;
1337 buffer->used_size = 0;
1338}
1339
1340void
1341buffer_init (struct buffer *buffer)
1342{
1343 memset (buffer, 0, sizeof (*buffer));
1344}
1345
1346char*
1347buffer_finish (struct buffer *buffer)
1348{
1349 char *ret = buffer->buffer;
1350 buffer->buffer = NULL;
1351 buffer->buffer_size = 0;
1352 buffer->used_size = 0;
1353 return ret;
1354}
1355
1356void
1357buffer_xml_printf (struct buffer *buffer, const char *format, ...)
1358{
1359 va_list ap;
1360 const char *f;
1361 const char *prev;
1362 int percent = 0;
1363
1364 va_start (ap, format);
1365
1366 prev = format;
1367 for (f = format; *f; f++)
1368 {
1369 if (percent)
1370 {
1371 switch (*f)
1372 {
1373 case 's':
1374 {
1375 char *p;
1376 char *a = va_arg (ap, char *);
1377 buffer_grow (buffer, prev, f - prev - 1);
1378 p = xml_escape_text (a);
1379 buffer_grow_str (buffer, p);
1380 free (p);
1381 prev = f + 1;
1382 }
1383 break;
1384 }
1385 percent = 0;
1386 }
1387 else if (*f == '%')
1388 percent = 1;
1389 }
1390
1391 buffer_grow_str (buffer, prev);
1392 va_end (ap);
1393}
This page took 0.689683 seconds and 4 git commands to generate.