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