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