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>
40 struct ui_file
*gdb_stdlog
;
42 static int remote_desc
;
44 /* Open a connection to a remote debugger.
45 NAME is the filename used for communication. */
48 remote_open (char *name
)
52 if (!strchr (name
, ':'))
54 remote_desc
= open (name
, O_RDWR
);
56 perror_with_name ("Could not open remote device");
60 struct termios termios
;
61 tcgetattr (remote_desc
, &termios
);
66 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
67 termios
.c_cflag
|= CLOCAL
| CS8
;
68 termios
.c_cc
[VMIN
] = 1;
69 termios
.c_cc
[VTIME
] = 0;
71 tcsetattr (remote_desc
, TCSANOW
, &termios
);
78 ioctl (remote_desc
, TCGETA
, &termio
);
83 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
84 termio
.c_cflag
|= CLOCAL
| CS8
;
85 termio
.c_cc
[VMIN
] = 1;
86 termio
.c_cc
[VTIME
] = 0;
88 ioctl (remote_desc
, TCSETA
, &termio
);
96 ioctl (remote_desc
, TIOCGETP
, &sg
);
98 ioctl (remote_desc
, TIOCSETP
, &sg
);
102 fprintf (stderr
, "Remote debugging using %s\n", name
);
108 struct sockaddr_in sockaddr
;
112 port_str
= strchr (name
, ':');
114 port
= atoi (port_str
+ 1);
116 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
118 perror_with_name ("Can't open socket");
120 /* Allow rapid reuse of this port. */
122 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
125 sockaddr
.sin_family
= PF_INET
;
126 sockaddr
.sin_port
= htons (port
);
127 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
129 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
130 || listen (tmp_desc
, 1))
131 perror_with_name ("Can't bind address");
133 tmp
= sizeof (sockaddr
);
134 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
135 if (remote_desc
== -1)
136 perror_with_name ("Accept failed");
138 /* Enable TCP keep alive process. */
140 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
142 /* Tell TCP not to delay small packets. This greatly speeds up
143 interactive response. */
145 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
146 (char *) &tmp
, sizeof (tmp
));
148 close (tmp_desc
); /* No longer need this */
150 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
151 exits when the remote side dies. */
153 /* Convert IP address to string. */
154 fprintf (stderr
, "Remote debugging from host %s\n",
155 inet_ntoa (sockaddr
.sin_addr
));
158 #if defined(F_SETFL) && defined (FASYNC)
159 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
160 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
161 #if defined (F_SETOWN)
162 fcntl (remote_desc
, F_SETOWN
, getpid ());
174 /* Convert hex digit A to a number. */
179 if (a
>= '0' && a
<= '9')
181 else if (a
>= 'a' && a
<= 'f')
184 error ("Reply contains invalid hex digit");
188 /* Convert number NIB to a hex digit. */
196 return 'a' + nib
- 10;
199 /* Send a packet to the remote machine, with error checking.
200 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
206 unsigned char csum
= 0;
209 int cnt
= strlen (buf
);
212 buf2
= malloc (PBUFSIZ
);
214 /* Copy the packet into buffer BUF2, encapsulating it
215 and giving it a checksum. */
220 for (i
= 0; i
< cnt
; i
++)
226 *p
++ = tohex ((csum
>> 4) & 0xf);
227 *p
++ = tohex (csum
& 0xf);
231 /* Send it over and over until we get a positive ack. */
237 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
239 perror ("putpkt(write)");
244 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
245 cc
= read (remote_desc
, buf3
, 1);
247 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
251 fprintf (stderr
, "putpkt(read): Got EOF\n");
253 perror ("putpkt(read)");
259 while (buf3
[0] != '+');
262 return 1; /* Success! */
265 /* Come here when we get an input interrupt from the remote side. This
266 interrupt should only be active while we are waiting for the child to do
267 something. About the only thing that should come through is a ^C, which
268 will cause us to send a SIGINT to the child. */
271 input_interrupt (int unused
)
274 struct timeval immediate
= { 0, 0 };
276 /* Protect against spurious interrupts. This has been observed to
277 be a problem under NetBSD 1.4 and 1.5. */
280 FD_SET (remote_desc
, &readset
);
281 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
286 cc
= read (remote_desc
, &c
, 1);
288 if (cc
!= 1 || c
!= '\003')
290 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
294 kill (inferior_pid
, SIGINT
);
299 enable_async_io (void)
301 signal (SIGIO
, input_interrupt
);
305 disable_async_io (void)
307 signal (SIGIO
, SIG_IGN
);
310 /* Returns next char from remote GDB. -1 if error. */
315 static char buf
[BUFSIZ
];
316 static int bufcnt
= 0;
320 return *bufp
++ & 0x7f;
322 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
327 fprintf (stderr
, "readchar: Got EOF\n");
336 return *bufp
++ & 0x7f;
339 /* Read a packet from the remote machine, with error checking,
340 and store it in BUF. Returns length of packet, or negative if error. */
346 unsigned char csum
, c1
, c2
;
359 printf ("[getpkt: discarding char '%c']\n", c
);
377 c1
= fromhex (readchar ());
378 c2
= fromhex (readchar ());
380 if (csum
== (c1
<< 4) + c2
)
383 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
384 (c1
<< 4) + c2
, csum
, buf
);
385 write (remote_desc
, "-", 1);
389 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
391 write (remote_desc
, "+", 1);
394 printf ("[sent ack]\n");
407 write_enn (char *buf
)
416 convert_int_to_ascii (char *from
, char *to
, int n
)
423 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
433 convert_ascii_to_int (char *from
, char *to
, int n
)
438 nib1
= fromhex (*from
++);
439 nib2
= fromhex (*from
++);
440 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
445 outreg (int regno
, char *buf
)
447 int regsize
= register_size (regno
);
449 if ((regno
>> 12) != 0)
450 *buf
++ = tohex ((regno
>> 12) & 0xf);
451 if ((regno
>> 8) != 0)
452 *buf
++ = tohex ((regno
>> 8) & 0xf);
453 *buf
++ = tohex ((regno
>> 4) & 0xf);
454 *buf
++ = tohex (regno
& 0xf);
456 convert_int_to_ascii (register_data (regno
), buf
, regsize
);
464 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
470 /* FIXME! Should be converting this signal number (numbered
471 according to the signal numbering of the system we are running on)
472 to the signal numbers used by the gdb protocol (see enum target_signal
474 nib
= ((signo
& 0xf0) >> 4);
475 *buf
++ = tohex (nib
);
477 *buf
++ = tohex (nib
);
481 const char **regp
= gdbserver_expedite_regs
;
484 buf
= outreg (find_regno (*regp
), buf
);
488 /* If the debugger hasn't used any thread features, don't burden it with
489 threads. If we didn't check this, GDB 4.13 and older would choke. */
490 if (cont_thread
!= 0)
492 if (old_thread_from_wait
!= thread_from_wait
)
494 sprintf (buf
, "thread:%x;", thread_from_wait
);
496 old_thread_from_wait
= thread_from_wait
;
500 /* For W and X, we're done. */
505 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
509 *mem_addr_ptr
= *len_ptr
= 0;
511 while ((ch
= from
[i
++]) != ',')
513 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
514 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
517 for (j
= 0; j
< 4; j
++)
519 if ((ch
= from
[i
++]) == 0)
521 *len_ptr
= *len_ptr
<< 4;
522 *len_ptr
|= fromhex (ch
) & 0x0f;
527 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
532 *mem_addr_ptr
= *len_ptr
= 0;
534 while ((ch
= from
[i
++]) != ',')
536 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
537 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
540 while ((ch
= from
[i
++]) != ':')
542 *len_ptr
= *len_ptr
<< 4;
543 *len_ptr
|= fromhex (ch
) & 0x0f;
546 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);