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