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