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