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, 2010, 2011
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 3 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, see <http://www.gnu.org/licenses/>. */
34 ptid_t general_thread
;
38 static int extended_protocol
;
39 static int response_needed
;
40 static int exit_requested
;
42 /* --once: Exit after the first connection has closed. */
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 /* Enable debugging of h/w breakpoint/watchpoint support. */
57 int pass_signals
[TARGET_SIGNAL_LAST
];
61 const char *gdbserver_xmltarget
;
63 /* The PID of the originally created or attached inferior. Used to
64 send signals to the process when GDB sends us an asynchronous interrupt
65 (user hitting Control-C in the client), and to wait for the child to exit
66 when no longer debugging it. */
68 unsigned long signal_pid
;
71 /* A file descriptor for the controlling terminal. */
74 /* TERMINAL_FD's original foreground group. */
75 pid_t old_foreground_pgrp
;
77 /* Hand back terminal ownership to the original foreground group. */
80 restore_old_foreground_pgrp (void)
82 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
86 /* Set if you want to disable optional thread related packets support
87 in gdbserver, for the sake of testing GDB against stubs that don't
89 int disable_packet_vCont
;
90 int disable_packet_Tthread
;
91 int disable_packet_qC
;
92 int disable_packet_qfThreadInfo
;
94 /* Last status reported to GDB. */
95 static struct target_waitstatus last_status
;
96 static ptid_t last_ptid
;
99 static unsigned char *mem_buf
;
101 /* Structure holding information relative to a single stop reply. We
102 keep a queue of these (really a singly-linked list) to push to GDB
106 /* Pointer to next in list. */
107 struct vstop_notif
*next
;
109 /* Thread or process that got the event. */
113 struct target_waitstatus status
;
116 /* The pending stop replies list head. */
117 static struct vstop_notif
*notif_queue
= NULL
;
119 /* Put a stop reply to the stop reply queue. */
122 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
124 struct vstop_notif
*new_notif
;
126 new_notif
= xmalloc (sizeof (*new_notif
));
127 new_notif
->next
= NULL
;
128 new_notif
->ptid
= ptid
;
129 new_notif
->status
= *status
;
133 struct vstop_notif
*tail
;
134 for (tail
= notif_queue
;
138 tail
->next
= new_notif
;
141 notif_queue
= new_notif
;
146 struct vstop_notif
*n
;
148 for (n
= notif_queue
; n
; n
= n
->next
)
151 fprintf (stderr
, "pending stop replies: %d\n", i
);
155 /* Place an event in the stop reply queue, and push a notification if
156 we aren't sending one yet. */
159 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
161 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
163 queue_stop_reply (ptid
, status
);
165 /* If this is the first stop reply in the queue, then inform GDB
166 about it, by sending a Stop notification. */
167 if (notif_queue
->next
== NULL
)
172 prepare_resume_reply (p
,
173 notif_queue
->ptid
, ¬if_queue
->status
);
174 putpkt_notif (own_buf
);
178 /* Get rid of the currently pending stop replies for PID. If PID is
179 -1, then apply to all processes. */
182 discard_queued_stop_replies (int pid
)
184 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
186 for (reply
= notif_queue
; reply
; reply
= next
)
191 || ptid_get_pid (reply
->ptid
) == pid
)
193 if (reply
== notif_queue
)
196 prev
->next
= reply
->next
;
205 /* If there are more stop replies to push, push one now. */
208 send_next_stop_reply (char *own_buf
)
211 prepare_resume_reply (own_buf
,
213 ¬if_queue
->status
);
219 target_running (void)
221 return all_threads
.head
!= NULL
;
225 start_inferior (char **argv
)
227 char **new_argv
= argv
;
229 if (wrapper_argv
!= NULL
)
233 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
235 for (i
= 0; argv
[i
] != NULL
; i
++)
237 new_argv
= alloca (sizeof (char *) * count
);
239 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
240 new_argv
[count
++] = wrapper_argv
[i
];
241 for (i
= 0; argv
[i
] != NULL
; i
++)
242 new_argv
[count
++] = argv
[i
];
243 new_argv
[count
] = NULL
;
249 for (i
= 0; new_argv
[i
]; ++i
)
250 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
255 signal (SIGTTOU
, SIG_DFL
);
256 signal (SIGTTIN
, SIG_DFL
);
259 signal_pid
= create_inferior (new_argv
[0], new_argv
);
261 /* FIXME: we don't actually know at this point that the create
262 actually succeeded. We won't know that until we wait. */
263 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
268 signal (SIGTTOU
, SIG_IGN
);
269 signal (SIGTTIN
, SIG_IGN
);
270 terminal_fd
= fileno (stderr
);
271 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
272 tcsetpgrp (terminal_fd
, signal_pid
);
273 atexit (restore_old_foreground_pgrp
);
276 if (wrapper_argv
!= NULL
)
278 struct thread_resume resume_info
;
280 resume_info
.thread
= pid_to_ptid (signal_pid
);
281 resume_info
.kind
= resume_continue
;
284 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
286 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
291 (*the_target
->resume
) (&resume_info
, 1);
293 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
294 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
297 current_inferior
->last_resume_kind
= resume_stop
;
298 current_inferior
->last_status
= last_status
;
300 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
302 current_inferior
->last_resume_kind
= resume_stop
;
303 current_inferior
->last_status
= last_status
;
307 /* Wait till we are at 1st instruction in program, return new pid
308 (assuming success). */
309 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
311 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
312 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
314 current_inferior
->last_resume_kind
= resume_stop
;
315 current_inferior
->last_status
= last_status
;
322 attach_inferior (int pid
)
324 /* myattach should return -1 if attaching is unsupported,
325 0 if it succeeded, and call error() otherwise. */
327 if (myattach (pid
) != 0)
330 fprintf (stderr
, "Attached; pid = %d\n", pid
);
333 /* FIXME - It may be that we should get the SIGNAL_PID from the
334 attach function, so that it can be the main thread instead of
335 whichever we were told to attach to. */
340 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
342 /* GDB knows to ignore the first SIGSTOP after attaching to a running
343 process using the "attach" command, but this is different; it's
344 just using "target remote". Pretend it's just starting up. */
345 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
346 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
347 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
349 current_inferior
->last_resume_kind
= resume_stop
;
350 current_inferior
->last_status
= last_status
;
356 extern int remote_debug
;
358 /* Decode a qXfer read request. Return 0 if everything looks OK,
362 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
364 /* After the read marker and annex, qXfer looks like a
365 traditional 'm' packet. */
366 decode_m_packet (buf
, ofs
, len
);
372 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
374 /* Extract and NUL-terminate the object. */
376 while (*buf
&& *buf
!= ':')
382 /* Extract and NUL-terminate the read/write action. */
384 while (*buf
&& *buf
!= ':')
390 /* Extract and NUL-terminate the annex. */
392 while (*buf
&& *buf
!= ':')
402 /* Write the response to a successful qXfer read. Returns the
403 length of the (binary) data stored in BUF, corresponding
404 to as much of DATA/LEN as we could fit. IS_MORE controls
405 the first character of the response. */
407 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
416 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
420 /* Handle all of the extended 'Q' packets. */
423 handle_general_set (char *own_buf
)
425 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
427 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
428 const char *p
= own_buf
+ strlen ("QPassSignals:");
431 p
= decode_address_to_semicolon (&cursig
, p
);
432 for (i
= 0; i
< numsigs
; i
++)
438 /* Keep looping, to clear the remaining signals. */
441 p
= decode_address_to_semicolon (&cursig
, p
);
446 strcpy (own_buf
, "OK");
450 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
454 fprintf (stderr
, "[noack mode enabled]\n");
463 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
465 char *mode
= own_buf
+ 9;
469 if (strcmp (mode
, "0") == 0)
471 else if (strcmp (mode
, "1") == 0)
475 /* We don't know what this mode is, so complain to
477 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
483 req_str
= req
? "non-stop" : "all-stop";
484 if (start_non_stop (req
) != 0)
486 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
494 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
500 if (target_supports_tracepoints ()
501 && handle_tracepoint_general_set (own_buf
))
504 /* Otherwise we didn't know what packet it was. Say we didn't
510 get_features_xml (const char *annex
)
512 /* gdbserver_xmltarget defines what to return when looking
513 for the "target.xml" file. Its contents can either be
514 verbatim XML code (prefixed with a '@') or else the name
515 of the actual XML file to be used in place of "target.xml".
517 This variable is set up from the auto-generated
518 init_registers_... routine for the current target. */
520 if (gdbserver_xmltarget
521 && strcmp (annex
, "target.xml") == 0)
523 if (*gdbserver_xmltarget
== '@')
524 return gdbserver_xmltarget
+ 1;
526 annex
= gdbserver_xmltarget
;
531 extern const char *const xml_builtin
[][2];
534 /* Look for the annex. */
535 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
536 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
539 if (xml_builtin
[i
][0] != NULL
)
540 return xml_builtin
[i
][1];
548 monitor_show_help (void)
550 monitor_output ("The following monitor commands are supported:\n");
551 monitor_output (" set debug <0|1>\n");
552 monitor_output (" Enable general debugging messages\n");
553 monitor_output (" set debug-hw-points <0|1>\n");
554 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
555 monitor_output (" set remote-debug <0|1>\n");
556 monitor_output (" Enable remote protocol debugging messages\n");
557 monitor_output (" exit\n");
558 monitor_output (" Quit GDBserver\n");
561 /* Read trace frame or inferior memory. Returns the number of bytes
562 actually read, zero when no further transfer is possible, and -1 on
563 error. Return of a positive value smaller than LEN does not
564 indicate there's no more to be read, only the end of the transfer.
565 E.g., when GDB reads memory from a traceframe, a first request may
566 be served from a memory block that does not cover the whole request
567 length. A following request gets the rest served from either
568 another block (of the same traceframe) or from the read-only
572 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
576 if (current_traceframe
>= 0)
579 ULONGEST length
= len
;
581 if (traceframe_read_mem (current_traceframe
,
582 memaddr
, myaddr
, len
, &nbytes
))
584 /* Data read from trace buffer, we're done. */
587 if (!in_readonly_region (memaddr
, length
))
589 /* Otherwise we have a valid readonly case, fall through. */
590 /* (assume no half-trace half-real blocks for now) */
593 res
= prepare_to_access_memory ();
596 res
= read_inferior_memory (memaddr
, myaddr
, len
);
597 done_accessing_memory ();
599 return res
== 0 ? len
: -1;
605 /* Write trace frame or inferior memory. Actually, writing to trace
606 frames is forbidden. */
609 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
611 if (current_traceframe
>= 0)
617 ret
= prepare_to_access_memory ();
620 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
621 done_accessing_memory ();
627 /* Subroutine of handle_search_memory to simplify it. */
630 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
631 gdb_byte
*pattern
, unsigned pattern_len
,
632 gdb_byte
*search_buf
,
633 unsigned chunk_size
, unsigned search_buf_size
,
634 CORE_ADDR
*found_addrp
)
636 /* Prime the search buffer. */
638 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
641 warning ("Unable to access target memory at 0x%lx, halting search.",
646 /* Perform the search.
648 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
649 When we've scanned N bytes we copy the trailing bytes to the start and
650 read in another N bytes. */
652 while (search_space_len
>= pattern_len
)
655 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
659 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
661 if (found_ptr
!= NULL
)
663 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
664 *found_addrp
= found_addr
;
668 /* Not found in this chunk, skip to next chunk. */
670 /* Don't let search_space_len wrap here, it's unsigned. */
671 if (search_space_len
>= chunk_size
)
672 search_space_len
-= chunk_size
;
674 search_space_len
= 0;
676 if (search_space_len
>= pattern_len
)
678 unsigned keep_len
= search_buf_size
- chunk_size
;
679 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
682 /* Copy the trailing part of the previous iteration to the front
683 of the buffer for the next iteration. */
684 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
686 nr_to_read
= (search_space_len
- keep_len
< chunk_size
687 ? search_space_len
- keep_len
690 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
691 nr_to_read
) != search_buf_size
)
693 warning ("Unable to access target memory "
694 "at 0x%lx, halting search.",
699 start_addr
+= chunk_size
;
708 /* Handle qSearch:memory packets. */
711 handle_search_memory (char *own_buf
, int packet_len
)
713 CORE_ADDR start_addr
;
714 CORE_ADDR search_space_len
;
716 unsigned int pattern_len
;
717 /* NOTE: also defined in find.c testcase. */
718 #define SEARCH_CHUNK_SIZE 16000
719 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
720 /* Buffer to hold memory contents for searching. */
721 gdb_byte
*search_buf
;
722 unsigned search_buf_size
;
724 CORE_ADDR found_addr
;
725 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
727 pattern
= malloc (packet_len
);
730 error ("Unable to allocate memory to perform the search");
731 strcpy (own_buf
, "E00");
734 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
735 packet_len
- cmd_name_len
,
736 &start_addr
, &search_space_len
,
737 pattern
, &pattern_len
) < 0)
740 error ("Error in parsing qSearch:memory packet");
741 strcpy (own_buf
, "E00");
745 search_buf_size
= chunk_size
+ pattern_len
- 1;
747 /* No point in trying to allocate a buffer larger than the search space. */
748 if (search_space_len
< search_buf_size
)
749 search_buf_size
= search_space_len
;
751 search_buf
= malloc (search_buf_size
);
752 if (search_buf
== NULL
)
755 error ("Unable to allocate memory to perform the search");
756 strcpy (own_buf
, "E00");
760 found
= handle_search_memory_1 (start_addr
, search_space_len
,
761 pattern
, pattern_len
,
762 search_buf
, chunk_size
, search_buf_size
,
766 sprintf (own_buf
, "1,%lx", (long) found_addr
);
768 strcpy (own_buf
, "0");
770 strcpy (own_buf
, "E00");
776 #define require_running(BUF) \
777 if (!target_running ()) \
783 /* Handle monitor commands not handled by target-specific handlers. */
786 handle_monitor_command (char *mon
)
788 if (strcmp (mon
, "set debug 1") == 0)
791 monitor_output ("Debug output enabled.\n");
793 else if (strcmp (mon
, "set debug 0") == 0)
796 monitor_output ("Debug output disabled.\n");
798 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
801 monitor_output ("H/W point debugging output enabled.\n");
803 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
806 monitor_output ("H/W point debugging output disabled.\n");
808 else if (strcmp (mon
, "set remote-debug 1") == 0)
811 monitor_output ("Protocol debug output enabled.\n");
813 else if (strcmp (mon
, "set remote-debug 0") == 0)
816 monitor_output ("Protocol debug output disabled.\n");
818 else if (strcmp (mon
, "help") == 0)
819 monitor_show_help ();
820 else if (strcmp (mon
, "exit") == 0)
824 monitor_output ("Unknown monitor command.\n\n");
825 monitor_show_help ();
830 /* Associates a callback with each supported qXfer'able object. */
834 /* The object this handler handles. */
837 /* Request that the target transfer up to LEN 8-bit bytes of the
838 target's OBJECT. The OFFSET, for a seekable object, specifies
839 the starting point. The ANNEX can be used to provide additional
840 data-specific information to the target.
842 Return the number of bytes actually transfered, zero when no
843 further transfer is possible, -1 on error, and -2 when the
844 transfer is not supported. Return of a positive value smaller
845 than LEN does not indicate the end of the object, only the end of
848 One, and only one, of readbuf or writebuf must be non-NULL. */
849 int (*xfer
) (const char *annex
,
850 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
851 ULONGEST offset
, LONGEST len
);
854 /* Handle qXfer:auxv:read. */
857 handle_qxfer_auxv (const char *annex
,
858 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
859 ULONGEST offset
, LONGEST len
)
861 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
864 if (annex
[0] != '\0' || !target_running ())
867 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
870 /* Handle qXfer:features:read. */
873 handle_qxfer_features (const char *annex
,
874 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
875 ULONGEST offset
, LONGEST len
)
877 const char *document
;
880 if (writebuf
!= NULL
)
883 if (!target_running ())
886 /* Grab the correct annex. */
887 document
= get_features_xml (annex
);
888 if (document
== NULL
)
891 total_len
= strlen (document
);
893 if (offset
> total_len
)
896 if (offset
+ len
> total_len
)
897 len
= total_len
- offset
;
899 memcpy (readbuf
, document
+ offset
, len
);
903 /* Handle qXfer:libraries:read. */
906 handle_qxfer_libraries (const char *annex
,
907 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
908 ULONGEST offset
, LONGEST len
)
910 unsigned int total_len
;
912 struct inferior_list_entry
*dll_ptr
;
914 if (writebuf
!= NULL
)
917 if (annex
[0] != '\0' || !target_running ())
920 /* Over-estimate the necessary memory. Assume that every character
921 in the library name must be escaped. */
923 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
924 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
926 document
= malloc (total_len
);
927 if (document
== NULL
)
930 strcpy (document
, "<library-list>\n");
931 p
= document
+ strlen (document
);
933 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
935 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
938 strcpy (p
, " <library name=\"");
940 name
= xml_escape_text (dll
->name
);
944 strcpy (p
, "\"><segment address=\"");
946 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
948 strcpy (p
, "\"/></library>\n");
952 strcpy (p
, "</library-list>\n");
954 total_len
= strlen (document
);
956 if (offset
> total_len
)
962 if (offset
+ len
> total_len
)
963 len
= total_len
- offset
;
965 memcpy (readbuf
, document
+ offset
, len
);
970 /* Handle qXfer:osadata:read. */
973 handle_qxfer_osdata (const char *annex
,
974 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
975 ULONGEST offset
, LONGEST len
)
977 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
980 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
983 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
986 handle_qxfer_siginfo (const char *annex
,
987 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
988 ULONGEST offset
, LONGEST len
)
990 if (the_target
->qxfer_siginfo
== NULL
)
993 if (annex
[0] != '\0' || !target_running ())
996 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
999 /* Handle qXfer:spu:read and qXfer:spu:write. */
1002 handle_qxfer_spu (const char *annex
,
1003 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1004 ULONGEST offset
, LONGEST len
)
1006 if (the_target
->qxfer_spu
== NULL
)
1009 if (!target_running ())
1012 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1015 /* Handle qXfer:statictrace:read. */
1018 handle_qxfer_statictrace (const char *annex
,
1019 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1020 ULONGEST offset
, LONGEST len
)
1024 if (writebuf
!= NULL
)
1027 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1030 if (traceframe_read_sdata (current_traceframe
, offset
,
1031 readbuf
, len
, &nbytes
))
1036 /* Helper for handle_qxfer_threads. */
1039 handle_qxfer_threads_proper (struct buffer
*buffer
)
1041 struct inferior_list_entry
*thread
;
1043 buffer_grow_str (buffer
, "<threads>\n");
1045 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1047 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1052 write_ptid (ptid_s
, ptid
);
1054 if (the_target
->core_of_thread
)
1055 core
= (*the_target
->core_of_thread
) (ptid
);
1059 sprintf (core_s
, "%d", core
);
1060 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1065 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1070 buffer_grow_str0 (buffer
, "</threads>\n");
1073 /* Handle qXfer:threads:read. */
1076 handle_qxfer_threads (const char *annex
,
1077 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1078 ULONGEST offset
, LONGEST len
)
1080 static char *result
= 0;
1081 static unsigned int result_length
= 0;
1083 if (writebuf
!= NULL
)
1086 if (!target_running () || annex
[0] != '\0')
1091 struct buffer buffer
;
1092 /* When asked for data at offset 0, generate everything and store into
1093 'result'. Successive reads will be served off 'result'. */
1097 buffer_init (&buffer
);
1099 handle_qxfer_threads_proper (&buffer
);
1101 result
= buffer_finish (&buffer
);
1102 result_length
= strlen (result
);
1103 buffer_free (&buffer
);
1106 if (offset
>= result_length
)
1108 /* We're out of data. */
1115 if (len
> result_length
- offset
)
1116 len
= result_length
- offset
;
1118 memcpy (readbuf
, result
+ offset
, len
);
1123 /* Handle qXfer:traceframe-info:read. */
1126 handle_qxfer_traceframe_info (const char *annex
,
1127 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1128 ULONGEST offset
, LONGEST len
)
1130 static char *result
= 0;
1131 static unsigned int result_length
= 0;
1133 if (writebuf
!= NULL
)
1136 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1141 struct buffer buffer
;
1143 /* When asked for data at offset 0, generate everything and
1144 store into 'result'. Successive reads will be served off
1148 buffer_init (&buffer
);
1150 traceframe_read_info (current_traceframe
, &buffer
);
1152 result
= buffer_finish (&buffer
);
1153 result_length
= strlen (result
);
1154 buffer_free (&buffer
);
1157 if (offset
>= result_length
)
1159 /* We're out of data. */
1166 if (len
> result_length
- offset
)
1167 len
= result_length
- offset
;
1169 memcpy (readbuf
, result
+ offset
, len
);
1173 /* Handle qXfer:fdpic:read. */
1176 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1177 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1179 if (the_target
->read_loadmap
== NULL
)
1182 if (!target_running ())
1185 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1188 static const struct qxfer qxfer_packets
[] =
1190 { "auxv", handle_qxfer_auxv
},
1191 { "fdpic", handle_qxfer_fdpic
},
1192 { "features", handle_qxfer_features
},
1193 { "libraries", handle_qxfer_libraries
},
1194 { "osdata", handle_qxfer_osdata
},
1195 { "siginfo", handle_qxfer_siginfo
},
1196 { "spu", handle_qxfer_spu
},
1197 { "statictrace", handle_qxfer_statictrace
},
1198 { "threads", handle_qxfer_threads
},
1199 { "traceframe-info", handle_qxfer_traceframe_info
},
1203 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1211 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1214 /* Grab the object, r/w and annex. */
1215 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1217 write_enn (own_buf
);
1222 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1225 const struct qxfer
*q
= &qxfer_packets
[i
];
1227 if (strcmp (object
, q
->object
) == 0)
1229 if (strcmp (rw
, "read") == 0)
1231 unsigned char *data
;
1236 /* Grab the offset and length. */
1237 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1239 write_enn (own_buf
);
1243 /* Read one extra byte, as an indicator of whether there is
1245 if (len
> PBUFSIZ
- 2)
1247 data
= malloc (len
+ 1);
1250 write_enn (own_buf
);
1253 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1260 write_enn (own_buf
);
1262 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1264 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1269 else if (strcmp (rw
, "write") == 0)
1274 unsigned char *data
;
1276 strcpy (own_buf
, "E00");
1277 data
= malloc (packet_len
- (offset
- own_buf
));
1280 write_enn (own_buf
);
1283 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1284 &ofs
, &len
, data
) < 0)
1287 write_enn (own_buf
);
1291 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1298 write_enn (own_buf
);
1300 sprintf (own_buf
, "%x", n
);
1313 /* Table used by the crc32 function to calcuate the checksum. */
1315 static unsigned int crc32_table
[256] =
1318 /* Compute 32 bit CRC from inferior memory.
1320 On success, return 32 bit CRC.
1321 On failure, return (unsigned long long) -1. */
1323 static unsigned long long
1324 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1326 if (!crc32_table
[1])
1328 /* Initialize the CRC table and the decoding table. */
1332 for (i
= 0; i
< 256; i
++)
1334 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1335 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1342 unsigned char byte
= 0;
1344 /* Return failure if memory read fails. */
1345 if (read_inferior_memory (base
, &byte
, 1) != 0)
1346 return (unsigned long long) -1;
1348 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1351 return (unsigned long long) crc
;
1354 /* Handle all of the extended 'q' packets. */
1357 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1359 static struct inferior_list_entry
*thread_ptr
;
1361 /* Reply the current thread id. */
1362 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1365 require_running (own_buf
);
1367 if (!ptid_equal (general_thread
, null_ptid
)
1368 && !ptid_equal (general_thread
, minus_one_ptid
))
1369 gdb_id
= general_thread
;
1372 thread_ptr
= all_threads
.head
;
1373 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1376 sprintf (own_buf
, "QC");
1378 write_ptid (own_buf
, gdb_id
);
1382 if (strcmp ("qSymbol::", own_buf
) == 0)
1384 /* GDB is suggesting new symbols have been loaded. This may
1385 mean a new shared library has been detected as loaded, so
1386 take the opportunity to check if breakpoints we think are
1387 inserted, still are. Note that it isn't guaranteed that
1388 we'll see this when a shared library is loaded, and nor will
1389 we see this for unloads (although breakpoints in unloaded
1390 libraries shouldn't trigger), as GDB may not find symbols for
1391 the library at all. We also re-validate breakpoints when we
1392 see a second GDB breakpoint for the same address, and or when
1393 we access breakpoint shadows. */
1394 validate_breakpoints ();
1396 if (target_supports_tracepoints ())
1397 tracepoint_look_up_symbols ();
1399 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1400 (*the_target
->look_up_symbols
) ();
1402 strcpy (own_buf
, "OK");
1406 if (!disable_packet_qfThreadInfo
)
1408 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1412 require_running (own_buf
);
1413 thread_ptr
= all_threads
.head
;
1416 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1417 write_ptid (own_buf
, gdb_id
);
1418 thread_ptr
= thread_ptr
->next
;
1422 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1426 require_running (own_buf
);
1427 if (thread_ptr
!= NULL
)
1430 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1431 write_ptid (own_buf
, gdb_id
);
1432 thread_ptr
= thread_ptr
->next
;
1437 sprintf (own_buf
, "l");
1443 if (the_target
->read_offsets
!= NULL
1444 && strcmp ("qOffsets", own_buf
) == 0)
1446 CORE_ADDR text
, data
;
1448 require_running (own_buf
);
1449 if (the_target
->read_offsets (&text
, &data
))
1450 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1451 (long)text
, (long)data
, (long)data
);
1453 write_enn (own_buf
);
1458 /* Protocol features query. */
1459 if (strncmp ("qSupported", own_buf
, 10) == 0
1460 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1462 char *p
= &own_buf
[10];
1463 int gdb_supports_qRelocInsn
= 0;
1465 /* Start processing qSupported packet. */
1466 target_process_qsupported (NULL
);
1468 /* Process each feature being provided by GDB. The first
1469 feature will follow a ':', and latter features will follow
1473 char **qsupported
= NULL
;
1477 /* Two passes, to avoid nested strtok calls in
1478 target_process_qsupported. */
1479 for (p
= strtok (p
+ 1, ";");
1481 p
= strtok (NULL
, ";"))
1484 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1485 qsupported
[count
- 1] = xstrdup (p
);
1488 for (i
= 0; i
< count
; i
++)
1491 if (strcmp (p
, "multiprocess+") == 0)
1493 /* GDB supports and wants multi-process support if
1495 if (target_supports_multi_process ())
1498 else if (strcmp (p
, "qRelocInsn+") == 0)
1500 /* GDB supports relocate instruction requests. */
1501 gdb_supports_qRelocInsn
= 1;
1504 target_process_qsupported (p
);
1512 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1514 /* We do not have any hook to indicate whether the target backend
1515 supports qXfer:libraries:read, so always report it. */
1516 strcat (own_buf
, ";qXfer:libraries:read+");
1518 if (the_target
->read_auxv
!= NULL
)
1519 strcat (own_buf
, ";qXfer:auxv:read+");
1521 if (the_target
->qxfer_spu
!= NULL
)
1522 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1524 if (the_target
->qxfer_siginfo
!= NULL
)
1525 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1527 if (the_target
->read_loadmap
!= NULL
)
1528 strcat (own_buf
, ";qXfer:fdpic:read+");
1530 /* We always report qXfer:features:read, as targets may
1531 install XML files on a subsequent call to arch_setup.
1532 If we reported to GDB on startup that we don't support
1533 qXfer:feature:read at all, we will never be re-queried. */
1534 strcat (own_buf
, ";qXfer:features:read+");
1536 if (transport_is_reliable
)
1537 strcat (own_buf
, ";QStartNoAckMode+");
1539 if (the_target
->qxfer_osdata
!= NULL
)
1540 strcat (own_buf
, ";qXfer:osdata:read+");
1542 if (target_supports_multi_process ())
1543 strcat (own_buf
, ";multiprocess+");
1545 if (target_supports_non_stop ())
1546 strcat (own_buf
, ";QNonStop+");
1548 strcat (own_buf
, ";qXfer:threads:read+");
1550 if (target_supports_tracepoints ())
1552 strcat (own_buf
, ";ConditionalTracepoints+");
1553 strcat (own_buf
, ";TraceStateVariables+");
1554 strcat (own_buf
, ";TracepointSource+");
1555 strcat (own_buf
, ";DisconnectedTracing+");
1556 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1557 strcat (own_buf
, ";FastTracepoints+");
1558 strcat (own_buf
, ";StaticTracepoints+");
1559 strcat (own_buf
, ";qXfer:statictrace:read+");
1560 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1561 strcat (own_buf
, ";EnableDisableTracepoints+");
1567 /* Thread-local storage support. */
1568 if (the_target
->get_tls_address
!= NULL
1569 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1571 char *p
= own_buf
+ 12;
1572 CORE_ADDR parts
[2], address
= 0;
1574 ptid_t ptid
= null_ptid
;
1576 require_running (own_buf
);
1578 for (i
= 0; i
< 3; i
++)
1586 p2
= strchr (p
, ',');
1599 ptid
= read_ptid (p
, NULL
);
1601 decode_address (&parts
[i
- 1], p
, len
);
1605 if (p
!= NULL
|| i
< 3)
1609 struct thread_info
*thread
= find_thread_ptid (ptid
);
1614 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1620 strcpy (own_buf
, paddress(address
));
1625 write_enn (own_buf
);
1629 /* Otherwise, pretend we do not understand this packet. */
1632 /* Windows OS Thread Information Block address support. */
1633 if (the_target
->get_tib_address
!= NULL
1634 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1639 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1641 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1644 strcpy (own_buf
, paddress(tlb
));
1649 write_enn (own_buf
);
1655 /* Handle "monitor" commands. */
1656 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1658 char *mon
= malloc (PBUFSIZ
);
1659 int len
= strlen (own_buf
+ 6);
1663 write_enn (own_buf
);
1667 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1669 write_enn (own_buf
);
1673 mon
[len
/ 2] = '\0';
1677 if (the_target
->handle_monitor_command
== NULL
1678 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1679 /* Default processing. */
1680 handle_monitor_command (mon
);
1686 if (strncmp ("qSearch:memory:", own_buf
,
1687 sizeof ("qSearch:memory:") - 1) == 0)
1689 require_running (own_buf
);
1690 handle_search_memory (own_buf
, packet_len
);
1694 if (strcmp (own_buf
, "qAttached") == 0
1695 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1697 struct process_info
*process
;
1699 if (own_buf
[sizeof ("qAttached") - 1])
1701 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1702 process
= (struct process_info
*)
1703 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1707 require_running (own_buf
);
1708 process
= current_process ();
1711 if (process
== NULL
)
1713 write_enn (own_buf
);
1717 strcpy (own_buf
, process
->attached
? "1" : "0");
1721 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1723 /* CRC check (compare-section). */
1727 unsigned long long crc
;
1729 require_running (own_buf
);
1730 base
= strtoul (own_buf
+ 5, &comma
, 16);
1731 if (*comma
++ != ',')
1733 write_enn (own_buf
);
1736 len
= strtoul (comma
, NULL
, 16);
1737 crc
= crc32 (base
, len
, 0xffffffff);
1738 /* Check for memory failure. */
1739 if (crc
== (unsigned long long) -1)
1741 write_enn (own_buf
);
1744 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1748 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1751 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1754 /* Otherwise we didn't know what packet it was. Say we didn't
1759 static void gdb_wants_all_threads_stopped (void);
1761 /* Parse vCont packets. */
1763 handle_v_cont (char *own_buf
)
1767 struct thread_resume
*resume_info
;
1768 struct thread_resume default_action
= {{0}};
1770 /* Count the number of semicolons in the packet. There should be one
1771 for every action. */
1777 p
= strchr (p
, ';');
1780 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1781 if (resume_info
== NULL
)
1789 if (p
[0] == 's' || p
[0] == 'S')
1790 resume_info
[i
].kind
= resume_step
;
1791 else if (p
[0] == 'c' || p
[0] == 'C')
1792 resume_info
[i
].kind
= resume_continue
;
1793 else if (p
[0] == 't')
1794 resume_info
[i
].kind
= resume_stop
;
1798 if (p
[0] == 'S' || p
[0] == 'C')
1801 sig
= strtol (p
+ 1, &q
, 16);
1806 if (!target_signal_to_host_p (sig
))
1808 resume_info
[i
].sig
= target_signal_to_host (sig
);
1812 resume_info
[i
].sig
= 0;
1818 resume_info
[i
].thread
= minus_one_ptid
;
1819 default_action
= resume_info
[i
];
1821 /* Note: we don't increment i here, we'll overwrite this entry
1822 the next time through. */
1824 else if (p
[0] == ':')
1826 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1831 if (p
[0] != ';' && p
[0] != 0)
1834 resume_info
[i
].thread
= ptid
;
1841 resume_info
[i
] = default_action
;
1843 /* Still used in occasional places in the backend. */
1845 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1846 && resume_info
[0].kind
!= resume_stop
)
1847 cont_thread
= resume_info
[0].thread
;
1849 cont_thread
= minus_one_ptid
;
1850 set_desired_inferior (0);
1855 (*the_target
->resume
) (resume_info
, n
);
1863 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1865 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1866 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1867 current_inferior
->last_status
= last_status
;
1869 /* From the client's perspective, all-stop mode always stops all
1870 threads implicitly (and the target backend has already done
1871 so by now). Tag all threads as "want-stopped", so we don't
1872 resume them implicitly without the client telling us to. */
1873 gdb_wants_all_threads_stopped ();
1874 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1875 disable_async_io ();
1877 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1878 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1879 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1884 write_enn (own_buf
);
1889 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1891 handle_v_attach (char *own_buf
)
1895 pid
= strtol (own_buf
+ 8, NULL
, 16);
1896 if (pid
!= 0 && attach_inferior (pid
) == 0)
1898 /* Don't report shared library events after attaching, even if
1899 some libraries are preloaded. GDB will always poll the
1900 library list. Avoids the "stopped by shared library event"
1901 notice on the GDB side. */
1906 /* In non-stop, we don't send a resume reply. Stop events
1907 will follow up using the normal notification
1912 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1918 write_enn (own_buf
);
1923 /* Run a new program. Return 1 if successful, 0 if failure. */
1925 handle_v_run (char *own_buf
)
1927 char *p
, *next_p
, **new_argv
;
1931 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1937 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1938 if (new_argv
== NULL
)
1940 write_enn (own_buf
);
1945 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1947 next_p
= strchr (p
, ';');
1949 next_p
= p
+ strlen (p
);
1951 if (i
== 0 && p
== next_p
)
1955 /* FIXME: Fail request if out of memory instead of dying. */
1956 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1957 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1958 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1967 if (new_argv
[0] == NULL
)
1969 /* GDB didn't specify a program to run. Use the program from the
1970 last run with the new argument list. */
1972 if (program_argv
== NULL
)
1974 write_enn (own_buf
);
1975 freeargv (new_argv
);
1979 new_argv
[0] = strdup (program_argv
[0]);
1980 if (new_argv
[0] == NULL
)
1982 write_enn (own_buf
);
1983 freeargv (new_argv
);
1988 /* Free the old argv and install the new one. */
1989 freeargv (program_argv
);
1990 program_argv
= new_argv
;
1992 start_inferior (program_argv
);
1993 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1995 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1997 /* In non-stop, sending a resume reply doesn't set the general
1998 thread, but GDB assumes a vRun sets it (this is so GDB can
1999 query which is the main thread of the new inferior. */
2001 general_thread
= last_ptid
;
2007 write_enn (own_buf
);
2012 /* Kill process. Return 1 if successful, 0 if failure. */
2014 handle_v_kill (char *own_buf
)
2017 char *p
= &own_buf
[6];
2019 pid
= strtol (p
, NULL
, 16);
2022 if (pid
!= 0 && kill_inferior (pid
) == 0)
2024 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2025 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2026 last_ptid
= pid_to_ptid (pid
);
2027 discard_queued_stop_replies (pid
);
2033 write_enn (own_buf
);
2038 /* Handle a 'vStopped' packet. */
2040 handle_v_stopped (char *own_buf
)
2042 /* If we're waiting for GDB to acknowledge a pending stop reply,
2043 consider that done. */
2046 struct vstop_notif
*head
;
2049 fprintf (stderr
, "vStopped: acking %s\n",
2050 target_pid_to_str (notif_queue
->ptid
));
2053 notif_queue
= notif_queue
->next
;
2057 /* Push another stop reply, or if there are no more left, an OK. */
2058 send_next_stop_reply (own_buf
);
2061 /* Handle all of the extended 'v' packets. */
2063 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2065 if (!disable_packet_vCont
)
2067 if (strncmp (own_buf
, "vCont;", 6) == 0)
2069 require_running (own_buf
);
2070 handle_v_cont (own_buf
);
2074 if (strncmp (own_buf
, "vCont?", 6) == 0)
2076 strcpy (own_buf
, "vCont;c;C;s;S;t");
2081 if (strncmp (own_buf
, "vFile:", 6) == 0
2082 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2085 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2087 if (!multi_process
&& target_running ())
2089 fprintf (stderr
, "Already debugging a process\n");
2090 write_enn (own_buf
);
2093 handle_v_attach (own_buf
);
2097 if (strncmp (own_buf
, "vRun;", 5) == 0)
2099 if (!multi_process
&& target_running ())
2101 fprintf (stderr
, "Already debugging a process\n");
2102 write_enn (own_buf
);
2105 handle_v_run (own_buf
);
2109 if (strncmp (own_buf
, "vKill;", 6) == 0)
2111 if (!target_running ())
2113 fprintf (stderr
, "No process to kill\n");
2114 write_enn (own_buf
);
2117 handle_v_kill (own_buf
);
2121 if (strncmp (own_buf
, "vStopped", 8) == 0)
2123 handle_v_stopped (own_buf
);
2127 /* Otherwise we didn't know what packet it was. Say we didn't
2133 /* Resume inferior and wait for another event. In non-stop mode,
2134 don't really wait here, but return immediatelly to the event
2137 myresume (char *own_buf
, int step
, int sig
)
2139 struct thread_resume resume_info
[2];
2141 int valid_cont_thread
;
2143 set_desired_inferior (0);
2145 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2146 && !ptid_equal (cont_thread
, minus_one_ptid
));
2148 if (step
|| sig
|| valid_cont_thread
)
2150 resume_info
[0].thread
2151 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2153 resume_info
[0].kind
= resume_step
;
2155 resume_info
[0].kind
= resume_continue
;
2156 resume_info
[0].sig
= sig
;
2160 if (!valid_cont_thread
)
2162 resume_info
[n
].thread
= minus_one_ptid
;
2163 resume_info
[n
].kind
= resume_continue
;
2164 resume_info
[n
].sig
= 0;
2171 (*the_target
->resume
) (resume_info
, n
);
2177 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2179 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2180 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2182 current_inferior
->last_resume_kind
= resume_stop
;
2183 current_inferior
->last_status
= last_status
;
2186 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2187 disable_async_io ();
2189 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2190 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2191 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2195 /* Callback for for_each_inferior. Make a new stop reply for each
2199 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2201 struct thread_info
*thread
= (struct thread_info
*) entry
;
2203 /* For now, assume targets that don't have this callback also don't
2204 manage the thread's last_status field. */
2205 if (the_target
->thread_stopped
== NULL
)
2207 /* Pass the last stop reply back to GDB, but don't notify
2209 queue_stop_reply (entry
->id
, &thread
->last_status
);
2213 if (thread_stopped (thread
))
2217 "Reporting thread %s as already stopped with %s\n",
2218 target_pid_to_str (entry
->id
),
2219 target_waitstatus_to_string (&thread
->last_status
));
2221 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2223 /* Pass the last stop reply back to GDB, but don't notify
2225 queue_stop_reply (entry
->id
, &thread
->last_status
);
2232 /* Set this inferior threads's state as "want-stopped". We won't
2233 resume this thread until the client gives us another action for
2237 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2239 struct thread_info
*thread
= (struct thread_info
*) entry
;
2241 thread
->last_resume_kind
= resume_stop
;
2243 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2245 /* Most threads are stopped implicitly (all-stop); tag that with
2247 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2248 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2252 /* Set all threads' states as "want-stopped". */
2255 gdb_wants_all_threads_stopped (void)
2257 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2260 /* Clear the gdb_detached flag of every process. */
2263 gdb_reattached_process (struct inferior_list_entry
*entry
)
2265 struct process_info
*process
= (struct process_info
*) entry
;
2267 process
->gdb_detached
= 0;
2270 /* Status handler for the '?' packet. */
2273 handle_status (char *own_buf
)
2275 /* GDB is connected, don't forward events to the target anymore. */
2276 for_each_inferior (&all_processes
, gdb_reattached_process
);
2278 /* In non-stop mode, we must send a stop reply for each stopped
2279 thread. In all-stop mode, just send one for the first stopped
2284 discard_queued_stop_replies (-1);
2285 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2287 /* The first is sent immediatly. OK is sent if there is no
2288 stopped thread, which is the same handling of the vStopped
2289 packet (by design). */
2290 send_next_stop_reply (own_buf
);
2295 stabilize_threads ();
2296 gdb_wants_all_threads_stopped ();
2298 if (all_threads
.head
)
2300 struct target_waitstatus status
;
2302 status
.kind
= TARGET_WAITKIND_STOPPED
;
2303 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2304 prepare_resume_reply (own_buf
,
2305 all_threads
.head
->id
, &status
);
2308 strcpy (own_buf
, "W00");
2313 gdbserver_version (void)
2315 printf ("GNU gdbserver %s%s\n"
2316 "Copyright (C) 2011 Free Software Foundation, Inc.\n"
2317 "gdbserver is free software, covered by the "
2318 "GNU General Public License.\n"
2319 "This gdbserver was configured as \"%s\"\n",
2320 PKGVERSION
, version
, host_name
);
2324 gdbserver_usage (FILE *stream
)
2326 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2327 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2328 "\tgdbserver [OPTIONS] --multi COMM\n"
2330 "COMM may either be a tty device (for serial debugging), or \n"
2331 "HOST:PORT to listen for a TCP connection.\n"
2334 " --debug Enable general debugging output.\n"
2335 " --remote-debug Enable remote protocol debugging output.\n"
2336 " --version Display version information and exit.\n"
2337 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2338 " --once Exit after the first connection has "
2340 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2341 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2345 gdbserver_show_disableable (FILE *stream
)
2347 fprintf (stream
, "Disableable packets:\n"
2348 " vCont \tAll vCont packets\n"
2349 " qC \tQuerying the current thread\n"
2350 " qfThreadInfo\tThread listing\n"
2351 " Tthread \tPassing the thread specifier in the "
2352 "T stop reply packet\n"
2353 " threads \tAll of the above\n");
2357 #undef require_running
2358 #define require_running(BUF) \
2359 if (!target_running ()) \
2366 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2368 int pid
= * (int *) args
;
2370 if (ptid_get_pid (entry
->id
) == pid
)
2377 kill_inferior_callback (struct inferior_list_entry
*entry
)
2379 struct process_info
*process
= (struct process_info
*) entry
;
2380 int pid
= ptid_get_pid (process
->head
.id
);
2382 kill_inferior (pid
);
2383 discard_queued_stop_replies (pid
);
2386 /* Callback for for_each_inferior to detach or kill the inferior,
2387 depending on whether we attached to it or not.
2388 We inform the user whether we're detaching or killing the process
2389 as this is only called when gdbserver is about to exit. */
2392 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2394 struct process_info
*process
= (struct process_info
*) entry
;
2395 int pid
= ptid_get_pid (process
->head
.id
);
2397 if (process
->attached
)
2398 detach_inferior (pid
);
2400 kill_inferior (pid
);
2402 discard_queued_stop_replies (pid
);
2405 /* for_each_inferior callback for detach_or_kill_for_exit to print
2406 the pids of started inferiors. */
2409 print_started_pid (struct inferior_list_entry
*entry
)
2411 struct process_info
*process
= (struct process_info
*) entry
;
2413 if (! process
->attached
)
2415 int pid
= ptid_get_pid (process
->head
.id
);
2416 fprintf (stderr
, " %d", pid
);
2420 /* for_each_inferior callback for detach_or_kill_for_exit to print
2421 the pids of attached inferiors. */
2424 print_attached_pid (struct inferior_list_entry
*entry
)
2426 struct process_info
*process
= (struct process_info
*) entry
;
2428 if (process
->attached
)
2430 int pid
= ptid_get_pid (process
->head
.id
);
2431 fprintf (stderr
, " %d", pid
);
2435 /* Call this when exiting gdbserver with possible inferiors that need
2436 to be killed or detached from. */
2439 detach_or_kill_for_exit (void)
2441 /* First print a list of the inferiors we will be killing/detaching.
2442 This is to assist the user, for example, in case the inferior unexpectedly
2443 dies after we exit: did we screw up or did the inferior exit on its own?
2444 Having this info will save some head-scratching. */
2446 if (have_started_inferiors_p ())
2448 fprintf (stderr
, "Killing process(es):");
2449 for_each_inferior (&all_processes
, print_started_pid
);
2450 fprintf (stderr
, "\n");
2452 if (have_attached_inferiors_p ())
2454 fprintf (stderr
, "Detaching process(es):");
2455 for_each_inferior (&all_processes
, print_attached_pid
);
2456 fprintf (stderr
, "\n");
2459 /* Now we can kill or detach the inferiors. */
2461 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2465 main (int argc
, char *argv
[])
2469 char *arg_end
, *port
;
2470 char **next_arg
= &argv
[1];
2475 while (*next_arg
!= NULL
&& **next_arg
== '-')
2477 if (strcmp (*next_arg
, "--version") == 0)
2479 gdbserver_version ();
2482 else if (strcmp (*next_arg
, "--help") == 0)
2484 gdbserver_usage (stdout
);
2487 else if (strcmp (*next_arg
, "--attach") == 0)
2489 else if (strcmp (*next_arg
, "--multi") == 0)
2491 else if (strcmp (*next_arg
, "--wrapper") == 0)
2495 wrapper_argv
= next_arg
;
2496 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2499 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2501 gdbserver_usage (stderr
);
2505 /* Consume the "--". */
2508 else if (strcmp (*next_arg
, "--debug") == 0)
2510 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2512 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2514 gdbserver_show_disableable (stdout
);
2517 else if (strncmp (*next_arg
,
2518 "--disable-packet=",
2519 sizeof ("--disable-packet=") - 1) == 0)
2521 char *packets
, *tok
;
2523 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2524 for (tok
= strtok (packets
, ",");
2526 tok
= strtok (NULL
, ","))
2528 if (strcmp ("vCont", tok
) == 0)
2529 disable_packet_vCont
= 1;
2530 else if (strcmp ("Tthread", tok
) == 0)
2531 disable_packet_Tthread
= 1;
2532 else if (strcmp ("qC", tok
) == 0)
2533 disable_packet_qC
= 1;
2534 else if (strcmp ("qfThreadInfo", tok
) == 0)
2535 disable_packet_qfThreadInfo
= 1;
2536 else if (strcmp ("threads", tok
) == 0)
2538 disable_packet_vCont
= 1;
2539 disable_packet_Tthread
= 1;
2540 disable_packet_qC
= 1;
2541 disable_packet_qfThreadInfo
= 1;
2545 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2547 gdbserver_show_disableable (stderr
);
2552 else if (strcmp (*next_arg
, "--once") == 0)
2556 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2564 if (setjmp (toplevel
))
2566 fprintf (stderr
, "Exiting\n");
2572 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2574 gdbserver_usage (stderr
);
2581 /* --attach used to come after PORT, so allow it there for
2583 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2590 && (*next_arg
== NULL
2591 || (*next_arg
)[0] == '\0'
2592 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2594 || next_arg
[1] != NULL
))
2599 gdbserver_usage (stderr
);
2603 initialize_async_io ();
2605 if (target_supports_tracepoints ())
2606 initialize_tracepoint ();
2608 own_buf
= xmalloc (PBUFSIZ
+ 1);
2609 mem_buf
= xmalloc (PBUFSIZ
);
2611 if (pid
== 0 && *next_arg
!= NULL
)
2615 n
= argc
- (next_arg
- argv
);
2616 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2617 for (i
= 0; i
< n
; i
++)
2618 program_argv
[i
] = xstrdup (next_arg
[i
]);
2619 program_argv
[i
] = NULL
;
2621 /* Wait till we are at first instruction in program. */
2622 start_inferior (program_argv
);
2624 /* We are now (hopefully) stopped at the first instruction of
2625 the target process. This assumes that the target process was
2626 successfully created. */
2630 if (attach_inferior (pid
) == -1)
2631 error ("Attaching not supported on this target");
2633 /* Otherwise succeeded. */
2637 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2638 last_status
.value
.integer
= 0;
2639 last_ptid
= minus_one_ptid
;
2642 /* Don't report shared library events on the initial connection,
2643 even if some libraries are preloaded. Avoids the "stopped by
2644 shared library event" notice on gdb side. */
2647 if (setjmp (toplevel
))
2649 detach_or_kill_for_exit ();
2653 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2654 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2659 if (!was_running
&& !multi_mode
)
2661 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2665 remote_prepare (port
);
2671 /* Be sure we're out of tfind mode. */
2672 current_traceframe
= -1;
2676 if (setjmp (toplevel
) != 0)
2678 /* An error occurred. */
2679 if (response_needed
)
2681 write_enn (own_buf
);
2686 /* Wait for events. This will return when all event sources are
2687 removed from the event loop. */
2688 start_event_loop ();
2690 /* If an exit was requested (using the "monitor exit" command),
2691 terminate now. The only other way to get here is for
2692 getpkt to fail; close the connection and reopen it at the
2695 if (exit_requested
|| run_once
)
2697 detach_or_kill_for_exit ();
2702 "Remote side has terminated connection. "
2703 "GDBserver will reopen the connection.\n");
2707 if (disconnected_tracing
)
2709 /* Try to enable non-stop/async mode, so we we can both
2710 wait for an async socket accept, and handle async
2711 target events simultaneously. There's also no point
2712 either in having the target always stop all threads,
2713 when we're going to pass signals down without
2717 if (start_non_stop (1))
2720 /* Detaching implicitly resumes all threads; simply
2721 disconnecting does not. */
2727 "Disconnected tracing disabled; stopping trace run.\n");
2734 /* Event loop callback that handles a serial event. The first byte in
2735 the serial buffer gets us here. We expect characters to arrive at
2736 a brisk pace, so we read the rest of the packet with a blocking
2740 process_serial_event (void)
2751 int new_packet_len
= -1;
2753 /* Used to decide when gdbserver should exit in
2754 multi-mode/remote. */
2755 static int have_ran
= 0;
2758 have_ran
= target_running ();
2760 disable_async_io ();
2762 response_needed
= 0;
2763 packet_len
= getpkt (own_buf
);
2764 if (packet_len
<= 0)
2767 /* Force an event loop break. */
2770 response_needed
= 1;
2777 handle_query (own_buf
, packet_len
, &new_packet_len
);
2780 handle_general_set (own_buf
);
2783 require_running (own_buf
);
2788 pid
= strtol (&own_buf
[i
], NULL
, 16);
2792 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2794 if (tracing
&& disconnected_tracing
)
2796 struct thread_resume resume_info
;
2797 struct process_info
*process
= find_process_pid (pid
);
2799 if (process
== NULL
)
2801 write_enn (own_buf
);
2806 "Disconnected tracing in effect, "
2807 "leaving gdbserver attached to the process\n");
2809 /* Make sure we're in non-stop/async mode, so we we can both
2810 wait for an async socket accept, and handle async target
2811 events simultaneously. There's also no point either in
2812 having the target stop all threads, when we're going to
2813 pass signals down without informing GDB. */
2817 fprintf (stderr
, "Forcing non-stop mode\n");
2823 process
->gdb_detached
= 1;
2825 /* Detaching implicitly resumes all threads. */
2826 resume_info
.thread
= minus_one_ptid
;
2827 resume_info
.kind
= resume_continue
;
2828 resume_info
.sig
= 0;
2829 (*the_target
->resume
) (&resume_info
, 1);
2832 break; /* from switch/case */
2835 fprintf (stderr
, "Detaching from process %d\n", pid
);
2837 if (detach_inferior (pid
) != 0)
2838 write_enn (own_buf
);
2841 discard_queued_stop_replies (pid
);
2844 if (extended_protocol
)
2846 /* Treat this like a normal program exit. */
2847 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2848 last_status
.value
.integer
= 0;
2849 last_ptid
= pid_to_ptid (pid
);
2851 current_inferior
= NULL
;
2858 /* If we are attached, then we can exit. Otherwise, we
2859 need to hang around doing nothing, until the child is
2861 join_inferior (pid
);
2867 extended_protocol
= 1;
2871 handle_status (own_buf
);
2874 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2876 ptid_t gdb_id
, thread_id
;
2879 require_running (own_buf
);
2881 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2883 pid
= ptid_get_pid (gdb_id
);
2885 if (ptid_equal (gdb_id
, null_ptid
)
2886 || ptid_equal (gdb_id
, minus_one_ptid
))
2887 thread_id
= null_ptid
;
2889 && ptid_equal (pid_to_ptid (pid
),
2892 struct thread_info
*thread
=
2893 (struct thread_info
*) find_inferior (&all_threads
,
2898 write_enn (own_buf
);
2902 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2906 thread_id
= gdb_id_to_thread_id (gdb_id
);
2907 if (ptid_equal (thread_id
, null_ptid
))
2909 write_enn (own_buf
);
2914 if (own_buf
[1] == 'g')
2916 if (ptid_equal (thread_id
, null_ptid
))
2918 /* GDB is telling us to choose any thread. Check if
2919 the currently selected thread is still valid. If
2920 it is not, select the first available. */
2921 struct thread_info
*thread
=
2922 (struct thread_info
*) find_inferior_id (&all_threads
,
2925 thread_id
= all_threads
.head
->id
;
2928 general_thread
= thread_id
;
2929 set_desired_inferior (1);
2931 else if (own_buf
[1] == 'c')
2932 cont_thread
= thread_id
;
2938 /* Silently ignore it so that gdb can extend the protocol
2939 without compatibility headaches. */
2944 require_running (own_buf
);
2945 if (current_traceframe
>= 0)
2947 struct regcache
*regcache
= new_register_cache ();
2949 if (fetch_traceframe_registers (current_traceframe
,
2951 registers_to_string (regcache
, own_buf
);
2953 write_enn (own_buf
);
2954 free_register_cache (regcache
);
2958 struct regcache
*regcache
;
2960 set_desired_inferior (1);
2961 regcache
= get_thread_regcache (current_inferior
, 1);
2962 registers_to_string (regcache
, own_buf
);
2966 require_running (own_buf
);
2967 if (current_traceframe
>= 0)
2968 write_enn (own_buf
);
2971 struct regcache
*regcache
;
2973 set_desired_inferior (1);
2974 regcache
= get_thread_regcache (current_inferior
, 1);
2975 registers_from_string (regcache
, &own_buf
[1]);
2980 require_running (own_buf
);
2981 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2982 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
2984 write_enn (own_buf
);
2986 convert_int_to_ascii (mem_buf
, own_buf
, res
);
2989 require_running (own_buf
);
2990 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2991 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2994 write_enn (own_buf
);
2997 require_running (own_buf
);
2998 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2999 &mem_addr
, &len
, &mem_buf
) < 0
3000 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3001 write_enn (own_buf
);
3006 require_running (own_buf
);
3007 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3008 if (target_signal_to_host_p (sig
))
3009 signal
= target_signal_to_host (sig
);
3012 myresume (own_buf
, 0, signal
);
3015 require_running (own_buf
);
3016 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3017 if (target_signal_to_host_p (sig
))
3018 signal
= target_signal_to_host (sig
);
3021 myresume (own_buf
, 1, signal
);
3024 require_running (own_buf
);
3026 myresume (own_buf
, 0, signal
);
3029 require_running (own_buf
);
3031 myresume (own_buf
, 1, signal
);
3033 case 'Z': /* insert_ ... */
3035 case 'z': /* remove_ ... */
3039 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3040 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3041 char type
= own_buf
[1];
3043 const int insert
= ch
== 'Z';
3045 /* Default to unrecognized/unsupported. */
3049 case '0': /* software-breakpoint */
3050 case '1': /* hardware-breakpoint */
3051 case '2': /* write watchpoint */
3052 case '3': /* read watchpoint */
3053 case '4': /* access watchpoint */
3054 require_running (own_buf
);
3055 if (insert
&& the_target
->insert_point
!= NULL
)
3056 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3057 else if (!insert
&& the_target
->remove_point
!= NULL
)
3058 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3070 write_enn (own_buf
);
3074 response_needed
= 0;
3075 if (!target_running ())
3076 /* The packet we received doesn't make sense - but we can't
3077 reply to it, either. */
3080 fprintf (stderr
, "Killing all inferiors\n");
3081 for_each_inferior (&all_processes
, kill_inferior_callback
);
3083 /* When using the extended protocol, we wait with no program
3084 running. The traditional protocol will exit instead. */
3085 if (extended_protocol
)
3087 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3088 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3096 ptid_t gdb_id
, thread_id
;
3098 require_running (own_buf
);
3100 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3101 thread_id
= gdb_id_to_thread_id (gdb_id
);
3102 if (ptid_equal (thread_id
, null_ptid
))
3104 write_enn (own_buf
);
3108 if (mythread_alive (thread_id
))
3111 write_enn (own_buf
);
3115 response_needed
= 0;
3117 /* Restarting the inferior is only supported in the extended
3119 if (extended_protocol
)
3121 if (target_running ())
3122 for_each_inferior (&all_processes
,
3123 kill_inferior_callback
);
3124 fprintf (stderr
, "GDBserver restarting\n");
3126 /* Wait till we are at 1st instruction in prog. */
3127 if (program_argv
!= NULL
)
3128 start_inferior (program_argv
);
3131 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3132 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3138 /* It is a request we don't understand. Respond with an
3139 empty packet so that gdb knows that we don't support this
3145 /* Extended (long) request. */
3146 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3150 /* It is a request we don't understand. Respond with an empty
3151 packet so that gdb knows that we don't support this
3157 if (new_packet_len
!= -1)
3158 putpkt_binary (own_buf
, new_packet_len
);
3162 response_needed
= 0;
3164 if (!extended_protocol
&& have_ran
&& !target_running ())
3166 /* In non-stop, defer exiting until GDB had a chance to query
3167 the whole vStopped list (until it gets an OK). */
3170 fprintf (stderr
, "GDBserver exiting\n");
3182 /* Event-loop callback for serial events. */
3185 handle_serial_event (int err
, gdb_client_data client_data
)
3188 fprintf (stderr
, "handling possible serial event\n");
3190 /* Really handle it. */
3191 if (process_serial_event () < 0)
3194 /* Be sure to not change the selected inferior behind GDB's back.
3195 Important in the non-stop mode asynchronous protocol. */
3196 set_desired_inferior (1);
3201 /* Event-loop callback for target events. */
3204 handle_target_event (int err
, gdb_client_data client_data
)
3207 fprintf (stderr
, "handling possible target event\n");
3209 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3212 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3214 int pid
= ptid_get_pid (last_ptid
);
3215 struct process_info
*process
= find_process_pid (pid
);
3216 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3218 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3219 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3221 mark_breakpoints_out (process
);
3222 mourn_inferior (process
);
3226 /* We're reporting this thread as stopped. Update its
3227 "want-stopped" state to what the client wants, until it
3228 gets a new resume action. */
3229 current_inferior
->last_resume_kind
= resume_stop
;
3230 current_inferior
->last_status
= last_status
;
3235 if (!target_running ())
3237 /* The last process exited. We're done. */
3241 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3243 /* A thread stopped with a signal, but gdb isn't
3244 connected to handle it. Pass it down to the
3245 inferior, as if it wasn't being traced. */
3246 struct thread_resume resume_info
;
3250 "GDB not connected; forwarding event %d for [%s]\n",
3251 (int) last_status
.kind
,
3252 target_pid_to_str (last_ptid
));
3254 resume_info
.thread
= last_ptid
;
3255 resume_info
.kind
= resume_continue
;
3256 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3257 (*the_target
->resume
) (&resume_info
, 1);
3259 else if (debug_threads
)
3260 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3261 (int) last_status
.kind
,
3262 target_pid_to_str (last_ptid
));
3266 /* Something interesting. Tell GDB about it. */
3267 push_event (last_ptid
, &last_status
);
3271 /* Be sure to not change the selected inferior behind GDB's back.
3272 Important in the non-stop mode asynchronous protocol. */
3273 set_desired_inferior (1);