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