OSDN Git Service

2002-04-09 Daniel Jacobowitz <drow@mvista.com>
[pf3gnuchains/pf3gnuchains3x.git] / gdb / gdbserver / remote-utils.c
1 /* Remote utility routines for the remote server for GDB.
2    Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002
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., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, 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 int remote_debug = 0;
41 struct ui_file *gdb_stdlog;
42
43 static int remote_desc;
44
45 /* Open a connection to a remote debugger.
46    NAME is the filename used for communication.  */
47
48 void
49 remote_open (char *name)
50 {
51   int save_fcntl_flags;
52   
53   if (!strchr (name, ':'))
54     {
55       remote_desc = open (name, O_RDWR);
56       if (remote_desc < 0)
57         perror_with_name ("Could not open remote device");
58
59 #ifdef HAVE_TERMIOS
60       {
61         struct termios termios;
62         tcgetattr (remote_desc, &termios);
63
64         termios.c_iflag = 0;
65         termios.c_oflag = 0;
66         termios.c_lflag = 0;
67         termios.c_cflag &= ~(CSIZE | PARENB);
68         termios.c_cflag |= CLOCAL | CS8;
69         termios.c_cc[VMIN] = 1;
70         termios.c_cc[VTIME] = 0;
71
72         tcsetattr (remote_desc, TCSANOW, &termios);
73       }
74 #endif
75
76 #ifdef HAVE_TERMIO
77       {
78         struct termio termio;
79         ioctl (remote_desc, TCGETA, &termio);
80
81         termio.c_iflag = 0;
82         termio.c_oflag = 0;
83         termio.c_lflag = 0;
84         termio.c_cflag &= ~(CSIZE | PARENB);
85         termio.c_cflag |= CLOCAL | CS8;
86         termio.c_cc[VMIN] = 1;
87         termio.c_cc[VTIME] = 0;
88
89         ioctl (remote_desc, TCSETA, &termio);
90       }
91 #endif
92
93 #ifdef HAVE_SGTTY
94       {
95         struct sgttyb sg;
96
97         ioctl (remote_desc, TIOCGETP, &sg);
98         sg.sg_flags = RAW;
99         ioctl (remote_desc, TIOCSETP, &sg);
100       }
101 #endif
102
103       fprintf (stderr, "Remote debugging using %s\n", name);
104     }
105   else
106     {
107       char *port_str;
108       int port;
109       struct sockaddr_in sockaddr;
110       int tmp;
111       int tmp_desc;
112
113       port_str = strchr (name, ':');
114
115       port = atoi (port_str + 1);
116
117       tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
118       if (tmp_desc < 0)
119         perror_with_name ("Can't open socket");
120
121       /* Allow rapid reuse of this port. */
122       tmp = 1;
123       setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
124                   sizeof (tmp));
125
126       sockaddr.sin_family = PF_INET;
127       sockaddr.sin_port = htons (port);
128       sockaddr.sin_addr.s_addr = INADDR_ANY;
129
130       if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
131           || listen (tmp_desc, 1))
132         perror_with_name ("Can't bind address");
133
134       tmp = sizeof (sockaddr);
135       remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
136       if (remote_desc == -1)
137         perror_with_name ("Accept failed");
138
139       /* Enable TCP keep alive process. */
140       tmp = 1;
141       setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
142
143       /* Tell TCP not to delay small packets.  This greatly speeds up
144          interactive response. */
145       tmp = 1;
146       setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
147                   (char *) &tmp, sizeof (tmp));
148
149       close (tmp_desc);         /* No longer need this */
150
151       signal (SIGPIPE, SIG_IGN);        /* If we don't do this, then gdbserver simply
152                                            exits when the remote side dies.  */
153
154       /* Convert IP address to string.  */
155       fprintf (stderr, "Remote debugging from host %s\n", 
156          inet_ntoa (sockaddr.sin_addr));
157     }
158
159 #if defined(F_SETFL) && defined (FASYNC)
160   save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
161   fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
162 #if defined (F_SETOWN)
163   fcntl (remote_desc, F_SETOWN, getpid ());
164 #endif
165 #endif
166   disable_async_io ();
167 }
168
169 void
170 remote_close (void)
171 {
172   close (remote_desc);
173 }
174
175 /* Convert hex digit A to a number.  */
176
177 static int
178 fromhex (int a)
179 {
180   if (a >= '0' && a <= '9')
181     return a - '0';
182   else if (a >= 'a' && a <= 'f')
183     return a - 'a' + 10;
184   else
185     error ("Reply contains invalid hex digit");
186   return 0;
187 }
188
189 /* Convert number NIB to a hex digit.  */
190
191 static int
192 tohex (int nib)
193 {
194   if (nib < 10)
195     return '0' + nib;
196   else
197     return 'a' + nib - 10;
198 }
199
200 /* Send a packet to the remote machine, with error checking.
201    The data of the packet is in BUF.  Returns >= 0 on success, -1 otherwise. */
202
203 int
204 putpkt (char *buf)
205 {
206   int i;
207   unsigned char csum = 0;
208   char *buf2;
209   char buf3[1];
210   int cnt = strlen (buf);
211   char *p;
212
213   buf2 = malloc (PBUFSIZ);
214
215   /* Copy the packet into buffer BUF2, encapsulating it
216      and giving it a checksum.  */
217
218   p = buf2;
219   *p++ = '$';
220
221   for (i = 0; i < cnt; i++)
222     {
223       csum += buf[i];
224       *p++ = buf[i];
225     }
226   *p++ = '#';
227   *p++ = tohex ((csum >> 4) & 0xf);
228   *p++ = tohex (csum & 0xf);
229
230   *p = '\0';
231
232   /* Send it over and over until we get a positive ack.  */
233
234   do
235     {
236       int cc;
237
238       if (write (remote_desc, buf2, p - buf2) != p - buf2)
239         {
240           perror ("putpkt(write)");
241           return -1;
242         }
243
244       if (remote_debug)
245         printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
246       cc = read (remote_desc, buf3, 1);
247       if (remote_debug)
248         printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
249       if (cc <= 0)
250         {
251           if (cc == 0)
252             fprintf (stderr, "putpkt(read): Got EOF\n");
253           else
254             perror ("putpkt(read)");
255
256           free (buf2);
257           return -1;
258         }
259     }
260   while (buf3[0] != '+');
261
262   free (buf2);
263   return 1;                     /* Success! */
264 }
265
266 /* Come here when we get an input interrupt from the remote side.  This
267    interrupt should only be active while we are waiting for the child to do
268    something.  About the only thing that should come through is a ^C, which
269    will cause us to send a SIGINT to the child.  */
270
271 static void
272 input_interrupt (int unused)
273 {
274   fd_set readset;
275   struct timeval immediate = { 0, 0 };
276
277   /* Protect against spurious interrupts.  This has been observed to
278      be a problem under NetBSD 1.4 and 1.5.  */
279
280   FD_ZERO (&readset);
281   FD_SET (remote_desc, &readset);
282   if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
283     {
284       int cc;
285       char c;
286       
287       cc = read (remote_desc, &c, 1);
288
289       if (cc != 1 || c != '\003')
290         {
291           fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
292           return;
293         }
294       
295       kill (inferior_pid, SIGINT);
296     }
297 }
298
299 void
300 enable_async_io (void)
301 {
302   signal (SIGIO, input_interrupt);
303 }
304
305 void
306 disable_async_io (void)
307 {
308   signal (SIGIO, SIG_IGN);
309 }
310
311 /* Returns next char from remote GDB.  -1 if error.  */
312
313 static int
314 readchar (void)
315 {
316   static char buf[BUFSIZ];
317   static int bufcnt = 0;
318   static char *bufp;
319
320   if (bufcnt-- > 0)
321     return *bufp++ & 0x7f;
322
323   bufcnt = read (remote_desc, buf, sizeof (buf));
324
325   if (bufcnt <= 0)
326     {
327       if (bufcnt == 0)
328         fprintf (stderr, "readchar: Got EOF\n");
329       else
330         perror ("readchar");
331
332       return -1;
333     }
334
335   bufp = buf;
336   bufcnt--;
337   return *bufp++ & 0x7f;
338 }
339
340 /* Read a packet from the remote machine, with error checking,
341    and store it in BUF.  Returns length of packet, or negative if error. */
342
343 int
344 getpkt (char *buf)
345 {
346   char *bp;
347   unsigned char csum, c1, c2;
348   int c;
349
350   while (1)
351     {
352       csum = 0;
353
354       while (1)
355         {
356           c = readchar ();
357           if (c == '$')
358             break;
359           if (remote_debug)
360             printf ("[getpkt: discarding char '%c']\n", c);
361           if (c < 0)
362             return -1;
363         }
364
365       bp = buf;
366       while (1)
367         {
368           c = readchar ();
369           if (c < 0)
370             return -1;
371           if (c == '#')
372             break;
373           *bp++ = c;
374           csum += c;
375         }
376       *bp = 0;
377
378       c1 = fromhex (readchar ());
379       c2 = fromhex (readchar ());
380
381       if (csum == (c1 << 4) + c2)
382         break;
383
384       fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
385                (c1 << 4) + c2, csum, buf);
386       write (remote_desc, "-", 1);
387     }
388
389   if (remote_debug)
390     printf ("getpkt (\"%s\");  [sending ack] \n", buf);
391
392   write (remote_desc, "+", 1);
393
394   if (remote_debug)
395     printf ("[sent ack]\n");
396   return bp - buf;
397 }
398
399 void
400 write_ok (char *buf)
401 {
402   buf[0] = 'O';
403   buf[1] = 'K';
404   buf[2] = '\0';
405 }
406
407 void
408 write_enn (char *buf)
409 {
410   buf[0] = 'E';
411   buf[1] = 'N';
412   buf[2] = 'N';
413   buf[3] = '\0';
414 }
415
416 void
417 convert_int_to_ascii (char *from, char *to, int n)
418 {
419   int nib;
420   char ch;
421   while (n--)
422     {
423       ch = *from++;
424       nib = ((ch & 0xf0) >> 4) & 0x0f;
425       *to++ = tohex (nib);
426       nib = ch & 0x0f;
427       *to++ = tohex (nib);
428     }
429   *to++ = 0;
430 }
431
432
433 void
434 convert_ascii_to_int (char *from, char *to, int n)
435 {
436   int nib1, nib2;
437   while (n--)
438     {
439       nib1 = fromhex (*from++);
440       nib2 = fromhex (*from++);
441       *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
442     }
443 }
444
445 static char *
446 outreg (int regno, char *buf)
447 {
448   int regsize = register_size (regno);
449
450   if ((regno >> 12) != 0)
451     *buf++ = tohex ((regno >> 12) & 0xf);
452   if ((regno >> 8) != 0)
453     *buf++ = tohex ((regno >> 8) & 0xf);
454   *buf++ = tohex ((regno >> 4) & 0xf);
455   *buf++ = tohex (regno & 0xf);
456   *buf++ = ':';
457   convert_int_to_ascii (register_data (regno), buf, regsize);
458   buf += 2 * regsize;
459   *buf++ = ';';
460
461   return buf;
462 }
463
464 void
465 prepare_resume_reply (char *buf, char status, unsigned char signo)
466 {
467   int nib, sig;
468
469   *buf++ = status;
470
471   sig = (int)target_signal_from_host (signo);
472
473   nib = ((sig & 0xf0) >> 4);
474   *buf++ = tohex (nib);
475   nib = sig & 0x0f;
476   *buf++ = tohex (nib);
477
478   if (status == 'T')
479     {
480       const char **regp = gdbserver_expedite_regs;
481       while (*regp)
482         {
483           buf = outreg (find_regno (*regp), buf);
484           regp ++;
485         }
486
487       /* If the debugger hasn't used any thread features, don't burden it with
488          threads.  If we didn't check this, GDB 4.13 and older would choke.  */
489       if (cont_thread != 0)
490         {
491           if (old_thread_from_wait != thread_from_wait)
492             {
493               sprintf (buf, "thread:%x;", thread_from_wait);
494               buf += strlen (buf);
495               old_thread_from_wait = thread_from_wait;
496             }
497         }
498     }
499   /* For W and X, we're done.  */
500   *buf++ = 0;
501 }
502
503 void
504 decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
505 {
506   int i = 0, j = 0;
507   char ch;
508   *mem_addr_ptr = *len_ptr = 0;
509
510   while ((ch = from[i++]) != ',')
511     {
512       *mem_addr_ptr = *mem_addr_ptr << 4;
513       *mem_addr_ptr |= fromhex (ch) & 0x0f;
514     }
515
516   for (j = 0; j < 4; j++)
517     {
518       if ((ch = from[i++]) == 0)
519         break;
520       *len_ptr = *len_ptr << 4;
521       *len_ptr |= fromhex (ch) & 0x0f;
522     }
523 }
524
525 void
526 decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
527                  char *to)
528 {
529   int i = 0;
530   char ch;
531   *mem_addr_ptr = *len_ptr = 0;
532
533   while ((ch = from[i++]) != ',')
534     {
535       *mem_addr_ptr = *mem_addr_ptr << 4;
536       *mem_addr_ptr |= fromhex (ch) & 0x0f;
537     }
538
539   while ((ch = from[i++]) != ':')
540     {
541       *len_ptr = *len_ptr << 4;
542       *len_ptr |= fromhex (ch) & 0x0f;
543     }
544
545   convert_ascii_to_int (&from[i++], to, *len_ptr);
546 }