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