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