Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
6aba47ca DJ |
2 | Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, |
3 | 2004, 2005, 2006, 2007 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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
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 JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
6f0f660e EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
21 | |
22 | #include "server.h" | |
23 | ||
68070c10 | 24 | #if HAVE_UNISTD_H |
a9fa9f7d | 25 | #include <unistd.h> |
68070c10 PA |
26 | #endif |
27 | #if HAVE_SIGNAL_H | |
a9fa9f7d | 28 | #include <signal.h> |
68070c10 | 29 | #endif |
b80864fb | 30 | #if HAVE_SYS_WAIT_H |
a9fa9f7d | 31 | #include <sys/wait.h> |
b80864fb | 32 | #endif |
a9fa9f7d | 33 | |
a1928bad DJ |
34 | unsigned long cont_thread; |
35 | unsigned long general_thread; | |
36 | unsigned long step_thread; | |
37 | unsigned long thread_from_wait; | |
38 | unsigned long old_thread_from_wait; | |
c906108c | 39 | int extended_protocol; |
0d62e5e8 DJ |
40 | int server_waiting; |
41 | ||
c74d0ad8 DJ |
42 | /* Enable miscellaneous debugging output. The name is historical - it |
43 | was originally used to debug LinuxThreads support. */ | |
44 | int debug_threads; | |
45 | ||
89be2091 DJ |
46 | int pass_signals[TARGET_SIGNAL_LAST]; |
47 | ||
c906108c | 48 | jmp_buf toplevel; |
c906108c | 49 | |
a9fa9f7d DJ |
50 | /* The PID of the originally created or attached inferior. Used to |
51 | send signals to the process when GDB sends us an asynchronous interrupt | |
52 | (user hitting Control-C in the client), and to wait for the child to exit | |
53 | when no longer debugging it. */ | |
54 | ||
a1928bad | 55 | unsigned long signal_pid; |
a9fa9f7d | 56 | |
290fadea RS |
57 | #ifdef SIGTTOU |
58 | /* A file descriptor for the controlling terminal. */ | |
59 | int terminal_fd; | |
60 | ||
61 | /* TERMINAL_FD's original foreground group. */ | |
62 | pid_t old_foreground_pgrp; | |
63 | ||
64 | /* Hand back terminal ownership to the original foreground group. */ | |
65 | ||
66 | static void | |
67 | restore_old_foreground_pgrp (void) | |
68 | { | |
69 | tcsetpgrp (terminal_fd, old_foreground_pgrp); | |
70 | } | |
71 | #endif | |
72 | ||
fc620387 | 73 | static int |
da85418c | 74 | start_inferior (char *argv[], char *statusptr) |
c906108c | 75 | { |
b80864fb | 76 | #ifdef SIGTTOU |
a9fa9f7d DJ |
77 | signal (SIGTTOU, SIG_DFL); |
78 | signal (SIGTTIN, SIG_DFL); | |
b80864fb | 79 | #endif |
a9fa9f7d DJ |
80 | |
81 | signal_pid = create_inferior (argv[0], argv); | |
0d62e5e8 | 82 | |
a1928bad | 83 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
a9fa9f7d | 84 | signal_pid); |
b80864fb | 85 | fflush (stderr); |
a9fa9f7d | 86 | |
b80864fb | 87 | #ifdef SIGTTOU |
a9fa9f7d DJ |
88 | signal (SIGTTOU, SIG_IGN); |
89 | signal (SIGTTIN, SIG_IGN); | |
290fadea RS |
90 | terminal_fd = fileno (stderr); |
91 | old_foreground_pgrp = tcgetpgrp (terminal_fd); | |
92 | tcsetpgrp (terminal_fd, signal_pid); | |
93 | atexit (restore_old_foreground_pgrp); | |
b80864fb | 94 | #endif |
c906108c SS |
95 | |
96 | /* Wait till we are at 1st instruction in program, return signal number. */ | |
0d62e5e8 | 97 | return mywait (statusptr, 0); |
c906108c SS |
98 | } |
99 | ||
45b7b345 | 100 | static int |
fc620387 | 101 | attach_inferior (int pid, char *statusptr, int *sigptr) |
45b7b345 DJ |
102 | { |
103 | /* myattach should return -1 if attaching is unsupported, | |
104 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 105 | |
45b7b345 DJ |
106 | if (myattach (pid) != 0) |
107 | return -1; | |
108 | ||
6910d122 | 109 | fprintf (stderr, "Attached; pid = %d\n", pid); |
b80864fb | 110 | fflush (stderr); |
6910d122 | 111 | |
a9fa9f7d DJ |
112 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
113 | attach function, so that it can be the main thread instead of | |
114 | whichever we were told to attach to. */ | |
115 | signal_pid = pid; | |
116 | ||
0d62e5e8 | 117 | *sigptr = mywait (statusptr, 0); |
45b7b345 | 118 | |
9db87ebd DJ |
119 | /* GDB knows to ignore the first SIGSTOP after attaching to a running |
120 | process using the "attach" command, but this is different; it's | |
121 | just using "target remote". Pretend it's just starting up. */ | |
b80864fb DJ |
122 | if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP) |
123 | *sigptr = TARGET_SIGNAL_TRAP; | |
9db87ebd | 124 | |
45b7b345 DJ |
125 | return 0; |
126 | } | |
127 | ||
c906108c | 128 | extern int remote_debug; |
ce3a066d | 129 | |
0876f84a DJ |
130 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
131 | or -1 otherwise. */ | |
132 | ||
133 | static int | |
134 | decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len) | |
135 | { | |
136 | /* Extract and NUL-terminate the annex. */ | |
137 | *annex = buf; | |
138 | while (*buf && *buf != ':') | |
139 | buf++; | |
140 | if (*buf == '\0') | |
141 | return -1; | |
142 | *buf++ = 0; | |
143 | ||
144 | /* After the read/write marker and annex, qXfer looks like a | |
145 | traditional 'm' packet. */ | |
146 | decode_m_packet (buf, ofs, len); | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | /* Write the response to a successful qXfer read. Returns the | |
152 | length of the (binary) data stored in BUF, corresponding | |
153 | to as much of DATA/LEN as we could fit. IS_MORE controls | |
154 | the first character of the response. */ | |
155 | static int | |
23181151 | 156 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
0876f84a DJ |
157 | { |
158 | int out_len; | |
159 | ||
160 | if (is_more) | |
161 | buf[0] = 'm'; | |
162 | else | |
163 | buf[0] = 'l'; | |
164 | ||
165 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, | |
166 | PBUFSIZ - 2) + 1; | |
167 | } | |
168 | ||
89be2091 DJ |
169 | /* Handle all of the extended 'Q' packets. */ |
170 | void | |
171 | handle_general_set (char *own_buf) | |
172 | { | |
173 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) | |
174 | { | |
175 | int numsigs = (int) TARGET_SIGNAL_LAST, i; | |
176 | const char *p = own_buf + strlen ("QPassSignals:"); | |
177 | CORE_ADDR cursig; | |
178 | ||
179 | p = decode_address_to_semicolon (&cursig, p); | |
180 | for (i = 0; i < numsigs; i++) | |
181 | { | |
182 | if (i == cursig) | |
183 | { | |
184 | pass_signals[i] = 1; | |
185 | if (*p == '\0') | |
186 | /* Keep looping, to clear the remaining signals. */ | |
187 | cursig = -1; | |
188 | else | |
189 | p = decode_address_to_semicolon (&cursig, p); | |
190 | } | |
191 | else | |
192 | pass_signals[i] = 0; | |
193 | } | |
194 | strcpy (own_buf, "OK"); | |
195 | return; | |
196 | } | |
197 | ||
198 | /* Otherwise we didn't know what packet it was. Say we didn't | |
199 | understand it. */ | |
200 | own_buf[0] = 0; | |
201 | } | |
202 | ||
23181151 | 203 | static const char * |
fb1e4ffc | 204 | get_features_xml (const char *annex) |
23181151 DJ |
205 | { |
206 | static int features_supported = -1; | |
207 | static char *document; | |
208 | ||
fb1e4ffc DJ |
209 | #ifdef USE_XML |
210 | extern const char *const xml_builtin[][2]; | |
211 | int i; | |
212 | ||
213 | /* Look for the annex. */ | |
214 | for (i = 0; xml_builtin[i][0] != NULL; i++) | |
215 | if (strcmp (annex, xml_builtin[i][0]) == 0) | |
216 | break; | |
217 | ||
218 | if (xml_builtin[i][0] != NULL) | |
219 | return xml_builtin[i][1]; | |
220 | #endif | |
221 | ||
222 | if (strcmp (annex, "target.xml") != 0) | |
223 | return NULL; | |
224 | ||
23181151 DJ |
225 | if (features_supported == -1) |
226 | { | |
820f2bda PA |
227 | const char *arch = NULL; |
228 | if (the_target->arch_string != NULL) | |
229 | arch = (*the_target->arch_string) (); | |
23181151 DJ |
230 | |
231 | if (arch == NULL) | |
232 | features_supported = 0; | |
233 | else | |
234 | { | |
235 | features_supported = 1; | |
236 | document = malloc (64 + strlen (arch)); | |
237 | snprintf (document, 64 + strlen (arch), | |
238 | "<target><architecture>%s</architecture></target>", | |
239 | arch); | |
240 | } | |
241 | } | |
242 | ||
243 | return document; | |
244 | } | |
245 | ||
c74d0ad8 DJ |
246 | void |
247 | monitor_show_help (void) | |
248 | { | |
249 | monitor_output ("The following monitor commands are supported:\n"); | |
250 | monitor_output (" set debug <0|1>\n"); | |
251 | monitor_output (" Enable general debugging messages\n"); | |
252 | monitor_output (" set remote-debug <0|1>\n"); | |
253 | monitor_output (" Enable remote protocol debugging messages\n"); | |
254 | } | |
255 | ||
ce3a066d DJ |
256 | /* Handle all of the extended 'q' packets. */ |
257 | void | |
0876f84a | 258 | handle_query (char *own_buf, int *new_packet_len_p) |
ce3a066d | 259 | { |
0d62e5e8 DJ |
260 | static struct inferior_list_entry *thread_ptr; |
261 | ||
ce3a066d DJ |
262 | if (strcmp ("qSymbol::", own_buf) == 0) |
263 | { | |
2f2893d9 DJ |
264 | if (the_target->look_up_symbols != NULL) |
265 | (*the_target->look_up_symbols) (); | |
266 | ||
ce3a066d DJ |
267 | strcpy (own_buf, "OK"); |
268 | return; | |
269 | } | |
270 | ||
0d62e5e8 DJ |
271 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
272 | { | |
273 | thread_ptr = all_threads.head; | |
a06660f7 | 274 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
275 | thread_ptr = thread_ptr->next; |
276 | return; | |
277 | } | |
aa691b87 | 278 | |
0d62e5e8 DJ |
279 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
280 | { | |
281 | if (thread_ptr != NULL) | |
282 | { | |
a06660f7 | 283 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
284 | thread_ptr = thread_ptr->next; |
285 | return; | |
286 | } | |
287 | else | |
288 | { | |
289 | sprintf (own_buf, "l"); | |
290 | return; | |
291 | } | |
292 | } | |
aa691b87 | 293 | |
52fb6437 NS |
294 | if (the_target->read_offsets != NULL |
295 | && strcmp ("qOffsets", own_buf) == 0) | |
296 | { | |
297 | CORE_ADDR text, data; | |
298 | ||
299 | if (the_target->read_offsets (&text, &data)) | |
300 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
301 | (long)text, (long)data, (long)data); | |
302 | else | |
303 | write_enn (own_buf); | |
304 | ||
305 | return; | |
306 | } | |
307 | ||
aa691b87 | 308 | if (the_target->read_auxv != NULL |
0876f84a | 309 | && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0) |
aa691b87 | 310 | { |
0876f84a DJ |
311 | unsigned char *data; |
312 | int n; | |
aa691b87 RM |
313 | CORE_ADDR ofs; |
314 | unsigned int len; | |
0876f84a DJ |
315 | char *annex; |
316 | ||
317 | /* Reject any annex; grab the offset and length. */ | |
318 | if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0 | |
319 | || annex[0] != '\0') | |
320 | { | |
321 | strcpy (own_buf, "E00"); | |
322 | return; | |
323 | } | |
324 | ||
325 | /* Read one extra byte, as an indicator of whether there is | |
326 | more. */ | |
327 | if (len > PBUFSIZ - 2) | |
328 | len = PBUFSIZ - 2; | |
329 | data = malloc (len + 1); | |
330 | n = (*the_target->read_auxv) (ofs, data, len + 1); | |
000ef4f0 DJ |
331 | if (n < 0) |
332 | write_enn (own_buf); | |
333 | else if (n > len) | |
0876f84a | 334 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
aa691b87 | 335 | else |
0876f84a DJ |
336 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
337 | ||
338 | free (data); | |
339 | ||
aa691b87 RM |
340 | return; |
341 | } | |
342 | ||
23181151 DJ |
343 | if (strncmp ("qXfer:features:read:", own_buf, 20) == 0) |
344 | { | |
345 | CORE_ADDR ofs; | |
346 | unsigned int len, total_len; | |
347 | const char *document; | |
348 | char *annex; | |
349 | ||
fb1e4ffc DJ |
350 | /* Check for support. */ |
351 | document = get_features_xml ("target.xml"); | |
23181151 DJ |
352 | if (document == NULL) |
353 | { | |
354 | own_buf[0] = '\0'; | |
355 | return; | |
356 | } | |
357 | ||
fb1e4ffc DJ |
358 | /* Grab the annex, offset, and length. */ |
359 | if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0) | |
360 | { | |
361 | strcpy (own_buf, "E00"); | |
362 | return; | |
363 | } | |
364 | ||
365 | /* Now grab the correct annex. */ | |
366 | document = get_features_xml (annex); | |
367 | if (document == NULL) | |
23181151 DJ |
368 | { |
369 | strcpy (own_buf, "E00"); | |
370 | return; | |
371 | } | |
372 | ||
373 | total_len = strlen (document); | |
374 | if (len > PBUFSIZ - 2) | |
375 | len = PBUFSIZ - 2; | |
376 | ||
377 | if (ofs > total_len) | |
378 | write_enn (own_buf); | |
379 | else if (len < total_len - ofs) | |
380 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
381 | len, 1); | |
382 | else | |
383 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
384 | total_len - ofs, 0); | |
385 | ||
386 | return; | |
387 | } | |
388 | ||
be2a5f71 DJ |
389 | /* Protocol features query. */ |
390 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
391 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
392 | { | |
89be2091 | 393 | sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1); |
0876f84a DJ |
394 | |
395 | if (the_target->read_auxv != NULL) | |
9f2e1e63 | 396 | strcat (own_buf, ";qXfer:auxv:read+"); |
0876f84a | 397 | |
fb1e4ffc | 398 | if (get_features_xml ("target.xml") != NULL) |
23181151 DJ |
399 | strcat (own_buf, ";qXfer:features:read+"); |
400 | ||
be2a5f71 DJ |
401 | return; |
402 | } | |
403 | ||
dae5f5cf DJ |
404 | /* Thread-local storage support. */ |
405 | if (the_target->get_tls_address != NULL | |
406 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) | |
407 | { | |
408 | char *p = own_buf + 12; | |
409 | CORE_ADDR parts[3], address = 0; | |
410 | int i, err; | |
411 | ||
412 | for (i = 0; i < 3; i++) | |
413 | { | |
414 | char *p2; | |
415 | int len; | |
416 | ||
417 | if (p == NULL) | |
418 | break; | |
419 | ||
420 | p2 = strchr (p, ','); | |
421 | if (p2) | |
422 | { | |
423 | len = p2 - p; | |
424 | p2++; | |
425 | } | |
426 | else | |
427 | { | |
428 | len = strlen (p); | |
429 | p2 = NULL; | |
430 | } | |
431 | ||
432 | decode_address (&parts[i], p, len); | |
433 | p = p2; | |
434 | } | |
435 | ||
436 | if (p != NULL || i < 3) | |
437 | err = 1; | |
438 | else | |
439 | { | |
440 | struct thread_info *thread = gdb_id_to_thread (parts[0]); | |
441 | ||
442 | if (thread == NULL) | |
443 | err = 2; | |
444 | else | |
445 | err = the_target->get_tls_address (thread, parts[1], parts[2], | |
446 | &address); | |
447 | } | |
448 | ||
449 | if (err == 0) | |
450 | { | |
451 | sprintf (own_buf, "%llx", address); | |
452 | return; | |
453 | } | |
454 | else if (err > 0) | |
455 | { | |
456 | write_enn (own_buf); | |
457 | return; | |
458 | } | |
459 | ||
460 | /* Otherwise, pretend we do not understand this packet. */ | |
461 | } | |
462 | ||
c74d0ad8 DJ |
463 | /* Handle "monitor" commands. */ |
464 | if (strncmp ("qRcmd,", own_buf, 6) == 0) | |
465 | { | |
466 | char *mon = malloc (PBUFSIZ); | |
467 | int len = strlen (own_buf + 6); | |
468 | ||
469 | if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) | |
470 | { | |
471 | write_enn (own_buf); | |
472 | free (mon); | |
473 | return; | |
474 | } | |
475 | mon[len / 2] = '\0'; | |
476 | ||
477 | write_ok (own_buf); | |
478 | ||
479 | if (strcmp (mon, "set debug 1") == 0) | |
480 | { | |
481 | debug_threads = 1; | |
482 | monitor_output ("Debug output enabled.\n"); | |
483 | } | |
484 | else if (strcmp (mon, "set debug 0") == 0) | |
485 | { | |
486 | debug_threads = 0; | |
487 | monitor_output ("Debug output disabled.\n"); | |
488 | } | |
489 | else if (strcmp (mon, "set remote-debug 1") == 0) | |
490 | { | |
491 | remote_debug = 1; | |
492 | monitor_output ("Protocol debug output enabled.\n"); | |
493 | } | |
494 | else if (strcmp (mon, "set remote-debug 0") == 0) | |
495 | { | |
496 | remote_debug = 0; | |
497 | monitor_output ("Protocol debug output disabled.\n"); | |
498 | } | |
499 | else if (strcmp (mon, "help") == 0) | |
500 | monitor_show_help (); | |
501 | else | |
502 | { | |
503 | monitor_output ("Unknown monitor command.\n\n"); | |
504 | monitor_show_help (); | |
505 | write_enn (own_buf); | |
506 | } | |
507 | ||
508 | free (mon); | |
509 | return; | |
510 | } | |
511 | ||
ce3a066d DJ |
512 | /* Otherwise we didn't know what packet it was. Say we didn't |
513 | understand it. */ | |
514 | own_buf[0] = 0; | |
515 | } | |
516 | ||
64386c31 DJ |
517 | /* Parse vCont packets. */ |
518 | void | |
fc620387 | 519 | handle_v_cont (char *own_buf, char *status, int *signal) |
64386c31 DJ |
520 | { |
521 | char *p, *q; | |
522 | int n = 0, i = 0; | |
523 | struct thread_resume *resume_info, default_action; | |
524 | ||
525 | /* Count the number of semicolons in the packet. There should be one | |
526 | for every action. */ | |
527 | p = &own_buf[5]; | |
528 | while (p) | |
529 | { | |
530 | n++; | |
531 | p++; | |
532 | p = strchr (p, ';'); | |
533 | } | |
534 | /* Allocate room for one extra action, for the default remain-stopped | |
535 | behavior; if no default action is in the list, we'll need the extra | |
536 | slot. */ | |
537 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
538 | ||
539 | default_action.thread = -1; | |
540 | default_action.leave_stopped = 1; | |
541 | default_action.step = 0; | |
542 | default_action.sig = 0; | |
543 | ||
544 | p = &own_buf[5]; | |
545 | i = 0; | |
546 | while (*p) | |
547 | { | |
548 | p++; | |
549 | ||
550 | resume_info[i].leave_stopped = 0; | |
551 | ||
552 | if (p[0] == 's' || p[0] == 'S') | |
553 | resume_info[i].step = 1; | |
554 | else if (p[0] == 'c' || p[0] == 'C') | |
555 | resume_info[i].step = 0; | |
556 | else | |
557 | goto err; | |
558 | ||
559 | if (p[0] == 'S' || p[0] == 'C') | |
560 | { | |
561 | int sig; | |
562 | sig = strtol (p + 1, &q, 16); | |
563 | if (p == q) | |
564 | goto err; | |
565 | p = q; | |
566 | ||
567 | if (!target_signal_to_host_p (sig)) | |
568 | goto err; | |
569 | resume_info[i].sig = target_signal_to_host (sig); | |
570 | } | |
571 | else | |
572 | { | |
573 | resume_info[i].sig = 0; | |
574 | p = p + 1; | |
575 | } | |
576 | ||
577 | if (p[0] == 0) | |
578 | { | |
579 | resume_info[i].thread = -1; | |
580 | default_action = resume_info[i]; | |
581 | ||
582 | /* Note: we don't increment i here, we'll overwrite this entry | |
583 | the next time through. */ | |
584 | } | |
585 | else if (p[0] == ':') | |
586 | { | |
a06660f7 DJ |
587 | unsigned int gdb_id = strtoul (p + 1, &q, 16); |
588 | unsigned long thread_id; | |
589 | ||
64386c31 DJ |
590 | if (p == q) |
591 | goto err; | |
592 | p = q; | |
593 | if (p[0] != ';' && p[0] != 0) | |
594 | goto err; | |
595 | ||
a06660f7 DJ |
596 | thread_id = gdb_id_to_thread_id (gdb_id); |
597 | if (thread_id) | |
598 | resume_info[i].thread = thread_id; | |
599 | else | |
600 | goto err; | |
601 | ||
64386c31 DJ |
602 | i++; |
603 | } | |
604 | } | |
605 | ||
606 | resume_info[i] = default_action; | |
607 | ||
608 | /* Still used in occasional places in the backend. */ | |
609 | if (n == 1 && resume_info[0].thread != -1) | |
610 | cont_thread = resume_info[0].thread; | |
611 | else | |
612 | cont_thread = -1; | |
dc3f8883 | 613 | set_desired_inferior (0); |
64386c31 DJ |
614 | |
615 | (*the_target->resume) (resume_info); | |
616 | ||
617 | free (resume_info); | |
618 | ||
619 | *signal = mywait (status, 1); | |
620 | prepare_resume_reply (own_buf, *status, *signal); | |
621 | return; | |
622 | ||
623 | err: | |
624 | /* No other way to report an error... */ | |
625 | strcpy (own_buf, ""); | |
626 | free (resume_info); | |
627 | return; | |
628 | } | |
629 | ||
630 | /* Handle all of the extended 'v' packets. */ | |
631 | void | |
fc620387 | 632 | handle_v_requests (char *own_buf, char *status, int *signal) |
64386c31 DJ |
633 | { |
634 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
635 | { | |
636 | handle_v_cont (own_buf, status, signal); | |
637 | return; | |
638 | } | |
639 | ||
640 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
641 | { | |
642 | strcpy (own_buf, "vCont;c;C;s;S"); | |
643 | return; | |
644 | } | |
645 | ||
646 | /* Otherwise we didn't know what packet it was. Say we didn't | |
647 | understand it. */ | |
648 | own_buf[0] = 0; | |
649 | return; | |
650 | } | |
651 | ||
652 | void | |
653 | myresume (int step, int sig) | |
654 | { | |
655 | struct thread_resume resume_info[2]; | |
656 | int n = 0; | |
657 | ||
d592fa2f | 658 | if (step || sig || (cont_thread != 0 && cont_thread != -1)) |
64386c31 DJ |
659 | { |
660 | resume_info[0].thread | |
661 | = ((struct inferior_list_entry *) current_inferior)->id; | |
662 | resume_info[0].step = step; | |
663 | resume_info[0].sig = sig; | |
664 | resume_info[0].leave_stopped = 0; | |
665 | n++; | |
666 | } | |
667 | resume_info[n].thread = -1; | |
668 | resume_info[n].step = 0; | |
669 | resume_info[n].sig = 0; | |
d592fa2f | 670 | resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); |
64386c31 DJ |
671 | |
672 | (*the_target->resume) (resume_info); | |
673 | } | |
674 | ||
0729219d | 675 | static int attached; |
c906108c | 676 | |
dd24457d DJ |
677 | static void |
678 | gdbserver_version (void) | |
679 | { | |
680 | printf ("GNU gdbserver %s\n" | |
681 | "Copyright (C) 2006 Free Software Foundation, Inc.\n" | |
682 | "gdbserver is free software, covered by the GNU General Public License.\n" | |
683 | "This gdbserver was configured as \"%s\"\n", | |
684 | version, host_name); | |
685 | } | |
686 | ||
0bc68c49 DJ |
687 | static void |
688 | gdbserver_usage (void) | |
689 | { | |
dd24457d DJ |
690 | printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" |
691 | "\tgdbserver COMM --attach PID\n" | |
692 | "\n" | |
693 | "COMM may either be a tty device (for serial debugging), or \n" | |
694 | "HOST:PORT to listen for a TCP connection.\n"); | |
0bc68c49 DJ |
695 | } |
696 | ||
c906108c | 697 | int |
da85418c | 698 | main (int argc, char *argv[]) |
c906108c | 699 | { |
f450004a | 700 | char ch, status, *own_buf; |
7fb85e41 | 701 | unsigned char *mem_buf; |
c906108c | 702 | int i = 0; |
fc620387 | 703 | int signal; |
c906108c SS |
704 | unsigned int len; |
705 | CORE_ADDR mem_addr; | |
0729219d DJ |
706 | int bad_attach; |
707 | int pid; | |
45b7b345 | 708 | char *arg_end; |
c906108c | 709 | |
dd24457d DJ |
710 | if (argc >= 2 && strcmp (argv[1], "--version") == 0) |
711 | { | |
712 | gdbserver_version (); | |
713 | exit (0); | |
714 | } | |
715 | ||
716 | if (argc >= 2 && strcmp (argv[1], "--help") == 0) | |
717 | { | |
718 | gdbserver_usage (); | |
719 | exit (0); | |
720 | } | |
721 | ||
c5aa993b | 722 | if (setjmp (toplevel)) |
c906108c | 723 | { |
c5aa993b JM |
724 | fprintf (stderr, "Exiting\n"); |
725 | exit (1); | |
c906108c SS |
726 | } |
727 | ||
0729219d DJ |
728 | bad_attach = 0; |
729 | pid = 0; | |
730 | attached = 0; | |
45b7b345 DJ |
731 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
732 | { | |
733 | if (argc == 4 | |
506c7aa0 | 734 | && argv[3][0] != '\0' |
45b7b345 DJ |
735 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 |
736 | && *arg_end == '\0') | |
737 | { | |
738 | ; | |
739 | } | |
740 | else | |
741 | bad_attach = 1; | |
742 | } | |
743 | ||
744 | if (argc < 3 || bad_attach) | |
dd24457d DJ |
745 | { |
746 | gdbserver_usage (); | |
747 | exit (1); | |
748 | } | |
c906108c | 749 | |
4ce44c66 JM |
750 | initialize_low (); |
751 | ||
0a30fbc4 | 752 | own_buf = malloc (PBUFSIZ); |
7fb85e41 | 753 | mem_buf = malloc (PBUFSIZ); |
0a30fbc4 | 754 | |
45b7b345 DJ |
755 | if (pid == 0) |
756 | { | |
757 | /* Wait till we are at first instruction in program. */ | |
758 | signal = start_inferior (&argv[2], &status); | |
c906108c | 759 | |
45b7b345 DJ |
760 | /* We are now stopped at the first instruction of the target process */ |
761 | } | |
762 | else | |
763 | { | |
764 | switch (attach_inferior (pid, &status, &signal)) | |
765 | { | |
766 | case -1: | |
767 | error ("Attaching not supported on this target"); | |
768 | break; | |
769 | default: | |
770 | attached = 1; | |
771 | break; | |
772 | } | |
773 | } | |
c906108c | 774 | |
8264bb58 DJ |
775 | if (setjmp (toplevel)) |
776 | { | |
777 | fprintf (stderr, "Killing inferior\n"); | |
778 | kill_inferior (); | |
779 | exit (1); | |
780 | } | |
781 | ||
c906108c SS |
782 | while (1) |
783 | { | |
784 | remote_open (argv[1]); | |
785 | ||
c5aa993b JM |
786 | restart: |
787 | setjmp (toplevel); | |
01f9e8fa | 788 | while (1) |
c906108c SS |
789 | { |
790 | unsigned char sig; | |
01f9e8fa DJ |
791 | int packet_len; |
792 | int new_packet_len = -1; | |
793 | ||
794 | packet_len = getpkt (own_buf); | |
795 | if (packet_len <= 0) | |
796 | break; | |
797 | ||
c906108c SS |
798 | i = 0; |
799 | ch = own_buf[i++]; | |
800 | switch (ch) | |
801 | { | |
ce3a066d | 802 | case 'q': |
0876f84a | 803 | handle_query (own_buf, &new_packet_len); |
ce3a066d | 804 | break; |
89be2091 DJ |
805 | case 'Q': |
806 | handle_general_set (own_buf); | |
807 | break; | |
b80864fb DJ |
808 | #ifndef USE_WIN32API |
809 | /* Skip "detach" support on mingw32, since we don't have | |
810 | waitpid. */ | |
6ad8ae5c DJ |
811 | case 'D': |
812 | fprintf (stderr, "Detaching from inferior\n"); | |
813 | detach_inferior (); | |
814 | write_ok (own_buf); | |
815 | putpkt (own_buf); | |
aa691b87 | 816 | remote_close (); |
6ad8ae5c DJ |
817 | |
818 | /* If we are attached, then we can exit. Otherwise, we need to | |
819 | hang around doing nothing, until the child is gone. */ | |
820 | if (!attached) | |
821 | { | |
822 | int status, ret; | |
823 | ||
824 | do { | |
825 | ret = waitpid (signal_pid, &status, 0); | |
826 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
827 | break; | |
828 | } while (ret != -1 || errno != ECHILD); | |
829 | } | |
830 | ||
831 | exit (0); | |
b80864fb | 832 | #endif |
6ad8ae5c | 833 | |
c906108c | 834 | case '!': |
45b7b345 DJ |
835 | if (attached == 0) |
836 | { | |
837 | extended_protocol = 1; | |
838 | prepare_resume_reply (own_buf, status, signal); | |
839 | } | |
840 | else | |
841 | { | |
842 | /* We can not use the extended protocol if we are | |
843 | attached, because we can not restart the running | |
844 | program. So return unrecognized. */ | |
845 | own_buf[0] = '\0'; | |
846 | } | |
c906108c SS |
847 | break; |
848 | case '?': | |
849 | prepare_resume_reply (own_buf, status, signal); | |
850 | break; | |
851 | case 'H': | |
a06660f7 | 852 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
c906108c | 853 | { |
a06660f7 DJ |
854 | unsigned long gdb_id, thread_id; |
855 | ||
856 | gdb_id = strtoul (&own_buf[2], NULL, 16); | |
857 | thread_id = gdb_id_to_thread_id (gdb_id); | |
858 | if (thread_id == 0) | |
859 | { | |
860 | write_enn (own_buf); | |
861 | break; | |
862 | } | |
863 | ||
864 | if (own_buf[1] == 'g') | |
865 | { | |
866 | general_thread = thread_id; | |
867 | set_desired_inferior (1); | |
868 | } | |
869 | else if (own_buf[1] == 'c') | |
870 | cont_thread = thread_id; | |
871 | else if (own_buf[1] == 's') | |
872 | step_thread = thread_id; | |
873 | ||
0d62e5e8 | 874 | write_ok (own_buf); |
a06660f7 DJ |
875 | } |
876 | else | |
877 | { | |
c906108c SS |
878 | /* Silently ignore it so that gdb can extend the protocol |
879 | without compatibility headaches. */ | |
880 | own_buf[0] = '\0'; | |
c906108c SS |
881 | } |
882 | break; | |
883 | case 'g': | |
0d62e5e8 | 884 | set_desired_inferior (1); |
0a30fbc4 | 885 | registers_to_string (own_buf); |
c906108c SS |
886 | break; |
887 | case 'G': | |
0d62e5e8 | 888 | set_desired_inferior (1); |
0a30fbc4 | 889 | registers_from_string (&own_buf[1]); |
c906108c SS |
890 | write_ok (own_buf); |
891 | break; | |
892 | case 'm': | |
893 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
c3e735a6 DJ |
894 | if (read_inferior_memory (mem_addr, mem_buf, len) == 0) |
895 | convert_int_to_ascii (mem_buf, own_buf, len); | |
896 | else | |
897 | write_enn (own_buf); | |
c906108c SS |
898 | break; |
899 | case 'M': | |
900 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
901 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
902 | write_ok (own_buf); | |
903 | else | |
904 | write_enn (own_buf); | |
905 | break; | |
01f9e8fa DJ |
906 | case 'X': |
907 | if (decode_X_packet (&own_buf[1], packet_len - 1, | |
908 | &mem_addr, &len, mem_buf) < 0 | |
909 | || write_inferior_memory (mem_addr, mem_buf, len) != 0) | |
910 | write_enn (own_buf); | |
911 | else | |
912 | write_ok (own_buf); | |
913 | break; | |
c906108c SS |
914 | case 'C': |
915 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
916 | if (target_signal_to_host_p (sig)) |
917 | signal = target_signal_to_host (sig); | |
918 | else | |
919 | signal = 0; | |
0d62e5e8 | 920 | set_desired_inferior (0); |
0e98d0a7 | 921 | myresume (0, signal); |
0d62e5e8 | 922 | signal = mywait (&status, 1); |
c906108c SS |
923 | prepare_resume_reply (own_buf, status, signal); |
924 | break; | |
925 | case 'S': | |
926 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
927 | if (target_signal_to_host_p (sig)) |
928 | signal = target_signal_to_host (sig); | |
929 | else | |
930 | signal = 0; | |
0d62e5e8 | 931 | set_desired_inferior (0); |
0e98d0a7 | 932 | myresume (1, signal); |
0d62e5e8 | 933 | signal = mywait (&status, 1); |
c906108c SS |
934 | prepare_resume_reply (own_buf, status, signal); |
935 | break; | |
936 | case 'c': | |
0d62e5e8 | 937 | set_desired_inferior (0); |
c906108c | 938 | myresume (0, 0); |
0d62e5e8 | 939 | signal = mywait (&status, 1); |
c906108c SS |
940 | prepare_resume_reply (own_buf, status, signal); |
941 | break; | |
942 | case 's': | |
0d62e5e8 | 943 | set_desired_inferior (0); |
c906108c | 944 | myresume (1, 0); |
0d62e5e8 | 945 | signal = mywait (&status, 1); |
c906108c SS |
946 | prepare_resume_reply (own_buf, status, signal); |
947 | break; | |
e013ee27 OF |
948 | case 'Z': |
949 | { | |
950 | char *lenptr; | |
951 | char *dataptr; | |
952 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
953 | int len = strtol (lenptr + 1, &dataptr, 16); | |
954 | char type = own_buf[1]; | |
955 | ||
956 | if (the_target->insert_watchpoint == NULL | |
957 | || (type < '2' || type > '4')) | |
958 | { | |
959 | /* No watchpoint support or not a watchpoint command; | |
960 | unrecognized either way. */ | |
961 | own_buf[0] = '\0'; | |
962 | } | |
963 | else | |
964 | { | |
965 | int res; | |
966 | ||
967 | res = (*the_target->insert_watchpoint) (type, addr, len); | |
968 | if (res == 0) | |
969 | write_ok (own_buf); | |
970 | else if (res == 1) | |
971 | /* Unsupported. */ | |
972 | own_buf[0] = '\0'; | |
973 | else | |
974 | write_enn (own_buf); | |
975 | } | |
976 | break; | |
977 | } | |
978 | case 'z': | |
979 | { | |
980 | char *lenptr; | |
981 | char *dataptr; | |
982 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
983 | int len = strtol (lenptr + 1, &dataptr, 16); | |
984 | char type = own_buf[1]; | |
985 | ||
986 | if (the_target->remove_watchpoint == NULL | |
987 | || (type < '2' || type > '4')) | |
988 | { | |
989 | /* No watchpoint support or not a watchpoint command; | |
990 | unrecognized either way. */ | |
991 | own_buf[0] = '\0'; | |
992 | } | |
993 | else | |
994 | { | |
995 | int res; | |
996 | ||
997 | res = (*the_target->remove_watchpoint) (type, addr, len); | |
998 | if (res == 0) | |
999 | write_ok (own_buf); | |
1000 | else if (res == 1) | |
1001 | /* Unsupported. */ | |
1002 | own_buf[0] = '\0'; | |
1003 | else | |
1004 | write_enn (own_buf); | |
1005 | } | |
1006 | break; | |
1007 | } | |
c906108c SS |
1008 | case 'k': |
1009 | fprintf (stderr, "Killing inferior\n"); | |
1010 | kill_inferior (); | |
1011 | /* When using the extended protocol, we start up a new | |
c5aa993b | 1012 | debugging session. The traditional protocol will |
c906108c SS |
1013 | exit instead. */ |
1014 | if (extended_protocol) | |
1015 | { | |
1016 | write_ok (own_buf); | |
1017 | fprintf (stderr, "GDBserver restarting\n"); | |
1018 | ||
1019 | /* Wait till we are at 1st instruction in prog. */ | |
1020 | signal = start_inferior (&argv[2], &status); | |
1021 | goto restart; | |
1022 | break; | |
1023 | } | |
1024 | else | |
1025 | { | |
1026 | exit (0); | |
1027 | break; | |
1028 | } | |
1029 | case 'T': | |
a06660f7 DJ |
1030 | { |
1031 | unsigned long gdb_id, thread_id; | |
1032 | ||
1033 | gdb_id = strtoul (&own_buf[1], NULL, 16); | |
1034 | thread_id = gdb_id_to_thread_id (gdb_id); | |
1035 | if (thread_id == 0) | |
1036 | { | |
1037 | write_enn (own_buf); | |
1038 | break; | |
1039 | } | |
1040 | ||
1041 | if (mythread_alive (thread_id)) | |
1042 | write_ok (own_buf); | |
1043 | else | |
1044 | write_enn (own_buf); | |
1045 | } | |
c906108c SS |
1046 | break; |
1047 | case 'R': | |
1048 | /* Restarting the inferior is only supported in the | |
c5aa993b | 1049 | extended protocol. */ |
c906108c SS |
1050 | if (extended_protocol) |
1051 | { | |
1052 | kill_inferior (); | |
1053 | write_ok (own_buf); | |
1054 | fprintf (stderr, "GDBserver restarting\n"); | |
1055 | ||
1056 | /* Wait till we are at 1st instruction in prog. */ | |
1057 | signal = start_inferior (&argv[2], &status); | |
1058 | goto restart; | |
1059 | break; | |
1060 | } | |
1061 | else | |
1062 | { | |
1063 | /* It is a request we don't understand. Respond with an | |
1064 | empty packet so that gdb knows that we don't support this | |
1065 | request. */ | |
1066 | own_buf[0] = '\0'; | |
1067 | break; | |
1068 | } | |
64386c31 DJ |
1069 | case 'v': |
1070 | /* Extended (long) request. */ | |
1071 | handle_v_requests (own_buf, &status, &signal); | |
1072 | break; | |
c906108c SS |
1073 | default: |
1074 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
1075 | empty packet so that gdb knows that we don't support this |
1076 | request. */ | |
c906108c SS |
1077 | own_buf[0] = '\0'; |
1078 | break; | |
1079 | } | |
1080 | ||
01f9e8fa DJ |
1081 | if (new_packet_len != -1) |
1082 | putpkt_binary (own_buf, new_packet_len); | |
1083 | else | |
1084 | putpkt (own_buf); | |
c906108c SS |
1085 | |
1086 | if (status == 'W') | |
1087 | fprintf (stderr, | |
3a7fb99b | 1088 | "\nChild exited with status %d\n", signal); |
c906108c | 1089 | if (status == 'X') |
b80864fb DJ |
1090 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", |
1091 | target_signal_to_host (signal), | |
1092 | target_signal_to_name (signal)); | |
c906108c SS |
1093 | if (status == 'W' || status == 'X') |
1094 | { | |
1095 | if (extended_protocol) | |
1096 | { | |
1097 | fprintf (stderr, "Killing inferior\n"); | |
1098 | kill_inferior (); | |
1099 | write_ok (own_buf); | |
1100 | fprintf (stderr, "GDBserver restarting\n"); | |
1101 | ||
1102 | /* Wait till we are at 1st instruction in prog. */ | |
1103 | signal = start_inferior (&argv[2], &status); | |
1104 | goto restart; | |
1105 | break; | |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | fprintf (stderr, "GDBserver exiting\n"); | |
1110 | exit (0); | |
1111 | } | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | /* We come here when getpkt fails. | |
1116 | ||
c5aa993b JM |
1117 | For the extended remote protocol we exit (and this is the only |
1118 | way we gracefully exit!). | |
c906108c | 1119 | |
c5aa993b JM |
1120 | For the traditional remote protocol close the connection, |
1121 | and re-open it at the top of the loop. */ | |
c906108c SS |
1122 | if (extended_protocol) |
1123 | { | |
1124 | remote_close (); | |
1125 | exit (0); | |
1126 | } | |
1127 | else | |
1128 | { | |
45b7b345 DJ |
1129 | fprintf (stderr, "Remote side has terminated connection. " |
1130 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
1131 | remote_close (); |
1132 | } | |
1133 | } | |
1134 | } |