1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
30 #include <sys/ioctl.h>
36 #include <netinet/in.h>
39 #include <sys/socket.h>
44 #if HAVE_NETINET_TCP_H
45 #include <netinet/tcp.h>
48 #include <sys/ioctl.h>
59 #include <arpa/inet.h>
71 #include <sys/iomgr.h>
74 #ifndef HAVE_SOCKLEN_T
75 typedef int socklen_t
;
78 #ifndef IN_PROCESS_AGENT
81 # define INVALID_DESCRIPTOR INVALID_SOCKET
83 # define INVALID_DESCRIPTOR -1
86 /* Extra value for readchar_callback. */
88 /* The callback is currently not scheduled. */
92 /* Status of the readchar callback.
93 Either NOT_SCHEDULED or the callback id. */
94 static int readchar_callback
= NOT_SCHEDULED
;
96 static int readchar (void);
97 static void reset_readchar (void);
98 static void reschedule (void);
100 /* A cache entry for a successfully looked-up symbol. */
105 struct sym_cache
*next
;
108 int remote_debug
= 0;
109 struct ui_file
*gdb_stdlog
;
111 static int remote_is_stdio
= 0;
113 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
114 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
116 /* FIXME headerize? */
117 extern int using_threads
;
118 extern int debug_threads
;
120 /* If true, then GDB has requested noack mode. */
122 /* If true, then we tell GDB to use noack mode by default. */
123 int transport_is_reliable
= 0;
126 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
127 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
133 return remote_desc
!= INVALID_DESCRIPTOR
;
136 /* Return true if the remote connection is over stdio. */
139 remote_connection_is_stdio (void)
141 return remote_is_stdio
;
145 enable_async_notification (int fd
)
147 #if defined(F_SETFL) && defined (FASYNC)
148 int save_fcntl_flags
;
150 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
151 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
152 #if defined (F_SETOWN)
153 fcntl (fd
, F_SETOWN
, getpid ());
159 handle_accept_event (int err
, gdb_client_data client_data
)
161 struct sockaddr_in sockaddr
;
165 debug_printf ("handling possible accept event\n");
167 tmp
= sizeof (sockaddr
);
168 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
169 if (remote_desc
== -1)
170 perror_with_name ("Accept failed");
172 /* Enable TCP keep alive process. */
174 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
175 (char *) &tmp
, sizeof (tmp
));
177 /* Tell TCP not to delay small packets. This greatly speeds up
178 interactive response. */
180 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
181 (char *) &tmp
, sizeof (tmp
));
184 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
185 exits when the remote side dies. */
191 close (listen_desc
); /* No longer need this */
193 closesocket (listen_desc
); /* No longer need this */
197 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
198 descriptor open for add_file_handler to wait for a new connection. */
199 delete_file_handler (listen_desc
);
201 /* Convert IP address to string. */
202 fprintf (stderr
, "Remote debugging from host %s\n",
203 inet_ntoa (sockaddr
.sin_addr
));
205 enable_async_notification (remote_desc
);
207 /* Register the event loop handler. */
208 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
210 /* We have a new GDB connection now. If we were disconnected
211 tracing, there's a window where the target could report a stop
212 event to the event loop, and since we have a connection now, we'd
213 try to send vStopped notifications to GDB. But, don't do that
214 until GDB as selected all-stop/non-stop, and has queried the
215 threads' status ('?'). */
221 /* Prepare for a later connection to a remote debugger.
222 NAME is the filename used for communication. */
225 remote_prepare (char *name
)
229 static int winsock_initialized
;
232 struct sockaddr_in sockaddr
;
237 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
239 /* We need to record fact that we're using stdio sooner than the
240 call to remote_open so start_inferior knows the connection is
243 transport_is_reliable
= 1;
247 port_str
= strchr (name
, ':');
248 if (port_str
== NULL
)
250 transport_is_reliable
= 0;
254 port
= strtoul (port_str
+ 1, &port_end
, 10);
255 if (port_str
[1] == '\0' || *port_end
!= '\0')
256 fatal ("Bad port argument: %s", name
);
259 if (!winsock_initialized
)
263 WSAStartup (MAKEWORD (1, 0), &wsad
);
264 winsock_initialized
= 1;
268 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
269 if (listen_desc
== -1)
270 perror_with_name ("Can't open socket");
272 /* Allow rapid reuse of this port. */
274 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
277 sockaddr
.sin_family
= PF_INET
;
278 sockaddr
.sin_port
= htons (port
);
279 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
281 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
282 || listen (listen_desc
, 1))
283 perror_with_name ("Can't bind address");
285 transport_is_reliable
= 1;
288 /* Open a connection to a remote debugger.
289 NAME is the filename used for communication. */
292 remote_open (char *name
)
296 port_str
= strchr (name
, ':');
298 if (port_str
== NULL
)
299 error ("Only <host>:<port> is supported on this platform.");
302 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
304 fprintf (stderr
, "Remote debugging using stdio\n");
306 /* Use stdin as the handle of the connection.
307 We only select on reads, for example. */
308 remote_desc
= fileno (stdin
);
310 enable_async_notification (remote_desc
);
312 /* Register the event loop handler. */
313 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
316 else if (port_str
== NULL
)
320 if (stat (name
, &statbuf
) == 0
321 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
322 remote_desc
= open (name
, O_RDWR
);
330 perror_with_name ("Could not open remote device");
334 struct termios termios
;
335 tcgetattr (remote_desc
, &termios
);
340 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
341 termios
.c_cflag
|= CLOCAL
| CS8
;
342 termios
.c_cc
[VMIN
] = 1;
343 termios
.c_cc
[VTIME
] = 0;
345 tcsetattr (remote_desc
, TCSANOW
, &termios
);
351 struct termio termio
;
352 ioctl (remote_desc
, TCGETA
, &termio
);
357 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
358 termio
.c_cflag
|= CLOCAL
| CS8
;
359 termio
.c_cc
[VMIN
] = 1;
360 termio
.c_cc
[VTIME
] = 0;
362 ioctl (remote_desc
, TCSETA
, &termio
);
370 ioctl (remote_desc
, TIOCGETP
, &sg
);
372 ioctl (remote_desc
, TIOCSETP
, &sg
);
376 fprintf (stderr
, "Remote debugging using %s\n", name
);
378 enable_async_notification (remote_desc
);
380 /* Register the event loop handler. */
381 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
383 #endif /* USE_WIN32API */
388 struct sockaddr_in sockaddr
;
390 len
= sizeof (sockaddr
);
391 if (getsockname (listen_desc
,
392 (struct sockaddr
*) &sockaddr
, &len
) < 0
393 || len
< sizeof (sockaddr
))
394 perror_with_name ("Can't determine port");
395 port
= ntohs (sockaddr
.sin_port
);
397 fprintf (stderr
, "Listening on port %d\n", port
);
400 /* Register the event loop handler. */
401 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
408 delete_file_handler (remote_desc
);
411 closesocket (remote_desc
);
413 if (! remote_connection_is_stdio ())
416 remote_desc
= INVALID_DESCRIPTOR
;
423 #ifndef IN_PROCESS_AGENT
426 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
433 for (i
= 0; i
< len
; i
++)
437 addr
= addr
| (fromhex (ch
) & 0x0f);
443 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
448 while (*end
!= '\0' && *end
!= ';')
451 decode_address (addrp
, start
, end
- start
);
460 #ifndef IN_PROCESS_AGENT
462 /* Look for a sequence of characters which can be run-length encoded.
463 If there are any, update *CSUM and *P. Otherwise, output the
464 single character. Return the number of characters consumed. */
467 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
471 /* Always output the character. */
475 /* Don't go past '~'. */
479 for (n
= 1; n
< remaining
; n
++)
480 if (buf
[n
] != buf
[0])
483 /* N is the index of the first character not the same as buf[0].
484 buf[0] is counted twice, so by decrementing N, we get the number
485 of characters the RLE sequence will replace. */
491 /* Skip the frame characters. The manual says to skip '+' and '-'
492 also, but there's no reason to. Unfortunately these two unusable
493 characters double the encoded length of a four byte zero
495 while (n
+ 29 == '$' || n
+ 29 == '#')
508 #ifndef IN_PROCESS_AGENT
510 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
513 write_ptid (char *buf
, ptid_t ptid
)
519 pid
= ptid_get_pid (ptid
);
521 buf
+= sprintf (buf
, "p-%x.", -pid
);
523 buf
+= sprintf (buf
, "p%x.", pid
);
525 tid
= ptid_get_lwp (ptid
);
527 buf
+= sprintf (buf
, "-%x", -tid
);
529 buf
+= sprintf (buf
, "%x", tid
);
535 hex_or_minus_one (char *buf
, char **obuf
)
539 if (strncmp (buf
, "-1", 2) == 0)
545 buf
= unpack_varlen_hex (buf
, &ret
);
553 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
554 passed the last parsed char. Returns null_ptid on error. */
556 read_ptid (char *buf
, char **obuf
)
560 ULONGEST pid
= 0, tid
= 0;
564 /* Multi-process ptid. */
565 pp
= unpack_varlen_hex (p
+ 1, &pid
);
567 error ("invalid remote ptid: %s\n", p
);
571 tid
= hex_or_minus_one (p
, &pp
);
575 return ptid_build (pid
, tid
, 0);
578 /* No multi-process. Just a tid. */
579 tid
= hex_or_minus_one (p
, &pp
);
581 /* Since the stub is not sending a process id, then default to
582 what's in the current inferior. */
583 pid
= ptid_get_pid (current_ptid
);
587 return ptid_build (pid
, tid
, 0);
590 /* Write COUNT bytes in BUF to the client.
591 The result is the number of bytes written or -1 if error.
592 This may return less than COUNT. */
595 write_prim (const void *buf
, int count
)
597 if (remote_connection_is_stdio ())
598 return write (fileno (stdout
), buf
, count
);
600 return write (remote_desc
, buf
, count
);
603 /* Read COUNT bytes from the client and store in BUF.
604 The result is the number of bytes read or -1 if error.
605 This may return less than COUNT. */
608 read_prim (void *buf
, int count
)
610 if (remote_connection_is_stdio ())
611 return read (fileno (stdin
), buf
, count
);
613 return read (remote_desc
, buf
, count
);
616 /* Send a packet to the remote machine, with error checking.
617 The data of the packet is in BUF, and the length of the
618 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
621 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
624 unsigned char csum
= 0;
629 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
631 /* Copy the packet into buffer BUF2, encapsulating it
632 and giving it a checksum. */
640 for (i
= 0; i
< cnt
;)
641 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
644 *p
++ = tohex ((csum
>> 4) & 0xf);
645 *p
++ = tohex (csum
& 0xf);
649 /* Send it over and over until we get a positive ack. */
653 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
655 perror ("putpkt(write)");
660 if (noack_mode
|| is_notif
)
662 /* Don't expect an ack then. */
666 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
668 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
676 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
690 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
694 /* Check for an input interrupt while we're here. */
695 if (cc
== '\003' && current_inferior
!= NULL
)
696 (*the_target
->request_interrupt
) ();
701 return 1; /* Success! */
705 putpkt_binary (char *buf
, int cnt
)
707 return putpkt_binary_1 (buf
, cnt
, 0);
710 /* Send a packet to the remote machine, with error checking. The data
711 of the packet is in BUF, and the packet should be a NUL-terminated
712 string. Returns >= 0 on success, -1 otherwise. */
717 return putpkt_binary (buf
, strlen (buf
));
721 putpkt_notif (char *buf
)
723 return putpkt_binary_1 (buf
, strlen (buf
), 1);
726 /* Come here when we get an input interrupt from the remote side. This
727 interrupt should only be active while we are waiting for the child to do
728 something. Thus this assumes readchar:bufcnt is 0.
729 About the only thing that should come through is a ^C, which
730 will cause us to request child interruption. */
733 input_interrupt (int unused
)
736 struct timeval immediate
= { 0, 0 };
738 /* Protect against spurious interrupts. This has been observed to
739 be a problem under NetBSD 1.4 and 1.5. */
742 FD_SET (remote_desc
, &readset
);
743 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
748 cc
= read_prim (&c
, 1);
750 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
752 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
757 (*the_target
->request_interrupt
) ();
761 /* Check if the remote side sent us an interrupt request (^C). */
763 check_remote_input_interrupt_request (void)
765 /* This function may be called before establishing communications,
766 therefore we need to validate the remote descriptor. */
768 if (remote_desc
== INVALID_DESCRIPTOR
)
774 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
775 accept Control-C from the client, and must be disabled when talking to
779 unblock_async_io (void)
784 sigemptyset (&sigio_set
);
785 sigaddset (&sigio_set
, SIGIO
);
786 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
792 nto_comctrl (int enable
)
794 struct sigevent event
;
798 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
799 event
.sigev_signo
= SIGIO
;
800 event
.sigev_code
= 0;
801 event
.sigev_value
.sival_ptr
= NULL
;
802 event
.sigev_priority
= -1;
803 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
807 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
812 /* Current state of asynchronous I/O. */
813 static int async_io_enabled
;
815 /* Enable asynchronous I/O. */
817 enable_async_io (void)
819 if (async_io_enabled
)
823 signal (SIGIO
, input_interrupt
);
825 async_io_enabled
= 1;
831 /* Disable asynchronous I/O. */
833 disable_async_io (void)
835 if (!async_io_enabled
)
839 signal (SIGIO
, SIG_IGN
);
841 async_io_enabled
= 0;
849 initialize_async_io (void)
851 /* Make sure that async I/O starts disabled. */
852 async_io_enabled
= 1;
855 /* Make sure the signal is unblocked. */
859 /* Internal buffer used by readchar.
860 These are global to readchar because reschedule_remote needs to be
861 able to tell whether the buffer is empty. */
863 static unsigned char readchar_buf
[BUFSIZ
];
864 static int readchar_bufcnt
= 0;
865 static unsigned char *readchar_bufp
;
867 /* Returns next char from remote GDB. -1 if error. */
874 if (readchar_bufcnt
== 0)
876 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
878 if (readchar_bufcnt
<= 0)
880 if (readchar_bufcnt
== 0)
881 fprintf (stderr
, "readchar: Got EOF\n");
888 readchar_bufp
= readchar_buf
;
892 ch
= *readchar_bufp
++;
897 /* Reset the readchar state machine. */
900 reset_readchar (void)
903 if (readchar_callback
!= NOT_SCHEDULED
)
905 delete_callback_event (readchar_callback
);
906 readchar_callback
= NOT_SCHEDULED
;
910 /* Process remaining data in readchar_buf. */
913 process_remaining (void *context
)
917 /* This is a one-shot event. */
918 readchar_callback
= NOT_SCHEDULED
;
920 if (readchar_bufcnt
> 0)
921 res
= handle_serial_event (0, NULL
);
928 /* If there is still data in the buffer, queue another event to process it,
929 we can't sleep in select yet. */
934 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
935 readchar_callback
= append_callback_event (process_remaining
, NULL
);
938 /* Read a packet from the remote machine, with error checking,
939 and store it in BUF. Returns length of packet, or negative if error. */
945 unsigned char csum
, c1
, c2
;
959 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
980 c1
= fromhex (readchar ());
981 c2
= fromhex (readchar ());
983 if (csum
== (c1
<< 4) + c2
)
989 "Bad checksum, sentsum=0x%x, csum=0x%x, "
990 "buf=%s [no-ack-mode, Bad medium?]\n",
991 (c1
<< 4) + c2
, csum
, buf
);
992 /* Not much we can do, GDB wasn't expecting an ack/nac. */
996 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
997 (c1
<< 4) + c2
, csum
, buf
);
998 if (write_prim ("-", 1) != 1)
1006 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1010 if (write_prim ("+", 1) != 1)
1015 fprintf (stderr
, "[sent ack]\n");
1023 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1032 write_ok (char *buf
)
1040 write_enn (char *buf
)
1042 /* Some day, we should define the meanings of the error codes... */
1051 #ifndef IN_PROCESS_AGENT
1054 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1056 if ((regno
>> 12) != 0)
1057 *buf
++ = tohex ((regno
>> 12) & 0xf);
1058 if ((regno
>> 8) != 0)
1059 *buf
++ = tohex ((regno
>> 8) & 0xf);
1060 *buf
++ = tohex ((regno
>> 4) & 0xf);
1061 *buf
++ = tohex (regno
& 0xf);
1063 collect_register_as_string (regcache
, regno
, buf
);
1064 buf
+= 2 * register_size (regcache
->tdesc
, regno
);
1071 new_thread_notify (int id
)
1075 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1079 if (server_waiting
== 0)
1082 sprintf (own_buf
, "n%x", id
);
1083 disable_async_io ();
1089 dead_thread_notify (int id
)
1093 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1097 sprintf (own_buf
, "x%x", id
);
1098 disable_async_io ();
1104 prepare_resume_reply (char *buf
, ptid_t ptid
,
1105 struct target_waitstatus
*status
)
1108 debug_printf ("Writing resume reply for %s:%d\n",
1109 target_pid_to_str (ptid
), status
->kind
);
1111 switch (status
->kind
)
1113 case TARGET_WAITKIND_STOPPED
:
1115 struct thread_info
*saved_inferior
;
1117 struct regcache
*regcache
;
1119 sprintf (buf
, "T%02x", status
->value
.sig
);
1120 buf
+= strlen (buf
);
1122 saved_inferior
= current_inferior
;
1124 current_inferior
= find_thread_ptid (ptid
);
1126 regp
= current_target_desc ()->expedite_regs
;
1128 regcache
= get_thread_regcache (current_inferior
, 1);
1130 if (the_target
->stopped_by_watchpoint
!= NULL
1131 && (*the_target
->stopped_by_watchpoint
) ())
1136 strncpy (buf
, "watch:", 6);
1139 addr
= (*the_target
->stopped_data_address
) ();
1141 /* Convert each byte of the address into two hexadecimal
1142 chars. Note that we take sizeof (void *) instead of
1143 sizeof (addr); this is to avoid sending a 64-bit
1144 address to a 32-bit GDB. */
1145 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1146 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1152 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1157 /* Formerly, if the debugger had not used any thread features
1158 we would not burden it with a thread status response. This
1159 was for the benefit of GDB 4.13 and older. However, in
1160 recent GDB versions the check (``if (cont_thread != 0)'')
1161 does not have the desired effect because of sillyness in
1162 the way that the remote protocol handles specifying a
1163 thread. Since thread support relies on qSymbol support
1164 anyway, assume GDB can handle threads. */
1166 if (using_threads
&& !disable_packet_Tthread
)
1168 /* This if (1) ought to be unnecessary. But remote_wait
1169 in GDB will claim this event belongs to inferior_ptid
1170 if we do not specify a thread, and there's no way for
1171 gdbserver to know what inferior_ptid is. */
1172 if (1 || !ptid_equal (general_thread
, ptid
))
1175 /* In non-stop, don't change the general thread behind
1178 general_thread
= ptid
;
1179 sprintf (buf
, "thread:");
1180 buf
+= strlen (buf
);
1181 buf
= write_ptid (buf
, ptid
);
1183 buf
+= strlen (buf
);
1185 core
= target_core_of_thread (ptid
);
1189 sprintf (buf
, "core:");
1190 buf
+= strlen (buf
);
1191 sprintf (buf
, "%x", core
);
1193 buf
+= strlen (buf
);
1200 strcpy (buf
, "library:;");
1201 buf
+= strlen (buf
);
1205 current_inferior
= saved_inferior
;
1208 case TARGET_WAITKIND_EXITED
:
1210 sprintf (buf
, "W%x;process:%x",
1211 status
->value
.integer
, ptid_get_pid (ptid
));
1213 sprintf (buf
, "W%02x", status
->value
.integer
);
1215 case TARGET_WAITKIND_SIGNALLED
:
1217 sprintf (buf
, "X%x;process:%x",
1218 status
->value
.sig
, ptid_get_pid (ptid
));
1220 sprintf (buf
, "X%02x", status
->value
.sig
);
1223 error ("unhandled waitkind");
1229 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
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 for (j
= 0; j
< 4; j
++)
1243 if ((ch
= from
[i
++]) == 0)
1245 *len_ptr
= *len_ptr
<< 4;
1246 *len_ptr
|= fromhex (ch
) & 0x0f;
1251 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1252 unsigned char **to_p
)
1256 *mem_addr_ptr
= *len_ptr
= 0;
1258 while ((ch
= from
[i
++]) != ',')
1260 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1261 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1264 while ((ch
= from
[i
++]) != ':')
1266 *len_ptr
= *len_ptr
<< 4;
1267 *len_ptr
|= fromhex (ch
) & 0x0f;
1271 *to_p
= xmalloc (*len_ptr
);
1273 convert_ascii_to_int (&from
[i
++], *to_p
, *len_ptr
);
1277 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1278 unsigned int *len_ptr
, unsigned char **to_p
)
1282 *mem_addr_ptr
= *len_ptr
= 0;
1284 while ((ch
= from
[i
++]) != ',')
1286 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1287 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1290 while ((ch
= from
[i
++]) != ':')
1292 *len_ptr
= *len_ptr
<< 4;
1293 *len_ptr
|= fromhex (ch
) & 0x0f;
1297 *to_p
= xmalloc (*len_ptr
);
1299 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1300 *to_p
, *len_ptr
) != *len_ptr
)
1306 /* Decode a qXfer write request. */
1309 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1310 unsigned int *len
, unsigned char *data
)
1315 /* Extract the offset. */
1317 while ((ch
= *buf
++) != ':')
1319 *offset
= *offset
<< 4;
1320 *offset
|= fromhex (ch
) & 0x0f;
1323 /* Get encoded data. */
1324 packet_len
-= buf
- b
;
1325 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1330 /* Decode the parameters of a qSearch:memory packet. */
1333 decode_search_memory_packet (const char *buf
, int packet_len
,
1334 CORE_ADDR
*start_addrp
,
1335 CORE_ADDR
*search_space_lenp
,
1336 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1338 const char *p
= buf
;
1340 p
= decode_address_to_semicolon (start_addrp
, p
);
1341 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1342 packet_len
-= p
- buf
;
1343 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1344 pattern
, packet_len
);
1349 free_sym_cache (struct sym_cache
*sym
)
1359 clear_symbol_cache (struct sym_cache
**symcache_p
)
1361 struct sym_cache
*sym
, *next
;
1363 /* Check the cache first. */
1364 for (sym
= *symcache_p
; sym
; sym
= next
)
1367 free_sym_cache (sym
);
1373 /* Get the address of NAME, and return it in ADDRP if found. if
1374 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1375 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1378 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1380 char own_buf
[266], *p
, *q
;
1382 struct sym_cache
*sym
;
1383 struct process_info
*proc
;
1385 proc
= current_process ();
1387 /* Check the cache first. */
1388 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1389 if (strcmp (name
, sym
->name
) == 0)
1395 /* It might not be an appropriate time to look up a symbol,
1396 e.g. while we're trying to fetch registers. */
1400 /* Send the request. */
1401 strcpy (own_buf
, "qSymbol:");
1402 bin2hex ((const gdb_byte
*) name
, own_buf
+ strlen ("qSymbol:"),
1404 if (putpkt (own_buf
) < 0)
1407 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1408 len
= getpkt (own_buf
);
1412 /* We ought to handle pretty much any packet at this point while we
1413 wait for the qSymbol "response". That requires re-entering the
1414 main loop. For now, this is an adequate approximation; allow
1415 GDB to read from memory while it figures out the address of the
1417 while (own_buf
[0] == 'm')
1420 unsigned char *mem_buf
;
1421 unsigned int mem_len
;
1423 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1424 mem_buf
= xmalloc (mem_len
);
1425 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1426 bin2hex (mem_buf
, own_buf
, mem_len
);
1428 write_enn (own_buf
);
1430 if (putpkt (own_buf
) < 0)
1432 len
= getpkt (own_buf
);
1437 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1439 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1443 p
= own_buf
+ strlen ("qSymbol:");
1445 while (*q
&& *q
!= ':')
1448 /* Make sure we found a value for the symbol. */
1449 if (p
== q
|| *q
== '\0')
1452 decode_address (addrp
, p
, q
- p
);
1454 /* Save the symbol in our cache. */
1455 sym
= xmalloc (sizeof (*sym
));
1456 sym
->name
= xstrdup (name
);
1458 sym
->next
= proc
->symbol_cache
;
1459 proc
->symbol_cache
= sym
;
1464 /* Relocate an instruction to execute at a different address. OLDLOC
1465 is the address in the inferior memory where the instruction to
1466 relocate is currently at. On input, TO points to the destination
1467 where we want the instruction to be copied (and possibly adjusted)
1468 to. On output, it points to one past the end of the resulting
1469 instruction(s). The effect of executing the instruction at TO
1470 shall be the same as if executing it at OLDLOC. For example, call
1471 instructions that implicitly push the return address on the stack
1472 should be adjusted to return to the instruction after OLDLOC;
1473 relative branches, and other PC-relative instructions need the
1474 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1477 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1481 ULONGEST written
= 0;
1483 /* Send the request. */
1484 strcpy (own_buf
, "qRelocInsn:");
1485 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1487 if (putpkt (own_buf
) < 0)
1490 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1491 len
= getpkt (own_buf
);
1495 /* We ought to handle pretty much any packet at this point while we
1496 wait for the qRelocInsn "response". That requires re-entering
1497 the main loop. For now, this is an adequate approximation; allow
1498 GDB to access memory. */
1499 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1502 unsigned char *mem_buf
= NULL
;
1503 unsigned int mem_len
;
1505 if (own_buf
[0] == 'm')
1507 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1508 mem_buf
= xmalloc (mem_len
);
1509 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1510 bin2hex (mem_buf
, own_buf
, mem_len
);
1512 write_enn (own_buf
);
1514 else if (own_buf
[0] == 'X')
1516 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1517 &mem_len
, &mem_buf
) < 0
1518 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1519 write_enn (own_buf
);
1525 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1526 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1529 write_enn (own_buf
);
1532 if (putpkt (own_buf
) < 0)
1534 len
= getpkt (own_buf
);
1539 if (own_buf
[0] == 'E')
1541 warning ("An error occurred while relocating an instruction: %s\n",
1546 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1548 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1553 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1560 monitor_output (const char *msg
)
1562 int len
= strlen (msg
);
1563 char *buf
= xmalloc (len
* 2 + 2);
1566 bin2hex ((const gdb_byte
*) msg
, buf
+ 1, len
);