a31547c9b393ef8b73b7a2e1235bb05c537a1d02
[external/binutils.git] / gdb / gdbserver / server.c
1 /* Main code for remote server for GDB.
2    Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "server.h"
23
24 int cont_thread;
25 int general_thread;
26 int thread_from_wait;
27 int old_thread_from_wait;
28 int extended_protocol;
29 jmp_buf toplevel;
30
31 static unsigned char
32 start_inferior (char *argv[], char *statusptr)
33 {
34   /* FIXME Check error? Or turn to void.  */
35   create_inferior (argv[0], argv);
36   /* FIXME Print pid properly.  */
37   fprintf (stderr, "Process %s created; pid = %d\n", argv[0], signal_pid);
38
39   /* Wait till we are at 1st instruction in program, return signal number.  */
40   return mywait (statusptr);
41 }
42
43 static int
44 attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
45 {
46   /* myattach should return -1 if attaching is unsupported,
47      0 if it succeeded, and call error() otherwise.  */
48   if (myattach (pid) != 0)
49     return -1;
50
51   *sigptr = mywait (statusptr);
52
53   return 0;
54 }
55
56 extern int remote_debug;
57
58 /* Handle all of the extended 'q' packets.  */
59 void
60 handle_query (char *own_buf)
61 {
62   if (strcmp ("qSymbol::", own_buf) == 0)
63     {
64       if (the_target->look_up_symbols != NULL)
65         (*the_target->look_up_symbols) ();
66
67       strcpy (own_buf, "OK");
68       return;
69     }
70
71   /* Otherwise we didn't know what packet it was.  Say we didn't
72      understand it.  */
73   own_buf[0] = 0;
74 }
75
76 static int attached;
77
78 static void
79 gdbserver_usage (void)
80 {
81   error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
82          "\tgdbserver COMM --attach PID\n"
83          "\n"
84          "COMM may either be a tty device (for serial debugging), or \n"
85          "HOST:PORT to listen for a TCP connection.\n");
86 }
87
88 int
89 main (int argc, char *argv[])
90 {
91   char ch, status, *own_buf, mem_buf[2000];
92   int i = 0;
93   unsigned char signal;
94   unsigned int len;
95   CORE_ADDR mem_addr;
96   int bad_attach;
97   int pid;
98   char *arg_end;
99
100   if (setjmp (toplevel))
101     {
102       fprintf (stderr, "Exiting\n");
103       exit (1);
104     }
105
106   bad_attach = 0;
107   pid = 0;
108   attached = 0;
109   if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
110     {
111       if (argc == 4
112           && argv[3] != '\0'
113           && (pid = strtoul (argv[3], &arg_end, 10)) != 0
114           && *arg_end == '\0')
115         {
116           ;
117         }
118       else
119         bad_attach = 1;
120     }
121
122   if (argc < 3 || bad_attach)
123     gdbserver_usage();
124
125   initialize_low ();
126
127   own_buf = malloc (PBUFSIZ);
128
129   if (pid == 0)
130     {
131       /* Wait till we are at first instruction in program.  */
132       signal = start_inferior (&argv[2], &status);
133
134       /* We are now stopped at the first instruction of the target process */
135     }
136   else
137     {
138       switch (attach_inferior (pid, &status, &signal))
139         {
140         case -1:
141           error ("Attaching not supported on this target");
142           break;
143         default:
144           attached = 1;
145           break;
146         }
147     }
148
149   while (1)
150     {
151       remote_open (argv[1]);
152
153     restart:
154       setjmp (toplevel);
155       while (getpkt (own_buf) > 0)
156         {
157           unsigned char sig;
158           i = 0;
159           ch = own_buf[i++];
160           switch (ch)
161             {
162             case 'q':
163               handle_query (own_buf);
164               break;
165             case 'd':
166               remote_debug = !remote_debug;
167               break;
168             case '!':
169               if (attached == 0)
170                 {
171                   extended_protocol = 1;
172                   prepare_resume_reply (own_buf, status, signal);
173                 }
174               else
175                 {
176                   /* We can not use the extended protocol if we are
177                      attached, because we can not restart the running
178                      program.  So return unrecognized.  */
179                   own_buf[0] = '\0';
180                 }
181               break;
182             case '?':
183               prepare_resume_reply (own_buf, status, signal);
184               break;
185             case 'H':
186               switch (own_buf[1])
187                 {
188                 case 'g':
189                   general_thread = strtol (&own_buf[2], NULL, 16);
190                   write_ok (own_buf);
191                   fetch_inferior_registers (0);
192                   break;
193                 case 'c':
194                   cont_thread = strtol (&own_buf[2], NULL, 16);
195                   write_ok (own_buf);
196                   break;
197                 default:
198                   /* Silently ignore it so that gdb can extend the protocol
199                      without compatibility headaches.  */
200                   own_buf[0] = '\0';
201                   break;
202                 }
203               break;
204             case 'g':
205               registers_to_string (own_buf);
206               break;
207             case 'G':
208               registers_from_string (&own_buf[1]);
209               store_inferior_registers (-1);
210               write_ok (own_buf);
211               break;
212             case 'm':
213               decode_m_packet (&own_buf[1], &mem_addr, &len);
214               read_inferior_memory (mem_addr, mem_buf, len);
215               convert_int_to_ascii (mem_buf, own_buf, len);
216               break;
217             case 'M':
218               decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
219               if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
220                 write_ok (own_buf);
221               else
222                 write_enn (own_buf);
223               break;
224             case 'C':
225               convert_ascii_to_int (own_buf + 1, &sig, 1);
226               if (target_signal_to_host_p (sig))
227                 signal = target_signal_to_host (sig);
228               else
229                 signal = 0;
230               myresume (0, signal);
231               signal = mywait (&status);
232               prepare_resume_reply (own_buf, status, signal);
233               break;
234             case 'S':
235               convert_ascii_to_int (own_buf + 1, &sig, 1);
236               if (target_signal_to_host_p (sig))
237                 signal = target_signal_to_host (sig);
238               else
239                 signal = 0;
240               myresume (1, signal);
241               signal = mywait (&status);
242               prepare_resume_reply (own_buf, status, signal);
243               break;
244             case 'c':
245               myresume (0, 0);
246               signal = mywait (&status);
247               prepare_resume_reply (own_buf, status, signal);
248               break;
249             case 's':
250               myresume (1, 0);
251               signal = mywait (&status);
252               prepare_resume_reply (own_buf, status, signal);
253               break;
254             case 'k':
255               fprintf (stderr, "Killing inferior\n");
256               kill_inferior ();
257               /* When using the extended protocol, we start up a new
258                  debugging session.   The traditional protocol will
259                  exit instead.  */
260               if (extended_protocol)
261                 {
262                   write_ok (own_buf);
263                   fprintf (stderr, "GDBserver restarting\n");
264
265                   /* Wait till we are at 1st instruction in prog.  */
266                   signal = start_inferior (&argv[2], &status);
267                   goto restart;
268                   break;
269                 }
270               else
271                 {
272                   exit (0);
273                   break;
274                 }
275             case 'T':
276               if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
277                 write_ok (own_buf);
278               else
279                 write_enn (own_buf);
280               break;
281             case 'R':
282               /* Restarting the inferior is only supported in the
283                  extended protocol.  */
284               if (extended_protocol)
285                 {
286                   kill_inferior ();
287                   write_ok (own_buf);
288                   fprintf (stderr, "GDBserver restarting\n");
289
290                   /* Wait till we are at 1st instruction in prog.  */
291                   signal = start_inferior (&argv[2], &status);
292                   goto restart;
293                   break;
294                 }
295               else
296                 {
297                   /* It is a request we don't understand.  Respond with an
298                      empty packet so that gdb knows that we don't support this
299                      request.  */
300                   own_buf[0] = '\0';
301                   break;
302                 }
303             default:
304               /* It is a request we don't understand.  Respond with an
305                  empty packet so that gdb knows that we don't support this
306                  request.  */
307               own_buf[0] = '\0';
308               break;
309             }
310
311           putpkt (own_buf);
312
313           if (status == 'W')
314             fprintf (stderr,
315                      "\nChild exited with status %d\n", sig);
316           if (status == 'X')
317             fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
318           if (status == 'W' || status == 'X')
319             {
320               if (extended_protocol)
321                 {
322                   fprintf (stderr, "Killing inferior\n");
323                   kill_inferior ();
324                   write_ok (own_buf);
325                   fprintf (stderr, "GDBserver restarting\n");
326
327                   /* Wait till we are at 1st instruction in prog.  */
328                   signal = start_inferior (&argv[2], &status);
329                   goto restart;
330                   break;
331                 }
332               else
333                 {
334                   fprintf (stderr, "GDBserver exiting\n");
335                   exit (0);
336                 }
337             }
338         }
339
340       /* We come here when getpkt fails.
341
342          For the extended remote protocol we exit (and this is the only
343          way we gracefully exit!).
344
345          For the traditional remote protocol close the connection,
346          and re-open it at the top of the loop.  */
347       if (extended_protocol)
348         {
349           remote_close ();
350           exit (0);
351         }
352       else
353         {
354           fprintf (stderr, "Remote side has terminated connection.  "
355                            "GDBserver will reopen the connection.\n");
356           remote_close ();
357         }
358     }
359 }