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