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