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, 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>. */
36 ptid_t general_thread
;
41 static int extended_protocol
;
42 static int response_needed
;
43 static int exit_requested
;
48 static char **program_argv
, **wrapper_argv
;
50 /* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
54 int pass_signals
[TARGET_SIGNAL_LAST
];
58 const char *gdbserver_xmltarget
;
60 /* The PID of the originally created or attached inferior. Used to
61 send signals to the process when GDB sends us an asynchronous interrupt
62 (user hitting Control-C in the client), and to wait for the child to exit
63 when no longer debugging it. */
65 unsigned long signal_pid
;
68 /* A file descriptor for the controlling terminal. */
71 /* TERMINAL_FD's original foreground group. */
72 pid_t old_foreground_pgrp
;
74 /* Hand back terminal ownership to the original foreground group. */
77 restore_old_foreground_pgrp (void)
79 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
83 /* Set if you want to disable optional thread related packets support
84 in gdbserver, for the sake of testing GDB against stubs that don't
86 int disable_packet_vCont
;
87 int disable_packet_Tthread
;
88 int disable_packet_qC
;
89 int disable_packet_qfThreadInfo
;
91 /* Last status reported to GDB. */
92 static struct target_waitstatus last_status
;
93 static ptid_t last_ptid
;
96 static unsigned char *mem_buf
;
98 /* Structure holding information relative to a single stop reply. We
99 keep a queue of these (really a singly-linked list) to push to GDB
103 /* Pointer to next in list. */
104 struct vstop_notif
*next
;
106 /* Thread or process that got the event. */
110 struct target_waitstatus status
;
113 /* The pending stop replies list head. */
114 static struct vstop_notif
*notif_queue
= NULL
;
116 /* Put a stop reply to the stop reply queue. */
119 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
121 struct vstop_notif
*new_notif
;
123 new_notif
= malloc (sizeof (*new_notif
));
124 new_notif
->next
= NULL
;
125 new_notif
->ptid
= ptid
;
126 new_notif
->status
= *status
;
130 struct vstop_notif
*tail
;
131 for (tail
= notif_queue
;
135 tail
->next
= new_notif
;
138 notif_queue
= new_notif
;
143 struct vstop_notif
*n
;
145 for (n
= notif_queue
; n
; n
= n
->next
)
148 fprintf (stderr
, "pending stop replies: %d\n", i
);
152 /* Place an event in the stop reply queue, and push a notification if
153 we aren't sending one yet. */
156 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
158 queue_stop_reply (ptid
, status
);
160 /* If this is the first stop reply in the queue, then inform GDB
161 about it, by sending a Stop notification. */
162 if (notif_queue
->next
== NULL
)
167 prepare_resume_reply (p
,
168 notif_queue
->ptid
, ¬if_queue
->status
);
169 putpkt_notif (own_buf
);
173 /* Get rid of the currently pending stop replies for PID. If PID is
174 -1, then apply to all processes. */
177 discard_queued_stop_replies (int pid
)
179 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
181 for (reply
= notif_queue
; reply
; reply
= next
)
186 || ptid_get_pid (reply
->ptid
) == pid
)
188 if (reply
== notif_queue
)
191 prev
->next
= reply
->next
;
200 /* If there are more stop replies to push, push one now. */
203 send_next_stop_reply (char *own_buf
)
206 prepare_resume_reply (own_buf
,
208 ¬if_queue
->status
);
214 target_running (void)
216 return all_threads
.head
!= NULL
;
220 start_inferior (char **argv
)
222 char **new_argv
= argv
;
224 if (wrapper_argv
!= NULL
)
228 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
230 for (i
= 0; argv
[i
] != NULL
; i
++)
232 new_argv
= alloca (sizeof (char *) * count
);
234 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
235 new_argv
[count
++] = wrapper_argv
[i
];
236 for (i
= 0; argv
[i
] != NULL
; i
++)
237 new_argv
[count
++] = argv
[i
];
238 new_argv
[count
] = NULL
;
242 signal (SIGTTOU
, SIG_DFL
);
243 signal (SIGTTIN
, SIG_DFL
);
246 signal_pid
= create_inferior (new_argv
[0], new_argv
);
248 /* FIXME: we don't actually know at this point that the create
249 actually succeeded. We won't know that until we wait. */
250 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
255 signal (SIGTTOU
, SIG_IGN
);
256 signal (SIGTTIN
, SIG_IGN
);
257 terminal_fd
= fileno (stderr
);
258 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
259 tcsetpgrp (terminal_fd
, signal_pid
);
260 atexit (restore_old_foreground_pgrp
);
263 if (wrapper_argv
!= NULL
)
265 struct thread_resume resume_info
;
268 resume_info
.thread
= pid_to_ptid (signal_pid
);
269 resume_info
.kind
= resume_continue
;
272 ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
274 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
279 (*the_target
->resume
) (&resume_info
, 1);
281 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
282 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
285 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
290 /* Wait till we are at 1st instruction in program, return new pid
291 (assuming success). */
292 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
298 attach_inferior (int pid
)
300 /* myattach should return -1 if attaching is unsupported,
301 0 if it succeeded, and call error() otherwise. */
303 if (myattach (pid
) != 0)
306 fprintf (stderr
, "Attached; pid = %d\n", pid
);
309 /* FIXME - It may be that we should get the SIGNAL_PID from the
310 attach function, so that it can be the main thread instead of
311 whichever we were told to attach to. */
316 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
318 /* GDB knows to ignore the first SIGSTOP after attaching to a running
319 process using the "attach" command, but this is different; it's
320 just using "target remote". Pretend it's just starting up. */
321 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
322 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
323 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
329 extern int remote_debug
;
331 /* Decode a qXfer read request. Return 0 if everything looks OK,
335 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
337 /* Extract and NUL-terminate the annex. */
339 while (*buf
&& *buf
!= ':')
345 /* After the read marker and annex, qXfer looks like a
346 traditional 'm' packet. */
347 decode_m_packet (buf
, ofs
, len
);
352 /* Write the response to a successful qXfer read. Returns the
353 length of the (binary) data stored in BUF, corresponding
354 to as much of DATA/LEN as we could fit. IS_MORE controls
355 the first character of the response. */
357 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
366 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
370 /* Handle all of the extended 'Q' packets. */
372 handle_general_set (char *own_buf
)
374 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
376 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
377 const char *p
= own_buf
+ strlen ("QPassSignals:");
380 p
= decode_address_to_semicolon (&cursig
, p
);
381 for (i
= 0; i
< numsigs
; i
++)
387 /* Keep looping, to clear the remaining signals. */
390 p
= decode_address_to_semicolon (&cursig
, p
);
395 strcpy (own_buf
, "OK");
399 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
403 fprintf (stderr
, "[noack mode enabled]\n");
412 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
414 char *mode
= own_buf
+ 9;
418 if (strcmp (mode
, "0") == 0)
420 else if (strcmp (mode
, "1") == 0)
424 /* We don't know what this mode is, so complain to
426 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
432 req_str
= req
? "non-stop" : "all-stop";
433 if (start_non_stop (req
) != 0)
435 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
443 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
449 /* Otherwise we didn't know what packet it was. Say we didn't
455 get_features_xml (const char *annex
)
457 /* gdbserver_xmltarget defines what to return when looking
458 for the "target.xml" file. Its contents can either be
459 verbatim XML code (prefixed with a '@') or else the name
460 of the actual XML file to be used in place of "target.xml".
462 This variable is set up from the auto-generated
463 init_registers_... routine for the current target. */
465 if (gdbserver_xmltarget
466 && strcmp (annex
, "target.xml") == 0)
468 if (*gdbserver_xmltarget
== '@')
469 return gdbserver_xmltarget
+ 1;
471 annex
= gdbserver_xmltarget
;
476 extern const char *const xml_builtin
[][2];
479 /* Look for the annex. */
480 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
481 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
484 if (xml_builtin
[i
][0] != NULL
)
485 return xml_builtin
[i
][1];
493 monitor_show_help (void)
495 monitor_output ("The following monitor commands are supported:\n");
496 monitor_output (" set debug <0|1>\n");
497 monitor_output (" Enable general debugging messages\n");
498 monitor_output (" set remote-debug <0|1>\n");
499 monitor_output (" Enable remote protocol debugging messages\n");
500 monitor_output (" exit\n");
501 monitor_output (" Quit GDBserver\n");
504 /* Subroutine of handle_search_memory to simplify it. */
507 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
508 gdb_byte
*pattern
, unsigned pattern_len
,
509 gdb_byte
*search_buf
,
510 unsigned chunk_size
, unsigned search_buf_size
,
511 CORE_ADDR
*found_addrp
)
513 /* Prime the search buffer. */
515 if (read_inferior_memory (start_addr
, search_buf
, search_buf_size
) != 0)
517 warning ("Unable to access target memory at 0x%lx, halting search.",
522 /* Perform the search.
524 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
525 When we've scanned N bytes we copy the trailing bytes to the start and
526 read in another N bytes. */
528 while (search_space_len
>= pattern_len
)
531 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
535 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
537 if (found_ptr
!= NULL
)
539 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
540 *found_addrp
= found_addr
;
544 /* Not found in this chunk, skip to next chunk. */
546 /* Don't let search_space_len wrap here, it's unsigned. */
547 if (search_space_len
>= chunk_size
)
548 search_space_len
-= chunk_size
;
550 search_space_len
= 0;
552 if (search_space_len
>= pattern_len
)
554 unsigned keep_len
= search_buf_size
- chunk_size
;
555 CORE_ADDR read_addr
= start_addr
+ keep_len
;
558 /* Copy the trailing part of the previous iteration to the front
559 of the buffer for the next iteration. */
560 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
562 nr_to_read
= (search_space_len
- keep_len
< chunk_size
563 ? search_space_len
- keep_len
566 if (read_inferior_memory (read_addr
, search_buf
+ keep_len
,
569 warning ("Unable to access target memory at 0x%lx, halting search.",
574 start_addr
+= chunk_size
;
583 /* Handle qSearch:memory packets. */
586 handle_search_memory (char *own_buf
, int packet_len
)
588 CORE_ADDR start_addr
;
589 CORE_ADDR search_space_len
;
591 unsigned int pattern_len
;
592 /* NOTE: also defined in find.c testcase. */
593 #define SEARCH_CHUNK_SIZE 16000
594 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
595 /* Buffer to hold memory contents for searching. */
596 gdb_byte
*search_buf
;
597 unsigned search_buf_size
;
599 CORE_ADDR found_addr
;
600 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
602 pattern
= malloc (packet_len
);
605 error ("Unable to allocate memory to perform the search");
606 strcpy (own_buf
, "E00");
609 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
610 packet_len
- cmd_name_len
,
611 &start_addr
, &search_space_len
,
612 pattern
, &pattern_len
) < 0)
615 error ("Error in parsing qSearch:memory packet");
616 strcpy (own_buf
, "E00");
620 search_buf_size
= chunk_size
+ pattern_len
- 1;
622 /* No point in trying to allocate a buffer larger than the search space. */
623 if (search_space_len
< search_buf_size
)
624 search_buf_size
= search_space_len
;
626 search_buf
= malloc (search_buf_size
);
627 if (search_buf
== NULL
)
630 error ("Unable to allocate memory to perform the search");
631 strcpy (own_buf
, "E00");
635 found
= handle_search_memory_1 (start_addr
, search_space_len
,
636 pattern
, pattern_len
,
637 search_buf
, chunk_size
, search_buf_size
,
641 sprintf (own_buf
, "1,%lx", (long) found_addr
);
643 strcpy (own_buf
, "0");
645 strcpy (own_buf
, "E00");
651 #define require_running(BUF) \
652 if (!target_running ()) \
658 /* Handle all of the extended 'q' packets. */
660 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
662 static struct inferior_list_entry
*thread_ptr
;
664 /* Reply the current thread id. */
665 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
668 require_running (own_buf
);
670 if (!ptid_equal (general_thread
, null_ptid
)
671 && !ptid_equal (general_thread
, minus_one_ptid
))
672 gdb_id
= general_thread
;
675 thread_ptr
= all_threads
.head
;
676 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
679 sprintf (own_buf
, "QC");
681 own_buf
= write_ptid (own_buf
, gdb_id
);
685 if (strcmp ("qSymbol::", own_buf
) == 0)
687 if (target_running () && the_target
->look_up_symbols
!= NULL
)
688 (*the_target
->look_up_symbols
) ();
690 strcpy (own_buf
, "OK");
694 if (!disable_packet_qfThreadInfo
)
696 if (strcmp ("qfThreadInfo", own_buf
) == 0)
700 require_running (own_buf
);
701 thread_ptr
= all_threads
.head
;
704 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
705 write_ptid (own_buf
, gdb_id
);
706 thread_ptr
= thread_ptr
->next
;
710 if (strcmp ("qsThreadInfo", own_buf
) == 0)
714 require_running (own_buf
);
715 if (thread_ptr
!= NULL
)
718 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
719 write_ptid (own_buf
, gdb_id
);
720 thread_ptr
= thread_ptr
->next
;
725 sprintf (own_buf
, "l");
731 if (the_target
->read_offsets
!= NULL
732 && strcmp ("qOffsets", own_buf
) == 0)
734 CORE_ADDR text
, data
;
736 require_running (own_buf
);
737 if (the_target
->read_offsets (&text
, &data
))
738 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
739 (long)text
, (long)data
, (long)data
);
746 if (the_target
->qxfer_spu
!= NULL
747 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
753 unsigned char *spu_buf
;
755 require_running (own_buf
);
756 strcpy (own_buf
, "E00");
757 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
759 if (len
> PBUFSIZ
- 2)
761 spu_buf
= malloc (len
+ 1);
765 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
769 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
771 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
777 if (the_target
->qxfer_spu
!= NULL
778 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
784 unsigned char *spu_buf
;
786 require_running (own_buf
);
787 strcpy (own_buf
, "E00");
788 spu_buf
= malloc (packet_len
- 15);
791 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
792 &ofs
, &len
, spu_buf
) < 0)
798 n
= (*the_target
->qxfer_spu
)
799 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
803 sprintf (own_buf
, "%x", n
);
809 if (the_target
->read_auxv
!= NULL
810 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
818 require_running (own_buf
);
820 /* Reject any annex; grab the offset and length. */
821 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
824 strcpy (own_buf
, "E00");
828 /* Read one extra byte, as an indicator of whether there is
830 if (len
> PBUFSIZ
- 2)
832 data
= malloc (len
+ 1);
838 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
842 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
844 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
851 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
854 unsigned int len
, total_len
;
855 const char *document
;
858 require_running (own_buf
);
860 /* Grab the annex, offset, and length. */
861 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
863 strcpy (own_buf
, "E00");
867 /* Now grab the correct annex. */
868 document
= get_features_xml (annex
);
869 if (document
== NULL
)
871 strcpy (own_buf
, "E00");
875 total_len
= strlen (document
);
876 if (len
> PBUFSIZ
- 2)
881 else if (len
< total_len
- ofs
)
882 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
885 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
891 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
894 unsigned int len
, total_len
;
896 struct inferior_list_entry
*dll_ptr
;
899 require_running (own_buf
);
901 /* Reject any annex; grab the offset and length. */
902 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
905 strcpy (own_buf
, "E00");
909 /* Over-estimate the necessary memory. Assume that every character
910 in the library name must be escaped. */
912 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
913 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
915 document
= malloc (total_len
);
916 if (document
== NULL
)
921 strcpy (document
, "<library-list>\n");
922 p
= document
+ strlen (document
);
924 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
926 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
929 strcpy (p
, " <library name=\"");
931 name
= xml_escape_text (dll
->name
);
935 strcpy (p
, "\"><segment address=\"");
937 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
939 strcpy (p
, "\"/></library>\n");
943 strcpy (p
, "</library-list>\n");
945 total_len
= strlen (document
);
946 if (len
> PBUFSIZ
- 2)
951 else if (len
< total_len
- ofs
)
952 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
955 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
962 if (the_target
->qxfer_osdata
!= NULL
963 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
969 unsigned char *workbuf
;
971 strcpy (own_buf
, "E00");
972 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
974 if (len
> PBUFSIZ
- 2)
976 workbuf
= malloc (len
+ 1);
980 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
984 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
986 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
992 if (the_target
->qxfer_siginfo
!= NULL
993 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
1001 require_running (own_buf
);
1003 /* Reject any annex; grab the offset and length. */
1004 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1005 || annex
[0] != '\0')
1007 strcpy (own_buf
, "E00");
1011 /* Read one extra byte, as an indicator of whether there is
1013 if (len
> PBUFSIZ
- 2)
1015 data
= malloc (len
+ 1);
1018 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
1020 write_enn (own_buf
);
1022 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1024 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1030 if (the_target
->qxfer_siginfo
!= NULL
1031 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
1037 unsigned char *data
;
1039 require_running (own_buf
);
1041 strcpy (own_buf
, "E00");
1042 data
= malloc (packet_len
- 19);
1045 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
1046 &ofs
, &len
, data
) < 0)
1052 n
= (*the_target
->qxfer_siginfo
)
1053 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
1055 write_enn (own_buf
);
1057 sprintf (own_buf
, "%x", n
);
1063 /* Protocol features query. */
1064 if (strncmp ("qSupported", own_buf
, 10) == 0
1065 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1067 char *p
= &own_buf
[10];
1069 /* Process each feature being provided by GDB. The first
1070 feature will follow a ':', and latter features will follow
1073 for (p
= strtok (p
+ 1, ";");
1075 p
= strtok (NULL
, ";"))
1077 if (strcmp (p
, "multiprocess+") == 0)
1079 /* GDB supports and wants multi-process support if
1081 if (target_supports_multi_process ())
1086 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1088 /* We do not have any hook to indicate whether the target backend
1089 supports qXfer:libraries:read, so always report it. */
1090 strcat (own_buf
, ";qXfer:libraries:read+");
1092 if (the_target
->read_auxv
!= NULL
)
1093 strcat (own_buf
, ";qXfer:auxv:read+");
1095 if (the_target
->qxfer_spu
!= NULL
)
1096 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1098 if (the_target
->qxfer_siginfo
!= NULL
)
1099 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1101 /* We always report qXfer:features:read, as targets may
1102 install XML files on a subsequent call to arch_setup.
1103 If we reported to GDB on startup that we don't support
1104 qXfer:feature:read at all, we will never be re-queried. */
1105 strcat (own_buf
, ";qXfer:features:read+");
1107 if (transport_is_reliable
)
1108 strcat (own_buf
, ";QStartNoAckMode+");
1110 if (the_target
->qxfer_osdata
!= NULL
)
1111 strcat (own_buf
, ";qXfer:osdata:read+");
1113 if (target_supports_multi_process ())
1114 strcat (own_buf
, ";multiprocess+");
1116 if (target_supports_non_stop ())
1117 strcat (own_buf
, ";QNonStop+");
1122 /* Thread-local storage support. */
1123 if (the_target
->get_tls_address
!= NULL
1124 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1126 char *p
= own_buf
+ 12;
1127 CORE_ADDR parts
[2], address
= 0;
1129 ptid_t ptid
= null_ptid
;
1131 require_running (own_buf
);
1133 for (i
= 0; i
< 3; i
++)
1141 p2
= strchr (p
, ',');
1154 ptid
= read_ptid (p
, NULL
);
1156 decode_address (&parts
[i
- 1], p
, len
);
1160 if (p
!= NULL
|| i
< 3)
1164 struct thread_info
*thread
= find_thread_ptid (ptid
);
1169 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1175 sprintf (own_buf
, "%llx", address
);
1180 write_enn (own_buf
);
1184 /* Otherwise, pretend we do not understand this packet. */
1187 /* Handle "monitor" commands. */
1188 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1190 char *mon
= malloc (PBUFSIZ
);
1191 int len
= strlen (own_buf
+ 6);
1195 write_enn (own_buf
);
1199 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1201 write_enn (own_buf
);
1205 mon
[len
/ 2] = '\0';
1209 if (strcmp (mon
, "set debug 1") == 0)
1212 monitor_output ("Debug output enabled.\n");
1214 else if (strcmp (mon
, "set debug 0") == 0)
1217 monitor_output ("Debug output disabled.\n");
1219 else if (strcmp (mon
, "set remote-debug 1") == 0)
1222 monitor_output ("Protocol debug output enabled.\n");
1224 else if (strcmp (mon
, "set remote-debug 0") == 0)
1227 monitor_output ("Protocol debug output disabled.\n");
1229 else if (strcmp (mon
, "help") == 0)
1230 monitor_show_help ();
1231 else if (strcmp (mon
, "exit") == 0)
1235 monitor_output ("Unknown monitor command.\n\n");
1236 monitor_show_help ();
1237 write_enn (own_buf
);
1244 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1246 require_running (own_buf
);
1247 handle_search_memory (own_buf
, packet_len
);
1251 if (strcmp (own_buf
, "qAttached") == 0
1252 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1254 struct process_info
*process
;
1256 if (own_buf
[sizeof ("qAttached") - 1])
1258 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1259 process
= (struct process_info
*)
1260 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1264 require_running (own_buf
);
1265 process
= current_process ();
1268 if (process
== NULL
)
1270 write_enn (own_buf
);
1274 strcpy (own_buf
, process
->attached
? "1" : "0");
1278 /* Otherwise we didn't know what packet it was. Say we didn't
1283 /* Parse vCont packets. */
1285 handle_v_cont (char *own_buf
)
1289 struct thread_resume
*resume_info
;
1290 struct thread_resume default_action
= {{0}};
1292 /* Count the number of semicolons in the packet. There should be one
1293 for every action. */
1299 p
= strchr (p
, ';');
1302 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1303 if (resume_info
== NULL
)
1311 if (p
[0] == 's' || p
[0] == 'S')
1312 resume_info
[i
].kind
= resume_step
;
1313 else if (p
[0] == 'c' || p
[0] == 'C')
1314 resume_info
[i
].kind
= resume_continue
;
1315 else if (p
[0] == 't')
1316 resume_info
[i
].kind
= resume_stop
;
1320 if (p
[0] == 'S' || p
[0] == 'C')
1323 sig
= strtol (p
+ 1, &q
, 16);
1328 if (!target_signal_to_host_p (sig
))
1330 resume_info
[i
].sig
= target_signal_to_host (sig
);
1334 resume_info
[i
].sig
= 0;
1340 resume_info
[i
].thread
= minus_one_ptid
;
1341 default_action
= resume_info
[i
];
1343 /* Note: we don't increment i here, we'll overwrite this entry
1344 the next time through. */
1346 else if (p
[0] == ':')
1348 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1353 if (p
[0] != ';' && p
[0] != 0)
1356 resume_info
[i
].thread
= ptid
;
1363 resume_info
[i
] = default_action
;
1365 /* Still used in occasional places in the backend. */
1367 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1368 && resume_info
[0].kind
!= resume_stop
)
1369 cont_thread
= resume_info
[0].thread
;
1371 cont_thread
= minus_one_ptid
;
1372 set_desired_inferior (0);
1377 (*the_target
->resume
) (resume_info
, n
);
1385 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1386 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1387 disable_async_io ();
1392 write_enn (own_buf
);
1397 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1399 handle_v_attach (char *own_buf
)
1403 pid
= strtol (own_buf
+ 8, NULL
, 16);
1404 if (pid
!= 0 && attach_inferior (pid
) == 0)
1406 /* Don't report shared library events after attaching, even if
1407 some libraries are preloaded. GDB will always poll the
1408 library list. Avoids the "stopped by shared library event"
1409 notice on the GDB side. */
1414 /* In non-stop, we don't send a resume reply. Stop events
1415 will follow up using the normal notification
1420 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1426 write_enn (own_buf
);
1431 /* Run a new program. Return 1 if successful, 0 if failure. */
1433 handle_v_run (char *own_buf
)
1435 char *p
, *next_p
, **new_argv
;
1439 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1445 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1446 if (new_argv
== NULL
)
1448 write_enn (own_buf
);
1453 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1455 next_p
= strchr (p
, ';');
1457 next_p
= p
+ strlen (p
);
1459 if (i
== 0 && p
== next_p
)
1463 /* FIXME: Fail request if out of memory instead of dying. */
1464 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1465 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1466 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1475 if (new_argv
[0] == NULL
)
1477 /* GDB didn't specify a program to run. Use the program from the
1478 last run with the new argument list. */
1480 if (program_argv
== NULL
)
1482 /* FIXME: new_argv memory leak */
1483 write_enn (own_buf
);
1487 new_argv
[0] = strdup (program_argv
[0]);
1488 if (new_argv
[0] == NULL
)
1490 /* FIXME: new_argv memory leak */
1491 write_enn (own_buf
);
1496 /* Free the old argv and install the new one. */
1497 freeargv (program_argv
);
1498 program_argv
= new_argv
;
1500 start_inferior (program_argv
);
1501 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1503 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1505 /* In non-stop, sending a resume reply doesn't set the general
1506 thread, but GDB assumes a vRun sets it (this is so GDB can
1507 query which is the main thread of the new inferior. */
1509 general_thread
= last_ptid
;
1515 write_enn (own_buf
);
1520 /* Kill process. Return 1 if successful, 0 if failure. */
1522 handle_v_kill (char *own_buf
)
1525 char *p
= &own_buf
[6];
1527 pid
= strtol (p
, NULL
, 16);
1528 if (pid
!= 0 && kill_inferior (pid
) == 0)
1530 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1531 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1532 last_ptid
= pid_to_ptid (pid
);
1533 discard_queued_stop_replies (pid
);
1539 write_enn (own_buf
);
1544 /* Handle a 'vStopped' packet. */
1546 handle_v_stopped (char *own_buf
)
1548 /* If we're waiting for GDB to acknowledge a pending stop reply,
1549 consider that done. */
1552 struct vstop_notif
*head
;
1555 fprintf (stderr
, "vStopped: acking %s\n",
1556 target_pid_to_str (notif_queue
->ptid
));
1559 notif_queue
= notif_queue
->next
;
1563 /* Push another stop reply, or if there are no more left, an OK. */
1564 send_next_stop_reply (own_buf
);
1567 /* Handle all of the extended 'v' packets. */
1569 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
1571 if (!disable_packet_vCont
)
1573 if (strncmp (own_buf
, "vCont;", 6) == 0)
1575 require_running (own_buf
);
1576 handle_v_cont (own_buf
);
1580 if (strncmp (own_buf
, "vCont?", 6) == 0)
1582 strcpy (own_buf
, "vCont;c;C;s;S;t");
1587 if (strncmp (own_buf
, "vFile:", 6) == 0
1588 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
1591 if (strncmp (own_buf
, "vAttach;", 8) == 0)
1593 if (!multi_process
&& target_running ())
1595 fprintf (stderr
, "Already debugging a process\n");
1596 write_enn (own_buf
);
1599 handle_v_attach (own_buf
);
1603 if (strncmp (own_buf
, "vRun;", 5) == 0)
1605 if (!multi_process
&& target_running ())
1607 fprintf (stderr
, "Already debugging a process\n");
1608 write_enn (own_buf
);
1611 handle_v_run (own_buf
);
1615 if (strncmp (own_buf
, "vKill;", 6) == 0)
1617 if (!target_running ())
1619 fprintf (stderr
, "No process to kill\n");
1620 write_enn (own_buf
);
1623 handle_v_kill (own_buf
);
1627 if (strncmp (own_buf
, "vStopped", 8) == 0)
1629 handle_v_stopped (own_buf
);
1633 /* Otherwise we didn't know what packet it was. Say we didn't
1639 /* Resume inferior and wait for another event. In non-stop mode,
1640 don't really wait here, but return immediatelly to the event
1643 myresume (char *own_buf
, int step
, int sig
)
1645 struct thread_resume resume_info
[2];
1647 int valid_cont_thread
;
1649 set_desired_inferior (0);
1651 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
1652 && !ptid_equal (cont_thread
, minus_one_ptid
));
1654 if (step
|| sig
|| valid_cont_thread
)
1656 resume_info
[0].thread
1657 = ((struct inferior_list_entry
*) current_inferior
)->id
;
1659 resume_info
[0].kind
= resume_step
;
1661 resume_info
[0].kind
= resume_continue
;
1662 resume_info
[0].sig
= sig
;
1666 if (!valid_cont_thread
)
1668 resume_info
[n
].thread
= minus_one_ptid
;
1669 resume_info
[n
].kind
= resume_continue
;
1670 resume_info
[n
].sig
= 0;
1677 (*the_target
->resume
) (resume_info
, n
);
1683 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1684 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1685 disable_async_io ();
1689 /* Callback for for_each_inferior. Make a new stop reply for each
1693 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
1695 int pid
= * (int *) arg
;
1698 || ptid_get_pid (entry
->id
) == pid
)
1700 struct target_waitstatus status
;
1702 status
.kind
= TARGET_WAITKIND_STOPPED
;
1703 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
1705 /* Pass the last stop reply back to GDB, but don't notify. */
1706 queue_stop_reply (entry
->id
, &status
);
1712 /* Status handler for the '?' packet. */
1715 handle_status (char *own_buf
)
1717 struct target_waitstatus status
;
1718 status
.kind
= TARGET_WAITKIND_STOPPED
;
1719 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
1721 /* In non-stop mode, we must send a stop reply for each stopped
1722 thread. In all-stop mode, just send one for the first stopped
1728 discard_queued_stop_replies (pid
);
1729 find_inferior (&all_threads
, queue_stop_reply_callback
, &pid
);
1731 /* The first is sent immediatly. OK is sent if there is no
1732 stopped thread, which is the same handling of the vStopped
1733 packet (by design). */
1734 send_next_stop_reply (own_buf
);
1738 if (all_threads
.head
)
1739 prepare_resume_reply (own_buf
,
1740 all_threads
.head
->id
, &status
);
1742 strcpy (own_buf
, "W00");
1747 gdbserver_version (void)
1749 printf ("GNU gdbserver %s%s\n"
1750 "Copyright (C) 2009 Free Software Foundation, Inc.\n"
1751 "gdbserver is free software, covered by the GNU General Public License.\n"
1752 "This gdbserver was configured as \"%s\"\n",
1753 PKGVERSION
, version
, host_name
);
1757 gdbserver_usage (FILE *stream
)
1759 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1760 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1761 "\tgdbserver [OPTIONS] --multi COMM\n"
1763 "COMM may either be a tty device (for serial debugging), or \n"
1764 "HOST:PORT to listen for a TCP connection.\n"
1767 " --debug Enable general debugging output.\n"
1768 " --remote-debug Enable remote protocol debugging output.\n"
1769 " --version Display version information and exit.\n"
1770 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
1771 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
1772 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
1776 gdbserver_show_disableable (FILE *stream
)
1778 fprintf (stream
, "Disableable packets:\n"
1779 " vCont \tAll vCont packets\n"
1780 " qC \tQuerying the current thread\n"
1781 " qfThreadInfo\tThread listing\n"
1782 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1783 " threads \tAll of the above\n");
1787 #undef require_running
1788 #define require_running(BUF) \
1789 if (!target_running ()) \
1796 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
1798 int pid
= * (int *) args
;
1800 if (ptid_get_pid (entry
->id
) == pid
)
1807 kill_inferior_callback (struct inferior_list_entry
*entry
)
1809 struct process_info
*process
= (struct process_info
*) entry
;
1810 int pid
= ptid_get_pid (process
->head
.id
);
1812 kill_inferior (pid
);
1813 discard_queued_stop_replies (pid
);
1816 /* Callback for for_each_inferior to detach or kill the inferior,
1817 depending on whether we attached to it or not.
1818 We inform the user whether we're detaching or killing the process
1819 as this is only called when gdbserver is about to exit. */
1822 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
1824 struct process_info
*process
= (struct process_info
*) entry
;
1825 int pid
= ptid_get_pid (process
->head
.id
);
1827 if (process
->attached
)
1828 detach_inferior (pid
);
1830 kill_inferior (pid
);
1832 discard_queued_stop_replies (pid
);
1835 /* for_each_inferior callback for detach_or_kill_for_exit to print
1836 the pids of started inferiors. */
1839 print_started_pid (struct inferior_list_entry
*entry
)
1841 struct process_info
*process
= (struct process_info
*) entry
;
1843 if (! process
->attached
)
1845 int pid
= ptid_get_pid (process
->head
.id
);
1846 fprintf (stderr
, " %d", pid
);
1850 /* for_each_inferior callback for detach_or_kill_for_exit to print
1851 the pids of attached inferiors. */
1854 print_attached_pid (struct inferior_list_entry
*entry
)
1856 struct process_info
*process
= (struct process_info
*) entry
;
1858 if (process
->attached
)
1860 int pid
= ptid_get_pid (process
->head
.id
);
1861 fprintf (stderr
, " %d", pid
);
1865 /* Call this when exiting gdbserver with possible inferiors that need
1866 to be killed or detached from. */
1869 detach_or_kill_for_exit (void)
1871 /* First print a list of the inferiors we will be killing/detaching.
1872 This is to assist the user, for example, in case the inferior unexpectedly
1873 dies after we exit: did we screw up or did the inferior exit on its own?
1874 Having this info will save some head-scratching. */
1876 if (have_started_inferiors_p ())
1878 fprintf (stderr
, "Killing process(es):");
1879 for_each_inferior (&all_processes
, print_started_pid
);
1880 fprintf (stderr
, "\n");
1882 if (have_attached_inferiors_p ())
1884 fprintf (stderr
, "Detaching process(es):");
1885 for_each_inferior (&all_processes
, print_attached_pid
);
1886 fprintf (stderr
, "\n");
1889 /* Now we can kill or detach the inferiors. */
1891 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
1895 join_inferiors_callback (struct inferior_list_entry
*entry
)
1897 struct process_info
*process
= (struct process_info
*) entry
;
1899 /* If we are attached, then we can exit. Otherwise, we need to hang
1900 around doing nothing, until the child is gone. */
1901 if (!process
->attached
)
1902 join_inferior (ptid_get_pid (process
->head
.id
));
1906 main (int argc
, char *argv
[])
1910 char *arg_end
, *port
;
1911 char **next_arg
= &argv
[1];
1916 while (*next_arg
!= NULL
&& **next_arg
== '-')
1918 if (strcmp (*next_arg
, "--version") == 0)
1920 gdbserver_version ();
1923 else if (strcmp (*next_arg
, "--help") == 0)
1925 gdbserver_usage (stdout
);
1928 else if (strcmp (*next_arg
, "--attach") == 0)
1930 else if (strcmp (*next_arg
, "--multi") == 0)
1932 else if (strcmp (*next_arg
, "--wrapper") == 0)
1936 wrapper_argv
= next_arg
;
1937 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
1940 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
1942 gdbserver_usage (stderr
);
1946 /* Consume the "--". */
1949 else if (strcmp (*next_arg
, "--debug") == 0)
1951 else if (strcmp (*next_arg
, "--remote-debug") == 0)
1953 else if (strcmp (*next_arg
, "--disable-packet") == 0)
1955 gdbserver_show_disableable (stdout
);
1958 else if (strncmp (*next_arg
,
1959 "--disable-packet=",
1960 sizeof ("--disable-packet=") - 1) == 0)
1962 char *packets
, *tok
;
1964 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
1965 for (tok
= strtok (packets
, ",");
1967 tok
= strtok (NULL
, ","))
1969 if (strcmp ("vCont", tok
) == 0)
1970 disable_packet_vCont
= 1;
1971 else if (strcmp ("Tthread", tok
) == 0)
1972 disable_packet_Tthread
= 1;
1973 else if (strcmp ("qC", tok
) == 0)
1974 disable_packet_qC
= 1;
1975 else if (strcmp ("qfThreadInfo", tok
) == 0)
1976 disable_packet_qfThreadInfo
= 1;
1977 else if (strcmp ("threads", tok
) == 0)
1979 disable_packet_vCont
= 1;
1980 disable_packet_Tthread
= 1;
1981 disable_packet_qC
= 1;
1982 disable_packet_qfThreadInfo
= 1;
1986 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
1988 gdbserver_show_disableable (stderr
);
1995 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2003 if (setjmp (toplevel
))
2005 fprintf (stderr
, "Exiting\n");
2011 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2013 gdbserver_usage (stderr
);
2020 /* --attach used to come after PORT, so allow it there for
2022 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2029 && (*next_arg
== NULL
2030 || (*next_arg
)[0] == '\0'
2031 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2033 || next_arg
[1] != NULL
))
2038 gdbserver_usage (stderr
);
2042 initialize_inferiors ();
2043 initialize_async_io ();
2046 own_buf
= xmalloc (PBUFSIZ
+ 1);
2047 mem_buf
= xmalloc (PBUFSIZ
);
2049 if (pid
== 0 && *next_arg
!= NULL
)
2053 n
= argc
- (next_arg
- argv
);
2054 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2055 for (i
= 0; i
< n
; i
++)
2056 program_argv
[i
] = xstrdup (next_arg
[i
]);
2057 program_argv
[i
] = NULL
;
2059 /* Wait till we are at first instruction in program. */
2060 start_inferior (program_argv
);
2062 /* We are now (hopefully) stopped at the first instruction of
2063 the target process. This assumes that the target process was
2064 successfully created. */
2068 if (attach_inferior (pid
) == -1)
2069 error ("Attaching not supported on this target");
2071 /* Otherwise succeeded. */
2075 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2076 last_status
.value
.integer
= 0;
2077 last_ptid
= minus_one_ptid
;
2080 /* Don't report shared library events on the initial connection,
2081 even if some libraries are preloaded. Avoids the "stopped by
2082 shared library event" notice on gdb side. */
2085 if (setjmp (toplevel
))
2087 detach_or_kill_for_exit ();
2091 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2092 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2097 if (!was_running
&& !multi_mode
)
2099 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2111 if (setjmp (toplevel
) != 0)
2113 /* An error occurred. */
2114 if (response_needed
)
2116 write_enn (own_buf
);
2121 /* Wait for events. This will return when all event sources are
2122 removed from the event loop. */
2123 start_event_loop ();
2125 /* If an exit was requested (using the "monitor exit" command),
2126 terminate now. The only other way to get here is for
2127 getpkt to fail; close the connection and reopen it at the
2132 detach_or_kill_for_exit ();
2136 fprintf (stderr
, "Remote side has terminated connection. "
2137 "GDBserver will reopen the connection.\n");
2141 /* Event loop callback that handles a serial event. The first byte in
2142 the serial buffer gets us here. We expect characters to arrive at
2143 a brisk pace, so we read the rest of the packet with a blocking
2147 process_serial_event (void)
2157 int new_packet_len
= -1;
2159 /* Used to decide when gdbserver should exit in
2160 multi-mode/remote. */
2161 static int have_ran
= 0;
2164 have_ran
= target_running ();
2166 disable_async_io ();
2168 response_needed
= 0;
2169 packet_len
= getpkt (own_buf
);
2170 if (packet_len
<= 0)
2176 response_needed
= 1;
2183 handle_query (own_buf
, packet_len
, &new_packet_len
);
2186 handle_general_set (own_buf
);
2189 require_running (own_buf
);
2194 pid
= strtol (&own_buf
[i
], NULL
, 16);
2198 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2200 fprintf (stderr
, "Detaching from process %d\n", pid
);
2201 if (detach_inferior (pid
) != 0)
2202 write_enn (own_buf
);
2205 discard_queued_stop_replies (pid
);
2208 if (extended_protocol
)
2210 /* Treat this like a normal program exit. */
2211 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2212 last_status
.value
.integer
= 0;
2213 last_ptid
= pid_to_ptid (pid
);
2215 current_inferior
= NULL
;
2222 /* If we are attached, then we can exit. Otherwise, we
2223 need to hang around doing nothing, until the child is
2225 for_each_inferior (&all_processes
,
2226 join_inferiors_callback
);
2232 extended_protocol
= 1;
2236 handle_status (own_buf
);
2239 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2241 ptid_t gdb_id
, thread_id
;
2244 require_running (own_buf
);
2246 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2248 pid
= ptid_get_pid (gdb_id
);
2250 if (ptid_equal (gdb_id
, null_ptid
)
2251 || ptid_equal (gdb_id
, minus_one_ptid
))
2252 thread_id
= null_ptid
;
2254 && ptid_equal (pid_to_ptid (pid
),
2257 struct thread_info
*thread
=
2258 (struct thread_info
*) find_inferior (&all_threads
,
2263 write_enn (own_buf
);
2267 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2271 thread_id
= gdb_id_to_thread_id (gdb_id
);
2272 if (ptid_equal (thread_id
, null_ptid
))
2274 write_enn (own_buf
);
2279 if (own_buf
[1] == 'g')
2281 if (ptid_equal (thread_id
, null_ptid
))
2283 /* GDB is telling us to choose any thread. Check if
2284 the currently selected thread is still valid. If
2285 it is not, select the first available. */
2286 struct thread_info
*thread
=
2287 (struct thread_info
*) find_inferior_id (&all_threads
,
2290 thread_id
= all_threads
.head
->id
;
2293 general_thread
= thread_id
;
2294 set_desired_inferior (1);
2296 else if (own_buf
[1] == 'c')
2297 cont_thread
= thread_id
;
2298 else if (own_buf
[1] == 's')
2299 step_thread
= thread_id
;
2305 /* Silently ignore it so that gdb can extend the protocol
2306 without compatibility headaches. */
2311 require_running (own_buf
);
2312 set_desired_inferior (1);
2313 registers_to_string (own_buf
);
2316 require_running (own_buf
);
2317 set_desired_inferior (1);
2318 registers_from_string (&own_buf
[1]);
2322 require_running (own_buf
);
2323 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2324 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
2325 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2327 write_enn (own_buf
);
2330 require_running (own_buf
);
2331 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
2332 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
2335 write_enn (own_buf
);
2338 require_running (own_buf
);
2339 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2340 &mem_addr
, &len
, mem_buf
) < 0
2341 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
2342 write_enn (own_buf
);
2347 require_running (own_buf
);
2348 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2349 if (target_signal_to_host_p (sig
))
2350 signal
= target_signal_to_host (sig
);
2353 myresume (own_buf
, 0, signal
);
2356 require_running (own_buf
);
2357 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2358 if (target_signal_to_host_p (sig
))
2359 signal
= target_signal_to_host (sig
);
2362 myresume (own_buf
, 1, signal
);
2365 require_running (own_buf
);
2367 myresume (own_buf
, 0, signal
);
2370 require_running (own_buf
);
2372 myresume (own_buf
, 1, signal
);
2378 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
2379 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
2380 char type
= own_buf
[1];
2382 if (the_target
->insert_watchpoint
== NULL
2383 || (type
< '2' || type
> '4'))
2385 /* No watchpoint support or not a watchpoint command;
2386 unrecognized either way. */
2393 require_running (own_buf
);
2394 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
2401 write_enn (own_buf
);
2409 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
2410 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
2411 char type
= own_buf
[1];
2413 if (the_target
->remove_watchpoint
== NULL
2414 || (type
< '2' || type
> '4'))
2416 /* No watchpoint support or not a watchpoint command;
2417 unrecognized either way. */
2424 require_running (own_buf
);
2425 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
2432 write_enn (own_buf
);
2437 response_needed
= 0;
2438 if (!target_running ())
2439 /* The packet we received doesn't make sense - but we can't
2440 reply to it, either. */
2443 fprintf (stderr
, "Killing all inferiors\n");
2444 for_each_inferior (&all_processes
, kill_inferior_callback
);
2446 /* When using the extended protocol, we wait with no program
2447 running. The traditional protocol will exit instead. */
2448 if (extended_protocol
)
2450 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2451 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2461 ptid_t gdb_id
, thread_id
;
2463 require_running (own_buf
);
2465 gdb_id
= read_ptid (&own_buf
[1], NULL
);
2466 thread_id
= gdb_id_to_thread_id (gdb_id
);
2467 if (ptid_equal (thread_id
, null_ptid
))
2469 write_enn (own_buf
);
2473 if (mythread_alive (thread_id
))
2476 write_enn (own_buf
);
2480 response_needed
= 0;
2482 /* Restarting the inferior is only supported in the extended
2484 if (extended_protocol
)
2486 if (target_running ())
2487 for_each_inferior (&all_processes
,
2488 kill_inferior_callback
);
2489 fprintf (stderr
, "GDBserver restarting\n");
2491 /* Wait till we are at 1st instruction in prog. */
2492 if (program_argv
!= NULL
)
2493 start_inferior (program_argv
);
2496 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2497 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2503 /* It is a request we don't understand. Respond with an
2504 empty packet so that gdb knows that we don't support this
2510 /* Extended (long) request. */
2511 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
2515 /* It is a request we don't understand. Respond with an empty
2516 packet so that gdb knows that we don't support this
2522 if (new_packet_len
!= -1)
2523 putpkt_binary (own_buf
, new_packet_len
);
2527 response_needed
= 0;
2529 if (!extended_protocol
&& have_ran
&& !target_running ())
2531 /* In non-stop, defer exiting until GDB had a chance to query
2532 the whole vStopped list (until it gets an OK). */
2535 fprintf (stderr
, "GDBserver exiting\n");
2542 /* Event-loop callback for serial events. */
2545 handle_serial_event (int err
, gdb_client_data client_data
)
2548 fprintf (stderr
, "handling possible serial event\n");
2550 /* Really handle it. */
2551 process_serial_event ();
2553 /* Be sure to not change the selected inferior behind GDB's back.
2554 Important in the non-stop mode asynchronous protocol. */
2555 set_desired_inferior (1);
2558 /* Event-loop callback for target events. */
2561 handle_target_event (int err
, gdb_client_data client_data
)
2564 fprintf (stderr
, "handling possible target event\n");
2566 last_ptid
= mywait (minus_one_ptid
, &last_status
,
2569 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
2571 /* Something interesting. Tell GDB about it. */
2572 push_event (last_ptid
, &last_status
);
2575 /* Be sure to not change the selected inferior behind GDB's back.
2576 Important in the non-stop mode asynchronous protocol. */
2577 set_desired_inferior (1);