No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / gdbserver / server.c
blob51b87642a83bd3a921df2726c17363e0167547a5
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.
6 This file is part of GDB.
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.
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.
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. */
23 #include "server.h"
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/wait.h>
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;
37 jmp_buf toplevel;
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. */
44 unsigned long signal_pid;
46 static int
47 start_inferior (char *argv[], char *statusptr)
49 signal (SIGTTOU, SIG_DFL);
50 signal (SIGTTIN, SIG_DFL);
52 signal_pid = create_inferior (argv[0], argv);
54 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
55 signal_pid);
57 signal (SIGTTOU, SIG_IGN);
58 signal (SIGTTIN, SIG_IGN);
59 tcsetpgrp (fileno (stderr), signal_pid);
61 /* Wait till we are at 1st instruction in program, return signal number. */
62 return mywait (statusptr, 0);
65 static int
66 attach_inferior (int pid, char *statusptr, int *sigptr)
68 /* myattach should return -1 if attaching is unsupported,
69 0 if it succeeded, and call error() otherwise. */
71 if (myattach (pid) != 0)
72 return -1;
74 fprintf (stderr, "Attached; pid = %d\n", pid);
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;
81 *sigptr = mywait (statusptr, 0);
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;
89 return 0;
92 extern int remote_debug;
94 /* Handle all of the extended 'q' packets. */
95 void
96 handle_query (char *own_buf)
98 static struct inferior_list_entry *thread_ptr;
100 if (strcmp ("qSymbol::", own_buf) == 0)
102 if (the_target->look_up_symbols != NULL)
103 (*the_target->look_up_symbols) ();
105 strcpy (own_buf, "OK");
106 return;
109 if (strcmp ("qfThreadInfo", own_buf) == 0)
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;
117 if (strcmp ("qsThreadInfo", own_buf) == 0)
119 if (thread_ptr != NULL)
121 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
122 thread_ptr = thread_ptr->next;
123 return;
125 else
127 sprintf (own_buf, "l");
128 return;
132 if (the_target->read_offsets != NULL
133 && strcmp ("qOffsets", own_buf) == 0)
135 CORE_ADDR text, data;
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);
143 return;
146 if (the_target->read_auxv != NULL
147 && strncmp ("qPart:auxv:read::", own_buf, 17) == 0)
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;
166 /* Otherwise we didn't know what packet it was. Say we didn't
167 understand it. */
168 own_buf[0] = 0;
171 /* Parse vCont packets. */
172 void
173 handle_v_cont (char *own_buf, char *status, int *signal)
175 char *p, *q;
176 int n = 0, i = 0;
177 struct thread_resume *resume_info, default_action;
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)
184 n++;
185 p++;
186 p = strchr (p, ';');
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]));
193 default_action.thread = -1;
194 default_action.leave_stopped = 1;
195 default_action.step = 0;
196 default_action.sig = 0;
198 p = &own_buf[5];
199 i = 0;
200 while (*p)
202 p++;
204 resume_info[i].leave_stopped = 0;
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;
213 if (p[0] == 'S' || p[0] == 'C')
215 int sig;
216 sig = strtol (p + 1, &q, 16);
217 if (p == q)
218 goto err;
219 p = q;
221 if (!target_signal_to_host_p (sig))
222 goto err;
223 resume_info[i].sig = target_signal_to_host (sig);
225 else
227 resume_info[i].sig = 0;
228 p = p + 1;
231 if (p[0] == 0)
233 resume_info[i].thread = -1;
234 default_action = resume_info[i];
236 /* Note: we don't increment i here, we'll overwrite this entry
237 the next time through. */
239 else if (p[0] == ':')
241 unsigned int gdb_id = strtoul (p + 1, &q, 16);
242 unsigned long thread_id;
244 if (p == q)
245 goto err;
246 p = q;
247 if (p[0] != ';' && p[0] != 0)
248 goto err;
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;
256 i++;
260 resume_info[i] = default_action;
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);
269 (*the_target->resume) (resume_info);
271 free (resume_info);
273 *signal = mywait (status, 1);
274 prepare_resume_reply (own_buf, *status, *signal);
275 return;
277 err:
278 /* No other way to report an error... */
279 strcpy (own_buf, "");
280 free (resume_info);
281 return;
284 /* Handle all of the extended 'v' packets. */
285 void
286 handle_v_requests (char *own_buf, char *status, int *signal)
288 if (strncmp (own_buf, "vCont;", 6) == 0)
290 handle_v_cont (own_buf, status, signal);
291 return;
294 if (strncmp (own_buf, "vCont?", 6) == 0)
296 strcpy (own_buf, "vCont;c;C;s;S");
297 return;
300 /* Otherwise we didn't know what packet it was. Say we didn't
301 understand it. */
302 own_buf[0] = 0;
303 return;
306 void
307 myresume (int step, int sig)
309 struct thread_resume resume_info[2];
310 int n = 0;
312 if (step || sig || (cont_thread != 0 && cont_thread != -1))
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++;
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);
326 (*the_target->resume) (resume_info);
329 static int attached;
331 static void
332 gdbserver_version (void)
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);
341 static void
342 gdbserver_usage (void)
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");
352 main (int argc, char *argv[])
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;
364 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
366 gdbserver_version ();
367 exit (0);
370 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
372 gdbserver_usage ();
373 exit (0);
376 if (setjmp (toplevel))
378 fprintf (stderr, "Exiting\n");
379 exit (1);
382 bad_attach = 0;
383 pid = 0;
384 attached = 0;
385 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
387 if (argc == 4
388 && argv[3] != '\0'
389 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
390 && *arg_end == '\0')
394 else
395 bad_attach = 1;
398 if (argc < 3 || bad_attach)
400 gdbserver_usage ();
401 exit (1);
404 initialize_low ();
406 own_buf = malloc (PBUFSIZ);
407 mem_buf = malloc (PBUFSIZ);
409 if (pid == 0)
411 /* Wait till we are at first instruction in program. */
412 signal = start_inferior (&argv[2], &status);
414 /* We are now stopped at the first instruction of the target process */
416 else
418 switch (attach_inferior (pid, &status, &signal))
420 case -1:
421 error ("Attaching not supported on this target");
422 break;
423 default:
424 attached = 1;
425 break;
429 while (1)
431 remote_open (argv[1]);
433 restart:
434 setjmp (toplevel);
435 while (getpkt (own_buf) > 0)
437 unsigned char sig;
438 i = 0;
439 ch = own_buf[i++];
440 switch (ch)
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 ();
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)
459 int status, ret;
461 do {
462 ret = waitpid (signal_pid, &status, 0);
463 if (WIFEXITED (status) || WIFSIGNALED (status))
464 break;
465 } while (ret != -1 || errno != ECHILD);
468 exit (0);
470 case '!':
471 if (attached == 0)
473 extended_protocol = 1;
474 prepare_resume_reply (own_buf, status, signal);
476 else
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';
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')
490 unsigned long gdb_id, thread_id;
492 gdb_id = strtoul (&own_buf[2], NULL, 16);
493 thread_id = gdb_id_to_thread_id (gdb_id);
494 if (thread_id == 0)
496 write_enn (own_buf);
497 break;
500 if (own_buf[1] == 'g')
502 general_thread = thread_id;
503 set_desired_inferior (1);
505 else if (own_buf[1] == 'c')
506 cont_thread = thread_id;
507 else if (own_buf[1] == 's')
508 step_thread = thread_id;
510 write_ok (own_buf);
512 else
514 /* Silently ignore it so that gdb can extend the protocol
515 without compatibility headaches. */
516 own_buf[0] = '\0';
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':
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];
584 if (the_target->insert_watchpoint == NULL
585 || (type < '2' || type > '4'))
587 /* No watchpoint support or not a watchpoint command;
588 unrecognized either way. */
589 own_buf[0] = '\0';
591 else
593 int res;
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);
604 break;
606 case 'z':
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];
614 if (the_target->remove_watchpoint == NULL
615 || (type < '2' || type > '4'))
617 /* No watchpoint support or not a watchpoint command;
618 unrecognized either way. */
619 own_buf[0] = '\0';
621 else
623 int res;
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);
634 break;
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)
644 write_ok (own_buf);
645 fprintf (stderr, "GDBserver restarting\n");
647 /* Wait till we are at 1st instruction in prog. */
648 signal = start_inferior (&argv[2], &status);
649 goto restart;
650 break;
652 else
654 exit (0);
655 break;
657 case 'T':
659 unsigned long gdb_id, thread_id;
661 gdb_id = strtoul (&own_buf[1], NULL, 16);
662 thread_id = gdb_id_to_thread_id (gdb_id);
663 if (thread_id == 0)
665 write_enn (own_buf);
666 break;
669 if (mythread_alive (thread_id))
670 write_ok (own_buf);
671 else
672 write_enn (own_buf);
674 break;
675 case 'R':
676 /* Restarting the inferior is only supported in the
677 extended protocol. */
678 if (extended_protocol)
680 kill_inferior ();
681 write_ok (own_buf);
682 fprintf (stderr, "GDBserver restarting\n");
684 /* Wait till we are at 1st instruction in prog. */
685 signal = start_inferior (&argv[2], &status);
686 goto restart;
687 break;
689 else
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;
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;
709 putpkt (own_buf);
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')
719 if (extended_protocol)
721 fprintf (stderr, "Killing inferior\n");
722 kill_inferior ();
723 write_ok (own_buf);
724 fprintf (stderr, "GDBserver restarting\n");
726 /* Wait till we are at 1st instruction in prog. */
727 signal = start_inferior (&argv[2], &status);
728 goto restart;
729 break;
731 else
733 fprintf (stderr, "GDBserver exiting\n");
734 exit (0);
739 /* We come here when getpkt fails.
741 For the extended remote protocol we exit (and this is the only
742 way we gracefully exit!).
744 For the traditional remote protocol close the connection,
745 and re-open it at the top of the loop. */
746 if (extended_protocol)
748 remote_close ();
749 exit (0);
751 else
753 fprintf (stderr, "Remote side has terminated connection. "
754 "GDBserver will reopen the connection.\n");
755 remote_close ();