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 /* Open a connection to a remote debugger.
50 NAME is the filename used for communication. */
53 remote_open (char *name
)
57 if (!strchr (name
, ':'))
59 remote_desc
= open (name
, O_RDWR
);
61 perror_with_name ("Could not open remote device");
65 struct termios termios
;
66 tcgetattr (remote_desc
, &termios
);
71 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
72 termios
.c_cflag
|= CLOCAL
| CS8
;
73 termios
.c_cc
[VMIN
] = 1;
74 termios
.c_cc
[VTIME
] = 0;
76 tcsetattr (remote_desc
, TCSANOW
, &termios
);
83 ioctl (remote_desc
, TCGETA
, &termio
);
88 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
89 termio
.c_cflag
|= CLOCAL
| CS8
;
90 termio
.c_cc
[VMIN
] = 1;
91 termio
.c_cc
[VTIME
] = 0;
93 ioctl (remote_desc
, TCSETA
, &termio
);
101 ioctl (remote_desc
, TIOCGETP
, &sg
);
103 ioctl (remote_desc
, TIOCSETP
, &sg
);
107 fprintf (stderr
, "Remote debugging using %s\n", name
);
113 struct sockaddr_in sockaddr
;
117 port_str
= strchr (name
, ':');
119 port
= atoi (port_str
+ 1);
121 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
123 perror_with_name ("Can't open socket");
125 /* Allow rapid reuse of this port. */
127 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
130 sockaddr
.sin_family
= PF_INET
;
131 sockaddr
.sin_port
= htons (port
);
132 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
134 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
135 || listen (tmp_desc
, 1))
136 perror_with_name ("Can't bind address");
138 tmp
= sizeof (sockaddr
);
139 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
140 if (remote_desc
== -1)
141 perror_with_name ("Accept failed");
143 /* Enable TCP keep alive process. */
145 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
147 /* Tell TCP not to delay small packets. This greatly speeds up
148 interactive response. */
150 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
151 (char *) &tmp
, sizeof (tmp
));
153 close (tmp_desc
); /* No longer need this */
155 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
156 exits when the remote side dies. */
158 /* Convert IP address to string. */
159 fprintf (stderr
, "Remote debugging from host %s\n",
160 inet_ntoa (sockaddr
.sin_addr
));
163 #if defined(F_SETFL) && defined (FASYNC)
164 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
165 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
166 #if defined (F_SETOWN)
167 fcntl (remote_desc
, F_SETOWN
, getpid ());
179 /* Convert hex digit A to a number. */
184 if (a
>= '0' && a
<= '9')
186 else if (a
>= 'a' && a
<= 'f')
189 error ("Reply contains invalid hex digit");
194 unhexify (char *bin
, const char *hex
, int count
)
198 for (i
= 0; i
< count
; i
++)
200 if (hex
[0] == 0 || hex
[1] == 0)
202 /* Hex string is short, or of uneven length.
203 Return the count that has been converted so far. */
206 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
213 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
220 for (i
= 0; i
< len
; i
++)
224 addr
= addr
| (fromhex (ch
) & 0x0f);
229 /* Convert number NIB to a hex digit. */
237 return 'a' + nib
- 10;
241 hexify (char *hex
, const char *bin
, int count
)
245 /* May use a length, or a nul-terminated string as input. */
247 count
= strlen (bin
);
249 for (i
= 0; i
< count
; i
++)
251 *hex
++ = tohex ((*bin
>> 4) & 0xf);
252 *hex
++ = tohex (*bin
++ & 0xf);
258 /* Send a packet to the remote machine, with error checking.
259 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
265 unsigned char csum
= 0;
268 int cnt
= strlen (buf
);
271 buf2
= malloc (PBUFSIZ
);
273 /* Copy the packet into buffer BUF2, encapsulating it
274 and giving it a checksum. */
279 for (i
= 0; i
< cnt
; i
++)
285 *p
++ = tohex ((csum
>> 4) & 0xf);
286 *p
++ = tohex (csum
& 0xf);
290 /* Send it over and over until we get a positive ack. */
296 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
298 perror ("putpkt(write)");
304 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
307 cc
= read (remote_desc
, buf3
, 1);
310 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
317 fprintf (stderr
, "putpkt(read): Got EOF\n");
319 perror ("putpkt(read)");
325 /* Check for an input interrupt while we're here. */
326 if (buf3
[0] == '\003')
327 kill ((*the_target
->signal_pid
) (), SIGINT
);
329 while (buf3
[0] != '+');
332 return 1; /* Success! */
335 /* Come here when we get an input interrupt from the remote side. This
336 interrupt should only be active while we are waiting for the child to do
337 something. About the only thing that should come through is a ^C, which
338 will cause us to send a SIGINT to the child. */
341 input_interrupt (int unused
)
344 struct timeval immediate
= { 0, 0 };
346 /* Protect against spurious interrupts. This has been observed to
347 be a problem under NetBSD 1.4 and 1.5. */
350 FD_SET (remote_desc
, &readset
);
351 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
356 cc
= read (remote_desc
, &c
, 1);
358 if (cc
!= 1 || c
!= '\003')
360 fprintf (stderr
, "input_interrupt, cc = %d c = %d\n", cc
, c
);
364 kill ((*the_target
->signal_pid
) (), SIGINT
);
369 enable_async_io (void)
371 signal (SIGIO
, input_interrupt
);
375 disable_async_io (void)
377 signal (SIGIO
, SIG_IGN
);
380 /* Returns next char from remote GDB. -1 if error. */
385 static char buf
[BUFSIZ
];
386 static int bufcnt
= 0;
390 return *bufp
++ & 0x7f;
392 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
397 fprintf (stderr
, "readchar: Got EOF\n");
406 return *bufp
++ & 0x7f;
409 /* Read a packet from the remote machine, with error checking,
410 and store it in BUF. Returns length of packet, or negative if error. */
416 unsigned char csum
, c1
, c2
;
430 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
451 c1
= fromhex (readchar ());
452 c2
= fromhex (readchar ());
454 if (csum
== (c1
<< 4) + c2
)
457 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
458 (c1
<< 4) + c2
, csum
, buf
);
459 write (remote_desc
, "-", 1);
464 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
468 write (remote_desc
, "+", 1);
472 fprintf (stderr
, "[sent ack]\n");
488 write_enn (char *buf
)
497 convert_int_to_ascii (char *from
, char *to
, int n
)
504 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
514 convert_ascii_to_int (char *from
, char *to
, int n
)
519 nib1
= fromhex (*from
++);
520 nib2
= fromhex (*from
++);
521 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
526 outreg (int regno
, char *buf
)
528 if ((regno
>> 12) != 0)
529 *buf
++ = tohex ((regno
>> 12) & 0xf);
530 if ((regno
>> 8) != 0)
531 *buf
++ = tohex ((regno
>> 8) & 0xf);
532 *buf
++ = tohex ((regno
>> 4) & 0xf);
533 *buf
++ = tohex (regno
& 0xf);
535 collect_register_as_string (regno
, buf
);
536 buf
+= 2 * register_size (regno
);
543 new_thread_notify (int id
)
547 /* The `n' response is not yet part of the remote protocol. Do nothing. */
551 if (server_waiting
== 0)
554 sprintf (own_buf
, "n%x", id
);
561 dead_thread_notify (int id
)
565 /* The `x' response is not yet part of the remote protocol. Do nothing. */
569 sprintf (own_buf
, "x%x", id
);
576 prepare_resume_reply (char *buf
, char status
, unsigned char signo
)
582 sig
= (int)target_signal_from_host (signo
);
584 nib
= ((sig
& 0xf0) >> 4);
585 *buf
++ = tohex (nib
);
587 *buf
++ = tohex (nib
);
591 const char **regp
= gdbserver_expedite_regs
;
594 buf
= outreg (find_regno (*regp
), buf
);
598 /* Formerly, if the debugger had not used any thread features we would not
599 burden it with a thread status response. This was for the benefit of
600 GDB 4.13 and older. However, in recent GDB versions the check
601 (``if (cont_thread != 0)'') does not have the desired effect because of
602 sillyness in the way that the remote protocol handles specifying a thread.
603 Since thread support relies on qSymbol support anyway, assume GDB can handle
608 /* FIXME right place to set this? */
609 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
611 fprintf (stderr
, "Writing resume reply for %d\n\n", thread_from_wait
);
612 if (old_thread_from_wait
!= thread_from_wait
)
614 general_thread
= thread_from_wait
;
615 sprintf (buf
, "thread:%x;", thread_from_wait
);
617 old_thread_from_wait
= thread_from_wait
;
621 /* For W and X, we're done. */
626 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
630 *mem_addr_ptr
= *len_ptr
= 0;
632 while ((ch
= from
[i
++]) != ',')
634 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
635 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
638 for (j
= 0; j
< 4; j
++)
640 if ((ch
= from
[i
++]) == 0)
642 *len_ptr
= *len_ptr
<< 4;
643 *len_ptr
|= fromhex (ch
) & 0x0f;
648 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
653 *mem_addr_ptr
= *len_ptr
= 0;
655 while ((ch
= from
[i
++]) != ',')
657 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
658 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
661 while ((ch
= from
[i
++]) != ':')
663 *len_ptr
= *len_ptr
<< 4;
664 *len_ptr
|= fromhex (ch
) & 0x0f;
667 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
671 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
673 char own_buf
[266], *p
, *q
;
676 /* Send the request. */
677 strcpy (own_buf
, "qSymbol:");
678 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
679 if (putpkt (own_buf
) < 0)
682 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
683 len
= getpkt (own_buf
);
687 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
689 /* Malformed response. */
692 fprintf (stderr
, "Malformed response to qSymbol, ignoring.\n");
699 p
= own_buf
+ strlen ("qSymbol:");
701 while (*q
&& *q
!= ':')
704 /* Make sure we found a value for the symbol. */
705 if (p
== q
|| *q
== '\0')
708 decode_address (addrp
, p
, q
- p
);
This page took 0.071504 seconds and 5 git commands to generate.