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