Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote utility routines for the remote server for GDB. |
6aba47ca | 2 | Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
4c38e0a4 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
9b254dd1 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "server.h" | |
22 | #include "terminal.h" | |
5b1c542e | 23 | #include "target.h" |
c906108c SS |
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 | |
ac8c974e AR |
69 | #if __QNX__ |
70 | #include <sys/iomgr.h> | |
71 | #endif /* __QNX__ */ | |
72 | ||
f450004a DJ |
73 | #ifndef HAVE_SOCKLEN_T |
74 | typedef int socklen_t; | |
75 | #endif | |
76 | ||
7390519e PA |
77 | #if USE_WIN32API |
78 | # define INVALID_DESCRIPTOR INVALID_SOCKET | |
79 | #else | |
80 | # define INVALID_DESCRIPTOR -1 | |
81 | #endif | |
82 | ||
fd500816 DJ |
83 | /* A cache entry for a successfully looked-up symbol. */ |
84 | struct sym_cache | |
85 | { | |
95954743 | 86 | char *name; |
fd500816 DJ |
87 | CORE_ADDR addr; |
88 | struct sym_cache *next; | |
89 | }; | |
90 | ||
c906108c | 91 | int remote_debug = 0; |
03863182 | 92 | struct ui_file *gdb_stdlog; |
c906108c | 93 | |
7390519e | 94 | static int remote_desc = INVALID_DESCRIPTOR; |
c906108c | 95 | |
0d62e5e8 DJ |
96 | /* FIXME headerize? */ |
97 | extern int using_threads; | |
98 | extern int debug_threads; | |
99 | ||
a6f3e723 SL |
100 | /* If true, then GDB has requested noack mode. */ |
101 | int noack_mode = 0; | |
102 | /* If true, then we tell GDB to use noack mode by default. */ | |
103 | int transport_is_reliable = 0; | |
104 | ||
0f48aa01 | 105 | #ifdef USE_WIN32API |
68070c10 PA |
106 | # define read(fd, buf, len) recv (fd, (char *) buf, len, 0) |
107 | # define write(fd, buf, len) send (fd, (char *) buf, len, 0) | |
0f48aa01 DJ |
108 | #endif |
109 | ||
c906108c SS |
110 | /* Open a connection to a remote debugger. |
111 | NAME is the filename used for communication. */ | |
112 | ||
113 | void | |
fba45db2 | 114 | remote_open (char *name) |
c906108c | 115 | { |
b80864fb | 116 | #if defined(F_SETFL) && defined (FASYNC) |
c906108c | 117 | int save_fcntl_flags; |
b80864fb | 118 | #endif |
8264bb58 DJ |
119 | char *port_str; |
120 | ||
121 | port_str = strchr (name, ':'); | |
122 | if (port_str == NULL) | |
c906108c | 123 | { |
b80864fb DJ |
124 | #ifdef USE_WIN32API |
125 | error ("Only <host>:<port> is supported on this platform."); | |
126 | #else | |
8264bb58 DJ |
127 | struct stat statbuf; |
128 | ||
129 | if (stat (name, &statbuf) == 0 | |
130 | && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) | |
131 | remote_desc = open (name, O_RDWR); | |
132 | else | |
133 | { | |
134 | errno = EINVAL; | |
135 | remote_desc = -1; | |
136 | } | |
137 | ||
c906108c SS |
138 | if (remote_desc < 0) |
139 | perror_with_name ("Could not open remote device"); | |
140 | ||
141 | #ifdef HAVE_TERMIOS | |
142 | { | |
143 | struct termios termios; | |
c5aa993b | 144 | tcgetattr (remote_desc, &termios); |
c906108c SS |
145 | |
146 | termios.c_iflag = 0; | |
147 | termios.c_oflag = 0; | |
148 | termios.c_lflag = 0; | |
c5aa993b | 149 | termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 150 | termios.c_cflag |= CLOCAL | CS8; |
d0608e50 | 151 | termios.c_cc[VMIN] = 1; |
c906108c SS |
152 | termios.c_cc[VTIME] = 0; |
153 | ||
c5aa993b | 154 | tcsetattr (remote_desc, TCSANOW, &termios); |
c906108c SS |
155 | } |
156 | #endif | |
157 | ||
158 | #ifdef HAVE_TERMIO | |
159 | { | |
160 | struct termio termio; | |
161 | ioctl (remote_desc, TCGETA, &termio); | |
162 | ||
163 | termio.c_iflag = 0; | |
164 | termio.c_oflag = 0; | |
165 | termio.c_lflag = 0; | |
c5aa993b | 166 | termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 167 | termio.c_cflag |= CLOCAL | CS8; |
d0608e50 | 168 | termio.c_cc[VMIN] = 1; |
c906108c SS |
169 | termio.c_cc[VTIME] = 0; |
170 | ||
171 | ioctl (remote_desc, TCSETA, &termio); | |
172 | } | |
173 | #endif | |
174 | ||
175 | #ifdef HAVE_SGTTY | |
176 | { | |
177 | struct sgttyb sg; | |
178 | ||
179 | ioctl (remote_desc, TIOCGETP, &sg); | |
180 | sg.sg_flags = RAW; | |
181 | ioctl (remote_desc, TIOCSETP, &sg); | |
182 | } | |
183 | #endif | |
184 | ||
e641a1ca | 185 | fprintf (stderr, "Remote debugging using %s\n", name); |
b80864fb | 186 | #endif /* USE_WIN32API */ |
a6f3e723 SL |
187 | |
188 | transport_is_reliable = 0; | |
c906108c SS |
189 | } |
190 | else | |
191 | { | |
b80864fb DJ |
192 | #ifdef USE_WIN32API |
193 | static int winsock_initialized; | |
194 | #endif | |
c906108c SS |
195 | int port; |
196 | struct sockaddr_in sockaddr; | |
f450004a | 197 | socklen_t tmp; |
c906108c | 198 | int tmp_desc; |
2d717e4f | 199 | char *port_end; |
c906108c | 200 | |
2d717e4f DJ |
201 | port = strtoul (port_str + 1, &port_end, 10); |
202 | if (port_str[1] == '\0' || *port_end != '\0') | |
203 | fatal ("Bad port argument: %s", name); | |
c906108c | 204 | |
b80864fb DJ |
205 | #ifdef USE_WIN32API |
206 | if (!winsock_initialized) | |
207 | { | |
208 | WSADATA wsad; | |
209 | ||
210 | WSAStartup (MAKEWORD (1, 0), &wsad); | |
211 | winsock_initialized = 1; | |
212 | } | |
213 | #endif | |
214 | ||
215 | tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
c906108c SS |
216 | if (tmp_desc < 0) |
217 | perror_with_name ("Can't open socket"); | |
218 | ||
219 | /* Allow rapid reuse of this port. */ | |
220 | tmp = 1; | |
c5aa993b JM |
221 | setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
222 | sizeof (tmp)); | |
c906108c SS |
223 | |
224 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 225 | sockaddr.sin_port = htons (port); |
c906108c SS |
226 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
227 | ||
c5aa993b | 228 | if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
c906108c SS |
229 | || listen (tmp_desc, 1)) |
230 | perror_with_name ("Can't bind address"); | |
231 | ||
6f8486da DJ |
232 | /* If port is zero, a random port will be selected, and the |
233 | fprintf below needs to know what port was selected. */ | |
234 | if (port == 0) | |
235 | { | |
236 | socklen_t len = sizeof (sockaddr); | |
237 | if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0 | |
238 | || len < sizeof (sockaddr)) | |
239 | perror_with_name ("Can't determine port"); | |
240 | port = ntohs (sockaddr.sin_port); | |
241 | } | |
242 | ||
6910d122 | 243 | fprintf (stderr, "Listening on port %d\n", port); |
b80864fb | 244 | fflush (stderr); |
6910d122 | 245 | |
c906108c | 246 | tmp = sizeof (sockaddr); |
c5aa993b | 247 | remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp); |
c906108c SS |
248 | if (remote_desc == -1) |
249 | perror_with_name ("Accept failed"); | |
250 | ||
c906108c SS |
251 | /* Enable TCP keep alive process. */ |
252 | tmp = 1; | |
aa0403d9 PA |
253 | setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, |
254 | (char *) &tmp, sizeof (tmp)); | |
c906108c SS |
255 | |
256 | /* Tell TCP not to delay small packets. This greatly speeds up | |
1b3f6016 | 257 | interactive response. */ |
c906108c | 258 | tmp = 1; |
373fe97f | 259 | setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY, |
c5aa993b | 260 | (char *) &tmp, sizeof (tmp)); |
c906108c | 261 | |
b80864fb DJ |
262 | |
263 | #ifndef USE_WIN32API | |
c906108c SS |
264 | close (tmp_desc); /* No longer need this */ |
265 | ||
c5aa993b JM |
266 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply |
267 | exits when the remote side dies. */ | |
b80864fb DJ |
268 | #else |
269 | closesocket (tmp_desc); /* No longer need this */ | |
270 | #endif | |
e641a1ca ML |
271 | |
272 | /* Convert IP address to string. */ | |
1b3f6016 PA |
273 | fprintf (stderr, "Remote debugging from host %s\n", |
274 | inet_ntoa (sockaddr.sin_addr)); | |
a6f3e723 SL |
275 | |
276 | transport_is_reliable = 1; | |
c906108c SS |
277 | } |
278 | ||
279 | #if defined(F_SETFL) && defined (FASYNC) | |
280 | save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0); | |
281 | fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC); | |
cf30a8e1 C |
282 | #if defined (F_SETOWN) |
283 | fcntl (remote_desc, F_SETOWN, getpid ()); | |
94dfea5d | 284 | #endif |
cf30a8e1 | 285 | #endif |
bd99dc85 PA |
286 | |
287 | /* Register the event loop handler. */ | |
288 | add_file_handler (remote_desc, handle_serial_event, NULL); | |
c906108c SS |
289 | } |
290 | ||
291 | void | |
fba45db2 | 292 | remote_close (void) |
c906108c | 293 | { |
bd99dc85 PA |
294 | delete_file_handler (remote_desc); |
295 | ||
b80864fb DJ |
296 | #ifdef USE_WIN32API |
297 | closesocket (remote_desc); | |
298 | #else | |
c906108c | 299 | close (remote_desc); |
b80864fb | 300 | #endif |
c906108c SS |
301 | } |
302 | ||
303 | /* Convert hex digit A to a number. */ | |
304 | ||
305 | static int | |
fba45db2 | 306 | fromhex (int a) |
c906108c SS |
307 | { |
308 | if (a >= '0' && a <= '9') | |
309 | return a - '0'; | |
310 | else if (a >= 'a' && a <= 'f') | |
311 | return a - 'a' + 10; | |
312 | else | |
313 | error ("Reply contains invalid hex digit"); | |
0a30fbc4 | 314 | return 0; |
c906108c SS |
315 | } |
316 | ||
95954743 PA |
317 | static const char hexchars[] = "0123456789abcdef"; |
318 | ||
319 | static int | |
320 | ishex (int ch, int *val) | |
321 | { | |
322 | if ((ch >= 'a') && (ch <= 'f')) | |
323 | { | |
324 | *val = ch - 'a' + 10; | |
325 | return 1; | |
326 | } | |
327 | if ((ch >= 'A') && (ch <= 'F')) | |
328 | { | |
329 | *val = ch - 'A' + 10; | |
330 | return 1; | |
331 | } | |
332 | if ((ch >= '0') && (ch <= '9')) | |
333 | { | |
334 | *val = ch - '0'; | |
335 | return 1; | |
336 | } | |
337 | return 0; | |
338 | } | |
339 | ||
ce3a066d DJ |
340 | int |
341 | unhexify (char *bin, const char *hex, int count) | |
342 | { | |
343 | int i; | |
344 | ||
345 | for (i = 0; i < count; i++) | |
346 | { | |
347 | if (hex[0] == 0 || hex[1] == 0) | |
1b3f6016 PA |
348 | { |
349 | /* Hex string is short, or of uneven length. | |
350 | Return the count that has been converted so far. */ | |
351 | return i; | |
352 | } | |
ce3a066d DJ |
353 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); |
354 | hex += 2; | |
355 | } | |
356 | return i; | |
357 | } | |
358 | ||
dae5f5cf | 359 | void |
2f2893d9 DJ |
360 | decode_address (CORE_ADDR *addrp, const char *start, int len) |
361 | { | |
362 | CORE_ADDR addr; | |
363 | char ch; | |
364 | int i; | |
365 | ||
366 | addr = 0; | |
367 | for (i = 0; i < len; i++) | |
368 | { | |
369 | ch = start[i]; | |
370 | addr = addr << 4; | |
371 | addr = addr | (fromhex (ch) & 0x0f); | |
372 | } | |
373 | *addrp = addr; | |
374 | } | |
375 | ||
89be2091 DJ |
376 | const char * |
377 | decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) | |
378 | { | |
379 | const char *end; | |
380 | ||
381 | end = start; | |
382 | while (*end != '\0' && *end != ';') | |
383 | end++; | |
384 | ||
385 | decode_address (addrp, start, end - start); | |
386 | ||
387 | if (*end == ';') | |
388 | end++; | |
389 | return end; | |
390 | } | |
391 | ||
c906108c SS |
392 | /* Convert number NIB to a hex digit. */ |
393 | ||
394 | static int | |
fba45db2 | 395 | tohex (int nib) |
c906108c SS |
396 | { |
397 | if (nib < 10) | |
398 | return '0' + nib; | |
399 | else | |
400 | return 'a' + nib - 10; | |
401 | } | |
402 | ||
ce3a066d DJ |
403 | int |
404 | hexify (char *hex, const char *bin, int count) | |
405 | { | |
406 | int i; | |
407 | ||
408 | /* May use a length, or a nul-terminated string as input. */ | |
409 | if (count == 0) | |
410 | count = strlen (bin); | |
411 | ||
412 | for (i = 0; i < count; i++) | |
413 | { | |
414 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
415 | *hex++ = tohex (*bin++ & 0xf); | |
416 | } | |
417 | *hex = 0; | |
418 | return i; | |
419 | } | |
420 | ||
01f9e8fa DJ |
421 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
422 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
423 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
424 | (which may be more than *OUT_LEN due to escape characters). The | |
425 | total number of bytes in the output buffer will be at most | |
426 | OUT_MAXLEN. */ | |
427 | ||
428 | int | |
429 | remote_escape_output (const gdb_byte *buffer, int len, | |
430 | gdb_byte *out_buf, int *out_len, | |
431 | int out_maxlen) | |
432 | { | |
433 | int input_index, output_index; | |
434 | ||
435 | output_index = 0; | |
436 | for (input_index = 0; input_index < len; input_index++) | |
437 | { | |
438 | gdb_byte b = buffer[input_index]; | |
439 | ||
440 | if (b == '$' || b == '#' || b == '}' || b == '*') | |
441 | { | |
442 | /* These must be escaped. */ | |
443 | if (output_index + 2 > out_maxlen) | |
444 | break; | |
445 | out_buf[output_index++] = '}'; | |
446 | out_buf[output_index++] = b ^ 0x20; | |
447 | } | |
448 | else | |
449 | { | |
450 | if (output_index + 1 > out_maxlen) | |
451 | break; | |
452 | out_buf[output_index++] = b; | |
453 | } | |
454 | } | |
455 | ||
456 | *out_len = input_index; | |
457 | return output_index; | |
458 | } | |
459 | ||
460 | /* Convert BUFFER, escaped data LEN bytes long, into binary data | |
461 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
462 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
463 | ||
464 | This function reverses remote_escape_output. It allows more | |
465 | escaped characters than that function does, in particular because | |
466 | '*' must be escaped to avoid the run-length encoding processing | |
467 | in reading packets. */ | |
468 | ||
469 | static int | |
470 | remote_unescape_input (const gdb_byte *buffer, int len, | |
471 | gdb_byte *out_buf, int out_maxlen) | |
472 | { | |
473 | int input_index, output_index; | |
474 | int escaped; | |
475 | ||
476 | output_index = 0; | |
477 | escaped = 0; | |
478 | for (input_index = 0; input_index < len; input_index++) | |
479 | { | |
480 | gdb_byte b = buffer[input_index]; | |
481 | ||
482 | if (output_index + 1 > out_maxlen) | |
483 | error ("Received too much data from the target."); | |
484 | ||
485 | if (escaped) | |
486 | { | |
487 | out_buf[output_index++] = b ^ 0x20; | |
488 | escaped = 0; | |
489 | } | |
490 | else if (b == '}') | |
491 | escaped = 1; | |
492 | else | |
493 | out_buf[output_index++] = b; | |
494 | } | |
495 | ||
496 | if (escaped) | |
497 | error ("Unmatched escape character in target response."); | |
498 | ||
499 | return output_index; | |
500 | } | |
501 | ||
5ffff7c1 DJ |
502 | /* Look for a sequence of characters which can be run-length encoded. |
503 | If there are any, update *CSUM and *P. Otherwise, output the | |
504 | single character. Return the number of characters consumed. */ | |
505 | ||
506 | static int | |
507 | try_rle (char *buf, int remaining, unsigned char *csum, char **p) | |
508 | { | |
509 | int n; | |
510 | ||
511 | /* Always output the character. */ | |
512 | *csum += buf[0]; | |
513 | *(*p)++ = buf[0]; | |
514 | ||
515 | /* Don't go past '~'. */ | |
516 | if (remaining > 97) | |
517 | remaining = 97; | |
518 | ||
519 | for (n = 1; n < remaining; n++) | |
520 | if (buf[n] != buf[0]) | |
521 | break; | |
522 | ||
523 | /* N is the index of the first character not the same as buf[0]. | |
524 | buf[0] is counted twice, so by decrementing N, we get the number | |
525 | of characters the RLE sequence will replace. */ | |
526 | n--; | |
527 | ||
528 | if (n < 3) | |
529 | return 1; | |
530 | ||
531 | /* Skip the frame characters. The manual says to skip '+' and '-' | |
532 | also, but there's no reason to. Unfortunately these two unusable | |
533 | characters double the encoded length of a four byte zero | |
534 | value. */ | |
535 | while (n + 29 == '$' || n + 29 == '#') | |
536 | n--; | |
537 | ||
538 | *csum += '*'; | |
539 | *(*p)++ = '*'; | |
540 | *csum += n + 29; | |
541 | *(*p)++ = n + 29; | |
542 | ||
543 | return n + 1; | |
544 | } | |
545 | ||
95954743 PA |
546 | char * |
547 | unpack_varlen_hex (char *buff, /* packet to parse */ | |
548 | ULONGEST *result) | |
549 | { | |
550 | int nibble; | |
551 | ULONGEST retval = 0; | |
552 | ||
553 | while (ishex (*buff, &nibble)) | |
554 | { | |
555 | buff++; | |
556 | retval = retval << 4; | |
557 | retval |= nibble & 0x0f; | |
558 | } | |
559 | *result = retval; | |
560 | return buff; | |
561 | } | |
562 | ||
563 | /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */ | |
564 | ||
565 | char * | |
566 | write_ptid (char *buf, ptid_t ptid) | |
567 | { | |
568 | int pid, tid; | |
569 | ||
570 | if (multi_process) | |
571 | { | |
572 | pid = ptid_get_pid (ptid); | |
573 | if (pid < 0) | |
574 | buf += sprintf (buf, "p-%x.", -pid); | |
575 | else | |
576 | buf += sprintf (buf, "p%x.", pid); | |
577 | } | |
578 | tid = ptid_get_lwp (ptid); | |
579 | if (tid < 0) | |
580 | buf += sprintf (buf, "-%x", -tid); | |
581 | else | |
582 | buf += sprintf (buf, "%x", tid); | |
583 | ||
584 | return buf; | |
585 | } | |
586 | ||
587 | ULONGEST | |
588 | hex_or_minus_one (char *buf, char **obuf) | |
589 | { | |
590 | ULONGEST ret; | |
591 | ||
592 | if (strncmp (buf, "-1", 2) == 0) | |
593 | { | |
594 | ret = (ULONGEST) -1; | |
595 | buf += 2; | |
596 | } | |
597 | else | |
598 | buf = unpack_varlen_hex (buf, &ret); | |
599 | ||
600 | if (obuf) | |
601 | *obuf = buf; | |
602 | ||
603 | return ret; | |
604 | } | |
605 | ||
606 | /* Extract a PTID from BUF. If non-null, OBUF is set to the to one | |
607 | passed the last parsed char. Returns null_ptid on error. */ | |
608 | ptid_t | |
609 | read_ptid (char *buf, char **obuf) | |
610 | { | |
611 | char *p = buf; | |
612 | char *pp; | |
613 | ULONGEST pid = 0, tid = 0; | |
614 | ||
615 | if (*p == 'p') | |
616 | { | |
617 | /* Multi-process ptid. */ | |
618 | pp = unpack_varlen_hex (p + 1, &pid); | |
619 | if (*pp != '.') | |
620 | error ("invalid remote ptid: %s\n", p); | |
621 | ||
622 | p = pp + 1; | |
623 | ||
624 | tid = hex_or_minus_one (p, &pp); | |
625 | ||
626 | if (obuf) | |
627 | *obuf = pp; | |
628 | return ptid_build (pid, tid, 0); | |
629 | } | |
630 | ||
631 | /* No multi-process. Just a tid. */ | |
632 | tid = hex_or_minus_one (p, &pp); | |
633 | ||
634 | /* Since the stub is not sending a process id, then default to | |
635 | what's in the current inferior. */ | |
636 | pid = ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id); | |
637 | ||
638 | if (obuf) | |
639 | *obuf = pp; | |
640 | return ptid_build (pid, tid, 0); | |
641 | } | |
642 | ||
c906108c | 643 | /* Send a packet to the remote machine, with error checking. |
01f9e8fa DJ |
644 | The data of the packet is in BUF, and the length of the |
645 | packet is in CNT. Returns >= 0 on success, -1 otherwise. */ | |
c906108c | 646 | |
bd99dc85 PA |
647 | static int |
648 | putpkt_binary_1 (char *buf, int cnt, int is_notif) | |
c906108c SS |
649 | { |
650 | int i; | |
651 | unsigned char csum = 0; | |
0a30fbc4 | 652 | char *buf2; |
c906108c | 653 | char buf3[1]; |
c906108c SS |
654 | char *p; |
655 | ||
bca929d3 | 656 | buf2 = xmalloc (PBUFSIZ); |
0a30fbc4 | 657 | |
c906108c SS |
658 | /* Copy the packet into buffer BUF2, encapsulating it |
659 | and giving it a checksum. */ | |
660 | ||
661 | p = buf2; | |
bd99dc85 PA |
662 | if (is_notif) |
663 | *p++ = '%'; | |
664 | else | |
665 | *p++ = '$'; | |
c906108c | 666 | |
5ffff7c1 DJ |
667 | for (i = 0; i < cnt;) |
668 | i += try_rle (buf + i, cnt - i, &csum, &p); | |
669 | ||
c906108c SS |
670 | *p++ = '#'; |
671 | *p++ = tohex ((csum >> 4) & 0xf); | |
672 | *p++ = tohex (csum & 0xf); | |
673 | ||
674 | *p = '\0'; | |
675 | ||
676 | /* Send it over and over until we get a positive ack. */ | |
677 | ||
678 | do | |
679 | { | |
680 | int cc; | |
681 | ||
0f48aa01 | 682 | if (write (remote_desc, buf2, p - buf2) != p - buf2) |
c906108c SS |
683 | { |
684 | perror ("putpkt(write)"); | |
f88c79e6 | 685 | free (buf2); |
c906108c SS |
686 | return -1; |
687 | } | |
688 | ||
bd99dc85 | 689 | if (noack_mode || is_notif) |
a6f3e723 SL |
690 | { |
691 | /* Don't expect an ack then. */ | |
692 | if (remote_debug) | |
693 | { | |
bd99dc85 PA |
694 | if (is_notif) |
695 | fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2); | |
696 | else | |
697 | fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2); | |
a6f3e723 SL |
698 | fflush (stderr); |
699 | } | |
700 | break; | |
701 | } | |
702 | ||
c906108c | 703 | if (remote_debug) |
0d62e5e8 DJ |
704 | { |
705 | fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2); | |
706 | fflush (stderr); | |
707 | } | |
0f48aa01 | 708 | cc = read (remote_desc, buf3, 1); |
c906108c | 709 | if (remote_debug) |
0d62e5e8 DJ |
710 | { |
711 | fprintf (stderr, "[received '%c' (0x%x)]\n", buf3[0], buf3[0]); | |
712 | fflush (stderr); | |
713 | } | |
714 | ||
c906108c SS |
715 | if (cc <= 0) |
716 | { | |
717 | if (cc == 0) | |
718 | fprintf (stderr, "putpkt(read): Got EOF\n"); | |
719 | else | |
720 | perror ("putpkt(read)"); | |
721 | ||
0a30fbc4 | 722 | free (buf2); |
c906108c SS |
723 | return -1; |
724 | } | |
0d62e5e8 DJ |
725 | |
726 | /* Check for an input interrupt while we're here. */ | |
2d717e4f | 727 | if (buf3[0] == '\003' && current_inferior != NULL) |
ef57601b | 728 | (*the_target->request_interrupt) (); |
c906108c SS |
729 | } |
730 | while (buf3[0] != '+'); | |
731 | ||
0a30fbc4 | 732 | free (buf2); |
c906108c SS |
733 | return 1; /* Success! */ |
734 | } | |
735 | ||
bd99dc85 PA |
736 | int |
737 | putpkt_binary (char *buf, int cnt) | |
738 | { | |
739 | return putpkt_binary_1 (buf, cnt, 0); | |
740 | } | |
741 | ||
01f9e8fa DJ |
742 | /* Send a packet to the remote machine, with error checking. The data |
743 | of the packet is in BUF, and the packet should be a NUL-terminated | |
744 | string. Returns >= 0 on success, -1 otherwise. */ | |
745 | ||
746 | int | |
747 | putpkt (char *buf) | |
748 | { | |
749 | return putpkt_binary (buf, strlen (buf)); | |
750 | } | |
751 | ||
bd99dc85 PA |
752 | int |
753 | putpkt_notif (char *buf) | |
754 | { | |
755 | return putpkt_binary_1 (buf, strlen (buf), 1); | |
756 | } | |
757 | ||
c906108c SS |
758 | /* Come here when we get an input interrupt from the remote side. This |
759 | interrupt should only be active while we are waiting for the child to do | |
760 | something. About the only thing that should come through is a ^C, which | |
ef57601b | 761 | will cause us to request child interruption. */ |
c906108c SS |
762 | |
763 | static void | |
0a30fbc4 | 764 | input_interrupt (int unused) |
c906108c | 765 | { |
cf30a8e1 C |
766 | fd_set readset; |
767 | struct timeval immediate = { 0, 0 }; | |
c906108c | 768 | |
cf30a8e1 C |
769 | /* Protect against spurious interrupts. This has been observed to |
770 | be a problem under NetBSD 1.4 and 1.5. */ | |
c906108c | 771 | |
cf30a8e1 C |
772 | FD_ZERO (&readset); |
773 | FD_SET (remote_desc, &readset); | |
774 | if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0) | |
c906108c | 775 | { |
cf30a8e1 | 776 | int cc; |
fd500816 | 777 | char c = 0; |
7390519e | 778 | |
0f48aa01 | 779 | cc = read (remote_desc, &c, 1); |
c906108c | 780 | |
2d717e4f | 781 | if (cc != 1 || c != '\003' || current_inferior == NULL) |
cf30a8e1 | 782 | { |
fd500816 DJ |
783 | fprintf (stderr, "input_interrupt, count = %d c = %d ('%c')\n", |
784 | cc, c, c); | |
cf30a8e1 C |
785 | return; |
786 | } | |
7390519e | 787 | |
ef57601b | 788 | (*the_target->request_interrupt) (); |
cf30a8e1 | 789 | } |
c906108c | 790 | } |
7390519e PA |
791 | |
792 | /* Check if the remote side sent us an interrupt request (^C). */ | |
793 | void | |
794 | check_remote_input_interrupt_request (void) | |
795 | { | |
796 | /* This function may be called before establishing communications, | |
797 | therefore we need to validate the remote descriptor. */ | |
798 | ||
799 | if (remote_desc == INVALID_DESCRIPTOR) | |
800 | return; | |
801 | ||
802 | input_interrupt (0); | |
803 | } | |
b80864fb DJ |
804 | |
805 | /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to | |
806 | accept Control-C from the client, and must be disabled when talking to | |
807 | the client. */ | |
c906108c | 808 | |
a20d5e98 | 809 | static void |
62ea82f5 DJ |
810 | unblock_async_io (void) |
811 | { | |
b80864fb | 812 | #ifndef USE_WIN32API |
62ea82f5 | 813 | sigset_t sigio_set; |
a20d5e98 | 814 | |
62ea82f5 DJ |
815 | sigemptyset (&sigio_set); |
816 | sigaddset (&sigio_set, SIGIO); | |
817 | sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); | |
b80864fb | 818 | #endif |
62ea82f5 DJ |
819 | } |
820 | ||
ac8c974e AR |
821 | #ifdef __QNX__ |
822 | static void | |
823 | nto_comctrl (int enable) | |
824 | { | |
825 | struct sigevent event; | |
826 | ||
827 | if (enable) | |
828 | { | |
829 | event.sigev_notify = SIGEV_SIGNAL_THREAD; | |
830 | event.sigev_signo = SIGIO; | |
831 | event.sigev_code = 0; | |
832 | event.sigev_value.sival_ptr = NULL; | |
833 | event.sigev_priority = -1; | |
834 | ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT, | |
835 | &event); | |
836 | } | |
837 | else | |
838 | ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL); | |
839 | } | |
840 | #endif /* __QNX__ */ | |
841 | ||
842 | ||
fd500816 DJ |
843 | /* Current state of asynchronous I/O. */ |
844 | static int async_io_enabled; | |
845 | ||
846 | /* Enable asynchronous I/O. */ | |
c906108c | 847 | void |
fba45db2 | 848 | enable_async_io (void) |
c906108c | 849 | { |
fd500816 DJ |
850 | if (async_io_enabled) |
851 | return; | |
852 | ||
b80864fb | 853 | #ifndef USE_WIN32API |
c906108c | 854 | signal (SIGIO, input_interrupt); |
b80864fb | 855 | #endif |
fd500816 | 856 | async_io_enabled = 1; |
ac8c974e AR |
857 | #ifdef __QNX__ |
858 | nto_comctrl (1); | |
859 | #endif /* __QNX__ */ | |
c906108c SS |
860 | } |
861 | ||
fd500816 | 862 | /* Disable asynchronous I/O. */ |
c906108c | 863 | void |
fba45db2 | 864 | disable_async_io (void) |
c906108c | 865 | { |
fd500816 DJ |
866 | if (!async_io_enabled) |
867 | return; | |
868 | ||
b80864fb | 869 | #ifndef USE_WIN32API |
c906108c | 870 | signal (SIGIO, SIG_IGN); |
b80864fb | 871 | #endif |
fd500816 | 872 | async_io_enabled = 0; |
ac8c974e AR |
873 | #ifdef __QNX__ |
874 | nto_comctrl (0); | |
875 | #endif /* __QNX__ */ | |
876 | ||
c906108c SS |
877 | } |
878 | ||
a20d5e98 DJ |
879 | void |
880 | initialize_async_io (void) | |
881 | { | |
882 | /* Make sure that async I/O starts disabled. */ | |
883 | async_io_enabled = 1; | |
884 | disable_async_io (); | |
885 | ||
886 | /* Make sure the signal is unblocked. */ | |
887 | unblock_async_io (); | |
888 | } | |
889 | ||
c906108c SS |
890 | /* Returns next char from remote GDB. -1 if error. */ |
891 | ||
892 | static int | |
fba45db2 | 893 | readchar (void) |
c906108c | 894 | { |
01f9e8fa | 895 | static unsigned char buf[BUFSIZ]; |
c906108c | 896 | static int bufcnt = 0; |
01f9e8fa | 897 | static unsigned char *bufp; |
c906108c SS |
898 | |
899 | if (bufcnt-- > 0) | |
01f9e8fa | 900 | return *bufp++; |
c906108c | 901 | |
0f48aa01 | 902 | bufcnt = read (remote_desc, buf, sizeof (buf)); |
c906108c SS |
903 | |
904 | if (bufcnt <= 0) | |
905 | { | |
906 | if (bufcnt == 0) | |
907 | fprintf (stderr, "readchar: Got EOF\n"); | |
908 | else | |
909 | perror ("readchar"); | |
910 | ||
911 | return -1; | |
912 | } | |
913 | ||
914 | bufp = buf; | |
915 | bufcnt--; | |
b79d787e | 916 | return *bufp++; |
c906108c SS |
917 | } |
918 | ||
919 | /* Read a packet from the remote machine, with error checking, | |
920 | and store it in BUF. Returns length of packet, or negative if error. */ | |
921 | ||
922 | int | |
fba45db2 | 923 | getpkt (char *buf) |
c906108c SS |
924 | { |
925 | char *bp; | |
926 | unsigned char csum, c1, c2; | |
927 | int c; | |
928 | ||
929 | while (1) | |
930 | { | |
931 | csum = 0; | |
932 | ||
933 | while (1) | |
934 | { | |
935 | c = readchar (); | |
936 | if (c == '$') | |
937 | break; | |
938 | if (remote_debug) | |
0d62e5e8 DJ |
939 | { |
940 | fprintf (stderr, "[getpkt: discarding char '%c']\n", c); | |
941 | fflush (stderr); | |
942 | } | |
943 | ||
c906108c SS |
944 | if (c < 0) |
945 | return -1; | |
946 | } | |
947 | ||
948 | bp = buf; | |
949 | while (1) | |
950 | { | |
951 | c = readchar (); | |
952 | if (c < 0) | |
953 | return -1; | |
954 | if (c == '#') | |
955 | break; | |
956 | *bp++ = c; | |
957 | csum += c; | |
958 | } | |
959 | *bp = 0; | |
960 | ||
961 | c1 = fromhex (readchar ()); | |
962 | c2 = fromhex (readchar ()); | |
c5aa993b | 963 | |
c906108c SS |
964 | if (csum == (c1 << 4) + c2) |
965 | break; | |
966 | ||
a6f3e723 SL |
967 | if (noack_mode) |
968 | { | |
969 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n", | |
970 | (c1 << 4) + c2, csum, buf); | |
971 | /* Not much we can do, GDB wasn't expecting an ack/nac. */ | |
972 | break; | |
973 | } | |
974 | ||
c906108c SS |
975 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", |
976 | (c1 << 4) + c2, csum, buf); | |
0f48aa01 | 977 | write (remote_desc, "-", 1); |
c906108c SS |
978 | } |
979 | ||
a6f3e723 | 980 | if (!noack_mode) |
0d62e5e8 | 981 | { |
a6f3e723 SL |
982 | if (remote_debug) |
983 | { | |
984 | fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); | |
985 | fflush (stderr); | |
986 | } | |
c906108c | 987 | |
a6f3e723 | 988 | write (remote_desc, "+", 1); |
c906108c | 989 | |
a6f3e723 SL |
990 | if (remote_debug) |
991 | { | |
992 | fprintf (stderr, "[sent ack]\n"); | |
993 | fflush (stderr); | |
994 | } | |
0d62e5e8 | 995 | } |
86b1f9c5 PM |
996 | else |
997 | { | |
998 | if (remote_debug) | |
999 | { | |
1000 | fprintf (stderr, "getpkt (\"%s\"); [no ack sent] \n", buf); | |
1001 | fflush (stderr); | |
1002 | } | |
1003 | } | |
0d62e5e8 | 1004 | |
c906108c SS |
1005 | return bp - buf; |
1006 | } | |
1007 | ||
1008 | void | |
fba45db2 | 1009 | write_ok (char *buf) |
c906108c SS |
1010 | { |
1011 | buf[0] = 'O'; | |
1012 | buf[1] = 'K'; | |
1013 | buf[2] = '\0'; | |
1014 | } | |
1015 | ||
1016 | void | |
fba45db2 | 1017 | write_enn (char *buf) |
c906108c | 1018 | { |
c89dc5d4 | 1019 | /* Some day, we should define the meanings of the error codes... */ |
c906108c | 1020 | buf[0] = 'E'; |
c89dc5d4 DJ |
1021 | buf[1] = '0'; |
1022 | buf[2] = '1'; | |
c906108c SS |
1023 | buf[3] = '\0'; |
1024 | } | |
1025 | ||
1026 | void | |
f450004a | 1027 | convert_int_to_ascii (unsigned char *from, char *to, int n) |
c906108c SS |
1028 | { |
1029 | int nib; | |
f450004a | 1030 | int ch; |
c906108c SS |
1031 | while (n--) |
1032 | { | |
1033 | ch = *from++; | |
1034 | nib = ((ch & 0xf0) >> 4) & 0x0f; | |
1035 | *to++ = tohex (nib); | |
1036 | nib = ch & 0x0f; | |
1037 | *to++ = tohex (nib); | |
1038 | } | |
1039 | *to++ = 0; | |
1040 | } | |
1041 | ||
1042 | ||
1043 | void | |
f450004a | 1044 | convert_ascii_to_int (char *from, unsigned char *to, int n) |
c906108c SS |
1045 | { |
1046 | int nib1, nib2; | |
1047 | while (n--) | |
1048 | { | |
1049 | nib1 = fromhex (*from++); | |
1050 | nib2 = fromhex (*from++); | |
1051 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | static char * | |
fba45db2 | 1056 | outreg (int regno, char *buf) |
c906108c | 1057 | { |
5c44784c JM |
1058 | if ((regno >> 12) != 0) |
1059 | *buf++ = tohex ((regno >> 12) & 0xf); | |
1060 | if ((regno >> 8) != 0) | |
1061 | *buf++ = tohex ((regno >> 8) & 0xf); | |
1062 | *buf++ = tohex ((regno >> 4) & 0xf); | |
c906108c SS |
1063 | *buf++ = tohex (regno & 0xf); |
1064 | *buf++ = ':'; | |
0d62e5e8 DJ |
1065 | collect_register_as_string (regno, buf); |
1066 | buf += 2 * register_size (regno); | |
c906108c SS |
1067 | *buf++ = ';'; |
1068 | ||
1069 | return buf; | |
1070 | } | |
1071 | ||
0d62e5e8 DJ |
1072 | void |
1073 | new_thread_notify (int id) | |
1074 | { | |
1075 | char own_buf[256]; | |
1076 | ||
1077 | /* The `n' response is not yet part of the remote protocol. Do nothing. */ | |
1078 | if (1) | |
1079 | return; | |
1080 | ||
1081 | if (server_waiting == 0) | |
1082 | return; | |
1083 | ||
1084 | sprintf (own_buf, "n%x", id); | |
1085 | disable_async_io (); | |
1086 | putpkt (own_buf); | |
1087 | enable_async_io (); | |
1088 | } | |
1089 | ||
1090 | void | |
1091 | dead_thread_notify (int id) | |
1092 | { | |
1093 | char own_buf[256]; | |
1094 | ||
1095 | /* The `x' response is not yet part of the remote protocol. Do nothing. */ | |
1096 | if (1) | |
1097 | return; | |
1098 | ||
1099 | sprintf (own_buf, "x%x", id); | |
1100 | disable_async_io (); | |
1101 | putpkt (own_buf); | |
1102 | enable_async_io (); | |
1103 | } | |
1104 | ||
c906108c | 1105 | void |
95954743 | 1106 | prepare_resume_reply (char *buf, ptid_t ptid, |
5b1c542e | 1107 | struct target_waitstatus *status) |
c906108c | 1108 | { |
5b1c542e | 1109 | if (debug_threads) |
95954743 PA |
1110 | fprintf (stderr, "Writing resume reply for %s:%d\n\n", |
1111 | target_pid_to_str (ptid), status->kind); | |
c906108c | 1112 | |
5b1c542e | 1113 | switch (status->kind) |
c906108c | 1114 | { |
5b1c542e PA |
1115 | case TARGET_WAITKIND_STOPPED: |
1116 | { | |
1117 | struct thread_info *saved_inferior; | |
1118 | const char **regp; | |
e013ee27 | 1119 | |
5b1c542e PA |
1120 | sprintf (buf, "T%02x", status->value.sig); |
1121 | buf += strlen (buf); | |
e013ee27 | 1122 | |
5b1c542e | 1123 | regp = gdbserver_expedite_regs; |
e013ee27 | 1124 | |
5b1c542e | 1125 | saved_inferior = current_inferior; |
e013ee27 | 1126 | |
e09875d4 | 1127 | current_inferior = find_thread_ptid (ptid); |
e013ee27 | 1128 | |
5b1c542e PA |
1129 | if (the_target->stopped_by_watchpoint != NULL |
1130 | && (*the_target->stopped_by_watchpoint) ()) | |
1131 | { | |
1132 | CORE_ADDR addr; | |
1133 | int i; | |
c906108c | 1134 | |
5b1c542e PA |
1135 | strncpy (buf, "watch:", 6); |
1136 | buf += 6; | |
0d62e5e8 | 1137 | |
5b1c542e | 1138 | addr = (*the_target->stopped_data_address) (); |
255e7678 | 1139 | |
5b1c542e PA |
1140 | /* Convert each byte of the address into two hexadecimal |
1141 | chars. Note that we take sizeof (void *) instead of | |
1142 | sizeof (addr); this is to avoid sending a 64-bit | |
1143 | address to a 32-bit GDB. */ | |
1144 | for (i = sizeof (void *) * 2; i > 0; i--) | |
1145 | *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); | |
1146 | *buf++ = ';'; | |
1147 | } | |
1148 | ||
1149 | while (*regp) | |
1150 | { | |
1151 | buf = outreg (find_regno (*regp), buf); | |
1152 | regp ++; | |
1153 | } | |
5472f405 | 1154 | *buf = '\0'; |
5b1c542e PA |
1155 | |
1156 | /* Formerly, if the debugger had not used any thread features | |
1157 | we would not burden it with a thread status response. This | |
1158 | was for the benefit of GDB 4.13 and older. However, in | |
1159 | recent GDB versions the check (``if (cont_thread != 0)'') | |
1160 | does not have the desired effect because of sillyness in | |
1161 | the way that the remote protocol handles specifying a | |
1162 | thread. Since thread support relies on qSymbol support | |
1163 | anyway, assume GDB can handle threads. */ | |
1164 | ||
1165 | if (using_threads && !disable_packet_Tthread) | |
1166 | { | |
1167 | /* This if (1) ought to be unnecessary. But remote_wait | |
1168 | in GDB will claim this event belongs to inferior_ptid | |
1169 | if we do not specify a thread, and there's no way for | |
1170 | gdbserver to know what inferior_ptid is. */ | |
95954743 | 1171 | if (1 || !ptid_equal (general_thread, ptid)) |
5b1c542e | 1172 | { |
bd99dc85 PA |
1173 | /* In non-stop, don't change the general thread behind |
1174 | GDB's back. */ | |
1175 | if (!non_stop) | |
1176 | general_thread = ptid; | |
95954743 PA |
1177 | sprintf (buf, "thread:"); |
1178 | buf += strlen (buf); | |
1179 | buf = write_ptid (buf, ptid); | |
1180 | strcat (buf, ";"); | |
5b1c542e PA |
1181 | buf += strlen (buf); |
1182 | } | |
1183 | } | |
1184 | ||
1185 | if (dlls_changed) | |
1186 | { | |
1187 | strcpy (buf, "library:;"); | |
1188 | buf += strlen (buf); | |
1189 | dlls_changed = 0; | |
1190 | } | |
1191 | ||
1192 | current_inferior = saved_inferior; | |
1193 | } | |
1194 | break; | |
1195 | case TARGET_WAITKIND_EXITED: | |
95954743 PA |
1196 | if (multi_process) |
1197 | sprintf (buf, "W%x;process:%x", | |
1198 | status->value.integer, ptid_get_pid (ptid)); | |
1199 | else | |
1200 | sprintf (buf, "W%02x", status->value.integer); | |
5b1c542e PA |
1201 | break; |
1202 | case TARGET_WAITKIND_SIGNALLED: | |
95954743 PA |
1203 | if (multi_process) |
1204 | sprintf (buf, "X%x;process:%x", | |
1205 | status->value.sig, ptid_get_pid (ptid)); | |
1206 | else | |
1207 | sprintf (buf, "X%02x", status->value.sig); | |
5b1c542e PA |
1208 | break; |
1209 | default: | |
1210 | error ("unhandled waitkind"); | |
1211 | break; | |
c906108c | 1212 | } |
c906108c SS |
1213 | } |
1214 | ||
1215 | void | |
fba45db2 | 1216 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
c906108c SS |
1217 | { |
1218 | int i = 0, j = 0; | |
1219 | char ch; | |
1220 | *mem_addr_ptr = *len_ptr = 0; | |
1221 | ||
1222 | while ((ch = from[i++]) != ',') | |
1223 | { | |
1224 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1225 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1226 | } | |
1227 | ||
1228 | for (j = 0; j < 4; j++) | |
1229 | { | |
1230 | if ((ch = from[i++]) == 0) | |
1231 | break; | |
1232 | *len_ptr = *len_ptr << 4; | |
1233 | *len_ptr |= fromhex (ch) & 0x0f; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | void | |
fba45db2 | 1238 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
f450004a | 1239 | unsigned char *to) |
c906108c SS |
1240 | { |
1241 | int i = 0; | |
1242 | char ch; | |
1243 | *mem_addr_ptr = *len_ptr = 0; | |
1244 | ||
1245 | while ((ch = from[i++]) != ',') | |
1246 | { | |
1247 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1248 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1249 | } | |
1250 | ||
1251 | while ((ch = from[i++]) != ':') | |
1252 | { | |
1253 | *len_ptr = *len_ptr << 4; | |
1254 | *len_ptr |= fromhex (ch) & 0x0f; | |
1255 | } | |
1256 | ||
1257 | convert_ascii_to_int (&from[i++], to, *len_ptr); | |
1258 | } | |
2f2893d9 | 1259 | |
01f9e8fa DJ |
1260 | int |
1261 | decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, | |
1262 | unsigned int *len_ptr, unsigned char *to) | |
1263 | { | |
1264 | int i = 0; | |
1265 | char ch; | |
1266 | *mem_addr_ptr = *len_ptr = 0; | |
1267 | ||
1268 | while ((ch = from[i++]) != ',') | |
1269 | { | |
1270 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1271 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1272 | } | |
1273 | ||
1274 | while ((ch = from[i++]) != ':') | |
1275 | { | |
1276 | *len_ptr = *len_ptr << 4; | |
1277 | *len_ptr |= fromhex (ch) & 0x0f; | |
1278 | } | |
1279 | ||
1280 | if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, | |
1281 | to, *len_ptr) != *len_ptr) | |
1282 | return -1; | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
0e7f50da UW |
1287 | /* Decode a qXfer write request. */ |
1288 | int | |
1289 | decode_xfer_write (char *buf, int packet_len, char **annex, CORE_ADDR *offset, | |
1290 | unsigned int *len, unsigned char *data) | |
1291 | { | |
1292 | char ch; | |
1293 | ||
1294 | /* Extract and NUL-terminate the annex. */ | |
1295 | *annex = buf; | |
1296 | while (*buf && *buf != ':') | |
1297 | buf++; | |
1298 | if (*buf == '\0') | |
1299 | return -1; | |
1300 | *buf++ = 0; | |
1301 | ||
1302 | /* Extract the offset. */ | |
1303 | *offset = 0; | |
1304 | while ((ch = *buf++) != ':') | |
1305 | { | |
1306 | *offset = *offset << 4; | |
1307 | *offset |= fromhex (ch) & 0x0f; | |
1308 | } | |
1309 | ||
1310 | /* Get encoded data. */ | |
1311 | packet_len -= buf - *annex; | |
1312 | *len = remote_unescape_input ((const gdb_byte *) buf, packet_len, | |
1313 | data, packet_len); | |
1314 | return 0; | |
1315 | } | |
1316 | ||
08388c79 DE |
1317 | /* Decode the parameters of a qSearch:memory packet. */ |
1318 | ||
1319 | int | |
1320 | decode_search_memory_packet (const char *buf, int packet_len, | |
1321 | CORE_ADDR *start_addrp, | |
1322 | CORE_ADDR *search_space_lenp, | |
1323 | gdb_byte *pattern, unsigned int *pattern_lenp) | |
1324 | { | |
1325 | const char *p = buf; | |
1326 | ||
1327 | p = decode_address_to_semicolon (start_addrp, p); | |
1328 | p = decode_address_to_semicolon (search_space_lenp, p); | |
1329 | packet_len -= p - buf; | |
1330 | *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len, | |
1331 | pattern, packet_len); | |
1332 | return 0; | |
1333 | } | |
1334 | ||
95954743 PA |
1335 | static void |
1336 | free_sym_cache (struct sym_cache *sym) | |
1337 | { | |
1338 | if (sym != NULL) | |
1339 | { | |
1340 | free (sym->name); | |
1341 | free (sym); | |
1342 | } | |
1343 | } | |
1344 | ||
1345 | void | |
1346 | clear_symbol_cache (struct sym_cache **symcache_p) | |
1347 | { | |
1348 | struct sym_cache *sym, *next; | |
1349 | ||
1350 | /* Check the cache first. */ | |
1351 | for (sym = *symcache_p; sym; sym = next) | |
1352 | { | |
1353 | next = sym->next; | |
1354 | free_sym_cache (sym); | |
1355 | } | |
1356 | ||
1357 | *symcache_p = NULL; | |
1358 | } | |
1359 | ||
fd500816 DJ |
1360 | /* Ask GDB for the address of NAME, and return it in ADDRP if found. |
1361 | Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ | |
1362 | ||
2f2893d9 DJ |
1363 | int |
1364 | look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
1365 | { | |
1366 | char own_buf[266], *p, *q; | |
1367 | int len; | |
fd500816 | 1368 | struct sym_cache *sym; |
95954743 PA |
1369 | struct process_info *proc; |
1370 | ||
1371 | proc = current_process (); | |
fd500816 DJ |
1372 | |
1373 | /* Check the cache first. */ | |
95954743 | 1374 | for (sym = proc->symbol_cache; sym; sym = sym->next) |
fd500816 DJ |
1375 | if (strcmp (name, sym->name) == 0) |
1376 | { | |
1377 | *addrp = sym->addr; | |
1378 | return 1; | |
1379 | } | |
2f2893d9 | 1380 | |
ea025f5f DJ |
1381 | /* If we've passed the call to thread_db_look_up_symbols, then |
1382 | anything not in the cache must not exist; we're not interested | |
1383 | in any libraries loaded after that point, only in symbols in | |
1384 | libpthread.so. It might not be an appropriate time to look | |
1385 | up a symbol, e.g. while we're trying to fetch registers. */ | |
95954743 | 1386 | if (proc->all_symbols_looked_up) |
ea025f5f DJ |
1387 | return 0; |
1388 | ||
2f2893d9 DJ |
1389 | /* Send the request. */ |
1390 | strcpy (own_buf, "qSymbol:"); | |
1391 | hexify (own_buf + strlen ("qSymbol:"), name, strlen (name)); | |
1392 | if (putpkt (own_buf) < 0) | |
1393 | return -1; | |
1394 | ||
1395 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ | |
1396 | len = getpkt (own_buf); | |
1397 | if (len < 0) | |
1398 | return -1; | |
1399 | ||
2bbe3cc1 DJ |
1400 | /* We ought to handle pretty much any packet at this point while we |
1401 | wait for the qSymbol "response". That requires re-entering the | |
1402 | main loop. For now, this is an adequate approximation; allow | |
1403 | GDB to read from memory while it figures out the address of the | |
1404 | symbol. */ | |
1405 | while (own_buf[0] == 'm') | |
1406 | { | |
1407 | CORE_ADDR mem_addr; | |
1408 | unsigned char *mem_buf; | |
1409 | unsigned int mem_len; | |
1410 | ||
1411 | decode_m_packet (&own_buf[1], &mem_addr, &mem_len); | |
bca929d3 | 1412 | mem_buf = xmalloc (mem_len); |
2bbe3cc1 DJ |
1413 | if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) |
1414 | convert_int_to_ascii (mem_buf, own_buf, mem_len); | |
1415 | else | |
1416 | write_enn (own_buf); | |
1417 | free (mem_buf); | |
1418 | if (putpkt (own_buf) < 0) | |
1419 | return -1; | |
1420 | len = getpkt (own_buf); | |
1421 | if (len < 0) | |
1422 | return -1; | |
1423 | } | |
1b3f6016 | 1424 | |
2f2893d9 DJ |
1425 | if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0) |
1426 | { | |
2bbe3cc1 | 1427 | warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf); |
2f2893d9 DJ |
1428 | return -1; |
1429 | } | |
1430 | ||
1431 | p = own_buf + strlen ("qSymbol:"); | |
1432 | q = p; | |
1433 | while (*q && *q != ':') | |
1434 | q++; | |
1435 | ||
1436 | /* Make sure we found a value for the symbol. */ | |
1437 | if (p == q || *q == '\0') | |
1438 | return 0; | |
1439 | ||
1440 | decode_address (addrp, p, q - p); | |
fd500816 DJ |
1441 | |
1442 | /* Save the symbol in our cache. */ | |
bca929d3 DE |
1443 | sym = xmalloc (sizeof (*sym)); |
1444 | sym->name = xstrdup (name); | |
fd500816 | 1445 | sym->addr = *addrp; |
95954743 PA |
1446 | sym->next = proc->symbol_cache; |
1447 | proc->symbol_cache = sym; | |
fd500816 | 1448 | |
2f2893d9 DJ |
1449 | return 1; |
1450 | } | |
c74d0ad8 DJ |
1451 | |
1452 | void | |
bce7165d | 1453 | monitor_output (const char *msg) |
c74d0ad8 | 1454 | { |
bca929d3 | 1455 | char *buf = xmalloc (strlen (msg) * 2 + 2); |
c74d0ad8 DJ |
1456 | |
1457 | buf[0] = 'O'; | |
1458 | hexify (buf + 1, msg, 0); | |
1459 | ||
1460 | putpkt (buf); | |
1461 | free (buf); | |
1462 | } | |
255e7678 DJ |
1463 | |
1464 | /* Return a malloc allocated string with special characters from TEXT | |
1465 | replaced by entity references. */ | |
1466 | ||
1467 | char * | |
1468 | xml_escape_text (const char *text) | |
1469 | { | |
1470 | char *result; | |
1471 | int i, special; | |
1472 | ||
1473 | /* Compute the length of the result. */ | |
1474 | for (i = 0, special = 0; text[i] != '\0'; i++) | |
1475 | switch (text[i]) | |
1476 | { | |
1477 | case '\'': | |
1478 | case '\"': | |
1479 | special += 5; | |
1480 | break; | |
1481 | case '&': | |
1482 | special += 4; | |
1483 | break; | |
1484 | case '<': | |
1485 | case '>': | |
1486 | special += 3; | |
1487 | break; | |
1488 | default: | |
1489 | break; | |
1490 | } | |
1491 | ||
1492 | /* Expand the result. */ | |
bca929d3 | 1493 | result = xmalloc (i + special + 1); |
255e7678 DJ |
1494 | for (i = 0, special = 0; text[i] != '\0'; i++) |
1495 | switch (text[i]) | |
1496 | { | |
1497 | case '\'': | |
1498 | strcpy (result + i + special, "'"); | |
1499 | special += 5; | |
1500 | break; | |
1501 | case '\"': | |
1502 | strcpy (result + i + special, """); | |
1503 | special += 5; | |
1504 | break; | |
1505 | case '&': | |
1506 | strcpy (result + i + special, "&"); | |
1507 | special += 4; | |
1508 | break; | |
1509 | case '<': | |
1510 | strcpy (result + i + special, "<"); | |
1511 | special += 3; | |
1512 | break; | |
1513 | case '>': | |
1514 | strcpy (result + i + special, ">"); | |
1515 | special += 3; | |
1516 | break; | |
1517 | default: | |
1518 | result[i + special] = text[i]; | |
1519 | break; | |
1520 | } | |
1521 | result[i + special] = '\0'; | |
1522 | ||
1523 | return result; | |
1524 | } | |
07e059b5 VP |
1525 | |
1526 | void | |
1527 | buffer_grow (struct buffer *buffer, const char *data, size_t size) | |
1528 | { | |
1529 | char *new_buffer; | |
1530 | size_t new_buffer_size; | |
1531 | ||
1532 | if (size == 0) | |
1533 | return; | |
1534 | ||
1535 | new_buffer_size = buffer->buffer_size; | |
1536 | ||
1537 | if (new_buffer_size == 0) | |
1538 | new_buffer_size = 1; | |
1539 | ||
1540 | while (buffer->used_size + size > new_buffer_size) | |
1541 | new_buffer_size *= 2; | |
1542 | new_buffer = realloc (buffer->buffer, new_buffer_size); | |
1543 | if (!new_buffer) | |
1544 | abort (); | |
1545 | memcpy (new_buffer + buffer->used_size, data, size); | |
1546 | buffer->buffer = new_buffer; | |
1547 | buffer->buffer_size = new_buffer_size; | |
1548 | buffer->used_size += size; | |
1549 | } | |
1550 | ||
1551 | void | |
1552 | buffer_free (struct buffer *buffer) | |
1553 | { | |
1554 | if (!buffer) | |
1555 | return; | |
1556 | ||
1557 | free (buffer->buffer); | |
1558 | buffer->buffer = NULL; | |
1559 | buffer->buffer_size = 0; | |
1560 | buffer->used_size = 0; | |
1561 | } | |
1562 | ||
1563 | void | |
1564 | buffer_init (struct buffer *buffer) | |
1565 | { | |
1566 | memset (buffer, 0, sizeof (*buffer)); | |
1567 | } | |
1568 | ||
1569 | char* | |
1570 | buffer_finish (struct buffer *buffer) | |
1571 | { | |
1572 | char *ret = buffer->buffer; | |
1573 | buffer->buffer = NULL; | |
1574 | buffer->buffer_size = 0; | |
1575 | buffer->used_size = 0; | |
1576 | return ret; | |
1577 | } | |
1578 | ||
1579 | void | |
1580 | buffer_xml_printf (struct buffer *buffer, const char *format, ...) | |
1581 | { | |
1582 | va_list ap; | |
1583 | const char *f; | |
1584 | const char *prev; | |
1585 | int percent = 0; | |
1586 | ||
1587 | va_start (ap, format); | |
1588 | ||
1589 | prev = format; | |
1590 | for (f = format; *f; f++) | |
1591 | { | |
1592 | if (percent) | |
1593 | { | |
1b3f6016 PA |
1594 | switch (*f) |
1595 | { | |
1596 | case 's': | |
1597 | { | |
1598 | char *p; | |
1599 | char *a = va_arg (ap, char *); | |
1600 | buffer_grow (buffer, prev, f - prev - 1); | |
1601 | p = xml_escape_text (a); | |
1602 | buffer_grow_str (buffer, p); | |
1603 | free (p); | |
1604 | prev = f + 1; | |
1605 | } | |
1606 | break; | |
1607 | } | |
1608 | percent = 0; | |
07e059b5 VP |
1609 | } |
1610 | else if (*f == '%') | |
1611 | percent = 1; | |
1612 | } | |
1613 | ||
1614 | buffer_grow_str (buffer, prev); | |
1615 | va_end (ap); | |
1616 | } |