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