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
;
39 static int extended_protocol
;
40 static int response_needed
;
41 static int exit_requested
;
46 static char **program_argv
, **wrapper_argv
;
48 /* Enable miscellaneous debugging output. The name is historical - it
49 was originally used to debug LinuxThreads support. */
52 /* Enable debugging of h/w breakpoint/watchpoint support. */
55 int pass_signals
[TARGET_SIGNAL_LAST
];
59 const char *gdbserver_xmltarget
;
61 /* The PID of the originally created or attached inferior. Used to
62 send signals to the process when GDB sends us an asynchronous interrupt
63 (user hitting Control-C in the client), and to wait for the child to exit
64 when no longer debugging it. */
66 unsigned long signal_pid
;
69 /* A file descriptor for the controlling terminal. */
72 /* TERMINAL_FD's original foreground group. */
73 pid_t old_foreground_pgrp
;
75 /* Hand back terminal ownership to the original foreground group. */
78 restore_old_foreground_pgrp (void)
80 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
84 /* Set if you want to disable optional thread related packets support
85 in gdbserver, for the sake of testing GDB against stubs that don't
87 int disable_packet_vCont
;
88 int disable_packet_Tthread
;
89 int disable_packet_qC
;
90 int disable_packet_qfThreadInfo
;
92 /* Last status reported to GDB. */
93 static struct target_waitstatus last_status
;
94 static ptid_t last_ptid
;
97 static unsigned char *mem_buf
;
99 /* Structure holding information relative to a single stop reply. We
100 keep a queue of these (really a singly-linked list) to push to GDB
104 /* Pointer to next in list. */
105 struct vstop_notif
*next
;
107 /* Thread or process that got the event. */
111 struct target_waitstatus status
;
114 /* The pending stop replies list head. */
115 static struct vstop_notif
*notif_queue
= NULL
;
117 /* Put a stop reply to the stop reply queue. */
120 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
122 struct vstop_notif
*new_notif
;
124 new_notif
= malloc (sizeof (*new_notif
));
125 new_notif
->next
= NULL
;
126 new_notif
->ptid
= ptid
;
127 new_notif
->status
= *status
;
131 struct vstop_notif
*tail
;
132 for (tail
= notif_queue
;
136 tail
->next
= new_notif
;
139 notif_queue
= new_notif
;
144 struct vstop_notif
*n
;
146 for (n
= notif_queue
; n
; n
= n
->next
)
149 fprintf (stderr
, "pending stop replies: %d\n", i
);
153 /* Place an event in the stop reply queue, and push a notification if
154 we aren't sending one yet. */
157 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
159 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
161 queue_stop_reply (ptid
, status
);
163 /* If this is the first stop reply in the queue, then inform GDB
164 about it, by sending a Stop notification. */
165 if (notif_queue
->next
== NULL
)
170 prepare_resume_reply (p
,
171 notif_queue
->ptid
, ¬if_queue
->status
);
172 putpkt_notif (own_buf
);
176 /* Get rid of the currently pending stop replies for PID. If PID is
177 -1, then apply to all processes. */
180 discard_queued_stop_replies (int pid
)
182 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
184 for (reply
= notif_queue
; reply
; reply
= next
)
189 || ptid_get_pid (reply
->ptid
) == pid
)
191 if (reply
== notif_queue
)
194 prev
->next
= reply
->next
;
203 /* If there are more stop replies to push, push one now. */
206 send_next_stop_reply (char *own_buf
)
209 prepare_resume_reply (own_buf
,
211 ¬if_queue
->status
);
217 target_running (void)
219 return all_threads
.head
!= NULL
;
223 start_inferior (char **argv
)
225 char **new_argv
= argv
;
227 if (wrapper_argv
!= NULL
)
231 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
233 for (i
= 0; argv
[i
] != NULL
; i
++)
235 new_argv
= alloca (sizeof (char *) * count
);
237 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
238 new_argv
[count
++] = wrapper_argv
[i
];
239 for (i
= 0; argv
[i
] != NULL
; i
++)
240 new_argv
[count
++] = argv
[i
];
241 new_argv
[count
] = NULL
;
247 for (i
= 0; new_argv
[i
]; ++i
)
248 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
253 signal (SIGTTOU
, SIG_DFL
);
254 signal (SIGTTIN
, SIG_DFL
);
257 signal_pid
= create_inferior (new_argv
[0], new_argv
);
259 /* FIXME: we don't actually know at this point that the create
260 actually succeeded. We won't know that until we wait. */
261 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
266 signal (SIGTTOU
, SIG_IGN
);
267 signal (SIGTTIN
, SIG_IGN
);
268 terminal_fd
= fileno (stderr
);
269 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
270 tcsetpgrp (terminal_fd
, signal_pid
);
271 atexit (restore_old_foreground_pgrp
);
274 if (wrapper_argv
!= NULL
)
276 struct thread_resume resume_info
;
278 resume_info
.thread
= pid_to_ptid (signal_pid
);
279 resume_info
.kind
= resume_continue
;
282 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
284 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
289 (*the_target
->resume
) (&resume_info
, 1);
291 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
292 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
295 current_inferior
->last_resume_kind
= resume_stop
;
296 current_inferior
->last_status
= last_status
;
298 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
300 current_inferior
->last_resume_kind
= resume_stop
;
301 current_inferior
->last_status
= last_status
;
305 /* Wait till we are at 1st instruction in program, return new pid
306 (assuming success). */
307 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
309 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
310 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
312 current_inferior
->last_resume_kind
= resume_stop
;
313 current_inferior
->last_status
= last_status
;
320 attach_inferior (int pid
)
322 /* myattach should return -1 if attaching is unsupported,
323 0 if it succeeded, and call error() otherwise. */
325 if (myattach (pid
) != 0)
328 fprintf (stderr
, "Attached; pid = %d\n", pid
);
331 /* FIXME - It may be that we should get the SIGNAL_PID from the
332 attach function, so that it can be the main thread instead of
333 whichever we were told to attach to. */
338 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
340 /* GDB knows to ignore the first SIGSTOP after attaching to a running
341 process using the "attach" command, but this is different; it's
342 just using "target remote". Pretend it's just starting up. */
343 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
344 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
345 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
347 current_inferior
->last_resume_kind
= resume_stop
;
348 current_inferior
->last_status
= last_status
;
354 extern int remote_debug
;
356 /* Decode a qXfer read request. Return 0 if everything looks OK,
360 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
362 /* After the read marker and annex, qXfer looks like a
363 traditional 'm' packet. */
364 decode_m_packet (buf
, ofs
, len
);
370 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
372 /* Extract and NUL-terminate the object. */
374 while (*buf
&& *buf
!= ':')
380 /* Extract and NUL-terminate the read/write action. */
382 while (*buf
&& *buf
!= ':')
388 /* Extract and NUL-terminate the annex. */
390 while (*buf
&& *buf
!= ':')
400 /* Write the response to a successful qXfer read. Returns the
401 length of the (binary) data stored in BUF, corresponding
402 to as much of DATA/LEN as we could fit. IS_MORE controls
403 the first character of the response. */
405 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
414 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
418 /* Handle all of the extended 'Q' packets. */
421 handle_general_set (char *own_buf
)
423 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
425 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
426 const char *p
= own_buf
+ strlen ("QPassSignals:");
429 p
= decode_address_to_semicolon (&cursig
, p
);
430 for (i
= 0; i
< numsigs
; i
++)
436 /* Keep looping, to clear the remaining signals. */
439 p
= decode_address_to_semicolon (&cursig
, p
);
444 strcpy (own_buf
, "OK");
448 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
452 fprintf (stderr
, "[noack mode enabled]\n");
461 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
463 char *mode
= own_buf
+ 9;
467 if (strcmp (mode
, "0") == 0)
469 else if (strcmp (mode
, "1") == 0)
473 /* We don't know what this mode is, so complain to
475 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
481 req_str
= req
? "non-stop" : "all-stop";
482 if (start_non_stop (req
) != 0)
484 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
492 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
498 if (target_supports_tracepoints ()
499 && handle_tracepoint_general_set (own_buf
))
502 /* Otherwise we didn't know what packet it was. Say we didn't
508 get_features_xml (const char *annex
)
510 /* gdbserver_xmltarget defines what to return when looking
511 for the "target.xml" file. Its contents can either be
512 verbatim XML code (prefixed with a '@') or else the name
513 of the actual XML file to be used in place of "target.xml".
515 This variable is set up from the auto-generated
516 init_registers_... routine for the current target. */
518 if (gdbserver_xmltarget
519 && strcmp (annex
, "target.xml") == 0)
521 if (*gdbserver_xmltarget
== '@')
522 return gdbserver_xmltarget
+ 1;
524 annex
= gdbserver_xmltarget
;
529 extern const char *const xml_builtin
[][2];
532 /* Look for the annex. */
533 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
534 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
537 if (xml_builtin
[i
][0] != NULL
)
538 return xml_builtin
[i
][1];
546 monitor_show_help (void)
548 monitor_output ("The following monitor commands are supported:\n");
549 monitor_output (" set debug <0|1>\n");
550 monitor_output (" Enable general debugging messages\n");
551 monitor_output (" set debug-hw-points <0|1>\n");
552 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
553 monitor_output (" set remote-debug <0|1>\n");
554 monitor_output (" Enable remote protocol debugging messages\n");
555 monitor_output (" exit\n");
556 monitor_output (" Quit GDBserver\n");
559 /* Read trace frame or inferior memory. */
562 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
566 if (current_traceframe
>= 0)
569 ULONGEST length
= len
;
571 if (traceframe_read_mem (current_traceframe
,
572 memaddr
, myaddr
, len
, &nbytes
))
574 /* Data read from trace buffer, we're done. */
575 if (nbytes
== length
)
577 if (!in_readonly_region (memaddr
, length
))
579 /* Otherwise we have a valid readonly case, fall through. */
580 /* (assume no half-trace half-real blocks for now) */
583 ret
= prepare_to_access_memory ();
586 ret
= read_inferior_memory (memaddr
, myaddr
, len
);
587 done_accessing_memory ();
593 /* Write trace frame or inferior memory. Actually, writing to trace
594 frames is forbidden. */
597 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
599 if (current_traceframe
>= 0)
605 ret
= prepare_to_access_memory ();
608 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
609 done_accessing_memory ();
615 /* Subroutine of handle_search_memory to simplify it. */
618 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
619 gdb_byte
*pattern
, unsigned pattern_len
,
620 gdb_byte
*search_buf
,
621 unsigned chunk_size
, unsigned search_buf_size
,
622 CORE_ADDR
*found_addrp
)
624 /* Prime the search buffer. */
626 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
) != 0)
628 warning ("Unable to access target memory at 0x%lx, halting search.",
633 /* Perform the search.
635 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
636 When we've scanned N bytes we copy the trailing bytes to the start and
637 read in another N bytes. */
639 while (search_space_len
>= pattern_len
)
642 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
646 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
648 if (found_ptr
!= NULL
)
650 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
651 *found_addrp
= found_addr
;
655 /* Not found in this chunk, skip to next chunk. */
657 /* Don't let search_space_len wrap here, it's unsigned. */
658 if (search_space_len
>= chunk_size
)
659 search_space_len
-= chunk_size
;
661 search_space_len
= 0;
663 if (search_space_len
>= pattern_len
)
665 unsigned keep_len
= search_buf_size
- chunk_size
;
666 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
669 /* Copy the trailing part of the previous iteration to the front
670 of the buffer for the next iteration. */
671 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
673 nr_to_read
= (search_space_len
- keep_len
< chunk_size
674 ? search_space_len
- keep_len
677 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
680 warning ("Unable to access target memory "
681 "at 0x%lx, halting search.",
686 start_addr
+= chunk_size
;
695 /* Handle qSearch:memory packets. */
698 handle_search_memory (char *own_buf
, int packet_len
)
700 CORE_ADDR start_addr
;
701 CORE_ADDR search_space_len
;
703 unsigned int pattern_len
;
704 /* NOTE: also defined in find.c testcase. */
705 #define SEARCH_CHUNK_SIZE 16000
706 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
707 /* Buffer to hold memory contents for searching. */
708 gdb_byte
*search_buf
;
709 unsigned search_buf_size
;
711 CORE_ADDR found_addr
;
712 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
714 pattern
= malloc (packet_len
);
717 error ("Unable to allocate memory to perform the search");
718 strcpy (own_buf
, "E00");
721 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
722 packet_len
- cmd_name_len
,
723 &start_addr
, &search_space_len
,
724 pattern
, &pattern_len
) < 0)
727 error ("Error in parsing qSearch:memory packet");
728 strcpy (own_buf
, "E00");
732 search_buf_size
= chunk_size
+ pattern_len
- 1;
734 /* No point in trying to allocate a buffer larger than the search space. */
735 if (search_space_len
< search_buf_size
)
736 search_buf_size
= search_space_len
;
738 search_buf
= malloc (search_buf_size
);
739 if (search_buf
== NULL
)
742 error ("Unable to allocate memory to perform the search");
743 strcpy (own_buf
, "E00");
747 found
= handle_search_memory_1 (start_addr
, search_space_len
,
748 pattern
, pattern_len
,
749 search_buf
, chunk_size
, search_buf_size
,
753 sprintf (own_buf
, "1,%lx", (long) found_addr
);
755 strcpy (own_buf
, "0");
757 strcpy (own_buf
, "E00");
763 #define require_running(BUF) \
764 if (!target_running ()) \
770 /* Handle monitor commands not handled by target-specific handlers. */
773 handle_monitor_command (char *mon
)
775 if (strcmp (mon
, "set debug 1") == 0)
778 monitor_output ("Debug output enabled.\n");
780 else if (strcmp (mon
, "set debug 0") == 0)
783 monitor_output ("Debug output disabled.\n");
785 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
788 monitor_output ("H/W point debugging output enabled.\n");
790 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
793 monitor_output ("H/W point debugging output disabled.\n");
795 else if (strcmp (mon
, "set remote-debug 1") == 0)
798 monitor_output ("Protocol debug output enabled.\n");
800 else if (strcmp (mon
, "set remote-debug 0") == 0)
803 monitor_output ("Protocol debug output disabled.\n");
805 else if (strcmp (mon
, "help") == 0)
806 monitor_show_help ();
807 else if (strcmp (mon
, "exit") == 0)
811 monitor_output ("Unknown monitor command.\n\n");
812 monitor_show_help ();
817 /* Associates a callback with each supported qXfer'able object. */
821 /* The object this handler handles. */
824 /* Request that the target transfer up to LEN 8-bit bytes of the
825 target's OBJECT. The OFFSET, for a seekable object, specifies
826 the starting point. The ANNEX can be used to provide additional
827 data-specific information to the target.
829 Return the number of bytes actually transfered, zero when no
830 further transfer is possible, -1 on error, and -2 when the
831 transfer is not supported. Return of a positive value smaller
832 than LEN does not indicate the end of the object, only the end of
835 One, and only one, of readbuf or writebuf must be non-NULL. */
836 int (*xfer
) (const char *annex
,
837 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
838 ULONGEST offset
, LONGEST len
);
841 /* Handle qXfer:auxv:read. */
844 handle_qxfer_auxv (const char *annex
,
845 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
846 ULONGEST offset
, LONGEST len
)
848 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
851 if (annex
[0] != '\0' || !target_running ())
854 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
857 /* Handle qXfer:features:read. */
860 handle_qxfer_features (const char *annex
,
861 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
862 ULONGEST offset
, LONGEST len
)
864 const char *document
;
867 if (writebuf
!= NULL
)
870 if (!target_running ())
873 /* Grab the correct annex. */
874 document
= get_features_xml (annex
);
875 if (document
== NULL
)
878 total_len
= strlen (document
);
880 if (offset
> total_len
)
883 if (offset
+ len
> total_len
)
884 len
= total_len
- offset
;
886 memcpy (readbuf
, document
+ offset
, len
);
890 /* Handle qXfer:libraries:read. */
893 handle_qxfer_libraries (const char *annex
,
894 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
895 ULONGEST offset
, LONGEST len
)
897 unsigned int total_len
;
899 struct inferior_list_entry
*dll_ptr
;
901 if (writebuf
!= NULL
)
904 if (annex
[0] != '\0' || !target_running ())
907 /* Over-estimate the necessary memory. Assume that every character
908 in the library name must be escaped. */
910 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
911 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
913 document
= malloc (total_len
);
914 if (document
== NULL
)
917 strcpy (document
, "<library-list>\n");
918 p
= document
+ strlen (document
);
920 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
922 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
925 strcpy (p
, " <library name=\"");
927 name
= xml_escape_text (dll
->name
);
931 strcpy (p
, "\"><segment address=\"");
933 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
935 strcpy (p
, "\"/></library>\n");
939 strcpy (p
, "</library-list>\n");
941 total_len
= strlen (document
);
943 if (offset
> total_len
)
949 if (offset
+ len
> total_len
)
950 len
= total_len
- offset
;
952 memcpy (readbuf
, document
+ offset
, len
);
957 /* Handle qXfer:osadata:read. */
960 handle_qxfer_osdata (const char *annex
,
961 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
962 ULONGEST offset
, LONGEST len
)
964 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
967 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
970 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
973 handle_qxfer_siginfo (const char *annex
,
974 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
975 ULONGEST offset
, LONGEST len
)
977 if (the_target
->qxfer_siginfo
== NULL
)
980 if (annex
[0] != '\0' || !target_running ())
983 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
986 /* Handle qXfer:spu:read and qXfer:spu:write. */
989 handle_qxfer_spu (const char *annex
,
990 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
991 ULONGEST offset
, LONGEST len
)
993 if (the_target
->qxfer_spu
== NULL
)
996 if (!target_running ())
999 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1002 /* Handle qXfer:statictrace:read. */
1005 handle_qxfer_statictrace (const char *annex
,
1006 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1007 ULONGEST offset
, LONGEST len
)
1011 if (writebuf
!= NULL
)
1014 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1017 if (traceframe_read_sdata (current_traceframe
, offset
,
1018 readbuf
, len
, &nbytes
))
1023 /* Helper for handle_qxfer_threads. */
1026 handle_qxfer_threads_proper (struct buffer
*buffer
)
1028 struct inferior_list_entry
*thread
;
1030 buffer_grow_str (buffer
, "<threads>\n");
1032 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1034 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1039 write_ptid (ptid_s
, ptid
);
1041 if (the_target
->core_of_thread
)
1042 core
= (*the_target
->core_of_thread
) (ptid
);
1046 sprintf (core_s
, "%d", core
);
1047 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1052 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1057 buffer_grow_str0 (buffer
, "</threads>\n");
1060 /* Handle qXfer:threads:read. */
1063 handle_qxfer_threads (const char *annex
,
1064 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1065 ULONGEST offset
, LONGEST len
)
1067 static char *result
= 0;
1068 static unsigned int result_length
= 0;
1070 if (writebuf
!= NULL
)
1073 if (!target_running () || annex
[0] != '\0')
1078 struct buffer buffer
;
1079 /* When asked for data at offset 0, generate everything and store into
1080 'result'. Successive reads will be served off 'result'. */
1084 buffer_init (&buffer
);
1086 handle_qxfer_threads_proper (&buffer
);
1088 result
= buffer_finish (&buffer
);
1089 result_length
= strlen (result
);
1090 buffer_free (&buffer
);
1093 if (offset
>= result_length
)
1095 /* We're out of data. */
1102 if (len
> result_length
- offset
)
1103 len
= result_length
- offset
;
1105 memcpy (readbuf
, result
+ offset
, len
);
1110 static const struct qxfer qxfer_packets
[] =
1112 { "auxv", handle_qxfer_auxv
},
1113 { "features", handle_qxfer_features
},
1114 { "libraries", handle_qxfer_libraries
},
1115 { "osdata", handle_qxfer_osdata
},
1116 { "siginfo", handle_qxfer_siginfo
},
1117 { "spu", handle_qxfer_spu
},
1118 { "statictrace", handle_qxfer_statictrace
},
1119 { "threads", handle_qxfer_threads
},
1123 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1131 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1134 /* Grab the object, r/w and annex. */
1135 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1137 write_enn (own_buf
);
1142 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1145 const struct qxfer
*q
= &qxfer_packets
[i
];
1147 if (strcmp (object
, q
->object
) == 0)
1149 if (strcmp (rw
, "read") == 0)
1151 unsigned char *data
;
1156 /* Grab the offset and length. */
1157 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1159 write_enn (own_buf
);
1163 /* Read one extra byte, as an indicator of whether there is
1165 if (len
> PBUFSIZ
- 2)
1167 data
= malloc (len
+ 1);
1170 write_enn (own_buf
);
1173 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1180 write_enn (own_buf
);
1182 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1184 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1189 else if (strcmp (rw
, "write") == 0)
1194 unsigned char *data
;
1196 strcpy (own_buf
, "E00");
1197 data
= malloc (packet_len
- (offset
- own_buf
));
1200 write_enn (own_buf
);
1203 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1204 &ofs
, &len
, data
) < 0)
1207 write_enn (own_buf
);
1211 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1218 write_enn (own_buf
);
1220 sprintf (own_buf
, "%x", n
);
1233 /* Table used by the crc32 function to calcuate the checksum. */
1235 static unsigned int crc32_table
[256] =
1238 /* Compute 32 bit CRC from inferior memory.
1240 On success, return 32 bit CRC.
1241 On failure, return (unsigned long long) -1. */
1243 static unsigned long long
1244 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1246 if (!crc32_table
[1])
1248 /* Initialize the CRC table and the decoding table. */
1252 for (i
= 0; i
< 256; i
++)
1254 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1255 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1262 unsigned char byte
= 0;
1264 /* Return failure if memory read fails. */
1265 if (read_inferior_memory (base
, &byte
, 1) != 0)
1266 return (unsigned long long) -1;
1268 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1271 return (unsigned long long) crc
;
1274 /* Handle all of the extended 'q' packets. */
1277 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1279 static struct inferior_list_entry
*thread_ptr
;
1281 /* Reply the current thread id. */
1282 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1285 require_running (own_buf
);
1287 if (!ptid_equal (general_thread
, null_ptid
)
1288 && !ptid_equal (general_thread
, minus_one_ptid
))
1289 gdb_id
= general_thread
;
1292 thread_ptr
= all_threads
.head
;
1293 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1296 sprintf (own_buf
, "QC");
1298 own_buf
= write_ptid (own_buf
, gdb_id
);
1302 if (strcmp ("qSymbol::", own_buf
) == 0)
1304 /* GDB is suggesting new symbols have been loaded. This may
1305 mean a new shared library has been detected as loaded, so
1306 take the opportunity to check if breakpoints we think are
1307 inserted, still are. Note that it isn't guaranteed that
1308 we'll see this when a shared library is loaded, and nor will
1309 we see this for unloads (although breakpoints in unloaded
1310 libraries shouldn't trigger), as GDB may not find symbols for
1311 the library at all. We also re-validate breakpoints when we
1312 see a second GDB breakpoint for the same address, and or when
1313 we access breakpoint shadows. */
1314 validate_breakpoints ();
1316 if (target_supports_tracepoints ())
1317 tracepoint_look_up_symbols ();
1319 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1320 (*the_target
->look_up_symbols
) ();
1322 strcpy (own_buf
, "OK");
1326 if (!disable_packet_qfThreadInfo
)
1328 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1332 require_running (own_buf
);
1333 thread_ptr
= all_threads
.head
;
1336 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1337 write_ptid (own_buf
, gdb_id
);
1338 thread_ptr
= thread_ptr
->next
;
1342 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1346 require_running (own_buf
);
1347 if (thread_ptr
!= NULL
)
1350 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1351 write_ptid (own_buf
, gdb_id
);
1352 thread_ptr
= thread_ptr
->next
;
1357 sprintf (own_buf
, "l");
1363 if (the_target
->read_offsets
!= NULL
1364 && strcmp ("qOffsets", own_buf
) == 0)
1366 CORE_ADDR text
, data
;
1368 require_running (own_buf
);
1369 if (the_target
->read_offsets (&text
, &data
))
1370 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1371 (long)text
, (long)data
, (long)data
);
1373 write_enn (own_buf
);
1378 /* Protocol features query. */
1379 if (strncmp ("qSupported", own_buf
, 10) == 0
1380 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1382 char *p
= &own_buf
[10];
1383 int gdb_supports_qRelocInsn
= 0;
1385 /* Start processing qSupported packet. */
1386 target_process_qsupported (NULL
);
1388 /* Process each feature being provided by GDB. The first
1389 feature will follow a ':', and latter features will follow
1393 char **qsupported
= NULL
;
1397 /* Two passes, to avoid nested strtok calls in
1398 target_process_qsupported. */
1399 for (p
= strtok (p
+ 1, ";");
1401 p
= strtok (NULL
, ";"))
1404 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1405 qsupported
[count
- 1] = xstrdup (p
);
1408 for (i
= 0; i
< count
; i
++)
1411 if (strcmp (p
, "multiprocess+") == 0)
1413 /* GDB supports and wants multi-process support if
1415 if (target_supports_multi_process ())
1418 else if (strcmp (p
, "qRelocInsn+") == 0)
1420 /* GDB supports relocate instruction requests. */
1421 gdb_supports_qRelocInsn
= 1;
1424 target_process_qsupported (p
);
1432 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1434 /* We do not have any hook to indicate whether the target backend
1435 supports qXfer:libraries:read, so always report it. */
1436 strcat (own_buf
, ";qXfer:libraries:read+");
1438 if (the_target
->read_auxv
!= NULL
)
1439 strcat (own_buf
, ";qXfer:auxv:read+");
1441 if (the_target
->qxfer_spu
!= NULL
)
1442 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1444 if (the_target
->qxfer_siginfo
!= NULL
)
1445 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1447 /* We always report qXfer:features:read, as targets may
1448 install XML files on a subsequent call to arch_setup.
1449 If we reported to GDB on startup that we don't support
1450 qXfer:feature:read at all, we will never be re-queried. */
1451 strcat (own_buf
, ";qXfer:features:read+");
1453 if (transport_is_reliable
)
1454 strcat (own_buf
, ";QStartNoAckMode+");
1456 if (the_target
->qxfer_osdata
!= NULL
)
1457 strcat (own_buf
, ";qXfer:osdata:read+");
1459 if (target_supports_multi_process ())
1460 strcat (own_buf
, ";multiprocess+");
1462 if (target_supports_non_stop ())
1463 strcat (own_buf
, ";QNonStop+");
1465 strcat (own_buf
, ";qXfer:threads:read+");
1467 if (target_supports_tracepoints ())
1469 strcat (own_buf
, ";ConditionalTracepoints+");
1470 strcat (own_buf
, ";TraceStateVariables+");
1471 strcat (own_buf
, ";TracepointSource+");
1472 strcat (own_buf
, ";DisconnectedTracing+");
1473 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1474 strcat (own_buf
, ";FastTracepoints+");
1475 strcat (own_buf
, ";StaticTracepoints+");
1476 strcat (own_buf
, ";qXfer:statictrace:read+");
1482 /* Thread-local storage support. */
1483 if (the_target
->get_tls_address
!= NULL
1484 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1486 char *p
= own_buf
+ 12;
1487 CORE_ADDR parts
[2], address
= 0;
1489 ptid_t ptid
= null_ptid
;
1491 require_running (own_buf
);
1493 for (i
= 0; i
< 3; i
++)
1501 p2
= strchr (p
, ',');
1514 ptid
= read_ptid (p
, NULL
);
1516 decode_address (&parts
[i
- 1], p
, len
);
1520 if (p
!= NULL
|| i
< 3)
1524 struct thread_info
*thread
= find_thread_ptid (ptid
);
1529 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1535 strcpy (own_buf
, paddress(address
));
1540 write_enn (own_buf
);
1544 /* Otherwise, pretend we do not understand this packet. */
1547 /* Windows OS Thread Information Block address support. */
1548 if (the_target
->get_tib_address
!= NULL
1549 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1554 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1556 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1559 strcpy (own_buf
, paddress(tlb
));
1564 write_enn (own_buf
);
1570 /* Handle "monitor" commands. */
1571 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1573 char *mon
= malloc (PBUFSIZ
);
1574 int len
= strlen (own_buf
+ 6);
1578 write_enn (own_buf
);
1582 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1584 write_enn (own_buf
);
1588 mon
[len
/ 2] = '\0';
1592 if (the_target
->handle_monitor_command
== NULL
1593 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1594 /* Default processing. */
1595 handle_monitor_command (mon
);
1601 if (strncmp ("qSearch:memory:", own_buf
,
1602 sizeof ("qSearch:memory:") - 1) == 0)
1604 require_running (own_buf
);
1605 handle_search_memory (own_buf
, packet_len
);
1609 if (strcmp (own_buf
, "qAttached") == 0
1610 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1612 struct process_info
*process
;
1614 if (own_buf
[sizeof ("qAttached") - 1])
1616 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1617 process
= (struct process_info
*)
1618 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1622 require_running (own_buf
);
1623 process
= current_process ();
1626 if (process
== NULL
)
1628 write_enn (own_buf
);
1632 strcpy (own_buf
, process
->attached
? "1" : "0");
1636 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1638 /* CRC check (compare-section). */
1642 unsigned long long crc
;
1644 require_running (own_buf
);
1645 base
= strtoul (own_buf
+ 5, &comma
, 16);
1646 if (*comma
++ != ',')
1648 write_enn (own_buf
);
1651 len
= strtoul (comma
, NULL
, 16);
1652 crc
= crc32 (base
, len
, 0xffffffff);
1653 /* Check for memory failure. */
1654 if (crc
== (unsigned long long) -1)
1656 write_enn (own_buf
);
1659 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1663 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1666 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1669 /* Otherwise we didn't know what packet it was. Say we didn't
1674 static void gdb_wants_all_threads_stopped (void);
1676 /* Parse vCont packets. */
1678 handle_v_cont (char *own_buf
)
1682 struct thread_resume
*resume_info
;
1683 struct thread_resume default_action
= {{0}};
1685 /* Count the number of semicolons in the packet. There should be one
1686 for every action. */
1692 p
= strchr (p
, ';');
1695 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1696 if (resume_info
== NULL
)
1704 if (p
[0] == 's' || p
[0] == 'S')
1705 resume_info
[i
].kind
= resume_step
;
1706 else if (p
[0] == 'c' || p
[0] == 'C')
1707 resume_info
[i
].kind
= resume_continue
;
1708 else if (p
[0] == 't')
1709 resume_info
[i
].kind
= resume_stop
;
1713 if (p
[0] == 'S' || p
[0] == 'C')
1716 sig
= strtol (p
+ 1, &q
, 16);
1721 if (!target_signal_to_host_p (sig
))
1723 resume_info
[i
].sig
= target_signal_to_host (sig
);
1727 resume_info
[i
].sig
= 0;
1733 resume_info
[i
].thread
= minus_one_ptid
;
1734 default_action
= resume_info
[i
];
1736 /* Note: we don't increment i here, we'll overwrite this entry
1737 the next time through. */
1739 else if (p
[0] == ':')
1741 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1746 if (p
[0] != ';' && p
[0] != 0)
1749 resume_info
[i
].thread
= ptid
;
1756 resume_info
[i
] = default_action
;
1758 /* Still used in occasional places in the backend. */
1760 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1761 && resume_info
[0].kind
!= resume_stop
)
1762 cont_thread
= resume_info
[0].thread
;
1764 cont_thread
= minus_one_ptid
;
1765 set_desired_inferior (0);
1770 (*the_target
->resume
) (resume_info
, n
);
1778 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1780 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1781 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1782 current_inferior
->last_status
= last_status
;
1784 /* From the client's perspective, all-stop mode always stops all
1785 threads implicitly (and the target backend has already done
1786 so by now). Tag all threads as "want-stopped", so we don't
1787 resume them implicitly without the client telling us to. */
1788 gdb_wants_all_threads_stopped ();
1789 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1790 disable_async_io ();
1792 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1793 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1794 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1799 write_enn (own_buf
);
1804 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1806 handle_v_attach (char *own_buf
)
1810 pid
= strtol (own_buf
+ 8, NULL
, 16);
1811 if (pid
!= 0 && attach_inferior (pid
) == 0)
1813 /* Don't report shared library events after attaching, even if
1814 some libraries are preloaded. GDB will always poll the
1815 library list. Avoids the "stopped by shared library event"
1816 notice on the GDB side. */
1821 /* In non-stop, we don't send a resume reply. Stop events
1822 will follow up using the normal notification
1827 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1833 write_enn (own_buf
);
1838 /* Run a new program. Return 1 if successful, 0 if failure. */
1840 handle_v_run (char *own_buf
)
1842 char *p
, *next_p
, **new_argv
;
1846 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1852 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1853 if (new_argv
== NULL
)
1855 write_enn (own_buf
);
1860 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1862 next_p
= strchr (p
, ';');
1864 next_p
= p
+ strlen (p
);
1866 if (i
== 0 && p
== next_p
)
1870 /* FIXME: Fail request if out of memory instead of dying. */
1871 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1872 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1873 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1882 if (new_argv
[0] == NULL
)
1884 /* GDB didn't specify a program to run. Use the program from the
1885 last run with the new argument list. */
1887 if (program_argv
== NULL
)
1889 /* FIXME: new_argv memory leak */
1890 write_enn (own_buf
);
1894 new_argv
[0] = strdup (program_argv
[0]);
1895 if (new_argv
[0] == NULL
)
1897 /* FIXME: new_argv memory leak */
1898 write_enn (own_buf
);
1903 /* Free the old argv and install the new one. */
1904 freeargv (program_argv
);
1905 program_argv
= new_argv
;
1907 start_inferior (program_argv
);
1908 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1910 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1912 /* In non-stop, sending a resume reply doesn't set the general
1913 thread, but GDB assumes a vRun sets it (this is so GDB can
1914 query which is the main thread of the new inferior. */
1916 general_thread
= last_ptid
;
1922 write_enn (own_buf
);
1927 /* Kill process. Return 1 if successful, 0 if failure. */
1929 handle_v_kill (char *own_buf
)
1932 char *p
= &own_buf
[6];
1934 pid
= strtol (p
, NULL
, 16);
1937 if (pid
!= 0 && kill_inferior (pid
) == 0)
1939 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1940 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1941 last_ptid
= pid_to_ptid (pid
);
1942 discard_queued_stop_replies (pid
);
1948 write_enn (own_buf
);
1953 /* Handle a 'vStopped' packet. */
1955 handle_v_stopped (char *own_buf
)
1957 /* If we're waiting for GDB to acknowledge a pending stop reply,
1958 consider that done. */
1961 struct vstop_notif
*head
;
1964 fprintf (stderr
, "vStopped: acking %s\n",
1965 target_pid_to_str (notif_queue
->ptid
));
1968 notif_queue
= notif_queue
->next
;
1972 /* Push another stop reply, or if there are no more left, an OK. */
1973 send_next_stop_reply (own_buf
);
1976 /* Handle all of the extended 'v' packets. */
1978 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
1980 if (!disable_packet_vCont
)
1982 if (strncmp (own_buf
, "vCont;", 6) == 0)
1984 require_running (own_buf
);
1985 handle_v_cont (own_buf
);
1989 if (strncmp (own_buf
, "vCont?", 6) == 0)
1991 strcpy (own_buf
, "vCont;c;C;s;S;t");
1996 if (strncmp (own_buf
, "vFile:", 6) == 0
1997 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2000 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2002 if (!multi_process
&& target_running ())
2004 fprintf (stderr
, "Already debugging a process\n");
2005 write_enn (own_buf
);
2008 handle_v_attach (own_buf
);
2012 if (strncmp (own_buf
, "vRun;", 5) == 0)
2014 if (!multi_process
&& target_running ())
2016 fprintf (stderr
, "Already debugging a process\n");
2017 write_enn (own_buf
);
2020 handle_v_run (own_buf
);
2024 if (strncmp (own_buf
, "vKill;", 6) == 0)
2026 if (!target_running ())
2028 fprintf (stderr
, "No process to kill\n");
2029 write_enn (own_buf
);
2032 handle_v_kill (own_buf
);
2036 if (strncmp (own_buf
, "vStopped", 8) == 0)
2038 handle_v_stopped (own_buf
);
2042 /* Otherwise we didn't know what packet it was. Say we didn't
2048 /* Resume inferior and wait for another event. In non-stop mode,
2049 don't really wait here, but return immediatelly to the event
2052 myresume (char *own_buf
, int step
, int sig
)
2054 struct thread_resume resume_info
[2];
2056 int valid_cont_thread
;
2058 set_desired_inferior (0);
2060 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2061 && !ptid_equal (cont_thread
, minus_one_ptid
));
2063 if (step
|| sig
|| valid_cont_thread
)
2065 resume_info
[0].thread
2066 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2068 resume_info
[0].kind
= resume_step
;
2070 resume_info
[0].kind
= resume_continue
;
2071 resume_info
[0].sig
= sig
;
2075 if (!valid_cont_thread
)
2077 resume_info
[n
].thread
= minus_one_ptid
;
2078 resume_info
[n
].kind
= resume_continue
;
2079 resume_info
[n
].sig
= 0;
2086 (*the_target
->resume
) (resume_info
, n
);
2092 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2094 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2095 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2097 current_inferior
->last_resume_kind
= resume_stop
;
2098 current_inferior
->last_status
= last_status
;
2101 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2102 disable_async_io ();
2104 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2105 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2106 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2110 /* Callback for for_each_inferior. Make a new stop reply for each
2114 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2116 struct thread_info
*thread
= (struct thread_info
*) entry
;
2118 /* For now, assume targets that don't have this callback also don't
2119 manage the thread's last_status field. */
2120 if (the_target
->thread_stopped
== NULL
)
2122 /* Pass the last stop reply back to GDB, but don't notify
2124 queue_stop_reply (entry
->id
, &thread
->last_status
);
2128 if (thread_stopped (thread
))
2132 "Reporting thread %s as already stopped with %s\n",
2133 target_pid_to_str (entry
->id
),
2134 target_waitstatus_to_string (&thread
->last_status
));
2136 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2138 /* Pass the last stop reply back to GDB, but don't notify
2140 queue_stop_reply (entry
->id
, &thread
->last_status
);
2147 /* Set this inferior threads's state as "want-stopped". We won't
2148 resume this thread until the client gives us another action for
2152 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2154 struct thread_info
*thread
= (struct thread_info
*) entry
;
2156 thread
->last_resume_kind
= resume_stop
;
2158 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2160 /* Most threads are stopped implicitly (all-stop); tag that with
2162 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2163 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2167 /* Set all threads' states as "want-stopped". */
2170 gdb_wants_all_threads_stopped (void)
2172 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2175 /* Clear the gdb_detached flag of every process. */
2178 gdb_reattached_process (struct inferior_list_entry
*entry
)
2180 struct process_info
*process
= (struct process_info
*) entry
;
2182 process
->gdb_detached
= 0;
2185 /* Status handler for the '?' packet. */
2188 handle_status (char *own_buf
)
2190 /* GDB is connected, don't forward events to the target anymore. */
2191 for_each_inferior (&all_processes
, gdb_reattached_process
);
2193 /* In non-stop mode, we must send a stop reply for each stopped
2194 thread. In all-stop mode, just send one for the first stopped
2199 discard_queued_stop_replies (-1);
2200 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2202 /* The first is sent immediatly. OK is sent if there is no
2203 stopped thread, which is the same handling of the vStopped
2204 packet (by design). */
2205 send_next_stop_reply (own_buf
);
2210 stabilize_threads ();
2211 gdb_wants_all_threads_stopped ();
2213 if (all_threads
.head
)
2215 struct target_waitstatus status
;
2217 status
.kind
= TARGET_WAITKIND_STOPPED
;
2218 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2219 prepare_resume_reply (own_buf
,
2220 all_threads
.head
->id
, &status
);
2223 strcpy (own_buf
, "W00");
2228 gdbserver_version (void)
2230 printf ("GNU gdbserver %s%s\n"
2231 "Copyright (C) 2011 Free Software Foundation, Inc.\n"
2232 "gdbserver is free software, covered by the "
2233 "GNU General Public License.\n"
2234 "This gdbserver was configured as \"%s\"\n",
2235 PKGVERSION
, version
, host_name
);
2239 gdbserver_usage (FILE *stream
)
2241 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2242 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2243 "\tgdbserver [OPTIONS] --multi COMM\n"
2245 "COMM may either be a tty device (for serial debugging), or \n"
2246 "HOST:PORT to listen for a TCP connection.\n"
2249 " --debug Enable general debugging output.\n"
2250 " --remote-debug Enable remote protocol debugging output.\n"
2251 " --version Display version information and exit.\n"
2252 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2253 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2254 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2258 gdbserver_show_disableable (FILE *stream
)
2260 fprintf (stream
, "Disableable packets:\n"
2261 " vCont \tAll vCont packets\n"
2262 " qC \tQuerying the current thread\n"
2263 " qfThreadInfo\tThread listing\n"
2264 " Tthread \tPassing the thread specifier in the "
2265 "T stop reply packet\n"
2266 " threads \tAll of the above\n");
2270 #undef require_running
2271 #define require_running(BUF) \
2272 if (!target_running ()) \
2279 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2281 int pid
= * (int *) args
;
2283 if (ptid_get_pid (entry
->id
) == pid
)
2290 kill_inferior_callback (struct inferior_list_entry
*entry
)
2292 struct process_info
*process
= (struct process_info
*) entry
;
2293 int pid
= ptid_get_pid (process
->head
.id
);
2295 kill_inferior (pid
);
2296 discard_queued_stop_replies (pid
);
2299 /* Callback for for_each_inferior to detach or kill the inferior,
2300 depending on whether we attached to it or not.
2301 We inform the user whether we're detaching or killing the process
2302 as this is only called when gdbserver is about to exit. */
2305 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2307 struct process_info
*process
= (struct process_info
*) entry
;
2308 int pid
= ptid_get_pid (process
->head
.id
);
2310 if (process
->attached
)
2311 detach_inferior (pid
);
2313 kill_inferior (pid
);
2315 discard_queued_stop_replies (pid
);
2318 /* for_each_inferior callback for detach_or_kill_for_exit to print
2319 the pids of started inferiors. */
2322 print_started_pid (struct inferior_list_entry
*entry
)
2324 struct process_info
*process
= (struct process_info
*) entry
;
2326 if (! process
->attached
)
2328 int pid
= ptid_get_pid (process
->head
.id
);
2329 fprintf (stderr
, " %d", pid
);
2333 /* for_each_inferior callback for detach_or_kill_for_exit to print
2334 the pids of attached inferiors. */
2337 print_attached_pid (struct inferior_list_entry
*entry
)
2339 struct process_info
*process
= (struct process_info
*) entry
;
2341 if (process
->attached
)
2343 int pid
= ptid_get_pid (process
->head
.id
);
2344 fprintf (stderr
, " %d", pid
);
2348 /* Call this when exiting gdbserver with possible inferiors that need
2349 to be killed or detached from. */
2352 detach_or_kill_for_exit (void)
2354 /* First print a list of the inferiors we will be killing/detaching.
2355 This is to assist the user, for example, in case the inferior unexpectedly
2356 dies after we exit: did we screw up or did the inferior exit on its own?
2357 Having this info will save some head-scratching. */
2359 if (have_started_inferiors_p ())
2361 fprintf (stderr
, "Killing process(es):");
2362 for_each_inferior (&all_processes
, print_started_pid
);
2363 fprintf (stderr
, "\n");
2365 if (have_attached_inferiors_p ())
2367 fprintf (stderr
, "Detaching process(es):");
2368 for_each_inferior (&all_processes
, print_attached_pid
);
2369 fprintf (stderr
, "\n");
2372 /* Now we can kill or detach the inferiors. */
2374 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2378 join_inferiors_callback (struct inferior_list_entry
*entry
)
2380 struct process_info
*process
= (struct process_info
*) entry
;
2382 /* If we are attached, then we can exit. Otherwise, we need to hang
2383 around doing nothing, until the child is gone. */
2384 if (!process
->attached
)
2385 join_inferior (ptid_get_pid (process
->head
.id
));
2389 main (int argc
, char *argv
[])
2393 char *arg_end
, *port
;
2394 char **next_arg
= &argv
[1];
2399 while (*next_arg
!= NULL
&& **next_arg
== '-')
2401 if (strcmp (*next_arg
, "--version") == 0)
2403 gdbserver_version ();
2406 else if (strcmp (*next_arg
, "--help") == 0)
2408 gdbserver_usage (stdout
);
2411 else if (strcmp (*next_arg
, "--attach") == 0)
2413 else if (strcmp (*next_arg
, "--multi") == 0)
2415 else if (strcmp (*next_arg
, "--wrapper") == 0)
2419 wrapper_argv
= next_arg
;
2420 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2423 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2425 gdbserver_usage (stderr
);
2429 /* Consume the "--". */
2432 else if (strcmp (*next_arg
, "--debug") == 0)
2434 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2436 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2438 gdbserver_show_disableable (stdout
);
2441 else if (strncmp (*next_arg
,
2442 "--disable-packet=",
2443 sizeof ("--disable-packet=") - 1) == 0)
2445 char *packets
, *tok
;
2447 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2448 for (tok
= strtok (packets
, ",");
2450 tok
= strtok (NULL
, ","))
2452 if (strcmp ("vCont", tok
) == 0)
2453 disable_packet_vCont
= 1;
2454 else if (strcmp ("Tthread", tok
) == 0)
2455 disable_packet_Tthread
= 1;
2456 else if (strcmp ("qC", tok
) == 0)
2457 disable_packet_qC
= 1;
2458 else if (strcmp ("qfThreadInfo", tok
) == 0)
2459 disable_packet_qfThreadInfo
= 1;
2460 else if (strcmp ("threads", tok
) == 0)
2462 disable_packet_vCont
= 1;
2463 disable_packet_Tthread
= 1;
2464 disable_packet_qC
= 1;
2465 disable_packet_qfThreadInfo
= 1;
2469 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2471 gdbserver_show_disableable (stderr
);
2478 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2486 if (setjmp (toplevel
))
2488 fprintf (stderr
, "Exiting\n");
2494 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2496 gdbserver_usage (stderr
);
2503 /* --attach used to come after PORT, so allow it there for
2505 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2512 && (*next_arg
== NULL
2513 || (*next_arg
)[0] == '\0'
2514 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2516 || next_arg
[1] != NULL
))
2521 gdbserver_usage (stderr
);
2525 initialize_inferiors ();
2526 initialize_async_io ();
2528 if (target_supports_tracepoints ())
2529 initialize_tracepoint ();
2531 own_buf
= xmalloc (PBUFSIZ
+ 1);
2532 mem_buf
= xmalloc (PBUFSIZ
);
2534 if (pid
== 0 && *next_arg
!= NULL
)
2538 n
= argc
- (next_arg
- argv
);
2539 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2540 for (i
= 0; i
< n
; i
++)
2541 program_argv
[i
] = xstrdup (next_arg
[i
]);
2542 program_argv
[i
] = NULL
;
2544 /* Wait till we are at first instruction in program. */
2545 start_inferior (program_argv
);
2547 /* We are now (hopefully) stopped at the first instruction of
2548 the target process. This assumes that the target process was
2549 successfully created. */
2553 if (attach_inferior (pid
) == -1)
2554 error ("Attaching not supported on this target");
2556 /* Otherwise succeeded. */
2560 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2561 last_status
.value
.integer
= 0;
2562 last_ptid
= minus_one_ptid
;
2565 /* Don't report shared library events on the initial connection,
2566 even if some libraries are preloaded. Avoids the "stopped by
2567 shared library event" notice on gdb side. */
2570 if (setjmp (toplevel
))
2572 detach_or_kill_for_exit ();
2576 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2577 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2582 if (!was_running
&& !multi_mode
)
2584 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2592 /* Be sure we're out of tfind mode. */
2593 current_traceframe
= -1;
2597 if (setjmp (toplevel
) != 0)
2599 /* An error occurred. */
2600 if (response_needed
)
2602 write_enn (own_buf
);
2607 /* Wait for events. This will return when all event sources are
2608 removed from the event loop. */
2609 start_event_loop ();
2611 /* If an exit was requested (using the "monitor exit" command),
2612 terminate now. The only other way to get here is for
2613 getpkt to fail; close the connection and reopen it at the
2618 detach_or_kill_for_exit ();
2623 "Remote side has terminated connection. "
2624 "GDBserver will reopen the connection.\n");
2628 if (disconnected_tracing
)
2630 /* Try to enable non-stop/async mode, so we we can both
2631 wait for an async socket accept, and handle async
2632 target events simultaneously. There's also no point
2633 either in having the target always stop all threads,
2634 when we're going to pass signals down without
2638 if (start_non_stop (1))
2641 /* Detaching implicitly resumes all threads; simply
2642 disconnecting does not. */
2648 "Disconnected tracing disabled; stopping trace run.\n");
2655 /* Event loop callback that handles a serial event. The first byte in
2656 the serial buffer gets us here. We expect characters to arrive at
2657 a brisk pace, so we read the rest of the packet with a blocking
2661 process_serial_event (void)
2671 int new_packet_len
= -1;
2673 /* Used to decide when gdbserver should exit in
2674 multi-mode/remote. */
2675 static int have_ran
= 0;
2678 have_ran
= target_running ();
2680 disable_async_io ();
2682 response_needed
= 0;
2683 packet_len
= getpkt (own_buf
);
2684 if (packet_len
<= 0)
2687 /* Force an event loop break. */
2690 response_needed
= 1;
2697 handle_query (own_buf
, packet_len
, &new_packet_len
);
2700 handle_general_set (own_buf
);
2703 require_running (own_buf
);
2708 pid
= strtol (&own_buf
[i
], NULL
, 16);
2712 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2714 if (tracing
&& disconnected_tracing
)
2716 struct thread_resume resume_info
;
2717 struct process_info
*process
= find_process_pid (pid
);
2719 if (process
== NULL
)
2721 write_enn (own_buf
);
2726 "Disconnected tracing in effect, "
2727 "leaving gdbserver attached to the process\n");
2729 /* Make sure we're in non-stop/async mode, so we we can both
2730 wait for an async socket accept, and handle async target
2731 events simultaneously. There's also no point either in
2732 having the target stop all threads, when we're going to
2733 pass signals down without informing GDB. */
2737 fprintf (stderr
, "Forcing non-stop mode\n");
2743 process
->gdb_detached
= 1;
2745 /* Detaching implicitly resumes all threads. */
2746 resume_info
.thread
= minus_one_ptid
;
2747 resume_info
.kind
= resume_continue
;
2748 resume_info
.sig
= 0;
2749 (*the_target
->resume
) (&resume_info
, 1);
2752 break; /* from switch/case */
2755 fprintf (stderr
, "Detaching from process %d\n", pid
);
2757 if (detach_inferior (pid
) != 0)
2758 write_enn (own_buf
);
2761 discard_queued_stop_replies (pid
);
2764 if (extended_protocol
)
2766 /* Treat this like a normal program exit. */
2767 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2768 last_status
.value
.integer
= 0;
2769 last_ptid
= pid_to_ptid (pid
);
2771 current_inferior
= NULL
;
2778 /* If we are attached, then we can exit. Otherwise, we
2779 need to hang around doing nothing, until the child is
2781 for_each_inferior (&all_processes
,
2782 join_inferiors_callback
);
2788 extended_protocol
= 1;
2792 handle_status (own_buf
);
2795 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2797 ptid_t gdb_id
, thread_id
;
2800 require_running (own_buf
);
2802 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2804 pid
= ptid_get_pid (gdb_id
);
2806 if (ptid_equal (gdb_id
, null_ptid
)
2807 || ptid_equal (gdb_id
, minus_one_ptid
))
2808 thread_id
= null_ptid
;
2810 && ptid_equal (pid_to_ptid (pid
),
2813 struct thread_info
*thread
=
2814 (struct thread_info
*) find_inferior (&all_threads
,
2819 write_enn (own_buf
);
2823 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2827 thread_id
= gdb_id_to_thread_id (gdb_id
);
2828 if (ptid_equal (thread_id
, null_ptid
))
2830 write_enn (own_buf
);
2835 if (own_buf
[1] == 'g')
2837 if (ptid_equal (thread_id
, null_ptid
))
2839 /* GDB is telling us to choose any thread. Check if
2840 the currently selected thread is still valid. If
2841 it is not, select the first available. */
2842 struct thread_info
*thread
=
2843 (struct thread_info
*) find_inferior_id (&all_threads
,
2846 thread_id
= all_threads
.head
->id
;
2849 general_thread
= thread_id
;
2850 set_desired_inferior (1);
2852 else if (own_buf
[1] == 'c')
2853 cont_thread
= thread_id
;
2854 else if (own_buf
[1] == 's')
2855 step_thread
= thread_id
;
2861 /* Silently ignore it so that gdb can extend the protocol
2862 without compatibility headaches. */
2867 require_running (own_buf
);
2868 if (current_traceframe
>= 0)
2870 struct regcache
*regcache
= new_register_cache ();
2872 if (fetch_traceframe_registers (current_traceframe
,
2874 registers_to_string (regcache
, own_buf
);
2876 write_enn (own_buf
);
2877 free_register_cache (regcache
);
2881 struct regcache
*regcache
;
2883 set_desired_inferior (1);
2884 regcache
= get_thread_regcache (current_inferior
, 1);
2885 registers_to_string (regcache
, own_buf
);
2889 require_running (own_buf
);
2890 if (current_traceframe
>= 0)
2891 write_enn (own_buf
);
2894 struct regcache
*regcache
;
2896 set_desired_inferior (1);
2897 regcache
= get_thread_regcache (current_inferior
, 1);
2898 registers_from_string (regcache
, &own_buf
[1]);
2903 require_running (own_buf
);
2904 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2905 if (gdb_read_memory (mem_addr
, mem_buf
, len
) == 0)
2906 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2908 write_enn (own_buf
);
2911 require_running (own_buf
);
2912 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2913 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2916 write_enn (own_buf
);
2919 require_running (own_buf
);
2920 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2921 &mem_addr
, &len
, &mem_buf
) < 0
2922 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2923 write_enn (own_buf
);
2928 require_running (own_buf
);
2929 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2930 if (target_signal_to_host_p (sig
))
2931 signal
= target_signal_to_host (sig
);
2934 myresume (own_buf
, 0, signal
);
2937 require_running (own_buf
);
2938 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2939 if (target_signal_to_host_p (sig
))
2940 signal
= target_signal_to_host (sig
);
2943 myresume (own_buf
, 1, signal
);
2946 require_running (own_buf
);
2948 myresume (own_buf
, 0, signal
);
2951 require_running (own_buf
);
2953 myresume (own_buf
, 1, signal
);
2955 case 'Z': /* insert_ ... */
2957 case 'z': /* remove_ ... */
2961 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
2962 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
2963 char type
= own_buf
[1];
2965 const int insert
= ch
== 'Z';
2967 /* Default to unrecognized/unsupported. */
2971 case '0': /* software-breakpoint */
2972 case '1': /* hardware-breakpoint */
2973 case '2': /* write watchpoint */
2974 case '3': /* read watchpoint */
2975 case '4': /* access watchpoint */
2976 require_running (own_buf
);
2977 if (insert
&& the_target
->insert_point
!= NULL
)
2978 res
= (*the_target
->insert_point
) (type
, addr
, len
);
2979 else if (!insert
&& the_target
->remove_point
!= NULL
)
2980 res
= (*the_target
->remove_point
) (type
, addr
, len
);
2992 write_enn (own_buf
);
2996 response_needed
= 0;
2997 if (!target_running ())
2998 /* The packet we received doesn't make sense - but we can't
2999 reply to it, either. */
3002 fprintf (stderr
, "Killing all inferiors\n");
3003 for_each_inferior (&all_processes
, kill_inferior_callback
);
3005 /* When using the extended protocol, we wait with no program
3006 running. The traditional protocol will exit instead. */
3007 if (extended_protocol
)
3009 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3010 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3018 ptid_t gdb_id
, thread_id
;
3020 require_running (own_buf
);
3022 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3023 thread_id
= gdb_id_to_thread_id (gdb_id
);
3024 if (ptid_equal (thread_id
, null_ptid
))
3026 write_enn (own_buf
);
3030 if (mythread_alive (thread_id
))
3033 write_enn (own_buf
);
3037 response_needed
= 0;
3039 /* Restarting the inferior is only supported in the extended
3041 if (extended_protocol
)
3043 if (target_running ())
3044 for_each_inferior (&all_processes
,
3045 kill_inferior_callback
);
3046 fprintf (stderr
, "GDBserver restarting\n");
3048 /* Wait till we are at 1st instruction in prog. */
3049 if (program_argv
!= NULL
)
3050 start_inferior (program_argv
);
3053 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3054 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3060 /* It is a request we don't understand. Respond with an
3061 empty packet so that gdb knows that we don't support this
3067 /* Extended (long) request. */
3068 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3072 /* It is a request we don't understand. Respond with an empty
3073 packet so that gdb knows that we don't support this
3079 if (new_packet_len
!= -1)
3080 putpkt_binary (own_buf
, new_packet_len
);
3084 response_needed
= 0;
3086 if (!extended_protocol
&& have_ran
&& !target_running ())
3088 /* In non-stop, defer exiting until GDB had a chance to query
3089 the whole vStopped list (until it gets an OK). */
3092 fprintf (stderr
, "GDBserver exiting\n");
3104 /* Event-loop callback for serial events. */
3107 handle_serial_event (int err
, gdb_client_data client_data
)
3110 fprintf (stderr
, "handling possible serial event\n");
3112 /* Really handle it. */
3113 if (process_serial_event () < 0)
3116 /* Be sure to not change the selected inferior behind GDB's back.
3117 Important in the non-stop mode asynchronous protocol. */
3118 set_desired_inferior (1);
3123 /* Event-loop callback for target events. */
3126 handle_target_event (int err
, gdb_client_data client_data
)
3129 fprintf (stderr
, "handling possible target event\n");
3131 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3134 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3136 int pid
= ptid_get_pid (last_ptid
);
3137 struct process_info
*process
= find_process_pid (pid
);
3138 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3140 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3141 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3143 mark_breakpoints_out (process
);
3144 mourn_inferior (process
);
3148 /* We're reporting this thread as stopped. Update its
3149 "want-stopped" state to what the client wants, until it
3150 gets a new resume action. */
3151 current_inferior
->last_resume_kind
= resume_stop
;
3152 current_inferior
->last_status
= last_status
;
3157 if (!target_running ())
3159 /* The last process exited. We're done. */
3163 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3165 /* A thread stopped with a signal, but gdb isn't
3166 connected to handle it. Pass it down to the
3167 inferior, as if it wasn't being traced. */
3168 struct thread_resume resume_info
;
3172 "GDB not connected; forwarding event %d for [%s]\n",
3173 (int) last_status
.kind
,
3174 target_pid_to_str (last_ptid
));
3176 resume_info
.thread
= last_ptid
;
3177 resume_info
.kind
= resume_continue
;
3178 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3179 (*the_target
->resume
) (&resume_info
, 1);
3181 else if (debug_threads
)
3182 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3183 (int) last_status
.kind
,
3184 target_pid_to_str (last_ptid
));
3188 /* Something interesting. Tell GDB about it. */
3189 push_event (last_ptid
, &last_status
);
3193 /* Be sure to not change the selected inferior behind GDB's back.
3194 Important in the non-stop mode asynchronous protocol. */
3195 set_desired_inferior (1);