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