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;
41 /* The PID of the originally created or attached inferior. Used to
42 send signals to the process when GDB sends us an asynchronous interrupt
43 (user hitting Control-C in the client), and to wait for the child to exit
44 when no longer debugging it. */
46 unsigned long signal_pid;
49 /* A file descriptor for the controlling terminal. */
52 /* TERMINAL_FD's original foreground group. */
53 pid_t old_foreground_pgrp;
55 /* Hand back terminal ownership to the original foreground group. */
58 restore_old_foreground_pgrp (void)
60 tcsetpgrp (terminal_fd, old_foreground_pgrp);
65 start_inferior (char *argv[], char *statusptr)
68 signal (SIGTTOU, SIG_DFL);
69 signal (SIGTTIN, SIG_DFL);
72 signal_pid = create_inferior (argv[0], argv);
74 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
79 signal (SIGTTOU, SIG_IGN);
80 signal (SIGTTIN, SIG_IGN);
81 terminal_fd = fileno (stderr);
82 old_foreground_pgrp = tcgetpgrp (terminal_fd);
83 tcsetpgrp (terminal_fd, signal_pid);
84 atexit (restore_old_foreground_pgrp);
87 /* Wait till we are at 1st instruction in program, return signal number. */
88 return mywait (statusptr, 0);
92 attach_inferior (int pid, char *statusptr, int *sigptr)
94 /* myattach should return -1 if attaching is unsupported,
95 0 if it succeeded, and call error() otherwise. */
97 if (myattach (pid) != 0)
100 fprintf (stderr, "Attached; pid = %d\n", pid);
103 /* FIXME - It may be that we should get the SIGNAL_PID from the
104 attach function, so that it can be the main thread instead of
105 whichever we were told to attach to. */
108 *sigptr = mywait (statusptr, 0);
110 /* GDB knows to ignore the first SIGSTOP after attaching to a running
111 process using the "attach" command, but this is different; it's
112 just using "target remote". Pretend it's just starting up. */
113 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
114 *sigptr = TARGET_SIGNAL_TRAP;
119 extern int remote_debug;
121 /* Decode a qXfer read request. Return 0 if everything looks OK,
125 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
127 /* Extract and NUL-terminate the annex. */
129 while (*buf && *buf != ':')
135 /* After the read/write marker and annex, qXfer looks like a
136 traditional 'm' packet. */
137 decode_m_packet (buf, ofs, len);
142 /* Write the response to a successful qXfer read. Returns the
143 length of the (binary) data stored in BUF, corresponding
144 to as much of DATA/LEN as we could fit. IS_MORE controls
145 the first character of the response. */
147 write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
156 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
160 /* Handle all of the extended 'q' packets. */
162 handle_query (char *own_buf, int *new_packet_len_p)
164 static struct inferior_list_entry *thread_ptr;
166 if (strcmp ("qSymbol::", own_buf) == 0)
168 if (the_target->look_up_symbols != NULL)
169 (*the_target->look_up_symbols) ();
171 strcpy (own_buf, "OK");
175 if (strcmp ("qfThreadInfo", own_buf) == 0)
177 thread_ptr = all_threads.head;
178 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
179 thread_ptr = thread_ptr->next;
183 if (strcmp ("qsThreadInfo", own_buf) == 0)
185 if (thread_ptr != NULL)
187 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
188 thread_ptr = thread_ptr->next;
193 sprintf (own_buf, "l");
198 if (the_target->read_offsets != NULL
199 && strcmp ("qOffsets", own_buf) == 0)
201 CORE_ADDR text, data;
203 if (the_target->read_offsets (&text, &data))
204 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
205 (long)text, (long)data, (long)data);
212 if (the_target->read_auxv != NULL
213 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
221 /* Reject any annex; grab the offset and length. */
222 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
225 strcpy (own_buf, "E00");
229 /* Read one extra byte, as an indicator of whether there is
231 if (len > PBUFSIZ - 2)
233 data = malloc (len + 1);
234 n = (*the_target->read_auxv) (ofs, data, len + 1);
238 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
240 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
247 /* Protocol features query. */
248 if (strncmp ("qSupported", own_buf, 10) == 0
249 && (own_buf[10] == ':' || own_buf[10] == '\0'))
251 sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1);
253 if (the_target->read_auxv != NULL)
254 strcat (own_buf, ";qXfer:auxv:read+");
259 /* Thread-local storage support. */
260 if (the_target->get_tls_address != NULL
261 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
263 char *p = own_buf + 12;
264 CORE_ADDR parts[3], address = 0;
267 for (i = 0; i < 3; i++)
275 p2 = strchr (p, ',');
287 decode_address (&parts[i], p, len);
291 if (p != NULL || i < 3)
295 struct thread_info *thread = gdb_id_to_thread (parts[0]);
300 err = the_target->get_tls_address (thread, parts[1], parts[2],
306 sprintf (own_buf, "%llx", address);
315 /* Otherwise, pretend we do not understand this packet. */
318 /* Otherwise we didn't know what packet it was. Say we didn't
323 /* Parse vCont packets. */
325 handle_v_cont (char *own_buf, char *status, int *signal)
329 struct thread_resume *resume_info, default_action;
331 /* Count the number of semicolons in the packet. There should be one
340 /* Allocate room for one extra action, for the default remain-stopped
341 behavior; if no default action is in the list, we'll need the extra
343 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
345 default_action.thread = -1;
346 default_action.leave_stopped = 1;
347 default_action.step = 0;
348 default_action.sig = 0;
356 resume_info[i].leave_stopped = 0;
358 if (p[0] == 's' || p[0] == 'S')
359 resume_info[i].step = 1;
360 else if (p[0] == 'c' || p[0] == 'C')
361 resume_info[i].step = 0;
365 if (p[0] == 'S' || p[0] == 'C')
368 sig = strtol (p + 1, &q, 16);
373 if (!target_signal_to_host_p (sig))
375 resume_info[i].sig = target_signal_to_host (sig);
379 resume_info[i].sig = 0;
385 resume_info[i].thread = -1;
386 default_action = resume_info[i];
388 /* Note: we don't increment i here, we'll overwrite this entry
389 the next time through. */
391 else if (p[0] == ':')
393 unsigned int gdb_id = strtoul (p + 1, &q, 16);
394 unsigned long thread_id;
399 if (p[0] != ';' && p[0] != 0)
402 thread_id = gdb_id_to_thread_id (gdb_id);
404 resume_info[i].thread = thread_id;
412 resume_info[i] = default_action;
414 /* Still used in occasional places in the backend. */
415 if (n == 1 && resume_info[0].thread != -1)
416 cont_thread = resume_info[0].thread;
419 set_desired_inferior (0);
421 (*the_target->resume) (resume_info);
425 *signal = mywait (status, 1);
426 prepare_resume_reply (own_buf, *status, *signal);
430 /* No other way to report an error... */
431 strcpy (own_buf, "");
436 /* Handle all of the extended 'v' packets. */
438 handle_v_requests (char *own_buf, char *status, int *signal)
440 if (strncmp (own_buf, "vCont;", 6) == 0)
442 handle_v_cont (own_buf, status, signal);
446 if (strncmp (own_buf, "vCont?", 6) == 0)
448 strcpy (own_buf, "vCont;c;C;s;S");
452 /* Otherwise we didn't know what packet it was. Say we didn't
459 myresume (int step, int sig)
461 struct thread_resume resume_info[2];
464 if (step || sig || (cont_thread != 0 && cont_thread != -1))
466 resume_info[0].thread
467 = ((struct inferior_list_entry *) current_inferior)->id;
468 resume_info[0].step = step;
469 resume_info[0].sig = sig;
470 resume_info[0].leave_stopped = 0;
473 resume_info[n].thread = -1;
474 resume_info[n].step = 0;
475 resume_info[n].sig = 0;
476 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
478 (*the_target->resume) (resume_info);
484 gdbserver_version (void)
486 printf ("GNU gdbserver %s\n"
487 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
488 "gdbserver is free software, covered by the GNU General Public License.\n"
489 "This gdbserver was configured as \"%s\"\n",
494 gdbserver_usage (void)
496 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
497 "\tgdbserver COMM --attach PID\n"
499 "COMM may either be a tty device (for serial debugging), or \n"
500 "HOST:PORT to listen for a TCP connection.\n");
504 main (int argc, char *argv[])
506 char ch, status, *own_buf;
507 unsigned char *mem_buf;
516 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
518 gdbserver_version ();
522 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
528 if (setjmp (toplevel))
530 fprintf (stderr, "Exiting\n");
537 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
541 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
550 if (argc < 3 || bad_attach)
558 own_buf = malloc (PBUFSIZ);
559 mem_buf = malloc (PBUFSIZ);
563 /* Wait till we are at first instruction in program. */
564 signal = start_inferior (&argv[2], &status);
566 /* We are now stopped at the first instruction of the target process */
570 switch (attach_inferior (pid, &status, &signal))
573 error ("Attaching not supported on this target");
583 remote_open (argv[1]);
591 int new_packet_len = -1;
593 packet_len = getpkt (own_buf);
602 handle_query (own_buf, &new_packet_len);
605 remote_debug = !remote_debug;
608 /* Skip "detach" support on mingw32, since we don't have
611 fprintf (stderr, "Detaching from inferior\n");
617 /* If we are attached, then we can exit. Otherwise, we need to
618 hang around doing nothing, until the child is gone. */
624 ret = waitpid (signal_pid, &status, 0);
625 if (WIFEXITED (status) || WIFSIGNALED (status))
627 } while (ret != -1 || errno != ECHILD);
636 extended_protocol = 1;
637 prepare_resume_reply (own_buf, status, signal);
641 /* We can not use the extended protocol if we are
642 attached, because we can not restart the running
643 program. So return unrecognized. */
648 prepare_resume_reply (own_buf, status, signal);
651 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
653 unsigned long gdb_id, thread_id;
655 gdb_id = strtoul (&own_buf[2], NULL, 16);
656 thread_id = gdb_id_to_thread_id (gdb_id);
663 if (own_buf[1] == 'g')
665 general_thread = thread_id;
666 set_desired_inferior (1);
668 else if (own_buf[1] == 'c')
669 cont_thread = thread_id;
670 else if (own_buf[1] == 's')
671 step_thread = thread_id;
677 /* Silently ignore it so that gdb can extend the protocol
678 without compatibility headaches. */
683 set_desired_inferior (1);
684 registers_to_string (own_buf);
687 set_desired_inferior (1);
688 registers_from_string (&own_buf[1]);
692 decode_m_packet (&own_buf[1], &mem_addr, &len);
693 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
694 convert_int_to_ascii (mem_buf, own_buf, len);
699 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
700 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
706 if (decode_X_packet (&own_buf[1], packet_len - 1,
707 &mem_addr, &len, mem_buf) < 0
708 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
714 convert_ascii_to_int (own_buf + 1, &sig, 1);
715 if (target_signal_to_host_p (sig))
716 signal = target_signal_to_host (sig);
719 set_desired_inferior (0);
720 myresume (0, signal);
721 signal = mywait (&status, 1);
722 prepare_resume_reply (own_buf, status, signal);
725 convert_ascii_to_int (own_buf + 1, &sig, 1);
726 if (target_signal_to_host_p (sig))
727 signal = target_signal_to_host (sig);
730 set_desired_inferior (0);
731 myresume (1, signal);
732 signal = mywait (&status, 1);
733 prepare_resume_reply (own_buf, status, signal);
736 set_desired_inferior (0);
738 signal = mywait (&status, 1);
739 prepare_resume_reply (own_buf, status, signal);
742 set_desired_inferior (0);
744 signal = mywait (&status, 1);
745 prepare_resume_reply (own_buf, status, signal);
751 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
752 int len = strtol (lenptr + 1, &dataptr, 16);
753 char type = own_buf[1];
755 if (the_target->insert_watchpoint == NULL
756 || (type < '2' || type > '4'))
758 /* No watchpoint support or not a watchpoint command;
759 unrecognized either way. */
766 res = (*the_target->insert_watchpoint) (type, addr, len);
781 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
782 int len = strtol (lenptr + 1, &dataptr, 16);
783 char type = own_buf[1];
785 if (the_target->remove_watchpoint == NULL
786 || (type < '2' || type > '4'))
788 /* No watchpoint support or not a watchpoint command;
789 unrecognized either way. */
796 res = (*the_target->remove_watchpoint) (type, addr, len);
808 fprintf (stderr, "Killing inferior\n");
810 /* When using the extended protocol, we start up a new
811 debugging session. The traditional protocol will
813 if (extended_protocol)
816 fprintf (stderr, "GDBserver restarting\n");
818 /* Wait till we are at 1st instruction in prog. */
819 signal = start_inferior (&argv[2], &status);
830 unsigned long gdb_id, thread_id;
832 gdb_id = strtoul (&own_buf[1], NULL, 16);
833 thread_id = gdb_id_to_thread_id (gdb_id);
840 if (mythread_alive (thread_id))
847 /* Restarting the inferior is only supported in the
848 extended protocol. */
849 if (extended_protocol)
853 fprintf (stderr, "GDBserver restarting\n");
855 /* Wait till we are at 1st instruction in prog. */
856 signal = start_inferior (&argv[2], &status);
862 /* It is a request we don't understand. Respond with an
863 empty packet so that gdb knows that we don't support this
869 /* Extended (long) request. */
870 handle_v_requests (own_buf, &status, &signal);
873 /* It is a request we don't understand. Respond with an
874 empty packet so that gdb knows that we don't support this
880 if (new_packet_len != -1)
881 putpkt_binary (own_buf, new_packet_len);
887 "\nChild exited with status %d\n", signal);
889 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
890 target_signal_to_host (signal),
891 target_signal_to_name (signal));
892 if (status == 'W' || status == 'X')
894 if (extended_protocol)
896 fprintf (stderr, "Killing inferior\n");
899 fprintf (stderr, "GDBserver restarting\n");
901 /* Wait till we are at 1st instruction in prog. */
902 signal = start_inferior (&argv[2], &status);
908 fprintf (stderr, "GDBserver exiting\n");
914 /* We come here when getpkt fails.
916 For the extended remote protocol we exit (and this is the only
917 way we gracefully exit!).
919 For the traditional remote protocol close the connection,
920 and re-open it at the top of the loop. */
921 if (extended_protocol)
928 fprintf (stderr, "Remote side has terminated connection. "
929 "GDBserver will reopen the connection.\n");