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