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");
617 if (setjmp (toplevel
))
619 fprintf (stderr
, "Killing inferior\n");
626 remote_open (argv
[1]);
634 int new_packet_len
= -1;
636 packet_len
= getpkt (own_buf
);
645 handle_query (own_buf
, &new_packet_len
);
648 handle_general_set (own_buf
);
651 remote_debug
= !remote_debug
;
654 /* Skip "detach" support on mingw32, since we don't have
657 fprintf (stderr
, "Detaching from inferior\n");
663 /* If we are attached, then we can exit. Otherwise, we need to
664 hang around doing nothing, until the child is gone. */
670 ret
= waitpid (signal_pid
, &status
, 0);
671 if (WIFEXITED (status
) || WIFSIGNALED (status
))
673 } while (ret
!= -1 || errno
!= ECHILD
);
682 extended_protocol
= 1;
683 prepare_resume_reply (own_buf
, status
, signal
);
687 /* We can not use the extended protocol if we are
688 attached, because we can not restart the running
689 program. So return unrecognized. */
694 prepare_resume_reply (own_buf
, status
, signal
);
697 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
699 unsigned long gdb_id
, thread_id
;
701 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
702 thread_id
= gdb_id_to_thread_id (gdb_id
);
709 if (own_buf
[1] == 'g')
711 general_thread
= thread_id
;
712 set_desired_inferior (1);
714 else if (own_buf
[1] == 'c')
715 cont_thread
= thread_id
;
716 else if (own_buf
[1] == 's')
717 step_thread
= thread_id
;
723 /* Silently ignore it so that gdb can extend the protocol
724 without compatibility headaches. */
729 set_desired_inferior (1);
730 registers_to_string (own_buf
);
733 set_desired_inferior (1);
734 registers_from_string (&own_buf
[1]);
738 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
739 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
740 convert_int_to_ascii (mem_buf
, own_buf
, len
);
745 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
746 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
752 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
753 &mem_addr
, &len
, mem_buf
) < 0
754 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
760 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
761 if (target_signal_to_host_p (sig
))
762 signal
= target_signal_to_host (sig
);
765 set_desired_inferior (0);
766 myresume (0, signal
);
767 signal
= mywait (&status
, 1);
768 prepare_resume_reply (own_buf
, status
, signal
);
771 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
772 if (target_signal_to_host_p (sig
))
773 signal
= target_signal_to_host (sig
);
776 set_desired_inferior (0);
777 myresume (1, signal
);
778 signal
= mywait (&status
, 1);
779 prepare_resume_reply (own_buf
, status
, signal
);
782 set_desired_inferior (0);
784 signal
= mywait (&status
, 1);
785 prepare_resume_reply (own_buf
, status
, signal
);
788 set_desired_inferior (0);
790 signal
= mywait (&status
, 1);
791 prepare_resume_reply (own_buf
, status
, signal
);
797 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
798 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
799 char type
= own_buf
[1];
801 if (the_target
->insert_watchpoint
== NULL
802 || (type
< '2' || type
> '4'))
804 /* No watchpoint support or not a watchpoint command;
805 unrecognized either way. */
812 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
827 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
828 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
829 char type
= own_buf
[1];
831 if (the_target
->remove_watchpoint
== NULL
832 || (type
< '2' || type
> '4'))
834 /* No watchpoint support or not a watchpoint command;
835 unrecognized either way. */
842 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
854 fprintf (stderr
, "Killing inferior\n");
856 /* When using the extended protocol, we start up a new
857 debugging session. The traditional protocol will
859 if (extended_protocol
)
862 fprintf (stderr
, "GDBserver restarting\n");
864 /* Wait till we are at 1st instruction in prog. */
865 signal
= start_inferior (&argv
[2], &status
);
876 unsigned long gdb_id
, thread_id
;
878 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
879 thread_id
= gdb_id_to_thread_id (gdb_id
);
886 if (mythread_alive (thread_id
))
893 /* Restarting the inferior is only supported in the
894 extended protocol. */
895 if (extended_protocol
)
899 fprintf (stderr
, "GDBserver restarting\n");
901 /* Wait till we are at 1st instruction in prog. */
902 signal
= start_inferior (&argv
[2], &status
);
908 /* It is a request we don't understand. Respond with an
909 empty packet so that gdb knows that we don't support this
915 /* Extended (long) request. */
916 handle_v_requests (own_buf
, &status
, &signal
);
919 /* It is a request we don't understand. Respond with an
920 empty packet so that gdb knows that we don't support this
926 if (new_packet_len
!= -1)
927 putpkt_binary (own_buf
, new_packet_len
);
933 "\nChild exited with status %d\n", signal
);
935 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
936 target_signal_to_host (signal
),
937 target_signal_to_name (signal
));
938 if (status
== 'W' || status
== 'X')
940 if (extended_protocol
)
942 fprintf (stderr
, "Killing inferior\n");
945 fprintf (stderr
, "GDBserver restarting\n");
947 /* Wait till we are at 1st instruction in prog. */
948 signal
= start_inferior (&argv
[2], &status
);
954 fprintf (stderr
, "GDBserver exiting\n");
960 /* We come here when getpkt fails.
962 For the extended remote protocol we exit (and this is the only
963 way we gracefully exit!).
965 For the traditional remote protocol close the connection,
966 and re-open it at the top of the loop. */
967 if (extended_protocol
)
974 fprintf (stderr
, "Remote side has terminated connection. "
975 "GDBserver will reopen the connection.\n");