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>
36 struct ui_file
*gdb_stdlog
;
38 static int remote_desc
;
40 /* Open a connection to a remote debugger.
41 NAME is the filename used for communication. */
49 if (!strchr (name
, ':'))
51 remote_desc
= open (name
, O_RDWR
);
53 perror_with_name ("Could not open remote device");
57 struct termios termios
;
58 tcgetattr (remote_desc
, &termios
);
63 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
64 termios
.c_cflag
|= CLOCAL
| CS8
;
65 termios
.c_cc
[VMIN
] = 0;
66 termios
.c_cc
[VTIME
] = 0;
68 tcsetattr (remote_desc
, TCSANOW
, &termios
);
75 ioctl (remote_desc
, TCGETA
, &termio
);
80 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
81 termio
.c_cflag
|= CLOCAL
| CS8
;
82 termio
.c_cc
[VMIN
] = 0;
83 termio
.c_cc
[VTIME
] = 0;
85 ioctl (remote_desc
, TCSETA
, &termio
);
93 ioctl (remote_desc
, TIOCGETP
, &sg
);
95 ioctl (remote_desc
, TIOCSETP
, &sg
);
105 struct sockaddr_in sockaddr
;
107 struct protoent
*protoent
;
110 port_str
= strchr (name
, ':');
112 port
= atoi (port_str
+ 1);
114 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
116 perror_with_name ("Can't open socket");
118 /* Allow rapid reuse of this port. */
120 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
123 sockaddr
.sin_family
= PF_INET
;
124 sockaddr
.sin_port
= htons (port
);
125 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
127 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
128 || listen (tmp_desc
, 1))
129 perror_with_name ("Can't bind address");
131 tmp
= sizeof (sockaddr
);
132 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
133 if (remote_desc
== -1)
134 perror_with_name ("Accept failed");
136 protoent
= getprotobyname ("tcp");
138 perror_with_name ("getprotobyname");
140 /* Enable TCP keep alive process. */
142 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
144 /* Tell TCP not to delay small packets. This greatly speeds up
145 interactive response. */
147 setsockopt (remote_desc
, protoent
->p_proto
, TCP_NODELAY
,
148 (char *) &tmp
, sizeof (tmp
));
150 close (tmp_desc
); /* No longer need this */
152 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
153 exits when the remote side dies. */
156 #if defined(F_SETFL) && defined (FASYNC)
157 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
158 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
161 fprintf (stderr
, "Remote debugging using %s\n", name
);
170 /* Convert hex digit A to a number. */
176 if (a
>= '0' && a
<= '9')
178 else if (a
>= 'a' && a
<= 'f')
181 error ("Reply contains invalid hex digit");
184 /* Convert number NIB to a hex digit. */
193 return 'a' + nib
- 10;
196 /* Send a packet to the remote machine, with error checking.
197 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
204 unsigned char csum
= 0;
207 int cnt
= strlen (buf
);
210 /* Copy the packet into buffer BUF2, encapsulating it
211 and giving it a checksum. */
216 for (i
= 0; i
< cnt
; i
++)
222 *p
++ = tohex ((csum
>> 4) & 0xf);
223 *p
++ = tohex (csum
& 0xf);
227 /* Send it over and over until we get a positive ack. */
233 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
235 perror ("putpkt(write)");
240 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
241 cc
= read (remote_desc
, buf3
, 1);
243 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
247 fprintf (stderr
, "putpkt(read): Got EOF\n");
249 perror ("putpkt(read)");
254 while (buf3
[0] != '+');
256 return 1; /* Success! */
259 /* Come here when we get an input interrupt from the remote side. This
260 interrupt should only be active while we are waiting for the child to do
261 something. About the only thing that should come through is a ^C, which
262 will cause us to send a SIGINT to the child. */
270 cc
= read (remote_desc
, &c
, 1);
272 if (cc
!= 1 || c
!= '\003')
274 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
278 kill (inferior_pid
, SIGINT
);
284 signal (SIGIO
, input_interrupt
);
290 signal (SIGIO
, SIG_IGN
);
293 /* Returns next char from remote GDB. -1 if error. */
298 static char buf
[BUFSIZ
];
299 static int bufcnt
= 0;
303 return *bufp
++ & 0x7f;
305 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
310 fprintf (stderr
, "readchar: Got EOF\n");
319 return *bufp
++ & 0x7f;
322 /* Read a packet from the remote machine, with error checking,
323 and store it in BUF. Returns length of packet, or negative if error. */
330 unsigned char csum
, c1
, c2
;
343 printf ("[getpkt: discarding char '%c']\n", c
);
361 c1
= fromhex (readchar ());
362 c2
= fromhex (readchar ());
364 if (csum
== (c1
<< 4) + c2
)
367 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
368 (c1
<< 4) + c2
, csum
, buf
);
369 write (remote_desc
, "-", 1);
373 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
375 write (remote_desc
, "+", 1);
378 printf ("[sent ack]\n");
402 convert_int_to_ascii (from
, to
, n
)
411 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
421 convert_ascii_to_int (from
, to
, n
)
428 nib1
= fromhex (*from
++);
429 nib2
= fromhex (*from
++);
430 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
439 int regsize
= REGISTER_RAW_SIZE (regno
);
441 if ((regno
>> 12) != 0)
442 *buf
++ = tohex ((regno
>> 12) & 0xf);
443 if ((regno
>> 8) != 0)
444 *buf
++ = tohex ((regno
>> 8) & 0xf);
445 *buf
++ = tohex ((regno
>> 4) & 0xf);
446 *buf
++ = tohex (regno
& 0xf);
448 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, regsize
);
456 prepare_resume_reply (buf
, status
, signo
)
465 /* FIXME! Should be converting this signal number (numbered
466 according to the signal numbering of the system we are running on)
467 to the signal numbers used by the gdb protocol (see enum target_signal
469 nib
= ((signo
& 0xf0) >> 4);
470 *buf
++ = tohex (nib
);
472 *buf
++ = tohex (nib
);
476 #ifdef GDBSERVER_RESUME_REGS
477 static int gdbserver_resume_regs
[] = GDBSERVER_RESUME_REGS
;
480 i
< sizeof (gdbserver_resume_regs
)
481 / sizeof (gdbserver_resume_regs
[0]);
484 int regnum
= gdbserver_resume_regs
[i
];
485 buf
= outreg (regnum
, buf
);
487 #else /* !defined(GDBSERVER_RESUME_REGS) */
488 buf
= outreg (PC_REGNUM
, buf
);
489 buf
= outreg (FP_REGNUM
, buf
);
490 buf
= outreg (SP_REGNUM
, buf
);
492 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
);
This page took 0.050078 seconds and 5 git commands to generate.