1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3 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, see <http://www.gnu.org/licenses/>. */
27 #include <sys/ioctl.h>
33 #include <netinet/in.h>
36 #include <sys/socket.h>
41 #if HAVE_NETINET_TCP_H
42 #include <netinet/tcp.h>
45 #include <sys/ioctl.h>
58 #include <arpa/inet.h>
70 #include <sys/iomgr.h>
73 #ifndef HAVE_SOCKLEN_T
74 typedef int socklen_t
;
77 #ifndef IN_PROCESS_AGENT
80 # define INVALID_DESCRIPTOR INVALID_SOCKET
82 # define INVALID_DESCRIPTOR -1
85 /* Extra value for readchar_callback. */
87 /* The callback is currently not scheduled. */
91 /* Status of the readchar callback.
92 Either NOT_SCHEDULED or the callback id. */
93 static int readchar_callback
= NOT_SCHEDULED
;
95 static int readchar (void);
96 static void reset_readchar (void);
97 static void reschedule (void);
99 /* A cache entry for a successfully looked-up symbol. */
104 struct sym_cache
*next
;
107 int remote_debug
= 0;
108 struct ui_file
*gdb_stdlog
;
110 static int remote_desc
= INVALID_DESCRIPTOR
;
111 static int listen_desc
= INVALID_DESCRIPTOR
;
113 /* FIXME headerize? */
114 extern int using_threads
;
115 extern int debug_threads
;
117 /* If true, then GDB has requested noack mode. */
119 /* If true, then we tell GDB to use noack mode by default. */
120 int transport_is_reliable
= 0;
123 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
124 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
130 return remote_desc
!= INVALID_DESCRIPTOR
;
134 enable_async_notification (int fd
)
136 #if defined(F_SETFL) && defined (FASYNC)
137 int save_fcntl_flags
;
139 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
140 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
141 #if defined (F_SETOWN)
142 fcntl (fd
, F_SETOWN
, getpid ());
148 handle_accept_event (int err
, gdb_client_data client_data
)
150 struct sockaddr_in sockaddr
;
154 fprintf (stderr
, "handling possible accept event\n");
156 tmp
= sizeof (sockaddr
);
157 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
158 if (remote_desc
== -1)
159 perror_with_name ("Accept failed");
161 /* Enable TCP keep alive process. */
163 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
164 (char *) &tmp
, sizeof (tmp
));
166 /* Tell TCP not to delay small packets. This greatly speeds up
167 interactive response. */
169 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
170 (char *) &tmp
, sizeof (tmp
));
173 close (listen_desc
); /* No longer need this */
175 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
176 exits when the remote side dies. */
178 closesocket (listen_desc
); /* No longer need this */
181 delete_file_handler (listen_desc
);
183 /* Convert IP address to string. */
184 fprintf (stderr
, "Remote debugging from host %s\n",
185 inet_ntoa (sockaddr
.sin_addr
));
187 enable_async_notification (remote_desc
);
189 /* Register the event loop handler. */
190 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
192 /* We have a new GDB connection now. If we were disconnected
193 tracing, there's a window where the target could report a stop
194 event to the event loop, and since we have a connection now, we'd
195 try to send vStopped notifications to GDB. But, don't do that
196 until GDB as selected all-stop/non-stop, and has queried the
197 threads' status ('?'). */
203 /* Open a connection to a remote debugger.
204 NAME is the filename used for communication. */
207 remote_open (char *name
)
211 port_str
= strchr (name
, ':');
212 if (port_str
== NULL
)
215 error ("Only <host>:<port> is supported on this platform.");
219 if (stat (name
, &statbuf
) == 0
220 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
221 remote_desc
= open (name
, O_RDWR
);
229 perror_with_name ("Could not open remote device");
233 struct termios termios
;
234 tcgetattr (remote_desc
, &termios
);
239 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
240 termios
.c_cflag
|= CLOCAL
| CS8
;
241 termios
.c_cc
[VMIN
] = 1;
242 termios
.c_cc
[VTIME
] = 0;
244 tcsetattr (remote_desc
, TCSANOW
, &termios
);
250 struct termio termio
;
251 ioctl (remote_desc
, TCGETA
, &termio
);
256 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
257 termio
.c_cflag
|= CLOCAL
| CS8
;
258 termio
.c_cc
[VMIN
] = 1;
259 termio
.c_cc
[VTIME
] = 0;
261 ioctl (remote_desc
, TCSETA
, &termio
);
269 ioctl (remote_desc
, TIOCGETP
, &sg
);
271 ioctl (remote_desc
, TIOCSETP
, &sg
);
275 fprintf (stderr
, "Remote debugging using %s\n", name
);
277 transport_is_reliable
= 0;
279 enable_async_notification (remote_desc
);
281 /* Register the event loop handler. */
282 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
283 #endif /* USE_WIN32API */
288 static int winsock_initialized
;
291 struct sockaddr_in sockaddr
;
295 port
= strtoul (port_str
+ 1, &port_end
, 10);
296 if (port_str
[1] == '\0' || *port_end
!= '\0')
297 fatal ("Bad port argument: %s", name
);
300 if (!winsock_initialized
)
304 WSAStartup (MAKEWORD (1, 0), &wsad
);
305 winsock_initialized
= 1;
309 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
310 if (listen_desc
== -1)
311 perror_with_name ("Can't open socket");
313 /* Allow rapid reuse of this port. */
315 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
318 sockaddr
.sin_family
= PF_INET
;
319 sockaddr
.sin_port
= htons (port
);
320 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
322 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
323 || listen (listen_desc
, 1))
324 perror_with_name ("Can't bind address");
326 /* If port is zero, a random port will be selected, and the
327 fprintf below needs to know what port was selected. */
330 socklen_t len
= sizeof (sockaddr
);
331 if (getsockname (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
332 || len
< sizeof (sockaddr
))
333 perror_with_name ("Can't determine port");
334 port
= ntohs (sockaddr
.sin_port
);
337 fprintf (stderr
, "Listening on port %d\n", port
);
340 /* Register the event loop handler. */
341 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
343 transport_is_reliable
= 1;
350 delete_file_handler (remote_desc
);
353 closesocket (remote_desc
);
357 remote_desc
= INVALID_DESCRIPTOR
;
362 /* Convert hex digit A to a number. */
367 if (a
>= '0' && a
<= '9')
369 else if (a
>= 'a' && a
<= 'f')
372 error ("Reply contains invalid hex digit");
378 static const char hexchars
[] = "0123456789abcdef";
381 ishex (int ch
, int *val
)
383 if ((ch
>= 'a') && (ch
<= 'f'))
385 *val
= ch
- 'a' + 10;
388 if ((ch
>= 'A') && (ch
<= 'F'))
390 *val
= ch
- 'A' + 10;
393 if ((ch
>= '0') && (ch
<= '9'))
401 #ifndef IN_PROCESS_AGENT
404 unhexify (char *bin
, const char *hex
, int count
)
408 for (i
= 0; i
< count
; i
++)
410 if (hex
[0] == 0 || hex
[1] == 0)
412 /* Hex string is short, or of uneven length.
413 Return the count that has been converted so far. */
416 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
423 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
430 for (i
= 0; i
< len
; i
++)
434 addr
= addr
| (fromhex (ch
) & 0x0f);
440 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
445 while (*end
!= '\0' && *end
!= ';')
448 decode_address (addrp
, start
, end
- start
);
457 /* Convert number NIB to a hex digit. */
465 return 'a' + nib
- 10;
468 #ifndef IN_PROCESS_AGENT
471 hexify (char *hex
, const char *bin
, int count
)
475 /* May use a length, or a nul-terminated string as input. */
477 count
= strlen (bin
);
479 for (i
= 0; i
< count
; i
++)
481 *hex
++ = tohex ((*bin
>> 4) & 0xf);
482 *hex
++ = tohex (*bin
++ & 0xf);
488 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
489 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
490 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
491 (which may be more than *OUT_LEN due to escape characters). The
492 total number of bytes in the output buffer will be at most
496 remote_escape_output (const gdb_byte
*buffer
, int len
,
497 gdb_byte
*out_buf
, int *out_len
,
500 int input_index
, output_index
;
503 for (input_index
= 0; input_index
< len
; input_index
++)
505 gdb_byte b
= buffer
[input_index
];
507 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
509 /* These must be escaped. */
510 if (output_index
+ 2 > out_maxlen
)
512 out_buf
[output_index
++] = '}';
513 out_buf
[output_index
++] = b
^ 0x20;
517 if (output_index
+ 1 > out_maxlen
)
519 out_buf
[output_index
++] = b
;
523 *out_len
= input_index
;
527 /* Convert BUFFER, escaped data LEN bytes long, into binary data
528 in OUT_BUF. Return the number of bytes written to OUT_BUF.
529 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
531 This function reverses remote_escape_output. It allows more
532 escaped characters than that function does, in particular because
533 '*' must be escaped to avoid the run-length encoding processing
534 in reading packets. */
537 remote_unescape_input (const gdb_byte
*buffer
, int len
,
538 gdb_byte
*out_buf
, int out_maxlen
)
540 int input_index
, output_index
;
545 for (input_index
= 0; input_index
< len
; input_index
++)
547 gdb_byte b
= buffer
[input_index
];
549 if (output_index
+ 1 > out_maxlen
)
550 error ("Received too much data from the target.");
554 out_buf
[output_index
++] = b
^ 0x20;
560 out_buf
[output_index
++] = b
;
564 error ("Unmatched escape character in target response.");
569 /* Look for a sequence of characters which can be run-length encoded.
570 If there are any, update *CSUM and *P. Otherwise, output the
571 single character. Return the number of characters consumed. */
574 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
578 /* Always output the character. */
582 /* Don't go past '~'. */
586 for (n
= 1; n
< remaining
; n
++)
587 if (buf
[n
] != buf
[0])
590 /* N is the index of the first character not the same as buf[0].
591 buf[0] is counted twice, so by decrementing N, we get the number
592 of characters the RLE sequence will replace. */
598 /* Skip the frame characters. The manual says to skip '+' and '-'
599 also, but there's no reason to. Unfortunately these two unusable
600 characters double the encoded length of a four byte zero
602 while (n
+ 29 == '$' || n
+ 29 == '#')
616 unpack_varlen_hex (char *buff
, /* packet to parse */
622 while (ishex (*buff
, &nibble
))
625 retval
= retval
<< 4;
626 retval
|= nibble
& 0x0f;
632 #ifndef IN_PROCESS_AGENT
634 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
637 write_ptid (char *buf
, ptid_t ptid
)
643 pid
= ptid_get_pid (ptid
);
645 buf
+= sprintf (buf
, "p-%x.", -pid
);
647 buf
+= sprintf (buf
, "p%x.", pid
);
649 tid
= ptid_get_lwp (ptid
);
651 buf
+= sprintf (buf
, "-%x", -tid
);
653 buf
+= sprintf (buf
, "%x", tid
);
659 hex_or_minus_one (char *buf
, char **obuf
)
663 if (strncmp (buf
, "-1", 2) == 0)
669 buf
= unpack_varlen_hex (buf
, &ret
);
677 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
678 passed the last parsed char. Returns null_ptid on error. */
680 read_ptid (char *buf
, char **obuf
)
684 ULONGEST pid
= 0, tid
= 0;
688 /* Multi-process ptid. */
689 pp
= unpack_varlen_hex (p
+ 1, &pid
);
691 error ("invalid remote ptid: %s\n", p
);
695 tid
= hex_or_minus_one (p
, &pp
);
699 return ptid_build (pid
, tid
, 0);
702 /* No multi-process. Just a tid. */
703 tid
= hex_or_minus_one (p
, &pp
);
705 /* Since the stub is not sending a process id, then default to
706 what's in the current inferior. */
707 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
711 return ptid_build (pid
, tid
, 0);
714 /* Send a packet to the remote machine, with error checking.
715 The data of the packet is in BUF, and the length of the
716 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
719 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
722 unsigned char csum
= 0;
727 buf2
= xmalloc (PBUFSIZ
);
729 /* Copy the packet into buffer BUF2, encapsulating it
730 and giving it a checksum. */
738 for (i
= 0; i
< cnt
;)
739 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
742 *p
++ = tohex ((csum
>> 4) & 0xf);
743 *p
++ = tohex (csum
& 0xf);
747 /* Send it over and over until we get a positive ack. */
751 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
753 perror ("putpkt(write)");
758 if (noack_mode
|| is_notif
)
760 /* Don't expect an ack then. */
764 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
766 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
774 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
788 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
792 /* Check for an input interrupt while we're here. */
793 if (cc
== '\003' && current_inferior
!= NULL
)
794 (*the_target
->request_interrupt
) ();
799 return 1; /* Success! */
803 putpkt_binary (char *buf
, int cnt
)
805 return putpkt_binary_1 (buf
, cnt
, 0);
808 /* Send a packet to the remote machine, with error checking. The data
809 of the packet is in BUF, and the packet should be a NUL-terminated
810 string. Returns >= 0 on success, -1 otherwise. */
815 return putpkt_binary (buf
, strlen (buf
));
819 putpkt_notif (char *buf
)
821 return putpkt_binary_1 (buf
, strlen (buf
), 1);
824 /* Come here when we get an input interrupt from the remote side. This
825 interrupt should only be active while we are waiting for the child to do
826 something. Thus this assumes readchar:bufcnt is 0.
827 About the only thing that should come through is a ^C, which
828 will cause us to request child interruption. */
831 input_interrupt (int unused
)
834 struct timeval immediate
= { 0, 0 };
836 /* Protect against spurious interrupts. This has been observed to
837 be a problem under NetBSD 1.4 and 1.5. */
840 FD_SET (remote_desc
, &readset
);
841 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
846 cc
= read (remote_desc
, &c
, 1);
848 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
850 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
855 (*the_target
->request_interrupt
) ();
859 /* Check if the remote side sent us an interrupt request (^C). */
861 check_remote_input_interrupt_request (void)
863 /* This function may be called before establishing communications,
864 therefore we need to validate the remote descriptor. */
866 if (remote_desc
== INVALID_DESCRIPTOR
)
872 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
873 accept Control-C from the client, and must be disabled when talking to
877 unblock_async_io (void)
882 sigemptyset (&sigio_set
);
883 sigaddset (&sigio_set
, SIGIO
);
884 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
890 nto_comctrl (int enable
)
892 struct sigevent event
;
896 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
897 event
.sigev_signo
= SIGIO
;
898 event
.sigev_code
= 0;
899 event
.sigev_value
.sival_ptr
= NULL
;
900 event
.sigev_priority
= -1;
901 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
905 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
910 /* Current state of asynchronous I/O. */
911 static int async_io_enabled
;
913 /* Enable asynchronous I/O. */
915 enable_async_io (void)
917 if (async_io_enabled
)
921 signal (SIGIO
, input_interrupt
);
923 async_io_enabled
= 1;
929 /* Disable asynchronous I/O. */
931 disable_async_io (void)
933 if (!async_io_enabled
)
937 signal (SIGIO
, SIG_IGN
);
939 async_io_enabled
= 0;
947 initialize_async_io (void)
949 /* Make sure that async I/O starts disabled. */
950 async_io_enabled
= 1;
953 /* Make sure the signal is unblocked. */
957 /* Internal buffer used by readchar.
958 These are global to readchar because reschedule_remote needs to be
959 able to tell whether the buffer is empty. */
961 static unsigned char readchar_buf
[BUFSIZ
];
962 static int readchar_bufcnt
= 0;
963 static unsigned char *readchar_bufp
;
965 /* Returns next char from remote GDB. -1 if error. */
972 if (readchar_bufcnt
== 0)
974 readchar_bufcnt
= read (remote_desc
, readchar_buf
, sizeof (readchar_buf
));
976 if (readchar_bufcnt
<= 0)
978 if (readchar_bufcnt
== 0)
979 fprintf (stderr
, "readchar: Got EOF\n");
986 readchar_bufp
= readchar_buf
;
990 ch
= *readchar_bufp
++;
995 /* Reset the readchar state machine. */
998 reset_readchar (void)
1000 readchar_bufcnt
= 0;
1001 if (readchar_callback
!= NOT_SCHEDULED
)
1003 delete_callback_event (readchar_callback
);
1004 readchar_callback
= NOT_SCHEDULED
;
1008 /* Process remaining data in readchar_buf. */
1011 process_remaining (void *context
)
1015 /* This is a one-shot event. */
1016 readchar_callback
= NOT_SCHEDULED
;
1018 if (readchar_bufcnt
> 0)
1019 res
= handle_serial_event (0, NULL
);
1026 /* If there is still data in the buffer, queue another event to process it,
1027 we can't sleep in select yet. */
1032 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
1033 readchar_callback
= append_callback_event (process_remaining
, NULL
);
1036 /* Read a packet from the remote machine, with error checking,
1037 and store it in BUF. Returns length of packet, or negative if error. */
1043 unsigned char csum
, c1
, c2
;
1057 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1078 c1
= fromhex (readchar ());
1079 c2
= fromhex (readchar ());
1081 if (csum
== (c1
<< 4) + c2
)
1086 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
1087 (c1
<< 4) + c2
, csum
, buf
);
1088 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1092 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1093 (c1
<< 4) + c2
, csum
, buf
);
1094 write (remote_desc
, "-", 1);
1101 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1105 write (remote_desc
, "+", 1);
1109 fprintf (stderr
, "[sent ack]\n");
1117 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1126 write_ok (char *buf
)
1134 write_enn (char *buf
)
1136 /* Some day, we should define the meanings of the error codes... */
1146 convert_int_to_ascii (const unsigned char *from
, char *to
, int n
)
1153 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1154 *to
++ = tohex (nib
);
1156 *to
++ = tohex (nib
);
1161 #ifndef IN_PROCESS_AGENT
1164 convert_ascii_to_int (const char *from
, unsigned char *to
, int n
)
1169 nib1
= fromhex (*from
++);
1170 nib2
= fromhex (*from
++);
1171 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1176 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1178 if ((regno
>> 12) != 0)
1179 *buf
++ = tohex ((regno
>> 12) & 0xf);
1180 if ((regno
>> 8) != 0)
1181 *buf
++ = tohex ((regno
>> 8) & 0xf);
1182 *buf
++ = tohex ((regno
>> 4) & 0xf);
1183 *buf
++ = tohex (regno
& 0xf);
1185 collect_register_as_string (regcache
, regno
, buf
);
1186 buf
+= 2 * register_size (regno
);
1193 new_thread_notify (int id
)
1197 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1201 if (server_waiting
== 0)
1204 sprintf (own_buf
, "n%x", id
);
1205 disable_async_io ();
1211 dead_thread_notify (int id
)
1215 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1219 sprintf (own_buf
, "x%x", id
);
1220 disable_async_io ();
1226 prepare_resume_reply (char *buf
, ptid_t ptid
,
1227 struct target_waitstatus
*status
)
1230 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1231 target_pid_to_str (ptid
), status
->kind
);
1233 switch (status
->kind
)
1235 case TARGET_WAITKIND_STOPPED
:
1237 struct thread_info
*saved_inferior
;
1239 struct regcache
*regcache
;
1241 sprintf (buf
, "T%02x", status
->value
.sig
);
1242 buf
+= strlen (buf
);
1244 regp
= gdbserver_expedite_regs
;
1246 saved_inferior
= current_inferior
;
1248 current_inferior
= find_thread_ptid (ptid
);
1250 regcache
= get_thread_regcache (current_inferior
, 1);
1252 if (the_target
->stopped_by_watchpoint
!= NULL
1253 && (*the_target
->stopped_by_watchpoint
) ())
1258 strncpy (buf
, "watch:", 6);
1261 addr
= (*the_target
->stopped_data_address
) ();
1263 /* Convert each byte of the address into two hexadecimal
1264 chars. Note that we take sizeof (void *) instead of
1265 sizeof (addr); this is to avoid sending a 64-bit
1266 address to a 32-bit GDB. */
1267 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1268 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1274 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1279 /* Formerly, if the debugger had not used any thread features
1280 we would not burden it with a thread status response. This
1281 was for the benefit of GDB 4.13 and older. However, in
1282 recent GDB versions the check (``if (cont_thread != 0)'')
1283 does not have the desired effect because of sillyness in
1284 the way that the remote protocol handles specifying a
1285 thread. Since thread support relies on qSymbol support
1286 anyway, assume GDB can handle threads. */
1288 if (using_threads
&& !disable_packet_Tthread
)
1290 /* This if (1) ought to be unnecessary. But remote_wait
1291 in GDB will claim this event belongs to inferior_ptid
1292 if we do not specify a thread, and there's no way for
1293 gdbserver to know what inferior_ptid is. */
1294 if (1 || !ptid_equal (general_thread
, ptid
))
1297 /* In non-stop, don't change the general thread behind
1300 general_thread
= ptid
;
1301 sprintf (buf
, "thread:");
1302 buf
+= strlen (buf
);
1303 buf
= write_ptid (buf
, ptid
);
1305 buf
+= strlen (buf
);
1307 if (the_target
->core_of_thread
)
1308 core
= (*the_target
->core_of_thread
) (ptid
);
1311 sprintf (buf
, "core:");
1312 buf
+= strlen (buf
);
1313 sprintf (buf
, "%x", core
);
1315 buf
+= strlen (buf
);
1322 strcpy (buf
, "library:;");
1323 buf
+= strlen (buf
);
1327 current_inferior
= saved_inferior
;
1330 case TARGET_WAITKIND_EXITED
:
1332 sprintf (buf
, "W%x;process:%x",
1333 status
->value
.integer
, ptid_get_pid (ptid
));
1335 sprintf (buf
, "W%02x", status
->value
.integer
);
1337 case TARGET_WAITKIND_SIGNALLED
:
1339 sprintf (buf
, "X%x;process:%x",
1340 status
->value
.sig
, ptid_get_pid (ptid
));
1342 sprintf (buf
, "X%02x", status
->value
.sig
);
1345 error ("unhandled waitkind");
1351 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1355 *mem_addr_ptr
= *len_ptr
= 0;
1357 while ((ch
= from
[i
++]) != ',')
1359 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1360 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1363 for (j
= 0; j
< 4; j
++)
1365 if ((ch
= from
[i
++]) == 0)
1367 *len_ptr
= *len_ptr
<< 4;
1368 *len_ptr
|= fromhex (ch
) & 0x0f;
1373 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1374 unsigned char **to_p
)
1378 *mem_addr_ptr
= *len_ptr
= 0;
1380 while ((ch
= from
[i
++]) != ',')
1382 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1383 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1386 while ((ch
= from
[i
++]) != ':')
1388 *len_ptr
= *len_ptr
<< 4;
1389 *len_ptr
|= fromhex (ch
) & 0x0f;
1393 *to_p
= xmalloc (*len_ptr
);
1395 convert_ascii_to_int (&from
[i
++], *to_p
, *len_ptr
);
1399 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1400 unsigned int *len_ptr
, unsigned char **to_p
)
1404 *mem_addr_ptr
= *len_ptr
= 0;
1406 while ((ch
= from
[i
++]) != ',')
1408 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1409 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1412 while ((ch
= from
[i
++]) != ':')
1414 *len_ptr
= *len_ptr
<< 4;
1415 *len_ptr
|= fromhex (ch
) & 0x0f;
1419 *to_p
= xmalloc (*len_ptr
);
1421 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1422 *to_p
, *len_ptr
) != *len_ptr
)
1428 /* Decode a qXfer write request. */
1430 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1431 unsigned int *len
, unsigned char *data
)
1435 /* Extract and NUL-terminate the annex. */
1437 while (*buf
&& *buf
!= ':')
1443 /* Extract the offset. */
1445 while ((ch
= *buf
++) != ':')
1447 *offset
= *offset
<< 4;
1448 *offset
|= fromhex (ch
) & 0x0f;
1451 /* Get encoded data. */
1452 packet_len
-= buf
- *annex
;
1453 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1458 /* Decode the parameters of a qSearch:memory packet. */
1461 decode_search_memory_packet (const char *buf
, int packet_len
,
1462 CORE_ADDR
*start_addrp
,
1463 CORE_ADDR
*search_space_lenp
,
1464 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1466 const char *p
= buf
;
1468 p
= decode_address_to_semicolon (start_addrp
, p
);
1469 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1470 packet_len
-= p
- buf
;
1471 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1472 pattern
, packet_len
);
1477 free_sym_cache (struct sym_cache
*sym
)
1487 clear_symbol_cache (struct sym_cache
**symcache_p
)
1489 struct sym_cache
*sym
, *next
;
1491 /* Check the cache first. */
1492 for (sym
= *symcache_p
; sym
; sym
= next
)
1495 free_sym_cache (sym
);
1501 /* Get the address of NAME, and return it in ADDRP if found. if
1502 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1503 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1506 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1508 char own_buf
[266], *p
, *q
;
1510 struct sym_cache
*sym
;
1511 struct process_info
*proc
;
1513 proc
= current_process ();
1515 /* Check the cache first. */
1516 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1517 if (strcmp (name
, sym
->name
) == 0)
1523 /* It might not be an appropriate time to look up a symbol,
1524 e.g. while we're trying to fetch registers. */
1528 /* Send the request. */
1529 strcpy (own_buf
, "qSymbol:");
1530 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1531 if (putpkt (own_buf
) < 0)
1534 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1535 len
= getpkt (own_buf
);
1539 /* We ought to handle pretty much any packet at this point while we
1540 wait for the qSymbol "response". That requires re-entering the
1541 main loop. For now, this is an adequate approximation; allow
1542 GDB to read from memory while it figures out the address of the
1544 while (own_buf
[0] == 'm')
1547 unsigned char *mem_buf
;
1548 unsigned int mem_len
;
1550 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1551 mem_buf
= xmalloc (mem_len
);
1552 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1553 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1555 write_enn (own_buf
);
1557 if (putpkt (own_buf
) < 0)
1559 len
= getpkt (own_buf
);
1564 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1566 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1570 p
= own_buf
+ strlen ("qSymbol:");
1572 while (*q
&& *q
!= ':')
1575 /* Make sure we found a value for the symbol. */
1576 if (p
== q
|| *q
== '\0')
1579 decode_address (addrp
, p
, q
- p
);
1581 /* Save the symbol in our cache. */
1582 sym
= xmalloc (sizeof (*sym
));
1583 sym
->name
= xstrdup (name
);
1585 sym
->next
= proc
->symbol_cache
;
1586 proc
->symbol_cache
= sym
;
1591 /* Relocate an instruction to execute at a different address. OLDLOC
1592 is the address in the inferior memory where the instruction to
1593 relocate is currently at. On input, TO points to the destination
1594 where we want the instruction to be copied (and possibly adjusted)
1595 to. On output, it points to one past the end of the resulting
1596 instruction(s). The effect of executing the instruction at TO
1597 shall be the same as if executing it at FROM. For example, call
1598 instructions that implicitly push the return address on the stack
1599 should be adjusted to return to the instruction after OLDLOC;
1600 relative branches, and other PC-relative instructions need the
1601 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1604 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1608 ULONGEST written
= 0;
1610 /* Send the request. */
1611 strcpy (own_buf
, "qRelocInsn:");
1612 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1614 if (putpkt (own_buf
) < 0)
1617 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1618 len
= getpkt (own_buf
);
1622 /* We ought to handle pretty much any packet at this point while we
1623 wait for the qRelocInsn "response". That requires re-entering
1624 the main loop. For now, this is an adequate approximation; allow
1625 GDB to access memory. */
1626 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1629 unsigned char *mem_buf
= NULL
;
1630 unsigned int mem_len
;
1632 if (own_buf
[0] == 'm')
1634 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1635 mem_buf
= xmalloc (mem_len
);
1636 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1637 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1639 write_enn (own_buf
);
1641 else if (own_buf
[0] == 'X')
1643 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1644 &mem_len
, &mem_buf
) < 0
1645 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1646 write_enn (own_buf
);
1652 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1653 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1656 write_enn (own_buf
);
1659 if (putpkt (own_buf
) < 0)
1661 len
= getpkt (own_buf
);
1666 if (own_buf
[0] == 'E')
1668 warning ("An error occurred while relocating an instruction: %s\n",
1673 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1675 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1680 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1687 monitor_output (const char *msg
)
1689 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1692 hexify (buf
+ 1, msg
, 0);
1698 /* Return a malloc allocated string with special characters from TEXT
1699 replaced by entity references. */
1702 xml_escape_text (const char *text
)
1707 /* Compute the length of the result. */
1708 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1726 /* Expand the result. */
1727 result
= xmalloc (i
+ special
+ 1);
1728 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1732 strcpy (result
+ i
+ special
, "'");
1736 strcpy (result
+ i
+ special
, """);
1740 strcpy (result
+ i
+ special
, "&");
1744 strcpy (result
+ i
+ special
, "<");
1748 strcpy (result
+ i
+ special
, ">");
1752 result
[i
+ special
] = text
[i
];
1755 result
[i
+ special
] = '\0';
1761 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1764 size_t new_buffer_size
;
1769 new_buffer_size
= buffer
->buffer_size
;
1771 if (new_buffer_size
== 0)
1772 new_buffer_size
= 1;
1774 while (buffer
->used_size
+ size
> new_buffer_size
)
1775 new_buffer_size
*= 2;
1776 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1779 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1780 buffer
->buffer
= new_buffer
;
1781 buffer
->buffer_size
= new_buffer_size
;
1782 buffer
->used_size
+= size
;
1786 buffer_free (struct buffer
*buffer
)
1791 free (buffer
->buffer
);
1792 buffer
->buffer
= NULL
;
1793 buffer
->buffer_size
= 0;
1794 buffer
->used_size
= 0;
1798 buffer_init (struct buffer
*buffer
)
1800 memset (buffer
, 0, sizeof (*buffer
));
1804 buffer_finish (struct buffer
*buffer
)
1806 char *ret
= buffer
->buffer
;
1807 buffer
->buffer
= NULL
;
1808 buffer
->buffer_size
= 0;
1809 buffer
->used_size
= 0;
1814 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1821 va_start (ap
, format
);
1824 for (f
= format
; *f
; f
++)
1833 char *a
= va_arg (ap
, char *);
1834 buffer_grow (buffer
, prev
, f
- prev
- 1);
1835 p
= xml_escape_text (a
);
1836 buffer_grow_str (buffer
, p
);
1843 int i
= va_arg (ap
, int);
1844 char b
[sizeof ("4294967295")];
1846 buffer_grow (buffer
, prev
, f
- prev
- 1);
1847 sprintf (b
, "%d", i
);
1848 buffer_grow_str (buffer
, b
);
1858 buffer_grow_str (buffer
, prev
);