1 /* Remote utility routines for the remote server for GDB.
2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
31 #include <netinet/tcp.h>
32 #include <sys/ioctl.h>
39 struct ui_file
*gdb_stdlog
;
41 static int remote_desc
;
43 /* Open a connection to a remote debugger.
44 NAME is the filename used for communication. */
47 remote_open (char *name
)
51 if (!strchr (name
, ':'))
53 remote_desc
= open (name
, O_RDWR
);
55 perror_with_name ("Could not open remote device");
59 struct termios termios
;
60 tcgetattr (remote_desc
, &termios
);
65 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
66 termios
.c_cflag
|= CLOCAL
| CS8
;
67 termios
.c_cc
[VMIN
] = 1;
68 termios
.c_cc
[VTIME
] = 0;
70 tcsetattr (remote_desc
, TCSANOW
, &termios
);
77 ioctl (remote_desc
, TCGETA
, &termio
);
82 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
83 termio
.c_cflag
|= CLOCAL
| CS8
;
84 termio
.c_cc
[VMIN
] = 1;
85 termio
.c_cc
[VTIME
] = 0;
87 ioctl (remote_desc
, TCSETA
, &termio
);
95 ioctl (remote_desc
, TIOCGETP
, &sg
);
97 ioctl (remote_desc
, TIOCSETP
, &sg
);
107 struct sockaddr_in sockaddr
;
109 struct protoent
*protoent
;
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 protoent
= getprotobyname ("tcp");
140 perror_with_name ("getprotobyname");
142 /* Enable TCP keep alive process. */
144 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
146 /* Tell TCP not to delay small packets. This greatly speeds up
147 interactive response. */
149 setsockopt (remote_desc
, protoent
->p_proto
, TCP_NODELAY
,
150 (char *) &tmp
, sizeof (tmp
));
152 close (tmp_desc
); /* No longer need this */
154 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
155 exits when the remote side dies. */
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 ());
166 fprintf (stderr
, "Remote debugging using %s\n", name
);
175 /* Convert hex digit A to a number. */
180 if (a
>= '0' && a
<= '9')
182 else if (a
>= 'a' && a
<= 'f')
185 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 /* Copy the packet into buffer BUF2, encapsulating it
213 and giving it a checksum. */
218 for (i
= 0; i
< cnt
; i
++)
224 *p
++ = tohex ((csum
>> 4) & 0xf);
225 *p
++ = tohex (csum
& 0xf);
229 /* Send it over and over until we get a positive ack. */
235 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
237 perror ("putpkt(write)");
242 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
243 cc
= read (remote_desc
, buf3
, 1);
245 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
249 fprintf (stderr
, "putpkt(read): Got EOF\n");
251 perror ("putpkt(read)");
256 while (buf3
[0] != '+');
258 return 1; /* Success! */
261 /* Come here when we get an input interrupt from the remote side. This
262 interrupt should only be active while we are waiting for the child to do
263 something. About the only thing that should come through is a ^C, which
264 will cause us to send a SIGINT to the child. */
267 input_interrupt (void)
270 struct timeval immediate
= { 0, 0 };
272 /* Protect against spurious interrupts. This has been observed to
273 be a problem under NetBSD 1.4 and 1.5. */
276 FD_SET (remote_desc
, &readset
);
277 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
282 cc
= read (remote_desc
, &c
, 1);
284 if (cc
!= 1 || c
!= '\003')
286 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
290 kill (inferior_pid
, SIGINT
);
295 enable_async_io (void)
297 signal (SIGIO
, input_interrupt
);
301 disable_async_io (void)
303 signal (SIGIO
, SIG_IGN
);
306 /* Returns next char from remote GDB. -1 if error. */
311 static char buf
[BUFSIZ
];
312 static int bufcnt
= 0;
316 return *bufp
++ & 0x7f;
318 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
323 fprintf (stderr
, "readchar: Got EOF\n");
332 return *bufp
++ & 0x7f;
335 /* Read a packet from the remote machine, with error checking,
336 and store it in BUF. Returns length of packet, or negative if error. */
342 unsigned char csum
, c1
, c2
;
355 printf ("[getpkt: discarding char '%c']\n", c
);
373 c1
= fromhex (readchar ());
374 c2
= fromhex (readchar ());
376 if (csum
== (c1
<< 4) + c2
)
379 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
380 (c1
<< 4) + c2
, csum
, buf
);
381 write (remote_desc
, "-", 1);
385 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
387 write (remote_desc
, "+", 1);
390 printf ("[sent ack]\n");
403 write_enn (char *buf
)
412 convert_int_to_ascii (char *from
, char *to
, int n
)
419 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
429 convert_ascii_to_int (char *from
, char *to
, int n
)
434 nib1
= fromhex (*from
++);
435 nib2
= fromhex (*from
++);
436 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
441 outreg (int regno
, char *buf
)
443 int regsize
= REGISTER_RAW_SIZE (regno
);
445 if ((regno
>> 12) != 0)
446 *buf
++ = tohex ((regno
>> 12) & 0xf);
447 if ((regno
>> 8) != 0)
448 *buf
++ = tohex ((regno
>> 8) & 0xf);
449 *buf
++ = tohex ((regno
>> 4) & 0xf);
450 *buf
++ = tohex (regno
& 0xf);
452 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, regsize
);
460 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
466 /* FIXME! Should be converting this signal number (numbered
467 according to the signal numbering of the system we are running on)
468 to the signal numbers used by the gdb protocol (see enum target_signal
470 nib
= ((signo
& 0xf0) >> 4);
471 *buf
++ = tohex (nib
);
473 *buf
++ = tohex (nib
);
477 #ifdef GDBSERVER_RESUME_REGS
478 static int gdbserver_resume_regs
[] = GDBSERVER_RESUME_REGS
;
481 i
< sizeof (gdbserver_resume_regs
)
482 / sizeof (gdbserver_resume_regs
[0]);
485 int regnum
= gdbserver_resume_regs
[i
];
486 buf
= outreg (regnum
, buf
);
488 #else /* !defined(GDBSERVER_RESUME_REGS) */
489 buf
= outreg (PC_REGNUM
, buf
);
490 buf
= outreg (FP_REGNUM
, buf
);
491 buf
= outreg (SP_REGNUM
, buf
);
493 buf
= outreg (NPC_REGNUM
, buf
);
495 buf
= outreg (O7_REGNUM
, buf
);
497 #endif /* GDBSERVER_RESUME_REGS */
499 /* If the debugger hasn't used any thread features, don't burden it with
500 threads. If we didn't check this, GDB 4.13 and older would choke. */
501 if (cont_thread
!= 0)
503 if (old_thread_from_wait
!= thread_from_wait
)
505 sprintf (buf
, "thread:%x;", thread_from_wait
);
507 old_thread_from_wait
= thread_from_wait
;
511 /* For W and X, we're done. */
516 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
520 *mem_addr_ptr
= *len_ptr
= 0;
522 while ((ch
= from
[i
++]) != ',')
524 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
525 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
528 for (j
= 0; j
< 4; j
++)
530 if ((ch
= from
[i
++]) == 0)
532 *len_ptr
= *len_ptr
<< 4;
533 *len_ptr
|= fromhex (ch
) & 0x0f;
538 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
543 *mem_addr_ptr
= *len_ptr
= 0;
545 while ((ch
= from
[i
++]) != ',')
547 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
548 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
551 while ((ch
= from
[i
++]) != ':')
553 *len_ptr
= *len_ptr
<< 4;
554 *len_ptr
|= fromhex (ch
) & 0x0f;
557 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
This page took 0.040791 seconds and 4 git commands to generate.