* remote-utils.c (prepare_resume_reply): Move declaration
[external/binutils.git] / gdb / gdbserver / remote-utils.c
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.
5
6    This file is part of GDB.
7
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.
12
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.
17
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.  */
22
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>
39
40 #ifndef HAVE_SOCKLEN_T
41 typedef int socklen_t;
42 #endif
43
44 /* A cache entry for a successfully looked-up symbol.  */
45 struct sym_cache
46 {
47   const char *name;
48   CORE_ADDR addr;
49   struct sym_cache *next;
50 };
51
52 /* The symbol cache.  */
53 static struct sym_cache *symbol_cache;
54
55 int remote_debug = 0;
56 struct ui_file *gdb_stdlog;
57
58 static int remote_desc;
59
60 /* FIXME headerize? */
61 extern int using_threads;
62 extern int debug_threads;
63
64 /* Open a connection to a remote debugger.
65    NAME is the filename used for communication.  */
66
67 void
68 remote_open (char *name)
69 {
70   int save_fcntl_flags;
71   
72   if (!strchr (name, ':'))
73     {
74       remote_desc = open (name, O_RDWR);
75       if (remote_desc < 0)
76         perror_with_name ("Could not open remote device");
77
78 #ifdef HAVE_TERMIOS
79       {
80         struct termios termios;
81         tcgetattr (remote_desc, &termios);
82
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;
90
91         tcsetattr (remote_desc, TCSANOW, &termios);
92       }
93 #endif
94
95 #ifdef HAVE_TERMIO
96       {
97         struct termio termio;
98         ioctl (remote_desc, TCGETA, &termio);
99
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;
107
108         ioctl (remote_desc, TCSETA, &termio);
109       }
110 #endif
111
112 #ifdef HAVE_SGTTY
113       {
114         struct sgttyb sg;
115
116         ioctl (remote_desc, TIOCGETP, &sg);
117         sg.sg_flags = RAW;
118         ioctl (remote_desc, TIOCSETP, &sg);
119       }
120 #endif
121
122       fprintf (stderr, "Remote debugging using %s\n", name);
123     }
124   else
125     {
126       char *port_str;
127       int port;
128       struct sockaddr_in sockaddr;
129       socklen_t tmp;
130       int tmp_desc;
131
132       port_str = strchr (name, ':');
133
134       port = atoi (port_str + 1);
135
136       tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
137       if (tmp_desc < 0)
138         perror_with_name ("Can't open socket");
139
140       /* Allow rapid reuse of this port. */
141       tmp = 1;
142       setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
143                   sizeof (tmp));
144
145       sockaddr.sin_family = PF_INET;
146       sockaddr.sin_port = htons (port);
147       sockaddr.sin_addr.s_addr = INADDR_ANY;
148
149       if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
150           || listen (tmp_desc, 1))
151         perror_with_name ("Can't bind address");
152
153       fprintf (stderr, "Listening on port %d\n", port);
154
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");
159
160       /* Enable TCP keep alive process. */
161       tmp = 1;
162       setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (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       close (tmp_desc);         /* No longer need this */
171
172       signal (SIGPIPE, SIG_IGN);        /* If we don't do this, then gdbserver simply
173                                            exits when the remote side dies.  */
174
175       /* Convert IP address to string.  */
176       fprintf (stderr, "Remote debugging from host %s\n", 
177          inet_ntoa (sockaddr.sin_addr));
178     }
179
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 ();
188 }
189
190 void
191 remote_close (void)
192 {
193   close (remote_desc);
194 }
195
196 /* Convert hex digit A to a number.  */
197
198 static int
199 fromhex (int a)
200 {
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;
208 }
209
210 int
211 unhexify (char *bin, const char *hex, int count)
212 {
213   int i;
214
215   for (i = 0; i < count; i++)
216     {
217       if (hex[0] == 0 || hex[1] == 0)
218         {
219           /* Hex string is short, or of uneven length.
220              Return the count that has been converted so far. */
221           return i;
222         }
223       *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
224       hex += 2;
225     }
226   return i;
227 }
228
229 static void
230 decode_address (CORE_ADDR *addrp, const char *start, int len)
231 {
232   CORE_ADDR addr;
233   char ch;
234   int i;
235
236   addr = 0;
237   for (i = 0; i < len; i++)
238     {
239       ch = start[i];
240       addr = addr << 4;
241       addr = addr | (fromhex (ch) & 0x0f);
242     }
243   *addrp = addr;
244 }
245
246 /* Convert number NIB to a hex digit.  */
247
248 static int
249 tohex (int nib)
250 {
251   if (nib < 10)
252     return '0' + nib;
253   else
254     return 'a' + nib - 10;
255 }
256
257 int
258 hexify (char *hex, const char *bin, int count)
259 {
260   int i;
261
262   /* May use a length, or a nul-terminated string as input. */
263   if (count == 0)
264     count = strlen (bin);
265
266   for (i = 0; i < count; i++)
267     {
268       *hex++ = tohex ((*bin >> 4) & 0xf);
269       *hex++ = tohex (*bin++ & 0xf);
270     }
271   *hex = 0;
272   return i;
273 }
274
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. */
277
278 int
279 putpkt (char *buf)
280 {
281   int i;
282   unsigned char csum = 0;
283   char *buf2;
284   char buf3[1];
285   int cnt = strlen (buf);
286   char *p;
287
288   buf2 = malloc (PBUFSIZ);
289
290   /* Copy the packet into buffer BUF2, encapsulating it
291      and giving it a checksum.  */
292
293   p = buf2;
294   *p++ = '$';
295
296   for (i = 0; i < cnt; i++)
297     {
298       csum += buf[i];
299       *p++ = buf[i];
300     }
301   *p++ = '#';
302   *p++ = tohex ((csum >> 4) & 0xf);
303   *p++ = tohex (csum & 0xf);
304
305   *p = '\0';
306
307   /* Send it over and over until we get a positive ack.  */
308
309   do
310     {
311       int cc;
312
313       if (write (remote_desc, buf2, p - buf2) != p - buf2)
314         {
315           perror ("putpkt(write)");
316           return -1;
317         }
318
319       if (remote_debug)
320         {
321           fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
322           fflush (stderr);
323         }
324       cc = read (remote_desc, buf3, 1);
325       if (remote_debug)
326         {
327           fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
328           fflush (stderr);
329         }
330
331       if (cc <= 0)
332         {
333           if (cc == 0)
334             fprintf (stderr, "putpkt(read): Got EOF\n");
335           else
336             perror ("putpkt(read)");
337
338           free (buf2);
339           return -1;
340         }
341
342       /* Check for an input interrupt while we're here.  */
343       if (buf3[0] == '\003')
344         (*the_target->send_signal) (SIGINT);
345     }
346   while (buf3[0] != '+');
347
348   free (buf2);
349   return 1;                     /* Success! */
350 }
351
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.  */
356
357 static void
358 input_interrupt (int unused)
359 {
360   fd_set readset;
361   struct timeval immediate = { 0, 0 };
362
363   /* Protect against spurious interrupts.  This has been observed to
364      be a problem under NetBSD 1.4 and 1.5.  */
365
366   FD_ZERO (&readset);
367   FD_SET (remote_desc, &readset);
368   if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
369     {
370       int cc;
371       char c = 0;
372       
373       cc = read (remote_desc, &c, 1);
374
375       if (cc != 1 || c != '\003')
376         {
377           fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n",
378                    cc, c, c);
379           return;
380         }
381       
382       (*the_target->send_signal) (SIGINT);
383     }
384 }
385
386 void
387 block_async_io (void)
388 {
389   sigset_t sigio_set;
390   sigemptyset (&sigio_set);
391   sigaddset (&sigio_set, SIGIO);
392   sigprocmask (SIG_BLOCK, &sigio_set, NULL);
393 }
394
395 void
396 unblock_async_io (void)
397 {
398   sigset_t sigio_set;
399   sigemptyset (&sigio_set);
400   sigaddset (&sigio_set, SIGIO);
401   sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
402 }
403
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.  */
407
408 /* Current state of asynchronous I/O.  */
409 static int async_io_enabled;
410
411 /* Enable asynchronous I/O.  */
412 void
413 enable_async_io (void)
414 {
415   if (async_io_enabled)
416     return;
417
418   signal (SIGIO, input_interrupt);
419   async_io_enabled = 1;
420 }
421
422 /* Disable asynchronous I/O.  */
423 void
424 disable_async_io (void)
425 {
426   if (!async_io_enabled)
427     return;
428
429   signal (SIGIO, SIG_IGN);
430   async_io_enabled = 0;
431 }
432
433 /* Returns next char from remote GDB.  -1 if error.  */
434
435 static int
436 readchar (void)
437 {
438   static char buf[BUFSIZ];
439   static int bufcnt = 0;
440   static char *bufp;
441
442   if (bufcnt-- > 0)
443     return *bufp++ & 0x7f;
444
445   bufcnt = read (remote_desc, buf, sizeof (buf));
446
447   if (bufcnt <= 0)
448     {
449       if (bufcnt == 0)
450         fprintf (stderr, "readchar: Got EOF\n");
451       else
452         perror ("readchar");
453
454       return -1;
455     }
456
457   bufp = buf;
458   bufcnt--;
459   return *bufp++ & 0x7f;
460 }
461
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. */
464
465 int
466 getpkt (char *buf)
467 {
468   char *bp;
469   unsigned char csum, c1, c2;
470   int c;
471
472   while (1)
473     {
474       csum = 0;
475
476       while (1)
477         {
478           c = readchar ();
479           if (c == '$')
480             break;
481           if (remote_debug)
482             {
483               fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
484               fflush (stderr);
485             }
486
487           if (c < 0)
488             return -1;
489         }
490
491       bp = buf;
492       while (1)
493         {
494           c = readchar ();
495           if (c < 0)
496             return -1;
497           if (c == '#')
498             break;
499           *bp++ = c;
500           csum += c;
501         }
502       *bp = 0;
503
504       c1 = fromhex (readchar ());
505       c2 = fromhex (readchar ());
506
507       if (csum == (c1 << 4) + c2)
508         break;
509
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);
513     }
514
515   if (remote_debug)
516     {
517       fprintf (stderr, "getpkt (\"%s\");  [sending ack] \n", buf);
518       fflush (stderr);
519     }
520
521   write (remote_desc, "+", 1);
522
523   if (remote_debug)
524     {
525       fprintf (stderr, "[sent ack]\n");
526       fflush (stderr);
527     }
528
529   return bp - buf;
530 }
531
532 void
533 write_ok (char *buf)
534 {
535   buf[0] = 'O';
536   buf[1] = 'K';
537   buf[2] = '\0';
538 }
539
540 void
541 write_enn (char *buf)
542 {
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';
548 }
549
550 void
551 convert_int_to_ascii (unsigned char *from, char *to, int n)
552 {
553   int nib;
554   int ch;
555   while (n--)
556     {
557       ch = *from++;
558       nib = ((ch & 0xf0) >> 4) & 0x0f;
559       *to++ = tohex (nib);
560       nib = ch & 0x0f;
561       *to++ = tohex (nib);
562     }
563   *to++ = 0;
564 }
565
566
567 void
568 convert_ascii_to_int (char *from, unsigned char *to, int n)
569 {
570   int nib1, nib2;
571   while (n--)
572     {
573       nib1 = fromhex (*from++);
574       nib2 = fromhex (*from++);
575       *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
576     }
577 }
578
579 static char *
580 outreg (int regno, char *buf)
581 {
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++ = ';';
592
593   return buf;
594 }
595
596 void
597 new_thread_notify (int id)
598 {
599   char own_buf[256];
600
601   /* The `n' response is not yet part of the remote protocol.  Do nothing.  */
602   if (1)
603     return;
604
605   if (server_waiting == 0)
606     return;
607
608   sprintf (own_buf, "n%x", id);
609   disable_async_io ();
610   putpkt (own_buf);
611   enable_async_io ();
612 }
613
614 void
615 dead_thread_notify (int id)
616 {
617   char own_buf[256];
618
619   /* The `x' response is not yet part of the remote protocol.  Do nothing.  */
620   if (1)
621     return;
622
623   sprintf (own_buf, "x%x", id);
624   disable_async_io ();
625   putpkt (own_buf);
626   enable_async_io ();
627 }
628
629 void
630 prepare_resume_reply (char *buf, char status, unsigned char signo)
631 {
632   int nib, sig;
633
634   *buf++ = status;
635
636   sig = (int)target_signal_from_host (signo);
637
638   nib = ((sig & 0xf0) >> 4);
639   *buf++ = tohex (nib);
640   nib = sig & 0x0f;
641   *buf++ = tohex (nib);
642
643   if (status == 'T')
644     {
645       const char **regp = gdbserver_expedite_regs;
646
647       if (the_target->stopped_by_watchpoint != NULL
648           && (*the_target->stopped_by_watchpoint) ())
649         {
650           CORE_ADDR addr;
651           int i;
652
653           strncpy (buf, "watch:", 6);
654           buf += 6;
655
656           addr = (*the_target->stopped_data_address) ();
657
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--)
662             {
663               *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
664             }
665           *buf++ = ';';
666         }
667
668       while (*regp)
669         {
670           buf = outreg (find_regno (*regp), buf);
671           regp ++;
672         }
673
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.  */
681
682       if (using_threads)
683         {
684           unsigned int gdb_id_from_wait;
685
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);
689
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)
697             {
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;
702             }
703         }
704     }
705   /* For W and X, we're done.  */
706   *buf++ = 0;
707 }
708
709 void
710 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
711 {
712   int i = 0, j = 0;
713   char ch;
714   *mem_addr_ptr = *len_ptr = 0;
715
716   while ((ch = from[i++]) != ',')
717     {
718       *mem_addr_ptr = *mem_addr_ptr << 4;
719       *mem_addr_ptr |= fromhex (ch) & 0x0f;
720     }
721
722   for (j = 0; j < 4; j++)
723     {
724       if ((ch = from[i++]) == 0)
725         break;
726       *len_ptr = *len_ptr << 4;
727       *len_ptr |= fromhex (ch) & 0x0f;
728     }
729 }
730
731 void
732 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
733                  unsigned char *to)
734 {
735   int i = 0;
736   char ch;
737   *mem_addr_ptr = *len_ptr = 0;
738
739   while ((ch = from[i++]) != ',')
740     {
741       *mem_addr_ptr = *mem_addr_ptr << 4;
742       *mem_addr_ptr |= fromhex (ch) & 0x0f;
743     }
744
745   while ((ch = from[i++]) != ':')
746     {
747       *len_ptr = *len_ptr << 4;
748       *len_ptr |= fromhex (ch) & 0x0f;
749     }
750
751   convert_ascii_to_int (&from[i++], to, *len_ptr);
752 }
753
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.  */
756
757 int
758 look_up_one_symbol (const char *name, CORE_ADDR *addrp)
759 {
760   char own_buf[266], *p, *q;
761   int len;
762   struct sym_cache *sym;
763
764   /* Check the cache first.  */
765   for (sym = symbol_cache; sym; sym = sym->next)
766     if (strcmp (name, sym->name) == 0)
767       {
768         *addrp = sym->addr;
769         return 1;
770       }
771
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;
777
778   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
779   len = getpkt (own_buf);
780   if (len < 0)
781     return -1;
782
783   if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
784     {
785       /* Malformed response.  */
786       if (remote_debug)
787         {
788           fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
789           fflush (stderr);
790         }
791
792       return -1;
793     }
794
795   p = own_buf + strlen ("qSymbol:");
796   q = p;
797   while (*q && *q != ':')
798     q++;
799
800   /* Make sure we found a value for the symbol.  */
801   if (p == q || *q == '\0')
802     return 0;
803
804   decode_address (addrp, p, q - p);
805
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;
812
813   return 1;
814 }