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, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include "pub_core_basics.h"
25 #include "pub_core_vki.h"
26 #include "pub_core_vkiscnums.h"
27 #include "pub_core_libcsignal.h"
28 #include "pub_core_options.h"
32 # if defined(VGO_linux)
33 #include <sys/prctl.h>
38 static int readchar (int single);
40 void remote_utils_output_status(void);
42 static int remote_desc;
44 static VgdbShared *shared;
45 static int last_looked_cntr = -1;
46 static struct vki_pollfd remote_desc_pollfdread_activity;
47 #define INVALID_DESCRIPTOR -1
49 /* for a gdbserver embedded in valgrind, we read from a FIFO and write
50 to another FIFO So, we need two descriptors */
51 static int write_remote_desc = INVALID_DESCRIPTOR;
52 static int pid_from_to_creator;
53 /* only this pid will remove the FIFOs: if an exec fails, we must avoid
54 that the exiting child believes it has to remove the FIFOs of its parent */
55 static int mknod_done = 0;
57 static char *from_gdb = NULL;
58 static char *to_gdb = NULL;
59 static char *shared_mem = NULL;
62 int open_fifo (char *side, char *path, int flags)
66 dlog(1, "Opening %s side %s\n", side, path);
67 o = VG_(open) (path, flags, 0);
69 sr_perror(o, "open fifo %s\n", path);
70 fatal ("valgrind: fatal error: vgdb FIFO cannot be opened.\n");
73 dlog(1, "result fd %d\n", fd);
75 fd = VG_(safe_fd)(fd);
76 dlog(1, "result safe_fd %d\n", fd);
78 fatal("safe_fd for vgdb FIFO failed\n");
82 void remote_utils_output_status(void)
85 VG_(umsg)("remote communication not initialized\n");
87 VG_(umsg)("shared->written_by_vgdb %d shared->seen_by_valgrind %d\n",
88 shared->written_by_vgdb, shared->seen_by_valgrind);
91 /* Returns 0 if vgdb and connection state looks good,
92 otherwise returns an int value telling which check failed. */
94 int vgdb_state_looks_bad(char* where)
96 if (VG_(kill)(shared->vgdb_pid, 0) != 0)
97 return 1; // vgdb process does not exist anymore.
99 if (remote_desc_activity(where) == 2)
100 return 2; // check for error on remote desc shows a problem
102 if (remote_desc == INVALID_DESCRIPTOR)
103 return 3; // after check, remote_desc not ok anymore
105 return 0; // all is ok.
108 /* On systems that defines PR_SET_PTRACER, verify if ptrace_scope is
109 is permissive enough for vgdb. Otherwise, call set_ptracer.
110 This is especially aimed at Ubuntu >= 10.10 which has added
111 the ptrace_scope context. */
113 void set_ptracer(void)
115 #ifdef PR_SET_PTRACER
117 char *ptrace_scope_setting_file = "/proc/sys/kernel/yama/ptrace_scope";
122 o = VG_(open) (ptrace_scope_setting_file, VKI_O_RDONLY, 0);
124 if (VG_(debugLog_getLevel)() >= 1) {
125 sr_perror(o, "error VG_(open) %s\n", ptrace_scope_setting_file);
127 /* can't read setting. Assuming ptrace can be called by vgdb. */
131 if (VG_(read) (fd, &ptrace_scope, 1) == 1) {
132 dlog(1, "ptrace_scope %c\n", ptrace_scope);
133 if (ptrace_scope != '0') {
134 /* insufficient default ptrace_scope.
135 Indicate to the kernel that we accept to be
136 ptraced by our vgdb. */
137 ret = VG_(prctl) (PR_SET_PTRACER, shared->vgdb_pid, 0, 0, 0);
138 dlog(1, "set_ptracer to vgdb_pid %d result %d\n",
139 shared->vgdb_pid, ret);
142 dlog(0, "Could not read the ptrace_scope setting from %s\n",
143 ptrace_scope_setting_file);
150 /* returns 1 if one or more poll "errors" is set.
151 Errors are: VKI_POLLERR or VKI_POLLHUP or VKI_POLLNAL */
153 int poll_cond (short revents)
155 return (revents & (VKI_POLLERR | VKI_POLLHUP | VKI_POLLNVAL));
158 /* Ensures we have a valid write file descriptor.
159 Returns 1 if we have a valid write file descriptor,
160 0 if the write fd could not be opened. */
162 int ensure_write_remote_desc(void)
164 struct vki_pollfd write_remote_desc_ok;
166 if (write_remote_desc != INVALID_DESCRIPTOR) {
167 write_remote_desc_ok.fd = write_remote_desc;
168 write_remote_desc_ok.events = VKI_POLLOUT;
169 write_remote_desc_ok.revents = 0;
170 ret = VG_(poll)(&write_remote_desc_ok, 1, 0);
171 if (ret && poll_cond(write_remote_desc_ok.revents)) {
172 dlog(1, "POLLcond %d closing write_remote_desc %d\n",
173 write_remote_desc_ok.revents, write_remote_desc);
174 VG_(close) (write_remote_desc);
175 write_remote_desc = INVALID_DESCRIPTOR;
178 if (write_remote_desc == INVALID_DESCRIPTOR) {
179 /* open_fifo write will block if the receiving vgdb
180 process is dead. So, let's check for vgdb state to
181 be reasonably sure someone is reading on the other
183 if (!vgdb_state_looks_bad("bad?@ensure_write_remote_desc")) {
185 write_remote_desc = open_fifo ("write", to_gdb, VKI_O_WRONLY);
189 return (write_remote_desc != INVALID_DESCRIPTOR);
192 #if defined(VGO_darwin)
193 #define VKI_S_IFIFO 0010000
196 void safe_mknod (char *nod)
199 m = VG_(mknod) (nod, VKI_S_IFIFO|0666, 0);
200 if (sr_isError (m)) {
201 if (sr_Err (m) == VKI_EEXIST) {
202 if (VG_(clo_verbosity) > 1) {
203 VG_(umsg)("%s already created\n", nod);
206 sr_perror(m, "mknod %s\n", nod);
207 VG_(umsg) ("valgrind: fatal error: vgdb FIFOs cannot be created.\n");
213 /* Open a connection to a remote debugger.
214 NAME is the filename used for communication.
215 For Valgrind, name is the prefix for the two read and write FIFOs
216 The two FIFOs names will be build by appending
217 -from-vgdb-to-pid and -to-vgdb-from-pid
218 with pid being the pidnr of the valgrind process These two FIFOs
219 will be created if not existing yet. They will be removed when
220 the gdbserver connection is closed or the process exits */
222 void remote_open (char *name)
224 int save_fcntl_flags;
225 VgdbShared vgdbinit =
226 {0, 0, 0, (Addr) VG_(invoke_gdbserver),
227 (Addr) VG_(threads), sizeof(ThreadState),
228 offsetof(ThreadState, status),
229 offsetof(ThreadState, os_state) + offsetof(ThreadOSstate, lwpid)};
230 const int pid = VG_(getpid)();
231 const int name_default = strcmp(name, VG_CLO_VGDB_PREFIX_DEFAULT) == 0;
234 int shared_mem_fd = INVALID_DESCRIPTOR;
236 if (from_gdb != NULL)
238 from_gdb = malloc (strlen(name) + 30);
241 to_gdb = malloc (strlen(name) + 30);
242 if (shared_mem != NULL)
244 shared_mem = malloc (strlen(name) + 30);
245 /* below 3 lines must match the equivalent in vgdb.c */
246 VG_(sprintf) (from_gdb, "%s-from-vgdb-to-%d", name, pid);
247 VG_(sprintf) (to_gdb, "%s-to-vgdb-from-%d", name, pid);
248 VG_(sprintf) (shared_mem, "%s-shared-mem-vgdb-%d", name, pid);
249 if (VG_(clo_verbosity) > 1) {
250 VG_(umsg)("embedded gdbserver: reading from %s\n", from_gdb);
251 VG_(umsg)("embedded gdbserver: writing to %s\n", to_gdb);
252 VG_(umsg)("embedded gdbserver: shared mem %s\n", shared_mem);
253 VG_(umsg)("CONTROL ME using: vgdb --pid=%d%s%s ...command...\n",
254 pid, (name_default ? "" : " --vgdb="),
255 (name_default ? "" : name));
256 VG_(umsg)("DEBUG ME using: (gdb) target remote | vgdb --pid=%d%s%s\n",
257 pid, (name_default ? "" : " --vgdb="),
258 (name_default ? "" : name));
259 VG_(umsg)(" --pid optional if only one valgrind process is running\n");
264 safe_mknod(from_gdb);
267 pid_from_to_creator = pid;
269 o = VG_(open) (shared_mem, VKI_O_CREAT|VKI_O_RDWR, 0666);
270 if (sr_isError (o)) {
271 sr_perror(o, "cannot create shared_mem file %s\n", shared_mem);
274 shared_mem_fd = sr_Res(o);
277 if (VG_(write)(shared_mem_fd, &vgdbinit, sizeof(VgdbShared))
278 != sizeof(VgdbShared)) {
279 fatal("error writing %d bytes to shared mem %s\n",
280 (int) sizeof(VgdbShared), shared_mem);
282 shared_mem_fd = VG_(safe_fd)(shared_mem_fd);
283 if (shared_mem_fd == -1) {
284 fatal("safe_fd for vgdb shared_mem %s failed\n", shared_mem);
287 SysRes res = VG_(am_shared_mmap_file_float_valgrind)
288 (sizeof(VgdbShared), VKI_PROT_READ|VKI_PROT_WRITE,
289 shared_mem_fd, (Off64T)0);
290 if (sr_isError(res)) {
291 sr_perror(res, "error VG_(am_shared_mmap_file_float_valgrind) %s\n",
295 addr_shared = sr_Res (res);
297 shared = (VgdbShared*) addr_shared;
300 /* we open the read side FIFO in non blocking mode
301 We then set the fd in blocking mode.
302 Opening in non-blocking read mode always succeeds while opening
303 in non-blocking write mode succeeds only if the fifo is already
304 opened in read mode. So, we wait till we have read the first
305 character from the read side before opening the write side. */
306 remote_desc = open_fifo ("read", from_gdb, VKI_O_RDONLY|VKI_O_NONBLOCK);
307 save_fcntl_flags = VG_(fcntl) (remote_desc, VKI_F_GETFL, 0);
308 VG_(fcntl) (remote_desc, VKI_F_SETFL, save_fcntl_flags & ~VKI_O_NONBLOCK);
309 remote_desc_pollfdread_activity.fd = remote_desc;
310 remote_desc_pollfdread_activity.events = VKI_POLLIN;
311 remote_desc_pollfdread_activity.revents = 0;
314 /* sync_gdb_connection wait a time long enough to let the connection
315 be properly closed if needed when closing the connection (in case
316 of detach or error), if we reopen it too quickly, it seems there
317 are some events queued in the kernel concerning the "old"
318 connection/remote_desc which are discovered with poll or select on
319 the "new" connection/remote_desc. We bypass this by waiting some
320 time to let a proper cleanup to be donex */
321 void sync_gdb_connection(void)
323 VG_(poll)(0, 0, 100);
327 char * ppFinishReason (FinishReason reason)
330 case orderly_finish: return "orderly_finish";
331 case reset_after_error: return "reset_after_error";
332 case reset_after_fork: return "reset_after_fork";
333 default: vg_assert (0);
337 void remote_finish (FinishReason reason)
339 dlog(1, "remote_finish (reason %s) %d %d\n",
340 ppFinishReason(reason), remote_desc, write_remote_desc);
341 reset_valgrind_sink(ppFinishReason(reason));
342 if (write_remote_desc != INVALID_DESCRIPTOR)
343 VG_(close) (write_remote_desc);
344 write_remote_desc = INVALID_DESCRIPTOR;
345 if (remote_desc != INVALID_DESCRIPTOR) {
346 remote_desc_pollfdread_activity.fd = INVALID_DESCRIPTOR;
347 remote_desc_pollfdread_activity.events = 0;
348 remote_desc_pollfdread_activity.revents = 0;
349 VG_(close) (remote_desc);
351 remote_desc = INVALID_DESCRIPTOR;
354 /* ensure the child will create its own FIFOs */
355 if (reason == reset_after_fork)
358 if (reason == reset_after_error)
359 sync_gdb_connection();
362 /* orderly close, cleans up everything */
363 void remote_close (void)
365 const int pid = VG_(getpid)();
366 remote_finish(orderly_finish);
367 if (pid == pid_from_to_creator) {
368 dlog(1, "unlinking\n %s\n %s\n %s\n",
369 from_gdb, to_gdb, shared_mem);
370 if (VG_(unlink) (from_gdb) == -1)
371 warning ("could not unlink %s\n", from_gdb);
372 if (VG_(unlink) (to_gdb) == -1)
373 warning ("could not unlink %s\n", to_gdb);
374 if (VG_(unlink) (shared_mem) == -1)
375 warning ("could not unlink %s\n", shared_mem);
378 dlog(1, "not creator => not unlinking %s and %s\n", from_gdb, to_gdb);
384 Bool remote_connected(void)
386 return write_remote_desc != INVALID_DESCRIPTOR;
389 /* cleanup after an error detected by poll_cond */
391 void error_poll_cond(void)
393 /* if we will close the connection, we assume either that
394 all characters have been seen or that they will be dropped. */
395 shared->seen_by_valgrind = shared->written_by_vgdb;
396 remote_finish(reset_after_error);
399 /* remote_desc_activity might be used at high frequency if the user
400 gives a small value to --vgdb-poll. So, the function avoids
401 doing repetitively system calls by rather looking at the
402 counter values maintained in shared memory by vgdb. */
403 int remote_desc_activity(char *msg)
406 const int looking_at = shared->written_by_vgdb;
407 if (shared->seen_by_valgrind == looking_at)
408 // if (last_looked_cntr == looking_at)
410 if (remote_desc == INVALID_DESCRIPTOR)
413 /* poll the remote desc */
414 remote_desc_pollfdread_activity.revents = 0;
415 ret = VG_(poll) (&remote_desc_pollfdread_activity, 1, 0);
416 if (ret && poll_cond(remote_desc_pollfdread_activity.revents)) {
417 dlog(1, "POLLcond %d remote_desc_pollfdread %d\n",
418 remote_desc_pollfdread_activity.revents, remote_desc);
423 "remote_desc_activity %s %d last_looked_cntr %d looking_at %d"
424 " shared->written_by_vgdb %d shared->seen_by_valgrind %d"
426 msg, remote_desc, last_looked_cntr, looking_at,
427 shared->written_by_vgdb, shared->seen_by_valgrind,
429 /* if no error from poll, indicate we have "seen" up to looking_at */
431 last_looked_cntr = looking_at;
435 /* Convert hex digit A to a number. */
440 if (a >= '0' && a <= '9')
442 else if (a >= 'a' && a <= 'f')
445 error ("Reply contains invalid hex digit 0x%x\n", a);
449 int unhexify (char *bin, const char *hex, int count)
453 for (i = 0; i < count; i++) {
454 if (hex[0] == 0 || hex[1] == 0) {
455 /* Hex string is short, or of uneven length.
456 Return the count that has been converted so far. */
459 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
465 void decode_address (CORE_ADDR *addrp, const char *start, int len)
472 for (i = 0; i < len; i++) {
475 addr = addr | (fromhex (ch) & 0x0f);
480 /* Convert number NIB to a hex digit. */
488 return 'a' + nib - 10;
491 int hexify (char *hex, const char *bin, int count)
495 /* May use a length, or a nul-terminated string as input. */
497 count = strlen (bin);
499 for (i = 0; i < count; i++) {
500 *hex++ = tohex ((*bin >> 4) & 0xf);
501 *hex++ = tohex (*bin++ & 0xf);
507 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
508 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
509 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
510 (which may be more than *OUT_LEN due to escape characters). The
511 total number of bytes in the output buffer will be at most
515 remote_escape_output (const gdb_byte *buffer, int len,
516 gdb_byte *out_buf, int *out_len,
519 int input_index, output_index;
522 for (input_index = 0; input_index < len; input_index++) {
523 gdb_byte b = buffer[input_index];
525 if (b == '$' || b == '#' || b == '}' || b == '*') {
526 /* These must be escaped. */
527 if (output_index + 2 > out_maxlen)
529 out_buf[output_index++] = '}';
530 out_buf[output_index++] = b ^ 0x20;
532 if (output_index + 1 > out_maxlen)
534 out_buf[output_index++] = b;
538 *out_len = input_index;
542 /* Convert BUFFER, escaped data LEN bytes long, into binary data
543 in OUT_BUF. Return the number of bytes written to OUT_BUF.
544 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
546 This function reverses remote_escape_output. It allows more
547 escaped characters than that function does, in particular because
548 '*' must be escaped to avoid the run-length encoding processing
549 in reading packets. */
552 int remote_unescape_input (const gdb_byte *buffer, int len,
553 gdb_byte *out_buf, int out_maxlen)
555 int input_index, output_index;
560 for (input_index = 0; input_index < len; input_index++) {
561 gdb_byte b = buffer[input_index];
563 if (output_index + 1 > out_maxlen)
564 error ("Received too much data (len %d) from the target.\n", len);
567 out_buf[output_index++] = b ^ 0x20;
569 } else if (b == '}') {
572 out_buf[output_index++] = b;
577 error ("Unmatched escape character in target response.\n");
582 /* Look for a sequence of characters which can be run-length encoded.
583 If there are any, update *CSUM and *P. Otherwise, output the
584 single character. Return the number of characters consumed. */
587 int try_rle (char *buf, int remaining, unsigned char *csum, char **p)
591 /* Always output the character. */
595 /* Don't go past '~'. */
599 for (n = 1; n < remaining; n++)
600 if (buf[n] != buf[0])
603 /* N is the index of the first character not the same as buf[0].
604 buf[0] is counted twice, so by decrementing N, we get the number
605 of characters the RLE sequence will replace. */
611 /* Skip the frame characters. The manual says to skip '+' and '-'
612 also, but there's no reason to. Unfortunately these two unusable
613 characters double the encoded length of a four byte zero
615 while (n + 29 == '$' || n + 29 == '#')
626 /* Send a packet to the remote machine, with error checking.
627 The data of the packet is in BUF, and the length of the
628 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
630 int putpkt_binary (char *buf, int cnt)
633 unsigned char csum = 0;
638 buf2 = malloc (PBUFSIZ);
640 /* Copy the packet into buffer BUF2, encapsulating it
641 and giving it a checksum. */
646 for (i = 0; i < cnt;)
647 i += try_rle (buf + i, cnt - i, &csum, &p);
650 *p++ = tohex ((csum >> 4) & 0xf);
651 *p++ = tohex (csum & 0xf);
655 /* we might have to write a pkt when out FIFO not yet/anymore opened */
656 if (!ensure_write_remote_desc()) {
657 warning ("putpkt(write) error: no write_remote_desc\n");
661 /* Send it once (noack_mode)
662 or send it over and over until we get a positive ack. */
665 if (VG_(write) (write_remote_desc, buf2, p - buf2) != p - buf2) {
666 warning ("putpkt(write) error\n");
671 dlog(1, "putpkt (\"%s\"); [no ack]\n", buf2);
673 dlog(1,"putpkt (\"%s\"); [looking for ack]\n", buf2);
680 dlog(1, "[received '%c' (0x%x)]\n", cc, cc);
684 dlog(1, "putpkt(read): Got EOF\n");
686 warning ("putpkt(read) error\n");
692 /* Check for an input interrupt while we're here. */
694 (*the_target->send_signal) (VKI_SIGINT);
699 return 1; /* Success! */
702 /* Send a packet to the remote machine, with error checking. The data
703 of the packet is in BUF, and the packet should be a NUL-terminated
704 string. Returns >= 0 on success, -1 otherwise. */
706 int putpkt (char *buf)
708 return putpkt_binary (buf, strlen (buf));
711 void monitor_output (char *s)
713 const int len = strlen(s);
714 char *buf = malloc(1 + 2*len + 1);
717 hexify(buf+1, s, len);
718 if (putpkt (buf) < 0) {
719 /* We probably have lost the connection with vgdb. */
720 reset_valgrind_sink("Error writing monitor output");
721 /* write again after reset */
722 VG_(printf) ("%s", s);
728 /* Returns next char from remote GDB. -1 if error. */
729 /* if single, only one character maximum can be read with
730 read system call. Otherwise, when reading an ack character
731 we might pile up the next gdb command in the static buf.
732 The read loop is then blocked in poll till gdb times out. */
734 int readchar (int single)
736 static unsigned char buf[PBUFSIZ];
737 static int bufcnt = 0;
738 static unsigned char *bufp;
744 if (remote_desc == INVALID_DESCRIPTOR)
747 /* No characters available in buf =>
748 wait for some characters to arrive */
749 remote_desc_pollfdread_activity.revents = 0;
750 ret = VG_(poll)(&remote_desc_pollfdread_activity, 1, -1);
752 dlog(0, "readchar: poll got %d\n", ret);
756 bufcnt = VG_(read) (remote_desc, buf, 1);
758 bufcnt = VG_(read) (remote_desc, buf, sizeof (buf));
762 dlog (1, "readchar: Got EOF\n");
764 warning ("readchar read error\n");
769 shared->seen_by_valgrind += bufcnt;
771 /* If we have received a character and we do not yet have a
772 connection, we better open our "write" fifo to let vgdb open its
774 if (write_remote_desc == INVALID_DESCRIPTOR
775 && !ensure_write_remote_desc()) {
776 dlog(1, "reachar: write_remote_desc could not be created");
782 if (poll_cond(remote_desc_pollfdread_activity.revents)) {
783 dlog(1, "readchar: POLLcond got %d\n",
784 remote_desc_pollfdread_activity.revents);
792 /* Read a packet from the remote machine, with error checking,
793 and store it in BUF. Returns length of packet, or negative if error. */
795 int getpkt (char *buf)
798 unsigned char csum, c1, c2;
808 dlog(1, "[getpkt: discarding char '%c']\n", c);
825 c1 = fromhex (readchar (0));
826 c2 = fromhex (readchar (0));
828 if (csum == (c1 << 4) + c2)
831 dlog (0, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
832 (c1 << 4) + c2, csum, buf);
833 if (!ensure_write_remote_desc()) {
834 dlog(1, "getpkt(write nack) no write_remote_desc");
836 VG_(write) (write_remote_desc, "-", 1);
840 dlog(1, "getpkt (\"%s\"); [no ack] \n", buf);
842 dlog(1, "getpkt (\"%s\"); [sending ack] \n", buf);
845 if (!ensure_write_remote_desc()) {
846 dlog(1, "getpkt(write ack) no write_remote_desc");
848 VG_(write) (write_remote_desc, "+", 1);
849 dlog(1, "[sent ack]\n");
855 void write_ok (char *buf)
862 void write_enn (char *buf)
864 /* Some day, we should define the meanings of the error codes... */
871 void convert_int_to_ascii (unsigned char *from, char *to, int n)
877 nib = ((ch & 0xf0) >> 4) & 0x0f;
886 void convert_ascii_to_int (char *from, unsigned char *to, int n)
890 nib1 = fromhex (*from++);
891 nib2 = fromhex (*from++);
892 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
897 char * outreg (int regno, char *buf)
899 if ((regno >> 12) != 0)
900 *buf++ = tohex ((regno >> 12) & 0xf);
901 if ((regno >> 8) != 0)
902 *buf++ = tohex ((regno >> 8) & 0xf);
903 *buf++ = tohex ((regno >> 4) & 0xf);
904 *buf++ = tohex (regno & 0xf);
906 collect_register_as_string (regno, buf);
907 buf += 2 * register_size (regno);
913 void prepare_resume_reply (char *buf, char status, unsigned char sig)
919 nib = ((sig & 0xf0) >> 4);
920 *buf++ = tohex (nib);
922 *buf++ = tohex (nib);
925 const char **regp = gdbserver_expedite_regs;
927 if (the_target->stopped_by_watchpoint != NULL
928 && (*the_target->stopped_by_watchpoint) ()) {
932 strncpy (buf, "watch:", 6);
935 addr = (*the_target->stopped_data_address) ();
937 /* Convert each byte of the address into two hexadecimal chars.
938 Note that we take sizeof (void *) instead of sizeof (addr);
939 this is to avoid sending a 64-bit address to a 32-bit GDB. */
940 for (i = sizeof (void *) * 2; i > 0; i--) {
941 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
947 buf = outreg (find_regno (*regp), buf);
952 unsigned int gdb_id_from_wait;
954 /* FIXME right place to set this? */
956 ((struct inferior_list_entry *)current_inferior)->id;
957 gdb_id_from_wait = thread_to_gdb_id (current_inferior);
959 dlog(1, "Writing resume reply for %ld\n", thread_from_wait);
960 /* This if (1) ought to be unnecessary. But remote_wait in GDB
961 will claim this event belongs to inferior_ptid if we do not
962 specify a thread, and there's no way for gdbserver to know
963 what inferior_ptid is. */
964 if (1 || old_thread_from_wait != thread_from_wait) {
965 general_thread = thread_from_wait;
966 VG_(sprintf) (buf, "thread:%x;", gdb_id_from_wait);
968 old_thread_from_wait = thread_from_wait;
972 /* For W and X, we're done. */
976 void decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
980 *mem_addr_ptr = *len_ptr = 0;
982 while ((ch = from[i++]) != ',') {
983 *mem_addr_ptr = *mem_addr_ptr << 4;
984 *mem_addr_ptr |= fromhex (ch) & 0x0f;
987 for (j = 0; j < 4; j++) {
988 if ((ch = from[i++]) == 0)
990 *len_ptr = *len_ptr << 4;
991 *len_ptr |= fromhex (ch) & 0x0f;
995 void decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
1000 *mem_addr_ptr = *len_ptr = 0;
1002 while ((ch = from[i++]) != ',') {
1003 *mem_addr_ptr = *mem_addr_ptr << 4;
1004 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1007 while ((ch = from[i++]) != ':') {
1008 *len_ptr = *len_ptr << 4;
1009 *len_ptr |= fromhex (ch) & 0x0f;
1012 convert_ascii_to_int (&from[i++], to, *len_ptr);
1015 int decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1016 unsigned int *len_ptr, unsigned char *to)
1020 *mem_addr_ptr = *len_ptr = 0;
1022 while ((ch = from[i++]) != ',') {
1023 *mem_addr_ptr = *mem_addr_ptr << 4;
1024 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1027 while ((ch = from[i++]) != ':') {
1028 *len_ptr = *len_ptr << 4;
1029 *len_ptr |= fromhex (ch) & 0x0f;
1032 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1033 to, *len_ptr) != *len_ptr)