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