Rewrite xcoff*_ppc_relocate_section.
[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>
0729219d 38#include <arpa/inet.h>
c906108c
SS
39
40int remote_debug = 0;
03863182 41struct ui_file *gdb_stdlog;
c906108c
SS
42
43static int remote_desc;
44
45/* Open a connection to a remote debugger.
46 NAME is the filename used for communication. */
47
48void
fba45db2 49remote_open (char *name)
c906108c
SS
50{
51 int save_fcntl_flags;
e641a1ca 52
c906108c
SS
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;
c5aa993b 62 tcgetattr (remote_desc, &termios);
c906108c
SS
63
64 termios.c_iflag = 0;
65 termios.c_oflag = 0;
66 termios.c_lflag = 0;
c5aa993b 67 termios.c_cflag &= ~(CSIZE | PARENB);
c906108c 68 termios.c_cflag |= CLOCAL | CS8;
d0608e50 69 termios.c_cc[VMIN] = 1;
c906108c
SS
70 termios.c_cc[VTIME] = 0;
71
c5aa993b 72 tcsetattr (remote_desc, TCSANOW, &termios);
c906108c
SS
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;
c5aa993b 84 termio.c_cflag &= ~(CSIZE | PARENB);
c906108c 85 termio.c_cflag |= CLOCAL | CS8;
d0608e50 86 termio.c_cc[VMIN] = 1;
c906108c
SS
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
e641a1ca 103 fprintf (stderr, "Remote debugging using %s\n", name);
c906108c
SS
104 }
105 else
106 {
107 char *port_str;
108 int port;
109 struct sockaddr_in sockaddr;
110 int tmp;
c906108c
SS
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
c906108c
SS
139 /* Enable TCP keep alive process. */
140 tmp = 1;
c5aa993b 141 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
c906108c
SS
142
143 /* Tell TCP not to delay small packets. This greatly speeds up
c5aa993b 144 interactive response. */
c906108c 145 tmp = 1;
373fe97f 146 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
c5aa993b 147 (char *) &tmp, sizeof (tmp));
c906108c
SS
148
149 close (tmp_desc); /* No longer need this */
150
c5aa993b
JM
151 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
152 exits when the remote side dies. */
e641a1ca
ML
153
154 /* Convert IP address to string. */
155 fprintf (stderr, "Remote debugging from host %s\n",
156 inet_ntoa (sockaddr.sin_addr));
c906108c
SS
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);
cf30a8e1
C
162#if defined (F_SETOWN)
163 fcntl (remote_desc, F_SETOWN, getpid ());
94dfea5d 164#endif
cf30a8e1 165#endif
c906108c 166 disable_async_io ();
c906108c
SS
167}
168
169void
fba45db2 170remote_close (void)
c906108c
SS
171{
172 close (remote_desc);
173}
174
175/* Convert hex digit A to a number. */
176
177static int
fba45db2 178fromhex (int a)
c906108c
SS
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");
0a30fbc4 186 return 0;
c906108c
SS
187}
188
ce3a066d
DJ
189int
190unhexify (char *bin, const char *hex, int count)
191{
192 int i;
193
194 for (i = 0; i < count; i++)
195 {
196 if (hex[0] == 0 || hex[1] == 0)
197 {
198 /* Hex string is short, or of uneven length.
199 Return the count that has been converted so far. */
200 return i;
201 }
202 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
203 hex += 2;
204 }
205 return i;
206}
207
2f2893d9
DJ
208static void
209decode_address (CORE_ADDR *addrp, const char *start, int len)
210{
211 CORE_ADDR addr;
212 char ch;
213 int i;
214
215 addr = 0;
216 for (i = 0; i < len; i++)
217 {
218 ch = start[i];
219 addr = addr << 4;
220 addr = addr | (fromhex (ch) & 0x0f);
221 }
222 *addrp = addr;
223}
224
c906108c
SS
225/* Convert number NIB to a hex digit. */
226
227static int
fba45db2 228tohex (int nib)
c906108c
SS
229{
230 if (nib < 10)
231 return '0' + nib;
232 else
233 return 'a' + nib - 10;
234}
235
ce3a066d
DJ
236int
237hexify (char *hex, const char *bin, int count)
238{
239 int i;
240
241 /* May use a length, or a nul-terminated string as input. */
242 if (count == 0)
243 count = strlen (bin);
244
245 for (i = 0; i < count; i++)
246 {
247 *hex++ = tohex ((*bin >> 4) & 0xf);
248 *hex++ = tohex (*bin++ & 0xf);
249 }
250 *hex = 0;
251 return i;
252}
253
c906108c
SS
254/* Send a packet to the remote machine, with error checking.
255 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
256
257int
fba45db2 258putpkt (char *buf)
c906108c
SS
259{
260 int i;
261 unsigned char csum = 0;
0a30fbc4 262 char *buf2;
c906108c
SS
263 char buf3[1];
264 int cnt = strlen (buf);
265 char *p;
266
0a30fbc4
DJ
267 buf2 = malloc (PBUFSIZ);
268
c906108c
SS
269 /* Copy the packet into buffer BUF2, encapsulating it
270 and giving it a checksum. */
271
272 p = buf2;
273 *p++ = '$';
274
275 for (i = 0; i < cnt; i++)
276 {
277 csum += buf[i];
278 *p++ = buf[i];
279 }
280 *p++ = '#';
281 *p++ = tohex ((csum >> 4) & 0xf);
282 *p++ = tohex (csum & 0xf);
283
284 *p = '\0';
285
286 /* Send it over and over until we get a positive ack. */
287
288 do
289 {
290 int cc;
291
292 if (write (remote_desc, buf2, p - buf2) != p - buf2)
293 {
294 perror ("putpkt(write)");
295 return -1;
296 }
297
298 if (remote_debug)
299 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
300 cc = read (remote_desc, buf3, 1);
301 if (remote_debug)
302 printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
303 if (cc <= 0)
304 {
305 if (cc == 0)
306 fprintf (stderr, "putpkt(read): Got EOF\n");
307 else
308 perror ("putpkt(read)");
309
0a30fbc4 310 free (buf2);
c906108c
SS
311 return -1;
312 }
313 }
314 while (buf3[0] != '+');
315
0a30fbc4 316 free (buf2);
c906108c
SS
317 return 1; /* Success! */
318}
319
320/* Come here when we get an input interrupt from the remote side. This
321 interrupt should only be active while we are waiting for the child to do
322 something. About the only thing that should come through is a ^C, which
323 will cause us to send a SIGINT to the child. */
324
325static void
0a30fbc4 326input_interrupt (int unused)
c906108c 327{
cf30a8e1
C
328 fd_set readset;
329 struct timeval immediate = { 0, 0 };
c906108c 330
cf30a8e1
C
331 /* Protect against spurious interrupts. This has been observed to
332 be a problem under NetBSD 1.4 and 1.5. */
c906108c 333
cf30a8e1
C
334 FD_ZERO (&readset);
335 FD_SET (remote_desc, &readset);
336 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 337 {
cf30a8e1
C
338 int cc;
339 char c;
340
341 cc = read (remote_desc, &c, 1);
c906108c 342
cf30a8e1
C
343 if (cc != 1 || c != '\003')
344 {
345 fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
346 return;
347 }
348
ce3a066d 349 kill (signal_pid, SIGINT);
cf30a8e1 350 }
c906108c
SS
351}
352
353void
fba45db2 354enable_async_io (void)
c906108c
SS
355{
356 signal (SIGIO, input_interrupt);
357}
358
359void
fba45db2 360disable_async_io (void)
c906108c
SS
361{
362 signal (SIGIO, SIG_IGN);
363}
364
365/* Returns next char from remote GDB. -1 if error. */
366
367static int
fba45db2 368readchar (void)
c906108c
SS
369{
370 static char buf[BUFSIZ];
371 static int bufcnt = 0;
372 static char *bufp;
373
374 if (bufcnt-- > 0)
375 return *bufp++ & 0x7f;
376
377 bufcnt = read (remote_desc, buf, sizeof (buf));
378
379 if (bufcnt <= 0)
380 {
381 if (bufcnt == 0)
382 fprintf (stderr, "readchar: Got EOF\n");
383 else
384 perror ("readchar");
385
386 return -1;
387 }
388
389 bufp = buf;
390 bufcnt--;
391 return *bufp++ & 0x7f;
392}
393
394/* Read a packet from the remote machine, with error checking,
395 and store it in BUF. Returns length of packet, or negative if error. */
396
397int
fba45db2 398getpkt (char *buf)
c906108c
SS
399{
400 char *bp;
401 unsigned char csum, c1, c2;
402 int c;
403
404 while (1)
405 {
406 csum = 0;
407
408 while (1)
409 {
410 c = readchar ();
411 if (c == '$')
412 break;
413 if (remote_debug)
414 printf ("[getpkt: discarding char '%c']\n", c);
415 if (c < 0)
416 return -1;
417 }
418
419 bp = buf;
420 while (1)
421 {
422 c = readchar ();
423 if (c < 0)
424 return -1;
425 if (c == '#')
426 break;
427 *bp++ = c;
428 csum += c;
429 }
430 *bp = 0;
431
432 c1 = fromhex (readchar ());
433 c2 = fromhex (readchar ());
c5aa993b 434
c906108c
SS
435 if (csum == (c1 << 4) + c2)
436 break;
437
438 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
439 (c1 << 4) + c2, csum, buf);
440 write (remote_desc, "-", 1);
441 }
442
443 if (remote_debug)
444 printf ("getpkt (\"%s\"); [sending ack] \n", buf);
445
446 write (remote_desc, "+", 1);
447
448 if (remote_debug)
449 printf ("[sent ack]\n");
450 return bp - buf;
451}
452
453void
fba45db2 454write_ok (char *buf)
c906108c
SS
455{
456 buf[0] = 'O';
457 buf[1] = 'K';
458 buf[2] = '\0';
459}
460
461void
fba45db2 462write_enn (char *buf)
c906108c
SS
463{
464 buf[0] = 'E';
465 buf[1] = 'N';
466 buf[2] = 'N';
467 buf[3] = '\0';
468}
469
470void
fba45db2 471convert_int_to_ascii (char *from, char *to, int n)
c906108c
SS
472{
473 int nib;
474 char ch;
475 while (n--)
476 {
477 ch = *from++;
478 nib = ((ch & 0xf0) >> 4) & 0x0f;
479 *to++ = tohex (nib);
480 nib = ch & 0x0f;
481 *to++ = tohex (nib);
482 }
483 *to++ = 0;
484}
485
486
487void
fba45db2 488convert_ascii_to_int (char *from, char *to, int n)
c906108c
SS
489{
490 int nib1, nib2;
491 while (n--)
492 {
493 nib1 = fromhex (*from++);
494 nib2 = fromhex (*from++);
495 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
496 }
497}
498
499static char *
fba45db2 500outreg (int regno, char *buf)
c906108c 501{
0a30fbc4 502 int regsize = register_size (regno);
c906108c 503
5c44784c
JM
504 if ((regno >> 12) != 0)
505 *buf++ = tohex ((regno >> 12) & 0xf);
506 if ((regno >> 8) != 0)
507 *buf++ = tohex ((regno >> 8) & 0xf);
508 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
509 *buf++ = tohex (regno & 0xf);
510 *buf++ = ':';
0a30fbc4 511 convert_int_to_ascii (register_data (regno), buf, regsize);
c906108c
SS
512 buf += 2 * regsize;
513 *buf++ = ';';
514
515 return buf;
516}
517
518void
fba45db2 519prepare_resume_reply (char *buf, char status, unsigned char signo)
c906108c 520{
0e98d0a7 521 int nib, sig;
c906108c
SS
522
523 *buf++ = status;
524
0e98d0a7
DJ
525 sig = (int)target_signal_from_host (signo);
526
527 nib = ((sig & 0xf0) >> 4);
c906108c 528 *buf++ = tohex (nib);
0e98d0a7 529 nib = sig & 0x0f;
c906108c
SS
530 *buf++ = tohex (nib);
531
532 if (status == 'T')
533 {
0a30fbc4
DJ
534 const char **regp = gdbserver_expedite_regs;
535 while (*regp)
5c44784c 536 {
0a30fbc4
DJ
537 buf = outreg (find_regno (*regp), buf);
538 regp ++;
5c44784c 539 }
c906108c
SS
540
541 /* If the debugger hasn't used any thread features, don't burden it with
0a30fbc4 542 threads. If we didn't check this, GDB 4.13 and older would choke. */
c906108c
SS
543 if (cont_thread != 0)
544 {
545 if (old_thread_from_wait != thread_from_wait)
546 {
547 sprintf (buf, "thread:%x;", thread_from_wait);
548 buf += strlen (buf);
549 old_thread_from_wait = thread_from_wait;
550 }
551 }
552 }
553 /* For W and X, we're done. */
554 *buf++ = 0;
555}
556
557void
fba45db2 558decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
559{
560 int i = 0, j = 0;
561 char ch;
562 *mem_addr_ptr = *len_ptr = 0;
563
564 while ((ch = from[i++]) != ',')
565 {
566 *mem_addr_ptr = *mem_addr_ptr << 4;
567 *mem_addr_ptr |= fromhex (ch) & 0x0f;
568 }
569
570 for (j = 0; j < 4; j++)
571 {
572 if ((ch = from[i++]) == 0)
573 break;
574 *len_ptr = *len_ptr << 4;
575 *len_ptr |= fromhex (ch) & 0x0f;
576 }
577}
578
579void
fba45db2
KB
580decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
581 char *to)
c906108c
SS
582{
583 int i = 0;
584 char ch;
585 *mem_addr_ptr = *len_ptr = 0;
586
587 while ((ch = from[i++]) != ',')
588 {
589 *mem_addr_ptr = *mem_addr_ptr << 4;
590 *mem_addr_ptr |= fromhex (ch) & 0x0f;
591 }
592
593 while ((ch = from[i++]) != ':')
594 {
595 *len_ptr = *len_ptr << 4;
596 *len_ptr |= fromhex (ch) & 0x0f;
597 }
598
599 convert_ascii_to_int (&from[i++], to, *len_ptr);
600}
2f2893d9
DJ
601
602int
603look_up_one_symbol (const char *name, CORE_ADDR *addrp)
604{
605 char own_buf[266], *p, *q;
606 int len;
607
608 /* Send the request. */
609 strcpy (own_buf, "qSymbol:");
610 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
611 if (putpkt (own_buf) < 0)
612 return -1;
613
614 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
615 len = getpkt (own_buf);
616 if (len < 0)
617 return -1;
618
619 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
620 {
621 /* Malformed response. */
622 if (remote_debug)
623 fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
624 return -1;
625 }
626
627 p = own_buf + strlen ("qSymbol:");
628 q = p;
629 while (*q && *q != ':')
630 q++;
631
632 /* Make sure we found a value for the symbol. */
633 if (p == q || *q == '\0')
634 return 0;
635
636 decode_address (addrp, p, q - p);
637 return 1;
638}
639
This page took 0.188825 seconds and 4 git commands to generate.