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. */
31 unsigned long cont_thread;
32 unsigned long general_thread;
33 unsigned long step_thread;
34 unsigned long thread_from_wait;
35 unsigned long old_thread_from_wait;
36 int extended_protocol;
39 int pass_signals[TARGET_SIGNAL_LAST];
43 /* The PID of the originally created or attached inferior. Used to
44 send signals to the process when GDB sends us an asynchronous interrupt
45 (user hitting Control-C in the client), and to wait for the child to exit
46 when no longer debugging it. */
48 unsigned long signal_pid;
51 /* A file descriptor for the controlling terminal. */
54 /* TERMINAL_FD's original foreground group. */
55 pid_t old_foreground_pgrp;
57 /* Hand back terminal ownership to the original foreground group. */
60 restore_old_foreground_pgrp (void)
62 tcsetpgrp (terminal_fd, old_foreground_pgrp);
67 start_inferior (char *argv[], char *statusptr)
70 signal (SIGTTOU, SIG_DFL);
71 signal (SIGTTIN, SIG_DFL);
74 signal_pid = create_inferior (argv[0], argv);
76 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
81 signal (SIGTTOU, SIG_IGN);
82 signal (SIGTTIN, SIG_IGN);
83 terminal_fd = fileno (stderr);
84 old_foreground_pgrp = tcgetpgrp (terminal_fd);
85 tcsetpgrp (terminal_fd, signal_pid);
86 atexit (restore_old_foreground_pgrp);
89 /* Wait till we are at 1st instruction in program, return signal number. */
90 return mywait (statusptr, 0);
94 attach_inferior (int pid, char *statusptr, int *sigptr)
96 /* myattach should return -1 if attaching is unsupported,
97 0 if it succeeded, and call error() otherwise. */
99 if (myattach (pid) != 0)
102 fprintf (stderr, "Attached; pid = %d\n", pid);
105 /* FIXME - It may be that we should get the SIGNAL_PID from the
106 attach function, so that it can be the main thread instead of
107 whichever we were told to attach to. */
110 *sigptr = mywait (statusptr, 0);
112 /* GDB knows to ignore the first SIGSTOP after attaching to a running
113 process using the "attach" command, but this is different; it's
114 just using "target remote". Pretend it's just starting up. */
115 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
116 *sigptr = TARGET_SIGNAL_TRAP;
121 extern int remote_debug;
123 /* Decode a qXfer read request. Return 0 if everything looks OK,
127 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
129 /* Extract and NUL-terminate the annex. */
131 while (*buf && *buf != ':')
137 /* After the read/write marker and annex, qXfer looks like a
138 traditional 'm' packet. */
139 decode_m_packet (buf, ofs, len);
144 /* Write the response to a successful qXfer read. Returns the
145 length of the (binary) data stored in BUF, corresponding
146 to as much of DATA/LEN as we could fit. IS_MORE controls
147 the first character of the response. */
149 write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
158 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
162 /* Handle all of the extended 'Q' packets. */
164 handle_general_set (char *own_buf)
166 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
168 int numsigs = (int) TARGET_SIGNAL_LAST, i;
169 const char *p = own_buf + strlen ("QPassSignals:");
172 p = decode_address_to_semicolon (&cursig, p);
173 for (i = 0; i < numsigs; i++)
179 /* Keep looping, to clear the remaining signals. */
182 p = decode_address_to_semicolon (&cursig, p);
187 strcpy (own_buf, "OK");
191 /* Otherwise we didn't know what packet it was. Say we didn't
196 /* Handle all of the extended 'q' packets. */
198 handle_query (char *own_buf, int *new_packet_len_p)
200 static struct inferior_list_entry *thread_ptr;
202 if (strcmp ("qSymbol::", own_buf) == 0)
204 if (the_target->look_up_symbols != NULL)
205 (*the_target->look_up_symbols) ();
207 strcpy (own_buf, "OK");
211 if (strcmp ("qfThreadInfo", own_buf) == 0)
213 thread_ptr = all_threads.head;
214 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
215 thread_ptr = thread_ptr->next;
219 if (strcmp ("qsThreadInfo", own_buf) == 0)
221 if (thread_ptr != NULL)
223 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
224 thread_ptr = thread_ptr->next;
229 sprintf (own_buf, "l");
234 if (the_target->read_offsets != NULL
235 && strcmp ("qOffsets", own_buf) == 0)
237 CORE_ADDR text, data;
239 if (the_target->read_offsets (&text, &data))
240 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
241 (long)text, (long)data, (long)data);
248 if (the_target->read_auxv != NULL
249 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
257 /* Reject any annex; grab the offset and length. */
258 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
261 strcpy (own_buf, "E00");
265 /* Read one extra byte, as an indicator of whether there is
267 if (len > PBUFSIZ - 2)
269 data = malloc (len + 1);
270 n = (*the_target->read_auxv) (ofs, data, len + 1);
274 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
276 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
283 /* Protocol features query. */
284 if (strncmp ("qSupported", own_buf, 10) == 0
285 && (own_buf[10] == ':' || own_buf[10] == '\0'))
287 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
289 if (the_target->read_auxv != NULL)
290 strcat (own_buf, ";qXfer:auxv:read+");
295 /* Thread-local storage support. */
296 if (the_target->get_tls_address != NULL
297 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
299 char *p = own_buf + 12;
300 CORE_ADDR parts[3], address = 0;
303 for (i = 0; i < 3; i++)
311 p2 = strchr (p, ',');
323 decode_address (&parts[i], p, len);
327 if (p != NULL || i < 3)
331 struct thread_info *thread = gdb_id_to_thread (parts[0]);
336 err = the_target->get_tls_address (thread, parts[1], parts[2],
342 sprintf (own_buf, "%llx", address);
351 /* Otherwise, pretend we do not understand this packet. */
354 /* Otherwise we didn't know what packet it was. Say we didn't
359 /* Parse vCont packets. */
361 handle_v_cont (char *own_buf, char *status, int *signal)
365 struct thread_resume *resume_info, default_action;
367 /* Count the number of semicolons in the packet. There should be one
376 /* Allocate room for one extra action, for the default remain-stopped
377 behavior; if no default action is in the list, we'll need the extra
379 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
381 default_action.thread = -1;
382 default_action.leave_stopped = 1;
383 default_action.step = 0;
384 default_action.sig = 0;
392 resume_info[i].leave_stopped = 0;
394 if (p[0] == 's' || p[0] == 'S')
395 resume_info[i].step = 1;
396 else if (p[0] == 'c' || p[0] == 'C')
397 resume_info[i].step = 0;
401 if (p[0] == 'S' || p[0] == 'C')
404 sig = strtol (p + 1, &q, 16);
409 if (!target_signal_to_host_p (sig))
411 resume_info[i].sig = target_signal_to_host (sig);
415 resume_info[i].sig = 0;
421 resume_info[i].thread = -1;
422 default_action = resume_info[i];
424 /* Note: we don't increment i here, we'll overwrite this entry
425 the next time through. */
427 else if (p[0] == ':')
429 unsigned int gdb_id = strtoul (p + 1, &q, 16);
430 unsigned long thread_id;
435 if (p[0] != ';' && p[0] != 0)
438 thread_id = gdb_id_to_thread_id (gdb_id);
440 resume_info[i].thread = thread_id;
448 resume_info[i] = default_action;
450 /* Still used in occasional places in the backend. */
451 if (n == 1 && resume_info[0].thread != -1)
452 cont_thread = resume_info[0].thread;
455 set_desired_inferior (0);
457 (*the_target->resume) (resume_info);
461 *signal = mywait (status, 1);
462 prepare_resume_reply (own_buf, *status, *signal);
466 /* No other way to report an error... */
467 strcpy (own_buf, "");
472 /* Handle all of the extended 'v' packets. */
474 handle_v_requests (char *own_buf, char *status, int *signal)
476 if (strncmp (own_buf, "vCont;", 6) == 0)
478 handle_v_cont (own_buf, status, signal);
482 if (strncmp (own_buf, "vCont?", 6) == 0)
484 strcpy (own_buf, "vCont;c;C;s;S");
488 /* Otherwise we didn't know what packet it was. Say we didn't
495 myresume (int step, int sig)
497 struct thread_resume resume_info[2];
500 if (step || sig || (cont_thread != 0 && cont_thread != -1))
502 resume_info[0].thread
503 = ((struct inferior_list_entry *) current_inferior)->id;
504 resume_info[0].step = step;
505 resume_info[0].sig = sig;
506 resume_info[0].leave_stopped = 0;
509 resume_info[n].thread = -1;
510 resume_info[n].step = 0;
511 resume_info[n].sig = 0;
512 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
514 (*the_target->resume) (resume_info);
520 gdbserver_version (void)
522 printf ("GNU gdbserver %s\n"
523 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
524 "gdbserver is free software, covered by the GNU General Public License.\n"
525 "This gdbserver was configured as \"%s\"\n",
530 gdbserver_usage (void)
532 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
533 "\tgdbserver COMM --attach PID\n"
535 "COMM may either be a tty device (for serial debugging), or \n"
536 "HOST:PORT to listen for a TCP connection.\n");
540 main (int argc, char *argv[])
542 char ch, status, *own_buf;
543 unsigned char *mem_buf;
552 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
554 gdbserver_version ();
558 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
564 if (setjmp (toplevel))
566 fprintf (stderr, "Exiting\n");
573 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
577 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
586 if (argc < 3 || bad_attach)
594 own_buf = malloc (PBUFSIZ);
595 mem_buf = malloc (PBUFSIZ);
599 /* Wait till we are at first instruction in program. */
600 signal = start_inferior (&argv[2], &status);
602 /* We are now stopped at the first instruction of the target process */
606 switch (attach_inferior (pid, &status, &signal))
609 error ("Attaching not supported on this target");
619 remote_open (argv[1]);
627 int new_packet_len = -1;
629 packet_len = getpkt (own_buf);
638 handle_query (own_buf, &new_packet_len);
641 handle_general_set (own_buf);
644 remote_debug = !remote_debug;
647 /* Skip "detach" support on mingw32, since we don't have
650 fprintf (stderr, "Detaching from inferior\n");
656 /* If we are attached, then we can exit. Otherwise, we need to
657 hang around doing nothing, until the child is gone. */
663 ret = waitpid (signal_pid, &status, 0);
664 if (WIFEXITED (status) || WIFSIGNALED (status))
666 } while (ret != -1 || errno != ECHILD);
675 extended_protocol = 1;
676 prepare_resume_reply (own_buf, status, signal);
680 /* We can not use the extended protocol if we are
681 attached, because we can not restart the running
682 program. So return unrecognized. */
687 prepare_resume_reply (own_buf, status, signal);
690 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
692 unsigned long gdb_id, thread_id;
694 gdb_id = strtoul (&own_buf[2], NULL, 16);
695 thread_id = gdb_id_to_thread_id (gdb_id);
702 if (own_buf[1] == 'g')
704 general_thread = thread_id;
705 set_desired_inferior (1);
707 else if (own_buf[1] == 'c')
708 cont_thread = thread_id;
709 else if (own_buf[1] == 's')
710 step_thread = thread_id;
716 /* Silently ignore it so that gdb can extend the protocol
717 without compatibility headaches. */
722 set_desired_inferior (1);
723 registers_to_string (own_buf);
726 set_desired_inferior (1);
727 registers_from_string (&own_buf[1]);
731 decode_m_packet (&own_buf[1], &mem_addr, &len);
732 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
733 convert_int_to_ascii (mem_buf, own_buf, len);
738 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
739 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
745 if (decode_X_packet (&own_buf[1], packet_len - 1,
746 &mem_addr, &len, mem_buf) < 0
747 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
753 convert_ascii_to_int (own_buf + 1, &sig, 1);
754 if (target_signal_to_host_p (sig))
755 signal = target_signal_to_host (sig);
758 set_desired_inferior (0);
759 myresume (0, signal);
760 signal = mywait (&status, 1);
761 prepare_resume_reply (own_buf, status, signal);
764 convert_ascii_to_int (own_buf + 1, &sig, 1);
765 if (target_signal_to_host_p (sig))
766 signal = target_signal_to_host (sig);
769 set_desired_inferior (0);
770 myresume (1, signal);
771 signal = mywait (&status, 1);
772 prepare_resume_reply (own_buf, status, signal);
775 set_desired_inferior (0);
777 signal = mywait (&status, 1);
778 prepare_resume_reply (own_buf, status, signal);
781 set_desired_inferior (0);
783 signal = mywait (&status, 1);
784 prepare_resume_reply (own_buf, status, signal);
790 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
791 int len = strtol (lenptr + 1, &dataptr, 16);
792 char type = own_buf[1];
794 if (the_target->insert_watchpoint == NULL
795 || (type < '2' || type > '4'))
797 /* No watchpoint support or not a watchpoint command;
798 unrecognized either way. */
805 res = (*the_target->insert_watchpoint) (type, addr, len);
820 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
821 int len = strtol (lenptr + 1, &dataptr, 16);
822 char type = own_buf[1];
824 if (the_target->remove_watchpoint == NULL
825 || (type < '2' || type > '4'))
827 /* No watchpoint support or not a watchpoint command;
828 unrecognized either way. */
835 res = (*the_target->remove_watchpoint) (type, addr, len);
847 fprintf (stderr, "Killing inferior\n");
849 /* When using the extended protocol, we start up a new
850 debugging session. The traditional protocol will
852 if (extended_protocol)
855 fprintf (stderr, "GDBserver restarting\n");
857 /* Wait till we are at 1st instruction in prog. */
858 signal = start_inferior (&argv[2], &status);
869 unsigned long gdb_id, thread_id;
871 gdb_id = strtoul (&own_buf[1], NULL, 16);
872 thread_id = gdb_id_to_thread_id (gdb_id);
879 if (mythread_alive (thread_id))
886 /* Restarting the inferior is only supported in the
887 extended protocol. */
888 if (extended_protocol)
892 fprintf (stderr, "GDBserver restarting\n");
894 /* Wait till we are at 1st instruction in prog. */
895 signal = start_inferior (&argv[2], &status);
901 /* It is a request we don't understand. Respond with an
902 empty packet so that gdb knows that we don't support this
908 /* Extended (long) request. */
909 handle_v_requests (own_buf, &status, &signal);
912 /* It is a request we don't understand. Respond with an
913 empty packet so that gdb knows that we don't support this
919 if (new_packet_len != -1)
920 putpkt_binary (own_buf, new_packet_len);
926 "\nChild exited with status %d\n", signal);
928 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
929 target_signal_to_host (signal),
930 target_signal_to_name (signal));
931 if (status == 'W' || status == 'X')
933 if (extended_protocol)
935 fprintf (stderr, "Killing inferior\n");
938 fprintf (stderr, "GDBserver restarting\n");
940 /* Wait till we are at 1st instruction in prog. */
941 signal = start_inferior (&argv[2], &status);
947 fprintf (stderr, "GDBserver exiting\n");
953 /* We come here when getpkt fails.
955 For the extended remote protocol we exit (and this is the only
956 way we gracefully exit!).
958 For the traditional remote protocol close the connection,
959 and re-open it at the top of the loop. */
960 if (extended_protocol)
967 fprintf (stderr, "Remote side has terminated connection. "
968 "GDBserver will reopen the connection.\n");