Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
db728ff7 | 2 | Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002 |
b6ba6518 | 3 | 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 | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "server.h" | |
23 | ||
a9fa9f7d DJ |
24 | #include <unistd.h> |
25 | #include <signal.h> | |
26 | #include <sys/wait.h> | |
27 | ||
c906108c SS |
28 | int cont_thread; |
29 | int general_thread; | |
0d62e5e8 | 30 | int step_thread; |
c906108c SS |
31 | int thread_from_wait; |
32 | int old_thread_from_wait; | |
33 | int extended_protocol; | |
0d62e5e8 DJ |
34 | int server_waiting; |
35 | ||
c906108c | 36 | jmp_buf toplevel; |
c906108c | 37 | |
a9fa9f7d DJ |
38 | /* The PID of the originally created or attached inferior. Used to |
39 | send signals to the process when GDB sends us an asynchronous interrupt | |
40 | (user hitting Control-C in the client), and to wait for the child to exit | |
41 | when no longer debugging it. */ | |
42 | ||
43 | int signal_pid; | |
44 | ||
c906108c | 45 | static unsigned char |
da85418c | 46 | start_inferior (char *argv[], char *statusptr) |
c906108c | 47 | { |
a9fa9f7d DJ |
48 | signal (SIGTTOU, SIG_DFL); |
49 | signal (SIGTTIN, SIG_DFL); | |
50 | ||
51 | signal_pid = create_inferior (argv[0], argv); | |
0d62e5e8 DJ |
52 | |
53 | fprintf (stderr, "Process %s created; pid = %d\n", argv[0], | |
a9fa9f7d DJ |
54 | signal_pid); |
55 | ||
56 | signal (SIGTTOU, SIG_IGN); | |
57 | signal (SIGTTIN, SIG_IGN); | |
58 | tcsetpgrp (fileno (stderr), signal_pid); | |
c906108c SS |
59 | |
60 | /* Wait till we are at 1st instruction in program, return signal number. */ | |
0d62e5e8 | 61 | return mywait (statusptr, 0); |
c906108c SS |
62 | } |
63 | ||
45b7b345 DJ |
64 | static int |
65 | attach_inferior (int pid, char *statusptr, unsigned char *sigptr) | |
66 | { | |
67 | /* myattach should return -1 if attaching is unsupported, | |
68 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 69 | |
45b7b345 DJ |
70 | if (myattach (pid) != 0) |
71 | return -1; | |
72 | ||
a9fa9f7d DJ |
73 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
74 | attach function, so that it can be the main thread instead of | |
75 | whichever we were told to attach to. */ | |
76 | signal_pid = pid; | |
77 | ||
0d62e5e8 | 78 | *sigptr = mywait (statusptr, 0); |
45b7b345 DJ |
79 | |
80 | return 0; | |
81 | } | |
82 | ||
c906108c | 83 | extern int remote_debug; |
ce3a066d DJ |
84 | |
85 | /* Handle all of the extended 'q' packets. */ | |
86 | void | |
87 | handle_query (char *own_buf) | |
88 | { | |
0d62e5e8 DJ |
89 | static struct inferior_list_entry *thread_ptr; |
90 | ||
ce3a066d DJ |
91 | if (strcmp ("qSymbol::", own_buf) == 0) |
92 | { | |
2f2893d9 DJ |
93 | if (the_target->look_up_symbols != NULL) |
94 | (*the_target->look_up_symbols) (); | |
95 | ||
ce3a066d DJ |
96 | strcpy (own_buf, "OK"); |
97 | return; | |
98 | } | |
99 | ||
0d62e5e8 DJ |
100 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
101 | { | |
102 | thread_ptr = all_threads.head; | |
103 | sprintf (own_buf, "m%x", thread_ptr->id); | |
104 | thread_ptr = thread_ptr->next; | |
105 | return; | |
106 | } | |
107 | ||
108 | if (strcmp ("qsThreadInfo", own_buf) == 0) | |
109 | { | |
110 | if (thread_ptr != NULL) | |
111 | { | |
112 | sprintf (own_buf, "m%x", thread_ptr->id); | |
113 | thread_ptr = thread_ptr->next; | |
114 | return; | |
115 | } | |
116 | else | |
117 | { | |
118 | sprintf (own_buf, "l"); | |
119 | return; | |
120 | } | |
121 | } | |
122 | ||
ce3a066d DJ |
123 | /* Otherwise we didn't know what packet it was. Say we didn't |
124 | understand it. */ | |
125 | own_buf[0] = 0; | |
126 | } | |
127 | ||
64386c31 DJ |
128 | /* Parse vCont packets. */ |
129 | void | |
130 | handle_v_cont (char *own_buf, char *status, unsigned char *signal) | |
131 | { | |
132 | char *p, *q; | |
133 | int n = 0, i = 0; | |
134 | struct thread_resume *resume_info, default_action; | |
135 | ||
136 | /* Count the number of semicolons in the packet. There should be one | |
137 | for every action. */ | |
138 | p = &own_buf[5]; | |
139 | while (p) | |
140 | { | |
141 | n++; | |
142 | p++; | |
143 | p = strchr (p, ';'); | |
144 | } | |
145 | /* Allocate room for one extra action, for the default remain-stopped | |
146 | behavior; if no default action is in the list, we'll need the extra | |
147 | slot. */ | |
148 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
149 | ||
150 | default_action.thread = -1; | |
151 | default_action.leave_stopped = 1; | |
152 | default_action.step = 0; | |
153 | default_action.sig = 0; | |
154 | ||
155 | p = &own_buf[5]; | |
156 | i = 0; | |
157 | while (*p) | |
158 | { | |
159 | p++; | |
160 | ||
161 | resume_info[i].leave_stopped = 0; | |
162 | ||
163 | if (p[0] == 's' || p[0] == 'S') | |
164 | resume_info[i].step = 1; | |
165 | else if (p[0] == 'c' || p[0] == 'C') | |
166 | resume_info[i].step = 0; | |
167 | else | |
168 | goto err; | |
169 | ||
170 | if (p[0] == 'S' || p[0] == 'C') | |
171 | { | |
172 | int sig; | |
173 | sig = strtol (p + 1, &q, 16); | |
174 | if (p == q) | |
175 | goto err; | |
176 | p = q; | |
177 | ||
178 | if (!target_signal_to_host_p (sig)) | |
179 | goto err; | |
180 | resume_info[i].sig = target_signal_to_host (sig); | |
181 | } | |
182 | else | |
183 | { | |
184 | resume_info[i].sig = 0; | |
185 | p = p + 1; | |
186 | } | |
187 | ||
188 | if (p[0] == 0) | |
189 | { | |
190 | resume_info[i].thread = -1; | |
191 | default_action = resume_info[i]; | |
192 | ||
193 | /* Note: we don't increment i here, we'll overwrite this entry | |
194 | the next time through. */ | |
195 | } | |
196 | else if (p[0] == ':') | |
197 | { | |
198 | resume_info[i].thread = strtol (p + 1, &q, 16); | |
199 | if (p == q) | |
200 | goto err; | |
201 | p = q; | |
202 | if (p[0] != ';' && p[0] != 0) | |
203 | goto err; | |
204 | ||
205 | i++; | |
206 | } | |
207 | } | |
208 | ||
209 | resume_info[i] = default_action; | |
210 | ||
211 | /* Still used in occasional places in the backend. */ | |
212 | if (n == 1 && resume_info[0].thread != -1) | |
213 | cont_thread = resume_info[0].thread; | |
214 | else | |
215 | cont_thread = -1; | |
216 | ||
217 | (*the_target->resume) (resume_info); | |
218 | ||
219 | free (resume_info); | |
220 | ||
221 | *signal = mywait (status, 1); | |
222 | prepare_resume_reply (own_buf, *status, *signal); | |
223 | return; | |
224 | ||
225 | err: | |
226 | /* No other way to report an error... */ | |
227 | strcpy (own_buf, ""); | |
228 | free (resume_info); | |
229 | return; | |
230 | } | |
231 | ||
232 | /* Handle all of the extended 'v' packets. */ | |
233 | void | |
234 | handle_v_requests (char *own_buf, char *status, unsigned char *signal) | |
235 | { | |
236 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
237 | { | |
238 | handle_v_cont (own_buf, status, signal); | |
239 | return; | |
240 | } | |
241 | ||
242 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
243 | { | |
244 | strcpy (own_buf, "vCont;c;C;s;S"); | |
245 | return; | |
246 | } | |
247 | ||
248 | /* Otherwise we didn't know what packet it was. Say we didn't | |
249 | understand it. */ | |
250 | own_buf[0] = 0; | |
251 | return; | |
252 | } | |
253 | ||
254 | void | |
255 | myresume (int step, int sig) | |
256 | { | |
257 | struct thread_resume resume_info[2]; | |
258 | int n = 0; | |
259 | ||
260 | if (step || sig || cont_thread > 0) | |
261 | { | |
262 | resume_info[0].thread | |
263 | = ((struct inferior_list_entry *) current_inferior)->id; | |
264 | resume_info[0].step = step; | |
265 | resume_info[0].sig = sig; | |
266 | resume_info[0].leave_stopped = 0; | |
267 | n++; | |
268 | } | |
269 | resume_info[n].thread = -1; | |
270 | resume_info[n].step = 0; | |
271 | resume_info[n].sig = 0; | |
272 | resume_info[n].leave_stopped = (cont_thread > 0); | |
273 | ||
274 | (*the_target->resume) (resume_info); | |
275 | } | |
276 | ||
0729219d | 277 | static int attached; |
c906108c | 278 | |
0bc68c49 DJ |
279 | static void |
280 | gdbserver_usage (void) | |
281 | { | |
282 | error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" | |
283 | "\tgdbserver COMM --attach PID\n" | |
284 | "\n" | |
285 | "COMM may either be a tty device (for serial debugging), or \n" | |
286 | "HOST:PORT to listen for a TCP connection.\n"); | |
287 | } | |
288 | ||
c906108c | 289 | int |
da85418c | 290 | main (int argc, char *argv[]) |
c906108c | 291 | { |
0a30fbc4 | 292 | char ch, status, *own_buf, mem_buf[2000]; |
c906108c SS |
293 | int i = 0; |
294 | unsigned char signal; | |
295 | unsigned int len; | |
296 | CORE_ADDR mem_addr; | |
0729219d DJ |
297 | int bad_attach; |
298 | int pid; | |
45b7b345 | 299 | char *arg_end; |
c906108c | 300 | |
c5aa993b | 301 | if (setjmp (toplevel)) |
c906108c | 302 | { |
c5aa993b JM |
303 | fprintf (stderr, "Exiting\n"); |
304 | exit (1); | |
c906108c SS |
305 | } |
306 | ||
0729219d DJ |
307 | bad_attach = 0; |
308 | pid = 0; | |
309 | attached = 0; | |
45b7b345 DJ |
310 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
311 | { | |
312 | if (argc == 4 | |
313 | && argv[3] != '\0' | |
314 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 | |
315 | && *arg_end == '\0') | |
316 | { | |
317 | ; | |
318 | } | |
319 | else | |
320 | bad_attach = 1; | |
321 | } | |
322 | ||
323 | if (argc < 3 || bad_attach) | |
0bc68c49 | 324 | gdbserver_usage(); |
c906108c | 325 | |
4ce44c66 JM |
326 | initialize_low (); |
327 | ||
0a30fbc4 DJ |
328 | own_buf = malloc (PBUFSIZ); |
329 | ||
45b7b345 DJ |
330 | if (pid == 0) |
331 | { | |
332 | /* Wait till we are at first instruction in program. */ | |
333 | signal = start_inferior (&argv[2], &status); | |
c906108c | 334 | |
45b7b345 DJ |
335 | /* We are now stopped at the first instruction of the target process */ |
336 | } | |
337 | else | |
338 | { | |
339 | switch (attach_inferior (pid, &status, &signal)) | |
340 | { | |
341 | case -1: | |
342 | error ("Attaching not supported on this target"); | |
343 | break; | |
344 | default: | |
345 | attached = 1; | |
346 | break; | |
347 | } | |
348 | } | |
c906108c SS |
349 | |
350 | while (1) | |
351 | { | |
352 | remote_open (argv[1]); | |
353 | ||
c5aa993b JM |
354 | restart: |
355 | setjmp (toplevel); | |
c906108c SS |
356 | while (getpkt (own_buf) > 0) |
357 | { | |
358 | unsigned char sig; | |
359 | i = 0; | |
360 | ch = own_buf[i++]; | |
361 | switch (ch) | |
362 | { | |
ce3a066d DJ |
363 | case 'q': |
364 | handle_query (own_buf); | |
365 | break; | |
c906108c SS |
366 | case 'd': |
367 | remote_debug = !remote_debug; | |
368 | break; | |
6ad8ae5c DJ |
369 | case 'D': |
370 | fprintf (stderr, "Detaching from inferior\n"); | |
371 | detach_inferior (); | |
372 | write_ok (own_buf); | |
373 | putpkt (own_buf); | |
374 | remote_close (); | |
375 | ||
376 | /* If we are attached, then we can exit. Otherwise, we need to | |
377 | hang around doing nothing, until the child is gone. */ | |
378 | if (!attached) | |
379 | { | |
380 | int status, ret; | |
381 | ||
382 | do { | |
383 | ret = waitpid (signal_pid, &status, 0); | |
384 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
385 | break; | |
386 | } while (ret != -1 || errno != ECHILD); | |
387 | } | |
388 | ||
389 | exit (0); | |
390 | ||
c906108c | 391 | case '!': |
45b7b345 DJ |
392 | if (attached == 0) |
393 | { | |
394 | extended_protocol = 1; | |
395 | prepare_resume_reply (own_buf, status, signal); | |
396 | } | |
397 | else | |
398 | { | |
399 | /* We can not use the extended protocol if we are | |
400 | attached, because we can not restart the running | |
401 | program. So return unrecognized. */ | |
402 | own_buf[0] = '\0'; | |
403 | } | |
c906108c SS |
404 | break; |
405 | case '?': | |
406 | prepare_resume_reply (own_buf, status, signal); | |
407 | break; | |
408 | case 'H': | |
409 | switch (own_buf[1]) | |
410 | { | |
411 | case 'g': | |
412 | general_thread = strtol (&own_buf[2], NULL, 16); | |
413 | write_ok (own_buf); | |
0d62e5e8 | 414 | set_desired_inferior (1); |
c906108c SS |
415 | break; |
416 | case 'c': | |
417 | cont_thread = strtol (&own_buf[2], NULL, 16); | |
418 | write_ok (own_buf); | |
419 | break; | |
0d62e5e8 DJ |
420 | case 's': |
421 | step_thread = strtol (&own_buf[2], NULL, 16); | |
422 | write_ok (own_buf); | |
423 | break; | |
c906108c SS |
424 | default: |
425 | /* Silently ignore it so that gdb can extend the protocol | |
426 | without compatibility headaches. */ | |
427 | own_buf[0] = '\0'; | |
428 | break; | |
429 | } | |
430 | break; | |
431 | case 'g': | |
0d62e5e8 | 432 | set_desired_inferior (1); |
0a30fbc4 | 433 | registers_to_string (own_buf); |
c906108c SS |
434 | break; |
435 | case 'G': | |
0d62e5e8 | 436 | set_desired_inferior (1); |
0a30fbc4 | 437 | registers_from_string (&own_buf[1]); |
c906108c SS |
438 | write_ok (own_buf); |
439 | break; | |
440 | case 'm': | |
441 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
442 | read_inferior_memory (mem_addr, mem_buf, len); | |
443 | convert_int_to_ascii (mem_buf, own_buf, len); | |
444 | break; | |
445 | case 'M': | |
446 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
447 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
448 | write_ok (own_buf); | |
449 | else | |
450 | write_enn (own_buf); | |
451 | break; | |
452 | case 'C': | |
453 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
454 | if (target_signal_to_host_p (sig)) |
455 | signal = target_signal_to_host (sig); | |
456 | else | |
457 | signal = 0; | |
0d62e5e8 | 458 | set_desired_inferior (0); |
0e98d0a7 | 459 | myresume (0, signal); |
0d62e5e8 | 460 | signal = mywait (&status, 1); |
c906108c SS |
461 | prepare_resume_reply (own_buf, status, signal); |
462 | break; | |
463 | case 'S': | |
464 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
465 | if (target_signal_to_host_p (sig)) |
466 | signal = target_signal_to_host (sig); | |
467 | else | |
468 | signal = 0; | |
0d62e5e8 | 469 | set_desired_inferior (0); |
0e98d0a7 | 470 | myresume (1, signal); |
0d62e5e8 | 471 | signal = mywait (&status, 1); |
c906108c SS |
472 | prepare_resume_reply (own_buf, status, signal); |
473 | break; | |
474 | case 'c': | |
0d62e5e8 | 475 | set_desired_inferior (0); |
c906108c | 476 | myresume (0, 0); |
0d62e5e8 | 477 | signal = mywait (&status, 1); |
c906108c SS |
478 | prepare_resume_reply (own_buf, status, signal); |
479 | break; | |
480 | case 's': | |
0d62e5e8 | 481 | set_desired_inferior (0); |
c906108c | 482 | myresume (1, 0); |
0d62e5e8 | 483 | signal = mywait (&status, 1); |
c906108c SS |
484 | prepare_resume_reply (own_buf, status, signal); |
485 | break; | |
486 | case 'k': | |
487 | fprintf (stderr, "Killing inferior\n"); | |
488 | kill_inferior (); | |
489 | /* When using the extended protocol, we start up a new | |
c5aa993b | 490 | debugging session. The traditional protocol will |
c906108c SS |
491 | exit instead. */ |
492 | if (extended_protocol) | |
493 | { | |
494 | write_ok (own_buf); | |
495 | fprintf (stderr, "GDBserver restarting\n"); | |
496 | ||
497 | /* Wait till we are at 1st instruction in prog. */ | |
498 | signal = start_inferior (&argv[2], &status); | |
499 | goto restart; | |
500 | break; | |
501 | } | |
502 | else | |
503 | { | |
504 | exit (0); | |
505 | break; | |
506 | } | |
507 | case 'T': | |
508 | if (mythread_alive (strtol (&own_buf[1], NULL, 16))) | |
509 | write_ok (own_buf); | |
510 | else | |
511 | write_enn (own_buf); | |
512 | break; | |
513 | case 'R': | |
514 | /* Restarting the inferior is only supported in the | |
c5aa993b | 515 | extended protocol. */ |
c906108c SS |
516 | if (extended_protocol) |
517 | { | |
518 | kill_inferior (); | |
519 | write_ok (own_buf); | |
520 | fprintf (stderr, "GDBserver restarting\n"); | |
521 | ||
522 | /* Wait till we are at 1st instruction in prog. */ | |
523 | signal = start_inferior (&argv[2], &status); | |
524 | goto restart; | |
525 | break; | |
526 | } | |
527 | else | |
528 | { | |
529 | /* It is a request we don't understand. Respond with an | |
530 | empty packet so that gdb knows that we don't support this | |
531 | request. */ | |
532 | own_buf[0] = '\0'; | |
533 | break; | |
534 | } | |
64386c31 DJ |
535 | case 'v': |
536 | /* Extended (long) request. */ | |
537 | handle_v_requests (own_buf, &status, &signal); | |
538 | break; | |
c906108c SS |
539 | default: |
540 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
541 | empty packet so that gdb knows that we don't support this |
542 | request. */ | |
c906108c SS |
543 | own_buf[0] = '\0'; |
544 | break; | |
545 | } | |
546 | ||
547 | putpkt (own_buf); | |
548 | ||
549 | if (status == 'W') | |
550 | fprintf (stderr, | |
551 | "\nChild exited with status %d\n", sig); | |
552 | if (status == 'X') | |
553 | fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig); | |
554 | if (status == 'W' || status == 'X') | |
555 | { | |
556 | if (extended_protocol) | |
557 | { | |
558 | fprintf (stderr, "Killing inferior\n"); | |
559 | kill_inferior (); | |
560 | write_ok (own_buf); | |
561 | fprintf (stderr, "GDBserver restarting\n"); | |
562 | ||
563 | /* Wait till we are at 1st instruction in prog. */ | |
564 | signal = start_inferior (&argv[2], &status); | |
565 | goto restart; | |
566 | break; | |
567 | } | |
568 | else | |
569 | { | |
570 | fprintf (stderr, "GDBserver exiting\n"); | |
571 | exit (0); | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
576 | /* We come here when getpkt fails. | |
577 | ||
c5aa993b JM |
578 | For the extended remote protocol we exit (and this is the only |
579 | way we gracefully exit!). | |
c906108c | 580 | |
c5aa993b JM |
581 | For the traditional remote protocol close the connection, |
582 | and re-open it at the top of the loop. */ | |
c906108c SS |
583 | if (extended_protocol) |
584 | { | |
585 | remote_close (); | |
586 | exit (0); | |
587 | } | |
588 | else | |
589 | { | |
45b7b345 DJ |
590 | fprintf (stderr, "Remote side has terminated connection. " |
591 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
592 | remote_close (); |
593 | } | |
594 | } | |
595 | } |