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);
236 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
238 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
245 /* Protocol features query. */
246 if (strncmp ("qSupported", own_buf, 10) == 0
247 && (own_buf[10] == ':' || own_buf[10] == '\0'))
249 sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1);
251 if (the_target->read_auxv != NULL)
252 strcat (own_buf, ";qXfer:auxv:read+");
257 /* Thread-local storage support. */
258 if (the_target->get_tls_address != NULL
259 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
261 char *p = own_buf + 12;
262 CORE_ADDR parts[3], address = 0;
265 for (i = 0; i < 3; i++)
273 p2 = strchr (p, ',');
285 decode_address (&parts[i], p, len);
289 if (p != NULL || i < 3)
293 struct thread_info *thread = gdb_id_to_thread (parts[0]);
298 err = the_target->get_tls_address (thread, parts[1], parts[2],
304 sprintf (own_buf, "%llx", address);
313 /* Otherwise, pretend we do not understand this packet. */
316 /* Otherwise we didn't know what packet it was. Say we didn't
321 /* Parse vCont packets. */
323 handle_v_cont (char *own_buf, char *status, int *signal)
327 struct thread_resume *resume_info, default_action;
329 /* Count the number of semicolons in the packet. There should be one
338 /* Allocate room for one extra action, for the default remain-stopped
339 behavior; if no default action is in the list, we'll need the extra
341 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
343 default_action.thread = -1;
344 default_action.leave_stopped = 1;
345 default_action.step = 0;
346 default_action.sig = 0;
354 resume_info[i].leave_stopped = 0;
356 if (p[0] == 's' || p[0] == 'S')
357 resume_info[i].step = 1;
358 else if (p[0] == 'c' || p[0] == 'C')
359 resume_info[i].step = 0;
363 if (p[0] == 'S' || p[0] == 'C')
366 sig = strtol (p + 1, &q, 16);
371 if (!target_signal_to_host_p (sig))
373 resume_info[i].sig = target_signal_to_host (sig);
377 resume_info[i].sig = 0;
383 resume_info[i].thread = -1;
384 default_action = resume_info[i];
386 /* Note: we don't increment i here, we'll overwrite this entry
387 the next time through. */
389 else if (p[0] == ':')
391 unsigned int gdb_id = strtoul (p + 1, &q, 16);
392 unsigned long thread_id;
397 if (p[0] != ';' && p[0] != 0)
400 thread_id = gdb_id_to_thread_id (gdb_id);
402 resume_info[i].thread = thread_id;
410 resume_info[i] = default_action;
412 /* Still used in occasional places in the backend. */
413 if (n == 1 && resume_info[0].thread != -1)
414 cont_thread = resume_info[0].thread;
417 set_desired_inferior (0);
419 (*the_target->resume) (resume_info);
423 *signal = mywait (status, 1);
424 prepare_resume_reply (own_buf, *status, *signal);
428 /* No other way to report an error... */
429 strcpy (own_buf, "");
434 /* Handle all of the extended 'v' packets. */
436 handle_v_requests (char *own_buf, char *status, int *signal)
438 if (strncmp (own_buf, "vCont;", 6) == 0)
440 handle_v_cont (own_buf, status, signal);
444 if (strncmp (own_buf, "vCont?", 6) == 0)
446 strcpy (own_buf, "vCont;c;C;s;S");
450 /* Otherwise we didn't know what packet it was. Say we didn't
457 myresume (int step, int sig)
459 struct thread_resume resume_info[2];
462 if (step || sig || (cont_thread != 0 && cont_thread != -1))
464 resume_info[0].thread
465 = ((struct inferior_list_entry *) current_inferior)->id;
466 resume_info[0].step = step;
467 resume_info[0].sig = sig;
468 resume_info[0].leave_stopped = 0;
471 resume_info[n].thread = -1;
472 resume_info[n].step = 0;
473 resume_info[n].sig = 0;
474 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
476 (*the_target->resume) (resume_info);
482 gdbserver_version (void)
484 printf ("GNU gdbserver %s\n"
485 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
486 "gdbserver is free software, covered by the GNU General Public License.\n"
487 "This gdbserver was configured as \"%s\"\n",
492 gdbserver_usage (void)
494 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
495 "\tgdbserver COMM --attach PID\n"
497 "COMM may either be a tty device (for serial debugging), or \n"
498 "HOST:PORT to listen for a TCP connection.\n");
502 main (int argc, char *argv[])
504 char ch, status, *own_buf;
505 unsigned char *mem_buf;
514 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
516 gdbserver_version ();
520 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
526 if (setjmp (toplevel))
528 fprintf (stderr, "Exiting\n");
535 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
539 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
548 if (argc < 3 || bad_attach)
556 own_buf = malloc (PBUFSIZ);
557 mem_buf = malloc (PBUFSIZ);
561 /* Wait till we are at first instruction in program. */
562 signal = start_inferior (&argv[2], &status);
564 /* We are now stopped at the first instruction of the target process */
568 switch (attach_inferior (pid, &status, &signal))
571 error ("Attaching not supported on this target");
581 remote_open (argv[1]);
589 int new_packet_len = -1;
591 packet_len = getpkt (own_buf);
600 handle_query (own_buf, &new_packet_len);
603 remote_debug = !remote_debug;
606 /* Skip "detach" support on mingw32, since we don't have
609 fprintf (stderr, "Detaching from inferior\n");
615 /* If we are attached, then we can exit. Otherwise, we need to
616 hang around doing nothing, until the child is gone. */
622 ret = waitpid (signal_pid, &status, 0);
623 if (WIFEXITED (status) || WIFSIGNALED (status))
625 } while (ret != -1 || errno != ECHILD);
634 extended_protocol = 1;
635 prepare_resume_reply (own_buf, status, signal);
639 /* We can not use the extended protocol if we are
640 attached, because we can not restart the running
641 program. So return unrecognized. */
646 prepare_resume_reply (own_buf, status, signal);
649 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
651 unsigned long gdb_id, thread_id;
653 gdb_id = strtoul (&own_buf[2], NULL, 16);
654 thread_id = gdb_id_to_thread_id (gdb_id);
661 if (own_buf[1] == 'g')
663 general_thread = thread_id;
664 set_desired_inferior (1);
666 else if (own_buf[1] == 'c')
667 cont_thread = thread_id;
668 else if (own_buf[1] == 's')
669 step_thread = thread_id;
675 /* Silently ignore it so that gdb can extend the protocol
676 without compatibility headaches. */
681 set_desired_inferior (1);
682 registers_to_string (own_buf);
685 set_desired_inferior (1);
686 registers_from_string (&own_buf[1]);
690 decode_m_packet (&own_buf[1], &mem_addr, &len);
691 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
692 convert_int_to_ascii (mem_buf, own_buf, len);
697 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
698 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
704 if (decode_X_packet (&own_buf[1], packet_len - 1,
705 &mem_addr, &len, mem_buf) < 0
706 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
712 convert_ascii_to_int (own_buf + 1, &sig, 1);
713 if (target_signal_to_host_p (sig))
714 signal = target_signal_to_host (sig);
717 set_desired_inferior (0);
718 myresume (0, signal);
719 signal = mywait (&status, 1);
720 prepare_resume_reply (own_buf, status, signal);
723 convert_ascii_to_int (own_buf + 1, &sig, 1);
724 if (target_signal_to_host_p (sig))
725 signal = target_signal_to_host (sig);
728 set_desired_inferior (0);
729 myresume (1, signal);
730 signal = mywait (&status, 1);
731 prepare_resume_reply (own_buf, status, signal);
734 set_desired_inferior (0);
736 signal = mywait (&status, 1);
737 prepare_resume_reply (own_buf, status, signal);
740 set_desired_inferior (0);
742 signal = mywait (&status, 1);
743 prepare_resume_reply (own_buf, status, signal);
749 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
750 int len = strtol (lenptr + 1, &dataptr, 16);
751 char type = own_buf[1];
753 if (the_target->insert_watchpoint == NULL
754 || (type < '2' || type > '4'))
756 /* No watchpoint support or not a watchpoint command;
757 unrecognized either way. */
764 res = (*the_target->insert_watchpoint) (type, addr, len);
779 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
780 int len = strtol (lenptr + 1, &dataptr, 16);
781 char type = own_buf[1];
783 if (the_target->remove_watchpoint == NULL
784 || (type < '2' || type > '4'))
786 /* No watchpoint support or not a watchpoint command;
787 unrecognized either way. */
794 res = (*the_target->remove_watchpoint) (type, addr, len);
806 fprintf (stderr, "Killing inferior\n");
808 /* When using the extended protocol, we start up a new
809 debugging session. The traditional protocol will
811 if (extended_protocol)
814 fprintf (stderr, "GDBserver restarting\n");
816 /* Wait till we are at 1st instruction in prog. */
817 signal = start_inferior (&argv[2], &status);
828 unsigned long gdb_id, thread_id;
830 gdb_id = strtoul (&own_buf[1], NULL, 16);
831 thread_id = gdb_id_to_thread_id (gdb_id);
838 if (mythread_alive (thread_id))
845 /* Restarting the inferior is only supported in the
846 extended protocol. */
847 if (extended_protocol)
851 fprintf (stderr, "GDBserver restarting\n");
853 /* Wait till we are at 1st instruction in prog. */
854 signal = start_inferior (&argv[2], &status);
860 /* It is a request we don't understand. Respond with an
861 empty packet so that gdb knows that we don't support this
867 /* Extended (long) request. */
868 handle_v_requests (own_buf, &status, &signal);
871 /* It is a request we don't understand. Respond with an
872 empty packet so that gdb knows that we don't support this
878 if (new_packet_len != -1)
879 putpkt_binary (own_buf, new_packet_len);
885 "\nChild exited with status %d\n", signal);
887 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
888 target_signal_to_host (signal),
889 target_signal_to_name (signal));
890 if (status == 'W' || status == 'X')
892 if (extended_protocol)
894 fprintf (stderr, "Killing inferior\n");
897 fprintf (stderr, "GDBserver restarting\n");
899 /* Wait till we are at 1st instruction in prog. */
900 signal = start_inferior (&argv[2], &status);
906 fprintf (stderr, "GDBserver exiting\n");
912 /* We come here when getpkt fails.
914 For the extended remote protocol we exit (and this is the only
915 way we gracefully exit!).
917 For the traditional remote protocol close the connection,
918 and re-open it at the top of the loop. */
919 if (extended_protocol)
926 fprintf (stderr, "Remote side has terminated connection. "
927 "GDBserver will reopen the connection.\n");