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>
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
;
96 /* FIXME headerize? */
97 extern int using_threads
;
98 extern int debug_threads
;
100 /* If true, then GDB has requested noack mode. */
102 /* If true, then we tell GDB to use noack mode by default. */
103 int transport_is_reliable
= 0;
106 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
107 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
110 /* Open a connection to a remote debugger.
111 NAME is the filename used for communication. */
114 remote_open (char *name
)
116 #if defined(F_SETFL) && defined (FASYNC)
117 int save_fcntl_flags
;
121 port_str
= strchr (name
, ':');
122 if (port_str
== NULL
)
125 error ("Only <host>:<port> is supported on this platform.");
129 if (stat (name
, &statbuf
) == 0
130 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
131 remote_desc
= open (name
, O_RDWR
);
139 perror_with_name ("Could not open remote device");
143 struct termios termios
;
144 tcgetattr (remote_desc
, &termios
);
149 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
150 termios
.c_cflag
|= CLOCAL
| CS8
;
151 termios
.c_cc
[VMIN
] = 1;
152 termios
.c_cc
[VTIME
] = 0;
154 tcsetattr (remote_desc
, TCSANOW
, &termios
);
160 struct termio termio
;
161 ioctl (remote_desc
, TCGETA
, &termio
);
166 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
167 termio
.c_cflag
|= CLOCAL
| CS8
;
168 termio
.c_cc
[VMIN
] = 1;
169 termio
.c_cc
[VTIME
] = 0;
171 ioctl (remote_desc
, TCSETA
, &termio
);
179 ioctl (remote_desc
, TIOCGETP
, &sg
);
181 ioctl (remote_desc
, TIOCSETP
, &sg
);
185 fprintf (stderr
, "Remote debugging using %s\n", name
);
186 #endif /* USE_WIN32API */
188 transport_is_reliable
= 0;
193 static int winsock_initialized
;
196 struct sockaddr_in sockaddr
;
201 port
= strtoul (port_str
+ 1, &port_end
, 10);
202 if (port_str
[1] == '\0' || *port_end
!= '\0')
203 fatal ("Bad port argument: %s", name
);
206 if (!winsock_initialized
)
210 WSAStartup (MAKEWORD (1, 0), &wsad
);
211 winsock_initialized
= 1;
215 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
217 perror_with_name ("Can't open socket");
219 /* Allow rapid reuse of this port. */
221 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
224 sockaddr
.sin_family
= PF_INET
;
225 sockaddr
.sin_port
= htons (port
);
226 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
228 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
229 || listen (tmp_desc
, 1))
230 perror_with_name ("Can't bind address");
232 /* If port is zero, a random port will be selected, and the
233 fprintf below needs to know what port was selected. */
236 socklen_t len
= sizeof (sockaddr
);
237 if (getsockname (tmp_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
238 || len
< sizeof (sockaddr
))
239 perror_with_name ("Can't determine port");
240 port
= ntohs (sockaddr
.sin_port
);
243 fprintf (stderr
, "Listening on port %d\n", port
);
246 tmp
= sizeof (sockaddr
);
247 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
248 if (remote_desc
== -1)
249 perror_with_name ("Accept failed");
251 /* Enable TCP keep alive process. */
253 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
254 (char *) &tmp
, sizeof (tmp
));
256 /* Tell TCP not to delay small packets. This greatly speeds up
257 interactive response. */
259 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
260 (char *) &tmp
, sizeof (tmp
));
264 close (tmp_desc
); /* No longer need this */
266 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
267 exits when the remote side dies. */
269 closesocket (tmp_desc
); /* No longer need this */
272 /* Convert IP address to string. */
273 fprintf (stderr
, "Remote debugging from host %s\n",
274 inet_ntoa (sockaddr
.sin_addr
));
276 transport_is_reliable
= 1;
279 #if defined(F_SETFL) && defined (FASYNC)
280 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
281 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
282 #if defined (F_SETOWN)
283 fcntl (remote_desc
, F_SETOWN
, getpid ());
287 /* Register the event loop handler. */
288 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
294 delete_file_handler (remote_desc
);
297 closesocket (remote_desc
);
303 /* Convert hex digit A to a number. */
308 if (a
>= '0' && a
<= '9')
310 else if (a
>= 'a' && a
<= 'f')
313 error ("Reply contains invalid hex digit");
317 static const char hexchars
[] = "0123456789abcdef";
320 ishex (int ch
, int *val
)
322 if ((ch
>= 'a') && (ch
<= 'f'))
324 *val
= ch
- 'a' + 10;
327 if ((ch
>= 'A') && (ch
<= 'F'))
329 *val
= ch
- 'A' + 10;
332 if ((ch
>= '0') && (ch
<= '9'))
341 unhexify (char *bin
, const char *hex
, int count
)
345 for (i
= 0; i
< count
; i
++)
347 if (hex
[0] == 0 || hex
[1] == 0)
349 /* Hex string is short, or of uneven length.
350 Return the count that has been converted so far. */
353 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
360 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
367 for (i
= 0; i
< len
; i
++)
371 addr
= addr
| (fromhex (ch
) & 0x0f);
377 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
382 while (*end
!= '\0' && *end
!= ';')
385 decode_address (addrp
, start
, end
- start
);
392 /* Convert number NIB to a hex digit. */
400 return 'a' + nib
- 10;
404 hexify (char *hex
, const char *bin
, int count
)
408 /* May use a length, or a nul-terminated string as input. */
410 count
= strlen (bin
);
412 for (i
= 0; i
< count
; i
++)
414 *hex
++ = tohex ((*bin
>> 4) & 0xf);
415 *hex
++ = tohex (*bin
++ & 0xf);
421 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
422 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
423 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
424 (which may be more than *OUT_LEN due to escape characters). The
425 total number of bytes in the output buffer will be at most
429 remote_escape_output (const gdb_byte
*buffer
, int len
,
430 gdb_byte
*out_buf
, int *out_len
,
433 int input_index
, output_index
;
436 for (input_index
= 0; input_index
< len
; input_index
++)
438 gdb_byte b
= buffer
[input_index
];
440 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
442 /* These must be escaped. */
443 if (output_index
+ 2 > out_maxlen
)
445 out_buf
[output_index
++] = '}';
446 out_buf
[output_index
++] = b
^ 0x20;
450 if (output_index
+ 1 > out_maxlen
)
452 out_buf
[output_index
++] = b
;
456 *out_len
= input_index
;
460 /* Convert BUFFER, escaped data LEN bytes long, into binary data
461 in OUT_BUF. Return the number of bytes written to OUT_BUF.
462 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
464 This function reverses remote_escape_output. It allows more
465 escaped characters than that function does, in particular because
466 '*' must be escaped to avoid the run-length encoding processing
467 in reading packets. */
470 remote_unescape_input (const gdb_byte
*buffer
, int len
,
471 gdb_byte
*out_buf
, int out_maxlen
)
473 int input_index
, output_index
;
478 for (input_index
= 0; input_index
< len
; input_index
++)
480 gdb_byte b
= buffer
[input_index
];
482 if (output_index
+ 1 > out_maxlen
)
483 error ("Received too much data from the target.");
487 out_buf
[output_index
++] = b
^ 0x20;
493 out_buf
[output_index
++] = b
;
497 error ("Unmatched escape character in target response.");
502 /* Look for a sequence of characters which can be run-length encoded.
503 If there are any, update *CSUM and *P. Otherwise, output the
504 single character. Return the number of characters consumed. */
507 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
511 /* Always output the character. */
515 /* Don't go past '~'. */
519 for (n
= 1; n
< remaining
; n
++)
520 if (buf
[n
] != buf
[0])
523 /* N is the index of the first character not the same as buf[0].
524 buf[0] is counted twice, so by decrementing N, we get the number
525 of characters the RLE sequence will replace. */
531 /* Skip the frame characters. The manual says to skip '+' and '-'
532 also, but there's no reason to. Unfortunately these two unusable
533 characters double the encoded length of a four byte zero
535 while (n
+ 29 == '$' || n
+ 29 == '#')
547 unpack_varlen_hex (char *buff
, /* packet to parse */
553 while (ishex (*buff
, &nibble
))
556 retval
= retval
<< 4;
557 retval
|= nibble
& 0x0f;
563 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
566 write_ptid (char *buf
, ptid_t ptid
)
572 pid
= ptid_get_pid (ptid
);
574 buf
+= sprintf (buf
, "p-%x.", -pid
);
576 buf
+= sprintf (buf
, "p%x.", pid
);
578 tid
= ptid_get_lwp (ptid
);
580 buf
+= sprintf (buf
, "-%x", -tid
);
582 buf
+= sprintf (buf
, "%x", tid
);
588 hex_or_minus_one (char *buf
, char **obuf
)
592 if (strncmp (buf
, "-1", 2) == 0)
598 buf
= unpack_varlen_hex (buf
, &ret
);
606 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
607 passed the last parsed char. Returns null_ptid on error. */
609 read_ptid (char *buf
, char **obuf
)
613 ULONGEST pid
= 0, tid
= 0;
617 /* Multi-process ptid. */
618 pp
= unpack_varlen_hex (p
+ 1, &pid
);
620 error ("invalid remote ptid: %s\n", p
);
624 tid
= hex_or_minus_one (p
, &pp
);
628 return ptid_build (pid
, tid
, 0);
631 /* No multi-process. Just a tid. */
632 tid
= hex_or_minus_one (p
, &pp
);
634 /* Since the stub is not sending a process id, then default to
635 what's in the current inferior. */
636 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
640 return ptid_build (pid
, tid
, 0);
643 /* Send a packet to the remote machine, with error checking.
644 The data of the packet is in BUF, and the length of the
645 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
648 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
651 unsigned char csum
= 0;
656 buf2
= xmalloc (PBUFSIZ
);
658 /* Copy the packet into buffer BUF2, encapsulating it
659 and giving it a checksum. */
667 for (i
= 0; i
< cnt
;)
668 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
671 *p
++ = tohex ((csum
>> 4) & 0xf);
672 *p
++ = tohex (csum
& 0xf);
676 /* Send it over and over until we get a positive ack. */
682 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
684 perror ("putpkt(write)");
689 if (noack_mode
|| is_notif
)
691 /* Don't expect an ack then. */
695 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
697 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
705 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
708 cc
= read (remote_desc
, buf3
, 1);
711 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
718 fprintf (stderr
, "putpkt(read): Got EOF\n");
720 perror ("putpkt(read)");
726 /* Check for an input interrupt while we're here. */
727 if (buf3
[0] == '\003' && current_inferior
!= NULL
)
728 (*the_target
->request_interrupt
) ();
730 while (buf3
[0] != '+');
733 return 1; /* Success! */
737 putpkt_binary (char *buf
, int cnt
)
739 return putpkt_binary_1 (buf
, cnt
, 0);
742 /* Send a packet to the remote machine, with error checking. The data
743 of the packet is in BUF, and the packet should be a NUL-terminated
744 string. Returns >= 0 on success, -1 otherwise. */
749 return putpkt_binary (buf
, strlen (buf
));
753 putpkt_notif (char *buf
)
755 return putpkt_binary_1 (buf
, strlen (buf
), 1);
758 /* Come here when we get an input interrupt from the remote side. This
759 interrupt should only be active while we are waiting for the child to do
760 something. About the only thing that should come through is a ^C, which
761 will cause us to request child interruption. */
764 input_interrupt (int unused
)
767 struct timeval immediate
= { 0, 0 };
769 /* Protect against spurious interrupts. This has been observed to
770 be a problem under NetBSD 1.4 and 1.5. */
773 FD_SET (remote_desc
, &readset
);
774 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
779 cc
= read (remote_desc
, &c
, 1);
781 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
783 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
788 (*the_target
->request_interrupt
) ();
792 /* Check if the remote side sent us an interrupt request (^C). */
794 check_remote_input_interrupt_request (void)
796 /* This function may be called before establishing communications,
797 therefore we need to validate the remote descriptor. */
799 if (remote_desc
== INVALID_DESCRIPTOR
)
805 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
806 accept Control-C from the client, and must be disabled when talking to
810 unblock_async_io (void)
815 sigemptyset (&sigio_set
);
816 sigaddset (&sigio_set
, SIGIO
);
817 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
823 nto_comctrl (int enable
)
825 struct sigevent event
;
829 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
830 event
.sigev_signo
= SIGIO
;
831 event
.sigev_code
= 0;
832 event
.sigev_value
.sival_ptr
= NULL
;
833 event
.sigev_priority
= -1;
834 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
838 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
843 /* Current state of asynchronous I/O. */
844 static int async_io_enabled
;
846 /* Enable asynchronous I/O. */
848 enable_async_io (void)
850 if (async_io_enabled
)
854 signal (SIGIO
, input_interrupt
);
856 async_io_enabled
= 1;
862 /* Disable asynchronous I/O. */
864 disable_async_io (void)
866 if (!async_io_enabled
)
870 signal (SIGIO
, SIG_IGN
);
872 async_io_enabled
= 0;
880 initialize_async_io (void)
882 /* Make sure that async I/O starts disabled. */
883 async_io_enabled
= 1;
886 /* Make sure the signal is unblocked. */
890 /* Returns next char from remote GDB. -1 if error. */
895 static unsigned char buf
[BUFSIZ
];
896 static int bufcnt
= 0;
897 static unsigned char *bufp
;
902 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
907 fprintf (stderr
, "readchar: Got EOF\n");
919 /* Read a packet from the remote machine, with error checking,
920 and store it in BUF. Returns length of packet, or negative if error. */
926 unsigned char csum
, c1
, c2
;
940 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
961 c1
= fromhex (readchar ());
962 c2
= fromhex (readchar ());
964 if (csum
== (c1
<< 4) + c2
)
969 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
970 (c1
<< 4) + c2
, csum
, buf
);
971 /* Not much we can do, GDB wasn't expecting an ack/nac. */
975 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
976 (c1
<< 4) + c2
, csum
, buf
);
977 write (remote_desc
, "-", 1);
984 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
988 write (remote_desc
, "+", 1);
992 fprintf (stderr
, "[sent ack]\n");
1000 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1009 write_ok (char *buf
)
1017 write_enn (char *buf
)
1019 /* Some day, we should define the meanings of the error codes... */
1027 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
1034 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1035 *to
++ = tohex (nib
);
1037 *to
++ = tohex (nib
);
1044 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
1049 nib1
= fromhex (*from
++);
1050 nib2
= fromhex (*from
++);
1051 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1056 outreg (int regno
, char *buf
)
1058 if ((regno
>> 12) != 0)
1059 *buf
++ = tohex ((regno
>> 12) & 0xf);
1060 if ((regno
>> 8) != 0)
1061 *buf
++ = tohex ((regno
>> 8) & 0xf);
1062 *buf
++ = tohex ((regno
>> 4) & 0xf);
1063 *buf
++ = tohex (regno
& 0xf);
1065 collect_register_as_string (regno
, buf
);
1066 buf
+= 2 * register_size (regno
);
1073 new_thread_notify (int id
)
1077 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1081 if (server_waiting
== 0)
1084 sprintf (own_buf
, "n%x", id
);
1085 disable_async_io ();
1091 dead_thread_notify (int id
)
1095 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1099 sprintf (own_buf
, "x%x", id
);
1100 disable_async_io ();
1106 prepare_resume_reply (char *buf
, ptid_t ptid
,
1107 struct target_waitstatus
*status
)
1110 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1111 target_pid_to_str (ptid
), status
->kind
);
1113 switch (status
->kind
)
1115 case TARGET_WAITKIND_STOPPED
:
1117 struct thread_info
*saved_inferior
;
1120 sprintf (buf
, "T%02x", status
->value
.sig
);
1121 buf
+= strlen (buf
);
1123 regp
= gdbserver_expedite_regs
;
1125 saved_inferior
= current_inferior
;
1127 current_inferior
= find_thread_ptid (ptid
);
1129 if (the_target
->stopped_by_watchpoint
!= NULL
1130 && (*the_target
->stopped_by_watchpoint
) ())
1135 strncpy (buf
, "watch:", 6);
1138 addr
= (*the_target
->stopped_data_address
) ();
1140 /* Convert each byte of the address into two hexadecimal
1141 chars. Note that we take sizeof (void *) instead of
1142 sizeof (addr); this is to avoid sending a 64-bit
1143 address to a 32-bit GDB. */
1144 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1145 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1151 buf
= outreg (find_regno (*regp
), buf
);
1156 /* Formerly, if the debugger had not used any thread features
1157 we would not burden it with a thread status response. This
1158 was for the benefit of GDB 4.13 and older. However, in
1159 recent GDB versions the check (``if (cont_thread != 0)'')
1160 does not have the desired effect because of sillyness in
1161 the way that the remote protocol handles specifying a
1162 thread. Since thread support relies on qSymbol support
1163 anyway, assume GDB can handle threads. */
1165 if (using_threads
&& !disable_packet_Tthread
)
1167 /* This if (1) ought to be unnecessary. But remote_wait
1168 in GDB will claim this event belongs to inferior_ptid
1169 if we do not specify a thread, and there's no way for
1170 gdbserver to know what inferior_ptid is. */
1171 if (1 || !ptid_equal (general_thread
, ptid
))
1173 /* In non-stop, don't change the general thread behind
1176 general_thread
= ptid
;
1177 sprintf (buf
, "thread:");
1178 buf
+= strlen (buf
);
1179 buf
= write_ptid (buf
, ptid
);
1181 buf
+= strlen (buf
);
1187 strcpy (buf
, "library:;");
1188 buf
+= strlen (buf
);
1192 current_inferior
= saved_inferior
;
1195 case TARGET_WAITKIND_EXITED
:
1197 sprintf (buf
, "W%x;process:%x",
1198 status
->value
.integer
, ptid_get_pid (ptid
));
1200 sprintf (buf
, "W%02x", status
->value
.integer
);
1202 case TARGET_WAITKIND_SIGNALLED
:
1204 sprintf (buf
, "X%x;process:%x",
1205 status
->value
.sig
, ptid_get_pid (ptid
));
1207 sprintf (buf
, "X%02x", status
->value
.sig
);
1210 error ("unhandled waitkind");
1216 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1220 *mem_addr_ptr
= *len_ptr
= 0;
1222 while ((ch
= from
[i
++]) != ',')
1224 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1225 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1228 for (j
= 0; j
< 4; j
++)
1230 if ((ch
= from
[i
++]) == 0)
1232 *len_ptr
= *len_ptr
<< 4;
1233 *len_ptr
|= fromhex (ch
) & 0x0f;
1238 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1243 *mem_addr_ptr
= *len_ptr
= 0;
1245 while ((ch
= from
[i
++]) != ',')
1247 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1248 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1251 while ((ch
= from
[i
++]) != ':')
1253 *len_ptr
= *len_ptr
<< 4;
1254 *len_ptr
|= fromhex (ch
) & 0x0f;
1257 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1261 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1262 unsigned int *len_ptr
, unsigned char *to
)
1266 *mem_addr_ptr
= *len_ptr
= 0;
1268 while ((ch
= from
[i
++]) != ',')
1270 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1271 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1274 while ((ch
= from
[i
++]) != ':')
1276 *len_ptr
= *len_ptr
<< 4;
1277 *len_ptr
|= fromhex (ch
) & 0x0f;
1280 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1281 to
, *len_ptr
) != *len_ptr
)
1287 /* Decode a qXfer write request. */
1289 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1290 unsigned int *len
, unsigned char *data
)
1294 /* Extract and NUL-terminate the annex. */
1296 while (*buf
&& *buf
!= ':')
1302 /* Extract the offset. */
1304 while ((ch
= *buf
++) != ':')
1306 *offset
= *offset
<< 4;
1307 *offset
|= fromhex (ch
) & 0x0f;
1310 /* Get encoded data. */
1311 packet_len
-= buf
- *annex
;
1312 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1317 /* Decode the parameters of a qSearch:memory packet. */
1320 decode_search_memory_packet (const char *buf
, int packet_len
,
1321 CORE_ADDR
*start_addrp
,
1322 CORE_ADDR
*search_space_lenp
,
1323 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1325 const char *p
= buf
;
1327 p
= decode_address_to_semicolon (start_addrp
, p
);
1328 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1329 packet_len
-= p
- buf
;
1330 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1331 pattern
, packet_len
);
1336 free_sym_cache (struct sym_cache
*sym
)
1346 clear_symbol_cache (struct sym_cache
**symcache_p
)
1348 struct sym_cache
*sym
, *next
;
1350 /* Check the cache first. */
1351 for (sym
= *symcache_p
; sym
; sym
= next
)
1354 free_sym_cache (sym
);
1360 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1361 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1364 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1366 char own_buf
[266], *p
, *q
;
1368 struct sym_cache
*sym
;
1369 struct process_info
*proc
;
1371 proc
= current_process ();
1373 /* Check the cache first. */
1374 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1375 if (strcmp (name
, sym
->name
) == 0)
1381 /* If we've passed the call to thread_db_look_up_symbols, then
1382 anything not in the cache must not exist; we're not interested
1383 in any libraries loaded after that point, only in symbols in
1384 libpthread.so. It might not be an appropriate time to look
1385 up a symbol, e.g. while we're trying to fetch registers. */
1386 if (proc
->all_symbols_looked_up
)
1389 /* Send the request. */
1390 strcpy (own_buf
, "qSymbol:");
1391 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1392 if (putpkt (own_buf
) < 0)
1395 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1396 len
= getpkt (own_buf
);
1400 /* We ought to handle pretty much any packet at this point while we
1401 wait for the qSymbol "response". That requires re-entering the
1402 main loop. For now, this is an adequate approximation; allow
1403 GDB to read from memory while it figures out the address of the
1405 while (own_buf
[0] == 'm')
1408 unsigned char *mem_buf
;
1409 unsigned int mem_len
;
1411 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1412 mem_buf
= xmalloc (mem_len
);
1413 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1414 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1416 write_enn (own_buf
);
1418 if (putpkt (own_buf
) < 0)
1420 len
= getpkt (own_buf
);
1425 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1427 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1431 p
= own_buf
+ strlen ("qSymbol:");
1433 while (*q
&& *q
!= ':')
1436 /* Make sure we found a value for the symbol. */
1437 if (p
== q
|| *q
== '\0')
1440 decode_address (addrp
, p
, q
- p
);
1442 /* Save the symbol in our cache. */
1443 sym
= xmalloc (sizeof (*sym
));
1444 sym
->name
= xstrdup (name
);
1446 sym
->next
= proc
->symbol_cache
;
1447 proc
->symbol_cache
= sym
;
1453 monitor_output (const char *msg
)
1455 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1458 hexify (buf
+ 1, msg
, 0);
1464 /* Return a malloc allocated string with special characters from TEXT
1465 replaced by entity references. */
1468 xml_escape_text (const char *text
)
1473 /* Compute the length of the result. */
1474 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1492 /* Expand the result. */
1493 result
= xmalloc (i
+ special
+ 1);
1494 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1498 strcpy (result
+ i
+ special
, "'");
1502 strcpy (result
+ i
+ special
, """);
1506 strcpy (result
+ i
+ special
, "&");
1510 strcpy (result
+ i
+ special
, "<");
1514 strcpy (result
+ i
+ special
, ">");
1518 result
[i
+ special
] = text
[i
];
1521 result
[i
+ special
] = '\0';
1527 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1530 size_t new_buffer_size
;
1535 new_buffer_size
= buffer
->buffer_size
;
1537 if (new_buffer_size
== 0)
1538 new_buffer_size
= 1;
1540 while (buffer
->used_size
+ size
> new_buffer_size
)
1541 new_buffer_size
*= 2;
1542 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1545 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1546 buffer
->buffer
= new_buffer
;
1547 buffer
->buffer_size
= new_buffer_size
;
1548 buffer
->used_size
+= size
;
1552 buffer_free (struct buffer
*buffer
)
1557 free (buffer
->buffer
);
1558 buffer
->buffer
= NULL
;
1559 buffer
->buffer_size
= 0;
1560 buffer
->used_size
= 0;
1564 buffer_init (struct buffer
*buffer
)
1566 memset (buffer
, 0, sizeof (*buffer
));
1570 buffer_finish (struct buffer
*buffer
)
1572 char *ret
= buffer
->buffer
;
1573 buffer
->buffer
= NULL
;
1574 buffer
->buffer_size
= 0;
1575 buffer
->used_size
= 0;
1580 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1587 va_start (ap
, format
);
1590 for (f
= format
; *f
; f
++)
1599 char *a
= va_arg (ap
, char *);
1600 buffer_grow (buffer
, prev
, f
- prev
- 1);
1601 p
= xml_escape_text (a
);
1602 buffer_grow_str (buffer
, p
);
1614 buffer_grow_str (buffer
, prev
);