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. */
343 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
345 /* GDB knows to ignore the first SIGSTOP after attaching to a running
346 process using the "attach" command, but this is different; it's
347 just using "target remote". Pretend it's just starting up. */
348 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
349 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
350 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
352 current_inferior
->last_resume_kind
= resume_stop
;
353 current_inferior
->last_status
= last_status
;
359 extern int remote_debug
;
361 /* Decode a qXfer read request. Return 0 if everything looks OK,
365 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
367 /* After the read marker and annex, qXfer looks like a
368 traditional 'm' packet. */
369 decode_m_packet (buf
, ofs
, len
);
375 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
377 /* Extract and NUL-terminate the object. */
379 while (*buf
&& *buf
!= ':')
385 /* Extract and NUL-terminate the read/write action. */
387 while (*buf
&& *buf
!= ':')
393 /* Extract and NUL-terminate the annex. */
395 while (*buf
&& *buf
!= ':')
405 /* Write the response to a successful qXfer read. Returns the
406 length of the (binary) data stored in BUF, corresponding
407 to as much of DATA/LEN as we could fit. IS_MORE controls
408 the first character of the response. */
410 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
419 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
423 /* Handle all of the extended 'Q' packets. */
426 handle_general_set (char *own_buf
)
428 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
430 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
431 const char *p
= own_buf
+ strlen ("QPassSignals:");
434 p
= decode_address_to_semicolon (&cursig
, p
);
435 for (i
= 0; i
< numsigs
; i
++)
441 /* Keep looping, to clear the remaining signals. */
444 p
= decode_address_to_semicolon (&cursig
, p
);
449 strcpy (own_buf
, "OK");
453 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
457 fprintf (stderr
, "[noack mode enabled]\n");
466 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
468 char *mode
= own_buf
+ 9;
472 if (strcmp (mode
, "0") == 0)
474 else if (strcmp (mode
, "1") == 0)
478 /* We don't know what this mode is, so complain to
480 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
486 req_str
= req
? "non-stop" : "all-stop";
487 if (start_non_stop (req
) != 0)
489 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
497 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
503 if (strncmp ("QDisableRandomization:", own_buf
,
504 strlen ("QDisableRandomization:")) == 0)
506 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
509 unpack_varlen_hex (packet
, &setting
);
510 disable_randomization
= setting
;
514 if (disable_randomization
)
515 fprintf (stderr
, "[address space randomization disabled]\n");
517 fprintf (stderr
, "[address space randomization enabled]\n");
524 if (target_supports_tracepoints ()
525 && handle_tracepoint_general_set (own_buf
))
528 /* Otherwise we didn't know what packet it was. Say we didn't
534 get_features_xml (const char *annex
)
536 /* gdbserver_xmltarget defines what to return when looking
537 for the "target.xml" file. Its contents can either be
538 verbatim XML code (prefixed with a '@') or else the name
539 of the actual XML file to be used in place of "target.xml".
541 This variable is set up from the auto-generated
542 init_registers_... routine for the current target. */
544 if (gdbserver_xmltarget
545 && strcmp (annex
, "target.xml") == 0)
547 if (*gdbserver_xmltarget
== '@')
548 return gdbserver_xmltarget
+ 1;
550 annex
= gdbserver_xmltarget
;
555 extern const char *const xml_builtin
[][2];
558 /* Look for the annex. */
559 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
560 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
563 if (xml_builtin
[i
][0] != NULL
)
564 return xml_builtin
[i
][1];
572 monitor_show_help (void)
574 monitor_output ("The following monitor commands are supported:\n");
575 monitor_output (" set debug <0|1>\n");
576 monitor_output (" Enable general debugging messages\n");
577 monitor_output (" set debug-hw-points <0|1>\n");
578 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
579 monitor_output (" set remote-debug <0|1>\n");
580 monitor_output (" Enable remote protocol debugging messages\n");
581 monitor_output (" exit\n");
582 monitor_output (" Quit GDBserver\n");
585 /* Read trace frame or inferior memory. Returns the number of bytes
586 actually read, zero when no further transfer is possible, and -1 on
587 error. Return of a positive value smaller than LEN does not
588 indicate there's no more to be read, only the end of the transfer.
589 E.g., when GDB reads memory from a traceframe, a first request may
590 be served from a memory block that does not cover the whole request
591 length. A following request gets the rest served from either
592 another block (of the same traceframe) or from the read-only
596 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
600 if (current_traceframe
>= 0)
603 ULONGEST length
= len
;
605 if (traceframe_read_mem (current_traceframe
,
606 memaddr
, myaddr
, len
, &nbytes
))
608 /* Data read from trace buffer, we're done. */
611 if (!in_readonly_region (memaddr
, length
))
613 /* Otherwise we have a valid readonly case, fall through. */
614 /* (assume no half-trace half-real blocks for now) */
617 res
= prepare_to_access_memory ();
620 res
= read_inferior_memory (memaddr
, myaddr
, len
);
621 done_accessing_memory ();
623 return res
== 0 ? len
: -1;
629 /* Write trace frame or inferior memory. Actually, writing to trace
630 frames is forbidden. */
633 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
635 if (current_traceframe
>= 0)
641 ret
= prepare_to_access_memory ();
644 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
645 done_accessing_memory ();
651 /* Subroutine of handle_search_memory to simplify it. */
654 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
655 gdb_byte
*pattern
, unsigned pattern_len
,
656 gdb_byte
*search_buf
,
657 unsigned chunk_size
, unsigned search_buf_size
,
658 CORE_ADDR
*found_addrp
)
660 /* Prime the search buffer. */
662 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
665 warning ("Unable to access target memory at 0x%lx, halting search.",
670 /* Perform the search.
672 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
673 When we've scanned N bytes we copy the trailing bytes to the start and
674 read in another N bytes. */
676 while (search_space_len
>= pattern_len
)
679 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
683 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
685 if (found_ptr
!= NULL
)
687 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
688 *found_addrp
= found_addr
;
692 /* Not found in this chunk, skip to next chunk. */
694 /* Don't let search_space_len wrap here, it's unsigned. */
695 if (search_space_len
>= chunk_size
)
696 search_space_len
-= chunk_size
;
698 search_space_len
= 0;
700 if (search_space_len
>= pattern_len
)
702 unsigned keep_len
= search_buf_size
- chunk_size
;
703 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
706 /* Copy the trailing part of the previous iteration to the front
707 of the buffer for the next iteration. */
708 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
710 nr_to_read
= (search_space_len
- keep_len
< chunk_size
711 ? search_space_len
- keep_len
714 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
715 nr_to_read
) != search_buf_size
)
717 warning ("Unable to access target memory "
718 "at 0x%lx, halting search.",
723 start_addr
+= chunk_size
;
732 /* Handle qSearch:memory packets. */
735 handle_search_memory (char *own_buf
, int packet_len
)
737 CORE_ADDR start_addr
;
738 CORE_ADDR search_space_len
;
740 unsigned int pattern_len
;
741 /* NOTE: also defined in find.c testcase. */
742 #define SEARCH_CHUNK_SIZE 16000
743 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
744 /* Buffer to hold memory contents for searching. */
745 gdb_byte
*search_buf
;
746 unsigned search_buf_size
;
748 CORE_ADDR found_addr
;
749 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
751 pattern
= malloc (packet_len
);
754 error ("Unable to allocate memory to perform the search");
755 strcpy (own_buf
, "E00");
758 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
759 packet_len
- cmd_name_len
,
760 &start_addr
, &search_space_len
,
761 pattern
, &pattern_len
) < 0)
764 error ("Error in parsing qSearch:memory packet");
765 strcpy (own_buf
, "E00");
769 search_buf_size
= chunk_size
+ pattern_len
- 1;
771 /* No point in trying to allocate a buffer larger than the search space. */
772 if (search_space_len
< search_buf_size
)
773 search_buf_size
= search_space_len
;
775 search_buf
= malloc (search_buf_size
);
776 if (search_buf
== NULL
)
779 error ("Unable to allocate memory to perform the search");
780 strcpy (own_buf
, "E00");
784 found
= handle_search_memory_1 (start_addr
, search_space_len
,
785 pattern
, pattern_len
,
786 search_buf
, chunk_size
, search_buf_size
,
790 sprintf (own_buf
, "1,%lx", (long) found_addr
);
792 strcpy (own_buf
, "0");
794 strcpy (own_buf
, "E00");
800 #define require_running(BUF) \
801 if (!target_running ()) \
807 /* Handle monitor commands not handled by target-specific handlers. */
810 handle_monitor_command (char *mon
)
812 if (strcmp (mon
, "set debug 1") == 0)
815 monitor_output ("Debug output enabled.\n");
817 else if (strcmp (mon
, "set debug 0") == 0)
820 monitor_output ("Debug output disabled.\n");
822 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
825 monitor_output ("H/W point debugging output enabled.\n");
827 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
830 monitor_output ("H/W point debugging output disabled.\n");
832 else if (strcmp (mon
, "set remote-debug 1") == 0)
835 monitor_output ("Protocol debug output enabled.\n");
837 else if (strcmp (mon
, "set remote-debug 0") == 0)
840 monitor_output ("Protocol debug output disabled.\n");
842 else if (strcmp (mon
, "help") == 0)
843 monitor_show_help ();
844 else if (strcmp (mon
, "exit") == 0)
848 monitor_output ("Unknown monitor command.\n\n");
849 monitor_show_help ();
854 /* Associates a callback with each supported qXfer'able object. */
858 /* The object this handler handles. */
861 /* Request that the target transfer up to LEN 8-bit bytes of the
862 target's OBJECT. The OFFSET, for a seekable object, specifies
863 the starting point. The ANNEX can be used to provide additional
864 data-specific information to the target.
866 Return the number of bytes actually transfered, zero when no
867 further transfer is possible, -1 on error, and -2 when the
868 transfer is not supported. Return of a positive value smaller
869 than LEN does not indicate the end of the object, only the end of
872 One, and only one, of readbuf or writebuf must be non-NULL. */
873 int (*xfer
) (const char *annex
,
874 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
875 ULONGEST offset
, LONGEST len
);
878 /* Handle qXfer:auxv:read. */
881 handle_qxfer_auxv (const char *annex
,
882 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
883 ULONGEST offset
, LONGEST len
)
885 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
888 if (annex
[0] != '\0' || !target_running ())
891 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
894 /* Handle qXfer:features:read. */
897 handle_qxfer_features (const char *annex
,
898 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
899 ULONGEST offset
, LONGEST len
)
901 const char *document
;
904 if (writebuf
!= NULL
)
907 if (!target_running ())
910 /* Grab the correct annex. */
911 document
= get_features_xml (annex
);
912 if (document
== NULL
)
915 total_len
= strlen (document
);
917 if (offset
> total_len
)
920 if (offset
+ len
> total_len
)
921 len
= total_len
- offset
;
923 memcpy (readbuf
, document
+ offset
, len
);
927 /* Handle qXfer:libraries:read. */
930 handle_qxfer_libraries (const char *annex
,
931 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
932 ULONGEST offset
, LONGEST len
)
934 unsigned int total_len
;
936 struct inferior_list_entry
*dll_ptr
;
938 if (writebuf
!= NULL
)
941 if (annex
[0] != '\0' || !target_running ())
944 /* Do not confuse this packet with qXfer:libraries-svr4:read. */
945 if (the_target
->qxfer_libraries_svr4
!= NULL
)
948 /* Over-estimate the necessary memory. Assume that every character
949 in the library name must be escaped. */
951 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
952 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
954 document
= malloc (total_len
);
955 if (document
== NULL
)
958 strcpy (document
, "<library-list>\n");
959 p
= document
+ strlen (document
);
961 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
963 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
966 strcpy (p
, " <library name=\"");
968 name
= xml_escape_text (dll
->name
);
972 strcpy (p
, "\"><segment address=\"");
974 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
976 strcpy (p
, "\"/></library>\n");
980 strcpy (p
, "</library-list>\n");
982 total_len
= strlen (document
);
984 if (offset
> total_len
)
990 if (offset
+ len
> total_len
)
991 len
= total_len
- offset
;
993 memcpy (readbuf
, document
+ offset
, len
);
998 /* Handle qXfer:libraries-svr4:read. */
1001 handle_qxfer_libraries_svr4 (const char *annex
,
1002 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1003 ULONGEST offset
, LONGEST len
)
1005 if (writebuf
!= NULL
)
1008 if (annex
[0] != '\0' || !target_running ()
1009 || the_target
->qxfer_libraries_svr4
== NULL
)
1012 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1015 /* Handle qXfer:osadata:read. */
1018 handle_qxfer_osdata (const char *annex
,
1019 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1020 ULONGEST offset
, LONGEST len
)
1022 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1025 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1028 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1031 handle_qxfer_siginfo (const char *annex
,
1032 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1033 ULONGEST offset
, LONGEST len
)
1035 if (the_target
->qxfer_siginfo
== NULL
)
1038 if (annex
[0] != '\0' || !target_running ())
1041 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1044 /* Handle qXfer:spu:read and qXfer:spu:write. */
1047 handle_qxfer_spu (const char *annex
,
1048 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1049 ULONGEST offset
, LONGEST len
)
1051 if (the_target
->qxfer_spu
== NULL
)
1054 if (!target_running ())
1057 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1060 /* Handle qXfer:statictrace:read. */
1063 handle_qxfer_statictrace (const char *annex
,
1064 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1065 ULONGEST offset
, LONGEST len
)
1069 if (writebuf
!= NULL
)
1072 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1075 if (traceframe_read_sdata (current_traceframe
, offset
,
1076 readbuf
, len
, &nbytes
))
1081 /* Helper for handle_qxfer_threads. */
1084 handle_qxfer_threads_proper (struct buffer
*buffer
)
1086 struct inferior_list_entry
*thread
;
1088 buffer_grow_str (buffer
, "<threads>\n");
1090 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1092 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1097 write_ptid (ptid_s
, ptid
);
1099 if (the_target
->core_of_thread
)
1100 core
= (*the_target
->core_of_thread
) (ptid
);
1104 sprintf (core_s
, "%d", core
);
1105 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1110 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1115 buffer_grow_str0 (buffer
, "</threads>\n");
1118 /* Handle qXfer:threads:read. */
1121 handle_qxfer_threads (const char *annex
,
1122 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1123 ULONGEST offset
, LONGEST len
)
1125 static char *result
= 0;
1126 static unsigned int result_length
= 0;
1128 if (writebuf
!= NULL
)
1131 if (!target_running () || annex
[0] != '\0')
1136 struct buffer buffer
;
1137 /* When asked for data at offset 0, generate everything and store into
1138 'result'. Successive reads will be served off 'result'. */
1142 buffer_init (&buffer
);
1144 handle_qxfer_threads_proper (&buffer
);
1146 result
= buffer_finish (&buffer
);
1147 result_length
= strlen (result
);
1148 buffer_free (&buffer
);
1151 if (offset
>= result_length
)
1153 /* We're out of data. */
1160 if (len
> result_length
- offset
)
1161 len
= result_length
- offset
;
1163 memcpy (readbuf
, result
+ offset
, len
);
1168 /* Handle qXfer:traceframe-info:read. */
1171 handle_qxfer_traceframe_info (const char *annex
,
1172 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1173 ULONGEST offset
, LONGEST len
)
1175 static char *result
= 0;
1176 static unsigned int result_length
= 0;
1178 if (writebuf
!= NULL
)
1181 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1186 struct buffer buffer
;
1188 /* When asked for data at offset 0, generate everything and
1189 store into 'result'. Successive reads will be served off
1193 buffer_init (&buffer
);
1195 traceframe_read_info (current_traceframe
, &buffer
);
1197 result
= buffer_finish (&buffer
);
1198 result_length
= strlen (result
);
1199 buffer_free (&buffer
);
1202 if (offset
>= result_length
)
1204 /* We're out of data. */
1211 if (len
> result_length
- offset
)
1212 len
= result_length
- offset
;
1214 memcpy (readbuf
, result
+ offset
, len
);
1218 /* Handle qXfer:fdpic:read. */
1221 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1222 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1224 if (the_target
->read_loadmap
== NULL
)
1227 if (!target_running ())
1230 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1233 static const struct qxfer qxfer_packets
[] =
1235 { "auxv", handle_qxfer_auxv
},
1236 { "fdpic", handle_qxfer_fdpic
},
1237 { "features", handle_qxfer_features
},
1238 { "libraries", handle_qxfer_libraries
},
1239 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1240 { "osdata", handle_qxfer_osdata
},
1241 { "siginfo", handle_qxfer_siginfo
},
1242 { "spu", handle_qxfer_spu
},
1243 { "statictrace", handle_qxfer_statictrace
},
1244 { "threads", handle_qxfer_threads
},
1245 { "traceframe-info", handle_qxfer_traceframe_info
},
1249 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1257 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1260 /* Grab the object, r/w and annex. */
1261 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1263 write_enn (own_buf
);
1268 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1271 const struct qxfer
*q
= &qxfer_packets
[i
];
1273 if (strcmp (object
, q
->object
) == 0)
1275 if (strcmp (rw
, "read") == 0)
1277 unsigned char *data
;
1282 /* Grab the offset and length. */
1283 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1285 write_enn (own_buf
);
1289 /* Read one extra byte, as an indicator of whether there is
1291 if (len
> PBUFSIZ
- 2)
1293 data
= malloc (len
+ 1);
1296 write_enn (own_buf
);
1299 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1306 write_enn (own_buf
);
1308 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1310 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1315 else if (strcmp (rw
, "write") == 0)
1320 unsigned char *data
;
1322 strcpy (own_buf
, "E00");
1323 data
= malloc (packet_len
- (offset
- own_buf
));
1326 write_enn (own_buf
);
1329 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1330 &ofs
, &len
, data
) < 0)
1333 write_enn (own_buf
);
1337 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1344 write_enn (own_buf
);
1346 sprintf (own_buf
, "%x", n
);
1359 /* Table used by the crc32 function to calcuate the checksum. */
1361 static unsigned int crc32_table
[256] =
1364 /* Compute 32 bit CRC from inferior memory.
1366 On success, return 32 bit CRC.
1367 On failure, return (unsigned long long) -1. */
1369 static unsigned long long
1370 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1372 if (!crc32_table
[1])
1374 /* Initialize the CRC table and the decoding table. */
1378 for (i
= 0; i
< 256; i
++)
1380 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1381 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1388 unsigned char byte
= 0;
1390 /* Return failure if memory read fails. */
1391 if (read_inferior_memory (base
, &byte
, 1) != 0)
1392 return (unsigned long long) -1;
1394 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1397 return (unsigned long long) crc
;
1400 /* Handle all of the extended 'q' packets. */
1403 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1405 static struct inferior_list_entry
*thread_ptr
;
1407 /* Reply the current thread id. */
1408 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1411 require_running (own_buf
);
1413 if (!ptid_equal (general_thread
, null_ptid
)
1414 && !ptid_equal (general_thread
, minus_one_ptid
))
1415 gdb_id
= general_thread
;
1418 thread_ptr
= all_threads
.head
;
1419 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1422 sprintf (own_buf
, "QC");
1424 write_ptid (own_buf
, gdb_id
);
1428 if (strcmp ("qSymbol::", own_buf
) == 0)
1430 /* GDB is suggesting new symbols have been loaded. This may
1431 mean a new shared library has been detected as loaded, so
1432 take the opportunity to check if breakpoints we think are
1433 inserted, still are. Note that it isn't guaranteed that
1434 we'll see this when a shared library is loaded, and nor will
1435 we see this for unloads (although breakpoints in unloaded
1436 libraries shouldn't trigger), as GDB may not find symbols for
1437 the library at all. We also re-validate breakpoints when we
1438 see a second GDB breakpoint for the same address, and or when
1439 we access breakpoint shadows. */
1440 validate_breakpoints ();
1442 if (target_supports_tracepoints ())
1443 tracepoint_look_up_symbols ();
1445 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1446 (*the_target
->look_up_symbols
) ();
1448 strcpy (own_buf
, "OK");
1452 if (!disable_packet_qfThreadInfo
)
1454 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1458 require_running (own_buf
);
1459 thread_ptr
= all_threads
.head
;
1462 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1463 write_ptid (own_buf
, gdb_id
);
1464 thread_ptr
= thread_ptr
->next
;
1468 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1472 require_running (own_buf
);
1473 if (thread_ptr
!= NULL
)
1476 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1477 write_ptid (own_buf
, gdb_id
);
1478 thread_ptr
= thread_ptr
->next
;
1483 sprintf (own_buf
, "l");
1489 if (the_target
->read_offsets
!= NULL
1490 && strcmp ("qOffsets", own_buf
) == 0)
1492 CORE_ADDR text
, data
;
1494 require_running (own_buf
);
1495 if (the_target
->read_offsets (&text
, &data
))
1496 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1497 (long)text
, (long)data
, (long)data
);
1499 write_enn (own_buf
);
1504 /* Protocol features query. */
1505 if (strncmp ("qSupported", own_buf
, 10) == 0
1506 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1508 char *p
= &own_buf
[10];
1509 int gdb_supports_qRelocInsn
= 0;
1511 /* Start processing qSupported packet. */
1512 target_process_qsupported (NULL
);
1514 /* Process each feature being provided by GDB. The first
1515 feature will follow a ':', and latter features will follow
1519 char **qsupported
= NULL
;
1523 /* Two passes, to avoid nested strtok calls in
1524 target_process_qsupported. */
1525 for (p
= strtok (p
+ 1, ";");
1527 p
= strtok (NULL
, ";"))
1530 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1531 qsupported
[count
- 1] = xstrdup (p
);
1534 for (i
= 0; i
< count
; i
++)
1537 if (strcmp (p
, "multiprocess+") == 0)
1539 /* GDB supports and wants multi-process support if
1541 if (target_supports_multi_process ())
1544 else if (strcmp (p
, "qRelocInsn+") == 0)
1546 /* GDB supports relocate instruction requests. */
1547 gdb_supports_qRelocInsn
= 1;
1550 target_process_qsupported (p
);
1558 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1560 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1561 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1564 /* We do not have any hook to indicate whether the non-SVR4 target
1565 backend supports qXfer:libraries:read, so always report it. */
1566 strcat (own_buf
, ";qXfer:libraries:read+");
1569 if (the_target
->read_auxv
!= NULL
)
1570 strcat (own_buf
, ";qXfer:auxv:read+");
1572 if (the_target
->qxfer_spu
!= NULL
)
1573 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1575 if (the_target
->qxfer_siginfo
!= NULL
)
1576 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1578 if (the_target
->read_loadmap
!= NULL
)
1579 strcat (own_buf
, ";qXfer:fdpic:read+");
1581 /* We always report qXfer:features:read, as targets may
1582 install XML files on a subsequent call to arch_setup.
1583 If we reported to GDB on startup that we don't support
1584 qXfer:feature:read at all, we will never be re-queried. */
1585 strcat (own_buf
, ";qXfer:features:read+");
1587 if (transport_is_reliable
)
1588 strcat (own_buf
, ";QStartNoAckMode+");
1590 if (the_target
->qxfer_osdata
!= NULL
)
1591 strcat (own_buf
, ";qXfer:osdata:read+");
1593 if (target_supports_multi_process ())
1594 strcat (own_buf
, ";multiprocess+");
1596 if (target_supports_non_stop ())
1597 strcat (own_buf
, ";QNonStop+");
1599 if (target_supports_disable_randomization ())
1600 strcat (own_buf
, ";QDisableRandomization+");
1602 strcat (own_buf
, ";qXfer:threads:read+");
1604 if (target_supports_tracepoints ())
1606 strcat (own_buf
, ";ConditionalTracepoints+");
1607 strcat (own_buf
, ";TraceStateVariables+");
1608 strcat (own_buf
, ";TracepointSource+");
1609 strcat (own_buf
, ";DisconnectedTracing+");
1610 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1611 strcat (own_buf
, ";FastTracepoints+");
1612 strcat (own_buf
, ";StaticTracepoints+");
1613 strcat (own_buf
, ";InstallInTrace+");
1614 strcat (own_buf
, ";qXfer:statictrace:read+");
1615 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1616 strcat (own_buf
, ";EnableDisableTracepoints+");
1617 strcat (own_buf
, ";tracenz+");
1623 /* Thread-local storage support. */
1624 if (the_target
->get_tls_address
!= NULL
1625 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1627 char *p
= own_buf
+ 12;
1628 CORE_ADDR parts
[2], address
= 0;
1630 ptid_t ptid
= null_ptid
;
1632 require_running (own_buf
);
1634 for (i
= 0; i
< 3; i
++)
1642 p2
= strchr (p
, ',');
1655 ptid
= read_ptid (p
, NULL
);
1657 decode_address (&parts
[i
- 1], p
, len
);
1661 if (p
!= NULL
|| i
< 3)
1665 struct thread_info
*thread
= find_thread_ptid (ptid
);
1670 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1676 strcpy (own_buf
, paddress(address
));
1681 write_enn (own_buf
);
1685 /* Otherwise, pretend we do not understand this packet. */
1688 /* Windows OS Thread Information Block address support. */
1689 if (the_target
->get_tib_address
!= NULL
1690 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1695 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1697 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1700 strcpy (own_buf
, paddress(tlb
));
1705 write_enn (own_buf
);
1711 /* Handle "monitor" commands. */
1712 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1714 char *mon
= malloc (PBUFSIZ
);
1715 int len
= strlen (own_buf
+ 6);
1719 write_enn (own_buf
);
1723 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1725 write_enn (own_buf
);
1729 mon
[len
/ 2] = '\0';
1733 if (the_target
->handle_monitor_command
== NULL
1734 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1735 /* Default processing. */
1736 handle_monitor_command (mon
);
1742 if (strncmp ("qSearch:memory:", own_buf
,
1743 sizeof ("qSearch:memory:") - 1) == 0)
1745 require_running (own_buf
);
1746 handle_search_memory (own_buf
, packet_len
);
1750 if (strcmp (own_buf
, "qAttached") == 0
1751 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1753 struct process_info
*process
;
1755 if (own_buf
[sizeof ("qAttached") - 1])
1757 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1758 process
= (struct process_info
*)
1759 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1763 require_running (own_buf
);
1764 process
= current_process ();
1767 if (process
== NULL
)
1769 write_enn (own_buf
);
1773 strcpy (own_buf
, process
->attached
? "1" : "0");
1777 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1779 /* CRC check (compare-section). */
1783 unsigned long long crc
;
1785 require_running (own_buf
);
1786 base
= strtoul (own_buf
+ 5, &comma
, 16);
1787 if (*comma
++ != ',')
1789 write_enn (own_buf
);
1792 len
= strtoul (comma
, NULL
, 16);
1793 crc
= crc32 (base
, len
, 0xffffffff);
1794 /* Check for memory failure. */
1795 if (crc
== (unsigned long long) -1)
1797 write_enn (own_buf
);
1800 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1804 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1807 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1810 /* Otherwise we didn't know what packet it was. Say we didn't
1815 static void gdb_wants_all_threads_stopped (void);
1817 /* Parse vCont packets. */
1819 handle_v_cont (char *own_buf
)
1823 struct thread_resume
*resume_info
;
1824 struct thread_resume default_action
= {{0}};
1826 /* Count the number of semicolons in the packet. There should be one
1827 for every action. */
1833 p
= strchr (p
, ';');
1836 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1837 if (resume_info
== NULL
)
1845 if (p
[0] == 's' || p
[0] == 'S')
1846 resume_info
[i
].kind
= resume_step
;
1847 else if (p
[0] == 'c' || p
[0] == 'C')
1848 resume_info
[i
].kind
= resume_continue
;
1849 else if (p
[0] == 't')
1850 resume_info
[i
].kind
= resume_stop
;
1854 if (p
[0] == 'S' || p
[0] == 'C')
1857 sig
= strtol (p
+ 1, &q
, 16);
1862 if (!target_signal_to_host_p (sig
))
1864 resume_info
[i
].sig
= target_signal_to_host (sig
);
1868 resume_info
[i
].sig
= 0;
1874 resume_info
[i
].thread
= minus_one_ptid
;
1875 default_action
= resume_info
[i
];
1877 /* Note: we don't increment i here, we'll overwrite this entry
1878 the next time through. */
1880 else if (p
[0] == ':')
1882 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1887 if (p
[0] != ';' && p
[0] != 0)
1890 resume_info
[i
].thread
= ptid
;
1897 resume_info
[i
] = default_action
;
1899 /* Still used in occasional places in the backend. */
1901 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1902 && resume_info
[0].kind
!= resume_stop
)
1903 cont_thread
= resume_info
[0].thread
;
1905 cont_thread
= minus_one_ptid
;
1906 set_desired_inferior (0);
1911 (*the_target
->resume
) (resume_info
, n
);
1919 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1921 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1922 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1923 current_inferior
->last_status
= last_status
;
1925 /* From the client's perspective, all-stop mode always stops all
1926 threads implicitly (and the target backend has already done
1927 so by now). Tag all threads as "want-stopped", so we don't
1928 resume them implicitly without the client telling us to. */
1929 gdb_wants_all_threads_stopped ();
1930 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1931 disable_async_io ();
1933 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1934 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1935 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1940 write_enn (own_buf
);
1945 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1947 handle_v_attach (char *own_buf
)
1951 pid
= strtol (own_buf
+ 8, NULL
, 16);
1952 if (pid
!= 0 && attach_inferior (pid
) == 0)
1954 /* Don't report shared library events after attaching, even if
1955 some libraries are preloaded. GDB will always poll the
1956 library list. Avoids the "stopped by shared library event"
1957 notice on the GDB side. */
1962 /* In non-stop, we don't send a resume reply. Stop events
1963 will follow up using the normal notification
1968 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1974 write_enn (own_buf
);
1979 /* Run a new program. Return 1 if successful, 0 if failure. */
1981 handle_v_run (char *own_buf
)
1983 char *p
, *next_p
, **new_argv
;
1987 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1993 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1994 if (new_argv
== NULL
)
1996 write_enn (own_buf
);
2001 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2003 next_p
= strchr (p
, ';');
2005 next_p
= p
+ strlen (p
);
2007 if (i
== 0 && p
== next_p
)
2011 /* FIXME: Fail request if out of memory instead of dying. */
2012 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2013 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2014 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2023 if (new_argv
[0] == NULL
)
2025 /* GDB didn't specify a program to run. Use the program from the
2026 last run with the new argument list. */
2028 if (program_argv
== NULL
)
2030 write_enn (own_buf
);
2031 freeargv (new_argv
);
2035 new_argv
[0] = strdup (program_argv
[0]);
2036 if (new_argv
[0] == NULL
)
2038 write_enn (own_buf
);
2039 freeargv (new_argv
);
2044 /* Free the old argv and install the new one. */
2045 freeargv (program_argv
);
2046 program_argv
= new_argv
;
2048 start_inferior (program_argv
);
2049 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2051 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2053 /* In non-stop, sending a resume reply doesn't set the general
2054 thread, but GDB assumes a vRun sets it (this is so GDB can
2055 query which is the main thread of the new inferior. */
2057 general_thread
= last_ptid
;
2063 write_enn (own_buf
);
2068 /* Kill process. Return 1 if successful, 0 if failure. */
2070 handle_v_kill (char *own_buf
)
2073 char *p
= &own_buf
[6];
2075 pid
= strtol (p
, NULL
, 16);
2078 if (pid
!= 0 && kill_inferior (pid
) == 0)
2080 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2081 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2082 last_ptid
= pid_to_ptid (pid
);
2083 discard_queued_stop_replies (pid
);
2089 write_enn (own_buf
);
2094 /* Handle a 'vStopped' packet. */
2096 handle_v_stopped (char *own_buf
)
2098 /* If we're waiting for GDB to acknowledge a pending stop reply,
2099 consider that done. */
2102 struct vstop_notif
*head
;
2105 fprintf (stderr
, "vStopped: acking %s\n",
2106 target_pid_to_str (notif_queue
->ptid
));
2109 notif_queue
= notif_queue
->next
;
2113 /* Push another stop reply, or if there are no more left, an OK. */
2114 send_next_stop_reply (own_buf
);
2117 /* Handle all of the extended 'v' packets. */
2119 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2121 if (!disable_packet_vCont
)
2123 if (strncmp (own_buf
, "vCont;", 6) == 0)
2125 require_running (own_buf
);
2126 handle_v_cont (own_buf
);
2130 if (strncmp (own_buf
, "vCont?", 6) == 0)
2132 strcpy (own_buf
, "vCont;c;C;s;S;t");
2137 if (strncmp (own_buf
, "vFile:", 6) == 0
2138 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2141 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2143 if (!multi_process
&& target_running ())
2145 fprintf (stderr
, "Already debugging a process\n");
2146 write_enn (own_buf
);
2149 handle_v_attach (own_buf
);
2153 if (strncmp (own_buf
, "vRun;", 5) == 0)
2155 if (!multi_process
&& target_running ())
2157 fprintf (stderr
, "Already debugging a process\n");
2158 write_enn (own_buf
);
2161 handle_v_run (own_buf
);
2165 if (strncmp (own_buf
, "vKill;", 6) == 0)
2167 if (!target_running ())
2169 fprintf (stderr
, "No process to kill\n");
2170 write_enn (own_buf
);
2173 handle_v_kill (own_buf
);
2177 if (strncmp (own_buf
, "vStopped", 8) == 0)
2179 handle_v_stopped (own_buf
);
2183 /* Otherwise we didn't know what packet it was. Say we didn't
2189 /* Resume inferior and wait for another event. In non-stop mode,
2190 don't really wait here, but return immediatelly to the event
2193 myresume (char *own_buf
, int step
, int sig
)
2195 struct thread_resume resume_info
[2];
2197 int valid_cont_thread
;
2199 set_desired_inferior (0);
2201 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2202 && !ptid_equal (cont_thread
, minus_one_ptid
));
2204 if (step
|| sig
|| valid_cont_thread
)
2206 resume_info
[0].thread
2207 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2209 resume_info
[0].kind
= resume_step
;
2211 resume_info
[0].kind
= resume_continue
;
2212 resume_info
[0].sig
= sig
;
2216 if (!valid_cont_thread
)
2218 resume_info
[n
].thread
= minus_one_ptid
;
2219 resume_info
[n
].kind
= resume_continue
;
2220 resume_info
[n
].sig
= 0;
2227 (*the_target
->resume
) (resume_info
, n
);
2233 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2235 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2236 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2238 current_inferior
->last_resume_kind
= resume_stop
;
2239 current_inferior
->last_status
= last_status
;
2242 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2243 disable_async_io ();
2245 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2246 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2247 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2251 /* Callback for for_each_inferior. Make a new stop reply for each
2255 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2257 struct thread_info
*thread
= (struct thread_info
*) entry
;
2259 /* For now, assume targets that don't have this callback also don't
2260 manage the thread's last_status field. */
2261 if (the_target
->thread_stopped
== NULL
)
2263 /* Pass the last stop reply back to GDB, but don't notify
2265 queue_stop_reply (entry
->id
, &thread
->last_status
);
2269 if (thread_stopped (thread
))
2273 "Reporting thread %s as already stopped with %s\n",
2274 target_pid_to_str (entry
->id
),
2275 target_waitstatus_to_string (&thread
->last_status
));
2277 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2279 /* Pass the last stop reply back to GDB, but don't notify
2281 queue_stop_reply (entry
->id
, &thread
->last_status
);
2288 /* Set this inferior threads's state as "want-stopped". We won't
2289 resume this thread until the client gives us another action for
2293 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2295 struct thread_info
*thread
= (struct thread_info
*) entry
;
2297 thread
->last_resume_kind
= resume_stop
;
2299 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2301 /* Most threads are stopped implicitly (all-stop); tag that with
2303 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2304 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2308 /* Set all threads' states as "want-stopped". */
2311 gdb_wants_all_threads_stopped (void)
2313 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2316 /* Clear the gdb_detached flag of every process. */
2319 gdb_reattached_process (struct inferior_list_entry
*entry
)
2321 struct process_info
*process
= (struct process_info
*) entry
;
2323 process
->gdb_detached
= 0;
2326 /* Status handler for the '?' packet. */
2329 handle_status (char *own_buf
)
2331 /* GDB is connected, don't forward events to the target anymore. */
2332 for_each_inferior (&all_processes
, gdb_reattached_process
);
2334 /* In non-stop mode, we must send a stop reply for each stopped
2335 thread. In all-stop mode, just send one for the first stopped
2340 discard_queued_stop_replies (-1);
2341 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2343 /* The first is sent immediatly. OK is sent if there is no
2344 stopped thread, which is the same handling of the vStopped
2345 packet (by design). */
2346 send_next_stop_reply (own_buf
);
2351 stabilize_threads ();
2352 gdb_wants_all_threads_stopped ();
2354 if (all_threads
.head
)
2356 struct target_waitstatus status
;
2358 status
.kind
= TARGET_WAITKIND_STOPPED
;
2359 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2360 prepare_resume_reply (own_buf
,
2361 all_threads
.head
->id
, &status
);
2364 strcpy (own_buf
, "W00");
2369 gdbserver_version (void)
2371 printf ("GNU gdbserver %s%s\n"
2372 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2373 "gdbserver is free software, covered by the "
2374 "GNU General Public License.\n"
2375 "This gdbserver was configured as \"%s\"\n",
2376 PKGVERSION
, version
, host_name
);
2380 gdbserver_usage (FILE *stream
)
2382 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2383 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2384 "\tgdbserver [OPTIONS] --multi COMM\n"
2386 "COMM may either be a tty device (for serial debugging), or \n"
2387 "HOST:PORT to listen for a TCP connection.\n"
2390 " --debug Enable general debugging output.\n"
2391 " --remote-debug Enable remote protocol debugging output.\n"
2392 " --version Display version information and exit.\n"
2393 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2394 " --once Exit after the first connection has "
2396 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2397 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2401 gdbserver_show_disableable (FILE *stream
)
2403 fprintf (stream
, "Disableable packets:\n"
2404 " vCont \tAll vCont packets\n"
2405 " qC \tQuerying the current thread\n"
2406 " qfThreadInfo\tThread listing\n"
2407 " Tthread \tPassing the thread specifier in the "
2408 "T stop reply packet\n"
2409 " threads \tAll of the above\n");
2413 #undef require_running
2414 #define require_running(BUF) \
2415 if (!target_running ()) \
2422 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2424 int pid
= * (int *) args
;
2426 if (ptid_get_pid (entry
->id
) == pid
)
2433 kill_inferior_callback (struct inferior_list_entry
*entry
)
2435 struct process_info
*process
= (struct process_info
*) entry
;
2436 int pid
= ptid_get_pid (process
->head
.id
);
2438 kill_inferior (pid
);
2439 discard_queued_stop_replies (pid
);
2442 /* Callback for for_each_inferior to detach or kill the inferior,
2443 depending on whether we attached to it or not.
2444 We inform the user whether we're detaching or killing the process
2445 as this is only called when gdbserver is about to exit. */
2448 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2450 struct process_info
*process
= (struct process_info
*) entry
;
2451 int pid
= ptid_get_pid (process
->head
.id
);
2453 if (process
->attached
)
2454 detach_inferior (pid
);
2456 kill_inferior (pid
);
2458 discard_queued_stop_replies (pid
);
2461 /* for_each_inferior callback for detach_or_kill_for_exit to print
2462 the pids of started inferiors. */
2465 print_started_pid (struct inferior_list_entry
*entry
)
2467 struct process_info
*process
= (struct process_info
*) entry
;
2469 if (! process
->attached
)
2471 int pid
= ptid_get_pid (process
->head
.id
);
2472 fprintf (stderr
, " %d", pid
);
2476 /* for_each_inferior callback for detach_or_kill_for_exit to print
2477 the pids of attached inferiors. */
2480 print_attached_pid (struct inferior_list_entry
*entry
)
2482 struct process_info
*process
= (struct process_info
*) entry
;
2484 if (process
->attached
)
2486 int pid
= ptid_get_pid (process
->head
.id
);
2487 fprintf (stderr
, " %d", pid
);
2491 /* Call this when exiting gdbserver with possible inferiors that need
2492 to be killed or detached from. */
2495 detach_or_kill_for_exit (void)
2497 /* First print a list of the inferiors we will be killing/detaching.
2498 This is to assist the user, for example, in case the inferior unexpectedly
2499 dies after we exit: did we screw up or did the inferior exit on its own?
2500 Having this info will save some head-scratching. */
2502 if (have_started_inferiors_p ())
2504 fprintf (stderr
, "Killing process(es):");
2505 for_each_inferior (&all_processes
, print_started_pid
);
2506 fprintf (stderr
, "\n");
2508 if (have_attached_inferiors_p ())
2510 fprintf (stderr
, "Detaching process(es):");
2511 for_each_inferior (&all_processes
, print_attached_pid
);
2512 fprintf (stderr
, "\n");
2515 /* Now we can kill or detach the inferiors. */
2517 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2521 main (int argc
, char *argv
[])
2525 char *arg_end
, *port
;
2526 char **next_arg
= &argv
[1];
2531 while (*next_arg
!= NULL
&& **next_arg
== '-')
2533 if (strcmp (*next_arg
, "--version") == 0)
2535 gdbserver_version ();
2538 else if (strcmp (*next_arg
, "--help") == 0)
2540 gdbserver_usage (stdout
);
2543 else if (strcmp (*next_arg
, "--attach") == 0)
2545 else if (strcmp (*next_arg
, "--multi") == 0)
2547 else if (strcmp (*next_arg
, "--wrapper") == 0)
2551 wrapper_argv
= next_arg
;
2552 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2555 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2557 gdbserver_usage (stderr
);
2561 /* Consume the "--". */
2564 else if (strcmp (*next_arg
, "--debug") == 0)
2566 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2568 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2570 gdbserver_show_disableable (stdout
);
2573 else if (strncmp (*next_arg
,
2574 "--disable-packet=",
2575 sizeof ("--disable-packet=") - 1) == 0)
2577 char *packets
, *tok
;
2579 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2580 for (tok
= strtok (packets
, ",");
2582 tok
= strtok (NULL
, ","))
2584 if (strcmp ("vCont", tok
) == 0)
2585 disable_packet_vCont
= 1;
2586 else if (strcmp ("Tthread", tok
) == 0)
2587 disable_packet_Tthread
= 1;
2588 else if (strcmp ("qC", tok
) == 0)
2589 disable_packet_qC
= 1;
2590 else if (strcmp ("qfThreadInfo", tok
) == 0)
2591 disable_packet_qfThreadInfo
= 1;
2592 else if (strcmp ("threads", tok
) == 0)
2594 disable_packet_vCont
= 1;
2595 disable_packet_Tthread
= 1;
2596 disable_packet_qC
= 1;
2597 disable_packet_qfThreadInfo
= 1;
2601 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2603 gdbserver_show_disableable (stderr
);
2608 else if (strcmp (*next_arg
, "-") == 0)
2610 /* "-" specifies a stdio connection and is a form of port
2612 *next_arg
= STDIO_CONNECTION_NAME
;
2615 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2616 disable_randomization
= 1;
2617 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2618 disable_randomization
= 0;
2619 else if (strcmp (*next_arg
, "--once") == 0)
2623 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2631 if (setjmp (toplevel
))
2633 fprintf (stderr
, "Exiting\n");
2639 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2641 gdbserver_usage (stderr
);
2645 /* We need to know whether the remote connection is stdio before
2646 starting the inferior. Inferiors created in this scenario have
2647 stdin,stdout redirected. So do this here before we call
2649 remote_prepare (port
);
2654 /* --attach used to come after PORT, so allow it there for
2656 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2663 && (*next_arg
== NULL
2664 || (*next_arg
)[0] == '\0'
2665 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2667 || next_arg
[1] != NULL
))
2672 gdbserver_usage (stderr
);
2676 initialize_async_io ();
2678 if (target_supports_tracepoints ())
2679 initialize_tracepoint ();
2681 own_buf
= xmalloc (PBUFSIZ
+ 1);
2682 mem_buf
= xmalloc (PBUFSIZ
);
2684 if (pid
== 0 && *next_arg
!= NULL
)
2688 n
= argc
- (next_arg
- argv
);
2689 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2690 for (i
= 0; i
< n
; i
++)
2691 program_argv
[i
] = xstrdup (next_arg
[i
]);
2692 program_argv
[i
] = NULL
;
2694 /* Wait till we are at first instruction in program. */
2695 start_inferior (program_argv
);
2697 /* We are now (hopefully) stopped at the first instruction of
2698 the target process. This assumes that the target process was
2699 successfully created. */
2703 if (attach_inferior (pid
) == -1)
2704 error ("Attaching not supported on this target");
2706 /* Otherwise succeeded. */
2710 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2711 last_status
.value
.integer
= 0;
2712 last_ptid
= minus_one_ptid
;
2715 /* Don't report shared library events on the initial connection,
2716 even if some libraries are preloaded. Avoids the "stopped by
2717 shared library event" notice on gdb side. */
2720 if (setjmp (toplevel
))
2722 detach_or_kill_for_exit ();
2726 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2727 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2732 if (!was_running
&& !multi_mode
)
2734 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2742 /* Be sure we're out of tfind mode. */
2743 current_traceframe
= -1;
2747 if (setjmp (toplevel
) != 0)
2749 /* An error occurred. */
2750 if (response_needed
)
2752 write_enn (own_buf
);
2757 /* Wait for events. This will return when all event sources are
2758 removed from the event loop. */
2759 start_event_loop ();
2761 /* If an exit was requested (using the "monitor exit" command),
2762 terminate now. The only other way to get here is for
2763 getpkt to fail; close the connection and reopen it at the
2766 if (exit_requested
|| run_once
)
2768 detach_or_kill_for_exit ();
2773 "Remote side has terminated connection. "
2774 "GDBserver will reopen the connection.\n");
2778 if (disconnected_tracing
)
2780 /* Try to enable non-stop/async mode, so we we can both
2781 wait for an async socket accept, and handle async
2782 target events simultaneously. There's also no point
2783 either in having the target always stop all threads,
2784 when we're going to pass signals down without
2788 if (start_non_stop (1))
2791 /* Detaching implicitly resumes all threads; simply
2792 disconnecting does not. */
2798 "Disconnected tracing disabled; stopping trace run.\n");
2805 /* Event loop callback that handles a serial event. The first byte in
2806 the serial buffer gets us here. We expect characters to arrive at
2807 a brisk pace, so we read the rest of the packet with a blocking
2811 process_serial_event (void)
2822 int new_packet_len
= -1;
2824 /* Used to decide when gdbserver should exit in
2825 multi-mode/remote. */
2826 static int have_ran
= 0;
2829 have_ran
= target_running ();
2831 disable_async_io ();
2833 response_needed
= 0;
2834 packet_len
= getpkt (own_buf
);
2835 if (packet_len
<= 0)
2838 /* Force an event loop break. */
2841 response_needed
= 1;
2848 handle_query (own_buf
, packet_len
, &new_packet_len
);
2851 handle_general_set (own_buf
);
2854 require_running (own_buf
);
2859 pid
= strtol (&own_buf
[i
], NULL
, 16);
2863 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2865 if (tracing
&& disconnected_tracing
)
2867 struct thread_resume resume_info
;
2868 struct process_info
*process
= find_process_pid (pid
);
2870 if (process
== NULL
)
2872 write_enn (own_buf
);
2877 "Disconnected tracing in effect, "
2878 "leaving gdbserver attached to the process\n");
2880 /* Make sure we're in non-stop/async mode, so we we can both
2881 wait for an async socket accept, and handle async target
2882 events simultaneously. There's also no point either in
2883 having the target stop all threads, when we're going to
2884 pass signals down without informing GDB. */
2888 fprintf (stderr
, "Forcing non-stop mode\n");
2894 process
->gdb_detached
= 1;
2896 /* Detaching implicitly resumes all threads. */
2897 resume_info
.thread
= minus_one_ptid
;
2898 resume_info
.kind
= resume_continue
;
2899 resume_info
.sig
= 0;
2900 (*the_target
->resume
) (&resume_info
, 1);
2903 break; /* from switch/case */
2906 fprintf (stderr
, "Detaching from process %d\n", pid
);
2908 if (detach_inferior (pid
) != 0)
2909 write_enn (own_buf
);
2912 discard_queued_stop_replies (pid
);
2915 if (extended_protocol
)
2917 /* Treat this like a normal program exit. */
2918 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2919 last_status
.value
.integer
= 0;
2920 last_ptid
= pid_to_ptid (pid
);
2922 current_inferior
= NULL
;
2929 /* If we are attached, then we can exit. Otherwise, we
2930 need to hang around doing nothing, until the child is
2932 join_inferior (pid
);
2938 extended_protocol
= 1;
2942 handle_status (own_buf
);
2945 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2947 ptid_t gdb_id
, thread_id
;
2950 require_running (own_buf
);
2952 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2954 pid
= ptid_get_pid (gdb_id
);
2956 if (ptid_equal (gdb_id
, null_ptid
)
2957 || ptid_equal (gdb_id
, minus_one_ptid
))
2958 thread_id
= null_ptid
;
2960 && ptid_equal (pid_to_ptid (pid
),
2963 struct thread_info
*thread
=
2964 (struct thread_info
*) find_inferior (&all_threads
,
2969 write_enn (own_buf
);
2973 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2977 thread_id
= gdb_id_to_thread_id (gdb_id
);
2978 if (ptid_equal (thread_id
, null_ptid
))
2980 write_enn (own_buf
);
2985 if (own_buf
[1] == 'g')
2987 if (ptid_equal (thread_id
, null_ptid
))
2989 /* GDB is telling us to choose any thread. Check if
2990 the currently selected thread is still valid. If
2991 it is not, select the first available. */
2992 struct thread_info
*thread
=
2993 (struct thread_info
*) find_inferior_id (&all_threads
,
2996 thread_id
= all_threads
.head
->id
;
2999 general_thread
= thread_id
;
3000 set_desired_inferior (1);
3002 else if (own_buf
[1] == 'c')
3003 cont_thread
= thread_id
;
3009 /* Silently ignore it so that gdb can extend the protocol
3010 without compatibility headaches. */
3015 require_running (own_buf
);
3016 if (current_traceframe
>= 0)
3018 struct regcache
*regcache
= new_register_cache ();
3020 if (fetch_traceframe_registers (current_traceframe
,
3022 registers_to_string (regcache
, own_buf
);
3024 write_enn (own_buf
);
3025 free_register_cache (regcache
);
3029 struct regcache
*regcache
;
3031 set_desired_inferior (1);
3032 regcache
= get_thread_regcache (current_inferior
, 1);
3033 registers_to_string (regcache
, own_buf
);
3037 require_running (own_buf
);
3038 if (current_traceframe
>= 0)
3039 write_enn (own_buf
);
3042 struct regcache
*regcache
;
3044 set_desired_inferior (1);
3045 regcache
= get_thread_regcache (current_inferior
, 1);
3046 registers_from_string (regcache
, &own_buf
[1]);
3051 require_running (own_buf
);
3052 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3053 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3055 write_enn (own_buf
);
3057 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3060 require_running (own_buf
);
3061 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3062 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3065 write_enn (own_buf
);
3068 require_running (own_buf
);
3069 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3070 &mem_addr
, &len
, &mem_buf
) < 0
3071 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3072 write_enn (own_buf
);
3077 require_running (own_buf
);
3078 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3079 if (target_signal_to_host_p (sig
))
3080 signal
= target_signal_to_host (sig
);
3083 myresume (own_buf
, 0, signal
);
3086 require_running (own_buf
);
3087 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3088 if (target_signal_to_host_p (sig
))
3089 signal
= target_signal_to_host (sig
);
3092 myresume (own_buf
, 1, signal
);
3095 require_running (own_buf
);
3097 myresume (own_buf
, 0, signal
);
3100 require_running (own_buf
);
3102 myresume (own_buf
, 1, signal
);
3104 case 'Z': /* insert_ ... */
3106 case 'z': /* remove_ ... */
3110 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3111 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3112 char type
= own_buf
[1];
3114 const int insert
= ch
== 'Z';
3116 /* Default to unrecognized/unsupported. */
3120 case '0': /* software-breakpoint */
3121 case '1': /* hardware-breakpoint */
3122 case '2': /* write watchpoint */
3123 case '3': /* read watchpoint */
3124 case '4': /* access watchpoint */
3125 require_running (own_buf
);
3126 if (insert
&& the_target
->insert_point
!= NULL
)
3127 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3128 else if (!insert
&& the_target
->remove_point
!= NULL
)
3129 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3141 write_enn (own_buf
);
3145 response_needed
= 0;
3146 if (!target_running ())
3147 /* The packet we received doesn't make sense - but we can't
3148 reply to it, either. */
3151 fprintf (stderr
, "Killing all inferiors\n");
3152 for_each_inferior (&all_processes
, kill_inferior_callback
);
3154 /* When using the extended protocol, we wait with no program
3155 running. The traditional protocol will exit instead. */
3156 if (extended_protocol
)
3158 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3159 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3167 ptid_t gdb_id
, thread_id
;
3169 require_running (own_buf
);
3171 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3172 thread_id
= gdb_id_to_thread_id (gdb_id
);
3173 if (ptid_equal (thread_id
, null_ptid
))
3175 write_enn (own_buf
);
3179 if (mythread_alive (thread_id
))
3182 write_enn (own_buf
);
3186 response_needed
= 0;
3188 /* Restarting the inferior is only supported in the extended
3190 if (extended_protocol
)
3192 if (target_running ())
3193 for_each_inferior (&all_processes
,
3194 kill_inferior_callback
);
3195 fprintf (stderr
, "GDBserver restarting\n");
3197 /* Wait till we are at 1st instruction in prog. */
3198 if (program_argv
!= NULL
)
3199 start_inferior (program_argv
);
3202 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3203 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3209 /* It is a request we don't understand. Respond with an
3210 empty packet so that gdb knows that we don't support this
3216 /* Extended (long) request. */
3217 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3221 /* It is a request we don't understand. Respond with an empty
3222 packet so that gdb knows that we don't support this
3228 if (new_packet_len
!= -1)
3229 putpkt_binary (own_buf
, new_packet_len
);
3233 response_needed
= 0;
3235 if (!extended_protocol
&& have_ran
&& !target_running ())
3237 /* In non-stop, defer exiting until GDB had a chance to query
3238 the whole vStopped list (until it gets an OK). */
3241 fprintf (stderr
, "GDBserver exiting\n");
3253 /* Event-loop callback for serial events. */
3256 handle_serial_event (int err
, gdb_client_data client_data
)
3259 fprintf (stderr
, "handling possible serial event\n");
3261 /* Really handle it. */
3262 if (process_serial_event () < 0)
3265 /* Be sure to not change the selected inferior behind GDB's back.
3266 Important in the non-stop mode asynchronous protocol. */
3267 set_desired_inferior (1);
3272 /* Event-loop callback for target events. */
3275 handle_target_event (int err
, gdb_client_data client_data
)
3278 fprintf (stderr
, "handling possible target event\n");
3280 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3283 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3285 int pid
= ptid_get_pid (last_ptid
);
3286 struct process_info
*process
= find_process_pid (pid
);
3287 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3289 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3290 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3292 mark_breakpoints_out (process
);
3293 mourn_inferior (process
);
3297 /* We're reporting this thread as stopped. Update its
3298 "want-stopped" state to what the client wants, until it
3299 gets a new resume action. */
3300 current_inferior
->last_resume_kind
= resume_stop
;
3301 current_inferior
->last_status
= last_status
;
3306 if (!target_running ())
3308 /* The last process exited. We're done. */
3312 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3314 /* A thread stopped with a signal, but gdb isn't
3315 connected to handle it. Pass it down to the
3316 inferior, as if it wasn't being traced. */
3317 struct thread_resume resume_info
;
3321 "GDB not connected; forwarding event %d for [%s]\n",
3322 (int) last_status
.kind
,
3323 target_pid_to_str (last_ptid
));
3325 resume_info
.thread
= last_ptid
;
3326 resume_info
.kind
= resume_continue
;
3327 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3328 (*the_target
->resume
) (&resume_info
, 1);
3330 else if (debug_threads
)
3331 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3332 (int) last_status
.kind
,
3333 target_pid_to_str (last_ptid
));
3337 /* Something interesting. Tell GDB about it. */
3338 push_event (last_ptid
, &last_status
);
3342 /* Be sure to not change the selected inferior behind GDB's back.
3343 Important in the non-stop mode asynchronous protocol. */
3344 set_desired_inferior (1);