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
;
78 # define INVALID_DESCRIPTOR INVALID_SOCKET
80 # define INVALID_DESCRIPTOR -1
83 static int readchar (void);
85 /* A cache entry for a successfully looked-up symbol. */
90 struct sym_cache
*next
;
94 struct ui_file
*gdb_stdlog
;
96 static int remote_desc
= INVALID_DESCRIPTOR
;
97 static int listen_desc
= INVALID_DESCRIPTOR
;
99 /* FIXME headerize? */
100 extern int using_threads
;
101 extern int debug_threads
;
103 /* If true, then GDB has requested noack mode. */
105 /* If true, then we tell GDB to use noack mode by default. */
106 int transport_is_reliable
= 0;
109 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
110 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
116 return remote_desc
!= INVALID_DESCRIPTOR
;
120 enable_async_notification (int fd
)
122 #if defined(F_SETFL) && defined (FASYNC)
123 int save_fcntl_flags
;
125 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
126 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
127 #if defined (F_SETOWN)
128 fcntl (fd
, F_SETOWN
, getpid ());
134 handle_accept_event (int err
, gdb_client_data client_data
)
136 struct sockaddr_in sockaddr
;
140 fprintf (stderr
, "handling possible accept event\n");
142 tmp
= sizeof (sockaddr
);
143 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
144 if (remote_desc
== -1)
145 perror_with_name ("Accept failed");
147 /* Enable TCP keep alive process. */
149 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
150 (char *) &tmp
, sizeof (tmp
));
152 /* Tell TCP not to delay small packets. This greatly speeds up
153 interactive response. */
155 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
156 (char *) &tmp
, sizeof (tmp
));
159 close (listen_desc
); /* No longer need this */
161 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
162 exits when the remote side dies. */
164 closesocket (listen_desc
); /* No longer need this */
167 delete_file_handler (listen_desc
);
169 /* Convert IP address to string. */
170 fprintf (stderr
, "Remote debugging from host %s\n",
171 inet_ntoa (sockaddr
.sin_addr
));
173 enable_async_notification (remote_desc
);
175 /* Register the event loop handler. */
176 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
178 /* We have a new GDB connection now. If we were disconnected
179 tracing, there's a window where the target could report a stop
180 event to the event loop, and since we have a connection now, we'd
181 try to send vStopped notifications to GDB. But, don't do that
182 until GDB as selected all-stop/non-stop, and has queried the
183 threads' status ('?'). */
189 /* Open a connection to a remote debugger.
190 NAME is the filename used for communication. */
193 remote_open (char *name
)
197 port_str
= strchr (name
, ':');
198 if (port_str
== NULL
)
201 error ("Only <host>:<port> is supported on this platform.");
205 if (stat (name
, &statbuf
) == 0
206 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
207 remote_desc
= open (name
, O_RDWR
);
215 perror_with_name ("Could not open remote device");
219 struct termios termios
;
220 tcgetattr (remote_desc
, &termios
);
225 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
226 termios
.c_cflag
|= CLOCAL
| CS8
;
227 termios
.c_cc
[VMIN
] = 1;
228 termios
.c_cc
[VTIME
] = 0;
230 tcsetattr (remote_desc
, TCSANOW
, &termios
);
236 struct termio termio
;
237 ioctl (remote_desc
, TCGETA
, &termio
);
242 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
243 termio
.c_cflag
|= CLOCAL
| CS8
;
244 termio
.c_cc
[VMIN
] = 1;
245 termio
.c_cc
[VTIME
] = 0;
247 ioctl (remote_desc
, TCSETA
, &termio
);
255 ioctl (remote_desc
, TIOCGETP
, &sg
);
257 ioctl (remote_desc
, TIOCSETP
, &sg
);
261 fprintf (stderr
, "Remote debugging using %s\n", name
);
263 transport_is_reliable
= 0;
265 enable_async_notification (remote_desc
);
267 /* Register the event loop handler. */
268 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
269 #endif /* USE_WIN32API */
274 static int winsock_initialized
;
277 struct sockaddr_in sockaddr
;
281 port
= strtoul (port_str
+ 1, &port_end
, 10);
282 if (port_str
[1] == '\0' || *port_end
!= '\0')
283 fatal ("Bad port argument: %s", name
);
286 if (!winsock_initialized
)
290 WSAStartup (MAKEWORD (1, 0), &wsad
);
291 winsock_initialized
= 1;
295 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
297 perror_with_name ("Can't open socket");
299 /* Allow rapid reuse of this port. */
301 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
304 sockaddr
.sin_family
= PF_INET
;
305 sockaddr
.sin_port
= htons (port
);
306 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
308 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
309 || listen (listen_desc
, 1))
310 perror_with_name ("Can't bind address");
312 /* If port is zero, a random port will be selected, and the
313 fprintf below needs to know what port was selected. */
316 socklen_t len
= sizeof (sockaddr
);
317 if (getsockname (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
318 || len
< sizeof (sockaddr
))
319 perror_with_name ("Can't determine port");
320 port
= ntohs (sockaddr
.sin_port
);
323 fprintf (stderr
, "Listening on port %d\n", port
);
326 /* Register the event loop handler. */
327 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
329 transport_is_reliable
= 1;
336 delete_file_handler (remote_desc
);
339 closesocket (remote_desc
);
343 remote_desc
= INVALID_DESCRIPTOR
;
346 /* Convert hex digit A to a number. */
351 if (a
>= '0' && a
<= '9')
353 else if (a
>= 'a' && a
<= 'f')
356 error ("Reply contains invalid hex digit");
360 static const char hexchars
[] = "0123456789abcdef";
363 ishex (int ch
, int *val
)
365 if ((ch
>= 'a') && (ch
<= 'f'))
367 *val
= ch
- 'a' + 10;
370 if ((ch
>= 'A') && (ch
<= 'F'))
372 *val
= ch
- 'A' + 10;
375 if ((ch
>= '0') && (ch
<= '9'))
384 unhexify (char *bin
, const char *hex
, int count
)
388 for (i
= 0; i
< count
; i
++)
390 if (hex
[0] == 0 || hex
[1] == 0)
392 /* Hex string is short, or of uneven length.
393 Return the count that has been converted so far. */
396 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
403 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
410 for (i
= 0; i
< len
; i
++)
414 addr
= addr
| (fromhex (ch
) & 0x0f);
420 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
425 while (*end
!= '\0' && *end
!= ';')
428 decode_address (addrp
, start
, end
- start
);
435 /* Convert number NIB to a hex digit. */
443 return 'a' + nib
- 10;
447 hexify (char *hex
, const char *bin
, int count
)
451 /* May use a length, or a nul-terminated string as input. */
453 count
= strlen (bin
);
455 for (i
= 0; i
< count
; i
++)
457 *hex
++ = tohex ((*bin
>> 4) & 0xf);
458 *hex
++ = tohex (*bin
++ & 0xf);
464 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
465 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
466 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
467 (which may be more than *OUT_LEN due to escape characters). The
468 total number of bytes in the output buffer will be at most
472 remote_escape_output (const gdb_byte
*buffer
, int len
,
473 gdb_byte
*out_buf
, int *out_len
,
476 int input_index
, output_index
;
479 for (input_index
= 0; input_index
< len
; input_index
++)
481 gdb_byte b
= buffer
[input_index
];
483 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
485 /* These must be escaped. */
486 if (output_index
+ 2 > out_maxlen
)
488 out_buf
[output_index
++] = '}';
489 out_buf
[output_index
++] = b
^ 0x20;
493 if (output_index
+ 1 > out_maxlen
)
495 out_buf
[output_index
++] = b
;
499 *out_len
= input_index
;
503 /* Convert BUFFER, escaped data LEN bytes long, into binary data
504 in OUT_BUF. Return the number of bytes written to OUT_BUF.
505 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
507 This function reverses remote_escape_output. It allows more
508 escaped characters than that function does, in particular because
509 '*' must be escaped to avoid the run-length encoding processing
510 in reading packets. */
513 remote_unescape_input (const gdb_byte
*buffer
, int len
,
514 gdb_byte
*out_buf
, int out_maxlen
)
516 int input_index
, output_index
;
521 for (input_index
= 0; input_index
< len
; input_index
++)
523 gdb_byte b
= buffer
[input_index
];
525 if (output_index
+ 1 > out_maxlen
)
526 error ("Received too much data from the target.");
530 out_buf
[output_index
++] = b
^ 0x20;
536 out_buf
[output_index
++] = b
;
540 error ("Unmatched escape character in target response.");
545 /* Look for a sequence of characters which can be run-length encoded.
546 If there are any, update *CSUM and *P. Otherwise, output the
547 single character. Return the number of characters consumed. */
550 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
554 /* Always output the character. */
558 /* Don't go past '~'. */
562 for (n
= 1; n
< remaining
; n
++)
563 if (buf
[n
] != buf
[0])
566 /* N is the index of the first character not the same as buf[0].
567 buf[0] is counted twice, so by decrementing N, we get the number
568 of characters the RLE sequence will replace. */
574 /* Skip the frame characters. The manual says to skip '+' and '-'
575 also, but there's no reason to. Unfortunately these two unusable
576 characters double the encoded length of a four byte zero
578 while (n
+ 29 == '$' || n
+ 29 == '#')
590 unpack_varlen_hex (char *buff
, /* packet to parse */
596 while (ishex (*buff
, &nibble
))
599 retval
= retval
<< 4;
600 retval
|= nibble
& 0x0f;
606 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
609 write_ptid (char *buf
, ptid_t ptid
)
615 pid
= ptid_get_pid (ptid
);
617 buf
+= sprintf (buf
, "p-%x.", -pid
);
619 buf
+= sprintf (buf
, "p%x.", pid
);
621 tid
= ptid_get_lwp (ptid
);
623 buf
+= sprintf (buf
, "-%x", -tid
);
625 buf
+= sprintf (buf
, "%x", tid
);
631 hex_or_minus_one (char *buf
, char **obuf
)
635 if (strncmp (buf
, "-1", 2) == 0)
641 buf
= unpack_varlen_hex (buf
, &ret
);
649 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
650 passed the last parsed char. Returns null_ptid on error. */
652 read_ptid (char *buf
, char **obuf
)
656 ULONGEST pid
= 0, tid
= 0;
660 /* Multi-process ptid. */
661 pp
= unpack_varlen_hex (p
+ 1, &pid
);
663 error ("invalid remote ptid: %s\n", p
);
667 tid
= hex_or_minus_one (p
, &pp
);
671 return ptid_build (pid
, tid
, 0);
674 /* No multi-process. Just a tid. */
675 tid
= hex_or_minus_one (p
, &pp
);
677 /* Since the stub is not sending a process id, then default to
678 what's in the current inferior. */
679 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
683 return ptid_build (pid
, tid
, 0);
686 /* Send a packet to the remote machine, with error checking.
687 The data of the packet is in BUF, and the length of the
688 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
691 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
694 unsigned char csum
= 0;
699 buf2
= xmalloc (PBUFSIZ
);
701 /* Copy the packet into buffer BUF2, encapsulating it
702 and giving it a checksum. */
710 for (i
= 0; i
< cnt
;)
711 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
714 *p
++ = tohex ((csum
>> 4) & 0xf);
715 *p
++ = tohex (csum
& 0xf);
719 /* Send it over and over until we get a positive ack. */
723 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
725 perror ("putpkt(write)");
730 if (noack_mode
|| is_notif
)
732 /* Don't expect an ack then. */
736 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
738 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
746 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
760 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
764 /* Check for an input interrupt while we're here. */
765 if (cc
== '\003' && current_inferior
!= NULL
)
766 (*the_target
->request_interrupt
) ();
771 return 1; /* Success! */
775 putpkt_binary (char *buf
, int cnt
)
777 return putpkt_binary_1 (buf
, cnt
, 0);
780 /* Send a packet to the remote machine, with error checking. The data
781 of the packet is in BUF, and the packet should be a NUL-terminated
782 string. Returns >= 0 on success, -1 otherwise. */
787 return putpkt_binary (buf
, strlen (buf
));
791 putpkt_notif (char *buf
)
793 return putpkt_binary_1 (buf
, strlen (buf
), 1);
796 /* Come here when we get an input interrupt from the remote side. This
797 interrupt should only be active while we are waiting for the child to do
798 something. Thus this assumes readchar:bufcnt is 0.
799 About the only thing that should come through is a ^C, which
800 will cause us to request child interruption. */
803 input_interrupt (int unused
)
806 struct timeval immediate
= { 0, 0 };
808 /* Protect against spurious interrupts. This has been observed to
809 be a problem under NetBSD 1.4 and 1.5. */
812 FD_SET (remote_desc
, &readset
);
813 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
818 cc
= read (remote_desc
, &c
, 1);
820 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
822 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
827 (*the_target
->request_interrupt
) ();
831 /* Check if the remote side sent us an interrupt request (^C). */
833 check_remote_input_interrupt_request (void)
835 /* This function may be called before establishing communications,
836 therefore we need to validate the remote descriptor. */
838 if (remote_desc
== INVALID_DESCRIPTOR
)
844 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
845 accept Control-C from the client, and must be disabled when talking to
849 unblock_async_io (void)
854 sigemptyset (&sigio_set
);
855 sigaddset (&sigio_set
, SIGIO
);
856 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
862 nto_comctrl (int enable
)
864 struct sigevent event
;
868 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
869 event
.sigev_signo
= SIGIO
;
870 event
.sigev_code
= 0;
871 event
.sigev_value
.sival_ptr
= NULL
;
872 event
.sigev_priority
= -1;
873 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
877 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
882 /* Current state of asynchronous I/O. */
883 static int async_io_enabled
;
885 /* Enable asynchronous I/O. */
887 enable_async_io (void)
889 if (async_io_enabled
)
893 signal (SIGIO
, input_interrupt
);
895 async_io_enabled
= 1;
901 /* Disable asynchronous I/O. */
903 disable_async_io (void)
905 if (!async_io_enabled
)
909 signal (SIGIO
, SIG_IGN
);
911 async_io_enabled
= 0;
919 initialize_async_io (void)
921 /* Make sure that async I/O starts disabled. */
922 async_io_enabled
= 1;
925 /* Make sure the signal is unblocked. */
929 /* Returns next char from remote GDB. -1 if error. */
934 static unsigned char buf
[BUFSIZ
];
935 static int bufcnt
= 0;
936 static unsigned char *bufp
;
941 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
946 fprintf (stderr
, "readchar: Got EOF\n");
958 /* Read a packet from the remote machine, with error checking,
959 and store it in BUF. Returns length of packet, or negative if error. */
965 unsigned char csum
, c1
, c2
;
979 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1000 c1
= fromhex (readchar ());
1001 c2
= fromhex (readchar ());
1003 if (csum
== (c1
<< 4) + c2
)
1008 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
1009 (c1
<< 4) + c2
, csum
, buf
);
1010 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1014 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1015 (c1
<< 4) + c2
, csum
, buf
);
1016 write (remote_desc
, "-", 1);
1023 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1027 write (remote_desc
, "+", 1);
1031 fprintf (stderr
, "[sent ack]\n");
1039 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1048 write_ok (char *buf
)
1056 write_enn (char *buf
)
1058 /* Some day, we should define the meanings of the error codes... */
1066 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
1073 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1074 *to
++ = tohex (nib
);
1076 *to
++ = tohex (nib
);
1083 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
1088 nib1
= fromhex (*from
++);
1089 nib2
= fromhex (*from
++);
1090 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1095 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1097 if ((regno
>> 12) != 0)
1098 *buf
++ = tohex ((regno
>> 12) & 0xf);
1099 if ((regno
>> 8) != 0)
1100 *buf
++ = tohex ((regno
>> 8) & 0xf);
1101 *buf
++ = tohex ((regno
>> 4) & 0xf);
1102 *buf
++ = tohex (regno
& 0xf);
1104 collect_register_as_string (regcache
, regno
, buf
);
1105 buf
+= 2 * register_size (regno
);
1112 new_thread_notify (int id
)
1116 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1120 if (server_waiting
== 0)
1123 sprintf (own_buf
, "n%x", id
);
1124 disable_async_io ();
1130 dead_thread_notify (int id
)
1134 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1138 sprintf (own_buf
, "x%x", id
);
1139 disable_async_io ();
1145 prepare_resume_reply (char *buf
, ptid_t ptid
,
1146 struct target_waitstatus
*status
)
1149 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1150 target_pid_to_str (ptid
), status
->kind
);
1152 switch (status
->kind
)
1154 case TARGET_WAITKIND_STOPPED
:
1156 struct thread_info
*saved_inferior
;
1158 struct regcache
*regcache
;
1160 sprintf (buf
, "T%02x", status
->value
.sig
);
1161 buf
+= strlen (buf
);
1163 regp
= gdbserver_expedite_regs
;
1165 saved_inferior
= current_inferior
;
1167 current_inferior
= find_thread_ptid (ptid
);
1169 regcache
= get_thread_regcache (current_inferior
, 1);
1171 if (the_target
->stopped_by_watchpoint
!= NULL
1172 && (*the_target
->stopped_by_watchpoint
) ())
1177 strncpy (buf
, "watch:", 6);
1180 addr
= (*the_target
->stopped_data_address
) ();
1182 /* Convert each byte of the address into two hexadecimal
1183 chars. Note that we take sizeof (void *) instead of
1184 sizeof (addr); this is to avoid sending a 64-bit
1185 address to a 32-bit GDB. */
1186 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1187 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1193 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1198 /* Formerly, if the debugger had not used any thread features
1199 we would not burden it with a thread status response. This
1200 was for the benefit of GDB 4.13 and older. However, in
1201 recent GDB versions the check (``if (cont_thread != 0)'')
1202 does not have the desired effect because of sillyness in
1203 the way that the remote protocol handles specifying a
1204 thread. Since thread support relies on qSymbol support
1205 anyway, assume GDB can handle threads. */
1207 if (using_threads
&& !disable_packet_Tthread
)
1209 /* This if (1) ought to be unnecessary. But remote_wait
1210 in GDB will claim this event belongs to inferior_ptid
1211 if we do not specify a thread, and there's no way for
1212 gdbserver to know what inferior_ptid is. */
1213 if (1 || !ptid_equal (general_thread
, ptid
))
1216 /* In non-stop, don't change the general thread behind
1219 general_thread
= ptid
;
1220 sprintf (buf
, "thread:");
1221 buf
+= strlen (buf
);
1222 buf
= write_ptid (buf
, ptid
);
1224 buf
+= strlen (buf
);
1226 if (the_target
->core_of_thread
)
1227 core
= (*the_target
->core_of_thread
) (ptid
);
1230 sprintf (buf
, "core:");
1231 buf
+= strlen (buf
);
1232 sprintf (buf
, "%x", core
);
1234 buf
+= strlen (buf
);
1241 strcpy (buf
, "library:;");
1242 buf
+= strlen (buf
);
1246 current_inferior
= saved_inferior
;
1249 case TARGET_WAITKIND_EXITED
:
1251 sprintf (buf
, "W%x;process:%x",
1252 status
->value
.integer
, ptid_get_pid (ptid
));
1254 sprintf (buf
, "W%02x", status
->value
.integer
);
1256 case TARGET_WAITKIND_SIGNALLED
:
1258 sprintf (buf
, "X%x;process:%x",
1259 status
->value
.sig
, ptid_get_pid (ptid
));
1261 sprintf (buf
, "X%02x", status
->value
.sig
);
1264 error ("unhandled waitkind");
1270 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1274 *mem_addr_ptr
= *len_ptr
= 0;
1276 while ((ch
= from
[i
++]) != ',')
1278 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1279 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1282 for (j
= 0; j
< 4; j
++)
1284 if ((ch
= from
[i
++]) == 0)
1286 *len_ptr
= *len_ptr
<< 4;
1287 *len_ptr
|= fromhex (ch
) & 0x0f;
1292 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1297 *mem_addr_ptr
= *len_ptr
= 0;
1299 while ((ch
= from
[i
++]) != ',')
1301 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1302 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1305 while ((ch
= from
[i
++]) != ':')
1307 *len_ptr
= *len_ptr
<< 4;
1308 *len_ptr
|= fromhex (ch
) & 0x0f;
1311 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1315 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1316 unsigned int *len_ptr
, unsigned char *to
)
1320 *mem_addr_ptr
= *len_ptr
= 0;
1322 while ((ch
= from
[i
++]) != ',')
1324 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1325 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1328 while ((ch
= from
[i
++]) != ':')
1330 *len_ptr
= *len_ptr
<< 4;
1331 *len_ptr
|= fromhex (ch
) & 0x0f;
1334 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1335 to
, *len_ptr
) != *len_ptr
)
1341 /* Decode a qXfer write request. */
1343 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1344 unsigned int *len
, unsigned char *data
)
1348 /* Extract and NUL-terminate the annex. */
1350 while (*buf
&& *buf
!= ':')
1356 /* Extract the offset. */
1358 while ((ch
= *buf
++) != ':')
1360 *offset
= *offset
<< 4;
1361 *offset
|= fromhex (ch
) & 0x0f;
1364 /* Get encoded data. */
1365 packet_len
-= buf
- *annex
;
1366 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1371 /* Decode the parameters of a qSearch:memory packet. */
1374 decode_search_memory_packet (const char *buf
, int packet_len
,
1375 CORE_ADDR
*start_addrp
,
1376 CORE_ADDR
*search_space_lenp
,
1377 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1379 const char *p
= buf
;
1381 p
= decode_address_to_semicolon (start_addrp
, p
);
1382 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1383 packet_len
-= p
- buf
;
1384 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1385 pattern
, packet_len
);
1390 free_sym_cache (struct sym_cache
*sym
)
1400 clear_symbol_cache (struct sym_cache
**symcache_p
)
1402 struct sym_cache
*sym
, *next
;
1404 /* Check the cache first. */
1405 for (sym
= *symcache_p
; sym
; sym
= next
)
1408 free_sym_cache (sym
);
1414 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1415 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1418 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1420 char own_buf
[266], *p
, *q
;
1422 struct sym_cache
*sym
;
1423 struct process_info
*proc
;
1425 proc
= current_process ();
1427 /* Check the cache first. */
1428 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1429 if (strcmp (name
, sym
->name
) == 0)
1435 /* If we've passed the call to thread_db_look_up_symbols, then
1436 anything not in the cache must not exist; we're not interested
1437 in any libraries loaded after that point, only in symbols in
1438 libpthread.so. It might not be an appropriate time to look
1439 up a symbol, e.g. while we're trying to fetch registers. */
1440 if (proc
->all_symbols_looked_up
)
1443 /* Send the request. */
1444 strcpy (own_buf
, "qSymbol:");
1445 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1446 if (putpkt (own_buf
) < 0)
1449 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1450 len
= getpkt (own_buf
);
1454 /* We ought to handle pretty much any packet at this point while we
1455 wait for the qSymbol "response". That requires re-entering the
1456 main loop. For now, this is an adequate approximation; allow
1457 GDB to read from memory while it figures out the address of the
1459 while (own_buf
[0] == 'm')
1462 unsigned char *mem_buf
;
1463 unsigned int mem_len
;
1465 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1466 mem_buf
= xmalloc (mem_len
);
1467 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1468 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1470 write_enn (own_buf
);
1472 if (putpkt (own_buf
) < 0)
1474 len
= getpkt (own_buf
);
1479 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1481 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1485 p
= own_buf
+ strlen ("qSymbol:");
1487 while (*q
&& *q
!= ':')
1490 /* Make sure we found a value for the symbol. */
1491 if (p
== q
|| *q
== '\0')
1494 decode_address (addrp
, p
, q
- p
);
1496 /* Save the symbol in our cache. */
1497 sym
= xmalloc (sizeof (*sym
));
1498 sym
->name
= xstrdup (name
);
1500 sym
->next
= proc
->symbol_cache
;
1501 proc
->symbol_cache
= sym
;
1507 monitor_output (const char *msg
)
1509 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1512 hexify (buf
+ 1, msg
, 0);
1518 /* Return a malloc allocated string with special characters from TEXT
1519 replaced by entity references. */
1522 xml_escape_text (const char *text
)
1527 /* Compute the length of the result. */
1528 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1546 /* Expand the result. */
1547 result
= xmalloc (i
+ special
+ 1);
1548 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1552 strcpy (result
+ i
+ special
, "'");
1556 strcpy (result
+ i
+ special
, """);
1560 strcpy (result
+ i
+ special
, "&");
1564 strcpy (result
+ i
+ special
, "<");
1568 strcpy (result
+ i
+ special
, ">");
1572 result
[i
+ special
] = text
[i
];
1575 result
[i
+ special
] = '\0';
1581 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1584 size_t new_buffer_size
;
1589 new_buffer_size
= buffer
->buffer_size
;
1591 if (new_buffer_size
== 0)
1592 new_buffer_size
= 1;
1594 while (buffer
->used_size
+ size
> new_buffer_size
)
1595 new_buffer_size
*= 2;
1596 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1599 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1600 buffer
->buffer
= new_buffer
;
1601 buffer
->buffer_size
= new_buffer_size
;
1602 buffer
->used_size
+= size
;
1606 buffer_free (struct buffer
*buffer
)
1611 free (buffer
->buffer
);
1612 buffer
->buffer
= NULL
;
1613 buffer
->buffer_size
= 0;
1614 buffer
->used_size
= 0;
1618 buffer_init (struct buffer
*buffer
)
1620 memset (buffer
, 0, sizeof (*buffer
));
1624 buffer_finish (struct buffer
*buffer
)
1626 char *ret
= buffer
->buffer
;
1627 buffer
->buffer
= NULL
;
1628 buffer
->buffer_size
= 0;
1629 buffer
->used_size
= 0;
1634 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1641 va_start (ap
, format
);
1644 for (f
= format
; *f
; f
++)
1653 char *a
= va_arg (ap
, char *);
1654 buffer_grow (buffer
, prev
, f
- prev
- 1);
1655 p
= xml_escape_text (a
);
1656 buffer_grow_str (buffer
, p
);
1663 int i
= va_arg (ap
, int);
1664 char b
[sizeof ("4294967295")];
1666 buffer_grow (buffer
, prev
, f
- prev
- 1);
1667 sprintf (b
, "%d", i
);
1668 buffer_grow_str (buffer
, b
);
1678 buffer_grow_str (buffer
, prev
);