Move stdarg.h to common-defs.h
[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 24#include "dll.h"
9c3d6531 25#include "rsp-low.h"
799cdc37 26
c906108c 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)
87ce2a04 164 debug_printf ("handling possible accept event\n");
8336d594
PA
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
0fb4aa4b
PA
420#endif
421
0fb4aa4b
PA
422#ifndef IN_PROCESS_AGENT
423
dae5f5cf 424void
2f2893d9
DJ
425decode_address (CORE_ADDR *addrp, const char *start, int len)
426{
427 CORE_ADDR addr;
428 char ch;
429 int i;
430
431 addr = 0;
432 for (i = 0; i < len; i++)
433 {
434 ch = start[i];
435 addr = addr << 4;
436 addr = addr | (fromhex (ch) & 0x0f);
437 }
438 *addrp = addr;
439}
440
89be2091
DJ
441const char *
442decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
443{
444 const char *end;
445
446 end = start;
447 while (*end != '\0' && *end != ';')
448 end++;
449
450 decode_address (addrp, start, end - start);
451
452 if (*end == ';')
453 end++;
454 return end;
455}
456
0fb4aa4b
PA
457#endif
458
0fb4aa4b
PA
459#ifndef IN_PROCESS_AGENT
460
5ffff7c1
DJ
461/* Look for a sequence of characters which can be run-length encoded.
462 If there are any, update *CSUM and *P. Otherwise, output the
463 single character. Return the number of characters consumed. */
464
465static int
466try_rle (char *buf, int remaining, unsigned char *csum, char **p)
467{
468 int n;
469
470 /* Always output the character. */
471 *csum += buf[0];
472 *(*p)++ = buf[0];
473
474 /* Don't go past '~'. */
475 if (remaining > 97)
476 remaining = 97;
477
478 for (n = 1; n < remaining; n++)
479 if (buf[n] != buf[0])
480 break;
481
482 /* N is the index of the first character not the same as buf[0].
483 buf[0] is counted twice, so by decrementing N, we get the number
484 of characters the RLE sequence will replace. */
485 n--;
486
487 if (n < 3)
488 return 1;
489
490 /* Skip the frame characters. The manual says to skip '+' and '-'
491 also, but there's no reason to. Unfortunately these two unusable
492 characters double the encoded length of a four byte zero
493 value. */
494 while (n + 29 == '$' || n + 29 == '#')
495 n--;
496
497 *csum += '*';
498 *(*p)++ = '*';
499 *csum += n + 29;
500 *(*p)++ = n + 29;
501
502 return n + 1;
503}
504
0fb4aa4b
PA
505#endif
506
0fb4aa4b
PA
507#ifndef IN_PROCESS_AGENT
508
95954743
PA
509/* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
510
511char *
512write_ptid (char *buf, ptid_t ptid)
513{
514 int pid, tid;
515
516 if (multi_process)
517 {
518 pid = ptid_get_pid (ptid);
519 if (pid < 0)
520 buf += sprintf (buf, "p-%x.", -pid);
521 else
522 buf += sprintf (buf, "p%x.", pid);
523 }
524 tid = ptid_get_lwp (ptid);
525 if (tid < 0)
526 buf += sprintf (buf, "-%x", -tid);
527 else
528 buf += sprintf (buf, "%x", tid);
529
530 return buf;
531}
532
533ULONGEST
534hex_or_minus_one (char *buf, char **obuf)
535{
536 ULONGEST ret;
537
538 if (strncmp (buf, "-1", 2) == 0)
539 {
540 ret = (ULONGEST) -1;
541 buf += 2;
542 }
543 else
544 buf = unpack_varlen_hex (buf, &ret);
545
546 if (obuf)
547 *obuf = buf;
548
549 return ret;
550}
551
552/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
553 passed the last parsed char. Returns null_ptid on error. */
554ptid_t
555read_ptid (char *buf, char **obuf)
556{
557 char *p = buf;
558 char *pp;
559 ULONGEST pid = 0, tid = 0;
560
561 if (*p == 'p')
562 {
563 /* Multi-process ptid. */
564 pp = unpack_varlen_hex (p + 1, &pid);
565 if (*pp != '.')
566 error ("invalid remote ptid: %s\n", p);
567
568 p = pp + 1;
569
570 tid = hex_or_minus_one (p, &pp);
571
572 if (obuf)
573 *obuf = pp;
574 return ptid_build (pid, tid, 0);
575 }
576
577 /* No multi-process. Just a tid. */
578 tid = hex_or_minus_one (p, &pp);
579
580 /* Since the stub is not sending a process id, then default to
581 what's in the current inferior. */
fbd5db48 582 pid = ptid_get_pid (current_ptid);
95954743
PA
583
584 if (obuf)
585 *obuf = pp;
586 return ptid_build (pid, tid, 0);
587}
588
e0f9f062
DE
589/* Write COUNT bytes in BUF to the client.
590 The result is the number of bytes written or -1 if error.
591 This may return less than COUNT. */
592
593static int
594write_prim (const void *buf, int count)
595{
596 if (remote_connection_is_stdio ())
597 return write (fileno (stdout), buf, count);
598 else
599 return write (remote_desc, buf, count);
600}
601
602/* Read COUNT bytes from the client and store in BUF.
603 The result is the number of bytes read or -1 if error.
604 This may return less than COUNT. */
605
606static int
607read_prim (void *buf, int count)
608{
609 if (remote_connection_is_stdio ())
610 return read (fileno (stdin), buf, count);
611 else
612 return read (remote_desc, buf, count);
613}
614
c906108c 615/* Send a packet to the remote machine, with error checking.
01f9e8fa
DJ
616 The data of the packet is in BUF, and the length of the
617 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
c906108c 618
bd99dc85
PA
619static int
620putpkt_binary_1 (char *buf, int cnt, int is_notif)
c906108c
SS
621{
622 int i;
623 unsigned char csum = 0;
0a30fbc4 624 char *buf2;
c906108c 625 char *p;
bc3b5632 626 int cc;
c906108c 627
e6edda56 628 buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
0a30fbc4 629
c906108c
SS
630 /* Copy the packet into buffer BUF2, encapsulating it
631 and giving it a checksum. */
632
633 p = buf2;
bd99dc85
PA
634 if (is_notif)
635 *p++ = '%';
636 else
637 *p++ = '$';
c906108c 638
5ffff7c1
DJ
639 for (i = 0; i < cnt;)
640 i += try_rle (buf + i, cnt - i, &csum, &p);
641
c906108c
SS
642 *p++ = '#';
643 *p++ = tohex ((csum >> 4) & 0xf);
644 *p++ = tohex (csum & 0xf);
645
646 *p = '\0';
647
648 /* Send it over and over until we get a positive ack. */
649
650 do
651 {
e0f9f062 652 if (write_prim (buf2, p - buf2) != p - buf2)
c906108c
SS
653 {
654 perror ("putpkt(write)");
f88c79e6 655 free (buf2);
c906108c
SS
656 return -1;
657 }
658
bd99dc85 659 if (noack_mode || is_notif)
a6f3e723
SL
660 {
661 /* Don't expect an ack then. */
662 if (remote_debug)
663 {
bd99dc85
PA
664 if (is_notif)
665 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2);
666 else
667 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
a6f3e723
SL
668 fflush (stderr);
669 }
670 break;
671 }
672
c906108c 673 if (remote_debug)
0d62e5e8
DJ
674 {
675 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
676 fflush (stderr);
677 }
0d62e5e8 678
bc3b5632 679 cc = readchar ();
c906108c 680
bc3b5632
DE
681 if (cc < 0)
682 {
0a30fbc4 683 free (buf2);
c906108c
SS
684 return -1;
685 }
0d62e5e8 686
bc3b5632
DE
687 if (remote_debug)
688 {
689 fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc);
690 fflush (stderr);
691 }
692
0d62e5e8 693 /* Check for an input interrupt while we're here. */
bc3b5632 694 if (cc == '\003' && current_inferior != NULL)
ef57601b 695 (*the_target->request_interrupt) ();
c906108c 696 }
bc3b5632 697 while (cc != '+');
c906108c 698
0a30fbc4 699 free (buf2);
c906108c
SS
700 return 1; /* Success! */
701}
702
bd99dc85
PA
703int
704putpkt_binary (char *buf, int cnt)
705{
706 return putpkt_binary_1 (buf, cnt, 0);
707}
708
01f9e8fa
DJ
709/* Send a packet to the remote machine, with error checking. The data
710 of the packet is in BUF, and the packet should be a NUL-terminated
711 string. Returns >= 0 on success, -1 otherwise. */
712
713int
714putpkt (char *buf)
715{
716 return putpkt_binary (buf, strlen (buf));
717}
718
bd99dc85
PA
719int
720putpkt_notif (char *buf)
721{
722 return putpkt_binary_1 (buf, strlen (buf), 1);
723}
724
c906108c
SS
725/* Come here when we get an input interrupt from the remote side. This
726 interrupt should only be active while we are waiting for the child to do
bc3b5632
DE
727 something. Thus this assumes readchar:bufcnt is 0.
728 About the only thing that should come through is a ^C, which
ef57601b 729 will cause us to request child interruption. */
c906108c
SS
730
731static void
0a30fbc4 732input_interrupt (int unused)
c906108c 733{
cf30a8e1
C
734 fd_set readset;
735 struct timeval immediate = { 0, 0 };
c906108c 736
cf30a8e1
C
737 /* Protect against spurious interrupts. This has been observed to
738 be a problem under NetBSD 1.4 and 1.5. */
c906108c 739
cf30a8e1
C
740 FD_ZERO (&readset);
741 FD_SET (remote_desc, &readset);
742 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 743 {
cf30a8e1 744 int cc;
fd500816 745 char c = 0;
7390519e 746
e0f9f062 747 cc = read_prim (&c, 1);
c906108c 748
2d717e4f 749 if (cc != 1 || c != '\003' || current_inferior == NULL)
cf30a8e1 750 {
fd500816
DJ
751 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
752 cc, c, c);
cf30a8e1
C
753 return;
754 }
7390519e 755
ef57601b 756 (*the_target->request_interrupt) ();
cf30a8e1 757 }
c906108c 758}
7390519e
PA
759
760/* Check if the remote side sent us an interrupt request (^C). */
761void
762check_remote_input_interrupt_request (void)
763{
764 /* This function may be called before establishing communications,
765 therefore we need to validate the remote descriptor. */
766
767 if (remote_desc == INVALID_DESCRIPTOR)
768 return;
769
770 input_interrupt (0);
771}
b80864fb
DJ
772
773/* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
774 accept Control-C from the client, and must be disabled when talking to
775 the client. */
c906108c 776
a20d5e98 777static void
62ea82f5
DJ
778unblock_async_io (void)
779{
b80864fb 780#ifndef USE_WIN32API
62ea82f5 781 sigset_t sigio_set;
a20d5e98 782
62ea82f5
DJ
783 sigemptyset (&sigio_set);
784 sigaddset (&sigio_set, SIGIO);
785 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
b80864fb 786#endif
62ea82f5
DJ
787}
788
ac8c974e
AR
789#ifdef __QNX__
790static void
791nto_comctrl (int enable)
792{
793 struct sigevent event;
794
795 if (enable)
796 {
797 event.sigev_notify = SIGEV_SIGNAL_THREAD;
798 event.sigev_signo = SIGIO;
799 event.sigev_code = 0;
800 event.sigev_value.sival_ptr = NULL;
801 event.sigev_priority = -1;
802 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
803 &event);
804 }
805 else
806 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
807}
808#endif /* __QNX__ */
809
810
fd500816
DJ
811/* Current state of asynchronous I/O. */
812static int async_io_enabled;
813
814/* Enable asynchronous I/O. */
c906108c 815void
fba45db2 816enable_async_io (void)
c906108c 817{
fd500816
DJ
818 if (async_io_enabled)
819 return;
820
b80864fb 821#ifndef USE_WIN32API
c906108c 822 signal (SIGIO, input_interrupt);
b80864fb 823#endif
fd500816 824 async_io_enabled = 1;
ac8c974e
AR
825#ifdef __QNX__
826 nto_comctrl (1);
827#endif /* __QNX__ */
c906108c
SS
828}
829
fd500816 830/* Disable asynchronous I/O. */
c906108c 831void
fba45db2 832disable_async_io (void)
c906108c 833{
fd500816
DJ
834 if (!async_io_enabled)
835 return;
836
b80864fb 837#ifndef USE_WIN32API
c906108c 838 signal (SIGIO, SIG_IGN);
b80864fb 839#endif
fd500816 840 async_io_enabled = 0;
ac8c974e
AR
841#ifdef __QNX__
842 nto_comctrl (0);
843#endif /* __QNX__ */
844
c906108c
SS
845}
846
a20d5e98
DJ
847void
848initialize_async_io (void)
849{
850 /* Make sure that async I/O starts disabled. */
851 async_io_enabled = 1;
852 disable_async_io ();
853
854 /* Make sure the signal is unblocked. */
855 unblock_async_io ();
856}
857
24b066ba
DE
858/* Internal buffer used by readchar.
859 These are global to readchar because reschedule_remote needs to be
860 able to tell whether the buffer is empty. */
861
862static unsigned char readchar_buf[BUFSIZ];
863static int readchar_bufcnt = 0;
864static unsigned char *readchar_bufp;
865
c906108c
SS
866/* Returns next char from remote GDB. -1 if error. */
867
868static int
fba45db2 869readchar (void)
c906108c 870{
24b066ba 871 int ch;
c906108c 872
24b066ba
DE
873 if (readchar_bufcnt == 0)
874 {
e0f9f062 875 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
c906108c 876
24b066ba
DE
877 if (readchar_bufcnt <= 0)
878 {
879 if (readchar_bufcnt == 0)
880 fprintf (stderr, "readchar: Got EOF\n");
881 else
882 perror ("readchar");
c906108c 883
24b066ba
DE
884 return -1;
885 }
c906108c 886
24b066ba 887 readchar_bufp = readchar_buf;
c906108c
SS
888 }
889
24b066ba
DE
890 readchar_bufcnt--;
891 ch = *readchar_bufp++;
892 reschedule ();
893 return ch;
894}
895
896/* Reset the readchar state machine. */
897
898static void
899reset_readchar (void)
900{
901 readchar_bufcnt = 0;
902 if (readchar_callback != NOT_SCHEDULED)
903 {
904 delete_callback_event (readchar_callback);
905 readchar_callback = NOT_SCHEDULED;
906 }
907}
908
909/* Process remaining data in readchar_buf. */
910
911static int
912process_remaining (void *context)
913{
914 int res;
915
916 /* This is a one-shot event. */
917 readchar_callback = NOT_SCHEDULED;
918
919 if (readchar_bufcnt > 0)
920 res = handle_serial_event (0, NULL);
921 else
922 res = 0;
923
924 return res;
925}
926
927/* If there is still data in the buffer, queue another event to process it,
928 we can't sleep in select yet. */
929
930static void
931reschedule (void)
932{
933 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
934 readchar_callback = append_callback_event (process_remaining, NULL);
c906108c
SS
935}
936
937/* Read a packet from the remote machine, with error checking,
938 and store it in BUF. Returns length of packet, or negative if error. */
939
940int
fba45db2 941getpkt (char *buf)
c906108c
SS
942{
943 char *bp;
944 unsigned char csum, c1, c2;
945 int c;
946
947 while (1)
948 {
949 csum = 0;
950
951 while (1)
952 {
953 c = readchar ();
954 if (c == '$')
955 break;
956 if (remote_debug)
0d62e5e8
DJ
957 {
958 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
959 fflush (stderr);
960 }
961
c906108c
SS
962 if (c < 0)
963 return -1;
964 }
965
966 bp = buf;
967 while (1)
968 {
969 c = readchar ();
970 if (c < 0)
971 return -1;
972 if (c == '#')
973 break;
974 *bp++ = c;
975 csum += c;
976 }
977 *bp = 0;
978
979 c1 = fromhex (readchar ());
980 c2 = fromhex (readchar ());
c5aa993b 981
c906108c
SS
982 if (csum == (c1 << 4) + c2)
983 break;
984
a6f3e723
SL
985 if (noack_mode)
986 {
493e2a69
MS
987 fprintf (stderr,
988 "Bad checksum, sentsum=0x%x, csum=0x%x, "
989 "buf=%s [no-ack-mode, Bad medium?]\n",
a6f3e723
SL
990 (c1 << 4) + c2, csum, buf);
991 /* Not much we can do, GDB wasn't expecting an ack/nac. */
992 break;
993 }
994
c906108c
SS
995 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
996 (c1 << 4) + c2, csum, buf);
e0f9f062 997 if (write_prim ("-", 1) != 1)
e581f2b4 998 return -1;
c906108c
SS
999 }
1000
a6f3e723 1001 if (!noack_mode)
0d62e5e8 1002 {
a6f3e723
SL
1003 if (remote_debug)
1004 {
1005 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
1006 fflush (stderr);
1007 }
c906108c 1008
e0f9f062 1009 if (write_prim ("+", 1) != 1)
e581f2b4 1010 return -1;
c906108c 1011
a6f3e723
SL
1012 if (remote_debug)
1013 {
1014 fprintf (stderr, "[sent ack]\n");
1015 fflush (stderr);
1016 }
0d62e5e8 1017 }
86b1f9c5
PM
1018 else
1019 {
1020 if (remote_debug)
1021 {
1022 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf);
1023 fflush (stderr);
1024 }
1025 }
0d62e5e8 1026
c906108c
SS
1027 return bp - buf;
1028}
1029
1030void
fba45db2 1031write_ok (char *buf)
c906108c
SS
1032{
1033 buf[0] = 'O';
1034 buf[1] = 'K';
1035 buf[2] = '\0';
1036}
1037
1038void
fba45db2 1039write_enn (char *buf)
c906108c 1040{
c89dc5d4 1041 /* Some day, we should define the meanings of the error codes... */
c906108c 1042 buf[0] = 'E';
c89dc5d4
DJ
1043 buf[1] = '0';
1044 buf[2] = '1';
c906108c
SS
1045 buf[3] = '\0';
1046}
1047
0fb4aa4b
PA
1048#endif
1049
0fb4aa4b 1050#ifndef IN_PROCESS_AGENT
c906108c 1051
c906108c 1052static char *
442ea881 1053outreg (struct regcache *regcache, int regno, char *buf)
c906108c 1054{
5c44784c
JM
1055 if ((regno >> 12) != 0)
1056 *buf++ = tohex ((regno >> 12) & 0xf);
1057 if ((regno >> 8) != 0)
1058 *buf++ = tohex ((regno >> 8) & 0xf);
1059 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
1060 *buf++ = tohex (regno & 0xf);
1061 *buf++ = ':';
442ea881 1062 collect_register_as_string (regcache, regno, buf);
3aee8918 1063 buf += 2 * register_size (regcache->tdesc, regno);
c906108c
SS
1064 *buf++ = ';';
1065
1066 return buf;
1067}
1068
0d62e5e8
DJ
1069void
1070new_thread_notify (int id)
1071{
1072 char own_buf[256];
1073
1074 /* The `n' response is not yet part of the remote protocol. Do nothing. */
1075 if (1)
1076 return;
1077
1078 if (server_waiting == 0)
1079 return;
1080
1081 sprintf (own_buf, "n%x", id);
1082 disable_async_io ();
1083 putpkt (own_buf);
1084 enable_async_io ();
1085}
1086
1087void
1088dead_thread_notify (int id)
1089{
1090 char own_buf[256];
1091
1092 /* The `x' response is not yet part of the remote protocol. Do nothing. */
1093 if (1)
1094 return;
1095
1096 sprintf (own_buf, "x%x", id);
1097 disable_async_io ();
1098 putpkt (own_buf);
1099 enable_async_io ();
1100}
1101
c906108c 1102void
95954743 1103prepare_resume_reply (char *buf, ptid_t ptid,
5b1c542e 1104 struct target_waitstatus *status)
c906108c 1105{
5b1c542e 1106 if (debug_threads)
87ce2a04
DE
1107 debug_printf ("Writing resume reply for %s:%d\n",
1108 target_pid_to_str (ptid), status->kind);
c906108c 1109
5b1c542e 1110 switch (status->kind)
c906108c 1111 {
5b1c542e
PA
1112 case TARGET_WAITKIND_STOPPED:
1113 {
1114 struct thread_info *saved_inferior;
1115 const char **regp;
442ea881 1116 struct regcache *regcache;
e013ee27 1117
5b1c542e
PA
1118 sprintf (buf, "T%02x", status->value.sig);
1119 buf += strlen (buf);
e013ee27 1120
5b1c542e 1121 saved_inferior = current_inferior;
e013ee27 1122
e09875d4 1123 current_inferior = find_thread_ptid (ptid);
e013ee27 1124
3aee8918
PA
1125 regp = current_target_desc ()->expedite_regs;
1126
442ea881
PA
1127 regcache = get_thread_regcache (current_inferior, 1);
1128
5b1c542e
PA
1129 if (the_target->stopped_by_watchpoint != NULL
1130 && (*the_target->stopped_by_watchpoint) ())
1131 {
1132 CORE_ADDR addr;
1133 int i;
c906108c 1134
5b1c542e
PA
1135 strncpy (buf, "watch:", 6);
1136 buf += 6;
0d62e5e8 1137
5b1c542e 1138 addr = (*the_target->stopped_data_address) ();
255e7678 1139
5b1c542e
PA
1140 /* Convert each byte of the address into two hexadecimal
1141 chars. Note that we take sizeof (void *) instead of
1142 sizeof (addr); this is to avoid sending a 64-bit
1143 address to a 32-bit GDB. */
1144 for (i = sizeof (void *) * 2; i > 0; i--)
1145 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1146 *buf++ = ';';
1147 }
1148
1149 while (*regp)
1150 {
3aee8918 1151 buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
5b1c542e
PA
1152 regp ++;
1153 }
5472f405 1154 *buf = '\0';
5b1c542e
PA
1155
1156 /* Formerly, if the debugger had not used any thread features
1157 we would not burden it with a thread status response. This
1158 was for the benefit of GDB 4.13 and older. However, in
1159 recent GDB versions the check (``if (cont_thread != 0)'')
1160 does not have the desired effect because of sillyness in
1161 the way that the remote protocol handles specifying a
1162 thread. Since thread support relies on qSymbol support
1163 anyway, assume GDB can handle threads. */
1164
1165 if (using_threads && !disable_packet_Tthread)
1166 {
1167 /* This if (1) ought to be unnecessary. But remote_wait
1168 in GDB will claim this event belongs to inferior_ptid
1169 if we do not specify a thread, and there's no way for
1170 gdbserver to know what inferior_ptid is. */
95954743 1171 if (1 || !ptid_equal (general_thread, ptid))
5b1c542e 1172 {
dc146f7c 1173 int core = -1;
bd99dc85
PA
1174 /* In non-stop, don't change the general thread behind
1175 GDB's back. */
1176 if (!non_stop)
1177 general_thread = ptid;
95954743
PA
1178 sprintf (buf, "thread:");
1179 buf += strlen (buf);
1180 buf = write_ptid (buf, ptid);
1181 strcat (buf, ";");
5b1c542e 1182 buf += strlen (buf);
dc146f7c 1183
3e10640f
YQ
1184 core = target_core_of_thread (ptid);
1185
dc146f7c
VP
1186 if (core != -1)
1187 {
1188 sprintf (buf, "core:");
1189 buf += strlen (buf);
1190 sprintf (buf, "%x", core);
1191 strcat (buf, ";");
1192 buf += strlen (buf);
1193 }
5b1c542e
PA
1194 }
1195 }
1196
1197 if (dlls_changed)
1198 {
1199 strcpy (buf, "library:;");
1200 buf += strlen (buf);
1201 dlls_changed = 0;
1202 }
1203
1204 current_inferior = saved_inferior;
1205 }
1206 break;
1207 case TARGET_WAITKIND_EXITED:
95954743
PA
1208 if (multi_process)
1209 sprintf (buf, "W%x;process:%x",
1210 status->value.integer, ptid_get_pid (ptid));
1211 else
1212 sprintf (buf, "W%02x", status->value.integer);
5b1c542e
PA
1213 break;
1214 case TARGET_WAITKIND_SIGNALLED:
95954743
PA
1215 if (multi_process)
1216 sprintf (buf, "X%x;process:%x",
1217 status->value.sig, ptid_get_pid (ptid));
1218 else
1219 sprintf (buf, "X%02x", status->value.sig);
5b1c542e
PA
1220 break;
1221 default:
1222 error ("unhandled waitkind");
1223 break;
c906108c 1224 }
c906108c
SS
1225}
1226
1227void
fba45db2 1228decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
1229{
1230 int i = 0, j = 0;
1231 char ch;
1232 *mem_addr_ptr = *len_ptr = 0;
1233
1234 while ((ch = from[i++]) != ',')
1235 {
1236 *mem_addr_ptr = *mem_addr_ptr << 4;
1237 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1238 }
1239
1240 for (j = 0; j < 4; j++)
1241 {
1242 if ((ch = from[i++]) == 0)
1243 break;
1244 *len_ptr = *len_ptr << 4;
1245 *len_ptr |= fromhex (ch) & 0x0f;
1246 }
1247}
1248
1249void
fba45db2 1250decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
fa593d66 1251 unsigned char **to_p)
c906108c
SS
1252{
1253 int i = 0;
1254 char ch;
1255 *mem_addr_ptr = *len_ptr = 0;
1256
1257 while ((ch = from[i++]) != ',')
1258 {
1259 *mem_addr_ptr = *mem_addr_ptr << 4;
1260 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1261 }
1262
1263 while ((ch = from[i++]) != ':')
1264 {
1265 *len_ptr = *len_ptr << 4;
1266 *len_ptr |= fromhex (ch) & 0x0f;
1267 }
1268
fa593d66
PA
1269 if (*to_p == NULL)
1270 *to_p = xmalloc (*len_ptr);
1271
a7191e8b 1272 hex2bin (&from[i++], *to_p, *len_ptr);
c906108c 1273}
2f2893d9 1274
01f9e8fa
DJ
1275int
1276decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
fa593d66 1277 unsigned int *len_ptr, unsigned char **to_p)
01f9e8fa
DJ
1278{
1279 int i = 0;
1280 char ch;
1281 *mem_addr_ptr = *len_ptr = 0;
1282
1283 while ((ch = from[i++]) != ',')
1284 {
1285 *mem_addr_ptr = *mem_addr_ptr << 4;
1286 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1287 }
1288
1289 while ((ch = from[i++]) != ':')
1290 {
1291 *len_ptr = *len_ptr << 4;
1292 *len_ptr |= fromhex (ch) & 0x0f;
1293 }
1294
fa593d66
PA
1295 if (*to_p == NULL)
1296 *to_p = xmalloc (*len_ptr);
1297
01f9e8fa 1298 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
fa593d66 1299 *to_p, *len_ptr) != *len_ptr)
01f9e8fa
DJ
1300 return -1;
1301
1302 return 0;
1303}
1304
0e7f50da 1305/* Decode a qXfer write request. */
d08aafef 1306
0e7f50da 1307int
d08aafef 1308decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
0e7f50da
UW
1309 unsigned int *len, unsigned char *data)
1310{
1311 char ch;
d08aafef 1312 char *b = buf;
0e7f50da
UW
1313
1314 /* Extract the offset. */
1315 *offset = 0;
1316 while ((ch = *buf++) != ':')
1317 {
1318 *offset = *offset << 4;
1319 *offset |= fromhex (ch) & 0x0f;
1320 }
1321
1322 /* Get encoded data. */
d08aafef 1323 packet_len -= buf - b;
0e7f50da
UW
1324 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1325 data, packet_len);
1326 return 0;
1327}
1328
08388c79
DE
1329/* Decode the parameters of a qSearch:memory packet. */
1330
1331int
1332decode_search_memory_packet (const char *buf, int packet_len,
1333 CORE_ADDR *start_addrp,
1334 CORE_ADDR *search_space_lenp,
1335 gdb_byte *pattern, unsigned int *pattern_lenp)
1336{
1337 const char *p = buf;
1338
1339 p = decode_address_to_semicolon (start_addrp, p);
1340 p = decode_address_to_semicolon (search_space_lenp, p);
1341 packet_len -= p - buf;
1342 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1343 pattern, packet_len);
1344 return 0;
1345}
1346
95954743
PA
1347static void
1348free_sym_cache (struct sym_cache *sym)
1349{
1350 if (sym != NULL)
1351 {
1352 free (sym->name);
1353 free (sym);
1354 }
1355}
1356
1357void
1358clear_symbol_cache (struct sym_cache **symcache_p)
1359{
1360 struct sym_cache *sym, *next;
1361
1362 /* Check the cache first. */
1363 for (sym = *symcache_p; sym; sym = next)
1364 {
1365 next = sym->next;
1366 free_sym_cache (sym);
1367 }
1368
1369 *symcache_p = NULL;
1370}
1371
9836d6ea
PA
1372/* Get the address of NAME, and return it in ADDRP if found. if
1373 MAY_ASK_GDB is false, assume symbol cache misses are failures.
fd500816
DJ
1374 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1375
2f2893d9 1376int
9836d6ea 1377look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
2f2893d9
DJ
1378{
1379 char own_buf[266], *p, *q;
1380 int len;
fd500816 1381 struct sym_cache *sym;
95954743
PA
1382 struct process_info *proc;
1383
1384 proc = current_process ();
fd500816
DJ
1385
1386 /* Check the cache first. */
95954743 1387 for (sym = proc->symbol_cache; sym; sym = sym->next)
fd500816
DJ
1388 if (strcmp (name, sym->name) == 0)
1389 {
1390 *addrp = sym->addr;
1391 return 1;
1392 }
2f2893d9 1393
9836d6ea
PA
1394 /* It might not be an appropriate time to look up a symbol,
1395 e.g. while we're trying to fetch registers. */
1396 if (!may_ask_gdb)
ea025f5f
DJ
1397 return 0;
1398
2f2893d9
DJ
1399 /* Send the request. */
1400 strcpy (own_buf, "qSymbol:");
971dc0b8
TT
1401 bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"),
1402 strlen (name));
2f2893d9
DJ
1403 if (putpkt (own_buf) < 0)
1404 return -1;
1405
1406 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1407 len = getpkt (own_buf);
1408 if (len < 0)
1409 return -1;
1410
2bbe3cc1
DJ
1411 /* We ought to handle pretty much any packet at this point while we
1412 wait for the qSymbol "response". That requires re-entering the
1413 main loop. For now, this is an adequate approximation; allow
1414 GDB to read from memory while it figures out the address of the
1415 symbol. */
1416 while (own_buf[0] == 'm')
1417 {
1418 CORE_ADDR mem_addr;
1419 unsigned char *mem_buf;
1420 unsigned int mem_len;
1421
1422 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
bca929d3 1423 mem_buf = xmalloc (mem_len);
2bbe3cc1 1424 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
e9371aff 1425 bin2hex (mem_buf, own_buf, mem_len);
2bbe3cc1
DJ
1426 else
1427 write_enn (own_buf);
1428 free (mem_buf);
1429 if (putpkt (own_buf) < 0)
1430 return -1;
1431 len = getpkt (own_buf);
1432 if (len < 0)
1433 return -1;
1434 }
1b3f6016 1435
2f2893d9
DJ
1436 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
1437 {
2bbe3cc1 1438 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
2f2893d9
DJ
1439 return -1;
1440 }
1441
1442 p = own_buf + strlen ("qSymbol:");
1443 q = p;
1444 while (*q && *q != ':')
1445 q++;
1446
1447 /* Make sure we found a value for the symbol. */
1448 if (p == q || *q == '\0')
1449 return 0;
1450
1451 decode_address (addrp, p, q - p);
fd500816
DJ
1452
1453 /* Save the symbol in our cache. */
bca929d3
DE
1454 sym = xmalloc (sizeof (*sym));
1455 sym->name = xstrdup (name);
fd500816 1456 sym->addr = *addrp;
95954743
PA
1457 sym->next = proc->symbol_cache;
1458 proc->symbol_cache = sym;
fd500816 1459
2f2893d9
DJ
1460 return 1;
1461}
c74d0ad8 1462
fa593d66
PA
1463/* Relocate an instruction to execute at a different address. OLDLOC
1464 is the address in the inferior memory where the instruction to
1465 relocate is currently at. On input, TO points to the destination
1466 where we want the instruction to be copied (and possibly adjusted)
1467 to. On output, it points to one past the end of the resulting
1468 instruction(s). The effect of executing the instruction at TO
86a30030 1469 shall be the same as if executing it at OLDLOC. For example, call
fa593d66
PA
1470 instructions that implicitly push the return address on the stack
1471 should be adjusted to return to the instruction after OLDLOC;
1472 relative branches, and other PC-relative instructions need the
1473 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1474
1475int
1476relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1477{
1478 char own_buf[266];
1479 int len;
1480 ULONGEST written = 0;
1481
1482 /* Send the request. */
1483 strcpy (own_buf, "qRelocInsn:");
1484 sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1485 paddress (*to));
1486 if (putpkt (own_buf) < 0)
1487 return -1;
1488
1489 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1490 len = getpkt (own_buf);
1491 if (len < 0)
1492 return -1;
1493
1494 /* We ought to handle pretty much any packet at this point while we
1495 wait for the qRelocInsn "response". That requires re-entering
1496 the main loop. For now, this is an adequate approximation; allow
1497 GDB to access memory. */
1498 while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X')
1499 {
1500 CORE_ADDR mem_addr;
1501 unsigned char *mem_buf = NULL;
1502 unsigned int mem_len;
1503
1504 if (own_buf[0] == 'm')
1505 {
1506 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
1507 mem_buf = xmalloc (mem_len);
1508 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
e9371aff 1509 bin2hex (mem_buf, own_buf, mem_len);
fa593d66
PA
1510 else
1511 write_enn (own_buf);
1512 }
1513 else if (own_buf[0] == 'X')
1514 {
1515 if (decode_X_packet (&own_buf[1], len - 1, &mem_addr,
1516 &mem_len, &mem_buf) < 0
1517 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
1518 write_enn (own_buf);
1519 else
1520 write_ok (own_buf);
1521 }
1522 else
1523 {
1524 decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf);
1525 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1526 write_ok (own_buf);
1527 else
1528 write_enn (own_buf);
1529 }
1530 free (mem_buf);
1531 if (putpkt (own_buf) < 0)
1532 return -1;
1533 len = getpkt (own_buf);
1534 if (len < 0)
1535 return -1;
1536 }
1537
1538 if (own_buf[0] == 'E')
1539 {
1540 warning ("An error occurred while relocating an instruction: %s\n",
1541 own_buf);
1542 return -1;
1543 }
1544
1545 if (strncmp (own_buf, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
1546 {
1547 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1548 own_buf);
1549 return -1;
1550 }
1551
1552 unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written);
1553
1554 *to += written;
1555 return 0;
1556}
1557
c74d0ad8 1558void
bce7165d 1559monitor_output (const char *msg)
c74d0ad8 1560{
0a822afb
TT
1561 int len = strlen (msg);
1562 char *buf = xmalloc (len * 2 + 2);
c74d0ad8
DJ
1563
1564 buf[0] = 'O';
971dc0b8 1565 bin2hex ((const gdb_byte *) msg, buf + 1, len);
c74d0ad8
DJ
1566
1567 putpkt (buf);
1568 free (buf);
1569}
255e7678 1570
0fb4aa4b 1571#endif
This page took 1.183948 seconds and 4 git commands to generate.