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