Skip thumb2 conditional backward search test for PE based targets.
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
CommitLineData
c906108c 1/* Remote utility routines for the remote server for GDB.
618f726f 2 Copyright (C) 1986-2016 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
9eb1356e
PA
33#if HAVE_NETINET_IN_H
34#include <netinet/in.h>
35#endif
36#if HAVE_SYS_SOCKET_H
37#include <sys/socket.h>
38#endif
b80864fb 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
438e1e42 54#include "gdb_sys_time.h"
cf30a8e1 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>
9eb1356e
PA
60
61#if USE_WIN32API
62#include <winsock2.h>
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{
9eb1356e 156 struct sockaddr_in sockaddr;
8336d594
PA
157 socklen_t tmp;
158
159 if (debug_threads)
87ce2a04 160 debug_printf ("handling possible accept event\n");
8336d594 161
9eb1356e
PA
162 tmp = sizeof (sockaddr);
163 remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
8336d594
PA
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",
9eb1356e 198 inet_ntoa (sockaddr.sin_addr));
8336d594
PA
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;
9eb1356e 227 struct sockaddr_in sockaddr;
03f2bd59
JK
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
9eb1356e
PA
272 sockaddr.sin_family = PF_INET;
273 sockaddr.sin_port = htons (port);
274 sockaddr.sin_addr.s_addr = INADDR_ANY;
03f2bd59 275
9eb1356e 276 if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
03f2bd59
JK
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
5b3da067 529static ULONGEST
95954743
PA
530hex_or_minus_one (char *buf, char **obuf)
531{
532 ULONGEST ret;
533
61012eef 534 if (startswith (buf, "-1"))
95954743
PA
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
3d40fbb5
PA
576 /* Since GDB is not sending a process id (multi-process extensions
577 are off), then there's only one process. Default to the first in
578 the list. */
579 pid = pid_of (get_first_process ());
95954743
PA
580
581 if (obuf)
582 *obuf = pp;
583 return ptid_build (pid, tid, 0);
584}
585
e0f9f062
DE
586/* Write COUNT bytes in BUF to the client.
587 The result is the number of bytes written or -1 if error.
588 This may return less than COUNT. */
589
590static int
591write_prim (const void *buf, int count)
592{
593 if (remote_connection_is_stdio ())
594 return write (fileno (stdout), buf, count);
595 else
596 return write (remote_desc, buf, count);
597}
598
599/* Read COUNT bytes from the client and store in BUF.
600 The result is the number of bytes read or -1 if error.
601 This may return less than COUNT. */
602
603static int
604read_prim (void *buf, int count)
605{
606 if (remote_connection_is_stdio ())
607 return read (fileno (stdin), buf, count);
608 else
609 return read (remote_desc, buf, count);
610}
611
c906108c 612/* Send a packet to the remote machine, with error checking.
01f9e8fa
DJ
613 The data of the packet is in BUF, and the length of the
614 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
c906108c 615
bd99dc85
PA
616static int
617putpkt_binary_1 (char *buf, int cnt, int is_notif)
c906108c
SS
618{
619 int i;
620 unsigned char csum = 0;
0a30fbc4 621 char *buf2;
c906108c 622 char *p;
bc3b5632 623 int cc;
c906108c 624
224c3ddb 625 buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
0a30fbc4 626
c906108c
SS
627 /* Copy the packet into buffer BUF2, encapsulating it
628 and giving it a checksum. */
629
630 p = buf2;
bd99dc85
PA
631 if (is_notif)
632 *p++ = '%';
633 else
634 *p++ = '$';
c906108c 635
5ffff7c1
DJ
636 for (i = 0; i < cnt;)
637 i += try_rle (buf + i, cnt - i, &csum, &p);
638
c906108c
SS
639 *p++ = '#';
640 *p++ = tohex ((csum >> 4) & 0xf);
641 *p++ = tohex (csum & 0xf);
642
643 *p = '\0';
644
645 /* Send it over and over until we get a positive ack. */
646
647 do
648 {
e0f9f062 649 if (write_prim (buf2, p - buf2) != p - buf2)
c906108c
SS
650 {
651 perror ("putpkt(write)");
f88c79e6 652 free (buf2);
c906108c
SS
653 return -1;
654 }
655
bd99dc85 656 if (noack_mode || is_notif)
a6f3e723
SL
657 {
658 /* Don't expect an ack then. */
659 if (remote_debug)
660 {
bd99dc85
PA
661 if (is_notif)
662 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2);
663 else
664 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
a6f3e723
SL
665 fflush (stderr);
666 }
667 break;
668 }
669
c906108c 670 if (remote_debug)
0d62e5e8
DJ
671 {
672 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
673 fflush (stderr);
674 }
0d62e5e8 675
bc3b5632 676 cc = readchar ();
c906108c 677
bc3b5632
DE
678 if (cc < 0)
679 {
0a30fbc4 680 free (buf2);
c906108c
SS
681 return -1;
682 }
0d62e5e8 683
bc3b5632
DE
684 if (remote_debug)
685 {
686 fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc);
687 fflush (stderr);
688 }
689
0d62e5e8 690 /* Check for an input interrupt while we're here. */
0bfdf32f 691 if (cc == '\003' && current_thread != NULL)
ef57601b 692 (*the_target->request_interrupt) ();
c906108c 693 }
bc3b5632 694 while (cc != '+');
c906108c 695
0a30fbc4 696 free (buf2);
c906108c
SS
697 return 1; /* Success! */
698}
699
bd99dc85
PA
700int
701putpkt_binary (char *buf, int cnt)
702{
703 return putpkt_binary_1 (buf, cnt, 0);
704}
705
01f9e8fa
DJ
706/* Send a packet to the remote machine, with error checking. The data
707 of the packet is in BUF, and the packet should be a NUL-terminated
708 string. Returns >= 0 on success, -1 otherwise. */
709
710int
711putpkt (char *buf)
712{
713 return putpkt_binary (buf, strlen (buf));
714}
715
bd99dc85
PA
716int
717putpkt_notif (char *buf)
718{
719 return putpkt_binary_1 (buf, strlen (buf), 1);
720}
721
c906108c
SS
722/* Come here when we get an input interrupt from the remote side. This
723 interrupt should only be active while we are waiting for the child to do
bc3b5632
DE
724 something. Thus this assumes readchar:bufcnt is 0.
725 About the only thing that should come through is a ^C, which
ef57601b 726 will cause us to request child interruption. */
c906108c
SS
727
728static void
0a30fbc4 729input_interrupt (int unused)
c906108c 730{
cf30a8e1
C
731 fd_set readset;
732 struct timeval immediate = { 0, 0 };
c906108c 733
cf30a8e1
C
734 /* Protect against spurious interrupts. This has been observed to
735 be a problem under NetBSD 1.4 and 1.5. */
c906108c 736
cf30a8e1
C
737 FD_ZERO (&readset);
738 FD_SET (remote_desc, &readset);
739 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 740 {
cf30a8e1 741 int cc;
fd500816 742 char c = 0;
7390519e 743
e0f9f062 744 cc = read_prim (&c, 1);
c906108c 745
fafcc06a 746 if (cc == 0)
cf30a8e1 747 {
fafcc06a
SDJ
748 fprintf (stderr, "client connection closed\n");
749 return;
750 }
f0db101d 751 else if (cc != 1 || c != '\003')
fafcc06a
SDJ
752 {
753 fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
754 if (isprint (c))
755 fprintf (stderr, "('%c')\n", c);
756 else
757 fprintf (stderr, "('\\x%02x')\n", c & 0xff);
cf30a8e1
C
758 return;
759 }
7390519e 760
ef57601b 761 (*the_target->request_interrupt) ();
cf30a8e1 762 }
c906108c 763}
7390519e
PA
764
765/* Check if the remote side sent us an interrupt request (^C). */
766void
767check_remote_input_interrupt_request (void)
768{
769 /* This function may be called before establishing communications,
770 therefore we need to validate the remote descriptor. */
771
772 if (remote_desc == INVALID_DESCRIPTOR)
773 return;
774
775 input_interrupt (0);
776}
b80864fb
DJ
777
778/* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
779 accept Control-C from the client, and must be disabled when talking to
780 the client. */
c906108c 781
a20d5e98 782static void
62ea82f5
DJ
783unblock_async_io (void)
784{
b80864fb 785#ifndef USE_WIN32API
62ea82f5 786 sigset_t sigio_set;
a20d5e98 787
62ea82f5
DJ
788 sigemptyset (&sigio_set);
789 sigaddset (&sigio_set, SIGIO);
790 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
b80864fb 791#endif
62ea82f5
DJ
792}
793
ac8c974e
AR
794#ifdef __QNX__
795static void
796nto_comctrl (int enable)
797{
798 struct sigevent event;
799
800 if (enable)
801 {
802 event.sigev_notify = SIGEV_SIGNAL_THREAD;
803 event.sigev_signo = SIGIO;
804 event.sigev_code = 0;
805 event.sigev_value.sival_ptr = NULL;
806 event.sigev_priority = -1;
807 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
808 &event);
809 }
810 else
811 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
812}
813#endif /* __QNX__ */
814
815
fd500816
DJ
816/* Current state of asynchronous I/O. */
817static int async_io_enabled;
818
819/* Enable asynchronous I/O. */
c906108c 820void
fba45db2 821enable_async_io (void)
c906108c 822{
fd500816
DJ
823 if (async_io_enabled)
824 return;
825
b80864fb 826#ifndef USE_WIN32API
c906108c 827 signal (SIGIO, input_interrupt);
b80864fb 828#endif
fd500816 829 async_io_enabled = 1;
ac8c974e
AR
830#ifdef __QNX__
831 nto_comctrl (1);
832#endif /* __QNX__ */
c906108c
SS
833}
834
fd500816 835/* Disable asynchronous I/O. */
c906108c 836void
fba45db2 837disable_async_io (void)
c906108c 838{
fd500816
DJ
839 if (!async_io_enabled)
840 return;
841
b80864fb 842#ifndef USE_WIN32API
c906108c 843 signal (SIGIO, SIG_IGN);
b80864fb 844#endif
fd500816 845 async_io_enabled = 0;
ac8c974e
AR
846#ifdef __QNX__
847 nto_comctrl (0);
848#endif /* __QNX__ */
849
c906108c
SS
850}
851
a20d5e98
DJ
852void
853initialize_async_io (void)
854{
855 /* Make sure that async I/O starts disabled. */
856 async_io_enabled = 1;
857 disable_async_io ();
858
859 /* Make sure the signal is unblocked. */
860 unblock_async_io ();
861}
862
24b066ba
DE
863/* Internal buffer used by readchar.
864 These are global to readchar because reschedule_remote needs to be
865 able to tell whether the buffer is empty. */
866
867static unsigned char readchar_buf[BUFSIZ];
868static int readchar_bufcnt = 0;
869static unsigned char *readchar_bufp;
870
c906108c
SS
871/* Returns next char from remote GDB. -1 if error. */
872
873static int
fba45db2 874readchar (void)
c906108c 875{
24b066ba 876 int ch;
c906108c 877
24b066ba
DE
878 if (readchar_bufcnt == 0)
879 {
e0f9f062 880 readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
c906108c 881
24b066ba
DE
882 if (readchar_bufcnt <= 0)
883 {
884 if (readchar_bufcnt == 0)
fddedbe6
PA
885 {
886 if (remote_debug)
887 fprintf (stderr, "readchar: Got EOF\n");
888 }
24b066ba
DE
889 else
890 perror ("readchar");
c906108c 891
24b066ba
DE
892 return -1;
893 }
c906108c 894
24b066ba 895 readchar_bufp = readchar_buf;
c906108c
SS
896 }
897
24b066ba
DE
898 readchar_bufcnt--;
899 ch = *readchar_bufp++;
900 reschedule ();
901 return ch;
902}
903
904/* Reset the readchar state machine. */
905
906static void
907reset_readchar (void)
908{
909 readchar_bufcnt = 0;
910 if (readchar_callback != NOT_SCHEDULED)
911 {
912 delete_callback_event (readchar_callback);
913 readchar_callback = NOT_SCHEDULED;
914 }
915}
916
917/* Process remaining data in readchar_buf. */
918
919static int
920process_remaining (void *context)
921{
922 int res;
923
924 /* This is a one-shot event. */
925 readchar_callback = NOT_SCHEDULED;
926
927 if (readchar_bufcnt > 0)
928 res = handle_serial_event (0, NULL);
929 else
930 res = 0;
931
932 return res;
933}
934
935/* If there is still data in the buffer, queue another event to process it,
936 we can't sleep in select yet. */
937
938static void
939reschedule (void)
940{
941 if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
942 readchar_callback = append_callback_event (process_remaining, NULL);
c906108c
SS
943}
944
945/* Read a packet from the remote machine, with error checking,
946 and store it in BUF. Returns length of packet, or negative if error. */
947
948int
fba45db2 949getpkt (char *buf)
c906108c
SS
950{
951 char *bp;
952 unsigned char csum, c1, c2;
953 int c;
954
955 while (1)
956 {
957 csum = 0;
958
959 while (1)
960 {
961 c = readchar ();
5a0dd67a
YQ
962
963 /* The '\003' may appear before or after each packet, so
964 check for an input interrupt. */
965 if (c == '\003')
966 {
967 (*the_target->request_interrupt) ();
968 continue;
969 }
970
c906108c
SS
971 if (c == '$')
972 break;
973 if (remote_debug)
0d62e5e8
DJ
974 {
975 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
976 fflush (stderr);
977 }
978
c906108c
SS
979 if (c < 0)
980 return -1;
981 }
982
983 bp = buf;
984 while (1)
985 {
986 c = readchar ();
987 if (c < 0)
988 return -1;
989 if (c == '#')
990 break;
991 *bp++ = c;
992 csum += c;
993 }
994 *bp = 0;
995
996 c1 = fromhex (readchar ());
997 c2 = fromhex (readchar ());
c5aa993b 998
c906108c
SS
999 if (csum == (c1 << 4) + c2)
1000 break;
1001
a6f3e723
SL
1002 if (noack_mode)
1003 {
493e2a69
MS
1004 fprintf (stderr,
1005 "Bad checksum, sentsum=0x%x, csum=0x%x, "
1006 "buf=%s [no-ack-mode, Bad medium?]\n",
a6f3e723
SL
1007 (c1 << 4) + c2, csum, buf);
1008 /* Not much we can do, GDB wasn't expecting an ack/nac. */
1009 break;
1010 }
1011
c906108c
SS
1012 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1013 (c1 << 4) + c2, csum, buf);
e0f9f062 1014 if (write_prim ("-", 1) != 1)
e581f2b4 1015 return -1;
c906108c
SS
1016 }
1017
a6f3e723 1018 if (!noack_mode)
0d62e5e8 1019 {
a6f3e723
SL
1020 if (remote_debug)
1021 {
1022 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
1023 fflush (stderr);
1024 }
c906108c 1025
e0f9f062 1026 if (write_prim ("+", 1) != 1)
e581f2b4 1027 return -1;
c906108c 1028
a6f3e723
SL
1029 if (remote_debug)
1030 {
1031 fprintf (stderr, "[sent ack]\n");
1032 fflush (stderr);
1033 }
0d62e5e8 1034 }
86b1f9c5
PM
1035 else
1036 {
1037 if (remote_debug)
1038 {
1039 fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf);
1040 fflush (stderr);
1041 }
1042 }
0d62e5e8 1043
c906108c
SS
1044 return bp - buf;
1045}
1046
1047void
fba45db2 1048write_ok (char *buf)
c906108c
SS
1049{
1050 buf[0] = 'O';
1051 buf[1] = 'K';
1052 buf[2] = '\0';
1053}
1054
1055void
fba45db2 1056write_enn (char *buf)
c906108c 1057{
c89dc5d4 1058 /* Some day, we should define the meanings of the error codes... */
c906108c 1059 buf[0] = 'E';
c89dc5d4
DJ
1060 buf[1] = '0';
1061 buf[2] = '1';
c906108c
SS
1062 buf[3] = '\0';
1063}
1064
0fb4aa4b
PA
1065#endif
1066
0fb4aa4b 1067#ifndef IN_PROCESS_AGENT
c906108c 1068
c906108c 1069static char *
442ea881 1070outreg (struct regcache *regcache, int regno, char *buf)
c906108c 1071{
5c44784c
JM
1072 if ((regno >> 12) != 0)
1073 *buf++ = tohex ((regno >> 12) & 0xf);
1074 if ((regno >> 8) != 0)
1075 *buf++ = tohex ((regno >> 8) & 0xf);
1076 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
1077 *buf++ = tohex (regno & 0xf);
1078 *buf++ = ':';
442ea881 1079 collect_register_as_string (regcache, regno, buf);
3aee8918 1080 buf += 2 * register_size (regcache->tdesc, regno);
c906108c
SS
1081 *buf++ = ';';
1082
1083 return buf;
1084}
1085
1086void
95954743 1087prepare_resume_reply (char *buf, ptid_t ptid,
5b1c542e 1088 struct target_waitstatus *status)
c906108c 1089{
5b1c542e 1090 if (debug_threads)
87ce2a04
DE
1091 debug_printf ("Writing resume reply for %s:%d\n",
1092 target_pid_to_str (ptid), status->kind);
c906108c 1093
5b1c542e 1094 switch (status->kind)
c906108c 1095 {
5b1c542e 1096 case TARGET_WAITKIND_STOPPED:
de0d863e 1097 case TARGET_WAITKIND_FORKED:
c269dbdb 1098 case TARGET_WAITKIND_VFORKED:
8228463c 1099 case TARGET_WAITKIND_VFORK_DONE:
94585166 1100 case TARGET_WAITKIND_EXECD:
65706a29 1101 case TARGET_WAITKIND_THREAD_CREATED:
82075af2
JS
1102 case TARGET_WAITKIND_SYSCALL_ENTRY:
1103 case TARGET_WAITKIND_SYSCALL_RETURN:
5b1c542e 1104 {
0bfdf32f 1105 struct thread_info *saved_thread;
5b1c542e 1106 const char **regp;
442ea881 1107 struct regcache *regcache;
e013ee27 1108
c269dbdb
DB
1109 if ((status->kind == TARGET_WAITKIND_FORKED && report_fork_events)
1110 || (status->kind == TARGET_WAITKIND_VFORKED && report_vfork_events))
de0d863e
DB
1111 {
1112 enum gdb_signal signal = GDB_SIGNAL_TRAP;
c269dbdb
DB
1113 const char *event = (status->kind == TARGET_WAITKIND_FORKED
1114 ? "fork" : "vfork");
de0d863e 1115
c269dbdb 1116 sprintf (buf, "T%02x%s:", signal, event);
de0d863e
DB
1117 buf += strlen (buf);
1118 buf = write_ptid (buf, status->value.related_pid);
1119 strcat (buf, ";");
1120 }
8228463c
PA
1121 else if (status->kind == TARGET_WAITKIND_VFORK_DONE && report_vfork_events)
1122 {
1123 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1124
1125 sprintf (buf, "T%02xvforkdone:;", signal);
1126 }
d57e0d50 1127 else if (status->kind == TARGET_WAITKIND_EXECD && report_exec_events)
94585166
DB
1128 {
1129 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1130 const char *event = "exec";
1131 char hexified_pathname[PATH_MAX * 2];
1132
1133 sprintf (buf, "T%02x%s:", signal, event);
1134 buf += strlen (buf);
1135
1136 /* Encode pathname to hexified format. */
1137 bin2hex ((const gdb_byte *) status->value.execd_pathname,
1138 hexified_pathname,
1139 strlen (status->value.execd_pathname));
1140
1141 sprintf (buf, "%s;", hexified_pathname);
1142 xfree (status->value.execd_pathname);
1143 status->value.execd_pathname = NULL;
1144 buf += strlen (buf);
1145 }
65706a29
PA
1146 else if (status->kind == TARGET_WAITKIND_THREAD_CREATED
1147 && report_thread_events)
1148 {
1149 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1150
1151 sprintf (buf, "T%02xcreate:;", signal);
1152 }
82075af2
JS
1153 else if (status->kind == TARGET_WAITKIND_SYSCALL_ENTRY
1154 || status->kind == TARGET_WAITKIND_SYSCALL_RETURN)
1155 {
1156 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1157 const char *event = (status->kind == TARGET_WAITKIND_SYSCALL_ENTRY
1158 ? "syscall_entry" : "syscall_return");
1159
1160 sprintf (buf, "T%02x%s:%x;", signal, event,
1161 status->value.syscall_number);
1162 }
de0d863e
DB
1163 else
1164 sprintf (buf, "T%02x", status->value.sig);
1165
5b1c542e 1166 buf += strlen (buf);
e013ee27 1167
0bfdf32f 1168 saved_thread = current_thread;
e013ee27 1169
0bfdf32f 1170 current_thread = find_thread_ptid (ptid);
e013ee27 1171
3aee8918
PA
1172 regp = current_target_desc ()->expedite_regs;
1173
0bfdf32f 1174 regcache = get_thread_regcache (current_thread, 1);
442ea881 1175
5b1c542e
PA
1176 if (the_target->stopped_by_watchpoint != NULL
1177 && (*the_target->stopped_by_watchpoint) ())
1178 {
1179 CORE_ADDR addr;
1180 int i;
c906108c 1181
5b1c542e
PA
1182 strncpy (buf, "watch:", 6);
1183 buf += 6;
0d62e5e8 1184
5b1c542e 1185 addr = (*the_target->stopped_data_address) ();
255e7678 1186
5b1c542e
PA
1187 /* Convert each byte of the address into two hexadecimal
1188 chars. Note that we take sizeof (void *) instead of
1189 sizeof (addr); this is to avoid sending a 64-bit
1190 address to a 32-bit GDB. */
1191 for (i = sizeof (void *) * 2; i > 0; i--)
1192 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1193 *buf++ = ';';
1194 }
1ec68e26
PA
1195 else if (swbreak_feature && target_stopped_by_sw_breakpoint ())
1196 {
1197 sprintf (buf, "swbreak:;");
1198 buf += strlen (buf);
1199 }
1200 else if (hwbreak_feature && target_stopped_by_hw_breakpoint ())
1201 {
1202 sprintf (buf, "hwbreak:;");
1203 buf += strlen (buf);
1204 }
5b1c542e
PA
1205
1206 while (*regp)
1207 {
3aee8918 1208 buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
5b1c542e
PA
1209 regp ++;
1210 }
5472f405 1211 *buf = '\0';
5b1c542e
PA
1212
1213 /* Formerly, if the debugger had not used any thread features
1214 we would not burden it with a thread status response. This
1215 was for the benefit of GDB 4.13 and older. However, in
1216 recent GDB versions the check (``if (cont_thread != 0)'')
1217 does not have the desired effect because of sillyness in
1218 the way that the remote protocol handles specifying a
1219 thread. Since thread support relies on qSymbol support
1220 anyway, assume GDB can handle threads. */
1221
1222 if (using_threads && !disable_packet_Tthread)
1223 {
1224 /* This if (1) ought to be unnecessary. But remote_wait
1225 in GDB will claim this event belongs to inferior_ptid
1226 if we do not specify a thread, and there's no way for
1227 gdbserver to know what inferior_ptid is. */
95954743 1228 if (1 || !ptid_equal (general_thread, ptid))
5b1c542e 1229 {
dc146f7c 1230 int core = -1;
bd99dc85
PA
1231 /* In non-stop, don't change the general thread behind
1232 GDB's back. */
1233 if (!non_stop)
1234 general_thread = ptid;
95954743
PA
1235 sprintf (buf, "thread:");
1236 buf += strlen (buf);
1237 buf = write_ptid (buf, ptid);
1238 strcat (buf, ";");
5b1c542e 1239 buf += strlen (buf);
dc146f7c 1240
3e10640f
YQ
1241 core = target_core_of_thread (ptid);
1242
dc146f7c
VP
1243 if (core != -1)
1244 {
1245 sprintf (buf, "core:");
1246 buf += strlen (buf);
1247 sprintf (buf, "%x", core);
1248 strcat (buf, ";");
1249 buf += strlen (buf);
1250 }
5b1c542e
PA
1251 }
1252 }
1253
1254 if (dlls_changed)
1255 {
1256 strcpy (buf, "library:;");
1257 buf += strlen (buf);
1258 dlls_changed = 0;
1259 }
1260
0bfdf32f 1261 current_thread = saved_thread;
5b1c542e
PA
1262 }
1263 break;
1264 case TARGET_WAITKIND_EXITED:
95954743
PA
1265 if (multi_process)
1266 sprintf (buf, "W%x;process:%x",
1267 status->value.integer, ptid_get_pid (ptid));
1268 else
1269 sprintf (buf, "W%02x", status->value.integer);
5b1c542e
PA
1270 break;
1271 case TARGET_WAITKIND_SIGNALLED:
95954743
PA
1272 if (multi_process)
1273 sprintf (buf, "X%x;process:%x",
1274 status->value.sig, ptid_get_pid (ptid));
1275 else
1276 sprintf (buf, "X%02x", status->value.sig);
5b1c542e 1277 break;
65706a29
PA
1278 case TARGET_WAITKIND_THREAD_EXITED:
1279 sprintf (buf, "w%x;", status->value.integer);
1280 buf += strlen (buf);
1281 buf = write_ptid (buf, ptid);
1282 break;
f2faf941
PA
1283 case TARGET_WAITKIND_NO_RESUMED:
1284 sprintf (buf, "N");
1285 break;
5b1c542e
PA
1286 default:
1287 error ("unhandled waitkind");
1288 break;
c906108c 1289 }
c906108c
SS
1290}
1291
1292void
fba45db2 1293decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
1294{
1295 int i = 0, j = 0;
1296 char ch;
1297 *mem_addr_ptr = *len_ptr = 0;
1298
1299 while ((ch = from[i++]) != ',')
1300 {
1301 *mem_addr_ptr = *mem_addr_ptr << 4;
1302 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1303 }
1304
1305 for (j = 0; j < 4; j++)
1306 {
1307 if ((ch = from[i++]) == 0)
1308 break;
1309 *len_ptr = *len_ptr << 4;
1310 *len_ptr |= fromhex (ch) & 0x0f;
1311 }
1312}
1313
1314void
fba45db2 1315decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
fa593d66 1316 unsigned char **to_p)
c906108c
SS
1317{
1318 int i = 0;
1319 char ch;
1320 *mem_addr_ptr = *len_ptr = 0;
1321
1322 while ((ch = from[i++]) != ',')
1323 {
1324 *mem_addr_ptr = *mem_addr_ptr << 4;
1325 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1326 }
1327
1328 while ((ch = from[i++]) != ':')
1329 {
1330 *len_ptr = *len_ptr << 4;
1331 *len_ptr |= fromhex (ch) & 0x0f;
1332 }
1333
fa593d66 1334 if (*to_p == NULL)
224c3ddb 1335 *to_p = (unsigned char *) xmalloc (*len_ptr);
fa593d66 1336
a7191e8b 1337 hex2bin (&from[i++], *to_p, *len_ptr);
c906108c 1338}
2f2893d9 1339
01f9e8fa
DJ
1340int
1341decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
fa593d66 1342 unsigned int *len_ptr, unsigned char **to_p)
01f9e8fa
DJ
1343{
1344 int i = 0;
1345 char ch;
1346 *mem_addr_ptr = *len_ptr = 0;
1347
1348 while ((ch = from[i++]) != ',')
1349 {
1350 *mem_addr_ptr = *mem_addr_ptr << 4;
1351 *mem_addr_ptr |= fromhex (ch) & 0x0f;
1352 }
1353
1354 while ((ch = from[i++]) != ':')
1355 {
1356 *len_ptr = *len_ptr << 4;
1357 *len_ptr |= fromhex (ch) & 0x0f;
1358 }
1359
fa593d66 1360 if (*to_p == NULL)
224c3ddb 1361 *to_p = (unsigned char *) xmalloc (*len_ptr);
fa593d66 1362
01f9e8fa 1363 if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
fa593d66 1364 *to_p, *len_ptr) != *len_ptr)
01f9e8fa
DJ
1365 return -1;
1366
1367 return 0;
1368}
1369
0e7f50da 1370/* Decode a qXfer write request. */
d08aafef 1371
0e7f50da 1372int
d08aafef 1373decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
0e7f50da
UW
1374 unsigned int *len, unsigned char *data)
1375{
1376 char ch;
d08aafef 1377 char *b = buf;
0e7f50da
UW
1378
1379 /* Extract the offset. */
1380 *offset = 0;
1381 while ((ch = *buf++) != ':')
1382 {
1383 *offset = *offset << 4;
1384 *offset |= fromhex (ch) & 0x0f;
1385 }
1386
1387 /* Get encoded data. */
d08aafef 1388 packet_len -= buf - b;
0e7f50da
UW
1389 *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1390 data, packet_len);
1391 return 0;
1392}
1393
08388c79
DE
1394/* Decode the parameters of a qSearch:memory packet. */
1395
1396int
1397decode_search_memory_packet (const char *buf, int packet_len,
1398 CORE_ADDR *start_addrp,
1399 CORE_ADDR *search_space_lenp,
1400 gdb_byte *pattern, unsigned int *pattern_lenp)
1401{
1402 const char *p = buf;
1403
1404 p = decode_address_to_semicolon (start_addrp, p);
1405 p = decode_address_to_semicolon (search_space_lenp, p);
1406 packet_len -= p - buf;
1407 *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1408 pattern, packet_len);
1409 return 0;
1410}
1411
95954743
PA
1412static void
1413free_sym_cache (struct sym_cache *sym)
1414{
1415 if (sym != NULL)
1416 {
1417 free (sym->name);
1418 free (sym);
1419 }
1420}
1421
1422void
1423clear_symbol_cache (struct sym_cache **symcache_p)
1424{
1425 struct sym_cache *sym, *next;
1426
1427 /* Check the cache first. */
1428 for (sym = *symcache_p; sym; sym = next)
1429 {
1430 next = sym->next;
1431 free_sym_cache (sym);
1432 }
1433
1434 *symcache_p = NULL;
1435}
1436
9836d6ea
PA
1437/* Get the address of NAME, and return it in ADDRP if found. if
1438 MAY_ASK_GDB is false, assume symbol cache misses are failures.
fd500816
DJ
1439 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1440
2f2893d9 1441int
9836d6ea 1442look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
2f2893d9
DJ
1443{
1444 char own_buf[266], *p, *q;
1445 int len;
fd500816 1446 struct sym_cache *sym;
95954743
PA
1447 struct process_info *proc;
1448
1449 proc = current_process ();
fd500816
DJ
1450
1451 /* Check the cache first. */
95954743 1452 for (sym = proc->symbol_cache; sym; sym = sym->next)
fd500816
DJ
1453 if (strcmp (name, sym->name) == 0)
1454 {
1455 *addrp = sym->addr;
1456 return 1;
1457 }
2f2893d9 1458
9836d6ea
PA
1459 /* It might not be an appropriate time to look up a symbol,
1460 e.g. while we're trying to fetch registers. */
1461 if (!may_ask_gdb)
ea025f5f
DJ
1462 return 0;
1463
2f2893d9
DJ
1464 /* Send the request. */
1465 strcpy (own_buf, "qSymbol:");
971dc0b8
TT
1466 bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"),
1467 strlen (name));
2f2893d9
DJ
1468 if (putpkt (own_buf) < 0)
1469 return -1;
1470
1471 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1472 len = getpkt (own_buf);
1473 if (len < 0)
1474 return -1;
1475
2bbe3cc1
DJ
1476 /* We ought to handle pretty much any packet at this point while we
1477 wait for the qSymbol "response". That requires re-entering the
1478 main loop. For now, this is an adequate approximation; allow
1479 GDB to read from memory while it figures out the address of the
1480 symbol. */
1481 while (own_buf[0] == 'm')
1482 {
1483 CORE_ADDR mem_addr;
1484 unsigned char *mem_buf;
1485 unsigned int mem_len;
1486
1487 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
224c3ddb 1488 mem_buf = (unsigned char *) xmalloc (mem_len);
2bbe3cc1 1489 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
e9371aff 1490 bin2hex (mem_buf, own_buf, mem_len);
2bbe3cc1
DJ
1491 else
1492 write_enn (own_buf);
1493 free (mem_buf);
1494 if (putpkt (own_buf) < 0)
1495 return -1;
1496 len = getpkt (own_buf);
1497 if (len < 0)
1498 return -1;
1499 }
1b3f6016 1500
61012eef 1501 if (!startswith (own_buf, "qSymbol:"))
2f2893d9 1502 {
2bbe3cc1 1503 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
2f2893d9
DJ
1504 return -1;
1505 }
1506
1507 p = own_buf + strlen ("qSymbol:");
1508 q = p;
1509 while (*q && *q != ':')
1510 q++;
1511
1512 /* Make sure we found a value for the symbol. */
1513 if (p == q || *q == '\0')
1514 return 0;
1515
1516 decode_address (addrp, p, q - p);
fd500816
DJ
1517
1518 /* Save the symbol in our cache. */
8d749320 1519 sym = XNEW (struct sym_cache);
bca929d3 1520 sym->name = xstrdup (name);
fd500816 1521 sym->addr = *addrp;
95954743
PA
1522 sym->next = proc->symbol_cache;
1523 proc->symbol_cache = sym;
fd500816 1524
2f2893d9
DJ
1525 return 1;
1526}
c74d0ad8 1527
fa593d66
PA
1528/* Relocate an instruction to execute at a different address. OLDLOC
1529 is the address in the inferior memory where the instruction to
1530 relocate is currently at. On input, TO points to the destination
1531 where we want the instruction to be copied (and possibly adjusted)
1532 to. On output, it points to one past the end of the resulting
1533 instruction(s). The effect of executing the instruction at TO
86a30030 1534 shall be the same as if executing it at OLDLOC. For example, call
fa593d66
PA
1535 instructions that implicitly push the return address on the stack
1536 should be adjusted to return to the instruction after OLDLOC;
1537 relative branches, and other PC-relative instructions need the
1538 offset adjusted; etc. Returns 0 on success, -1 on failure. */
1539
1540int
1541relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1542{
1543 char own_buf[266];
1544 int len;
1545 ULONGEST written = 0;
1546
1547 /* Send the request. */
1548 strcpy (own_buf, "qRelocInsn:");
1549 sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1550 paddress (*to));
1551 if (putpkt (own_buf) < 0)
1552 return -1;
1553
1554 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1555 len = getpkt (own_buf);
1556 if (len < 0)
1557 return -1;
1558
1559 /* We ought to handle pretty much any packet at this point while we
1560 wait for the qRelocInsn "response". That requires re-entering
1561 the main loop. For now, this is an adequate approximation; allow
1562 GDB to access memory. */
1563 while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X')
1564 {
1565 CORE_ADDR mem_addr;
1566 unsigned char *mem_buf = NULL;
1567 unsigned int mem_len;
1568
1569 if (own_buf[0] == 'm')
1570 {
1571 decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
224c3ddb 1572 mem_buf = (unsigned char *) xmalloc (mem_len);
fa593d66 1573 if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
e9371aff 1574 bin2hex (mem_buf, own_buf, mem_len);
fa593d66
PA
1575 else
1576 write_enn (own_buf);
1577 }
1578 else if (own_buf[0] == 'X')
1579 {
1580 if (decode_X_packet (&own_buf[1], len - 1, &mem_addr,
1581 &mem_len, &mem_buf) < 0
1582 || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
1583 write_enn (own_buf);
1584 else
1585 write_ok (own_buf);
1586 }
1587 else
1588 {
1589 decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf);
1590 if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1591 write_ok (own_buf);
1592 else
1593 write_enn (own_buf);
1594 }
1595 free (mem_buf);
1596 if (putpkt (own_buf) < 0)
1597 return -1;
1598 len = getpkt (own_buf);
1599 if (len < 0)
1600 return -1;
1601 }
1602
1603 if (own_buf[0] == 'E')
1604 {
1605 warning ("An error occurred while relocating an instruction: %s\n",
1606 own_buf);
1607 return -1;
1608 }
1609
61012eef 1610 if (!startswith (own_buf, "qRelocInsn:"))
fa593d66
PA
1611 {
1612 warning ("Malformed response to qRelocInsn, ignoring: %s\n",
1613 own_buf);
1614 return -1;
1615 }
1616
1617 unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written);
1618
1619 *to += written;
1620 return 0;
1621}
1622
c74d0ad8 1623void
bce7165d 1624monitor_output (const char *msg)
c74d0ad8 1625{
0a822afb 1626 int len = strlen (msg);
224c3ddb 1627 char *buf = (char *) xmalloc (len * 2 + 2);
c74d0ad8
DJ
1628
1629 buf[0] = 'O';
971dc0b8 1630 bin2hex ((const gdb_byte *) msg, buf + 1, len);
c74d0ad8
DJ
1631
1632 putpkt (buf);
1633 free (buf);
1634}
255e7678 1635
0fb4aa4b 1636#endif
This page took 1.266066 seconds and 4 git commands to generate.