Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
0b302171 JB |
2 | Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software |
3 | Foundation, Inc. | |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "server.h" | |
21 | ||
68070c10 | 22 | #if HAVE_UNISTD_H |
a9fa9f7d | 23 | #include <unistd.h> |
68070c10 PA |
24 | #endif |
25 | #if HAVE_SIGNAL_H | |
a9fa9f7d | 26 | #include <signal.h> |
68070c10 | 27 | #endif |
b80864fb | 28 | #if HAVE_SYS_WAIT_H |
a9fa9f7d | 29 | #include <sys/wait.h> |
b80864fb | 30 | #endif |
a9fa9f7d | 31 | |
95954743 PA |
32 | ptid_t cont_thread; |
33 | ptid_t general_thread; | |
5b1c542e | 34 | |
0d62e5e8 DJ |
35 | int server_waiting; |
36 | ||
2d717e4f | 37 | static int extended_protocol; |
2d717e4f DJ |
38 | static int response_needed; |
39 | static int exit_requested; | |
40 | ||
03f2bd59 JK |
41 | /* --once: Exit after the first connection has closed. */ |
42 | int run_once; | |
43 | ||
95954743 | 44 | int multi_process; |
bd99dc85 PA |
45 | int non_stop; |
46 | ||
03583c20 UW |
47 | /* Whether we should attempt to disable the operating system's address |
48 | space randomization feature before starting an inferior. */ | |
49 | int disable_randomization = 1; | |
50 | ||
ccd213ac | 51 | static char **program_argv, **wrapper_argv; |
2d717e4f | 52 | |
c74d0ad8 DJ |
53 | /* Enable miscellaneous debugging output. The name is historical - it |
54 | was originally used to debug LinuxThreads support. */ | |
55 | int debug_threads; | |
56 | ||
aa5ca48f DE |
57 | /* Enable debugging of h/w breakpoint/watchpoint support. */ |
58 | int debug_hw_points; | |
59 | ||
89be2091 DJ |
60 | int pass_signals[TARGET_SIGNAL_LAST]; |
61 | ||
c906108c | 62 | jmp_buf toplevel; |
c906108c | 63 | |
9b4b61c8 UW |
64 | const char *gdbserver_xmltarget; |
65 | ||
a9fa9f7d DJ |
66 | /* The PID of the originally created or attached inferior. Used to |
67 | send signals to the process when GDB sends us an asynchronous interrupt | |
68 | (user hitting Control-C in the client), and to wait for the child to exit | |
69 | when no longer debugging it. */ | |
70 | ||
a1928bad | 71 | unsigned long signal_pid; |
a9fa9f7d | 72 | |
290fadea RS |
73 | #ifdef SIGTTOU |
74 | /* A file descriptor for the controlling terminal. */ | |
75 | int terminal_fd; | |
76 | ||
77 | /* TERMINAL_FD's original foreground group. */ | |
78 | pid_t old_foreground_pgrp; | |
79 | ||
80 | /* Hand back terminal ownership to the original foreground group. */ | |
81 | ||
82 | static void | |
83 | restore_old_foreground_pgrp (void) | |
84 | { | |
85 | tcsetpgrp (terminal_fd, old_foreground_pgrp); | |
86 | } | |
87 | #endif | |
88 | ||
ec56be1b PA |
89 | /* Set if you want to disable optional thread related packets support |
90 | in gdbserver, for the sake of testing GDB against stubs that don't | |
91 | support them. */ | |
92 | int disable_packet_vCont; | |
93 | int disable_packet_Tthread; | |
94 | int disable_packet_qC; | |
95 | int disable_packet_qfThreadInfo; | |
96 | ||
5b1c542e PA |
97 | /* Last status reported to GDB. */ |
98 | static struct target_waitstatus last_status; | |
95954743 | 99 | static ptid_t last_ptid; |
5b1c542e | 100 | |
bd99dc85 PA |
101 | static char *own_buf; |
102 | static unsigned char *mem_buf; | |
103 | ||
104 | /* Structure holding information relative to a single stop reply. We | |
105 | keep a queue of these (really a singly-linked list) to push to GDB | |
106 | in non-stop mode. */ | |
107 | struct vstop_notif | |
108 | { | |
109 | /* Pointer to next in list. */ | |
110 | struct vstop_notif *next; | |
111 | ||
112 | /* Thread or process that got the event. */ | |
95954743 | 113 | ptid_t ptid; |
bd99dc85 PA |
114 | |
115 | /* Event info. */ | |
116 | struct target_waitstatus status; | |
117 | }; | |
118 | ||
119 | /* The pending stop replies list head. */ | |
120 | static struct vstop_notif *notif_queue = NULL; | |
121 | ||
122 | /* Put a stop reply to the stop reply queue. */ | |
123 | ||
124 | static void | |
95954743 | 125 | queue_stop_reply (ptid_t ptid, struct target_waitstatus *status) |
bd99dc85 PA |
126 | { |
127 | struct vstop_notif *new_notif; | |
128 | ||
cfdee94a | 129 | new_notif = xmalloc (sizeof (*new_notif)); |
bd99dc85 PA |
130 | new_notif->next = NULL; |
131 | new_notif->ptid = ptid; | |
132 | new_notif->status = *status; | |
133 | ||
134 | if (notif_queue) | |
135 | { | |
136 | struct vstop_notif *tail; | |
137 | for (tail = notif_queue; | |
138 | tail && tail->next; | |
139 | tail = tail->next) | |
140 | ; | |
141 | tail->next = new_notif; | |
142 | } | |
143 | else | |
144 | notif_queue = new_notif; | |
145 | ||
146 | if (remote_debug) | |
147 | { | |
148 | int i = 0; | |
149 | struct vstop_notif *n; | |
150 | ||
151 | for (n = notif_queue; n; n = n->next) | |
152 | i++; | |
153 | ||
154 | fprintf (stderr, "pending stop replies: %d\n", i); | |
155 | } | |
156 | } | |
157 | ||
158 | /* Place an event in the stop reply queue, and push a notification if | |
159 | we aren't sending one yet. */ | |
160 | ||
161 | void | |
95954743 | 162 | push_event (ptid_t ptid, struct target_waitstatus *status) |
bd99dc85 | 163 | { |
d20a8ad9 PA |
164 | gdb_assert (status->kind != TARGET_WAITKIND_IGNORE); |
165 | ||
bd99dc85 PA |
166 | queue_stop_reply (ptid, status); |
167 | ||
168 | /* If this is the first stop reply in the queue, then inform GDB | |
169 | about it, by sending a Stop notification. */ | |
170 | if (notif_queue->next == NULL) | |
171 | { | |
172 | char *p = own_buf; | |
173 | strcpy (p, "Stop:"); | |
174 | p += strlen (p); | |
175 | prepare_resume_reply (p, | |
176 | notif_queue->ptid, ¬if_queue->status); | |
177 | putpkt_notif (own_buf); | |
178 | } | |
179 | } | |
180 | ||
95954743 PA |
181 | /* Get rid of the currently pending stop replies for PID. If PID is |
182 | -1, then apply to all processes. */ | |
bd99dc85 PA |
183 | |
184 | static void | |
95954743 | 185 | discard_queued_stop_replies (int pid) |
bd99dc85 | 186 | { |
95954743 | 187 | struct vstop_notif *prev = NULL, *reply, *next; |
bd99dc85 | 188 | |
95954743 | 189 | for (reply = notif_queue; reply; reply = next) |
bd99dc85 | 190 | { |
95954743 PA |
191 | next = reply->next; |
192 | ||
193 | if (pid == -1 | |
194 | || ptid_get_pid (reply->ptid) == pid) | |
195 | { | |
196 | if (reply == notif_queue) | |
197 | notif_queue = next; | |
198 | else | |
199 | prev->next = reply->next; | |
bd99dc85 | 200 | |
95954743 PA |
201 | free (reply); |
202 | } | |
203 | else | |
204 | prev = reply; | |
bd99dc85 PA |
205 | } |
206 | } | |
207 | ||
208 | /* If there are more stop replies to push, push one now. */ | |
209 | ||
210 | static void | |
211 | send_next_stop_reply (char *own_buf) | |
212 | { | |
213 | if (notif_queue) | |
214 | prepare_resume_reply (own_buf, | |
215 | notif_queue->ptid, | |
216 | ¬if_queue->status); | |
217 | else | |
218 | write_ok (own_buf); | |
219 | } | |
220 | ||
2d717e4f DJ |
221 | static int |
222 | target_running (void) | |
223 | { | |
224 | return all_threads.head != NULL; | |
225 | } | |
226 | ||
fc620387 | 227 | static int |
5b1c542e | 228 | start_inferior (char **argv) |
c906108c | 229 | { |
ccd213ac | 230 | char **new_argv = argv; |
2d717e4f | 231 | |
ccd213ac DJ |
232 | if (wrapper_argv != NULL) |
233 | { | |
234 | int i, count = 1; | |
235 | ||
236 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
237 | count++; | |
238 | for (i = 0; argv[i] != NULL; i++) | |
239 | count++; | |
240 | new_argv = alloca (sizeof (char *) * count); | |
241 | count = 0; | |
242 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
243 | new_argv[count++] = wrapper_argv[i]; | |
244 | for (i = 0; argv[i] != NULL; i++) | |
245 | new_argv[count++] = argv[i]; | |
246 | new_argv[count] = NULL; | |
247 | } | |
248 | ||
65730243 DE |
249 | if (debug_threads) |
250 | { | |
251 | int i; | |
252 | for (i = 0; new_argv[i]; ++i) | |
253 | fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]); | |
254 | fflush (stderr); | |
255 | } | |
256 | ||
b80864fb | 257 | #ifdef SIGTTOU |
a9fa9f7d DJ |
258 | signal (SIGTTOU, SIG_DFL); |
259 | signal (SIGTTIN, SIG_DFL); | |
b80864fb | 260 | #endif |
a9fa9f7d | 261 | |
ccd213ac | 262 | signal_pid = create_inferior (new_argv[0], new_argv); |
0d62e5e8 | 263 | |
c588c53c MS |
264 | /* FIXME: we don't actually know at this point that the create |
265 | actually succeeded. We won't know that until we wait. */ | |
a1928bad | 266 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
a9fa9f7d | 267 | signal_pid); |
b80864fb | 268 | fflush (stderr); |
a9fa9f7d | 269 | |
b80864fb | 270 | #ifdef SIGTTOU |
a9fa9f7d DJ |
271 | signal (SIGTTOU, SIG_IGN); |
272 | signal (SIGTTIN, SIG_IGN); | |
290fadea RS |
273 | terminal_fd = fileno (stderr); |
274 | old_foreground_pgrp = tcgetpgrp (terminal_fd); | |
275 | tcsetpgrp (terminal_fd, signal_pid); | |
276 | atexit (restore_old_foreground_pgrp); | |
b80864fb | 277 | #endif |
c906108c | 278 | |
ccd213ac DJ |
279 | if (wrapper_argv != NULL) |
280 | { | |
281 | struct thread_resume resume_info; | |
ccd213ac | 282 | |
95954743 | 283 | resume_info.thread = pid_to_ptid (signal_pid); |
bd99dc85 | 284 | resume_info.kind = resume_continue; |
ccd213ac | 285 | resume_info.sig = 0; |
ccd213ac | 286 | |
06db92f0 | 287 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
bd99dc85 | 288 | |
5b1c542e PA |
289 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
290 | return signal_pid; | |
ccd213ac DJ |
291 | |
292 | do | |
293 | { | |
2bd7c093 | 294 | (*the_target->resume) (&resume_info, 1); |
ccd213ac | 295 | |
06db92f0 | 296 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
5b1c542e PA |
297 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
298 | return signal_pid; | |
d20a8ad9 PA |
299 | |
300 | current_inferior->last_resume_kind = resume_stop; | |
301 | current_inferior->last_status = last_status; | |
ccd213ac | 302 | } |
5b1c542e | 303 | while (last_status.value.sig != TARGET_SIGNAL_TRAP); |
ccd213ac | 304 | |
d20a8ad9 PA |
305 | current_inferior->last_resume_kind = resume_stop; |
306 | current_inferior->last_status = last_status; | |
5b1c542e | 307 | return signal_pid; |
ccd213ac DJ |
308 | } |
309 | ||
5b1c542e PA |
310 | /* Wait till we are at 1st instruction in program, return new pid |
311 | (assuming success). */ | |
95954743 | 312 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
5b1c542e | 313 | |
d20a8ad9 PA |
314 | if (last_status.kind != TARGET_WAITKIND_EXITED |
315 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
316 | { | |
317 | current_inferior->last_resume_kind = resume_stop; | |
318 | current_inferior->last_status = last_status; | |
319 | } | |
320 | ||
5b1c542e | 321 | return signal_pid; |
c906108c SS |
322 | } |
323 | ||
45b7b345 | 324 | static int |
5b1c542e | 325 | attach_inferior (int pid) |
45b7b345 DJ |
326 | { |
327 | /* myattach should return -1 if attaching is unsupported, | |
328 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 329 | |
45b7b345 DJ |
330 | if (myattach (pid) != 0) |
331 | return -1; | |
332 | ||
6910d122 | 333 | fprintf (stderr, "Attached; pid = %d\n", pid); |
b80864fb | 334 | fflush (stderr); |
6910d122 | 335 | |
a9fa9f7d DJ |
336 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
337 | attach function, so that it can be the main thread instead of | |
338 | whichever we were told to attach to. */ | |
339 | signal_pid = pid; | |
340 | ||
7d5d4e98 PA |
341 | /* Clear this so the backend doesn't get confused, thinking |
342 | CONT_THREAD died, and it needs to resume all threads. */ | |
343 | cont_thread = null_ptid; | |
344 | ||
bd99dc85 PA |
345 | if (!non_stop) |
346 | { | |
95954743 | 347 | last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0); |
bd99dc85 PA |
348 | |
349 | /* GDB knows to ignore the first SIGSTOP after attaching to a running | |
350 | process using the "attach" command, but this is different; it's | |
351 | just using "target remote". Pretend it's just starting up. */ | |
352 | if (last_status.kind == TARGET_WAITKIND_STOPPED | |
353 | && last_status.value.sig == TARGET_SIGNAL_STOP) | |
354 | last_status.value.sig = TARGET_SIGNAL_TRAP; | |
d20a8ad9 PA |
355 | |
356 | current_inferior->last_resume_kind = resume_stop; | |
357 | current_inferior->last_status = last_status; | |
bd99dc85 | 358 | } |
9db87ebd | 359 | |
45b7b345 DJ |
360 | return 0; |
361 | } | |
362 | ||
c906108c | 363 | extern int remote_debug; |
ce3a066d | 364 | |
0876f84a DJ |
365 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
366 | or -1 otherwise. */ | |
367 | ||
368 | static int | |
d08aafef | 369 | decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len) |
0876f84a | 370 | { |
d08aafef PA |
371 | /* After the read marker and annex, qXfer looks like a |
372 | traditional 'm' packet. */ | |
373 | decode_m_packet (buf, ofs, len); | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | static int | |
379 | decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset) | |
380 | { | |
381 | /* Extract and NUL-terminate the object. */ | |
382 | *object = buf; | |
383 | while (*buf && *buf != ':') | |
384 | buf++; | |
385 | if (*buf == '\0') | |
386 | return -1; | |
387 | *buf++ = 0; | |
388 | ||
389 | /* Extract and NUL-terminate the read/write action. */ | |
390 | *rw = buf; | |
391 | while (*buf && *buf != ':') | |
392 | buf++; | |
393 | if (*buf == '\0') | |
394 | return -1; | |
395 | *buf++ = 0; | |
396 | ||
0876f84a DJ |
397 | /* Extract and NUL-terminate the annex. */ |
398 | *annex = buf; | |
399 | while (*buf && *buf != ':') | |
400 | buf++; | |
401 | if (*buf == '\0') | |
402 | return -1; | |
403 | *buf++ = 0; | |
404 | ||
d08aafef | 405 | *offset = buf; |
0876f84a DJ |
406 | return 0; |
407 | } | |
408 | ||
409 | /* Write the response to a successful qXfer read. Returns the | |
410 | length of the (binary) data stored in BUF, corresponding | |
411 | to as much of DATA/LEN as we could fit. IS_MORE controls | |
412 | the first character of the response. */ | |
413 | static int | |
23181151 | 414 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
0876f84a DJ |
415 | { |
416 | int out_len; | |
417 | ||
418 | if (is_more) | |
419 | buf[0] = 'm'; | |
420 | else | |
421 | buf[0] = 'l'; | |
422 | ||
423 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, | |
424 | PBUFSIZ - 2) + 1; | |
425 | } | |
426 | ||
89be2091 | 427 | /* Handle all of the extended 'Q' packets. */ |
ae1ada35 DE |
428 | |
429 | static void | |
89be2091 DJ |
430 | handle_general_set (char *own_buf) |
431 | { | |
432 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) | |
433 | { | |
434 | int numsigs = (int) TARGET_SIGNAL_LAST, i; | |
435 | const char *p = own_buf + strlen ("QPassSignals:"); | |
436 | CORE_ADDR cursig; | |
437 | ||
438 | p = decode_address_to_semicolon (&cursig, p); | |
439 | for (i = 0; i < numsigs; i++) | |
440 | { | |
441 | if (i == cursig) | |
442 | { | |
443 | pass_signals[i] = 1; | |
444 | if (*p == '\0') | |
445 | /* Keep looping, to clear the remaining signals. */ | |
446 | cursig = -1; | |
447 | else | |
448 | p = decode_address_to_semicolon (&cursig, p); | |
449 | } | |
450 | else | |
451 | pass_signals[i] = 0; | |
452 | } | |
453 | strcpy (own_buf, "OK"); | |
454 | return; | |
455 | } | |
456 | ||
a6f3e723 SL |
457 | if (strcmp (own_buf, "QStartNoAckMode") == 0) |
458 | { | |
459 | if (remote_debug) | |
460 | { | |
461 | fprintf (stderr, "[noack mode enabled]\n"); | |
462 | fflush (stderr); | |
463 | } | |
464 | ||
465 | noack_mode = 1; | |
466 | write_ok (own_buf); | |
467 | return; | |
468 | } | |
469 | ||
bd99dc85 PA |
470 | if (strncmp (own_buf, "QNonStop:", 9) == 0) |
471 | { | |
472 | char *mode = own_buf + 9; | |
473 | int req = -1; | |
474 | char *req_str; | |
475 | ||
476 | if (strcmp (mode, "0") == 0) | |
477 | req = 0; | |
478 | else if (strcmp (mode, "1") == 0) | |
479 | req = 1; | |
480 | else | |
481 | { | |
482 | /* We don't know what this mode is, so complain to | |
483 | GDB. */ | |
484 | fprintf (stderr, "Unknown non-stop mode requested: %s\n", | |
485 | own_buf); | |
486 | write_enn (own_buf); | |
487 | return; | |
488 | } | |
489 | ||
490 | req_str = req ? "non-stop" : "all-stop"; | |
491 | if (start_non_stop (req) != 0) | |
492 | { | |
493 | fprintf (stderr, "Setting %s mode failed\n", req_str); | |
494 | write_enn (own_buf); | |
495 | return; | |
496 | } | |
497 | ||
498 | non_stop = req; | |
499 | ||
500 | if (remote_debug) | |
501 | fprintf (stderr, "[%s mode enabled]\n", req_str); | |
502 | ||
503 | write_ok (own_buf); | |
504 | return; | |
505 | } | |
506 | ||
03583c20 UW |
507 | if (strncmp ("QDisableRandomization:", own_buf, |
508 | strlen ("QDisableRandomization:")) == 0) | |
509 | { | |
510 | char *packet = own_buf + strlen ("QDisableRandomization:"); | |
511 | ULONGEST setting; | |
512 | ||
513 | unpack_varlen_hex (packet, &setting); | |
514 | disable_randomization = setting; | |
515 | ||
516 | if (remote_debug) | |
517 | { | |
518 | if (disable_randomization) | |
519 | fprintf (stderr, "[address space randomization disabled]\n"); | |
520 | else | |
521 | fprintf (stderr, "[address space randomization enabled]\n"); | |
522 | } | |
523 | ||
524 | write_ok (own_buf); | |
525 | return; | |
526 | } | |
527 | ||
219f2f23 PA |
528 | if (target_supports_tracepoints () |
529 | && handle_tracepoint_general_set (own_buf)) | |
530 | return; | |
531 | ||
89be2091 DJ |
532 | /* Otherwise we didn't know what packet it was. Say we didn't |
533 | understand it. */ | |
534 | own_buf[0] = 0; | |
535 | } | |
536 | ||
23181151 | 537 | static const char * |
fb1e4ffc | 538 | get_features_xml (const char *annex) |
23181151 | 539 | { |
9b4b61c8 UW |
540 | /* gdbserver_xmltarget defines what to return when looking |
541 | for the "target.xml" file. Its contents can either be | |
542 | verbatim XML code (prefixed with a '@') or else the name | |
543 | of the actual XML file to be used in place of "target.xml". | |
fb1e4ffc | 544 | |
9b4b61c8 UW |
545 | This variable is set up from the auto-generated |
546 | init_registers_... routine for the current target. */ | |
fb1e4ffc | 547 | |
9b4b61c8 | 548 | if (gdbserver_xmltarget |
221c031f | 549 | && strcmp (annex, "target.xml") == 0) |
23181151 | 550 | { |
9b4b61c8 UW |
551 | if (*gdbserver_xmltarget == '@') |
552 | return gdbserver_xmltarget + 1; | |
23181151 | 553 | else |
9b4b61c8 | 554 | annex = gdbserver_xmltarget; |
23181151 DJ |
555 | } |
556 | ||
9b4b61c8 UW |
557 | #ifdef USE_XML |
558 | { | |
559 | extern const char *const xml_builtin[][2]; | |
560 | int i; | |
561 | ||
562 | /* Look for the annex. */ | |
563 | for (i = 0; xml_builtin[i][0] != NULL; i++) | |
564 | if (strcmp (annex, xml_builtin[i][0]) == 0) | |
565 | break; | |
566 | ||
567 | if (xml_builtin[i][0] != NULL) | |
568 | return xml_builtin[i][1]; | |
569 | } | |
570 | #endif | |
571 | ||
572 | return NULL; | |
23181151 DJ |
573 | } |
574 | ||
c74d0ad8 DJ |
575 | void |
576 | monitor_show_help (void) | |
577 | { | |
578 | monitor_output ("The following monitor commands are supported:\n"); | |
579 | monitor_output (" set debug <0|1>\n"); | |
1b3f6016 | 580 | monitor_output (" Enable general debugging messages\n"); |
aa5ca48f DE |
581 | monitor_output (" set debug-hw-points <0|1>\n"); |
582 | monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n"); | |
c74d0ad8 DJ |
583 | monitor_output (" set remote-debug <0|1>\n"); |
584 | monitor_output (" Enable remote protocol debugging messages\n"); | |
ecd7ecbc DJ |
585 | monitor_output (" exit\n"); |
586 | monitor_output (" Quit GDBserver\n"); | |
c74d0ad8 DJ |
587 | } |
588 | ||
764880b7 PA |
589 | /* Read trace frame or inferior memory. Returns the number of bytes |
590 | actually read, zero when no further transfer is possible, and -1 on | |
591 | error. Return of a positive value smaller than LEN does not | |
592 | indicate there's no more to be read, only the end of the transfer. | |
593 | E.g., when GDB reads memory from a traceframe, a first request may | |
594 | be served from a memory block that does not cover the whole request | |
595 | length. A following request gets the rest served from either | |
596 | another block (of the same traceframe) or from the read-only | |
597 | regions. */ | |
219f2f23 PA |
598 | |
599 | static int | |
90d74c30 | 600 | gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
219f2f23 | 601 | { |
764880b7 | 602 | int res; |
90d74c30 | 603 | |
219f2f23 PA |
604 | if (current_traceframe >= 0) |
605 | { | |
606 | ULONGEST nbytes; | |
607 | ULONGEST length = len; | |
608 | ||
609 | if (traceframe_read_mem (current_traceframe, | |
610 | memaddr, myaddr, len, &nbytes)) | |
611 | return EIO; | |
612 | /* Data read from trace buffer, we're done. */ | |
764880b7 PA |
613 | if (nbytes > 0) |
614 | return nbytes; | |
219f2f23 | 615 | if (!in_readonly_region (memaddr, length)) |
764880b7 | 616 | return -1; |
219f2f23 PA |
617 | /* Otherwise we have a valid readonly case, fall through. */ |
618 | /* (assume no half-trace half-real blocks for now) */ | |
619 | } | |
620 | ||
764880b7 PA |
621 | res = prepare_to_access_memory (); |
622 | if (res == 0) | |
90d74c30 | 623 | { |
764880b7 | 624 | res = read_inferior_memory (memaddr, myaddr, len); |
0146f85b | 625 | done_accessing_memory (); |
90d74c30 | 626 | |
764880b7 PA |
627 | return res == 0 ? len : -1; |
628 | } | |
629 | else | |
630 | return -1; | |
219f2f23 PA |
631 | } |
632 | ||
633 | /* Write trace frame or inferior memory. Actually, writing to trace | |
634 | frames is forbidden. */ | |
635 | ||
636 | static int | |
90d74c30 | 637 | gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
219f2f23 PA |
638 | { |
639 | if (current_traceframe >= 0) | |
640 | return EIO; | |
641 | else | |
90d74c30 PA |
642 | { |
643 | int ret; | |
644 | ||
645 | ret = prepare_to_access_memory (); | |
646 | if (ret == 0) | |
647 | { | |
648 | ret = write_inferior_memory (memaddr, myaddr, len); | |
0146f85b | 649 | done_accessing_memory (); |
90d74c30 PA |
650 | } |
651 | return ret; | |
652 | } | |
219f2f23 PA |
653 | } |
654 | ||
08388c79 DE |
655 | /* Subroutine of handle_search_memory to simplify it. */ |
656 | ||
657 | static int | |
658 | handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len, | |
659 | gdb_byte *pattern, unsigned pattern_len, | |
660 | gdb_byte *search_buf, | |
661 | unsigned chunk_size, unsigned search_buf_size, | |
662 | CORE_ADDR *found_addrp) | |
663 | { | |
664 | /* Prime the search buffer. */ | |
665 | ||
764880b7 PA |
666 | if (gdb_read_memory (start_addr, search_buf, search_buf_size) |
667 | != search_buf_size) | |
08388c79 | 668 | { |
5e1471f5 | 669 | warning ("Unable to access target memory at 0x%lx, halting search.", |
08388c79 DE |
670 | (long) start_addr); |
671 | return -1; | |
672 | } | |
673 | ||
674 | /* Perform the search. | |
675 | ||
676 | The loop is kept simple by allocating [N + pattern-length - 1] bytes. | |
677 | When we've scanned N bytes we copy the trailing bytes to the start and | |
678 | read in another N bytes. */ | |
679 | ||
680 | while (search_space_len >= pattern_len) | |
681 | { | |
682 | gdb_byte *found_ptr; | |
683 | unsigned nr_search_bytes = (search_space_len < search_buf_size | |
684 | ? search_space_len | |
685 | : search_buf_size); | |
686 | ||
687 | found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len); | |
688 | ||
689 | if (found_ptr != NULL) | |
690 | { | |
691 | CORE_ADDR found_addr = start_addr + (found_ptr - search_buf); | |
692 | *found_addrp = found_addr; | |
693 | return 1; | |
694 | } | |
695 | ||
696 | /* Not found in this chunk, skip to next chunk. */ | |
697 | ||
698 | /* Don't let search_space_len wrap here, it's unsigned. */ | |
699 | if (search_space_len >= chunk_size) | |
700 | search_space_len -= chunk_size; | |
701 | else | |
702 | search_space_len = 0; | |
703 | ||
704 | if (search_space_len >= pattern_len) | |
705 | { | |
706 | unsigned keep_len = search_buf_size - chunk_size; | |
8a35fb51 | 707 | CORE_ADDR read_addr = start_addr + chunk_size + keep_len; |
08388c79 DE |
708 | int nr_to_read; |
709 | ||
710 | /* Copy the trailing part of the previous iteration to the front | |
711 | of the buffer for the next iteration. */ | |
712 | memcpy (search_buf, search_buf + chunk_size, keep_len); | |
713 | ||
714 | nr_to_read = (search_space_len - keep_len < chunk_size | |
715 | ? search_space_len - keep_len | |
716 | : chunk_size); | |
717 | ||
90d74c30 | 718 | if (gdb_read_memory (read_addr, search_buf + keep_len, |
764880b7 | 719 | nr_to_read) != search_buf_size) |
08388c79 | 720 | { |
493e2a69 MS |
721 | warning ("Unable to access target memory " |
722 | "at 0x%lx, halting search.", | |
08388c79 DE |
723 | (long) read_addr); |
724 | return -1; | |
725 | } | |
726 | ||
727 | start_addr += chunk_size; | |
728 | } | |
729 | } | |
730 | ||
731 | /* Not found. */ | |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | /* Handle qSearch:memory packets. */ | |
737 | ||
738 | static void | |
739 | handle_search_memory (char *own_buf, int packet_len) | |
740 | { | |
741 | CORE_ADDR start_addr; | |
742 | CORE_ADDR search_space_len; | |
743 | gdb_byte *pattern; | |
744 | unsigned int pattern_len; | |
745 | /* NOTE: also defined in find.c testcase. */ | |
746 | #define SEARCH_CHUNK_SIZE 16000 | |
747 | const unsigned chunk_size = SEARCH_CHUNK_SIZE; | |
748 | /* Buffer to hold memory contents for searching. */ | |
749 | gdb_byte *search_buf; | |
750 | unsigned search_buf_size; | |
751 | int found; | |
752 | CORE_ADDR found_addr; | |
753 | int cmd_name_len = sizeof ("qSearch:memory:") - 1; | |
754 | ||
aef93bd7 | 755 | pattern = malloc (packet_len); |
08388c79 DE |
756 | if (pattern == NULL) |
757 | { | |
5e1471f5 | 758 | error ("Unable to allocate memory to perform the search"); |
08388c79 DE |
759 | strcpy (own_buf, "E00"); |
760 | return; | |
761 | } | |
762 | if (decode_search_memory_packet (own_buf + cmd_name_len, | |
763 | packet_len - cmd_name_len, | |
764 | &start_addr, &search_space_len, | |
765 | pattern, &pattern_len) < 0) | |
766 | { | |
767 | free (pattern); | |
5e1471f5 | 768 | error ("Error in parsing qSearch:memory packet"); |
08388c79 DE |
769 | strcpy (own_buf, "E00"); |
770 | return; | |
771 | } | |
772 | ||
773 | search_buf_size = chunk_size + pattern_len - 1; | |
774 | ||
775 | /* No point in trying to allocate a buffer larger than the search space. */ | |
776 | if (search_space_len < search_buf_size) | |
777 | search_buf_size = search_space_len; | |
778 | ||
aef93bd7 | 779 | search_buf = malloc (search_buf_size); |
08388c79 DE |
780 | if (search_buf == NULL) |
781 | { | |
782 | free (pattern); | |
5e1471f5 | 783 | error ("Unable to allocate memory to perform the search"); |
08388c79 DE |
784 | strcpy (own_buf, "E00"); |
785 | return; | |
786 | } | |
787 | ||
788 | found = handle_search_memory_1 (start_addr, search_space_len, | |
789 | pattern, pattern_len, | |
790 | search_buf, chunk_size, search_buf_size, | |
791 | &found_addr); | |
792 | ||
793 | if (found > 0) | |
794 | sprintf (own_buf, "1,%lx", (long) found_addr); | |
795 | else if (found == 0) | |
796 | strcpy (own_buf, "0"); | |
797 | else | |
798 | strcpy (own_buf, "E00"); | |
799 | ||
800 | free (search_buf); | |
801 | free (pattern); | |
802 | } | |
803 | ||
2d717e4f DJ |
804 | #define require_running(BUF) \ |
805 | if (!target_running ()) \ | |
806 | { \ | |
807 | write_enn (BUF); \ | |
808 | return; \ | |
809 | } | |
810 | ||
cdbfd419 PP |
811 | /* Handle monitor commands not handled by target-specific handlers. */ |
812 | ||
813 | static void | |
814 | handle_monitor_command (char *mon) | |
815 | { | |
816 | if (strcmp (mon, "set debug 1") == 0) | |
817 | { | |
818 | debug_threads = 1; | |
819 | monitor_output ("Debug output enabled.\n"); | |
820 | } | |
821 | else if (strcmp (mon, "set debug 0") == 0) | |
822 | { | |
823 | debug_threads = 0; | |
824 | monitor_output ("Debug output disabled.\n"); | |
825 | } | |
826 | else if (strcmp (mon, "set debug-hw-points 1") == 0) | |
827 | { | |
828 | debug_hw_points = 1; | |
829 | monitor_output ("H/W point debugging output enabled.\n"); | |
830 | } | |
831 | else if (strcmp (mon, "set debug-hw-points 0") == 0) | |
832 | { | |
833 | debug_hw_points = 0; | |
834 | monitor_output ("H/W point debugging output disabled.\n"); | |
835 | } | |
836 | else if (strcmp (mon, "set remote-debug 1") == 0) | |
837 | { | |
838 | remote_debug = 1; | |
839 | monitor_output ("Protocol debug output enabled.\n"); | |
840 | } | |
841 | else if (strcmp (mon, "set remote-debug 0") == 0) | |
842 | { | |
843 | remote_debug = 0; | |
844 | monitor_output ("Protocol debug output disabled.\n"); | |
845 | } | |
846 | else if (strcmp (mon, "help") == 0) | |
847 | monitor_show_help (); | |
848 | else if (strcmp (mon, "exit") == 0) | |
849 | exit_requested = 1; | |
850 | else | |
851 | { | |
852 | monitor_output ("Unknown monitor command.\n\n"); | |
853 | monitor_show_help (); | |
854 | write_enn (own_buf); | |
855 | } | |
856 | } | |
857 | ||
d08aafef PA |
858 | /* Associates a callback with each supported qXfer'able object. */ |
859 | ||
860 | struct qxfer | |
861 | { | |
862 | /* The object this handler handles. */ | |
863 | const char *object; | |
864 | ||
865 | /* Request that the target transfer up to LEN 8-bit bytes of the | |
866 | target's OBJECT. The OFFSET, for a seekable object, specifies | |
867 | the starting point. The ANNEX can be used to provide additional | |
868 | data-specific information to the target. | |
869 | ||
870 | Return the number of bytes actually transfered, zero when no | |
871 | further transfer is possible, -1 on error, and -2 when the | |
872 | transfer is not supported. Return of a positive value smaller | |
873 | than LEN does not indicate the end of the object, only the end of | |
874 | the transfer. | |
875 | ||
876 | One, and only one, of readbuf or writebuf must be non-NULL. */ | |
877 | int (*xfer) (const char *annex, | |
878 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
879 | ULONGEST offset, LONGEST len); | |
880 | }; | |
881 | ||
882 | /* Handle qXfer:auxv:read. */ | |
883 | ||
884 | static int | |
885 | handle_qxfer_auxv (const char *annex, | |
886 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
887 | ULONGEST offset, LONGEST len) | |
888 | { | |
889 | if (the_target->read_auxv == NULL || writebuf != NULL) | |
890 | return -2; | |
891 | ||
892 | if (annex[0] != '\0' || !target_running ()) | |
893 | return -1; | |
894 | ||
895 | return (*the_target->read_auxv) (offset, readbuf, len); | |
896 | } | |
897 | ||
898 | /* Handle qXfer:features:read. */ | |
899 | ||
900 | static int | |
901 | handle_qxfer_features (const char *annex, | |
902 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
903 | ULONGEST offset, LONGEST len) | |
904 | { | |
905 | const char *document; | |
906 | size_t total_len; | |
907 | ||
908 | if (writebuf != NULL) | |
909 | return -2; | |
910 | ||
911 | if (!target_running ()) | |
912 | return -1; | |
913 | ||
914 | /* Grab the correct annex. */ | |
915 | document = get_features_xml (annex); | |
916 | if (document == NULL) | |
917 | return -1; | |
918 | ||
919 | total_len = strlen (document); | |
920 | ||
921 | if (offset > total_len) | |
922 | return -1; | |
923 | ||
924 | if (offset + len > total_len) | |
925 | len = total_len - offset; | |
926 | ||
927 | memcpy (readbuf, document + offset, len); | |
928 | return len; | |
929 | } | |
930 | ||
931 | /* Handle qXfer:libraries:read. */ | |
932 | ||
933 | static int | |
934 | handle_qxfer_libraries (const char *annex, | |
935 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
936 | ULONGEST offset, LONGEST len) | |
937 | { | |
938 | unsigned int total_len; | |
939 | char *document, *p; | |
940 | struct inferior_list_entry *dll_ptr; | |
941 | ||
942 | if (writebuf != NULL) | |
943 | return -2; | |
944 | ||
945 | if (annex[0] != '\0' || !target_running ()) | |
946 | return -1; | |
947 | ||
2268b414 JK |
948 | /* Do not confuse this packet with qXfer:libraries-svr4:read. */ |
949 | if (the_target->qxfer_libraries_svr4 != NULL) | |
950 | return 0; | |
951 | ||
d08aafef PA |
952 | /* Over-estimate the necessary memory. Assume that every character |
953 | in the library name must be escaped. */ | |
954 | total_len = 64; | |
955 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
956 | total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); | |
957 | ||
958 | document = malloc (total_len); | |
959 | if (document == NULL) | |
960 | return -1; | |
961 | ||
962 | strcpy (document, "<library-list>\n"); | |
963 | p = document + strlen (document); | |
964 | ||
965 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
966 | { | |
967 | struct dll_info *dll = (struct dll_info *) dll_ptr; | |
968 | char *name; | |
969 | ||
970 | strcpy (p, " <library name=\""); | |
971 | p = p + strlen (p); | |
972 | name = xml_escape_text (dll->name); | |
973 | strcpy (p, name); | |
974 | free (name); | |
975 | p = p + strlen (p); | |
976 | strcpy (p, "\"><segment address=\""); | |
977 | p = p + strlen (p); | |
978 | sprintf (p, "0x%lx", (long) dll->base_addr); | |
979 | p = p + strlen (p); | |
980 | strcpy (p, "\"/></library>\n"); | |
981 | p = p + strlen (p); | |
982 | } | |
983 | ||
984 | strcpy (p, "</library-list>\n"); | |
985 | ||
986 | total_len = strlen (document); | |
987 | ||
988 | if (offset > total_len) | |
989 | { | |
990 | free (document); | |
991 | return -1; | |
992 | } | |
993 | ||
994 | if (offset + len > total_len) | |
995 | len = total_len - offset; | |
996 | ||
997 | memcpy (readbuf, document + offset, len); | |
998 | free (document); | |
999 | return len; | |
1000 | } | |
1001 | ||
2268b414 JK |
1002 | /* Handle qXfer:libraries-svr4:read. */ |
1003 | ||
1004 | static int | |
1005 | handle_qxfer_libraries_svr4 (const char *annex, | |
1006 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1007 | ULONGEST offset, LONGEST len) | |
1008 | { | |
1009 | if (writebuf != NULL) | |
1010 | return -2; | |
1011 | ||
1012 | if (annex[0] != '\0' || !target_running () | |
1013 | || the_target->qxfer_libraries_svr4 == NULL) | |
1014 | return -1; | |
1015 | ||
1016 | return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len); | |
1017 | } | |
1018 | ||
d08aafef PA |
1019 | /* Handle qXfer:osadata:read. */ |
1020 | ||
1021 | static int | |
1022 | handle_qxfer_osdata (const char *annex, | |
1023 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1024 | ULONGEST offset, LONGEST len) | |
1025 | { | |
1026 | if (the_target->qxfer_osdata == NULL || writebuf != NULL) | |
1027 | return -2; | |
1028 | ||
1029 | return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len); | |
1030 | } | |
1031 | ||
1032 | /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */ | |
1033 | ||
1034 | static int | |
1035 | handle_qxfer_siginfo (const char *annex, | |
1036 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1037 | ULONGEST offset, LONGEST len) | |
1038 | { | |
1039 | if (the_target->qxfer_siginfo == NULL) | |
1040 | return -2; | |
1041 | ||
1042 | if (annex[0] != '\0' || !target_running ()) | |
1043 | return -1; | |
1044 | ||
1045 | return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len); | |
1046 | } | |
1047 | ||
1048 | /* Handle qXfer:spu:read and qXfer:spu:write. */ | |
1049 | ||
1050 | static int | |
1051 | handle_qxfer_spu (const char *annex, | |
1052 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1053 | ULONGEST offset, LONGEST len) | |
1054 | { | |
1055 | if (the_target->qxfer_spu == NULL) | |
1056 | return -2; | |
1057 | ||
1058 | if (!target_running ()) | |
1059 | return -1; | |
1060 | ||
1061 | return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len); | |
1062 | } | |
1063 | ||
1064 | /* Handle qXfer:statictrace:read. */ | |
1065 | ||
1066 | static int | |
1067 | handle_qxfer_statictrace (const char *annex, | |
1068 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1069 | ULONGEST offset, LONGEST len) | |
1070 | { | |
1071 | ULONGEST nbytes; | |
1072 | ||
1073 | if (writebuf != NULL) | |
1074 | return -2; | |
1075 | ||
1076 | if (annex[0] != '\0' || !target_running () || current_traceframe == -1) | |
1077 | return -1; | |
1078 | ||
1079 | if (traceframe_read_sdata (current_traceframe, offset, | |
1080 | readbuf, len, &nbytes)) | |
1081 | return -1; | |
1082 | return nbytes; | |
1083 | } | |
1084 | ||
1085 | /* Helper for handle_qxfer_threads. */ | |
1086 | ||
dc146f7c | 1087 | static void |
d08aafef | 1088 | handle_qxfer_threads_proper (struct buffer *buffer) |
dc146f7c VP |
1089 | { |
1090 | struct inferior_list_entry *thread; | |
1091 | ||
1092 | buffer_grow_str (buffer, "<threads>\n"); | |
1093 | ||
1094 | for (thread = all_threads.head; thread; thread = thread->next) | |
1095 | { | |
1096 | ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread); | |
1097 | char ptid_s[100]; | |
1098 | int core = -1; | |
1099 | char core_s[21]; | |
1100 | ||
1101 | write_ptid (ptid_s, ptid); | |
1102 | ||
1103 | if (the_target->core_of_thread) | |
1104 | core = (*the_target->core_of_thread) (ptid); | |
1105 | ||
1106 | if (core != -1) | |
1107 | { | |
1108 | sprintf (core_s, "%d", core); | |
1109 | buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n", | |
1110 | ptid_s, core_s); | |
1111 | } | |
1112 | else | |
1113 | { | |
1114 | buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n", | |
1115 | ptid_s); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | buffer_grow_str0 (buffer, "</threads>\n"); | |
1120 | } | |
1121 | ||
d08aafef PA |
1122 | /* Handle qXfer:threads:read. */ |
1123 | ||
dc146f7c | 1124 | static int |
d08aafef PA |
1125 | handle_qxfer_threads (const char *annex, |
1126 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1127 | ULONGEST offset, LONGEST len) | |
dc146f7c VP |
1128 | { |
1129 | static char *result = 0; | |
1130 | static unsigned int result_length = 0; | |
1131 | ||
d08aafef PA |
1132 | if (writebuf != NULL) |
1133 | return -2; | |
1134 | ||
1135 | if (!target_running () || annex[0] != '\0') | |
1136 | return -1; | |
dc146f7c VP |
1137 | |
1138 | if (offset == 0) | |
1139 | { | |
1140 | struct buffer buffer; | |
1141 | /* When asked for data at offset 0, generate everything and store into | |
1142 | 'result'. Successive reads will be served off 'result'. */ | |
1143 | if (result) | |
1144 | free (result); | |
1145 | ||
1146 | buffer_init (&buffer); | |
1147 | ||
d08aafef | 1148 | handle_qxfer_threads_proper (&buffer); |
dc146f7c VP |
1149 | |
1150 | result = buffer_finish (&buffer); | |
1151 | result_length = strlen (result); | |
1152 | buffer_free (&buffer); | |
1153 | } | |
1154 | ||
1155 | if (offset >= result_length) | |
1156 | { | |
1157 | /* We're out of data. */ | |
1158 | free (result); | |
1159 | result = NULL; | |
1160 | result_length = 0; | |
1161 | return 0; | |
1162 | } | |
1163 | ||
d08aafef PA |
1164 | if (len > result_length - offset) |
1165 | len = result_length - offset; | |
1166 | ||
1167 | memcpy (readbuf, result + offset, len); | |
1168 | ||
1169 | return len; | |
1170 | } | |
1171 | ||
b3b9301e PA |
1172 | /* Handle qXfer:traceframe-info:read. */ |
1173 | ||
1174 | static int | |
1175 | handle_qxfer_traceframe_info (const char *annex, | |
1176 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1177 | ULONGEST offset, LONGEST len) | |
1178 | { | |
1179 | static char *result = 0; | |
1180 | static unsigned int result_length = 0; | |
1181 | ||
1182 | if (writebuf != NULL) | |
1183 | return -2; | |
1184 | ||
1185 | if (!target_running () || annex[0] != '\0' || current_traceframe == -1) | |
1186 | return -1; | |
1187 | ||
1188 | if (offset == 0) | |
1189 | { | |
1190 | struct buffer buffer; | |
1191 | ||
1192 | /* When asked for data at offset 0, generate everything and | |
1193 | store into 'result'. Successive reads will be served off | |
1194 | 'result'. */ | |
1195 | free (result); | |
1196 | ||
1197 | buffer_init (&buffer); | |
1198 | ||
1199 | traceframe_read_info (current_traceframe, &buffer); | |
1200 | ||
1201 | result = buffer_finish (&buffer); | |
1202 | result_length = strlen (result); | |
1203 | buffer_free (&buffer); | |
1204 | } | |
1205 | ||
1206 | if (offset >= result_length) | |
1207 | { | |
1208 | /* We're out of data. */ | |
1209 | free (result); | |
1210 | result = NULL; | |
1211 | result_length = 0; | |
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | if (len > result_length - offset) | |
1216 | len = result_length - offset; | |
1217 | ||
1218 | memcpy (readbuf, result + offset, len); | |
1219 | return len; | |
1220 | } | |
1221 | ||
78d85199 YQ |
1222 | /* Handle qXfer:fdpic:read. */ |
1223 | ||
1224 | static int | |
1225 | handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf, | |
1226 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
1227 | { | |
1228 | if (the_target->read_loadmap == NULL) | |
1229 | return -2; | |
1230 | ||
1231 | if (!target_running ()) | |
1232 | return -1; | |
1233 | ||
1234 | return (*the_target->read_loadmap) (annex, offset, readbuf, len); | |
1235 | } | |
1236 | ||
d08aafef PA |
1237 | static const struct qxfer qxfer_packets[] = |
1238 | { | |
1239 | { "auxv", handle_qxfer_auxv }, | |
78d85199 | 1240 | { "fdpic", handle_qxfer_fdpic}, |
d08aafef PA |
1241 | { "features", handle_qxfer_features }, |
1242 | { "libraries", handle_qxfer_libraries }, | |
2268b414 | 1243 | { "libraries-svr4", handle_qxfer_libraries_svr4 }, |
d08aafef PA |
1244 | { "osdata", handle_qxfer_osdata }, |
1245 | { "siginfo", handle_qxfer_siginfo }, | |
1246 | { "spu", handle_qxfer_spu }, | |
1247 | { "statictrace", handle_qxfer_statictrace }, | |
1248 | { "threads", handle_qxfer_threads }, | |
b3b9301e | 1249 | { "traceframe-info", handle_qxfer_traceframe_info }, |
d08aafef PA |
1250 | }; |
1251 | ||
1252 | static int | |
1253 | handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p) | |
1254 | { | |
1255 | int i; | |
1256 | char *object; | |
1257 | char *rw; | |
1258 | char *annex; | |
1259 | char *offset; | |
1260 | ||
1261 | if (strncmp (own_buf, "qXfer:", 6) != 0) | |
1262 | return 0; | |
1263 | ||
1264 | /* Grab the object, r/w and annex. */ | |
1265 | if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0) | |
1266 | { | |
1267 | write_enn (own_buf); | |
1268 | return 1; | |
1269 | } | |
1270 | ||
1271 | for (i = 0; | |
1272 | i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]); | |
1273 | i++) | |
1274 | { | |
1275 | const struct qxfer *q = &qxfer_packets[i]; | |
1276 | ||
1277 | if (strcmp (object, q->object) == 0) | |
1278 | { | |
1279 | if (strcmp (rw, "read") == 0) | |
1280 | { | |
1281 | unsigned char *data; | |
1282 | int n; | |
1283 | CORE_ADDR ofs; | |
1284 | unsigned int len; | |
1285 | ||
1286 | /* Grab the offset and length. */ | |
1287 | if (decode_xfer_read (offset, &ofs, &len) < 0) | |
1288 | { | |
1289 | write_enn (own_buf); | |
1290 | return 1; | |
1291 | } | |
1292 | ||
1293 | /* Read one extra byte, as an indicator of whether there is | |
1294 | more. */ | |
1295 | if (len > PBUFSIZ - 2) | |
1296 | len = PBUFSIZ - 2; | |
1297 | data = malloc (len + 1); | |
1298 | if (data == NULL) | |
1299 | { | |
1300 | write_enn (own_buf); | |
1301 | return 1; | |
1302 | } | |
1303 | n = (*q->xfer) (annex, data, NULL, ofs, len + 1); | |
1304 | if (n == -2) | |
1305 | { | |
1306 | free (data); | |
1307 | return 0; | |
1308 | } | |
1309 | else if (n < 0) | |
1310 | write_enn (own_buf); | |
1311 | else if (n > len) | |
1312 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); | |
1313 | else | |
1314 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); | |
1315 | ||
1316 | free (data); | |
1317 | return 1; | |
1318 | } | |
1319 | else if (strcmp (rw, "write") == 0) | |
1320 | { | |
1321 | int n; | |
1322 | unsigned int len; | |
1323 | CORE_ADDR ofs; | |
1324 | unsigned char *data; | |
1325 | ||
1326 | strcpy (own_buf, "E00"); | |
1327 | data = malloc (packet_len - (offset - own_buf)); | |
1328 | if (data == NULL) | |
1329 | { | |
1330 | write_enn (own_buf); | |
1331 | return 1; | |
1332 | } | |
1333 | if (decode_xfer_write (offset, packet_len - (offset - own_buf), | |
1334 | &ofs, &len, data) < 0) | |
1335 | { | |
1336 | free (data); | |
1337 | write_enn (own_buf); | |
1338 | return 1; | |
1339 | } | |
1340 | ||
1341 | n = (*q->xfer) (annex, NULL, data, ofs, len); | |
1342 | if (n == -2) | |
1343 | { | |
1344 | free (data); | |
1345 | return 0; | |
1346 | } | |
1347 | else if (n < 0) | |
1348 | write_enn (own_buf); | |
1349 | else | |
1350 | sprintf (own_buf, "%x", n); | |
dc146f7c | 1351 | |
d08aafef PA |
1352 | free (data); |
1353 | return 1; | |
1354 | } | |
dc146f7c | 1355 | |
d08aafef PA |
1356 | return 0; |
1357 | } | |
1358 | } | |
dc146f7c | 1359 | |
d08aafef | 1360 | return 0; |
dc146f7c VP |
1361 | } |
1362 | ||
30ba68cb MS |
1363 | /* Table used by the crc32 function to calcuate the checksum. */ |
1364 | ||
1365 | static unsigned int crc32_table[256] = | |
1366 | {0, 0}; | |
1367 | ||
1368 | /* Compute 32 bit CRC from inferior memory. | |
1369 | ||
1370 | On success, return 32 bit CRC. | |
1371 | On failure, return (unsigned long long) -1. */ | |
1372 | ||
1373 | static unsigned long long | |
1374 | crc32 (CORE_ADDR base, int len, unsigned int crc) | |
1375 | { | |
1376 | if (!crc32_table[1]) | |
1377 | { | |
1378 | /* Initialize the CRC table and the decoding table. */ | |
1379 | int i, j; | |
1380 | unsigned int c; | |
1381 | ||
1382 | for (i = 0; i < 256; i++) | |
1383 | { | |
1384 | for (c = i << 24, j = 8; j > 0; --j) | |
1385 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
1386 | crc32_table[i] = c; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | while (len--) | |
1391 | { | |
1392 | unsigned char byte = 0; | |
1393 | ||
1394 | /* Return failure if memory read fails. */ | |
1395 | if (read_inferior_memory (base, &byte, 1) != 0) | |
1396 | return (unsigned long long) -1; | |
1397 | ||
1398 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255]; | |
1399 | base++; | |
1400 | } | |
1401 | return (unsigned long long) crc; | |
1402 | } | |
1403 | ||
ce3a066d | 1404 | /* Handle all of the extended 'q' packets. */ |
d08aafef | 1405 | |
ce3a066d | 1406 | void |
0e7f50da | 1407 | handle_query (char *own_buf, int packet_len, int *new_packet_len_p) |
ce3a066d | 1408 | { |
0d62e5e8 DJ |
1409 | static struct inferior_list_entry *thread_ptr; |
1410 | ||
bb63802a | 1411 | /* Reply the current thread id. */ |
db42f210 | 1412 | if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC) |
bb63802a | 1413 | { |
95954743 | 1414 | ptid_t gdb_id; |
2d717e4f | 1415 | require_running (own_buf); |
bd99dc85 | 1416 | |
95954743 PA |
1417 | if (!ptid_equal (general_thread, null_ptid) |
1418 | && !ptid_equal (general_thread, minus_one_ptid)) | |
bd99dc85 PA |
1419 | gdb_id = general_thread; |
1420 | else | |
1421 | { | |
1422 | thread_ptr = all_threads.head; | |
1423 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1424 | } | |
1425 | ||
95954743 PA |
1426 | sprintf (own_buf, "QC"); |
1427 | own_buf += 2; | |
4b812f4e | 1428 | write_ptid (own_buf, gdb_id); |
bb63802a UW |
1429 | return; |
1430 | } | |
1431 | ||
ce3a066d DJ |
1432 | if (strcmp ("qSymbol::", own_buf) == 0) |
1433 | { | |
d3bbe7a0 PA |
1434 | /* GDB is suggesting new symbols have been loaded. This may |
1435 | mean a new shared library has been detected as loaded, so | |
1436 | take the opportunity to check if breakpoints we think are | |
1437 | inserted, still are. Note that it isn't guaranteed that | |
1438 | we'll see this when a shared library is loaded, and nor will | |
1439 | we see this for unloads (although breakpoints in unloaded | |
1440 | libraries shouldn't trigger), as GDB may not find symbols for | |
1441 | the library at all. We also re-validate breakpoints when we | |
1442 | see a second GDB breakpoint for the same address, and or when | |
1443 | we access breakpoint shadows. */ | |
1444 | validate_breakpoints (); | |
1445 | ||
fa593d66 PA |
1446 | if (target_supports_tracepoints ()) |
1447 | tracepoint_look_up_symbols (); | |
1448 | ||
2d717e4f | 1449 | if (target_running () && the_target->look_up_symbols != NULL) |
2f2893d9 DJ |
1450 | (*the_target->look_up_symbols) (); |
1451 | ||
ce3a066d DJ |
1452 | strcpy (own_buf, "OK"); |
1453 | return; | |
1454 | } | |
1455 | ||
db42f210 | 1456 | if (!disable_packet_qfThreadInfo) |
0d62e5e8 | 1457 | { |
db42f210 | 1458 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
0d62e5e8 | 1459 | { |
95954743 PA |
1460 | ptid_t gdb_id; |
1461 | ||
db42f210 PA |
1462 | require_running (own_buf); |
1463 | thread_ptr = all_threads.head; | |
95954743 PA |
1464 | |
1465 | *own_buf++ = 'm'; | |
1466 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1467 | write_ptid (own_buf, gdb_id); | |
0d62e5e8 DJ |
1468 | thread_ptr = thread_ptr->next; |
1469 | return; | |
1470 | } | |
db42f210 PA |
1471 | |
1472 | if (strcmp ("qsThreadInfo", own_buf) == 0) | |
0d62e5e8 | 1473 | { |
95954743 PA |
1474 | ptid_t gdb_id; |
1475 | ||
db42f210 PA |
1476 | require_running (own_buf); |
1477 | if (thread_ptr != NULL) | |
1478 | { | |
95954743 PA |
1479 | *own_buf++ = 'm'; |
1480 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1481 | write_ptid (own_buf, gdb_id); | |
db42f210 PA |
1482 | thread_ptr = thread_ptr->next; |
1483 | return; | |
1484 | } | |
1485 | else | |
1486 | { | |
1487 | sprintf (own_buf, "l"); | |
1488 | return; | |
1489 | } | |
0d62e5e8 DJ |
1490 | } |
1491 | } | |
aa691b87 | 1492 | |
52fb6437 NS |
1493 | if (the_target->read_offsets != NULL |
1494 | && strcmp ("qOffsets", own_buf) == 0) | |
1495 | { | |
1496 | CORE_ADDR text, data; | |
2d717e4f DJ |
1497 | |
1498 | require_running (own_buf); | |
52fb6437 NS |
1499 | if (the_target->read_offsets (&text, &data)) |
1500 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
1501 | (long)text, (long)data, (long)data); | |
1502 | else | |
1503 | write_enn (own_buf); | |
1b3f6016 | 1504 | |
52fb6437 NS |
1505 | return; |
1506 | } | |
1507 | ||
be2a5f71 DJ |
1508 | /* Protocol features query. */ |
1509 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
1510 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
1511 | { | |
95954743 | 1512 | char *p = &own_buf[10]; |
fa593d66 | 1513 | int gdb_supports_qRelocInsn = 0; |
95954743 | 1514 | |
1570b33e L |
1515 | /* Start processing qSupported packet. */ |
1516 | target_process_qsupported (NULL); | |
1517 | ||
95954743 PA |
1518 | /* Process each feature being provided by GDB. The first |
1519 | feature will follow a ':', and latter features will follow | |
1520 | ';'. */ | |
1521 | if (*p == ':') | |
d149dd1d PA |
1522 | { |
1523 | char **qsupported = NULL; | |
1524 | int count = 0; | |
1525 | int i; | |
1526 | ||
1527 | /* Two passes, to avoid nested strtok calls in | |
1528 | target_process_qsupported. */ | |
1529 | for (p = strtok (p + 1, ";"); | |
1530 | p != NULL; | |
1531 | p = strtok (NULL, ";")) | |
1532 | { | |
1533 | count++; | |
1534 | qsupported = xrealloc (qsupported, count * sizeof (char *)); | |
1535 | qsupported[count - 1] = xstrdup (p); | |
1536 | } | |
1537 | ||
1538 | for (i = 0; i < count; i++) | |
1539 | { | |
1540 | p = qsupported[i]; | |
1541 | if (strcmp (p, "multiprocess+") == 0) | |
1542 | { | |
1543 | /* GDB supports and wants multi-process support if | |
1544 | possible. */ | |
1545 | if (target_supports_multi_process ()) | |
1546 | multi_process = 1; | |
1547 | } | |
fa593d66 PA |
1548 | else if (strcmp (p, "qRelocInsn+") == 0) |
1549 | { | |
1550 | /* GDB supports relocate instruction requests. */ | |
1551 | gdb_supports_qRelocInsn = 1; | |
1552 | } | |
d149dd1d PA |
1553 | else |
1554 | target_process_qsupported (p); | |
1555 | ||
1556 | free (p); | |
1557 | } | |
1558 | ||
1559 | free (qsupported); | |
1560 | } | |
95954743 | 1561 | |
89be2091 | 1562 | sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1); |
0876f84a | 1563 | |
2268b414 JK |
1564 | if (the_target->qxfer_libraries_svr4 != NULL) |
1565 | strcat (own_buf, ";qXfer:libraries-svr4:read+"); | |
1566 | else | |
1567 | { | |
1568 | /* We do not have any hook to indicate whether the non-SVR4 target | |
1569 | backend supports qXfer:libraries:read, so always report it. */ | |
1570 | strcat (own_buf, ";qXfer:libraries:read+"); | |
1571 | } | |
255e7678 | 1572 | |
0876f84a | 1573 | if (the_target->read_auxv != NULL) |
9f2e1e63 | 1574 | strcat (own_buf, ";qXfer:auxv:read+"); |
2d717e4f | 1575 | |
0e7f50da UW |
1576 | if (the_target->qxfer_spu != NULL) |
1577 | strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+"); | |
0876f84a | 1578 | |
4aa995e1 PA |
1579 | if (the_target->qxfer_siginfo != NULL) |
1580 | strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+"); | |
1581 | ||
78d85199 YQ |
1582 | if (the_target->read_loadmap != NULL) |
1583 | strcat (own_buf, ";qXfer:fdpic:read+"); | |
1584 | ||
221c031f UW |
1585 | /* We always report qXfer:features:read, as targets may |
1586 | install XML files on a subsequent call to arch_setup. | |
1587 | If we reported to GDB on startup that we don't support | |
1588 | qXfer:feature:read at all, we will never be re-queried. */ | |
1589 | strcat (own_buf, ";qXfer:features:read+"); | |
23181151 | 1590 | |
a6f3e723 SL |
1591 | if (transport_is_reliable) |
1592 | strcat (own_buf, ";QStartNoAckMode+"); | |
07e059b5 VP |
1593 | |
1594 | if (the_target->qxfer_osdata != NULL) | |
1b3f6016 | 1595 | strcat (own_buf, ";qXfer:osdata:read+"); |
07e059b5 | 1596 | |
cf8fd78b PA |
1597 | if (target_supports_multi_process ()) |
1598 | strcat (own_buf, ";multiprocess+"); | |
95954743 | 1599 | |
bd99dc85 PA |
1600 | if (target_supports_non_stop ()) |
1601 | strcat (own_buf, ";QNonStop+"); | |
1602 | ||
03583c20 UW |
1603 | if (target_supports_disable_randomization ()) |
1604 | strcat (own_buf, ";QDisableRandomization+"); | |
1605 | ||
dc146f7c VP |
1606 | strcat (own_buf, ";qXfer:threads:read+"); |
1607 | ||
219f2f23 PA |
1608 | if (target_supports_tracepoints ()) |
1609 | { | |
1610 | strcat (own_buf, ";ConditionalTracepoints+"); | |
1611 | strcat (own_buf, ";TraceStateVariables+"); | |
1612 | strcat (own_buf, ";TracepointSource+"); | |
8336d594 | 1613 | strcat (own_buf, ";DisconnectedTracing+"); |
fa593d66 PA |
1614 | if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ()) |
1615 | strcat (own_buf, ";FastTracepoints+"); | |
0fb4aa4b | 1616 | strcat (own_buf, ";StaticTracepoints+"); |
1e4d1764 | 1617 | strcat (own_buf, ";InstallInTrace+"); |
0fb4aa4b | 1618 | strcat (own_buf, ";qXfer:statictrace:read+"); |
b3b9301e | 1619 | strcat (own_buf, ";qXfer:traceframe-info:read+"); |
d248b706 | 1620 | strcat (own_buf, ";EnableDisableTracepoints+"); |
3065dfb6 | 1621 | strcat (own_buf, ";tracenz+"); |
219f2f23 PA |
1622 | } |
1623 | ||
be2a5f71 DJ |
1624 | return; |
1625 | } | |
1626 | ||
dae5f5cf DJ |
1627 | /* Thread-local storage support. */ |
1628 | if (the_target->get_tls_address != NULL | |
1629 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) | |
1630 | { | |
1631 | char *p = own_buf + 12; | |
5b1c542e | 1632 | CORE_ADDR parts[2], address = 0; |
dae5f5cf | 1633 | int i, err; |
95954743 | 1634 | ptid_t ptid = null_ptid; |
dae5f5cf | 1635 | |
2d717e4f DJ |
1636 | require_running (own_buf); |
1637 | ||
dae5f5cf DJ |
1638 | for (i = 0; i < 3; i++) |
1639 | { | |
1640 | char *p2; | |
1641 | int len; | |
1642 | ||
1643 | if (p == NULL) | |
1644 | break; | |
1645 | ||
1646 | p2 = strchr (p, ','); | |
1647 | if (p2) | |
1648 | { | |
1649 | len = p2 - p; | |
1650 | p2++; | |
1651 | } | |
1652 | else | |
1653 | { | |
1654 | len = strlen (p); | |
1655 | p2 = NULL; | |
1656 | } | |
1657 | ||
5b1c542e | 1658 | if (i == 0) |
95954743 | 1659 | ptid = read_ptid (p, NULL); |
5b1c542e PA |
1660 | else |
1661 | decode_address (&parts[i - 1], p, len); | |
dae5f5cf DJ |
1662 | p = p2; |
1663 | } | |
1664 | ||
1665 | if (p != NULL || i < 3) | |
1666 | err = 1; | |
1667 | else | |
1668 | { | |
e09875d4 | 1669 | struct thread_info *thread = find_thread_ptid (ptid); |
dae5f5cf DJ |
1670 | |
1671 | if (thread == NULL) | |
1672 | err = 2; | |
1673 | else | |
5b1c542e | 1674 | err = the_target->get_tls_address (thread, parts[0], parts[1], |
dae5f5cf DJ |
1675 | &address); |
1676 | } | |
1677 | ||
1678 | if (err == 0) | |
1679 | { | |
c6f46ca0 | 1680 | strcpy (own_buf, paddress(address)); |
dae5f5cf DJ |
1681 | return; |
1682 | } | |
1683 | else if (err > 0) | |
1684 | { | |
1685 | write_enn (own_buf); | |
1686 | return; | |
1687 | } | |
1688 | ||
1689 | /* Otherwise, pretend we do not understand this packet. */ | |
1690 | } | |
1691 | ||
711e434b PM |
1692 | /* Windows OS Thread Information Block address support. */ |
1693 | if (the_target->get_tib_address != NULL | |
1694 | && strncmp ("qGetTIBAddr:", own_buf, 12) == 0) | |
1695 | { | |
1696 | char *annex; | |
1697 | int n; | |
1698 | CORE_ADDR tlb; | |
1699 | ptid_t ptid = read_ptid (own_buf + 12, &annex); | |
1700 | ||
1701 | n = (*the_target->get_tib_address) (ptid, &tlb); | |
1702 | if (n == 1) | |
1703 | { | |
c6f46ca0 | 1704 | strcpy (own_buf, paddress(tlb)); |
711e434b PM |
1705 | return; |
1706 | } | |
1707 | else if (n == 0) | |
1708 | { | |
1709 | write_enn (own_buf); | |
1710 | return; | |
1711 | } | |
1712 | return; | |
1713 | } | |
1714 | ||
c74d0ad8 DJ |
1715 | /* Handle "monitor" commands. */ |
1716 | if (strncmp ("qRcmd,", own_buf, 6) == 0) | |
1717 | { | |
aef93bd7 | 1718 | char *mon = malloc (PBUFSIZ); |
c74d0ad8 DJ |
1719 | int len = strlen (own_buf + 6); |
1720 | ||
aef93bd7 DE |
1721 | if (mon == NULL) |
1722 | { | |
1723 | write_enn (own_buf); | |
1724 | return; | |
1725 | } | |
1726 | ||
d41b6bb4 | 1727 | if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) |
c74d0ad8 DJ |
1728 | { |
1729 | write_enn (own_buf); | |
1730 | free (mon); | |
1731 | return; | |
1732 | } | |
1733 | mon[len / 2] = '\0'; | |
1734 | ||
1735 | write_ok (own_buf); | |
1736 | ||
cdbfd419 PP |
1737 | if (the_target->handle_monitor_command == NULL |
1738 | || (*the_target->handle_monitor_command) (mon) == 0) | |
1739 | /* Default processing. */ | |
1740 | handle_monitor_command (mon); | |
c74d0ad8 DJ |
1741 | |
1742 | free (mon); | |
1743 | return; | |
1744 | } | |
1745 | ||
493e2a69 MS |
1746 | if (strncmp ("qSearch:memory:", own_buf, |
1747 | sizeof ("qSearch:memory:") - 1) == 0) | |
08388c79 DE |
1748 | { |
1749 | require_running (own_buf); | |
1750 | handle_search_memory (own_buf, packet_len); | |
1751 | return; | |
1752 | } | |
1753 | ||
95954743 PA |
1754 | if (strcmp (own_buf, "qAttached") == 0 |
1755 | || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0) | |
0b16c5cf | 1756 | { |
95954743 PA |
1757 | struct process_info *process; |
1758 | ||
1759 | if (own_buf[sizeof ("qAttached") - 1]) | |
1760 | { | |
1761 | int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16); | |
1762 | process = (struct process_info *) | |
1763 | find_inferior_id (&all_processes, pid_to_ptid (pid)); | |
1764 | } | |
1765 | else | |
1766 | { | |
1767 | require_running (own_buf); | |
1768 | process = current_process (); | |
1769 | } | |
1770 | ||
1771 | if (process == NULL) | |
1772 | { | |
1773 | write_enn (own_buf); | |
1774 | return; | |
1775 | } | |
1776 | ||
1777 | strcpy (own_buf, process->attached ? "1" : "0"); | |
0b16c5cf PA |
1778 | return; |
1779 | } | |
1780 | ||
30ba68cb MS |
1781 | if (strncmp ("qCRC:", own_buf, 5) == 0) |
1782 | { | |
1783 | /* CRC check (compare-section). */ | |
1784 | char *comma; | |
1785 | CORE_ADDR base; | |
1786 | int len; | |
1787 | unsigned long long crc; | |
1788 | ||
1789 | require_running (own_buf); | |
2280c721 | 1790 | base = strtoul (own_buf + 5, &comma, 16); |
30ba68cb MS |
1791 | if (*comma++ != ',') |
1792 | { | |
1793 | write_enn (own_buf); | |
1794 | return; | |
1795 | } | |
1796 | len = strtoul (comma, NULL, 16); | |
1797 | crc = crc32 (base, len, 0xffffffff); | |
1798 | /* Check for memory failure. */ | |
1799 | if (crc == (unsigned long long) -1) | |
1800 | { | |
1801 | write_enn (own_buf); | |
1802 | return; | |
1803 | } | |
1804 | sprintf (own_buf, "C%lx", (unsigned long) crc); | |
1805 | return; | |
1806 | } | |
1807 | ||
d08aafef PA |
1808 | if (handle_qxfer (own_buf, packet_len, new_packet_len_p)) |
1809 | return; | |
1810 | ||
219f2f23 PA |
1811 | if (target_supports_tracepoints () && handle_tracepoint_query (own_buf)) |
1812 | return; | |
1813 | ||
ce3a066d DJ |
1814 | /* Otherwise we didn't know what packet it was. Say we didn't |
1815 | understand it. */ | |
1816 | own_buf[0] = 0; | |
1817 | } | |
1818 | ||
ce1a5b52 PA |
1819 | static void gdb_wants_all_threads_stopped (void); |
1820 | ||
64386c31 DJ |
1821 | /* Parse vCont packets. */ |
1822 | void | |
5b1c542e | 1823 | handle_v_cont (char *own_buf) |
64386c31 DJ |
1824 | { |
1825 | char *p, *q; | |
1826 | int n = 0, i = 0; | |
2bd7c093 | 1827 | struct thread_resume *resume_info; |
95954743 | 1828 | struct thread_resume default_action = {{0}}; |
64386c31 DJ |
1829 | |
1830 | /* Count the number of semicolons in the packet. There should be one | |
1831 | for every action. */ | |
1832 | p = &own_buf[5]; | |
1833 | while (p) | |
1834 | { | |
1835 | n++; | |
1836 | p++; | |
1837 | p = strchr (p, ';'); | |
1838 | } | |
2bd7c093 PA |
1839 | |
1840 | resume_info = malloc (n * sizeof (resume_info[0])); | |
aef93bd7 DE |
1841 | if (resume_info == NULL) |
1842 | goto err; | |
64386c31 | 1843 | |
64386c31 | 1844 | p = &own_buf[5]; |
64386c31 DJ |
1845 | while (*p) |
1846 | { | |
1847 | p++; | |
1848 | ||
64386c31 | 1849 | if (p[0] == 's' || p[0] == 'S') |
bd99dc85 | 1850 | resume_info[i].kind = resume_step; |
64386c31 | 1851 | else if (p[0] == 'c' || p[0] == 'C') |
bd99dc85 PA |
1852 | resume_info[i].kind = resume_continue; |
1853 | else if (p[0] == 't') | |
1854 | resume_info[i].kind = resume_stop; | |
64386c31 DJ |
1855 | else |
1856 | goto err; | |
1857 | ||
1858 | if (p[0] == 'S' || p[0] == 'C') | |
1859 | { | |
1860 | int sig; | |
1861 | sig = strtol (p + 1, &q, 16); | |
1862 | if (p == q) | |
1863 | goto err; | |
1864 | p = q; | |
1865 | ||
1866 | if (!target_signal_to_host_p (sig)) | |
1867 | goto err; | |
1868 | resume_info[i].sig = target_signal_to_host (sig); | |
1869 | } | |
1870 | else | |
1871 | { | |
1872 | resume_info[i].sig = 0; | |
1873 | p = p + 1; | |
1874 | } | |
1875 | ||
1876 | if (p[0] == 0) | |
1877 | { | |
95954743 | 1878 | resume_info[i].thread = minus_one_ptid; |
64386c31 DJ |
1879 | default_action = resume_info[i]; |
1880 | ||
1881 | /* Note: we don't increment i here, we'll overwrite this entry | |
1882 | the next time through. */ | |
1883 | } | |
1884 | else if (p[0] == ':') | |
1885 | { | |
95954743 | 1886 | ptid_t ptid = read_ptid (p + 1, &q); |
a06660f7 | 1887 | |
64386c31 DJ |
1888 | if (p == q) |
1889 | goto err; | |
1890 | p = q; | |
1891 | if (p[0] != ';' && p[0] != 0) | |
1892 | goto err; | |
1893 | ||
95954743 | 1894 | resume_info[i].thread = ptid; |
a06660f7 | 1895 | |
64386c31 DJ |
1896 | i++; |
1897 | } | |
1898 | } | |
1899 | ||
2bd7c093 PA |
1900 | if (i < n) |
1901 | resume_info[i] = default_action; | |
64386c31 DJ |
1902 | |
1903 | /* Still used in occasional places in the backend. */ | |
bd99dc85 | 1904 | if (n == 1 |
95954743 | 1905 | && !ptid_equal (resume_info[0].thread, minus_one_ptid) |
bd99dc85 | 1906 | && resume_info[0].kind != resume_stop) |
64386c31 DJ |
1907 | cont_thread = resume_info[0].thread; |
1908 | else | |
95954743 | 1909 | cont_thread = minus_one_ptid; |
dc3f8883 | 1910 | set_desired_inferior (0); |
64386c31 | 1911 | |
bd99dc85 PA |
1912 | if (!non_stop) |
1913 | enable_async_io (); | |
1914 | ||
2bd7c093 | 1915 | (*the_target->resume) (resume_info, n); |
64386c31 DJ |
1916 | |
1917 | free (resume_info); | |
1918 | ||
bd99dc85 PA |
1919 | if (non_stop) |
1920 | write_ok (own_buf); | |
1921 | else | |
1922 | { | |
95954743 | 1923 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
ce1a5b52 | 1924 | |
d20a8ad9 PA |
1925 | if (last_status.kind != TARGET_WAITKIND_EXITED |
1926 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
1927 | current_inferior->last_status = last_status; | |
1928 | ||
ce1a5b52 PA |
1929 | /* From the client's perspective, all-stop mode always stops all |
1930 | threads implicitly (and the target backend has already done | |
1931 | so by now). Tag all threads as "want-stopped", so we don't | |
1932 | resume them implicitly without the client telling us to. */ | |
1933 | gdb_wants_all_threads_stopped (); | |
bd99dc85 PA |
1934 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
1935 | disable_async_io (); | |
6bd31874 JB |
1936 | |
1937 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
1938 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
1939 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); | |
bd99dc85 | 1940 | } |
64386c31 DJ |
1941 | return; |
1942 | ||
1943 | err: | |
255e7678 | 1944 | write_enn (own_buf); |
64386c31 DJ |
1945 | free (resume_info); |
1946 | return; | |
1947 | } | |
1948 | ||
2d717e4f DJ |
1949 | /* Attach to a new program. Return 1 if successful, 0 if failure. */ |
1950 | int | |
5b1c542e | 1951 | handle_v_attach (char *own_buf) |
2d717e4f DJ |
1952 | { |
1953 | int pid; | |
1954 | ||
1955 | pid = strtol (own_buf + 8, NULL, 16); | |
5b1c542e | 1956 | if (pid != 0 && attach_inferior (pid) == 0) |
2d717e4f | 1957 | { |
aeba519e PA |
1958 | /* Don't report shared library events after attaching, even if |
1959 | some libraries are preloaded. GDB will always poll the | |
1960 | library list. Avoids the "stopped by shared library event" | |
1961 | notice on the GDB side. */ | |
1962 | dlls_changed = 0; | |
bd99dc85 PA |
1963 | |
1964 | if (non_stop) | |
1965 | { | |
1966 | /* In non-stop, we don't send a resume reply. Stop events | |
1967 | will follow up using the normal notification | |
1968 | mechanism. */ | |
1969 | write_ok (own_buf); | |
1970 | } | |
1971 | else | |
1972 | prepare_resume_reply (own_buf, last_ptid, &last_status); | |
1973 | ||
2d717e4f DJ |
1974 | return 1; |
1975 | } | |
1976 | else | |
1977 | { | |
1978 | write_enn (own_buf); | |
1979 | return 0; | |
1980 | } | |
1981 | } | |
1982 | ||
1983 | /* Run a new program. Return 1 if successful, 0 if failure. */ | |
1984 | static int | |
5b1c542e | 1985 | handle_v_run (char *own_buf) |
2d717e4f | 1986 | { |
aef93bd7 | 1987 | char *p, *next_p, **new_argv; |
2d717e4f DJ |
1988 | int i, new_argc; |
1989 | ||
1990 | new_argc = 0; | |
1991 | for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';')) | |
1992 | { | |
1993 | p++; | |
1994 | new_argc++; | |
1995 | } | |
1996 | ||
aef93bd7 DE |
1997 | new_argv = calloc (new_argc + 2, sizeof (char *)); |
1998 | if (new_argv == NULL) | |
1999 | { | |
2000 | write_enn (own_buf); | |
2001 | return 0; | |
2002 | } | |
2003 | ||
2d717e4f DJ |
2004 | i = 0; |
2005 | for (p = own_buf + strlen ("vRun;"); *p; p = next_p) | |
2006 | { | |
2007 | next_p = strchr (p, ';'); | |
2008 | if (next_p == NULL) | |
2009 | next_p = p + strlen (p); | |
2010 | ||
2011 | if (i == 0 && p == next_p) | |
2012 | new_argv[i] = NULL; | |
2013 | else | |
2014 | { | |
aef93bd7 | 2015 | /* FIXME: Fail request if out of memory instead of dying. */ |
bca929d3 | 2016 | new_argv[i] = xmalloc (1 + (next_p - p) / 2); |
2d717e4f DJ |
2017 | unhexify (new_argv[i], p, (next_p - p) / 2); |
2018 | new_argv[i][(next_p - p) / 2] = '\0'; | |
2019 | } | |
2020 | ||
2021 | if (*next_p) | |
2022 | next_p++; | |
2023 | i++; | |
2024 | } | |
2025 | new_argv[i] = NULL; | |
2026 | ||
2027 | if (new_argv[0] == NULL) | |
2028 | { | |
f142445f DJ |
2029 | /* GDB didn't specify a program to run. Use the program from the |
2030 | last run with the new argument list. */ | |
9b710a42 | 2031 | |
2d717e4f DJ |
2032 | if (program_argv == NULL) |
2033 | { | |
2034 | write_enn (own_buf); | |
b2c04452 | 2035 | freeargv (new_argv); |
2d717e4f DJ |
2036 | return 0; |
2037 | } | |
2038 | ||
aef93bd7 DE |
2039 | new_argv[0] = strdup (program_argv[0]); |
2040 | if (new_argv[0] == NULL) | |
2041 | { | |
aef93bd7 | 2042 | write_enn (own_buf); |
b2c04452 | 2043 | freeargv (new_argv); |
aef93bd7 | 2044 | return 0; |
1b3f6016 | 2045 | } |
2d717e4f | 2046 | } |
f142445f | 2047 | |
aef93bd7 DE |
2048 | /* Free the old argv and install the new one. */ |
2049 | freeargv (program_argv); | |
f142445f | 2050 | program_argv = new_argv; |
2d717e4f | 2051 | |
5b1c542e PA |
2052 | start_inferior (program_argv); |
2053 | if (last_status.kind == TARGET_WAITKIND_STOPPED) | |
2d717e4f | 2054 | { |
5b1c542e | 2055 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
bd99dc85 PA |
2056 | |
2057 | /* In non-stop, sending a resume reply doesn't set the general | |
2058 | thread, but GDB assumes a vRun sets it (this is so GDB can | |
2059 | query which is the main thread of the new inferior. */ | |
2060 | if (non_stop) | |
2061 | general_thread = last_ptid; | |
2062 | ||
2d717e4f DJ |
2063 | return 1; |
2064 | } | |
2065 | else | |
2066 | { | |
2067 | write_enn (own_buf); | |
2068 | return 0; | |
2069 | } | |
2070 | } | |
2071 | ||
95954743 PA |
2072 | /* Kill process. Return 1 if successful, 0 if failure. */ |
2073 | int | |
2074 | handle_v_kill (char *own_buf) | |
2075 | { | |
2076 | int pid; | |
2077 | char *p = &own_buf[6]; | |
0f54c268 PM |
2078 | if (multi_process) |
2079 | pid = strtol (p, NULL, 16); | |
2080 | else | |
2081 | pid = signal_pid; | |
95954743 PA |
2082 | if (pid != 0 && kill_inferior (pid) == 0) |
2083 | { | |
2084 | last_status.kind = TARGET_WAITKIND_SIGNALLED; | |
2085 | last_status.value.sig = TARGET_SIGNAL_KILL; | |
2086 | last_ptid = pid_to_ptid (pid); | |
2087 | discard_queued_stop_replies (pid); | |
2088 | write_ok (own_buf); | |
2089 | return 1; | |
2090 | } | |
2091 | else | |
2092 | { | |
2093 | write_enn (own_buf); | |
2094 | return 0; | |
2095 | } | |
2096 | } | |
2097 | ||
bd99dc85 PA |
2098 | /* Handle a 'vStopped' packet. */ |
2099 | static void | |
2100 | handle_v_stopped (char *own_buf) | |
2101 | { | |
2102 | /* If we're waiting for GDB to acknowledge a pending stop reply, | |
2103 | consider that done. */ | |
2104 | if (notif_queue) | |
2105 | { | |
2106 | struct vstop_notif *head; | |
2107 | ||
2108 | if (remote_debug) | |
95954743 PA |
2109 | fprintf (stderr, "vStopped: acking %s\n", |
2110 | target_pid_to_str (notif_queue->ptid)); | |
bd99dc85 PA |
2111 | |
2112 | head = notif_queue; | |
2113 | notif_queue = notif_queue->next; | |
2114 | free (head); | |
2115 | } | |
2116 | ||
2117 | /* Push another stop reply, or if there are no more left, an OK. */ | |
2118 | send_next_stop_reply (own_buf); | |
2119 | } | |
2120 | ||
64386c31 DJ |
2121 | /* Handle all of the extended 'v' packets. */ |
2122 | void | |
5b1c542e | 2123 | handle_v_requests (char *own_buf, int packet_len, int *new_packet_len) |
64386c31 | 2124 | { |
db42f210 | 2125 | if (!disable_packet_vCont) |
64386c31 | 2126 | { |
db42f210 PA |
2127 | if (strncmp (own_buf, "vCont;", 6) == 0) |
2128 | { | |
2129 | require_running (own_buf); | |
5b1c542e | 2130 | handle_v_cont (own_buf); |
db42f210 PA |
2131 | return; |
2132 | } | |
64386c31 | 2133 | |
db42f210 PA |
2134 | if (strncmp (own_buf, "vCont?", 6) == 0) |
2135 | { | |
bd99dc85 | 2136 | strcpy (own_buf, "vCont;c;C;s;S;t"); |
db42f210 PA |
2137 | return; |
2138 | } | |
64386c31 DJ |
2139 | } |
2140 | ||
a6b151f1 DJ |
2141 | if (strncmp (own_buf, "vFile:", 6) == 0 |
2142 | && handle_vFile (own_buf, packet_len, new_packet_len)) | |
2143 | return; | |
2144 | ||
2d717e4f DJ |
2145 | if (strncmp (own_buf, "vAttach;", 8) == 0) |
2146 | { | |
901f9912 | 2147 | if ((!extended_protocol || !multi_process) && target_running ()) |
2d717e4f | 2148 | { |
fd96d250 PA |
2149 | fprintf (stderr, "Already debugging a process\n"); |
2150 | write_enn (own_buf); | |
2151 | return; | |
2d717e4f | 2152 | } |
5b1c542e | 2153 | handle_v_attach (own_buf); |
2d717e4f DJ |
2154 | return; |
2155 | } | |
2156 | ||
2157 | if (strncmp (own_buf, "vRun;", 5) == 0) | |
2158 | { | |
901f9912 | 2159 | if ((!extended_protocol || !multi_process) && target_running ()) |
2d717e4f | 2160 | { |
fd96d250 PA |
2161 | fprintf (stderr, "Already debugging a process\n"); |
2162 | write_enn (own_buf); | |
2163 | return; | |
2d717e4f | 2164 | } |
5b1c542e | 2165 | handle_v_run (own_buf); |
2d717e4f DJ |
2166 | return; |
2167 | } | |
2168 | ||
95954743 PA |
2169 | if (strncmp (own_buf, "vKill;", 6) == 0) |
2170 | { | |
2171 | if (!target_running ()) | |
2172 | { | |
2173 | fprintf (stderr, "No process to kill\n"); | |
2174 | write_enn (own_buf); | |
2175 | return; | |
2176 | } | |
2177 | handle_v_kill (own_buf); | |
2178 | return; | |
2179 | } | |
2180 | ||
bd99dc85 PA |
2181 | if (strncmp (own_buf, "vStopped", 8) == 0) |
2182 | { | |
2183 | handle_v_stopped (own_buf); | |
2184 | return; | |
2185 | } | |
2186 | ||
64386c31 DJ |
2187 | /* Otherwise we didn't know what packet it was. Say we didn't |
2188 | understand it. */ | |
2189 | own_buf[0] = 0; | |
2190 | return; | |
2191 | } | |
2192 | ||
bd99dc85 PA |
2193 | /* Resume inferior and wait for another event. In non-stop mode, |
2194 | don't really wait here, but return immediatelly to the event | |
2195 | loop. */ | |
1fd7cdc2 | 2196 | static void |
5b1c542e | 2197 | myresume (char *own_buf, int step, int sig) |
64386c31 DJ |
2198 | { |
2199 | struct thread_resume resume_info[2]; | |
2200 | int n = 0; | |
2bd7c093 | 2201 | int valid_cont_thread; |
a20d5e98 DJ |
2202 | |
2203 | set_desired_inferior (0); | |
64386c31 | 2204 | |
95954743 PA |
2205 | valid_cont_thread = (!ptid_equal (cont_thread, null_ptid) |
2206 | && !ptid_equal (cont_thread, minus_one_ptid)); | |
2bd7c093 PA |
2207 | |
2208 | if (step || sig || valid_cont_thread) | |
64386c31 DJ |
2209 | { |
2210 | resume_info[0].thread | |
2211 | = ((struct inferior_list_entry *) current_inferior)->id; | |
bd99dc85 PA |
2212 | if (step) |
2213 | resume_info[0].kind = resume_step; | |
2214 | else | |
2215 | resume_info[0].kind = resume_continue; | |
64386c31 | 2216 | resume_info[0].sig = sig; |
64386c31 DJ |
2217 | n++; |
2218 | } | |
2bd7c093 PA |
2219 | |
2220 | if (!valid_cont_thread) | |
2221 | { | |
95954743 | 2222 | resume_info[n].thread = minus_one_ptid; |
bd99dc85 | 2223 | resume_info[n].kind = resume_continue; |
2bd7c093 PA |
2224 | resume_info[n].sig = 0; |
2225 | n++; | |
2226 | } | |
64386c31 | 2227 | |
bd99dc85 PA |
2228 | if (!non_stop) |
2229 | enable_async_io (); | |
2230 | ||
2bd7c093 | 2231 | (*the_target->resume) (resume_info, n); |
bd99dc85 PA |
2232 | |
2233 | if (non_stop) | |
2234 | write_ok (own_buf); | |
2235 | else | |
2236 | { | |
95954743 | 2237 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
d20a8ad9 PA |
2238 | |
2239 | if (last_status.kind != TARGET_WAITKIND_EXITED | |
2240 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
2241 | { | |
2242 | current_inferior->last_resume_kind = resume_stop; | |
2243 | current_inferior->last_status = last_status; | |
2244 | } | |
2245 | ||
bd99dc85 PA |
2246 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
2247 | disable_async_io (); | |
6bd31874 JB |
2248 | |
2249 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
2250 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
2251 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); | |
bd99dc85 PA |
2252 | } |
2253 | } | |
2254 | ||
2255 | /* Callback for for_each_inferior. Make a new stop reply for each | |
2256 | stopped thread. */ | |
2257 | ||
95954743 PA |
2258 | static int |
2259 | queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg) | |
bd99dc85 | 2260 | { |
8336d594 | 2261 | struct thread_info *thread = (struct thread_info *) entry; |
bd99dc85 | 2262 | |
8336d594 PA |
2263 | /* For now, assume targets that don't have this callback also don't |
2264 | manage the thread's last_status field. */ | |
2265 | if (the_target->thread_stopped == NULL) | |
95954743 | 2266 | { |
8336d594 PA |
2267 | /* Pass the last stop reply back to GDB, but don't notify |
2268 | yet. */ | |
2269 | queue_stop_reply (entry->id, &thread->last_status); | |
2270 | } | |
2271 | else | |
2272 | { | |
2273 | if (thread_stopped (thread)) | |
2274 | { | |
2275 | if (debug_threads) | |
493e2a69 MS |
2276 | fprintf (stderr, |
2277 | "Reporting thread %s as already stopped with %s\n", | |
8336d594 PA |
2278 | target_pid_to_str (entry->id), |
2279 | target_waitstatus_to_string (&thread->last_status)); | |
2280 | ||
d20a8ad9 PA |
2281 | gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE); |
2282 | ||
8336d594 PA |
2283 | /* Pass the last stop reply back to GDB, but don't notify |
2284 | yet. */ | |
2285 | queue_stop_reply (entry->id, &thread->last_status); | |
2286 | } | |
95954743 PA |
2287 | } |
2288 | ||
2289 | return 0; | |
64386c31 DJ |
2290 | } |
2291 | ||
ce1a5b52 PA |
2292 | /* Set this inferior threads's state as "want-stopped". We won't |
2293 | resume this thread until the client gives us another action for | |
2294 | it. */ | |
8336d594 PA |
2295 | |
2296 | static void | |
2297 | gdb_wants_thread_stopped (struct inferior_list_entry *entry) | |
2298 | { | |
2299 | struct thread_info *thread = (struct thread_info *) entry; | |
2300 | ||
2301 | thread->last_resume_kind = resume_stop; | |
2302 | ||
2303 | if (thread->last_status.kind == TARGET_WAITKIND_IGNORE) | |
2304 | { | |
ce1a5b52 PA |
2305 | /* Most threads are stopped implicitly (all-stop); tag that with |
2306 | signal 0. */ | |
8336d594 PA |
2307 | thread->last_status.kind = TARGET_WAITKIND_STOPPED; |
2308 | thread->last_status.value.sig = TARGET_SIGNAL_0; | |
2309 | } | |
2310 | } | |
2311 | ||
2312 | /* Set all threads' states as "want-stopped". */ | |
2313 | ||
2314 | static void | |
2315 | gdb_wants_all_threads_stopped (void) | |
2316 | { | |
2317 | for_each_inferior (&all_threads, gdb_wants_thread_stopped); | |
2318 | } | |
2319 | ||
2320 | /* Clear the gdb_detached flag of every process. */ | |
2321 | ||
2322 | static void | |
2323 | gdb_reattached_process (struct inferior_list_entry *entry) | |
2324 | { | |
2325 | struct process_info *process = (struct process_info *) entry; | |
2326 | ||
2327 | process->gdb_detached = 0; | |
2328 | } | |
2329 | ||
5b1c542e PA |
2330 | /* Status handler for the '?' packet. */ |
2331 | ||
2332 | static void | |
2333 | handle_status (char *own_buf) | |
2334 | { | |
8336d594 PA |
2335 | /* GDB is connected, don't forward events to the target anymore. */ |
2336 | for_each_inferior (&all_processes, gdb_reattached_process); | |
bd99dc85 PA |
2337 | |
2338 | /* In non-stop mode, we must send a stop reply for each stopped | |
2339 | thread. In all-stop mode, just send one for the first stopped | |
2340 | thread we find. */ | |
2341 | ||
2342 | if (non_stop) | |
2343 | { | |
8336d594 PA |
2344 | discard_queued_stop_replies (-1); |
2345 | find_inferior (&all_threads, queue_stop_reply_callback, NULL); | |
bd99dc85 PA |
2346 | |
2347 | /* The first is sent immediatly. OK is sent if there is no | |
2348 | stopped thread, which is the same handling of the vStopped | |
2349 | packet (by design). */ | |
2350 | send_next_stop_reply (own_buf); | |
2351 | } | |
5b1c542e | 2352 | else |
bd99dc85 | 2353 | { |
7984d532 | 2354 | pause_all (0); |
fa593d66 | 2355 | stabilize_threads (); |
8336d594 PA |
2356 | gdb_wants_all_threads_stopped (); |
2357 | ||
bd99dc85 | 2358 | if (all_threads.head) |
8336d594 PA |
2359 | { |
2360 | struct target_waitstatus status; | |
2361 | ||
2362 | status.kind = TARGET_WAITKIND_STOPPED; | |
2363 | status.value.sig = TARGET_SIGNAL_TRAP; | |
2364 | prepare_resume_reply (own_buf, | |
2365 | all_threads.head->id, &status); | |
2366 | } | |
bd99dc85 PA |
2367 | else |
2368 | strcpy (own_buf, "W00"); | |
2369 | } | |
5b1c542e PA |
2370 | } |
2371 | ||
dd24457d DJ |
2372 | static void |
2373 | gdbserver_version (void) | |
2374 | { | |
c16158bc | 2375 | printf ("GNU gdbserver %s%s\n" |
67827812 | 2376 | "Copyright (C) 2012 Free Software Foundation, Inc.\n" |
493e2a69 MS |
2377 | "gdbserver is free software, covered by the " |
2378 | "GNU General Public License.\n" | |
dd24457d | 2379 | "This gdbserver was configured as \"%s\"\n", |
c16158bc | 2380 | PKGVERSION, version, host_name); |
dd24457d DJ |
2381 | } |
2382 | ||
0bc68c49 | 2383 | static void |
c16158bc | 2384 | gdbserver_usage (FILE *stream) |
0bc68c49 | 2385 | { |
c16158bc JM |
2386 | fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n" |
2387 | "\tgdbserver [OPTIONS] --attach COMM PID\n" | |
2388 | "\tgdbserver [OPTIONS] --multi COMM\n" | |
2389 | "\n" | |
2390 | "COMM may either be a tty device (for serial debugging), or \n" | |
2391 | "HOST:PORT to listen for a TCP connection.\n" | |
2392 | "\n" | |
2393 | "Options:\n" | |
62709adf PA |
2394 | " --debug Enable general debugging output.\n" |
2395 | " --remote-debug Enable remote protocol debugging output.\n" | |
2396 | " --version Display version information and exit.\n" | |
03f2bd59 JK |
2397 | " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n" |
2398 | " --once Exit after the first connection has " | |
2399 | "closed.\n"); | |
c16158bc JM |
2400 | if (REPORT_BUGS_TO[0] && stream == stdout) |
2401 | fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO); | |
0bc68c49 DJ |
2402 | } |
2403 | ||
db42f210 PA |
2404 | static void |
2405 | gdbserver_show_disableable (FILE *stream) | |
2406 | { | |
2407 | fprintf (stream, "Disableable packets:\n" | |
2408 | " vCont \tAll vCont packets\n" | |
2409 | " qC \tQuerying the current thread\n" | |
2410 | " qfThreadInfo\tThread listing\n" | |
493e2a69 MS |
2411 | " Tthread \tPassing the thread specifier in the " |
2412 | "T stop reply packet\n" | |
db42f210 PA |
2413 | " threads \tAll of the above\n"); |
2414 | } | |
2415 | ||
2416 | ||
2d717e4f DJ |
2417 | #undef require_running |
2418 | #define require_running(BUF) \ | |
2419 | if (!target_running ()) \ | |
2420 | { \ | |
2421 | write_enn (BUF); \ | |
2422 | break; \ | |
2423 | } | |
2424 | ||
95954743 PA |
2425 | static int |
2426 | first_thread_of (struct inferior_list_entry *entry, void *args) | |
2427 | { | |
2428 | int pid = * (int *) args; | |
2429 | ||
2430 | if (ptid_get_pid (entry->id) == pid) | |
2431 | return 1; | |
2432 | ||
2433 | return 0; | |
2434 | } | |
2435 | ||
2436 | static void | |
2437 | kill_inferior_callback (struct inferior_list_entry *entry) | |
2438 | { | |
2439 | struct process_info *process = (struct process_info *) entry; | |
2440 | int pid = ptid_get_pid (process->head.id); | |
2441 | ||
2442 | kill_inferior (pid); | |
2443 | discard_queued_stop_replies (pid); | |
2444 | } | |
2445 | ||
9f767825 DE |
2446 | /* Callback for for_each_inferior to detach or kill the inferior, |
2447 | depending on whether we attached to it or not. | |
2448 | We inform the user whether we're detaching or killing the process | |
2449 | as this is only called when gdbserver is about to exit. */ | |
2450 | ||
95954743 PA |
2451 | static void |
2452 | detach_or_kill_inferior_callback (struct inferior_list_entry *entry) | |
2453 | { | |
2454 | struct process_info *process = (struct process_info *) entry; | |
2455 | int pid = ptid_get_pid (process->head.id); | |
2456 | ||
2457 | if (process->attached) | |
2458 | detach_inferior (pid); | |
2459 | else | |
2460 | kill_inferior (pid); | |
2461 | ||
2462 | discard_queued_stop_replies (pid); | |
2463 | } | |
2464 | ||
9f767825 DE |
2465 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
2466 | the pids of started inferiors. */ | |
2467 | ||
2468 | static void | |
2469 | print_started_pid (struct inferior_list_entry *entry) | |
2470 | { | |
2471 | struct process_info *process = (struct process_info *) entry; | |
2472 | ||
2473 | if (! process->attached) | |
2474 | { | |
2475 | int pid = ptid_get_pid (process->head.id); | |
2476 | fprintf (stderr, " %d", pid); | |
2477 | } | |
2478 | } | |
2479 | ||
2480 | /* for_each_inferior callback for detach_or_kill_for_exit to print | |
2481 | the pids of attached inferiors. */ | |
2482 | ||
2483 | static void | |
2484 | print_attached_pid (struct inferior_list_entry *entry) | |
2485 | { | |
2486 | struct process_info *process = (struct process_info *) entry; | |
2487 | ||
2488 | if (process->attached) | |
2489 | { | |
2490 | int pid = ptid_get_pid (process->head.id); | |
2491 | fprintf (stderr, " %d", pid); | |
2492 | } | |
2493 | } | |
2494 | ||
2495 | /* Call this when exiting gdbserver with possible inferiors that need | |
2496 | to be killed or detached from. */ | |
2497 | ||
2498 | static void | |
2499 | detach_or_kill_for_exit (void) | |
2500 | { | |
2501 | /* First print a list of the inferiors we will be killing/detaching. | |
2502 | This is to assist the user, for example, in case the inferior unexpectedly | |
2503 | dies after we exit: did we screw up or did the inferior exit on its own? | |
2504 | Having this info will save some head-scratching. */ | |
2505 | ||
2506 | if (have_started_inferiors_p ()) | |
2507 | { | |
2508 | fprintf (stderr, "Killing process(es):"); | |
2509 | for_each_inferior (&all_processes, print_started_pid); | |
2510 | fprintf (stderr, "\n"); | |
2511 | } | |
2512 | if (have_attached_inferiors_p ()) | |
2513 | { | |
2514 | fprintf (stderr, "Detaching process(es):"); | |
2515 | for_each_inferior (&all_processes, print_attached_pid); | |
2516 | fprintf (stderr, "\n"); | |
2517 | } | |
2518 | ||
2519 | /* Now we can kill or detach the inferiors. */ | |
2520 | ||
2521 | for_each_inferior (&all_processes, detach_or_kill_inferior_callback); | |
2522 | } | |
2523 | ||
c906108c | 2524 | int |
da85418c | 2525 | main (int argc, char *argv[]) |
c906108c | 2526 | { |
0729219d DJ |
2527 | int bad_attach; |
2528 | int pid; | |
2d717e4f DJ |
2529 | char *arg_end, *port; |
2530 | char **next_arg = &argv[1]; | |
2531 | int multi_mode = 0; | |
2532 | int attach = 0; | |
2533 | int was_running; | |
c906108c | 2534 | |
2d717e4f | 2535 | while (*next_arg != NULL && **next_arg == '-') |
dd24457d | 2536 | { |
2d717e4f DJ |
2537 | if (strcmp (*next_arg, "--version") == 0) |
2538 | { | |
2539 | gdbserver_version (); | |
2540 | exit (0); | |
2541 | } | |
2542 | else if (strcmp (*next_arg, "--help") == 0) | |
2543 | { | |
c16158bc | 2544 | gdbserver_usage (stdout); |
2d717e4f DJ |
2545 | exit (0); |
2546 | } | |
2547 | else if (strcmp (*next_arg, "--attach") == 0) | |
2548 | attach = 1; | |
2549 | else if (strcmp (*next_arg, "--multi") == 0) | |
2550 | multi_mode = 1; | |
ccd213ac DJ |
2551 | else if (strcmp (*next_arg, "--wrapper") == 0) |
2552 | { | |
2553 | next_arg++; | |
2554 | ||
2555 | wrapper_argv = next_arg; | |
2556 | while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) | |
2557 | next_arg++; | |
2558 | ||
2559 | if (next_arg == wrapper_argv || *next_arg == NULL) | |
2560 | { | |
c16158bc | 2561 | gdbserver_usage (stderr); |
ccd213ac DJ |
2562 | exit (1); |
2563 | } | |
2564 | ||
2565 | /* Consume the "--". */ | |
2566 | *next_arg = NULL; | |
2567 | } | |
2d717e4f DJ |
2568 | else if (strcmp (*next_arg, "--debug") == 0) |
2569 | debug_threads = 1; | |
62709adf PA |
2570 | else if (strcmp (*next_arg, "--remote-debug") == 0) |
2571 | remote_debug = 1; | |
db42f210 PA |
2572 | else if (strcmp (*next_arg, "--disable-packet") == 0) |
2573 | { | |
2574 | gdbserver_show_disableable (stdout); | |
2575 | exit (0); | |
2576 | } | |
2577 | else if (strncmp (*next_arg, | |
2578 | "--disable-packet=", | |
2579 | sizeof ("--disable-packet=") - 1) == 0) | |
2580 | { | |
2581 | char *packets, *tok; | |
2582 | ||
2583 | packets = *next_arg += sizeof ("--disable-packet=") - 1; | |
2584 | for (tok = strtok (packets, ","); | |
2585 | tok != NULL; | |
2586 | tok = strtok (NULL, ",")) | |
2587 | { | |
2588 | if (strcmp ("vCont", tok) == 0) | |
2589 | disable_packet_vCont = 1; | |
2590 | else if (strcmp ("Tthread", tok) == 0) | |
2591 | disable_packet_Tthread = 1; | |
2592 | else if (strcmp ("qC", tok) == 0) | |
2593 | disable_packet_qC = 1; | |
2594 | else if (strcmp ("qfThreadInfo", tok) == 0) | |
2595 | disable_packet_qfThreadInfo = 1; | |
2596 | else if (strcmp ("threads", tok) == 0) | |
2597 | { | |
2598 | disable_packet_vCont = 1; | |
2599 | disable_packet_Tthread = 1; | |
2600 | disable_packet_qC = 1; | |
2601 | disable_packet_qfThreadInfo = 1; | |
2602 | } | |
2603 | else | |
2604 | { | |
2605 | fprintf (stderr, "Don't know how to disable \"%s\".\n\n", | |
2606 | tok); | |
2607 | gdbserver_show_disableable (stderr); | |
2608 | exit (1); | |
2609 | } | |
2610 | } | |
2611 | } | |
e0f9f062 DE |
2612 | else if (strcmp (*next_arg, "-") == 0) |
2613 | { | |
2614 | /* "-" specifies a stdio connection and is a form of port | |
2615 | specification. */ | |
2616 | *next_arg = STDIO_CONNECTION_NAME; | |
2617 | break; | |
2618 | } | |
03583c20 UW |
2619 | else if (strcmp (*next_arg, "--disable-randomization") == 0) |
2620 | disable_randomization = 1; | |
2621 | else if (strcmp (*next_arg, "--no-disable-randomization") == 0) | |
2622 | disable_randomization = 0; | |
03f2bd59 JK |
2623 | else if (strcmp (*next_arg, "--once") == 0) |
2624 | run_once = 1; | |
2d717e4f DJ |
2625 | else |
2626 | { | |
2627 | fprintf (stderr, "Unknown argument: %s\n", *next_arg); | |
2628 | exit (1); | |
2629 | } | |
dd24457d | 2630 | |
2d717e4f DJ |
2631 | next_arg++; |
2632 | continue; | |
dd24457d DJ |
2633 | } |
2634 | ||
c5aa993b | 2635 | if (setjmp (toplevel)) |
c906108c | 2636 | { |
c5aa993b JM |
2637 | fprintf (stderr, "Exiting\n"); |
2638 | exit (1); | |
c906108c SS |
2639 | } |
2640 | ||
2d717e4f DJ |
2641 | port = *next_arg; |
2642 | next_arg++; | |
2643 | if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) | |
2644 | { | |
c16158bc | 2645 | gdbserver_usage (stderr); |
2d717e4f DJ |
2646 | exit (1); |
2647 | } | |
2648 | ||
e0f9f062 DE |
2649 | /* We need to know whether the remote connection is stdio before |
2650 | starting the inferior. Inferiors created in this scenario have | |
2651 | stdin,stdout redirected. So do this here before we call | |
2652 | start_inferior. */ | |
2653 | remote_prepare (port); | |
2654 | ||
0729219d DJ |
2655 | bad_attach = 0; |
2656 | pid = 0; | |
2d717e4f DJ |
2657 | |
2658 | /* --attach used to come after PORT, so allow it there for | |
2659 | compatibility. */ | |
2660 | if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) | |
45b7b345 | 2661 | { |
2d717e4f DJ |
2662 | attach = 1; |
2663 | next_arg++; | |
45b7b345 DJ |
2664 | } |
2665 | ||
2d717e4f DJ |
2666 | if (attach |
2667 | && (*next_arg == NULL | |
2668 | || (*next_arg)[0] == '\0' | |
2669 | || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 | |
2670 | || *arg_end != '\0' | |
2671 | || next_arg[1] != NULL)) | |
2672 | bad_attach = 1; | |
2673 | ||
2674 | if (bad_attach) | |
dd24457d | 2675 | { |
c16158bc | 2676 | gdbserver_usage (stderr); |
dd24457d DJ |
2677 | exit (1); |
2678 | } | |
c906108c | 2679 | |
a20d5e98 | 2680 | initialize_async_io (); |
4ce44c66 | 2681 | initialize_low (); |
219f2f23 PA |
2682 | if (target_supports_tracepoints ()) |
2683 | initialize_tracepoint (); | |
4ce44c66 | 2684 | |
bca929d3 DE |
2685 | own_buf = xmalloc (PBUFSIZ + 1); |
2686 | mem_buf = xmalloc (PBUFSIZ); | |
0a30fbc4 | 2687 | |
2d717e4f | 2688 | if (pid == 0 && *next_arg != NULL) |
45b7b345 | 2689 | { |
2d717e4f DJ |
2690 | int i, n; |
2691 | ||
2692 | n = argc - (next_arg - argv); | |
bca929d3 | 2693 | program_argv = xmalloc (sizeof (char *) * (n + 1)); |
2d717e4f | 2694 | for (i = 0; i < n; i++) |
bca929d3 | 2695 | program_argv[i] = xstrdup (next_arg[i]); |
2d717e4f DJ |
2696 | program_argv[i] = NULL; |
2697 | ||
45b7b345 | 2698 | /* Wait till we are at first instruction in program. */ |
5b1c542e | 2699 | start_inferior (program_argv); |
c906108c | 2700 | |
c588c53c MS |
2701 | /* We are now (hopefully) stopped at the first instruction of |
2702 | the target process. This assumes that the target process was | |
2703 | successfully created. */ | |
45b7b345 | 2704 | } |
2d717e4f DJ |
2705 | else if (pid != 0) |
2706 | { | |
5b1c542e | 2707 | if (attach_inferior (pid) == -1) |
2d717e4f DJ |
2708 | error ("Attaching not supported on this target"); |
2709 | ||
2710 | /* Otherwise succeeded. */ | |
2711 | } | |
45b7b345 DJ |
2712 | else |
2713 | { | |
5b1c542e PA |
2714 | last_status.kind = TARGET_WAITKIND_EXITED; |
2715 | last_status.value.integer = 0; | |
95954743 | 2716 | last_ptid = minus_one_ptid; |
45b7b345 | 2717 | } |
c906108c | 2718 | |
311de423 PA |
2719 | /* Don't report shared library events on the initial connection, |
2720 | even if some libraries are preloaded. Avoids the "stopped by | |
2721 | shared library event" notice on gdb side. */ | |
2722 | dlls_changed = 0; | |
2723 | ||
8264bb58 DJ |
2724 | if (setjmp (toplevel)) |
2725 | { | |
f128d5e9 PA |
2726 | /* If something fails and longjmps while detaching or killing |
2727 | inferiors, we'd end up here again, stuck in an infinite loop | |
2728 | trap. Be sure that if that happens, we exit immediately | |
2729 | instead. */ | |
01b17894 PA |
2730 | if (setjmp (toplevel) == 0) |
2731 | detach_or_kill_for_exit (); | |
2732 | else | |
2733 | fprintf (stderr, "Detach or kill failed. Exiting\n"); | |
8264bb58 DJ |
2734 | exit (1); |
2735 | } | |
2736 | ||
5b1c542e PA |
2737 | if (last_status.kind == TARGET_WAITKIND_EXITED |
2738 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
2d717e4f DJ |
2739 | was_running = 0; |
2740 | else | |
2741 | was_running = 1; | |
2742 | ||
2743 | if (!was_running && !multi_mode) | |
c588c53c | 2744 | { |
2d717e4f | 2745 | fprintf (stderr, "No program to debug. GDBserver exiting.\n"); |
c588c53c MS |
2746 | exit (1); |
2747 | } | |
2748 | ||
c906108c SS |
2749 | while (1) |
2750 | { | |
a6f3e723 | 2751 | noack_mode = 0; |
95954743 | 2752 | multi_process = 0; |
8336d594 PA |
2753 | /* Be sure we're out of tfind mode. */ |
2754 | current_traceframe = -1; | |
bd99dc85 | 2755 | |
2d717e4f | 2756 | remote_open (port); |
c906108c | 2757 | |
2d717e4f DJ |
2758 | if (setjmp (toplevel) != 0) |
2759 | { | |
2760 | /* An error occurred. */ | |
2761 | if (response_needed) | |
2762 | { | |
2763 | write_enn (own_buf); | |
2764 | putpkt (own_buf); | |
2765 | } | |
2766 | } | |
2767 | ||
bd99dc85 | 2768 | /* Wait for events. This will return when all event sources are |
8336d594 | 2769 | removed from the event loop. */ |
bd99dc85 PA |
2770 | start_event_loop (); |
2771 | ||
2772 | /* If an exit was requested (using the "monitor exit" command), | |
2773 | terminate now. The only other way to get here is for | |
2774 | getpkt to fail; close the connection and reopen it at the | |
2775 | top of the loop. */ | |
2776 | ||
03f2bd59 | 2777 | if (exit_requested || run_once) |
c906108c | 2778 | { |
01b17894 PA |
2779 | /* If something fails and longjmps while detaching or |
2780 | killing inferiors, we'd end up here again, stuck in an | |
2781 | infinite loop trap. Be sure that if that happens, we | |
2782 | exit immediately instead. */ | |
2783 | if (setjmp (toplevel) == 0) | |
2784 | { | |
2785 | detach_or_kill_for_exit (); | |
2786 | exit (0); | |
2787 | } | |
2788 | else | |
2789 | { | |
2790 | fprintf (stderr, "Detach or kill failed. Exiting\n"); | |
2791 | exit (1); | |
2792 | } | |
bd99dc85 | 2793 | } |
8336d594 PA |
2794 | |
2795 | fprintf (stderr, | |
2796 | "Remote side has terminated connection. " | |
2797 | "GDBserver will reopen the connection.\n"); | |
2798 | ||
2799 | if (tracing) | |
2800 | { | |
2801 | if (disconnected_tracing) | |
2802 | { | |
2803 | /* Try to enable non-stop/async mode, so we we can both | |
2804 | wait for an async socket accept, and handle async | |
2805 | target events simultaneously. There's also no point | |
2806 | either in having the target always stop all threads, | |
2807 | when we're going to pass signals down without | |
2808 | informing GDB. */ | |
2809 | if (!non_stop) | |
2810 | { | |
2811 | if (start_non_stop (1)) | |
2812 | non_stop = 1; | |
2813 | ||
2814 | /* Detaching implicitly resumes all threads; simply | |
2815 | disconnecting does not. */ | |
2816 | } | |
2817 | } | |
2818 | else | |
2819 | { | |
2820 | fprintf (stderr, | |
2821 | "Disconnected tracing disabled; stopping trace run.\n"); | |
2822 | stop_tracing (); | |
2823 | } | |
2824 | } | |
bd99dc85 PA |
2825 | } |
2826 | } | |
01f9e8fa | 2827 | |
bd99dc85 PA |
2828 | /* Event loop callback that handles a serial event. The first byte in |
2829 | the serial buffer gets us here. We expect characters to arrive at | |
2830 | a brisk pace, so we read the rest of the packet with a blocking | |
2831 | getpkt call. */ | |
01f9e8fa | 2832 | |
8336d594 | 2833 | static int |
bd99dc85 PA |
2834 | process_serial_event (void) |
2835 | { | |
2836 | char ch; | |
2837 | int i = 0; | |
2838 | int signal; | |
2839 | unsigned int len; | |
764880b7 | 2840 | int res; |
bd99dc85 | 2841 | CORE_ADDR mem_addr; |
95954743 | 2842 | int pid; |
bd99dc85 PA |
2843 | unsigned char sig; |
2844 | int packet_len; | |
2845 | int new_packet_len = -1; | |
2846 | ||
2847 | /* Used to decide when gdbserver should exit in | |
2848 | multi-mode/remote. */ | |
2849 | static int have_ran = 0; | |
2850 | ||
2851 | if (!have_ran) | |
2852 | have_ran = target_running (); | |
2853 | ||
2854 | disable_async_io (); | |
2855 | ||
2856 | response_needed = 0; | |
2857 | packet_len = getpkt (own_buf); | |
2858 | if (packet_len <= 0) | |
2859 | { | |
bd99dc85 | 2860 | remote_close (); |
8336d594 PA |
2861 | /* Force an event loop break. */ |
2862 | return -1; | |
bd99dc85 PA |
2863 | } |
2864 | response_needed = 1; | |
2865 | ||
2866 | i = 0; | |
2867 | ch = own_buf[i++]; | |
2868 | switch (ch) | |
2869 | { | |
2870 | case 'q': | |
2871 | handle_query (own_buf, packet_len, &new_packet_len); | |
2872 | break; | |
2873 | case 'Q': | |
2874 | handle_general_set (own_buf); | |
2875 | break; | |
2876 | case 'D': | |
2877 | require_running (own_buf); | |
95954743 PA |
2878 | |
2879 | if (multi_process) | |
2880 | { | |
2881 | i++; /* skip ';' */ | |
2882 | pid = strtol (&own_buf[i], NULL, 16); | |
2883 | } | |
2884 | else | |
2885 | pid = | |
2886 | ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id); | |
2887 | ||
8336d594 PA |
2888 | if (tracing && disconnected_tracing) |
2889 | { | |
2890 | struct thread_resume resume_info; | |
2891 | struct process_info *process = find_process_pid (pid); | |
2892 | ||
2893 | if (process == NULL) | |
2894 | { | |
2895 | write_enn (own_buf); | |
2896 | break; | |
2897 | } | |
2898 | ||
2899 | fprintf (stderr, | |
2900 | "Disconnected tracing in effect, " | |
2901 | "leaving gdbserver attached to the process\n"); | |
2902 | ||
2903 | /* Make sure we're in non-stop/async mode, so we we can both | |
2904 | wait for an async socket accept, and handle async target | |
2905 | events simultaneously. There's also no point either in | |
2906 | having the target stop all threads, when we're going to | |
2907 | pass signals down without informing GDB. */ | |
2908 | if (!non_stop) | |
2909 | { | |
2910 | if (debug_threads) | |
2911 | fprintf (stderr, "Forcing non-stop mode\n"); | |
2912 | ||
2913 | non_stop = 1; | |
2914 | start_non_stop (1); | |
2915 | } | |
2916 | ||
2917 | process->gdb_detached = 1; | |
2918 | ||
2919 | /* Detaching implicitly resumes all threads. */ | |
2920 | resume_info.thread = minus_one_ptid; | |
2921 | resume_info.kind = resume_continue; | |
2922 | resume_info.sig = 0; | |
2923 | (*the_target->resume) (&resume_info, 1); | |
2924 | ||
2925 | write_ok (own_buf); | |
2926 | break; /* from switch/case */ | |
2927 | } | |
2928 | ||
95954743 | 2929 | fprintf (stderr, "Detaching from process %d\n", pid); |
8336d594 | 2930 | stop_tracing (); |
95954743 | 2931 | if (detach_inferior (pid) != 0) |
bd99dc85 PA |
2932 | write_enn (own_buf); |
2933 | else | |
2934 | { | |
95954743 | 2935 | discard_queued_stop_replies (pid); |
bd99dc85 PA |
2936 | write_ok (own_buf); |
2937 | ||
2938 | if (extended_protocol) | |
c906108c | 2939 | { |
bd99dc85 PA |
2940 | /* Treat this like a normal program exit. */ |
2941 | last_status.kind = TARGET_WAITKIND_EXITED; | |
2942 | last_status.value.integer = 0; | |
95954743 | 2943 | last_ptid = pid_to_ptid (pid); |
2d717e4f | 2944 | |
bd99dc85 PA |
2945 | current_inferior = NULL; |
2946 | } | |
2947 | else | |
2948 | { | |
2949 | putpkt (own_buf); | |
2950 | remote_close (); | |
2951 | ||
2952 | /* If we are attached, then we can exit. Otherwise, we | |
2953 | need to hang around doing nothing, until the child is | |
2954 | gone. */ | |
71f55dd8 | 2955 | join_inferior (pid); |
bd99dc85 PA |
2956 | exit (0); |
2957 | } | |
2958 | } | |
2959 | break; | |
2960 | case '!': | |
2961 | extended_protocol = 1; | |
2962 | write_ok (own_buf); | |
2963 | break; | |
2964 | case '?': | |
2965 | handle_status (own_buf); | |
2966 | break; | |
2967 | case 'H': | |
2968 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') | |
2969 | { | |
95954743 PA |
2970 | ptid_t gdb_id, thread_id; |
2971 | int pid; | |
bd99dc85 PA |
2972 | |
2973 | require_running (own_buf); | |
95954743 PA |
2974 | |
2975 | gdb_id = read_ptid (&own_buf[2], NULL); | |
2976 | ||
2977 | pid = ptid_get_pid (gdb_id); | |
2978 | ||
2979 | if (ptid_equal (gdb_id, null_ptid) | |
2980 | || ptid_equal (gdb_id, minus_one_ptid)) | |
2981 | thread_id = null_ptid; | |
2982 | else if (pid != 0 | |
2983 | && ptid_equal (pid_to_ptid (pid), | |
2984 | gdb_id)) | |
2985 | { | |
2986 | struct thread_info *thread = | |
2987 | (struct thread_info *) find_inferior (&all_threads, | |
2988 | first_thread_of, | |
2989 | &pid); | |
2990 | if (!thread) | |
2991 | { | |
2992 | write_enn (own_buf); | |
2993 | break; | |
2994 | } | |
2995 | ||
2996 | thread_id = ((struct inferior_list_entry *)thread)->id; | |
2997 | } | |
bd99dc85 PA |
2998 | else |
2999 | { | |
3000 | thread_id = gdb_id_to_thread_id (gdb_id); | |
95954743 | 3001 | if (ptid_equal (thread_id, null_ptid)) |
c906108c | 3002 | { |
a06660f7 | 3003 | write_enn (own_buf); |
c906108c SS |
3004 | break; |
3005 | } | |
c906108c SS |
3006 | } |
3007 | ||
bd99dc85 | 3008 | if (own_buf[1] == 'g') |
c906108c | 3009 | { |
95954743 | 3010 | if (ptid_equal (thread_id, null_ptid)) |
c906108c | 3011 | { |
bd99dc85 PA |
3012 | /* GDB is telling us to choose any thread. Check if |
3013 | the currently selected thread is still valid. If | |
3014 | it is not, select the first available. */ | |
3015 | struct thread_info *thread = | |
3016 | (struct thread_info *) find_inferior_id (&all_threads, | |
3017 | general_thread); | |
3018 | if (thread == NULL) | |
3019 | thread_id = all_threads.head->id; | |
c906108c | 3020 | } |
bd99dc85 PA |
3021 | |
3022 | general_thread = thread_id; | |
3023 | set_desired_inferior (1); | |
c906108c | 3024 | } |
bd99dc85 PA |
3025 | else if (own_buf[1] == 'c') |
3026 | cont_thread = thread_id; | |
c906108c | 3027 | |
bd99dc85 PA |
3028 | write_ok (own_buf); |
3029 | } | |
3030 | else | |
3031 | { | |
3032 | /* Silently ignore it so that gdb can extend the protocol | |
3033 | without compatibility headaches. */ | |
3034 | own_buf[0] = '\0'; | |
2d717e4f | 3035 | } |
bd99dc85 PA |
3036 | break; |
3037 | case 'g': | |
219f2f23 PA |
3038 | require_running (own_buf); |
3039 | if (current_traceframe >= 0) | |
3040 | { | |
3041 | struct regcache *regcache = new_register_cache (); | |
3042 | ||
3043 | if (fetch_traceframe_registers (current_traceframe, | |
3044 | regcache, -1) == 0) | |
3045 | registers_to_string (regcache, own_buf); | |
3046 | else | |
3047 | write_enn (own_buf); | |
3048 | free_register_cache (regcache); | |
3049 | } | |
3050 | else | |
3051 | { | |
3052 | struct regcache *regcache; | |
3053 | ||
3054 | set_desired_inferior (1); | |
3055 | regcache = get_thread_regcache (current_inferior, 1); | |
3056 | registers_to_string (regcache, own_buf); | |
3057 | } | |
bd99dc85 PA |
3058 | break; |
3059 | case 'G': | |
219f2f23 PA |
3060 | require_running (own_buf); |
3061 | if (current_traceframe >= 0) | |
3062 | write_enn (own_buf); | |
3063 | else | |
3064 | { | |
442ea881 PA |
3065 | struct regcache *regcache; |
3066 | ||
442ea881 PA |
3067 | set_desired_inferior (1); |
3068 | regcache = get_thread_regcache (current_inferior, 1); | |
3069 | registers_from_string (regcache, &own_buf[1]); | |
3070 | write_ok (own_buf); | |
3071 | } | |
bd99dc85 PA |
3072 | break; |
3073 | case 'm': | |
3074 | require_running (own_buf); | |
3075 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
764880b7 PA |
3076 | res = gdb_read_memory (mem_addr, mem_buf, len); |
3077 | if (res < 0) | |
bd99dc85 | 3078 | write_enn (own_buf); |
764880b7 PA |
3079 | else |
3080 | convert_int_to_ascii (mem_buf, own_buf, res); | |
bd99dc85 PA |
3081 | break; |
3082 | case 'M': | |
3083 | require_running (own_buf); | |
fa593d66 | 3084 | decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf); |
90d74c30 | 3085 | if (gdb_write_memory (mem_addr, mem_buf, len) == 0) |
bd99dc85 PA |
3086 | write_ok (own_buf); |
3087 | else | |
3088 | write_enn (own_buf); | |
3089 | break; | |
3090 | case 'X': | |
3091 | require_running (own_buf); | |
3092 | if (decode_X_packet (&own_buf[1], packet_len - 1, | |
fa593d66 | 3093 | &mem_addr, &len, &mem_buf) < 0 |
90d74c30 | 3094 | || gdb_write_memory (mem_addr, mem_buf, len) != 0) |
bd99dc85 PA |
3095 | write_enn (own_buf); |
3096 | else | |
3097 | write_ok (own_buf); | |
3098 | break; | |
3099 | case 'C': | |
3100 | require_running (own_buf); | |
3101 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
3102 | if (target_signal_to_host_p (sig)) | |
3103 | signal = target_signal_to_host (sig); | |
3104 | else | |
3105 | signal = 0; | |
3106 | myresume (own_buf, 0, signal); | |
3107 | break; | |
3108 | case 'S': | |
3109 | require_running (own_buf); | |
3110 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
3111 | if (target_signal_to_host_p (sig)) | |
3112 | signal = target_signal_to_host (sig); | |
3113 | else | |
3114 | signal = 0; | |
3115 | myresume (own_buf, 1, signal); | |
3116 | break; | |
3117 | case 'c': | |
3118 | require_running (own_buf); | |
3119 | signal = 0; | |
3120 | myresume (own_buf, 0, signal); | |
3121 | break; | |
3122 | case 's': | |
3123 | require_running (own_buf); | |
3124 | signal = 0; | |
3125 | myresume (own_buf, 1, signal); | |
3126 | break; | |
c6314022 AR |
3127 | case 'Z': /* insert_ ... */ |
3128 | /* Fallthrough. */ | |
3129 | case 'z': /* remove_ ... */ | |
bd99dc85 PA |
3130 | { |
3131 | char *lenptr; | |
3132 | char *dataptr; | |
3133 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
3134 | int len = strtol (lenptr + 1, &dataptr, 16); | |
3135 | char type = own_buf[1]; | |
c6314022 | 3136 | int res; |
d993e290 | 3137 | const int insert = ch == 'Z'; |
c6314022 | 3138 | |
d993e290 PA |
3139 | /* Default to unrecognized/unsupported. */ |
3140 | res = 1; | |
3141 | switch (type) | |
3142 | { | |
3143 | case '0': /* software-breakpoint */ | |
3144 | case '1': /* hardware-breakpoint */ | |
3145 | case '2': /* write watchpoint */ | |
3146 | case '3': /* read watchpoint */ | |
3147 | case '4': /* access watchpoint */ | |
3148 | require_running (own_buf); | |
3149 | if (insert && the_target->insert_point != NULL) | |
3150 | res = (*the_target->insert_point) (type, addr, len); | |
3151 | else if (!insert && the_target->remove_point != NULL) | |
3152 | res = (*the_target->remove_point) (type, addr, len); | |
3153 | break; | |
3154 | default: | |
3155 | break; | |
3156 | } | |
bd99dc85 | 3157 | |
c6314022 AR |
3158 | if (res == 0) |
3159 | write_ok (own_buf); | |
3160 | else if (res == 1) | |
3161 | /* Unsupported. */ | |
3162 | own_buf[0] = '\0'; | |
bd99dc85 | 3163 | else |
c6314022 | 3164 | write_enn (own_buf); |
bd99dc85 PA |
3165 | break; |
3166 | } | |
3167 | case 'k': | |
3168 | response_needed = 0; | |
3169 | if (!target_running ()) | |
95954743 PA |
3170 | /* The packet we received doesn't make sense - but we can't |
3171 | reply to it, either. */ | |
8336d594 | 3172 | return 0; |
c906108c | 3173 | |
95954743 PA |
3174 | fprintf (stderr, "Killing all inferiors\n"); |
3175 | for_each_inferior (&all_processes, kill_inferior_callback); | |
c906108c | 3176 | |
bd99dc85 PA |
3177 | /* When using the extended protocol, we wait with no program |
3178 | running. The traditional protocol will exit instead. */ | |
3179 | if (extended_protocol) | |
3180 | { | |
3181 | last_status.kind = TARGET_WAITKIND_EXITED; | |
3182 | last_status.value.sig = TARGET_SIGNAL_KILL; | |
8336d594 | 3183 | return 0; |
bd99dc85 PA |
3184 | } |
3185 | else | |
8336d594 PA |
3186 | exit (0); |
3187 | ||
bd99dc85 PA |
3188 | case 'T': |
3189 | { | |
95954743 | 3190 | ptid_t gdb_id, thread_id; |
bd99dc85 PA |
3191 | |
3192 | require_running (own_buf); | |
95954743 PA |
3193 | |
3194 | gdb_id = read_ptid (&own_buf[1], NULL); | |
bd99dc85 | 3195 | thread_id = gdb_id_to_thread_id (gdb_id); |
95954743 | 3196 | if (ptid_equal (thread_id, null_ptid)) |
bd99dc85 PA |
3197 | { |
3198 | write_enn (own_buf); | |
3199 | break; | |
3200 | } | |
3201 | ||
3202 | if (mythread_alive (thread_id)) | |
3203 | write_ok (own_buf); | |
3204 | else | |
3205 | write_enn (own_buf); | |
3206 | } | |
3207 | break; | |
3208 | case 'R': | |
3209 | response_needed = 0; | |
3210 | ||
3211 | /* Restarting the inferior is only supported in the extended | |
3212 | protocol. */ | |
3213 | if (extended_protocol) | |
3214 | { | |
3215 | if (target_running ()) | |
95954743 PA |
3216 | for_each_inferior (&all_processes, |
3217 | kill_inferior_callback); | |
bd99dc85 PA |
3218 | fprintf (stderr, "GDBserver restarting\n"); |
3219 | ||
3220 | /* Wait till we are at 1st instruction in prog. */ | |
3221 | if (program_argv != NULL) | |
3222 | start_inferior (program_argv); | |
3223 | else | |
3224 | { | |
3225 | last_status.kind = TARGET_WAITKIND_EXITED; | |
3226 | last_status.value.sig = TARGET_SIGNAL_KILL; | |
3227 | } | |
8336d594 | 3228 | return 0; |
c906108c SS |
3229 | } |
3230 | else | |
3231 | { | |
bd99dc85 PA |
3232 | /* It is a request we don't understand. Respond with an |
3233 | empty packet so that gdb knows that we don't support this | |
3234 | request. */ | |
3235 | own_buf[0] = '\0'; | |
3236 | break; | |
3237 | } | |
3238 | case 'v': | |
3239 | /* Extended (long) request. */ | |
3240 | handle_v_requests (own_buf, packet_len, &new_packet_len); | |
3241 | break; | |
3242 | ||
3243 | default: | |
3244 | /* It is a request we don't understand. Respond with an empty | |
3245 | packet so that gdb knows that we don't support this | |
3246 | request. */ | |
3247 | own_buf[0] = '\0'; | |
3248 | break; | |
3249 | } | |
3250 | ||
3251 | if (new_packet_len != -1) | |
3252 | putpkt_binary (own_buf, new_packet_len); | |
3253 | else | |
3254 | putpkt (own_buf); | |
3255 | ||
3256 | response_needed = 0; | |
3257 | ||
3258 | if (!extended_protocol && have_ran && !target_running ()) | |
3259 | { | |
3260 | /* In non-stop, defer exiting until GDB had a chance to query | |
3261 | the whole vStopped list (until it gets an OK). */ | |
3262 | if (!notif_queue) | |
3263 | { | |
3264 | fprintf (stderr, "GDBserver exiting\n"); | |
c906108c | 3265 | remote_close (); |
bd99dc85 | 3266 | exit (0); |
c906108c SS |
3267 | } |
3268 | } | |
8336d594 PA |
3269 | |
3270 | if (exit_requested) | |
3271 | return -1; | |
3272 | ||
3273 | return 0; | |
c906108c | 3274 | } |
bd99dc85 PA |
3275 | |
3276 | /* Event-loop callback for serial events. */ | |
3277 | ||
8336d594 | 3278 | int |
bd99dc85 PA |
3279 | handle_serial_event (int err, gdb_client_data client_data) |
3280 | { | |
3281 | if (debug_threads) | |
3282 | fprintf (stderr, "handling possible serial event\n"); | |
3283 | ||
3284 | /* Really handle it. */ | |
8336d594 PA |
3285 | if (process_serial_event () < 0) |
3286 | return -1; | |
bd99dc85 PA |
3287 | |
3288 | /* Be sure to not change the selected inferior behind GDB's back. | |
3289 | Important in the non-stop mode asynchronous protocol. */ | |
3290 | set_desired_inferior (1); | |
8336d594 PA |
3291 | |
3292 | return 0; | |
bd99dc85 PA |
3293 | } |
3294 | ||
3295 | /* Event-loop callback for target events. */ | |
3296 | ||
8336d594 | 3297 | int |
bd99dc85 PA |
3298 | handle_target_event (int err, gdb_client_data client_data) |
3299 | { | |
3300 | if (debug_threads) | |
3301 | fprintf (stderr, "handling possible target event\n"); | |
3302 | ||
95954743 PA |
3303 | last_ptid = mywait (minus_one_ptid, &last_status, |
3304 | TARGET_WNOHANG, 1); | |
bd99dc85 PA |
3305 | |
3306 | if (last_status.kind != TARGET_WAITKIND_IGNORE) | |
3307 | { | |
8336d594 PA |
3308 | int pid = ptid_get_pid (last_ptid); |
3309 | struct process_info *process = find_process_pid (pid); | |
3310 | int forward_event = !gdb_connected () || process->gdb_detached; | |
3311 | ||
3312 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
3313 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
f9e39928 PA |
3314 | { |
3315 | mark_breakpoints_out (process); | |
3316 | mourn_inferior (process); | |
3317 | } | |
ce1a5b52 | 3318 | else |
d20a8ad9 PA |
3319 | { |
3320 | /* We're reporting this thread as stopped. Update its | |
3321 | "want-stopped" state to what the client wants, until it | |
3322 | gets a new resume action. */ | |
3323 | current_inferior->last_resume_kind = resume_stop; | |
3324 | current_inferior->last_status = last_status; | |
3325 | } | |
8336d594 PA |
3326 | |
3327 | if (forward_event) | |
3328 | { | |
3329 | if (!target_running ()) | |
3330 | { | |
3331 | /* The last process exited. We're done. */ | |
3332 | exit (0); | |
3333 | } | |
3334 | ||
3335 | if (last_status.kind == TARGET_WAITKIND_STOPPED) | |
3336 | { | |
3337 | /* A thread stopped with a signal, but gdb isn't | |
3338 | connected to handle it. Pass it down to the | |
3339 | inferior, as if it wasn't being traced. */ | |
3340 | struct thread_resume resume_info; | |
3341 | ||
3342 | if (debug_threads) | |
3343 | fprintf (stderr, | |
3344 | "GDB not connected; forwarding event %d for [%s]\n", | |
3345 | (int) last_status.kind, | |
3346 | target_pid_to_str (last_ptid)); | |
3347 | ||
3348 | resume_info.thread = last_ptid; | |
3349 | resume_info.kind = resume_continue; | |
30d50328 | 3350 | resume_info.sig = target_signal_to_host (last_status.value.sig); |
8336d594 PA |
3351 | (*the_target->resume) (&resume_info, 1); |
3352 | } | |
3353 | else if (debug_threads) | |
3354 | fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n", | |
3355 | (int) last_status.kind, | |
3356 | target_pid_to_str (last_ptid)); | |
3357 | } | |
3358 | else | |
3359 | { | |
3360 | /* Something interesting. Tell GDB about it. */ | |
3361 | push_event (last_ptid, &last_status); | |
3362 | } | |
bd99dc85 PA |
3363 | } |
3364 | ||
3365 | /* Be sure to not change the selected inferior behind GDB's back. | |
3366 | Important in the non-stop mode asynchronous protocol. */ | |
3367 | set_desired_inferior (1); | |
8336d594 PA |
3368 | |
3369 | return 0; | |
bd99dc85 | 3370 | } |