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