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