1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004,
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. */
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;
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;
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],
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);
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)
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. */
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)
92 extern int remote_debug;
94 /* Handle all of the extended 'q' packets. */
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");
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;
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;
127 sprintf (own_buf, "l");
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);
146 if (the_target->read_auxv != NULL
147 && strncmp ("qPart:auxv:read::", own_buf, 17) == 0)
149 unsigned char data[(PBUFSIZ - 1) / 2];
153 decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */
154 if (len > sizeof data)
156 n = (*the_target->read_auxv) (ofs, data, len);
162 convert_int_to_ascii (data, own_buf, n);
166 /* Protocol features query. */
167 if (strncmp ("qSupported", own_buf, 10) == 0
168 && (own_buf[10] == ':' || own_buf[10] == '\0'))
170 sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1);
174 /* Otherwise we didn't know what packet it was. Say we didn't
179 /* Parse vCont packets. */
181 handle_v_cont (char *own_buf, char *status, int *signal)
185 struct thread_resume *resume_info, default_action;
187 /* Count the number of semicolons in the packet. There should be one
196 /* Allocate room for one extra action, for the default remain-stopped
197 behavior; if no default action is in the list, we'll need the extra
199 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
201 default_action.thread = -1;
202 default_action.leave_stopped = 1;
203 default_action.step = 0;
204 default_action.sig = 0;
212 resume_info[i].leave_stopped = 0;
214 if (p[0] == 's' || p[0] == 'S')
215 resume_info[i].step = 1;
216 else if (p[0] == 'c' || p[0] == 'C')
217 resume_info[i].step = 0;
221 if (p[0] == 'S' || p[0] == 'C')
224 sig = strtol (p + 1, &q, 16);
229 if (!target_signal_to_host_p (sig))
231 resume_info[i].sig = target_signal_to_host (sig);
235 resume_info[i].sig = 0;
241 resume_info[i].thread = -1;
242 default_action = resume_info[i];
244 /* Note: we don't increment i here, we'll overwrite this entry
245 the next time through. */
247 else if (p[0] == ':')
249 unsigned int gdb_id = strtoul (p + 1, &q, 16);
250 unsigned long thread_id;
255 if (p[0] != ';' && p[0] != 0)
258 thread_id = gdb_id_to_thread_id (gdb_id);
260 resume_info[i].thread = thread_id;
268 resume_info[i] = default_action;
270 /* Still used in occasional places in the backend. */
271 if (n == 1 && resume_info[0].thread != -1)
272 cont_thread = resume_info[0].thread;
275 set_desired_inferior (0);
277 (*the_target->resume) (resume_info);
281 *signal = mywait (status, 1);
282 prepare_resume_reply (own_buf, *status, *signal);
286 /* No other way to report an error... */
287 strcpy (own_buf, "");
292 /* Handle all of the extended 'v' packets. */
294 handle_v_requests (char *own_buf, char *status, int *signal)
296 if (strncmp (own_buf, "vCont;", 6) == 0)
298 handle_v_cont (own_buf, status, signal);
302 if (strncmp (own_buf, "vCont?", 6) == 0)
304 strcpy (own_buf, "vCont;c;C;s;S");
308 /* Otherwise we didn't know what packet it was. Say we didn't
315 myresume (int step, int sig)
317 struct thread_resume resume_info[2];
320 if (step || sig || (cont_thread != 0 && cont_thread != -1))
322 resume_info[0].thread
323 = ((struct inferior_list_entry *) current_inferior)->id;
324 resume_info[0].step = step;
325 resume_info[0].sig = sig;
326 resume_info[0].leave_stopped = 0;
329 resume_info[n].thread = -1;
330 resume_info[n].step = 0;
331 resume_info[n].sig = 0;
332 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
334 (*the_target->resume) (resume_info);
340 gdbserver_version (void)
342 printf ("GNU gdbserver %s\n"
343 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
344 "gdbserver is free software, covered by the GNU General Public License.\n"
345 "This gdbserver was configured as \"%s\"\n",
350 gdbserver_usage (void)
352 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
353 "\tgdbserver COMM --attach PID\n"
355 "COMM may either be a tty device (for serial debugging), or \n"
356 "HOST:PORT to listen for a TCP connection.\n");
360 main (int argc, char *argv[])
362 char ch, status, *own_buf;
363 unsigned char *mem_buf;
372 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
374 gdbserver_version ();
378 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
384 if (setjmp (toplevel))
386 fprintf (stderr, "Exiting\n");
393 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
397 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
406 if (argc < 3 || bad_attach)
414 own_buf = malloc (PBUFSIZ);
415 mem_buf = malloc (PBUFSIZ);
419 /* Wait till we are at first instruction in program. */
420 signal = start_inferior (&argv[2], &status);
422 /* We are now stopped at the first instruction of the target process */
426 switch (attach_inferior (pid, &status, &signal))
429 error ("Attaching not supported on this target");
439 remote_open (argv[1]);
443 while (getpkt (own_buf) > 0)
451 handle_query (own_buf);
454 remote_debug = !remote_debug;
457 fprintf (stderr, "Detaching from inferior\n");
463 /* If we are attached, then we can exit. Otherwise, we need to
464 hang around doing nothing, until the child is gone. */
470 ret = waitpid (signal_pid, &status, 0);
471 if (WIFEXITED (status) || WIFSIGNALED (status))
473 } while (ret != -1 || errno != ECHILD);
481 extended_protocol = 1;
482 prepare_resume_reply (own_buf, status, signal);
486 /* We can not use the extended protocol if we are
487 attached, because we can not restart the running
488 program. So return unrecognized. */
493 prepare_resume_reply (own_buf, status, signal);
496 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
498 unsigned long gdb_id, thread_id;
500 gdb_id = strtoul (&own_buf[2], NULL, 16);
501 thread_id = gdb_id_to_thread_id (gdb_id);
508 if (own_buf[1] == 'g')
510 general_thread = thread_id;
511 set_desired_inferior (1);
513 else if (own_buf[1] == 'c')
514 cont_thread = thread_id;
515 else if (own_buf[1] == 's')
516 step_thread = thread_id;
522 /* Silently ignore it so that gdb can extend the protocol
523 without compatibility headaches. */
528 set_desired_inferior (1);
529 registers_to_string (own_buf);
532 set_desired_inferior (1);
533 registers_from_string (&own_buf[1]);
537 decode_m_packet (&own_buf[1], &mem_addr, &len);
538 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
539 convert_int_to_ascii (mem_buf, own_buf, len);
544 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
545 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
551 convert_ascii_to_int (own_buf + 1, &sig, 1);
552 if (target_signal_to_host_p (sig))
553 signal = target_signal_to_host (sig);
556 set_desired_inferior (0);
557 myresume (0, signal);
558 signal = mywait (&status, 1);
559 prepare_resume_reply (own_buf, status, signal);
562 convert_ascii_to_int (own_buf + 1, &sig, 1);
563 if (target_signal_to_host_p (sig))
564 signal = target_signal_to_host (sig);
567 set_desired_inferior (0);
568 myresume (1, signal);
569 signal = mywait (&status, 1);
570 prepare_resume_reply (own_buf, status, signal);
573 set_desired_inferior (0);
575 signal = mywait (&status, 1);
576 prepare_resume_reply (own_buf, status, signal);
579 set_desired_inferior (0);
581 signal = mywait (&status, 1);
582 prepare_resume_reply (own_buf, status, signal);
588 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
589 int len = strtol (lenptr + 1, &dataptr, 16);
590 char type = own_buf[1];
592 if (the_target->insert_watchpoint == NULL
593 || (type < '2' || type > '4'))
595 /* No watchpoint support or not a watchpoint command;
596 unrecognized either way. */
603 res = (*the_target->insert_watchpoint) (type, addr, len);
618 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
619 int len = strtol (lenptr + 1, &dataptr, 16);
620 char type = own_buf[1];
622 if (the_target->remove_watchpoint == NULL
623 || (type < '2' || type > '4'))
625 /* No watchpoint support or not a watchpoint command;
626 unrecognized either way. */
633 res = (*the_target->remove_watchpoint) (type, addr, len);
645 fprintf (stderr, "Killing inferior\n");
647 /* When using the extended protocol, we start up a new
648 debugging session. The traditional protocol will
650 if (extended_protocol)
653 fprintf (stderr, "GDBserver restarting\n");
655 /* Wait till we are at 1st instruction in prog. */
656 signal = start_inferior (&argv[2], &status);
667 unsigned long gdb_id, thread_id;
669 gdb_id = strtoul (&own_buf[1], NULL, 16);
670 thread_id = gdb_id_to_thread_id (gdb_id);
677 if (mythread_alive (thread_id))
684 /* Restarting the inferior is only supported in the
685 extended protocol. */
686 if (extended_protocol)
690 fprintf (stderr, "GDBserver restarting\n");
692 /* Wait till we are at 1st instruction in prog. */
693 signal = start_inferior (&argv[2], &status);
699 /* It is a request we don't understand. Respond with an
700 empty packet so that gdb knows that we don't support this
706 /* Extended (long) request. */
707 handle_v_requests (own_buf, &status, &signal);
710 /* It is a request we don't understand. Respond with an
711 empty packet so that gdb knows that we don't support this
721 "\nChild exited with status %d\n", signal);
723 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
725 if (status == 'W' || status == 'X')
727 if (extended_protocol)
729 fprintf (stderr, "Killing inferior\n");
732 fprintf (stderr, "GDBserver restarting\n");
734 /* Wait till we are at 1st instruction in prog. */
735 signal = start_inferior (&argv[2], &status);
741 fprintf (stderr, "GDBserver exiting\n");
747 /* We come here when getpkt fails.
749 For the extended remote protocol we exit (and this is the only
750 way we gracefully exit!).
752 For the traditional remote protocol close the connection,
753 and re-open it at the top of the loop. */
754 if (extended_protocol)
761 fprintf (stderr, "Remote side has terminated connection. "
762 "GDBserver will reopen the connection.\n");