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