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
;
644 buf
= outreg (find_regno (*regp
), buf
);
648 /* Formerly, if the debugger had not used any thread features we would not
649 burden it with a thread status response. This was for the benefit of
650 GDB 4.13 and older. However, in recent GDB versions the check
651 (``if (cont_thread != 0)'') does not have the desired effect because of
652 sillyness in the way that the remote protocol handles specifying a thread.
653 Since thread support relies on qSymbol support anyway, assume GDB can handle
658 /* FIXME right place to set this? */
659 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
661 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
662 /* This if (1) ought to be unnecessary. But remote_wait in GDB
663 will claim this event belongs to inferior_ptid if we do not
664 specify a thread, and there's no way for gdbserver to know
665 what inferior_ptid is. */
666 if (1 || old_thread_from_wait
!= thread_from_wait
)
668 general_thread
= thread_from_wait
;
669 sprintf (buf
, "thread:%lx;", thread_from_wait
);
671 old_thread_from_wait
= thread_from_wait
;
675 /* For W and X, we're done. */
680 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
684 *mem_addr_ptr
= *len_ptr
= 0;
686 while ((ch
= from
[i
++]) != ',')
688 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
689 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
692 for (j
= 0; j
< 4; j
++)
694 if ((ch
= from
[i
++]) == 0)
696 *len_ptr
= *len_ptr
<< 4;
697 *len_ptr
|= fromhex (ch
) & 0x0f;
702 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
707 *mem_addr_ptr
= *len_ptr
= 0;
709 while ((ch
= from
[i
++]) != ',')
711 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
712 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
715 while ((ch
= from
[i
++]) != ':')
717 *len_ptr
= *len_ptr
<< 4;
718 *len_ptr
|= fromhex (ch
) & 0x0f;
721 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
724 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
725 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
728 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
730 char own_buf
[266], *p
, *q
;
732 struct sym_cache
*sym
;
734 /* Check the cache first. */
735 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
736 if (strcmp (name
, sym
->name
) == 0)
742 /* Send the request. */
743 strcpy (own_buf
, "qSymbol:");
744 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
745 if (putpkt (own_buf
) < 0)
748 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
749 len
= getpkt (own_buf
);
753 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
755 /* Malformed response. */
758 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
765 p
= own_buf
+ strlen ("qSymbol:");
767 while (*q
&& *q
!= ':')
770 /* Make sure we found a value for the symbol. */
771 if (p
== q
|| *q
== '\0')
774 decode_address (addrp
, p
, q
- p
);
776 /* Save the symbol in our cache. */
777 sym
= malloc (sizeof (*sym
));
778 sym
->name
= strdup (name
);
780 sym
->next
= symbol_cache
;
This page took 0.048882 seconds and 4 git commands to generate.