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