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
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>
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t
;
74 # define INVALID_DESCRIPTOR INVALID_SOCKET
76 # define INVALID_DESCRIPTOR -1
79 /* A cache entry for a successfully looked-up symbol. */
84 struct sym_cache
*next
;
88 struct ui_file
*gdb_stdlog
;
90 static int remote_desc
= INVALID_DESCRIPTOR
;
92 /* FIXME headerize? */
93 extern int using_threads
;
94 extern int debug_threads
;
96 /* If true, then GDB has requested noack mode. */
98 /* If true, then we tell GDB to use noack mode by default. */
99 int transport_is_reliable
= 0;
102 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
103 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
106 /* Open a connection to a remote debugger.
107 NAME is the filename used for communication. */
110 remote_open (char *name
)
112 #if defined(F_SETFL) && defined (FASYNC)
113 int save_fcntl_flags
;
117 port_str
= strchr (name
, ':');
118 if (port_str
== NULL
)
121 error ("Only <host>:<port> is supported on this platform.");
125 if (stat (name
, &statbuf
) == 0
126 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
127 remote_desc
= open (name
, O_RDWR
);
135 perror_with_name ("Could not open remote device");
139 struct termios termios
;
140 tcgetattr (remote_desc
, &termios
);
145 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
146 termios
.c_cflag
|= CLOCAL
| CS8
;
147 termios
.c_cc
[VMIN
] = 1;
148 termios
.c_cc
[VTIME
] = 0;
150 tcsetattr (remote_desc
, TCSANOW
, &termios
);
156 struct termio termio
;
157 ioctl (remote_desc
, TCGETA
, &termio
);
162 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
163 termio
.c_cflag
|= CLOCAL
| CS8
;
164 termio
.c_cc
[VMIN
] = 1;
165 termio
.c_cc
[VTIME
] = 0;
167 ioctl (remote_desc
, TCSETA
, &termio
);
175 ioctl (remote_desc
, TIOCGETP
, &sg
);
177 ioctl (remote_desc
, TIOCSETP
, &sg
);
181 fprintf (stderr
, "Remote debugging using %s\n", name
);
182 #endif /* USE_WIN32API */
184 transport_is_reliable
= 0;
189 static int winsock_initialized
;
192 struct sockaddr_in sockaddr
;
197 port
= strtoul (port_str
+ 1, &port_end
, 10);
198 if (port_str
[1] == '\0' || *port_end
!= '\0')
199 fatal ("Bad port argument: %s", name
);
202 if (!winsock_initialized
)
206 WSAStartup (MAKEWORD (1, 0), &wsad
);
207 winsock_initialized
= 1;
211 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
213 perror_with_name ("Can't open socket");
215 /* Allow rapid reuse of this port. */
217 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
220 sockaddr
.sin_family
= PF_INET
;
221 sockaddr
.sin_port
= htons (port
);
222 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
224 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
225 || listen (tmp_desc
, 1))
226 perror_with_name ("Can't bind address");
228 /* If port is zero, a random port will be selected, and the
229 fprintf below needs to know what port was selected. */
232 socklen_t len
= sizeof (sockaddr
);
233 if (getsockname (tmp_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
234 || len
< sizeof (sockaddr
))
235 perror_with_name ("Can't determine port");
236 port
= ntohs (sockaddr
.sin_port
);
239 fprintf (stderr
, "Listening on port %d\n", port
);
242 tmp
= sizeof (sockaddr
);
243 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
244 if (remote_desc
== -1)
245 perror_with_name ("Accept failed");
247 /* Enable TCP keep alive process. */
249 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
250 (char *) &tmp
, sizeof (tmp
));
252 /* Tell TCP not to delay small packets. This greatly speeds up
253 interactive response. */
255 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
256 (char *) &tmp
, sizeof (tmp
));
260 close (tmp_desc
); /* No longer need this */
262 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
263 exits when the remote side dies. */
265 closesocket (tmp_desc
); /* No longer need this */
268 /* Convert IP address to string. */
269 fprintf (stderr
, "Remote debugging from host %s\n",
270 inet_ntoa (sockaddr
.sin_addr
));
272 transport_is_reliable
= 1;
275 #if defined(F_SETFL) && defined (FASYNC)
276 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
277 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
278 #if defined (F_SETOWN)
279 fcntl (remote_desc
, F_SETOWN
, getpid ());
283 /* Register the event loop handler. */
284 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
290 delete_file_handler (remote_desc
);
293 closesocket (remote_desc
);
299 /* Convert hex digit A to a number. */
304 if (a
>= '0' && a
<= '9')
306 else if (a
>= 'a' && a
<= 'f')
309 error ("Reply contains invalid hex digit");
313 static const char hexchars
[] = "0123456789abcdef";
316 ishex (int ch
, int *val
)
318 if ((ch
>= 'a') && (ch
<= 'f'))
320 *val
= ch
- 'a' + 10;
323 if ((ch
>= 'A') && (ch
<= 'F'))
325 *val
= ch
- 'A' + 10;
328 if ((ch
>= '0') && (ch
<= '9'))
337 unhexify (char *bin
, const char *hex
, int count
)
341 for (i
= 0; i
< count
; i
++)
343 if (hex
[0] == 0 || hex
[1] == 0)
345 /* Hex string is short, or of uneven length.
346 Return the count that has been converted so far. */
349 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
356 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
363 for (i
= 0; i
< len
; i
++)
367 addr
= addr
| (fromhex (ch
) & 0x0f);
373 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
378 while (*end
!= '\0' && *end
!= ';')
381 decode_address (addrp
, start
, end
- start
);
388 /* Convert number NIB to a hex digit. */
396 return 'a' + nib
- 10;
400 hexify (char *hex
, const char *bin
, int count
)
404 /* May use a length, or a nul-terminated string as input. */
406 count
= strlen (bin
);
408 for (i
= 0; i
< count
; i
++)
410 *hex
++ = tohex ((*bin
>> 4) & 0xf);
411 *hex
++ = tohex (*bin
++ & 0xf);
417 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
418 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
419 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
420 (which may be more than *OUT_LEN due to escape characters). The
421 total number of bytes in the output buffer will be at most
425 remote_escape_output (const gdb_byte
*buffer
, int len
,
426 gdb_byte
*out_buf
, int *out_len
,
429 int input_index
, output_index
;
432 for (input_index
= 0; input_index
< len
; input_index
++)
434 gdb_byte b
= buffer
[input_index
];
436 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
438 /* These must be escaped. */
439 if (output_index
+ 2 > out_maxlen
)
441 out_buf
[output_index
++] = '}';
442 out_buf
[output_index
++] = b
^ 0x20;
446 if (output_index
+ 1 > out_maxlen
)
448 out_buf
[output_index
++] = b
;
452 *out_len
= input_index
;
456 /* Convert BUFFER, escaped data LEN bytes long, into binary data
457 in OUT_BUF. Return the number of bytes written to OUT_BUF.
458 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
460 This function reverses remote_escape_output. It allows more
461 escaped characters than that function does, in particular because
462 '*' must be escaped to avoid the run-length encoding processing
463 in reading packets. */
466 remote_unescape_input (const gdb_byte
*buffer
, int len
,
467 gdb_byte
*out_buf
, int out_maxlen
)
469 int input_index
, output_index
;
474 for (input_index
= 0; input_index
< len
; input_index
++)
476 gdb_byte b
= buffer
[input_index
];
478 if (output_index
+ 1 > out_maxlen
)
479 error ("Received too much data from the target.");
483 out_buf
[output_index
++] = b
^ 0x20;
489 out_buf
[output_index
++] = b
;
493 error ("Unmatched escape character in target response.");
498 /* Look for a sequence of characters which can be run-length encoded.
499 If there are any, update *CSUM and *P. Otherwise, output the
500 single character. Return the number of characters consumed. */
503 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
507 /* Always output the character. */
511 /* Don't go past '~'. */
515 for (n
= 1; n
< remaining
; n
++)
516 if (buf
[n
] != buf
[0])
519 /* N is the index of the first character not the same as buf[0].
520 buf[0] is counted twice, so by decrementing N, we get the number
521 of characters the RLE sequence will replace. */
527 /* Skip the frame characters. The manual says to skip '+' and '-'
528 also, but there's no reason to. Unfortunately these two unusable
529 characters double the encoded length of a four byte zero
531 while (n
+ 29 == '$' || n
+ 29 == '#')
543 unpack_varlen_hex (char *buff
, /* packet to parse */
549 while (ishex (*buff
, &nibble
))
552 retval
= retval
<< 4;
553 retval
|= nibble
& 0x0f;
559 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
562 write_ptid (char *buf
, ptid_t ptid
)
568 pid
= ptid_get_pid (ptid
);
570 buf
+= sprintf (buf
, "p-%x.", -pid
);
572 buf
+= sprintf (buf
, "p%x.", pid
);
574 tid
= ptid_get_lwp (ptid
);
576 buf
+= sprintf (buf
, "-%x", -tid
);
578 buf
+= sprintf (buf
, "%x", tid
);
584 hex_or_minus_one (char *buf
, char **obuf
)
588 if (strncmp (buf
, "-1", 2) == 0)
594 buf
= unpack_varlen_hex (buf
, &ret
);
602 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
603 passed the last parsed char. Returns null_ptid on error. */
605 read_ptid (char *buf
, char **obuf
)
609 ULONGEST pid
= 0, tid
= 0;
613 /* Multi-process ptid. */
614 pp
= unpack_varlen_hex (p
+ 1, &pid
);
616 error ("invalid remote ptid: %s\n", p
);
620 tid
= hex_or_minus_one (p
, &pp
);
624 return ptid_build (pid
, tid
, 0);
627 /* No multi-process. Just a tid. */
628 tid
= hex_or_minus_one (p
, &pp
);
630 /* Since the stub is not sending a process id, then default to
631 what's in the current inferior. */
632 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
636 return ptid_build (pid
, tid
, 0);
639 /* Send a packet to the remote machine, with error checking.
640 The data of the packet is in BUF, and the length of the
641 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
644 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
647 unsigned char csum
= 0;
652 buf2
= xmalloc (PBUFSIZ
);
654 /* Copy the packet into buffer BUF2, encapsulating it
655 and giving it a checksum. */
663 for (i
= 0; i
< cnt
;)
664 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
667 *p
++ = tohex ((csum
>> 4) & 0xf);
668 *p
++ = tohex (csum
& 0xf);
672 /* Send it over and over until we get a positive ack. */
678 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
680 perror ("putpkt(write)");
685 if (noack_mode
|| is_notif
)
687 /* Don't expect an ack then. */
691 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
693 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
701 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
704 cc
= read (remote_desc
, buf3
, 1);
707 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
714 fprintf (stderr
, "putpkt(read): Got EOF\n");
716 perror ("putpkt(read)");
722 /* Check for an input interrupt while we're here. */
723 if (buf3
[0] == '\003' && current_inferior
!= NULL
)
724 (*the_target
->request_interrupt
) ();
726 while (buf3
[0] != '+');
729 return 1; /* Success! */
733 putpkt_binary (char *buf
, int cnt
)
735 return putpkt_binary_1 (buf
, cnt
, 0);
738 /* Send a packet to the remote machine, with error checking. The data
739 of the packet is in BUF, and the packet should be a NUL-terminated
740 string. Returns >= 0 on success, -1 otherwise. */
745 return putpkt_binary (buf
, strlen (buf
));
749 putpkt_notif (char *buf
)
751 return putpkt_binary_1 (buf
, strlen (buf
), 1);
754 /* Come here when we get an input interrupt from the remote side. This
755 interrupt should only be active while we are waiting for the child to do
756 something. About the only thing that should come through is a ^C, which
757 will cause us to request child interruption. */
760 input_interrupt (int unused
)
763 struct timeval immediate
= { 0, 0 };
765 /* Protect against spurious interrupts. This has been observed to
766 be a problem under NetBSD 1.4 and 1.5. */
769 FD_SET (remote_desc
, &readset
);
770 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
775 cc
= read (remote_desc
, &c
, 1);
777 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
779 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
784 (*the_target
->request_interrupt
) ();
788 /* Check if the remote side sent us an interrupt request (^C). */
790 check_remote_input_interrupt_request (void)
792 /* This function may be called before establishing communications,
793 therefore we need to validate the remote descriptor. */
795 if (remote_desc
== INVALID_DESCRIPTOR
)
801 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
802 accept Control-C from the client, and must be disabled when talking to
806 unblock_async_io (void)
811 sigemptyset (&sigio_set
);
812 sigaddset (&sigio_set
, SIGIO
);
813 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
817 /* Current state of asynchronous I/O. */
818 static int async_io_enabled
;
820 /* Enable asynchronous I/O. */
822 enable_async_io (void)
824 if (async_io_enabled
)
828 signal (SIGIO
, input_interrupt
);
830 async_io_enabled
= 1;
833 /* Disable asynchronous I/O. */
835 disable_async_io (void)
837 if (!async_io_enabled
)
841 signal (SIGIO
, SIG_IGN
);
843 async_io_enabled
= 0;
847 initialize_async_io (void)
849 /* Make sure that async I/O starts disabled. */
850 async_io_enabled
= 1;
853 /* Make sure the signal is unblocked. */
857 /* Returns next char from remote GDB. -1 if error. */
862 static unsigned char buf
[BUFSIZ
];
863 static int bufcnt
= 0;
864 static unsigned char *bufp
;
869 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
874 fprintf (stderr
, "readchar: Got EOF\n");
886 /* Read a packet from the remote machine, with error checking,
887 and store it in BUF. Returns length of packet, or negative if error. */
893 unsigned char csum
, c1
, c2
;
907 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
928 c1
= fromhex (readchar ());
929 c2
= fromhex (readchar ());
931 if (csum
== (c1
<< 4) + c2
)
936 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
937 (c1
<< 4) + c2
, csum
, buf
);
938 /* Not much we can do, GDB wasn't expecting an ack/nac. */
942 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
943 (c1
<< 4) + c2
, csum
, buf
);
944 write (remote_desc
, "-", 1);
951 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
955 write (remote_desc
, "+", 1);
959 fprintf (stderr
, "[sent ack]\n");
967 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
984 write_enn (char *buf
)
986 /* Some day, we should define the meanings of the error codes... */
994 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
1001 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1002 *to
++ = tohex (nib
);
1004 *to
++ = tohex (nib
);
1011 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
1016 nib1
= fromhex (*from
++);
1017 nib2
= fromhex (*from
++);
1018 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1023 outreg (int regno
, char *buf
)
1025 if ((regno
>> 12) != 0)
1026 *buf
++ = tohex ((regno
>> 12) & 0xf);
1027 if ((regno
>> 8) != 0)
1028 *buf
++ = tohex ((regno
>> 8) & 0xf);
1029 *buf
++ = tohex ((regno
>> 4) & 0xf);
1030 *buf
++ = tohex (regno
& 0xf);
1032 collect_register_as_string (regno
, buf
);
1033 buf
+= 2 * register_size (regno
);
1040 new_thread_notify (int id
)
1044 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1048 if (server_waiting
== 0)
1051 sprintf (own_buf
, "n%x", id
);
1052 disable_async_io ();
1058 dead_thread_notify (int id
)
1062 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1066 sprintf (own_buf
, "x%x", id
);
1067 disable_async_io ();
1073 prepare_resume_reply (char *buf
, ptid_t ptid
,
1074 struct target_waitstatus
*status
)
1077 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1078 target_pid_to_str (ptid
), status
->kind
);
1080 switch (status
->kind
)
1082 case TARGET_WAITKIND_STOPPED
:
1084 struct thread_info
*saved_inferior
;
1087 sprintf (buf
, "T%02x", status
->value
.sig
);
1088 buf
+= strlen (buf
);
1090 regp
= gdbserver_expedite_regs
;
1092 saved_inferior
= current_inferior
;
1094 current_inferior
= find_thread_ptid (ptid
);
1096 if (the_target
->stopped_by_watchpoint
!= NULL
1097 && (*the_target
->stopped_by_watchpoint
) ())
1102 strncpy (buf
, "watch:", 6);
1105 addr
= (*the_target
->stopped_data_address
) ();
1107 /* Convert each byte of the address into two hexadecimal
1108 chars. Note that we take sizeof (void *) instead of
1109 sizeof (addr); this is to avoid sending a 64-bit
1110 address to a 32-bit GDB. */
1111 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1112 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1118 buf
= outreg (find_regno (*regp
), buf
);
1123 /* Formerly, if the debugger had not used any thread features
1124 we would not burden it with a thread status response. This
1125 was for the benefit of GDB 4.13 and older. However, in
1126 recent GDB versions the check (``if (cont_thread != 0)'')
1127 does not have the desired effect because of sillyness in
1128 the way that the remote protocol handles specifying a
1129 thread. Since thread support relies on qSymbol support
1130 anyway, assume GDB can handle threads. */
1132 if (using_threads
&& !disable_packet_Tthread
)
1134 /* This if (1) ought to be unnecessary. But remote_wait
1135 in GDB will claim this event belongs to inferior_ptid
1136 if we do not specify a thread, and there's no way for
1137 gdbserver to know what inferior_ptid is. */
1138 if (1 || !ptid_equal (general_thread
, ptid
))
1140 /* In non-stop, don't change the general thread behind
1143 general_thread
= ptid
;
1144 sprintf (buf
, "thread:");
1145 buf
+= strlen (buf
);
1146 buf
= write_ptid (buf
, ptid
);
1148 buf
+= strlen (buf
);
1154 strcpy (buf
, "library:;");
1155 buf
+= strlen (buf
);
1159 current_inferior
= saved_inferior
;
1162 case TARGET_WAITKIND_EXITED
:
1164 sprintf (buf
, "W%x;process:%x",
1165 status
->value
.integer
, ptid_get_pid (ptid
));
1167 sprintf (buf
, "W%02x", status
->value
.integer
);
1169 case TARGET_WAITKIND_SIGNALLED
:
1171 sprintf (buf
, "X%x;process:%x",
1172 status
->value
.sig
, ptid_get_pid (ptid
));
1174 sprintf (buf
, "X%02x", status
->value
.sig
);
1177 error ("unhandled waitkind");
1183 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1187 *mem_addr_ptr
= *len_ptr
= 0;
1189 while ((ch
= from
[i
++]) != ',')
1191 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1192 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1195 for (j
= 0; j
< 4; j
++)
1197 if ((ch
= from
[i
++]) == 0)
1199 *len_ptr
= *len_ptr
<< 4;
1200 *len_ptr
|= fromhex (ch
) & 0x0f;
1205 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1210 *mem_addr_ptr
= *len_ptr
= 0;
1212 while ((ch
= from
[i
++]) != ',')
1214 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1215 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1218 while ((ch
= from
[i
++]) != ':')
1220 *len_ptr
= *len_ptr
<< 4;
1221 *len_ptr
|= fromhex (ch
) & 0x0f;
1224 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1228 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1229 unsigned int *len_ptr
, unsigned char *to
)
1233 *mem_addr_ptr
= *len_ptr
= 0;
1235 while ((ch
= from
[i
++]) != ',')
1237 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1238 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1241 while ((ch
= from
[i
++]) != ':')
1243 *len_ptr
= *len_ptr
<< 4;
1244 *len_ptr
|= fromhex (ch
) & 0x0f;
1247 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1248 to
, *len_ptr
) != *len_ptr
)
1254 /* Decode a qXfer write request. */
1256 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1257 unsigned int *len
, unsigned char *data
)
1261 /* Extract and NUL-terminate the annex. */
1263 while (*buf
&& *buf
!= ':')
1269 /* Extract the offset. */
1271 while ((ch
= *buf
++) != ':')
1273 *offset
= *offset
<< 4;
1274 *offset
|= fromhex (ch
) & 0x0f;
1277 /* Get encoded data. */
1278 packet_len
-= buf
- *annex
;
1279 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1284 /* Decode the parameters of a qSearch:memory packet. */
1287 decode_search_memory_packet (const char *buf
, int packet_len
,
1288 CORE_ADDR
*start_addrp
,
1289 CORE_ADDR
*search_space_lenp
,
1290 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1292 const char *p
= buf
;
1294 p
= decode_address_to_semicolon (start_addrp
, p
);
1295 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1296 packet_len
-= p
- buf
;
1297 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1298 pattern
, packet_len
);
1303 free_sym_cache (struct sym_cache
*sym
)
1313 clear_symbol_cache (struct sym_cache
**symcache_p
)
1315 struct sym_cache
*sym
, *next
;
1317 /* Check the cache first. */
1318 for (sym
= *symcache_p
; sym
; sym
= next
)
1321 free_sym_cache (sym
);
1327 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1328 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1331 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1333 char own_buf
[266], *p
, *q
;
1335 struct sym_cache
*sym
;
1336 struct process_info
*proc
;
1338 proc
= current_process ();
1340 /* Check the cache first. */
1341 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1342 if (strcmp (name
, sym
->name
) == 0)
1348 /* If we've passed the call to thread_db_look_up_symbols, then
1349 anything not in the cache must not exist; we're not interested
1350 in any libraries loaded after that point, only in symbols in
1351 libpthread.so. It might not be an appropriate time to look
1352 up a symbol, e.g. while we're trying to fetch registers. */
1353 if (proc
->all_symbols_looked_up
)
1356 /* Send the request. */
1357 strcpy (own_buf
, "qSymbol:");
1358 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1359 if (putpkt (own_buf
) < 0)
1362 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1363 len
= getpkt (own_buf
);
1367 /* We ought to handle pretty much any packet at this point while we
1368 wait for the qSymbol "response". That requires re-entering the
1369 main loop. For now, this is an adequate approximation; allow
1370 GDB to read from memory while it figures out the address of the
1372 while (own_buf
[0] == 'm')
1375 unsigned char *mem_buf
;
1376 unsigned int mem_len
;
1378 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1379 mem_buf
= xmalloc (mem_len
);
1380 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1381 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1383 write_enn (own_buf
);
1385 if (putpkt (own_buf
) < 0)
1387 len
= getpkt (own_buf
);
1392 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1394 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1398 p
= own_buf
+ strlen ("qSymbol:");
1400 while (*q
&& *q
!= ':')
1403 /* Make sure we found a value for the symbol. */
1404 if (p
== q
|| *q
== '\0')
1407 decode_address (addrp
, p
, q
- p
);
1409 /* Save the symbol in our cache. */
1410 sym
= xmalloc (sizeof (*sym
));
1411 sym
->name
= xstrdup (name
);
1413 sym
->next
= proc
->symbol_cache
;
1414 proc
->symbol_cache
= sym
;
1420 monitor_output (const char *msg
)
1422 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1425 hexify (buf
+ 1, msg
, 0);
1431 /* Return a malloc allocated string with special characters from TEXT
1432 replaced by entity references. */
1435 xml_escape_text (const char *text
)
1440 /* Compute the length of the result. */
1441 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1459 /* Expand the result. */
1460 result
= xmalloc (i
+ special
+ 1);
1461 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1465 strcpy (result
+ i
+ special
, "'");
1469 strcpy (result
+ i
+ special
, """);
1473 strcpy (result
+ i
+ special
, "&");
1477 strcpy (result
+ i
+ special
, "<");
1481 strcpy (result
+ i
+ special
, ">");
1485 result
[i
+ special
] = text
[i
];
1488 result
[i
+ special
] = '\0';
1494 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1497 size_t new_buffer_size
;
1502 new_buffer_size
= buffer
->buffer_size
;
1504 if (new_buffer_size
== 0)
1505 new_buffer_size
= 1;
1507 while (buffer
->used_size
+ size
> new_buffer_size
)
1508 new_buffer_size
*= 2;
1509 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1512 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1513 buffer
->buffer
= new_buffer
;
1514 buffer
->buffer_size
= new_buffer_size
;
1515 buffer
->used_size
+= size
;
1519 buffer_free (struct buffer
*buffer
)
1524 free (buffer
->buffer
);
1525 buffer
->buffer
= NULL
;
1526 buffer
->buffer_size
= 0;
1527 buffer
->used_size
= 0;
1531 buffer_init (struct buffer
*buffer
)
1533 memset (buffer
, 0, sizeof (*buffer
));
1537 buffer_finish (struct buffer
*buffer
)
1539 char *ret
= buffer
->buffer
;
1540 buffer
->buffer
= NULL
;
1541 buffer
->buffer_size
= 0;
1542 buffer
->used_size
= 0;
1547 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1554 va_start (ap
, format
);
1557 for (f
= format
; *f
; f
++)
1566 char *a
= va_arg (ap
, char *);
1567 buffer_grow (buffer
, prev
, f
- prev
- 1);
1568 p
= xml_escape_text (a
);
1569 buffer_grow_str (buffer
, p
);
1581 buffer_grow_str (buffer
, prev
);