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
;
43 /* --once: Exit after the first connection has closed. */
49 static char **program_argv
, **wrapper_argv
;
51 /* Enable miscellaneous debugging output. The name is historical - it
52 was originally used to debug LinuxThreads support. */
55 /* Enable debugging of h/w breakpoint/watchpoint support. */
58 int pass_signals
[TARGET_SIGNAL_LAST
];
62 const char *gdbserver_xmltarget
;
64 /* The PID of the originally created or attached inferior. Used to
65 send signals to the process when GDB sends us an asynchronous interrupt
66 (user hitting Control-C in the client), and to wait for the child to exit
67 when no longer debugging it. */
69 unsigned long signal_pid
;
72 /* A file descriptor for the controlling terminal. */
75 /* TERMINAL_FD's original foreground group. */
76 pid_t old_foreground_pgrp
;
78 /* Hand back terminal ownership to the original foreground group. */
81 restore_old_foreground_pgrp (void)
83 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
87 /* Set if you want to disable optional thread related packets support
88 in gdbserver, for the sake of testing GDB against stubs that don't
90 int disable_packet_vCont
;
91 int disable_packet_Tthread
;
92 int disable_packet_qC
;
93 int disable_packet_qfThreadInfo
;
95 /* Last status reported to GDB. */
96 static struct target_waitstatus last_status
;
97 static ptid_t last_ptid
;
100 static unsigned char *mem_buf
;
102 /* Structure holding information relative to a single stop reply. We
103 keep a queue of these (really a singly-linked list) to push to GDB
107 /* Pointer to next in list. */
108 struct vstop_notif
*next
;
110 /* Thread or process that got the event. */
114 struct target_waitstatus status
;
117 /* The pending stop replies list head. */
118 static struct vstop_notif
*notif_queue
= NULL
;
120 /* Put a stop reply to the stop reply queue. */
123 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
125 struct vstop_notif
*new_notif
;
127 new_notif
= xmalloc (sizeof (*new_notif
));
128 new_notif
->next
= NULL
;
129 new_notif
->ptid
= ptid
;
130 new_notif
->status
= *status
;
134 struct vstop_notif
*tail
;
135 for (tail
= notif_queue
;
139 tail
->next
= new_notif
;
142 notif_queue
= new_notif
;
147 struct vstop_notif
*n
;
149 for (n
= notif_queue
; n
; n
= n
->next
)
152 fprintf (stderr
, "pending stop replies: %d\n", i
);
156 /* Place an event in the stop reply queue, and push a notification if
157 we aren't sending one yet. */
160 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
162 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
164 queue_stop_reply (ptid
, status
);
166 /* If this is the first stop reply in the queue, then inform GDB
167 about it, by sending a Stop notification. */
168 if (notif_queue
->next
== NULL
)
173 prepare_resume_reply (p
,
174 notif_queue
->ptid
, ¬if_queue
->status
);
175 putpkt_notif (own_buf
);
179 /* Get rid of the currently pending stop replies for PID. If PID is
180 -1, then apply to all processes. */
183 discard_queued_stop_replies (int pid
)
185 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
187 for (reply
= notif_queue
; reply
; reply
= next
)
192 || ptid_get_pid (reply
->ptid
) == pid
)
194 if (reply
== notif_queue
)
197 prev
->next
= reply
->next
;
206 /* If there are more stop replies to push, push one now. */
209 send_next_stop_reply (char *own_buf
)
212 prepare_resume_reply (own_buf
,
214 ¬if_queue
->status
);
220 target_running (void)
222 return all_threads
.head
!= NULL
;
226 start_inferior (char **argv
)
228 char **new_argv
= argv
;
230 if (wrapper_argv
!= NULL
)
234 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
236 for (i
= 0; argv
[i
] != NULL
; i
++)
238 new_argv
= alloca (sizeof (char *) * count
);
240 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
241 new_argv
[count
++] = wrapper_argv
[i
];
242 for (i
= 0; argv
[i
] != NULL
; i
++)
243 new_argv
[count
++] = argv
[i
];
244 new_argv
[count
] = NULL
;
250 for (i
= 0; new_argv
[i
]; ++i
)
251 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
256 signal (SIGTTOU
, SIG_DFL
);
257 signal (SIGTTIN
, SIG_DFL
);
260 signal_pid
= create_inferior (new_argv
[0], new_argv
);
262 /* FIXME: we don't actually know at this point that the create
263 actually succeeded. We won't know that until we wait. */
264 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
269 signal (SIGTTOU
, SIG_IGN
);
270 signal (SIGTTIN
, SIG_IGN
);
271 terminal_fd
= fileno (stderr
);
272 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
273 tcsetpgrp (terminal_fd
, signal_pid
);
274 atexit (restore_old_foreground_pgrp
);
277 if (wrapper_argv
!= NULL
)
279 struct thread_resume resume_info
;
281 resume_info
.thread
= pid_to_ptid (signal_pid
);
282 resume_info
.kind
= resume_continue
;
285 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
287 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
292 (*the_target
->resume
) (&resume_info
, 1);
294 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
295 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
298 current_inferior
->last_resume_kind
= resume_stop
;
299 current_inferior
->last_status
= last_status
;
301 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
303 current_inferior
->last_resume_kind
= resume_stop
;
304 current_inferior
->last_status
= last_status
;
308 /* Wait till we are at 1st instruction in program, return new pid
309 (assuming success). */
310 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
312 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
313 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
315 current_inferior
->last_resume_kind
= resume_stop
;
316 current_inferior
->last_status
= last_status
;
323 attach_inferior (int pid
)
325 /* myattach should return -1 if attaching is unsupported,
326 0 if it succeeded, and call error() otherwise. */
328 if (myattach (pid
) != 0)
331 fprintf (stderr
, "Attached; pid = %d\n", pid
);
334 /* FIXME - It may be that we should get the SIGNAL_PID from the
335 attach function, so that it can be the main thread instead of
336 whichever we were told to attach to. */
341 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
343 /* GDB knows to ignore the first SIGSTOP after attaching to a running
344 process using the "attach" command, but this is different; it's
345 just using "target remote". Pretend it's just starting up. */
346 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
347 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
348 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
350 current_inferior
->last_resume_kind
= resume_stop
;
351 current_inferior
->last_status
= last_status
;
357 extern int remote_debug
;
359 /* Decode a qXfer read request. Return 0 if everything looks OK,
363 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
365 /* After the read marker and annex, qXfer looks like a
366 traditional 'm' packet. */
367 decode_m_packet (buf
, ofs
, len
);
373 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
375 /* Extract and NUL-terminate the object. */
377 while (*buf
&& *buf
!= ':')
383 /* Extract and NUL-terminate the read/write action. */
385 while (*buf
&& *buf
!= ':')
391 /* Extract and NUL-terminate the annex. */
393 while (*buf
&& *buf
!= ':')
403 /* Write the response to a successful qXfer read. Returns the
404 length of the (binary) data stored in BUF, corresponding
405 to as much of DATA/LEN as we could fit. IS_MORE controls
406 the first character of the response. */
408 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
417 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
421 /* Handle all of the extended 'Q' packets. */
424 handle_general_set (char *own_buf
)
426 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
428 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
429 const char *p
= own_buf
+ strlen ("QPassSignals:");
432 p
= decode_address_to_semicolon (&cursig
, p
);
433 for (i
= 0; i
< numsigs
; i
++)
439 /* Keep looping, to clear the remaining signals. */
442 p
= decode_address_to_semicolon (&cursig
, p
);
447 strcpy (own_buf
, "OK");
451 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
455 fprintf (stderr
, "[noack mode enabled]\n");
464 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
466 char *mode
= own_buf
+ 9;
470 if (strcmp (mode
, "0") == 0)
472 else if (strcmp (mode
, "1") == 0)
476 /* We don't know what this mode is, so complain to
478 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
484 req_str
= req
? "non-stop" : "all-stop";
485 if (start_non_stop (req
) != 0)
487 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
495 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
501 if (target_supports_tracepoints ()
502 && handle_tracepoint_general_set (own_buf
))
505 /* Otherwise we didn't know what packet it was. Say we didn't
511 get_features_xml (const char *annex
)
513 /* gdbserver_xmltarget defines what to return when looking
514 for the "target.xml" file. Its contents can either be
515 verbatim XML code (prefixed with a '@') or else the name
516 of the actual XML file to be used in place of "target.xml".
518 This variable is set up from the auto-generated
519 init_registers_... routine for the current target. */
521 if (gdbserver_xmltarget
522 && strcmp (annex
, "target.xml") == 0)
524 if (*gdbserver_xmltarget
== '@')
525 return gdbserver_xmltarget
+ 1;
527 annex
= gdbserver_xmltarget
;
532 extern const char *const xml_builtin
[][2];
535 /* Look for the annex. */
536 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
537 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
540 if (xml_builtin
[i
][0] != NULL
)
541 return xml_builtin
[i
][1];
549 monitor_show_help (void)
551 monitor_output ("The following monitor commands are supported:\n");
552 monitor_output (" set debug <0|1>\n");
553 monitor_output (" Enable general debugging messages\n");
554 monitor_output (" set debug-hw-points <0|1>\n");
555 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
556 monitor_output (" set remote-debug <0|1>\n");
557 monitor_output (" Enable remote protocol debugging messages\n");
558 monitor_output (" exit\n");
559 monitor_output (" Quit GDBserver\n");
562 /* Read trace frame or inferior memory. Returns the number of bytes
563 actually read, zero when no further transfer is possible, and -1 on
564 error. Return of a positive value smaller than LEN does not
565 indicate there's no more to be read, only the end of the transfer.
566 E.g., when GDB reads memory from a traceframe, a first request may
567 be served from a memory block that does not cover the whole request
568 length. A following request gets the rest served from either
569 another block (of the same traceframe) or from the read-only
573 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
577 if (current_traceframe
>= 0)
580 ULONGEST length
= len
;
582 if (traceframe_read_mem (current_traceframe
,
583 memaddr
, myaddr
, len
, &nbytes
))
585 /* Data read from trace buffer, we're done. */
588 if (!in_readonly_region (memaddr
, length
))
590 /* Otherwise we have a valid readonly case, fall through. */
591 /* (assume no half-trace half-real blocks for now) */
594 res
= prepare_to_access_memory ();
597 res
= read_inferior_memory (memaddr
, myaddr
, len
);
598 done_accessing_memory ();
600 return res
== 0 ? len
: -1;
606 /* Write trace frame or inferior memory. Actually, writing to trace
607 frames is forbidden. */
610 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
612 if (current_traceframe
>= 0)
618 ret
= prepare_to_access_memory ();
621 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
622 done_accessing_memory ();
628 /* Subroutine of handle_search_memory to simplify it. */
631 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
632 gdb_byte
*pattern
, unsigned pattern_len
,
633 gdb_byte
*search_buf
,
634 unsigned chunk_size
, unsigned search_buf_size
,
635 CORE_ADDR
*found_addrp
)
637 /* Prime the search buffer. */
639 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
642 warning ("Unable to access target memory at 0x%lx, halting search.",
647 /* Perform the search.
649 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
650 When we've scanned N bytes we copy the trailing bytes to the start and
651 read in another N bytes. */
653 while (search_space_len
>= pattern_len
)
656 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
660 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
662 if (found_ptr
!= NULL
)
664 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
665 *found_addrp
= found_addr
;
669 /* Not found in this chunk, skip to next chunk. */
671 /* Don't let search_space_len wrap here, it's unsigned. */
672 if (search_space_len
>= chunk_size
)
673 search_space_len
-= chunk_size
;
675 search_space_len
= 0;
677 if (search_space_len
>= pattern_len
)
679 unsigned keep_len
= search_buf_size
- chunk_size
;
680 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
683 /* Copy the trailing part of the previous iteration to the front
684 of the buffer for the next iteration. */
685 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
687 nr_to_read
= (search_space_len
- keep_len
< chunk_size
688 ? search_space_len
- keep_len
691 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
692 nr_to_read
) != search_buf_size
)
694 warning ("Unable to access target memory "
695 "at 0x%lx, halting search.",
700 start_addr
+= chunk_size
;
709 /* Handle qSearch:memory packets. */
712 handle_search_memory (char *own_buf
, int packet_len
)
714 CORE_ADDR start_addr
;
715 CORE_ADDR search_space_len
;
717 unsigned int pattern_len
;
718 /* NOTE: also defined in find.c testcase. */
719 #define SEARCH_CHUNK_SIZE 16000
720 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
721 /* Buffer to hold memory contents for searching. */
722 gdb_byte
*search_buf
;
723 unsigned search_buf_size
;
725 CORE_ADDR found_addr
;
726 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
728 pattern
= malloc (packet_len
);
731 error ("Unable to allocate memory to perform the search");
732 strcpy (own_buf
, "E00");
735 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
736 packet_len
- cmd_name_len
,
737 &start_addr
, &search_space_len
,
738 pattern
, &pattern_len
) < 0)
741 error ("Error in parsing qSearch:memory packet");
742 strcpy (own_buf
, "E00");
746 search_buf_size
= chunk_size
+ pattern_len
- 1;
748 /* No point in trying to allocate a buffer larger than the search space. */
749 if (search_space_len
< search_buf_size
)
750 search_buf_size
= search_space_len
;
752 search_buf
= malloc (search_buf_size
);
753 if (search_buf
== NULL
)
756 error ("Unable to allocate memory to perform the search");
757 strcpy (own_buf
, "E00");
761 found
= handle_search_memory_1 (start_addr
, search_space_len
,
762 pattern
, pattern_len
,
763 search_buf
, chunk_size
, search_buf_size
,
767 sprintf (own_buf
, "1,%lx", (long) found_addr
);
769 strcpy (own_buf
, "0");
771 strcpy (own_buf
, "E00");
777 #define require_running(BUF) \
778 if (!target_running ()) \
784 /* Handle monitor commands not handled by target-specific handlers. */
787 handle_monitor_command (char *mon
)
789 if (strcmp (mon
, "set debug 1") == 0)
792 monitor_output ("Debug output enabled.\n");
794 else if (strcmp (mon
, "set debug 0") == 0)
797 monitor_output ("Debug output disabled.\n");
799 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
802 monitor_output ("H/W point debugging output enabled.\n");
804 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
807 monitor_output ("H/W point debugging output disabled.\n");
809 else if (strcmp (mon
, "set remote-debug 1") == 0)
812 monitor_output ("Protocol debug output enabled.\n");
814 else if (strcmp (mon
, "set remote-debug 0") == 0)
817 monitor_output ("Protocol debug output disabled.\n");
819 else if (strcmp (mon
, "help") == 0)
820 monitor_show_help ();
821 else if (strcmp (mon
, "exit") == 0)
825 monitor_output ("Unknown monitor command.\n\n");
826 monitor_show_help ();
831 /* Associates a callback with each supported qXfer'able object. */
835 /* The object this handler handles. */
838 /* Request that the target transfer up to LEN 8-bit bytes of the
839 target's OBJECT. The OFFSET, for a seekable object, specifies
840 the starting point. The ANNEX can be used to provide additional
841 data-specific information to the target.
843 Return the number of bytes actually transfered, zero when no
844 further transfer is possible, -1 on error, and -2 when the
845 transfer is not supported. Return of a positive value smaller
846 than LEN does not indicate the end of the object, only the end of
849 One, and only one, of readbuf or writebuf must be non-NULL. */
850 int (*xfer
) (const char *annex
,
851 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
852 ULONGEST offset
, LONGEST len
);
855 /* Handle qXfer:auxv:read. */
858 handle_qxfer_auxv (const char *annex
,
859 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
860 ULONGEST offset
, LONGEST len
)
862 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
865 if (annex
[0] != '\0' || !target_running ())
868 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
871 /* Handle qXfer:features:read. */
874 handle_qxfer_features (const char *annex
,
875 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
876 ULONGEST offset
, LONGEST len
)
878 const char *document
;
881 if (writebuf
!= NULL
)
884 if (!target_running ())
887 /* Grab the correct annex. */
888 document
= get_features_xml (annex
);
889 if (document
== NULL
)
892 total_len
= strlen (document
);
894 if (offset
> total_len
)
897 if (offset
+ len
> total_len
)
898 len
= total_len
- offset
;
900 memcpy (readbuf
, document
+ offset
, len
);
904 /* Handle qXfer:libraries:read. */
907 handle_qxfer_libraries (const char *annex
,
908 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
909 ULONGEST offset
, LONGEST len
)
911 unsigned int total_len
;
913 struct inferior_list_entry
*dll_ptr
;
915 if (writebuf
!= NULL
)
918 if (annex
[0] != '\0' || !target_running ())
921 /* Over-estimate the necessary memory. Assume that every character
922 in the library name must be escaped. */
924 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
925 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
927 document
= malloc (total_len
);
928 if (document
== NULL
)
931 strcpy (document
, "<library-list>\n");
932 p
= document
+ strlen (document
);
934 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
936 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
939 strcpy (p
, " <library name=\"");
941 name
= xml_escape_text (dll
->name
);
945 strcpy (p
, "\"><segment address=\"");
947 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
949 strcpy (p
, "\"/></library>\n");
953 strcpy (p
, "</library-list>\n");
955 total_len
= strlen (document
);
957 if (offset
> total_len
)
963 if (offset
+ len
> total_len
)
964 len
= total_len
- offset
;
966 memcpy (readbuf
, document
+ offset
, len
);
971 /* Handle qXfer:osadata:read. */
974 handle_qxfer_osdata (const char *annex
,
975 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
976 ULONGEST offset
, LONGEST len
)
978 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
981 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
984 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
987 handle_qxfer_siginfo (const char *annex
,
988 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
989 ULONGEST offset
, LONGEST len
)
991 if (the_target
->qxfer_siginfo
== NULL
)
994 if (annex
[0] != '\0' || !target_running ())
997 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1000 /* Handle qXfer:spu:read and qXfer:spu:write. */
1003 handle_qxfer_spu (const char *annex
,
1004 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1005 ULONGEST offset
, LONGEST len
)
1007 if (the_target
->qxfer_spu
== NULL
)
1010 if (!target_running ())
1013 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1016 /* Handle qXfer:statictrace:read. */
1019 handle_qxfer_statictrace (const char *annex
,
1020 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1021 ULONGEST offset
, LONGEST len
)
1025 if (writebuf
!= NULL
)
1028 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1031 if (traceframe_read_sdata (current_traceframe
, offset
,
1032 readbuf
, len
, &nbytes
))
1037 /* Helper for handle_qxfer_threads. */
1040 handle_qxfer_threads_proper (struct buffer
*buffer
)
1042 struct inferior_list_entry
*thread
;
1044 buffer_grow_str (buffer
, "<threads>\n");
1046 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1048 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1053 write_ptid (ptid_s
, ptid
);
1055 if (the_target
->core_of_thread
)
1056 core
= (*the_target
->core_of_thread
) (ptid
);
1060 sprintf (core_s
, "%d", core
);
1061 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1066 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1071 buffer_grow_str0 (buffer
, "</threads>\n");
1074 /* Handle qXfer:threads:read. */
1077 handle_qxfer_threads (const char *annex
,
1078 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1079 ULONGEST offset
, LONGEST len
)
1081 static char *result
= 0;
1082 static unsigned int result_length
= 0;
1084 if (writebuf
!= NULL
)
1087 if (!target_running () || annex
[0] != '\0')
1092 struct buffer buffer
;
1093 /* When asked for data at offset 0, generate everything and store into
1094 'result'. Successive reads will be served off 'result'. */
1098 buffer_init (&buffer
);
1100 handle_qxfer_threads_proper (&buffer
);
1102 result
= buffer_finish (&buffer
);
1103 result_length
= strlen (result
);
1104 buffer_free (&buffer
);
1107 if (offset
>= result_length
)
1109 /* We're out of data. */
1116 if (len
> result_length
- offset
)
1117 len
= result_length
- offset
;
1119 memcpy (readbuf
, result
+ offset
, len
);
1124 /* Handle qXfer:traceframe-info:read. */
1127 handle_qxfer_traceframe_info (const char *annex
,
1128 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1129 ULONGEST offset
, LONGEST len
)
1131 static char *result
= 0;
1132 static unsigned int result_length
= 0;
1134 if (writebuf
!= NULL
)
1137 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1142 struct buffer buffer
;
1144 /* When asked for data at offset 0, generate everything and
1145 store into 'result'. Successive reads will be served off
1149 buffer_init (&buffer
);
1151 traceframe_read_info (current_traceframe
, &buffer
);
1153 result
= buffer_finish (&buffer
);
1154 result_length
= strlen (result
);
1155 buffer_free (&buffer
);
1158 if (offset
>= result_length
)
1160 /* We're out of data. */
1167 if (len
> result_length
- offset
)
1168 len
= result_length
- offset
;
1170 memcpy (readbuf
, result
+ offset
, len
);
1174 static const struct qxfer qxfer_packets
[] =
1176 { "auxv", handle_qxfer_auxv
},
1177 { "features", handle_qxfer_features
},
1178 { "libraries", handle_qxfer_libraries
},
1179 { "osdata", handle_qxfer_osdata
},
1180 { "siginfo", handle_qxfer_siginfo
},
1181 { "spu", handle_qxfer_spu
},
1182 { "statictrace", handle_qxfer_statictrace
},
1183 { "threads", handle_qxfer_threads
},
1184 { "traceframe-info", handle_qxfer_traceframe_info
},
1188 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1196 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1199 /* Grab the object, r/w and annex. */
1200 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1202 write_enn (own_buf
);
1207 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1210 const struct qxfer
*q
= &qxfer_packets
[i
];
1212 if (strcmp (object
, q
->object
) == 0)
1214 if (strcmp (rw
, "read") == 0)
1216 unsigned char *data
;
1221 /* Grab the offset and length. */
1222 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1224 write_enn (own_buf
);
1228 /* Read one extra byte, as an indicator of whether there is
1230 if (len
> PBUFSIZ
- 2)
1232 data
= malloc (len
+ 1);
1235 write_enn (own_buf
);
1238 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1245 write_enn (own_buf
);
1247 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1249 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1254 else if (strcmp (rw
, "write") == 0)
1259 unsigned char *data
;
1261 strcpy (own_buf
, "E00");
1262 data
= malloc (packet_len
- (offset
- own_buf
));
1265 write_enn (own_buf
);
1268 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1269 &ofs
, &len
, data
) < 0)
1272 write_enn (own_buf
);
1276 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1283 write_enn (own_buf
);
1285 sprintf (own_buf
, "%x", n
);
1298 /* Table used by the crc32 function to calcuate the checksum. */
1300 static unsigned int crc32_table
[256] =
1303 /* Compute 32 bit CRC from inferior memory.
1305 On success, return 32 bit CRC.
1306 On failure, return (unsigned long long) -1. */
1308 static unsigned long long
1309 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1311 if (!crc32_table
[1])
1313 /* Initialize the CRC table and the decoding table. */
1317 for (i
= 0; i
< 256; i
++)
1319 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1320 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1327 unsigned char byte
= 0;
1329 /* Return failure if memory read fails. */
1330 if (read_inferior_memory (base
, &byte
, 1) != 0)
1331 return (unsigned long long) -1;
1333 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1336 return (unsigned long long) crc
;
1339 /* Handle all of the extended 'q' packets. */
1342 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1344 static struct inferior_list_entry
*thread_ptr
;
1346 /* Reply the current thread id. */
1347 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1350 require_running (own_buf
);
1352 if (!ptid_equal (general_thread
, null_ptid
)
1353 && !ptid_equal (general_thread
, minus_one_ptid
))
1354 gdb_id
= general_thread
;
1357 thread_ptr
= all_threads
.head
;
1358 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1361 sprintf (own_buf
, "QC");
1363 write_ptid (own_buf
, gdb_id
);
1367 if (strcmp ("qSymbol::", own_buf
) == 0)
1369 /* GDB is suggesting new symbols have been loaded. This may
1370 mean a new shared library has been detected as loaded, so
1371 take the opportunity to check if breakpoints we think are
1372 inserted, still are. Note that it isn't guaranteed that
1373 we'll see this when a shared library is loaded, and nor will
1374 we see this for unloads (although breakpoints in unloaded
1375 libraries shouldn't trigger), as GDB may not find symbols for
1376 the library at all. We also re-validate breakpoints when we
1377 see a second GDB breakpoint for the same address, and or when
1378 we access breakpoint shadows. */
1379 validate_breakpoints ();
1381 if (target_supports_tracepoints ())
1382 tracepoint_look_up_symbols ();
1384 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1385 (*the_target
->look_up_symbols
) ();
1387 strcpy (own_buf
, "OK");
1391 if (!disable_packet_qfThreadInfo
)
1393 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1397 require_running (own_buf
);
1398 thread_ptr
= all_threads
.head
;
1401 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1402 write_ptid (own_buf
, gdb_id
);
1403 thread_ptr
= thread_ptr
->next
;
1407 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1411 require_running (own_buf
);
1412 if (thread_ptr
!= NULL
)
1415 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1416 write_ptid (own_buf
, gdb_id
);
1417 thread_ptr
= thread_ptr
->next
;
1422 sprintf (own_buf
, "l");
1428 if (the_target
->read_offsets
!= NULL
1429 && strcmp ("qOffsets", own_buf
) == 0)
1431 CORE_ADDR text
, data
;
1433 require_running (own_buf
);
1434 if (the_target
->read_offsets (&text
, &data
))
1435 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1436 (long)text
, (long)data
, (long)data
);
1438 write_enn (own_buf
);
1443 /* Protocol features query. */
1444 if (strncmp ("qSupported", own_buf
, 10) == 0
1445 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1447 char *p
= &own_buf
[10];
1448 int gdb_supports_qRelocInsn
= 0;
1450 /* Start processing qSupported packet. */
1451 target_process_qsupported (NULL
);
1453 /* Process each feature being provided by GDB. The first
1454 feature will follow a ':', and latter features will follow
1458 char **qsupported
= NULL
;
1462 /* Two passes, to avoid nested strtok calls in
1463 target_process_qsupported. */
1464 for (p
= strtok (p
+ 1, ";");
1466 p
= strtok (NULL
, ";"))
1469 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1470 qsupported
[count
- 1] = xstrdup (p
);
1473 for (i
= 0; i
< count
; i
++)
1476 if (strcmp (p
, "multiprocess+") == 0)
1478 /* GDB supports and wants multi-process support if
1480 if (target_supports_multi_process ())
1483 else if (strcmp (p
, "qRelocInsn+") == 0)
1485 /* GDB supports relocate instruction requests. */
1486 gdb_supports_qRelocInsn
= 1;
1489 target_process_qsupported (p
);
1497 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1499 /* We do not have any hook to indicate whether the target backend
1500 supports qXfer:libraries:read, so always report it. */
1501 strcat (own_buf
, ";qXfer:libraries:read+");
1503 if (the_target
->read_auxv
!= NULL
)
1504 strcat (own_buf
, ";qXfer:auxv:read+");
1506 if (the_target
->qxfer_spu
!= NULL
)
1507 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1509 if (the_target
->qxfer_siginfo
!= NULL
)
1510 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1512 /* We always report qXfer:features:read, as targets may
1513 install XML files on a subsequent call to arch_setup.
1514 If we reported to GDB on startup that we don't support
1515 qXfer:feature:read at all, we will never be re-queried. */
1516 strcat (own_buf
, ";qXfer:features:read+");
1518 if (transport_is_reliable
)
1519 strcat (own_buf
, ";QStartNoAckMode+");
1521 if (the_target
->qxfer_osdata
!= NULL
)
1522 strcat (own_buf
, ";qXfer:osdata:read+");
1524 if (target_supports_multi_process ())
1525 strcat (own_buf
, ";multiprocess+");
1527 if (target_supports_non_stop ())
1528 strcat (own_buf
, ";QNonStop+");
1530 strcat (own_buf
, ";qXfer:threads:read+");
1532 if (target_supports_tracepoints ())
1534 strcat (own_buf
, ";ConditionalTracepoints+");
1535 strcat (own_buf
, ";TraceStateVariables+");
1536 strcat (own_buf
, ";TracepointSource+");
1537 strcat (own_buf
, ";DisconnectedTracing+");
1538 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1539 strcat (own_buf
, ";FastTracepoints+");
1540 strcat (own_buf
, ";StaticTracepoints+");
1541 strcat (own_buf
, ";qXfer:statictrace:read+");
1542 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1548 /* Thread-local storage support. */
1549 if (the_target
->get_tls_address
!= NULL
1550 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1552 char *p
= own_buf
+ 12;
1553 CORE_ADDR parts
[2], address
= 0;
1555 ptid_t ptid
= null_ptid
;
1557 require_running (own_buf
);
1559 for (i
= 0; i
< 3; i
++)
1567 p2
= strchr (p
, ',');
1580 ptid
= read_ptid (p
, NULL
);
1582 decode_address (&parts
[i
- 1], p
, len
);
1586 if (p
!= NULL
|| i
< 3)
1590 struct thread_info
*thread
= find_thread_ptid (ptid
);
1595 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1601 strcpy (own_buf
, paddress(address
));
1606 write_enn (own_buf
);
1610 /* Otherwise, pretend we do not understand this packet. */
1613 /* Windows OS Thread Information Block address support. */
1614 if (the_target
->get_tib_address
!= NULL
1615 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1620 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1622 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1625 strcpy (own_buf
, paddress(tlb
));
1630 write_enn (own_buf
);
1636 /* Handle "monitor" commands. */
1637 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1639 char *mon
= malloc (PBUFSIZ
);
1640 int len
= strlen (own_buf
+ 6);
1644 write_enn (own_buf
);
1648 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1650 write_enn (own_buf
);
1654 mon
[len
/ 2] = '\0';
1658 if (the_target
->handle_monitor_command
== NULL
1659 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1660 /* Default processing. */
1661 handle_monitor_command (mon
);
1667 if (strncmp ("qSearch:memory:", own_buf
,
1668 sizeof ("qSearch:memory:") - 1) == 0)
1670 require_running (own_buf
);
1671 handle_search_memory (own_buf
, packet_len
);
1675 if (strcmp (own_buf
, "qAttached") == 0
1676 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1678 struct process_info
*process
;
1680 if (own_buf
[sizeof ("qAttached") - 1])
1682 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1683 process
= (struct process_info
*)
1684 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1688 require_running (own_buf
);
1689 process
= current_process ();
1692 if (process
== NULL
)
1694 write_enn (own_buf
);
1698 strcpy (own_buf
, process
->attached
? "1" : "0");
1702 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1704 /* CRC check (compare-section). */
1708 unsigned long long crc
;
1710 require_running (own_buf
);
1711 base
= strtoul (own_buf
+ 5, &comma
, 16);
1712 if (*comma
++ != ',')
1714 write_enn (own_buf
);
1717 len
= strtoul (comma
, NULL
, 16);
1718 crc
= crc32 (base
, len
, 0xffffffff);
1719 /* Check for memory failure. */
1720 if (crc
== (unsigned long long) -1)
1722 write_enn (own_buf
);
1725 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1729 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1732 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1735 /* Otherwise we didn't know what packet it was. Say we didn't
1740 static void gdb_wants_all_threads_stopped (void);
1742 /* Parse vCont packets. */
1744 handle_v_cont (char *own_buf
)
1748 struct thread_resume
*resume_info
;
1749 struct thread_resume default_action
= {{0}};
1751 /* Count the number of semicolons in the packet. There should be one
1752 for every action. */
1758 p
= strchr (p
, ';');
1761 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1762 if (resume_info
== NULL
)
1770 if (p
[0] == 's' || p
[0] == 'S')
1771 resume_info
[i
].kind
= resume_step
;
1772 else if (p
[0] == 'c' || p
[0] == 'C')
1773 resume_info
[i
].kind
= resume_continue
;
1774 else if (p
[0] == 't')
1775 resume_info
[i
].kind
= resume_stop
;
1779 if (p
[0] == 'S' || p
[0] == 'C')
1782 sig
= strtol (p
+ 1, &q
, 16);
1787 if (!target_signal_to_host_p (sig
))
1789 resume_info
[i
].sig
= target_signal_to_host (sig
);
1793 resume_info
[i
].sig
= 0;
1799 resume_info
[i
].thread
= minus_one_ptid
;
1800 default_action
= resume_info
[i
];
1802 /* Note: we don't increment i here, we'll overwrite this entry
1803 the next time through. */
1805 else if (p
[0] == ':')
1807 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1812 if (p
[0] != ';' && p
[0] != 0)
1815 resume_info
[i
].thread
= ptid
;
1822 resume_info
[i
] = default_action
;
1824 /* Still used in occasional places in the backend. */
1826 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1827 && resume_info
[0].kind
!= resume_stop
)
1828 cont_thread
= resume_info
[0].thread
;
1830 cont_thread
= minus_one_ptid
;
1831 set_desired_inferior (0);
1836 (*the_target
->resume
) (resume_info
, n
);
1844 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1846 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1847 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1848 current_inferior
->last_status
= last_status
;
1850 /* From the client's perspective, all-stop mode always stops all
1851 threads implicitly (and the target backend has already done
1852 so by now). Tag all threads as "want-stopped", so we don't
1853 resume them implicitly without the client telling us to. */
1854 gdb_wants_all_threads_stopped ();
1855 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1856 disable_async_io ();
1858 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1859 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1860 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1865 write_enn (own_buf
);
1870 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1872 handle_v_attach (char *own_buf
)
1876 pid
= strtol (own_buf
+ 8, NULL
, 16);
1877 if (pid
!= 0 && attach_inferior (pid
) == 0)
1879 /* Don't report shared library events after attaching, even if
1880 some libraries are preloaded. GDB will always poll the
1881 library list. Avoids the "stopped by shared library event"
1882 notice on the GDB side. */
1887 /* In non-stop, we don't send a resume reply. Stop events
1888 will follow up using the normal notification
1893 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1899 write_enn (own_buf
);
1904 /* Run a new program. Return 1 if successful, 0 if failure. */
1906 handle_v_run (char *own_buf
)
1908 char *p
, *next_p
, **new_argv
;
1912 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1918 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1919 if (new_argv
== NULL
)
1921 write_enn (own_buf
);
1926 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1928 next_p
= strchr (p
, ';');
1930 next_p
= p
+ strlen (p
);
1932 if (i
== 0 && p
== next_p
)
1936 /* FIXME: Fail request if out of memory instead of dying. */
1937 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1938 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1939 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1948 if (new_argv
[0] == NULL
)
1950 /* GDB didn't specify a program to run. Use the program from the
1951 last run with the new argument list. */
1953 if (program_argv
== NULL
)
1955 write_enn (own_buf
);
1956 freeargv (new_argv
);
1960 new_argv
[0] = strdup (program_argv
[0]);
1961 if (new_argv
[0] == NULL
)
1963 write_enn (own_buf
);
1964 freeargv (new_argv
);
1969 /* Free the old argv and install the new one. */
1970 freeargv (program_argv
);
1971 program_argv
= new_argv
;
1973 start_inferior (program_argv
);
1974 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1976 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1978 /* In non-stop, sending a resume reply doesn't set the general
1979 thread, but GDB assumes a vRun sets it (this is so GDB can
1980 query which is the main thread of the new inferior. */
1982 general_thread
= last_ptid
;
1988 write_enn (own_buf
);
1993 /* Kill process. Return 1 if successful, 0 if failure. */
1995 handle_v_kill (char *own_buf
)
1998 char *p
= &own_buf
[6];
2000 pid
= strtol (p
, NULL
, 16);
2003 if (pid
!= 0 && kill_inferior (pid
) == 0)
2005 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2006 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2007 last_ptid
= pid_to_ptid (pid
);
2008 discard_queued_stop_replies (pid
);
2014 write_enn (own_buf
);
2019 /* Handle a 'vStopped' packet. */
2021 handle_v_stopped (char *own_buf
)
2023 /* If we're waiting for GDB to acknowledge a pending stop reply,
2024 consider that done. */
2027 struct vstop_notif
*head
;
2030 fprintf (stderr
, "vStopped: acking %s\n",
2031 target_pid_to_str (notif_queue
->ptid
));
2034 notif_queue
= notif_queue
->next
;
2038 /* Push another stop reply, or if there are no more left, an OK. */
2039 send_next_stop_reply (own_buf
);
2042 /* Handle all of the extended 'v' packets. */
2044 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2046 if (!disable_packet_vCont
)
2048 if (strncmp (own_buf
, "vCont;", 6) == 0)
2050 require_running (own_buf
);
2051 handle_v_cont (own_buf
);
2055 if (strncmp (own_buf
, "vCont?", 6) == 0)
2057 strcpy (own_buf
, "vCont;c;C;s;S;t");
2062 if (strncmp (own_buf
, "vFile:", 6) == 0
2063 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2066 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2068 if (!multi_process
&& target_running ())
2070 fprintf (stderr
, "Already debugging a process\n");
2071 write_enn (own_buf
);
2074 handle_v_attach (own_buf
);
2078 if (strncmp (own_buf
, "vRun;", 5) == 0)
2080 if (!multi_process
&& target_running ())
2082 fprintf (stderr
, "Already debugging a process\n");
2083 write_enn (own_buf
);
2086 handle_v_run (own_buf
);
2090 if (strncmp (own_buf
, "vKill;", 6) == 0)
2092 if (!target_running ())
2094 fprintf (stderr
, "No process to kill\n");
2095 write_enn (own_buf
);
2098 handle_v_kill (own_buf
);
2102 if (strncmp (own_buf
, "vStopped", 8) == 0)
2104 handle_v_stopped (own_buf
);
2108 /* Otherwise we didn't know what packet it was. Say we didn't
2114 /* Resume inferior and wait for another event. In non-stop mode,
2115 don't really wait here, but return immediatelly to the event
2118 myresume (char *own_buf
, int step
, int sig
)
2120 struct thread_resume resume_info
[2];
2122 int valid_cont_thread
;
2124 set_desired_inferior (0);
2126 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2127 && !ptid_equal (cont_thread
, minus_one_ptid
));
2129 if (step
|| sig
|| valid_cont_thread
)
2131 resume_info
[0].thread
2132 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2134 resume_info
[0].kind
= resume_step
;
2136 resume_info
[0].kind
= resume_continue
;
2137 resume_info
[0].sig
= sig
;
2141 if (!valid_cont_thread
)
2143 resume_info
[n
].thread
= minus_one_ptid
;
2144 resume_info
[n
].kind
= resume_continue
;
2145 resume_info
[n
].sig
= 0;
2152 (*the_target
->resume
) (resume_info
, n
);
2158 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2160 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2161 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2163 current_inferior
->last_resume_kind
= resume_stop
;
2164 current_inferior
->last_status
= last_status
;
2167 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2168 disable_async_io ();
2170 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2171 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2172 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2176 /* Callback for for_each_inferior. Make a new stop reply for each
2180 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2182 struct thread_info
*thread
= (struct thread_info
*) entry
;
2184 /* For now, assume targets that don't have this callback also don't
2185 manage the thread's last_status field. */
2186 if (the_target
->thread_stopped
== NULL
)
2188 /* Pass the last stop reply back to GDB, but don't notify
2190 queue_stop_reply (entry
->id
, &thread
->last_status
);
2194 if (thread_stopped (thread
))
2198 "Reporting thread %s as already stopped with %s\n",
2199 target_pid_to_str (entry
->id
),
2200 target_waitstatus_to_string (&thread
->last_status
));
2202 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2204 /* Pass the last stop reply back to GDB, but don't notify
2206 queue_stop_reply (entry
->id
, &thread
->last_status
);
2213 /* Set this inferior threads's state as "want-stopped". We won't
2214 resume this thread until the client gives us another action for
2218 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2220 struct thread_info
*thread
= (struct thread_info
*) entry
;
2222 thread
->last_resume_kind
= resume_stop
;
2224 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2226 /* Most threads are stopped implicitly (all-stop); tag that with
2228 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2229 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2233 /* Set all threads' states as "want-stopped". */
2236 gdb_wants_all_threads_stopped (void)
2238 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2241 /* Clear the gdb_detached flag of every process. */
2244 gdb_reattached_process (struct inferior_list_entry
*entry
)
2246 struct process_info
*process
= (struct process_info
*) entry
;
2248 process
->gdb_detached
= 0;
2251 /* Status handler for the '?' packet. */
2254 handle_status (char *own_buf
)
2256 /* GDB is connected, don't forward events to the target anymore. */
2257 for_each_inferior (&all_processes
, gdb_reattached_process
);
2259 /* In non-stop mode, we must send a stop reply for each stopped
2260 thread. In all-stop mode, just send one for the first stopped
2265 discard_queued_stop_replies (-1);
2266 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2268 /* The first is sent immediatly. OK is sent if there is no
2269 stopped thread, which is the same handling of the vStopped
2270 packet (by design). */
2271 send_next_stop_reply (own_buf
);
2276 stabilize_threads ();
2277 gdb_wants_all_threads_stopped ();
2279 if (all_threads
.head
)
2281 struct target_waitstatus status
;
2283 status
.kind
= TARGET_WAITKIND_STOPPED
;
2284 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2285 prepare_resume_reply (own_buf
,
2286 all_threads
.head
->id
, &status
);
2289 strcpy (own_buf
, "W00");
2294 gdbserver_version (void)
2296 printf ("GNU gdbserver %s%s\n"
2297 "Copyright (C) 2011 Free Software Foundation, Inc.\n"
2298 "gdbserver is free software, covered by the "
2299 "GNU General Public License.\n"
2300 "This gdbserver was configured as \"%s\"\n",
2301 PKGVERSION
, version
, host_name
);
2305 gdbserver_usage (FILE *stream
)
2307 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2308 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2309 "\tgdbserver [OPTIONS] --multi COMM\n"
2311 "COMM may either be a tty device (for serial debugging), or \n"
2312 "HOST:PORT to listen for a TCP connection.\n"
2315 " --debug Enable general debugging output.\n"
2316 " --remote-debug Enable remote protocol debugging output.\n"
2317 " --version Display version information and exit.\n"
2318 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2319 " --once Exit after the first connection has "
2321 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2322 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2326 gdbserver_show_disableable (FILE *stream
)
2328 fprintf (stream
, "Disableable packets:\n"
2329 " vCont \tAll vCont packets\n"
2330 " qC \tQuerying the current thread\n"
2331 " qfThreadInfo\tThread listing\n"
2332 " Tthread \tPassing the thread specifier in the "
2333 "T stop reply packet\n"
2334 " threads \tAll of the above\n");
2338 #undef require_running
2339 #define require_running(BUF) \
2340 if (!target_running ()) \
2347 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2349 int pid
= * (int *) args
;
2351 if (ptid_get_pid (entry
->id
) == pid
)
2358 kill_inferior_callback (struct inferior_list_entry
*entry
)
2360 struct process_info
*process
= (struct process_info
*) entry
;
2361 int pid
= ptid_get_pid (process
->head
.id
);
2363 kill_inferior (pid
);
2364 discard_queued_stop_replies (pid
);
2367 /* Callback for for_each_inferior to detach or kill the inferior,
2368 depending on whether we attached to it or not.
2369 We inform the user whether we're detaching or killing the process
2370 as this is only called when gdbserver is about to exit. */
2373 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2375 struct process_info
*process
= (struct process_info
*) entry
;
2376 int pid
= ptid_get_pid (process
->head
.id
);
2378 if (process
->attached
)
2379 detach_inferior (pid
);
2381 kill_inferior (pid
);
2383 discard_queued_stop_replies (pid
);
2386 /* for_each_inferior callback for detach_or_kill_for_exit to print
2387 the pids of started inferiors. */
2390 print_started_pid (struct inferior_list_entry
*entry
)
2392 struct process_info
*process
= (struct process_info
*) entry
;
2394 if (! process
->attached
)
2396 int pid
= ptid_get_pid (process
->head
.id
);
2397 fprintf (stderr
, " %d", pid
);
2401 /* for_each_inferior callback for detach_or_kill_for_exit to print
2402 the pids of attached inferiors. */
2405 print_attached_pid (struct inferior_list_entry
*entry
)
2407 struct process_info
*process
= (struct process_info
*) entry
;
2409 if (process
->attached
)
2411 int pid
= ptid_get_pid (process
->head
.id
);
2412 fprintf (stderr
, " %d", pid
);
2416 /* Call this when exiting gdbserver with possible inferiors that need
2417 to be killed or detached from. */
2420 detach_or_kill_for_exit (void)
2422 /* First print a list of the inferiors we will be killing/detaching.
2423 This is to assist the user, for example, in case the inferior unexpectedly
2424 dies after we exit: did we screw up or did the inferior exit on its own?
2425 Having this info will save some head-scratching. */
2427 if (have_started_inferiors_p ())
2429 fprintf (stderr
, "Killing process(es):");
2430 for_each_inferior (&all_processes
, print_started_pid
);
2431 fprintf (stderr
, "\n");
2433 if (have_attached_inferiors_p ())
2435 fprintf (stderr
, "Detaching process(es):");
2436 for_each_inferior (&all_processes
, print_attached_pid
);
2437 fprintf (stderr
, "\n");
2440 /* Now we can kill or detach the inferiors. */
2442 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2446 join_inferiors_callback (struct inferior_list_entry
*entry
)
2448 struct process_info
*process
= (struct process_info
*) entry
;
2450 /* If we are attached, then we can exit. Otherwise, we need to hang
2451 around doing nothing, until the child is gone. */
2452 if (!process
->attached
)
2453 join_inferior (ptid_get_pid (process
->head
.id
));
2457 main (int argc
, char *argv
[])
2461 char *arg_end
, *port
;
2462 char **next_arg
= &argv
[1];
2467 while (*next_arg
!= NULL
&& **next_arg
== '-')
2469 if (strcmp (*next_arg
, "--version") == 0)
2471 gdbserver_version ();
2474 else if (strcmp (*next_arg
, "--help") == 0)
2476 gdbserver_usage (stdout
);
2479 else if (strcmp (*next_arg
, "--attach") == 0)
2481 else if (strcmp (*next_arg
, "--multi") == 0)
2483 else if (strcmp (*next_arg
, "--wrapper") == 0)
2487 wrapper_argv
= next_arg
;
2488 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2491 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2493 gdbserver_usage (stderr
);
2497 /* Consume the "--". */
2500 else if (strcmp (*next_arg
, "--debug") == 0)
2502 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2504 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2506 gdbserver_show_disableable (stdout
);
2509 else if (strncmp (*next_arg
,
2510 "--disable-packet=",
2511 sizeof ("--disable-packet=") - 1) == 0)
2513 char *packets
, *tok
;
2515 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2516 for (tok
= strtok (packets
, ",");
2518 tok
= strtok (NULL
, ","))
2520 if (strcmp ("vCont", tok
) == 0)
2521 disable_packet_vCont
= 1;
2522 else if (strcmp ("Tthread", tok
) == 0)
2523 disable_packet_Tthread
= 1;
2524 else if (strcmp ("qC", tok
) == 0)
2525 disable_packet_qC
= 1;
2526 else if (strcmp ("qfThreadInfo", tok
) == 0)
2527 disable_packet_qfThreadInfo
= 1;
2528 else if (strcmp ("threads", tok
) == 0)
2530 disable_packet_vCont
= 1;
2531 disable_packet_Tthread
= 1;
2532 disable_packet_qC
= 1;
2533 disable_packet_qfThreadInfo
= 1;
2537 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2539 gdbserver_show_disableable (stderr
);
2544 else if (strcmp (*next_arg
, "--once") == 0)
2548 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2556 if (setjmp (toplevel
))
2558 fprintf (stderr
, "Exiting\n");
2564 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2566 gdbserver_usage (stderr
);
2573 /* --attach used to come after PORT, so allow it there for
2575 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2582 && (*next_arg
== NULL
2583 || (*next_arg
)[0] == '\0'
2584 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2586 || next_arg
[1] != NULL
))
2591 gdbserver_usage (stderr
);
2595 initialize_inferiors ();
2596 initialize_async_io ();
2598 if (target_supports_tracepoints ())
2599 initialize_tracepoint ();
2601 own_buf
= xmalloc (PBUFSIZ
+ 1);
2602 mem_buf
= xmalloc (PBUFSIZ
);
2604 if (pid
== 0 && *next_arg
!= NULL
)
2608 n
= argc
- (next_arg
- argv
);
2609 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2610 for (i
= 0; i
< n
; i
++)
2611 program_argv
[i
] = xstrdup (next_arg
[i
]);
2612 program_argv
[i
] = NULL
;
2614 /* Wait till we are at first instruction in program. */
2615 start_inferior (program_argv
);
2617 /* We are now (hopefully) stopped at the first instruction of
2618 the target process. This assumes that the target process was
2619 successfully created. */
2623 if (attach_inferior (pid
) == -1)
2624 error ("Attaching not supported on this target");
2626 /* Otherwise succeeded. */
2630 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2631 last_status
.value
.integer
= 0;
2632 last_ptid
= minus_one_ptid
;
2635 /* Don't report shared library events on the initial connection,
2636 even if some libraries are preloaded. Avoids the "stopped by
2637 shared library event" notice on gdb side. */
2640 if (setjmp (toplevel
))
2642 detach_or_kill_for_exit ();
2646 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2647 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2652 if (!was_running
&& !multi_mode
)
2654 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2658 remote_prepare (port
);
2664 /* Be sure we're out of tfind mode. */
2665 current_traceframe
= -1;
2669 if (setjmp (toplevel
) != 0)
2671 /* An error occurred. */
2672 if (response_needed
)
2674 write_enn (own_buf
);
2679 /* Wait for events. This will return when all event sources are
2680 removed from the event loop. */
2681 start_event_loop ();
2683 /* If an exit was requested (using the "monitor exit" command),
2684 terminate now. The only other way to get here is for
2685 getpkt to fail; close the connection and reopen it at the
2688 if (exit_requested
|| run_once
)
2690 detach_or_kill_for_exit ();
2695 "Remote side has terminated connection. "
2696 "GDBserver will reopen the connection.\n");
2700 if (disconnected_tracing
)
2702 /* Try to enable non-stop/async mode, so we we can both
2703 wait for an async socket accept, and handle async
2704 target events simultaneously. There's also no point
2705 either in having the target always stop all threads,
2706 when we're going to pass signals down without
2710 if (start_non_stop (1))
2713 /* Detaching implicitly resumes all threads; simply
2714 disconnecting does not. */
2720 "Disconnected tracing disabled; stopping trace run.\n");
2727 /* Event loop callback that handles a serial event. The first byte in
2728 the serial buffer gets us here. We expect characters to arrive at
2729 a brisk pace, so we read the rest of the packet with a blocking
2733 process_serial_event (void)
2744 int new_packet_len
= -1;
2746 /* Used to decide when gdbserver should exit in
2747 multi-mode/remote. */
2748 static int have_ran
= 0;
2751 have_ran
= target_running ();
2753 disable_async_io ();
2755 response_needed
= 0;
2756 packet_len
= getpkt (own_buf
);
2757 if (packet_len
<= 0)
2760 /* Force an event loop break. */
2763 response_needed
= 1;
2770 handle_query (own_buf
, packet_len
, &new_packet_len
);
2773 handle_general_set (own_buf
);
2776 require_running (own_buf
);
2781 pid
= strtol (&own_buf
[i
], NULL
, 16);
2785 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2787 if (tracing
&& disconnected_tracing
)
2789 struct thread_resume resume_info
;
2790 struct process_info
*process
= find_process_pid (pid
);
2792 if (process
== NULL
)
2794 write_enn (own_buf
);
2799 "Disconnected tracing in effect, "
2800 "leaving gdbserver attached to the process\n");
2802 /* Make sure we're in non-stop/async mode, so we we can both
2803 wait for an async socket accept, and handle async target
2804 events simultaneously. There's also no point either in
2805 having the target stop all threads, when we're going to
2806 pass signals down without informing GDB. */
2810 fprintf (stderr
, "Forcing non-stop mode\n");
2816 process
->gdb_detached
= 1;
2818 /* Detaching implicitly resumes all threads. */
2819 resume_info
.thread
= minus_one_ptid
;
2820 resume_info
.kind
= resume_continue
;
2821 resume_info
.sig
= 0;
2822 (*the_target
->resume
) (&resume_info
, 1);
2825 break; /* from switch/case */
2828 fprintf (stderr
, "Detaching from process %d\n", pid
);
2830 if (detach_inferior (pid
) != 0)
2831 write_enn (own_buf
);
2834 discard_queued_stop_replies (pid
);
2837 if (extended_protocol
)
2839 /* Treat this like a normal program exit. */
2840 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2841 last_status
.value
.integer
= 0;
2842 last_ptid
= pid_to_ptid (pid
);
2844 current_inferior
= NULL
;
2851 /* If we are attached, then we can exit. Otherwise, we
2852 need to hang around doing nothing, until the child is
2854 for_each_inferior (&all_processes
,
2855 join_inferiors_callback
);
2861 extended_protocol
= 1;
2865 handle_status (own_buf
);
2868 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2870 ptid_t gdb_id
, thread_id
;
2873 require_running (own_buf
);
2875 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2877 pid
= ptid_get_pid (gdb_id
);
2879 if (ptid_equal (gdb_id
, null_ptid
)
2880 || ptid_equal (gdb_id
, minus_one_ptid
))
2881 thread_id
= null_ptid
;
2883 && ptid_equal (pid_to_ptid (pid
),
2886 struct thread_info
*thread
=
2887 (struct thread_info
*) find_inferior (&all_threads
,
2892 write_enn (own_buf
);
2896 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2900 thread_id
= gdb_id_to_thread_id (gdb_id
);
2901 if (ptid_equal (thread_id
, null_ptid
))
2903 write_enn (own_buf
);
2908 if (own_buf
[1] == 'g')
2910 if (ptid_equal (thread_id
, null_ptid
))
2912 /* GDB is telling us to choose any thread. Check if
2913 the currently selected thread is still valid. If
2914 it is not, select the first available. */
2915 struct thread_info
*thread
=
2916 (struct thread_info
*) find_inferior_id (&all_threads
,
2919 thread_id
= all_threads
.head
->id
;
2922 general_thread
= thread_id
;
2923 set_desired_inferior (1);
2925 else if (own_buf
[1] == 'c')
2926 cont_thread
= thread_id
;
2927 else if (own_buf
[1] == 's')
2928 step_thread
= thread_id
;
2934 /* Silently ignore it so that gdb can extend the protocol
2935 without compatibility headaches. */
2940 require_running (own_buf
);
2941 if (current_traceframe
>= 0)
2943 struct regcache
*regcache
= new_register_cache ();
2945 if (fetch_traceframe_registers (current_traceframe
,
2947 registers_to_string (regcache
, own_buf
);
2949 write_enn (own_buf
);
2950 free_register_cache (regcache
);
2954 struct regcache
*regcache
;
2956 set_desired_inferior (1);
2957 regcache
= get_thread_regcache (current_inferior
, 1);
2958 registers_to_string (regcache
, own_buf
);
2962 require_running (own_buf
);
2963 if (current_traceframe
>= 0)
2964 write_enn (own_buf
);
2967 struct regcache
*regcache
;
2969 set_desired_inferior (1);
2970 regcache
= get_thread_regcache (current_inferior
, 1);
2971 registers_from_string (regcache
, &own_buf
[1]);
2976 require_running (own_buf
);
2977 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2978 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
2980 write_enn (own_buf
);
2982 convert_int_to_ascii (mem_buf
, own_buf
, res
);
2985 require_running (own_buf
);
2986 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2987 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2990 write_enn (own_buf
);
2993 require_running (own_buf
);
2994 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2995 &mem_addr
, &len
, &mem_buf
) < 0
2996 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2997 write_enn (own_buf
);
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
, 0, signal
);
3011 require_running (own_buf
);
3012 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3013 if (target_signal_to_host_p (sig
))
3014 signal
= target_signal_to_host (sig
);
3017 myresume (own_buf
, 1, signal
);
3020 require_running (own_buf
);
3022 myresume (own_buf
, 0, signal
);
3025 require_running (own_buf
);
3027 myresume (own_buf
, 1, signal
);
3029 case 'Z': /* insert_ ... */
3031 case 'z': /* remove_ ... */
3035 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3036 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3037 char type
= own_buf
[1];
3039 const int insert
= ch
== 'Z';
3041 /* Default to unrecognized/unsupported. */
3045 case '0': /* software-breakpoint */
3046 case '1': /* hardware-breakpoint */
3047 case '2': /* write watchpoint */
3048 case '3': /* read watchpoint */
3049 case '4': /* access watchpoint */
3050 require_running (own_buf
);
3051 if (insert
&& the_target
->insert_point
!= NULL
)
3052 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3053 else if (!insert
&& the_target
->remove_point
!= NULL
)
3054 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3066 write_enn (own_buf
);
3070 response_needed
= 0;
3071 if (!target_running ())
3072 /* The packet we received doesn't make sense - but we can't
3073 reply to it, either. */
3076 fprintf (stderr
, "Killing all inferiors\n");
3077 for_each_inferior (&all_processes
, kill_inferior_callback
);
3079 /* When using the extended protocol, we wait with no program
3080 running. The traditional protocol will exit instead. */
3081 if (extended_protocol
)
3083 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3084 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3092 ptid_t gdb_id
, thread_id
;
3094 require_running (own_buf
);
3096 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3097 thread_id
= gdb_id_to_thread_id (gdb_id
);
3098 if (ptid_equal (thread_id
, null_ptid
))
3100 write_enn (own_buf
);
3104 if (mythread_alive (thread_id
))
3107 write_enn (own_buf
);
3111 response_needed
= 0;
3113 /* Restarting the inferior is only supported in the extended
3115 if (extended_protocol
)
3117 if (target_running ())
3118 for_each_inferior (&all_processes
,
3119 kill_inferior_callback
);
3120 fprintf (stderr
, "GDBserver restarting\n");
3122 /* Wait till we are at 1st instruction in prog. */
3123 if (program_argv
!= NULL
)
3124 start_inferior (program_argv
);
3127 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3128 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3134 /* It is a request we don't understand. Respond with an
3135 empty packet so that gdb knows that we don't support this
3141 /* Extended (long) request. */
3142 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3146 /* It is a request we don't understand. Respond with an empty
3147 packet so that gdb knows that we don't support this
3153 if (new_packet_len
!= -1)
3154 putpkt_binary (own_buf
, new_packet_len
);
3158 response_needed
= 0;
3160 if (!extended_protocol
&& have_ran
&& !target_running ())
3162 /* In non-stop, defer exiting until GDB had a chance to query
3163 the whole vStopped list (until it gets an OK). */
3166 fprintf (stderr
, "GDBserver exiting\n");
3178 /* Event-loop callback for serial events. */
3181 handle_serial_event (int err
, gdb_client_data client_data
)
3184 fprintf (stderr
, "handling possible serial event\n");
3186 /* Really handle it. */
3187 if (process_serial_event () < 0)
3190 /* Be sure to not change the selected inferior behind GDB's back.
3191 Important in the non-stop mode asynchronous protocol. */
3192 set_desired_inferior (1);
3197 /* Event-loop callback for target events. */
3200 handle_target_event (int err
, gdb_client_data client_data
)
3203 fprintf (stderr
, "handling possible target event\n");
3205 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3208 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3210 int pid
= ptid_get_pid (last_ptid
);
3211 struct process_info
*process
= find_process_pid (pid
);
3212 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3214 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3215 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3217 mark_breakpoints_out (process
);
3218 mourn_inferior (process
);
3222 /* We're reporting this thread as stopped. Update its
3223 "want-stopped" state to what the client wants, until it
3224 gets a new resume action. */
3225 current_inferior
->last_resume_kind
= resume_stop
;
3226 current_inferior
->last_status
= last_status
;
3231 if (!target_running ())
3233 /* The last process exited. We're done. */
3237 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3239 /* A thread stopped with a signal, but gdb isn't
3240 connected to handle it. Pass it down to the
3241 inferior, as if it wasn't being traced. */
3242 struct thread_resume resume_info
;
3246 "GDB not connected; forwarding event %d for [%s]\n",
3247 (int) last_status
.kind
,
3248 target_pid_to_str (last_ptid
));
3250 resume_info
.thread
= last_ptid
;
3251 resume_info
.kind
= resume_continue
;
3252 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3253 (*the_target
->resume
) (&resume_info
, 1);
3255 else if (debug_threads
)
3256 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3257 (int) last_status
.kind
,
3258 target_pid_to_str (last_ptid
));
3262 /* Something interesting. Tell GDB about it. */
3263 push_event (last_ptid
, &last_status
);
3267 /* Be sure to not change the selected inferior behind GDB's back.
3268 Important in the non-stop mode asynchronous protocol. */
3269 set_desired_inferior (1);