* linux-arm-low.c (arm_get_pc): Print out stop PC in debug mode.
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
CommitLineData
c906108c 1/* Remote utility routines for the remote server for GDB.
0a30fbc4 2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
c89dc5d4 3 2002, 2003, 2004
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
0d62e5e8
DJ
45/* FIXME headerize? */
46extern int using_threads;
47extern int debug_threads;
48
c906108c
SS
49/* Open a connection to a remote debugger.
50 NAME is the filename used for communication. */
51
52void
fba45db2 53remote_open (char *name)
c906108c
SS
54{
55 int save_fcntl_flags;
e641a1ca 56
c906108c
SS
57 if (!strchr (name, ':'))
58 {
59 remote_desc = open (name, O_RDWR);
60 if (remote_desc < 0)
61 perror_with_name ("Could not open remote device");
62
63#ifdef HAVE_TERMIOS
64 {
65 struct termios termios;
c5aa993b 66 tcgetattr (remote_desc, &termios);
c906108c
SS
67
68 termios.c_iflag = 0;
69 termios.c_oflag = 0;
70 termios.c_lflag = 0;
c5aa993b 71 termios.c_cflag &= ~(CSIZE | PARENB);
c906108c 72 termios.c_cflag |= CLOCAL | CS8;
d0608e50 73 termios.c_cc[VMIN] = 1;
c906108c
SS
74 termios.c_cc[VTIME] = 0;
75
c5aa993b 76 tcsetattr (remote_desc, TCSANOW, &termios);
c906108c
SS
77 }
78#endif
79
80#ifdef HAVE_TERMIO
81 {
82 struct termio termio;
83 ioctl (remote_desc, TCGETA, &termio);
84
85 termio.c_iflag = 0;
86 termio.c_oflag = 0;
87 termio.c_lflag = 0;
c5aa993b 88 termio.c_cflag &= ~(CSIZE | PARENB);
c906108c 89 termio.c_cflag |= CLOCAL | CS8;
d0608e50 90 termio.c_cc[VMIN] = 1;
c906108c
SS
91 termio.c_cc[VTIME] = 0;
92
93 ioctl (remote_desc, TCSETA, &termio);
94 }
95#endif
96
97#ifdef HAVE_SGTTY
98 {
99 struct sgttyb sg;
100
101 ioctl (remote_desc, TIOCGETP, &sg);
102 sg.sg_flags = RAW;
103 ioctl (remote_desc, TIOCSETP, &sg);
104 }
105#endif
106
e641a1ca 107 fprintf (stderr, "Remote debugging using %s\n", name);
c906108c
SS
108 }
109 else
110 {
111 char *port_str;
112 int port;
113 struct sockaddr_in sockaddr;
114 int tmp;
c906108c
SS
115 int tmp_desc;
116
117 port_str = strchr (name, ':');
118
119 port = atoi (port_str + 1);
120
121 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
122 if (tmp_desc < 0)
123 perror_with_name ("Can't open socket");
124
125 /* Allow rapid reuse of this port. */
126 tmp = 1;
c5aa993b
JM
127 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
128 sizeof (tmp));
c906108c
SS
129
130 sockaddr.sin_family = PF_INET;
c5aa993b 131 sockaddr.sin_port = htons (port);
c906108c
SS
132 sockaddr.sin_addr.s_addr = INADDR_ANY;
133
c5aa993b 134 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
c906108c
SS
135 || listen (tmp_desc, 1))
136 perror_with_name ("Can't bind address");
137
138 tmp = sizeof (sockaddr);
c5aa993b 139 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
c906108c
SS
140 if (remote_desc == -1)
141 perror_with_name ("Accept failed");
142
c906108c
SS
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 149 tmp = 1;
373fe97f 150 setsockopt (remote_desc, IPPROTO_TCP, 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
ce3a066d
DJ
193int
194unhexify (char *bin, const char *hex, int count)
195{
196 int i;
197
198 for (i = 0; i < count; i++)
199 {
200 if (hex[0] == 0 || hex[1] == 0)
201 {
202 /* Hex string is short, or of uneven length.
203 Return the count that has been converted so far. */
204 return i;
205 }
206 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
207 hex += 2;
208 }
209 return i;
210}
211
2f2893d9
DJ
212static void
213decode_address (CORE_ADDR *addrp, const char *start, int len)
214{
215 CORE_ADDR addr;
216 char ch;
217 int i;
218
219 addr = 0;
220 for (i = 0; i < len; i++)
221 {
222 ch = start[i];
223 addr = addr << 4;
224 addr = addr | (fromhex (ch) & 0x0f);
225 }
226 *addrp = addr;
227}
228
c906108c
SS
229/* Convert number NIB to a hex digit. */
230
231static int
fba45db2 232tohex (int nib)
c906108c
SS
233{
234 if (nib < 10)
235 return '0' + nib;
236 else
237 return 'a' + nib - 10;
238}
239
ce3a066d
DJ
240int
241hexify (char *hex, const char *bin, int count)
242{
243 int i;
244
245 /* May use a length, or a nul-terminated string as input. */
246 if (count == 0)
247 count = strlen (bin);
248
249 for (i = 0; i < count; i++)
250 {
251 *hex++ = tohex ((*bin >> 4) & 0xf);
252 *hex++ = tohex (*bin++ & 0xf);
253 }
254 *hex = 0;
255 return i;
256}
257
c906108c
SS
258/* Send a packet to the remote machine, with error checking.
259 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
260
261int
fba45db2 262putpkt (char *buf)
c906108c
SS
263{
264 int i;
265 unsigned char csum = 0;
0a30fbc4 266 char *buf2;
c906108c
SS
267 char buf3[1];
268 int cnt = strlen (buf);
269 char *p;
270
0a30fbc4
DJ
271 buf2 = malloc (PBUFSIZ);
272
c906108c
SS
273 /* Copy the packet into buffer BUF2, encapsulating it
274 and giving it a checksum. */
275
276 p = buf2;
277 *p++ = '$';
278
279 for (i = 0; i < cnt; i++)
280 {
281 csum += buf[i];
282 *p++ = buf[i];
283 }
284 *p++ = '#';
285 *p++ = tohex ((csum >> 4) & 0xf);
286 *p++ = tohex (csum & 0xf);
287
288 *p = '\0';
289
290 /* Send it over and over until we get a positive ack. */
291
292 do
293 {
294 int cc;
295
296 if (write (remote_desc, buf2, p - buf2) != p - buf2)
297 {
298 perror ("putpkt(write)");
299 return -1;
300 }
301
302 if (remote_debug)
0d62e5e8
DJ
303 {
304 fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
305 fflush (stderr);
306 }
c906108c
SS
307 cc = read (remote_desc, buf3, 1);
308 if (remote_debug)
0d62e5e8
DJ
309 {
310 fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
311 fflush (stderr);
312 }
313
c906108c
SS
314 if (cc <= 0)
315 {
316 if (cc == 0)
317 fprintf (stderr, "putpkt(read): Got EOF\n");
318 else
319 perror ("putpkt(read)");
320
0a30fbc4 321 free (buf2);
c906108c
SS
322 return -1;
323 }
0d62e5e8
DJ
324
325 /* Check for an input interrupt while we're here. */
326 if (buf3[0] == '\003')
e5379b03 327 (*the_target->send_signal) (SIGINT);
c906108c
SS
328 }
329 while (buf3[0] != '+');
330
0a30fbc4 331 free (buf2);
c906108c
SS
332 return 1; /* Success! */
333}
334
335/* Come here when we get an input interrupt from the remote side. This
336 interrupt should only be active while we are waiting for the child to do
337 something. About the only thing that should come through is a ^C, which
338 will cause us to send a SIGINT to the child. */
339
340static void
0a30fbc4 341input_interrupt (int unused)
c906108c 342{
cf30a8e1
C
343 fd_set readset;
344 struct timeval immediate = { 0, 0 };
c906108c 345
cf30a8e1
C
346 /* Protect against spurious interrupts. This has been observed to
347 be a problem under NetBSD 1.4 and 1.5. */
c906108c 348
cf30a8e1
C
349 FD_ZERO (&readset);
350 FD_SET (remote_desc, &readset);
351 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
c906108c 352 {
cf30a8e1
C
353 int cc;
354 char c;
355
356 cc = read (remote_desc, &c, 1);
c906108c 357
cf30a8e1
C
358 if (cc != 1 || c != '\003')
359 {
360 fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
361 return;
362 }
363
e5379b03 364 (*the_target->send_signal) (SIGINT);
cf30a8e1 365 }
c906108c
SS
366}
367
368void
fba45db2 369enable_async_io (void)
c906108c
SS
370{
371 signal (SIGIO, input_interrupt);
372}
373
374void
fba45db2 375disable_async_io (void)
c906108c
SS
376{
377 signal (SIGIO, SIG_IGN);
378}
379
380/* Returns next char from remote GDB. -1 if error. */
381
382static int
fba45db2 383readchar (void)
c906108c
SS
384{
385 static char buf[BUFSIZ];
386 static int bufcnt = 0;
387 static char *bufp;
388
389 if (bufcnt-- > 0)
390 return *bufp++ & 0x7f;
391
392 bufcnt = read (remote_desc, buf, sizeof (buf));
393
394 if (bufcnt <= 0)
395 {
396 if (bufcnt == 0)
397 fprintf (stderr, "readchar: Got EOF\n");
398 else
399 perror ("readchar");
400
401 return -1;
402 }
403
404 bufp = buf;
405 bufcnt--;
406 return *bufp++ & 0x7f;
407}
408
409/* Read a packet from the remote machine, with error checking,
410 and store it in BUF. Returns length of packet, or negative if error. */
411
412int
fba45db2 413getpkt (char *buf)
c906108c
SS
414{
415 char *bp;
416 unsigned char csum, c1, c2;
417 int c;
418
419 while (1)
420 {
421 csum = 0;
422
423 while (1)
424 {
425 c = readchar ();
426 if (c == '$')
427 break;
428 if (remote_debug)
0d62e5e8
DJ
429 {
430 fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
431 fflush (stderr);
432 }
433
c906108c
SS
434 if (c < 0)
435 return -1;
436 }
437
438 bp = buf;
439 while (1)
440 {
441 c = readchar ();
442 if (c < 0)
443 return -1;
444 if (c == '#')
445 break;
446 *bp++ = c;
447 csum += c;
448 }
449 *bp = 0;
450
451 c1 = fromhex (readchar ());
452 c2 = fromhex (readchar ());
c5aa993b 453
c906108c
SS
454 if (csum == (c1 << 4) + c2)
455 break;
456
457 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
458 (c1 << 4) + c2, csum, buf);
459 write (remote_desc, "-", 1);
460 }
461
462 if (remote_debug)
0d62e5e8
DJ
463 {
464 fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf);
465 fflush (stderr);
466 }
c906108c
SS
467
468 write (remote_desc, "+", 1);
469
470 if (remote_debug)
0d62e5e8
DJ
471 {
472 fprintf (stderr, "[sent ack]\n");
473 fflush (stderr);
474 }
475
c906108c
SS
476 return bp - buf;
477}
478
479void
fba45db2 480write_ok (char *buf)
c906108c
SS
481{
482 buf[0] = 'O';
483 buf[1] = 'K';
484 buf[2] = '\0';
485}
486
487void
fba45db2 488write_enn (char *buf)
c906108c 489{
c89dc5d4 490 /* Some day, we should define the meanings of the error codes... */
c906108c 491 buf[0] = 'E';
c89dc5d4
DJ
492 buf[1] = '0';
493 buf[2] = '1';
c906108c
SS
494 buf[3] = '\0';
495}
496
497void
fba45db2 498convert_int_to_ascii (char *from, char *to, int n)
c906108c
SS
499{
500 int nib;
501 char ch;
502 while (n--)
503 {
504 ch = *from++;
505 nib = ((ch & 0xf0) >> 4) & 0x0f;
506 *to++ = tohex (nib);
507 nib = ch & 0x0f;
508 *to++ = tohex (nib);
509 }
510 *to++ = 0;
511}
512
513
514void
fba45db2 515convert_ascii_to_int (char *from, char *to, int n)
c906108c
SS
516{
517 int nib1, nib2;
518 while (n--)
519 {
520 nib1 = fromhex (*from++);
521 nib2 = fromhex (*from++);
522 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
523 }
524}
525
526static char *
fba45db2 527outreg (int regno, char *buf)
c906108c 528{
5c44784c
JM
529 if ((regno >> 12) != 0)
530 *buf++ = tohex ((regno >> 12) & 0xf);
531 if ((regno >> 8) != 0)
532 *buf++ = tohex ((regno >> 8) & 0xf);
533 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
534 *buf++ = tohex (regno & 0xf);
535 *buf++ = ':';
0d62e5e8
DJ
536 collect_register_as_string (regno, buf);
537 buf += 2 * register_size (regno);
c906108c
SS
538 *buf++ = ';';
539
540 return buf;
541}
542
0d62e5e8
DJ
543void
544new_thread_notify (int id)
545{
546 char own_buf[256];
547
548 /* The `n' response is not yet part of the remote protocol. Do nothing. */
549 if (1)
550 return;
551
552 if (server_waiting == 0)
553 return;
554
555 sprintf (own_buf, "n%x", id);
556 disable_async_io ();
557 putpkt (own_buf);
558 enable_async_io ();
559}
560
561void
562dead_thread_notify (int id)
563{
564 char own_buf[256];
565
566 /* The `x' response is not yet part of the remote protocol. Do nothing. */
567 if (1)
568 return;
569
570 sprintf (own_buf, "x%x", id);
571 disable_async_io ();
572 putpkt (own_buf);
573 enable_async_io ();
574}
575
c906108c 576void
fba45db2 577prepare_resume_reply (char *buf, char status, unsigned char signo)
c906108c 578{
0e98d0a7 579 int nib, sig;
c906108c
SS
580
581 *buf++ = status;
582
0e98d0a7
DJ
583 sig = (int)target_signal_from_host (signo);
584
585 nib = ((sig & 0xf0) >> 4);
c906108c 586 *buf++ = tohex (nib);
0e98d0a7 587 nib = sig & 0x0f;
c906108c
SS
588 *buf++ = tohex (nib);
589
590 if (status == 'T')
591 {
0a30fbc4
DJ
592 const char **regp = gdbserver_expedite_regs;
593 while (*regp)
5c44784c 594 {
0a30fbc4
DJ
595 buf = outreg (find_regno (*regp), buf);
596 regp ++;
5c44784c 597 }
c906108c 598
0d62e5e8
DJ
599 /* Formerly, if the debugger had not used any thread features we would not
600 burden it with a thread status response. This was for the benefit of
601 GDB 4.13 and older. However, in recent GDB versions the check
602 (``if (cont_thread != 0)'') does not have the desired effect because of
603 sillyness in the way that the remote protocol handles specifying a thread.
604 Since thread support relies on qSymbol support anyway, assume GDB can handle
605 threads. */
606
607 if (using_threads)
c906108c 608 {
0d62e5e8
DJ
609 /* FIXME right place to set this? */
610 thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id;
611 if (debug_threads)
612 fprintf (stderr, "Writing resume reply for %d\n\n", thread_from_wait);
c906108c
SS
613 if (old_thread_from_wait != thread_from_wait)
614 {
0d62e5e8 615 general_thread = thread_from_wait;
c906108c
SS
616 sprintf (buf, "thread:%x;", thread_from_wait);
617 buf += strlen (buf);
618 old_thread_from_wait = thread_from_wait;
619 }
620 }
621 }
622 /* For W and X, we're done. */
623 *buf++ = 0;
624}
625
626void
fba45db2 627decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
628{
629 int i = 0, j = 0;
630 char ch;
631 *mem_addr_ptr = *len_ptr = 0;
632
633 while ((ch = from[i++]) != ',')
634 {
635 *mem_addr_ptr = *mem_addr_ptr << 4;
636 *mem_addr_ptr |= fromhex (ch) & 0x0f;
637 }
638
639 for (j = 0; j < 4; j++)
640 {
641 if ((ch = from[i++]) == 0)
642 break;
643 *len_ptr = *len_ptr << 4;
644 *len_ptr |= fromhex (ch) & 0x0f;
645 }
646}
647
648void
fba45db2
KB
649decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
650 char *to)
c906108c
SS
651{
652 int i = 0;
653 char ch;
654 *mem_addr_ptr = *len_ptr = 0;
655
656 while ((ch = from[i++]) != ',')
657 {
658 *mem_addr_ptr = *mem_addr_ptr << 4;
659 *mem_addr_ptr |= fromhex (ch) & 0x0f;
660 }
661
662 while ((ch = from[i++]) != ':')
663 {
664 *len_ptr = *len_ptr << 4;
665 *len_ptr |= fromhex (ch) & 0x0f;
666 }
667
668 convert_ascii_to_int (&from[i++], to, *len_ptr);
669}
2f2893d9
DJ
670
671int
672look_up_one_symbol (const char *name, CORE_ADDR *addrp)
673{
674 char own_buf[266], *p, *q;
675 int len;
676
677 /* Send the request. */
678 strcpy (own_buf, "qSymbol:");
679 hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
680 if (putpkt (own_buf) < 0)
681 return -1;
682
683 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
684 len = getpkt (own_buf);
685 if (len < 0)
686 return -1;
687
688 if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
689 {
690 /* Malformed response. */
691 if (remote_debug)
0d62e5e8
DJ
692 {
693 fprintf (stderr, "Malformed response to qSymbol, ignoring.\n");
694 fflush (stderr);
695 }
696
2f2893d9
DJ
697 return -1;
698 }
699
700 p = own_buf + strlen ("qSymbol:");
701 q = p;
702 while (*q && *q != ':')
703 q++;
704
705 /* Make sure we found a value for the symbol. */
706 if (p == q || *q == '\0')
707 return 0;
708
709 decode_address (addrp, p, q - p);
710 return 1;
711}
712
This page took 0.46865 seconds and 4 git commands to generate.