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 /* Extra value for readchar_callback. */
85 /* The callback is currently not scheduled. */
89 /* Status of the readchar callback.
90 Either NOT_SCHEDULED or the callback id. */
91 static int readchar_callback
= NOT_SCHEDULED
;
93 static int readchar (void);
94 static void reset_readchar (void);
95 static void reschedule (void);
97 /* A cache entry for a successfully looked-up symbol. */
102 struct sym_cache
*next
;
105 int remote_debug
= 0;
106 struct ui_file
*gdb_stdlog
;
108 static int remote_desc
= INVALID_DESCRIPTOR
;
109 static int listen_desc
= INVALID_DESCRIPTOR
;
111 /* FIXME headerize? */
112 extern int using_threads
;
113 extern int debug_threads
;
115 /* If true, then GDB has requested noack mode. */
117 /* If true, then we tell GDB to use noack mode by default. */
118 int transport_is_reliable
= 0;
121 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
122 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
128 return remote_desc
!= INVALID_DESCRIPTOR
;
132 enable_async_notification (int fd
)
134 #if defined(F_SETFL) && defined (FASYNC)
135 int save_fcntl_flags
;
137 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
138 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
139 #if defined (F_SETOWN)
140 fcntl (fd
, F_SETOWN
, getpid ());
146 handle_accept_event (int err
, gdb_client_data client_data
)
148 struct sockaddr_in sockaddr
;
152 fprintf (stderr
, "handling possible accept event\n");
154 tmp
= sizeof (sockaddr
);
155 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
156 if (remote_desc
== -1)
157 perror_with_name ("Accept failed");
159 /* Enable TCP keep alive process. */
161 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
162 (char *) &tmp
, sizeof (tmp
));
164 /* Tell TCP not to delay small packets. This greatly speeds up
165 interactive response. */
167 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
168 (char *) &tmp
, sizeof (tmp
));
171 close (listen_desc
); /* No longer need this */
173 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
174 exits when the remote side dies. */
176 closesocket (listen_desc
); /* No longer need this */
179 delete_file_handler (listen_desc
);
181 /* Convert IP address to string. */
182 fprintf (stderr
, "Remote debugging from host %s\n",
183 inet_ntoa (sockaddr
.sin_addr
));
185 enable_async_notification (remote_desc
);
187 /* Register the event loop handler. */
188 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
190 /* We have a new GDB connection now. If we were disconnected
191 tracing, there's a window where the target could report a stop
192 event to the event loop, and since we have a connection now, we'd
193 try to send vStopped notifications to GDB. But, don't do that
194 until GDB as selected all-stop/non-stop, and has queried the
195 threads' status ('?'). */
201 /* Open a connection to a remote debugger.
202 NAME is the filename used for communication. */
205 remote_open (char *name
)
209 port_str
= strchr (name
, ':');
210 if (port_str
== NULL
)
213 error ("Only <host>:<port> is supported on this platform.");
217 if (stat (name
, &statbuf
) == 0
218 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
219 remote_desc
= open (name
, O_RDWR
);
227 perror_with_name ("Could not open remote device");
231 struct termios termios
;
232 tcgetattr (remote_desc
, &termios
);
237 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
238 termios
.c_cflag
|= CLOCAL
| CS8
;
239 termios
.c_cc
[VMIN
] = 1;
240 termios
.c_cc
[VTIME
] = 0;
242 tcsetattr (remote_desc
, TCSANOW
, &termios
);
248 struct termio termio
;
249 ioctl (remote_desc
, TCGETA
, &termio
);
254 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
255 termio
.c_cflag
|= CLOCAL
| CS8
;
256 termio
.c_cc
[VMIN
] = 1;
257 termio
.c_cc
[VTIME
] = 0;
259 ioctl (remote_desc
, TCSETA
, &termio
);
267 ioctl (remote_desc
, TIOCGETP
, &sg
);
269 ioctl (remote_desc
, TIOCSETP
, &sg
);
273 fprintf (stderr
, "Remote debugging using %s\n", name
);
275 transport_is_reliable
= 0;
277 enable_async_notification (remote_desc
);
279 /* Register the event loop handler. */
280 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
281 #endif /* USE_WIN32API */
286 static int winsock_initialized
;
289 struct sockaddr_in sockaddr
;
293 port
= strtoul (port_str
+ 1, &port_end
, 10);
294 if (port_str
[1] == '\0' || *port_end
!= '\0')
295 fatal ("Bad port argument: %s", name
);
298 if (!winsock_initialized
)
302 WSAStartup (MAKEWORD (1, 0), &wsad
);
303 winsock_initialized
= 1;
307 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
308 if (listen_desc
== -1)
309 perror_with_name ("Can't open socket");
311 /* Allow rapid reuse of this port. */
313 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
316 sockaddr
.sin_family
= PF_INET
;
317 sockaddr
.sin_port
= htons (port
);
318 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
320 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
321 || listen (listen_desc
, 1))
322 perror_with_name ("Can't bind address");
324 /* If port is zero, a random port will be selected, and the
325 fprintf below needs to know what port was selected. */
328 socklen_t len
= sizeof (sockaddr
);
329 if (getsockname (listen_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
330 || len
< sizeof (sockaddr
))
331 perror_with_name ("Can't determine port");
332 port
= ntohs (sockaddr
.sin_port
);
335 fprintf (stderr
, "Listening on port %d\n", port
);
338 /* Register the event loop handler. */
339 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
341 transport_is_reliable
= 1;
348 delete_file_handler (remote_desc
);
351 closesocket (remote_desc
);
355 remote_desc
= INVALID_DESCRIPTOR
;
360 /* Convert hex digit A to a number. */
365 if (a
>= '0' && a
<= '9')
367 else if (a
>= 'a' && a
<= 'f')
370 error ("Reply contains invalid hex digit");
374 static const char hexchars
[] = "0123456789abcdef";
377 ishex (int ch
, int *val
)
379 if ((ch
>= 'a') && (ch
<= 'f'))
381 *val
= ch
- 'a' + 10;
384 if ((ch
>= 'A') && (ch
<= 'F'))
386 *val
= ch
- 'A' + 10;
389 if ((ch
>= '0') && (ch
<= '9'))
398 unhexify (char *bin
, const char *hex
, int count
)
402 for (i
= 0; i
< count
; i
++)
404 if (hex
[0] == 0 || hex
[1] == 0)
406 /* Hex string is short, or of uneven length.
407 Return the count that has been converted so far. */
410 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
417 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
424 for (i
= 0; i
< len
; i
++)
428 addr
= addr
| (fromhex (ch
) & 0x0f);
434 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
439 while (*end
!= '\0' && *end
!= ';')
442 decode_address (addrp
, start
, end
- start
);
449 /* Convert number NIB to a hex digit. */
457 return 'a' + nib
- 10;
461 hexify (char *hex
, const char *bin
, int count
)
465 /* May use a length, or a nul-terminated string as input. */
467 count
= strlen (bin
);
469 for (i
= 0; i
< count
; i
++)
471 *hex
++ = tohex ((*bin
>> 4) & 0xf);
472 *hex
++ = tohex (*bin
++ & 0xf);
478 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
479 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
480 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
481 (which may be more than *OUT_LEN due to escape characters). The
482 total number of bytes in the output buffer will be at most
486 remote_escape_output (const gdb_byte
*buffer
, int len
,
487 gdb_byte
*out_buf
, int *out_len
,
490 int input_index
, output_index
;
493 for (input_index
= 0; input_index
< len
; input_index
++)
495 gdb_byte b
= buffer
[input_index
];
497 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
499 /* These must be escaped. */
500 if (output_index
+ 2 > out_maxlen
)
502 out_buf
[output_index
++] = '}';
503 out_buf
[output_index
++] = b
^ 0x20;
507 if (output_index
+ 1 > out_maxlen
)
509 out_buf
[output_index
++] = b
;
513 *out_len
= input_index
;
517 /* Convert BUFFER, escaped data LEN bytes long, into binary data
518 in OUT_BUF. Return the number of bytes written to OUT_BUF.
519 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
521 This function reverses remote_escape_output. It allows more
522 escaped characters than that function does, in particular because
523 '*' must be escaped to avoid the run-length encoding processing
524 in reading packets. */
527 remote_unescape_input (const gdb_byte
*buffer
, int len
,
528 gdb_byte
*out_buf
, int out_maxlen
)
530 int input_index
, output_index
;
535 for (input_index
= 0; input_index
< len
; input_index
++)
537 gdb_byte b
= buffer
[input_index
];
539 if (output_index
+ 1 > out_maxlen
)
540 error ("Received too much data from the target.");
544 out_buf
[output_index
++] = b
^ 0x20;
550 out_buf
[output_index
++] = b
;
554 error ("Unmatched escape character in target response.");
559 /* Look for a sequence of characters which can be run-length encoded.
560 If there are any, update *CSUM and *P. Otherwise, output the
561 single character. Return the number of characters consumed. */
564 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
568 /* Always output the character. */
572 /* Don't go past '~'. */
576 for (n
= 1; n
< remaining
; n
++)
577 if (buf
[n
] != buf
[0])
580 /* N is the index of the first character not the same as buf[0].
581 buf[0] is counted twice, so by decrementing N, we get the number
582 of characters the RLE sequence will replace. */
588 /* Skip the frame characters. The manual says to skip '+' and '-'
589 also, but there's no reason to. Unfortunately these two unusable
590 characters double the encoded length of a four byte zero
592 while (n
+ 29 == '$' || n
+ 29 == '#')
604 unpack_varlen_hex (char *buff
, /* packet to parse */
610 while (ishex (*buff
, &nibble
))
613 retval
= retval
<< 4;
614 retval
|= nibble
& 0x0f;
620 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
623 write_ptid (char *buf
, ptid_t ptid
)
629 pid
= ptid_get_pid (ptid
);
631 buf
+= sprintf (buf
, "p-%x.", -pid
);
633 buf
+= sprintf (buf
, "p%x.", pid
);
635 tid
= ptid_get_lwp (ptid
);
637 buf
+= sprintf (buf
, "-%x", -tid
);
639 buf
+= sprintf (buf
, "%x", tid
);
645 hex_or_minus_one (char *buf
, char **obuf
)
649 if (strncmp (buf
, "-1", 2) == 0)
655 buf
= unpack_varlen_hex (buf
, &ret
);
663 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
664 passed the last parsed char. Returns null_ptid on error. */
666 read_ptid (char *buf
, char **obuf
)
670 ULONGEST pid
= 0, tid
= 0;
674 /* Multi-process ptid. */
675 pp
= unpack_varlen_hex (p
+ 1, &pid
);
677 error ("invalid remote ptid: %s\n", p
);
681 tid
= hex_or_minus_one (p
, &pp
);
685 return ptid_build (pid
, tid
, 0);
688 /* No multi-process. Just a tid. */
689 tid
= hex_or_minus_one (p
, &pp
);
691 /* Since the stub is not sending a process id, then default to
692 what's in the current inferior. */
693 pid
= ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
697 return ptid_build (pid
, tid
, 0);
700 /* Send a packet to the remote machine, with error checking.
701 The data of the packet is in BUF, and the length of the
702 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
705 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
708 unsigned char csum
= 0;
713 buf2
= xmalloc (PBUFSIZ
);
715 /* Copy the packet into buffer BUF2, encapsulating it
716 and giving it a checksum. */
724 for (i
= 0; i
< cnt
;)
725 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
728 *p
++ = tohex ((csum
>> 4) & 0xf);
729 *p
++ = tohex (csum
& 0xf);
733 /* Send it over and over until we get a positive ack. */
737 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
739 perror ("putpkt(write)");
744 if (noack_mode
|| is_notif
)
746 /* Don't expect an ack then. */
750 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
752 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
760 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
774 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
778 /* Check for an input interrupt while we're here. */
779 if (cc
== '\003' && current_inferior
!= NULL
)
780 (*the_target
->request_interrupt
) ();
785 return 1; /* Success! */
789 putpkt_binary (char *buf
, int cnt
)
791 return putpkt_binary_1 (buf
, cnt
, 0);
794 /* Send a packet to the remote machine, with error checking. The data
795 of the packet is in BUF, and the packet should be a NUL-terminated
796 string. Returns >= 0 on success, -1 otherwise. */
801 return putpkt_binary (buf
, strlen (buf
));
805 putpkt_notif (char *buf
)
807 return putpkt_binary_1 (buf
, strlen (buf
), 1);
810 /* Come here when we get an input interrupt from the remote side. This
811 interrupt should only be active while we are waiting for the child to do
812 something. Thus this assumes readchar:bufcnt is 0.
813 About the only thing that should come through is a ^C, which
814 will cause us to request child interruption. */
817 input_interrupt (int unused
)
820 struct timeval immediate
= { 0, 0 };
822 /* Protect against spurious interrupts. This has been observed to
823 be a problem under NetBSD 1.4 and 1.5. */
826 FD_SET (remote_desc
, &readset
);
827 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
832 cc
= read (remote_desc
, &c
, 1);
834 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
836 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
841 (*the_target
->request_interrupt
) ();
845 /* Check if the remote side sent us an interrupt request (^C). */
847 check_remote_input_interrupt_request (void)
849 /* This function may be called before establishing communications,
850 therefore we need to validate the remote descriptor. */
852 if (remote_desc
== INVALID_DESCRIPTOR
)
858 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
859 accept Control-C from the client, and must be disabled when talking to
863 unblock_async_io (void)
868 sigemptyset (&sigio_set
);
869 sigaddset (&sigio_set
, SIGIO
);
870 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
876 nto_comctrl (int enable
)
878 struct sigevent event
;
882 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
883 event
.sigev_signo
= SIGIO
;
884 event
.sigev_code
= 0;
885 event
.sigev_value
.sival_ptr
= NULL
;
886 event
.sigev_priority
= -1;
887 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
891 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
896 /* Current state of asynchronous I/O. */
897 static int async_io_enabled
;
899 /* Enable asynchronous I/O. */
901 enable_async_io (void)
903 if (async_io_enabled
)
907 signal (SIGIO
, input_interrupt
);
909 async_io_enabled
= 1;
915 /* Disable asynchronous I/O. */
917 disable_async_io (void)
919 if (!async_io_enabled
)
923 signal (SIGIO
, SIG_IGN
);
925 async_io_enabled
= 0;
933 initialize_async_io (void)
935 /* Make sure that async I/O starts disabled. */
936 async_io_enabled
= 1;
939 /* Make sure the signal is unblocked. */
943 /* Internal buffer used by readchar.
944 These are global to readchar because reschedule_remote needs to be
945 able to tell whether the buffer is empty. */
947 static unsigned char readchar_buf
[BUFSIZ
];
948 static int readchar_bufcnt
= 0;
949 static unsigned char *readchar_bufp
;
951 /* Returns next char from remote GDB. -1 if error. */
958 if (readchar_bufcnt
== 0)
960 readchar_bufcnt
= read (remote_desc
, readchar_buf
, sizeof (readchar_buf
));
962 if (readchar_bufcnt
<= 0)
964 if (readchar_bufcnt
== 0)
965 fprintf (stderr
, "readchar: Got EOF\n");
972 readchar_bufp
= readchar_buf
;
976 ch
= *readchar_bufp
++;
981 /* Reset the readchar state machine. */
984 reset_readchar (void)
987 if (readchar_callback
!= NOT_SCHEDULED
)
989 delete_callback_event (readchar_callback
);
990 readchar_callback
= NOT_SCHEDULED
;
994 /* Process remaining data in readchar_buf. */
997 process_remaining (void *context
)
1001 /* This is a one-shot event. */
1002 readchar_callback
= NOT_SCHEDULED
;
1004 if (readchar_bufcnt
> 0)
1005 res
= handle_serial_event (0, NULL
);
1012 /* If there is still data in the buffer, queue another event to process it,
1013 we can't sleep in select yet. */
1018 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
1019 readchar_callback
= append_callback_event (process_remaining
, NULL
);
1022 /* Read a packet from the remote machine, with error checking,
1023 and store it in BUF. Returns length of packet, or negative if error. */
1029 unsigned char csum
, c1
, c2
;
1043 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
1064 c1
= fromhex (readchar ());
1065 c2
= fromhex (readchar ());
1067 if (csum
== (c1
<< 4) + c2
)
1072 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
1073 (c1
<< 4) + c2
, csum
, buf
);
1074 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1078 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1079 (c1
<< 4) + c2
, csum
, buf
);
1080 write (remote_desc
, "-", 1);
1087 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1091 write (remote_desc
, "+", 1);
1095 fprintf (stderr
, "[sent ack]\n");
1103 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1112 write_ok (char *buf
)
1120 write_enn (char *buf
)
1122 /* Some day, we should define the meanings of the error codes... */
1130 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
1137 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
1138 *to
++ = tohex (nib
);
1140 *to
++ = tohex (nib
);
1147 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
1152 nib1
= fromhex (*from
++);
1153 nib2
= fromhex (*from
++);
1154 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
1159 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1161 if ((regno
>> 12) != 0)
1162 *buf
++ = tohex ((regno
>> 12) & 0xf);
1163 if ((regno
>> 8) != 0)
1164 *buf
++ = tohex ((regno
>> 8) & 0xf);
1165 *buf
++ = tohex ((regno
>> 4) & 0xf);
1166 *buf
++ = tohex (regno
& 0xf);
1168 collect_register_as_string (regcache
, regno
, buf
);
1169 buf
+= 2 * register_size (regno
);
1176 new_thread_notify (int id
)
1180 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1184 if (server_waiting
== 0)
1187 sprintf (own_buf
, "n%x", id
);
1188 disable_async_io ();
1194 dead_thread_notify (int id
)
1198 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1202 sprintf (own_buf
, "x%x", id
);
1203 disable_async_io ();
1209 prepare_resume_reply (char *buf
, ptid_t ptid
,
1210 struct target_waitstatus
*status
)
1213 fprintf (stderr
, "Writing resume reply for %s:%d\n\n",
1214 target_pid_to_str (ptid
), status
->kind
);
1216 switch (status
->kind
)
1218 case TARGET_WAITKIND_STOPPED
:
1220 struct thread_info
*saved_inferior
;
1222 struct regcache
*regcache
;
1224 sprintf (buf
, "T%02x", status
->value
.sig
);
1225 buf
+= strlen (buf
);
1227 regp
= gdbserver_expedite_regs
;
1229 saved_inferior
= current_inferior
;
1231 current_inferior
= find_thread_ptid (ptid
);
1233 regcache
= get_thread_regcache (current_inferior
, 1);
1235 if (the_target
->stopped_by_watchpoint
!= NULL
1236 && (*the_target
->stopped_by_watchpoint
) ())
1241 strncpy (buf
, "watch:", 6);
1244 addr
= (*the_target
->stopped_data_address
) ();
1246 /* Convert each byte of the address into two hexadecimal
1247 chars. Note that we take sizeof (void *) instead of
1248 sizeof (addr); this is to avoid sending a 64-bit
1249 address to a 32-bit GDB. */
1250 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1251 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1257 buf
= outreg (regcache
, find_regno (*regp
), buf
);
1262 /* Formerly, if the debugger had not used any thread features
1263 we would not burden it with a thread status response. This
1264 was for the benefit of GDB 4.13 and older. However, in
1265 recent GDB versions the check (``if (cont_thread != 0)'')
1266 does not have the desired effect because of sillyness in
1267 the way that the remote protocol handles specifying a
1268 thread. Since thread support relies on qSymbol support
1269 anyway, assume GDB can handle threads. */
1271 if (using_threads
&& !disable_packet_Tthread
)
1273 /* This if (1) ought to be unnecessary. But remote_wait
1274 in GDB will claim this event belongs to inferior_ptid
1275 if we do not specify a thread, and there's no way for
1276 gdbserver to know what inferior_ptid is. */
1277 if (1 || !ptid_equal (general_thread
, ptid
))
1280 /* In non-stop, don't change the general thread behind
1283 general_thread
= ptid
;
1284 sprintf (buf
, "thread:");
1285 buf
+= strlen (buf
);
1286 buf
= write_ptid (buf
, ptid
);
1288 buf
+= strlen (buf
);
1290 if (the_target
->core_of_thread
)
1291 core
= (*the_target
->core_of_thread
) (ptid
);
1294 sprintf (buf
, "core:");
1295 buf
+= strlen (buf
);
1296 sprintf (buf
, "%x", core
);
1298 buf
+= strlen (buf
);
1305 strcpy (buf
, "library:;");
1306 buf
+= strlen (buf
);
1310 current_inferior
= saved_inferior
;
1313 case TARGET_WAITKIND_EXITED
:
1315 sprintf (buf
, "W%x;process:%x",
1316 status
->value
.integer
, ptid_get_pid (ptid
));
1318 sprintf (buf
, "W%02x", status
->value
.integer
);
1320 case TARGET_WAITKIND_SIGNALLED
:
1322 sprintf (buf
, "X%x;process:%x",
1323 status
->value
.sig
, ptid_get_pid (ptid
));
1325 sprintf (buf
, "X%02x", status
->value
.sig
);
1328 error ("unhandled waitkind");
1334 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1338 *mem_addr_ptr
= *len_ptr
= 0;
1340 while ((ch
= from
[i
++]) != ',')
1342 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1343 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1346 for (j
= 0; j
< 4; j
++)
1348 if ((ch
= from
[i
++]) == 0)
1350 *len_ptr
= *len_ptr
<< 4;
1351 *len_ptr
|= fromhex (ch
) & 0x0f;
1356 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1361 *mem_addr_ptr
= *len_ptr
= 0;
1363 while ((ch
= from
[i
++]) != ',')
1365 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1366 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1369 while ((ch
= from
[i
++]) != ':')
1371 *len_ptr
= *len_ptr
<< 4;
1372 *len_ptr
|= fromhex (ch
) & 0x0f;
1375 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1379 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1380 unsigned int *len_ptr
, unsigned char *to
)
1384 *mem_addr_ptr
= *len_ptr
= 0;
1386 while ((ch
= from
[i
++]) != ',')
1388 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1389 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1392 while ((ch
= from
[i
++]) != ':')
1394 *len_ptr
= *len_ptr
<< 4;
1395 *len_ptr
|= fromhex (ch
) & 0x0f;
1398 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1399 to
, *len_ptr
) != *len_ptr
)
1405 /* Decode a qXfer write request. */
1407 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1408 unsigned int *len
, unsigned char *data
)
1412 /* Extract and NUL-terminate the annex. */
1414 while (*buf
&& *buf
!= ':')
1420 /* Extract the offset. */
1422 while ((ch
= *buf
++) != ':')
1424 *offset
= *offset
<< 4;
1425 *offset
|= fromhex (ch
) & 0x0f;
1428 /* Get encoded data. */
1429 packet_len
-= buf
- *annex
;
1430 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1435 /* Decode the parameters of a qSearch:memory packet. */
1438 decode_search_memory_packet (const char *buf
, int packet_len
,
1439 CORE_ADDR
*start_addrp
,
1440 CORE_ADDR
*search_space_lenp
,
1441 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1443 const char *p
= buf
;
1445 p
= decode_address_to_semicolon (start_addrp
, p
);
1446 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1447 packet_len
-= p
- buf
;
1448 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1449 pattern
, packet_len
);
1454 free_sym_cache (struct sym_cache
*sym
)
1464 clear_symbol_cache (struct sym_cache
**symcache_p
)
1466 struct sym_cache
*sym
, *next
;
1468 /* Check the cache first. */
1469 for (sym
= *symcache_p
; sym
; sym
= next
)
1472 free_sym_cache (sym
);
1478 /* Get the address of NAME, and return it in ADDRP if found. if
1479 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1480 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1483 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1485 char own_buf
[266], *p
, *q
;
1487 struct sym_cache
*sym
;
1488 struct process_info
*proc
;
1490 proc
= current_process ();
1492 /* Check the cache first. */
1493 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1494 if (strcmp (name
, sym
->name
) == 0)
1500 /* It might not be an appropriate time to look up a symbol,
1501 e.g. while we're trying to fetch registers. */
1505 /* Send the request. */
1506 strcpy (own_buf
, "qSymbol:");
1507 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1508 if (putpkt (own_buf
) < 0)
1511 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1512 len
= getpkt (own_buf
);
1516 /* We ought to handle pretty much any packet at this point while we
1517 wait for the qSymbol "response". That requires re-entering the
1518 main loop. For now, this is an adequate approximation; allow
1519 GDB to read from memory while it figures out the address of the
1521 while (own_buf
[0] == 'm')
1524 unsigned char *mem_buf
;
1525 unsigned int mem_len
;
1527 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1528 mem_buf
= xmalloc (mem_len
);
1529 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1530 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1532 write_enn (own_buf
);
1534 if (putpkt (own_buf
) < 0)
1536 len
= getpkt (own_buf
);
1541 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1543 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1547 p
= own_buf
+ strlen ("qSymbol:");
1549 while (*q
&& *q
!= ':')
1552 /* Make sure we found a value for the symbol. */
1553 if (p
== q
|| *q
== '\0')
1556 decode_address (addrp
, p
, q
- p
);
1558 /* Save the symbol in our cache. */
1559 sym
= xmalloc (sizeof (*sym
));
1560 sym
->name
= xstrdup (name
);
1562 sym
->next
= proc
->symbol_cache
;
1563 proc
->symbol_cache
= sym
;
1569 monitor_output (const char *msg
)
1571 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1574 hexify (buf
+ 1, msg
, 0);
1580 /* Return a malloc allocated string with special characters from TEXT
1581 replaced by entity references. */
1584 xml_escape_text (const char *text
)
1589 /* Compute the length of the result. */
1590 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1608 /* Expand the result. */
1609 result
= xmalloc (i
+ special
+ 1);
1610 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1614 strcpy (result
+ i
+ special
, "'");
1618 strcpy (result
+ i
+ special
, """);
1622 strcpy (result
+ i
+ special
, "&");
1626 strcpy (result
+ i
+ special
, "<");
1630 strcpy (result
+ i
+ special
, ">");
1634 result
[i
+ special
] = text
[i
];
1637 result
[i
+ special
] = '\0';
1643 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1646 size_t new_buffer_size
;
1651 new_buffer_size
= buffer
->buffer_size
;
1653 if (new_buffer_size
== 0)
1654 new_buffer_size
= 1;
1656 while (buffer
->used_size
+ size
> new_buffer_size
)
1657 new_buffer_size
*= 2;
1658 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1661 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1662 buffer
->buffer
= new_buffer
;
1663 buffer
->buffer_size
= new_buffer_size
;
1664 buffer
->used_size
+= size
;
1668 buffer_free (struct buffer
*buffer
)
1673 free (buffer
->buffer
);
1674 buffer
->buffer
= NULL
;
1675 buffer
->buffer_size
= 0;
1676 buffer
->used_size
= 0;
1680 buffer_init (struct buffer
*buffer
)
1682 memset (buffer
, 0, sizeof (*buffer
));
1686 buffer_finish (struct buffer
*buffer
)
1688 char *ret
= buffer
->buffer
;
1689 buffer
->buffer
= NULL
;
1690 buffer
->buffer_size
= 0;
1691 buffer
->used_size
= 0;
1696 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1703 va_start (ap
, format
);
1706 for (f
= format
; *f
; f
++)
1715 char *a
= va_arg (ap
, char *);
1716 buffer_grow (buffer
, prev
, f
- prev
- 1);
1717 p
= xml_escape_text (a
);
1718 buffer_grow_str (buffer
, p
);
1725 int i
= va_arg (ap
, int);
1726 char b
[sizeof ("4294967295")];
1728 buffer_grow (buffer
, prev
, f
- prev
- 1);
1729 sprintf (b
, "%d", i
);
1730 buffer_grow_str (buffer
, b
);
1740 buffer_grow_str (buffer
, prev
);