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., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <netinet/tcp.h>
35 extern int remote_desc
;
36 extern int remote_debugging
;
45 void convert_ascii_to_int ();
46 void convert_int_to_ascii ();
47 void prepare_resume_reply ();
49 /* Open a connection to a remote debugger.
50 NAME is the filename used for communication. */
53 remote_open (name
, from_tty
)
61 if (!strchr (name
, ':'))
63 remote_desc
= open (name
, O_RDWR
);
65 perror_with_name ("Could not open remote device");
67 ioctl (remote_desc
, TIOCGETP
, &sg
);
69 ioctl (remote_desc
, TIOCSETP
, &sg
);
75 struct sockaddr_in sockaddr
;
78 port_str
= strchr (name
, ':');
80 port
= atoi (port_str
+ 1);
82 remote_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
84 perror_with_name ("Can't open socket");
86 /* Allow rapid reuse of this port. */
88 setsockopt (remote_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
,
91 /* Enable TCP keep alive process. */
93 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
95 sockaddr
.sin_family
= PF_INET
;
96 sockaddr
.sin_port
= htons(port
);
97 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
99 if (bind (remote_desc
, &sockaddr
, sizeof (sockaddr
))
100 || listen (remote_desc
, 1))
101 perror_with_name ("Can't bind address");
103 tmp
= sizeof (sockaddr
);
104 remote_desc
= accept (remote_desc
, &sockaddr
, &tmp
);
105 if (remote_desc
== -1)
106 perror_with_name ("Accept failed");
109 setsockopt (remote_desc
, 6, TCP_NODELAY
, (char *)&tmp
, sizeof(tmp
));
112 fprintf (stderr
, "Remote debugging using %s\n", name
);
113 remote_debugging
= 1;
116 /* Convert hex digit A to a number. */
122 if (a
>= '0' && a
<= '9')
124 else if (a
>= 'a' && a
<= 'f')
127 error ("Reply contains invalid hex digit");
130 /* Convert number NIB to a hex digit. */
139 return 'a' + nib
- 10;
142 /* Send the command in BUF to the remote machine,
143 and read the reply into BUF.
144 Report an error if we get an error reply. */
154 error ("Remote failure reply: E");
157 /* Send a packet to the remote machine, with error checking.
158 The data of the packet is in BUF. */
165 unsigned char csum
= 0;
168 int cnt
= strlen (buf
);
171 /* Copy the packet into buffer BUF2, encapsulating it
172 and giving it a checksum. */
177 for (i
= 0; i
< cnt
; i
++)
183 *p
++ = tohex ((csum
>> 4) & 0xf);
184 *p
++ = tohex (csum
& 0xf);
186 /* Send it over and over until we get a positive ack. */
190 write (remote_desc
, buf2
, p
- buf2
);
191 read (remote_desc
, buf3
, 1);
193 while (buf3
[0] != '+');
199 static char buf
[BUFSIZ
];
200 static int bufcnt
= 0;
204 return *bufp
++ & 0x7f;
206 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
211 fatal ("read error, quitting");
216 return *bufp
++ & 0x7f;
219 /* Read a packet from the remote machine, with error checking,
220 and store it in BUF. */
227 unsigned char csum
, c1
, c2
;
234 while ((c
= readchar ()) != '$');
247 c1
= fromhex (readchar ());
248 c2
= fromhex (readchar ());
249 if (csum
== (c1
<< 4) + c2
)
252 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
253 (c1
<< 4) + c2
, csum
, buf
);
254 write (remote_desc
, "-", 1);
257 write (remote_desc
, "+", 1);
280 convert_int_to_ascii (from
, to
, n
)
289 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
299 convert_ascii_to_int (from
, to
, n
)
306 nib1
= fromhex (*from
++);
307 nib2
= fromhex (*from
++);
308 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
317 extern char registers
[];
319 *buf
++ = tohex (regno
>> 4);
320 *buf
++ = tohex (regno
& 0xf);
322 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, 4);
330 prepare_resume_reply (buf
, status
, signal
)
332 unsigned char signal
;
339 nib
= ((signal
& 0xf0) >> 4);
340 *buf
++ = tohex (nib
);
342 *buf
++ = tohex (nib
);
344 buf
= outreg (PC_REGNUM
, buf
);
345 buf
= outreg (FP_REGNUM
, buf
);
346 buf
= outreg (SP_REGNUM
, buf
);
348 buf
= outreg (NPC_REGNUM
, buf
);
351 buf
= outreg (O7_REGNUM
, buf
);
358 decode_m_packet (from
, mem_addr_ptr
, len_ptr
)
360 unsigned int *mem_addr_ptr
, *len_ptr
;
364 *mem_addr_ptr
= *len_ptr
= 0;
366 while ((ch
= from
[i
++]) != ',')
368 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
369 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
372 for (j
= 0; j
< 4; j
++)
374 if ((ch
= from
[i
++]) == 0)
376 *len_ptr
= *len_ptr
<< 4;
377 *len_ptr
|= fromhex (ch
) & 0x0f;
382 decode_M_packet (from
, mem_addr_ptr
, len_ptr
, to
)
384 unsigned int *mem_addr_ptr
, *len_ptr
;
388 *mem_addr_ptr
= *len_ptr
= 0;
390 while ((ch
= from
[i
++]) != ',')
392 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
393 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
396 while ((ch
= from
[i
++]) != ':')
398 *len_ptr
= *len_ptr
<< 4;
399 *len_ptr
|= fromhex (ch
) & 0x0f;
402 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
This page took 0.03757 seconds and 4 git commands to generate.