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