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 /* FIXME headerize? */
46 extern int using_threads
;
47 extern int debug_threads
;
49 extern int signal_pid
;
51 /* Open a connection to a remote debugger.
52 NAME is the filename used for communication. */
55 remote_open (char *name
)
59 if (!strchr (name
, ':'))
61 remote_desc
= open (name
, O_RDWR
);
63 perror_with_name ("Could not open remote device");
67 struct termios termios
;
68 tcgetattr (remote_desc
, &termios
);
73 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
74 termios
.c_cflag
|= CLOCAL
| CS8
;
75 termios
.c_cc
[VMIN
] = 1;
76 termios
.c_cc
[VTIME
] = 0;
78 tcsetattr (remote_desc
, TCSANOW
, &termios
);
85 ioctl (remote_desc
, TCGETA
, &termio
);
90 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
91 termio
.c_cflag
|= CLOCAL
| CS8
;
92 termio
.c_cc
[VMIN
] = 1;
93 termio
.c_cc
[VTIME
] = 0;
95 ioctl (remote_desc
, TCSETA
, &termio
);
103 ioctl (remote_desc
, TIOCGETP
, &sg
);
105 ioctl (remote_desc
, TIOCSETP
, &sg
);
109 fprintf (stderr
, "Remote debugging using %s\n", name
);
115 struct sockaddr_in sockaddr
;
119 port_str
= strchr (name
, ':');
121 port
= atoi (port_str
+ 1);
123 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
125 perror_with_name ("Can't open socket");
127 /* Allow rapid reuse of this port. */
129 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
132 sockaddr
.sin_family
= PF_INET
;
133 sockaddr
.sin_port
= htons (port
);
134 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
136 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
137 || listen (tmp_desc
, 1))
138 perror_with_name ("Can't bind address");
140 tmp
= sizeof (sockaddr
);
141 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
142 if (remote_desc
== -1)
143 perror_with_name ("Accept failed");
145 /* Enable TCP keep alive process. */
147 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
149 /* Tell TCP not to delay small packets. This greatly speeds up
150 interactive response. */
152 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
153 (char *) &tmp
, sizeof (tmp
));
155 close (tmp_desc
); /* No longer need this */
157 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
158 exits when the remote side dies. */
160 /* Convert IP address to string. */
161 fprintf (stderr
, "Remote debugging from host %s\n",
162 inet_ntoa (sockaddr
.sin_addr
));
165 #if defined(F_SETFL) && defined (FASYNC)
166 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
167 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
168 #if defined (F_SETOWN)
169 fcntl (remote_desc
, F_SETOWN
, getpid ());
181 /* Convert hex digit A to a number. */
186 if (a
>= '0' && a
<= '9')
188 else if (a
>= 'a' && a
<= 'f')
191 error ("Reply contains invalid hex digit");
196 unhexify (char *bin
, const char *hex
, int count
)
200 for (i
= 0; i
< count
; i
++)
202 if (hex
[0] == 0 || hex
[1] == 0)
204 /* Hex string is short, or of uneven length.
205 Return the count that has been converted so far. */
208 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
215 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
222 for (i
= 0; i
< len
; i
++)
226 addr
= addr
| (fromhex (ch
) & 0x0f);
231 /* Convert number NIB to a hex digit. */
239 return 'a' + nib
- 10;
243 hexify (char *hex
, const char *bin
, int count
)
247 /* May use a length, or a nul-terminated string as input. */
249 count
= strlen (bin
);
251 for (i
= 0; i
< count
; i
++)
253 *hex
++ = tohex ((*bin
>> 4) & 0xf);
254 *hex
++ = tohex (*bin
++ & 0xf);
260 /* Send a packet to the remote machine, with error checking.
261 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
267 unsigned char csum
= 0;
270 int cnt
= strlen (buf
);
273 buf2
= malloc (PBUFSIZ
);
275 /* Copy the packet into buffer BUF2, encapsulating it
276 and giving it a checksum. */
281 for (i
= 0; i
< cnt
; i
++)
287 *p
++ = tohex ((csum
>> 4) & 0xf);
288 *p
++ = tohex (csum
& 0xf);
292 /* Send it over and over until we get a positive ack. */
298 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
300 perror ("putpkt(write)");
306 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
309 cc
= read (remote_desc
, buf3
, 1);
312 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
319 fprintf (stderr
, "putpkt(read): Got EOF\n");
321 perror ("putpkt(read)");
327 /* Check for an input interrupt while we're here. */
328 if (buf3
[0] == '\003')
329 kill (signal_pid
, SIGINT
);
331 while (buf3
[0] != '+');
334 return 1; /* Success! */
337 /* Come here when we get an input interrupt from the remote side. This
338 interrupt should only be active while we are waiting for the child to do
339 something. About the only thing that should come through is a ^C, which
340 will cause us to send a SIGINT to the child. */
343 input_interrupt (int unused
)
346 struct timeval immediate
= { 0, 0 };
348 /* Protect against spurious interrupts. This has been observed to
349 be a problem under NetBSD 1.4 and 1.5. */
352 FD_SET (remote_desc
, &readset
);
353 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
358 cc
= read (remote_desc
, &c
, 1);
360 if (cc
!= 1 || c
!= '\003')
362 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
366 kill (signal_pid
, SIGINT
);
371 enable_async_io (void)
373 signal (SIGIO
, input_interrupt
);
377 disable_async_io (void)
379 signal (SIGIO
, SIG_IGN
);
382 /* Returns next char from remote GDB. -1 if error. */
387 static char buf
[BUFSIZ
];
388 static int bufcnt
= 0;
392 return *bufp
++ & 0x7f;
394 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
399 fprintf (stderr
, "readchar: Got EOF\n");
408 return *bufp
++ & 0x7f;
411 /* Read a packet from the remote machine, with error checking,
412 and store it in BUF. Returns length of packet, or negative if error. */
418 unsigned char csum
, c1
, c2
;
432 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
453 c1
= fromhex (readchar ());
454 c2
= fromhex (readchar ());
456 if (csum
== (c1
<< 4) + c2
)
459 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
460 (c1
<< 4) + c2
, csum
, buf
);
461 write (remote_desc
, "-", 1);
466 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
470 write (remote_desc
, "+", 1);
474 fprintf (stderr
, "[sent ack]\n");
490 write_enn (char *buf
)
499 convert_int_to_ascii (char *from
, char *to
, int n
)
506 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
516 convert_ascii_to_int (char *from
, char *to
, int n
)
521 nib1
= fromhex (*from
++);
522 nib2
= fromhex (*from
++);
523 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
528 outreg (int regno
, char *buf
)
530 if ((regno
>> 12) != 0)
531 *buf
++ = tohex ((regno
>> 12) & 0xf);
532 if ((regno
>> 8) != 0)
533 *buf
++ = tohex ((regno
>> 8) & 0xf);
534 *buf
++ = tohex ((regno
>> 4) & 0xf);
535 *buf
++ = tohex (regno
& 0xf);
537 collect_register_as_string (regno
, buf
);
538 buf
+= 2 * register_size (regno
);
545 new_thread_notify (int id
)
549 /* The `n' response is not yet part of the remote protocol. Do nothing. */
553 if (server_waiting
== 0)
556 sprintf (own_buf
, "n%x", id
);
563 dead_thread_notify (int id
)
567 /* The `x' response is not yet part of the remote protocol. Do nothing. */
571 sprintf (own_buf
, "x%x", id
);
578 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
584 sig
= (int)target_signal_from_host (signo
);
586 nib
= ((sig
& 0xf0) >> 4);
587 *buf
++ = tohex (nib
);
589 *buf
++ = tohex (nib
);
593 const char **regp
= gdbserver_expedite_regs
;
596 buf
= outreg (find_regno (*regp
), buf
);
600 /* Formerly, if the debugger had not used any thread features we would not
601 burden it with a thread status response. This was for the benefit of
602 GDB 4.13 and older. However, in recent GDB versions the check
603 (``if (cont_thread != 0)'') does not have the desired effect because of
604 sillyness in the way that the remote protocol handles specifying a thread.
605 Since thread support relies on qSymbol support anyway, assume GDB can handle
610 /* FIXME right place to set this? */
611 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
613 fprintf (stderr
, "Writing resume reply for %d\n\n", thread_from_wait
);
614 if (old_thread_from_wait
!= thread_from_wait
)
616 general_thread
= thread_from_wait
;
617 sprintf (buf
, "thread:%x;", thread_from_wait
);
619 old_thread_from_wait
= thread_from_wait
;
623 /* For W and X, we're done. */
628 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
632 *mem_addr_ptr
= *len_ptr
= 0;
634 while ((ch
= from
[i
++]) != ',')
636 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
637 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
640 for (j
= 0; j
< 4; j
++)
642 if ((ch
= from
[i
++]) == 0)
644 *len_ptr
= *len_ptr
<< 4;
645 *len_ptr
|= fromhex (ch
) & 0x0f;
650 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
655 *mem_addr_ptr
= *len_ptr
= 0;
657 while ((ch
= from
[i
++]) != ',')
659 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
660 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
663 while ((ch
= from
[i
++]) != ':')
665 *len_ptr
= *len_ptr
<< 4;
666 *len_ptr
|= fromhex (ch
) & 0x0f;
669 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
673 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
675 char own_buf
[266], *p
, *q
;
678 /* Send the request. */
679 strcpy (own_buf
, "qSymbol:");
680 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
681 if (putpkt (own_buf
) < 0)
684 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
685 len
= getpkt (own_buf
);
689 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
691 /* Malformed response. */
694 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
701 p
= own_buf
+ strlen ("qSymbol:");
703 while (*q
&& *q
!= ':')
706 /* Make sure we found a value for the symbol. */
707 if (p
== q
|| *q
== '\0')
710 decode_address (addrp
, p
, q
- p
);
This page took 0.043583 seconds and 4 git commands to generate.