1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2015 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"
33 #include "btrace-common.h"
34 #include "filestuff.h"
35 #include "tracepoint.h"
39 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
40 `vCont'. Note the multi-process extensions made `vCont' a
41 requirement, so `Hc pPID.TID' is pretty much undefined. So
42 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
43 resuming all threads of the process (again, `Hc' isn't used for
44 multi-process), or a specific thread ptid_t. */
47 /* The thread set with an `Hg' packet. */
48 ptid_t general_thread
;
52 static int extended_protocol
;
53 static int response_needed
;
54 static int exit_requested
;
56 /* --once: Exit after the first connection has closed. */
64 /* Whether we should attempt to disable the operating system's address
65 space randomization feature before starting an inferior. */
66 int disable_randomization
= 1;
68 static char **program_argv
, **wrapper_argv
;
70 int pass_signals
[GDB_SIGNAL_LAST
];
71 int program_signals
[GDB_SIGNAL_LAST
];
72 int program_signals_p
;
74 /* The PID of the originally created or attached inferior. Used to
75 send signals to the process when GDB sends us an asynchronous interrupt
76 (user hitting Control-C in the client), and to wait for the child to exit
77 when no longer debugging it. */
79 unsigned long signal_pid
;
82 /* A file descriptor for the controlling terminal. */
85 /* TERMINAL_FD's original foreground group. */
86 pid_t old_foreground_pgrp
;
88 /* Hand back terminal ownership to the original foreground group. */
91 restore_old_foreground_pgrp (void)
93 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
97 /* Set if you want to disable optional thread related packets support
98 in gdbserver, for the sake of testing GDB against stubs that don't
100 int disable_packet_vCont
;
101 int disable_packet_Tthread
;
102 int disable_packet_qC
;
103 int disable_packet_qfThreadInfo
;
105 /* Last status reported to GDB. */
106 static struct target_waitstatus last_status
;
107 static ptid_t last_ptid
;
109 static char *own_buf
;
110 static unsigned char *mem_buf
;
112 /* A sub-class of 'struct notif_event' for stop, holding information
113 relative to a single stop reply. We keep a queue of these to
114 push to GDB in non-stop mode. */
118 struct notif_event base
;
120 /* Thread or process that got the event. */
124 struct target_waitstatus status
;
127 /* The current btrace configuration. This is gdbserver's mirror of GDB's
128 btrace configuration. */
129 static struct btrace_config current_btrace_conf
;
131 DEFINE_QUEUE_P (notif_event_p
);
133 /* Put a stop reply to the stop reply queue. */
136 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
138 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
140 new_notif
->ptid
= ptid
;
141 new_notif
->status
= *status
;
143 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
147 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
148 QUEUE_ITER (notif_event_p
) *iter
,
149 struct notif_event
*event
,
155 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
157 if (q
->free_func
!= NULL
)
158 q
->free_func (event
);
160 QUEUE_remove_elem (notif_event_p
, q
, iter
);
166 /* Get rid of the currently pending stop replies for PID. If PID is
167 -1, then apply to all processes. */
170 discard_queued_stop_replies (int pid
)
172 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
173 remove_all_on_match_pid
, &pid
);
177 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
179 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
181 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
184 struct notif_server notif_stop
=
186 "vStopped", "Stop", NULL
, vstop_notif_reply
,
190 target_running (void)
192 return get_first_thread () != NULL
;
196 start_inferior (char **argv
)
198 char **new_argv
= argv
;
200 if (wrapper_argv
!= NULL
)
204 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
206 for (i
= 0; argv
[i
] != NULL
; i
++)
208 new_argv
= alloca (sizeof (char *) * count
);
210 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
211 new_argv
[count
++] = wrapper_argv
[i
];
212 for (i
= 0; argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = argv
[i
];
214 new_argv
[count
] = NULL
;
220 for (i
= 0; new_argv
[i
]; ++i
)
221 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
226 signal (SIGTTOU
, SIG_DFL
);
227 signal (SIGTTIN
, SIG_DFL
);
230 signal_pid
= create_inferior (new_argv
[0], new_argv
);
232 /* FIXME: we don't actually know at this point that the create
233 actually succeeded. We won't know that until we wait. */
234 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
239 signal (SIGTTOU
, SIG_IGN
);
240 signal (SIGTTIN
, SIG_IGN
);
241 terminal_fd
= fileno (stderr
);
242 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
243 tcsetpgrp (terminal_fd
, signal_pid
);
244 atexit (restore_old_foreground_pgrp
);
247 if (wrapper_argv
!= NULL
)
249 struct thread_resume resume_info
;
251 memset (&resume_info
, 0, sizeof (resume_info
));
252 resume_info
.thread
= pid_to_ptid (signal_pid
);
253 resume_info
.kind
= resume_continue
;
256 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
258 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
263 (*the_target
->resume
) (&resume_info
, 1);
265 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
266 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
269 current_thread
->last_resume_kind
= resume_stop
;
270 current_thread
->last_status
= last_status
;
272 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
277 /* Wait till we are at 1st instruction in program, return new pid
278 (assuming success). */
279 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
281 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
282 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
284 current_thread
->last_resume_kind
= resume_stop
;
285 current_thread
->last_status
= last_status
;
292 attach_inferior (int pid
)
294 /* myattach should return -1 if attaching is unsupported,
295 0 if it succeeded, and call error() otherwise. */
297 if (myattach (pid
) != 0)
300 fprintf (stderr
, "Attached; pid = %d\n", pid
);
303 /* FIXME - It may be that we should get the SIGNAL_PID from the
304 attach function, so that it can be the main thread instead of
305 whichever we were told to attach to. */
310 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
312 /* GDB knows to ignore the first SIGSTOP after attaching to a running
313 process using the "attach" command, but this is different; it's
314 just using "target remote". Pretend it's just starting up. */
315 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
316 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
317 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
319 current_thread
->last_resume_kind
= resume_stop
;
320 current_thread
->last_status
= last_status
;
326 extern int remote_debug
;
328 /* Decode a qXfer read request. Return 0 if everything looks OK,
332 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
334 /* After the read marker and annex, qXfer looks like a
335 traditional 'm' packet. */
336 decode_m_packet (buf
, ofs
, len
);
342 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
344 /* Extract and NUL-terminate the object. */
346 while (*buf
&& *buf
!= ':')
352 /* Extract and NUL-terminate the read/write action. */
354 while (*buf
&& *buf
!= ':')
360 /* Extract and NUL-terminate the annex. */
362 while (*buf
&& *buf
!= ':')
372 /* Write the response to a successful qXfer read. Returns the
373 length of the (binary) data stored in BUF, corresponding
374 to as much of DATA/LEN as we could fit. IS_MORE controls
375 the first character of the response. */
377 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
386 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
390 /* Handle btrace enabling in BTS format. */
393 handle_btrace_enable_bts (struct thread_info
*thread
)
395 if (thread
->btrace
!= NULL
)
396 return "E.Btrace already enabled.";
398 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
399 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
400 ¤t_btrace_conf
);
401 if (thread
->btrace
== NULL
)
402 return "E.Could not enable btrace.";
407 /* Handle btrace disabling. */
410 handle_btrace_disable (struct thread_info
*thread
)
413 if (thread
->btrace
== NULL
)
414 return "E.Branch tracing not enabled.";
416 if (target_disable_btrace (thread
->btrace
) != 0)
417 return "E.Could not disable branch tracing.";
419 thread
->btrace
= NULL
;
423 /* Handle the "Qbtrace" packet. */
426 handle_btrace_general_set (char *own_buf
)
428 struct thread_info
*thread
;
432 if (strncmp ("Qbtrace:", own_buf
, strlen ("Qbtrace:")) != 0)
435 op
= own_buf
+ strlen ("Qbtrace:");
437 if (ptid_equal (general_thread
, null_ptid
)
438 || ptid_equal (general_thread
, minus_one_ptid
))
440 strcpy (own_buf
, "E.Must select a single thread.");
444 thread
= find_thread_ptid (general_thread
);
447 strcpy (own_buf
, "E.No such thread.");
453 if (strcmp (op
, "bts") == 0)
454 err
= handle_btrace_enable_bts (thread
);
455 else if (strcmp (op
, "off") == 0)
456 err
= handle_btrace_disable (thread
);
458 err
= "E.Bad Qbtrace operation. Use bts or off.";
461 strcpy (own_buf
, err
);
468 /* Handle the "Qbtrace-conf" packet. */
471 handle_btrace_conf_general_set (char *own_buf
)
473 struct thread_info
*thread
;
476 if (strncmp ("Qbtrace-conf:", own_buf
, strlen ("Qbtrace-conf:")) != 0)
479 op
= own_buf
+ strlen ("Qbtrace-conf:");
481 if (ptid_equal (general_thread
, null_ptid
)
482 || ptid_equal (general_thread
, minus_one_ptid
))
484 strcpy (own_buf
, "E.Must select a single thread.");
488 thread
= find_thread_ptid (general_thread
);
491 strcpy (own_buf
, "E.No such thread.");
495 if (strncmp (op
, "bts:size=", strlen ("bts:size=")) == 0)
501 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
502 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
504 strcpy (own_buf
, "E.Bad size value.");
508 current_btrace_conf
.bts
.size
= (unsigned int) size
;
512 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
520 /* Handle all of the extended 'Q' packets. */
523 handle_general_set (char *own_buf
)
525 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
527 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
528 const char *p
= own_buf
+ strlen ("QPassSignals:");
531 p
= decode_address_to_semicolon (&cursig
, p
);
532 for (i
= 0; i
< numsigs
; i
++)
538 /* Keep looping, to clear the remaining signals. */
541 p
= decode_address_to_semicolon (&cursig
, p
);
546 strcpy (own_buf
, "OK");
550 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
552 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
553 const char *p
= own_buf
+ strlen ("QProgramSignals:");
556 program_signals_p
= 1;
558 p
= decode_address_to_semicolon (&cursig
, p
);
559 for (i
= 0; i
< numsigs
; i
++)
563 program_signals
[i
] = 1;
565 /* Keep looping, to clear the remaining signals. */
568 p
= decode_address_to_semicolon (&cursig
, p
);
571 program_signals
[i
] = 0;
573 strcpy (own_buf
, "OK");
577 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
581 fprintf (stderr
, "[noack mode enabled]\n");
590 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
592 char *mode
= own_buf
+ 9;
596 if (strcmp (mode
, "0") == 0)
598 else if (strcmp (mode
, "1") == 0)
602 /* We don't know what this mode is, so complain to
604 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
610 req_str
= req
? "non-stop" : "all-stop";
611 if (start_non_stop (req
) != 0)
613 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
621 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
627 if (strncmp ("QDisableRandomization:", own_buf
,
628 strlen ("QDisableRandomization:")) == 0)
630 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
633 unpack_varlen_hex (packet
, &setting
);
634 disable_randomization
= setting
;
638 if (disable_randomization
)
639 fprintf (stderr
, "[address space randomization disabled]\n");
641 fprintf (stderr
, "[address space randomization enabled]\n");
648 if (target_supports_tracepoints ()
649 && handle_tracepoint_general_set (own_buf
))
652 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
654 char *mode
= own_buf
+ strlen ("QAgent:");
657 if (strcmp (mode
, "0") == 0)
659 else if (strcmp (mode
, "1") == 0)
663 /* We don't know what this value is, so complain to GDB. */
664 sprintf (own_buf
, "E.Unknown QAgent value");
668 /* Update the flag. */
671 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
676 if (handle_btrace_general_set (own_buf
))
679 if (handle_btrace_conf_general_set (own_buf
))
682 /* Otherwise we didn't know what packet it was. Say we didn't
688 get_features_xml (const char *annex
)
690 const struct target_desc
*desc
= current_target_desc ();
692 /* `desc->xmltarget' defines what to return when looking for the
693 "target.xml" file. Its contents can either be verbatim XML code
694 (prefixed with a '@') or else the name of the actual XML file to
695 be used in place of "target.xml".
697 This variable is set up from the auto-generated
698 init_registers_... routine for the current target. */
700 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
702 if (*desc
->xmltarget
== '@')
703 return desc
->xmltarget
+ 1;
705 annex
= desc
->xmltarget
;
710 extern const char *const xml_builtin
[][2];
713 /* Look for the annex. */
714 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
715 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
718 if (xml_builtin
[i
][0] != NULL
)
719 return xml_builtin
[i
][1];
727 monitor_show_help (void)
729 monitor_output ("The following monitor commands are supported:\n");
730 monitor_output (" set debug <0|1>\n");
731 monitor_output (" Enable general debugging messages\n");
732 monitor_output (" set debug-hw-points <0|1>\n");
733 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
734 monitor_output (" set remote-debug <0|1>\n");
735 monitor_output (" Enable remote protocol debugging messages\n");
736 monitor_output (" set debug-format option1[,option2,...]\n");
737 monitor_output (" Add additional information to debugging messages\n");
738 monitor_output (" Options: all, none");
739 monitor_output (", timestamp");
740 monitor_output ("\n");
741 monitor_output (" exit\n");
742 monitor_output (" Quit GDBserver\n");
745 /* Read trace frame or inferior memory. Returns the number of bytes
746 actually read, zero when no further transfer is possible, and -1 on
747 error. Return of a positive value smaller than LEN does not
748 indicate there's no more to be read, only the end of the transfer.
749 E.g., when GDB reads memory from a traceframe, a first request may
750 be served from a memory block that does not cover the whole request
751 length. A following request gets the rest served from either
752 another block (of the same traceframe) or from the read-only
756 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
760 if (current_traceframe
>= 0)
763 ULONGEST length
= len
;
765 if (traceframe_read_mem (current_traceframe
,
766 memaddr
, myaddr
, len
, &nbytes
))
768 /* Data read from trace buffer, we're done. */
771 if (!in_readonly_region (memaddr
, length
))
773 /* Otherwise we have a valid readonly case, fall through. */
774 /* (assume no half-trace half-real blocks for now) */
777 res
= prepare_to_access_memory ();
780 res
= read_inferior_memory (memaddr
, myaddr
, len
);
781 done_accessing_memory ();
783 return res
== 0 ? len
: -1;
789 /* Write trace frame or inferior memory. Actually, writing to trace
790 frames is forbidden. */
793 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
795 if (current_traceframe
>= 0)
801 ret
= prepare_to_access_memory ();
804 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
805 done_accessing_memory ();
811 /* Subroutine of handle_search_memory to simplify it. */
814 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
815 gdb_byte
*pattern
, unsigned pattern_len
,
816 gdb_byte
*search_buf
,
817 unsigned chunk_size
, unsigned search_buf_size
,
818 CORE_ADDR
*found_addrp
)
820 /* Prime the search buffer. */
822 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
825 warning ("Unable to access %ld bytes of target "
826 "memory at 0x%lx, halting search.",
827 (long) search_buf_size
, (long) start_addr
);
831 /* Perform the search.
833 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
834 When we've scanned N bytes we copy the trailing bytes to the start and
835 read in another N bytes. */
837 while (search_space_len
>= pattern_len
)
840 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
844 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
846 if (found_ptr
!= NULL
)
848 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
849 *found_addrp
= found_addr
;
853 /* Not found in this chunk, skip to next chunk. */
855 /* Don't let search_space_len wrap here, it's unsigned. */
856 if (search_space_len
>= chunk_size
)
857 search_space_len
-= chunk_size
;
859 search_space_len
= 0;
861 if (search_space_len
>= pattern_len
)
863 unsigned keep_len
= search_buf_size
- chunk_size
;
864 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
867 /* Copy the trailing part of the previous iteration to the front
868 of the buffer for the next iteration. */
869 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
871 nr_to_read
= (search_space_len
- keep_len
< chunk_size
872 ? search_space_len
- keep_len
875 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
876 nr_to_read
) != search_buf_size
)
878 warning ("Unable to access %ld bytes of target memory "
879 "at 0x%lx, halting search.",
880 (long) nr_to_read
, (long) read_addr
);
884 start_addr
+= chunk_size
;
893 /* Handle qSearch:memory packets. */
896 handle_search_memory (char *own_buf
, int packet_len
)
898 CORE_ADDR start_addr
;
899 CORE_ADDR search_space_len
;
901 unsigned int pattern_len
;
902 /* NOTE: also defined in find.c testcase. */
903 #define SEARCH_CHUNK_SIZE 16000
904 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
905 /* Buffer to hold memory contents for searching. */
906 gdb_byte
*search_buf
;
907 unsigned search_buf_size
;
909 CORE_ADDR found_addr
;
910 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
912 pattern
= malloc (packet_len
);
915 error ("Unable to allocate memory to perform the search");
916 strcpy (own_buf
, "E00");
919 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
920 packet_len
- cmd_name_len
,
921 &start_addr
, &search_space_len
,
922 pattern
, &pattern_len
) < 0)
925 error ("Error in parsing qSearch:memory packet");
926 strcpy (own_buf
, "E00");
930 search_buf_size
= chunk_size
+ pattern_len
- 1;
932 /* No point in trying to allocate a buffer larger than the search space. */
933 if (search_space_len
< search_buf_size
)
934 search_buf_size
= search_space_len
;
936 search_buf
= malloc (search_buf_size
);
937 if (search_buf
== NULL
)
940 error ("Unable to allocate memory to perform the search");
941 strcpy (own_buf
, "E00");
945 found
= handle_search_memory_1 (start_addr
, search_space_len
,
946 pattern
, pattern_len
,
947 search_buf
, chunk_size
, search_buf_size
,
951 sprintf (own_buf
, "1,%lx", (long) found_addr
);
953 strcpy (own_buf
, "0");
955 strcpy (own_buf
, "E00");
961 #define require_running(BUF) \
962 if (!target_running ()) \
968 /* Parse options to --debug-format= and "monitor set debug-format".
969 ARG is the text after "--debug-format=" or "monitor set debug-format".
970 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
971 This triggers calls to monitor_output.
972 The result is NULL if all options were parsed ok, otherwise an error
973 message which the caller must free.
975 N.B. These commands affect all debug format settings, they are not
976 cumulative. If a format is not specified, it is turned off.
977 However, we don't go to extra trouble with things like
978 "monitor set debug-format all,none,timestamp".
979 Instead we just parse them one at a time, in order.
981 The syntax for "monitor set debug" we support here is not identical
982 to gdb's "set debug foo on|off" because we also use this function to
983 parse "--debug-format=foo,bar". */
986 parse_debug_format_options (const char *arg
, int is_monitor
)
988 VEC (char_ptr
) *options
;
992 /* First turn all debug format options off. */
995 /* First remove leading spaces, for "monitor set debug-format". */
996 while (isspace (*arg
))
999 options
= delim_string_to_char_ptr_vec (arg
, ',');
1001 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1003 if (strcmp (option
, "all") == 0)
1005 debug_timestamp
= 1;
1007 monitor_output ("All extra debug format options enabled.\n");
1009 else if (strcmp (option
, "none") == 0)
1011 debug_timestamp
= 0;
1013 monitor_output ("All extra debug format options disabled.\n");
1015 else if (strcmp (option
, "timestamp") == 0)
1017 debug_timestamp
= 1;
1019 monitor_output ("Timestamps will be added to debug output.\n");
1021 else if (*option
== '\0')
1023 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1028 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1031 free_char_ptr_vec (options
);
1036 free_char_ptr_vec (options
);
1040 /* Handle monitor commands not handled by target-specific handlers. */
1043 handle_monitor_command (char *mon
, char *own_buf
)
1045 if (strcmp (mon
, "set debug 1") == 0)
1048 monitor_output ("Debug output enabled.\n");
1050 else if (strcmp (mon
, "set debug 0") == 0)
1053 monitor_output ("Debug output disabled.\n");
1055 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1057 show_debug_regs
= 1;
1058 monitor_output ("H/W point debugging output enabled.\n");
1060 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1062 show_debug_regs
= 0;
1063 monitor_output ("H/W point debugging output disabled.\n");
1065 else if (strcmp (mon
, "set remote-debug 1") == 0)
1068 monitor_output ("Protocol debug output enabled.\n");
1070 else if (strcmp (mon
, "set remote-debug 0") == 0)
1073 monitor_output ("Protocol debug output disabled.\n");
1075 else if (strncmp (mon
, "set debug-format ",
1076 sizeof ("set debug-format ") - 1) == 0)
1079 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1082 if (error_msg
!= NULL
)
1084 monitor_output (error_msg
);
1085 monitor_show_help ();
1086 write_enn (own_buf
);
1090 else if (strcmp (mon
, "help") == 0)
1091 monitor_show_help ();
1092 else if (strcmp (mon
, "exit") == 0)
1096 monitor_output ("Unknown monitor command.\n\n");
1097 monitor_show_help ();
1098 write_enn (own_buf
);
1102 /* Associates a callback with each supported qXfer'able object. */
1106 /* The object this handler handles. */
1109 /* Request that the target transfer up to LEN 8-bit bytes of the
1110 target's OBJECT. The OFFSET, for a seekable object, specifies
1111 the starting point. The ANNEX can be used to provide additional
1112 data-specific information to the target.
1114 Return the number of bytes actually transfered, zero when no
1115 further transfer is possible, -1 on error, -2 when the transfer
1116 is not supported, and -3 on a verbose error message that should
1117 be preserved. Return of a positive value smaller than LEN does
1118 not indicate the end of the object, only the end of the transfer.
1120 One, and only one, of readbuf or writebuf must be non-NULL. */
1121 int (*xfer
) (const char *annex
,
1122 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1123 ULONGEST offset
, LONGEST len
);
1126 /* Handle qXfer:auxv:read. */
1129 handle_qxfer_auxv (const char *annex
,
1130 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1131 ULONGEST offset
, LONGEST len
)
1133 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1136 if (annex
[0] != '\0' || !target_running ())
1139 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1142 /* Handle qXfer:features:read. */
1145 handle_qxfer_features (const char *annex
,
1146 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1147 ULONGEST offset
, LONGEST len
)
1149 const char *document
;
1152 if (writebuf
!= NULL
)
1155 if (!target_running ())
1158 /* Grab the correct annex. */
1159 document
= get_features_xml (annex
);
1160 if (document
== NULL
)
1163 total_len
= strlen (document
);
1165 if (offset
> total_len
)
1168 if (offset
+ len
> total_len
)
1169 len
= total_len
- offset
;
1171 memcpy (readbuf
, document
+ offset
, len
);
1175 /* Worker routine for handle_qxfer_libraries.
1176 Add to the length pointed to by ARG a conservative estimate of the
1177 length needed to transmit the file name of INF. */
1180 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1182 struct dll_info
*dll
= (struct dll_info
*) inf
;
1183 unsigned int *total_len
= arg
;
1185 /* Over-estimate the necessary memory. Assume that every character
1186 in the library name must be escaped. */
1187 *total_len
+= 128 + 6 * strlen (dll
->name
);
1190 /* Worker routine for handle_qxfer_libraries.
1191 Emit the XML to describe the library in INF. */
1194 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1196 struct dll_info
*dll
= (struct dll_info
*) inf
;
1201 strcpy (p
, " <library name=\"");
1203 name
= xml_escape_text (dll
->name
);
1207 strcpy (p
, "\"><segment address=\"");
1209 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1211 strcpy (p
, "\"/></library>\n");
1217 /* Handle qXfer:libraries:read. */
1220 handle_qxfer_libraries (const char *annex
,
1221 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1222 ULONGEST offset
, LONGEST len
)
1224 unsigned int total_len
;
1227 if (writebuf
!= NULL
)
1230 if (annex
[0] != '\0' || !target_running ())
1234 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1237 document
= malloc (total_len
);
1238 if (document
== NULL
)
1241 strcpy (document
, "<library-list>\n");
1242 p
= document
+ strlen (document
);
1244 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1246 strcpy (p
, "</library-list>\n");
1248 total_len
= strlen (document
);
1250 if (offset
> total_len
)
1256 if (offset
+ len
> total_len
)
1257 len
= total_len
- offset
;
1259 memcpy (readbuf
, document
+ offset
, len
);
1264 /* Handle qXfer:libraries-svr4:read. */
1267 handle_qxfer_libraries_svr4 (const char *annex
,
1268 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1269 ULONGEST offset
, LONGEST len
)
1271 if (writebuf
!= NULL
)
1274 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1277 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1280 /* Handle qXfer:osadata:read. */
1283 handle_qxfer_osdata (const char *annex
,
1284 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1285 ULONGEST offset
, LONGEST len
)
1287 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1290 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1293 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1296 handle_qxfer_siginfo (const char *annex
,
1297 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1298 ULONGEST offset
, LONGEST len
)
1300 if (the_target
->qxfer_siginfo
== NULL
)
1303 if (annex
[0] != '\0' || !target_running ())
1306 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1309 /* Handle qXfer:spu:read and qXfer:spu:write. */
1312 handle_qxfer_spu (const char *annex
,
1313 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1314 ULONGEST offset
, LONGEST len
)
1316 if (the_target
->qxfer_spu
== NULL
)
1319 if (!target_running ())
1322 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1325 /* Handle qXfer:statictrace:read. */
1328 handle_qxfer_statictrace (const char *annex
,
1329 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1330 ULONGEST offset
, LONGEST len
)
1334 if (writebuf
!= NULL
)
1337 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1340 if (traceframe_read_sdata (current_traceframe
, offset
,
1341 readbuf
, len
, &nbytes
))
1346 /* Helper for handle_qxfer_threads_proper.
1347 Emit the XML to describe the thread of INF. */
1350 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1352 struct thread_info
*thread
= (struct thread_info
*) inf
;
1353 struct buffer
*buffer
= arg
;
1354 ptid_t ptid
= thread_to_gdb_id (thread
);
1356 int core
= target_core_of_thread (ptid
);
1359 write_ptid (ptid_s
, ptid
);
1363 sprintf (core_s
, "%d", core
);
1364 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1369 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1374 /* Helper for handle_qxfer_threads. */
1377 handle_qxfer_threads_proper (struct buffer
*buffer
)
1379 buffer_grow_str (buffer
, "<threads>\n");
1381 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1384 buffer_grow_str0 (buffer
, "</threads>\n");
1387 /* Handle qXfer:threads:read. */
1390 handle_qxfer_threads (const char *annex
,
1391 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1392 ULONGEST offset
, LONGEST len
)
1394 static char *result
= 0;
1395 static unsigned int result_length
= 0;
1397 if (writebuf
!= NULL
)
1400 if (!target_running () || annex
[0] != '\0')
1405 struct buffer buffer
;
1406 /* When asked for data at offset 0, generate everything and store into
1407 'result'. Successive reads will be served off 'result'. */
1411 buffer_init (&buffer
);
1413 handle_qxfer_threads_proper (&buffer
);
1415 result
= buffer_finish (&buffer
);
1416 result_length
= strlen (result
);
1417 buffer_free (&buffer
);
1420 if (offset
>= result_length
)
1422 /* We're out of data. */
1429 if (len
> result_length
- offset
)
1430 len
= result_length
- offset
;
1432 memcpy (readbuf
, result
+ offset
, len
);
1437 /* Handle qXfer:traceframe-info:read. */
1440 handle_qxfer_traceframe_info (const char *annex
,
1441 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1442 ULONGEST offset
, LONGEST len
)
1444 static char *result
= 0;
1445 static unsigned int result_length
= 0;
1447 if (writebuf
!= NULL
)
1450 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1455 struct buffer buffer
;
1457 /* When asked for data at offset 0, generate everything and
1458 store into 'result'. Successive reads will be served off
1462 buffer_init (&buffer
);
1464 traceframe_read_info (current_traceframe
, &buffer
);
1466 result
= buffer_finish (&buffer
);
1467 result_length
= strlen (result
);
1468 buffer_free (&buffer
);
1471 if (offset
>= result_length
)
1473 /* We're out of data. */
1480 if (len
> result_length
- offset
)
1481 len
= result_length
- offset
;
1483 memcpy (readbuf
, result
+ offset
, len
);
1487 /* Handle qXfer:fdpic:read. */
1490 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1491 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1493 if (the_target
->read_loadmap
== NULL
)
1496 if (!target_running ())
1499 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1502 /* Handle qXfer:btrace:read. */
1505 handle_qxfer_btrace (const char *annex
,
1506 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1507 ULONGEST offset
, LONGEST len
)
1509 static struct buffer cache
;
1510 struct thread_info
*thread
;
1513 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1516 if (!target_running ())
1519 if (ptid_equal (general_thread
, null_ptid
)
1520 || ptid_equal (general_thread
, minus_one_ptid
))
1522 strcpy (own_buf
, "E.Must select a single thread.");
1526 thread
= find_thread_ptid (general_thread
);
1529 strcpy (own_buf
, "E.No such thread.");
1533 if (thread
->btrace
== NULL
)
1535 strcpy (own_buf
, "E.Btrace not enabled.");
1539 if (strcmp (annex
, "all") == 0)
1540 type
= BTRACE_READ_ALL
;
1541 else if (strcmp (annex
, "new") == 0)
1542 type
= BTRACE_READ_NEW
;
1543 else if (strcmp (annex
, "delta") == 0)
1544 type
= BTRACE_READ_DELTA
;
1547 strcpy (own_buf
, "E.Bad annex.");
1553 buffer_free (&cache
);
1555 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1558 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1562 else if (offset
> cache
.used_size
)
1564 buffer_free (&cache
);
1568 if (len
> cache
.used_size
- offset
)
1569 len
= cache
.used_size
- offset
;
1571 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1576 /* Handle qXfer:btrace-conf:read. */
1579 handle_qxfer_btrace_conf (const char *annex
,
1580 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1581 ULONGEST offset
, LONGEST len
)
1583 static struct buffer cache
;
1584 struct thread_info
*thread
;
1587 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1590 if (annex
[0] != '\0' || !target_running ())
1593 if (ptid_equal (general_thread
, null_ptid
)
1594 || ptid_equal (general_thread
, minus_one_ptid
))
1596 strcpy (own_buf
, "E.Must select a single thread.");
1600 thread
= find_thread_ptid (general_thread
);
1603 strcpy (own_buf
, "E.No such thread.");
1607 if (thread
->btrace
== NULL
)
1609 strcpy (own_buf
, "E.Btrace not enabled.");
1615 buffer_free (&cache
);
1617 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1620 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1624 else if (offset
> cache
.used_size
)
1626 buffer_free (&cache
);
1630 if (len
> cache
.used_size
- offset
)
1631 len
= cache
.used_size
- offset
;
1633 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1638 static const struct qxfer qxfer_packets
[] =
1640 { "auxv", handle_qxfer_auxv
},
1641 { "btrace", handle_qxfer_btrace
},
1642 { "btrace-conf", handle_qxfer_btrace_conf
},
1643 { "fdpic", handle_qxfer_fdpic
},
1644 { "features", handle_qxfer_features
},
1645 { "libraries", handle_qxfer_libraries
},
1646 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1647 { "osdata", handle_qxfer_osdata
},
1648 { "siginfo", handle_qxfer_siginfo
},
1649 { "spu", handle_qxfer_spu
},
1650 { "statictrace", handle_qxfer_statictrace
},
1651 { "threads", handle_qxfer_threads
},
1652 { "traceframe-info", handle_qxfer_traceframe_info
},
1656 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1664 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1667 /* Grab the object, r/w and annex. */
1668 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1670 write_enn (own_buf
);
1675 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1678 const struct qxfer
*q
= &qxfer_packets
[i
];
1680 if (strcmp (object
, q
->object
) == 0)
1682 if (strcmp (rw
, "read") == 0)
1684 unsigned char *data
;
1689 /* Grab the offset and length. */
1690 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1692 write_enn (own_buf
);
1696 /* Read one extra byte, as an indicator of whether there is
1698 if (len
> PBUFSIZ
- 2)
1700 data
= malloc (len
+ 1);
1703 write_enn (own_buf
);
1706 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1714 /* Preserve error message. */
1717 write_enn (own_buf
);
1719 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1721 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1726 else if (strcmp (rw
, "write") == 0)
1731 unsigned char *data
;
1733 strcpy (own_buf
, "E00");
1734 data
= malloc (packet_len
- (offset
- own_buf
));
1737 write_enn (own_buf
);
1740 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1741 &ofs
, &len
, data
) < 0)
1744 write_enn (own_buf
);
1748 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1756 /* Preserve error message. */
1759 write_enn (own_buf
);
1761 sprintf (own_buf
, "%x", n
);
1774 /* Table used by the crc32 function to calcuate the checksum. */
1776 static unsigned int crc32_table
[256] =
1779 /* Compute 32 bit CRC from inferior memory.
1781 On success, return 32 bit CRC.
1782 On failure, return (unsigned long long) -1. */
1784 static unsigned long long
1785 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1787 if (!crc32_table
[1])
1789 /* Initialize the CRC table and the decoding table. */
1793 for (i
= 0; i
< 256; i
++)
1795 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1796 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1803 unsigned char byte
= 0;
1805 /* Return failure if memory read fails. */
1806 if (read_inferior_memory (base
, &byte
, 1) != 0)
1807 return (unsigned long long) -1;
1809 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1812 return (unsigned long long) crc
;
1815 /* Add supported btrace packets to BUF. */
1818 supported_btrace_packets (char *buf
)
1820 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1822 strcat (buf
, ";Qbtrace:bts+");
1823 strcat (buf
, ";Qbtrace-conf:bts:size+");
1828 strcat (buf
, ";Qbtrace:off+");
1829 strcat (buf
, ";qXfer:btrace:read+");
1830 strcat (buf
, ";qXfer:btrace-conf:read+");
1833 /* Handle all of the extended 'q' packets. */
1836 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1838 static struct inferior_list_entry
*thread_ptr
;
1840 /* Reply the current thread id. */
1841 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1844 require_running (own_buf
);
1846 if (!ptid_equal (general_thread
, null_ptid
)
1847 && !ptid_equal (general_thread
, minus_one_ptid
))
1848 gdb_id
= general_thread
;
1851 thread_ptr
= get_first_inferior (&all_threads
);
1852 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1855 sprintf (own_buf
, "QC");
1857 write_ptid (own_buf
, gdb_id
);
1861 if (strcmp ("qSymbol::", own_buf
) == 0)
1863 /* GDB is suggesting new symbols have been loaded. This may
1864 mean a new shared library has been detected as loaded, so
1865 take the opportunity to check if breakpoints we think are
1866 inserted, still are. Note that it isn't guaranteed that
1867 we'll see this when a shared library is loaded, and nor will
1868 we see this for unloads (although breakpoints in unloaded
1869 libraries shouldn't trigger), as GDB may not find symbols for
1870 the library at all. We also re-validate breakpoints when we
1871 see a second GDB breakpoint for the same address, and or when
1872 we access breakpoint shadows. */
1873 validate_breakpoints ();
1875 if (target_supports_tracepoints ())
1876 tracepoint_look_up_symbols ();
1878 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1879 (*the_target
->look_up_symbols
) ();
1881 strcpy (own_buf
, "OK");
1885 if (!disable_packet_qfThreadInfo
)
1887 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1891 require_running (own_buf
);
1892 thread_ptr
= get_first_inferior (&all_threads
);
1895 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1896 write_ptid (own_buf
, gdb_id
);
1897 thread_ptr
= thread_ptr
->next
;
1901 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1905 require_running (own_buf
);
1906 if (thread_ptr
!= NULL
)
1909 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1910 write_ptid (own_buf
, gdb_id
);
1911 thread_ptr
= thread_ptr
->next
;
1916 sprintf (own_buf
, "l");
1922 if (the_target
->read_offsets
!= NULL
1923 && strcmp ("qOffsets", own_buf
) == 0)
1925 CORE_ADDR text
, data
;
1927 require_running (own_buf
);
1928 if (the_target
->read_offsets (&text
, &data
))
1929 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1930 (long)text
, (long)data
, (long)data
);
1932 write_enn (own_buf
);
1937 /* Protocol features query. */
1938 if (strncmp ("qSupported", own_buf
, 10) == 0
1939 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1941 char *p
= &own_buf
[10];
1942 int gdb_supports_qRelocInsn
= 0;
1944 /* Start processing qSupported packet. */
1945 target_process_qsupported (NULL
);
1947 /* Process each feature being provided by GDB. The first
1948 feature will follow a ':', and latter features will follow
1952 char **qsupported
= NULL
;
1956 /* Two passes, to avoid nested strtok calls in
1957 target_process_qsupported. */
1958 for (p
= strtok (p
+ 1, ";");
1960 p
= strtok (NULL
, ";"))
1963 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1964 qsupported
[count
- 1] = xstrdup (p
);
1967 for (i
= 0; i
< count
; i
++)
1970 if (strcmp (p
, "multiprocess+") == 0)
1972 /* GDB supports and wants multi-process support if
1974 if (target_supports_multi_process ())
1977 else if (strcmp (p
, "qRelocInsn+") == 0)
1979 /* GDB supports relocate instruction requests. */
1980 gdb_supports_qRelocInsn
= 1;
1982 else if (strcmp (p
, "swbreak+") == 0)
1984 /* GDB wants us to report whether a trap is caused
1985 by a software breakpoint and for us to handle PC
1986 adjustment if necessary on this target. */
1987 if (target_supports_stopped_by_sw_breakpoint ())
1988 swbreak_feature
= 1;
1990 else if (strcmp (p
, "hwbreak+") == 0)
1992 /* GDB wants us to report whether a trap is caused
1993 by a hardware breakpoint. */
1994 if (target_supports_stopped_by_hw_breakpoint ())
1995 hwbreak_feature
= 1;
1998 target_process_qsupported (p
);
2007 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2010 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2011 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2012 ";augmented-libraries-svr4-read+");
2015 /* We do not have any hook to indicate whether the non-SVR4 target
2016 backend supports qXfer:libraries:read, so always report it. */
2017 strcat (own_buf
, ";qXfer:libraries:read+");
2020 if (the_target
->read_auxv
!= NULL
)
2021 strcat (own_buf
, ";qXfer:auxv:read+");
2023 if (the_target
->qxfer_spu
!= NULL
)
2024 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2026 if (the_target
->qxfer_siginfo
!= NULL
)
2027 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2029 if (the_target
->read_loadmap
!= NULL
)
2030 strcat (own_buf
, ";qXfer:fdpic:read+");
2032 /* We always report qXfer:features:read, as targets may
2033 install XML files on a subsequent call to arch_setup.
2034 If we reported to GDB on startup that we don't support
2035 qXfer:feature:read at all, we will never be re-queried. */
2036 strcat (own_buf
, ";qXfer:features:read+");
2038 if (transport_is_reliable
)
2039 strcat (own_buf
, ";QStartNoAckMode+");
2041 if (the_target
->qxfer_osdata
!= NULL
)
2042 strcat (own_buf
, ";qXfer:osdata:read+");
2044 if (target_supports_multi_process ())
2045 strcat (own_buf
, ";multiprocess+");
2047 if (target_supports_non_stop ())
2048 strcat (own_buf
, ";QNonStop+");
2050 if (target_supports_disable_randomization ())
2051 strcat (own_buf
, ";QDisableRandomization+");
2053 strcat (own_buf
, ";qXfer:threads:read+");
2055 if (target_supports_tracepoints ())
2057 strcat (own_buf
, ";ConditionalTracepoints+");
2058 strcat (own_buf
, ";TraceStateVariables+");
2059 strcat (own_buf
, ";TracepointSource+");
2060 strcat (own_buf
, ";DisconnectedTracing+");
2061 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2062 strcat (own_buf
, ";FastTracepoints+");
2063 strcat (own_buf
, ";StaticTracepoints+");
2064 strcat (own_buf
, ";InstallInTrace+");
2065 strcat (own_buf
, ";qXfer:statictrace:read+");
2066 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2067 strcat (own_buf
, ";EnableDisableTracepoints+");
2068 strcat (own_buf
, ";QTBuffer:size+");
2069 strcat (own_buf
, ";tracenz+");
2072 /* Support target-side breakpoint conditions and commands. */
2073 strcat (own_buf
, ";ConditionalBreakpoints+");
2074 strcat (own_buf
, ";BreakpointCommands+");
2076 if (target_supports_agent ())
2077 strcat (own_buf
, ";QAgent+");
2079 supported_btrace_packets (own_buf
);
2081 if (target_supports_stopped_by_sw_breakpoint ())
2082 strcat (own_buf
, ";swbreak+");
2084 if (target_supports_stopped_by_hw_breakpoint ())
2085 strcat (own_buf
, ";hwbreak+");
2090 /* Thread-local storage support. */
2091 if (the_target
->get_tls_address
!= NULL
2092 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
2094 char *p
= own_buf
+ 12;
2095 CORE_ADDR parts
[2], address
= 0;
2097 ptid_t ptid
= null_ptid
;
2099 require_running (own_buf
);
2101 for (i
= 0; i
< 3; i
++)
2109 p2
= strchr (p
, ',');
2122 ptid
= read_ptid (p
, NULL
);
2124 decode_address (&parts
[i
- 1], p
, len
);
2128 if (p
!= NULL
|| i
< 3)
2132 struct thread_info
*thread
= find_thread_ptid (ptid
);
2137 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2143 strcpy (own_buf
, paddress(address
));
2148 write_enn (own_buf
);
2152 /* Otherwise, pretend we do not understand this packet. */
2155 /* Windows OS Thread Information Block address support. */
2156 if (the_target
->get_tib_address
!= NULL
2157 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
2162 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2164 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2167 strcpy (own_buf
, paddress(tlb
));
2172 write_enn (own_buf
);
2178 /* Handle "monitor" commands. */
2179 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
2181 char *mon
= malloc (PBUFSIZ
);
2182 int len
= strlen (own_buf
+ 6);
2186 write_enn (own_buf
);
2191 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2193 write_enn (own_buf
);
2197 mon
[len
/ 2] = '\0';
2201 if (the_target
->handle_monitor_command
== NULL
2202 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2203 /* Default processing. */
2204 handle_monitor_command (mon
, own_buf
);
2210 if (strncmp ("qSearch:memory:", own_buf
,
2211 sizeof ("qSearch:memory:") - 1) == 0)
2213 require_running (own_buf
);
2214 handle_search_memory (own_buf
, packet_len
);
2218 if (strcmp (own_buf
, "qAttached") == 0
2219 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
2221 struct process_info
*process
;
2223 if (own_buf
[sizeof ("qAttached") - 1])
2225 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2226 process
= (struct process_info
*)
2227 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2231 require_running (own_buf
);
2232 process
= current_process ();
2235 if (process
== NULL
)
2237 write_enn (own_buf
);
2241 strcpy (own_buf
, process
->attached
? "1" : "0");
2245 if (strncmp ("qCRC:", own_buf
, 5) == 0)
2247 /* CRC check (compare-section). */
2251 unsigned long long crc
;
2253 require_running (own_buf
);
2254 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2255 if (*comma
++ != ',')
2257 write_enn (own_buf
);
2260 len
= strtoul (comma
, NULL
, 16);
2261 crc
= crc32 (base
, len
, 0xffffffff);
2262 /* Check for memory failure. */
2263 if (crc
== (unsigned long long) -1)
2265 write_enn (own_buf
);
2268 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2272 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2275 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2278 /* Otherwise we didn't know what packet it was. Say we didn't
2283 static void gdb_wants_all_threads_stopped (void);
2284 static void resume (struct thread_resume
*actions
, size_t n
);
2286 /* The callback that is passed to visit_actioned_threads. */
2287 typedef int (visit_actioned_threads_callback_ftype
)
2288 (const struct thread_resume
*, struct thread_info
*);
2290 /* Struct to pass data to visit_actioned_threads. */
2292 struct visit_actioned_threads_data
2294 const struct thread_resume
*actions
;
2296 visit_actioned_threads_callback_ftype
*callback
;
2299 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2300 true if CALLBACK returns true. Returns false if no matching thread
2301 is found or CALLBACK results false.
2302 Note: This function is itself a callback for find_inferior. */
2305 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2307 struct visit_actioned_threads_data
*data
= datap
;
2308 const struct thread_resume
*actions
= data
->actions
;
2309 size_t num_actions
= data
->num_actions
;
2310 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2313 for (i
= 0; i
< num_actions
; i
++)
2315 const struct thread_resume
*action
= &actions
[i
];
2317 if (ptid_equal (action
->thread
, minus_one_ptid
)
2318 || ptid_equal (action
->thread
, entry
->id
)
2319 || ((ptid_get_pid (action
->thread
)
2320 == ptid_get_pid (entry
->id
))
2321 && ptid_get_lwp (action
->thread
) == -1))
2323 struct thread_info
*thread
= (struct thread_info
*) entry
;
2325 if ((*callback
) (action
, thread
))
2333 /* Callback for visit_actioned_threads. If the thread has a pending
2334 status to report, report it now. */
2337 handle_pending_status (const struct thread_resume
*resumption
,
2338 struct thread_info
*thread
)
2340 if (thread
->status_pending_p
)
2342 thread
->status_pending_p
= 0;
2344 last_status
= thread
->last_status
;
2345 last_ptid
= thread
->entry
.id
;
2346 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2352 /* Parse vCont packets. */
2354 handle_v_cont (char *own_buf
)
2358 struct thread_resume
*resume_info
;
2359 struct thread_resume default_action
= {{0}};
2361 /* Count the number of semicolons in the packet. There should be one
2362 for every action. */
2368 p
= strchr (p
, ';');
2371 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2372 if (resume_info
== NULL
)
2380 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2382 if (p
[0] == 's' || p
[0] == 'S')
2383 resume_info
[i
].kind
= resume_step
;
2384 else if (p
[0] == 'r')
2385 resume_info
[i
].kind
= resume_step
;
2386 else if (p
[0] == 'c' || p
[0] == 'C')
2387 resume_info
[i
].kind
= resume_continue
;
2388 else if (p
[0] == 't')
2389 resume_info
[i
].kind
= resume_stop
;
2393 if (p
[0] == 'S' || p
[0] == 'C')
2396 sig
= strtol (p
+ 1, &q
, 16);
2401 if (!gdb_signal_to_host_p (sig
))
2403 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2405 else if (p
[0] == 'r')
2409 p
= unpack_varlen_hex (p
+ 1, &addr
);
2410 resume_info
[i
].step_range_start
= addr
;
2415 p
= unpack_varlen_hex (p
+ 1, &addr
);
2416 resume_info
[i
].step_range_end
= addr
;
2425 resume_info
[i
].thread
= minus_one_ptid
;
2426 default_action
= resume_info
[i
];
2428 /* Note: we don't increment i here, we'll overwrite this entry
2429 the next time through. */
2431 else if (p
[0] == ':')
2433 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2438 if (p
[0] != ';' && p
[0] != 0)
2441 resume_info
[i
].thread
= ptid
;
2448 resume_info
[i
] = default_action
;
2450 set_desired_thread (0);
2452 resume (resume_info
, n
);
2457 write_enn (own_buf
);
2462 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2465 resume (struct thread_resume
*actions
, size_t num_actions
)
2469 /* Check if among the threads that GDB wants actioned, there's
2470 one with a pending status to report. If so, skip actually
2471 resuming/stopping and report the pending event
2473 struct visit_actioned_threads_data data
;
2475 data
.actions
= actions
;
2476 data
.num_actions
= num_actions
;
2477 data
.callback
= handle_pending_status
;
2478 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2484 (*the_target
->resume
) (actions
, num_actions
);
2490 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2492 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2494 /* No proper RSP support for this yet. At least return
2496 sprintf (own_buf
, "E.No unwaited-for children left.");
2497 disable_async_io ();
2501 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2502 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2503 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2504 current_thread
->last_status
= last_status
;
2506 /* From the client's perspective, all-stop mode always stops all
2507 threads implicitly (and the target backend has already done
2508 so by now). Tag all threads as "want-stopped", so we don't
2509 resume them implicitly without the client telling us to. */
2510 gdb_wants_all_threads_stopped ();
2511 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2512 disable_async_io ();
2514 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2515 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2516 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2520 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2522 handle_v_attach (char *own_buf
)
2526 pid
= strtol (own_buf
+ 8, NULL
, 16);
2527 if (pid
!= 0 && attach_inferior (pid
) == 0)
2529 /* Don't report shared library events after attaching, even if
2530 some libraries are preloaded. GDB will always poll the
2531 library list. Avoids the "stopped by shared library event"
2532 notice on the GDB side. */
2537 /* In non-stop, we don't send a resume reply. Stop events
2538 will follow up using the normal notification
2543 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2549 write_enn (own_buf
);
2554 /* Run a new program. Return 1 if successful, 0 if failure. */
2556 handle_v_run (char *own_buf
)
2558 char *p
, *next_p
, **new_argv
;
2562 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2568 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2569 if (new_argv
== NULL
)
2571 write_enn (own_buf
);
2576 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2578 next_p
= strchr (p
, ';');
2580 next_p
= p
+ strlen (p
);
2582 if (i
== 0 && p
== next_p
)
2586 /* FIXME: Fail request if out of memory instead of dying. */
2587 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2588 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2589 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2598 if (new_argv
[0] == NULL
)
2600 /* GDB didn't specify a program to run. Use the program from the
2601 last run with the new argument list. */
2603 if (program_argv
== NULL
)
2605 write_enn (own_buf
);
2606 freeargv (new_argv
);
2610 new_argv
[0] = strdup (program_argv
[0]);
2611 if (new_argv
[0] == NULL
)
2613 write_enn (own_buf
);
2614 freeargv (new_argv
);
2619 /* Free the old argv and install the new one. */
2620 freeargv (program_argv
);
2621 program_argv
= new_argv
;
2623 start_inferior (program_argv
);
2624 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2626 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2628 /* In non-stop, sending a resume reply doesn't set the general
2629 thread, but GDB assumes a vRun sets it (this is so GDB can
2630 query which is the main thread of the new inferior. */
2632 general_thread
= last_ptid
;
2638 write_enn (own_buf
);
2643 /* Kill process. Return 1 if successful, 0 if failure. */
2645 handle_v_kill (char *own_buf
)
2648 char *p
= &own_buf
[6];
2650 pid
= strtol (p
, NULL
, 16);
2653 if (pid
!= 0 && kill_inferior (pid
) == 0)
2655 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2656 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2657 last_ptid
= pid_to_ptid (pid
);
2658 discard_queued_stop_replies (pid
);
2664 write_enn (own_buf
);
2669 /* Handle all of the extended 'v' packets. */
2671 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2673 if (!disable_packet_vCont
)
2675 if (strncmp (own_buf
, "vCont;", 6) == 0)
2677 require_running (own_buf
);
2678 handle_v_cont (own_buf
);
2682 if (strncmp (own_buf
, "vCont?", 6) == 0)
2684 strcpy (own_buf
, "vCont;c;C;s;S;t");
2685 if (target_supports_range_stepping ())
2687 own_buf
= own_buf
+ strlen (own_buf
);
2688 strcpy (own_buf
, ";r");
2694 if (strncmp (own_buf
, "vFile:", 6) == 0
2695 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2698 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2700 if ((!extended_protocol
|| !multi_process
) && target_running ())
2702 fprintf (stderr
, "Already debugging a process\n");
2703 write_enn (own_buf
);
2706 handle_v_attach (own_buf
);
2710 if (strncmp (own_buf
, "vRun;", 5) == 0)
2712 if ((!extended_protocol
|| !multi_process
) && target_running ())
2714 fprintf (stderr
, "Already debugging a process\n");
2715 write_enn (own_buf
);
2718 handle_v_run (own_buf
);
2722 if (strncmp (own_buf
, "vKill;", 6) == 0)
2724 if (!target_running ())
2726 fprintf (stderr
, "No process to kill\n");
2727 write_enn (own_buf
);
2730 handle_v_kill (own_buf
);
2734 if (handle_notif_ack (own_buf
, packet_len
))
2737 /* Otherwise we didn't know what packet it was. Say we didn't
2743 /* Resume thread and wait for another event. In non-stop mode,
2744 don't really wait here, but return immediatelly to the event
2747 myresume (char *own_buf
, int step
, int sig
)
2749 struct thread_resume resume_info
[2];
2751 int valid_cont_thread
;
2753 set_desired_thread (0);
2755 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2756 && !ptid_equal (cont_thread
, minus_one_ptid
));
2758 if (step
|| sig
|| valid_cont_thread
)
2760 resume_info
[0].thread
= current_ptid
;
2762 resume_info
[0].kind
= resume_step
;
2764 resume_info
[0].kind
= resume_continue
;
2765 resume_info
[0].sig
= sig
;
2769 if (!valid_cont_thread
)
2771 resume_info
[n
].thread
= minus_one_ptid
;
2772 resume_info
[n
].kind
= resume_continue
;
2773 resume_info
[n
].sig
= 0;
2777 resume (resume_info
, n
);
2780 /* Callback for for_each_inferior. Make a new stop reply for each
2784 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2786 struct thread_info
*thread
= (struct thread_info
*) entry
;
2788 /* For now, assume targets that don't have this callback also don't
2789 manage the thread's last_status field. */
2790 if (the_target
->thread_stopped
== NULL
)
2792 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2794 new_notif
->ptid
= entry
->id
;
2795 new_notif
->status
= thread
->last_status
;
2796 /* Pass the last stop reply back to GDB, but don't notify
2798 notif_event_enque (¬if_stop
,
2799 (struct notif_event
*) new_notif
);
2803 if (thread_stopped (thread
))
2808 = target_waitstatus_to_string (&thread
->last_status
);
2810 debug_printf ("Reporting thread %s as already stopped with %s\n",
2811 target_pid_to_str (entry
->id
),
2814 xfree (status_string
);
2817 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2819 /* Pass the last stop reply back to GDB, but don't notify
2821 queue_stop_reply (entry
->id
, &thread
->last_status
);
2828 /* Set this inferior threads's state as "want-stopped". We won't
2829 resume this thread until the client gives us another action for
2833 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2835 struct thread_info
*thread
= (struct thread_info
*) entry
;
2837 thread
->last_resume_kind
= resume_stop
;
2839 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2841 /* Most threads are stopped implicitly (all-stop); tag that with
2843 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2844 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2848 /* Set all threads' states as "want-stopped". */
2851 gdb_wants_all_threads_stopped (void)
2853 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2856 /* Clear the gdb_detached flag of every process. */
2859 gdb_reattached_process (struct inferior_list_entry
*entry
)
2861 struct process_info
*process
= (struct process_info
*) entry
;
2863 process
->gdb_detached
= 0;
2866 /* Callback for for_each_inferior. Clear the thread's pending status
2870 clear_pending_status_callback (struct inferior_list_entry
*entry
)
2872 struct thread_info
*thread
= (struct thread_info
*) entry
;
2874 thread
->status_pending_p
= 0;
2877 /* Callback for for_each_inferior. If the thread is stopped with an
2878 interesting event, mark it as having a pending event. */
2881 set_pending_status_callback (struct inferior_list_entry
*entry
)
2883 struct thread_info
*thread
= (struct thread_info
*) entry
;
2885 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
2886 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
2887 /* A breakpoint, watchpoint or finished step from a previous
2888 GDB run isn't considered interesting for a new GDB run.
2889 If we left those pending, the new GDB could consider them
2890 random SIGTRAPs. This leaves out real async traps. We'd
2891 have to peek into the (target-specific) siginfo to
2892 distinguish those. */
2893 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
2894 thread
->status_pending_p
= 1;
2897 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
2898 pending status to report to GDB. */
2901 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
2903 struct thread_info
*thread
= (struct thread_info
*) entry
;
2905 return thread
->status_pending_p
;
2908 /* Status handler for the '?' packet. */
2911 handle_status (char *own_buf
)
2913 /* GDB is connected, don't forward events to the target anymore. */
2914 for_each_inferior (&all_processes
, gdb_reattached_process
);
2916 /* In non-stop mode, we must send a stop reply for each stopped
2917 thread. In all-stop mode, just send one for the first stopped
2922 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2924 /* The first is sent immediatly. OK is sent if there is no
2925 stopped thread, which is the same handling of the vStopped
2926 packet (by design). */
2927 notif_write_event (¬if_stop
, own_buf
);
2931 struct inferior_list_entry
*thread
= NULL
;
2934 stabilize_threads ();
2935 gdb_wants_all_threads_stopped ();
2937 /* We can only report one status, but we might be coming out of
2938 non-stop -- if more than one thread is stopped with
2939 interesting events, leave events for the threads we're not
2940 reporting now pending. They'll be reported the next time the
2941 threads are resumed. Start by marking all interesting events
2943 for_each_inferior (&all_threads
, set_pending_status_callback
);
2945 /* Prefer the last thread that reported an event to GDB (even if
2946 that was a GDB_SIGNAL_TRAP). */
2947 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
2948 && last_status
.kind
!= TARGET_WAITKIND_EXITED
2949 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2950 thread
= find_inferior_id (&all_threads
, last_ptid
);
2952 /* If the last event thread is not found for some reason, look
2953 for some other thread that might have an event to report. */
2955 thread
= find_inferior (&all_threads
,
2956 find_status_pending_thread_callback
, NULL
);
2958 /* If we're still out of luck, simply pick the first thread in
2961 thread
= get_first_inferior (&all_threads
);
2965 struct thread_info
*tp
= (struct thread_info
*) thread
;
2967 /* We're reporting this event, so it's no longer
2969 tp
->status_pending_p
= 0;
2971 /* GDB assumes the current thread is the thread we're
2972 reporting the status for. */
2973 general_thread
= thread
->id
;
2974 set_desired_thread (1);
2976 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2977 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
2980 strcpy (own_buf
, "W00");
2985 gdbserver_version (void)
2987 printf ("GNU gdbserver %s%s\n"
2988 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
2989 "gdbserver is free software, covered by the "
2990 "GNU General Public License.\n"
2991 "This gdbserver was configured as \"%s\"\n",
2992 PKGVERSION
, version
, host_name
);
2996 gdbserver_usage (FILE *stream
)
2998 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2999 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3000 "\tgdbserver [OPTIONS] --multi COMM\n"
3002 "COMM may either be a tty device (for serial debugging), or \n"
3003 "HOST:PORT to listen for a TCP connection.\n"
3006 " --debug Enable general debugging output.\n"
3007 " --debug-format=opt1[,opt2,...]\n"
3008 " Specify extra content in debugging output.\n"
3013 " --remote-debug Enable remote protocol debugging output.\n"
3014 " --version Display version information and exit.\n"
3015 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3016 " --once Exit after the first connection has "
3018 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3019 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3023 gdbserver_show_disableable (FILE *stream
)
3025 fprintf (stream
, "Disableable packets:\n"
3026 " vCont \tAll vCont packets\n"
3027 " qC \tQuerying the current thread\n"
3028 " qfThreadInfo\tThread listing\n"
3029 " Tthread \tPassing the thread specifier in the "
3030 "T stop reply packet\n"
3031 " threads \tAll of the above\n");
3035 #undef require_running
3036 #define require_running(BUF) \
3037 if (!target_running ()) \
3044 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3046 int pid
= * (int *) args
;
3048 if (ptid_get_pid (entry
->id
) == pid
)
3055 kill_inferior_callback (struct inferior_list_entry
*entry
)
3057 struct process_info
*process
= (struct process_info
*) entry
;
3058 int pid
= ptid_get_pid (process
->entry
.id
);
3060 kill_inferior (pid
);
3061 discard_queued_stop_replies (pid
);
3064 /* Callback for for_each_inferior to detach or kill the inferior,
3065 depending on whether we attached to it or not.
3066 We inform the user whether we're detaching or killing the process
3067 as this is only called when gdbserver is about to exit. */
3070 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3072 struct process_info
*process
= (struct process_info
*) entry
;
3073 int pid
= ptid_get_pid (process
->entry
.id
);
3075 if (process
->attached
)
3076 detach_inferior (pid
);
3078 kill_inferior (pid
);
3080 discard_queued_stop_replies (pid
);
3083 /* for_each_inferior callback for detach_or_kill_for_exit to print
3084 the pids of started inferiors. */
3087 print_started_pid (struct inferior_list_entry
*entry
)
3089 struct process_info
*process
= (struct process_info
*) entry
;
3091 if (! process
->attached
)
3093 int pid
= ptid_get_pid (process
->entry
.id
);
3094 fprintf (stderr
, " %d", pid
);
3098 /* for_each_inferior callback for detach_or_kill_for_exit to print
3099 the pids of attached inferiors. */
3102 print_attached_pid (struct inferior_list_entry
*entry
)
3104 struct process_info
*process
= (struct process_info
*) entry
;
3106 if (process
->attached
)
3108 int pid
= ptid_get_pid (process
->entry
.id
);
3109 fprintf (stderr
, " %d", pid
);
3113 /* Call this when exiting gdbserver with possible inferiors that need
3114 to be killed or detached from. */
3117 detach_or_kill_for_exit (void)
3119 /* First print a list of the inferiors we will be killing/detaching.
3120 This is to assist the user, for example, in case the inferior unexpectedly
3121 dies after we exit: did we screw up or did the inferior exit on its own?
3122 Having this info will save some head-scratching. */
3124 if (have_started_inferiors_p ())
3126 fprintf (stderr
, "Killing process(es):");
3127 for_each_inferior (&all_processes
, print_started_pid
);
3128 fprintf (stderr
, "\n");
3130 if (have_attached_inferiors_p ())
3132 fprintf (stderr
, "Detaching process(es):");
3133 for_each_inferior (&all_processes
, print_attached_pid
);
3134 fprintf (stderr
, "\n");
3137 /* Now we can kill or detach the inferiors. */
3139 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3142 /* Value that will be passed to exit(3) when gdbserver exits. */
3143 static int exit_code
;
3145 /* Cleanup version of detach_or_kill_for_exit. */
3148 detach_or_kill_for_exit_cleanup (void *ignore
)
3150 volatile struct gdb_exception exception
;
3152 TRY_CATCH (exception
, RETURN_MASK_ALL
)
3154 detach_or_kill_for_exit ();
3157 if (exception
.reason
< 0)
3160 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3165 /* Main function. This is called by the real "main" function,
3166 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3168 static void ATTRIBUTE_NORETURN
3169 captured_main (int argc
, char *argv
[])
3173 char *arg_end
, *port
;
3174 char **next_arg
= &argv
[1];
3175 volatile int multi_mode
= 0;
3176 volatile int attach
= 0;
3179 while (*next_arg
!= NULL
&& **next_arg
== '-')
3181 if (strcmp (*next_arg
, "--version") == 0)
3183 gdbserver_version ();
3186 else if (strcmp (*next_arg
, "--help") == 0)
3188 gdbserver_usage (stdout
);
3191 else if (strcmp (*next_arg
, "--attach") == 0)
3193 else if (strcmp (*next_arg
, "--multi") == 0)
3195 else if (strcmp (*next_arg
, "--wrapper") == 0)
3199 wrapper_argv
= next_arg
;
3200 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3203 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3205 gdbserver_usage (stderr
);
3209 /* Consume the "--". */
3212 else if (strcmp (*next_arg
, "--debug") == 0)
3214 else if (strncmp (*next_arg
,
3216 sizeof ("--debug-format=") - 1) == 0)
3219 = parse_debug_format_options ((*next_arg
)
3220 + sizeof ("--debug-format=") - 1, 0);
3222 if (error_msg
!= NULL
)
3224 fprintf (stderr
, "%s", error_msg
);
3228 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3230 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3232 gdbserver_show_disableable (stdout
);
3235 else if (strncmp (*next_arg
,
3236 "--disable-packet=",
3237 sizeof ("--disable-packet=") - 1) == 0)
3239 char *packets
, *tok
;
3241 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3242 for (tok
= strtok (packets
, ",");
3244 tok
= strtok (NULL
, ","))
3246 if (strcmp ("vCont", tok
) == 0)
3247 disable_packet_vCont
= 1;
3248 else if (strcmp ("Tthread", tok
) == 0)
3249 disable_packet_Tthread
= 1;
3250 else if (strcmp ("qC", tok
) == 0)
3251 disable_packet_qC
= 1;
3252 else if (strcmp ("qfThreadInfo", tok
) == 0)
3253 disable_packet_qfThreadInfo
= 1;
3254 else if (strcmp ("threads", tok
) == 0)
3256 disable_packet_vCont
= 1;
3257 disable_packet_Tthread
= 1;
3258 disable_packet_qC
= 1;
3259 disable_packet_qfThreadInfo
= 1;
3263 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3265 gdbserver_show_disableable (stderr
);
3270 else if (strcmp (*next_arg
, "-") == 0)
3272 /* "-" specifies a stdio connection and is a form of port
3274 *next_arg
= STDIO_CONNECTION_NAME
;
3277 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3278 disable_randomization
= 1;
3279 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3280 disable_randomization
= 0;
3281 else if (strcmp (*next_arg
, "--once") == 0)
3285 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3295 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3297 gdbserver_usage (stderr
);
3301 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3302 opened by remote_prepare. */
3305 /* We need to know whether the remote connection is stdio before
3306 starting the inferior. Inferiors created in this scenario have
3307 stdin,stdout redirected. So do this here before we call
3309 remote_prepare (port
);
3314 /* --attach used to come after PORT, so allow it there for
3316 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3323 && (*next_arg
== NULL
3324 || (*next_arg
)[0] == '\0'
3325 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3327 || next_arg
[1] != NULL
))
3332 gdbserver_usage (stderr
);
3336 initialize_async_io ();
3338 initialize_event_loop ();
3339 if (target_supports_tracepoints ())
3340 initialize_tracepoint ();
3342 own_buf
= xmalloc (PBUFSIZ
+ 1);
3343 mem_buf
= xmalloc (PBUFSIZ
);
3345 if (pid
== 0 && *next_arg
!= NULL
)
3349 n
= argc
- (next_arg
- argv
);
3350 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3351 for (i
= 0; i
< n
; i
++)
3352 program_argv
[i
] = xstrdup (next_arg
[i
]);
3353 program_argv
[i
] = NULL
;
3355 /* Wait till we are at first instruction in program. */
3356 start_inferior (program_argv
);
3358 /* We are now (hopefully) stopped at the first instruction of
3359 the target process. This assumes that the target process was
3360 successfully created. */
3364 if (attach_inferior (pid
) == -1)
3365 error ("Attaching not supported on this target");
3367 /* Otherwise succeeded. */
3371 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3372 last_status
.value
.integer
= 0;
3373 last_ptid
= minus_one_ptid
;
3375 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3377 initialize_notif ();
3379 /* Don't report shared library events on the initial connection,
3380 even if some libraries are preloaded. Avoids the "stopped by
3381 shared library event" notice on gdb side. */
3384 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3385 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3390 if (!was_running
&& !multi_mode
)
3391 error ("No program to debug");
3395 volatile struct gdb_exception exception
;
3399 /* Be sure we're out of tfind mode. */
3400 current_traceframe
= -1;
3401 cont_thread
= null_ptid
;
3402 swbreak_feature
= 0;
3403 hwbreak_feature
= 0;
3407 TRY_CATCH (exception
, RETURN_MASK_ERROR
)
3409 /* Wait for events. This will return when all event sources
3410 are removed from the event loop. */
3411 start_event_loop ();
3413 /* If an exit was requested (using the "monitor exit"
3414 command), terminate now. The only other way to get
3415 here is for getpkt to fail; close the connection
3416 and reopen it at the top of the loop. */
3418 if (exit_requested
|| run_once
)
3419 throw_quit ("Quit");
3422 "Remote side has terminated connection. "
3423 "GDBserver will reopen the connection.\n");
3425 /* Get rid of any pending statuses. An eventual reconnection
3426 (by the same GDB instance or another) will refresh all its
3427 state from scratch. */
3428 discard_queued_stop_replies (-1);
3429 for_each_inferior (&all_threads
,
3430 clear_pending_status_callback
);
3434 if (disconnected_tracing
)
3436 /* Try to enable non-stop/async mode, so we we can
3437 both wait for an async socket accept, and handle
3438 async target events simultaneously. There's also
3439 no point either in having the target always stop
3440 all threads, when we're going to pass signals
3441 down without informing GDB. */
3444 if (start_non_stop (1))
3447 /* Detaching implicitly resumes all threads;
3448 simply disconnecting does not. */
3454 "Disconnected tracing disabled; "
3455 "stopping trace run.\n");
3461 if (exception
.reason
== RETURN_ERROR
)
3463 if (response_needed
)
3465 write_enn (own_buf
);
3472 /* Main function. */
3475 main (int argc
, char *argv
[])
3477 volatile struct gdb_exception exception
;
3479 TRY_CATCH (exception
, RETURN_MASK_ALL
)
3481 captured_main (argc
, argv
);
3484 /* captured_main should never return. */
3485 gdb_assert (exception
.reason
< 0);
3487 if (exception
.reason
== RETURN_ERROR
)
3490 fprintf (stderr
, "%s\n", exception
.message
);
3491 fprintf (stderr
, "Exiting\n");
3498 /* Skip PACKET until the next semi-colon (or end of string). */
3501 skip_to_semicolon (char **packet
)
3503 while (**packet
!= '\0' && **packet
!= ';')
3507 /* Process options coming from Z packets for a breakpoint. PACKET is
3508 the packet buffer. *PACKET is updated to point to the first char
3509 after the last processed option. */
3512 process_point_options (struct breakpoint
*bp
, char **packet
)
3514 char *dataptr
= *packet
;
3517 /* Check if data has the correct format. */
3518 if (*dataptr
!= ';')
3525 if (*dataptr
== ';')
3528 if (*dataptr
== 'X')
3530 /* Conditional expression. */
3532 debug_printf ("Found breakpoint condition.\n");
3533 if (!add_breakpoint_condition (bp
, &dataptr
))
3534 skip_to_semicolon (&dataptr
);
3536 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
3538 dataptr
+= strlen ("cmds:");
3540 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3541 persist
= (*dataptr
== '1');
3543 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3544 skip_to_semicolon (&dataptr
);
3548 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3550 /* Skip tokens until we find one that we recognize. */
3551 skip_to_semicolon (&dataptr
);
3557 /* Event loop callback that handles a serial event. The first byte in
3558 the serial buffer gets us here. We expect characters to arrive at
3559 a brisk pace, so we read the rest of the packet with a blocking
3563 process_serial_event (void)
3574 int new_packet_len
= -1;
3576 /* Used to decide when gdbserver should exit in
3577 multi-mode/remote. */
3578 static int have_ran
= 0;
3581 have_ran
= target_running ();
3583 disable_async_io ();
3585 response_needed
= 0;
3586 packet_len
= getpkt (own_buf
);
3587 if (packet_len
<= 0)
3590 /* Force an event loop break. */
3593 response_needed
= 1;
3600 handle_query (own_buf
, packet_len
, &new_packet_len
);
3603 handle_general_set (own_buf
);
3606 require_running (own_buf
);
3611 pid
= strtol (&own_buf
[i
], NULL
, 16);
3614 pid
= ptid_get_pid (current_ptid
);
3616 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3618 struct thread_resume resume_info
;
3619 struct process_info
*process
= find_process_pid (pid
);
3621 if (process
== NULL
)
3623 write_enn (own_buf
);
3627 if (tracing
&& disconnected_tracing
)
3629 "Disconnected tracing in effect, "
3630 "leaving gdbserver attached to the process\n");
3632 if (any_persistent_commands ())
3634 "Persistent commands are present, "
3635 "leaving gdbserver attached to the process\n");
3637 /* Make sure we're in non-stop/async mode, so we we can both
3638 wait for an async socket accept, and handle async target
3639 events simultaneously. There's also no point either in
3640 having the target stop all threads, when we're going to
3641 pass signals down without informing GDB. */
3645 debug_printf ("Forcing non-stop mode\n");
3651 process
->gdb_detached
= 1;
3653 /* Detaching implicitly resumes all threads. */
3654 resume_info
.thread
= minus_one_ptid
;
3655 resume_info
.kind
= resume_continue
;
3656 resume_info
.sig
= 0;
3657 (*the_target
->resume
) (&resume_info
, 1);
3660 break; /* from switch/case */
3663 fprintf (stderr
, "Detaching from process %d\n", pid
);
3665 if (detach_inferior (pid
) != 0)
3666 write_enn (own_buf
);
3669 discard_queued_stop_replies (pid
);
3672 if (extended_protocol
)
3674 /* Treat this like a normal program exit. */
3675 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3676 last_status
.value
.integer
= 0;
3677 last_ptid
= pid_to_ptid (pid
);
3679 current_thread
= NULL
;
3686 /* If we are attached, then we can exit. Otherwise, we
3687 need to hang around doing nothing, until the child is
3689 join_inferior (pid
);
3695 extended_protocol
= 1;
3699 handle_status (own_buf
);
3702 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3704 ptid_t gdb_id
, thread_id
;
3707 require_running (own_buf
);
3709 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3711 pid
= ptid_get_pid (gdb_id
);
3713 if (ptid_equal (gdb_id
, null_ptid
)
3714 || ptid_equal (gdb_id
, minus_one_ptid
))
3715 thread_id
= null_ptid
;
3717 && ptid_equal (pid_to_ptid (pid
),
3720 struct thread_info
*thread
=
3721 (struct thread_info
*) find_inferior (&all_threads
,
3726 write_enn (own_buf
);
3730 thread_id
= thread
->entry
.id
;
3734 thread_id
= gdb_id_to_thread_id (gdb_id
);
3735 if (ptid_equal (thread_id
, null_ptid
))
3737 write_enn (own_buf
);
3742 if (own_buf
[1] == 'g')
3744 if (ptid_equal (thread_id
, null_ptid
))
3746 /* GDB is telling us to choose any thread. Check if
3747 the currently selected thread is still valid. If
3748 it is not, select the first available. */
3749 struct thread_info
*thread
=
3750 (struct thread_info
*) find_inferior_id (&all_threads
,
3754 thread
= get_first_thread ();
3755 thread_id
= thread
->entry
.id
;
3759 general_thread
= thread_id
;
3760 set_desired_thread (1);
3762 else if (own_buf
[1] == 'c')
3763 cont_thread
= thread_id
;
3769 /* Silently ignore it so that gdb can extend the protocol
3770 without compatibility headaches. */
3775 require_running (own_buf
);
3776 if (current_traceframe
>= 0)
3778 struct regcache
*regcache
3779 = new_register_cache (current_target_desc ());
3781 if (fetch_traceframe_registers (current_traceframe
,
3783 registers_to_string (regcache
, own_buf
);
3785 write_enn (own_buf
);
3786 free_register_cache (regcache
);
3790 struct regcache
*regcache
;
3792 set_desired_thread (1);
3793 regcache
= get_thread_regcache (current_thread
, 1);
3794 registers_to_string (regcache
, own_buf
);
3798 require_running (own_buf
);
3799 if (current_traceframe
>= 0)
3800 write_enn (own_buf
);
3803 struct regcache
*regcache
;
3805 set_desired_thread (1);
3806 regcache
= get_thread_regcache (current_thread
, 1);
3807 registers_from_string (regcache
, &own_buf
[1]);
3812 require_running (own_buf
);
3813 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3814 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3816 write_enn (own_buf
);
3818 bin2hex (mem_buf
, own_buf
, res
);
3821 require_running (own_buf
);
3822 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3823 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3826 write_enn (own_buf
);
3829 require_running (own_buf
);
3830 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3831 &mem_addr
, &len
, &mem_buf
) < 0
3832 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3833 write_enn (own_buf
);
3838 require_running (own_buf
);
3839 hex2bin (own_buf
+ 1, &sig
, 1);
3840 if (gdb_signal_to_host_p (sig
))
3841 signal
= gdb_signal_to_host (sig
);
3844 myresume (own_buf
, 0, signal
);
3847 require_running (own_buf
);
3848 hex2bin (own_buf
+ 1, &sig
, 1);
3849 if (gdb_signal_to_host_p (sig
))
3850 signal
= gdb_signal_to_host (sig
);
3853 myresume (own_buf
, 1, signal
);
3856 require_running (own_buf
);
3858 myresume (own_buf
, 0, signal
);
3861 require_running (own_buf
);
3863 myresume (own_buf
, 1, signal
);
3865 case 'Z': /* insert_ ... */
3867 case 'z': /* remove_ ... */
3872 char type
= own_buf
[1];
3874 const int insert
= ch
== 'Z';
3875 char *p
= &own_buf
[3];
3877 p
= unpack_varlen_hex (p
, &addr
);
3878 len
= strtol (p
+ 1, &dataptr
, 16);
3882 struct breakpoint
*bp
;
3884 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
3889 /* GDB may have sent us a list of *point parameters to
3890 be evaluated on the target's side. Read such list
3891 here. If we already have a list of parameters, GDB
3892 is telling us to drop that list and use this one
3894 clear_breakpoint_conditions_and_commands (bp
);
3895 process_point_options (bp
, &dataptr
);
3899 res
= delete_gdb_breakpoint (type
, addr
, len
);
3907 write_enn (own_buf
);
3911 response_needed
= 0;
3912 if (!target_running ())
3913 /* The packet we received doesn't make sense - but we can't
3914 reply to it, either. */
3917 fprintf (stderr
, "Killing all inferiors\n");
3918 for_each_inferior (&all_processes
, kill_inferior_callback
);
3920 /* When using the extended protocol, we wait with no program
3921 running. The traditional protocol will exit instead. */
3922 if (extended_protocol
)
3924 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3925 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3933 ptid_t gdb_id
, thread_id
;
3935 require_running (own_buf
);
3937 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3938 thread_id
= gdb_id_to_thread_id (gdb_id
);
3939 if (ptid_equal (thread_id
, null_ptid
))
3941 write_enn (own_buf
);
3945 if (mythread_alive (thread_id
))
3948 write_enn (own_buf
);
3952 response_needed
= 0;
3954 /* Restarting the inferior is only supported in the extended
3956 if (extended_protocol
)
3958 if (target_running ())
3959 for_each_inferior (&all_processes
,
3960 kill_inferior_callback
);
3961 fprintf (stderr
, "GDBserver restarting\n");
3963 /* Wait till we are at 1st instruction in prog. */
3964 if (program_argv
!= NULL
)
3965 start_inferior (program_argv
);
3968 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3969 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3975 /* It is a request we don't understand. Respond with an
3976 empty packet so that gdb knows that we don't support this
3982 /* Extended (long) request. */
3983 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3987 /* It is a request we don't understand. Respond with an empty
3988 packet so that gdb knows that we don't support this
3994 if (new_packet_len
!= -1)
3995 putpkt_binary (own_buf
, new_packet_len
);
3999 response_needed
= 0;
4001 if (!extended_protocol
&& have_ran
&& !target_running ())
4003 /* In non-stop, defer exiting until GDB had a chance to query
4004 the whole vStopped list (until it gets an OK). */
4005 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4007 /* Be transparent when GDB is connected through stdio -- no
4008 need to spam GDB's console. */
4009 if (!remote_connection_is_stdio ())
4010 fprintf (stderr
, "GDBserver exiting\n");
4022 /* Event-loop callback for serial events. */
4025 handle_serial_event (int err
, gdb_client_data client_data
)
4028 debug_printf ("handling possible serial event\n");
4030 /* Really handle it. */
4031 if (process_serial_event () < 0)
4034 /* Be sure to not change the selected thread behind GDB's back.
4035 Important in the non-stop mode asynchronous protocol. */
4036 set_desired_thread (1);
4041 /* Event-loop callback for target events. */
4044 handle_target_event (int err
, gdb_client_data client_data
)
4047 debug_printf ("handling possible target event\n");
4049 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4052 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4054 /* No RSP support for this yet. */
4056 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4058 int pid
= ptid_get_pid (last_ptid
);
4059 struct process_info
*process
= find_process_pid (pid
);
4060 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4062 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4063 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4065 mark_breakpoints_out (process
);
4066 mourn_inferior (process
);
4070 /* We're reporting this thread as stopped. Update its
4071 "want-stopped" state to what the client wants, until it
4072 gets a new resume action. */
4073 current_thread
->last_resume_kind
= resume_stop
;
4074 current_thread
->last_status
= last_status
;
4079 if (!target_running ())
4081 /* The last process exited. We're done. */
4085 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4087 /* A thread stopped with a signal, but gdb isn't
4088 connected to handle it. Pass it down to the
4089 inferior, as if it wasn't being traced. */
4090 struct thread_resume resume_info
;
4093 debug_printf ("GDB not connected; forwarding event %d for"
4095 (int) last_status
.kind
,
4096 target_pid_to_str (last_ptid
));
4098 resume_info
.thread
= last_ptid
;
4099 resume_info
.kind
= resume_continue
;
4100 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4101 (*the_target
->resume
) (&resume_info
, 1);
4103 else if (debug_threads
)
4104 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4105 (int) last_status
.kind
,
4106 target_pid_to_str (last_ptid
));
4110 struct vstop_notif
*vstop_notif
4111 = xmalloc (sizeof (struct vstop_notif
));
4113 vstop_notif
->status
= last_status
;
4114 vstop_notif
->ptid
= last_ptid
;
4115 /* Push Stop notification. */
4116 notif_push (¬if_stop
,
4117 (struct notif_event
*) vstop_notif
);
4121 /* Be sure to not change the selected thread behind GDB's back.
4122 Important in the non-stop mode asynchronous protocol. */
4123 set_desired_thread (1);