2012-11-15 Pierre Muller <muller@sourceware.org>
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
CommitLineData
c906108c 1/* Remote utility routines for the remote server for GDB.
0b302171 2 Copyright (C) 1986, 1989, 1993-2012 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19#include "server.h"
20#include "terminal.h"
5b1c542e 21#include "target.h"
623b6bdf 22#include "gdbthread.h"
c906108c
SS
23#include <stdio.h>
24#include <string.h>
b80864fb 25#if HAVE_SYS_IOCTL_H
c906108c 26#include <sys/ioctl.h>
b80864fb 27#endif
68070c10 28#if HAVE_SYS_FILE_H
c906108c 29#include <sys/file.h>
68070c10 30#endif
b80864fb 31#if HAVE_NETINET_IN_H
c906108c 32#include <netinet/in.h>
b80864fb
DJ
33#endif
34#if HAVE_SYS_SOCKET_H
c906108c 35#include <sys/socket.h>
b80864fb
DJ
36#endif
37#if HAVE_NETDB_H
c906108c 38#include <netdb.h>
b80864fb
DJ
39#endif
40#if HAVE_NETINET_TCP_H
c906108c 41#include <netinet/tcp.h>
b80864fb
DJ
42#endif
43#if HAVE_SYS_IOCTL_H
c906108c 44#include <sys/ioctl.h>
b80864fb 45#endif
68070c10 46#if HAVE_SIGNAL_H
c906108c 47#include <signal.h>
68070c10
PA
48#endif
49#if HAVE_FCNTL_H
c906108c 50#include <fcntl.h>
68070c10 51#endif
cf30a8e1 52#include <sys/time.h>
68070c10 53#if HAVE_UNISTD_H
cf30a8e1 54#include <unistd.h>
68070c10 55#endif
b80864fb 56#if HAVE_ARPA_INET_H
0729219d 57#include <arpa/inet.h>
b80864fb 58#endif
8bdce1ff 59#include "gdb_stat.h"
68070c10 60#if HAVE_ERRNO_H
8264bb58 61#include <errno.h>
68070c10 62#endif
b80864fb
DJ
63
64#if USE_WIN32API
12ea4b69 65#include <winsock2.h>
b80864fb 66#endif
c906108c 67
ac8c974e
AR
68#if __QNX__
69#include <sys/iomgr.h>
70#endif /* __QNX__ */
71
f450004a
DJ
72#ifndef HAVE_SOCKLEN_T
73typedef int socklen_t;
74#endif
75
0fb4aa4b
PA
76#ifndef IN_PROCESS_AGENT
77
7390519e
PA
78#if USE_WIN32API
79# define INVALID_DESCRIPTOR INVALID_SOCKET
80#else
81# define INVALID_DESCRIPTOR -1
82#endif
83
24b066ba
DE
84/* Extra value for readchar_callback. */
85enum {
86 /* The callback is currently not scheduled. */
87 NOT_SCHEDULED = -1
88};
89
90/* Status of the readchar callback.
91 Either NOT_SCHEDULED or the callback id. */
92static int readchar_callback = NOT_SCHEDULED;
93
bc3b5632 94static int readchar (void);
24b066ba
DE
95static void reset_readchar (void);
96static void reschedule (void);
bc3b5632 97
fd500816
DJ
98/* A cache entry for a successfully looked-up symbol. */
99struct sym_cache
100{
95954743 101 char *name;
fd500816
DJ
102 CORE_ADDR addr;
103 struct sym_cache *next;
104};
105
c906108c 106int remote_debug = 0;
03863182 107struct ui_file *gdb_stdlog;
c906108c 108
e0f9f062
DE
109static int remote_is_stdio = 0;
110
ec48365d
PA
111static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR;
112static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR;
c906108c 113
0d62e5e8
DJ
114/* FIXME headerize? */
115extern int using_threads;
116extern int debug_threads;
117
a6f3e723
SL
118/* If true, then GDB has requested noack mode. */
119int noack_mode = 0;
120/* If true, then we tell GDB to use noack mode by default. */
121int transport_is_reliable = 0;
122
0f48aa01 123#ifdef USE_WIN32API
68070c10
PA
124# define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
125# define write(fd, buf, len) send (fd, (char *) buf, len, 0)
0f48aa01
DJ
126#endif
127
8336d594
PA
128int
129gdb_connected (void)
130{
131 return remote_desc != INVALID_DESCRIPTOR;
132}
133
e0f9f062
DE
134/* Return true if the remote connection is over stdio. */
135
136int
137remote_connection_is_stdio (void)
138{
139 return remote_is_stdio;
140}
141
8336d594
PA
142static void
143enable_async_notification (int fd)
144{
145#if defined(F_SETFL) && defined (FASYNC)
146 int save_fcntl_flags;
147
148 save_fcntl_flags = fcntl (fd, F_GETFL, 0);
149 fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
150#if defined (F_SETOWN)
151 fcntl (fd, F_SETOWN, getpid ());
152#endif
153#endif
154}
155
156static int
157handle_accept_event (int err, gdb_client_data client_data)
158{
159 struct sockaddr_in sockaddr;
160 socklen_t tmp;
161
162 if (debug_threads)
163 fprintf (stderr, "handling possible accept event\n");
164
165 tmp = sizeof (sockaddr);
166 remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
167 if (remote_desc == -1)
168 perror_with_name ("Accept failed");
169
170 /* Enable TCP keep alive process. */
171 tmp = 1;
172 setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
173 (char *) &tmp, sizeof (tmp));
174
175 /* Tell TCP not to delay small packets. This greatly speeds up
176 interactive response. */
177 tmp = 1;
178 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
179 (char *) &tmp, sizeof (tmp));
180
181#ifndef USE_WIN32API
8336d594
PA
182 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
183 exits when the remote side dies. */
03f2bd59
JK
184#endif
185
186 if (run_once)
187 {
188#ifndef USE_WIN32API
189 close (listen_desc); /* No longer need this */
8336d594 190#else
03f2bd59 191 closesocket (listen_desc); /* No longer need this */
8336d594 192#endif
03f2bd59 193 }
8336d594 194
03f2bd59
JK
195 /* Even if !RUN_ONCE no longer notice new connections. Still keep the
196 descriptor open for add_file_handler to wait for a new connection. */
8336d594
PA
197 delete_file_handler (listen_desc);
198
199 /* Convert IP address to string. */
200 fprintf (stderr, "Remote debugging from host %s\n",
201 inet_ntoa (sockaddr.sin_addr));
202
203 enable_async_notification (remote_desc);
204
205 /* Register the event loop handler. */
206 add_file_handler (remote_desc, handle_serial_event, NULL);
207
208 /* We have a new GDB connection now. If we were disconnected
209 tracing, there's a window where the target could report a stop
210 event to the event loop, and since we have a connection now, we'd
211 try to send vStopped notifications to GDB. But, don't do that
212 until GDB as selected all-stop/non-stop, and has queried the
213 threads' status ('?'). */
214 target_async (0);
215
216 return 0;
217}
218
03f2bd59
JK
219/* Prepare for a later connection to a remote debugger.
220 NAME is the filename used for communication. */
221
222void
223remote_prepare (char *name)
224{
225 char *port_str;
226#ifdef USE_WIN32API
227 static int winsock_initialized;
228#endif
229 int port;
230 struct sockaddr_in sockaddr;
231 socklen_t tmp;
232 char *port_end;
233
e0f9f062
DE
234 remote_is_stdio = 0;
235 if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
236 {
237 /* We need to record fact that we're using stdio sooner than the
238 call to remote_open so start_inferior knows the connection is
239 via stdio. */
240 remote_is_stdio = 1;
241 transport_is_reliable = 1;
242 return;
243 }
244
03f2bd59
JK
245 port_str = strchr (name, ':');
246 if (port_str == NULL)
247 {
248 transport_is_reliable = 0;
249 return;
250 }
251
252 port = strtoul (port_str + 1, &port_end, 10);
253 if (port_str[1] == '\0' || *port_end != '\0')
254 fatal ("Bad port argument: %s", name);
255
256#ifdef USE_WIN32API
257 if (!winsock_initialized)
258 {
259 WSADATA wsad;
260
261 WSAStartup (MAKEWORD (1, 0), &wsad);
262 winsock_initialized = 1;
263 }
264#endif
265
266 listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
267 if (listen_desc == -1)
268 perror_with_name ("Can't open socket");
269
270 /* Allow rapid reuse of this port. */
271 tmp = 1;
272 setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
273 sizeof (tmp));
274
275 sockaddr.sin_family = PF_INET;
276 sockaddr.sin_port = htons (port);
277 sockaddr.sin_addr.s_addr = INADDR_ANY;
278
279 if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
280 || listen (listen_desc, 1))
281 perror_with_name ("Can't bind address");
282
283 transport_is_reliable = 1;
284}
285
c906108c
SS
286/* Open a connection to a remote debugger.
287 NAME is the filename used for communication. */
288
289void
fba45db2 290remote_open (char *name)
c906108c 291{
8264bb58
DJ
292 char *port_str;
293
294 port_str = strchr (name, ':');
e0f9f062 295#ifdef USE_WIN32API
8264bb58 296 if (port_str == NULL)
e0f9f062
DE
297 error ("Only <host>:<port> is supported on this platform.");
298#endif
299
300 if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
301 {
302 fprintf (stderr, "Remote debugging using stdio\n");
303
304 /* Use stdin as the handle of the connection.
305 We only select on reads, for example. */
306 remote_desc = fileno (stdin);
307
308 enable_async_notification (remote_desc);
309
310 /* Register the event loop handler. */
311 add_file_handler (remote_desc, handle_serial_event, NULL);
312 }
313#ifndef USE_WIN32API
314 else if (port_str == NULL)
c906108c 315 {
8264bb58
DJ
316 struct stat statbuf;
317
318 if (stat (name, &statbuf) == 0
319 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
320 remote_desc = open (name, O_RDWR);
321 else
322 {
323 errno = EINVAL;
324 remote_desc = -1;
325 }
326
c906108c
SS
327 if (remote_desc < 0)
328 perror_with_name ("Could not open remote device");
329
330#ifdef HAVE_TERMIOS
331 {
332 struct termios termios;
c5aa993b 333 tcgetattr (remote_desc, &termios);
c906108c
SS
334
335 termios.c_iflag = 0;
336 termios.c_oflag = 0;
337 termios.c_lflag = 0;
c5aa993b 338 termios.c_cflag &= ~(CSIZE | PARENB);
c906108c 339 termios.c_cflag |= CLOCAL | CS8;
d0608e50 340 termios.c_cc[VMIN] = 1;
c906108c
SS
341 termios.c_cc[VTIME] = 0;
342
c5aa993b 343 tcsetattr (remote_desc, TCSANOW, &termios);
c906108c
SS
344 }
345#endif
346
347#ifdef HAVE_TERMIO
348 {
349 struct termio termio;
350 ioctl (remote_desc, TCGETA, &termio);
351
352 termio.c_iflag = 0;
353 termio.c_oflag = 0;
354 termio.c_lflag = 0;
c5aa993b 355 termio.c_cflag &= ~(CSIZE | PARENB);
c906108c 356 termio.c_cflag |= CLOCAL | CS8;
d0608e50 357 termio.c_cc[VMIN] = 1;
c906108c
SS
358 termio.c_cc[VTIME] = 0;
359
360 ioctl (remote_desc, TCSETA, &termio);
361 }
362#endif
363
364#ifdef HAVE_SGTTY
365 {
366 struct sgttyb sg;
367
368 ioctl (remote_desc, TIOCGETP, &sg);
369 sg.sg_flags = RAW;
370 ioctl (remote_desc, TIOCSETP, &sg);
371 }
372#endif
373
e641a1ca 374 fprintf (stderr, "Remote debugging using %s\n", name);
a6f3e723 375
8336d594
PA
376 enable_async_notification (remote_desc);
377
378 /* Register the event loop handler. */
379 add_file_handler (remote_desc, handle_serial_event, NULL);
c906108c 380 }
e0f9f062 381#endif /* USE_WIN32API */
c906108c
SS
382 else
383 {
c906108c 384 int port;
03f2bd59 385 socklen_t len;
c906108c 386 struct sockaddr_in sockaddr;
c906108c 387
03f2bd59
JK
388 len = sizeof (sockaddr);
389 if (getsockname (listen_desc,
390 (struct sockaddr *) &sockaddr, &len) < 0
391 || len < sizeof (sockaddr))
392 perror_with_name ("Can't determine port");
393 port = ntohs (sockaddr.sin_port);
6f8486da 394
6910d122 395 fprintf (stderr, "Listening on port %d\n", port);
b80864fb 396 fflush (stderr);
6910d122 397
8336d594
PA
398 /* Register the event loop handler. */
399 add_file_handler (listen_desc, handle_accept_event, NULL);
c906108c 400 }
c906108c
SS
401}
402
403void
fba45db2 404remote_close (void)
c906108c 405{
bd99dc85
PA
406 delete_file_handler (remote_desc);
407
b80864fb
DJ
408#ifdef USE_WIN32API
409 closesocket (remote_desc);
410#else
e0f9f062
DE
411 if (! remote_connection_is_stdio ())
412 close (remote_desc);
b80864fb 413#endif
8336d594 414 remote_desc = INVALID_DESCRIPTOR;
24b066ba
DE
415
416 reset_readchar ();
c906108c
SS
417}
418
419/* Convert hex digit A to a number. */
420
421static int
fba45db2 422fromhex (int a)
c906108c
SS
423{
424 if (a >= '0' && a <= '9')
425 return a - '0';
426 else if (a >= 'a' && a <= 'f')
427 return a - 'a' + 10;
428 else
429 error ("Reply contains invalid hex digit");
0a30fbc4 430 return 0;
c906108c
SS
431}
432
0fb4aa4b
PA
433#endif
434
95954743
PA
435static const char hexchars[] = "0123456789abcdef";
436
437static int
438ishex (int ch, int *val)
439{
440 if ((ch >= 'a') && (ch <= 'f'))
441 {
442 *val = ch - 'a' + 10;
443 return 1;
444 }
445 if ((ch >= 'A') && (ch <= 'F'))
446 {
447 *val = ch - 'A' + 10;
448 return 1;
449 }
450 if ((ch >= '0') && (ch <= '9'))
451 {
452 *val = ch - '0';
453 return 1;
454 }
455 return 0;
456}
457
0fb4aa4b
PA
458#ifndef IN_PROCESS_AGENT
459
ce3a066d
DJ
460int
461unhexify (char *bin, const char *hex, int count)
462{
463 int i;
464
465 for (i = 0; i < count; i++)
466 {
467 if (hex[0] == 0 || hex[1] == 0)
1b3f6016
PA
468 {
469 /* Hex string is short, or of uneven length.
470 Return the count that has been converted so far. */
471 return i;
472 }
ce3a066d
DJ
473 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
474 hex += 2;
475 }
476 return i;
477}
478
dae5f5cf 479void
2f2893d9
DJ
480decode_address (CORE_ADDR *addrp, const char *start, int len)
481{
482 CORE_ADDR addr;
483 char ch;
484 int i;
485
486 addr = 0;
487 for (i = 0; i < len; i++)
488 {
489 ch = start[i];
490 addr = addr << 4;
491 addr = addr | (fromhex (ch) & 0x0f);
492 }
493 *addrp = addr;
494}
495
89be2091
DJ
496const char *
497decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
498{
499 const char *end;
500
501 end = start;
502 while (*end != '\0' && *end != ';')
503 end++;
504
505 decode_address (addrp, start, end - start);
506
507 if (*end == ';')
508 end++;
509 return end;
510}
511
0fb4aa4b
PA
512#endif
513
c906108c
SS
514/* Convert number NIB to a hex digit. */
515
516static int
fba45db2 517tohex (int nib)
c906108c
SS
518{
519 if (nib < 10)
520 return '0' + nib;
521 else
522 return 'a' + nib - 10;
523}
524
0fb4aa4b
PA
525#ifndef IN_PROCESS_AGENT
526
ce3a066d
DJ
527int
528hexify (char *hex, const char *bin, int count)
529{
530 int i;
531
532 /* May use a length, or a nul-terminated string as input. */
533 if (count == 0)
534 count = strlen (bin);
535
536 for (i = 0; i < count; i++)
537 {
538 *hex++ = tohex ((*bin >> 4) & 0xf);
539 *hex++ = tohex (*bin++ & 0xf);
540 }
541 *hex = 0;
542 return i;
543}
544
01f9e8fa
DJ
545/* Convert BUFFER, binary data at least LEN bytes long, into escaped
546 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
547 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
548 (which may be more than *OUT_LEN due to escape characters). The
549 total number of bytes in the output buffer will be at most
550 OUT_MAXLEN. */
551
552int
553remote_escape_output (const gdb_byte *buffer, int len,
554 gdb_byte *out_buf, int *out_len,
555 int out_maxlen)
556{
557 int input_index, output_index;
558
559 output_index = 0;
560 for (input_index = 0; input_index < len; input_index++)
561 {
562 gdb_byte b = buffer[input_index];
563
564 if (b == '$' || b == '#' || b == '}' || b == '*')
565 {
566 /* These must be escaped. */
567 if (output_index + 2 > out_maxlen)
568 break;
569 out_buf[output_index++] = '}';
570 out_buf[output_index++] = b ^ 0x20;
571 }
572 else
573 {
574 if (output_index + 1 > out_maxlen)
575 break;
576 out_buf[output_index++] = b;
577 }
578 }
579
580 *out_len = input_index;
581 return output_index;
582}
583
584/* Convert BUFFER, escaped data LEN bytes long, into binary data
585 in OUT_BUF. Return the number of bytes written to OUT_BUF.
586 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
587
588 This function reverses remote_escape_output. It allows more
589 escaped characters than that function does, in particular because
590 '*' must be escaped to avoid the run-length encoding processing
591 in reading packets. */
592
593static int
594remote_unescape_input (const gdb_byte *buffer, int len,
595 gdb_byte *out_buf, int out_maxlen)
596{
597 int input_index, output_index;
598 int escaped;
599
600 output_index = 0;
601 escaped = 0;
602 for (input_index = 0; input_index < len; input_index++)
603 {
604 gdb_byte b = buffer[input_index];
605
606 if (output_index + 1 > out_maxlen)
607 error ("Received too much data from the target.");
608
609 if (escaped)
610 {
611 out_buf[output_index++] = b ^ 0x20;
612 escaped = 0;
613 }
614 else if (b == '}')
615 escaped = 1;
616 else
617 out_buf[output_index++] = b;
618 }
619
620 if (escaped)
621 error ("Unmatched escape character in target response.");
622
623 return output_index;
624}
625
5ffff7c1
DJ
626/* Look for a sequence of characters which can be run-length encoded.
627 If there are any, update *CSUM and *P. Otherwise, output the
628 single character. Return the number of characters consumed. */
629
630static int
631try_rle (char *buf, int remaining, unsigned char *csum, char **p)
632{
633 int n;
634
635 /* Always output the character. */
636 *csum += buf[0];
637 *(*p)++ = buf[0];
638
639 /* Don't go past '~'. */
640 if (remaining > 97)
641 remaining = 97;
642
643 for (n = 1; n < remaining; n++)
644 if (buf[n] != buf[0])
645 break;
646
647 /* N is the index of the first character not the same as buf[0].
648 buf[0] is counted twice, so by decrementing N, we get the number
649 of characters the RLE sequence will replace. */
650 n--;
651
652 if (n < 3)
653 return 1;
654
655 /* Skip the frame characters. The manual says to skip '+' and '-'
656 also, but there's no reason to. Unfortunately these two unusable
657 characters double the encoded length of a four byte zero
658 value. */
659 while (n + 29 == '$' || n + 29 == '#')
660 n--;
661
662 *csum += '*';
663 *(*p)++ = '*';
664 *csum += n + 29;
665 *(*p)++ = n + 29;
666
667 return n + 1;
668}
669
0fb4aa4b
PA
670#endif
671
95954743
PA
672char *
673unpack_varlen_hex (char *buff, /* packet to parse */
674 ULONGEST *result)
675{
676 int nibble;
677 ULONGEST retval = 0;
678
679 while (ishex (*buff, &nibble))
680 {
681 buff++;
682 retval = retval << 4;
683 retval |= nibble & 0x0f;
684 }
685 *result = retval;
686 return buff;
687}
688
0fb4aa4b
PA
689#ifndef IN_PROCESS_AGENT
690
95954743
PA
691/* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
692
693char *
694write_ptid (char *buf, ptid_t ptid)
695{
696 int pid, tid;
697
698 if (multi_process)
699 {
700 pid = ptid_get_pid (ptid);
701 if (pid < 0)
702 buf += sprintf (buf, "p-%x.", -pid);
703 else
704 buf += sprintf (buf, "p%x.", pid);
705 }
706 tid = ptid_get_lwp (ptid);
707 if (tid < 0)
708 buf += sprintf (buf, "-%x", -tid);
709 else
710 buf += sprintf (buf, "%x", tid);
711
712 return buf;
713}
714
715ULONGEST
716hex_or_minus_one (char *buf, char **obuf)
717{
718 ULONGEST ret;
719
720 if (strncmp (buf, "-1", 2) == 0)
721 {
722 ret = (ULONGEST) -1;
723 buf += 2;
724 }
725 else
726 buf = unpack_varlen_hex (buf, &ret);
727
728 if (obuf)
729 *obuf = buf;
730
731 return ret;
732}
733
734/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
735 passed the last parsed char. Returns null_ptid on error. */
736ptid_t
737read_ptid (char *buf, char **obuf)
738{
739 char *p = buf;
740 char *pp;
741 ULONGEST pid = 0, tid = 0;
742
743 if (*p == 'p')
744 {
745 /* Multi-process ptid. */
746 pp = unpack_varlen_hex (p + 1, &pid);
747 if (*pp != '.')
748 error ("invalid remote ptid: %s\n", p);
749
750 p = pp + 1;
751
752 tid = hex_or_minus_one (p, &pp);
753
754 if (obuf)
755 *obuf = pp;
756 return ptid_build (pid, tid, 0);
757 }
758
759 /* No multi-process. Just a tid. */
760 tid = hex_or_minus_one (p, &pp);
761
762 /* Since the stub is not sending a process id, then default to
763 what's in the current inferior. */
fbd5db48 764 pid = ptid_get_pid (current_ptid);
95954743
PA
765
766 if (obuf)
767 *obuf = pp;
768 return ptid_build (pid, tid, 0);
769}
770
e0f9f062
DE
771/* Write COUNT bytes in BUF to the client.
772 The result is the number of bytes written or -1 if error.
773 This may return less than COUNT. */
774
775static int
776write_prim (const void *buf, int count)
777{
778 if (remote_connection_is_stdio ())
779 return write (fileno (stdout), buf, count);
780 else
781 return write (remote_desc, buf, count);
782}
783
784/* Read COUNT bytes from the client and store in BUF.
785 The result is the number of bytes read or -1 if error.
786 This may return less than COUNT. */
787
788static int
789read_prim (void *buf, int count)
790{
791 if (remote_connection_is_stdio ())
792 return read (fileno (stdin), buf, count);
793 else
794 return read (remote_desc, buf, count);
795}
796
c906108c 797/* Send a packet to the remote machine, with error checking.
01f9e8fa
DJ
798 The data of the packet is in BUF, and the length of the
799 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
c906108c 800
bd99dc85
PA
801static int
802putpkt_binary_1 (char *buf, int cnt, int is_notif)
c906108c
SS
803{
804 int i;
805 unsigned char csum = 0;
0a30fbc4 806 char *buf2;
c906108c 807 char *p;
bc3b5632 808 int cc;
c906108c 809
e6edda56 810 buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
0a30fbc4 811
c906108c
SS
812 /* Copy the packet into buffer BUF2, encapsulating it
813 and giving it a checksum. */
814
815 p = buf2;
bd99dc85
PA
816 if (is_notif)
817 *p++ = '%';
818 else
819 *p++ = '$';
c906108c 820
5ffff7c1
DJ
821 for (i = 0; i < cnt;)
822 i += try_rle (buf + i, cnt - i, &csum, &p);
823
c906108c
SS
824 *p++ = '#';
825 *p++ = tohex ((csum >> 4) & 0xf);
826 *p++ = tohex (csum & 0xf);
827
828 *p = '\0';
829
830 /* Send it over and over until we get a positive ack. */
831
832 do
833 {
e0f9f062 834 if (write_prim (buf2, p - buf2) != p - buf2)
c906108c
SS
835 {
836 perror ("putpkt(write)");
f88c79e6 837 free (buf2);
c906108c
SS
838 return -1;
839 }
840
bd99dc85 841 if (noack_mode || is_notif)
a6f3e723
SL
842 {
843 /* Don't expect an ack then. */
844 if (remote_debug)
845 {
bd99dc85
PA
846 if (is_notif)
847 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2);
848 else
849 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
a6f3e723
SL
850 fflush (stderr);
851 }
852 break;
853 }
854
c906108c 855 if (remote_debug)
0d62e5e8
DJ
856 {
857 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
858 fflush (stderr);
859 }
0d62e5e8 860
bc3b5632 861 cc = readchar ();
c906108c 862
bc3b5632
DE
863 if (cc < 0)
864 {
0a30fbc4 865 free (buf2);
c906108c
SS
866 return -1;
867 }
0d62e5e8 868
bc3b5632
DE
869 if (remote_debug)
870 {
871 fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc);
872 fflush (stderr);
873 }
874
0d62e5e8 875 /* Check for an input interrupt while we're here. */
bc3b5632 876 if (cc == '\003' && current_inferior != NULL)
ef57601b 877 (*the_target->request_interrupt) ();
c906108c 878 }
bc3b5632 879 while (cc != '+');
c906108c 880
0a30fbc4 881 free (buf2);
c906108c
SS
882 return 1; /* Success! */
883}
884
bd99dc85
PA
885int
886putpkt_binary (char *buf, int cnt)
887{
888 return putpkt_binary_1 (buf, cnt, 0);
889}
890
01f9e8fa
DJ
891/* Send a packet to the remote machine, with error checking. The data
892 of the packet is in BUF, and the packet should be a NUL-terminated
893 string. Returns >= 0 on success, -1 otherwise. */
894
895int
896putpkt (char *buf)
897{
898 return putpkt_binary (buf, strlen (buf));
899}
900
bd99dc85
PA
901int
902putpkt_notif (char *buf)
903{
904 return putpkt_binary_1 (buf, strlen (buf), 1);
905}
906
c906108c
SS
907/* Come here when we get an input interrupt from the remote side. This
908 interrupt should only be active while we are waiting for the child to do
bc3b5632
DE
909 something. Thus this assumes readchar:bufcnt is 0.
910 About the only thing that should come through is a ^C, which
ef57601b 911 will cause us to request child interruption. */
c906108c
SS
912
913static void
0a30fbc4 914input_interrupt (int unused)
c906108c 915{
cf30a8e1
C
916 fd_set readset;
917 struct timeval immediate = { 0, 0 };
c906108c 918
cf30a8e1
C
919 /* Protect against spurious interrupts. This has been observed to
920 be a problem under NetBSD 1.4 and 1.5. */
c906108c 921
cf30a8e1
C
922 FD_ZERO (&readset);
923 FD_SET (remote_desc, &readset);
924 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 925 {
cf30a8e1 926 int cc;
fd500816 927 char c = 0;
7390519e 928
e0f9f062 929 cc = read_prim (&c, 1);
c906108c 930
2d717e4f 931 if (cc != 1 || c != '\003' || current_inferior == NULL)
cf30a8e1 932 {
fd500816
DJ
933 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
934 cc, c, c);
cf30a8e1
C
935 return;
936 }
7390519e 937
ef57601b 938 (*the_target->request_interrupt) ();
cf30a8e1 939 }
c906108c 940}
7390519e
PA
941
942/* Check if the remote side sent us an interrupt request (^C). */
943void
944check_remote_input_interrupt_request (void)
945{
946 /* This function may be called before establishing communications,
947 therefore we need to validate the remote descriptor. */
948
949 if (remote_desc == INVALID_DESCRIPTOR)
950 return;
951
952 input_interrupt (0);
953}
b80864fb
DJ
954
955/* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
956 accept Control-C from the client, and must be disabled when talking to
957 the client. */
c906108c 958
a20d5e98 959static void
62ea82f5
DJ
960unblock_async_io (void)
961{
b80864fb 962#ifndef USE_WIN32API
62ea82f5 963 sigset_t sigio_set;
a20d5e98 964
62ea82f5
DJ
965 sigemptyset (&sigio_set);
966 sigaddset (&sigio_set, SIGIO);
967 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
b80864fb 968#endif
62ea82f5
DJ
969}
970
ac8c974e
AR
971#ifdef __QNX__
972static void
973nto_comctrl (int enable)
974{
975 struct sigevent event;
976
977 if (enable)
978 {
979 event.sigev_notify = SIGEV_SIGNAL_THREAD;
980 event.sigev_signo = SIGIO;
981 event.sigev_code = 0;
982 event.sigev_value.sival_ptr = NULL;
983 event.sigev_priority = -1;
984 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
985 &event);
986 }
987 else
988 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
989}
990#endif /* __QNX__ */
991
992
fd500816
DJ
993/* Current state of asynchronous I/O. */
994static int async_io_enabled;
995
996/* Enable asynchronous I/O. */
c906108c 997void
fba45db2 998enable_async_io (void)
c906108c 999{
fd500816
DJ
1000 if (async_io_enabled)
1001 return;
1002
b80864fb 1003#ifndef USE_WIN32API
c906108c 1004 signal (SIGIO, input_interrupt);
b80864fb 1005#endif
fd500816 1006 async_io_enabled = 1;
ac8c974e
AR
1007#ifdef __QNX__
1008 nto_comctrl (1);
1009#endif /* __QNX__ */
c906108c
SS
1010}
1011
fd500816 1012/* Disable asynchronous I/O. */
c906108c 1013void
fba45db2 1014disable_async_io (void)
c906108c 1015{
fd500816
DJ
1016 if (!async_io_enabled)
1017 return;
1018
b80864fb 1019#ifndef USE_WIN32API
c906108c 1020 signal (SIGIO, SIG_IGN);
b80864fb 1021#endif
fd500816 1022 async_io_enabled = 0;
ac8c974e
AR
1023#ifdef __QNX__
1024 nto_comctrl (0);
1025#endif /* __QNX__ */
1026
c906108c
SS
1027}
1028
a20d5e98
DJ
1029void
1030initialize_async_io (void)
1031{
1032 /* Make sure that async I/O starts disabled. */
1033 async_io_enabled = 1;
1034 disable_async_io ();
1035
1036 /* Make sure the signal is unblocked. */
1037 unblock_async_io ();
1038}
1039
24b066ba
DE
1040/* Internal buffer used by readchar.
1041 These are global to readchar because reschedule_remote needs to be
1042 able to tell whether the buffer is empty. */
1043
1044static unsigned char readchar_buf[BUFSIZ];
1045static int readchar_bufcnt = 0;
1046static unsigned char *readchar_bufp;
1047
c906108c
SS
1048/* Returns next char from remote GDB. -1 if error. */
1049
1050static int
fba45db2 1051readchar (void)
c906108c 1052{
24b066ba 1053 int ch;
c906108c 1054
24b066ba
DE
1055 if (readchar_bufcnt == 0)
1056 {
e0f9f062 1057 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
c906108c 1058
24b066ba
DE
1059 if (readchar_bufcnt <= 0)
1060 {
1061 if (readchar_bufcnt == 0)
1062 fprintf (stderr, "readchar: Got EOF\n");
1063 else
1064 perror ("readchar");
c906108c 1065
24b066ba
DE
1066 return -1;
1067 }
c906108c 1068
24b066ba 1069 readchar_bufp = readchar_buf;
c906108c
SS
1070 }
1071
24b066ba
DE
1072 readchar_bufcnt--;
1073 ch = *readchar_bufp++;
1074 reschedule ();
1075 return ch;
1076}
1077
1078/* Reset the readchar state machine. */
1079
1080static void
1081reset_readchar (void)
1082{
1083 readchar_bufcnt = 0;
1084 if (readchar_callback != NOT_SCHEDULED)
1085 {
1086 delete_callback_event (readchar_callback);
1087 readchar_callback = NOT_SCHEDULED;
1088 }
1089}
1090
1091/* Process remaining data in readchar_buf. */
1092
1093static int
1094process_remaining (void *context)
1095{
1096 int res;
1097
1098 /* This is a one-shot event. */
1099 readchar_callback = NOT_SCHEDULED;
1100
1101 if (readchar_bufcnt > 0)
1102 res = handle_serial_event (0, NULL);
1103 else
1104 res = 0;
1105
1106 return res;
1107}
1108
1109/* If there is still data in the buffer, queue another event to process it,
1110 we can't sleep in select yet. */
1111
1112static void
1113reschedule (void)
1114{
1115 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
1116 readchar_callback = append_callback_event (process_remaining, NULL);
c906108c
SS
1117}
1118
1119/* Read a packet from the remote machine, with error checking,
1120 and store it in BUF. Returns length of packet, or negative if error. */
1121
1122int
fba45db2 1123getpkt (char *buf)
c906108c
SS
1124{
1125 char *bp;
1126 unsigned char csum, c1, c2;
1127 int c;
1128
1129 while (1)
1130 {
1131 csum = 0;
1132
1133 while (1)
1134 {
1135 c = readchar ();
1136 if (c == '$')
1137 break;
1138 if (remote_debug)
0d62e5e8
DJ
1139 {
1140 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
1141 fflush (stderr);
1142 }
1143
c906108c
SS
1144 if (c < 0)
1145 return -1;
1146 }
1147
1148 bp = buf;
1149 while (1)
1150 {
1151 c = readchar ();
1152 if (c < 0)
1153 return -1;
1154 if (c == '#')
1155 break;
1156 *bp++ = c;
1157 csum += c;
1158 }
1159 *bp = 0;
1160
1161 c1 = fromhex (readchar ());
1162 c2 = fromhex (readchar ());
c5aa993b 1163
c906108c
SS
1164 if (csum == (c1 << 4) + c2)
1165 break;
1166
a6f3e723
SL
1167 if (noack_mode)
1168 {
493e2a69
MS
1169 fprintf (stderr,
1170 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1171 "buf=%s [no-ack-mode, Bad medium?]\n",
a6f3e723
SL
1172 (c1 << 4) + c2, csum, buf);
1173 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1174 break;
1175 }
1176
c906108c
SS
1177 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1178 (c1 << 4) + c2, csum, buf);
e0f9f062 1179 if (write_prim ("-", 1) != 1)
e581f2b4 1180 return -1;
c906108c
SS
1181 }
1182
a6f3e723 1183 if (!noack_mode)
0d62e5e8 1184 {
a6f3e723
SL
1185 if (remote_debug)
1186 {
1187 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
1188 fflush (stderr);
1189 }
c906108c 1190
e0f9f062 1191 if (write_prim ("+", 1) != 1)
e581f2b4 1192 return -1;
c906108c 1193
a6f3e723
SL
1194 if (remote_debug)
1195 {
1196 fprintf (stderr, "[sent ack]\n");
1197 fflush (stderr);
1198 }
0d62e5e8 1199 }
86b1f9c5
PM
1200 else
1201 {
1202 if (remote_debug)
1203 {
1204 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf);
1205 fflush (stderr);
1206 }
1207 }
0d62e5e8 1208
c906108c
SS
1209 return bp - buf;
1210}
1211
1212void
fba45db2 1213write_ok (char *buf)
c906108c
SS
1214{
1215 buf[0] = 'O';
1216 buf[1] = 'K';
1217 buf[2] = '\0';
1218}
1219
1220void
fba45db2 1221write_enn (char *buf)
c906108c 1222{
c89dc5d4 1223 /* Some day, we should define the meanings of the error codes... */
c906108c 1224 buf[0] = 'E';
c89dc5d4
DJ
1225 buf[1] = '0';
1226 buf[2] = '1';
c906108c
SS
1227 buf[3] = '\0';
1228}
1229
0fb4aa4b
PA
1230#endif
1231
c906108c 1232void
fa593d66 1233convert_int_to_ascii (const unsigned char *from, char *to, int n)
c906108c
SS
1234{
1235 int nib;
f450004a 1236 int ch;
c906108c
SS
1237 while (n--)
1238 {
1239 ch = *from++;
1240 nib = ((ch & 0xf0) >> 4) & 0x0f;
1241 *to++ = tohex (nib);
1242 nib = ch & 0x0f;
1243 *to++ = tohex (nib);
1244 }
1245 *to++ = 0;
1246}
1247
0fb4aa4b 1248#ifndef IN_PROCESS_AGENT
c906108c
SS
1249
1250void
fa593d66 1251convert_ascii_to_int (const char *from, unsigned char *to, int n)
c906108c
SS
1252{
1253 int nib1, nib2;
1254 while (n--)
1255 {
1256 nib1 = fromhex (*from++);
1257 nib2 = fromhex (*from++);
1258 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
1259 }
1260}
1261
1262static char *
442ea881 1263outreg (struct regcache *regcache, int regno, char *buf)
c906108c 1264{
5c44784c
JM
1265 if ((regno >> 12) != 0)
1266 *buf++ = tohex ((regno >> 12) & 0xf);
1267 if ((regno >> 8) != 0)
1268 *buf++ = tohex ((regno >> 8) & 0xf);
1269 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
1270 *buf++ = tohex (regno & 0xf);
1271 *buf++ = ':';
442ea881 1272 collect_register_as_string (regcache, regno, buf);
0d62e5e8 1273 buf += 2 * register_size (regno);
c906108c
SS
1274 *buf++ = ';';
1275
1276 return buf;
1277}
1278
0d62e5e8
DJ
1279void
1280new_thread_notify (int id)
1281{
1282 char own_buf[256];
1283
1284 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1285 if (1)
1286 return;
1287
1288 if (server_waiting == 0)
1289 return;
1290
1291 sprintf (own_buf, "n%x", id);
1292 disable_async_io ();
1293 putpkt (own_buf);
1294 enable_async_io ();
1295}
1296
1297void
1298dead_thread_notify (int id)
1299{
1300 char own_buf[256];
1301
1302 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1303 if (1)
1304 return;
1305
1306 sprintf (own_buf, "x%x", id);
1307 disable_async_io ();
1308 putpkt (own_buf);
1309 enable_async_io ();
1310}
1311
c906108c 1312void
95954743 1313prepare_resume_reply (char *buf, ptid_t ptid,
5b1c542e 1314 struct target_waitstatus *status)
c906108c 1315{
5b1c542e 1316 if (debug_threads)
e77616d7 1317 fprintf (stderr, "Writing resume reply for %s:%d\n",
95954743 1318 target_pid_to_str (ptid), status->kind);
c906108c 1319
5b1c542e 1320 switch (status->kind)
c906108c 1321 {
5b1c542e
PA
1322 case TARGET_WAITKIND_STOPPED:
1323 {
1324 struct thread_info *saved_inferior;
1325 const char **regp;
442ea881 1326 struct regcache *regcache;
e013ee27 1327
5b1c542e
PA
1328 sprintf (buf, "T%02x", status->value.sig);
1329 buf += strlen (buf);
e013ee27 1330
5b1c542e 1331 regp = gdbserver_expedite_regs;
e013ee27 1332
5b1c542e 1333 saved_inferior = current_inferior;
e013ee27 1334
e09875d4 1335 current_inferior = find_thread_ptid (ptid);
e013ee27 1336
442ea881
PA
1337 regcache = get_thread_regcache (current_inferior, 1);
1338
5b1c542e
PA
1339 if (the_target->stopped_by_watchpoint != NULL
1340 && (*the_target->stopped_by_watchpoint) ())
1341 {
1342 CORE_ADDR addr;
1343 int i;
c906108c 1344
5b1c542e
PA
1345 strncpy (buf, "watch:", 6);
1346 buf += 6;
0d62e5e8 1347
5b1c542e 1348 addr = (*the_target->stopped_data_address) ();
255e7678 1349
5b1c542e
PA
1350 /* Convert each byte of the address into two hexadecimal
1351 chars. Note that we take sizeof (void *) instead of
1352 sizeof (addr); this is to avoid sending a 64-bit
1353 address to a 32-bit GDB. */
1354 for (i = sizeof (void *) * 2; i > 0; i--)
1355 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1356 *buf++ = ';';
1357 }
1358
1359 while (*regp)
1360 {
442ea881 1361 buf = outreg (regcache, find_regno (*regp), buf);
5b1c542e
PA
1362 regp ++;
1363 }
5472f405 1364 *buf = '\0';
5b1c542e
PA
1365
1366 /* Formerly, if the debugger had not used any thread features
1367 we would not burden it with a thread status response. This
1368 was for the benefit of GDB 4.13 and older. However, in
1369 recent GDB versions the check (``if (cont_thread != 0)'')
1370 does not have the desired effect because of sillyness in
1371 the way that the remote protocol handles specifying a
1372 thread. Since thread support relies on qSymbol support
1373 anyway, assume GDB can handle threads. */
1374
1375 if (using_threads && !disable_packet_Tthread)
1376 {
1377 /* This if (1) ought to be unnecessary. But remote_wait
1378 in GDB will claim this event belongs to inferior_ptid
1379 if we do not specify a thread, and there's no way for
1380 gdbserver to know what inferior_ptid is. */
95954743 1381 if (1 || !ptid_equal (general_thread, ptid))
5b1c542e 1382 {
dc146f7c 1383 int core = -1;
bd99dc85
PA
1384 /* In non-stop, don't change the general thread behind
1385 GDB's back. */
1386 if (!non_stop)
1387 general_thread = ptid;
95954743
PA
1388 sprintf (buf, "thread:");
1389 buf += strlen (buf);
1390 buf = write_ptid (buf, ptid);
1391 strcat (buf, ";");
5b1c542e 1392 buf += strlen (buf);
dc146f7c 1393
3e10640f
YQ
1394 core = target_core_of_thread (ptid);
1395
dc146f7c
VP
1396 if (core != -1)
1397 {
1398 sprintf (buf, "core:");
1399 buf += strlen (buf);
1400 sprintf (buf, "%x", core);
1401 strcat (buf, ";");
1402 buf += strlen (buf);
1403 }
5b1c542e
PA
1404 }
1405 }
1406
1407 if (dlls_changed)
1408 {
1409 strcpy (buf, "library:;");
1410 buf += strlen (buf);
1411 dlls_changed = 0;
1412 }
1413
1414 current_inferior = saved_inferior;
1415 }
1416 break;
1417 case TARGET_WAITKIND_EXITED:
95954743
PA
1418 if (multi_process)
1419 sprintf (buf, "W%x;process:%x",
1420 status->value.integer, ptid_get_pid (ptid));
1421 else
1422 sprintf (buf, "W%02x", status->value.integer);
5b1c542e
PA
1423 break;
1424 case TARGET_WAITKIND_SIGNALLED:
95954743
PA
1425 if (multi_process)
1426 sprintf (buf, "X%x;process:%x",
1427 status->value.sig, ptid_get_pid (ptid));
1428 else
1429 sprintf (buf, "X%02x", status->value.sig);
5b1c542e
PA
1430 break;
1431 default:
1432 error ("unhandled waitkind");
1433 break;
c906108c 1434 }
c906108c
SS
1435}
1436
1437void
fba45db2 1438decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
1439{
1440 int i = 0, j = 0;
1441 char ch;
1442 *mem_addr_ptr = *len_ptr = 0;
1443
1444 while ((ch = from[i++]) != ',')
1445 {
1446 *mem_addr_ptr = *mem_addr_ptr << 4;
1447 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1448 }
1449
1450 for (j = 0; j < 4; j++)
1451 {
1452 if ((ch = from[i++]) == 0)
1453 break;
1454 *len_ptr = *len_ptr << 4;
1455 *len_ptr |= fromhex (ch) & 0x0f;
1456 }
1457}
1458
1459void
fba45db2 1460decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
fa593d66 1461 unsigned char **to_p)
c906108c
SS
1462{
1463 int i = 0;
1464 char ch;
1465 *mem_addr_ptr = *len_ptr = 0;
1466
1467 while ((ch = from[i++]) != ',')
1468 {
1469 *mem_addr_ptr = *mem_addr_ptr << 4;
1470 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1471 }
1472
1473 while ((ch = from[i++]) != ':')
1474 {
1475 *len_ptr = *len_ptr << 4;
1476 *len_ptr |= fromhex (ch) & 0x0f;
1477 }
1478
fa593d66
PA
1479 if (*to_p == NULL)
1480 *to_p = xmalloc (*len_ptr);
1481
1482 convert_ascii_to_int (&from[i++], *to_p, *len_ptr);
c906108c 1483}
2f2893d9 1484
01f9e8fa
DJ
1485int
1486decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
fa593d66 1487 unsigned int *len_ptr, unsigned char **to_p)
01f9e8fa
DJ
1488{
1489 int i = 0;
1490 char ch;
1491 *mem_addr_ptr = *len_ptr = 0;
1492
1493 while ((ch = from[i++]) != ',')
1494 {
1495 *mem_addr_ptr = *mem_addr_ptr << 4;
1496 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1497 }
1498
1499 while ((ch = from[i++]) != ':')
1500 {
1501 *len_ptr = *len_ptr << 4;
1502 *len_ptr |= fromhex (ch) & 0x0f;
1503 }
1504
fa593d66
PA
1505 if (*to_p == NULL)
1506 *to_p = xmalloc (*len_ptr);
1507
01f9e8fa 1508 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
fa593d66 1509 *to_p, *len_ptr) != *len_ptr)
01f9e8fa
DJ
1510 return -1;
1511
1512 return 0;
1513}
1514
0e7f50da 1515/* Decode a qXfer write request. */
d08aafef 1516
0e7f50da 1517int
d08aafef 1518decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
0e7f50da
UW
1519 unsigned int *len, unsigned char *data)
1520{
1521 char ch;
d08aafef 1522 char *b = buf;
0e7f50da
UW
1523
1524 /* Extract the offset. */
1525 *offset = 0;
1526 while ((ch = *buf++) != ':')
1527 {
1528 *offset = *offset << 4;
1529 *offset |= fromhex (ch) & 0x0f;
1530 }
1531
1532 /* Get encoded data. */
d08aafef 1533 packet_len -= buf - b;
0e7f50da
UW
1534 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1535 data, packet_len);
1536 return 0;
1537}
1538
08388c79
DE
1539/* Decode the parameters of a qSearch:memory packet. */
1540
1541int
1542decode_search_memory_packet (const char *buf, int packet_len,
1543 CORE_ADDR *start_addrp,
1544 CORE_ADDR *search_space_lenp,
1545 gdb_byte *pattern, unsigned int *pattern_lenp)
1546{
1547 const char *p = buf;
1548
1549 p = decode_address_to_semicolon (start_addrp, p);
1550 p = decode_address_to_semicolon (search_space_lenp, p);
1551 packet_len -= p - buf;
1552 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1553 pattern, packet_len);
1554 return 0;
1555}
1556
95954743
PA
1557static void
1558free_sym_cache (struct sym_cache *sym)
1559{
1560 if (sym != NULL)
1561 {
1562 free (sym->name);
1563 free (sym);
1564 }
1565}
1566
1567void
1568clear_symbol_cache (struct sym_cache **symcache_p)
1569{
1570 struct sym_cache *sym, *next;
1571
1572 /* Check the cache first. */
1573 for (sym = *symcache_p; sym; sym = next)
1574 {
1575 next = sym->next;
1576 free_sym_cache (sym);
1577 }
1578
1579 *symcache_p = NULL;
1580}
1581
9836d6ea
PA
1582/* Get the address of NAME, and return it in ADDRP if found. if
1583 MAY_ASK_GDB is false, assume symbol cache misses are failures.
fd500816
DJ
1584 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1585
2f2893d9 1586int
9836d6ea 1587look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
2f2893d9
DJ
1588{
1589 char own_buf[266], *p, *q;
1590 int len;
fd500816 1591 struct sym_cache *sym;
95954743
PA
1592 struct process_info *proc;
1593
1594 proc = current_process ();
fd500816
DJ
1595
1596 /* Check the cache first. */
95954743 1597 for (sym = proc->symbol_cache; sym; sym = sym->next)
fd500816
DJ
1598 if (strcmp (name, sym->name) == 0)
1599 {
1600 *addrp = sym->addr;
1601 return 1;
1602 }
2f2893d9 1603
9836d6ea
PA
1604 /* It might not be an appropriate time to look up a symbol,
1605 e.g. while we're trying to fetch registers. */
1606 if (!may_ask_gdb)
ea025f5f
DJ
1607 return 0;
1608
2f2893d9
DJ
1609 /* Send the request. */
1610 strcpy (own_buf, "qSymbol:");
1611 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
1612 if (putpkt (own_buf) < 0)
1613 return -1;
1614
1615 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1616 len = getpkt (own_buf);
1617 if (len < 0)
1618 return -1;
1619
2bbe3cc1
DJ
1620 /* We ought to handle pretty much any packet at this point while we
1621 wait for the qSymbol "response". That requires re-entering the
1622 main loop. For now, this is an adequate approximation; allow
1623 GDB to read from memory while it figures out the address of the
1624 symbol. */
1625 while (own_buf[0] == 'm')
1626 {
1627 CORE_ADDR mem_addr;
1628 unsigned char *mem_buf;
1629 unsigned int mem_len;
1630
1631 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
bca929d3 1632 mem_buf = xmalloc (mem_len);
2bbe3cc1
DJ
1633 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1634 convert_int_to_ascii (mem_buf, own_buf, mem_len);
1635 else
1636 write_enn (own_buf);
1637 free (mem_buf);
1638 if (putpkt (own_buf) < 0)
1639 return -1;
1640 len = getpkt (own_buf);
1641 if (len < 0)
1642 return -1;
1643 }
1b3f6016 1644
2f2893d9
DJ
1645 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
1646 {
2bbe3cc1 1647 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
2f2893d9
DJ
1648 return -1;
1649 }
1650
1651 p = own_buf + strlen ("qSymbol:");
1652 q = p;
1653 while (*q && *q != ':')
1654 q++;
1655
1656 /* Make sure we found a value for the symbol. */
1657 if (p == q || *q == '\0')
1658 return 0;
1659
1660 decode_address (addrp, p, q - p);
fd500816
DJ
1661
1662 /* Save the symbol in our cache. */
bca929d3
DE
1663 sym = xmalloc (sizeof (*sym));
1664 sym->name = xstrdup (name);
fd500816 1665 sym->addr = *addrp;
95954743
PA
1666 sym->next = proc->symbol_cache;
1667 proc->symbol_cache = sym;
fd500816 1668
2f2893d9
DJ
1669 return 1;
1670}
c74d0ad8 1671
fa593d66
PA
1672/* Relocate an instruction to execute at a different address. OLDLOC
1673 is the address in the inferior memory where the instruction to
1674 relocate is currently at. On input, TO points to the destination
1675 where we want the instruction to be copied (and possibly adjusted)
1676 to. On output, it points to one past the end of the resulting
1677 instruction(s). The effect of executing the instruction at TO
86a30030 1678 shall be the same as if executing it at OLDLOC. For example, call
fa593d66
PA
1679 instructions that implicitly push the return address on the stack
1680 should be adjusted to return to the instruction after OLDLOC;
1681 relative branches, and other PC-relative instructions need the
1682 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1683
1684int
1685relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1686{
1687 char own_buf[266];
1688 int len;
1689 ULONGEST written = 0;
1690
1691 /* Send the request. */
1692 strcpy (own_buf, "qRelocInsn:");
1693 sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1694 paddress (*to));
1695 if (putpkt (own_buf) < 0)
1696 return -1;
1697
1698 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1699 len = getpkt (own_buf);
1700 if (len < 0)
1701 return -1;
1702
1703 /* We ought to handle pretty much any packet at this point while we
1704 wait for the qRelocInsn "response". That requires re-entering
1705 the main loop. For now, this is an adequate approximation; allow
1706 GDB to access memory. */
1707 while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X')
1708 {
1709 CORE_ADDR mem_addr;
1710 unsigned char *mem_buf = NULL;
1711 unsigned int mem_len;
1712
1713 if (own_buf[0] == 'm')
1714 {
1715 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
1716 mem_buf = xmalloc (mem_len);
1717 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1718 convert_int_to_ascii (mem_buf, own_buf, mem_len);
1719 else
1720 write_enn (own_buf);
1721 }
1722 else if (own_buf[0] == 'X')
1723 {
1724 if (decode_X_packet (&own_buf[1], len - 1, &mem_addr,
1725 &mem_len, &mem_buf) < 0
1726 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
1727 write_enn (own_buf);
1728 else
1729 write_ok (own_buf);
1730 }
1731 else
1732 {
1733 decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf);
1734 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1735 write_ok (own_buf);
1736 else
1737 write_enn (own_buf);
1738 }
1739 free (mem_buf);
1740 if (putpkt (own_buf) < 0)
1741 return -1;
1742 len = getpkt (own_buf);
1743 if (len < 0)
1744 return -1;
1745 }
1746
1747 if (own_buf[0] == 'E')
1748 {
1749 warning ("An error occurred while relocating an instruction: %s\n",
1750 own_buf);
1751 return -1;
1752 }
1753
1754 if (strncmp (own_buf, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1755 {
1756 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1757 own_buf);
1758 return -1;
1759 }
1760
1761 unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written);
1762
1763 *to += written;
1764 return 0;
1765}
1766
c74d0ad8 1767void
bce7165d 1768monitor_output (const char *msg)
c74d0ad8 1769{
bca929d3 1770 char *buf = xmalloc (strlen (msg) * 2 + 2);
c74d0ad8
DJ
1771
1772 buf[0] = 'O';
1773 hexify (buf + 1, msg, 0);
1774
1775 putpkt (buf);
1776 free (buf);
1777}
255e7678 1778
0fb4aa4b 1779#endif
This page took 1.187909 seconds and 4 git commands to generate.