remote-utils.cc revision 1.1.1.3 1 1.1 christos /* Remote utility routines for the remote server for GDB.
2 1.1.1.3 christos Copyright (C) 1986-2024 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of GDB.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1 christos #if HAVE_TERMIOS_H
20 1.1 christos #include <termios.h>
21 1.1 christos #endif
22 1.1 christos #include "target.h"
23 1.1 christos #include "gdbthread.h"
24 1.1 christos #include "tdesc.h"
25 1.1 christos #include "debug.h"
26 1.1 christos #include "dll.h"
27 1.1 christos #include "gdbsupport/rsp-low.h"
28 1.1 christos #include "gdbsupport/netstuff.h"
29 1.1 christos #include "gdbsupport/filestuff.h"
30 1.1 christos #include "gdbsupport/gdb-sigmask.h"
31 1.1 christos #include <ctype.h>
32 1.1 christos #if HAVE_SYS_IOCTL_H
33 1.1 christos #include <sys/ioctl.h>
34 1.1 christos #endif
35 1.1 christos #if HAVE_SYS_FILE_H
36 1.1 christos #include <sys/file.h>
37 1.1 christos #endif
38 1.1 christos #if HAVE_NETINET_IN_H
39 1.1 christos #include <netinet/in.h>
40 1.1 christos #endif
41 1.1 christos #if HAVE_SYS_SOCKET_H
42 1.1 christos #include <sys/socket.h>
43 1.1 christos #endif
44 1.1 christos #if HAVE_NETDB_H
45 1.1 christos #include <netdb.h>
46 1.1 christos #endif
47 1.1 christos #if HAVE_NETINET_TCP_H
48 1.1 christos #include <netinet/tcp.h>
49 1.1 christos #endif
50 1.1 christos #if HAVE_SYS_IOCTL_H
51 1.1 christos #include <sys/ioctl.h>
52 1.1 christos #endif
53 1.1 christos #if HAVE_SIGNAL_H
54 1.1 christos #include <signal.h>
55 1.1 christos #endif
56 1.1 christos #if HAVE_FCNTL_H
57 1.1 christos #include <fcntl.h>
58 1.1 christos #endif
59 1.1 christos #include "gdbsupport/gdb_sys_time.h"
60 1.1 christos #include <unistd.h>
61 1.1 christos #if HAVE_ARPA_INET_H
62 1.1 christos #include <arpa/inet.h>
63 1.1 christos #endif
64 1.1 christos #include <sys/stat.h>
65 1.1 christos
66 1.1 christos #if USE_WIN32API
67 1.1 christos #include <ws2tcpip.h>
68 1.1 christos #endif
69 1.1 christos
70 1.1 christos #ifndef HAVE_SOCKLEN_T
71 1.1 christos typedef int socklen_t;
72 1.1 christos #endif
73 1.1 christos
74 1.1 christos #ifndef IN_PROCESS_AGENT
75 1.1 christos
76 1.1 christos /* Extra value for readchar_callback. */
77 1.1 christos enum {
78 1.1 christos /* The callback is currently not scheduled. */
79 1.1 christos NOT_SCHEDULED = -1
80 1.1 christos };
81 1.1 christos
82 1.1 christos /* Status of the readchar callback.
83 1.1 christos Either NOT_SCHEDULED or the callback id. */
84 1.1 christos static int readchar_callback = NOT_SCHEDULED;
85 1.1 christos
86 1.1 christos static int readchar (void);
87 1.1 christos static void reset_readchar (void);
88 1.1 christos static void reschedule (void);
89 1.1 christos
90 1.1 christos /* A cache entry for a successfully looked-up symbol. */
91 1.1 christos struct sym_cache
92 1.1 christos {
93 1.1 christos char *name;
94 1.1 christos CORE_ADDR addr;
95 1.1 christos struct sym_cache *next;
96 1.1 christos };
97 1.1 christos
98 1.1 christos static int remote_is_stdio = 0;
99 1.1 christos
100 1.1 christos static int remote_desc = -1;
101 1.1 christos static int listen_desc = -1;
102 1.1 christos
103 1.1 christos #ifdef USE_WIN32API
104 1.1.1.2 christos /* gnulib wraps these as macros, undo them. */
105 1.1.1.2 christos # undef read
106 1.1.1.2 christos # undef write
107 1.1.1.2 christos
108 1.1 christos # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
109 1.1 christos # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
110 1.1 christos #endif
111 1.1 christos
112 1.1 christos int
113 1.1 christos gdb_connected (void)
114 1.1 christos {
115 1.1 christos return remote_desc != -1;
116 1.1 christos }
117 1.1 christos
118 1.1 christos /* Return true if the remote connection is over stdio. */
119 1.1 christos
120 1.1 christos int
121 1.1 christos remote_connection_is_stdio (void)
122 1.1 christos {
123 1.1 christos return remote_is_stdio;
124 1.1 christos }
125 1.1 christos
126 1.1 christos static void
127 1.1 christos enable_async_notification (int fd)
128 1.1 christos {
129 1.1 christos #if defined(F_SETFL) && defined (FASYNC)
130 1.1 christos int save_fcntl_flags;
131 1.1 christos
132 1.1 christos save_fcntl_flags = fcntl (fd, F_GETFL, 0);
133 1.1 christos fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
134 1.1 christos #if defined (F_SETOWN)
135 1.1 christos fcntl (fd, F_SETOWN, getpid ());
136 1.1 christos #endif
137 1.1 christos #endif
138 1.1 christos }
139 1.1 christos
140 1.1 christos static void
141 1.1 christos handle_accept_event (int err, gdb_client_data client_data)
142 1.1 christos {
143 1.1 christos struct sockaddr_storage sockaddr;
144 1.1 christos socklen_t len = sizeof (sockaddr);
145 1.1 christos
146 1.1.1.2 christos threads_debug_printf ("handling possible accept event");
147 1.1 christos
148 1.1 christos remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &len);
149 1.1 christos if (remote_desc == -1)
150 1.1 christos perror_with_name ("Accept failed");
151 1.1 christos
152 1.1 christos /* Enable TCP keep alive process. */
153 1.1 christos socklen_t tmp = 1;
154 1.1 christos setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
155 1.1 christos (char *) &tmp, sizeof (tmp));
156 1.1 christos
157 1.1 christos /* Tell TCP not to delay small packets. This greatly speeds up
158 1.1 christos interactive response. */
159 1.1 christos tmp = 1;
160 1.1 christos setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
161 1.1 christos (char *) &tmp, sizeof (tmp));
162 1.1 christos
163 1.1 christos #ifndef USE_WIN32API
164 1.1 christos signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
165 1.1 christos exits when the remote side dies. */
166 1.1 christos #endif
167 1.1 christos
168 1.1 christos if (run_once)
169 1.1 christos {
170 1.1 christos #ifndef USE_WIN32API
171 1.1 christos close (listen_desc); /* No longer need this */
172 1.1 christos #else
173 1.1 christos closesocket (listen_desc); /* No longer need this */
174 1.1 christos #endif
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* Even if !RUN_ONCE no longer notice new connections. Still keep the
178 1.1 christos descriptor open for add_file_handler to wait for a new connection. */
179 1.1 christos delete_file_handler (listen_desc);
180 1.1 christos
181 1.1 christos /* Convert IP address to string. */
182 1.1 christos char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
183 1.1 christos
184 1.1 christos int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
185 1.1 christos orig_host, sizeof (orig_host),
186 1.1 christos orig_port, sizeof (orig_port),
187 1.1 christos NI_NUMERICHOST | NI_NUMERICSERV);
188 1.1 christos
189 1.1 christos if (r != 0)
190 1.1 christos fprintf (stderr, _("Could not obtain remote address: %s\n"),
191 1.1 christos gai_strerror (r));
192 1.1 christos else
193 1.1 christos fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
194 1.1 christos orig_host, orig_port);
195 1.1 christos
196 1.1 christos enable_async_notification (remote_desc);
197 1.1 christos
198 1.1 christos /* Register the event loop handler. */
199 1.1.1.2 christos add_file_handler (remote_desc, handle_serial_event, NULL, "remote-net");
200 1.1 christos
201 1.1 christos /* We have a new GDB connection now. If we were disconnected
202 1.1 christos tracing, there's a window where the target could report a stop
203 1.1 christos event to the event loop, and since we have a connection now, we'd
204 1.1 christos try to send vStopped notifications to GDB. But, don't do that
205 1.1 christos until GDB as selected all-stop/non-stop, and has queried the
206 1.1 christos threads' status ('?'). */
207 1.1 christos target_async (0);
208 1.1 christos }
209 1.1 christos
210 1.1 christos /* Prepare for a later connection to a remote debugger.
211 1.1 christos NAME is the filename used for communication. */
212 1.1 christos
213 1.1 christos void
214 1.1 christos remote_prepare (const char *name)
215 1.1 christos {
216 1.1 christos client_state &cs = get_client_state ();
217 1.1 christos #ifdef USE_WIN32API
218 1.1 christos static int winsock_initialized;
219 1.1 christos #endif
220 1.1 christos socklen_t tmp;
221 1.1 christos
222 1.1 christos remote_is_stdio = 0;
223 1.1 christos if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
224 1.1 christos {
225 1.1 christos /* We need to record fact that we're using stdio sooner than the
226 1.1 christos call to remote_open so start_inferior knows the connection is
227 1.1 christos via stdio. */
228 1.1 christos remote_is_stdio = 1;
229 1.1 christos cs.transport_is_reliable = 1;
230 1.1 christos return;
231 1.1 christos }
232 1.1 christos
233 1.1 christos struct addrinfo hint;
234 1.1 christos struct addrinfo *ainfo;
235 1.1 christos
236 1.1 christos memset (&hint, 0, sizeof (hint));
237 1.1 christos /* Assume no prefix will be passed, therefore we should use
238 1.1 christos AF_UNSPEC. */
239 1.1 christos hint.ai_family = AF_UNSPEC;
240 1.1 christos hint.ai_socktype = SOCK_STREAM;
241 1.1 christos hint.ai_protocol = IPPROTO_TCP;
242 1.1 christos
243 1.1 christos parsed_connection_spec parsed
244 1.1 christos = parse_connection_spec_without_prefix (name, &hint);
245 1.1 christos
246 1.1 christos if (parsed.port_str.empty ())
247 1.1 christos {
248 1.1 christos cs.transport_is_reliable = 0;
249 1.1 christos return;
250 1.1 christos }
251 1.1 christos
252 1.1 christos #ifdef USE_WIN32API
253 1.1 christos if (!winsock_initialized)
254 1.1 christos {
255 1.1 christos WSADATA wsad;
256 1.1 christos
257 1.1 christos WSAStartup (MAKEWORD (1, 0), &wsad);
258 1.1 christos winsock_initialized = 1;
259 1.1 christos }
260 1.1 christos #endif
261 1.1 christos
262 1.1 christos int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
263 1.1 christos &hint, &ainfo);
264 1.1 christos
265 1.1 christos if (r != 0)
266 1.1 christos error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
267 1.1 christos
268 1.1 christos scoped_free_addrinfo freeaddrinfo (ainfo);
269 1.1 christos
270 1.1 christos struct addrinfo *iter;
271 1.1 christos
272 1.1 christos for (iter = ainfo; iter != NULL; iter = iter->ai_next)
273 1.1 christos {
274 1.1 christos listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
275 1.1 christos iter->ai_protocol);
276 1.1 christos
277 1.1 christos if (listen_desc >= 0)
278 1.1 christos break;
279 1.1 christos }
280 1.1 christos
281 1.1 christos if (iter == NULL)
282 1.1 christos perror_with_name ("Can't open socket");
283 1.1 christos
284 1.1 christos /* Allow rapid reuse of this port. */
285 1.1 christos tmp = 1;
286 1.1 christos setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
287 1.1 christos sizeof (tmp));
288 1.1 christos
289 1.1 christos switch (iter->ai_family)
290 1.1 christos {
291 1.1 christos case AF_INET:
292 1.1 christos ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
293 1.1 christos break;
294 1.1 christos case AF_INET6:
295 1.1 christos ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
296 1.1 christos break;
297 1.1 christos default:
298 1.1.1.2 christos internal_error (_("Invalid 'ai_family' %d\n"), iter->ai_family);
299 1.1 christos }
300 1.1 christos
301 1.1 christos if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
302 1.1 christos perror_with_name ("Can't bind address");
303 1.1 christos
304 1.1 christos if (listen (listen_desc, 1) != 0)
305 1.1 christos perror_with_name ("Can't listen on socket");
306 1.1 christos
307 1.1 christos cs.transport_is_reliable = 1;
308 1.1 christos }
309 1.1 christos
310 1.1 christos /* Open a connection to a remote debugger.
311 1.1 christos NAME is the filename used for communication. */
312 1.1 christos
313 1.1 christos void
314 1.1 christos remote_open (const char *name)
315 1.1 christos {
316 1.1 christos const char *port_str;
317 1.1 christos
318 1.1 christos port_str = strchr (name, ':');
319 1.1 christos #ifdef USE_WIN32API
320 1.1 christos if (port_str == NULL)
321 1.1 christos error ("Only HOST:PORT is supported on this platform.");
322 1.1 christos #endif
323 1.1 christos
324 1.1 christos if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
325 1.1 christos {
326 1.1 christos fprintf (stderr, "Remote debugging using stdio\n");
327 1.1 christos
328 1.1 christos /* Use stdin as the handle of the connection.
329 1.1 christos We only select on reads, for example. */
330 1.1 christos remote_desc = fileno (stdin);
331 1.1 christos
332 1.1 christos enable_async_notification (remote_desc);
333 1.1 christos
334 1.1 christos /* Register the event loop handler. */
335 1.1.1.2 christos add_file_handler (remote_desc, handle_serial_event, NULL, "remote-stdio");
336 1.1 christos }
337 1.1 christos #ifndef USE_WIN32API
338 1.1 christos else if (port_str == NULL)
339 1.1 christos {
340 1.1 christos struct stat statbuf;
341 1.1 christos
342 1.1 christos if (stat (name, &statbuf) == 0
343 1.1 christos && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
344 1.1 christos remote_desc = open (name, O_RDWR);
345 1.1 christos else
346 1.1 christos {
347 1.1 christos errno = EINVAL;
348 1.1 christos remote_desc = -1;
349 1.1 christos }
350 1.1 christos
351 1.1 christos if (remote_desc < 0)
352 1.1 christos perror_with_name ("Could not open remote device");
353 1.1 christos
354 1.1 christos #if HAVE_TERMIOS_H
355 1.1 christos {
356 1.1 christos struct termios termios;
357 1.1 christos tcgetattr (remote_desc, &termios);
358 1.1 christos
359 1.1 christos termios.c_iflag = 0;
360 1.1 christos termios.c_oflag = 0;
361 1.1 christos termios.c_lflag = 0;
362 1.1 christos termios.c_cflag &= ~(CSIZE | PARENB);
363 1.1 christos termios.c_cflag |= CLOCAL | CS8;
364 1.1 christos termios.c_cc[VMIN] = 1;
365 1.1 christos termios.c_cc[VTIME] = 0;
366 1.1 christos
367 1.1 christos tcsetattr (remote_desc, TCSANOW, &termios);
368 1.1 christos }
369 1.1 christos #endif
370 1.1 christos
371 1.1 christos fprintf (stderr, "Remote debugging using %s\n", name);
372 1.1 christos
373 1.1 christos enable_async_notification (remote_desc);
374 1.1 christos
375 1.1 christos /* Register the event loop handler. */
376 1.1.1.2 christos add_file_handler (remote_desc, handle_serial_event, NULL,
377 1.1.1.2 christos "remote-device");
378 1.1 christos }
379 1.1 christos #endif /* USE_WIN32API */
380 1.1 christos else
381 1.1 christos {
382 1.1 christos char listen_port[GDB_NI_MAX_PORT];
383 1.1 christos struct sockaddr_storage sockaddr;
384 1.1 christos socklen_t len = sizeof (sockaddr);
385 1.1 christos
386 1.1 christos if (getsockname (listen_desc, (struct sockaddr *) &sockaddr, &len) < 0)
387 1.1 christos perror_with_name ("Can't determine port");
388 1.1 christos
389 1.1 christos int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
390 1.1 christos NULL, 0,
391 1.1 christos listen_port, sizeof (listen_port),
392 1.1 christos NI_NUMERICSERV);
393 1.1 christos
394 1.1 christos if (r != 0)
395 1.1 christos fprintf (stderr, _("Can't obtain port where we are listening: %s"),
396 1.1 christos gai_strerror (r));
397 1.1 christos else
398 1.1 christos fprintf (stderr, _("Listening on port %s\n"), listen_port);
399 1.1 christos
400 1.1 christos fflush (stderr);
401 1.1 christos
402 1.1 christos /* Register the event loop handler. */
403 1.1.1.2 christos add_file_handler (listen_desc, handle_accept_event, NULL,
404 1.1.1.2 christos "remote-listen");
405 1.1 christos }
406 1.1 christos }
407 1.1 christos
408 1.1 christos void
409 1.1 christos remote_close (void)
410 1.1 christos {
411 1.1 christos delete_file_handler (remote_desc);
412 1.1 christos
413 1.1 christos disable_async_io ();
414 1.1 christos
415 1.1 christos #ifdef USE_WIN32API
416 1.1 christos closesocket (remote_desc);
417 1.1 christos #else
418 1.1 christos if (! remote_connection_is_stdio ())
419 1.1 christos close (remote_desc);
420 1.1 christos #endif
421 1.1 christos remote_desc = -1;
422 1.1 christos
423 1.1 christos reset_readchar ();
424 1.1 christos }
425 1.1 christos
426 1.1 christos #endif
427 1.1 christos
428 1.1 christos #ifndef IN_PROCESS_AGENT
429 1.1 christos
430 1.1 christos void
431 1.1 christos decode_address (CORE_ADDR *addrp, const char *start, int len)
432 1.1 christos {
433 1.1 christos CORE_ADDR addr;
434 1.1 christos char ch;
435 1.1 christos int i;
436 1.1 christos
437 1.1 christos addr = 0;
438 1.1 christos for (i = 0; i < len; i++)
439 1.1 christos {
440 1.1 christos ch = start[i];
441 1.1 christos addr = addr << 4;
442 1.1 christos addr = addr | (fromhex (ch) & 0x0f);
443 1.1 christos }
444 1.1 christos *addrp = addr;
445 1.1 christos }
446 1.1 christos
447 1.1 christos const char *
448 1.1 christos decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
449 1.1 christos {
450 1.1 christos const char *end;
451 1.1 christos
452 1.1 christos end = start;
453 1.1 christos while (*end != '\0' && *end != ';')
454 1.1 christos end++;
455 1.1 christos
456 1.1 christos decode_address (addrp, start, end - start);
457 1.1 christos
458 1.1 christos if (*end == ';')
459 1.1 christos end++;
460 1.1 christos return end;
461 1.1 christos }
462 1.1 christos
463 1.1 christos #endif
464 1.1 christos
465 1.1 christos #ifndef IN_PROCESS_AGENT
466 1.1 christos
467 1.1 christos /* Look for a sequence of characters which can be run-length encoded.
468 1.1 christos If there are any, update *CSUM and *P. Otherwise, output the
469 1.1 christos single character. Return the number of characters consumed. */
470 1.1 christos
471 1.1 christos static int
472 1.1 christos try_rle (char *buf, int remaining, unsigned char *csum, char **p)
473 1.1 christos {
474 1.1 christos int n;
475 1.1 christos
476 1.1 christos /* Always output the character. */
477 1.1 christos *csum += buf[0];
478 1.1 christos *(*p)++ = buf[0];
479 1.1 christos
480 1.1 christos /* Don't go past '~'. */
481 1.1 christos if (remaining > 97)
482 1.1 christos remaining = 97;
483 1.1 christos
484 1.1 christos for (n = 1; n < remaining; n++)
485 1.1 christos if (buf[n] != buf[0])
486 1.1 christos break;
487 1.1 christos
488 1.1 christos /* N is the index of the first character not the same as buf[0].
489 1.1 christos buf[0] is counted twice, so by decrementing N, we get the number
490 1.1 christos of characters the RLE sequence will replace. */
491 1.1 christos n--;
492 1.1 christos
493 1.1 christos if (n < 3)
494 1.1 christos return 1;
495 1.1 christos
496 1.1 christos /* Skip the frame characters. The manual says to skip '+' and '-'
497 1.1 christos also, but there's no reason to. Unfortunately these two unusable
498 1.1 christos characters double the encoded length of a four byte zero
499 1.1 christos value. */
500 1.1 christos while (n + 29 == '$' || n + 29 == '#')
501 1.1 christos n--;
502 1.1 christos
503 1.1 christos *csum += '*';
504 1.1 christos *(*p)++ = '*';
505 1.1 christos *csum += n + 29;
506 1.1 christos *(*p)++ = n + 29;
507 1.1 christos
508 1.1 christos return n + 1;
509 1.1 christos }
510 1.1 christos
511 1.1 christos #endif
512 1.1 christos
513 1.1 christos #ifndef IN_PROCESS_AGENT
514 1.1 christos
515 1.1 christos /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
516 1.1 christos
517 1.1 christos char *
518 1.1 christos write_ptid (char *buf, ptid_t ptid)
519 1.1 christos {
520 1.1 christos client_state &cs = get_client_state ();
521 1.1 christos int pid, tid;
522 1.1 christos
523 1.1 christos if (cs.multi_process)
524 1.1 christos {
525 1.1 christos pid = ptid.pid ();
526 1.1 christos if (pid < 0)
527 1.1 christos buf += sprintf (buf, "p-%x.", -pid);
528 1.1 christos else
529 1.1 christos buf += sprintf (buf, "p%x.", pid);
530 1.1 christos }
531 1.1 christos tid = ptid.lwp ();
532 1.1 christos if (tid < 0)
533 1.1 christos buf += sprintf (buf, "-%x", -tid);
534 1.1 christos else
535 1.1 christos buf += sprintf (buf, "%x", tid);
536 1.1 christos
537 1.1 christos return buf;
538 1.1 christos }
539 1.1 christos
540 1.1 christos static ULONGEST
541 1.1 christos hex_or_minus_one (const char *buf, const char **obuf)
542 1.1 christos {
543 1.1 christos ULONGEST ret;
544 1.1 christos
545 1.1 christos if (startswith (buf, "-1"))
546 1.1 christos {
547 1.1 christos ret = (ULONGEST) -1;
548 1.1 christos buf += 2;
549 1.1 christos }
550 1.1 christos else
551 1.1 christos buf = unpack_varlen_hex (buf, &ret);
552 1.1 christos
553 1.1 christos if (obuf)
554 1.1 christos *obuf = buf;
555 1.1 christos
556 1.1 christos return ret;
557 1.1 christos }
558 1.1 christos
559 1.1 christos /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
560 1.1 christos passed the last parsed char. Returns null_ptid on error. */
561 1.1 christos ptid_t
562 1.1 christos read_ptid (const char *buf, const char **obuf)
563 1.1 christos {
564 1.1 christos const char *p = buf;
565 1.1 christos const char *pp;
566 1.1 christos ULONGEST pid = 0, tid = 0;
567 1.1 christos
568 1.1 christos if (*p == 'p')
569 1.1 christos {
570 1.1 christos /* Multi-process ptid. */
571 1.1 christos pp = unpack_varlen_hex (p + 1, &pid);
572 1.1 christos if (*pp != '.')
573 1.1 christos error ("invalid remote ptid: %s\n", p);
574 1.1 christos
575 1.1 christos p = pp + 1;
576 1.1 christos
577 1.1 christos tid = hex_or_minus_one (p, &pp);
578 1.1 christos
579 1.1 christos if (obuf)
580 1.1 christos *obuf = pp;
581 1.1.1.2 christos return ptid_t (pid, tid);
582 1.1 christos }
583 1.1 christos
584 1.1 christos /* No multi-process. Just a tid. */
585 1.1 christos tid = hex_or_minus_one (p, &pp);
586 1.1 christos
587 1.1 christos /* Since GDB is not sending a process id (multi-process extensions
588 1.1 christos are off), then there's only one process. Default to the first in
589 1.1 christos the list. */
590 1.1 christos pid = pid_of (get_first_process ());
591 1.1 christos
592 1.1 christos if (obuf)
593 1.1 christos *obuf = pp;
594 1.1.1.2 christos return ptid_t (pid, tid);
595 1.1 christos }
596 1.1 christos
597 1.1 christos /* Write COUNT bytes in BUF to the client.
598 1.1 christos The result is the number of bytes written or -1 if error.
599 1.1 christos This may return less than COUNT. */
600 1.1 christos
601 1.1 christos static int
602 1.1 christos write_prim (const void *buf, int count)
603 1.1 christos {
604 1.1 christos if (remote_connection_is_stdio ())
605 1.1 christos return write (fileno (stdout), buf, count);
606 1.1 christos else
607 1.1 christos return write (remote_desc, buf, count);
608 1.1 christos }
609 1.1 christos
610 1.1 christos /* Read COUNT bytes from the client and store in BUF.
611 1.1 christos The result is the number of bytes read or -1 if error.
612 1.1 christos This may return less than COUNT. */
613 1.1 christos
614 1.1 christos static int
615 1.1 christos read_prim (void *buf, int count)
616 1.1 christos {
617 1.1 christos if (remote_connection_is_stdio ())
618 1.1 christos return read (fileno (stdin), buf, count);
619 1.1 christos else
620 1.1 christos return read (remote_desc, buf, count);
621 1.1 christos }
622 1.1 christos
623 1.1 christos /* Send a packet to the remote machine, with error checking.
624 1.1 christos The data of the packet is in BUF, and the length of the
625 1.1 christos packet is in CNT. Returns >= 0 on success, -1 otherwise. */
626 1.1 christos
627 1.1 christos static int
628 1.1 christos putpkt_binary_1 (char *buf, int cnt, int is_notif)
629 1.1 christos {
630 1.1 christos client_state &cs = get_client_state ();
631 1.1 christos int i;
632 1.1 christos unsigned char csum = 0;
633 1.1 christos char *buf2;
634 1.1 christos char *p;
635 1.1 christos int cc;
636 1.1 christos
637 1.1 christos buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
638 1.1 christos
639 1.1 christos /* Copy the packet into buffer BUF2, encapsulating it
640 1.1 christos and giving it a checksum. */
641 1.1 christos
642 1.1 christos p = buf2;
643 1.1 christos if (is_notif)
644 1.1 christos *p++ = '%';
645 1.1 christos else
646 1.1 christos *p++ = '$';
647 1.1 christos
648 1.1 christos for (i = 0; i < cnt;)
649 1.1 christos i += try_rle (buf + i, cnt - i, &csum, &p);
650 1.1 christos
651 1.1 christos *p++ = '#';
652 1.1 christos *p++ = tohex ((csum >> 4) & 0xf);
653 1.1 christos *p++ = tohex (csum & 0xf);
654 1.1 christos
655 1.1 christos *p = '\0';
656 1.1 christos
657 1.1 christos /* Send it over and over until we get a positive ack. */
658 1.1 christos
659 1.1 christos do
660 1.1 christos {
661 1.1 christos if (write_prim (buf2, p - buf2) != p - buf2)
662 1.1 christos {
663 1.1 christos perror ("putpkt(write)");
664 1.1 christos free (buf2);
665 1.1 christos return -1;
666 1.1 christos }
667 1.1 christos
668 1.1 christos if (cs.noack_mode || is_notif)
669 1.1 christos {
670 1.1 christos /* Don't expect an ack then. */
671 1.1.1.2 christos if (is_notif)
672 1.1.1.2 christos remote_debug_printf ("putpkt (\"%s\"); [notif]", buf2);
673 1.1.1.2 christos else
674 1.1.1.2 christos remote_debug_printf ("putpkt (\"%s\"); [noack mode]", buf2);
675 1.1.1.2 christos
676 1.1 christos break;
677 1.1 christos }
678 1.1 christos
679 1.1.1.2 christos remote_debug_printf ("putpkt (\"%s\"); [looking for ack]", buf2);
680 1.1 christos
681 1.1 christos cc = readchar ();
682 1.1 christos
683 1.1 christos if (cc < 0)
684 1.1 christos {
685 1.1 christos free (buf2);
686 1.1 christos return -1;
687 1.1 christos }
688 1.1 christos
689 1.1.1.2 christos remote_debug_printf ("[received '%c' (0x%x)]", cc, cc);
690 1.1 christos
691 1.1 christos /* Check for an input interrupt while we're here. */
692 1.1 christos if (cc == '\003' && current_thread != NULL)
693 1.1 christos the_target->request_interrupt ();
694 1.1 christos }
695 1.1 christos while (cc != '+');
696 1.1 christos
697 1.1 christos free (buf2);
698 1.1 christos return 1; /* Success! */
699 1.1 christos }
700 1.1 christos
701 1.1 christos int
702 1.1 christos putpkt_binary (char *buf, int cnt)
703 1.1 christos {
704 1.1 christos return putpkt_binary_1 (buf, cnt, 0);
705 1.1 christos }
706 1.1 christos
707 1.1 christos /* Send a packet to the remote machine, with error checking. The data
708 1.1 christos of the packet is in BUF, and the packet should be a NUL-terminated
709 1.1 christos string. Returns >= 0 on success, -1 otherwise. */
710 1.1 christos
711 1.1 christos int
712 1.1 christos putpkt (char *buf)
713 1.1 christos {
714 1.1 christos return putpkt_binary (buf, strlen (buf));
715 1.1 christos }
716 1.1 christos
717 1.1 christos int
718 1.1 christos putpkt_notif (char *buf)
719 1.1 christos {
720 1.1 christos return putpkt_binary_1 (buf, strlen (buf), 1);
721 1.1 christos }
722 1.1 christos
723 1.1 christos /* Come here when we get an input interrupt from the remote side. This
724 1.1 christos interrupt should only be active while we are waiting for the child to do
725 1.1 christos something. Thus this assumes readchar:bufcnt is 0.
726 1.1 christos About the only thing that should come through is a ^C, which
727 1.1 christos will cause us to request child interruption. */
728 1.1 christos
729 1.1 christos static void
730 1.1 christos input_interrupt (int unused)
731 1.1 christos {
732 1.1 christos fd_set readset;
733 1.1 christos struct timeval immediate = { 0, 0 };
734 1.1 christos
735 1.1 christos /* Protect against spurious interrupts. This has been observed to
736 1.1 christos be a problem under NetBSD 1.4 and 1.5. */
737 1.1 christos
738 1.1 christos FD_ZERO (&readset);
739 1.1 christos FD_SET (remote_desc, &readset);
740 1.1 christos if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
741 1.1 christos {
742 1.1 christos int cc;
743 1.1 christos char c = 0;
744 1.1 christos
745 1.1 christos cc = read_prim (&c, 1);
746 1.1 christos
747 1.1 christos if (cc == 0)
748 1.1 christos {
749 1.1 christos fprintf (stderr, "client connection closed\n");
750 1.1 christos return;
751 1.1 christos }
752 1.1 christos else if (cc != 1 || c != '\003')
753 1.1 christos {
754 1.1 christos fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
755 1.1 christos if (isprint (c))
756 1.1 christos fprintf (stderr, "('%c')\n", c);
757 1.1 christos else
758 1.1 christos fprintf (stderr, "('\\x%02x')\n", c & 0xff);
759 1.1 christos return;
760 1.1 christos }
761 1.1 christos
762 1.1 christos the_target->request_interrupt ();
763 1.1 christos }
764 1.1 christos }
765 1.1 christos
766 1.1 christos /* Check if the remote side sent us an interrupt request (^C). */
767 1.1 christos void
768 1.1 christos check_remote_input_interrupt_request (void)
769 1.1 christos {
770 1.1 christos /* This function may be called before establishing communications,
771 1.1 christos therefore we need to validate the remote descriptor. */
772 1.1 christos
773 1.1 christos if (remote_desc == -1)
774 1.1 christos return;
775 1.1 christos
776 1.1 christos input_interrupt (0);
777 1.1 christos }
778 1.1 christos
779 1.1 christos /* Asynchronous I/O support. SIGIO must be unblocked when waiting,
780 1.1 christos in order to accept Control-C from the client, and must be blocked
781 1.1 christos when talking to the client. */
782 1.1 christos
783 1.1 christos static void
784 1.1 christos block_unblock_async_io (int block)
785 1.1 christos {
786 1.1 christos #ifndef USE_WIN32API
787 1.1 christos sigset_t sigio_set;
788 1.1 christos
789 1.1 christos sigemptyset (&sigio_set);
790 1.1 christos sigaddset (&sigio_set, SIGIO);
791 1.1 christos gdb_sigmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
792 1.1 christos #endif
793 1.1 christos }
794 1.1 christos
795 1.1 christos /* Current state of asynchronous I/O. */
796 1.1 christos static int async_io_enabled;
797 1.1 christos
798 1.1 christos /* Enable asynchronous I/O. */
799 1.1 christos void
800 1.1 christos enable_async_io (void)
801 1.1 christos {
802 1.1 christos if (async_io_enabled)
803 1.1 christos return;
804 1.1 christos
805 1.1 christos block_unblock_async_io (0);
806 1.1 christos
807 1.1 christos async_io_enabled = 1;
808 1.1 christos }
809 1.1 christos
810 1.1 christos /* Disable asynchronous I/O. */
811 1.1 christos void
812 1.1 christos disable_async_io (void)
813 1.1 christos {
814 1.1 christos if (!async_io_enabled)
815 1.1 christos return;
816 1.1 christos
817 1.1 christos block_unblock_async_io (1);
818 1.1 christos
819 1.1 christos async_io_enabled = 0;
820 1.1 christos }
821 1.1 christos
822 1.1 christos void
823 1.1 christos initialize_async_io (void)
824 1.1 christos {
825 1.1 christos /* Make sure that async I/O starts blocked. */
826 1.1 christos async_io_enabled = 1;
827 1.1 christos disable_async_io ();
828 1.1 christos
829 1.1 christos /* Install the signal handler. */
830 1.1 christos #ifndef USE_WIN32API
831 1.1 christos signal (SIGIO, input_interrupt);
832 1.1 christos #endif
833 1.1 christos }
834 1.1 christos
835 1.1 christos /* Internal buffer used by readchar.
836 1.1 christos These are global to readchar because reschedule_remote needs to be
837 1.1 christos able to tell whether the buffer is empty. */
838 1.1 christos
839 1.1 christos static unsigned char readchar_buf[BUFSIZ];
840 1.1 christos static int readchar_bufcnt = 0;
841 1.1 christos static unsigned char *readchar_bufp;
842 1.1 christos
843 1.1 christos /* Returns next char from remote GDB. -1 if error. */
844 1.1 christos
845 1.1 christos static int
846 1.1 christos readchar (void)
847 1.1 christos {
848 1.1 christos int ch;
849 1.1 christos
850 1.1 christos if (readchar_bufcnt == 0)
851 1.1 christos {
852 1.1 christos readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
853 1.1 christos
854 1.1 christos if (readchar_bufcnt <= 0)
855 1.1 christos {
856 1.1 christos if (readchar_bufcnt == 0)
857 1.1 christos {
858 1.1.1.2 christos remote_debug_printf ("readchar: Got EOF");
859 1.1 christos }
860 1.1 christos else
861 1.1 christos perror ("readchar");
862 1.1 christos
863 1.1 christos return -1;
864 1.1 christos }
865 1.1 christos
866 1.1 christos readchar_bufp = readchar_buf;
867 1.1 christos }
868 1.1 christos
869 1.1 christos readchar_bufcnt--;
870 1.1 christos ch = *readchar_bufp++;
871 1.1 christos reschedule ();
872 1.1 christos return ch;
873 1.1 christos }
874 1.1 christos
875 1.1 christos /* Reset the readchar state machine. */
876 1.1 christos
877 1.1 christos static void
878 1.1 christos reset_readchar (void)
879 1.1 christos {
880 1.1 christos readchar_bufcnt = 0;
881 1.1 christos if (readchar_callback != NOT_SCHEDULED)
882 1.1 christos {
883 1.1 christos delete_timer (readchar_callback);
884 1.1 christos readchar_callback = NOT_SCHEDULED;
885 1.1 christos }
886 1.1 christos }
887 1.1 christos
888 1.1 christos /* Process remaining data in readchar_buf. */
889 1.1 christos
890 1.1 christos static void
891 1.1 christos process_remaining (void *context)
892 1.1 christos {
893 1.1 christos /* This is a one-shot event. */
894 1.1 christos readchar_callback = NOT_SCHEDULED;
895 1.1 christos
896 1.1 christos if (readchar_bufcnt > 0)
897 1.1 christos handle_serial_event (0, NULL);
898 1.1 christos }
899 1.1 christos
900 1.1 christos /* If there is still data in the buffer, queue another event to process it,
901 1.1 christos we can't sleep in select yet. */
902 1.1 christos
903 1.1 christos static void
904 1.1 christos reschedule (void)
905 1.1 christos {
906 1.1 christos if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
907 1.1 christos readchar_callback = create_timer (0, process_remaining, NULL);
908 1.1 christos }
909 1.1 christos
910 1.1 christos /* Read a packet from the remote machine, with error checking,
911 1.1 christos and store it in BUF. Returns length of packet, or negative if error. */
912 1.1 christos
913 1.1 christos int
914 1.1 christos getpkt (char *buf)
915 1.1 christos {
916 1.1 christos client_state &cs = get_client_state ();
917 1.1 christos char *bp;
918 1.1 christos unsigned char csum, c1, c2;
919 1.1 christos int c;
920 1.1 christos
921 1.1 christos while (1)
922 1.1 christos {
923 1.1 christos csum = 0;
924 1.1 christos
925 1.1 christos while (1)
926 1.1 christos {
927 1.1 christos c = readchar ();
928 1.1 christos
929 1.1 christos /* The '\003' may appear before or after each packet, so
930 1.1 christos check for an input interrupt. */
931 1.1 christos if (c == '\003')
932 1.1 christos {
933 1.1 christos the_target->request_interrupt ();
934 1.1 christos continue;
935 1.1 christos }
936 1.1 christos
937 1.1 christos if (c == '$')
938 1.1 christos break;
939 1.1.1.2 christos
940 1.1.1.2 christos remote_debug_printf ("[getpkt: discarding char '%c']", c);
941 1.1 christos
942 1.1 christos if (c < 0)
943 1.1 christos return -1;
944 1.1 christos }
945 1.1 christos
946 1.1 christos bp = buf;
947 1.1 christos while (1)
948 1.1 christos {
949 1.1 christos c = readchar ();
950 1.1 christos if (c < 0)
951 1.1 christos return -1;
952 1.1 christos if (c == '#')
953 1.1 christos break;
954 1.1 christos *bp++ = c;
955 1.1 christos csum += c;
956 1.1 christos }
957 1.1 christos *bp = 0;
958 1.1 christos
959 1.1 christos c1 = fromhex (readchar ());
960 1.1 christos c2 = fromhex (readchar ());
961 1.1 christos
962 1.1 christos if (csum == (c1 << 4) + c2)
963 1.1 christos break;
964 1.1 christos
965 1.1 christos if (cs.noack_mode)
966 1.1 christos {
967 1.1 christos fprintf (stderr,
968 1.1 christos "Bad checksum, sentsum=0x%x, csum=0x%x, "
969 1.1 christos "buf=%s [no-ack-mode, Bad medium?]\n",
970 1.1 christos (c1 << 4) + c2, csum, buf);
971 1.1 christos /* Not much we can do, GDB wasn't expecting an ack/nac. */
972 1.1 christos break;
973 1.1 christos }
974 1.1 christos
975 1.1 christos fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
976 1.1 christos (c1 << 4) + c2, csum, buf);
977 1.1 christos if (write_prim ("-", 1) != 1)
978 1.1 christos return -1;
979 1.1 christos }
980 1.1 christos
981 1.1 christos if (!cs.noack_mode)
982 1.1 christos {
983 1.1.1.2 christos remote_debug_printf ("getpkt (\"%s\"); [sending ack]", buf);
984 1.1 christos
985 1.1 christos if (write_prim ("+", 1) != 1)
986 1.1 christos return -1;
987 1.1 christos
988 1.1.1.2 christos remote_debug_printf ("[sent ack]");
989 1.1 christos }
990 1.1 christos else
991 1.1.1.2 christos remote_debug_printf ("getpkt (\"%s\"); [no ack sent]", buf);
992 1.1 christos
993 1.1 christos /* The readchar above may have already read a '\003' out of the socket
994 1.1 christos and moved it to the local buffer. For example, when GDB sends
995 1.1 christos vCont;c immediately followed by interrupt (see
996 1.1 christos gdb.base/interrupt-noterm.exp). As soon as we see the vCont;c, we'll
997 1.1 christos resume the inferior and wait. Since we've already moved the '\003'
998 1.1 christos to the local buffer, SIGIO won't help. In that case, if we don't
999 1.1 christos check for interrupt after the vCont;c packet, the interrupt character
1000 1.1 christos would stay in the buffer unattended until after the next (unrelated)
1001 1.1 christos stop. */
1002 1.1 christos while (readchar_bufcnt > 0 && *readchar_bufp == '\003')
1003 1.1 christos {
1004 1.1 christos /* Consume the interrupt character in the buffer. */
1005 1.1 christos readchar ();
1006 1.1 christos the_target->request_interrupt ();
1007 1.1 christos }
1008 1.1 christos
1009 1.1 christos return bp - buf;
1010 1.1 christos }
1011 1.1 christos
1012 1.1 christos void
1013 1.1 christos write_ok (char *buf)
1014 1.1 christos {
1015 1.1 christos buf[0] = 'O';
1016 1.1 christos buf[1] = 'K';
1017 1.1 christos buf[2] = '\0';
1018 1.1 christos }
1019 1.1 christos
1020 1.1 christos void
1021 1.1 christos write_enn (char *buf)
1022 1.1 christos {
1023 1.1 christos /* Some day, we should define the meanings of the error codes... */
1024 1.1 christos buf[0] = 'E';
1025 1.1 christos buf[1] = '0';
1026 1.1 christos buf[2] = '1';
1027 1.1 christos buf[3] = '\0';
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos #endif
1031 1.1 christos
1032 1.1 christos #ifndef IN_PROCESS_AGENT
1033 1.1 christos
1034 1.1 christos static char *
1035 1.1 christos outreg (struct regcache *regcache, int regno, char *buf)
1036 1.1 christos {
1037 1.1 christos if ((regno >> 12) != 0)
1038 1.1 christos *buf++ = tohex ((regno >> 12) & 0xf);
1039 1.1 christos if ((regno >> 8) != 0)
1040 1.1 christos *buf++ = tohex ((regno >> 8) & 0xf);
1041 1.1 christos *buf++ = tohex ((regno >> 4) & 0xf);
1042 1.1 christos *buf++ = tohex (regno & 0xf);
1043 1.1 christos *buf++ = ':';
1044 1.1 christos collect_register_as_string (regcache, regno, buf);
1045 1.1 christos buf += 2 * register_size (regcache->tdesc, regno);
1046 1.1 christos *buf++ = ';';
1047 1.1 christos
1048 1.1 christos return buf;
1049 1.1 christos }
1050 1.1 christos
1051 1.1 christos void
1052 1.1.1.2 christos prepare_resume_reply (char *buf, ptid_t ptid, const target_waitstatus &status)
1053 1.1 christos {
1054 1.1 christos client_state &cs = get_client_state ();
1055 1.1.1.3 christos threads_debug_printf ("Writing resume reply for %s: %s",
1056 1.1.1.3 christos target_pid_to_str (ptid).c_str (),
1057 1.1.1.3 christos status.to_string ().c_str ());
1058 1.1 christos
1059 1.1.1.2 christos switch (status.kind ())
1060 1.1 christos {
1061 1.1 christos case TARGET_WAITKIND_STOPPED:
1062 1.1 christos case TARGET_WAITKIND_FORKED:
1063 1.1 christos case TARGET_WAITKIND_VFORKED:
1064 1.1 christos case TARGET_WAITKIND_VFORK_DONE:
1065 1.1.1.3 christos case TARGET_WAITKIND_THREAD_CLONED:
1066 1.1 christos case TARGET_WAITKIND_EXECD:
1067 1.1 christos case TARGET_WAITKIND_THREAD_CREATED:
1068 1.1 christos case TARGET_WAITKIND_SYSCALL_ENTRY:
1069 1.1 christos case TARGET_WAITKIND_SYSCALL_RETURN:
1070 1.1 christos {
1071 1.1 christos struct regcache *regcache;
1072 1.1.1.2 christos char *buf_start = buf;
1073 1.1 christos
1074 1.1.1.3 christos if ((status.kind () == TARGET_WAITKIND_FORKED
1075 1.1.1.3 christos && cs.report_fork_events)
1076 1.1.1.2 christos || (status.kind () == TARGET_WAITKIND_VFORKED
1077 1.1.1.3 christos && cs.report_vfork_events)
1078 1.1.1.3 christos || status.kind () == TARGET_WAITKIND_THREAD_CLONED)
1079 1.1 christos {
1080 1.1 christos enum gdb_signal signal = GDB_SIGNAL_TRAP;
1081 1.1.1.3 christos
1082 1.1.1.3 christos auto kind_remote_str = [] (target_waitkind kind)
1083 1.1.1.3 christos {
1084 1.1.1.3 christos switch (kind)
1085 1.1.1.3 christos {
1086 1.1.1.3 christos case TARGET_WAITKIND_FORKED:
1087 1.1.1.3 christos return "fork";
1088 1.1.1.3 christos case TARGET_WAITKIND_VFORKED:
1089 1.1.1.3 christos return "vfork";
1090 1.1.1.3 christos case TARGET_WAITKIND_THREAD_CLONED:
1091 1.1.1.3 christos return "clone";
1092 1.1.1.3 christos default:
1093 1.1.1.3 christos gdb_assert_not_reached ("unhandled kind");
1094 1.1.1.3 christos }
1095 1.1.1.3 christos };
1096 1.1.1.3 christos
1097 1.1.1.3 christos const char *event = kind_remote_str (status.kind ());
1098 1.1 christos
1099 1.1 christos sprintf (buf, "T%02x%s:", signal, event);
1100 1.1 christos buf += strlen (buf);
1101 1.1.1.2 christos buf = write_ptid (buf, status.child_ptid ());
1102 1.1 christos strcat (buf, ";");
1103 1.1 christos }
1104 1.1.1.2 christos else if (status.kind () == TARGET_WAITKIND_VFORK_DONE
1105 1.1 christos && cs.report_vfork_events)
1106 1.1 christos {
1107 1.1 christos enum gdb_signal signal = GDB_SIGNAL_TRAP;
1108 1.1 christos
1109 1.1 christos sprintf (buf, "T%02xvforkdone:;", signal);
1110 1.1 christos }
1111 1.1.1.2 christos else if (status.kind () == TARGET_WAITKIND_EXECD && cs.report_exec_events)
1112 1.1 christos {
1113 1.1 christos enum gdb_signal signal = GDB_SIGNAL_TRAP;
1114 1.1 christos const char *event = "exec";
1115 1.1 christos char hexified_pathname[PATH_MAX * 2];
1116 1.1 christos
1117 1.1 christos sprintf (buf, "T%02x%s:", signal, event);
1118 1.1 christos buf += strlen (buf);
1119 1.1 christos
1120 1.1 christos /* Encode pathname to hexified format. */
1121 1.1.1.2 christos bin2hex ((const gdb_byte *) status.execd_pathname (),
1122 1.1 christos hexified_pathname,
1123 1.1.1.2 christos strlen (status.execd_pathname ()));
1124 1.1 christos
1125 1.1 christos sprintf (buf, "%s;", hexified_pathname);
1126 1.1 christos buf += strlen (buf);
1127 1.1 christos }
1128 1.1.1.2 christos else if (status.kind () == TARGET_WAITKIND_THREAD_CREATED
1129 1.1 christos && cs.report_thread_events)
1130 1.1 christos {
1131 1.1 christos enum gdb_signal signal = GDB_SIGNAL_TRAP;
1132 1.1 christos
1133 1.1 christos sprintf (buf, "T%02xcreate:;", signal);
1134 1.1 christos }
1135 1.1.1.2 christos else if (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
1136 1.1.1.2 christos || status.kind () == TARGET_WAITKIND_SYSCALL_RETURN)
1137 1.1 christos {
1138 1.1 christos enum gdb_signal signal = GDB_SIGNAL_TRAP;
1139 1.1.1.2 christos const char *event = (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
1140 1.1 christos ? "syscall_entry" : "syscall_return");
1141 1.1 christos
1142 1.1 christos sprintf (buf, "T%02x%s:%x;", signal, event,
1143 1.1.1.2 christos status.syscall_number ());
1144 1.1 christos }
1145 1.1 christos else
1146 1.1.1.2 christos sprintf (buf, "T%02x", status.sig ());
1147 1.1 christos
1148 1.1 christos if (disable_packet_T)
1149 1.1 christos {
1150 1.1 christos /* This is a bit (OK, a lot) of a kludge, however, this isn't
1151 1.1 christos really a user feature, but exists only so GDB can use the
1152 1.1 christos gdbserver to test handling of the 'S' stop reply packet, so
1153 1.1 christos we would rather this code be as simple as possible.
1154 1.1 christos
1155 1.1 christos By this point we've started to build the 'T' stop packet,
1156 1.1 christos and it should look like 'Txx....' where 'x' is a hex digit.
1157 1.1 christos An 'S' stop packet always looks like 'Sxx', so all we do
1158 1.1 christos here is convert the buffer from a T packet to an S packet
1159 1.1 christos and the avoid adding any extra content by breaking out. */
1160 1.1.1.2 christos gdb_assert (buf_start[0] == 'T');
1161 1.1.1.2 christos gdb_assert (isxdigit (buf_start[1]));
1162 1.1.1.2 christos gdb_assert (isxdigit (buf_start[2]));
1163 1.1.1.2 christos buf_start[0] = 'S';
1164 1.1.1.2 christos buf_start[3] = '\0';
1165 1.1 christos break;
1166 1.1 christos }
1167 1.1 christos
1168 1.1 christos buf += strlen (buf);
1169 1.1 christos
1170 1.1.1.2 christos scoped_restore_current_thread restore_thread;
1171 1.1 christos
1172 1.1 christos switch_to_thread (the_target, ptid);
1173 1.1 christos
1174 1.1 christos regcache = get_thread_regcache (current_thread, 1);
1175 1.1 christos
1176 1.1 christos if (the_target->stopped_by_watchpoint ())
1177 1.1 christos {
1178 1.1 christos CORE_ADDR addr;
1179 1.1 christos int i;
1180 1.1 christos
1181 1.1 christos memcpy (buf, "watch:", 6);
1182 1.1 christos buf += 6;
1183 1.1 christos
1184 1.1 christos addr = the_target->stopped_data_address ();
1185 1.1 christos
1186 1.1 christos /* Convert each byte of the address into two hexadecimal
1187 1.1 christos chars. Note that we take sizeof (void *) instead of
1188 1.1 christos sizeof (addr); this is to avoid sending a 64-bit
1189 1.1 christos address to a 32-bit GDB. */
1190 1.1 christos for (i = sizeof (void *) * 2; i > 0; i--)
1191 1.1 christos *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
1192 1.1 christos *buf++ = ';';
1193 1.1 christos }
1194 1.1 christos else if (cs.swbreak_feature && target_stopped_by_sw_breakpoint ())
1195 1.1 christos {
1196 1.1 christos sprintf (buf, "swbreak:;");
1197 1.1 christos buf += strlen (buf);
1198 1.1 christos }
1199 1.1 christos else if (cs.hwbreak_feature && target_stopped_by_hw_breakpoint ())
1200 1.1 christos {
1201 1.1 christos sprintf (buf, "hwbreak:;");
1202 1.1 christos buf += strlen (buf);
1203 1.1 christos }
1204 1.1 christos
1205 1.1.1.3 christos /* Handle the expedited registers. */
1206 1.1.1.3 christos for (const std::string &expedited_reg :
1207 1.1.1.3 christos current_target_desc ()->expedite_regs)
1208 1.1.1.3 christos buf = outreg (regcache, find_regno (regcache->tdesc,
1209 1.1.1.3 christos expedited_reg.c_str ()), buf);
1210 1.1 christos *buf = '\0';
1211 1.1 christos
1212 1.1 christos /* Formerly, if the debugger had not used any thread features
1213 1.1 christos we would not burden it with a thread status response. This
1214 1.1 christos was for the benefit of GDB 4.13 and older. However, in
1215 1.1 christos recent GDB versions the check (``if (cont_thread != 0)'')
1216 1.1.1.3 christos does not have the desired effect because of silliness in
1217 1.1 christos the way that the remote protocol handles specifying a
1218 1.1 christos thread. Since thread support relies on qSymbol support
1219 1.1 christos anyway, assume GDB can handle threads. */
1220 1.1 christos
1221 1.1 christos if (using_threads && !disable_packet_Tthread)
1222 1.1 christos {
1223 1.1 christos /* This if (1) ought to be unnecessary. But remote_wait
1224 1.1 christos in GDB will claim this event belongs to inferior_ptid
1225 1.1 christos if we do not specify a thread, and there's no way for
1226 1.1 christos gdbserver to know what inferior_ptid is. */
1227 1.1 christos if (1 || cs.general_thread != ptid)
1228 1.1 christos {
1229 1.1 christos int core = -1;
1230 1.1 christos /* In non-stop, don't change the general thread behind
1231 1.1 christos GDB's back. */
1232 1.1 christos if (!non_stop)
1233 1.1 christos cs.general_thread = ptid;
1234 1.1 christos sprintf (buf, "thread:");
1235 1.1 christos buf += strlen (buf);
1236 1.1 christos buf = write_ptid (buf, ptid);
1237 1.1 christos strcat (buf, ";");
1238 1.1 christos buf += strlen (buf);
1239 1.1 christos
1240 1.1 christos core = target_core_of_thread (ptid);
1241 1.1 christos
1242 1.1 christos if (core != -1)
1243 1.1 christos {
1244 1.1 christos sprintf (buf, "core:");
1245 1.1 christos buf += strlen (buf);
1246 1.1 christos sprintf (buf, "%x", core);
1247 1.1 christos strcat (buf, ";");
1248 1.1 christos buf += strlen (buf);
1249 1.1 christos }
1250 1.1 christos }
1251 1.1 christos }
1252 1.1 christos
1253 1.1.1.2 christos if (current_process ()->dlls_changed)
1254 1.1 christos {
1255 1.1 christos strcpy (buf, "library:;");
1256 1.1 christos buf += strlen (buf);
1257 1.1.1.2 christos current_process ()->dlls_changed = false;
1258 1.1 christos }
1259 1.1 christos }
1260 1.1 christos break;
1261 1.1 christos case TARGET_WAITKIND_EXITED:
1262 1.1 christos if (cs.multi_process)
1263 1.1 christos sprintf (buf, "W%x;process:%x",
1264 1.1.1.2 christos status.exit_status (), ptid.pid ());
1265 1.1 christos else
1266 1.1.1.2 christos sprintf (buf, "W%02x", status.exit_status ());
1267 1.1 christos break;
1268 1.1 christos case TARGET_WAITKIND_SIGNALLED:
1269 1.1 christos if (cs.multi_process)
1270 1.1 christos sprintf (buf, "X%x;process:%x",
1271 1.1.1.2 christos status.sig (), ptid.pid ());
1272 1.1 christos else
1273 1.1.1.2 christos sprintf (buf, "X%02x", status.sig ());
1274 1.1 christos break;
1275 1.1 christos case TARGET_WAITKIND_THREAD_EXITED:
1276 1.1.1.2 christos sprintf (buf, "w%x;", status.exit_status ());
1277 1.1 christos buf += strlen (buf);
1278 1.1 christos buf = write_ptid (buf, ptid);
1279 1.1 christos break;
1280 1.1 christos case TARGET_WAITKIND_NO_RESUMED:
1281 1.1 christos sprintf (buf, "N");
1282 1.1 christos break;
1283 1.1 christos default:
1284 1.1 christos error ("unhandled waitkind");
1285 1.1 christos break;
1286 1.1 christos }
1287 1.1 christos }
1288 1.1 christos
1289 1.1.1.2 christos /* See remote-utils.h. */
1290 1.1.1.2 christos
1291 1.1.1.2 christos const char *
1292 1.1.1.2 christos decode_m_packet_params (const char *from, CORE_ADDR *mem_addr_ptr,
1293 1.1.1.2 christos unsigned int *len_ptr, const char end_marker)
1294 1.1 christos {
1295 1.1.1.2 christos int i = 0;
1296 1.1 christos char ch;
1297 1.1 christos *mem_addr_ptr = *len_ptr = 0;
1298 1.1 christos
1299 1.1 christos while ((ch = from[i++]) != ',')
1300 1.1 christos {
1301 1.1 christos *mem_addr_ptr = *mem_addr_ptr << 4;
1302 1.1 christos *mem_addr_ptr |= fromhex (ch) & 0x0f;
1303 1.1 christos }
1304 1.1 christos
1305 1.1.1.2 christos while ((ch = from[i++]) != end_marker)
1306 1.1 christos {
1307 1.1 christos *len_ptr = *len_ptr << 4;
1308 1.1 christos *len_ptr |= fromhex (ch) & 0x0f;
1309 1.1 christos }
1310 1.1.1.2 christos
1311 1.1.1.2 christos return from + i;
1312 1.1 christos }
1313 1.1 christos
1314 1.1 christos void
1315 1.1.1.2 christos decode_m_packet (const char *from, CORE_ADDR *mem_addr_ptr,
1316 1.1.1.2 christos unsigned int *len_ptr)
1317 1.1 christos {
1318 1.1.1.2 christos decode_m_packet_params (from, mem_addr_ptr, len_ptr, '\0');
1319 1.1.1.2 christos }
1320 1.1 christos
1321 1.1.1.2 christos void
1322 1.1.1.2 christos decode_M_packet (const char *from, CORE_ADDR *mem_addr_ptr,
1323 1.1.1.2 christos unsigned int *len_ptr, unsigned char **to_p)
1324 1.1.1.2 christos {
1325 1.1.1.2 christos from = decode_m_packet_params (from, mem_addr_ptr, len_ptr, ':');
1326 1.1 christos
1327 1.1 christos if (*to_p == NULL)
1328 1.1 christos *to_p = (unsigned char *) xmalloc (*len_ptr);
1329 1.1 christos
1330 1.1.1.2 christos hex2bin (from, *to_p, *len_ptr);
1331 1.1 christos }
1332 1.1 christos
1333 1.1 christos int
1334 1.1 christos decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
1335 1.1 christos unsigned int *len_ptr, unsigned char **to_p)
1336 1.1 christos {
1337 1.1 christos int i = 0;
1338 1.1 christos char ch;
1339 1.1 christos *mem_addr_ptr = *len_ptr = 0;
1340 1.1 christos
1341 1.1 christos while ((ch = from[i++]) != ',')
1342 1.1 christos {
1343 1.1 christos *mem_addr_ptr = *mem_addr_ptr << 4;
1344 1.1 christos *mem_addr_ptr |= fromhex (ch) & 0x0f;
1345 1.1 christos }
1346 1.1 christos
1347 1.1 christos while ((ch = from[i++]) != ':')
1348 1.1 christos {
1349 1.1 christos *len_ptr = *len_ptr << 4;
1350 1.1 christos *len_ptr |= fromhex (ch) & 0x0f;
1351 1.1 christos }
1352 1.1 christos
1353 1.1 christos if (*to_p == NULL)
1354 1.1 christos *to_p = (unsigned char *) xmalloc (*len_ptr);
1355 1.1 christos
1356 1.1 christos if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
1357 1.1 christos *to_p, *len_ptr) != *len_ptr)
1358 1.1 christos return -1;
1359 1.1 christos
1360 1.1 christos return 0;
1361 1.1 christos }
1362 1.1 christos
1363 1.1 christos /* Decode a qXfer write request. */
1364 1.1 christos
1365 1.1 christos int
1366 1.1 christos decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
1367 1.1 christos unsigned int *len, unsigned char *data)
1368 1.1 christos {
1369 1.1 christos char ch;
1370 1.1 christos char *b = buf;
1371 1.1 christos
1372 1.1 christos /* Extract the offset. */
1373 1.1 christos *offset = 0;
1374 1.1 christos while ((ch = *buf++) != ':')
1375 1.1 christos {
1376 1.1 christos *offset = *offset << 4;
1377 1.1 christos *offset |= fromhex (ch) & 0x0f;
1378 1.1 christos }
1379 1.1 christos
1380 1.1 christos /* Get encoded data. */
1381 1.1 christos packet_len -= buf - b;
1382 1.1 christos *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
1383 1.1 christos data, packet_len);
1384 1.1 christos return 0;
1385 1.1 christos }
1386 1.1 christos
1387 1.1 christos /* Decode the parameters of a qSearch:memory packet. */
1388 1.1 christos
1389 1.1 christos int
1390 1.1 christos decode_search_memory_packet (const char *buf, int packet_len,
1391 1.1 christos CORE_ADDR *start_addrp,
1392 1.1 christos CORE_ADDR *search_space_lenp,
1393 1.1 christos gdb_byte *pattern, unsigned int *pattern_lenp)
1394 1.1 christos {
1395 1.1 christos const char *p = buf;
1396 1.1 christos
1397 1.1 christos p = decode_address_to_semicolon (start_addrp, p);
1398 1.1 christos p = decode_address_to_semicolon (search_space_lenp, p);
1399 1.1 christos packet_len -= p - buf;
1400 1.1 christos *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
1401 1.1 christos pattern, packet_len);
1402 1.1 christos return 0;
1403 1.1 christos }
1404 1.1 christos
1405 1.1 christos static void
1406 1.1 christos free_sym_cache (struct sym_cache *sym)
1407 1.1 christos {
1408 1.1 christos if (sym != NULL)
1409 1.1 christos {
1410 1.1 christos free (sym->name);
1411 1.1 christos free (sym);
1412 1.1 christos }
1413 1.1 christos }
1414 1.1 christos
1415 1.1 christos void
1416 1.1 christos clear_symbol_cache (struct sym_cache **symcache_p)
1417 1.1 christos {
1418 1.1 christos struct sym_cache *sym, *next;
1419 1.1 christos
1420 1.1 christos /* Check the cache first. */
1421 1.1 christos for (sym = *symcache_p; sym; sym = next)
1422 1.1 christos {
1423 1.1 christos next = sym->next;
1424 1.1 christos free_sym_cache (sym);
1425 1.1 christos }
1426 1.1 christos
1427 1.1 christos *symcache_p = NULL;
1428 1.1 christos }
1429 1.1 christos
1430 1.1 christos /* Get the address of NAME, and return it in ADDRP if found. if
1431 1.1 christos MAY_ASK_GDB is false, assume symbol cache misses are failures.
1432 1.1 christos Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
1433 1.1 christos
1434 1.1 christos int
1435 1.1 christos look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
1436 1.1 christos {
1437 1.1 christos client_state &cs = get_client_state ();
1438 1.1 christos char *p, *q;
1439 1.1 christos int len;
1440 1.1 christos struct sym_cache *sym;
1441 1.1 christos struct process_info *proc;
1442 1.1 christos
1443 1.1 christos proc = current_process ();
1444 1.1 christos
1445 1.1 christos /* Check the cache first. */
1446 1.1 christos for (sym = proc->symbol_cache; sym; sym = sym->next)
1447 1.1 christos if (strcmp (name, sym->name) == 0)
1448 1.1 christos {
1449 1.1 christos *addrp = sym->addr;
1450 1.1 christos return 1;
1451 1.1 christos }
1452 1.1 christos
1453 1.1 christos /* It might not be an appropriate time to look up a symbol,
1454 1.1 christos e.g. while we're trying to fetch registers. */
1455 1.1 christos if (!may_ask_gdb)
1456 1.1 christos return 0;
1457 1.1 christos
1458 1.1 christos /* Send the request. */
1459 1.1 christos strcpy (cs.own_buf, "qSymbol:");
1460 1.1 christos bin2hex ((const gdb_byte *) name, cs.own_buf + strlen ("qSymbol:"),
1461 1.1 christos strlen (name));
1462 1.1 christos if (putpkt (cs.own_buf) < 0)
1463 1.1 christos return -1;
1464 1.1 christos
1465 1.1 christos /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1466 1.1 christos len = getpkt (cs.own_buf);
1467 1.1 christos if (len < 0)
1468 1.1 christos return -1;
1469 1.1 christos
1470 1.1 christos /* We ought to handle pretty much any packet at this point while we
1471 1.1 christos wait for the qSymbol "response". That requires re-entering the
1472 1.1 christos main loop. For now, this is an adequate approximation; allow
1473 1.1 christos GDB to read from memory and handle 'v' packets (for vFile transfers)
1474 1.1 christos while it figures out the address of the symbol. */
1475 1.1 christos while (1)
1476 1.1 christos {
1477 1.1 christos if (cs.own_buf[0] == 'm')
1478 1.1 christos {
1479 1.1 christos CORE_ADDR mem_addr;
1480 1.1 christos unsigned char *mem_buf;
1481 1.1 christos unsigned int mem_len;
1482 1.1 christos
1483 1.1 christos decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1484 1.1 christos mem_buf = (unsigned char *) xmalloc (mem_len);
1485 1.1 christos if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1486 1.1 christos bin2hex (mem_buf, cs.own_buf, mem_len);
1487 1.1 christos else
1488 1.1 christos write_enn (cs.own_buf);
1489 1.1 christos free (mem_buf);
1490 1.1 christos if (putpkt (cs.own_buf) < 0)
1491 1.1 christos return -1;
1492 1.1 christos }
1493 1.1 christos else if (cs.own_buf[0] == 'v')
1494 1.1 christos {
1495 1.1 christos int new_len = -1;
1496 1.1 christos handle_v_requests (cs.own_buf, len, &new_len);
1497 1.1 christos if (new_len != -1)
1498 1.1 christos putpkt_binary (cs.own_buf, new_len);
1499 1.1 christos else
1500 1.1 christos putpkt (cs.own_buf);
1501 1.1 christos }
1502 1.1 christos else
1503 1.1 christos break;
1504 1.1 christos len = getpkt (cs.own_buf);
1505 1.1 christos if (len < 0)
1506 1.1 christos return -1;
1507 1.1 christos }
1508 1.1 christos
1509 1.1 christos if (!startswith (cs.own_buf, "qSymbol:"))
1510 1.1 christos {
1511 1.1 christos warning ("Malformed response to qSymbol, ignoring: %s", cs.own_buf);
1512 1.1 christos return -1;
1513 1.1 christos }
1514 1.1 christos
1515 1.1 christos p = cs.own_buf + strlen ("qSymbol:");
1516 1.1 christos q = p;
1517 1.1 christos while (*q && *q != ':')
1518 1.1 christos q++;
1519 1.1 christos
1520 1.1 christos /* Make sure we found a value for the symbol. */
1521 1.1 christos if (p == q || *q == '\0')
1522 1.1 christos return 0;
1523 1.1 christos
1524 1.1 christos decode_address (addrp, p, q - p);
1525 1.1 christos
1526 1.1 christos /* Save the symbol in our cache. */
1527 1.1 christos sym = XNEW (struct sym_cache);
1528 1.1 christos sym->name = xstrdup (name);
1529 1.1 christos sym->addr = *addrp;
1530 1.1 christos sym->next = proc->symbol_cache;
1531 1.1 christos proc->symbol_cache = sym;
1532 1.1 christos
1533 1.1 christos return 1;
1534 1.1 christos }
1535 1.1 christos
1536 1.1 christos /* Relocate an instruction to execute at a different address. OLDLOC
1537 1.1 christos is the address in the inferior memory where the instruction to
1538 1.1 christos relocate is currently at. On input, TO points to the destination
1539 1.1 christos where we want the instruction to be copied (and possibly adjusted)
1540 1.1 christos to. On output, it points to one past the end of the resulting
1541 1.1 christos instruction(s). The effect of executing the instruction at TO
1542 1.1 christos shall be the same as if executing it at OLDLOC. For example, call
1543 1.1 christos instructions that implicitly push the return address on the stack
1544 1.1 christos should be adjusted to return to the instruction after OLDLOC;
1545 1.1 christos relative branches, and other PC-relative instructions need the
1546 1.1 christos offset adjusted; etc. Returns 0 on success, -1 on failure. */
1547 1.1 christos
1548 1.1 christos int
1549 1.1 christos relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
1550 1.1 christos {
1551 1.1 christos client_state &cs = get_client_state ();
1552 1.1 christos int len;
1553 1.1 christos ULONGEST written = 0;
1554 1.1 christos
1555 1.1 christos /* Send the request. */
1556 1.1 christos sprintf (cs.own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
1557 1.1 christos paddress (*to));
1558 1.1 christos if (putpkt (cs.own_buf) < 0)
1559 1.1 christos return -1;
1560 1.1 christos
1561 1.1 christos /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
1562 1.1 christos len = getpkt (cs.own_buf);
1563 1.1 christos if (len < 0)
1564 1.1 christos return -1;
1565 1.1 christos
1566 1.1 christos /* We ought to handle pretty much any packet at this point while we
1567 1.1 christos wait for the qRelocInsn "response". That requires re-entering
1568 1.1 christos the main loop. For now, this is an adequate approximation; allow
1569 1.1 christos GDB to access memory. */
1570 1.1 christos while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M' || cs.own_buf[0] == 'X')
1571 1.1 christos {
1572 1.1 christos CORE_ADDR mem_addr;
1573 1.1 christos unsigned char *mem_buf = NULL;
1574 1.1 christos unsigned int mem_len;
1575 1.1 christos
1576 1.1 christos if (cs.own_buf[0] == 'm')
1577 1.1 christos {
1578 1.1 christos decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
1579 1.1 christos mem_buf = (unsigned char *) xmalloc (mem_len);
1580 1.1 christos if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
1581 1.1 christos bin2hex (mem_buf, cs.own_buf, mem_len);
1582 1.1 christos else
1583 1.1 christos write_enn (cs.own_buf);
1584 1.1 christos }
1585 1.1 christos else if (cs.own_buf[0] == 'X')
1586 1.1 christos {
1587 1.1 christos if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
1588 1.1 christos &mem_len, &mem_buf) < 0
1589 1.1 christos || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
1590 1.1 christos write_enn (cs.own_buf);
1591 1.1 christos else
1592 1.1 christos write_ok (cs.own_buf);
1593 1.1 christos }
1594 1.1 christos else
1595 1.1 christos {
1596 1.1 christos decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
1597 1.1 christos if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
1598 1.1 christos write_ok (cs.own_buf);
1599 1.1 christos else
1600 1.1 christos write_enn (cs.own_buf);
1601 1.1 christos }
1602 1.1 christos free (mem_buf);
1603 1.1 christos if (putpkt (cs.own_buf) < 0)
1604 1.1 christos return -1;
1605 1.1 christos len = getpkt (cs.own_buf);
1606 1.1 christos if (len < 0)
1607 1.1 christos return -1;
1608 1.1 christos }
1609 1.1 christos
1610 1.1 christos if (cs.own_buf[0] == 'E')
1611 1.1 christos {
1612 1.1 christos warning ("An error occurred while relocating an instruction: %s",
1613 1.1 christos cs.own_buf);
1614 1.1 christos return -1;
1615 1.1 christos }
1616 1.1 christos
1617 1.1 christos if (!startswith (cs.own_buf, "qRelocInsn:"))
1618 1.1 christos {
1619 1.1 christos warning ("Malformed response to qRelocInsn, ignoring: %s",
1620 1.1 christos cs.own_buf);
1621 1.1 christos return -1;
1622 1.1 christos }
1623 1.1 christos
1624 1.1 christos unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
1625 1.1 christos
1626 1.1 christos *to += written;
1627 1.1 christos return 0;
1628 1.1 christos }
1629 1.1 christos
1630 1.1 christos void
1631 1.1 christos monitor_output (const char *msg)
1632 1.1 christos {
1633 1.1 christos int len = strlen (msg);
1634 1.1 christos char *buf = (char *) xmalloc (len * 2 + 2);
1635 1.1 christos
1636 1.1 christos buf[0] = 'O';
1637 1.1 christos bin2hex ((const gdb_byte *) msg, buf + 1, len);
1638 1.1 christos
1639 1.1 christos putpkt (buf);
1640 1.1 christos free (buf);
1641 1.1 christos }
1642 1.1 christos
1643 1.1 christos #endif
1644