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