1 /* Remote utility routines for the remote server for GDB.
2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
27 #include <sys/ioctl.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
32 #include <netinet/tcp.h>
33 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
40 /* A cache entry for a successfully looked-up symbol. */
45 struct sym_cache
*next
;
48 /* The symbol cache. */
49 static struct sym_cache
*symbol_cache
;
52 struct ui_file
*gdb_stdlog
;
54 static int remote_desc
;
56 /* FIXME headerize? */
57 extern int using_threads
;
58 extern int debug_threads
;
60 /* Open a connection to a remote debugger.
61 NAME is the filename used for communication. */
64 remote_open (char *name
)
68 if (!strchr (name
, ':'))
70 remote_desc
= open (name
, O_RDWR
);
72 perror_with_name ("Could not open remote device");
76 struct termios termios
;
77 tcgetattr (remote_desc
, &termios
);
82 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
83 termios
.c_cflag
|= CLOCAL
| CS8
;
84 termios
.c_cc
[VMIN
] = 1;
85 termios
.c_cc
[VTIME
] = 0;
87 tcsetattr (remote_desc
, TCSANOW
, &termios
);
94 ioctl (remote_desc
, TCGETA
, &termio
);
99 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
100 termio
.c_cflag
|= CLOCAL
| CS8
;
101 termio
.c_cc
[VMIN
] = 1;
102 termio
.c_cc
[VTIME
] = 0;
104 ioctl (remote_desc
, TCSETA
, &termio
);
112 ioctl (remote_desc
, TIOCGETP
, &sg
);
114 ioctl (remote_desc
, TIOCSETP
, &sg
);
118 fprintf (stderr
, "Remote debugging using %s\n", name
);
124 struct sockaddr_in sockaddr
;
128 port_str
= strchr (name
, ':');
130 port
= atoi (port_str
+ 1);
132 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
134 perror_with_name ("Can't open socket");
136 /* Allow rapid reuse of this port. */
138 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
141 sockaddr
.sin_family
= PF_INET
;
142 sockaddr
.sin_port
= htons (port
);
143 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
145 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
146 || listen (tmp_desc
, 1))
147 perror_with_name ("Can't bind address");
149 fprintf (stderr
, "Listening on port %d\n", port
);
151 tmp
= sizeof (sockaddr
);
152 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
153 if (remote_desc
== -1)
154 perror_with_name ("Accept failed");
156 /* Enable TCP keep alive process. */
158 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
160 /* Tell TCP not to delay small packets. This greatly speeds up
161 interactive response. */
163 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
164 (char *) &tmp
, sizeof (tmp
));
166 close (tmp_desc
); /* No longer need this */
168 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
169 exits when the remote side dies. */
171 /* Convert IP address to string. */
172 fprintf (stderr
, "Remote debugging from host %s\n",
173 inet_ntoa (sockaddr
.sin_addr
));
176 #if defined(F_SETFL) && defined (FASYNC)
177 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
178 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
179 #if defined (F_SETOWN)
180 fcntl (remote_desc
, F_SETOWN
, getpid ());
192 /* Convert hex digit A to a number. */
197 if (a
>= '0' && a
<= '9')
199 else if (a
>= 'a' && a
<= 'f')
202 error ("Reply contains invalid hex digit");
207 unhexify (char *bin
, const char *hex
, int count
)
211 for (i
= 0; i
< count
; i
++)
213 if (hex
[0] == 0 || hex
[1] == 0)
215 /* Hex string is short, or of uneven length.
216 Return the count that has been converted so far. */
219 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
226 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
233 for (i
= 0; i
< len
; i
++)
237 addr
= addr
| (fromhex (ch
) & 0x0f);
242 /* Convert number NIB to a hex digit. */
250 return 'a' + nib
- 10;
254 hexify (char *hex
, const char *bin
, int count
)
258 /* May use a length, or a nul-terminated string as input. */
260 count
= strlen (bin
);
262 for (i
= 0; i
< count
; i
++)
264 *hex
++ = tohex ((*bin
>> 4) & 0xf);
265 *hex
++ = tohex (*bin
++ & 0xf);
271 /* Send a packet to the remote machine, with error checking.
272 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
278 unsigned char csum
= 0;
281 int cnt
= strlen (buf
);
284 buf2
= malloc (PBUFSIZ
);
286 /* Copy the packet into buffer BUF2, encapsulating it
287 and giving it a checksum. */
292 for (i
= 0; i
< cnt
; i
++)
298 *p
++ = tohex ((csum
>> 4) & 0xf);
299 *p
++ = tohex (csum
& 0xf);
303 /* Send it over and over until we get a positive ack. */
309 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
311 perror ("putpkt(write)");
317 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
320 cc
= read (remote_desc
, buf3
, 1);
323 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
330 fprintf (stderr
, "putpkt(read): Got EOF\n");
332 perror ("putpkt(read)");
338 /* Check for an input interrupt while we're here. */
339 if (buf3
[0] == '\003')
340 (*the_target
->send_signal
) (SIGINT
);
342 while (buf3
[0] != '+');
345 return 1; /* Success! */
348 /* Come here when we get an input interrupt from the remote side. This
349 interrupt should only be active while we are waiting for the child to do
350 something. About the only thing that should come through is a ^C, which
351 will cause us to send a SIGINT to the child. */
354 input_interrupt (int unused
)
357 struct timeval immediate
= { 0, 0 };
359 /* Protect against spurious interrupts. This has been observed to
360 be a problem under NetBSD 1.4 and 1.5. */
363 FD_SET (remote_desc
, &readset
);
364 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
369 cc
= read (remote_desc
, &c
, 1);
371 if (cc
!= 1 || c
!= '\003')
373 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
378 (*the_target
->send_signal
) (SIGINT
);
383 block_async_io (void)
386 sigemptyset (&sigio_set
);
387 sigaddset (&sigio_set
, SIGIO
);
388 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
392 unblock_async_io (void)
395 sigemptyset (&sigio_set
);
396 sigaddset (&sigio_set
, SIGIO
);
397 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
400 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
401 accept Control-C from the client, and must be disabled when talking to
404 /* Current state of asynchronous I/O. */
405 static int async_io_enabled
;
407 /* Enable asynchronous I/O. */
409 enable_async_io (void)
411 if (async_io_enabled
)
414 signal (SIGIO
, input_interrupt
);
415 async_io_enabled
= 1;
418 /* Disable asynchronous I/O. */
420 disable_async_io (void)
422 if (!async_io_enabled
)
425 signal (SIGIO
, SIG_IGN
);
426 async_io_enabled
= 0;
429 /* Returns next char from remote GDB. -1 if error. */
434 static char buf
[BUFSIZ
];
435 static int bufcnt
= 0;
439 return *bufp
++ & 0x7f;
441 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
446 fprintf (stderr
, "readchar: Got EOF\n");
455 return *bufp
++ & 0x7f;
458 /* Read a packet from the remote machine, with error checking,
459 and store it in BUF. Returns length of packet, or negative if error. */
465 unsigned char csum
, c1
, c2
;
479 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
500 c1
= fromhex (readchar ());
501 c2
= fromhex (readchar ());
503 if (csum
== (c1
<< 4) + c2
)
506 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
507 (c1
<< 4) + c2
, csum
, buf
);
508 write (remote_desc
, "-", 1);
513 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
517 write (remote_desc
, "+", 1);
521 fprintf (stderr
, "[sent ack]\n");
537 write_enn (char *buf
)
539 /* Some day, we should define the meanings of the error codes... */
547 convert_int_to_ascii (char *from
, char *to
, int n
)
554 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
564 convert_ascii_to_int (char *from
, char *to
, int n
)
569 nib1
= fromhex (*from
++);
570 nib2
= fromhex (*from
++);
571 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
576 outreg (int regno
, char *buf
)
578 if ((regno
>> 12) != 0)
579 *buf
++ = tohex ((regno
>> 12) & 0xf);
580 if ((regno
>> 8) != 0)
581 *buf
++ = tohex ((regno
>> 8) & 0xf);
582 *buf
++ = tohex ((regno
>> 4) & 0xf);
583 *buf
++ = tohex (regno
& 0xf);
585 collect_register_as_string (regno
, buf
);
586 buf
+= 2 * register_size (regno
);
593 new_thread_notify (int id
)
597 /* The `n' response is not yet part of the remote protocol. Do nothing. */
601 if (server_waiting
== 0)
604 sprintf (own_buf
, "n%x", id
);
611 dead_thread_notify (int id
)
615 /* The `x' response is not yet part of the remote protocol. Do nothing. */
619 sprintf (own_buf
, "x%x", id
);
626 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
632 sig
= (int)target_signal_from_host (signo
);
634 nib
= ((sig
& 0xf0) >> 4);
635 *buf
++ = tohex (nib
);
637 *buf
++ = tohex (nib
);
641 const char **regp
= gdbserver_expedite_regs
;
643 if (the_target
->stopped_by_watchpoint
!= NULL
644 && (*the_target
->stopped_by_watchpoint
) ())
649 strncpy (buf
, "watch:", 6);
652 addr
= (*the_target
->stopped_data_address
) ();
654 /* Convert each byte of the address into two hexadecimal chars.
655 Note that we take sizeof (void *) instead of sizeof (addr);
656 this is to avoid sending a 64-bit address to a 32-bit GDB. */
657 for (i
= sizeof (void *) * 2; i
> 0; i
--)
659 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
666 buf
= outreg (find_regno (*regp
), buf
);
670 /* Formerly, if the debugger had not used any thread features we would not
671 burden it with a thread status response. This was for the benefit of
672 GDB 4.13 and older. However, in recent GDB versions the check
673 (``if (cont_thread != 0)'') does not have the desired effect because of
674 sillyness in the way that the remote protocol handles specifying a thread.
675 Since thread support relies on qSymbol support anyway, assume GDB can handle
680 /* FIXME right place to set this? */
681 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
683 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
684 /* This if (1) ought to be unnecessary. But remote_wait in GDB
685 will claim this event belongs to inferior_ptid if we do not
686 specify a thread, and there's no way for gdbserver to know
687 what inferior_ptid is. */
688 if (1 || old_thread_from_wait
!= thread_from_wait
)
690 general_thread
= thread_from_wait
;
691 sprintf (buf
, "thread:%lx;", thread_from_wait
);
693 old_thread_from_wait
= thread_from_wait
;
697 /* For W and X, we're done. */
702 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
706 *mem_addr_ptr
= *len_ptr
= 0;
708 while ((ch
= from
[i
++]) != ',')
710 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
711 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
714 for (j
= 0; j
< 4; j
++)
716 if ((ch
= from
[i
++]) == 0)
718 *len_ptr
= *len_ptr
<< 4;
719 *len_ptr
|= fromhex (ch
) & 0x0f;
724 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
729 *mem_addr_ptr
= *len_ptr
= 0;
731 while ((ch
= from
[i
++]) != ',')
733 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
734 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
737 while ((ch
= from
[i
++]) != ':')
739 *len_ptr
= *len_ptr
<< 4;
740 *len_ptr
|= fromhex (ch
) & 0x0f;
743 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
746 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
747 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
750 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
752 char own_buf
[266], *p
, *q
;
754 struct sym_cache
*sym
;
756 /* Check the cache first. */
757 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
758 if (strcmp (name
, sym
->name
) == 0)
764 /* Send the request. */
765 strcpy (own_buf
, "qSymbol:");
766 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
767 if (putpkt (own_buf
) < 0)
770 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
771 len
= getpkt (own_buf
);
775 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
777 /* Malformed response. */
780 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
787 p
= own_buf
+ strlen ("qSymbol:");
789 while (*q
&& *q
!= ':')
792 /* Make sure we found a value for the symbol. */
793 if (p
== q
|| *q
== '\0')
796 decode_address (addrp
, p
, q
- p
);
798 /* Save the symbol in our cache. */
799 sym
= malloc (sizeof (*sym
));
800 sym
->name
= strdup (name
);
802 sym
->next
= symbol_cache
;