1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
29 #include <netinet/tcp.h>
30 #include <sys/ioctl.h>
36 static int remote_desc
;
38 /* Open a connection to a remote debugger.
39 NAME is the filename used for communication. */
47 if (!strchr (name
, ':'))
49 remote_desc
= open (name
, O_RDWR
);
51 perror_with_name ("Could not open remote device");
55 struct termios termios
;
56 tcgetattr(remote_desc
, &termios
);
61 termios
.c_cflag
&= ~(CSIZE
|PARENB
);
62 termios
.c_cflag
|= CLOCAL
| CS8
;
63 termios
.c_cc
[VMIN
] = 0;
64 termios
.c_cc
[VTIME
] = 0;
66 tcsetattr(remote_desc
, TCSANOW
, &termios
);
73 ioctl (remote_desc
, TCGETA
, &termio
);
78 termio
.c_cflag
&= ~(CSIZE
|PARENB
);
79 termio
.c_cflag
|= CLOCAL
| CS8
;
80 termio
.c_cc
[VMIN
] = 0;
81 termio
.c_cc
[VTIME
] = 0;
83 ioctl (remote_desc
, TCSETA
, &termio
);
91 ioctl (remote_desc
, TIOCGETP
, &sg
);
93 ioctl (remote_desc
, TIOCSETP
, &sg
);
103 struct sockaddr_in sockaddr
;
105 struct protoent
*protoent
;
108 port_str
= strchr (name
, ':');
110 port
= atoi (port_str
+ 1);
112 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
114 perror_with_name ("Can't open socket");
116 /* Allow rapid reuse of this port. */
118 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
,
121 sockaddr
.sin_family
= PF_INET
;
122 sockaddr
.sin_port
= htons(port
);
123 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
125 if (bind (tmp_desc
, (struct sockaddr
*)&sockaddr
, sizeof (sockaddr
))
126 || listen (tmp_desc
, 1))
127 perror_with_name ("Can't bind address");
129 tmp
= sizeof (sockaddr
);
130 remote_desc
= accept (tmp_desc
, (struct sockaddr
*)&sockaddr
, &tmp
);
131 if (remote_desc
== -1)
132 perror_with_name ("Accept failed");
134 protoent
= getprotobyname ("tcp");
136 perror_with_name ("getprotobyname");
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
, protoent
->p_proto
, 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. */
154 #if defined(F_SETFL) && defined (FASYNC)
155 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
156 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
159 fprintf (stderr
, "Remote debugging using %s\n", name
);
168 /* Convert hex digit A to a number. */
174 if (a
>= '0' && a
<= '9')
176 else if (a
>= 'a' && a
<= 'f')
179 error ("Reply contains invalid hex digit");
182 /* Convert number NIB to a hex digit. */
191 return 'a' + nib
- 10;
194 /* Send a packet to the remote machine, with error checking.
195 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
202 unsigned char csum
= 0;
205 int cnt
= strlen (buf
);
208 /* Copy the packet into buffer BUF2, encapsulating it
209 and giving it a checksum. */
214 for (i
= 0; i
< cnt
; i
++)
220 *p
++ = tohex ((csum
>> 4) & 0xf);
221 *p
++ = tohex (csum
& 0xf);
225 /* Send it over and over until we get a positive ack. */
231 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
233 perror ("putpkt(write)");
238 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
239 cc
= read (remote_desc
, buf3
, 1);
241 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
245 fprintf (stderr
, "putpkt(read): Got EOF\n");
247 perror ("putpkt(read)");
252 while (buf3
[0] != '+');
254 return 1; /* Success! */
257 /* Come here when we get an input interrupt from the remote side. This
258 interrupt should only be active while we are waiting for the child to do
259 something. About the only thing that should come through is a ^C, which
260 will cause us to send a SIGINT to the child. */
268 cc
= read (remote_desc
, &c
, 1);
270 if (cc
!= 1 || c
!= '\003')
272 fprintf(stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
276 kill (inferior_pid
, SIGINT
);
282 signal (SIGIO
, input_interrupt
);
288 signal (SIGIO
, SIG_IGN
);
291 /* Returns next char from remote GDB. -1 if error. */
296 static char buf
[BUFSIZ
];
297 static int bufcnt
= 0;
301 return *bufp
++ & 0x7f;
303 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
308 fprintf (stderr
, "readchar: Got EOF\n");
317 return *bufp
++ & 0x7f;
320 /* Read a packet from the remote machine, with error checking,
321 and store it in BUF. Returns length of packet, or negative if error. */
328 unsigned char csum
, c1
, c2
;
341 printf ("[getpkt: discarding char '%c']\n", c
);
359 c1
= fromhex (readchar ());
360 c2
= fromhex (readchar ());
362 if (csum
== (c1
<< 4) + c2
)
365 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
366 (c1
<< 4) + c2
, csum
, buf
);
367 write (remote_desc
, "-", 1);
371 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
373 write (remote_desc
, "+", 1);
376 printf ("[sent ack]\n");
400 convert_int_to_ascii (from
, to
, n
)
409 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
419 convert_ascii_to_int (from
, to
, n
)
426 nib1
= fromhex (*from
++);
427 nib2
= fromhex (*from
++);
428 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
437 extern char registers
[];
438 int regsize
= REGISTER_RAW_SIZE (regno
);
440 *buf
++ = tohex (regno
>> 4);
441 *buf
++ = tohex (regno
& 0xf);
443 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, regsize
);
451 prepare_resume_reply (buf
, status
, signo
)
460 /* FIXME! Should be converting this signal number (numbered
461 according to the signal numbering of the system we are running on)
462 to the signal numbers used by the gdb protocol (see enum target_signal
464 nib
= ((signo
& 0xf0) >> 4);
465 *buf
++ = tohex (nib
);
467 *buf
++ = tohex (nib
);
471 buf
= outreg (PC_REGNUM
, buf
);
472 buf
= outreg (FP_REGNUM
, buf
);
473 buf
= outreg (SP_REGNUM
, buf
);
475 buf
= outreg (NPC_REGNUM
, buf
);
478 buf
= outreg (O7_REGNUM
, buf
);
481 /* If the debugger hasn't used any thread features, don't burden it with
482 threads. If we didn't check this, GDB 4.13 and older would choke. */
483 if (cont_thread
!= 0)
485 if (old_thread_from_wait
!= thread_from_wait
)
487 sprintf (buf
, "thread:%x;", thread_from_wait
);
489 old_thread_from_wait
= thread_from_wait
;
493 /* For W and X, we're done. */
498 decode_m_packet (from
, mem_addr_ptr
, len_ptr
)
500 CORE_ADDR
*mem_addr_ptr
;
501 unsigned int *len_ptr
;
505 *mem_addr_ptr
= *len_ptr
= 0;
507 while ((ch
= from
[i
++]) != ',')
509 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
510 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
513 for (j
= 0; j
< 4; j
++)
515 if ((ch
= from
[i
++]) == 0)
517 *len_ptr
= *len_ptr
<< 4;
518 *len_ptr
|= fromhex (ch
) & 0x0f;
523 decode_M_packet (from
, mem_addr_ptr
, len_ptr
, to
)
525 CORE_ADDR
*mem_addr_ptr
;
526 unsigned int *len_ptr
;
530 *mem_addr_ptr
= *len_ptr
= 0;
532 while ((ch
= from
[i
++]) != ',')
534 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
535 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
538 while ((ch
= from
[i
++]) != ':')
540 *len_ptr
= *len_ptr
<< 4;
541 *len_ptr
|= fromhex (ch
) & 0x0f;
544 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);