1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986-2015 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"
28 #include <sys/ioctl.h>
36 #if HAVE_NETINET_TCP_H
37 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
51 #include <arpa/inet.h>
54 #include "gdb_socket.h"
57 #include <sys/iomgr.h>
60 #ifndef HAVE_SOCKLEN_T
61 typedef int socklen_t
;
64 #ifndef IN_PROCESS_AGENT
67 # define INVALID_DESCRIPTOR INVALID_SOCKET
69 # define INVALID_DESCRIPTOR -1
72 /* Extra value for readchar_callback. */
74 /* The callback is currently not scheduled. */
78 /* Status of the readchar callback.
79 Either NOT_SCHEDULED or the callback id. */
80 static int readchar_callback
= NOT_SCHEDULED
;
82 static int readchar (void);
83 static void reset_readchar (void);
84 static void reschedule (void);
86 /* A cache entry for a successfully looked-up symbol. */
91 struct sym_cache
*next
;
95 struct ui_file
*gdb_stdlog
;
97 static int remote_is_stdio
= 0;
99 static gdb_fildes_t remote_desc
= INVALID_DESCRIPTOR
;
100 static gdb_fildes_t listen_desc
= INVALID_DESCRIPTOR
;
102 /* FIXME headerize? */
103 extern int using_threads
;
104 extern int debug_threads
;
106 /* If true, then GDB has requested noack mode. */
108 /* If true, then we tell GDB to use noack mode by default. */
109 int transport_is_reliable
= 0;
112 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
113 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
119 return remote_desc
!= INVALID_DESCRIPTOR
;
122 /* Return true if the remote connection is over stdio. */
125 remote_connection_is_stdio (void)
127 return remote_is_stdio
;
131 enable_async_notification (int fd
)
133 #if defined(F_SETFL) && defined (FASYNC)
134 int save_fcntl_flags
;
136 save_fcntl_flags
= fcntl (fd
, F_GETFL
, 0);
137 fcntl (fd
, F_SETFL
, save_fcntl_flags
| FASYNC
);
138 #if defined (F_SETOWN)
139 fcntl (fd
, F_SETOWN
, getpid ());
145 handle_accept_event (int err
, gdb_client_data client_data
)
147 union gdb_sockaddr_u sockaddr
;
151 debug_printf ("handling possible accept event\n");
153 tmp
= sizeof (sockaddr
.sa_in
);
154 remote_desc
= accept (listen_desc
, &sockaddr
.sa
, &tmp
);
155 if (remote_desc
== -1)
156 perror_with_name ("Accept failed");
158 /* Enable TCP keep alive process. */
160 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
161 (char *) &tmp
, sizeof (tmp
));
163 /* Tell TCP not to delay small packets. This greatly speeds up
164 interactive response. */
166 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
167 (char *) &tmp
, sizeof (tmp
));
170 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
171 exits when the remote side dies. */
177 close (listen_desc
); /* No longer need this */
179 closesocket (listen_desc
); /* No longer need this */
183 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
184 descriptor open for add_file_handler to wait for a new connection. */
185 delete_file_handler (listen_desc
);
187 /* Convert IP address to string. */
188 fprintf (stderr
, "Remote debugging from host %s\n",
189 inet_ntoa (sockaddr
.sa_in
.sin_addr
));
191 enable_async_notification (remote_desc
);
193 /* Register the event loop handler. */
194 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
196 /* We have a new GDB connection now. If we were disconnected
197 tracing, there's a window where the target could report a stop
198 event to the event loop, and since we have a connection now, we'd
199 try to send vStopped notifications to GDB. But, don't do that
200 until GDB as selected all-stop/non-stop, and has queried the
201 threads' status ('?'). */
207 /* Prepare for a later connection to a remote debugger.
208 NAME is the filename used for communication. */
211 remote_prepare (char *name
)
215 static int winsock_initialized
;
218 union gdb_sockaddr_u sockaddr
;
223 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
225 /* We need to record fact that we're using stdio sooner than the
226 call to remote_open so start_inferior knows the connection is
229 transport_is_reliable
= 1;
233 port_str
= strchr (name
, ':');
234 if (port_str
== NULL
)
236 transport_is_reliable
= 0;
240 port
= strtoul (port_str
+ 1, &port_end
, 10);
241 if (port_str
[1] == '\0' || *port_end
!= '\0')
242 error ("Bad port argument: %s", name
);
245 if (!winsock_initialized
)
249 WSAStartup (MAKEWORD (1, 0), &wsad
);
250 winsock_initialized
= 1;
254 listen_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
255 if (listen_desc
== -1)
256 perror_with_name ("Can't open socket");
258 /* Allow rapid reuse of this port. */
260 setsockopt (listen_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
263 sockaddr
.sa_in
.sin_family
= PF_INET
;
264 sockaddr
.sa_in
.sin_port
= htons (port
);
265 sockaddr
.sa_in
.sin_addr
.s_addr
= INADDR_ANY
;
267 if (bind (listen_desc
, &sockaddr
.sa
, sizeof (sockaddr
.sa_in
))
268 || listen (listen_desc
, 1))
269 perror_with_name ("Can't bind address");
271 transport_is_reliable
= 1;
274 /* Open a connection to a remote debugger.
275 NAME is the filename used for communication. */
278 remote_open (char *name
)
282 port_str
= strchr (name
, ':');
284 if (port_str
== NULL
)
285 error ("Only <host>:<port> is supported on this platform.");
288 if (strcmp (name
, STDIO_CONNECTION_NAME
) == 0)
290 fprintf (stderr
, "Remote debugging using stdio\n");
292 /* Use stdin as the handle of the connection.
293 We only select on reads, for example. */
294 remote_desc
= fileno (stdin
);
296 enable_async_notification (remote_desc
);
298 /* Register the event loop handler. */
299 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
302 else if (port_str
== NULL
)
306 if (stat (name
, &statbuf
) == 0
307 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
308 remote_desc
= open (name
, O_RDWR
);
316 perror_with_name ("Could not open remote device");
320 struct termios termios
;
321 tcgetattr (remote_desc
, &termios
);
326 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
327 termios
.c_cflag
|= CLOCAL
| CS8
;
328 termios
.c_cc
[VMIN
] = 1;
329 termios
.c_cc
[VTIME
] = 0;
331 tcsetattr (remote_desc
, TCSANOW
, &termios
);
337 struct termio termio
;
338 ioctl (remote_desc
, TCGETA
, &termio
);
343 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
344 termio
.c_cflag
|= CLOCAL
| CS8
;
345 termio
.c_cc
[VMIN
] = 1;
346 termio
.c_cc
[VTIME
] = 0;
348 ioctl (remote_desc
, TCSETA
, &termio
);
356 ioctl (remote_desc
, TIOCGETP
, &sg
);
358 ioctl (remote_desc
, TIOCSETP
, &sg
);
362 fprintf (stderr
, "Remote debugging using %s\n", name
);
364 enable_async_notification (remote_desc
);
366 /* Register the event loop handler. */
367 add_file_handler (remote_desc
, handle_serial_event
, NULL
);
369 #endif /* USE_WIN32API */
374 struct sockaddr_in sockaddr
;
376 len
= sizeof (sockaddr
);
377 if (getsockname (listen_desc
,
378 (struct sockaddr
*) &sockaddr
, &len
) < 0
379 || len
< sizeof (sockaddr
))
380 perror_with_name ("Can't determine port");
381 port
= ntohs (sockaddr
.sin_port
);
383 fprintf (stderr
, "Listening on port %d\n", port
);
386 /* Register the event loop handler. */
387 add_file_handler (listen_desc
, handle_accept_event
, NULL
);
394 delete_file_handler (remote_desc
);
397 closesocket (remote_desc
);
399 if (! remote_connection_is_stdio ())
402 remote_desc
= INVALID_DESCRIPTOR
;
409 #ifndef IN_PROCESS_AGENT
412 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
419 for (i
= 0; i
< len
; i
++)
423 addr
= addr
| (fromhex (ch
) & 0x0f);
429 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
434 while (*end
!= '\0' && *end
!= ';')
437 decode_address (addrp
, start
, end
- start
);
446 #ifndef IN_PROCESS_AGENT
448 /* Look for a sequence of characters which can be run-length encoded.
449 If there are any, update *CSUM and *P. Otherwise, output the
450 single character. Return the number of characters consumed. */
453 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
457 /* Always output the character. */
461 /* Don't go past '~'. */
465 for (n
= 1; n
< remaining
; n
++)
466 if (buf
[n
] != buf
[0])
469 /* N is the index of the first character not the same as buf[0].
470 buf[0] is counted twice, so by decrementing N, we get the number
471 of characters the RLE sequence will replace. */
477 /* Skip the frame characters. The manual says to skip '+' and '-'
478 also, but there's no reason to. Unfortunately these two unusable
479 characters double the encoded length of a four byte zero
481 while (n
+ 29 == '$' || n
+ 29 == '#')
494 #ifndef IN_PROCESS_AGENT
496 /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
499 write_ptid (char *buf
, ptid_t ptid
)
505 pid
= ptid_get_pid (ptid
);
507 buf
+= sprintf (buf
, "p-%x.", -pid
);
509 buf
+= sprintf (buf
, "p%x.", pid
);
511 tid
= ptid_get_lwp (ptid
);
513 buf
+= sprintf (buf
, "-%x", -tid
);
515 buf
+= sprintf (buf
, "%x", tid
);
521 hex_or_minus_one (char *buf
, char **obuf
)
525 if (startswith (buf
, "-1"))
531 buf
= unpack_varlen_hex (buf
, &ret
);
539 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
540 passed the last parsed char. Returns null_ptid on error. */
542 read_ptid (char *buf
, char **obuf
)
546 ULONGEST pid
= 0, tid
= 0;
550 /* Multi-process ptid. */
551 pp
= unpack_varlen_hex (p
+ 1, &pid
);
553 error ("invalid remote ptid: %s\n", p
);
557 tid
= hex_or_minus_one (p
, &pp
);
561 return ptid_build (pid
, tid
, 0);
564 /* No multi-process. Just a tid. */
565 tid
= hex_or_minus_one (p
, &pp
);
567 /* Since the stub is not sending a process id, then default to
568 what's in the current inferior. */
569 pid
= ptid_get_pid (current_ptid
);
573 return ptid_build (pid
, tid
, 0);
576 /* Write COUNT bytes in BUF to the client.
577 The result is the number of bytes written or -1 if error.
578 This may return less than COUNT. */
581 write_prim (const void *buf
, int count
)
583 if (remote_connection_is_stdio ())
584 return write (fileno (stdout
), buf
, count
);
586 return write (remote_desc
, buf
, count
);
589 /* Read COUNT bytes from the client and store in BUF.
590 The result is the number of bytes read or -1 if error.
591 This may return less than COUNT. */
594 read_prim (void *buf
, int count
)
596 if (remote_connection_is_stdio ())
597 return read (fileno (stdin
), buf
, count
);
599 return read (remote_desc
, buf
, count
);
602 /* Send a packet to the remote machine, with error checking.
603 The data of the packet is in BUF, and the length of the
604 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
607 putpkt_binary_1 (char *buf
, int cnt
, int is_notif
)
610 unsigned char csum
= 0;
615 buf2
= xmalloc (strlen ("$") + cnt
+ strlen ("#nn") + 1);
617 /* Copy the packet into buffer BUF2, encapsulating it
618 and giving it a checksum. */
626 for (i
= 0; i
< cnt
;)
627 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
630 *p
++ = tohex ((csum
>> 4) & 0xf);
631 *p
++ = tohex (csum
& 0xf);
635 /* Send it over and over until we get a positive ack. */
639 if (write_prim (buf2
, p
- buf2
) != p
- buf2
)
641 perror ("putpkt(write)");
646 if (noack_mode
|| is_notif
)
648 /* Don't expect an ack then. */
652 fprintf (stderr
, "putpkt (\"%s\"); [notif]\n", buf2
);
654 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
662 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
676 fprintf (stderr
, "[received '%c' (0x%x)]\n", cc
, cc
);
680 /* Check for an input interrupt while we're here. */
681 if (cc
== '\003' && current_thread
!= NULL
)
682 (*the_target
->request_interrupt
) ();
687 return 1; /* Success! */
691 putpkt_binary (char *buf
, int cnt
)
693 return putpkt_binary_1 (buf
, cnt
, 0);
696 /* Send a packet to the remote machine, with error checking. The data
697 of the packet is in BUF, and the packet should be a NUL-terminated
698 string. Returns >= 0 on success, -1 otherwise. */
703 return putpkt_binary (buf
, strlen (buf
));
707 putpkt_notif (char *buf
)
709 return putpkt_binary_1 (buf
, strlen (buf
), 1);
712 /* Come here when we get an input interrupt from the remote side. This
713 interrupt should only be active while we are waiting for the child to do
714 something. Thus this assumes readchar:bufcnt is 0.
715 About the only thing that should come through is a ^C, which
716 will cause us to request child interruption. */
719 input_interrupt (int unused
)
722 struct timeval immediate
= { 0, 0 };
724 /* Protect against spurious interrupts. This has been observed to
725 be a problem under NetBSD 1.4 and 1.5. */
728 FD_SET (remote_desc
, &readset
);
729 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
734 cc
= read_prim (&c
, 1);
738 fprintf (stderr
, "client connection closed\n");
741 else if (cc
!= 1 || c
!= '\003' || current_thread
== NULL
)
743 fprintf (stderr
, "input_interrupt, count = %d c = %d ", cc
, c
);
745 fprintf (stderr
, "('%c')\n", c
);
747 fprintf (stderr
, "('\\x%02x')\n", c
& 0xff);
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_thread
;
1111 struct regcache
*regcache
;
1113 sprintf (buf
, "T%02x", status
->value
.sig
);
1114 buf
+= strlen (buf
);
1116 saved_thread
= current_thread
;
1118 current_thread
= find_thread_ptid (ptid
);
1120 regp
= current_target_desc ()->expedite_regs
;
1122 regcache
= get_thread_regcache (current_thread
, 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);
1143 else if (swbreak_feature
&& target_stopped_by_sw_breakpoint ())
1145 sprintf (buf
, "swbreak:;");
1146 buf
+= strlen (buf
);
1148 else if (hwbreak_feature
&& target_stopped_by_hw_breakpoint ())
1150 sprintf (buf
, "hwbreak:;");
1151 buf
+= strlen (buf
);
1156 buf
= outreg (regcache
, find_regno (regcache
->tdesc
, *regp
), buf
);
1161 /* Formerly, if the debugger had not used any thread features
1162 we would not burden it with a thread status response. This
1163 was for the benefit of GDB 4.13 and older. However, in
1164 recent GDB versions the check (``if (cont_thread != 0)'')
1165 does not have the desired effect because of sillyness in
1166 the way that the remote protocol handles specifying a
1167 thread. Since thread support relies on qSymbol support
1168 anyway, assume GDB can handle threads. */
1170 if (using_threads
&& !disable_packet_Tthread
)
1172 /* This if (1) ought to be unnecessary. But remote_wait
1173 in GDB will claim this event belongs to inferior_ptid
1174 if we do not specify a thread, and there's no way for
1175 gdbserver to know what inferior_ptid is. */
1176 if (1 || !ptid_equal (general_thread
, ptid
))
1179 /* In non-stop, don't change the general thread behind
1182 general_thread
= ptid
;
1183 sprintf (buf
, "thread:");
1184 buf
+= strlen (buf
);
1185 buf
= write_ptid (buf
, ptid
);
1187 buf
+= strlen (buf
);
1189 core
= target_core_of_thread (ptid
);
1193 sprintf (buf
, "core:");
1194 buf
+= strlen (buf
);
1195 sprintf (buf
, "%x", core
);
1197 buf
+= strlen (buf
);
1204 strcpy (buf
, "library:;");
1205 buf
+= strlen (buf
);
1209 current_thread
= saved_thread
;
1212 case TARGET_WAITKIND_EXITED
:
1214 sprintf (buf
, "W%x;process:%x",
1215 status
->value
.integer
, ptid_get_pid (ptid
));
1217 sprintf (buf
, "W%02x", status
->value
.integer
);
1219 case TARGET_WAITKIND_SIGNALLED
:
1221 sprintf (buf
, "X%x;process:%x",
1222 status
->value
.sig
, ptid_get_pid (ptid
));
1224 sprintf (buf
, "X%02x", status
->value
.sig
);
1227 error ("unhandled waitkind");
1233 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1237 *mem_addr_ptr
= *len_ptr
= 0;
1239 while ((ch
= from
[i
++]) != ',')
1241 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1242 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1245 for (j
= 0; j
< 4; j
++)
1247 if ((ch
= from
[i
++]) == 0)
1249 *len_ptr
= *len_ptr
<< 4;
1250 *len_ptr
|= fromhex (ch
) & 0x0f;
1255 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1256 unsigned char **to_p
)
1260 *mem_addr_ptr
= *len_ptr
= 0;
1262 while ((ch
= from
[i
++]) != ',')
1264 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1265 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1268 while ((ch
= from
[i
++]) != ':')
1270 *len_ptr
= *len_ptr
<< 4;
1271 *len_ptr
|= fromhex (ch
) & 0x0f;
1275 *to_p
= xmalloc (*len_ptr
);
1277 hex2bin (&from
[i
++], *to_p
, *len_ptr
);
1281 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1282 unsigned int *len_ptr
, unsigned char **to_p
)
1286 *mem_addr_ptr
= *len_ptr
= 0;
1288 while ((ch
= from
[i
++]) != ',')
1290 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1291 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1294 while ((ch
= from
[i
++]) != ':')
1296 *len_ptr
= *len_ptr
<< 4;
1297 *len_ptr
|= fromhex (ch
) & 0x0f;
1301 *to_p
= xmalloc (*len_ptr
);
1303 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1304 *to_p
, *len_ptr
) != *len_ptr
)
1310 /* Decode a qXfer write request. */
1313 decode_xfer_write (char *buf
, int packet_len
, CORE_ADDR
*offset
,
1314 unsigned int *len
, unsigned char *data
)
1319 /* Extract the offset. */
1321 while ((ch
= *buf
++) != ':')
1323 *offset
= *offset
<< 4;
1324 *offset
|= fromhex (ch
) & 0x0f;
1327 /* Get encoded data. */
1328 packet_len
-= buf
- b
;
1329 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1334 /* Decode the parameters of a qSearch:memory packet. */
1337 decode_search_memory_packet (const char *buf
, int packet_len
,
1338 CORE_ADDR
*start_addrp
,
1339 CORE_ADDR
*search_space_lenp
,
1340 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1342 const char *p
= buf
;
1344 p
= decode_address_to_semicolon (start_addrp
, p
);
1345 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1346 packet_len
-= p
- buf
;
1347 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1348 pattern
, packet_len
);
1353 free_sym_cache (struct sym_cache
*sym
)
1363 clear_symbol_cache (struct sym_cache
**symcache_p
)
1365 struct sym_cache
*sym
, *next
;
1367 /* Check the cache first. */
1368 for (sym
= *symcache_p
; sym
; sym
= next
)
1371 free_sym_cache (sym
);
1377 /* Get the address of NAME, and return it in ADDRP if found. if
1378 MAY_ASK_GDB is false, assume symbol cache misses are failures.
1379 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1382 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
, int may_ask_gdb
)
1384 char own_buf
[266], *p
, *q
;
1386 struct sym_cache
*sym
;
1387 struct process_info
*proc
;
1389 proc
= current_process ();
1391 /* Check the cache first. */
1392 for (sym
= proc
->symbol_cache
; sym
; sym
= sym
->next
)
1393 if (strcmp (name
, sym
->name
) == 0)
1399 /* It might not be an appropriate time to look up a symbol,
1400 e.g. while we're trying to fetch registers. */
1404 /* Send the request. */
1405 strcpy (own_buf
, "qSymbol:");
1406 bin2hex ((const gdb_byte
*) name
, own_buf
+ strlen ("qSymbol:"),
1408 if (putpkt (own_buf
) < 0)
1411 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1412 len
= getpkt (own_buf
);
1416 /* We ought to handle pretty much any packet at this point while we
1417 wait for the qSymbol "response". That requires re-entering the
1418 main loop. For now, this is an adequate approximation; allow
1419 GDB to read from memory while it figures out the address of the
1421 while (own_buf
[0] == 'm')
1424 unsigned char *mem_buf
;
1425 unsigned int mem_len
;
1427 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1428 mem_buf
= xmalloc (mem_len
);
1429 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1430 bin2hex (mem_buf
, own_buf
, mem_len
);
1432 write_enn (own_buf
);
1434 if (putpkt (own_buf
) < 0)
1436 len
= getpkt (own_buf
);
1441 if (!startswith (own_buf
, "qSymbol:"))
1443 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1447 p
= own_buf
+ strlen ("qSymbol:");
1449 while (*q
&& *q
!= ':')
1452 /* Make sure we found a value for the symbol. */
1453 if (p
== q
|| *q
== '\0')
1456 decode_address (addrp
, p
, q
- p
);
1458 /* Save the symbol in our cache. */
1459 sym
= xmalloc (sizeof (*sym
));
1460 sym
->name
= xstrdup (name
);
1462 sym
->next
= proc
->symbol_cache
;
1463 proc
->symbol_cache
= sym
;
1468 /* Relocate an instruction to execute at a different address. OLDLOC
1469 is the address in the inferior memory where the instruction to
1470 relocate is currently at. On input, TO points to the destination
1471 where we want the instruction to be copied (and possibly adjusted)
1472 to. On output, it points to one past the end of the resulting
1473 instruction(s). The effect of executing the instruction at TO
1474 shall be the same as if executing it at OLDLOC. For example, call
1475 instructions that implicitly push the return address on the stack
1476 should be adjusted to return to the instruction after OLDLOC;
1477 relative branches, and other PC-relative instructions need the
1478 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1481 relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1485 ULONGEST written
= 0;
1487 /* Send the request. */
1488 strcpy (own_buf
, "qRelocInsn:");
1489 sprintf (own_buf
, "qRelocInsn:%s;%s", paddress (oldloc
),
1491 if (putpkt (own_buf
) < 0)
1494 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1495 len
= getpkt (own_buf
);
1499 /* We ought to handle pretty much any packet at this point while we
1500 wait for the qRelocInsn "response". That requires re-entering
1501 the main loop. For now, this is an adequate approximation; allow
1502 GDB to access memory. */
1503 while (own_buf
[0] == 'm' || own_buf
[0] == 'M' || own_buf
[0] == 'X')
1506 unsigned char *mem_buf
= NULL
;
1507 unsigned int mem_len
;
1509 if (own_buf
[0] == 'm')
1511 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1512 mem_buf
= xmalloc (mem_len
);
1513 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1514 bin2hex (mem_buf
, own_buf
, mem_len
);
1516 write_enn (own_buf
);
1518 else if (own_buf
[0] == 'X')
1520 if (decode_X_packet (&own_buf
[1], len
- 1, &mem_addr
,
1521 &mem_len
, &mem_buf
) < 0
1522 || write_inferior_memory (mem_addr
, mem_buf
, mem_len
) != 0)
1523 write_enn (own_buf
);
1529 decode_M_packet (&own_buf
[1], &mem_addr
, &mem_len
, &mem_buf
);
1530 if (write_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1533 write_enn (own_buf
);
1536 if (putpkt (own_buf
) < 0)
1538 len
= getpkt (own_buf
);
1543 if (own_buf
[0] == 'E')
1545 warning ("An error occurred while relocating an instruction: %s\n",
1550 if (!startswith (own_buf
, "qRelocInsn:"))
1552 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1557 unpack_varlen_hex (own_buf
+ strlen ("qRelocInsn:"), &written
);
1564 monitor_output (const char *msg
)
1566 int len
= strlen (msg
);
1567 char *buf
= xmalloc (len
* 2 + 2);
1570 bin2hex ((const gdb_byte
*) msg
, buf
+ 1, len
);