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