b4d3aac67841835c85b0787a6ee1766037c09462
[deliverable/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, 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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 unsigned char
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, unsigned char *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%lx", thread_ptr->id);
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%lx", thread_ptr->id);
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 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, unsigned char *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 resume_info[i].thread = strtoul (p + 1, &q, 16);
222 if (p == q)
223 goto err;
224 p = q;
225 if (p[0] != ';' && p[0] != 0)
226 goto err;
227
228 i++;
229 }
230 }
231
232 resume_info[i] = default_action;
233
234 /* Still used in occasional places in the backend. */
235 if (n == 1 && resume_info[0].thread != -1)
236 cont_thread = resume_info[0].thread;
237 else
238 cont_thread = -1;
239 set_desired_inferior (0);
240
241 (*the_target->resume) (resume_info);
242
243 free (resume_info);
244
245 *signal = mywait (status, 1);
246 prepare_resume_reply (own_buf, *status, *signal);
247 return;
248
249 err:
250 /* No other way to report an error... */
251 strcpy (own_buf, "");
252 free (resume_info);
253 return;
254 }
255
256 /* Handle all of the extended 'v' packets. */
257 void
258 handle_v_requests (char *own_buf, char *status, unsigned char *signal)
259 {
260 if (strncmp (own_buf, "vCont;", 6) == 0)
261 {
262 handle_v_cont (own_buf, status, signal);
263 return;
264 }
265
266 if (strncmp (own_buf, "vCont?", 6) == 0)
267 {
268 strcpy (own_buf, "vCont;c;C;s;S");
269 return;
270 }
271
272 /* Otherwise we didn't know what packet it was. Say we didn't
273 understand it. */
274 own_buf[0] = 0;
275 return;
276 }
277
278 void
279 myresume (int step, int sig)
280 {
281 struct thread_resume resume_info[2];
282 int n = 0;
283
284 if (step || sig || cont_thread > 0)
285 {
286 resume_info[0].thread
287 = ((struct inferior_list_entry *) current_inferior)->id;
288 resume_info[0].step = step;
289 resume_info[0].sig = sig;
290 resume_info[0].leave_stopped = 0;
291 n++;
292 }
293 resume_info[n].thread = -1;
294 resume_info[n].step = 0;
295 resume_info[n].sig = 0;
296 resume_info[n].leave_stopped = (cont_thread > 0);
297
298 (*the_target->resume) (resume_info);
299 }
300
301 static int attached;
302
303 static void
304 gdbserver_usage (void)
305 {
306 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
307 "\tgdbserver COMM --attach PID\n"
308 "\n"
309 "COMM may either be a tty device (for serial debugging), or \n"
310 "HOST:PORT to listen for a TCP connection.\n");
311 }
312
313 int
314 main (int argc, char *argv[])
315 {
316 char ch, status, *own_buf, mem_buf[2000];
317 int i = 0;
318 unsigned char signal;
319 unsigned int len;
320 CORE_ADDR mem_addr;
321 int bad_attach;
322 int pid;
323 char *arg_end;
324
325 if (setjmp (toplevel))
326 {
327 fprintf (stderr, "Exiting\n");
328 exit (1);
329 }
330
331 bad_attach = 0;
332 pid = 0;
333 attached = 0;
334 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
335 {
336 if (argc == 4
337 && argv[3] != '\0'
338 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
339 && *arg_end == '\0')
340 {
341 ;
342 }
343 else
344 bad_attach = 1;
345 }
346
347 if (argc < 3 || bad_attach)
348 gdbserver_usage();
349
350 initialize_low ();
351
352 own_buf = malloc (PBUFSIZ);
353
354 if (pid == 0)
355 {
356 /* Wait till we are at first instruction in program. */
357 signal = start_inferior (&argv[2], &status);
358
359 /* We are now stopped at the first instruction of the target process */
360 }
361 else
362 {
363 switch (attach_inferior (pid, &status, &signal))
364 {
365 case -1:
366 error ("Attaching not supported on this target");
367 break;
368 default:
369 attached = 1;
370 break;
371 }
372 }
373
374 while (1)
375 {
376 remote_open (argv[1]);
377
378 restart:
379 setjmp (toplevel);
380 while (getpkt (own_buf) > 0)
381 {
382 unsigned char sig;
383 i = 0;
384 ch = own_buf[i++];
385 switch (ch)
386 {
387 case 'q':
388 handle_query (own_buf);
389 break;
390 case 'd':
391 remote_debug = !remote_debug;
392 break;
393 case 'D':
394 fprintf (stderr, "Detaching from inferior\n");
395 detach_inferior ();
396 write_ok (own_buf);
397 putpkt (own_buf);
398 remote_close ();
399
400 /* If we are attached, then we can exit. Otherwise, we need to
401 hang around doing nothing, until the child is gone. */
402 if (!attached)
403 {
404 int status, ret;
405
406 do {
407 ret = waitpid (signal_pid, &status, 0);
408 if (WIFEXITED (status) || WIFSIGNALED (status))
409 break;
410 } while (ret != -1 || errno != ECHILD);
411 }
412
413 exit (0);
414
415 case '!':
416 if (attached == 0)
417 {
418 extended_protocol = 1;
419 prepare_resume_reply (own_buf, status, signal);
420 }
421 else
422 {
423 /* We can not use the extended protocol if we are
424 attached, because we can not restart the running
425 program. So return unrecognized. */
426 own_buf[0] = '\0';
427 }
428 break;
429 case '?':
430 prepare_resume_reply (own_buf, status, signal);
431 break;
432 case 'H':
433 switch (own_buf[1])
434 {
435 case 'g':
436 general_thread = strtoul (&own_buf[2], NULL, 16);
437 write_ok (own_buf);
438 set_desired_inferior (1);
439 break;
440 case 'c':
441 cont_thread = strtoul (&own_buf[2], NULL, 16);
442 write_ok (own_buf);
443 break;
444 case 's':
445 step_thread = strtoul (&own_buf[2], NULL, 16);
446 write_ok (own_buf);
447 break;
448 default:
449 /* Silently ignore it so that gdb can extend the protocol
450 without compatibility headaches. */
451 own_buf[0] = '\0';
452 break;
453 }
454 break;
455 case 'g':
456 set_desired_inferior (1);
457 registers_to_string (own_buf);
458 break;
459 case 'G':
460 set_desired_inferior (1);
461 registers_from_string (&own_buf[1]);
462 write_ok (own_buf);
463 break;
464 case 'm':
465 decode_m_packet (&own_buf[1], &mem_addr, &len);
466 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
467 convert_int_to_ascii (mem_buf, own_buf, len);
468 else
469 write_enn (own_buf);
470 break;
471 case 'M':
472 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
473 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
474 write_ok (own_buf);
475 else
476 write_enn (own_buf);
477 break;
478 case 'C':
479 convert_ascii_to_int (own_buf + 1, &sig, 1);
480 if (target_signal_to_host_p (sig))
481 signal = target_signal_to_host (sig);
482 else
483 signal = 0;
484 set_desired_inferior (0);
485 myresume (0, signal);
486 signal = mywait (&status, 1);
487 prepare_resume_reply (own_buf, status, signal);
488 break;
489 case 'S':
490 convert_ascii_to_int (own_buf + 1, &sig, 1);
491 if (target_signal_to_host_p (sig))
492 signal = target_signal_to_host (sig);
493 else
494 signal = 0;
495 set_desired_inferior (0);
496 myresume (1, signal);
497 signal = mywait (&status, 1);
498 prepare_resume_reply (own_buf, status, signal);
499 break;
500 case 'c':
501 set_desired_inferior (0);
502 myresume (0, 0);
503 signal = mywait (&status, 1);
504 prepare_resume_reply (own_buf, status, signal);
505 break;
506 case 's':
507 set_desired_inferior (0);
508 myresume (1, 0);
509 signal = mywait (&status, 1);
510 prepare_resume_reply (own_buf, status, signal);
511 break;
512 case 'k':
513 fprintf (stderr, "Killing inferior\n");
514 kill_inferior ();
515 /* When using the extended protocol, we start up a new
516 debugging session. The traditional protocol will
517 exit instead. */
518 if (extended_protocol)
519 {
520 write_ok (own_buf);
521 fprintf (stderr, "GDBserver restarting\n");
522
523 /* Wait till we are at 1st instruction in prog. */
524 signal = start_inferior (&argv[2], &status);
525 goto restart;
526 break;
527 }
528 else
529 {
530 exit (0);
531 break;
532 }
533 case 'T':
534 if (mythread_alive (strtoul (&own_buf[1], NULL, 16)))
535 write_ok (own_buf);
536 else
537 write_enn (own_buf);
538 break;
539 case 'R':
540 /* Restarting the inferior is only supported in the
541 extended protocol. */
542 if (extended_protocol)
543 {
544 kill_inferior ();
545 write_ok (own_buf);
546 fprintf (stderr, "GDBserver restarting\n");
547
548 /* Wait till we are at 1st instruction in prog. */
549 signal = start_inferior (&argv[2], &status);
550 goto restart;
551 break;
552 }
553 else
554 {
555 /* It is a request we don't understand. Respond with an
556 empty packet so that gdb knows that we don't support this
557 request. */
558 own_buf[0] = '\0';
559 break;
560 }
561 case 'v':
562 /* Extended (long) request. */
563 handle_v_requests (own_buf, &status, &signal);
564 break;
565 default:
566 /* It is a request we don't understand. Respond with an
567 empty packet so that gdb knows that we don't support this
568 request. */
569 own_buf[0] = '\0';
570 break;
571 }
572
573 putpkt (own_buf);
574
575 if (status == 'W')
576 fprintf (stderr,
577 "\nChild exited with status %d\n", signal);
578 if (status == 'X')
579 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
580 signal);
581 if (status == 'W' || status == 'X')
582 {
583 if (extended_protocol)
584 {
585 fprintf (stderr, "Killing inferior\n");
586 kill_inferior ();
587 write_ok (own_buf);
588 fprintf (stderr, "GDBserver restarting\n");
589
590 /* Wait till we are at 1st instruction in prog. */
591 signal = start_inferior (&argv[2], &status);
592 goto restart;
593 break;
594 }
595 else
596 {
597 fprintf (stderr, "GDBserver exiting\n");
598 exit (0);
599 }
600 }
601 }
602
603 /* We come here when getpkt fails.
604
605 For the extended remote protocol we exit (and this is the only
606 way we gracefully exit!).
607
608 For the traditional remote protocol close the connection,
609 and re-open it at the top of the loop. */
610 if (extended_protocol)
611 {
612 remote_close ();
613 exit (0);
614 }
615 else
616 {
617 fprintf (stderr, "Remote side has terminated connection. "
618 "GDBserver will reopen the connection.\n");
619 remote_close ();
620 }
621 }
622 }
This page took 0.045426 seconds and 4 git commands to generate.