gdb/
[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 /* Decode a qXfer read request. Return 0 if everything looks OK,
95 or -1 otherwise. */
96
97 static int
98 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
99 {
100 /* Extract and NUL-terminate the annex. */
101 *annex = buf;
102 while (*buf && *buf != ':')
103 buf++;
104 if (*buf == '\0')
105 return -1;
106 *buf++ = 0;
107
108 /* After the read/write marker and annex, qXfer looks like a
109 traditional 'm' packet. */
110 decode_m_packet (buf, ofs, len);
111
112 return 0;
113 }
114
115 /* Write the response to a successful qXfer read. Returns the
116 length of the (binary) data stored in BUF, corresponding
117 to as much of DATA/LEN as we could fit. IS_MORE controls
118 the first character of the response. */
119 static int
120 write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
121 {
122 int out_len;
123
124 if (is_more)
125 buf[0] = 'm';
126 else
127 buf[0] = 'l';
128
129 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
130 PBUFSIZ - 2) + 1;
131 }
132
133 /* Handle all of the extended 'q' packets. */
134 void
135 handle_query (char *own_buf, int *new_packet_len_p)
136 {
137 static struct inferior_list_entry *thread_ptr;
138
139 if (strcmp ("qSymbol::", own_buf) == 0)
140 {
141 if (the_target->look_up_symbols != NULL)
142 (*the_target->look_up_symbols) ();
143
144 strcpy (own_buf, "OK");
145 return;
146 }
147
148 if (strcmp ("qfThreadInfo", own_buf) == 0)
149 {
150 thread_ptr = all_threads.head;
151 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
152 thread_ptr = thread_ptr->next;
153 return;
154 }
155
156 if (strcmp ("qsThreadInfo", own_buf) == 0)
157 {
158 if (thread_ptr != NULL)
159 {
160 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
161 thread_ptr = thread_ptr->next;
162 return;
163 }
164 else
165 {
166 sprintf (own_buf, "l");
167 return;
168 }
169 }
170
171 if (the_target->read_offsets != NULL
172 && strcmp ("qOffsets", own_buf) == 0)
173 {
174 CORE_ADDR text, data;
175
176 if (the_target->read_offsets (&text, &data))
177 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
178 (long)text, (long)data, (long)data);
179 else
180 write_enn (own_buf);
181
182 return;
183 }
184
185 if (the_target->read_auxv != NULL
186 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
187 {
188 unsigned char *data;
189 int n;
190 CORE_ADDR ofs;
191 unsigned int len;
192 char *annex;
193
194 /* Reject any annex; grab the offset and length. */
195 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
196 || annex[0] != '\0')
197 {
198 strcpy (own_buf, "E00");
199 return;
200 }
201
202 /* Read one extra byte, as an indicator of whether there is
203 more. */
204 if (len > PBUFSIZ - 2)
205 len = PBUFSIZ - 2;
206 data = malloc (len + 1);
207 n = (*the_target->read_auxv) (ofs, data, len + 1);
208 if (n > len)
209 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
210 else
211 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
212
213 free (data);
214
215 return;
216 }
217
218 /* Protocol features query. */
219 if (strncmp ("qSupported", own_buf, 10) == 0
220 && (own_buf[10] == ':' || own_buf[10] == '\0'))
221 {
222 sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1);
223
224 if (the_target->read_auxv != NULL)
225 strcat (own_buf, ";qPart:auxv:read+");
226
227 return;
228 }
229
230 /* Otherwise we didn't know what packet it was. Say we didn't
231 understand it. */
232 own_buf[0] = 0;
233 }
234
235 /* Parse vCont packets. */
236 void
237 handle_v_cont (char *own_buf, char *status, int *signal)
238 {
239 char *p, *q;
240 int n = 0, i = 0;
241 struct thread_resume *resume_info, default_action;
242
243 /* Count the number of semicolons in the packet. There should be one
244 for every action. */
245 p = &own_buf[5];
246 while (p)
247 {
248 n++;
249 p++;
250 p = strchr (p, ';');
251 }
252 /* Allocate room for one extra action, for the default remain-stopped
253 behavior; if no default action is in the list, we'll need the extra
254 slot. */
255 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
256
257 default_action.thread = -1;
258 default_action.leave_stopped = 1;
259 default_action.step = 0;
260 default_action.sig = 0;
261
262 p = &own_buf[5];
263 i = 0;
264 while (*p)
265 {
266 p++;
267
268 resume_info[i].leave_stopped = 0;
269
270 if (p[0] == 's' || p[0] == 'S')
271 resume_info[i].step = 1;
272 else if (p[0] == 'c' || p[0] == 'C')
273 resume_info[i].step = 0;
274 else
275 goto err;
276
277 if (p[0] == 'S' || p[0] == 'C')
278 {
279 int sig;
280 sig = strtol (p + 1, &q, 16);
281 if (p == q)
282 goto err;
283 p = q;
284
285 if (!target_signal_to_host_p (sig))
286 goto err;
287 resume_info[i].sig = target_signal_to_host (sig);
288 }
289 else
290 {
291 resume_info[i].sig = 0;
292 p = p + 1;
293 }
294
295 if (p[0] == 0)
296 {
297 resume_info[i].thread = -1;
298 default_action = resume_info[i];
299
300 /* Note: we don't increment i here, we'll overwrite this entry
301 the next time through. */
302 }
303 else if (p[0] == ':')
304 {
305 unsigned int gdb_id = strtoul (p + 1, &q, 16);
306 unsigned long thread_id;
307
308 if (p == q)
309 goto err;
310 p = q;
311 if (p[0] != ';' && p[0] != 0)
312 goto err;
313
314 thread_id = gdb_id_to_thread_id (gdb_id);
315 if (thread_id)
316 resume_info[i].thread = thread_id;
317 else
318 goto err;
319
320 i++;
321 }
322 }
323
324 resume_info[i] = default_action;
325
326 /* Still used in occasional places in the backend. */
327 if (n == 1 && resume_info[0].thread != -1)
328 cont_thread = resume_info[0].thread;
329 else
330 cont_thread = -1;
331 set_desired_inferior (0);
332
333 (*the_target->resume) (resume_info);
334
335 free (resume_info);
336
337 *signal = mywait (status, 1);
338 prepare_resume_reply (own_buf, *status, *signal);
339 return;
340
341 err:
342 /* No other way to report an error... */
343 strcpy (own_buf, "");
344 free (resume_info);
345 return;
346 }
347
348 /* Handle all of the extended 'v' packets. */
349 void
350 handle_v_requests (char *own_buf, char *status, int *signal)
351 {
352 if (strncmp (own_buf, "vCont;", 6) == 0)
353 {
354 handle_v_cont (own_buf, status, signal);
355 return;
356 }
357
358 if (strncmp (own_buf, "vCont?", 6) == 0)
359 {
360 strcpy (own_buf, "vCont;c;C;s;S");
361 return;
362 }
363
364 /* Otherwise we didn't know what packet it was. Say we didn't
365 understand it. */
366 own_buf[0] = 0;
367 return;
368 }
369
370 void
371 myresume (int step, int sig)
372 {
373 struct thread_resume resume_info[2];
374 int n = 0;
375
376 if (step || sig || (cont_thread != 0 && cont_thread != -1))
377 {
378 resume_info[0].thread
379 = ((struct inferior_list_entry *) current_inferior)->id;
380 resume_info[0].step = step;
381 resume_info[0].sig = sig;
382 resume_info[0].leave_stopped = 0;
383 n++;
384 }
385 resume_info[n].thread = -1;
386 resume_info[n].step = 0;
387 resume_info[n].sig = 0;
388 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
389
390 (*the_target->resume) (resume_info);
391 }
392
393 static int attached;
394
395 static void
396 gdbserver_version (void)
397 {
398 printf ("GNU gdbserver %s\n"
399 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
400 "gdbserver is free software, covered by the GNU General Public License.\n"
401 "This gdbserver was configured as \"%s\"\n",
402 version, host_name);
403 }
404
405 static void
406 gdbserver_usage (void)
407 {
408 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
409 "\tgdbserver COMM --attach PID\n"
410 "\n"
411 "COMM may either be a tty device (for serial debugging), or \n"
412 "HOST:PORT to listen for a TCP connection.\n");
413 }
414
415 int
416 main (int argc, char *argv[])
417 {
418 char ch, status, *own_buf;
419 unsigned char *mem_buf;
420 int i = 0;
421 int signal;
422 unsigned int len;
423 CORE_ADDR mem_addr;
424 int bad_attach;
425 int pid;
426 char *arg_end;
427
428 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
429 {
430 gdbserver_version ();
431 exit (0);
432 }
433
434 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
435 {
436 gdbserver_usage ();
437 exit (0);
438 }
439
440 if (setjmp (toplevel))
441 {
442 fprintf (stderr, "Exiting\n");
443 exit (1);
444 }
445
446 bad_attach = 0;
447 pid = 0;
448 attached = 0;
449 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
450 {
451 if (argc == 4
452 && argv[3] != '\0'
453 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
454 && *arg_end == '\0')
455 {
456 ;
457 }
458 else
459 bad_attach = 1;
460 }
461
462 if (argc < 3 || bad_attach)
463 {
464 gdbserver_usage ();
465 exit (1);
466 }
467
468 initialize_low ();
469
470 own_buf = malloc (PBUFSIZ);
471 mem_buf = malloc (PBUFSIZ);
472
473 if (pid == 0)
474 {
475 /* Wait till we are at first instruction in program. */
476 signal = start_inferior (&argv[2], &status);
477
478 /* We are now stopped at the first instruction of the target process */
479 }
480 else
481 {
482 switch (attach_inferior (pid, &status, &signal))
483 {
484 case -1:
485 error ("Attaching not supported on this target");
486 break;
487 default:
488 attached = 1;
489 break;
490 }
491 }
492
493 while (1)
494 {
495 remote_open (argv[1]);
496
497 restart:
498 setjmp (toplevel);
499 while (1)
500 {
501 unsigned char sig;
502 int packet_len;
503 int new_packet_len = -1;
504
505 packet_len = getpkt (own_buf);
506 if (packet_len <= 0)
507 break;
508
509 i = 0;
510 ch = own_buf[i++];
511 switch (ch)
512 {
513 case 'q':
514 handle_query (own_buf, &new_packet_len);
515 break;
516 case 'd':
517 remote_debug = !remote_debug;
518 break;
519 case 'D':
520 fprintf (stderr, "Detaching from inferior\n");
521 detach_inferior ();
522 write_ok (own_buf);
523 putpkt (own_buf);
524 remote_close ();
525
526 /* If we are attached, then we can exit. Otherwise, we need to
527 hang around doing nothing, until the child is gone. */
528 if (!attached)
529 {
530 int status, ret;
531
532 do {
533 ret = waitpid (signal_pid, &status, 0);
534 if (WIFEXITED (status) || WIFSIGNALED (status))
535 break;
536 } while (ret != -1 || errno != ECHILD);
537 }
538
539 exit (0);
540
541 case '!':
542 if (attached == 0)
543 {
544 extended_protocol = 1;
545 prepare_resume_reply (own_buf, status, signal);
546 }
547 else
548 {
549 /* We can not use the extended protocol if we are
550 attached, because we can not restart the running
551 program. So return unrecognized. */
552 own_buf[0] = '\0';
553 }
554 break;
555 case '?':
556 prepare_resume_reply (own_buf, status, signal);
557 break;
558 case 'H':
559 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
560 {
561 unsigned long gdb_id, thread_id;
562
563 gdb_id = strtoul (&own_buf[2], NULL, 16);
564 thread_id = gdb_id_to_thread_id (gdb_id);
565 if (thread_id == 0)
566 {
567 write_enn (own_buf);
568 break;
569 }
570
571 if (own_buf[1] == 'g')
572 {
573 general_thread = thread_id;
574 set_desired_inferior (1);
575 }
576 else if (own_buf[1] == 'c')
577 cont_thread = thread_id;
578 else if (own_buf[1] == 's')
579 step_thread = thread_id;
580
581 write_ok (own_buf);
582 }
583 else
584 {
585 /* Silently ignore it so that gdb can extend the protocol
586 without compatibility headaches. */
587 own_buf[0] = '\0';
588 }
589 break;
590 case 'g':
591 set_desired_inferior (1);
592 registers_to_string (own_buf);
593 break;
594 case 'G':
595 set_desired_inferior (1);
596 registers_from_string (&own_buf[1]);
597 write_ok (own_buf);
598 break;
599 case 'm':
600 decode_m_packet (&own_buf[1], &mem_addr, &len);
601 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
602 convert_int_to_ascii (mem_buf, own_buf, len);
603 else
604 write_enn (own_buf);
605 break;
606 case 'M':
607 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
608 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
609 write_ok (own_buf);
610 else
611 write_enn (own_buf);
612 break;
613 case 'X':
614 if (decode_X_packet (&own_buf[1], packet_len - 1,
615 &mem_addr, &len, mem_buf) < 0
616 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
617 write_enn (own_buf);
618 else
619 write_ok (own_buf);
620 break;
621 case 'C':
622 convert_ascii_to_int (own_buf + 1, &sig, 1);
623 if (target_signal_to_host_p (sig))
624 signal = target_signal_to_host (sig);
625 else
626 signal = 0;
627 set_desired_inferior (0);
628 myresume (0, signal);
629 signal = mywait (&status, 1);
630 prepare_resume_reply (own_buf, status, signal);
631 break;
632 case 'S':
633 convert_ascii_to_int (own_buf + 1, &sig, 1);
634 if (target_signal_to_host_p (sig))
635 signal = target_signal_to_host (sig);
636 else
637 signal = 0;
638 set_desired_inferior (0);
639 myresume (1, signal);
640 signal = mywait (&status, 1);
641 prepare_resume_reply (own_buf, status, signal);
642 break;
643 case 'c':
644 set_desired_inferior (0);
645 myresume (0, 0);
646 signal = mywait (&status, 1);
647 prepare_resume_reply (own_buf, status, signal);
648 break;
649 case 's':
650 set_desired_inferior (0);
651 myresume (1, 0);
652 signal = mywait (&status, 1);
653 prepare_resume_reply (own_buf, status, signal);
654 break;
655 case 'Z':
656 {
657 char *lenptr;
658 char *dataptr;
659 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
660 int len = strtol (lenptr + 1, &dataptr, 16);
661 char type = own_buf[1];
662
663 if (the_target->insert_watchpoint == NULL
664 || (type < '2' || type > '4'))
665 {
666 /* No watchpoint support or not a watchpoint command;
667 unrecognized either way. */
668 own_buf[0] = '\0';
669 }
670 else
671 {
672 int res;
673
674 res = (*the_target->insert_watchpoint) (type, addr, len);
675 if (res == 0)
676 write_ok (own_buf);
677 else if (res == 1)
678 /* Unsupported. */
679 own_buf[0] = '\0';
680 else
681 write_enn (own_buf);
682 }
683 break;
684 }
685 case 'z':
686 {
687 char *lenptr;
688 char *dataptr;
689 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
690 int len = strtol (lenptr + 1, &dataptr, 16);
691 char type = own_buf[1];
692
693 if (the_target->remove_watchpoint == NULL
694 || (type < '2' || type > '4'))
695 {
696 /* No watchpoint support or not a watchpoint command;
697 unrecognized either way. */
698 own_buf[0] = '\0';
699 }
700 else
701 {
702 int res;
703
704 res = (*the_target->remove_watchpoint) (type, addr, len);
705 if (res == 0)
706 write_ok (own_buf);
707 else if (res == 1)
708 /* Unsupported. */
709 own_buf[0] = '\0';
710 else
711 write_enn (own_buf);
712 }
713 break;
714 }
715 case 'k':
716 fprintf (stderr, "Killing inferior\n");
717 kill_inferior ();
718 /* When using the extended protocol, we start up a new
719 debugging session. The traditional protocol will
720 exit instead. */
721 if (extended_protocol)
722 {
723 write_ok (own_buf);
724 fprintf (stderr, "GDBserver restarting\n");
725
726 /* Wait till we are at 1st instruction in prog. */
727 signal = start_inferior (&argv[2], &status);
728 goto restart;
729 break;
730 }
731 else
732 {
733 exit (0);
734 break;
735 }
736 case 'T':
737 {
738 unsigned long gdb_id, thread_id;
739
740 gdb_id = strtoul (&own_buf[1], NULL, 16);
741 thread_id = gdb_id_to_thread_id (gdb_id);
742 if (thread_id == 0)
743 {
744 write_enn (own_buf);
745 break;
746 }
747
748 if (mythread_alive (thread_id))
749 write_ok (own_buf);
750 else
751 write_enn (own_buf);
752 }
753 break;
754 case 'R':
755 /* Restarting the inferior is only supported in the
756 extended protocol. */
757 if (extended_protocol)
758 {
759 kill_inferior ();
760 write_ok (own_buf);
761 fprintf (stderr, "GDBserver restarting\n");
762
763 /* Wait till we are at 1st instruction in prog. */
764 signal = start_inferior (&argv[2], &status);
765 goto restart;
766 break;
767 }
768 else
769 {
770 /* It is a request we don't understand. Respond with an
771 empty packet so that gdb knows that we don't support this
772 request. */
773 own_buf[0] = '\0';
774 break;
775 }
776 case 'v':
777 /* Extended (long) request. */
778 handle_v_requests (own_buf, &status, &signal);
779 break;
780 default:
781 /* It is a request we don't understand. Respond with an
782 empty packet so that gdb knows that we don't support this
783 request. */
784 own_buf[0] = '\0';
785 break;
786 }
787
788 if (new_packet_len != -1)
789 putpkt_binary (own_buf, new_packet_len);
790 else
791 putpkt (own_buf);
792
793 if (status == 'W')
794 fprintf (stderr,
795 "\nChild exited with status %d\n", signal);
796 if (status == 'X')
797 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
798 signal);
799 if (status == 'W' || status == 'X')
800 {
801 if (extended_protocol)
802 {
803 fprintf (stderr, "Killing inferior\n");
804 kill_inferior ();
805 write_ok (own_buf);
806 fprintf (stderr, "GDBserver restarting\n");
807
808 /* Wait till we are at 1st instruction in prog. */
809 signal = start_inferior (&argv[2], &status);
810 goto restart;
811 break;
812 }
813 else
814 {
815 fprintf (stderr, "GDBserver exiting\n");
816 exit (0);
817 }
818 }
819 }
820
821 /* We come here when getpkt fails.
822
823 For the extended remote protocol we exit (and this is the only
824 way we gracefully exit!).
825
826 For the traditional remote protocol close the connection,
827 and re-open it at the top of the loop. */
828 if (extended_protocol)
829 {
830 remote_close ();
831 exit (0);
832 }
833 else
834 {
835 fprintf (stderr, "Remote side has terminated connection. "
836 "GDBserver will reopen the connection.\n");
837 remote_close ();
838 }
839 }
840 }