1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbthread.h"
32 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
33 `vCont'. Note the multi-process extensions made `vCont' a
34 requirement, so `Hc pPID.TID' is pretty much undefined. So
35 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
36 resuming all threads of the process (again, `Hc' isn't used for
37 multi-process), or a specific thread ptid_t.
39 We also set this when handling a single-thread `vCont' resume, as
40 some places in the backends check it to know when (and for which
41 thread) single-thread scheduler-locking is in effect. */
44 /* The thread set with an `Hg' packet. */
45 ptid_t general_thread
;
49 static int extended_protocol
;
50 static int response_needed
;
51 static int exit_requested
;
53 /* --once: Exit after the first connection has closed. */
59 /* Whether we should attempt to disable the operating system's address
60 space randomization feature before starting an inferior. */
61 int disable_randomization
= 1;
63 static char **program_argv
, **wrapper_argv
;
65 /* Enable miscellaneous debugging output. The name is historical - it
66 was originally used to debug LinuxThreads support. */
69 /* Enable debugging of h/w breakpoint/watchpoint support. */
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
78 const char *gdbserver_xmltarget
;
80 /* The PID of the originally created or attached inferior. Used to
81 send signals to the process when GDB sends us an asynchronous interrupt
82 (user hitting Control-C in the client), and to wait for the child to exit
83 when no longer debugging it. */
85 unsigned long signal_pid
;
88 /* A file descriptor for the controlling terminal. */
91 /* TERMINAL_FD's original foreground group. */
92 pid_t old_foreground_pgrp
;
94 /* Hand back terminal ownership to the original foreground group. */
97 restore_old_foreground_pgrp (void)
99 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
103 /* Set if you want to disable optional thread related packets support
104 in gdbserver, for the sake of testing GDB against stubs that don't
106 int disable_packet_vCont
;
107 int disable_packet_Tthread
;
108 int disable_packet_qC
;
109 int disable_packet_qfThreadInfo
;
111 /* Last status reported to GDB. */
112 static struct target_waitstatus last_status
;
113 static ptid_t last_ptid
;
115 static char *own_buf
;
116 static unsigned char *mem_buf
;
118 /* Structure holding information relative to a single stop reply. We
119 keep a queue of these (really a singly-linked list) to push to GDB
123 /* Pointer to next in list. */
124 struct vstop_notif
*next
;
126 /* Thread or process that got the event. */
130 struct target_waitstatus status
;
133 /* The pending stop replies list head. */
134 static struct vstop_notif
*notif_queue
= NULL
;
136 /* Put a stop reply to the stop reply queue. */
139 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
141 struct vstop_notif
*new_notif
;
143 new_notif
= xmalloc (sizeof (*new_notif
));
144 new_notif
->next
= NULL
;
145 new_notif
->ptid
= ptid
;
146 new_notif
->status
= *status
;
150 struct vstop_notif
*tail
;
151 for (tail
= notif_queue
;
155 tail
->next
= new_notif
;
158 notif_queue
= new_notif
;
163 struct vstop_notif
*n
;
165 for (n
= notif_queue
; n
; n
= n
->next
)
168 fprintf (stderr
, "pending stop replies: %d\n", i
);
172 /* Place an event in the stop reply queue, and push a notification if
173 we aren't sending one yet. */
176 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
178 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
180 queue_stop_reply (ptid
, status
);
182 /* If this is the first stop reply in the queue, then inform GDB
183 about it, by sending a Stop notification. */
184 if (notif_queue
->next
== NULL
)
189 prepare_resume_reply (p
,
190 notif_queue
->ptid
, ¬if_queue
->status
);
191 putpkt_notif (own_buf
);
195 /* Get rid of the currently pending stop replies for PID. If PID is
196 -1, then apply to all processes. */
199 discard_queued_stop_replies (int pid
)
201 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
203 for (reply
= notif_queue
; reply
; reply
= next
)
208 || ptid_get_pid (reply
->ptid
) == pid
)
210 if (reply
== notif_queue
)
213 prev
->next
= reply
->next
;
222 /* If there are more stop replies to push, push one now. */
225 send_next_stop_reply (char *own_buf
)
228 prepare_resume_reply (own_buf
,
230 ¬if_queue
->status
);
236 target_running (void)
238 return all_threads
.head
!= NULL
;
242 start_inferior (char **argv
)
244 char **new_argv
= argv
;
246 if (wrapper_argv
!= NULL
)
250 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
252 for (i
= 0; argv
[i
] != NULL
; i
++)
254 new_argv
= alloca (sizeof (char *) * count
);
256 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
257 new_argv
[count
++] = wrapper_argv
[i
];
258 for (i
= 0; argv
[i
] != NULL
; i
++)
259 new_argv
[count
++] = argv
[i
];
260 new_argv
[count
] = NULL
;
266 for (i
= 0; new_argv
[i
]; ++i
)
267 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
272 signal (SIGTTOU
, SIG_DFL
);
273 signal (SIGTTIN
, SIG_DFL
);
276 /* Clear this so the backend doesn't get confused, thinking
277 CONT_THREAD died, and it needs to resume all threads. */
278 cont_thread
= null_ptid
;
280 signal_pid
= create_inferior (new_argv
[0], new_argv
);
282 /* FIXME: we don't actually know at this point that the create
283 actually succeeded. We won't know that until we wait. */
284 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
289 signal (SIGTTOU
, SIG_IGN
);
290 signal (SIGTTIN
, SIG_IGN
);
291 terminal_fd
= fileno (stderr
);
292 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
293 tcsetpgrp (terminal_fd
, signal_pid
);
294 atexit (restore_old_foreground_pgrp
);
297 if (wrapper_argv
!= NULL
)
299 struct thread_resume resume_info
;
301 resume_info
.thread
= pid_to_ptid (signal_pid
);
302 resume_info
.kind
= resume_continue
;
305 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
307 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
312 (*the_target
->resume
) (&resume_info
, 1);
314 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
315 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
318 current_inferior
->last_resume_kind
= resume_stop
;
319 current_inferior
->last_status
= last_status
;
321 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
326 /* Wait till we are at 1st instruction in program, return new pid
327 (assuming success). */
328 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
330 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
331 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
333 current_inferior
->last_resume_kind
= resume_stop
;
334 current_inferior
->last_status
= last_status
;
341 attach_inferior (int pid
)
343 /* myattach should return -1 if attaching is unsupported,
344 0 if it succeeded, and call error() otherwise. */
346 if (myattach (pid
) != 0)
349 fprintf (stderr
, "Attached; pid = %d\n", pid
);
352 /* FIXME - It may be that we should get the SIGNAL_PID from the
353 attach function, so that it can be the main thread instead of
354 whichever we were told to attach to. */
357 /* Clear this so the backend doesn't get confused, thinking
358 CONT_THREAD died, and it needs to resume all threads. */
359 cont_thread
= null_ptid
;
363 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
365 /* GDB knows to ignore the first SIGSTOP after attaching to a running
366 process using the "attach" command, but this is different; it's
367 just using "target remote". Pretend it's just starting up. */
368 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
369 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
370 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
372 current_inferior
->last_resume_kind
= resume_stop
;
373 current_inferior
->last_status
= last_status
;
379 extern int remote_debug
;
381 /* Decode a qXfer read request. Return 0 if everything looks OK,
385 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
387 /* After the read marker and annex, qXfer looks like a
388 traditional 'm' packet. */
389 decode_m_packet (buf
, ofs
, len
);
395 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
397 /* Extract and NUL-terminate the object. */
399 while (*buf
&& *buf
!= ':')
405 /* Extract and NUL-terminate the read/write action. */
407 while (*buf
&& *buf
!= ':')
413 /* Extract and NUL-terminate the annex. */
415 while (*buf
&& *buf
!= ':')
425 /* Write the response to a successful qXfer read. Returns the
426 length of the (binary) data stored in BUF, corresponding
427 to as much of DATA/LEN as we could fit. IS_MORE controls
428 the first character of the response. */
430 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
439 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
443 /* Handle all of the extended 'Q' packets. */
446 handle_general_set (char *own_buf
)
448 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
450 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
451 const char *p
= own_buf
+ strlen ("QPassSignals:");
454 p
= decode_address_to_semicolon (&cursig
, p
);
455 for (i
= 0; i
< numsigs
; i
++)
461 /* Keep looping, to clear the remaining signals. */
464 p
= decode_address_to_semicolon (&cursig
, p
);
469 strcpy (own_buf
, "OK");
473 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
475 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
476 const char *p
= own_buf
+ strlen ("QProgramSignals:");
479 program_signals_p
= 1;
481 p
= decode_address_to_semicolon (&cursig
, p
);
482 for (i
= 0; i
< numsigs
; i
++)
486 program_signals
[i
] = 1;
488 /* Keep looping, to clear the remaining signals. */
491 p
= decode_address_to_semicolon (&cursig
, p
);
494 program_signals
[i
] = 0;
496 strcpy (own_buf
, "OK");
500 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
504 fprintf (stderr
, "[noack mode enabled]\n");
513 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
515 char *mode
= own_buf
+ 9;
519 if (strcmp (mode
, "0") == 0)
521 else if (strcmp (mode
, "1") == 0)
525 /* We don't know what this mode is, so complain to
527 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
533 req_str
= req
? "non-stop" : "all-stop";
534 if (start_non_stop (req
) != 0)
536 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
544 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
550 if (strncmp ("QDisableRandomization:", own_buf
,
551 strlen ("QDisableRandomization:")) == 0)
553 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
556 unpack_varlen_hex (packet
, &setting
);
557 disable_randomization
= setting
;
561 if (disable_randomization
)
562 fprintf (stderr
, "[address space randomization disabled]\n");
564 fprintf (stderr
, "[address space randomization enabled]\n");
571 if (target_supports_tracepoints ()
572 && handle_tracepoint_general_set (own_buf
))
575 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
577 char *mode
= own_buf
+ strlen ("QAgent:");
580 if (strcmp (mode
, "0") == 0)
582 else if (strcmp (mode
, "1") == 0)
586 /* We don't know what this value is, so complain to GDB. */
587 sprintf (own_buf
, "E.Unknown QAgent value");
591 /* Update the flag. */
594 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
599 /* Otherwise we didn't know what packet it was. Say we didn't
605 get_features_xml (const char *annex
)
607 /* gdbserver_xmltarget defines what to return when looking
608 for the "target.xml" file. Its contents can either be
609 verbatim XML code (prefixed with a '@') or else the name
610 of the actual XML file to be used in place of "target.xml".
612 This variable is set up from the auto-generated
613 init_registers_... routine for the current target. */
615 if (gdbserver_xmltarget
616 && strcmp (annex
, "target.xml") == 0)
618 if (*gdbserver_xmltarget
== '@')
619 return gdbserver_xmltarget
+ 1;
621 annex
= gdbserver_xmltarget
;
626 extern const char *const xml_builtin
[][2];
629 /* Look for the annex. */
630 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
631 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
634 if (xml_builtin
[i
][0] != NULL
)
635 return xml_builtin
[i
][1];
643 monitor_show_help (void)
645 monitor_output ("The following monitor commands are supported:\n");
646 monitor_output (" set debug <0|1>\n");
647 monitor_output (" Enable general debugging messages\n");
648 monitor_output (" set debug-hw-points <0|1>\n");
649 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
650 monitor_output (" set remote-debug <0|1>\n");
651 monitor_output (" Enable remote protocol debugging messages\n");
652 monitor_output (" exit\n");
653 monitor_output (" Quit GDBserver\n");
656 /* Read trace frame or inferior memory. Returns the number of bytes
657 actually read, zero when no further transfer is possible, and -1 on
658 error. Return of a positive value smaller than LEN does not
659 indicate there's no more to be read, only the end of the transfer.
660 E.g., when GDB reads memory from a traceframe, a first request may
661 be served from a memory block that does not cover the whole request
662 length. A following request gets the rest served from either
663 another block (of the same traceframe) or from the read-only
667 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
671 if (current_traceframe
>= 0)
674 ULONGEST length
= len
;
676 if (traceframe_read_mem (current_traceframe
,
677 memaddr
, myaddr
, len
, &nbytes
))
679 /* Data read from trace buffer, we're done. */
682 if (!in_readonly_region (memaddr
, length
))
684 /* Otherwise we have a valid readonly case, fall through. */
685 /* (assume no half-trace half-real blocks for now) */
688 res
= prepare_to_access_memory ();
691 res
= read_inferior_memory (memaddr
, myaddr
, len
);
692 done_accessing_memory ();
694 return res
== 0 ? len
: -1;
700 /* Write trace frame or inferior memory. Actually, writing to trace
701 frames is forbidden. */
704 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
706 if (current_traceframe
>= 0)
712 ret
= prepare_to_access_memory ();
715 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
716 done_accessing_memory ();
722 /* Subroutine of handle_search_memory to simplify it. */
725 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
726 gdb_byte
*pattern
, unsigned pattern_len
,
727 gdb_byte
*search_buf
,
728 unsigned chunk_size
, unsigned search_buf_size
,
729 CORE_ADDR
*found_addrp
)
731 /* Prime the search buffer. */
733 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
736 warning ("Unable to access %ld bytes of target "
737 "memory at 0x%lx, halting search.",
738 (long) search_buf_size
, (long) start_addr
);
742 /* Perform the search.
744 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
745 When we've scanned N bytes we copy the trailing bytes to the start and
746 read in another N bytes. */
748 while (search_space_len
>= pattern_len
)
751 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
755 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
757 if (found_ptr
!= NULL
)
759 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
760 *found_addrp
= found_addr
;
764 /* Not found in this chunk, skip to next chunk. */
766 /* Don't let search_space_len wrap here, it's unsigned. */
767 if (search_space_len
>= chunk_size
)
768 search_space_len
-= chunk_size
;
770 search_space_len
= 0;
772 if (search_space_len
>= pattern_len
)
774 unsigned keep_len
= search_buf_size
- chunk_size
;
775 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
778 /* Copy the trailing part of the previous iteration to the front
779 of the buffer for the next iteration. */
780 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
782 nr_to_read
= (search_space_len
- keep_len
< chunk_size
783 ? search_space_len
- keep_len
786 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
787 nr_to_read
) != search_buf_size
)
789 warning ("Unable to access %ld bytes of target memory "
790 "at 0x%lx, halting search.",
791 (long) nr_to_read
, (long) read_addr
);
795 start_addr
+= chunk_size
;
804 /* Handle qSearch:memory packets. */
807 handle_search_memory (char *own_buf
, int packet_len
)
809 CORE_ADDR start_addr
;
810 CORE_ADDR search_space_len
;
812 unsigned int pattern_len
;
813 /* NOTE: also defined in find.c testcase. */
814 #define SEARCH_CHUNK_SIZE 16000
815 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
816 /* Buffer to hold memory contents for searching. */
817 gdb_byte
*search_buf
;
818 unsigned search_buf_size
;
820 CORE_ADDR found_addr
;
821 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
823 pattern
= malloc (packet_len
);
826 error ("Unable to allocate memory to perform the search");
827 strcpy (own_buf
, "E00");
830 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
831 packet_len
- cmd_name_len
,
832 &start_addr
, &search_space_len
,
833 pattern
, &pattern_len
) < 0)
836 error ("Error in parsing qSearch:memory packet");
837 strcpy (own_buf
, "E00");
841 search_buf_size
= chunk_size
+ pattern_len
- 1;
843 /* No point in trying to allocate a buffer larger than the search space. */
844 if (search_space_len
< search_buf_size
)
845 search_buf_size
= search_space_len
;
847 search_buf
= malloc (search_buf_size
);
848 if (search_buf
== NULL
)
851 error ("Unable to allocate memory to perform the search");
852 strcpy (own_buf
, "E00");
856 found
= handle_search_memory_1 (start_addr
, search_space_len
,
857 pattern
, pattern_len
,
858 search_buf
, chunk_size
, search_buf_size
,
862 sprintf (own_buf
, "1,%lx", (long) found_addr
);
864 strcpy (own_buf
, "0");
866 strcpy (own_buf
, "E00");
872 #define require_running(BUF) \
873 if (!target_running ()) \
879 /* Handle monitor commands not handled by target-specific handlers. */
882 handle_monitor_command (char *mon
, char *own_buf
)
884 if (strcmp (mon
, "set debug 1") == 0)
887 monitor_output ("Debug output enabled.\n");
889 else if (strcmp (mon
, "set debug 0") == 0)
892 monitor_output ("Debug output disabled.\n");
894 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
897 monitor_output ("H/W point debugging output enabled.\n");
899 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
902 monitor_output ("H/W point debugging output disabled.\n");
904 else if (strcmp (mon
, "set remote-debug 1") == 0)
907 monitor_output ("Protocol debug output enabled.\n");
909 else if (strcmp (mon
, "set remote-debug 0") == 0)
912 monitor_output ("Protocol debug output disabled.\n");
914 else if (strcmp (mon
, "help") == 0)
915 monitor_show_help ();
916 else if (strcmp (mon
, "exit") == 0)
920 monitor_output ("Unknown monitor command.\n\n");
921 monitor_show_help ();
926 /* Associates a callback with each supported qXfer'able object. */
930 /* The object this handler handles. */
933 /* Request that the target transfer up to LEN 8-bit bytes of the
934 target's OBJECT. The OFFSET, for a seekable object, specifies
935 the starting point. The ANNEX can be used to provide additional
936 data-specific information to the target.
938 Return the number of bytes actually transfered, zero when no
939 further transfer is possible, -1 on error, and -2 when the
940 transfer is not supported. Return of a positive value smaller
941 than LEN does not indicate the end of the object, only the end of
944 One, and only one, of readbuf or writebuf must be non-NULL. */
945 int (*xfer
) (const char *annex
,
946 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
947 ULONGEST offset
, LONGEST len
);
950 /* Handle qXfer:auxv:read. */
953 handle_qxfer_auxv (const char *annex
,
954 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
955 ULONGEST offset
, LONGEST len
)
957 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
960 if (annex
[0] != '\0' || !target_running ())
963 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
966 /* Handle qXfer:features:read. */
969 handle_qxfer_features (const char *annex
,
970 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
971 ULONGEST offset
, LONGEST len
)
973 const char *document
;
976 if (writebuf
!= NULL
)
979 if (!target_running ())
982 /* Grab the correct annex. */
983 document
= get_features_xml (annex
);
984 if (document
== NULL
)
987 total_len
= strlen (document
);
989 if (offset
> total_len
)
992 if (offset
+ len
> total_len
)
993 len
= total_len
- offset
;
995 memcpy (readbuf
, document
+ offset
, len
);
999 /* Handle qXfer:libraries:read. */
1002 handle_qxfer_libraries (const char *annex
,
1003 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1004 ULONGEST offset
, LONGEST len
)
1006 unsigned int total_len
;
1008 struct inferior_list_entry
*dll_ptr
;
1010 if (writebuf
!= NULL
)
1013 if (annex
[0] != '\0' || !target_running ())
1016 /* Over-estimate the necessary memory. Assume that every character
1017 in the library name must be escaped. */
1019 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1020 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1022 document
= malloc (total_len
);
1023 if (document
== NULL
)
1026 strcpy (document
, "<library-list>\n");
1027 p
= document
+ strlen (document
);
1029 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1031 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1034 strcpy (p
, " <library name=\"");
1036 name
= xml_escape_text (dll
->name
);
1040 strcpy (p
, "\"><segment address=\"");
1042 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1044 strcpy (p
, "\"/></library>\n");
1048 strcpy (p
, "</library-list>\n");
1050 total_len
= strlen (document
);
1052 if (offset
> total_len
)
1058 if (offset
+ len
> total_len
)
1059 len
= total_len
- offset
;
1061 memcpy (readbuf
, document
+ offset
, len
);
1066 /* Handle qXfer:libraries-svr4:read. */
1069 handle_qxfer_libraries_svr4 (const char *annex
,
1070 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1071 ULONGEST offset
, LONGEST len
)
1073 if (writebuf
!= NULL
)
1076 if (annex
[0] != '\0' || !target_running ()
1077 || the_target
->qxfer_libraries_svr4
== NULL
)
1080 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1083 /* Handle qXfer:osadata:read. */
1086 handle_qxfer_osdata (const char *annex
,
1087 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1088 ULONGEST offset
, LONGEST len
)
1090 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1093 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1096 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1099 handle_qxfer_siginfo (const char *annex
,
1100 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1101 ULONGEST offset
, LONGEST len
)
1103 if (the_target
->qxfer_siginfo
== NULL
)
1106 if (annex
[0] != '\0' || !target_running ())
1109 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1112 /* Handle qXfer:spu:read and qXfer:spu:write. */
1115 handle_qxfer_spu (const char *annex
,
1116 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1117 ULONGEST offset
, LONGEST len
)
1119 if (the_target
->qxfer_spu
== NULL
)
1122 if (!target_running ())
1125 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1128 /* Handle qXfer:statictrace:read. */
1131 handle_qxfer_statictrace (const char *annex
,
1132 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1133 ULONGEST offset
, LONGEST len
)
1137 if (writebuf
!= NULL
)
1140 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1143 if (traceframe_read_sdata (current_traceframe
, offset
,
1144 readbuf
, len
, &nbytes
))
1149 /* Helper for handle_qxfer_threads. */
1152 handle_qxfer_threads_proper (struct buffer
*buffer
)
1154 struct inferior_list_entry
*thread
;
1156 buffer_grow_str (buffer
, "<threads>\n");
1158 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1160 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1162 int core
= target_core_of_thread (ptid
);
1165 write_ptid (ptid_s
, ptid
);
1169 sprintf (core_s
, "%d", core
);
1170 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1175 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1180 buffer_grow_str0 (buffer
, "</threads>\n");
1183 /* Handle qXfer:threads:read. */
1186 handle_qxfer_threads (const char *annex
,
1187 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1188 ULONGEST offset
, LONGEST len
)
1190 static char *result
= 0;
1191 static unsigned int result_length
= 0;
1193 if (writebuf
!= NULL
)
1196 if (!target_running () || annex
[0] != '\0')
1201 struct buffer buffer
;
1202 /* When asked for data at offset 0, generate everything and store into
1203 'result'. Successive reads will be served off 'result'. */
1207 buffer_init (&buffer
);
1209 handle_qxfer_threads_proper (&buffer
);
1211 result
= buffer_finish (&buffer
);
1212 result_length
= strlen (result
);
1213 buffer_free (&buffer
);
1216 if (offset
>= result_length
)
1218 /* We're out of data. */
1225 if (len
> result_length
- offset
)
1226 len
= result_length
- offset
;
1228 memcpy (readbuf
, result
+ offset
, len
);
1233 /* Handle qXfer:traceframe-info:read. */
1236 handle_qxfer_traceframe_info (const char *annex
,
1237 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1238 ULONGEST offset
, LONGEST len
)
1240 static char *result
= 0;
1241 static unsigned int result_length
= 0;
1243 if (writebuf
!= NULL
)
1246 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1251 struct buffer buffer
;
1253 /* When asked for data at offset 0, generate everything and
1254 store into 'result'. Successive reads will be served off
1258 buffer_init (&buffer
);
1260 traceframe_read_info (current_traceframe
, &buffer
);
1262 result
= buffer_finish (&buffer
);
1263 result_length
= strlen (result
);
1264 buffer_free (&buffer
);
1267 if (offset
>= result_length
)
1269 /* We're out of data. */
1276 if (len
> result_length
- offset
)
1277 len
= result_length
- offset
;
1279 memcpy (readbuf
, result
+ offset
, len
);
1283 /* Handle qXfer:fdpic:read. */
1286 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1287 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1289 if (the_target
->read_loadmap
== NULL
)
1292 if (!target_running ())
1295 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1298 static const struct qxfer qxfer_packets
[] =
1300 { "auxv", handle_qxfer_auxv
},
1301 { "fdpic", handle_qxfer_fdpic
},
1302 { "features", handle_qxfer_features
},
1303 { "libraries", handle_qxfer_libraries
},
1304 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1305 { "osdata", handle_qxfer_osdata
},
1306 { "siginfo", handle_qxfer_siginfo
},
1307 { "spu", handle_qxfer_spu
},
1308 { "statictrace", handle_qxfer_statictrace
},
1309 { "threads", handle_qxfer_threads
},
1310 { "traceframe-info", handle_qxfer_traceframe_info
},
1314 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1322 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1325 /* Grab the object, r/w and annex. */
1326 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1328 write_enn (own_buf
);
1333 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1336 const struct qxfer
*q
= &qxfer_packets
[i
];
1338 if (strcmp (object
, q
->object
) == 0)
1340 if (strcmp (rw
, "read") == 0)
1342 unsigned char *data
;
1347 /* Grab the offset and length. */
1348 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1350 write_enn (own_buf
);
1354 /* Read one extra byte, as an indicator of whether there is
1356 if (len
> PBUFSIZ
- 2)
1358 data
= malloc (len
+ 1);
1361 write_enn (own_buf
);
1364 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1371 write_enn (own_buf
);
1373 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1375 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1380 else if (strcmp (rw
, "write") == 0)
1385 unsigned char *data
;
1387 strcpy (own_buf
, "E00");
1388 data
= malloc (packet_len
- (offset
- own_buf
));
1391 write_enn (own_buf
);
1394 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1395 &ofs
, &len
, data
) < 0)
1398 write_enn (own_buf
);
1402 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1409 write_enn (own_buf
);
1411 sprintf (own_buf
, "%x", n
);
1424 /* Table used by the crc32 function to calcuate the checksum. */
1426 static unsigned int crc32_table
[256] =
1429 /* Compute 32 bit CRC from inferior memory.
1431 On success, return 32 bit CRC.
1432 On failure, return (unsigned long long) -1. */
1434 static unsigned long long
1435 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1437 if (!crc32_table
[1])
1439 /* Initialize the CRC table and the decoding table. */
1443 for (i
= 0; i
< 256; i
++)
1445 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1446 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1453 unsigned char byte
= 0;
1455 /* Return failure if memory read fails. */
1456 if (read_inferior_memory (base
, &byte
, 1) != 0)
1457 return (unsigned long long) -1;
1459 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1462 return (unsigned long long) crc
;
1465 /* Handle all of the extended 'q' packets. */
1468 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1470 static struct inferior_list_entry
*thread_ptr
;
1472 /* Reply the current thread id. */
1473 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1476 require_running (own_buf
);
1478 if (!ptid_equal (general_thread
, null_ptid
)
1479 && !ptid_equal (general_thread
, minus_one_ptid
))
1480 gdb_id
= general_thread
;
1483 thread_ptr
= all_threads
.head
;
1484 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1487 sprintf (own_buf
, "QC");
1489 write_ptid (own_buf
, gdb_id
);
1493 if (strcmp ("qSymbol::", own_buf
) == 0)
1495 /* GDB is suggesting new symbols have been loaded. This may
1496 mean a new shared library has been detected as loaded, so
1497 take the opportunity to check if breakpoints we think are
1498 inserted, still are. Note that it isn't guaranteed that
1499 we'll see this when a shared library is loaded, and nor will
1500 we see this for unloads (although breakpoints in unloaded
1501 libraries shouldn't trigger), as GDB may not find symbols for
1502 the library at all. We also re-validate breakpoints when we
1503 see a second GDB breakpoint for the same address, and or when
1504 we access breakpoint shadows. */
1505 validate_breakpoints ();
1507 if (target_supports_tracepoints ())
1508 tracepoint_look_up_symbols ();
1510 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1511 (*the_target
->look_up_symbols
) ();
1513 strcpy (own_buf
, "OK");
1517 if (!disable_packet_qfThreadInfo
)
1519 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1523 require_running (own_buf
);
1524 thread_ptr
= all_threads
.head
;
1527 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1528 write_ptid (own_buf
, gdb_id
);
1529 thread_ptr
= thread_ptr
->next
;
1533 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1537 require_running (own_buf
);
1538 if (thread_ptr
!= NULL
)
1541 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1542 write_ptid (own_buf
, gdb_id
);
1543 thread_ptr
= thread_ptr
->next
;
1548 sprintf (own_buf
, "l");
1554 if (the_target
->read_offsets
!= NULL
1555 && strcmp ("qOffsets", own_buf
) == 0)
1557 CORE_ADDR text
, data
;
1559 require_running (own_buf
);
1560 if (the_target
->read_offsets (&text
, &data
))
1561 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1562 (long)text
, (long)data
, (long)data
);
1564 write_enn (own_buf
);
1569 /* Protocol features query. */
1570 if (strncmp ("qSupported", own_buf
, 10) == 0
1571 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1573 char *p
= &own_buf
[10];
1574 int gdb_supports_qRelocInsn
= 0;
1576 /* Start processing qSupported packet. */
1577 target_process_qsupported (NULL
);
1579 /* Process each feature being provided by GDB. The first
1580 feature will follow a ':', and latter features will follow
1584 char **qsupported
= NULL
;
1588 /* Two passes, to avoid nested strtok calls in
1589 target_process_qsupported. */
1590 for (p
= strtok (p
+ 1, ";");
1592 p
= strtok (NULL
, ";"))
1595 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1596 qsupported
[count
- 1] = xstrdup (p
);
1599 for (i
= 0; i
< count
; i
++)
1602 if (strcmp (p
, "multiprocess+") == 0)
1604 /* GDB supports and wants multi-process support if
1606 if (target_supports_multi_process ())
1609 else if (strcmp (p
, "qRelocInsn+") == 0)
1611 /* GDB supports relocate instruction requests. */
1612 gdb_supports_qRelocInsn
= 1;
1615 target_process_qsupported (p
);
1624 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1627 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1628 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1631 /* We do not have any hook to indicate whether the non-SVR4 target
1632 backend supports qXfer:libraries:read, so always report it. */
1633 strcat (own_buf
, ";qXfer:libraries:read+");
1636 if (the_target
->read_auxv
!= NULL
)
1637 strcat (own_buf
, ";qXfer:auxv:read+");
1639 if (the_target
->qxfer_spu
!= NULL
)
1640 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1642 if (the_target
->qxfer_siginfo
!= NULL
)
1643 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1645 if (the_target
->read_loadmap
!= NULL
)
1646 strcat (own_buf
, ";qXfer:fdpic:read+");
1648 /* We always report qXfer:features:read, as targets may
1649 install XML files on a subsequent call to arch_setup.
1650 If we reported to GDB on startup that we don't support
1651 qXfer:feature:read at all, we will never be re-queried. */
1652 strcat (own_buf
, ";qXfer:features:read+");
1654 if (transport_is_reliable
)
1655 strcat (own_buf
, ";QStartNoAckMode+");
1657 if (the_target
->qxfer_osdata
!= NULL
)
1658 strcat (own_buf
, ";qXfer:osdata:read+");
1660 if (target_supports_multi_process ())
1661 strcat (own_buf
, ";multiprocess+");
1663 if (target_supports_non_stop ())
1664 strcat (own_buf
, ";QNonStop+");
1666 if (target_supports_disable_randomization ())
1667 strcat (own_buf
, ";QDisableRandomization+");
1669 strcat (own_buf
, ";qXfer:threads:read+");
1671 if (target_supports_tracepoints ())
1673 strcat (own_buf
, ";ConditionalTracepoints+");
1674 strcat (own_buf
, ";TraceStateVariables+");
1675 strcat (own_buf
, ";TracepointSource+");
1676 strcat (own_buf
, ";DisconnectedTracing+");
1677 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1678 strcat (own_buf
, ";FastTracepoints+");
1679 strcat (own_buf
, ";StaticTracepoints+");
1680 strcat (own_buf
, ";InstallInTrace+");
1681 strcat (own_buf
, ";qXfer:statictrace:read+");
1682 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1683 strcat (own_buf
, ";EnableDisableTracepoints+");
1684 strcat (own_buf
, ";tracenz+");
1687 /* Support target-side breakpoint conditions and commands. */
1688 strcat (own_buf
, ";ConditionalBreakpoints+");
1689 strcat (own_buf
, ";BreakpointCommands+");
1691 if (target_supports_agent ())
1692 strcat (own_buf
, ";QAgent+");
1697 /* Thread-local storage support. */
1698 if (the_target
->get_tls_address
!= NULL
1699 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1701 char *p
= own_buf
+ 12;
1702 CORE_ADDR parts
[2], address
= 0;
1704 ptid_t ptid
= null_ptid
;
1706 require_running (own_buf
);
1708 for (i
= 0; i
< 3; i
++)
1716 p2
= strchr (p
, ',');
1729 ptid
= read_ptid (p
, NULL
);
1731 decode_address (&parts
[i
- 1], p
, len
);
1735 if (p
!= NULL
|| i
< 3)
1739 struct thread_info
*thread
= find_thread_ptid (ptid
);
1744 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1750 strcpy (own_buf
, paddress(address
));
1755 write_enn (own_buf
);
1759 /* Otherwise, pretend we do not understand this packet. */
1762 /* Windows OS Thread Information Block address support. */
1763 if (the_target
->get_tib_address
!= NULL
1764 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1769 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1771 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1774 strcpy (own_buf
, paddress(tlb
));
1779 write_enn (own_buf
);
1785 /* Handle "monitor" commands. */
1786 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1788 char *mon
= malloc (PBUFSIZ
);
1789 int len
= strlen (own_buf
+ 6);
1793 write_enn (own_buf
);
1797 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1799 write_enn (own_buf
);
1803 mon
[len
/ 2] = '\0';
1807 if (the_target
->handle_monitor_command
== NULL
1808 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1809 /* Default processing. */
1810 handle_monitor_command (mon
, own_buf
);
1816 if (strncmp ("qSearch:memory:", own_buf
,
1817 sizeof ("qSearch:memory:") - 1) == 0)
1819 require_running (own_buf
);
1820 handle_search_memory (own_buf
, packet_len
);
1824 if (strcmp (own_buf
, "qAttached") == 0
1825 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1827 struct process_info
*process
;
1829 if (own_buf
[sizeof ("qAttached") - 1])
1831 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1832 process
= (struct process_info
*)
1833 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1837 require_running (own_buf
);
1838 process
= current_process ();
1841 if (process
== NULL
)
1843 write_enn (own_buf
);
1847 strcpy (own_buf
, process
->attached
? "1" : "0");
1851 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1853 /* CRC check (compare-section). */
1857 unsigned long long crc
;
1859 require_running (own_buf
);
1860 base
= strtoul (own_buf
+ 5, &comma
, 16);
1861 if (*comma
++ != ',')
1863 write_enn (own_buf
);
1866 len
= strtoul (comma
, NULL
, 16);
1867 crc
= crc32 (base
, len
, 0xffffffff);
1868 /* Check for memory failure. */
1869 if (crc
== (unsigned long long) -1)
1871 write_enn (own_buf
);
1874 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1878 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1881 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1884 /* Otherwise we didn't know what packet it was. Say we didn't
1889 static void gdb_wants_all_threads_stopped (void);
1891 /* Parse vCont packets. */
1893 handle_v_cont (char *own_buf
)
1897 struct thread_resume
*resume_info
;
1898 struct thread_resume default_action
= {{0}};
1900 /* Count the number of semicolons in the packet. There should be one
1901 for every action. */
1907 p
= strchr (p
, ';');
1910 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1911 if (resume_info
== NULL
)
1919 if (p
[0] == 's' || p
[0] == 'S')
1920 resume_info
[i
].kind
= resume_step
;
1921 else if (p
[0] == 'c' || p
[0] == 'C')
1922 resume_info
[i
].kind
= resume_continue
;
1923 else if (p
[0] == 't')
1924 resume_info
[i
].kind
= resume_stop
;
1928 if (p
[0] == 'S' || p
[0] == 'C')
1931 sig
= strtol (p
+ 1, &q
, 16);
1936 if (!gdb_signal_to_host_p (sig
))
1938 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
1942 resume_info
[i
].sig
= 0;
1948 resume_info
[i
].thread
= minus_one_ptid
;
1949 default_action
= resume_info
[i
];
1951 /* Note: we don't increment i here, we'll overwrite this entry
1952 the next time through. */
1954 else if (p
[0] == ':')
1956 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1961 if (p
[0] != ';' && p
[0] != 0)
1964 resume_info
[i
].thread
= ptid
;
1971 resume_info
[i
] = default_action
;
1973 /* `cont_thread' is still used in occasional places in the backend,
1974 to implement single-thread scheduler-locking. Doesn't make sense
1975 to set it if we see a stop request, or a wildcard action (one
1976 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
1978 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1979 || ptid_get_lwp (resume_info
[0].thread
) == -1)
1980 && resume_info
[0].kind
!= resume_stop
)
1981 cont_thread
= resume_info
[0].thread
;
1983 cont_thread
= minus_one_ptid
;
1984 set_desired_inferior (0);
1989 (*the_target
->resume
) (resume_info
, n
);
1997 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1999 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2000 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2001 current_inferior
->last_status
= last_status
;
2003 /* From the client's perspective, all-stop mode always stops all
2004 threads implicitly (and the target backend has already done
2005 so by now). Tag all threads as "want-stopped", so we don't
2006 resume them implicitly without the client telling us to. */
2007 gdb_wants_all_threads_stopped ();
2008 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2009 disable_async_io ();
2011 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2012 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2013 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2018 write_enn (own_buf
);
2023 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2025 handle_v_attach (char *own_buf
)
2029 pid
= strtol (own_buf
+ 8, NULL
, 16);
2030 if (pid
!= 0 && attach_inferior (pid
) == 0)
2032 /* Don't report shared library events after attaching, even if
2033 some libraries are preloaded. GDB will always poll the
2034 library list. Avoids the "stopped by shared library event"
2035 notice on the GDB side. */
2040 /* In non-stop, we don't send a resume reply. Stop events
2041 will follow up using the normal notification
2046 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2052 write_enn (own_buf
);
2057 /* Run a new program. Return 1 if successful, 0 if failure. */
2059 handle_v_run (char *own_buf
)
2061 char *p
, *next_p
, **new_argv
;
2065 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2071 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2072 if (new_argv
== NULL
)
2074 write_enn (own_buf
);
2079 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2081 next_p
= strchr (p
, ';');
2083 next_p
= p
+ strlen (p
);
2085 if (i
== 0 && p
== next_p
)
2089 /* FIXME: Fail request if out of memory instead of dying. */
2090 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2091 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2092 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2101 if (new_argv
[0] == NULL
)
2103 /* GDB didn't specify a program to run. Use the program from the
2104 last run with the new argument list. */
2106 if (program_argv
== NULL
)
2108 write_enn (own_buf
);
2109 freeargv (new_argv
);
2113 new_argv
[0] = strdup (program_argv
[0]);
2114 if (new_argv
[0] == NULL
)
2116 write_enn (own_buf
);
2117 freeargv (new_argv
);
2122 /* Free the old argv and install the new one. */
2123 freeargv (program_argv
);
2124 program_argv
= new_argv
;
2126 start_inferior (program_argv
);
2127 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2129 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2131 /* In non-stop, sending a resume reply doesn't set the general
2132 thread, but GDB assumes a vRun sets it (this is so GDB can
2133 query which is the main thread of the new inferior. */
2135 general_thread
= last_ptid
;
2141 write_enn (own_buf
);
2146 /* Kill process. Return 1 if successful, 0 if failure. */
2148 handle_v_kill (char *own_buf
)
2151 char *p
= &own_buf
[6];
2153 pid
= strtol (p
, NULL
, 16);
2156 if (pid
!= 0 && kill_inferior (pid
) == 0)
2158 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2159 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2160 last_ptid
= pid_to_ptid (pid
);
2161 discard_queued_stop_replies (pid
);
2167 write_enn (own_buf
);
2172 /* Handle a 'vStopped' packet. */
2174 handle_v_stopped (char *own_buf
)
2176 /* If we're waiting for GDB to acknowledge a pending stop reply,
2177 consider that done. */
2180 struct vstop_notif
*head
;
2183 fprintf (stderr
, "vStopped: acking %s\n",
2184 target_pid_to_str (notif_queue
->ptid
));
2187 notif_queue
= notif_queue
->next
;
2191 /* Push another stop reply, or if there are no more left, an OK. */
2192 send_next_stop_reply (own_buf
);
2195 /* Handle all of the extended 'v' packets. */
2197 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2199 if (!disable_packet_vCont
)
2201 if (strncmp (own_buf
, "vCont;", 6) == 0)
2203 require_running (own_buf
);
2204 handle_v_cont (own_buf
);
2208 if (strncmp (own_buf
, "vCont?", 6) == 0)
2210 strcpy (own_buf
, "vCont;c;C;s;S;t");
2215 if (strncmp (own_buf
, "vFile:", 6) == 0
2216 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2219 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2221 if ((!extended_protocol
|| !multi_process
) && target_running ())
2223 fprintf (stderr
, "Already debugging a process\n");
2224 write_enn (own_buf
);
2227 handle_v_attach (own_buf
);
2231 if (strncmp (own_buf
, "vRun;", 5) == 0)
2233 if ((!extended_protocol
|| !multi_process
) && target_running ())
2235 fprintf (stderr
, "Already debugging a process\n");
2236 write_enn (own_buf
);
2239 handle_v_run (own_buf
);
2243 if (strncmp (own_buf
, "vKill;", 6) == 0)
2245 if (!target_running ())
2247 fprintf (stderr
, "No process to kill\n");
2248 write_enn (own_buf
);
2251 handle_v_kill (own_buf
);
2255 if (strncmp (own_buf
, "vStopped", 8) == 0)
2257 handle_v_stopped (own_buf
);
2261 /* Otherwise we didn't know what packet it was. Say we didn't
2267 /* Resume inferior and wait for another event. In non-stop mode,
2268 don't really wait here, but return immediatelly to the event
2271 myresume (char *own_buf
, int step
, int sig
)
2273 struct thread_resume resume_info
[2];
2275 int valid_cont_thread
;
2277 set_desired_inferior (0);
2279 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2280 && !ptid_equal (cont_thread
, minus_one_ptid
));
2282 if (step
|| sig
|| valid_cont_thread
)
2284 resume_info
[0].thread
= current_ptid
;
2286 resume_info
[0].kind
= resume_step
;
2288 resume_info
[0].kind
= resume_continue
;
2289 resume_info
[0].sig
= sig
;
2293 if (!valid_cont_thread
)
2295 resume_info
[n
].thread
= minus_one_ptid
;
2296 resume_info
[n
].kind
= resume_continue
;
2297 resume_info
[n
].sig
= 0;
2304 (*the_target
->resume
) (resume_info
, n
);
2310 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2312 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2313 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2315 current_inferior
->last_resume_kind
= resume_stop
;
2316 current_inferior
->last_status
= last_status
;
2319 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2320 disable_async_io ();
2322 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2323 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2324 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2328 /* Callback for for_each_inferior. Make a new stop reply for each
2332 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2334 struct thread_info
*thread
= (struct thread_info
*) entry
;
2336 /* For now, assume targets that don't have this callback also don't
2337 manage the thread's last_status field. */
2338 if (the_target
->thread_stopped
== NULL
)
2340 /* Pass the last stop reply back to GDB, but don't notify
2342 queue_stop_reply (entry
->id
, &thread
->last_status
);
2346 if (thread_stopped (thread
))
2350 "Reporting thread %s as already stopped with %s\n",
2351 target_pid_to_str (entry
->id
),
2352 target_waitstatus_to_string (&thread
->last_status
));
2354 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2356 /* Pass the last stop reply back to GDB, but don't notify
2358 queue_stop_reply (entry
->id
, &thread
->last_status
);
2365 /* Set this inferior threads's state as "want-stopped". We won't
2366 resume this thread until the client gives us another action for
2370 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2372 struct thread_info
*thread
= (struct thread_info
*) entry
;
2374 thread
->last_resume_kind
= resume_stop
;
2376 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2378 /* Most threads are stopped implicitly (all-stop); tag that with
2380 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2381 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2385 /* Set all threads' states as "want-stopped". */
2388 gdb_wants_all_threads_stopped (void)
2390 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2393 /* Clear the gdb_detached flag of every process. */
2396 gdb_reattached_process (struct inferior_list_entry
*entry
)
2398 struct process_info
*process
= (struct process_info
*) entry
;
2400 process
->gdb_detached
= 0;
2403 /* Status handler for the '?' packet. */
2406 handle_status (char *own_buf
)
2408 /* GDB is connected, don't forward events to the target anymore. */
2409 for_each_inferior (&all_processes
, gdb_reattached_process
);
2411 /* In non-stop mode, we must send a stop reply for each stopped
2412 thread. In all-stop mode, just send one for the first stopped
2417 discard_queued_stop_replies (-1);
2418 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2420 /* The first is sent immediatly. OK is sent if there is no
2421 stopped thread, which is the same handling of the vStopped
2422 packet (by design). */
2423 send_next_stop_reply (own_buf
);
2428 stabilize_threads ();
2429 gdb_wants_all_threads_stopped ();
2431 if (all_threads
.head
)
2433 struct target_waitstatus status
;
2435 status
.kind
= TARGET_WAITKIND_STOPPED
;
2436 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2437 prepare_resume_reply (own_buf
,
2438 all_threads
.head
->id
, &status
);
2441 strcpy (own_buf
, "W00");
2446 gdbserver_version (void)
2448 printf ("GNU gdbserver %s%s\n"
2449 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2450 "gdbserver is free software, covered by the "
2451 "GNU General Public License.\n"
2452 "This gdbserver was configured as \"%s\"\n",
2453 PKGVERSION
, version
, host_name
);
2457 gdbserver_usage (FILE *stream
)
2459 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2460 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2461 "\tgdbserver [OPTIONS] --multi COMM\n"
2463 "COMM may either be a tty device (for serial debugging), or \n"
2464 "HOST:PORT to listen for a TCP connection.\n"
2467 " --debug Enable general debugging output.\n"
2468 " --remote-debug Enable remote protocol debugging output.\n"
2469 " --version Display version information and exit.\n"
2470 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2471 " --once Exit after the first connection has "
2473 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2474 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2478 gdbserver_show_disableable (FILE *stream
)
2480 fprintf (stream
, "Disableable packets:\n"
2481 " vCont \tAll vCont packets\n"
2482 " qC \tQuerying the current thread\n"
2483 " qfThreadInfo\tThread listing\n"
2484 " Tthread \tPassing the thread specifier in the "
2485 "T stop reply packet\n"
2486 " threads \tAll of the above\n");
2490 #undef require_running
2491 #define require_running(BUF) \
2492 if (!target_running ()) \
2499 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2501 int pid
= * (int *) args
;
2503 if (ptid_get_pid (entry
->id
) == pid
)
2510 kill_inferior_callback (struct inferior_list_entry
*entry
)
2512 struct process_info
*process
= (struct process_info
*) entry
;
2513 int pid
= ptid_get_pid (process
->head
.id
);
2515 kill_inferior (pid
);
2516 discard_queued_stop_replies (pid
);
2519 /* Callback for for_each_inferior to detach or kill the inferior,
2520 depending on whether we attached to it or not.
2521 We inform the user whether we're detaching or killing the process
2522 as this is only called when gdbserver is about to exit. */
2525 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2527 struct process_info
*process
= (struct process_info
*) entry
;
2528 int pid
= ptid_get_pid (process
->head
.id
);
2530 if (process
->attached
)
2531 detach_inferior (pid
);
2533 kill_inferior (pid
);
2535 discard_queued_stop_replies (pid
);
2538 /* for_each_inferior callback for detach_or_kill_for_exit to print
2539 the pids of started inferiors. */
2542 print_started_pid (struct inferior_list_entry
*entry
)
2544 struct process_info
*process
= (struct process_info
*) entry
;
2546 if (! process
->attached
)
2548 int pid
= ptid_get_pid (process
->head
.id
);
2549 fprintf (stderr
, " %d", pid
);
2553 /* for_each_inferior callback for detach_or_kill_for_exit to print
2554 the pids of attached inferiors. */
2557 print_attached_pid (struct inferior_list_entry
*entry
)
2559 struct process_info
*process
= (struct process_info
*) entry
;
2561 if (process
->attached
)
2563 int pid
= ptid_get_pid (process
->head
.id
);
2564 fprintf (stderr
, " %d", pid
);
2568 /* Call this when exiting gdbserver with possible inferiors that need
2569 to be killed or detached from. */
2572 detach_or_kill_for_exit (void)
2574 /* First print a list of the inferiors we will be killing/detaching.
2575 This is to assist the user, for example, in case the inferior unexpectedly
2576 dies after we exit: did we screw up or did the inferior exit on its own?
2577 Having this info will save some head-scratching. */
2579 if (have_started_inferiors_p ())
2581 fprintf (stderr
, "Killing process(es):");
2582 for_each_inferior (&all_processes
, print_started_pid
);
2583 fprintf (stderr
, "\n");
2585 if (have_attached_inferiors_p ())
2587 fprintf (stderr
, "Detaching process(es):");
2588 for_each_inferior (&all_processes
, print_attached_pid
);
2589 fprintf (stderr
, "\n");
2592 /* Now we can kill or detach the inferiors. */
2594 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2598 main (int argc
, char *argv
[])
2602 char *arg_end
, *port
;
2603 char **next_arg
= &argv
[1];
2604 volatile int multi_mode
= 0;
2605 volatile int attach
= 0;
2608 while (*next_arg
!= NULL
&& **next_arg
== '-')
2610 if (strcmp (*next_arg
, "--version") == 0)
2612 gdbserver_version ();
2615 else if (strcmp (*next_arg
, "--help") == 0)
2617 gdbserver_usage (stdout
);
2620 else if (strcmp (*next_arg
, "--attach") == 0)
2622 else if (strcmp (*next_arg
, "--multi") == 0)
2624 else if (strcmp (*next_arg
, "--wrapper") == 0)
2628 wrapper_argv
= next_arg
;
2629 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2632 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2634 gdbserver_usage (stderr
);
2638 /* Consume the "--". */
2641 else if (strcmp (*next_arg
, "--debug") == 0)
2643 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2645 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2647 gdbserver_show_disableable (stdout
);
2650 else if (strncmp (*next_arg
,
2651 "--disable-packet=",
2652 sizeof ("--disable-packet=") - 1) == 0)
2654 char *packets
, *tok
;
2656 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2657 for (tok
= strtok (packets
, ",");
2659 tok
= strtok (NULL
, ","))
2661 if (strcmp ("vCont", tok
) == 0)
2662 disable_packet_vCont
= 1;
2663 else if (strcmp ("Tthread", tok
) == 0)
2664 disable_packet_Tthread
= 1;
2665 else if (strcmp ("qC", tok
) == 0)
2666 disable_packet_qC
= 1;
2667 else if (strcmp ("qfThreadInfo", tok
) == 0)
2668 disable_packet_qfThreadInfo
= 1;
2669 else if (strcmp ("threads", tok
) == 0)
2671 disable_packet_vCont
= 1;
2672 disable_packet_Tthread
= 1;
2673 disable_packet_qC
= 1;
2674 disable_packet_qfThreadInfo
= 1;
2678 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2680 gdbserver_show_disableable (stderr
);
2685 else if (strcmp (*next_arg
, "-") == 0)
2687 /* "-" specifies a stdio connection and is a form of port
2689 *next_arg
= STDIO_CONNECTION_NAME
;
2692 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2693 disable_randomization
= 1;
2694 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2695 disable_randomization
= 0;
2696 else if (strcmp (*next_arg
, "--once") == 0)
2700 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2708 if (setjmp (toplevel
))
2710 fprintf (stderr
, "Exiting\n");
2716 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2718 gdbserver_usage (stderr
);
2722 /* We need to know whether the remote connection is stdio before
2723 starting the inferior. Inferiors created in this scenario have
2724 stdin,stdout redirected. So do this here before we call
2726 remote_prepare (port
);
2731 /* --attach used to come after PORT, so allow it there for
2733 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2740 && (*next_arg
== NULL
2741 || (*next_arg
)[0] == '\0'
2742 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2744 || next_arg
[1] != NULL
))
2749 gdbserver_usage (stderr
);
2753 initialize_async_io ();
2755 if (target_supports_tracepoints ())
2756 initialize_tracepoint ();
2758 own_buf
= xmalloc (PBUFSIZ
+ 1);
2759 mem_buf
= xmalloc (PBUFSIZ
);
2761 if (pid
== 0 && *next_arg
!= NULL
)
2765 n
= argc
- (next_arg
- argv
);
2766 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2767 for (i
= 0; i
< n
; i
++)
2768 program_argv
[i
] = xstrdup (next_arg
[i
]);
2769 program_argv
[i
] = NULL
;
2771 /* Wait till we are at first instruction in program. */
2772 start_inferior (program_argv
);
2774 /* We are now (hopefully) stopped at the first instruction of
2775 the target process. This assumes that the target process was
2776 successfully created. */
2780 if (attach_inferior (pid
) == -1)
2781 error ("Attaching not supported on this target");
2783 /* Otherwise succeeded. */
2787 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2788 last_status
.value
.integer
= 0;
2789 last_ptid
= minus_one_ptid
;
2792 /* Don't report shared library events on the initial connection,
2793 even if some libraries are preloaded. Avoids the "stopped by
2794 shared library event" notice on gdb side. */
2797 if (setjmp (toplevel
))
2799 /* If something fails and longjmps while detaching or killing
2800 inferiors, we'd end up here again, stuck in an infinite loop
2801 trap. Be sure that if that happens, we exit immediately
2803 if (setjmp (toplevel
) == 0)
2804 detach_or_kill_for_exit ();
2806 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2810 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2811 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2816 if (!was_running
&& !multi_mode
)
2818 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2826 /* Be sure we're out of tfind mode. */
2827 current_traceframe
= -1;
2831 if (setjmp (toplevel
) != 0)
2833 /* An error occurred. */
2834 if (response_needed
)
2836 write_enn (own_buf
);
2841 /* Wait for events. This will return when all event sources are
2842 removed from the event loop. */
2843 start_event_loop ();
2845 /* If an exit was requested (using the "monitor exit" command),
2846 terminate now. The only other way to get here is for
2847 getpkt to fail; close the connection and reopen it at the
2850 if (exit_requested
|| run_once
)
2852 /* If something fails and longjmps while detaching or
2853 killing inferiors, we'd end up here again, stuck in an
2854 infinite loop trap. Be sure that if that happens, we
2855 exit immediately instead. */
2856 if (setjmp (toplevel
) == 0)
2858 detach_or_kill_for_exit ();
2863 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2869 "Remote side has terminated connection. "
2870 "GDBserver will reopen the connection.\n");
2874 if (disconnected_tracing
)
2876 /* Try to enable non-stop/async mode, so we we can both
2877 wait for an async socket accept, and handle async
2878 target events simultaneously. There's also no point
2879 either in having the target always stop all threads,
2880 when we're going to pass signals down without
2884 if (start_non_stop (1))
2887 /* Detaching implicitly resumes all threads; simply
2888 disconnecting does not. */
2894 "Disconnected tracing disabled; stopping trace run.\n");
2901 /* Process options coming from Z packets for *point at address
2902 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2903 to point to the first char after the last processed option. */
2906 process_point_options (CORE_ADDR point_addr
, char **packet
)
2908 char *dataptr
= *packet
;
2911 /* Check if data has the correct format. */
2912 if (*dataptr
!= ';')
2919 if (*dataptr
== ';')
2922 if (*dataptr
== 'X')
2924 /* Conditional expression. */
2925 fprintf (stderr
, "Found breakpoint condition.\n");
2926 add_breakpoint_condition (point_addr
, &dataptr
);
2928 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
2930 dataptr
+= strlen ("cmds:");
2932 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
2933 persist
= (*dataptr
== '1');
2935 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
2939 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2941 /* Skip tokens until we find one that we recognize. */
2942 while (*dataptr
&& *dataptr
!= ';')
2949 /* Event loop callback that handles a serial event. The first byte in
2950 the serial buffer gets us here. We expect characters to arrive at
2951 a brisk pace, so we read the rest of the packet with a blocking
2955 process_serial_event (void)
2966 int new_packet_len
= -1;
2968 /* Used to decide when gdbserver should exit in
2969 multi-mode/remote. */
2970 static int have_ran
= 0;
2973 have_ran
= target_running ();
2975 disable_async_io ();
2977 response_needed
= 0;
2978 packet_len
= getpkt (own_buf
);
2979 if (packet_len
<= 0)
2982 /* Force an event loop break. */
2985 response_needed
= 1;
2992 handle_query (own_buf
, packet_len
, &new_packet_len
);
2995 handle_general_set (own_buf
);
2998 require_running (own_buf
);
3003 pid
= strtol (&own_buf
[i
], NULL
, 16);
3006 pid
= ptid_get_pid (current_ptid
);
3008 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3010 struct thread_resume resume_info
;
3011 struct process_info
*process
= find_process_pid (pid
);
3013 if (process
== NULL
)
3015 write_enn (own_buf
);
3019 if (tracing
&& disconnected_tracing
)
3021 "Disconnected tracing in effect, "
3022 "leaving gdbserver attached to the process\n");
3024 if (any_persistent_commands ())
3026 "Persistent commands are present, "
3027 "leaving gdbserver attached to the process\n");
3029 /* Make sure we're in non-stop/async mode, so we we can both
3030 wait for an async socket accept, and handle async target
3031 events simultaneously. There's also no point either in
3032 having the target stop all threads, when we're going to
3033 pass signals down without informing GDB. */
3037 fprintf (stderr
, "Forcing non-stop mode\n");
3043 process
->gdb_detached
= 1;
3045 /* Detaching implicitly resumes all threads. */
3046 resume_info
.thread
= minus_one_ptid
;
3047 resume_info
.kind
= resume_continue
;
3048 resume_info
.sig
= 0;
3049 (*the_target
->resume
) (&resume_info
, 1);
3052 break; /* from switch/case */
3055 fprintf (stderr
, "Detaching from process %d\n", pid
);
3057 if (detach_inferior (pid
) != 0)
3058 write_enn (own_buf
);
3061 discard_queued_stop_replies (pid
);
3064 if (extended_protocol
)
3066 /* Treat this like a normal program exit. */
3067 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3068 last_status
.value
.integer
= 0;
3069 last_ptid
= pid_to_ptid (pid
);
3071 current_inferior
= NULL
;
3078 /* If we are attached, then we can exit. Otherwise, we
3079 need to hang around doing nothing, until the child is
3081 join_inferior (pid
);
3087 extended_protocol
= 1;
3091 handle_status (own_buf
);
3094 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3096 ptid_t gdb_id
, thread_id
;
3099 require_running (own_buf
);
3101 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3103 pid
= ptid_get_pid (gdb_id
);
3105 if (ptid_equal (gdb_id
, null_ptid
)
3106 || ptid_equal (gdb_id
, minus_one_ptid
))
3107 thread_id
= null_ptid
;
3109 && ptid_equal (pid_to_ptid (pid
),
3112 struct thread_info
*thread
=
3113 (struct thread_info
*) find_inferior (&all_threads
,
3118 write_enn (own_buf
);
3122 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3126 thread_id
= gdb_id_to_thread_id (gdb_id
);
3127 if (ptid_equal (thread_id
, null_ptid
))
3129 write_enn (own_buf
);
3134 if (own_buf
[1] == 'g')
3136 if (ptid_equal (thread_id
, null_ptid
))
3138 /* GDB is telling us to choose any thread. Check if
3139 the currently selected thread is still valid. If
3140 it is not, select the first available. */
3141 struct thread_info
*thread
=
3142 (struct thread_info
*) find_inferior_id (&all_threads
,
3145 thread_id
= all_threads
.head
->id
;
3148 general_thread
= thread_id
;
3149 set_desired_inferior (1);
3151 else if (own_buf
[1] == 'c')
3152 cont_thread
= thread_id
;
3158 /* Silently ignore it so that gdb can extend the protocol
3159 without compatibility headaches. */
3164 require_running (own_buf
);
3165 if (current_traceframe
>= 0)
3167 struct regcache
*regcache
= new_register_cache ();
3169 if (fetch_traceframe_registers (current_traceframe
,
3171 registers_to_string (regcache
, own_buf
);
3173 write_enn (own_buf
);
3174 free_register_cache (regcache
);
3178 struct regcache
*regcache
;
3180 set_desired_inferior (1);
3181 regcache
= get_thread_regcache (current_inferior
, 1);
3182 registers_to_string (regcache
, own_buf
);
3186 require_running (own_buf
);
3187 if (current_traceframe
>= 0)
3188 write_enn (own_buf
);
3191 struct regcache
*regcache
;
3193 set_desired_inferior (1);
3194 regcache
= get_thread_regcache (current_inferior
, 1);
3195 registers_from_string (regcache
, &own_buf
[1]);
3200 require_running (own_buf
);
3201 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3202 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3204 write_enn (own_buf
);
3206 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3209 require_running (own_buf
);
3210 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3211 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3214 write_enn (own_buf
);
3217 require_running (own_buf
);
3218 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3219 &mem_addr
, &len
, &mem_buf
) < 0
3220 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3221 write_enn (own_buf
);
3226 require_running (own_buf
);
3227 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3228 if (gdb_signal_to_host_p (sig
))
3229 signal
= gdb_signal_to_host (sig
);
3232 myresume (own_buf
, 0, signal
);
3235 require_running (own_buf
);
3236 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3237 if (gdb_signal_to_host_p (sig
))
3238 signal
= gdb_signal_to_host (sig
);
3241 myresume (own_buf
, 1, signal
);
3244 require_running (own_buf
);
3246 myresume (own_buf
, 0, signal
);
3249 require_running (own_buf
);
3251 myresume (own_buf
, 1, signal
);
3253 case 'Z': /* insert_ ... */
3255 case 'z': /* remove_ ... */
3259 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3260 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3261 char type
= own_buf
[1];
3263 const int insert
= ch
== 'Z';
3265 /* Default to unrecognized/unsupported. */
3269 case '0': /* software-breakpoint */
3270 case '1': /* hardware-breakpoint */
3271 case '2': /* write watchpoint */
3272 case '3': /* read watchpoint */
3273 case '4': /* access watchpoint */
3274 require_running (own_buf
);
3275 if (insert
&& the_target
->insert_point
!= NULL
)
3277 /* Insert the breakpoint. If it is already inserted, nothing
3279 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3281 /* GDB may have sent us a list of *point parameters to be
3282 evaluated on the target's side. Read such list here. If we
3283 already have a list of parameters, GDB is telling us to drop
3284 that list and use this one instead. */
3285 if (!res
&& (type
== '0' || type
== '1'))
3287 /* Remove previous conditions. */
3288 clear_gdb_breakpoint_conditions (addr
);
3289 process_point_options (addr
, &dataptr
);
3292 else if (!insert
&& the_target
->remove_point
!= NULL
)
3293 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3305 write_enn (own_buf
);
3309 response_needed
= 0;
3310 if (!target_running ())
3311 /* The packet we received doesn't make sense - but we can't
3312 reply to it, either. */
3315 fprintf (stderr
, "Killing all inferiors\n");
3316 for_each_inferior (&all_processes
, kill_inferior_callback
);
3318 /* When using the extended protocol, we wait with no program
3319 running. The traditional protocol will exit instead. */
3320 if (extended_protocol
)
3322 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3323 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3331 ptid_t gdb_id
, thread_id
;
3333 require_running (own_buf
);
3335 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3336 thread_id
= gdb_id_to_thread_id (gdb_id
);
3337 if (ptid_equal (thread_id
, null_ptid
))
3339 write_enn (own_buf
);
3343 if (mythread_alive (thread_id
))
3346 write_enn (own_buf
);
3350 response_needed
= 0;
3352 /* Restarting the inferior is only supported in the extended
3354 if (extended_protocol
)
3356 if (target_running ())
3357 for_each_inferior (&all_processes
,
3358 kill_inferior_callback
);
3359 fprintf (stderr
, "GDBserver restarting\n");
3361 /* Wait till we are at 1st instruction in prog. */
3362 if (program_argv
!= NULL
)
3363 start_inferior (program_argv
);
3366 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3367 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3373 /* It is a request we don't understand. Respond with an
3374 empty packet so that gdb knows that we don't support this
3380 /* Extended (long) request. */
3381 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3385 /* It is a request we don't understand. Respond with an empty
3386 packet so that gdb knows that we don't support this
3392 if (new_packet_len
!= -1)
3393 putpkt_binary (own_buf
, new_packet_len
);
3397 response_needed
= 0;
3399 if (!extended_protocol
&& have_ran
&& !target_running ())
3401 /* In non-stop, defer exiting until GDB had a chance to query
3402 the whole vStopped list (until it gets an OK). */
3405 fprintf (stderr
, "GDBserver exiting\n");
3417 /* Event-loop callback for serial events. */
3420 handle_serial_event (int err
, gdb_client_data client_data
)
3423 fprintf (stderr
, "handling possible serial event\n");
3425 /* Really handle it. */
3426 if (process_serial_event () < 0)
3429 /* Be sure to not change the selected inferior behind GDB's back.
3430 Important in the non-stop mode asynchronous protocol. */
3431 set_desired_inferior (1);
3436 /* Event-loop callback for target events. */
3439 handle_target_event (int err
, gdb_client_data client_data
)
3442 fprintf (stderr
, "handling possible target event\n");
3444 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3447 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3449 int pid
= ptid_get_pid (last_ptid
);
3450 struct process_info
*process
= find_process_pid (pid
);
3451 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3453 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3454 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3456 mark_breakpoints_out (process
);
3457 mourn_inferior (process
);
3461 /* We're reporting this thread as stopped. Update its
3462 "want-stopped" state to what the client wants, until it
3463 gets a new resume action. */
3464 current_inferior
->last_resume_kind
= resume_stop
;
3465 current_inferior
->last_status
= last_status
;
3470 if (!target_running ())
3472 /* The last process exited. We're done. */
3476 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3478 /* A thread stopped with a signal, but gdb isn't
3479 connected to handle it. Pass it down to the
3480 inferior, as if it wasn't being traced. */
3481 struct thread_resume resume_info
;
3485 "GDB not connected; forwarding event %d for [%s]\n",
3486 (int) last_status
.kind
,
3487 target_pid_to_str (last_ptid
));
3489 resume_info
.thread
= last_ptid
;
3490 resume_info
.kind
= resume_continue
;
3491 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3492 (*the_target
->resume
) (&resume_info
, 1);
3494 else if (debug_threads
)
3495 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3496 (int) last_status
.kind
,
3497 target_pid_to_str (last_ptid
));
3501 /* Something interesting. Tell GDB about it. */
3502 push_event (last_ptid
, &last_status
);
3506 /* Be sure to not change the selected inferior behind GDB's back.
3507 Important in the non-stop mode asynchronous protocol. */
3508 set_desired_inferior (1);