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