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