Commit | Line | Data |
---|---|---|
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 | |
40 | int remote_debug = 0; | |
03863182 | 41 | struct ui_file *gdb_stdlog; |
c906108c SS |
42 | |
43 | static int remote_desc; | |
44 | ||
45 | /* Open a connection to a remote debugger. | |
46 | NAME is the filename used for communication. */ | |
47 | ||
48 | void | |
fba45db2 | 49 | remote_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 | ||
169 | void | |
fba45db2 | 170 | remote_close (void) |
c906108c SS |
171 | { |
172 | close (remote_desc); | |
173 | } | |
174 | ||
175 | /* Convert hex digit A to a number. */ | |
176 | ||
177 | static int | |
fba45db2 | 178 | fromhex (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 |
189 | int |
190 | unhexify (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 | ||
c906108c SS |
208 | /* Convert number NIB to a hex digit. */ |
209 | ||
210 | static int | |
fba45db2 | 211 | tohex (int nib) |
c906108c SS |
212 | { |
213 | if (nib < 10) | |
214 | return '0' + nib; | |
215 | else | |
216 | return 'a' + nib - 10; | |
217 | } | |
218 | ||
ce3a066d DJ |
219 | int |
220 | hexify (char *hex, const char *bin, int count) | |
221 | { | |
222 | int i; | |
223 | ||
224 | /* May use a length, or a nul-terminated string as input. */ | |
225 | if (count == 0) | |
226 | count = strlen (bin); | |
227 | ||
228 | for (i = 0; i < count; i++) | |
229 | { | |
230 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
231 | *hex++ = tohex (*bin++ & 0xf); | |
232 | } | |
233 | *hex = 0; | |
234 | return i; | |
235 | } | |
236 | ||
c906108c SS |
237 | /* Send a packet to the remote machine, with error checking. |
238 | The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */ | |
239 | ||
240 | int | |
fba45db2 | 241 | putpkt (char *buf) |
c906108c SS |
242 | { |
243 | int i; | |
244 | unsigned char csum = 0; | |
0a30fbc4 | 245 | char *buf2; |
c906108c SS |
246 | char buf3[1]; |
247 | int cnt = strlen (buf); | |
248 | char *p; | |
249 | ||
0a30fbc4 DJ |
250 | buf2 = malloc (PBUFSIZ); |
251 | ||
c906108c SS |
252 | /* Copy the packet into buffer BUF2, encapsulating it |
253 | and giving it a checksum. */ | |
254 | ||
255 | p = buf2; | |
256 | *p++ = '$'; | |
257 | ||
258 | for (i = 0; i < cnt; i++) | |
259 | { | |
260 | csum += buf[i]; | |
261 | *p++ = buf[i]; | |
262 | } | |
263 | *p++ = '#'; | |
264 | *p++ = tohex ((csum >> 4) & 0xf); | |
265 | *p++ = tohex (csum & 0xf); | |
266 | ||
267 | *p = '\0'; | |
268 | ||
269 | /* Send it over and over until we get a positive ack. */ | |
270 | ||
271 | do | |
272 | { | |
273 | int cc; | |
274 | ||
275 | if (write (remote_desc, buf2, p - buf2) != p - buf2) | |
276 | { | |
277 | perror ("putpkt(write)"); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | if (remote_debug) | |
282 | printf ("putpkt (\"%s\"); [looking for ack]\n", buf2); | |
283 | cc = read (remote_desc, buf3, 1); | |
284 | if (remote_debug) | |
285 | printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]); | |
286 | if (cc <= 0) | |
287 | { | |
288 | if (cc == 0) | |
289 | fprintf (stderr, "putpkt(read): Got EOF\n"); | |
290 | else | |
291 | perror ("putpkt(read)"); | |
292 | ||
0a30fbc4 | 293 | free (buf2); |
c906108c SS |
294 | return -1; |
295 | } | |
296 | } | |
297 | while (buf3[0] != '+'); | |
298 | ||
0a30fbc4 | 299 | free (buf2); |
c906108c SS |
300 | return 1; /* Success! */ |
301 | } | |
302 | ||
303 | /* Come here when we get an input interrupt from the remote side. This | |
304 | interrupt should only be active while we are waiting for the child to do | |
305 | something. About the only thing that should come through is a ^C, which | |
306 | will cause us to send a SIGINT to the child. */ | |
307 | ||
308 | static void | |
0a30fbc4 | 309 | input_interrupt (int unused) |
c906108c | 310 | { |
cf30a8e1 C |
311 | fd_set readset; |
312 | struct timeval immediate = { 0, 0 }; | |
c906108c | 313 | |
cf30a8e1 C |
314 | /* Protect against spurious interrupts. This has been observed to |
315 | be a problem under NetBSD 1.4 and 1.5. */ | |
c906108c | 316 | |
cf30a8e1 C |
317 | FD_ZERO (&readset); |
318 | FD_SET (remote_desc, &readset); | |
319 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) | |
c906108c | 320 | { |
cf30a8e1 C |
321 | int cc; |
322 | char c; | |
323 | ||
324 | cc = read (remote_desc, &c, 1); | |
c906108c | 325 | |
cf30a8e1 C |
326 | if (cc != 1 || c != '\003') |
327 | { | |
328 | fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c); | |
329 | return; | |
330 | } | |
331 | ||
ce3a066d | 332 | kill (signal_pid, SIGINT); |
cf30a8e1 | 333 | } |
c906108c SS |
334 | } |
335 | ||
336 | void | |
fba45db2 | 337 | enable_async_io (void) |
c906108c SS |
338 | { |
339 | signal (SIGIO, input_interrupt); | |
340 | } | |
341 | ||
342 | void | |
fba45db2 | 343 | disable_async_io (void) |
c906108c SS |
344 | { |
345 | signal (SIGIO, SIG_IGN); | |
346 | } | |
347 | ||
348 | /* Returns next char from remote GDB. -1 if error. */ | |
349 | ||
350 | static int | |
fba45db2 | 351 | readchar (void) |
c906108c SS |
352 | { |
353 | static char buf[BUFSIZ]; | |
354 | static int bufcnt = 0; | |
355 | static char *bufp; | |
356 | ||
357 | if (bufcnt-- > 0) | |
358 | return *bufp++ & 0x7f; | |
359 | ||
360 | bufcnt = read (remote_desc, buf, sizeof (buf)); | |
361 | ||
362 | if (bufcnt <= 0) | |
363 | { | |
364 | if (bufcnt == 0) | |
365 | fprintf (stderr, "readchar: Got EOF\n"); | |
366 | else | |
367 | perror ("readchar"); | |
368 | ||
369 | return -1; | |
370 | } | |
371 | ||
372 | bufp = buf; | |
373 | bufcnt--; | |
374 | return *bufp++ & 0x7f; | |
375 | } | |
376 | ||
377 | /* Read a packet from the remote machine, with error checking, | |
378 | and store it in BUF. Returns length of packet, or negative if error. */ | |
379 | ||
380 | int | |
fba45db2 | 381 | getpkt (char *buf) |
c906108c SS |
382 | { |
383 | char *bp; | |
384 | unsigned char csum, c1, c2; | |
385 | int c; | |
386 | ||
387 | while (1) | |
388 | { | |
389 | csum = 0; | |
390 | ||
391 | while (1) | |
392 | { | |
393 | c = readchar (); | |
394 | if (c == '$') | |
395 | break; | |
396 | if (remote_debug) | |
397 | printf ("[getpkt: discarding char '%c']\n", c); | |
398 | if (c < 0) | |
399 | return -1; | |
400 | } | |
401 | ||
402 | bp = buf; | |
403 | while (1) | |
404 | { | |
405 | c = readchar (); | |
406 | if (c < 0) | |
407 | return -1; | |
408 | if (c == '#') | |
409 | break; | |
410 | *bp++ = c; | |
411 | csum += c; | |
412 | } | |
413 | *bp = 0; | |
414 | ||
415 | c1 = fromhex (readchar ()); | |
416 | c2 = fromhex (readchar ()); | |
c5aa993b | 417 | |
c906108c SS |
418 | if (csum == (c1 << 4) + c2) |
419 | break; | |
420 | ||
421 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
422 | (c1 << 4) + c2, csum, buf); | |
423 | write (remote_desc, "-", 1); | |
424 | } | |
425 | ||
426 | if (remote_debug) | |
427 | printf ("getpkt (\"%s\"); [sending ack] \n", buf); | |
428 | ||
429 | write (remote_desc, "+", 1); | |
430 | ||
431 | if (remote_debug) | |
432 | printf ("[sent ack]\n"); | |
433 | return bp - buf; | |
434 | } | |
435 | ||
436 | void | |
fba45db2 | 437 | write_ok (char *buf) |
c906108c SS |
438 | { |
439 | buf[0] = 'O'; | |
440 | buf[1] = 'K'; | |
441 | buf[2] = '\0'; | |
442 | } | |
443 | ||
444 | void | |
fba45db2 | 445 | write_enn (char *buf) |
c906108c SS |
446 | { |
447 | buf[0] = 'E'; | |
448 | buf[1] = 'N'; | |
449 | buf[2] = 'N'; | |
450 | buf[3] = '\0'; | |
451 | } | |
452 | ||
453 | void | |
fba45db2 | 454 | convert_int_to_ascii (char *from, char *to, int n) |
c906108c SS |
455 | { |
456 | int nib; | |
457 | char ch; | |
458 | while (n--) | |
459 | { | |
460 | ch = *from++; | |
461 | nib = ((ch & 0xf0) >> 4) & 0x0f; | |
462 | *to++ = tohex (nib); | |
463 | nib = ch & 0x0f; | |
464 | *to++ = tohex (nib); | |
465 | } | |
466 | *to++ = 0; | |
467 | } | |
468 | ||
469 | ||
470 | void | |
fba45db2 | 471 | convert_ascii_to_int (char *from, char *to, int n) |
c906108c SS |
472 | { |
473 | int nib1, nib2; | |
474 | while (n--) | |
475 | { | |
476 | nib1 = fromhex (*from++); | |
477 | nib2 = fromhex (*from++); | |
478 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); | |
479 | } | |
480 | } | |
481 | ||
482 | static char * | |
fba45db2 | 483 | outreg (int regno, char *buf) |
c906108c | 484 | { |
0a30fbc4 | 485 | int regsize = register_size (regno); |
c906108c | 486 | |
5c44784c JM |
487 | if ((regno >> 12) != 0) |
488 | *buf++ = tohex ((regno >> 12) & 0xf); | |
489 | if ((regno >> 8) != 0) | |
490 | *buf++ = tohex ((regno >> 8) & 0xf); | |
491 | *buf++ = tohex ((regno >> 4) & 0xf); | |
c906108c SS |
492 | *buf++ = tohex (regno & 0xf); |
493 | *buf++ = ':'; | |
0a30fbc4 | 494 | convert_int_to_ascii (register_data (regno), buf, regsize); |
c906108c SS |
495 | buf += 2 * regsize; |
496 | *buf++ = ';'; | |
497 | ||
498 | return buf; | |
499 | } | |
500 | ||
501 | void | |
fba45db2 | 502 | prepare_resume_reply (char *buf, char status, unsigned char signo) |
c906108c | 503 | { |
0e98d0a7 | 504 | int nib, sig; |
c906108c SS |
505 | |
506 | *buf++ = status; | |
507 | ||
0e98d0a7 DJ |
508 | sig = (int)target_signal_from_host (signo); |
509 | ||
510 | nib = ((sig & 0xf0) >> 4); | |
c906108c | 511 | *buf++ = tohex (nib); |
0e98d0a7 | 512 | nib = sig & 0x0f; |
c906108c SS |
513 | *buf++ = tohex (nib); |
514 | ||
515 | if (status == 'T') | |
516 | { | |
0a30fbc4 DJ |
517 | const char **regp = gdbserver_expedite_regs; |
518 | while (*regp) | |
5c44784c | 519 | { |
0a30fbc4 DJ |
520 | buf = outreg (find_regno (*regp), buf); |
521 | regp ++; | |
5c44784c | 522 | } |
c906108c SS |
523 | |
524 | /* If the debugger hasn't used any thread features, don't burden it with | |
0a30fbc4 | 525 | threads. If we didn't check this, GDB 4.13 and older would choke. */ |
c906108c SS |
526 | if (cont_thread != 0) |
527 | { | |
528 | if (old_thread_from_wait != thread_from_wait) | |
529 | { | |
530 | sprintf (buf, "thread:%x;", thread_from_wait); | |
531 | buf += strlen (buf); | |
532 | old_thread_from_wait = thread_from_wait; | |
533 | } | |
534 | } | |
535 | } | |
536 | /* For W and X, we're done. */ | |
537 | *buf++ = 0; | |
538 | } | |
539 | ||
540 | void | |
fba45db2 | 541 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
c906108c SS |
542 | { |
543 | int i = 0, j = 0; | |
544 | char ch; | |
545 | *mem_addr_ptr = *len_ptr = 0; | |
546 | ||
547 | while ((ch = from[i++]) != ',') | |
548 | { | |
549 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
550 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
551 | } | |
552 | ||
553 | for (j = 0; j < 4; j++) | |
554 | { | |
555 | if ((ch = from[i++]) == 0) | |
556 | break; | |
557 | *len_ptr = *len_ptr << 4; | |
558 | *len_ptr |= fromhex (ch) & 0x0f; | |
559 | } | |
560 | } | |
561 | ||
562 | void | |
fba45db2 KB |
563 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
564 | char *to) | |
c906108c SS |
565 | { |
566 | int i = 0; | |
567 | char ch; | |
568 | *mem_addr_ptr = *len_ptr = 0; | |
569 | ||
570 | while ((ch = from[i++]) != ',') | |
571 | { | |
572 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
573 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
574 | } | |
575 | ||
576 | while ((ch = from[i++]) != ':') | |
577 | { | |
578 | *len_ptr = *len_ptr << 4; | |
579 | *len_ptr |= fromhex (ch) & 0x0f; | |
580 | } | |
581 | ||
582 | convert_ascii_to_int (&from[i++], to, *len_ptr); | |
583 | } |