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