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. Returns the number of bytes
560 actually read, zero when no further transfer is possible, and -1 on
561 error. Return of a positive value smaller than LEN does not
562 indicate there's no more to be read, only the end of the transfer.
563 E.g., when GDB reads memory from a traceframe, a first request may
564 be served from a memory block that does not cover the whole request
565 length. A following request gets the rest served from either
566 another block (of the same traceframe) or from the read-only
570 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
574 if (current_traceframe
>= 0)
577 ULONGEST length
= len
;
579 if (traceframe_read_mem (current_traceframe
,
580 memaddr
, myaddr
, len
, &nbytes
))
582 /* Data read from trace buffer, we're done. */
585 if (!in_readonly_region (memaddr
, length
))
587 /* Otherwise we have a valid readonly case, fall through. */
588 /* (assume no half-trace half-real blocks for now) */
591 res
= prepare_to_access_memory ();
594 res
= read_inferior_memory (memaddr
, myaddr
, len
);
595 done_accessing_memory ();
597 return res
== 0 ? len
: -1;
603 /* Write trace frame or inferior memory. Actually, writing to trace
604 frames is forbidden. */
607 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
609 if (current_traceframe
>= 0)
615 ret
= prepare_to_access_memory ();
618 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
619 done_accessing_memory ();
625 /* Subroutine of handle_search_memory to simplify it. */
628 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
629 gdb_byte
*pattern
, unsigned pattern_len
,
630 gdb_byte
*search_buf
,
631 unsigned chunk_size
, unsigned search_buf_size
,
632 CORE_ADDR
*found_addrp
)
634 /* Prime the search buffer. */
636 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
639 warning ("Unable to access target memory at 0x%lx, halting search.",
644 /* Perform the search.
646 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
647 When we've scanned N bytes we copy the trailing bytes to the start and
648 read in another N bytes. */
650 while (search_space_len
>= pattern_len
)
653 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
657 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
659 if (found_ptr
!= NULL
)
661 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
662 *found_addrp
= found_addr
;
666 /* Not found in this chunk, skip to next chunk. */
668 /* Don't let search_space_len wrap here, it's unsigned. */
669 if (search_space_len
>= chunk_size
)
670 search_space_len
-= chunk_size
;
672 search_space_len
= 0;
674 if (search_space_len
>= pattern_len
)
676 unsigned keep_len
= search_buf_size
- chunk_size
;
677 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
680 /* Copy the trailing part of the previous iteration to the front
681 of the buffer for the next iteration. */
682 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
684 nr_to_read
= (search_space_len
- keep_len
< chunk_size
685 ? search_space_len
- keep_len
688 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
689 nr_to_read
) != search_buf_size
)
691 warning ("Unable to access target memory "
692 "at 0x%lx, halting search.",
697 start_addr
+= chunk_size
;
706 /* Handle qSearch:memory packets. */
709 handle_search_memory (char *own_buf
, int packet_len
)
711 CORE_ADDR start_addr
;
712 CORE_ADDR search_space_len
;
714 unsigned int pattern_len
;
715 /* NOTE: also defined in find.c testcase. */
716 #define SEARCH_CHUNK_SIZE 16000
717 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
718 /* Buffer to hold memory contents for searching. */
719 gdb_byte
*search_buf
;
720 unsigned search_buf_size
;
722 CORE_ADDR found_addr
;
723 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
725 pattern
= malloc (packet_len
);
728 error ("Unable to allocate memory to perform the search");
729 strcpy (own_buf
, "E00");
732 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
733 packet_len
- cmd_name_len
,
734 &start_addr
, &search_space_len
,
735 pattern
, &pattern_len
) < 0)
738 error ("Error in parsing qSearch:memory packet");
739 strcpy (own_buf
, "E00");
743 search_buf_size
= chunk_size
+ pattern_len
- 1;
745 /* No point in trying to allocate a buffer larger than the search space. */
746 if (search_space_len
< search_buf_size
)
747 search_buf_size
= search_space_len
;
749 search_buf
= malloc (search_buf_size
);
750 if (search_buf
== NULL
)
753 error ("Unable to allocate memory to perform the search");
754 strcpy (own_buf
, "E00");
758 found
= handle_search_memory_1 (start_addr
, search_space_len
,
759 pattern
, pattern_len
,
760 search_buf
, chunk_size
, search_buf_size
,
764 sprintf (own_buf
, "1,%lx", (long) found_addr
);
766 strcpy (own_buf
, "0");
768 strcpy (own_buf
, "E00");
774 #define require_running(BUF) \
775 if (!target_running ()) \
781 /* Handle monitor commands not handled by target-specific handlers. */
784 handle_monitor_command (char *mon
)
786 if (strcmp (mon
, "set debug 1") == 0)
789 monitor_output ("Debug output enabled.\n");
791 else if (strcmp (mon
, "set debug 0") == 0)
794 monitor_output ("Debug output disabled.\n");
796 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
799 monitor_output ("H/W point debugging output enabled.\n");
801 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
804 monitor_output ("H/W point debugging output disabled.\n");
806 else if (strcmp (mon
, "set remote-debug 1") == 0)
809 monitor_output ("Protocol debug output enabled.\n");
811 else if (strcmp (mon
, "set remote-debug 0") == 0)
814 monitor_output ("Protocol debug output disabled.\n");
816 else if (strcmp (mon
, "help") == 0)
817 monitor_show_help ();
818 else if (strcmp (mon
, "exit") == 0)
822 monitor_output ("Unknown monitor command.\n\n");
823 monitor_show_help ();
828 /* Associates a callback with each supported qXfer'able object. */
832 /* The object this handler handles. */
835 /* Request that the target transfer up to LEN 8-bit bytes of the
836 target's OBJECT. The OFFSET, for a seekable object, specifies
837 the starting point. The ANNEX can be used to provide additional
838 data-specific information to the target.
840 Return the number of bytes actually transfered, zero when no
841 further transfer is possible, -1 on error, and -2 when the
842 transfer is not supported. Return of a positive value smaller
843 than LEN does not indicate the end of the object, only the end of
846 One, and only one, of readbuf or writebuf must be non-NULL. */
847 int (*xfer
) (const char *annex
,
848 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
849 ULONGEST offset
, LONGEST len
);
852 /* Handle qXfer:auxv:read. */
855 handle_qxfer_auxv (const char *annex
,
856 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
857 ULONGEST offset
, LONGEST len
)
859 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
862 if (annex
[0] != '\0' || !target_running ())
865 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
868 /* Handle qXfer:features:read. */
871 handle_qxfer_features (const char *annex
,
872 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
873 ULONGEST offset
, LONGEST len
)
875 const char *document
;
878 if (writebuf
!= NULL
)
881 if (!target_running ())
884 /* Grab the correct annex. */
885 document
= get_features_xml (annex
);
886 if (document
== NULL
)
889 total_len
= strlen (document
);
891 if (offset
> total_len
)
894 if (offset
+ len
> total_len
)
895 len
= total_len
- offset
;
897 memcpy (readbuf
, document
+ offset
, len
);
901 /* Handle qXfer:libraries:read. */
904 handle_qxfer_libraries (const char *annex
,
905 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
906 ULONGEST offset
, LONGEST len
)
908 unsigned int total_len
;
910 struct inferior_list_entry
*dll_ptr
;
912 if (writebuf
!= NULL
)
915 if (annex
[0] != '\0' || !target_running ())
918 /* Over-estimate the necessary memory. Assume that every character
919 in the library name must be escaped. */
921 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
922 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
924 document
= malloc (total_len
);
925 if (document
== NULL
)
928 strcpy (document
, "<library-list>\n");
929 p
= document
+ strlen (document
);
931 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
933 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
936 strcpy (p
, " <library name=\"");
938 name
= xml_escape_text (dll
->name
);
942 strcpy (p
, "\"><segment address=\"");
944 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
946 strcpy (p
, "\"/></library>\n");
950 strcpy (p
, "</library-list>\n");
952 total_len
= strlen (document
);
954 if (offset
> total_len
)
960 if (offset
+ len
> total_len
)
961 len
= total_len
- offset
;
963 memcpy (readbuf
, document
+ offset
, len
);
968 /* Handle qXfer:osadata:read. */
971 handle_qxfer_osdata (const char *annex
,
972 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
973 ULONGEST offset
, LONGEST len
)
975 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
978 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
981 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
984 handle_qxfer_siginfo (const char *annex
,
985 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
986 ULONGEST offset
, LONGEST len
)
988 if (the_target
->qxfer_siginfo
== NULL
)
991 if (annex
[0] != '\0' || !target_running ())
994 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
997 /* Handle qXfer:spu:read and qXfer:spu:write. */
1000 handle_qxfer_spu (const char *annex
,
1001 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1002 ULONGEST offset
, LONGEST len
)
1004 if (the_target
->qxfer_spu
== NULL
)
1007 if (!target_running ())
1010 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1013 /* Handle qXfer:statictrace:read. */
1016 handle_qxfer_statictrace (const char *annex
,
1017 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1018 ULONGEST offset
, LONGEST len
)
1022 if (writebuf
!= NULL
)
1025 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1028 if (traceframe_read_sdata (current_traceframe
, offset
,
1029 readbuf
, len
, &nbytes
))
1034 /* Helper for handle_qxfer_threads. */
1037 handle_qxfer_threads_proper (struct buffer
*buffer
)
1039 struct inferior_list_entry
*thread
;
1041 buffer_grow_str (buffer
, "<threads>\n");
1043 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1045 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1050 write_ptid (ptid_s
, ptid
);
1052 if (the_target
->core_of_thread
)
1053 core
= (*the_target
->core_of_thread
) (ptid
);
1057 sprintf (core_s
, "%d", core
);
1058 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1063 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1068 buffer_grow_str0 (buffer
, "</threads>\n");
1071 /* Handle qXfer:threads:read. */
1074 handle_qxfer_threads (const char *annex
,
1075 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1076 ULONGEST offset
, LONGEST len
)
1078 static char *result
= 0;
1079 static unsigned int result_length
= 0;
1081 if (writebuf
!= NULL
)
1084 if (!target_running () || annex
[0] != '\0')
1089 struct buffer buffer
;
1090 /* When asked for data at offset 0, generate everything and store into
1091 'result'. Successive reads will be served off 'result'. */
1095 buffer_init (&buffer
);
1097 handle_qxfer_threads_proper (&buffer
);
1099 result
= buffer_finish (&buffer
);
1100 result_length
= strlen (result
);
1101 buffer_free (&buffer
);
1104 if (offset
>= result_length
)
1106 /* We're out of data. */
1113 if (len
> result_length
- offset
)
1114 len
= result_length
- offset
;
1116 memcpy (readbuf
, result
+ offset
, len
);
1121 /* Handle qXfer:traceframe-info:read. */
1124 handle_qxfer_traceframe_info (const char *annex
,
1125 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1126 ULONGEST offset
, LONGEST len
)
1128 static char *result
= 0;
1129 static unsigned int result_length
= 0;
1131 if (writebuf
!= NULL
)
1134 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1139 struct buffer buffer
;
1141 /* When asked for data at offset 0, generate everything and
1142 store into 'result'. Successive reads will be served off
1146 buffer_init (&buffer
);
1148 traceframe_read_info (current_traceframe
, &buffer
);
1150 result
= buffer_finish (&buffer
);
1151 result_length
= strlen (result
);
1152 buffer_free (&buffer
);
1155 if (offset
>= result_length
)
1157 /* We're out of data. */
1164 if (len
> result_length
- offset
)
1165 len
= result_length
- offset
;
1167 memcpy (readbuf
, result
+ offset
, len
);
1171 static const struct qxfer qxfer_packets
[] =
1173 { "auxv", handle_qxfer_auxv
},
1174 { "features", handle_qxfer_features
},
1175 { "libraries", handle_qxfer_libraries
},
1176 { "osdata", handle_qxfer_osdata
},
1177 { "siginfo", handle_qxfer_siginfo
},
1178 { "spu", handle_qxfer_spu
},
1179 { "statictrace", handle_qxfer_statictrace
},
1180 { "threads", handle_qxfer_threads
},
1181 { "traceframe-info", handle_qxfer_traceframe_info
},
1185 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1193 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1196 /* Grab the object, r/w and annex. */
1197 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1199 write_enn (own_buf
);
1204 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1207 const struct qxfer
*q
= &qxfer_packets
[i
];
1209 if (strcmp (object
, q
->object
) == 0)
1211 if (strcmp (rw
, "read") == 0)
1213 unsigned char *data
;
1218 /* Grab the offset and length. */
1219 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1221 write_enn (own_buf
);
1225 /* Read one extra byte, as an indicator of whether there is
1227 if (len
> PBUFSIZ
- 2)
1229 data
= malloc (len
+ 1);
1232 write_enn (own_buf
);
1235 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1242 write_enn (own_buf
);
1244 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1246 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1251 else if (strcmp (rw
, "write") == 0)
1256 unsigned char *data
;
1258 strcpy (own_buf
, "E00");
1259 data
= malloc (packet_len
- (offset
- own_buf
));
1262 write_enn (own_buf
);
1265 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1266 &ofs
, &len
, data
) < 0)
1269 write_enn (own_buf
);
1273 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1280 write_enn (own_buf
);
1282 sprintf (own_buf
, "%x", n
);
1295 /* Table used by the crc32 function to calcuate the checksum. */
1297 static unsigned int crc32_table
[256] =
1300 /* Compute 32 bit CRC from inferior memory.
1302 On success, return 32 bit CRC.
1303 On failure, return (unsigned long long) -1. */
1305 static unsigned long long
1306 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1308 if (!crc32_table
[1])
1310 /* Initialize the CRC table and the decoding table. */
1314 for (i
= 0; i
< 256; i
++)
1316 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1317 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1324 unsigned char byte
= 0;
1326 /* Return failure if memory read fails. */
1327 if (read_inferior_memory (base
, &byte
, 1) != 0)
1328 return (unsigned long long) -1;
1330 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1333 return (unsigned long long) crc
;
1336 /* Handle all of the extended 'q' packets. */
1339 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1341 static struct inferior_list_entry
*thread_ptr
;
1343 /* Reply the current thread id. */
1344 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1347 require_running (own_buf
);
1349 if (!ptid_equal (general_thread
, null_ptid
)
1350 && !ptid_equal (general_thread
, minus_one_ptid
))
1351 gdb_id
= general_thread
;
1354 thread_ptr
= all_threads
.head
;
1355 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1358 sprintf (own_buf
, "QC");
1360 own_buf
= write_ptid (own_buf
, gdb_id
);
1364 if (strcmp ("qSymbol::", own_buf
) == 0)
1366 /* GDB is suggesting new symbols have been loaded. This may
1367 mean a new shared library has been detected as loaded, so
1368 take the opportunity to check if breakpoints we think are
1369 inserted, still are. Note that it isn't guaranteed that
1370 we'll see this when a shared library is loaded, and nor will
1371 we see this for unloads (although breakpoints in unloaded
1372 libraries shouldn't trigger), as GDB may not find symbols for
1373 the library at all. We also re-validate breakpoints when we
1374 see a second GDB breakpoint for the same address, and or when
1375 we access breakpoint shadows. */
1376 validate_breakpoints ();
1378 if (target_supports_tracepoints ())
1379 tracepoint_look_up_symbols ();
1381 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1382 (*the_target
->look_up_symbols
) ();
1384 strcpy (own_buf
, "OK");
1388 if (!disable_packet_qfThreadInfo
)
1390 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1394 require_running (own_buf
);
1395 thread_ptr
= all_threads
.head
;
1398 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1399 write_ptid (own_buf
, gdb_id
);
1400 thread_ptr
= thread_ptr
->next
;
1404 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1408 require_running (own_buf
);
1409 if (thread_ptr
!= NULL
)
1412 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1413 write_ptid (own_buf
, gdb_id
);
1414 thread_ptr
= thread_ptr
->next
;
1419 sprintf (own_buf
, "l");
1425 if (the_target
->read_offsets
!= NULL
1426 && strcmp ("qOffsets", own_buf
) == 0)
1428 CORE_ADDR text
, data
;
1430 require_running (own_buf
);
1431 if (the_target
->read_offsets (&text
, &data
))
1432 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1433 (long)text
, (long)data
, (long)data
);
1435 write_enn (own_buf
);
1440 /* Protocol features query. */
1441 if (strncmp ("qSupported", own_buf
, 10) == 0
1442 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1444 char *p
= &own_buf
[10];
1445 int gdb_supports_qRelocInsn
= 0;
1447 /* Start processing qSupported packet. */
1448 target_process_qsupported (NULL
);
1450 /* Process each feature being provided by GDB. The first
1451 feature will follow a ':', and latter features will follow
1455 char **qsupported
= NULL
;
1459 /* Two passes, to avoid nested strtok calls in
1460 target_process_qsupported. */
1461 for (p
= strtok (p
+ 1, ";");
1463 p
= strtok (NULL
, ";"))
1466 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1467 qsupported
[count
- 1] = xstrdup (p
);
1470 for (i
= 0; i
< count
; i
++)
1473 if (strcmp (p
, "multiprocess+") == 0)
1475 /* GDB supports and wants multi-process support if
1477 if (target_supports_multi_process ())
1480 else if (strcmp (p
, "qRelocInsn+") == 0)
1482 /* GDB supports relocate instruction requests. */
1483 gdb_supports_qRelocInsn
= 1;
1486 target_process_qsupported (p
);
1494 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1496 /* We do not have any hook to indicate whether the target backend
1497 supports qXfer:libraries:read, so always report it. */
1498 strcat (own_buf
, ";qXfer:libraries:read+");
1500 if (the_target
->read_auxv
!= NULL
)
1501 strcat (own_buf
, ";qXfer:auxv:read+");
1503 if (the_target
->qxfer_spu
!= NULL
)
1504 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1506 if (the_target
->qxfer_siginfo
!= NULL
)
1507 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1509 /* We always report qXfer:features:read, as targets may
1510 install XML files on a subsequent call to arch_setup.
1511 If we reported to GDB on startup that we don't support
1512 qXfer:feature:read at all, we will never be re-queried. */
1513 strcat (own_buf
, ";qXfer:features:read+");
1515 if (transport_is_reliable
)
1516 strcat (own_buf
, ";QStartNoAckMode+");
1518 if (the_target
->qxfer_osdata
!= NULL
)
1519 strcat (own_buf
, ";qXfer:osdata:read+");
1521 if (target_supports_multi_process ())
1522 strcat (own_buf
, ";multiprocess+");
1524 if (target_supports_non_stop ())
1525 strcat (own_buf
, ";QNonStop+");
1527 strcat (own_buf
, ";qXfer:threads:read+");
1529 if (target_supports_tracepoints ())
1531 strcat (own_buf
, ";ConditionalTracepoints+");
1532 strcat (own_buf
, ";TraceStateVariables+");
1533 strcat (own_buf
, ";TracepointSource+");
1534 strcat (own_buf
, ";DisconnectedTracing+");
1535 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1536 strcat (own_buf
, ";FastTracepoints+");
1537 strcat (own_buf
, ";StaticTracepoints+");
1538 strcat (own_buf
, ";qXfer:statictrace:read+");
1539 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1545 /* Thread-local storage support. */
1546 if (the_target
->get_tls_address
!= NULL
1547 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1549 char *p
= own_buf
+ 12;
1550 CORE_ADDR parts
[2], address
= 0;
1552 ptid_t ptid
= null_ptid
;
1554 require_running (own_buf
);
1556 for (i
= 0; i
< 3; i
++)
1564 p2
= strchr (p
, ',');
1577 ptid
= read_ptid (p
, NULL
);
1579 decode_address (&parts
[i
- 1], p
, len
);
1583 if (p
!= NULL
|| i
< 3)
1587 struct thread_info
*thread
= find_thread_ptid (ptid
);
1592 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1598 strcpy (own_buf
, paddress(address
));
1603 write_enn (own_buf
);
1607 /* Otherwise, pretend we do not understand this packet. */
1610 /* Windows OS Thread Information Block address support. */
1611 if (the_target
->get_tib_address
!= NULL
1612 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1617 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1619 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1622 strcpy (own_buf
, paddress(tlb
));
1627 write_enn (own_buf
);
1633 /* Handle "monitor" commands. */
1634 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1636 char *mon
= malloc (PBUFSIZ
);
1637 int len
= strlen (own_buf
+ 6);
1641 write_enn (own_buf
);
1645 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1647 write_enn (own_buf
);
1651 mon
[len
/ 2] = '\0';
1655 if (the_target
->handle_monitor_command
== NULL
1656 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1657 /* Default processing. */
1658 handle_monitor_command (mon
);
1664 if (strncmp ("qSearch:memory:", own_buf
,
1665 sizeof ("qSearch:memory:") - 1) == 0)
1667 require_running (own_buf
);
1668 handle_search_memory (own_buf
, packet_len
);
1672 if (strcmp (own_buf
, "qAttached") == 0
1673 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1675 struct process_info
*process
;
1677 if (own_buf
[sizeof ("qAttached") - 1])
1679 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1680 process
= (struct process_info
*)
1681 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1685 require_running (own_buf
);
1686 process
= current_process ();
1689 if (process
== NULL
)
1691 write_enn (own_buf
);
1695 strcpy (own_buf
, process
->attached
? "1" : "0");
1699 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1701 /* CRC check (compare-section). */
1705 unsigned long long crc
;
1707 require_running (own_buf
);
1708 base
= strtoul (own_buf
+ 5, &comma
, 16);
1709 if (*comma
++ != ',')
1711 write_enn (own_buf
);
1714 len
= strtoul (comma
, NULL
, 16);
1715 crc
= crc32 (base
, len
, 0xffffffff);
1716 /* Check for memory failure. */
1717 if (crc
== (unsigned long long) -1)
1719 write_enn (own_buf
);
1722 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1726 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1729 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1732 /* Otherwise we didn't know what packet it was. Say we didn't
1737 static void gdb_wants_all_threads_stopped (void);
1739 /* Parse vCont packets. */
1741 handle_v_cont (char *own_buf
)
1745 struct thread_resume
*resume_info
;
1746 struct thread_resume default_action
= {{0}};
1748 /* Count the number of semicolons in the packet. There should be one
1749 for every action. */
1755 p
= strchr (p
, ';');
1758 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1759 if (resume_info
== NULL
)
1767 if (p
[0] == 's' || p
[0] == 'S')
1768 resume_info
[i
].kind
= resume_step
;
1769 else if (p
[0] == 'c' || p
[0] == 'C')
1770 resume_info
[i
].kind
= resume_continue
;
1771 else if (p
[0] == 't')
1772 resume_info
[i
].kind
= resume_stop
;
1776 if (p
[0] == 'S' || p
[0] == 'C')
1779 sig
= strtol (p
+ 1, &q
, 16);
1784 if (!target_signal_to_host_p (sig
))
1786 resume_info
[i
].sig
= target_signal_to_host (sig
);
1790 resume_info
[i
].sig
= 0;
1796 resume_info
[i
].thread
= minus_one_ptid
;
1797 default_action
= resume_info
[i
];
1799 /* Note: we don't increment i here, we'll overwrite this entry
1800 the next time through. */
1802 else if (p
[0] == ':')
1804 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1809 if (p
[0] != ';' && p
[0] != 0)
1812 resume_info
[i
].thread
= ptid
;
1819 resume_info
[i
] = default_action
;
1821 /* Still used in occasional places in the backend. */
1823 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1824 && resume_info
[0].kind
!= resume_stop
)
1825 cont_thread
= resume_info
[0].thread
;
1827 cont_thread
= minus_one_ptid
;
1828 set_desired_inferior (0);
1833 (*the_target
->resume
) (resume_info
, n
);
1841 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1843 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1844 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1845 current_inferior
->last_status
= last_status
;
1847 /* From the client's perspective, all-stop mode always stops all
1848 threads implicitly (and the target backend has already done
1849 so by now). Tag all threads as "want-stopped", so we don't
1850 resume them implicitly without the client telling us to. */
1851 gdb_wants_all_threads_stopped ();
1852 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1853 disable_async_io ();
1855 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1856 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1857 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1862 write_enn (own_buf
);
1867 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1869 handle_v_attach (char *own_buf
)
1873 pid
= strtol (own_buf
+ 8, NULL
, 16);
1874 if (pid
!= 0 && attach_inferior (pid
) == 0)
1876 /* Don't report shared library events after attaching, even if
1877 some libraries are preloaded. GDB will always poll the
1878 library list. Avoids the "stopped by shared library event"
1879 notice on the GDB side. */
1884 /* In non-stop, we don't send a resume reply. Stop events
1885 will follow up using the normal notification
1890 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1896 write_enn (own_buf
);
1901 /* Run a new program. Return 1 if successful, 0 if failure. */
1903 handle_v_run (char *own_buf
)
1905 char *p
, *next_p
, **new_argv
;
1909 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1915 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1916 if (new_argv
== NULL
)
1918 write_enn (own_buf
);
1923 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1925 next_p
= strchr (p
, ';');
1927 next_p
= p
+ strlen (p
);
1929 if (i
== 0 && p
== next_p
)
1933 /* FIXME: Fail request if out of memory instead of dying. */
1934 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1935 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1936 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1945 if (new_argv
[0] == NULL
)
1947 /* GDB didn't specify a program to run. Use the program from the
1948 last run with the new argument list. */
1950 if (program_argv
== NULL
)
1952 /* FIXME: new_argv memory leak */
1953 write_enn (own_buf
);
1957 new_argv
[0] = strdup (program_argv
[0]);
1958 if (new_argv
[0] == NULL
)
1960 /* FIXME: new_argv memory leak */
1961 write_enn (own_buf
);
1966 /* Free the old argv and install the new one. */
1967 freeargv (program_argv
);
1968 program_argv
= new_argv
;
1970 start_inferior (program_argv
);
1971 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1973 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1975 /* In non-stop, sending a resume reply doesn't set the general
1976 thread, but GDB assumes a vRun sets it (this is so GDB can
1977 query which is the main thread of the new inferior. */
1979 general_thread
= last_ptid
;
1985 write_enn (own_buf
);
1990 /* Kill process. Return 1 if successful, 0 if failure. */
1992 handle_v_kill (char *own_buf
)
1995 char *p
= &own_buf
[6];
1997 pid
= strtol (p
, NULL
, 16);
2000 if (pid
!= 0 && kill_inferior (pid
) == 0)
2002 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2003 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2004 last_ptid
= pid_to_ptid (pid
);
2005 discard_queued_stop_replies (pid
);
2011 write_enn (own_buf
);
2016 /* Handle a 'vStopped' packet. */
2018 handle_v_stopped (char *own_buf
)
2020 /* If we're waiting for GDB to acknowledge a pending stop reply,
2021 consider that done. */
2024 struct vstop_notif
*head
;
2027 fprintf (stderr
, "vStopped: acking %s\n",
2028 target_pid_to_str (notif_queue
->ptid
));
2031 notif_queue
= notif_queue
->next
;
2035 /* Push another stop reply, or if there are no more left, an OK. */
2036 send_next_stop_reply (own_buf
);
2039 /* Handle all of the extended 'v' packets. */
2041 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2043 if (!disable_packet_vCont
)
2045 if (strncmp (own_buf
, "vCont;", 6) == 0)
2047 require_running (own_buf
);
2048 handle_v_cont (own_buf
);
2052 if (strncmp (own_buf
, "vCont?", 6) == 0)
2054 strcpy (own_buf
, "vCont;c;C;s;S;t");
2059 if (strncmp (own_buf
, "vFile:", 6) == 0
2060 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2063 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2065 if (!multi_process
&& target_running ())
2067 fprintf (stderr
, "Already debugging a process\n");
2068 write_enn (own_buf
);
2071 handle_v_attach (own_buf
);
2075 if (strncmp (own_buf
, "vRun;", 5) == 0)
2077 if (!multi_process
&& target_running ())
2079 fprintf (stderr
, "Already debugging a process\n");
2080 write_enn (own_buf
);
2083 handle_v_run (own_buf
);
2087 if (strncmp (own_buf
, "vKill;", 6) == 0)
2089 if (!target_running ())
2091 fprintf (stderr
, "No process to kill\n");
2092 write_enn (own_buf
);
2095 handle_v_kill (own_buf
);
2099 if (strncmp (own_buf
, "vStopped", 8) == 0)
2101 handle_v_stopped (own_buf
);
2105 /* Otherwise we didn't know what packet it was. Say we didn't
2111 /* Resume inferior and wait for another event. In non-stop mode,
2112 don't really wait here, but return immediatelly to the event
2115 myresume (char *own_buf
, int step
, int sig
)
2117 struct thread_resume resume_info
[2];
2119 int valid_cont_thread
;
2121 set_desired_inferior (0);
2123 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2124 && !ptid_equal (cont_thread
, minus_one_ptid
));
2126 if (step
|| sig
|| valid_cont_thread
)
2128 resume_info
[0].thread
2129 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2131 resume_info
[0].kind
= resume_step
;
2133 resume_info
[0].kind
= resume_continue
;
2134 resume_info
[0].sig
= sig
;
2138 if (!valid_cont_thread
)
2140 resume_info
[n
].thread
= minus_one_ptid
;
2141 resume_info
[n
].kind
= resume_continue
;
2142 resume_info
[n
].sig
= 0;
2149 (*the_target
->resume
) (resume_info
, n
);
2155 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2157 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2158 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2160 current_inferior
->last_resume_kind
= resume_stop
;
2161 current_inferior
->last_status
= last_status
;
2164 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2165 disable_async_io ();
2167 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2168 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2169 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2173 /* Callback for for_each_inferior. Make a new stop reply for each
2177 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2179 struct thread_info
*thread
= (struct thread_info
*) entry
;
2181 /* For now, assume targets that don't have this callback also don't
2182 manage the thread's last_status field. */
2183 if (the_target
->thread_stopped
== NULL
)
2185 /* Pass the last stop reply back to GDB, but don't notify
2187 queue_stop_reply (entry
->id
, &thread
->last_status
);
2191 if (thread_stopped (thread
))
2195 "Reporting thread %s as already stopped with %s\n",
2196 target_pid_to_str (entry
->id
),
2197 target_waitstatus_to_string (&thread
->last_status
));
2199 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2201 /* Pass the last stop reply back to GDB, but don't notify
2203 queue_stop_reply (entry
->id
, &thread
->last_status
);
2210 /* Set this inferior threads's state as "want-stopped". We won't
2211 resume this thread until the client gives us another action for
2215 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2217 struct thread_info
*thread
= (struct thread_info
*) entry
;
2219 thread
->last_resume_kind
= resume_stop
;
2221 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2223 /* Most threads are stopped implicitly (all-stop); tag that with
2225 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2226 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2230 /* Set all threads' states as "want-stopped". */
2233 gdb_wants_all_threads_stopped (void)
2235 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2238 /* Clear the gdb_detached flag of every process. */
2241 gdb_reattached_process (struct inferior_list_entry
*entry
)
2243 struct process_info
*process
= (struct process_info
*) entry
;
2245 process
->gdb_detached
= 0;
2248 /* Status handler for the '?' packet. */
2251 handle_status (char *own_buf
)
2253 /* GDB is connected, don't forward events to the target anymore. */
2254 for_each_inferior (&all_processes
, gdb_reattached_process
);
2256 /* In non-stop mode, we must send a stop reply for each stopped
2257 thread. In all-stop mode, just send one for the first stopped
2262 discard_queued_stop_replies (-1);
2263 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2265 /* The first is sent immediatly. OK is sent if there is no
2266 stopped thread, which is the same handling of the vStopped
2267 packet (by design). */
2268 send_next_stop_reply (own_buf
);
2273 stabilize_threads ();
2274 gdb_wants_all_threads_stopped ();
2276 if (all_threads
.head
)
2278 struct target_waitstatus status
;
2280 status
.kind
= TARGET_WAITKIND_STOPPED
;
2281 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2282 prepare_resume_reply (own_buf
,
2283 all_threads
.head
->id
, &status
);
2286 strcpy (own_buf
, "W00");
2291 gdbserver_version (void)
2293 printf ("GNU gdbserver %s%s\n"
2294 "Copyright (C) 2011 Free Software Foundation, Inc.\n"
2295 "gdbserver is free software, covered by the "
2296 "GNU General Public License.\n"
2297 "This gdbserver was configured as \"%s\"\n",
2298 PKGVERSION
, version
, host_name
);
2302 gdbserver_usage (FILE *stream
)
2304 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2305 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2306 "\tgdbserver [OPTIONS] --multi COMM\n"
2308 "COMM may either be a tty device (for serial debugging), or \n"
2309 "HOST:PORT to listen for a TCP connection.\n"
2312 " --debug Enable general debugging output.\n"
2313 " --remote-debug Enable remote protocol debugging output.\n"
2314 " --version Display version information and exit.\n"
2315 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2316 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2317 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2321 gdbserver_show_disableable (FILE *stream
)
2323 fprintf (stream
, "Disableable packets:\n"
2324 " vCont \tAll vCont packets\n"
2325 " qC \tQuerying the current thread\n"
2326 " qfThreadInfo\tThread listing\n"
2327 " Tthread \tPassing the thread specifier in the "
2328 "T stop reply packet\n"
2329 " threads \tAll of the above\n");
2333 #undef require_running
2334 #define require_running(BUF) \
2335 if (!target_running ()) \
2342 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2344 int pid
= * (int *) args
;
2346 if (ptid_get_pid (entry
->id
) == pid
)
2353 kill_inferior_callback (struct inferior_list_entry
*entry
)
2355 struct process_info
*process
= (struct process_info
*) entry
;
2356 int pid
= ptid_get_pid (process
->head
.id
);
2358 kill_inferior (pid
);
2359 discard_queued_stop_replies (pid
);
2362 /* Callback for for_each_inferior to detach or kill the inferior,
2363 depending on whether we attached to it or not.
2364 We inform the user whether we're detaching or killing the process
2365 as this is only called when gdbserver is about to exit. */
2368 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2370 struct process_info
*process
= (struct process_info
*) entry
;
2371 int pid
= ptid_get_pid (process
->head
.id
);
2373 if (process
->attached
)
2374 detach_inferior (pid
);
2376 kill_inferior (pid
);
2378 discard_queued_stop_replies (pid
);
2381 /* for_each_inferior callback for detach_or_kill_for_exit to print
2382 the pids of started inferiors. */
2385 print_started_pid (struct inferior_list_entry
*entry
)
2387 struct process_info
*process
= (struct process_info
*) entry
;
2389 if (! process
->attached
)
2391 int pid
= ptid_get_pid (process
->head
.id
);
2392 fprintf (stderr
, " %d", pid
);
2396 /* for_each_inferior callback for detach_or_kill_for_exit to print
2397 the pids of attached inferiors. */
2400 print_attached_pid (struct inferior_list_entry
*entry
)
2402 struct process_info
*process
= (struct process_info
*) entry
;
2404 if (process
->attached
)
2406 int pid
= ptid_get_pid (process
->head
.id
);
2407 fprintf (stderr
, " %d", pid
);
2411 /* Call this when exiting gdbserver with possible inferiors that need
2412 to be killed or detached from. */
2415 detach_or_kill_for_exit (void)
2417 /* First print a list of the inferiors we will be killing/detaching.
2418 This is to assist the user, for example, in case the inferior unexpectedly
2419 dies after we exit: did we screw up or did the inferior exit on its own?
2420 Having this info will save some head-scratching. */
2422 if (have_started_inferiors_p ())
2424 fprintf (stderr
, "Killing process(es):");
2425 for_each_inferior (&all_processes
, print_started_pid
);
2426 fprintf (stderr
, "\n");
2428 if (have_attached_inferiors_p ())
2430 fprintf (stderr
, "Detaching process(es):");
2431 for_each_inferior (&all_processes
, print_attached_pid
);
2432 fprintf (stderr
, "\n");
2435 /* Now we can kill or detach the inferiors. */
2437 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2441 join_inferiors_callback (struct inferior_list_entry
*entry
)
2443 struct process_info
*process
= (struct process_info
*) entry
;
2445 /* If we are attached, then we can exit. Otherwise, we need to hang
2446 around doing nothing, until the child is gone. */
2447 if (!process
->attached
)
2448 join_inferior (ptid_get_pid (process
->head
.id
));
2452 main (int argc
, char *argv
[])
2456 char *arg_end
, *port
;
2457 char **next_arg
= &argv
[1];
2462 while (*next_arg
!= NULL
&& **next_arg
== '-')
2464 if (strcmp (*next_arg
, "--version") == 0)
2466 gdbserver_version ();
2469 else if (strcmp (*next_arg
, "--help") == 0)
2471 gdbserver_usage (stdout
);
2474 else if (strcmp (*next_arg
, "--attach") == 0)
2476 else if (strcmp (*next_arg
, "--multi") == 0)
2478 else if (strcmp (*next_arg
, "--wrapper") == 0)
2482 wrapper_argv
= next_arg
;
2483 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2486 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2488 gdbserver_usage (stderr
);
2492 /* Consume the "--". */
2495 else if (strcmp (*next_arg
, "--debug") == 0)
2497 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2499 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2501 gdbserver_show_disableable (stdout
);
2504 else if (strncmp (*next_arg
,
2505 "--disable-packet=",
2506 sizeof ("--disable-packet=") - 1) == 0)
2508 char *packets
, *tok
;
2510 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2511 for (tok
= strtok (packets
, ",");
2513 tok
= strtok (NULL
, ","))
2515 if (strcmp ("vCont", tok
) == 0)
2516 disable_packet_vCont
= 1;
2517 else if (strcmp ("Tthread", tok
) == 0)
2518 disable_packet_Tthread
= 1;
2519 else if (strcmp ("qC", tok
) == 0)
2520 disable_packet_qC
= 1;
2521 else if (strcmp ("qfThreadInfo", tok
) == 0)
2522 disable_packet_qfThreadInfo
= 1;
2523 else if (strcmp ("threads", tok
) == 0)
2525 disable_packet_vCont
= 1;
2526 disable_packet_Tthread
= 1;
2527 disable_packet_qC
= 1;
2528 disable_packet_qfThreadInfo
= 1;
2532 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2534 gdbserver_show_disableable (stderr
);
2541 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2549 if (setjmp (toplevel
))
2551 fprintf (stderr
, "Exiting\n");
2557 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2559 gdbserver_usage (stderr
);
2566 /* --attach used to come after PORT, so allow it there for
2568 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2575 && (*next_arg
== NULL
2576 || (*next_arg
)[0] == '\0'
2577 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2579 || next_arg
[1] != NULL
))
2584 gdbserver_usage (stderr
);
2588 initialize_inferiors ();
2589 initialize_async_io ();
2591 if (target_supports_tracepoints ())
2592 initialize_tracepoint ();
2594 own_buf
= xmalloc (PBUFSIZ
+ 1);
2595 mem_buf
= xmalloc (PBUFSIZ
);
2597 if (pid
== 0 && *next_arg
!= NULL
)
2601 n
= argc
- (next_arg
- argv
);
2602 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2603 for (i
= 0; i
< n
; i
++)
2604 program_argv
[i
] = xstrdup (next_arg
[i
]);
2605 program_argv
[i
] = NULL
;
2607 /* Wait till we are at first instruction in program. */
2608 start_inferior (program_argv
);
2610 /* We are now (hopefully) stopped at the first instruction of
2611 the target process. This assumes that the target process was
2612 successfully created. */
2616 if (attach_inferior (pid
) == -1)
2617 error ("Attaching not supported on this target");
2619 /* Otherwise succeeded. */
2623 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2624 last_status
.value
.integer
= 0;
2625 last_ptid
= minus_one_ptid
;
2628 /* Don't report shared library events on the initial connection,
2629 even if some libraries are preloaded. Avoids the "stopped by
2630 shared library event" notice on gdb side. */
2633 if (setjmp (toplevel
))
2635 detach_or_kill_for_exit ();
2639 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2640 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2645 if (!was_running
&& !multi_mode
)
2647 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2655 /* Be sure we're out of tfind mode. */
2656 current_traceframe
= -1;
2660 if (setjmp (toplevel
) != 0)
2662 /* An error occurred. */
2663 if (response_needed
)
2665 write_enn (own_buf
);
2670 /* Wait for events. This will return when all event sources are
2671 removed from the event loop. */
2672 start_event_loop ();
2674 /* If an exit was requested (using the "monitor exit" command),
2675 terminate now. The only other way to get here is for
2676 getpkt to fail; close the connection and reopen it at the
2681 detach_or_kill_for_exit ();
2686 "Remote side has terminated connection. "
2687 "GDBserver will reopen the connection.\n");
2691 if (disconnected_tracing
)
2693 /* Try to enable non-stop/async mode, so we we can both
2694 wait for an async socket accept, and handle async
2695 target events simultaneously. There's also no point
2696 either in having the target always stop all threads,
2697 when we're going to pass signals down without
2701 if (start_non_stop (1))
2704 /* Detaching implicitly resumes all threads; simply
2705 disconnecting does not. */
2711 "Disconnected tracing disabled; stopping trace run.\n");
2718 /* Event loop callback that handles a serial event. The first byte in
2719 the serial buffer gets us here. We expect characters to arrive at
2720 a brisk pace, so we read the rest of the packet with a blocking
2724 process_serial_event (void)
2735 int new_packet_len
= -1;
2737 /* Used to decide when gdbserver should exit in
2738 multi-mode/remote. */
2739 static int have_ran
= 0;
2742 have_ran
= target_running ();
2744 disable_async_io ();
2746 response_needed
= 0;
2747 packet_len
= getpkt (own_buf
);
2748 if (packet_len
<= 0)
2751 /* Force an event loop break. */
2754 response_needed
= 1;
2761 handle_query (own_buf
, packet_len
, &new_packet_len
);
2764 handle_general_set (own_buf
);
2767 require_running (own_buf
);
2772 pid
= strtol (&own_buf
[i
], NULL
, 16);
2776 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2778 if (tracing
&& disconnected_tracing
)
2780 struct thread_resume resume_info
;
2781 struct process_info
*process
= find_process_pid (pid
);
2783 if (process
== NULL
)
2785 write_enn (own_buf
);
2790 "Disconnected tracing in effect, "
2791 "leaving gdbserver attached to the process\n");
2793 /* Make sure we're in non-stop/async mode, so we we can both
2794 wait for an async socket accept, and handle async target
2795 events simultaneously. There's also no point either in
2796 having the target stop all threads, when we're going to
2797 pass signals down without informing GDB. */
2801 fprintf (stderr
, "Forcing non-stop mode\n");
2807 process
->gdb_detached
= 1;
2809 /* Detaching implicitly resumes all threads. */
2810 resume_info
.thread
= minus_one_ptid
;
2811 resume_info
.kind
= resume_continue
;
2812 resume_info
.sig
= 0;
2813 (*the_target
->resume
) (&resume_info
, 1);
2816 break; /* from switch/case */
2819 fprintf (stderr
, "Detaching from process %d\n", pid
);
2821 if (detach_inferior (pid
) != 0)
2822 write_enn (own_buf
);
2825 discard_queued_stop_replies (pid
);
2828 if (extended_protocol
)
2830 /* Treat this like a normal program exit. */
2831 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2832 last_status
.value
.integer
= 0;
2833 last_ptid
= pid_to_ptid (pid
);
2835 current_inferior
= NULL
;
2842 /* If we are attached, then we can exit. Otherwise, we
2843 need to hang around doing nothing, until the child is
2845 for_each_inferior (&all_processes
,
2846 join_inferiors_callback
);
2852 extended_protocol
= 1;
2856 handle_status (own_buf
);
2859 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2861 ptid_t gdb_id
, thread_id
;
2864 require_running (own_buf
);
2866 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2868 pid
= ptid_get_pid (gdb_id
);
2870 if (ptid_equal (gdb_id
, null_ptid
)
2871 || ptid_equal (gdb_id
, minus_one_ptid
))
2872 thread_id
= null_ptid
;
2874 && ptid_equal (pid_to_ptid (pid
),
2877 struct thread_info
*thread
=
2878 (struct thread_info
*) find_inferior (&all_threads
,
2883 write_enn (own_buf
);
2887 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2891 thread_id
= gdb_id_to_thread_id (gdb_id
);
2892 if (ptid_equal (thread_id
, null_ptid
))
2894 write_enn (own_buf
);
2899 if (own_buf
[1] == 'g')
2901 if (ptid_equal (thread_id
, null_ptid
))
2903 /* GDB is telling us to choose any thread. Check if
2904 the currently selected thread is still valid. If
2905 it is not, select the first available. */
2906 struct thread_info
*thread
=
2907 (struct thread_info
*) find_inferior_id (&all_threads
,
2910 thread_id
= all_threads
.head
->id
;
2913 general_thread
= thread_id
;
2914 set_desired_inferior (1);
2916 else if (own_buf
[1] == 'c')
2917 cont_thread
= thread_id
;
2918 else if (own_buf
[1] == 's')
2919 step_thread
= thread_id
;
2925 /* Silently ignore it so that gdb can extend the protocol
2926 without compatibility headaches. */
2931 require_running (own_buf
);
2932 if (current_traceframe
>= 0)
2934 struct regcache
*regcache
= new_register_cache ();
2936 if (fetch_traceframe_registers (current_traceframe
,
2938 registers_to_string (regcache
, own_buf
);
2940 write_enn (own_buf
);
2941 free_register_cache (regcache
);
2945 struct regcache
*regcache
;
2947 set_desired_inferior (1);
2948 regcache
= get_thread_regcache (current_inferior
, 1);
2949 registers_to_string (regcache
, own_buf
);
2953 require_running (own_buf
);
2954 if (current_traceframe
>= 0)
2955 write_enn (own_buf
);
2958 struct regcache
*regcache
;
2960 set_desired_inferior (1);
2961 regcache
= get_thread_regcache (current_inferior
, 1);
2962 registers_from_string (regcache
, &own_buf
[1]);
2967 require_running (own_buf
);
2968 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2969 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
2971 write_enn (own_buf
);
2973 convert_int_to_ascii (mem_buf
, own_buf
, res
);
2976 require_running (own_buf
);
2977 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2978 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2981 write_enn (own_buf
);
2984 require_running (own_buf
);
2985 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2986 &mem_addr
, &len
, &mem_buf
) < 0
2987 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2988 write_enn (own_buf
);
2993 require_running (own_buf
);
2994 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2995 if (target_signal_to_host_p (sig
))
2996 signal
= target_signal_to_host (sig
);
2999 myresume (own_buf
, 0, signal
);
3002 require_running (own_buf
);
3003 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3004 if (target_signal_to_host_p (sig
))
3005 signal
= target_signal_to_host (sig
);
3008 myresume (own_buf
, 1, signal
);
3011 require_running (own_buf
);
3013 myresume (own_buf
, 0, signal
);
3016 require_running (own_buf
);
3018 myresume (own_buf
, 1, signal
);
3020 case 'Z': /* insert_ ... */
3022 case 'z': /* remove_ ... */
3026 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3027 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3028 char type
= own_buf
[1];
3030 const int insert
= ch
== 'Z';
3032 /* Default to unrecognized/unsupported. */
3036 case '0': /* software-breakpoint */
3037 case '1': /* hardware-breakpoint */
3038 case '2': /* write watchpoint */
3039 case '3': /* read watchpoint */
3040 case '4': /* access watchpoint */
3041 require_running (own_buf
);
3042 if (insert
&& the_target
->insert_point
!= NULL
)
3043 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3044 else if (!insert
&& the_target
->remove_point
!= NULL
)
3045 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3057 write_enn (own_buf
);
3061 response_needed
= 0;
3062 if (!target_running ())
3063 /* The packet we received doesn't make sense - but we can't
3064 reply to it, either. */
3067 fprintf (stderr
, "Killing all inferiors\n");
3068 for_each_inferior (&all_processes
, kill_inferior_callback
);
3070 /* When using the extended protocol, we wait with no program
3071 running. The traditional protocol will exit instead. */
3072 if (extended_protocol
)
3074 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3075 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3083 ptid_t gdb_id
, thread_id
;
3085 require_running (own_buf
);
3087 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3088 thread_id
= gdb_id_to_thread_id (gdb_id
);
3089 if (ptid_equal (thread_id
, null_ptid
))
3091 write_enn (own_buf
);
3095 if (mythread_alive (thread_id
))
3098 write_enn (own_buf
);
3102 response_needed
= 0;
3104 /* Restarting the inferior is only supported in the extended
3106 if (extended_protocol
)
3108 if (target_running ())
3109 for_each_inferior (&all_processes
,
3110 kill_inferior_callback
);
3111 fprintf (stderr
, "GDBserver restarting\n");
3113 /* Wait till we are at 1st instruction in prog. */
3114 if (program_argv
!= NULL
)
3115 start_inferior (program_argv
);
3118 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3119 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3125 /* It is a request we don't understand. Respond with an
3126 empty packet so that gdb knows that we don't support this
3132 /* Extended (long) request. */
3133 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3137 /* It is a request we don't understand. Respond with an empty
3138 packet so that gdb knows that we don't support this
3144 if (new_packet_len
!= -1)
3145 putpkt_binary (own_buf
, new_packet_len
);
3149 response_needed
= 0;
3151 if (!extended_protocol
&& have_ran
&& !target_running ())
3153 /* In non-stop, defer exiting until GDB had a chance to query
3154 the whole vStopped list (until it gets an OK). */
3157 fprintf (stderr
, "GDBserver exiting\n");
3169 /* Event-loop callback for serial events. */
3172 handle_serial_event (int err
, gdb_client_data client_data
)
3175 fprintf (stderr
, "handling possible serial event\n");
3177 /* Really handle it. */
3178 if (process_serial_event () < 0)
3181 /* Be sure to not change the selected inferior behind GDB's back.
3182 Important in the non-stop mode asynchronous protocol. */
3183 set_desired_inferior (1);
3188 /* Event-loop callback for target events. */
3191 handle_target_event (int err
, gdb_client_data client_data
)
3194 fprintf (stderr
, "handling possible target event\n");
3196 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3199 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3201 int pid
= ptid_get_pid (last_ptid
);
3202 struct process_info
*process
= find_process_pid (pid
);
3203 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3205 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3206 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3208 mark_breakpoints_out (process
);
3209 mourn_inferior (process
);
3213 /* We're reporting this thread as stopped. Update its
3214 "want-stopped" state to what the client wants, until it
3215 gets a new resume action. */
3216 current_inferior
->last_resume_kind
= resume_stop
;
3217 current_inferior
->last_status
= last_status
;
3222 if (!target_running ())
3224 /* The last process exited. We're done. */
3228 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3230 /* A thread stopped with a signal, but gdb isn't
3231 connected to handle it. Pass it down to the
3232 inferior, as if it wasn't being traced. */
3233 struct thread_resume resume_info
;
3237 "GDB not connected; forwarding event %d for [%s]\n",
3238 (int) last_status
.kind
,
3239 target_pid_to_str (last_ptid
));
3241 resume_info
.thread
= last_ptid
;
3242 resume_info
.kind
= resume_continue
;
3243 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3244 (*the_target
->resume
) (&resume_info
, 1);
3246 else if (debug_threads
)
3247 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3248 (int) last_status
.kind
,
3249 target_pid_to_str (last_ptid
));
3253 /* Something interesting. Tell GDB about it. */
3254 push_event (last_ptid
, &last_status
);
3258 /* Be sure to not change the selected inferior behind GDB's back.
3259 Important in the non-stop mode asynchronous protocol. */
3260 set_desired_inferior (1);