1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbthread.h"
34 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
35 `vCont'. Note the multi-process extensions made `vCont' a
36 requirement, so `Hc pPID.TID' is pretty much undefined. So
37 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
38 resuming all threads of the process (again, `Hc' isn't used for
39 multi-process), or a specific thread ptid_t.
41 We also set this when handling a single-thread `vCont' resume, as
42 some places in the backends check it to know when (and for which
43 thread) single-thread scheduler-locking is in effect. */
46 /* The thread set with an `Hg' packet. */
47 ptid_t general_thread
;
51 static int extended_protocol
;
52 static int response_needed
;
53 static int exit_requested
;
55 /* --once: Exit after the first connection has closed. */
61 /* Whether we should attempt to disable the operating system's address
62 space randomization feature before starting an inferior. */
63 int disable_randomization
= 1;
65 static char **program_argv
, **wrapper_argv
;
67 /* Enable miscellaneous debugging output. The name is historical - it
68 was originally used to debug LinuxThreads support. */
71 /* Enable debugging of h/w breakpoint/watchpoint support. */
74 int pass_signals
[GDB_SIGNAL_LAST
];
75 int program_signals
[GDB_SIGNAL_LAST
];
76 int program_signals_p
;
80 const char *gdbserver_xmltarget
;
82 /* The PID of the originally created or attached inferior. Used to
83 send signals to the process when GDB sends us an asynchronous interrupt
84 (user hitting Control-C in the client), and to wait for the child to exit
85 when no longer debugging it. */
87 unsigned long signal_pid
;
90 /* A file descriptor for the controlling terminal. */
93 /* TERMINAL_FD's original foreground group. */
94 pid_t old_foreground_pgrp
;
96 /* Hand back terminal ownership to the original foreground group. */
99 restore_old_foreground_pgrp (void)
101 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
105 /* Set if you want to disable optional thread related packets support
106 in gdbserver, for the sake of testing GDB against stubs that don't
108 int disable_packet_vCont
;
109 int disable_packet_Tthread
;
110 int disable_packet_qC
;
111 int disable_packet_qfThreadInfo
;
113 /* Last status reported to GDB. */
114 static struct target_waitstatus last_status
;
115 static ptid_t last_ptid
;
117 static char *own_buf
;
118 static unsigned char *mem_buf
;
120 /* Structure holding information relative to a single stop reply. We
121 keep a queue of these (really a singly-linked list) to push to GDB
125 /* Pointer to next in list. */
126 struct vstop_notif
*next
;
128 /* Thread or process that got the event. */
132 struct target_waitstatus status
;
135 /* The pending stop replies list head. */
136 static struct vstop_notif
*notif_queue
= NULL
;
138 /* Put a stop reply to the stop reply queue. */
141 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
143 struct vstop_notif
*new_notif
;
145 new_notif
= xmalloc (sizeof (*new_notif
));
146 new_notif
->next
= NULL
;
147 new_notif
->ptid
= ptid
;
148 new_notif
->status
= *status
;
152 struct vstop_notif
*tail
;
153 for (tail
= notif_queue
;
157 tail
->next
= new_notif
;
160 notif_queue
= new_notif
;
165 struct vstop_notif
*n
;
167 for (n
= notif_queue
; n
; n
= n
->next
)
170 fprintf (stderr
, "pending stop replies: %d\n", i
);
174 /* Place an event in the stop reply queue, and push a notification if
175 we aren't sending one yet. */
178 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
180 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
182 queue_stop_reply (ptid
, status
);
184 /* If this is the first stop reply in the queue, then inform GDB
185 about it, by sending a Stop notification. */
186 if (notif_queue
->next
== NULL
)
191 prepare_resume_reply (p
,
192 notif_queue
->ptid
, ¬if_queue
->status
);
193 putpkt_notif (own_buf
);
197 /* Get rid of the currently pending stop replies for PID. If PID is
198 -1, then apply to all processes. */
201 discard_queued_stop_replies (int pid
)
203 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
205 for (reply
= notif_queue
; reply
; reply
= next
)
210 || ptid_get_pid (reply
->ptid
) == pid
)
212 if (reply
== notif_queue
)
215 prev
->next
= reply
->next
;
224 /* If there are more stop replies to push, push one now. */
227 send_next_stop_reply (char *own_buf
)
230 prepare_resume_reply (own_buf
,
232 ¬if_queue
->status
);
238 target_running (void)
240 return all_threads
.head
!= NULL
;
244 start_inferior (char **argv
)
246 char **new_argv
= argv
;
248 if (wrapper_argv
!= NULL
)
252 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
254 for (i
= 0; argv
[i
] != NULL
; i
++)
256 new_argv
= alloca (sizeof (char *) * count
);
258 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
259 new_argv
[count
++] = wrapper_argv
[i
];
260 for (i
= 0; argv
[i
] != NULL
; i
++)
261 new_argv
[count
++] = argv
[i
];
262 new_argv
[count
] = NULL
;
268 for (i
= 0; new_argv
[i
]; ++i
)
269 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
274 signal (SIGTTOU
, SIG_DFL
);
275 signal (SIGTTIN
, SIG_DFL
);
278 /* Clear this so the backend doesn't get confused, thinking
279 CONT_THREAD died, and it needs to resume all threads. */
280 cont_thread
= null_ptid
;
282 signal_pid
= create_inferior (new_argv
[0], new_argv
);
284 /* FIXME: we don't actually know at this point that the create
285 actually succeeded. We won't know that until we wait. */
286 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
291 signal (SIGTTOU
, SIG_IGN
);
292 signal (SIGTTIN
, SIG_IGN
);
293 terminal_fd
= fileno (stderr
);
294 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
295 tcsetpgrp (terminal_fd
, signal_pid
);
296 atexit (restore_old_foreground_pgrp
);
299 if (wrapper_argv
!= NULL
)
301 struct thread_resume resume_info
;
303 resume_info
.thread
= pid_to_ptid (signal_pid
);
304 resume_info
.kind
= resume_continue
;
307 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
309 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
314 (*the_target
->resume
) (&resume_info
, 1);
316 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
317 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
320 current_inferior
->last_resume_kind
= resume_stop
;
321 current_inferior
->last_status
= last_status
;
323 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
328 /* Wait till we are at 1st instruction in program, return new pid
329 (assuming success). */
330 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
332 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
333 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
335 current_inferior
->last_resume_kind
= resume_stop
;
336 current_inferior
->last_status
= last_status
;
343 attach_inferior (int pid
)
345 /* myattach should return -1 if attaching is unsupported,
346 0 if it succeeded, and call error() otherwise. */
348 if (myattach (pid
) != 0)
351 fprintf (stderr
, "Attached; pid = %d\n", pid
);
354 /* FIXME - It may be that we should get the SIGNAL_PID from the
355 attach function, so that it can be the main thread instead of
356 whichever we were told to attach to. */
359 /* Clear this so the backend doesn't get confused, thinking
360 CONT_THREAD died, and it needs to resume all threads. */
361 cont_thread
= null_ptid
;
365 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
367 /* GDB knows to ignore the first SIGSTOP after attaching to a running
368 process using the "attach" command, but this is different; it's
369 just using "target remote". Pretend it's just starting up. */
370 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
371 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
372 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
374 current_inferior
->last_resume_kind
= resume_stop
;
375 current_inferior
->last_status
= last_status
;
381 extern int remote_debug
;
383 /* Decode a qXfer read request. Return 0 if everything looks OK,
387 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
389 /* After the read marker and annex, qXfer looks like a
390 traditional 'm' packet. */
391 decode_m_packet (buf
, ofs
, len
);
397 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
399 /* Extract and NUL-terminate the object. */
401 while (*buf
&& *buf
!= ':')
407 /* Extract and NUL-terminate the read/write action. */
409 while (*buf
&& *buf
!= ':')
415 /* Extract and NUL-terminate the annex. */
417 while (*buf
&& *buf
!= ':')
427 /* Write the response to a successful qXfer read. Returns the
428 length of the (binary) data stored in BUF, corresponding
429 to as much of DATA/LEN as we could fit. IS_MORE controls
430 the first character of the response. */
432 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
441 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
445 /* Handle all of the extended 'Q' packets. */
448 handle_general_set (char *own_buf
)
450 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
452 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
453 const char *p
= own_buf
+ strlen ("QPassSignals:");
456 p
= decode_address_to_semicolon (&cursig
, p
);
457 for (i
= 0; i
< numsigs
; i
++)
463 /* Keep looping, to clear the remaining signals. */
466 p
= decode_address_to_semicolon (&cursig
, p
);
471 strcpy (own_buf
, "OK");
475 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
477 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
478 const char *p
= own_buf
+ strlen ("QProgramSignals:");
481 program_signals_p
= 1;
483 p
= decode_address_to_semicolon (&cursig
, p
);
484 for (i
= 0; i
< numsigs
; i
++)
488 program_signals
[i
] = 1;
490 /* Keep looping, to clear the remaining signals. */
493 p
= decode_address_to_semicolon (&cursig
, p
);
496 program_signals
[i
] = 0;
498 strcpy (own_buf
, "OK");
502 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
506 fprintf (stderr
, "[noack mode enabled]\n");
515 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
517 char *mode
= own_buf
+ 9;
521 if (strcmp (mode
, "0") == 0)
523 else if (strcmp (mode
, "1") == 0)
527 /* We don't know what this mode is, so complain to
529 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
535 req_str
= req
? "non-stop" : "all-stop";
536 if (start_non_stop (req
) != 0)
538 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
546 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
552 if (strncmp ("QDisableRandomization:", own_buf
,
553 strlen ("QDisableRandomization:")) == 0)
555 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
558 unpack_varlen_hex (packet
, &setting
);
559 disable_randomization
= setting
;
563 if (disable_randomization
)
564 fprintf (stderr
, "[address space randomization disabled]\n");
566 fprintf (stderr
, "[address space randomization enabled]\n");
573 if (target_supports_tracepoints ()
574 && handle_tracepoint_general_set (own_buf
))
577 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
579 char *mode
= own_buf
+ strlen ("QAgent:");
582 if (strcmp (mode
, "0") == 0)
584 else if (strcmp (mode
, "1") == 0)
588 /* We don't know what this value is, so complain to GDB. */
589 sprintf (own_buf
, "E.Unknown QAgent value");
593 /* Update the flag. */
596 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
601 /* Otherwise we didn't know what packet it was. Say we didn't
607 get_features_xml (const char *annex
)
609 /* gdbserver_xmltarget defines what to return when looking
610 for the "target.xml" file. Its contents can either be
611 verbatim XML code (prefixed with a '@') or else the name
612 of the actual XML file to be used in place of "target.xml".
614 This variable is set up from the auto-generated
615 init_registers_... routine for the current target. */
617 if (gdbserver_xmltarget
618 && strcmp (annex
, "target.xml") == 0)
620 if (*gdbserver_xmltarget
== '@')
621 return gdbserver_xmltarget
+ 1;
623 annex
= gdbserver_xmltarget
;
628 extern const char *const xml_builtin
[][2];
631 /* Look for the annex. */
632 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
633 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
636 if (xml_builtin
[i
][0] != NULL
)
637 return xml_builtin
[i
][1];
645 monitor_show_help (void)
647 monitor_output ("The following monitor commands are supported:\n");
648 monitor_output (" set debug <0|1>\n");
649 monitor_output (" Enable general debugging messages\n");
650 monitor_output (" set debug-hw-points <0|1>\n");
651 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
652 monitor_output (" set remote-debug <0|1>\n");
653 monitor_output (" Enable remote protocol debugging messages\n");
654 monitor_output (" exit\n");
655 monitor_output (" Quit GDBserver\n");
658 /* Read trace frame or inferior memory. Returns the number of bytes
659 actually read, zero when no further transfer is possible, and -1 on
660 error. Return of a positive value smaller than LEN does not
661 indicate there's no more to be read, only the end of the transfer.
662 E.g., when GDB reads memory from a traceframe, a first request may
663 be served from a memory block that does not cover the whole request
664 length. A following request gets the rest served from either
665 another block (of the same traceframe) or from the read-only
669 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
673 if (current_traceframe
>= 0)
676 ULONGEST length
= len
;
678 if (traceframe_read_mem (current_traceframe
,
679 memaddr
, myaddr
, len
, &nbytes
))
681 /* Data read from trace buffer, we're done. */
684 if (!in_readonly_region (memaddr
, length
))
686 /* Otherwise we have a valid readonly case, fall through. */
687 /* (assume no half-trace half-real blocks for now) */
690 res
= prepare_to_access_memory ();
693 res
= read_inferior_memory (memaddr
, myaddr
, len
);
694 done_accessing_memory ();
696 return res
== 0 ? len
: -1;
702 /* Write trace frame or inferior memory. Actually, writing to trace
703 frames is forbidden. */
706 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
708 if (current_traceframe
>= 0)
714 ret
= prepare_to_access_memory ();
717 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
718 done_accessing_memory ();
724 /* Subroutine of handle_search_memory to simplify it. */
727 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
728 gdb_byte
*pattern
, unsigned pattern_len
,
729 gdb_byte
*search_buf
,
730 unsigned chunk_size
, unsigned search_buf_size
,
731 CORE_ADDR
*found_addrp
)
733 /* Prime the search buffer. */
735 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
738 warning ("Unable to access target memory at 0x%lx, halting search.",
743 /* Perform the search.
745 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
746 When we've scanned N bytes we copy the trailing bytes to the start and
747 read in another N bytes. */
749 while (search_space_len
>= pattern_len
)
752 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
756 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
758 if (found_ptr
!= NULL
)
760 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
761 *found_addrp
= found_addr
;
765 /* Not found in this chunk, skip to next chunk. */
767 /* Don't let search_space_len wrap here, it's unsigned. */
768 if (search_space_len
>= chunk_size
)
769 search_space_len
-= chunk_size
;
771 search_space_len
= 0;
773 if (search_space_len
>= pattern_len
)
775 unsigned keep_len
= search_buf_size
- chunk_size
;
776 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
779 /* Copy the trailing part of the previous iteration to the front
780 of the buffer for the next iteration. */
781 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
783 nr_to_read
= (search_space_len
- keep_len
< chunk_size
784 ? search_space_len
- keep_len
787 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
788 nr_to_read
) != search_buf_size
)
790 warning ("Unable to access target memory "
791 "at 0x%lx, halting search.",
796 start_addr
+= chunk_size
;
805 /* Handle qSearch:memory packets. */
808 handle_search_memory (char *own_buf
, int packet_len
)
810 CORE_ADDR start_addr
;
811 CORE_ADDR search_space_len
;
813 unsigned int pattern_len
;
814 /* NOTE: also defined in find.c testcase. */
815 #define SEARCH_CHUNK_SIZE 16000
816 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
817 /* Buffer to hold memory contents for searching. */
818 gdb_byte
*search_buf
;
819 unsigned search_buf_size
;
821 CORE_ADDR found_addr
;
822 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
824 pattern
= malloc (packet_len
);
827 error ("Unable to allocate memory to perform the search");
828 strcpy (own_buf
, "E00");
831 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
832 packet_len
- cmd_name_len
,
833 &start_addr
, &search_space_len
,
834 pattern
, &pattern_len
) < 0)
837 error ("Error in parsing qSearch:memory packet");
838 strcpy (own_buf
, "E00");
842 search_buf_size
= chunk_size
+ pattern_len
- 1;
844 /* No point in trying to allocate a buffer larger than the search space. */
845 if (search_space_len
< search_buf_size
)
846 search_buf_size
= search_space_len
;
848 search_buf
= malloc (search_buf_size
);
849 if (search_buf
== NULL
)
852 error ("Unable to allocate memory to perform the search");
853 strcpy (own_buf
, "E00");
857 found
= handle_search_memory_1 (start_addr
, search_space_len
,
858 pattern
, pattern_len
,
859 search_buf
, chunk_size
, search_buf_size
,
863 sprintf (own_buf
, "1,%lx", (long) found_addr
);
865 strcpy (own_buf
, "0");
867 strcpy (own_buf
, "E00");
873 #define require_running(BUF) \
874 if (!target_running ()) \
880 /* Handle monitor commands not handled by target-specific handlers. */
883 handle_monitor_command (char *mon
, char *own_buf
)
885 if (strcmp (mon
, "set debug 1") == 0)
888 monitor_output ("Debug output enabled.\n");
890 else if (strcmp (mon
, "set debug 0") == 0)
893 monitor_output ("Debug output disabled.\n");
895 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
898 monitor_output ("H/W point debugging output enabled.\n");
900 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
903 monitor_output ("H/W point debugging output disabled.\n");
905 else if (strcmp (mon
, "set remote-debug 1") == 0)
908 monitor_output ("Protocol debug output enabled.\n");
910 else if (strcmp (mon
, "set remote-debug 0") == 0)
913 monitor_output ("Protocol debug output disabled.\n");
915 else if (strcmp (mon
, "help") == 0)
916 monitor_show_help ();
917 else if (strcmp (mon
, "exit") == 0)
921 monitor_output ("Unknown monitor command.\n\n");
922 monitor_show_help ();
927 /* Associates a callback with each supported qXfer'able object. */
931 /* The object this handler handles. */
934 /* Request that the target transfer up to LEN 8-bit bytes of the
935 target's OBJECT. The OFFSET, for a seekable object, specifies
936 the starting point. The ANNEX can be used to provide additional
937 data-specific information to the target.
939 Return the number of bytes actually transfered, zero when no
940 further transfer is possible, -1 on error, and -2 when the
941 transfer is not supported. Return of a positive value smaller
942 than LEN does not indicate the end of the object, only the end of
945 One, and only one, of readbuf or writebuf must be non-NULL. */
946 int (*xfer
) (const char *annex
,
947 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
948 ULONGEST offset
, LONGEST len
);
951 /* Handle qXfer:auxv:read. */
954 handle_qxfer_auxv (const char *annex
,
955 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
956 ULONGEST offset
, LONGEST len
)
958 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
961 if (annex
[0] != '\0' || !target_running ())
964 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
967 /* Handle qXfer:features:read. */
970 handle_qxfer_features (const char *annex
,
971 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
972 ULONGEST offset
, LONGEST len
)
974 const char *document
;
977 if (writebuf
!= NULL
)
980 if (!target_running ())
983 /* Grab the correct annex. */
984 document
= get_features_xml (annex
);
985 if (document
== NULL
)
988 total_len
= strlen (document
);
990 if (offset
> total_len
)
993 if (offset
+ len
> total_len
)
994 len
= total_len
- offset
;
996 memcpy (readbuf
, document
+ offset
, len
);
1000 /* Handle qXfer:libraries:read. */
1003 handle_qxfer_libraries (const char *annex
,
1004 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1005 ULONGEST offset
, LONGEST len
)
1007 unsigned int total_len
;
1009 struct inferior_list_entry
*dll_ptr
;
1011 if (writebuf
!= NULL
)
1014 if (annex
[0] != '\0' || !target_running ())
1017 /* Over-estimate the necessary memory. Assume that every character
1018 in the library name must be escaped. */
1020 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1021 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1023 document
= malloc (total_len
);
1024 if (document
== NULL
)
1027 strcpy (document
, "<library-list>\n");
1028 p
= document
+ strlen (document
);
1030 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1032 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1035 strcpy (p
, " <library name=\"");
1037 name
= xml_escape_text (dll
->name
);
1041 strcpy (p
, "\"><segment address=\"");
1043 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1045 strcpy (p
, "\"/></library>\n");
1049 strcpy (p
, "</library-list>\n");
1051 total_len
= strlen (document
);
1053 if (offset
> total_len
)
1059 if (offset
+ len
> total_len
)
1060 len
= total_len
- offset
;
1062 memcpy (readbuf
, document
+ offset
, len
);
1067 /* Handle qXfer:libraries-svr4:read. */
1070 handle_qxfer_libraries_svr4 (const char *annex
,
1071 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1072 ULONGEST offset
, LONGEST len
)
1074 if (writebuf
!= NULL
)
1077 if (annex
[0] != '\0' || !target_running ()
1078 || the_target
->qxfer_libraries_svr4
== NULL
)
1081 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1084 /* Handle qXfer:osadata:read. */
1087 handle_qxfer_osdata (const char *annex
,
1088 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1089 ULONGEST offset
, LONGEST len
)
1091 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1094 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1097 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1100 handle_qxfer_siginfo (const char *annex
,
1101 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1102 ULONGEST offset
, LONGEST len
)
1104 if (the_target
->qxfer_siginfo
== NULL
)
1107 if (annex
[0] != '\0' || !target_running ())
1110 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1113 /* Handle qXfer:spu:read and qXfer:spu:write. */
1116 handle_qxfer_spu (const char *annex
,
1117 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1118 ULONGEST offset
, LONGEST len
)
1120 if (the_target
->qxfer_spu
== NULL
)
1123 if (!target_running ())
1126 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1129 /* Handle qXfer:statictrace:read. */
1132 handle_qxfer_statictrace (const char *annex
,
1133 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1134 ULONGEST offset
, LONGEST len
)
1138 if (writebuf
!= NULL
)
1141 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1144 if (traceframe_read_sdata (current_traceframe
, offset
,
1145 readbuf
, len
, &nbytes
))
1150 /* Helper for handle_qxfer_threads. */
1153 handle_qxfer_threads_proper (struct buffer
*buffer
)
1155 struct inferior_list_entry
*thread
;
1157 buffer_grow_str (buffer
, "<threads>\n");
1159 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1161 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1163 int core
= target_core_of_thread (ptid
);
1166 write_ptid (ptid_s
, ptid
);
1170 sprintf (core_s
, "%d", core
);
1171 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1176 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1181 buffer_grow_str0 (buffer
, "</threads>\n");
1184 /* Handle qXfer:threads:read. */
1187 handle_qxfer_threads (const char *annex
,
1188 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1189 ULONGEST offset
, LONGEST len
)
1191 static char *result
= 0;
1192 static unsigned int result_length
= 0;
1194 if (writebuf
!= NULL
)
1197 if (!target_running () || annex
[0] != '\0')
1202 struct buffer buffer
;
1203 /* When asked for data at offset 0, generate everything and store into
1204 'result'. Successive reads will be served off 'result'. */
1208 buffer_init (&buffer
);
1210 handle_qxfer_threads_proper (&buffer
);
1212 result
= buffer_finish (&buffer
);
1213 result_length
= strlen (result
);
1214 buffer_free (&buffer
);
1217 if (offset
>= result_length
)
1219 /* We're out of data. */
1226 if (len
> result_length
- offset
)
1227 len
= result_length
- offset
;
1229 memcpy (readbuf
, result
+ offset
, len
);
1234 /* Handle qXfer:traceframe-info:read. */
1237 handle_qxfer_traceframe_info (const char *annex
,
1238 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1239 ULONGEST offset
, LONGEST len
)
1241 static char *result
= 0;
1242 static unsigned int result_length
= 0;
1244 if (writebuf
!= NULL
)
1247 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1252 struct buffer buffer
;
1254 /* When asked for data at offset 0, generate everything and
1255 store into 'result'. Successive reads will be served off
1259 buffer_init (&buffer
);
1261 traceframe_read_info (current_traceframe
, &buffer
);
1263 result
= buffer_finish (&buffer
);
1264 result_length
= strlen (result
);
1265 buffer_free (&buffer
);
1268 if (offset
>= result_length
)
1270 /* We're out of data. */
1277 if (len
> result_length
- offset
)
1278 len
= result_length
- offset
;
1280 memcpy (readbuf
, result
+ offset
, len
);
1284 /* Handle qXfer:fdpic:read. */
1287 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1288 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1290 if (the_target
->read_loadmap
== NULL
)
1293 if (!target_running ())
1296 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1299 static const struct qxfer qxfer_packets
[] =
1301 { "auxv", handle_qxfer_auxv
},
1302 { "fdpic", handle_qxfer_fdpic
},
1303 { "features", handle_qxfer_features
},
1304 { "libraries", handle_qxfer_libraries
},
1305 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1306 { "osdata", handle_qxfer_osdata
},
1307 { "siginfo", handle_qxfer_siginfo
},
1308 { "spu", handle_qxfer_spu
},
1309 { "statictrace", handle_qxfer_statictrace
},
1310 { "threads", handle_qxfer_threads
},
1311 { "traceframe-info", handle_qxfer_traceframe_info
},
1315 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1323 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1326 /* Grab the object, r/w and annex. */
1327 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1329 write_enn (own_buf
);
1334 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1337 const struct qxfer
*q
= &qxfer_packets
[i
];
1339 if (strcmp (object
, q
->object
) == 0)
1341 if (strcmp (rw
, "read") == 0)
1343 unsigned char *data
;
1348 /* Grab the offset and length. */
1349 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1351 write_enn (own_buf
);
1355 /* Read one extra byte, as an indicator of whether there is
1357 if (len
> PBUFSIZ
- 2)
1359 data
= malloc (len
+ 1);
1362 write_enn (own_buf
);
1365 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1372 write_enn (own_buf
);
1374 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1376 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1381 else if (strcmp (rw
, "write") == 0)
1386 unsigned char *data
;
1388 strcpy (own_buf
, "E00");
1389 data
= malloc (packet_len
- (offset
- own_buf
));
1392 write_enn (own_buf
);
1395 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1396 &ofs
, &len
, data
) < 0)
1399 write_enn (own_buf
);
1403 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1410 write_enn (own_buf
);
1412 sprintf (own_buf
, "%x", n
);
1425 /* Table used by the crc32 function to calcuate the checksum. */
1427 static unsigned int crc32_table
[256] =
1430 /* Compute 32 bit CRC from inferior memory.
1432 On success, return 32 bit CRC.
1433 On failure, return (unsigned long long) -1. */
1435 static unsigned long long
1436 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1438 if (!crc32_table
[1])
1440 /* Initialize the CRC table and the decoding table. */
1444 for (i
= 0; i
< 256; i
++)
1446 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1447 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1454 unsigned char byte
= 0;
1456 /* Return failure if memory read fails. */
1457 if (read_inferior_memory (base
, &byte
, 1) != 0)
1458 return (unsigned long long) -1;
1460 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1463 return (unsigned long long) crc
;
1466 /* Handle all of the extended 'q' packets. */
1469 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1471 static struct inferior_list_entry
*thread_ptr
;
1473 /* Reply the current thread id. */
1474 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1477 require_running (own_buf
);
1479 if (!ptid_equal (general_thread
, null_ptid
)
1480 && !ptid_equal (general_thread
, minus_one_ptid
))
1481 gdb_id
= general_thread
;
1484 thread_ptr
= all_threads
.head
;
1485 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1488 sprintf (own_buf
, "QC");
1490 write_ptid (own_buf
, gdb_id
);
1494 if (strcmp ("qSymbol::", own_buf
) == 0)
1496 /* GDB is suggesting new symbols have been loaded. This may
1497 mean a new shared library has been detected as loaded, so
1498 take the opportunity to check if breakpoints we think are
1499 inserted, still are. Note that it isn't guaranteed that
1500 we'll see this when a shared library is loaded, and nor will
1501 we see this for unloads (although breakpoints in unloaded
1502 libraries shouldn't trigger), as GDB may not find symbols for
1503 the library at all. We also re-validate breakpoints when we
1504 see a second GDB breakpoint for the same address, and or when
1505 we access breakpoint shadows. */
1506 validate_breakpoints ();
1508 if (target_supports_tracepoints ())
1509 tracepoint_look_up_symbols ();
1511 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1512 (*the_target
->look_up_symbols
) ();
1514 strcpy (own_buf
, "OK");
1518 if (!disable_packet_qfThreadInfo
)
1520 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1524 require_running (own_buf
);
1525 thread_ptr
= all_threads
.head
;
1528 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1529 write_ptid (own_buf
, gdb_id
);
1530 thread_ptr
= thread_ptr
->next
;
1534 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1538 require_running (own_buf
);
1539 if (thread_ptr
!= NULL
)
1542 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1543 write_ptid (own_buf
, gdb_id
);
1544 thread_ptr
= thread_ptr
->next
;
1549 sprintf (own_buf
, "l");
1555 if (the_target
->read_offsets
!= NULL
1556 && strcmp ("qOffsets", own_buf
) == 0)
1558 CORE_ADDR text
, data
;
1560 require_running (own_buf
);
1561 if (the_target
->read_offsets (&text
, &data
))
1562 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1563 (long)text
, (long)data
, (long)data
);
1565 write_enn (own_buf
);
1570 /* Protocol features query. */
1571 if (strncmp ("qSupported", own_buf
, 10) == 0
1572 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1574 char *p
= &own_buf
[10];
1575 int gdb_supports_qRelocInsn
= 0;
1577 /* Start processing qSupported packet. */
1578 target_process_qsupported (NULL
);
1580 /* Process each feature being provided by GDB. The first
1581 feature will follow a ':', and latter features will follow
1585 char **qsupported
= NULL
;
1589 /* Two passes, to avoid nested strtok calls in
1590 target_process_qsupported. */
1591 for (p
= strtok (p
+ 1, ";");
1593 p
= strtok (NULL
, ";"))
1596 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1597 qsupported
[count
- 1] = xstrdup (p
);
1600 for (i
= 0; i
< count
; i
++)
1603 if (strcmp (p
, "multiprocess+") == 0)
1605 /* GDB supports and wants multi-process support if
1607 if (target_supports_multi_process ())
1610 else if (strcmp (p
, "qRelocInsn+") == 0)
1612 /* GDB supports relocate instruction requests. */
1613 gdb_supports_qRelocInsn
= 1;
1616 target_process_qsupported (p
);
1625 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1628 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1629 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1632 /* We do not have any hook to indicate whether the non-SVR4 target
1633 backend supports qXfer:libraries:read, so always report it. */
1634 strcat (own_buf
, ";qXfer:libraries:read+");
1637 if (the_target
->read_auxv
!= NULL
)
1638 strcat (own_buf
, ";qXfer:auxv:read+");
1640 if (the_target
->qxfer_spu
!= NULL
)
1641 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1643 if (the_target
->qxfer_siginfo
!= NULL
)
1644 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1646 if (the_target
->read_loadmap
!= NULL
)
1647 strcat (own_buf
, ";qXfer:fdpic:read+");
1649 /* We always report qXfer:features:read, as targets may
1650 install XML files on a subsequent call to arch_setup.
1651 If we reported to GDB on startup that we don't support
1652 qXfer:feature:read at all, we will never be re-queried. */
1653 strcat (own_buf
, ";qXfer:features:read+");
1655 if (transport_is_reliable
)
1656 strcat (own_buf
, ";QStartNoAckMode+");
1658 if (the_target
->qxfer_osdata
!= NULL
)
1659 strcat (own_buf
, ";qXfer:osdata:read+");
1661 if (target_supports_multi_process ())
1662 strcat (own_buf
, ";multiprocess+");
1664 if (target_supports_non_stop ())
1665 strcat (own_buf
, ";QNonStop+");
1667 if (target_supports_disable_randomization ())
1668 strcat (own_buf
, ";QDisableRandomization+");
1670 strcat (own_buf
, ";qXfer:threads:read+");
1672 if (target_supports_tracepoints ())
1674 strcat (own_buf
, ";ConditionalTracepoints+");
1675 strcat (own_buf
, ";TraceStateVariables+");
1676 strcat (own_buf
, ";TracepointSource+");
1677 strcat (own_buf
, ";DisconnectedTracing+");
1678 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1679 strcat (own_buf
, ";FastTracepoints+");
1680 strcat (own_buf
, ";StaticTracepoints+");
1681 strcat (own_buf
, ";InstallInTrace+");
1682 strcat (own_buf
, ";qXfer:statictrace:read+");
1683 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1684 strcat (own_buf
, ";EnableDisableTracepoints+");
1685 strcat (own_buf
, ";tracenz+");
1688 /* Support target-side breakpoint conditions and commands. */
1689 strcat (own_buf
, ";ConditionalBreakpoints+");
1690 strcat (own_buf
, ";BreakpointCommands+");
1692 if (target_supports_agent ())
1693 strcat (own_buf
, ";QAgent+");
1698 /* Thread-local storage support. */
1699 if (the_target
->get_tls_address
!= NULL
1700 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1702 char *p
= own_buf
+ 12;
1703 CORE_ADDR parts
[2], address
= 0;
1705 ptid_t ptid
= null_ptid
;
1707 require_running (own_buf
);
1709 for (i
= 0; i
< 3; i
++)
1717 p2
= strchr (p
, ',');
1730 ptid
= read_ptid (p
, NULL
);
1732 decode_address (&parts
[i
- 1], p
, len
);
1736 if (p
!= NULL
|| i
< 3)
1740 struct thread_info
*thread
= find_thread_ptid (ptid
);
1745 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1751 strcpy (own_buf
, paddress(address
));
1756 write_enn (own_buf
);
1760 /* Otherwise, pretend we do not understand this packet. */
1763 /* Windows OS Thread Information Block address support. */
1764 if (the_target
->get_tib_address
!= NULL
1765 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1770 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1772 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1775 strcpy (own_buf
, paddress(tlb
));
1780 write_enn (own_buf
);
1786 /* Handle "monitor" commands. */
1787 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1789 char *mon
= malloc (PBUFSIZ
);
1790 int len
= strlen (own_buf
+ 6);
1794 write_enn (own_buf
);
1798 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1800 write_enn (own_buf
);
1804 mon
[len
/ 2] = '\0';
1808 if (the_target
->handle_monitor_command
== NULL
1809 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1810 /* Default processing. */
1811 handle_monitor_command (mon
, own_buf
);
1817 if (strncmp ("qSearch:memory:", own_buf
,
1818 sizeof ("qSearch:memory:") - 1) == 0)
1820 require_running (own_buf
);
1821 handle_search_memory (own_buf
, packet_len
);
1825 if (strcmp (own_buf
, "qAttached") == 0
1826 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1828 struct process_info
*process
;
1830 if (own_buf
[sizeof ("qAttached") - 1])
1832 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1833 process
= (struct process_info
*)
1834 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1838 require_running (own_buf
);
1839 process
= current_process ();
1842 if (process
== NULL
)
1844 write_enn (own_buf
);
1848 strcpy (own_buf
, process
->attached
? "1" : "0");
1852 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1854 /* CRC check (compare-section). */
1858 unsigned long long crc
;
1860 require_running (own_buf
);
1861 base
= strtoul (own_buf
+ 5, &comma
, 16);
1862 if (*comma
++ != ',')
1864 write_enn (own_buf
);
1867 len
= strtoul (comma
, NULL
, 16);
1868 crc
= crc32 (base
, len
, 0xffffffff);
1869 /* Check for memory failure. */
1870 if (crc
== (unsigned long long) -1)
1872 write_enn (own_buf
);
1875 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1879 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1882 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1885 /* Otherwise we didn't know what packet it was. Say we didn't
1890 static void gdb_wants_all_threads_stopped (void);
1892 /* Parse vCont packets. */
1894 handle_v_cont (char *own_buf
)
1898 struct thread_resume
*resume_info
;
1899 struct thread_resume default_action
= {{0}};
1901 /* Count the number of semicolons in the packet. There should be one
1902 for every action. */
1908 p
= strchr (p
, ';');
1911 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1912 if (resume_info
== NULL
)
1920 if (p
[0] == 's' || p
[0] == 'S')
1921 resume_info
[i
].kind
= resume_step
;
1922 else if (p
[0] == 'c' || p
[0] == 'C')
1923 resume_info
[i
].kind
= resume_continue
;
1924 else if (p
[0] == 't')
1925 resume_info
[i
].kind
= resume_stop
;
1929 if (p
[0] == 'S' || p
[0] == 'C')
1932 sig
= strtol (p
+ 1, &q
, 16);
1937 if (!gdb_signal_to_host_p (sig
))
1939 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
1943 resume_info
[i
].sig
= 0;
1949 resume_info
[i
].thread
= minus_one_ptid
;
1950 default_action
= resume_info
[i
];
1952 /* Note: we don't increment i here, we'll overwrite this entry
1953 the next time through. */
1955 else if (p
[0] == ':')
1957 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1962 if (p
[0] != ';' && p
[0] != 0)
1965 resume_info
[i
].thread
= ptid
;
1972 resume_info
[i
] = default_action
;
1974 /* `cont_thread' is still used in occasional places in the backend,
1975 to implement single-thread scheduler-locking. Doesn't make sense
1976 to set it if we see a stop request, or a wildcard action (one
1977 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
1979 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1980 || ptid_get_lwp (resume_info
[0].thread
) == -1)
1981 && resume_info
[0].kind
!= resume_stop
)
1982 cont_thread
= resume_info
[0].thread
;
1984 cont_thread
= minus_one_ptid
;
1985 set_desired_inferior (0);
1990 (*the_target
->resume
) (resume_info
, n
);
1998 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2000 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2001 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2002 current_inferior
->last_status
= last_status
;
2004 /* From the client's perspective, all-stop mode always stops all
2005 threads implicitly (and the target backend has already done
2006 so by now). Tag all threads as "want-stopped", so we don't
2007 resume them implicitly without the client telling us to. */
2008 gdb_wants_all_threads_stopped ();
2009 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2010 disable_async_io ();
2012 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2013 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2014 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2019 write_enn (own_buf
);
2024 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2026 handle_v_attach (char *own_buf
)
2030 pid
= strtol (own_buf
+ 8, NULL
, 16);
2031 if (pid
!= 0 && attach_inferior (pid
) == 0)
2033 /* Don't report shared library events after attaching, even if
2034 some libraries are preloaded. GDB will always poll the
2035 library list. Avoids the "stopped by shared library event"
2036 notice on the GDB side. */
2041 /* In non-stop, we don't send a resume reply. Stop events
2042 will follow up using the normal notification
2047 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2053 write_enn (own_buf
);
2058 /* Run a new program. Return 1 if successful, 0 if failure. */
2060 handle_v_run (char *own_buf
)
2062 char *p
, *next_p
, **new_argv
;
2066 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2072 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2073 if (new_argv
== NULL
)
2075 write_enn (own_buf
);
2080 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2082 next_p
= strchr (p
, ';');
2084 next_p
= p
+ strlen (p
);
2086 if (i
== 0 && p
== next_p
)
2090 /* FIXME: Fail request if out of memory instead of dying. */
2091 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2092 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2093 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2102 if (new_argv
[0] == NULL
)
2104 /* GDB didn't specify a program to run. Use the program from the
2105 last run with the new argument list. */
2107 if (program_argv
== NULL
)
2109 write_enn (own_buf
);
2110 freeargv (new_argv
);
2114 new_argv
[0] = strdup (program_argv
[0]);
2115 if (new_argv
[0] == NULL
)
2117 write_enn (own_buf
);
2118 freeargv (new_argv
);
2123 /* Free the old argv and install the new one. */
2124 freeargv (program_argv
);
2125 program_argv
= new_argv
;
2127 start_inferior (program_argv
);
2128 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2130 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2132 /* In non-stop, sending a resume reply doesn't set the general
2133 thread, but GDB assumes a vRun sets it (this is so GDB can
2134 query which is the main thread of the new inferior. */
2136 general_thread
= last_ptid
;
2142 write_enn (own_buf
);
2147 /* Kill process. Return 1 if successful, 0 if failure. */
2149 handle_v_kill (char *own_buf
)
2152 char *p
= &own_buf
[6];
2154 pid
= strtol (p
, NULL
, 16);
2157 if (pid
!= 0 && kill_inferior (pid
) == 0)
2159 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2160 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2161 last_ptid
= pid_to_ptid (pid
);
2162 discard_queued_stop_replies (pid
);
2168 write_enn (own_buf
);
2173 /* Handle a 'vStopped' packet. */
2175 handle_v_stopped (char *own_buf
)
2177 /* If we're waiting for GDB to acknowledge a pending stop reply,
2178 consider that done. */
2181 struct vstop_notif
*head
;
2184 fprintf (stderr
, "vStopped: acking %s\n",
2185 target_pid_to_str (notif_queue
->ptid
));
2188 notif_queue
= notif_queue
->next
;
2192 /* Push another stop reply, or if there are no more left, an OK. */
2193 send_next_stop_reply (own_buf
);
2196 /* Handle all of the extended 'v' packets. */
2198 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2200 if (!disable_packet_vCont
)
2202 if (strncmp (own_buf
, "vCont;", 6) == 0)
2204 require_running (own_buf
);
2205 handle_v_cont (own_buf
);
2209 if (strncmp (own_buf
, "vCont?", 6) == 0)
2211 strcpy (own_buf
, "vCont;c;C;s;S;t");
2216 if (strncmp (own_buf
, "vFile:", 6) == 0
2217 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2220 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2222 if ((!extended_protocol
|| !multi_process
) && target_running ())
2224 fprintf (stderr
, "Already debugging a process\n");
2225 write_enn (own_buf
);
2228 handle_v_attach (own_buf
);
2232 if (strncmp (own_buf
, "vRun;", 5) == 0)
2234 if ((!extended_protocol
|| !multi_process
) && target_running ())
2236 fprintf (stderr
, "Already debugging a process\n");
2237 write_enn (own_buf
);
2240 handle_v_run (own_buf
);
2244 if (strncmp (own_buf
, "vKill;", 6) == 0)
2246 if (!target_running ())
2248 fprintf (stderr
, "No process to kill\n");
2249 write_enn (own_buf
);
2252 handle_v_kill (own_buf
);
2256 if (strncmp (own_buf
, "vStopped", 8) == 0)
2258 handle_v_stopped (own_buf
);
2262 /* Otherwise we didn't know what packet it was. Say we didn't
2268 /* Resume inferior and wait for another event. In non-stop mode,
2269 don't really wait here, but return immediatelly to the event
2272 myresume (char *own_buf
, int step
, int sig
)
2274 struct thread_resume resume_info
[2];
2276 int valid_cont_thread
;
2278 set_desired_inferior (0);
2280 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2281 && !ptid_equal (cont_thread
, minus_one_ptid
));
2283 if (step
|| sig
|| valid_cont_thread
)
2285 resume_info
[0].thread
2286 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2288 resume_info
[0].kind
= resume_step
;
2290 resume_info
[0].kind
= resume_continue
;
2291 resume_info
[0].sig
= sig
;
2295 if (!valid_cont_thread
)
2297 resume_info
[n
].thread
= minus_one_ptid
;
2298 resume_info
[n
].kind
= resume_continue
;
2299 resume_info
[n
].sig
= 0;
2306 (*the_target
->resume
) (resume_info
, n
);
2312 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2314 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2315 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2317 current_inferior
->last_resume_kind
= resume_stop
;
2318 current_inferior
->last_status
= last_status
;
2321 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2322 disable_async_io ();
2324 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2325 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2326 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2330 /* Callback for for_each_inferior. Make a new stop reply for each
2334 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2336 struct thread_info
*thread
= (struct thread_info
*) entry
;
2338 /* For now, assume targets that don't have this callback also don't
2339 manage the thread's last_status field. */
2340 if (the_target
->thread_stopped
== NULL
)
2342 /* Pass the last stop reply back to GDB, but don't notify
2344 queue_stop_reply (entry
->id
, &thread
->last_status
);
2348 if (thread_stopped (thread
))
2352 "Reporting thread %s as already stopped with %s\n",
2353 target_pid_to_str (entry
->id
),
2354 target_waitstatus_to_string (&thread
->last_status
));
2356 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2358 /* Pass the last stop reply back to GDB, but don't notify
2360 queue_stop_reply (entry
->id
, &thread
->last_status
);
2367 /* Set this inferior threads's state as "want-stopped". We won't
2368 resume this thread until the client gives us another action for
2372 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2374 struct thread_info
*thread
= (struct thread_info
*) entry
;
2376 thread
->last_resume_kind
= resume_stop
;
2378 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2380 /* Most threads are stopped implicitly (all-stop); tag that with
2382 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2383 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2387 /* Set all threads' states as "want-stopped". */
2390 gdb_wants_all_threads_stopped (void)
2392 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2395 /* Clear the gdb_detached flag of every process. */
2398 gdb_reattached_process (struct inferior_list_entry
*entry
)
2400 struct process_info
*process
= (struct process_info
*) entry
;
2402 process
->gdb_detached
= 0;
2405 /* Status handler for the '?' packet. */
2408 handle_status (char *own_buf
)
2410 /* GDB is connected, don't forward events to the target anymore. */
2411 for_each_inferior (&all_processes
, gdb_reattached_process
);
2413 /* In non-stop mode, we must send a stop reply for each stopped
2414 thread. In all-stop mode, just send one for the first stopped
2419 discard_queued_stop_replies (-1);
2420 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2422 /* The first is sent immediatly. OK is sent if there is no
2423 stopped thread, which is the same handling of the vStopped
2424 packet (by design). */
2425 send_next_stop_reply (own_buf
);
2430 stabilize_threads ();
2431 gdb_wants_all_threads_stopped ();
2433 if (all_threads
.head
)
2435 struct target_waitstatus status
;
2437 status
.kind
= TARGET_WAITKIND_STOPPED
;
2438 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2439 prepare_resume_reply (own_buf
,
2440 all_threads
.head
->id
, &status
);
2443 strcpy (own_buf
, "W00");
2448 gdbserver_version (void)
2450 printf ("GNU gdbserver %s%s\n"
2451 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2452 "gdbserver is free software, covered by the "
2453 "GNU General Public License.\n"
2454 "This gdbserver was configured as \"%s\"\n",
2455 PKGVERSION
, version
, host_name
);
2459 gdbserver_usage (FILE *stream
)
2461 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2462 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2463 "\tgdbserver [OPTIONS] --multi COMM\n"
2465 "COMM may either be a tty device (for serial debugging), or \n"
2466 "HOST:PORT to listen for a TCP connection.\n"
2469 " --debug Enable general debugging output.\n"
2470 " --remote-debug Enable remote protocol debugging output.\n"
2471 " --version Display version information and exit.\n"
2472 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2473 " --once Exit after the first connection has "
2475 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2476 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2480 gdbserver_show_disableable (FILE *stream
)
2482 fprintf (stream
, "Disableable packets:\n"
2483 " vCont \tAll vCont packets\n"
2484 " qC \tQuerying the current thread\n"
2485 " qfThreadInfo\tThread listing\n"
2486 " Tthread \tPassing the thread specifier in the "
2487 "T stop reply packet\n"
2488 " threads \tAll of the above\n");
2492 #undef require_running
2493 #define require_running(BUF) \
2494 if (!target_running ()) \
2501 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2503 int pid
= * (int *) args
;
2505 if (ptid_get_pid (entry
->id
) == pid
)
2512 kill_inferior_callback (struct inferior_list_entry
*entry
)
2514 struct process_info
*process
= (struct process_info
*) entry
;
2515 int pid
= ptid_get_pid (process
->head
.id
);
2517 kill_inferior (pid
);
2518 discard_queued_stop_replies (pid
);
2521 /* Callback for for_each_inferior to detach or kill the inferior,
2522 depending on whether we attached to it or not.
2523 We inform the user whether we're detaching or killing the process
2524 as this is only called when gdbserver is about to exit. */
2527 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2529 struct process_info
*process
= (struct process_info
*) entry
;
2530 int pid
= ptid_get_pid (process
->head
.id
);
2532 if (process
->attached
)
2533 detach_inferior (pid
);
2535 kill_inferior (pid
);
2537 discard_queued_stop_replies (pid
);
2540 /* for_each_inferior callback for detach_or_kill_for_exit to print
2541 the pids of started inferiors. */
2544 print_started_pid (struct inferior_list_entry
*entry
)
2546 struct process_info
*process
= (struct process_info
*) entry
;
2548 if (! process
->attached
)
2550 int pid
= ptid_get_pid (process
->head
.id
);
2551 fprintf (stderr
, " %d", pid
);
2555 /* for_each_inferior callback for detach_or_kill_for_exit to print
2556 the pids of attached inferiors. */
2559 print_attached_pid (struct inferior_list_entry
*entry
)
2561 struct process_info
*process
= (struct process_info
*) entry
;
2563 if (process
->attached
)
2565 int pid
= ptid_get_pid (process
->head
.id
);
2566 fprintf (stderr
, " %d", pid
);
2570 /* Call this when exiting gdbserver with possible inferiors that need
2571 to be killed or detached from. */
2574 detach_or_kill_for_exit (void)
2576 /* First print a list of the inferiors we will be killing/detaching.
2577 This is to assist the user, for example, in case the inferior unexpectedly
2578 dies after we exit: did we screw up or did the inferior exit on its own?
2579 Having this info will save some head-scratching. */
2581 if (have_started_inferiors_p ())
2583 fprintf (stderr
, "Killing process(es):");
2584 for_each_inferior (&all_processes
, print_started_pid
);
2585 fprintf (stderr
, "\n");
2587 if (have_attached_inferiors_p ())
2589 fprintf (stderr
, "Detaching process(es):");
2590 for_each_inferior (&all_processes
, print_attached_pid
);
2591 fprintf (stderr
, "\n");
2594 /* Now we can kill or detach the inferiors. */
2596 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2600 main (int argc
, char *argv
[])
2604 char *arg_end
, *port
;
2605 char **next_arg
= &argv
[1];
2606 volatile int multi_mode
= 0;
2607 volatile int attach
= 0;
2610 while (*next_arg
!= NULL
&& **next_arg
== '-')
2612 if (strcmp (*next_arg
, "--version") == 0)
2614 gdbserver_version ();
2617 else if (strcmp (*next_arg
, "--help") == 0)
2619 gdbserver_usage (stdout
);
2622 else if (strcmp (*next_arg
, "--attach") == 0)
2624 else if (strcmp (*next_arg
, "--multi") == 0)
2626 else if (strcmp (*next_arg
, "--wrapper") == 0)
2630 wrapper_argv
= next_arg
;
2631 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2634 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2636 gdbserver_usage (stderr
);
2640 /* Consume the "--". */
2643 else if (strcmp (*next_arg
, "--debug") == 0)
2645 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2647 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2649 gdbserver_show_disableable (stdout
);
2652 else if (strncmp (*next_arg
,
2653 "--disable-packet=",
2654 sizeof ("--disable-packet=") - 1) == 0)
2656 char *packets
, *tok
;
2658 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2659 for (tok
= strtok (packets
, ",");
2661 tok
= strtok (NULL
, ","))
2663 if (strcmp ("vCont", tok
) == 0)
2664 disable_packet_vCont
= 1;
2665 else if (strcmp ("Tthread", tok
) == 0)
2666 disable_packet_Tthread
= 1;
2667 else if (strcmp ("qC", tok
) == 0)
2668 disable_packet_qC
= 1;
2669 else if (strcmp ("qfThreadInfo", tok
) == 0)
2670 disable_packet_qfThreadInfo
= 1;
2671 else if (strcmp ("threads", tok
) == 0)
2673 disable_packet_vCont
= 1;
2674 disable_packet_Tthread
= 1;
2675 disable_packet_qC
= 1;
2676 disable_packet_qfThreadInfo
= 1;
2680 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2682 gdbserver_show_disableable (stderr
);
2687 else if (strcmp (*next_arg
, "-") == 0)
2689 /* "-" specifies a stdio connection and is a form of port
2691 *next_arg
= STDIO_CONNECTION_NAME
;
2694 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2695 disable_randomization
= 1;
2696 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2697 disable_randomization
= 0;
2698 else if (strcmp (*next_arg
, "--once") == 0)
2702 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2710 if (setjmp (toplevel
))
2712 fprintf (stderr
, "Exiting\n");
2718 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2720 gdbserver_usage (stderr
);
2724 /* We need to know whether the remote connection is stdio before
2725 starting the inferior. Inferiors created in this scenario have
2726 stdin,stdout redirected. So do this here before we call
2728 remote_prepare (port
);
2733 /* --attach used to come after PORT, so allow it there for
2735 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2742 && (*next_arg
== NULL
2743 || (*next_arg
)[0] == '\0'
2744 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2746 || next_arg
[1] != NULL
))
2751 gdbserver_usage (stderr
);
2755 initialize_async_io ();
2757 if (target_supports_tracepoints ())
2758 initialize_tracepoint ();
2760 own_buf
= xmalloc (PBUFSIZ
+ 1);
2761 mem_buf
= xmalloc (PBUFSIZ
);
2763 if (pid
== 0 && *next_arg
!= NULL
)
2767 n
= argc
- (next_arg
- argv
);
2768 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2769 for (i
= 0; i
< n
; i
++)
2770 program_argv
[i
] = xstrdup (next_arg
[i
]);
2771 program_argv
[i
] = NULL
;
2773 /* Wait till we are at first instruction in program. */
2774 start_inferior (program_argv
);
2776 /* We are now (hopefully) stopped at the first instruction of
2777 the target process. This assumes that the target process was
2778 successfully created. */
2782 if (attach_inferior (pid
) == -1)
2783 error ("Attaching not supported on this target");
2785 /* Otherwise succeeded. */
2789 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2790 last_status
.value
.integer
= 0;
2791 last_ptid
= minus_one_ptid
;
2794 /* Don't report shared library events on the initial connection,
2795 even if some libraries are preloaded. Avoids the "stopped by
2796 shared library event" notice on gdb side. */
2799 if (setjmp (toplevel
))
2801 /* If something fails and longjmps while detaching or killing
2802 inferiors, we'd end up here again, stuck in an infinite loop
2803 trap. Be sure that if that happens, we exit immediately
2805 if (setjmp (toplevel
) == 0)
2806 detach_or_kill_for_exit ();
2808 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2812 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2813 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2818 if (!was_running
&& !multi_mode
)
2820 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2828 /* Be sure we're out of tfind mode. */
2829 current_traceframe
= -1;
2833 if (setjmp (toplevel
) != 0)
2835 /* An error occurred. */
2836 if (response_needed
)
2838 write_enn (own_buf
);
2843 /* Wait for events. This will return when all event sources are
2844 removed from the event loop. */
2845 start_event_loop ();
2847 /* If an exit was requested (using the "monitor exit" command),
2848 terminate now. The only other way to get here is for
2849 getpkt to fail; close the connection and reopen it at the
2852 if (exit_requested
|| run_once
)
2854 /* If something fails and longjmps while detaching or
2855 killing inferiors, we'd end up here again, stuck in an
2856 infinite loop trap. Be sure that if that happens, we
2857 exit immediately instead. */
2858 if (setjmp (toplevel
) == 0)
2860 detach_or_kill_for_exit ();
2865 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2871 "Remote side has terminated connection. "
2872 "GDBserver will reopen the connection.\n");
2876 if (disconnected_tracing
)
2878 /* Try to enable non-stop/async mode, so we we can both
2879 wait for an async socket accept, and handle async
2880 target events simultaneously. There's also no point
2881 either in having the target always stop all threads,
2882 when we're going to pass signals down without
2886 if (start_non_stop (1))
2889 /* Detaching implicitly resumes all threads; simply
2890 disconnecting does not. */
2896 "Disconnected tracing disabled; stopping trace run.\n");
2903 /* Process options coming from Z packets for *point at address
2904 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2905 to point to the first char after the last processed option. */
2908 process_point_options (CORE_ADDR point_addr
, char **packet
)
2910 char *dataptr
= *packet
;
2913 /* Check if data has the correct format. */
2914 if (*dataptr
!= ';')
2921 if (*dataptr
== ';')
2924 if (*dataptr
== 'X')
2926 /* Conditional expression. */
2927 fprintf (stderr
, "Found breakpoint condition.\n");
2928 add_breakpoint_condition (point_addr
, &dataptr
);
2930 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
2932 dataptr
+= strlen ("cmds:");
2934 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
2935 persist
= (*dataptr
== '1');
2937 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
2941 /* Unrecognized token, just skip it. */
2942 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2946 /* Skip tokens until we find one that we recognize. */
2947 while (*dataptr
&& *dataptr
!= ';')
2953 /* Event loop callback that handles a serial event. The first byte in
2954 the serial buffer gets us here. We expect characters to arrive at
2955 a brisk pace, so we read the rest of the packet with a blocking
2959 process_serial_event (void)
2970 int new_packet_len
= -1;
2972 /* Used to decide when gdbserver should exit in
2973 multi-mode/remote. */
2974 static int have_ran
= 0;
2977 have_ran
= target_running ();
2979 disable_async_io ();
2981 response_needed
= 0;
2982 packet_len
= getpkt (own_buf
);
2983 if (packet_len
<= 0)
2986 /* Force an event loop break. */
2989 response_needed
= 1;
2996 handle_query (own_buf
, packet_len
, &new_packet_len
);
2999 handle_general_set (own_buf
);
3002 require_running (own_buf
);
3007 pid
= strtol (&own_buf
[i
], NULL
, 16);
3011 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
3013 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3015 struct thread_resume resume_info
;
3016 struct process_info
*process
= find_process_pid (pid
);
3018 if (process
== NULL
)
3020 write_enn (own_buf
);
3024 if (tracing
&& disconnected_tracing
)
3026 "Disconnected tracing in effect, "
3027 "leaving gdbserver attached to the process\n");
3029 if (any_persistent_commands ())
3031 "Persistent commands are present, "
3032 "leaving gdbserver attached to the process\n");
3034 /* Make sure we're in non-stop/async mode, so we we can both
3035 wait for an async socket accept, and handle async target
3036 events simultaneously. There's also no point either in
3037 having the target stop all threads, when we're going to
3038 pass signals down without informing GDB. */
3042 fprintf (stderr
, "Forcing non-stop mode\n");
3048 process
->gdb_detached
= 1;
3050 /* Detaching implicitly resumes all threads. */
3051 resume_info
.thread
= minus_one_ptid
;
3052 resume_info
.kind
= resume_continue
;
3053 resume_info
.sig
= 0;
3054 (*the_target
->resume
) (&resume_info
, 1);
3057 break; /* from switch/case */
3060 fprintf (stderr
, "Detaching from process %d\n", pid
);
3062 if (detach_inferior (pid
) != 0)
3063 write_enn (own_buf
);
3066 discard_queued_stop_replies (pid
);
3069 if (extended_protocol
)
3071 /* Treat this like a normal program exit. */
3072 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3073 last_status
.value
.integer
= 0;
3074 last_ptid
= pid_to_ptid (pid
);
3076 current_inferior
= NULL
;
3083 /* If we are attached, then we can exit. Otherwise, we
3084 need to hang around doing nothing, until the child is
3086 join_inferior (pid
);
3092 extended_protocol
= 1;
3096 handle_status (own_buf
);
3099 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3101 ptid_t gdb_id
, thread_id
;
3104 require_running (own_buf
);
3106 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3108 pid
= ptid_get_pid (gdb_id
);
3110 if (ptid_equal (gdb_id
, null_ptid
)
3111 || ptid_equal (gdb_id
, minus_one_ptid
))
3112 thread_id
= null_ptid
;
3114 && ptid_equal (pid_to_ptid (pid
),
3117 struct thread_info
*thread
=
3118 (struct thread_info
*) find_inferior (&all_threads
,
3123 write_enn (own_buf
);
3127 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3131 thread_id
= gdb_id_to_thread_id (gdb_id
);
3132 if (ptid_equal (thread_id
, null_ptid
))
3134 write_enn (own_buf
);
3139 if (own_buf
[1] == 'g')
3141 if (ptid_equal (thread_id
, null_ptid
))
3143 /* GDB is telling us to choose any thread. Check if
3144 the currently selected thread is still valid. If
3145 it is not, select the first available. */
3146 struct thread_info
*thread
=
3147 (struct thread_info
*) find_inferior_id (&all_threads
,
3150 thread_id
= all_threads
.head
->id
;
3153 general_thread
= thread_id
;
3154 set_desired_inferior (1);
3156 else if (own_buf
[1] == 'c')
3157 cont_thread
= thread_id
;
3163 /* Silently ignore it so that gdb can extend the protocol
3164 without compatibility headaches. */
3169 require_running (own_buf
);
3170 if (current_traceframe
>= 0)
3172 struct regcache
*regcache
= new_register_cache ();
3174 if (fetch_traceframe_registers (current_traceframe
,
3176 registers_to_string (regcache
, own_buf
);
3178 write_enn (own_buf
);
3179 free_register_cache (regcache
);
3183 struct regcache
*regcache
;
3185 set_desired_inferior (1);
3186 regcache
= get_thread_regcache (current_inferior
, 1);
3187 registers_to_string (regcache
, own_buf
);
3191 require_running (own_buf
);
3192 if (current_traceframe
>= 0)
3193 write_enn (own_buf
);
3196 struct regcache
*regcache
;
3198 set_desired_inferior (1);
3199 regcache
= get_thread_regcache (current_inferior
, 1);
3200 registers_from_string (regcache
, &own_buf
[1]);
3205 require_running (own_buf
);
3206 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3207 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3209 write_enn (own_buf
);
3211 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3214 require_running (own_buf
);
3215 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3216 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3219 write_enn (own_buf
);
3222 require_running (own_buf
);
3223 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3224 &mem_addr
, &len
, &mem_buf
) < 0
3225 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3226 write_enn (own_buf
);
3231 require_running (own_buf
);
3232 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3233 if (gdb_signal_to_host_p (sig
))
3234 signal
= gdb_signal_to_host (sig
);
3237 myresume (own_buf
, 0, signal
);
3240 require_running (own_buf
);
3241 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3242 if (gdb_signal_to_host_p (sig
))
3243 signal
= gdb_signal_to_host (sig
);
3246 myresume (own_buf
, 1, signal
);
3249 require_running (own_buf
);
3251 myresume (own_buf
, 0, signal
);
3254 require_running (own_buf
);
3256 myresume (own_buf
, 1, signal
);
3258 case 'Z': /* insert_ ... */
3260 case 'z': /* remove_ ... */
3264 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3265 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3266 char type
= own_buf
[1];
3268 const int insert
= ch
== 'Z';
3270 /* Default to unrecognized/unsupported. */
3274 case '0': /* software-breakpoint */
3275 case '1': /* hardware-breakpoint */
3276 case '2': /* write watchpoint */
3277 case '3': /* read watchpoint */
3278 case '4': /* access watchpoint */
3279 require_running (own_buf
);
3280 if (insert
&& the_target
->insert_point
!= NULL
)
3282 /* Insert the breakpoint. If it is already inserted, nothing
3284 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3286 /* GDB may have sent us a list of *point parameters to be
3287 evaluated on the target's side. Read such list here. If we
3288 already have a list of parameters, GDB is telling us to drop
3289 that list and use this one instead. */
3290 if (!res
&& (type
== '0' || type
== '1'))
3292 /* Remove previous conditions. */
3293 clear_gdb_breakpoint_conditions (addr
);
3294 process_point_options (addr
, &dataptr
);
3297 else if (!insert
&& the_target
->remove_point
!= NULL
)
3298 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3310 write_enn (own_buf
);
3314 response_needed
= 0;
3315 if (!target_running ())
3316 /* The packet we received doesn't make sense - but we can't
3317 reply to it, either. */
3320 fprintf (stderr
, "Killing all inferiors\n");
3321 for_each_inferior (&all_processes
, kill_inferior_callback
);
3323 /* When using the extended protocol, we wait with no program
3324 running. The traditional protocol will exit instead. */
3325 if (extended_protocol
)
3327 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3328 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3336 ptid_t gdb_id
, thread_id
;
3338 require_running (own_buf
);
3340 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3341 thread_id
= gdb_id_to_thread_id (gdb_id
);
3342 if (ptid_equal (thread_id
, null_ptid
))
3344 write_enn (own_buf
);
3348 if (mythread_alive (thread_id
))
3351 write_enn (own_buf
);
3355 response_needed
= 0;
3357 /* Restarting the inferior is only supported in the extended
3359 if (extended_protocol
)
3361 if (target_running ())
3362 for_each_inferior (&all_processes
,
3363 kill_inferior_callback
);
3364 fprintf (stderr
, "GDBserver restarting\n");
3366 /* Wait till we are at 1st instruction in prog. */
3367 if (program_argv
!= NULL
)
3368 start_inferior (program_argv
);
3371 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3372 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3378 /* It is a request we don't understand. Respond with an
3379 empty packet so that gdb knows that we don't support this
3385 /* Extended (long) request. */
3386 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3390 /* It is a request we don't understand. Respond with an empty
3391 packet so that gdb knows that we don't support this
3397 if (new_packet_len
!= -1)
3398 putpkt_binary (own_buf
, new_packet_len
);
3402 response_needed
= 0;
3404 if (!extended_protocol
&& have_ran
&& !target_running ())
3406 /* In non-stop, defer exiting until GDB had a chance to query
3407 the whole vStopped list (until it gets an OK). */
3410 fprintf (stderr
, "GDBserver exiting\n");
3422 /* Event-loop callback for serial events. */
3425 handle_serial_event (int err
, gdb_client_data client_data
)
3428 fprintf (stderr
, "handling possible serial event\n");
3430 /* Really handle it. */
3431 if (process_serial_event () < 0)
3434 /* Be sure to not change the selected inferior behind GDB's back.
3435 Important in the non-stop mode asynchronous protocol. */
3436 set_desired_inferior (1);
3441 /* Event-loop callback for target events. */
3444 handle_target_event (int err
, gdb_client_data client_data
)
3447 fprintf (stderr
, "handling possible target event\n");
3449 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3452 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3454 int pid
= ptid_get_pid (last_ptid
);
3455 struct process_info
*process
= find_process_pid (pid
);
3456 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3458 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3459 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3461 mark_breakpoints_out (process
);
3462 mourn_inferior (process
);
3466 /* We're reporting this thread as stopped. Update its
3467 "want-stopped" state to what the client wants, until it
3468 gets a new resume action. */
3469 current_inferior
->last_resume_kind
= resume_stop
;
3470 current_inferior
->last_status
= last_status
;
3475 if (!target_running ())
3477 /* The last process exited. We're done. */
3481 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3483 /* A thread stopped with a signal, but gdb isn't
3484 connected to handle it. Pass it down to the
3485 inferior, as if it wasn't being traced. */
3486 struct thread_resume resume_info
;
3490 "GDB not connected; forwarding event %d for [%s]\n",
3491 (int) last_status
.kind
,
3492 target_pid_to_str (last_ptid
));
3494 resume_info
.thread
= last_ptid
;
3495 resume_info
.kind
= resume_continue
;
3496 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3497 (*the_target
->resume
) (&resume_info
, 1);
3499 else if (debug_threads
)
3500 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3501 (int) last_status
.kind
,
3502 target_pid_to_str (last_ptid
));
3506 /* Something interesting. Tell GDB about it. */
3507 push_event (last_ptid
, &last_status
);
3511 /* Be sure to not change the selected inferior behind GDB's back.
3512 Important in the non-stop mode asynchronous protocol. */
3513 set_desired_inferior (1);