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