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