1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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 #ifndef HAVE_SOCKLEN_T
41 typedef int socklen_t
;
44 /* A cache entry for a successfully looked-up symbol. */
49 struct sym_cache
*next
;
52 /* The symbol cache. */
53 static struct sym_cache
*symbol_cache
;
56 struct ui_file
*gdb_stdlog
;
58 static int remote_desc
;
60 /* FIXME headerize? */
61 extern int using_threads
;
62 extern int debug_threads
;
64 /* Open a connection to a remote debugger.
65 NAME is the filename used for communication. */
68 remote_open (char *name
)
72 if (!strchr (name
, ':'))
74 remote_desc
= open (name
, O_RDWR
);
76 perror_with_name ("Could not open remote device");
80 struct termios termios
;
81 tcgetattr (remote_desc
, &termios
);
86 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
87 termios
.c_cflag
|= CLOCAL
| CS8
;
88 termios
.c_cc
[VMIN
] = 1;
89 termios
.c_cc
[VTIME
] = 0;
91 tcsetattr (remote_desc
, TCSANOW
, &termios
);
98 ioctl (remote_desc
, TCGETA
, &termio
);
103 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
104 termio
.c_cflag
|= CLOCAL
| CS8
;
105 termio
.c_cc
[VMIN
] = 1;
106 termio
.c_cc
[VTIME
] = 0;
108 ioctl (remote_desc
, TCSETA
, &termio
);
116 ioctl (remote_desc
, TIOCGETP
, &sg
);
118 ioctl (remote_desc
, TIOCSETP
, &sg
);
122 fprintf (stderr
, "Remote debugging using %s\n", name
);
128 struct sockaddr_in sockaddr
;
132 port_str
= strchr (name
, ':');
134 port
= atoi (port_str
+ 1);
136 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
138 perror_with_name ("Can't open socket");
140 /* Allow rapid reuse of this port. */
142 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
145 sockaddr
.sin_family
= PF_INET
;
146 sockaddr
.sin_port
= htons (port
);
147 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
149 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
150 || listen (tmp_desc
, 1))
151 perror_with_name ("Can't bind address");
153 fprintf (stderr
, "Listening on port %d\n", port
);
155 tmp
= sizeof (sockaddr
);
156 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
157 if (remote_desc
== -1)
158 perror_with_name ("Accept failed");
160 /* Enable TCP keep alive process. */
162 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
164 /* Tell TCP not to delay small packets. This greatly speeds up
165 interactive response. */
167 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
168 (char *) &tmp
, sizeof (tmp
));
170 close (tmp_desc
); /* No longer need this */
172 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
173 exits when the remote side dies. */
175 /* Convert IP address to string. */
176 fprintf (stderr
, "Remote debugging from host %s\n",
177 inet_ntoa (sockaddr
.sin_addr
));
180 #if defined(F_SETFL) && defined (FASYNC)
181 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
182 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
183 #if defined (F_SETOWN)
184 fcntl (remote_desc
, F_SETOWN
, getpid ());
196 /* Convert hex digit A to a number. */
201 if (a
>= '0' && a
<= '9')
203 else if (a
>= 'a' && a
<= 'f')
206 error ("Reply contains invalid hex digit");
211 unhexify (char *bin
, const char *hex
, int count
)
215 for (i
= 0; i
< count
; i
++)
217 if (hex
[0] == 0 || hex
[1] == 0)
219 /* Hex string is short, or of uneven length.
220 Return the count that has been converted so far. */
223 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
230 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
237 for (i
= 0; i
< len
; i
++)
241 addr
= addr
| (fromhex (ch
) & 0x0f);
246 /* Convert number NIB to a hex digit. */
254 return 'a' + nib
- 10;
258 hexify (char *hex
, const char *bin
, int count
)
262 /* May use a length, or a nul-terminated string as input. */
264 count
= strlen (bin
);
266 for (i
= 0; i
< count
; i
++)
268 *hex
++ = tohex ((*bin
>> 4) & 0xf);
269 *hex
++ = tohex (*bin
++ & 0xf);
275 /* Send a packet to the remote machine, with error checking.
276 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
282 unsigned char csum
= 0;
285 int cnt
= strlen (buf
);
288 buf2
= malloc (PBUFSIZ
);
290 /* Copy the packet into buffer BUF2, encapsulating it
291 and giving it a checksum. */
296 for (i
= 0; i
< cnt
; i
++)
302 *p
++ = tohex ((csum
>> 4) & 0xf);
303 *p
++ = tohex (csum
& 0xf);
307 /* Send it over and over until we get a positive ack. */
313 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
315 perror ("putpkt(write)");
321 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
324 cc
= read (remote_desc
, buf3
, 1);
327 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
334 fprintf (stderr
, "putpkt(read): Got EOF\n");
336 perror ("putpkt(read)");
342 /* Check for an input interrupt while we're here. */
343 if (buf3
[0] == '\003')
344 (*the_target
->send_signal
) (SIGINT
);
346 while (buf3
[0] != '+');
349 return 1; /* Success! */
352 /* Come here when we get an input interrupt from the remote side. This
353 interrupt should only be active while we are waiting for the child to do
354 something. About the only thing that should come through is a ^C, which
355 will cause us to send a SIGINT to the child. */
358 input_interrupt (int unused
)
361 struct timeval immediate
= { 0, 0 };
363 /* Protect against spurious interrupts. This has been observed to
364 be a problem under NetBSD 1.4 and 1.5. */
367 FD_SET (remote_desc
, &readset
);
368 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
373 cc
= read (remote_desc
, &c
, 1);
375 if (cc
!= 1 || c
!= '\003')
377 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
382 (*the_target
->send_signal
) (SIGINT
);
387 block_async_io (void)
390 sigemptyset (&sigio_set
);
391 sigaddset (&sigio_set
, SIGIO
);
392 sigprocmask (SIG_BLOCK
, &sigio_set
, NULL
);
396 unblock_async_io (void)
399 sigemptyset (&sigio_set
);
400 sigaddset (&sigio_set
, SIGIO
);
401 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
404 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
405 accept Control-C from the client, and must be disabled when talking to
408 /* Current state of asynchronous I/O. */
409 static int async_io_enabled
;
411 /* Enable asynchronous I/O. */
413 enable_async_io (void)
415 if (async_io_enabled
)
418 signal (SIGIO
, input_interrupt
);
419 async_io_enabled
= 1;
422 /* Disable asynchronous I/O. */
424 disable_async_io (void)
426 if (!async_io_enabled
)
429 signal (SIGIO
, SIG_IGN
);
430 async_io_enabled
= 0;
433 /* Returns next char from remote GDB. -1 if error. */
438 static char buf
[BUFSIZ
];
439 static int bufcnt
= 0;
443 return *bufp
++ & 0x7f;
445 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
450 fprintf (stderr
, "readchar: Got EOF\n");
459 return *bufp
++ & 0x7f;
462 /* Read a packet from the remote machine, with error checking,
463 and store it in BUF. Returns length of packet, or negative if error. */
469 unsigned char csum
, c1
, c2
;
483 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
504 c1
= fromhex (readchar ());
505 c2
= fromhex (readchar ());
507 if (csum
== (c1
<< 4) + c2
)
510 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
511 (c1
<< 4) + c2
, csum
, buf
);
512 write (remote_desc
, "-", 1);
517 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
521 write (remote_desc
, "+", 1);
525 fprintf (stderr
, "[sent ack]\n");
541 write_enn (char *buf
)
543 /* Some day, we should define the meanings of the error codes... */
551 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
558 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
568 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
573 nib1
= fromhex (*from
++);
574 nib2
= fromhex (*from
++);
575 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
580 outreg (int regno
, char *buf
)
582 if ((regno
>> 12) != 0)
583 *buf
++ = tohex ((regno
>> 12) & 0xf);
584 if ((regno
>> 8) != 0)
585 *buf
++ = tohex ((regno
>> 8) & 0xf);
586 *buf
++ = tohex ((regno
>> 4) & 0xf);
587 *buf
++ = tohex (regno
& 0xf);
589 collect_register_as_string (regno
, buf
);
590 buf
+= 2 * register_size (regno
);
597 new_thread_notify (int id
)
601 /* The `n' response is not yet part of the remote protocol. Do nothing. */
605 if (server_waiting
== 0)
608 sprintf (own_buf
, "n%x", id
);
615 dead_thread_notify (int id
)
619 /* The `x' response is not yet part of the remote protocol. Do nothing. */
623 sprintf (own_buf
, "x%x", id
);
630 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
636 sig
= (int)target_signal_from_host (signo
);
638 nib
= ((sig
& 0xf0) >> 4);
639 *buf
++ = tohex (nib
);
641 *buf
++ = tohex (nib
);
645 const char **regp
= gdbserver_expedite_regs
;
647 if (the_target
->stopped_by_watchpoint
!= NULL
648 && (*the_target
->stopped_by_watchpoint
) ())
653 strncpy (buf
, "watch:", 6);
656 addr
= (*the_target
->stopped_data_address
) ();
658 /* Convert each byte of the address into two hexadecimal chars.
659 Note that we take sizeof (void *) instead of sizeof (addr);
660 this is to avoid sending a 64-bit address to a 32-bit GDB. */
661 for (i
= sizeof (void *) * 2; i
> 0; i
--)
663 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
670 buf
= outreg (find_regno (*regp
), buf
);
674 /* Formerly, if the debugger had not used any thread features we would not
675 burden it with a thread status response. This was for the benefit of
676 GDB 4.13 and older. However, in recent GDB versions the check
677 (``if (cont_thread != 0)'') does not have the desired effect because of
678 sillyness in the way that the remote protocol handles specifying a thread.
679 Since thread support relies on qSymbol support anyway, assume GDB can handle
684 unsigned int gdb_id_from_wait
;
686 /* FIXME right place to set this? */
687 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
688 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
691 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
692 /* This if (1) ought to be unnecessary. But remote_wait in GDB
693 will claim this event belongs to inferior_ptid if we do not
694 specify a thread, and there's no way for gdbserver to know
695 what inferior_ptid is. */
696 if (1 || old_thread_from_wait
!= thread_from_wait
)
698 general_thread
= thread_from_wait
;
699 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
701 old_thread_from_wait
= thread_from_wait
;
705 /* For W and X, we're done. */
710 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
714 *mem_addr_ptr
= *len_ptr
= 0;
716 while ((ch
= from
[i
++]) != ',')
718 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
719 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
722 for (j
= 0; j
< 4; j
++)
724 if ((ch
= from
[i
++]) == 0)
726 *len_ptr
= *len_ptr
<< 4;
727 *len_ptr
|= fromhex (ch
) & 0x0f;
732 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
737 *mem_addr_ptr
= *len_ptr
= 0;
739 while ((ch
= from
[i
++]) != ',')
741 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
742 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
745 while ((ch
= from
[i
++]) != ':')
747 *len_ptr
= *len_ptr
<< 4;
748 *len_ptr
|= fromhex (ch
) & 0x0f;
751 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
754 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
755 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
758 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
760 char own_buf
[266], *p
, *q
;
762 struct sym_cache
*sym
;
764 /* Check the cache first. */
765 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
766 if (strcmp (name
, sym
->name
) == 0)
772 /* Send the request. */
773 strcpy (own_buf
, "qSymbol:");
774 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
775 if (putpkt (own_buf
) < 0)
778 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
779 len
= getpkt (own_buf
);
783 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
785 /* Malformed response. */
788 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
795 p
= own_buf
+ strlen ("qSymbol:");
797 while (*q
&& *q
!= ':')
800 /* Make sure we found a value for the symbol. */
801 if (p
== q
|| *q
== '\0')
804 decode_address (addrp
, p
, q
- p
);
806 /* Save the symbol in our cache. */
807 sym
= malloc (sizeof (*sym
));
808 sym
->name
= strdup (name
);
810 sym
->next
= symbol_cache
;