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