1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
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/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
24 #include "exceptions.h"
30 #include "process-stratum-target.h"
31 #include "cli/cli-cmds.h"
33 #include "gdbthread.h"
35 #include "remote-notif.h"
38 #include "observable.h"
40 #include "cli/cli-decode.h"
41 #include "cli/cli-setshow.h"
42 #include "cli/cli-style.h"
43 #include "target-descriptions.h"
45 #include "gdbsupport/filestuff.h"
46 #include "gdbsupport/rsp-low.h"
50 #include "gdbsupport/gdb_sys_time.h"
52 #include "gdbsupport/event-loop.h"
53 #include "event-top.h"
61 #include "remote-fileio.h"
62 #include "gdbsupport/fileio.h"
64 #include "xml-support.h"
66 #include "memory-map.h"
68 #include "tracepoint.h"
71 #include "gdbsupport/agent.h"
73 #include "record-btrace.h"
74 #include "gdbsupport/scoped_restore.h"
75 #include "gdbsupport/environ.h"
76 #include "gdbsupport/byte-vector.h"
77 #include "gdbsupport/search.h"
80 #include <unordered_map>
81 #include "async-event.h"
82 #include "gdbsupport/selftest.h"
83 #include "cli/cli-style.h"
85 /* The remote target. */
87 static const char remote_doc
[] = N_("\
88 Use a remote computer via a serial line, using a gdb-specific protocol.\n\
89 Specify the serial device it is connected to\n\
90 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
94 bool remote_debug
= false;
96 #define OPAQUETHREADBYTES 8
98 /* a 64 bit opaque identifier */
99 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
101 struct gdb_ext_thread_info
;
102 struct threads_listing_context
;
103 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
104 struct protocol_feature
;
108 typedef std::unique_ptr
<stop_reply
> stop_reply_up
;
110 /* Generic configuration support for packets the stub optionally
111 supports. Allows the user to specify the use of the packet as well
112 as allowing GDB to auto-detect support in the remote stub. */
116 PACKET_SUPPORT_UNKNOWN
= 0,
121 /* Convert the packet support auto_boolean to a name used for gdb printing. */
124 get_packet_support_name (auto_boolean support
)
128 case AUTO_BOOLEAN_TRUE
:
130 case AUTO_BOOLEAN_FALSE
:
132 case AUTO_BOOLEAN_AUTO
:
135 gdb_assert_not_reached ("invalid var_auto_boolean");
139 /* Convert the target type (future remote target or currently connected target)
140 to a name used for gdb printing. */
143 get_target_type_name (bool target_connected
)
145 if (target_connected
)
146 return _("on the current remote target");
148 return _("on future remote targets");
151 /* Analyze a packet's return value and update the packet config
161 /* Keeps packet's return value. If packet's return value is PACKET_ERROR,
162 err_msg contains an error message string from E.string or the number
163 stored as a string from E.num. */
167 /* Private ctors for internal use. Clients should use the public
168 factory static methods instead. */
170 /* Construct a PACKET_ERROR packet_result. */
171 packet_result (const char *err_msg
, bool textual_err_msg
)
172 : m_status (PACKET_ERROR
),
174 m_textual_err_msg (textual_err_msg
)
177 /* Construct an PACKET_OK/PACKET_UNKNOWN packet_result. */
178 explicit packet_result (enum packet_status status
)
181 gdb_assert (status
!= PACKET_ERROR
);
185 enum packet_status
status () const
187 return this->m_status
;
190 const char *err_msg () const
192 gdb_assert (this->m_status
== PACKET_ERROR
);
193 return this->m_err_msg
.c_str ();
196 bool textual_err_msg () const
198 gdb_assert (this->m_status
== PACKET_ERROR
);
199 return this->m_textual_err_msg
;
202 static packet_result
make_numeric_error (const char *err_msg
)
204 return packet_result (err_msg
, false);
207 static packet_result
make_textual_error (const char *err_msg
)
209 return packet_result (err_msg
, true);
212 static packet_result
make_ok ()
214 return packet_result (PACKET_OK
);
217 static packet_result
make_unknown ()
219 return packet_result (PACKET_UNKNOWN
);
223 enum packet_status m_status
;
224 std::string m_err_msg
;
226 /* True if we have a textual error message, from an "E.MESSAGE"
228 bool m_textual_err_msg
= false;
231 /* Enumeration of packets for a remote target. */
251 PACKET_vFile_readlink
,
255 PACKET_qXfer_features
,
256 PACKET_qXfer_exec_file
,
257 PACKET_qXfer_libraries
,
258 PACKET_qXfer_libraries_svr4
,
259 PACKET_qXfer_memory_map
,
261 PACKET_qXfer_threads
,
262 PACKET_qXfer_statictrace_read
,
263 PACKET_qXfer_traceframe_info
,
270 PACKET_QCatchSyscalls
,
271 PACKET_QProgramSignals
,
272 PACKET_QSetWorkingDir
,
273 PACKET_QStartupWithShell
,
274 PACKET_QEnvironmentHexEncoded
,
275 PACKET_QEnvironmentReset
,
276 PACKET_QEnvironmentUnset
,
278 PACKET_qSearch_memory
,
281 PACKET_QStartNoAckMode
,
283 PACKET_qXfer_siginfo_read
,
284 PACKET_qXfer_siginfo_write
,
287 /* Support for conditional tracepoints. */
288 PACKET_ConditionalTracepoints
,
290 /* Support for target-side breakpoint conditions. */
291 PACKET_ConditionalBreakpoints
,
293 /* Support for target-side breakpoint commands. */
294 PACKET_BreakpointCommands
,
296 /* Support for fast tracepoints. */
297 PACKET_FastTracepoints
,
299 /* Support for static tracepoints. */
300 PACKET_StaticTracepoints
,
302 /* Support for installing tracepoints while a trace experiment is
304 PACKET_InstallInTrace
,
308 PACKET_TracepointSource
,
311 PACKET_QDisableRandomization
,
313 PACKET_QTBuffer_size
,
319 /* Support for the QNonStop packet. */
322 /* Support for the QThreadEvents packet. */
323 PACKET_QThreadEvents
,
325 /* Support for the QThreadOptions packet. */
326 PACKET_QThreadOptions
,
328 /* Support for multi-process extensions. */
329 PACKET_multiprocess_feature
,
331 /* Support for enabling and disabling tracepoints while a trace
332 experiment is running. */
333 PACKET_EnableDisableTracepoints_feature
,
335 /* Support for collecting strings using the tracenz bytecode. */
336 PACKET_tracenz_feature
,
338 /* Support for continuing to run a trace experiment while GDB is
340 PACKET_DisconnectedTracing_feature
,
342 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
343 PACKET_augmented_libraries_svr4_read_feature
,
345 /* Support for the qXfer:btrace-conf:read packet. */
346 PACKET_qXfer_btrace_conf
,
348 /* Support for the Qbtrace-conf:bts:size packet. */
349 PACKET_Qbtrace_conf_bts_size
,
351 /* Support for swbreak+ feature. */
352 PACKET_swbreak_feature
,
354 /* Support for hwbreak+ feature. */
355 PACKET_hwbreak_feature
,
357 /* Support for fork events. */
358 PACKET_fork_event_feature
,
360 /* Support for vfork events. */
361 PACKET_vfork_event_feature
,
363 /* Support for the Qbtrace-conf:pt:size packet. */
364 PACKET_Qbtrace_conf_pt_size
,
366 /* Support for the Qbtrace-conf:pt:ptwrite packet. */
367 PACKET_Qbtrace_conf_pt_ptwrite
,
369 /* Support for the Qbtrace-conf:pt:event-tracing packet. */
370 PACKET_Qbtrace_conf_pt_event_tracing
,
372 /* Support for exec events. */
373 PACKET_exec_event_feature
,
375 /* Support for query supported vCont actions. */
376 PACKET_vContSupported
,
378 /* Support remote CTRL-C. */
381 /* Support TARGET_WAITKIND_NO_RESUMED. */
384 /* Support for memory tagging, allocation tag fetch/store
385 packets and the tag violation stop replies. */
386 PACKET_memory_tagging_feature
,
388 /* Support for the qIsAddressTagged packet. */
389 PACKET_qIsAddressTagged
,
391 /* Support for accepting error message in a E.errtext format.
392 This allows every remote packet to return E.errtext.
394 This feature only exists to fix a backwards compatibility issue
395 with the qRcmd and m packets. Historically, these two packets didn't
396 support E.errtext style errors, but when this feature is on
397 these two packets can receive E.errtext style errors.
399 All new packets should be written to always accept E.errtext style
400 errors, and so they should not need to check for this feature. */
401 PACKET_accept_error_message
,
406 struct threads_listing_context
;
408 /* Stub vCont actions support.
410 Each field is a boolean flag indicating whether the stub reports
411 support for the corresponding action. */
413 struct vCont_action_support
428 /* About this many threadids fit in a packet. */
430 #define MAXTHREADLISTRESULTS 32
432 /* Data for the vFile:pread readahead cache. */
434 struct readahead_cache
436 /* Invalidate the readahead cache. */
439 /* Invalidate the readahead cache if it is holding data for FD. */
440 void invalidate_fd (int fd
);
442 /* Serve pread from the readahead cache. Returns number of bytes
443 read, or 0 if the request can't be served from the cache. */
444 int pread (int fd
, gdb_byte
*read_buf
, size_t len
, ULONGEST offset
);
446 /* The file descriptor for the file that is being cached. -1 if the
450 /* The offset into the file that the cache buffer corresponds
454 /* The buffer holding the cache contents. */
455 gdb::byte_vector buf
;
457 /* Cache hit and miss counters. */
458 ULONGEST hit_count
= 0;
459 ULONGEST miss_count
= 0;
462 /* Description of the remote protocol for a given architecture. */
466 long offset
; /* Offset into G packet. */
467 long regnum
; /* GDB's internal register number. */
468 LONGEST pnum
; /* Remote protocol register number. */
469 bool in_g_packet
; /* Always part of G packet. */
470 /* long size in bytes; == register_size (arch, regnum);
472 /* char *name; == gdbarch_register_name (arch, regnum);
476 struct remote_arch_state
478 explicit remote_arch_state (struct gdbarch
*gdbarch
);
480 /* Description of the remote protocol registers. */
481 long sizeof_g_packet
;
483 /* Description of the remote protocol registers indexed by REGNUM
484 (making an array gdbarch_num_regs in size). */
485 std::unique_ptr
<packet_reg
[]> regs
;
487 /* This is the size (in chars) of the first response to the ``g''
488 packet. It is used as a heuristic when determining the maximum
489 size of memory-read and memory-write packets. A target will
490 typically only reserve a buffer large enough to hold the ``g''
491 packet. The size does not include packet overhead (headers and
493 long actual_register_packet_size
;
495 /* This is the maximum size (in chars) of a non read/write packet.
496 It is also used as a cap on the size of read/write packets. */
497 long remote_packet_size
;
500 /* Description of the remote protocol state for the currently
501 connected target. This is per-target state, and independent of the
502 selected architecture. */
511 /* Get the remote arch state for GDBARCH. */
512 struct remote_arch_state
*get_remote_arch_state (struct gdbarch
*gdbarch
);
514 void create_async_event_handler ()
516 gdb_assert (m_async_event_handler_token
== nullptr);
517 m_async_event_handler_token
518 = ::create_async_event_handler ([] (gdb_client_data data
)
520 inferior_event_handler (INF_REG_EVENT
);
525 void mark_async_event_handler ()
527 gdb_assert (this->is_async_p ());
528 ::mark_async_event_handler (m_async_event_handler_token
);
531 void clear_async_event_handler ()
532 { ::clear_async_event_handler (m_async_event_handler_token
); }
534 bool async_event_handler_marked () const
535 { return ::async_event_handler_marked (m_async_event_handler_token
); }
537 void delete_async_event_handler ()
539 if (m_async_event_handler_token
!= nullptr)
540 ::delete_async_event_handler (&m_async_event_handler_token
);
543 bool is_async_p () const
545 /* We're async whenever the serial device is. */
546 gdb_assert (this->remote_desc
!= nullptr);
547 return serial_is_async_p (this->remote_desc
);
550 bool can_async_p () const
552 /* We can async whenever the serial device can. */
553 gdb_assert (this->remote_desc
!= nullptr);
554 return serial_can_async_p (this->remote_desc
);
559 /* A buffer to use for incoming packets, and its current size. The
560 buffer is grown dynamically for larger incoming packets.
561 Outgoing packets may also be constructed in this buffer.
562 The size of the buffer is always at least REMOTE_PACKET_SIZE;
563 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
565 gdb::char_vector buf
;
567 /* True if we're going through initial connection setup (finding out
568 about the remote side's threads, relocating symbols, etc.). */
569 bool starting_up
= false;
571 /* If we negotiated packet size explicitly (and thus can bypass
572 heuristics for the largest packet size that will not overflow
573 a buffer in the stub), this will be set to that packet size.
574 Otherwise zero, meaning to use the guessed size. */
575 long explicit_packet_size
= 0;
577 /* True, if in no ack mode. That is, neither GDB nor the stub will
578 expect acks from each other. The connection is assumed to be
580 bool noack_mode
= false;
582 /* True if we're connected in extended remote mode. */
583 bool extended
= false;
585 /* True if we resumed the target and we're waiting for the target to
586 stop. In the mean time, we can't start another command/query.
587 The remote server wouldn't be ready to process it, so we'd
588 timeout waiting for a reply that would never come and eventually
589 we'd close the connection. This can happen in asynchronous mode
590 because we allow GDB commands while the target is running. */
591 bool waiting_for_stop_reply
= false;
593 /* The status of the stub support for the various vCont actions. */
594 vCont_action_support supports_vCont
;
596 /* True if the user has pressed Ctrl-C, but the target hasn't
597 responded to that. */
598 bool ctrlc_pending_p
= false;
600 /* True if we saw a Ctrl-C while reading or writing from/to the
601 remote descriptor. At that point it is not safe to send a remote
602 interrupt packet, so we instead remember we saw the Ctrl-C and
603 process it once we're done with sending/receiving the current
604 packet, which should be shortly. If however that takes too long,
605 and the user presses Ctrl-C again, we offer to disconnect. */
606 bool got_ctrlc_during_io
= false;
608 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
609 remote_open knows that we don't have a file open when the program
611 struct serial
*remote_desc
= nullptr;
613 /* These are the threads which we last sent to the remote system. The
614 TID member will be -1 for all or -2 for not sent yet. */
615 ptid_t general_thread
= null_ptid
;
616 ptid_t continue_thread
= null_ptid
;
618 /* This is the traceframe which we last selected on the remote system.
619 It will be -1 if no traceframe is selected. */
620 int remote_traceframe_number
= -1;
622 char *last_pass_packet
= nullptr;
624 /* The last QProgramSignals packet sent to the target. We bypass
625 sending a new program signals list down to the target if the new
626 packet is exactly the same as the last we sent. IOW, we only let
627 the target know about program signals list changes. */
628 char *last_program_signals_packet
= nullptr;
630 /* Similarly, the last QThreadEvents state we sent to the
632 bool last_thread_events
= false;
634 gdb_signal last_sent_signal
= GDB_SIGNAL_0
;
636 bool last_sent_step
= false;
638 /* The execution direction of the last resume we got. */
639 exec_direction_kind last_resume_exec_dir
= EXEC_FORWARD
;
641 char *finished_object
= nullptr;
642 char *finished_annex
= nullptr;
643 ULONGEST finished_offset
= 0;
645 /* Should we try the 'ThreadInfo' query packet?
647 This variable (NOT available to the user: auto-detect only!)
648 determines whether GDB will use the new, simpler "ThreadInfo"
649 query or the older, more complex syntax for thread queries.
650 This is an auto-detect variable (set to true at each connect,
651 and set to false when the target fails to recognize it). */
652 bool use_threadinfo_query
= false;
653 bool use_threadextra_query
= false;
655 threadref echo_nextthread
{};
656 threadref nextthread
{};
657 threadref resultthreadlist
[MAXTHREADLISTRESULTS
] {};
659 /* The state of remote notification. */
660 struct remote_notif_state
*notif_state
= nullptr;
662 /* The branch trace configuration. */
663 struct btrace_config btrace_config
{};
665 /* The argument to the last "vFile:setfs:" packet we sent, used
666 to avoid sending repeated unnecessary "vFile:setfs:" packets.
667 Initialized to -1 to indicate that no "vFile:setfs:" packet
668 has yet been sent. */
671 /* A readahead cache for vFile:pread. Often, reading a binary
672 involves a sequence of small reads. E.g., when parsing an ELF
673 file. A readahead cache helps mostly the case of remote
674 debugging on a connection with higher latency, due to the
675 request/reply nature of the RSP. We only cache data for a single
676 file descriptor at a time. */
677 struct readahead_cache readahead_cache
;
679 /* The list of already fetched and acknowledged stop events. This
680 queue is used for notification Stop, and other notifications
681 don't need queue for their events, because the notification
682 events of Stop can't be consumed immediately, so that events
683 should be queued first, and be consumed by remote_wait_{ns,as}
684 one per time. Other notifications can consume their events
685 immediately, so queue is not needed for them. */
686 std::vector
<stop_reply_up
> stop_reply_queue
;
688 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
689 ``forever'' still use the normal timeout mechanism. This is
690 currently used by the ASYNC code to guarantee that target reads
691 during the initial connect always time-out. Once getpkt has been
692 modified to return a timeout indication and, in turn
693 remote_wait()/wait_for_inferior() have gained a timeout parameter
695 bool wait_forever_enabled_p
= true;
697 /* The set of thread options the target reported it supports, via
699 gdb_thread_options supported_thread_options
= 0;
701 /* Contains the regnums of the expedited registers in the last stop
703 std::set
<int> last_seen_expedited_registers
;
706 /* Asynchronous signal handle registered as event loop source for
707 when we have pending events ready to be passed to the core. */
708 async_event_handler
*m_async_event_handler_token
= nullptr;
710 /* Mapping of remote protocol data for each gdbarch. Usually there
711 is only one entry here, though we may see more with stubs that
712 support multi-process. */
713 std::unordered_map
<struct gdbarch
*, remote_arch_state
>
717 static const target_info remote_target_info
= {
719 N_("Remote target using gdb-specific protocol"),
723 /* Description of a remote packet. */
725 struct packet_description
727 /* Name of the packet used for gdb output. */
730 /* Title of the packet, used by the set/show remote name-packet
731 commands to identify the individual packages and gdb output. */
735 /* Configuration of a remote packet. */
739 /* If auto, GDB auto-detects support for this packet or feature,
740 either through qSupported, or by trying the packet and looking
741 at the response. If true, GDB assumes the target supports this
742 packet. If false, the packet is disabled. Configs that don't
743 have an associated command always have this set to auto. */
744 enum auto_boolean detect
;
746 /* Does the target support this packet? */
747 enum packet_support support
;
750 /* User configurable variables for the number of characters in a
751 memory read/write packet. MIN (rsa->remote_packet_size,
752 rsa->sizeof_g_packet) is the default. Some targets need smaller
753 values (fifo overruns, et.al.) and some users need larger values
754 (speed up transfers). The variables ``preferred_*'' (the user
755 request), ``current_*'' (what was actually set) and ``forced_*''
756 (Positive - a soft limit, negative - a hard limit). */
758 struct memory_packet_config
765 /* These global variables contain the default configuration for every new
766 remote_feature object. */
767 static memory_packet_config memory_read_packet_config
=
769 "memory-read-packet-size",
771 static memory_packet_config memory_write_packet_config
=
773 "memory-write-packet-size",
776 /* This global array contains packet descriptions (name and title). */
777 static packet_description packets_descriptions
[PACKET_MAX
];
778 /* This global array contains the default configuration for every new
779 per-remote target array. */
780 static packet_config remote_protocol_packets
[PACKET_MAX
];
782 /* Description of a remote target's features. It stores the configuration
783 and provides functions to determine supported features of the target. */
785 struct remote_features
789 m_memory_read_packet_config
= memory_read_packet_config
;
790 m_memory_write_packet_config
= memory_write_packet_config
;
792 std::copy (std::begin (remote_protocol_packets
),
793 std::end (remote_protocol_packets
),
794 std::begin (m_protocol_packets
));
796 ~remote_features () = default;
798 DISABLE_COPY_AND_ASSIGN (remote_features
);
800 /* Returns whether a given packet defined by its enum value is supported. */
801 enum packet_support
packet_support (int) const;
803 /* Returns the packet's corresponding "set remote foo-packet" command
804 state. See struct packet_config for more details. */
805 enum auto_boolean
packet_set_cmd_state (int packet
) const
806 { return m_protocol_packets
[packet
].detect
; }
808 /* Returns true if the multi-process extensions are in effect. */
809 int remote_multi_process_p () const
810 { return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
; }
812 /* Returns true if fork events are supported. */
813 int remote_fork_event_p () const
814 { return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
; }
816 /* Returns true if vfork events are supported. */
817 int remote_vfork_event_p () const
818 { return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
; }
820 /* Returns true if exec events are supported. */
821 int remote_exec_event_p () const
822 { return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
; }
824 /* Returns true if memory tagging is supported, false otherwise. */
825 bool remote_memory_tagging_p () const
826 { return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
; }
828 /* Reset all packets back to "unknown support". Called when opening a
829 new connection to a remote target. */
830 void reset_all_packet_configs_support ();
832 /* Check result value in BUF for packet WHICH_PACKET and update the packet's
833 support configuration accordingly. */
834 packet_result
packet_ok (const char *buf
, const int which_packet
);
835 packet_result
packet_ok (const gdb::char_vector
&buf
, const int which_packet
);
837 /* Configuration of a remote target's memory read packet. */
838 memory_packet_config m_memory_read_packet_config
;
839 /* Configuration of a remote target's memory write packet. */
840 memory_packet_config m_memory_write_packet_config
;
842 /* The per-remote target array which stores a remote's packet
844 packet_config m_protocol_packets
[PACKET_MAX
];
847 class remote_target
: public process_stratum_target
850 remote_target () = default;
851 ~remote_target () override
;
853 const target_info
&info () const override
854 { return remote_target_info
; }
856 const char *connection_string () override
;
858 thread_control_capabilities
get_thread_control_capabilities () override
859 { return tc_schedlock
; }
861 /* Open a remote connection. */
862 static void open (const char *, int);
864 void close () override
;
866 void detach (inferior
*, int) override
;
867 void disconnect (const char *, int) override
;
869 void commit_requested_thread_options ();
871 void commit_resumed () override
;
872 void resume (ptid_t
, int, enum gdb_signal
) override
;
873 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
874 bool has_pending_events () override
;
876 void fetch_registers (struct regcache
*, int) override
;
877 void store_registers (struct regcache
*, int) override
;
878 void prepare_to_store (struct regcache
*) override
;
880 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
882 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
883 enum remove_bp_reason
) override
;
886 bool stopped_by_sw_breakpoint () override
;
887 bool supports_stopped_by_sw_breakpoint () override
;
889 bool stopped_by_hw_breakpoint () override
;
891 bool supports_stopped_by_hw_breakpoint () override
;
893 bool stopped_by_watchpoint () override
;
895 bool stopped_data_address (CORE_ADDR
*) override
;
897 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
899 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
901 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
903 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
905 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
907 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
908 struct expression
*) override
;
910 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
911 struct expression
*) override
;
913 void kill () override
;
915 void load (const char *, int) override
;
917 void mourn_inferior () override
;
919 void pass_signals (gdb::array_view
<const unsigned char>) override
;
921 int set_syscall_catchpoint (int, bool, int,
922 gdb::array_view
<const int>) override
;
924 void program_signals (gdb::array_view
<const unsigned char>) override
;
926 bool thread_alive (ptid_t ptid
) override
;
928 const char *thread_name (struct thread_info
*) override
;
930 void update_thread_list () override
;
932 std::string
pid_to_str (ptid_t
) override
;
934 const char *extra_thread_info (struct thread_info
*) override
;
936 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
938 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
940 inferior
*inf
) override
;
942 gdb::array_view
<const gdb_byte
> thread_info_to_thread_handle (struct thread_info
*tp
)
945 void stop (ptid_t
) override
;
947 void interrupt () override
;
949 void pass_ctrlc () override
;
951 enum target_xfer_status
xfer_partial (enum target_object object
,
954 const gdb_byte
*writebuf
,
955 ULONGEST offset
, ULONGEST len
,
956 ULONGEST
*xfered_len
) override
;
958 ULONGEST
get_memory_xfer_limit () override
;
960 void rcmd (const char *command
, struct ui_file
*output
) override
;
962 const char *pid_to_exec_file (int pid
) override
;
964 void log_command (const char *cmd
) override
966 serial_log_command (this, cmd
);
969 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
970 CORE_ADDR load_module_addr
,
971 CORE_ADDR offset
) override
;
973 bool can_execute_reverse () override
;
975 std::vector
<mem_region
> memory_map () override
;
977 void flash_erase (ULONGEST address
, LONGEST length
) override
;
979 void flash_done () override
;
981 const struct target_desc
*read_description () override
;
983 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
984 const gdb_byte
*pattern
, ULONGEST pattern_len
,
985 CORE_ADDR
*found_addrp
) override
;
987 bool can_async_p () override
;
989 bool is_async_p () override
;
991 void async (bool) override
;
993 int async_wait_fd () override
;
995 void thread_events (bool) override
;
997 bool supports_set_thread_options (gdb_thread_options
) override
;
999 int can_do_single_step () override
;
1001 void terminal_inferior () override
;
1003 void terminal_ours () override
;
1005 bool supports_non_stop () override
;
1007 bool supports_multi_process () override
;
1009 bool supports_disable_randomization () override
;
1011 bool filesystem_is_local () override
;
1014 int fileio_open (struct inferior
*inf
, const char *filename
,
1015 int flags
, int mode
, int warn_if_slow
,
1016 fileio_error
*target_errno
) override
;
1018 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
1019 ULONGEST offset
, fileio_error
*target_errno
) override
;
1021 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
1022 ULONGEST offset
, fileio_error
*target_errno
) override
;
1024 int fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
) override
;
1026 int fileio_stat (struct inferior
*inf
, const char *filename
,
1027 struct stat
*sb
, fileio_error
*target_errno
) override
;
1029 int fileio_close (int fd
, fileio_error
*target_errno
) override
;
1031 int fileio_unlink (struct inferior
*inf
,
1032 const char *filename
,
1033 fileio_error
*target_errno
) override
;
1035 std::optional
<std::string
>
1036 fileio_readlink (struct inferior
*inf
,
1037 const char *filename
,
1038 fileio_error
*target_errno
) override
;
1040 bool supports_enable_disable_tracepoint () override
;
1042 bool supports_string_tracing () override
;
1044 int remote_supports_cond_tracepoints ();
1046 bool supports_evaluation_of_breakpoint_conditions () override
;
1048 int remote_supports_fast_tracepoints ();
1050 int remote_supports_static_tracepoints ();
1052 int remote_supports_install_in_trace ();
1054 bool can_run_breakpoint_commands () override
;
1056 void trace_init () override
;
1058 void download_tracepoint (struct bp_location
*location
) override
;
1060 bool can_download_tracepoint () override
;
1062 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
1064 void enable_tracepoint (struct bp_location
*location
) override
;
1066 void disable_tracepoint (struct bp_location
*location
) override
;
1068 void trace_set_readonly_regions () override
;
1070 void trace_start () override
;
1072 int get_trace_status (struct trace_status
*ts
) override
;
1074 void get_tracepoint_status (tracepoint
*tp
, struct uploaded_tp
*utp
)
1077 void trace_stop () override
;
1079 int trace_find (enum trace_find_type type
, int num
,
1080 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
1082 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
1084 int save_trace_data (const char *filename
) override
;
1086 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
1088 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
1090 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
1092 int get_min_fast_tracepoint_insn_len () override
;
1094 void set_disconnected_tracing (int val
) override
;
1096 void set_circular_trace_buffer (int val
) override
;
1098 void set_trace_buffer_size (LONGEST val
) override
;
1100 bool set_trace_notes (const char *user
, const char *notes
,
1101 const char *stopnotes
) override
;
1103 int core_of_thread (ptid_t ptid
) override
;
1105 int verify_memory (const gdb_byte
*data
,
1106 CORE_ADDR memaddr
, ULONGEST size
) override
;
1109 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
1111 void set_permissions () override
;
1113 bool static_tracepoint_marker_at (CORE_ADDR
,
1114 struct static_tracepoint_marker
*marker
)
1117 std::vector
<static_tracepoint_marker
>
1118 static_tracepoint_markers_by_strid (const char *id
) override
;
1120 traceframe_info_up
traceframe_info () override
;
1122 bool use_agent (bool use
) override
;
1123 bool can_use_agent () override
;
1125 struct btrace_target_info
*
1126 enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
) override
;
1128 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
1130 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
1132 enum btrace_error
read_btrace (struct btrace_data
*data
,
1133 struct btrace_target_info
*btinfo
,
1134 enum btrace_read_type type
) override
;
1136 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
1137 bool augmented_libraries_svr4_read () override
;
1138 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
1139 void follow_clone (ptid_t child_ptid
) override
;
1140 void follow_exec (inferior
*, ptid_t
, const char *) override
;
1141 int insert_fork_catchpoint (int) override
;
1142 int remove_fork_catchpoint (int) override
;
1143 int insert_vfork_catchpoint (int) override
;
1144 int remove_vfork_catchpoint (int) override
;
1145 int insert_exec_catchpoint (int) override
;
1146 int remove_exec_catchpoint (int) override
;
1147 enum exec_direction_kind
execution_direction () override
;
1149 bool supports_memory_tagging () override
;
1151 bool fetch_memtags (CORE_ADDR address
, size_t len
,
1152 gdb::byte_vector
&tags
, int type
) override
;
1154 bool store_memtags (CORE_ADDR address
, size_t len
,
1155 const gdb::byte_vector
&tags
, int type
) override
;
1157 bool is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
) override
;
1159 public: /* Remote specific methods. */
1161 void remote_download_command_source (int num
, ULONGEST addr
,
1162 struct command_line
*cmds
);
1164 void remote_file_put (const char *local_file
, const char *remote_file
,
1166 void remote_file_get (const char *remote_file
, const char *local_file
,
1168 void remote_file_delete (const char *remote_file
, int from_tty
);
1170 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
1171 ULONGEST offset
, fileio_error
*remote_errno
);
1172 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
1173 ULONGEST offset
, fileio_error
*remote_errno
);
1174 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
1175 ULONGEST offset
, fileio_error
*remote_errno
);
1177 int remote_hostio_send_command (int command_bytes
, int which_packet
,
1178 fileio_error
*remote_errno
, const char **attachment
,
1179 int *attachment_len
);
1180 int remote_hostio_set_filesystem (struct inferior
*inf
,
1181 fileio_error
*remote_errno
);
1182 /* We should get rid of this and use fileio_open directly. */
1183 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
1184 int flags
, int mode
, int warn_if_slow
,
1185 fileio_error
*remote_errno
);
1186 int remote_hostio_close (int fd
, fileio_error
*remote_errno
);
1188 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
1189 fileio_error
*remote_errno
);
1191 struct remote_state
*get_remote_state ();
1193 long get_remote_packet_size (void);
1194 long get_memory_packet_size (struct memory_packet_config
*config
);
1196 long get_memory_write_packet_size ();
1197 long get_memory_read_packet_size ();
1199 char *append_pending_thread_resumptions (char *p
, char *endp
,
1201 static void open_1 (const char *name
, int from_tty
, int extended_p
);
1202 void start_remote (int from_tty
, int extended_p
);
1203 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
1205 char *append_resumption (char *p
, char *endp
,
1206 ptid_t ptid
, int step
, gdb_signal siggnal
);
1207 int remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
1208 gdb_signal siggnal
);
1210 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
1212 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
1213 target_wait_flags options
);
1214 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
1215 target_wait_flags options
);
1217 ptid_t
process_stop_reply (stop_reply_up stop_reply
,
1218 target_waitstatus
*status
);
1220 ptid_t select_thread_for_ambiguous_stop_reply
1221 (const struct target_waitstatus
&status
);
1223 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
1225 void print_one_stopped_thread (thread_info
*thread
);
1226 void process_initial_stop_replies (int from_tty
);
1228 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
1231 void btrace_sync_conf (const btrace_config
*conf
);
1233 void remote_btrace_maybe_reopen ();
1235 void remove_new_children (threads_listing_context
*context
);
1236 void kill_new_fork_children (inferior
*inf
);
1237 void discard_pending_stop_replies (struct inferior
*inf
);
1238 int stop_reply_queue_length ();
1240 void check_pending_events_prevent_wildcard_vcont
1241 (bool *may_global_wildcard_vcont
);
1243 void discard_pending_stop_replies_in_queue ();
1244 stop_reply_up
remote_notif_remove_queued_reply (ptid_t ptid
);
1245 stop_reply_up
queued_stop_reply (ptid_t ptid
);
1246 int peek_stop_reply (ptid_t ptid
);
1247 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
1249 void remote_stop_ns (ptid_t ptid
);
1250 void remote_interrupt_as ();
1251 void remote_interrupt_ns ();
1253 char *remote_get_noisy_reply ();
1254 int remote_query_attached (int pid
);
1255 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
1258 ptid_t
remote_current_thread (ptid_t oldpid
);
1259 ptid_t
get_current_thread (const char *wait_status
);
1261 void set_thread (ptid_t ptid
, int gen
);
1262 void set_general_thread (ptid_t ptid
);
1263 void set_continue_thread (ptid_t ptid
);
1264 void set_general_process ();
1266 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
1268 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
1269 gdb_ext_thread_info
*info
);
1270 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
1271 gdb_ext_thread_info
*info
);
1273 int parse_threadlist_response (const char *pkt
, int result_limit
,
1274 threadref
*original_echo
,
1275 threadref
*resultlist
,
1277 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
1278 int result_limit
, int *done
, int *result_count
,
1279 threadref
*threadlist
);
1281 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1282 void *context
, int looplimit
);
1284 int remote_get_threads_with_ql (threads_listing_context
*context
);
1285 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
1286 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
1288 void extended_remote_restart ();
1290 void get_offsets ();
1292 void remote_check_symbols ();
1294 void remote_supported_packet (const struct protocol_feature
*feature
,
1295 enum packet_support support
,
1296 const char *argument
);
1298 void remote_query_supported ();
1300 void remote_packet_size (const protocol_feature
*feature
,
1301 packet_support support
, const char *value
);
1302 void remote_supported_thread_options (const protocol_feature
*feature
,
1303 enum packet_support support
,
1306 void remote_serial_quit_handler ();
1308 void remote_detach_pid (int pid
);
1310 void remote_vcont_probe ();
1312 void remote_resume_with_hc (ptid_t ptid
, int step
,
1313 gdb_signal siggnal
);
1315 void send_interrupt_sequence ();
1316 void interrupt_query ();
1318 void remote_notif_get_pending_events (const notif_client
*nc
);
1320 int fetch_register_using_p (struct regcache
*regcache
,
1322 int send_g_packet ();
1323 void process_g_packet (struct regcache
*regcache
);
1324 void fetch_registers_using_g (struct regcache
*regcache
);
1325 int store_register_using_P (const struct regcache
*regcache
,
1327 void store_registers_using_G (const struct regcache
*regcache
);
1329 void set_remote_traceframe ();
1331 void check_binary_download (CORE_ADDR addr
);
1333 target_xfer_status
remote_write_bytes_aux (const char *header
,
1335 const gdb_byte
*myaddr
,
1338 ULONGEST
*xfered_len_units
,
1342 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
1343 const gdb_byte
*myaddr
, ULONGEST len
,
1344 int unit_size
, ULONGEST
*xfered_len
);
1346 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
1348 int unit_size
, ULONGEST
*xfered_len_units
);
1350 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
1354 ULONGEST
*xfered_len
);
1356 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
1357 gdb_byte
*myaddr
, ULONGEST len
,
1359 ULONGEST
*xfered_len
);
1361 packet_status
remote_send_printf (const char *format
, ...)
1362 ATTRIBUTE_PRINTF (2, 3);
1364 target_xfer_status
remote_flash_write (ULONGEST address
,
1365 ULONGEST length
, ULONGEST
*xfered_len
,
1366 const gdb_byte
*data
);
1368 int readchar (int timeout
);
1370 void remote_serial_write (const char *str
, int len
);
1371 void remote_serial_send_break ();
1373 int putpkt (const char *buf
);
1374 int putpkt_binary (const char *buf
, int cnt
);
1376 int putpkt (const gdb::char_vector
&buf
)
1378 return putpkt (buf
.data ());
1382 long read_frame (gdb::char_vector
*buf_p
);
1383 int getpkt (gdb::char_vector
*buf
, bool forever
= false,
1384 bool *is_notif
= nullptr);
1385 int remote_vkill (int pid
);
1386 void remote_kill_k ();
1388 void extended_remote_disable_randomization (int val
);
1389 int extended_remote_run (const std::string
&args
);
1391 void send_environment_packet (const char *action
,
1395 void extended_remote_environment_support ();
1396 void extended_remote_set_inferior_cwd ();
1398 target_xfer_status
remote_write_qxfer (const char *object_name
,
1400 const gdb_byte
*writebuf
,
1401 ULONGEST offset
, LONGEST len
,
1402 ULONGEST
*xfered_len
,
1403 const unsigned int which_packet
);
1405 target_xfer_status
remote_read_qxfer (const char *object_name
,
1407 gdb_byte
*readbuf
, ULONGEST offset
,
1409 ULONGEST
*xfered_len
,
1410 const unsigned int which_packet
);
1412 void push_stop_reply (stop_reply_up new_event
);
1414 bool vcont_r_supported ();
1416 remote_features m_features
;
1420 bool start_remote_1 (int from_tty
, int extended_p
);
1422 /* The remote state. Don't reference this directly. Use the
1423 get_remote_state method instead. */
1424 remote_state m_remote_state
;
1427 static const target_info extended_remote_target_info
= {
1429 N_("Extended remote target using gdb-specific protocol"),
1433 /* Set up the extended remote target by extending the standard remote
1434 target and adding to it. */
1436 class extended_remote_target final
: public remote_target
1439 const target_info
&info () const override
1440 { return extended_remote_target_info
; }
1442 /* Open an extended-remote connection. */
1443 static void open (const char *, int);
1445 bool can_create_inferior () override
{ return true; }
1446 void create_inferior (const char *, const std::string
&,
1447 char **, int) override
;
1449 void detach (inferior
*, int) override
;
1451 bool can_attach () override
{ return true; }
1452 void attach (const char *, int) override
;
1454 void post_attach (int) override
;
1455 bool supports_disable_randomization () override
;
1458 struct stop_reply
: public notif_event
1460 /* The identifier of the thread about this event */
1463 /* The remote state this event is associated with. When the remote
1464 connection, represented by a remote_state object, is closed,
1465 all the associated stop_reply events should be released. */
1466 struct remote_state
*rs
;
1468 struct target_waitstatus ws
;
1470 /* The architecture associated with the expedited registers. */
1473 /* Expedited registers. This makes remote debugging a bit more
1474 efficient for those targets that provide critical registers as
1475 part of their normal status mechanism (as another roundtrip to
1476 fetch them is avoided). */
1477 std::vector
<cached_reg_t
> regcache
;
1479 enum target_stop_reason stop_reason
;
1481 CORE_ADDR watch_data_address
;
1486 /* Return TARGET as a remote_target if it is one, else nullptr. */
1488 static remote_target
*
1489 as_remote_target (process_stratum_target
*target
)
1491 return dynamic_cast<remote_target
*> (target
);
1497 is_remote_target (process_stratum_target
*target
)
1499 return as_remote_target (target
) != nullptr;
1505 remote_register_is_expedited (int regnum
)
1507 remote_target
*rt
= as_remote_target (current_inferior ()->process_target ());
1512 remote_state
*rs
= rt
->get_remote_state ();
1513 return rs
->last_seen_expedited_registers
.count (regnum
) > 0;
1516 /* Per-program-space data key. */
1517 static const registry
<program_space
>::key
<char, gdb::xfree_deleter
<char>>
1520 /* The variable registered as the control variable used by the
1521 remote exec-file commands. While the remote exec-file setting is
1522 per-program-space, the set/show machinery uses this as the
1523 location of the remote exec-file value. */
1524 static std::string remote_exec_file_var
;
1526 /* The size to align memory write packets, when practical. The protocol
1527 does not guarantee any alignment, and gdb will generate short
1528 writes and unaligned writes, but even as a best-effort attempt this
1529 can improve bulk transfers. For instance, if a write is misaligned
1530 relative to the target's data bus, the stub may need to make an extra
1531 round trip fetching data from the target. This doesn't make a
1532 huge difference, but it's easy to do, so we try to be helpful.
1534 The alignment chosen is arbitrary; usually data bus width is
1535 important here, not the possibly larger cache line size. */
1536 enum { REMOTE_ALIGN_WRITES
= 16 };
1538 /* Prototypes for local functions. */
1540 static int hexnumlen (ULONGEST num
);
1542 static int stubhex (int ch
);
1544 static int hexnumstr (char *, ULONGEST
);
1546 static int hexnumnstr (char *, ULONGEST
, int);
1548 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1550 static int stub_unpack_int (const char *buff
, int fieldlength
);
1552 static void set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
1553 cmd_list_element
*c
);
1555 static void show_packet_config_cmd (ui_file
*file
,
1556 const unsigned int which_packet
,
1557 remote_target
*remote
);
1559 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1561 struct cmd_list_element
*c
,
1564 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1566 static bool remote_read_description_p (struct target_ops
*target
);
1568 static void remote_console_output (const char *msg
, ui_file
*stream
);
1570 static void remote_btrace_reset (remote_state
*rs
);
1572 [[noreturn
]] static void remote_unpush_and_throw (remote_target
*target
);
1576 static struct cmd_list_element
*remote_cmdlist
;
1578 /* For "set remote" and "show remote". */
1580 static struct cmd_list_element
*remote_set_cmdlist
;
1581 static struct cmd_list_element
*remote_show_cmdlist
;
1583 /* Controls whether GDB is willing to use range stepping. */
1585 static bool use_range_stepping
= true;
1587 /* From the remote target's point of view, each thread is in one of these three
1589 enum class resume_state
1591 /* Not resumed - we haven't been asked to resume this thread. */
1594 /* We have been asked to resume this thread, but haven't sent a vCont action
1595 for it yet. We'll need to consider it next time commit_resume is
1597 RESUMED_PENDING_VCONT
,
1599 /* We have been asked to resume this thread, and we have sent a vCont action
1604 /* Information about a thread's pending vCont-resume. Used when a thread is in
1605 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1606 stores this information which is then picked up by
1607 remote_target::commit_resume to know which is the proper action for this
1608 thread to include in the vCont packet. */
1609 struct resumed_pending_vcont_info
1611 /* True if the last resume call for this thread was a step request, false
1612 if a continue request. */
1615 /* The signal specified in the last resume call for this thread. */
1619 /* Private data that we'll store in (struct thread_info)->priv. */
1620 struct remote_thread_info
: public private_thread_info
1626 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1627 sequence of bytes. */
1628 gdb::byte_vector thread_handle
;
1630 /* Whether the target stopped for a breakpoint/watchpoint. */
1631 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1633 /* This is set to the data address of the access causing the target
1634 to stop for a watchpoint. */
1635 CORE_ADDR watch_data_address
= 0;
1637 /* Get the thread's resume state. */
1638 enum resume_state
get_resume_state () const
1640 return m_resume_state
;
1643 /* Put the thread in the NOT_RESUMED state. */
1644 void set_not_resumed ()
1646 m_resume_state
= resume_state::NOT_RESUMED
;
1649 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1650 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1652 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1653 m_resumed_pending_vcont_info
.step
= step
;
1654 m_resumed_pending_vcont_info
.sig
= sig
;
1657 /* Get the information this thread's pending vCont-resumption.
1659 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1661 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1663 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1665 return m_resumed_pending_vcont_info
;
1668 /* Put the thread in the VCONT_RESUMED state. */
1671 m_resume_state
= resume_state::RESUMED
;
1675 /* Resume state for this thread. This is used to implement vCont action
1676 coalescing (only when the target operates in non-stop mode).
1678 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1679 which notes that this thread must be considered in the next commit_resume
1682 remote_target::commit_resume sends a vCont packet with actions for the
1683 threads in the RESUMED_PENDING_VCONT state and moves them to the
1684 VCONT_RESUMED state.
1686 When reporting a stop to the core for a thread, that thread is moved back
1687 to the NOT_RESUMED state. */
1688 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1690 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1691 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1694 remote_state::remote_state ()
1699 remote_state::~remote_state ()
1701 xfree (this->last_pass_packet
);
1702 xfree (this->last_program_signals_packet
);
1703 xfree (this->finished_object
);
1704 xfree (this->finished_annex
);
1707 /* Utility: generate error from an incoming stub packet. */
1709 trace_error (char *buf
)
1712 return; /* not an error msg */
1715 case '1': /* malformed packet error */
1716 if (*++buf
== '0') /* general case: */
1717 error (_("remote.c: error in outgoing packet."));
1719 error (_("remote.c: error in outgoing packet at field #%ld."),
1720 strtol (buf
, NULL
, 16));
1722 error (_("Target returns error code '%s'."), buf
);
1726 /* Utility: wait for reply from stub, while accepting "O" packets. */
1729 remote_target::remote_get_noisy_reply ()
1731 struct remote_state
*rs
= get_remote_state ();
1733 do /* Loop on reply from remote stub. */
1737 QUIT
; /* Allow user to bail out with ^C. */
1739 buf
= rs
->buf
.data ();
1742 else if (startswith (buf
, "qRelocInsn:"))
1745 CORE_ADDR from
, to
, org_to
;
1747 int adjusted_size
= 0;
1750 p
= buf
+ strlen ("qRelocInsn:");
1751 pp
= unpack_varlen_hex (p
, &ul
);
1753 error (_("invalid qRelocInsn packet: %s"), buf
);
1757 unpack_varlen_hex (p
, &ul
);
1764 gdbarch_relocate_instruction (current_inferior ()->arch (),
1768 catch (const gdb_exception
&ex
)
1770 if (ex
.error
== MEMORY_ERROR
)
1772 /* Propagate memory errors silently back to the
1773 target. The stub may have limited the range of
1774 addresses we can write to, for example. */
1778 /* Something unexpectedly bad happened. Be verbose
1779 so we can tell what, and propagate the error back
1780 to the stub, so it doesn't get stuck waiting for
1782 exception_fprintf (gdb_stderr
, ex
,
1783 _("warning: relocating instruction: "));
1790 adjusted_size
= to
- org_to
;
1792 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1796 else if (buf
[0] == 'O' && buf
[1] != 'K')
1798 /* 'O' message from stub */
1799 remote_console_output (buf
+ 1, gdb_stdtarg
);
1802 return buf
; /* Here's the actual reply. */
1807 struct remote_arch_state
*
1808 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1810 remote_arch_state
*rsa
;
1812 auto it
= this->m_arch_states
.find (gdbarch
);
1813 if (it
== this->m_arch_states
.end ())
1815 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1816 std::forward_as_tuple (gdbarch
),
1817 std::forward_as_tuple (gdbarch
));
1818 rsa
= &p
.first
->second
;
1820 /* Make sure that the packet buffer is plenty big enough for
1821 this architecture. */
1822 if (this->buf
.size () < rsa
->remote_packet_size
)
1823 this->buf
.resize (2 * rsa
->remote_packet_size
);
1831 /* Fetch the global remote target state. */
1834 remote_target::get_remote_state ()
1836 /* Make sure that the remote architecture state has been
1837 initialized, because doing so might reallocate rs->buf. Any
1838 function which calls getpkt also needs to be mindful of changes
1839 to rs->buf, but this call limits the number of places which run
1841 m_remote_state
.get_remote_arch_state (current_inferior ()->arch ());
1843 return &m_remote_state
;
1846 /* Fetch the remote exec-file from the current program space. */
1849 get_remote_exec_file (void)
1851 char *remote_exec_file
;
1853 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1854 if (remote_exec_file
== NULL
)
1857 return remote_exec_file
;
1860 /* Set the remote exec file for PSPACE. */
1863 set_pspace_remote_exec_file (struct program_space
*pspace
,
1864 const char *remote_exec_file
)
1866 char *old_file
= remote_pspace_data
.get (pspace
);
1869 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1872 /* The "set/show remote exec-file" set command hook. */
1875 set_remote_exec_file (const char *ignored
, int from_tty
,
1876 struct cmd_list_element
*c
)
1878 set_pspace_remote_exec_file (current_program_space
,
1879 remote_exec_file_var
.c_str ());
1882 /* The "set/show remote exec-file" show command hook. */
1885 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1886 struct cmd_list_element
*cmd
, const char *value
)
1888 gdb_printf (file
, "%s\n", get_remote_exec_file ());
1892 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1894 int regnum
, num_remote_regs
, offset
;
1895 struct packet_reg
**remote_regs
;
1897 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1899 struct packet_reg
*r
= ®s
[regnum
];
1901 if (register_size (gdbarch
, regnum
) == 0)
1902 /* Do not try to fetch zero-sized (placeholder) registers. */
1905 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1910 /* Define the g/G packet format as the contents of each register
1911 with a remote protocol number, in order of ascending protocol
1914 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1915 for (num_remote_regs
= 0, regnum
= 0;
1916 regnum
< gdbarch_num_regs (gdbarch
);
1918 if (regs
[regnum
].pnum
!= -1)
1919 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1921 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1922 [] (const packet_reg
*a
, const packet_reg
*b
)
1923 { return a
->pnum
< b
->pnum
; });
1925 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1927 remote_regs
[regnum
]->in_g_packet
= true;
1928 remote_regs
[regnum
]->offset
= offset
;
1929 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1935 /* Given the architecture described by GDBARCH, return the remote
1936 protocol register's number and the register's offset in the g/G
1937 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1938 If the target does not have a mapping for REGNUM, return false,
1939 otherwise, return true. */
1942 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1943 int *pnum
, int *poffset
)
1945 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1947 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1949 map_regcache_remote_table (gdbarch
, regs
.data ());
1951 *pnum
= regs
[regnum
].pnum
;
1952 *poffset
= regs
[regnum
].offset
;
1957 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1959 /* Use the architecture to build a regnum<->pnum table, which will be
1960 1:1 unless a feature set specifies otherwise. */
1961 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1963 /* Record the maximum possible size of the g packet - it may turn out
1965 this->sizeof_g_packet
1966 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1968 /* Default maximum number of characters in a packet body. Many
1969 remote stubs have a hardwired buffer size of 400 bytes
1970 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1971 as the maximum packet-size to ensure that the packet and an extra
1972 NUL character can always fit in the buffer. This stops GDB
1973 trashing stubs that try to squeeze an extra NUL into what is
1974 already a full buffer (As of 1999-12-04 that was most stubs). */
1975 this->remote_packet_size
= 400 - 1;
1977 /* This one is filled in when a ``g'' packet is received. */
1978 this->actual_register_packet_size
= 0;
1980 /* Should rsa->sizeof_g_packet needs more space than the
1981 default, adjust the size accordingly. Remember that each byte is
1982 encoded as two characters. 32 is the overhead for the packet
1983 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1984 (``$NN:G...#NN'') is a better guess, the below has been padded a
1986 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1987 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1990 /* Get a pointer to the current remote target. If not connected to a
1991 remote target, return NULL. */
1993 static remote_target
*
1994 get_current_remote_target ()
1996 target_ops
*proc_target
= current_inferior ()->process_target ();
1997 return dynamic_cast<remote_target
*> (proc_target
);
2000 /* Return the current allowed size of a remote packet. This is
2001 inferred from the current architecture, and should be used to
2002 limit the length of outgoing packets. */
2004 remote_target::get_remote_packet_size ()
2006 struct remote_state
*rs
= get_remote_state ();
2007 remote_arch_state
*rsa
2008 = rs
->get_remote_arch_state (current_inferior ()->arch ());
2010 if (rs
->explicit_packet_size
)
2011 return rs
->explicit_packet_size
;
2013 return rsa
->remote_packet_size
;
2016 static struct packet_reg
*
2017 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
2020 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
2024 struct packet_reg
*r
= &rsa
->regs
[regnum
];
2026 gdb_assert (r
->regnum
== regnum
);
2031 static struct packet_reg
*
2032 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
2037 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
2039 struct packet_reg
*r
= &rsa
->regs
[i
];
2041 if (r
->pnum
== pnum
)
2047 /* Allow the user to specify what sequence to send to the remote
2048 when he requests a program interruption: Although ^C is usually
2049 what remote systems expect (this is the default, here), it is
2050 sometimes preferable to send a break. On other systems such
2051 as the Linux kernel, a break followed by g, which is Magic SysRq g
2052 is required in order to interrupt the execution. */
2053 const char interrupt_sequence_control_c
[] = "Ctrl-C";
2054 const char interrupt_sequence_break
[] = "BREAK";
2055 const char interrupt_sequence_break_g
[] = "BREAK-g";
2056 static const char *const interrupt_sequence_modes
[] =
2058 interrupt_sequence_control_c
,
2059 interrupt_sequence_break
,
2060 interrupt_sequence_break_g
,
2063 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
2066 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
2067 struct cmd_list_element
*c
,
2070 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
2072 _("Send the ASCII ETX character (Ctrl-c) "
2073 "to the remote target to interrupt the "
2074 "execution of the program.\n"));
2075 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
2077 _("send a break signal to the remote target "
2078 "to interrupt the execution of the program.\n"));
2079 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
2081 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
2082 "the remote target to interrupt the execution "
2083 "of Linux kernel.\n"));
2085 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
2086 interrupt_sequence_mode
);
2089 /* This boolean variable specifies whether interrupt_sequence is sent
2090 to the remote target when gdb connects to it.
2091 This is mostly needed when you debug the Linux kernel: The Linux kernel
2092 expects BREAK g which is Magic SysRq g for connecting gdb. */
2093 static bool interrupt_on_connect
= false;
2095 /* This variable is used to implement the "set/show remotebreak" commands.
2096 Since these commands are now deprecated in favor of "set/show remote
2097 interrupt-sequence", it no longer has any effect on the code. */
2098 static bool remote_break
;
2101 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2104 interrupt_sequence_mode
= interrupt_sequence_break
;
2106 interrupt_sequence_mode
= interrupt_sequence_control_c
;
2110 show_remotebreak (struct ui_file
*file
, int from_tty
,
2111 struct cmd_list_element
*c
,
2116 /* This variable sets the number of bits in an address that are to be
2117 sent in a memory ("M" or "m") packet. Normally, after stripping
2118 leading zeros, the entire address would be sent. This variable
2119 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
2120 initial implementation of remote.c restricted the address sent in
2121 memory packets to ``host::sizeof long'' bytes - (typically 32
2122 bits). Consequently, for 64 bit targets, the upper 32 bits of an
2123 address was never sent. Since fixing this bug may cause a break in
2124 some remote targets this variable is principally provided to
2125 facilitate backward compatibility. */
2127 static unsigned int remote_address_size
;
2130 /* The default max memory-write-packet-size, when the setting is
2131 "fixed". The 16k is historical. (It came from older GDB's using
2132 alloca for buffers and the knowledge (folklore?) that some hosts
2133 don't cope very well with large alloca calls.) */
2134 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
2136 /* The minimum remote packet size for memory transfers. Ensures we
2137 can write at least one byte. */
2138 #define MIN_MEMORY_PACKET_SIZE 20
2140 /* Get the memory packet size, assuming it is fixed. */
2143 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
2145 gdb_assert (config
->fixed_p
);
2147 if (config
->size
<= 0)
2148 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
2150 return config
->size
;
2153 /* Compute the current size of a read/write packet. Since this makes
2154 use of ``actual_register_packet_size'' the computation is dynamic. */
2157 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
2159 struct remote_state
*rs
= get_remote_state ();
2160 remote_arch_state
*rsa
2161 = rs
->get_remote_arch_state (current_inferior ()->arch ());
2164 if (config
->fixed_p
)
2165 what_they_get
= get_fixed_memory_packet_size (config
);
2168 what_they_get
= get_remote_packet_size ();
2169 /* Limit the packet to the size specified by the user. */
2170 if (config
->size
> 0
2171 && what_they_get
> config
->size
)
2172 what_they_get
= config
->size
;
2174 /* Limit it to the size of the targets ``g'' response unless we have
2175 permission from the stub to use a larger packet size. */
2176 if (rs
->explicit_packet_size
== 0
2177 && rsa
->actual_register_packet_size
> 0
2178 && what_they_get
> rsa
->actual_register_packet_size
)
2179 what_they_get
= rsa
->actual_register_packet_size
;
2181 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
2182 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
2184 /* Make sure there is room in the global buffer for this packet
2185 (including its trailing NUL byte). */
2186 if (rs
->buf
.size () < what_they_get
+ 1)
2187 rs
->buf
.resize (2 * what_they_get
);
2189 return what_they_get
;
2192 /* Update the size of a read/write packet. If they user wants
2193 something really big then do a sanity check. */
2196 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
,
2197 bool target_connected
)
2199 int fixed_p
= config
->fixed_p
;
2200 long size
= config
->size
;
2203 error (_("Argument required (integer, \"fixed\" or \"limit\")."));
2204 else if (strcmp (args
, "hard") == 0
2205 || strcmp (args
, "fixed") == 0)
2207 else if (strcmp (args
, "soft") == 0
2208 || strcmp (args
, "limit") == 0)
2214 size
= strtoul (args
, &end
, 0);
2216 error (_("Invalid %s (bad syntax)."), config
->name
);
2218 /* Instead of explicitly capping the size of a packet to or
2219 disallowing it, the user is allowed to set the size to
2220 something arbitrarily large. */
2224 if (fixed_p
&& !config
->fixed_p
)
2226 /* So that the query shows the correct value. */
2227 long query_size
= (size
<= 0
2228 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
2231 if (target_connected
2232 && !query (_("The target may not be able to correctly handle a %s\n"
2233 "of %ld bytes. Change the packet size? "),
2234 config
->name
, query_size
))
2235 error (_("Packet size not changed."));
2236 else if (!target_connected
2237 && !query (_("Future remote targets may not be able to "
2238 "correctly handle a %s\nof %ld bytes. Change the "
2239 "packet size for future remote targets? "),
2240 config
->name
, query_size
))
2241 error (_("Packet size not changed."));
2243 /* Update the config. */
2244 config
->fixed_p
= fixed_p
;
2245 config
->size
= size
;
2247 const char *target_type
= get_target_type_name (target_connected
);
2248 gdb_printf (_("The %s %s is set to \"%s\".\n"), config
->name
, target_type
,
2253 /* Show the memory-read or write-packet size configuration CONFIG of the
2254 target REMOTE. If REMOTE is nullptr, the default configuration for future
2255 remote targets should be passed in CONFIG. */
2258 show_memory_packet_size (memory_packet_config
*config
, remote_target
*remote
)
2260 const char *target_type
= get_target_type_name (remote
!= nullptr);
2262 if (config
->size
== 0)
2263 gdb_printf (_("The %s %s is 0 (default). "), config
->name
, target_type
);
2265 gdb_printf (_("The %s %s is %ld. "), config
->name
, target_type
,
2268 if (config
->fixed_p
)
2269 gdb_printf (_("Packets are fixed at %ld bytes.\n"),
2270 get_fixed_memory_packet_size (config
));
2273 if (remote
!= nullptr)
2274 gdb_printf (_("Packets are limited to %ld bytes.\n"),
2275 remote
->get_memory_packet_size (config
));
2277 gdb_puts ("The actual limit will be further reduced "
2278 "dependent on the target.\n");
2282 /* Configure the memory-write-packet size of the currently selected target. If
2283 no target is available, the default configuration for future remote targets
2287 set_memory_write_packet_size (const char *args
, int from_tty
)
2289 remote_target
*remote
= get_current_remote_target ();
2290 if (remote
!= nullptr)
2292 set_memory_packet_size
2293 (args
, &remote
->m_features
.m_memory_write_packet_config
, true);
2297 memory_packet_config
* config
= &memory_write_packet_config
;
2298 set_memory_packet_size (args
, config
, false);
2302 /* Display the memory-write-packet size of the currently selected target. If
2303 no target is available, the default configuration for future remote targets
2307 show_memory_write_packet_size (const char *args
, int from_tty
)
2309 remote_target
*remote
= get_current_remote_target ();
2310 if (remote
!= nullptr)
2311 show_memory_packet_size (&remote
->m_features
.m_memory_write_packet_config
,
2314 show_memory_packet_size (&memory_write_packet_config
, nullptr);
2317 /* Show the number of hardware watchpoints that can be used. */
2320 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
2321 struct cmd_list_element
*c
,
2324 gdb_printf (file
, _("The maximum number of target hardware "
2325 "watchpoints is %s.\n"), value
);
2328 /* Show the length limit (in bytes) for hardware watchpoints. */
2331 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
2332 struct cmd_list_element
*c
,
2335 gdb_printf (file
, _("The maximum length (in bytes) of a target "
2336 "hardware watchpoint is %s.\n"), value
);
2339 /* Show the number of hardware breakpoints that can be used. */
2342 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
2343 struct cmd_list_element
*c
,
2346 gdb_printf (file
, _("The maximum number of target hardware "
2347 "breakpoints is %s.\n"), value
);
2350 /* Controls the maximum number of characters to display in the debug output
2351 for each remote packet. The remaining characters are omitted. */
2353 static int remote_packet_max_chars
= 512;
2355 /* Show the maximum number of characters to display for each remote packet
2356 when remote debugging is enabled. */
2359 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
2360 struct cmd_list_element
*c
,
2363 gdb_printf (file
, _("Number of remote packet characters to "
2364 "display is %s.\n"), value
);
2368 remote_target::get_memory_write_packet_size ()
2370 return get_memory_packet_size (&m_features
.m_memory_write_packet_config
);
2373 /* Configure the memory-read-packet size of the currently selected target. If
2374 no target is available, the default configuration for future remote targets
2378 set_memory_read_packet_size (const char *args
, int from_tty
)
2380 remote_target
*remote
= get_current_remote_target ();
2381 if (remote
!= nullptr)
2382 set_memory_packet_size
2383 (args
, &remote
->m_features
.m_memory_read_packet_config
, true);
2386 memory_packet_config
* config
= &memory_read_packet_config
;
2387 set_memory_packet_size (args
, config
, false);
2392 /* Display the memory-read-packet size of the currently selected target. If
2393 no target is available, the default configuration for future remote targets
2397 show_memory_read_packet_size (const char *args
, int from_tty
)
2399 remote_target
*remote
= get_current_remote_target ();
2400 if (remote
!= nullptr)
2401 show_memory_packet_size (&remote
->m_features
.m_memory_read_packet_config
,
2404 show_memory_packet_size (&memory_read_packet_config
, nullptr);
2408 remote_target::get_memory_read_packet_size ()
2410 long size
= get_memory_packet_size (&m_features
.m_memory_read_packet_config
);
2412 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
2413 extra buffer size argument before the memory read size can be
2414 increased beyond this. */
2415 if (size
> get_remote_packet_size ())
2416 size
= get_remote_packet_size ();
2420 static enum packet_support
packet_config_support (const packet_config
*config
);
2424 set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
2425 cmd_list_element
*c
)
2427 remote_target
*remote
= get_current_remote_target ();
2428 gdb_assert (c
->var
.has_value ());
2430 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2431 const int packet_idx
= std::distance (remote_protocol_packets
,
2434 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2436 const char *name
= packets_descriptions
[packet_idx
].name
;
2437 const auto_boolean value
= c
->var
->get
<auto_boolean
> ();
2438 const char *support
= get_packet_support_name (value
);
2439 const char *target_type
= get_target_type_name (remote
!= nullptr);
2441 if (remote
!= nullptr)
2442 remote
->m_features
.m_protocol_packets
[packet_idx
].detect
= value
;
2444 remote_protocol_packets
[packet_idx
].detect
= value
;
2446 gdb_printf (_("Support for the '%s' packet %s is set to \"%s\".\n"), name
,
2447 target_type
, support
);
2451 internal_error (_("Could not find config for %s"), c
->name
);
2455 show_packet_config_cmd (ui_file
*file
, const unsigned int which_packet
,
2456 remote_target
*remote
)
2458 const char *support
= "internal-error";
2459 const char *target_type
= get_target_type_name (remote
!= nullptr);
2461 packet_config
*config
;
2462 if (remote
!= nullptr)
2463 config
= &remote
->m_features
.m_protocol_packets
[which_packet
];
2465 config
= &remote_protocol_packets
[which_packet
];
2467 switch (packet_config_support (config
))
2470 support
= "enabled";
2472 case PACKET_DISABLE
:
2473 support
= "disabled";
2475 case PACKET_SUPPORT_UNKNOWN
:
2476 support
= "unknown";
2479 switch (config
->detect
)
2481 case AUTO_BOOLEAN_AUTO
:
2483 _("Support for the '%s' packet %s is \"auto\", "
2485 packets_descriptions
[which_packet
].name
, target_type
,
2488 case AUTO_BOOLEAN_TRUE
:
2489 case AUTO_BOOLEAN_FALSE
:
2491 _("Support for the '%s' packet %s is \"%s\".\n"),
2492 packets_descriptions
[which_packet
].name
, target_type
,
2493 get_packet_support_name (config
->detect
));
2499 add_packet_config_cmd (const unsigned int which_packet
, const char *name
,
2500 const char *title
, int legacy
)
2502 packets_descriptions
[which_packet
].name
= name
;
2503 packets_descriptions
[which_packet
].title
= title
;
2505 packet_config
*config
= &remote_protocol_packets
[which_packet
];
2507 gdb::unique_xmalloc_ptr
<char> set_doc
2508 = xstrprintf ("Set use of remote protocol `%s' packet.", name
);
2509 gdb::unique_xmalloc_ptr
<char> show_doc
2510 = xstrprintf ("Show current use of remote protocol `%s' packet.", name
);
2511 /* set/show TITLE-packet {auto,on,off} */
2512 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
2513 set_show_commands cmds
2514 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
2515 &config
->detect
, set_doc
.get (),
2516 show_doc
.get (), NULL
, /* help_doc */
2517 set_remote_protocol_packet_cmd
,
2518 show_remote_protocol_packet_cmd
,
2519 &remote_set_cmdlist
, &remote_show_cmdlist
);
2520 cmds
.show
->set_context (config
);
2521 cmds
.set
->set_context (config
);
2523 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
2526 /* It's not clear who should take ownership of the LEGACY_NAME string
2527 created below, so, for now, place the string into a static vector
2528 which ensures the strings is released when GDB exits. */
2529 static std::vector
<gdb::unique_xmalloc_ptr
<char>> legacy_names
;
2530 gdb::unique_xmalloc_ptr
<char> legacy_name
2531 = xstrprintf ("%s-packet", name
);
2532 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
2533 &remote_set_cmdlist
);
2534 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
2535 &remote_show_cmdlist
);
2536 legacy_names
.emplace_back (std::move (legacy_name
));
2540 /* Check GDBserver's reply packet. Return packet_result structure
2541 which contains the packet_status enum and an error message for the
2544 An error packet can always take the form Exx (where xx is a hex
2546 static packet_result
2547 packet_check_result (const char *buf
)
2551 /* The stub recognized the packet request. Check that the
2552 operation succeeded. */
2554 && isxdigit (buf
[1]) && isxdigit (buf
[2])
2556 /* "Enn" - definitely an error. */
2557 return packet_result::make_numeric_error (buf
+ 1);
2559 /* Always treat "E." as an error. This will be used for
2560 more verbose error messages, such as E.memtypes. */
2561 if (buf
[0] == 'E' && buf
[1] == '.')
2564 return packet_result::make_textual_error (buf
+ 2);
2566 return packet_result::make_textual_error ("no error provided");
2569 /* The packet may or may not be OK. Just assume it is. */
2570 return packet_result::make_ok ();
2574 /* The stub does not support the packet. */
2575 return packet_result::make_unknown ();
2579 static packet_result
2580 packet_check_result (const gdb::char_vector
&buf
)
2582 return packet_check_result (buf
.data ());
2586 remote_features::packet_ok (const char *buf
, const int which_packet
)
2588 packet_config
*config
= &m_protocol_packets
[which_packet
];
2589 packet_description
*descr
= &packets_descriptions
[which_packet
];
2591 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2592 && config
->support
== PACKET_DISABLE
)
2593 internal_error (_("packet_ok: attempt to use a disabled packet"));
2595 packet_result result
= packet_check_result (buf
);
2596 switch (result
.status ())
2600 /* The stub recognized the packet request. */
2601 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2603 remote_debug_printf ("Packet %s (%s) is supported",
2604 descr
->name
, descr
->title
);
2605 config
->support
= PACKET_ENABLE
;
2608 case PACKET_UNKNOWN
:
2609 /* The stub does not support the packet. */
2610 if (config
->detect
== AUTO_BOOLEAN_AUTO
2611 && config
->support
== PACKET_ENABLE
)
2613 /* If the stub previously indicated that the packet was
2614 supported then there is a protocol error. */
2615 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2616 descr
->name
, descr
->title
);
2618 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2620 /* The user set it wrong. */
2621 error (_("Enabled packet %s (%s) not recognized by stub"),
2622 descr
->name
, descr
->title
);
2625 remote_debug_printf ("Packet %s (%s) is NOT supported", descr
->name
,
2627 config
->support
= PACKET_DISABLE
;
2635 remote_features::packet_ok (const gdb::char_vector
&buf
, const int which_packet
)
2637 return packet_ok (buf
.data (), which_packet
);
2640 /* Returns whether a given packet or feature is supported. This takes
2641 into account the state of the corresponding "set remote foo-packet"
2642 command, which may be used to bypass auto-detection. */
2644 static enum packet_support
2645 packet_config_support (const packet_config
*config
)
2647 switch (config
->detect
)
2649 case AUTO_BOOLEAN_TRUE
:
2650 return PACKET_ENABLE
;
2651 case AUTO_BOOLEAN_FALSE
:
2652 return PACKET_DISABLE
;
2653 case AUTO_BOOLEAN_AUTO
:
2654 return config
->support
;
2656 gdb_assert_not_reached ("bad switch");
2661 remote_features::packet_support (int packet
) const
2663 const packet_config
*config
= &m_protocol_packets
[packet
];
2664 return packet_config_support (config
);
2668 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2669 struct cmd_list_element
*c
,
2672 remote_target
*remote
= get_current_remote_target ();
2673 gdb_assert (c
->var
.has_value ());
2675 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2676 const int packet_idx
= std::distance (remote_protocol_packets
,
2679 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2681 show_packet_config_cmd (file
, packet_idx
, remote
);
2684 internal_error (_("Could not find config for %s"), c
->name
);
2687 /* Should we try one of the 'Z' requests? */
2691 Z_PACKET_SOFTWARE_BP
,
2692 Z_PACKET_HARDWARE_BP
,
2699 /* For compatibility with older distributions. Provide a ``set remote
2700 Z-packet ...'' command that updates all the Z packet types. */
2702 static enum auto_boolean remote_Z_packet_detect
;
2705 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2706 struct cmd_list_element
*c
)
2708 remote_target
*remote
= get_current_remote_target ();
2711 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2713 if (remote
!= nullptr)
2714 remote
->m_features
.m_protocol_packets
[PACKET_Z0
+ i
].detect
2715 = remote_Z_packet_detect
;
2717 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2720 const char *support
= get_packet_support_name (remote_Z_packet_detect
);
2721 const char *target_type
= get_target_type_name (remote
!= nullptr);
2722 gdb_printf (_("Use of Z packets %s is set to \"%s\".\n"), target_type
,
2728 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2729 struct cmd_list_element
*c
,
2732 remote_target
*remote
= get_current_remote_target ();
2735 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2736 show_packet_config_cmd (file
, PACKET_Z0
+ i
, remote
);
2739 /* Insert fork catchpoint target routine. If fork events are enabled
2740 then return success, nothing more to do. */
2743 remote_target::insert_fork_catchpoint (int pid
)
2745 return !m_features
.remote_fork_event_p ();
2748 /* Remove fork catchpoint target routine. Nothing to do, just
2752 remote_target::remove_fork_catchpoint (int pid
)
2757 /* Insert vfork catchpoint target routine. If vfork events are enabled
2758 then return success, nothing more to do. */
2761 remote_target::insert_vfork_catchpoint (int pid
)
2763 return !m_features
.remote_vfork_event_p ();
2766 /* Remove vfork catchpoint target routine. Nothing to do, just
2770 remote_target::remove_vfork_catchpoint (int pid
)
2775 /* Insert exec catchpoint target routine. If exec events are
2776 enabled, just return success. */
2779 remote_target::insert_exec_catchpoint (int pid
)
2781 return !m_features
.remote_exec_event_p ();
2784 /* Remove exec catchpoint target routine. Nothing to do, just
2788 remote_target::remove_exec_catchpoint (int pid
)
2795 /* Take advantage of the fact that the TID field is not used, to tag
2796 special ptids with it set to != 0. */
2797 static const ptid_t
magic_null_ptid (42000, -1, 1);
2798 static const ptid_t
not_sent_ptid (42000, -2, 1);
2799 static const ptid_t
any_thread_ptid (42000, 0, 1);
2801 /* Find out if the stub attached to PID (and hence GDB should offer to
2802 detach instead of killing it when bailing out). */
2805 remote_target::remote_query_attached (int pid
)
2807 struct remote_state
*rs
= get_remote_state ();
2808 size_t size
= get_remote_packet_size ();
2810 if (m_features
.packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2813 if (m_features
.remote_multi_process_p ())
2814 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2816 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2821 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_qAttached
);
2822 switch (result
.status ())
2825 if (strcmp (rs
->buf
.data (), "1") == 0)
2829 warning (_("Remote failure reply: %s"), result
.err_msg ());
2831 case PACKET_UNKNOWN
:
2838 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2839 has been invented by GDB, instead of reported by the target. Since
2840 we can be connected to a remote system before before knowing about
2841 any inferior, mark the target with execution when we find the first
2842 inferior. If ATTACHED is 1, then we had just attached to this
2843 inferior. If it is 0, then we just created this inferior. If it
2844 is -1, then try querying the remote stub to find out if it had
2845 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2846 attempt to open this inferior's executable as the main executable
2847 if no main executable is open already. */
2850 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2853 struct inferior
*inf
;
2855 /* Check whether this process we're learning about is to be
2856 considered attached, or if is to be considered to have been
2857 spawned by the stub. */
2859 attached
= remote_query_attached (pid
);
2861 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
2863 /* If the target shares code across all inferiors, then every
2864 attach adds a new inferior. */
2865 inf
= add_inferior (pid
);
2867 /* ... and every inferior is bound to the same program space.
2868 However, each inferior may still have its own address
2870 inf
->aspace
= maybe_new_address_space ();
2871 inf
->pspace
= current_program_space
;
2875 /* In the traditional debugging scenario, there's a 1-1 match
2876 between program/address spaces. We simply bind the inferior
2877 to the program space's address space. */
2878 inf
= current_inferior ();
2880 /* However, if the current inferior is already bound to a
2881 process, find some other empty inferior. */
2885 for (inferior
*it
: all_inferiors ())
2894 /* Since all inferiors were already bound to a process, add
2896 inf
= add_inferior_with_spaces ();
2898 switch_to_inferior_no_thread (inf
);
2899 inf
->push_target (this);
2900 inferior_appeared (inf
, pid
);
2903 inf
->attach_flag
= attached
;
2904 inf
->fake_pid_p
= fake_pid_p
;
2906 /* If no main executable is currently open then attempt to
2907 open the file that was executed to create this inferior. */
2908 if (try_open_exec
&& current_program_space
->exec_filename () == nullptr)
2909 exec_file_locate_attach (pid
, 0, 1);
2911 /* Check for exec file mismatch, and let the user solve it. */
2912 validate_exec_file (1);
2917 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2918 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2921 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2922 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2923 remote_state::starting_up flag) is true then the new thread is added
2924 silently, otherwise the new thread will be announced to the user. */
2927 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2930 struct remote_state
*rs
= get_remote_state ();
2931 struct thread_info
*thread
;
2933 /* GDB historically didn't pull threads in the initial connection
2934 setup. If the remote target doesn't even have a concept of
2935 threads (e.g., a bare-metal target), even if internally we
2936 consider that a single-threaded target, mentioning a new thread
2937 might be confusing to the user. Be silent then, preserving the
2938 age old behavior. */
2939 if (rs
->starting_up
|| silent_p
)
2940 thread
= add_thread_silent (this, ptid
);
2942 thread
= add_thread (this, ptid
);
2945 get_remote_thread_info (thread
)->set_resumed ();
2946 set_executing (this, ptid
, executing
);
2947 set_running (this, ptid
, running
);
2952 /* Come here when we learn about a thread id from the remote target.
2953 It may be the first time we hear about such thread, so take the
2954 opportunity to add it to GDB's thread list. In case this is the
2955 first time we're noticing its corresponding inferior, add it to
2956 GDB's inferior list as well. EXECUTING indicates whether the
2957 thread is (internally) executing or stopped. */
2960 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2962 /* In non-stop mode, we assume new found threads are (externally)
2963 running until proven otherwise with a stop reply. In all-stop,
2964 we can only get here if all threads are stopped. */
2965 bool running
= target_is_non_stop_p ();
2967 /* If this is a new thread, add it to GDB's thread list.
2968 If we leave it up to WFI to do this, bad things will happen. */
2970 thread_info
*tp
= this->find_thread (currthread
);
2971 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2973 /* We're seeing an event on a thread id we knew had exited.
2974 This has to be a new thread reusing the old id. Add it. */
2975 remote_add_thread (currthread
, running
, executing
, false);
2979 if (!in_thread_list (this, currthread
))
2981 struct inferior
*inf
= NULL
;
2982 int pid
= currthread
.pid ();
2984 if (inferior_ptid
.is_pid ()
2985 && pid
== inferior_ptid
.pid ())
2987 /* inferior_ptid has no thread member yet. This can happen
2988 with the vAttach -> remote_wait,"TAAthread:" path if the
2989 stub doesn't support qC. This is the first stop reported
2990 after an attach, so this is the main thread. Update the
2991 ptid in the thread list. */
2992 if (in_thread_list (this, ptid_t (pid
)))
2993 thread_change_ptid (this, inferior_ptid
, currthread
);
2997 = remote_add_thread (currthread
, running
, executing
, false);
2998 switch_to_thread (thr
);
3003 if (magic_null_ptid
== inferior_ptid
)
3005 /* inferior_ptid is not set yet. This can happen with the
3006 vRun -> remote_wait,"TAAthread:" path if the stub
3007 doesn't support qC. This is the first stop reported
3008 after an attach, so this is the main thread. Update the
3009 ptid in the thread list. */
3010 thread_change_ptid (this, inferior_ptid
, currthread
);
3014 /* When connecting to a target remote, or to a target
3015 extended-remote which already was debugging an inferior, we
3016 may not know about it yet. Add it before adding its child
3017 thread, so notifications are emitted in a sensible order. */
3018 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
3020 bool fake_pid_p
= !m_features
.remote_multi_process_p ();
3022 inf
= remote_add_inferior (fake_pid_p
,
3023 currthread
.pid (), -1, 1);
3026 /* This is really a new thread. Add it. */
3027 thread_info
*new_thr
3028 = remote_add_thread (currthread
, running
, executing
, false);
3030 /* If we found a new inferior, let the common code do whatever
3031 it needs to with it (e.g., read shared libraries, insert
3032 breakpoints), unless we're just setting up an all-stop
3036 struct remote_state
*rs
= get_remote_state ();
3038 if (!rs
->starting_up
)
3039 notice_new_inferior (new_thr
, executing
, 0);
3044 /* Return THREAD's private thread data, creating it if necessary. */
3046 static remote_thread_info
*
3047 get_remote_thread_info (thread_info
*thread
)
3049 gdb_assert (thread
!= NULL
);
3051 if (thread
->priv
== NULL
)
3052 thread
->priv
= std::make_unique
<remote_thread_info
> ();
3054 return gdb::checked_static_cast
<remote_thread_info
*> (thread
->priv
.get ());
3057 /* Return PTID's private thread data, creating it if necessary. */
3059 static remote_thread_info
*
3060 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
3062 thread_info
*thr
= target
->find_thread (ptid
);
3063 return get_remote_thread_info (thr
);
3066 /* Call this function as a result of
3067 1) A halt indication (T packet) containing a thread id
3068 2) A direct query of currthread
3069 3) Successful execution of set thread */
3072 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
3074 rs
->general_thread
= currthread
;
3077 /* If 'QPassSignals' is supported, tell the remote stub what signals
3078 it can simply pass through to the inferior without reporting. */
3081 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
3083 if (m_features
.packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
3085 char *pass_packet
, *p
;
3087 struct remote_state
*rs
= get_remote_state ();
3089 gdb_assert (pass_signals
.size () < 256);
3090 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
3092 if (pass_signals
[i
])
3095 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
3096 strcpy (pass_packet
, "QPassSignals:");
3097 p
= pass_packet
+ strlen (pass_packet
);
3098 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
3100 if (pass_signals
[i
])
3103 *p
++ = tohex (i
>> 4);
3104 *p
++ = tohex (i
& 15);
3113 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
3115 putpkt (pass_packet
);
3117 m_features
.packet_ok (rs
->buf
, PACKET_QPassSignals
);
3118 xfree (rs
->last_pass_packet
);
3119 rs
->last_pass_packet
= pass_packet
;
3122 xfree (pass_packet
);
3126 /* If 'QCatchSyscalls' is supported, tell the remote stub
3127 to report syscalls to GDB. */
3130 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
3131 gdb::array_view
<const int> syscall_counts
)
3133 const char *catch_packet
;
3136 if (m_features
.packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
3138 /* Not supported. */
3142 if (needed
&& any_count
== 0)
3144 /* Count how many syscalls are to be caught. */
3145 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
3147 if (syscall_counts
[i
] != 0)
3152 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
3153 pid
, needed
, any_count
, n_sysno
);
3155 std::string built_packet
;
3158 /* Prepare a packet with the sysno list, assuming max 8+1
3159 characters for a sysno. If the resulting packet size is too
3160 big, fallback on the non-selective packet. */
3161 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
3162 built_packet
.reserve (maxpktsz
);
3163 built_packet
= "QCatchSyscalls:1";
3166 /* Add in each syscall to be caught. */
3167 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
3169 if (syscall_counts
[i
] != 0)
3170 string_appendf (built_packet
, ";%zx", i
);
3173 if (built_packet
.size () > get_remote_packet_size ())
3175 /* catch_packet too big. Fallback to less efficient
3176 non selective mode, with GDB doing the filtering. */
3177 catch_packet
= "QCatchSyscalls:1";
3180 catch_packet
= built_packet
.c_str ();
3183 catch_packet
= "QCatchSyscalls:0";
3185 struct remote_state
*rs
= get_remote_state ();
3187 putpkt (catch_packet
);
3189 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_QCatchSyscalls
);
3190 if (result
.status () == PACKET_OK
)
3196 /* If 'QProgramSignals' is supported, tell the remote stub what
3197 signals it should pass through to the inferior when detaching. */
3200 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
3202 if (m_features
.packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
3206 struct remote_state
*rs
= get_remote_state ();
3208 gdb_assert (signals
.size () < 256);
3209 for (size_t i
= 0; i
< signals
.size (); i
++)
3214 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
3215 strcpy (packet
, "QProgramSignals:");
3216 p
= packet
+ strlen (packet
);
3217 for (size_t i
= 0; i
< signals
.size (); i
++)
3219 if (signal_pass_state (i
))
3222 *p
++ = tohex (i
>> 4);
3223 *p
++ = tohex (i
& 15);
3232 if (!rs
->last_program_signals_packet
3233 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
3237 m_features
.packet_ok (rs
->buf
, PACKET_QProgramSignals
);
3238 xfree (rs
->last_program_signals_packet
);
3239 rs
->last_program_signals_packet
= packet
;
3246 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
3247 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
3248 thread. If GEN is set, set the general thread, if not, then set
3249 the step/continue thread. */
3251 remote_target::set_thread (ptid_t ptid
, int gen
)
3253 struct remote_state
*rs
= get_remote_state ();
3254 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
3255 char *buf
= rs
->buf
.data ();
3256 char *endbuf
= buf
+ get_remote_packet_size ();
3262 *buf
++ = gen
? 'g' : 'c';
3263 if (ptid
== magic_null_ptid
)
3264 xsnprintf (buf
, endbuf
- buf
, "0");
3265 else if (ptid
== any_thread_ptid
)
3266 xsnprintf (buf
, endbuf
- buf
, "0");
3267 else if (ptid
== minus_one_ptid
)
3268 xsnprintf (buf
, endbuf
- buf
, "-1");
3270 write_ptid (buf
, endbuf
, ptid
);
3274 rs
->general_thread
= ptid
;
3276 rs
->continue_thread
= ptid
;
3280 remote_target::set_general_thread (ptid_t ptid
)
3282 set_thread (ptid
, 1);
3286 remote_target::set_continue_thread (ptid_t ptid
)
3288 set_thread (ptid
, 0);
3291 /* Change the remote current process. Which thread within the process
3292 ends up selected isn't important, as long as it is the same process
3293 as what INFERIOR_PTID points to.
3295 This comes from that fact that there is no explicit notion of
3296 "selected process" in the protocol. The selected process for
3297 general operations is the process the selected general thread
3301 remote_target::set_general_process ()
3303 /* If the remote can't handle multiple processes, don't bother. */
3304 if (!m_features
.remote_multi_process_p ())
3307 remote_state
*rs
= get_remote_state ();
3309 /* We only need to change the remote current thread if it's pointing
3310 at some other process. */
3311 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
3312 set_general_thread (inferior_ptid
);
3316 /* Return nonzero if this is the main thread that we made up ourselves
3317 to model non-threaded targets as single-threaded. */
3320 remote_thread_always_alive (ptid_t ptid
)
3322 if (ptid
== magic_null_ptid
)
3323 /* The main thread is always alive. */
3326 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
3327 /* The main thread is always alive. This can happen after a
3328 vAttach, if the remote side doesn't support
3335 /* Return nonzero if the thread PTID is still alive on the remote
3339 remote_target::thread_alive (ptid_t ptid
)
3341 struct remote_state
*rs
= get_remote_state ();
3344 /* Check if this is a thread that we made up ourselves to model
3345 non-threaded targets as single-threaded. */
3346 if (remote_thread_always_alive (ptid
))
3349 p
= rs
->buf
.data ();
3350 endp
= p
+ get_remote_packet_size ();
3353 write_ptid (p
, endp
, ptid
);
3357 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
3360 /* Return a pointer to a thread name if we know it and NULL otherwise.
3361 The thread_info object owns the memory for the name. */
3364 remote_target::thread_name (struct thread_info
*info
)
3366 if (info
->priv
!= NULL
)
3368 const std::string
&name
= get_remote_thread_info (info
)->name
;
3369 return !name
.empty () ? name
.c_str () : NULL
;
3375 /* About these extended threadlist and threadinfo packets. They are
3376 variable length packets but, the fields within them are often fixed
3377 length. They are redundant enough to send over UDP as is the
3378 remote protocol in general. There is a matching unit test module
3381 /* WARNING: This threadref data structure comes from the remote O.S.,
3382 libstub protocol encoding, and remote.c. It is not particularly
3385 /* Right now, the internal structure is int. We want it to be bigger.
3386 Plan to fix this. */
3388 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3390 /* gdb_ext_thread_info is an internal GDB data structure which is
3391 equivalent to the reply of the remote threadinfo packet. */
3393 struct gdb_ext_thread_info
3395 threadref threadid
; /* External form of thread reference. */
3396 int active
; /* Has state interesting to GDB?
3398 char display
[256]; /* Brief state display, name,
3399 blocked/suspended. */
3400 char shortname
[32]; /* To be used to name threads. */
3401 char more_display
[256]; /* Long info, statistics, queue depth,
3405 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3407 static const char *unpack_nibble (const char *buf
, int *val
);
3409 static const char *unpack_byte (const char *buf
, int *value
);
3411 static char *pack_int (char *buf
, int value
);
3413 static const char *unpack_int (const char *buf
, int *value
);
3415 static const char *unpack_string (const char *src
, char *dest
, int length
);
3417 static char *pack_threadid (char *pkt
, threadref
*id
);
3419 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3421 void int_to_threadref (threadref
*id
, int value
);
3423 static int threadref_to_int (threadref
*ref
);
3425 static void copy_threadref (threadref
*dest
, threadref
*src
);
3427 static int threadmatch (threadref
*dest
, threadref
*src
);
3429 static char *pack_threadinfo_request (char *pkt
, int mode
,
3432 static char *pack_threadlist_request (char *pkt
, int startflag
,
3434 threadref
*nextthread
);
3436 static int remote_newthread_step (threadref
*ref
, void *context
);
3439 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3440 buffer we're allowed to write to. Returns
3441 BUF+CHARACTERS_WRITTEN. */
3444 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3448 if (m_features
.remote_multi_process_p ())
3452 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3454 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3458 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3460 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3465 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3466 last parsed char. Returns null_ptid if no thread id is found, and
3467 throws an error if the thread id has an invalid format. */
3470 read_ptid (const char *buf
, const char **obuf
)
3472 const char *p
= buf
;
3474 ULONGEST pid
= 0, tid
= 0;
3478 /* Multi-process ptid. */
3479 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3481 error (_("invalid remote ptid: %s"), p
);
3484 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3487 return ptid_t (pid
, tid
);
3490 /* No multi-process. Just a tid. */
3491 pp
= unpack_varlen_hex (p
, &tid
);
3493 /* Return null_ptid when no thread id is found. */
3501 /* Since the stub is not sending a process id, default to what's
3502 current_inferior, unless it doesn't have a PID yet. If so,
3503 then since there's no way to know the pid of the reported
3504 threads, use the magic number. */
3505 inferior
*inf
= current_inferior ();
3507 pid
= magic_null_ptid
.pid ();
3513 return ptid_t (pid
, tid
);
3519 if (ch
>= 'a' && ch
<= 'f')
3520 return ch
- 'a' + 10;
3521 if (ch
>= '0' && ch
<= '9')
3523 if (ch
>= 'A' && ch
<= 'F')
3524 return ch
- 'A' + 10;
3529 stub_unpack_int (const char *buff
, int fieldlength
)
3536 nibble
= stubhex (*buff
++);
3540 retval
= retval
<< 4;
3546 unpack_nibble (const char *buf
, int *val
)
3548 *val
= fromhex (*buf
++);
3553 unpack_byte (const char *buf
, int *value
)
3555 *value
= stub_unpack_int (buf
, 2);
3560 pack_int (char *buf
, int value
)
3562 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3563 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3564 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3565 buf
= pack_hex_byte (buf
, (value
& 0xff));
3570 unpack_int (const char *buf
, int *value
)
3572 *value
= stub_unpack_int (buf
, 8);
3576 #if 0 /* Currently unused, uncomment when needed. */
3577 static char *pack_string (char *pkt
, char *string
);
3580 pack_string (char *pkt
, char *string
)
3585 len
= strlen (string
);
3587 len
= 200; /* Bigger than most GDB packets, junk??? */
3588 pkt
= pack_hex_byte (pkt
, len
);
3592 if ((ch
== '\0') || (ch
== '#'))
3593 ch
= '*'; /* Protect encapsulation. */
3598 #endif /* 0 (unused) */
3601 unpack_string (const char *src
, char *dest
, int length
)
3610 pack_threadid (char *pkt
, threadref
*id
)
3613 unsigned char *altid
;
3615 altid
= (unsigned char *) id
;
3616 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3618 pkt
= pack_hex_byte (pkt
, *altid
++);
3624 unpack_threadid (const char *inbuf
, threadref
*id
)
3627 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3630 altref
= (char *) id
;
3632 while (inbuf
< limit
)
3634 x
= stubhex (*inbuf
++);
3635 y
= stubhex (*inbuf
++);
3636 *altref
++ = (x
<< 4) | y
;
3641 /* Externally, threadrefs are 64 bits but internally, they are still
3642 ints. This is due to a mismatch of specifications. We would like
3643 to use 64bit thread references internally. This is an adapter
3647 int_to_threadref (threadref
*id
, int value
)
3649 unsigned char *scan
;
3651 scan
= (unsigned char *) id
;
3657 *scan
++ = (value
>> 24) & 0xff;
3658 *scan
++ = (value
>> 16) & 0xff;
3659 *scan
++ = (value
>> 8) & 0xff;
3660 *scan
++ = (value
& 0xff);
3664 threadref_to_int (threadref
*ref
)
3667 unsigned char *scan
;
3673 value
= (value
<< 8) | ((*scan
++) & 0xff);
3678 copy_threadref (threadref
*dest
, threadref
*src
)
3681 unsigned char *csrc
, *cdest
;
3683 csrc
= (unsigned char *) src
;
3684 cdest
= (unsigned char *) dest
;
3691 threadmatch (threadref
*dest
, threadref
*src
)
3693 /* Things are broken right now, so just assume we got a match. */
3695 unsigned char *srcp
, *destp
;
3697 srcp
= (char *) src
;
3698 destp
= (char *) dest
;
3702 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3709 threadid:1, # always request threadid
3716 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3719 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3721 *pkt
++ = 'q'; /* Info Query */
3722 *pkt
++ = 'P'; /* process or thread info */
3723 pkt
= pack_int (pkt
, mode
); /* mode */
3724 pkt
= pack_threadid (pkt
, id
); /* threadid */
3725 *pkt
= '\0'; /* terminate */
3729 /* These values tag the fields in a thread info response packet. */
3730 /* Tagging the fields allows us to request specific fields and to
3731 add more fields as time goes by. */
3733 #define TAG_THREADID 1 /* Echo the thread identifier. */
3734 #define TAG_EXISTS 2 /* Is this process defined enough to
3735 fetch registers and its stack? */
3736 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3737 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3738 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3742 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3743 threadref
*expectedref
,
3744 gdb_ext_thread_info
*info
)
3746 struct remote_state
*rs
= get_remote_state ();
3750 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3753 /* info->threadid = 0; FIXME: implement zero_threadref. */
3755 info
->display
[0] = '\0';
3756 info
->shortname
[0] = '\0';
3757 info
->more_display
[0] = '\0';
3759 /* Assume the characters indicating the packet type have been
3761 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3762 pkt
= unpack_threadid (pkt
, &ref
);
3765 warning (_("Incomplete response to threadinfo request."));
3766 if (!threadmatch (&ref
, expectedref
))
3767 { /* This is an answer to a different request. */
3768 warning (_("ERROR RMT Thread info mismatch."));
3771 copy_threadref (&info
->threadid
, &ref
);
3773 /* Loop on tagged fields , try to bail if something goes wrong. */
3775 /* Packets are terminated with nulls. */
3776 while ((pkt
< limit
) && mask
&& *pkt
)
3778 pkt
= unpack_int (pkt
, &tag
); /* tag */
3779 pkt
= unpack_byte (pkt
, &length
); /* length */
3780 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3782 warning (_("ERROR RMT: threadinfo tag mismatch."));
3786 if (tag
== TAG_THREADID
)
3790 warning (_("ERROR RMT: length of threadid is not 16."));
3794 pkt
= unpack_threadid (pkt
, &ref
);
3795 mask
= mask
& ~TAG_THREADID
;
3798 if (tag
== TAG_EXISTS
)
3800 info
->active
= stub_unpack_int (pkt
, length
);
3802 mask
= mask
& ~(TAG_EXISTS
);
3805 warning (_("ERROR RMT: 'exists' length too long."));
3811 if (tag
== TAG_THREADNAME
)
3813 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3814 mask
= mask
& ~TAG_THREADNAME
;
3817 if (tag
== TAG_DISPLAY
)
3819 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3820 mask
= mask
& ~TAG_DISPLAY
;
3823 if (tag
== TAG_MOREDISPLAY
)
3825 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3826 mask
= mask
& ~TAG_MOREDISPLAY
;
3829 warning (_("ERROR RMT: unknown thread info tag."));
3830 break; /* Not a tag we know about. */
3836 remote_target::remote_get_threadinfo (threadref
*threadid
,
3838 gdb_ext_thread_info
*info
)
3840 struct remote_state
*rs
= get_remote_state ();
3843 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3847 if (rs
->buf
[0] == '\0')
3850 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3855 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3858 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3859 threadref
*nextthread
)
3861 *pkt
++ = 'q'; /* info query packet */
3862 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3863 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3864 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3865 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3870 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3873 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3874 threadref
*original_echo
,
3875 threadref
*resultlist
,
3878 struct remote_state
*rs
= get_remote_state ();
3879 int count
, resultcount
, done
;
3882 /* Assume the 'q' and 'M chars have been stripped. */
3883 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3884 /* done parse past here */
3885 pkt
= unpack_byte (pkt
, &count
); /* count field */
3886 pkt
= unpack_nibble (pkt
, &done
);
3887 /* The first threadid is the argument threadid. */
3888 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3889 while ((count
-- > 0) && (pkt
< limit
))
3891 pkt
= unpack_threadid (pkt
, resultlist
++);
3892 if (resultcount
++ >= result_limit
)
3900 /* Fetch the next batch of threads from the remote. Returns -1 if the
3901 qL packet is not supported, 0 on error and 1 on success. */
3904 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3905 int result_limit
, int *done
, int *result_count
,
3906 threadref
*threadlist
)
3908 struct remote_state
*rs
= get_remote_state ();
3911 /* Truncate result limit to be smaller than the packet size. */
3912 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3913 >= get_remote_packet_size ())
3914 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3916 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3920 if (rs
->buf
[0] == '\0')
3922 /* Packet not supported. */
3927 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3928 &rs
->echo_nextthread
, threadlist
, done
);
3930 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3932 /* FIXME: This is a good reason to drop the packet. */
3933 /* Possibly, there is a duplicate response. */
3935 retransmit immediately - race conditions
3936 retransmit after timeout - yes
3938 wait for packet, then exit
3940 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3941 return 0; /* I choose simply exiting. */
3943 if (*result_count
<= 0)
3947 warning (_("RMT ERROR : failed to get remote thread list."));
3950 return result
; /* break; */
3952 if (*result_count
> result_limit
)
3955 warning (_("RMT ERROR: threadlist response longer than requested."));
3961 /* Fetch the list of remote threads, with the qL packet, and call
3962 STEPFUNCTION for each thread found. Stops iterating and returns 1
3963 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3964 STEPFUNCTION returns false. If the packet is not supported,
3968 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3969 void *context
, int looplimit
)
3971 struct remote_state
*rs
= get_remote_state ();
3972 int done
, i
, result_count
;
3980 if (loopcount
++ > looplimit
)
3983 warning (_("Remote fetch threadlist -infinite loop-."));
3986 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3987 MAXTHREADLISTRESULTS
,
3988 &done
, &result_count
,
3989 rs
->resultthreadlist
);
3992 /* Clear for later iterations. */
3994 /* Setup to resume next batch of thread references, set nextthread. */
3995 if (result_count
>= 1)
3996 copy_threadref (&rs
->nextthread
,
3997 &rs
->resultthreadlist
[result_count
- 1]);
3999 while (result_count
--)
4001 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
4011 /* A thread found on the remote target. */
4015 explicit thread_item (ptid_t ptid_
)
4019 thread_item (thread_item
&&other
) = default;
4020 thread_item
&operator= (thread_item
&&other
) = default;
4022 DISABLE_COPY_AND_ASSIGN (thread_item
);
4024 /* The thread's PTID. */
4027 /* The thread's extra info. */
4030 /* The thread's name. */
4033 /* The core the thread was running on. -1 if not known. */
4036 /* The thread handle associated with the thread. */
4037 gdb::byte_vector thread_handle
;
4040 /* Context passed around to the various methods listing remote
4041 threads. As new threads are found, they're added to the ITEMS
4044 struct threads_listing_context
4046 /* Return true if this object contains an entry for a thread with ptid
4049 bool contains_thread (ptid_t ptid
) const
4051 auto match_ptid
= [&] (const thread_item
&item
)
4053 return item
.ptid
== ptid
;
4056 auto it
= std::find_if (this->items
.begin (),
4060 return it
!= this->items
.end ();
4063 /* Remove the thread with ptid PTID. */
4065 void remove_thread (ptid_t ptid
)
4067 auto match_ptid
= [&] (const thread_item
&item
)
4069 return item
.ptid
== ptid
;
4072 auto it
= std::remove_if (this->items
.begin (),
4076 if (it
!= this->items
.end ())
4077 this->items
.erase (it
);
4080 /* The threads found on the remote target. */
4081 std::vector
<thread_item
> items
;
4085 remote_newthread_step (threadref
*ref
, void *data
)
4087 struct threads_listing_context
*context
4088 = (struct threads_listing_context
*) data
;
4089 int pid
= inferior_ptid
.pid ();
4090 int lwp
= threadref_to_int (ref
);
4091 ptid_t
ptid (pid
, lwp
);
4093 context
->items
.emplace_back (ptid
);
4095 return 1; /* continue iterator */
4098 #define CRAZY_MAX_THREADS 1000
4101 remote_target::remote_current_thread (ptid_t oldpid
)
4103 struct remote_state
*rs
= get_remote_state ();
4107 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
4112 result
= read_ptid (&rs
->buf
[2], &obuf
);
4114 remote_debug_printf ("warning: garbage in qC reply");
4122 /* List remote threads using the deprecated qL packet. */
4125 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
4127 if (remote_threadlist_iterator (remote_newthread_step
, context
,
4128 CRAZY_MAX_THREADS
) >= 0)
4134 #if defined(HAVE_LIBEXPAT)
4137 start_thread (struct gdb_xml_parser
*parser
,
4138 const struct gdb_xml_element
*element
,
4140 std::vector
<gdb_xml_value
> &attributes
)
4142 struct threads_listing_context
*data
4143 = (struct threads_listing_context
*) user_data
;
4144 struct gdb_xml_value
*attr
;
4146 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
4147 ptid_t ptid
= read_ptid (id
, NULL
);
4149 thread_item
&item
= data
->items
.emplace_back (ptid
);
4151 attr
= xml_find_attribute (attributes
, "core");
4153 item
.core
= *(ULONGEST
*) attr
->value
.get ();
4155 attr
= xml_find_attribute (attributes
, "name");
4157 item
.name
= (const char *) attr
->value
.get ();
4159 attr
= xml_find_attribute (attributes
, "handle");
4161 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
4165 end_thread (struct gdb_xml_parser
*parser
,
4166 const struct gdb_xml_element
*element
,
4167 void *user_data
, const char *body_text
)
4169 struct threads_listing_context
*data
4170 = (struct threads_listing_context
*) user_data
;
4172 if (body_text
!= NULL
&& *body_text
!= '\0')
4173 data
->items
.back ().extra
= body_text
;
4176 const struct gdb_xml_attribute thread_attributes
[] = {
4177 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
4178 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
4179 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4180 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4181 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
4184 const struct gdb_xml_element thread_children
[] = {
4185 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4188 const struct gdb_xml_element threads_children
[] = {
4189 { "thread", thread_attributes
, thread_children
,
4190 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
4191 start_thread
, end_thread
},
4192 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4195 const struct gdb_xml_element threads_elements
[] = {
4196 { "threads", NULL
, threads_children
,
4197 GDB_XML_EF_NONE
, NULL
, NULL
},
4198 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4203 /* List remote threads using qXfer:threads:read. */
4206 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
4208 #if defined(HAVE_LIBEXPAT)
4209 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4211 std::optional
<gdb::char_vector
> xml
4212 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
4214 if (xml
&& (*xml
)[0] != '\0')
4216 gdb_xml_parse_quick (_("threads"), "threads.dtd",
4217 threads_elements
, xml
->data (), context
);
4227 /* List remote threads using qfThreadInfo/qsThreadInfo. */
4230 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
4232 struct remote_state
*rs
= get_remote_state ();
4234 if (rs
->use_threadinfo_query
)
4238 putpkt ("qfThreadInfo");
4240 bufp
= rs
->buf
.data ();
4241 if (bufp
[0] != '\0') /* q packet recognized */
4243 while (*bufp
++ == 'm') /* reply contains one or more TID */
4247 ptid_t ptid
= read_ptid (bufp
, &bufp
);
4248 context
->items
.emplace_back (ptid
);
4250 while (*bufp
++ == ','); /* comma-separated list */
4251 putpkt ("qsThreadInfo");
4253 bufp
= rs
->buf
.data ();
4259 /* Packet not recognized. */
4260 rs
->use_threadinfo_query
= 0;
4267 /* Return true if INF only has one non-exited thread. */
4270 has_single_non_exited_thread (inferior
*inf
)
4273 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
4279 /* Implement the to_update_thread_list function for the remote
4283 remote_target::update_thread_list ()
4285 struct threads_listing_context context
;
4288 /* We have a few different mechanisms to fetch the thread list. Try
4289 them all, starting with the most preferred one first, falling
4290 back to older methods. */
4291 if (remote_get_threads_with_qxfer (&context
)
4292 || remote_get_threads_with_qthreadinfo (&context
)
4293 || remote_get_threads_with_ql (&context
))
4297 if (context
.items
.empty ()
4298 && remote_thread_always_alive (inferior_ptid
))
4300 /* Some targets don't really support threads, but still
4301 reply an (empty) thread list in response to the thread
4302 listing packets, instead of replying "packet not
4303 supported". Exit early so we don't delete the main
4308 /* CONTEXT now holds the current thread list on the remote
4309 target end. Delete GDB-side threads no longer found on the
4311 for (thread_info
*tp
: all_threads_safe ())
4313 if (tp
->inf
->process_target () != this)
4316 if (!context
.contains_thread (tp
->ptid
))
4318 /* Do not remove the thread if it is the last thread in
4319 the inferior. This situation happens when we have a
4320 pending exit process status to process. Otherwise we
4321 may end up with a seemingly live inferior (i.e. pid
4322 != 0) that has no threads. */
4323 if (has_single_non_exited_thread (tp
->inf
))
4326 /* Do not remove the thread if we've requested to be
4327 notified of its exit. For example, the thread may be
4328 displaced stepping, infrun will need to handle the
4329 exit event, and displaced stepping info is recorded
4330 in the thread object. If we deleted the thread now,
4331 we'd lose that info. */
4332 if ((tp
->thread_options () & GDB_THREAD_OPTION_EXIT
) != 0)
4340 /* Remove any unreported fork/vfork/clone child threads from
4341 CONTEXT so that we don't interfere with follow
4342 fork/vfork/clone, which is where creation of such threads is
4344 remove_new_children (&context
);
4346 /* And now add threads we don't know about yet to our list. */
4347 for (thread_item
&item
: context
.items
)
4349 if (item
.ptid
!= null_ptid
)
4351 /* In non-stop mode, we assume new found threads are
4352 executing until proven otherwise with a stop reply.
4353 In all-stop, we can only get here if all threads are
4355 bool executing
= target_is_non_stop_p ();
4357 remote_notice_new_inferior (item
.ptid
, executing
);
4359 thread_info
*tp
= this->find_thread (item
.ptid
);
4360 remote_thread_info
*info
= get_remote_thread_info (tp
);
4361 info
->core
= item
.core
;
4362 info
->extra
= std::move (item
.extra
);
4363 info
->name
= std::move (item
.name
);
4364 info
->thread_handle
= std::move (item
.thread_handle
);
4371 /* If no thread listing method is supported, then query whether
4372 each known thread is alive, one by one, with the T packet.
4373 If the target doesn't support threads at all, then this is a
4374 no-op. See remote_thread_alive. */
4380 * Collect a descriptive string about the given thread.
4381 * The target may say anything it wants to about the thread
4382 * (typically info about its blocked / runnable state, name, etc.).
4383 * This string will appear in the info threads display.
4385 * Optional: targets are not required to implement this function.
4389 remote_target::extra_thread_info (thread_info
*tp
)
4391 struct remote_state
*rs
= get_remote_state ();
4394 struct gdb_ext_thread_info threadinfo
;
4396 if (rs
->remote_desc
== 0) /* paranoia */
4397 internal_error (_("remote_threads_extra_info"));
4399 if (tp
->ptid
== magic_null_ptid
4400 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4401 /* This is the main thread which was added by GDB. The remote
4402 server doesn't know about it. */
4405 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4407 /* If already have cached info, use it. */
4408 if (!extra
.empty ())
4409 return extra
.c_str ();
4411 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4413 /* If we're using qXfer:threads:read, then the extra info is
4414 included in the XML. So if we didn't have anything cached,
4415 it's because there's really no extra info. */
4419 if (rs
->use_threadextra_query
)
4421 char *b
= rs
->buf
.data ();
4422 char *endb
= b
+ get_remote_packet_size ();
4424 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4426 write_ptid (b
, endb
, tp
->ptid
);
4430 if (rs
->buf
[0] != 0)
4432 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4433 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4434 return extra
.c_str ();
4438 /* If the above query fails, fall back to the old method. */
4439 rs
->use_threadextra_query
= 0;
4440 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4441 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4442 int_to_threadref (&id
, tp
->ptid
.lwp ());
4443 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4444 if (threadinfo
.active
)
4446 if (*threadinfo
.shortname
)
4447 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4448 if (*threadinfo
.display
)
4450 if (!extra
.empty ())
4452 string_appendf (extra
, " State: %s", threadinfo
.display
);
4454 if (*threadinfo
.more_display
)
4456 if (!extra
.empty ())
4458 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4460 return extra
.c_str ();
4467 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4468 struct static_tracepoint_marker
*marker
)
4470 struct remote_state
*rs
= get_remote_state ();
4471 char *p
= rs
->buf
.data ();
4473 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4475 p
+= hexnumstr (p
, addr
);
4478 p
= rs
->buf
.data ();
4481 error (_("Remote failure reply: %s"), p
);
4485 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4492 std::vector
<static_tracepoint_marker
>
4493 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4495 struct remote_state
*rs
= get_remote_state ();
4496 std::vector
<static_tracepoint_marker
> markers
;
4498 static_tracepoint_marker marker
;
4500 /* Ask for a first packet of static tracepoint marker
4504 p
= rs
->buf
.data ();
4506 error (_("Remote failure reply: %s"), p
);
4512 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4514 if (strid
== NULL
|| marker
.str_id
== strid
)
4515 markers
.push_back (std::move (marker
));
4517 while (*p
++ == ','); /* comma-separated list */
4518 /* Ask for another packet of static tracepoint definition. */
4521 p
= rs
->buf
.data ();
4528 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4531 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4533 return ptid_t (inferior_ptid
.pid (), lwp
);
4537 /* Restart the remote side; this is an extended protocol operation. */
4540 remote_target::extended_remote_restart ()
4542 struct remote_state
*rs
= get_remote_state ();
4544 /* Send the restart command; for reasons I don't understand the
4545 remote side really expects a number after the "R". */
4546 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4549 remote_fileio_reset ();
4552 /* Clean up connection to a remote debugger. */
4555 remote_target::close ()
4557 /* Make sure we leave stdin registered in the event loop. */
4560 trace_reset_local_state ();
4565 remote_target::~remote_target ()
4567 struct remote_state
*rs
= get_remote_state ();
4569 /* Check for NULL because we may get here with a partially
4570 constructed target/connection. */
4571 if (rs
->remote_desc
== nullptr)
4574 serial_close (rs
->remote_desc
);
4576 /* We are destroying the remote target, so we should discard
4577 everything of this target. */
4578 discard_pending_stop_replies_in_queue ();
4580 rs
->delete_async_event_handler ();
4582 delete rs
->notif_state
;
4585 /* Query the remote side for the text, data and bss offsets. */
4588 remote_target::get_offsets ()
4590 struct remote_state
*rs
= get_remote_state ();
4593 int lose
, num_segments
= 0, do_sections
, do_segments
;
4594 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4596 if (current_program_space
->symfile_object_file
== NULL
)
4599 putpkt ("qOffsets");
4601 buf
= rs
->buf
.data ();
4603 if (buf
[0] == '\000')
4604 return; /* Return silently. Stub doesn't support
4608 warning (_("Remote failure reply: %s"), buf
);
4612 /* Pick up each field in turn. This used to be done with scanf, but
4613 scanf will make trouble if CORE_ADDR size doesn't match
4614 conversion directives correctly. The following code will work
4615 with any size of CORE_ADDR. */
4616 text_addr
= data_addr
= bss_addr
= 0;
4620 if (startswith (ptr
, "Text="))
4623 /* Don't use strtol, could lose on big values. */
4624 while (*ptr
&& *ptr
!= ';')
4625 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4627 if (startswith (ptr
, ";Data="))
4630 while (*ptr
&& *ptr
!= ';')
4631 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4636 if (!lose
&& startswith (ptr
, ";Bss="))
4639 while (*ptr
&& *ptr
!= ';')
4640 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4642 if (bss_addr
!= data_addr
)
4643 warning (_("Target reported unsupported offsets: %s"), buf
);
4648 else if (startswith (ptr
, "TextSeg="))
4651 /* Don't use strtol, could lose on big values. */
4652 while (*ptr
&& *ptr
!= ';')
4653 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4656 if (startswith (ptr
, ";DataSeg="))
4659 while (*ptr
&& *ptr
!= ';')
4660 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4668 error (_("Malformed response to offset query, %s"), buf
);
4669 else if (*ptr
!= '\0')
4670 warning (_("Target reported unsupported offsets: %s"), buf
);
4672 objfile
*objf
= current_program_space
->symfile_object_file
;
4673 section_offsets offs
= objf
->section_offsets
;
4675 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
.get ());
4676 do_segments
= (data
!= NULL
);
4677 do_sections
= num_segments
== 0;
4679 if (num_segments
> 0)
4681 segments
[0] = text_addr
;
4682 segments
[1] = data_addr
;
4684 /* If we have two segments, we can still try to relocate everything
4685 by assuming that the .text and .data offsets apply to the whole
4686 text and data segments. Convert the offsets given in the packet
4687 to base addresses for symfile_map_offsets_to_segments. */
4688 else if (data
!= nullptr && data
->segments
.size () == 2)
4690 segments
[0] = data
->segments
[0].base
+ text_addr
;
4691 segments
[1] = data
->segments
[1].base
+ data_addr
;
4694 /* If the object file has only one segment, assume that it is text
4695 rather than data; main programs with no writable data are rare,
4696 but programs with no code are useless. Of course the code might
4697 have ended up in the data segment... to detect that we would need
4698 the permissions here. */
4699 else if (data
&& data
->segments
.size () == 1)
4701 segments
[0] = data
->segments
[0].base
+ text_addr
;
4704 /* There's no way to relocate by segment. */
4710 int ret
= symfile_map_offsets_to_segments (objf
->obfd
.get (),
4712 num_segments
, segments
);
4714 if (ret
== 0 && !do_sections
)
4715 error (_("Can not handle qOffsets TextSeg "
4716 "response with this symbol file"));
4724 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4726 /* This is a temporary kludge to force data and bss to use the
4727 same offsets because that's what nlmconv does now. The real
4728 solution requires changes to the stub and remote.c that I
4729 don't have time to do right now. */
4731 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4732 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4735 objfile_relocate (objf
, offs
);
4738 /* Send interrupt_sequence to remote target. */
4741 remote_target::send_interrupt_sequence ()
4743 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4744 remote_serial_write ("\x03", 1);
4745 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4746 remote_serial_send_break ();
4747 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4749 remote_serial_send_break ();
4750 remote_serial_write ("g", 1);
4753 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
4754 interrupt_sequence_mode
);
4757 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4758 and extract the PTID. Returns NULL_PTID if not found. */
4761 stop_reply_extract_thread (const char *stop_reply
)
4763 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4767 /* Txx r:val ; r:val (...) */
4770 /* Look for "register" named "thread". */
4775 p1
= strchr (p
, ':');
4779 if (strncmp (p
, "thread", p1
- p
) == 0)
4780 return read_ptid (++p1
, &p
);
4782 p1
= strchr (p
, ';');
4794 /* Determine the remote side's current thread. If we have a stop
4795 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4796 "thread" register we can extract the current thread from. If not,
4797 ask the remote which is the current thread with qC. The former
4798 method avoids a roundtrip. */
4801 remote_target::get_current_thread (const char *wait_status
)
4803 ptid_t ptid
= null_ptid
;
4805 /* Note we don't use remote_parse_stop_reply as that makes use of
4806 the target architecture, which we haven't yet fully determined at
4808 if (wait_status
!= NULL
)
4809 ptid
= stop_reply_extract_thread (wait_status
);
4810 if (ptid
== null_ptid
)
4811 ptid
= remote_current_thread (inferior_ptid
);
4816 /* Query the remote target for which is the current thread/process,
4817 add it to our tables, and update INFERIOR_PTID. The caller is
4818 responsible for setting the state such that the remote end is ready
4819 to return the current thread.
4821 This function is called after handling the '?' or 'vRun' packets,
4822 whose response is a stop reply from which we can also try
4823 extracting the thread. If the target doesn't support the explicit
4824 qC query, we infer the current thread from that stop reply, passed
4825 in in WAIT_STATUS, which may be NULL.
4827 The function returns pointer to the main thread of the inferior. */
4830 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4832 bool fake_pid_p
= false;
4834 switch_to_no_thread ();
4836 /* Now, if we have thread information, update the current thread's
4838 ptid_t curr_ptid
= get_current_thread (wait_status
);
4840 if (curr_ptid
!= null_ptid
)
4842 if (!m_features
.remote_multi_process_p ())
4847 /* Without this, some commands which require an active target
4848 (such as kill) won't work. This variable serves (at least)
4849 double duty as both the pid of the target process (if it has
4850 such), and as a flag indicating that a target is active. */
4851 curr_ptid
= magic_null_ptid
;
4855 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4857 /* Add the main thread and switch to it. Don't try reading
4858 registers yet, since we haven't fetched the target description
4860 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4861 switch_to_thread_no_regs (tp
);
4866 /* Print info about a thread that was found already stopped on
4870 remote_target::print_one_stopped_thread (thread_info
*thread
)
4872 target_waitstatus ws
;
4874 /* If there is a pending waitstatus, use it. If there isn't it's because
4875 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4876 and process_initial_stop_replies decided it wasn't interesting to save
4877 and report to the core. */
4878 if (thread
->has_pending_waitstatus ())
4880 ws
= thread
->pending_waitstatus ();
4881 thread
->clear_pending_waitstatus ();
4885 ws
.set_stopped (GDB_SIGNAL_0
);
4888 switch_to_thread (thread
);
4889 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4890 set_current_sal_from_frame (get_current_frame ());
4892 /* For "info program". */
4893 set_last_target_status (this, thread
->ptid
, ws
);
4895 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4897 enum gdb_signal sig
= ws
.sig ();
4899 if (signal_print_state (sig
))
4900 notify_signal_received (sig
);
4903 notify_normal_stop (nullptr, 1);
4906 /* Process all initial stop replies the remote side sent in response
4907 to the ? packet. These indicate threads that were already stopped
4908 on initial connection. We mark these threads as stopped and print
4909 their current frame before giving the user the prompt. */
4912 remote_target::process_initial_stop_replies (int from_tty
)
4914 int pending_stop_replies
= stop_reply_queue_length ();
4915 struct thread_info
*selected
= NULL
;
4916 struct thread_info
*lowest_stopped
= NULL
;
4917 struct thread_info
*first
= NULL
;
4919 /* This is only used when the target is non-stop. */
4920 gdb_assert (target_is_non_stop_p ());
4922 /* Consume the initial pending events. */
4923 while (pending_stop_replies
-- > 0)
4925 ptid_t waiton_ptid
= minus_one_ptid
;
4927 struct target_waitstatus ws
;
4928 int ignore_event
= 0;
4930 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4932 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4936 case TARGET_WAITKIND_IGNORE
:
4937 case TARGET_WAITKIND_NO_RESUMED
:
4938 case TARGET_WAITKIND_SIGNALLED
:
4939 case TARGET_WAITKIND_EXITED
:
4940 /* We shouldn't see these, but if we do, just ignore. */
4941 remote_debug_printf ("event ignored");
4952 thread_info
*evthread
= this->find_thread (event_ptid
);
4954 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4956 enum gdb_signal sig
= ws
.sig ();
4958 /* Stubs traditionally report SIGTRAP as initial signal,
4959 instead of signal 0. Suppress it. */
4960 if (sig
== GDB_SIGNAL_TRAP
)
4962 evthread
->set_stop_signal (sig
);
4963 ws
.set_stopped (sig
);
4966 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4967 || ws
.sig () != GDB_SIGNAL_0
)
4968 evthread
->set_pending_waitstatus (ws
);
4970 set_executing (this, event_ptid
, false);
4971 set_running (this, event_ptid
, false);
4972 get_remote_thread_info (evthread
)->set_not_resumed ();
4975 /* "Notice" the new inferiors before anything related to
4976 registers/memory. */
4977 for (inferior
*inf
: all_non_exited_inferiors (this))
4979 inf
->needs_setup
= true;
4983 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4984 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4989 /* If all-stop on top of non-stop, pause all threads. Note this
4990 records the threads' stop pc, so must be done after "noticing"
4995 /* At this point, the remote target is not async. It needs to be for
4996 the poll in stop_all_threads to consider events from it, so enable
4998 gdb_assert (!this->is_async_p ());
4999 SCOPE_EXIT
{ target_async (false); };
5000 target_async (true);
5001 stop_all_threads ("remote connect in all-stop");
5004 /* If all threads of an inferior were already stopped, we
5005 haven't setup the inferior yet. */
5006 for (inferior
*inf
: all_non_exited_inferiors (this))
5008 if (inf
->needs_setup
)
5010 thread_info
*thread
= any_live_thread_of_inferior (inf
);
5011 switch_to_thread_no_regs (thread
);
5017 /* Now go over all threads that are stopped, and print their current
5018 frame. If all-stop, then if there's a signalled thread, pick
5020 for (thread_info
*thread
: all_non_exited_threads (this))
5026 thread
->set_running (false);
5027 else if (thread
->state
!= THREAD_STOPPED
)
5030 if (selected
== nullptr && thread
->has_pending_waitstatus ())
5033 if (lowest_stopped
== NULL
5034 || thread
->inf
->num
< lowest_stopped
->inf
->num
5035 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
5036 lowest_stopped
= thread
;
5039 print_one_stopped_thread (thread
);
5042 /* In all-stop, we only print the status of one thread, and leave
5043 others with their status pending. */
5046 thread_info
*thread
= selected
;
5048 thread
= lowest_stopped
;
5052 print_one_stopped_thread (thread
);
5056 /* Mark a remote_target as starting (by setting the starting_up flag within
5057 its remote_state) for the lifetime of this object. The reference count
5058 on the remote target is temporarily incremented, to prevent the target
5059 being deleted under our feet. */
5061 struct scoped_mark_target_starting
5063 /* Constructor, TARGET is the target to be marked as starting, its
5064 reference count will be incremented. */
5065 scoped_mark_target_starting (remote_target
*target
)
5066 : m_remote_target (remote_target_ref::new_reference (target
)),
5067 m_restore_starting_up (set_starting_up_flag (target
))
5072 /* Helper function, set the starting_up flag on TARGET and return an
5073 object which, when it goes out of scope, will restore the previous
5074 value of the starting_up flag. */
5075 static scoped_restore_tmpl
<bool>
5076 set_starting_up_flag (remote_target
*target
)
5078 remote_state
*rs
= target
->get_remote_state ();
5079 gdb_assert (!rs
->starting_up
);
5080 return make_scoped_restore (&rs
->starting_up
, true);
5083 /* A gdb::ref_ptr pointer to a remote_target. */
5084 using remote_target_ref
= gdb::ref_ptr
<remote_target
, target_ops_ref_policy
>;
5086 /* A reference to the target on which we are operating. */
5087 remote_target_ref m_remote_target
;
5089 /* An object which restores the previous value of the starting_up flag
5090 when it goes out of scope. */
5091 scoped_restore_tmpl
<bool> m_restore_starting_up
;
5094 /* Transfer ownership of the stop_reply owned by EVENT to a
5095 stop_reply_up object. */
5097 static stop_reply_up
5098 as_stop_reply_up (notif_event_up event
)
5100 auto *stop_reply
= static_cast<struct stop_reply
*> (event
.release ());
5101 return stop_reply_up (stop_reply
);
5104 /* Helper for remote_target::start_remote, start the remote connection and
5105 sync state. Return true if everything goes OK, otherwise, return false.
5106 This function exists so that the scoped_restore created within it will
5107 expire before we return to remote_target::start_remote. */
5110 remote_target::start_remote_1 (int from_tty
, int extended_p
)
5112 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
5114 struct remote_state
*rs
= get_remote_state ();
5116 /* Signal other parts that we're going through the initial setup,
5117 and so things may not be stable yet. E.g., we don't try to
5118 install tracepoints until we've relocated symbols. Also, a
5119 Ctrl-C before we're connected and synced up can't interrupt the
5120 target. Instead, it offers to drop the (potentially wedged)
5122 scoped_mark_target_starting
target_is_starting (this);
5126 if (interrupt_on_connect
)
5127 send_interrupt_sequence ();
5129 /* Ack any packet which the remote side has already sent. */
5130 remote_serial_write ("+", 1);
5132 /* The first packet we send to the target is the optional "supported
5133 packets" request. If the target can answer this, it will tell us
5134 which later probes to skip. */
5135 remote_query_supported ();
5137 /* Check vCont support and set the remote state's vCont_action_support
5139 remote_vcont_probe ();
5141 /* If the stub wants to get a QAllow, compose one and send it. */
5142 if (m_features
.packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
5145 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
5146 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
5147 as a reply to known packet. For packet "vFile:setfs:" it is an
5148 invalid reply and GDB would return error in
5149 remote_hostio_set_filesystem, making remote files access impossible.
5150 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
5151 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
5153 const char v_mustreplyempty
[] = "vMustReplyEmpty";
5155 putpkt (v_mustreplyempty
);
5157 if (strcmp (rs
->buf
.data (), "OK") == 0)
5159 m_features
.m_protocol_packets
[PACKET_vFile_setfs
].support
5162 else if (strcmp (rs
->buf
.data (), "") != 0)
5163 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
5167 /* Next, we possibly activate noack mode.
5169 If the QStartNoAckMode packet configuration is set to AUTO,
5170 enable noack mode if the stub reported a wish for it with
5173 If set to TRUE, then enable noack mode even if the stub didn't
5174 report it in qSupported. If the stub doesn't reply OK, the
5175 session ends with an error.
5177 If FALSE, then don't activate noack mode, regardless of what the
5178 stub claimed should be the default with qSupported. */
5180 if (m_features
.packet_support (PACKET_QStartNoAckMode
) != PACKET_DISABLE
)
5182 putpkt ("QStartNoAckMode");
5184 if ((m_features
.packet_ok (rs
->buf
, PACKET_QStartNoAckMode
)).status ()
5191 /* Tell the remote that we are using the extended protocol. */
5196 /* Let the target know which signals it is allowed to pass down to
5198 update_signals_program_target ();
5200 /* Next, if the target can specify a description, read it. We do
5201 this before anything involving memory or registers. */
5202 target_find_description ();
5204 /* Next, now that we know something about the target, update the
5205 address spaces in the program spaces. */
5206 update_address_spaces ();
5208 /* On OSs where the list of libraries is global to all
5209 processes, we fetch them early. */
5210 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
5211 solib_add (NULL
, from_tty
, auto_solib_add
);
5213 if (target_is_non_stop_p ())
5215 if (m_features
.packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
5216 error (_("Non-stop mode requested, but remote "
5217 "does not support non-stop"));
5219 putpkt ("QNonStop:1");
5222 if (strcmp (rs
->buf
.data (), "OK") != 0)
5223 error (_("Remote refused setting non-stop mode with: %s"),
5226 /* Find about threads and processes the stub is already
5227 controlling. We default to adding them in the running state.
5228 The '?' query below will then tell us about which threads are
5230 this->update_thread_list ();
5232 else if (m_features
.packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
5234 /* Don't assume that the stub can operate in all-stop mode.
5235 Request it explicitly. */
5236 putpkt ("QNonStop:0");
5239 if (strcmp (rs
->buf
.data (), "OK") != 0)
5240 error (_("Remote refused setting all-stop mode with: %s"),
5244 /* Upload TSVs regardless of whether the target is running or not. The
5245 remote stub, such as GDBserver, may have some predefined or builtin
5246 TSVs, even if the target is not running. */
5247 if (get_trace_status (current_trace_status ()) != -1)
5249 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
5251 upload_trace_state_variables (&uploaded_tsvs
);
5252 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
5255 /* Check whether the target is running now. */
5259 if (!target_is_non_stop_p ())
5261 char *wait_status
= NULL
;
5263 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
5266 error (_("The target is not running (try extended-remote?)"));
5271 /* Save the reply for later. */
5272 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
5273 strcpy (wait_status
, rs
->buf
.data ());
5276 /* Fetch thread list. */
5277 target_update_thread_list ();
5279 /* Let the stub know that we want it to return the thread. */
5280 set_continue_thread (minus_one_ptid
);
5282 if (thread_count (this) == 0)
5284 /* Target has no concept of threads at all. GDB treats
5285 non-threaded target as single-threaded; add a main
5287 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
5288 get_remote_thread_info (tp
)->set_resumed ();
5292 /* We have thread information; select the thread the target
5293 says should be current. If we're reconnecting to a
5294 multi-threaded program, this will ideally be the thread
5295 that last reported an event before GDB disconnected. */
5296 ptid_t curr_thread
= get_current_thread (wait_status
);
5297 if (curr_thread
== null_ptid
)
5299 /* Odd... The target was able to list threads, but not
5300 tell us which thread was current (no "thread"
5301 register in T stop reply?). Just pick the first
5302 thread in the thread list then. */
5304 remote_debug_printf ("warning: couldn't determine remote "
5305 "current thread; picking first in list.");
5307 for (thread_info
*tp
: all_non_exited_threads (this,
5310 switch_to_thread (tp
);
5315 switch_to_thread (this->find_thread (curr_thread
));
5317 get_remote_thread_info (inferior_thread ())->set_resumed ();
5320 /* init_wait_for_inferior should be called before get_offsets in order
5321 to manage `inserted' flag in bp loc in a correct state.
5322 breakpoint_init_inferior, called from init_wait_for_inferior, set
5323 `inserted' flag to 0, while before breakpoint_re_set, called from
5324 start_remote, set `inserted' flag to 1. In the initialization of
5325 inferior, breakpoint_init_inferior should be called first, and then
5326 breakpoint_re_set can be called. If this order is broken, state of
5327 `inserted' flag is wrong, and cause some problems on breakpoint
5329 init_wait_for_inferior ();
5331 get_offsets (); /* Get text, data & bss offsets. */
5333 /* If we could not find a description using qXfer, and we know
5334 how to do it some other way, try again. This is not
5335 supported for non-stop; it could be, but it is tricky if
5336 there are no stopped threads when we connect. */
5337 if (remote_read_description_p (this)
5338 && gdbarch_target_desc (current_inferior ()->arch ()) == NULL
)
5340 target_clear_description ();
5341 target_find_description ();
5344 /* Use the previously fetched status. */
5345 gdb_assert (wait_status
!= NULL
);
5346 notif_event_up reply
5347 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
5348 push_stop_reply (as_stop_reply_up (std::move (reply
)));
5350 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
5354 /* Clear WFI global state. Do this before finding about new
5355 threads and inferiors, and setting the current inferior.
5356 Otherwise we would clear the proceed status of the current
5357 inferior when we want its stop_soon state to be preserved
5358 (see notice_new_inferior). */
5359 init_wait_for_inferior ();
5361 /* In non-stop, we will either get an "OK", meaning that there
5362 are no stopped threads at this time; or, a regular stop
5363 reply. In the latter case, there may be more than one thread
5364 stopped --- we pull them all out using the vStopped
5366 if (strcmp (rs
->buf
.data (), "OK") != 0)
5368 const notif_client
*notif
= ¬if_client_stop
;
5370 /* remote_notif_get_pending_replies acks this one, and gets
5372 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
5373 = remote_notif_parse (this, notif
, rs
->buf
.data ());
5374 remote_notif_get_pending_events (notif
);
5377 if (thread_count (this) == 0)
5380 error (_("The target is not running (try extended-remote?)"));
5384 /* Report all signals during attach/startup. */
5387 /* If there are already stopped threads, mark them stopped and
5388 report their stops before giving the prompt to the user. */
5389 process_initial_stop_replies (from_tty
);
5391 if (target_can_async_p ())
5392 target_async (true);
5395 /* Give the target a chance to look up symbols. */
5396 for (inferior
*inf
: all_inferiors (this))
5398 /* The inferiors that exist at this point were created from what
5399 was found already running on the remote side, so we know they
5401 gdb_assert (this->has_execution (inf
));
5403 /* No use without a symbol-file. */
5404 if (inf
->pspace
->symfile_object_file
== nullptr)
5407 /* Need to switch to a specific thread, because remote_check_symbols
5408 uses INFERIOR_PTID to set the general thread. */
5409 scoped_restore_current_thread restore_thread
;
5410 thread_info
*thread
= any_thread_of_inferior (inf
);
5411 switch_to_thread (thread
);
5412 this->remote_check_symbols ();
5415 /* Possibly the target has been engaged in a trace run started
5416 previously; find out where things are at. */
5417 if (get_trace_status (current_trace_status ()) != -1)
5419 struct uploaded_tp
*uploaded_tps
= NULL
;
5421 if (current_trace_status ()->running
)
5422 gdb_printf (_("Trace is already running on the target.\n"));
5424 upload_tracepoints (&uploaded_tps
);
5426 merge_uploaded_tracepoints (&uploaded_tps
);
5429 /* Possibly the target has been engaged in a btrace record started
5430 previously; find out where things are at. */
5431 remote_btrace_maybe_reopen ();
5436 /* Start the remote connection and sync state. */
5439 remote_target::start_remote (int from_tty
, int extended_p
)
5441 if (start_remote_1 (from_tty
, extended_p
)
5442 && breakpoints_should_be_inserted_now ())
5443 insert_breakpoints ();
5447 remote_target::connection_string ()
5449 remote_state
*rs
= get_remote_state ();
5451 if (rs
->remote_desc
->name
!= NULL
)
5452 return rs
->remote_desc
->name
;
5457 /* Open a connection to a remote debugger.
5458 NAME is the filename used for communication. */
5461 remote_target::open (const char *name
, int from_tty
)
5463 open_1 (name
, from_tty
, 0);
5466 /* Open a connection to a remote debugger using the extended
5467 remote gdb protocol. NAME is the filename used for communication. */
5470 extended_remote_target::open (const char *name
, int from_tty
)
5472 open_1 (name
, from_tty
, 1 /*extended_p */);
5476 remote_features::reset_all_packet_configs_support ()
5480 for (i
= 0; i
< PACKET_MAX
; i
++)
5481 m_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5484 /* Initialize all packet configs. */
5487 init_all_packet_configs (void)
5491 for (i
= 0; i
< PACKET_MAX
; i
++)
5493 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5494 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5498 /* Symbol look-up. */
5501 remote_target::remote_check_symbols ()
5506 /* It doesn't make sense to send a qSymbol packet for an inferior that
5507 doesn't have execution, because the remote side doesn't know about
5508 inferiors without execution. */
5509 gdb_assert (target_has_execution ());
5511 if (m_features
.packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5514 /* Make sure the remote is pointing at the right process. Note
5515 there's no way to select "no process". */
5516 set_general_process ();
5518 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5519 because we need both at the same time. */
5520 gdb::char_vector
msg (get_remote_packet_size ());
5521 gdb::char_vector
reply (get_remote_packet_size ());
5523 /* Invite target to request symbol lookups. */
5525 putpkt ("qSymbol::");
5527 m_features
.packet_ok (reply
, PACKET_qSymbol
);
5529 while (startswith (reply
.data (), "qSymbol:"))
5532 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5535 bound_minimal_symbol sym
5536 = lookup_minimal_symbol (current_program_space
, msg
.data ());
5537 if (sym
.minsym
== NULL
)
5538 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5542 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
5543 CORE_ADDR sym_addr
= sym
.value_address ();
5545 /* If this is a function address, return the start of code
5546 instead of any data function descriptor. */
5547 sym_addr
= gdbarch_convert_from_func_ptr_addr
5548 (current_inferior ()->arch (), sym_addr
,
5549 current_inferior ()->top_target ());
5551 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5552 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5555 putpkt (msg
.data ());
5560 static struct serial
*
5561 remote_serial_open (const char *name
)
5563 static int udp_warning
= 0;
5565 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5566 of in ser-tcp.c, because it is the remote protocol assuming that the
5567 serial connection is reliable and not the serial connection promising
5569 if (!udp_warning
&& startswith (name
, "udp:"))
5571 warning (_("The remote protocol may be unreliable over UDP.\n"
5572 "Some events may be lost, rendering further debugging "
5577 return serial_open (name
);
5580 /* Inform the target of our permission settings. The permission flags
5581 work without this, but if the target knows the settings, it can do
5582 a couple things. First, it can add its own check, to catch cases
5583 that somehow manage to get by the permissions checks in target
5584 methods. Second, if the target is wired to disallow particular
5585 settings (for instance, a system in the field that is not set up to
5586 be able to stop at a breakpoint), it can object to any unavailable
5590 remote_target::set_permissions ()
5592 struct remote_state
*rs
= get_remote_state ();
5594 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5595 "WriteReg:%x;WriteMem:%x;"
5596 "InsertBreak:%x;InsertTrace:%x;"
5597 "InsertFastTrace:%x;Stop:%x",
5598 may_write_registers
, may_write_memory
,
5599 may_insert_breakpoints
, may_insert_tracepoints
,
5600 may_insert_fast_tracepoints
, may_stop
);
5604 /* If the target didn't like the packet, warn the user. Do not try
5605 to undo the user's settings, that would just be maddening. */
5606 if (strcmp (rs
->buf
.data (), "OK") != 0)
5607 warning (_("Remote refused setting permissions with: %s"),
5611 /* This type describes each known response to the qSupported
5613 struct protocol_feature
5615 /* The name of this protocol feature. */
5618 /* The default for this protocol feature. */
5619 enum packet_support default_support
;
5621 /* The function to call when this feature is reported, or after
5622 qSupported processing if the feature is not supported.
5623 The first argument points to this structure. The second
5624 argument indicates whether the packet requested support be
5625 enabled, disabled, or probed (or the default, if this function
5626 is being called at the end of processing and this feature was
5627 not reported). The third argument may be NULL; if not NULL, it
5628 is a NUL-terminated string taken from the packet following
5629 this feature's name and an equals sign. */
5630 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5631 enum packet_support
, const char *);
5633 /* The corresponding packet for this feature. Only used if
5634 FUNC is remote_supported_packet. */
5639 remote_supported_packet (remote_target
*remote
,
5640 const struct protocol_feature
*feature
,
5641 enum packet_support support
,
5642 const char *argument
)
5646 warning (_("Remote qSupported response supplied an unexpected value for"
5647 " \"%s\"."), feature
->name
);
5651 remote
->m_features
.m_protocol_packets
[feature
->packet
].support
= support
;
5655 remote_target::remote_packet_size (const protocol_feature
*feature
,
5656 enum packet_support support
,
5659 struct remote_state
*rs
= get_remote_state ();
5664 if (support
!= PACKET_ENABLE
)
5667 if (value
== NULL
|| *value
== '\0')
5669 warning (_("Remote target reported \"%s\" without a size."),
5675 packet_size
= strtol (value
, &value_end
, 16);
5676 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5678 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5679 feature
->name
, value
);
5683 /* Record the new maximum packet size. */
5684 rs
->explicit_packet_size
= packet_size
;
5688 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5689 enum packet_support support
, const char *value
)
5691 remote
->remote_packet_size (feature
, support
, value
);
5695 remote_target::remote_supported_thread_options (const protocol_feature
*feature
,
5696 enum packet_support support
,
5699 struct remote_state
*rs
= get_remote_state ();
5701 m_features
.m_protocol_packets
[feature
->packet
].support
= support
;
5703 if (support
!= PACKET_ENABLE
)
5706 if (value
== nullptr || *value
== '\0')
5708 warning (_("Remote target reported \"%s\" without supported options."),
5713 ULONGEST options
= 0;
5714 const char *p
= unpack_varlen_hex (value
, &options
);
5718 warning (_("Remote target reported \"%s\" with "
5719 "bad thread options: \"%s\"."),
5720 feature
->name
, value
);
5724 /* Record the set of supported options. */
5725 rs
->supported_thread_options
= (gdb_thread_option
) options
;
5729 remote_supported_thread_options (remote_target
*remote
,
5730 const protocol_feature
*feature
,
5731 enum packet_support support
,
5734 remote
->remote_supported_thread_options (feature
, support
, value
);
5737 static const struct protocol_feature remote_protocol_features
[] = {
5738 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5739 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5740 PACKET_qXfer_auxv
},
5741 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5742 PACKET_qXfer_exec_file
},
5743 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5744 PACKET_qXfer_features
},
5745 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5746 PACKET_qXfer_libraries
},
5747 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5748 PACKET_qXfer_libraries_svr4
},
5749 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5750 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5751 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5752 PACKET_qXfer_memory_map
},
5753 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5754 PACKET_qXfer_osdata
},
5755 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5756 PACKET_qXfer_threads
},
5757 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5758 PACKET_qXfer_traceframe_info
},
5759 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5760 PACKET_QPassSignals
},
5761 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5762 PACKET_QCatchSyscalls
},
5763 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5764 PACKET_QProgramSignals
},
5765 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5766 PACKET_QSetWorkingDir
},
5767 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5768 PACKET_QStartupWithShell
},
5769 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5770 PACKET_QEnvironmentHexEncoded
},
5771 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5772 PACKET_QEnvironmentReset
},
5773 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5774 PACKET_QEnvironmentUnset
},
5775 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5776 PACKET_QStartNoAckMode
},
5777 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5778 PACKET_multiprocess_feature
},
5779 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5780 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5781 PACKET_qXfer_siginfo_read
},
5782 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5783 PACKET_qXfer_siginfo_write
},
5784 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5785 PACKET_ConditionalTracepoints
},
5786 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5787 PACKET_ConditionalBreakpoints
},
5788 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5789 PACKET_BreakpointCommands
},
5790 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5791 PACKET_FastTracepoints
},
5792 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5793 PACKET_StaticTracepoints
},
5794 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5795 PACKET_InstallInTrace
},
5796 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5797 PACKET_DisconnectedTracing_feature
},
5798 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5800 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5802 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5803 PACKET_TracepointSource
},
5804 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5806 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5807 PACKET_EnableDisableTracepoints_feature
},
5808 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5809 PACKET_qXfer_fdpic
},
5810 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5812 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5813 PACKET_QDisableRandomization
},
5814 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5815 { "QTBuffer:size", PACKET_DISABLE
,
5816 remote_supported_packet
, PACKET_QTBuffer_size
},
5817 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5818 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5819 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5820 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5821 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5822 PACKET_qXfer_btrace
},
5823 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5824 PACKET_qXfer_btrace_conf
},
5825 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5826 PACKET_Qbtrace_conf_bts_size
},
5827 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5828 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5829 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5830 PACKET_fork_event_feature
},
5831 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5832 PACKET_vfork_event_feature
},
5833 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5834 PACKET_exec_event_feature
},
5835 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5836 PACKET_Qbtrace_conf_pt_size
},
5837 { "Qbtrace-conf:pt:ptwrite", PACKET_DISABLE
, remote_supported_packet
,
5838 PACKET_Qbtrace_conf_pt_ptwrite
},
5839 { "Qbtrace-conf:pt:event-tracing", PACKET_DISABLE
, remote_supported_packet
,
5840 PACKET_Qbtrace_conf_pt_event_tracing
},
5841 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5842 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5843 { "QThreadOptions", PACKET_DISABLE
, remote_supported_thread_options
,
5844 PACKET_QThreadOptions
},
5845 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5846 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5847 PACKET_memory_tagging_feature
},
5848 { "error-message", PACKET_ENABLE
, remote_supported_packet
,
5849 PACKET_accept_error_message
},
5850 { "binary-upload", PACKET_DISABLE
, remote_supported_packet
, PACKET_x
},
5853 static char *remote_support_xml
;
5855 /* Register string appended to "xmlRegisters=" in qSupported query. */
5858 register_remote_support_xml (const char *xml
)
5860 #if defined(HAVE_LIBEXPAT)
5861 if (remote_support_xml
== NULL
)
5862 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5865 char *copy
= xstrdup (remote_support_xml
+ 13);
5867 char *p
= strtok_r (copy
, ",", &saveptr
);
5871 if (strcmp (p
, xml
) == 0)
5878 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5881 remote_support_xml
= reconcat (remote_support_xml
,
5882 remote_support_xml
, ",", xml
,
5889 remote_query_supported_append (std::string
*msg
, const char *append
)
5893 msg
->append (append
);
5897 remote_target::remote_query_supported ()
5899 struct remote_state
*rs
= get_remote_state ();
5902 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5904 /* The packet support flags are handled differently for this packet
5905 than for most others. We treat an error, a disabled packet, and
5906 an empty response identically: any features which must be reported
5907 to be used will be automatically disabled. An empty buffer
5908 accomplishes this, since that is also the representation for a list
5909 containing no features. */
5912 if (m_features
.packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5916 if (m_features
.packet_set_cmd_state (PACKET_multiprocess_feature
)
5917 != AUTO_BOOLEAN_FALSE
)
5918 remote_query_supported_append (&q
, "multiprocess+");
5920 if (m_features
.packet_set_cmd_state (PACKET_swbreak_feature
)
5921 != AUTO_BOOLEAN_FALSE
)
5922 remote_query_supported_append (&q
, "swbreak+");
5924 if (m_features
.packet_set_cmd_state (PACKET_hwbreak_feature
)
5925 != AUTO_BOOLEAN_FALSE
)
5926 remote_query_supported_append (&q
, "hwbreak+");
5928 remote_query_supported_append (&q
, "qRelocInsn+");
5930 if (m_features
.packet_set_cmd_state (PACKET_fork_event_feature
)
5931 != AUTO_BOOLEAN_FALSE
)
5932 remote_query_supported_append (&q
, "fork-events+");
5934 if (m_features
.packet_set_cmd_state (PACKET_vfork_event_feature
)
5935 != AUTO_BOOLEAN_FALSE
)
5936 remote_query_supported_append (&q
, "vfork-events+");
5938 if (m_features
.packet_set_cmd_state (PACKET_exec_event_feature
)
5939 != AUTO_BOOLEAN_FALSE
)
5940 remote_query_supported_append (&q
, "exec-events+");
5942 if (m_features
.packet_set_cmd_state (PACKET_vContSupported
)
5943 != AUTO_BOOLEAN_FALSE
)
5944 remote_query_supported_append (&q
, "vContSupported+");
5946 if (m_features
.packet_set_cmd_state (PACKET_QThreadEvents
)
5947 != AUTO_BOOLEAN_FALSE
)
5948 remote_query_supported_append (&q
, "QThreadEvents+");
5950 if (m_features
.packet_set_cmd_state (PACKET_QThreadOptions
)
5951 != AUTO_BOOLEAN_FALSE
)
5952 remote_query_supported_append (&q
, "QThreadOptions+");
5954 if (m_features
.packet_set_cmd_state (PACKET_no_resumed
)
5955 != AUTO_BOOLEAN_FALSE
)
5956 remote_query_supported_append (&q
, "no-resumed+");
5958 if (m_features
.packet_set_cmd_state (PACKET_memory_tagging_feature
)
5959 != AUTO_BOOLEAN_FALSE
)
5960 remote_query_supported_append (&q
, "memory-tagging+");
5962 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5963 the qSupported:xmlRegisters=i386 handling. */
5964 if (remote_support_xml
!= NULL
5965 && (m_features
.packet_support (PACKET_qXfer_features
)
5967 remote_query_supported_append (&q
, remote_support_xml
);
5969 if (m_features
.packet_set_cmd_state (PACKET_accept_error_message
)
5970 != AUTO_BOOLEAN_FALSE
)
5971 remote_query_supported_append (&q
, "error-message+");
5973 q
= "qSupported:" + q
;
5974 putpkt (q
.c_str ());
5978 /* If an error occurred, warn, but do not return - just reset the
5979 buffer to empty and go on to disable features. */
5980 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_qSupported
);
5981 if (result
.status () == PACKET_ERROR
)
5983 warning (_("Remote failure reply: %s"), result
.err_msg ());
5988 memset (seen
, 0, sizeof (seen
));
5990 next
= rs
->buf
.data ();
5993 enum packet_support is_supported
;
5994 char *p
, *end
, *name_end
, *value
;
5996 /* First separate out this item from the rest of the packet. If
5997 there's another item after this, we overwrite the separator
5998 (terminated strings are much easier to work with). */
6000 end
= strchr (p
, ';');
6003 end
= p
+ strlen (p
);
6013 warning (_("empty item in \"qSupported\" response"));
6018 name_end
= strchr (p
, '=');
6021 /* This is a name=value entry. */
6022 is_supported
= PACKET_ENABLE
;
6023 value
= name_end
+ 1;
6032 is_supported
= PACKET_ENABLE
;
6036 is_supported
= PACKET_DISABLE
;
6040 is_supported
= PACKET_SUPPORT_UNKNOWN
;
6044 warning (_("unrecognized item \"%s\" "
6045 "in \"qSupported\" response"), p
);
6051 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
6052 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
6054 const struct protocol_feature
*feature
;
6057 feature
= &remote_protocol_features
[i
];
6058 feature
->func (this, feature
, is_supported
, value
);
6063 /* If we increased the packet size, make sure to increase the global
6064 buffer size also. We delay this until after parsing the entire
6065 qSupported packet, because this is the same buffer we were
6067 if (rs
->buf
.size () < rs
->explicit_packet_size
)
6068 rs
->buf
.resize (rs
->explicit_packet_size
);
6070 /* Handle the defaults for unmentioned features. */
6071 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
6074 const struct protocol_feature
*feature
;
6076 feature
= &remote_protocol_features
[i
];
6077 feature
->func (this, feature
, feature
->default_support
, NULL
);
6081 /* Serial QUIT handler for the remote serial descriptor.
6083 Defers handling a Ctrl-C until we're done with the current
6084 command/response packet sequence, unless:
6086 - We're setting up the connection. Don't send a remote interrupt
6087 request, as we're not fully synced yet. Quit immediately
6090 - The target has been resumed in the foreground
6091 (target_terminal::is_ours is false) with a synchronous resume
6092 packet, and we're blocked waiting for the stop reply, thus a
6093 Ctrl-C should be immediately sent to the target.
6095 - We get a second Ctrl-C while still within the same serial read or
6096 write. In that case the serial is seemingly wedged --- offer to
6099 - We see a second Ctrl-C without target response, after having
6100 previously interrupted the target. In that case the target/stub
6101 is probably wedged --- offer to quit/disconnect.
6105 remote_target::remote_serial_quit_handler ()
6107 struct remote_state
*rs
= get_remote_state ();
6109 if (check_quit_flag ())
6111 /* If we're starting up, we're not fully synced yet. Quit
6113 if (rs
->starting_up
)
6115 else if (rs
->got_ctrlc_during_io
)
6117 if (query (_("The target is not responding to GDB commands.\n"
6118 "Stop debugging it? ")))
6119 remote_unpush_and_throw (this);
6121 /* If ^C has already been sent once, offer to disconnect. */
6122 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
6124 /* All-stop protocol, and blocked waiting for stop reply. Send
6125 an interrupt request. */
6126 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
6127 target_interrupt ();
6129 rs
->got_ctrlc_during_io
= 1;
6133 /* The remote_target that is current while the quit handler is
6134 overridden with remote_serial_quit_handler. */
6135 static remote_target
*curr_quit_handler_target
;
6138 remote_serial_quit_handler ()
6140 curr_quit_handler_target
->remote_serial_quit_handler ();
6143 /* Remove the remote target from the target stack of each inferior
6144 that is using it. Upper targets depend on it so remove them
6148 remote_unpush_target (remote_target
*target
)
6150 /* We have to unpush the target from all inferiors, even those that
6152 scoped_restore_current_inferior restore_current_inferior
;
6154 for (inferior
*inf
: all_inferiors (target
))
6156 switch_to_inferior_no_thread (inf
);
6157 inf
->pop_all_targets_at_and_above (process_stratum
);
6158 generic_mourn_inferior ();
6161 /* Don't rely on target_close doing this when the target is popped
6162 from the last remote inferior above, because something may be
6163 holding a reference to the target higher up on the stack, meaning
6164 target_close won't be called yet. We lost the connection to the
6165 target, so clear these now, otherwise we may later throw
6166 TARGET_CLOSE_ERROR while trying to tell the remote target to
6168 fileio_handles_invalidate_target (target
);
6171 [[noreturn
]] static void
6172 remote_unpush_and_throw (remote_target
*target
)
6174 remote_unpush_target (target
);
6175 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
6179 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
6181 remote_target
*curr_remote
= get_current_remote_target ();
6184 error (_("To open a remote debug connection, you need to specify what\n"
6185 "serial device is attached to the remote system\n"
6186 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
6188 /* If we're connected to a running target, target_preopen will kill it.
6189 Ask this question first, before target_preopen has a chance to kill
6191 if (curr_remote
!= NULL
&& !target_has_execution ())
6194 && !query (_("Already connected to a remote target. Disconnect? ")))
6195 error (_("Still connected."));
6198 /* Here the possibly existing remote target gets unpushed. */
6199 target_preopen (from_tty
);
6201 remote_fileio_reset ();
6202 reopen_exec_file ();
6203 reread_symbols (from_tty
);
6205 remote_target
*remote
6206 = (extended_p
? new extended_remote_target () : new remote_target ());
6207 target_ops_up
target_holder (remote
);
6209 remote_state
*rs
= remote
->get_remote_state ();
6211 /* See FIXME above. */
6212 if (!target_async_permitted
)
6213 rs
->wait_forever_enabled_p
= true;
6215 rs
->remote_desc
= remote_serial_open (name
);
6217 if (baud_rate
!= -1)
6221 serial_setbaudrate (rs
->remote_desc
, baud_rate
);
6223 catch (const gdb_exception_error
&)
6225 /* The requested speed could not be set. Error out to
6226 top level after closing remote_desc. Take care to
6227 set remote_desc to NULL to avoid closing remote_desc
6229 serial_close (rs
->remote_desc
);
6230 rs
->remote_desc
= NULL
;
6235 serial_setparity (rs
->remote_desc
, serial_parity
);
6236 serial_raw (rs
->remote_desc
);
6238 /* If there is something sitting in the buffer we might take it as a
6239 response to a command, which would be bad. */
6240 serial_flush_input (rs
->remote_desc
);
6244 gdb_puts ("Remote debugging using ");
6249 /* Switch to using the remote target now. */
6250 current_inferior ()->push_target (std::move (target_holder
));
6252 /* Register extra event sources in the event loop. */
6253 rs
->create_async_event_handler ();
6255 rs
->notif_state
= remote_notif_state_allocate (remote
);
6257 /* Reset the target state; these things will be queried either by
6258 remote_query_supported or as they are needed. */
6259 remote
->m_features
.reset_all_packet_configs_support ();
6260 rs
->explicit_packet_size
= 0;
6262 rs
->extended
= extended_p
;
6263 rs
->waiting_for_stop_reply
= 0;
6264 rs
->ctrlc_pending_p
= 0;
6265 rs
->got_ctrlc_during_io
= 0;
6267 rs
->general_thread
= not_sent_ptid
;
6268 rs
->continue_thread
= not_sent_ptid
;
6269 rs
->remote_traceframe_number
= -1;
6271 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
6273 /* Probe for ability to use "ThreadInfo" query, as required. */
6274 rs
->use_threadinfo_query
= 1;
6275 rs
->use_threadextra_query
= 1;
6277 rs
->readahead_cache
.invalidate ();
6279 if (target_async_permitted
)
6281 /* FIXME: cagney/1999-09-23: During the initial connection it is
6282 assumed that the target is already ready and able to respond to
6283 requests. Unfortunately remote_start_remote() eventually calls
6284 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
6285 around this. Eventually a mechanism that allows
6286 wait_for_inferior() to expect/get timeouts will be
6288 rs
->wait_forever_enabled_p
= false;
6291 /* First delete any symbols previously loaded from shared libraries. */
6292 no_shared_libraries (current_program_space
);
6294 /* Start the remote connection. If error() or QUIT, discard this
6295 target (we'd otherwise be in an inconsistent state) and then
6296 propagate the error on up the exception chain. This ensures that
6297 the caller doesn't stumble along blindly assuming that the
6298 function succeeded. The CLI doesn't have this problem but other
6299 UI's, such as MI do.
6301 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
6302 this function should return an error indication letting the
6303 caller restore the previous state. Unfortunately the command
6304 ``target remote'' is directly wired to this function making that
6305 impossible. On a positive note, the CLI side of this problem has
6306 been fixed - the function set_cmd_context() makes it possible for
6307 all the ``target ....'' commands to share a common callback
6308 function. See cli-dump.c. */
6313 remote
->start_remote (from_tty
, extended_p
);
6315 catch (const gdb_exception
&ex
)
6317 /* Pop the partially set up target - unless something else did
6318 already before throwing the exception. */
6319 if (ex
.error
!= TARGET_CLOSE_ERROR
)
6320 remote_unpush_target (remote
);
6325 remote_btrace_reset (rs
);
6327 if (target_async_permitted
)
6328 rs
->wait_forever_enabled_p
= true;
6331 /* Determine if WS represents a fork status. */
6334 is_fork_status (target_waitkind kind
)
6336 return (kind
== TARGET_WAITKIND_FORKED
6337 || kind
== TARGET_WAITKIND_VFORKED
);
6340 /* Return a reference to the field where a pending child status, if
6341 there's one, is recorded. If there's no child event pending, the
6342 returned waitstatus has TARGET_WAITKIND_IGNORE kind. */
6344 static const target_waitstatus
&
6345 thread_pending_status (struct thread_info
*thread
)
6347 return (thread
->has_pending_waitstatus ()
6348 ? thread
->pending_waitstatus ()
6349 : thread
->pending_follow
);
6352 /* Return THREAD's pending status if it is a pending fork/vfork (but
6353 not clone) parent, else return nullptr. */
6355 static const target_waitstatus
*
6356 thread_pending_fork_status (struct thread_info
*thread
)
6358 const target_waitstatus
&ws
= thread_pending_status (thread
);
6360 if (!is_fork_status (ws
.kind ()))
6366 /* Return THREAD's pending status if is is a pending fork/vfork/clone
6367 event, else return nullptr. */
6369 static const target_waitstatus
*
6370 thread_pending_child_status (thread_info
*thread
)
6372 const target_waitstatus
&ws
= thread_pending_status (thread
);
6374 if (!is_new_child_status (ws
.kind ()))
6380 /* Detach the specified process. */
6383 remote_target::remote_detach_pid (int pid
)
6385 struct remote_state
*rs
= get_remote_state ();
6387 /* This should not be necessary, but the handling for D;PID in
6388 GDBserver versions prior to 8.2 incorrectly assumes that the
6389 selected process points to the same process we're detaching,
6390 leading to misbehavior (and possibly GDBserver crashing) when it
6391 does not. Since it's easy and cheap, work around it by forcing
6392 GDBserver to select GDB's current process. */
6393 set_general_process ();
6395 if (m_features
.remote_multi_process_p ())
6396 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
6398 strcpy (rs
->buf
.data (), "D");
6403 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
6405 else if (rs
->buf
[0] == '\0')
6406 error (_("Remote doesn't know how to detach"));
6409 /* It is possible that we have an unprocessed exit event for this
6410 pid. If this is the case then we can ignore the failure to detach
6411 and just pretend that the detach worked, as far as the user is
6412 concerned, the process exited immediately after the detach. */
6413 bool process_has_already_exited
= false;
6414 remote_notif_get_pending_events (¬if_client_stop
);
6415 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6417 if (reply
->ptid
.pid () != pid
)
6420 enum target_waitkind kind
= reply
->ws
.kind ();
6421 if (kind
== TARGET_WAITKIND_EXITED
6422 || kind
== TARGET_WAITKIND_SIGNALLED
)
6424 process_has_already_exited
= true;
6426 ("detach failed, but process already exited");
6431 if (!process_has_already_exited
)
6432 error (_("can't detach process: %s"), (char *) rs
->buf
.data ());
6436 /* This detaches a program to which we previously attached, using
6437 inferior_ptid to identify the process. After this is done, GDB
6438 can be used to debug some other program. We better not have left
6439 any breakpoints in the target program or it'll die when it hits
6443 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
6445 int pid
= inferior_ptid
.pid ();
6446 struct remote_state
*rs
= get_remote_state ();
6449 if (!target_has_execution ())
6450 error (_("No process to detach from."));
6452 target_announce_detach (from_tty
);
6454 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
6456 /* If we're in breakpoints-always-inserted mode, or the inferior
6457 is running, we have to remove breakpoints before detaching.
6458 We don't do this in common code instead because not all
6459 targets support removing breakpoints while the target is
6460 running. The remote target / gdbserver does, though. */
6461 remove_breakpoints_inf (current_inferior ());
6464 /* Tell the remote target to detach. */
6465 remote_detach_pid (pid
);
6467 /* Exit only if this is the only active inferior. */
6468 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
6469 gdb_puts (_("Ending remote debugging.\n"));
6471 /* See if any thread of the inferior we are detaching has a pending fork
6472 status. In that case, we must detach from the child resulting from
6474 for (thread_info
*thread
: inf
->non_exited_threads ())
6476 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
6481 remote_detach_pid (ws
->child_ptid ().pid ());
6484 /* Check also for any pending fork events in the stop reply queue. */
6485 remote_notif_get_pending_events (¬if_client_stop
);
6486 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6488 if (reply
->ptid
.pid () != pid
)
6491 if (!is_fork_status (reply
->ws
.kind ()))
6494 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
6497 thread_info
*tp
= this->find_thread (inferior_ptid
);
6499 /* Check to see if we are detaching a fork parent. Note that if we
6500 are detaching a fork child, tp == NULL. */
6501 is_fork_parent
= (tp
!= NULL
6502 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
6504 /* If doing detach-on-fork, we don't mourn, because that will delete
6505 breakpoints that should be available for the followed inferior. */
6506 if (!is_fork_parent
)
6508 /* Save the pid as a string before mourning, since that will
6509 unpush the remote target, and we need the string after. */
6510 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6512 target_mourn_inferior (inferior_ptid
);
6513 if (print_inferior_events
)
6514 gdb_printf (_("[Inferior %d (%s) detached]\n"),
6515 inf
->num
, infpid
.c_str ());
6519 switch_to_no_thread ();
6520 detach_inferior (current_inferior ());
6525 remote_target::detach (inferior
*inf
, int from_tty
)
6527 remote_detach_1 (inf
, from_tty
);
6531 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6533 remote_detach_1 (inf
, from_tty
);
6536 /* Target follow-fork function for remote targets. On entry, and
6537 at return, the current inferior is the fork parent.
6539 Note that although this is currently only used for extended-remote,
6540 it is named remote_follow_fork in anticipation of using it for the
6541 remote target as well. */
6544 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6545 target_waitkind fork_kind
, bool follow_child
,
6548 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6549 fork_kind
, follow_child
, detach_fork
);
6551 if ((fork_kind
== TARGET_WAITKIND_FORKED
6552 && m_features
.remote_fork_event_p ())
6553 || (fork_kind
== TARGET_WAITKIND_VFORKED
6554 && m_features
.remote_vfork_event_p ()))
6556 /* When following the parent and detaching the child, we detach
6557 the child here. For the case of following the child and
6558 detaching the parent, the detach is done in the target-
6559 independent follow fork code in infrun.c. We can't use
6560 target_detach when detaching an unfollowed child because
6561 the client side doesn't know anything about the child. */
6562 if (detach_fork
&& !follow_child
)
6564 /* Detach the fork child. */
6565 remote_detach_pid (child_ptid
.pid ());
6571 remote_target::follow_clone (ptid_t child_ptid
)
6573 remote_add_thread (child_ptid
, false, false, false);
6576 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6577 in the program space of the new inferior. */
6580 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6581 const char *execd_pathname
)
6583 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6585 /* We know that this is a target file name, so if it has the "target:"
6586 prefix we strip it off before saving it in the program space. */
6587 if (is_target_filename (execd_pathname
))
6588 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6590 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6593 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6596 remote_target::disconnect (const char *args
, int from_tty
)
6599 error (_("Argument given to \"disconnect\" when remotely debugging."));
6601 /* Make sure we unpush even the extended remote targets. Calling
6602 target_mourn_inferior won't unpush, and
6603 remote_target::mourn_inferior won't unpush if there is more than
6604 one inferior left. */
6605 remote_unpush_target (this);
6608 gdb_puts ("Ending remote debugging.\n");
6611 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6612 be chatty about it. */
6615 extended_remote_target::attach (const char *args
, int from_tty
)
6617 struct remote_state
*rs
= get_remote_state ();
6619 char *wait_status
= NULL
;
6621 pid
= parse_pid_to_attach (args
);
6623 /* Remote PID can be freely equal to getpid, do not check it here the same
6624 way as in other targets. */
6626 if (m_features
.packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6627 error (_("This target does not support attaching to a process"));
6629 target_announce_attach (from_tty
, pid
);
6631 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6635 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_vAttach
);
6636 switch (result
.status ())
6639 if (!target_is_non_stop_p ())
6641 /* Save the reply for later. */
6642 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6643 strcpy (wait_status
, rs
->buf
.data ());
6645 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6646 error (_("Attaching to %s failed with: %s"),
6647 target_pid_to_str (ptid_t (pid
)).c_str (),
6650 case PACKET_UNKNOWN
:
6651 error (_("This target does not support attaching to a process"));
6653 error (_("Attaching to %s failed: %s"),
6654 target_pid_to_str (ptid_t (pid
)).c_str (), result
.err_msg ());
6657 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6659 inferior_ptid
= ptid_t (pid
);
6661 if (target_is_non_stop_p ())
6663 /* Get list of threads. */
6664 update_thread_list ();
6666 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6667 if (thread
!= nullptr)
6668 switch_to_thread (thread
);
6670 /* Invalidate our notion of the remote current thread. */
6671 record_currthread (rs
, minus_one_ptid
);
6675 /* Now, if we have thread information, update the main thread's
6677 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6679 /* Add the main thread to the thread list. We add the thread
6680 silently in this case (the final true parameter). */
6681 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6683 switch_to_thread (thr
);
6686 /* Next, if the target can specify a description, read it. We do
6687 this before anything involving memory or registers. */
6688 target_find_description ();
6690 if (!target_is_non_stop_p ())
6692 /* Use the previously fetched status. */
6693 gdb_assert (wait_status
!= NULL
);
6695 notif_event_up reply
6696 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6697 push_stop_reply (as_stop_reply_up (std::move (reply
)));
6701 gdb_assert (wait_status
== NULL
);
6703 gdb_assert (target_can_async_p ());
6707 /* Implementation of the to_post_attach method. */
6710 extended_remote_target::post_attach (int pid
)
6712 /* Get text, data & bss offsets. */
6715 /* In certain cases GDB might not have had the chance to start
6716 symbol lookup up until now. This could happen if the debugged
6717 binary is not using shared libraries, the vsyscall page is not
6718 present (on Linux) and the binary itself hadn't changed since the
6719 debugging process was started. */
6720 if (current_program_space
->symfile_object_file
!= NULL
)
6721 remote_check_symbols();
6725 /* Check for the availability of vCont. This function should also check
6729 remote_target::remote_vcont_probe ()
6731 remote_state
*rs
= get_remote_state ();
6734 strcpy (rs
->buf
.data (), "vCont?");
6737 buf
= rs
->buf
.data ();
6739 /* Make sure that the features we assume are supported. */
6740 if (startswith (buf
, "vCont"))
6743 int support_c
, support_C
;
6745 rs
->supports_vCont
.s
= 0;
6746 rs
->supports_vCont
.S
= 0;
6749 rs
->supports_vCont
.t
= 0;
6750 rs
->supports_vCont
.r
= 0;
6751 while (p
&& *p
== ';')
6754 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6755 rs
->supports_vCont
.s
= 1;
6756 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6757 rs
->supports_vCont
.S
= 1;
6758 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6760 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6762 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6763 rs
->supports_vCont
.t
= 1;
6764 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6765 rs
->supports_vCont
.r
= 1;
6767 p
= strchr (p
, ';');
6770 /* If c, and C are not all supported, we can't use vCont. Clearing
6771 BUF will make packet_ok disable the packet. */
6772 if (!support_c
|| !support_C
)
6776 m_features
.packet_ok (rs
->buf
, PACKET_vCont
);
6779 /* Helper function for building "vCont" resumptions. Write a
6780 resumption to P. ENDP points to one-passed-the-end of the buffer
6781 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6782 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6783 resumed thread should be single-stepped and/or signalled. If PTID
6784 equals minus_one_ptid, then all threads are resumed; if PTID
6785 represents a process, then all threads of the process are
6789 remote_target::append_resumption (char *p
, char *endp
,
6790 ptid_t ptid
, int step
, gdb_signal siggnal
)
6792 struct remote_state
*rs
= get_remote_state ();
6794 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6795 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6797 /* GDB is willing to range step. */
6798 && use_range_stepping
6799 /* Target supports range stepping. */
6800 && rs
->supports_vCont
.r
6801 /* We don't currently support range stepping multiple
6802 threads with a wildcard (though the protocol allows it,
6803 so stubs shouldn't make an active effort to forbid
6805 && !(m_features
.remote_multi_process_p () && ptid
.is_pid ()))
6807 struct thread_info
*tp
;
6809 if (ptid
== minus_one_ptid
)
6811 /* If we don't know about the target thread's tid, then
6812 we're resuming magic_null_ptid (see caller). */
6813 tp
= this->find_thread (magic_null_ptid
);
6816 tp
= this->find_thread (ptid
);
6817 gdb_assert (tp
!= NULL
);
6819 if (tp
->control
.may_range_step
)
6821 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
6823 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6824 phex_nz (tp
->control
.step_range_start
,
6826 phex_nz (tp
->control
.step_range_end
,
6830 p
+= xsnprintf (p
, endp
- p
, ";s");
6833 p
+= xsnprintf (p
, endp
- p
, ";s");
6834 else if (siggnal
!= GDB_SIGNAL_0
)
6835 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6837 p
+= xsnprintf (p
, endp
- p
, ";c");
6839 if (m_features
.remote_multi_process_p () && ptid
.is_pid ())
6843 /* All (-1) threads of process. */
6844 nptid
= ptid_t (ptid
.pid (), -1);
6846 p
+= xsnprintf (p
, endp
- p
, ":");
6847 p
= write_ptid (p
, endp
, nptid
);
6849 else if (ptid
!= minus_one_ptid
)
6851 p
+= xsnprintf (p
, endp
- p
, ":");
6852 p
= write_ptid (p
, endp
, ptid
);
6858 /* Clear the thread's private info on resume. */
6861 resume_clear_thread_private_info (struct thread_info
*thread
)
6863 if (thread
->priv
!= NULL
)
6865 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6867 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6868 priv
->watch_data_address
= 0;
6872 /* Append a vCont continue-with-signal action for threads that have a
6873 non-zero stop signal. */
6876 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6879 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6880 if (inferior_ptid
!= thread
->ptid
6881 && thread
->stop_signal () != GDB_SIGNAL_0
)
6883 p
= append_resumption (p
, endp
, thread
->ptid
,
6884 0, thread
->stop_signal ());
6885 thread
->set_stop_signal (GDB_SIGNAL_0
);
6886 resume_clear_thread_private_info (thread
);
6892 /* Set the target running, using the packets that use Hc
6896 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6899 struct remote_state
*rs
= get_remote_state ();
6902 rs
->last_sent_signal
= siggnal
;
6903 rs
->last_sent_step
= step
;
6905 /* The c/s/C/S resume packets use Hc, so set the continue
6907 if (ptid
== minus_one_ptid
)
6908 set_continue_thread (any_thread_ptid
);
6910 set_continue_thread (ptid
);
6912 for (thread_info
*thread
: all_non_exited_threads (this))
6913 resume_clear_thread_private_info (thread
);
6915 buf
= rs
->buf
.data ();
6916 if (::execution_direction
== EXEC_REVERSE
)
6918 /* We don't pass signals to the target in reverse exec mode. */
6919 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6920 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6923 if (step
&& m_features
.packet_support (PACKET_bs
) == PACKET_DISABLE
)
6924 error (_("Remote reverse-step not supported."));
6925 if (!step
&& m_features
.packet_support (PACKET_bc
) == PACKET_DISABLE
)
6926 error (_("Remote reverse-continue not supported."));
6928 strcpy (buf
, step
? "bs" : "bc");
6930 else if (siggnal
!= GDB_SIGNAL_0
)
6932 buf
[0] = step
? 'S' : 'C';
6933 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6934 buf
[2] = tohex (((int) siggnal
) & 0xf);
6938 strcpy (buf
, step
? "s" : "c");
6943 /* Resume the remote inferior by using a "vCont" packet. SCOPE_PTID,
6944 STEP, and SIGGNAL have the same meaning as in target_resume. This
6945 function returns non-zero iff it resumes the inferior.
6947 This function issues a strict subset of all possible vCont commands
6951 remote_target::remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
6952 enum gdb_signal siggnal
)
6954 struct remote_state
*rs
= get_remote_state ();
6958 /* No reverse execution actions defined for vCont. */
6959 if (::execution_direction
== EXEC_REVERSE
)
6962 if (m_features
.packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6965 p
= rs
->buf
.data ();
6966 endp
= p
+ get_remote_packet_size ();
6968 /* If we could generate a wider range of packets, we'd have to worry
6969 about overflowing BUF. Should there be a generic
6970 "multi-part-packet" packet? */
6972 p
+= xsnprintf (p
, endp
- p
, "vCont");
6974 if (scope_ptid
== magic_null_ptid
)
6976 /* MAGIC_NULL_PTID means that we don't have any active threads,
6977 so we don't have any TID numbers the inferior will
6978 understand. Make sure to only send forms that do not specify
6980 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6982 else if (scope_ptid
== minus_one_ptid
|| scope_ptid
.is_pid ())
6984 /* Resume all threads (of all processes, or of a single
6985 process), with preference for INFERIOR_PTID. This assumes
6986 inferior_ptid belongs to the set of all threads we are about
6988 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6990 /* Step inferior_ptid, with or without signal. */
6991 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6994 /* Also pass down any pending signaled resumption for other
6995 threads not the current. */
6996 p
= append_pending_thread_resumptions (p
, endp
, scope_ptid
);
6998 /* And continue others without a signal. */
6999 append_resumption (p
, endp
, scope_ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
7003 /* Scheduler locking; resume only SCOPE_PTID. */
7004 append_resumption (p
, endp
, scope_ptid
, step
, siggnal
);
7007 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
7010 if (target_is_non_stop_p ())
7012 /* In non-stop, the stub replies to vCont with "OK". The stop
7013 reply will be reported asynchronously by means of a `%Stop'
7016 if (strcmp (rs
->buf
.data (), "OK") != 0)
7017 error (_("Unexpected vCont reply in non-stop mode: %s"),
7024 /* Tell the remote machine to resume. */
7027 remote_target::resume (ptid_t scope_ptid
, int step
, enum gdb_signal siggnal
)
7029 struct remote_state
*rs
= get_remote_state ();
7031 /* When connected in non-stop mode, the core resumes threads
7032 individually. Resuming remote threads directly in target_resume
7033 would thus result in sending one packet per thread. Instead, to
7034 minimize roundtrip latency, here we just store the resume
7035 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
7036 resumption will be done in remote_target::commit_resume, where we'll be
7037 able to do vCont action coalescing. */
7038 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
7040 remote_thread_info
*remote_thr
7041 = get_remote_thread_info (inferior_thread ());
7043 /* We don't expect the core to ask to resume an already resumed (from
7044 its point of view) thread. */
7045 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
7047 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
7049 /* There's actually nothing that says that the core can't
7050 request a wildcard resume in non-stop mode, though. It's
7051 just that we know it doesn't currently, so we don't bother
7053 gdb_assert (scope_ptid
== inferior_ptid
);
7057 commit_requested_thread_options ();
7059 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
7060 (explained in remote-notif.c:handle_notification) so
7061 remote_notif_process is not called. We need find a place where
7062 it is safe to start a 'vNotif' sequence. It is good to do it
7063 before resuming inferior, because inferior was stopped and no RSP
7064 traffic at that moment. */
7065 if (!target_is_non_stop_p ())
7066 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
7068 rs
->last_resume_exec_dir
= ::execution_direction
;
7070 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
7071 if (!remote_resume_with_vcont (scope_ptid
, step
, siggnal
))
7072 remote_resume_with_hc (scope_ptid
, step
, siggnal
);
7074 /* Update resumed state tracked by the remote target. */
7075 for (thread_info
*tp
: all_non_exited_threads (this, scope_ptid
))
7076 get_remote_thread_info (tp
)->set_resumed ();
7078 /* We've just told the target to resume. The remote server will
7079 wait for the inferior to stop, and then send a stop reply. In
7080 the mean time, we can't start another command/query ourselves
7081 because the stub wouldn't be ready to process it. This applies
7082 only to the base all-stop protocol, however. In non-stop (which
7083 only supports vCont), the stub replies with an "OK", and is
7084 immediate able to process further serial input. */
7085 if (!target_is_non_stop_p ())
7086 rs
->waiting_for_stop_reply
= 1;
7089 /* Private per-inferior info for target remote processes. */
7091 struct remote_inferior
: public private_inferior
7093 /* Whether we can send a wildcard vCont for this process. */
7094 bool may_wildcard_vcont
= true;
7097 /* Get the remote private inferior data associated to INF. */
7099 static remote_inferior
*
7100 get_remote_inferior (inferior
*inf
)
7102 if (inf
->priv
== NULL
)
7103 inf
->priv
= std::make_unique
<remote_inferior
> ();
7105 return gdb::checked_static_cast
<remote_inferior
*> (inf
->priv
.get ());
7108 /* Class used to track the construction of a vCont packet in the
7109 outgoing packet buffer. This is used to send multiple vCont
7110 packets if we have more actions than would fit a single packet. */
7115 explicit vcont_builder (remote_target
*remote
)
7122 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
7127 /* The remote target. */
7128 remote_target
*m_remote
;
7130 /* Pointer to the first action. P points here if no action has been
7132 char *m_first_action
;
7134 /* Where the next action will be appended. */
7137 /* The end of the buffer. Must never write past this. */
7141 /* Prepare the outgoing buffer for a new vCont packet. */
7144 vcont_builder::restart ()
7146 struct remote_state
*rs
= m_remote
->get_remote_state ();
7148 m_p
= rs
->buf
.data ();
7149 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
7150 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
7151 m_first_action
= m_p
;
7154 /* If the vCont packet being built has any action, send it to the
7158 vcont_builder::flush ()
7160 struct remote_state
*rs
;
7162 if (m_p
== m_first_action
)
7165 rs
= m_remote
->get_remote_state ();
7166 m_remote
->putpkt (rs
->buf
);
7167 m_remote
->getpkt (&rs
->buf
);
7168 if (strcmp (rs
->buf
.data (), "OK") != 0)
7169 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
7172 /* The largest action is range-stepping, with its two addresses. This
7173 is more than sufficient. If a new, bigger action is created, it'll
7174 quickly trigger a failed assertion in append_resumption (and we'll
7176 #define MAX_ACTION_SIZE 200
7178 /* Append a new vCont action in the outgoing packet being built. If
7179 the action doesn't fit the packet along with previous actions, push
7180 what we've got so far to the remote end and start over a new vCont
7181 packet (with the new action). */
7184 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
7186 char buf
[MAX_ACTION_SIZE
+ 1];
7188 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
7189 ptid
, step
, siggnal
);
7191 /* Check whether this new action would fit in the vCont packet along
7192 with previous actions. If not, send what we've got so far and
7193 start a new vCont packet. */
7194 size_t rsize
= endp
- buf
;
7195 if (rsize
> m_endp
- m_p
)
7200 /* Should now fit. */
7201 gdb_assert (rsize
<= m_endp
- m_p
);
7204 memcpy (m_p
, buf
, rsize
);
7209 /* to_commit_resume implementation. */
7212 remote_target::commit_resumed ()
7214 /* If connected in all-stop mode, we'd send the remote resume
7215 request directly from remote_resume. Likewise if
7216 reverse-debugging, as there are no defined vCont actions for
7217 reverse execution. */
7218 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
7221 commit_requested_thread_options ();
7223 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
7224 instead of resuming all threads of each process individually.
7225 However, if any thread of a process must remain halted, we can't
7226 send wildcard resumes and must send one action per thread.
7228 Care must be taken to not resume threads/processes the server
7229 side already told us are stopped, but the core doesn't know about
7230 yet, because the events are still in the vStopped notification
7233 #1 => vCont s:p1.1;c
7235 #3 <= %Stopped T05 p1.1
7240 #8 (infrun handles the stop for p1.1 and continues stepping)
7241 #9 => vCont s:p1.1;c
7243 The last vCont above would resume thread p1.2 by mistake, because
7244 the server has no idea that the event for p1.2 had not been
7247 The server side must similarly ignore resume actions for the
7248 thread that has a pending %Stopped notification (and any other
7249 threads with events pending), until GDB acks the notification
7250 with vStopped. Otherwise, e.g., the following case is
7253 #1 => g (or any other packet)
7255 #3 <= %Stopped T05 p1.2
7256 #4 => vCont s:p1.1;c
7259 Above, the server must not resume thread p1.2. GDB can't know
7260 that p1.2 stopped until it acks the %Stopped notification, and
7261 since from GDB's perspective all threads should be running, it
7264 Finally, special care must also be given to handling fork/vfork
7265 events. A (v)fork event actually tells us that two processes
7266 stopped -- the parent and the child. Until we follow the fork,
7267 we must not resume the child. Therefore, if we have a pending
7268 fork follow, we must not send a global wildcard resume action
7269 (vCont;c). We can still send process-wide wildcards though. */
7271 /* Start by assuming a global wildcard (vCont;c) is possible. */
7272 bool may_global_wildcard_vcont
= true;
7274 /* And assume every process is individually wildcard-able too. */
7275 for (inferior
*inf
: all_non_exited_inferiors (this))
7277 remote_inferior
*priv
= get_remote_inferior (inf
);
7279 priv
->may_wildcard_vcont
= true;
7282 /* Check for any pending events (not reported or processed yet) and
7283 disable process and global wildcard resumes appropriately. */
7284 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
7286 bool any_pending_vcont_resume
= false;
7288 for (thread_info
*tp
: all_non_exited_threads (this))
7290 remote_thread_info
*priv
= get_remote_thread_info (tp
);
7292 /* If a thread of a process is not meant to be resumed, then we
7293 can't wildcard that process. */
7294 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
7296 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
7298 /* And if we can't wildcard a process, we can't wildcard
7299 everything either. */
7300 may_global_wildcard_vcont
= false;
7304 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
7305 any_pending_vcont_resume
= true;
7307 /* If a thread is the parent of an unfollowed fork/vfork/clone,
7308 then we can't do a global wildcard, as that would resume the
7310 if (thread_pending_child_status (tp
) != nullptr)
7311 may_global_wildcard_vcont
= false;
7314 /* We didn't have any resumed thread pending a vCont resume, so nothing to
7316 if (!any_pending_vcont_resume
)
7319 /* Now let's build the vCont packet(s). Actions must be appended
7320 from narrower to wider scopes (thread -> process -> global). If
7321 we end up with too many actions for a single packet vcont_builder
7322 flushes the current vCont packet to the remote side and starts a
7324 struct vcont_builder
vcont_builder (this);
7326 /* Threads first. */
7327 for (thread_info
*tp
: all_non_exited_threads (this))
7329 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7331 /* If the thread was previously vCont-resumed, no need to send a specific
7332 action for it. If we didn't receive a resume request for it, don't
7333 send an action for it either. */
7334 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
7337 gdb_assert (!thread_is_in_step_over_chain (tp
));
7339 /* We should never be commit-resuming a thread that has a stop reply.
7340 Otherwise, we would end up reporting a stop event for a thread while
7341 it is running on the remote target. */
7342 remote_state
*rs
= get_remote_state ();
7343 for (const auto &stop_reply
: rs
->stop_reply_queue
)
7344 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
7346 const resumed_pending_vcont_info
&info
7347 = remote_thr
->resumed_pending_vcont_info ();
7349 /* Check if we need to send a specific action for this thread. If not,
7350 it will be included in a wildcard resume instead. */
7351 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
7352 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
7353 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
7355 remote_thr
->set_resumed ();
7358 /* Now check whether we can send any process-wide wildcard. This is
7359 to avoid sending a global wildcard in the case nothing is
7360 supposed to be resumed. */
7361 bool any_process_wildcard
= false;
7363 for (inferior
*inf
: all_non_exited_inferiors (this))
7365 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7367 any_process_wildcard
= true;
7372 if (any_process_wildcard
)
7374 /* If all processes are wildcard-able, then send a single "c"
7375 action, otherwise, send an "all (-1) threads of process"
7376 continue action for each running process, if any. */
7377 if (may_global_wildcard_vcont
)
7379 vcont_builder
.push_action (minus_one_ptid
,
7380 false, GDB_SIGNAL_0
);
7384 for (inferior
*inf
: all_non_exited_inferiors (this))
7386 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7388 vcont_builder
.push_action (ptid_t (inf
->pid
),
7389 false, GDB_SIGNAL_0
);
7395 vcont_builder
.flush ();
7398 /* Implementation of target_has_pending_events. */
7401 remote_target::has_pending_events ()
7403 if (target_can_async_p ())
7405 remote_state
*rs
= get_remote_state ();
7407 if (rs
->async_event_handler_marked ())
7410 /* Note that BUFCNT can be negative, indicating sticky
7412 if (rs
->remote_desc
->bufcnt
!= 0)
7420 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
7421 thread, all threads of a remote process, or all threads of all
7425 remote_target::remote_stop_ns (ptid_t ptid
)
7427 struct remote_state
*rs
= get_remote_state ();
7428 char *p
= rs
->buf
.data ();
7429 char *endp
= p
+ get_remote_packet_size ();
7431 /* If any thread that needs to stop was resumed but pending a vCont
7432 resume, generate a phony stop_reply. However, first check
7433 whether the thread wasn't resumed with a signal. Generating a
7434 phony stop in that case would result in losing the signal. */
7435 bool needs_commit
= false;
7436 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7438 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7440 if (remote_thr
->get_resume_state ()
7441 == resume_state::RESUMED_PENDING_VCONT
)
7443 const resumed_pending_vcont_info
&info
7444 = remote_thr
->resumed_pending_vcont_info ();
7445 if (info
.sig
!= GDB_SIGNAL_0
)
7447 /* This signal must be forwarded to the inferior. We
7448 could commit-resume just this thread, but its simpler
7449 to just commit-resume everything. */
7450 needs_commit
= true;
7459 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7461 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7463 if (remote_thr
->get_resume_state ()
7464 == resume_state::RESUMED_PENDING_VCONT
)
7466 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
7467 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
7469 pulongest (tp
->ptid
.tid ()));
7471 /* Check that the thread wasn't resumed with a signal.
7472 Generating a phony stop would result in losing the
7474 const resumed_pending_vcont_info
&info
7475 = remote_thr
->resumed_pending_vcont_info ();
7476 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
7478 stop_reply_up sr
= std::make_unique
<stop_reply
> ();
7479 sr
->ptid
= tp
->ptid
;
7481 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
7482 sr
->arch
= tp
->inf
->arch ();
7483 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7484 sr
->watch_data_address
= 0;
7486 this->push_stop_reply (std::move (sr
));
7488 /* Pretend that this thread was actually resumed on the
7489 remote target, then stopped. If we leave it in the
7490 RESUMED_PENDING_VCONT state and the commit_resumed
7491 method is called while the stop reply is still in the
7492 queue, we'll end up reporting a stop event to the core
7493 for that thread while it is running on the remote
7494 target... that would be bad. */
7495 remote_thr
->set_resumed ();
7499 if (!rs
->supports_vCont
.t
)
7500 error (_("Remote server does not support stopping threads"));
7502 if (ptid
== minus_one_ptid
7503 || (!m_features
.remote_multi_process_p () && ptid
.is_pid ()))
7504 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7509 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7512 /* All (-1) threads of process. */
7513 nptid
= ptid_t (ptid
.pid (), -1);
7516 /* Small optimization: if we already have a stop reply for
7517 this thread, no use in telling the stub we want this
7519 if (peek_stop_reply (ptid
))
7525 write_ptid (p
, endp
, nptid
);
7528 /* In non-stop, we get an immediate OK reply. The stop reply will
7529 come in asynchronously by notification. */
7532 if (strcmp (rs
->buf
.data (), "OK") != 0)
7533 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7537 /* All-stop version of target_interrupt. Sends a break or a ^C to
7538 interrupt the remote target. It is undefined which thread of which
7539 process reports the interrupt. */
7542 remote_target::remote_interrupt_as ()
7544 struct remote_state
*rs
= get_remote_state ();
7546 rs
->ctrlc_pending_p
= 1;
7548 /* If the inferior is stopped already, but the core didn't know
7549 about it yet, just ignore the request. The pending stop events
7550 will be collected in remote_wait. */
7551 if (stop_reply_queue_length () > 0)
7554 /* Send interrupt_sequence to remote target. */
7555 send_interrupt_sequence ();
7558 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7559 the remote target. It is undefined which thread of which process
7560 reports the interrupt. Throws an error if the packet is not
7561 supported by the server. */
7564 remote_target::remote_interrupt_ns ()
7566 struct remote_state
*rs
= get_remote_state ();
7567 char *p
= rs
->buf
.data ();
7568 char *endp
= p
+ get_remote_packet_size ();
7570 xsnprintf (p
, endp
- p
, "vCtrlC");
7572 /* In non-stop, we get an immediate OK reply. The stop reply will
7573 come in asynchronously by notification. */
7577 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_vCtrlC
);
7578 switch (result
.status ())
7582 case PACKET_UNKNOWN
:
7583 error (_("No support for interrupting the remote target."));
7585 error (_("Interrupting target failed: %s"), result
.err_msg ());
7589 /* Implement the to_stop function for the remote targets. */
7592 remote_target::stop (ptid_t ptid
)
7594 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7596 if (target_is_non_stop_p ())
7597 remote_stop_ns (ptid
);
7600 /* We don't currently have a way to transparently pause the
7601 remote target in all-stop mode. Interrupt it instead. */
7602 remote_interrupt_as ();
7606 /* Implement the to_interrupt function for the remote targets. */
7609 remote_target::interrupt ()
7611 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7613 if (target_is_non_stop_p ())
7614 remote_interrupt_ns ();
7616 remote_interrupt_as ();
7619 /* Implement the to_pass_ctrlc function for the remote targets. */
7622 remote_target::pass_ctrlc ()
7624 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7626 struct remote_state
*rs
= get_remote_state ();
7628 /* If we're starting up, we're not fully synced yet. Quit
7630 if (rs
->starting_up
)
7632 /* If ^C has already been sent once, offer to disconnect. */
7633 else if (rs
->ctrlc_pending_p
)
7636 target_interrupt ();
7639 /* Ask the user what to do when an interrupt is received. */
7642 remote_target::interrupt_query ()
7644 struct remote_state
*rs
= get_remote_state ();
7646 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7648 if (query (_("The target is not responding to interrupt requests.\n"
7649 "Stop debugging it? ")))
7651 remote_unpush_target (this);
7652 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7657 if (query (_("Interrupted while waiting for the program.\n"
7658 "Give up waiting? ")))
7663 /* Enable/disable target terminal ownership. Most targets can use
7664 terminal groups to control terminal ownership. Remote targets are
7665 different in that explicit transfer of ownership to/from GDB/target
7669 remote_target::terminal_inferior ()
7671 /* NOTE: At this point we could also register our selves as the
7672 recipient of all input. Any characters typed could then be
7673 passed on down to the target. */
7677 remote_target::terminal_ours ()
7682 remote_console_output (const char *msg
, ui_file
*stream
)
7686 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7689 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7698 /* Return the length of the stop reply queue. */
7701 remote_target::stop_reply_queue_length ()
7703 remote_state
*rs
= get_remote_state ();
7704 return rs
->stop_reply_queue
.size ();
7708 remote_notif_stop_parse (remote_target
*remote
,
7709 const notif_client
*self
, const char *buf
,
7710 struct notif_event
*event
)
7712 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7716 remote_notif_stop_ack (remote_target
*remote
,
7717 const notif_client
*self
, const char *buf
,
7718 notif_event_up event
)
7720 stop_reply_up stop_reply
= as_stop_reply_up (std::move (event
));
7723 putpkt (remote
, self
->ack_command
);
7725 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7726 the notification. It was left in the queue because we need to
7727 acknowledge it and pull the rest of the notifications out. */
7728 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7729 remote
->push_stop_reply (std::move (stop_reply
));
7733 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7734 const notif_client
*self
)
7736 /* We can't get pending events in remote_notif_process for
7737 notification stop, and we have to do this in remote_wait_ns
7738 instead. If we fetch all queued events from stub, remote stub
7739 may exit and we have no chance to process them back in
7741 remote_state
*rs
= remote
->get_remote_state ();
7742 rs
->mark_async_event_handler ();
7746 static notif_event_up
7747 remote_notif_stop_alloc_reply ()
7749 return notif_event_up (new struct stop_reply ());
7752 /* A client of notification Stop. */
7754 const notif_client notif_client_stop
=
7758 remote_notif_stop_parse
,
7759 remote_notif_stop_ack
,
7760 remote_notif_stop_can_get_pending_events
,
7761 remote_notif_stop_alloc_reply
,
7765 /* If CONTEXT contains any fork/vfork/clone child threads that have
7766 not been reported yet, remove them from the CONTEXT list. If such
7767 a thread exists it is because we are stopped at a fork/vfork/clone
7768 catchpoint and have not yet called follow_fork/follow_clone, which
7769 will set up the host-side data structures for the new child. */
7772 remote_target::remove_new_children (threads_listing_context
*context
)
7774 const notif_client
*notif
= ¬if_client_stop
;
7776 /* For any threads stopped at a (v)fork/clone event, remove the
7777 corresponding child threads from the CONTEXT list. */
7778 for (thread_info
*thread
: all_non_exited_threads (this))
7780 const target_waitstatus
*ws
= thread_pending_child_status (thread
);
7785 context
->remove_thread (ws
->child_ptid ());
7788 /* Check for any pending (v)fork/clone events (not reported or
7789 processed yet) in process PID and remove those child threads from
7790 the CONTEXT list as well. */
7791 remote_notif_get_pending_events (notif
);
7792 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7793 if (is_new_child_status (event
->ws
.kind ()))
7794 context
->remove_thread (event
->ws
.child_ptid ());
7795 else if (event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7796 context
->remove_thread (event
->ptid
);
7799 /* Check whether any event pending in the vStopped queue would prevent a
7800 global or process wildcard vCont action. Set *may_global_wildcard to
7801 false if we can't do a global wildcard (vCont;c), and clear the event
7802 inferior's may_wildcard_vcont flag if we can't do a process-wide
7803 wildcard resume (vCont;c:pPID.-1). */
7806 remote_target::check_pending_events_prevent_wildcard_vcont
7807 (bool *may_global_wildcard
)
7809 const notif_client
*notif
= ¬if_client_stop
;
7811 remote_notif_get_pending_events (notif
);
7812 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7814 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7815 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7818 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7819 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7820 *may_global_wildcard
= false;
7822 /* This may be the first time we heard about this process.
7823 Regardless, we must not do a global wildcard resume, otherwise
7824 we'd resume this process too. */
7825 *may_global_wildcard
= false;
7826 if (event
->ptid
!= null_ptid
)
7828 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7830 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7835 /* Discard all pending stop replies of inferior INF. */
7838 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7840 struct remote_state
*rs
= get_remote_state ();
7841 struct remote_notif_state
*rns
= rs
->notif_state
;
7843 /* This function can be notified when an inferior exists. When the
7844 target is not remote, the notification state is NULL. */
7845 if (rs
->remote_desc
== NULL
)
7848 struct notif_event
*notif_event
7849 = rns
->pending_event
[notif_client_stop
.id
].get ();
7850 auto *reply
= static_cast<stop_reply
*> (notif_event
);
7852 /* Discard the in-flight notification. */
7853 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7855 /* Leave the notification pending, since the server expects that
7856 we acknowledge it with vStopped. But clear its contents, so
7857 that later on when we acknowledge it, we also discard it. */
7859 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7860 reply
->ptid
.to_string().c_str(),
7861 reply
->ws
.to_string ().c_str ());
7862 reply
->ws
.set_ignore ();
7865 /* Discard the stop replies we have already pulled with
7867 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7868 rs
->stop_reply_queue
.end (),
7869 [=] (const stop_reply_up
&event
)
7871 return event
->ptid
.pid () == inf
->pid
;
7873 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7875 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7876 (*it
)->ptid
.to_string().c_str(),
7877 (*it
)->ws
.to_string ().c_str ());
7878 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7881 /* Discard the stop replies for RS in stop_reply_queue. */
7884 remote_target::discard_pending_stop_replies_in_queue ()
7886 remote_state
*rs
= get_remote_state ();
7888 /* Discard the stop replies we have already pulled with
7890 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7891 rs
->stop_reply_queue
.end (),
7892 [=] (const stop_reply_up
&event
)
7894 return event
->rs
== rs
;
7896 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7899 /* Remove the first reply in 'stop_reply_queue' which matches
7903 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7905 remote_state
*rs
= get_remote_state ();
7907 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7908 rs
->stop_reply_queue
.end (),
7909 [=] (const stop_reply_up
&event
)
7911 return event
->ptid
.matches (ptid
);
7913 stop_reply_up result
;
7914 if (iter
!= rs
->stop_reply_queue
.end ())
7916 result
= std::move (*iter
);
7917 rs
->stop_reply_queue
.erase (iter
);
7921 gdb_printf (gdb_stdlog
,
7922 "notif: discard queued event: 'Stop' in %s\n",
7923 ptid
.to_string ().c_str ());
7928 /* Look for a queued stop reply belonging to PTID. If one is found,
7929 remove it from the queue, and return it. Returns NULL if none is
7930 found. If there are still queued events left to process, tell the
7931 event loop to get back to target_wait soon. */
7934 remote_target::queued_stop_reply (ptid_t ptid
)
7936 remote_state
*rs
= get_remote_state ();
7937 stop_reply_up r
= remote_notif_remove_queued_reply (ptid
);
7939 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7941 /* There's still at least an event left. */
7942 rs
->mark_async_event_handler ();
7948 /* Push a fully parsed stop reply in the stop reply queue. Since we
7949 know that we now have at least one queued event left to pass to the
7950 core side, tell the event loop to get back to target_wait soon. */
7953 remote_target::push_stop_reply (stop_reply_up new_event
)
7955 remote_state
*rs
= get_remote_state ();
7956 rs
->stop_reply_queue
.push_back (std::move (new_event
));
7959 gdb_printf (gdb_stdlog
,
7960 "notif: push 'Stop' %s to queue %d\n",
7961 new_event
->ptid
.to_string ().c_str (),
7962 int (rs
->stop_reply_queue
.size ()));
7964 /* Mark the pending event queue only if async mode is currently enabled.
7965 If async mode is not currently enabled, then, if it later becomes
7966 enabled, and there are events in this queue, we will mark the event
7967 token at that point, see remote_target::async. */
7968 if (target_is_async_p ())
7969 rs
->mark_async_event_handler ();
7972 /* Returns true if we have a stop reply for PTID. */
7975 remote_target::peek_stop_reply (ptid_t ptid
)
7977 remote_state
*rs
= get_remote_state ();
7978 for (auto &event
: rs
->stop_reply_queue
)
7979 if (ptid
== event
->ptid
7980 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7985 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7986 starting with P and ending with PEND matches PREFIX. */
7989 strprefix (const char *p
, const char *pend
, const char *prefix
)
7991 for ( ; p
< pend
; p
++, prefix
++)
7994 return *prefix
== '\0';
7997 /* Parse the stop reply in BUF. Either the function succeeds, and the
7998 result is stored in EVENT, or throws an error. */
8001 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
8003 remote_arch_state
*rsa
= NULL
;
8008 event
->ptid
= null_ptid
;
8009 event
->rs
= get_remote_state ();
8010 event
->ws
.set_ignore ();
8011 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
8012 event
->regcache
.clear ();
8017 case 'T': /* Status with PC, SP, FP, ... */
8018 /* Expedited reply, containing Signal, {regno, reg} repeat. */
8019 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
8021 n... = register number
8022 r... = register contents
8025 p
= &buf
[3]; /* after Txx */
8031 p1
= strchr (p
, ':');
8033 error (_("Malformed packet(a) (missing colon): %s\n\
8037 error (_("Malformed packet(a) (missing register number): %s\n\
8041 /* Some "registers" are actually extended stop information.
8042 Note if you're adding a new entry here: GDB 7.9 and
8043 earlier assume that all register "numbers" that start
8044 with an hex digit are real register numbers. Make sure
8045 the server only sends such a packet if it knows the
8046 client understands it. */
8048 if (strprefix (p
, p1
, "thread"))
8049 event
->ptid
= read_ptid (++p1
, &p
);
8050 else if (strprefix (p
, p1
, "syscall_entry"))
8054 p
= unpack_varlen_hex (++p1
, &sysno
);
8055 event
->ws
.set_syscall_entry ((int) sysno
);
8057 else if (strprefix (p
, p1
, "syscall_return"))
8061 p
= unpack_varlen_hex (++p1
, &sysno
);
8062 event
->ws
.set_syscall_return ((int) sysno
);
8064 else if (strprefix (p
, p1
, "watch")
8065 || strprefix (p
, p1
, "rwatch")
8066 || strprefix (p
, p1
, "awatch"))
8068 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
8069 p
= unpack_varlen_hex (++p1
, &addr
);
8070 event
->watch_data_address
= (CORE_ADDR
) addr
;
8072 else if (strprefix (p
, p1
, "swbreak"))
8074 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
8076 /* Make sure the stub doesn't forget to indicate support
8078 if (m_features
.packet_support (PACKET_swbreak_feature
)
8080 error (_("Unexpected swbreak stop reason"));
8082 /* The value part is documented as "must be empty",
8083 though we ignore it, in case we ever decide to make
8084 use of it in a backward compatible way. */
8085 p
= strchrnul (p1
+ 1, ';');
8087 else if (strprefix (p
, p1
, "hwbreak"))
8089 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
8091 /* Make sure the stub doesn't forget to indicate support
8093 if (m_features
.packet_support (PACKET_hwbreak_feature
)
8095 error (_("Unexpected hwbreak stop reason"));
8098 p
= strchrnul (p1
+ 1, ';');
8100 else if (strprefix (p
, p1
, "library"))
8102 event
->ws
.set_loaded ();
8103 p
= strchrnul (p1
+ 1, ';');
8105 else if (strprefix (p
, p1
, "replaylog"))
8107 event
->ws
.set_no_history ();
8108 /* p1 will indicate "begin" or "end", but it makes
8109 no difference for now, so ignore it. */
8110 p
= strchrnul (p1
+ 1, ';');
8112 else if (strprefix (p
, p1
, "core"))
8116 p
= unpack_varlen_hex (++p1
, &c
);
8119 else if (strprefix (p
, p1
, "fork"))
8120 event
->ws
.set_forked (read_ptid (++p1
, &p
));
8121 else if (strprefix (p
, p1
, "vfork"))
8122 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
8123 else if (strprefix (p
, p1
, "clone"))
8124 event
->ws
.set_thread_cloned (read_ptid (++p1
, &p
));
8125 else if (strprefix (p
, p1
, "vforkdone"))
8127 event
->ws
.set_vfork_done ();
8128 p
= strchrnul (p1
+ 1, ';');
8130 else if (strprefix (p
, p1
, "exec"))
8135 /* Determine the length of the execd pathname. */
8136 p
= unpack_varlen_hex (++p1
, &ignored
);
8137 pathlen
= (p
- p1
) / 2;
8139 /* Save the pathname for event reporting and for
8140 the next run command. */
8141 gdb::unique_xmalloc_ptr
<char> pathname
8142 ((char *) xmalloc (pathlen
+ 1));
8143 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
8144 pathname
.get ()[pathlen
] = '\0';
8146 /* This is freed during event handling. */
8147 event
->ws
.set_execd (std::move (pathname
));
8149 /* Skip the registers included in this packet, since
8150 they may be for an architecture different from the
8151 one used by the original program. */
8154 else if (strprefix (p
, p1
, "create"))
8156 event
->ws
.set_thread_created ();
8157 p
= strchrnul (p1
+ 1, ';');
8166 p
= strchrnul (p1
+ 1, ';');
8171 /* Maybe a real ``P'' register number. */
8172 p_temp
= unpack_varlen_hex (p
, &pnum
);
8173 /* If the first invalid character is the colon, we got a
8174 register number. Otherwise, it's an unknown stop
8178 /* If we haven't parsed the event's thread yet, find
8179 it now, in order to find the architecture of the
8180 reported expedited registers. */
8181 if (event
->ptid
== null_ptid
)
8183 /* If there is no thread-id information then leave
8184 the event->ptid as null_ptid. Later in
8185 process_stop_reply we will pick a suitable
8187 const char *thr
= strstr (p1
+ 1, ";thread:");
8189 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
8196 = (event
->ptid
== null_ptid
8198 : find_inferior_ptid (this, event
->ptid
));
8199 /* If this is the first time we learn anything
8200 about this process, skip the registers
8201 included in this packet, since we don't yet
8202 know which architecture to use to parse them.
8203 We'll determine the architecture later when
8204 we process the stop reply and retrieve the
8205 target description, via
8206 remote_notice_new_inferior ->
8207 post_create_inferior. */
8210 p
= strchrnul (p1
+ 1, ';');
8215 event
->arch
= inf
->arch ();
8216 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
8220 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
8221 cached_reg_t cached_reg
;
8224 error (_("Remote sent bad register number %s: %s\n\
8226 hex_string (pnum
), p
, buf
);
8228 int reg_size
= register_size (event
->arch
, reg
->regnum
);
8229 cached_reg
.num
= reg
->regnum
;
8230 cached_reg
.data
.resize (reg_size
);
8233 fieldsize
= hex2bin (p
, cached_reg
.data
.data (),
8234 cached_reg
.data
.size ());
8236 if (fieldsize
< reg_size
)
8237 warning (_("Remote reply is too short: %s"), buf
);
8239 event
->regcache
.push_back (std::move (cached_reg
));
8243 /* Not a number. Silently skip unknown optional
8245 p
= strchrnul (p1
+ 1, ';');
8250 error (_("Remote register badly formatted: %s\nhere: %s"),
8255 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
8259 case 'S': /* Old style status, just signal only. */
8263 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
8264 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
8265 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
8267 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
8270 case 'w': /* Thread exited. */
8274 p
= unpack_varlen_hex (&buf
[1], &value
);
8275 event
->ws
.set_thread_exited (value
);
8277 error (_("stop reply packet badly formatted: %s"), buf
);
8278 event
->ptid
= read_ptid (++p
, NULL
);
8281 case 'W': /* Target exited. */
8286 /* GDB used to accept only 2 hex chars here. Stubs should
8287 only send more if they detect GDB supports multi-process
8289 p
= unpack_varlen_hex (&buf
[1], &value
);
8293 /* The remote process exited. */
8294 event
->ws
.set_exited (value
);
8298 /* The remote process exited with a signal. */
8299 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
8300 event
->ws
.set_signalled ((enum gdb_signal
) value
);
8302 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
8305 /* If no process is specified, return null_ptid, and let the
8306 caller figure out the right process to use. */
8316 else if (startswith (p
, "process:"))
8320 p
+= sizeof ("process:") - 1;
8321 unpack_varlen_hex (p
, &upid
);
8325 error (_("unknown stop reply packet: %s"), buf
);
8328 error (_("unknown stop reply packet: %s"), buf
);
8329 event
->ptid
= ptid_t (pid
);
8333 event
->ws
.set_no_resumed ();
8334 event
->ptid
= minus_one_ptid
;
8339 /* When the stub wants to tell GDB about a new notification reply, it
8340 sends a notification (%Stop, for example). Those can come it at
8341 any time, hence, we have to make sure that any pending
8342 putpkt/getpkt sequence we're making is finished, before querying
8343 the stub for more events with the corresponding ack command
8344 (vStopped, for example). E.g., if we started a vStopped sequence
8345 immediately upon receiving the notification, something like this
8353 1.6) <-- (registers reply to step #1.3)
8355 Obviously, the reply in step #1.6 would be unexpected to a vStopped
8358 To solve this, whenever we parse a %Stop notification successfully,
8359 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
8360 doing whatever we were doing:
8366 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
8367 2.5) <-- (registers reply to step #2.3)
8369 Eventually after step #2.5, we return to the event loop, which
8370 notices there's an event on the
8371 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
8372 associated callback --- the function below. At this point, we're
8373 always safe to start a vStopped sequence. :
8376 2.7) <-- T05 thread:2
8382 remote_target::remote_notif_get_pending_events (const notif_client
*nc
)
8384 struct remote_state
*rs
= get_remote_state ();
8386 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
8389 gdb_printf (gdb_stdlog
,
8390 "notif: process: '%s' ack pending event\n",
8394 nc
->ack (this, nc
, rs
->buf
.data (),
8395 std::move (rs
->notif_state
->pending_event
[nc
->id
]));
8400 if (strcmp (rs
->buf
.data (), "OK") == 0)
8403 remote_notif_ack (this, nc
, rs
->buf
.data ());
8409 gdb_printf (gdb_stdlog
,
8410 "notif: process: '%s' no pending reply\n",
8415 /* Wrapper around remote_target::remote_notif_get_pending_events to
8416 avoid having to export the whole remote_target class. */
8419 remote_notif_get_pending_events (remote_target
*remote
, const notif_client
*nc
)
8421 remote
->remote_notif_get_pending_events (nc
);
8424 /* Called from process_stop_reply when the stop packet we are responding
8425 to didn't include a process-id or thread-id. STATUS is the stop event
8426 we are responding to.
8428 It is the task of this function to select a suitable thread (or process)
8429 and return its ptid, this is the thread (or process) we will assume the
8430 stop event came from.
8432 In some cases there isn't really any choice about which thread (or
8433 process) is selected, a basic remote with a single process containing a
8434 single thread might choose not to send any process-id or thread-id in
8435 its stop packets, this function will select and return the one and only
8438 However, if a target supports multiple threads (or processes) and still
8439 doesn't include a thread-id (or process-id) in its stop packet then
8440 first, this is a badly behaving target, and second, we're going to have
8441 to select a thread (or process) at random and use that. This function
8442 will print a warning to the user if it detects that there is the
8443 possibility that GDB is guessing which thread (or process) to
8446 Note that this is called before GDB fetches the updated thread list from the
8447 target. So it's possible for the stop reply to be ambiguous and for GDB to
8448 not realize it. For example, if there's initially one thread, the target
8449 spawns a second thread, and then sends a stop reply without an id that
8450 concerns the first thread. GDB will assume the stop reply is about the
8451 first thread - the only thread it knows about - without printing a warning.
8452 Anyway, if the remote meant for the stop reply to be about the second thread,
8453 then it would be really broken, because GDB doesn't know about that thread
8457 remote_target::select_thread_for_ambiguous_stop_reply
8458 (const target_waitstatus
&status
)
8460 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8462 /* Some stop events apply to all threads in an inferior, while others
8463 only apply to a single thread. */
8464 bool process_wide_stop
8465 = (status
.kind () == TARGET_WAITKIND_EXITED
8466 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
8468 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
8470 thread_info
*first_resumed_thread
= nullptr;
8471 bool ambiguous
= false;
8473 /* Consider all non-exited threads of the target, find the first resumed
8475 for (thread_info
*thr
: all_non_exited_threads (this))
8477 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
8479 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
8482 if (first_resumed_thread
== nullptr)
8483 first_resumed_thread
= thr
;
8484 else if (!process_wide_stop
8485 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8489 gdb_assert (first_resumed_thread
!= nullptr);
8491 remote_debug_printf ("first resumed thread is %s",
8492 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8493 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8495 /* Warn if the remote target is sending ambiguous stop replies. */
8498 static bool warned
= false;
8502 /* If you are seeing this warning then the remote target has
8503 stopped without specifying a thread-id, but the target
8504 does have multiple threads (or inferiors), and so GDB is
8505 having to guess which thread stopped.
8507 Examples of what might cause this are the target sending
8508 and 'S' stop packet, or a 'T' stop packet and not
8509 including a thread-id.
8511 Additionally, the target might send a 'W' or 'X packet
8512 without including a process-id, when the target has
8513 multiple running inferiors. */
8514 if (process_wide_stop
)
8515 warning (_("multi-inferior target stopped without "
8516 "sending a process-id, using first "
8517 "non-exited inferior"));
8519 warning (_("multi-threaded target stopped without "
8520 "sending a thread-id, using first "
8521 "non-exited thread"));
8526 /* If this is a stop for all threads then don't use a particular threads
8527 ptid, instead create a new ptid where only the pid field is set. */
8528 if (process_wide_stop
)
8529 return ptid_t (first_resumed_thread
->ptid
.pid ());
8531 return first_resumed_thread
->ptid
;
8534 /* Called when it is decided that STOP_REPLY holds the info of the
8535 event that is to be returned to the core. This function always
8536 destroys STOP_REPLY. */
8539 remote_target::process_stop_reply (stop_reply_up stop_reply
,
8540 struct target_waitstatus
*status
)
8542 *status
= stop_reply
->ws
;
8543 ptid_t ptid
= stop_reply
->ptid
;
8544 struct remote_state
*rs
= get_remote_state ();
8546 /* Forget about last reply's expedited registers. */
8547 rs
->last_seen_expedited_registers
.clear ();
8549 /* If no thread/process was reported by the stub then select a suitable
8551 if (ptid
== null_ptid
)
8552 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8553 gdb_assert (ptid
!= null_ptid
);
8555 if (status
->kind () != TARGET_WAITKIND_EXITED
8556 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8557 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8559 remote_notice_new_inferior (ptid
, false);
8561 /* Expedited registers. */
8562 if (!stop_reply
->regcache
.empty ())
8564 /* 'w' stop replies don't cary expedited registers (which
8565 wouldn't make any sense for a thread that is gone
8567 gdb_assert (status
->kind () != TARGET_WAITKIND_THREAD_EXITED
);
8570 = get_thread_arch_regcache (find_inferior_ptid (this, ptid
), ptid
,
8573 for (cached_reg_t
®
: stop_reply
->regcache
)
8575 regcache
->raw_supply (reg
.num
, reg
.data
);
8576 rs
->last_seen_expedited_registers
.insert (reg
.num
);
8580 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8581 remote_thr
->core
= stop_reply
->core
;
8582 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8583 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8585 if (target_is_non_stop_p ())
8587 /* If the target works in non-stop mode, a stop-reply indicates that
8588 only this thread stopped. */
8589 remote_thr
->set_not_resumed ();
8593 /* If the target works in all-stop mode, a stop-reply indicates that
8594 all the target's threads stopped. */
8595 for (thread_info
*tp
: all_non_exited_threads (this))
8596 get_remote_thread_info (tp
)->set_not_resumed ();
8603 /* The non-stop mode version of target_wait. */
8606 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8607 target_wait_flags options
)
8609 struct remote_state
*rs
= get_remote_state ();
8611 bool is_notif
= false;
8613 /* If in non-stop mode, get out of getpkt even if a
8614 notification is received. */
8616 ret
= getpkt (&rs
->buf
, false /* forever */, &is_notif
);
8619 if (ret
!= -1 && !is_notif
)
8622 case 'E': /* Error of some sort. */
8623 /* We're out of sync with the target now. Did it continue
8624 or not? We can't tell which thread it was in non-stop,
8625 so just ignore this. */
8626 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8628 case 'O': /* Console output. */
8629 remote_console_output (&rs
->buf
[1], gdb_stdtarg
);
8632 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8636 /* Acknowledge a pending stop reply that may have arrived in the
8638 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8639 remote_notif_get_pending_events (¬if_client_stop
);
8641 /* If indeed we noticed a stop reply, we're done. */
8642 stop_reply_up stop_reply
= queued_stop_reply (ptid
);
8643 if (stop_reply
!= NULL
)
8644 return process_stop_reply (std::move (stop_reply
), status
);
8646 /* Still no event. If we're just polling for an event, then
8647 return to the event loop. */
8648 if (options
& TARGET_WNOHANG
)
8650 status
->set_ignore ();
8651 return minus_one_ptid
;
8654 /* Otherwise do a blocking wait. */
8655 ret
= getpkt (&rs
->buf
, true /* forever */, &is_notif
);
8659 /* Return the first resumed thread. */
8662 first_remote_resumed_thread (remote_target
*target
)
8664 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8670 /* Wait until the remote machine stops, then return, storing status in
8671 STATUS just as `wait' would. */
8674 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8675 target_wait_flags options
)
8677 struct remote_state
*rs
= get_remote_state ();
8678 ptid_t event_ptid
= null_ptid
;
8680 stop_reply_up stop_reply
;
8684 status
->set_ignore ();
8686 stop_reply
= queued_stop_reply (ptid
);
8687 if (stop_reply
!= NULL
)
8689 /* None of the paths that push a stop reply onto the queue should
8690 have set the waiting_for_stop_reply flag. */
8691 gdb_assert (!rs
->waiting_for_stop_reply
);
8692 event_ptid
= process_stop_reply (std::move (stop_reply
), status
);
8696 bool forever
= ((options
& TARGET_WNOHANG
) == 0
8697 && rs
->wait_forever_enabled_p
);
8699 if (!rs
->waiting_for_stop_reply
)
8701 status
->set_no_resumed ();
8702 return minus_one_ptid
;
8705 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8706 _never_ wait for ever -> test on target_is_async_p().
8707 However, before we do that we need to ensure that the caller
8708 knows how to take the target into/out of async mode. */
8710 int ret
= getpkt (&rs
->buf
, forever
, &is_notif
);
8712 /* GDB gets a notification. Return to core as this event is
8714 if (ret
!= -1 && is_notif
)
8715 return minus_one_ptid
;
8717 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8718 return minus_one_ptid
;
8720 buf
= rs
->buf
.data ();
8722 /* Assume that the target has acknowledged Ctrl-C unless we receive
8723 an 'F' or 'O' packet. */
8724 if (buf
[0] != 'F' && buf
[0] != 'O')
8725 rs
->ctrlc_pending_p
= 0;
8729 case 'E': /* Error of some sort. */
8730 /* We're out of sync with the target now. Did it continue or
8731 not? Not is more likely, so report a stop. */
8732 rs
->waiting_for_stop_reply
= 0;
8734 warning (_("Remote failure reply: %s"), buf
);
8735 status
->set_stopped (GDB_SIGNAL_0
);
8737 case 'F': /* File-I/O request. */
8738 /* GDB may access the inferior memory while handling the File-I/O
8739 request, but we don't want GDB accessing memory while waiting
8740 for a stop reply. See the comments in putpkt_binary. Set
8741 waiting_for_stop_reply to 0 temporarily. */
8742 rs
->waiting_for_stop_reply
= 0;
8743 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8744 rs
->ctrlc_pending_p
= 0;
8745 /* GDB handled the File-I/O request, and the target is running
8746 again. Keep waiting for events. */
8747 rs
->waiting_for_stop_reply
= 1;
8749 case 'N': case 'T': case 'S': case 'X': case 'W': case 'w':
8751 /* There is a stop reply to handle. */
8752 rs
->waiting_for_stop_reply
= 0;
8755 = as_stop_reply_up (remote_notif_parse (this,
8759 event_ptid
= process_stop_reply (std::move (stop_reply
), status
);
8762 case 'O': /* Console output. */
8763 remote_console_output (buf
+ 1, gdb_stdtarg
);
8766 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8768 /* Zero length reply means that we tried 'S' or 'C' and the
8769 remote system doesn't support it. */
8770 target_terminal::ours_for_output ();
8772 ("Can't send signals to this remote system. %s not sent.\n",
8773 gdb_signal_to_name (rs
->last_sent_signal
));
8774 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8775 target_terminal::inferior ();
8777 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8783 warning (_("Invalid remote reply: %s"), buf
);
8788 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8789 return minus_one_ptid
;
8790 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8792 /* Nothing interesting happened. If we're doing a non-blocking
8793 poll, we're done. Otherwise, go back to waiting. */
8794 if (options
& TARGET_WNOHANG
)
8795 return minus_one_ptid
;
8799 else if (status
->kind () != TARGET_WAITKIND_EXITED
8800 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8802 if (event_ptid
!= null_ptid
)
8803 record_currthread (rs
, event_ptid
);
8805 event_ptid
= first_remote_resumed_thread (this);
8809 /* A process exit. Invalidate our notion of current thread. */
8810 record_currthread (rs
, minus_one_ptid
);
8811 /* It's possible that the packet did not include a pid. */
8812 if (event_ptid
== null_ptid
)
8813 event_ptid
= first_remote_resumed_thread (this);
8814 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8815 if (event_ptid
== null_ptid
)
8816 event_ptid
= magic_null_ptid
;
8822 /* Wait until the remote machine stops, then return, storing status in
8823 STATUS just as `wait' would. */
8826 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8827 target_wait_flags options
)
8829 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8831 remote_state
*rs
= get_remote_state ();
8833 /* Start by clearing the flag that asks for our wait method to be called,
8834 we'll mark it again at the end if needed. If the target is not in
8835 async mode then the async token should not be marked. */
8836 if (target_is_async_p ())
8837 rs
->clear_async_event_handler ();
8839 gdb_assert (!rs
->async_event_handler_marked ());
8843 if (target_is_non_stop_p ())
8844 event_ptid
= wait_ns (ptid
, status
, options
);
8846 event_ptid
= wait_as (ptid
, status
, options
);
8848 if (target_is_async_p ())
8850 /* If there are events left in the queue, or unacknowledged
8851 notifications, then tell the event loop to call us again. */
8852 if (!rs
->stop_reply_queue
.empty ()
8853 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8854 rs
->mark_async_event_handler ();
8860 /* Fetch a single register using a 'p' packet. */
8863 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8866 struct gdbarch
*gdbarch
= regcache
->arch ();
8867 struct remote_state
*rs
= get_remote_state ();
8869 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8872 if (m_features
.packet_support (PACKET_p
) == PACKET_DISABLE
)
8875 if (reg
->pnum
== -1)
8878 p
= rs
->buf
.data ();
8880 p
+= hexnumstr (p
, reg
->pnum
);
8885 buf
= rs
->buf
.data ();
8887 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_p
);
8888 switch (result
.status ())
8892 case PACKET_UNKNOWN
:
8895 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8896 gdbarch_register_name (regcache
->arch (), reg
->regnum
),
8900 /* If this register is unfetchable, tell the regcache. */
8903 regcache
->raw_supply (reg
->regnum
, NULL
);
8907 /* Otherwise, parse and supply the value. */
8913 error (_("fetch_register_using_p: early buf termination"));
8915 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8918 regcache
->raw_supply (reg
->regnum
, regp
);
8922 /* Fetch the registers included in the target's 'g' packet. */
8925 remote_target::send_g_packet ()
8927 struct remote_state
*rs
= get_remote_state ();
8930 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8933 packet_result result
= packet_check_result (rs
->buf
);
8934 if (result
.status () == PACKET_ERROR
)
8935 error (_("Could not read registers; remote failure reply '%s'"),
8938 /* We can get out of synch in various cases. If the first character
8939 in the buffer is not a hex character, assume that has happened
8940 and try to fetch another packet to read. */
8941 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8942 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8943 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8944 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8946 remote_debug_printf ("Bad register packet; fetching a new packet");
8950 buf_len
= strlen (rs
->buf
.data ());
8952 /* Sanity check the received packet. */
8953 if (buf_len
% 2 != 0)
8954 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8960 remote_target::process_g_packet (struct regcache
*regcache
)
8962 struct gdbarch
*gdbarch
= regcache
->arch ();
8963 struct remote_state
*rs
= get_remote_state ();
8964 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8969 buf_len
= strlen (rs
->buf
.data ());
8971 /* Further sanity checks, with knowledge of the architecture. */
8972 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8973 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8975 rsa
->sizeof_g_packet
, buf_len
/ 2,
8978 /* Save the size of the packet sent to us by the target. It is used
8979 as a heuristic when determining the max size of packets that the
8980 target can safely receive. */
8981 if (rsa
->actual_register_packet_size
== 0)
8982 rsa
->actual_register_packet_size
= buf_len
;
8984 /* If this is smaller than we guessed the 'g' packet would be,
8985 update our records. A 'g' reply that doesn't include a register's
8986 value implies either that the register is not available, or that
8987 the 'p' packet must be used. */
8988 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8990 long sizeof_g_packet
= buf_len
/ 2;
8992 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8994 long offset
= rsa
->regs
[i
].offset
;
8995 long reg_size
= register_size (gdbarch
, i
);
8997 if (rsa
->regs
[i
].pnum
== -1)
9000 if (offset
>= sizeof_g_packet
)
9001 rsa
->regs
[i
].in_g_packet
= false;
9002 else if (offset
+ reg_size
> sizeof_g_packet
)
9003 error (_("Truncated register %d in remote 'g' packet"), i
);
9005 rsa
->regs
[i
].in_g_packet
= true;
9008 /* Looks valid enough, we can assume this is the correct length
9009 for a 'g' packet. It's important not to adjust
9010 rsa->sizeof_g_packet if we have truncated registers otherwise
9011 this "if" won't be run the next time the method is called
9012 with a packet of the same size and one of the internal errors
9013 below will trigger instead. */
9014 rsa
->sizeof_g_packet
= sizeof_g_packet
;
9017 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
9019 /* Unimplemented registers read as all bits zero. */
9020 memset (regs
, 0, rsa
->sizeof_g_packet
);
9022 /* Reply describes registers byte by byte, each byte encoded as two
9023 hex characters. Suck them all up, then supply them to the
9024 register cacheing/storage mechanism. */
9026 p
= rs
->buf
.data ();
9027 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
9029 if (p
[0] == 0 || p
[1] == 0)
9030 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
9031 internal_error (_("unexpected end of 'g' packet reply"));
9033 if (p
[0] == 'x' && p
[1] == 'x')
9034 regs
[i
] = 0; /* 'x' */
9036 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
9040 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
9042 struct packet_reg
*r
= &rsa
->regs
[i
];
9043 long reg_size
= register_size (gdbarch
, i
);
9047 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
9048 /* This shouldn't happen - we adjusted in_g_packet above. */
9049 internal_error (_("unexpected end of 'g' packet reply"));
9050 else if (rs
->buf
[r
->offset
* 2] == 'x')
9052 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
9053 /* The register isn't available, mark it as such (at
9054 the same time setting the value to zero). */
9055 regcache
->raw_supply (r
->regnum
, NULL
);
9058 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
9064 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
9067 process_g_packet (regcache
);
9070 /* Make the remote selected traceframe match GDB's selected
9074 remote_target::set_remote_traceframe ()
9077 struct remote_state
*rs
= get_remote_state ();
9079 if (rs
->remote_traceframe_number
== get_traceframe_number ())
9082 /* Avoid recursion, remote_trace_find calls us again. */
9083 rs
->remote_traceframe_number
= get_traceframe_number ();
9085 newnum
= target_trace_find (tfind_number
,
9086 get_traceframe_number (), 0, 0, NULL
);
9088 /* Should not happen. If it does, all bets are off. */
9089 if (newnum
!= get_traceframe_number ())
9090 warning (_("could not set remote traceframe"));
9094 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
9096 struct gdbarch
*gdbarch
= regcache
->arch ();
9097 struct remote_state
*rs
= get_remote_state ();
9098 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
9101 set_remote_traceframe ();
9102 set_general_thread (regcache
->ptid ());
9106 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
9108 gdb_assert (reg
!= NULL
);
9110 /* If this register might be in the 'g' packet, try that first -
9111 we are likely to read more than one register. If this is the
9112 first 'g' packet, we might be overly optimistic about its
9113 contents, so fall back to 'p'. */
9114 if (reg
->in_g_packet
)
9116 fetch_registers_using_g (regcache
);
9117 if (reg
->in_g_packet
)
9121 if (fetch_register_using_p (regcache
, reg
))
9124 /* This register is not available. */
9125 regcache
->raw_supply (reg
->regnum
, NULL
);
9130 fetch_registers_using_g (regcache
);
9132 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
9133 if (!rsa
->regs
[i
].in_g_packet
)
9134 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
9136 /* This register is not available. */
9137 regcache
->raw_supply (i
, NULL
);
9141 /* Prepare to store registers. Since we may send them all (using a
9142 'G' request), we have to read out the ones we don't want to change
9146 remote_target::prepare_to_store (struct regcache
*regcache
)
9148 struct remote_state
*rs
= get_remote_state ();
9149 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
9152 /* Make sure the entire registers array is valid. */
9153 switch (m_features
.packet_support (PACKET_P
))
9155 case PACKET_DISABLE
:
9156 case PACKET_SUPPORT_UNKNOWN
:
9157 /* Make sure all the necessary registers are cached. */
9158 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
9159 if (rsa
->regs
[i
].in_g_packet
)
9160 regcache
->raw_update (rsa
->regs
[i
].regnum
);
9167 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
9168 packet was not recognized. */
9171 remote_target::store_register_using_P (const struct regcache
*regcache
,
9174 struct gdbarch
*gdbarch
= regcache
->arch ();
9175 struct remote_state
*rs
= get_remote_state ();
9176 /* Try storing a single register. */
9177 char *buf
= rs
->buf
.data ();
9178 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
9181 if (m_features
.packet_support (PACKET_P
) == PACKET_DISABLE
)
9184 if (reg
->pnum
== -1)
9187 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
9188 p
= buf
+ strlen (buf
);
9189 regcache
->raw_collect (reg
->regnum
, regp
);
9190 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
9194 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_P
);
9195 switch (result
.status ())
9200 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
9201 gdbarch_register_name (gdbarch
, reg
->regnum
), result
.err_msg ());
9202 case PACKET_UNKNOWN
:
9205 internal_error (_("Bad result from packet_ok"));
9209 /* Store register REGNUM, or all registers if REGNUM == -1, from the
9210 contents of the register cache buffer. FIXME: ignores errors. */
9213 remote_target::store_registers_using_G (const struct regcache
*regcache
)
9215 struct remote_state
*rs
= get_remote_state ();
9216 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
9220 /* Extract all the registers in the regcache copying them into a
9225 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
9226 memset (regs
, 0, rsa
->sizeof_g_packet
);
9227 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
9229 struct packet_reg
*r
= &rsa
->regs
[i
];
9232 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
9236 /* Command describes registers byte by byte,
9237 each byte encoded as two hex characters. */
9238 p
= rs
->buf
.data ();
9240 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
9243 packet_result pkt_status
= packet_check_result (rs
->buf
);
9244 if (pkt_status
.status () == PACKET_ERROR
)
9245 error (_("Could not write registers; remote failure reply '%s'"),
9246 pkt_status
.err_msg ());
9249 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
9250 of the register cache buffer. FIXME: ignores errors. */
9253 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
9255 struct gdbarch
*gdbarch
= regcache
->arch ();
9256 struct remote_state
*rs
= get_remote_state ();
9257 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
9260 set_remote_traceframe ();
9261 set_general_thread (regcache
->ptid ());
9265 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
9267 gdb_assert (reg
!= NULL
);
9269 /* Always prefer to store registers using the 'P' packet if
9270 possible; we often change only a small number of registers.
9271 Sometimes we change a larger number; we'd need help from a
9272 higher layer to know to use 'G'. */
9273 if (store_register_using_P (regcache
, reg
))
9276 /* For now, don't complain if we have no way to write the
9277 register. GDB loses track of unavailable registers too
9278 easily. Some day, this may be an error. We don't have
9279 any way to read the register, either... */
9280 if (!reg
->in_g_packet
)
9283 store_registers_using_G (regcache
);
9287 store_registers_using_G (regcache
);
9289 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
9290 if (!rsa
->regs
[i
].in_g_packet
)
9291 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
9292 /* See above for why we do not issue an error here. */
9297 /* Return the number of hex digits in num. */
9300 hexnumlen (ULONGEST num
)
9304 for (i
= 0; num
!= 0; i
++)
9307 return std::max (i
, 1);
9310 /* Set BUF to the minimum number of hex digits representing NUM. */
9313 hexnumstr (char *buf
, ULONGEST num
)
9315 int len
= hexnumlen (num
);
9317 return hexnumnstr (buf
, num
, len
);
9321 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
9324 hexnumnstr (char *buf
, ULONGEST num
, int width
)
9330 for (i
= width
- 1; i
>= 0; i
--)
9332 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
9339 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
9342 remote_address_masked (CORE_ADDR addr
)
9344 unsigned int address_size
= remote_address_size
;
9346 /* If "remoteaddresssize" was not set, default to target address size. */
9348 address_size
= gdbarch_addr_bit (current_inferior ()->arch ());
9350 if (address_size
> 0
9351 && address_size
< (sizeof (ULONGEST
) * 8))
9353 /* Only create a mask when that mask can safely be constructed
9354 in a ULONGEST variable. */
9357 mask
= (mask
<< address_size
) - 1;
9363 /* Determine whether the remote target supports binary downloading.
9364 This is accomplished by sending a no-op memory write of zero length
9365 to the target at the specified address. It does not suffice to send
9366 the whole packet, since many stubs strip the eighth bit and
9367 subsequently compute a wrong checksum, which causes real havoc with
9370 NOTE: This can still lose if the serial line is not eight-bit
9371 clean. In cases like this, the user should clear "remote
9375 remote_target::check_binary_download (CORE_ADDR addr
)
9377 struct remote_state
*rs
= get_remote_state ();
9379 switch (m_features
.packet_support (PACKET_X
))
9381 case PACKET_DISABLE
:
9385 case PACKET_SUPPORT_UNKNOWN
:
9389 p
= rs
->buf
.data ();
9391 p
+= hexnumstr (p
, (ULONGEST
) addr
);
9393 p
+= hexnumstr (p
, (ULONGEST
) 0);
9397 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9400 if (rs
->buf
[0] == '\0')
9402 remote_debug_printf ("binary downloading NOT supported by target");
9403 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
9407 remote_debug_printf ("binary downloading supported by target");
9408 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
9415 /* Helper function to resize the payload in order to try to get a good
9416 alignment. We try to write an amount of data such that the next write will
9417 start on an address aligned on REMOTE_ALIGN_WRITES. */
9420 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
9422 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
9425 /* Write memory data directly to the remote machine.
9426 This does not inform the data cache; the data cache uses this.
9427 HEADER is the starting part of the packet.
9428 MEMADDR is the address in the remote memory space.
9429 MYADDR is the address of the buffer in our space.
9430 LEN_UNITS is the number of addressable units to write.
9431 UNIT_SIZE is the length in bytes of an addressable unit.
9432 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
9433 should send data as binary ('X'), or hex-encoded ('M').
9435 The function creates packet of the form
9436 <HEADER><ADDRESS>,<LENGTH>:<DATA>
9438 where encoding of <DATA> is terminated by PACKET_FORMAT.
9440 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
9443 Return the transferred status, error or OK (an
9444 'enum target_xfer_status' value). Save the number of addressable units
9445 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
9447 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
9448 exchange between gdb and the stub could look like (?? in place of the
9454 -> $M1000,3:eeeeffffeeee#??
9458 <- eeeeffffeeeedddd */
9461 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
9462 const gdb_byte
*myaddr
,
9465 ULONGEST
*xfered_len_units
,
9466 char packet_format
, int use_length
)
9468 struct remote_state
*rs
= get_remote_state ();
9474 int payload_capacity_bytes
;
9475 int payload_length_bytes
;
9477 if (packet_format
!= 'X' && packet_format
!= 'M')
9478 internal_error (_("remote_write_bytes_aux: bad packet format"));
9481 return TARGET_XFER_EOF
;
9483 payload_capacity_bytes
= get_memory_write_packet_size ();
9485 /* The packet buffer will be large enough for the payload;
9486 get_memory_packet_size ensures this. */
9489 /* Compute the size of the actual payload by subtracting out the
9490 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9492 payload_capacity_bytes
-= strlen ("$,:#NN");
9494 /* The comma won't be used. */
9495 payload_capacity_bytes
+= 1;
9496 payload_capacity_bytes
-= strlen (header
);
9497 payload_capacity_bytes
-= hexnumlen (memaddr
);
9499 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9501 strcat (rs
->buf
.data (), header
);
9502 p
= rs
->buf
.data () + strlen (header
);
9504 /* Compute a best guess of the number of bytes actually transferred. */
9505 if (packet_format
== 'X')
9507 /* Best guess at number of bytes that will fit. */
9508 todo_units
= std::min (len_units
,
9509 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9511 payload_capacity_bytes
-= hexnumlen (todo_units
);
9512 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9516 /* Number of bytes that will fit. */
9518 = std::min (len_units
,
9519 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9521 payload_capacity_bytes
-= hexnumlen (todo_units
);
9522 todo_units
= std::min (todo_units
,
9523 (payload_capacity_bytes
/ unit_size
) / 2);
9526 if (todo_units
<= 0)
9527 internal_error (_("minimum packet size too small to write data"));
9529 /* If we already need another packet, then try to align the end
9530 of this packet to a useful boundary. */
9531 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9532 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9534 /* Append "<memaddr>". */
9535 memaddr
= remote_address_masked (memaddr
);
9536 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9543 /* Append the length and retain its location and size. It may need to be
9544 adjusted once the packet body has been created. */
9546 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9554 /* Append the packet body. */
9555 if (packet_format
== 'X')
9557 /* Binary mode. Send target system values byte by byte, in
9558 increasing byte addresses. Only escape certain critical
9560 payload_length_bytes
=
9561 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9562 &units_written
, payload_capacity_bytes
);
9564 /* If not all TODO units fit, then we'll need another packet. Make
9565 a second try to keep the end of the packet aligned. Don't do
9566 this if the packet is tiny. */
9567 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9571 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9573 if (new_todo_units
!= units_written
)
9574 payload_length_bytes
=
9575 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9576 (gdb_byte
*) p
, &units_written
,
9577 payload_capacity_bytes
);
9580 p
+= payload_length_bytes
;
9581 if (use_length
&& units_written
< todo_units
)
9583 /* Escape chars have filled up the buffer prematurely,
9584 and we have actually sent fewer units than planned.
9585 Fix-up the length field of the packet. Use the same
9586 number of characters as before. */
9587 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9589 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9594 /* Normal mode: Send target system values byte by byte, in
9595 increasing byte addresses. Each byte is encoded as a two hex
9597 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9598 units_written
= todo_units
;
9601 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9604 if (rs
->buf
[0] == 'E')
9605 return TARGET_XFER_E_IO
;
9607 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9608 send fewer units than we'd planned. */
9609 *xfered_len_units
= (ULONGEST
) units_written
;
9610 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9613 /* Write memory data directly to the remote machine.
9614 This does not inform the data cache; the data cache uses this.
9615 MEMADDR is the address in the remote memory space.
9616 MYADDR is the address of the buffer in our space.
9617 LEN is the number of bytes.
9619 Return the transferred status, error or OK (an
9620 'enum target_xfer_status' value). Save the number of bytes
9621 transferred in *XFERED_LEN. Only transfer a single packet. */
9624 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9625 ULONGEST len
, int unit_size
,
9626 ULONGEST
*xfered_len
)
9628 const char *packet_format
= NULL
;
9630 /* Check whether the target supports binary download. */
9631 check_binary_download (memaddr
);
9633 switch (m_features
.packet_support (PACKET_X
))
9636 packet_format
= "X";
9638 case PACKET_DISABLE
:
9639 packet_format
= "M";
9641 case PACKET_SUPPORT_UNKNOWN
:
9642 internal_error (_("remote_write_bytes: bad internal state"));
9644 internal_error (_("bad switch"));
9647 return remote_write_bytes_aux (packet_format
,
9648 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9649 packet_format
[0], 1);
9652 /* Read memory data directly from the remote machine.
9653 This does not use the data cache; the data cache uses this.
9654 MEMADDR is the address in the remote memory space.
9655 MYADDR is the address of the buffer in our space.
9656 LEN_UNITS is the number of addressable memory units to read..
9657 UNIT_SIZE is the length in bytes of an addressable unit.
9659 Return the transferred status, error or OK (an
9660 'enum target_xfer_status' value). Save the number of bytes
9661 transferred in *XFERED_LEN_UNITS.
9663 See the comment of remote_write_bytes_aux for an example of
9664 memory read/write exchange between gdb and the stub. */
9667 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9669 int unit_size
, ULONGEST
*xfered_len_units
)
9671 struct remote_state
*rs
= get_remote_state ();
9672 int buf_size_bytes
; /* Max size of packet output buffer. */
9676 buf_size_bytes
= get_memory_read_packet_size ();
9677 /* The packet buffer will be large enough for the payload;
9678 get_memory_packet_size ensures this. */
9680 /* Number of units that will fit. */
9681 todo_units
= std::min (len_units
,
9682 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9684 memaddr
= remote_address_masked (memaddr
);
9686 /* Construct "m/x"<memaddr>","<len>". */
9687 auto send_request
= [this, rs
, memaddr
, todo_units
] (char format
) -> void
9689 char *buffer
= rs
->buf
.data ();
9691 buffer
+= hexnumstr (buffer
, (ULONGEST
) memaddr
);
9693 buffer
+= hexnumstr (buffer
, (ULONGEST
) todo_units
);
9698 /* Determine which packet format to use. The target's support for
9699 'x' may be unknown. We just try. If it doesn't work, we try
9702 if (m_features
.packet_support (PACKET_x
) == PACKET_DISABLE
)
9703 packet_format
= 'm';
9705 packet_format
= 'x';
9707 send_request (packet_format
);
9708 int packet_len
= getpkt (&rs
->buf
);
9710 return TARGET_XFER_E_IO
;
9712 if (m_features
.packet_support (PACKET_x
) == PACKET_SUPPORT_UNKNOWN
)
9714 if (rs
->buf
[0] == '\0')
9716 remote_debug_printf ("binary uploading NOT supported by target");
9717 m_features
.m_protocol_packets
[PACKET_x
].support
= PACKET_DISABLE
;
9719 /* Try again using 'm'. */
9720 packet_format
= 'm';
9721 send_request (packet_format
);
9722 packet_len
= getpkt (&rs
->buf
);
9724 return TARGET_XFER_E_IO
;
9728 remote_debug_printf ("binary uploading supported by target");
9729 m_features
.m_protocol_packets
[PACKET_x
].support
= PACKET_ENABLE
;
9733 packet_result result
= packet_check_result (rs
->buf
);
9734 if (result
.status () == PACKET_ERROR
)
9735 return TARGET_XFER_E_IO
;
9737 char *p
= rs
->buf
.data ();
9738 if (packet_format
== 'x')
9741 return TARGET_XFER_E_IO
;
9743 /* Adjust for 'b'. */
9746 decoded_bytes
= remote_unescape_input ((const gdb_byte
*) p
,
9748 todo_units
* unit_size
);
9752 /* Reply describes memory byte by byte, each byte encoded as two hex
9754 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9757 /* Return what we have. Let higher layers handle partial reads. */
9758 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9759 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9762 /* Using the set of read-only target sections of remote, read live
9765 For interface/parameters/return description see target.h,
9769 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9773 ULONGEST
*xfered_len
)
9775 const struct target_section
*secp
;
9777 secp
= target_section_by_addr (this, memaddr
);
9779 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9781 ULONGEST memend
= memaddr
+ len
;
9783 const std::vector
<target_section
> *table
9784 = target_get_section_table (this);
9785 for (const target_section
&p
: *table
)
9787 if (memaddr
>= p
.addr
)
9789 if (memend
<= p
.endaddr
)
9791 /* Entire transfer is within this section. */
9792 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9795 else if (memaddr
>= p
.endaddr
)
9797 /* This section ends before the transfer starts. */
9802 /* This section overlaps the transfer. Just do half. */
9803 len
= p
.endaddr
- memaddr
;
9804 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9811 return TARGET_XFER_EOF
;
9814 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9815 first if the requested memory is unavailable in traceframe.
9816 Otherwise, fall back to remote_read_bytes_1. */
9819 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9820 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9821 ULONGEST
*xfered_len
)
9824 return TARGET_XFER_EOF
;
9826 if (get_traceframe_number () != -1)
9828 std::vector
<mem_range
> available
;
9830 /* If we fail to get the set of available memory, then the
9831 target does not support querying traceframe info, and so we
9832 attempt reading from the traceframe anyway (assuming the
9833 target implements the old QTro packet then). */
9834 if (traceframe_available_memory (&available
, memaddr
, len
))
9836 if (available
.empty () || available
[0].start
!= memaddr
)
9838 enum target_xfer_status res
;
9840 /* Don't read into the traceframe's available
9842 if (!available
.empty ())
9844 LONGEST oldlen
= len
;
9846 len
= available
[0].start
- memaddr
;
9847 gdb_assert (len
<= oldlen
);
9850 /* This goes through the topmost target again. */
9851 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9852 len
, unit_size
, xfered_len
);
9853 if (res
== TARGET_XFER_OK
)
9854 return TARGET_XFER_OK
;
9857 /* No use trying further, we know some memory starting
9858 at MEMADDR isn't available. */
9860 return (*xfered_len
!= 0) ?
9861 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9865 /* Don't try to read more than how much is available, in
9866 case the target implements the deprecated QTro packet to
9867 cater for older GDBs (the target's knowledge of read-only
9868 sections may be outdated by now). */
9869 len
= available
[0].length
;
9873 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9878 /* Sends a packet with content determined by the printf format string
9879 FORMAT and the remaining arguments, then gets the reply. Returns
9880 whether the packet was a success, a failure, or unknown. */
9883 remote_target::remote_send_printf (const char *format
, ...)
9885 struct remote_state
*rs
= get_remote_state ();
9886 int max_size
= get_remote_packet_size ();
9889 va_start (ap
, format
);
9892 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9896 if (size
>= max_size
)
9897 internal_error (_("Too long remote packet."));
9899 if (putpkt (rs
->buf
) < 0)
9900 error (_("Communication problem with target."));
9905 return packet_check_result (rs
->buf
).status ();
9908 /* Flash writing can take quite some time. We'll set
9909 effectively infinite timeout for flash operations.
9910 In future, we'll need to decide on a better approach. */
9911 static const int remote_flash_timeout
= 1000;
9914 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9916 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
9917 enum packet_status ret
;
9918 scoped_restore restore_timeout
9919 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9921 ret
= remote_send_printf ("vFlashErase:%s,%s",
9922 phex (address
, addr_size
),
9926 case PACKET_UNKNOWN
:
9927 error (_("Remote target does not support flash erase"));
9929 error (_("Error erasing flash with vFlashErase packet"));
9936 remote_target::remote_flash_write (ULONGEST address
,
9937 ULONGEST length
, ULONGEST
*xfered_len
,
9938 const gdb_byte
*data
)
9940 scoped_restore restore_timeout
9941 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9942 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9947 remote_target::flash_done ()
9951 scoped_restore restore_timeout
9952 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9954 ret
= remote_send_printf ("vFlashDone");
9958 case PACKET_UNKNOWN
:
9959 error (_("Remote target does not support vFlashDone"));
9961 error (_("Error finishing flash operation"));
9968 /* Stuff for dealing with the packets which are part of this protocol.
9969 See comment at top of file for details. */
9971 /* Read a single character from the remote end. The current quit
9972 handler is overridden to avoid quitting in the middle of packet
9973 sequence, as that would break communication with the remote server.
9974 See remote_serial_quit_handler for more detail. */
9977 remote_target::readchar (int timeout
)
9980 struct remote_state
*rs
= get_remote_state ();
9984 scoped_restore restore_quit_target
9985 = make_scoped_restore (&curr_quit_handler_target
, this);
9986 scoped_restore restore_quit
9987 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9989 rs
->got_ctrlc_during_io
= 0;
9991 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9993 if (rs
->got_ctrlc_during_io
)
9996 catch (const gdb_exception_error
&ex
)
9998 remote_unpush_target (this);
9999 throw_error (TARGET_CLOSE_ERROR
,
10000 _("Remote communication error. "
10001 "Target disconnected: %s"),
10008 if (ch
== SERIAL_EOF
)
10010 remote_unpush_target (this);
10011 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
10017 /* Wrapper for serial_write that closes the target and throws if
10018 writing fails. The current quit handler is overridden to avoid
10019 quitting in the middle of packet sequence, as that would break
10020 communication with the remote server. See
10021 remote_serial_quit_handler for more detail. */
10024 remote_target::remote_serial_write (const char *str
, int len
)
10026 struct remote_state
*rs
= get_remote_state ();
10028 scoped_restore restore_quit_target
10029 = make_scoped_restore (&curr_quit_handler_target
, this);
10030 scoped_restore restore_quit
10031 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
10033 rs
->got_ctrlc_during_io
= 0;
10037 serial_write (rs
->remote_desc
, str
, len
);
10039 catch (const gdb_exception_error
&ex
)
10041 remote_unpush_target (this);
10042 throw_error (TARGET_CLOSE_ERROR
,
10043 _("Remote communication error. "
10044 "Target disconnected: %s"),
10048 if (rs
->got_ctrlc_during_io
)
10053 remote_target::remote_serial_send_break ()
10055 struct remote_state
*rs
= get_remote_state ();
10059 serial_send_break (rs
->remote_desc
);
10061 catch (const gdb_exception_error
&ex
)
10063 remote_unpush_target (this);
10064 throw_error (TARGET_CLOSE_ERROR
,
10065 _("Remote communication error. "
10066 "Target disconnected: %s"),
10071 /* Return a string representing an escaped version of BUF, of len N.
10072 E.g. \n is converted to \\n, \t to \\t, etc. */
10075 escape_buffer (const char *buf
, int n
)
10079 stb
.putstrn (buf
, n
, '\\');
10080 return stb
.release ();
10084 remote_target::putpkt (const char *buf
)
10086 return putpkt_binary (buf
, strlen (buf
));
10089 /* Wrapper around remote_target::putpkt to avoid exporting
10093 putpkt (remote_target
*remote
, const char *buf
)
10095 return remote
->putpkt (buf
);
10098 /* Send a packet to the remote machine, with error checking. The data
10099 of the packet is in BUF. The string in BUF can be at most
10100 get_remote_packet_size () - 5 to account for the $, # and checksum,
10101 and for a possible /0 if we are debugging (remote_debug) and want
10102 to print the sent packet as a string. */
10105 remote_target::putpkt_binary (const char *buf
, int cnt
)
10107 struct remote_state
*rs
= get_remote_state ();
10109 unsigned char csum
= 0;
10110 gdb::def_vector
<char> data (cnt
+ 6);
10111 char *buf2
= data
.data ();
10117 /* Catch cases like trying to read memory or listing threads while
10118 we're waiting for a stop reply. The remote server wouldn't be
10119 ready to handle this request, so we'd hang and timeout. We don't
10120 have to worry about this in synchronous mode, because in that
10121 case it's not possible to issue a command while the target is
10122 running. This is not a problem in non-stop mode, because in that
10123 case, the stub is always ready to process serial input. */
10124 if (!target_is_non_stop_p ()
10125 && target_is_async_p ()
10126 && rs
->waiting_for_stop_reply
)
10128 error (_("Cannot execute this command while the target is running.\n"
10129 "Use the \"interrupt\" command to stop the target\n"
10130 "and then try again."));
10133 /* Copy the packet into buffer BUF2, encapsulating it
10134 and giving it a checksum. */
10139 for (i
= 0; i
< cnt
; i
++)
10145 *p
++ = tohex ((csum
>> 4) & 0xf);
10146 *p
++ = tohex (csum
& 0xf);
10148 /* Send it over and over until we get a positive ack. */
10156 int len
= (int) (p
- buf2
);
10159 if (remote_packet_max_chars
< 0)
10162 max_chars
= remote_packet_max_chars
;
10165 = escape_buffer (buf2
, std::min (len
, max_chars
));
10167 if (len
> max_chars
)
10168 remote_debug_printf_nofunc
10169 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
10172 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
10174 remote_serial_write (buf2
, p
- buf2
);
10176 /* If this is a no acks version of the remote protocol, send the
10177 packet and move on. */
10178 if (rs
->noack_mode
)
10181 /* Read until either a timeout occurs (-2) or '+' is read.
10182 Handle any notification that arrives in the mean time. */
10185 ch
= readchar (remote_timeout
);
10190 remote_debug_printf_nofunc ("Received Ack");
10193 remote_debug_printf_nofunc ("Received Nak");
10195 case SERIAL_TIMEOUT
:
10199 break; /* Retransmit buffer. */
10202 remote_debug_printf ("Packet instead of Ack, ignoring it");
10203 /* It's probably an old response sent because an ACK
10204 was lost. Gobble up the packet and ack it so it
10205 doesn't get retransmitted when we resend this
10208 remote_serial_write ("+", 1);
10209 continue; /* Now, go look for +. */
10216 /* If we got a notification, handle it, and go back to looking
10218 /* We've found the start of a notification. Now
10219 collect the data. */
10220 val
= read_frame (&rs
->buf
);
10223 remote_debug_printf_nofunc
10224 (" Notification received: %s",
10225 escape_buffer (rs
->buf
.data (), val
).c_str ());
10227 handle_notification (rs
->notif_state
, rs
->buf
.data ());
10228 /* We're in sync now, rewait for the ack. */
10232 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
10237 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
10241 break; /* Here to retransmit. */
10245 /* This is wrong. If doing a long backtrace, the user should be
10246 able to get out next time we call QUIT, without anything as
10247 violent as interrupt_query. If we want to provide a way out of
10248 here without getting to the next QUIT, it should be based on
10249 hitting ^C twice as in remote_wait. */
10253 interrupt_query ();
10261 /* Come here after finding the start of a frame when we expected an
10262 ack. Do our best to discard the rest of this packet. */
10265 remote_target::skip_frame ()
10271 c
= readchar (remote_timeout
);
10274 case SERIAL_TIMEOUT
:
10275 /* Nothing we can do. */
10278 /* Discard the two bytes of checksum and stop. */
10279 c
= readchar (remote_timeout
);
10281 c
= readchar (remote_timeout
);
10284 case '*': /* Run length encoding. */
10285 /* Discard the repeat count. */
10286 c
= readchar (remote_timeout
);
10291 /* A regular character. */
10297 /* Come here after finding the start of the frame. Collect the rest
10298 into *BUF, verifying the checksum, length, and handling run-length
10299 compression. NUL terminate the buffer. If there is not enough room,
10302 Returns -1 on error, number of characters in buffer (ignoring the
10303 trailing NULL) on success. (could be extended to return one of the
10304 SERIAL status indications). */
10307 remote_target::read_frame (gdb::char_vector
*buf_p
)
10309 unsigned char csum
;
10312 char *buf
= buf_p
->data ();
10313 struct remote_state
*rs
= get_remote_state ();
10320 c
= readchar (remote_timeout
);
10323 case SERIAL_TIMEOUT
:
10324 remote_debug_printf ("Timeout in mid-packet, retrying");
10328 remote_debug_printf ("Saw new packet start in middle of old one");
10329 return -1; /* Start a new packet, count retries. */
10333 unsigned char pktcsum
;
10339 check_0
= readchar (remote_timeout
);
10341 check_1
= readchar (remote_timeout
);
10343 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
10345 remote_debug_printf ("Timeout in checksum, retrying");
10348 else if (check_0
< 0 || check_1
< 0)
10350 remote_debug_printf ("Communication error in checksum");
10354 /* Don't recompute the checksum; with no ack packets we
10355 don't have any way to indicate a packet retransmission
10357 if (rs
->noack_mode
)
10360 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
10361 if (csum
== pktcsum
)
10364 remote_debug_printf
10365 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
10366 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
10368 /* Number of characters in buffer ignoring trailing
10372 case '*': /* Run length encoding. */
10377 c
= readchar (remote_timeout
);
10379 repeat
= c
- ' ' + 3; /* Compute repeat count. */
10381 /* The character before ``*'' is repeated. */
10383 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
10385 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
10387 /* Make some more room in the buffer. */
10388 buf_p
->resize (buf_p
->size () + repeat
);
10389 buf
= buf_p
->data ();
10392 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
10398 gdb_printf (_("Invalid run length encoding: %s\n"), buf
);
10402 if (bc
>= buf_p
->size () - 1)
10404 /* Make some more room in the buffer. */
10405 buf_p
->resize (buf_p
->size () * 2);
10406 buf
= buf_p
->data ();
10416 /* Set this to the maximum number of seconds to wait instead of waiting forever
10417 in target_wait(). If this timer times out, then it generates an error and
10418 the command is aborted. This replaces most of the need for timeouts in the
10419 GDB test suite, and makes it possible to distinguish between a hung target
10420 and one with slow communications. */
10422 static int watchdog
= 0;
10424 show_watchdog (struct ui_file
*file
, int from_tty
,
10425 struct cmd_list_element
*c
, const char *value
)
10427 gdb_printf (file
, _("Watchdog timer is %s.\n"), value
);
10430 /* Read a packet from the remote machine, with error checking, and
10431 store it in *BUF. Resize *BUF if necessary to hold the result. If
10432 FOREVER, wait forever rather than timing out; this is used (in
10433 synchronous mode) to wait for a target that is is executing user
10434 code to stop. If FOREVER == false, this function is allowed to time
10435 out gracefully and return an indication of this to the caller.
10436 Otherwise return the number of bytes read. If IS_NOTIF is not
10437 NULL, then consider receiving a notification enough reason to
10438 return to the caller. In this case, *IS_NOTIF is an output boolean
10439 that indicates whether *BUF holds a notification or not (a regular
10443 remote_target::getpkt (gdb::char_vector
*buf
, bool forever
, bool *is_notif
)
10445 struct remote_state
*rs
= get_remote_state ();
10451 strcpy (buf
->data (), "timeout");
10454 timeout
= watchdog
> 0 ? watchdog
: -1;
10455 else if (is_notif
!= nullptr)
10456 timeout
= 0; /* There should already be a char in the buffer. If
10459 timeout
= remote_timeout
;
10461 #define MAX_TRIES 3
10463 /* Process any number of notifications, and then return when
10464 we get a packet. */
10467 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
10469 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
10471 /* This can loop forever if the remote side sends us
10472 characters continuously, but if it pauses, we'll get
10473 SERIAL_TIMEOUT from readchar because of timeout. Then
10474 we'll count that as a retry.
10476 Note that even when forever is set, we will only wait
10477 forever prior to the start of a packet. After that, we
10478 expect characters to arrive at a brisk pace. They should
10479 show up within remote_timeout intervals. */
10481 c
= readchar (timeout
);
10482 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
10484 if (c
== SERIAL_TIMEOUT
)
10486 if (is_notif
!= nullptr)
10487 return -1; /* Don't complain, it's normal to not get
10488 anything in this case. */
10490 if (forever
) /* Watchdog went off? Kill the target. */
10492 remote_unpush_target (this);
10493 throw_error (TARGET_CLOSE_ERROR
,
10494 _("Watchdog timeout has expired. "
10495 "Target detached."));
10498 remote_debug_printf ("Timed out.");
10502 /* We've found the start of a packet or notification.
10503 Now collect the data. */
10504 val
= read_frame (buf
);
10509 remote_serial_write ("-", 1);
10512 if (tries
> MAX_TRIES
)
10514 /* We have tried hard enough, and just can't receive the
10515 packet/notification. Give up. */
10516 gdb_printf (_("Ignoring packet error, continuing...\n"));
10518 /* Skip the ack char if we're in no-ack mode. */
10519 if (!rs
->noack_mode
)
10520 remote_serial_write ("+", 1);
10524 /* If we got an ordinary packet, return that to our caller. */
10531 if (remote_packet_max_chars
< 0)
10534 max_chars
= remote_packet_max_chars
;
10537 = escape_buffer (buf
->data (),
10538 std::min (val
, max_chars
));
10540 if (val
> max_chars
)
10541 remote_debug_printf_nofunc
10542 ("Packet received: %s [%d of %d bytes omitted]", str
.c_str (),
10543 val
- max_chars
, val
);
10545 remote_debug_printf_nofunc ("Packet received: %s",
10549 /* Skip the ack char if we're in no-ack mode. */
10550 if (!rs
->noack_mode
)
10551 remote_serial_write ("+", 1);
10552 if (is_notif
!= NULL
)
10557 /* If we got a notification, handle it, and go back to looking
10561 gdb_assert (c
== '%');
10563 remote_debug_printf_nofunc
10564 (" Notification received: %s",
10565 escape_buffer (buf
->data (), val
).c_str ());
10567 if (is_notif
!= NULL
)
10570 handle_notification (rs
->notif_state
, buf
->data ());
10572 /* Notifications require no acknowledgement. */
10574 if (is_notif
!= nullptr)
10580 /* Kill any new fork children of inferior INF that haven't been
10581 processed by follow_fork. */
10584 remote_target::kill_new_fork_children (inferior
*inf
)
10586 remote_state
*rs
= get_remote_state ();
10587 const notif_client
*notif
= ¬if_client_stop
;
10589 /* Kill the fork child threads of any threads in inferior INF that are stopped
10590 at a fork event. */
10591 for (thread_info
*thread
: inf
->non_exited_threads ())
10593 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10598 int child_pid
= ws
->child_ptid ().pid ();
10599 int res
= remote_vkill (child_pid
);
10602 error (_("Can't kill fork child process %d"), child_pid
);
10605 /* Check for any pending fork events (not reported or processed yet)
10606 in inferior INF and kill those fork child threads as well. */
10607 remote_notif_get_pending_events (notif
);
10608 for (auto &event
: rs
->stop_reply_queue
)
10610 if (event
->ptid
.pid () != inf
->pid
)
10613 if (!is_fork_status (event
->ws
.kind ()))
10616 int child_pid
= event
->ws
.child_ptid ().pid ();
10617 int res
= remote_vkill (child_pid
);
10620 error (_("Can't kill fork child process %d"), child_pid
);
10625 /* Target hook to kill the current inferior. */
10628 remote_target::kill ()
10631 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10633 gdb_assert (inf
!= nullptr);
10635 if (m_features
.packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10637 /* If we're stopped while forking and we haven't followed yet,
10638 kill the child task. We need to do this before killing the
10639 parent task because if this is a vfork then the parent will
10641 kill_new_fork_children (inf
);
10643 res
= remote_vkill (inf
->pid
);
10646 target_mourn_inferior (inferior_ptid
);
10651 /* If we are in 'target remote' mode and we are killing the only
10652 inferior, then we will tell gdbserver to exit and unpush the
10654 if (res
== -1 && !m_features
.remote_multi_process_p ()
10655 && number_of_live_inferiors (this) == 1)
10659 /* We've killed the remote end, we get to mourn it. If we are
10660 not in extended mode, mourning the inferior also unpushes
10661 remote_ops from the target stack, which closes the remote
10663 target_mourn_inferior (inferior_ptid
);
10668 error (_("Can't kill process"));
10671 /* Send a kill request to the target using the 'vKill' packet. */
10674 remote_target::remote_vkill (int pid
)
10676 if (m_features
.packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10679 remote_state
*rs
= get_remote_state ();
10681 /* Tell the remote target to detach. */
10682 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10686 switch ((m_features
.packet_ok (rs
->buf
, PACKET_vKill
)).status ())
10692 case PACKET_UNKNOWN
:
10695 internal_error (_("Bad result from packet_ok"));
10699 /* Send a kill request to the target using the 'k' packet. */
10702 remote_target::remote_kill_k ()
10704 /* Catch errors so the user can quit from gdb even when we
10705 aren't on speaking terms with the remote system. */
10710 catch (const gdb_exception_error
&ex
)
10712 if (ex
.error
== TARGET_CLOSE_ERROR
)
10714 /* If we got an (EOF) error that caused the target
10715 to go away, then we're done, that's what we wanted.
10716 "k" is susceptible to cause a premature EOF, given
10717 that the remote server isn't actually required to
10718 reply to "k", and it can happen that it doesn't
10719 even get to reply ACK to the "k". */
10723 /* Otherwise, something went wrong. We didn't actually kill
10724 the target. Just propagate the exception, and let the
10725 user or higher layers decide what to do. */
10731 remote_target::mourn_inferior ()
10733 struct remote_state
*rs
= get_remote_state ();
10735 /* We're no longer interested in notification events of an inferior
10736 that exited or was killed/detached. */
10737 discard_pending_stop_replies (current_inferior ());
10739 /* In 'target remote' mode with one inferior, we close the connection. */
10740 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10742 remote_unpush_target (this);
10746 /* In case we got here due to an error, but we're going to stay
10748 rs
->waiting_for_stop_reply
= 0;
10750 /* If the current general thread belonged to the process we just
10751 detached from or has exited, the remote side current general
10752 thread becomes undefined. Considering a case like this:
10754 - We just got here due to a detach.
10755 - The process that we're detaching from happens to immediately
10756 report a global breakpoint being hit in non-stop mode, in the
10757 same thread we had selected before.
10758 - GDB attaches to this process again.
10759 - This event happens to be the next event we handle.
10761 GDB would consider that the current general thread didn't need to
10762 be set on the stub side (with Hg), since for all it knew,
10763 GENERAL_THREAD hadn't changed.
10765 Notice that although in all-stop mode, the remote server always
10766 sets the current thread to the thread reporting the stop event,
10767 that doesn't happen in non-stop mode; in non-stop, the stub *must
10768 not* change the current thread when reporting a breakpoint hit,
10769 due to the decoupling of event reporting and event handling.
10771 To keep things simple, we always invalidate our notion of the
10773 record_currthread (rs
, minus_one_ptid
);
10775 /* Call common code to mark the inferior as not running. */
10776 generic_mourn_inferior ();
10780 extended_remote_target::supports_disable_randomization ()
10782 return (m_features
.packet_support (PACKET_QDisableRandomization
)
10787 remote_target::extended_remote_disable_randomization (int val
)
10789 struct remote_state
*rs
= get_remote_state ();
10792 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10793 "QDisableRandomization:%x", val
);
10795 reply
= remote_get_noisy_reply ();
10796 if (*reply
== '\0')
10797 error (_("Target does not support QDisableRandomization."));
10798 if (strcmp (reply
, "OK") != 0)
10799 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10803 remote_target::extended_remote_run (const std::string
&args
)
10805 struct remote_state
*rs
= get_remote_state ();
10807 const char *remote_exec_file
= get_remote_exec_file ();
10809 /* If the user has disabled vRun support, or we have detected that
10810 support is not available, do not try it. */
10811 if (m_features
.packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10814 strcpy (rs
->buf
.data (), "vRun;");
10815 len
= strlen (rs
->buf
.data ());
10817 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10818 error (_("Remote file name too long for run packet"));
10819 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10820 strlen (remote_exec_file
));
10822 if (!args
.empty ())
10826 gdb_argv
argv (args
.c_str ());
10827 for (i
= 0; argv
[i
] != NULL
; i
++)
10829 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10830 error (_("Argument list too long for run packet"));
10831 rs
->buf
[len
++] = ';';
10832 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10837 rs
->buf
[len
++] = '\0';
10842 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_vRun
);
10843 switch (result
.status ())
10846 /* We have a wait response. All is well. */
10848 case PACKET_UNKNOWN
:
10851 /* If we have a textual error message, print just that. This
10852 makes remote debugging output the same as native output, when
10854 if (result
.textual_err_msg ())
10855 error (("%s"), result
.err_msg ());
10856 if (remote_exec_file
[0] == '\0')
10857 error (_("Running the default executable on the remote target failed; "
10858 "try \"set remote exec-file\"?"));
10860 error (_("Running \"%s\" on the remote target failed"),
10863 gdb_assert_not_reached ("bad switch");
10867 /* Helper function to send set/unset environment packets. ACTION is
10868 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10869 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10873 remote_target::send_environment_packet (const char *action
,
10874 const char *packet
,
10877 remote_state
*rs
= get_remote_state ();
10879 /* Convert the environment variable to an hex string, which
10880 is the best format to be transmitted over the wire. */
10881 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10884 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10885 "%s:%s", packet
, encoded_value
.c_str ());
10889 if (strcmp (rs
->buf
.data (), "OK") != 0)
10890 warning (_("Unable to %s environment variable '%s' on remote."),
10894 /* Helper function to handle the QEnvironment* packets. */
10897 remote_target::extended_remote_environment_support ()
10899 remote_state
*rs
= get_remote_state ();
10901 if (m_features
.packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10903 putpkt ("QEnvironmentReset");
10905 if (strcmp (rs
->buf
.data (), "OK") != 0)
10906 warning (_("Unable to reset environment on remote."));
10909 gdb_environ
*e
= ¤t_inferior ()->environment
;
10911 if (m_features
.packet_support (PACKET_QEnvironmentHexEncoded
)
10914 for (const std::string
&el
: e
->user_set_env ())
10915 send_environment_packet ("set", "QEnvironmentHexEncoded",
10920 if (m_features
.packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10921 for (const std::string
&el
: e
->user_unset_env ())
10922 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10925 /* Helper function to set the current working directory for the
10926 inferior in the remote target. */
10929 remote_target::extended_remote_set_inferior_cwd ()
10931 if (m_features
.packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10933 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10934 remote_state
*rs
= get_remote_state ();
10936 if (!inferior_cwd
.empty ())
10938 std::string hexpath
10939 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10940 inferior_cwd
.size ());
10942 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10943 "QSetWorkingDir:%s", hexpath
.c_str ());
10947 /* An empty inferior_cwd means that the user wants us to
10948 reset the remote server's inferior's cwd. */
10949 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10950 "QSetWorkingDir:");
10955 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_QSetWorkingDir
);
10956 if (result
.status () == PACKET_ERROR
)
10958 Remote replied unexpectedly while setting the inferior's working\n\
10960 result
.err_msg ());
10961 if (result
.status () == PACKET_UNKNOWN
)
10962 error (_("Remote target failed to process setting the inferior's working directory"));
10967 /* In the extended protocol we want to be able to do things like
10968 "run" and have them basically work as expected. So we need
10969 a special create_inferior function. We support changing the
10970 executable file and the command line arguments, but not the
10974 extended_remote_target::create_inferior (const char *exec_file
,
10975 const std::string
&args
,
10976 char **env
, int from_tty
)
10980 struct remote_state
*rs
= get_remote_state ();
10981 const char *remote_exec_file
= get_remote_exec_file ();
10983 /* If running asynchronously, register the target file descriptor
10984 with the event loop. */
10985 if (target_can_async_p ())
10986 target_async (true);
10988 /* Disable address space randomization if requested (and supported). */
10989 if (supports_disable_randomization ())
10990 extended_remote_disable_randomization (disable_randomization
);
10992 /* If startup-with-shell is on, we inform gdbserver to start the
10993 remote inferior using a shell. */
10994 if (m_features
.packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10996 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10997 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
11000 if (strcmp (rs
->buf
.data (), "OK") != 0)
11002 Remote replied unexpectedly while setting startup-with-shell: %s"),
11006 extended_remote_environment_support ();
11008 extended_remote_set_inferior_cwd ();
11010 /* Now restart the remote server. */
11011 run_worked
= extended_remote_run (args
) != -1;
11014 /* vRun was not supported. Fail if we need it to do what the
11016 if (remote_exec_file
[0])
11017 error (_("Remote target does not support \"set remote exec-file\""));
11018 if (!args
.empty ())
11019 error (_("Remote target does not support \"set args\" or run ARGS"));
11021 /* Fall back to "R". */
11022 extended_remote_restart ();
11025 /* vRun's success return is a stop reply. */
11026 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
11027 add_current_inferior_and_thread (stop_reply
);
11029 /* Get updated offsets, if the stub uses qOffsets. */
11034 /* Given a location's target info BP_TGT and the packet buffer BUF, output
11035 the list of conditions (in agent expression bytecode format), if any, the
11036 target needs to evaluate. The output is placed into the packet buffer
11037 started from BUF and ended at BUF_END. */
11040 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
11041 struct bp_target_info
*bp_tgt
, char *buf
,
11044 if (bp_tgt
->conditions
.empty ())
11047 buf
+= strlen (buf
);
11048 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
11051 /* Send conditions to the target. */
11052 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
11054 xsnprintf (buf
, buf_end
- buf
, "X%x,", (int) aexpr
->buf
.size ());
11055 buf
+= strlen (buf
);
11056 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
11057 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
11064 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
11065 struct bp_target_info
*bp_tgt
, char *buf
)
11067 if (bp_tgt
->tcommands
.empty ())
11070 buf
+= strlen (buf
);
11072 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
11073 buf
+= strlen (buf
);
11075 /* Concatenate all the agent expressions that are commands into the
11077 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
11079 sprintf (buf
, "X%x,", (int) aexpr
->buf
.size ());
11080 buf
+= strlen (buf
);
11081 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
11082 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
11087 /* Insert a breakpoint. On targets that have software breakpoint
11088 support, we ask the remote target to do the work; on targets
11089 which don't, we insert a traditional memory breakpoint. */
11092 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
11093 struct bp_target_info
*bp_tgt
)
11095 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
11096 If it succeeds, then set the support to PACKET_ENABLE. If it
11097 fails, and the user has explicitly requested the Z support then
11098 report an error, otherwise, mark it disabled and go on. */
11100 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
11102 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
11103 struct remote_state
*rs
;
11106 /* Make sure the remote is pointing at the right process, if
11108 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11109 set_general_process ();
11111 rs
= get_remote_state ();
11112 p
= rs
->buf
.data ();
11113 endbuf
= p
+ get_remote_packet_size ();
11118 addr
= (ULONGEST
) remote_address_masked (addr
);
11119 p
+= hexnumstr (p
, addr
);
11120 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
11122 if (supports_evaluation_of_breakpoint_conditions ())
11123 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
11125 if (can_run_breakpoint_commands ())
11126 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
11131 switch ((m_features
.packet_ok (rs
->buf
, PACKET_Z0
)).status ())
11137 case PACKET_UNKNOWN
:
11142 /* If this breakpoint has target-side commands but this stub doesn't
11143 support Z0 packets, throw error. */
11144 if (!bp_tgt
->tcommands
.empty ())
11145 throw_error (NOT_SUPPORTED_ERROR
, _("\
11146 Target doesn't support breakpoints that have target side commands."));
11148 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
11152 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
11153 struct bp_target_info
*bp_tgt
,
11154 enum remove_bp_reason reason
)
11156 CORE_ADDR addr
= bp_tgt
->placed_address
;
11157 struct remote_state
*rs
= get_remote_state ();
11159 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
11161 char *p
= rs
->buf
.data ();
11162 char *endbuf
= p
+ get_remote_packet_size ();
11164 /* Make sure the remote is pointing at the right process, if
11166 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11167 set_general_process ();
11173 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
11174 p
+= hexnumstr (p
, addr
);
11175 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
11180 return (rs
->buf
[0] == 'E');
11183 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
11186 static enum Z_packet_type
11187 watchpoint_to_Z_packet (int type
)
11192 return Z_PACKET_WRITE_WP
;
11195 return Z_PACKET_READ_WP
;
11198 return Z_PACKET_ACCESS_WP
;
11201 internal_error (_("hw_bp_to_z: bad watchpoint type %d"), type
);
11206 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
11207 enum target_hw_bp_type type
, struct expression
*cond
)
11209 struct remote_state
*rs
= get_remote_state ();
11210 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
11212 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
11214 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
11215 + to_underlying (packet
))) == PACKET_DISABLE
)
11218 /* Make sure the remote is pointing at the right process, if
11220 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11221 set_general_process ();
11223 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
11224 p
= strchr (rs
->buf
.data (), '\0');
11225 addr
= remote_address_masked (addr
);
11226 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11227 xsnprintf (p
, endbuf
- p
, ",%x", len
);
11232 switch ((m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
11233 + to_underlying (packet
)))).status ())
11237 case PACKET_UNKNOWN
:
11242 internal_error (_("remote_insert_watchpoint: reached end of function"));
11246 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
11247 CORE_ADDR start
, int length
)
11249 CORE_ADDR diff
= remote_address_masked (addr
- start
);
11251 return diff
< length
;
11256 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
11257 enum target_hw_bp_type type
, struct expression
*cond
)
11259 struct remote_state
*rs
= get_remote_state ();
11260 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
11262 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
11264 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
11265 + to_underlying (packet
))) == PACKET_DISABLE
)
11268 /* Make sure the remote is pointing at the right process, if
11270 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11271 set_general_process ();
11273 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
11274 p
= strchr (rs
->buf
.data (), '\0');
11275 addr
= remote_address_masked (addr
);
11276 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11277 xsnprintf (p
, endbuf
- p
, ",%x", len
);
11281 switch ((m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
11282 + to_underlying (packet
)))).status ())
11285 case PACKET_UNKNOWN
:
11290 internal_error (_("remote_remove_watchpoint: reached end of function"));
11294 static int remote_hw_watchpoint_limit
= -1;
11295 static int remote_hw_watchpoint_length_limit
= -1;
11296 static int remote_hw_breakpoint_limit
= -1;
11299 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
11301 if (remote_hw_watchpoint_length_limit
== 0)
11303 else if (remote_hw_watchpoint_length_limit
< 0)
11305 else if (len
<= remote_hw_watchpoint_length_limit
)
11312 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
11314 if (type
== bp_hardware_breakpoint
)
11316 if (remote_hw_breakpoint_limit
== 0)
11318 else if (remote_hw_breakpoint_limit
< 0)
11320 else if (cnt
<= remote_hw_breakpoint_limit
)
11325 if (remote_hw_watchpoint_limit
== 0)
11327 else if (remote_hw_watchpoint_limit
< 0)
11331 else if (cnt
<= remote_hw_watchpoint_limit
)
11337 /* The to_stopped_by_sw_breakpoint method of target remote. */
11340 remote_target::stopped_by_sw_breakpoint ()
11342 struct thread_info
*thread
= inferior_thread ();
11344 return (thread
->priv
!= NULL
11345 && (get_remote_thread_info (thread
)->stop_reason
11346 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
11349 /* The to_supports_stopped_by_sw_breakpoint method of target
11353 remote_target::supports_stopped_by_sw_breakpoint ()
11355 return (m_features
.packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
11358 /* The to_stopped_by_hw_breakpoint method of target remote. */
11361 remote_target::stopped_by_hw_breakpoint ()
11363 struct thread_info
*thread
= inferior_thread ();
11365 return (thread
->priv
!= NULL
11366 && (get_remote_thread_info (thread
)->stop_reason
11367 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
11370 /* The to_supports_stopped_by_hw_breakpoint method of target
11374 remote_target::supports_stopped_by_hw_breakpoint ()
11376 return (m_features
.packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
11380 remote_target::stopped_by_watchpoint ()
11382 struct thread_info
*thread
= inferior_thread ();
11384 return (thread
->priv
!= NULL
11385 && (get_remote_thread_info (thread
)->stop_reason
11386 == TARGET_STOPPED_BY_WATCHPOINT
));
11390 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
11392 struct thread_info
*thread
= inferior_thread ();
11394 if (thread
->priv
!= NULL
11395 && (get_remote_thread_info (thread
)->stop_reason
11396 == TARGET_STOPPED_BY_WATCHPOINT
))
11398 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
11407 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
11408 struct bp_target_info
*bp_tgt
)
11410 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
11411 struct remote_state
*rs
;
11414 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11417 /* Make sure the remote is pointing at the right process, if
11419 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11420 set_general_process ();
11422 rs
= get_remote_state ();
11423 p
= rs
->buf
.data ();
11424 endbuf
= p
+ get_remote_packet_size ();
11430 addr
= remote_address_masked (addr
);
11431 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11432 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11434 if (supports_evaluation_of_breakpoint_conditions ())
11435 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
11437 if (can_run_breakpoint_commands ())
11438 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
11443 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_Z1
);
11444 switch (result
.status ())
11447 error (_("Remote failure reply: %s"), result
.err_msg ());
11448 case PACKET_UNKNOWN
:
11453 internal_error (_("remote_insert_hw_breakpoint: reached end of function"));
11458 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
11459 struct bp_target_info
*bp_tgt
)
11462 struct remote_state
*rs
= get_remote_state ();
11463 char *p
= rs
->buf
.data ();
11464 char *endbuf
= p
+ get_remote_packet_size ();
11466 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11469 /* Make sure the remote is pointing at the right process, if
11471 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11472 set_general_process ();
11478 addr
= remote_address_masked (bp_tgt
->placed_address
);
11479 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11480 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11485 switch ((m_features
.packet_ok (rs
->buf
, PACKET_Z1
)).status ())
11488 case PACKET_UNKNOWN
:
11493 internal_error (_("remote_remove_hw_breakpoint: reached end of function"));
11496 /* Verify memory using the "qCRC:" request. */
11499 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
11501 struct remote_state
*rs
= get_remote_state ();
11502 unsigned long host_crc
, target_crc
;
11505 /* It doesn't make sense to use qCRC if the remote target is
11506 connected but not running. */
11507 if (target_has_execution ()
11508 && m_features
.packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
11510 enum packet_status status
;
11512 /* Make sure the remote is pointing at the right process. */
11513 set_general_process ();
11515 /* FIXME: assumes lma can fit into long. */
11516 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
11517 (long) lma
, (long) size
);
11520 /* Be clever; compute the host_crc before waiting for target
11522 host_crc
= xcrc32 (data
, size
, 0xffffffff);
11526 status
= (m_features
.packet_ok (rs
->buf
, PACKET_qCRC
)).status ();
11527 if (status
== PACKET_ERROR
)
11529 else if (status
== PACKET_OK
)
11531 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11532 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11534 return (host_crc
== target_crc
);
11538 return simple_verify_memory (this, data
, lma
, size
);
11541 /* compare-sections command
11543 With no arguments, compares each loadable section in the exec bfd
11544 with the same memory range on the target, and reports mismatches.
11545 Useful for verifying the image on the target against the exec file. */
11548 compare_sections_command (const char *args
, int from_tty
)
11551 const char *sectname
;
11552 bfd_size_type size
;
11555 int mismatched
= 0;
11559 if (!current_program_space
->exec_bfd ())
11560 error (_("command cannot be used without an exec file"));
11562 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11568 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11570 if (!(s
->flags
& SEC_LOAD
))
11571 continue; /* Skip non-loadable section. */
11573 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11574 continue; /* Skip writeable sections */
11576 size
= bfd_section_size (s
);
11578 continue; /* Skip zero-length section. */
11580 sectname
= bfd_section_name (s
);
11581 if (args
&& strcmp (args
, sectname
) != 0)
11582 continue; /* Not the section selected by user. */
11584 matched
= 1; /* Do this section. */
11587 gdb::byte_vector
sectdata (size
);
11588 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11589 sectdata
.data (), 0, size
);
11591 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11594 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11595 paddress (current_inferior ()->arch (), lma
),
11596 paddress (current_inferior ()->arch (), lma
+ size
));
11598 gdb_printf ("Section %s, range %s -- %s: ", sectname
,
11599 paddress (current_inferior ()->arch (), lma
),
11600 paddress (current_inferior ()->arch (), lma
+ size
));
11602 gdb_printf ("matched.\n");
11605 gdb_printf ("MIS-MATCHED!\n");
11609 if (mismatched
> 0)
11610 warning (_("One or more sections of the target image does "
11611 "not match the loaded file"));
11612 if (args
&& !matched
)
11613 gdb_printf (_("No loaded section named '%s'.\n"), args
);
11616 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11617 into remote target. The number of bytes written to the remote
11618 target is returned, or -1 for error. */
11621 remote_target::remote_write_qxfer (const char *object_name
,
11622 const char *annex
, const gdb_byte
*writebuf
,
11623 ULONGEST offset
, LONGEST len
,
11624 ULONGEST
*xfered_len
,
11625 const unsigned int which_packet
)
11629 struct remote_state
*rs
= get_remote_state ();
11630 int max_size
= get_memory_write_packet_size ();
11632 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11633 return TARGET_XFER_E_IO
;
11635 /* Insert header. */
11636 i
= snprintf (rs
->buf
.data (), max_size
,
11637 "qXfer:%s:write:%s:%s:",
11638 object_name
, annex
? annex
: "",
11639 phex_nz (offset
, sizeof offset
));
11640 max_size
-= (i
+ 1);
11642 /* Escape as much data as fits into rs->buf. */
11643 buf_len
= remote_escape_output
11644 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11646 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11647 || getpkt (&rs
->buf
) < 0
11648 || (m_features
.packet_ok (rs
->buf
, which_packet
)).status () != PACKET_OK
)
11649 return TARGET_XFER_E_IO
;
11651 unpack_varlen_hex (rs
->buf
.data (), &n
);
11654 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11657 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11658 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11659 number of bytes read is returned, or 0 for EOF, or -1 for error.
11660 The number of bytes read may be less than LEN without indicating an
11661 EOF. PACKET is checked and updated to indicate whether the remote
11662 target supports this object. */
11665 remote_target::remote_read_qxfer (const char *object_name
,
11667 gdb_byte
*readbuf
, ULONGEST offset
,
11669 ULONGEST
*xfered_len
,
11670 const unsigned int which_packet
)
11672 struct remote_state
*rs
= get_remote_state ();
11673 LONGEST i
, n
, packet_len
;
11675 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11676 return TARGET_XFER_E_IO
;
11678 /* Check whether we've cached an end-of-object packet that matches
11680 if (rs
->finished_object
)
11682 if (strcmp (object_name
, rs
->finished_object
) == 0
11683 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11684 && offset
== rs
->finished_offset
)
11685 return TARGET_XFER_EOF
;
11688 /* Otherwise, we're now reading something different. Discard
11690 xfree (rs
->finished_object
);
11691 xfree (rs
->finished_annex
);
11692 rs
->finished_object
= NULL
;
11693 rs
->finished_annex
= NULL
;
11696 /* Request only enough to fit in a single packet. The actual data
11697 may not, since we don't know how much of it will need to be escaped;
11698 the target is free to respond with slightly less data. We subtract
11699 five to account for the response type and the protocol frame. */
11700 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11701 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11702 "qXfer:%s:read:%s:%s,%s",
11703 object_name
, annex
? annex
: "",
11704 phex_nz (offset
, sizeof offset
),
11705 phex_nz (n
, sizeof n
));
11706 i
= putpkt (rs
->buf
);
11708 return TARGET_XFER_E_IO
;
11711 packet_len
= getpkt (&rs
->buf
);
11713 || m_features
.packet_ok (rs
->buf
, which_packet
).status () != PACKET_OK
)
11714 return TARGET_XFER_E_IO
;
11716 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11717 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11719 /* 'm' means there is (or at least might be) more data after this
11720 batch. That does not make sense unless there's at least one byte
11721 of data in this reply. */
11722 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11723 error (_("Remote qXfer reply contained no data."));
11725 /* Got some data. */
11726 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11727 packet_len
- 1, readbuf
, n
);
11729 /* 'l' is an EOF marker, possibly including a final block of data,
11730 or possibly empty. If we have the final block of a non-empty
11731 object, record this fact to bypass a subsequent partial read. */
11732 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11734 rs
->finished_object
= xstrdup (object_name
);
11735 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11736 rs
->finished_offset
= offset
+ i
;
11740 return TARGET_XFER_EOF
;
11744 return TARGET_XFER_OK
;
11748 enum target_xfer_status
11749 remote_target::xfer_partial (enum target_object object
,
11750 const char *annex
, gdb_byte
*readbuf
,
11751 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11752 ULONGEST
*xfered_len
)
11754 struct remote_state
*rs
;
11759 = gdbarch_addressable_memory_unit_size (current_inferior ()->arch ());
11761 set_remote_traceframe ();
11762 set_general_thread (inferior_ptid
);
11764 rs
= get_remote_state ();
11766 /* Handle memory using the standard memory routines. */
11767 if (object
== TARGET_OBJECT_MEMORY
)
11769 /* If the remote target is connected but not running, we should
11770 pass this request down to a lower stratum (e.g. the executable
11772 if (!target_has_execution ())
11773 return TARGET_XFER_EOF
;
11775 if (writebuf
!= NULL
)
11776 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11779 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11783 /* Handle extra signal info using qxfer packets. */
11784 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11787 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11788 xfered_len
, PACKET_qXfer_siginfo_read
);
11790 return remote_write_qxfer ("siginfo", annex
, writebuf
, offset
, len
,
11791 xfered_len
, PACKET_qXfer_siginfo_write
);
11794 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11797 return remote_read_qxfer ("statictrace", annex
,
11798 readbuf
, offset
, len
, xfered_len
,
11799 PACKET_qXfer_statictrace_read
);
11801 return TARGET_XFER_E_IO
;
11804 /* Only handle flash writes. */
11805 if (writebuf
!= NULL
)
11809 case TARGET_OBJECT_FLASH
:
11810 return remote_flash_write (offset
, len
, xfered_len
,
11814 return TARGET_XFER_E_IO
;
11818 /* Map pre-existing objects onto letters. DO NOT do this for new
11819 objects!!! Instead specify new query packets. */
11822 case TARGET_OBJECT_AVR
:
11826 case TARGET_OBJECT_AUXV
:
11827 gdb_assert (annex
== NULL
);
11828 return remote_read_qxfer
11829 ("auxv", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_auxv
);
11831 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11832 return remote_read_qxfer
11833 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11834 PACKET_qXfer_features
);
11836 case TARGET_OBJECT_LIBRARIES
:
11837 return remote_read_qxfer
11838 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11839 PACKET_qXfer_libraries
);
11841 case TARGET_OBJECT_LIBRARIES_SVR4
:
11842 return remote_read_qxfer
11843 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11844 PACKET_qXfer_libraries_svr4
);
11846 case TARGET_OBJECT_MEMORY_MAP
:
11847 gdb_assert (annex
== NULL
);
11848 return remote_read_qxfer
11849 ("memory-map", annex
, readbuf
, offset
, len
, xfered_len
,
11850 PACKET_qXfer_memory_map
);
11852 case TARGET_OBJECT_OSDATA
:
11853 /* Should only get here if we're connected. */
11854 gdb_assert (rs
->remote_desc
);
11855 return remote_read_qxfer
11856 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11857 PACKET_qXfer_osdata
);
11859 case TARGET_OBJECT_THREADS
:
11860 gdb_assert (annex
== NULL
);
11861 return remote_read_qxfer
11862 ("threads", annex
, readbuf
, offset
, len
, xfered_len
,
11863 PACKET_qXfer_threads
);
11865 case TARGET_OBJECT_TRACEFRAME_INFO
:
11866 gdb_assert (annex
== NULL
);
11867 return remote_read_qxfer
11868 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11869 PACKET_qXfer_traceframe_info
);
11871 case TARGET_OBJECT_FDPIC
:
11872 return remote_read_qxfer
11873 ("fdpic", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_fdpic
);
11875 case TARGET_OBJECT_OPENVMS_UIB
:
11876 return remote_read_qxfer
11877 ("uib", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_uib
);
11879 case TARGET_OBJECT_BTRACE
:
11880 return remote_read_qxfer
11881 ("btrace", annex
, readbuf
, offset
, len
, xfered_len
,
11882 PACKET_qXfer_btrace
);
11884 case TARGET_OBJECT_BTRACE_CONF
:
11885 return remote_read_qxfer
11886 ("btrace-conf", annex
, readbuf
, offset
, len
, xfered_len
,
11887 PACKET_qXfer_btrace_conf
);
11889 case TARGET_OBJECT_EXEC_FILE
:
11890 return remote_read_qxfer
11891 ("exec-file", annex
, readbuf
, offset
, len
, xfered_len
,
11892 PACKET_qXfer_exec_file
);
11895 return TARGET_XFER_E_IO
;
11898 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11899 large enough let the caller deal with it. */
11900 if (len
< get_remote_packet_size ())
11901 return TARGET_XFER_E_IO
;
11902 len
= get_remote_packet_size ();
11904 /* Except for querying the minimum buffer size, target must be open. */
11905 if (!rs
->remote_desc
)
11906 error (_("remote query is only available after target open"));
11908 gdb_assert (annex
!= NULL
);
11909 gdb_assert (readbuf
!= NULL
);
11911 p2
= rs
->buf
.data ();
11913 *p2
++ = query_type
;
11915 /* We used one buffer char for the remote protocol q command and
11916 another for the query type. As the remote protocol encapsulation
11917 uses 4 chars plus one extra in case we are debugging
11918 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11921 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11923 /* Bad caller may have sent forbidden characters. */
11924 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11929 gdb_assert (annex
[i
] == '\0');
11931 i
= putpkt (rs
->buf
);
11933 return TARGET_XFER_E_IO
;
11936 strcpy ((char *) readbuf
, rs
->buf
.data ());
11938 *xfered_len
= strlen ((char *) readbuf
);
11939 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11942 /* Implementation of to_get_memory_xfer_limit. */
11945 remote_target::get_memory_xfer_limit ()
11947 return get_memory_write_packet_size ();
11951 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11952 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11953 CORE_ADDR
*found_addrp
)
11955 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
11956 struct remote_state
*rs
= get_remote_state ();
11957 int max_size
= get_memory_write_packet_size ();
11959 /* Number of packet bytes used to encode the pattern;
11960 this could be more than PATTERN_LEN due to escape characters. */
11961 int escaped_pattern_len
;
11962 /* Amount of pattern that was encodable in the packet. */
11963 int used_pattern_len
;
11966 ULONGEST found_addr
;
11968 auto read_memory
= [this] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11970 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11974 /* Don't go to the target if we don't have to. This is done before
11975 checking packet_support to avoid the possibility that a success for this
11976 edge case means the facility works in general. */
11977 if (pattern_len
> search_space_len
)
11979 if (pattern_len
== 0)
11981 *found_addrp
= start_addr
;
11985 /* If we already know the packet isn't supported, fall back to the simple
11986 way of searching memory. */
11988 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11990 /* Target doesn't provided special support, fall back and use the
11991 standard support (copy memory and do the search here). */
11992 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11993 pattern
, pattern_len
, found_addrp
);
11996 /* Make sure the remote is pointing at the right process. */
11997 set_general_process ();
11999 /* Insert header. */
12000 i
= snprintf (rs
->buf
.data (), max_size
,
12001 "qSearch:memory:%s;%s;",
12002 phex_nz (start_addr
, addr_size
),
12003 phex_nz (search_space_len
, sizeof (search_space_len
)));
12004 max_size
-= (i
+ 1);
12006 /* Escape as much data as fits into rs->buf. */
12007 escaped_pattern_len
=
12008 remote_escape_output (pattern
, pattern_len
, 1,
12009 (gdb_byte
*) rs
->buf
.data () + i
,
12010 &used_pattern_len
, max_size
);
12012 /* Bail if the pattern is too large. */
12013 if (used_pattern_len
!= pattern_len
)
12014 error (_("Pattern is too large to transmit to remote target."));
12016 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
12017 || getpkt (&rs
->buf
) < 0
12018 || m_features
.packet_ok (rs
->buf
, PACKET_qSearch_memory
).status ()
12021 /* The request may not have worked because the command is not
12022 supported. If so, fall back to the simple way. */
12023 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
12025 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
12026 pattern
, pattern_len
, found_addrp
);
12031 if (rs
->buf
[0] == '0')
12033 else if (rs
->buf
[0] == '1')
12036 if (rs
->buf
[1] != ',')
12037 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
12038 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
12039 *found_addrp
= found_addr
;
12042 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
12048 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
12050 struct remote_state
*rs
= get_remote_state ();
12051 char *p
= rs
->buf
.data ();
12053 if (!rs
->remote_desc
)
12054 error (_("remote rcmd is only available after target open"));
12056 /* Send a NULL command across as an empty command. */
12057 if (command
== NULL
)
12060 /* The query prefix. */
12061 strcpy (rs
->buf
.data (), "qRcmd,");
12062 p
= strchr (rs
->buf
.data (), '\0');
12064 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
12065 > get_remote_packet_size ())
12066 error (_("\"monitor\" command ``%s'' is too long."), command
);
12068 /* Encode the actual command. */
12069 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
12071 if (putpkt (rs
->buf
) < 0)
12072 error (_("Communication problem with target."));
12074 /* get/display the response */
12079 /* XXX - see also remote_get_noisy_reply(). */
12080 QUIT
; /* Allow user to bail out with ^C. */
12082 if (getpkt (&rs
->buf
) == -1)
12084 /* Timeout. Continue to (try to) read responses.
12085 This is better than stopping with an error, assuming the stub
12086 is still executing the (long) monitor command.
12087 If needed, the user can interrupt gdb using C-c, obtaining
12088 an effect similar to stop on timeout. */
12091 buf
= rs
->buf
.data ();
12092 if (buf
[0] == 'O' && buf
[1] != 'K')
12094 /* 'O' message from stub. */
12095 remote_console_output (buf
+ 1, outbuf
);
12098 packet_result result
= packet_check_result (buf
);
12099 switch (result
.status ())
12101 case PACKET_UNKNOWN
:
12102 error (_("Target does not support this command."));
12104 error (_("Protocol error with Rcmd: %s."), result
.err_msg ());
12109 if (strcmp (buf
, "OK") != 0)
12111 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
12113 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
12114 gdb_putc (c
, outbuf
);
12121 std::vector
<mem_region
>
12122 remote_target::memory_map ()
12124 std::vector
<mem_region
> result
;
12125 std::optional
<gdb::char_vector
> text
12126 = target_read_stralloc (current_inferior ()->top_target (),
12127 TARGET_OBJECT_MEMORY_MAP
, NULL
);
12130 result
= parse_memory_map (text
->data ());
12135 /* Set of callbacks used to implement the 'maint packet' command. */
12137 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
12139 /* Called before the packet is sent. BUF is the packet content before
12140 the protocol specific prefix, suffix, and escaping is added. */
12142 void sending (gdb::array_view
<const char> &buf
) override
12144 gdb_puts ("sending: ");
12145 print_packet (buf
);
12149 /* Called with BUF, the reply from the remote target. */
12151 void received (gdb::array_view
<const char> &buf
) override
12153 gdb_puts ("received: \"");
12154 print_packet (buf
);
12160 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
12161 '\x??' with '??' replaced by the hexadecimal value of the byte. */
12164 print_packet (gdb::array_view
<const char> &buf
)
12168 for (int i
= 0; i
< buf
.size (); ++i
)
12170 gdb_byte c
= buf
[i
];
12172 gdb_putc (c
, &stb
);
12174 gdb_printf (&stb
, "\\x%02x", (unsigned char) c
);
12177 gdb_puts (stb
.string ().c_str ());
12181 /* See remote.h. */
12184 send_remote_packet (gdb::array_view
<const char> &buf
,
12185 send_remote_packet_callbacks
*callbacks
)
12187 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
12188 error (_("a remote packet must not be empty"));
12190 remote_target
*remote
= get_current_remote_target ();
12191 if (remote
== nullptr)
12192 error (_("packets can only be sent to a remote target"));
12194 callbacks
->sending (buf
);
12196 remote
->putpkt_binary (buf
.data (), buf
.size ());
12197 remote_state
*rs
= remote
->get_remote_state ();
12198 int bytes
= remote
->getpkt (&rs
->buf
);
12201 error (_("error while fetching packet from remote target"));
12203 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
12204 callbacks
->received (view
);
12207 /* Entry point for the 'maint packet' command. */
12210 cli_packet_command (const char *args
, int from_tty
)
12212 cli_packet_command_callbacks cb
;
12213 gdb::array_view
<const char> view
12214 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
12215 send_remote_packet (view
, &cb
);
12219 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
12221 static void display_thread_info (struct gdb_ext_thread_info
*info
);
12223 static void threadset_test_cmd (char *cmd
, int tty
);
12225 static void threadalive_test (char *cmd
, int tty
);
12227 static void threadlist_test_cmd (char *cmd
, int tty
);
12229 int get_and_display_threadinfo (threadref
*ref
);
12231 static void threadinfo_test_cmd (char *cmd
, int tty
);
12233 static int thread_display_step (threadref
*ref
, void *context
);
12235 static void threadlist_update_test_cmd (char *cmd
, int tty
);
12237 static void init_remote_threadtests (void);
12239 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
12242 threadset_test_cmd (const char *cmd
, int tty
)
12244 int sample_thread
= SAMPLE_THREAD
;
12246 gdb_printf (_("Remote threadset test\n"));
12247 set_general_thread (sample_thread
);
12252 threadalive_test (const char *cmd
, int tty
)
12254 int sample_thread
= SAMPLE_THREAD
;
12255 int pid
= inferior_ptid
.pid ();
12256 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
12258 if (remote_thread_alive (ptid
))
12259 gdb_printf ("PASS: Thread alive test\n");
12261 gdb_printf ("FAIL: Thread alive test\n");
12264 void output_threadid (char *title
, threadref
*ref
);
12267 output_threadid (char *title
, threadref
*ref
)
12271 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
12273 gdb_printf ("%s %s\n", title
, (&hexid
[0]));
12277 threadlist_test_cmd (const char *cmd
, int tty
)
12280 threadref nextthread
;
12281 int done
, result_count
;
12282 threadref threadlist
[3];
12284 gdb_printf ("Remote Threadlist test\n");
12285 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
12286 &result_count
, &threadlist
[0]))
12287 gdb_printf ("FAIL: threadlist test\n");
12290 threadref
*scan
= threadlist
;
12291 threadref
*limit
= scan
+ result_count
;
12293 while (scan
< limit
)
12294 output_threadid (" thread ", scan
++);
12299 display_thread_info (struct gdb_ext_thread_info
*info
)
12301 output_threadid ("Threadid: ", &info
->threadid
);
12302 gdb_printf ("Name: %s\n ", info
->shortname
);
12303 gdb_printf ("State: %s\n", info
->display
);
12304 gdb_printf ("other: %s\n\n", info
->more_display
);
12308 get_and_display_threadinfo (threadref
*ref
)
12312 struct gdb_ext_thread_info threadinfo
;
12314 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
12315 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
12316 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
12317 display_thread_info (&threadinfo
);
12322 threadinfo_test_cmd (const char *cmd
, int tty
)
12324 int athread
= SAMPLE_THREAD
;
12328 int_to_threadref (&thread
, athread
);
12329 gdb_printf ("Remote Threadinfo test\n");
12330 if (!get_and_display_threadinfo (&thread
))
12331 gdb_printf ("FAIL cannot get thread info\n");
12335 thread_display_step (threadref
*ref
, void *context
)
12337 /* output_threadid(" threadstep ",ref); *//* simple test */
12338 return get_and_display_threadinfo (ref
);
12342 threadlist_update_test_cmd (const char *cmd
, int tty
)
12344 gdb_printf ("Remote Threadlist update test\n");
12345 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
12349 init_remote_threadtests (void)
12351 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
12352 _("Fetch and print the remote list of "
12353 "thread identifiers, one pkt only."));
12354 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
12355 _("Fetch and display info about one thread."));
12356 add_com ("tset", class_obscure
, threadset_test_cmd
,
12357 _("Test setting to a different thread."));
12358 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
12359 _("Iterate through updating all remote thread info."));
12360 add_com ("talive", class_obscure
, threadalive_test
,
12361 _("Remote thread alive test."));
12366 /* Convert a thread ID to a string. */
12369 remote_target::pid_to_str (ptid_t ptid
)
12371 if (ptid
== null_ptid
)
12372 return normal_pid_to_str (ptid
);
12373 else if (ptid
.is_pid ())
12375 /* Printing an inferior target id. */
12377 /* When multi-process extensions are off, there's no way in the
12378 remote protocol to know the remote process id, if there's any
12379 at all. There's one exception --- when we're connected with
12380 target extended-remote, and we manually attached to a process
12381 with "attach PID". We don't record anywhere a flag that
12382 allows us to distinguish that case from the case of
12383 connecting with extended-remote and the stub already being
12384 attached to a process, and reporting yes to qAttached, hence
12385 no smart special casing here. */
12386 if (!m_features
.remote_multi_process_p ())
12387 return "Remote target";
12389 return normal_pid_to_str (ptid
);
12393 if (magic_null_ptid
== ptid
)
12394 return "Thread <main>";
12395 else if (m_features
.remote_multi_process_p ())
12396 if (ptid
.lwp () == 0)
12397 return normal_pid_to_str (ptid
);
12399 return string_printf ("Thread %d.%ld",
12400 ptid
.pid (), ptid
.lwp ());
12402 return string_printf ("Thread %ld", ptid
.lwp ());
12406 /* Get the address of the thread local variable in OBJFILE which is
12407 stored at OFFSET within the thread local storage for thread PTID. */
12410 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
12413 if (m_features
.packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
12415 struct remote_state
*rs
= get_remote_state ();
12416 char *p
= rs
->buf
.data ();
12417 char *endp
= p
+ get_remote_packet_size ();
12419 strcpy (p
, "qGetTLSAddr:");
12421 p
= write_ptid (p
, endp
, ptid
);
12423 p
+= hexnumstr (p
, offset
);
12425 p
+= hexnumstr (p
, lm
);
12430 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTLSAddr
);
12431 if (result
.status () == PACKET_OK
)
12435 unpack_varlen_hex (rs
->buf
.data (), &addr
);
12438 else if (result
.status () == PACKET_UNKNOWN
)
12439 throw_error (TLS_GENERIC_ERROR
,
12440 _("Remote target doesn't support qGetTLSAddr packet"));
12442 throw_error (TLS_GENERIC_ERROR
,
12443 _("Remote target failed to process qGetTLSAddr request"));
12446 throw_error (TLS_GENERIC_ERROR
,
12447 _("TLS not supported or disabled on this target"));
12452 /* Provide thread local base, i.e. Thread Information Block address.
12453 Returns 1 if ptid is found and thread_local_base is non zero. */
12456 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
12458 if (m_features
.packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
12460 struct remote_state
*rs
= get_remote_state ();
12461 char *p
= rs
->buf
.data ();
12462 char *endp
= p
+ get_remote_packet_size ();
12464 strcpy (p
, "qGetTIBAddr:");
12466 p
= write_ptid (p
, endp
, ptid
);
12471 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTIBAddr
);
12472 if (result
.status () == PACKET_OK
)
12475 unpack_varlen_hex (rs
->buf
.data (), &val
);
12477 *addr
= (CORE_ADDR
) val
;
12480 else if (result
.status () == PACKET_UNKNOWN
)
12481 error (_("Remote target doesn't support qGetTIBAddr packet"));
12483 error (_("Remote target failed to process qGetTIBAddr request, %s"),
12484 result
.err_msg ());
12487 error (_("qGetTIBAddr not supported or disabled on this target"));
12492 /* Support for inferring a target description based on the current
12493 architecture and the size of a 'g' packet. While the 'g' packet
12494 can have any size (since optional registers can be left off the
12495 end), some sizes are easily recognizable given knowledge of the
12496 approximate architecture. */
12498 struct remote_g_packet_guess
12500 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
12507 const struct target_desc
*tdesc
;
12510 struct remote_g_packet_data
12512 std::vector
<remote_g_packet_guess
> guesses
;
12515 static const registry
<gdbarch
>::key
<struct remote_g_packet_data
>
12516 remote_g_packet_data_handle
;
12518 static struct remote_g_packet_data
*
12519 get_g_packet_data (struct gdbarch
*gdbarch
)
12521 struct remote_g_packet_data
*data
12522 = remote_g_packet_data_handle
.get (gdbarch
);
12523 if (data
== nullptr)
12524 data
= remote_g_packet_data_handle
.emplace (gdbarch
);
12529 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12530 const struct target_desc
*tdesc
)
12532 struct remote_g_packet_data
*data
= get_g_packet_data (gdbarch
);
12534 gdb_assert (tdesc
!= NULL
);
12536 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12537 if (guess
.bytes
== bytes
)
12538 internal_error (_("Duplicate g packet description added for size %d"),
12541 data
->guesses
.emplace_back (bytes
, tdesc
);
12544 /* Return true if remote_read_description would do anything on this target
12545 and architecture, false otherwise. */
12548 remote_read_description_p (struct target_ops
*target
)
12550 remote_g_packet_data
*data
= get_g_packet_data (current_inferior ()->arch ());
12552 return !data
->guesses
.empty ();
12555 const struct target_desc
*
12556 remote_target::read_description ()
12558 remote_g_packet_data
*data
= get_g_packet_data (current_inferior ()->arch ());
12560 /* Do not try this during initial connection, when we do not know
12561 whether there is a running but stopped thread. */
12562 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12563 return beneath ()->read_description ();
12565 if (!data
->guesses
.empty ())
12567 int bytes
= send_g_packet ();
12569 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12570 if (guess
.bytes
== bytes
)
12571 return guess
.tdesc
;
12573 /* We discard the g packet. A minor optimization would be to
12574 hold on to it, and fill the register cache once we have selected
12575 an architecture, but it's too tricky to do safely. */
12578 return beneath ()->read_description ();
12581 /* Remote file transfer support. This is host-initiated I/O, not
12582 target-initiated; for target-initiated, see remote-fileio.c. */
12584 /* If *LEFT is at least the length of STRING, copy STRING to
12585 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12586 decrease *LEFT. Otherwise raise an error. */
12589 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12591 int len
= strlen (string
);
12594 error (_("Packet too long for target."));
12596 memcpy (*buffer
, string
, len
);
12600 /* NUL-terminate the buffer as a convenience, if there is
12606 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12607 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12608 decrease *LEFT. Otherwise raise an error. */
12611 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12614 if (2 * len
> *left
)
12615 error (_("Packet too long for target."));
12617 bin2hex (bytes
, *buffer
, len
);
12618 *buffer
+= 2 * len
;
12621 /* NUL-terminate the buffer as a convenience, if there is
12627 /* If *LEFT is large enough, convert VALUE to hex and add it to
12628 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12629 decrease *LEFT. Otherwise raise an error. */
12632 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12634 int len
= hexnumlen (value
);
12637 error (_("Packet too long for target."));
12639 hexnumstr (*buffer
, value
);
12643 /* NUL-terminate the buffer as a convenience, if there is
12649 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12650 value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
12651 was included, and *ATTACHMENT to point to the start of the annex
12652 if any. The length of the packet isn't needed here; there may
12653 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12655 Return 0 if the packet could be parsed, -1 if it could not. If
12656 -1 is returned, the other variables may not be initialized. */
12659 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12660 fileio_error
*remote_errno
, const char **attachment
)
12664 *remote_errno
= FILEIO_SUCCESS
;
12665 *attachment
= NULL
;
12667 if (buffer
[0] != 'F')
12671 *retcode
= strtol (&buffer
[1], &p
, 16);
12672 if (errno
!= 0 || p
== &buffer
[1])
12675 /* Check for ",errno". */
12679 *remote_errno
= (fileio_error
) strtol (p
+ 1, &p2
, 16);
12680 if (errno
!= 0 || p
+ 1 == p2
)
12685 /* Check for ";attachment". If there is no attachment, the
12686 packet should end here. */
12689 *attachment
= p
+ 1;
12692 else if (*p
== '\0')
12698 /* Send a prepared I/O packet to the target and read its response.
12699 The prepared packet is in the global RS->BUF before this function
12700 is called, and the answer is there when we return.
12702 COMMAND_BYTES is the length of the request to send, which may include
12703 binary data. WHICH_PACKET is the packet configuration to check
12704 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12705 is set to the error number and -1 is returned. Otherwise the value
12706 returned by the function is returned.
12708 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12709 attachment is expected; an error will be reported if there's a
12710 mismatch. If one is found, *ATTACHMENT will be set to point into
12711 the packet buffer and *ATTACHMENT_LEN will be set to the
12712 attachment's length. */
12715 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12716 fileio_error
*remote_errno
, const char **attachment
,
12717 int *attachment_len
)
12719 struct remote_state
*rs
= get_remote_state ();
12720 int ret
, bytes_read
;
12721 const char *attachment_tmp
;
12723 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
12725 *remote_errno
= FILEIO_ENOSYS
;
12729 putpkt_binary (rs
->buf
.data (), command_bytes
);
12730 bytes_read
= getpkt (&rs
->buf
);
12732 /* If it timed out, something is wrong. Don't try to parse the
12734 if (bytes_read
< 0)
12736 *remote_errno
= FILEIO_EINVAL
;
12740 switch (m_features
.packet_ok (rs
->buf
, which_packet
).status ())
12743 *remote_errno
= FILEIO_EINVAL
;
12745 case PACKET_UNKNOWN
:
12746 *remote_errno
= FILEIO_ENOSYS
;
12752 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12755 *remote_errno
= FILEIO_EINVAL
;
12759 if (*remote_errno
!= FILEIO_SUCCESS
)
12762 /* Make sure we saw an attachment if and only if we expected one. */
12763 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12764 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12766 *remote_errno
= FILEIO_EINVAL
;
12770 /* If an attachment was found, it must point into the packet buffer;
12771 work out how many bytes there were. */
12772 if (attachment_tmp
!= NULL
)
12774 *attachment
= attachment_tmp
;
12775 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12781 /* See declaration.h. */
12784 readahead_cache::invalidate ()
12789 /* See declaration.h. */
12792 readahead_cache::invalidate_fd (int fd
)
12794 if (this->fd
== fd
)
12798 /* Set the filesystem remote_hostio functions that take FILENAME
12799 arguments will use. Return 0 on success, or -1 if an error
12800 occurs (and set *REMOTE_ERRNO). */
12803 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12804 fileio_error
*remote_errno
)
12806 struct remote_state
*rs
= get_remote_state ();
12807 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12808 char *p
= rs
->buf
.data ();
12809 int left
= get_remote_packet_size () - 1;
12813 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12816 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12819 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12821 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12822 remote_buffer_add_string (&p
, &left
, arg
);
12824 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12825 remote_errno
, NULL
, NULL
);
12827 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12831 rs
->fs_pid
= required_pid
;
12836 /* Implementation of to_fileio_open. */
12839 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12840 int flags
, int mode
, int warn_if_slow
,
12841 fileio_error
*remote_errno
)
12843 struct remote_state
*rs
= get_remote_state ();
12844 char *p
= rs
->buf
.data ();
12845 int left
= get_remote_packet_size () - 1;
12847 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12850 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12852 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12853 strlen (filename
));
12854 remote_buffer_add_string (&p
, &left
, ",");
12856 remote_buffer_add_int (&p
, &left
, flags
);
12857 remote_buffer_add_string (&p
, &left
, ",");
12859 remote_buffer_add_int (&p
, &left
, mode
);
12861 int res
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12862 remote_errno
, nullptr, nullptr);
12864 if (warn_if_slow
&& res
!= -1)
12866 static int warning_issued
= 0;
12868 gdb_printf (_("Reading %ps from remote target...\n"),
12869 styled_string (file_name_style
.style (), filename
));
12871 if (!warning_issued
)
12873 warning (_("File transfers from remote targets can be slow."
12874 " Use \"%ps\" to access files locally"
12876 styled_string (command_style
.style (), "set sysroot"));
12877 warning_issued
= 1;
12885 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12886 int flags
, int mode
, int warn_if_slow
,
12887 fileio_error
*remote_errno
)
12889 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12893 /* Implementation of to_fileio_pwrite. */
12896 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12897 ULONGEST offset
, fileio_error
*remote_errno
)
12899 struct remote_state
*rs
= get_remote_state ();
12900 char *p
= rs
->buf
.data ();
12901 int left
= get_remote_packet_size ();
12904 rs
->readahead_cache
.invalidate_fd (fd
);
12906 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12908 remote_buffer_add_int (&p
, &left
, fd
);
12909 remote_buffer_add_string (&p
, &left
, ",");
12911 remote_buffer_add_int (&p
, &left
, offset
);
12912 remote_buffer_add_string (&p
, &left
, ",");
12914 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12915 (get_remote_packet_size ()
12916 - (p
- rs
->buf
.data ())));
12918 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12919 remote_errno
, NULL
, NULL
);
12923 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12924 ULONGEST offset
, fileio_error
*remote_errno
)
12926 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12929 /* Helper for the implementation of to_fileio_pread. Read the file
12930 from the remote side with vFile:pread. */
12933 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12934 ULONGEST offset
, fileio_error
*remote_errno
)
12936 struct remote_state
*rs
= get_remote_state ();
12937 char *p
= rs
->buf
.data ();
12938 const char *attachment
;
12939 int left
= get_remote_packet_size ();
12940 int ret
, attachment_len
;
12943 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12945 remote_buffer_add_int (&p
, &left
, fd
);
12946 remote_buffer_add_string (&p
, &left
, ",");
12948 remote_buffer_add_int (&p
, &left
, len
);
12949 remote_buffer_add_string (&p
, &left
, ",");
12951 remote_buffer_add_int (&p
, &left
, offset
);
12953 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12954 remote_errno
, &attachment
,
12960 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12962 if (read_len
!= ret
)
12963 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12968 /* See declaration.h. */
12971 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12975 && this->offset
<= offset
12976 && offset
< this->offset
+ this->buf
.size ())
12978 ULONGEST max
= this->offset
+ this->buf
.size ();
12980 if (offset
+ len
> max
)
12981 len
= max
- offset
;
12983 memcpy (read_buf
, &this->buf
[offset
- this->offset
], len
);
12990 /* Implementation of to_fileio_pread. */
12993 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12994 ULONGEST offset
, fileio_error
*remote_errno
)
12997 struct remote_state
*rs
= get_remote_state ();
12998 readahead_cache
*cache
= &rs
->readahead_cache
;
13000 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
13003 cache
->hit_count
++;
13005 remote_debug_printf ("readahead cache hit %s",
13006 pulongest (cache
->hit_count
));
13010 cache
->miss_count
++;
13012 remote_debug_printf ("readahead cache miss %s",
13013 pulongest (cache
->miss_count
));
13016 cache
->offset
= offset
;
13017 cache
->buf
.resize (get_remote_packet_size ());
13019 ret
= remote_hostio_pread_vFile (cache
->fd
, &cache
->buf
[0],
13020 cache
->buf
.size (),
13021 cache
->offset
, remote_errno
);
13024 cache
->invalidate_fd (fd
);
13028 cache
->buf
.resize (ret
);
13029 return cache
->pread (fd
, read_buf
, len
, offset
);
13033 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
13034 ULONGEST offset
, fileio_error
*remote_errno
)
13036 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
13039 /* Implementation of to_fileio_close. */
13042 remote_target::remote_hostio_close (int fd
, fileio_error
*remote_errno
)
13044 struct remote_state
*rs
= get_remote_state ();
13045 char *p
= rs
->buf
.data ();
13046 int left
= get_remote_packet_size () - 1;
13048 rs
->readahead_cache
.invalidate_fd (fd
);
13050 remote_buffer_add_string (&p
, &left
, "vFile:close:");
13052 remote_buffer_add_int (&p
, &left
, fd
);
13054 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
13055 remote_errno
, NULL
, NULL
);
13059 remote_target::fileio_close (int fd
, fileio_error
*remote_errno
)
13061 return remote_hostio_close (fd
, remote_errno
);
13064 /* Implementation of to_fileio_unlink. */
13067 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
13068 fileio_error
*remote_errno
)
13070 struct remote_state
*rs
= get_remote_state ();
13071 char *p
= rs
->buf
.data ();
13072 int left
= get_remote_packet_size () - 1;
13074 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
13077 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
13079 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
13080 strlen (filename
));
13082 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
13083 remote_errno
, NULL
, NULL
);
13087 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
13088 fileio_error
*remote_errno
)
13090 return remote_hostio_unlink (inf
, filename
, remote_errno
);
13093 /* Implementation of to_fileio_readlink. */
13095 std::optional
<std::string
>
13096 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
13097 fileio_error
*remote_errno
)
13099 struct remote_state
*rs
= get_remote_state ();
13100 char *p
= rs
->buf
.data ();
13101 const char *attachment
;
13102 int left
= get_remote_packet_size ();
13103 int len
, attachment_len
;
13106 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
13109 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
13111 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
13112 strlen (filename
));
13114 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
13115 remote_errno
, &attachment
,
13121 std::string
ret (len
, '\0');
13123 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
13124 (gdb_byte
*) &ret
[0], len
);
13125 if (read_len
!= len
)
13126 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
13131 /* Helper function to handle ::fileio_fstat and ::fileio_stat result
13132 processing. When this function is called the remote syscall has been
13133 performed and we know we didn't get an error back.
13135 ATTACHMENT and ATTACHMENT_LEN are the attachment data extracted from the
13136 remote syscall reply. EXPECTED_LEN is the length returned from the
13137 fstat or stat call, this the length of the returned data (in ATTACHMENT)
13138 once it has been decoded. The fstat/stat result (from the ATTACHMENT
13139 data) is to be placed in ST. */
13142 fileio_process_fstat_and_stat_reply (const char *attachment
,
13143 int attachment_len
,
13147 struct fio_stat fst
;
13150 = remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
13151 (gdb_byte
*) &fst
, sizeof (fst
));
13153 if (read_len
!= expected_len
)
13154 error (_("vFile:fstat returned %d, but %d bytes."),
13155 expected_len
, read_len
);
13157 if (read_len
!= sizeof (fst
))
13158 error (_("vFile:fstat returned %d bytes, but expecting %d."),
13159 read_len
, (int) sizeof (fst
));
13161 remote_fileio_to_host_stat (&fst
, st
);
13166 /* Implementation of to_fileio_fstat. */
13169 remote_target::fileio_fstat (int fd
, struct stat
*st
, fileio_error
*remote_errno
)
13171 struct remote_state
*rs
= get_remote_state ();
13172 char *p
= rs
->buf
.data ();
13173 int left
= get_remote_packet_size ();
13174 int attachment_len
, ret
;
13175 const char *attachment
;
13177 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
13179 remote_buffer_add_int (&p
, &left
, fd
);
13181 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
13182 remote_errno
, &attachment
,
13186 if (*remote_errno
!= FILEIO_ENOSYS
)
13189 /* Strictly we should return -1, ENOSYS here, but when
13190 "set sysroot remote:" was implemented in August 2008
13191 BFD's need for a stat function was sidestepped with
13192 this hack. This was not remedied until March 2015
13193 so we retain the previous behavior to avoid breaking
13196 Note that the memset is a March 2015 addition; older
13197 GDBs set st_size *and nothing else* so the structure
13198 would have garbage in all other fields. This might
13199 break something but retaining the previous behavior
13200 here would be just too wrong. */
13202 memset (st
, 0, sizeof (struct stat
));
13203 st
->st_size
= INT_MAX
;
13207 return fileio_process_fstat_and_stat_reply (attachment
, attachment_len
,
13211 /* Implementation of to_fileio_stat. */
13214 remote_target::fileio_stat (struct inferior
*inf
, const char *filename
,
13215 struct stat
*st
, fileio_error
*remote_errno
)
13217 struct remote_state
*rs
= get_remote_state ();
13218 char *p
= rs
->buf
.data ();
13219 int left
= get_remote_packet_size () - 1;
13221 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
13224 remote_buffer_add_string (&p
, &left
, "vFile:stat:");
13226 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
13227 strlen (filename
));
13229 int attachment_len
;
13230 const char *attachment
;
13231 int ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_stat
,
13232 remote_errno
, &attachment
,
13235 /* Unlike ::fileio_fstat, the stat fileio call was added later on, and
13236 has none of the legacy bfd issues, so we can just return the error. */
13240 return fileio_process_fstat_and_stat_reply (attachment
, attachment_len
,
13244 /* Implementation of to_filesystem_is_local. */
13247 remote_target::filesystem_is_local ()
13249 /* Valgrind GDB presents itself as a remote target but works
13250 on the local filesystem: it does not implement remote get
13251 and users are not expected to set a sysroot. To handle
13252 this case we treat the remote filesystem as local if the
13253 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
13254 does not support vFile:open. */
13255 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
13257 packet_support ps
= m_features
.packet_support (PACKET_vFile_open
);
13259 if (ps
== PACKET_SUPPORT_UNKNOWN
)
13262 fileio_error remote_errno
;
13264 /* Try opening a file to probe support. The supplied
13265 filename is irrelevant, we only care about whether
13266 the stub recognizes the packet or not. */
13267 fd
= remote_hostio_open (NULL
, "just probing",
13268 FILEIO_O_RDONLY
, 0700, 0,
13272 remote_hostio_close (fd
, &remote_errno
);
13274 ps
= m_features
.packet_support (PACKET_vFile_open
);
13277 if (ps
== PACKET_DISABLE
)
13279 static int warning_issued
= 0;
13281 if (!warning_issued
)
13283 warning (_("remote target does not support file"
13284 " transfer, attempting to access files"
13285 " from local filesystem."));
13286 warning_issued
= 1;
13297 remote_hostio_error (fileio_error errnum
)
13299 int host_error
= fileio_error_to_host (errnum
);
13301 if (host_error
== -1)
13302 error (_("Unknown remote I/O error %d"), errnum
);
13304 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
13307 /* A RAII wrapper around a remote file descriptor. */
13309 class scoped_remote_fd
13312 scoped_remote_fd (remote_target
*remote
, int fd
)
13313 : m_remote (remote
), m_fd (fd
)
13317 ~scoped_remote_fd ()
13323 fileio_error remote_errno
;
13324 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
13326 catch (const gdb_exception_quit
&ex
)
13328 /* We can't throw from a destructor, so re-set the quit flag
13329 for later QUIT checking. */
13332 catch (const gdb_exception_forced_quit
&ex
)
13334 /* Like above, but (eventually) cause GDB to terminate by
13335 setting sync_quit_force_run. */
13336 set_force_quit_flag ();
13340 /* Swallow exception before it escapes the dtor. If
13341 something goes wrong, likely the connection is gone,
13342 and there's nothing else that can be done. */
13347 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
13349 /* Release ownership of the file descriptor, and return it. */
13350 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
13357 /* Return the owned file descriptor. */
13358 int get () const noexcept
13364 /* The remote target. */
13365 remote_target
*m_remote
;
13367 /* The owned remote I/O file descriptor. */
13372 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
13374 remote_target
*remote
= get_current_remote_target ();
13376 if (remote
== nullptr)
13377 error (_("command can only be used with remote target"));
13379 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
13383 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
13386 int retcode
, bytes
, io_size
;
13387 fileio_error remote_errno
;
13388 int bytes_in_buffer
;
13392 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
13394 perror_with_name (local_file
);
13396 scoped_remote_fd fd
13397 (this, remote_hostio_open (NULL
,
13398 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
13400 0700, 0, &remote_errno
));
13401 if (fd
.get () == -1)
13402 remote_hostio_error (remote_errno
);
13404 /* Send up to this many bytes at once. They won't all fit in the
13405 remote packet limit, so we'll transfer slightly fewer. */
13406 io_size
= get_remote_packet_size ();
13407 gdb::byte_vector
buffer (io_size
);
13409 bytes_in_buffer
= 0;
13412 while (bytes_in_buffer
|| !saw_eof
)
13416 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
13417 io_size
- bytes_in_buffer
,
13421 if (ferror (file
.get ()))
13422 error (_("Error reading %s."), local_file
);
13425 /* EOF. Unless there is something still in the
13426 buffer from the last iteration, we are done. */
13428 if (bytes_in_buffer
== 0)
13436 bytes
+= bytes_in_buffer
;
13437 bytes_in_buffer
= 0;
13439 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
13440 offset
, &remote_errno
);
13443 remote_hostio_error (remote_errno
);
13444 else if (retcode
== 0)
13445 error (_("Remote write of %d bytes returned 0!"), bytes
);
13446 else if (retcode
< bytes
)
13448 /* Short write. Save the rest of the read data for the next
13450 bytes_in_buffer
= bytes
- retcode
;
13451 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
13457 if (remote_hostio_close (fd
.release (), &remote_errno
))
13458 remote_hostio_error (remote_errno
);
13461 gdb_printf (_("Successfully sent file \"%ps\".\n"),
13462 styled_string (file_name_style
.style (), local_file
));
13466 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
13468 remote_target
*remote
= get_current_remote_target ();
13470 if (remote
== nullptr)
13471 error (_("command can only be used with remote target"));
13473 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
13477 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
13480 fileio_error remote_errno
;
13481 int bytes
, io_size
;
13484 scoped_remote_fd fd
13485 (this, remote_hostio_open (NULL
,
13486 remote_file
, FILEIO_O_RDONLY
, 0, 0,
13488 if (fd
.get () == -1)
13489 remote_hostio_error (remote_errno
);
13491 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
13493 perror_with_name (local_file
);
13495 /* Send up to this many bytes at once. They won't all fit in the
13496 remote packet limit, so we'll transfer slightly fewer. */
13497 io_size
= get_remote_packet_size ();
13498 gdb::byte_vector
buffer (io_size
);
13503 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
13506 /* Success, but no bytes, means end-of-file. */
13509 remote_hostio_error (remote_errno
);
13513 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
13515 perror_with_name (local_file
);
13518 if (remote_hostio_close (fd
.release (), &remote_errno
))
13519 remote_hostio_error (remote_errno
);
13522 gdb_printf (_("Successfully fetched file \"%ps\".\n"),
13523 styled_string (file_name_style
.style (), remote_file
));
13527 remote_file_delete (const char *remote_file
, int from_tty
)
13529 remote_target
*remote
= get_current_remote_target ();
13531 if (remote
== nullptr)
13532 error (_("command can only be used with remote target"));
13534 remote
->remote_file_delete (remote_file
, from_tty
);
13538 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
13541 fileio_error remote_errno
;
13543 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
13545 remote_hostio_error (remote_errno
);
13548 gdb_printf (_("Successfully deleted file \"%ps\".\n"),
13549 styled_string (file_name_style
.style (), remote_file
));
13553 remote_put_command (const char *args
, int from_tty
)
13556 error_no_arg (_("file to put"));
13558 gdb_argv
argv (args
);
13559 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13560 error (_("Invalid parameters to remote put"));
13562 remote_file_put (argv
[0], argv
[1], from_tty
);
13566 remote_get_command (const char *args
, int from_tty
)
13569 error_no_arg (_("file to get"));
13571 gdb_argv
argv (args
);
13572 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13573 error (_("Invalid parameters to remote get"));
13575 remote_file_get (argv
[0], argv
[1], from_tty
);
13579 remote_delete_command (const char *args
, int from_tty
)
13582 error_no_arg (_("file to delete"));
13584 gdb_argv
argv (args
);
13585 if (argv
[0] == NULL
|| argv
[1] != NULL
)
13586 error (_("Invalid parameters to remote delete"));
13588 remote_file_delete (argv
[0], from_tty
);
13592 remote_target::can_execute_reverse ()
13594 if (m_features
.packet_support (PACKET_bs
) == PACKET_ENABLE
13595 || m_features
.packet_support (PACKET_bc
) == PACKET_ENABLE
)
13602 remote_target::supports_non_stop ()
13608 remote_target::supports_disable_randomization ()
13610 /* Only supported in extended mode. */
13615 remote_target::supports_multi_process ()
13617 return m_features
.remote_multi_process_p ();
13621 remote_target::remote_supports_cond_tracepoints ()
13623 return (m_features
.packet_support (PACKET_ConditionalTracepoints
)
13628 remote_target::supports_evaluation_of_breakpoint_conditions ()
13630 return (m_features
.packet_support (PACKET_ConditionalBreakpoints
)
13635 remote_target::remote_supports_fast_tracepoints ()
13637 return m_features
.packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13641 remote_target::remote_supports_static_tracepoints ()
13643 return m_features
.packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13647 remote_target::remote_supports_install_in_trace ()
13649 return m_features
.packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13653 remote_target::supports_enable_disable_tracepoint ()
13655 return (m_features
.packet_support (PACKET_EnableDisableTracepoints_feature
)
13660 remote_target::supports_string_tracing ()
13662 return m_features
.packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13666 remote_target::can_run_breakpoint_commands ()
13668 return m_features
.packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13672 remote_target::trace_init ()
13674 struct remote_state
*rs
= get_remote_state ();
13677 remote_get_noisy_reply ();
13678 if (strcmp (rs
->buf
.data (), "OK") != 0)
13679 error (_("Target does not support this command."));
13682 /* Recursive routine to walk through command list including loops, and
13683 download packets for each command. */
13686 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13687 struct command_line
*cmds
)
13689 struct remote_state
*rs
= get_remote_state ();
13690 struct command_line
*cmd
;
13692 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13694 QUIT
; /* Allow user to bail out with ^C. */
13695 strcpy (rs
->buf
.data (), "QTDPsrc:");
13696 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13697 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13698 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13700 remote_get_noisy_reply ();
13701 if (strcmp (rs
->buf
.data (), "OK"))
13702 warning (_("Target does not support source download."));
13704 if (cmd
->control_type
== while_control
13705 || cmd
->control_type
== while_stepping_control
)
13707 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13709 QUIT
; /* Allow user to bail out with ^C. */
13710 strcpy (rs
->buf
.data (), "QTDPsrc:");
13711 encode_source_string (num
, addr
, "cmd", "end",
13712 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13713 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13715 remote_get_noisy_reply ();
13716 if (strcmp (rs
->buf
.data (), "OK"))
13717 warning (_("Target does not support source download."));
13723 remote_target::download_tracepoint (struct bp_location
*loc
)
13727 std::vector
<std::string
> tdp_actions
;
13728 std::vector
<std::string
> stepping_actions
;
13730 struct breakpoint
*b
= loc
->owner
;
13731 tracepoint
*t
= gdb::checked_static_cast
<tracepoint
*> (b
);
13732 struct remote_state
*rs
= get_remote_state ();
13734 const char *err_msg
= _("Tracepoint packet too large for target.");
13737 /* We use a buffer other than rs->buf because we'll build strings
13738 across multiple statements, and other statements in between could
13740 gdb::char_vector
buf (get_remote_packet_size ());
13742 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13744 tpaddr
= loc
->address
;
13745 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13746 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13747 b
->number
, addrbuf
, /* address */
13748 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13749 t
->step_count
, t
->pass_count
);
13751 if (ret
< 0 || ret
>= buf
.size ())
13752 error ("%s", err_msg
);
13754 /* Fast tracepoints are mostly handled by the target, but we can
13755 tell the target how big of an instruction block should be moved
13757 if (b
->type
== bp_fast_tracepoint
)
13759 /* Only test for support at download time; we may not know
13760 target capabilities at definition time. */
13761 if (remote_supports_fast_tracepoints ())
13763 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13766 size_left
= buf
.size () - strlen (buf
.data ());
13767 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13769 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13771 if (ret
< 0 || ret
>= size_left
)
13772 error ("%s", err_msg
);
13775 /* If it passed validation at definition but fails now,
13776 something is very wrong. */
13777 internal_error (_("Fast tracepoint not valid during download"));
13780 /* Fast tracepoints are functionally identical to regular
13781 tracepoints, so don't take lack of support as a reason to
13782 give up on the trace run. */
13783 warning (_("Target does not support fast tracepoints, "
13784 "downloading %d as regular tracepoint"), b
->number
);
13786 else if (b
->type
== bp_static_tracepoint
13787 || b
->type
== bp_static_marker_tracepoint
)
13789 /* Only test for support at download time; we may not know
13790 target capabilities at definition time. */
13791 if (remote_supports_static_tracepoints ())
13793 struct static_tracepoint_marker marker
;
13795 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13797 size_left
= buf
.size () - strlen (buf
.data ());
13798 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13801 if (ret
< 0 || ret
>= size_left
)
13802 error ("%s", err_msg
);
13805 error (_("Static tracepoint not valid during download"));
13808 /* Fast tracepoints are functionally identical to regular
13809 tracepoints, so don't take lack of support as a reason
13810 to give up on the trace run. */
13811 error (_("Target does not support static tracepoints"));
13813 /* If the tracepoint has a conditional, make it into an agent
13814 expression and append to the definition. */
13817 /* Only test support at download time, we may not know target
13818 capabilities at definition time. */
13819 if (remote_supports_cond_tracepoints ())
13821 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13824 size_left
= buf
.size () - strlen (buf
.data ());
13826 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13827 size_left
, ":X%x,", (int) aexpr
->buf
.size ());
13829 if (ret
< 0 || ret
>= size_left
)
13830 error ("%s", err_msg
);
13832 size_left
= buf
.size () - strlen (buf
.data ());
13834 /* Two bytes to encode each aexpr byte, plus the terminating
13836 if (aexpr
->buf
.size () * 2 + 1 > size_left
)
13837 error ("%s", err_msg
);
13839 pkt
= buf
.data () + strlen (buf
.data ());
13841 for (int ndx
= 0; ndx
< aexpr
->buf
.size (); ++ndx
)
13842 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13846 warning (_("Target does not support conditional tracepoints, "
13847 "ignoring tp %d cond"), b
->number
);
13850 if (b
->commands
|| !default_collect
.empty ())
13852 size_left
= buf
.size () - strlen (buf
.data ());
13854 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13857 if (ret
< 0 || ret
>= size_left
)
13858 error ("%s", err_msg
);
13861 putpkt (buf
.data ());
13862 remote_get_noisy_reply ();
13863 if (strcmp (rs
->buf
.data (), "OK"))
13864 error (_("Target does not support tracepoints."));
13866 /* do_single_steps (t); */
13867 for (auto action_it
= tdp_actions
.begin ();
13868 action_it
!= tdp_actions
.end (); action_it
++)
13870 QUIT
; /* Allow user to bail out with ^C. */
13872 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13873 || !stepping_actions
.empty ());
13875 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13876 b
->number
, addrbuf
, /* address */
13877 action_it
->c_str (),
13878 has_more
? '-' : 0);
13880 if (ret
< 0 || ret
>= buf
.size ())
13881 error ("%s", err_msg
);
13883 putpkt (buf
.data ());
13884 remote_get_noisy_reply ();
13885 if (strcmp (rs
->buf
.data (), "OK"))
13886 error (_("Error on target while setting tracepoints."));
13889 for (auto action_it
= stepping_actions
.begin ();
13890 action_it
!= stepping_actions
.end (); action_it
++)
13892 QUIT
; /* Allow user to bail out with ^C. */
13894 bool is_first
= action_it
== stepping_actions
.begin ();
13895 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13897 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13898 b
->number
, addrbuf
, /* address */
13899 is_first
? "S" : "",
13900 action_it
->c_str (),
13901 has_more
? "-" : "");
13903 if (ret
< 0 || ret
>= buf
.size ())
13904 error ("%s", err_msg
);
13906 putpkt (buf
.data ());
13907 remote_get_noisy_reply ();
13908 if (strcmp (rs
->buf
.data (), "OK"))
13909 error (_("Error on target while setting tracepoints."));
13912 if (m_features
.packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13914 if (b
->locspec
!= nullptr)
13916 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13918 if (ret
< 0 || ret
>= buf
.size ())
13919 error ("%s", err_msg
);
13921 const char *str
= b
->locspec
->to_string ();
13922 encode_source_string (b
->number
, loc
->address
, "at", str
,
13923 buf
.data () + strlen (buf
.data ()),
13924 buf
.size () - strlen (buf
.data ()));
13925 putpkt (buf
.data ());
13926 remote_get_noisy_reply ();
13927 if (strcmp (rs
->buf
.data (), "OK"))
13928 warning (_("Target does not support source download."));
13930 if (b
->cond_string
)
13932 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13934 if (ret
< 0 || ret
>= buf
.size ())
13935 error ("%s", err_msg
);
13937 encode_source_string (b
->number
, loc
->address
,
13938 "cond", b
->cond_string
.get (),
13939 buf
.data () + strlen (buf
.data ()),
13940 buf
.size () - strlen (buf
.data ()));
13941 putpkt (buf
.data ());
13942 remote_get_noisy_reply ();
13943 if (strcmp (rs
->buf
.data (), "OK"))
13944 warning (_("Target does not support source download."));
13946 remote_download_command_source (b
->number
, loc
->address
,
13947 breakpoint_commands (b
));
13952 remote_target::can_download_tracepoint ()
13954 struct remote_state
*rs
= get_remote_state ();
13955 struct trace_status
*ts
;
13958 /* Don't try to install tracepoints until we've relocated our
13959 symbols, and fetched and merged the target's tracepoint list with
13961 if (rs
->starting_up
)
13964 ts
= current_trace_status ();
13965 status
= get_trace_status (ts
);
13967 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13970 /* If we are in a tracing experiment, but remote stub doesn't support
13971 installing tracepoint in trace, we have to return. */
13972 if (!remote_supports_install_in_trace ())
13980 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13982 struct remote_state
*rs
= get_remote_state ();
13985 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13986 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13988 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13989 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13990 >= get_remote_packet_size ())
13991 error (_("Trace state variable name too long for tsv definition packet"));
13992 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13995 remote_get_noisy_reply ();
13996 if (rs
->buf
[0] == '\0')
13997 error (_("Target does not support this command."));
13998 if (strcmp (rs
->buf
.data (), "OK") != 0)
13999 error (_("Error on target while downloading trace state variable."));
14003 remote_target::enable_tracepoint (struct bp_location
*location
)
14005 struct remote_state
*rs
= get_remote_state ();
14007 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
14008 location
->owner
->number
,
14009 phex (location
->address
, sizeof (CORE_ADDR
)));
14011 remote_get_noisy_reply ();
14012 if (rs
->buf
[0] == '\0')
14013 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
14014 if (strcmp (rs
->buf
.data (), "OK") != 0)
14015 error (_("Error on target while enabling tracepoint."));
14019 remote_target::disable_tracepoint (struct bp_location
*location
)
14021 struct remote_state
*rs
= get_remote_state ();
14023 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
14024 location
->owner
->number
,
14025 phex (location
->address
, sizeof (CORE_ADDR
)));
14027 remote_get_noisy_reply ();
14028 if (rs
->buf
[0] == '\0')
14029 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
14030 if (strcmp (rs
->buf
.data (), "OK") != 0)
14031 error (_("Error on target while disabling tracepoint."));
14035 remote_target::trace_set_readonly_regions ()
14038 bfd_size_type size
;
14042 bfd
*abfd
= current_program_space
->exec_bfd ();
14045 return; /* No information to give. */
14047 struct remote_state
*rs
= get_remote_state ();
14049 strcpy (rs
->buf
.data (), "QTro");
14050 offset
= strlen (rs
->buf
.data ());
14051 for (s
= abfd
->sections
; s
; s
= s
->next
)
14053 char tmp1
[40], tmp2
[40];
14056 if ((s
->flags
& SEC_LOAD
) == 0
14057 /* || (s->flags & SEC_CODE) == 0 */
14058 || (s
->flags
& SEC_READONLY
) == 0)
14062 vma
= bfd_section_vma (s
);
14063 size
= bfd_section_size (s
);
14064 bfd_sprintf_vma (abfd
, tmp1
, vma
);
14065 bfd_sprintf_vma (abfd
, tmp2
, vma
+ size
);
14066 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
14067 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
14069 if (m_features
.packet_support (PACKET_qXfer_traceframe_info
)
14072 Too many sections for read-only sections definition packet."));
14075 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
14077 offset
+= sec_length
;
14087 remote_target::trace_start ()
14089 struct remote_state
*rs
= get_remote_state ();
14091 putpkt ("QTStart");
14092 remote_get_noisy_reply ();
14093 if (rs
->buf
[0] == '\0')
14094 error (_("Target does not support this command."));
14095 if (strcmp (rs
->buf
.data (), "OK") != 0)
14096 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
14100 remote_target::get_trace_status (struct trace_status
*ts
)
14102 /* Initialize it just to avoid a GCC false warning. */
14104 struct remote_state
*rs
= get_remote_state ();
14106 if (m_features
.packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
14109 /* FIXME we need to get register block size some other way. */
14110 trace_regblock_size
14111 = rs
->get_remote_arch_state (current_inferior ()->arch ())->sizeof_g_packet
;
14113 putpkt ("qTStatus");
14117 p
= remote_get_noisy_reply ();
14119 catch (const gdb_exception_error
&ex
)
14121 if (ex
.error
!= TARGET_CLOSE_ERROR
)
14123 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
14129 packet_result result
= m_features
.packet_ok (p
, PACKET_qTStatus
);
14131 switch (result
.status ())
14134 error (_("Remote failure reply: %s"), result
.err_msg ());
14135 /* If the remote target doesn't do tracing, flag it. */
14136 case PACKET_UNKNOWN
:
14140 /* We're working with a live target. */
14141 ts
->filename
= NULL
;
14144 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
14146 /* Function 'parse_trace_status' sets default value of each field of
14147 'ts' at first, so we don't have to do it here. */
14148 parse_trace_status (p
, ts
);
14150 return ts
->running
;
14154 remote_target::get_tracepoint_status (tracepoint
*tp
,
14155 struct uploaded_tp
*utp
)
14157 struct remote_state
*rs
= get_remote_state ();
14159 size_t size
= get_remote_packet_size ();
14164 tp
->traceframe_usage
= 0;
14165 for (bp_location
&loc
: tp
->locations ())
14167 /* If the tracepoint was never downloaded, don't go asking for
14169 if (tp
->number_on_target
== 0)
14171 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
14172 phex_nz (loc
.address
, 0));
14174 reply
= remote_get_noisy_reply ();
14175 if (reply
&& *reply
)
14178 parse_tracepoint_status (reply
+ 1, tp
, utp
);
14184 utp
->hit_count
= 0;
14185 utp
->traceframe_usage
= 0;
14186 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
14187 phex_nz (utp
->addr
, 0));
14189 reply
= remote_get_noisy_reply ();
14190 if (reply
&& *reply
)
14193 parse_tracepoint_status (reply
+ 1, tp
, utp
);
14199 remote_target::trace_stop ()
14201 struct remote_state
*rs
= get_remote_state ();
14204 remote_get_noisy_reply ();
14205 if (rs
->buf
[0] == '\0')
14206 error (_("Target does not support this command."));
14207 if (strcmp (rs
->buf
.data (), "OK") != 0)
14208 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
14212 remote_target::trace_find (enum trace_find_type type
, int num
,
14213 CORE_ADDR addr1
, CORE_ADDR addr2
,
14216 struct remote_state
*rs
= get_remote_state ();
14217 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
14219 int target_frameno
= -1, target_tracept
= -1;
14221 /* Lookups other than by absolute frame number depend on the current
14222 trace selected, so make sure it is correct on the remote end
14224 if (type
!= tfind_number
)
14225 set_remote_traceframe ();
14227 p
= rs
->buf
.data ();
14228 strcpy (p
, "QTFrame:");
14229 p
= strchr (p
, '\0');
14233 xsnprintf (p
, endbuf
- p
, "%x", num
);
14236 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
14239 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
14242 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
14243 phex_nz (addr2
, 0));
14245 case tfind_outside
:
14246 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
14247 phex_nz (addr2
, 0));
14250 error (_("Unknown trace find type %d"), type
);
14254 reply
= remote_get_noisy_reply ();
14255 if (*reply
== '\0')
14256 error (_("Target does not support this command."));
14258 while (reply
&& *reply
)
14263 target_frameno
= (int) strtol (p
, &reply
, 16);
14265 error (_("Unable to parse trace frame number"));
14266 /* Don't update our remote traceframe number cache on failure
14267 to select a remote traceframe. */
14268 if (target_frameno
== -1)
14273 target_tracept
= (int) strtol (p
, &reply
, 16);
14275 error (_("Unable to parse tracepoint number"));
14277 case 'O': /* "OK"? */
14278 if (reply
[1] == 'K' && reply
[2] == '\0')
14281 error (_("Bogus reply from target: %s"), reply
);
14284 error (_("Bogus reply from target: %s"), reply
);
14287 *tpp
= target_tracept
;
14289 rs
->remote_traceframe_number
= target_frameno
;
14290 return target_frameno
;
14294 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
14296 struct remote_state
*rs
= get_remote_state ();
14300 set_remote_traceframe ();
14302 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
14304 reply
= remote_get_noisy_reply ();
14305 if (reply
&& *reply
)
14309 unpack_varlen_hex (reply
+ 1, &uval
);
14310 *val
= (LONGEST
) uval
;
14318 remote_target::save_trace_data (const char *filename
)
14320 struct remote_state
*rs
= get_remote_state ();
14323 p
= rs
->buf
.data ();
14324 strcpy (p
, "QTSave:");
14326 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
14327 >= get_remote_packet_size ())
14328 error (_("Remote file name too long for trace save packet"));
14329 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
14332 reply
= remote_get_noisy_reply ();
14333 if (*reply
== '\0')
14334 error (_("Target does not support this command."));
14335 if (strcmp (reply
, "OK") != 0)
14336 error (_("Bogus reply from target: %s"), reply
);
14340 /* This is basically a memory transfer, but needs to be its own packet
14341 because we don't know how the target actually organizes its trace
14342 memory, plus we want to be able to ask for as much as possible, but
14343 not be unhappy if we don't get as much as we ask for. */
14346 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
14348 struct remote_state
*rs
= get_remote_state ();
14353 p
= rs
->buf
.data ();
14354 strcpy (p
, "qTBuffer:");
14356 p
+= hexnumstr (p
, offset
);
14358 p
+= hexnumstr (p
, len
);
14362 reply
= remote_get_noisy_reply ();
14363 if (reply
&& *reply
)
14365 /* 'l' by itself means we're at the end of the buffer and
14366 there is nothing more to get. */
14370 /* Convert the reply into binary. Limit the number of bytes to
14371 convert according to our passed-in buffer size, rather than
14372 what was returned in the packet; if the target is
14373 unexpectedly generous and gives us a bigger reply than we
14374 asked for, we don't want to crash. */
14375 rslt
= hex2bin (reply
, buf
, len
);
14379 /* Something went wrong, flag as an error. */
14384 remote_target::set_disconnected_tracing (int val
)
14386 struct remote_state
*rs
= get_remote_state ();
14388 if (m_features
.packet_support (PACKET_DisconnectedTracing_feature
)
14393 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
14394 "QTDisconnected:%x", val
);
14396 reply
= remote_get_noisy_reply ();
14397 if (*reply
== '\0')
14398 error (_("Target does not support this command."));
14399 if (strcmp (reply
, "OK") != 0)
14400 error (_("Bogus reply from target: %s"), reply
);
14403 warning (_("Target does not support disconnected tracing."));
14407 remote_target::core_of_thread (ptid_t ptid
)
14409 thread_info
*info
= this->find_thread (ptid
);
14411 if (info
!= NULL
&& info
->priv
!= NULL
)
14412 return get_remote_thread_info (info
)->core
;
14418 remote_target::set_circular_trace_buffer (int val
)
14420 struct remote_state
*rs
= get_remote_state ();
14423 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
14424 "QTBuffer:circular:%x", val
);
14426 reply
= remote_get_noisy_reply ();
14427 if (*reply
== '\0')
14428 error (_("Target does not support this command."));
14429 if (strcmp (reply
, "OK") != 0)
14430 error (_("Bogus reply from target: %s"), reply
);
14434 remote_target::traceframe_info ()
14436 std::optional
<gdb::char_vector
> text
14437 = target_read_stralloc (current_inferior ()->top_target (),
14438 TARGET_OBJECT_TRACEFRAME_INFO
,
14441 return parse_traceframe_info (text
->data ());
14446 /* Handle the qTMinFTPILen packet. Returns the minimum length of
14447 instruction on which a fast tracepoint may be placed. Returns -1
14448 if the packet is not supported, and 0 if the minimum instruction
14449 length is unknown. */
14452 remote_target::get_min_fast_tracepoint_insn_len ()
14454 struct remote_state
*rs
= get_remote_state ();
14457 /* If we're not debugging a process yet, the IPA can't be
14459 if (!target_has_execution ())
14462 /* Make sure the remote is pointing at the right process. */
14463 set_general_process ();
14465 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
14467 reply
= remote_get_noisy_reply ();
14468 if (*reply
== '\0')
14472 ULONGEST min_insn_len
;
14474 unpack_varlen_hex (reply
, &min_insn_len
);
14476 return (int) min_insn_len
;
14481 remote_target::set_trace_buffer_size (LONGEST val
)
14483 if (m_features
.packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
14485 struct remote_state
*rs
= get_remote_state ();
14486 char *buf
= rs
->buf
.data ();
14487 char *endbuf
= buf
+ get_remote_packet_size ();
14489 gdb_assert (val
>= 0 || val
== -1);
14490 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
14491 /* Send -1 as literal "-1" to avoid host size dependency. */
14495 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
14498 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
14501 remote_get_noisy_reply ();
14502 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_QTBuffer_size
);
14503 switch (result
.status ())
14506 warning (_("Error reply from target: %s"), result
.err_msg ());
14508 case PACKET_UNKNOWN
:
14509 warning (_("Remote target failed to process the request "));
14515 remote_target::set_trace_notes (const char *user
, const char *notes
,
14516 const char *stop_notes
)
14518 struct remote_state
*rs
= get_remote_state ();
14520 char *buf
= rs
->buf
.data ();
14521 char *endbuf
= buf
+ get_remote_packet_size ();
14524 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
14527 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
14528 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
14534 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
14535 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
14541 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
14542 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
14546 /* Ensure the buffer is terminated. */
14550 reply
= remote_get_noisy_reply ();
14551 if (*reply
== '\0')
14554 if (strcmp (reply
, "OK") != 0)
14555 error (_("Bogus reply from target: %s"), reply
);
14561 remote_target::use_agent (bool use
)
14563 if (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
14565 struct remote_state
*rs
= get_remote_state ();
14567 /* If the stub supports QAgent. */
14568 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
14572 if (strcmp (rs
->buf
.data (), "OK") == 0)
14583 remote_target::can_use_agent ()
14585 return (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
14588 #if defined (HAVE_LIBEXPAT)
14590 /* Check the btrace document version. */
14593 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
14594 const struct gdb_xml_element
*element
,
14596 std::vector
<gdb_xml_value
> &attributes
)
14598 const char *version
14599 = (const char *) xml_find_attribute (attributes
, "version")->value
.get ();
14601 if (strcmp (version
, "1.0") != 0)
14602 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
14605 /* Parse a btrace "block" xml record. */
14608 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
14609 const struct gdb_xml_element
*element
,
14611 std::vector
<gdb_xml_value
> &attributes
)
14613 struct btrace_data
*btrace
;
14614 ULONGEST
*begin
, *end
;
14616 btrace
= (struct btrace_data
*) user_data
;
14618 switch (btrace
->format
)
14620 case BTRACE_FORMAT_BTS
:
14623 case BTRACE_FORMAT_NONE
:
14624 btrace
->format
= BTRACE_FORMAT_BTS
;
14625 btrace
->variant
.bts
.blocks
= new std::vector
<btrace_block
>;
14629 gdb_xml_error (parser
, _("Btrace format error."));
14632 begin
= (ULONGEST
*) xml_find_attribute (attributes
, "begin")->value
.get ();
14633 end
= (ULONGEST
*) xml_find_attribute (attributes
, "end")->value
.get ();
14634 btrace
->variant
.bts
.blocks
->emplace_back (*begin
, *end
);
14637 /* Parse a "raw" xml record. */
14640 parse_xml_raw (struct gdb_xml_parser
*parser
, const char *body_text
,
14641 gdb_byte
**pdata
, size_t *psize
)
14646 len
= strlen (body_text
);
14648 gdb_xml_error (parser
, _("Bad raw data size."));
14652 gdb::unique_xmalloc_ptr
<gdb_byte
> data ((gdb_byte
*) xmalloc (size
));
14655 /* We use hex encoding - see gdbsupport/rsp-low.h. */
14663 if (hi
== 0 || lo
== 0)
14664 gdb_xml_error (parser
, _("Bad hex encoding."));
14666 *bin
++ = fromhex (hi
) * 16 + fromhex (lo
);
14670 *pdata
= data
.release ();
14674 /* Parse a btrace pt-config "cpu" xml record. */
14677 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser
*parser
,
14678 const struct gdb_xml_element
*element
,
14680 std::vector
<gdb_xml_value
> &attributes
)
14682 struct btrace_data
*btrace
;
14683 const char *vendor
;
14684 ULONGEST
*family
, *model
, *stepping
;
14687 = (const char *) xml_find_attribute (attributes
, "vendor")->value
.get ();
14689 = (ULONGEST
*) xml_find_attribute (attributes
, "family")->value
.get ();
14691 = (ULONGEST
*) xml_find_attribute (attributes
, "model")->value
.get ();
14693 = (ULONGEST
*) xml_find_attribute (attributes
, "stepping")->value
.get ();
14695 btrace
= (struct btrace_data
*) user_data
;
14697 if (strcmp (vendor
, "GenuineIntel") == 0)
14698 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_INTEL
;
14700 btrace
->variant
.pt
.config
.cpu
.family
= *family
;
14701 btrace
->variant
.pt
.config
.cpu
.model
= *model
;
14702 btrace
->variant
.pt
.config
.cpu
.stepping
= *stepping
;
14705 /* Parse a btrace pt "raw" xml record. */
14708 parse_xml_btrace_pt_raw (struct gdb_xml_parser
*parser
,
14709 const struct gdb_xml_element
*element
,
14710 void *user_data
, const char *body_text
)
14712 struct btrace_data
*btrace
;
14714 btrace
= (struct btrace_data
*) user_data
;
14715 parse_xml_raw (parser
, body_text
, &btrace
->variant
.pt
.data
,
14716 &btrace
->variant
.pt
.size
);
14719 /* Parse a btrace "pt" xml record. */
14722 parse_xml_btrace_pt (struct gdb_xml_parser
*parser
,
14723 const struct gdb_xml_element
*element
,
14725 std::vector
<gdb_xml_value
> &attributes
)
14727 struct btrace_data
*btrace
;
14729 btrace
= (struct btrace_data
*) user_data
;
14730 btrace
->format
= BTRACE_FORMAT_PT
;
14731 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_UNKNOWN
;
14732 btrace
->variant
.pt
.data
= NULL
;
14733 btrace
->variant
.pt
.size
= 0;
14736 static const struct gdb_xml_attribute block_attributes
[] = {
14737 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14738 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14739 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14742 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes
[] = {
14743 { "vendor", GDB_XML_AF_NONE
, NULL
, NULL
},
14744 { "family", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14745 { "model", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14746 { "stepping", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14747 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14750 static const struct gdb_xml_element btrace_pt_config_children
[] = {
14751 { "cpu", btrace_pt_config_cpu_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14752 parse_xml_btrace_pt_config_cpu
, NULL
},
14753 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14756 static const struct gdb_xml_element btrace_pt_children
[] = {
14757 { "pt-config", NULL
, btrace_pt_config_children
, GDB_XML_EF_OPTIONAL
, NULL
,
14759 { "raw", NULL
, NULL
, GDB_XML_EF_OPTIONAL
, NULL
, parse_xml_btrace_pt_raw
},
14760 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14763 static const struct gdb_xml_attribute btrace_attributes
[] = {
14764 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
14765 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14768 static const struct gdb_xml_element btrace_children
[] = {
14769 { "block", block_attributes
, NULL
,
14770 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
14771 { "pt", NULL
, btrace_pt_children
, GDB_XML_EF_OPTIONAL
, parse_xml_btrace_pt
,
14773 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14776 static const struct gdb_xml_element btrace_elements
[] = {
14777 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
14778 check_xml_btrace_version
, NULL
},
14779 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14782 #endif /* defined (HAVE_LIBEXPAT) */
14784 /* Parse a branch trace xml document XML into DATA. */
14787 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
14789 #if defined (HAVE_LIBEXPAT)
14792 btrace_data result
;
14793 result
.format
= BTRACE_FORMAT_NONE
;
14795 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
14798 error (_("Error parsing branch trace."));
14800 /* Keep parse results. */
14801 *btrace
= std::move (result
);
14803 #else /* !defined (HAVE_LIBEXPAT) */
14805 error (_("Cannot process branch trace. XML support was disabled at "
14808 #endif /* !defined (HAVE_LIBEXPAT) */
14811 #if defined (HAVE_LIBEXPAT)
14813 /* Parse a btrace-conf "bts" xml record. */
14816 parse_xml_btrace_conf_bts (struct gdb_xml_parser
*parser
,
14817 const struct gdb_xml_element
*element
,
14819 std::vector
<gdb_xml_value
> &attributes
)
14821 struct btrace_config
*conf
;
14822 struct gdb_xml_value
*size
;
14824 conf
= (struct btrace_config
*) user_data
;
14825 conf
->format
= BTRACE_FORMAT_BTS
;
14826 conf
->bts
.size
= 0;
14828 size
= xml_find_attribute (attributes
, "size");
14830 conf
->bts
.size
= (unsigned int) *(ULONGEST
*) size
->value
.get ();
14833 /* Parse a btrace-conf "pt" xml record. */
14836 parse_xml_btrace_conf_pt (struct gdb_xml_parser
*parser
,
14837 const struct gdb_xml_element
*element
,
14839 std::vector
<gdb_xml_value
> &attributes
)
14841 struct btrace_config
*conf
;
14842 struct gdb_xml_value
*size
, *ptwrite
, *event_tracing
;
14844 conf
= (struct btrace_config
*) user_data
;
14845 conf
->format
= BTRACE_FORMAT_PT
;
14848 size
= xml_find_attribute (attributes
, "size");
14850 conf
->pt
.size
= (unsigned int) *(ULONGEST
*) size
->value
.get ();
14852 ptwrite
= xml_find_attribute (attributes
, "ptwrite");
14853 if (ptwrite
!= nullptr)
14854 conf
->pt
.ptwrite
= (bool) *(ULONGEST
*) ptwrite
->value
.get ();
14856 event_tracing
= xml_find_attribute (attributes
, "event-tracing");
14857 if (event_tracing
!= nullptr)
14858 conf
->pt
.event_tracing
= (bool) *(ULONGEST
*) event_tracing
->value
.get ();
14861 static const struct gdb_xml_attribute btrace_conf_pt_attributes
[] = {
14862 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
14863 { "ptwrite", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_enum
,
14864 gdb_xml_enums_boolean
},
14865 { "event-tracing", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_enum
,
14866 gdb_xml_enums_boolean
},
14867 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14870 static const struct gdb_xml_attribute btrace_conf_bts_attributes
[] = {
14871 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
14872 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14875 static const struct gdb_xml_element btrace_conf_children
[] = {
14876 { "bts", btrace_conf_bts_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14877 parse_xml_btrace_conf_bts
, NULL
},
14878 { "pt", btrace_conf_pt_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14879 parse_xml_btrace_conf_pt
, NULL
},
14880 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14883 static const struct gdb_xml_attribute btrace_conf_attributes
[] = {
14884 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
14885 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14888 static const struct gdb_xml_element btrace_conf_elements
[] = {
14889 { "btrace-conf", btrace_conf_attributes
, btrace_conf_children
,
14890 GDB_XML_EF_NONE
, NULL
, NULL
},
14891 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14894 #endif /* defined (HAVE_LIBEXPAT) */
14896 /* Parse a branch trace configuration xml document XML into CONF. */
14899 parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
)
14901 #if defined (HAVE_LIBEXPAT)
14904 errcode
= gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
14905 btrace_conf_elements
, xml
, conf
);
14907 error (_("Error parsing branch trace configuration."));
14909 #else /* !defined (HAVE_LIBEXPAT) */
14911 error (_("Cannot process the branch trace configuration. XML support "
14912 "was disabled at compile time."));
14914 #endif /* !defined (HAVE_LIBEXPAT) */
14917 /* Reset our idea of our target's btrace configuration. */
14920 remote_btrace_reset (remote_state
*rs
)
14922 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14925 /* Synchronize the configuration with the target. */
14928 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14930 struct remote_state
*rs
;
14931 char *buf
, *pos
, *endbuf
;
14933 rs
= get_remote_state ();
14934 buf
= rs
->buf
.data ();
14935 endbuf
= buf
+ get_remote_packet_size ();
14937 if (m_features
.packet_support (PACKET_Qbtrace_conf_bts_size
) == PACKET_ENABLE
14938 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14941 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14942 packets_descriptions
[PACKET_Qbtrace_conf_bts_size
].name
,
14948 packet_result result
= m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_bts_size
);
14949 if (result
.status () == PACKET_ERROR
)
14950 error (_("Failed to configure the BTS buffer size: %s"), result
.err_msg ());
14952 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14955 if (m_features
.packet_support (PACKET_Qbtrace_conf_pt_size
) == PACKET_ENABLE
14956 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14959 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14960 packets_descriptions
[PACKET_Qbtrace_conf_pt_size
].name
,
14966 packet_result result
= m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_size
);
14967 if (result
.status () == PACKET_ERROR
)
14968 error (_("Failed to configure the trace buffer size: %s"), result
.err_msg ());
14970 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14973 if ((m_features
.packet_support (PACKET_Qbtrace_conf_pt_ptwrite
)
14975 && conf
->pt
.ptwrite
!= rs
->btrace_config
.pt
.ptwrite
)
14978 const char *ptw
= conf
->pt
.ptwrite
? "yes" : "no";
14980 = packets_descriptions
[PACKET_Qbtrace_conf_pt_ptwrite
].name
;
14981 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=\"%s\"", name
, ptw
);
14984 getpkt (&rs
->buf
, 0);
14986 packet_result result
14987 = m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_ptwrite
);
14988 if (result
.status () == PACKET_ERROR
)
14990 if (buf
[0] == 'E' && buf
[1] == '.')
14991 error (_("Failed to sync ptwrite config: %s"), buf
+ 2);
14993 error (_("Failed to sync ptwrite config."));
14996 rs
->btrace_config
.pt
.ptwrite
= conf
->pt
.ptwrite
;
14999 /* Event tracing is a user setting, warn if it is set but the target
15000 doesn't support it. */
15001 if ((m_features
.packet_support (PACKET_Qbtrace_conf_pt_event_tracing
)
15003 && conf
->pt
.event_tracing
)
15004 warning (_("Target does not support event-tracing."));
15006 if ((m_features
.packet_support (PACKET_Qbtrace_conf_pt_event_tracing
)
15008 && conf
->pt
.event_tracing
!= rs
->btrace_config
.pt
.event_tracing
)
15011 const char *event_tracing
= conf
->pt
.event_tracing
? "yes" : "no";
15013 = packets_descriptions
[PACKET_Qbtrace_conf_pt_event_tracing
].name
;
15014 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=\"%s\"", name
, event_tracing
);
15017 getpkt (&rs
->buf
, 0);
15019 packet_result result
15020 = m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_event_tracing
);
15021 if (result
.status () == PACKET_ERROR
)
15023 if (buf
[0] == 'E' && buf
[1] == '.')
15024 error (_("Failed to sync event-tracing config: %s"), buf
+ 2);
15026 error (_("Failed to sync event-tracing config."));
15029 rs
->btrace_config
.pt
.event_tracing
= conf
->pt
.event_tracing
;
15033 /* Read TP's btrace configuration from the target and store it into CONF. */
15036 btrace_read_config (thread_info
*tp
, btrace_config
*conf
)
15038 /* target_read_stralloc relies on INFERIOR_PTID. */
15039 scoped_restore_current_thread restore_thread
;
15040 switch_to_thread (tp
);
15042 std::optional
<gdb::char_vector
> xml
15043 = target_read_stralloc (current_inferior ()->top_target (),
15044 TARGET_OBJECT_BTRACE_CONF
, "");
15046 parse_xml_btrace_conf (conf
, xml
->data ());
15049 /* Maybe reopen target btrace. */
15052 remote_target::remote_btrace_maybe_reopen ()
15054 struct remote_state
*rs
= get_remote_state ();
15055 int btrace_target_pushed
= 0;
15056 #if !defined (HAVE_LIBIPT)
15060 /* Don't bother walking the entirety of the remote thread list when
15061 we know the feature isn't supported by the remote. */
15062 if (m_features
.packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
15065 for (thread_info
*tp
: all_non_exited_threads (this))
15067 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
15068 btrace_read_config (tp
, &rs
->btrace_config
);
15070 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
15073 #if !defined (HAVE_LIBIPT)
15074 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
15079 warning (_("Target is recording using Intel Processor Trace "
15080 "but support was disabled at compile time."));
15085 #endif /* !defined (HAVE_LIBIPT) */
15087 /* Push target, once, but before anything else happens. This way our
15088 changes to the threads will be cleaned up by unpushing the target
15089 in case btrace_read_config () throws. */
15090 if (!btrace_target_pushed
)
15092 btrace_target_pushed
= 1;
15093 record_btrace_push_target ();
15094 gdb_printf (_("Target is recording using %s.\n"),
15095 btrace_format_string (rs
->btrace_config
.format
));
15099 = new btrace_target_info
{ tp
->ptid
, rs
->btrace_config
};
15103 /* Enable branch tracing. */
15105 struct btrace_target_info
*
15106 remote_target::enable_btrace (thread_info
*tp
,
15107 const struct btrace_config
*conf
)
15109 struct packet_config
*packet
= NULL
;
15110 struct remote_state
*rs
= get_remote_state ();
15111 char *buf
= rs
->buf
.data ();
15112 char *endbuf
= buf
+ get_remote_packet_size ();
15114 unsigned int which_packet
;
15115 switch (conf
->format
)
15117 case BTRACE_FORMAT_BTS
:
15118 which_packet
= PACKET_Qbtrace_bts
;
15120 case BTRACE_FORMAT_PT
:
15121 which_packet
= PACKET_Qbtrace_pt
;
15124 internal_error (_("Bad branch btrace format: %u."),
15125 (unsigned int) conf
->format
);
15128 packet
= &m_features
.m_protocol_packets
[which_packet
];
15129 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
15130 error (_("Target does not support branch tracing."));
15132 btrace_sync_conf (conf
);
15134 ptid_t ptid
= tp
->ptid
;
15135 set_general_thread (ptid
);
15137 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
15138 packets_descriptions
[which_packet
].name
);
15142 packet_result result
= m_features
.packet_ok (rs
->buf
, which_packet
);
15143 if (result
.status () == PACKET_ERROR
)
15144 error (_("Could not enable branch tracing for %s: %s"),
15145 target_pid_to_str (ptid
).c_str (), result
.err_msg ());
15147 btrace_target_info
*tinfo
= new btrace_target_info
{ ptid
};
15149 /* If we fail to read the configuration, we lose some information, but the
15150 tracing itself is not impacted. */
15153 btrace_read_config (tp
, &tinfo
->conf
);
15155 catch (const gdb_exception_error
&err
)
15157 if (err
.message
!= NULL
)
15158 warning ("%s", err
.what ());
15164 /* Disable branch tracing. */
15167 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
15169 struct remote_state
*rs
= get_remote_state ();
15170 char *buf
= rs
->buf
.data ();
15171 char *endbuf
= buf
+ get_remote_packet_size ();
15173 if (m_features
.packet_support (PACKET_Qbtrace_off
) != PACKET_ENABLE
)
15174 error (_("Target does not support branch tracing."));
15176 set_general_thread (tinfo
->ptid
);
15178 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
15179 packets_descriptions
[PACKET_Qbtrace_off
].name
);
15183 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_Qbtrace_off
);
15184 if (result
.status () == PACKET_ERROR
)
15185 error (_("Could not disable branch tracing for %s: %s"),
15186 target_pid_to_str (tinfo
->ptid
).c_str (), result
.err_msg ());
15191 /* Teardown branch tracing. */
15194 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
15196 /* We must not talk to the target during teardown. */
15200 /* Read the branch trace. */
15203 remote_target::read_btrace (struct btrace_data
*btrace
,
15204 struct btrace_target_info
*tinfo
,
15205 enum btrace_read_type type
)
15209 if (m_features
.packet_support (PACKET_qXfer_btrace
) != PACKET_ENABLE
)
15210 error (_("Target does not support branch tracing."));
15212 #if !defined(HAVE_LIBEXPAT)
15213 error (_("Cannot process branch tracing result. XML parsing not supported."));
15218 case BTRACE_READ_ALL
:
15221 case BTRACE_READ_NEW
:
15224 case BTRACE_READ_DELTA
:
15228 internal_error (_("Bad branch tracing read type: %u."),
15229 (unsigned int) type
);
15232 std::optional
<gdb::char_vector
> xml
15233 = target_read_stralloc (current_inferior ()->top_target (),
15234 TARGET_OBJECT_BTRACE
, annex
);
15236 return BTRACE_ERR_UNKNOWN
;
15238 parse_xml_btrace (btrace
, xml
->data ());
15240 return BTRACE_ERR_NONE
;
15243 const struct btrace_config
*
15244 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
15246 return &tinfo
->conf
;
15250 remote_target::augmented_libraries_svr4_read ()
15253 (m_features
.packet_support (PACKET_augmented_libraries_svr4_read_feature
)
15257 /* Implementation of to_load. */
15260 remote_target::load (const char *name
, int from_tty
)
15262 generic_load (name
, from_tty
);
15265 /* Accepts an integer PID; returns a string representing a file that
15266 can be opened on the remote side to get the symbols for the child
15267 process. Returns NULL if the operation is not supported. */
15270 remote_target::pid_to_exec_file (int pid
)
15272 static std::optional
<gdb::char_vector
> filename
;
15273 char *annex
= NULL
;
15275 if (m_features
.packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
15278 inferior
*inf
= find_inferior_pid (this, pid
);
15280 internal_error (_("not currently attached to process %d"), pid
);
15282 if (!inf
->fake_pid_p
)
15284 const int annex_size
= 9;
15286 annex
= (char *) alloca (annex_size
);
15287 xsnprintf (annex
, annex_size
, "%x", pid
);
15290 filename
= target_read_stralloc (current_inferior ()->top_target (),
15291 TARGET_OBJECT_EXEC_FILE
, annex
);
15293 return filename
? filename
->data () : nullptr;
15296 /* Implement the to_can_do_single_step target_ops method. */
15299 remote_target::can_do_single_step ()
15301 /* We can only tell whether target supports single step or not by
15302 supported s and S vCont actions if the stub supports vContSupported
15303 feature. If the stub doesn't support vContSupported feature,
15304 we have conservatively to think target doesn't supports single
15306 if (m_features
.packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
15308 struct remote_state
*rs
= get_remote_state ();
15310 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
15316 /* Implementation of the to_execution_direction method for the remote
15319 enum exec_direction_kind
15320 remote_target::execution_direction ()
15322 struct remote_state
*rs
= get_remote_state ();
15324 return rs
->last_resume_exec_dir
;
15327 /* Return pointer to the thread_info struct which corresponds to
15328 THREAD_HANDLE (having length HANDLE_LEN). */
15331 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
15335 for (thread_info
*tp
: all_non_exited_threads (this))
15337 remote_thread_info
*priv
= get_remote_thread_info (tp
);
15339 if (tp
->inf
== inf
&& priv
!= NULL
)
15341 if (handle_len
!= priv
->thread_handle
.size ())
15342 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
15343 handle_len
, priv
->thread_handle
.size ());
15344 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
15353 gdb::array_view
<const gdb_byte
>
15354 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
15356 remote_thread_info
*priv
= get_remote_thread_info (tp
);
15357 return priv
->thread_handle
;
15361 remote_target::can_async_p ()
15363 /* This flag should be checked in the common target.c code. */
15364 gdb_assert (target_async_permitted
);
15366 /* We're async whenever the serial device can. */
15367 return get_remote_state ()->can_async_p ();
15371 remote_target::is_async_p ()
15373 /* We're async whenever the serial device is. */
15374 return get_remote_state ()->is_async_p ();
15377 /* Pass the SERIAL event on and up to the client. One day this code
15378 will be able to delay notifying the client of an event until the
15379 point where an entire packet has been received. */
15381 static serial_event_ftype remote_async_serial_handler
;
15384 remote_async_serial_handler (struct serial
*scb
, void *context
)
15386 /* Don't propagate error information up to the client. Instead let
15387 the client find out about the error by querying the target. */
15388 inferior_event_handler (INF_REG_EVENT
);
15392 remote_target::async_wait_fd ()
15394 struct remote_state
*rs
= get_remote_state ();
15395 return rs
->remote_desc
->fd
;
15399 remote_target::async (bool enable
)
15401 struct remote_state
*rs
= get_remote_state ();
15405 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
15407 /* If there are pending events in the stop reply queue tell the
15408 event loop to process them. */
15409 if (!rs
->stop_reply_queue
.empty ())
15410 rs
->mark_async_event_handler ();
15412 /* For simplicity, below we clear the pending events token
15413 without remembering whether it is marked, so here we always
15414 mark it. If there's actually no pending notification to
15415 process, this ends up being a no-op (other than a spurious
15416 event-loop wakeup). */
15417 if (target_is_non_stop_p ())
15418 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
15422 serial_async (rs
->remote_desc
, NULL
, NULL
);
15423 /* If the core is disabling async, it doesn't want to be
15424 disturbed with target events. Clear all async event sources
15426 rs
->clear_async_event_handler ();
15428 if (target_is_non_stop_p ())
15429 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
15433 /* Implementation of the to_thread_events method. */
15436 remote_target::thread_events (bool enable
)
15438 struct remote_state
*rs
= get_remote_state ();
15439 size_t size
= get_remote_packet_size ();
15441 if (m_features
.packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
15444 if (rs
->last_thread_events
== enable
)
15447 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
15451 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_QThreadEvents
);
15452 switch (result
.status ())
15455 if (strcmp (rs
->buf
.data (), "OK") != 0)
15456 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
15457 rs
->last_thread_events
= enable
;
15460 warning (_("Remote failure reply: %s"), result
.err_msg ());
15462 case PACKET_UNKNOWN
:
15467 /* Implementation of the supports_set_thread_options target
15471 remote_target::supports_set_thread_options (gdb_thread_options options
)
15473 remote_state
*rs
= get_remote_state ();
15474 return (m_features
.packet_support (PACKET_QThreadOptions
) == PACKET_ENABLE
15475 && (rs
->supported_thread_options
& options
) == options
);
15478 /* For coalescing reasons, actually sending the options to the target
15479 happens at resume time, via this function. See target_resume for
15480 all-stop, and target_commit_resumed for non-stop. */
15483 remote_target::commit_requested_thread_options ()
15485 struct remote_state
*rs
= get_remote_state ();
15487 if (m_features
.packet_support (PACKET_QThreadOptions
) != PACKET_ENABLE
)
15490 char *p
= rs
->buf
.data ();
15491 char *endp
= p
+ get_remote_packet_size ();
15493 /* Clear options for all threads by default. Note that unlike
15494 vCont, the rightmost options that match a thread apply, so we
15495 don't have to worry about whether we can use wildcard ptids. */
15496 strcpy (p
, "QThreadOptions;0");
15499 /* Send the QThreadOptions packet stored in P. */
15500 auto flush
= [&] ()
15505 getpkt (&rs
->buf
, 0);
15507 packet_result result
= m_features
.packet_ok (rs
->buf
, PACKET_QThreadOptions
);
15508 switch (result
.status ())
15511 if (strcmp (rs
->buf
.data (), "OK") != 0)
15512 error (_("Remote refused setting thread options: %s"), rs
->buf
.data ());
15515 error (_("Remote failure reply: %s"), result
.err_msg ());
15516 case PACKET_UNKNOWN
:
15517 gdb_assert_not_reached ("PACKET_UNKNOWN");
15522 /* Prepare P for another QThreadOptions packet. */
15523 auto restart
= [&] ()
15525 p
= rs
->buf
.data ();
15526 strcpy (p
, "QThreadOptions");
15530 /* Now set non-zero options for threads that need them. We don't
15531 bother with the case of all threads of a process wanting the same
15532 non-zero options as that's not an expected scenario. */
15533 for (thread_info
*tp
: all_non_exited_threads (this))
15535 gdb_thread_options options
= tp
->thread_options ();
15540 /* It might be possible to we have more threads with options
15541 than can fit a single QThreadOptions packet. So build each
15542 options/thread pair in this separate buffer to make sure it
15544 constexpr size_t max_options_size
= 100;
15545 char obuf
[max_options_size
];
15546 char *obuf_p
= obuf
;
15547 char *obuf_endp
= obuf
+ max_options_size
;
15550 obuf_p
+= xsnprintf (obuf_p
, obuf_endp
- obuf_p
, "%s",
15551 phex_nz (options
, sizeof (options
)));
15552 if (tp
->ptid
!= magic_null_ptid
)
15555 obuf_p
= write_ptid (obuf_p
, obuf_endp
, tp
->ptid
);
15558 size_t osize
= obuf_p
- obuf
;
15559 if (osize
> endp
- p
)
15561 /* This new options/thread pair doesn't fit the packet
15562 buffer. Send what we have already. */
15566 /* Should now fit. */
15567 gdb_assert (osize
<= endp
- p
);
15570 memcpy (p
, obuf
, osize
);
15578 show_remote_cmd (const char *args
, int from_tty
)
15580 /* We can't just use cmd_show_list here, because we want to skip
15581 the redundant "show remote Z-packet" and the legacy aliases. */
15582 struct cmd_list_element
*list
= remote_show_cmdlist
;
15583 struct ui_out
*uiout
= current_uiout
;
15585 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
15586 for (; list
!= NULL
; list
= list
->next
)
15587 if (strcmp (list
->name
, "Z-packet") == 0)
15589 else if (list
->type
== not_set_cmd
)
15590 /* Alias commands are exactly like the original, except they
15591 don't have the normal type. */
15595 ui_out_emit_tuple
option_emitter (uiout
, "option");
15597 uiout
->field_string ("name", list
->name
);
15598 uiout
->text (": ");
15599 if (list
->type
== show_cmd
)
15600 do_show_command (NULL
, from_tty
, list
);
15602 cmd_func (list
, NULL
, from_tty
);
15606 /* Some change happened in PSPACE's objfile list (obfiles added or removed),
15607 offer all inferiors using that program space a change to look up symbols. */
15610 remote_objfile_changed_check_symbols (program_space
*pspace
)
15612 /* The affected program space is possibly shared by multiple inferiors.
15613 Consider sending a qSymbol packet for each of the inferiors using that
15615 for (inferior
*inf
: all_inferiors ())
15617 if (inf
->pspace
!= pspace
)
15620 /* Check whether the inferior's process target is a remote target. */
15621 remote_target
*remote
= as_remote_target (inf
->process_target ());
15622 if (remote
== nullptr)
15625 /* When we are attaching or handling a fork child and the shared library
15626 subsystem reads the list of loaded libraries, we receive new objfile
15627 events in between each found library. The libraries are read in an
15628 undefined order, so if we gave the remote side a chance to look up
15629 symbols between each objfile, we might give it an inconsistent picture
15630 of the inferior. It could appear that a library A appears loaded but
15631 a library B does not, even though library A requires library B. That
15632 would present a state that couldn't normally exist in the inferior.
15634 So, skip these events, we'll give the remote a chance to look up
15635 symbols once all the loaded libraries and their symbols are known to
15637 if (inf
->in_initial_library_scan
)
15640 if (!remote
->has_execution (inf
))
15643 /* Need to switch to a specific thread, because remote_check_symbols will
15644 set the general thread using INFERIOR_PTID.
15646 It's possible to have inferiors with no thread here, because we are
15647 called very early in the connection process, while the inferior is
15648 being set up, before threads are added. Just skip it, start_remote_1
15649 also calls remote_check_symbols when it's done setting things up. */
15650 thread_info
*thread
= any_thread_of_inferior (inf
);
15651 if (thread
!= nullptr)
15653 scoped_restore_current_thread restore_thread
;
15654 switch_to_thread (thread
);
15655 remote
->remote_check_symbols ();
15660 /* Function to be called whenever a new objfile (shlib) is detected. */
15663 remote_new_objfile (struct objfile
*objfile
)
15665 remote_objfile_changed_check_symbols (objfile
->pspace ());
15668 /* Pull all the tracepoints defined on the target and create local
15669 data structures representing them. We don't want to create real
15670 tracepoints yet, we don't want to mess up the user's existing
15674 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
15676 struct remote_state
*rs
= get_remote_state ();
15679 /* Ask for a first packet of tracepoint definition. */
15682 p
= rs
->buf
.data ();
15683 while (*p
&& *p
!= 'l')
15685 parse_tracepoint_definition (p
, utpp
);
15686 /* Ask for another packet of tracepoint definition. */
15689 p
= rs
->buf
.data ();
15695 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
15697 struct remote_state
*rs
= get_remote_state ();
15700 /* Ask for a first packet of variable definition. */
15703 p
= rs
->buf
.data ();
15704 while (*p
&& *p
!= 'l')
15706 parse_tsv_definition (p
, utsvp
);
15707 /* Ask for another packet of variable definition. */
15710 p
= rs
->buf
.data ();
15715 /* The "set/show range-stepping" show hook. */
15718 show_range_stepping (struct ui_file
*file
, int from_tty
,
15719 struct cmd_list_element
*c
,
15723 _("Debugger's willingness to use range stepping "
15724 "is %s.\n"), value
);
15727 /* Return true if the vCont;r action is supported by the remote
15731 remote_target::vcont_r_supported ()
15733 return (m_features
.packet_support (PACKET_vCont
) == PACKET_ENABLE
15734 && get_remote_state ()->supports_vCont
.r
);
15737 /* The "set/show range-stepping" set hook. */
15740 set_range_stepping (const char *ignore_args
, int from_tty
,
15741 struct cmd_list_element
*c
)
15743 /* When enabling, check whether range stepping is actually supported
15744 by the target, and warn if not. */
15745 if (use_range_stepping
)
15747 remote_target
*remote
= get_current_remote_target ();
15749 || !remote
->vcont_r_supported ())
15750 warning (_("Range stepping is not supported by the current target"));
15755 show_remote_debug (struct ui_file
*file
, int from_tty
,
15756 struct cmd_list_element
*c
, const char *value
)
15758 gdb_printf (file
, _("Debugging of remote protocol is %s.\n"),
15763 show_remote_timeout (struct ui_file
*file
, int from_tty
,
15764 struct cmd_list_element
*c
, const char *value
)
15767 _("Timeout limit to wait for target to respond is %s.\n"),
15771 /* Implement the "supports_memory_tagging" target_ops method. */
15774 remote_target::supports_memory_tagging ()
15776 return m_features
.remote_memory_tagging_p ();
15779 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
15782 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
15783 size_t len
, int type
)
15785 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
15787 std::string request
= string_printf ("qMemTags:%s,%s:%s",
15788 phex_nz (address
, addr_size
),
15789 phex_nz (len
, sizeof (len
)),
15790 phex_nz (type
, sizeof (type
)));
15792 strcpy (packet
.data (), request
.c_str ());
15795 /* Parse the qMemTags packet reply into TAGS.
15797 Return true if successful, false otherwise. */
15800 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
15801 gdb::byte_vector
&tags
)
15803 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
15806 /* Copy the tag data. */
15807 tags
= hex2bin (reply
.data () + 1);
15812 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
15815 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
15816 size_t len
, int type
,
15817 const gdb::byte_vector
&tags
)
15819 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
15821 /* Put together the main packet, address and length. */
15822 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
15823 phex_nz (address
, addr_size
),
15824 phex_nz (len
, sizeof (len
)),
15825 phex_nz (type
, sizeof (type
)));
15826 request
+= bin2hex (tags
.data (), tags
.size ());
15828 /* Check if we have exceeded the maximum packet size. */
15829 if (packet
.size () < request
.length ())
15830 error (_("Contents too big for packet QMemTags."));
15832 strcpy (packet
.data (), request
.c_str ());
15836 create_is_address_tagged_request (gdbarch
*gdbarch
, gdb::char_vector
&packet
,
15840 std::string request
;
15842 addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
15843 request
= string_printf ("qIsAddressTagged:%s", phex_nz (address
, addr_size
));
15845 if (packet
.size () < request
.length () + 1)
15846 error (_("Contents too big for packet qIsAddressTagged."));
15848 strcpy (packet
.data (), request
.c_str ());
15852 check_is_address_tagged_reply (remote_target
*remote
, gdb::char_vector
&packet
,
15855 gdb_assert (remote
!= nullptr);
15856 /* Check reply and disable qIsAddressTagged usage if it's not supported. */
15857 packet_result result
= remote
->m_features
.packet_ok (packet
,
15858 PACKET_qIsAddressTagged
);
15860 /* Return false on error (Exx), empty reply (packet not supported), or reply
15861 size doesn't match 2 hex digits. */
15862 if ((result
.status () != PACKET_OK
) || (strlen (packet
.data ()) != 2))
15866 /* Convert only 2 hex digits, i.e. 1 byte in hex format. */
15867 hex2bin (packet
.data (), &reply
, 1);
15869 if (reply
== 0x00 || reply
== 0x01)
15875 /* Invalid reply. */
15879 /* Implement the "fetch_memtags" target_ops method. */
15882 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
15883 gdb::byte_vector
&tags
, int type
)
15885 /* Make sure the qMemTags packet is supported. */
15886 if (!m_features
.remote_memory_tagging_p ())
15887 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
15889 struct remote_state
*rs
= get_remote_state ();
15891 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
15896 return parse_fetch_memtags_reply (rs
->buf
, tags
);
15899 /* Implement the "store_memtags" target_ops method. */
15902 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
15903 const gdb::byte_vector
&tags
, int type
)
15905 /* Make sure the QMemTags packet is supported. */
15906 if (!m_features
.remote_memory_tagging_p ())
15907 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
15909 struct remote_state
*rs
= get_remote_state ();
15911 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
15916 /* Verify if the request was successful. */
15917 return packet_check_result (rs
->buf
).status () == PACKET_OK
;
15920 /* Implement the "is_address_tagged" target_ops method. */
15923 remote_target::is_address_tagged (gdbarch
*gdbarch
, CORE_ADDR address
)
15925 /* Firstly, attempt to check the address using the qIsAddressTagged
15927 if (m_features
.packet_support (PACKET_qIsAddressTagged
) != PACKET_DISABLE
)
15929 remote_target
*remote
= get_current_remote_target ();
15930 struct remote_state
*rs
= get_remote_state ();
15931 bool is_addr_tagged
;
15933 create_is_address_tagged_request (gdbarch
, rs
->buf
, address
);
15938 /* If qIsAddressTagged is not supported PACKET_qIsAddressTagged will be
15939 set to PACKET_DISABLE so no further attempt is made to check addresses
15940 using this packet and the fallback mechanism below will be used
15941 instead. Also, if the check fails due to an error (Exx reply) the
15942 fallback is used too. Otherwise, the qIsAddressTagged query succeeded
15943 and is_addr_tagged is valid. */
15944 if (check_is_address_tagged_reply (remote
, rs
->buf
, is_addr_tagged
))
15945 return is_addr_tagged
;
15948 /* Fallback to arch-specific method of checking whether an address is tagged
15949 in case check via qIsAddressTagged fails. */
15950 return gdbarch_tagged_address_p (gdbarch
, address
);
15953 /* Return true if remote target T is non-stop. */
15956 remote_target_is_non_stop_p (remote_target
*t
)
15958 scoped_restore_current_thread restore_thread
;
15959 switch_to_target_no_thread (t
);
15961 return target_is_non_stop_p ();
15966 namespace selftests
{
15969 test_memory_tagging_functions ()
15971 remote_target remote
;
15973 struct packet_config
*config
15974 = &remote
.m_features
.m_protocol_packets
[PACKET_memory_tagging_feature
];
15976 scoped_restore restore_memtag_support_
15977 = make_scoped_restore (&config
->support
);
15979 struct gdbarch
*gdbarch
= current_inferior ()->arch ();
15981 /* Test memory tagging packet support. */
15982 config
->support
= PACKET_SUPPORT_UNKNOWN
;
15983 SELF_CHECK (remote
.supports_memory_tagging () == false);
15984 config
->support
= PACKET_DISABLE
;
15985 SELF_CHECK (remote
.supports_memory_tagging () == false);
15986 config
->support
= PACKET_ENABLE
;
15987 SELF_CHECK (remote
.supports_memory_tagging () == true);
15989 /* Setup testing. */
15990 gdb::char_vector packet
;
15991 gdb::byte_vector tags
, bv
;
15992 std::string expected
, reply
;
15993 packet
.resize (32000);
15995 /* Test creating a qMemTags request. */
15997 expected
= "qMemTags:0,0:0";
15998 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
15999 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
16001 expected
= "qMemTags:deadbeef,10:1";
16002 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
16003 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
16005 /* Test parsing a qMemTags reply. */
16007 /* Error reply, tags vector unmodified. */
16009 strcpy (packet
.data (), reply
.c_str ());
16011 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
16012 SELF_CHECK (tags
.size () == 0);
16014 /* Valid reply, tags vector updated. */
16018 for (int i
= 0; i
< 5; i
++)
16021 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
16022 strcpy (packet
.data (), reply
.c_str ());
16024 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
16025 SELF_CHECK (tags
.size () == 5);
16027 for (int i
= 0; i
< 5; i
++)
16028 SELF_CHECK (tags
[i
] == i
);
16030 /* Test creating a QMemTags request. */
16032 /* Empty tag data. */
16034 expected
= "QMemTags:0,0:0:";
16035 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
16036 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
16037 expected
.length ()) == 0);
16039 /* Non-empty tag data. */
16041 for (int i
= 0; i
< 5; i
++)
16042 tags
.push_back (i
);
16043 expected
= "QMemTags:deadbeef,ff:1:0001020304";
16044 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
16045 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
16046 expected
.length ()) == 0);
16048 /* Test creating a qIsAddressTagged request. */
16049 expected
= "qIsAddressTagged:deadbeef";
16050 create_is_address_tagged_request (gdbarch
, packet
, 0xdeadbeef);
16051 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
16053 /* Test error reply on qIsAddressTagged request. */
16055 strcpy (packet
.data (), reply
.c_str ());
16056 /* is_tagged must not change, hence it's tested too. */
16057 bool is_tagged
= false;
16058 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16060 SELF_CHECK (is_tagged
== false);
16062 /* Test 'tagged' as reply. */
16064 strcpy (packet
.data (), reply
.c_str ());
16065 /* Because the byte is 01, is_tagged should be set to true. */
16067 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16069 SELF_CHECK (is_tagged
== true);
16071 /* Test 'not tagged' as reply. */
16073 strcpy (packet
.data (), reply
.c_str ());
16074 /* Because the byte is 00, is_tagged should be set to false. */
16076 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16078 SELF_CHECK (is_tagged
== false);
16080 /* Test an invalid reply (neither 00 nor 01). */
16082 strcpy (packet
.data (), reply
.c_str ());
16083 /* Because the byte is invalid is_tagged must not change. */
16085 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16087 SELF_CHECK (is_tagged
== false);
16089 /* Test malformed reply of incorrect length. */
16090 reply
= "0104A590001234006";
16091 strcpy (packet
.data (), reply
.c_str ());
16092 /* Because this is a malformed reply is_tagged must not change. */
16094 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16096 SELF_CHECK (is_tagged
== false);
16098 /* Test empty reply. */
16100 strcpy (packet
.data (), reply
.c_str ());
16101 /* is_tagged must not change, hence it's tested too. */
16103 /* On the previous tests, qIsAddressTagged packet was auto detected and set
16104 as supported. But an empty reply means the packet is unsupported, so for
16105 testing the empty reply the support is reset to unknown state, otherwise
16106 packet_ok will complain. */
16107 remote
.m_features
.m_protocol_packets
[PACKET_qIsAddressTagged
].support
=
16108 PACKET_SUPPORT_UNKNOWN
;
16109 SELF_CHECK (check_is_address_tagged_reply (&remote
, packet
, is_tagged
) ==
16111 SELF_CHECK (is_tagged
== true);
16115 test_packet_check_result ()
16117 std::string buf
= "E.msg";
16118 packet_result result
= packet_check_result (buf
.data ());
16120 SELF_CHECK (result
.status () == PACKET_ERROR
);
16121 SELF_CHECK (strcmp(result
.err_msg (), "msg") == 0);
16123 result
= packet_check_result ("E01");
16124 SELF_CHECK (result
.status () == PACKET_ERROR
);
16125 SELF_CHECK (strcmp(result
.err_msg (), "01") == 0);
16127 SELF_CHECK (packet_check_result ("E1").status () == PACKET_OK
);
16129 SELF_CHECK (packet_check_result ("E000").status () == PACKET_OK
);
16131 result
= packet_check_result ("E.");
16132 SELF_CHECK (result
.status () == PACKET_ERROR
);
16133 SELF_CHECK (strcmp(result
.err_msg (), "no error provided") == 0);
16135 SELF_CHECK (packet_check_result ("some response").status () == PACKET_OK
);
16137 SELF_CHECK (packet_check_result ("").status () == PACKET_UNKNOWN
);
16139 } // namespace selftests
16140 #endif /* GDB_SELF_TEST */
16142 void _initialize_remote ();
16144 _initialize_remote ()
16146 add_target (remote_target_info
, remote_target::open
);
16147 add_target (extended_remote_target_info
, extended_remote_target::open
);
16149 /* Hook into new objfile notification. */
16150 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
16151 gdb::observers::all_objfiles_removed
.attach
16152 (remote_objfile_changed_check_symbols
, "remote");
16155 init_remote_threadtests ();
16158 /* set/show remote ... */
16160 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
16161 Remote protocol specific variables.\n\
16162 Configure various remote-protocol specific variables such as\n\
16163 the packets being used."),
16164 &remote_set_cmdlist
,
16165 0 /* allow-unknown */, &setlist
);
16166 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
16167 Remote protocol specific variables.\n\
16168 Configure various remote-protocol specific variables such as\n\
16169 the packets being used."),
16170 &remote_show_cmdlist
,
16171 0 /* allow-unknown */, &showlist
);
16173 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
16174 Compare section data on target to the exec file.\n\
16175 Argument is a single section name (default: all loaded sections).\n\
16176 To compare only read-only loaded sections, specify the -r option."),
16179 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
16180 Send an arbitrary packet to a remote target.\n\
16181 maintenance packet TEXT\n\
16182 If GDB is talking to an inferior via the GDB serial protocol, then\n\
16183 this command sends the string TEXT to the inferior, and displays the\n\
16184 response packet. GDB supplies the initial `$' character, and the\n\
16185 terminating `#' character and checksum."),
16188 set_show_commands remotebreak_cmds
16189 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
16190 Set whether to send break if interrupted."), _("\
16191 Show whether to send break if interrupted."), _("\
16192 If set, a break, instead of a cntrl-c, is sent to the remote target."),
16193 set_remotebreak
, show_remotebreak
,
16194 &setlist
, &showlist
);
16195 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
16196 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
16198 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
16199 interrupt_sequence_modes
, &interrupt_sequence_mode
,
16201 Set interrupt sequence to remote target."), _("\
16202 Show interrupt sequence to remote target."), _("\
16203 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
16204 NULL
, show_interrupt_sequence
,
16205 &remote_set_cmdlist
,
16206 &remote_show_cmdlist
);
16208 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
16209 &interrupt_on_connect
, _("\
16210 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
16211 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
16212 If set, interrupt sequence is sent to remote target."),
16214 &remote_set_cmdlist
, &remote_show_cmdlist
);
16216 /* Install commands for configuring memory read/write packets. */
16218 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
16219 Set the maximum number of bytes per memory write packet (deprecated)."),
16221 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
16222 Show the maximum number of bytes per memory write packet (deprecated)."),
16224 add_cmd ("memory-write-packet-size", no_class
,
16225 set_memory_write_packet_size
, _("\
16226 Set the maximum number of bytes per memory-write packet.\n\
16227 Specify the number of bytes in a packet or 0 (zero) for the\n\
16228 default packet size. The actual limit is further reduced\n\
16229 dependent on the target. Specify \"fixed\" to disable the\n\
16230 further restriction and \"limit\" to enable that restriction."),
16231 &remote_set_cmdlist
);
16232 add_cmd ("memory-read-packet-size", no_class
,
16233 set_memory_read_packet_size
, _("\
16234 Set the maximum number of bytes per memory-read packet.\n\
16235 Specify the number of bytes in a packet or 0 (zero) for the\n\
16236 default packet size. The actual limit is further reduced\n\
16237 dependent on the target. Specify \"fixed\" to disable the\n\
16238 further restriction and \"limit\" to enable that restriction."),
16239 &remote_set_cmdlist
);
16240 add_cmd ("memory-write-packet-size", no_class
,
16241 show_memory_write_packet_size
,
16242 _("Show the maximum number of bytes per memory-write packet."),
16243 &remote_show_cmdlist
);
16244 add_cmd ("memory-read-packet-size", no_class
,
16245 show_memory_read_packet_size
,
16246 _("Show the maximum number of bytes per memory-read packet."),
16247 &remote_show_cmdlist
);
16249 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
16250 &remote_hw_watchpoint_limit
, _("\
16251 Set the maximum number of target hardware watchpoints."), _("\
16252 Show the maximum number of target hardware watchpoints."), _("\
16253 Specify \"unlimited\" for unlimited hardware watchpoints."),
16254 NULL
, show_hardware_watchpoint_limit
,
16255 &remote_set_cmdlist
,
16256 &remote_show_cmdlist
);
16257 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
16259 &remote_hw_watchpoint_length_limit
, _("\
16260 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
16261 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
16262 Specify \"unlimited\" to allow watchpoints of unlimited size."),
16263 NULL
, show_hardware_watchpoint_length_limit
,
16264 &remote_set_cmdlist
, &remote_show_cmdlist
);
16265 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
16266 &remote_hw_breakpoint_limit
, _("\
16267 Set the maximum number of target hardware breakpoints."), _("\
16268 Show the maximum number of target hardware breakpoints."), _("\
16269 Specify \"unlimited\" for unlimited hardware breakpoints."),
16270 NULL
, show_hardware_breakpoint_limit
,
16271 &remote_set_cmdlist
, &remote_show_cmdlist
);
16273 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
16274 &remote_address_size
, _("\
16275 Set the maximum size of the address (in bits) in a memory packet."), _("\
16276 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
16278 NULL
, /* FIXME: i18n: */
16279 &setlist
, &showlist
);
16281 init_all_packet_configs ();
16283 add_packet_config_cmd (PACKET_X
, "X", "binary-download", 1);
16285 add_packet_config_cmd (PACKET_x
, "x", "binary-upload", 0);
16287 add_packet_config_cmd (PACKET_vCont
, "vCont", "verbose-resume", 0);
16289 add_packet_config_cmd (PACKET_QPassSignals
, "QPassSignals", "pass-signals",
16292 add_packet_config_cmd (PACKET_QCatchSyscalls
, "QCatchSyscalls",
16293 "catch-syscalls", 0);
16295 add_packet_config_cmd (PACKET_QProgramSignals
, "QProgramSignals",
16296 "program-signals", 0);
16298 add_packet_config_cmd (PACKET_QSetWorkingDir
, "QSetWorkingDir",
16299 "set-working-dir", 0);
16301 add_packet_config_cmd (PACKET_QStartupWithShell
, "QStartupWithShell",
16302 "startup-with-shell", 0);
16304 add_packet_config_cmd (PACKET_QEnvironmentHexEncoded
,"QEnvironmentHexEncoded",
16305 "environment-hex-encoded", 0);
16307 add_packet_config_cmd (PACKET_QEnvironmentReset
, "QEnvironmentReset",
16308 "environment-reset", 0);
16310 add_packet_config_cmd (PACKET_QEnvironmentUnset
, "QEnvironmentUnset",
16311 "environment-unset", 0);
16313 add_packet_config_cmd (PACKET_qSymbol
, "qSymbol", "symbol-lookup", 0);
16315 add_packet_config_cmd (PACKET_P
, "P", "set-register", 1);
16317 add_packet_config_cmd (PACKET_p
, "p", "fetch-register", 1);
16319 add_packet_config_cmd (PACKET_Z0
, "Z0", "software-breakpoint", 0);
16321 add_packet_config_cmd (PACKET_Z1
, "Z1", "hardware-breakpoint", 0);
16323 add_packet_config_cmd (PACKET_Z2
, "Z2", "write-watchpoint", 0);
16325 add_packet_config_cmd (PACKET_Z3
, "Z3", "read-watchpoint", 0);
16327 add_packet_config_cmd (PACKET_Z4
, "Z4", "access-watchpoint", 0);
16329 add_packet_config_cmd (PACKET_qXfer_auxv
, "qXfer:auxv:read",
16330 "read-aux-vector", 0);
16332 add_packet_config_cmd (PACKET_qXfer_exec_file
, "qXfer:exec-file:read",
16333 "pid-to-exec-file", 0);
16335 add_packet_config_cmd (PACKET_qXfer_features
,
16336 "qXfer:features:read", "target-features", 0);
16338 add_packet_config_cmd (PACKET_qXfer_libraries
, "qXfer:libraries:read",
16339 "library-info", 0);
16341 add_packet_config_cmd (PACKET_qXfer_libraries_svr4
,
16342 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
16344 add_packet_config_cmd (PACKET_qXfer_memory_map
, "qXfer:memory-map:read",
16347 add_packet_config_cmd (PACKET_qXfer_osdata
, "qXfer:osdata:read", "osdata", 0);
16349 add_packet_config_cmd (PACKET_qXfer_threads
, "qXfer:threads:read", "threads",
16352 add_packet_config_cmd (PACKET_qXfer_siginfo_read
, "qXfer:siginfo:read",
16353 "read-siginfo-object", 0);
16355 add_packet_config_cmd (PACKET_qXfer_siginfo_write
, "qXfer:siginfo:write",
16356 "write-siginfo-object", 0);
16358 add_packet_config_cmd (PACKET_qXfer_traceframe_info
,
16359 "qXfer:traceframe-info:read", "traceframe-info", 0);
16361 add_packet_config_cmd (PACKET_qXfer_uib
, "qXfer:uib:read",
16362 "unwind-info-block", 0);
16364 add_packet_config_cmd (PACKET_qGetTLSAddr
, "qGetTLSAddr",
16365 "get-thread-local-storage-address", 0);
16367 add_packet_config_cmd (PACKET_qGetTIBAddr
, "qGetTIBAddr",
16368 "get-thread-information-block-address", 0);
16370 add_packet_config_cmd (PACKET_bc
, "bc", "reverse-continue", 0);
16372 add_packet_config_cmd (PACKET_bs
, "bs", "reverse-step", 0);
16374 add_packet_config_cmd (PACKET_qSupported
, "qSupported", "supported-packets",
16377 add_packet_config_cmd (PACKET_qSearch_memory
, "qSearch:memory",
16378 "search-memory", 0);
16380 add_packet_config_cmd (PACKET_qTStatus
, "qTStatus", "trace-status", 0);
16382 add_packet_config_cmd (PACKET_vFile_setfs
, "vFile:setfs", "hostio-setfs", 0);
16384 add_packet_config_cmd (PACKET_vFile_open
, "vFile:open", "hostio-open", 0);
16386 add_packet_config_cmd (PACKET_vFile_pread
, "vFile:pread", "hostio-pread", 0);
16388 add_packet_config_cmd (PACKET_vFile_pwrite
, "vFile:pwrite", "hostio-pwrite",
16391 add_packet_config_cmd (PACKET_vFile_close
, "vFile:close", "hostio-close", 0);
16393 add_packet_config_cmd (PACKET_vFile_unlink
, "vFile:unlink", "hostio-unlink",
16396 add_packet_config_cmd (PACKET_vFile_readlink
, "vFile:readlink",
16397 "hostio-readlink", 0);
16399 add_packet_config_cmd (PACKET_vFile_fstat
, "vFile:fstat", "hostio-fstat", 0);
16401 add_packet_config_cmd (PACKET_vFile_stat
, "vFile:stat", "hostio-stat", 0);
16403 add_packet_config_cmd (PACKET_vAttach
, "vAttach", "attach", 0);
16405 add_packet_config_cmd (PACKET_vRun
, "vRun", "run", 0);
16407 add_packet_config_cmd (PACKET_QStartNoAckMode
, "QStartNoAckMode", "noack", 0);
16409 add_packet_config_cmd (PACKET_vKill
, "vKill", "kill", 0);
16411 add_packet_config_cmd (PACKET_qAttached
, "qAttached", "query-attached", 0);
16413 add_packet_config_cmd (PACKET_ConditionalTracepoints
,
16414 "ConditionalTracepoints", "conditional-tracepoints",
16417 add_packet_config_cmd (PACKET_ConditionalBreakpoints
,
16418 "ConditionalBreakpoints", "conditional-breakpoints",
16421 add_packet_config_cmd (PACKET_BreakpointCommands
, "BreakpointCommands",
16422 "breakpoint-commands", 0);
16424 add_packet_config_cmd (PACKET_FastTracepoints
, "FastTracepoints",
16425 "fast-tracepoints", 0);
16427 add_packet_config_cmd (PACKET_TracepointSource
, "TracepointSource",
16428 "TracepointSource", 0);
16430 add_packet_config_cmd (PACKET_QAllow
, "QAllow", "allow", 0);
16432 add_packet_config_cmd (PACKET_StaticTracepoints
, "StaticTracepoints",
16433 "static-tracepoints", 0);
16435 add_packet_config_cmd (PACKET_InstallInTrace
, "InstallInTrace",
16436 "install-in-trace", 0);
16438 add_packet_config_cmd (PACKET_qXfer_statictrace_read
,
16439 "qXfer:statictrace:read", "read-sdata-object", 0);
16441 add_packet_config_cmd (PACKET_qXfer_fdpic
, "qXfer:fdpic:read",
16442 "read-fdpic-loadmap", 0);
16444 add_packet_config_cmd (PACKET_QDisableRandomization
, "QDisableRandomization",
16445 "disable-randomization", 0);
16447 add_packet_config_cmd (PACKET_QAgent
, "QAgent", "agent", 0);
16449 add_packet_config_cmd (PACKET_QTBuffer_size
, "QTBuffer:size",
16450 "trace-buffer-size", 0);
16452 add_packet_config_cmd (PACKET_Qbtrace_off
, "Qbtrace:off", "disable-btrace",
16455 add_packet_config_cmd (PACKET_Qbtrace_bts
, "Qbtrace:bts", "enable-btrace-bts",
16458 add_packet_config_cmd (PACKET_Qbtrace_pt
, "Qbtrace:pt", "enable-btrace-pt",
16461 add_packet_config_cmd (PACKET_qXfer_btrace
, "qXfer:btrace", "read-btrace", 0);
16463 add_packet_config_cmd (PACKET_qXfer_btrace_conf
, "qXfer:btrace-conf",
16464 "read-btrace-conf", 0);
16466 add_packet_config_cmd (PACKET_Qbtrace_conf_bts_size
, "Qbtrace-conf:bts:size",
16467 "btrace-conf-bts-size", 0);
16469 add_packet_config_cmd (PACKET_multiprocess_feature
, "multiprocess-feature",
16470 "multiprocess-feature", 0);
16472 add_packet_config_cmd (PACKET_swbreak_feature
, "swbreak-feature",
16473 "swbreak-feature", 0);
16475 add_packet_config_cmd (PACKET_hwbreak_feature
, "hwbreak-feature",
16476 "hwbreak-feature", 0);
16478 add_packet_config_cmd (PACKET_fork_event_feature
, "fork-event-feature",
16479 "fork-event-feature", 0);
16481 add_packet_config_cmd (PACKET_vfork_event_feature
, "vfork-event-feature",
16482 "vfork-event-feature", 0);
16484 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_size
, "Qbtrace-conf:pt:size",
16485 "btrace-conf-pt-size", 0);
16487 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_ptwrite
, "Qbtrace-conf:pt:ptwrite",
16488 "btrace-conf-pt-ptwrite", 0);
16490 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_event_tracing
,
16491 "Qbtrace-conf:pt:event-tracing",
16492 "btrace-conf-pt-event-tracing", 0);
16494 add_packet_config_cmd (PACKET_vContSupported
, "vContSupported",
16495 "verbose-resume-supported", 0);
16497 add_packet_config_cmd (PACKET_exec_event_feature
, "exec-event-feature",
16498 "exec-event-feature", 0);
16500 add_packet_config_cmd (PACKET_vCtrlC
, "vCtrlC", "ctrl-c", 0);
16502 add_packet_config_cmd (PACKET_QThreadEvents
, "QThreadEvents", "thread-events",
16505 add_packet_config_cmd (PACKET_QThreadOptions
, "QThreadOptions",
16506 "thread-options", 0);
16508 add_packet_config_cmd (PACKET_no_resumed
, "N stop reply",
16509 "no-resumed-stop-reply", 0);
16511 add_packet_config_cmd (PACKET_memory_tagging_feature
,
16512 "memory-tagging-feature", "memory-tagging-feature", 0);
16514 add_packet_config_cmd (PACKET_qIsAddressTagged
,
16515 "qIsAddressTagged", "memory-tagging-address-check", 0);
16517 add_packet_config_cmd (PACKET_accept_error_message
,
16518 "error-message", "error-message", 0);
16520 /* Assert that we've registered "set remote foo-packet" commands
16521 for all packet configs. */
16525 for (i
= 0; i
< PACKET_MAX
; i
++)
16527 /* Ideally all configs would have a command associated. Some
16528 still don't though. */
16533 case PACKET_QNonStop
:
16534 case PACKET_EnableDisableTracepoints_feature
:
16535 case PACKET_tracenz_feature
:
16536 case PACKET_DisconnectedTracing_feature
:
16537 case PACKET_augmented_libraries_svr4_read_feature
:
16539 /* Additions to this list need to be well justified:
16540 pre-existing packets are OK; new packets are not. */
16548 /* This catches both forgetting to add a config command, and
16549 forgetting to remove a packet from the exception list. */
16550 gdb_assert (excepted
== (packets_descriptions
[i
].name
== NULL
));
16554 /* Keep the old ``set remote Z-packet ...'' working. Each individual
16555 Z sub-packet has its own set and show commands, but users may
16556 have sets to this variable in their .gdbinit files (or in their
16558 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
16559 &remote_Z_packet_detect
, _("\
16560 Set use of remote protocol `Z' packets."), _("\
16561 Show use of remote protocol `Z' packets."), _("\
16562 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
16564 set_remote_protocol_Z_packet_cmd
,
16565 show_remote_protocol_Z_packet_cmd
,
16566 /* FIXME: i18n: Use of remote protocol
16567 `Z' packets is %s. */
16568 &remote_set_cmdlist
, &remote_show_cmdlist
);
16570 add_basic_prefix_cmd ("remote", class_files
, _("\
16571 Manipulate files on the remote system.\n\
16572 Transfer files to and from the remote target system."),
16574 0 /* allow-unknown */, &cmdlist
);
16576 add_cmd ("put", class_files
, remote_put_command
,
16577 _("Copy a local file to the remote system."),
16580 add_cmd ("get", class_files
, remote_get_command
,
16581 _("Copy a remote file to the local system."),
16584 add_cmd ("delete", class_files
, remote_delete_command
,
16585 _("Delete a remote file."),
16588 add_setshow_string_noescape_cmd ("exec-file", class_files
,
16589 &remote_exec_file_var
, _("\
16590 Set the remote pathname for \"run\"."), _("\
16591 Show the remote pathname for \"run\"."), NULL
,
16592 set_remote_exec_file
,
16593 show_remote_exec_file
,
16594 &remote_set_cmdlist
,
16595 &remote_show_cmdlist
);
16597 add_setshow_boolean_cmd ("range-stepping", class_run
,
16598 &use_range_stepping
, _("\
16599 Enable or disable range stepping."), _("\
16600 Show whether target-assisted range stepping is enabled."), _("\
16601 If on, and the target supports it, when stepping a source line, GDB\n\
16602 tells the target to step the corresponding range of addresses itself instead\n\
16603 of issuing multiple single-steps. This speeds up source level\n\
16604 stepping. If off, GDB always issues single-steps, even if range\n\
16605 stepping is supported by the target. The default is on."),
16606 set_range_stepping
,
16607 show_range_stepping
,
16611 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
16612 Set watchdog timer."), _("\
16613 Show watchdog timer."), _("\
16614 When non-zero, this timeout is used instead of waiting forever for a target\n\
16615 to finish a low-level step or continue operation. If the specified amount\n\
16616 of time passes without a response from the target, an error occurs."),
16619 &setlist
, &showlist
);
16621 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
16622 &remote_packet_max_chars
, _("\
16623 Set the maximum number of characters to display for each remote packet."), _("\
16624 Show the maximum number of characters to display for each remote packet."), _("\
16625 Specify \"unlimited\" to display all the characters."),
16626 NULL
, show_remote_packet_max_chars
,
16627 &setdebuglist
, &showdebuglist
);
16629 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
16630 _("Set debugging of remote protocol."),
16631 _("Show debugging of remote protocol."),
16633 When enabled, each packet sent or received with the remote target\n\
16637 &setdebuglist
, &showdebuglist
);
16639 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
16640 &remote_timeout
, _("\
16641 Set timeout limit to wait for target to respond."), _("\
16642 Show timeout limit to wait for target to respond."), _("\
16643 This value is used to set the time limit for gdb to wait for a response\n\
16644 from the target."),
16646 show_remote_timeout
,
16647 &setlist
, &showlist
);
16649 /* Eventually initialize fileio. See fileio.c */
16650 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
16653 selftests::register_test ("remote_memory_tagging",
16654 selftests::test_memory_tagging_functions
);
16655 selftests::register_test ("packet_check_result",
16656 selftests::test_packet_check_result
);