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 /* Enable miscellaneous debugging output. The name is historical - it
39 was originally used to debug LinuxThreads support. */
42 int pass_signals
[TARGET_SIGNAL_LAST
];
46 /* The PID of the originally created or attached inferior. Used to
47 send signals to the process when GDB sends us an asynchronous interrupt
48 (user hitting Control-C in the client), and to wait for the child to exit
49 when no longer debugging it. */
51 unsigned long signal_pid
;
54 /* A file descriptor for the controlling terminal. */
57 /* TERMINAL_FD's original foreground group. */
58 pid_t old_foreground_pgrp
;
60 /* Hand back terminal ownership to the original foreground group. */
63 restore_old_foreground_pgrp (void)
65 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
70 start_inferior (char *argv
[], char *statusptr
)
73 signal (SIGTTOU
, SIG_DFL
);
74 signal (SIGTTIN
, SIG_DFL
);
77 signal_pid
= create_inferior (argv
[0], argv
);
79 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
84 signal (SIGTTOU
, SIG_IGN
);
85 signal (SIGTTIN
, SIG_IGN
);
86 terminal_fd
= fileno (stderr
);
87 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
88 tcsetpgrp (terminal_fd
, signal_pid
);
89 atexit (restore_old_foreground_pgrp
);
92 /* Wait till we are at 1st instruction in program, return signal number. */
93 return mywait (statusptr
, 0);
97 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
99 /* myattach should return -1 if attaching is unsupported,
100 0 if it succeeded, and call error() otherwise. */
102 if (myattach (pid
) != 0)
105 fprintf (stderr
, "Attached; pid = %d\n", pid
);
108 /* FIXME - It may be that we should get the SIGNAL_PID from the
109 attach function, so that it can be the main thread instead of
110 whichever we were told to attach to. */
113 *sigptr
= mywait (statusptr
, 0);
115 /* GDB knows to ignore the first SIGSTOP after attaching to a running
116 process using the "attach" command, but this is different; it's
117 just using "target remote". Pretend it's just starting up. */
118 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
119 *sigptr
= TARGET_SIGNAL_TRAP
;
124 extern int remote_debug
;
126 /* Decode a qXfer read request. Return 0 if everything looks OK,
130 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
132 /* Extract and NUL-terminate the annex. */
134 while (*buf
&& *buf
!= ':')
140 /* After the read/write marker and annex, qXfer looks like a
141 traditional 'm' packet. */
142 decode_m_packet (buf
, ofs
, len
);
147 /* Write the response to a successful qXfer read. Returns the
148 length of the (binary) data stored in BUF, corresponding
149 to as much of DATA/LEN as we could fit. IS_MORE controls
150 the first character of the response. */
152 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
161 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
165 /* Handle all of the extended 'Q' packets. */
167 handle_general_set (char *own_buf
)
169 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
171 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
172 const char *p
= own_buf
+ strlen ("QPassSignals:");
175 p
= decode_address_to_semicolon (&cursig
, p
);
176 for (i
= 0; i
< numsigs
; i
++)
182 /* Keep looping, to clear the remaining signals. */
185 p
= decode_address_to_semicolon (&cursig
, p
);
190 strcpy (own_buf
, "OK");
194 /* Otherwise we didn't know what packet it was. Say we didn't
200 get_features_xml (const char *annex
)
202 static int features_supported
= -1;
203 static char *document
;
206 extern const char *const xml_builtin
[][2];
209 /* Look for the annex. */
210 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
211 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
214 if (xml_builtin
[i
][0] != NULL
)
215 return xml_builtin
[i
][1];
218 if (strcmp (annex
, "target.xml") != 0)
221 if (features_supported
== -1)
223 const char *arch
= NULL
;
224 if (the_target
->arch_string
!= NULL
)
225 arch
= (*the_target
->arch_string
) ();
228 features_supported
= 0;
231 features_supported
= 1;
232 document
= malloc (64 + strlen (arch
));
233 snprintf (document
, 64 + strlen (arch
),
234 "<target><architecture>%s</architecture></target>",
243 monitor_show_help (void)
245 monitor_output ("The following monitor commands are supported:\n");
246 monitor_output (" set debug <0|1>\n");
247 monitor_output (" Enable general debugging messages\n");
248 monitor_output (" set remote-debug <0|1>\n");
249 monitor_output (" Enable remote protocol debugging messages\n");
252 /* Handle all of the extended 'q' packets. */
254 handle_query (char *own_buf
, int *new_packet_len_p
)
256 static struct inferior_list_entry
*thread_ptr
;
258 if (strcmp ("qSymbol::", own_buf
) == 0)
260 if (the_target
->look_up_symbols
!= NULL
)
261 (*the_target
->look_up_symbols
) ();
263 strcpy (own_buf
, "OK");
267 if (strcmp ("qfThreadInfo", own_buf
) == 0)
269 thread_ptr
= all_threads
.head
;
270 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
271 thread_ptr
= thread_ptr
->next
;
275 if (strcmp ("qsThreadInfo", own_buf
) == 0)
277 if (thread_ptr
!= NULL
)
279 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
280 thread_ptr
= thread_ptr
->next
;
285 sprintf (own_buf
, "l");
290 if (the_target
->read_offsets
!= NULL
291 && strcmp ("qOffsets", own_buf
) == 0)
293 CORE_ADDR text
, data
;
295 if (the_target
->read_offsets (&text
, &data
))
296 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
297 (long)text
, (long)data
, (long)data
);
304 if (the_target
->read_auxv
!= NULL
305 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
313 /* Reject any annex; grab the offset and length. */
314 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
317 strcpy (own_buf
, "E00");
321 /* Read one extra byte, as an indicator of whether there is
323 if (len
> PBUFSIZ
- 2)
325 data
= malloc (len
+ 1);
326 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
330 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
332 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
339 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
342 unsigned int len
, total_len
;
343 const char *document
;
346 /* Check for support. */
347 document
= get_features_xml ("target.xml");
348 if (document
== NULL
)
354 /* Grab the annex, offset, and length. */
355 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
357 strcpy (own_buf
, "E00");
361 /* Now grab the correct annex. */
362 document
= get_features_xml (annex
);
363 if (document
== NULL
)
365 strcpy (own_buf
, "E00");
369 total_len
= strlen (document
);
370 if (len
> PBUFSIZ
- 2)
375 else if (len
< total_len
- ofs
)
376 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
379 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
385 /* Protocol features query. */
386 if (strncmp ("qSupported", own_buf
, 10) == 0
387 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
389 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
391 if (the_target
->read_auxv
!= NULL
)
392 strcat (own_buf
, ";qXfer:auxv:read+");
394 if (get_features_xml ("target.xml") != NULL
)
395 strcat (own_buf
, ";qXfer:features:read+");
400 /* Thread-local storage support. */
401 if (the_target
->get_tls_address
!= NULL
402 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
404 char *p
= own_buf
+ 12;
405 CORE_ADDR parts
[3], address
= 0;
408 for (i
= 0; i
< 3; i
++)
416 p2
= strchr (p
, ',');
428 decode_address (&parts
[i
], p
, len
);
432 if (p
!= NULL
|| i
< 3)
436 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
441 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
447 sprintf (own_buf
, "%llx", address
);
456 /* Otherwise, pretend we do not understand this packet. */
459 /* Handle "monitor" commands. */
460 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
462 char *mon
= malloc (PBUFSIZ
);
463 int len
= strlen (own_buf
+ 6);
465 if ((len
% 1) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
475 if (strcmp (mon
, "set debug 1") == 0)
478 monitor_output ("Debug output enabled.\n");
480 else if (strcmp (mon
, "set debug 0") == 0)
483 monitor_output ("Debug output disabled.\n");
485 else if (strcmp (mon
, "set remote-debug 1") == 0)
488 monitor_output ("Protocol debug output enabled.\n");
490 else if (strcmp (mon
, "set remote-debug 0") == 0)
493 monitor_output ("Protocol debug output disabled.\n");
495 else if (strcmp (mon
, "help") == 0)
496 monitor_show_help ();
499 monitor_output ("Unknown monitor command.\n\n");
500 monitor_show_help ();
508 /* Otherwise we didn't know what packet it was. Say we didn't
513 /* Parse vCont packets. */
515 handle_v_cont (char *own_buf
, char *status
, int *signal
)
519 struct thread_resume
*resume_info
, default_action
;
521 /* Count the number of semicolons in the packet. There should be one
530 /* Allocate room for one extra action, for the default remain-stopped
531 behavior; if no default action is in the list, we'll need the extra
533 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
535 default_action
.thread
= -1;
536 default_action
.leave_stopped
= 1;
537 default_action
.step
= 0;
538 default_action
.sig
= 0;
546 resume_info
[i
].leave_stopped
= 0;
548 if (p
[0] == 's' || p
[0] == 'S')
549 resume_info
[i
].step
= 1;
550 else if (p
[0] == 'c' || p
[0] == 'C')
551 resume_info
[i
].step
= 0;
555 if (p
[0] == 'S' || p
[0] == 'C')
558 sig
= strtol (p
+ 1, &q
, 16);
563 if (!target_signal_to_host_p (sig
))
565 resume_info
[i
].sig
= target_signal_to_host (sig
);
569 resume_info
[i
].sig
= 0;
575 resume_info
[i
].thread
= -1;
576 default_action
= resume_info
[i
];
578 /* Note: we don't increment i here, we'll overwrite this entry
579 the next time through. */
581 else if (p
[0] == ':')
583 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
584 unsigned long thread_id
;
589 if (p
[0] != ';' && p
[0] != 0)
592 thread_id
= gdb_id_to_thread_id (gdb_id
);
594 resume_info
[i
].thread
= thread_id
;
602 resume_info
[i
] = default_action
;
604 /* Still used in occasional places in the backend. */
605 if (n
== 1 && resume_info
[0].thread
!= -1)
606 cont_thread
= resume_info
[0].thread
;
609 set_desired_inferior (0);
611 (*the_target
->resume
) (resume_info
);
615 *signal
= mywait (status
, 1);
616 prepare_resume_reply (own_buf
, *status
, *signal
);
620 /* No other way to report an error... */
621 strcpy (own_buf
, "");
626 /* Handle all of the extended 'v' packets. */
628 handle_v_requests (char *own_buf
, char *status
, int *signal
)
630 if (strncmp (own_buf
, "vCont;", 6) == 0)
632 handle_v_cont (own_buf
, status
, signal
);
636 if (strncmp (own_buf
, "vCont?", 6) == 0)
638 strcpy (own_buf
, "vCont;c;C;s;S");
642 /* Otherwise we didn't know what packet it was. Say we didn't
649 myresume (int step
, int sig
)
651 struct thread_resume resume_info
[2];
654 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
656 resume_info
[0].thread
657 = ((struct inferior_list_entry
*) current_inferior
)->id
;
658 resume_info
[0].step
= step
;
659 resume_info
[0].sig
= sig
;
660 resume_info
[0].leave_stopped
= 0;
663 resume_info
[n
].thread
= -1;
664 resume_info
[n
].step
= 0;
665 resume_info
[n
].sig
= 0;
666 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
668 (*the_target
->resume
) (resume_info
);
674 gdbserver_version (void)
676 printf ("GNU gdbserver %s\n"
677 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
678 "gdbserver is free software, covered by the GNU General Public License.\n"
679 "This gdbserver was configured as \"%s\"\n",
684 gdbserver_usage (void)
686 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
687 "\tgdbserver COMM --attach PID\n"
689 "COMM may either be a tty device (for serial debugging), or \n"
690 "HOST:PORT to listen for a TCP connection.\n");
694 main (int argc
, char *argv
[])
696 char ch
, status
, *own_buf
;
697 unsigned char *mem_buf
;
706 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
708 gdbserver_version ();
712 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
718 if (setjmp (toplevel
))
720 fprintf (stderr
, "Exiting\n");
727 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
731 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
740 if (argc
< 3 || bad_attach
)
748 own_buf
= malloc (PBUFSIZ
);
749 mem_buf
= malloc (PBUFSIZ
);
753 /* Wait till we are at first instruction in program. */
754 signal
= start_inferior (&argv
[2], &status
);
756 /* We are now stopped at the first instruction of the target process */
760 switch (attach_inferior (pid
, &status
, &signal
))
763 error ("Attaching not supported on this target");
771 if (setjmp (toplevel
))
773 fprintf (stderr
, "Killing inferior\n");
780 remote_open (argv
[1]);
788 int new_packet_len
= -1;
790 packet_len
= getpkt (own_buf
);
799 handle_query (own_buf
, &new_packet_len
);
802 handle_general_set (own_buf
);
805 /* Skip "detach" support on mingw32, since we don't have
808 fprintf (stderr
, "Detaching from inferior\n");
814 /* If we are attached, then we can exit. Otherwise, we need to
815 hang around doing nothing, until the child is gone. */
821 ret
= waitpid (signal_pid
, &status
, 0);
822 if (WIFEXITED (status
) || WIFSIGNALED (status
))
824 } while (ret
!= -1 || errno
!= ECHILD
);
833 extended_protocol
= 1;
834 prepare_resume_reply (own_buf
, status
, signal
);
838 /* We can not use the extended protocol if we are
839 attached, because we can not restart the running
840 program. So return unrecognized. */
845 prepare_resume_reply (own_buf
, status
, signal
);
848 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
850 unsigned long gdb_id
, thread_id
;
852 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
853 thread_id
= gdb_id_to_thread_id (gdb_id
);
860 if (own_buf
[1] == 'g')
862 general_thread
= thread_id
;
863 set_desired_inferior (1);
865 else if (own_buf
[1] == 'c')
866 cont_thread
= thread_id
;
867 else if (own_buf
[1] == 's')
868 step_thread
= thread_id
;
874 /* Silently ignore it so that gdb can extend the protocol
875 without compatibility headaches. */
880 set_desired_inferior (1);
881 registers_to_string (own_buf
);
884 set_desired_inferior (1);
885 registers_from_string (&own_buf
[1]);
889 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
890 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
891 convert_int_to_ascii (mem_buf
, own_buf
, len
);
896 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
897 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
903 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
904 &mem_addr
, &len
, mem_buf
) < 0
905 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
911 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
912 if (target_signal_to_host_p (sig
))
913 signal
= target_signal_to_host (sig
);
916 set_desired_inferior (0);
917 myresume (0, signal
);
918 signal
= mywait (&status
, 1);
919 prepare_resume_reply (own_buf
, status
, signal
);
922 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
923 if (target_signal_to_host_p (sig
))
924 signal
= target_signal_to_host (sig
);
927 set_desired_inferior (0);
928 myresume (1, signal
);
929 signal
= mywait (&status
, 1);
930 prepare_resume_reply (own_buf
, status
, signal
);
933 set_desired_inferior (0);
935 signal
= mywait (&status
, 1);
936 prepare_resume_reply (own_buf
, status
, signal
);
939 set_desired_inferior (0);
941 signal
= mywait (&status
, 1);
942 prepare_resume_reply (own_buf
, status
, signal
);
948 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
949 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
950 char type
= own_buf
[1];
952 if (the_target
->insert_watchpoint
== NULL
953 || (type
< '2' || type
> '4'))
955 /* No watchpoint support or not a watchpoint command;
956 unrecognized either way. */
963 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
978 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
979 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
980 char type
= own_buf
[1];
982 if (the_target
->remove_watchpoint
== NULL
983 || (type
< '2' || type
> '4'))
985 /* No watchpoint support or not a watchpoint command;
986 unrecognized either way. */
993 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1000 write_enn (own_buf
);
1005 fprintf (stderr
, "Killing inferior\n");
1007 /* When using the extended protocol, we start up a new
1008 debugging session. The traditional protocol will
1010 if (extended_protocol
)
1013 fprintf (stderr
, "GDBserver restarting\n");
1015 /* Wait till we are at 1st instruction in prog. */
1016 signal
= start_inferior (&argv
[2], &status
);
1027 unsigned long gdb_id
, thread_id
;
1029 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1030 thread_id
= gdb_id_to_thread_id (gdb_id
);
1033 write_enn (own_buf
);
1037 if (mythread_alive (thread_id
))
1040 write_enn (own_buf
);
1044 /* Restarting the inferior is only supported in the
1045 extended protocol. */
1046 if (extended_protocol
)
1050 fprintf (stderr
, "GDBserver restarting\n");
1052 /* Wait till we are at 1st instruction in prog. */
1053 signal
= start_inferior (&argv
[2], &status
);
1059 /* It is a request we don't understand. Respond with an
1060 empty packet so that gdb knows that we don't support this
1066 /* Extended (long) request. */
1067 handle_v_requests (own_buf
, &status
, &signal
);
1070 /* It is a request we don't understand. Respond with an
1071 empty packet so that gdb knows that we don't support this
1077 if (new_packet_len
!= -1)
1078 putpkt_binary (own_buf
, new_packet_len
);
1084 "\nChild exited with status %d\n", signal
);
1086 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1087 target_signal_to_host (signal
),
1088 target_signal_to_name (signal
));
1089 if (status
== 'W' || status
== 'X')
1091 if (extended_protocol
)
1093 fprintf (stderr
, "Killing inferior\n");
1096 fprintf (stderr
, "GDBserver restarting\n");
1098 /* Wait till we are at 1st instruction in prog. */
1099 signal
= start_inferior (&argv
[2], &status
);
1105 fprintf (stderr
, "GDBserver exiting\n");
1111 /* We come here when getpkt fails.
1113 For the extended remote protocol we exit (and this is the only
1114 way we gracefully exit!).
1116 For the traditional remote protocol close the connection,
1117 and re-open it at the top of the loop. */
1118 if (extended_protocol
)
1125 fprintf (stderr
, "Remote side has terminated connection. "
1126 "GDBserver will reopen the connection.\n");