Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
28e7fd62 | 2 | Copyright (C) 1989-2013 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" |
d1feda86 | 21 | #include "agent.h" |
14a00470 | 22 | #include "notif.h" |
3aee8918 | 23 | #include "tdesc.h" |
c906108c | 24 | |
a9fa9f7d | 25 | #include <unistd.h> |
68070c10 | 26 | #if HAVE_SIGNAL_H |
a9fa9f7d | 27 | #include <signal.h> |
68070c10 | 28 | #endif |
8bdce1ff | 29 | #include "gdb_wait.h" |
9accd112 | 30 | #include "btrace-common.h" |
602e3198 | 31 | #include "filestuff.h" |
c144c7a0 | 32 | #include "tracepoint.h" |
799cdc37 | 33 | #include "dll.h" |
533b0600 | 34 | #include "hostio.h" |
a9fa9f7d | 35 | |
122f36ef PA |
36 | /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of |
37 | `vCont'. Note the multi-process extensions made `vCont' a | |
38 | requirement, so `Hc pPID.TID' is pretty much undefined. So | |
39 | CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for | |
40 | resuming all threads of the process (again, `Hc' isn't used for | |
41 | multi-process), or a specific thread ptid_t. | |
42 | ||
43 | We also set this when handling a single-thread `vCont' resume, as | |
44 | some places in the backends check it to know when (and for which | |
45 | thread) single-thread scheduler-locking is in effect. */ | |
95954743 | 46 | ptid_t cont_thread; |
122f36ef PA |
47 | |
48 | /* The thread set with an `Hg' packet. */ | |
95954743 | 49 | ptid_t general_thread; |
5b1c542e | 50 | |
0d62e5e8 DJ |
51 | int server_waiting; |
52 | ||
2d717e4f | 53 | static int extended_protocol; |
2d717e4f DJ |
54 | static int response_needed; |
55 | static int exit_requested; | |
56 | ||
03f2bd59 JK |
57 | /* --once: Exit after the first connection has closed. */ |
58 | int run_once; | |
59 | ||
95954743 | 60 | int multi_process; |
bd99dc85 PA |
61 | int non_stop; |
62 | ||
03583c20 UW |
63 | /* Whether we should attempt to disable the operating system's address |
64 | space randomization feature before starting an inferior. */ | |
65 | int disable_randomization = 1; | |
66 | ||
ccd213ac | 67 | static char **program_argv, **wrapper_argv; |
2d717e4f | 68 | |
c74d0ad8 DJ |
69 | /* Enable miscellaneous debugging output. The name is historical - it |
70 | was originally used to debug LinuxThreads support. */ | |
71 | int debug_threads; | |
72 | ||
aa5ca48f DE |
73 | /* Enable debugging of h/w breakpoint/watchpoint support. */ |
74 | int debug_hw_points; | |
75 | ||
a493e3e2 PA |
76 | int pass_signals[GDB_SIGNAL_LAST]; |
77 | int program_signals[GDB_SIGNAL_LAST]; | |
9b224c5e | 78 | int program_signals_p; |
89be2091 | 79 | |
c906108c | 80 | jmp_buf toplevel; |
c906108c | 81 | |
a9fa9f7d DJ |
82 | /* The PID of the originally created or attached inferior. Used to |
83 | send signals to the process when GDB sends us an asynchronous interrupt | |
84 | (user hitting Control-C in the client), and to wait for the child to exit | |
85 | when no longer debugging it. */ | |
86 | ||
a1928bad | 87 | unsigned long signal_pid; |
a9fa9f7d | 88 | |
290fadea RS |
89 | #ifdef SIGTTOU |
90 | /* A file descriptor for the controlling terminal. */ | |
91 | int terminal_fd; | |
92 | ||
93 | /* TERMINAL_FD's original foreground group. */ | |
94 | pid_t old_foreground_pgrp; | |
95 | ||
96 | /* Hand back terminal ownership to the original foreground group. */ | |
97 | ||
98 | static void | |
99 | restore_old_foreground_pgrp (void) | |
100 | { | |
101 | tcsetpgrp (terminal_fd, old_foreground_pgrp); | |
102 | } | |
103 | #endif | |
104 | ||
ec56be1b PA |
105 | /* Set if you want to disable optional thread related packets support |
106 | in gdbserver, for the sake of testing GDB against stubs that don't | |
107 | support them. */ | |
108 | int disable_packet_vCont; | |
109 | int disable_packet_Tthread; | |
110 | int disable_packet_qC; | |
111 | int disable_packet_qfThreadInfo; | |
112 | ||
5b1c542e PA |
113 | /* Last status reported to GDB. */ |
114 | static struct target_waitstatus last_status; | |
95954743 | 115 | static ptid_t last_ptid; |
5b1c542e | 116 | |
bd99dc85 PA |
117 | static char *own_buf; |
118 | static unsigned char *mem_buf; | |
119 | ||
14a00470 YQ |
120 | /* A sub-class of 'struct notif_event' for stop, holding information |
121 | relative to a single stop reply. We keep a queue of these to | |
122 | push to GDB in non-stop mode. */ | |
123 | ||
bd99dc85 PA |
124 | struct vstop_notif |
125 | { | |
14a00470 | 126 | struct notif_event base; |
bd99dc85 PA |
127 | |
128 | /* Thread or process that got the event. */ | |
95954743 | 129 | ptid_t ptid; |
bd99dc85 PA |
130 | |
131 | /* Event info. */ | |
132 | struct target_waitstatus status; | |
133 | }; | |
134 | ||
14a00470 | 135 | DEFINE_QUEUE_P (notif_event_p); |
bd99dc85 PA |
136 | |
137 | /* Put a stop reply to the stop reply queue. */ | |
138 | ||
139 | static void | |
95954743 | 140 | queue_stop_reply (ptid_t ptid, struct target_waitstatus *status) |
bd99dc85 | 141 | { |
14a00470 | 142 | struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif)); |
bd99dc85 | 143 | |
bd99dc85 PA |
144 | new_notif->ptid = ptid; |
145 | new_notif->status = *status; | |
146 | ||
14a00470 | 147 | notif_event_enque (¬if_stop, (struct notif_event *) new_notif); |
bd99dc85 PA |
148 | } |
149 | ||
14a00470 YQ |
150 | static int |
151 | remove_all_on_match_pid (QUEUE (notif_event_p) *q, | |
152 | QUEUE_ITER (notif_event_p) *iter, | |
153 | struct notif_event *event, | |
154 | void *data) | |
bd99dc85 | 155 | { |
14a00470 | 156 | int *pid = data; |
d20a8ad9 | 157 | |
14a00470 YQ |
158 | if (*pid == -1 |
159 | || ptid_get_pid (((struct vstop_notif *) event)->ptid) == *pid) | |
bd99dc85 | 160 | { |
14a00470 YQ |
161 | if (q->free_func != NULL) |
162 | q->free_func (event); | |
163 | ||
164 | QUEUE_remove_elem (notif_event_p, q, iter); | |
bd99dc85 | 165 | } |
14a00470 YQ |
166 | |
167 | return 1; | |
bd99dc85 PA |
168 | } |
169 | ||
95954743 PA |
170 | /* Get rid of the currently pending stop replies for PID. If PID is |
171 | -1, then apply to all processes. */ | |
bd99dc85 PA |
172 | |
173 | static void | |
95954743 | 174 | discard_queued_stop_replies (int pid) |
bd99dc85 | 175 | { |
14a00470 YQ |
176 | QUEUE_iterate (notif_event_p, notif_stop.queue, |
177 | remove_all_on_match_pid, &pid); | |
bd99dc85 PA |
178 | } |
179 | ||
bd99dc85 | 180 | static void |
14a00470 | 181 | vstop_notif_reply (struct notif_event *event, char *own_buf) |
bd99dc85 | 182 | { |
14a00470 YQ |
183 | struct vstop_notif *vstop = (struct vstop_notif *) event; |
184 | ||
185 | prepare_resume_reply (own_buf, vstop->ptid, &vstop->status); | |
bd99dc85 PA |
186 | } |
187 | ||
14a00470 YQ |
188 | struct notif_server notif_stop = |
189 | { | |
190 | "vStopped", "Stop", NULL, vstop_notif_reply, | |
191 | }; | |
192 | ||
2d717e4f DJ |
193 | static int |
194 | target_running (void) | |
195 | { | |
196 | return all_threads.head != NULL; | |
197 | } | |
198 | ||
fc620387 | 199 | static int |
5b1c542e | 200 | start_inferior (char **argv) |
c906108c | 201 | { |
ccd213ac | 202 | char **new_argv = argv; |
2d717e4f | 203 | |
ccd213ac DJ |
204 | if (wrapper_argv != NULL) |
205 | { | |
206 | int i, count = 1; | |
207 | ||
208 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
209 | count++; | |
210 | for (i = 0; argv[i] != NULL; i++) | |
211 | count++; | |
212 | new_argv = alloca (sizeof (char *) * count); | |
213 | count = 0; | |
214 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
215 | new_argv[count++] = wrapper_argv[i]; | |
216 | for (i = 0; argv[i] != NULL; i++) | |
217 | new_argv[count++] = argv[i]; | |
218 | new_argv[count] = NULL; | |
219 | } | |
220 | ||
65730243 DE |
221 | if (debug_threads) |
222 | { | |
223 | int i; | |
224 | for (i = 0; new_argv[i]; ++i) | |
225 | fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]); | |
226 | fflush (stderr); | |
227 | } | |
228 | ||
b80864fb | 229 | #ifdef SIGTTOU |
a9fa9f7d DJ |
230 | signal (SIGTTOU, SIG_DFL); |
231 | signal (SIGTTIN, SIG_DFL); | |
b80864fb | 232 | #endif |
a9fa9f7d | 233 | |
122f36ef PA |
234 | /* Clear this so the backend doesn't get confused, thinking |
235 | CONT_THREAD died, and it needs to resume all threads. */ | |
236 | cont_thread = null_ptid; | |
237 | ||
ccd213ac | 238 | signal_pid = create_inferior (new_argv[0], new_argv); |
0d62e5e8 | 239 | |
c588c53c MS |
240 | /* FIXME: we don't actually know at this point that the create |
241 | actually succeeded. We won't know that until we wait. */ | |
a1928bad | 242 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
a9fa9f7d | 243 | signal_pid); |
b80864fb | 244 | fflush (stderr); |
a9fa9f7d | 245 | |
b80864fb | 246 | #ifdef SIGTTOU |
a9fa9f7d DJ |
247 | signal (SIGTTOU, SIG_IGN); |
248 | signal (SIGTTIN, SIG_IGN); | |
290fadea RS |
249 | terminal_fd = fileno (stderr); |
250 | old_foreground_pgrp = tcgetpgrp (terminal_fd); | |
251 | tcsetpgrp (terminal_fd, signal_pid); | |
252 | atexit (restore_old_foreground_pgrp); | |
b80864fb | 253 | #endif |
c906108c | 254 | |
ccd213ac DJ |
255 | if (wrapper_argv != NULL) |
256 | { | |
257 | struct thread_resume resume_info; | |
ccd213ac | 258 | |
95954743 | 259 | resume_info.thread = pid_to_ptid (signal_pid); |
bd99dc85 | 260 | resume_info.kind = resume_continue; |
ccd213ac | 261 | resume_info.sig = 0; |
ccd213ac | 262 | |
06db92f0 | 263 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
bd99dc85 | 264 | |
5b1c542e PA |
265 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
266 | return signal_pid; | |
ccd213ac DJ |
267 | |
268 | do | |
269 | { | |
2bd7c093 | 270 | (*the_target->resume) (&resume_info, 1); |
ccd213ac | 271 | |
06db92f0 | 272 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
5b1c542e PA |
273 | if (last_status.kind != TARGET_WAITKIND_STOPPED) |
274 | return signal_pid; | |
d20a8ad9 PA |
275 | |
276 | current_inferior->last_resume_kind = resume_stop; | |
277 | current_inferior->last_status = last_status; | |
ccd213ac | 278 | } |
a493e3e2 | 279 | while (last_status.value.sig != GDB_SIGNAL_TRAP); |
ccd213ac | 280 | |
5b1c542e | 281 | return signal_pid; |
ccd213ac DJ |
282 | } |
283 | ||
5b1c542e PA |
284 | /* Wait till we are at 1st instruction in program, return new pid |
285 | (assuming success). */ | |
95954743 | 286 | last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0); |
5b1c542e | 287 | |
d20a8ad9 PA |
288 | if (last_status.kind != TARGET_WAITKIND_EXITED |
289 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
290 | { | |
291 | current_inferior->last_resume_kind = resume_stop; | |
292 | current_inferior->last_status = last_status; | |
293 | } | |
294 | ||
5b1c542e | 295 | return signal_pid; |
c906108c SS |
296 | } |
297 | ||
45b7b345 | 298 | static int |
5b1c542e | 299 | attach_inferior (int pid) |
45b7b345 DJ |
300 | { |
301 | /* myattach should return -1 if attaching is unsupported, | |
302 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 303 | |
45b7b345 DJ |
304 | if (myattach (pid) != 0) |
305 | return -1; | |
306 | ||
6910d122 | 307 | fprintf (stderr, "Attached; pid = %d\n", pid); |
b80864fb | 308 | fflush (stderr); |
6910d122 | 309 | |
a9fa9f7d DJ |
310 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
311 | attach function, so that it can be the main thread instead of | |
312 | whichever we were told to attach to. */ | |
313 | signal_pid = pid; | |
314 | ||
7d5d4e98 PA |
315 | /* Clear this so the backend doesn't get confused, thinking |
316 | CONT_THREAD died, and it needs to resume all threads. */ | |
317 | cont_thread = null_ptid; | |
318 | ||
bd99dc85 PA |
319 | if (!non_stop) |
320 | { | |
95954743 | 321 | last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0); |
bd99dc85 PA |
322 | |
323 | /* GDB knows to ignore the first SIGSTOP after attaching to a running | |
324 | process using the "attach" command, but this is different; it's | |
325 | just using "target remote". Pretend it's just starting up. */ | |
326 | if (last_status.kind == TARGET_WAITKIND_STOPPED | |
a493e3e2 PA |
327 | && last_status.value.sig == GDB_SIGNAL_STOP) |
328 | last_status.value.sig = GDB_SIGNAL_TRAP; | |
d20a8ad9 PA |
329 | |
330 | current_inferior->last_resume_kind = resume_stop; | |
331 | current_inferior->last_status = last_status; | |
bd99dc85 | 332 | } |
9db87ebd | 333 | |
45b7b345 DJ |
334 | return 0; |
335 | } | |
336 | ||
c906108c | 337 | extern int remote_debug; |
ce3a066d | 338 | |
0876f84a DJ |
339 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
340 | or -1 otherwise. */ | |
341 | ||
342 | static int | |
d08aafef | 343 | decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len) |
0876f84a | 344 | { |
d08aafef PA |
345 | /* After the read marker and annex, qXfer looks like a |
346 | traditional 'm' packet. */ | |
347 | decode_m_packet (buf, ofs, len); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | static int | |
353 | decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset) | |
354 | { | |
355 | /* Extract and NUL-terminate the object. */ | |
356 | *object = buf; | |
357 | while (*buf && *buf != ':') | |
358 | buf++; | |
359 | if (*buf == '\0') | |
360 | return -1; | |
361 | *buf++ = 0; | |
362 | ||
363 | /* Extract and NUL-terminate the read/write action. */ | |
364 | *rw = buf; | |
365 | while (*buf && *buf != ':') | |
366 | buf++; | |
367 | if (*buf == '\0') | |
368 | return -1; | |
369 | *buf++ = 0; | |
370 | ||
0876f84a DJ |
371 | /* Extract and NUL-terminate the annex. */ |
372 | *annex = buf; | |
373 | while (*buf && *buf != ':') | |
374 | buf++; | |
375 | if (*buf == '\0') | |
376 | return -1; | |
377 | *buf++ = 0; | |
378 | ||
d08aafef | 379 | *offset = buf; |
0876f84a DJ |
380 | return 0; |
381 | } | |
382 | ||
383 | /* Write the response to a successful qXfer read. Returns the | |
384 | length of the (binary) data stored in BUF, corresponding | |
385 | to as much of DATA/LEN as we could fit. IS_MORE controls | |
386 | the first character of the response. */ | |
387 | static int | |
23181151 | 388 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
0876f84a DJ |
389 | { |
390 | int out_len; | |
391 | ||
392 | if (is_more) | |
393 | buf[0] = 'm'; | |
394 | else | |
395 | buf[0] = 'l'; | |
396 | ||
397 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, | |
398 | PBUFSIZ - 2) + 1; | |
399 | } | |
400 | ||
9accd112 MM |
401 | /* Handle btrace enabling. */ |
402 | ||
403 | static const char * | |
404 | handle_btrace_enable (struct thread_info *thread) | |
405 | { | |
406 | if (thread->btrace != NULL) | |
407 | return "E.Btrace already enabled."; | |
408 | ||
409 | thread->btrace = target_enable_btrace (thread->entry.id); | |
410 | if (thread->btrace == NULL) | |
411 | return "E.Could not enable btrace."; | |
412 | ||
413 | return NULL; | |
414 | } | |
415 | ||
416 | /* Handle btrace disabling. */ | |
417 | ||
418 | static const char * | |
419 | handle_btrace_disable (struct thread_info *thread) | |
420 | { | |
421 | ||
422 | if (thread->btrace == NULL) | |
423 | return "E.Branch tracing not enabled."; | |
424 | ||
425 | if (target_disable_btrace (thread->btrace) != 0) | |
426 | return "E.Could not disable branch tracing."; | |
427 | ||
428 | thread->btrace = NULL; | |
429 | return NULL; | |
430 | } | |
431 | ||
432 | /* Handle the "Qbtrace" packet. */ | |
433 | ||
434 | static int | |
435 | handle_btrace_general_set (char *own_buf) | |
436 | { | |
437 | struct thread_info *thread; | |
438 | const char *err; | |
439 | char *op; | |
440 | ||
441 | if (strncmp ("Qbtrace:", own_buf, strlen ("Qbtrace:")) != 0) | |
442 | return 0; | |
443 | ||
444 | op = own_buf + strlen ("Qbtrace:"); | |
445 | ||
446 | if (!target_supports_btrace ()) | |
447 | { | |
448 | strcpy (own_buf, "E.Target does not support branch tracing."); | |
449 | return -1; | |
450 | } | |
451 | ||
452 | if (ptid_equal (general_thread, null_ptid) | |
453 | || ptid_equal (general_thread, minus_one_ptid)) | |
454 | { | |
455 | strcpy (own_buf, "E.Must select a single thread."); | |
456 | return -1; | |
457 | } | |
458 | ||
459 | thread = find_thread_ptid (general_thread); | |
460 | if (thread == NULL) | |
461 | { | |
462 | strcpy (own_buf, "E.No such thread."); | |
463 | return -1; | |
464 | } | |
465 | ||
466 | err = NULL; | |
467 | ||
468 | if (strcmp (op, "bts") == 0) | |
469 | err = handle_btrace_enable (thread); | |
470 | else if (strcmp (op, "off") == 0) | |
471 | err = handle_btrace_disable (thread); | |
472 | else | |
473 | err = "E.Bad Qbtrace operation. Use bts or off."; | |
474 | ||
475 | if (err != 0) | |
476 | strcpy (own_buf, err); | |
477 | else | |
478 | write_ok (own_buf); | |
479 | ||
480 | return 1; | |
481 | } | |
482 | ||
89be2091 | 483 | /* Handle all of the extended 'Q' packets. */ |
ae1ada35 DE |
484 | |
485 | static void | |
89be2091 DJ |
486 | handle_general_set (char *own_buf) |
487 | { | |
488 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) | |
489 | { | |
a493e3e2 | 490 | int numsigs = (int) GDB_SIGNAL_LAST, i; |
89be2091 DJ |
491 | const char *p = own_buf + strlen ("QPassSignals:"); |
492 | CORE_ADDR cursig; | |
493 | ||
494 | p = decode_address_to_semicolon (&cursig, p); | |
495 | for (i = 0; i < numsigs; i++) | |
496 | { | |
497 | if (i == cursig) | |
498 | { | |
499 | pass_signals[i] = 1; | |
500 | if (*p == '\0') | |
501 | /* Keep looping, to clear the remaining signals. */ | |
502 | cursig = -1; | |
503 | else | |
504 | p = decode_address_to_semicolon (&cursig, p); | |
505 | } | |
506 | else | |
507 | pass_signals[i] = 0; | |
508 | } | |
509 | strcpy (own_buf, "OK"); | |
510 | return; | |
511 | } | |
512 | ||
9b224c5e PA |
513 | if (strncmp ("QProgramSignals:", own_buf, strlen ("QProgramSignals:")) == 0) |
514 | { | |
a493e3e2 | 515 | int numsigs = (int) GDB_SIGNAL_LAST, i; |
9b224c5e PA |
516 | const char *p = own_buf + strlen ("QProgramSignals:"); |
517 | CORE_ADDR cursig; | |
518 | ||
519 | program_signals_p = 1; | |
520 | ||
521 | p = decode_address_to_semicolon (&cursig, p); | |
522 | for (i = 0; i < numsigs; i++) | |
523 | { | |
524 | if (i == cursig) | |
525 | { | |
526 | program_signals[i] = 1; | |
527 | if (*p == '\0') | |
528 | /* Keep looping, to clear the remaining signals. */ | |
529 | cursig = -1; | |
530 | else | |
531 | p = decode_address_to_semicolon (&cursig, p); | |
532 | } | |
533 | else | |
534 | program_signals[i] = 0; | |
535 | } | |
536 | strcpy (own_buf, "OK"); | |
537 | return; | |
538 | } | |
539 | ||
a6f3e723 SL |
540 | if (strcmp (own_buf, "QStartNoAckMode") == 0) |
541 | { | |
542 | if (remote_debug) | |
543 | { | |
544 | fprintf (stderr, "[noack mode enabled]\n"); | |
545 | fflush (stderr); | |
546 | } | |
547 | ||
548 | noack_mode = 1; | |
549 | write_ok (own_buf); | |
550 | return; | |
551 | } | |
552 | ||
bd99dc85 PA |
553 | if (strncmp (own_buf, "QNonStop:", 9) == 0) |
554 | { | |
555 | char *mode = own_buf + 9; | |
556 | int req = -1; | |
557 | char *req_str; | |
558 | ||
559 | if (strcmp (mode, "0") == 0) | |
560 | req = 0; | |
561 | else if (strcmp (mode, "1") == 0) | |
562 | req = 1; | |
563 | else | |
564 | { | |
565 | /* We don't know what this mode is, so complain to | |
566 | GDB. */ | |
567 | fprintf (stderr, "Unknown non-stop mode requested: %s\n", | |
568 | own_buf); | |
569 | write_enn (own_buf); | |
570 | return; | |
571 | } | |
572 | ||
573 | req_str = req ? "non-stop" : "all-stop"; | |
574 | if (start_non_stop (req) != 0) | |
575 | { | |
576 | fprintf (stderr, "Setting %s mode failed\n", req_str); | |
577 | write_enn (own_buf); | |
578 | return; | |
579 | } | |
580 | ||
581 | non_stop = req; | |
582 | ||
583 | if (remote_debug) | |
584 | fprintf (stderr, "[%s mode enabled]\n", req_str); | |
585 | ||
586 | write_ok (own_buf); | |
587 | return; | |
588 | } | |
589 | ||
03583c20 UW |
590 | if (strncmp ("QDisableRandomization:", own_buf, |
591 | strlen ("QDisableRandomization:")) == 0) | |
592 | { | |
593 | char *packet = own_buf + strlen ("QDisableRandomization:"); | |
594 | ULONGEST setting; | |
595 | ||
596 | unpack_varlen_hex (packet, &setting); | |
597 | disable_randomization = setting; | |
598 | ||
599 | if (remote_debug) | |
600 | { | |
601 | if (disable_randomization) | |
602 | fprintf (stderr, "[address space randomization disabled]\n"); | |
603 | else | |
604 | fprintf (stderr, "[address space randomization enabled]\n"); | |
605 | } | |
606 | ||
607 | write_ok (own_buf); | |
608 | return; | |
609 | } | |
610 | ||
219f2f23 PA |
611 | if (target_supports_tracepoints () |
612 | && handle_tracepoint_general_set (own_buf)) | |
613 | return; | |
614 | ||
d1feda86 YQ |
615 | if (strncmp ("QAgent:", own_buf, strlen ("QAgent:")) == 0) |
616 | { | |
617 | char *mode = own_buf + strlen ("QAgent:"); | |
618 | int req = 0; | |
619 | ||
620 | if (strcmp (mode, "0") == 0) | |
621 | req = 0; | |
622 | else if (strcmp (mode, "1") == 0) | |
623 | req = 1; | |
624 | else | |
625 | { | |
626 | /* We don't know what this value is, so complain to GDB. */ | |
627 | sprintf (own_buf, "E.Unknown QAgent value"); | |
628 | return; | |
629 | } | |
630 | ||
631 | /* Update the flag. */ | |
632 | use_agent = req; | |
633 | if (remote_debug) | |
634 | fprintf (stderr, "[%s agent]\n", req ? "Enable" : "Disable"); | |
635 | write_ok (own_buf); | |
636 | return; | |
637 | } | |
638 | ||
9accd112 MM |
639 | if (handle_btrace_general_set (own_buf)) |
640 | return; | |
641 | ||
89be2091 DJ |
642 | /* Otherwise we didn't know what packet it was. Say we didn't |
643 | understand it. */ | |
644 | own_buf[0] = 0; | |
645 | } | |
646 | ||
23181151 | 647 | static const char * |
fb1e4ffc | 648 | get_features_xml (const char *annex) |
23181151 | 649 | { |
3aee8918 PA |
650 | const struct target_desc *desc = current_target_desc (); |
651 | ||
652 | /* `desc->xmltarget' defines what to return when looking for the | |
653 | "target.xml" file. Its contents can either be verbatim XML code | |
654 | (prefixed with a '@') or else the name of the actual XML file to | |
655 | be used in place of "target.xml". | |
fb1e4ffc | 656 | |
9b4b61c8 UW |
657 | This variable is set up from the auto-generated |
658 | init_registers_... routine for the current target. */ | |
fb1e4ffc | 659 | |
3aee8918 | 660 | if (desc->xmltarget != NULL && strcmp (annex, "target.xml") == 0) |
23181151 | 661 | { |
3aee8918 PA |
662 | if (*desc->xmltarget == '@') |
663 | return desc->xmltarget + 1; | |
23181151 | 664 | else |
3aee8918 | 665 | annex = desc->xmltarget; |
23181151 DJ |
666 | } |
667 | ||
9b4b61c8 UW |
668 | #ifdef USE_XML |
669 | { | |
670 | extern const char *const xml_builtin[][2]; | |
671 | int i; | |
672 | ||
673 | /* Look for the annex. */ | |
674 | for (i = 0; xml_builtin[i][0] != NULL; i++) | |
675 | if (strcmp (annex, xml_builtin[i][0]) == 0) | |
676 | break; | |
677 | ||
678 | if (xml_builtin[i][0] != NULL) | |
679 | return xml_builtin[i][1]; | |
680 | } | |
681 | #endif | |
682 | ||
683 | return NULL; | |
23181151 DJ |
684 | } |
685 | ||
c74d0ad8 DJ |
686 | void |
687 | monitor_show_help (void) | |
688 | { | |
689 | monitor_output ("The following monitor commands are supported:\n"); | |
690 | monitor_output (" set debug <0|1>\n"); | |
1b3f6016 | 691 | monitor_output (" Enable general debugging messages\n"); |
aa5ca48f DE |
692 | monitor_output (" set debug-hw-points <0|1>\n"); |
693 | monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n"); | |
c74d0ad8 DJ |
694 | monitor_output (" set remote-debug <0|1>\n"); |
695 | monitor_output (" Enable remote protocol debugging messages\n"); | |
ecd7ecbc DJ |
696 | monitor_output (" exit\n"); |
697 | monitor_output (" Quit GDBserver\n"); | |
c74d0ad8 DJ |
698 | } |
699 | ||
764880b7 PA |
700 | /* Read trace frame or inferior memory. Returns the number of bytes |
701 | actually read, zero when no further transfer is possible, and -1 on | |
702 | error. Return of a positive value smaller than LEN does not | |
703 | indicate there's no more to be read, only the end of the transfer. | |
704 | E.g., when GDB reads memory from a traceframe, a first request may | |
705 | be served from a memory block that does not cover the whole request | |
706 | length. A following request gets the rest served from either | |
707 | another block (of the same traceframe) or from the read-only | |
708 | regions. */ | |
219f2f23 PA |
709 | |
710 | static int | |
90d74c30 | 711 | gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
219f2f23 | 712 | { |
764880b7 | 713 | int res; |
90d74c30 | 714 | |
219f2f23 PA |
715 | if (current_traceframe >= 0) |
716 | { | |
717 | ULONGEST nbytes; | |
718 | ULONGEST length = len; | |
719 | ||
720 | if (traceframe_read_mem (current_traceframe, | |
721 | memaddr, myaddr, len, &nbytes)) | |
9a13b2fa | 722 | return -1; |
219f2f23 | 723 | /* Data read from trace buffer, we're done. */ |
764880b7 PA |
724 | if (nbytes > 0) |
725 | return nbytes; | |
219f2f23 | 726 | if (!in_readonly_region (memaddr, length)) |
764880b7 | 727 | return -1; |
219f2f23 PA |
728 | /* Otherwise we have a valid readonly case, fall through. */ |
729 | /* (assume no half-trace half-real blocks for now) */ | |
730 | } | |
731 | ||
764880b7 PA |
732 | res = prepare_to_access_memory (); |
733 | if (res == 0) | |
90d74c30 | 734 | { |
764880b7 | 735 | res = read_inferior_memory (memaddr, myaddr, len); |
0146f85b | 736 | done_accessing_memory (); |
90d74c30 | 737 | |
764880b7 PA |
738 | return res == 0 ? len : -1; |
739 | } | |
740 | else | |
741 | return -1; | |
219f2f23 PA |
742 | } |
743 | ||
744 | /* Write trace frame or inferior memory. Actually, writing to trace | |
745 | frames is forbidden. */ | |
746 | ||
747 | static int | |
90d74c30 | 748 | gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len) |
219f2f23 PA |
749 | { |
750 | if (current_traceframe >= 0) | |
751 | return EIO; | |
752 | else | |
90d74c30 PA |
753 | { |
754 | int ret; | |
755 | ||
756 | ret = prepare_to_access_memory (); | |
757 | if (ret == 0) | |
758 | { | |
759 | ret = write_inferior_memory (memaddr, myaddr, len); | |
0146f85b | 760 | done_accessing_memory (); |
90d74c30 PA |
761 | } |
762 | return ret; | |
763 | } | |
219f2f23 PA |
764 | } |
765 | ||
08388c79 DE |
766 | /* Subroutine of handle_search_memory to simplify it. */ |
767 | ||
768 | static int | |
769 | handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len, | |
770 | gdb_byte *pattern, unsigned pattern_len, | |
771 | gdb_byte *search_buf, | |
772 | unsigned chunk_size, unsigned search_buf_size, | |
773 | CORE_ADDR *found_addrp) | |
774 | { | |
775 | /* Prime the search buffer. */ | |
776 | ||
764880b7 PA |
777 | if (gdb_read_memory (start_addr, search_buf, search_buf_size) |
778 | != search_buf_size) | |
08388c79 | 779 | { |
b3dc46ff AB |
780 | warning ("Unable to access %ld bytes of target " |
781 | "memory at 0x%lx, halting search.", | |
782 | (long) search_buf_size, (long) start_addr); | |
08388c79 DE |
783 | return -1; |
784 | } | |
785 | ||
786 | /* Perform the search. | |
787 | ||
788 | The loop is kept simple by allocating [N + pattern-length - 1] bytes. | |
789 | When we've scanned N bytes we copy the trailing bytes to the start and | |
790 | read in another N bytes. */ | |
791 | ||
792 | while (search_space_len >= pattern_len) | |
793 | { | |
794 | gdb_byte *found_ptr; | |
795 | unsigned nr_search_bytes = (search_space_len < search_buf_size | |
796 | ? search_space_len | |
797 | : search_buf_size); | |
798 | ||
799 | found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len); | |
800 | ||
801 | if (found_ptr != NULL) | |
802 | { | |
803 | CORE_ADDR found_addr = start_addr + (found_ptr - search_buf); | |
804 | *found_addrp = found_addr; | |
805 | return 1; | |
806 | } | |
807 | ||
808 | /* Not found in this chunk, skip to next chunk. */ | |
809 | ||
810 | /* Don't let search_space_len wrap here, it's unsigned. */ | |
811 | if (search_space_len >= chunk_size) | |
812 | search_space_len -= chunk_size; | |
813 | else | |
814 | search_space_len = 0; | |
815 | ||
816 | if (search_space_len >= pattern_len) | |
817 | { | |
818 | unsigned keep_len = search_buf_size - chunk_size; | |
8a35fb51 | 819 | CORE_ADDR read_addr = start_addr + chunk_size + keep_len; |
08388c79 DE |
820 | int nr_to_read; |
821 | ||
822 | /* Copy the trailing part of the previous iteration to the front | |
823 | of the buffer for the next iteration. */ | |
824 | memcpy (search_buf, search_buf + chunk_size, keep_len); | |
825 | ||
826 | nr_to_read = (search_space_len - keep_len < chunk_size | |
827 | ? search_space_len - keep_len | |
828 | : chunk_size); | |
829 | ||
90d74c30 | 830 | if (gdb_read_memory (read_addr, search_buf + keep_len, |
764880b7 | 831 | nr_to_read) != search_buf_size) |
08388c79 | 832 | { |
b3dc46ff | 833 | warning ("Unable to access %ld bytes of target memory " |
493e2a69 | 834 | "at 0x%lx, halting search.", |
b3dc46ff | 835 | (long) nr_to_read, (long) read_addr); |
08388c79 DE |
836 | return -1; |
837 | } | |
838 | ||
839 | start_addr += chunk_size; | |
840 | } | |
841 | } | |
842 | ||
843 | /* Not found. */ | |
844 | ||
845 | return 0; | |
846 | } | |
847 | ||
848 | /* Handle qSearch:memory packets. */ | |
849 | ||
850 | static void | |
851 | handle_search_memory (char *own_buf, int packet_len) | |
852 | { | |
853 | CORE_ADDR start_addr; | |
854 | CORE_ADDR search_space_len; | |
855 | gdb_byte *pattern; | |
856 | unsigned int pattern_len; | |
857 | /* NOTE: also defined in find.c testcase. */ | |
858 | #define SEARCH_CHUNK_SIZE 16000 | |
859 | const unsigned chunk_size = SEARCH_CHUNK_SIZE; | |
860 | /* Buffer to hold memory contents for searching. */ | |
861 | gdb_byte *search_buf; | |
862 | unsigned search_buf_size; | |
863 | int found; | |
864 | CORE_ADDR found_addr; | |
865 | int cmd_name_len = sizeof ("qSearch:memory:") - 1; | |
866 | ||
aef93bd7 | 867 | pattern = malloc (packet_len); |
08388c79 DE |
868 | if (pattern == NULL) |
869 | { | |
5e1471f5 | 870 | error ("Unable to allocate memory to perform the search"); |
08388c79 DE |
871 | strcpy (own_buf, "E00"); |
872 | return; | |
873 | } | |
874 | if (decode_search_memory_packet (own_buf + cmd_name_len, | |
875 | packet_len - cmd_name_len, | |
876 | &start_addr, &search_space_len, | |
877 | pattern, &pattern_len) < 0) | |
878 | { | |
879 | free (pattern); | |
5e1471f5 | 880 | error ("Error in parsing qSearch:memory packet"); |
08388c79 DE |
881 | strcpy (own_buf, "E00"); |
882 | return; | |
883 | } | |
884 | ||
885 | search_buf_size = chunk_size + pattern_len - 1; | |
886 | ||
887 | /* No point in trying to allocate a buffer larger than the search space. */ | |
888 | if (search_space_len < search_buf_size) | |
889 | search_buf_size = search_space_len; | |
890 | ||
aef93bd7 | 891 | search_buf = malloc (search_buf_size); |
08388c79 DE |
892 | if (search_buf == NULL) |
893 | { | |
894 | free (pattern); | |
5e1471f5 | 895 | error ("Unable to allocate memory to perform the search"); |
08388c79 DE |
896 | strcpy (own_buf, "E00"); |
897 | return; | |
898 | } | |
899 | ||
900 | found = handle_search_memory_1 (start_addr, search_space_len, | |
901 | pattern, pattern_len, | |
902 | search_buf, chunk_size, search_buf_size, | |
903 | &found_addr); | |
904 | ||
905 | if (found > 0) | |
906 | sprintf (own_buf, "1,%lx", (long) found_addr); | |
907 | else if (found == 0) | |
908 | strcpy (own_buf, "0"); | |
909 | else | |
910 | strcpy (own_buf, "E00"); | |
911 | ||
912 | free (search_buf); | |
913 | free (pattern); | |
914 | } | |
915 | ||
2d717e4f DJ |
916 | #define require_running(BUF) \ |
917 | if (!target_running ()) \ | |
918 | { \ | |
919 | write_enn (BUF); \ | |
920 | return; \ | |
921 | } | |
922 | ||
cdbfd419 PP |
923 | /* Handle monitor commands not handled by target-specific handlers. */ |
924 | ||
925 | static void | |
d73f2619 | 926 | handle_monitor_command (char *mon, char *own_buf) |
cdbfd419 PP |
927 | { |
928 | if (strcmp (mon, "set debug 1") == 0) | |
929 | { | |
930 | debug_threads = 1; | |
931 | monitor_output ("Debug output enabled.\n"); | |
932 | } | |
933 | else if (strcmp (mon, "set debug 0") == 0) | |
934 | { | |
935 | debug_threads = 0; | |
936 | monitor_output ("Debug output disabled.\n"); | |
937 | } | |
938 | else if (strcmp (mon, "set debug-hw-points 1") == 0) | |
939 | { | |
940 | debug_hw_points = 1; | |
941 | monitor_output ("H/W point debugging output enabled.\n"); | |
942 | } | |
943 | else if (strcmp (mon, "set debug-hw-points 0") == 0) | |
944 | { | |
945 | debug_hw_points = 0; | |
946 | monitor_output ("H/W point debugging output disabled.\n"); | |
947 | } | |
948 | else if (strcmp (mon, "set remote-debug 1") == 0) | |
949 | { | |
950 | remote_debug = 1; | |
951 | monitor_output ("Protocol debug output enabled.\n"); | |
952 | } | |
953 | else if (strcmp (mon, "set remote-debug 0") == 0) | |
954 | { | |
955 | remote_debug = 0; | |
956 | monitor_output ("Protocol debug output disabled.\n"); | |
957 | } | |
958 | else if (strcmp (mon, "help") == 0) | |
959 | monitor_show_help (); | |
960 | else if (strcmp (mon, "exit") == 0) | |
961 | exit_requested = 1; | |
962 | else | |
963 | { | |
964 | monitor_output ("Unknown monitor command.\n\n"); | |
965 | monitor_show_help (); | |
966 | write_enn (own_buf); | |
967 | } | |
968 | } | |
969 | ||
d08aafef PA |
970 | /* Associates a callback with each supported qXfer'able object. */ |
971 | ||
972 | struct qxfer | |
973 | { | |
974 | /* The object this handler handles. */ | |
975 | const char *object; | |
976 | ||
977 | /* Request that the target transfer up to LEN 8-bit bytes of the | |
978 | target's OBJECT. The OFFSET, for a seekable object, specifies | |
979 | the starting point. The ANNEX can be used to provide additional | |
980 | data-specific information to the target. | |
981 | ||
982 | Return the number of bytes actually transfered, zero when no | |
5cc22e4c MM |
983 | further transfer is possible, -1 on error, -2 when the transfer |
984 | is not supported, and -3 on a verbose error message that should | |
985 | be preserved. Return of a positive value smaller than LEN does | |
986 | not indicate the end of the object, only the end of the transfer. | |
d08aafef PA |
987 | |
988 | One, and only one, of readbuf or writebuf must be non-NULL. */ | |
989 | int (*xfer) (const char *annex, | |
990 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
991 | ULONGEST offset, LONGEST len); | |
992 | }; | |
993 | ||
994 | /* Handle qXfer:auxv:read. */ | |
995 | ||
996 | static int | |
997 | handle_qxfer_auxv (const char *annex, | |
998 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
999 | ULONGEST offset, LONGEST len) | |
1000 | { | |
1001 | if (the_target->read_auxv == NULL || writebuf != NULL) | |
1002 | return -2; | |
1003 | ||
1004 | if (annex[0] != '\0' || !target_running ()) | |
1005 | return -1; | |
1006 | ||
1007 | return (*the_target->read_auxv) (offset, readbuf, len); | |
1008 | } | |
1009 | ||
1010 | /* Handle qXfer:features:read. */ | |
1011 | ||
1012 | static int | |
1013 | handle_qxfer_features (const char *annex, | |
1014 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1015 | ULONGEST offset, LONGEST len) | |
1016 | { | |
1017 | const char *document; | |
1018 | size_t total_len; | |
1019 | ||
1020 | if (writebuf != NULL) | |
1021 | return -2; | |
1022 | ||
1023 | if (!target_running ()) | |
1024 | return -1; | |
1025 | ||
1026 | /* Grab the correct annex. */ | |
1027 | document = get_features_xml (annex); | |
1028 | if (document == NULL) | |
1029 | return -1; | |
1030 | ||
1031 | total_len = strlen (document); | |
1032 | ||
1033 | if (offset > total_len) | |
1034 | return -1; | |
1035 | ||
1036 | if (offset + len > total_len) | |
1037 | len = total_len - offset; | |
1038 | ||
1039 | memcpy (readbuf, document + offset, len); | |
1040 | return len; | |
1041 | } | |
1042 | ||
1043 | /* Handle qXfer:libraries:read. */ | |
1044 | ||
1045 | static int | |
1046 | handle_qxfer_libraries (const char *annex, | |
1047 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1048 | ULONGEST offset, LONGEST len) | |
1049 | { | |
1050 | unsigned int total_len; | |
1051 | char *document, *p; | |
1052 | struct inferior_list_entry *dll_ptr; | |
1053 | ||
1054 | if (writebuf != NULL) | |
1055 | return -2; | |
1056 | ||
1057 | if (annex[0] != '\0' || !target_running ()) | |
1058 | return -1; | |
1059 | ||
1060 | /* Over-estimate the necessary memory. Assume that every character | |
1061 | in the library name must be escaped. */ | |
1062 | total_len = 64; | |
1063 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
1064 | total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); | |
1065 | ||
1066 | document = malloc (total_len); | |
1067 | if (document == NULL) | |
1068 | return -1; | |
1069 | ||
1070 | strcpy (document, "<library-list>\n"); | |
1071 | p = document + strlen (document); | |
1072 | ||
1073 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
1074 | { | |
1075 | struct dll_info *dll = (struct dll_info *) dll_ptr; | |
1076 | char *name; | |
1077 | ||
1078 | strcpy (p, " <library name=\""); | |
1079 | p = p + strlen (p); | |
1080 | name = xml_escape_text (dll->name); | |
1081 | strcpy (p, name); | |
1082 | free (name); | |
1083 | p = p + strlen (p); | |
1084 | strcpy (p, "\"><segment address=\""); | |
1085 | p = p + strlen (p); | |
1086 | sprintf (p, "0x%lx", (long) dll->base_addr); | |
1087 | p = p + strlen (p); | |
1088 | strcpy (p, "\"/></library>\n"); | |
1089 | p = p + strlen (p); | |
1090 | } | |
1091 | ||
1092 | strcpy (p, "</library-list>\n"); | |
1093 | ||
1094 | total_len = strlen (document); | |
1095 | ||
1096 | if (offset > total_len) | |
1097 | { | |
1098 | free (document); | |
1099 | return -1; | |
1100 | } | |
1101 | ||
1102 | if (offset + len > total_len) | |
1103 | len = total_len - offset; | |
1104 | ||
1105 | memcpy (readbuf, document + offset, len); | |
1106 | free (document); | |
1107 | return len; | |
1108 | } | |
1109 | ||
2268b414 JK |
1110 | /* Handle qXfer:libraries-svr4:read. */ |
1111 | ||
1112 | static int | |
1113 | handle_qxfer_libraries_svr4 (const char *annex, | |
1114 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1115 | ULONGEST offset, LONGEST len) | |
1116 | { | |
1117 | if (writebuf != NULL) | |
1118 | return -2; | |
1119 | ||
b1fbec62 | 1120 | if (!target_running () || the_target->qxfer_libraries_svr4 == NULL) |
2268b414 JK |
1121 | return -1; |
1122 | ||
1123 | return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len); | |
1124 | } | |
1125 | ||
d08aafef PA |
1126 | /* Handle qXfer:osadata:read. */ |
1127 | ||
1128 | static int | |
1129 | handle_qxfer_osdata (const char *annex, | |
1130 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1131 | ULONGEST offset, LONGEST len) | |
1132 | { | |
1133 | if (the_target->qxfer_osdata == NULL || writebuf != NULL) | |
1134 | return -2; | |
1135 | ||
1136 | return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len); | |
1137 | } | |
1138 | ||
1139 | /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */ | |
1140 | ||
1141 | static int | |
1142 | handle_qxfer_siginfo (const char *annex, | |
1143 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1144 | ULONGEST offset, LONGEST len) | |
1145 | { | |
1146 | if (the_target->qxfer_siginfo == NULL) | |
1147 | return -2; | |
1148 | ||
1149 | if (annex[0] != '\0' || !target_running ()) | |
1150 | return -1; | |
1151 | ||
1152 | return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len); | |
1153 | } | |
1154 | ||
1155 | /* Handle qXfer:spu:read and qXfer:spu:write. */ | |
1156 | ||
1157 | static int | |
1158 | handle_qxfer_spu (const char *annex, | |
1159 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1160 | ULONGEST offset, LONGEST len) | |
1161 | { | |
1162 | if (the_target->qxfer_spu == NULL) | |
1163 | return -2; | |
1164 | ||
1165 | if (!target_running ()) | |
1166 | return -1; | |
1167 | ||
1168 | return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len); | |
1169 | } | |
1170 | ||
1171 | /* Handle qXfer:statictrace:read. */ | |
1172 | ||
1173 | static int | |
1174 | handle_qxfer_statictrace (const char *annex, | |
1175 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1176 | ULONGEST offset, LONGEST len) | |
1177 | { | |
1178 | ULONGEST nbytes; | |
1179 | ||
1180 | if (writebuf != NULL) | |
1181 | return -2; | |
1182 | ||
1183 | if (annex[0] != '\0' || !target_running () || current_traceframe == -1) | |
1184 | return -1; | |
1185 | ||
1186 | if (traceframe_read_sdata (current_traceframe, offset, | |
1187 | readbuf, len, &nbytes)) | |
1188 | return -1; | |
1189 | return nbytes; | |
1190 | } | |
1191 | ||
1192 | /* Helper for handle_qxfer_threads. */ | |
1193 | ||
dc146f7c | 1194 | static void |
d08aafef | 1195 | handle_qxfer_threads_proper (struct buffer *buffer) |
dc146f7c VP |
1196 | { |
1197 | struct inferior_list_entry *thread; | |
1198 | ||
1199 | buffer_grow_str (buffer, "<threads>\n"); | |
1200 | ||
1201 | for (thread = all_threads.head; thread; thread = thread->next) | |
1202 | { | |
1203 | ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread); | |
1204 | char ptid_s[100]; | |
3e10640f | 1205 | int core = target_core_of_thread (ptid); |
dc146f7c VP |
1206 | char core_s[21]; |
1207 | ||
1208 | write_ptid (ptid_s, ptid); | |
1209 | ||
dc146f7c VP |
1210 | if (core != -1) |
1211 | { | |
1212 | sprintf (core_s, "%d", core); | |
1213 | buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n", | |
1214 | ptid_s, core_s); | |
1215 | } | |
1216 | else | |
1217 | { | |
1218 | buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n", | |
1219 | ptid_s); | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | buffer_grow_str0 (buffer, "</threads>\n"); | |
1224 | } | |
1225 | ||
d08aafef PA |
1226 | /* Handle qXfer:threads:read. */ |
1227 | ||
dc146f7c | 1228 | static int |
d08aafef PA |
1229 | handle_qxfer_threads (const char *annex, |
1230 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1231 | ULONGEST offset, LONGEST len) | |
dc146f7c VP |
1232 | { |
1233 | static char *result = 0; | |
1234 | static unsigned int result_length = 0; | |
1235 | ||
d08aafef PA |
1236 | if (writebuf != NULL) |
1237 | return -2; | |
1238 | ||
1239 | if (!target_running () || annex[0] != '\0') | |
1240 | return -1; | |
dc146f7c VP |
1241 | |
1242 | if (offset == 0) | |
1243 | { | |
1244 | struct buffer buffer; | |
1245 | /* When asked for data at offset 0, generate everything and store into | |
1246 | 'result'. Successive reads will be served off 'result'. */ | |
1247 | if (result) | |
1248 | free (result); | |
1249 | ||
1250 | buffer_init (&buffer); | |
1251 | ||
d08aafef | 1252 | handle_qxfer_threads_proper (&buffer); |
dc146f7c VP |
1253 | |
1254 | result = buffer_finish (&buffer); | |
1255 | result_length = strlen (result); | |
1256 | buffer_free (&buffer); | |
1257 | } | |
1258 | ||
1259 | if (offset >= result_length) | |
1260 | { | |
1261 | /* We're out of data. */ | |
1262 | free (result); | |
1263 | result = NULL; | |
1264 | result_length = 0; | |
1265 | return 0; | |
1266 | } | |
1267 | ||
d08aafef PA |
1268 | if (len > result_length - offset) |
1269 | len = result_length - offset; | |
1270 | ||
1271 | memcpy (readbuf, result + offset, len); | |
1272 | ||
1273 | return len; | |
1274 | } | |
1275 | ||
b3b9301e PA |
1276 | /* Handle qXfer:traceframe-info:read. */ |
1277 | ||
1278 | static int | |
1279 | handle_qxfer_traceframe_info (const char *annex, | |
1280 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1281 | ULONGEST offset, LONGEST len) | |
1282 | { | |
1283 | static char *result = 0; | |
1284 | static unsigned int result_length = 0; | |
1285 | ||
1286 | if (writebuf != NULL) | |
1287 | return -2; | |
1288 | ||
1289 | if (!target_running () || annex[0] != '\0' || current_traceframe == -1) | |
1290 | return -1; | |
1291 | ||
1292 | if (offset == 0) | |
1293 | { | |
1294 | struct buffer buffer; | |
1295 | ||
1296 | /* When asked for data at offset 0, generate everything and | |
1297 | store into 'result'. Successive reads will be served off | |
1298 | 'result'. */ | |
1299 | free (result); | |
1300 | ||
1301 | buffer_init (&buffer); | |
1302 | ||
1303 | traceframe_read_info (current_traceframe, &buffer); | |
1304 | ||
1305 | result = buffer_finish (&buffer); | |
1306 | result_length = strlen (result); | |
1307 | buffer_free (&buffer); | |
1308 | } | |
1309 | ||
1310 | if (offset >= result_length) | |
1311 | { | |
1312 | /* We're out of data. */ | |
1313 | free (result); | |
1314 | result = NULL; | |
1315 | result_length = 0; | |
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | if (len > result_length - offset) | |
1320 | len = result_length - offset; | |
1321 | ||
1322 | memcpy (readbuf, result + offset, len); | |
1323 | return len; | |
1324 | } | |
1325 | ||
78d85199 YQ |
1326 | /* Handle qXfer:fdpic:read. */ |
1327 | ||
1328 | static int | |
1329 | handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf, | |
1330 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
1331 | { | |
1332 | if (the_target->read_loadmap == NULL) | |
1333 | return -2; | |
1334 | ||
1335 | if (!target_running ()) | |
1336 | return -1; | |
1337 | ||
1338 | return (*the_target->read_loadmap) (annex, offset, readbuf, len); | |
1339 | } | |
1340 | ||
9accd112 MM |
1341 | /* Handle qXfer:btrace:read. */ |
1342 | ||
1343 | static int | |
1344 | handle_qxfer_btrace (const char *annex, | |
1345 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
1346 | ULONGEST offset, LONGEST len) | |
1347 | { | |
1348 | static struct buffer cache; | |
1349 | struct thread_info *thread; | |
1350 | int type; | |
1351 | ||
1352 | if (the_target->read_btrace == NULL || writebuf != NULL) | |
1353 | return -2; | |
1354 | ||
1355 | if (!target_running ()) | |
1356 | return -1; | |
1357 | ||
1358 | if (ptid_equal (general_thread, null_ptid) | |
1359 | || ptid_equal (general_thread, minus_one_ptid)) | |
1360 | { | |
1361 | strcpy (own_buf, "E.Must select a single thread."); | |
1362 | return -3; | |
1363 | } | |
1364 | ||
1365 | thread = find_thread_ptid (general_thread); | |
1366 | if (thread == NULL) | |
1367 | { | |
1368 | strcpy (own_buf, "E.No such thread."); | |
1369 | return -3; | |
1370 | } | |
1371 | ||
1372 | if (thread->btrace == NULL) | |
1373 | { | |
1374 | strcpy (own_buf, "E.Btrace not enabled."); | |
1375 | return -3; | |
1376 | } | |
1377 | ||
1378 | if (strcmp (annex, "all") == 0) | |
1379 | type = btrace_read_all; | |
1380 | else if (strcmp (annex, "new") == 0) | |
1381 | type = btrace_read_new; | |
1382 | else | |
1383 | { | |
1384 | strcpy (own_buf, "E.Bad annex."); | |
1385 | return -3; | |
1386 | } | |
1387 | ||
1388 | if (offset == 0) | |
1389 | { | |
1390 | buffer_free (&cache); | |
1391 | ||
1392 | target_read_btrace (thread->btrace, &cache, type); | |
1393 | } | |
1394 | else if (offset > cache.used_size) | |
1395 | { | |
1396 | buffer_free (&cache); | |
1397 | return -3; | |
1398 | } | |
1399 | ||
1400 | if (len > cache.used_size - offset) | |
1401 | len = cache.used_size - offset; | |
1402 | ||
1403 | memcpy (readbuf, cache.buffer + offset, len); | |
1404 | ||
1405 | return len; | |
1406 | } | |
1407 | ||
d08aafef PA |
1408 | static const struct qxfer qxfer_packets[] = |
1409 | { | |
1410 | { "auxv", handle_qxfer_auxv }, | |
9accd112 | 1411 | { "btrace", handle_qxfer_btrace }, |
78d85199 | 1412 | { "fdpic", handle_qxfer_fdpic}, |
d08aafef PA |
1413 | { "features", handle_qxfer_features }, |
1414 | { "libraries", handle_qxfer_libraries }, | |
2268b414 | 1415 | { "libraries-svr4", handle_qxfer_libraries_svr4 }, |
d08aafef PA |
1416 | { "osdata", handle_qxfer_osdata }, |
1417 | { "siginfo", handle_qxfer_siginfo }, | |
1418 | { "spu", handle_qxfer_spu }, | |
1419 | { "statictrace", handle_qxfer_statictrace }, | |
1420 | { "threads", handle_qxfer_threads }, | |
b3b9301e | 1421 | { "traceframe-info", handle_qxfer_traceframe_info }, |
d08aafef PA |
1422 | }; |
1423 | ||
1424 | static int | |
1425 | handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p) | |
1426 | { | |
1427 | int i; | |
1428 | char *object; | |
1429 | char *rw; | |
1430 | char *annex; | |
1431 | char *offset; | |
1432 | ||
1433 | if (strncmp (own_buf, "qXfer:", 6) != 0) | |
1434 | return 0; | |
1435 | ||
1436 | /* Grab the object, r/w and annex. */ | |
1437 | if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0) | |
1438 | { | |
1439 | write_enn (own_buf); | |
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | for (i = 0; | |
1444 | i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]); | |
1445 | i++) | |
1446 | { | |
1447 | const struct qxfer *q = &qxfer_packets[i]; | |
1448 | ||
1449 | if (strcmp (object, q->object) == 0) | |
1450 | { | |
1451 | if (strcmp (rw, "read") == 0) | |
1452 | { | |
1453 | unsigned char *data; | |
1454 | int n; | |
1455 | CORE_ADDR ofs; | |
1456 | unsigned int len; | |
1457 | ||
1458 | /* Grab the offset and length. */ | |
1459 | if (decode_xfer_read (offset, &ofs, &len) < 0) | |
1460 | { | |
1461 | write_enn (own_buf); | |
1462 | return 1; | |
1463 | } | |
1464 | ||
1465 | /* Read one extra byte, as an indicator of whether there is | |
1466 | more. */ | |
1467 | if (len > PBUFSIZ - 2) | |
1468 | len = PBUFSIZ - 2; | |
1469 | data = malloc (len + 1); | |
1470 | if (data == NULL) | |
1471 | { | |
1472 | write_enn (own_buf); | |
1473 | return 1; | |
1474 | } | |
1475 | n = (*q->xfer) (annex, data, NULL, ofs, len + 1); | |
1476 | if (n == -2) | |
1477 | { | |
1478 | free (data); | |
1479 | return 0; | |
1480 | } | |
5cc22e4c MM |
1481 | else if (n == -3) |
1482 | { | |
1483 | /* Preserve error message. */ | |
1484 | } | |
d08aafef PA |
1485 | else if (n < 0) |
1486 | write_enn (own_buf); | |
1487 | else if (n > len) | |
1488 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); | |
1489 | else | |
1490 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); | |
1491 | ||
1492 | free (data); | |
1493 | return 1; | |
1494 | } | |
1495 | else if (strcmp (rw, "write") == 0) | |
1496 | { | |
1497 | int n; | |
1498 | unsigned int len; | |
1499 | CORE_ADDR ofs; | |
1500 | unsigned char *data; | |
1501 | ||
1502 | strcpy (own_buf, "E00"); | |
1503 | data = malloc (packet_len - (offset - own_buf)); | |
1504 | if (data == NULL) | |
1505 | { | |
1506 | write_enn (own_buf); | |
1507 | return 1; | |
1508 | } | |
1509 | if (decode_xfer_write (offset, packet_len - (offset - own_buf), | |
1510 | &ofs, &len, data) < 0) | |
1511 | { | |
1512 | free (data); | |
1513 | write_enn (own_buf); | |
1514 | return 1; | |
1515 | } | |
1516 | ||
1517 | n = (*q->xfer) (annex, NULL, data, ofs, len); | |
1518 | if (n == -2) | |
1519 | { | |
1520 | free (data); | |
1521 | return 0; | |
1522 | } | |
5cc22e4c MM |
1523 | else if (n == -3) |
1524 | { | |
1525 | /* Preserve error message. */ | |
1526 | } | |
d08aafef PA |
1527 | else if (n < 0) |
1528 | write_enn (own_buf); | |
1529 | else | |
1530 | sprintf (own_buf, "%x", n); | |
dc146f7c | 1531 | |
d08aafef PA |
1532 | free (data); |
1533 | return 1; | |
1534 | } | |
dc146f7c | 1535 | |
d08aafef PA |
1536 | return 0; |
1537 | } | |
1538 | } | |
dc146f7c | 1539 | |
d08aafef | 1540 | return 0; |
dc146f7c VP |
1541 | } |
1542 | ||
30ba68cb MS |
1543 | /* Table used by the crc32 function to calcuate the checksum. */ |
1544 | ||
1545 | static unsigned int crc32_table[256] = | |
1546 | {0, 0}; | |
1547 | ||
1548 | /* Compute 32 bit CRC from inferior memory. | |
1549 | ||
1550 | On success, return 32 bit CRC. | |
1551 | On failure, return (unsigned long long) -1. */ | |
1552 | ||
1553 | static unsigned long long | |
1554 | crc32 (CORE_ADDR base, int len, unsigned int crc) | |
1555 | { | |
1556 | if (!crc32_table[1]) | |
1557 | { | |
1558 | /* Initialize the CRC table and the decoding table. */ | |
1559 | int i, j; | |
1560 | unsigned int c; | |
1561 | ||
1562 | for (i = 0; i < 256; i++) | |
1563 | { | |
1564 | for (c = i << 24, j = 8; j > 0; --j) | |
1565 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
1566 | crc32_table[i] = c; | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | while (len--) | |
1571 | { | |
1572 | unsigned char byte = 0; | |
1573 | ||
1574 | /* Return failure if memory read fails. */ | |
1575 | if (read_inferior_memory (base, &byte, 1) != 0) | |
1576 | return (unsigned long long) -1; | |
1577 | ||
1578 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255]; | |
1579 | base++; | |
1580 | } | |
1581 | return (unsigned long long) crc; | |
1582 | } | |
1583 | ||
ce3a066d | 1584 | /* Handle all of the extended 'q' packets. */ |
d08aafef | 1585 | |
ce3a066d | 1586 | void |
0e7f50da | 1587 | handle_query (char *own_buf, int packet_len, int *new_packet_len_p) |
ce3a066d | 1588 | { |
0d62e5e8 DJ |
1589 | static struct inferior_list_entry *thread_ptr; |
1590 | ||
bb63802a | 1591 | /* Reply the current thread id. */ |
db42f210 | 1592 | if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC) |
bb63802a | 1593 | { |
95954743 | 1594 | ptid_t gdb_id; |
2d717e4f | 1595 | require_running (own_buf); |
bd99dc85 | 1596 | |
95954743 PA |
1597 | if (!ptid_equal (general_thread, null_ptid) |
1598 | && !ptid_equal (general_thread, minus_one_ptid)) | |
bd99dc85 PA |
1599 | gdb_id = general_thread; |
1600 | else | |
1601 | { | |
1602 | thread_ptr = all_threads.head; | |
1603 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1604 | } | |
1605 | ||
95954743 PA |
1606 | sprintf (own_buf, "QC"); |
1607 | own_buf += 2; | |
4b812f4e | 1608 | write_ptid (own_buf, gdb_id); |
bb63802a UW |
1609 | return; |
1610 | } | |
1611 | ||
ce3a066d DJ |
1612 | if (strcmp ("qSymbol::", own_buf) == 0) |
1613 | { | |
d3bbe7a0 PA |
1614 | /* GDB is suggesting new symbols have been loaded. This may |
1615 | mean a new shared library has been detected as loaded, so | |
1616 | take the opportunity to check if breakpoints we think are | |
1617 | inserted, still are. Note that it isn't guaranteed that | |
1618 | we'll see this when a shared library is loaded, and nor will | |
1619 | we see this for unloads (although breakpoints in unloaded | |
1620 | libraries shouldn't trigger), as GDB may not find symbols for | |
1621 | the library at all. We also re-validate breakpoints when we | |
1622 | see a second GDB breakpoint for the same address, and or when | |
1623 | we access breakpoint shadows. */ | |
1624 | validate_breakpoints (); | |
1625 | ||
fa593d66 PA |
1626 | if (target_supports_tracepoints ()) |
1627 | tracepoint_look_up_symbols (); | |
1628 | ||
2d717e4f | 1629 | if (target_running () && the_target->look_up_symbols != NULL) |
2f2893d9 DJ |
1630 | (*the_target->look_up_symbols) (); |
1631 | ||
ce3a066d DJ |
1632 | strcpy (own_buf, "OK"); |
1633 | return; | |
1634 | } | |
1635 | ||
db42f210 | 1636 | if (!disable_packet_qfThreadInfo) |
0d62e5e8 | 1637 | { |
db42f210 | 1638 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
0d62e5e8 | 1639 | { |
95954743 PA |
1640 | ptid_t gdb_id; |
1641 | ||
db42f210 PA |
1642 | require_running (own_buf); |
1643 | thread_ptr = all_threads.head; | |
95954743 PA |
1644 | |
1645 | *own_buf++ = 'm'; | |
1646 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1647 | write_ptid (own_buf, gdb_id); | |
0d62e5e8 DJ |
1648 | thread_ptr = thread_ptr->next; |
1649 | return; | |
1650 | } | |
db42f210 PA |
1651 | |
1652 | if (strcmp ("qsThreadInfo", own_buf) == 0) | |
0d62e5e8 | 1653 | { |
95954743 PA |
1654 | ptid_t gdb_id; |
1655 | ||
db42f210 PA |
1656 | require_running (own_buf); |
1657 | if (thread_ptr != NULL) | |
1658 | { | |
95954743 PA |
1659 | *own_buf++ = 'm'; |
1660 | gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr); | |
1661 | write_ptid (own_buf, gdb_id); | |
db42f210 PA |
1662 | thread_ptr = thread_ptr->next; |
1663 | return; | |
1664 | } | |
1665 | else | |
1666 | { | |
1667 | sprintf (own_buf, "l"); | |
1668 | return; | |
1669 | } | |
0d62e5e8 DJ |
1670 | } |
1671 | } | |
aa691b87 | 1672 | |
52fb6437 NS |
1673 | if (the_target->read_offsets != NULL |
1674 | && strcmp ("qOffsets", own_buf) == 0) | |
1675 | { | |
1676 | CORE_ADDR text, data; | |
2d717e4f DJ |
1677 | |
1678 | require_running (own_buf); | |
52fb6437 NS |
1679 | if (the_target->read_offsets (&text, &data)) |
1680 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
1681 | (long)text, (long)data, (long)data); | |
1682 | else | |
1683 | write_enn (own_buf); | |
1b3f6016 | 1684 | |
52fb6437 NS |
1685 | return; |
1686 | } | |
1687 | ||
be2a5f71 DJ |
1688 | /* Protocol features query. */ |
1689 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
1690 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
1691 | { | |
95954743 | 1692 | char *p = &own_buf[10]; |
fa593d66 | 1693 | int gdb_supports_qRelocInsn = 0; |
95954743 | 1694 | |
1570b33e L |
1695 | /* Start processing qSupported packet. */ |
1696 | target_process_qsupported (NULL); | |
1697 | ||
95954743 PA |
1698 | /* Process each feature being provided by GDB. The first |
1699 | feature will follow a ':', and latter features will follow | |
1700 | ';'. */ | |
1701 | if (*p == ':') | |
d149dd1d PA |
1702 | { |
1703 | char **qsupported = NULL; | |
1704 | int count = 0; | |
1705 | int i; | |
1706 | ||
1707 | /* Two passes, to avoid nested strtok calls in | |
1708 | target_process_qsupported. */ | |
1709 | for (p = strtok (p + 1, ";"); | |
1710 | p != NULL; | |
1711 | p = strtok (NULL, ";")) | |
1712 | { | |
1713 | count++; | |
1714 | qsupported = xrealloc (qsupported, count * sizeof (char *)); | |
1715 | qsupported[count - 1] = xstrdup (p); | |
1716 | } | |
1717 | ||
1718 | for (i = 0; i < count; i++) | |
1719 | { | |
1720 | p = qsupported[i]; | |
1721 | if (strcmp (p, "multiprocess+") == 0) | |
1722 | { | |
1723 | /* GDB supports and wants multi-process support if | |
1724 | possible. */ | |
1725 | if (target_supports_multi_process ()) | |
1726 | multi_process = 1; | |
1727 | } | |
fa593d66 PA |
1728 | else if (strcmp (p, "qRelocInsn+") == 0) |
1729 | { | |
1730 | /* GDB supports relocate instruction requests. */ | |
1731 | gdb_supports_qRelocInsn = 1; | |
1732 | } | |
d149dd1d PA |
1733 | else |
1734 | target_process_qsupported (p); | |
1735 | ||
1736 | free (p); | |
1737 | } | |
1738 | ||
1739 | free (qsupported); | |
1740 | } | |
95954743 | 1741 | |
9b224c5e PA |
1742 | sprintf (own_buf, |
1743 | "PacketSize=%x;QPassSignals+;QProgramSignals+", | |
1744 | PBUFSIZ - 1); | |
0876f84a | 1745 | |
2268b414 | 1746 | if (the_target->qxfer_libraries_svr4 != NULL) |
b1fbec62 GB |
1747 | strcat (own_buf, ";qXfer:libraries-svr4:read+" |
1748 | ";augmented-libraries-svr4-read+"); | |
2268b414 JK |
1749 | else |
1750 | { | |
1751 | /* We do not have any hook to indicate whether the non-SVR4 target | |
1752 | backend supports qXfer:libraries:read, so always report it. */ | |
1753 | strcat (own_buf, ";qXfer:libraries:read+"); | |
1754 | } | |
255e7678 | 1755 | |
0876f84a | 1756 | if (the_target->read_auxv != NULL) |
9f2e1e63 | 1757 | strcat (own_buf, ";qXfer:auxv:read+"); |
2d717e4f | 1758 | |
0e7f50da UW |
1759 | if (the_target->qxfer_spu != NULL) |
1760 | strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+"); | |
0876f84a | 1761 | |
4aa995e1 PA |
1762 | if (the_target->qxfer_siginfo != NULL) |
1763 | strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+"); | |
1764 | ||
78d85199 YQ |
1765 | if (the_target->read_loadmap != NULL) |
1766 | strcat (own_buf, ";qXfer:fdpic:read+"); | |
1767 | ||
221c031f UW |
1768 | /* We always report qXfer:features:read, as targets may |
1769 | install XML files on a subsequent call to arch_setup. | |
1770 | If we reported to GDB on startup that we don't support | |
1771 | qXfer:feature:read at all, we will never be re-queried. */ | |
1772 | strcat (own_buf, ";qXfer:features:read+"); | |
23181151 | 1773 | |
a6f3e723 SL |
1774 | if (transport_is_reliable) |
1775 | strcat (own_buf, ";QStartNoAckMode+"); | |
07e059b5 VP |
1776 | |
1777 | if (the_target->qxfer_osdata != NULL) | |
1b3f6016 | 1778 | strcat (own_buf, ";qXfer:osdata:read+"); |
07e059b5 | 1779 | |
cf8fd78b PA |
1780 | if (target_supports_multi_process ()) |
1781 | strcat (own_buf, ";multiprocess+"); | |
95954743 | 1782 | |
bd99dc85 PA |
1783 | if (target_supports_non_stop ()) |
1784 | strcat (own_buf, ";QNonStop+"); | |
1785 | ||
03583c20 UW |
1786 | if (target_supports_disable_randomization ()) |
1787 | strcat (own_buf, ";QDisableRandomization+"); | |
1788 | ||
dc146f7c VP |
1789 | strcat (own_buf, ";qXfer:threads:read+"); |
1790 | ||
219f2f23 PA |
1791 | if (target_supports_tracepoints ()) |
1792 | { | |
1793 | strcat (own_buf, ";ConditionalTracepoints+"); | |
1794 | strcat (own_buf, ";TraceStateVariables+"); | |
1795 | strcat (own_buf, ";TracepointSource+"); | |
8336d594 | 1796 | strcat (own_buf, ";DisconnectedTracing+"); |
fa593d66 PA |
1797 | if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ()) |
1798 | strcat (own_buf, ";FastTracepoints+"); | |
0fb4aa4b | 1799 | strcat (own_buf, ";StaticTracepoints+"); |
1e4d1764 | 1800 | strcat (own_buf, ";InstallInTrace+"); |
0fb4aa4b | 1801 | strcat (own_buf, ";qXfer:statictrace:read+"); |
b3b9301e | 1802 | strcat (own_buf, ";qXfer:traceframe-info:read+"); |
d248b706 | 1803 | strcat (own_buf, ";EnableDisableTracepoints+"); |
f6f899bf | 1804 | strcat (own_buf, ";QTBuffer:size+"); |
3065dfb6 | 1805 | strcat (own_buf, ";tracenz+"); |
219f2f23 PA |
1806 | } |
1807 | ||
d3ce09f5 | 1808 | /* Support target-side breakpoint conditions and commands. */ |
9f3a5c85 | 1809 | strcat (own_buf, ";ConditionalBreakpoints+"); |
d3ce09f5 | 1810 | strcat (own_buf, ";BreakpointCommands+"); |
9f3a5c85 | 1811 | |
d1feda86 YQ |
1812 | if (target_supports_agent ()) |
1813 | strcat (own_buf, ";QAgent+"); | |
1814 | ||
9accd112 MM |
1815 | if (target_supports_btrace ()) |
1816 | { | |
1817 | strcat (own_buf, ";Qbtrace:bts+"); | |
1818 | strcat (own_buf, ";Qbtrace:off+"); | |
1819 | strcat (own_buf, ";qXfer:btrace:read+"); | |
1820 | } | |
1821 | ||
be2a5f71 DJ |
1822 | return; |
1823 | } | |
1824 | ||
dae5f5cf DJ |
1825 | /* Thread-local storage support. */ |
1826 | if (the_target->get_tls_address != NULL | |
1827 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) | |
1828 | { | |
1829 | char *p = own_buf + 12; | |
5b1c542e | 1830 | CORE_ADDR parts[2], address = 0; |
dae5f5cf | 1831 | int i, err; |
95954743 | 1832 | ptid_t ptid = null_ptid; |
dae5f5cf | 1833 | |
2d717e4f DJ |
1834 | require_running (own_buf); |
1835 | ||
dae5f5cf DJ |
1836 | for (i = 0; i < 3; i++) |
1837 | { | |
1838 | char *p2; | |
1839 | int len; | |
1840 | ||
1841 | if (p == NULL) | |
1842 | break; | |
1843 | ||
1844 | p2 = strchr (p, ','); | |
1845 | if (p2) | |
1846 | { | |
1847 | len = p2 - p; | |
1848 | p2++; | |
1849 | } | |
1850 | else | |
1851 | { | |
1852 | len = strlen (p); | |
1853 | p2 = NULL; | |
1854 | } | |
1855 | ||
5b1c542e | 1856 | if (i == 0) |
95954743 | 1857 | ptid = read_ptid (p, NULL); |
5b1c542e PA |
1858 | else |
1859 | decode_address (&parts[i - 1], p, len); | |
dae5f5cf DJ |
1860 | p = p2; |
1861 | } | |
1862 | ||
1863 | if (p != NULL || i < 3) | |
1864 | err = 1; | |
1865 | else | |
1866 | { | |
e09875d4 | 1867 | struct thread_info *thread = find_thread_ptid (ptid); |
dae5f5cf DJ |
1868 | |
1869 | if (thread == NULL) | |
1870 | err = 2; | |
1871 | else | |
5b1c542e | 1872 | err = the_target->get_tls_address (thread, parts[0], parts[1], |
dae5f5cf DJ |
1873 | &address); |
1874 | } | |
1875 | ||
1876 | if (err == 0) | |
1877 | { | |
c6f46ca0 | 1878 | strcpy (own_buf, paddress(address)); |
dae5f5cf DJ |
1879 | return; |
1880 | } | |
1881 | else if (err > 0) | |
1882 | { | |
1883 | write_enn (own_buf); | |
1884 | return; | |
1885 | } | |
1886 | ||
1887 | /* Otherwise, pretend we do not understand this packet. */ | |
1888 | } | |
1889 | ||
711e434b PM |
1890 | /* Windows OS Thread Information Block address support. */ |
1891 | if (the_target->get_tib_address != NULL | |
1892 | && strncmp ("qGetTIBAddr:", own_buf, 12) == 0) | |
1893 | { | |
1894 | char *annex; | |
1895 | int n; | |
1896 | CORE_ADDR tlb; | |
1897 | ptid_t ptid = read_ptid (own_buf + 12, &annex); | |
1898 | ||
1899 | n = (*the_target->get_tib_address) (ptid, &tlb); | |
1900 | if (n == 1) | |
1901 | { | |
c6f46ca0 | 1902 | strcpy (own_buf, paddress(tlb)); |
711e434b PM |
1903 | return; |
1904 | } | |
1905 | else if (n == 0) | |
1906 | { | |
1907 | write_enn (own_buf); | |
1908 | return; | |
1909 | } | |
1910 | return; | |
1911 | } | |
1912 | ||
c74d0ad8 DJ |
1913 | /* Handle "monitor" commands. */ |
1914 | if (strncmp ("qRcmd,", own_buf, 6) == 0) | |
1915 | { | |
aef93bd7 | 1916 | char *mon = malloc (PBUFSIZ); |
c74d0ad8 DJ |
1917 | int len = strlen (own_buf + 6); |
1918 | ||
aef93bd7 DE |
1919 | if (mon == NULL) |
1920 | { | |
1921 | write_enn (own_buf); | |
1922 | return; | |
1923 | } | |
1924 | ||
d41b6bb4 | 1925 | if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) |
c74d0ad8 DJ |
1926 | { |
1927 | write_enn (own_buf); | |
1928 | free (mon); | |
1929 | return; | |
1930 | } | |
1931 | mon[len / 2] = '\0'; | |
1932 | ||
1933 | write_ok (own_buf); | |
1934 | ||
cdbfd419 PP |
1935 | if (the_target->handle_monitor_command == NULL |
1936 | || (*the_target->handle_monitor_command) (mon) == 0) | |
1937 | /* Default processing. */ | |
d73f2619 | 1938 | handle_monitor_command (mon, own_buf); |
c74d0ad8 DJ |
1939 | |
1940 | free (mon); | |
1941 | return; | |
1942 | } | |
1943 | ||
493e2a69 MS |
1944 | if (strncmp ("qSearch:memory:", own_buf, |
1945 | sizeof ("qSearch:memory:") - 1) == 0) | |
08388c79 DE |
1946 | { |
1947 | require_running (own_buf); | |
1948 | handle_search_memory (own_buf, packet_len); | |
1949 | return; | |
1950 | } | |
1951 | ||
95954743 PA |
1952 | if (strcmp (own_buf, "qAttached") == 0 |
1953 | || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0) | |
0b16c5cf | 1954 | { |
95954743 PA |
1955 | struct process_info *process; |
1956 | ||
1957 | if (own_buf[sizeof ("qAttached") - 1]) | |
1958 | { | |
1959 | int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16); | |
1960 | process = (struct process_info *) | |
1961 | find_inferior_id (&all_processes, pid_to_ptid (pid)); | |
1962 | } | |
1963 | else | |
1964 | { | |
1965 | require_running (own_buf); | |
1966 | process = current_process (); | |
1967 | } | |
1968 | ||
1969 | if (process == NULL) | |
1970 | { | |
1971 | write_enn (own_buf); | |
1972 | return; | |
1973 | } | |
1974 | ||
1975 | strcpy (own_buf, process->attached ? "1" : "0"); | |
0b16c5cf PA |
1976 | return; |
1977 | } | |
1978 | ||
30ba68cb MS |
1979 | if (strncmp ("qCRC:", own_buf, 5) == 0) |
1980 | { | |
1981 | /* CRC check (compare-section). */ | |
1982 | char *comma; | |
aca22551 | 1983 | ULONGEST base; |
30ba68cb MS |
1984 | int len; |
1985 | unsigned long long crc; | |
1986 | ||
1987 | require_running (own_buf); | |
aca22551 | 1988 | comma = unpack_varlen_hex (own_buf + 5, &base); |
30ba68cb MS |
1989 | if (*comma++ != ',') |
1990 | { | |
1991 | write_enn (own_buf); | |
1992 | return; | |
1993 | } | |
1994 | len = strtoul (comma, NULL, 16); | |
1995 | crc = crc32 (base, len, 0xffffffff); | |
1996 | /* Check for memory failure. */ | |
1997 | if (crc == (unsigned long long) -1) | |
1998 | { | |
1999 | write_enn (own_buf); | |
2000 | return; | |
2001 | } | |
2002 | sprintf (own_buf, "C%lx", (unsigned long) crc); | |
2003 | return; | |
2004 | } | |
2005 | ||
d08aafef PA |
2006 | if (handle_qxfer (own_buf, packet_len, new_packet_len_p)) |
2007 | return; | |
2008 | ||
219f2f23 PA |
2009 | if (target_supports_tracepoints () && handle_tracepoint_query (own_buf)) |
2010 | return; | |
2011 | ||
ce3a066d DJ |
2012 | /* Otherwise we didn't know what packet it was. Say we didn't |
2013 | understand it. */ | |
2014 | own_buf[0] = 0; | |
2015 | } | |
2016 | ||
ce1a5b52 PA |
2017 | static void gdb_wants_all_threads_stopped (void); |
2018 | ||
64386c31 DJ |
2019 | /* Parse vCont packets. */ |
2020 | void | |
5b1c542e | 2021 | handle_v_cont (char *own_buf) |
64386c31 DJ |
2022 | { |
2023 | char *p, *q; | |
2024 | int n = 0, i = 0; | |
2bd7c093 | 2025 | struct thread_resume *resume_info; |
95954743 | 2026 | struct thread_resume default_action = {{0}}; |
64386c31 DJ |
2027 | |
2028 | /* Count the number of semicolons in the packet. There should be one | |
2029 | for every action. */ | |
2030 | p = &own_buf[5]; | |
2031 | while (p) | |
2032 | { | |
2033 | n++; | |
2034 | p++; | |
2035 | p = strchr (p, ';'); | |
2036 | } | |
2bd7c093 PA |
2037 | |
2038 | resume_info = malloc (n * sizeof (resume_info[0])); | |
aef93bd7 DE |
2039 | if (resume_info == NULL) |
2040 | goto err; | |
64386c31 | 2041 | |
64386c31 | 2042 | p = &own_buf[5]; |
64386c31 DJ |
2043 | while (*p) |
2044 | { | |
2045 | p++; | |
2046 | ||
c2d6af84 PA |
2047 | memset (&resume_info[i], 0, sizeof resume_info[i]); |
2048 | ||
64386c31 | 2049 | if (p[0] == 's' || p[0] == 'S') |
bd99dc85 | 2050 | resume_info[i].kind = resume_step; |
c2d6af84 PA |
2051 | else if (p[0] == 'r') |
2052 | resume_info[i].kind = resume_step; | |
64386c31 | 2053 | else if (p[0] == 'c' || p[0] == 'C') |
bd99dc85 PA |
2054 | resume_info[i].kind = resume_continue; |
2055 | else if (p[0] == 't') | |
2056 | resume_info[i].kind = resume_stop; | |
64386c31 DJ |
2057 | else |
2058 | goto err; | |
2059 | ||
2060 | if (p[0] == 'S' || p[0] == 'C') | |
2061 | { | |
2062 | int sig; | |
2063 | sig = strtol (p + 1, &q, 16); | |
2064 | if (p == q) | |
2065 | goto err; | |
2066 | p = q; | |
2067 | ||
2ea28649 | 2068 | if (!gdb_signal_to_host_p (sig)) |
64386c31 | 2069 | goto err; |
2ea28649 | 2070 | resume_info[i].sig = gdb_signal_to_host (sig); |
64386c31 | 2071 | } |
c2d6af84 PA |
2072 | else if (p[0] == 'r') |
2073 | { | |
6740dc9c | 2074 | ULONGEST addr; |
c2d6af84 | 2075 | |
6740dc9c PA |
2076 | p = unpack_varlen_hex (p + 1, &addr); |
2077 | resume_info[i].step_range_start = addr; | |
c2d6af84 | 2078 | |
6740dc9c PA |
2079 | if (*p != ',') |
2080 | goto err; | |
c2d6af84 | 2081 | |
6740dc9c PA |
2082 | p = unpack_varlen_hex (p + 1, &addr); |
2083 | resume_info[i].step_range_end = addr; | |
c2d6af84 | 2084 | } |
64386c31 DJ |
2085 | else |
2086 | { | |
64386c31 DJ |
2087 | p = p + 1; |
2088 | } | |
2089 | ||
2090 | if (p[0] == 0) | |
2091 | { | |
95954743 | 2092 | resume_info[i].thread = minus_one_ptid; |
64386c31 DJ |
2093 | default_action = resume_info[i]; |
2094 | ||
2095 | /* Note: we don't increment i here, we'll overwrite this entry | |
2096 | the next time through. */ | |
2097 | } | |
2098 | else if (p[0] == ':') | |
2099 | { | |
95954743 | 2100 | ptid_t ptid = read_ptid (p + 1, &q); |
a06660f7 | 2101 | |
64386c31 DJ |
2102 | if (p == q) |
2103 | goto err; | |
2104 | p = q; | |
2105 | if (p[0] != ';' && p[0] != 0) | |
2106 | goto err; | |
2107 | ||
95954743 | 2108 | resume_info[i].thread = ptid; |
a06660f7 | 2109 | |
64386c31 DJ |
2110 | i++; |
2111 | } | |
2112 | } | |
2113 | ||
2bd7c093 PA |
2114 | if (i < n) |
2115 | resume_info[i] = default_action; | |
64386c31 | 2116 | |
122f36ef PA |
2117 | /* `cont_thread' is still used in occasional places in the backend, |
2118 | to implement single-thread scheduler-locking. Doesn't make sense | |
0c9070b3 YQ |
2119 | to set it if we see a stop request, or a wildcard action (one |
2120 | with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */ | |
bd99dc85 | 2121 | if (n == 1 |
122f36ef PA |
2122 | && !(ptid_equal (resume_info[0].thread, minus_one_ptid) |
2123 | || ptid_get_lwp (resume_info[0].thread) == -1) | |
bd99dc85 | 2124 | && resume_info[0].kind != resume_stop) |
64386c31 DJ |
2125 | cont_thread = resume_info[0].thread; |
2126 | else | |
95954743 | 2127 | cont_thread = minus_one_ptid; |
dc3f8883 | 2128 | set_desired_inferior (0); |
64386c31 | 2129 | |
bd99dc85 PA |
2130 | if (!non_stop) |
2131 | enable_async_io (); | |
2132 | ||
2bd7c093 | 2133 | (*the_target->resume) (resume_info, n); |
64386c31 DJ |
2134 | |
2135 | free (resume_info); | |
2136 | ||
bd99dc85 PA |
2137 | if (non_stop) |
2138 | write_ok (own_buf); | |
2139 | else | |
2140 | { | |
95954743 | 2141 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
ce1a5b52 | 2142 | |
d20a8ad9 PA |
2143 | if (last_status.kind != TARGET_WAITKIND_EXITED |
2144 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
2145 | current_inferior->last_status = last_status; | |
2146 | ||
ce1a5b52 PA |
2147 | /* From the client's perspective, all-stop mode always stops all |
2148 | threads implicitly (and the target backend has already done | |
2149 | so by now). Tag all threads as "want-stopped", so we don't | |
2150 | resume them implicitly without the client telling us to. */ | |
2151 | gdb_wants_all_threads_stopped (); | |
bd99dc85 PA |
2152 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
2153 | disable_async_io (); | |
6bd31874 JB |
2154 | |
2155 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
2156 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
2157 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); | |
bd99dc85 | 2158 | } |
64386c31 DJ |
2159 | return; |
2160 | ||
2161 | err: | |
255e7678 | 2162 | write_enn (own_buf); |
64386c31 DJ |
2163 | free (resume_info); |
2164 | return; | |
2165 | } | |
2166 | ||
2d717e4f DJ |
2167 | /* Attach to a new program. Return 1 if successful, 0 if failure. */ |
2168 | int | |
5b1c542e | 2169 | handle_v_attach (char *own_buf) |
2d717e4f DJ |
2170 | { |
2171 | int pid; | |
2172 | ||
2173 | pid = strtol (own_buf + 8, NULL, 16); | |
5b1c542e | 2174 | if (pid != 0 && attach_inferior (pid) == 0) |
2d717e4f | 2175 | { |
aeba519e PA |
2176 | /* Don't report shared library events after attaching, even if |
2177 | some libraries are preloaded. GDB will always poll the | |
2178 | library list. Avoids the "stopped by shared library event" | |
2179 | notice on the GDB side. */ | |
2180 | dlls_changed = 0; | |
bd99dc85 PA |
2181 | |
2182 | if (non_stop) | |
2183 | { | |
2184 | /* In non-stop, we don't send a resume reply. Stop events | |
2185 | will follow up using the normal notification | |
2186 | mechanism. */ | |
2187 | write_ok (own_buf); | |
2188 | } | |
2189 | else | |
2190 | prepare_resume_reply (own_buf, last_ptid, &last_status); | |
2191 | ||
2d717e4f DJ |
2192 | return 1; |
2193 | } | |
2194 | else | |
2195 | { | |
2196 | write_enn (own_buf); | |
2197 | return 0; | |
2198 | } | |
2199 | } | |
2200 | ||
2201 | /* Run a new program. Return 1 if successful, 0 if failure. */ | |
2202 | static int | |
5b1c542e | 2203 | handle_v_run (char *own_buf) |
2d717e4f | 2204 | { |
aef93bd7 | 2205 | char *p, *next_p, **new_argv; |
2d717e4f DJ |
2206 | int i, new_argc; |
2207 | ||
2208 | new_argc = 0; | |
2209 | for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';')) | |
2210 | { | |
2211 | p++; | |
2212 | new_argc++; | |
2213 | } | |
2214 | ||
aef93bd7 DE |
2215 | new_argv = calloc (new_argc + 2, sizeof (char *)); |
2216 | if (new_argv == NULL) | |
2217 | { | |
2218 | write_enn (own_buf); | |
2219 | return 0; | |
2220 | } | |
2221 | ||
2d717e4f DJ |
2222 | i = 0; |
2223 | for (p = own_buf + strlen ("vRun;"); *p; p = next_p) | |
2224 | { | |
2225 | next_p = strchr (p, ';'); | |
2226 | if (next_p == NULL) | |
2227 | next_p = p + strlen (p); | |
2228 | ||
2229 | if (i == 0 && p == next_p) | |
2230 | new_argv[i] = NULL; | |
2231 | else | |
2232 | { | |
aef93bd7 | 2233 | /* FIXME: Fail request if out of memory instead of dying. */ |
bca929d3 | 2234 | new_argv[i] = xmalloc (1 + (next_p - p) / 2); |
2d717e4f DJ |
2235 | unhexify (new_argv[i], p, (next_p - p) / 2); |
2236 | new_argv[i][(next_p - p) / 2] = '\0'; | |
2237 | } | |
2238 | ||
2239 | if (*next_p) | |
2240 | next_p++; | |
2241 | i++; | |
2242 | } | |
2243 | new_argv[i] = NULL; | |
2244 | ||
2245 | if (new_argv[0] == NULL) | |
2246 | { | |
f142445f DJ |
2247 | /* GDB didn't specify a program to run. Use the program from the |
2248 | last run with the new argument list. */ | |
9b710a42 | 2249 | |
2d717e4f DJ |
2250 | if (program_argv == NULL) |
2251 | { | |
2252 | write_enn (own_buf); | |
b2c04452 | 2253 | freeargv (new_argv); |
2d717e4f DJ |
2254 | return 0; |
2255 | } | |
2256 | ||
aef93bd7 DE |
2257 | new_argv[0] = strdup (program_argv[0]); |
2258 | if (new_argv[0] == NULL) | |
2259 | { | |
aef93bd7 | 2260 | write_enn (own_buf); |
b2c04452 | 2261 | freeargv (new_argv); |
aef93bd7 | 2262 | return 0; |
1b3f6016 | 2263 | } |
2d717e4f | 2264 | } |
f142445f | 2265 | |
aef93bd7 DE |
2266 | /* Free the old argv and install the new one. */ |
2267 | freeargv (program_argv); | |
f142445f | 2268 | program_argv = new_argv; |
2d717e4f | 2269 | |
5b1c542e PA |
2270 | start_inferior (program_argv); |
2271 | if (last_status.kind == TARGET_WAITKIND_STOPPED) | |
2d717e4f | 2272 | { |
5b1c542e | 2273 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
bd99dc85 PA |
2274 | |
2275 | /* In non-stop, sending a resume reply doesn't set the general | |
2276 | thread, but GDB assumes a vRun sets it (this is so GDB can | |
2277 | query which is the main thread of the new inferior. */ | |
2278 | if (non_stop) | |
2279 | general_thread = last_ptid; | |
2280 | ||
2d717e4f DJ |
2281 | return 1; |
2282 | } | |
2283 | else | |
2284 | { | |
2285 | write_enn (own_buf); | |
2286 | return 0; | |
2287 | } | |
2288 | } | |
2289 | ||
95954743 PA |
2290 | /* Kill process. Return 1 if successful, 0 if failure. */ |
2291 | int | |
2292 | handle_v_kill (char *own_buf) | |
2293 | { | |
2294 | int pid; | |
2295 | char *p = &own_buf[6]; | |
0f54c268 PM |
2296 | if (multi_process) |
2297 | pid = strtol (p, NULL, 16); | |
2298 | else | |
2299 | pid = signal_pid; | |
95954743 PA |
2300 | if (pid != 0 && kill_inferior (pid) == 0) |
2301 | { | |
2302 | last_status.kind = TARGET_WAITKIND_SIGNALLED; | |
a493e3e2 | 2303 | last_status.value.sig = GDB_SIGNAL_KILL; |
95954743 PA |
2304 | last_ptid = pid_to_ptid (pid); |
2305 | discard_queued_stop_replies (pid); | |
2306 | write_ok (own_buf); | |
2307 | return 1; | |
2308 | } | |
2309 | else | |
2310 | { | |
2311 | write_enn (own_buf); | |
2312 | return 0; | |
2313 | } | |
2314 | } | |
2315 | ||
64386c31 DJ |
2316 | /* Handle all of the extended 'v' packets. */ |
2317 | void | |
5b1c542e | 2318 | handle_v_requests (char *own_buf, int packet_len, int *new_packet_len) |
64386c31 | 2319 | { |
db42f210 | 2320 | if (!disable_packet_vCont) |
64386c31 | 2321 | { |
db42f210 PA |
2322 | if (strncmp (own_buf, "vCont;", 6) == 0) |
2323 | { | |
2324 | require_running (own_buf); | |
5b1c542e | 2325 | handle_v_cont (own_buf); |
db42f210 PA |
2326 | return; |
2327 | } | |
64386c31 | 2328 | |
db42f210 PA |
2329 | if (strncmp (own_buf, "vCont?", 6) == 0) |
2330 | { | |
bd99dc85 | 2331 | strcpy (own_buf, "vCont;c;C;s;S;t"); |
c2d6af84 PA |
2332 | if (target_supports_range_stepping ()) |
2333 | { | |
2334 | own_buf = own_buf + strlen (own_buf); | |
2335 | strcpy (own_buf, ";r"); | |
2336 | } | |
db42f210 PA |
2337 | return; |
2338 | } | |
64386c31 DJ |
2339 | } |
2340 | ||
a6b151f1 DJ |
2341 | if (strncmp (own_buf, "vFile:", 6) == 0 |
2342 | && handle_vFile (own_buf, packet_len, new_packet_len)) | |
2343 | return; | |
2344 | ||
2d717e4f DJ |
2345 | if (strncmp (own_buf, "vAttach;", 8) == 0) |
2346 | { | |
901f9912 | 2347 | if ((!extended_protocol || !multi_process) && target_running ()) |
2d717e4f | 2348 | { |
fd96d250 PA |
2349 | fprintf (stderr, "Already debugging a process\n"); |
2350 | write_enn (own_buf); | |
2351 | return; | |
2d717e4f | 2352 | } |
5b1c542e | 2353 | handle_v_attach (own_buf); |
2d717e4f DJ |
2354 | return; |
2355 | } | |
2356 | ||
2357 | if (strncmp (own_buf, "vRun;", 5) == 0) | |
2358 | { | |
901f9912 | 2359 | if ((!extended_protocol || !multi_process) && target_running ()) |
2d717e4f | 2360 | { |
fd96d250 PA |
2361 | fprintf (stderr, "Already debugging a process\n"); |
2362 | write_enn (own_buf); | |
2363 | return; | |
2d717e4f | 2364 | } |
5b1c542e | 2365 | handle_v_run (own_buf); |
2d717e4f DJ |
2366 | return; |
2367 | } | |
2368 | ||
95954743 PA |
2369 | if (strncmp (own_buf, "vKill;", 6) == 0) |
2370 | { | |
2371 | if (!target_running ()) | |
2372 | { | |
2373 | fprintf (stderr, "No process to kill\n"); | |
2374 | write_enn (own_buf); | |
2375 | return; | |
2376 | } | |
2377 | handle_v_kill (own_buf); | |
2378 | return; | |
2379 | } | |
2380 | ||
14a00470 YQ |
2381 | if (handle_notif_ack (own_buf, packet_len)) |
2382 | return; | |
bd99dc85 | 2383 | |
64386c31 DJ |
2384 | /* Otherwise we didn't know what packet it was. Say we didn't |
2385 | understand it. */ | |
2386 | own_buf[0] = 0; | |
2387 | return; | |
2388 | } | |
2389 | ||
bd99dc85 PA |
2390 | /* Resume inferior and wait for another event. In non-stop mode, |
2391 | don't really wait here, but return immediatelly to the event | |
2392 | loop. */ | |
1fd7cdc2 | 2393 | static void |
5b1c542e | 2394 | myresume (char *own_buf, int step, int sig) |
64386c31 DJ |
2395 | { |
2396 | struct thread_resume resume_info[2]; | |
2397 | int n = 0; | |
2bd7c093 | 2398 | int valid_cont_thread; |
a20d5e98 DJ |
2399 | |
2400 | set_desired_inferior (0); | |
64386c31 | 2401 | |
95954743 PA |
2402 | valid_cont_thread = (!ptid_equal (cont_thread, null_ptid) |
2403 | && !ptid_equal (cont_thread, minus_one_ptid)); | |
2bd7c093 PA |
2404 | |
2405 | if (step || sig || valid_cont_thread) | |
64386c31 | 2406 | { |
fbd5db48 | 2407 | resume_info[0].thread = current_ptid; |
bd99dc85 PA |
2408 | if (step) |
2409 | resume_info[0].kind = resume_step; | |
2410 | else | |
2411 | resume_info[0].kind = resume_continue; | |
64386c31 | 2412 | resume_info[0].sig = sig; |
64386c31 DJ |
2413 | n++; |
2414 | } | |
2bd7c093 PA |
2415 | |
2416 | if (!valid_cont_thread) | |
2417 | { | |
95954743 | 2418 | resume_info[n].thread = minus_one_ptid; |
bd99dc85 | 2419 | resume_info[n].kind = resume_continue; |
2bd7c093 PA |
2420 | resume_info[n].sig = 0; |
2421 | n++; | |
2422 | } | |
64386c31 | 2423 | |
bd99dc85 PA |
2424 | if (!non_stop) |
2425 | enable_async_io (); | |
2426 | ||
2bd7c093 | 2427 | (*the_target->resume) (resume_info, n); |
bd99dc85 PA |
2428 | |
2429 | if (non_stop) | |
2430 | write_ok (own_buf); | |
2431 | else | |
2432 | { | |
95954743 | 2433 | last_ptid = mywait (minus_one_ptid, &last_status, 0, 1); |
d20a8ad9 PA |
2434 | |
2435 | if (last_status.kind != TARGET_WAITKIND_EXITED | |
2436 | && last_status.kind != TARGET_WAITKIND_SIGNALLED) | |
2437 | { | |
2438 | current_inferior->last_resume_kind = resume_stop; | |
2439 | current_inferior->last_status = last_status; | |
2440 | } | |
2441 | ||
bd99dc85 PA |
2442 | prepare_resume_reply (own_buf, last_ptid, &last_status); |
2443 | disable_async_io (); | |
6bd31874 JB |
2444 | |
2445 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
2446 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
2447 | mourn_inferior (find_process_pid (ptid_get_pid (last_ptid))); | |
bd99dc85 PA |
2448 | } |
2449 | } | |
2450 | ||
2451 | /* Callback for for_each_inferior. Make a new stop reply for each | |
2452 | stopped thread. */ | |
2453 | ||
95954743 PA |
2454 | static int |
2455 | queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg) | |
bd99dc85 | 2456 | { |
8336d594 | 2457 | struct thread_info *thread = (struct thread_info *) entry; |
bd99dc85 | 2458 | |
8336d594 PA |
2459 | /* For now, assume targets that don't have this callback also don't |
2460 | manage the thread's last_status field. */ | |
2461 | if (the_target->thread_stopped == NULL) | |
95954743 | 2462 | { |
14a00470 YQ |
2463 | struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif)); |
2464 | ||
2465 | new_notif->ptid = entry->id; | |
2466 | new_notif->status = thread->last_status; | |
8336d594 PA |
2467 | /* Pass the last stop reply back to GDB, but don't notify |
2468 | yet. */ | |
14a00470 YQ |
2469 | notif_event_enque (¬if_stop, |
2470 | (struct notif_event *) new_notif); | |
8336d594 PA |
2471 | } |
2472 | else | |
2473 | { | |
2474 | if (thread_stopped (thread)) | |
2475 | { | |
2476 | if (debug_threads) | |
3360c0bf LM |
2477 | { |
2478 | char *status_string | |
2479 | = target_waitstatus_to_string (&thread->last_status); | |
2480 | ||
2481 | fprintf (stderr, | |
2482 | "Reporting thread %s as already stopped with %s\n", | |
2483 | target_pid_to_str (entry->id), | |
2484 | status_string); | |
2485 | ||
2486 | xfree (status_string); | |
2487 | } | |
8336d594 | 2488 | |
d20a8ad9 PA |
2489 | gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE); |
2490 | ||
8336d594 PA |
2491 | /* Pass the last stop reply back to GDB, but don't notify |
2492 | yet. */ | |
2493 | queue_stop_reply (entry->id, &thread->last_status); | |
2494 | } | |
95954743 PA |
2495 | } |
2496 | ||
2497 | return 0; | |
64386c31 DJ |
2498 | } |
2499 | ||
ce1a5b52 PA |
2500 | /* Set this inferior threads's state as "want-stopped". We won't |
2501 | resume this thread until the client gives us another action for | |
2502 | it. */ | |
8336d594 PA |
2503 | |
2504 | static void | |
2505 | gdb_wants_thread_stopped (struct inferior_list_entry *entry) | |
2506 | { | |
2507 | struct thread_info *thread = (struct thread_info *) entry; | |
2508 | ||
2509 | thread->last_resume_kind = resume_stop; | |
2510 | ||
2511 | if (thread->last_status.kind == TARGET_WAITKIND_IGNORE) | |
2512 | { | |
ce1a5b52 PA |
2513 | /* Most threads are stopped implicitly (all-stop); tag that with |
2514 | signal 0. */ | |
8336d594 | 2515 | thread->last_status.kind = TARGET_WAITKIND_STOPPED; |
a493e3e2 | 2516 | thread->last_status.value.sig = GDB_SIGNAL_0; |
8336d594 PA |
2517 | } |
2518 | } | |
2519 | ||
2520 | /* Set all threads' states as "want-stopped". */ | |
2521 | ||
2522 | static void | |
2523 | gdb_wants_all_threads_stopped (void) | |
2524 | { | |
2525 | for_each_inferior (&all_threads, gdb_wants_thread_stopped); | |
2526 | } | |
2527 | ||
2528 | /* Clear the gdb_detached flag of every process. */ | |
2529 | ||
2530 | static void | |
2531 | gdb_reattached_process (struct inferior_list_entry *entry) | |
2532 | { | |
2533 | struct process_info *process = (struct process_info *) entry; | |
2534 | ||
2535 | process->gdb_detached = 0; | |
2536 | } | |
2537 | ||
5b1c542e PA |
2538 | /* Status handler for the '?' packet. */ |
2539 | ||
2540 | static void | |
2541 | handle_status (char *own_buf) | |
2542 | { | |
8336d594 PA |
2543 | /* GDB is connected, don't forward events to the target anymore. */ |
2544 | for_each_inferior (&all_processes, gdb_reattached_process); | |
bd99dc85 PA |
2545 | |
2546 | /* In non-stop mode, we must send a stop reply for each stopped | |
2547 | thread. In all-stop mode, just send one for the first stopped | |
2548 | thread we find. */ | |
2549 | ||
2550 | if (non_stop) | |
2551 | { | |
8336d594 PA |
2552 | discard_queued_stop_replies (-1); |
2553 | find_inferior (&all_threads, queue_stop_reply_callback, NULL); | |
bd99dc85 PA |
2554 | |
2555 | /* The first is sent immediatly. OK is sent if there is no | |
2556 | stopped thread, which is the same handling of the vStopped | |
2557 | packet (by design). */ | |
14a00470 | 2558 | notif_write_event (¬if_stop, own_buf); |
bd99dc85 | 2559 | } |
5b1c542e | 2560 | else |
bd99dc85 | 2561 | { |
7984d532 | 2562 | pause_all (0); |
fa593d66 | 2563 | stabilize_threads (); |
8336d594 PA |
2564 | gdb_wants_all_threads_stopped (); |
2565 | ||
bd99dc85 | 2566 | if (all_threads.head) |
8336d594 PA |
2567 | { |
2568 | struct target_waitstatus status; | |
2569 | ||
2570 | status.kind = TARGET_WAITKIND_STOPPED; | |
a493e3e2 | 2571 | status.value.sig = GDB_SIGNAL_TRAP; |
8336d594 PA |
2572 | prepare_resume_reply (own_buf, |
2573 | all_threads.head->id, &status); | |
2574 | } | |
bd99dc85 PA |
2575 | else |
2576 | strcpy (own_buf, "W00"); | |
2577 | } | |
5b1c542e PA |
2578 | } |
2579 | ||
dd24457d DJ |
2580 | static void |
2581 | gdbserver_version (void) | |
2582 | { | |
c16158bc | 2583 | printf ("GNU gdbserver %s%s\n" |
b9786c74 | 2584 | "Copyright (C) 2013 Free Software Foundation, Inc.\n" |
493e2a69 MS |
2585 | "gdbserver is free software, covered by the " |
2586 | "GNU General Public License.\n" | |
dd24457d | 2587 | "This gdbserver was configured as \"%s\"\n", |
c16158bc | 2588 | PKGVERSION, version, host_name); |
dd24457d DJ |
2589 | } |
2590 | ||
0bc68c49 | 2591 | static void |
c16158bc | 2592 | gdbserver_usage (FILE *stream) |
0bc68c49 | 2593 | { |
c16158bc JM |
2594 | fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n" |
2595 | "\tgdbserver [OPTIONS] --attach COMM PID\n" | |
2596 | "\tgdbserver [OPTIONS] --multi COMM\n" | |
2597 | "\n" | |
2598 | "COMM may either be a tty device (for serial debugging), or \n" | |
2599 | "HOST:PORT to listen for a TCP connection.\n" | |
2600 | "\n" | |
2601 | "Options:\n" | |
62709adf PA |
2602 | " --debug Enable general debugging output.\n" |
2603 | " --remote-debug Enable remote protocol debugging output.\n" | |
2604 | " --version Display version information and exit.\n" | |
03f2bd59 JK |
2605 | " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n" |
2606 | " --once Exit after the first connection has " | |
2607 | "closed.\n"); | |
c16158bc JM |
2608 | if (REPORT_BUGS_TO[0] && stream == stdout) |
2609 | fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO); | |
0bc68c49 DJ |
2610 | } |
2611 | ||
db42f210 PA |
2612 | static void |
2613 | gdbserver_show_disableable (FILE *stream) | |
2614 | { | |
2615 | fprintf (stream, "Disableable packets:\n" | |
2616 | " vCont \tAll vCont packets\n" | |
2617 | " qC \tQuerying the current thread\n" | |
2618 | " qfThreadInfo\tThread listing\n" | |
493e2a69 MS |
2619 | " Tthread \tPassing the thread specifier in the " |
2620 | "T stop reply packet\n" | |
db42f210 PA |
2621 | " threads \tAll of the above\n"); |
2622 | } | |
2623 | ||
2624 | ||
2d717e4f DJ |
2625 | #undef require_running |
2626 | #define require_running(BUF) \ | |
2627 | if (!target_running ()) \ | |
2628 | { \ | |
2629 | write_enn (BUF); \ | |
2630 | break; \ | |
2631 | } | |
2632 | ||
95954743 PA |
2633 | static int |
2634 | first_thread_of (struct inferior_list_entry *entry, void *args) | |
2635 | { | |
2636 | int pid = * (int *) args; | |
2637 | ||
2638 | if (ptid_get_pid (entry->id) == pid) | |
2639 | return 1; | |
2640 | ||
2641 | return 0; | |
2642 | } | |
2643 | ||
2644 | static void | |
2645 | kill_inferior_callback (struct inferior_list_entry *entry) | |
2646 | { | |
2647 | struct process_info *process = (struct process_info *) entry; | |
2648 | int pid = ptid_get_pid (process->head.id); | |
2649 | ||
2650 | kill_inferior (pid); | |
2651 | discard_queued_stop_replies (pid); | |
2652 | } | |
2653 | ||
9f767825 DE |
2654 | /* Callback for for_each_inferior to detach or kill the inferior, |
2655 | depending on whether we attached to it or not. | |
2656 | We inform the user whether we're detaching or killing the process | |
2657 | as this is only called when gdbserver is about to exit. */ | |
2658 | ||
95954743 PA |
2659 | static void |
2660 | detach_or_kill_inferior_callback (struct inferior_list_entry *entry) | |
2661 | { | |
2662 | struct process_info *process = (struct process_info *) entry; | |
2663 | int pid = ptid_get_pid (process->head.id); | |
2664 | ||
2665 | if (process->attached) | |
2666 | detach_inferior (pid); | |
2667 | else | |
2668 | kill_inferior (pid); | |
2669 | ||
2670 | discard_queued_stop_replies (pid); | |
2671 | } | |
2672 | ||
9f767825 DE |
2673 | /* for_each_inferior callback for detach_or_kill_for_exit to print |
2674 | the pids of started inferiors. */ | |
2675 | ||
2676 | static void | |
2677 | print_started_pid (struct inferior_list_entry *entry) | |
2678 | { | |
2679 | struct process_info *process = (struct process_info *) entry; | |
2680 | ||
2681 | if (! process->attached) | |
2682 | { | |
2683 | int pid = ptid_get_pid (process->head.id); | |
2684 | fprintf (stderr, " %d", pid); | |
2685 | } | |
2686 | } | |
2687 | ||
2688 | /* for_each_inferior callback for detach_or_kill_for_exit to print | |
2689 | the pids of attached inferiors. */ | |
2690 | ||
2691 | static void | |
2692 | print_attached_pid (struct inferior_list_entry *entry) | |
2693 | { | |
2694 | struct process_info *process = (struct process_info *) entry; | |
2695 | ||
2696 | if (process->attached) | |
2697 | { | |
2698 | int pid = ptid_get_pid (process->head.id); | |
2699 | fprintf (stderr, " %d", pid); | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | /* Call this when exiting gdbserver with possible inferiors that need | |
2704 | to be killed or detached from. */ | |
2705 | ||
2706 | static void | |
2707 | detach_or_kill_for_exit (void) | |
2708 | { | |
2709 | /* First print a list of the inferiors we will be killing/detaching. | |
2710 | This is to assist the user, for example, in case the inferior unexpectedly | |
2711 | dies after we exit: did we screw up or did the inferior exit on its own? | |
2712 | Having this info will save some head-scratching. */ | |
2713 | ||
2714 | if (have_started_inferiors_p ()) | |
2715 | { | |
2716 | fprintf (stderr, "Killing process(es):"); | |
2717 | for_each_inferior (&all_processes, print_started_pid); | |
2718 | fprintf (stderr, "\n"); | |
2719 | } | |
2720 | if (have_attached_inferiors_p ()) | |
2721 | { | |
2722 | fprintf (stderr, "Detaching process(es):"); | |
2723 | for_each_inferior (&all_processes, print_attached_pid); | |
2724 | fprintf (stderr, "\n"); | |
2725 | } | |
2726 | ||
2727 | /* Now we can kill or detach the inferiors. */ | |
2728 | ||
2729 | for_each_inferior (&all_processes, detach_or_kill_inferior_callback); | |
2730 | } | |
2731 | ||
c906108c | 2732 | int |
da85418c | 2733 | main (int argc, char *argv[]) |
c906108c | 2734 | { |
0729219d DJ |
2735 | int bad_attach; |
2736 | int pid; | |
2d717e4f DJ |
2737 | char *arg_end, *port; |
2738 | char **next_arg = &argv[1]; | |
89dc0afd JK |
2739 | volatile int multi_mode = 0; |
2740 | volatile int attach = 0; | |
2d717e4f | 2741 | int was_running; |
c906108c | 2742 | |
2d717e4f | 2743 | while (*next_arg != NULL && **next_arg == '-') |
dd24457d | 2744 | { |
2d717e4f DJ |
2745 | if (strcmp (*next_arg, "--version") == 0) |
2746 | { | |
2747 | gdbserver_version (); | |
2748 | exit (0); | |
2749 | } | |
2750 | else if (strcmp (*next_arg, "--help") == 0) | |
2751 | { | |
c16158bc | 2752 | gdbserver_usage (stdout); |
2d717e4f DJ |
2753 | exit (0); |
2754 | } | |
2755 | else if (strcmp (*next_arg, "--attach") == 0) | |
2756 | attach = 1; | |
2757 | else if (strcmp (*next_arg, "--multi") == 0) | |
2758 | multi_mode = 1; | |
ccd213ac DJ |
2759 | else if (strcmp (*next_arg, "--wrapper") == 0) |
2760 | { | |
2761 | next_arg++; | |
2762 | ||
2763 | wrapper_argv = next_arg; | |
2764 | while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) | |
2765 | next_arg++; | |
2766 | ||
2767 | if (next_arg == wrapper_argv || *next_arg == NULL) | |
2768 | { | |
c16158bc | 2769 | gdbserver_usage (stderr); |
ccd213ac DJ |
2770 | exit (1); |
2771 | } | |
2772 | ||
2773 | /* Consume the "--". */ | |
2774 | *next_arg = NULL; | |
2775 | } | |
2d717e4f DJ |
2776 | else if (strcmp (*next_arg, "--debug") == 0) |
2777 | debug_threads = 1; | |
62709adf PA |
2778 | else if (strcmp (*next_arg, "--remote-debug") == 0) |
2779 | remote_debug = 1; | |
db42f210 PA |
2780 | else if (strcmp (*next_arg, "--disable-packet") == 0) |
2781 | { | |
2782 | gdbserver_show_disableable (stdout); | |
2783 | exit (0); | |
2784 | } | |
2785 | else if (strncmp (*next_arg, | |
2786 | "--disable-packet=", | |
2787 | sizeof ("--disable-packet=") - 1) == 0) | |
2788 | { | |
2789 | char *packets, *tok; | |
2790 | ||
2791 | packets = *next_arg += sizeof ("--disable-packet=") - 1; | |
2792 | for (tok = strtok (packets, ","); | |
2793 | tok != NULL; | |
2794 | tok = strtok (NULL, ",")) | |
2795 | { | |
2796 | if (strcmp ("vCont", tok) == 0) | |
2797 | disable_packet_vCont = 1; | |
2798 | else if (strcmp ("Tthread", tok) == 0) | |
2799 | disable_packet_Tthread = 1; | |
2800 | else if (strcmp ("qC", tok) == 0) | |
2801 | disable_packet_qC = 1; | |
2802 | else if (strcmp ("qfThreadInfo", tok) == 0) | |
2803 | disable_packet_qfThreadInfo = 1; | |
2804 | else if (strcmp ("threads", tok) == 0) | |
2805 | { | |
2806 | disable_packet_vCont = 1; | |
2807 | disable_packet_Tthread = 1; | |
2808 | disable_packet_qC = 1; | |
2809 | disable_packet_qfThreadInfo = 1; | |
2810 | } | |
2811 | else | |
2812 | { | |
2813 | fprintf (stderr, "Don't know how to disable \"%s\".\n\n", | |
2814 | tok); | |
2815 | gdbserver_show_disableable (stderr); | |
2816 | exit (1); | |
2817 | } | |
2818 | } | |
2819 | } | |
e0f9f062 DE |
2820 | else if (strcmp (*next_arg, "-") == 0) |
2821 | { | |
2822 | /* "-" specifies a stdio connection and is a form of port | |
2823 | specification. */ | |
2824 | *next_arg = STDIO_CONNECTION_NAME; | |
2825 | break; | |
2826 | } | |
03583c20 UW |
2827 | else if (strcmp (*next_arg, "--disable-randomization") == 0) |
2828 | disable_randomization = 1; | |
2829 | else if (strcmp (*next_arg, "--no-disable-randomization") == 0) | |
2830 | disable_randomization = 0; | |
03f2bd59 JK |
2831 | else if (strcmp (*next_arg, "--once") == 0) |
2832 | run_once = 1; | |
2d717e4f DJ |
2833 | else |
2834 | { | |
2835 | fprintf (stderr, "Unknown argument: %s\n", *next_arg); | |
2836 | exit (1); | |
2837 | } | |
dd24457d | 2838 | |
2d717e4f DJ |
2839 | next_arg++; |
2840 | continue; | |
dd24457d DJ |
2841 | } |
2842 | ||
c5aa993b | 2843 | if (setjmp (toplevel)) |
c906108c | 2844 | { |
c5aa993b JM |
2845 | fprintf (stderr, "Exiting\n"); |
2846 | exit (1); | |
c906108c SS |
2847 | } |
2848 | ||
2d717e4f DJ |
2849 | port = *next_arg; |
2850 | next_arg++; | |
2851 | if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) | |
2852 | { | |
c16158bc | 2853 | gdbserver_usage (stderr); |
2d717e4f DJ |
2854 | exit (1); |
2855 | } | |
2856 | ||
602e3198 JK |
2857 | /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be |
2858 | opened by remote_prepare. */ | |
2859 | notice_open_fds (); | |
2860 | ||
e0f9f062 DE |
2861 | /* We need to know whether the remote connection is stdio before |
2862 | starting the inferior. Inferiors created in this scenario have | |
2863 | stdin,stdout redirected. So do this here before we call | |
2864 | start_inferior. */ | |
2865 | remote_prepare (port); | |
2866 | ||
0729219d DJ |
2867 | bad_attach = 0; |
2868 | pid = 0; | |
2d717e4f DJ |
2869 | |
2870 | /* --attach used to come after PORT, so allow it there for | |
2871 | compatibility. */ | |
2872 | if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) | |
45b7b345 | 2873 | { |
2d717e4f DJ |
2874 | attach = 1; |
2875 | next_arg++; | |
45b7b345 DJ |
2876 | } |
2877 | ||
2d717e4f DJ |
2878 | if (attach |
2879 | && (*next_arg == NULL | |
2880 | || (*next_arg)[0] == '\0' | |
2881 | || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 | |
2882 | || *arg_end != '\0' | |
2883 | || next_arg[1] != NULL)) | |
2884 | bad_attach = 1; | |
2885 | ||
2886 | if (bad_attach) | |
dd24457d | 2887 | { |
c16158bc | 2888 | gdbserver_usage (stderr); |
dd24457d DJ |
2889 | exit (1); |
2890 | } | |
c906108c | 2891 | |
a20d5e98 | 2892 | initialize_async_io (); |
4ce44c66 | 2893 | initialize_low (); |
60f662b0 | 2894 | initialize_event_loop (); |
219f2f23 PA |
2895 | if (target_supports_tracepoints ()) |
2896 | initialize_tracepoint (); | |
4ce44c66 | 2897 | |
bca929d3 DE |
2898 | own_buf = xmalloc (PBUFSIZ + 1); |
2899 | mem_buf = xmalloc (PBUFSIZ); | |
0a30fbc4 | 2900 | |
2d717e4f | 2901 | if (pid == 0 && *next_arg != NULL) |
45b7b345 | 2902 | { |
2d717e4f DJ |
2903 | int i, n; |
2904 | ||
2905 | n = argc - (next_arg - argv); | |
bca929d3 | 2906 | program_argv = xmalloc (sizeof (char *) * (n + 1)); |
2d717e4f | 2907 | for (i = 0; i < n; i++) |
bca929d3 | 2908 | program_argv[i] = xstrdup (next_arg[i]); |
2d717e4f DJ |
2909 | program_argv[i] = NULL; |
2910 | ||
45b7b345 | 2911 | /* Wait till we are at first instruction in program. */ |
5b1c542e | 2912 | start_inferior (program_argv); |
c906108c | 2913 | |
c588c53c MS |
2914 | /* We are now (hopefully) stopped at the first instruction of |
2915 | the target process. This assumes that the target process was | |
2916 | successfully created. */ | |
45b7b345 | 2917 | } |
2d717e4f DJ |
2918 | else if (pid != 0) |
2919 | { | |
5b1c542e | 2920 | if (attach_inferior (pid) == -1) |
2d717e4f DJ |
2921 | error ("Attaching not supported on this target"); |
2922 | ||
2923 | /* Otherwise succeeded. */ | |
2924 | } | |
45b7b345 DJ |
2925 | else |
2926 | { | |
5b1c542e PA |
2927 | last_status.kind = TARGET_WAITKIND_EXITED; |
2928 | last_status.value.integer = 0; | |
95954743 | 2929 | last_ptid = minus_one_ptid; |
45b7b345 | 2930 | } |
c906108c | 2931 | |
14a00470 YQ |
2932 | initialize_notif (); |
2933 | ||
311de423 PA |
2934 | /* Don't report shared library events on the initial connection, |
2935 | even if some libraries are preloaded. Avoids the "stopped by | |
2936 | shared library event" notice on gdb side. */ | |
2937 | dlls_changed = 0; | |
2938 | ||
8264bb58 DJ |
2939 | if (setjmp (toplevel)) |
2940 | { | |
f128d5e9 PA |
2941 | /* If something fails and longjmps while detaching or killing |
2942 | inferiors, we'd end up here again, stuck in an infinite loop | |
2943 | trap. Be sure that if that happens, we exit immediately | |
2944 | instead. */ | |
01b17894 PA |
2945 | if (setjmp (toplevel) == 0) |
2946 | detach_or_kill_for_exit (); | |
2947 | else | |
2948 | fprintf (stderr, "Detach or kill failed. Exiting\n"); | |
8264bb58 DJ |
2949 | exit (1); |
2950 | } | |
2951 | ||
5b1c542e PA |
2952 | if (last_status.kind == TARGET_WAITKIND_EXITED |
2953 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
2d717e4f DJ |
2954 | was_running = 0; |
2955 | else | |
2956 | was_running = 1; | |
2957 | ||
2958 | if (!was_running && !multi_mode) | |
c588c53c | 2959 | { |
2d717e4f | 2960 | fprintf (stderr, "No program to debug. GDBserver exiting.\n"); |
c588c53c MS |
2961 | exit (1); |
2962 | } | |
2963 | ||
c906108c SS |
2964 | while (1) |
2965 | { | |
a6f3e723 | 2966 | noack_mode = 0; |
95954743 | 2967 | multi_process = 0; |
8336d594 PA |
2968 | /* Be sure we're out of tfind mode. */ |
2969 | current_traceframe = -1; | |
bd99dc85 | 2970 | |
2d717e4f | 2971 | remote_open (port); |
c906108c | 2972 | |
2d717e4f DJ |
2973 | if (setjmp (toplevel) != 0) |
2974 | { | |
2975 | /* An error occurred. */ | |
2976 | if (response_needed) | |
2977 | { | |
2978 | write_enn (own_buf); | |
2979 | putpkt (own_buf); | |
2980 | } | |
2981 | } | |
2982 | ||
bd99dc85 | 2983 | /* Wait for events. This will return when all event sources are |
8336d594 | 2984 | removed from the event loop. */ |
bd99dc85 PA |
2985 | start_event_loop (); |
2986 | ||
2987 | /* If an exit was requested (using the "monitor exit" command), | |
2988 | terminate now. The only other way to get here is for | |
2989 | getpkt to fail; close the connection and reopen it at the | |
2990 | top of the loop. */ | |
2991 | ||
03f2bd59 | 2992 | if (exit_requested || run_once) |
c906108c | 2993 | { |
01b17894 PA |
2994 | /* If something fails and longjmps while detaching or |
2995 | killing inferiors, we'd end up here again, stuck in an | |
2996 | infinite loop trap. Be sure that if that happens, we | |
2997 | exit immediately instead. */ | |
2998 | if (setjmp (toplevel) == 0) | |
2999 | { | |
3000 | detach_or_kill_for_exit (); | |
3001 | exit (0); | |
3002 | } | |
3003 | else | |
3004 | { | |
3005 | fprintf (stderr, "Detach or kill failed. Exiting\n"); | |
3006 | exit (1); | |
3007 | } | |
bd99dc85 | 3008 | } |
8336d594 PA |
3009 | |
3010 | fprintf (stderr, | |
3011 | "Remote side has terminated connection. " | |
3012 | "GDBserver will reopen the connection.\n"); | |
3013 | ||
3014 | if (tracing) | |
3015 | { | |
3016 | if (disconnected_tracing) | |
3017 | { | |
3018 | /* Try to enable non-stop/async mode, so we we can both | |
3019 | wait for an async socket accept, and handle async | |
3020 | target events simultaneously. There's also no point | |
3021 | either in having the target always stop all threads, | |
3022 | when we're going to pass signals down without | |
3023 | informing GDB. */ | |
3024 | if (!non_stop) | |
3025 | { | |
3026 | if (start_non_stop (1)) | |
3027 | non_stop = 1; | |
3028 | ||
3029 | /* Detaching implicitly resumes all threads; simply | |
3030 | disconnecting does not. */ | |
3031 | } | |
3032 | } | |
3033 | else | |
3034 | { | |
3035 | fprintf (stderr, | |
3036 | "Disconnected tracing disabled; stopping trace run.\n"); | |
3037 | stop_tracing (); | |
3038 | } | |
3039 | } | |
bd99dc85 PA |
3040 | } |
3041 | } | |
01f9e8fa | 3042 | |
9f3a5c85 LM |
3043 | /* Process options coming from Z packets for *point at address |
3044 | POINT_ADDR. PACKET is the packet buffer. *PACKET is updated | |
3045 | to point to the first char after the last processed option. */ | |
3046 | ||
3047 | static void | |
3048 | process_point_options (CORE_ADDR point_addr, char **packet) | |
3049 | { | |
3050 | char *dataptr = *packet; | |
d3ce09f5 | 3051 | int persist; |
9f3a5c85 LM |
3052 | |
3053 | /* Check if data has the correct format. */ | |
3054 | if (*dataptr != ';') | |
3055 | return; | |
3056 | ||
3057 | dataptr++; | |
3058 | ||
3059 | while (*dataptr) | |
3060 | { | |
d3ce09f5 SS |
3061 | if (*dataptr == ';') |
3062 | ++dataptr; | |
3063 | ||
3064 | if (*dataptr == 'X') | |
9f3a5c85 | 3065 | { |
d3ce09f5 | 3066 | /* Conditional expression. */ |
d171ca78 YQ |
3067 | if (debug_threads) |
3068 | fprintf (stderr, "Found breakpoint condition.\n"); | |
d3ce09f5 SS |
3069 | add_breakpoint_condition (point_addr, &dataptr); |
3070 | } | |
3071 | else if (strncmp (dataptr, "cmds:", strlen ("cmds:")) == 0) | |
3072 | { | |
3073 | dataptr += strlen ("cmds:"); | |
3074 | if (debug_threads) | |
3075 | fprintf (stderr, "Found breakpoint commands %s.\n", dataptr); | |
3076 | persist = (*dataptr == '1'); | |
3077 | dataptr += 2; | |
3078 | add_breakpoint_commands (point_addr, &dataptr, persist); | |
3079 | } | |
3080 | else | |
3081 | { | |
d3ce09f5 SS |
3082 | fprintf (stderr, "Unknown token %c, ignoring.\n", |
3083 | *dataptr); | |
78a99e91 PA |
3084 | /* Skip tokens until we find one that we recognize. */ |
3085 | while (*dataptr && *dataptr != ';') | |
3086 | dataptr++; | |
9f3a5c85 | 3087 | } |
9f3a5c85 LM |
3088 | } |
3089 | *packet = dataptr; | |
3090 | } | |
3091 | ||
bd99dc85 PA |
3092 | /* Event loop callback that handles a serial event. The first byte in |
3093 | the serial buffer gets us here. We expect characters to arrive at | |
3094 | a brisk pace, so we read the rest of the packet with a blocking | |
3095 | getpkt call. */ | |
01f9e8fa | 3096 | |
8336d594 | 3097 | static int |
bd99dc85 PA |
3098 | process_serial_event (void) |
3099 | { | |
3100 | char ch; | |
3101 | int i = 0; | |
3102 | int signal; | |
3103 | unsigned int len; | |
764880b7 | 3104 | int res; |
bd99dc85 | 3105 | CORE_ADDR mem_addr; |
95954743 | 3106 | int pid; |
bd99dc85 PA |
3107 | unsigned char sig; |
3108 | int packet_len; | |
3109 | int new_packet_len = -1; | |
3110 | ||
3111 | /* Used to decide when gdbserver should exit in | |
3112 | multi-mode/remote. */ | |
3113 | static int have_ran = 0; | |
3114 | ||
3115 | if (!have_ran) | |
3116 | have_ran = target_running (); | |
3117 | ||
3118 | disable_async_io (); | |
3119 | ||
3120 | response_needed = 0; | |
3121 | packet_len = getpkt (own_buf); | |
3122 | if (packet_len <= 0) | |
3123 | { | |
bd99dc85 | 3124 | remote_close (); |
8336d594 PA |
3125 | /* Force an event loop break. */ |
3126 | return -1; | |
bd99dc85 PA |
3127 | } |
3128 | response_needed = 1; | |
3129 | ||
3130 | i = 0; | |
3131 | ch = own_buf[i++]; | |
3132 | switch (ch) | |
3133 | { | |
3134 | case 'q': | |
3135 | handle_query (own_buf, packet_len, &new_packet_len); | |
3136 | break; | |
3137 | case 'Q': | |
3138 | handle_general_set (own_buf); | |
3139 | break; | |
3140 | case 'D': | |
3141 | require_running (own_buf); | |
95954743 PA |
3142 | |
3143 | if (multi_process) | |
3144 | { | |
3145 | i++; /* skip ';' */ | |
3146 | pid = strtol (&own_buf[i], NULL, 16); | |
3147 | } | |
3148 | else | |
fbd5db48 | 3149 | pid = ptid_get_pid (current_ptid); |
95954743 | 3150 | |
d3ce09f5 | 3151 | if ((tracing && disconnected_tracing) || any_persistent_commands ()) |
8336d594 PA |
3152 | { |
3153 | struct thread_resume resume_info; | |
3154 | struct process_info *process = find_process_pid (pid); | |
3155 | ||
3156 | if (process == NULL) | |
3157 | { | |
3158 | write_enn (own_buf); | |
3159 | break; | |
3160 | } | |
3161 | ||
d3ce09f5 SS |
3162 | if (tracing && disconnected_tracing) |
3163 | fprintf (stderr, | |
3164 | "Disconnected tracing in effect, " | |
3165 | "leaving gdbserver attached to the process\n"); | |
3166 | ||
3167 | if (any_persistent_commands ()) | |
3168 | fprintf (stderr, | |
3169 | "Persistent commands are present, " | |
3170 | "leaving gdbserver attached to the process\n"); | |
8336d594 PA |
3171 | |
3172 | /* Make sure we're in non-stop/async mode, so we we can both | |
3173 | wait for an async socket accept, and handle async target | |
3174 | events simultaneously. There's also no point either in | |
3175 | having the target stop all threads, when we're going to | |
3176 | pass signals down without informing GDB. */ | |
3177 | if (!non_stop) | |
3178 | { | |
3179 | if (debug_threads) | |
3180 | fprintf (stderr, "Forcing non-stop mode\n"); | |
3181 | ||
3182 | non_stop = 1; | |
3183 | start_non_stop (1); | |
3184 | } | |
3185 | ||
3186 | process->gdb_detached = 1; | |
3187 | ||
3188 | /* Detaching implicitly resumes all threads. */ | |
3189 | resume_info.thread = minus_one_ptid; | |
3190 | resume_info.kind = resume_continue; | |
3191 | resume_info.sig = 0; | |
3192 | (*the_target->resume) (&resume_info, 1); | |
3193 | ||
3194 | write_ok (own_buf); | |
3195 | break; /* from switch/case */ | |
3196 | } | |
3197 | ||
95954743 | 3198 | fprintf (stderr, "Detaching from process %d\n", pid); |
8336d594 | 3199 | stop_tracing (); |
95954743 | 3200 | if (detach_inferior (pid) != 0) |
bd99dc85 PA |
3201 | write_enn (own_buf); |
3202 | else | |
3203 | { | |
95954743 | 3204 | discard_queued_stop_replies (pid); |
bd99dc85 PA |
3205 | write_ok (own_buf); |
3206 | ||
3207 | if (extended_protocol) | |
c906108c | 3208 | { |
bd99dc85 PA |
3209 | /* Treat this like a normal program exit. */ |
3210 | last_status.kind = TARGET_WAITKIND_EXITED; | |
3211 | last_status.value.integer = 0; | |
95954743 | 3212 | last_ptid = pid_to_ptid (pid); |
2d717e4f | 3213 | |
bd99dc85 PA |
3214 | current_inferior = NULL; |
3215 | } | |
3216 | else | |
3217 | { | |
3218 | putpkt (own_buf); | |
3219 | remote_close (); | |
3220 | ||
3221 | /* If we are attached, then we can exit. Otherwise, we | |
3222 | need to hang around doing nothing, until the child is | |
3223 | gone. */ | |
71f55dd8 | 3224 | join_inferior (pid); |
bd99dc85 PA |
3225 | exit (0); |
3226 | } | |
3227 | } | |
3228 | break; | |
3229 | case '!': | |
3230 | extended_protocol = 1; | |
3231 | write_ok (own_buf); | |
3232 | break; | |
3233 | case '?': | |
3234 | handle_status (own_buf); | |
3235 | break; | |
3236 | case 'H': | |
3237 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') | |
3238 | { | |
95954743 PA |
3239 | ptid_t gdb_id, thread_id; |
3240 | int pid; | |
bd99dc85 PA |
3241 | |
3242 | require_running (own_buf); | |
95954743 PA |
3243 | |
3244 | gdb_id = read_ptid (&own_buf[2], NULL); | |
3245 | ||
3246 | pid = ptid_get_pid (gdb_id); | |
3247 | ||
3248 | if (ptid_equal (gdb_id, null_ptid) | |
3249 | || ptid_equal (gdb_id, minus_one_ptid)) | |
3250 | thread_id = null_ptid; | |
3251 | else if (pid != 0 | |
3252 | && ptid_equal (pid_to_ptid (pid), | |
3253 | gdb_id)) | |
3254 | { | |
3255 | struct thread_info *thread = | |
3256 | (struct thread_info *) find_inferior (&all_threads, | |
3257 | first_thread_of, | |
3258 | &pid); | |
3259 | if (!thread) | |
3260 | { | |
3261 | write_enn (own_buf); | |
3262 | break; | |
3263 | } | |
3264 | ||
3265 | thread_id = ((struct inferior_list_entry *)thread)->id; | |
3266 | } | |
bd99dc85 PA |
3267 | else |
3268 | { | |
3269 | thread_id = gdb_id_to_thread_id (gdb_id); | |
95954743 | 3270 | if (ptid_equal (thread_id, null_ptid)) |
c906108c | 3271 | { |
a06660f7 | 3272 | write_enn (own_buf); |
c906108c SS |
3273 | break; |
3274 | } | |
c906108c SS |
3275 | } |
3276 | ||
bd99dc85 | 3277 | if (own_buf[1] == 'g') |
c906108c | 3278 | { |
95954743 | 3279 | if (ptid_equal (thread_id, null_ptid)) |
c906108c | 3280 | { |
bd99dc85 PA |
3281 | /* GDB is telling us to choose any thread. Check if |
3282 | the currently selected thread is still valid. If | |
3283 | it is not, select the first available. */ | |
3284 | struct thread_info *thread = | |
3285 | (struct thread_info *) find_inferior_id (&all_threads, | |
3286 | general_thread); | |
3287 | if (thread == NULL) | |
3288 | thread_id = all_threads.head->id; | |
c906108c | 3289 | } |
bd99dc85 PA |
3290 | |
3291 | general_thread = thread_id; | |
3292 | set_desired_inferior (1); | |
c906108c | 3293 | } |
bd99dc85 PA |
3294 | else if (own_buf[1] == 'c') |
3295 | cont_thread = thread_id; | |
c906108c | 3296 | |
bd99dc85 PA |
3297 | write_ok (own_buf); |
3298 | } | |
3299 | else | |
3300 | { | |
3301 | /* Silently ignore it so that gdb can extend the protocol | |
3302 | without compatibility headaches. */ | |
3303 | own_buf[0] = '\0'; | |
2d717e4f | 3304 | } |
bd99dc85 PA |
3305 | break; |
3306 | case 'g': | |
219f2f23 PA |
3307 | require_running (own_buf); |
3308 | if (current_traceframe >= 0) | |
3309 | { | |
3aee8918 PA |
3310 | struct regcache *regcache |
3311 | = new_register_cache (current_target_desc ()); | |
219f2f23 PA |
3312 | |
3313 | if (fetch_traceframe_registers (current_traceframe, | |
3314 | regcache, -1) == 0) | |
3315 | registers_to_string (regcache, own_buf); | |
3316 | else | |
3317 | write_enn (own_buf); | |
3318 | free_register_cache (regcache); | |
3319 | } | |
3320 | else | |
3321 | { | |
3322 | struct regcache *regcache; | |
3323 | ||
3324 | set_desired_inferior (1); | |
3325 | regcache = get_thread_regcache (current_inferior, 1); | |
3326 | registers_to_string (regcache, own_buf); | |
3327 | } | |
bd99dc85 PA |
3328 | break; |
3329 | case 'G': | |
219f2f23 PA |
3330 | require_running (own_buf); |
3331 | if (current_traceframe >= 0) | |
3332 | write_enn (own_buf); | |
3333 | else | |
3334 | { | |
442ea881 PA |
3335 | struct regcache *regcache; |
3336 | ||
442ea881 PA |
3337 | set_desired_inferior (1); |
3338 | regcache = get_thread_regcache (current_inferior, 1); | |
3339 | registers_from_string (regcache, &own_buf[1]); | |
3340 | write_ok (own_buf); | |
3341 | } | |
bd99dc85 PA |
3342 | break; |
3343 | case 'm': | |
3344 | require_running (own_buf); | |
3345 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
764880b7 PA |
3346 | res = gdb_read_memory (mem_addr, mem_buf, len); |
3347 | if (res < 0) | |
bd99dc85 | 3348 | write_enn (own_buf); |
764880b7 PA |
3349 | else |
3350 | convert_int_to_ascii (mem_buf, own_buf, res); | |
bd99dc85 PA |
3351 | break; |
3352 | case 'M': | |
3353 | require_running (own_buf); | |
fa593d66 | 3354 | decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf); |
90d74c30 | 3355 | if (gdb_write_memory (mem_addr, mem_buf, len) == 0) |
bd99dc85 PA |
3356 | write_ok (own_buf); |
3357 | else | |
3358 | write_enn (own_buf); | |
3359 | break; | |
3360 | case 'X': | |
3361 | require_running (own_buf); | |
3362 | if (decode_X_packet (&own_buf[1], packet_len - 1, | |
fa593d66 | 3363 | &mem_addr, &len, &mem_buf) < 0 |
90d74c30 | 3364 | || gdb_write_memory (mem_addr, mem_buf, len) != 0) |
bd99dc85 PA |
3365 | write_enn (own_buf); |
3366 | else | |
3367 | write_ok (own_buf); | |
3368 | break; | |
3369 | case 'C': | |
3370 | require_running (own_buf); | |
3371 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
2ea28649 PA |
3372 | if (gdb_signal_to_host_p (sig)) |
3373 | signal = gdb_signal_to_host (sig); | |
bd99dc85 PA |
3374 | else |
3375 | signal = 0; | |
3376 | myresume (own_buf, 0, signal); | |
3377 | break; | |
3378 | case 'S': | |
3379 | require_running (own_buf); | |
3380 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
2ea28649 PA |
3381 | if (gdb_signal_to_host_p (sig)) |
3382 | signal = gdb_signal_to_host (sig); | |
bd99dc85 PA |
3383 | else |
3384 | signal = 0; | |
3385 | myresume (own_buf, 1, signal); | |
3386 | break; | |
3387 | case 'c': | |
3388 | require_running (own_buf); | |
3389 | signal = 0; | |
3390 | myresume (own_buf, 0, signal); | |
3391 | break; | |
3392 | case 's': | |
3393 | require_running (own_buf); | |
3394 | signal = 0; | |
3395 | myresume (own_buf, 1, signal); | |
3396 | break; | |
c6314022 AR |
3397 | case 'Z': /* insert_ ... */ |
3398 | /* Fallthrough. */ | |
3399 | case 'z': /* remove_ ... */ | |
bd99dc85 | 3400 | { |
bd99dc85 | 3401 | char *dataptr; |
aca22551 PA |
3402 | ULONGEST addr; |
3403 | int len; | |
bd99dc85 | 3404 | char type = own_buf[1]; |
c6314022 | 3405 | int res; |
d993e290 | 3406 | const int insert = ch == 'Z'; |
aca22551 PA |
3407 | char *p = &own_buf[3]; |
3408 | ||
3409 | p = unpack_varlen_hex (p, &addr); | |
3410 | len = strtol (p + 1, &dataptr, 16); | |
c6314022 | 3411 | |
d993e290 PA |
3412 | /* Default to unrecognized/unsupported. */ |
3413 | res = 1; | |
3414 | switch (type) | |
3415 | { | |
3416 | case '0': /* software-breakpoint */ | |
3417 | case '1': /* hardware-breakpoint */ | |
3418 | case '2': /* write watchpoint */ | |
3419 | case '3': /* read watchpoint */ | |
3420 | case '4': /* access watchpoint */ | |
3421 | require_running (own_buf); | |
3422 | if (insert && the_target->insert_point != NULL) | |
9f3a5c85 LM |
3423 | { |
3424 | /* Insert the breakpoint. If it is already inserted, nothing | |
3425 | will take place. */ | |
3426 | res = (*the_target->insert_point) (type, addr, len); | |
3427 | ||
3428 | /* GDB may have sent us a list of *point parameters to be | |
3429 | evaluated on the target's side. Read such list here. If we | |
3430 | already have a list of parameters, GDB is telling us to drop | |
3431 | that list and use this one instead. */ | |
3432 | if (!res && (type == '0' || type == '1')) | |
3433 | { | |
3434 | /* Remove previous conditions. */ | |
3435 | clear_gdb_breakpoint_conditions (addr); | |
3436 | process_point_options (addr, &dataptr); | |
3437 | } | |
3438 | } | |
d993e290 PA |
3439 | else if (!insert && the_target->remove_point != NULL) |
3440 | res = (*the_target->remove_point) (type, addr, len); | |
3441 | break; | |
3442 | default: | |
3443 | break; | |
3444 | } | |
bd99dc85 | 3445 | |
c6314022 AR |
3446 | if (res == 0) |
3447 | write_ok (own_buf); | |
3448 | else if (res == 1) | |
3449 | /* Unsupported. */ | |
3450 | own_buf[0] = '\0'; | |
bd99dc85 | 3451 | else |
c6314022 | 3452 | write_enn (own_buf); |
bd99dc85 PA |
3453 | break; |
3454 | } | |
3455 | case 'k': | |
3456 | response_needed = 0; | |
3457 | if (!target_running ()) | |
95954743 PA |
3458 | /* The packet we received doesn't make sense - but we can't |
3459 | reply to it, either. */ | |
8336d594 | 3460 | return 0; |
c906108c | 3461 | |
95954743 PA |
3462 | fprintf (stderr, "Killing all inferiors\n"); |
3463 | for_each_inferior (&all_processes, kill_inferior_callback); | |
c906108c | 3464 | |
bd99dc85 PA |
3465 | /* When using the extended protocol, we wait with no program |
3466 | running. The traditional protocol will exit instead. */ | |
3467 | if (extended_protocol) | |
3468 | { | |
3469 | last_status.kind = TARGET_WAITKIND_EXITED; | |
a493e3e2 | 3470 | last_status.value.sig = GDB_SIGNAL_KILL; |
8336d594 | 3471 | return 0; |
bd99dc85 PA |
3472 | } |
3473 | else | |
8336d594 PA |
3474 | exit (0); |
3475 | ||
bd99dc85 PA |
3476 | case 'T': |
3477 | { | |
95954743 | 3478 | ptid_t gdb_id, thread_id; |
bd99dc85 PA |
3479 | |
3480 | require_running (own_buf); | |
95954743 PA |
3481 | |
3482 | gdb_id = read_ptid (&own_buf[1], NULL); | |
bd99dc85 | 3483 | thread_id = gdb_id_to_thread_id (gdb_id); |
95954743 | 3484 | if (ptid_equal (thread_id, null_ptid)) |
bd99dc85 PA |
3485 | { |
3486 | write_enn (own_buf); | |
3487 | break; | |
3488 | } | |
3489 | ||
3490 | if (mythread_alive (thread_id)) | |
3491 | write_ok (own_buf); | |
3492 | else | |
3493 | write_enn (own_buf); | |
3494 | } | |
3495 | break; | |
3496 | case 'R': | |
3497 | response_needed = 0; | |
3498 | ||
3499 | /* Restarting the inferior is only supported in the extended | |
3500 | protocol. */ | |
3501 | if (extended_protocol) | |
3502 | { | |
3503 | if (target_running ()) | |
95954743 PA |
3504 | for_each_inferior (&all_processes, |
3505 | kill_inferior_callback); | |
bd99dc85 PA |
3506 | fprintf (stderr, "GDBserver restarting\n"); |
3507 | ||
3508 | /* Wait till we are at 1st instruction in prog. */ | |
3509 | if (program_argv != NULL) | |
3510 | start_inferior (program_argv); | |
3511 | else | |
3512 | { | |
3513 | last_status.kind = TARGET_WAITKIND_EXITED; | |
a493e3e2 | 3514 | last_status.value.sig = GDB_SIGNAL_KILL; |
bd99dc85 | 3515 | } |
8336d594 | 3516 | return 0; |
c906108c SS |
3517 | } |
3518 | else | |
3519 | { | |
bd99dc85 PA |
3520 | /* It is a request we don't understand. Respond with an |
3521 | empty packet so that gdb knows that we don't support this | |
3522 | request. */ | |
3523 | own_buf[0] = '\0'; | |
3524 | break; | |
3525 | } | |
3526 | case 'v': | |
3527 | /* Extended (long) request. */ | |
3528 | handle_v_requests (own_buf, packet_len, &new_packet_len); | |
3529 | break; | |
3530 | ||
3531 | default: | |
3532 | /* It is a request we don't understand. Respond with an empty | |
3533 | packet so that gdb knows that we don't support this | |
3534 | request. */ | |
3535 | own_buf[0] = '\0'; | |
3536 | break; | |
3537 | } | |
3538 | ||
3539 | if (new_packet_len != -1) | |
3540 | putpkt_binary (own_buf, new_packet_len); | |
3541 | else | |
3542 | putpkt (own_buf); | |
3543 | ||
3544 | response_needed = 0; | |
3545 | ||
3546 | if (!extended_protocol && have_ran && !target_running ()) | |
3547 | { | |
3548 | /* In non-stop, defer exiting until GDB had a chance to query | |
3549 | the whole vStopped list (until it gets an OK). */ | |
14a00470 | 3550 | if (QUEUE_is_empty (notif_event_p, notif_stop.queue)) |
bd99dc85 PA |
3551 | { |
3552 | fprintf (stderr, "GDBserver exiting\n"); | |
c906108c | 3553 | remote_close (); |
bd99dc85 | 3554 | exit (0); |
c906108c SS |
3555 | } |
3556 | } | |
8336d594 PA |
3557 | |
3558 | if (exit_requested) | |
3559 | return -1; | |
3560 | ||
3561 | return 0; | |
c906108c | 3562 | } |
bd99dc85 PA |
3563 | |
3564 | /* Event-loop callback for serial events. */ | |
3565 | ||
8336d594 | 3566 | int |
bd99dc85 PA |
3567 | handle_serial_event (int err, gdb_client_data client_data) |
3568 | { | |
3569 | if (debug_threads) | |
3570 | fprintf (stderr, "handling possible serial event\n"); | |
3571 | ||
3572 | /* Really handle it. */ | |
8336d594 PA |
3573 | if (process_serial_event () < 0) |
3574 | return -1; | |
bd99dc85 PA |
3575 | |
3576 | /* Be sure to not change the selected inferior behind GDB's back. | |
3577 | Important in the non-stop mode asynchronous protocol. */ | |
3578 | set_desired_inferior (1); | |
8336d594 PA |
3579 | |
3580 | return 0; | |
bd99dc85 PA |
3581 | } |
3582 | ||
3583 | /* Event-loop callback for target events. */ | |
3584 | ||
8336d594 | 3585 | int |
bd99dc85 PA |
3586 | handle_target_event (int err, gdb_client_data client_data) |
3587 | { | |
3588 | if (debug_threads) | |
3589 | fprintf (stderr, "handling possible target event\n"); | |
3590 | ||
95954743 PA |
3591 | last_ptid = mywait (minus_one_ptid, &last_status, |
3592 | TARGET_WNOHANG, 1); | |
bd99dc85 PA |
3593 | |
3594 | if (last_status.kind != TARGET_WAITKIND_IGNORE) | |
3595 | { | |
8336d594 PA |
3596 | int pid = ptid_get_pid (last_ptid); |
3597 | struct process_info *process = find_process_pid (pid); | |
3598 | int forward_event = !gdb_connected () || process->gdb_detached; | |
3599 | ||
3600 | if (last_status.kind == TARGET_WAITKIND_EXITED | |
3601 | || last_status.kind == TARGET_WAITKIND_SIGNALLED) | |
f9e39928 PA |
3602 | { |
3603 | mark_breakpoints_out (process); | |
3604 | mourn_inferior (process); | |
3605 | } | |
ce1a5b52 | 3606 | else |
d20a8ad9 PA |
3607 | { |
3608 | /* We're reporting this thread as stopped. Update its | |
3609 | "want-stopped" state to what the client wants, until it | |
3610 | gets a new resume action. */ | |
3611 | current_inferior->last_resume_kind = resume_stop; | |
3612 | current_inferior->last_status = last_status; | |
3613 | } | |
8336d594 PA |
3614 | |
3615 | if (forward_event) | |
3616 | { | |
3617 | if (!target_running ()) | |
3618 | { | |
3619 | /* The last process exited. We're done. */ | |
3620 | exit (0); | |
3621 | } | |
3622 | ||
3623 | if (last_status.kind == TARGET_WAITKIND_STOPPED) | |
3624 | { | |
3625 | /* A thread stopped with a signal, but gdb isn't | |
3626 | connected to handle it. Pass it down to the | |
3627 | inferior, as if it wasn't being traced. */ | |
3628 | struct thread_resume resume_info; | |
3629 | ||
3630 | if (debug_threads) | |
3631 | fprintf (stderr, | |
3632 | "GDB not connected; forwarding event %d for [%s]\n", | |
3633 | (int) last_status.kind, | |
3634 | target_pid_to_str (last_ptid)); | |
3635 | ||
3636 | resume_info.thread = last_ptid; | |
3637 | resume_info.kind = resume_continue; | |
2ea28649 | 3638 | resume_info.sig = gdb_signal_to_host (last_status.value.sig); |
8336d594 PA |
3639 | (*the_target->resume) (&resume_info, 1); |
3640 | } | |
3641 | else if (debug_threads) | |
3642 | fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n", | |
3643 | (int) last_status.kind, | |
3644 | target_pid_to_str (last_ptid)); | |
3645 | } | |
3646 | else | |
3647 | { | |
14a00470 YQ |
3648 | struct vstop_notif *vstop_notif |
3649 | = xmalloc (sizeof (struct vstop_notif)); | |
3650 | ||
3651 | vstop_notif->status = last_status; | |
3652 | vstop_notif->ptid = last_ptid; | |
3653 | /* Push Stop notification. */ | |
3654 | notif_push (¬if_stop, | |
3655 | (struct notif_event *) vstop_notif); | |
8336d594 | 3656 | } |
bd99dc85 PA |
3657 | } |
3658 | ||
3659 | /* Be sure to not change the selected inferior behind GDB's back. | |
3660 | Important in the non-stop mode asynchronous protocol. */ | |
3661 | set_desired_inferior (1); | |
8336d594 PA |
3662 | |
3663 | return 0; | |
bd99dc85 | 3664 | } |