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