]>
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 | } | |
aa691b87 | 107 | |
0d62e5e8 DJ |
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 | } | |
aa691b87 RM |
122 | |
123 | if (the_target->read_auxv != NULL | |
124 | && strncmp ("qPart:auxv:read::", own_buf, 17) == 0) | |
125 | { | |
126 | char data[(PBUFSIZ - 1) / 2]; | |
127 | CORE_ADDR ofs; | |
128 | unsigned int len; | |
129 | int n; | |
130 | decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */ | |
131 | if (len > sizeof data) | |
132 | len = sizeof data; | |
133 | n = (*the_target->read_auxv) (ofs, data, len); | |
134 | if (n == 0) | |
135 | write_ok (own_buf); | |
136 | else if (n < 0) | |
137 | write_enn (own_buf); | |
138 | else | |
139 | convert_int_to_ascii (data, own_buf, n); | |
140 | return; | |
141 | } | |
142 | ||
ce3a066d DJ |
143 | /* Otherwise we didn't know what packet it was. Say we didn't |
144 | understand it. */ | |
145 | own_buf[0] = 0; | |
146 | } | |
147 | ||
64386c31 DJ |
148 | /* Parse vCont packets. */ |
149 | void | |
150 | handle_v_cont (char *own_buf, char *status, unsigned char *signal) | |
151 | { | |
152 | char *p, *q; | |
153 | int n = 0, i = 0; | |
154 | struct thread_resume *resume_info, default_action; | |
155 | ||
156 | /* Count the number of semicolons in the packet. There should be one | |
157 | for every action. */ | |
158 | p = &own_buf[5]; | |
159 | while (p) | |
160 | { | |
161 | n++; | |
162 | p++; | |
163 | p = strchr (p, ';'); | |
164 | } | |
165 | /* Allocate room for one extra action, for the default remain-stopped | |
166 | behavior; if no default action is in the list, we'll need the extra | |
167 | slot. */ | |
168 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
169 | ||
170 | default_action.thread = -1; | |
171 | default_action.leave_stopped = 1; | |
172 | default_action.step = 0; | |
173 | default_action.sig = 0; | |
174 | ||
175 | p = &own_buf[5]; | |
176 | i = 0; | |
177 | while (*p) | |
178 | { | |
179 | p++; | |
180 | ||
181 | resume_info[i].leave_stopped = 0; | |
182 | ||
183 | if (p[0] == 's' || p[0] == 'S') | |
184 | resume_info[i].step = 1; | |
185 | else if (p[0] == 'c' || p[0] == 'C') | |
186 | resume_info[i].step = 0; | |
187 | else | |
188 | goto err; | |
189 | ||
190 | if (p[0] == 'S' || p[0] == 'C') | |
191 | { | |
192 | int sig; | |
193 | sig = strtol (p + 1, &q, 16); | |
194 | if (p == q) | |
195 | goto err; | |
196 | p = q; | |
197 | ||
198 | if (!target_signal_to_host_p (sig)) | |
199 | goto err; | |
200 | resume_info[i].sig = target_signal_to_host (sig); | |
201 | } | |
202 | else | |
203 | { | |
204 | resume_info[i].sig = 0; | |
205 | p = p + 1; | |
206 | } | |
207 | ||
208 | if (p[0] == 0) | |
209 | { | |
210 | resume_info[i].thread = -1; | |
211 | default_action = resume_info[i]; | |
212 | ||
213 | /* Note: we don't increment i here, we'll overwrite this entry | |
214 | the next time through. */ | |
215 | } | |
216 | else if (p[0] == ':') | |
217 | { | |
218 | resume_info[i].thread = strtol (p + 1, &q, 16); | |
219 | if (p == q) | |
220 | goto err; | |
221 | p = q; | |
222 | if (p[0] != ';' && p[0] != 0) | |
223 | goto err; | |
224 | ||
225 | i++; | |
226 | } | |
227 | } | |
228 | ||
229 | resume_info[i] = default_action; | |
230 | ||
231 | /* Still used in occasional places in the backend. */ | |
232 | if (n == 1 && resume_info[0].thread != -1) | |
233 | cont_thread = resume_info[0].thread; | |
234 | else | |
235 | cont_thread = -1; | |
236 | ||
237 | (*the_target->resume) (resume_info); | |
238 | ||
239 | free (resume_info); | |
240 | ||
241 | *signal = mywait (status, 1); | |
242 | prepare_resume_reply (own_buf, *status, *signal); | |
243 | return; | |
244 | ||
245 | err: | |
246 | /* No other way to report an error... */ | |
247 | strcpy (own_buf, ""); | |
248 | free (resume_info); | |
249 | return; | |
250 | } | |
251 | ||
252 | /* Handle all of the extended 'v' packets. */ | |
253 | void | |
254 | handle_v_requests (char *own_buf, char *status, unsigned char *signal) | |
255 | { | |
256 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
257 | { | |
258 | handle_v_cont (own_buf, status, signal); | |
259 | return; | |
260 | } | |
261 | ||
262 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
263 | { | |
264 | strcpy (own_buf, "vCont;c;C;s;S"); | |
265 | return; | |
266 | } | |
267 | ||
268 | /* Otherwise we didn't know what packet it was. Say we didn't | |
269 | understand it. */ | |
270 | own_buf[0] = 0; | |
271 | return; | |
272 | } | |
273 | ||
274 | void | |
275 | myresume (int step, int sig) | |
276 | { | |
277 | struct thread_resume resume_info[2]; | |
278 | int n = 0; | |
279 | ||
280 | if (step || sig || cont_thread > 0) | |
281 | { | |
282 | resume_info[0].thread | |
283 | = ((struct inferior_list_entry *) current_inferior)->id; | |
284 | resume_info[0].step = step; | |
285 | resume_info[0].sig = sig; | |
286 | resume_info[0].leave_stopped = 0; | |
287 | n++; | |
288 | } | |
289 | resume_info[n].thread = -1; | |
290 | resume_info[n].step = 0; | |
291 | resume_info[n].sig = 0; | |
292 | resume_info[n].leave_stopped = (cont_thread > 0); | |
293 | ||
294 | (*the_target->resume) (resume_info); | |
295 | } | |
296 | ||
0729219d | 297 | static int attached; |
c906108c | 298 | |
0bc68c49 DJ |
299 | static void |
300 | gdbserver_usage (void) | |
301 | { | |
302 | error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" | |
303 | "\tgdbserver COMM --attach PID\n" | |
304 | "\n" | |
305 | "COMM may either be a tty device (for serial debugging), or \n" | |
306 | "HOST:PORT to listen for a TCP connection.\n"); | |
307 | } | |
308 | ||
c906108c | 309 | int |
da85418c | 310 | main (int argc, char *argv[]) |
c906108c | 311 | { |
0a30fbc4 | 312 | char ch, status, *own_buf, mem_buf[2000]; |
c906108c SS |
313 | int i = 0; |
314 | unsigned char signal; | |
315 | unsigned int len; | |
316 | CORE_ADDR mem_addr; | |
0729219d DJ |
317 | int bad_attach; |
318 | int pid; | |
45b7b345 | 319 | char *arg_end; |
c906108c | 320 | |
c5aa993b | 321 | if (setjmp (toplevel)) |
c906108c | 322 | { |
c5aa993b JM |
323 | fprintf (stderr, "Exiting\n"); |
324 | exit (1); | |
c906108c SS |
325 | } |
326 | ||
0729219d DJ |
327 | bad_attach = 0; |
328 | pid = 0; | |
329 | attached = 0; | |
45b7b345 DJ |
330 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
331 | { | |
332 | if (argc == 4 | |
333 | && argv[3] != '\0' | |
334 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 | |
335 | && *arg_end == '\0') | |
336 | { | |
337 | ; | |
338 | } | |
339 | else | |
340 | bad_attach = 1; | |
341 | } | |
342 | ||
343 | if (argc < 3 || bad_attach) | |
0bc68c49 | 344 | gdbserver_usage(); |
c906108c | 345 | |
4ce44c66 JM |
346 | initialize_low (); |
347 | ||
0a30fbc4 DJ |
348 | own_buf = malloc (PBUFSIZ); |
349 | ||
45b7b345 DJ |
350 | if (pid == 0) |
351 | { | |
352 | /* Wait till we are at first instruction in program. */ | |
353 | signal = start_inferior (&argv[2], &status); | |
c906108c | 354 | |
45b7b345 DJ |
355 | /* We are now stopped at the first instruction of the target process */ |
356 | } | |
357 | else | |
358 | { | |
359 | switch (attach_inferior (pid, &status, &signal)) | |
360 | { | |
361 | case -1: | |
362 | error ("Attaching not supported on this target"); | |
363 | break; | |
364 | default: | |
365 | attached = 1; | |
366 | break; | |
367 | } | |
368 | } | |
c906108c SS |
369 | |
370 | while (1) | |
371 | { | |
372 | remote_open (argv[1]); | |
373 | ||
c5aa993b JM |
374 | restart: |
375 | setjmp (toplevel); | |
c906108c SS |
376 | while (getpkt (own_buf) > 0) |
377 | { | |
378 | unsigned char sig; | |
379 | i = 0; | |
380 | ch = own_buf[i++]; | |
381 | switch (ch) | |
382 | { | |
ce3a066d DJ |
383 | case 'q': |
384 | handle_query (own_buf); | |
385 | break; | |
c906108c SS |
386 | case 'd': |
387 | remote_debug = !remote_debug; | |
388 | break; | |
6ad8ae5c DJ |
389 | case 'D': |
390 | fprintf (stderr, "Detaching from inferior\n"); | |
391 | detach_inferior (); | |
392 | write_ok (own_buf); | |
393 | putpkt (own_buf); | |
aa691b87 | 394 | remote_close (); |
6ad8ae5c DJ |
395 | |
396 | /* If we are attached, then we can exit. Otherwise, we need to | |
397 | hang around doing nothing, until the child is gone. */ | |
398 | if (!attached) | |
399 | { | |
400 | int status, ret; | |
401 | ||
402 | do { | |
403 | ret = waitpid (signal_pid, &status, 0); | |
404 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
405 | break; | |
406 | } while (ret != -1 || errno != ECHILD); | |
407 | } | |
408 | ||
409 | exit (0); | |
410 | ||
c906108c | 411 | case '!': |
45b7b345 DJ |
412 | if (attached == 0) |
413 | { | |
414 | extended_protocol = 1; | |
415 | prepare_resume_reply (own_buf, status, signal); | |
416 | } | |
417 | else | |
418 | { | |
419 | /* We can not use the extended protocol if we are | |
420 | attached, because we can not restart the running | |
421 | program. So return unrecognized. */ | |
422 | own_buf[0] = '\0'; | |
423 | } | |
c906108c SS |
424 | break; |
425 | case '?': | |
426 | prepare_resume_reply (own_buf, status, signal); | |
427 | break; | |
428 | case 'H': | |
429 | switch (own_buf[1]) | |
430 | { | |
431 | case 'g': | |
432 | general_thread = strtol (&own_buf[2], NULL, 16); | |
433 | write_ok (own_buf); | |
0d62e5e8 | 434 | set_desired_inferior (1); |
c906108c SS |
435 | break; |
436 | case 'c': | |
437 | cont_thread = strtol (&own_buf[2], NULL, 16); | |
438 | write_ok (own_buf); | |
439 | break; | |
0d62e5e8 DJ |
440 | case 's': |
441 | step_thread = strtol (&own_buf[2], NULL, 16); | |
442 | write_ok (own_buf); | |
443 | break; | |
c906108c SS |
444 | default: |
445 | /* Silently ignore it so that gdb can extend the protocol | |
446 | without compatibility headaches. */ | |
447 | own_buf[0] = '\0'; | |
448 | break; | |
449 | } | |
450 | break; | |
451 | case 'g': | |
0d62e5e8 | 452 | set_desired_inferior (1); |
0a30fbc4 | 453 | registers_to_string (own_buf); |
c906108c SS |
454 | break; |
455 | case 'G': | |
0d62e5e8 | 456 | set_desired_inferior (1); |
0a30fbc4 | 457 | registers_from_string (&own_buf[1]); |
c906108c SS |
458 | write_ok (own_buf); |
459 | break; | |
460 | case 'm': | |
461 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
462 | read_inferior_memory (mem_addr, mem_buf, len); | |
463 | convert_int_to_ascii (mem_buf, own_buf, len); | |
464 | break; | |
465 | case 'M': | |
466 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
467 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
468 | write_ok (own_buf); | |
469 | else | |
470 | write_enn (own_buf); | |
471 | break; | |
472 | case 'C': | |
473 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
474 | if (target_signal_to_host_p (sig)) |
475 | signal = target_signal_to_host (sig); | |
476 | else | |
477 | signal = 0; | |
0d62e5e8 | 478 | set_desired_inferior (0); |
0e98d0a7 | 479 | myresume (0, signal); |
0d62e5e8 | 480 | signal = mywait (&status, 1); |
c906108c SS |
481 | prepare_resume_reply (own_buf, status, signal); |
482 | break; | |
483 | case 'S': | |
484 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
485 | if (target_signal_to_host_p (sig)) |
486 | signal = target_signal_to_host (sig); | |
487 | else | |
488 | signal = 0; | |
0d62e5e8 | 489 | set_desired_inferior (0); |
0e98d0a7 | 490 | myresume (1, signal); |
0d62e5e8 | 491 | signal = mywait (&status, 1); |
c906108c SS |
492 | prepare_resume_reply (own_buf, status, signal); |
493 | break; | |
494 | case 'c': | |
0d62e5e8 | 495 | set_desired_inferior (0); |
c906108c | 496 | myresume (0, 0); |
0d62e5e8 | 497 | signal = mywait (&status, 1); |
c906108c SS |
498 | prepare_resume_reply (own_buf, status, signal); |
499 | break; | |
500 | case 's': | |
0d62e5e8 | 501 | set_desired_inferior (0); |
c906108c | 502 | myresume (1, 0); |
0d62e5e8 | 503 | signal = mywait (&status, 1); |
c906108c SS |
504 | prepare_resume_reply (own_buf, status, signal); |
505 | break; | |
506 | case 'k': | |
507 | fprintf (stderr, "Killing inferior\n"); | |
508 | kill_inferior (); | |
509 | /* When using the extended protocol, we start up a new | |
c5aa993b | 510 | debugging session. The traditional protocol will |
c906108c SS |
511 | exit instead. */ |
512 | if (extended_protocol) | |
513 | { | |
514 | write_ok (own_buf); | |
515 | fprintf (stderr, "GDBserver restarting\n"); | |
516 | ||
517 | /* Wait till we are at 1st instruction in prog. */ | |
518 | signal = start_inferior (&argv[2], &status); | |
519 | goto restart; | |
520 | break; | |
521 | } | |
522 | else | |
523 | { | |
524 | exit (0); | |
525 | break; | |
526 | } | |
527 | case 'T': | |
528 | if (mythread_alive (strtol (&own_buf[1], NULL, 16))) | |
529 | write_ok (own_buf); | |
530 | else | |
531 | write_enn (own_buf); | |
532 | break; | |
533 | case 'R': | |
534 | /* Restarting the inferior is only supported in the | |
c5aa993b | 535 | extended protocol. */ |
c906108c SS |
536 | if (extended_protocol) |
537 | { | |
538 | kill_inferior (); | |
539 | write_ok (own_buf); | |
540 | fprintf (stderr, "GDBserver restarting\n"); | |
541 | ||
542 | /* Wait till we are at 1st instruction in prog. */ | |
543 | signal = start_inferior (&argv[2], &status); | |
544 | goto restart; | |
545 | break; | |
546 | } | |
547 | else | |
548 | { | |
549 | /* It is a request we don't understand. Respond with an | |
550 | empty packet so that gdb knows that we don't support this | |
551 | request. */ | |
552 | own_buf[0] = '\0'; | |
553 | break; | |
554 | } | |
64386c31 DJ |
555 | case 'v': |
556 | /* Extended (long) request. */ | |
557 | handle_v_requests (own_buf, &status, &signal); | |
558 | break; | |
c906108c SS |
559 | default: |
560 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
561 | empty packet so that gdb knows that we don't support this |
562 | request. */ | |
c906108c SS |
563 | own_buf[0] = '\0'; |
564 | break; | |
565 | } | |
566 | ||
567 | putpkt (own_buf); | |
568 | ||
569 | if (status == 'W') | |
570 | fprintf (stderr, | |
571 | "\nChild exited with status %d\n", sig); | |
572 | if (status == 'X') | |
573 | fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig); | |
574 | if (status == 'W' || status == 'X') | |
575 | { | |
576 | if (extended_protocol) | |
577 | { | |
578 | fprintf (stderr, "Killing inferior\n"); | |
579 | kill_inferior (); | |
580 | write_ok (own_buf); | |
581 | fprintf (stderr, "GDBserver restarting\n"); | |
582 | ||
583 | /* Wait till we are at 1st instruction in prog. */ | |
584 | signal = start_inferior (&argv[2], &status); | |
585 | goto restart; | |
586 | break; | |
587 | } | |
588 | else | |
589 | { | |
590 | fprintf (stderr, "GDBserver exiting\n"); | |
591 | exit (0); | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | /* We come here when getpkt fails. | |
597 | ||
c5aa993b JM |
598 | For the extended remote protocol we exit (and this is the only |
599 | way we gracefully exit!). | |
c906108c | 600 | |
c5aa993b JM |
601 | For the traditional remote protocol close the connection, |
602 | and re-open it at the top of the loop. */ | |
c906108c SS |
603 | if (extended_protocol) |
604 | { | |
605 | remote_close (); | |
606 | exit (0); | |
607 | } | |
608 | else | |
609 | { | |
45b7b345 DJ |
610 | fprintf (stderr, "Remote side has terminated connection. " |
611 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
612 | remote_close (); |
613 | } | |
614 | } | |
615 | } |