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