]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote utility routines for the remote server for GDB. |
6aba47ca DJ |
2 | Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 | |
6f0f660e EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
21 | |
22 | #include "server.h" | |
23 | #include "terminal.h" | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
b80864fb | 26 | #if HAVE_SYS_IOCTL_H |
c906108c | 27 | #include <sys/ioctl.h> |
b80864fb | 28 | #endif |
c906108c | 29 | #include <sys/file.h> |
b80864fb | 30 | #if HAVE_NETINET_IN_H |
c906108c | 31 | #include <netinet/in.h> |
b80864fb DJ |
32 | #endif |
33 | #if HAVE_SYS_SOCKET_H | |
c906108c | 34 | #include <sys/socket.h> |
b80864fb DJ |
35 | #endif |
36 | #if HAVE_NETDB_H | |
c906108c | 37 | #include <netdb.h> |
b80864fb DJ |
38 | #endif |
39 | #if HAVE_NETINET_TCP_H | |
c906108c | 40 | #include <netinet/tcp.h> |
b80864fb DJ |
41 | #endif |
42 | #if HAVE_SYS_IOCTL_H | |
c906108c | 43 | #include <sys/ioctl.h> |
b80864fb | 44 | #endif |
c906108c SS |
45 | #include <signal.h> |
46 | #include <fcntl.h> | |
cf30a8e1 C |
47 | #include <sys/time.h> |
48 | #include <unistd.h> | |
b80864fb | 49 | #if HAVE_ARPA_INET_H |
0729219d | 50 | #include <arpa/inet.h> |
b80864fb | 51 | #endif |
8264bb58 DJ |
52 | #include <sys/stat.h> |
53 | #include <errno.h> | |
b80864fb DJ |
54 | |
55 | #if USE_WIN32API | |
56 | #include <winsock.h> | |
57 | #endif | |
c906108c | 58 | |
f450004a DJ |
59 | #ifndef HAVE_SOCKLEN_T |
60 | typedef int socklen_t; | |
61 | #endif | |
62 | ||
fd500816 DJ |
63 | /* A cache entry for a successfully looked-up symbol. */ |
64 | struct sym_cache | |
65 | { | |
66 | const char *name; | |
67 | CORE_ADDR addr; | |
68 | struct sym_cache *next; | |
69 | }; | |
70 | ||
71 | /* The symbol cache. */ | |
72 | static struct sym_cache *symbol_cache; | |
73 | ||
ea025f5f DJ |
74 | /* If this flag has been set, assume cache misses are |
75 | failures. */ | |
76 | int all_symbols_looked_up; | |
77 | ||
c906108c | 78 | int remote_debug = 0; |
03863182 | 79 | struct ui_file *gdb_stdlog; |
c906108c SS |
80 | |
81 | static int remote_desc; | |
82 | ||
0d62e5e8 DJ |
83 | /* FIXME headerize? */ |
84 | extern int using_threads; | |
85 | extern int debug_threads; | |
86 | ||
0f48aa01 DJ |
87 | #ifdef USE_WIN32API |
88 | # define read(fd, buf, len) recv (fd, buf, len, 0) | |
89 | # define write(fd, buf, len) send (fd, buf, len, 0) | |
90 | #endif | |
91 | ||
c906108c SS |
92 | /* Open a connection to a remote debugger. |
93 | NAME is the filename used for communication. */ | |
94 | ||
95 | void | |
fba45db2 | 96 | remote_open (char *name) |
c906108c | 97 | { |
b80864fb | 98 | #if defined(F_SETFL) && defined (FASYNC) |
c906108c | 99 | int save_fcntl_flags; |
b80864fb | 100 | #endif |
8264bb58 DJ |
101 | char *port_str; |
102 | ||
103 | port_str = strchr (name, ':'); | |
104 | if (port_str == NULL) | |
c906108c | 105 | { |
b80864fb DJ |
106 | #ifdef USE_WIN32API |
107 | error ("Only <host>:<port> is supported on this platform."); | |
108 | #else | |
8264bb58 DJ |
109 | struct stat statbuf; |
110 | ||
111 | if (stat (name, &statbuf) == 0 | |
112 | && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) | |
113 | remote_desc = open (name, O_RDWR); | |
114 | else | |
115 | { | |
116 | errno = EINVAL; | |
117 | remote_desc = -1; | |
118 | } | |
119 | ||
c906108c SS |
120 | if (remote_desc < 0) |
121 | perror_with_name ("Could not open remote device"); | |
122 | ||
123 | #ifdef HAVE_TERMIOS | |
124 | { | |
125 | struct termios termios; | |
c5aa993b | 126 | tcgetattr (remote_desc, &termios); |
c906108c SS |
127 | |
128 | termios.c_iflag = 0; | |
129 | termios.c_oflag = 0; | |
130 | termios.c_lflag = 0; | |
c5aa993b | 131 | termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 132 | termios.c_cflag |= CLOCAL | CS8; |
d0608e50 | 133 | termios.c_cc[VMIN] = 1; |
c906108c SS |
134 | termios.c_cc[VTIME] = 0; |
135 | ||
c5aa993b | 136 | tcsetattr (remote_desc, TCSANOW, &termios); |
c906108c SS |
137 | } |
138 | #endif | |
139 | ||
140 | #ifdef HAVE_TERMIO | |
141 | { | |
142 | struct termio termio; | |
143 | ioctl (remote_desc, TCGETA, &termio); | |
144 | ||
145 | termio.c_iflag = 0; | |
146 | termio.c_oflag = 0; | |
147 | termio.c_lflag = 0; | |
c5aa993b | 148 | termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 149 | termio.c_cflag |= CLOCAL | CS8; |
d0608e50 | 150 | termio.c_cc[VMIN] = 1; |
c906108c SS |
151 | termio.c_cc[VTIME] = 0; |
152 | ||
153 | ioctl (remote_desc, TCSETA, &termio); | |
154 | } | |
155 | #endif | |
156 | ||
157 | #ifdef HAVE_SGTTY | |
158 | { | |
159 | struct sgttyb sg; | |
160 | ||
161 | ioctl (remote_desc, TIOCGETP, &sg); | |
162 | sg.sg_flags = RAW; | |
163 | ioctl (remote_desc, TIOCSETP, &sg); | |
164 | } | |
165 | #endif | |
166 | ||
e641a1ca | 167 | fprintf (stderr, "Remote debugging using %s\n", name); |
b80864fb | 168 | #endif /* USE_WIN32API */ |
c906108c SS |
169 | } |
170 | else | |
171 | { | |
b80864fb DJ |
172 | #ifdef USE_WIN32API |
173 | static int winsock_initialized; | |
174 | #endif | |
c906108c SS |
175 | char *port_str; |
176 | int port; | |
177 | struct sockaddr_in sockaddr; | |
f450004a | 178 | socklen_t tmp; |
c906108c SS |
179 | int tmp_desc; |
180 | ||
181 | port_str = strchr (name, ':'); | |
182 | ||
183 | port = atoi (port_str + 1); | |
184 | ||
b80864fb DJ |
185 | #ifdef USE_WIN32API |
186 | if (!winsock_initialized) | |
187 | { | |
188 | WSADATA wsad; | |
189 | ||
190 | WSAStartup (MAKEWORD (1, 0), &wsad); | |
191 | winsock_initialized = 1; | |
192 | } | |
193 | #endif | |
194 | ||
195 | tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
c906108c SS |
196 | if (tmp_desc < 0) |
197 | perror_with_name ("Can't open socket"); | |
198 | ||
199 | /* Allow rapid reuse of this port. */ | |
200 | tmp = 1; | |
c5aa993b JM |
201 | setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
202 | sizeof (tmp)); | |
c906108c SS |
203 | |
204 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 205 | sockaddr.sin_port = htons (port); |
c906108c SS |
206 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
207 | ||
c5aa993b | 208 | if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
c906108c SS |
209 | || listen (tmp_desc, 1)) |
210 | perror_with_name ("Can't bind address"); | |
211 | ||
6f8486da DJ |
212 | /* If port is zero, a random port will be selected, and the |
213 | fprintf below needs to know what port was selected. */ | |
214 | if (port == 0) | |
215 | { | |
216 | socklen_t len = sizeof (sockaddr); | |
217 | if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0 | |
218 | || len < sizeof (sockaddr)) | |
219 | perror_with_name ("Can't determine port"); | |
220 | port = ntohs (sockaddr.sin_port); | |
221 | } | |
222 | ||
6910d122 | 223 | fprintf (stderr, "Listening on port %d\n", port); |
b80864fb | 224 | fflush (stderr); |
6910d122 | 225 | |
c906108c | 226 | tmp = sizeof (sockaddr); |
c5aa993b | 227 | remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp); |
c906108c SS |
228 | if (remote_desc == -1) |
229 | perror_with_name ("Accept failed"); | |
230 | ||
c906108c SS |
231 | /* Enable TCP keep alive process. */ |
232 | tmp = 1; | |
c5aa993b | 233 | setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
234 | |
235 | /* Tell TCP not to delay small packets. This greatly speeds up | |
c5aa993b | 236 | interactive response. */ |
c906108c | 237 | tmp = 1; |
373fe97f | 238 | setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY, |
c5aa993b | 239 | (char *) &tmp, sizeof (tmp)); |
c906108c | 240 | |
b80864fb DJ |
241 | |
242 | #ifndef USE_WIN32API | |
c906108c SS |
243 | close (tmp_desc); /* No longer need this */ |
244 | ||
c5aa993b JM |
245 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply |
246 | exits when the remote side dies. */ | |
b80864fb DJ |
247 | #else |
248 | closesocket (tmp_desc); /* No longer need this */ | |
249 | #endif | |
e641a1ca ML |
250 | |
251 | /* Convert IP address to string. */ | |
252 | fprintf (stderr, "Remote debugging from host %s\n", | |
253 | inet_ntoa (sockaddr.sin_addr)); | |
c906108c SS |
254 | } |
255 | ||
256 | #if defined(F_SETFL) && defined (FASYNC) | |
257 | save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0); | |
258 | fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC); | |
cf30a8e1 C |
259 | #if defined (F_SETOWN) |
260 | fcntl (remote_desc, F_SETOWN, getpid ()); | |
94dfea5d | 261 | #endif |
cf30a8e1 | 262 | #endif |
c906108c | 263 | disable_async_io (); |
c906108c SS |
264 | } |
265 | ||
266 | void | |
fba45db2 | 267 | remote_close (void) |
c906108c | 268 | { |
b80864fb DJ |
269 | #ifdef USE_WIN32API |
270 | closesocket (remote_desc); | |
271 | #else | |
c906108c | 272 | close (remote_desc); |
b80864fb | 273 | #endif |
c906108c SS |
274 | } |
275 | ||
276 | /* Convert hex digit A to a number. */ | |
277 | ||
278 | static int | |
fba45db2 | 279 | fromhex (int a) |
c906108c SS |
280 | { |
281 | if (a >= '0' && a <= '9') | |
282 | return a - '0'; | |
283 | else if (a >= 'a' && a <= 'f') | |
284 | return a - 'a' + 10; | |
285 | else | |
286 | error ("Reply contains invalid hex digit"); | |
0a30fbc4 | 287 | return 0; |
c906108c SS |
288 | } |
289 | ||
ce3a066d DJ |
290 | int |
291 | unhexify (char *bin, const char *hex, int count) | |
292 | { | |
293 | int i; | |
294 | ||
295 | for (i = 0; i < count; i++) | |
296 | { | |
297 | if (hex[0] == 0 || hex[1] == 0) | |
298 | { | |
299 | /* Hex string is short, or of uneven length. | |
300 | Return the count that has been converted so far. */ | |
301 | return i; | |
302 | } | |
303 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
304 | hex += 2; | |
305 | } | |
306 | return i; | |
307 | } | |
308 | ||
dae5f5cf | 309 | void |
2f2893d9 DJ |
310 | decode_address (CORE_ADDR *addrp, const char *start, int len) |
311 | { | |
312 | CORE_ADDR addr; | |
313 | char ch; | |
314 | int i; | |
315 | ||
316 | addr = 0; | |
317 | for (i = 0; i < len; i++) | |
318 | { | |
319 | ch = start[i]; | |
320 | addr = addr << 4; | |
321 | addr = addr | (fromhex (ch) & 0x0f); | |
322 | } | |
323 | *addrp = addr; | |
324 | } | |
325 | ||
89be2091 DJ |
326 | const char * |
327 | decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) | |
328 | { | |
329 | const char *end; | |
330 | ||
331 | end = start; | |
332 | while (*end != '\0' && *end != ';') | |
333 | end++; | |
334 | ||
335 | decode_address (addrp, start, end - start); | |
336 | ||
337 | if (*end == ';') | |
338 | end++; | |
339 | return end; | |
340 | } | |
341 | ||
c906108c SS |
342 | /* Convert number NIB to a hex digit. */ |
343 | ||
344 | static int | |
fba45db2 | 345 | tohex (int nib) |
c906108c SS |
346 | { |
347 | if (nib < 10) | |
348 | return '0' + nib; | |
349 | else | |
350 | return 'a' + nib - 10; | |
351 | } | |
352 | ||
ce3a066d DJ |
353 | int |
354 | hexify (char *hex, const char *bin, int count) | |
355 | { | |
356 | int i; | |
357 | ||
358 | /* May use a length, or a nul-terminated string as input. */ | |
359 | if (count == 0) | |
360 | count = strlen (bin); | |
361 | ||
362 | for (i = 0; i < count; i++) | |
363 | { | |
364 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
365 | *hex++ = tohex (*bin++ & 0xf); | |
366 | } | |
367 | *hex = 0; | |
368 | return i; | |
369 | } | |
370 | ||
01f9e8fa DJ |
371 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
372 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
373 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
374 | (which may be more than *OUT_LEN due to escape characters). The | |
375 | total number of bytes in the output buffer will be at most | |
376 | OUT_MAXLEN. */ | |
377 | ||
378 | int | |
379 | remote_escape_output (const gdb_byte *buffer, int len, | |
380 | gdb_byte *out_buf, int *out_len, | |
381 | int out_maxlen) | |
382 | { | |
383 | int input_index, output_index; | |
384 | ||
385 | output_index = 0; | |
386 | for (input_index = 0; input_index < len; input_index++) | |
387 | { | |
388 | gdb_byte b = buffer[input_index]; | |
389 | ||
390 | if (b == '$' || b == '#' || b == '}' || b == '*') | |
391 | { | |
392 | /* These must be escaped. */ | |
393 | if (output_index + 2 > out_maxlen) | |
394 | break; | |
395 | out_buf[output_index++] = '}'; | |
396 | out_buf[output_index++] = b ^ 0x20; | |
397 | } | |
398 | else | |
399 | { | |
400 | if (output_index + 1 > out_maxlen) | |
401 | break; | |
402 | out_buf[output_index++] = b; | |
403 | } | |
404 | } | |
405 | ||
406 | *out_len = input_index; | |
407 | return output_index; | |
408 | } | |
409 | ||
410 | /* Convert BUFFER, escaped data LEN bytes long, into binary data | |
411 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
412 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
413 | ||
414 | This function reverses remote_escape_output. It allows more | |
415 | escaped characters than that function does, in particular because | |
416 | '*' must be escaped to avoid the run-length encoding processing | |
417 | in reading packets. */ | |
418 | ||
419 | static int | |
420 | remote_unescape_input (const gdb_byte *buffer, int len, | |
421 | gdb_byte *out_buf, int out_maxlen) | |
422 | { | |
423 | int input_index, output_index; | |
424 | int escaped; | |
425 | ||
426 | output_index = 0; | |
427 | escaped = 0; | |
428 | for (input_index = 0; input_index < len; input_index++) | |
429 | { | |
430 | gdb_byte b = buffer[input_index]; | |
431 | ||
432 | if (output_index + 1 > out_maxlen) | |
433 | error ("Received too much data from the target."); | |
434 | ||
435 | if (escaped) | |
436 | { | |
437 | out_buf[output_index++] = b ^ 0x20; | |
438 | escaped = 0; | |
439 | } | |
440 | else if (b == '}') | |
441 | escaped = 1; | |
442 | else | |
443 | out_buf[output_index++] = b; | |
444 | } | |
445 | ||
446 | if (escaped) | |
447 | error ("Unmatched escape character in target response."); | |
448 | ||
449 | return output_index; | |
450 | } | |
451 | ||
5ffff7c1 DJ |
452 | /* Look for a sequence of characters which can be run-length encoded. |
453 | If there are any, update *CSUM and *P. Otherwise, output the | |
454 | single character. Return the number of characters consumed. */ | |
455 | ||
456 | static int | |
457 | try_rle (char *buf, int remaining, unsigned char *csum, char **p) | |
458 | { | |
459 | int n; | |
460 | ||
461 | /* Always output the character. */ | |
462 | *csum += buf[0]; | |
463 | *(*p)++ = buf[0]; | |
464 | ||
465 | /* Don't go past '~'. */ | |
466 | if (remaining > 97) | |
467 | remaining = 97; | |
468 | ||
469 | for (n = 1; n < remaining; n++) | |
470 | if (buf[n] != buf[0]) | |
471 | break; | |
472 | ||
473 | /* N is the index of the first character not the same as buf[0]. | |
474 | buf[0] is counted twice, so by decrementing N, we get the number | |
475 | of characters the RLE sequence will replace. */ | |
476 | n--; | |
477 | ||
478 | if (n < 3) | |
479 | return 1; | |
480 | ||
481 | /* Skip the frame characters. The manual says to skip '+' and '-' | |
482 | also, but there's no reason to. Unfortunately these two unusable | |
483 | characters double the encoded length of a four byte zero | |
484 | value. */ | |
485 | while (n + 29 == '$' || n + 29 == '#') | |
486 | n--; | |
487 | ||
488 | *csum += '*'; | |
489 | *(*p)++ = '*'; | |
490 | *csum += n + 29; | |
491 | *(*p)++ = n + 29; | |
492 | ||
493 | return n + 1; | |
494 | } | |
495 | ||
c906108c | 496 | /* Send a packet to the remote machine, with error checking. |
01f9e8fa DJ |
497 | The data of the packet is in BUF, and the length of the |
498 | packet is in CNT. Returns >= 0 on success, -1 otherwise. */ | |
c906108c SS |
499 | |
500 | int | |
01f9e8fa | 501 | putpkt_binary (char *buf, int cnt) |
c906108c SS |
502 | { |
503 | int i; | |
504 | unsigned char csum = 0; | |
0a30fbc4 | 505 | char *buf2; |
c906108c | 506 | char buf3[1]; |
c906108c SS |
507 | char *p; |
508 | ||
0a30fbc4 DJ |
509 | buf2 = malloc (PBUFSIZ); |
510 | ||
c906108c SS |
511 | /* Copy the packet into buffer BUF2, encapsulating it |
512 | and giving it a checksum. */ | |
513 | ||
514 | p = buf2; | |
515 | *p++ = '$'; | |
516 | ||
5ffff7c1 DJ |
517 | for (i = 0; i < cnt;) |
518 | i += try_rle (buf + i, cnt - i, &csum, &p); | |
519 | ||
c906108c SS |
520 | *p++ = '#'; |
521 | *p++ = tohex ((csum >> 4) & 0xf); | |
522 | *p++ = tohex (csum & 0xf); | |
523 | ||
524 | *p = '\0'; | |
525 | ||
526 | /* Send it over and over until we get a positive ack. */ | |
527 | ||
528 | do | |
529 | { | |
530 | int cc; | |
531 | ||
0f48aa01 | 532 | if (write (remote_desc, buf2, p - buf2) != p - buf2) |
c906108c SS |
533 | { |
534 | perror ("putpkt(write)"); | |
535 | return -1; | |
536 | } | |
537 | ||
538 | if (remote_debug) | |
0d62e5e8 DJ |
539 | { |
540 | fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2); | |
541 | fflush (stderr); | |
542 | } | |
0f48aa01 | 543 | cc = read (remote_desc, buf3, 1); |
c906108c | 544 | if (remote_debug) |
0d62e5e8 DJ |
545 | { |
546 | fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]); | |
547 | fflush (stderr); | |
548 | } | |
549 | ||
c906108c SS |
550 | if (cc <= 0) |
551 | { | |
552 | if (cc == 0) | |
553 | fprintf (stderr, "putpkt(read): Got EOF\n"); | |
554 | else | |
555 | perror ("putpkt(read)"); | |
556 | ||
0a30fbc4 | 557 | free (buf2); |
c906108c SS |
558 | return -1; |
559 | } | |
0d62e5e8 DJ |
560 | |
561 | /* Check for an input interrupt while we're here. */ | |
562 | if (buf3[0] == '\003') | |
ef57601b | 563 | (*the_target->request_interrupt) (); |
c906108c SS |
564 | } |
565 | while (buf3[0] != '+'); | |
566 | ||
0a30fbc4 | 567 | free (buf2); |
c906108c SS |
568 | return 1; /* Success! */ |
569 | } | |
570 | ||
01f9e8fa DJ |
571 | /* Send a packet to the remote machine, with error checking. The data |
572 | of the packet is in BUF, and the packet should be a NUL-terminated | |
573 | string. Returns >= 0 on success, -1 otherwise. */ | |
574 | ||
575 | int | |
576 | putpkt (char *buf) | |
577 | { | |
578 | return putpkt_binary (buf, strlen (buf)); | |
579 | } | |
580 | ||
b80864fb | 581 | #ifndef USE_WIN32API |
01f9e8fa | 582 | |
c906108c SS |
583 | /* Come here when we get an input interrupt from the remote side. This |
584 | interrupt should only be active while we are waiting for the child to do | |
585 | something. About the only thing that should come through is a ^C, which | |
ef57601b | 586 | will cause us to request child interruption. */ |
c906108c SS |
587 | |
588 | static void | |
0a30fbc4 | 589 | input_interrupt (int unused) |
c906108c | 590 | { |
cf30a8e1 C |
591 | fd_set readset; |
592 | struct timeval immediate = { 0, 0 }; | |
c906108c | 593 | |
cf30a8e1 C |
594 | /* Protect against spurious interrupts. This has been observed to |
595 | be a problem under NetBSD 1.4 and 1.5. */ | |
c906108c | 596 | |
cf30a8e1 C |
597 | FD_ZERO (&readset); |
598 | FD_SET (remote_desc, &readset); | |
599 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) | |
c906108c | 600 | { |
cf30a8e1 | 601 | int cc; |
fd500816 | 602 | char c = 0; |
cf30a8e1 | 603 | |
0f48aa01 | 604 | cc = read (remote_desc, &c, 1); |
c906108c | 605 | |
cf30a8e1 C |
606 | if (cc != 1 || c != '\003') |
607 | { | |
fd500816 DJ |
608 | fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n", |
609 | cc, c, c); | |
cf30a8e1 C |
610 | return; |
611 | } | |
612 | ||
ef57601b | 613 | (*the_target->request_interrupt) (); |
cf30a8e1 | 614 | } |
c906108c | 615 | } |
b80864fb DJ |
616 | #endif |
617 | ||
618 | /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to | |
619 | accept Control-C from the client, and must be disabled when talking to | |
620 | the client. */ | |
c906108c | 621 | |
62ea82f5 DJ |
622 | void |
623 | block_async_io (void) | |
624 | { | |
b80864fb | 625 | #ifndef USE_WIN32API |
62ea82f5 DJ |
626 | sigset_t sigio_set; |
627 | sigemptyset (&sigio_set); | |
628 | sigaddset (&sigio_set, SIGIO); | |
629 | sigprocmask (SIG_BLOCK, &sigio_set, NULL); | |
b80864fb | 630 | #endif |
62ea82f5 DJ |
631 | } |
632 | ||
633 | void | |
634 | unblock_async_io (void) | |
635 | { | |
b80864fb | 636 | #ifndef USE_WIN32API |
62ea82f5 DJ |
637 | sigset_t sigio_set; |
638 | sigemptyset (&sigio_set); | |
639 | sigaddset (&sigio_set, SIGIO); | |
640 | sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); | |
b80864fb | 641 | #endif |
62ea82f5 DJ |
642 | } |
643 | ||
fd500816 DJ |
644 | /* Current state of asynchronous I/O. */ |
645 | static int async_io_enabled; | |
646 | ||
647 | /* Enable asynchronous I/O. */ | |
c906108c | 648 | void |
fba45db2 | 649 | enable_async_io (void) |
c906108c | 650 | { |
fd500816 DJ |
651 | if (async_io_enabled) |
652 | return; | |
653 | ||
b80864fb | 654 | #ifndef USE_WIN32API |
c906108c | 655 | signal (SIGIO, input_interrupt); |
b80864fb | 656 | #endif |
fd500816 | 657 | async_io_enabled = 1; |
c906108c SS |
658 | } |
659 | ||
fd500816 | 660 | /* Disable asynchronous I/O. */ |
c906108c | 661 | void |
fba45db2 | 662 | disable_async_io (void) |
c906108c | 663 | { |
fd500816 DJ |
664 | if (!async_io_enabled) |
665 | return; | |
666 | ||
b80864fb | 667 | #ifndef USE_WIN32API |
c906108c | 668 | signal (SIGIO, SIG_IGN); |
b80864fb | 669 | #endif |
fd500816 | 670 | async_io_enabled = 0; |
c906108c SS |
671 | } |
672 | ||
673 | /* Returns next char from remote GDB. -1 if error. */ | |
674 | ||
675 | static int | |
fba45db2 | 676 | readchar (void) |
c906108c | 677 | { |
01f9e8fa | 678 | static unsigned char buf[BUFSIZ]; |
c906108c | 679 | static int bufcnt = 0; |
01f9e8fa | 680 | static unsigned char *bufp; |
c906108c SS |
681 | |
682 | if (bufcnt-- > 0) | |
01f9e8fa | 683 | return *bufp++; |
c906108c | 684 | |
0f48aa01 | 685 | bufcnt = read (remote_desc, buf, sizeof (buf)); |
c906108c SS |
686 | |
687 | if (bufcnt <= 0) | |
688 | { | |
689 | if (bufcnt == 0) | |
690 | fprintf (stderr, "readchar: Got EOF\n"); | |
691 | else | |
692 | perror ("readchar"); | |
693 | ||
694 | return -1; | |
695 | } | |
696 | ||
697 | bufp = buf; | |
698 | bufcnt--; | |
699 | return *bufp++ & 0x7f; | |
700 | } | |
701 | ||
702 | /* Read a packet from the remote machine, with error checking, | |
703 | and store it in BUF. Returns length of packet, or negative if error. */ | |
704 | ||
705 | int | |
fba45db2 | 706 | getpkt (char *buf) |
c906108c SS |
707 | { |
708 | char *bp; | |
709 | unsigned char csum, c1, c2; | |
710 | int c; | |
711 | ||
712 | while (1) | |
713 | { | |
714 | csum = 0; | |
715 | ||
716 | while (1) | |
717 | { | |
718 | c = readchar (); | |
719 | if (c == '$') | |
720 | break; | |
721 | if (remote_debug) | |
0d62e5e8 DJ |
722 | { |
723 | fprintf (stderr, "[getpkt: discarding char '%c']\n", c); | |
724 | fflush (stderr); | |
725 | } | |
726 | ||
c906108c SS |
727 | if (c < 0) |
728 | return -1; | |
729 | } | |
730 | ||
731 | bp = buf; | |
732 | while (1) | |
733 | { | |
734 | c = readchar (); | |
735 | if (c < 0) | |
736 | return -1; | |
737 | if (c == '#') | |
738 | break; | |
739 | *bp++ = c; | |
740 | csum += c; | |
741 | } | |
742 | *bp = 0; | |
743 | ||
744 | c1 = fromhex (readchar ()); | |
745 | c2 = fromhex (readchar ()); | |
c5aa993b | 746 | |
c906108c SS |
747 | if (csum == (c1 << 4) + c2) |
748 | break; | |
749 | ||
750 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
751 | (c1 << 4) + c2, csum, buf); | |
0f48aa01 | 752 | write (remote_desc, "-", 1); |
c906108c SS |
753 | } |
754 | ||
755 | if (remote_debug) | |
0d62e5e8 DJ |
756 | { |
757 | fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); | |
758 | fflush (stderr); | |
759 | } | |
c906108c | 760 | |
0f48aa01 | 761 | write (remote_desc, "+", 1); |
c906108c SS |
762 | |
763 | if (remote_debug) | |
0d62e5e8 DJ |
764 | { |
765 | fprintf (stderr, "[sent ack]\n"); | |
766 | fflush (stderr); | |
767 | } | |
768 | ||
c906108c SS |
769 | return bp - buf; |
770 | } | |
771 | ||
772 | void | |
fba45db2 | 773 | write_ok (char *buf) |
c906108c SS |
774 | { |
775 | buf[0] = 'O'; | |
776 | buf[1] = 'K'; | |
777 | buf[2] = '\0'; | |
778 | } | |
779 | ||
780 | void | |
fba45db2 | 781 | write_enn (char *buf) |
c906108c | 782 | { |
c89dc5d4 | 783 | /* Some day, we should define the meanings of the error codes... */ |
c906108c | 784 | buf[0] = 'E'; |
c89dc5d4 DJ |
785 | buf[1] = '0'; |
786 | buf[2] = '1'; | |
c906108c SS |
787 | buf[3] = '\0'; |
788 | } | |
789 | ||
790 | void | |
f450004a | 791 | convert_int_to_ascii (unsigned char *from, char *to, int n) |
c906108c SS |
792 | { |
793 | int nib; | |
f450004a | 794 | int ch; |
c906108c SS |
795 | while (n--) |
796 | { | |
797 | ch = *from++; | |
798 | nib = ((ch & 0xf0) >> 4) & 0x0f; | |
799 | *to++ = tohex (nib); | |
800 | nib = ch & 0x0f; | |
801 | *to++ = tohex (nib); | |
802 | } | |
803 | *to++ = 0; | |
804 | } | |
805 | ||
806 | ||
807 | void | |
f450004a | 808 | convert_ascii_to_int (char *from, unsigned char *to, int n) |
c906108c SS |
809 | { |
810 | int nib1, nib2; | |
811 | while (n--) | |
812 | { | |
813 | nib1 = fromhex (*from++); | |
814 | nib2 = fromhex (*from++); | |
815 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); | |
816 | } | |
817 | } | |
818 | ||
819 | static char * | |
fba45db2 | 820 | outreg (int regno, char *buf) |
c906108c | 821 | { |
5c44784c JM |
822 | if ((regno >> 12) != 0) |
823 | *buf++ = tohex ((regno >> 12) & 0xf); | |
824 | if ((regno >> 8) != 0) | |
825 | *buf++ = tohex ((regno >> 8) & 0xf); | |
826 | *buf++ = tohex ((regno >> 4) & 0xf); | |
c906108c SS |
827 | *buf++ = tohex (regno & 0xf); |
828 | *buf++ = ':'; | |
0d62e5e8 DJ |
829 | collect_register_as_string (regno, buf); |
830 | buf += 2 * register_size (regno); | |
c906108c SS |
831 | *buf++ = ';'; |
832 | ||
833 | return buf; | |
834 | } | |
835 | ||
0d62e5e8 DJ |
836 | void |
837 | new_thread_notify (int id) | |
838 | { | |
839 | char own_buf[256]; | |
840 | ||
841 | /* The `n' response is not yet part of the remote protocol. Do nothing. */ | |
842 | if (1) | |
843 | return; | |
844 | ||
845 | if (server_waiting == 0) | |
846 | return; | |
847 | ||
848 | sprintf (own_buf, "n%x", id); | |
849 | disable_async_io (); | |
850 | putpkt (own_buf); | |
851 | enable_async_io (); | |
852 | } | |
853 | ||
854 | void | |
855 | dead_thread_notify (int id) | |
856 | { | |
857 | char own_buf[256]; | |
858 | ||
859 | /* The `x' response is not yet part of the remote protocol. Do nothing. */ | |
860 | if (1) | |
861 | return; | |
862 | ||
863 | sprintf (own_buf, "x%x", id); | |
864 | disable_async_io (); | |
865 | putpkt (own_buf); | |
866 | enable_async_io (); | |
867 | } | |
868 | ||
c906108c | 869 | void |
b80864fb | 870 | prepare_resume_reply (char *buf, char status, unsigned char sig) |
c906108c | 871 | { |
b80864fb | 872 | int nib; |
c906108c SS |
873 | |
874 | *buf++ = status; | |
875 | ||
0e98d0a7 | 876 | nib = ((sig & 0xf0) >> 4); |
c906108c | 877 | *buf++ = tohex (nib); |
0e98d0a7 | 878 | nib = sig & 0x0f; |
c906108c SS |
879 | *buf++ = tohex (nib); |
880 | ||
881 | if (status == 'T') | |
882 | { | |
0a30fbc4 | 883 | const char **regp = gdbserver_expedite_regs; |
e013ee27 OF |
884 | |
885 | if (the_target->stopped_by_watchpoint != NULL | |
886 | && (*the_target->stopped_by_watchpoint) ()) | |
887 | { | |
888 | CORE_ADDR addr; | |
889 | int i; | |
890 | ||
891 | strncpy (buf, "watch:", 6); | |
892 | buf += 6; | |
893 | ||
894 | addr = (*the_target->stopped_data_address) (); | |
895 | ||
896 | /* Convert each byte of the address into two hexadecimal chars. | |
897 | Note that we take sizeof (void *) instead of sizeof (addr); | |
898 | this is to avoid sending a 64-bit address to a 32-bit GDB. */ | |
899 | for (i = sizeof (void *) * 2; i > 0; i--) | |
900 | { | |
901 | *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); | |
902 | } | |
903 | *buf++ = ';'; | |
904 | } | |
905 | ||
0a30fbc4 | 906 | while (*regp) |
5c44784c | 907 | { |
0a30fbc4 DJ |
908 | buf = outreg (find_regno (*regp), buf); |
909 | regp ++; | |
5c44784c | 910 | } |
c906108c | 911 | |
0d62e5e8 DJ |
912 | /* Formerly, if the debugger had not used any thread features we would not |
913 | burden it with a thread status response. This was for the benefit of | |
914 | GDB 4.13 and older. However, in recent GDB versions the check | |
915 | (``if (cont_thread != 0)'') does not have the desired effect because of | |
916 | sillyness in the way that the remote protocol handles specifying a thread. | |
917 | Since thread support relies on qSymbol support anyway, assume GDB can handle | |
918 | threads. */ | |
919 | ||
920 | if (using_threads) | |
c906108c | 921 | { |
b92a518e DJ |
922 | unsigned int gdb_id_from_wait; |
923 | ||
0d62e5e8 DJ |
924 | /* FIXME right place to set this? */ |
925 | thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id; | |
b92a518e | 926 | gdb_id_from_wait = thread_to_gdb_id (current_inferior); |
a06660f7 | 927 | |
0d62e5e8 | 928 | if (debug_threads) |
a1928bad | 929 | fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait); |
89a208da DJ |
930 | /* This if (1) ought to be unnecessary. But remote_wait in GDB |
931 | will claim this event belongs to inferior_ptid if we do not | |
932 | specify a thread, and there's no way for gdbserver to know | |
933 | what inferior_ptid is. */ | |
934 | if (1 || old_thread_from_wait != thread_from_wait) | |
c906108c | 935 | { |
0d62e5e8 | 936 | general_thread = thread_from_wait; |
a06660f7 | 937 | sprintf (buf, "thread:%x;", gdb_id_from_wait); |
c906108c SS |
938 | buf += strlen (buf); |
939 | old_thread_from_wait = thread_from_wait; | |
940 | } | |
941 | } | |
942 | } | |
943 | /* For W and X, we're done. */ | |
944 | *buf++ = 0; | |
945 | } | |
946 | ||
947 | void | |
fba45db2 | 948 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
c906108c SS |
949 | { |
950 | int i = 0, j = 0; | |
951 | char ch; | |
952 | *mem_addr_ptr = *len_ptr = 0; | |
953 | ||
954 | while ((ch = from[i++]) != ',') | |
955 | { | |
956 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
957 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
958 | } | |
959 | ||
960 | for (j = 0; j < 4; j++) | |
961 | { | |
962 | if ((ch = from[i++]) == 0) | |
963 | break; | |
964 | *len_ptr = *len_ptr << 4; | |
965 | *len_ptr |= fromhex (ch) & 0x0f; | |
966 | } | |
967 | } | |
968 | ||
969 | void | |
fba45db2 | 970 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
f450004a | 971 | unsigned char *to) |
c906108c SS |
972 | { |
973 | int i = 0; | |
974 | char ch; | |
975 | *mem_addr_ptr = *len_ptr = 0; | |
976 | ||
977 | while ((ch = from[i++]) != ',') | |
978 | { | |
979 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
980 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
981 | } | |
982 | ||
983 | while ((ch = from[i++]) != ':') | |
984 | { | |
985 | *len_ptr = *len_ptr << 4; | |
986 | *len_ptr |= fromhex (ch) & 0x0f; | |
987 | } | |
988 | ||
989 | convert_ascii_to_int (&from[i++], to, *len_ptr); | |
990 | } | |
2f2893d9 | 991 | |
01f9e8fa DJ |
992 | int |
993 | decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, | |
994 | unsigned int *len_ptr, unsigned char *to) | |
995 | { | |
996 | int i = 0; | |
997 | char ch; | |
998 | *mem_addr_ptr = *len_ptr = 0; | |
999 | ||
1000 | while ((ch = from[i++]) != ',') | |
1001 | { | |
1002 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1003 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1004 | } | |
1005 | ||
1006 | while ((ch = from[i++]) != ':') | |
1007 | { | |
1008 | *len_ptr = *len_ptr << 4; | |
1009 | *len_ptr |= fromhex (ch) & 0x0f; | |
1010 | } | |
1011 | ||
1012 | if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, | |
1013 | to, *len_ptr) != *len_ptr) | |
1014 | return -1; | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
fd500816 DJ |
1019 | /* Ask GDB for the address of NAME, and return it in ADDRP if found. |
1020 | Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ | |
1021 | ||
2f2893d9 DJ |
1022 | int |
1023 | look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
1024 | { | |
1025 | char own_buf[266], *p, *q; | |
1026 | int len; | |
fd500816 DJ |
1027 | struct sym_cache *sym; |
1028 | ||
1029 | /* Check the cache first. */ | |
1030 | for (sym = symbol_cache; sym; sym = sym->next) | |
1031 | if (strcmp (name, sym->name) == 0) | |
1032 | { | |
1033 | *addrp = sym->addr; | |
1034 | return 1; | |
1035 | } | |
2f2893d9 | 1036 | |
ea025f5f DJ |
1037 | /* If we've passed the call to thread_db_look_up_symbols, then |
1038 | anything not in the cache must not exist; we're not interested | |
1039 | in any libraries loaded after that point, only in symbols in | |
1040 | libpthread.so. It might not be an appropriate time to look | |
1041 | up a symbol, e.g. while we're trying to fetch registers. */ | |
1042 | if (all_symbols_looked_up) | |
1043 | return 0; | |
1044 | ||
2f2893d9 DJ |
1045 | /* Send the request. */ |
1046 | strcpy (own_buf, "qSymbol:"); | |
1047 | hexify (own_buf + strlen ("qSymbol:"), name, strlen (name)); | |
1048 | if (putpkt (own_buf) < 0) | |
1049 | return -1; | |
1050 | ||
1051 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ | |
1052 | len = getpkt (own_buf); | |
1053 | if (len < 0) | |
1054 | return -1; | |
1055 | ||
1056 | if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0) | |
1057 | { | |
1058 | /* Malformed response. */ | |
1059 | if (remote_debug) | |
0d62e5e8 DJ |
1060 | { |
1061 | fprintf (stderr, "Malformed response to qSymbol, ignoring.\n"); | |
1062 | fflush (stderr); | |
1063 | } | |
1064 | ||
2f2893d9 DJ |
1065 | return -1; |
1066 | } | |
1067 | ||
1068 | p = own_buf + strlen ("qSymbol:"); | |
1069 | q = p; | |
1070 | while (*q && *q != ':') | |
1071 | q++; | |
1072 | ||
1073 | /* Make sure we found a value for the symbol. */ | |
1074 | if (p == q || *q == '\0') | |
1075 | return 0; | |
1076 | ||
1077 | decode_address (addrp, p, q - p); | |
fd500816 DJ |
1078 | |
1079 | /* Save the symbol in our cache. */ | |
1080 | sym = malloc (sizeof (*sym)); | |
1081 | sym->name = strdup (name); | |
1082 | sym->addr = *addrp; | |
1083 | sym->next = symbol_cache; | |
1084 | symbol_cache = sym; | |
1085 | ||
2f2893d9 DJ |
1086 | return 1; |
1087 | } | |
c74d0ad8 DJ |
1088 | |
1089 | void | |
bce7165d | 1090 | monitor_output (const char *msg) |
c74d0ad8 DJ |
1091 | { |
1092 | char *buf = malloc (strlen (msg) * 2 + 2); | |
1093 | ||
1094 | buf[0] = 'O'; | |
1095 | hexify (buf + 1, msg, 0); | |
1096 | ||
1097 | putpkt (buf); | |
1098 | free (buf); | |
1099 | } |