OSDN Git Service

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