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