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, |
9b254dd1 DJ |
3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
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" | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
b80864fb | 25 | #if HAVE_SYS_IOCTL_H |
c906108c | 26 | #include <sys/ioctl.h> |
b80864fb | 27 | #endif |
68070c10 | 28 | #if HAVE_SYS_FILE_H |
c906108c | 29 | #include <sys/file.h> |
68070c10 | 30 | #endif |
b80864fb | 31 | #if HAVE_NETINET_IN_H |
c906108c | 32 | #include <netinet/in.h> |
b80864fb DJ |
33 | #endif |
34 | #if HAVE_SYS_SOCKET_H | |
c906108c | 35 | #include <sys/socket.h> |
b80864fb DJ |
36 | #endif |
37 | #if HAVE_NETDB_H | |
c906108c | 38 | #include <netdb.h> |
b80864fb DJ |
39 | #endif |
40 | #if HAVE_NETINET_TCP_H | |
c906108c | 41 | #include <netinet/tcp.h> |
b80864fb DJ |
42 | #endif |
43 | #if HAVE_SYS_IOCTL_H | |
c906108c | 44 | #include <sys/ioctl.h> |
b80864fb | 45 | #endif |
68070c10 | 46 | #if HAVE_SIGNAL_H |
c906108c | 47 | #include <signal.h> |
68070c10 PA |
48 | #endif |
49 | #if HAVE_FCNTL_H | |
c906108c | 50 | #include <fcntl.h> |
68070c10 | 51 | #endif |
cf30a8e1 | 52 | #include <sys/time.h> |
68070c10 | 53 | #if HAVE_UNISTD_H |
cf30a8e1 | 54 | #include <unistd.h> |
68070c10 | 55 | #endif |
b80864fb | 56 | #if HAVE_ARPA_INET_H |
0729219d | 57 | #include <arpa/inet.h> |
b80864fb | 58 | #endif |
8264bb58 | 59 | #include <sys/stat.h> |
68070c10 | 60 | #if HAVE_ERRNO_H |
8264bb58 | 61 | #include <errno.h> |
68070c10 | 62 | #endif |
b80864fb DJ |
63 | |
64 | #if USE_WIN32API | |
65 | #include <winsock.h> | |
66 | #endif | |
c906108c | 67 | |
f450004a DJ |
68 | #ifndef HAVE_SOCKLEN_T |
69 | typedef int socklen_t; | |
70 | #endif | |
71 | ||
7390519e PA |
72 | #if USE_WIN32API |
73 | # define INVALID_DESCRIPTOR INVALID_SOCKET | |
74 | #else | |
75 | # define INVALID_DESCRIPTOR -1 | |
76 | #endif | |
77 | ||
fd500816 DJ |
78 | /* A cache entry for a successfully looked-up symbol. */ |
79 | struct sym_cache | |
80 | { | |
81 | const char *name; | |
82 | CORE_ADDR addr; | |
83 | struct sym_cache *next; | |
84 | }; | |
85 | ||
86 | /* The symbol cache. */ | |
87 | static struct sym_cache *symbol_cache; | |
88 | ||
ea025f5f DJ |
89 | /* If this flag has been set, assume cache misses are |
90 | failures. */ | |
91 | int all_symbols_looked_up; | |
92 | ||
c906108c | 93 | int remote_debug = 0; |
03863182 | 94 | struct ui_file *gdb_stdlog; |
c906108c | 95 | |
7390519e | 96 | static int remote_desc = INVALID_DESCRIPTOR; |
c906108c | 97 | |
0d62e5e8 DJ |
98 | /* FIXME headerize? */ |
99 | extern int using_threads; | |
100 | extern int debug_threads; | |
101 | ||
0f48aa01 | 102 | #ifdef USE_WIN32API |
68070c10 PA |
103 | # define read(fd, buf, len) recv (fd, (char *) buf, len, 0) |
104 | # define write(fd, buf, len) send (fd, (char *) buf, len, 0) | |
0f48aa01 DJ |
105 | #endif |
106 | ||
c906108c SS |
107 | /* Open a connection to a remote debugger. |
108 | NAME is the filename used for communication. */ | |
109 | ||
110 | void | |
fba45db2 | 111 | remote_open (char *name) |
c906108c | 112 | { |
b80864fb | 113 | #if defined(F_SETFL) && defined (FASYNC) |
c906108c | 114 | int save_fcntl_flags; |
b80864fb | 115 | #endif |
8264bb58 DJ |
116 | char *port_str; |
117 | ||
118 | port_str = strchr (name, ':'); | |
119 | if (port_str == NULL) | |
c906108c | 120 | { |
b80864fb DJ |
121 | #ifdef USE_WIN32API |
122 | error ("Only <host>:<port> is supported on this platform."); | |
123 | #else | |
8264bb58 DJ |
124 | struct stat statbuf; |
125 | ||
126 | if (stat (name, &statbuf) == 0 | |
127 | && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) | |
128 | remote_desc = open (name, O_RDWR); | |
129 | else | |
130 | { | |
131 | errno = EINVAL; | |
132 | remote_desc = -1; | |
133 | } | |
134 | ||
c906108c SS |
135 | if (remote_desc < 0) |
136 | perror_with_name ("Could not open remote device"); | |
137 | ||
138 | #ifdef HAVE_TERMIOS | |
139 | { | |
140 | struct termios termios; | |
c5aa993b | 141 | tcgetattr (remote_desc, &termios); |
c906108c SS |
142 | |
143 | termios.c_iflag = 0; | |
144 | termios.c_oflag = 0; | |
145 | termios.c_lflag = 0; | |
c5aa993b | 146 | termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 147 | termios.c_cflag |= CLOCAL | CS8; |
d0608e50 | 148 | termios.c_cc[VMIN] = 1; |
c906108c SS |
149 | termios.c_cc[VTIME] = 0; |
150 | ||
c5aa993b | 151 | tcsetattr (remote_desc, TCSANOW, &termios); |
c906108c SS |
152 | } |
153 | #endif | |
154 | ||
155 | #ifdef HAVE_TERMIO | |
156 | { | |
157 | struct termio termio; | |
158 | ioctl (remote_desc, TCGETA, &termio); | |
159 | ||
160 | termio.c_iflag = 0; | |
161 | termio.c_oflag = 0; | |
162 | termio.c_lflag = 0; | |
c5aa993b | 163 | termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 164 | termio.c_cflag |= CLOCAL | CS8; |
d0608e50 | 165 | termio.c_cc[VMIN] = 1; |
c906108c SS |
166 | termio.c_cc[VTIME] = 0; |
167 | ||
168 | ioctl (remote_desc, TCSETA, &termio); | |
169 | } | |
170 | #endif | |
171 | ||
172 | #ifdef HAVE_SGTTY | |
173 | { | |
174 | struct sgttyb sg; | |
175 | ||
176 | ioctl (remote_desc, TIOCGETP, &sg); | |
177 | sg.sg_flags = RAW; | |
178 | ioctl (remote_desc, TIOCSETP, &sg); | |
179 | } | |
180 | #endif | |
181 | ||
e641a1ca | 182 | fprintf (stderr, "Remote debugging using %s\n", name); |
b80864fb | 183 | #endif /* USE_WIN32API */ |
c906108c SS |
184 | } |
185 | else | |
186 | { | |
b80864fb DJ |
187 | #ifdef USE_WIN32API |
188 | static int winsock_initialized; | |
189 | #endif | |
c906108c SS |
190 | int port; |
191 | struct sockaddr_in sockaddr; | |
f450004a | 192 | socklen_t tmp; |
c906108c | 193 | int tmp_desc; |
2d717e4f | 194 | char *port_end; |
c906108c | 195 | |
2d717e4f DJ |
196 | port = strtoul (port_str + 1, &port_end, 10); |
197 | if (port_str[1] == '\0' || *port_end != '\0') | |
198 | fatal ("Bad port argument: %s", name); | |
c906108c | 199 | |
b80864fb DJ |
200 | #ifdef USE_WIN32API |
201 | if (!winsock_initialized) | |
202 | { | |
203 | WSADATA wsad; | |
204 | ||
205 | WSAStartup (MAKEWORD (1, 0), &wsad); | |
206 | winsock_initialized = 1; | |
207 | } | |
208 | #endif | |
209 | ||
210 | tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
c906108c SS |
211 | if (tmp_desc < 0) |
212 | perror_with_name ("Can't open socket"); | |
213 | ||
214 | /* Allow rapid reuse of this port. */ | |
215 | tmp = 1; | |
c5aa993b JM |
216 | setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, |
217 | sizeof (tmp)); | |
c906108c SS |
218 | |
219 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 220 | sockaddr.sin_port = htons (port); |
c906108c SS |
221 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
222 | ||
c5aa993b | 223 | if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) |
c906108c SS |
224 | || listen (tmp_desc, 1)) |
225 | perror_with_name ("Can't bind address"); | |
226 | ||
6f8486da DJ |
227 | /* If port is zero, a random port will be selected, and the |
228 | fprintf below needs to know what port was selected. */ | |
229 | if (port == 0) | |
230 | { | |
231 | socklen_t len = sizeof (sockaddr); | |
232 | if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0 | |
233 | || len < sizeof (sockaddr)) | |
234 | perror_with_name ("Can't determine port"); | |
235 | port = ntohs (sockaddr.sin_port); | |
236 | } | |
237 | ||
6910d122 | 238 | fprintf (stderr, "Listening on port %d\n", port); |
b80864fb | 239 | fflush (stderr); |
6910d122 | 240 | |
c906108c | 241 | tmp = sizeof (sockaddr); |
c5aa993b | 242 | remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp); |
c906108c SS |
243 | if (remote_desc == -1) |
244 | perror_with_name ("Accept failed"); | |
245 | ||
c906108c SS |
246 | /* Enable TCP keep alive process. */ |
247 | tmp = 1; | |
aa0403d9 PA |
248 | setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, |
249 | (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 SS |
279 | } |
280 | ||
281 | void | |
fba45db2 | 282 | remote_close (void) |
c906108c | 283 | { |
b80864fb DJ |
284 | #ifdef USE_WIN32API |
285 | closesocket (remote_desc); | |
286 | #else | |
c906108c | 287 | close (remote_desc); |
b80864fb | 288 | #endif |
c906108c SS |
289 | } |
290 | ||
291 | /* Convert hex digit A to a number. */ | |
292 | ||
293 | static int | |
fba45db2 | 294 | fromhex (int a) |
c906108c SS |
295 | { |
296 | if (a >= '0' && a <= '9') | |
297 | return a - '0'; | |
298 | else if (a >= 'a' && a <= 'f') | |
299 | return a - 'a' + 10; | |
300 | else | |
301 | error ("Reply contains invalid hex digit"); | |
0a30fbc4 | 302 | return 0; |
c906108c SS |
303 | } |
304 | ||
ce3a066d DJ |
305 | int |
306 | unhexify (char *bin, const char *hex, int count) | |
307 | { | |
308 | int i; | |
309 | ||
310 | for (i = 0; i < count; i++) | |
311 | { | |
312 | if (hex[0] == 0 || hex[1] == 0) | |
313 | { | |
314 | /* Hex string is short, or of uneven length. | |
315 | Return the count that has been converted so far. */ | |
316 | return i; | |
317 | } | |
318 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
319 | hex += 2; | |
320 | } | |
321 | return i; | |
322 | } | |
323 | ||
dae5f5cf | 324 | void |
2f2893d9 DJ |
325 | decode_address (CORE_ADDR *addrp, const char *start, int len) |
326 | { | |
327 | CORE_ADDR addr; | |
328 | char ch; | |
329 | int i; | |
330 | ||
331 | addr = 0; | |
332 | for (i = 0; i < len; i++) | |
333 | { | |
334 | ch = start[i]; | |
335 | addr = addr << 4; | |
336 | addr = addr | (fromhex (ch) & 0x0f); | |
337 | } | |
338 | *addrp = addr; | |
339 | } | |
340 | ||
89be2091 DJ |
341 | const char * |
342 | decode_address_to_semicolon (CORE_ADDR *addrp, const char *start) | |
343 | { | |
344 | const char *end; | |
345 | ||
346 | end = start; | |
347 | while (*end != '\0' && *end != ';') | |
348 | end++; | |
349 | ||
350 | decode_address (addrp, start, end - start); | |
351 | ||
352 | if (*end == ';') | |
353 | end++; | |
354 | return end; | |
355 | } | |
356 | ||
c906108c SS |
357 | /* Convert number NIB to a hex digit. */ |
358 | ||
359 | static int | |
fba45db2 | 360 | tohex (int nib) |
c906108c SS |
361 | { |
362 | if (nib < 10) | |
363 | return '0' + nib; | |
364 | else | |
365 | return 'a' + nib - 10; | |
366 | } | |
367 | ||
ce3a066d DJ |
368 | int |
369 | hexify (char *hex, const char *bin, int count) | |
370 | { | |
371 | int i; | |
372 | ||
373 | /* May use a length, or a nul-terminated string as input. */ | |
374 | if (count == 0) | |
375 | count = strlen (bin); | |
376 | ||
377 | for (i = 0; i < count; i++) | |
378 | { | |
379 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
380 | *hex++ = tohex (*bin++ & 0xf); | |
381 | } | |
382 | *hex = 0; | |
383 | return i; | |
384 | } | |
385 | ||
01f9e8fa DJ |
386 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
387 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
388 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
389 | (which may be more than *OUT_LEN due to escape characters). The | |
390 | total number of bytes in the output buffer will be at most | |
391 | OUT_MAXLEN. */ | |
392 | ||
393 | int | |
394 | remote_escape_output (const gdb_byte *buffer, int len, | |
395 | gdb_byte *out_buf, int *out_len, | |
396 | int out_maxlen) | |
397 | { | |
398 | int input_index, output_index; | |
399 | ||
400 | output_index = 0; | |
401 | for (input_index = 0; input_index < len; input_index++) | |
402 | { | |
403 | gdb_byte b = buffer[input_index]; | |
404 | ||
405 | if (b == '$' || b == '#' || b == '}' || b == '*') | |
406 | { | |
407 | /* These must be escaped. */ | |
408 | if (output_index + 2 > out_maxlen) | |
409 | break; | |
410 | out_buf[output_index++] = '}'; | |
411 | out_buf[output_index++] = b ^ 0x20; | |
412 | } | |
413 | else | |
414 | { | |
415 | if (output_index + 1 > out_maxlen) | |
416 | break; | |
417 | out_buf[output_index++] = b; | |
418 | } | |
419 | } | |
420 | ||
421 | *out_len = input_index; | |
422 | return output_index; | |
423 | } | |
424 | ||
425 | /* Convert BUFFER, escaped data LEN bytes long, into binary data | |
426 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
427 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
428 | ||
429 | This function reverses remote_escape_output. It allows more | |
430 | escaped characters than that function does, in particular because | |
431 | '*' must be escaped to avoid the run-length encoding processing | |
432 | in reading packets. */ | |
433 | ||
434 | static int | |
435 | remote_unescape_input (const gdb_byte *buffer, int len, | |
436 | gdb_byte *out_buf, int out_maxlen) | |
437 | { | |
438 | int input_index, output_index; | |
439 | int escaped; | |
440 | ||
441 | output_index = 0; | |
442 | escaped = 0; | |
443 | for (input_index = 0; input_index < len; input_index++) | |
444 | { | |
445 | gdb_byte b = buffer[input_index]; | |
446 | ||
447 | if (output_index + 1 > out_maxlen) | |
448 | error ("Received too much data from the target."); | |
449 | ||
450 | if (escaped) | |
451 | { | |
452 | out_buf[output_index++] = b ^ 0x20; | |
453 | escaped = 0; | |
454 | } | |
455 | else if (b == '}') | |
456 | escaped = 1; | |
457 | else | |
458 | out_buf[output_index++] = b; | |
459 | } | |
460 | ||
461 | if (escaped) | |
462 | error ("Unmatched escape character in target response."); | |
463 | ||
464 | return output_index; | |
465 | } | |
466 | ||
5ffff7c1 DJ |
467 | /* Look for a sequence of characters which can be run-length encoded. |
468 | If there are any, update *CSUM and *P. Otherwise, output the | |
469 | single character. Return the number of characters consumed. */ | |
470 | ||
471 | static int | |
472 | try_rle (char *buf, int remaining, unsigned char *csum, char **p) | |
473 | { | |
474 | int n; | |
475 | ||
476 | /* Always output the character. */ | |
477 | *csum += buf[0]; | |
478 | *(*p)++ = buf[0]; | |
479 | ||
480 | /* Don't go past '~'. */ | |
481 | if (remaining > 97) | |
482 | remaining = 97; | |
483 | ||
484 | for (n = 1; n < remaining; n++) | |
485 | if (buf[n] != buf[0]) | |
486 | break; | |
487 | ||
488 | /* N is the index of the first character not the same as buf[0]. | |
489 | buf[0] is counted twice, so by decrementing N, we get the number | |
490 | of characters the RLE sequence will replace. */ | |
491 | n--; | |
492 | ||
493 | if (n < 3) | |
494 | return 1; | |
495 | ||
496 | /* Skip the frame characters. The manual says to skip '+' and '-' | |
497 | also, but there's no reason to. Unfortunately these two unusable | |
498 | characters double the encoded length of a four byte zero | |
499 | value. */ | |
500 | while (n + 29 == '$' || n + 29 == '#') | |
501 | n--; | |
502 | ||
503 | *csum += '*'; | |
504 | *(*p)++ = '*'; | |
505 | *csum += n + 29; | |
506 | *(*p)++ = n + 29; | |
507 | ||
508 | return n + 1; | |
509 | } | |
510 | ||
c906108c | 511 | /* Send a packet to the remote machine, with error checking. |
01f9e8fa DJ |
512 | The data of the packet is in BUF, and the length of the |
513 | packet is in CNT. Returns >= 0 on success, -1 otherwise. */ | |
c906108c SS |
514 | |
515 | int | |
01f9e8fa | 516 | putpkt_binary (char *buf, int cnt) |
c906108c SS |
517 | { |
518 | int i; | |
519 | unsigned char csum = 0; | |
0a30fbc4 | 520 | char *buf2; |
c906108c | 521 | char buf3[1]; |
c906108c SS |
522 | char *p; |
523 | ||
0a30fbc4 DJ |
524 | buf2 = malloc (PBUFSIZ); |
525 | ||
c906108c SS |
526 | /* Copy the packet into buffer BUF2, encapsulating it |
527 | and giving it a checksum. */ | |
528 | ||
529 | p = buf2; | |
530 | *p++ = '$'; | |
531 | ||
5ffff7c1 DJ |
532 | for (i = 0; i < cnt;) |
533 | i += try_rle (buf + i, cnt - i, &csum, &p); | |
534 | ||
c906108c SS |
535 | *p++ = '#'; |
536 | *p++ = tohex ((csum >> 4) & 0xf); | |
537 | *p++ = tohex (csum & 0xf); | |
538 | ||
539 | *p = '\0'; | |
540 | ||
541 | /* Send it over and over until we get a positive ack. */ | |
542 | ||
543 | do | |
544 | { | |
545 | int cc; | |
546 | ||
0f48aa01 | 547 | if (write (remote_desc, buf2, p - buf2) != p - buf2) |
c906108c SS |
548 | { |
549 | perror ("putpkt(write)"); | |
f88c79e6 | 550 | free (buf2); |
c906108c SS |
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. */ | |
2d717e4f | 578 | if (buf3[0] == '\003' && current_inferior != NULL) |
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 | |
2d717e4f | 620 | if (cc != 1 || c != '\003' || current_inferior == NULL) |
cf30a8e1 | 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 | |
a20d5e98 | 648 | static void |
62ea82f5 DJ |
649 | unblock_async_io (void) |
650 | { | |
b80864fb | 651 | #ifndef USE_WIN32API |
62ea82f5 | 652 | sigset_t sigio_set; |
a20d5e98 | 653 | |
62ea82f5 DJ |
654 | sigemptyset (&sigio_set); |
655 | sigaddset (&sigio_set, SIGIO); | |
656 | sigprocmask (SIG_UNBLOCK, &sigio_set, NULL); | |
b80864fb | 657 | #endif |
62ea82f5 DJ |
658 | } |
659 | ||
fd500816 DJ |
660 | /* Current state of asynchronous I/O. */ |
661 | static int async_io_enabled; | |
662 | ||
663 | /* Enable asynchronous I/O. */ | |
c906108c | 664 | void |
fba45db2 | 665 | enable_async_io (void) |
c906108c | 666 | { |
fd500816 DJ |
667 | if (async_io_enabled) |
668 | return; | |
669 | ||
b80864fb | 670 | #ifndef USE_WIN32API |
c906108c | 671 | signal (SIGIO, input_interrupt); |
b80864fb | 672 | #endif |
fd500816 | 673 | async_io_enabled = 1; |
c906108c SS |
674 | } |
675 | ||
fd500816 | 676 | /* Disable asynchronous I/O. */ |
c906108c | 677 | void |
fba45db2 | 678 | disable_async_io (void) |
c906108c | 679 | { |
fd500816 DJ |
680 | if (!async_io_enabled) |
681 | return; | |
682 | ||
b80864fb | 683 | #ifndef USE_WIN32API |
c906108c | 684 | signal (SIGIO, SIG_IGN); |
b80864fb | 685 | #endif |
fd500816 | 686 | async_io_enabled = 0; |
c906108c SS |
687 | } |
688 | ||
a20d5e98 DJ |
689 | void |
690 | initialize_async_io (void) | |
691 | { | |
692 | /* Make sure that async I/O starts disabled. */ | |
693 | async_io_enabled = 1; | |
694 | disable_async_io (); | |
695 | ||
696 | /* Make sure the signal is unblocked. */ | |
697 | unblock_async_io (); | |
698 | } | |
699 | ||
c906108c SS |
700 | /* Returns next char from remote GDB. -1 if error. */ |
701 | ||
702 | static int | |
fba45db2 | 703 | readchar (void) |
c906108c | 704 | { |
01f9e8fa | 705 | static unsigned char buf[BUFSIZ]; |
c906108c | 706 | static int bufcnt = 0; |
01f9e8fa | 707 | static unsigned char *bufp; |
c906108c SS |
708 | |
709 | if (bufcnt-- > 0) | |
01f9e8fa | 710 | return *bufp++; |
c906108c | 711 | |
0f48aa01 | 712 | bufcnt = read (remote_desc, buf, sizeof (buf)); |
c906108c SS |
713 | |
714 | if (bufcnt <= 0) | |
715 | { | |
716 | if (bufcnt == 0) | |
717 | fprintf (stderr, "readchar: Got EOF\n"); | |
718 | else | |
719 | perror ("readchar"); | |
720 | ||
721 | return -1; | |
722 | } | |
723 | ||
724 | bufp = buf; | |
725 | bufcnt--; | |
b79d787e | 726 | return *bufp++; |
c906108c SS |
727 | } |
728 | ||
729 | /* Read a packet from the remote machine, with error checking, | |
730 | and store it in BUF. Returns length of packet, or negative if error. */ | |
731 | ||
732 | int | |
fba45db2 | 733 | getpkt (char *buf) |
c906108c SS |
734 | { |
735 | char *bp; | |
736 | unsigned char csum, c1, c2; | |
737 | int c; | |
738 | ||
739 | while (1) | |
740 | { | |
741 | csum = 0; | |
742 | ||
743 | while (1) | |
744 | { | |
745 | c = readchar (); | |
746 | if (c == '$') | |
747 | break; | |
748 | if (remote_debug) | |
0d62e5e8 DJ |
749 | { |
750 | fprintf (stderr, "[getpkt: discarding char '%c']\n", c); | |
751 | fflush (stderr); | |
752 | } | |
753 | ||
c906108c SS |
754 | if (c < 0) |
755 | return -1; | |
756 | } | |
757 | ||
758 | bp = buf; | |
759 | while (1) | |
760 | { | |
761 | c = readchar (); | |
762 | if (c < 0) | |
763 | return -1; | |
764 | if (c == '#') | |
765 | break; | |
766 | *bp++ = c; | |
767 | csum += c; | |
768 | } | |
769 | *bp = 0; | |
770 | ||
771 | c1 = fromhex (readchar ()); | |
772 | c2 = fromhex (readchar ()); | |
c5aa993b | 773 | |
c906108c SS |
774 | if (csum == (c1 << 4) + c2) |
775 | break; | |
776 | ||
777 | fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
778 | (c1 << 4) + c2, csum, buf); | |
0f48aa01 | 779 | write (remote_desc, "-", 1); |
c906108c SS |
780 | } |
781 | ||
782 | if (remote_debug) | |
0d62e5e8 DJ |
783 | { |
784 | fprintf (stderr, "getpkt (\"%s\"); [sending ack] \n", buf); | |
785 | fflush (stderr); | |
786 | } | |
c906108c | 787 | |
0f48aa01 | 788 | write (remote_desc, "+", 1); |
c906108c SS |
789 | |
790 | if (remote_debug) | |
0d62e5e8 DJ |
791 | { |
792 | fprintf (stderr, "[sent ack]\n"); | |
793 | fflush (stderr); | |
794 | } | |
795 | ||
c906108c SS |
796 | return bp - buf; |
797 | } | |
798 | ||
799 | void | |
fba45db2 | 800 | write_ok (char *buf) |
c906108c SS |
801 | { |
802 | buf[0] = 'O'; | |
803 | buf[1] = 'K'; | |
804 | buf[2] = '\0'; | |
805 | } | |
806 | ||
807 | void | |
fba45db2 | 808 | write_enn (char *buf) |
c906108c | 809 | { |
c89dc5d4 | 810 | /* Some day, we should define the meanings of the error codes... */ |
c906108c | 811 | buf[0] = 'E'; |
c89dc5d4 DJ |
812 | buf[1] = '0'; |
813 | buf[2] = '1'; | |
c906108c SS |
814 | buf[3] = '\0'; |
815 | } | |
816 | ||
817 | void | |
f450004a | 818 | convert_int_to_ascii (unsigned char *from, char *to, int n) |
c906108c SS |
819 | { |
820 | int nib; | |
f450004a | 821 | int ch; |
c906108c SS |
822 | while (n--) |
823 | { | |
824 | ch = *from++; | |
825 | nib = ((ch & 0xf0) >> 4) & 0x0f; | |
826 | *to++ = tohex (nib); | |
827 | nib = ch & 0x0f; | |
828 | *to++ = tohex (nib); | |
829 | } | |
830 | *to++ = 0; | |
831 | } | |
832 | ||
833 | ||
834 | void | |
f450004a | 835 | convert_ascii_to_int (char *from, unsigned char *to, int n) |
c906108c SS |
836 | { |
837 | int nib1, nib2; | |
838 | while (n--) | |
839 | { | |
840 | nib1 = fromhex (*from++); | |
841 | nib2 = fromhex (*from++); | |
842 | *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f); | |
843 | } | |
844 | } | |
845 | ||
846 | static char * | |
fba45db2 | 847 | outreg (int regno, char *buf) |
c906108c | 848 | { |
5c44784c JM |
849 | if ((regno >> 12) != 0) |
850 | *buf++ = tohex ((regno >> 12) & 0xf); | |
851 | if ((regno >> 8) != 0) | |
852 | *buf++ = tohex ((regno >> 8) & 0xf); | |
853 | *buf++ = tohex ((regno >> 4) & 0xf); | |
c906108c SS |
854 | *buf++ = tohex (regno & 0xf); |
855 | *buf++ = ':'; | |
0d62e5e8 DJ |
856 | collect_register_as_string (regno, buf); |
857 | buf += 2 * register_size (regno); | |
c906108c SS |
858 | *buf++ = ';'; |
859 | ||
860 | return buf; | |
861 | } | |
862 | ||
0d62e5e8 DJ |
863 | void |
864 | new_thread_notify (int id) | |
865 | { | |
866 | char own_buf[256]; | |
867 | ||
868 | /* The `n' response is not yet part of the remote protocol. Do nothing. */ | |
869 | if (1) | |
870 | return; | |
871 | ||
872 | if (server_waiting == 0) | |
873 | return; | |
874 | ||
875 | sprintf (own_buf, "n%x", id); | |
876 | disable_async_io (); | |
877 | putpkt (own_buf); | |
878 | enable_async_io (); | |
879 | } | |
880 | ||
881 | void | |
882 | dead_thread_notify (int id) | |
883 | { | |
884 | char own_buf[256]; | |
885 | ||
886 | /* The `x' response is not yet part of the remote protocol. Do nothing. */ | |
887 | if (1) | |
888 | return; | |
889 | ||
890 | sprintf (own_buf, "x%x", id); | |
891 | disable_async_io (); | |
892 | putpkt (own_buf); | |
893 | enable_async_io (); | |
894 | } | |
895 | ||
c906108c | 896 | void |
b80864fb | 897 | prepare_resume_reply (char *buf, char status, unsigned char sig) |
c906108c | 898 | { |
b80864fb | 899 | int nib; |
c906108c SS |
900 | |
901 | *buf++ = status; | |
902 | ||
0e98d0a7 | 903 | nib = ((sig & 0xf0) >> 4); |
c906108c | 904 | *buf++ = tohex (nib); |
0e98d0a7 | 905 | nib = sig & 0x0f; |
c906108c SS |
906 | *buf++ = tohex (nib); |
907 | ||
908 | if (status == 'T') | |
909 | { | |
0a30fbc4 | 910 | const char **regp = gdbserver_expedite_regs; |
e013ee27 OF |
911 | |
912 | if (the_target->stopped_by_watchpoint != NULL | |
913 | && (*the_target->stopped_by_watchpoint) ()) | |
914 | { | |
915 | CORE_ADDR addr; | |
916 | int i; | |
917 | ||
918 | strncpy (buf, "watch:", 6); | |
919 | buf += 6; | |
920 | ||
921 | addr = (*the_target->stopped_data_address) (); | |
922 | ||
923 | /* Convert each byte of the address into two hexadecimal chars. | |
924 | Note that we take sizeof (void *) instead of sizeof (addr); | |
925 | this is to avoid sending a 64-bit address to a 32-bit GDB. */ | |
926 | for (i = sizeof (void *) * 2; i > 0; i--) | |
927 | { | |
928 | *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf); | |
929 | } | |
930 | *buf++ = ';'; | |
931 | } | |
932 | ||
0a30fbc4 | 933 | while (*regp) |
5c44784c | 934 | { |
0a30fbc4 DJ |
935 | buf = outreg (find_regno (*regp), buf); |
936 | regp ++; | |
5c44784c | 937 | } |
c906108c | 938 | |
0d62e5e8 DJ |
939 | /* Formerly, if the debugger had not used any thread features we would not |
940 | burden it with a thread status response. This was for the benefit of | |
941 | GDB 4.13 and older. However, in recent GDB versions the check | |
942 | (``if (cont_thread != 0)'') does not have the desired effect because of | |
943 | sillyness in the way that the remote protocol handles specifying a thread. | |
944 | Since thread support relies on qSymbol support anyway, assume GDB can handle | |
945 | threads. */ | |
946 | ||
947 | if (using_threads) | |
c906108c | 948 | { |
b92a518e DJ |
949 | unsigned int gdb_id_from_wait; |
950 | ||
0d62e5e8 DJ |
951 | /* FIXME right place to set this? */ |
952 | thread_from_wait = ((struct inferior_list_entry *)current_inferior)->id; | |
b92a518e | 953 | gdb_id_from_wait = thread_to_gdb_id (current_inferior); |
a06660f7 | 954 | |
0d62e5e8 | 955 | if (debug_threads) |
a1928bad | 956 | fprintf (stderr, "Writing resume reply for %ld\n\n", thread_from_wait); |
89a208da DJ |
957 | /* This if (1) ought to be unnecessary. But remote_wait in GDB |
958 | will claim this event belongs to inferior_ptid if we do not | |
959 | specify a thread, and there's no way for gdbserver to know | |
960 | what inferior_ptid is. */ | |
961 | if (1 || old_thread_from_wait != thread_from_wait) | |
c906108c | 962 | { |
0d62e5e8 | 963 | general_thread = thread_from_wait; |
a06660f7 | 964 | sprintf (buf, "thread:%x;", gdb_id_from_wait); |
c906108c SS |
965 | buf += strlen (buf); |
966 | old_thread_from_wait = thread_from_wait; | |
967 | } | |
968 | } | |
255e7678 DJ |
969 | |
970 | if (dlls_changed) | |
971 | { | |
972 | strcpy (buf, "library:;"); | |
973 | buf += strlen (buf); | |
974 | dlls_changed = 0; | |
975 | } | |
c906108c SS |
976 | } |
977 | /* For W and X, we're done. */ | |
978 | *buf++ = 0; | |
979 | } | |
980 | ||
981 | void | |
fba45db2 | 982 | decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr) |
c906108c SS |
983 | { |
984 | int i = 0, j = 0; | |
985 | char ch; | |
986 | *mem_addr_ptr = *len_ptr = 0; | |
987 | ||
988 | while ((ch = from[i++]) != ',') | |
989 | { | |
990 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
991 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
992 | } | |
993 | ||
994 | for (j = 0; j < 4; j++) | |
995 | { | |
996 | if ((ch = from[i++]) == 0) | |
997 | break; | |
998 | *len_ptr = *len_ptr << 4; | |
999 | *len_ptr |= fromhex (ch) & 0x0f; | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | void | |
fba45db2 | 1004 | decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, |
f450004a | 1005 | unsigned char *to) |
c906108c SS |
1006 | { |
1007 | int i = 0; | |
1008 | char ch; | |
1009 | *mem_addr_ptr = *len_ptr = 0; | |
1010 | ||
1011 | while ((ch = from[i++]) != ',') | |
1012 | { | |
1013 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1014 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1015 | } | |
1016 | ||
1017 | while ((ch = from[i++]) != ':') | |
1018 | { | |
1019 | *len_ptr = *len_ptr << 4; | |
1020 | *len_ptr |= fromhex (ch) & 0x0f; | |
1021 | } | |
1022 | ||
1023 | convert_ascii_to_int (&from[i++], to, *len_ptr); | |
1024 | } | |
2f2893d9 | 1025 | |
01f9e8fa DJ |
1026 | int |
1027 | decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr, | |
1028 | unsigned int *len_ptr, unsigned char *to) | |
1029 | { | |
1030 | int i = 0; | |
1031 | char ch; | |
1032 | *mem_addr_ptr = *len_ptr = 0; | |
1033 | ||
1034 | while ((ch = from[i++]) != ',') | |
1035 | { | |
1036 | *mem_addr_ptr = *mem_addr_ptr << 4; | |
1037 | *mem_addr_ptr |= fromhex (ch) & 0x0f; | |
1038 | } | |
1039 | ||
1040 | while ((ch = from[i++]) != ':') | |
1041 | { | |
1042 | *len_ptr = *len_ptr << 4; | |
1043 | *len_ptr |= fromhex (ch) & 0x0f; | |
1044 | } | |
1045 | ||
1046 | if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i, | |
1047 | to, *len_ptr) != *len_ptr) | |
1048 | return -1; | |
1049 | ||
1050 | return 0; | |
1051 | } | |
1052 | ||
0e7f50da UW |
1053 | /* Decode a qXfer write request. */ |
1054 | int | |
1055 | decode_xfer_write (char *buf, int packet_len, char **annex, CORE_ADDR *offset, | |
1056 | unsigned int *len, unsigned char *data) | |
1057 | { | |
1058 | char ch; | |
1059 | ||
1060 | /* Extract and NUL-terminate the annex. */ | |
1061 | *annex = buf; | |
1062 | while (*buf && *buf != ':') | |
1063 | buf++; | |
1064 | if (*buf == '\0') | |
1065 | return -1; | |
1066 | *buf++ = 0; | |
1067 | ||
1068 | /* Extract the offset. */ | |
1069 | *offset = 0; | |
1070 | while ((ch = *buf++) != ':') | |
1071 | { | |
1072 | *offset = *offset << 4; | |
1073 | *offset |= fromhex (ch) & 0x0f; | |
1074 | } | |
1075 | ||
1076 | /* Get encoded data. */ | |
1077 | packet_len -= buf - *annex; | |
1078 | *len = remote_unescape_input ((const gdb_byte *) buf, packet_len, | |
1079 | data, packet_len); | |
1080 | return 0; | |
1081 | } | |
1082 | ||
08388c79 DE |
1083 | /* Decode the parameters of a qSearch:memory packet. */ |
1084 | ||
1085 | int | |
1086 | decode_search_memory_packet (const char *buf, int packet_len, | |
1087 | CORE_ADDR *start_addrp, | |
1088 | CORE_ADDR *search_space_lenp, | |
1089 | gdb_byte *pattern, unsigned int *pattern_lenp) | |
1090 | { | |
1091 | const char *p = buf; | |
1092 | ||
1093 | p = decode_address_to_semicolon (start_addrp, p); | |
1094 | p = decode_address_to_semicolon (search_space_lenp, p); | |
1095 | packet_len -= p - buf; | |
1096 | *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len, | |
1097 | pattern, packet_len); | |
1098 | return 0; | |
1099 | } | |
1100 | ||
fd500816 DJ |
1101 | /* Ask GDB for the address of NAME, and return it in ADDRP if found. |
1102 | Returns 1 if the symbol is found, 0 if it is not, -1 on error. */ | |
1103 | ||
2f2893d9 DJ |
1104 | int |
1105 | look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
1106 | { | |
1107 | char own_buf[266], *p, *q; | |
1108 | int len; | |
fd500816 DJ |
1109 | struct sym_cache *sym; |
1110 | ||
1111 | /* Check the cache first. */ | |
1112 | for (sym = symbol_cache; sym; sym = sym->next) | |
1113 | if (strcmp (name, sym->name) == 0) | |
1114 | { | |
1115 | *addrp = sym->addr; | |
1116 | return 1; | |
1117 | } | |
2f2893d9 | 1118 | |
ea025f5f DJ |
1119 | /* If we've passed the call to thread_db_look_up_symbols, then |
1120 | anything not in the cache must not exist; we're not interested | |
1121 | in any libraries loaded after that point, only in symbols in | |
1122 | libpthread.so. It might not be an appropriate time to look | |
1123 | up a symbol, e.g. while we're trying to fetch registers. */ | |
1124 | if (all_symbols_looked_up) | |
1125 | return 0; | |
1126 | ||
2f2893d9 DJ |
1127 | /* Send the request. */ |
1128 | strcpy (own_buf, "qSymbol:"); | |
1129 | hexify (own_buf + strlen ("qSymbol:"), name, strlen (name)); | |
1130 | if (putpkt (own_buf) < 0) | |
1131 | return -1; | |
1132 | ||
1133 | /* FIXME: Eventually add buffer overflow checking (to getpkt?) */ | |
1134 | len = getpkt (own_buf); | |
1135 | if (len < 0) | |
1136 | return -1; | |
1137 | ||
2bbe3cc1 DJ |
1138 | /* We ought to handle pretty much any packet at this point while we |
1139 | wait for the qSymbol "response". That requires re-entering the | |
1140 | main loop. For now, this is an adequate approximation; allow | |
1141 | GDB to read from memory while it figures out the address of the | |
1142 | symbol. */ | |
1143 | while (own_buf[0] == 'm') | |
1144 | { | |
1145 | CORE_ADDR mem_addr; | |
1146 | unsigned char *mem_buf; | |
1147 | unsigned int mem_len; | |
1148 | ||
1149 | decode_m_packet (&own_buf[1], &mem_addr, &mem_len); | |
1150 | mem_buf = malloc (mem_len); | |
1151 | if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) | |
1152 | convert_int_to_ascii (mem_buf, own_buf, mem_len); | |
1153 | else | |
1154 | write_enn (own_buf); | |
1155 | free (mem_buf); | |
1156 | if (putpkt (own_buf) < 0) | |
1157 | return -1; | |
1158 | len = getpkt (own_buf); | |
1159 | if (len < 0) | |
1160 | return -1; | |
1161 | } | |
1162 | ||
2f2893d9 DJ |
1163 | if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0) |
1164 | { | |
2bbe3cc1 | 1165 | warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf); |
2f2893d9 DJ |
1166 | return -1; |
1167 | } | |
1168 | ||
1169 | p = own_buf + strlen ("qSymbol:"); | |
1170 | q = p; | |
1171 | while (*q && *q != ':') | |
1172 | q++; | |
1173 | ||
1174 | /* Make sure we found a value for the symbol. */ | |
1175 | if (p == q || *q == '\0') | |
1176 | return 0; | |
1177 | ||
1178 | decode_address (addrp, p, q - p); | |
fd500816 DJ |
1179 | |
1180 | /* Save the symbol in our cache. */ | |
1181 | sym = malloc (sizeof (*sym)); | |
1182 | sym->name = strdup (name); | |
1183 | sym->addr = *addrp; | |
1184 | sym->next = symbol_cache; | |
1185 | symbol_cache = sym; | |
1186 | ||
2f2893d9 DJ |
1187 | return 1; |
1188 | } | |
c74d0ad8 DJ |
1189 | |
1190 | void | |
bce7165d | 1191 | monitor_output (const char *msg) |
c74d0ad8 DJ |
1192 | { |
1193 | char *buf = malloc (strlen (msg) * 2 + 2); | |
1194 | ||
1195 | buf[0] = 'O'; | |
1196 | hexify (buf + 1, msg, 0); | |
1197 | ||
1198 | putpkt (buf); | |
1199 | free (buf); | |
1200 | } | |
255e7678 DJ |
1201 | |
1202 | /* Return a malloc allocated string with special characters from TEXT | |
1203 | replaced by entity references. */ | |
1204 | ||
1205 | char * | |
1206 | xml_escape_text (const char *text) | |
1207 | { | |
1208 | char *result; | |
1209 | int i, special; | |
1210 | ||
1211 | /* Compute the length of the result. */ | |
1212 | for (i = 0, special = 0; text[i] != '\0'; i++) | |
1213 | switch (text[i]) | |
1214 | { | |
1215 | case '\'': | |
1216 | case '\"': | |
1217 | special += 5; | |
1218 | break; | |
1219 | case '&': | |
1220 | special += 4; | |
1221 | break; | |
1222 | case '<': | |
1223 | case '>': | |
1224 | special += 3; | |
1225 | break; | |
1226 | default: | |
1227 | break; | |
1228 | } | |
1229 | ||
1230 | /* Expand the result. */ | |
1231 | result = malloc (i + special + 1); | |
1232 | for (i = 0, special = 0; text[i] != '\0'; i++) | |
1233 | switch (text[i]) | |
1234 | { | |
1235 | case '\'': | |
1236 | strcpy (result + i + special, "'"); | |
1237 | special += 5; | |
1238 | break; | |
1239 | case '\"': | |
1240 | strcpy (result + i + special, """); | |
1241 | special += 5; | |
1242 | break; | |
1243 | case '&': | |
1244 | strcpy (result + i + special, "&"); | |
1245 | special += 4; | |
1246 | break; | |
1247 | case '<': | |
1248 | strcpy (result + i + special, "<"); | |
1249 | special += 3; | |
1250 | break; | |
1251 | case '>': | |
1252 | strcpy (result + i + special, ">"); | |
1253 | special += 3; | |
1254 | break; | |
1255 | default: | |
1256 | result[i + special] = text[i]; | |
1257 | break; | |
1258 | } | |
1259 | result[i + special] = '\0'; | |
1260 | ||
1261 | return result; | |
1262 | } |