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