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,
19 Boston, MA 02111-1307, USA. */
25 #include <sys/ioctl.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
30 #include <netinet/tcp.h>
31 #include <sys/ioctl.h>
37 static int remote_desc
;
39 /* Open a connection to a remote debugger.
40 NAME is the filename used for communication. */
48 if (!strchr (name
, ':'))
50 remote_desc
= open (name
, O_RDWR
);
52 perror_with_name ("Could not open remote device");
56 struct termios termios
;
57 tcgetattr (remote_desc
, &termios
);
62 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
63 termios
.c_cflag
|= CLOCAL
| CS8
;
64 termios
.c_cc
[VMIN
] = 0;
65 termios
.c_cc
[VTIME
] = 0;
67 tcsetattr (remote_desc
, TCSANOW
, &termios
);
74 ioctl (remote_desc
, TCGETA
, &termio
);
79 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
80 termio
.c_cflag
|= CLOCAL
| CS8
;
81 termio
.c_cc
[VMIN
] = 0;
82 termio
.c_cc
[VTIME
] = 0;
84 ioctl (remote_desc
, TCSETA
, &termio
);
92 ioctl (remote_desc
, TIOCGETP
, &sg
);
94 ioctl (remote_desc
, TIOCSETP
, &sg
);
104 struct sockaddr_in sockaddr
;
106 struct protoent
*protoent
;
109 port_str
= strchr (name
, ':');
111 port
= atoi (port_str
+ 1);
113 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
115 perror_with_name ("Can't open socket");
117 /* Allow rapid reuse of this port. */
119 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
122 sockaddr
.sin_family
= PF_INET
;
123 sockaddr
.sin_port
= htons (port
);
124 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
126 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
127 || listen (tmp_desc
, 1))
128 perror_with_name ("Can't bind address");
130 tmp
= sizeof (sockaddr
);
131 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
132 if (remote_desc
== -1)
133 perror_with_name ("Accept failed");
135 protoent
= getprotobyname ("tcp");
137 perror_with_name ("getprotobyname");
139 /* Enable TCP keep alive process. */
141 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
143 /* Tell TCP not to delay small packets. This greatly speeds up
144 interactive response. */
146 setsockopt (remote_desc
, protoent
->p_proto
, TCP_NODELAY
,
147 (char *) &tmp
, sizeof (tmp
));
149 close (tmp_desc
); /* No longer need this */
151 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
152 exits when the remote side dies. */
155 #if defined(F_SETFL) && defined (FASYNC)
156 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
157 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
160 fprintf (stderr
, "Remote debugging using %s\n", name
);
169 /* Convert hex digit A to a number. */
175 if (a
>= '0' && a
<= '9')
177 else if (a
>= 'a' && a
<= 'f')
180 error ("Reply contains invalid hex digit");
183 /* Convert number NIB to a hex digit. */
192 return 'a' + nib
- 10;
195 /* Send a packet to the remote machine, with error checking.
196 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
203 unsigned char csum
= 0;
206 int cnt
= strlen (buf
);
209 /* Copy the packet into buffer BUF2, encapsulating it
210 and giving it a checksum. */
215 for (i
= 0; i
< cnt
; i
++)
221 *p
++ = tohex ((csum
>> 4) & 0xf);
222 *p
++ = tohex (csum
& 0xf);
226 /* Send it over and over until we get a positive ack. */
232 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
234 perror ("putpkt(write)");
239 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
240 cc
= read (remote_desc
, buf3
, 1);
242 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
246 fprintf (stderr
, "putpkt(read): Got EOF\n");
248 perror ("putpkt(read)");
253 while (buf3
[0] != '+');
255 return 1; /* Success! */
258 /* Come here when we get an input interrupt from the remote side. This
259 interrupt should only be active while we are waiting for the child to do
260 something. About the only thing that should come through is a ^C, which
261 will cause us to send a SIGINT to the child. */
269 cc
= read (remote_desc
, &c
, 1);
271 if (cc
!= 1 || c
!= '\003')
273 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
277 kill (inferior_pid
, SIGINT
);
283 signal (SIGIO
, input_interrupt
);
289 signal (SIGIO
, SIG_IGN
);
292 /* Returns next char from remote GDB. -1 if error. */
297 static char buf
[BUFSIZ
];
298 static int bufcnt
= 0;
302 return *bufp
++ & 0x7f;
304 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
309 fprintf (stderr
, "readchar: Got EOF\n");
318 return *bufp
++ & 0x7f;
321 /* Read a packet from the remote machine, with error checking,
322 and store it in BUF. Returns length of packet, or negative if error. */
329 unsigned char csum
, c1
, c2
;
342 printf ("[getpkt: discarding char '%c']\n", c
);
360 c1
= fromhex (readchar ());
361 c2
= fromhex (readchar ());
363 if (csum
== (c1
<< 4) + c2
)
366 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
367 (c1
<< 4) + c2
, csum
, buf
);
368 write (remote_desc
, "-", 1);
372 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
374 write (remote_desc
, "+", 1);
377 printf ("[sent ack]\n");
401 convert_int_to_ascii (from
, to
, n
)
410 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
420 convert_ascii_to_int (from
, to
, n
)
427 nib1
= fromhex (*from
++);
428 nib2
= fromhex (*from
++);
429 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
438 int regsize
= REGISTER_RAW_SIZE (regno
);
440 if ((regno
>> 12) != 0)
441 *buf
++ = tohex ((regno
>> 12) & 0xf);
442 if ((regno
>> 8) != 0)
443 *buf
++ = tohex ((regno
>> 8) & 0xf);
444 *buf
++ = tohex ((regno
>> 4) & 0xf);
445 *buf
++ = tohex (regno
& 0xf);
447 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, regsize
);
455 prepare_resume_reply (buf
, status
, signo
)
464 /* FIXME! Should be converting this signal number (numbered
465 according to the signal numbering of the system we are running on)
466 to the signal numbers used by the gdb protocol (see enum target_signal
468 nib
= ((signo
& 0xf0) >> 4);
469 *buf
++ = tohex (nib
);
471 *buf
++ = tohex (nib
);
475 #ifdef GDBSERVER_RESUME_REGS
476 static int gdbserver_resume_regs
[] = GDBSERVER_RESUME_REGS
;
479 i
< sizeof (gdbserver_resume_regs
)
480 / sizeof (gdbserver_resume_regs
[0]);
483 int regnum
= gdbserver_resume_regs
[i
];
484 buf
= outreg (regnum
, buf
);
486 #else /* !defined(GDBSERVER_RESUME_REGS) */
487 buf
= outreg (PC_REGNUM
, buf
);
488 buf
= outreg (FP_REGNUM
, buf
);
489 buf
= outreg (SP_REGNUM
, buf
);
491 buf
= outreg (NPC_REGNUM
, buf
);
494 buf
= outreg (O7_REGNUM
, buf
);
496 #endif /* GDBSERVER_RESUME_REGS */
498 /* If the debugger hasn't used any thread features, don't burden it with
499 threads. If we didn't check this, GDB 4.13 and older would choke. */
500 if (cont_thread
!= 0)
502 if (old_thread_from_wait
!= thread_from_wait
)
504 sprintf (buf
, "thread:%x;", thread_from_wait
);
506 old_thread_from_wait
= thread_from_wait
;
510 /* For W and X, we're done. */
515 decode_m_packet (from
, mem_addr_ptr
, len_ptr
)
517 CORE_ADDR
*mem_addr_ptr
;
518 unsigned int *len_ptr
;
522 *mem_addr_ptr
= *len_ptr
= 0;
524 while ((ch
= from
[i
++]) != ',')
526 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
527 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
530 for (j
= 0; j
< 4; j
++)
532 if ((ch
= from
[i
++]) == 0)
534 *len_ptr
= *len_ptr
<< 4;
535 *len_ptr
|= fromhex (ch
) & 0x0f;
540 decode_M_packet (from
, mem_addr_ptr
, len_ptr
, to
)
542 CORE_ADDR
*mem_addr_ptr
;
543 unsigned int *len_ptr
;
547 *mem_addr_ptr
= *len_ptr
= 0;
549 while ((ch
= from
[i
++]) != ',')
551 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
552 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
555 while ((ch
= from
[i
++]) != ':')
557 *len_ptr
= *len_ptr
<< 4;
558 *len_ptr
|= fromhex (ch
) & 0x0f;
561 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);