]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/remote-utils.c
1 /* Remote utility routines for the remote server for GDB.
2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include <sys/ioctl.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
32 #include <netinet/tcp.h>
33 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
41 struct ui_file
*gdb_stdlog
;
43 static int remote_desc
;
45 /* Open a connection to a remote debugger.
46 NAME is the filename used for communication. */
49 remote_open (char *name
)
53 if (!strchr (name
, ':'))
55 remote_desc
= open (name
, O_RDWR
);
57 perror_with_name ("Could not open remote device");
61 struct termios termios
;
62 tcgetattr (remote_desc
, &termios
);
67 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
68 termios
.c_cflag
|= CLOCAL
| CS8
;
69 termios
.c_cc
[VMIN
] = 1;
70 termios
.c_cc
[VTIME
] = 0;
72 tcsetattr (remote_desc
, TCSANOW
, &termios
);
79 ioctl (remote_desc
, TCGETA
, &termio
);
84 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
85 termio
.c_cflag
|= CLOCAL
| CS8
;
86 termio
.c_cc
[VMIN
] = 1;
87 termio
.c_cc
[VTIME
] = 0;
89 ioctl (remote_desc
, TCSETA
, &termio
);
97 ioctl (remote_desc
, TIOCGETP
, &sg
);
99 ioctl (remote_desc
, TIOCSETP
, &sg
);
103 fprintf (stderr
, "Remote debugging using %s\n", name
);
109 struct sockaddr_in sockaddr
;
113 port_str
= strchr (name
, ':');
115 port
= atoi (port_str
+ 1);
117 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
119 perror_with_name ("Can't open socket");
121 /* Allow rapid reuse of this port. */
123 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
126 sockaddr
.sin_family
= PF_INET
;
127 sockaddr
.sin_port
= htons (port
);
128 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
130 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
131 || listen (tmp_desc
, 1))
132 perror_with_name ("Can't bind address");
134 tmp
= sizeof (sockaddr
);
135 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
136 if (remote_desc
== -1)
137 perror_with_name ("Accept failed");
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
, IPPROTO_TCP
, 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. */
154 /* Convert IP address to string. */
155 fprintf (stderr
, "Remote debugging from host %s\n",
156 inet_ntoa (sockaddr
.sin_addr
));
159 #if defined(F_SETFL) && defined (FASYNC)
160 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
161 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
162 #if defined (F_SETOWN)
163 fcntl (remote_desc
, F_SETOWN
, getpid ());
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");
190 unhexify (char *bin
, const char *hex
, int count
)
194 for (i
= 0; i
< count
; i
++)
196 if (hex
[0] == 0 || hex
[1] == 0)
198 /* Hex string is short, or of uneven length.
199 Return the count that has been converted so far. */
202 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
209 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
216 for (i
= 0; i
< len
; i
++)
220 addr
= addr
| (fromhex (ch
) & 0x0f);
225 /* Convert number NIB to a hex digit. */
233 return 'a' + nib
- 10;
237 hexify (char *hex
, const char *bin
, int count
)
241 /* May use a length, or a nul-terminated string as input. */
243 count
= strlen (bin
);
245 for (i
= 0; i
< count
; i
++)
247 *hex
++ = tohex ((*bin
>> 4) & 0xf);
248 *hex
++ = tohex (*bin
++ & 0xf);
254 /* Send a packet to the remote machine, with error checking.
255 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
261 unsigned char csum
= 0;
264 int cnt
= strlen (buf
);
267 buf2
= malloc (PBUFSIZ
);
269 /* Copy the packet into buffer BUF2, encapsulating it
270 and giving it a checksum. */
275 for (i
= 0; i
< cnt
; i
++)
281 *p
++ = tohex ((csum
>> 4) & 0xf);
282 *p
++ = tohex (csum
& 0xf);
286 /* Send it over and over until we get a positive ack. */
292 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
294 perror ("putpkt(write)");
299 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2
);
300 cc
= read (remote_desc
, buf3
, 1);
302 printf ("[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
306 fprintf (stderr
, "putpkt(read): Got EOF\n");
308 perror ("putpkt(read)");
314 while (buf3
[0] != '+');
317 return 1; /* Success! */
320 /* Come here when we get an input interrupt from the remote side. This
321 interrupt should only be active while we are waiting for the child to do
322 something. About the only thing that should come through is a ^C, which
323 will cause us to send a SIGINT to the child. */
326 input_interrupt (int unused
)
329 struct timeval immediate
= { 0, 0 };
331 /* Protect against spurious interrupts. This has been observed to
332 be a problem under NetBSD 1.4 and 1.5. */
335 FD_SET (remote_desc
, &readset
);
336 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
341 cc
= read (remote_desc
, &c
, 1);
343 if (cc
!= 1 || c
!= '\003')
345 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
349 kill (signal_pid
, SIGINT
);
354 enable_async_io (void)
356 signal (SIGIO
, input_interrupt
);
360 disable_async_io (void)
362 signal (SIGIO
, SIG_IGN
);
365 /* Returns next char from remote GDB. -1 if error. */
370 static char buf
[BUFSIZ
];
371 static int bufcnt
= 0;
375 return *bufp
++ & 0x7f;
377 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
382 fprintf (stderr
, "readchar: Got EOF\n");
391 return *bufp
++ & 0x7f;
394 /* Read a packet from the remote machine, with error checking,
395 and store it in BUF. Returns length of packet, or negative if error. */
401 unsigned char csum
, c1
, c2
;
414 printf ("[getpkt: discarding char '%c']\n", c
);
432 c1
= fromhex (readchar ());
433 c2
= fromhex (readchar ());
435 if (csum
== (c1
<< 4) + c2
)
438 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
439 (c1
<< 4) + c2
, csum
, buf
);
440 write (remote_desc
, "-", 1);
444 printf ("getpkt (\"%s\"); [sending ack] \n", buf
);
446 write (remote_desc
, "+", 1);
449 printf ("[sent ack]\n");
462 write_enn (char *buf
)
471 convert_int_to_ascii (char *from
, char *to
, int n
)
478 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
488 convert_ascii_to_int (char *from
, char *to
, int n
)
493 nib1
= fromhex (*from
++);
494 nib2
= fromhex (*from
++);
495 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
500 outreg (int regno
, char *buf
)
502 int regsize
= register_size (regno
);
504 if ((regno
>> 12) != 0)
505 *buf
++ = tohex ((regno
>> 12) & 0xf);
506 if ((regno
>> 8) != 0)
507 *buf
++ = tohex ((regno
>> 8) & 0xf);
508 *buf
++ = tohex ((regno
>> 4) & 0xf);
509 *buf
++ = tohex (regno
& 0xf);
511 convert_int_to_ascii (register_data (regno
), buf
, regsize
);
519 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
525 sig
= (int)target_signal_from_host (signo
);
527 nib
= ((sig
& 0xf0) >> 4);
528 *buf
++ = tohex (nib
);
530 *buf
++ = tohex (nib
);
534 const char **regp
= gdbserver_expedite_regs
;
537 buf
= outreg (find_regno (*regp
), buf
);
541 /* If the debugger hasn't used any thread features, don't burden it with
542 threads. If we didn't check this, GDB 4.13 and older would choke. */
543 if (cont_thread
!= 0)
545 if (old_thread_from_wait
!= thread_from_wait
)
547 sprintf (buf
, "thread:%x;", thread_from_wait
);
549 old_thread_from_wait
= thread_from_wait
;
553 /* For W and X, we're done. */
558 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
562 *mem_addr_ptr
= *len_ptr
= 0;
564 while ((ch
= from
[i
++]) != ',')
566 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
567 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
570 for (j
= 0; j
< 4; j
++)
572 if ((ch
= from
[i
++]) == 0)
574 *len_ptr
= *len_ptr
<< 4;
575 *len_ptr
|= fromhex (ch
) & 0x0f;
580 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
585 *mem_addr_ptr
= *len_ptr
= 0;
587 while ((ch
= from
[i
++]) != ',')
589 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
590 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
593 while ((ch
= from
[i
++]) != ':')
595 *len_ptr
= *len_ptr
<< 4;
596 *len_ptr
|= fromhex (ch
) & 0x0f;
599 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
603 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
605 char own_buf
[266], *p
, *q
;
608 /* Send the request. */
609 strcpy (own_buf
, "qSymbol:");
610 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
611 if (putpkt (own_buf
) < 0)
614 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
615 len
= getpkt (own_buf
);
619 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
621 /* Malformed response. */
623 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
627 p
= own_buf
+ strlen ("qSymbol:");
629 while (*q
&& *q
!= ':')
632 /* Make sure we found a value for the symbol. */
633 if (p
== q
|| *q
== '\0')
636 decode_address (addrp
, p
, q
- p
);