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