]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/server.c
gdb/
[thirdparty/binutils-gdb.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 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/wait.h>
27
28 int cont_thread;
29 int general_thread;
30 int step_thread;
31 int thread_from_wait;
32 int old_thread_from_wait;
33 int extended_protocol;
34 int server_waiting;
35
36 jmp_buf toplevel;
37
38 /* The PID of the originally created or attached inferior. Used to
39 send signals to the process when GDB sends us an asynchronous interrupt
40 (user hitting Control-C in the client), and to wait for the child to exit
41 when no longer debugging it. */
42
43 int signal_pid;
44
45 static unsigned char
46 start_inferior (char *argv[], char *statusptr)
47 {
48 signal (SIGTTOU, SIG_DFL);
49 signal (SIGTTIN, SIG_DFL);
50
51 signal_pid = create_inferior (argv[0], argv);
52
53 fprintf (stderr, "Process %s created; pid = %d\n", argv[0],
54 signal_pid);
55
56 signal (SIGTTOU, SIG_IGN);
57 signal (SIGTTIN, SIG_IGN);
58 tcsetpgrp (fileno (stderr), signal_pid);
59
60 /* Wait till we are at 1st instruction in program, return signal number. */
61 return mywait (statusptr, 0);
62 }
63
64 static int
65 attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
66 {
67 /* myattach should return -1 if attaching is unsupported,
68 0 if it succeeded, and call error() otherwise. */
69
70 if (myattach (pid) != 0)
71 return -1;
72
73 /* FIXME - It may be that we should get the SIGNAL_PID from the
74 attach function, so that it can be the main thread instead of
75 whichever we were told to attach to. */
76 signal_pid = pid;
77
78 *sigptr = mywait (statusptr, 0);
79
80 return 0;
81 }
82
83 extern int remote_debug;
84
85 /* Handle all of the extended 'q' packets. */
86 void
87 handle_query (char *own_buf)
88 {
89 static struct inferior_list_entry *thread_ptr;
90
91 if (strcmp ("qSymbol::", own_buf) == 0)
92 {
93 if (the_target->look_up_symbols != NULL)
94 (*the_target->look_up_symbols) ();
95
96 strcpy (own_buf, "OK");
97 return;
98 }
99
100 if (strcmp ("qfThreadInfo", own_buf) == 0)
101 {
102 thread_ptr = all_threads.head;
103 sprintf (own_buf, "m%x", thread_ptr->id);
104 thread_ptr = thread_ptr->next;
105 return;
106 }
107
108 if (strcmp ("qsThreadInfo", own_buf) == 0)
109 {
110 if (thread_ptr != NULL)
111 {
112 sprintf (own_buf, "m%x", thread_ptr->id);
113 thread_ptr = thread_ptr->next;
114 return;
115 }
116 else
117 {
118 sprintf (own_buf, "l");
119 return;
120 }
121 }
122
123 /* Otherwise we didn't know what packet it was. Say we didn't
124 understand it. */
125 own_buf[0] = 0;
126 }
127
128 static int attached;
129
130 static void
131 gdbserver_usage (void)
132 {
133 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
134 "\tgdbserver COMM --attach PID\n"
135 "\n"
136 "COMM may either be a tty device (for serial debugging), or \n"
137 "HOST:PORT to listen for a TCP connection.\n");
138 }
139
140 int
141 main (int argc, char *argv[])
142 {
143 char ch, status, *own_buf, mem_buf[2000];
144 int i = 0;
145 unsigned char signal;
146 unsigned int len;
147 CORE_ADDR mem_addr;
148 int bad_attach;
149 int pid;
150 char *arg_end;
151
152 if (setjmp (toplevel))
153 {
154 fprintf (stderr, "Exiting\n");
155 exit (1);
156 }
157
158 bad_attach = 0;
159 pid = 0;
160 attached = 0;
161 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
162 {
163 if (argc == 4
164 && argv[3] != '\0'
165 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
166 && *arg_end == '\0')
167 {
168 ;
169 }
170 else
171 bad_attach = 1;
172 }
173
174 if (argc < 3 || bad_attach)
175 gdbserver_usage();
176
177 initialize_low ();
178
179 own_buf = malloc (PBUFSIZ);
180
181 if (pid == 0)
182 {
183 /* Wait till we are at first instruction in program. */
184 signal = start_inferior (&argv[2], &status);
185
186 /* We are now stopped at the first instruction of the target process */
187 }
188 else
189 {
190 switch (attach_inferior (pid, &status, &signal))
191 {
192 case -1:
193 error ("Attaching not supported on this target");
194 break;
195 default:
196 attached = 1;
197 break;
198 }
199 }
200
201 while (1)
202 {
203 remote_open (argv[1]);
204
205 restart:
206 setjmp (toplevel);
207 while (getpkt (own_buf) > 0)
208 {
209 unsigned char sig;
210 i = 0;
211 ch = own_buf[i++];
212 switch (ch)
213 {
214 case 'q':
215 handle_query (own_buf);
216 break;
217 case 'd':
218 remote_debug = !remote_debug;
219 break;
220 case 'D':
221 fprintf (stderr, "Detaching from inferior\n");
222 detach_inferior ();
223 write_ok (own_buf);
224 putpkt (own_buf);
225 remote_close ();
226
227 /* If we are attached, then we can exit. Otherwise, we need to
228 hang around doing nothing, until the child is gone. */
229 if (!attached)
230 {
231 int status, ret;
232
233 do {
234 ret = waitpid (signal_pid, &status, 0);
235 if (WIFEXITED (status) || WIFSIGNALED (status))
236 break;
237 } while (ret != -1 || errno != ECHILD);
238 }
239
240 exit (0);
241
242 case '!':
243 if (attached == 0)
244 {
245 extended_protocol = 1;
246 prepare_resume_reply (own_buf, status, signal);
247 }
248 else
249 {
250 /* We can not use the extended protocol if we are
251 attached, because we can not restart the running
252 program. So return unrecognized. */
253 own_buf[0] = '\0';
254 }
255 break;
256 case '?':
257 prepare_resume_reply (own_buf, status, signal);
258 break;
259 case 'H':
260 switch (own_buf[1])
261 {
262 case 'g':
263 general_thread = strtol (&own_buf[2], NULL, 16);
264 write_ok (own_buf);
265 set_desired_inferior (1);
266 break;
267 case 'c':
268 cont_thread = strtol (&own_buf[2], NULL, 16);
269 write_ok (own_buf);
270 break;
271 case 's':
272 step_thread = strtol (&own_buf[2], NULL, 16);
273 write_ok (own_buf);
274 break;
275 default:
276 /* Silently ignore it so that gdb can extend the protocol
277 without compatibility headaches. */
278 own_buf[0] = '\0';
279 break;
280 }
281 break;
282 case 'g':
283 set_desired_inferior (1);
284 registers_to_string (own_buf);
285 break;
286 case 'G':
287 set_desired_inferior (1);
288 registers_from_string (&own_buf[1]);
289 write_ok (own_buf);
290 break;
291 case 'm':
292 decode_m_packet (&own_buf[1], &mem_addr, &len);
293 read_inferior_memory (mem_addr, mem_buf, len);
294 convert_int_to_ascii (mem_buf, own_buf, len);
295 break;
296 case 'M':
297 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
298 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
299 write_ok (own_buf);
300 else
301 write_enn (own_buf);
302 break;
303 case 'C':
304 convert_ascii_to_int (own_buf + 1, &sig, 1);
305 if (target_signal_to_host_p (sig))
306 signal = target_signal_to_host (sig);
307 else
308 signal = 0;
309 set_desired_inferior (0);
310 myresume (0, signal);
311 signal = mywait (&status, 1);
312 prepare_resume_reply (own_buf, status, signal);
313 break;
314 case 'S':
315 convert_ascii_to_int (own_buf + 1, &sig, 1);
316 if (target_signal_to_host_p (sig))
317 signal = target_signal_to_host (sig);
318 else
319 signal = 0;
320 set_desired_inferior (0);
321 myresume (1, signal);
322 signal = mywait (&status, 1);
323 prepare_resume_reply (own_buf, status, signal);
324 break;
325 case 'c':
326 set_desired_inferior (0);
327 myresume (0, 0);
328 signal = mywait (&status, 1);
329 prepare_resume_reply (own_buf, status, signal);
330 break;
331 case 's':
332 set_desired_inferior (0);
333 myresume (1, 0);
334 signal = mywait (&status, 1);
335 prepare_resume_reply (own_buf, status, signal);
336 break;
337 case 'k':
338 fprintf (stderr, "Killing inferior\n");
339 kill_inferior ();
340 /* When using the extended protocol, we start up a new
341 debugging session. The traditional protocol will
342 exit instead. */
343 if (extended_protocol)
344 {
345 write_ok (own_buf);
346 fprintf (stderr, "GDBserver restarting\n");
347
348 /* Wait till we are at 1st instruction in prog. */
349 signal = start_inferior (&argv[2], &status);
350 goto restart;
351 break;
352 }
353 else
354 {
355 exit (0);
356 break;
357 }
358 case 'T':
359 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
360 write_ok (own_buf);
361 else
362 write_enn (own_buf);
363 break;
364 case 'R':
365 /* Restarting the inferior is only supported in the
366 extended protocol. */
367 if (extended_protocol)
368 {
369 kill_inferior ();
370 write_ok (own_buf);
371 fprintf (stderr, "GDBserver restarting\n");
372
373 /* Wait till we are at 1st instruction in prog. */
374 signal = start_inferior (&argv[2], &status);
375 goto restart;
376 break;
377 }
378 else
379 {
380 /* It is a request we don't understand. Respond with an
381 empty packet so that gdb knows that we don't support this
382 request. */
383 own_buf[0] = '\0';
384 break;
385 }
386 default:
387 /* It is a request we don't understand. Respond with an
388 empty packet so that gdb knows that we don't support this
389 request. */
390 own_buf[0] = '\0';
391 break;
392 }
393
394 putpkt (own_buf);
395
396 if (status == 'W')
397 fprintf (stderr,
398 "\nChild exited with status %d\n", sig);
399 if (status == 'X')
400 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
401 if (status == 'W' || status == 'X')
402 {
403 if (extended_protocol)
404 {
405 fprintf (stderr, "Killing inferior\n");
406 kill_inferior ();
407 write_ok (own_buf);
408 fprintf (stderr, "GDBserver restarting\n");
409
410 /* Wait till we are at 1st instruction in prog. */
411 signal = start_inferior (&argv[2], &status);
412 goto restart;
413 break;
414 }
415 else
416 {
417 fprintf (stderr, "GDBserver exiting\n");
418 exit (0);
419 }
420 }
421 }
422
423 /* We come here when getpkt fails.
424
425 For the extended remote protocol we exit (and this is the only
426 way we gracefully exit!).
427
428 For the traditional remote protocol close the connection,
429 and re-open it at the top of the loop. */
430 if (extended_protocol)
431 {
432 remote_close ();
433 exit (0);
434 }
435 else
436 {
437 fprintf (stderr, "Remote side has terminated connection. "
438 "GDBserver will reopen the connection.\n");
439 remote_close ();
440 }
441 }
442 }