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 /* Otherwise we didn't know what packet it was. Say we didn't
262 /* Parse vCont packets. */
264 handle_v_cont (char *own_buf
, char *status
, int *signal
)
268 struct thread_resume
*resume_info
, default_action
;
270 /* Count the number of semicolons in the packet. There should be one
279 /* Allocate room for one extra action, for the default remain-stopped
280 behavior; if no default action is in the list, we'll need the extra
282 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
284 default_action
.thread
= -1;
285 default_action
.leave_stopped
= 1;
286 default_action
.step
= 0;
287 default_action
.sig
= 0;
295 resume_info
[i
].leave_stopped
= 0;
297 if (p
[0] == 's' || p
[0] == 'S')
298 resume_info
[i
].step
= 1;
299 else if (p
[0] == 'c' || p
[0] == 'C')
300 resume_info
[i
].step
= 0;
304 if (p
[0] == 'S' || p
[0] == 'C')
307 sig
= strtol (p
+ 1, &q
, 16);
312 if (!target_signal_to_host_p (sig
))
314 resume_info
[i
].sig
= target_signal_to_host (sig
);
318 resume_info
[i
].sig
= 0;
324 resume_info
[i
].thread
= -1;
325 default_action
= resume_info
[i
];
327 /* Note: we don't increment i here, we'll overwrite this entry
328 the next time through. */
330 else if (p
[0] == ':')
332 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
333 unsigned long thread_id
;
338 if (p
[0] != ';' && p
[0] != 0)
341 thread_id
= gdb_id_to_thread_id (gdb_id
);
343 resume_info
[i
].thread
= thread_id
;
351 resume_info
[i
] = default_action
;
353 /* Still used in occasional places in the backend. */
354 if (n
== 1 && resume_info
[0].thread
!= -1)
355 cont_thread
= resume_info
[0].thread
;
358 set_desired_inferior (0);
360 (*the_target
->resume
) (resume_info
);
364 *signal
= mywait (status
, 1);
365 prepare_resume_reply (own_buf
, *status
, *signal
);
369 /* No other way to report an error... */
370 strcpy (own_buf
, "");
375 /* Handle all of the extended 'v' packets. */
377 handle_v_requests (char *own_buf
, char *status
, int *signal
)
379 if (strncmp (own_buf
, "vCont;", 6) == 0)
381 handle_v_cont (own_buf
, status
, signal
);
385 if (strncmp (own_buf
, "vCont?", 6) == 0)
387 strcpy (own_buf
, "vCont;c;C;s;S");
391 /* Otherwise we didn't know what packet it was. Say we didn't
398 myresume (int step
, int sig
)
400 struct thread_resume resume_info
[2];
403 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
405 resume_info
[0].thread
406 = ((struct inferior_list_entry
*) current_inferior
)->id
;
407 resume_info
[0].step
= step
;
408 resume_info
[0].sig
= sig
;
409 resume_info
[0].leave_stopped
= 0;
412 resume_info
[n
].thread
= -1;
413 resume_info
[n
].step
= 0;
414 resume_info
[n
].sig
= 0;
415 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
417 (*the_target
->resume
) (resume_info
);
423 gdbserver_version (void)
425 printf ("GNU gdbserver %s\n"
426 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
427 "gdbserver is free software, covered by the GNU General Public License.\n"
428 "This gdbserver was configured as \"%s\"\n",
433 gdbserver_usage (void)
435 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
436 "\tgdbserver COMM --attach PID\n"
438 "COMM may either be a tty device (for serial debugging), or \n"
439 "HOST:PORT to listen for a TCP connection.\n");
443 main (int argc
, char *argv
[])
445 char ch
, status
, *own_buf
;
446 unsigned char *mem_buf
;
455 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
457 gdbserver_version ();
461 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
467 if (setjmp (toplevel
))
469 fprintf (stderr
, "Exiting\n");
476 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
480 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
489 if (argc
< 3 || bad_attach
)
497 own_buf
= malloc (PBUFSIZ
);
498 mem_buf
= malloc (PBUFSIZ
);
502 /* Wait till we are at first instruction in program. */
503 signal
= start_inferior (&argv
[2], &status
);
505 /* We are now stopped at the first instruction of the target process */
509 switch (attach_inferior (pid
, &status
, &signal
))
512 error ("Attaching not supported on this target");
522 remote_open (argv
[1]);
530 int new_packet_len
= -1;
532 packet_len
= getpkt (own_buf
);
541 handle_query (own_buf
, &new_packet_len
);
544 remote_debug
= !remote_debug
;
547 /* Skip "detach" support on mingw32, since we don't have
550 fprintf (stderr
, "Detaching from inferior\n");
556 /* If we are attached, then we can exit. Otherwise, we need to
557 hang around doing nothing, until the child is gone. */
563 ret
= waitpid (signal_pid
, &status
, 0);
564 if (WIFEXITED (status
) || WIFSIGNALED (status
))
566 } while (ret
!= -1 || errno
!= ECHILD
);
575 extended_protocol
= 1;
576 prepare_resume_reply (own_buf
, status
, signal
);
580 /* We can not use the extended protocol if we are
581 attached, because we can not restart the running
582 program. So return unrecognized. */
587 prepare_resume_reply (own_buf
, status
, signal
);
590 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
592 unsigned long gdb_id
, thread_id
;
594 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
595 thread_id
= gdb_id_to_thread_id (gdb_id
);
602 if (own_buf
[1] == 'g')
604 general_thread
= thread_id
;
605 set_desired_inferior (1);
607 else if (own_buf
[1] == 'c')
608 cont_thread
= thread_id
;
609 else if (own_buf
[1] == 's')
610 step_thread
= thread_id
;
616 /* Silently ignore it so that gdb can extend the protocol
617 without compatibility headaches. */
622 set_desired_inferior (1);
623 registers_to_string (own_buf
);
626 set_desired_inferior (1);
627 registers_from_string (&own_buf
[1]);
631 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
632 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
633 convert_int_to_ascii (mem_buf
, own_buf
, len
);
638 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
639 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
645 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
646 &mem_addr
, &len
, mem_buf
) < 0
647 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
653 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
654 if (target_signal_to_host_p (sig
))
655 signal
= target_signal_to_host (sig
);
658 set_desired_inferior (0);
659 myresume (0, signal
);
660 signal
= mywait (&status
, 1);
661 prepare_resume_reply (own_buf
, status
, signal
);
664 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
665 if (target_signal_to_host_p (sig
))
666 signal
= target_signal_to_host (sig
);
669 set_desired_inferior (0);
670 myresume (1, signal
);
671 signal
= mywait (&status
, 1);
672 prepare_resume_reply (own_buf
, status
, signal
);
675 set_desired_inferior (0);
677 signal
= mywait (&status
, 1);
678 prepare_resume_reply (own_buf
, status
, signal
);
681 set_desired_inferior (0);
683 signal
= mywait (&status
, 1);
684 prepare_resume_reply (own_buf
, status
, signal
);
690 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
691 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
692 char type
= own_buf
[1];
694 if (the_target
->insert_watchpoint
== NULL
695 || (type
< '2' || type
> '4'))
697 /* No watchpoint support or not a watchpoint command;
698 unrecognized either way. */
705 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
720 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
721 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
722 char type
= own_buf
[1];
724 if (the_target
->remove_watchpoint
== NULL
725 || (type
< '2' || type
> '4'))
727 /* No watchpoint support or not a watchpoint command;
728 unrecognized either way. */
735 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
747 fprintf (stderr
, "Killing inferior\n");
749 /* When using the extended protocol, we start up a new
750 debugging session. The traditional protocol will
752 if (extended_protocol
)
755 fprintf (stderr
, "GDBserver restarting\n");
757 /* Wait till we are at 1st instruction in prog. */
758 signal
= start_inferior (&argv
[2], &status
);
769 unsigned long gdb_id
, thread_id
;
771 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
772 thread_id
= gdb_id_to_thread_id (gdb_id
);
779 if (mythread_alive (thread_id
))
786 /* Restarting the inferior is only supported in the
787 extended protocol. */
788 if (extended_protocol
)
792 fprintf (stderr
, "GDBserver restarting\n");
794 /* Wait till we are at 1st instruction in prog. */
795 signal
= start_inferior (&argv
[2], &status
);
801 /* It is a request we don't understand. Respond with an
802 empty packet so that gdb knows that we don't support this
808 /* Extended (long) request. */
809 handle_v_requests (own_buf
, &status
, &signal
);
812 /* It is a request we don't understand. Respond with an
813 empty packet so that gdb knows that we don't support this
819 if (new_packet_len
!= -1)
820 putpkt_binary (own_buf
, new_packet_len
);
826 "\nChild exited with status %d\n", signal
);
828 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
829 target_signal_to_host (signal
),
830 target_signal_to_name (signal
));
831 if (status
== 'W' || status
== 'X')
833 if (extended_protocol
)
835 fprintf (stderr
, "Killing inferior\n");
838 fprintf (stderr
, "GDBserver restarting\n");
840 /* Wait till we are at 1st instruction in prog. */
841 signal
= start_inferior (&argv
[2], &status
);
847 fprintf (stderr
, "GDBserver exiting\n");
853 /* We come here when getpkt fails.
855 For the extended remote protocol we exit (and this is the only
856 way we gracefully exit!).
858 For the traditional remote protocol close the connection,
859 and re-open it at the top of the loop. */
860 if (extended_protocol
)
867 fprintf (stderr
, "Remote side has terminated connection. "
868 "GDBserver will reopen the connection.\n");