Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / gdbserver / remote-utils.c
blobb8634e877fe31f8c64af702362647f9f7296f336
1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include "server.h"
24 #include "terminal.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/file.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #include <netdb.h>
32 #include <netinet/tcp.h>
33 #include <sys/ioctl.h>
34 #include <signal.h>
35 #include <fcntl.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38 #include <arpa/inet.h>
40 #ifndef HAVE_SOCKLEN_T
41 typedef int socklen_t;
42 #endif
44 /* A cache entry for a successfully looked-up symbol. */
45 struct sym_cache
47 const char *name;
48 CORE_ADDR addr;
49 struct sym_cache *next;
52 /* The symbol cache. */
53 static struct sym_cache *symbol_cache;
55 int remote_debug = 0;
56 struct ui_file *gdb_stdlog;
58 static int remote_desc;
60 /* FIXME headerize? */
61 extern int using_threads;
62 extern int debug_threads;
64 /* Open a connection to a remote debugger.
65 NAME is the filename used for communication. */
67 void
68 remote_open (char *name)
70 int save_fcntl_flags;
72 if (!strchr (name, ':'))
74 remote_desc = open (name, O_RDWR);
75 if (remote_desc < 0)
76 perror_with_name ("Could not open remote device");
78 #ifdef HAVE_TERMIOS
80 struct termios termios;
81 tcgetattr (remote_desc, &termios);
83 termios.c_iflag = 0;
84 termios.c_oflag = 0;
85 termios.c_lflag = 0;
86 termios.c_cflag &= ~(CSIZE | PARENB);
87 termios.c_cflag |= CLOCAL | CS8;
88 termios.c_cc[VMIN] = 1;
89 termios.c_cc[VTIME] = 0;
91 tcsetattr (remote_desc, TCSANOW, &termios);
93 #endif
95 #ifdef HAVE_TERMIO
97 struct termio termio;
98 ioctl (remote_desc, TCGETA, &termio);
100 termio.c_iflag = 0;
101 termio.c_oflag = 0;
102 termio.c_lflag = 0;
103 termio.c_cflag &= ~(CSIZE | PARENB);
104 termio.c_cflag |= CLOCAL | CS8;
105 termio.c_cc[VMIN] = 1;
106 termio.c_cc[VTIME] = 0;
108 ioctl (remote_desc, TCSETA, &termio);
110 #endif
112 #ifdef HAVE_SGTTY
114 struct sgttyb sg;
116 ioctl (remote_desc, TIOCGETP, &sg);
117 sg.sg_flags = RAW;
118 ioctl (remote_desc, TIOCSETP, &sg);
120 #endif
122 fprintf (stderr, "Remote debugging using %s\n", name);
124 else
126 char *port_str;
127 int port;
128 struct sockaddr_in sockaddr;
129 socklen_t tmp;
130 int tmp_desc;
132 port_str = strchr (name, ':');
134 port = atoi (port_str + 1);
136 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
137 if (tmp_desc < 0)
138 perror_with_name ("Can't open socket");
140 /* Allow rapid reuse of this port. */
141 tmp = 1;
142 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
143 sizeof (tmp));
145 sockaddr.sin_family = PF_INET;
146 sockaddr.sin_port = htons (port);
147 sockaddr.sin_addr.s_addr = INADDR_ANY;
149 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
150 || listen (tmp_desc, 1))
151 perror_with_name ("Can't bind address");
153 fprintf (stderr, "Listening on port %d\n", port);
155 tmp = sizeof (sockaddr);
156 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
157 if (remote_desc == -1)
158 perror_with_name ("Accept failed");
160 /* Enable TCP keep alive process. */
161 tmp = 1;
162 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
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));
170 close (tmp_desc); /* No longer need this */
172 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
173 exits when the remote side dies. */
175 /* Convert IP address to string. */
176 fprintf (stderr, "Remote debugging from host %s\n",
177 inet_ntoa (sockaddr.sin_addr));
180 #if defined(F_SETFL) && defined (FASYNC)
181 save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
182 fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
183 #if defined (F_SETOWN)
184 fcntl (remote_desc, F_SETOWN, getpid ());
185 #endif
186 #endif
187 disable_async_io ();
190 void
191 remote_close (void)
193 close (remote_desc);
196 /* Convert hex digit A to a number. */
198 static int
199 fromhex (int a)
201 if (a >= '0' && a <= '9')
202 return a - '0';
203 else if (a >= 'a' && a <= 'f')
204 return a - 'a' + 10;
205 else
206 error ("Reply contains invalid hex digit");
207 return 0;
211 unhexify (char *bin, const char *hex, int count)
213 int i;
215 for (i = 0; i < count; i++)
217 if (hex[0] == 0 || hex[1] == 0)
219 /* Hex string is short, or of uneven length.
220 Return the count that has been converted so far. */
221 return i;
223 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
224 hex += 2;
226 return i;
229 static void
230 decode_address (CORE_ADDR *addrp, const char *start, int len)
232 CORE_ADDR addr;
233 char ch;
234 int i;
236 addr = 0;
237 for (i = 0; i < len; i++)
239 ch = start[i];
240 addr = addr << 4;
241 addr = addr | (fromhex (ch) & 0x0f);
243 *addrp = addr;
246 /* Convert number NIB to a hex digit. */
248 static int
249 tohex (int nib)
251 if (nib < 10)
252 return '0' + nib;
253 else
254 return 'a' + nib - 10;
258 hexify (char *hex, const char *bin, int count)
260 int i;
262 /* May use a length, or a nul-terminated string as input. */
263 if (count == 0)
264 count = strlen (bin);
266 for (i = 0; i < count; i++)
268 *hex++ = tohex ((*bin >> 4) & 0xf);
269 *hex++ = tohex (*bin++ & 0xf);
271 *hex = 0;
272 return i;
275 /* Send a packet to the remote machine, with error checking.
276 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
279 putpkt (char *buf)
281 int i;
282 unsigned char csum = 0;
283 char *buf2;
284 char buf3[1];
285 int cnt = strlen (buf);
286 char *p;
288 buf2 = malloc (PBUFSIZ);
290 /* Copy the packet into buffer BUF2, encapsulating it
291 and giving it a checksum. */
293 p = buf2;
294 *p++ = '$';
296 for (i = 0; i < cnt; i++)
298 csum += buf[i];
299 *p++ = buf[i];
301 *p++ = '#';
302 *p++ = tohex ((csum >> 4) & 0xf);
303 *p++ = tohex (csum & 0xf);
305 *p = '\0';
307 /* Send it over and over until we get a positive ack. */
311 int cc;
313 if (write (remote_desc, buf2, p - buf2) != p - buf2)
315 perror ("putpkt(write)");
316 return -1;
319 if (remote_debug)
321 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
322 fflush (stderr);
324 cc = read (remote_desc, buf3, 1);
325 if (remote_debug)
327 fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
328 fflush (stderr);
331 if (cc <= 0)
333 if (cc == 0)
334 fprintf (stderr, "putpkt(read): Got EOF\n");
335 else
336 perror ("putpkt(read)");
338 free (buf2);
339 return -1;
342 /* Check for an input interrupt while we're here. */
343 if (buf3[0] == '\003')
344 (*the_target->send_signal) (SIGINT);
346 while (buf3[0] != '+');
348 free (buf2);
349 return 1; /* Success! */
352 /* Come here when we get an input interrupt from the remote side. This
353 interrupt should only be active while we are waiting for the child to do
354 something. About the only thing that should come through is a ^C, which
355 will cause us to send a SIGINT to the child. */
357 static void
358 input_interrupt (int unused)
360 fd_set readset;
361 struct timeval immediate = { 0, 0 };
363 /* Protect against spurious interrupts. This has been observed to
364 be a problem under NetBSD 1.4 and 1.5. */
366 FD_ZERO (&readset);
367 FD_SET (remote_desc, &readset);
368 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
370 int cc;
371 char c = 0;
373 cc = read (remote_desc, &c, 1);
375 if (cc != 1 || c != '\003')
377 fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
378 cc, c, c);
379 return;
382 (*the_target->send_signal) (SIGINT);
386 void
387 block_async_io (void)
389 sigset_t sigio_set;
390 sigemptyset (&sigio_set);
391 sigaddset (&sigio_set, SIGIO);
392 sigprocmask (SIG_BLOCK, &sigio_set, NULL);
395 void
396 unblock_async_io (void)
398 sigset_t sigio_set;
399 sigemptyset (&sigio_set);
400 sigaddset (&sigio_set, SIGIO);
401 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
404 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
405 accept Control-C from the client, and must be disabled when talking to
406 the client. */
408 /* Current state of asynchronous I/O. */
409 static int async_io_enabled;
411 /* Enable asynchronous I/O. */
412 void
413 enable_async_io (void)
415 if (async_io_enabled)
416 return;
418 signal (SIGIO, input_interrupt);
419 async_io_enabled = 1;
422 /* Disable asynchronous I/O. */
423 void
424 disable_async_io (void)
426 if (!async_io_enabled)
427 return;
429 signal (SIGIO, SIG_IGN);
430 async_io_enabled = 0;
433 /* Returns next char from remote GDB. -1 if error. */
435 static int
436 readchar (void)
438 static char buf[BUFSIZ];
439 static int bufcnt = 0;
440 static char *bufp;
442 if (bufcnt-- > 0)
443 return *bufp++ & 0x7f;
445 bufcnt = read (remote_desc, buf, sizeof (buf));
447 if (bufcnt <= 0)
449 if (bufcnt == 0)
450 fprintf (stderr, "readchar: Got EOF\n");
451 else
452 perror ("readchar");
454 return -1;
457 bufp = buf;
458 bufcnt--;
459 return *bufp++ & 0x7f;
462 /* Read a packet from the remote machine, with error checking,
463 and store it in BUF. Returns length of packet, or negative if error. */
466 getpkt (char *buf)
468 char *bp;
469 unsigned char csum, c1, c2;
470 int c;
472 while (1)
474 csum = 0;
476 while (1)
478 c = readchar ();
479 if (c == '$')
480 break;
481 if (remote_debug)
483 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
484 fflush (stderr);
487 if (c < 0)
488 return -1;
491 bp = buf;
492 while (1)
494 c = readchar ();
495 if (c < 0)
496 return -1;
497 if (c == '#')
498 break;
499 *bp++ = c;
500 csum += c;
502 *bp = 0;
504 c1 = fromhex (readchar ());
505 c2 = fromhex (readchar ());
507 if (csum == (c1 << 4) + c2)
508 break;
510 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
511 (c1 << 4) + c2, csum, buf);
512 write (remote_desc, "-", 1);
515 if (remote_debug)
517 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
518 fflush (stderr);
521 write (remote_desc, "+", 1);
523 if (remote_debug)
525 fprintf (stderr, "[sent ack]\n");
526 fflush (stderr);
529 return bp - buf;
532 void
533 write_ok (char *buf)
535 buf[0] = 'O';
536 buf[1] = 'K';
537 buf[2] = '\0';
540 void
541 write_enn (char *buf)
543 /* Some day, we should define the meanings of the error codes... */
544 buf[0] = 'E';
545 buf[1] = '0';
546 buf[2] = '1';
547 buf[3] = '\0';
550 void
551 convert_int_to_ascii (unsigned char *from, char *to, int n)
553 int nib;
554 int ch;
555 while (n--)
557 ch = *from++;
558 nib = ((ch & 0xf0) >> 4) & 0x0f;
559 *to++ = tohex (nib);
560 nib = ch & 0x0f;
561 *to++ = tohex (nib);
563 *to++ = 0;
567 void
568 convert_ascii_to_int (char *from, unsigned char *to, int n)
570 int nib1, nib2;
571 while (n--)
573 nib1 = fromhex (*from++);
574 nib2 = fromhex (*from++);
575 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
579 static char *
580 outreg (int regno, char *buf)
582 if ((regno >> 12) != 0)
583 *buf++ = tohex ((regno >> 12) & 0xf);
584 if ((regno >> 8) != 0)
585 *buf++ = tohex ((regno >> 8) & 0xf);
586 *buf++ = tohex ((regno >> 4) & 0xf);
587 *buf++ = tohex (regno & 0xf);
588 *buf++ = ':';
589 collect_register_as_string (regno, buf);
590 buf += 2 * register_size (regno);
591 *buf++ = ';';
593 return buf;
596 void
597 new_thread_notify (int id)
599 char own_buf[256];
601 /* The `n' response is not yet part of the remote protocol. Do nothing. */
602 if (1)
603 return;
605 if (server_waiting == 0)
606 return;
608 sprintf (own_buf, "n%x", id);
609 disable_async_io ();
610 putpkt (own_buf);
611 enable_async_io ();
614 void
615 dead_thread_notify (int id)
617 char own_buf[256];
619 /* The `x' response is not yet part of the remote protocol. Do nothing. */
620 if (1)
621 return;
623 sprintf (own_buf, "x%x", id);
624 disable_async_io ();
625 putpkt (own_buf);
626 enable_async_io ();
629 void
630 prepare_resume_reply (char *buf, char status, unsigned char signo)
632 int nib, sig;
634 *buf++ = status;
636 sig = (int)target_signal_from_host (signo);
638 nib = ((sig & 0xf0) >> 4);
639 *buf++ = tohex (nib);
640 nib = sig & 0x0f;
641 *buf++ = tohex (nib);
643 if (status == 'T')
645 const char **regp = gdbserver_expedite_regs;
647 if (the_target->stopped_by_watchpoint != NULL
648 && (*the_target->stopped_by_watchpoint) ())
650 CORE_ADDR addr;
651 int i;
653 strncpy (buf, "watch:", 6);
654 buf += 6;
656 addr = (*the_target->stopped_data_address) ();
658 /* Convert each byte of the address into two hexadecimal chars.
659 Note that we take sizeof (void *) instead of sizeof (addr);
660 this is to avoid sending a 64-bit address to a 32-bit GDB. */
661 for (i = sizeof (void *) * 2; i > 0; i--)
663 *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
665 *buf++ = ';';
668 while (*regp)
670 buf = outreg (find_regno (*regp), buf);
671 regp ++;
674 /* Formerly, if the debugger had not used any thread features we would not
675 burden it with a thread status response. This was for the benefit of
676 GDB 4.13 and older. However, in recent GDB versions the check
677 (``if (cont_thread != 0)'') does not have the desired effect because of
678 sillyness in the way that the remote protocol handles specifying a thread.
679 Since thread support relies on qSymbol support anyway, assume GDB can handle
680 threads. */
682 if (using_threads)
684 unsigned int gdb_id_from_wait;
686 /* FIXME right place to set this? */
687 thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id;
688 gdb_id_from_wait = thread_to_gdb_id (current_inferior);
690 if (debug_threads)
691 fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait);
692 /* This if (1) ought to be unnecessary. But remote_wait in GDB
693 will claim this event belongs to inferior_ptid if we do not
694 specify a thread, and there's no way for gdbserver to know
695 what inferior_ptid is. */
696 if (1 || old_thread_from_wait != thread_from_wait)
698 general_thread = thread_from_wait;
699 sprintf (buf, "thread:%x;", gdb_id_from_wait);
700 buf += strlen (buf);
701 old_thread_from_wait = thread_from_wait;
705 /* For W and X, we're done. */
706 *buf++ = 0;
709 void
710 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
712 int i = 0, j = 0;
713 char ch;
714 *mem_addr_ptr = *len_ptr = 0;
716 while ((ch = from[i++]) != ',')
718 *mem_addr_ptr = *mem_addr_ptr << 4;
719 *mem_addr_ptr |= fromhex (ch) & 0x0f;
722 for (j = 0; j < 4; j++)
724 if ((ch = from[i++]) == 0)
725 break;
726 *len_ptr = *len_ptr << 4;
727 *len_ptr |= fromhex (ch) & 0x0f;
731 void
732 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
733 unsigned char *to)
735 int i = 0;
736 char ch;
737 *mem_addr_ptr = *len_ptr = 0;
739 while ((ch = from[i++]) != ',')
741 *mem_addr_ptr = *mem_addr_ptr << 4;
742 *mem_addr_ptr |= fromhex (ch) & 0x0f;
745 while ((ch = from[i++]) != ':')
747 *len_ptr = *len_ptr << 4;
748 *len_ptr |= fromhex (ch) & 0x0f;
751 convert_ascii_to_int (&from[i++], to, *len_ptr);
754 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
755 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
758 look_up_one_symbol (const char *name, CORE_ADDR *addrp)
760 char own_buf[266], *p, *q;
761 int len;
762 struct sym_cache *sym;
764 /* Check the cache first. */
765 for (sym = symbol_cache; sym; sym = sym->next)
766 if (strcmp (name, sym->name) == 0)
768 *addrp = sym->addr;
769 return 1;
772 /* Send the request. */
773 strcpy (own_buf, "qSymbol:");
774 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
775 if (putpkt (own_buf) < 0)
776 return -1;
778 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
779 len = getpkt (own_buf);
780 if (len < 0)
781 return -1;
783 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
785 /* Malformed response. */
786 if (remote_debug)
788 fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
789 fflush (stderr);
792 return -1;
795 p = own_buf + strlen ("qSymbol:");
796 q = p;
797 while (*q && *q != ':')
798 q++;
800 /* Make sure we found a value for the symbol. */
801 if (p == q || *q == '\0')
802 return 0;
804 decode_address (addrp, p, q - p);
806 /* Save the symbol in our cache. */
807 sym = malloc (sizeof (*sym));
808 sym->name = strdup (name);
809 sym->addr = *addrp;
810 sym->next = symbol_cache;
811 symbol_cache = sym;
813 return 1;