1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbthread.h"
32 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
33 `vCont'. Note the multi-process extensions made `vCont' a
34 requirement, so `Hc pPID.TID' is pretty much undefined. So
35 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
36 resuming all threads of the process (again, `Hc' isn't used for
37 multi-process), or a specific thread ptid_t.
39 We also set this when handling a single-thread `vCont' resume, as
40 some places in the backends check it to know when (and for which
41 thread) single-thread scheduler-locking is in effect. */
44 /* The thread set with an `Hg' packet. */
45 ptid_t general_thread
;
49 static int extended_protocol
;
50 static int response_needed
;
51 static int exit_requested
;
53 /* --once: Exit after the first connection has closed. */
59 /* Whether we should attempt to disable the operating system's address
60 space randomization feature before starting an inferior. */
61 int disable_randomization
= 1;
63 static char **program_argv
, **wrapper_argv
;
65 /* Enable miscellaneous debugging output. The name is historical - it
66 was originally used to debug LinuxThreads support. */
69 /* Enable debugging of h/w breakpoint/watchpoint support. */
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
78 const char *gdbserver_xmltarget
;
80 /* The PID of the originally created or attached inferior. Used to
81 send signals to the process when GDB sends us an asynchronous interrupt
82 (user hitting Control-C in the client), and to wait for the child to exit
83 when no longer debugging it. */
85 unsigned long signal_pid
;
88 /* A file descriptor for the controlling terminal. */
91 /* TERMINAL_FD's original foreground group. */
92 pid_t old_foreground_pgrp
;
94 /* Hand back terminal ownership to the original foreground group. */
97 restore_old_foreground_pgrp (void)
99 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
103 /* Set if you want to disable optional thread related packets support
104 in gdbserver, for the sake of testing GDB against stubs that don't
106 int disable_packet_vCont
;
107 int disable_packet_Tthread
;
108 int disable_packet_qC
;
109 int disable_packet_qfThreadInfo
;
111 /* Last status reported to GDB. */
112 static struct target_waitstatus last_status
;
113 static ptid_t last_ptid
;
115 static char *own_buf
;
116 static unsigned char *mem_buf
;
118 /* A sub-class of 'struct notif_event' for stop, holding information
119 relative to a single stop reply. We keep a queue of these to
120 push to GDB in non-stop mode. */
124 struct notif_event base
;
126 /* Thread or process that got the event. */
130 struct target_waitstatus status
;
133 DEFINE_QUEUE_P (notif_event_p
);
135 /* Put a stop reply to the stop reply queue. */
138 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
140 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
142 new_notif
->ptid
= ptid
;
143 new_notif
->status
= *status
;
145 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
149 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
150 QUEUE_ITER (notif_event_p
) *iter
,
151 struct notif_event
*event
,
157 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
159 if (q
->free_func
!= NULL
)
160 q
->free_func (event
);
162 QUEUE_remove_elem (notif_event_p
, q
, iter
);
168 /* Get rid of the currently pending stop replies for PID. If PID is
169 -1, then apply to all processes. */
172 discard_queued_stop_replies (int pid
)
174 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
175 remove_all_on_match_pid
, &pid
);
179 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
181 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
183 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
186 struct notif_server notif_stop
=
188 "vStopped", "Stop", NULL
, vstop_notif_reply
,
192 target_running (void)
194 return all_threads
.head
!= NULL
;
198 start_inferior (char **argv
)
200 char **new_argv
= argv
;
202 if (wrapper_argv
!= NULL
)
206 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
208 for (i
= 0; argv
[i
] != NULL
; i
++)
210 new_argv
= alloca (sizeof (char *) * count
);
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = wrapper_argv
[i
];
214 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
[count
++] = argv
[i
];
216 new_argv
[count
] = NULL
;
222 for (i
= 0; new_argv
[i
]; ++i
)
223 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
228 signal (SIGTTOU
, SIG_DFL
);
229 signal (SIGTTIN
, SIG_DFL
);
232 /* Clear this so the backend doesn't get confused, thinking
233 CONT_THREAD died, and it needs to resume all threads. */
234 cont_thread
= null_ptid
;
236 signal_pid
= create_inferior (new_argv
[0], new_argv
);
238 /* FIXME: we don't actually know at this point that the create
239 actually succeeded. We won't know that until we wait. */
240 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
245 signal (SIGTTOU
, SIG_IGN
);
246 signal (SIGTTIN
, SIG_IGN
);
247 terminal_fd
= fileno (stderr
);
248 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
249 tcsetpgrp (terminal_fd
, signal_pid
);
250 atexit (restore_old_foreground_pgrp
);
253 if (wrapper_argv
!= NULL
)
255 struct thread_resume resume_info
;
257 resume_info
.thread
= pid_to_ptid (signal_pid
);
258 resume_info
.kind
= resume_continue
;
261 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
263 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
268 (*the_target
->resume
) (&resume_info
, 1);
270 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
271 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
274 current_inferior
->last_resume_kind
= resume_stop
;
275 current_inferior
->last_status
= last_status
;
277 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
282 /* Wait till we are at 1st instruction in program, return new pid
283 (assuming success). */
284 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
286 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
287 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
289 current_inferior
->last_resume_kind
= resume_stop
;
290 current_inferior
->last_status
= last_status
;
297 attach_inferior (int pid
)
299 /* myattach should return -1 if attaching is unsupported,
300 0 if it succeeded, and call error() otherwise. */
302 if (myattach (pid
) != 0)
305 fprintf (stderr
, "Attached; pid = %d\n", pid
);
308 /* FIXME - It may be that we should get the SIGNAL_PID from the
309 attach function, so that it can be the main thread instead of
310 whichever we were told to attach to. */
313 /* Clear this so the backend doesn't get confused, thinking
314 CONT_THREAD died, and it needs to resume all threads. */
315 cont_thread
= null_ptid
;
319 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
321 /* GDB knows to ignore the first SIGSTOP after attaching to a running
322 process using the "attach" command, but this is different; it's
323 just using "target remote". Pretend it's just starting up. */
324 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
325 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
326 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
328 current_inferior
->last_resume_kind
= resume_stop
;
329 current_inferior
->last_status
= last_status
;
335 extern int remote_debug
;
337 /* Decode a qXfer read request. Return 0 if everything looks OK,
341 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
343 /* After the read marker and annex, qXfer looks like a
344 traditional 'm' packet. */
345 decode_m_packet (buf
, ofs
, len
);
351 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
353 /* Extract and NUL-terminate the object. */
355 while (*buf
&& *buf
!= ':')
361 /* Extract and NUL-terminate the read/write action. */
363 while (*buf
&& *buf
!= ':')
369 /* Extract and NUL-terminate the annex. */
371 while (*buf
&& *buf
!= ':')
381 /* Write the response to a successful qXfer read. Returns the
382 length of the (binary) data stored in BUF, corresponding
383 to as much of DATA/LEN as we could fit. IS_MORE controls
384 the first character of the response. */
386 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
395 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
399 /* Handle all of the extended 'Q' packets. */
402 handle_general_set (char *own_buf
)
404 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
406 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
407 const char *p
= own_buf
+ strlen ("QPassSignals:");
410 p
= decode_address_to_semicolon (&cursig
, p
);
411 for (i
= 0; i
< numsigs
; i
++)
417 /* Keep looping, to clear the remaining signals. */
420 p
= decode_address_to_semicolon (&cursig
, p
);
425 strcpy (own_buf
, "OK");
429 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
431 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
432 const char *p
= own_buf
+ strlen ("QProgramSignals:");
435 program_signals_p
= 1;
437 p
= decode_address_to_semicolon (&cursig
, p
);
438 for (i
= 0; i
< numsigs
; i
++)
442 program_signals
[i
] = 1;
444 /* Keep looping, to clear the remaining signals. */
447 p
= decode_address_to_semicolon (&cursig
, p
);
450 program_signals
[i
] = 0;
452 strcpy (own_buf
, "OK");
456 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
460 fprintf (stderr
, "[noack mode enabled]\n");
469 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
471 char *mode
= own_buf
+ 9;
475 if (strcmp (mode
, "0") == 0)
477 else if (strcmp (mode
, "1") == 0)
481 /* We don't know what this mode is, so complain to
483 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
489 req_str
= req
? "non-stop" : "all-stop";
490 if (start_non_stop (req
) != 0)
492 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
500 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
506 if (strncmp ("QDisableRandomization:", own_buf
,
507 strlen ("QDisableRandomization:")) == 0)
509 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
512 unpack_varlen_hex (packet
, &setting
);
513 disable_randomization
= setting
;
517 if (disable_randomization
)
518 fprintf (stderr
, "[address space randomization disabled]\n");
520 fprintf (stderr
, "[address space randomization enabled]\n");
527 if (target_supports_tracepoints ()
528 && handle_tracepoint_general_set (own_buf
))
531 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
533 char *mode
= own_buf
+ strlen ("QAgent:");
536 if (strcmp (mode
, "0") == 0)
538 else if (strcmp (mode
, "1") == 0)
542 /* We don't know what this value is, so complain to GDB. */
543 sprintf (own_buf
, "E.Unknown QAgent value");
547 /* Update the flag. */
550 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
555 /* Otherwise we didn't know what packet it was. Say we didn't
561 get_features_xml (const char *annex
)
563 /* gdbserver_xmltarget defines what to return when looking
564 for the "target.xml" file. Its contents can either be
565 verbatim XML code (prefixed with a '@') or else the name
566 of the actual XML file to be used in place of "target.xml".
568 This variable is set up from the auto-generated
569 init_registers_... routine for the current target. */
571 if (gdbserver_xmltarget
572 && strcmp (annex
, "target.xml") == 0)
574 if (*gdbserver_xmltarget
== '@')
575 return gdbserver_xmltarget
+ 1;
577 annex
= gdbserver_xmltarget
;
582 extern const char *const xml_builtin
[][2];
585 /* Look for the annex. */
586 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
587 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
590 if (xml_builtin
[i
][0] != NULL
)
591 return xml_builtin
[i
][1];
599 monitor_show_help (void)
601 monitor_output ("The following monitor commands are supported:\n");
602 monitor_output (" set debug <0|1>\n");
603 monitor_output (" Enable general debugging messages\n");
604 monitor_output (" set debug-hw-points <0|1>\n");
605 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
606 monitor_output (" set remote-debug <0|1>\n");
607 monitor_output (" Enable remote protocol debugging messages\n");
608 monitor_output (" exit\n");
609 monitor_output (" Quit GDBserver\n");
612 /* Read trace frame or inferior memory. Returns the number of bytes
613 actually read, zero when no further transfer is possible, and -1 on
614 error. Return of a positive value smaller than LEN does not
615 indicate there's no more to be read, only the end of the transfer.
616 E.g., when GDB reads memory from a traceframe, a first request may
617 be served from a memory block that does not cover the whole request
618 length. A following request gets the rest served from either
619 another block (of the same traceframe) or from the read-only
623 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
627 if (current_traceframe
>= 0)
630 ULONGEST length
= len
;
632 if (traceframe_read_mem (current_traceframe
,
633 memaddr
, myaddr
, len
, &nbytes
))
635 /* Data read from trace buffer, we're done. */
638 if (!in_readonly_region (memaddr
, length
))
640 /* Otherwise we have a valid readonly case, fall through. */
641 /* (assume no half-trace half-real blocks for now) */
644 res
= prepare_to_access_memory ();
647 res
= read_inferior_memory (memaddr
, myaddr
, len
);
648 done_accessing_memory ();
650 return res
== 0 ? len
: -1;
656 /* Write trace frame or inferior memory. Actually, writing to trace
657 frames is forbidden. */
660 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
662 if (current_traceframe
>= 0)
668 ret
= prepare_to_access_memory ();
671 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
672 done_accessing_memory ();
678 /* Subroutine of handle_search_memory to simplify it. */
681 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
682 gdb_byte
*pattern
, unsigned pattern_len
,
683 gdb_byte
*search_buf
,
684 unsigned chunk_size
, unsigned search_buf_size
,
685 CORE_ADDR
*found_addrp
)
687 /* Prime the search buffer. */
689 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
692 warning ("Unable to access %ld bytes of target "
693 "memory at 0x%lx, halting search.",
694 (long) search_buf_size
, (long) start_addr
);
698 /* Perform the search.
700 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
701 When we've scanned N bytes we copy the trailing bytes to the start and
702 read in another N bytes. */
704 while (search_space_len
>= pattern_len
)
707 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
711 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
713 if (found_ptr
!= NULL
)
715 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
716 *found_addrp
= found_addr
;
720 /* Not found in this chunk, skip to next chunk. */
722 /* Don't let search_space_len wrap here, it's unsigned. */
723 if (search_space_len
>= chunk_size
)
724 search_space_len
-= chunk_size
;
726 search_space_len
= 0;
728 if (search_space_len
>= pattern_len
)
730 unsigned keep_len
= search_buf_size
- chunk_size
;
731 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
734 /* Copy the trailing part of the previous iteration to the front
735 of the buffer for the next iteration. */
736 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
738 nr_to_read
= (search_space_len
- keep_len
< chunk_size
739 ? search_space_len
- keep_len
742 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
743 nr_to_read
) != search_buf_size
)
745 warning ("Unable to access %ld bytes of target memory "
746 "at 0x%lx, halting search.",
747 (long) nr_to_read
, (long) read_addr
);
751 start_addr
+= chunk_size
;
760 /* Handle qSearch:memory packets. */
763 handle_search_memory (char *own_buf
, int packet_len
)
765 CORE_ADDR start_addr
;
766 CORE_ADDR search_space_len
;
768 unsigned int pattern_len
;
769 /* NOTE: also defined in find.c testcase. */
770 #define SEARCH_CHUNK_SIZE 16000
771 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
772 /* Buffer to hold memory contents for searching. */
773 gdb_byte
*search_buf
;
774 unsigned search_buf_size
;
776 CORE_ADDR found_addr
;
777 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
779 pattern
= malloc (packet_len
);
782 error ("Unable to allocate memory to perform the search");
783 strcpy (own_buf
, "E00");
786 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
787 packet_len
- cmd_name_len
,
788 &start_addr
, &search_space_len
,
789 pattern
, &pattern_len
) < 0)
792 error ("Error in parsing qSearch:memory packet");
793 strcpy (own_buf
, "E00");
797 search_buf_size
= chunk_size
+ pattern_len
- 1;
799 /* No point in trying to allocate a buffer larger than the search space. */
800 if (search_space_len
< search_buf_size
)
801 search_buf_size
= search_space_len
;
803 search_buf
= malloc (search_buf_size
);
804 if (search_buf
== NULL
)
807 error ("Unable to allocate memory to perform the search");
808 strcpy (own_buf
, "E00");
812 found
= handle_search_memory_1 (start_addr
, search_space_len
,
813 pattern
, pattern_len
,
814 search_buf
, chunk_size
, search_buf_size
,
818 sprintf (own_buf
, "1,%lx", (long) found_addr
);
820 strcpy (own_buf
, "0");
822 strcpy (own_buf
, "E00");
828 #define require_running(BUF) \
829 if (!target_running ()) \
835 /* Handle monitor commands not handled by target-specific handlers. */
838 handle_monitor_command (char *mon
, char *own_buf
)
840 if (strcmp (mon
, "set debug 1") == 0)
843 monitor_output ("Debug output enabled.\n");
845 else if (strcmp (mon
, "set debug 0") == 0)
848 monitor_output ("Debug output disabled.\n");
850 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
853 monitor_output ("H/W point debugging output enabled.\n");
855 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
858 monitor_output ("H/W point debugging output disabled.\n");
860 else if (strcmp (mon
, "set remote-debug 1") == 0)
863 monitor_output ("Protocol debug output enabled.\n");
865 else if (strcmp (mon
, "set remote-debug 0") == 0)
868 monitor_output ("Protocol debug output disabled.\n");
870 else if (strcmp (mon
, "help") == 0)
871 monitor_show_help ();
872 else if (strcmp (mon
, "exit") == 0)
876 monitor_output ("Unknown monitor command.\n\n");
877 monitor_show_help ();
882 /* Associates a callback with each supported qXfer'able object. */
886 /* The object this handler handles. */
889 /* Request that the target transfer up to LEN 8-bit bytes of the
890 target's OBJECT. The OFFSET, for a seekable object, specifies
891 the starting point. The ANNEX can be used to provide additional
892 data-specific information to the target.
894 Return the number of bytes actually transfered, zero when no
895 further transfer is possible, -1 on error, and -2 when the
896 transfer is not supported. Return of a positive value smaller
897 than LEN does not indicate the end of the object, only the end of
900 One, and only one, of readbuf or writebuf must be non-NULL. */
901 int (*xfer
) (const char *annex
,
902 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
903 ULONGEST offset
, LONGEST len
);
906 /* Handle qXfer:auxv:read. */
909 handle_qxfer_auxv (const char *annex
,
910 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
911 ULONGEST offset
, LONGEST len
)
913 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
916 if (annex
[0] != '\0' || !target_running ())
919 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
922 /* Handle qXfer:features:read. */
925 handle_qxfer_features (const char *annex
,
926 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
927 ULONGEST offset
, LONGEST len
)
929 const char *document
;
932 if (writebuf
!= NULL
)
935 if (!target_running ())
938 /* Grab the correct annex. */
939 document
= get_features_xml (annex
);
940 if (document
== NULL
)
943 total_len
= strlen (document
);
945 if (offset
> total_len
)
948 if (offset
+ len
> total_len
)
949 len
= total_len
- offset
;
951 memcpy (readbuf
, document
+ offset
, len
);
955 /* Handle qXfer:libraries:read. */
958 handle_qxfer_libraries (const char *annex
,
959 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
960 ULONGEST offset
, LONGEST len
)
962 unsigned int total_len
;
964 struct inferior_list_entry
*dll_ptr
;
966 if (writebuf
!= NULL
)
969 if (annex
[0] != '\0' || !target_running ())
972 /* Over-estimate the necessary memory. Assume that every character
973 in the library name must be escaped. */
975 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
976 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
978 document
= malloc (total_len
);
979 if (document
== NULL
)
982 strcpy (document
, "<library-list>\n");
983 p
= document
+ strlen (document
);
985 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
987 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
990 strcpy (p
, " <library name=\"");
992 name
= xml_escape_text (dll
->name
);
996 strcpy (p
, "\"><segment address=\"");
998 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1000 strcpy (p
, "\"/></library>\n");
1004 strcpy (p
, "</library-list>\n");
1006 total_len
= strlen (document
);
1008 if (offset
> total_len
)
1014 if (offset
+ len
> total_len
)
1015 len
= total_len
- offset
;
1017 memcpy (readbuf
, document
+ offset
, len
);
1022 /* Handle qXfer:libraries-svr4:read. */
1025 handle_qxfer_libraries_svr4 (const char *annex
,
1026 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1027 ULONGEST offset
, LONGEST len
)
1029 if (writebuf
!= NULL
)
1032 if (annex
[0] != '\0' || !target_running ()
1033 || the_target
->qxfer_libraries_svr4
== NULL
)
1036 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1039 /* Handle qXfer:osadata:read. */
1042 handle_qxfer_osdata (const char *annex
,
1043 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1044 ULONGEST offset
, LONGEST len
)
1046 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1049 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1052 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1055 handle_qxfer_siginfo (const char *annex
,
1056 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1057 ULONGEST offset
, LONGEST len
)
1059 if (the_target
->qxfer_siginfo
== NULL
)
1062 if (annex
[0] != '\0' || !target_running ())
1065 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1068 /* Handle qXfer:spu:read and qXfer:spu:write. */
1071 handle_qxfer_spu (const char *annex
,
1072 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1073 ULONGEST offset
, LONGEST len
)
1075 if (the_target
->qxfer_spu
== NULL
)
1078 if (!target_running ())
1081 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1084 /* Handle qXfer:statictrace:read. */
1087 handle_qxfer_statictrace (const char *annex
,
1088 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1089 ULONGEST offset
, LONGEST len
)
1093 if (writebuf
!= NULL
)
1096 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1099 if (traceframe_read_sdata (current_traceframe
, offset
,
1100 readbuf
, len
, &nbytes
))
1105 /* Helper for handle_qxfer_threads. */
1108 handle_qxfer_threads_proper (struct buffer
*buffer
)
1110 struct inferior_list_entry
*thread
;
1112 buffer_grow_str (buffer
, "<threads>\n");
1114 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1116 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1118 int core
= target_core_of_thread (ptid
);
1121 write_ptid (ptid_s
, ptid
);
1125 sprintf (core_s
, "%d", core
);
1126 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1131 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1136 buffer_grow_str0 (buffer
, "</threads>\n");
1139 /* Handle qXfer:threads:read. */
1142 handle_qxfer_threads (const char *annex
,
1143 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1144 ULONGEST offset
, LONGEST len
)
1146 static char *result
= 0;
1147 static unsigned int result_length
= 0;
1149 if (writebuf
!= NULL
)
1152 if (!target_running () || annex
[0] != '\0')
1157 struct buffer buffer
;
1158 /* When asked for data at offset 0, generate everything and store into
1159 'result'. Successive reads will be served off 'result'. */
1163 buffer_init (&buffer
);
1165 handle_qxfer_threads_proper (&buffer
);
1167 result
= buffer_finish (&buffer
);
1168 result_length
= strlen (result
);
1169 buffer_free (&buffer
);
1172 if (offset
>= result_length
)
1174 /* We're out of data. */
1181 if (len
> result_length
- offset
)
1182 len
= result_length
- offset
;
1184 memcpy (readbuf
, result
+ offset
, len
);
1189 /* Handle qXfer:traceframe-info:read. */
1192 handle_qxfer_traceframe_info (const char *annex
,
1193 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1194 ULONGEST offset
, LONGEST len
)
1196 static char *result
= 0;
1197 static unsigned int result_length
= 0;
1199 if (writebuf
!= NULL
)
1202 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1207 struct buffer buffer
;
1209 /* When asked for data at offset 0, generate everything and
1210 store into 'result'. Successive reads will be served off
1214 buffer_init (&buffer
);
1216 traceframe_read_info (current_traceframe
, &buffer
);
1218 result
= buffer_finish (&buffer
);
1219 result_length
= strlen (result
);
1220 buffer_free (&buffer
);
1223 if (offset
>= result_length
)
1225 /* We're out of data. */
1232 if (len
> result_length
- offset
)
1233 len
= result_length
- offset
;
1235 memcpy (readbuf
, result
+ offset
, len
);
1239 /* Handle qXfer:fdpic:read. */
1242 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1243 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1245 if (the_target
->read_loadmap
== NULL
)
1248 if (!target_running ())
1251 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1254 static const struct qxfer qxfer_packets
[] =
1256 { "auxv", handle_qxfer_auxv
},
1257 { "fdpic", handle_qxfer_fdpic
},
1258 { "features", handle_qxfer_features
},
1259 { "libraries", handle_qxfer_libraries
},
1260 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1261 { "osdata", handle_qxfer_osdata
},
1262 { "siginfo", handle_qxfer_siginfo
},
1263 { "spu", handle_qxfer_spu
},
1264 { "statictrace", handle_qxfer_statictrace
},
1265 { "threads", handle_qxfer_threads
},
1266 { "traceframe-info", handle_qxfer_traceframe_info
},
1270 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1278 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1281 /* Grab the object, r/w and annex. */
1282 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1284 write_enn (own_buf
);
1289 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1292 const struct qxfer
*q
= &qxfer_packets
[i
];
1294 if (strcmp (object
, q
->object
) == 0)
1296 if (strcmp (rw
, "read") == 0)
1298 unsigned char *data
;
1303 /* Grab the offset and length. */
1304 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1306 write_enn (own_buf
);
1310 /* Read one extra byte, as an indicator of whether there is
1312 if (len
> PBUFSIZ
- 2)
1314 data
= malloc (len
+ 1);
1317 write_enn (own_buf
);
1320 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1327 write_enn (own_buf
);
1329 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1331 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1336 else if (strcmp (rw
, "write") == 0)
1341 unsigned char *data
;
1343 strcpy (own_buf
, "E00");
1344 data
= malloc (packet_len
- (offset
- own_buf
));
1347 write_enn (own_buf
);
1350 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1351 &ofs
, &len
, data
) < 0)
1354 write_enn (own_buf
);
1358 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1365 write_enn (own_buf
);
1367 sprintf (own_buf
, "%x", n
);
1380 /* Table used by the crc32 function to calcuate the checksum. */
1382 static unsigned int crc32_table
[256] =
1385 /* Compute 32 bit CRC from inferior memory.
1387 On success, return 32 bit CRC.
1388 On failure, return (unsigned long long) -1. */
1390 static unsigned long long
1391 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1393 if (!crc32_table
[1])
1395 /* Initialize the CRC table and the decoding table. */
1399 for (i
= 0; i
< 256; i
++)
1401 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1402 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1409 unsigned char byte
= 0;
1411 /* Return failure if memory read fails. */
1412 if (read_inferior_memory (base
, &byte
, 1) != 0)
1413 return (unsigned long long) -1;
1415 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1418 return (unsigned long long) crc
;
1421 /* Handle all of the extended 'q' packets. */
1424 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1426 static struct inferior_list_entry
*thread_ptr
;
1428 /* Reply the current thread id. */
1429 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1432 require_running (own_buf
);
1434 if (!ptid_equal (general_thread
, null_ptid
)
1435 && !ptid_equal (general_thread
, minus_one_ptid
))
1436 gdb_id
= general_thread
;
1439 thread_ptr
= all_threads
.head
;
1440 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1443 sprintf (own_buf
, "QC");
1445 write_ptid (own_buf
, gdb_id
);
1449 if (strcmp ("qSymbol::", own_buf
) == 0)
1451 /* GDB is suggesting new symbols have been loaded. This may
1452 mean a new shared library has been detected as loaded, so
1453 take the opportunity to check if breakpoints we think are
1454 inserted, still are. Note that it isn't guaranteed that
1455 we'll see this when a shared library is loaded, and nor will
1456 we see this for unloads (although breakpoints in unloaded
1457 libraries shouldn't trigger), as GDB may not find symbols for
1458 the library at all. We also re-validate breakpoints when we
1459 see a second GDB breakpoint for the same address, and or when
1460 we access breakpoint shadows. */
1461 validate_breakpoints ();
1463 if (target_supports_tracepoints ())
1464 tracepoint_look_up_symbols ();
1466 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1467 (*the_target
->look_up_symbols
) ();
1469 strcpy (own_buf
, "OK");
1473 if (!disable_packet_qfThreadInfo
)
1475 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1479 require_running (own_buf
);
1480 thread_ptr
= all_threads
.head
;
1483 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1484 write_ptid (own_buf
, gdb_id
);
1485 thread_ptr
= thread_ptr
->next
;
1489 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1493 require_running (own_buf
);
1494 if (thread_ptr
!= NULL
)
1497 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1498 write_ptid (own_buf
, gdb_id
);
1499 thread_ptr
= thread_ptr
->next
;
1504 sprintf (own_buf
, "l");
1510 if (the_target
->read_offsets
!= NULL
1511 && strcmp ("qOffsets", own_buf
) == 0)
1513 CORE_ADDR text
, data
;
1515 require_running (own_buf
);
1516 if (the_target
->read_offsets (&text
, &data
))
1517 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1518 (long)text
, (long)data
, (long)data
);
1520 write_enn (own_buf
);
1525 /* Protocol features query. */
1526 if (strncmp ("qSupported", own_buf
, 10) == 0
1527 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1529 char *p
= &own_buf
[10];
1530 int gdb_supports_qRelocInsn
= 0;
1532 /* Start processing qSupported packet. */
1533 target_process_qsupported (NULL
);
1535 /* Process each feature being provided by GDB. The first
1536 feature will follow a ':', and latter features will follow
1540 char **qsupported
= NULL
;
1544 /* Two passes, to avoid nested strtok calls in
1545 target_process_qsupported. */
1546 for (p
= strtok (p
+ 1, ";");
1548 p
= strtok (NULL
, ";"))
1551 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1552 qsupported
[count
- 1] = xstrdup (p
);
1555 for (i
= 0; i
< count
; i
++)
1558 if (strcmp (p
, "multiprocess+") == 0)
1560 /* GDB supports and wants multi-process support if
1562 if (target_supports_multi_process ())
1565 else if (strcmp (p
, "qRelocInsn+") == 0)
1567 /* GDB supports relocate instruction requests. */
1568 gdb_supports_qRelocInsn
= 1;
1571 target_process_qsupported (p
);
1580 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1583 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1584 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1587 /* We do not have any hook to indicate whether the non-SVR4 target
1588 backend supports qXfer:libraries:read, so always report it. */
1589 strcat (own_buf
, ";qXfer:libraries:read+");
1592 if (the_target
->read_auxv
!= NULL
)
1593 strcat (own_buf
, ";qXfer:auxv:read+");
1595 if (the_target
->qxfer_spu
!= NULL
)
1596 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1598 if (the_target
->qxfer_siginfo
!= NULL
)
1599 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1601 if (the_target
->read_loadmap
!= NULL
)
1602 strcat (own_buf
, ";qXfer:fdpic:read+");
1604 /* We always report qXfer:features:read, as targets may
1605 install XML files on a subsequent call to arch_setup.
1606 If we reported to GDB on startup that we don't support
1607 qXfer:feature:read at all, we will never be re-queried. */
1608 strcat (own_buf
, ";qXfer:features:read+");
1610 if (transport_is_reliable
)
1611 strcat (own_buf
, ";QStartNoAckMode+");
1613 if (the_target
->qxfer_osdata
!= NULL
)
1614 strcat (own_buf
, ";qXfer:osdata:read+");
1616 if (target_supports_multi_process ())
1617 strcat (own_buf
, ";multiprocess+");
1619 if (target_supports_non_stop ())
1620 strcat (own_buf
, ";QNonStop+");
1622 if (target_supports_disable_randomization ())
1623 strcat (own_buf
, ";QDisableRandomization+");
1625 strcat (own_buf
, ";qXfer:threads:read+");
1627 if (target_supports_tracepoints ())
1629 strcat (own_buf
, ";ConditionalTracepoints+");
1630 strcat (own_buf
, ";TraceStateVariables+");
1631 strcat (own_buf
, ";TracepointSource+");
1632 strcat (own_buf
, ";DisconnectedTracing+");
1633 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1634 strcat (own_buf
, ";FastTracepoints+");
1635 strcat (own_buf
, ";StaticTracepoints+");
1636 strcat (own_buf
, ";InstallInTrace+");
1637 strcat (own_buf
, ";qXfer:statictrace:read+");
1638 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1639 strcat (own_buf
, ";EnableDisableTracepoints+");
1640 strcat (own_buf
, ";QTBuffer:size+");
1641 strcat (own_buf
, ";tracenz+");
1644 /* Support target-side breakpoint conditions and commands. */
1645 strcat (own_buf
, ";ConditionalBreakpoints+");
1646 strcat (own_buf
, ";BreakpointCommands+");
1648 if (target_supports_agent ())
1649 strcat (own_buf
, ";QAgent+");
1654 /* Thread-local storage support. */
1655 if (the_target
->get_tls_address
!= NULL
1656 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1658 char *p
= own_buf
+ 12;
1659 CORE_ADDR parts
[2], address
= 0;
1661 ptid_t ptid
= null_ptid
;
1663 require_running (own_buf
);
1665 for (i
= 0; i
< 3; i
++)
1673 p2
= strchr (p
, ',');
1686 ptid
= read_ptid (p
, NULL
);
1688 decode_address (&parts
[i
- 1], p
, len
);
1692 if (p
!= NULL
|| i
< 3)
1696 struct thread_info
*thread
= find_thread_ptid (ptid
);
1701 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1707 strcpy (own_buf
, paddress(address
));
1712 write_enn (own_buf
);
1716 /* Otherwise, pretend we do not understand this packet. */
1719 /* Windows OS Thread Information Block address support. */
1720 if (the_target
->get_tib_address
!= NULL
1721 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1726 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1728 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1731 strcpy (own_buf
, paddress(tlb
));
1736 write_enn (own_buf
);
1742 /* Handle "monitor" commands. */
1743 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1745 char *mon
= malloc (PBUFSIZ
);
1746 int len
= strlen (own_buf
+ 6);
1750 write_enn (own_buf
);
1754 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1756 write_enn (own_buf
);
1760 mon
[len
/ 2] = '\0';
1764 if (the_target
->handle_monitor_command
== NULL
1765 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1766 /* Default processing. */
1767 handle_monitor_command (mon
, own_buf
);
1773 if (strncmp ("qSearch:memory:", own_buf
,
1774 sizeof ("qSearch:memory:") - 1) == 0)
1776 require_running (own_buf
);
1777 handle_search_memory (own_buf
, packet_len
);
1781 if (strcmp (own_buf
, "qAttached") == 0
1782 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1784 struct process_info
*process
;
1786 if (own_buf
[sizeof ("qAttached") - 1])
1788 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1789 process
= (struct process_info
*)
1790 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1794 require_running (own_buf
);
1795 process
= current_process ();
1798 if (process
== NULL
)
1800 write_enn (own_buf
);
1804 strcpy (own_buf
, process
->attached
? "1" : "0");
1808 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1810 /* CRC check (compare-section). */
1814 unsigned long long crc
;
1816 require_running (own_buf
);
1817 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
1818 if (*comma
++ != ',')
1820 write_enn (own_buf
);
1823 len
= strtoul (comma
, NULL
, 16);
1824 crc
= crc32 (base
, len
, 0xffffffff);
1825 /* Check for memory failure. */
1826 if (crc
== (unsigned long long) -1)
1828 write_enn (own_buf
);
1831 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1835 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1838 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1841 /* Otherwise we didn't know what packet it was. Say we didn't
1846 static void gdb_wants_all_threads_stopped (void);
1848 /* Parse vCont packets. */
1850 handle_v_cont (char *own_buf
)
1854 struct thread_resume
*resume_info
;
1855 struct thread_resume default_action
= {{0}};
1857 /* Count the number of semicolons in the packet. There should be one
1858 for every action. */
1864 p
= strchr (p
, ';');
1867 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1868 if (resume_info
== NULL
)
1876 if (p
[0] == 's' || p
[0] == 'S')
1877 resume_info
[i
].kind
= resume_step
;
1878 else if (p
[0] == 'c' || p
[0] == 'C')
1879 resume_info
[i
].kind
= resume_continue
;
1880 else if (p
[0] == 't')
1881 resume_info
[i
].kind
= resume_stop
;
1885 if (p
[0] == 'S' || p
[0] == 'C')
1888 sig
= strtol (p
+ 1, &q
, 16);
1893 if (!gdb_signal_to_host_p (sig
))
1895 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
1899 resume_info
[i
].sig
= 0;
1905 resume_info
[i
].thread
= minus_one_ptid
;
1906 default_action
= resume_info
[i
];
1908 /* Note: we don't increment i here, we'll overwrite this entry
1909 the next time through. */
1911 else if (p
[0] == ':')
1913 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1918 if (p
[0] != ';' && p
[0] != 0)
1921 resume_info
[i
].thread
= ptid
;
1928 resume_info
[i
] = default_action
;
1930 /* `cont_thread' is still used in occasional places in the backend,
1931 to implement single-thread scheduler-locking. Doesn't make sense
1932 to set it if we see a stop request, or a wildcard action (one
1933 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
1935 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1936 || ptid_get_lwp (resume_info
[0].thread
) == -1)
1937 && resume_info
[0].kind
!= resume_stop
)
1938 cont_thread
= resume_info
[0].thread
;
1940 cont_thread
= minus_one_ptid
;
1941 set_desired_inferior (0);
1946 (*the_target
->resume
) (resume_info
, n
);
1954 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1956 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1957 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1958 current_inferior
->last_status
= last_status
;
1960 /* From the client's perspective, all-stop mode always stops all
1961 threads implicitly (and the target backend has already done
1962 so by now). Tag all threads as "want-stopped", so we don't
1963 resume them implicitly without the client telling us to. */
1964 gdb_wants_all_threads_stopped ();
1965 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1966 disable_async_io ();
1968 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1969 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1970 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1975 write_enn (own_buf
);
1980 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1982 handle_v_attach (char *own_buf
)
1986 pid
= strtol (own_buf
+ 8, NULL
, 16);
1987 if (pid
!= 0 && attach_inferior (pid
) == 0)
1989 /* Don't report shared library events after attaching, even if
1990 some libraries are preloaded. GDB will always poll the
1991 library list. Avoids the "stopped by shared library event"
1992 notice on the GDB side. */
1997 /* In non-stop, we don't send a resume reply. Stop events
1998 will follow up using the normal notification
2003 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2009 write_enn (own_buf
);
2014 /* Run a new program. Return 1 if successful, 0 if failure. */
2016 handle_v_run (char *own_buf
)
2018 char *p
, *next_p
, **new_argv
;
2022 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2028 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2029 if (new_argv
== NULL
)
2031 write_enn (own_buf
);
2036 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2038 next_p
= strchr (p
, ';');
2040 next_p
= p
+ strlen (p
);
2042 if (i
== 0 && p
== next_p
)
2046 /* FIXME: Fail request if out of memory instead of dying. */
2047 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2048 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2049 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2058 if (new_argv
[0] == NULL
)
2060 /* GDB didn't specify a program to run. Use the program from the
2061 last run with the new argument list. */
2063 if (program_argv
== NULL
)
2065 write_enn (own_buf
);
2066 freeargv (new_argv
);
2070 new_argv
[0] = strdup (program_argv
[0]);
2071 if (new_argv
[0] == NULL
)
2073 write_enn (own_buf
);
2074 freeargv (new_argv
);
2079 /* Free the old argv and install the new one. */
2080 freeargv (program_argv
);
2081 program_argv
= new_argv
;
2083 start_inferior (program_argv
);
2084 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2086 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2088 /* In non-stop, sending a resume reply doesn't set the general
2089 thread, but GDB assumes a vRun sets it (this is so GDB can
2090 query which is the main thread of the new inferior. */
2092 general_thread
= last_ptid
;
2098 write_enn (own_buf
);
2103 /* Kill process. Return 1 if successful, 0 if failure. */
2105 handle_v_kill (char *own_buf
)
2108 char *p
= &own_buf
[6];
2110 pid
= strtol (p
, NULL
, 16);
2113 if (pid
!= 0 && kill_inferior (pid
) == 0)
2115 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2116 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2117 last_ptid
= pid_to_ptid (pid
);
2118 discard_queued_stop_replies (pid
);
2124 write_enn (own_buf
);
2129 /* Handle all of the extended 'v' packets. */
2131 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2133 if (!disable_packet_vCont
)
2135 if (strncmp (own_buf
, "vCont;", 6) == 0)
2137 require_running (own_buf
);
2138 handle_v_cont (own_buf
);
2142 if (strncmp (own_buf
, "vCont?", 6) == 0)
2144 strcpy (own_buf
, "vCont;c;C;s;S;t");
2149 if (strncmp (own_buf
, "vFile:", 6) == 0
2150 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2153 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2155 if ((!extended_protocol
|| !multi_process
) && target_running ())
2157 fprintf (stderr
, "Already debugging a process\n");
2158 write_enn (own_buf
);
2161 handle_v_attach (own_buf
);
2165 if (strncmp (own_buf
, "vRun;", 5) == 0)
2167 if ((!extended_protocol
|| !multi_process
) && target_running ())
2169 fprintf (stderr
, "Already debugging a process\n");
2170 write_enn (own_buf
);
2173 handle_v_run (own_buf
);
2177 if (strncmp (own_buf
, "vKill;", 6) == 0)
2179 if (!target_running ())
2181 fprintf (stderr
, "No process to kill\n");
2182 write_enn (own_buf
);
2185 handle_v_kill (own_buf
);
2189 if (handle_notif_ack (own_buf
, packet_len
))
2192 /* Otherwise we didn't know what packet it was. Say we didn't
2198 /* Resume inferior and wait for another event. In non-stop mode,
2199 don't really wait here, but return immediatelly to the event
2202 myresume (char *own_buf
, int step
, int sig
)
2204 struct thread_resume resume_info
[2];
2206 int valid_cont_thread
;
2208 set_desired_inferior (0);
2210 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2211 && !ptid_equal (cont_thread
, minus_one_ptid
));
2213 if (step
|| sig
|| valid_cont_thread
)
2215 resume_info
[0].thread
= current_ptid
;
2217 resume_info
[0].kind
= resume_step
;
2219 resume_info
[0].kind
= resume_continue
;
2220 resume_info
[0].sig
= sig
;
2224 if (!valid_cont_thread
)
2226 resume_info
[n
].thread
= minus_one_ptid
;
2227 resume_info
[n
].kind
= resume_continue
;
2228 resume_info
[n
].sig
= 0;
2235 (*the_target
->resume
) (resume_info
, n
);
2241 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2243 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2244 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2246 current_inferior
->last_resume_kind
= resume_stop
;
2247 current_inferior
->last_status
= last_status
;
2250 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2251 disable_async_io ();
2253 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2254 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2255 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2259 /* Callback for for_each_inferior. Make a new stop reply for each
2263 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2265 struct thread_info
*thread
= (struct thread_info
*) entry
;
2267 /* For now, assume targets that don't have this callback also don't
2268 manage the thread's last_status field. */
2269 if (the_target
->thread_stopped
== NULL
)
2271 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2273 new_notif
->ptid
= entry
->id
;
2274 new_notif
->status
= thread
->last_status
;
2275 /* Pass the last stop reply back to GDB, but don't notify
2277 notif_event_enque (¬if_stop
,
2278 (struct notif_event
*) new_notif
);
2282 if (thread_stopped (thread
))
2286 "Reporting thread %s as already stopped with %s\n",
2287 target_pid_to_str (entry
->id
),
2288 target_waitstatus_to_string (&thread
->last_status
));
2290 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2292 /* Pass the last stop reply back to GDB, but don't notify
2294 queue_stop_reply (entry
->id
, &thread
->last_status
);
2301 /* Set this inferior threads's state as "want-stopped". We won't
2302 resume this thread until the client gives us another action for
2306 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2308 struct thread_info
*thread
= (struct thread_info
*) entry
;
2310 thread
->last_resume_kind
= resume_stop
;
2312 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2314 /* Most threads are stopped implicitly (all-stop); tag that with
2316 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2317 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2321 /* Set all threads' states as "want-stopped". */
2324 gdb_wants_all_threads_stopped (void)
2326 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2329 /* Clear the gdb_detached flag of every process. */
2332 gdb_reattached_process (struct inferior_list_entry
*entry
)
2334 struct process_info
*process
= (struct process_info
*) entry
;
2336 process
->gdb_detached
= 0;
2339 /* Status handler for the '?' packet. */
2342 handle_status (char *own_buf
)
2344 /* GDB is connected, don't forward events to the target anymore. */
2345 for_each_inferior (&all_processes
, gdb_reattached_process
);
2347 /* In non-stop mode, we must send a stop reply for each stopped
2348 thread. In all-stop mode, just send one for the first stopped
2353 discard_queued_stop_replies (-1);
2354 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2356 /* The first is sent immediatly. OK is sent if there is no
2357 stopped thread, which is the same handling of the vStopped
2358 packet (by design). */
2359 notif_write_event (¬if_stop
, own_buf
);
2364 stabilize_threads ();
2365 gdb_wants_all_threads_stopped ();
2367 if (all_threads
.head
)
2369 struct target_waitstatus status
;
2371 status
.kind
= TARGET_WAITKIND_STOPPED
;
2372 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2373 prepare_resume_reply (own_buf
,
2374 all_threads
.head
->id
, &status
);
2377 strcpy (own_buf
, "W00");
2382 gdbserver_version (void)
2384 printf ("GNU gdbserver %s%s\n"
2385 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
2386 "gdbserver is free software, covered by the "
2387 "GNU General Public License.\n"
2388 "This gdbserver was configured as \"%s\"\n",
2389 PKGVERSION
, version
, host_name
);
2393 gdbserver_usage (FILE *stream
)
2395 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2396 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2397 "\tgdbserver [OPTIONS] --multi COMM\n"
2399 "COMM may either be a tty device (for serial debugging), or \n"
2400 "HOST:PORT to listen for a TCP connection.\n"
2403 " --debug Enable general debugging output.\n"
2404 " --remote-debug Enable remote protocol debugging output.\n"
2405 " --version Display version information and exit.\n"
2406 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2407 " --once Exit after the first connection has "
2409 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2410 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2414 gdbserver_show_disableable (FILE *stream
)
2416 fprintf (stream
, "Disableable packets:\n"
2417 " vCont \tAll vCont packets\n"
2418 " qC \tQuerying the current thread\n"
2419 " qfThreadInfo\tThread listing\n"
2420 " Tthread \tPassing the thread specifier in the "
2421 "T stop reply packet\n"
2422 " threads \tAll of the above\n");
2426 #undef require_running
2427 #define require_running(BUF) \
2428 if (!target_running ()) \
2435 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2437 int pid
= * (int *) args
;
2439 if (ptid_get_pid (entry
->id
) == pid
)
2446 kill_inferior_callback (struct inferior_list_entry
*entry
)
2448 struct process_info
*process
= (struct process_info
*) entry
;
2449 int pid
= ptid_get_pid (process
->head
.id
);
2451 kill_inferior (pid
);
2452 discard_queued_stop_replies (pid
);
2455 /* Callback for for_each_inferior to detach or kill the inferior,
2456 depending on whether we attached to it or not.
2457 We inform the user whether we're detaching or killing the process
2458 as this is only called when gdbserver is about to exit. */
2461 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2463 struct process_info
*process
= (struct process_info
*) entry
;
2464 int pid
= ptid_get_pid (process
->head
.id
);
2466 if (process
->attached
)
2467 detach_inferior (pid
);
2469 kill_inferior (pid
);
2471 discard_queued_stop_replies (pid
);
2474 /* for_each_inferior callback for detach_or_kill_for_exit to print
2475 the pids of started inferiors. */
2478 print_started_pid (struct inferior_list_entry
*entry
)
2480 struct process_info
*process
= (struct process_info
*) entry
;
2482 if (! process
->attached
)
2484 int pid
= ptid_get_pid (process
->head
.id
);
2485 fprintf (stderr
, " %d", pid
);
2489 /* for_each_inferior callback for detach_or_kill_for_exit to print
2490 the pids of attached inferiors. */
2493 print_attached_pid (struct inferior_list_entry
*entry
)
2495 struct process_info
*process
= (struct process_info
*) entry
;
2497 if (process
->attached
)
2499 int pid
= ptid_get_pid (process
->head
.id
);
2500 fprintf (stderr
, " %d", pid
);
2504 /* Call this when exiting gdbserver with possible inferiors that need
2505 to be killed or detached from. */
2508 detach_or_kill_for_exit (void)
2510 /* First print a list of the inferiors we will be killing/detaching.
2511 This is to assist the user, for example, in case the inferior unexpectedly
2512 dies after we exit: did we screw up or did the inferior exit on its own?
2513 Having this info will save some head-scratching. */
2515 if (have_started_inferiors_p ())
2517 fprintf (stderr
, "Killing process(es):");
2518 for_each_inferior (&all_processes
, print_started_pid
);
2519 fprintf (stderr
, "\n");
2521 if (have_attached_inferiors_p ())
2523 fprintf (stderr
, "Detaching process(es):");
2524 for_each_inferior (&all_processes
, print_attached_pid
);
2525 fprintf (stderr
, "\n");
2528 /* Now we can kill or detach the inferiors. */
2530 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2534 main (int argc
, char *argv
[])
2538 char *arg_end
, *port
;
2539 char **next_arg
= &argv
[1];
2540 volatile int multi_mode
= 0;
2541 volatile int attach
= 0;
2544 while (*next_arg
!= NULL
&& **next_arg
== '-')
2546 if (strcmp (*next_arg
, "--version") == 0)
2548 gdbserver_version ();
2551 else if (strcmp (*next_arg
, "--help") == 0)
2553 gdbserver_usage (stdout
);
2556 else if (strcmp (*next_arg
, "--attach") == 0)
2558 else if (strcmp (*next_arg
, "--multi") == 0)
2560 else if (strcmp (*next_arg
, "--wrapper") == 0)
2564 wrapper_argv
= next_arg
;
2565 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2568 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2570 gdbserver_usage (stderr
);
2574 /* Consume the "--". */
2577 else if (strcmp (*next_arg
, "--debug") == 0)
2579 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2581 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2583 gdbserver_show_disableable (stdout
);
2586 else if (strncmp (*next_arg
,
2587 "--disable-packet=",
2588 sizeof ("--disable-packet=") - 1) == 0)
2590 char *packets
, *tok
;
2592 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2593 for (tok
= strtok (packets
, ",");
2595 tok
= strtok (NULL
, ","))
2597 if (strcmp ("vCont", tok
) == 0)
2598 disable_packet_vCont
= 1;
2599 else if (strcmp ("Tthread", tok
) == 0)
2600 disable_packet_Tthread
= 1;
2601 else if (strcmp ("qC", tok
) == 0)
2602 disable_packet_qC
= 1;
2603 else if (strcmp ("qfThreadInfo", tok
) == 0)
2604 disable_packet_qfThreadInfo
= 1;
2605 else if (strcmp ("threads", tok
) == 0)
2607 disable_packet_vCont
= 1;
2608 disable_packet_Tthread
= 1;
2609 disable_packet_qC
= 1;
2610 disable_packet_qfThreadInfo
= 1;
2614 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2616 gdbserver_show_disableable (stderr
);
2621 else if (strcmp (*next_arg
, "-") == 0)
2623 /* "-" specifies a stdio connection and is a form of port
2625 *next_arg
= STDIO_CONNECTION_NAME
;
2628 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2629 disable_randomization
= 1;
2630 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2631 disable_randomization
= 0;
2632 else if (strcmp (*next_arg
, "--once") == 0)
2636 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2644 if (setjmp (toplevel
))
2646 fprintf (stderr
, "Exiting\n");
2652 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2654 gdbserver_usage (stderr
);
2658 /* We need to know whether the remote connection is stdio before
2659 starting the inferior. Inferiors created in this scenario have
2660 stdin,stdout redirected. So do this here before we call
2662 remote_prepare (port
);
2667 /* --attach used to come after PORT, so allow it there for
2669 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2676 && (*next_arg
== NULL
2677 || (*next_arg
)[0] == '\0'
2678 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2680 || next_arg
[1] != NULL
))
2685 gdbserver_usage (stderr
);
2689 initialize_async_io ();
2691 initialize_event_loop ();
2692 if (target_supports_tracepoints ())
2693 initialize_tracepoint ();
2695 own_buf
= xmalloc (PBUFSIZ
+ 1);
2696 mem_buf
= xmalloc (PBUFSIZ
);
2698 if (pid
== 0 && *next_arg
!= NULL
)
2702 n
= argc
- (next_arg
- argv
);
2703 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2704 for (i
= 0; i
< n
; i
++)
2705 program_argv
[i
] = xstrdup (next_arg
[i
]);
2706 program_argv
[i
] = NULL
;
2708 /* Wait till we are at first instruction in program. */
2709 start_inferior (program_argv
);
2711 /* We are now (hopefully) stopped at the first instruction of
2712 the target process. This assumes that the target process was
2713 successfully created. */
2717 if (attach_inferior (pid
) == -1)
2718 error ("Attaching not supported on this target");
2720 /* Otherwise succeeded. */
2724 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2725 last_status
.value
.integer
= 0;
2726 last_ptid
= minus_one_ptid
;
2729 initialize_notif ();
2731 /* Don't report shared library events on the initial connection,
2732 even if some libraries are preloaded. Avoids the "stopped by
2733 shared library event" notice on gdb side. */
2736 if (setjmp (toplevel
))
2738 /* If something fails and longjmps while detaching or killing
2739 inferiors, we'd end up here again, stuck in an infinite loop
2740 trap. Be sure that if that happens, we exit immediately
2742 if (setjmp (toplevel
) == 0)
2743 detach_or_kill_for_exit ();
2745 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2749 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2750 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2755 if (!was_running
&& !multi_mode
)
2757 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2765 /* Be sure we're out of tfind mode. */
2766 current_traceframe
= -1;
2770 if (setjmp (toplevel
) != 0)
2772 /* An error occurred. */
2773 if (response_needed
)
2775 write_enn (own_buf
);
2780 /* Wait for events. This will return when all event sources are
2781 removed from the event loop. */
2782 start_event_loop ();
2784 /* If an exit was requested (using the "monitor exit" command),
2785 terminate now. The only other way to get here is for
2786 getpkt to fail; close the connection and reopen it at the
2789 if (exit_requested
|| run_once
)
2791 /* If something fails and longjmps while detaching or
2792 killing inferiors, we'd end up here again, stuck in an
2793 infinite loop trap. Be sure that if that happens, we
2794 exit immediately instead. */
2795 if (setjmp (toplevel
) == 0)
2797 detach_or_kill_for_exit ();
2802 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2808 "Remote side has terminated connection. "
2809 "GDBserver will reopen the connection.\n");
2813 if (disconnected_tracing
)
2815 /* Try to enable non-stop/async mode, so we we can both
2816 wait for an async socket accept, and handle async
2817 target events simultaneously. There's also no point
2818 either in having the target always stop all threads,
2819 when we're going to pass signals down without
2823 if (start_non_stop (1))
2826 /* Detaching implicitly resumes all threads; simply
2827 disconnecting does not. */
2833 "Disconnected tracing disabled; stopping trace run.\n");
2840 /* Process options coming from Z packets for *point at address
2841 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2842 to point to the first char after the last processed option. */
2845 process_point_options (CORE_ADDR point_addr
, char **packet
)
2847 char *dataptr
= *packet
;
2850 /* Check if data has the correct format. */
2851 if (*dataptr
!= ';')
2858 if (*dataptr
== ';')
2861 if (*dataptr
== 'X')
2863 /* Conditional expression. */
2865 fprintf (stderr
, "Found breakpoint condition.\n");
2866 add_breakpoint_condition (point_addr
, &dataptr
);
2868 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
2870 dataptr
+= strlen ("cmds:");
2872 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
2873 persist
= (*dataptr
== '1');
2875 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
2879 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2881 /* Skip tokens until we find one that we recognize. */
2882 while (*dataptr
&& *dataptr
!= ';')
2889 /* Event loop callback that handles a serial event. The first byte in
2890 the serial buffer gets us here. We expect characters to arrive at
2891 a brisk pace, so we read the rest of the packet with a blocking
2895 process_serial_event (void)
2906 int new_packet_len
= -1;
2908 /* Used to decide when gdbserver should exit in
2909 multi-mode/remote. */
2910 static int have_ran
= 0;
2913 have_ran
= target_running ();
2915 disable_async_io ();
2917 response_needed
= 0;
2918 packet_len
= getpkt (own_buf
);
2919 if (packet_len
<= 0)
2922 /* Force an event loop break. */
2925 response_needed
= 1;
2932 handle_query (own_buf
, packet_len
, &new_packet_len
);
2935 handle_general_set (own_buf
);
2938 require_running (own_buf
);
2943 pid
= strtol (&own_buf
[i
], NULL
, 16);
2946 pid
= ptid_get_pid (current_ptid
);
2948 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
2950 struct thread_resume resume_info
;
2951 struct process_info
*process
= find_process_pid (pid
);
2953 if (process
== NULL
)
2955 write_enn (own_buf
);
2959 if (tracing
&& disconnected_tracing
)
2961 "Disconnected tracing in effect, "
2962 "leaving gdbserver attached to the process\n");
2964 if (any_persistent_commands ())
2966 "Persistent commands are present, "
2967 "leaving gdbserver attached to the process\n");
2969 /* Make sure we're in non-stop/async mode, so we we can both
2970 wait for an async socket accept, and handle async target
2971 events simultaneously. There's also no point either in
2972 having the target stop all threads, when we're going to
2973 pass signals down without informing GDB. */
2977 fprintf (stderr
, "Forcing non-stop mode\n");
2983 process
->gdb_detached
= 1;
2985 /* Detaching implicitly resumes all threads. */
2986 resume_info
.thread
= minus_one_ptid
;
2987 resume_info
.kind
= resume_continue
;
2988 resume_info
.sig
= 0;
2989 (*the_target
->resume
) (&resume_info
, 1);
2992 break; /* from switch/case */
2995 fprintf (stderr
, "Detaching from process %d\n", pid
);
2997 if (detach_inferior (pid
) != 0)
2998 write_enn (own_buf
);
3001 discard_queued_stop_replies (pid
);
3004 if (extended_protocol
)
3006 /* Treat this like a normal program exit. */
3007 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3008 last_status
.value
.integer
= 0;
3009 last_ptid
= pid_to_ptid (pid
);
3011 current_inferior
= NULL
;
3018 /* If we are attached, then we can exit. Otherwise, we
3019 need to hang around doing nothing, until the child is
3021 join_inferior (pid
);
3027 extended_protocol
= 1;
3031 handle_status (own_buf
);
3034 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3036 ptid_t gdb_id
, thread_id
;
3039 require_running (own_buf
);
3041 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3043 pid
= ptid_get_pid (gdb_id
);
3045 if (ptid_equal (gdb_id
, null_ptid
)
3046 || ptid_equal (gdb_id
, minus_one_ptid
))
3047 thread_id
= null_ptid
;
3049 && ptid_equal (pid_to_ptid (pid
),
3052 struct thread_info
*thread
=
3053 (struct thread_info
*) find_inferior (&all_threads
,
3058 write_enn (own_buf
);
3062 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3066 thread_id
= gdb_id_to_thread_id (gdb_id
);
3067 if (ptid_equal (thread_id
, null_ptid
))
3069 write_enn (own_buf
);
3074 if (own_buf
[1] == 'g')
3076 if (ptid_equal (thread_id
, null_ptid
))
3078 /* GDB is telling us to choose any thread. Check if
3079 the currently selected thread is still valid. If
3080 it is not, select the first available. */
3081 struct thread_info
*thread
=
3082 (struct thread_info
*) find_inferior_id (&all_threads
,
3085 thread_id
= all_threads
.head
->id
;
3088 general_thread
= thread_id
;
3089 set_desired_inferior (1);
3091 else if (own_buf
[1] == 'c')
3092 cont_thread
= thread_id
;
3098 /* Silently ignore it so that gdb can extend the protocol
3099 without compatibility headaches. */
3104 require_running (own_buf
);
3105 if (current_traceframe
>= 0)
3107 struct regcache
*regcache
= new_register_cache ();
3109 if (fetch_traceframe_registers (current_traceframe
,
3111 registers_to_string (regcache
, own_buf
);
3113 write_enn (own_buf
);
3114 free_register_cache (regcache
);
3118 struct regcache
*regcache
;
3120 set_desired_inferior (1);
3121 regcache
= get_thread_regcache (current_inferior
, 1);
3122 registers_to_string (regcache
, own_buf
);
3126 require_running (own_buf
);
3127 if (current_traceframe
>= 0)
3128 write_enn (own_buf
);
3131 struct regcache
*regcache
;
3133 set_desired_inferior (1);
3134 regcache
= get_thread_regcache (current_inferior
, 1);
3135 registers_from_string (regcache
, &own_buf
[1]);
3140 require_running (own_buf
);
3141 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3142 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3144 write_enn (own_buf
);
3146 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3149 require_running (own_buf
);
3150 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3151 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3154 write_enn (own_buf
);
3157 require_running (own_buf
);
3158 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3159 &mem_addr
, &len
, &mem_buf
) < 0
3160 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3161 write_enn (own_buf
);
3166 require_running (own_buf
);
3167 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3168 if (gdb_signal_to_host_p (sig
))
3169 signal
= gdb_signal_to_host (sig
);
3172 myresume (own_buf
, 0, signal
);
3175 require_running (own_buf
);
3176 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3177 if (gdb_signal_to_host_p (sig
))
3178 signal
= gdb_signal_to_host (sig
);
3181 myresume (own_buf
, 1, signal
);
3184 require_running (own_buf
);
3186 myresume (own_buf
, 0, signal
);
3189 require_running (own_buf
);
3191 myresume (own_buf
, 1, signal
);
3193 case 'Z': /* insert_ ... */
3195 case 'z': /* remove_ ... */
3200 char type
= own_buf
[1];
3202 const int insert
= ch
== 'Z';
3203 char *p
= &own_buf
[3];
3205 p
= unpack_varlen_hex (p
, &addr
);
3206 len
= strtol (p
+ 1, &dataptr
, 16);
3208 /* Default to unrecognized/unsupported. */
3212 case '0': /* software-breakpoint */
3213 case '1': /* hardware-breakpoint */
3214 case '2': /* write watchpoint */
3215 case '3': /* read watchpoint */
3216 case '4': /* access watchpoint */
3217 require_running (own_buf
);
3218 if (insert
&& the_target
->insert_point
!= NULL
)
3220 /* Insert the breakpoint. If it is already inserted, nothing
3222 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3224 /* GDB may have sent us a list of *point parameters to be
3225 evaluated on the target's side. Read such list here. If we
3226 already have a list of parameters, GDB is telling us to drop
3227 that list and use this one instead. */
3228 if (!res
&& (type
== '0' || type
== '1'))
3230 /* Remove previous conditions. */
3231 clear_gdb_breakpoint_conditions (addr
);
3232 process_point_options (addr
, &dataptr
);
3235 else if (!insert
&& the_target
->remove_point
!= NULL
)
3236 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3248 write_enn (own_buf
);
3252 response_needed
= 0;
3253 if (!target_running ())
3254 /* The packet we received doesn't make sense - but we can't
3255 reply to it, either. */
3258 fprintf (stderr
, "Killing all inferiors\n");
3259 for_each_inferior (&all_processes
, kill_inferior_callback
);
3261 /* When using the extended protocol, we wait with no program
3262 running. The traditional protocol will exit instead. */
3263 if (extended_protocol
)
3265 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3266 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3274 ptid_t gdb_id
, thread_id
;
3276 require_running (own_buf
);
3278 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3279 thread_id
= gdb_id_to_thread_id (gdb_id
);
3280 if (ptid_equal (thread_id
, null_ptid
))
3282 write_enn (own_buf
);
3286 if (mythread_alive (thread_id
))
3289 write_enn (own_buf
);
3293 response_needed
= 0;
3295 /* Restarting the inferior is only supported in the extended
3297 if (extended_protocol
)
3299 if (target_running ())
3300 for_each_inferior (&all_processes
,
3301 kill_inferior_callback
);
3302 fprintf (stderr
, "GDBserver restarting\n");
3304 /* Wait till we are at 1st instruction in prog. */
3305 if (program_argv
!= NULL
)
3306 start_inferior (program_argv
);
3309 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3310 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3316 /* It is a request we don't understand. Respond with an
3317 empty packet so that gdb knows that we don't support this
3323 /* Extended (long) request. */
3324 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3328 /* It is a request we don't understand. Respond with an empty
3329 packet so that gdb knows that we don't support this
3335 if (new_packet_len
!= -1)
3336 putpkt_binary (own_buf
, new_packet_len
);
3340 response_needed
= 0;
3342 if (!extended_protocol
&& have_ran
&& !target_running ())
3344 /* In non-stop, defer exiting until GDB had a chance to query
3345 the whole vStopped list (until it gets an OK). */
3346 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
3348 fprintf (stderr
, "GDBserver exiting\n");
3360 /* Event-loop callback for serial events. */
3363 handle_serial_event (int err
, gdb_client_data client_data
)
3366 fprintf (stderr
, "handling possible serial event\n");
3368 /* Really handle it. */
3369 if (process_serial_event () < 0)
3372 /* Be sure to not change the selected inferior behind GDB's back.
3373 Important in the non-stop mode asynchronous protocol. */
3374 set_desired_inferior (1);
3379 /* Event-loop callback for target events. */
3382 handle_target_event (int err
, gdb_client_data client_data
)
3385 fprintf (stderr
, "handling possible target event\n");
3387 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3390 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3392 int pid
= ptid_get_pid (last_ptid
);
3393 struct process_info
*process
= find_process_pid (pid
);
3394 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3396 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3397 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3399 mark_breakpoints_out (process
);
3400 mourn_inferior (process
);
3404 /* We're reporting this thread as stopped. Update its
3405 "want-stopped" state to what the client wants, until it
3406 gets a new resume action. */
3407 current_inferior
->last_resume_kind
= resume_stop
;
3408 current_inferior
->last_status
= last_status
;
3413 if (!target_running ())
3415 /* The last process exited. We're done. */
3419 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3421 /* A thread stopped with a signal, but gdb isn't
3422 connected to handle it. Pass it down to the
3423 inferior, as if it wasn't being traced. */
3424 struct thread_resume resume_info
;
3428 "GDB not connected; forwarding event %d for [%s]\n",
3429 (int) last_status
.kind
,
3430 target_pid_to_str (last_ptid
));
3432 resume_info
.thread
= last_ptid
;
3433 resume_info
.kind
= resume_continue
;
3434 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3435 (*the_target
->resume
) (&resume_info
, 1);
3437 else if (debug_threads
)
3438 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3439 (int) last_status
.kind
,
3440 target_pid_to_str (last_ptid
));
3444 struct vstop_notif
*vstop_notif
3445 = xmalloc (sizeof (struct vstop_notif
));
3447 vstop_notif
->status
= last_status
;
3448 vstop_notif
->ptid
= last_ptid
;
3449 /* Push Stop notification. */
3450 notif_push (¬if_stop
,
3451 (struct notif_event
*) vstop_notif
);
3455 /* Be sure to not change the selected inferior behind GDB's back.
3456 Important in the non-stop mode asynchronous protocol. */
3457 set_desired_inferior (1);