1 /* Remote utility routines for the remote server for GDB.
2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
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>
37 struct ui_file
*gdb_stdlog
;
39 static int remote_desc
;
41 /* Open a connection to a remote debugger.
42 NAME is the filename used for communication. */
45 remote_open (char *name
)
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. */
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. */
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. */
201 unsigned char csum
= 0;
204 int cnt
= strlen (buf
);
207 /* Copy the packet into buffer BUF2, encapsulating it
208 and giving it a checksum. */
213 for (i
= 0; i
< cnt
; i
++)
219 *p
++ = tohex ((csum
>> 4) & 0xf);
220 *p
++ = tohex (csum
& 0xf);
224 /* Send it over and over until we get a positive ack. */
230 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
232 perror ("putpkt(write)");
237 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
238 cc
= read (remote_desc
, buf3
, 1);
240 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
244 fprintf (stderr
, "putpkt(read): Got EOF\n");
246 perror ("putpkt(read)");
251 while (buf3
[0] != '+');
253 return 1; /* Success! */
256 /* Come here when we get an input interrupt from the remote side. This
257 interrupt should only be active while we are waiting for the child to do
258 something. About the only thing that should come through is a ^C, which
259 will cause us to send a SIGINT to the child. */
262 input_interrupt (void)
267 cc
= read (remote_desc
, &c
, 1);
269 if (cc
!= 1 || c
!= '\003')
271 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
275 kill (inferior_pid
, SIGINT
);
279 enable_async_io (void)
281 signal (SIGIO
, input_interrupt
);
285 disable_async_io (void)
287 signal (SIGIO
, SIG_IGN
);
290 /* Returns next char from remote GDB. -1 if error. */
295 static char buf
[BUFSIZ
];
296 static int bufcnt
= 0;
300 return *bufp
++ & 0x7f;
302 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
307 fprintf (stderr
, "readchar: Got EOF\n");
316 return *bufp
++ & 0x7f;
319 /* Read a packet from the remote machine, with error checking,
320 and store it in BUF. Returns length of packet, or negative if error. */
326 unsigned char csum
, c1
, c2
;
339 printf ("[getpkt: discarding char '%c']\n", c
);
357 c1
= fromhex (readchar ());
358 c2
= fromhex (readchar ());
360 if (csum
== (c1
<< 4) + c2
)
363 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
364 (c1
<< 4) + c2
, csum
, buf
);
365 write (remote_desc
, "-", 1);
369 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
371 write (remote_desc
, "+", 1);
374 printf ("[sent ack]\n");
387 write_enn (char *buf
)
396 convert_int_to_ascii (char *from
, char *to
, int n
)
403 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
413 convert_ascii_to_int (char *from
, char *to
, int n
)
418 nib1
= fromhex (*from
++);
419 nib2
= fromhex (*from
++);
420 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
425 outreg (int regno
, char *buf
)
427 int regsize
= REGISTER_RAW_SIZE (regno
);
429 if ((regno
>> 12) != 0)
430 *buf
++ = tohex ((regno
>> 12) & 0xf);
431 if ((regno
>> 8) != 0)
432 *buf
++ = tohex ((regno
>> 8) & 0xf);
433 *buf
++ = tohex ((regno
>> 4) & 0xf);
434 *buf
++ = tohex (regno
& 0xf);
436 convert_int_to_ascii (®isters
[REGISTER_BYTE (regno
)], buf
, regsize
);
444 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
450 /* FIXME! Should be converting this signal number (numbered
451 according to the signal numbering of the system we are running on)
452 to the signal numbers used by the gdb protocol (see enum target_signal
454 nib
= ((signo
& 0xf0) >> 4);
455 *buf
++ = tohex (nib
);
457 *buf
++ = tohex (nib
);
461 #ifdef GDBSERVER_RESUME_REGS
462 static int gdbserver_resume_regs
[] = GDBSERVER_RESUME_REGS
;
465 i
< sizeof (gdbserver_resume_regs
)
466 / sizeof (gdbserver_resume_regs
[0]);
469 int regnum
= gdbserver_resume_regs
[i
];
470 buf
= outreg (regnum
, buf
);
472 #else /* !defined(GDBSERVER_RESUME_REGS) */
473 buf
= outreg (PC_REGNUM
, buf
);
474 buf
= outreg (FP_REGNUM
, buf
);
475 buf
= outreg (SP_REGNUM
, buf
);
477 buf
= outreg (NPC_REGNUM
, buf
);
479 buf
= outreg (O7_REGNUM
, buf
);
481 #endif /* GDBSERVER_RESUME_REGS */
483 /* If the debugger hasn't used any thread features, don't burden it with
484 threads. If we didn't check this, GDB 4.13 and older would choke. */
485 if (cont_thread
!= 0)
487 if (old_thread_from_wait
!= thread_from_wait
)
489 sprintf (buf
, "thread:%x;", thread_from_wait
);
491 old_thread_from_wait
= thread_from_wait
;
495 /* For W and X, we're done. */
500 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
504 *mem_addr_ptr
= *len_ptr
= 0;
506 while ((ch
= from
[i
++]) != ',')
508 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
509 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
512 for (j
= 0; j
< 4; j
++)
514 if ((ch
= from
[i
++]) == 0)
516 *len_ptr
= *len_ptr
<< 4;
517 *len_ptr
|= fromhex (ch
) & 0x0f;
522 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
527 *mem_addr_ptr
= *len_ptr
= 0;
529 while ((ch
= from
[i
++]) != ',')
531 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
532 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
535 while ((ch
= from
[i
++]) != ':')
537 *len_ptr
= *len_ptr
<< 4;
538 *len_ptr
|= fromhex (ch
) & 0x0f;
541 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
This page took 0.040616 seconds and 4 git commands to generate.