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 /* A cache entry for a successfully looked-up symbol. */
88 struct sym_cache
*next
;
92 struct ui_file
*gdb_stdlog
;
94 static int remote_desc
= INVALID_DESCRIPTOR
;
95 static int listen_desc
= INVALID_DESCRIPTOR
;
97 /* FIXME headerize? */
98 extern int using_threads
;
99 extern int debug_threads
;
101 /* If true, then GDB has requested noack mode. */
103 /* If true, then we tell GDB to use noack mode by default. */
104 int transport_is_reliable
= 0;
107 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
108 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
114 return remote_desc
!= INVALID_DESCRIPTOR
;
118 enable_async_notification (int fd
)
120 #if defined(F_SETFL) && defined (FASYNC)
121 int save_fcntl_flags
;
123 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
124 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
125 #if defined (F_SETOWN)
126 fcntl (fd
, F_SETOWN
, getpid ());
132 handle_accept_event (int err
, gdb_client_data client_data
)
134 struct sockaddr_in sockaddr
;
138 fprintf (stderr
, "handling possible accept event\n");
140 tmp
= sizeof (sockaddr
);
141 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
142 if (remote_desc
== -1)
143 perror_with_name ("Accept failed");
145 /* Enable TCP keep alive process. */
147 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
148 (char *) &tmp
, sizeof (tmp
));
150 /* Tell TCP not to delay small packets. This greatly speeds up
151 interactive response. */
153 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
154 (char *) &tmp
, sizeof (tmp
));
157 close (listen_desc
); /* No longer need this */
159 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
160 exits when the remote side dies. */
162 closesocket (listen_desc
); /* No longer need this */
165 delete_file_handler (listen_desc
);
167 /* Convert IP address to string. */
168 fprintf (stderr
, "Remote debugging from host %s\n",
169 inet_ntoa (sockaddr
.sin_addr
));
171 enable_async_notification (remote_desc
);
173 /* Register the event loop handler. */
174 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
176 /* We have a new GDB connection now. If we were disconnected
177 tracing, there's a window where the target could report a stop
178 event to the event loop, and since we have a connection now, we'd
179 try to send vStopped notifications to GDB. But, don't do that
180 until GDB as selected all-stop/non-stop, and has queried the
181 threads' status ('?'). */
187 /* Open a connection to a remote debugger.
188 NAME is the filename used for communication. */
191 remote_open (char *name
)
195 port_str
= strchr (name
, ':');
196 if (port_str
== NULL
)
199 error ("Only <host>:<port> is supported on this platform.");
203 if (stat (name
, &statbuf
) == 0
204 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
205 remote_desc
= open (name
, O_RDWR
);
213 perror_with_name ("Could not open remote device");
217 struct termios termios
;
218 tcgetattr (remote_desc
, &termios
);
223 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
224 termios
.c_cflag
|= CLOCAL
| CS8
;
225 termios
.c_cc
[VMIN
] = 1;
226 termios
.c_cc
[VTIME
] = 0;
228 tcsetattr (remote_desc
, TCSANOW
, &termios
);
234 struct termio termio
;
235 ioctl (remote_desc
, TCGETA
, &termio
);
240 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
241 termio
.c_cflag
|= CLOCAL
| CS8
;
242 termio
.c_cc
[VMIN
] = 1;
243 termio
.c_cc
[VTIME
] = 0;
245 ioctl (remote_desc
, TCSETA
, &termio
);
253 ioctl (remote_desc
, TIOCGETP
, &sg
);
255 ioctl (remote_desc
, TIOCSETP
, &sg
);
259 fprintf (stderr
, "Remote debugging using %s\n", name
);
261 transport_is_reliable
= 0;
263 enable_async_notification (remote_desc
);
265 /* Register the event loop handler. */
266 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
267 #endif /* USE_WIN32API */
272 static int winsock_initialized
;
275 struct sockaddr_in sockaddr
;
279 port
= strtoul (port_str
+ 1, &port_end
, 10);
280 if (port_str
[1] == '\0' || *port_end
!= '\0')
281 fatal ("Bad port argument: %s", name
);
284 if (!winsock_initialized
)
288 WSAStartup (MAKEWORD (1, 0), &wsad
);
289 winsock_initialized
= 1;
293 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
295 perror_with_name ("Can't open socket");
297 /* Allow rapid reuse of this port. */
299 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
302 sockaddr
.sin_family
= PF_INET
;
303 sockaddr
.sin_port
= htons (port
);
304 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
306 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
307 || listen (listen_desc
, 1))
308 perror_with_name ("Can't bind address");
310 /* If port is zero, a random port will be selected, and the
311 fprintf below needs to know what port was selected. */
314 socklen_t len
= sizeof (sockaddr
);
315 if (getsockname (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
316 || len
< sizeof (sockaddr
))
317 perror_with_name ("Can't determine port");
318 port
= ntohs (sockaddr
.sin_port
);
321 fprintf (stderr
, "Listening on port %d\n", port
);
324 /* Register the event loop handler. */
325 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
327 transport_is_reliable
= 1;
334 delete_file_handler (remote_desc
);
337 closesocket (remote_desc
);
341 remote_desc
= INVALID_DESCRIPTOR
;
344 /* Convert hex digit A to a number. */
349 if (a
>= '0' && a
<= '9')
351 else if (a
>= 'a' && a
<= 'f')
354 error ("Reply contains invalid hex digit");
358 static const char hexchars
[] = "0123456789abcdef";
361 ishex (int ch
, int *val
)
363 if ((ch
>= 'a') && (ch
<= 'f'))
365 *val
= ch
- 'a' + 10;
368 if ((ch
>= 'A') && (ch
<= 'F'))
370 *val
= ch
- 'A' + 10;
373 if ((ch
>= '0') && (ch
<= '9'))
382 unhexify (char *bin
, const char *hex
, int count
)
386 for (i
= 0; i
< count
; i
++)
388 if (hex
[0] == 0 || hex
[1] == 0)
390 /* Hex string is short, or of uneven length.
391 Return the count that has been converted so far. */
394 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
401 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
408 for (i
= 0; i
< len
; i
++)
412 addr
= addr
| (fromhex (ch
) & 0x0f);
418 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
423 while (*end
!= '\0' && *end
!= ';')
426 decode_address (addrp
, start
, end
- start
);
433 /* Convert number NIB to a hex digit. */
441 return 'a' + nib
- 10;
445 hexify (char *hex
, const char *bin
, int count
)
449 /* May use a length, or a nul-terminated string as input. */
451 count
= strlen (bin
);
453 for (i
= 0; i
< count
; i
++)
455 *hex
++ = tohex ((*bin
>> 4) & 0xf);
456 *hex
++ = tohex (*bin
++ & 0xf);
462 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
463 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
464 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
465 (which may be more than *OUT_LEN due to escape characters). The
466 total number of bytes in the output buffer will be at most
470 remote_escape_output (const gdb_byte
*buffer
, int len
,
471 gdb_byte
*out_buf
, int *out_len
,
474 int input_index
, output_index
;
477 for (input_index
= 0; input_index
< len
; input_index
++)
479 gdb_byte b
= buffer
[input_index
];
481 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
483 /* These must be escaped. */
484 if (output_index
+ 2 > out_maxlen
)
486 out_buf
[output_index
++] = '}';
487 out_buf
[output_index
++] = b
^ 0x20;
491 if (output_index
+ 1 > out_maxlen
)
493 out_buf
[output_index
++] = b
;
497 *out_len
= input_index
;
501 /* Convert BUFFER, escaped data LEN bytes long, into binary data
502 in OUT_BUF. Return the number of bytes written to OUT_BUF.
503 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
505 This function reverses remote_escape_output. It allows more
506 escaped characters than that function does, in particular because
507 '*' must be escaped to avoid the run-length encoding processing
508 in reading packets. */
511 remote_unescape_input (const gdb_byte
*buffer
, int len
,
512 gdb_byte
*out_buf
, int out_maxlen
)
514 int input_index
, output_index
;
519 for (input_index
= 0; input_index
< len
; input_index
++)
521 gdb_byte b
= buffer
[input_index
];
523 if (output_index
+ 1 > out_maxlen
)
524 error ("Received too much data from the target.");
528 out_buf
[output_index
++] = b
^ 0x20;
534 out_buf
[output_index
++] = b
;
538 error ("Unmatched escape character in target response.");
543 /* Look for a sequence of characters which can be run-length encoded.
544 If there are any, update *CSUM and *P. Otherwise, output the
545 single character. Return the number of characters consumed. */
548 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
552 /* Always output the character. */
556 /* Don't go past '~'. */
560 for (n
= 1; n
< remaining
; n
++)
561 if (buf
[n
] != buf
[0])
564 /* N is the index of the first character not the same as buf[0].
565 buf[0] is counted twice, so by decrementing N, we get the number
566 of characters the RLE sequence will replace. */
572 /* Skip the frame characters. The manual says to skip '+' and '-'
573 also, but there's no reason to. Unfortunately these two unusable
574 characters double the encoded length of a four byte zero
576 while (n
+ 29 == '$' || n
+ 29 == '#')
588 unpack_varlen_hex (char *buff
, /* packet to parse */
594 while (ishex (*buff
, &nibble
))
597 retval
= retval
<< 4;
598 retval
|= nibble
& 0x0f;
604 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
607 write_ptid (char *buf
, ptid_t ptid
)
613 pid
= ptid_get_pid (ptid
);
615 buf
+= sprintf (buf
, "p-%x.", -pid
);
617 buf
+= sprintf (buf
, "p%x.", pid
);
619 tid
= ptid_get_lwp (ptid
);
621 buf
+= sprintf (buf
, "-%x", -tid
);
623 buf
+= sprintf (buf
, "%x", tid
);
629 hex_or_minus_one (char *buf
, char **obuf
)
633 if (strncmp (buf
, "-1", 2) == 0)
639 buf
= unpack_varlen_hex (buf
, &ret
);
647 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
648 passed the last parsed char. Returns null_ptid on error. */
650 read_ptid (char *buf
, char **obuf
)
654 ULONGEST pid
= 0, tid
= 0;
658 /* Multi-process ptid. */
659 pp
= unpack_varlen_hex (p
+ 1, &pid
);
661 error ("invalid remote ptid: %s\n", p
);
665 tid
= hex_or_minus_one (p
, &pp
);
669 return ptid_build (pid
, tid
, 0);
672 /* No multi-process. Just a tid. */
673 tid
= hex_or_minus_one (p
, &pp
);
675 /* Since the stub is not sending a process id, then default to
676 what's in the current inferior. */
677 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
681 return ptid_build (pid
, tid
, 0);
684 /* Send a packet to the remote machine, with error checking.
685 The data of the packet is in BUF, and the length of the
686 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
689 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
692 unsigned char csum
= 0;
697 buf2
= xmalloc (PBUFSIZ
);
699 /* Copy the packet into buffer BUF2, encapsulating it
700 and giving it a checksum. */
708 for (i
= 0; i
< cnt
;)
709 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
712 *p
++ = tohex ((csum
>> 4) & 0xf);
713 *p
++ = tohex (csum
& 0xf);
717 /* 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
);
749 cc
= read (remote_desc
, buf3
, 1);
752 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
759 fprintf (stderr
, "putpkt(read): Got EOF\n");
761 perror ("putpkt(read)");
767 /* Check for an input interrupt while we're here. */
768 if (buf3
[0] == '\003' && current_inferior
!= NULL
)
769 (*the_target
->request_interrupt
) ();
771 while (buf3
[0] != '+');
774 return 1; /* Success! */
778 putpkt_binary (char *buf
, int cnt
)
780 return putpkt_binary_1 (buf
, cnt
, 0);
783 /* Send a packet to the remote machine, with error checking. The data
784 of the packet is in BUF, and the packet should be a NUL-terminated
785 string. Returns >= 0 on success, -1 otherwise. */
790 return putpkt_binary (buf
, strlen (buf
));
794 putpkt_notif (char *buf
)
796 return putpkt_binary_1 (buf
, strlen (buf
), 1);
799 /* Come here when we get an input interrupt from the remote side. This
800 interrupt should only be active while we are waiting for the child to do
801 something. About the only thing that should come through is a ^C, which
802 will cause us to request child interruption. */
805 input_interrupt (int unused
)
808 struct timeval immediate
= { 0, 0 };
810 /* Protect against spurious interrupts. This has been observed to
811 be a problem under NetBSD 1.4 and 1.5. */
814 FD_SET (remote_desc
, &readset
);
815 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
820 cc
= read (remote_desc
, &c
, 1);
822 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
824 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
829 (*the_target
->request_interrupt
) ();
833 /* Check if the remote side sent us an interrupt request (^C). */
835 check_remote_input_interrupt_request (void)
837 /* This function may be called before establishing communications,
838 therefore we need to validate the remote descriptor. */
840 if (remote_desc
== INVALID_DESCRIPTOR
)
846 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
847 accept Control-C from the client, and must be disabled when talking to
851 unblock_async_io (void)
856 sigemptyset (&sigio_set
);
857 sigaddset (&sigio_set
, SIGIO
);
858 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
864 nto_comctrl (int enable
)
866 struct sigevent event
;
870 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
871 event
.sigev_signo
= SIGIO
;
872 event
.sigev_code
= 0;
873 event
.sigev_value
.sival_ptr
= NULL
;
874 event
.sigev_priority
= -1;
875 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
879 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
884 /* Current state of asynchronous I/O. */
885 static int async_io_enabled
;
887 /* Enable asynchronous I/O. */
889 enable_async_io (void)
891 if (async_io_enabled
)
895 signal (SIGIO
, input_interrupt
);
897 async_io_enabled
= 1;
903 /* Disable asynchronous I/O. */
905 disable_async_io (void)
907 if (!async_io_enabled
)
911 signal (SIGIO
, SIG_IGN
);
913 async_io_enabled
= 0;
921 initialize_async_io (void)
923 /* Make sure that async I/O starts disabled. */
924 async_io_enabled
= 1;
927 /* Make sure the signal is unblocked. */
931 /* Returns next char from remote GDB. -1 if error. */
936 static unsigned char buf
[BUFSIZ
];
937 static int bufcnt
= 0;
938 static unsigned char *bufp
;
943 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
948 fprintf (stderr
, "readchar: Got EOF\n");
960 /* Read a packet from the remote machine, with error checking,
961 and store it in BUF. Returns length of packet, or negative if error. */
967 unsigned char csum
, c1
, c2
;
981 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1002 c1
= fromhex (readchar ());
1003 c2
= fromhex (readchar ());
1005 if (csum
== (c1
<< 4) + c2
)
1010 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
1011 (c1
<< 4) + c2
, csum
, buf
);
1012 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1016 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1017 (c1
<< 4) + c2
, csum
, buf
);
1018 write (remote_desc
, "-", 1);
1025 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1029 write (remote_desc
, "+", 1);
1033 fprintf (stderr
, "[sent ack]\n");
1041 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1050 write_ok (char *buf
)
1058 write_enn (char *buf
)
1060 /* Some day, we should define the meanings of the error codes... */
1068 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
1075 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1076 *to
++ = tohex (nib
);
1078 *to
++ = tohex (nib
);
1085 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
1090 nib1
= fromhex (*from
++);
1091 nib2
= fromhex (*from
++);
1092 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1097 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1099 if ((regno
>> 12) != 0)
1100 *buf
++ = tohex ((regno
>> 12) & 0xf);
1101 if ((regno
>> 8) != 0)
1102 *buf
++ = tohex ((regno
>> 8) & 0xf);
1103 *buf
++ = tohex ((regno
>> 4) & 0xf);
1104 *buf
++ = tohex (regno
& 0xf);
1106 collect_register_as_string (regcache
, regno
, buf
);
1107 buf
+= 2 * register_size (regno
);
1114 new_thread_notify (int id
)
1118 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1122 if (server_waiting
== 0)
1125 sprintf (own_buf
, "n%x", id
);
1126 disable_async_io ();
1132 dead_thread_notify (int id
)
1136 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1140 sprintf (own_buf
, "x%x", id
);
1141 disable_async_io ();
1147 prepare_resume_reply (char *buf
, ptid_t ptid
,
1148 struct target_waitstatus
*status
)
1151 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1152 target_pid_to_str (ptid
), status
->kind
);
1154 switch (status
->kind
)
1156 case TARGET_WAITKIND_STOPPED
:
1158 struct thread_info
*saved_inferior
;
1160 struct regcache
*regcache
;
1162 sprintf (buf
, "T%02x", status
->value
.sig
);
1163 buf
+= strlen (buf
);
1165 regp
= gdbserver_expedite_regs
;
1167 saved_inferior
= current_inferior
;
1169 current_inferior
= find_thread_ptid (ptid
);
1171 regcache
= get_thread_regcache (current_inferior
, 1);
1173 if (the_target
->stopped_by_watchpoint
!= NULL
1174 && (*the_target
->stopped_by_watchpoint
) ())
1179 strncpy (buf
, "watch:", 6);
1182 addr
= (*the_target
->stopped_data_address
) ();
1184 /* Convert each byte of the address into two hexadecimal
1185 chars. Note that we take sizeof (void *) instead of
1186 sizeof (addr); this is to avoid sending a 64-bit
1187 address to a 32-bit GDB. */
1188 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1189 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1195 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1200 /* Formerly, if the debugger had not used any thread features
1201 we would not burden it with a thread status response. This
1202 was for the benefit of GDB 4.13 and older. However, in
1203 recent GDB versions the check (``if (cont_thread != 0)'')
1204 does not have the desired effect because of sillyness in
1205 the way that the remote protocol handles specifying a
1206 thread. Since thread support relies on qSymbol support
1207 anyway, assume GDB can handle threads. */
1209 if (using_threads
&& !disable_packet_Tthread
)
1211 /* This if (1) ought to be unnecessary. But remote_wait
1212 in GDB will claim this event belongs to inferior_ptid
1213 if we do not specify a thread, and there's no way for
1214 gdbserver to know what inferior_ptid is. */
1215 if (1 || !ptid_equal (general_thread
, ptid
))
1218 /* In non-stop, don't change the general thread behind
1221 general_thread
= ptid
;
1222 sprintf (buf
, "thread:");
1223 buf
+= strlen (buf
);
1224 buf
= write_ptid (buf
, ptid
);
1226 buf
+= strlen (buf
);
1228 if (the_target
->core_of_thread
)
1229 core
= (*the_target
->core_of_thread
) (ptid
);
1232 sprintf (buf
, "core:");
1233 buf
+= strlen (buf
);
1234 sprintf (buf
, "%x", core
);
1236 buf
+= strlen (buf
);
1243 strcpy (buf
, "library:;");
1244 buf
+= strlen (buf
);
1248 current_inferior
= saved_inferior
;
1251 case TARGET_WAITKIND_EXITED
:
1253 sprintf (buf
, "W%x;process:%x",
1254 status
->value
.integer
, ptid_get_pid (ptid
));
1256 sprintf (buf
, "W%02x", status
->value
.integer
);
1258 case TARGET_WAITKIND_SIGNALLED
:
1260 sprintf (buf
, "X%x;process:%x",
1261 status
->value
.sig
, ptid_get_pid (ptid
));
1263 sprintf (buf
, "X%02x", status
->value
.sig
);
1266 error ("unhandled waitkind");
1272 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1276 *mem_addr_ptr
= *len_ptr
= 0;
1278 while ((ch
= from
[i
++]) != ',')
1280 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1281 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1284 for (j
= 0; j
< 4; j
++)
1286 if ((ch
= from
[i
++]) == 0)
1288 *len_ptr
= *len_ptr
<< 4;
1289 *len_ptr
|= fromhex (ch
) & 0x0f;
1294 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1299 *mem_addr_ptr
= *len_ptr
= 0;
1301 while ((ch
= from
[i
++]) != ',')
1303 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1304 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1307 while ((ch
= from
[i
++]) != ':')
1309 *len_ptr
= *len_ptr
<< 4;
1310 *len_ptr
|= fromhex (ch
) & 0x0f;
1313 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1317 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1318 unsigned int *len_ptr
, unsigned char *to
)
1322 *mem_addr_ptr
= *len_ptr
= 0;
1324 while ((ch
= from
[i
++]) != ',')
1326 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1327 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1330 while ((ch
= from
[i
++]) != ':')
1332 *len_ptr
= *len_ptr
<< 4;
1333 *len_ptr
|= fromhex (ch
) & 0x0f;
1336 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1337 to
, *len_ptr
) != *len_ptr
)
1343 /* Decode a qXfer write request. */
1345 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1346 unsigned int *len
, unsigned char *data
)
1350 /* Extract and NUL-terminate the annex. */
1352 while (*buf
&& *buf
!= ':')
1358 /* Extract the offset. */
1360 while ((ch
= *buf
++) != ':')
1362 *offset
= *offset
<< 4;
1363 *offset
|= fromhex (ch
) & 0x0f;
1366 /* Get encoded data. */
1367 packet_len
-= buf
- *annex
;
1368 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1373 /* Decode the parameters of a qSearch:memory packet. */
1376 decode_search_memory_packet (const char *buf
, int packet_len
,
1377 CORE_ADDR
*start_addrp
,
1378 CORE_ADDR
*search_space_lenp
,
1379 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1381 const char *p
= buf
;
1383 p
= decode_address_to_semicolon (start_addrp
, p
);
1384 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1385 packet_len
-= p
- buf
;
1386 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1387 pattern
, packet_len
);
1392 free_sym_cache (struct sym_cache
*sym
)
1402 clear_symbol_cache (struct sym_cache
**symcache_p
)
1404 struct sym_cache
*sym
, *next
;
1406 /* Check the cache first. */
1407 for (sym
= *symcache_p
; sym
; sym
= next
)
1410 free_sym_cache (sym
);
1416 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1417 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1420 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1422 char own_buf
[266], *p
, *q
;
1424 struct sym_cache
*sym
;
1425 struct process_info
*proc
;
1427 proc
= current_process ();
1429 /* Check the cache first. */
1430 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1431 if (strcmp (name
, sym
->name
) == 0)
1437 /* If we've passed the call to thread_db_look_up_symbols, then
1438 anything not in the cache must not exist; we're not interested
1439 in any libraries loaded after that point, only in symbols in
1440 libpthread.so. It might not be an appropriate time to look
1441 up a symbol, e.g. while we're trying to fetch registers. */
1442 if (proc
->all_symbols_looked_up
)
1445 /* Send the request. */
1446 strcpy (own_buf
, "qSymbol:");
1447 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1448 if (putpkt (own_buf
) < 0)
1451 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1452 len
= getpkt (own_buf
);
1456 /* We ought to handle pretty much any packet at this point while we
1457 wait for the qSymbol "response". That requires re-entering the
1458 main loop. For now, this is an adequate approximation; allow
1459 GDB to read from memory while it figures out the address of the
1461 while (own_buf
[0] == 'm')
1464 unsigned char *mem_buf
;
1465 unsigned int mem_len
;
1467 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1468 mem_buf
= xmalloc (mem_len
);
1469 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1470 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1472 write_enn (own_buf
);
1474 if (putpkt (own_buf
) < 0)
1476 len
= getpkt (own_buf
);
1481 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1483 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1487 p
= own_buf
+ strlen ("qSymbol:");
1489 while (*q
&& *q
!= ':')
1492 /* Make sure we found a value for the symbol. */
1493 if (p
== q
|| *q
== '\0')
1496 decode_address (addrp
, p
, q
- p
);
1498 /* Save the symbol in our cache. */
1499 sym
= xmalloc (sizeof (*sym
));
1500 sym
->name
= xstrdup (name
);
1502 sym
->next
= proc
->symbol_cache
;
1503 proc
->symbol_cache
= sym
;
1509 monitor_output (const char *msg
)
1511 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1514 hexify (buf
+ 1, msg
, 0);
1520 /* Return a malloc allocated string with special characters from TEXT
1521 replaced by entity references. */
1524 xml_escape_text (const char *text
)
1529 /* Compute the length of the result. */
1530 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1548 /* Expand the result. */
1549 result
= xmalloc (i
+ special
+ 1);
1550 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1554 strcpy (result
+ i
+ special
, "'");
1558 strcpy (result
+ i
+ special
, """);
1562 strcpy (result
+ i
+ special
, "&");
1566 strcpy (result
+ i
+ special
, "<");
1570 strcpy (result
+ i
+ special
, ">");
1574 result
[i
+ special
] = text
[i
];
1577 result
[i
+ special
] = '\0';
1583 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1586 size_t new_buffer_size
;
1591 new_buffer_size
= buffer
->buffer_size
;
1593 if (new_buffer_size
== 0)
1594 new_buffer_size
= 1;
1596 while (buffer
->used_size
+ size
> new_buffer_size
)
1597 new_buffer_size
*= 2;
1598 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1601 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1602 buffer
->buffer
= new_buffer
;
1603 buffer
->buffer_size
= new_buffer_size
;
1604 buffer
->used_size
+= size
;
1608 buffer_free (struct buffer
*buffer
)
1613 free (buffer
->buffer
);
1614 buffer
->buffer
= NULL
;
1615 buffer
->buffer_size
= 0;
1616 buffer
->used_size
= 0;
1620 buffer_init (struct buffer
*buffer
)
1622 memset (buffer
, 0, sizeof (*buffer
));
1626 buffer_finish (struct buffer
*buffer
)
1628 char *ret
= buffer
->buffer
;
1629 buffer
->buffer
= NULL
;
1630 buffer
->buffer_size
= 0;
1631 buffer
->used_size
= 0;
1636 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1643 va_start (ap
, format
);
1646 for (f
= format
; *f
; f
++)
1655 char *a
= va_arg (ap
, char *);
1656 buffer_grow (buffer
, prev
, f
- prev
- 1);
1657 p
= xml_escape_text (a
);
1658 buffer_grow_str (buffer
, p
);
1665 int i
= va_arg (ap
, int);
1666 char b
[sizeof ("4294967295")];
1668 buffer_grow (buffer
, prev
, f
- prev
- 1);
1669 sprintf (b
, "%d", i
);
1670 buffer_grow_str (buffer
, b
);
1680 buffer_grow_str (buffer
, prev
);