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 | ||
bb63802a UW |
262 | /* Reply the current thread id. */ |
263 | if (strcmp ("qC", own_buf) == 0) | |
264 | { | |
265 | thread_ptr = all_threads.head; | |
266 | sprintf (own_buf, "QC%x", | |
267 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); | |
268 | return; | |
269 | } | |
270 | ||
ce3a066d DJ |
271 | if (strcmp ("qSymbol::", own_buf) == 0) |
272 | { | |
2f2893d9 DJ |
273 | if (the_target->look_up_symbols != NULL) |
274 | (*the_target->look_up_symbols) (); | |
275 | ||
ce3a066d DJ |
276 | strcpy (own_buf, "OK"); |
277 | return; | |
278 | } | |
279 | ||
0d62e5e8 DJ |
280 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
281 | { | |
282 | thread_ptr = all_threads.head; | |
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 | } | |
aa691b87 | 287 | |
0d62e5e8 DJ |
288 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
289 | { | |
290 | if (thread_ptr != NULL) | |
291 | { | |
a06660f7 | 292 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
293 | thread_ptr = thread_ptr->next; |
294 | return; | |
295 | } | |
296 | else | |
297 | { | |
298 | sprintf (own_buf, "l"); | |
299 | return; | |
300 | } | |
301 | } | |
aa691b87 | 302 | |
52fb6437 NS |
303 | if (the_target->read_offsets != NULL |
304 | && strcmp ("qOffsets", own_buf) == 0) | |
305 | { | |
306 | CORE_ADDR text, data; | |
307 | ||
308 | if (the_target->read_offsets (&text, &data)) | |
309 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
310 | (long)text, (long)data, (long)data); | |
311 | else | |
312 | write_enn (own_buf); | |
313 | ||
314 | return; | |
315 | } | |
316 | ||
aa691b87 | 317 | if (the_target->read_auxv != NULL |
0876f84a | 318 | && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0) |
aa691b87 | 319 | { |
0876f84a DJ |
320 | unsigned char *data; |
321 | int n; | |
aa691b87 RM |
322 | CORE_ADDR ofs; |
323 | unsigned int len; | |
0876f84a DJ |
324 | char *annex; |
325 | ||
326 | /* Reject any annex; grab the offset and length. */ | |
327 | if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0 | |
328 | || annex[0] != '\0') | |
329 | { | |
330 | strcpy (own_buf, "E00"); | |
331 | return; | |
332 | } | |
333 | ||
334 | /* Read one extra byte, as an indicator of whether there is | |
335 | more. */ | |
336 | if (len > PBUFSIZ - 2) | |
337 | len = PBUFSIZ - 2; | |
338 | data = malloc (len + 1); | |
339 | n = (*the_target->read_auxv) (ofs, data, len + 1); | |
000ef4f0 DJ |
340 | if (n < 0) |
341 | write_enn (own_buf); | |
342 | else if (n > len) | |
0876f84a | 343 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
aa691b87 | 344 | else |
0876f84a DJ |
345 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
346 | ||
347 | free (data); | |
348 | ||
aa691b87 RM |
349 | return; |
350 | } | |
351 | ||
23181151 DJ |
352 | if (strncmp ("qXfer:features:read:", own_buf, 20) == 0) |
353 | { | |
354 | CORE_ADDR ofs; | |
355 | unsigned int len, total_len; | |
356 | const char *document; | |
357 | char *annex; | |
358 | ||
fb1e4ffc DJ |
359 | /* Check for support. */ |
360 | document = get_features_xml ("target.xml"); | |
23181151 DJ |
361 | if (document == NULL) |
362 | { | |
363 | own_buf[0] = '\0'; | |
364 | return; | |
365 | } | |
366 | ||
fb1e4ffc DJ |
367 | /* Grab the annex, offset, and length. */ |
368 | if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0) | |
369 | { | |
370 | strcpy (own_buf, "E00"); | |
371 | return; | |
372 | } | |
373 | ||
374 | /* Now grab the correct annex. */ | |
375 | document = get_features_xml (annex); | |
376 | if (document == NULL) | |
23181151 DJ |
377 | { |
378 | strcpy (own_buf, "E00"); | |
379 | return; | |
380 | } | |
381 | ||
382 | total_len = strlen (document); | |
383 | if (len > PBUFSIZ - 2) | |
384 | len = PBUFSIZ - 2; | |
385 | ||
386 | if (ofs > total_len) | |
387 | write_enn (own_buf); | |
388 | else if (len < total_len - ofs) | |
389 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
390 | len, 1); | |
391 | else | |
392 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
393 | total_len - ofs, 0); | |
394 | ||
395 | return; | |
396 | } | |
397 | ||
be2a5f71 DJ |
398 | /* Protocol features query. */ |
399 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
400 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
401 | { | |
89be2091 | 402 | sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1); |
0876f84a DJ |
403 | |
404 | if (the_target->read_auxv != NULL) | |
9f2e1e63 | 405 | strcat (own_buf, ";qXfer:auxv:read+"); |
0876f84a | 406 | |
fb1e4ffc | 407 | if (get_features_xml ("target.xml") != NULL) |
23181151 DJ |
408 | strcat (own_buf, ";qXfer:features:read+"); |
409 | ||
be2a5f71 DJ |
410 | return; |
411 | } | |
412 | ||
dae5f5cf DJ |
413 | /* Thread-local storage support. */ |
414 | if (the_target->get_tls_address != NULL | |
415 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) | |
416 | { | |
417 | char *p = own_buf + 12; | |
418 | CORE_ADDR parts[3], address = 0; | |
419 | int i, err; | |
420 | ||
421 | for (i = 0; i < 3; i++) | |
422 | { | |
423 | char *p2; | |
424 | int len; | |
425 | ||
426 | if (p == NULL) | |
427 | break; | |
428 | ||
429 | p2 = strchr (p, ','); | |
430 | if (p2) | |
431 | { | |
432 | len = p2 - p; | |
433 | p2++; | |
434 | } | |
435 | else | |
436 | { | |
437 | len = strlen (p); | |
438 | p2 = NULL; | |
439 | } | |
440 | ||
441 | decode_address (&parts[i], p, len); | |
442 | p = p2; | |
443 | } | |
444 | ||
445 | if (p != NULL || i < 3) | |
446 | err = 1; | |
447 | else | |
448 | { | |
449 | struct thread_info *thread = gdb_id_to_thread (parts[0]); | |
450 | ||
451 | if (thread == NULL) | |
452 | err = 2; | |
453 | else | |
454 | err = the_target->get_tls_address (thread, parts[1], parts[2], | |
455 | &address); | |
456 | } | |
457 | ||
458 | if (err == 0) | |
459 | { | |
460 | sprintf (own_buf, "%llx", address); | |
461 | return; | |
462 | } | |
463 | else if (err > 0) | |
464 | { | |
465 | write_enn (own_buf); | |
466 | return; | |
467 | } | |
468 | ||
469 | /* Otherwise, pretend we do not understand this packet. */ | |
470 | } | |
471 | ||
c74d0ad8 DJ |
472 | /* Handle "monitor" commands. */ |
473 | if (strncmp ("qRcmd,", own_buf, 6) == 0) | |
474 | { | |
475 | char *mon = malloc (PBUFSIZ); | |
476 | int len = strlen (own_buf + 6); | |
477 | ||
478 | if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) | |
479 | { | |
480 | write_enn (own_buf); | |
481 | free (mon); | |
482 | return; | |
483 | } | |
484 | mon[len / 2] = '\0'; | |
485 | ||
486 | write_ok (own_buf); | |
487 | ||
488 | if (strcmp (mon, "set debug 1") == 0) | |
489 | { | |
490 | debug_threads = 1; | |
491 | monitor_output ("Debug output enabled.\n"); | |
492 | } | |
493 | else if (strcmp (mon, "set debug 0") == 0) | |
494 | { | |
495 | debug_threads = 0; | |
496 | monitor_output ("Debug output disabled.\n"); | |
497 | } | |
498 | else if (strcmp (mon, "set remote-debug 1") == 0) | |
499 | { | |
500 | remote_debug = 1; | |
501 | monitor_output ("Protocol debug output enabled.\n"); | |
502 | } | |
503 | else if (strcmp (mon, "set remote-debug 0") == 0) | |
504 | { | |
505 | remote_debug = 0; | |
506 | monitor_output ("Protocol debug output disabled.\n"); | |
507 | } | |
508 | else if (strcmp (mon, "help") == 0) | |
509 | monitor_show_help (); | |
510 | else | |
511 | { | |
512 | monitor_output ("Unknown monitor command.\n\n"); | |
513 | monitor_show_help (); | |
514 | write_enn (own_buf); | |
515 | } | |
516 | ||
517 | free (mon); | |
518 | return; | |
519 | } | |
520 | ||
ce3a066d DJ |
521 | /* Otherwise we didn't know what packet it was. Say we didn't |
522 | understand it. */ | |
523 | own_buf[0] = 0; | |
524 | } | |
525 | ||
64386c31 DJ |
526 | /* Parse vCont packets. */ |
527 | void | |
fc620387 | 528 | handle_v_cont (char *own_buf, char *status, int *signal) |
64386c31 DJ |
529 | { |
530 | char *p, *q; | |
531 | int n = 0, i = 0; | |
532 | struct thread_resume *resume_info, default_action; | |
533 | ||
534 | /* Count the number of semicolons in the packet. There should be one | |
535 | for every action. */ | |
536 | p = &own_buf[5]; | |
537 | while (p) | |
538 | { | |
539 | n++; | |
540 | p++; | |
541 | p = strchr (p, ';'); | |
542 | } | |
543 | /* Allocate room for one extra action, for the default remain-stopped | |
544 | behavior; if no default action is in the list, we'll need the extra | |
545 | slot. */ | |
546 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
547 | ||
548 | default_action.thread = -1; | |
549 | default_action.leave_stopped = 1; | |
550 | default_action.step = 0; | |
551 | default_action.sig = 0; | |
552 | ||
553 | p = &own_buf[5]; | |
554 | i = 0; | |
555 | while (*p) | |
556 | { | |
557 | p++; | |
558 | ||
559 | resume_info[i].leave_stopped = 0; | |
560 | ||
561 | if (p[0] == 's' || p[0] == 'S') | |
562 | resume_info[i].step = 1; | |
563 | else if (p[0] == 'c' || p[0] == 'C') | |
564 | resume_info[i].step = 0; | |
565 | else | |
566 | goto err; | |
567 | ||
568 | if (p[0] == 'S' || p[0] == 'C') | |
569 | { | |
570 | int sig; | |
571 | sig = strtol (p + 1, &q, 16); | |
572 | if (p == q) | |
573 | goto err; | |
574 | p = q; | |
575 | ||
576 | if (!target_signal_to_host_p (sig)) | |
577 | goto err; | |
578 | resume_info[i].sig = target_signal_to_host (sig); | |
579 | } | |
580 | else | |
581 | { | |
582 | resume_info[i].sig = 0; | |
583 | p = p + 1; | |
584 | } | |
585 | ||
586 | if (p[0] == 0) | |
587 | { | |
588 | resume_info[i].thread = -1; | |
589 | default_action = resume_info[i]; | |
590 | ||
591 | /* Note: we don't increment i here, we'll overwrite this entry | |
592 | the next time through. */ | |
593 | } | |
594 | else if (p[0] == ':') | |
595 | { | |
a06660f7 DJ |
596 | unsigned int gdb_id = strtoul (p + 1, &q, 16); |
597 | unsigned long thread_id; | |
598 | ||
64386c31 DJ |
599 | if (p == q) |
600 | goto err; | |
601 | p = q; | |
602 | if (p[0] != ';' && p[0] != 0) | |
603 | goto err; | |
604 | ||
a06660f7 DJ |
605 | thread_id = gdb_id_to_thread_id (gdb_id); |
606 | if (thread_id) | |
607 | resume_info[i].thread = thread_id; | |
608 | else | |
609 | goto err; | |
610 | ||
64386c31 DJ |
611 | i++; |
612 | } | |
613 | } | |
614 | ||
615 | resume_info[i] = default_action; | |
616 | ||
617 | /* Still used in occasional places in the backend. */ | |
618 | if (n == 1 && resume_info[0].thread != -1) | |
619 | cont_thread = resume_info[0].thread; | |
620 | else | |
621 | cont_thread = -1; | |
dc3f8883 | 622 | set_desired_inferior (0); |
64386c31 DJ |
623 | |
624 | (*the_target->resume) (resume_info); | |
625 | ||
626 | free (resume_info); | |
627 | ||
628 | *signal = mywait (status, 1); | |
629 | prepare_resume_reply (own_buf, *status, *signal); | |
630 | return; | |
631 | ||
632 | err: | |
633 | /* No other way to report an error... */ | |
634 | strcpy (own_buf, ""); | |
635 | free (resume_info); | |
636 | return; | |
637 | } | |
638 | ||
639 | /* Handle all of the extended 'v' packets. */ | |
640 | void | |
fc620387 | 641 | handle_v_requests (char *own_buf, char *status, int *signal) |
64386c31 DJ |
642 | { |
643 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
644 | { | |
645 | handle_v_cont (own_buf, status, signal); | |
646 | return; | |
647 | } | |
648 | ||
649 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
650 | { | |
651 | strcpy (own_buf, "vCont;c;C;s;S"); | |
652 | return; | |
653 | } | |
654 | ||
655 | /* Otherwise we didn't know what packet it was. Say we didn't | |
656 | understand it. */ | |
657 | own_buf[0] = 0; | |
658 | return; | |
659 | } | |
660 | ||
661 | void | |
662 | myresume (int step, int sig) | |
663 | { | |
664 | struct thread_resume resume_info[2]; | |
665 | int n = 0; | |
666 | ||
d592fa2f | 667 | if (step || sig || (cont_thread != 0 && cont_thread != -1)) |
64386c31 DJ |
668 | { |
669 | resume_info[0].thread | |
670 | = ((struct inferior_list_entry *) current_inferior)->id; | |
671 | resume_info[0].step = step; | |
672 | resume_info[0].sig = sig; | |
673 | resume_info[0].leave_stopped = 0; | |
674 | n++; | |
675 | } | |
676 | resume_info[n].thread = -1; | |
677 | resume_info[n].step = 0; | |
678 | resume_info[n].sig = 0; | |
d592fa2f | 679 | resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); |
64386c31 DJ |
680 | |
681 | (*the_target->resume) (resume_info); | |
682 | } | |
683 | ||
0729219d | 684 | static int attached; |
c906108c | 685 | |
dd24457d DJ |
686 | static void |
687 | gdbserver_version (void) | |
688 | { | |
689 | printf ("GNU gdbserver %s\n" | |
690 | "Copyright (C) 2006 Free Software Foundation, Inc.\n" | |
691 | "gdbserver is free software, covered by the GNU General Public License.\n" | |
692 | "This gdbserver was configured as \"%s\"\n", | |
693 | version, host_name); | |
694 | } | |
695 | ||
0bc68c49 DJ |
696 | static void |
697 | gdbserver_usage (void) | |
698 | { | |
dd24457d DJ |
699 | printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" |
700 | "\tgdbserver COMM --attach PID\n" | |
701 | "\n" | |
702 | "COMM may either be a tty device (for serial debugging), or \n" | |
703 | "HOST:PORT to listen for a TCP connection.\n"); | |
0bc68c49 DJ |
704 | } |
705 | ||
c906108c | 706 | int |
da85418c | 707 | main (int argc, char *argv[]) |
c906108c | 708 | { |
f450004a | 709 | char ch, status, *own_buf; |
7fb85e41 | 710 | unsigned char *mem_buf; |
c906108c | 711 | int i = 0; |
fc620387 | 712 | int signal; |
c906108c SS |
713 | unsigned int len; |
714 | CORE_ADDR mem_addr; | |
0729219d DJ |
715 | int bad_attach; |
716 | int pid; | |
45b7b345 | 717 | char *arg_end; |
c906108c | 718 | |
dd24457d DJ |
719 | if (argc >= 2 && strcmp (argv[1], "--version") == 0) |
720 | { | |
721 | gdbserver_version (); | |
722 | exit (0); | |
723 | } | |
724 | ||
725 | if (argc >= 2 && strcmp (argv[1], "--help") == 0) | |
726 | { | |
727 | gdbserver_usage (); | |
728 | exit (0); | |
729 | } | |
730 | ||
c5aa993b | 731 | if (setjmp (toplevel)) |
c906108c | 732 | { |
c5aa993b JM |
733 | fprintf (stderr, "Exiting\n"); |
734 | exit (1); | |
c906108c SS |
735 | } |
736 | ||
0729219d DJ |
737 | bad_attach = 0; |
738 | pid = 0; | |
739 | attached = 0; | |
45b7b345 DJ |
740 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
741 | { | |
742 | if (argc == 4 | |
506c7aa0 | 743 | && argv[3][0] != '\0' |
45b7b345 DJ |
744 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 |
745 | && *arg_end == '\0') | |
746 | { | |
747 | ; | |
748 | } | |
749 | else | |
750 | bad_attach = 1; | |
751 | } | |
752 | ||
753 | if (argc < 3 || bad_attach) | |
dd24457d DJ |
754 | { |
755 | gdbserver_usage (); | |
756 | exit (1); | |
757 | } | |
c906108c | 758 | |
4ce44c66 JM |
759 | initialize_low (); |
760 | ||
0a30fbc4 | 761 | own_buf = malloc (PBUFSIZ); |
7fb85e41 | 762 | mem_buf = malloc (PBUFSIZ); |
0a30fbc4 | 763 | |
45b7b345 DJ |
764 | if (pid == 0) |
765 | { | |
766 | /* Wait till we are at first instruction in program. */ | |
767 | signal = start_inferior (&argv[2], &status); | |
c906108c | 768 | |
45b7b345 DJ |
769 | /* We are now stopped at the first instruction of the target process */ |
770 | } | |
771 | else | |
772 | { | |
773 | switch (attach_inferior (pid, &status, &signal)) | |
774 | { | |
775 | case -1: | |
776 | error ("Attaching not supported on this target"); | |
777 | break; | |
778 | default: | |
779 | attached = 1; | |
780 | break; | |
781 | } | |
782 | } | |
c906108c | 783 | |
8264bb58 DJ |
784 | if (setjmp (toplevel)) |
785 | { | |
786 | fprintf (stderr, "Killing inferior\n"); | |
787 | kill_inferior (); | |
788 | exit (1); | |
789 | } | |
790 | ||
c906108c SS |
791 | while (1) |
792 | { | |
793 | remote_open (argv[1]); | |
794 | ||
c5aa993b JM |
795 | restart: |
796 | setjmp (toplevel); | |
01f9e8fa | 797 | while (1) |
c906108c SS |
798 | { |
799 | unsigned char sig; | |
01f9e8fa DJ |
800 | int packet_len; |
801 | int new_packet_len = -1; | |
802 | ||
803 | packet_len = getpkt (own_buf); | |
804 | if (packet_len <= 0) | |
805 | break; | |
806 | ||
c906108c SS |
807 | i = 0; |
808 | ch = own_buf[i++]; | |
809 | switch (ch) | |
810 | { | |
ce3a066d | 811 | case 'q': |
0876f84a | 812 | handle_query (own_buf, &new_packet_len); |
ce3a066d | 813 | break; |
89be2091 DJ |
814 | case 'Q': |
815 | handle_general_set (own_buf); | |
816 | break; | |
6ad8ae5c DJ |
817 | case 'D': |
818 | fprintf (stderr, "Detaching from inferior\n"); | |
444d6139 | 819 | if (detach_inferior () != 0) |
6ad8ae5c | 820 | { |
444d6139 PA |
821 | write_enn (own_buf); |
822 | putpkt (own_buf); | |
6ad8ae5c | 823 | } |
444d6139 PA |
824 | else |
825 | { | |
826 | write_ok (own_buf); | |
827 | putpkt (own_buf); | |
828 | remote_close (); | |
6ad8ae5c | 829 | |
444d6139 PA |
830 | /* If we are attached, then we can exit. Otherwise, we |
831 | need to hang around doing nothing, until the child | |
832 | is gone. */ | |
833 | if (!attached) | |
834 | join_inferior (); | |
6ad8ae5c | 835 | |
444d6139 PA |
836 | exit (0); |
837 | } | |
c906108c | 838 | case '!': |
45b7b345 DJ |
839 | if (attached == 0) |
840 | { | |
841 | extended_protocol = 1; | |
842 | prepare_resume_reply (own_buf, status, signal); | |
843 | } | |
844 | else | |
845 | { | |
846 | /* We can not use the extended protocol if we are | |
847 | attached, because we can not restart the running | |
848 | program. So return unrecognized. */ | |
849 | own_buf[0] = '\0'; | |
850 | } | |
c906108c SS |
851 | break; |
852 | case '?': | |
853 | prepare_resume_reply (own_buf, status, signal); | |
854 | break; | |
855 | case 'H': | |
a06660f7 | 856 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
c906108c | 857 | { |
a06660f7 DJ |
858 | unsigned long gdb_id, thread_id; |
859 | ||
860 | gdb_id = strtoul (&own_buf[2], NULL, 16); | |
861 | thread_id = gdb_id_to_thread_id (gdb_id); | |
862 | if (thread_id == 0) | |
863 | { | |
864 | write_enn (own_buf); | |
865 | break; | |
866 | } | |
867 | ||
868 | if (own_buf[1] == 'g') | |
869 | { | |
870 | general_thread = thread_id; | |
871 | set_desired_inferior (1); | |
872 | } | |
873 | else if (own_buf[1] == 'c') | |
874 | cont_thread = thread_id; | |
875 | else if (own_buf[1] == 's') | |
876 | step_thread = thread_id; | |
877 | ||
0d62e5e8 | 878 | write_ok (own_buf); |
a06660f7 DJ |
879 | } |
880 | else | |
881 | { | |
c906108c SS |
882 | /* Silently ignore it so that gdb can extend the protocol |
883 | without compatibility headaches. */ | |
884 | own_buf[0] = '\0'; | |
c906108c SS |
885 | } |
886 | break; | |
887 | case 'g': | |
0d62e5e8 | 888 | set_desired_inferior (1); |
0a30fbc4 | 889 | registers_to_string (own_buf); |
c906108c SS |
890 | break; |
891 | case 'G': | |
0d62e5e8 | 892 | set_desired_inferior (1); |
0a30fbc4 | 893 | registers_from_string (&own_buf[1]); |
c906108c SS |
894 | write_ok (own_buf); |
895 | break; | |
896 | case 'm': | |
897 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
c3e735a6 DJ |
898 | if (read_inferior_memory (mem_addr, mem_buf, len) == 0) |
899 | convert_int_to_ascii (mem_buf, own_buf, len); | |
900 | else | |
901 | write_enn (own_buf); | |
c906108c SS |
902 | break; |
903 | case 'M': | |
904 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
905 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
906 | write_ok (own_buf); | |
907 | else | |
908 | write_enn (own_buf); | |
909 | break; | |
01f9e8fa DJ |
910 | case 'X': |
911 | if (decode_X_packet (&own_buf[1], packet_len - 1, | |
912 | &mem_addr, &len, mem_buf) < 0 | |
913 | || write_inferior_memory (mem_addr, mem_buf, len) != 0) | |
914 | write_enn (own_buf); | |
915 | else | |
916 | write_ok (own_buf); | |
917 | break; | |
c906108c SS |
918 | case 'C': |
919 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
920 | if (target_signal_to_host_p (sig)) |
921 | signal = target_signal_to_host (sig); | |
922 | else | |
923 | signal = 0; | |
0d62e5e8 | 924 | set_desired_inferior (0); |
0e98d0a7 | 925 | myresume (0, signal); |
0d62e5e8 | 926 | signal = mywait (&status, 1); |
c906108c SS |
927 | prepare_resume_reply (own_buf, status, signal); |
928 | break; | |
929 | case 'S': | |
930 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
931 | if (target_signal_to_host_p (sig)) |
932 | signal = target_signal_to_host (sig); | |
933 | else | |
934 | signal = 0; | |
0d62e5e8 | 935 | set_desired_inferior (0); |
0e98d0a7 | 936 | myresume (1, signal); |
0d62e5e8 | 937 | signal = mywait (&status, 1); |
c906108c SS |
938 | prepare_resume_reply (own_buf, status, signal); |
939 | break; | |
940 | case 'c': | |
0d62e5e8 | 941 | set_desired_inferior (0); |
c906108c | 942 | myresume (0, 0); |
0d62e5e8 | 943 | signal = mywait (&status, 1); |
c906108c SS |
944 | prepare_resume_reply (own_buf, status, signal); |
945 | break; | |
946 | case 's': | |
0d62e5e8 | 947 | set_desired_inferior (0); |
c906108c | 948 | myresume (1, 0); |
0d62e5e8 | 949 | signal = mywait (&status, 1); |
c906108c SS |
950 | prepare_resume_reply (own_buf, status, signal); |
951 | break; | |
e013ee27 OF |
952 | case 'Z': |
953 | { | |
954 | char *lenptr; | |
955 | char *dataptr; | |
956 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
957 | int len = strtol (lenptr + 1, &dataptr, 16); | |
958 | char type = own_buf[1]; | |
959 | ||
960 | if (the_target->insert_watchpoint == NULL | |
961 | || (type < '2' || type > '4')) | |
962 | { | |
963 | /* No watchpoint support or not a watchpoint command; | |
964 | unrecognized either way. */ | |
965 | own_buf[0] = '\0'; | |
966 | } | |
967 | else | |
968 | { | |
969 | int res; | |
970 | ||
971 | res = (*the_target->insert_watchpoint) (type, addr, len); | |
972 | if (res == 0) | |
973 | write_ok (own_buf); | |
974 | else if (res == 1) | |
975 | /* Unsupported. */ | |
976 | own_buf[0] = '\0'; | |
977 | else | |
978 | write_enn (own_buf); | |
979 | } | |
980 | break; | |
981 | } | |
982 | case 'z': | |
983 | { | |
984 | char *lenptr; | |
985 | char *dataptr; | |
986 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
987 | int len = strtol (lenptr + 1, &dataptr, 16); | |
988 | char type = own_buf[1]; | |
989 | ||
990 | if (the_target->remove_watchpoint == NULL | |
991 | || (type < '2' || type > '4')) | |
992 | { | |
993 | /* No watchpoint support or not a watchpoint command; | |
994 | unrecognized either way. */ | |
995 | own_buf[0] = '\0'; | |
996 | } | |
997 | else | |
998 | { | |
999 | int res; | |
1000 | ||
1001 | res = (*the_target->remove_watchpoint) (type, addr, len); | |
1002 | if (res == 0) | |
1003 | write_ok (own_buf); | |
1004 | else if (res == 1) | |
1005 | /* Unsupported. */ | |
1006 | own_buf[0] = '\0'; | |
1007 | else | |
1008 | write_enn (own_buf); | |
1009 | } | |
1010 | break; | |
1011 | } | |
c906108c SS |
1012 | case 'k': |
1013 | fprintf (stderr, "Killing inferior\n"); | |
1014 | kill_inferior (); | |
1015 | /* When using the extended protocol, we start up a new | |
c5aa993b | 1016 | debugging session. The traditional protocol will |
c906108c SS |
1017 | exit instead. */ |
1018 | if (extended_protocol) | |
1019 | { | |
1020 | write_ok (own_buf); | |
1021 | fprintf (stderr, "GDBserver restarting\n"); | |
1022 | ||
1023 | /* Wait till we are at 1st instruction in prog. */ | |
1024 | signal = start_inferior (&argv[2], &status); | |
1025 | goto restart; | |
1026 | break; | |
1027 | } | |
1028 | else | |
1029 | { | |
1030 | exit (0); | |
1031 | break; | |
1032 | } | |
1033 | case 'T': | |
a06660f7 DJ |
1034 | { |
1035 | unsigned long gdb_id, thread_id; | |
1036 | ||
1037 | gdb_id = strtoul (&own_buf[1], NULL, 16); | |
1038 | thread_id = gdb_id_to_thread_id (gdb_id); | |
1039 | if (thread_id == 0) | |
1040 | { | |
1041 | write_enn (own_buf); | |
1042 | break; | |
1043 | } | |
1044 | ||
1045 | if (mythread_alive (thread_id)) | |
1046 | write_ok (own_buf); | |
1047 | else | |
1048 | write_enn (own_buf); | |
1049 | } | |
c906108c SS |
1050 | break; |
1051 | case 'R': | |
1052 | /* Restarting the inferior is only supported in the | |
c5aa993b | 1053 | extended protocol. */ |
c906108c SS |
1054 | if (extended_protocol) |
1055 | { | |
1056 | kill_inferior (); | |
1057 | write_ok (own_buf); | |
1058 | fprintf (stderr, "GDBserver restarting\n"); | |
1059 | ||
1060 | /* Wait till we are at 1st instruction in prog. */ | |
1061 | signal = start_inferior (&argv[2], &status); | |
1062 | goto restart; | |
1063 | break; | |
1064 | } | |
1065 | else | |
1066 | { | |
1067 | /* It is a request we don't understand. Respond with an | |
1068 | empty packet so that gdb knows that we don't support this | |
1069 | request. */ | |
1070 | own_buf[0] = '\0'; | |
1071 | break; | |
1072 | } | |
64386c31 DJ |
1073 | case 'v': |
1074 | /* Extended (long) request. */ | |
1075 | handle_v_requests (own_buf, &status, &signal); | |
1076 | break; | |
c906108c SS |
1077 | default: |
1078 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
1079 | empty packet so that gdb knows that we don't support this |
1080 | request. */ | |
c906108c SS |
1081 | own_buf[0] = '\0'; |
1082 | break; | |
1083 | } | |
1084 | ||
01f9e8fa DJ |
1085 | if (new_packet_len != -1) |
1086 | putpkt_binary (own_buf, new_packet_len); | |
1087 | else | |
1088 | putpkt (own_buf); | |
c906108c SS |
1089 | |
1090 | if (status == 'W') | |
1091 | fprintf (stderr, | |
3a7fb99b | 1092 | "\nChild exited with status %d\n", signal); |
c906108c | 1093 | if (status == 'X') |
b80864fb DJ |
1094 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", |
1095 | target_signal_to_host (signal), | |
1096 | target_signal_to_name (signal)); | |
c906108c SS |
1097 | if (status == 'W' || status == 'X') |
1098 | { | |
1099 | if (extended_protocol) | |
1100 | { | |
1101 | fprintf (stderr, "Killing inferior\n"); | |
1102 | kill_inferior (); | |
1103 | write_ok (own_buf); | |
1104 | fprintf (stderr, "GDBserver restarting\n"); | |
1105 | ||
1106 | /* Wait till we are at 1st instruction in prog. */ | |
1107 | signal = start_inferior (&argv[2], &status); | |
1108 | goto restart; | |
1109 | break; | |
1110 | } | |
1111 | else | |
1112 | { | |
1113 | fprintf (stderr, "GDBserver exiting\n"); | |
1114 | exit (0); | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | /* We come here when getpkt fails. | |
1120 | ||
c5aa993b JM |
1121 | For the extended remote protocol we exit (and this is the only |
1122 | way we gracefully exit!). | |
c906108c | 1123 | |
c5aa993b JM |
1124 | For the traditional remote protocol close the connection, |
1125 | and re-open it at the top of the loop. */ | |
c906108c SS |
1126 | if (extended_protocol) |
1127 | { | |
1128 | remote_close (); | |
1129 | exit (0); | |
1130 | } | |
1131 | else | |
1132 | { | |
45b7b345 DJ |
1133 | fprintf (stderr, "Remote side has terminated connection. " |
1134 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
1135 | remote_close (); |
1136 | } | |
1137 | } | |
1138 | } |