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