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