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