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/>. */
33 ptid_t general_thread
;
37 static int extended_protocol
;
38 static int response_needed
;
39 static int exit_requested
;
41 /* --once: Exit after the first connection has closed. */
47 /* Whether we should attempt to disable the operating system's address
48 space randomization feature before starting an inferior. */
49 int disable_randomization
= 1;
51 static char **program_argv
, **wrapper_argv
;
53 /* Enable miscellaneous debugging output. The name is historical - it
54 was originally used to debug LinuxThreads support. */
57 /* Enable debugging of h/w breakpoint/watchpoint support. */
60 int pass_signals
[TARGET_SIGNAL_LAST
];
64 const char *gdbserver_xmltarget
;
66 /* The PID of the originally created or attached inferior. Used to
67 send signals to the process when GDB sends us an asynchronous interrupt
68 (user hitting Control-C in the client), and to wait for the child to exit
69 when no longer debugging it. */
71 unsigned long signal_pid
;
74 /* A file descriptor for the controlling terminal. */
77 /* TERMINAL_FD's original foreground group. */
78 pid_t old_foreground_pgrp
;
80 /* Hand back terminal ownership to the original foreground group. */
83 restore_old_foreground_pgrp (void)
85 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
89 /* Set if you want to disable optional thread related packets support
90 in gdbserver, for the sake of testing GDB against stubs that don't
92 int disable_packet_vCont
;
93 int disable_packet_Tthread
;
94 int disable_packet_qC
;
95 int disable_packet_qfThreadInfo
;
97 /* Last status reported to GDB. */
98 static struct target_waitstatus last_status
;
99 static ptid_t last_ptid
;
101 static char *own_buf
;
102 static unsigned char *mem_buf
;
104 /* Structure holding information relative to a single stop reply. We
105 keep a queue of these (really a singly-linked list) to push to GDB
109 /* Pointer to next in list. */
110 struct vstop_notif
*next
;
112 /* Thread or process that got the event. */
116 struct target_waitstatus status
;
119 /* The pending stop replies list head. */
120 static struct vstop_notif
*notif_queue
= NULL
;
122 /* Put a stop reply to the stop reply queue. */
125 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
127 struct vstop_notif
*new_notif
;
129 new_notif
= xmalloc (sizeof (*new_notif
));
130 new_notif
->next
= NULL
;
131 new_notif
->ptid
= ptid
;
132 new_notif
->status
= *status
;
136 struct vstop_notif
*tail
;
137 for (tail
= notif_queue
;
141 tail
->next
= new_notif
;
144 notif_queue
= new_notif
;
149 struct vstop_notif
*n
;
151 for (n
= notif_queue
; n
; n
= n
->next
)
154 fprintf (stderr
, "pending stop replies: %d\n", i
);
158 /* Place an event in the stop reply queue, and push a notification if
159 we aren't sending one yet. */
162 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
164 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
166 queue_stop_reply (ptid
, status
);
168 /* If this is the first stop reply in the queue, then inform GDB
169 about it, by sending a Stop notification. */
170 if (notif_queue
->next
== NULL
)
175 prepare_resume_reply (p
,
176 notif_queue
->ptid
, ¬if_queue
->status
);
177 putpkt_notif (own_buf
);
181 /* Get rid of the currently pending stop replies for PID. If PID is
182 -1, then apply to all processes. */
185 discard_queued_stop_replies (int pid
)
187 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
189 for (reply
= notif_queue
; reply
; reply
= next
)
194 || ptid_get_pid (reply
->ptid
) == pid
)
196 if (reply
== notif_queue
)
199 prev
->next
= reply
->next
;
208 /* If there are more stop replies to push, push one now. */
211 send_next_stop_reply (char *own_buf
)
214 prepare_resume_reply (own_buf
,
216 ¬if_queue
->status
);
222 target_running (void)
224 return all_threads
.head
!= NULL
;
228 start_inferior (char **argv
)
230 char **new_argv
= argv
;
232 if (wrapper_argv
!= NULL
)
236 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
238 for (i
= 0; argv
[i
] != NULL
; i
++)
240 new_argv
= alloca (sizeof (char *) * count
);
242 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
243 new_argv
[count
++] = wrapper_argv
[i
];
244 for (i
= 0; argv
[i
] != NULL
; i
++)
245 new_argv
[count
++] = argv
[i
];
246 new_argv
[count
] = NULL
;
252 for (i
= 0; new_argv
[i
]; ++i
)
253 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
258 signal (SIGTTOU
, SIG_DFL
);
259 signal (SIGTTIN
, SIG_DFL
);
262 signal_pid
= create_inferior (new_argv
[0], new_argv
);
264 /* FIXME: we don't actually know at this point that the create
265 actually succeeded. We won't know that until we wait. */
266 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
271 signal (SIGTTOU
, SIG_IGN
);
272 signal (SIGTTIN
, SIG_IGN
);
273 terminal_fd
= fileno (stderr
);
274 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
275 tcsetpgrp (terminal_fd
, signal_pid
);
276 atexit (restore_old_foreground_pgrp
);
279 if (wrapper_argv
!= NULL
)
281 struct thread_resume resume_info
;
283 resume_info
.thread
= pid_to_ptid (signal_pid
);
284 resume_info
.kind
= resume_continue
;
287 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
289 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
294 (*the_target
->resume
) (&resume_info
, 1);
296 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
297 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
300 current_inferior
->last_resume_kind
= resume_stop
;
301 current_inferior
->last_status
= last_status
;
303 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
305 current_inferior
->last_resume_kind
= resume_stop
;
306 current_inferior
->last_status
= last_status
;
310 /* Wait till we are at 1st instruction in program, return new pid
311 (assuming success). */
312 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
314 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
315 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
317 current_inferior
->last_resume_kind
= resume_stop
;
318 current_inferior
->last_status
= last_status
;
325 attach_inferior (int pid
)
327 /* myattach should return -1 if attaching is unsupported,
328 0 if it succeeded, and call error() otherwise. */
330 if (myattach (pid
) != 0)
333 fprintf (stderr
, "Attached; pid = %d\n", pid
);
336 /* FIXME - It may be that we should get the SIGNAL_PID from the
337 attach function, so that it can be the main thread instead of
338 whichever we were told to attach to. */
341 /* Clear this so the backend doesn't get confused, thinking
342 CONT_THREAD died, and it needs to resume all threads. */
343 cont_thread
= null_ptid
;
347 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
349 /* GDB knows to ignore the first SIGSTOP after attaching to a running
350 process using the "attach" command, but this is different; it's
351 just using "target remote". Pretend it's just starting up. */
352 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
353 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
354 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
356 current_inferior
->last_resume_kind
= resume_stop
;
357 current_inferior
->last_status
= last_status
;
363 extern int remote_debug
;
365 /* Decode a qXfer read request. Return 0 if everything looks OK,
369 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
371 /* After the read marker and annex, qXfer looks like a
372 traditional 'm' packet. */
373 decode_m_packet (buf
, ofs
, len
);
379 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
381 /* Extract and NUL-terminate the object. */
383 while (*buf
&& *buf
!= ':')
389 /* Extract and NUL-terminate the read/write action. */
391 while (*buf
&& *buf
!= ':')
397 /* Extract and NUL-terminate the annex. */
399 while (*buf
&& *buf
!= ':')
409 /* Write the response to a successful qXfer read. Returns the
410 length of the (binary) data stored in BUF, corresponding
411 to as much of DATA/LEN as we could fit. IS_MORE controls
412 the first character of the response. */
414 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
423 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
427 /* Handle all of the extended 'Q' packets. */
430 handle_general_set (char *own_buf
)
432 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
434 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
435 const char *p
= own_buf
+ strlen ("QPassSignals:");
438 p
= decode_address_to_semicolon (&cursig
, p
);
439 for (i
= 0; i
< numsigs
; i
++)
445 /* Keep looping, to clear the remaining signals. */
448 p
= decode_address_to_semicolon (&cursig
, p
);
453 strcpy (own_buf
, "OK");
457 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
461 fprintf (stderr
, "[noack mode enabled]\n");
470 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
472 char *mode
= own_buf
+ 9;
476 if (strcmp (mode
, "0") == 0)
478 else if (strcmp (mode
, "1") == 0)
482 /* We don't know what this mode is, so complain to
484 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
490 req_str
= req
? "non-stop" : "all-stop";
491 if (start_non_stop (req
) != 0)
493 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
501 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
507 if (strncmp ("QDisableRandomization:", own_buf
,
508 strlen ("QDisableRandomization:")) == 0)
510 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
513 unpack_varlen_hex (packet
, &setting
);
514 disable_randomization
= setting
;
518 if (disable_randomization
)
519 fprintf (stderr
, "[address space randomization disabled]\n");
521 fprintf (stderr
, "[address space randomization enabled]\n");
528 if (target_supports_tracepoints ()
529 && handle_tracepoint_general_set (own_buf
))
532 /* Otherwise we didn't know what packet it was. Say we didn't
538 get_features_xml (const char *annex
)
540 /* gdbserver_xmltarget defines what to return when looking
541 for the "target.xml" file. Its contents can either be
542 verbatim XML code (prefixed with a '@') or else the name
543 of the actual XML file to be used in place of "target.xml".
545 This variable is set up from the auto-generated
546 init_registers_... routine for the current target. */
548 if (gdbserver_xmltarget
549 && strcmp (annex
, "target.xml") == 0)
551 if (*gdbserver_xmltarget
== '@')
552 return gdbserver_xmltarget
+ 1;
554 annex
= gdbserver_xmltarget
;
559 extern const char *const xml_builtin
[][2];
562 /* Look for the annex. */
563 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
564 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
567 if (xml_builtin
[i
][0] != NULL
)
568 return xml_builtin
[i
][1];
576 monitor_show_help (void)
578 monitor_output ("The following monitor commands are supported:\n");
579 monitor_output (" set debug <0|1>\n");
580 monitor_output (" Enable general debugging messages\n");
581 monitor_output (" set debug-hw-points <0|1>\n");
582 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
583 monitor_output (" set remote-debug <0|1>\n");
584 monitor_output (" Enable remote protocol debugging messages\n");
585 monitor_output (" exit\n");
586 monitor_output (" Quit GDBserver\n");
589 /* Read trace frame or inferior memory. Returns the number of bytes
590 actually read, zero when no further transfer is possible, and -1 on
591 error. Return of a positive value smaller than LEN does not
592 indicate there's no more to be read, only the end of the transfer.
593 E.g., when GDB reads memory from a traceframe, a first request may
594 be served from a memory block that does not cover the whole request
595 length. A following request gets the rest served from either
596 another block (of the same traceframe) or from the read-only
600 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
604 if (current_traceframe
>= 0)
607 ULONGEST length
= len
;
609 if (traceframe_read_mem (current_traceframe
,
610 memaddr
, myaddr
, len
, &nbytes
))
612 /* Data read from trace buffer, we're done. */
615 if (!in_readonly_region (memaddr
, length
))
617 /* Otherwise we have a valid readonly case, fall through. */
618 /* (assume no half-trace half-real blocks for now) */
621 res
= prepare_to_access_memory ();
624 res
= read_inferior_memory (memaddr
, myaddr
, len
);
625 done_accessing_memory ();
627 return res
== 0 ? len
: -1;
633 /* Write trace frame or inferior memory. Actually, writing to trace
634 frames is forbidden. */
637 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
639 if (current_traceframe
>= 0)
645 ret
= prepare_to_access_memory ();
648 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
649 done_accessing_memory ();
655 /* Subroutine of handle_search_memory to simplify it. */
658 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
659 gdb_byte
*pattern
, unsigned pattern_len
,
660 gdb_byte
*search_buf
,
661 unsigned chunk_size
, unsigned search_buf_size
,
662 CORE_ADDR
*found_addrp
)
664 /* Prime the search buffer. */
666 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
669 warning ("Unable to access target memory at 0x%lx, halting search.",
674 /* Perform the search.
676 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
677 When we've scanned N bytes we copy the trailing bytes to the start and
678 read in another N bytes. */
680 while (search_space_len
>= pattern_len
)
683 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
687 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
689 if (found_ptr
!= NULL
)
691 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
692 *found_addrp
= found_addr
;
696 /* Not found in this chunk, skip to next chunk. */
698 /* Don't let search_space_len wrap here, it's unsigned. */
699 if (search_space_len
>= chunk_size
)
700 search_space_len
-= chunk_size
;
702 search_space_len
= 0;
704 if (search_space_len
>= pattern_len
)
706 unsigned keep_len
= search_buf_size
- chunk_size
;
707 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
710 /* Copy the trailing part of the previous iteration to the front
711 of the buffer for the next iteration. */
712 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
714 nr_to_read
= (search_space_len
- keep_len
< chunk_size
715 ? search_space_len
- keep_len
718 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
719 nr_to_read
) != search_buf_size
)
721 warning ("Unable to access target memory "
722 "at 0x%lx, halting search.",
727 start_addr
+= chunk_size
;
736 /* Handle qSearch:memory packets. */
739 handle_search_memory (char *own_buf
, int packet_len
)
741 CORE_ADDR start_addr
;
742 CORE_ADDR search_space_len
;
744 unsigned int pattern_len
;
745 /* NOTE: also defined in find.c testcase. */
746 #define SEARCH_CHUNK_SIZE 16000
747 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
748 /* Buffer to hold memory contents for searching. */
749 gdb_byte
*search_buf
;
750 unsigned search_buf_size
;
752 CORE_ADDR found_addr
;
753 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
755 pattern
= malloc (packet_len
);
758 error ("Unable to allocate memory to perform the search");
759 strcpy (own_buf
, "E00");
762 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
763 packet_len
- cmd_name_len
,
764 &start_addr
, &search_space_len
,
765 pattern
, &pattern_len
) < 0)
768 error ("Error in parsing qSearch:memory packet");
769 strcpy (own_buf
, "E00");
773 search_buf_size
= chunk_size
+ pattern_len
- 1;
775 /* No point in trying to allocate a buffer larger than the search space. */
776 if (search_space_len
< search_buf_size
)
777 search_buf_size
= search_space_len
;
779 search_buf
= malloc (search_buf_size
);
780 if (search_buf
== NULL
)
783 error ("Unable to allocate memory to perform the search");
784 strcpy (own_buf
, "E00");
788 found
= handle_search_memory_1 (start_addr
, search_space_len
,
789 pattern
, pattern_len
,
790 search_buf
, chunk_size
, search_buf_size
,
794 sprintf (own_buf
, "1,%lx", (long) found_addr
);
796 strcpy (own_buf
, "0");
798 strcpy (own_buf
, "E00");
804 #define require_running(BUF) \
805 if (!target_running ()) \
811 /* Handle monitor commands not handled by target-specific handlers. */
814 handle_monitor_command (char *mon
, char *own_buf
)
816 if (strcmp (mon
, "set debug 1") == 0)
819 monitor_output ("Debug output enabled.\n");
821 else if (strcmp (mon
, "set debug 0") == 0)
824 monitor_output ("Debug output disabled.\n");
826 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
829 monitor_output ("H/W point debugging output enabled.\n");
831 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
834 monitor_output ("H/W point debugging output disabled.\n");
836 else if (strcmp (mon
, "set remote-debug 1") == 0)
839 monitor_output ("Protocol debug output enabled.\n");
841 else if (strcmp (mon
, "set remote-debug 0") == 0)
844 monitor_output ("Protocol debug output disabled.\n");
846 else if (strcmp (mon
, "help") == 0)
847 monitor_show_help ();
848 else if (strcmp (mon
, "exit") == 0)
852 monitor_output ("Unknown monitor command.\n\n");
853 monitor_show_help ();
858 /* Associates a callback with each supported qXfer'able object. */
862 /* The object this handler handles. */
865 /* Request that the target transfer up to LEN 8-bit bytes of the
866 target's OBJECT. The OFFSET, for a seekable object, specifies
867 the starting point. The ANNEX can be used to provide additional
868 data-specific information to the target.
870 Return the number of bytes actually transfered, zero when no
871 further transfer is possible, -1 on error, and -2 when the
872 transfer is not supported. Return of a positive value smaller
873 than LEN does not indicate the end of the object, only the end of
876 One, and only one, of readbuf or writebuf must be non-NULL. */
877 int (*xfer
) (const char *annex
,
878 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
879 ULONGEST offset
, LONGEST len
);
882 /* Handle qXfer:auxv:read. */
885 handle_qxfer_auxv (const char *annex
,
886 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
887 ULONGEST offset
, LONGEST len
)
889 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
892 if (annex
[0] != '\0' || !target_running ())
895 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
898 /* Handle qXfer:features:read. */
901 handle_qxfer_features (const char *annex
,
902 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
903 ULONGEST offset
, LONGEST len
)
905 const char *document
;
908 if (writebuf
!= NULL
)
911 if (!target_running ())
914 /* Grab the correct annex. */
915 document
= get_features_xml (annex
);
916 if (document
== NULL
)
919 total_len
= strlen (document
);
921 if (offset
> total_len
)
924 if (offset
+ len
> total_len
)
925 len
= total_len
- offset
;
927 memcpy (readbuf
, document
+ offset
, len
);
931 /* Handle qXfer:libraries:read. */
934 handle_qxfer_libraries (const char *annex
,
935 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
936 ULONGEST offset
, LONGEST len
)
938 unsigned int total_len
;
940 struct inferior_list_entry
*dll_ptr
;
942 if (writebuf
!= NULL
)
945 if (annex
[0] != '\0' || !target_running ())
948 /* Do not confuse this packet with qXfer:libraries-svr4:read. */
949 if (the_target
->qxfer_libraries_svr4
!= NULL
)
952 /* Over-estimate the necessary memory. Assume that every character
953 in the library name must be escaped. */
955 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
956 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
958 document
= malloc (total_len
);
959 if (document
== NULL
)
962 strcpy (document
, "<library-list>\n");
963 p
= document
+ strlen (document
);
965 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
967 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
970 strcpy (p
, " <library name=\"");
972 name
= xml_escape_text (dll
->name
);
976 strcpy (p
, "\"><segment address=\"");
978 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
980 strcpy (p
, "\"/></library>\n");
984 strcpy (p
, "</library-list>\n");
986 total_len
= strlen (document
);
988 if (offset
> total_len
)
994 if (offset
+ len
> total_len
)
995 len
= total_len
- offset
;
997 memcpy (readbuf
, document
+ offset
, len
);
1002 /* Handle qXfer:libraries-svr4:read. */
1005 handle_qxfer_libraries_svr4 (const char *annex
,
1006 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1007 ULONGEST offset
, LONGEST len
)
1009 if (writebuf
!= NULL
)
1012 if (annex
[0] != '\0' || !target_running ()
1013 || the_target
->qxfer_libraries_svr4
== NULL
)
1016 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1019 /* Handle qXfer:osadata:read. */
1022 handle_qxfer_osdata (const char *annex
,
1023 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1024 ULONGEST offset
, LONGEST len
)
1026 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1029 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1032 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1035 handle_qxfer_siginfo (const char *annex
,
1036 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1037 ULONGEST offset
, LONGEST len
)
1039 if (the_target
->qxfer_siginfo
== NULL
)
1042 if (annex
[0] != '\0' || !target_running ())
1045 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1048 /* Handle qXfer:spu:read and qXfer:spu:write. */
1051 handle_qxfer_spu (const char *annex
,
1052 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1053 ULONGEST offset
, LONGEST len
)
1055 if (the_target
->qxfer_spu
== NULL
)
1058 if (!target_running ())
1061 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1064 /* Handle qXfer:statictrace:read. */
1067 handle_qxfer_statictrace (const char *annex
,
1068 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1069 ULONGEST offset
, LONGEST len
)
1073 if (writebuf
!= NULL
)
1076 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1079 if (traceframe_read_sdata (current_traceframe
, offset
,
1080 readbuf
, len
, &nbytes
))
1085 /* Helper for handle_qxfer_threads. */
1088 handle_qxfer_threads_proper (struct buffer
*buffer
)
1090 struct inferior_list_entry
*thread
;
1092 buffer_grow_str (buffer
, "<threads>\n");
1094 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1096 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1101 write_ptid (ptid_s
, ptid
);
1103 if (the_target
->core_of_thread
)
1104 core
= (*the_target
->core_of_thread
) (ptid
);
1108 sprintf (core_s
, "%d", core
);
1109 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1114 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1119 buffer_grow_str0 (buffer
, "</threads>\n");
1122 /* Handle qXfer:threads:read. */
1125 handle_qxfer_threads (const char *annex
,
1126 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1127 ULONGEST offset
, LONGEST len
)
1129 static char *result
= 0;
1130 static unsigned int result_length
= 0;
1132 if (writebuf
!= NULL
)
1135 if (!target_running () || annex
[0] != '\0')
1140 struct buffer buffer
;
1141 /* When asked for data at offset 0, generate everything and store into
1142 'result'. Successive reads will be served off 'result'. */
1146 buffer_init (&buffer
);
1148 handle_qxfer_threads_proper (&buffer
);
1150 result
= buffer_finish (&buffer
);
1151 result_length
= strlen (result
);
1152 buffer_free (&buffer
);
1155 if (offset
>= result_length
)
1157 /* We're out of data. */
1164 if (len
> result_length
- offset
)
1165 len
= result_length
- offset
;
1167 memcpy (readbuf
, result
+ offset
, len
);
1172 /* Handle qXfer:traceframe-info:read. */
1175 handle_qxfer_traceframe_info (const char *annex
,
1176 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1177 ULONGEST offset
, LONGEST len
)
1179 static char *result
= 0;
1180 static unsigned int result_length
= 0;
1182 if (writebuf
!= NULL
)
1185 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1190 struct buffer buffer
;
1192 /* When asked for data at offset 0, generate everything and
1193 store into 'result'. Successive reads will be served off
1197 buffer_init (&buffer
);
1199 traceframe_read_info (current_traceframe
, &buffer
);
1201 result
= buffer_finish (&buffer
);
1202 result_length
= strlen (result
);
1203 buffer_free (&buffer
);
1206 if (offset
>= result_length
)
1208 /* We're out of data. */
1215 if (len
> result_length
- offset
)
1216 len
= result_length
- offset
;
1218 memcpy (readbuf
, result
+ offset
, len
);
1222 /* Handle qXfer:fdpic:read. */
1225 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1226 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1228 if (the_target
->read_loadmap
== NULL
)
1231 if (!target_running ())
1234 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1237 static const struct qxfer qxfer_packets
[] =
1239 { "auxv", handle_qxfer_auxv
},
1240 { "fdpic", handle_qxfer_fdpic
},
1241 { "features", handle_qxfer_features
},
1242 { "libraries", handle_qxfer_libraries
},
1243 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1244 { "osdata", handle_qxfer_osdata
},
1245 { "siginfo", handle_qxfer_siginfo
},
1246 { "spu", handle_qxfer_spu
},
1247 { "statictrace", handle_qxfer_statictrace
},
1248 { "threads", handle_qxfer_threads
},
1249 { "traceframe-info", handle_qxfer_traceframe_info
},
1253 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1261 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1264 /* Grab the object, r/w and annex. */
1265 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1267 write_enn (own_buf
);
1272 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1275 const struct qxfer
*q
= &qxfer_packets
[i
];
1277 if (strcmp (object
, q
->object
) == 0)
1279 if (strcmp (rw
, "read") == 0)
1281 unsigned char *data
;
1286 /* Grab the offset and length. */
1287 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1289 write_enn (own_buf
);
1293 /* Read one extra byte, as an indicator of whether there is
1295 if (len
> PBUFSIZ
- 2)
1297 data
= malloc (len
+ 1);
1300 write_enn (own_buf
);
1303 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1310 write_enn (own_buf
);
1312 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1314 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1319 else if (strcmp (rw
, "write") == 0)
1324 unsigned char *data
;
1326 strcpy (own_buf
, "E00");
1327 data
= malloc (packet_len
- (offset
- own_buf
));
1330 write_enn (own_buf
);
1333 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1334 &ofs
, &len
, data
) < 0)
1337 write_enn (own_buf
);
1341 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1348 write_enn (own_buf
);
1350 sprintf (own_buf
, "%x", n
);
1363 /* Table used by the crc32 function to calcuate the checksum. */
1365 static unsigned int crc32_table
[256] =
1368 /* Compute 32 bit CRC from inferior memory.
1370 On success, return 32 bit CRC.
1371 On failure, return (unsigned long long) -1. */
1373 static unsigned long long
1374 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1376 if (!crc32_table
[1])
1378 /* Initialize the CRC table and the decoding table. */
1382 for (i
= 0; i
< 256; i
++)
1384 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1385 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1392 unsigned char byte
= 0;
1394 /* Return failure if memory read fails. */
1395 if (read_inferior_memory (base
, &byte
, 1) != 0)
1396 return (unsigned long long) -1;
1398 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1401 return (unsigned long long) crc
;
1404 /* Handle all of the extended 'q' packets. */
1407 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1409 static struct inferior_list_entry
*thread_ptr
;
1411 /* Reply the current thread id. */
1412 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1415 require_running (own_buf
);
1417 if (!ptid_equal (general_thread
, null_ptid
)
1418 && !ptid_equal (general_thread
, minus_one_ptid
))
1419 gdb_id
= general_thread
;
1422 thread_ptr
= all_threads
.head
;
1423 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1426 sprintf (own_buf
, "QC");
1428 write_ptid (own_buf
, gdb_id
);
1432 if (strcmp ("qSymbol::", own_buf
) == 0)
1434 /* GDB is suggesting new symbols have been loaded. This may
1435 mean a new shared library has been detected as loaded, so
1436 take the opportunity to check if breakpoints we think are
1437 inserted, still are. Note that it isn't guaranteed that
1438 we'll see this when a shared library is loaded, and nor will
1439 we see this for unloads (although breakpoints in unloaded
1440 libraries shouldn't trigger), as GDB may not find symbols for
1441 the library at all. We also re-validate breakpoints when we
1442 see a second GDB breakpoint for the same address, and or when
1443 we access breakpoint shadows. */
1444 validate_breakpoints ();
1446 if (target_supports_tracepoints ())
1447 tracepoint_look_up_symbols ();
1449 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1450 (*the_target
->look_up_symbols
) ();
1452 strcpy (own_buf
, "OK");
1456 if (!disable_packet_qfThreadInfo
)
1458 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1462 require_running (own_buf
);
1463 thread_ptr
= all_threads
.head
;
1466 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1467 write_ptid (own_buf
, gdb_id
);
1468 thread_ptr
= thread_ptr
->next
;
1472 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1476 require_running (own_buf
);
1477 if (thread_ptr
!= NULL
)
1480 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1481 write_ptid (own_buf
, gdb_id
);
1482 thread_ptr
= thread_ptr
->next
;
1487 sprintf (own_buf
, "l");
1493 if (the_target
->read_offsets
!= NULL
1494 && strcmp ("qOffsets", own_buf
) == 0)
1496 CORE_ADDR text
, data
;
1498 require_running (own_buf
);
1499 if (the_target
->read_offsets (&text
, &data
))
1500 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1501 (long)text
, (long)data
, (long)data
);
1503 write_enn (own_buf
);
1508 /* Protocol features query. */
1509 if (strncmp ("qSupported", own_buf
, 10) == 0
1510 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1512 char *p
= &own_buf
[10];
1513 int gdb_supports_qRelocInsn
= 0;
1515 /* Start processing qSupported packet. */
1516 target_process_qsupported (NULL
);
1518 /* Process each feature being provided by GDB. The first
1519 feature will follow a ':', and latter features will follow
1523 char **qsupported
= NULL
;
1527 /* Two passes, to avoid nested strtok calls in
1528 target_process_qsupported. */
1529 for (p
= strtok (p
+ 1, ";");
1531 p
= strtok (NULL
, ";"))
1534 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1535 qsupported
[count
- 1] = xstrdup (p
);
1538 for (i
= 0; i
< count
; i
++)
1541 if (strcmp (p
, "multiprocess+") == 0)
1543 /* GDB supports and wants multi-process support if
1545 if (target_supports_multi_process ())
1548 else if (strcmp (p
, "qRelocInsn+") == 0)
1550 /* GDB supports relocate instruction requests. */
1551 gdb_supports_qRelocInsn
= 1;
1554 target_process_qsupported (p
);
1562 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1564 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1565 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1568 /* We do not have any hook to indicate whether the non-SVR4 target
1569 backend supports qXfer:libraries:read, so always report it. */
1570 strcat (own_buf
, ";qXfer:libraries:read+");
1573 if (the_target
->read_auxv
!= NULL
)
1574 strcat (own_buf
, ";qXfer:auxv:read+");
1576 if (the_target
->qxfer_spu
!= NULL
)
1577 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1579 if (the_target
->qxfer_siginfo
!= NULL
)
1580 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1582 if (the_target
->read_loadmap
!= NULL
)
1583 strcat (own_buf
, ";qXfer:fdpic:read+");
1585 /* We always report qXfer:features:read, as targets may
1586 install XML files on a subsequent call to arch_setup.
1587 If we reported to GDB on startup that we don't support
1588 qXfer:feature:read at all, we will never be re-queried. */
1589 strcat (own_buf
, ";qXfer:features:read+");
1591 if (transport_is_reliable
)
1592 strcat (own_buf
, ";QStartNoAckMode+");
1594 if (the_target
->qxfer_osdata
!= NULL
)
1595 strcat (own_buf
, ";qXfer:osdata:read+");
1597 if (target_supports_multi_process ())
1598 strcat (own_buf
, ";multiprocess+");
1600 if (target_supports_non_stop ())
1601 strcat (own_buf
, ";QNonStop+");
1603 if (target_supports_disable_randomization ())
1604 strcat (own_buf
, ";QDisableRandomization+");
1606 strcat (own_buf
, ";qXfer:threads:read+");
1608 if (target_supports_tracepoints ())
1610 strcat (own_buf
, ";ConditionalTracepoints+");
1611 strcat (own_buf
, ";TraceStateVariables+");
1612 strcat (own_buf
, ";TracepointSource+");
1613 strcat (own_buf
, ";DisconnectedTracing+");
1614 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1615 strcat (own_buf
, ";FastTracepoints+");
1616 strcat (own_buf
, ";StaticTracepoints+");
1617 strcat (own_buf
, ";InstallInTrace+");
1618 strcat (own_buf
, ";qXfer:statictrace:read+");
1619 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1620 strcat (own_buf
, ";EnableDisableTracepoints+");
1621 strcat (own_buf
, ";tracenz+");
1624 /* Support target-side breakpoint conditions. */
1625 strcat (own_buf
, ";ConditionalBreakpoints+");
1630 /* Thread-local storage support. */
1631 if (the_target
->get_tls_address
!= NULL
1632 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1634 char *p
= own_buf
+ 12;
1635 CORE_ADDR parts
[2], address
= 0;
1637 ptid_t ptid
= null_ptid
;
1639 require_running (own_buf
);
1641 for (i
= 0; i
< 3; i
++)
1649 p2
= strchr (p
, ',');
1662 ptid
= read_ptid (p
, NULL
);
1664 decode_address (&parts
[i
- 1], p
, len
);
1668 if (p
!= NULL
|| i
< 3)
1672 struct thread_info
*thread
= find_thread_ptid (ptid
);
1677 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1683 strcpy (own_buf
, paddress(address
));
1688 write_enn (own_buf
);
1692 /* Otherwise, pretend we do not understand this packet. */
1695 /* Windows OS Thread Information Block address support. */
1696 if (the_target
->get_tib_address
!= NULL
1697 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1702 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1704 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1707 strcpy (own_buf
, paddress(tlb
));
1712 write_enn (own_buf
);
1718 /* Handle "monitor" commands. */
1719 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1721 char *mon
= malloc (PBUFSIZ
);
1722 int len
= strlen (own_buf
+ 6);
1726 write_enn (own_buf
);
1730 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1732 write_enn (own_buf
);
1736 mon
[len
/ 2] = '\0';
1740 if (the_target
->handle_monitor_command
== NULL
1741 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1742 /* Default processing. */
1743 handle_monitor_command (mon
, own_buf
);
1749 if (strncmp ("qSearch:memory:", own_buf
,
1750 sizeof ("qSearch:memory:") - 1) == 0)
1752 require_running (own_buf
);
1753 handle_search_memory (own_buf
, packet_len
);
1757 if (strcmp (own_buf
, "qAttached") == 0
1758 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1760 struct process_info
*process
;
1762 if (own_buf
[sizeof ("qAttached") - 1])
1764 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1765 process
= (struct process_info
*)
1766 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1770 require_running (own_buf
);
1771 process
= current_process ();
1774 if (process
== NULL
)
1776 write_enn (own_buf
);
1780 strcpy (own_buf
, process
->attached
? "1" : "0");
1784 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1786 /* CRC check (compare-section). */
1790 unsigned long long crc
;
1792 require_running (own_buf
);
1793 base
= strtoul (own_buf
+ 5, &comma
, 16);
1794 if (*comma
++ != ',')
1796 write_enn (own_buf
);
1799 len
= strtoul (comma
, NULL
, 16);
1800 crc
= crc32 (base
, len
, 0xffffffff);
1801 /* Check for memory failure. */
1802 if (crc
== (unsigned long long) -1)
1804 write_enn (own_buf
);
1807 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1811 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1814 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1817 /* Otherwise we didn't know what packet it was. Say we didn't
1822 static void gdb_wants_all_threads_stopped (void);
1824 /* Parse vCont packets. */
1826 handle_v_cont (char *own_buf
)
1830 struct thread_resume
*resume_info
;
1831 struct thread_resume default_action
= {{0}};
1833 /* Count the number of semicolons in the packet. There should be one
1834 for every action. */
1840 p
= strchr (p
, ';');
1843 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1844 if (resume_info
== NULL
)
1852 if (p
[0] == 's' || p
[0] == 'S')
1853 resume_info
[i
].kind
= resume_step
;
1854 else if (p
[0] == 'c' || p
[0] == 'C')
1855 resume_info
[i
].kind
= resume_continue
;
1856 else if (p
[0] == 't')
1857 resume_info
[i
].kind
= resume_stop
;
1861 if (p
[0] == 'S' || p
[0] == 'C')
1864 sig
= strtol (p
+ 1, &q
, 16);
1869 if (!target_signal_to_host_p (sig
))
1871 resume_info
[i
].sig
= target_signal_to_host (sig
);
1875 resume_info
[i
].sig
= 0;
1881 resume_info
[i
].thread
= minus_one_ptid
;
1882 default_action
= resume_info
[i
];
1884 /* Note: we don't increment i here, we'll overwrite this entry
1885 the next time through. */
1887 else if (p
[0] == ':')
1889 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1894 if (p
[0] != ';' && p
[0] != 0)
1897 resume_info
[i
].thread
= ptid
;
1904 resume_info
[i
] = default_action
;
1906 /* Still used in occasional places in the backend. */
1908 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1909 && resume_info
[0].kind
!= resume_stop
)
1910 cont_thread
= resume_info
[0].thread
;
1912 cont_thread
= minus_one_ptid
;
1913 set_desired_inferior (0);
1918 (*the_target
->resume
) (resume_info
, n
);
1926 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1928 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1929 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1930 current_inferior
->last_status
= last_status
;
1932 /* From the client's perspective, all-stop mode always stops all
1933 threads implicitly (and the target backend has already done
1934 so by now). Tag all threads as "want-stopped", so we don't
1935 resume them implicitly without the client telling us to. */
1936 gdb_wants_all_threads_stopped ();
1937 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1938 disable_async_io ();
1940 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1941 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1942 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1947 write_enn (own_buf
);
1952 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1954 handle_v_attach (char *own_buf
)
1958 pid
= strtol (own_buf
+ 8, NULL
, 16);
1959 if (pid
!= 0 && attach_inferior (pid
) == 0)
1961 /* Don't report shared library events after attaching, even if
1962 some libraries are preloaded. GDB will always poll the
1963 library list. Avoids the "stopped by shared library event"
1964 notice on the GDB side. */
1969 /* In non-stop, we don't send a resume reply. Stop events
1970 will follow up using the normal notification
1975 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1981 write_enn (own_buf
);
1986 /* Run a new program. Return 1 if successful, 0 if failure. */
1988 handle_v_run (char *own_buf
)
1990 char *p
, *next_p
, **new_argv
;
1994 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2000 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2001 if (new_argv
== NULL
)
2003 write_enn (own_buf
);
2008 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2010 next_p
= strchr (p
, ';');
2012 next_p
= p
+ strlen (p
);
2014 if (i
== 0 && p
== next_p
)
2018 /* FIXME: Fail request if out of memory instead of dying. */
2019 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2020 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2021 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2030 if (new_argv
[0] == NULL
)
2032 /* GDB didn't specify a program to run. Use the program from the
2033 last run with the new argument list. */
2035 if (program_argv
== NULL
)
2037 write_enn (own_buf
);
2038 freeargv (new_argv
);
2042 new_argv
[0] = strdup (program_argv
[0]);
2043 if (new_argv
[0] == NULL
)
2045 write_enn (own_buf
);
2046 freeargv (new_argv
);
2051 /* Free the old argv and install the new one. */
2052 freeargv (program_argv
);
2053 program_argv
= new_argv
;
2055 start_inferior (program_argv
);
2056 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2058 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2060 /* In non-stop, sending a resume reply doesn't set the general
2061 thread, but GDB assumes a vRun sets it (this is so GDB can
2062 query which is the main thread of the new inferior. */
2064 general_thread
= last_ptid
;
2070 write_enn (own_buf
);
2075 /* Kill process. Return 1 if successful, 0 if failure. */
2077 handle_v_kill (char *own_buf
)
2080 char *p
= &own_buf
[6];
2082 pid
= strtol (p
, NULL
, 16);
2085 if (pid
!= 0 && kill_inferior (pid
) == 0)
2087 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2088 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2089 last_ptid
= pid_to_ptid (pid
);
2090 discard_queued_stop_replies (pid
);
2096 write_enn (own_buf
);
2101 /* Handle a 'vStopped' packet. */
2103 handle_v_stopped (char *own_buf
)
2105 /* If we're waiting for GDB to acknowledge a pending stop reply,
2106 consider that done. */
2109 struct vstop_notif
*head
;
2112 fprintf (stderr
, "vStopped: acking %s\n",
2113 target_pid_to_str (notif_queue
->ptid
));
2116 notif_queue
= notif_queue
->next
;
2120 /* Push another stop reply, or if there are no more left, an OK. */
2121 send_next_stop_reply (own_buf
);
2124 /* Handle all of the extended 'v' packets. */
2126 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2128 if (!disable_packet_vCont
)
2130 if (strncmp (own_buf
, "vCont;", 6) == 0)
2132 require_running (own_buf
);
2133 handle_v_cont (own_buf
);
2137 if (strncmp (own_buf
, "vCont?", 6) == 0)
2139 strcpy (own_buf
, "vCont;c;C;s;S;t");
2144 if (strncmp (own_buf
, "vFile:", 6) == 0
2145 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2148 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2150 if ((!extended_protocol
|| !multi_process
) && target_running ())
2152 fprintf (stderr
, "Already debugging a process\n");
2153 write_enn (own_buf
);
2156 handle_v_attach (own_buf
);
2160 if (strncmp (own_buf
, "vRun;", 5) == 0)
2162 if ((!extended_protocol
|| !multi_process
) && target_running ())
2164 fprintf (stderr
, "Already debugging a process\n");
2165 write_enn (own_buf
);
2168 handle_v_run (own_buf
);
2172 if (strncmp (own_buf
, "vKill;", 6) == 0)
2174 if (!target_running ())
2176 fprintf (stderr
, "No process to kill\n");
2177 write_enn (own_buf
);
2180 handle_v_kill (own_buf
);
2184 if (strncmp (own_buf
, "vStopped", 8) == 0)
2186 handle_v_stopped (own_buf
);
2190 /* Otherwise we didn't know what packet it was. Say we didn't
2196 /* Resume inferior and wait for another event. In non-stop mode,
2197 don't really wait here, but return immediatelly to the event
2200 myresume (char *own_buf
, int step
, int sig
)
2202 struct thread_resume resume_info
[2];
2204 int valid_cont_thread
;
2206 set_desired_inferior (0);
2208 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2209 && !ptid_equal (cont_thread
, minus_one_ptid
));
2211 if (step
|| sig
|| valid_cont_thread
)
2213 resume_info
[0].thread
2214 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2216 resume_info
[0].kind
= resume_step
;
2218 resume_info
[0].kind
= resume_continue
;
2219 resume_info
[0].sig
= sig
;
2223 if (!valid_cont_thread
)
2225 resume_info
[n
].thread
= minus_one_ptid
;
2226 resume_info
[n
].kind
= resume_continue
;
2227 resume_info
[n
].sig
= 0;
2234 (*the_target
->resume
) (resume_info
, n
);
2240 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2242 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2243 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2245 current_inferior
->last_resume_kind
= resume_stop
;
2246 current_inferior
->last_status
= last_status
;
2249 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2250 disable_async_io ();
2252 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2253 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2254 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2258 /* Callback for for_each_inferior. Make a new stop reply for each
2262 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2264 struct thread_info
*thread
= (struct thread_info
*) entry
;
2266 /* For now, assume targets that don't have this callback also don't
2267 manage the thread's last_status field. */
2268 if (the_target
->thread_stopped
== NULL
)
2270 /* Pass the last stop reply back to GDB, but don't notify
2272 queue_stop_reply (entry
->id
, &thread
->last_status
);
2276 if (thread_stopped (thread
))
2280 "Reporting thread %s as already stopped with %s\n",
2281 target_pid_to_str (entry
->id
),
2282 target_waitstatus_to_string (&thread
->last_status
));
2284 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2286 /* Pass the last stop reply back to GDB, but don't notify
2288 queue_stop_reply (entry
->id
, &thread
->last_status
);
2295 /* Set this inferior threads's state as "want-stopped". We won't
2296 resume this thread until the client gives us another action for
2300 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2302 struct thread_info
*thread
= (struct thread_info
*) entry
;
2304 thread
->last_resume_kind
= resume_stop
;
2306 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2308 /* Most threads are stopped implicitly (all-stop); tag that with
2310 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2311 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2315 /* Set all threads' states as "want-stopped". */
2318 gdb_wants_all_threads_stopped (void)
2320 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2323 /* Clear the gdb_detached flag of every process. */
2326 gdb_reattached_process (struct inferior_list_entry
*entry
)
2328 struct process_info
*process
= (struct process_info
*) entry
;
2330 process
->gdb_detached
= 0;
2333 /* Status handler for the '?' packet. */
2336 handle_status (char *own_buf
)
2338 /* GDB is connected, don't forward events to the target anymore. */
2339 for_each_inferior (&all_processes
, gdb_reattached_process
);
2341 /* In non-stop mode, we must send a stop reply for each stopped
2342 thread. In all-stop mode, just send one for the first stopped
2347 discard_queued_stop_replies (-1);
2348 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2350 /* The first is sent immediatly. OK is sent if there is no
2351 stopped thread, which is the same handling of the vStopped
2352 packet (by design). */
2353 send_next_stop_reply (own_buf
);
2358 stabilize_threads ();
2359 gdb_wants_all_threads_stopped ();
2361 if (all_threads
.head
)
2363 struct target_waitstatus status
;
2365 status
.kind
= TARGET_WAITKIND_STOPPED
;
2366 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2367 prepare_resume_reply (own_buf
,
2368 all_threads
.head
->id
, &status
);
2371 strcpy (own_buf
, "W00");
2376 gdbserver_version (void)
2378 printf ("GNU gdbserver %s%s\n"
2379 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2380 "gdbserver is free software, covered by the "
2381 "GNU General Public License.\n"
2382 "This gdbserver was configured as \"%s\"\n",
2383 PKGVERSION
, version
, host_name
);
2387 gdbserver_usage (FILE *stream
)
2389 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2390 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2391 "\tgdbserver [OPTIONS] --multi COMM\n"
2393 "COMM may either be a tty device (for serial debugging), or \n"
2394 "HOST:PORT to listen for a TCP connection.\n"
2397 " --debug Enable general debugging output.\n"
2398 " --remote-debug Enable remote protocol debugging output.\n"
2399 " --version Display version information and exit.\n"
2400 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2401 " --once Exit after the first connection has "
2403 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2404 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2408 gdbserver_show_disableable (FILE *stream
)
2410 fprintf (stream
, "Disableable packets:\n"
2411 " vCont \tAll vCont packets\n"
2412 " qC \tQuerying the current thread\n"
2413 " qfThreadInfo\tThread listing\n"
2414 " Tthread \tPassing the thread specifier in the "
2415 "T stop reply packet\n"
2416 " threads \tAll of the above\n");
2420 #undef require_running
2421 #define require_running(BUF) \
2422 if (!target_running ()) \
2429 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2431 int pid
= * (int *) args
;
2433 if (ptid_get_pid (entry
->id
) == pid
)
2440 kill_inferior_callback (struct inferior_list_entry
*entry
)
2442 struct process_info
*process
= (struct process_info
*) entry
;
2443 int pid
= ptid_get_pid (process
->head
.id
);
2445 kill_inferior (pid
);
2446 discard_queued_stop_replies (pid
);
2449 /* Callback for for_each_inferior to detach or kill the inferior,
2450 depending on whether we attached to it or not.
2451 We inform the user whether we're detaching or killing the process
2452 as this is only called when gdbserver is about to exit. */
2455 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2457 struct process_info
*process
= (struct process_info
*) entry
;
2458 int pid
= ptid_get_pid (process
->head
.id
);
2460 if (process
->attached
)
2461 detach_inferior (pid
);
2463 kill_inferior (pid
);
2465 discard_queued_stop_replies (pid
);
2468 /* for_each_inferior callback for detach_or_kill_for_exit to print
2469 the pids of started inferiors. */
2472 print_started_pid (struct inferior_list_entry
*entry
)
2474 struct process_info
*process
= (struct process_info
*) entry
;
2476 if (! process
->attached
)
2478 int pid
= ptid_get_pid (process
->head
.id
);
2479 fprintf (stderr
, " %d", pid
);
2483 /* for_each_inferior callback for detach_or_kill_for_exit to print
2484 the pids of attached inferiors. */
2487 print_attached_pid (struct inferior_list_entry
*entry
)
2489 struct process_info
*process
= (struct process_info
*) entry
;
2491 if (process
->attached
)
2493 int pid
= ptid_get_pid (process
->head
.id
);
2494 fprintf (stderr
, " %d", pid
);
2498 /* Call this when exiting gdbserver with possible inferiors that need
2499 to be killed or detached from. */
2502 detach_or_kill_for_exit (void)
2504 /* First print a list of the inferiors we will be killing/detaching.
2505 This is to assist the user, for example, in case the inferior unexpectedly
2506 dies after we exit: did we screw up or did the inferior exit on its own?
2507 Having this info will save some head-scratching. */
2509 if (have_started_inferiors_p ())
2511 fprintf (stderr
, "Killing process(es):");
2512 for_each_inferior (&all_processes
, print_started_pid
);
2513 fprintf (stderr
, "\n");
2515 if (have_attached_inferiors_p ())
2517 fprintf (stderr
, "Detaching process(es):");
2518 for_each_inferior (&all_processes
, print_attached_pid
);
2519 fprintf (stderr
, "\n");
2522 /* Now we can kill or detach the inferiors. */
2524 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2528 main (int argc
, char *argv
[])
2532 char *arg_end
, *port
;
2533 char **next_arg
= &argv
[1];
2538 while (*next_arg
!= NULL
&& **next_arg
== '-')
2540 if (strcmp (*next_arg
, "--version") == 0)
2542 gdbserver_version ();
2545 else if (strcmp (*next_arg
, "--help") == 0)
2547 gdbserver_usage (stdout
);
2550 else if (strcmp (*next_arg
, "--attach") == 0)
2552 else if (strcmp (*next_arg
, "--multi") == 0)
2554 else if (strcmp (*next_arg
, "--wrapper") == 0)
2558 wrapper_argv
= next_arg
;
2559 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2562 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2564 gdbserver_usage (stderr
);
2568 /* Consume the "--". */
2571 else if (strcmp (*next_arg
, "--debug") == 0)
2573 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2575 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2577 gdbserver_show_disableable (stdout
);
2580 else if (strncmp (*next_arg
,
2581 "--disable-packet=",
2582 sizeof ("--disable-packet=") - 1) == 0)
2584 char *packets
, *tok
;
2586 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2587 for (tok
= strtok (packets
, ",");
2589 tok
= strtok (NULL
, ","))
2591 if (strcmp ("vCont", tok
) == 0)
2592 disable_packet_vCont
= 1;
2593 else if (strcmp ("Tthread", tok
) == 0)
2594 disable_packet_Tthread
= 1;
2595 else if (strcmp ("qC", tok
) == 0)
2596 disable_packet_qC
= 1;
2597 else if (strcmp ("qfThreadInfo", tok
) == 0)
2598 disable_packet_qfThreadInfo
= 1;
2599 else if (strcmp ("threads", tok
) == 0)
2601 disable_packet_vCont
= 1;
2602 disable_packet_Tthread
= 1;
2603 disable_packet_qC
= 1;
2604 disable_packet_qfThreadInfo
= 1;
2608 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2610 gdbserver_show_disableable (stderr
);
2615 else if (strcmp (*next_arg
, "-") == 0)
2617 /* "-" specifies a stdio connection and is a form of port
2619 *next_arg
= STDIO_CONNECTION_NAME
;
2622 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2623 disable_randomization
= 1;
2624 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2625 disable_randomization
= 0;
2626 else if (strcmp (*next_arg
, "--once") == 0)
2630 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2638 if (setjmp (toplevel
))
2640 fprintf (stderr
, "Exiting\n");
2646 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2648 gdbserver_usage (stderr
);
2652 /* We need to know whether the remote connection is stdio before
2653 starting the inferior. Inferiors created in this scenario have
2654 stdin,stdout redirected. So do this here before we call
2656 remote_prepare (port
);
2661 /* --attach used to come after PORT, so allow it there for
2663 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2670 && (*next_arg
== NULL
2671 || (*next_arg
)[0] == '\0'
2672 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2674 || next_arg
[1] != NULL
))
2679 gdbserver_usage (stderr
);
2683 initialize_async_io ();
2685 if (target_supports_tracepoints ())
2686 initialize_tracepoint ();
2688 own_buf
= xmalloc (PBUFSIZ
+ 1);
2689 mem_buf
= xmalloc (PBUFSIZ
);
2691 if (pid
== 0 && *next_arg
!= NULL
)
2695 n
= argc
- (next_arg
- argv
);
2696 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2697 for (i
= 0; i
< n
; i
++)
2698 program_argv
[i
] = xstrdup (next_arg
[i
]);
2699 program_argv
[i
] = NULL
;
2701 /* Wait till we are at first instruction in program. */
2702 start_inferior (program_argv
);
2704 /* We are now (hopefully) stopped at the first instruction of
2705 the target process. This assumes that the target process was
2706 successfully created. */
2710 if (attach_inferior (pid
) == -1)
2711 error ("Attaching not supported on this target");
2713 /* Otherwise succeeded. */
2717 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2718 last_status
.value
.integer
= 0;
2719 last_ptid
= minus_one_ptid
;
2722 /* Don't report shared library events on the initial connection,
2723 even if some libraries are preloaded. Avoids the "stopped by
2724 shared library event" notice on gdb side. */
2727 if (setjmp (toplevel
))
2729 /* If something fails and longjmps while detaching or killing
2730 inferiors, we'd end up here again, stuck in an infinite loop
2731 trap. Be sure that if that happens, we exit immediately
2733 if (setjmp (toplevel
) == 0)
2734 detach_or_kill_for_exit ();
2736 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2740 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2741 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2746 if (!was_running
&& !multi_mode
)
2748 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2756 /* Be sure we're out of tfind mode. */
2757 current_traceframe
= -1;
2761 if (setjmp (toplevel
) != 0)
2763 /* An error occurred. */
2764 if (response_needed
)
2766 write_enn (own_buf
);
2771 /* Wait for events. This will return when all event sources are
2772 removed from the event loop. */
2773 start_event_loop ();
2775 /* If an exit was requested (using the "monitor exit" command),
2776 terminate now. The only other way to get here is for
2777 getpkt to fail; close the connection and reopen it at the
2780 if (exit_requested
|| run_once
)
2782 /* If something fails and longjmps while detaching or
2783 killing inferiors, we'd end up here again, stuck in an
2784 infinite loop trap. Be sure that if that happens, we
2785 exit immediately instead. */
2786 if (setjmp (toplevel
) == 0)
2788 detach_or_kill_for_exit ();
2793 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2799 "Remote side has terminated connection. "
2800 "GDBserver will reopen the connection.\n");
2804 if (disconnected_tracing
)
2806 /* Try to enable non-stop/async mode, so we we can both
2807 wait for an async socket accept, and handle async
2808 target events simultaneously. There's also no point
2809 either in having the target always stop all threads,
2810 when we're going to pass signals down without
2814 if (start_non_stop (1))
2817 /* Detaching implicitly resumes all threads; simply
2818 disconnecting does not. */
2824 "Disconnected tracing disabled; stopping trace run.\n");
2831 /* Process options coming from Z packets for *point at address
2832 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2833 to point to the first char after the last processed option. */
2836 process_point_options (CORE_ADDR point_addr
, char **packet
)
2838 char *dataptr
= *packet
;
2840 /* Check if data has the correct format. */
2841 if (*dataptr
!= ';')
2851 /* Conditional expression. */
2852 fprintf (stderr
, "Found breakpoint condition.\n");
2853 add_breakpoint_condition (point_addr
, &dataptr
);
2856 /* Unrecognized token, just skip it. */
2857 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2861 /* Skip tokens until we find one that we recognize. */
2862 while (*dataptr
&& *dataptr
!= 'X' && *dataptr
!= ';')
2868 /* Event loop callback that handles a serial event. The first byte in
2869 the serial buffer gets us here. We expect characters to arrive at
2870 a brisk pace, so we read the rest of the packet with a blocking
2874 process_serial_event (void)
2885 int new_packet_len
= -1;
2887 /* Used to decide when gdbserver should exit in
2888 multi-mode/remote. */
2889 static int have_ran
= 0;
2892 have_ran
= target_running ();
2894 disable_async_io ();
2896 response_needed
= 0;
2897 packet_len
= getpkt (own_buf
);
2898 if (packet_len
<= 0)
2901 /* Force an event loop break. */
2904 response_needed
= 1;
2911 handle_query (own_buf
, packet_len
, &new_packet_len
);
2914 handle_general_set (own_buf
);
2917 require_running (own_buf
);
2922 pid
= strtol (&own_buf
[i
], NULL
, 16);
2926 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2928 if (tracing
&& disconnected_tracing
)
2930 struct thread_resume resume_info
;
2931 struct process_info
*process
= find_process_pid (pid
);
2933 if (process
== NULL
)
2935 write_enn (own_buf
);
2940 "Disconnected tracing in effect, "
2941 "leaving gdbserver attached to the process\n");
2943 /* Make sure we're in non-stop/async mode, so we we can both
2944 wait for an async socket accept, and handle async target
2945 events simultaneously. There's also no point either in
2946 having the target stop all threads, when we're going to
2947 pass signals down without informing GDB. */
2951 fprintf (stderr
, "Forcing non-stop mode\n");
2957 process
->gdb_detached
= 1;
2959 /* Detaching implicitly resumes all threads. */
2960 resume_info
.thread
= minus_one_ptid
;
2961 resume_info
.kind
= resume_continue
;
2962 resume_info
.sig
= 0;
2963 (*the_target
->resume
) (&resume_info
, 1);
2966 break; /* from switch/case */
2969 fprintf (stderr
, "Detaching from process %d\n", pid
);
2971 if (detach_inferior (pid
) != 0)
2972 write_enn (own_buf
);
2975 discard_queued_stop_replies (pid
);
2978 if (extended_protocol
)
2980 /* Treat this like a normal program exit. */
2981 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2982 last_status
.value
.integer
= 0;
2983 last_ptid
= pid_to_ptid (pid
);
2985 current_inferior
= NULL
;
2992 /* If we are attached, then we can exit. Otherwise, we
2993 need to hang around doing nothing, until the child is
2995 join_inferior (pid
);
3001 extended_protocol
= 1;
3005 handle_status (own_buf
);
3008 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3010 ptid_t gdb_id
, thread_id
;
3013 require_running (own_buf
);
3015 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3017 pid
= ptid_get_pid (gdb_id
);
3019 if (ptid_equal (gdb_id
, null_ptid
)
3020 || ptid_equal (gdb_id
, minus_one_ptid
))
3021 thread_id
= null_ptid
;
3023 && ptid_equal (pid_to_ptid (pid
),
3026 struct thread_info
*thread
=
3027 (struct thread_info
*) find_inferior (&all_threads
,
3032 write_enn (own_buf
);
3036 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3040 thread_id
= gdb_id_to_thread_id (gdb_id
);
3041 if (ptid_equal (thread_id
, null_ptid
))
3043 write_enn (own_buf
);
3048 if (own_buf
[1] == 'g')
3050 if (ptid_equal (thread_id
, null_ptid
))
3052 /* GDB is telling us to choose any thread. Check if
3053 the currently selected thread is still valid. If
3054 it is not, select the first available. */
3055 struct thread_info
*thread
=
3056 (struct thread_info
*) find_inferior_id (&all_threads
,
3059 thread_id
= all_threads
.head
->id
;
3062 general_thread
= thread_id
;
3063 set_desired_inferior (1);
3065 else if (own_buf
[1] == 'c')
3066 cont_thread
= thread_id
;
3072 /* Silently ignore it so that gdb can extend the protocol
3073 without compatibility headaches. */
3078 require_running (own_buf
);
3079 if (current_traceframe
>= 0)
3081 struct regcache
*regcache
= new_register_cache ();
3083 if (fetch_traceframe_registers (current_traceframe
,
3085 registers_to_string (regcache
, own_buf
);
3087 write_enn (own_buf
);
3088 free_register_cache (regcache
);
3092 struct regcache
*regcache
;
3094 set_desired_inferior (1);
3095 regcache
= get_thread_regcache (current_inferior
, 1);
3096 registers_to_string (regcache
, own_buf
);
3100 require_running (own_buf
);
3101 if (current_traceframe
>= 0)
3102 write_enn (own_buf
);
3105 struct regcache
*regcache
;
3107 set_desired_inferior (1);
3108 regcache
= get_thread_regcache (current_inferior
, 1);
3109 registers_from_string (regcache
, &own_buf
[1]);
3114 require_running (own_buf
);
3115 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3116 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3118 write_enn (own_buf
);
3120 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3123 require_running (own_buf
);
3124 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3125 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3128 write_enn (own_buf
);
3131 require_running (own_buf
);
3132 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3133 &mem_addr
, &len
, &mem_buf
) < 0
3134 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3135 write_enn (own_buf
);
3140 require_running (own_buf
);
3141 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3142 if (target_signal_to_host_p (sig
))
3143 signal
= target_signal_to_host (sig
);
3146 myresume (own_buf
, 0, signal
);
3149 require_running (own_buf
);
3150 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3151 if (target_signal_to_host_p (sig
))
3152 signal
= target_signal_to_host (sig
);
3155 myresume (own_buf
, 1, signal
);
3158 require_running (own_buf
);
3160 myresume (own_buf
, 0, signal
);
3163 require_running (own_buf
);
3165 myresume (own_buf
, 1, signal
);
3167 case 'Z': /* insert_ ... */
3169 case 'z': /* remove_ ... */
3173 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3174 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3175 char type
= own_buf
[1];
3177 const int insert
= ch
== 'Z';
3179 /* Default to unrecognized/unsupported. */
3183 case '0': /* software-breakpoint */
3184 case '1': /* hardware-breakpoint */
3185 case '2': /* write watchpoint */
3186 case '3': /* read watchpoint */
3187 case '4': /* access watchpoint */
3188 require_running (own_buf
);
3189 if (insert
&& the_target
->insert_point
!= NULL
)
3191 /* Insert the breakpoint. If it is already inserted, nothing
3193 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3195 /* GDB may have sent us a list of *point parameters to be
3196 evaluated on the target's side. Read such list here. If we
3197 already have a list of parameters, GDB is telling us to drop
3198 that list and use this one instead. */
3199 if (!res
&& (type
== '0' || type
== '1'))
3201 /* Remove previous conditions. */
3202 clear_gdb_breakpoint_conditions (addr
);
3203 process_point_options (addr
, &dataptr
);
3206 else if (!insert
&& the_target
->remove_point
!= NULL
)
3207 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3219 write_enn (own_buf
);
3223 response_needed
= 0;
3224 if (!target_running ())
3225 /* The packet we received doesn't make sense - but we can't
3226 reply to it, either. */
3229 fprintf (stderr
, "Killing all inferiors\n");
3230 for_each_inferior (&all_processes
, kill_inferior_callback
);
3232 /* When using the extended protocol, we wait with no program
3233 running. The traditional protocol will exit instead. */
3234 if (extended_protocol
)
3236 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3237 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3245 ptid_t gdb_id
, thread_id
;
3247 require_running (own_buf
);
3249 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3250 thread_id
= gdb_id_to_thread_id (gdb_id
);
3251 if (ptid_equal (thread_id
, null_ptid
))
3253 write_enn (own_buf
);
3257 if (mythread_alive (thread_id
))
3260 write_enn (own_buf
);
3264 response_needed
= 0;
3266 /* Restarting the inferior is only supported in the extended
3268 if (extended_protocol
)
3270 if (target_running ())
3271 for_each_inferior (&all_processes
,
3272 kill_inferior_callback
);
3273 fprintf (stderr
, "GDBserver restarting\n");
3275 /* Wait till we are at 1st instruction in prog. */
3276 if (program_argv
!= NULL
)
3277 start_inferior (program_argv
);
3280 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3281 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3287 /* It is a request we don't understand. Respond with an
3288 empty packet so that gdb knows that we don't support this
3294 /* Extended (long) request. */
3295 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3299 /* It is a request we don't understand. Respond with an empty
3300 packet so that gdb knows that we don't support this
3306 if (new_packet_len
!= -1)
3307 putpkt_binary (own_buf
, new_packet_len
);
3311 response_needed
= 0;
3313 if (!extended_protocol
&& have_ran
&& !target_running ())
3315 /* In non-stop, defer exiting until GDB had a chance to query
3316 the whole vStopped list (until it gets an OK). */
3319 fprintf (stderr
, "GDBserver exiting\n");
3331 /* Event-loop callback for serial events. */
3334 handle_serial_event (int err
, gdb_client_data client_data
)
3337 fprintf (stderr
, "handling possible serial event\n");
3339 /* Really handle it. */
3340 if (process_serial_event () < 0)
3343 /* Be sure to not change the selected inferior behind GDB's back.
3344 Important in the non-stop mode asynchronous protocol. */
3345 set_desired_inferior (1);
3350 /* Event-loop callback for target events. */
3353 handle_target_event (int err
, gdb_client_data client_data
)
3356 fprintf (stderr
, "handling possible target event\n");
3358 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3361 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3363 int pid
= ptid_get_pid (last_ptid
);
3364 struct process_info
*process
= find_process_pid (pid
);
3365 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3367 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3368 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3370 mark_breakpoints_out (process
);
3371 mourn_inferior (process
);
3375 /* We're reporting this thread as stopped. Update its
3376 "want-stopped" state to what the client wants, until it
3377 gets a new resume action. */
3378 current_inferior
->last_resume_kind
= resume_stop
;
3379 current_inferior
->last_status
= last_status
;
3384 if (!target_running ())
3386 /* The last process exited. We're done. */
3390 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3392 /* A thread stopped with a signal, but gdb isn't
3393 connected to handle it. Pass it down to the
3394 inferior, as if it wasn't being traced. */
3395 struct thread_resume resume_info
;
3399 "GDB not connected; forwarding event %d for [%s]\n",
3400 (int) last_status
.kind
,
3401 target_pid_to_str (last_ptid
));
3403 resume_info
.thread
= last_ptid
;
3404 resume_info
.kind
= resume_continue
;
3405 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3406 (*the_target
->resume
) (&resume_info
, 1);
3408 else if (debug_threads
)
3409 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3410 (int) last_status
.kind
,
3411 target_pid_to_str (last_ptid
));
3415 /* Something interesting. Tell GDB about it. */
3416 push_event (last_ptid
, &last_status
);
3420 /* Be sure to not change the selected inferior behind GDB's back.
3421 Important in the non-stop mode asynchronous protocol. */
3422 set_desired_inferior (1);