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