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