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