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