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