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. */
29 unsigned long cont_thread
;
30 unsigned long general_thread
;
31 unsigned long step_thread
;
32 unsigned long thread_from_wait
;
33 unsigned long old_thread_from_wait
;
34 int extended_protocol
;
39 /* The PID of the originally created or attached inferior. Used to
40 send signals to the process when GDB sends us an asynchronous interrupt
41 (user hitting Control-C in the client), and to wait for the child to exit
42 when no longer debugging it. */
44 unsigned long signal_pid
;
47 start_inferior (char *argv
[], char *statusptr
)
49 signal (SIGTTOU
, SIG_DFL
);
50 signal (SIGTTIN
, SIG_DFL
);
52 signal_pid
= create_inferior (argv
[0], argv
);
54 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
57 signal (SIGTTOU
, SIG_IGN
);
58 signal (SIGTTIN
, SIG_IGN
);
59 tcsetpgrp (fileno (stderr
), signal_pid
);
61 /* Wait till we are at 1st instruction in program, return signal number. */
62 return mywait (statusptr
, 0);
66 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
68 /* myattach should return -1 if attaching is unsupported,
69 0 if it succeeded, and call error() otherwise. */
71 if (myattach (pid
) != 0)
74 fprintf (stderr
, "Attached; pid = %d\n", pid
);
76 /* FIXME - It may be that we should get the SIGNAL_PID from the
77 attach function, so that it can be the main thread instead of
78 whichever we were told to attach to. */
81 *sigptr
= mywait (statusptr
, 0);
83 /* GDB knows to ignore the first SIGSTOP after attaching to a running
84 process using the "attach" command, but this is different; it's
85 just using "target remote". Pretend it's just starting up. */
86 if (*statusptr
== 'T' && *sigptr
== SIGSTOP
)
92 extern int remote_debug
;
94 /* Decode a qXfer read request. Return 0 if everything looks OK,
98 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
100 /* Extract and NUL-terminate the annex. */
102 while (*buf
&& *buf
!= ':')
108 /* After the read/write marker and annex, qXfer looks like a
109 traditional 'm' packet. */
110 decode_m_packet (buf
, ofs
, len
);
115 /* Write the response to a successful qXfer read. Returns the
116 length of the (binary) data stored in BUF, corresponding
117 to as much of DATA/LEN as we could fit. IS_MORE controls
118 the first character of the response. */
120 write_qxfer_response (char *buf
, unsigned char *data
, int len
, int is_more
)
129 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
133 /* Handle all of the extended 'q' packets. */
135 handle_query (char *own_buf
, int *new_packet_len_p
)
137 static struct inferior_list_entry
*thread_ptr
;
139 if (strcmp ("qSymbol::", own_buf
) == 0)
141 if (the_target
->look_up_symbols
!= NULL
)
142 (*the_target
->look_up_symbols
) ();
144 strcpy (own_buf
, "OK");
148 if (strcmp ("qfThreadInfo", own_buf
) == 0)
150 thread_ptr
= all_threads
.head
;
151 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
152 thread_ptr
= thread_ptr
->next
;
156 if (strcmp ("qsThreadInfo", own_buf
) == 0)
158 if (thread_ptr
!= NULL
)
160 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
161 thread_ptr
= thread_ptr
->next
;
166 sprintf (own_buf
, "l");
171 if (the_target
->read_offsets
!= NULL
172 && strcmp ("qOffsets", own_buf
) == 0)
174 CORE_ADDR text
, data
;
176 if (the_target
->read_offsets (&text
, &data
))
177 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
178 (long)text
, (long)data
, (long)data
);
185 if (the_target
->read_auxv
!= NULL
186 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
194 /* Reject any annex; grab the offset and length. */
195 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
198 strcpy (own_buf
, "E00");
202 /* Read one extra byte, as an indicator of whether there is
204 if (len
> PBUFSIZ
- 2)
206 data
= malloc (len
+ 1);
207 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
209 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
211 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
218 /* Protocol features query. */
219 if (strncmp ("qSupported", own_buf
, 10) == 0
220 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
222 sprintf (own_buf
, "PacketSize=%x", PBUFSIZ
- 1);
224 if (the_target
->read_auxv
!= NULL
)
225 strcat (own_buf
, ";qPart:auxv:read+");
230 /* Otherwise we didn't know what packet it was. Say we didn't
235 /* Parse vCont packets. */
237 handle_v_cont (char *own_buf
, char *status
, int *signal
)
241 struct thread_resume
*resume_info
, default_action
;
243 /* Count the number of semicolons in the packet. There should be one
252 /* Allocate room for one extra action, for the default remain-stopped
253 behavior; if no default action is in the list, we'll need the extra
255 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
257 default_action
.thread
= -1;
258 default_action
.leave_stopped
= 1;
259 default_action
.step
= 0;
260 default_action
.sig
= 0;
268 resume_info
[i
].leave_stopped
= 0;
270 if (p
[0] == 's' || p
[0] == 'S')
271 resume_info
[i
].step
= 1;
272 else if (p
[0] == 'c' || p
[0] == 'C')
273 resume_info
[i
].step
= 0;
277 if (p
[0] == 'S' || p
[0] == 'C')
280 sig
= strtol (p
+ 1, &q
, 16);
285 if (!target_signal_to_host_p (sig
))
287 resume_info
[i
].sig
= target_signal_to_host (sig
);
291 resume_info
[i
].sig
= 0;
297 resume_info
[i
].thread
= -1;
298 default_action
= resume_info
[i
];
300 /* Note: we don't increment i here, we'll overwrite this entry
301 the next time through. */
303 else if (p
[0] == ':')
305 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
306 unsigned long thread_id
;
311 if (p
[0] != ';' && p
[0] != 0)
314 thread_id
= gdb_id_to_thread_id (gdb_id
);
316 resume_info
[i
].thread
= thread_id
;
324 resume_info
[i
] = default_action
;
326 /* Still used in occasional places in the backend. */
327 if (n
== 1 && resume_info
[0].thread
!= -1)
328 cont_thread
= resume_info
[0].thread
;
331 set_desired_inferior (0);
333 (*the_target
->resume
) (resume_info
);
337 *signal
= mywait (status
, 1);
338 prepare_resume_reply (own_buf
, *status
, *signal
);
342 /* No other way to report an error... */
343 strcpy (own_buf
, "");
348 /* Handle all of the extended 'v' packets. */
350 handle_v_requests (char *own_buf
, char *status
, int *signal
)
352 if (strncmp (own_buf
, "vCont;", 6) == 0)
354 handle_v_cont (own_buf
, status
, signal
);
358 if (strncmp (own_buf
, "vCont?", 6) == 0)
360 strcpy (own_buf
, "vCont;c;C;s;S");
364 /* Otherwise we didn't know what packet it was. Say we didn't
371 myresume (int step
, int sig
)
373 struct thread_resume resume_info
[2];
376 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
378 resume_info
[0].thread
379 = ((struct inferior_list_entry
*) current_inferior
)->id
;
380 resume_info
[0].step
= step
;
381 resume_info
[0].sig
= sig
;
382 resume_info
[0].leave_stopped
= 0;
385 resume_info
[n
].thread
= -1;
386 resume_info
[n
].step
= 0;
387 resume_info
[n
].sig
= 0;
388 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
390 (*the_target
->resume
) (resume_info
);
396 gdbserver_version (void)
398 printf ("GNU gdbserver %s\n"
399 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
400 "gdbserver is free software, covered by the GNU General Public License.\n"
401 "This gdbserver was configured as \"%s\"\n",
406 gdbserver_usage (void)
408 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
409 "\tgdbserver COMM --attach PID\n"
411 "COMM may either be a tty device (for serial debugging), or \n"
412 "HOST:PORT to listen for a TCP connection.\n");
416 main (int argc
, char *argv
[])
418 char ch
, status
, *own_buf
;
419 unsigned char *mem_buf
;
428 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
430 gdbserver_version ();
434 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
440 if (setjmp (toplevel
))
442 fprintf (stderr
, "Exiting\n");
449 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
453 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
462 if (argc
< 3 || bad_attach
)
470 own_buf
= malloc (PBUFSIZ
);
471 mem_buf
= malloc (PBUFSIZ
);
475 /* Wait till we are at first instruction in program. */
476 signal
= start_inferior (&argv
[2], &status
);
478 /* We are now stopped at the first instruction of the target process */
482 switch (attach_inferior (pid
, &status
, &signal
))
485 error ("Attaching not supported on this target");
495 remote_open (argv
[1]);
503 int new_packet_len
= -1;
505 packet_len
= getpkt (own_buf
);
514 handle_query (own_buf
, &new_packet_len
);
517 remote_debug
= !remote_debug
;
520 fprintf (stderr
, "Detaching from inferior\n");
526 /* If we are attached, then we can exit. Otherwise, we need to
527 hang around doing nothing, until the child is gone. */
533 ret
= waitpid (signal_pid
, &status
, 0);
534 if (WIFEXITED (status
) || WIFSIGNALED (status
))
536 } while (ret
!= -1 || errno
!= ECHILD
);
544 extended_protocol
= 1;
545 prepare_resume_reply (own_buf
, status
, signal
);
549 /* We can not use the extended protocol if we are
550 attached, because we can not restart the running
551 program. So return unrecognized. */
556 prepare_resume_reply (own_buf
, status
, signal
);
559 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
561 unsigned long gdb_id
, thread_id
;
563 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
564 thread_id
= gdb_id_to_thread_id (gdb_id
);
571 if (own_buf
[1] == 'g')
573 general_thread
= thread_id
;
574 set_desired_inferior (1);
576 else if (own_buf
[1] == 'c')
577 cont_thread
= thread_id
;
578 else if (own_buf
[1] == 's')
579 step_thread
= thread_id
;
585 /* Silently ignore it so that gdb can extend the protocol
586 without compatibility headaches. */
591 set_desired_inferior (1);
592 registers_to_string (own_buf
);
595 set_desired_inferior (1);
596 registers_from_string (&own_buf
[1]);
600 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
601 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
602 convert_int_to_ascii (mem_buf
, own_buf
, len
);
607 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
608 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
614 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
615 &mem_addr
, &len
, mem_buf
) < 0
616 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
622 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
623 if (target_signal_to_host_p (sig
))
624 signal
= target_signal_to_host (sig
);
627 set_desired_inferior (0);
628 myresume (0, signal
);
629 signal
= mywait (&status
, 1);
630 prepare_resume_reply (own_buf
, status
, signal
);
633 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
634 if (target_signal_to_host_p (sig
))
635 signal
= target_signal_to_host (sig
);
638 set_desired_inferior (0);
639 myresume (1, signal
);
640 signal
= mywait (&status
, 1);
641 prepare_resume_reply (own_buf
, status
, signal
);
644 set_desired_inferior (0);
646 signal
= mywait (&status
, 1);
647 prepare_resume_reply (own_buf
, status
, signal
);
650 set_desired_inferior (0);
652 signal
= mywait (&status
, 1);
653 prepare_resume_reply (own_buf
, status
, signal
);
659 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
660 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
661 char type
= own_buf
[1];
663 if (the_target
->insert_watchpoint
== NULL
664 || (type
< '2' || type
> '4'))
666 /* No watchpoint support or not a watchpoint command;
667 unrecognized either way. */
674 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
689 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
690 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
691 char type
= own_buf
[1];
693 if (the_target
->remove_watchpoint
== NULL
694 || (type
< '2' || type
> '4'))
696 /* No watchpoint support or not a watchpoint command;
697 unrecognized either way. */
704 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
716 fprintf (stderr
, "Killing inferior\n");
718 /* When using the extended protocol, we start up a new
719 debugging session. The traditional protocol will
721 if (extended_protocol
)
724 fprintf (stderr
, "GDBserver restarting\n");
726 /* Wait till we are at 1st instruction in prog. */
727 signal
= start_inferior (&argv
[2], &status
);
738 unsigned long gdb_id
, thread_id
;
740 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
741 thread_id
= gdb_id_to_thread_id (gdb_id
);
748 if (mythread_alive (thread_id
))
755 /* Restarting the inferior is only supported in the
756 extended protocol. */
757 if (extended_protocol
)
761 fprintf (stderr
, "GDBserver restarting\n");
763 /* Wait till we are at 1st instruction in prog. */
764 signal
= start_inferior (&argv
[2], &status
);
770 /* It is a request we don't understand. Respond with an
771 empty packet so that gdb knows that we don't support this
777 /* Extended (long) request. */
778 handle_v_requests (own_buf
, &status
, &signal
);
781 /* It is a request we don't understand. Respond with an
782 empty packet so that gdb knows that we don't support this
788 if (new_packet_len
!= -1)
789 putpkt_binary (own_buf
, new_packet_len
);
795 "\nChild exited with status %d\n", signal
);
797 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n",
799 if (status
== 'W' || status
== 'X')
801 if (extended_protocol
)
803 fprintf (stderr
, "Killing inferior\n");
806 fprintf (stderr
, "GDBserver restarting\n");
808 /* Wait till we are at 1st instruction in prog. */
809 signal
= start_inferior (&argv
[2], &status
);
815 fprintf (stderr
, "GDBserver exiting\n");
821 /* We come here when getpkt fails.
823 For the extended remote protocol we exit (and this is the only
824 way we gracefully exit!).
826 For the traditional remote protocol close the connection,
827 and re-open it at the top of the loop. */
828 if (extended_protocol
)
835 fprintf (stderr
, "Remote side has terminated connection. "
836 "GDBserver will reopen the connection.\n");