gdb/
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004,
3 2005, 2006
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "server.h"
24
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/wait.h>
28
29 unsigned long cont_thread;
30 unsigned long general_thread;
31 unsigned long step_thread;
32 unsigned long thread_from_wait;
33 unsigned long old_thread_from_wait;
34 int extended_protocol;
35 int server_waiting;
36
37 jmp_buf toplevel;
38
39 /* The PID of the originally created or attached inferior. Used to
40 send signals to the process when GDB sends us an asynchronous interrupt
41 (user hitting Control-C in the client), and to wait for the child to exit
42 when no longer debugging it. */
43
44 unsigned long signal_pid;
45
46 static int
47 start_inferior (char *argv[], char *statusptr)
48 {
49 signal (SIGTTOU, SIG_DFL);
50 signal (SIGTTIN, SIG_DFL);
51
52 signal_pid = create_inferior (argv[0], argv);
53
54 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
55 signal_pid);
56
57 signal (SIGTTOU, SIG_IGN);
58 signal (SIGTTIN, SIG_IGN);
59 tcsetpgrp (fileno (stderr), signal_pid);
60
61 /* Wait till we are at 1st instruction in program, return signal number. */
62 return mywait (statusptr, 0);
63 }
64
65 static int
66 attach_inferior (int pid, char *statusptr, int *sigptr)
67 {
68 /* myattach should return -1 if attaching is unsupported,
69 0 if it succeeded, and call error() otherwise. */
70
71 if (myattach (pid) != 0)
72 return -1;
73
74 fprintf (stderr, "Attached; pid = %d\n", pid);
75
76 /* FIXME - It may be that we should get the SIGNAL_PID from the
77 attach function, so that it can be the main thread instead of
78 whichever we were told to attach to. */
79 signal_pid = pid;
80
81 *sigptr = mywait (statusptr, 0);
82
83 /* GDB knows to ignore the first SIGSTOP after attaching to a running
84 process using the "attach" command, but this is different; it's
85 just using "target remote". Pretend it's just starting up. */
86 if (*statusptr == 'T' && *sigptr == SIGSTOP)
87 *sigptr = SIGTRAP;
88
89 return 0;
90 }
91
92 extern int remote_debug;
93
94 /* Handle all of the extended 'q' packets. */
95 void
96 handle_query (char *own_buf)
97 {
98 static struct inferior_list_entry *thread_ptr;
99
100 if (strcmp ("qSymbol::", own_buf) == 0)
101 {
102 if (the_target->look_up_symbols != NULL)
103 (*the_target->look_up_symbols) ();
104
105 strcpy (own_buf, "OK");
106 return;
107 }
108
109 if (strcmp ("qfThreadInfo", own_buf) == 0)
110 {
111 thread_ptr = all_threads.head;
112 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
113 thread_ptr = thread_ptr->next;
114 return;
115 }
116
117 if (strcmp ("qsThreadInfo", own_buf) == 0)
118 {
119 if (thread_ptr != NULL)
120 {
121 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
122 thread_ptr = thread_ptr->next;
123 return;
124 }
125 else
126 {
127 sprintf (own_buf, "l");
128 return;
129 }
130 }
131
132 if (the_target->read_offsets != NULL
133 && strcmp ("qOffsets", own_buf) == 0)
134 {
135 CORE_ADDR text, data;
136
137 if (the_target->read_offsets (&text, &data))
138 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
139 (long)text, (long)data, (long)data);
140 else
141 write_enn (own_buf);
142
143 return;
144 }
145
146 if (the_target->read_auxv != NULL
147 && strncmp ("qPart:auxv:read::", own_buf, 17) == 0)
148 {
149 unsigned char data[(PBUFSIZ - 1) / 2];
150 CORE_ADDR ofs;
151 unsigned int len;
152 int n;
153 decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */
154 if (len > sizeof data)
155 len = sizeof data;
156 n = (*the_target->read_auxv) (ofs, data, len);
157 if (n == 0)
158 write_ok (own_buf);
159 else if (n < 0)
160 write_enn (own_buf);
161 else
162 convert_int_to_ascii (data, own_buf, n);
163 return;
164 }
165
166 /* Protocol features query. */
167 if (strncmp ("qSupported", own_buf, 10) == 0
168 && (own_buf[10] == ':' || own_buf[10] == '\0'))
169 {
170 sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1);
171 return;
172 }
173
174 /* Otherwise we didn't know what packet it was. Say we didn't
175 understand it. */
176 own_buf[0] = 0;
177 }
178
179 /* Parse vCont packets. */
180 void
181 handle_v_cont (char *own_buf, char *status, int *signal)
182 {
183 char *p, *q;
184 int n = 0, i = 0;
185 struct thread_resume *resume_info, default_action;
186
187 /* Count the number of semicolons in the packet. There should be one
188 for every action. */
189 p = &own_buf[5];
190 while (p)
191 {
192 n++;
193 p++;
194 p = strchr (p, ';');
195 }
196 /* Allocate room for one extra action, for the default remain-stopped
197 behavior; if no default action is in the list, we'll need the extra
198 slot. */
199 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
200
201 default_action.thread = -1;
202 default_action.leave_stopped = 1;
203 default_action.step = 0;
204 default_action.sig = 0;
205
206 p = &own_buf[5];
207 i = 0;
208 while (*p)
209 {
210 p++;
211
212 resume_info[i].leave_stopped = 0;
213
214 if (p[0] == 's' || p[0] == 'S')
215 resume_info[i].step = 1;
216 else if (p[0] == 'c' || p[0] == 'C')
217 resume_info[i].step = 0;
218 else
219 goto err;
220
221 if (p[0] == 'S' || p[0] == 'C')
222 {
223 int sig;
224 sig = strtol (p + 1, &q, 16);
225 if (p == q)
226 goto err;
227 p = q;
228
229 if (!target_signal_to_host_p (sig))
230 goto err;
231 resume_info[i].sig = target_signal_to_host (sig);
232 }
233 else
234 {
235 resume_info[i].sig = 0;
236 p = p + 1;
237 }
238
239 if (p[0] == 0)
240 {
241 resume_info[i].thread = -1;
242 default_action = resume_info[i];
243
244 /* Note: we don't increment i here, we'll overwrite this entry
245 the next time through. */
246 }
247 else if (p[0] == ':')
248 {
249 unsigned int gdb_id = strtoul (p + 1, &q, 16);
250 unsigned long thread_id;
251
252 if (p == q)
253 goto err;
254 p = q;
255 if (p[0] != ';' && p[0] != 0)
256 goto err;
257
258 thread_id = gdb_id_to_thread_id (gdb_id);
259 if (thread_id)
260 resume_info[i].thread = thread_id;
261 else
262 goto err;
263
264 i++;
265 }
266 }
267
268 resume_info[i] = default_action;
269
270 /* Still used in occasional places in the backend. */
271 if (n == 1 && resume_info[0].thread != -1)
272 cont_thread = resume_info[0].thread;
273 else
274 cont_thread = -1;
275 set_desired_inferior (0);
276
277 (*the_target->resume) (resume_info);
278
279 free (resume_info);
280
281 *signal = mywait (status, 1);
282 prepare_resume_reply (own_buf, *status, *signal);
283 return;
284
285 err:
286 /* No other way to report an error... */
287 strcpy (own_buf, "");
288 free (resume_info);
289 return;
290 }
291
292 /* Handle all of the extended 'v' packets. */
293 void
294 handle_v_requests (char *own_buf, char *status, int *signal)
295 {
296 if (strncmp (own_buf, "vCont;", 6) == 0)
297 {
298 handle_v_cont (own_buf, status, signal);
299 return;
300 }
301
302 if (strncmp (own_buf, "vCont?", 6) == 0)
303 {
304 strcpy (own_buf, "vCont;c;C;s;S");
305 return;
306 }
307
308 /* Otherwise we didn't know what packet it was. Say we didn't
309 understand it. */
310 own_buf[0] = 0;
311 return;
312 }
313
314 void
315 myresume (int step, int sig)
316 {
317 struct thread_resume resume_info[2];
318 int n = 0;
319
320 if (step || sig || (cont_thread != 0 && cont_thread != -1))
321 {
322 resume_info[0].thread
323 = ((struct inferior_list_entry *) current_inferior)->id;
324 resume_info[0].step = step;
325 resume_info[0].sig = sig;
326 resume_info[0].leave_stopped = 0;
327 n++;
328 }
329 resume_info[n].thread = -1;
330 resume_info[n].step = 0;
331 resume_info[n].sig = 0;
332 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
333
334 (*the_target->resume) (resume_info);
335 }
336
337 static int attached;
338
339 static void
340 gdbserver_version (void)
341 {
342 printf ("GNU gdbserver %s\n"
343 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
344 "gdbserver is free software, covered by the GNU General Public License.\n"
345 "This gdbserver was configured as \"%s\"\n",
346 version, host_name);
347 }
348
349 static void
350 gdbserver_usage (void)
351 {
352 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
353 "\tgdbserver COMM --attach PID\n"
354 "\n"
355 "COMM may either be a tty device (for serial debugging), or \n"
356 "HOST:PORT to listen for a TCP connection.\n");
357 }
358
359 int
360 main (int argc, char *argv[])
361 {
362 char ch, status, *own_buf;
363 unsigned char *mem_buf;
364 int i = 0;
365 int signal;
366 unsigned int len;
367 CORE_ADDR mem_addr;
368 int bad_attach;
369 int pid;
370 char *arg_end;
371
372 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
373 {
374 gdbserver_version ();
375 exit (0);
376 }
377
378 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
379 {
380 gdbserver_usage ();
381 exit (0);
382 }
383
384 if (setjmp (toplevel))
385 {
386 fprintf (stderr, "Exiting\n");
387 exit (1);
388 }
389
390 bad_attach = 0;
391 pid = 0;
392 attached = 0;
393 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
394 {
395 if (argc == 4
396 && argv[3] != '\0'
397 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
398 && *arg_end == '\0')
399 {
400 ;
401 }
402 else
403 bad_attach = 1;
404 }
405
406 if (argc < 3 || bad_attach)
407 {
408 gdbserver_usage ();
409 exit (1);
410 }
411
412 initialize_low ();
413
414 own_buf = malloc (PBUFSIZ);
415 mem_buf = malloc (PBUFSIZ);
416
417 if (pid == 0)
418 {
419 /* Wait till we are at first instruction in program. */
420 signal = start_inferior (&argv[2], &status);
421
422 /* We are now stopped at the first instruction of the target process */
423 }
424 else
425 {
426 switch (attach_inferior (pid, &status, &signal))
427 {
428 case -1:
429 error ("Attaching not supported on this target");
430 break;
431 default:
432 attached = 1;
433 break;
434 }
435 }
436
437 while (1)
438 {
439 remote_open (argv[1]);
440
441 restart:
442 setjmp (toplevel);
443 while (getpkt (own_buf) > 0)
444 {
445 unsigned char sig;
446 i = 0;
447 ch = own_buf[i++];
448 switch (ch)
449 {
450 case 'q':
451 handle_query (own_buf);
452 break;
453 case 'd':
454 remote_debug = !remote_debug;
455 break;
456 case 'D':
457 fprintf (stderr, "Detaching from inferior\n");
458 detach_inferior ();
459 write_ok (own_buf);
460 putpkt (own_buf);
461 remote_close ();
462
463 /* If we are attached, then we can exit. Otherwise, we need to
464 hang around doing nothing, until the child is gone. */
465 if (!attached)
466 {
467 int status, ret;
468
469 do {
470 ret = waitpid (signal_pid, &status, 0);
471 if (WIFEXITED (status) || WIFSIGNALED (status))
472 break;
473 } while (ret != -1 || errno != ECHILD);
474 }
475
476 exit (0);
477
478 case '!':
479 if (attached == 0)
480 {
481 extended_protocol = 1;
482 prepare_resume_reply (own_buf, status, signal);
483 }
484 else
485 {
486 /* We can not use the extended protocol if we are
487 attached, because we can not restart the running
488 program. So return unrecognized. */
489 own_buf[0] = '\0';
490 }
491 break;
492 case '?':
493 prepare_resume_reply (own_buf, status, signal);
494 break;
495 case 'H':
496 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
497 {
498 unsigned long gdb_id, thread_id;
499
500 gdb_id = strtoul (&own_buf[2], NULL, 16);
501 thread_id = gdb_id_to_thread_id (gdb_id);
502 if (thread_id == 0)
503 {
504 write_enn (own_buf);
505 break;
506 }
507
508 if (own_buf[1] == 'g')
509 {
510 general_thread = thread_id;
511 set_desired_inferior (1);
512 }
513 else if (own_buf[1] == 'c')
514 cont_thread = thread_id;
515 else if (own_buf[1] == 's')
516 step_thread = thread_id;
517
518 write_ok (own_buf);
519 }
520 else
521 {
522 /* Silently ignore it so that gdb can extend the protocol
523 without compatibility headaches. */
524 own_buf[0] = '\0';
525 }
526 break;
527 case 'g':
528 set_desired_inferior (1);
529 registers_to_string (own_buf);
530 break;
531 case 'G':
532 set_desired_inferior (1);
533 registers_from_string (&own_buf[1]);
534 write_ok (own_buf);
535 break;
536 case 'm':
537 decode_m_packet (&own_buf[1], &mem_addr, &len);
538 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
539 convert_int_to_ascii (mem_buf, own_buf, len);
540 else
541 write_enn (own_buf);
542 break;
543 case 'M':
544 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
545 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
546 write_ok (own_buf);
547 else
548 write_enn (own_buf);
549 break;
550 case 'C':
551 convert_ascii_to_int (own_buf + 1, &sig, 1);
552 if (target_signal_to_host_p (sig))
553 signal = target_signal_to_host (sig);
554 else
555 signal = 0;
556 set_desired_inferior (0);
557 myresume (0, signal);
558 signal = mywait (&status, 1);
559 prepare_resume_reply (own_buf, status, signal);
560 break;
561 case 'S':
562 convert_ascii_to_int (own_buf + 1, &sig, 1);
563 if (target_signal_to_host_p (sig))
564 signal = target_signal_to_host (sig);
565 else
566 signal = 0;
567 set_desired_inferior (0);
568 myresume (1, signal);
569 signal = mywait (&status, 1);
570 prepare_resume_reply (own_buf, status, signal);
571 break;
572 case 'c':
573 set_desired_inferior (0);
574 myresume (0, 0);
575 signal = mywait (&status, 1);
576 prepare_resume_reply (own_buf, status, signal);
577 break;
578 case 's':
579 set_desired_inferior (0);
580 myresume (1, 0);
581 signal = mywait (&status, 1);
582 prepare_resume_reply (own_buf, status, signal);
583 break;
584 case 'Z':
585 {
586 char *lenptr;
587 char *dataptr;
588 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
589 int len = strtol (lenptr + 1, &dataptr, 16);
590 char type = own_buf[1];
591
592 if (the_target->insert_watchpoint == NULL
593 || (type < '2' || type > '4'))
594 {
595 /* No watchpoint support or not a watchpoint command;
596 unrecognized either way. */
597 own_buf[0] = '\0';
598 }
599 else
600 {
601 int res;
602
603 res = (*the_target->insert_watchpoint) (type, addr, len);
604 if (res == 0)
605 write_ok (own_buf);
606 else if (res == 1)
607 /* Unsupported. */
608 own_buf[0] = '\0';
609 else
610 write_enn (own_buf);
611 }
612 break;
613 }
614 case 'z':
615 {
616 char *lenptr;
617 char *dataptr;
618 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
619 int len = strtol (lenptr + 1, &dataptr, 16);
620 char type = own_buf[1];
621
622 if (the_target->remove_watchpoint == NULL
623 || (type < '2' || type > '4'))
624 {
625 /* No watchpoint support or not a watchpoint command;
626 unrecognized either way. */
627 own_buf[0] = '\0';
628 }
629 else
630 {
631 int res;
632
633 res = (*the_target->remove_watchpoint) (type, addr, len);
634 if (res == 0)
635 write_ok (own_buf);
636 else if (res == 1)
637 /* Unsupported. */
638 own_buf[0] = '\0';
639 else
640 write_enn (own_buf);
641 }
642 break;
643 }
644 case 'k':
645 fprintf (stderr, "Killing inferior\n");
646 kill_inferior ();
647 /* When using the extended protocol, we start up a new
648 debugging session. The traditional protocol will
649 exit instead. */
650 if (extended_protocol)
651 {
652 write_ok (own_buf);
653 fprintf (stderr, "GDBserver restarting\n");
654
655 /* Wait till we are at 1st instruction in prog. */
656 signal = start_inferior (&argv[2], &status);
657 goto restart;
658 break;
659 }
660 else
661 {
662 exit (0);
663 break;
664 }
665 case 'T':
666 {
667 unsigned long gdb_id, thread_id;
668
669 gdb_id = strtoul (&own_buf[1], NULL, 16);
670 thread_id = gdb_id_to_thread_id (gdb_id);
671 if (thread_id == 0)
672 {
673 write_enn (own_buf);
674 break;
675 }
676
677 if (mythread_alive (thread_id))
678 write_ok (own_buf);
679 else
680 write_enn (own_buf);
681 }
682 break;
683 case 'R':
684 /* Restarting the inferior is only supported in the
685 extended protocol. */
686 if (extended_protocol)
687 {
688 kill_inferior ();
689 write_ok (own_buf);
690 fprintf (stderr, "GDBserver restarting\n");
691
692 /* Wait till we are at 1st instruction in prog. */
693 signal = start_inferior (&argv[2], &status);
694 goto restart;
695 break;
696 }
697 else
698 {
699 /* It is a request we don't understand. Respond with an
700 empty packet so that gdb knows that we don't support this
701 request. */
702 own_buf[0] = '\0';
703 break;
704 }
705 case 'v':
706 /* Extended (long) request. */
707 handle_v_requests (own_buf, &status, &signal);
708 break;
709 default:
710 /* It is a request we don't understand. Respond with an
711 empty packet so that gdb knows that we don't support this
712 request. */
713 own_buf[0] = '\0';
714 break;
715 }
716
717 putpkt (own_buf);
718
719 if (status == 'W')
720 fprintf (stderr,
721 "\nChild exited with status %d\n", signal);
722 if (status == 'X')
723 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
724 signal);
725 if (status == 'W' || status == 'X')
726 {
727 if (extended_protocol)
728 {
729 fprintf (stderr, "Killing inferior\n");
730 kill_inferior ();
731 write_ok (own_buf);
732 fprintf (stderr, "GDBserver restarting\n");
733
734 /* Wait till we are at 1st instruction in prog. */
735 signal = start_inferior (&argv[2], &status);
736 goto restart;
737 break;
738 }
739 else
740 {
741 fprintf (stderr, "GDBserver exiting\n");
742 exit (0);
743 }
744 }
745 }
746
747 /* We come here when getpkt fails.
748
749 For the extended remote protocol we exit (and this is the only
750 way we gracefully exit!).
751
752 For the traditional remote protocol close the connection,
753 and re-open it at the top of the loop. */
754 if (extended_protocol)
755 {
756 remote_close ();
757 exit (0);
758 }
759 else
760 {
761 fprintf (stderr, "Remote side has terminated connection. "
762 "GDBserver will reopen the connection.\n");
763 remote_close ();
764 }
765 }
766 }
This page took 0.047117 seconds and 4 git commands to generate.