Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote utility routines for the remote server for GDB. |
cf30a8e1 | 2 | Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 |
b6ba6518 | 3 | Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "server.h" | |
23 | #include "terminal.h" | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <sys/ioctl.h> | |
27 | #include <sys/file.h> | |
28 | #include <netinet/in.h> | |
29 | #include <sys/socket.h> | |
30 | #include <netdb.h> | |
31 | #include <netinet/tcp.h> | |
32 | #include <sys/ioctl.h> | |
33 | #include <signal.h> | |
34 | #include <fcntl.h> | |
cf30a8e1 C |
35 | #include <sys/time.h> |
36 | #include <unistd.h> | |
c906108c SS |
37 | |
38 | int remote_debug = 0; | |
03863182 | 39 | struct ui_file *gdb_stdlog; |
c906108c SS |
40 | |
41 | static int remote_desc; | |
42 | ||
43 | /* Open a connection to a remote debugger. | |
44 | NAME is the filename used for communication. */ | |
45 | ||
46 | void | |
fba45db2 | 47 | remote_open (char *name) |
c906108c SS |
48 | { |
49 | int save_fcntl_flags; | |
50 | ||
51 | if (!strchr (name, ':')) | |
52 | { | |
53 | remote_desc = open (name, O_RDWR); | |
54 | if (remote_desc < 0) | |
55 | perror_with_name ("Could not open remote device"); | |
56 | ||
57 | #ifdef HAVE_TERMIOS | |
58 | { | |
59 | struct termios termios; | |
c5aa993b | 60 | tcgetattr (remote_desc, &termios); |
c906108c SS |
61 | |
62 | termios.c_iflag = 0; | |
63 | termios.c_oflag = 0; | |
64 | termios.c_lflag = 0; | |
c5aa993b | 65 | termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 66 | termios.c_cflag |= CLOCAL | CS8; |
d0608e50 | 67 | termios.c_cc[VMIN] = 1; |
c906108c SS |
68 | termios.c_cc[VTIME] = 0; |
69 | ||
c5aa993b | 70 | tcsetattr (remote_desc, TCSANOW, &termios); |
c906108c SS |
71 | } |
72 | #endif | |
73 | ||
74 | #ifdef HAVE_TERMIO | |
75 | { | |
76 | struct termio termio; | |
77 | ioctl (remote_desc, TCGETA, &termio); | |
78 | ||
79 | termio.c_iflag = 0; | |
80 | termio.c_oflag = 0; | |
81 | termio.c_lflag = 0; | |
c5aa993b | 82 | termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 83 | termio.c_cflag |= CLOCAL | CS8; |
d0608e50 | 84 | termio.c_cc[VMIN] = 1; |
c906108c SS |
85 | termio.c_cc[VTIME] = 0; |
86 | ||
87 | ioctl (remote_desc, TCSETA, &termio); | |
88 | } | |
89 | #endif | |
90 | ||
91 | #ifdef HAVE_SGTTY | |
92 | { | |
93 | struct sgttyb sg; | |
94 | ||
95 | ioctl (remote_desc, TIOCGETP, &sg); | |
96 | sg.sg_flags = RAW; | |
97 | ioctl (remote_desc, TIOCSETP, &sg); | |
98 | } | |
99 | #endif | |
100 | ||
101 | ||
102 | } | |
103 | else | |
104 | { | |
105 | char *port_str; | |
106 | int port; | |
107 | struct sockaddr_in sockaddr; | |
108 | int tmp; | |
109 | struct protoent *protoent; | |
110 | int tmp_desc; | |
111 | ||
112 | port_str = strchr (name, ':'); | |
113 | ||
114 | port = atoi (port_str + 1); | |
115 | ||
116 | tmp_desc = socket (PF_INET, SOCK_STREAM, 0); | |
117 | if (tmp_desc < 0) | |
118 | perror_with_name ("Can't open socket"); | |
119 | ||
120 | /* Allow rapid reuse of this port. */ | |
121 | tmp = 1; | |
c5aa993b JM |
122 | setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
123 | sizeof (tmp)); | |
c906108c SS |
124 | |
125 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 126 | sockaddr.sin_port = htons (port); |
c906108c SS |
127 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
128 | ||
c5aa993b | 129 | if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
c906108c SS |
130 | || listen (tmp_desc, 1)) |
131 | perror_with_name ("Can't bind address"); | |
132 | ||
133 | tmp = sizeof (sockaddr); | |
c5aa993b | 134 | remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp); |
c906108c SS |
135 | if (remote_desc == -1) |
136 | perror_with_name ("Accept failed"); | |
137 | ||
138 | protoent = getprotobyname ("tcp"); | |
139 | if (!protoent) | |
140 | perror_with_name ("getprotobyname"); | |
141 | ||
142 | /* Enable TCP keep alive process. */ | |
143 | tmp = 1; | |
c5aa993b | 144 | setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
145 | |
146 | /* Tell TCP not to delay small packets. This greatly speeds up | |
c5aa993b | 147 | interactive response. */ |
c906108c SS |
148 | tmp = 1; |
149 | setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY, | |
c5aa993b | 150 | (char *) &tmp, sizeof (tmp)); |
c906108c SS |
151 | |
152 | close (tmp_desc); /* No longer need this */ | |
153 | ||
c5aa993b JM |
154 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply |
155 | exits when the remote side dies. */ | |
c906108c SS |
156 | } |
157 | ||
158 | #if defined(F_SETFL) && defined (FASYNC) | |
159 | save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0); | |
160 | fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC); | |
cf30a8e1 C |
161 | #if defined (F_SETOWN) |
162 | fcntl (remote_desc, F_SETOWN, getpid ()); | |
94dfea5d | 163 | #endif |
cf30a8e1 | 164 | #endif |
c906108c | 165 | disable_async_io (); |
c906108c SS |
166 | fprintf (stderr, "Remote debugging using %s\n", name); |
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"); | |
186 | } | |
187 | ||
188 | /* Convert number NIB to a hex digit. */ | |
189 | ||
190 | static int | |
fba45db2 | 191 | tohex (int nib) |
c906108c SS |
192 | { |
193 | if (nib < 10) | |
194 | return '0' + nib; | |
195 | else | |
196 | return 'a' + nib - 10; | |
197 | } | |
198 | ||
199 | /* Send a packet to the remote machine, with error checking. | |
200 | The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */ | |
201 | ||
202 | int | |
fba45db2 | 203 | putpkt (char *buf) |
c906108c SS |
204 | { |
205 | int i; | |
206 | unsigned char csum = 0; | |
5c44784c | 207 | char buf2[PBUFSIZ]; |
c906108c SS |
208 | char buf3[1]; |
209 | int cnt = strlen (buf); | |
210 | char *p; | |
211 | ||
212 | /* Copy the packet into buffer BUF2, encapsulating it | |
213 | and giving it a checksum. */ | |
214 | ||
215 | p = buf2; | |
216 | *p++ = '$'; | |
217 | ||
218 | for (i = 0; i < cnt; i++) | |
219 | { | |
220 | csum += buf[i]; | |
221 | *p++ = buf[i]; | |
222 | } | |
223 | *p++ = '#'; | |
224 | *p++ = tohex ((csum >> 4) & 0xf); | |
225 | *p++ = tohex (csum & 0xf); | |
226 | ||
227 | *p = '\0'; | |
228 | ||
229 | /* Send it over and over until we get a positive ack. */ | |
230 | ||
231 | do | |
232 | { | |
233 | int cc; | |
234 | ||
235 | if (write (remote_desc, buf2, p - buf2) != p - buf2) | |
236 | { | |
237 | perror ("putpkt(write)"); | |
238 | return -1; | |
239 | } | |
240 | ||
241 | if (remote_debug) | |
242 | printf ("putpkt (\"%s\"); [looking for ack]\n", buf2); | |
243 | cc = read (remote_desc, buf3, 1); | |
244 | if (remote_debug) | |
245 | printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]); | |
246 | if (cc <= 0) | |
247 | { | |
248 | if (cc == 0) | |
249 | fprintf (stderr, "putpkt(read): Got EOF\n"); | |
250 | else | |
251 | perror ("putpkt(read)"); | |
252 | ||
253 | return -1; | |
254 | } | |
255 | } | |
256 | while (buf3[0] != '+'); | |
257 | ||
258 | return 1; /* Success! */ | |
259 | } | |
260 | ||
261 | /* Come here when we get an input interrupt from the remote side. This | |
262 | interrupt should only be active while we are waiting for the child to do | |
263 | something. About the only thing that should come through is a ^C, which | |
264 | will cause us to send a SIGINT to the child. */ | |
265 | ||
266 | static void | |
fba45db2 | 267 | input_interrupt (void) |
c906108c | 268 | { |
cf30a8e1 C |
269 | fd_set readset; |
270 | struct timeval immediate = { 0, 0 }; | |
c906108c | 271 | |
cf30a8e1 C |
272 | /* Protect against spurious interrupts. This has been observed to |
273 | be a problem under NetBSD 1.4 and 1.5. */ | |
c906108c | 274 | |
cf30a8e1 C |
275 | FD_ZERO (&readset); |
276 | FD_SET (remote_desc, &readset); | |
277 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) | |
c906108c | 278 | { |
cf30a8e1 C |
279 | int cc; |
280 | char c; | |
281 | ||
282 | cc = read (remote_desc, &c, 1); | |
c906108c | 283 | |
cf30a8e1 C |
284 | if (cc != 1 || c != '\003') |
285 | { | |
286 | fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c); | |
287 | return; | |
288 | } | |
289 | ||
290 | kill (inferior_pid, SIGINT); | |
291 | } | |
c906108c SS |
292 | } |
293 | ||
294 | void | |
fba45db2 | 295 | enable_async_io (void) |
c906108c SS |
296 | { |
297 | signal (SIGIO, input_interrupt); | |
298 | } | |
299 | ||
300 | void | |
fba45db2 | 301 | disable_async_io (void) |
c906108c SS |
302 | { |
303 | signal (SIGIO, SIG_IGN); | |
304 | } | |
305 | ||
306 | /* Returns next char from remote GDB. -1 if error. */ | |
307 | ||
308 | static int | |
fba45db2 | 309 | readchar (void) |
c906108c SS |
310 | { |
311 | static char buf[BUFSIZ]; | |
312 | static int bufcnt = 0; | |
313 | static char *bufp; | |
314 | ||
315 | if (bufcnt-- > 0) | |
316 | return *bufp++ & 0x7f; | |
317 | ||
318 | bufcnt = read (remote_desc, buf, sizeof (buf)); | |
319 | ||
320 | if (bufcnt <= 0) | |
321 | { | |
322 | if (bufcnt == 0) | |
323 | fprintf (stderr, "readchar: Got EOF\n"); | |
324 | else | |
325 | perror ("readchar"); | |
326 | ||
327 | return -1; | |
328 | } | |
329 | ||
330 | bufp = buf; | |
331 | bufcnt--; | |
332 | return *bufp++ & 0x7f; | |
333 | } | |
334 | ||
335 | /* Read a packet from the remote machine, with error checking, | |
336 | and store it in BUF. Returns length of packet, or negative if error. */ | |
337 | ||
338 | int | |
fba45db2 | 339 | getpkt (char *buf) |
c906108c SS |
340 | { |
341 | char *bp; | |
342 | unsigned char csum, c1, c2; | |
343 | int c; | |
344 | ||
345 | while (1) | |
346 | { | |
347 | csum = 0; | |
348 | ||
349 | while (1) | |
350 | { | |
351 | c = readchar (); | |
352 | if (c == '$') | |
353 | break; | |
354 | if (remote_debug) | |
355 | printf ("[getpkt: discarding char '%c']\n", c); | |
356 | if (c < 0) | |
357 | return -1; | |
358 | } | |
359 | ||
360 | bp = buf; | |
361 | while (1) | |
362 | { | |
363 | c = readchar (); | |
364 | if (c < 0) | |
365 | return -1; | |
366 | if (c == '#') | |
367 | break; | |
368 | *bp++ = c; | |
369 | csum += c; | |
370 | } | |
371 | *bp = 0; | |
372 | ||
373 | c1 = fromhex (readchar ()); | |
374 | c2 = fromhex (readchar ()); | |
c5aa993b | 375 | |
c906108c SS |
376 | if (csum == (c1 << 4) + c2) |
377 | break; | |
378 | ||
379 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
380 | (c1 << 4) + c2, csum, buf); | |
381 | write (remote_desc, "-", 1); | |
382 | } | |
383 | ||
384 | if (remote_debug) | |
385 | printf ("getpkt (\"%s\"); [sending ack] \n", buf); | |
386 | ||
387 | write (remote_desc, "+", 1); | |
388 | ||
389 | if (remote_debug) | |
390 | printf ("[sent ack]\n"); | |
391 | return bp - buf; | |
392 | } | |
393 | ||
394 | void | |
fba45db2 | 395 | write_ok (char *buf) |
c906108c SS |
396 | { |
397 | buf[0] = 'O'; | |
398 | buf[1] = 'K'; | |
399 | buf[2] = '\0'; | |
400 | } | |
401 | ||
402 | void | |
fba45db2 | 403 | write_enn (char *buf) |
c906108c SS |
404 | { |
405 | buf[0] = 'E'; | |
406 | buf[1] = 'N'; | |
407 | buf[2] = 'N'; | |
408 | buf[3] = '\0'; | |
409 | } | |
410 | ||
411 | void | |
fba45db2 | 412 | convert_int_to_ascii (char *from, char *to, int n) |
c906108c SS |
413 | { |
414 | int nib; | |
415 | char ch; | |
416 | while (n--) | |
417 | { | |
418 | ch = *from++; | |
419 | nib = ((ch & 0xf0) >> 4) & 0x0f; | |
420 | *to++ = tohex (nib); | |
421 | nib = ch & 0x0f; | |
422 | *to++ = tohex (nib); | |
423 | } | |
424 | *to++ = 0; | |
425 | } | |
426 | ||
427 | ||
428 | void | |
fba45db2 | 429 | convert_ascii_to_int (char *from, char *to, int n) |
c906108c SS |
430 | { |
431 | int nib1, nib2; | |
432 | while (n--) | |
433 | { | |
434 | nib1 = fromhex (*from++); | |
435 | nib2 = fromhex (*from++); | |
436 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); | |
437 | } | |
438 | } | |
439 | ||
440 | static char * | |
fba45db2 | 441 | outreg (int regno, char *buf) |
c906108c | 442 | { |
c906108c SS |
443 | int regsize = REGISTER_RAW_SIZE (regno); |
444 | ||
5c44784c JM |
445 | if ((regno >> 12) != 0) |
446 | *buf++ = tohex ((regno >> 12) & 0xf); | |
447 | if ((regno >> 8) != 0) | |
448 | *buf++ = tohex ((regno >> 8) & 0xf); | |
449 | *buf++ = tohex ((regno >> 4) & 0xf); | |
c906108c SS |
450 | *buf++ = tohex (regno & 0xf); |
451 | *buf++ = ':'; | |
452 | convert_int_to_ascii (®isters[REGISTER_BYTE (regno)], buf, regsize); | |
453 | buf += 2 * regsize; | |
454 | *buf++ = ';'; | |
455 | ||
456 | return buf; | |
457 | } | |
458 | ||
459 | void | |
fba45db2 | 460 | prepare_resume_reply (char *buf, char status, unsigned char signo) |
c906108c SS |
461 | { |
462 | int nib; | |
463 | ||
464 | *buf++ = status; | |
465 | ||
466 | /* FIXME! Should be converting this signal number (numbered | |
467 | according to the signal numbering of the system we are running on) | |
468 | to the signal numbers used by the gdb protocol (see enum target_signal | |
469 | in gdb/target.h). */ | |
470 | nib = ((signo & 0xf0) >> 4); | |
471 | *buf++ = tohex (nib); | |
472 | nib = signo & 0x0f; | |
473 | *buf++ = tohex (nib); | |
474 | ||
475 | if (status == 'T') | |
476 | { | |
5c44784c JM |
477 | #ifdef GDBSERVER_RESUME_REGS |
478 | static int gdbserver_resume_regs[] = GDBSERVER_RESUME_REGS ; | |
479 | int i; | |
480 | for (i = 0; | |
481 | i < sizeof (gdbserver_resume_regs) | |
482 | / sizeof (gdbserver_resume_regs[0]); | |
483 | i++) | |
484 | { | |
485 | int regnum = gdbserver_resume_regs[i]; | |
486 | buf = outreg (regnum, buf); | |
487 | } | |
488 | #else /* !defined(GDBSERVER_RESUME_REGS) */ | |
c906108c SS |
489 | buf = outreg (PC_REGNUM, buf); |
490 | buf = outreg (FP_REGNUM, buf); | |
491 | buf = outreg (SP_REGNUM, buf); | |
03863182 AC |
492 | if (NPC_REGNUM >= 0) |
493 | buf = outreg (NPC_REGNUM, buf); | |
c906108c SS |
494 | #ifdef O7_REGNUM |
495 | buf = outreg (O7_REGNUM, buf); | |
496 | #endif | |
5c44784c | 497 | #endif /* GDBSERVER_RESUME_REGS */ |
c906108c SS |
498 | |
499 | /* If the debugger hasn't used any thread features, don't burden it with | |
c5aa993b | 500 | threads. If we didn't check this, GDB 4.13 and older would choke. */ |
c906108c SS |
501 | if (cont_thread != 0) |
502 | { | |
503 | if (old_thread_from_wait != thread_from_wait) | |
504 | { | |
505 | sprintf (buf, "thread:%x;", thread_from_wait); | |
506 | buf += strlen (buf); | |
507 | old_thread_from_wait = thread_from_wait; | |
508 | } | |
509 | } | |
510 | } | |
511 | /* For W and X, we're done. */ | |
512 | *buf++ = 0; | |
513 | } | |
514 | ||
515 | void | |
fba45db2 | 516 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
c906108c SS |
517 | { |
518 | int i = 0, j = 0; | |
519 | char ch; | |
520 | *mem_addr_ptr = *len_ptr = 0; | |
521 | ||
522 | while ((ch = from[i++]) != ',') | |
523 | { | |
524 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
525 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
526 | } | |
527 | ||
528 | for (j = 0; j < 4; j++) | |
529 | { | |
530 | if ((ch = from[i++]) == 0) | |
531 | break; | |
532 | *len_ptr = *len_ptr << 4; | |
533 | *len_ptr |= fromhex (ch) & 0x0f; | |
534 | } | |
535 | } | |
536 | ||
537 | void | |
fba45db2 KB |
538 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
539 | char *to) | |
c906108c SS |
540 | { |
541 | int i = 0; | |
542 | char ch; | |
543 | *mem_addr_ptr = *len_ptr = 0; | |
544 | ||
545 | while ((ch = from[i++]) != ',') | |
546 | { | |
547 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
548 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
549 | } | |
550 | ||
551 | while ((ch = from[i++]) != ':') | |
552 | { | |
553 | *len_ptr = *len_ptr << 4; | |
554 | *len_ptr |= fromhex (ch) & 0x0f; | |
555 | } | |
556 | ||
557 | convert_ascii_to_int (&from[i++], to, *len_ptr); | |
558 | } |