c87751664a2eb3e69a1952d8c5813c85aa5f2676
1 /* Remote utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <sys/ioctl.h>
32 #include <netinet/in.h>
35 #include <sys/socket.h>
40 #if HAVE_NETINET_TCP_H
41 #include <netinet/tcp.h>
44 #include <sys/ioctl.h>
57 #include <arpa/inet.h>
68 #ifndef HAVE_SOCKLEN_T
69 typedef int socklen_t
;
73 # define INVALID_DESCRIPTOR INVALID_SOCKET
75 # define INVALID_DESCRIPTOR -1
78 /* A cache entry for a successfully looked-up symbol. */
83 struct sym_cache
*next
;
86 /* The symbol cache. */
87 static struct sym_cache
*symbol_cache
;
89 /* If this flag has been set, assume cache misses are
91 int all_symbols_looked_up
;
94 struct ui_file
*gdb_stdlog
;
96 static int remote_desc
= INVALID_DESCRIPTOR
;
98 /* FIXME headerize? */
99 extern int using_threads
;
100 extern int debug_threads
;
102 /* If true, then GDB has requested noack mode. */
104 /* If true, then we tell GDB to use noack mode by default. */
105 int transport_is_reliable
= 0;
108 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
109 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
112 /* Open a connection to a remote debugger.
113 NAME is the filename used for communication. */
116 remote_open (char *name
)
118 #if defined(F_SETFL) && defined (FASYNC)
119 int save_fcntl_flags
;
123 port_str
= strchr (name
, ':');
124 if (port_str
== NULL
)
127 error ("Only <host>:<port> is supported on this platform.");
131 if (stat (name
, &statbuf
) == 0
132 && (S_ISCHR (statbuf
.st_mode
) || S_ISFIFO (statbuf
.st_mode
)))
133 remote_desc
= open (name
, O_RDWR
);
141 perror_with_name ("Could not open remote device");
145 struct termios termios
;
146 tcgetattr (remote_desc
, &termios
);
151 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
152 termios
.c_cflag
|= CLOCAL
| CS8
;
153 termios
.c_cc
[VMIN
] = 1;
154 termios
.c_cc
[VTIME
] = 0;
156 tcsetattr (remote_desc
, TCSANOW
, &termios
);
162 struct termio termio
;
163 ioctl (remote_desc
, TCGETA
, &termio
);
168 termio
.c_cflag
&= ~(CSIZE
| PARENB
);
169 termio
.c_cflag
|= CLOCAL
| CS8
;
170 termio
.c_cc
[VMIN
] = 1;
171 termio
.c_cc
[VTIME
] = 0;
173 ioctl (remote_desc
, TCSETA
, &termio
);
181 ioctl (remote_desc
, TIOCGETP
, &sg
);
183 ioctl (remote_desc
, TIOCSETP
, &sg
);
187 fprintf (stderr
, "Remote debugging using %s\n", name
);
188 #endif /* USE_WIN32API */
190 transport_is_reliable
= 0;
195 static int winsock_initialized
;
198 struct sockaddr_in sockaddr
;
203 port
= strtoul (port_str
+ 1, &port_end
, 10);
204 if (port_str
[1] == '\0' || *port_end
!= '\0')
205 fatal ("Bad port argument: %s", name
);
208 if (!winsock_initialized
)
212 WSAStartup (MAKEWORD (1, 0), &wsad
);
213 winsock_initialized
= 1;
217 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
219 perror_with_name ("Can't open socket");
221 /* Allow rapid reuse of this port. */
223 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
226 sockaddr
.sin_family
= PF_INET
;
227 sockaddr
.sin_port
= htons (port
);
228 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
230 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
231 || listen (tmp_desc
, 1))
232 perror_with_name ("Can't bind address");
234 /* If port is zero, a random port will be selected, and the
235 fprintf below needs to know what port was selected. */
238 socklen_t len
= sizeof (sockaddr
);
239 if (getsockname (tmp_desc
, (struct sockaddr
*) &sockaddr
, &len
) < 0
240 || len
< sizeof (sockaddr
))
241 perror_with_name ("Can't determine port");
242 port
= ntohs (sockaddr
.sin_port
);
245 fprintf (stderr
, "Listening on port %d\n", port
);
248 tmp
= sizeof (sockaddr
);
249 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
250 if (remote_desc
== -1)
251 perror_with_name ("Accept failed");
253 /* Enable TCP keep alive process. */
255 setsockopt (remote_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
256 (char *) &tmp
, sizeof (tmp
));
258 /* Tell TCP not to delay small packets. This greatly speeds up
259 interactive response. */
261 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
262 (char *) &tmp
, sizeof (tmp
));
266 close (tmp_desc
); /* No longer need this */
268 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbserver simply
269 exits when the remote side dies. */
271 closesocket (tmp_desc
); /* No longer need this */
274 /* Convert IP address to string. */
275 fprintf (stderr
, "Remote debugging from host %s\n",
276 inet_ntoa (sockaddr
.sin_addr
));
278 transport_is_reliable
= 1;
281 #if defined(F_SETFL) && defined (FASYNC)
282 save_fcntl_flags
= fcntl (remote_desc
, F_GETFL
, 0);
283 fcntl (remote_desc
, F_SETFL
, save_fcntl_flags
| FASYNC
);
284 #if defined (F_SETOWN)
285 fcntl (remote_desc
, F_SETOWN
, getpid ());
294 closesocket (remote_desc
);
300 /* Convert hex digit A to a number. */
305 if (a
>= '0' && a
<= '9')
307 else if (a
>= 'a' && a
<= 'f')
310 error ("Reply contains invalid hex digit");
315 unhexify (char *bin
, const char *hex
, int count
)
319 for (i
= 0; i
< count
; i
++)
321 if (hex
[0] == 0 || hex
[1] == 0)
323 /* Hex string is short, or of uneven length.
324 Return the count that has been converted so far. */
327 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
334 decode_address (CORE_ADDR
*addrp
, const char *start
, int len
)
341 for (i
= 0; i
< len
; i
++)
345 addr
= addr
| (fromhex (ch
) & 0x0f);
351 decode_address_to_semicolon (CORE_ADDR
*addrp
, const char *start
)
356 while (*end
!= '\0' && *end
!= ';')
359 decode_address (addrp
, start
, end
- start
);
366 /* Convert number NIB to a hex digit. */
374 return 'a' + nib
- 10;
378 hexify (char *hex
, const char *bin
, int count
)
382 /* May use a length, or a nul-terminated string as input. */
384 count
= strlen (bin
);
386 for (i
= 0; i
< count
; i
++)
388 *hex
++ = tohex ((*bin
>> 4) & 0xf);
389 *hex
++ = tohex (*bin
++ & 0xf);
395 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
396 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
397 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
398 (which may be more than *OUT_LEN due to escape characters). The
399 total number of bytes in the output buffer will be at most
403 remote_escape_output (const gdb_byte
*buffer
, int len
,
404 gdb_byte
*out_buf
, int *out_len
,
407 int input_index
, output_index
;
410 for (input_index
= 0; input_index
< len
; input_index
++)
412 gdb_byte b
= buffer
[input_index
];
414 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
416 /* These must be escaped. */
417 if (output_index
+ 2 > out_maxlen
)
419 out_buf
[output_index
++] = '}';
420 out_buf
[output_index
++] = b
^ 0x20;
424 if (output_index
+ 1 > out_maxlen
)
426 out_buf
[output_index
++] = b
;
430 *out_len
= input_index
;
434 /* Convert BUFFER, escaped data LEN bytes long, into binary data
435 in OUT_BUF. Return the number of bytes written to OUT_BUF.
436 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
438 This function reverses remote_escape_output. It allows more
439 escaped characters than that function does, in particular because
440 '*' must be escaped to avoid the run-length encoding processing
441 in reading packets. */
444 remote_unescape_input (const gdb_byte
*buffer
, int len
,
445 gdb_byte
*out_buf
, int out_maxlen
)
447 int input_index
, output_index
;
452 for (input_index
= 0; input_index
< len
; input_index
++)
454 gdb_byte b
= buffer
[input_index
];
456 if (output_index
+ 1 > out_maxlen
)
457 error ("Received too much data from the target.");
461 out_buf
[output_index
++] = b
^ 0x20;
467 out_buf
[output_index
++] = b
;
471 error ("Unmatched escape character in target response.");
476 /* Look for a sequence of characters which can be run-length encoded.
477 If there are any, update *CSUM and *P. Otherwise, output the
478 single character. Return the number of characters consumed. */
481 try_rle (char *buf
, int remaining
, unsigned char *csum
, char **p
)
485 /* Always output the character. */
489 /* Don't go past '~'. */
493 for (n
= 1; n
< remaining
; n
++)
494 if (buf
[n
] != buf
[0])
497 /* N is the index of the first character not the same as buf[0].
498 buf[0] is counted twice, so by decrementing N, we get the number
499 of characters the RLE sequence will replace. */
505 /* Skip the frame characters. The manual says to skip '+' and '-'
506 also, but there's no reason to. Unfortunately these two unusable
507 characters double the encoded length of a four byte zero
509 while (n
+ 29 == '$' || n
+ 29 == '#')
520 /* Send a packet to the remote machine, with error checking.
521 The data of the packet is in BUF, and the length of the
522 packet is in CNT. Returns >= 0 on success, -1 otherwise. */
525 putpkt_binary (char *buf
, int cnt
)
528 unsigned char csum
= 0;
533 buf2
= xmalloc (PBUFSIZ
);
535 /* Copy the packet into buffer BUF2, encapsulating it
536 and giving it a checksum. */
541 for (i
= 0; i
< cnt
;)
542 i
+= try_rle (buf
+ i
, cnt
- i
, &csum
, &p
);
545 *p
++ = tohex ((csum
>> 4) & 0xf);
546 *p
++ = tohex (csum
& 0xf);
550 /* Send it over and over until we get a positive ack. */
556 if (write (remote_desc
, buf2
, p
- buf2
) != p
- buf2
)
558 perror ("putpkt(write)");
565 /* Don't expect an ack then. */
568 fprintf (stderr
, "putpkt (\"%s\"); [noack mode]\n", buf2
);
576 fprintf (stderr
, "putpkt (\"%s\"); [looking for ack]\n", buf2
);
579 cc
= read (remote_desc
, buf3
, 1);
582 fprintf (stderr
, "[received '%c' (0x%x)]\n", buf3
[0], buf3
[0]);
589 fprintf (stderr
, "putpkt(read): Got EOF\n");
591 perror ("putpkt(read)");
597 /* Check for an input interrupt while we're here. */
598 if (buf3
[0] == '\003' && current_inferior
!= NULL
)
599 (*the_target
->request_interrupt
) ();
601 while (buf3
[0] != '+');
604 return 1; /* Success! */
607 /* Send a packet to the remote machine, with error checking. The data
608 of the packet is in BUF, and the packet should be a NUL-terminated
609 string. Returns >= 0 on success, -1 otherwise. */
614 return putpkt_binary (buf
, strlen (buf
));
617 /* Come here when we get an input interrupt from the remote side. This
618 interrupt should only be active while we are waiting for the child to do
619 something. About the only thing that should come through is a ^C, which
620 will cause us to request child interruption. */
623 input_interrupt (int unused
)
626 struct timeval immediate
= { 0, 0 };
628 /* Protect against spurious interrupts. This has been observed to
629 be a problem under NetBSD 1.4 and 1.5. */
632 FD_SET (remote_desc
, &readset
);
633 if (select (remote_desc
+ 1, &readset
, 0, 0, &immediate
) > 0)
638 cc
= read (remote_desc
, &c
, 1);
640 if (cc
!= 1 || c
!= '\003' || current_inferior
== NULL
)
642 fprintf (stderr
, "input_interrupt, count = %d c = %d ('%c')\n",
647 (*the_target
->request_interrupt
) ();
651 /* Check if the remote side sent us an interrupt request (^C). */
653 check_remote_input_interrupt_request (void)
655 /* This function may be called before establishing communications,
656 therefore we need to validate the remote descriptor. */
658 if (remote_desc
== INVALID_DESCRIPTOR
)
664 /* Asynchronous I/O support. SIGIO must be enabled when waiting, in order to
665 accept Control-C from the client, and must be disabled when talking to
669 unblock_async_io (void)
674 sigemptyset (&sigio_set
);
675 sigaddset (&sigio_set
, SIGIO
);
676 sigprocmask (SIG_UNBLOCK
, &sigio_set
, NULL
);
680 /* Current state of asynchronous I/O. */
681 static int async_io_enabled
;
683 /* Enable asynchronous I/O. */
685 enable_async_io (void)
687 if (async_io_enabled
)
691 signal (SIGIO
, input_interrupt
);
693 async_io_enabled
= 1;
696 /* Disable asynchronous I/O. */
698 disable_async_io (void)
700 if (!async_io_enabled
)
704 signal (SIGIO
, SIG_IGN
);
706 async_io_enabled
= 0;
710 initialize_async_io (void)
712 /* Make sure that async I/O starts disabled. */
713 async_io_enabled
= 1;
716 /* Make sure the signal is unblocked. */
720 /* Returns next char from remote GDB. -1 if error. */
725 static unsigned char buf
[BUFSIZ
];
726 static int bufcnt
= 0;
727 static unsigned char *bufp
;
732 bufcnt
= read (remote_desc
, buf
, sizeof (buf
));
737 fprintf (stderr
, "readchar: Got EOF\n");
749 /* Read a packet from the remote machine, with error checking,
750 and store it in BUF. Returns length of packet, or negative if error. */
756 unsigned char csum
, c1
, c2
;
770 fprintf (stderr
, "[getpkt: discarding char '%c']\n", c
);
791 c1
= fromhex (readchar ());
792 c2
= fromhex (readchar ());
794 if (csum
== (c1
<< 4) + c2
)
799 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s [no-ack-mode, Bad medium?]\n",
800 (c1
<< 4) + c2
, csum
, buf
);
801 /* Not much we can do, GDB wasn't expecting an ack/nac. */
805 fprintf (stderr
, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
806 (c1
<< 4) + c2
, csum
, buf
);
807 write (remote_desc
, "-", 1);
814 fprintf (stderr
, "getpkt (\"%s\"); [sending ack] \n", buf
);
818 write (remote_desc
, "+", 1);
822 fprintf (stderr
, "[sent ack]\n");
830 fprintf (stderr
, "getpkt (\"%s\"); [no ack sent] \n", buf
);
847 write_enn (char *buf
)
849 /* Some day, we should define the meanings of the error codes... */
857 convert_int_to_ascii (unsigned char *from
, char *to
, int n
)
864 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
874 convert_ascii_to_int (char *from
, unsigned char *to
, int n
)
879 nib1
= fromhex (*from
++);
880 nib2
= fromhex (*from
++);
881 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
886 outreg (int regno
, char *buf
)
888 if ((regno
>> 12) != 0)
889 *buf
++ = tohex ((regno
>> 12) & 0xf);
890 if ((regno
>> 8) != 0)
891 *buf
++ = tohex ((regno
>> 8) & 0xf);
892 *buf
++ = tohex ((regno
>> 4) & 0xf);
893 *buf
++ = tohex (regno
& 0xf);
895 collect_register_as_string (regno
, buf
);
896 buf
+= 2 * register_size (regno
);
903 new_thread_notify (int id
)
907 /* The `n' response is not yet part of the remote protocol. Do nothing. */
911 if (server_waiting
== 0)
914 sprintf (own_buf
, "n%x", id
);
921 dead_thread_notify (int id
)
925 /* The `x' response is not yet part of the remote protocol. Do nothing. */
929 sprintf (own_buf
, "x%x", id
);
936 prepare_resume_reply (char *buf
, char status
, unsigned char sig
)
942 nib
= ((sig
& 0xf0) >> 4);
943 *buf
++ = tohex (nib
);
945 *buf
++ = tohex (nib
);
949 const char **regp
= gdbserver_expedite_regs
;
951 if (the_target
->stopped_by_watchpoint
!= NULL
952 && (*the_target
->stopped_by_watchpoint
) ())
957 strncpy (buf
, "watch:", 6);
960 addr
= (*the_target
->stopped_data_address
) ();
962 /* Convert each byte of the address into two hexadecimal chars.
963 Note that we take sizeof (void *) instead of sizeof (addr);
964 this is to avoid sending a 64-bit address to a 32-bit GDB. */
965 for (i
= sizeof (void *) * 2; i
> 0; i
--)
967 *buf
++ = tohex ((addr
>> (i
- 1) * 4) & 0xf);
974 buf
= outreg (find_regno (*regp
), buf
);
978 /* Formerly, if the debugger had not used any thread features we would not
979 burden it with a thread status response. This was for the benefit of
980 GDB 4.13 and older. However, in recent GDB versions the check
981 (``if (cont_thread != 0)'') does not have the desired effect because of
982 sillyness in the way that the remote protocol handles specifying a thread.
983 Since thread support relies on qSymbol support anyway, assume GDB can handle
986 if (using_threads
&& !disable_packet_Tthread
)
988 unsigned int gdb_id_from_wait
;
990 /* FIXME right place to set this? */
991 thread_from_wait
= ((struct inferior_list_entry
*)current_inferior
)->id
;
992 gdb_id_from_wait
= thread_to_gdb_id (current_inferior
);
995 fprintf (stderr
, "Writing resume reply for %ld\n\n", thread_from_wait
);
996 /* This if (1) ought to be unnecessary. But remote_wait in GDB
997 will claim this event belongs to inferior_ptid if we do not
998 specify a thread, and there's no way for gdbserver to know
999 what inferior_ptid is. */
1000 if (1 || old_thread_from_wait
!= thread_from_wait
)
1002 general_thread
= thread_from_wait
;
1003 sprintf (buf
, "thread:%x;", gdb_id_from_wait
);
1004 buf
+= strlen (buf
);
1005 old_thread_from_wait
= thread_from_wait
;
1011 strcpy (buf
, "library:;");
1012 buf
+= strlen (buf
);
1016 /* For W and X, we're done. */
1021 decode_m_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
)
1025 *mem_addr_ptr
= *len_ptr
= 0;
1027 while ((ch
= from
[i
++]) != ',')
1029 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1030 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1033 for (j
= 0; j
< 4; j
++)
1035 if ((ch
= from
[i
++]) == 0)
1037 *len_ptr
= *len_ptr
<< 4;
1038 *len_ptr
|= fromhex (ch
) & 0x0f;
1043 decode_M_packet (char *from
, CORE_ADDR
*mem_addr_ptr
, unsigned int *len_ptr
,
1048 *mem_addr_ptr
= *len_ptr
= 0;
1050 while ((ch
= from
[i
++]) != ',')
1052 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1053 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1056 while ((ch
= from
[i
++]) != ':')
1058 *len_ptr
= *len_ptr
<< 4;
1059 *len_ptr
|= fromhex (ch
) & 0x0f;
1062 convert_ascii_to_int (&from
[i
++], to
, *len_ptr
);
1066 decode_X_packet (char *from
, int packet_len
, CORE_ADDR
*mem_addr_ptr
,
1067 unsigned int *len_ptr
, unsigned char *to
)
1071 *mem_addr_ptr
= *len_ptr
= 0;
1073 while ((ch
= from
[i
++]) != ',')
1075 *mem_addr_ptr
= *mem_addr_ptr
<< 4;
1076 *mem_addr_ptr
|= fromhex (ch
) & 0x0f;
1079 while ((ch
= from
[i
++]) != ':')
1081 *len_ptr
= *len_ptr
<< 4;
1082 *len_ptr
|= fromhex (ch
) & 0x0f;
1085 if (remote_unescape_input ((const gdb_byte
*) &from
[i
], packet_len
- i
,
1086 to
, *len_ptr
) != *len_ptr
)
1092 /* Decode a qXfer write request. */
1094 decode_xfer_write (char *buf
, int packet_len
, char **annex
, CORE_ADDR
*offset
,
1095 unsigned int *len
, unsigned char *data
)
1099 /* Extract and NUL-terminate the annex. */
1101 while (*buf
&& *buf
!= ':')
1107 /* Extract the offset. */
1109 while ((ch
= *buf
++) != ':')
1111 *offset
= *offset
<< 4;
1112 *offset
|= fromhex (ch
) & 0x0f;
1115 /* Get encoded data. */
1116 packet_len
-= buf
- *annex
;
1117 *len
= remote_unescape_input ((const gdb_byte
*) buf
, packet_len
,
1122 /* Decode the parameters of a qSearch:memory packet. */
1125 decode_search_memory_packet (const char *buf
, int packet_len
,
1126 CORE_ADDR
*start_addrp
,
1127 CORE_ADDR
*search_space_lenp
,
1128 gdb_byte
*pattern
, unsigned int *pattern_lenp
)
1130 const char *p
= buf
;
1132 p
= decode_address_to_semicolon (start_addrp
, p
);
1133 p
= decode_address_to_semicolon (search_space_lenp
, p
);
1134 packet_len
-= p
- buf
;
1135 *pattern_lenp
= remote_unescape_input ((const gdb_byte
*) p
, packet_len
,
1136 pattern
, packet_len
);
1140 /* Ask GDB for the address of NAME, and return it in ADDRP if found.
1141 Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1144 look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
1146 char own_buf
[266], *p
, *q
;
1148 struct sym_cache
*sym
;
1150 /* Check the cache first. */
1151 for (sym
= symbol_cache
; sym
; sym
= sym
->next
)
1152 if (strcmp (name
, sym
->name
) == 0)
1158 /* If we've passed the call to thread_db_look_up_symbols, then
1159 anything not in the cache must not exist; we're not interested
1160 in any libraries loaded after that point, only in symbols in
1161 libpthread.so. It might not be an appropriate time to look
1162 up a symbol, e.g. while we're trying to fetch registers. */
1163 if (all_symbols_looked_up
)
1166 /* Send the request. */
1167 strcpy (own_buf
, "qSymbol:");
1168 hexify (own_buf
+ strlen ("qSymbol:"), name
, strlen (name
));
1169 if (putpkt (own_buf
) < 0)
1172 /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1173 len
= getpkt (own_buf
);
1177 /* We ought to handle pretty much any packet at this point while we
1178 wait for the qSymbol "response". That requires re-entering the
1179 main loop. For now, this is an adequate approximation; allow
1180 GDB to read from memory while it figures out the address of the
1182 while (own_buf
[0] == 'm')
1185 unsigned char *mem_buf
;
1186 unsigned int mem_len
;
1188 decode_m_packet (&own_buf
[1], &mem_addr
, &mem_len
);
1189 mem_buf
= xmalloc (mem_len
);
1190 if (read_inferior_memory (mem_addr
, mem_buf
, mem_len
) == 0)
1191 convert_int_to_ascii (mem_buf
, own_buf
, mem_len
);
1193 write_enn (own_buf
);
1195 if (putpkt (own_buf
) < 0)
1197 len
= getpkt (own_buf
);
1202 if (strncmp (own_buf
, "qSymbol:", strlen ("qSymbol:")) != 0)
1204 warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf
);
1208 p
= own_buf
+ strlen ("qSymbol:");
1210 while (*q
&& *q
!= ':')
1213 /* Make sure we found a value for the symbol. */
1214 if (p
== q
|| *q
== '\0')
1217 decode_address (addrp
, p
, q
- p
);
1219 /* Save the symbol in our cache. */
1220 sym
= xmalloc (sizeof (*sym
));
1221 sym
->name
= xstrdup (name
);
1223 sym
->next
= symbol_cache
;
1230 monitor_output (const char *msg
)
1232 char *buf
= xmalloc (strlen (msg
) * 2 + 2);
1235 hexify (buf
+ 1, msg
, 0);
1241 /* Return a malloc allocated string with special characters from TEXT
1242 replaced by entity references. */
1245 xml_escape_text (const char *text
)
1250 /* Compute the length of the result. */
1251 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1269 /* Expand the result. */
1270 result
= xmalloc (i
+ special
+ 1);
1271 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
1275 strcpy (result
+ i
+ special
, "'");
1279 strcpy (result
+ i
+ special
, """);
1283 strcpy (result
+ i
+ special
, "&");
1287 strcpy (result
+ i
+ special
, "<");
1291 strcpy (result
+ i
+ special
, ">");
1295 result
[i
+ special
] = text
[i
];
1298 result
[i
+ special
] = '\0';
1304 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
1307 size_t new_buffer_size
;
1312 new_buffer_size
= buffer
->buffer_size
;
1314 if (new_buffer_size
== 0)
1315 new_buffer_size
= 1;
1317 while (buffer
->used_size
+ size
> new_buffer_size
)
1318 new_buffer_size
*= 2;
1319 new_buffer
= realloc (buffer
->buffer
, new_buffer_size
);
1322 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
1323 buffer
->buffer
= new_buffer
;
1324 buffer
->buffer_size
= new_buffer_size
;
1325 buffer
->used_size
+= size
;
1329 buffer_free (struct buffer
*buffer
)
1334 free (buffer
->buffer
);
1335 buffer
->buffer
= NULL
;
1336 buffer
->buffer_size
= 0;
1337 buffer
->used_size
= 0;
1341 buffer_init (struct buffer
*buffer
)
1343 memset (buffer
, 0, sizeof (*buffer
));
1347 buffer_finish (struct buffer
*buffer
)
1349 char *ret
= buffer
->buffer
;
1350 buffer
->buffer
= NULL
;
1351 buffer
->buffer_size
= 0;
1352 buffer
->used_size
= 0;
1357 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
1364 va_start (ap
, format
);
1367 for (f
= format
; *f
; f
++)
1376 char *a
= va_arg (ap
, char *);
1377 buffer_grow (buffer
, prev
, f
- prev
- 1);
1378 p
= xml_escape_text (a
);
1379 buffer_grow_str (buffer
, p
);
1391 buffer_grow_str (buffer
, prev
);
This page took 0.056375 seconds and 3 git commands to generate.