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/>. */
34 ptid_t general_thread
;
38 static int extended_protocol
;
39 static int response_needed
;
40 static int exit_requested
;
42 /* --once: Exit after the first connection has closed. */
48 /* Whether we should attempt to disable the operating system's address
49 space randomization feature before starting an inferior. */
50 int disable_randomization
= 1;
52 static char **program_argv
, **wrapper_argv
;
54 /* Enable miscellaneous debugging output. The name is historical - it
55 was originally used to debug LinuxThreads support. */
58 /* Enable debugging of h/w breakpoint/watchpoint support. */
61 int pass_signals
[TARGET_SIGNAL_LAST
];
65 const char *gdbserver_xmltarget
;
67 /* The PID of the originally created or attached inferior. Used to
68 send signals to the process when GDB sends us an asynchronous interrupt
69 (user hitting Control-C in the client), and to wait for the child to exit
70 when no longer debugging it. */
72 unsigned long signal_pid
;
75 /* A file descriptor for the controlling terminal. */
78 /* TERMINAL_FD's original foreground group. */
79 pid_t old_foreground_pgrp
;
81 /* Hand back terminal ownership to the original foreground group. */
84 restore_old_foreground_pgrp (void)
86 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
90 /* Set if you want to disable optional thread related packets support
91 in gdbserver, for the sake of testing GDB against stubs that don't
93 int disable_packet_vCont
;
94 int disable_packet_Tthread
;
95 int disable_packet_qC
;
96 int disable_packet_qfThreadInfo
;
98 /* Last status reported to GDB. */
99 static struct target_waitstatus last_status
;
100 static ptid_t last_ptid
;
102 static char *own_buf
;
103 static unsigned char *mem_buf
;
105 /* Structure holding information relative to a single stop reply. We
106 keep a queue of these (really a singly-linked list) to push to GDB
110 /* Pointer to next in list. */
111 struct vstop_notif
*next
;
113 /* Thread or process that got the event. */
117 struct target_waitstatus status
;
120 /* The pending stop replies list head. */
121 static struct vstop_notif
*notif_queue
= NULL
;
123 /* Put a stop reply to the stop reply queue. */
126 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
128 struct vstop_notif
*new_notif
;
130 new_notif
= xmalloc (sizeof (*new_notif
));
131 new_notif
->next
= NULL
;
132 new_notif
->ptid
= ptid
;
133 new_notif
->status
= *status
;
137 struct vstop_notif
*tail
;
138 for (tail
= notif_queue
;
142 tail
->next
= new_notif
;
145 notif_queue
= new_notif
;
150 struct vstop_notif
*n
;
152 for (n
= notif_queue
; n
; n
= n
->next
)
155 fprintf (stderr
, "pending stop replies: %d\n", i
);
159 /* Place an event in the stop reply queue, and push a notification if
160 we aren't sending one yet. */
163 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
165 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
167 queue_stop_reply (ptid
, status
);
169 /* If this is the first stop reply in the queue, then inform GDB
170 about it, by sending a Stop notification. */
171 if (notif_queue
->next
== NULL
)
176 prepare_resume_reply (p
,
177 notif_queue
->ptid
, ¬if_queue
->status
);
178 putpkt_notif (own_buf
);
182 /* Get rid of the currently pending stop replies for PID. If PID is
183 -1, then apply to all processes. */
186 discard_queued_stop_replies (int pid
)
188 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
190 for (reply
= notif_queue
; reply
; reply
= next
)
195 || ptid_get_pid (reply
->ptid
) == pid
)
197 if (reply
== notif_queue
)
200 prev
->next
= reply
->next
;
209 /* If there are more stop replies to push, push one now. */
212 send_next_stop_reply (char *own_buf
)
215 prepare_resume_reply (own_buf
,
217 ¬if_queue
->status
);
223 target_running (void)
225 return all_threads
.head
!= NULL
;
229 start_inferior (char **argv
)
231 char **new_argv
= argv
;
233 if (wrapper_argv
!= NULL
)
237 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
239 for (i
= 0; argv
[i
] != NULL
; i
++)
241 new_argv
= alloca (sizeof (char *) * count
);
243 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
244 new_argv
[count
++] = wrapper_argv
[i
];
245 for (i
= 0; argv
[i
] != NULL
; i
++)
246 new_argv
[count
++] = argv
[i
];
247 new_argv
[count
] = NULL
;
253 for (i
= 0; new_argv
[i
]; ++i
)
254 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
259 signal (SIGTTOU
, SIG_DFL
);
260 signal (SIGTTIN
, SIG_DFL
);
263 signal_pid
= create_inferior (new_argv
[0], new_argv
);
265 /* FIXME: we don't actually know at this point that the create
266 actually succeeded. We won't know that until we wait. */
267 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
272 signal (SIGTTOU
, SIG_IGN
);
273 signal (SIGTTIN
, SIG_IGN
);
274 terminal_fd
= fileno (stderr
);
275 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
276 tcsetpgrp (terminal_fd
, signal_pid
);
277 atexit (restore_old_foreground_pgrp
);
280 if (wrapper_argv
!= NULL
)
282 struct thread_resume resume_info
;
284 resume_info
.thread
= pid_to_ptid (signal_pid
);
285 resume_info
.kind
= resume_continue
;
288 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
290 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
295 (*the_target
->resume
) (&resume_info
, 1);
297 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
298 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
301 current_inferior
->last_resume_kind
= resume_stop
;
302 current_inferior
->last_status
= last_status
;
304 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
306 current_inferior
->last_resume_kind
= resume_stop
;
307 current_inferior
->last_status
= last_status
;
311 /* Wait till we are at 1st instruction in program, return new pid
312 (assuming success). */
313 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
315 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
316 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
318 current_inferior
->last_resume_kind
= resume_stop
;
319 current_inferior
->last_status
= last_status
;
326 attach_inferior (int pid
)
328 /* myattach should return -1 if attaching is unsupported,
329 0 if it succeeded, and call error() otherwise. */
331 if (myattach (pid
) != 0)
334 fprintf (stderr
, "Attached; pid = %d\n", pid
);
337 /* FIXME - It may be that we should get the SIGNAL_PID from the
338 attach function, so that it can be the main thread instead of
339 whichever we were told to attach to. */
342 /* Clear this so the backend doesn't get confused, thinking
343 CONT_THREAD died, and it needs to resume all threads. */
344 cont_thread
= null_ptid
;
348 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
350 /* GDB knows to ignore the first SIGSTOP after attaching to a running
351 process using the "attach" command, but this is different; it's
352 just using "target remote". Pretend it's just starting up. */
353 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
354 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
355 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
357 current_inferior
->last_resume_kind
= resume_stop
;
358 current_inferior
->last_status
= last_status
;
364 extern int remote_debug
;
366 /* Decode a qXfer read request. Return 0 if everything looks OK,
370 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
372 /* After the read marker and annex, qXfer looks like a
373 traditional 'm' packet. */
374 decode_m_packet (buf
, ofs
, len
);
380 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
382 /* Extract and NUL-terminate the object. */
384 while (*buf
&& *buf
!= ':')
390 /* Extract and NUL-terminate the read/write action. */
392 while (*buf
&& *buf
!= ':')
398 /* Extract and NUL-terminate the annex. */
400 while (*buf
&& *buf
!= ':')
410 /* Write the response to a successful qXfer read. Returns the
411 length of the (binary) data stored in BUF, corresponding
412 to as much of DATA/LEN as we could fit. IS_MORE controls
413 the first character of the response. */
415 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
424 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
428 /* Handle all of the extended 'Q' packets. */
431 handle_general_set (char *own_buf
)
433 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
435 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
436 const char *p
= own_buf
+ strlen ("QPassSignals:");
439 p
= decode_address_to_semicolon (&cursig
, p
);
440 for (i
= 0; i
< numsigs
; i
++)
446 /* Keep looping, to clear the remaining signals. */
449 p
= decode_address_to_semicolon (&cursig
, p
);
454 strcpy (own_buf
, "OK");
458 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
462 fprintf (stderr
, "[noack mode enabled]\n");
471 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
473 char *mode
= own_buf
+ 9;
477 if (strcmp (mode
, "0") == 0)
479 else if (strcmp (mode
, "1") == 0)
483 /* We don't know what this mode is, so complain to
485 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
491 req_str
= req
? "non-stop" : "all-stop";
492 if (start_non_stop (req
) != 0)
494 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
502 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
508 if (strncmp ("QDisableRandomization:", own_buf
,
509 strlen ("QDisableRandomization:")) == 0)
511 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
514 unpack_varlen_hex (packet
, &setting
);
515 disable_randomization
= setting
;
519 if (disable_randomization
)
520 fprintf (stderr
, "[address space randomization disabled]\n");
522 fprintf (stderr
, "[address space randomization enabled]\n");
529 if (target_supports_tracepoints ()
530 && handle_tracepoint_general_set (own_buf
))
533 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
535 char *mode
= own_buf
+ strlen ("QAgent:");
538 if (strcmp (mode
, "0") == 0)
540 else if (strcmp (mode
, "1") == 0)
544 /* We don't know what this value is, so complain to GDB. */
545 sprintf (own_buf
, "E.Unknown QAgent value");
549 /* Update the flag. */
552 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
557 /* Otherwise we didn't know what packet it was. Say we didn't
563 get_features_xml (const char *annex
)
565 /* gdbserver_xmltarget defines what to return when looking
566 for the "target.xml" file. Its contents can either be
567 verbatim XML code (prefixed with a '@') or else the name
568 of the actual XML file to be used in place of "target.xml".
570 This variable is set up from the auto-generated
571 init_registers_... routine for the current target. */
573 if (gdbserver_xmltarget
574 && strcmp (annex
, "target.xml") == 0)
576 if (*gdbserver_xmltarget
== '@')
577 return gdbserver_xmltarget
+ 1;
579 annex
= gdbserver_xmltarget
;
584 extern const char *const xml_builtin
[][2];
587 /* Look for the annex. */
588 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
589 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
592 if (xml_builtin
[i
][0] != NULL
)
593 return xml_builtin
[i
][1];
601 monitor_show_help (void)
603 monitor_output ("The following monitor commands are supported:\n");
604 monitor_output (" set debug <0|1>\n");
605 monitor_output (" Enable general debugging messages\n");
606 monitor_output (" set debug-hw-points <0|1>\n");
607 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
608 monitor_output (" set remote-debug <0|1>\n");
609 monitor_output (" Enable remote protocol debugging messages\n");
610 monitor_output (" exit\n");
611 monitor_output (" Quit GDBserver\n");
614 /* Read trace frame or inferior memory. Returns the number of bytes
615 actually read, zero when no further transfer is possible, and -1 on
616 error. Return of a positive value smaller than LEN does not
617 indicate there's no more to be read, only the end of the transfer.
618 E.g., when GDB reads memory from a traceframe, a first request may
619 be served from a memory block that does not cover the whole request
620 length. A following request gets the rest served from either
621 another block (of the same traceframe) or from the read-only
625 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
629 if (current_traceframe
>= 0)
632 ULONGEST length
= len
;
634 if (traceframe_read_mem (current_traceframe
,
635 memaddr
, myaddr
, len
, &nbytes
))
637 /* Data read from trace buffer, we're done. */
640 if (!in_readonly_region (memaddr
, length
))
642 /* Otherwise we have a valid readonly case, fall through. */
643 /* (assume no half-trace half-real blocks for now) */
646 res
= prepare_to_access_memory ();
649 res
= read_inferior_memory (memaddr
, myaddr
, len
);
650 done_accessing_memory ();
652 return res
== 0 ? len
: -1;
658 /* Write trace frame or inferior memory. Actually, writing to trace
659 frames is forbidden. */
662 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
664 if (current_traceframe
>= 0)
670 ret
= prepare_to_access_memory ();
673 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
674 done_accessing_memory ();
680 /* Subroutine of handle_search_memory to simplify it. */
683 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
684 gdb_byte
*pattern
, unsigned pattern_len
,
685 gdb_byte
*search_buf
,
686 unsigned chunk_size
, unsigned search_buf_size
,
687 CORE_ADDR
*found_addrp
)
689 /* Prime the search buffer. */
691 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
694 warning ("Unable to access target memory at 0x%lx, halting search.",
699 /* Perform the search.
701 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
702 When we've scanned N bytes we copy the trailing bytes to the start and
703 read in another N bytes. */
705 while (search_space_len
>= pattern_len
)
708 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
712 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
714 if (found_ptr
!= NULL
)
716 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
717 *found_addrp
= found_addr
;
721 /* Not found in this chunk, skip to next chunk. */
723 /* Don't let search_space_len wrap here, it's unsigned. */
724 if (search_space_len
>= chunk_size
)
725 search_space_len
-= chunk_size
;
727 search_space_len
= 0;
729 if (search_space_len
>= pattern_len
)
731 unsigned keep_len
= search_buf_size
- chunk_size
;
732 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
735 /* Copy the trailing part of the previous iteration to the front
736 of the buffer for the next iteration. */
737 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
739 nr_to_read
= (search_space_len
- keep_len
< chunk_size
740 ? search_space_len
- keep_len
743 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
744 nr_to_read
) != search_buf_size
)
746 warning ("Unable to access target memory "
747 "at 0x%lx, halting search.",
752 start_addr
+= chunk_size
;
761 /* Handle qSearch:memory packets. */
764 handle_search_memory (char *own_buf
, int packet_len
)
766 CORE_ADDR start_addr
;
767 CORE_ADDR search_space_len
;
769 unsigned int pattern_len
;
770 /* NOTE: also defined in find.c testcase. */
771 #define SEARCH_CHUNK_SIZE 16000
772 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
773 /* Buffer to hold memory contents for searching. */
774 gdb_byte
*search_buf
;
775 unsigned search_buf_size
;
777 CORE_ADDR found_addr
;
778 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
780 pattern
= malloc (packet_len
);
783 error ("Unable to allocate memory to perform the search");
784 strcpy (own_buf
, "E00");
787 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
788 packet_len
- cmd_name_len
,
789 &start_addr
, &search_space_len
,
790 pattern
, &pattern_len
) < 0)
793 error ("Error in parsing qSearch:memory packet");
794 strcpy (own_buf
, "E00");
798 search_buf_size
= chunk_size
+ pattern_len
- 1;
800 /* No point in trying to allocate a buffer larger than the search space. */
801 if (search_space_len
< search_buf_size
)
802 search_buf_size
= search_space_len
;
804 search_buf
= malloc (search_buf_size
);
805 if (search_buf
== NULL
)
808 error ("Unable to allocate memory to perform the search");
809 strcpy (own_buf
, "E00");
813 found
= handle_search_memory_1 (start_addr
, search_space_len
,
814 pattern
, pattern_len
,
815 search_buf
, chunk_size
, search_buf_size
,
819 sprintf (own_buf
, "1,%lx", (long) found_addr
);
821 strcpy (own_buf
, "0");
823 strcpy (own_buf
, "E00");
829 #define require_running(BUF) \
830 if (!target_running ()) \
836 /* Handle monitor commands not handled by target-specific handlers. */
839 handle_monitor_command (char *mon
, char *own_buf
)
841 if (strcmp (mon
, "set debug 1") == 0)
844 monitor_output ("Debug output enabled.\n");
846 else if (strcmp (mon
, "set debug 0") == 0)
849 monitor_output ("Debug output disabled.\n");
851 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
854 monitor_output ("H/W point debugging output enabled.\n");
856 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
859 monitor_output ("H/W point debugging output disabled.\n");
861 else if (strcmp (mon
, "set remote-debug 1") == 0)
864 monitor_output ("Protocol debug output enabled.\n");
866 else if (strcmp (mon
, "set remote-debug 0") == 0)
869 monitor_output ("Protocol debug output disabled.\n");
871 else if (strcmp (mon
, "help") == 0)
872 monitor_show_help ();
873 else if (strcmp (mon
, "exit") == 0)
877 monitor_output ("Unknown monitor command.\n\n");
878 monitor_show_help ();
883 /* Associates a callback with each supported qXfer'able object. */
887 /* The object this handler handles. */
890 /* Request that the target transfer up to LEN 8-bit bytes of the
891 target's OBJECT. The OFFSET, for a seekable object, specifies
892 the starting point. The ANNEX can be used to provide additional
893 data-specific information to the target.
895 Return the number of bytes actually transfered, zero when no
896 further transfer is possible, -1 on error, and -2 when the
897 transfer is not supported. Return of a positive value smaller
898 than LEN does not indicate the end of the object, only the end of
901 One, and only one, of readbuf or writebuf must be non-NULL. */
902 int (*xfer
) (const char *annex
,
903 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
904 ULONGEST offset
, LONGEST len
);
907 /* Handle qXfer:auxv:read. */
910 handle_qxfer_auxv (const char *annex
,
911 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
912 ULONGEST offset
, LONGEST len
)
914 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
917 if (annex
[0] != '\0' || !target_running ())
920 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
923 /* Handle qXfer:features:read. */
926 handle_qxfer_features (const char *annex
,
927 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
928 ULONGEST offset
, LONGEST len
)
930 const char *document
;
933 if (writebuf
!= NULL
)
936 if (!target_running ())
939 /* Grab the correct annex. */
940 document
= get_features_xml (annex
);
941 if (document
== NULL
)
944 total_len
= strlen (document
);
946 if (offset
> total_len
)
949 if (offset
+ len
> total_len
)
950 len
= total_len
- offset
;
952 memcpy (readbuf
, document
+ offset
, len
);
956 /* Handle qXfer:libraries:read. */
959 handle_qxfer_libraries (const char *annex
,
960 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
961 ULONGEST offset
, LONGEST len
)
963 unsigned int total_len
;
965 struct inferior_list_entry
*dll_ptr
;
967 if (writebuf
!= NULL
)
970 if (annex
[0] != '\0' || !target_running ())
973 /* Do not confuse this packet with qXfer:libraries-svr4:read. */
974 if (the_target
->qxfer_libraries_svr4
!= NULL
)
977 /* Over-estimate the necessary memory. Assume that every character
978 in the library name must be escaped. */
980 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
981 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
983 document
= malloc (total_len
);
984 if (document
== NULL
)
987 strcpy (document
, "<library-list>\n");
988 p
= document
+ strlen (document
);
990 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
992 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
995 strcpy (p
, " <library name=\"");
997 name
= xml_escape_text (dll
->name
);
1001 strcpy (p
, "\"><segment address=\"");
1003 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1005 strcpy (p
, "\"/></library>\n");
1009 strcpy (p
, "</library-list>\n");
1011 total_len
= strlen (document
);
1013 if (offset
> total_len
)
1019 if (offset
+ len
> total_len
)
1020 len
= total_len
- offset
;
1022 memcpy (readbuf
, document
+ offset
, len
);
1027 /* Handle qXfer:libraries-svr4:read. */
1030 handle_qxfer_libraries_svr4 (const char *annex
,
1031 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1032 ULONGEST offset
, LONGEST len
)
1034 if (writebuf
!= NULL
)
1037 if (annex
[0] != '\0' || !target_running ()
1038 || the_target
->qxfer_libraries_svr4
== NULL
)
1041 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1044 /* Handle qXfer:osadata:read. */
1047 handle_qxfer_osdata (const char *annex
,
1048 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1049 ULONGEST offset
, LONGEST len
)
1051 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1054 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1057 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1060 handle_qxfer_siginfo (const char *annex
,
1061 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1062 ULONGEST offset
, LONGEST len
)
1064 if (the_target
->qxfer_siginfo
== NULL
)
1067 if (annex
[0] != '\0' || !target_running ())
1070 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1073 /* Handle qXfer:spu:read and qXfer:spu:write. */
1076 handle_qxfer_spu (const char *annex
,
1077 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1078 ULONGEST offset
, LONGEST len
)
1080 if (the_target
->qxfer_spu
== NULL
)
1083 if (!target_running ())
1086 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1089 /* Handle qXfer:statictrace:read. */
1092 handle_qxfer_statictrace (const char *annex
,
1093 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1094 ULONGEST offset
, LONGEST len
)
1098 if (writebuf
!= NULL
)
1101 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1104 if (traceframe_read_sdata (current_traceframe
, offset
,
1105 readbuf
, len
, &nbytes
))
1110 /* Helper for handle_qxfer_threads. */
1113 handle_qxfer_threads_proper (struct buffer
*buffer
)
1115 struct inferior_list_entry
*thread
;
1117 buffer_grow_str (buffer
, "<threads>\n");
1119 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1121 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1126 write_ptid (ptid_s
, ptid
);
1128 if (the_target
->core_of_thread
)
1129 core
= (*the_target
->core_of_thread
) (ptid
);
1133 sprintf (core_s
, "%d", core
);
1134 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1139 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1144 buffer_grow_str0 (buffer
, "</threads>\n");
1147 /* Handle qXfer:threads:read. */
1150 handle_qxfer_threads (const char *annex
,
1151 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1152 ULONGEST offset
, LONGEST len
)
1154 static char *result
= 0;
1155 static unsigned int result_length
= 0;
1157 if (writebuf
!= NULL
)
1160 if (!target_running () || annex
[0] != '\0')
1165 struct buffer buffer
;
1166 /* When asked for data at offset 0, generate everything and store into
1167 'result'. Successive reads will be served off 'result'. */
1171 buffer_init (&buffer
);
1173 handle_qxfer_threads_proper (&buffer
);
1175 result
= buffer_finish (&buffer
);
1176 result_length
= strlen (result
);
1177 buffer_free (&buffer
);
1180 if (offset
>= result_length
)
1182 /* We're out of data. */
1189 if (len
> result_length
- offset
)
1190 len
= result_length
- offset
;
1192 memcpy (readbuf
, result
+ offset
, len
);
1197 /* Handle qXfer:traceframe-info:read. */
1200 handle_qxfer_traceframe_info (const char *annex
,
1201 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1202 ULONGEST offset
, LONGEST len
)
1204 static char *result
= 0;
1205 static unsigned int result_length
= 0;
1207 if (writebuf
!= NULL
)
1210 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1215 struct buffer buffer
;
1217 /* When asked for data at offset 0, generate everything and
1218 store into 'result'. Successive reads will be served off
1222 buffer_init (&buffer
);
1224 traceframe_read_info (current_traceframe
, &buffer
);
1226 result
= buffer_finish (&buffer
);
1227 result_length
= strlen (result
);
1228 buffer_free (&buffer
);
1231 if (offset
>= result_length
)
1233 /* We're out of data. */
1240 if (len
> result_length
- offset
)
1241 len
= result_length
- offset
;
1243 memcpy (readbuf
, result
+ offset
, len
);
1247 /* Handle qXfer:fdpic:read. */
1250 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1251 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1253 if (the_target
->read_loadmap
== NULL
)
1256 if (!target_running ())
1259 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1262 static const struct qxfer qxfer_packets
[] =
1264 { "auxv", handle_qxfer_auxv
},
1265 { "fdpic", handle_qxfer_fdpic
},
1266 { "features", handle_qxfer_features
},
1267 { "libraries", handle_qxfer_libraries
},
1268 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1269 { "osdata", handle_qxfer_osdata
},
1270 { "siginfo", handle_qxfer_siginfo
},
1271 { "spu", handle_qxfer_spu
},
1272 { "statictrace", handle_qxfer_statictrace
},
1273 { "threads", handle_qxfer_threads
},
1274 { "traceframe-info", handle_qxfer_traceframe_info
},
1278 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1286 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1289 /* Grab the object, r/w and annex. */
1290 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1292 write_enn (own_buf
);
1297 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1300 const struct qxfer
*q
= &qxfer_packets
[i
];
1302 if (strcmp (object
, q
->object
) == 0)
1304 if (strcmp (rw
, "read") == 0)
1306 unsigned char *data
;
1311 /* Grab the offset and length. */
1312 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1314 write_enn (own_buf
);
1318 /* Read one extra byte, as an indicator of whether there is
1320 if (len
> PBUFSIZ
- 2)
1322 data
= malloc (len
+ 1);
1325 write_enn (own_buf
);
1328 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1335 write_enn (own_buf
);
1337 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1339 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1344 else if (strcmp (rw
, "write") == 0)
1349 unsigned char *data
;
1351 strcpy (own_buf
, "E00");
1352 data
= malloc (packet_len
- (offset
- own_buf
));
1355 write_enn (own_buf
);
1358 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1359 &ofs
, &len
, data
) < 0)
1362 write_enn (own_buf
);
1366 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1373 write_enn (own_buf
);
1375 sprintf (own_buf
, "%x", n
);
1388 /* Table used by the crc32 function to calcuate the checksum. */
1390 static unsigned int crc32_table
[256] =
1393 /* Compute 32 bit CRC from inferior memory.
1395 On success, return 32 bit CRC.
1396 On failure, return (unsigned long long) -1. */
1398 static unsigned long long
1399 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1401 if (!crc32_table
[1])
1403 /* Initialize the CRC table and the decoding table. */
1407 for (i
= 0; i
< 256; i
++)
1409 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1410 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1417 unsigned char byte
= 0;
1419 /* Return failure if memory read fails. */
1420 if (read_inferior_memory (base
, &byte
, 1) != 0)
1421 return (unsigned long long) -1;
1423 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1426 return (unsigned long long) crc
;
1429 /* Handle all of the extended 'q' packets. */
1432 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1434 static struct inferior_list_entry
*thread_ptr
;
1436 /* Reply the current thread id. */
1437 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1440 require_running (own_buf
);
1442 if (!ptid_equal (general_thread
, null_ptid
)
1443 && !ptid_equal (general_thread
, minus_one_ptid
))
1444 gdb_id
= general_thread
;
1447 thread_ptr
= all_threads
.head
;
1448 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1451 sprintf (own_buf
, "QC");
1453 write_ptid (own_buf
, gdb_id
);
1457 if (strcmp ("qSymbol::", own_buf
) == 0)
1459 /* GDB is suggesting new symbols have been loaded. This may
1460 mean a new shared library has been detected as loaded, so
1461 take the opportunity to check if breakpoints we think are
1462 inserted, still are. Note that it isn't guaranteed that
1463 we'll see this when a shared library is loaded, and nor will
1464 we see this for unloads (although breakpoints in unloaded
1465 libraries shouldn't trigger), as GDB may not find symbols for
1466 the library at all. We also re-validate breakpoints when we
1467 see a second GDB breakpoint for the same address, and or when
1468 we access breakpoint shadows. */
1469 validate_breakpoints ();
1471 if (target_supports_tracepoints ())
1472 tracepoint_look_up_symbols ();
1474 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1475 (*the_target
->look_up_symbols
) ();
1477 strcpy (own_buf
, "OK");
1481 if (!disable_packet_qfThreadInfo
)
1483 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1487 require_running (own_buf
);
1488 thread_ptr
= all_threads
.head
;
1491 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1492 write_ptid (own_buf
, gdb_id
);
1493 thread_ptr
= thread_ptr
->next
;
1497 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1501 require_running (own_buf
);
1502 if (thread_ptr
!= NULL
)
1505 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1506 write_ptid (own_buf
, gdb_id
);
1507 thread_ptr
= thread_ptr
->next
;
1512 sprintf (own_buf
, "l");
1518 if (the_target
->read_offsets
!= NULL
1519 && strcmp ("qOffsets", own_buf
) == 0)
1521 CORE_ADDR text
, data
;
1523 require_running (own_buf
);
1524 if (the_target
->read_offsets (&text
, &data
))
1525 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1526 (long)text
, (long)data
, (long)data
);
1528 write_enn (own_buf
);
1533 /* Protocol features query. */
1534 if (strncmp ("qSupported", own_buf
, 10) == 0
1535 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1537 char *p
= &own_buf
[10];
1538 int gdb_supports_qRelocInsn
= 0;
1540 /* Start processing qSupported packet. */
1541 target_process_qsupported (NULL
);
1543 /* Process each feature being provided by GDB. The first
1544 feature will follow a ':', and latter features will follow
1548 char **qsupported
= NULL
;
1552 /* Two passes, to avoid nested strtok calls in
1553 target_process_qsupported. */
1554 for (p
= strtok (p
+ 1, ";");
1556 p
= strtok (NULL
, ";"))
1559 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1560 qsupported
[count
- 1] = xstrdup (p
);
1563 for (i
= 0; i
< count
; i
++)
1566 if (strcmp (p
, "multiprocess+") == 0)
1568 /* GDB supports and wants multi-process support if
1570 if (target_supports_multi_process ())
1573 else if (strcmp (p
, "qRelocInsn+") == 0)
1575 /* GDB supports relocate instruction requests. */
1576 gdb_supports_qRelocInsn
= 1;
1579 target_process_qsupported (p
);
1587 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1589 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1590 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1593 /* We do not have any hook to indicate whether the non-SVR4 target
1594 backend supports qXfer:libraries:read, so always report it. */
1595 strcat (own_buf
, ";qXfer:libraries:read+");
1598 if (the_target
->read_auxv
!= NULL
)
1599 strcat (own_buf
, ";qXfer:auxv:read+");
1601 if (the_target
->qxfer_spu
!= NULL
)
1602 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1604 if (the_target
->qxfer_siginfo
!= NULL
)
1605 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1607 if (the_target
->read_loadmap
!= NULL
)
1608 strcat (own_buf
, ";qXfer:fdpic:read+");
1610 /* We always report qXfer:features:read, as targets may
1611 install XML files on a subsequent call to arch_setup.
1612 If we reported to GDB on startup that we don't support
1613 qXfer:feature:read at all, we will never be re-queried. */
1614 strcat (own_buf
, ";qXfer:features:read+");
1616 if (transport_is_reliable
)
1617 strcat (own_buf
, ";QStartNoAckMode+");
1619 if (the_target
->qxfer_osdata
!= NULL
)
1620 strcat (own_buf
, ";qXfer:osdata:read+");
1622 if (target_supports_multi_process ())
1623 strcat (own_buf
, ";multiprocess+");
1625 if (target_supports_non_stop ())
1626 strcat (own_buf
, ";QNonStop+");
1628 if (target_supports_disable_randomization ())
1629 strcat (own_buf
, ";QDisableRandomization+");
1631 strcat (own_buf
, ";qXfer:threads:read+");
1633 if (target_supports_tracepoints ())
1635 strcat (own_buf
, ";ConditionalTracepoints+");
1636 strcat (own_buf
, ";TraceStateVariables+");
1637 strcat (own_buf
, ";TracepointSource+");
1638 strcat (own_buf
, ";DisconnectedTracing+");
1639 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1640 strcat (own_buf
, ";FastTracepoints+");
1641 strcat (own_buf
, ";StaticTracepoints+");
1642 strcat (own_buf
, ";InstallInTrace+");
1643 strcat (own_buf
, ";qXfer:statictrace:read+");
1644 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1645 strcat (own_buf
, ";EnableDisableTracepoints+");
1646 strcat (own_buf
, ";tracenz+");
1649 /* Support target-side breakpoint conditions. */
1650 strcat (own_buf
, ";ConditionalBreakpoints+");
1652 if (target_supports_agent ())
1653 strcat (own_buf
, ";QAgent+");
1658 /* Thread-local storage support. */
1659 if (the_target
->get_tls_address
!= NULL
1660 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1662 char *p
= own_buf
+ 12;
1663 CORE_ADDR parts
[2], address
= 0;
1665 ptid_t ptid
= null_ptid
;
1667 require_running (own_buf
);
1669 for (i
= 0; i
< 3; i
++)
1677 p2
= strchr (p
, ',');
1690 ptid
= read_ptid (p
, NULL
);
1692 decode_address (&parts
[i
- 1], p
, len
);
1696 if (p
!= NULL
|| i
< 3)
1700 struct thread_info
*thread
= find_thread_ptid (ptid
);
1705 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1711 strcpy (own_buf
, paddress(address
));
1716 write_enn (own_buf
);
1720 /* Otherwise, pretend we do not understand this packet. */
1723 /* Windows OS Thread Information Block address support. */
1724 if (the_target
->get_tib_address
!= NULL
1725 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1730 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1732 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1735 strcpy (own_buf
, paddress(tlb
));
1740 write_enn (own_buf
);
1746 /* Handle "monitor" commands. */
1747 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1749 char *mon
= malloc (PBUFSIZ
);
1750 int len
= strlen (own_buf
+ 6);
1754 write_enn (own_buf
);
1758 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1760 write_enn (own_buf
);
1764 mon
[len
/ 2] = '\0';
1768 if (the_target
->handle_monitor_command
== NULL
1769 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1770 /* Default processing. */
1771 handle_monitor_command (mon
, own_buf
);
1777 if (strncmp ("qSearch:memory:", own_buf
,
1778 sizeof ("qSearch:memory:") - 1) == 0)
1780 require_running (own_buf
);
1781 handle_search_memory (own_buf
, packet_len
);
1785 if (strcmp (own_buf
, "qAttached") == 0
1786 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1788 struct process_info
*process
;
1790 if (own_buf
[sizeof ("qAttached") - 1])
1792 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1793 process
= (struct process_info
*)
1794 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1798 require_running (own_buf
);
1799 process
= current_process ();
1802 if (process
== NULL
)
1804 write_enn (own_buf
);
1808 strcpy (own_buf
, process
->attached
? "1" : "0");
1812 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1814 /* CRC check (compare-section). */
1818 unsigned long long crc
;
1820 require_running (own_buf
);
1821 base
= strtoul (own_buf
+ 5, &comma
, 16);
1822 if (*comma
++ != ',')
1824 write_enn (own_buf
);
1827 len
= strtoul (comma
, NULL
, 16);
1828 crc
= crc32 (base
, len
, 0xffffffff);
1829 /* Check for memory failure. */
1830 if (crc
== (unsigned long long) -1)
1832 write_enn (own_buf
);
1835 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1839 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1842 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1845 /* Otherwise we didn't know what packet it was. Say we didn't
1850 static void gdb_wants_all_threads_stopped (void);
1852 /* Parse vCont packets. */
1854 handle_v_cont (char *own_buf
)
1858 struct thread_resume
*resume_info
;
1859 struct thread_resume default_action
= {{0}};
1861 /* Count the number of semicolons in the packet. There should be one
1862 for every action. */
1868 p
= strchr (p
, ';');
1871 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1872 if (resume_info
== NULL
)
1880 if (p
[0] == 's' || p
[0] == 'S')
1881 resume_info
[i
].kind
= resume_step
;
1882 else if (p
[0] == 'c' || p
[0] == 'C')
1883 resume_info
[i
].kind
= resume_continue
;
1884 else if (p
[0] == 't')
1885 resume_info
[i
].kind
= resume_stop
;
1889 if (p
[0] == 'S' || p
[0] == 'C')
1892 sig
= strtol (p
+ 1, &q
, 16);
1897 if (!target_signal_to_host_p (sig
))
1899 resume_info
[i
].sig
= target_signal_to_host (sig
);
1903 resume_info
[i
].sig
= 0;
1909 resume_info
[i
].thread
= minus_one_ptid
;
1910 default_action
= resume_info
[i
];
1912 /* Note: we don't increment i here, we'll overwrite this entry
1913 the next time through. */
1915 else if (p
[0] == ':')
1917 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1922 if (p
[0] != ';' && p
[0] != 0)
1925 resume_info
[i
].thread
= ptid
;
1932 resume_info
[i
] = default_action
;
1934 /* Still used in occasional places in the backend. */
1936 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1937 && resume_info
[0].kind
!= resume_stop
)
1938 cont_thread
= resume_info
[0].thread
;
1940 cont_thread
= minus_one_ptid
;
1941 set_desired_inferior (0);
1946 (*the_target
->resume
) (resume_info
, n
);
1954 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1956 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1957 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1958 current_inferior
->last_status
= last_status
;
1960 /* From the client's perspective, all-stop mode always stops all
1961 threads implicitly (and the target backend has already done
1962 so by now). Tag all threads as "want-stopped", so we don't
1963 resume them implicitly without the client telling us to. */
1964 gdb_wants_all_threads_stopped ();
1965 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1966 disable_async_io ();
1968 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1969 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1970 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1975 write_enn (own_buf
);
1980 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1982 handle_v_attach (char *own_buf
)
1986 pid
= strtol (own_buf
+ 8, NULL
, 16);
1987 if (pid
!= 0 && attach_inferior (pid
) == 0)
1989 /* Don't report shared library events after attaching, even if
1990 some libraries are preloaded. GDB will always poll the
1991 library list. Avoids the "stopped by shared library event"
1992 notice on the GDB side. */
1997 /* In non-stop, we don't send a resume reply. Stop events
1998 will follow up using the normal notification
2003 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2009 write_enn (own_buf
);
2014 /* Run a new program. Return 1 if successful, 0 if failure. */
2016 handle_v_run (char *own_buf
)
2018 char *p
, *next_p
, **new_argv
;
2022 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2028 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2029 if (new_argv
== NULL
)
2031 write_enn (own_buf
);
2036 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2038 next_p
= strchr (p
, ';');
2040 next_p
= p
+ strlen (p
);
2042 if (i
== 0 && p
== next_p
)
2046 /* FIXME: Fail request if out of memory instead of dying. */
2047 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2048 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2049 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2058 if (new_argv
[0] == NULL
)
2060 /* GDB didn't specify a program to run. Use the program from the
2061 last run with the new argument list. */
2063 if (program_argv
== NULL
)
2065 write_enn (own_buf
);
2066 freeargv (new_argv
);
2070 new_argv
[0] = strdup (program_argv
[0]);
2071 if (new_argv
[0] == NULL
)
2073 write_enn (own_buf
);
2074 freeargv (new_argv
);
2079 /* Free the old argv and install the new one. */
2080 freeargv (program_argv
);
2081 program_argv
= new_argv
;
2083 start_inferior (program_argv
);
2084 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2086 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2088 /* In non-stop, sending a resume reply doesn't set the general
2089 thread, but GDB assumes a vRun sets it (this is so GDB can
2090 query which is the main thread of the new inferior. */
2092 general_thread
= last_ptid
;
2098 write_enn (own_buf
);
2103 /* Kill process. Return 1 if successful, 0 if failure. */
2105 handle_v_kill (char *own_buf
)
2108 char *p
= &own_buf
[6];
2110 pid
= strtol (p
, NULL
, 16);
2113 if (pid
!= 0 && kill_inferior (pid
) == 0)
2115 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2116 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2117 last_ptid
= pid_to_ptid (pid
);
2118 discard_queued_stop_replies (pid
);
2124 write_enn (own_buf
);
2129 /* Handle a 'vStopped' packet. */
2131 handle_v_stopped (char *own_buf
)
2133 /* If we're waiting for GDB to acknowledge a pending stop reply,
2134 consider that done. */
2137 struct vstop_notif
*head
;
2140 fprintf (stderr
, "vStopped: acking %s\n",
2141 target_pid_to_str (notif_queue
->ptid
));
2144 notif_queue
= notif_queue
->next
;
2148 /* Push another stop reply, or if there are no more left, an OK. */
2149 send_next_stop_reply (own_buf
);
2152 /* Handle all of the extended 'v' packets. */
2154 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2156 if (!disable_packet_vCont
)
2158 if (strncmp (own_buf
, "vCont;", 6) == 0)
2160 require_running (own_buf
);
2161 handle_v_cont (own_buf
);
2165 if (strncmp (own_buf
, "vCont?", 6) == 0)
2167 strcpy (own_buf
, "vCont;c;C;s;S;t");
2172 if (strncmp (own_buf
, "vFile:", 6) == 0
2173 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2176 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2178 if ((!extended_protocol
|| !multi_process
) && target_running ())
2180 fprintf (stderr
, "Already debugging a process\n");
2181 write_enn (own_buf
);
2184 handle_v_attach (own_buf
);
2188 if (strncmp (own_buf
, "vRun;", 5) == 0)
2190 if ((!extended_protocol
|| !multi_process
) && target_running ())
2192 fprintf (stderr
, "Already debugging a process\n");
2193 write_enn (own_buf
);
2196 handle_v_run (own_buf
);
2200 if (strncmp (own_buf
, "vKill;", 6) == 0)
2202 if (!target_running ())
2204 fprintf (stderr
, "No process to kill\n");
2205 write_enn (own_buf
);
2208 handle_v_kill (own_buf
);
2212 if (strncmp (own_buf
, "vStopped", 8) == 0)
2214 handle_v_stopped (own_buf
);
2218 /* Otherwise we didn't know what packet it was. Say we didn't
2224 /* Resume inferior and wait for another event. In non-stop mode,
2225 don't really wait here, but return immediatelly to the event
2228 myresume (char *own_buf
, int step
, int sig
)
2230 struct thread_resume resume_info
[2];
2232 int valid_cont_thread
;
2234 set_desired_inferior (0);
2236 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2237 && !ptid_equal (cont_thread
, minus_one_ptid
));
2239 if (step
|| sig
|| valid_cont_thread
)
2241 resume_info
[0].thread
2242 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2244 resume_info
[0].kind
= resume_step
;
2246 resume_info
[0].kind
= resume_continue
;
2247 resume_info
[0].sig
= sig
;
2251 if (!valid_cont_thread
)
2253 resume_info
[n
].thread
= minus_one_ptid
;
2254 resume_info
[n
].kind
= resume_continue
;
2255 resume_info
[n
].sig
= 0;
2262 (*the_target
->resume
) (resume_info
, n
);
2268 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2270 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2271 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2273 current_inferior
->last_resume_kind
= resume_stop
;
2274 current_inferior
->last_status
= last_status
;
2277 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2278 disable_async_io ();
2280 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2281 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2282 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2286 /* Callback for for_each_inferior. Make a new stop reply for each
2290 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2292 struct thread_info
*thread
= (struct thread_info
*) entry
;
2294 /* For now, assume targets that don't have this callback also don't
2295 manage the thread's last_status field. */
2296 if (the_target
->thread_stopped
== NULL
)
2298 /* Pass the last stop reply back to GDB, but don't notify
2300 queue_stop_reply (entry
->id
, &thread
->last_status
);
2304 if (thread_stopped (thread
))
2308 "Reporting thread %s as already stopped with %s\n",
2309 target_pid_to_str (entry
->id
),
2310 target_waitstatus_to_string (&thread
->last_status
));
2312 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2314 /* Pass the last stop reply back to GDB, but don't notify
2316 queue_stop_reply (entry
->id
, &thread
->last_status
);
2323 /* Set this inferior threads's state as "want-stopped". We won't
2324 resume this thread until the client gives us another action for
2328 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2330 struct thread_info
*thread
= (struct thread_info
*) entry
;
2332 thread
->last_resume_kind
= resume_stop
;
2334 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2336 /* Most threads are stopped implicitly (all-stop); tag that with
2338 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2339 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2343 /* Set all threads' states as "want-stopped". */
2346 gdb_wants_all_threads_stopped (void)
2348 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2351 /* Clear the gdb_detached flag of every process. */
2354 gdb_reattached_process (struct inferior_list_entry
*entry
)
2356 struct process_info
*process
= (struct process_info
*) entry
;
2358 process
->gdb_detached
= 0;
2361 /* Status handler for the '?' packet. */
2364 handle_status (char *own_buf
)
2366 /* GDB is connected, don't forward events to the target anymore. */
2367 for_each_inferior (&all_processes
, gdb_reattached_process
);
2369 /* In non-stop mode, we must send a stop reply for each stopped
2370 thread. In all-stop mode, just send one for the first stopped
2375 discard_queued_stop_replies (-1);
2376 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2378 /* The first is sent immediatly. OK is sent if there is no
2379 stopped thread, which is the same handling of the vStopped
2380 packet (by design). */
2381 send_next_stop_reply (own_buf
);
2386 stabilize_threads ();
2387 gdb_wants_all_threads_stopped ();
2389 if (all_threads
.head
)
2391 struct target_waitstatus status
;
2393 status
.kind
= TARGET_WAITKIND_STOPPED
;
2394 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2395 prepare_resume_reply (own_buf
,
2396 all_threads
.head
->id
, &status
);
2399 strcpy (own_buf
, "W00");
2404 gdbserver_version (void)
2406 printf ("GNU gdbserver %s%s\n"
2407 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2408 "gdbserver is free software, covered by the "
2409 "GNU General Public License.\n"
2410 "This gdbserver was configured as \"%s\"\n",
2411 PKGVERSION
, version
, host_name
);
2415 gdbserver_usage (FILE *stream
)
2417 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2418 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2419 "\tgdbserver [OPTIONS] --multi COMM\n"
2421 "COMM may either be a tty device (for serial debugging), or \n"
2422 "HOST:PORT to listen for a TCP connection.\n"
2425 " --debug Enable general debugging output.\n"
2426 " --remote-debug Enable remote protocol debugging output.\n"
2427 " --version Display version information and exit.\n"
2428 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2429 " --once Exit after the first connection has "
2431 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2432 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2436 gdbserver_show_disableable (FILE *stream
)
2438 fprintf (stream
, "Disableable packets:\n"
2439 " vCont \tAll vCont packets\n"
2440 " qC \tQuerying the current thread\n"
2441 " qfThreadInfo\tThread listing\n"
2442 " Tthread \tPassing the thread specifier in the "
2443 "T stop reply packet\n"
2444 " threads \tAll of the above\n");
2448 #undef require_running
2449 #define require_running(BUF) \
2450 if (!target_running ()) \
2457 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2459 int pid
= * (int *) args
;
2461 if (ptid_get_pid (entry
->id
) == pid
)
2468 kill_inferior_callback (struct inferior_list_entry
*entry
)
2470 struct process_info
*process
= (struct process_info
*) entry
;
2471 int pid
= ptid_get_pid (process
->head
.id
);
2473 kill_inferior (pid
);
2474 discard_queued_stop_replies (pid
);
2477 /* Callback for for_each_inferior to detach or kill the inferior,
2478 depending on whether we attached to it or not.
2479 We inform the user whether we're detaching or killing the process
2480 as this is only called when gdbserver is about to exit. */
2483 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2485 struct process_info
*process
= (struct process_info
*) entry
;
2486 int pid
= ptid_get_pid (process
->head
.id
);
2488 if (process
->attached
)
2489 detach_inferior (pid
);
2491 kill_inferior (pid
);
2493 discard_queued_stop_replies (pid
);
2496 /* for_each_inferior callback for detach_or_kill_for_exit to print
2497 the pids of started inferiors. */
2500 print_started_pid (struct inferior_list_entry
*entry
)
2502 struct process_info
*process
= (struct process_info
*) entry
;
2504 if (! process
->attached
)
2506 int pid
= ptid_get_pid (process
->head
.id
);
2507 fprintf (stderr
, " %d", pid
);
2511 /* for_each_inferior callback for detach_or_kill_for_exit to print
2512 the pids of attached inferiors. */
2515 print_attached_pid (struct inferior_list_entry
*entry
)
2517 struct process_info
*process
= (struct process_info
*) entry
;
2519 if (process
->attached
)
2521 int pid
= ptid_get_pid (process
->head
.id
);
2522 fprintf (stderr
, " %d", pid
);
2526 /* Call this when exiting gdbserver with possible inferiors that need
2527 to be killed or detached from. */
2530 detach_or_kill_for_exit (void)
2532 /* First print a list of the inferiors we will be killing/detaching.
2533 This is to assist the user, for example, in case the inferior unexpectedly
2534 dies after we exit: did we screw up or did the inferior exit on its own?
2535 Having this info will save some head-scratching. */
2537 if (have_started_inferiors_p ())
2539 fprintf (stderr
, "Killing process(es):");
2540 for_each_inferior (&all_processes
, print_started_pid
);
2541 fprintf (stderr
, "\n");
2543 if (have_attached_inferiors_p ())
2545 fprintf (stderr
, "Detaching process(es):");
2546 for_each_inferior (&all_processes
, print_attached_pid
);
2547 fprintf (stderr
, "\n");
2550 /* Now we can kill or detach the inferiors. */
2552 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2556 main (int argc
, char *argv
[])
2560 char *arg_end
, *port
;
2561 char **next_arg
= &argv
[1];
2566 while (*next_arg
!= NULL
&& **next_arg
== '-')
2568 if (strcmp (*next_arg
, "--version") == 0)
2570 gdbserver_version ();
2573 else if (strcmp (*next_arg
, "--help") == 0)
2575 gdbserver_usage (stdout
);
2578 else if (strcmp (*next_arg
, "--attach") == 0)
2580 else if (strcmp (*next_arg
, "--multi") == 0)
2582 else if (strcmp (*next_arg
, "--wrapper") == 0)
2586 wrapper_argv
= next_arg
;
2587 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2590 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2592 gdbserver_usage (stderr
);
2596 /* Consume the "--". */
2599 else if (strcmp (*next_arg
, "--debug") == 0)
2601 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2603 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2605 gdbserver_show_disableable (stdout
);
2608 else if (strncmp (*next_arg
,
2609 "--disable-packet=",
2610 sizeof ("--disable-packet=") - 1) == 0)
2612 char *packets
, *tok
;
2614 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2615 for (tok
= strtok (packets
, ",");
2617 tok
= strtok (NULL
, ","))
2619 if (strcmp ("vCont", tok
) == 0)
2620 disable_packet_vCont
= 1;
2621 else if (strcmp ("Tthread", tok
) == 0)
2622 disable_packet_Tthread
= 1;
2623 else if (strcmp ("qC", tok
) == 0)
2624 disable_packet_qC
= 1;
2625 else if (strcmp ("qfThreadInfo", tok
) == 0)
2626 disable_packet_qfThreadInfo
= 1;
2627 else if (strcmp ("threads", tok
) == 0)
2629 disable_packet_vCont
= 1;
2630 disable_packet_Tthread
= 1;
2631 disable_packet_qC
= 1;
2632 disable_packet_qfThreadInfo
= 1;
2636 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2638 gdbserver_show_disableable (stderr
);
2643 else if (strcmp (*next_arg
, "-") == 0)
2645 /* "-" specifies a stdio connection and is a form of port
2647 *next_arg
= STDIO_CONNECTION_NAME
;
2650 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2651 disable_randomization
= 1;
2652 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2653 disable_randomization
= 0;
2654 else if (strcmp (*next_arg
, "--once") == 0)
2658 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2666 if (setjmp (toplevel
))
2668 fprintf (stderr
, "Exiting\n");
2674 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2676 gdbserver_usage (stderr
);
2680 /* We need to know whether the remote connection is stdio before
2681 starting the inferior. Inferiors created in this scenario have
2682 stdin,stdout redirected. So do this here before we call
2684 remote_prepare (port
);
2689 /* --attach used to come after PORT, so allow it there for
2691 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2698 && (*next_arg
== NULL
2699 || (*next_arg
)[0] == '\0'
2700 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2702 || next_arg
[1] != NULL
))
2707 gdbserver_usage (stderr
);
2711 initialize_async_io ();
2713 if (target_supports_tracepoints ())
2714 initialize_tracepoint ();
2716 own_buf
= xmalloc (PBUFSIZ
+ 1);
2717 mem_buf
= xmalloc (PBUFSIZ
);
2719 if (pid
== 0 && *next_arg
!= NULL
)
2723 n
= argc
- (next_arg
- argv
);
2724 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2725 for (i
= 0; i
< n
; i
++)
2726 program_argv
[i
] = xstrdup (next_arg
[i
]);
2727 program_argv
[i
] = NULL
;
2729 /* Wait till we are at first instruction in program. */
2730 start_inferior (program_argv
);
2732 /* We are now (hopefully) stopped at the first instruction of
2733 the target process. This assumes that the target process was
2734 successfully created. */
2738 if (attach_inferior (pid
) == -1)
2739 error ("Attaching not supported on this target");
2741 /* Otherwise succeeded. */
2745 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2746 last_status
.value
.integer
= 0;
2747 last_ptid
= minus_one_ptid
;
2750 /* Don't report shared library events on the initial connection,
2751 even if some libraries are preloaded. Avoids the "stopped by
2752 shared library event" notice on gdb side. */
2755 if (setjmp (toplevel
))
2757 /* If something fails and longjmps while detaching or killing
2758 inferiors, we'd end up here again, stuck in an infinite loop
2759 trap. Be sure that if that happens, we exit immediately
2761 if (setjmp (toplevel
) == 0)
2762 detach_or_kill_for_exit ();
2764 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2768 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2769 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2774 if (!was_running
&& !multi_mode
)
2776 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2784 /* Be sure we're out of tfind mode. */
2785 current_traceframe
= -1;
2789 if (setjmp (toplevel
) != 0)
2791 /* An error occurred. */
2792 if (response_needed
)
2794 write_enn (own_buf
);
2799 /* Wait for events. This will return when all event sources are
2800 removed from the event loop. */
2801 start_event_loop ();
2803 /* If an exit was requested (using the "monitor exit" command),
2804 terminate now. The only other way to get here is for
2805 getpkt to fail; close the connection and reopen it at the
2808 if (exit_requested
|| run_once
)
2810 /* If something fails and longjmps while detaching or
2811 killing inferiors, we'd end up here again, stuck in an
2812 infinite loop trap. Be sure that if that happens, we
2813 exit immediately instead. */
2814 if (setjmp (toplevel
) == 0)
2816 detach_or_kill_for_exit ();
2821 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2827 "Remote side has terminated connection. "
2828 "GDBserver will reopen the connection.\n");
2832 if (disconnected_tracing
)
2834 /* Try to enable non-stop/async mode, so we we can both
2835 wait for an async socket accept, and handle async
2836 target events simultaneously. There's also no point
2837 either in having the target always stop all threads,
2838 when we're going to pass signals down without
2842 if (start_non_stop (1))
2845 /* Detaching implicitly resumes all threads; simply
2846 disconnecting does not. */
2852 "Disconnected tracing disabled; stopping trace run.\n");
2859 /* Process options coming from Z packets for *point at address
2860 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2861 to point to the first char after the last processed option. */
2864 process_point_options (CORE_ADDR point_addr
, char **packet
)
2866 char *dataptr
= *packet
;
2868 /* Check if data has the correct format. */
2869 if (*dataptr
!= ';')
2879 /* Conditional expression. */
2880 fprintf (stderr
, "Found breakpoint condition.\n");
2881 add_breakpoint_condition (point_addr
, &dataptr
);
2884 /* Unrecognized token, just skip it. */
2885 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2889 /* Skip tokens until we find one that we recognize. */
2890 while (*dataptr
&& *dataptr
!= 'X' && *dataptr
!= ';')
2896 /* Event loop callback that handles a serial event. The first byte in
2897 the serial buffer gets us here. We expect characters to arrive at
2898 a brisk pace, so we read the rest of the packet with a blocking
2902 process_serial_event (void)
2913 int new_packet_len
= -1;
2915 /* Used to decide when gdbserver should exit in
2916 multi-mode/remote. */
2917 static int have_ran
= 0;
2920 have_ran
= target_running ();
2922 disable_async_io ();
2924 response_needed
= 0;
2925 packet_len
= getpkt (own_buf
);
2926 if (packet_len
<= 0)
2929 /* Force an event loop break. */
2932 response_needed
= 1;
2939 handle_query (own_buf
, packet_len
, &new_packet_len
);
2942 handle_general_set (own_buf
);
2945 require_running (own_buf
);
2950 pid
= strtol (&own_buf
[i
], NULL
, 16);
2954 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2956 if (tracing
&& disconnected_tracing
)
2958 struct thread_resume resume_info
;
2959 struct process_info
*process
= find_process_pid (pid
);
2961 if (process
== NULL
)
2963 write_enn (own_buf
);
2968 "Disconnected tracing in effect, "
2969 "leaving gdbserver attached to the process\n");
2971 /* Make sure we're in non-stop/async mode, so we we can both
2972 wait for an async socket accept, and handle async target
2973 events simultaneously. There's also no point either in
2974 having the target stop all threads, when we're going to
2975 pass signals down without informing GDB. */
2979 fprintf (stderr
, "Forcing non-stop mode\n");
2985 process
->gdb_detached
= 1;
2987 /* Detaching implicitly resumes all threads. */
2988 resume_info
.thread
= minus_one_ptid
;
2989 resume_info
.kind
= resume_continue
;
2990 resume_info
.sig
= 0;
2991 (*the_target
->resume
) (&resume_info
, 1);
2994 break; /* from switch/case */
2997 fprintf (stderr
, "Detaching from process %d\n", pid
);
2999 if (detach_inferior (pid
) != 0)
3000 write_enn (own_buf
);
3003 discard_queued_stop_replies (pid
);
3006 if (extended_protocol
)
3008 /* Treat this like a normal program exit. */
3009 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3010 last_status
.value
.integer
= 0;
3011 last_ptid
= pid_to_ptid (pid
);
3013 current_inferior
= NULL
;
3020 /* If we are attached, then we can exit. Otherwise, we
3021 need to hang around doing nothing, until the child is
3023 join_inferior (pid
);
3029 extended_protocol
= 1;
3033 handle_status (own_buf
);
3036 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3038 ptid_t gdb_id
, thread_id
;
3041 require_running (own_buf
);
3043 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3045 pid
= ptid_get_pid (gdb_id
);
3047 if (ptid_equal (gdb_id
, null_ptid
)
3048 || ptid_equal (gdb_id
, minus_one_ptid
))
3049 thread_id
= null_ptid
;
3051 && ptid_equal (pid_to_ptid (pid
),
3054 struct thread_info
*thread
=
3055 (struct thread_info
*) find_inferior (&all_threads
,
3060 write_enn (own_buf
);
3064 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3068 thread_id
= gdb_id_to_thread_id (gdb_id
);
3069 if (ptid_equal (thread_id
, null_ptid
))
3071 write_enn (own_buf
);
3076 if (own_buf
[1] == 'g')
3078 if (ptid_equal (thread_id
, null_ptid
))
3080 /* GDB is telling us to choose any thread. Check if
3081 the currently selected thread is still valid. If
3082 it is not, select the first available. */
3083 struct thread_info
*thread
=
3084 (struct thread_info
*) find_inferior_id (&all_threads
,
3087 thread_id
= all_threads
.head
->id
;
3090 general_thread
= thread_id
;
3091 set_desired_inferior (1);
3093 else if (own_buf
[1] == 'c')
3094 cont_thread
= thread_id
;
3100 /* Silently ignore it so that gdb can extend the protocol
3101 without compatibility headaches. */
3106 require_running (own_buf
);
3107 if (current_traceframe
>= 0)
3109 struct regcache
*regcache
= new_register_cache ();
3111 if (fetch_traceframe_registers (current_traceframe
,
3113 registers_to_string (regcache
, own_buf
);
3115 write_enn (own_buf
);
3116 free_register_cache (regcache
);
3120 struct regcache
*regcache
;
3122 set_desired_inferior (1);
3123 regcache
= get_thread_regcache (current_inferior
, 1);
3124 registers_to_string (regcache
, own_buf
);
3128 require_running (own_buf
);
3129 if (current_traceframe
>= 0)
3130 write_enn (own_buf
);
3133 struct regcache
*regcache
;
3135 set_desired_inferior (1);
3136 regcache
= get_thread_regcache (current_inferior
, 1);
3137 registers_from_string (regcache
, &own_buf
[1]);
3142 require_running (own_buf
);
3143 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3144 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3146 write_enn (own_buf
);
3148 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3151 require_running (own_buf
);
3152 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3153 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3156 write_enn (own_buf
);
3159 require_running (own_buf
);
3160 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3161 &mem_addr
, &len
, &mem_buf
) < 0
3162 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3163 write_enn (own_buf
);
3168 require_running (own_buf
);
3169 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3170 if (target_signal_to_host_p (sig
))
3171 signal
= target_signal_to_host (sig
);
3174 myresume (own_buf
, 0, signal
);
3177 require_running (own_buf
);
3178 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3179 if (target_signal_to_host_p (sig
))
3180 signal
= target_signal_to_host (sig
);
3183 myresume (own_buf
, 1, signal
);
3186 require_running (own_buf
);
3188 myresume (own_buf
, 0, signal
);
3191 require_running (own_buf
);
3193 myresume (own_buf
, 1, signal
);
3195 case 'Z': /* insert_ ... */
3197 case 'z': /* remove_ ... */
3201 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3202 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3203 char type
= own_buf
[1];
3205 const int insert
= ch
== 'Z';
3207 /* Default to unrecognized/unsupported. */
3211 case '0': /* software-breakpoint */
3212 case '1': /* hardware-breakpoint */
3213 case '2': /* write watchpoint */
3214 case '3': /* read watchpoint */
3215 case '4': /* access watchpoint */
3216 require_running (own_buf
);
3217 if (insert
&& the_target
->insert_point
!= NULL
)
3219 /* Insert the breakpoint. If it is already inserted, nothing
3221 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3223 /* GDB may have sent us a list of *point parameters to be
3224 evaluated on the target's side. Read such list here. If we
3225 already have a list of parameters, GDB is telling us to drop
3226 that list and use this one instead. */
3227 if (!res
&& (type
== '0' || type
== '1'))
3229 /* Remove previous conditions. */
3230 clear_gdb_breakpoint_conditions (addr
);
3231 process_point_options (addr
, &dataptr
);
3234 else if (!insert
&& the_target
->remove_point
!= NULL
)
3235 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3247 write_enn (own_buf
);
3251 response_needed
= 0;
3252 if (!target_running ())
3253 /* The packet we received doesn't make sense - but we can't
3254 reply to it, either. */
3257 fprintf (stderr
, "Killing all inferiors\n");
3258 for_each_inferior (&all_processes
, kill_inferior_callback
);
3260 /* When using the extended protocol, we wait with no program
3261 running. The traditional protocol will exit instead. */
3262 if (extended_protocol
)
3264 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3265 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3273 ptid_t gdb_id
, thread_id
;
3275 require_running (own_buf
);
3277 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3278 thread_id
= gdb_id_to_thread_id (gdb_id
);
3279 if (ptid_equal (thread_id
, null_ptid
))
3281 write_enn (own_buf
);
3285 if (mythread_alive (thread_id
))
3288 write_enn (own_buf
);
3292 response_needed
= 0;
3294 /* Restarting the inferior is only supported in the extended
3296 if (extended_protocol
)
3298 if (target_running ())
3299 for_each_inferior (&all_processes
,
3300 kill_inferior_callback
);
3301 fprintf (stderr
, "GDBserver restarting\n");
3303 /* Wait till we are at 1st instruction in prog. */
3304 if (program_argv
!= NULL
)
3305 start_inferior (program_argv
);
3308 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3309 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3315 /* It is a request we don't understand. Respond with an
3316 empty packet so that gdb knows that we don't support this
3322 /* Extended (long) request. */
3323 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3327 /* It is a request we don't understand. Respond with an empty
3328 packet so that gdb knows that we don't support this
3334 if (new_packet_len
!= -1)
3335 putpkt_binary (own_buf
, new_packet_len
);
3339 response_needed
= 0;
3341 if (!extended_protocol
&& have_ran
&& !target_running ())
3343 /* In non-stop, defer exiting until GDB had a chance to query
3344 the whole vStopped list (until it gets an OK). */
3347 fprintf (stderr
, "GDBserver exiting\n");
3359 /* Event-loop callback for serial events. */
3362 handle_serial_event (int err
, gdb_client_data client_data
)
3365 fprintf (stderr
, "handling possible serial event\n");
3367 /* Really handle it. */
3368 if (process_serial_event () < 0)
3371 /* Be sure to not change the selected inferior behind GDB's back.
3372 Important in the non-stop mode asynchronous protocol. */
3373 set_desired_inferior (1);
3378 /* Event-loop callback for target events. */
3381 handle_target_event (int err
, gdb_client_data client_data
)
3384 fprintf (stderr
, "handling possible target event\n");
3386 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3389 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3391 int pid
= ptid_get_pid (last_ptid
);
3392 struct process_info
*process
= find_process_pid (pid
);
3393 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3395 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3396 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3398 mark_breakpoints_out (process
);
3399 mourn_inferior (process
);
3403 /* We're reporting this thread as stopped. Update its
3404 "want-stopped" state to what the client wants, until it
3405 gets a new resume action. */
3406 current_inferior
->last_resume_kind
= resume_stop
;
3407 current_inferior
->last_status
= last_status
;
3412 if (!target_running ())
3414 /* The last process exited. We're done. */
3418 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3420 /* A thread stopped with a signal, but gdb isn't
3421 connected to handle it. Pass it down to the
3422 inferior, as if it wasn't being traced. */
3423 struct thread_resume resume_info
;
3427 "GDB not connected; forwarding event %d for [%s]\n",
3428 (int) last_status
.kind
,
3429 target_pid_to_str (last_ptid
));
3431 resume_info
.thread
= last_ptid
;
3432 resume_info
.kind
= resume_continue
;
3433 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3434 (*the_target
->resume
) (&resume_info
, 1);
3436 else if (debug_threads
)
3437 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3438 (int) last_status
.kind
,
3439 target_pid_to_str (last_ptid
));
3443 /* Something interesting. Tell GDB about it. */
3444 push_event (last_ptid
, &last_status
);
3448 /* Be sure to not change the selected inferior behind GDB's back.
3449 Important in the non-stop mode asynchronous protocol. */
3450 set_desired_inferior (1);