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