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"
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>
56 #include <arpa/inet.h>
65 #include <sys/iomgr.h>
68 #ifndef HAVE_SOCKLEN_T
69 typedef int socklen_t
;
72 #ifndef IN_PROCESS_AGENT
75 # define INVALID_DESCRIPTOR INVALID_SOCKET
77 # define INVALID_DESCRIPTOR -1
80 /* Extra value for readchar_callback. */
82 /* The callback is currently not scheduled. */
86 /* Status of the readchar callback.
87 Either NOT_SCHEDULED or the callback id. */
88 static int readchar_callback
= NOT_SCHEDULED
;
90 static int readchar (void);
91 static void reset_readchar (void);
92 static void reschedule (void);
94 /* A cache entry for a successfully looked-up symbol. */
99 struct sym_cache
*next
;
102 int remote_debug
= 0;
103 struct ui_file
*gdb_stdlog
;
105 static int remote_is_stdio
= 0;
107 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
108 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
110 /* FIXME headerize? */
111 extern int using_threads
;
112 extern int debug_threads
;
114 /* If true, then GDB has requested noack mode. */
116 /* If true, then we tell GDB to use noack mode by default. */
117 int transport_is_reliable
= 0;
120 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
121 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
127 return remote_desc
!= INVALID_DESCRIPTOR
;
130 /* Return true if the remote connection is over stdio. */
133 remote_connection_is_stdio (void)
135 return remote_is_stdio
;
139 enable_async_notification (int fd
)
141 #if defined(F_SETFL) && defined (FASYNC)
142 int save_fcntl_flags
;
144 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
145 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
146 #if defined (F_SETOWN)
147 fcntl (fd
, F_SETOWN
, getpid ());
153 handle_accept_event (int err
, gdb_client_data client_data
)
155 struct sockaddr_in sockaddr
;
159 debug_printf ("handling possible accept event\n");
161 tmp
= sizeof (sockaddr
);
162 remote_desc
= accept (listen_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
163 if (remote_desc
== -1)
164 perror_with_name ("Accept failed");
166 /* Enable TCP keep alive process. */
168 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
169 (char *) &tmp
, sizeof (tmp
));
171 /* Tell TCP not to delay small packets. This greatly speeds up
172 interactive response. */
174 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
175 (char *) &tmp
, sizeof (tmp
));
178 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
179 exits when the remote side dies. */
185 close (listen_desc
); /* No longer need this */
187 closesocket (listen_desc
); /* No longer need this */
191 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
192 descriptor open for add_file_handler to wait for a new connection. */
193 delete_file_handler (listen_desc
);
195 /* Convert IP address to string. */
196 fprintf (stderr
, "Remote debugging from host %s\n",
197 inet_ntoa (sockaddr
.sin_addr
));
199 enable_async_notification (remote_desc
);
201 /* Register the event loop handler. */
202 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
204 /* We have a new GDB connection now. If we were disconnected
205 tracing, there's a window where the target could report a stop
206 event to the event loop, and since we have a connection now, we'd
207 try to send vStopped notifications to GDB. But, don't do that
208 until GDB as selected all-stop/non-stop, and has queried the
209 threads' status ('?'). */
215 /* Prepare for a later connection to a remote debugger.
216 NAME is the filename used for communication. */
219 remote_prepare (char *name
)
223 static int winsock_initialized
;
226 struct sockaddr_in sockaddr
;
231 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
233 /* We need to record fact that we're using stdio sooner than the
234 call to remote_open so start_inferior knows the connection is
237 transport_is_reliable
= 1;
241 port_str
= strchr (name
, ':');
242 if (port_str
== NULL
)
244 transport_is_reliable
= 0;
248 port
= strtoul (port_str
+ 1, &port_end
, 10);
249 if (port_str
[1] == '\0' || *port_end
!= '\0')
250 error ("Bad port argument: %s", name
);
253 if (!winsock_initialized
)
257 WSAStartup (MAKEWORD (1, 0), &wsad
);
258 winsock_initialized
= 1;
262 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
263 if (listen_desc
== -1)
264 perror_with_name ("Can't open socket");
266 /* Allow rapid reuse of this port. */
268 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
271 sockaddr
.sin_family
= PF_INET
;
272 sockaddr
.sin_port
= htons (port
);
273 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
275 if (bind (listen_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
276 || listen (listen_desc
, 1))
277 perror_with_name ("Can't bind address");
279 transport_is_reliable
= 1;
282 /* Open a connection to a remote debugger.
283 NAME is the filename used for communication. */
286 remote_open (char *name
)
290 port_str
= strchr (name
, ':');
292 if (port_str
== NULL
)
293 error ("Only <host>:<port> is supported on this platform.");
296 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
298 fprintf (stderr
, "Remote debugging using stdio\n");
300 /* Use stdin as the handle of the connection.
301 We only select on reads, for example. */
302 remote_desc
= fileno (stdin
);
304 enable_async_notification (remote_desc
);
306 /* Register the event loop handler. */
307 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
310 else if (port_str
== NULL
)
314 if (stat (name
, &statbuf
) == 0
315 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
316 remote_desc
= open (name
, O_RDWR
);
324 perror_with_name ("Could not open remote device");
328 struct termios termios
;
329 tcgetattr (remote_desc
, &termios
);
334 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
335 termios
.c_cflag
|= CLOCAL
| CS8
;
336 termios
.c_cc
[VMIN
] = 1;
337 termios
.c_cc
[VTIME
] = 0;
339 tcsetattr (remote_desc
, TCSANOW
, &termios
);
345 struct termio termio
;
346 ioctl (remote_desc
, TCGETA
, &termio
);
351 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
352 termio
.c_cflag
|= CLOCAL
| CS8
;
353 termio
.c_cc
[VMIN
] = 1;
354 termio
.c_cc
[VTIME
] = 0;
356 ioctl (remote_desc
, TCSETA
, &termio
);
364 ioctl (remote_desc
, TIOCGETP
, &sg
);
366 ioctl (remote_desc
, TIOCSETP
, &sg
);
370 fprintf (stderr
, "Remote debugging using %s\n", name
);
372 enable_async_notification (remote_desc
);
374 /* Register the event loop handler. */
375 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
377 #endif /* USE_WIN32API */
382 struct sockaddr_in sockaddr
;
384 len
= sizeof (sockaddr
);
385 if (getsockname (listen_desc
,
386 (struct sockaddr
*) &sockaddr
, &len
) < 0
387 || len
< sizeof (sockaddr
))
388 perror_with_name ("Can't determine port");
389 port
= ntohs (sockaddr
.sin_port
);
391 fprintf (stderr
, "Listening on port %d\n", port
);
394 /* Register the event loop handler. */
395 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
402 delete_file_handler (remote_desc
);
405 closesocket (remote_desc
);
407 if (! remote_connection_is_stdio ())
410 remote_desc
= INVALID_DESCRIPTOR
;
417 #ifndef IN_PROCESS_AGENT
420 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
427 for (i
= 0; i
< len
; i
++)
431 addr
= addr
| (fromhex (ch
) & 0x0f);
437 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
442 while (*end
!= '\0' && *end
!= ';')
445 decode_address (addrp
, start
, end
- start
);
454 #ifndef IN_PROCESS_AGENT
456 /* Look for a sequence of characters which can be run-length encoded.
457 If there are any, update *CSUM and *P. Otherwise, output the
458 single character. Return the number of characters consumed. */
461 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
465 /* Always output the character. */
469 /* Don't go past '~'. */
473 for (n
= 1; n
< remaining
; n
++)
474 if (buf
[n
] != buf
[0])
477 /* N is the index of the first character not the same as buf[0].
478 buf[0] is counted twice, so by decrementing N, we get the number
479 of characters the RLE sequence will replace. */
485 /* Skip the frame characters. The manual says to skip '+' and '-'
486 also, but there's no reason to. Unfortunately these two unusable
487 characters double the encoded length of a four byte zero
489 while (n
+ 29 == '$' || n
+ 29 == '#')
502 #ifndef IN_PROCESS_AGENT
504 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
507 write_ptid (char *buf
, ptid_t ptid
)
513 pid
= ptid_get_pid (ptid
);
515 buf
+= sprintf (buf
, "p-%x.", -pid
);
517 buf
+= sprintf (buf
, "p%x.", pid
);
519 tid
= ptid_get_lwp (ptid
);
521 buf
+= sprintf (buf
, "-%x", -tid
);
523 buf
+= sprintf (buf
, "%x", tid
);
529 hex_or_minus_one (char *buf
, char **obuf
)
533 if (strncmp (buf
, "-1", 2) == 0)
539 buf
= unpack_varlen_hex (buf
, &ret
);
547 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
548 passed the last parsed char. Returns null_ptid on error. */
550 read_ptid (char *buf
, char **obuf
)
554 ULONGEST pid
= 0, tid
= 0;
558 /* Multi-process ptid. */
559 pp
= unpack_varlen_hex (p
+ 1, &pid
);
561 error ("invalid remote ptid: %s\n", p
);
565 tid
= hex_or_minus_one (p
, &pp
);
569 return ptid_build (pid
, tid
, 0);
572 /* No multi-process. Just a tid. */
573 tid
= hex_or_minus_one (p
, &pp
);
575 /* Since the stub is not sending a process id, then default to
576 what's in the current inferior. */
577 pid
= ptid_get_pid (current_ptid
);
581 return ptid_build (pid
, tid
, 0);
584 /* Write COUNT bytes in BUF to the client.
585 The result is the number of bytes written or -1 if error.
586 This may return less than COUNT. */
589 write_prim (const void *buf
, int count
)
591 if (remote_connection_is_stdio ())
592 return write (fileno (stdout
), buf
, count
);
594 return write (remote_desc
, buf
, count
);
597 /* Read COUNT bytes from the client and store in BUF.
598 The result is the number of bytes read or -1 if error.
599 This may return less than COUNT. */
602 read_prim (void *buf
, int count
)
604 if (remote_connection_is_stdio ())
605 return read (fileno (stdin
), buf
, count
);
607 return read (remote_desc
, buf
, count
);
610 /* Send a packet to the remote machine, with error checking.
611 The data of the packet is in BUF, and the length of the
612 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
615 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
618 unsigned char csum
= 0;
623 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
625 /* Copy the packet into buffer BUF2, encapsulating it
626 and giving it a checksum. */
634 for (i
= 0; i
< cnt
;)
635 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
638 *p
++ = tohex ((csum
>> 4) & 0xf);
639 *p
++ = tohex (csum
& 0xf);
643 /* Send it over and over until we get a positive ack. */
647 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
649 perror ("putpkt(write)");
654 if (noack_mode
|| is_notif
)
656 /* Don't expect an ack then. */
660 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
662 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
670 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
684 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
688 /* Check for an input interrupt while we're here. */
689 if (cc
== '\003' && current_inferior
!= NULL
)
690 (*the_target
->request_interrupt
) ();
695 return 1; /* Success! */
699 putpkt_binary (char *buf
, int cnt
)
701 return putpkt_binary_1 (buf
, cnt
, 0);
704 /* Send a packet to the remote machine, with error checking. The data
705 of the packet is in BUF, and the packet should be a NUL-terminated
706 string. Returns >= 0 on success, -1 otherwise. */
711 return putpkt_binary (buf
, strlen (buf
));
715 putpkt_notif (char *buf
)
717 return putpkt_binary_1 (buf
, strlen (buf
), 1);
720 /* Come here when we get an input interrupt from the remote side. This
721 interrupt should only be active while we are waiting for the child to do
722 something. Thus this assumes readchar:bufcnt is 0.
723 About the only thing that should come through is a ^C, which
724 will cause us to request child interruption. */
727 input_interrupt (int unused
)
730 struct timeval immediate
= { 0, 0 };
732 /* Protect against spurious interrupts. This has been observed to
733 be a problem under NetBSD 1.4 and 1.5. */
736 FD_SET (remote_desc
, &readset
);
737 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
742 cc
= read_prim (&c
, 1);
744 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
746 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
751 (*the_target
->request_interrupt
) ();
755 /* Check if the remote side sent us an interrupt request (^C). */
757 check_remote_input_interrupt_request (void)
759 /* This function may be called before establishing communications,
760 therefore we need to validate the remote descriptor. */
762 if (remote_desc
== INVALID_DESCRIPTOR
)
768 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
769 accept Control-C from the client, and must be disabled when talking to
773 unblock_async_io (void)
778 sigemptyset (&sigio_set
);
779 sigaddset (&sigio_set
, SIGIO
);
780 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
786 nto_comctrl (int enable
)
788 struct sigevent event
;
792 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
793 event
.sigev_signo
= SIGIO
;
794 event
.sigev_code
= 0;
795 event
.sigev_value
.sival_ptr
= NULL
;
796 event
.sigev_priority
= -1;
797 ionotify (remote_desc
, _NOTIFY_ACTION_POLLARM
, _NOTIFY_COND_INPUT
,
801 ionotify (remote_desc
, _NOTIFY_ACTION_POLL
, _NOTIFY_COND_INPUT
, NULL
);
806 /* Current state of asynchronous I/O. */
807 static int async_io_enabled
;
809 /* Enable asynchronous I/O. */
811 enable_async_io (void)
813 if (async_io_enabled
)
817 signal (SIGIO
, input_interrupt
);
819 async_io_enabled
= 1;
825 /* Disable asynchronous I/O. */
827 disable_async_io (void)
829 if (!async_io_enabled
)
833 signal (SIGIO
, SIG_IGN
);
835 async_io_enabled
= 0;
843 initialize_async_io (void)
845 /* Make sure that async I/O starts disabled. */
846 async_io_enabled
= 1;
849 /* Make sure the signal is unblocked. */
853 /* Internal buffer used by readchar.
854 These are global to readchar because reschedule_remote needs to be
855 able to tell whether the buffer is empty. */
857 static unsigned char readchar_buf
[BUFSIZ
];
858 static int readchar_bufcnt
= 0;
859 static unsigned char *readchar_bufp
;
861 /* Returns next char from remote GDB. -1 if error. */
868 if (readchar_bufcnt
== 0)
870 readchar_bufcnt
= read_prim (readchar_buf
, sizeof (readchar_buf
));
872 if (readchar_bufcnt
<= 0)
874 if (readchar_bufcnt
== 0)
875 fprintf (stderr
, "readchar: Got EOF\n");
882 readchar_bufp
= readchar_buf
;
886 ch
= *readchar_bufp
++;
891 /* Reset the readchar state machine. */
894 reset_readchar (void)
897 if (readchar_callback
!= NOT_SCHEDULED
)
899 delete_callback_event (readchar_callback
);
900 readchar_callback
= NOT_SCHEDULED
;
904 /* Process remaining data in readchar_buf. */
907 process_remaining (void *context
)
911 /* This is a one-shot event. */
912 readchar_callback
= NOT_SCHEDULED
;
914 if (readchar_bufcnt
> 0)
915 res
= handle_serial_event (0, NULL
);
922 /* If there is still data in the buffer, queue another event to process it,
923 we can't sleep in select yet. */
928 if (readchar_bufcnt
> 0 && readchar_callback
== NOT_SCHEDULED
)
929 readchar_callback
= append_callback_event (process_remaining
, NULL
);
932 /* Read a packet from the remote machine, with error checking,
933 and store it in BUF. Returns length of packet, or negative if error. */
939 unsigned char csum
, c1
, c2
;
953 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
974 c1
= fromhex (readchar ());
975 c2
= fromhex (readchar ());
977 if (csum
== (c1
<< 4) + c2
)
983 "Bad checksum, sentsum=0x%x, csum=0x%x, "
984 "buf=%s [no-ack-mode, Bad medium?]\n",
985 (c1
<< 4) + c2
, csum
, buf
);
986 /* Not much we can do, GDB wasn't expecting an ack/nac. */
990 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
991 (c1
<< 4) + c2
, csum
, buf
);
992 if (write_prim ("-", 1) != 1)
1000 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
1004 if (write_prim ("+", 1) != 1)
1009 fprintf (stderr
, "[sent ack]\n");
1017 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
1026 write_ok (char *buf
)
1034 write_enn (char *buf
)
1036 /* Some day, we should define the meanings of the error codes... */
1045 #ifndef IN_PROCESS_AGENT
1048 outreg (struct regcache
*regcache
, int regno
, char *buf
)
1050 if ((regno
>> 12) != 0)
1051 *buf
++ = tohex ((regno
>> 12) & 0xf);
1052 if ((regno
>> 8) != 0)
1053 *buf
++ = tohex ((regno
>> 8) & 0xf);
1054 *buf
++ = tohex ((regno
>> 4) & 0xf);
1055 *buf
++ = tohex (regno
& 0xf);
1057 collect_register_as_string (regcache
, regno
, buf
);
1058 buf
+= 2 * register_size (regcache
->tdesc
, regno
);
1065 new_thread_notify (int id
)
1069 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1073 if (server_waiting
== 0)
1076 sprintf (own_buf
, "n%x", id
);
1077 disable_async_io ();
1083 dead_thread_notify (int id
)
1087 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1091 sprintf (own_buf
, "x%x", id
);
1092 disable_async_io ();
1098 prepare_resume_reply (char *buf
, ptid_t ptid
,
1099 struct target_waitstatus
*status
)
1102 debug_printf ("Writing resume reply for %s:%d\n",
1103 target_pid_to_str (ptid
), status
->kind
);
1105 switch (status
->kind
)
1107 case TARGET_WAITKIND_STOPPED
:
1109 struct thread_info
*saved_inferior
;
1111 struct regcache
*regcache
;
1113 sprintf (buf
, "T%02x", status
->value
.sig
);
1114 buf
+= strlen (buf
);
1116 saved_inferior
= current_inferior
;
1118 current_inferior
= find_thread_ptid (ptid
);
1120 regp
= current_target_desc ()->expedite_regs
;
1122 regcache
= get_thread_regcache (current_inferior
, 1);
1124 if (the_target
->stopped_by_watchpoint
!= NULL
1125 && (*the_target
->stopped_by_watchpoint
) ())
1130 strncpy (buf
, "watch:", 6);
1133 addr
= (*the_target
->stopped_data_address
) ();
1135 /* Convert each byte of the address into two hexadecimal
1136 chars. Note that we take sizeof (void *) instead of
1137 sizeof (addr); this is to avoid sending a 64-bit
1138 address to a 32-bit GDB. */
1139 for (i
= sizeof (void *) * 2; i
> 0; i
--)
1140 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
1146 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1151 /* Formerly, if the debugger had not used any thread features
1152 we would not burden it with a thread status response. This
1153 was for the benefit of GDB 4.13 and older. However, in
1154 recent GDB versions the check (``if (cont_thread != 0)'')
1155 does not have the desired effect because of sillyness in
1156 the way that the remote protocol handles specifying a
1157 thread. Since thread support relies on qSymbol support
1158 anyway, assume GDB can handle threads. */
1160 if (using_threads
&& !disable_packet_Tthread
)
1162 /* This if (1) ought to be unnecessary. But remote_wait
1163 in GDB will claim this event belongs to inferior_ptid
1164 if we do not specify a thread, and there's no way for
1165 gdbserver to know what inferior_ptid is. */
1166 if (1 || !ptid_equal (general_thread
, ptid
))
1169 /* In non-stop, don't change the general thread behind
1172 general_thread
= ptid
;
1173 sprintf (buf
, "thread:");
1174 buf
+= strlen (buf
);
1175 buf
= write_ptid (buf
, ptid
);
1177 buf
+= strlen (buf
);
1179 core
= target_core_of_thread (ptid
);
1183 sprintf (buf
, "core:");
1184 buf
+= strlen (buf
);
1185 sprintf (buf
, "%x", core
);
1187 buf
+= strlen (buf
);
1194 strcpy (buf
, "library:;");
1195 buf
+= strlen (buf
);
1199 current_inferior
= saved_inferior
;
1202 case TARGET_WAITKIND_EXITED
:
1204 sprintf (buf
, "W%x;process:%x",
1205 status
->value
.integer
, ptid_get_pid (ptid
));
1207 sprintf (buf
, "W%02x", status
->value
.integer
);
1209 case TARGET_WAITKIND_SIGNALLED
:
1211 sprintf (buf
, "X%x;process:%x",
1212 status
->value
.sig
, ptid_get_pid (ptid
));
1214 sprintf (buf
, "X%02x", status
->value
.sig
);
1217 error ("unhandled waitkind");
1223 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1227 *mem_addr_ptr
= *len_ptr
= 0;
1229 while ((ch
= from
[i
++]) != ',')
1231 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1232 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1235 for (j
= 0; j
< 4; j
++)
1237 if ((ch
= from
[i
++]) == 0)
1239 *len_ptr
= *len_ptr
<< 4;
1240 *len_ptr
|= fromhex (ch
) & 0x0f;
1245 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1246 unsigned char **to_p
)
1250 *mem_addr_ptr
= *len_ptr
= 0;
1252 while ((ch
= from
[i
++]) != ',')
1254 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1255 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1258 while ((ch
= from
[i
++]) != ':')
1260 *len_ptr
= *len_ptr
<< 4;
1261 *len_ptr
|= fromhex (ch
) & 0x0f;
1265 *to_p
= xmalloc (*len_ptr
);
1267 hex2bin (&from
[i
++], *to_p
, *len_ptr
);
1271 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1272 unsigned int *len_ptr
, unsigned char **to_p
)
1276 *mem_addr_ptr
= *len_ptr
= 0;
1278 while ((ch
= from
[i
++]) != ',')
1280 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1281 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1284 while ((ch
= from
[i
++]) != ':')
1286 *len_ptr
= *len_ptr
<< 4;
1287 *len_ptr
|= fromhex (ch
) & 0x0f;
1291 *to_p
= xmalloc (*len_ptr
);
1293 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1294 *to_p
, *len_ptr
) != *len_ptr
)
1300 /* Decode a qXfer write request. */
1303 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1304 unsigned int *len
, unsigned char *data
)
1309 /* Extract the offset. */
1311 while ((ch
= *buf
++) != ':')
1313 *offset
= *offset
<< 4;
1314 *offset
|= fromhex (ch
) & 0x0f;
1317 /* Get encoded data. */
1318 packet_len
-= buf
- b
;
1319 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1324 /* Decode the parameters of a qSearch:memory packet. */
1327 decode_search_memory_packet (const char *buf
, int packet_len
,
1328 CORE_ADDR
*start_addrp
,
1329 CORE_ADDR
*search_space_lenp
,
1330 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1332 const char *p
= buf
;
1334 p
= decode_address_to_semicolon (start_addrp
, p
);
1335 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1336 packet_len
-= p
- buf
;
1337 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1338 pattern
, packet_len
);
1343 free_sym_cache (struct sym_cache
*sym
)
1353 clear_symbol_cache (struct sym_cache
**symcache_p
)
1355 struct sym_cache
*sym
, *next
;
1357 /* Check the cache first. */
1358 for (sym
= *symcache_p
; sym
; sym
= next
)
1361 free_sym_cache (sym
);
1367 /* Get the address of NAME, and return it in ADDRP if found. if
1368 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1369 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1372 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1374 char own_buf
[266], *p
, *q
;
1376 struct sym_cache
*sym
;
1377 struct process_info
*proc
;
1379 proc
= current_process ();
1381 /* Check the cache first. */
1382 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1383 if (strcmp (name
, sym
->name
) == 0)
1389 /* It might not be an appropriate time to look up a symbol,
1390 e.g. while we're trying to fetch registers. */
1394 /* Send the request. */
1395 strcpy (own_buf
, "qSymbol:");
1396 bin2hex ((const gdb_byte
*) name
, own_buf
+ strlen ("qSymbol:"),
1398 if (putpkt (own_buf
) < 0)
1401 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1402 len
= getpkt (own_buf
);
1406 /* We ought to handle pretty much any packet at this point while we
1407 wait for the qSymbol "response". That requires re-entering the
1408 main loop. For now, this is an adequate approximation; allow
1409 GDB to read from memory while it figures out the address of the
1411 while (own_buf
[0] == 'm')
1414 unsigned char *mem_buf
;
1415 unsigned int mem_len
;
1417 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1418 mem_buf
= xmalloc (mem_len
);
1419 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1420 bin2hex (mem_buf
, own_buf
, mem_len
);
1422 write_enn (own_buf
);
1424 if (putpkt (own_buf
) < 0)
1426 len
= getpkt (own_buf
);
1431 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1433 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1437 p
= own_buf
+ strlen ("qSymbol:");
1439 while (*q
&& *q
!= ':')
1442 /* Make sure we found a value for the symbol. */
1443 if (p
== q
|| *q
== '\0')
1446 decode_address (addrp
, p
, q
- p
);
1448 /* Save the symbol in our cache. */
1449 sym
= xmalloc (sizeof (*sym
));
1450 sym
->name
= xstrdup (name
);
1452 sym
->next
= proc
->symbol_cache
;
1453 proc
->symbol_cache
= sym
;
1458 /* Relocate an instruction to execute at a different address. OLDLOC
1459 is the address in the inferior memory where the instruction to
1460 relocate is currently at. On input, TO points to the destination
1461 where we want the instruction to be copied (and possibly adjusted)
1462 to. On output, it points to one past the end of the resulting
1463 instruction(s). The effect of executing the instruction at TO
1464 shall be the same as if executing it at OLDLOC. For example, call
1465 instructions that implicitly push the return address on the stack
1466 should be adjusted to return to the instruction after OLDLOC;
1467 relative branches, and other PC-relative instructions need the
1468 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1471 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1475 ULONGEST written
= 0;
1477 /* Send the request. */
1478 strcpy (own_buf
, "qRelocInsn:");
1479 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1481 if (putpkt (own_buf
) < 0)
1484 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1485 len
= getpkt (own_buf
);
1489 /* We ought to handle pretty much any packet at this point while we
1490 wait for the qRelocInsn "response". That requires re-entering
1491 the main loop. For now, this is an adequate approximation; allow
1492 GDB to access memory. */
1493 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1496 unsigned char *mem_buf
= NULL
;
1497 unsigned int mem_len
;
1499 if (own_buf
[0] == 'm')
1501 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1502 mem_buf
= xmalloc (mem_len
);
1503 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1504 bin2hex (mem_buf
, own_buf
, mem_len
);
1506 write_enn (own_buf
);
1508 else if (own_buf
[0] == 'X')
1510 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1511 &mem_len
, &mem_buf
) < 0
1512 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1513 write_enn (own_buf
);
1519 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1520 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1523 write_enn (own_buf
);
1526 if (putpkt (own_buf
) < 0)
1528 len
= getpkt (own_buf
);
1533 if (own_buf
[0] == 'E')
1535 warning ("An error occurred while relocating an instruction: %s\n",
1540 if (strncmp (own_buf
, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1542 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1547 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1554 monitor_output (const char *msg
)
1556 int len
= strlen (msg
);
1557 char *buf
= xmalloc (len
* 2 + 2);
1560 bin2hex ((const gdb_byte
*) msg
, buf
+ 1, len
);