gdb/
[external/binutils.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   while (1)
618     {
619       remote_open (argv[1]);
620
621     restart:
622       setjmp (toplevel);
623       while (1)
624         {
625           unsigned char sig;
626           int packet_len;
627           int new_packet_len = -1;
628
629           packet_len = getpkt (own_buf);
630           if (packet_len <= 0)
631             break;
632
633           i = 0;
634           ch = own_buf[i++];
635           switch (ch)
636             {
637             case 'q':
638               handle_query (own_buf, &new_packet_len);
639               break;
640             case 'Q':
641               handle_general_set (own_buf);
642               break;
643             case 'd':
644               remote_debug = !remote_debug;
645               break;
646 #ifndef USE_WIN32API
647             /* Skip "detach" support on mingw32, since we don't have
648                waitpid.  */
649             case 'D':
650               fprintf (stderr, "Detaching from inferior\n");
651               detach_inferior ();
652               write_ok (own_buf);
653               putpkt (own_buf);
654               remote_close ();
655
656               /* If we are attached, then we can exit.  Otherwise, we need to
657                  hang around doing nothing, until the child is gone.  */
658               if (!attached)
659                 {
660                   int status, ret;
661
662                   do {
663                     ret = waitpid (signal_pid, &status, 0);
664                     if (WIFEXITED (status) || WIFSIGNALED (status))
665                       break;
666                   } while (ret != -1 || errno != ECHILD);
667                 }
668
669               exit (0);
670 #endif
671
672             case '!':
673               if (attached == 0)
674                 {
675                   extended_protocol = 1;
676                   prepare_resume_reply (own_buf, status, signal);
677                 }
678               else
679                 {
680                   /* We can not use the extended protocol if we are
681                      attached, because we can not restart the running
682                      program.  So return unrecognized.  */
683                   own_buf[0] = '\0';
684                 }
685               break;
686             case '?':
687               prepare_resume_reply (own_buf, status, signal);
688               break;
689             case 'H':
690               if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
691                 {
692                   unsigned long gdb_id, thread_id;
693
694                   gdb_id = strtoul (&own_buf[2], NULL, 16);
695                   thread_id = gdb_id_to_thread_id (gdb_id);
696                   if (thread_id == 0)
697                     {
698                       write_enn (own_buf);
699                       break;
700                     }
701
702                   if (own_buf[1] == 'g')
703                     {
704                       general_thread = thread_id;
705                       set_desired_inferior (1);
706                     }
707                   else if (own_buf[1] == 'c')
708                     cont_thread = thread_id;
709                   else if (own_buf[1] == 's')
710                     step_thread = thread_id;
711
712                   write_ok (own_buf);
713                 }
714               else
715                 {
716                   /* Silently ignore it so that gdb can extend the protocol
717                      without compatibility headaches.  */
718                   own_buf[0] = '\0';
719                 }
720               break;
721             case 'g':
722               set_desired_inferior (1);
723               registers_to_string (own_buf);
724               break;
725             case 'G':
726               set_desired_inferior (1);
727               registers_from_string (&own_buf[1]);
728               write_ok (own_buf);
729               break;
730             case 'm':
731               decode_m_packet (&own_buf[1], &mem_addr, &len);
732               if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
733                 convert_int_to_ascii (mem_buf, own_buf, len);
734               else
735                 write_enn (own_buf);
736               break;
737             case 'M':
738               decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
739               if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
740                 write_ok (own_buf);
741               else
742                 write_enn (own_buf);
743               break;
744             case 'X':
745               if (decode_X_packet (&own_buf[1], packet_len - 1,
746                                    &mem_addr, &len, mem_buf) < 0
747                   || write_inferior_memory (mem_addr, mem_buf, len) != 0)
748                 write_enn (own_buf);
749               else
750                 write_ok (own_buf);
751               break;
752             case 'C':
753               convert_ascii_to_int (own_buf + 1, &sig, 1);
754               if (target_signal_to_host_p (sig))
755                 signal = target_signal_to_host (sig);
756               else
757                 signal = 0;
758               set_desired_inferior (0);
759               myresume (0, signal);
760               signal = mywait (&status, 1);
761               prepare_resume_reply (own_buf, status, signal);
762               break;
763             case 'S':
764               convert_ascii_to_int (own_buf + 1, &sig, 1);
765               if (target_signal_to_host_p (sig))
766                 signal = target_signal_to_host (sig);
767               else
768                 signal = 0;
769               set_desired_inferior (0);
770               myresume (1, signal);
771               signal = mywait (&status, 1);
772               prepare_resume_reply (own_buf, status, signal);
773               break;
774             case 'c':
775               set_desired_inferior (0);
776               myresume (0, 0);
777               signal = mywait (&status, 1);
778               prepare_resume_reply (own_buf, status, signal);
779               break;
780             case 's':
781               set_desired_inferior (0);
782               myresume (1, 0);
783               signal = mywait (&status, 1);
784               prepare_resume_reply (own_buf, status, signal);
785               break;
786             case 'Z':
787               {
788                 char *lenptr;
789                 char *dataptr;
790                 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
791                 int len = strtol (lenptr + 1, &dataptr, 16);
792                 char type = own_buf[1];
793
794                 if (the_target->insert_watchpoint == NULL
795                     || (type < '2' || type > '4'))
796                   {
797                     /* No watchpoint support or not a watchpoint command;
798                        unrecognized either way.  */
799                     own_buf[0] = '\0';
800                   }
801                 else
802                   {
803                     int res;
804
805                     res = (*the_target->insert_watchpoint) (type, addr, len);
806                     if (res == 0)
807                       write_ok (own_buf);
808                     else if (res == 1)
809                       /* Unsupported.  */
810                       own_buf[0] = '\0';
811                     else
812                       write_enn (own_buf);
813                   }
814                 break;
815               }
816             case 'z':
817               {
818                 char *lenptr;
819                 char *dataptr;
820                 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
821                 int len = strtol (lenptr + 1, &dataptr, 16);
822                 char type = own_buf[1];
823
824                 if (the_target->remove_watchpoint == NULL
825                     || (type < '2' || type > '4'))
826                   {
827                     /* No watchpoint support or not a watchpoint command;
828                        unrecognized either way.  */
829                     own_buf[0] = '\0';
830                   }
831                 else
832                   {
833                     int res;
834
835                     res = (*the_target->remove_watchpoint) (type, addr, len);
836                     if (res == 0)
837                       write_ok (own_buf);
838                     else if (res == 1)
839                       /* Unsupported.  */
840                       own_buf[0] = '\0';
841                     else
842                       write_enn (own_buf);
843                   }
844                 break;
845               }
846             case 'k':
847               fprintf (stderr, "Killing inferior\n");
848               kill_inferior ();
849               /* When using the extended protocol, we start up a new
850                  debugging session.   The traditional protocol will
851                  exit instead.  */
852               if (extended_protocol)
853                 {
854                   write_ok (own_buf);
855                   fprintf (stderr, "GDBserver restarting\n");
856
857                   /* Wait till we are at 1st instruction in prog.  */
858                   signal = start_inferior (&argv[2], &status);
859                   goto restart;
860                   break;
861                 }
862               else
863                 {
864                   exit (0);
865                   break;
866                 }
867             case 'T':
868               {
869                 unsigned long gdb_id, thread_id;
870
871                 gdb_id = strtoul (&own_buf[1], NULL, 16);
872                 thread_id = gdb_id_to_thread_id (gdb_id);
873                 if (thread_id == 0)
874                   {
875                     write_enn (own_buf);
876                     break;
877                   }
878
879                 if (mythread_alive (thread_id))
880                   write_ok (own_buf);
881                 else
882                   write_enn (own_buf);
883               }
884               break;
885             case 'R':
886               /* Restarting the inferior is only supported in the
887                  extended protocol.  */
888               if (extended_protocol)
889                 {
890                   kill_inferior ();
891                   write_ok (own_buf);
892                   fprintf (stderr, "GDBserver restarting\n");
893
894                   /* Wait till we are at 1st instruction in prog.  */
895                   signal = start_inferior (&argv[2], &status);
896                   goto restart;
897                   break;
898                 }
899               else
900                 {
901                   /* It is a request we don't understand.  Respond with an
902                      empty packet so that gdb knows that we don't support this
903                      request.  */
904                   own_buf[0] = '\0';
905                   break;
906                 }
907             case 'v':
908               /* Extended (long) request.  */
909               handle_v_requests (own_buf, &status, &signal);
910               break;
911             default:
912               /* It is a request we don't understand.  Respond with an
913                  empty packet so that gdb knows that we don't support this
914                  request.  */
915               own_buf[0] = '\0';
916               break;
917             }
918
919           if (new_packet_len != -1)
920             putpkt_binary (own_buf, new_packet_len);
921           else
922             putpkt (own_buf);
923
924           if (status == 'W')
925             fprintf (stderr,
926                      "\nChild exited with status %d\n", signal);
927           if (status == 'X')
928             fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
929                      target_signal_to_host (signal),
930                      target_signal_to_name (signal));
931           if (status == 'W' || status == 'X')
932             {
933               if (extended_protocol)
934                 {
935                   fprintf (stderr, "Killing inferior\n");
936                   kill_inferior ();
937                   write_ok (own_buf);
938                   fprintf (stderr, "GDBserver restarting\n");
939
940                   /* Wait till we are at 1st instruction in prog.  */
941                   signal = start_inferior (&argv[2], &status);
942                   goto restart;
943                   break;
944                 }
945               else
946                 {
947                   fprintf (stderr, "GDBserver exiting\n");
948                   exit (0);
949                 }
950             }
951         }
952
953       /* We come here when getpkt fails.
954
955          For the extended remote protocol we exit (and this is the only
956          way we gracefully exit!).
957
958          For the traditional remote protocol close the connection,
959          and re-open it at the top of the loop.  */
960       if (extended_protocol)
961         {
962           remote_close ();
963           exit (0);
964         }
965       else
966         {
967           fprintf (stderr, "Remote side has terminated connection.  "
968                            "GDBserver will reopen the connection.\n");
969           remote_close ();
970         }
971     }
972 }