1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
30 unsigned long cont_thread
;
31 unsigned long general_thread
;
32 unsigned long step_thread
;
33 unsigned long thread_from_wait
;
34 unsigned long old_thread_from_wait
;
35 int extended_protocol
;
38 int pass_signals
[TARGET_SIGNAL_LAST
];
42 /* The PID of the originally created or attached inferior. Used to
43 send signals to the process when GDB sends us an asynchronous interrupt
44 (user hitting Control-C in the client), and to wait for the child to exit
45 when no longer debugging it. */
47 unsigned long signal_pid
;
50 /* A file descriptor for the controlling terminal. */
53 /* TERMINAL_FD's original foreground group. */
54 pid_t old_foreground_pgrp
;
56 /* Hand back terminal ownership to the original foreground group. */
59 restore_old_foreground_pgrp (void)
61 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
66 start_inferior (char *argv
[], char *statusptr
)
69 signal (SIGTTOU
, SIG_DFL
);
70 signal (SIGTTIN
, SIG_DFL
);
73 signal_pid
= create_inferior (argv
[0], argv
);
75 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
80 signal (SIGTTOU
, SIG_IGN
);
81 signal (SIGTTIN
, SIG_IGN
);
82 terminal_fd
= fileno (stderr
);
83 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
84 tcsetpgrp (terminal_fd
, signal_pid
);
85 atexit (restore_old_foreground_pgrp
);
88 /* Wait till we are at 1st instruction in program, return signal number. */
89 return mywait (statusptr
, 0);
93 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
95 /* myattach should return -1 if attaching is unsupported,
96 0 if it succeeded, and call error() otherwise. */
98 if (myattach (pid
) != 0)
101 fprintf (stderr
, "Attached; pid = %d\n", pid
);
104 /* FIXME - It may be that we should get the SIGNAL_PID from the
105 attach function, so that it can be the main thread instead of
106 whichever we were told to attach to. */
109 *sigptr
= mywait (statusptr
, 0);
111 /* GDB knows to ignore the first SIGSTOP after attaching to a running
112 process using the "attach" command, but this is different; it's
113 just using "target remote". Pretend it's just starting up. */
114 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
115 *sigptr
= TARGET_SIGNAL_TRAP
;
120 extern int remote_debug
;
122 /* Decode a qXfer read request. Return 0 if everything looks OK,
126 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
128 /* Extract and NUL-terminate the annex. */
130 while (*buf
&& *buf
!= ':')
136 /* After the read/write marker and annex, qXfer looks like a
137 traditional 'm' packet. */
138 decode_m_packet (buf
, ofs
, len
);
143 /* Write the response to a successful qXfer read. Returns the
144 length of the (binary) data stored in BUF, corresponding
145 to as much of DATA/LEN as we could fit. IS_MORE controls
146 the first character of the response. */
148 write_qxfer_response (char *buf
, unsigned char *data
, int len
, int is_more
)
157 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
161 /* Handle all of the extended 'Q' packets. */
163 handle_general_set (char *own_buf
)
165 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
167 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
168 const char *p
= own_buf
+ strlen ("QPassSignals:");
171 p
= decode_address_to_semicolon (&cursig
, p
);
172 for (i
= 0; i
< numsigs
; i
++)
178 /* Keep looping, to clear the remaining signals. */
181 p
= decode_address_to_semicolon (&cursig
, p
);
186 strcpy (own_buf
, "OK");
190 /* Otherwise we didn't know what packet it was. Say we didn't
195 /* Handle all of the extended 'q' packets. */
197 handle_query (char *own_buf
, int *new_packet_len_p
)
199 static struct inferior_list_entry
*thread_ptr
;
201 if (strcmp ("qSymbol::", own_buf
) == 0)
203 if (the_target
->look_up_symbols
!= NULL
)
204 (*the_target
->look_up_symbols
) ();
206 strcpy (own_buf
, "OK");
210 if (strcmp ("qfThreadInfo", own_buf
) == 0)
212 thread_ptr
= all_threads
.head
;
213 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
214 thread_ptr
= thread_ptr
->next
;
218 if (strcmp ("qsThreadInfo", own_buf
) == 0)
220 if (thread_ptr
!= NULL
)
222 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
223 thread_ptr
= thread_ptr
->next
;
228 sprintf (own_buf
, "l");
233 if (the_target
->read_offsets
!= NULL
234 && strcmp ("qOffsets", own_buf
) == 0)
236 CORE_ADDR text
, data
;
238 if (the_target
->read_offsets (&text
, &data
))
239 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
240 (long)text
, (long)data
, (long)data
);
247 if (the_target
->read_auxv
!= NULL
248 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
256 /* Reject any annex; grab the offset and length. */
257 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
260 strcpy (own_buf
, "E00");
264 /* Read one extra byte, as an indicator of whether there is
266 if (len
> PBUFSIZ
- 2)
268 data
= malloc (len
+ 1);
269 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
273 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
275 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
282 /* Protocol features query. */
283 if (strncmp ("qSupported", own_buf
, 10) == 0
284 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
286 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
288 if (the_target
->read_auxv
!= NULL
)
289 strcat (own_buf
, ";qXfer:auxv:read+");
294 /* Thread-local storage support. */
295 if (the_target
->get_tls_address
!= NULL
296 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
298 char *p
= own_buf
+ 12;
299 CORE_ADDR parts
[3], address
= 0;
302 for (i
= 0; i
< 3; i
++)
310 p2
= strchr (p
, ',');
322 decode_address (&parts
[i
], p
, len
);
326 if (p
!= NULL
|| i
< 3)
330 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
335 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
341 sprintf (own_buf
, "%llx", address
);
350 /* Otherwise, pretend we do not understand this packet. */
353 /* Otherwise we didn't know what packet it was. Say we didn't
358 /* Parse vCont packets. */
360 handle_v_cont (char *own_buf
, char *status
, int *signal
)
364 struct thread_resume
*resume_info
, default_action
;
366 /* Count the number of semicolons in the packet. There should be one
375 /* Allocate room for one extra action, for the default remain-stopped
376 behavior; if no default action is in the list, we'll need the extra
378 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
380 default_action
.thread
= -1;
381 default_action
.leave_stopped
= 1;
382 default_action
.step
= 0;
383 default_action
.sig
= 0;
391 resume_info
[i
].leave_stopped
= 0;
393 if (p
[0] == 's' || p
[0] == 'S')
394 resume_info
[i
].step
= 1;
395 else if (p
[0] == 'c' || p
[0] == 'C')
396 resume_info
[i
].step
= 0;
400 if (p
[0] == 'S' || p
[0] == 'C')
403 sig
= strtol (p
+ 1, &q
, 16);
408 if (!target_signal_to_host_p (sig
))
410 resume_info
[i
].sig
= target_signal_to_host (sig
);
414 resume_info
[i
].sig
= 0;
420 resume_info
[i
].thread
= -1;
421 default_action
= resume_info
[i
];
423 /* Note: we don't increment i here, we'll overwrite this entry
424 the next time through. */
426 else if (p
[0] == ':')
428 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
429 unsigned long thread_id
;
434 if (p
[0] != ';' && p
[0] != 0)
437 thread_id
= gdb_id_to_thread_id (gdb_id
);
439 resume_info
[i
].thread
= thread_id
;
447 resume_info
[i
] = default_action
;
449 /* Still used in occasional places in the backend. */
450 if (n
== 1 && resume_info
[0].thread
!= -1)
451 cont_thread
= resume_info
[0].thread
;
454 set_desired_inferior (0);
456 (*the_target
->resume
) (resume_info
);
460 *signal
= mywait (status
, 1);
461 prepare_resume_reply (own_buf
, *status
, *signal
);
465 /* No other way to report an error... */
466 strcpy (own_buf
, "");
471 /* Handle all of the extended 'v' packets. */
473 handle_v_requests (char *own_buf
, char *status
, int *signal
)
475 if (strncmp (own_buf
, "vCont;", 6) == 0)
477 handle_v_cont (own_buf
, status
, signal
);
481 if (strncmp (own_buf
, "vCont?", 6) == 0)
483 strcpy (own_buf
, "vCont;c;C;s;S");
487 /* Otherwise we didn't know what packet it was. Say we didn't
494 myresume (int step
, int sig
)
496 struct thread_resume resume_info
[2];
499 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
501 resume_info
[0].thread
502 = ((struct inferior_list_entry
*) current_inferior
)->id
;
503 resume_info
[0].step
= step
;
504 resume_info
[0].sig
= sig
;
505 resume_info
[0].leave_stopped
= 0;
508 resume_info
[n
].thread
= -1;
509 resume_info
[n
].step
= 0;
510 resume_info
[n
].sig
= 0;
511 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
513 (*the_target
->resume
) (resume_info
);
519 gdbserver_version (void)
521 printf ("GNU gdbserver %s\n"
522 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
523 "gdbserver is free software, covered by the GNU General Public License.\n"
524 "This gdbserver was configured as \"%s\"\n",
529 gdbserver_usage (void)
531 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
532 "\tgdbserver COMM --attach PID\n"
534 "COMM may either be a tty device (for serial debugging), or \n"
535 "HOST:PORT to listen for a TCP connection.\n");
539 main (int argc
, char *argv
[])
541 char ch
, status
, *own_buf
;
542 unsigned char *mem_buf
;
551 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
553 gdbserver_version ();
557 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
563 if (setjmp (toplevel
))
565 fprintf (stderr
, "Exiting\n");
572 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
576 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
585 if (argc
< 3 || bad_attach
)
593 own_buf
= malloc (PBUFSIZ
);
594 mem_buf
= malloc (PBUFSIZ
);
598 /* Wait till we are at first instruction in program. */
599 signal
= start_inferior (&argv
[2], &status
);
601 /* We are now stopped at the first instruction of the target process */
605 switch (attach_inferior (pid
, &status
, &signal
))
608 error ("Attaching not supported on this target");
616 if (setjmp (toplevel
))
618 fprintf (stderr
, "Killing inferior\n");
625 remote_open (argv
[1]);
633 int new_packet_len
= -1;
635 packet_len
= getpkt (own_buf
);
644 handle_query (own_buf
, &new_packet_len
);
647 handle_general_set (own_buf
);
650 remote_debug
= !remote_debug
;
653 /* Skip "detach" support on mingw32, since we don't have
656 fprintf (stderr
, "Detaching from inferior\n");
662 /* If we are attached, then we can exit. Otherwise, we need to
663 hang around doing nothing, until the child is gone. */
669 ret
= waitpid (signal_pid
, &status
, 0);
670 if (WIFEXITED (status
) || WIFSIGNALED (status
))
672 } while (ret
!= -1 || errno
!= ECHILD
);
681 extended_protocol
= 1;
682 prepare_resume_reply (own_buf
, status
, signal
);
686 /* We can not use the extended protocol if we are
687 attached, because we can not restart the running
688 program. So return unrecognized. */
693 prepare_resume_reply (own_buf
, status
, signal
);
696 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
698 unsigned long gdb_id
, thread_id
;
700 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
701 thread_id
= gdb_id_to_thread_id (gdb_id
);
708 if (own_buf
[1] == 'g')
710 general_thread
= thread_id
;
711 set_desired_inferior (1);
713 else if (own_buf
[1] == 'c')
714 cont_thread
= thread_id
;
715 else if (own_buf
[1] == 's')
716 step_thread
= thread_id
;
722 /* Silently ignore it so that gdb can extend the protocol
723 without compatibility headaches. */
728 set_desired_inferior (1);
729 registers_to_string (own_buf
);
732 set_desired_inferior (1);
733 registers_from_string (&own_buf
[1]);
737 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
738 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
739 convert_int_to_ascii (mem_buf
, own_buf
, len
);
744 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
745 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
751 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
752 &mem_addr
, &len
, mem_buf
) < 0
753 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
759 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
760 if (target_signal_to_host_p (sig
))
761 signal
= target_signal_to_host (sig
);
764 set_desired_inferior (0);
765 myresume (0, signal
);
766 signal
= mywait (&status
, 1);
767 prepare_resume_reply (own_buf
, status
, signal
);
770 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
771 if (target_signal_to_host_p (sig
))
772 signal
= target_signal_to_host (sig
);
775 set_desired_inferior (0);
776 myresume (1, signal
);
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
);
787 set_desired_inferior (0);
789 signal
= mywait (&status
, 1);
790 prepare_resume_reply (own_buf
, status
, signal
);
796 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
797 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
798 char type
= own_buf
[1];
800 if (the_target
->insert_watchpoint
== NULL
801 || (type
< '2' || type
> '4'))
803 /* No watchpoint support or not a watchpoint command;
804 unrecognized either way. */
811 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
826 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
827 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
828 char type
= own_buf
[1];
830 if (the_target
->remove_watchpoint
== NULL
831 || (type
< '2' || type
> '4'))
833 /* No watchpoint support or not a watchpoint command;
834 unrecognized either way. */
841 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
853 fprintf (stderr
, "Killing inferior\n");
855 /* When using the extended protocol, we start up a new
856 debugging session. The traditional protocol will
858 if (extended_protocol
)
861 fprintf (stderr
, "GDBserver restarting\n");
863 /* Wait till we are at 1st instruction in prog. */
864 signal
= start_inferior (&argv
[2], &status
);
875 unsigned long gdb_id
, thread_id
;
877 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
878 thread_id
= gdb_id_to_thread_id (gdb_id
);
885 if (mythread_alive (thread_id
))
892 /* Restarting the inferior is only supported in the
893 extended protocol. */
894 if (extended_protocol
)
898 fprintf (stderr
, "GDBserver restarting\n");
900 /* Wait till we are at 1st instruction in prog. */
901 signal
= start_inferior (&argv
[2], &status
);
907 /* It is a request we don't understand. Respond with an
908 empty packet so that gdb knows that we don't support this
914 /* Extended (long) request. */
915 handle_v_requests (own_buf
, &status
, &signal
);
918 /* It is a request we don't understand. Respond with an
919 empty packet so that gdb knows that we don't support this
925 if (new_packet_len
!= -1)
926 putpkt_binary (own_buf
, new_packet_len
);
932 "\nChild exited with status %d\n", signal
);
934 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
935 target_signal_to_host (signal
),
936 target_signal_to_name (signal
));
937 if (status
== 'W' || status
== 'X')
939 if (extended_protocol
)
941 fprintf (stderr
, "Killing inferior\n");
944 fprintf (stderr
, "GDBserver restarting\n");
946 /* Wait till we are at 1st instruction in prog. */
947 signal
= start_inferior (&argv
[2], &status
);
953 fprintf (stderr
, "GDBserver exiting\n");
959 /* We come here when getpkt fails.
961 For the extended remote protocol we exit (and this is the only
962 way we gracefully exit!).
964 For the traditional remote protocol close the connection,
965 and re-open it at the top of the loop. */
966 if (extended_protocol
)
973 fprintf (stderr
, "Remote side has terminated connection. "
974 "GDBserver will reopen the connection.\n");