1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2023 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. */
30 #include "process-stratum-target.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 "target-descriptions.h"
44 #include "gdbsupport/filestuff.h"
45 #include "gdbsupport/rsp-low.h"
49 #include "gdbsupport/gdb_sys_time.h"
51 #include "gdbsupport/event-loop.h"
52 #include "event-top.h"
60 #include "remote-fileio.h"
61 #include "gdbsupport/fileio.h"
63 #include "xml-support.h"
65 #include "memory-map.h"
67 #include "tracepoint.h"
70 #include "gdbsupport/agent.h"
72 #include "record-btrace.h"
73 #include "gdbsupport/scoped_restore.h"
74 #include "gdbsupport/environ.h"
75 #include "gdbsupport/byte-vector.h"
76 #include "gdbsupport/search.h"
79 #include <unordered_map>
80 #include "async-event.h"
81 #include "gdbsupport/selftest.h"
83 /* The remote target. */
85 static const char remote_doc
[] = N_("\
86 Use a remote computer via a serial line, using a gdb-specific protocol.\n\
87 Specify the serial device it is connected to\n\
88 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
92 bool remote_debug
= false;
94 #define OPAQUETHREADBYTES 8
96 /* a 64 bit opaque identifier */
97 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
99 struct gdb_ext_thread_info
;
100 struct threads_listing_context
;
101 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
102 struct protocol_feature
;
106 typedef std::unique_ptr
<stop_reply
> stop_reply_up
;
108 /* Generic configuration support for packets the stub optionally
109 supports. Allows the user to specify the use of the packet as well
110 as allowing GDB to auto-detect support in the remote stub. */
114 PACKET_SUPPORT_UNKNOWN
= 0,
119 /* Convert the packet support auto_boolean to a name used for gdb printing. */
122 get_packet_support_name (auto_boolean support
)
126 case AUTO_BOOLEAN_TRUE
:
128 case AUTO_BOOLEAN_FALSE
:
130 case AUTO_BOOLEAN_AUTO
:
133 gdb_assert_not_reached ("invalid var_auto_boolean");
137 /* Convert the target type (future remote target or currently connected target)
138 to a name used for gdb printing. */
141 get_target_type_name (bool target_connected
)
143 if (target_connected
)
144 return _("on the current remote target");
146 return _("on future remote targets");
149 /* Analyze a packet's return value and update the packet config
159 /* Enumeration of packets for a remote target. */
178 PACKET_vFile_readlink
,
181 PACKET_qXfer_features
,
182 PACKET_qXfer_exec_file
,
183 PACKET_qXfer_libraries
,
184 PACKET_qXfer_libraries_svr4
,
185 PACKET_qXfer_memory_map
,
187 PACKET_qXfer_threads
,
188 PACKET_qXfer_statictrace_read
,
189 PACKET_qXfer_traceframe_info
,
196 PACKET_QCatchSyscalls
,
197 PACKET_QProgramSignals
,
198 PACKET_QSetWorkingDir
,
199 PACKET_QStartupWithShell
,
200 PACKET_QEnvironmentHexEncoded
,
201 PACKET_QEnvironmentReset
,
202 PACKET_QEnvironmentUnset
,
204 PACKET_qSearch_memory
,
207 PACKET_QStartNoAckMode
,
209 PACKET_qXfer_siginfo_read
,
210 PACKET_qXfer_siginfo_write
,
213 /* Support for conditional tracepoints. */
214 PACKET_ConditionalTracepoints
,
216 /* Support for target-side breakpoint conditions. */
217 PACKET_ConditionalBreakpoints
,
219 /* Support for target-side breakpoint commands. */
220 PACKET_BreakpointCommands
,
222 /* Support for fast tracepoints. */
223 PACKET_FastTracepoints
,
225 /* Support for static tracepoints. */
226 PACKET_StaticTracepoints
,
228 /* Support for installing tracepoints while a trace experiment is
230 PACKET_InstallInTrace
,
234 PACKET_TracepointSource
,
237 PACKET_QDisableRandomization
,
239 PACKET_QTBuffer_size
,
245 /* Support for the QNonStop packet. */
248 /* Support for the QThreadEvents packet. */
249 PACKET_QThreadEvents
,
251 /* Support for multi-process extensions. */
252 PACKET_multiprocess_feature
,
254 /* Support for enabling and disabling tracepoints while a trace
255 experiment is running. */
256 PACKET_EnableDisableTracepoints_feature
,
258 /* Support for collecting strings using the tracenz bytecode. */
259 PACKET_tracenz_feature
,
261 /* Support for continuing to run a trace experiment while GDB is
263 PACKET_DisconnectedTracing_feature
,
265 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
266 PACKET_augmented_libraries_svr4_read_feature
,
268 /* Support for the qXfer:btrace-conf:read packet. */
269 PACKET_qXfer_btrace_conf
,
271 /* Support for the Qbtrace-conf:bts:size packet. */
272 PACKET_Qbtrace_conf_bts_size
,
274 /* Support for swbreak+ feature. */
275 PACKET_swbreak_feature
,
277 /* Support for hwbreak+ feature. */
278 PACKET_hwbreak_feature
,
280 /* Support for fork events. */
281 PACKET_fork_event_feature
,
283 /* Support for vfork events. */
284 PACKET_vfork_event_feature
,
286 /* Support for the Qbtrace-conf:pt:size packet. */
287 PACKET_Qbtrace_conf_pt_size
,
289 /* Support for exec events. */
290 PACKET_exec_event_feature
,
292 /* Support for query supported vCont actions. */
293 PACKET_vContSupported
,
295 /* Support remote CTRL-C. */
298 /* Support TARGET_WAITKIND_NO_RESUMED. */
301 /* Support for memory tagging, allocation tag fetch/store
302 packets and the tag violation stop replies. */
303 PACKET_memory_tagging_feature
,
308 struct threads_listing_context
;
310 /* Stub vCont actions support.
312 Each field is a boolean flag indicating whether the stub reports
313 support for the corresponding action. */
315 struct vCont_action_support
330 /* About this many threadids fit in a packet. */
332 #define MAXTHREADLISTRESULTS 32
334 /* Data for the vFile:pread readahead cache. */
336 struct readahead_cache
338 /* Invalidate the readahead cache. */
341 /* Invalidate the readahead cache if it is holding data for FD. */
342 void invalidate_fd (int fd
);
344 /* Serve pread from the readahead cache. Returns number of bytes
345 read, or 0 if the request can't be served from the cache. */
346 int pread (int fd
, gdb_byte
*read_buf
, size_t len
, ULONGEST offset
);
348 /* The file descriptor for the file that is being cached. -1 if the
352 /* The offset into the file that the cache buffer corresponds
356 /* The buffer holding the cache contents. */
357 gdb_byte
*buf
= nullptr;
358 /* The buffer's size. We try to read as much as fits into a packet
362 /* Cache hit and miss counters. */
363 ULONGEST hit_count
= 0;
364 ULONGEST miss_count
= 0;
367 /* Description of the remote protocol for a given architecture. */
371 long offset
; /* Offset into G packet. */
372 long regnum
; /* GDB's internal register number. */
373 LONGEST pnum
; /* Remote protocol register number. */
374 int in_g_packet
; /* Always part of G packet. */
375 /* long size in bytes; == register_size (target_gdbarch (), regnum);
377 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
381 struct remote_arch_state
383 explicit remote_arch_state (struct gdbarch
*gdbarch
);
385 /* Description of the remote protocol registers. */
386 long sizeof_g_packet
;
388 /* Description of the remote protocol registers indexed by REGNUM
389 (making an array gdbarch_num_regs in size). */
390 std::unique_ptr
<packet_reg
[]> regs
;
392 /* This is the size (in chars) of the first response to the ``g''
393 packet. It is used as a heuristic when determining the maximum
394 size of memory-read and memory-write packets. A target will
395 typically only reserve a buffer large enough to hold the ``g''
396 packet. The size does not include packet overhead (headers and
398 long actual_register_packet_size
;
400 /* This is the maximum size (in chars) of a non read/write packet.
401 It is also used as a cap on the size of read/write packets. */
402 long remote_packet_size
;
405 /* Description of the remote protocol state for the currently
406 connected target. This is per-target state, and independent of the
407 selected architecture. */
416 /* Get the remote arch state for GDBARCH. */
417 struct remote_arch_state
*get_remote_arch_state (struct gdbarch
*gdbarch
);
421 /* A buffer to use for incoming packets, and its current size. The
422 buffer is grown dynamically for larger incoming packets.
423 Outgoing packets may also be constructed in this buffer.
424 The size of the buffer is always at least REMOTE_PACKET_SIZE;
425 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
427 gdb::char_vector buf
;
429 /* True if we're going through initial connection setup (finding out
430 about the remote side's threads, relocating symbols, etc.). */
431 bool starting_up
= false;
433 /* If we negotiated packet size explicitly (and thus can bypass
434 heuristics for the largest packet size that will not overflow
435 a buffer in the stub), this will be set to that packet size.
436 Otherwise zero, meaning to use the guessed size. */
437 long explicit_packet_size
= 0;
439 /* True, if in no ack mode. That is, neither GDB nor the stub will
440 expect acks from each other. The connection is assumed to be
442 bool noack_mode
= false;
444 /* True if we're connected in extended remote mode. */
445 bool extended
= false;
447 /* True if we resumed the target and we're waiting for the target to
448 stop. In the mean time, we can't start another command/query.
449 The remote server wouldn't be ready to process it, so we'd
450 timeout waiting for a reply that would never come and eventually
451 we'd close the connection. This can happen in asynchronous mode
452 because we allow GDB commands while the target is running. */
453 bool waiting_for_stop_reply
= false;
455 /* The status of the stub support for the various vCont actions. */
456 vCont_action_support supports_vCont
;
458 /* True if the user has pressed Ctrl-C, but the target hasn't
459 responded to that. */
460 bool ctrlc_pending_p
= false;
462 /* True if we saw a Ctrl-C while reading or writing from/to the
463 remote descriptor. At that point it is not safe to send a remote
464 interrupt packet, so we instead remember we saw the Ctrl-C and
465 process it once we're done with sending/receiving the current
466 packet, which should be shortly. If however that takes too long,
467 and the user presses Ctrl-C again, we offer to disconnect. */
468 bool got_ctrlc_during_io
= false;
470 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
471 remote_open knows that we don't have a file open when the program
473 struct serial
*remote_desc
= nullptr;
475 /* These are the threads which we last sent to the remote system. The
476 TID member will be -1 for all or -2 for not sent yet. */
477 ptid_t general_thread
= null_ptid
;
478 ptid_t continue_thread
= null_ptid
;
480 /* This is the traceframe which we last selected on the remote system.
481 It will be -1 if no traceframe is selected. */
482 int remote_traceframe_number
= -1;
484 char *last_pass_packet
= nullptr;
486 /* The last QProgramSignals packet sent to the target. We bypass
487 sending a new program signals list down to the target if the new
488 packet is exactly the same as the last we sent. IOW, we only let
489 the target know about program signals list changes. */
490 char *last_program_signals_packet
= nullptr;
492 gdb_signal last_sent_signal
= GDB_SIGNAL_0
;
494 bool last_sent_step
= false;
496 /* The execution direction of the last resume we got. */
497 exec_direction_kind last_resume_exec_dir
= EXEC_FORWARD
;
499 char *finished_object
= nullptr;
500 char *finished_annex
= nullptr;
501 ULONGEST finished_offset
= 0;
503 /* Should we try the 'ThreadInfo' query packet?
505 This variable (NOT available to the user: auto-detect only!)
506 determines whether GDB will use the new, simpler "ThreadInfo"
507 query or the older, more complex syntax for thread queries.
508 This is an auto-detect variable (set to true at each connect,
509 and set to false when the target fails to recognize it). */
510 bool use_threadinfo_query
= false;
511 bool use_threadextra_query
= false;
513 threadref echo_nextthread
{};
514 threadref nextthread
{};
515 threadref resultthreadlist
[MAXTHREADLISTRESULTS
] {};
517 /* The state of remote notification. */
518 struct remote_notif_state
*notif_state
= nullptr;
520 /* The branch trace configuration. */
521 struct btrace_config btrace_config
{};
523 /* The argument to the last "vFile:setfs:" packet we sent, used
524 to avoid sending repeated unnecessary "vFile:setfs:" packets.
525 Initialized to -1 to indicate that no "vFile:setfs:" packet
526 has yet been sent. */
529 /* A readahead cache for vFile:pread. Often, reading a binary
530 involves a sequence of small reads. E.g., when parsing an ELF
531 file. A readahead cache helps mostly the case of remote
532 debugging on a connection with higher latency, due to the
533 request/reply nature of the RSP. We only cache data for a single
534 file descriptor at a time. */
535 struct readahead_cache readahead_cache
;
537 /* The list of already fetched and acknowledged stop events. This
538 queue is used for notification Stop, and other notifications
539 don't need queue for their events, because the notification
540 events of Stop can't be consumed immediately, so that events
541 should be queued first, and be consumed by remote_wait_{ns,as}
542 one per time. Other notifications can consume their events
543 immediately, so queue is not needed for them. */
544 std::vector
<stop_reply_up
> stop_reply_queue
;
546 /* Asynchronous signal handle registered as event loop source for
547 when we have pending events ready to be passed to the core. */
548 struct async_event_handler
*remote_async_inferior_event_token
= nullptr;
550 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
551 ``forever'' still use the normal timeout mechanism. This is
552 currently used by the ASYNC code to guarentee that target reads
553 during the initial connect always time-out. Once getpkt has been
554 modified to return a timeout indication and, in turn
555 remote_wait()/wait_for_inferior() have gained a timeout parameter
557 int wait_forever_enabled_p
= 1;
560 /* Mapping of remote protocol data for each gdbarch. Usually there
561 is only one entry here, though we may see more with stubs that
562 support multi-process. */
563 std::unordered_map
<struct gdbarch
*, remote_arch_state
>
567 static const target_info remote_target_info
= {
569 N_("Remote target using gdb-specific protocol"),
573 /* Description of a remote packet. */
575 struct packet_description
577 /* Name of the packet used for gdb output. */
580 /* Title of the packet, used by the set/show remote name-packet
581 commands to identify the individual packages and gdb output. */
585 /* Configuration of a remote packet. */
589 /* If auto, GDB auto-detects support for this packet or feature,
590 either through qSupported, or by trying the packet and looking
591 at the response. If true, GDB assumes the target supports this
592 packet. If false, the packet is disabled. Configs that don't
593 have an associated command always have this set to auto. */
594 enum auto_boolean detect
;
596 /* Does the target support this packet? */
597 enum packet_support support
;
600 /* User configurable variables for the number of characters in a
601 memory read/write packet. MIN (rsa->remote_packet_size,
602 rsa->sizeof_g_packet) is the default. Some targets need smaller
603 values (fifo overruns, et.al.) and some users need larger values
604 (speed up transfers). The variables ``preferred_*'' (the user
605 request), ``current_*'' (what was actually set) and ``forced_*''
606 (Positive - a soft limit, negative - a hard limit). */
608 struct memory_packet_config
615 /* These global variables contain the default configuration for every new
616 remote_feature object. */
617 static memory_packet_config memory_read_packet_config
=
619 "memory-read-packet-size",
621 static memory_packet_config memory_write_packet_config
=
623 "memory-write-packet-size",
626 /* This global array contains packet descriptions (name and title). */
627 static packet_description packets_descriptions
[PACKET_MAX
];
628 /* This global array contains the default configuration for every new
629 per-remote target array. */
630 static packet_config remote_protocol_packets
[PACKET_MAX
];
632 /* Description of a remote target's features. It stores the configuration
633 and provides functions to determine supported features of the target. */
635 struct remote_features
639 m_memory_read_packet_config
= memory_read_packet_config
;
640 m_memory_write_packet_config
= memory_write_packet_config
;
642 std::copy (std::begin (remote_protocol_packets
),
643 std::end (remote_protocol_packets
),
644 std::begin (m_protocol_packets
));
646 ~remote_features () = default;
648 DISABLE_COPY_AND_ASSIGN (remote_features
);
650 /* Returns whether a given packet defined by its enum value is supported. */
651 enum packet_support
packet_support (int) const;
653 /* Returns the packet's corresponding "set remote foo-packet" command
654 state. See struct packet_config for more details. */
655 enum auto_boolean
packet_set_cmd_state (int packet
) const
656 { return m_protocol_packets
[packet
].detect
; }
658 /* Returns true if the multi-process extensions are in effect. */
659 int remote_multi_process_p () const
660 { return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
; }
662 /* Returns true if fork events are supported. */
663 int remote_fork_event_p () const
664 { return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
; }
666 /* Returns true if vfork events are supported. */
667 int remote_vfork_event_p () const
668 { return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
; }
670 /* Returns true if exec events are supported. */
671 int remote_exec_event_p () const
672 { return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
; }
674 /* Returns true if memory tagging is supported, false otherwise. */
675 bool remote_memory_tagging_p () const
676 { return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
; }
678 /* Reset all packets back to "unknown support". Called when opening a
679 new connection to a remote target. */
680 void reset_all_packet_configs_support ();
682 /* Check result value in BUF for packet WHICH_PACKET and update the packet's
683 support configuration accordingly. */
684 packet_result
packet_ok (const char *buf
, const int which_packet
);
685 packet_result
packet_ok (const gdb::char_vector
&buf
, const int which_packet
);
687 /* Configuration of a remote target's memory read packet. */
688 memory_packet_config m_memory_read_packet_config
;
689 /* Configuration of a remote target's memory write packet. */
690 memory_packet_config m_memory_write_packet_config
;
692 /* The per-remote target array which stores a remote's packet
694 packet_config m_protocol_packets
[PACKET_MAX
];
697 class remote_target
: public process_stratum_target
700 remote_target () = default;
701 ~remote_target () override
;
703 const target_info
&info () const override
704 { return remote_target_info
; }
706 const char *connection_string () override
;
708 thread_control_capabilities
get_thread_control_capabilities () override
709 { return tc_schedlock
; }
711 /* Open a remote connection. */
712 static void open (const char *, int);
714 void close () override
;
716 void detach (inferior
*, int) override
;
717 void disconnect (const char *, int) override
;
719 void commit_resumed () override
;
720 void resume (ptid_t
, int, enum gdb_signal
) override
;
721 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
722 bool has_pending_events () override
;
724 void fetch_registers (struct regcache
*, int) override
;
725 void store_registers (struct regcache
*, int) override
;
726 void prepare_to_store (struct regcache
*) override
;
728 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
730 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
731 enum remove_bp_reason
) override
;
734 bool stopped_by_sw_breakpoint () override
;
735 bool supports_stopped_by_sw_breakpoint () override
;
737 bool stopped_by_hw_breakpoint () override
;
739 bool supports_stopped_by_hw_breakpoint () override
;
741 bool stopped_by_watchpoint () override
;
743 bool stopped_data_address (CORE_ADDR
*) override
;
745 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
747 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
749 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
751 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
753 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
755 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
756 struct expression
*) override
;
758 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
759 struct expression
*) override
;
761 void kill () override
;
763 void load (const char *, int) override
;
765 void mourn_inferior () override
;
767 void pass_signals (gdb::array_view
<const unsigned char>) override
;
769 int set_syscall_catchpoint (int, bool, int,
770 gdb::array_view
<const int>) override
;
772 void program_signals (gdb::array_view
<const unsigned char>) override
;
774 bool thread_alive (ptid_t ptid
) override
;
776 const char *thread_name (struct thread_info
*) override
;
778 void update_thread_list () override
;
780 std::string
pid_to_str (ptid_t
) override
;
782 const char *extra_thread_info (struct thread_info
*) override
;
784 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
786 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
788 inferior
*inf
) override
;
790 gdb::byte_vector
thread_info_to_thread_handle (struct thread_info
*tp
)
793 void stop (ptid_t
) override
;
795 void interrupt () override
;
797 void pass_ctrlc () override
;
799 enum target_xfer_status
xfer_partial (enum target_object object
,
802 const gdb_byte
*writebuf
,
803 ULONGEST offset
, ULONGEST len
,
804 ULONGEST
*xfered_len
) override
;
806 ULONGEST
get_memory_xfer_limit () override
;
808 void rcmd (const char *command
, struct ui_file
*output
) override
;
810 const char *pid_to_exec_file (int pid
) override
;
812 void log_command (const char *cmd
) override
814 serial_log_command (this, cmd
);
817 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
818 CORE_ADDR load_module_addr
,
819 CORE_ADDR offset
) override
;
821 bool can_execute_reverse () override
;
823 std::vector
<mem_region
> memory_map () override
;
825 void flash_erase (ULONGEST address
, LONGEST length
) override
;
827 void flash_done () override
;
829 const struct target_desc
*read_description () override
;
831 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
832 const gdb_byte
*pattern
, ULONGEST pattern_len
,
833 CORE_ADDR
*found_addrp
) override
;
835 bool can_async_p () override
;
837 bool is_async_p () override
;
839 void async (bool) override
;
841 int async_wait_fd () override
;
843 void thread_events (int) override
;
845 int can_do_single_step () override
;
847 void terminal_inferior () override
;
849 void terminal_ours () override
;
851 bool supports_non_stop () override
;
853 bool supports_multi_process () override
;
855 bool supports_disable_randomization () override
;
857 bool filesystem_is_local () override
;
860 int fileio_open (struct inferior
*inf
, const char *filename
,
861 int flags
, int mode
, int warn_if_slow
,
862 fileio_error
*target_errno
) override
;
864 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
865 ULONGEST offset
, fileio_error
*target_errno
) override
;
867 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
868 ULONGEST offset
, fileio_error
*target_errno
) override
;
870 int fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
) override
;
872 int fileio_close (int fd
, fileio_error
*target_errno
) override
;
874 int fileio_unlink (struct inferior
*inf
,
875 const char *filename
,
876 fileio_error
*target_errno
) override
;
878 gdb::optional
<std::string
>
879 fileio_readlink (struct inferior
*inf
,
880 const char *filename
,
881 fileio_error
*target_errno
) override
;
883 bool supports_enable_disable_tracepoint () override
;
885 bool supports_string_tracing () override
;
887 int remote_supports_cond_tracepoints ();
889 bool supports_evaluation_of_breakpoint_conditions () override
;
891 int remote_supports_fast_tracepoints ();
893 int remote_supports_static_tracepoints ();
895 int remote_supports_install_in_trace ();
897 bool can_run_breakpoint_commands () override
;
899 void trace_init () override
;
901 void download_tracepoint (struct bp_location
*location
) override
;
903 bool can_download_tracepoint () override
;
905 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
907 void enable_tracepoint (struct bp_location
*location
) override
;
909 void disable_tracepoint (struct bp_location
*location
) override
;
911 void trace_set_readonly_regions () override
;
913 void trace_start () override
;
915 int get_trace_status (struct trace_status
*ts
) override
;
917 void get_tracepoint_status (struct breakpoint
*tp
, struct uploaded_tp
*utp
)
920 void trace_stop () override
;
922 int trace_find (enum trace_find_type type
, int num
,
923 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
925 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
927 int save_trace_data (const char *filename
) override
;
929 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
931 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
933 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
935 int get_min_fast_tracepoint_insn_len () override
;
937 void set_disconnected_tracing (int val
) override
;
939 void set_circular_trace_buffer (int val
) override
;
941 void set_trace_buffer_size (LONGEST val
) override
;
943 bool set_trace_notes (const char *user
, const char *notes
,
944 const char *stopnotes
) override
;
946 int core_of_thread (ptid_t ptid
) override
;
948 int verify_memory (const gdb_byte
*data
,
949 CORE_ADDR memaddr
, ULONGEST size
) override
;
952 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
954 void set_permissions () override
;
956 bool static_tracepoint_marker_at (CORE_ADDR
,
957 struct static_tracepoint_marker
*marker
)
960 std::vector
<static_tracepoint_marker
>
961 static_tracepoint_markers_by_strid (const char *id
) override
;
963 traceframe_info_up
traceframe_info () override
;
965 bool use_agent (bool use
) override
;
966 bool can_use_agent () override
;
968 struct btrace_target_info
*
969 enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
) override
;
971 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
973 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
975 enum btrace_error
read_btrace (struct btrace_data
*data
,
976 struct btrace_target_info
*btinfo
,
977 enum btrace_read_type type
) override
;
979 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
980 bool augmented_libraries_svr4_read () override
;
981 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
982 void follow_exec (inferior
*, ptid_t
, const char *) override
;
983 int insert_fork_catchpoint (int) override
;
984 int remove_fork_catchpoint (int) override
;
985 int insert_vfork_catchpoint (int) override
;
986 int remove_vfork_catchpoint (int) override
;
987 int insert_exec_catchpoint (int) override
;
988 int remove_exec_catchpoint (int) override
;
989 enum exec_direction_kind
execution_direction () override
;
991 bool supports_memory_tagging () override
;
993 bool fetch_memtags (CORE_ADDR address
, size_t len
,
994 gdb::byte_vector
&tags
, int type
) override
;
996 bool store_memtags (CORE_ADDR address
, size_t len
,
997 const gdb::byte_vector
&tags
, int type
) override
;
999 public: /* Remote specific methods. */
1001 void remote_download_command_source (int num
, ULONGEST addr
,
1002 struct command_line
*cmds
);
1004 void remote_file_put (const char *local_file
, const char *remote_file
,
1006 void remote_file_get (const char *remote_file
, const char *local_file
,
1008 void remote_file_delete (const char *remote_file
, int from_tty
);
1010 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
1011 ULONGEST offset
, fileio_error
*remote_errno
);
1012 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
1013 ULONGEST offset
, fileio_error
*remote_errno
);
1014 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
1015 ULONGEST offset
, fileio_error
*remote_errno
);
1017 int remote_hostio_send_command (int command_bytes
, int which_packet
,
1018 fileio_error
*remote_errno
, const char **attachment
,
1019 int *attachment_len
);
1020 int remote_hostio_set_filesystem (struct inferior
*inf
,
1021 fileio_error
*remote_errno
);
1022 /* We should get rid of this and use fileio_open directly. */
1023 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
1024 int flags
, int mode
, int warn_if_slow
,
1025 fileio_error
*remote_errno
);
1026 int remote_hostio_close (int fd
, fileio_error
*remote_errno
);
1028 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
1029 fileio_error
*remote_errno
);
1031 struct remote_state
*get_remote_state ();
1033 long get_remote_packet_size (void);
1034 long get_memory_packet_size (struct memory_packet_config
*config
);
1036 long get_memory_write_packet_size ();
1037 long get_memory_read_packet_size ();
1039 char *append_pending_thread_resumptions (char *p
, char *endp
,
1041 static void open_1 (const char *name
, int from_tty
, int extended_p
);
1042 void start_remote (int from_tty
, int extended_p
);
1043 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
1045 char *append_resumption (char *p
, char *endp
,
1046 ptid_t ptid
, int step
, gdb_signal siggnal
);
1047 int remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
1048 gdb_signal siggnal
);
1050 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
1052 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
1053 target_wait_flags options
);
1054 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
1055 target_wait_flags options
);
1057 ptid_t
process_stop_reply (struct stop_reply
*stop_reply
,
1058 target_waitstatus
*status
);
1060 ptid_t select_thread_for_ambiguous_stop_reply
1061 (const struct target_waitstatus
&status
);
1063 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
1065 void print_one_stopped_thread (thread_info
*thread
);
1066 void process_initial_stop_replies (int from_tty
);
1068 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
1071 void btrace_sync_conf (const btrace_config
*conf
);
1073 void remote_btrace_maybe_reopen ();
1075 void remove_new_fork_children (threads_listing_context
*context
);
1076 void kill_new_fork_children (inferior
*inf
);
1077 void discard_pending_stop_replies (struct inferior
*inf
);
1078 int stop_reply_queue_length ();
1080 void check_pending_events_prevent_wildcard_vcont
1081 (bool *may_global_wildcard_vcont
);
1083 void discard_pending_stop_replies_in_queue ();
1084 struct stop_reply
*remote_notif_remove_queued_reply (ptid_t ptid
);
1085 struct stop_reply
*queued_stop_reply (ptid_t ptid
);
1086 int peek_stop_reply (ptid_t ptid
);
1087 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
1089 void remote_stop_ns (ptid_t ptid
);
1090 void remote_interrupt_as ();
1091 void remote_interrupt_ns ();
1093 char *remote_get_noisy_reply ();
1094 int remote_query_attached (int pid
);
1095 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
1098 ptid_t
remote_current_thread (ptid_t oldpid
);
1099 ptid_t
get_current_thread (const char *wait_status
);
1101 void set_thread (ptid_t ptid
, int gen
);
1102 void set_general_thread (ptid_t ptid
);
1103 void set_continue_thread (ptid_t ptid
);
1104 void set_general_process ();
1106 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
1108 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
1109 gdb_ext_thread_info
*info
);
1110 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
1111 gdb_ext_thread_info
*info
);
1113 int parse_threadlist_response (const char *pkt
, int result_limit
,
1114 threadref
*original_echo
,
1115 threadref
*resultlist
,
1117 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
1118 int result_limit
, int *done
, int *result_count
,
1119 threadref
*threadlist
);
1121 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1122 void *context
, int looplimit
);
1124 int remote_get_threads_with_ql (threads_listing_context
*context
);
1125 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
1126 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
1128 void extended_remote_restart ();
1130 void get_offsets ();
1132 void remote_check_symbols ();
1134 void remote_supported_packet (const struct protocol_feature
*feature
,
1135 enum packet_support support
,
1136 const char *argument
);
1138 void remote_query_supported ();
1140 void remote_packet_size (const protocol_feature
*feature
,
1141 packet_support support
, const char *value
);
1143 void remote_serial_quit_handler ();
1145 void remote_detach_pid (int pid
);
1147 void remote_vcont_probe ();
1149 void remote_resume_with_hc (ptid_t ptid
, int step
,
1150 gdb_signal siggnal
);
1152 void send_interrupt_sequence ();
1153 void interrupt_query ();
1155 void remote_notif_get_pending_events (const notif_client
*nc
);
1157 int fetch_register_using_p (struct regcache
*regcache
,
1159 int send_g_packet ();
1160 void process_g_packet (struct regcache
*regcache
);
1161 void fetch_registers_using_g (struct regcache
*regcache
);
1162 int store_register_using_P (const struct regcache
*regcache
,
1164 void store_registers_using_G (const struct regcache
*regcache
);
1166 void set_remote_traceframe ();
1168 void check_binary_download (CORE_ADDR addr
);
1170 target_xfer_status
remote_write_bytes_aux (const char *header
,
1172 const gdb_byte
*myaddr
,
1175 ULONGEST
*xfered_len_units
,
1179 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
1180 const gdb_byte
*myaddr
, ULONGEST len
,
1181 int unit_size
, ULONGEST
*xfered_len
);
1183 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
1185 int unit_size
, ULONGEST
*xfered_len_units
);
1187 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
1191 ULONGEST
*xfered_len
);
1193 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
1194 gdb_byte
*myaddr
, ULONGEST len
,
1196 ULONGEST
*xfered_len
);
1198 packet_result
remote_send_printf (const char *format
, ...)
1199 ATTRIBUTE_PRINTF (2, 3);
1201 target_xfer_status
remote_flash_write (ULONGEST address
,
1202 ULONGEST length
, ULONGEST
*xfered_len
,
1203 const gdb_byte
*data
);
1205 int readchar (int timeout
);
1207 void remote_serial_write (const char *str
, int len
);
1209 int putpkt (const char *buf
);
1210 int putpkt_binary (const char *buf
, int cnt
);
1212 int putpkt (const gdb::char_vector
&buf
)
1214 return putpkt (buf
.data ());
1218 long read_frame (gdb::char_vector
*buf_p
);
1219 void getpkt (gdb::char_vector
*buf
, int forever
);
1220 int getpkt_or_notif_sane_1 (gdb::char_vector
*buf
, int forever
,
1221 int expecting_notif
, int *is_notif
);
1222 int getpkt_sane (gdb::char_vector
*buf
, int forever
);
1223 int getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
1225 int remote_vkill (int pid
);
1226 void remote_kill_k ();
1228 void extended_remote_disable_randomization (int val
);
1229 int extended_remote_run (const std::string
&args
);
1231 void send_environment_packet (const char *action
,
1235 void extended_remote_environment_support ();
1236 void extended_remote_set_inferior_cwd ();
1238 target_xfer_status
remote_write_qxfer (const char *object_name
,
1240 const gdb_byte
*writebuf
,
1241 ULONGEST offset
, LONGEST len
,
1242 ULONGEST
*xfered_len
,
1243 const unsigned int which_packet
);
1245 target_xfer_status
remote_read_qxfer (const char *object_name
,
1247 gdb_byte
*readbuf
, ULONGEST offset
,
1249 ULONGEST
*xfered_len
,
1250 const unsigned int which_packet
);
1252 void push_stop_reply (struct stop_reply
*new_event
);
1254 bool vcont_r_supported ();
1256 remote_features m_features
;
1260 bool start_remote_1 (int from_tty
, int extended_p
);
1262 /* The remote state. Don't reference this directly. Use the
1263 get_remote_state method instead. */
1264 remote_state m_remote_state
;
1267 static const target_info extended_remote_target_info
= {
1269 N_("Extended remote target using gdb-specific protocol"),
1273 /* Set up the extended remote target by extending the standard remote
1274 target and adding to it. */
1276 class extended_remote_target final
: public remote_target
1279 const target_info
&info () const override
1280 { return extended_remote_target_info
; }
1282 /* Open an extended-remote connection. */
1283 static void open (const char *, int);
1285 bool can_create_inferior () override
{ return true; }
1286 void create_inferior (const char *, const std::string
&,
1287 char **, int) override
;
1289 void detach (inferior
*, int) override
;
1291 bool can_attach () override
{ return true; }
1292 void attach (const char *, int) override
;
1294 void post_attach (int) override
;
1295 bool supports_disable_randomization () override
;
1298 struct stop_reply
: public notif_event
1302 /* The identifier of the thread about this event */
1305 /* The remote state this event is associated with. When the remote
1306 connection, represented by a remote_state object, is closed,
1307 all the associated stop_reply events should be released. */
1308 struct remote_state
*rs
;
1310 struct target_waitstatus ws
;
1312 /* The architecture associated with the expedited registers. */
1315 /* Expedited registers. This makes remote debugging a bit more
1316 efficient for those targets that provide critical registers as
1317 part of their normal status mechanism (as another roundtrip to
1318 fetch them is avoided). */
1319 std::vector
<cached_reg_t
> regcache
;
1321 enum target_stop_reason stop_reason
;
1323 CORE_ADDR watch_data_address
;
1328 /* Return TARGET as a remote_target if it is one, else nullptr. */
1330 static remote_target
*
1331 as_remote_target (process_stratum_target
*target
)
1333 return dynamic_cast<remote_target
*> (target
);
1339 is_remote_target (process_stratum_target
*target
)
1341 return as_remote_target (target
) != nullptr;
1344 /* Per-program-space data key. */
1345 static const registry
<program_space
>::key
<char, gdb::xfree_deleter
<char>>
1348 /* The variable registered as the control variable used by the
1349 remote exec-file commands. While the remote exec-file setting is
1350 per-program-space, the set/show machinery uses this as the
1351 location of the remote exec-file value. */
1352 static std::string remote_exec_file_var
;
1354 /* The size to align memory write packets, when practical. The protocol
1355 does not guarantee any alignment, and gdb will generate short
1356 writes and unaligned writes, but even as a best-effort attempt this
1357 can improve bulk transfers. For instance, if a write is misaligned
1358 relative to the target's data bus, the stub may need to make an extra
1359 round trip fetching data from the target. This doesn't make a
1360 huge difference, but it's easy to do, so we try to be helpful.
1362 The alignment chosen is arbitrary; usually data bus width is
1363 important here, not the possibly larger cache line size. */
1364 enum { REMOTE_ALIGN_WRITES
= 16 };
1366 /* Prototypes for local functions. */
1368 static int hexnumlen (ULONGEST num
);
1370 static int stubhex (int ch
);
1372 static int hexnumstr (char *, ULONGEST
);
1374 static int hexnumnstr (char *, ULONGEST
, int);
1376 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1378 static int stub_unpack_int (const char *buff
, int fieldlength
);
1380 static void set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
1381 cmd_list_element
*c
);
1383 static void show_packet_config_cmd (ui_file
*file
,
1384 const unsigned int which_packet
,
1385 remote_target
*remote
);
1387 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1389 struct cmd_list_element
*c
,
1392 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1394 static void remote_async_inferior_event_handler (gdb_client_data
);
1396 static bool remote_read_description_p (struct target_ops
*target
);
1398 static void remote_console_output (const char *msg
);
1400 static void remote_btrace_reset (remote_state
*rs
);
1402 static void remote_unpush_and_throw (remote_target
*target
);
1406 static struct cmd_list_element
*remote_cmdlist
;
1408 /* For "set remote" and "show remote". */
1410 static struct cmd_list_element
*remote_set_cmdlist
;
1411 static struct cmd_list_element
*remote_show_cmdlist
;
1413 /* Controls whether GDB is willing to use range stepping. */
1415 static bool use_range_stepping
= true;
1417 /* From the remote target's point of view, each thread is in one of these three
1419 enum class resume_state
1421 /* Not resumed - we haven't been asked to resume this thread. */
1424 /* We have been asked to resume this thread, but haven't sent a vCont action
1425 for it yet. We'll need to consider it next time commit_resume is
1427 RESUMED_PENDING_VCONT
,
1429 /* We have been asked to resume this thread, and we have sent a vCont action
1434 /* Information about a thread's pending vCont-resume. Used when a thread is in
1435 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1436 stores this information which is then picked up by
1437 remote_target::commit_resume to know which is the proper action for this
1438 thread to include in the vCont packet. */
1439 struct resumed_pending_vcont_info
1441 /* True if the last resume call for this thread was a step request, false
1442 if a continue request. */
1445 /* The signal specified in the last resume call for this thread. */
1449 /* Private data that we'll store in (struct thread_info)->priv. */
1450 struct remote_thread_info
: public private_thread_info
1456 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1457 sequence of bytes. */
1458 gdb::byte_vector thread_handle
;
1460 /* Whether the target stopped for a breakpoint/watchpoint. */
1461 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1463 /* This is set to the data address of the access causing the target
1464 to stop for a watchpoint. */
1465 CORE_ADDR watch_data_address
= 0;
1467 /* Get the thread's resume state. */
1468 enum resume_state
get_resume_state () const
1470 return m_resume_state
;
1473 /* Put the thread in the NOT_RESUMED state. */
1474 void set_not_resumed ()
1476 m_resume_state
= resume_state::NOT_RESUMED
;
1479 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1480 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1482 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1483 m_resumed_pending_vcont_info
.step
= step
;
1484 m_resumed_pending_vcont_info
.sig
= sig
;
1487 /* Get the information this thread's pending vCont-resumption.
1489 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1491 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1493 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1495 return m_resumed_pending_vcont_info
;
1498 /* Put the thread in the VCONT_RESUMED state. */
1501 m_resume_state
= resume_state::RESUMED
;
1505 /* Resume state for this thread. This is used to implement vCont action
1506 coalescing (only when the target operates in non-stop mode).
1508 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1509 which notes that this thread must be considered in the next commit_resume
1512 remote_target::commit_resume sends a vCont packet with actions for the
1513 threads in the RESUMED_PENDING_VCONT state and moves them to the
1514 VCONT_RESUMED state.
1516 When reporting a stop to the core for a thread, that thread is moved back
1517 to the NOT_RESUMED state. */
1518 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1520 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1521 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1524 remote_state::remote_state ()
1529 remote_state::~remote_state ()
1531 xfree (this->last_pass_packet
);
1532 xfree (this->last_program_signals_packet
);
1533 xfree (this->finished_object
);
1534 xfree (this->finished_annex
);
1537 /* Utility: generate error from an incoming stub packet. */
1539 trace_error (char *buf
)
1542 return; /* not an error msg */
1545 case '1': /* malformed packet error */
1546 if (*++buf
== '0') /* general case: */
1547 error (_("remote.c: error in outgoing packet."));
1549 error (_("remote.c: error in outgoing packet at field #%ld."),
1550 strtol (buf
, NULL
, 16));
1552 error (_("Target returns error code '%s'."), buf
);
1556 /* Utility: wait for reply from stub, while accepting "O" packets. */
1559 remote_target::remote_get_noisy_reply ()
1561 struct remote_state
*rs
= get_remote_state ();
1563 do /* Loop on reply from remote stub. */
1567 QUIT
; /* Allow user to bail out with ^C. */
1568 getpkt (&rs
->buf
, 0);
1569 buf
= rs
->buf
.data ();
1572 else if (startswith (buf
, "qRelocInsn:"))
1575 CORE_ADDR from
, to
, org_to
;
1577 int adjusted_size
= 0;
1580 p
= buf
+ strlen ("qRelocInsn:");
1581 pp
= unpack_varlen_hex (p
, &ul
);
1583 error (_("invalid qRelocInsn packet: %s"), buf
);
1587 unpack_varlen_hex (p
, &ul
);
1594 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
1597 catch (const gdb_exception
&ex
)
1599 if (ex
.error
== MEMORY_ERROR
)
1601 /* Propagate memory errors silently back to the
1602 target. The stub may have limited the range of
1603 addresses we can write to, for example. */
1607 /* Something unexpectedly bad happened. Be verbose
1608 so we can tell what, and propagate the error back
1609 to the stub, so it doesn't get stuck waiting for
1611 exception_fprintf (gdb_stderr
, ex
,
1612 _("warning: relocating instruction: "));
1619 adjusted_size
= to
- org_to
;
1621 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1625 else if (buf
[0] == 'O' && buf
[1] != 'K')
1626 remote_console_output (buf
+ 1); /* 'O' message from stub */
1628 return buf
; /* Here's the actual reply. */
1633 struct remote_arch_state
*
1634 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1636 remote_arch_state
*rsa
;
1638 auto it
= this->m_arch_states
.find (gdbarch
);
1639 if (it
== this->m_arch_states
.end ())
1641 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1642 std::forward_as_tuple (gdbarch
),
1643 std::forward_as_tuple (gdbarch
));
1644 rsa
= &p
.first
->second
;
1646 /* Make sure that the packet buffer is plenty big enough for
1647 this architecture. */
1648 if (this->buf
.size () < rsa
->remote_packet_size
)
1649 this->buf
.resize (2 * rsa
->remote_packet_size
);
1657 /* Fetch the global remote target state. */
1660 remote_target::get_remote_state ()
1662 /* Make sure that the remote architecture state has been
1663 initialized, because doing so might reallocate rs->buf. Any
1664 function which calls getpkt also needs to be mindful of changes
1665 to rs->buf, but this call limits the number of places which run
1667 m_remote_state
.get_remote_arch_state (target_gdbarch ());
1669 return &m_remote_state
;
1672 /* Fetch the remote exec-file from the current program space. */
1675 get_remote_exec_file (void)
1677 char *remote_exec_file
;
1679 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1680 if (remote_exec_file
== NULL
)
1683 return remote_exec_file
;
1686 /* Set the remote exec file for PSPACE. */
1689 set_pspace_remote_exec_file (struct program_space
*pspace
,
1690 const char *remote_exec_file
)
1692 char *old_file
= remote_pspace_data
.get (pspace
);
1695 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1698 /* The "set/show remote exec-file" set command hook. */
1701 set_remote_exec_file (const char *ignored
, int from_tty
,
1702 struct cmd_list_element
*c
)
1704 set_pspace_remote_exec_file (current_program_space
,
1705 remote_exec_file_var
.c_str ());
1708 /* The "set/show remote exec-file" show command hook. */
1711 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1712 struct cmd_list_element
*cmd
, const char *value
)
1714 gdb_printf (file
, "%s\n", get_remote_exec_file ());
1718 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1720 int regnum
, num_remote_regs
, offset
;
1721 struct packet_reg
**remote_regs
;
1723 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1725 struct packet_reg
*r
= ®s
[regnum
];
1727 if (register_size (gdbarch
, regnum
) == 0)
1728 /* Do not try to fetch zero-sized (placeholder) registers. */
1731 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1736 /* Define the g/G packet format as the contents of each register
1737 with a remote protocol number, in order of ascending protocol
1740 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1741 for (num_remote_regs
= 0, regnum
= 0;
1742 regnum
< gdbarch_num_regs (gdbarch
);
1744 if (regs
[regnum
].pnum
!= -1)
1745 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1747 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1748 [] (const packet_reg
*a
, const packet_reg
*b
)
1749 { return a
->pnum
< b
->pnum
; });
1751 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1753 remote_regs
[regnum
]->in_g_packet
= 1;
1754 remote_regs
[regnum
]->offset
= offset
;
1755 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1761 /* Given the architecture described by GDBARCH, return the remote
1762 protocol register's number and the register's offset in the g/G
1763 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1764 If the target does not have a mapping for REGNUM, return false,
1765 otherwise, return true. */
1768 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1769 int *pnum
, int *poffset
)
1771 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1773 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1775 map_regcache_remote_table (gdbarch
, regs
.data ());
1777 *pnum
= regs
[regnum
].pnum
;
1778 *poffset
= regs
[regnum
].offset
;
1783 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1785 /* Use the architecture to build a regnum<->pnum table, which will be
1786 1:1 unless a feature set specifies otherwise. */
1787 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1789 /* Record the maximum possible size of the g packet - it may turn out
1791 this->sizeof_g_packet
1792 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1794 /* Default maximum number of characters in a packet body. Many
1795 remote stubs have a hardwired buffer size of 400 bytes
1796 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1797 as the maximum packet-size to ensure that the packet and an extra
1798 NUL character can always fit in the buffer. This stops GDB
1799 trashing stubs that try to squeeze an extra NUL into what is
1800 already a full buffer (As of 1999-12-04 that was most stubs). */
1801 this->remote_packet_size
= 400 - 1;
1803 /* This one is filled in when a ``g'' packet is received. */
1804 this->actual_register_packet_size
= 0;
1806 /* Should rsa->sizeof_g_packet needs more space than the
1807 default, adjust the size accordingly. Remember that each byte is
1808 encoded as two characters. 32 is the overhead for the packet
1809 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1810 (``$NN:G...#NN'') is a better guess, the below has been padded a
1812 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1813 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1816 /* Get a pointer to the current remote target. If not connected to a
1817 remote target, return NULL. */
1819 static remote_target
*
1820 get_current_remote_target ()
1822 target_ops
*proc_target
= current_inferior ()->process_target ();
1823 return dynamic_cast<remote_target
*> (proc_target
);
1826 /* Return the current allowed size of a remote packet. This is
1827 inferred from the current architecture, and should be used to
1828 limit the length of outgoing packets. */
1830 remote_target::get_remote_packet_size ()
1832 struct remote_state
*rs
= get_remote_state ();
1833 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1835 if (rs
->explicit_packet_size
)
1836 return rs
->explicit_packet_size
;
1838 return rsa
->remote_packet_size
;
1841 static struct packet_reg
*
1842 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1845 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
1849 struct packet_reg
*r
= &rsa
->regs
[regnum
];
1851 gdb_assert (r
->regnum
== regnum
);
1856 static struct packet_reg
*
1857 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1862 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1864 struct packet_reg
*r
= &rsa
->regs
[i
];
1866 if (r
->pnum
== pnum
)
1872 /* Allow the user to specify what sequence to send to the remote
1873 when he requests a program interruption: Although ^C is usually
1874 what remote systems expect (this is the default, here), it is
1875 sometimes preferable to send a break. On other systems such
1876 as the Linux kernel, a break followed by g, which is Magic SysRq g
1877 is required in order to interrupt the execution. */
1878 const char interrupt_sequence_control_c
[] = "Ctrl-C";
1879 const char interrupt_sequence_break
[] = "BREAK";
1880 const char interrupt_sequence_break_g
[] = "BREAK-g";
1881 static const char *const interrupt_sequence_modes
[] =
1883 interrupt_sequence_control_c
,
1884 interrupt_sequence_break
,
1885 interrupt_sequence_break_g
,
1888 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
1891 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
1892 struct cmd_list_element
*c
,
1895 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
1897 _("Send the ASCII ETX character (Ctrl-c) "
1898 "to the remote target to interrupt the "
1899 "execution of the program.\n"));
1900 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
1902 _("send a break signal to the remote target "
1903 "to interrupt the execution of the program.\n"));
1904 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
1906 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1907 "the remote target to interrupt the execution "
1908 "of Linux kernel.\n"));
1910 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
1911 interrupt_sequence_mode
);
1914 /* This boolean variable specifies whether interrupt_sequence is sent
1915 to the remote target when gdb connects to it.
1916 This is mostly needed when you debug the Linux kernel: The Linux kernel
1917 expects BREAK g which is Magic SysRq g for connecting gdb. */
1918 static bool interrupt_on_connect
= false;
1920 /* This variable is used to implement the "set/show remotebreak" commands.
1921 Since these commands are now deprecated in favor of "set/show remote
1922 interrupt-sequence", it no longer has any effect on the code. */
1923 static bool remote_break
;
1926 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
1929 interrupt_sequence_mode
= interrupt_sequence_break
;
1931 interrupt_sequence_mode
= interrupt_sequence_control_c
;
1935 show_remotebreak (struct ui_file
*file
, int from_tty
,
1936 struct cmd_list_element
*c
,
1941 /* This variable sets the number of bits in an address that are to be
1942 sent in a memory ("M" or "m") packet. Normally, after stripping
1943 leading zeros, the entire address would be sent. This variable
1944 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1945 initial implementation of remote.c restricted the address sent in
1946 memory packets to ``host::sizeof long'' bytes - (typically 32
1947 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1948 address was never sent. Since fixing this bug may cause a break in
1949 some remote targets this variable is principally provided to
1950 facilitate backward compatibility. */
1952 static unsigned int remote_address_size
;
1955 /* The default max memory-write-packet-size, when the setting is
1956 "fixed". The 16k is historical. (It came from older GDB's using
1957 alloca for buffers and the knowledge (folklore?) that some hosts
1958 don't cope very well with large alloca calls.) */
1959 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
1961 /* The minimum remote packet size for memory transfers. Ensures we
1962 can write at least one byte. */
1963 #define MIN_MEMORY_PACKET_SIZE 20
1965 /* Get the memory packet size, assuming it is fixed. */
1968 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
1970 gdb_assert (config
->fixed_p
);
1972 if (config
->size
<= 0)
1973 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
1975 return config
->size
;
1978 /* Compute the current size of a read/write packet. Since this makes
1979 use of ``actual_register_packet_size'' the computation is dynamic. */
1982 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
1984 struct remote_state
*rs
= get_remote_state ();
1985 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1988 if (config
->fixed_p
)
1989 what_they_get
= get_fixed_memory_packet_size (config
);
1992 what_they_get
= get_remote_packet_size ();
1993 /* Limit the packet to the size specified by the user. */
1994 if (config
->size
> 0
1995 && what_they_get
> config
->size
)
1996 what_they_get
= config
->size
;
1998 /* Limit it to the size of the targets ``g'' response unless we have
1999 permission from the stub to use a larger packet size. */
2000 if (rs
->explicit_packet_size
== 0
2001 && rsa
->actual_register_packet_size
> 0
2002 && what_they_get
> rsa
->actual_register_packet_size
)
2003 what_they_get
= rsa
->actual_register_packet_size
;
2005 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
2006 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
2008 /* Make sure there is room in the global buffer for this packet
2009 (including its trailing NUL byte). */
2010 if (rs
->buf
.size () < what_they_get
+ 1)
2011 rs
->buf
.resize (2 * what_they_get
);
2013 return what_they_get
;
2016 /* Update the size of a read/write packet. If they user wants
2017 something really big then do a sanity check. */
2020 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
,
2021 bool target_connected
)
2023 int fixed_p
= config
->fixed_p
;
2024 long size
= config
->size
;
2027 error (_("Argument required (integer, \"fixed\" or \"limit\")."));
2028 else if (strcmp (args
, "hard") == 0
2029 || strcmp (args
, "fixed") == 0)
2031 else if (strcmp (args
, "soft") == 0
2032 || strcmp (args
, "limit") == 0)
2038 size
= strtoul (args
, &end
, 0);
2040 error (_("Invalid %s (bad syntax)."), config
->name
);
2042 /* Instead of explicitly capping the size of a packet to or
2043 disallowing it, the user is allowed to set the size to
2044 something arbitrarily large. */
2048 if (fixed_p
&& !config
->fixed_p
)
2050 /* So that the query shows the correct value. */
2051 long query_size
= (size
<= 0
2052 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
2055 if (target_connected
2056 && !query (_("The target may not be able to correctly handle a %s\n"
2057 "of %ld bytes. Change the packet size? "),
2058 config
->name
, query_size
))
2059 error (_("Packet size not changed."));
2060 else if (!target_connected
2061 && !query (_("Future remote targets may not be able to "
2062 "correctly handle a %s\nof %ld bytes. Change the "
2063 "packet size for future remote targets? "),
2064 config
->name
, query_size
))
2065 error (_("Packet size not changed."));
2067 /* Update the config. */
2068 config
->fixed_p
= fixed_p
;
2069 config
->size
= size
;
2071 const char *target_type
= get_target_type_name (target_connected
);
2072 gdb_printf (_("The %s %s is set to \"%s\".\n"), config
->name
, target_type
,
2077 /* Show the memory-read or write-packet size configuration CONFIG of the
2078 target REMOTE. If REMOTE is nullptr, the default configuration for future
2079 remote targets should be passed in CONFIG. */
2082 show_memory_packet_size (memory_packet_config
*config
, remote_target
*remote
)
2084 const char *target_type
= get_target_type_name (remote
!= nullptr);
2086 if (config
->size
== 0)
2087 gdb_printf (_("The %s %s is 0 (default). "), config
->name
, target_type
);
2089 gdb_printf (_("The %s %s is %ld. "), config
->name
, target_type
,
2092 if (config
->fixed_p
)
2093 gdb_printf (_("Packets are fixed at %ld bytes.\n"),
2094 get_fixed_memory_packet_size (config
));
2097 if (remote
!= nullptr)
2098 gdb_printf (_("Packets are limited to %ld bytes.\n"),
2099 remote
->get_memory_packet_size (config
));
2101 gdb_puts ("The actual limit will be further reduced "
2102 "dependent on the target.\n");
2106 /* Configure the memory-write-packet size of the currently selected target. If
2107 no target is available, the default configuration for future remote targets
2111 set_memory_write_packet_size (const char *args
, int from_tty
)
2113 remote_target
*remote
= get_current_remote_target ();
2114 if (remote
!= nullptr)
2116 set_memory_packet_size
2117 (args
, &remote
->m_features
.m_memory_write_packet_config
, true);
2121 memory_packet_config
* config
= &memory_write_packet_config
;
2122 set_memory_packet_size (args
, config
, false);
2126 /* Display the memory-write-packet size of the currently selected target. If
2127 no target is available, the default configuration for future remote targets
2131 show_memory_write_packet_size (const char *args
, int from_tty
)
2133 remote_target
*remote
= get_current_remote_target ();
2134 if (remote
!= nullptr)
2135 show_memory_packet_size (&remote
->m_features
.m_memory_write_packet_config
,
2138 show_memory_packet_size (&memory_write_packet_config
, nullptr);
2141 /* Show the number of hardware watchpoints that can be used. */
2144 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
2145 struct cmd_list_element
*c
,
2148 gdb_printf (file
, _("The maximum number of target hardware "
2149 "watchpoints is %s.\n"), value
);
2152 /* Show the length limit (in bytes) for hardware watchpoints. */
2155 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
2156 struct cmd_list_element
*c
,
2159 gdb_printf (file
, _("The maximum length (in bytes) of a target "
2160 "hardware watchpoint is %s.\n"), value
);
2163 /* Show the number of hardware breakpoints that can be used. */
2166 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
2167 struct cmd_list_element
*c
,
2170 gdb_printf (file
, _("The maximum number of target hardware "
2171 "breakpoints is %s.\n"), value
);
2174 /* Controls the maximum number of characters to display in the debug output
2175 for each remote packet. The remaining characters are omitted. */
2177 static int remote_packet_max_chars
= 512;
2179 /* Show the maximum number of characters to display for each remote packet
2180 when remote debugging is enabled. */
2183 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
2184 struct cmd_list_element
*c
,
2187 gdb_printf (file
, _("Number of remote packet characters to "
2188 "display is %s.\n"), value
);
2192 remote_target::get_memory_write_packet_size ()
2194 return get_memory_packet_size (&m_features
.m_memory_write_packet_config
);
2197 /* Configure the memory-read-packet size of the currently selected target. If
2198 no target is available, the default configuration for future remote targets
2202 set_memory_read_packet_size (const char *args
, int from_tty
)
2204 remote_target
*remote
= get_current_remote_target ();
2205 if (remote
!= nullptr)
2206 set_memory_packet_size
2207 (args
, &remote
->m_features
.m_memory_read_packet_config
, true);
2210 memory_packet_config
* config
= &memory_read_packet_config
;
2211 set_memory_packet_size (args
, config
, false);
2216 /* Display the memory-read-packet size of the currently selected target. If
2217 no target is available, the default configuration for future remote targets
2221 show_memory_read_packet_size (const char *args
, int from_tty
)
2223 remote_target
*remote
= get_current_remote_target ();
2224 if (remote
!= nullptr)
2225 show_memory_packet_size (&remote
->m_features
.m_memory_read_packet_config
,
2228 show_memory_packet_size (&memory_read_packet_config
, nullptr);
2232 remote_target::get_memory_read_packet_size ()
2234 long size
= get_memory_packet_size (&m_features
.m_memory_read_packet_config
);
2236 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
2237 extra buffer size argument before the memory read size can be
2238 increased beyond this. */
2239 if (size
> get_remote_packet_size ())
2240 size
= get_remote_packet_size ();
2244 static enum packet_support
packet_config_support (const packet_config
*config
);
2248 set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
2249 cmd_list_element
*c
)
2251 remote_target
*remote
= get_current_remote_target ();
2252 gdb_assert (c
->var
.has_value ());
2254 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2255 const int packet_idx
= std::distance (remote_protocol_packets
,
2258 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2260 const char *name
= packets_descriptions
[packet_idx
].name
;
2261 const auto_boolean value
= c
->var
->get
<auto_boolean
> ();
2262 const char *support
= get_packet_support_name (value
);
2263 const char *target_type
= get_target_type_name (remote
!= nullptr);
2265 if (remote
!= nullptr)
2266 remote
->m_features
.m_protocol_packets
[packet_idx
].detect
= value
;
2268 remote_protocol_packets
[packet_idx
].detect
= value
;
2270 gdb_printf (_("Support for the '%s' packet %s is set to \"%s\".\n"), name
,
2271 target_type
, support
);
2275 internal_error (_("Could not find config for %s"), c
->name
);
2279 show_packet_config_cmd (ui_file
*file
, const unsigned int which_packet
,
2280 remote_target
*remote
)
2282 const char *support
= "internal-error";
2283 const char *target_type
= get_target_type_name (remote
!= nullptr);
2285 packet_config
*config
;
2286 if (remote
!= nullptr)
2287 config
= &remote
->m_features
.m_protocol_packets
[which_packet
];
2289 config
= &remote_protocol_packets
[which_packet
];
2291 switch (packet_config_support (config
))
2294 support
= "enabled";
2296 case PACKET_DISABLE
:
2297 support
= "disabled";
2299 case PACKET_SUPPORT_UNKNOWN
:
2300 support
= "unknown";
2303 switch (config
->detect
)
2305 case AUTO_BOOLEAN_AUTO
:
2307 _("Support for the '%s' packet %s is \"auto\", "
2309 packets_descriptions
[which_packet
].name
, target_type
,
2312 case AUTO_BOOLEAN_TRUE
:
2313 case AUTO_BOOLEAN_FALSE
:
2315 _("Support for the '%s' packet %s is \"%s\".\n"),
2316 packets_descriptions
[which_packet
].name
, target_type
,
2317 get_packet_support_name (config
->detect
));
2323 add_packet_config_cmd (const unsigned int which_packet
, const char *name
,
2324 const char *title
, int legacy
)
2326 packets_descriptions
[which_packet
].name
= name
;
2327 packets_descriptions
[which_packet
].title
= title
;
2329 packet_config
*config
= &remote_protocol_packets
[which_packet
];
2331 gdb::unique_xmalloc_ptr
<char> set_doc
2332 = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
2334 gdb::unique_xmalloc_ptr
<char> show_doc
2335 = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
2337 /* set/show TITLE-packet {auto,on,off} */
2338 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
2339 set_show_commands cmds
2340 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
2341 &config
->detect
, set_doc
.get (),
2342 show_doc
.get (), NULL
, /* help_doc */
2343 set_remote_protocol_packet_cmd
,
2344 show_remote_protocol_packet_cmd
,
2345 &remote_set_cmdlist
, &remote_show_cmdlist
);
2346 cmds
.show
->set_context (config
);
2347 cmds
.set
->set_context (config
);
2349 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
2352 /* It's not clear who should take ownership of the LEGACY_NAME string
2353 created below, so, for now, place the string into a static vector
2354 which ensures the strings is released when GDB exits. */
2355 static std::vector
<gdb::unique_xmalloc_ptr
<char>> legacy_names
;
2356 gdb::unique_xmalloc_ptr
<char> legacy_name
2357 = xstrprintf ("%s-packet", name
);
2358 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
2359 &remote_set_cmdlist
);
2360 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
2361 &remote_show_cmdlist
);
2362 legacy_names
.emplace_back (std::move (legacy_name
));
2366 static enum packet_result
2367 packet_check_result (const char *buf
)
2371 /* The stub recognized the packet request. Check that the
2372 operation succeeded. */
2374 && isxdigit (buf
[1]) && isxdigit (buf
[2])
2376 /* "Enn" - definitely an error. */
2377 return PACKET_ERROR
;
2379 /* Always treat "E." as an error. This will be used for
2380 more verbose error messages, such as E.memtypes. */
2381 if (buf
[0] == 'E' && buf
[1] == '.')
2382 return PACKET_ERROR
;
2384 /* The packet may or may not be OK. Just assume it is. */
2388 /* The stub does not support the packet. */
2389 return PACKET_UNKNOWN
;
2392 static enum packet_result
2393 packet_check_result (const gdb::char_vector
&buf
)
2395 return packet_check_result (buf
.data ());
2399 remote_features::packet_ok (const char *buf
, const int which_packet
)
2401 packet_config
*config
= &m_protocol_packets
[which_packet
];
2402 packet_description
*descr
= &packets_descriptions
[which_packet
];
2404 enum packet_result result
;
2406 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2407 && config
->support
== PACKET_DISABLE
)
2408 internal_error (_("packet_ok: attempt to use a disabled packet"));
2410 result
= packet_check_result (buf
);
2415 /* The stub recognized the packet request. */
2416 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2418 remote_debug_printf ("Packet %s (%s) is supported",
2419 descr
->name
, descr
->title
);
2420 config
->support
= PACKET_ENABLE
;
2423 case PACKET_UNKNOWN
:
2424 /* The stub does not support the packet. */
2425 if (config
->detect
== AUTO_BOOLEAN_AUTO
2426 && config
->support
== PACKET_ENABLE
)
2428 /* If the stub previously indicated that the packet was
2429 supported then there is a protocol error. */
2430 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2431 descr
->name
, descr
->title
);
2433 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2435 /* The user set it wrong. */
2436 error (_("Enabled packet %s (%s) not recognized by stub"),
2437 descr
->name
, descr
->title
);
2440 remote_debug_printf ("Packet %s (%s) is NOT supported", descr
->name
,
2442 config
->support
= PACKET_DISABLE
;
2450 remote_features::packet_ok (const gdb::char_vector
&buf
, const int which_packet
)
2452 return packet_ok (buf
.data (), which_packet
);
2455 /* Returns whether a given packet or feature is supported. This takes
2456 into account the state of the corresponding "set remote foo-packet"
2457 command, which may be used to bypass auto-detection. */
2459 static enum packet_support
2460 packet_config_support (const packet_config
*config
)
2462 switch (config
->detect
)
2464 case AUTO_BOOLEAN_TRUE
:
2465 return PACKET_ENABLE
;
2466 case AUTO_BOOLEAN_FALSE
:
2467 return PACKET_DISABLE
;
2468 case AUTO_BOOLEAN_AUTO
:
2469 return config
->support
;
2471 gdb_assert_not_reached ("bad switch");
2476 remote_features::packet_support (int packet
) const
2478 const packet_config
*config
= &m_protocol_packets
[packet
];
2479 return packet_config_support (config
);
2483 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2484 struct cmd_list_element
*c
,
2487 remote_target
*remote
= get_current_remote_target ();
2488 gdb_assert (c
->var
.has_value ());
2490 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2491 const int packet_idx
= std::distance (remote_protocol_packets
,
2494 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2496 show_packet_config_cmd (file
, packet_idx
, remote
);
2499 internal_error (_("Could not find config for %s"), c
->name
);
2502 /* Should we try one of the 'Z' requests? */
2506 Z_PACKET_SOFTWARE_BP
,
2507 Z_PACKET_HARDWARE_BP
,
2514 /* For compatibility with older distributions. Provide a ``set remote
2515 Z-packet ...'' command that updates all the Z packet types. */
2517 static enum auto_boolean remote_Z_packet_detect
;
2520 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2521 struct cmd_list_element
*c
)
2523 remote_target
*remote
= get_current_remote_target ();
2526 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2528 if (remote
!= nullptr)
2529 remote
->m_features
.m_protocol_packets
[PACKET_Z0
+ i
].detect
2530 = remote_Z_packet_detect
;
2532 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2535 const char *support
= get_packet_support_name (remote_Z_packet_detect
);
2536 const char *target_type
= get_target_type_name (remote
!= nullptr);
2537 gdb_printf (_("Use of Z packets %s is set to \"%s\".\n"), target_type
,
2543 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2544 struct cmd_list_element
*c
,
2547 remote_target
*remote
= get_current_remote_target ();
2550 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2551 show_packet_config_cmd (file
, PACKET_Z0
+ i
, remote
);
2554 /* Insert fork catchpoint target routine. If fork events are enabled
2555 then return success, nothing more to do. */
2558 remote_target::insert_fork_catchpoint (int pid
)
2560 return !m_features
.remote_fork_event_p ();
2563 /* Remove fork catchpoint target routine. Nothing to do, just
2567 remote_target::remove_fork_catchpoint (int pid
)
2572 /* Insert vfork catchpoint target routine. If vfork events are enabled
2573 then return success, nothing more to do. */
2576 remote_target::insert_vfork_catchpoint (int pid
)
2578 return !m_features
.remote_vfork_event_p ();
2581 /* Remove vfork catchpoint target routine. Nothing to do, just
2585 remote_target::remove_vfork_catchpoint (int pid
)
2590 /* Insert exec catchpoint target routine. If exec events are
2591 enabled, just return success. */
2594 remote_target::insert_exec_catchpoint (int pid
)
2596 return !m_features
.remote_exec_event_p ();
2599 /* Remove exec catchpoint target routine. Nothing to do, just
2603 remote_target::remove_exec_catchpoint (int pid
)
2610 /* Take advantage of the fact that the TID field is not used, to tag
2611 special ptids with it set to != 0. */
2612 static const ptid_t
magic_null_ptid (42000, -1, 1);
2613 static const ptid_t
not_sent_ptid (42000, -2, 1);
2614 static const ptid_t
any_thread_ptid (42000, 0, 1);
2616 /* Find out if the stub attached to PID (and hence GDB should offer to
2617 detach instead of killing it when bailing out). */
2620 remote_target::remote_query_attached (int pid
)
2622 struct remote_state
*rs
= get_remote_state ();
2623 size_t size
= get_remote_packet_size ();
2625 if (m_features
.packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2628 if (m_features
.remote_multi_process_p ())
2629 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2631 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2634 getpkt (&rs
->buf
, 0);
2636 switch (m_features
.packet_ok (rs
->buf
, PACKET_qAttached
))
2639 if (strcmp (rs
->buf
.data (), "1") == 0)
2643 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
2645 case PACKET_UNKNOWN
:
2652 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2653 has been invented by GDB, instead of reported by the target. Since
2654 we can be connected to a remote system before before knowing about
2655 any inferior, mark the target with execution when we find the first
2656 inferior. If ATTACHED is 1, then we had just attached to this
2657 inferior. If it is 0, then we just created this inferior. If it
2658 is -1, then try querying the remote stub to find out if it had
2659 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2660 attempt to open this inferior's executable as the main executable
2661 if no main executable is open already. */
2664 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2667 struct inferior
*inf
;
2669 /* Check whether this process we're learning about is to be
2670 considered attached, or if is to be considered to have been
2671 spawned by the stub. */
2673 attached
= remote_query_attached (pid
);
2675 if (gdbarch_has_global_solist (target_gdbarch ()))
2677 /* If the target shares code across all inferiors, then every
2678 attach adds a new inferior. */
2679 inf
= add_inferior (pid
);
2681 /* ... and every inferior is bound to the same program space.
2682 However, each inferior may still have its own address
2684 inf
->aspace
= maybe_new_address_space ();
2685 inf
->pspace
= current_program_space
;
2689 /* In the traditional debugging scenario, there's a 1-1 match
2690 between program/address spaces. We simply bind the inferior
2691 to the program space's address space. */
2692 inf
= current_inferior ();
2694 /* However, if the current inferior is already bound to a
2695 process, find some other empty inferior. */
2699 for (inferior
*it
: all_inferiors ())
2708 /* Since all inferiors were already bound to a process, add
2710 inf
= add_inferior_with_spaces ();
2712 switch_to_inferior_no_thread (inf
);
2713 inf
->push_target (this);
2714 inferior_appeared (inf
, pid
);
2717 inf
->attach_flag
= attached
;
2718 inf
->fake_pid_p
= fake_pid_p
;
2720 /* If no main executable is currently open then attempt to
2721 open the file that was executed to create this inferior. */
2722 if (try_open_exec
&& get_exec_file (0) == NULL
)
2723 exec_file_locate_attach (pid
, 0, 1);
2725 /* Check for exec file mismatch, and let the user solve it. */
2726 validate_exec_file (1);
2731 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2732 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2735 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2736 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2737 remote_state::starting_up flag) is true then the new thread is added
2738 silently, otherwise the new thread will be announced to the user. */
2741 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2744 struct remote_state
*rs
= get_remote_state ();
2745 struct thread_info
*thread
;
2747 /* GDB historically didn't pull threads in the initial connection
2748 setup. If the remote target doesn't even have a concept of
2749 threads (e.g., a bare-metal target), even if internally we
2750 consider that a single-threaded target, mentioning a new thread
2751 might be confusing to the user. Be silent then, preserving the
2752 age old behavior. */
2753 if (rs
->starting_up
|| silent_p
)
2754 thread
= add_thread_silent (this, ptid
);
2756 thread
= add_thread (this, ptid
);
2758 /* We start by assuming threads are resumed. That state then gets updated
2759 when we process a matching stop reply. */
2760 get_remote_thread_info (thread
)->set_resumed ();
2762 set_executing (this, ptid
, executing
);
2763 set_running (this, ptid
, running
);
2768 /* Come here when we learn about a thread id from the remote target.
2769 It may be the first time we hear about such thread, so take the
2770 opportunity to add it to GDB's thread list. In case this is the
2771 first time we're noticing its corresponding inferior, add it to
2772 GDB's inferior list as well. EXECUTING indicates whether the
2773 thread is (internally) executing or stopped. */
2776 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2778 /* In non-stop mode, we assume new found threads are (externally)
2779 running until proven otherwise with a stop reply. In all-stop,
2780 we can only get here if all threads are stopped. */
2781 bool running
= target_is_non_stop_p ();
2783 /* If this is a new thread, add it to GDB's thread list.
2784 If we leave it up to WFI to do this, bad things will happen. */
2786 thread_info
*tp
= this->find_thread (currthread
);
2787 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2789 /* We're seeing an event on a thread id we knew had exited.
2790 This has to be a new thread reusing the old id. Add it. */
2791 remote_add_thread (currthread
, running
, executing
, false);
2795 if (!in_thread_list (this, currthread
))
2797 struct inferior
*inf
= NULL
;
2798 int pid
= currthread
.pid ();
2800 if (inferior_ptid
.is_pid ()
2801 && pid
== inferior_ptid
.pid ())
2803 /* inferior_ptid has no thread member yet. This can happen
2804 with the vAttach -> remote_wait,"TAAthread:" path if the
2805 stub doesn't support qC. This is the first stop reported
2806 after an attach, so this is the main thread. Update the
2807 ptid in the thread list. */
2808 if (in_thread_list (this, ptid_t (pid
)))
2809 thread_change_ptid (this, inferior_ptid
, currthread
);
2813 = remote_add_thread (currthread
, running
, executing
, false);
2814 switch_to_thread (thr
);
2819 if (magic_null_ptid
== inferior_ptid
)
2821 /* inferior_ptid is not set yet. This can happen with the
2822 vRun -> remote_wait,"TAAthread:" path if the stub
2823 doesn't support qC. This is the first stop reported
2824 after an attach, so this is the main thread. Update the
2825 ptid in the thread list. */
2826 thread_change_ptid (this, inferior_ptid
, currthread
);
2830 /* When connecting to a target remote, or to a target
2831 extended-remote which already was debugging an inferior, we
2832 may not know about it yet. Add it before adding its child
2833 thread, so notifications are emitted in a sensible order. */
2834 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
2836 bool fake_pid_p
= !m_features
.remote_multi_process_p ();
2838 inf
= remote_add_inferior (fake_pid_p
,
2839 currthread
.pid (), -1, 1);
2842 /* This is really a new thread. Add it. */
2843 thread_info
*new_thr
2844 = remote_add_thread (currthread
, running
, executing
, false);
2846 /* If we found a new inferior, let the common code do whatever
2847 it needs to with it (e.g., read shared libraries, insert
2848 breakpoints), unless we're just setting up an all-stop
2852 struct remote_state
*rs
= get_remote_state ();
2854 if (!rs
->starting_up
)
2855 notice_new_inferior (new_thr
, executing
, 0);
2860 /* Return THREAD's private thread data, creating it if necessary. */
2862 static remote_thread_info
*
2863 get_remote_thread_info (thread_info
*thread
)
2865 gdb_assert (thread
!= NULL
);
2867 if (thread
->priv
== NULL
)
2868 thread
->priv
.reset (new remote_thread_info
);
2870 return gdb::checked_static_cast
<remote_thread_info
*> (thread
->priv
.get ());
2873 /* Return PTID's private thread data, creating it if necessary. */
2875 static remote_thread_info
*
2876 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
2878 thread_info
*thr
= target
->find_thread (ptid
);
2879 return get_remote_thread_info (thr
);
2882 /* Call this function as a result of
2883 1) A halt indication (T packet) containing a thread id
2884 2) A direct query of currthread
2885 3) Successful execution of set thread */
2888 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
2890 rs
->general_thread
= currthread
;
2893 /* If 'QPassSignals' is supported, tell the remote stub what signals
2894 it can simply pass through to the inferior without reporting. */
2897 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2899 if (m_features
.packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
2901 char *pass_packet
, *p
;
2903 struct remote_state
*rs
= get_remote_state ();
2905 gdb_assert (pass_signals
.size () < 256);
2906 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2908 if (pass_signals
[i
])
2911 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
2912 strcpy (pass_packet
, "QPassSignals:");
2913 p
= pass_packet
+ strlen (pass_packet
);
2914 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2916 if (pass_signals
[i
])
2919 *p
++ = tohex (i
>> 4);
2920 *p
++ = tohex (i
& 15);
2929 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
2931 putpkt (pass_packet
);
2932 getpkt (&rs
->buf
, 0);
2933 m_features
.packet_ok (rs
->buf
, PACKET_QPassSignals
);
2934 xfree (rs
->last_pass_packet
);
2935 rs
->last_pass_packet
= pass_packet
;
2938 xfree (pass_packet
);
2942 /* If 'QCatchSyscalls' is supported, tell the remote stub
2943 to report syscalls to GDB. */
2946 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
2947 gdb::array_view
<const int> syscall_counts
)
2949 const char *catch_packet
;
2950 enum packet_result result
;
2953 if (m_features
.packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
2955 /* Not supported. */
2959 if (needed
&& any_count
== 0)
2961 /* Count how many syscalls are to be caught. */
2962 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2964 if (syscall_counts
[i
] != 0)
2969 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
2970 pid
, needed
, any_count
, n_sysno
);
2972 std::string built_packet
;
2975 /* Prepare a packet with the sysno list, assuming max 8+1
2976 characters for a sysno. If the resulting packet size is too
2977 big, fallback on the non-selective packet. */
2978 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
2979 built_packet
.reserve (maxpktsz
);
2980 built_packet
= "QCatchSyscalls:1";
2983 /* Add in each syscall to be caught. */
2984 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2986 if (syscall_counts
[i
] != 0)
2987 string_appendf (built_packet
, ";%zx", i
);
2990 if (built_packet
.size () > get_remote_packet_size ())
2992 /* catch_packet too big. Fallback to less efficient
2993 non selective mode, with GDB doing the filtering. */
2994 catch_packet
= "QCatchSyscalls:1";
2997 catch_packet
= built_packet
.c_str ();
3000 catch_packet
= "QCatchSyscalls:0";
3002 struct remote_state
*rs
= get_remote_state ();
3004 putpkt (catch_packet
);
3005 getpkt (&rs
->buf
, 0);
3006 result
= m_features
.packet_ok (rs
->buf
, PACKET_QCatchSyscalls
);
3007 if (result
== PACKET_OK
)
3013 /* If 'QProgramSignals' is supported, tell the remote stub what
3014 signals it should pass through to the inferior when detaching. */
3017 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
3019 if (m_features
.packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
3023 struct remote_state
*rs
= get_remote_state ();
3025 gdb_assert (signals
.size () < 256);
3026 for (size_t i
= 0; i
< signals
.size (); i
++)
3031 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
3032 strcpy (packet
, "QProgramSignals:");
3033 p
= packet
+ strlen (packet
);
3034 for (size_t i
= 0; i
< signals
.size (); i
++)
3036 if (signal_pass_state (i
))
3039 *p
++ = tohex (i
>> 4);
3040 *p
++ = tohex (i
& 15);
3049 if (!rs
->last_program_signals_packet
3050 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
3053 getpkt (&rs
->buf
, 0);
3054 m_features
.packet_ok (rs
->buf
, PACKET_QProgramSignals
);
3055 xfree (rs
->last_program_signals_packet
);
3056 rs
->last_program_signals_packet
= packet
;
3063 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
3064 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
3065 thread. If GEN is set, set the general thread, if not, then set
3066 the step/continue thread. */
3068 remote_target::set_thread (ptid_t ptid
, int gen
)
3070 struct remote_state
*rs
= get_remote_state ();
3071 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
3072 char *buf
= rs
->buf
.data ();
3073 char *endbuf
= buf
+ get_remote_packet_size ();
3079 *buf
++ = gen
? 'g' : 'c';
3080 if (ptid
== magic_null_ptid
)
3081 xsnprintf (buf
, endbuf
- buf
, "0");
3082 else if (ptid
== any_thread_ptid
)
3083 xsnprintf (buf
, endbuf
- buf
, "0");
3084 else if (ptid
== minus_one_ptid
)
3085 xsnprintf (buf
, endbuf
- buf
, "-1");
3087 write_ptid (buf
, endbuf
, ptid
);
3089 getpkt (&rs
->buf
, 0);
3091 rs
->general_thread
= ptid
;
3093 rs
->continue_thread
= ptid
;
3097 remote_target::set_general_thread (ptid_t ptid
)
3099 set_thread (ptid
, 1);
3103 remote_target::set_continue_thread (ptid_t ptid
)
3105 set_thread (ptid
, 0);
3108 /* Change the remote current process. Which thread within the process
3109 ends up selected isn't important, as long as it is the same process
3110 as what INFERIOR_PTID points to.
3112 This comes from that fact that there is no explicit notion of
3113 "selected process" in the protocol. The selected process for
3114 general operations is the process the selected general thread
3118 remote_target::set_general_process ()
3120 /* If the remote can't handle multiple processes, don't bother. */
3121 if (!m_features
.remote_multi_process_p ())
3124 remote_state
*rs
= get_remote_state ();
3126 /* We only need to change the remote current thread if it's pointing
3127 at some other process. */
3128 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
3129 set_general_thread (inferior_ptid
);
3133 /* Return nonzero if this is the main thread that we made up ourselves
3134 to model non-threaded targets as single-threaded. */
3137 remote_thread_always_alive (ptid_t ptid
)
3139 if (ptid
== magic_null_ptid
)
3140 /* The main thread is always alive. */
3143 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
3144 /* The main thread is always alive. This can happen after a
3145 vAttach, if the remote side doesn't support
3152 /* Return nonzero if the thread PTID is still alive on the remote
3156 remote_target::thread_alive (ptid_t ptid
)
3158 struct remote_state
*rs
= get_remote_state ();
3161 /* Check if this is a thread that we made up ourselves to model
3162 non-threaded targets as single-threaded. */
3163 if (remote_thread_always_alive (ptid
))
3166 p
= rs
->buf
.data ();
3167 endp
= p
+ get_remote_packet_size ();
3170 write_ptid (p
, endp
, ptid
);
3173 getpkt (&rs
->buf
, 0);
3174 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
3177 /* Return a pointer to a thread name if we know it and NULL otherwise.
3178 The thread_info object owns the memory for the name. */
3181 remote_target::thread_name (struct thread_info
*info
)
3183 if (info
->priv
!= NULL
)
3185 const std::string
&name
= get_remote_thread_info (info
)->name
;
3186 return !name
.empty () ? name
.c_str () : NULL
;
3192 /* About these extended threadlist and threadinfo packets. They are
3193 variable length packets but, the fields within them are often fixed
3194 length. They are redundant enough to send over UDP as is the
3195 remote protocol in general. There is a matching unit test module
3198 /* WARNING: This threadref data structure comes from the remote O.S.,
3199 libstub protocol encoding, and remote.c. It is not particularly
3202 /* Right now, the internal structure is int. We want it to be bigger.
3203 Plan to fix this. */
3205 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3207 /* gdb_ext_thread_info is an internal GDB data structure which is
3208 equivalent to the reply of the remote threadinfo packet. */
3210 struct gdb_ext_thread_info
3212 threadref threadid
; /* External form of thread reference. */
3213 int active
; /* Has state interesting to GDB?
3215 char display
[256]; /* Brief state display, name,
3216 blocked/suspended. */
3217 char shortname
[32]; /* To be used to name threads. */
3218 char more_display
[256]; /* Long info, statistics, queue depth,
3222 /* The volume of remote transfers can be limited by submitting
3223 a mask containing bits specifying the desired information.
3224 Use a union of these values as the 'selection' parameter to
3225 get_thread_info. FIXME: Make these TAG names more thread specific. */
3227 #define TAG_THREADID 1
3228 #define TAG_EXISTS 2
3229 #define TAG_DISPLAY 4
3230 #define TAG_THREADNAME 8
3231 #define TAG_MOREDISPLAY 16
3233 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3235 static const char *unpack_nibble (const char *buf
, int *val
);
3237 static const char *unpack_byte (const char *buf
, int *value
);
3239 static char *pack_int (char *buf
, int value
);
3241 static const char *unpack_int (const char *buf
, int *value
);
3243 static const char *unpack_string (const char *src
, char *dest
, int length
);
3245 static char *pack_threadid (char *pkt
, threadref
*id
);
3247 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3249 void int_to_threadref (threadref
*id
, int value
);
3251 static int threadref_to_int (threadref
*ref
);
3253 static void copy_threadref (threadref
*dest
, threadref
*src
);
3255 static int threadmatch (threadref
*dest
, threadref
*src
);
3257 static char *pack_threadinfo_request (char *pkt
, int mode
,
3260 static char *pack_threadlist_request (char *pkt
, int startflag
,
3262 threadref
*nextthread
);
3264 static int remote_newthread_step (threadref
*ref
, void *context
);
3267 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3268 buffer we're allowed to write to. Returns
3269 BUF+CHARACTERS_WRITTEN. */
3272 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3276 if (m_features
.remote_multi_process_p ())
3280 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3282 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3286 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3288 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3293 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3294 last parsed char. Returns null_ptid if no thread id is found, and
3295 throws an error if the thread id has an invalid format. */
3298 read_ptid (const char *buf
, const char **obuf
)
3300 const char *p
= buf
;
3302 ULONGEST pid
= 0, tid
= 0;
3306 /* Multi-process ptid. */
3307 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3309 error (_("invalid remote ptid: %s"), p
);
3312 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3315 return ptid_t (pid
, tid
);
3318 /* No multi-process. Just a tid. */
3319 pp
= unpack_varlen_hex (p
, &tid
);
3321 /* Return null_ptid when no thread id is found. */
3329 /* Since the stub is not sending a process id, default to what's
3330 current_inferior, unless it doesn't have a PID yet. If so,
3331 then since there's no way to know the pid of the reported
3332 threads, use the magic number. */
3333 inferior
*inf
= current_inferior ();
3335 pid
= magic_null_ptid
.pid ();
3341 return ptid_t (pid
, tid
);
3347 if (ch
>= 'a' && ch
<= 'f')
3348 return ch
- 'a' + 10;
3349 if (ch
>= '0' && ch
<= '9')
3351 if (ch
>= 'A' && ch
<= 'F')
3352 return ch
- 'A' + 10;
3357 stub_unpack_int (const char *buff
, int fieldlength
)
3364 nibble
= stubhex (*buff
++);
3368 retval
= retval
<< 4;
3374 unpack_nibble (const char *buf
, int *val
)
3376 *val
= fromhex (*buf
++);
3381 unpack_byte (const char *buf
, int *value
)
3383 *value
= stub_unpack_int (buf
, 2);
3388 pack_int (char *buf
, int value
)
3390 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3391 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3392 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3393 buf
= pack_hex_byte (buf
, (value
& 0xff));
3398 unpack_int (const char *buf
, int *value
)
3400 *value
= stub_unpack_int (buf
, 8);
3404 #if 0 /* Currently unused, uncomment when needed. */
3405 static char *pack_string (char *pkt
, char *string
);
3408 pack_string (char *pkt
, char *string
)
3413 len
= strlen (string
);
3415 len
= 200; /* Bigger than most GDB packets, junk??? */
3416 pkt
= pack_hex_byte (pkt
, len
);
3420 if ((ch
== '\0') || (ch
== '#'))
3421 ch
= '*'; /* Protect encapsulation. */
3426 #endif /* 0 (unused) */
3429 unpack_string (const char *src
, char *dest
, int length
)
3438 pack_threadid (char *pkt
, threadref
*id
)
3441 unsigned char *altid
;
3443 altid
= (unsigned char *) id
;
3444 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3446 pkt
= pack_hex_byte (pkt
, *altid
++);
3452 unpack_threadid (const char *inbuf
, threadref
*id
)
3455 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3458 altref
= (char *) id
;
3460 while (inbuf
< limit
)
3462 x
= stubhex (*inbuf
++);
3463 y
= stubhex (*inbuf
++);
3464 *altref
++ = (x
<< 4) | y
;
3469 /* Externally, threadrefs are 64 bits but internally, they are still
3470 ints. This is due to a mismatch of specifications. We would like
3471 to use 64bit thread references internally. This is an adapter
3475 int_to_threadref (threadref
*id
, int value
)
3477 unsigned char *scan
;
3479 scan
= (unsigned char *) id
;
3485 *scan
++ = (value
>> 24) & 0xff;
3486 *scan
++ = (value
>> 16) & 0xff;
3487 *scan
++ = (value
>> 8) & 0xff;
3488 *scan
++ = (value
& 0xff);
3492 threadref_to_int (threadref
*ref
)
3495 unsigned char *scan
;
3501 value
= (value
<< 8) | ((*scan
++) & 0xff);
3506 copy_threadref (threadref
*dest
, threadref
*src
)
3509 unsigned char *csrc
, *cdest
;
3511 csrc
= (unsigned char *) src
;
3512 cdest
= (unsigned char *) dest
;
3519 threadmatch (threadref
*dest
, threadref
*src
)
3521 /* Things are broken right now, so just assume we got a match. */
3523 unsigned char *srcp
, *destp
;
3525 srcp
= (char *) src
;
3526 destp
= (char *) dest
;
3530 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3537 threadid:1, # always request threadid
3544 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3547 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3549 *pkt
++ = 'q'; /* Info Query */
3550 *pkt
++ = 'P'; /* process or thread info */
3551 pkt
= pack_int (pkt
, mode
); /* mode */
3552 pkt
= pack_threadid (pkt
, id
); /* threadid */
3553 *pkt
= '\0'; /* terminate */
3557 /* These values tag the fields in a thread info response packet. */
3558 /* Tagging the fields allows us to request specific fields and to
3559 add more fields as time goes by. */
3561 #define TAG_THREADID 1 /* Echo the thread identifier. */
3562 #define TAG_EXISTS 2 /* Is this process defined enough to
3563 fetch registers and its stack? */
3564 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3565 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3566 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3570 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3571 threadref
*expectedref
,
3572 gdb_ext_thread_info
*info
)
3574 struct remote_state
*rs
= get_remote_state ();
3578 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3581 /* info->threadid = 0; FIXME: implement zero_threadref. */
3583 info
->display
[0] = '\0';
3584 info
->shortname
[0] = '\0';
3585 info
->more_display
[0] = '\0';
3587 /* Assume the characters indicating the packet type have been
3589 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3590 pkt
= unpack_threadid (pkt
, &ref
);
3593 warning (_("Incomplete response to threadinfo request."));
3594 if (!threadmatch (&ref
, expectedref
))
3595 { /* This is an answer to a different request. */
3596 warning (_("ERROR RMT Thread info mismatch."));
3599 copy_threadref (&info
->threadid
, &ref
);
3601 /* Loop on tagged fields , try to bail if something goes wrong. */
3603 /* Packets are terminated with nulls. */
3604 while ((pkt
< limit
) && mask
&& *pkt
)
3606 pkt
= unpack_int (pkt
, &tag
); /* tag */
3607 pkt
= unpack_byte (pkt
, &length
); /* length */
3608 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3610 warning (_("ERROR RMT: threadinfo tag mismatch."));
3614 if (tag
== TAG_THREADID
)
3618 warning (_("ERROR RMT: length of threadid is not 16."));
3622 pkt
= unpack_threadid (pkt
, &ref
);
3623 mask
= mask
& ~TAG_THREADID
;
3626 if (tag
== TAG_EXISTS
)
3628 info
->active
= stub_unpack_int (pkt
, length
);
3630 mask
= mask
& ~(TAG_EXISTS
);
3633 warning (_("ERROR RMT: 'exists' length too long."));
3639 if (tag
== TAG_THREADNAME
)
3641 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3642 mask
= mask
& ~TAG_THREADNAME
;
3645 if (tag
== TAG_DISPLAY
)
3647 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3648 mask
= mask
& ~TAG_DISPLAY
;
3651 if (tag
== TAG_MOREDISPLAY
)
3653 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3654 mask
= mask
& ~TAG_MOREDISPLAY
;
3657 warning (_("ERROR RMT: unknown thread info tag."));
3658 break; /* Not a tag we know about. */
3664 remote_target::remote_get_threadinfo (threadref
*threadid
,
3666 gdb_ext_thread_info
*info
)
3668 struct remote_state
*rs
= get_remote_state ();
3671 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3673 getpkt (&rs
->buf
, 0);
3675 if (rs
->buf
[0] == '\0')
3678 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3683 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3686 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3687 threadref
*nextthread
)
3689 *pkt
++ = 'q'; /* info query packet */
3690 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3691 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3692 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3693 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3698 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3701 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3702 threadref
*original_echo
,
3703 threadref
*resultlist
,
3706 struct remote_state
*rs
= get_remote_state ();
3707 int count
, resultcount
, done
;
3710 /* Assume the 'q' and 'M chars have been stripped. */
3711 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3712 /* done parse past here */
3713 pkt
= unpack_byte (pkt
, &count
); /* count field */
3714 pkt
= unpack_nibble (pkt
, &done
);
3715 /* The first threadid is the argument threadid. */
3716 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3717 while ((count
-- > 0) && (pkt
< limit
))
3719 pkt
= unpack_threadid (pkt
, resultlist
++);
3720 if (resultcount
++ >= result_limit
)
3728 /* Fetch the next batch of threads from the remote. Returns -1 if the
3729 qL packet is not supported, 0 on error and 1 on success. */
3732 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3733 int result_limit
, int *done
, int *result_count
,
3734 threadref
*threadlist
)
3736 struct remote_state
*rs
= get_remote_state ();
3739 /* Truncate result limit to be smaller than the packet size. */
3740 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3741 >= get_remote_packet_size ())
3742 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3744 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3747 getpkt (&rs
->buf
, 0);
3748 if (rs
->buf
[0] == '\0')
3750 /* Packet not supported. */
3755 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3756 &rs
->echo_nextthread
, threadlist
, done
);
3758 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3760 /* FIXME: This is a good reason to drop the packet. */
3761 /* Possibly, there is a duplicate response. */
3763 retransmit immediatly - race conditions
3764 retransmit after timeout - yes
3766 wait for packet, then exit
3768 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3769 return 0; /* I choose simply exiting. */
3771 if (*result_count
<= 0)
3775 warning (_("RMT ERROR : failed to get remote thread list."));
3778 return result
; /* break; */
3780 if (*result_count
> result_limit
)
3783 warning (_("RMT ERROR: threadlist response longer than requested."));
3789 /* Fetch the list of remote threads, with the qL packet, and call
3790 STEPFUNCTION for each thread found. Stops iterating and returns 1
3791 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3792 STEPFUNCTION returns false. If the packet is not supported,
3796 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3797 void *context
, int looplimit
)
3799 struct remote_state
*rs
= get_remote_state ();
3800 int done
, i
, result_count
;
3808 if (loopcount
++ > looplimit
)
3811 warning (_("Remote fetch threadlist -infinite loop-."));
3814 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3815 MAXTHREADLISTRESULTS
,
3816 &done
, &result_count
,
3817 rs
->resultthreadlist
);
3820 /* Clear for later iterations. */
3822 /* Setup to resume next batch of thread references, set nextthread. */
3823 if (result_count
>= 1)
3824 copy_threadref (&rs
->nextthread
,
3825 &rs
->resultthreadlist
[result_count
- 1]);
3827 while (result_count
--)
3829 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
3839 /* A thread found on the remote target. */
3843 explicit thread_item (ptid_t ptid_
)
3847 thread_item (thread_item
&&other
) = default;
3848 thread_item
&operator= (thread_item
&&other
) = default;
3850 DISABLE_COPY_AND_ASSIGN (thread_item
);
3852 /* The thread's PTID. */
3855 /* The thread's extra info. */
3858 /* The thread's name. */
3861 /* The core the thread was running on. -1 if not known. */
3864 /* The thread handle associated with the thread. */
3865 gdb::byte_vector thread_handle
;
3868 /* Context passed around to the various methods listing remote
3869 threads. As new threads are found, they're added to the ITEMS
3872 struct threads_listing_context
3874 /* Return true if this object contains an entry for a thread with ptid
3877 bool contains_thread (ptid_t ptid
) const
3879 auto match_ptid
= [&] (const thread_item
&item
)
3881 return item
.ptid
== ptid
;
3884 auto it
= std::find_if (this->items
.begin (),
3888 return it
!= this->items
.end ();
3891 /* Remove the thread with ptid PTID. */
3893 void remove_thread (ptid_t ptid
)
3895 auto match_ptid
= [&] (const thread_item
&item
)
3897 return item
.ptid
== ptid
;
3900 auto it
= std::remove_if (this->items
.begin (),
3904 if (it
!= this->items
.end ())
3905 this->items
.erase (it
);
3908 /* The threads found on the remote target. */
3909 std::vector
<thread_item
> items
;
3913 remote_newthread_step (threadref
*ref
, void *data
)
3915 struct threads_listing_context
*context
3916 = (struct threads_listing_context
*) data
;
3917 int pid
= inferior_ptid
.pid ();
3918 int lwp
= threadref_to_int (ref
);
3919 ptid_t
ptid (pid
, lwp
);
3921 context
->items
.emplace_back (ptid
);
3923 return 1; /* continue iterator */
3926 #define CRAZY_MAX_THREADS 1000
3929 remote_target::remote_current_thread (ptid_t oldpid
)
3931 struct remote_state
*rs
= get_remote_state ();
3934 getpkt (&rs
->buf
, 0);
3935 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
3940 result
= read_ptid (&rs
->buf
[2], &obuf
);
3942 remote_debug_printf ("warning: garbage in qC reply");
3950 /* List remote threads using the deprecated qL packet. */
3953 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
3955 if (remote_threadlist_iterator (remote_newthread_step
, context
,
3956 CRAZY_MAX_THREADS
) >= 0)
3962 #if defined(HAVE_LIBEXPAT)
3965 start_thread (struct gdb_xml_parser
*parser
,
3966 const struct gdb_xml_element
*element
,
3968 std::vector
<gdb_xml_value
> &attributes
)
3970 struct threads_listing_context
*data
3971 = (struct threads_listing_context
*) user_data
;
3972 struct gdb_xml_value
*attr
;
3974 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
3975 ptid_t ptid
= read_ptid (id
, NULL
);
3977 data
->items
.emplace_back (ptid
);
3978 thread_item
&item
= data
->items
.back ();
3980 attr
= xml_find_attribute (attributes
, "core");
3982 item
.core
= *(ULONGEST
*) attr
->value
.get ();
3984 attr
= xml_find_attribute (attributes
, "name");
3986 item
.name
= (const char *) attr
->value
.get ();
3988 attr
= xml_find_attribute (attributes
, "handle");
3990 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
3994 end_thread (struct gdb_xml_parser
*parser
,
3995 const struct gdb_xml_element
*element
,
3996 void *user_data
, const char *body_text
)
3998 struct threads_listing_context
*data
3999 = (struct threads_listing_context
*) user_data
;
4001 if (body_text
!= NULL
&& *body_text
!= '\0')
4002 data
->items
.back ().extra
= body_text
;
4005 const struct gdb_xml_attribute thread_attributes
[] = {
4006 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
4007 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
4008 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4009 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4010 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
4013 const struct gdb_xml_element thread_children
[] = {
4014 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4017 const struct gdb_xml_element threads_children
[] = {
4018 { "thread", thread_attributes
, thread_children
,
4019 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
4020 start_thread
, end_thread
},
4021 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4024 const struct gdb_xml_element threads_elements
[] = {
4025 { "threads", NULL
, threads_children
,
4026 GDB_XML_EF_NONE
, NULL
, NULL
},
4027 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4032 /* List remote threads using qXfer:threads:read. */
4035 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
4037 #if defined(HAVE_LIBEXPAT)
4038 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4040 gdb::optional
<gdb::char_vector
> xml
4041 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
4043 if (xml
&& (*xml
)[0] != '\0')
4045 gdb_xml_parse_quick (_("threads"), "threads.dtd",
4046 threads_elements
, xml
->data (), context
);
4056 /* List remote threads using qfThreadInfo/qsThreadInfo. */
4059 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
4061 struct remote_state
*rs
= get_remote_state ();
4063 if (rs
->use_threadinfo_query
)
4067 putpkt ("qfThreadInfo");
4068 getpkt (&rs
->buf
, 0);
4069 bufp
= rs
->buf
.data ();
4070 if (bufp
[0] != '\0') /* q packet recognized */
4072 while (*bufp
++ == 'm') /* reply contains one or more TID */
4076 ptid_t ptid
= read_ptid (bufp
, &bufp
);
4077 context
->items
.emplace_back (ptid
);
4079 while (*bufp
++ == ','); /* comma-separated list */
4080 putpkt ("qsThreadInfo");
4081 getpkt (&rs
->buf
, 0);
4082 bufp
= rs
->buf
.data ();
4088 /* Packet not recognized. */
4089 rs
->use_threadinfo_query
= 0;
4096 /* Return true if INF only has one non-exited thread. */
4099 has_single_non_exited_thread (inferior
*inf
)
4102 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
4108 /* Implement the to_update_thread_list function for the remote
4112 remote_target::update_thread_list ()
4114 struct threads_listing_context context
;
4117 /* We have a few different mechanisms to fetch the thread list. Try
4118 them all, starting with the most preferred one first, falling
4119 back to older methods. */
4120 if (remote_get_threads_with_qxfer (&context
)
4121 || remote_get_threads_with_qthreadinfo (&context
)
4122 || remote_get_threads_with_ql (&context
))
4126 if (context
.items
.empty ()
4127 && remote_thread_always_alive (inferior_ptid
))
4129 /* Some targets don't really support threads, but still
4130 reply an (empty) thread list in response to the thread
4131 listing packets, instead of replying "packet not
4132 supported". Exit early so we don't delete the main
4137 /* CONTEXT now holds the current thread list on the remote
4138 target end. Delete GDB-side threads no longer found on the
4140 for (thread_info
*tp
: all_threads_safe ())
4142 if (tp
->inf
->process_target () != this)
4145 if (!context
.contains_thread (tp
->ptid
))
4147 /* Do not remove the thread if it is the last thread in
4148 the inferior. This situation happens when we have a
4149 pending exit process status to process. Otherwise we
4150 may end up with a seemingly live inferior (i.e. pid
4151 != 0) that has no threads. */
4152 if (has_single_non_exited_thread (tp
->inf
))
4160 /* Remove any unreported fork child threads from CONTEXT so
4161 that we don't interfere with follow fork, which is where
4162 creation of such threads is handled. */
4163 remove_new_fork_children (&context
);
4165 /* And now add threads we don't know about yet to our list. */
4166 for (thread_item
&item
: context
.items
)
4168 if (item
.ptid
!= null_ptid
)
4170 /* In non-stop mode, we assume new found threads are
4171 executing until proven otherwise with a stop reply.
4172 In all-stop, we can only get here if all threads are
4174 bool executing
= target_is_non_stop_p ();
4176 remote_notice_new_inferior (item
.ptid
, executing
);
4178 thread_info
*tp
= this->find_thread (item
.ptid
);
4179 remote_thread_info
*info
= get_remote_thread_info (tp
);
4180 info
->core
= item
.core
;
4181 info
->extra
= std::move (item
.extra
);
4182 info
->name
= std::move (item
.name
);
4183 info
->thread_handle
= std::move (item
.thread_handle
);
4190 /* If no thread listing method is supported, then query whether
4191 each known thread is alive, one by one, with the T packet.
4192 If the target doesn't support threads at all, then this is a
4193 no-op. See remote_thread_alive. */
4199 * Collect a descriptive string about the given thread.
4200 * The target may say anything it wants to about the thread
4201 * (typically info about its blocked / runnable state, name, etc.).
4202 * This string will appear in the info threads display.
4204 * Optional: targets are not required to implement this function.
4208 remote_target::extra_thread_info (thread_info
*tp
)
4210 struct remote_state
*rs
= get_remote_state ();
4213 struct gdb_ext_thread_info threadinfo
;
4215 if (rs
->remote_desc
== 0) /* paranoia */
4216 internal_error (_("remote_threads_extra_info"));
4218 if (tp
->ptid
== magic_null_ptid
4219 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4220 /* This is the main thread which was added by GDB. The remote
4221 server doesn't know about it. */
4224 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4226 /* If already have cached info, use it. */
4227 if (!extra
.empty ())
4228 return extra
.c_str ();
4230 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4232 /* If we're using qXfer:threads:read, then the extra info is
4233 included in the XML. So if we didn't have anything cached,
4234 it's because there's really no extra info. */
4238 if (rs
->use_threadextra_query
)
4240 char *b
= rs
->buf
.data ();
4241 char *endb
= b
+ get_remote_packet_size ();
4243 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4245 write_ptid (b
, endb
, tp
->ptid
);
4248 getpkt (&rs
->buf
, 0);
4249 if (rs
->buf
[0] != 0)
4251 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4252 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4253 return extra
.c_str ();
4257 /* If the above query fails, fall back to the old method. */
4258 rs
->use_threadextra_query
= 0;
4259 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4260 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4261 int_to_threadref (&id
, tp
->ptid
.lwp ());
4262 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4263 if (threadinfo
.active
)
4265 if (*threadinfo
.shortname
)
4266 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4267 if (*threadinfo
.display
)
4269 if (!extra
.empty ())
4271 string_appendf (extra
, " State: %s", threadinfo
.display
);
4273 if (*threadinfo
.more_display
)
4275 if (!extra
.empty ())
4277 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4279 return extra
.c_str ();
4286 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4287 struct static_tracepoint_marker
*marker
)
4289 struct remote_state
*rs
= get_remote_state ();
4290 char *p
= rs
->buf
.data ();
4292 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4294 p
+= hexnumstr (p
, addr
);
4296 getpkt (&rs
->buf
, 0);
4297 p
= rs
->buf
.data ();
4300 error (_("Remote failure reply: %s"), p
);
4304 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4311 std::vector
<static_tracepoint_marker
>
4312 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4314 struct remote_state
*rs
= get_remote_state ();
4315 std::vector
<static_tracepoint_marker
> markers
;
4317 static_tracepoint_marker marker
;
4319 /* Ask for a first packet of static tracepoint marker
4322 getpkt (&rs
->buf
, 0);
4323 p
= rs
->buf
.data ();
4325 error (_("Remote failure reply: %s"), p
);
4331 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4333 if (strid
== NULL
|| marker
.str_id
== strid
)
4334 markers
.push_back (std::move (marker
));
4336 while (*p
++ == ','); /* comma-separated list */
4337 /* Ask for another packet of static tracepoint definition. */
4339 getpkt (&rs
->buf
, 0);
4340 p
= rs
->buf
.data ();
4347 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4350 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4352 return ptid_t (inferior_ptid
.pid (), lwp
);
4356 /* Restart the remote side; this is an extended protocol operation. */
4359 remote_target::extended_remote_restart ()
4361 struct remote_state
*rs
= get_remote_state ();
4363 /* Send the restart command; for reasons I don't understand the
4364 remote side really expects a number after the "R". */
4365 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4368 remote_fileio_reset ();
4371 /* Clean up connection to a remote debugger. */
4374 remote_target::close ()
4376 /* Make sure we leave stdin registered in the event loop. */
4379 trace_reset_local_state ();
4384 remote_target::~remote_target ()
4386 struct remote_state
*rs
= get_remote_state ();
4388 /* Check for NULL because we may get here with a partially
4389 constructed target/connection. */
4390 if (rs
->remote_desc
== nullptr)
4393 serial_close (rs
->remote_desc
);
4395 /* We are destroying the remote target, so we should discard
4396 everything of this target. */
4397 discard_pending_stop_replies_in_queue ();
4399 if (rs
->remote_async_inferior_event_token
)
4400 delete_async_event_handler (&rs
->remote_async_inferior_event_token
);
4402 delete rs
->notif_state
;
4405 /* Query the remote side for the text, data and bss offsets. */
4408 remote_target::get_offsets ()
4410 struct remote_state
*rs
= get_remote_state ();
4413 int lose
, num_segments
= 0, do_sections
, do_segments
;
4414 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4416 if (current_program_space
->symfile_object_file
== NULL
)
4419 putpkt ("qOffsets");
4420 getpkt (&rs
->buf
, 0);
4421 buf
= rs
->buf
.data ();
4423 if (buf
[0] == '\000')
4424 return; /* Return silently. Stub doesn't support
4428 warning (_("Remote failure reply: %s"), buf
);
4432 /* Pick up each field in turn. This used to be done with scanf, but
4433 scanf will make trouble if CORE_ADDR size doesn't match
4434 conversion directives correctly. The following code will work
4435 with any size of CORE_ADDR. */
4436 text_addr
= data_addr
= bss_addr
= 0;
4440 if (startswith (ptr
, "Text="))
4443 /* Don't use strtol, could lose on big values. */
4444 while (*ptr
&& *ptr
!= ';')
4445 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4447 if (startswith (ptr
, ";Data="))
4450 while (*ptr
&& *ptr
!= ';')
4451 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4456 if (!lose
&& startswith (ptr
, ";Bss="))
4459 while (*ptr
&& *ptr
!= ';')
4460 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4462 if (bss_addr
!= data_addr
)
4463 warning (_("Target reported unsupported offsets: %s"), buf
);
4468 else if (startswith (ptr
, "TextSeg="))
4471 /* Don't use strtol, could lose on big values. */
4472 while (*ptr
&& *ptr
!= ';')
4473 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4476 if (startswith (ptr
, ";DataSeg="))
4479 while (*ptr
&& *ptr
!= ';')
4480 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4488 error (_("Malformed response to offset query, %s"), buf
);
4489 else if (*ptr
!= '\0')
4490 warning (_("Target reported unsupported offsets: %s"), buf
);
4492 objfile
*objf
= current_program_space
->symfile_object_file
;
4493 section_offsets offs
= objf
->section_offsets
;
4495 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
.get ());
4496 do_segments
= (data
!= NULL
);
4497 do_sections
= num_segments
== 0;
4499 if (num_segments
> 0)
4501 segments
[0] = text_addr
;
4502 segments
[1] = data_addr
;
4504 /* If we have two segments, we can still try to relocate everything
4505 by assuming that the .text and .data offsets apply to the whole
4506 text and data segments. Convert the offsets given in the packet
4507 to base addresses for symfile_map_offsets_to_segments. */
4508 else if (data
!= nullptr && data
->segments
.size () == 2)
4510 segments
[0] = data
->segments
[0].base
+ text_addr
;
4511 segments
[1] = data
->segments
[1].base
+ data_addr
;
4514 /* If the object file has only one segment, assume that it is text
4515 rather than data; main programs with no writable data are rare,
4516 but programs with no code are useless. Of course the code might
4517 have ended up in the data segment... to detect that we would need
4518 the permissions here. */
4519 else if (data
&& data
->segments
.size () == 1)
4521 segments
[0] = data
->segments
[0].base
+ text_addr
;
4524 /* There's no way to relocate by segment. */
4530 int ret
= symfile_map_offsets_to_segments (objf
->obfd
.get (),
4532 num_segments
, segments
);
4534 if (ret
== 0 && !do_sections
)
4535 error (_("Can not handle qOffsets TextSeg "
4536 "response with this symbol file"));
4544 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4546 /* This is a temporary kludge to force data and bss to use the
4547 same offsets because that's what nlmconv does now. The real
4548 solution requires changes to the stub and remote.c that I
4549 don't have time to do right now. */
4551 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4552 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4555 objfile_relocate (objf
, offs
);
4558 /* Send interrupt_sequence to remote target. */
4561 remote_target::send_interrupt_sequence ()
4563 struct remote_state
*rs
= get_remote_state ();
4565 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4566 remote_serial_write ("\x03", 1);
4567 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4568 serial_send_break (rs
->remote_desc
);
4569 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4571 serial_send_break (rs
->remote_desc
);
4572 remote_serial_write ("g", 1);
4575 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
4576 interrupt_sequence_mode
);
4580 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4581 and extract the PTID. Returns NULL_PTID if not found. */
4584 stop_reply_extract_thread (const char *stop_reply
)
4586 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4590 /* Txx r:val ; r:val (...) */
4593 /* Look for "register" named "thread". */
4598 p1
= strchr (p
, ':');
4602 if (strncmp (p
, "thread", p1
- p
) == 0)
4603 return read_ptid (++p1
, &p
);
4605 p1
= strchr (p
, ';');
4617 /* Determine the remote side's current thread. If we have a stop
4618 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4619 "thread" register we can extract the current thread from. If not,
4620 ask the remote which is the current thread with qC. The former
4621 method avoids a roundtrip. */
4624 remote_target::get_current_thread (const char *wait_status
)
4626 ptid_t ptid
= null_ptid
;
4628 /* Note we don't use remote_parse_stop_reply as that makes use of
4629 the target architecture, which we haven't yet fully determined at
4631 if (wait_status
!= NULL
)
4632 ptid
= stop_reply_extract_thread (wait_status
);
4633 if (ptid
== null_ptid
)
4634 ptid
= remote_current_thread (inferior_ptid
);
4639 /* Query the remote target for which is the current thread/process,
4640 add it to our tables, and update INFERIOR_PTID. The caller is
4641 responsible for setting the state such that the remote end is ready
4642 to return the current thread.
4644 This function is called after handling the '?' or 'vRun' packets,
4645 whose response is a stop reply from which we can also try
4646 extracting the thread. If the target doesn't support the explicit
4647 qC query, we infer the current thread from that stop reply, passed
4648 in in WAIT_STATUS, which may be NULL.
4650 The function returns pointer to the main thread of the inferior. */
4653 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4655 bool fake_pid_p
= false;
4657 switch_to_no_thread ();
4659 /* Now, if we have thread information, update the current thread's
4661 ptid_t curr_ptid
= get_current_thread (wait_status
);
4663 if (curr_ptid
!= null_ptid
)
4665 if (!m_features
.remote_multi_process_p ())
4670 /* Without this, some commands which require an active target
4671 (such as kill) won't work. This variable serves (at least)
4672 double duty as both the pid of the target process (if it has
4673 such), and as a flag indicating that a target is active. */
4674 curr_ptid
= magic_null_ptid
;
4678 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4680 /* Add the main thread and switch to it. Don't try reading
4681 registers yet, since we haven't fetched the target description
4683 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4684 switch_to_thread_no_regs (tp
);
4689 /* Print info about a thread that was found already stopped on
4693 remote_target::print_one_stopped_thread (thread_info
*thread
)
4695 target_waitstatus ws
;
4697 /* If there is a pending waitstatus, use it. If there isn't it's because
4698 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4699 and process_initial_stop_replies decided it wasn't interesting to save
4700 and report to the core. */
4701 if (thread
->has_pending_waitstatus ())
4703 ws
= thread
->pending_waitstatus ();
4704 thread
->clear_pending_waitstatus ();
4708 ws
.set_stopped (GDB_SIGNAL_0
);
4711 switch_to_thread (thread
);
4712 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4713 set_current_sal_from_frame (get_current_frame ());
4715 /* For "info program". */
4716 set_last_target_status (this, thread
->ptid
, ws
);
4718 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4720 enum gdb_signal sig
= ws
.sig ();
4722 if (signal_print_state (sig
))
4723 notify_signal_received (sig
);
4726 notify_normal_stop (nullptr, 1);
4729 /* Process all initial stop replies the remote side sent in response
4730 to the ? packet. These indicate threads that were already stopped
4731 on initial connection. We mark these threads as stopped and print
4732 their current frame before giving the user the prompt. */
4735 remote_target::process_initial_stop_replies (int from_tty
)
4737 int pending_stop_replies
= stop_reply_queue_length ();
4738 struct thread_info
*selected
= NULL
;
4739 struct thread_info
*lowest_stopped
= NULL
;
4740 struct thread_info
*first
= NULL
;
4742 /* This is only used when the target is non-stop. */
4743 gdb_assert (target_is_non_stop_p ());
4745 /* Consume the initial pending events. */
4746 while (pending_stop_replies
-- > 0)
4748 ptid_t waiton_ptid
= minus_one_ptid
;
4750 struct target_waitstatus ws
;
4751 int ignore_event
= 0;
4753 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4755 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4759 case TARGET_WAITKIND_IGNORE
:
4760 case TARGET_WAITKIND_NO_RESUMED
:
4761 case TARGET_WAITKIND_SIGNALLED
:
4762 case TARGET_WAITKIND_EXITED
:
4763 /* We shouldn't see these, but if we do, just ignore. */
4764 remote_debug_printf ("event ignored");
4775 thread_info
*evthread
= this->find_thread (event_ptid
);
4777 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4779 enum gdb_signal sig
= ws
.sig ();
4781 /* Stubs traditionally report SIGTRAP as initial signal,
4782 instead of signal 0. Suppress it. */
4783 if (sig
== GDB_SIGNAL_TRAP
)
4785 evthread
->set_stop_signal (sig
);
4786 ws
.set_stopped (sig
);
4789 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4790 || ws
.sig () != GDB_SIGNAL_0
)
4791 evthread
->set_pending_waitstatus (ws
);
4793 set_executing (this, event_ptid
, false);
4794 set_running (this, event_ptid
, false);
4795 get_remote_thread_info (evthread
)->set_not_resumed ();
4798 /* "Notice" the new inferiors before anything related to
4799 registers/memory. */
4800 for (inferior
*inf
: all_non_exited_inferiors (this))
4802 inf
->needs_setup
= true;
4806 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4807 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4812 /* If all-stop on top of non-stop, pause all threads. Note this
4813 records the threads' stop pc, so must be done after "noticing"
4818 /* At this point, the remote target is not async. It needs to be for
4819 the poll in stop_all_threads to consider events from it, so enable
4821 gdb_assert (!this->is_async_p ());
4822 SCOPE_EXIT
{ target_async (false); };
4823 target_async (true);
4824 stop_all_threads ("remote connect in all-stop");
4827 /* If all threads of an inferior were already stopped, we
4828 haven't setup the inferior yet. */
4829 for (inferior
*inf
: all_non_exited_inferiors (this))
4831 if (inf
->needs_setup
)
4833 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4834 switch_to_thread_no_regs (thread
);
4840 /* Now go over all threads that are stopped, and print their current
4841 frame. If all-stop, then if there's a signalled thread, pick
4843 for (thread_info
*thread
: all_non_exited_threads (this))
4849 thread
->set_running (false);
4850 else if (thread
->state
!= THREAD_STOPPED
)
4853 if (selected
== nullptr && thread
->has_pending_waitstatus ())
4856 if (lowest_stopped
== NULL
4857 || thread
->inf
->num
< lowest_stopped
->inf
->num
4858 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
4859 lowest_stopped
= thread
;
4862 print_one_stopped_thread (thread
);
4865 /* In all-stop, we only print the status of one thread, and leave
4866 others with their status pending. */
4869 thread_info
*thread
= selected
;
4871 thread
= lowest_stopped
;
4875 print_one_stopped_thread (thread
);
4879 /* Mark a remote_target as starting (by setting the starting_up flag within
4880 its remote_state) for the lifetime of this object. The reference count
4881 on the remote target is temporarily incremented, to prevent the target
4882 being deleted under our feet. */
4884 struct scoped_mark_target_starting
4886 /* Constructor, TARGET is the target to be marked as starting, its
4887 reference count will be incremented. */
4888 scoped_mark_target_starting (remote_target
*target
)
4889 : m_remote_target (remote_target_ref::new_reference (target
)),
4890 m_restore_starting_up (set_starting_up_flag (target
))
4895 /* Helper function, set the starting_up flag on TARGET and return an
4896 object which, when it goes out of scope, will restore the previous
4897 value of the starting_up flag. */
4898 static scoped_restore_tmpl
<bool>
4899 set_starting_up_flag (remote_target
*target
)
4901 remote_state
*rs
= target
->get_remote_state ();
4902 gdb_assert (!rs
->starting_up
);
4903 return make_scoped_restore (&rs
->starting_up
, true);
4906 /* A gdb::ref_ptr pointer to a remote_target. */
4907 using remote_target_ref
= gdb::ref_ptr
<remote_target
, target_ops_ref_policy
>;
4909 /* A reference to the target on which we are operating. */
4910 remote_target_ref m_remote_target
;
4912 /* An object which restores the previous value of the starting_up flag
4913 when it goes out of scope. */
4914 scoped_restore_tmpl
<bool> m_restore_starting_up
;
4917 /* Helper for remote_target::start_remote, start the remote connection and
4918 sync state. Return true if everything goes OK, otherwise, return false.
4919 This function exists so that the scoped_restore created within it will
4920 expire before we return to remote_target::start_remote. */
4923 remote_target::start_remote_1 (int from_tty
, int extended_p
)
4925 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
4927 struct remote_state
*rs
= get_remote_state ();
4929 /* Signal other parts that we're going through the initial setup,
4930 and so things may not be stable yet. E.g., we don't try to
4931 install tracepoints until we've relocated symbols. Also, a
4932 Ctrl-C before we're connected and synced up can't interrupt the
4933 target. Instead, it offers to drop the (potentially wedged)
4935 scoped_mark_target_starting
target_is_starting (this);
4939 if (interrupt_on_connect
)
4940 send_interrupt_sequence ();
4942 /* Ack any packet which the remote side has already sent. */
4943 remote_serial_write ("+", 1);
4945 /* The first packet we send to the target is the optional "supported
4946 packets" request. If the target can answer this, it will tell us
4947 which later probes to skip. */
4948 remote_query_supported ();
4950 /* Check vCont support and set the remote state's vCont_action_support
4952 remote_vcont_probe ();
4954 /* If the stub wants to get a QAllow, compose one and send it. */
4955 if (m_features
.packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
4958 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4959 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4960 as a reply to known packet. For packet "vFile:setfs:" it is an
4961 invalid reply and GDB would return error in
4962 remote_hostio_set_filesystem, making remote files access impossible.
4963 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4964 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4966 const char v_mustreplyempty
[] = "vMustReplyEmpty";
4968 putpkt (v_mustreplyempty
);
4969 getpkt (&rs
->buf
, 0);
4970 if (strcmp (rs
->buf
.data (), "OK") == 0)
4972 m_features
.m_protocol_packets
[PACKET_vFile_setfs
].support
4975 else if (strcmp (rs
->buf
.data (), "") != 0)
4976 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
4980 /* Next, we possibly activate noack mode.
4982 If the QStartNoAckMode packet configuration is set to AUTO,
4983 enable noack mode if the stub reported a wish for it with
4986 If set to TRUE, then enable noack mode even if the stub didn't
4987 report it in qSupported. If the stub doesn't reply OK, the
4988 session ends with an error.
4990 If FALSE, then don't activate noack mode, regardless of what the
4991 stub claimed should be the default with qSupported. */
4993 if (m_features
.packet_support (PACKET_QStartNoAckMode
) != PACKET_DISABLE
)
4995 putpkt ("QStartNoAckMode");
4996 getpkt (&rs
->buf
, 0);
4997 if (m_features
.packet_ok (rs
->buf
, PACKET_QStartNoAckMode
) == PACKET_OK
)
5003 /* Tell the remote that we are using the extended protocol. */
5005 getpkt (&rs
->buf
, 0);
5008 /* Let the target know which signals it is allowed to pass down to
5010 update_signals_program_target ();
5012 /* Next, if the target can specify a description, read it. We do
5013 this before anything involving memory or registers. */
5014 target_find_description ();
5016 /* Next, now that we know something about the target, update the
5017 address spaces in the program spaces. */
5018 update_address_spaces ();
5020 /* On OSs where the list of libraries is global to all
5021 processes, we fetch them early. */
5022 if (gdbarch_has_global_solist (target_gdbarch ()))
5023 solib_add (NULL
, from_tty
, auto_solib_add
);
5025 if (target_is_non_stop_p ())
5027 if (m_features
.packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
5028 error (_("Non-stop mode requested, but remote "
5029 "does not support non-stop"));
5031 putpkt ("QNonStop:1");
5032 getpkt (&rs
->buf
, 0);
5034 if (strcmp (rs
->buf
.data (), "OK") != 0)
5035 error (_("Remote refused setting non-stop mode with: %s"),
5038 /* Find about threads and processes the stub is already
5039 controlling. We default to adding them in the running state.
5040 The '?' query below will then tell us about which threads are
5042 this->update_thread_list ();
5044 else if (m_features
.packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
5046 /* Don't assume that the stub can operate in all-stop mode.
5047 Request it explicitly. */
5048 putpkt ("QNonStop:0");
5049 getpkt (&rs
->buf
, 0);
5051 if (strcmp (rs
->buf
.data (), "OK") != 0)
5052 error (_("Remote refused setting all-stop mode with: %s"),
5056 /* Upload TSVs regardless of whether the target is running or not. The
5057 remote stub, such as GDBserver, may have some predefined or builtin
5058 TSVs, even if the target is not running. */
5059 if (get_trace_status (current_trace_status ()) != -1)
5061 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
5063 upload_trace_state_variables (&uploaded_tsvs
);
5064 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
5067 /* Check whether the target is running now. */
5069 getpkt (&rs
->buf
, 0);
5071 if (!target_is_non_stop_p ())
5073 char *wait_status
= NULL
;
5075 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
5078 error (_("The target is not running (try extended-remote?)"));
5083 /* Save the reply for later. */
5084 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
5085 strcpy (wait_status
, rs
->buf
.data ());
5088 /* Fetch thread list. */
5089 target_update_thread_list ();
5091 /* Let the stub know that we want it to return the thread. */
5092 set_continue_thread (minus_one_ptid
);
5094 if (thread_count (this) == 0)
5096 /* Target has no concept of threads at all. GDB treats
5097 non-threaded target as single-threaded; add a main
5099 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
5100 get_remote_thread_info (tp
)->set_resumed ();
5104 /* We have thread information; select the thread the target
5105 says should be current. If we're reconnecting to a
5106 multi-threaded program, this will ideally be the thread
5107 that last reported an event before GDB disconnected. */
5108 ptid_t curr_thread
= get_current_thread (wait_status
);
5109 if (curr_thread
== null_ptid
)
5111 /* Odd... The target was able to list threads, but not
5112 tell us which thread was current (no "thread"
5113 register in T stop reply?). Just pick the first
5114 thread in the thread list then. */
5116 remote_debug_printf ("warning: couldn't determine remote "
5117 "current thread; picking first in list.");
5119 for (thread_info
*tp
: all_non_exited_threads (this,
5122 switch_to_thread (tp
);
5127 switch_to_thread (this->find_thread (curr_thread
));
5130 /* init_wait_for_inferior should be called before get_offsets in order
5131 to manage `inserted' flag in bp loc in a correct state.
5132 breakpoint_init_inferior, called from init_wait_for_inferior, set
5133 `inserted' flag to 0, while before breakpoint_re_set, called from
5134 start_remote, set `inserted' flag to 1. In the initialization of
5135 inferior, breakpoint_init_inferior should be called first, and then
5136 breakpoint_re_set can be called. If this order is broken, state of
5137 `inserted' flag is wrong, and cause some problems on breakpoint
5139 init_wait_for_inferior ();
5141 get_offsets (); /* Get text, data & bss offsets. */
5143 /* If we could not find a description using qXfer, and we know
5144 how to do it some other way, try again. This is not
5145 supported for non-stop; it could be, but it is tricky if
5146 there are no stopped threads when we connect. */
5147 if (remote_read_description_p (this)
5148 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
5150 target_clear_description ();
5151 target_find_description ();
5154 /* Use the previously fetched status. */
5155 gdb_assert (wait_status
!= NULL
);
5156 struct notif_event
*reply
5157 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
5158 push_stop_reply ((struct stop_reply
*) reply
);
5160 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
5164 /* Clear WFI global state. Do this before finding about new
5165 threads and inferiors, and setting the current inferior.
5166 Otherwise we would clear the proceed status of the current
5167 inferior when we want its stop_soon state to be preserved
5168 (see notice_new_inferior). */
5169 init_wait_for_inferior ();
5171 /* In non-stop, we will either get an "OK", meaning that there
5172 are no stopped threads at this time; or, a regular stop
5173 reply. In the latter case, there may be more than one thread
5174 stopped --- we pull them all out using the vStopped
5176 if (strcmp (rs
->buf
.data (), "OK") != 0)
5178 const notif_client
*notif
= ¬if_client_stop
;
5180 /* remote_notif_get_pending_replies acks this one, and gets
5182 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
5183 = remote_notif_parse (this, notif
, rs
->buf
.data ());
5184 remote_notif_get_pending_events (notif
);
5187 if (thread_count (this) == 0)
5190 error (_("The target is not running (try extended-remote?)"));
5194 /* Report all signals during attach/startup. */
5197 /* If there are already stopped threads, mark them stopped and
5198 report their stops before giving the prompt to the user. */
5199 process_initial_stop_replies (from_tty
);
5201 if (target_can_async_p ())
5202 target_async (true);
5205 /* Give the target a chance to look up symbols. */
5206 for (inferior
*inf
: all_inferiors (this))
5208 /* The inferiors that exist at this point were created from what
5209 was found already running on the remote side, so we know they
5211 gdb_assert (this->has_execution (inf
));
5213 /* No use without a symbol-file. */
5214 if (inf
->pspace
->symfile_object_file
== nullptr)
5217 /* Need to switch to a specific thread, because remote_check_symbols
5218 uses INFERIOR_PTID to set the general thread. */
5219 scoped_restore_current_thread restore_thread
;
5220 thread_info
*thread
= any_thread_of_inferior (inf
);
5221 switch_to_thread (thread
);
5222 this->remote_check_symbols ();
5225 /* Possibly the target has been engaged in a trace run started
5226 previously; find out where things are at. */
5227 if (get_trace_status (current_trace_status ()) != -1)
5229 struct uploaded_tp
*uploaded_tps
= NULL
;
5231 if (current_trace_status ()->running
)
5232 gdb_printf (_("Trace is already running on the target.\n"));
5234 upload_tracepoints (&uploaded_tps
);
5236 merge_uploaded_tracepoints (&uploaded_tps
);
5239 /* Possibly the target has been engaged in a btrace record started
5240 previously; find out where things are at. */
5241 remote_btrace_maybe_reopen ();
5246 /* Start the remote connection and sync state. */
5249 remote_target::start_remote (int from_tty
, int extended_p
)
5251 if (start_remote_1 (from_tty
, extended_p
)
5252 && breakpoints_should_be_inserted_now ())
5253 insert_breakpoints ();
5257 remote_target::connection_string ()
5259 remote_state
*rs
= get_remote_state ();
5261 if (rs
->remote_desc
->name
!= NULL
)
5262 return rs
->remote_desc
->name
;
5267 /* Open a connection to a remote debugger.
5268 NAME is the filename used for communication. */
5271 remote_target::open (const char *name
, int from_tty
)
5273 open_1 (name
, from_tty
, 0);
5276 /* Open a connection to a remote debugger using the extended
5277 remote gdb protocol. NAME is the filename used for communication. */
5280 extended_remote_target::open (const char *name
, int from_tty
)
5282 open_1 (name
, from_tty
, 1 /*extended_p */);
5286 remote_features::reset_all_packet_configs_support ()
5290 for (i
= 0; i
< PACKET_MAX
; i
++)
5291 m_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5294 /* Initialize all packet configs. */
5297 init_all_packet_configs (void)
5301 for (i
= 0; i
< PACKET_MAX
; i
++)
5303 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5304 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5308 /* Symbol look-up. */
5311 remote_target::remote_check_symbols ()
5316 /* It doesn't make sense to send a qSymbol packet for an inferior that
5317 doesn't have execution, because the remote side doesn't know about
5318 inferiors without execution. */
5319 gdb_assert (target_has_execution ());
5321 if (m_features
.packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5324 /* Make sure the remote is pointing at the right process. Note
5325 there's no way to select "no process". */
5326 set_general_process ();
5328 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5329 because we need both at the same time. */
5330 gdb::char_vector
msg (get_remote_packet_size ());
5331 gdb::char_vector
reply (get_remote_packet_size ());
5333 /* Invite target to request symbol lookups. */
5335 putpkt ("qSymbol::");
5337 m_features
.packet_ok (reply
, PACKET_qSymbol
);
5339 while (startswith (reply
.data (), "qSymbol:"))
5341 struct bound_minimal_symbol sym
;
5344 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5347 sym
= lookup_minimal_symbol (msg
.data (), NULL
, NULL
);
5348 if (sym
.minsym
== NULL
)
5349 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5353 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
5354 CORE_ADDR sym_addr
= sym
.value_address ();
5356 /* If this is a function address, return the start of code
5357 instead of any data function descriptor. */
5358 sym_addr
= gdbarch_convert_from_func_ptr_addr
5359 (target_gdbarch (), sym_addr
, current_inferior ()->top_target ());
5361 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5362 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5365 putpkt (msg
.data ());
5370 static struct serial
*
5371 remote_serial_open (const char *name
)
5373 static int udp_warning
= 0;
5375 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5376 of in ser-tcp.c, because it is the remote protocol assuming that the
5377 serial connection is reliable and not the serial connection promising
5379 if (!udp_warning
&& startswith (name
, "udp:"))
5381 warning (_("The remote protocol may be unreliable over UDP.\n"
5382 "Some events may be lost, rendering further debugging "
5387 return serial_open (name
);
5390 /* Inform the target of our permission settings. The permission flags
5391 work without this, but if the target knows the settings, it can do
5392 a couple things. First, it can add its own check, to catch cases
5393 that somehow manage to get by the permissions checks in target
5394 methods. Second, if the target is wired to disallow particular
5395 settings (for instance, a system in the field that is not set up to
5396 be able to stop at a breakpoint), it can object to any unavailable
5400 remote_target::set_permissions ()
5402 struct remote_state
*rs
= get_remote_state ();
5404 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5405 "WriteReg:%x;WriteMem:%x;"
5406 "InsertBreak:%x;InsertTrace:%x;"
5407 "InsertFastTrace:%x;Stop:%x",
5408 may_write_registers
, may_write_memory
,
5409 may_insert_breakpoints
, may_insert_tracepoints
,
5410 may_insert_fast_tracepoints
, may_stop
);
5412 getpkt (&rs
->buf
, 0);
5414 /* If the target didn't like the packet, warn the user. Do not try
5415 to undo the user's settings, that would just be maddening. */
5416 if (strcmp (rs
->buf
.data (), "OK") != 0)
5417 warning (_("Remote refused setting permissions with: %s"),
5421 /* This type describes each known response to the qSupported
5423 struct protocol_feature
5425 /* The name of this protocol feature. */
5428 /* The default for this protocol feature. */
5429 enum packet_support default_support
;
5431 /* The function to call when this feature is reported, or after
5432 qSupported processing if the feature is not supported.
5433 The first argument points to this structure. The second
5434 argument indicates whether the packet requested support be
5435 enabled, disabled, or probed (or the default, if this function
5436 is being called at the end of processing and this feature was
5437 not reported). The third argument may be NULL; if not NULL, it
5438 is a NUL-terminated string taken from the packet following
5439 this feature's name and an equals sign. */
5440 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5441 enum packet_support
, const char *);
5443 /* The corresponding packet for this feature. Only used if
5444 FUNC is remote_supported_packet. */
5449 remote_supported_packet (remote_target
*remote
,
5450 const struct protocol_feature
*feature
,
5451 enum packet_support support
,
5452 const char *argument
)
5456 warning (_("Remote qSupported response supplied an unexpected value for"
5457 " \"%s\"."), feature
->name
);
5461 remote
->m_features
.m_protocol_packets
[feature
->packet
].support
= support
;
5465 remote_target::remote_packet_size (const protocol_feature
*feature
,
5466 enum packet_support support
, const char *value
)
5468 struct remote_state
*rs
= get_remote_state ();
5473 if (support
!= PACKET_ENABLE
)
5476 if (value
== NULL
|| *value
== '\0')
5478 warning (_("Remote target reported \"%s\" without a size."),
5484 packet_size
= strtol (value
, &value_end
, 16);
5485 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5487 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5488 feature
->name
, value
);
5492 /* Record the new maximum packet size. */
5493 rs
->explicit_packet_size
= packet_size
;
5497 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5498 enum packet_support support
, const char *value
)
5500 remote
->remote_packet_size (feature
, support
, value
);
5503 static const struct protocol_feature remote_protocol_features
[] = {
5504 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5505 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5506 PACKET_qXfer_auxv
},
5507 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5508 PACKET_qXfer_exec_file
},
5509 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5510 PACKET_qXfer_features
},
5511 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5512 PACKET_qXfer_libraries
},
5513 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5514 PACKET_qXfer_libraries_svr4
},
5515 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5516 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5517 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5518 PACKET_qXfer_memory_map
},
5519 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5520 PACKET_qXfer_osdata
},
5521 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5522 PACKET_qXfer_threads
},
5523 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5524 PACKET_qXfer_traceframe_info
},
5525 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5526 PACKET_QPassSignals
},
5527 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5528 PACKET_QCatchSyscalls
},
5529 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5530 PACKET_QProgramSignals
},
5531 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5532 PACKET_QSetWorkingDir
},
5533 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5534 PACKET_QStartupWithShell
},
5535 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5536 PACKET_QEnvironmentHexEncoded
},
5537 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5538 PACKET_QEnvironmentReset
},
5539 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5540 PACKET_QEnvironmentUnset
},
5541 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5542 PACKET_QStartNoAckMode
},
5543 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5544 PACKET_multiprocess_feature
},
5545 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5546 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5547 PACKET_qXfer_siginfo_read
},
5548 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5549 PACKET_qXfer_siginfo_write
},
5550 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5551 PACKET_ConditionalTracepoints
},
5552 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5553 PACKET_ConditionalBreakpoints
},
5554 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5555 PACKET_BreakpointCommands
},
5556 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5557 PACKET_FastTracepoints
},
5558 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5559 PACKET_StaticTracepoints
},
5560 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5561 PACKET_InstallInTrace
},
5562 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5563 PACKET_DisconnectedTracing_feature
},
5564 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5566 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5568 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5569 PACKET_TracepointSource
},
5570 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5572 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5573 PACKET_EnableDisableTracepoints_feature
},
5574 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5575 PACKET_qXfer_fdpic
},
5576 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5578 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5579 PACKET_QDisableRandomization
},
5580 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5581 { "QTBuffer:size", PACKET_DISABLE
,
5582 remote_supported_packet
, PACKET_QTBuffer_size
},
5583 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5584 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5585 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5586 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5587 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5588 PACKET_qXfer_btrace
},
5589 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5590 PACKET_qXfer_btrace_conf
},
5591 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5592 PACKET_Qbtrace_conf_bts_size
},
5593 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5594 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5595 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5596 PACKET_fork_event_feature
},
5597 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5598 PACKET_vfork_event_feature
},
5599 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5600 PACKET_exec_event_feature
},
5601 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5602 PACKET_Qbtrace_conf_pt_size
},
5603 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5604 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5605 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5606 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5607 PACKET_memory_tagging_feature
},
5610 static char *remote_support_xml
;
5612 /* Register string appended to "xmlRegisters=" in qSupported query. */
5615 register_remote_support_xml (const char *xml
)
5617 #if defined(HAVE_LIBEXPAT)
5618 if (remote_support_xml
== NULL
)
5619 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5622 char *copy
= xstrdup (remote_support_xml
+ 13);
5624 char *p
= strtok_r (copy
, ",", &saveptr
);
5628 if (strcmp (p
, xml
) == 0)
5635 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5638 remote_support_xml
= reconcat (remote_support_xml
,
5639 remote_support_xml
, ",", xml
,
5646 remote_query_supported_append (std::string
*msg
, const char *append
)
5650 msg
->append (append
);
5654 remote_target::remote_query_supported ()
5656 struct remote_state
*rs
= get_remote_state ();
5659 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5661 /* The packet support flags are handled differently for this packet
5662 than for most others. We treat an error, a disabled packet, and
5663 an empty response identically: any features which must be reported
5664 to be used will be automatically disabled. An empty buffer
5665 accomplishes this, since that is also the representation for a list
5666 containing no features. */
5669 if (m_features
.packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5673 if (m_features
.packet_set_cmd_state (PACKET_multiprocess_feature
)
5674 != AUTO_BOOLEAN_FALSE
)
5675 remote_query_supported_append (&q
, "multiprocess+");
5677 if (m_features
.packet_set_cmd_state (PACKET_swbreak_feature
)
5678 != AUTO_BOOLEAN_FALSE
)
5679 remote_query_supported_append (&q
, "swbreak+");
5681 if (m_features
.packet_set_cmd_state (PACKET_hwbreak_feature
)
5682 != AUTO_BOOLEAN_FALSE
)
5683 remote_query_supported_append (&q
, "hwbreak+");
5685 remote_query_supported_append (&q
, "qRelocInsn+");
5687 if (m_features
.packet_set_cmd_state (PACKET_fork_event_feature
)
5688 != AUTO_BOOLEAN_FALSE
)
5689 remote_query_supported_append (&q
, "fork-events+");
5691 if (m_features
.packet_set_cmd_state (PACKET_vfork_event_feature
)
5692 != AUTO_BOOLEAN_FALSE
)
5693 remote_query_supported_append (&q
, "vfork-events+");
5695 if (m_features
.packet_set_cmd_state (PACKET_exec_event_feature
)
5696 != AUTO_BOOLEAN_FALSE
)
5697 remote_query_supported_append (&q
, "exec-events+");
5699 if (m_features
.packet_set_cmd_state (PACKET_vContSupported
)
5700 != AUTO_BOOLEAN_FALSE
)
5701 remote_query_supported_append (&q
, "vContSupported+");
5703 if (m_features
.packet_set_cmd_state (PACKET_QThreadEvents
)
5704 != AUTO_BOOLEAN_FALSE
)
5705 remote_query_supported_append (&q
, "QThreadEvents+");
5707 if (m_features
.packet_set_cmd_state (PACKET_no_resumed
)
5708 != AUTO_BOOLEAN_FALSE
)
5709 remote_query_supported_append (&q
, "no-resumed+");
5711 if (m_features
.packet_set_cmd_state (PACKET_memory_tagging_feature
)
5712 != AUTO_BOOLEAN_FALSE
)
5713 remote_query_supported_append (&q
, "memory-tagging+");
5715 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5716 the qSupported:xmlRegisters=i386 handling. */
5717 if (remote_support_xml
!= NULL
5718 && (m_features
.packet_support (PACKET_qXfer_features
)
5720 remote_query_supported_append (&q
, remote_support_xml
);
5722 q
= "qSupported:" + q
;
5723 putpkt (q
.c_str ());
5725 getpkt (&rs
->buf
, 0);
5727 /* If an error occured, warn, but do not return - just reset the
5728 buffer to empty and go on to disable features. */
5729 if (m_features
.packet_ok (rs
->buf
, PACKET_qSupported
) == PACKET_ERROR
)
5731 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
5736 memset (seen
, 0, sizeof (seen
));
5738 next
= rs
->buf
.data ();
5741 enum packet_support is_supported
;
5742 char *p
, *end
, *name_end
, *value
;
5744 /* First separate out this item from the rest of the packet. If
5745 there's another item after this, we overwrite the separator
5746 (terminated strings are much easier to work with). */
5748 end
= strchr (p
, ';');
5751 end
= p
+ strlen (p
);
5761 warning (_("empty item in \"qSupported\" response"));
5766 name_end
= strchr (p
, '=');
5769 /* This is a name=value entry. */
5770 is_supported
= PACKET_ENABLE
;
5771 value
= name_end
+ 1;
5780 is_supported
= PACKET_ENABLE
;
5784 is_supported
= PACKET_DISABLE
;
5788 is_supported
= PACKET_SUPPORT_UNKNOWN
;
5792 warning (_("unrecognized item \"%s\" "
5793 "in \"qSupported\" response"), p
);
5799 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5800 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
5802 const struct protocol_feature
*feature
;
5805 feature
= &remote_protocol_features
[i
];
5806 feature
->func (this, feature
, is_supported
, value
);
5811 /* If we increased the packet size, make sure to increase the global
5812 buffer size also. We delay this until after parsing the entire
5813 qSupported packet, because this is the same buffer we were
5815 if (rs
->buf
.size () < rs
->explicit_packet_size
)
5816 rs
->buf
.resize (rs
->explicit_packet_size
);
5818 /* Handle the defaults for unmentioned features. */
5819 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5822 const struct protocol_feature
*feature
;
5824 feature
= &remote_protocol_features
[i
];
5825 feature
->func (this, feature
, feature
->default_support
, NULL
);
5829 /* Serial QUIT handler for the remote serial descriptor.
5831 Defers handling a Ctrl-C until we're done with the current
5832 command/response packet sequence, unless:
5834 - We're setting up the connection. Don't send a remote interrupt
5835 request, as we're not fully synced yet. Quit immediately
5838 - The target has been resumed in the foreground
5839 (target_terminal::is_ours is false) with a synchronous resume
5840 packet, and we're blocked waiting for the stop reply, thus a
5841 Ctrl-C should be immediately sent to the target.
5843 - We get a second Ctrl-C while still within the same serial read or
5844 write. In that case the serial is seemingly wedged --- offer to
5847 - We see a second Ctrl-C without target response, after having
5848 previously interrupted the target. In that case the target/stub
5849 is probably wedged --- offer to quit/disconnect.
5853 remote_target::remote_serial_quit_handler ()
5855 struct remote_state
*rs
= get_remote_state ();
5857 if (check_quit_flag ())
5859 /* If we're starting up, we're not fully synced yet. Quit
5861 if (rs
->starting_up
)
5863 else if (rs
->got_ctrlc_during_io
)
5865 if (query (_("The target is not responding to GDB commands.\n"
5866 "Stop debugging it? ")))
5867 remote_unpush_and_throw (this);
5869 /* If ^C has already been sent once, offer to disconnect. */
5870 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
5872 /* All-stop protocol, and blocked waiting for stop reply. Send
5873 an interrupt request. */
5874 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
5875 target_interrupt ();
5877 rs
->got_ctrlc_during_io
= 1;
5881 /* The remote_target that is current while the quit handler is
5882 overridden with remote_serial_quit_handler. */
5883 static remote_target
*curr_quit_handler_target
;
5886 remote_serial_quit_handler ()
5888 curr_quit_handler_target
->remote_serial_quit_handler ();
5891 /* Remove the remote target from the target stack of each inferior
5892 that is using it. Upper targets depend on it so remove them
5896 remote_unpush_target (remote_target
*target
)
5898 /* We have to unpush the target from all inferiors, even those that
5900 scoped_restore_current_inferior restore_current_inferior
;
5902 for (inferior
*inf
: all_inferiors (target
))
5904 switch_to_inferior_no_thread (inf
);
5905 inf
->pop_all_targets_at_and_above (process_stratum
);
5906 generic_mourn_inferior ();
5909 /* Don't rely on target_close doing this when the target is popped
5910 from the last remote inferior above, because something may be
5911 holding a reference to the target higher up on the stack, meaning
5912 target_close won't be called yet. We lost the connection to the
5913 target, so clear these now, otherwise we may later throw
5914 TARGET_CLOSE_ERROR while trying to tell the remote target to
5916 fileio_handles_invalidate_target (target
);
5920 remote_unpush_and_throw (remote_target
*target
)
5922 remote_unpush_target (target
);
5923 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
5927 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
5929 remote_target
*curr_remote
= get_current_remote_target ();
5932 error (_("To open a remote debug connection, you need to specify what\n"
5933 "serial device is attached to the remote system\n"
5934 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
5936 /* If we're connected to a running target, target_preopen will kill it.
5937 Ask this question first, before target_preopen has a chance to kill
5939 if (curr_remote
!= NULL
&& !target_has_execution ())
5942 && !query (_("Already connected to a remote target. Disconnect? ")))
5943 error (_("Still connected."));
5946 /* Here the possibly existing remote target gets unpushed. */
5947 target_preopen (from_tty
);
5949 remote_fileio_reset ();
5950 reopen_exec_file ();
5951 reread_symbols (from_tty
);
5953 remote_target
*remote
5954 = (extended_p
? new extended_remote_target () : new remote_target ());
5955 target_ops_up
target_holder (remote
);
5957 remote_state
*rs
= remote
->get_remote_state ();
5959 /* See FIXME above. */
5960 if (!target_async_permitted
)
5961 rs
->wait_forever_enabled_p
= 1;
5963 rs
->remote_desc
= remote_serial_open (name
);
5964 if (!rs
->remote_desc
)
5965 perror_with_name (name
);
5967 if (baud_rate
!= -1)
5969 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
5971 /* The requested speed could not be set. Error out to
5972 top level after closing remote_desc. Take care to
5973 set remote_desc to NULL to avoid closing remote_desc
5975 serial_close (rs
->remote_desc
);
5976 rs
->remote_desc
= NULL
;
5977 perror_with_name (name
);
5981 serial_setparity (rs
->remote_desc
, serial_parity
);
5982 serial_raw (rs
->remote_desc
);
5984 /* If there is something sitting in the buffer we might take it as a
5985 response to a command, which would be bad. */
5986 serial_flush_input (rs
->remote_desc
);
5990 gdb_puts ("Remote debugging using ");
5995 /* Switch to using the remote target now. */
5996 current_inferior ()->push_target (std::move (target_holder
));
5998 /* Register extra event sources in the event loop. */
5999 rs
->remote_async_inferior_event_token
6000 = create_async_event_handler (remote_async_inferior_event_handler
, nullptr,
6002 rs
->notif_state
= remote_notif_state_allocate (remote
);
6004 /* Reset the target state; these things will be queried either by
6005 remote_query_supported or as they are needed. */
6006 remote
->m_features
.reset_all_packet_configs_support ();
6007 rs
->explicit_packet_size
= 0;
6009 rs
->extended
= extended_p
;
6010 rs
->waiting_for_stop_reply
= 0;
6011 rs
->ctrlc_pending_p
= 0;
6012 rs
->got_ctrlc_during_io
= 0;
6014 rs
->general_thread
= not_sent_ptid
;
6015 rs
->continue_thread
= not_sent_ptid
;
6016 rs
->remote_traceframe_number
= -1;
6018 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
6020 /* Probe for ability to use "ThreadInfo" query, as required. */
6021 rs
->use_threadinfo_query
= 1;
6022 rs
->use_threadextra_query
= 1;
6024 rs
->readahead_cache
.invalidate ();
6026 if (target_async_permitted
)
6028 /* FIXME: cagney/1999-09-23: During the initial connection it is
6029 assumed that the target is already ready and able to respond to
6030 requests. Unfortunately remote_start_remote() eventually calls
6031 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
6032 around this. Eventually a mechanism that allows
6033 wait_for_inferior() to expect/get timeouts will be
6035 rs
->wait_forever_enabled_p
= 0;
6038 /* First delete any symbols previously loaded from shared libraries. */
6039 no_shared_libraries (NULL
, 0);
6041 /* Start the remote connection. If error() or QUIT, discard this
6042 target (we'd otherwise be in an inconsistent state) and then
6043 propogate the error on up the exception chain. This ensures that
6044 the caller doesn't stumble along blindly assuming that the
6045 function succeeded. The CLI doesn't have this problem but other
6046 UI's, such as MI do.
6048 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
6049 this function should return an error indication letting the
6050 caller restore the previous state. Unfortunately the command
6051 ``target remote'' is directly wired to this function making that
6052 impossible. On a positive note, the CLI side of this problem has
6053 been fixed - the function set_cmd_context() makes it possible for
6054 all the ``target ....'' commands to share a common callback
6055 function. See cli-dump.c. */
6060 remote
->start_remote (from_tty
, extended_p
);
6062 catch (const gdb_exception
&ex
)
6064 /* Pop the partially set up target - unless something else did
6065 already before throwing the exception. */
6066 if (ex
.error
!= TARGET_CLOSE_ERROR
)
6067 remote_unpush_target (remote
);
6072 remote_btrace_reset (rs
);
6074 if (target_async_permitted
)
6075 rs
->wait_forever_enabled_p
= 1;
6078 /* Determine if WS represents a fork status. */
6081 is_fork_status (target_waitkind kind
)
6083 return (kind
== TARGET_WAITKIND_FORKED
6084 || kind
== TARGET_WAITKIND_VFORKED
);
6087 /* Return THREAD's pending status if it is a pending fork parent, else
6090 static const target_waitstatus
*
6091 thread_pending_fork_status (struct thread_info
*thread
)
6093 const target_waitstatus
&ws
6094 = (thread
->has_pending_waitstatus ()
6095 ? thread
->pending_waitstatus ()
6096 : thread
->pending_follow
);
6098 if (!is_fork_status (ws
.kind ()))
6104 /* Detach the specified process. */
6107 remote_target::remote_detach_pid (int pid
)
6109 struct remote_state
*rs
= get_remote_state ();
6111 /* This should not be necessary, but the handling for D;PID in
6112 GDBserver versions prior to 8.2 incorrectly assumes that the
6113 selected process points to the same process we're detaching,
6114 leading to misbehavior (and possibly GDBserver crashing) when it
6115 does not. Since it's easy and cheap, work around it by forcing
6116 GDBserver to select GDB's current process. */
6117 set_general_process ();
6119 if (m_features
.remote_multi_process_p ())
6120 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
6122 strcpy (rs
->buf
.data (), "D");
6125 getpkt (&rs
->buf
, 0);
6127 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
6129 else if (rs
->buf
[0] == '\0')
6130 error (_("Remote doesn't know how to detach"));
6132 error (_("Can't detach process."));
6135 /* This detaches a program to which we previously attached, using
6136 inferior_ptid to identify the process. After this is done, GDB
6137 can be used to debug some other program. We better not have left
6138 any breakpoints in the target program or it'll die when it hits
6142 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
6144 int pid
= inferior_ptid
.pid ();
6145 struct remote_state
*rs
= get_remote_state ();
6148 if (!target_has_execution ())
6149 error (_("No process to detach from."));
6151 target_announce_detach (from_tty
);
6153 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
6155 /* If we're in breakpoints-always-inserted mode, or the inferior
6156 is running, we have to remove breakpoints before detaching.
6157 We don't do this in common code instead because not all
6158 targets support removing breakpoints while the target is
6159 running. The remote target / gdbserver does, though. */
6160 remove_breakpoints_inf (current_inferior ());
6163 /* Tell the remote target to detach. */
6164 remote_detach_pid (pid
);
6166 /* Exit only if this is the only active inferior. */
6167 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
6168 gdb_puts (_("Ending remote debugging.\n"));
6170 /* See if any thread of the inferior we are detaching has a pending fork
6171 status. In that case, we must detach from the child resulting from
6173 for (thread_info
*thread
: inf
->non_exited_threads ())
6175 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
6180 remote_detach_pid (ws
->child_ptid ().pid ());
6183 /* Check also for any pending fork events in the stop reply queue. */
6184 remote_notif_get_pending_events (¬if_client_stop
);
6185 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6187 if (reply
->ptid
.pid () != pid
)
6190 if (!is_fork_status (reply
->ws
.kind ()))
6193 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
6196 thread_info
*tp
= this->find_thread (inferior_ptid
);
6198 /* Check to see if we are detaching a fork parent. Note that if we
6199 are detaching a fork child, tp == NULL. */
6200 is_fork_parent
= (tp
!= NULL
6201 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
6203 /* If doing detach-on-fork, we don't mourn, because that will delete
6204 breakpoints that should be available for the followed inferior. */
6205 if (!is_fork_parent
)
6207 /* Save the pid as a string before mourning, since that will
6208 unpush the remote target, and we need the string after. */
6209 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6211 target_mourn_inferior (inferior_ptid
);
6212 if (print_inferior_events
)
6213 gdb_printf (_("[Inferior %d (%s) detached]\n"),
6214 inf
->num
, infpid
.c_str ());
6218 switch_to_no_thread ();
6219 detach_inferior (current_inferior ());
6224 remote_target::detach (inferior
*inf
, int from_tty
)
6226 remote_detach_1 (inf
, from_tty
);
6230 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6232 remote_detach_1 (inf
, from_tty
);
6235 /* Target follow-fork function for remote targets. On entry, and
6236 at return, the current inferior is the fork parent.
6238 Note that although this is currently only used for extended-remote,
6239 it is named remote_follow_fork in anticipation of using it for the
6240 remote target as well. */
6243 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6244 target_waitkind fork_kind
, bool follow_child
,
6247 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6248 fork_kind
, follow_child
, detach_fork
);
6250 if ((fork_kind
== TARGET_WAITKIND_FORKED
6251 && m_features
.remote_fork_event_p ())
6252 || (fork_kind
== TARGET_WAITKIND_VFORKED
6253 && m_features
.remote_vfork_event_p ()))
6255 /* When following the parent and detaching the child, we detach
6256 the child here. For the case of following the child and
6257 detaching the parent, the detach is done in the target-
6258 independent follow fork code in infrun.c. We can't use
6259 target_detach when detaching an unfollowed child because
6260 the client side doesn't know anything about the child. */
6261 if (detach_fork
&& !follow_child
)
6263 /* Detach the fork child. */
6264 remote_detach_pid (child_ptid
.pid ());
6269 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6270 in the program space of the new inferior. */
6273 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6274 const char *execd_pathname
)
6276 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6278 /* We know that this is a target file name, so if it has the "target:"
6279 prefix we strip it off before saving it in the program space. */
6280 if (is_target_filename (execd_pathname
))
6281 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6283 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6286 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6289 remote_target::disconnect (const char *args
, int from_tty
)
6292 error (_("Argument given to \"disconnect\" when remotely debugging."));
6294 /* Make sure we unpush even the extended remote targets. Calling
6295 target_mourn_inferior won't unpush, and
6296 remote_target::mourn_inferior won't unpush if there is more than
6297 one inferior left. */
6298 remote_unpush_target (this);
6301 gdb_puts ("Ending remote debugging.\n");
6304 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6305 be chatty about it. */
6308 extended_remote_target::attach (const char *args
, int from_tty
)
6310 struct remote_state
*rs
= get_remote_state ();
6312 char *wait_status
= NULL
;
6314 pid
= parse_pid_to_attach (args
);
6316 /* Remote PID can be freely equal to getpid, do not check it here the same
6317 way as in other targets. */
6319 if (m_features
.packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6320 error (_("This target does not support attaching to a process"));
6322 target_announce_attach (from_tty
, pid
);
6324 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6326 getpkt (&rs
->buf
, 0);
6328 switch (m_features
.packet_ok (rs
->buf
, PACKET_vAttach
))
6331 if (!target_is_non_stop_p ())
6333 /* Save the reply for later. */
6334 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6335 strcpy (wait_status
, rs
->buf
.data ());
6337 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6338 error (_("Attaching to %s failed with: %s"),
6339 target_pid_to_str (ptid_t (pid
)).c_str (),
6342 case PACKET_UNKNOWN
:
6343 error (_("This target does not support attaching to a process"));
6345 error (_("Attaching to %s failed"),
6346 target_pid_to_str (ptid_t (pid
)).c_str ());
6349 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6351 inferior_ptid
= ptid_t (pid
);
6353 if (target_is_non_stop_p ())
6355 /* Get list of threads. */
6356 update_thread_list ();
6358 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6359 if (thread
!= nullptr)
6360 switch_to_thread (thread
);
6362 /* Invalidate our notion of the remote current thread. */
6363 record_currthread (rs
, minus_one_ptid
);
6367 /* Now, if we have thread information, update the main thread's
6369 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6371 /* Add the main thread to the thread list. We add the thread
6372 silently in this case (the final true parameter). */
6373 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6375 switch_to_thread (thr
);
6378 /* Next, if the target can specify a description, read it. We do
6379 this before anything involving memory or registers. */
6380 target_find_description ();
6382 if (!target_is_non_stop_p ())
6384 /* Use the previously fetched status. */
6385 gdb_assert (wait_status
!= NULL
);
6387 struct notif_event
*reply
6388 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6390 push_stop_reply ((struct stop_reply
*) reply
);
6394 gdb_assert (wait_status
== NULL
);
6396 gdb_assert (target_can_async_p ());
6400 /* Implementation of the to_post_attach method. */
6403 extended_remote_target::post_attach (int pid
)
6405 /* Get text, data & bss offsets. */
6408 /* In certain cases GDB might not have had the chance to start
6409 symbol lookup up until now. This could happen if the debugged
6410 binary is not using shared libraries, the vsyscall page is not
6411 present (on Linux) and the binary itself hadn't changed since the
6412 debugging process was started. */
6413 if (current_program_space
->symfile_object_file
!= NULL
)
6414 remote_check_symbols();
6418 /* Check for the availability of vCont. This function should also check
6422 remote_target::remote_vcont_probe ()
6424 remote_state
*rs
= get_remote_state ();
6427 strcpy (rs
->buf
.data (), "vCont?");
6429 getpkt (&rs
->buf
, 0);
6430 buf
= rs
->buf
.data ();
6432 /* Make sure that the features we assume are supported. */
6433 if (startswith (buf
, "vCont"))
6436 int support_c
, support_C
;
6438 rs
->supports_vCont
.s
= 0;
6439 rs
->supports_vCont
.S
= 0;
6442 rs
->supports_vCont
.t
= 0;
6443 rs
->supports_vCont
.r
= 0;
6444 while (p
&& *p
== ';')
6447 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6448 rs
->supports_vCont
.s
= 1;
6449 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6450 rs
->supports_vCont
.S
= 1;
6451 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6453 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6455 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6456 rs
->supports_vCont
.t
= 1;
6457 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6458 rs
->supports_vCont
.r
= 1;
6460 p
= strchr (p
, ';');
6463 /* If c, and C are not all supported, we can't use vCont. Clearing
6464 BUF will make packet_ok disable the packet. */
6465 if (!support_c
|| !support_C
)
6469 m_features
.packet_ok (rs
->buf
, PACKET_vCont
);
6472 /* Helper function for building "vCont" resumptions. Write a
6473 resumption to P. ENDP points to one-passed-the-end of the buffer
6474 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6475 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6476 resumed thread should be single-stepped and/or signalled. If PTID
6477 equals minus_one_ptid, then all threads are resumed; if PTID
6478 represents a process, then all threads of the process are
6482 remote_target::append_resumption (char *p
, char *endp
,
6483 ptid_t ptid
, int step
, gdb_signal siggnal
)
6485 struct remote_state
*rs
= get_remote_state ();
6487 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6488 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6490 /* GDB is willing to range step. */
6491 && use_range_stepping
6492 /* Target supports range stepping. */
6493 && rs
->supports_vCont
.r
6494 /* We don't currently support range stepping multiple
6495 threads with a wildcard (though the protocol allows it,
6496 so stubs shouldn't make an active effort to forbid
6498 && !(m_features
.remote_multi_process_p () && ptid
.is_pid ()))
6500 struct thread_info
*tp
;
6502 if (ptid
== minus_one_ptid
)
6504 /* If we don't know about the target thread's tid, then
6505 we're resuming magic_null_ptid (see caller). */
6506 tp
= this->find_thread (magic_null_ptid
);
6509 tp
= this->find_thread (ptid
);
6510 gdb_assert (tp
!= NULL
);
6512 if (tp
->control
.may_range_step
)
6514 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6516 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6517 phex_nz (tp
->control
.step_range_start
,
6519 phex_nz (tp
->control
.step_range_end
,
6523 p
+= xsnprintf (p
, endp
- p
, ";s");
6526 p
+= xsnprintf (p
, endp
- p
, ";s");
6527 else if (siggnal
!= GDB_SIGNAL_0
)
6528 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6530 p
+= xsnprintf (p
, endp
- p
, ";c");
6532 if (m_features
.remote_multi_process_p () && ptid
.is_pid ())
6536 /* All (-1) threads of process. */
6537 nptid
= ptid_t (ptid
.pid (), -1);
6539 p
+= xsnprintf (p
, endp
- p
, ":");
6540 p
= write_ptid (p
, endp
, nptid
);
6542 else if (ptid
!= minus_one_ptid
)
6544 p
+= xsnprintf (p
, endp
- p
, ":");
6545 p
= write_ptid (p
, endp
, ptid
);
6551 /* Clear the thread's private info on resume. */
6554 resume_clear_thread_private_info (struct thread_info
*thread
)
6556 if (thread
->priv
!= NULL
)
6558 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6560 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6561 priv
->watch_data_address
= 0;
6565 /* Append a vCont continue-with-signal action for threads that have a
6566 non-zero stop signal. */
6569 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6572 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6573 if (inferior_ptid
!= thread
->ptid
6574 && thread
->stop_signal () != GDB_SIGNAL_0
)
6576 p
= append_resumption (p
, endp
, thread
->ptid
,
6577 0, thread
->stop_signal ());
6578 thread
->set_stop_signal (GDB_SIGNAL_0
);
6579 resume_clear_thread_private_info (thread
);
6585 /* Set the target running, using the packets that use Hc
6589 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6592 struct remote_state
*rs
= get_remote_state ();
6595 rs
->last_sent_signal
= siggnal
;
6596 rs
->last_sent_step
= step
;
6598 /* The c/s/C/S resume packets use Hc, so set the continue
6600 if (ptid
== minus_one_ptid
)
6601 set_continue_thread (any_thread_ptid
);
6603 set_continue_thread (ptid
);
6605 for (thread_info
*thread
: all_non_exited_threads (this))
6606 resume_clear_thread_private_info (thread
);
6608 buf
= rs
->buf
.data ();
6609 if (::execution_direction
== EXEC_REVERSE
)
6611 /* We don't pass signals to the target in reverse exec mode. */
6612 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6613 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6616 if (step
&& m_features
.packet_support (PACKET_bs
) == PACKET_DISABLE
)
6617 error (_("Remote reverse-step not supported."));
6618 if (!step
&& m_features
.packet_support (PACKET_bc
) == PACKET_DISABLE
)
6619 error (_("Remote reverse-continue not supported."));
6621 strcpy (buf
, step
? "bs" : "bc");
6623 else if (siggnal
!= GDB_SIGNAL_0
)
6625 buf
[0] = step
? 'S' : 'C';
6626 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6627 buf
[2] = tohex (((int) siggnal
) & 0xf);
6631 strcpy (buf
, step
? "s" : "c");
6636 /* Resume the remote inferior by using a "vCont" packet. SCOPE_PTID,
6637 STEP, and SIGGNAL have the same meaning as in target_resume. This
6638 function returns non-zero iff it resumes the inferior.
6640 This function issues a strict subset of all possible vCont commands
6644 remote_target::remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
6645 enum gdb_signal siggnal
)
6647 struct remote_state
*rs
= get_remote_state ();
6651 /* No reverse execution actions defined for vCont. */
6652 if (::execution_direction
== EXEC_REVERSE
)
6655 if (m_features
.packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6658 p
= rs
->buf
.data ();
6659 endp
= p
+ get_remote_packet_size ();
6661 /* If we could generate a wider range of packets, we'd have to worry
6662 about overflowing BUF. Should there be a generic
6663 "multi-part-packet" packet? */
6665 p
+= xsnprintf (p
, endp
- p
, "vCont");
6667 if (scope_ptid
== magic_null_ptid
)
6669 /* MAGIC_NULL_PTID means that we don't have any active threads,
6670 so we don't have any TID numbers the inferior will
6671 understand. Make sure to only send forms that do not specify
6673 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6675 else if (scope_ptid
== minus_one_ptid
|| scope_ptid
.is_pid ())
6677 /* Resume all threads (of all processes, or of a single
6678 process), with preference for INFERIOR_PTID. This assumes
6679 inferior_ptid belongs to the set of all threads we are about
6681 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6683 /* Step inferior_ptid, with or without signal. */
6684 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6687 /* Also pass down any pending signaled resumption for other
6688 threads not the current. */
6689 p
= append_pending_thread_resumptions (p
, endp
, scope_ptid
);
6691 /* And continue others without a signal. */
6692 append_resumption (p
, endp
, scope_ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
6696 /* Scheduler locking; resume only SCOPE_PTID. */
6697 append_resumption (p
, endp
, scope_ptid
, step
, siggnal
);
6700 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
6703 if (target_is_non_stop_p ())
6705 /* In non-stop, the stub replies to vCont with "OK". The stop
6706 reply will be reported asynchronously by means of a `%Stop'
6708 getpkt (&rs
->buf
, 0);
6709 if (strcmp (rs
->buf
.data (), "OK") != 0)
6710 error (_("Unexpected vCont reply in non-stop mode: %s"),
6717 /* Tell the remote machine to resume. */
6720 remote_target::resume (ptid_t scope_ptid
, int step
, enum gdb_signal siggnal
)
6722 struct remote_state
*rs
= get_remote_state ();
6724 /* When connected in non-stop mode, the core resumes threads
6725 individually. Resuming remote threads directly in target_resume
6726 would thus result in sending one packet per thread. Instead, to
6727 minimize roundtrip latency, here we just store the resume
6728 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
6729 resumption will be done in remote_target::commit_resume, where we'll be
6730 able to do vCont action coalescing. */
6731 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
6733 remote_thread_info
*remote_thr
6734 = get_remote_thread_info (inferior_thread ());
6736 /* We don't expect the core to ask to resume an already resumed (from
6737 its point of view) thread. */
6738 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
6740 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
6742 /* There's actually nothing that says that the core can't
6743 request a wildcard resume in non-stop mode, though. It's
6744 just that we know it doesn't currently, so we don't bother
6746 gdb_assert (scope_ptid
== inferior_ptid
);
6750 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6751 (explained in remote-notif.c:handle_notification) so
6752 remote_notif_process is not called. We need find a place where
6753 it is safe to start a 'vNotif' sequence. It is good to do it
6754 before resuming inferior, because inferior was stopped and no RSP
6755 traffic at that moment. */
6756 if (!target_is_non_stop_p ())
6757 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
6759 rs
->last_resume_exec_dir
= ::execution_direction
;
6761 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6762 if (!remote_resume_with_vcont (scope_ptid
, step
, siggnal
))
6763 remote_resume_with_hc (scope_ptid
, step
, siggnal
);
6765 /* Update resumed state tracked by the remote target. */
6766 for (thread_info
*tp
: all_non_exited_threads (this, scope_ptid
))
6767 get_remote_thread_info (tp
)->set_resumed ();
6769 /* We've just told the target to resume. The remote server will
6770 wait for the inferior to stop, and then send a stop reply. In
6771 the mean time, we can't start another command/query ourselves
6772 because the stub wouldn't be ready to process it. This applies
6773 only to the base all-stop protocol, however. In non-stop (which
6774 only supports vCont), the stub replies with an "OK", and is
6775 immediate able to process further serial input. */
6776 if (!target_is_non_stop_p ())
6777 rs
->waiting_for_stop_reply
= 1;
6780 /* Private per-inferior info for target remote processes. */
6782 struct remote_inferior
: public private_inferior
6784 /* Whether we can send a wildcard vCont for this process. */
6785 bool may_wildcard_vcont
= true;
6788 /* Get the remote private inferior data associated to INF. */
6790 static remote_inferior
*
6791 get_remote_inferior (inferior
*inf
)
6793 if (inf
->priv
== NULL
)
6794 inf
->priv
.reset (new remote_inferior
);
6796 return gdb::checked_static_cast
<remote_inferior
*> (inf
->priv
.get ());
6799 /* Class used to track the construction of a vCont packet in the
6800 outgoing packet buffer. This is used to send multiple vCont
6801 packets if we have more actions than would fit a single packet. */
6806 explicit vcont_builder (remote_target
*remote
)
6813 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
6818 /* The remote target. */
6819 remote_target
*m_remote
;
6821 /* Pointer to the first action. P points here if no action has been
6823 char *m_first_action
;
6825 /* Where the next action will be appended. */
6828 /* The end of the buffer. Must never write past this. */
6832 /* Prepare the outgoing buffer for a new vCont packet. */
6835 vcont_builder::restart ()
6837 struct remote_state
*rs
= m_remote
->get_remote_state ();
6839 m_p
= rs
->buf
.data ();
6840 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
6841 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
6842 m_first_action
= m_p
;
6845 /* If the vCont packet being built has any action, send it to the
6849 vcont_builder::flush ()
6851 struct remote_state
*rs
;
6853 if (m_p
== m_first_action
)
6856 rs
= m_remote
->get_remote_state ();
6857 m_remote
->putpkt (rs
->buf
);
6858 m_remote
->getpkt (&rs
->buf
, 0);
6859 if (strcmp (rs
->buf
.data (), "OK") != 0)
6860 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
6863 /* The largest action is range-stepping, with its two addresses. This
6864 is more than sufficient. If a new, bigger action is created, it'll
6865 quickly trigger a failed assertion in append_resumption (and we'll
6867 #define MAX_ACTION_SIZE 200
6869 /* Append a new vCont action in the outgoing packet being built. If
6870 the action doesn't fit the packet along with previous actions, push
6871 what we've got so far to the remote end and start over a new vCont
6872 packet (with the new action). */
6875 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
6877 char buf
[MAX_ACTION_SIZE
+ 1];
6879 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
6880 ptid
, step
, siggnal
);
6882 /* Check whether this new action would fit in the vCont packet along
6883 with previous actions. If not, send what we've got so far and
6884 start a new vCont packet. */
6885 size_t rsize
= endp
- buf
;
6886 if (rsize
> m_endp
- m_p
)
6891 /* Should now fit. */
6892 gdb_assert (rsize
<= m_endp
- m_p
);
6895 memcpy (m_p
, buf
, rsize
);
6900 /* to_commit_resume implementation. */
6903 remote_target::commit_resumed ()
6905 /* If connected in all-stop mode, we'd send the remote resume
6906 request directly from remote_resume. Likewise if
6907 reverse-debugging, as there are no defined vCont actions for
6908 reverse execution. */
6909 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
6912 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6913 instead of resuming all threads of each process individually.
6914 However, if any thread of a process must remain halted, we can't
6915 send wildcard resumes and must send one action per thread.
6917 Care must be taken to not resume threads/processes the server
6918 side already told us are stopped, but the core doesn't know about
6919 yet, because the events are still in the vStopped notification
6922 #1 => vCont s:p1.1;c
6924 #3 <= %Stopped T05 p1.1
6929 #8 (infrun handles the stop for p1.1 and continues stepping)
6930 #9 => vCont s:p1.1;c
6932 The last vCont above would resume thread p1.2 by mistake, because
6933 the server has no idea that the event for p1.2 had not been
6936 The server side must similarly ignore resume actions for the
6937 thread that has a pending %Stopped notification (and any other
6938 threads with events pending), until GDB acks the notification
6939 with vStopped. Otherwise, e.g., the following case is
6942 #1 => g (or any other packet)
6944 #3 <= %Stopped T05 p1.2
6945 #4 => vCont s:p1.1;c
6948 Above, the server must not resume thread p1.2. GDB can't know
6949 that p1.2 stopped until it acks the %Stopped notification, and
6950 since from GDB's perspective all threads should be running, it
6953 Finally, special care must also be given to handling fork/vfork
6954 events. A (v)fork event actually tells us that two processes
6955 stopped -- the parent and the child. Until we follow the fork,
6956 we must not resume the child. Therefore, if we have a pending
6957 fork follow, we must not send a global wildcard resume action
6958 (vCont;c). We can still send process-wide wildcards though. */
6960 /* Start by assuming a global wildcard (vCont;c) is possible. */
6961 bool may_global_wildcard_vcont
= true;
6963 /* And assume every process is individually wildcard-able too. */
6964 for (inferior
*inf
: all_non_exited_inferiors (this))
6966 remote_inferior
*priv
= get_remote_inferior (inf
);
6968 priv
->may_wildcard_vcont
= true;
6971 /* Check for any pending events (not reported or processed yet) and
6972 disable process and global wildcard resumes appropriately. */
6973 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
6975 bool any_pending_vcont_resume
= false;
6977 for (thread_info
*tp
: all_non_exited_threads (this))
6979 remote_thread_info
*priv
= get_remote_thread_info (tp
);
6981 /* If a thread of a process is not meant to be resumed, then we
6982 can't wildcard that process. */
6983 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
6985 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
6987 /* And if we can't wildcard a process, we can't wildcard
6988 everything either. */
6989 may_global_wildcard_vcont
= false;
6993 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
6994 any_pending_vcont_resume
= true;
6996 /* If a thread is the parent of an unfollowed fork, then we
6997 can't do a global wildcard, as that would resume the fork
6999 if (thread_pending_fork_status (tp
) != nullptr)
7000 may_global_wildcard_vcont
= false;
7003 /* We didn't have any resumed thread pending a vCont resume, so nothing to
7005 if (!any_pending_vcont_resume
)
7008 /* Now let's build the vCont packet(s). Actions must be appended
7009 from narrower to wider scopes (thread -> process -> global). If
7010 we end up with too many actions for a single packet vcont_builder
7011 flushes the current vCont packet to the remote side and starts a
7013 struct vcont_builder
vcont_builder (this);
7015 /* Threads first. */
7016 for (thread_info
*tp
: all_non_exited_threads (this))
7018 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7020 /* If the thread was previously vCont-resumed, no need to send a specific
7021 action for it. If we didn't receive a resume request for it, don't
7022 send an action for it either. */
7023 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
7026 gdb_assert (!thread_is_in_step_over_chain (tp
));
7028 /* We should never be commit-resuming a thread that has a stop reply.
7029 Otherwise, we would end up reporting a stop event for a thread while
7030 it is running on the remote target. */
7031 remote_state
*rs
= get_remote_state ();
7032 for (const auto &stop_reply
: rs
->stop_reply_queue
)
7033 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
7035 const resumed_pending_vcont_info
&info
7036 = remote_thr
->resumed_pending_vcont_info ();
7038 /* Check if we need to send a specific action for this thread. If not,
7039 it will be included in a wildcard resume instead. */
7040 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
7041 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
7042 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
7044 remote_thr
->set_resumed ();
7047 /* Now check whether we can send any process-wide wildcard. This is
7048 to avoid sending a global wildcard in the case nothing is
7049 supposed to be resumed. */
7050 bool any_process_wildcard
= false;
7052 for (inferior
*inf
: all_non_exited_inferiors (this))
7054 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7056 any_process_wildcard
= true;
7061 if (any_process_wildcard
)
7063 /* If all processes are wildcard-able, then send a single "c"
7064 action, otherwise, send an "all (-1) threads of process"
7065 continue action for each running process, if any. */
7066 if (may_global_wildcard_vcont
)
7068 vcont_builder
.push_action (minus_one_ptid
,
7069 false, GDB_SIGNAL_0
);
7073 for (inferior
*inf
: all_non_exited_inferiors (this))
7075 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7077 vcont_builder
.push_action (ptid_t (inf
->pid
),
7078 false, GDB_SIGNAL_0
);
7084 vcont_builder
.flush ();
7087 /* Implementation of target_has_pending_events. */
7090 remote_target::has_pending_events ()
7092 if (target_can_async_p ())
7094 remote_state
*rs
= get_remote_state ();
7096 if (async_event_handler_marked (rs
->remote_async_inferior_event_token
))
7099 /* Note that BUFCNT can be negative, indicating sticky
7101 if (rs
->remote_desc
->bufcnt
!= 0)
7109 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
7110 thread, all threads of a remote process, or all threads of all
7114 remote_target::remote_stop_ns (ptid_t ptid
)
7116 struct remote_state
*rs
= get_remote_state ();
7117 char *p
= rs
->buf
.data ();
7118 char *endp
= p
+ get_remote_packet_size ();
7120 /* If any thread that needs to stop was resumed but pending a vCont
7121 resume, generate a phony stop_reply. However, first check
7122 whether the thread wasn't resumed with a signal. Generating a
7123 phony stop in that case would result in losing the signal. */
7124 bool needs_commit
= false;
7125 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7127 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7129 if (remote_thr
->get_resume_state ()
7130 == resume_state::RESUMED_PENDING_VCONT
)
7132 const resumed_pending_vcont_info
&info
7133 = remote_thr
->resumed_pending_vcont_info ();
7134 if (info
.sig
!= GDB_SIGNAL_0
)
7136 /* This signal must be forwarded to the inferior. We
7137 could commit-resume just this thread, but its simpler
7138 to just commit-resume everything. */
7139 needs_commit
= true;
7148 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7150 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7152 if (remote_thr
->get_resume_state ()
7153 == resume_state::RESUMED_PENDING_VCONT
)
7155 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
7156 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
7158 pulongest (tp
->ptid
.tid ()));
7160 /* Check that the thread wasn't resumed with a signal.
7161 Generating a phony stop would result in losing the
7163 const resumed_pending_vcont_info
&info
7164 = remote_thr
->resumed_pending_vcont_info ();
7165 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
7167 stop_reply
*sr
= new stop_reply ();
7168 sr
->ptid
= tp
->ptid
;
7170 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
7171 sr
->arch
= tp
->inf
->gdbarch
;
7172 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7173 sr
->watch_data_address
= 0;
7175 this->push_stop_reply (sr
);
7177 /* Pretend that this thread was actually resumed on the
7178 remote target, then stopped. If we leave it in the
7179 RESUMED_PENDING_VCONT state and the commit_resumed
7180 method is called while the stop reply is still in the
7181 queue, we'll end up reporting a stop event to the core
7182 for that thread while it is running on the remote
7183 target... that would be bad. */
7184 remote_thr
->set_resumed ();
7188 if (!rs
->supports_vCont
.t
)
7189 error (_("Remote server does not support stopping threads"));
7191 if (ptid
== minus_one_ptid
7192 || (!m_features
.remote_multi_process_p () && ptid
.is_pid ()))
7193 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7198 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7201 /* All (-1) threads of process. */
7202 nptid
= ptid_t (ptid
.pid (), -1);
7205 /* Small optimization: if we already have a stop reply for
7206 this thread, no use in telling the stub we want this
7208 if (peek_stop_reply (ptid
))
7214 write_ptid (p
, endp
, nptid
);
7217 /* In non-stop, we get an immediate OK reply. The stop reply will
7218 come in asynchronously by notification. */
7220 getpkt (&rs
->buf
, 0);
7221 if (strcmp (rs
->buf
.data (), "OK") != 0)
7222 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7226 /* All-stop version of target_interrupt. Sends a break or a ^C to
7227 interrupt the remote target. It is undefined which thread of which
7228 process reports the interrupt. */
7231 remote_target::remote_interrupt_as ()
7233 struct remote_state
*rs
= get_remote_state ();
7235 rs
->ctrlc_pending_p
= 1;
7237 /* If the inferior is stopped already, but the core didn't know
7238 about it yet, just ignore the request. The pending stop events
7239 will be collected in remote_wait. */
7240 if (stop_reply_queue_length () > 0)
7243 /* Send interrupt_sequence to remote target. */
7244 send_interrupt_sequence ();
7247 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7248 the remote target. It is undefined which thread of which process
7249 reports the interrupt. Throws an error if the packet is not
7250 supported by the server. */
7253 remote_target::remote_interrupt_ns ()
7255 struct remote_state
*rs
= get_remote_state ();
7256 char *p
= rs
->buf
.data ();
7257 char *endp
= p
+ get_remote_packet_size ();
7259 xsnprintf (p
, endp
- p
, "vCtrlC");
7261 /* In non-stop, we get an immediate OK reply. The stop reply will
7262 come in asynchronously by notification. */
7264 getpkt (&rs
->buf
, 0);
7266 switch (m_features
.packet_ok (rs
->buf
, PACKET_vCtrlC
))
7270 case PACKET_UNKNOWN
:
7271 error (_("No support for interrupting the remote target."));
7273 error (_("Interrupting target failed: %s"), rs
->buf
.data ());
7277 /* Implement the to_stop function for the remote targets. */
7280 remote_target::stop (ptid_t ptid
)
7282 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7284 if (target_is_non_stop_p ())
7285 remote_stop_ns (ptid
);
7288 /* We don't currently have a way to transparently pause the
7289 remote target in all-stop mode. Interrupt it instead. */
7290 remote_interrupt_as ();
7294 /* Implement the to_interrupt function for the remote targets. */
7297 remote_target::interrupt ()
7299 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7301 if (target_is_non_stop_p ())
7302 remote_interrupt_ns ();
7304 remote_interrupt_as ();
7307 /* Implement the to_pass_ctrlc function for the remote targets. */
7310 remote_target::pass_ctrlc ()
7312 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7314 struct remote_state
*rs
= get_remote_state ();
7316 /* If we're starting up, we're not fully synced yet. Quit
7318 if (rs
->starting_up
)
7320 /* If ^C has already been sent once, offer to disconnect. */
7321 else if (rs
->ctrlc_pending_p
)
7324 target_interrupt ();
7327 /* Ask the user what to do when an interrupt is received. */
7330 remote_target::interrupt_query ()
7332 struct remote_state
*rs
= get_remote_state ();
7334 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7336 if (query (_("The target is not responding to interrupt requests.\n"
7337 "Stop debugging it? ")))
7339 remote_unpush_target (this);
7340 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7345 if (query (_("Interrupted while waiting for the program.\n"
7346 "Give up waiting? ")))
7351 /* Enable/disable target terminal ownership. Most targets can use
7352 terminal groups to control terminal ownership. Remote targets are
7353 different in that explicit transfer of ownership to/from GDB/target
7357 remote_target::terminal_inferior ()
7359 /* NOTE: At this point we could also register our selves as the
7360 recipient of all input. Any characters typed could then be
7361 passed on down to the target. */
7365 remote_target::terminal_ours ()
7370 remote_console_output (const char *msg
)
7374 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7377 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7381 gdb_stdtarg
->puts (tb
);
7383 gdb_stdtarg
->flush ();
7386 /* Return the length of the stop reply queue. */
7389 remote_target::stop_reply_queue_length ()
7391 remote_state
*rs
= get_remote_state ();
7392 return rs
->stop_reply_queue
.size ();
7396 remote_notif_stop_parse (remote_target
*remote
,
7397 const notif_client
*self
, const char *buf
,
7398 struct notif_event
*event
)
7400 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7404 remote_notif_stop_ack (remote_target
*remote
,
7405 const notif_client
*self
, const char *buf
,
7406 struct notif_event
*event
)
7408 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
7411 putpkt (remote
, self
->ack_command
);
7413 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7414 the notification. It was left in the queue because we need to
7415 acknowledge it and pull the rest of the notifications out. */
7416 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7417 remote
->push_stop_reply (stop_reply
);
7421 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7422 const notif_client
*self
)
7424 /* We can't get pending events in remote_notif_process for
7425 notification stop, and we have to do this in remote_wait_ns
7426 instead. If we fetch all queued events from stub, remote stub
7427 may exit and we have no chance to process them back in
7429 remote_state
*rs
= remote
->get_remote_state ();
7430 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7434 stop_reply::~stop_reply ()
7436 for (cached_reg_t
®
: regcache
)
7440 static notif_event_up
7441 remote_notif_stop_alloc_reply ()
7443 return notif_event_up (new struct stop_reply ());
7446 /* A client of notification Stop. */
7448 const notif_client notif_client_stop
=
7452 remote_notif_stop_parse
,
7453 remote_notif_stop_ack
,
7454 remote_notif_stop_can_get_pending_events
,
7455 remote_notif_stop_alloc_reply
,
7459 /* If CONTEXT contains any fork child threads that have not been
7460 reported yet, remove them from the CONTEXT list. If such a
7461 thread exists it is because we are stopped at a fork catchpoint
7462 and have not yet called follow_fork, which will set up the
7463 host-side data structures for the new process. */
7466 remote_target::remove_new_fork_children (threads_listing_context
*context
)
7468 const notif_client
*notif
= ¬if_client_stop
;
7470 /* For any threads stopped at a fork event, remove the corresponding
7471 fork child threads from the CONTEXT list. */
7472 for (thread_info
*thread
: all_non_exited_threads (this))
7474 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
7479 context
->remove_thread (ws
->child_ptid ());
7482 /* Check for any pending fork events (not reported or processed yet)
7483 in process PID and remove those fork child threads from the
7484 CONTEXT list as well. */
7485 remote_notif_get_pending_events (notif
);
7486 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7487 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7488 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7489 context
->remove_thread (event
->ws
.child_ptid ());
7490 else if (event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7491 context
->remove_thread (event
->ptid
);
7494 /* Check whether any event pending in the vStopped queue would prevent a
7495 global or process wildcard vCont action. Set *may_global_wildcard to
7496 false if we can't do a global wildcard (vCont;c), and clear the event
7497 inferior's may_wildcard_vcont flag if we can't do a process-wide
7498 wildcard resume (vCont;c:pPID.-1). */
7501 remote_target::check_pending_events_prevent_wildcard_vcont
7502 (bool *may_global_wildcard
)
7504 const notif_client
*notif
= ¬if_client_stop
;
7506 remote_notif_get_pending_events (notif
);
7507 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7509 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7510 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7513 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7514 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7515 *may_global_wildcard
= false;
7517 /* This may be the first time we heard about this process.
7518 Regardless, we must not do a global wildcard resume, otherwise
7519 we'd resume this process too. */
7520 *may_global_wildcard
= false;
7521 if (event
->ptid
!= null_ptid
)
7523 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7525 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7530 /* Discard all pending stop replies of inferior INF. */
7533 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7535 struct stop_reply
*reply
;
7536 struct remote_state
*rs
= get_remote_state ();
7537 struct remote_notif_state
*rns
= rs
->notif_state
;
7539 /* This function can be notified when an inferior exists. When the
7540 target is not remote, the notification state is NULL. */
7541 if (rs
->remote_desc
== NULL
)
7544 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
7546 /* Discard the in-flight notification. */
7547 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7549 /* Leave the notification pending, since the server expects that
7550 we acknowledge it with vStopped. But clear its contents, so
7551 that later on when we acknowledge it, we also discard it. */
7553 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7554 reply
->ptid
.to_string().c_str(),
7555 reply
->ws
.to_string ().c_str ());
7556 reply
->ws
.set_ignore ();
7559 /* Discard the stop replies we have already pulled with
7561 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7562 rs
->stop_reply_queue
.end (),
7563 [=] (const stop_reply_up
&event
)
7565 return event
->ptid
.pid () == inf
->pid
;
7567 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7569 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7570 reply
->ptid
.to_string().c_str(),
7571 reply
->ws
.to_string ().c_str ());
7572 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7575 /* Discard the stop replies for RS in stop_reply_queue. */
7578 remote_target::discard_pending_stop_replies_in_queue ()
7580 remote_state
*rs
= get_remote_state ();
7582 /* Discard the stop replies we have already pulled with
7584 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7585 rs
->stop_reply_queue
.end (),
7586 [=] (const stop_reply_up
&event
)
7588 return event
->rs
== rs
;
7590 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7593 /* Remove the first reply in 'stop_reply_queue' which matches
7597 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7599 remote_state
*rs
= get_remote_state ();
7601 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7602 rs
->stop_reply_queue
.end (),
7603 [=] (const stop_reply_up
&event
)
7605 return event
->ptid
.matches (ptid
);
7607 struct stop_reply
*result
;
7608 if (iter
== rs
->stop_reply_queue
.end ())
7612 result
= iter
->release ();
7613 rs
->stop_reply_queue
.erase (iter
);
7617 gdb_printf (gdb_stdlog
,
7618 "notif: discard queued event: 'Stop' in %s\n",
7619 ptid
.to_string ().c_str ());
7624 /* Look for a queued stop reply belonging to PTID. If one is found,
7625 remove it from the queue, and return it. Returns NULL if none is
7626 found. If there are still queued events left to process, tell the
7627 event loop to get back to target_wait soon. */
7630 remote_target::queued_stop_reply (ptid_t ptid
)
7632 remote_state
*rs
= get_remote_state ();
7633 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
7635 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7637 /* There's still at least an event left. */
7638 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7644 /* Push a fully parsed stop reply in the stop reply queue. Since we
7645 know that we now have at least one queued event left to pass to the
7646 core side, tell the event loop to get back to target_wait soon. */
7649 remote_target::push_stop_reply (struct stop_reply
*new_event
)
7651 remote_state
*rs
= get_remote_state ();
7652 rs
->stop_reply_queue
.push_back (stop_reply_up (new_event
));
7655 gdb_printf (gdb_stdlog
,
7656 "notif: push 'Stop' %s to queue %d\n",
7657 new_event
->ptid
.to_string ().c_str (),
7658 int (rs
->stop_reply_queue
.size ()));
7660 /* Mark the pending event queue only if async mode is currently enabled.
7661 If async mode is not currently enabled, then, if it later becomes
7662 enabled, and there are events in this queue, we will mark the event
7663 token at that point, see remote_target::async. */
7664 if (target_is_async_p ())
7665 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7668 /* Returns true if we have a stop reply for PTID. */
7671 remote_target::peek_stop_reply (ptid_t ptid
)
7673 remote_state
*rs
= get_remote_state ();
7674 for (auto &event
: rs
->stop_reply_queue
)
7675 if (ptid
== event
->ptid
7676 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7681 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7682 starting with P and ending with PEND matches PREFIX. */
7685 strprefix (const char *p
, const char *pend
, const char *prefix
)
7687 for ( ; p
< pend
; p
++, prefix
++)
7690 return *prefix
== '\0';
7693 /* Parse the stop reply in BUF. Either the function succeeds, and the
7694 result is stored in EVENT, or throws an error. */
7697 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
7699 remote_arch_state
*rsa
= NULL
;
7704 event
->ptid
= null_ptid
;
7705 event
->rs
= get_remote_state ();
7706 event
->ws
.set_ignore ();
7707 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7708 event
->regcache
.clear ();
7713 case 'T': /* Status with PC, SP, FP, ... */
7714 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7715 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7717 n... = register number
7718 r... = register contents
7721 p
= &buf
[3]; /* after Txx */
7727 p1
= strchr (p
, ':');
7729 error (_("Malformed packet(a) (missing colon): %s\n\
7733 error (_("Malformed packet(a) (missing register number): %s\n\
7737 /* Some "registers" are actually extended stop information.
7738 Note if you're adding a new entry here: GDB 7.9 and
7739 earlier assume that all register "numbers" that start
7740 with an hex digit are real register numbers. Make sure
7741 the server only sends such a packet if it knows the
7742 client understands it. */
7744 if (strprefix (p
, p1
, "thread"))
7745 event
->ptid
= read_ptid (++p1
, &p
);
7746 else if (strprefix (p
, p1
, "syscall_entry"))
7750 p
= unpack_varlen_hex (++p1
, &sysno
);
7751 event
->ws
.set_syscall_entry ((int) sysno
);
7753 else if (strprefix (p
, p1
, "syscall_return"))
7757 p
= unpack_varlen_hex (++p1
, &sysno
);
7758 event
->ws
.set_syscall_return ((int) sysno
);
7760 else if (strprefix (p
, p1
, "watch")
7761 || strprefix (p
, p1
, "rwatch")
7762 || strprefix (p
, p1
, "awatch"))
7764 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
7765 p
= unpack_varlen_hex (++p1
, &addr
);
7766 event
->watch_data_address
= (CORE_ADDR
) addr
;
7768 else if (strprefix (p
, p1
, "swbreak"))
7770 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
7772 /* Make sure the stub doesn't forget to indicate support
7774 if (m_features
.packet_support (PACKET_swbreak_feature
)
7776 error (_("Unexpected swbreak stop reason"));
7778 /* The value part is documented as "must be empty",
7779 though we ignore it, in case we ever decide to make
7780 use of it in a backward compatible way. */
7781 p
= strchrnul (p1
+ 1, ';');
7783 else if (strprefix (p
, p1
, "hwbreak"))
7785 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
7787 /* Make sure the stub doesn't forget to indicate support
7789 if (m_features
.packet_support (PACKET_hwbreak_feature
)
7791 error (_("Unexpected hwbreak stop reason"));
7794 p
= strchrnul (p1
+ 1, ';');
7796 else if (strprefix (p
, p1
, "library"))
7798 event
->ws
.set_loaded ();
7799 p
= strchrnul (p1
+ 1, ';');
7801 else if (strprefix (p
, p1
, "replaylog"))
7803 event
->ws
.set_no_history ();
7804 /* p1 will indicate "begin" or "end", but it makes
7805 no difference for now, so ignore it. */
7806 p
= strchrnul (p1
+ 1, ';');
7808 else if (strprefix (p
, p1
, "core"))
7812 p
= unpack_varlen_hex (++p1
, &c
);
7815 else if (strprefix (p
, p1
, "fork"))
7816 event
->ws
.set_forked (read_ptid (++p1
, &p
));
7817 else if (strprefix (p
, p1
, "vfork"))
7818 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
7819 else if (strprefix (p
, p1
, "vforkdone"))
7821 event
->ws
.set_vfork_done ();
7822 p
= strchrnul (p1
+ 1, ';');
7824 else if (strprefix (p
, p1
, "exec"))
7829 /* Determine the length of the execd pathname. */
7830 p
= unpack_varlen_hex (++p1
, &ignored
);
7831 pathlen
= (p
- p1
) / 2;
7833 /* Save the pathname for event reporting and for
7834 the next run command. */
7835 gdb::unique_xmalloc_ptr
<char> pathname
7836 ((char *) xmalloc (pathlen
+ 1));
7837 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
7838 pathname
.get ()[pathlen
] = '\0';
7840 /* This is freed during event handling. */
7841 event
->ws
.set_execd (std::move (pathname
));
7843 /* Skip the registers included in this packet, since
7844 they may be for an architecture different from the
7845 one used by the original program. */
7848 else if (strprefix (p
, p1
, "create"))
7850 event
->ws
.set_thread_created ();
7851 p
= strchrnul (p1
+ 1, ';');
7860 p
= strchrnul (p1
+ 1, ';');
7865 /* Maybe a real ``P'' register number. */
7866 p_temp
= unpack_varlen_hex (p
, &pnum
);
7867 /* If the first invalid character is the colon, we got a
7868 register number. Otherwise, it's an unknown stop
7872 /* If we haven't parsed the event's thread yet, find
7873 it now, in order to find the architecture of the
7874 reported expedited registers. */
7875 if (event
->ptid
== null_ptid
)
7877 /* If there is no thread-id information then leave
7878 the event->ptid as null_ptid. Later in
7879 process_stop_reply we will pick a suitable
7881 const char *thr
= strstr (p1
+ 1, ";thread:");
7883 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
7890 = (event
->ptid
== null_ptid
7892 : find_inferior_ptid (this, event
->ptid
));
7893 /* If this is the first time we learn anything
7894 about this process, skip the registers
7895 included in this packet, since we don't yet
7896 know which architecture to use to parse them.
7897 We'll determine the architecture later when
7898 we process the stop reply and retrieve the
7899 target description, via
7900 remote_notice_new_inferior ->
7901 post_create_inferior. */
7904 p
= strchrnul (p1
+ 1, ';');
7909 event
->arch
= inf
->gdbarch
;
7910 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
7914 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
7915 cached_reg_t cached_reg
;
7918 error (_("Remote sent bad register number %s: %s\n\
7920 hex_string (pnum
), p
, buf
);
7922 cached_reg
.num
= reg
->regnum
;
7923 cached_reg
.data
= (gdb_byte
*)
7924 xmalloc (register_size (event
->arch
, reg
->regnum
));
7927 fieldsize
= hex2bin (p
, cached_reg
.data
,
7928 register_size (event
->arch
, reg
->regnum
));
7930 if (fieldsize
< register_size (event
->arch
, reg
->regnum
))
7931 warning (_("Remote reply is too short: %s"), buf
);
7933 event
->regcache
.push_back (cached_reg
);
7937 /* Not a number. Silently skip unknown optional
7939 p
= strchrnul (p1
+ 1, ';');
7944 error (_("Remote register badly formatted: %s\nhere: %s"),
7949 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7953 case 'S': /* Old style status, just signal only. */
7957 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
7958 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
7959 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
7961 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
7964 case 'w': /* Thread exited. */
7968 p
= unpack_varlen_hex (&buf
[1], &value
);
7969 event
->ws
.set_thread_exited (value
);
7971 error (_("stop reply packet badly formatted: %s"), buf
);
7972 event
->ptid
= read_ptid (++p
, NULL
);
7975 case 'W': /* Target exited. */
7980 /* GDB used to accept only 2 hex chars here. Stubs should
7981 only send more if they detect GDB supports multi-process
7983 p
= unpack_varlen_hex (&buf
[1], &value
);
7987 /* The remote process exited. */
7988 event
->ws
.set_exited (value
);
7992 /* The remote process exited with a signal. */
7993 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
7994 event
->ws
.set_signalled ((enum gdb_signal
) value
);
7996 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
7999 /* If no process is specified, return null_ptid, and let the
8000 caller figure out the right process to use. */
8010 else if (startswith (p
, "process:"))
8014 p
+= sizeof ("process:") - 1;
8015 unpack_varlen_hex (p
, &upid
);
8019 error (_("unknown stop reply packet: %s"), buf
);
8022 error (_("unknown stop reply packet: %s"), buf
);
8023 event
->ptid
= ptid_t (pid
);
8027 event
->ws
.set_no_resumed ();
8028 event
->ptid
= minus_one_ptid
;
8033 /* When the stub wants to tell GDB about a new notification reply, it
8034 sends a notification (%Stop, for example). Those can come it at
8035 any time, hence, we have to make sure that any pending
8036 putpkt/getpkt sequence we're making is finished, before querying
8037 the stub for more events with the corresponding ack command
8038 (vStopped, for example). E.g., if we started a vStopped sequence
8039 immediately upon receiving the notification, something like this
8047 1.6) <-- (registers reply to step #1.3)
8049 Obviously, the reply in step #1.6 would be unexpected to a vStopped
8052 To solve this, whenever we parse a %Stop notification successfully,
8053 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
8054 doing whatever we were doing:
8060 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
8061 2.5) <-- (registers reply to step #2.3)
8063 Eventually after step #2.5, we return to the event loop, which
8064 notices there's an event on the
8065 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
8066 associated callback --- the function below. At this point, we're
8067 always safe to start a vStopped sequence. :
8070 2.7) <-- T05 thread:2
8076 remote_target::remote_notif_get_pending_events (const notif_client
*nc
)
8078 struct remote_state
*rs
= get_remote_state ();
8080 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
8083 gdb_printf (gdb_stdlog
,
8084 "notif: process: '%s' ack pending event\n",
8088 nc
->ack (this, nc
, rs
->buf
.data (),
8089 rs
->notif_state
->pending_event
[nc
->id
]);
8090 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
8094 getpkt (&rs
->buf
, 0);
8095 if (strcmp (rs
->buf
.data (), "OK") == 0)
8098 remote_notif_ack (this, nc
, rs
->buf
.data ());
8104 gdb_printf (gdb_stdlog
,
8105 "notif: process: '%s' no pending reply\n",
8110 /* Wrapper around remote_target::remote_notif_get_pending_events to
8111 avoid having to export the whole remote_target class. */
8114 remote_notif_get_pending_events (remote_target
*remote
, const notif_client
*nc
)
8116 remote
->remote_notif_get_pending_events (nc
);
8119 /* Called from process_stop_reply when the stop packet we are responding
8120 to didn't include a process-id or thread-id. STATUS is the stop event
8121 we are responding to.
8123 It is the task of this function to select a suitable thread (or process)
8124 and return its ptid, this is the thread (or process) we will assume the
8125 stop event came from.
8127 In some cases there isn't really any choice about which thread (or
8128 process) is selected, a basic remote with a single process containing a
8129 single thread might choose not to send any process-id or thread-id in
8130 its stop packets, this function will select and return the one and only
8133 However, if a target supports multiple threads (or processes) and still
8134 doesn't include a thread-id (or process-id) in its stop packet then
8135 first, this is a badly behaving target, and second, we're going to have
8136 to select a thread (or process) at random and use that. This function
8137 will print a warning to the user if it detects that there is the
8138 possibility that GDB is guessing which thread (or process) to
8141 Note that this is called before GDB fetches the updated thread list from the
8142 target. So it's possible for the stop reply to be ambiguous and for GDB to
8143 not realize it. For example, if there's initially one thread, the target
8144 spawns a second thread, and then sends a stop reply without an id that
8145 concerns the first thread. GDB will assume the stop reply is about the
8146 first thread - the only thread it knows about - without printing a warning.
8147 Anyway, if the remote meant for the stop reply to be about the second thread,
8148 then it would be really broken, because GDB doesn't know about that thread
8152 remote_target::select_thread_for_ambiguous_stop_reply
8153 (const target_waitstatus
&status
)
8155 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8157 /* Some stop events apply to all threads in an inferior, while others
8158 only apply to a single thread. */
8159 bool process_wide_stop
8160 = (status
.kind () == TARGET_WAITKIND_EXITED
8161 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
8163 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
8165 thread_info
*first_resumed_thread
= nullptr;
8166 bool ambiguous
= false;
8168 /* Consider all non-exited threads of the target, find the first resumed
8170 for (thread_info
*thr
: all_non_exited_threads (this))
8172 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
8174 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
8177 if (first_resumed_thread
== nullptr)
8178 first_resumed_thread
= thr
;
8179 else if (!process_wide_stop
8180 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8184 gdb_assert (first_resumed_thread
!= nullptr);
8186 remote_debug_printf ("first resumed thread is %s",
8187 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8188 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8190 /* Warn if the remote target is sending ambiguous stop replies. */
8193 static bool warned
= false;
8197 /* If you are seeing this warning then the remote target has
8198 stopped without specifying a thread-id, but the target
8199 does have multiple threads (or inferiors), and so GDB is
8200 having to guess which thread stopped.
8202 Examples of what might cause this are the target sending
8203 and 'S' stop packet, or a 'T' stop packet and not
8204 including a thread-id.
8206 Additionally, the target might send a 'W' or 'X packet
8207 without including a process-id, when the target has
8208 multiple running inferiors. */
8209 if (process_wide_stop
)
8210 warning (_("multi-inferior target stopped without "
8211 "sending a process-id, using first "
8212 "non-exited inferior"));
8214 warning (_("multi-threaded target stopped without "
8215 "sending a thread-id, using first "
8216 "non-exited thread"));
8221 /* If this is a stop for all threads then don't use a particular threads
8222 ptid, instead create a new ptid where only the pid field is set. */
8223 if (process_wide_stop
)
8224 return ptid_t (first_resumed_thread
->ptid
.pid ());
8226 return first_resumed_thread
->ptid
;
8229 /* Called when it is decided that STOP_REPLY holds the info of the
8230 event that is to be returned to the core. This function always
8231 destroys STOP_REPLY. */
8234 remote_target::process_stop_reply (struct stop_reply
*stop_reply
,
8235 struct target_waitstatus
*status
)
8237 *status
= stop_reply
->ws
;
8238 ptid_t ptid
= stop_reply
->ptid
;
8240 /* If no thread/process was reported by the stub then select a suitable
8242 if (ptid
== null_ptid
)
8243 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8244 gdb_assert (ptid
!= null_ptid
);
8246 if (status
->kind () != TARGET_WAITKIND_EXITED
8247 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8248 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8250 /* Expedited registers. */
8251 if (!stop_reply
->regcache
.empty ())
8253 struct regcache
*regcache
8254 = get_thread_arch_regcache (this, ptid
, stop_reply
->arch
);
8256 for (cached_reg_t
®
: stop_reply
->regcache
)
8258 regcache
->raw_supply (reg
.num
, reg
.data
);
8262 stop_reply
->regcache
.clear ();
8265 remote_notice_new_inferior (ptid
, false);
8266 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8267 remote_thr
->core
= stop_reply
->core
;
8268 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8269 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8271 if (target_is_non_stop_p ())
8273 /* If the target works in non-stop mode, a stop-reply indicates that
8274 only this thread stopped. */
8275 remote_thr
->set_not_resumed ();
8279 /* If the target works in all-stop mode, a stop-reply indicates that
8280 all the target's threads stopped. */
8281 for (thread_info
*tp
: all_non_exited_threads (this))
8282 get_remote_thread_info (tp
)->set_not_resumed ();
8290 /* The non-stop mode version of target_wait. */
8293 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8294 target_wait_flags options
)
8296 struct remote_state
*rs
= get_remote_state ();
8297 struct stop_reply
*stop_reply
;
8301 /* If in non-stop mode, get out of getpkt even if a
8302 notification is received. */
8304 ret
= getpkt_or_notif_sane (&rs
->buf
, 0 /* forever */, &is_notif
);
8307 if (ret
!= -1 && !is_notif
)
8310 case 'E': /* Error of some sort. */
8311 /* We're out of sync with the target now. Did it continue
8312 or not? We can't tell which thread it was in non-stop,
8313 so just ignore this. */
8314 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8316 case 'O': /* Console output. */
8317 remote_console_output (&rs
->buf
[1]);
8320 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8324 /* Acknowledge a pending stop reply that may have arrived in the
8326 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8327 remote_notif_get_pending_events (¬if_client_stop
);
8329 /* If indeed we noticed a stop reply, we're done. */
8330 stop_reply
= queued_stop_reply (ptid
);
8331 if (stop_reply
!= NULL
)
8332 return process_stop_reply (stop_reply
, status
);
8334 /* Still no event. If we're just polling for an event, then
8335 return to the event loop. */
8336 if (options
& TARGET_WNOHANG
)
8338 status
->set_ignore ();
8339 return minus_one_ptid
;
8342 /* Otherwise do a blocking wait. */
8343 ret
= getpkt_or_notif_sane (&rs
->buf
, 1 /* forever */, &is_notif
);
8347 /* Return the first resumed thread. */
8350 first_remote_resumed_thread (remote_target
*target
)
8352 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8358 /* Wait until the remote machine stops, then return, storing status in
8359 STATUS just as `wait' would. */
8362 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8363 target_wait_flags options
)
8365 struct remote_state
*rs
= get_remote_state ();
8366 ptid_t event_ptid
= null_ptid
;
8368 struct stop_reply
*stop_reply
;
8372 status
->set_ignore ();
8374 stop_reply
= queued_stop_reply (ptid
);
8375 if (stop_reply
!= NULL
)
8377 /* None of the paths that push a stop reply onto the queue should
8378 have set the waiting_for_stop_reply flag. */
8379 gdb_assert (!rs
->waiting_for_stop_reply
);
8380 event_ptid
= process_stop_reply (stop_reply
, status
);
8384 int forever
= ((options
& TARGET_WNOHANG
) == 0
8385 && rs
->wait_forever_enabled_p
);
8387 if (!rs
->waiting_for_stop_reply
)
8389 status
->set_no_resumed ();
8390 return minus_one_ptid
;
8393 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8394 _never_ wait for ever -> test on target_is_async_p().
8395 However, before we do that we need to ensure that the caller
8396 knows how to take the target into/out of async mode. */
8398 int ret
= getpkt_or_notif_sane (&rs
->buf
, forever
, &is_notif
);
8400 /* GDB gets a notification. Return to core as this event is
8402 if (ret
!= -1 && is_notif
)
8403 return minus_one_ptid
;
8405 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8406 return minus_one_ptid
;
8408 buf
= rs
->buf
.data ();
8410 /* Assume that the target has acknowledged Ctrl-C unless we receive
8411 an 'F' or 'O' packet. */
8412 if (buf
[0] != 'F' && buf
[0] != 'O')
8413 rs
->ctrlc_pending_p
= 0;
8417 case 'E': /* Error of some sort. */
8418 /* We're out of sync with the target now. Did it continue or
8419 not? Not is more likely, so report a stop. */
8420 rs
->waiting_for_stop_reply
= 0;
8422 warning (_("Remote failure reply: %s"), buf
);
8423 status
->set_stopped (GDB_SIGNAL_0
);
8425 case 'F': /* File-I/O request. */
8426 /* GDB may access the inferior memory while handling the File-I/O
8427 request, but we don't want GDB accessing memory while waiting
8428 for a stop reply. See the comments in putpkt_binary. Set
8429 waiting_for_stop_reply to 0 temporarily. */
8430 rs
->waiting_for_stop_reply
= 0;
8431 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8432 rs
->ctrlc_pending_p
= 0;
8433 /* GDB handled the File-I/O request, and the target is running
8434 again. Keep waiting for events. */
8435 rs
->waiting_for_stop_reply
= 1;
8437 case 'N': case 'T': case 'S': case 'X': case 'W':
8439 /* There is a stop reply to handle. */
8440 rs
->waiting_for_stop_reply
= 0;
8443 = (struct stop_reply
*) remote_notif_parse (this,
8447 event_ptid
= process_stop_reply (stop_reply
, status
);
8450 case 'O': /* Console output. */
8451 remote_console_output (buf
+ 1);
8454 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8456 /* Zero length reply means that we tried 'S' or 'C' and the
8457 remote system doesn't support it. */
8458 target_terminal::ours_for_output ();
8460 ("Can't send signals to this remote system. %s not sent.\n",
8461 gdb_signal_to_name (rs
->last_sent_signal
));
8462 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8463 target_terminal::inferior ();
8465 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8471 warning (_("Invalid remote reply: %s"), buf
);
8476 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8477 return minus_one_ptid
;
8478 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8480 /* Nothing interesting happened. If we're doing a non-blocking
8481 poll, we're done. Otherwise, go back to waiting. */
8482 if (options
& TARGET_WNOHANG
)
8483 return minus_one_ptid
;
8487 else if (status
->kind () != TARGET_WAITKIND_EXITED
8488 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8490 if (event_ptid
!= null_ptid
)
8491 record_currthread (rs
, event_ptid
);
8493 event_ptid
= first_remote_resumed_thread (this);
8497 /* A process exit. Invalidate our notion of current thread. */
8498 record_currthread (rs
, minus_one_ptid
);
8499 /* It's possible that the packet did not include a pid. */
8500 if (event_ptid
== null_ptid
)
8501 event_ptid
= first_remote_resumed_thread (this);
8502 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8503 if (event_ptid
== null_ptid
)
8504 event_ptid
= magic_null_ptid
;
8510 /* Wait until the remote machine stops, then return, storing status in
8511 STATUS just as `wait' would. */
8514 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8515 target_wait_flags options
)
8517 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8519 remote_state
*rs
= get_remote_state ();
8521 /* Start by clearing the flag that asks for our wait method to be called,
8522 we'll mark it again at the end if needed. If the target is not in
8523 async mode then the async token should not be marked. */
8524 if (target_is_async_p ())
8525 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
8527 gdb_assert (!async_event_handler_marked
8528 (rs
->remote_async_inferior_event_token
));
8532 if (target_is_non_stop_p ())
8533 event_ptid
= wait_ns (ptid
, status
, options
);
8535 event_ptid
= wait_as (ptid
, status
, options
);
8537 if (target_is_async_p ())
8539 /* If there are events left in the queue, or unacknowledged
8540 notifications, then tell the event loop to call us again. */
8541 if (!rs
->stop_reply_queue
.empty ()
8542 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8543 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
8549 /* Fetch a single register using a 'p' packet. */
8552 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8555 struct gdbarch
*gdbarch
= regcache
->arch ();
8556 struct remote_state
*rs
= get_remote_state ();
8558 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8561 if (m_features
.packet_support (PACKET_p
) == PACKET_DISABLE
)
8564 if (reg
->pnum
== -1)
8567 p
= rs
->buf
.data ();
8569 p
+= hexnumstr (p
, reg
->pnum
);
8572 getpkt (&rs
->buf
, 0);
8574 buf
= rs
->buf
.data ();
8576 switch (m_features
.packet_ok (rs
->buf
, PACKET_p
))
8580 case PACKET_UNKNOWN
:
8583 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8584 gdbarch_register_name (regcache
->arch (),
8589 /* If this register is unfetchable, tell the regcache. */
8592 regcache
->raw_supply (reg
->regnum
, NULL
);
8596 /* Otherwise, parse and supply the value. */
8602 error (_("fetch_register_using_p: early buf termination"));
8604 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8607 regcache
->raw_supply (reg
->regnum
, regp
);
8611 /* Fetch the registers included in the target's 'g' packet. */
8614 remote_target::send_g_packet ()
8616 struct remote_state
*rs
= get_remote_state ();
8619 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8621 getpkt (&rs
->buf
, 0);
8622 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8623 error (_("Could not read registers; remote failure reply '%s'"),
8626 /* We can get out of synch in various cases. If the first character
8627 in the buffer is not a hex character, assume that has happened
8628 and try to fetch another packet to read. */
8629 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8630 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8631 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8632 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8634 remote_debug_printf ("Bad register packet; fetching a new packet");
8635 getpkt (&rs
->buf
, 0);
8638 buf_len
= strlen (rs
->buf
.data ());
8640 /* Sanity check the received packet. */
8641 if (buf_len
% 2 != 0)
8642 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8648 remote_target::process_g_packet (struct regcache
*regcache
)
8650 struct gdbarch
*gdbarch
= regcache
->arch ();
8651 struct remote_state
*rs
= get_remote_state ();
8652 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8657 buf_len
= strlen (rs
->buf
.data ());
8659 /* Further sanity checks, with knowledge of the architecture. */
8660 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8661 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8663 rsa
->sizeof_g_packet
, buf_len
/ 2,
8666 /* Save the size of the packet sent to us by the target. It is used
8667 as a heuristic when determining the max size of packets that the
8668 target can safely receive. */
8669 if (rsa
->actual_register_packet_size
== 0)
8670 rsa
->actual_register_packet_size
= buf_len
;
8672 /* If this is smaller than we guessed the 'g' packet would be,
8673 update our records. A 'g' reply that doesn't include a register's
8674 value implies either that the register is not available, or that
8675 the 'p' packet must be used. */
8676 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8678 long sizeof_g_packet
= buf_len
/ 2;
8680 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8682 long offset
= rsa
->regs
[i
].offset
;
8683 long reg_size
= register_size (gdbarch
, i
);
8685 if (rsa
->regs
[i
].pnum
== -1)
8688 if (offset
>= sizeof_g_packet
)
8689 rsa
->regs
[i
].in_g_packet
= 0;
8690 else if (offset
+ reg_size
> sizeof_g_packet
)
8691 error (_("Truncated register %d in remote 'g' packet"), i
);
8693 rsa
->regs
[i
].in_g_packet
= 1;
8696 /* Looks valid enough, we can assume this is the correct length
8697 for a 'g' packet. It's important not to adjust
8698 rsa->sizeof_g_packet if we have truncated registers otherwise
8699 this "if" won't be run the next time the method is called
8700 with a packet of the same size and one of the internal errors
8701 below will trigger instead. */
8702 rsa
->sizeof_g_packet
= sizeof_g_packet
;
8705 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
8707 /* Unimplemented registers read as all bits zero. */
8708 memset (regs
, 0, rsa
->sizeof_g_packet
);
8710 /* Reply describes registers byte by byte, each byte encoded as two
8711 hex characters. Suck them all up, then supply them to the
8712 register cacheing/storage mechanism. */
8714 p
= rs
->buf
.data ();
8715 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
8717 if (p
[0] == 0 || p
[1] == 0)
8718 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
8719 internal_error (_("unexpected end of 'g' packet reply"));
8721 if (p
[0] == 'x' && p
[1] == 'x')
8722 regs
[i
] = 0; /* 'x' */
8724 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8728 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8730 struct packet_reg
*r
= &rsa
->regs
[i
];
8731 long reg_size
= register_size (gdbarch
, i
);
8735 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
8736 /* This shouldn't happen - we adjusted in_g_packet above. */
8737 internal_error (_("unexpected end of 'g' packet reply"));
8738 else if (rs
->buf
[r
->offset
* 2] == 'x')
8740 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
8741 /* The register isn't available, mark it as such (at
8742 the same time setting the value to zero). */
8743 regcache
->raw_supply (r
->regnum
, NULL
);
8746 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
8752 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
8755 process_g_packet (regcache
);
8758 /* Make the remote selected traceframe match GDB's selected
8762 remote_target::set_remote_traceframe ()
8765 struct remote_state
*rs
= get_remote_state ();
8767 if (rs
->remote_traceframe_number
== get_traceframe_number ())
8770 /* Avoid recursion, remote_trace_find calls us again. */
8771 rs
->remote_traceframe_number
= get_traceframe_number ();
8773 newnum
= target_trace_find (tfind_number
,
8774 get_traceframe_number (), 0, 0, NULL
);
8776 /* Should not happen. If it does, all bets are off. */
8777 if (newnum
!= get_traceframe_number ())
8778 warning (_("could not set remote traceframe"));
8782 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
8784 struct gdbarch
*gdbarch
= regcache
->arch ();
8785 struct remote_state
*rs
= get_remote_state ();
8786 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8789 set_remote_traceframe ();
8790 set_general_thread (regcache
->ptid ());
8794 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8796 gdb_assert (reg
!= NULL
);
8798 /* If this register might be in the 'g' packet, try that first -
8799 we are likely to read more than one register. If this is the
8800 first 'g' packet, we might be overly optimistic about its
8801 contents, so fall back to 'p'. */
8802 if (reg
->in_g_packet
)
8804 fetch_registers_using_g (regcache
);
8805 if (reg
->in_g_packet
)
8809 if (fetch_register_using_p (regcache
, reg
))
8812 /* This register is not available. */
8813 regcache
->raw_supply (reg
->regnum
, NULL
);
8818 fetch_registers_using_g (regcache
);
8820 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8821 if (!rsa
->regs
[i
].in_g_packet
)
8822 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
8824 /* This register is not available. */
8825 regcache
->raw_supply (i
, NULL
);
8829 /* Prepare to store registers. Since we may send them all (using a
8830 'G' request), we have to read out the ones we don't want to change
8834 remote_target::prepare_to_store (struct regcache
*regcache
)
8836 struct remote_state
*rs
= get_remote_state ();
8837 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8840 /* Make sure the entire registers array is valid. */
8841 switch (m_features
.packet_support (PACKET_P
))
8843 case PACKET_DISABLE
:
8844 case PACKET_SUPPORT_UNKNOWN
:
8845 /* Make sure all the necessary registers are cached. */
8846 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8847 if (rsa
->regs
[i
].in_g_packet
)
8848 regcache
->raw_update (rsa
->regs
[i
].regnum
);
8855 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
8856 packet was not recognized. */
8859 remote_target::store_register_using_P (const struct regcache
*regcache
,
8862 struct gdbarch
*gdbarch
= regcache
->arch ();
8863 struct remote_state
*rs
= get_remote_state ();
8864 /* Try storing a single register. */
8865 char *buf
= rs
->buf
.data ();
8866 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8869 if (m_features
.packet_support (PACKET_P
) == PACKET_DISABLE
)
8872 if (reg
->pnum
== -1)
8875 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
8876 p
= buf
+ strlen (buf
);
8877 regcache
->raw_collect (reg
->regnum
, regp
);
8878 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
8880 getpkt (&rs
->buf
, 0);
8882 switch (m_features
.packet_ok (rs
->buf
, PACKET_P
))
8887 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8888 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
.data ());
8889 case PACKET_UNKNOWN
:
8892 internal_error (_("Bad result from packet_ok"));
8896 /* Store register REGNUM, or all registers if REGNUM == -1, from the
8897 contents of the register cache buffer. FIXME: ignores errors. */
8900 remote_target::store_registers_using_G (const struct regcache
*regcache
)
8902 struct remote_state
*rs
= get_remote_state ();
8903 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8907 /* Extract all the registers in the regcache copying them into a
8912 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
8913 memset (regs
, 0, rsa
->sizeof_g_packet
);
8914 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8916 struct packet_reg
*r
= &rsa
->regs
[i
];
8919 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
8923 /* Command describes registers byte by byte,
8924 each byte encoded as two hex characters. */
8925 p
= rs
->buf
.data ();
8927 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
8929 getpkt (&rs
->buf
, 0);
8930 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8931 error (_("Could not write registers; remote failure reply '%s'"),
8935 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8936 of the register cache buffer. FIXME: ignores errors. */
8939 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
8941 struct gdbarch
*gdbarch
= regcache
->arch ();
8942 struct remote_state
*rs
= get_remote_state ();
8943 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8946 set_remote_traceframe ();
8947 set_general_thread (regcache
->ptid ());
8951 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8953 gdb_assert (reg
!= NULL
);
8955 /* Always prefer to store registers using the 'P' packet if
8956 possible; we often change only a small number of registers.
8957 Sometimes we change a larger number; we'd need help from a
8958 higher layer to know to use 'G'. */
8959 if (store_register_using_P (regcache
, reg
))
8962 /* For now, don't complain if we have no way to write the
8963 register. GDB loses track of unavailable registers too
8964 easily. Some day, this may be an error. We don't have
8965 any way to read the register, either... */
8966 if (!reg
->in_g_packet
)
8969 store_registers_using_G (regcache
);
8973 store_registers_using_G (regcache
);
8975 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8976 if (!rsa
->regs
[i
].in_g_packet
)
8977 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
8978 /* See above for why we do not issue an error here. */
8983 /* Return the number of hex digits in num. */
8986 hexnumlen (ULONGEST num
)
8990 for (i
= 0; num
!= 0; i
++)
8993 return std::max (i
, 1);
8996 /* Set BUF to the minimum number of hex digits representing NUM. */
8999 hexnumstr (char *buf
, ULONGEST num
)
9001 int len
= hexnumlen (num
);
9003 return hexnumnstr (buf
, num
, len
);
9007 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
9010 hexnumnstr (char *buf
, ULONGEST num
, int width
)
9016 for (i
= width
- 1; i
>= 0; i
--)
9018 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
9025 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
9028 remote_address_masked (CORE_ADDR addr
)
9030 unsigned int address_size
= remote_address_size
;
9032 /* If "remoteaddresssize" was not set, default to target address size. */
9034 address_size
= gdbarch_addr_bit (target_gdbarch ());
9036 if (address_size
> 0
9037 && address_size
< (sizeof (ULONGEST
) * 8))
9039 /* Only create a mask when that mask can safely be constructed
9040 in a ULONGEST variable. */
9043 mask
= (mask
<< address_size
) - 1;
9049 /* Determine whether the remote target supports binary downloading.
9050 This is accomplished by sending a no-op memory write of zero length
9051 to the target at the specified address. It does not suffice to send
9052 the whole packet, since many stubs strip the eighth bit and
9053 subsequently compute a wrong checksum, which causes real havoc with
9056 NOTE: This can still lose if the serial line is not eight-bit
9057 clean. In cases like this, the user should clear "remote
9061 remote_target::check_binary_download (CORE_ADDR addr
)
9063 struct remote_state
*rs
= get_remote_state ();
9065 switch (m_features
.packet_support (PACKET_X
))
9067 case PACKET_DISABLE
:
9071 case PACKET_SUPPORT_UNKNOWN
:
9075 p
= rs
->buf
.data ();
9077 p
+= hexnumstr (p
, (ULONGEST
) addr
);
9079 p
+= hexnumstr (p
, (ULONGEST
) 0);
9083 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9084 getpkt (&rs
->buf
, 0);
9086 if (rs
->buf
[0] == '\0')
9088 remote_debug_printf ("binary downloading NOT supported by target");
9089 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
9093 remote_debug_printf ("binary downloading supported by target");
9094 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
9101 /* Helper function to resize the payload in order to try to get a good
9102 alignment. We try to write an amount of data such that the next write will
9103 start on an address aligned on REMOTE_ALIGN_WRITES. */
9106 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
9108 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
9111 /* Write memory data directly to the remote machine.
9112 This does not inform the data cache; the data cache uses this.
9113 HEADER is the starting part of the packet.
9114 MEMADDR is the address in the remote memory space.
9115 MYADDR is the address of the buffer in our space.
9116 LEN_UNITS is the number of addressable units to write.
9117 UNIT_SIZE is the length in bytes of an addressable unit.
9118 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
9119 should send data as binary ('X'), or hex-encoded ('M').
9121 The function creates packet of the form
9122 <HEADER><ADDRESS>,<LENGTH>:<DATA>
9124 where encoding of <DATA> is terminated by PACKET_FORMAT.
9126 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
9129 Return the transferred status, error or OK (an
9130 'enum target_xfer_status' value). Save the number of addressable units
9131 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
9133 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
9134 exchange between gdb and the stub could look like (?? in place of the
9140 -> $M1000,3:eeeeffffeeee#??
9144 <- eeeeffffeeeedddd */
9147 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
9148 const gdb_byte
*myaddr
,
9151 ULONGEST
*xfered_len_units
,
9152 char packet_format
, int use_length
)
9154 struct remote_state
*rs
= get_remote_state ();
9160 int payload_capacity_bytes
;
9161 int payload_length_bytes
;
9163 if (packet_format
!= 'X' && packet_format
!= 'M')
9164 internal_error (_("remote_write_bytes_aux: bad packet format"));
9167 return TARGET_XFER_EOF
;
9169 payload_capacity_bytes
= get_memory_write_packet_size ();
9171 /* The packet buffer will be large enough for the payload;
9172 get_memory_packet_size ensures this. */
9175 /* Compute the size of the actual payload by subtracting out the
9176 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9178 payload_capacity_bytes
-= strlen ("$,:#NN");
9180 /* The comma won't be used. */
9181 payload_capacity_bytes
+= 1;
9182 payload_capacity_bytes
-= strlen (header
);
9183 payload_capacity_bytes
-= hexnumlen (memaddr
);
9185 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9187 strcat (rs
->buf
.data (), header
);
9188 p
= rs
->buf
.data () + strlen (header
);
9190 /* Compute a best guess of the number of bytes actually transfered. */
9191 if (packet_format
== 'X')
9193 /* Best guess at number of bytes that will fit. */
9194 todo_units
= std::min (len_units
,
9195 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9197 payload_capacity_bytes
-= hexnumlen (todo_units
);
9198 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9202 /* Number of bytes that will fit. */
9204 = std::min (len_units
,
9205 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9207 payload_capacity_bytes
-= hexnumlen (todo_units
);
9208 todo_units
= std::min (todo_units
,
9209 (payload_capacity_bytes
/ unit_size
) / 2);
9212 if (todo_units
<= 0)
9213 internal_error (_("minimum packet size too small to write data"));
9215 /* If we already need another packet, then try to align the end
9216 of this packet to a useful boundary. */
9217 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9218 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9220 /* Append "<memaddr>". */
9221 memaddr
= remote_address_masked (memaddr
);
9222 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9229 /* Append the length and retain its location and size. It may need to be
9230 adjusted once the packet body has been created. */
9232 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9240 /* Append the packet body. */
9241 if (packet_format
== 'X')
9243 /* Binary mode. Send target system values byte by byte, in
9244 increasing byte addresses. Only escape certain critical
9246 payload_length_bytes
=
9247 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9248 &units_written
, payload_capacity_bytes
);
9250 /* If not all TODO units fit, then we'll need another packet. Make
9251 a second try to keep the end of the packet aligned. Don't do
9252 this if the packet is tiny. */
9253 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9257 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9259 if (new_todo_units
!= units_written
)
9260 payload_length_bytes
=
9261 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9262 (gdb_byte
*) p
, &units_written
,
9263 payload_capacity_bytes
);
9266 p
+= payload_length_bytes
;
9267 if (use_length
&& units_written
< todo_units
)
9269 /* Escape chars have filled up the buffer prematurely,
9270 and we have actually sent fewer units than planned.
9271 Fix-up the length field of the packet. Use the same
9272 number of characters as before. */
9273 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9275 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9280 /* Normal mode: Send target system values byte by byte, in
9281 increasing byte addresses. Each byte is encoded as a two hex
9283 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9284 units_written
= todo_units
;
9287 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9288 getpkt (&rs
->buf
, 0);
9290 if (rs
->buf
[0] == 'E')
9291 return TARGET_XFER_E_IO
;
9293 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9294 send fewer units than we'd planned. */
9295 *xfered_len_units
= (ULONGEST
) units_written
;
9296 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9299 /* Write memory data directly to the remote machine.
9300 This does not inform the data cache; the data cache uses this.
9301 MEMADDR is the address in the remote memory space.
9302 MYADDR is the address of the buffer in our space.
9303 LEN is the number of bytes.
9305 Return the transferred status, error or OK (an
9306 'enum target_xfer_status' value). Save the number of bytes
9307 transferred in *XFERED_LEN. Only transfer a single packet. */
9310 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9311 ULONGEST len
, int unit_size
,
9312 ULONGEST
*xfered_len
)
9314 const char *packet_format
= NULL
;
9316 /* Check whether the target supports binary download. */
9317 check_binary_download (memaddr
);
9319 switch (m_features
.packet_support (PACKET_X
))
9322 packet_format
= "X";
9324 case PACKET_DISABLE
:
9325 packet_format
= "M";
9327 case PACKET_SUPPORT_UNKNOWN
:
9328 internal_error (_("remote_write_bytes: bad internal state"));
9330 internal_error (_("bad switch"));
9333 return remote_write_bytes_aux (packet_format
,
9334 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9335 packet_format
[0], 1);
9338 /* Read memory data directly from the remote machine.
9339 This does not use the data cache; the data cache uses this.
9340 MEMADDR is the address in the remote memory space.
9341 MYADDR is the address of the buffer in our space.
9342 LEN_UNITS is the number of addressable memory units to read..
9343 UNIT_SIZE is the length in bytes of an addressable unit.
9345 Return the transferred status, error or OK (an
9346 'enum target_xfer_status' value). Save the number of bytes
9347 transferred in *XFERED_LEN_UNITS.
9349 See the comment of remote_write_bytes_aux for an example of
9350 memory read/write exchange between gdb and the stub. */
9353 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9355 int unit_size
, ULONGEST
*xfered_len_units
)
9357 struct remote_state
*rs
= get_remote_state ();
9358 int buf_size_bytes
; /* Max size of packet output buffer. */
9363 buf_size_bytes
= get_memory_read_packet_size ();
9364 /* The packet buffer will be large enough for the payload;
9365 get_memory_packet_size ensures this. */
9367 /* Number of units that will fit. */
9368 todo_units
= std::min (len_units
,
9369 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9371 /* Construct "m"<memaddr>","<len>". */
9372 memaddr
= remote_address_masked (memaddr
);
9373 p
= rs
->buf
.data ();
9375 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9377 p
+= hexnumstr (p
, (ULONGEST
) todo_units
);
9380 getpkt (&rs
->buf
, 0);
9381 if (rs
->buf
[0] == 'E'
9382 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
9383 && rs
->buf
[3] == '\0')
9384 return TARGET_XFER_E_IO
;
9385 /* Reply describes memory byte by byte, each byte encoded as two hex
9387 p
= rs
->buf
.data ();
9388 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9389 /* Return what we have. Let higher layers handle partial reads. */
9390 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9391 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9394 /* Using the set of read-only target sections of remote, read live
9397 For interface/parameters/return description see target.h,
9401 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9405 ULONGEST
*xfered_len
)
9407 const struct target_section
*secp
;
9409 secp
= target_section_by_addr (this, memaddr
);
9411 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9413 ULONGEST memend
= memaddr
+ len
;
9415 const target_section_table
*table
= target_get_section_table (this);
9416 for (const target_section
&p
: *table
)
9418 if (memaddr
>= p
.addr
)
9420 if (memend
<= p
.endaddr
)
9422 /* Entire transfer is within this section. */
9423 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9426 else if (memaddr
>= p
.endaddr
)
9428 /* This section ends before the transfer starts. */
9433 /* This section overlaps the transfer. Just do half. */
9434 len
= p
.endaddr
- memaddr
;
9435 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9442 return TARGET_XFER_EOF
;
9445 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9446 first if the requested memory is unavailable in traceframe.
9447 Otherwise, fall back to remote_read_bytes_1. */
9450 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9451 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9452 ULONGEST
*xfered_len
)
9455 return TARGET_XFER_EOF
;
9457 if (get_traceframe_number () != -1)
9459 std::vector
<mem_range
> available
;
9461 /* If we fail to get the set of available memory, then the
9462 target does not support querying traceframe info, and so we
9463 attempt reading from the traceframe anyway (assuming the
9464 target implements the old QTro packet then). */
9465 if (traceframe_available_memory (&available
, memaddr
, len
))
9467 if (available
.empty () || available
[0].start
!= memaddr
)
9469 enum target_xfer_status res
;
9471 /* Don't read into the traceframe's available
9473 if (!available
.empty ())
9475 LONGEST oldlen
= len
;
9477 len
= available
[0].start
- memaddr
;
9478 gdb_assert (len
<= oldlen
);
9481 /* This goes through the topmost target again. */
9482 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9483 len
, unit_size
, xfered_len
);
9484 if (res
== TARGET_XFER_OK
)
9485 return TARGET_XFER_OK
;
9488 /* No use trying further, we know some memory starting
9489 at MEMADDR isn't available. */
9491 return (*xfered_len
!= 0) ?
9492 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9496 /* Don't try to read more than how much is available, in
9497 case the target implements the deprecated QTro packet to
9498 cater for older GDBs (the target's knowledge of read-only
9499 sections may be outdated by now). */
9500 len
= available
[0].length
;
9504 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9509 /* Sends a packet with content determined by the printf format string
9510 FORMAT and the remaining arguments, then gets the reply. Returns
9511 whether the packet was a success, a failure, or unknown. */
9514 remote_target::remote_send_printf (const char *format
, ...)
9516 struct remote_state
*rs
= get_remote_state ();
9517 int max_size
= get_remote_packet_size ();
9520 va_start (ap
, format
);
9523 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9527 if (size
>= max_size
)
9528 internal_error (_("Too long remote packet."));
9530 if (putpkt (rs
->buf
) < 0)
9531 error (_("Communication problem with target."));
9534 getpkt (&rs
->buf
, 0);
9536 return packet_check_result (rs
->buf
);
9539 /* Flash writing can take quite some time. We'll set
9540 effectively infinite timeout for flash operations.
9541 In future, we'll need to decide on a better approach. */
9542 static const int remote_flash_timeout
= 1000;
9545 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9547 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
9548 enum packet_result ret
;
9549 scoped_restore restore_timeout
9550 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9552 ret
= remote_send_printf ("vFlashErase:%s,%s",
9553 phex (address
, addr_size
),
9557 case PACKET_UNKNOWN
:
9558 error (_("Remote target does not support flash erase"));
9560 error (_("Error erasing flash with vFlashErase packet"));
9567 remote_target::remote_flash_write (ULONGEST address
,
9568 ULONGEST length
, ULONGEST
*xfered_len
,
9569 const gdb_byte
*data
)
9571 scoped_restore restore_timeout
9572 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9573 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9578 remote_target::flash_done ()
9582 scoped_restore restore_timeout
9583 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9585 ret
= remote_send_printf ("vFlashDone");
9589 case PACKET_UNKNOWN
:
9590 error (_("Remote target does not support vFlashDone"));
9592 error (_("Error finishing flash operation"));
9599 /* Stuff for dealing with the packets which are part of this protocol.
9600 See comment at top of file for details. */
9602 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
9603 error to higher layers. Called when a serial error is detected.
9604 The exception message is STRING, followed by a colon and a blank,
9605 the system error message for errno at function entry and final dot
9606 for output compatibility with throw_perror_with_name. */
9609 unpush_and_perror (remote_target
*target
, const char *string
)
9611 int saved_errno
= errno
;
9613 remote_unpush_target (target
);
9614 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
9615 safe_strerror (saved_errno
));
9618 /* Read a single character from the remote end. The current quit
9619 handler is overridden to avoid quitting in the middle of packet
9620 sequence, as that would break communication with the remote server.
9621 See remote_serial_quit_handler for more detail. */
9624 remote_target::readchar (int timeout
)
9627 struct remote_state
*rs
= get_remote_state ();
9630 scoped_restore restore_quit_target
9631 = make_scoped_restore (&curr_quit_handler_target
, this);
9632 scoped_restore restore_quit
9633 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9635 rs
->got_ctrlc_during_io
= 0;
9637 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9639 if (rs
->got_ctrlc_during_io
)
9646 switch ((enum serial_rc
) ch
)
9649 remote_unpush_target (this);
9650 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
9653 unpush_and_perror (this, _("Remote communication error. "
9654 "Target disconnected."));
9656 case SERIAL_TIMEOUT
:
9662 /* Wrapper for serial_write that closes the target and throws if
9663 writing fails. The current quit handler is overridden to avoid
9664 quitting in the middle of packet sequence, as that would break
9665 communication with the remote server. See
9666 remote_serial_quit_handler for more detail. */
9669 remote_target::remote_serial_write (const char *str
, int len
)
9671 struct remote_state
*rs
= get_remote_state ();
9673 scoped_restore restore_quit_target
9674 = make_scoped_restore (&curr_quit_handler_target
, this);
9675 scoped_restore restore_quit
9676 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9678 rs
->got_ctrlc_during_io
= 0;
9680 if (serial_write (rs
->remote_desc
, str
, len
))
9682 unpush_and_perror (this, _("Remote communication error. "
9683 "Target disconnected."));
9686 if (rs
->got_ctrlc_during_io
)
9690 /* Return a string representing an escaped version of BUF, of len N.
9691 E.g. \n is converted to \\n, \t to \\t, etc. */
9694 escape_buffer (const char *buf
, int n
)
9698 stb
.putstrn (buf
, n
, '\\');
9699 return stb
.release ();
9703 remote_target::putpkt (const char *buf
)
9705 return putpkt_binary (buf
, strlen (buf
));
9708 /* Wrapper around remote_target::putpkt to avoid exporting
9712 putpkt (remote_target
*remote
, const char *buf
)
9714 return remote
->putpkt (buf
);
9717 /* Send a packet to the remote machine, with error checking. The data
9718 of the packet is in BUF. The string in BUF can be at most
9719 get_remote_packet_size () - 5 to account for the $, # and checksum,
9720 and for a possible /0 if we are debugging (remote_debug) and want
9721 to print the sent packet as a string. */
9724 remote_target::putpkt_binary (const char *buf
, int cnt
)
9726 struct remote_state
*rs
= get_remote_state ();
9728 unsigned char csum
= 0;
9729 gdb::def_vector
<char> data (cnt
+ 6);
9730 char *buf2
= data
.data ();
9736 /* Catch cases like trying to read memory or listing threads while
9737 we're waiting for a stop reply. The remote server wouldn't be
9738 ready to handle this request, so we'd hang and timeout. We don't
9739 have to worry about this in synchronous mode, because in that
9740 case it's not possible to issue a command while the target is
9741 running. This is not a problem in non-stop mode, because in that
9742 case, the stub is always ready to process serial input. */
9743 if (!target_is_non_stop_p ()
9744 && target_is_async_p ()
9745 && rs
->waiting_for_stop_reply
)
9747 error (_("Cannot execute this command while the target is running.\n"
9748 "Use the \"interrupt\" command to stop the target\n"
9749 "and then try again."));
9752 /* Copy the packet into buffer BUF2, encapsulating it
9753 and giving it a checksum. */
9758 for (i
= 0; i
< cnt
; i
++)
9764 *p
++ = tohex ((csum
>> 4) & 0xf);
9765 *p
++ = tohex (csum
& 0xf);
9767 /* Send it over and over until we get a positive ack. */
9775 int len
= (int) (p
- buf2
);
9778 if (remote_packet_max_chars
< 0)
9781 max_chars
= remote_packet_max_chars
;
9784 = escape_buffer (buf2
, std::min (len
, max_chars
));
9786 if (len
> max_chars
)
9787 remote_debug_printf_nofunc
9788 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
9791 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
9793 remote_serial_write (buf2
, p
- buf2
);
9795 /* If this is a no acks version of the remote protocol, send the
9796 packet and move on. */
9800 /* Read until either a timeout occurs (-2) or '+' is read.
9801 Handle any notification that arrives in the mean time. */
9804 ch
= readchar (remote_timeout
);
9809 remote_debug_printf_nofunc ("Received Ack");
9812 remote_debug_printf_nofunc ("Received Nak");
9814 case SERIAL_TIMEOUT
:
9818 break; /* Retransmit buffer. */
9821 remote_debug_printf ("Packet instead of Ack, ignoring it");
9822 /* It's probably an old response sent because an ACK
9823 was lost. Gobble up the packet and ack it so it
9824 doesn't get retransmitted when we resend this
9827 remote_serial_write ("+", 1);
9828 continue; /* Now, go look for +. */
9835 /* If we got a notification, handle it, and go back to looking
9837 /* We've found the start of a notification. Now
9838 collect the data. */
9839 val
= read_frame (&rs
->buf
);
9842 remote_debug_printf_nofunc
9843 (" Notification received: %s",
9844 escape_buffer (rs
->buf
.data (), val
).c_str ());
9846 handle_notification (rs
->notif_state
, rs
->buf
.data ());
9847 /* We're in sync now, rewait for the ack. */
9851 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9857 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9861 break; /* Here to retransmit. */
9865 /* This is wrong. If doing a long backtrace, the user should be
9866 able to get out next time we call QUIT, without anything as
9867 violent as interrupt_query. If we want to provide a way out of
9868 here without getting to the next QUIT, it should be based on
9869 hitting ^C twice as in remote_wait. */
9881 /* Come here after finding the start of a frame when we expected an
9882 ack. Do our best to discard the rest of this packet. */
9885 remote_target::skip_frame ()
9891 c
= readchar (remote_timeout
);
9894 case SERIAL_TIMEOUT
:
9895 /* Nothing we can do. */
9898 /* Discard the two bytes of checksum and stop. */
9899 c
= readchar (remote_timeout
);
9901 c
= readchar (remote_timeout
);
9904 case '*': /* Run length encoding. */
9905 /* Discard the repeat count. */
9906 c
= readchar (remote_timeout
);
9911 /* A regular character. */
9917 /* Come here after finding the start of the frame. Collect the rest
9918 into *BUF, verifying the checksum, length, and handling run-length
9919 compression. NUL terminate the buffer. If there is not enough room,
9922 Returns -1 on error, number of characters in buffer (ignoring the
9923 trailing NULL) on success. (could be extended to return one of the
9924 SERIAL status indications). */
9927 remote_target::read_frame (gdb::char_vector
*buf_p
)
9932 char *buf
= buf_p
->data ();
9933 struct remote_state
*rs
= get_remote_state ();
9940 c
= readchar (remote_timeout
);
9943 case SERIAL_TIMEOUT
:
9944 remote_debug_printf ("Timeout in mid-packet, retrying");
9948 remote_debug_printf ("Saw new packet start in middle of old one");
9949 return -1; /* Start a new packet, count retries. */
9953 unsigned char pktcsum
;
9959 check_0
= readchar (remote_timeout
);
9961 check_1
= readchar (remote_timeout
);
9963 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
9965 remote_debug_printf ("Timeout in checksum, retrying");
9968 else if (check_0
< 0 || check_1
< 0)
9970 remote_debug_printf ("Communication error in checksum");
9974 /* Don't recompute the checksum; with no ack packets we
9975 don't have any way to indicate a packet retransmission
9980 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
9981 if (csum
== pktcsum
)
9985 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
9986 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
9988 /* Number of characters in buffer ignoring trailing
9992 case '*': /* Run length encoding. */
9997 c
= readchar (remote_timeout
);
9999 repeat
= c
- ' ' + 3; /* Compute repeat count. */
10001 /* The character before ``*'' is repeated. */
10003 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
10005 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
10007 /* Make some more room in the buffer. */
10008 buf_p
->resize (buf_p
->size () + repeat
);
10009 buf
= buf_p
->data ();
10012 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
10018 gdb_printf (_("Invalid run length encoding: %s\n"), buf
);
10022 if (bc
>= buf_p
->size () - 1)
10024 /* Make some more room in the buffer. */
10025 buf_p
->resize (buf_p
->size () * 2);
10026 buf
= buf_p
->data ();
10036 /* Set this to the maximum number of seconds to wait instead of waiting forever
10037 in target_wait(). If this timer times out, then it generates an error and
10038 the command is aborted. This replaces most of the need for timeouts in the
10039 GDB test suite, and makes it possible to distinguish between a hung target
10040 and one with slow communications. */
10042 static int watchdog
= 0;
10044 show_watchdog (struct ui_file
*file
, int from_tty
,
10045 struct cmd_list_element
*c
, const char *value
)
10047 gdb_printf (file
, _("Watchdog timer is %s.\n"), value
);
10050 /* Read a packet from the remote machine, with error checking, and
10051 store it in *BUF. Resize *BUF if necessary to hold the result. If
10052 FOREVER, wait forever rather than timing out; this is used (in
10053 synchronous mode) to wait for a target that is is executing user
10055 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
10056 don't have to change all the calls to getpkt to deal with the
10057 return value, because at the moment I don't know what the right
10058 thing to do it for those. */
10061 remote_target::getpkt (gdb::char_vector
*buf
, int forever
)
10063 getpkt_sane (buf
, forever
);
10067 /* Read a packet from the remote machine, with error checking, and
10068 store it in *BUF. Resize *BUF if necessary to hold the result. If
10069 FOREVER, wait forever rather than timing out; this is used (in
10070 synchronous mode) to wait for a target that is is executing user
10071 code to stop. If FOREVER == 0, this function is allowed to time
10072 out gracefully and return an indication of this to the caller.
10073 Otherwise return the number of bytes read. If EXPECTING_NOTIF,
10074 consider receiving a notification enough reason to return to the
10075 caller. *IS_NOTIF is an output boolean that indicates whether *BUF
10076 holds a notification or not (a regular packet). */
10079 remote_target::getpkt_or_notif_sane_1 (gdb::char_vector
*buf
,
10080 int forever
, int expecting_notif
,
10083 struct remote_state
*rs
= get_remote_state ();
10089 strcpy (buf
->data (), "timeout");
10092 timeout
= watchdog
> 0 ? watchdog
: -1;
10093 else if (expecting_notif
)
10094 timeout
= 0; /* There should already be a char in the buffer. If
10097 timeout
= remote_timeout
;
10099 #define MAX_TRIES 3
10101 /* Process any number of notifications, and then return when
10102 we get a packet. */
10105 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
10107 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
10109 /* This can loop forever if the remote side sends us
10110 characters continuously, but if it pauses, we'll get
10111 SERIAL_TIMEOUT from readchar because of timeout. Then
10112 we'll count that as a retry.
10114 Note that even when forever is set, we will only wait
10115 forever prior to the start of a packet. After that, we
10116 expect characters to arrive at a brisk pace. They should
10117 show up within remote_timeout intervals. */
10119 c
= readchar (timeout
);
10120 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
10122 if (c
== SERIAL_TIMEOUT
)
10124 if (expecting_notif
)
10125 return -1; /* Don't complain, it's normal to not get
10126 anything in this case. */
10128 if (forever
) /* Watchdog went off? Kill the target. */
10130 remote_unpush_target (this);
10131 throw_error (TARGET_CLOSE_ERROR
,
10132 _("Watchdog timeout has expired. "
10133 "Target detached."));
10136 remote_debug_printf ("Timed out.");
10140 /* We've found the start of a packet or notification.
10141 Now collect the data. */
10142 val
= read_frame (buf
);
10147 remote_serial_write ("-", 1);
10150 if (tries
> MAX_TRIES
)
10152 /* We have tried hard enough, and just can't receive the
10153 packet/notification. Give up. */
10154 gdb_printf (_("Ignoring packet error, continuing...\n"));
10156 /* Skip the ack char if we're in no-ack mode. */
10157 if (!rs
->noack_mode
)
10158 remote_serial_write ("+", 1);
10162 /* If we got an ordinary packet, return that to our caller. */
10169 if (remote_packet_max_chars
< 0)
10172 max_chars
= remote_packet_max_chars
;
10175 = escape_buffer (buf
->data (),
10176 std::min (val
, max_chars
));
10178 if (val
> max_chars
)
10179 remote_debug_printf_nofunc
10180 ("Packet received: %s [%d bytes omitted]", str
.c_str (),
10183 remote_debug_printf_nofunc ("Packet received: %s",
10187 /* Skip the ack char if we're in no-ack mode. */
10188 if (!rs
->noack_mode
)
10189 remote_serial_write ("+", 1);
10190 if (is_notif
!= NULL
)
10195 /* If we got a notification, handle it, and go back to looking
10199 gdb_assert (c
== '%');
10201 remote_debug_printf_nofunc
10202 (" Notification received: %s",
10203 escape_buffer (buf
->data (), val
).c_str ());
10205 if (is_notif
!= NULL
)
10208 handle_notification (rs
->notif_state
, buf
->data ());
10210 /* Notifications require no acknowledgement. */
10212 if (expecting_notif
)
10219 remote_target::getpkt_sane (gdb::char_vector
*buf
, int forever
)
10221 return getpkt_or_notif_sane_1 (buf
, forever
, 0, NULL
);
10225 remote_target::getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
10228 return getpkt_or_notif_sane_1 (buf
, forever
, 1, is_notif
);
10231 /* Kill any new fork children of inferior INF that haven't been
10232 processed by follow_fork. */
10235 remote_target::kill_new_fork_children (inferior
*inf
)
10237 remote_state
*rs
= get_remote_state ();
10238 const notif_client
*notif
= ¬if_client_stop
;
10240 /* Kill the fork child threads of any threads in inferior INF that are stopped
10241 at a fork event. */
10242 for (thread_info
*thread
: inf
->non_exited_threads ())
10244 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10249 int child_pid
= ws
->child_ptid ().pid ();
10250 int res
= remote_vkill (child_pid
);
10253 error (_("Can't kill fork child process %d"), child_pid
);
10256 /* Check for any pending fork events (not reported or processed yet)
10257 in inferior INF and kill those fork child threads as well. */
10258 remote_notif_get_pending_events (notif
);
10259 for (auto &event
: rs
->stop_reply_queue
)
10261 if (event
->ptid
.pid () != inf
->pid
)
10264 if (!is_fork_status (event
->ws
.kind ()))
10267 int child_pid
= event
->ws
.child_ptid ().pid ();
10268 int res
= remote_vkill (child_pid
);
10271 error (_("Can't kill fork child process %d"), child_pid
);
10276 /* Target hook to kill the current inferior. */
10279 remote_target::kill ()
10282 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10284 gdb_assert (inf
!= nullptr);
10286 if (m_features
.packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10288 /* If we're stopped while forking and we haven't followed yet,
10289 kill the child task. We need to do this before killing the
10290 parent task because if this is a vfork then the parent will
10292 kill_new_fork_children (inf
);
10294 res
= remote_vkill (inf
->pid
);
10297 target_mourn_inferior (inferior_ptid
);
10302 /* If we are in 'target remote' mode and we are killing the only
10303 inferior, then we will tell gdbserver to exit and unpush the
10305 if (res
== -1 && !m_features
.remote_multi_process_p ()
10306 && number_of_live_inferiors (this) == 1)
10310 /* We've killed the remote end, we get to mourn it. If we are
10311 not in extended mode, mourning the inferior also unpushes
10312 remote_ops from the target stack, which closes the remote
10314 target_mourn_inferior (inferior_ptid
);
10319 error (_("Can't kill process"));
10322 /* Send a kill request to the target using the 'vKill' packet. */
10325 remote_target::remote_vkill (int pid
)
10327 if (m_features
.packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10330 remote_state
*rs
= get_remote_state ();
10332 /* Tell the remote target to detach. */
10333 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10335 getpkt (&rs
->buf
, 0);
10337 switch (m_features
.packet_ok (rs
->buf
, PACKET_vKill
))
10343 case PACKET_UNKNOWN
:
10346 internal_error (_("Bad result from packet_ok"));
10350 /* Send a kill request to the target using the 'k' packet. */
10353 remote_target::remote_kill_k ()
10355 /* Catch errors so the user can quit from gdb even when we
10356 aren't on speaking terms with the remote system. */
10361 catch (const gdb_exception_error
&ex
)
10363 if (ex
.error
== TARGET_CLOSE_ERROR
)
10365 /* If we got an (EOF) error that caused the target
10366 to go away, then we're done, that's what we wanted.
10367 "k" is susceptible to cause a premature EOF, given
10368 that the remote server isn't actually required to
10369 reply to "k", and it can happen that it doesn't
10370 even get to reply ACK to the "k". */
10374 /* Otherwise, something went wrong. We didn't actually kill
10375 the target. Just propagate the exception, and let the
10376 user or higher layers decide what to do. */
10382 remote_target::mourn_inferior ()
10384 struct remote_state
*rs
= get_remote_state ();
10386 /* We're no longer interested in notification events of an inferior
10387 that exited or was killed/detached. */
10388 discard_pending_stop_replies (current_inferior ());
10390 /* In 'target remote' mode with one inferior, we close the connection. */
10391 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10393 remote_unpush_target (this);
10397 /* In case we got here due to an error, but we're going to stay
10399 rs
->waiting_for_stop_reply
= 0;
10401 /* If the current general thread belonged to the process we just
10402 detached from or has exited, the remote side current general
10403 thread becomes undefined. Considering a case like this:
10405 - We just got here due to a detach.
10406 - The process that we're detaching from happens to immediately
10407 report a global breakpoint being hit in non-stop mode, in the
10408 same thread we had selected before.
10409 - GDB attaches to this process again.
10410 - This event happens to be the next event we handle.
10412 GDB would consider that the current general thread didn't need to
10413 be set on the stub side (with Hg), since for all it knew,
10414 GENERAL_THREAD hadn't changed.
10416 Notice that although in all-stop mode, the remote server always
10417 sets the current thread to the thread reporting the stop event,
10418 that doesn't happen in non-stop mode; in non-stop, the stub *must
10419 not* change the current thread when reporting a breakpoint hit,
10420 due to the decoupling of event reporting and event handling.
10422 To keep things simple, we always invalidate our notion of the
10424 record_currthread (rs
, minus_one_ptid
);
10426 /* Call common code to mark the inferior as not running. */
10427 generic_mourn_inferior ();
10431 extended_remote_target::supports_disable_randomization ()
10433 return (m_features
.packet_support (PACKET_QDisableRandomization
)
10438 remote_target::extended_remote_disable_randomization (int val
)
10440 struct remote_state
*rs
= get_remote_state ();
10443 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10444 "QDisableRandomization:%x", val
);
10446 reply
= remote_get_noisy_reply ();
10447 if (*reply
== '\0')
10448 error (_("Target does not support QDisableRandomization."));
10449 if (strcmp (reply
, "OK") != 0)
10450 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10454 remote_target::extended_remote_run (const std::string
&args
)
10456 struct remote_state
*rs
= get_remote_state ();
10458 const char *remote_exec_file
= get_remote_exec_file ();
10460 /* If the user has disabled vRun support, or we have detected that
10461 support is not available, do not try it. */
10462 if (m_features
.packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10465 strcpy (rs
->buf
.data (), "vRun;");
10466 len
= strlen (rs
->buf
.data ());
10468 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10469 error (_("Remote file name too long for run packet"));
10470 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10471 strlen (remote_exec_file
));
10473 if (!args
.empty ())
10477 gdb_argv
argv (args
.c_str ());
10478 for (i
= 0; argv
[i
] != NULL
; i
++)
10480 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10481 error (_("Argument list too long for run packet"));
10482 rs
->buf
[len
++] = ';';
10483 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10488 rs
->buf
[len
++] = '\0';
10491 getpkt (&rs
->buf
, 0);
10493 switch (m_features
.packet_ok (rs
->buf
, PACKET_vRun
))
10496 /* We have a wait response. All is well. */
10498 case PACKET_UNKNOWN
:
10501 if (remote_exec_file
[0] == '\0')
10502 error (_("Running the default executable on the remote target failed; "
10503 "try \"set remote exec-file\"?"));
10505 error (_("Running \"%s\" on the remote target failed"),
10508 gdb_assert_not_reached ("bad switch");
10512 /* Helper function to send set/unset environment packets. ACTION is
10513 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10514 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10518 remote_target::send_environment_packet (const char *action
,
10519 const char *packet
,
10522 remote_state
*rs
= get_remote_state ();
10524 /* Convert the environment variable to an hex string, which
10525 is the best format to be transmitted over the wire. */
10526 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10529 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10530 "%s:%s", packet
, encoded_value
.c_str ());
10533 getpkt (&rs
->buf
, 0);
10534 if (strcmp (rs
->buf
.data (), "OK") != 0)
10535 warning (_("Unable to %s environment variable '%s' on remote."),
10539 /* Helper function to handle the QEnvironment* packets. */
10542 remote_target::extended_remote_environment_support ()
10544 remote_state
*rs
= get_remote_state ();
10546 if (m_features
.packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10548 putpkt ("QEnvironmentReset");
10549 getpkt (&rs
->buf
, 0);
10550 if (strcmp (rs
->buf
.data (), "OK") != 0)
10551 warning (_("Unable to reset environment on remote."));
10554 gdb_environ
*e
= ¤t_inferior ()->environment
;
10556 if (m_features
.packet_support (PACKET_QEnvironmentHexEncoded
)
10559 for (const std::string
&el
: e
->user_set_env ())
10560 send_environment_packet ("set", "QEnvironmentHexEncoded",
10565 if (m_features
.packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10566 for (const std::string
&el
: e
->user_unset_env ())
10567 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10570 /* Helper function to set the current working directory for the
10571 inferior in the remote target. */
10574 remote_target::extended_remote_set_inferior_cwd ()
10576 if (m_features
.packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10578 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10579 remote_state
*rs
= get_remote_state ();
10581 if (!inferior_cwd
.empty ())
10583 std::string hexpath
10584 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10585 inferior_cwd
.size ());
10587 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10588 "QSetWorkingDir:%s", hexpath
.c_str ());
10592 /* An empty inferior_cwd means that the user wants us to
10593 reset the remote server's inferior's cwd. */
10594 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10595 "QSetWorkingDir:");
10599 getpkt (&rs
->buf
, 0);
10600 if (m_features
.packet_ok (rs
->buf
, PACKET_QSetWorkingDir
) != PACKET_OK
)
10602 Remote replied unexpectedly while setting the inferior's working\n\
10609 /* In the extended protocol we want to be able to do things like
10610 "run" and have them basically work as expected. So we need
10611 a special create_inferior function. We support changing the
10612 executable file and the command line arguments, but not the
10616 extended_remote_target::create_inferior (const char *exec_file
,
10617 const std::string
&args
,
10618 char **env
, int from_tty
)
10622 struct remote_state
*rs
= get_remote_state ();
10623 const char *remote_exec_file
= get_remote_exec_file ();
10625 /* If running asynchronously, register the target file descriptor
10626 with the event loop. */
10627 if (target_can_async_p ())
10628 target_async (true);
10630 /* Disable address space randomization if requested (and supported). */
10631 if (supports_disable_randomization ())
10632 extended_remote_disable_randomization (disable_randomization
);
10634 /* If startup-with-shell is on, we inform gdbserver to start the
10635 remote inferior using a shell. */
10636 if (m_features
.packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10638 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10639 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
10641 getpkt (&rs
->buf
, 0);
10642 if (strcmp (rs
->buf
.data (), "OK") != 0)
10644 Remote replied unexpectedly while setting startup-with-shell: %s"),
10648 extended_remote_environment_support ();
10650 extended_remote_set_inferior_cwd ();
10652 /* Now restart the remote server. */
10653 run_worked
= extended_remote_run (args
) != -1;
10656 /* vRun was not supported. Fail if we need it to do what the
10658 if (remote_exec_file
[0])
10659 error (_("Remote target does not support \"set remote exec-file\""));
10660 if (!args
.empty ())
10661 error (_("Remote target does not support \"set args\" or run ARGS"));
10663 /* Fall back to "R". */
10664 extended_remote_restart ();
10667 /* vRun's success return is a stop reply. */
10668 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
10669 add_current_inferior_and_thread (stop_reply
);
10671 /* Get updated offsets, if the stub uses qOffsets. */
10676 /* Given a location's target info BP_TGT and the packet buffer BUF, output
10677 the list of conditions (in agent expression bytecode format), if any, the
10678 target needs to evaluate. The output is placed into the packet buffer
10679 started from BUF and ended at BUF_END. */
10682 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
10683 struct bp_target_info
*bp_tgt
, char *buf
,
10686 if (bp_tgt
->conditions
.empty ())
10689 buf
+= strlen (buf
);
10690 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
10693 /* Send conditions to the target. */
10694 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
10696 xsnprintf (buf
, buf_end
- buf
, "X%x,", aexpr
->len
);
10697 buf
+= strlen (buf
);
10698 for (int i
= 0; i
< aexpr
->len
; ++i
)
10699 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10706 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
10707 struct bp_target_info
*bp_tgt
, char *buf
)
10709 if (bp_tgt
->tcommands
.empty ())
10712 buf
+= strlen (buf
);
10714 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
10715 buf
+= strlen (buf
);
10717 /* Concatenate all the agent expressions that are commands into the
10719 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
10721 sprintf (buf
, "X%x,", aexpr
->len
);
10722 buf
+= strlen (buf
);
10723 for (int i
= 0; i
< aexpr
->len
; ++i
)
10724 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10729 /* Insert a breakpoint. On targets that have software breakpoint
10730 support, we ask the remote target to do the work; on targets
10731 which don't, we insert a traditional memory breakpoint. */
10734 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
10735 struct bp_target_info
*bp_tgt
)
10737 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10738 If it succeeds, then set the support to PACKET_ENABLE. If it
10739 fails, and the user has explicitly requested the Z support then
10740 report an error, otherwise, mark it disabled and go on. */
10742 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10744 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10745 struct remote_state
*rs
;
10748 /* Make sure the remote is pointing at the right process, if
10750 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10751 set_general_process ();
10753 rs
= get_remote_state ();
10754 p
= rs
->buf
.data ();
10755 endbuf
= p
+ get_remote_packet_size ();
10760 addr
= (ULONGEST
) remote_address_masked (addr
);
10761 p
+= hexnumstr (p
, addr
);
10762 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10764 if (supports_evaluation_of_breakpoint_conditions ())
10765 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10767 if (can_run_breakpoint_commands ())
10768 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10771 getpkt (&rs
->buf
, 0);
10773 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z0
))
10779 case PACKET_UNKNOWN
:
10784 /* If this breakpoint has target-side commands but this stub doesn't
10785 support Z0 packets, throw error. */
10786 if (!bp_tgt
->tcommands
.empty ())
10787 throw_error (NOT_SUPPORTED_ERROR
, _("\
10788 Target doesn't support breakpoints that have target side commands."));
10790 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
10794 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
10795 struct bp_target_info
*bp_tgt
,
10796 enum remove_bp_reason reason
)
10798 CORE_ADDR addr
= bp_tgt
->placed_address
;
10799 struct remote_state
*rs
= get_remote_state ();
10801 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10803 char *p
= rs
->buf
.data ();
10804 char *endbuf
= p
+ get_remote_packet_size ();
10806 /* Make sure the remote is pointing at the right process, if
10808 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10809 set_general_process ();
10815 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
10816 p
+= hexnumstr (p
, addr
);
10817 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10820 getpkt (&rs
->buf
, 0);
10822 return (rs
->buf
[0] == 'E');
10825 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
10828 static enum Z_packet_type
10829 watchpoint_to_Z_packet (int type
)
10834 return Z_PACKET_WRITE_WP
;
10837 return Z_PACKET_READ_WP
;
10840 return Z_PACKET_ACCESS_WP
;
10843 internal_error (_("hw_bp_to_z: bad watchpoint type %d"), type
);
10848 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
10849 enum target_hw_bp_type type
, struct expression
*cond
)
10851 struct remote_state
*rs
= get_remote_state ();
10852 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10854 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10856 if (m_features
.packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10859 /* Make sure the remote is pointing at the right process, if
10861 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10862 set_general_process ();
10864 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
10865 p
= strchr (rs
->buf
.data (), '\0');
10866 addr
= remote_address_masked (addr
);
10867 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10868 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10871 getpkt (&rs
->buf
, 0);
10873 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z0
+ packet
))
10877 case PACKET_UNKNOWN
:
10882 internal_error (_("remote_insert_watchpoint: reached end of function"));
10886 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
10887 CORE_ADDR start
, int length
)
10889 CORE_ADDR diff
= remote_address_masked (addr
- start
);
10891 return diff
< length
;
10896 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
10897 enum target_hw_bp_type type
, struct expression
*cond
)
10899 struct remote_state
*rs
= get_remote_state ();
10900 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10902 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10904 if (m_features
.packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10907 /* Make sure the remote is pointing at the right process, if
10909 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10910 set_general_process ();
10912 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
10913 p
= strchr (rs
->buf
.data (), '\0');
10914 addr
= remote_address_masked (addr
);
10915 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10916 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10918 getpkt (&rs
->buf
, 0);
10920 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z0
+ packet
))
10923 case PACKET_UNKNOWN
:
10928 internal_error (_("remote_remove_watchpoint: reached end of function"));
10932 static int remote_hw_watchpoint_limit
= -1;
10933 static int remote_hw_watchpoint_length_limit
= -1;
10934 static int remote_hw_breakpoint_limit
= -1;
10937 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
10939 if (remote_hw_watchpoint_length_limit
== 0)
10941 else if (remote_hw_watchpoint_length_limit
< 0)
10943 else if (len
<= remote_hw_watchpoint_length_limit
)
10950 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
10952 if (type
== bp_hardware_breakpoint
)
10954 if (remote_hw_breakpoint_limit
== 0)
10956 else if (remote_hw_breakpoint_limit
< 0)
10958 else if (cnt
<= remote_hw_breakpoint_limit
)
10963 if (remote_hw_watchpoint_limit
== 0)
10965 else if (remote_hw_watchpoint_limit
< 0)
10969 else if (cnt
<= remote_hw_watchpoint_limit
)
10975 /* The to_stopped_by_sw_breakpoint method of target remote. */
10978 remote_target::stopped_by_sw_breakpoint ()
10980 struct thread_info
*thread
= inferior_thread ();
10982 return (thread
->priv
!= NULL
10983 && (get_remote_thread_info (thread
)->stop_reason
10984 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
10987 /* The to_supports_stopped_by_sw_breakpoint method of target
10991 remote_target::supports_stopped_by_sw_breakpoint ()
10993 return (m_features
.packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
10996 /* The to_stopped_by_hw_breakpoint method of target remote. */
10999 remote_target::stopped_by_hw_breakpoint ()
11001 struct thread_info
*thread
= inferior_thread ();
11003 return (thread
->priv
!= NULL
11004 && (get_remote_thread_info (thread
)->stop_reason
11005 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
11008 /* The to_supports_stopped_by_hw_breakpoint method of target
11012 remote_target::supports_stopped_by_hw_breakpoint ()
11014 return (m_features
.packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
11018 remote_target::stopped_by_watchpoint ()
11020 struct thread_info
*thread
= inferior_thread ();
11022 return (thread
->priv
!= NULL
11023 && (get_remote_thread_info (thread
)->stop_reason
11024 == TARGET_STOPPED_BY_WATCHPOINT
));
11028 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
11030 struct thread_info
*thread
= inferior_thread ();
11032 if (thread
->priv
!= NULL
11033 && (get_remote_thread_info (thread
)->stop_reason
11034 == TARGET_STOPPED_BY_WATCHPOINT
))
11036 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
11045 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
11046 struct bp_target_info
*bp_tgt
)
11048 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
11049 struct remote_state
*rs
;
11053 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11056 /* Make sure the remote is pointing at the right process, if
11058 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
11059 set_general_process ();
11061 rs
= get_remote_state ();
11062 p
= rs
->buf
.data ();
11063 endbuf
= p
+ get_remote_packet_size ();
11069 addr
= remote_address_masked (addr
);
11070 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11071 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11073 if (supports_evaluation_of_breakpoint_conditions ())
11074 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
11076 if (can_run_breakpoint_commands ())
11077 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
11080 getpkt (&rs
->buf
, 0);
11082 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11085 if (rs
->buf
[1] == '.')
11087 message
= strchr (&rs
->buf
[2], '.');
11089 error (_("Remote failure reply: %s"), message
+ 1);
11092 case PACKET_UNKNOWN
:
11097 internal_error (_("remote_insert_hw_breakpoint: reached end of function"));
11102 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
11103 struct bp_target_info
*bp_tgt
)
11106 struct remote_state
*rs
= get_remote_state ();
11107 char *p
= rs
->buf
.data ();
11108 char *endbuf
= p
+ get_remote_packet_size ();
11110 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11113 /* Make sure the remote is pointing at the right process, if
11115 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
11116 set_general_process ();
11122 addr
= remote_address_masked (bp_tgt
->placed_address
);
11123 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11124 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11127 getpkt (&rs
->buf
, 0);
11129 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11132 case PACKET_UNKNOWN
:
11137 internal_error (_("remote_remove_hw_breakpoint: reached end of function"));
11140 /* Verify memory using the "qCRC:" request. */
11143 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
11145 struct remote_state
*rs
= get_remote_state ();
11146 unsigned long host_crc
, target_crc
;
11149 /* It doesn't make sense to use qCRC if the remote target is
11150 connected but not running. */
11151 if (target_has_execution ()
11152 && m_features
.packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
11154 enum packet_result result
;
11156 /* Make sure the remote is pointing at the right process. */
11157 set_general_process ();
11159 /* FIXME: assumes lma can fit into long. */
11160 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
11161 (long) lma
, (long) size
);
11164 /* Be clever; compute the host_crc before waiting for target
11166 host_crc
= xcrc32 (data
, size
, 0xffffffff);
11168 getpkt (&rs
->buf
, 0);
11170 result
= m_features
.packet_ok (rs
->buf
, PACKET_qCRC
);
11171 if (result
== PACKET_ERROR
)
11173 else if (result
== PACKET_OK
)
11175 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11176 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11178 return (host_crc
== target_crc
);
11182 return simple_verify_memory (this, data
, lma
, size
);
11185 /* compare-sections command
11187 With no arguments, compares each loadable section in the exec bfd
11188 with the same memory range on the target, and reports mismatches.
11189 Useful for verifying the image on the target against the exec file. */
11192 compare_sections_command (const char *args
, int from_tty
)
11195 const char *sectname
;
11196 bfd_size_type size
;
11199 int mismatched
= 0;
11203 if (!current_program_space
->exec_bfd ())
11204 error (_("command cannot be used without an exec file"));
11206 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11212 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11214 if (!(s
->flags
& SEC_LOAD
))
11215 continue; /* Skip non-loadable section. */
11217 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11218 continue; /* Skip writeable sections */
11220 size
= bfd_section_size (s
);
11222 continue; /* Skip zero-length section. */
11224 sectname
= bfd_section_name (s
);
11225 if (args
&& strcmp (args
, sectname
) != 0)
11226 continue; /* Not the section selected by user. */
11228 matched
= 1; /* Do this section. */
11231 gdb::byte_vector
sectdata (size
);
11232 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11233 sectdata
.data (), 0, size
);
11235 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11238 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11239 paddress (target_gdbarch (), lma
),
11240 paddress (target_gdbarch (), lma
+ size
));
11242 gdb_printf ("Section %s, range %s -- %s: ", sectname
,
11243 paddress (target_gdbarch (), lma
),
11244 paddress (target_gdbarch (), lma
+ size
));
11246 gdb_printf ("matched.\n");
11249 gdb_printf ("MIS-MATCHED!\n");
11253 if (mismatched
> 0)
11254 warning (_("One or more sections of the target image does "
11255 "not match the loaded file"));
11256 if (args
&& !matched
)
11257 gdb_printf (_("No loaded section named '%s'.\n"), args
);
11260 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11261 into remote target. The number of bytes written to the remote
11262 target is returned, or -1 for error. */
11265 remote_target::remote_write_qxfer (const char *object_name
,
11266 const char *annex
, const gdb_byte
*writebuf
,
11267 ULONGEST offset
, LONGEST len
,
11268 ULONGEST
*xfered_len
,
11269 const unsigned int which_packet
)
11273 struct remote_state
*rs
= get_remote_state ();
11274 int max_size
= get_memory_write_packet_size ();
11276 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11277 return TARGET_XFER_E_IO
;
11279 /* Insert header. */
11280 i
= snprintf (rs
->buf
.data (), max_size
,
11281 "qXfer:%s:write:%s:%s:",
11282 object_name
, annex
? annex
: "",
11283 phex_nz (offset
, sizeof offset
));
11284 max_size
-= (i
+ 1);
11286 /* Escape as much data as fits into rs->buf. */
11287 buf_len
= remote_escape_output
11288 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11290 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11291 || getpkt_sane (&rs
->buf
, 0) < 0
11292 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11293 return TARGET_XFER_E_IO
;
11295 unpack_varlen_hex (rs
->buf
.data (), &n
);
11298 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11301 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11302 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11303 number of bytes read is returned, or 0 for EOF, or -1 for error.
11304 The number of bytes read may be less than LEN without indicating an
11305 EOF. PACKET is checked and updated to indicate whether the remote
11306 target supports this object. */
11309 remote_target::remote_read_qxfer (const char *object_name
,
11311 gdb_byte
*readbuf
, ULONGEST offset
,
11313 ULONGEST
*xfered_len
,
11314 const unsigned int which_packet
)
11316 struct remote_state
*rs
= get_remote_state ();
11317 LONGEST i
, n
, packet_len
;
11319 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11320 return TARGET_XFER_E_IO
;
11322 /* Check whether we've cached an end-of-object packet that matches
11324 if (rs
->finished_object
)
11326 if (strcmp (object_name
, rs
->finished_object
) == 0
11327 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11328 && offset
== rs
->finished_offset
)
11329 return TARGET_XFER_EOF
;
11332 /* Otherwise, we're now reading something different. Discard
11334 xfree (rs
->finished_object
);
11335 xfree (rs
->finished_annex
);
11336 rs
->finished_object
= NULL
;
11337 rs
->finished_annex
= NULL
;
11340 /* Request only enough to fit in a single packet. The actual data
11341 may not, since we don't know how much of it will need to be escaped;
11342 the target is free to respond with slightly less data. We subtract
11343 five to account for the response type and the protocol frame. */
11344 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11345 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11346 "qXfer:%s:read:%s:%s,%s",
11347 object_name
, annex
? annex
: "",
11348 phex_nz (offset
, sizeof offset
),
11349 phex_nz (n
, sizeof n
));
11350 i
= putpkt (rs
->buf
);
11352 return TARGET_XFER_E_IO
;
11355 packet_len
= getpkt_sane (&rs
->buf
, 0);
11357 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11358 return TARGET_XFER_E_IO
;
11360 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11361 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11363 /* 'm' means there is (or at least might be) more data after this
11364 batch. That does not make sense unless there's at least one byte
11365 of data in this reply. */
11366 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11367 error (_("Remote qXfer reply contained no data."));
11369 /* Got some data. */
11370 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11371 packet_len
- 1, readbuf
, n
);
11373 /* 'l' is an EOF marker, possibly including a final block of data,
11374 or possibly empty. If we have the final block of a non-empty
11375 object, record this fact to bypass a subsequent partial read. */
11376 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11378 rs
->finished_object
= xstrdup (object_name
);
11379 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11380 rs
->finished_offset
= offset
+ i
;
11384 return TARGET_XFER_EOF
;
11388 return TARGET_XFER_OK
;
11392 enum target_xfer_status
11393 remote_target::xfer_partial (enum target_object object
,
11394 const char *annex
, gdb_byte
*readbuf
,
11395 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11396 ULONGEST
*xfered_len
)
11398 struct remote_state
*rs
;
11402 int unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
11404 set_remote_traceframe ();
11405 set_general_thread (inferior_ptid
);
11407 rs
= get_remote_state ();
11409 /* Handle memory using the standard memory routines. */
11410 if (object
== TARGET_OBJECT_MEMORY
)
11412 /* If the remote target is connected but not running, we should
11413 pass this request down to a lower stratum (e.g. the executable
11415 if (!target_has_execution ())
11416 return TARGET_XFER_EOF
;
11418 if (writebuf
!= NULL
)
11419 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11422 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11426 /* Handle extra signal info using qxfer packets. */
11427 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11430 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11431 xfered_len
, PACKET_qXfer_siginfo_read
);
11433 return remote_write_qxfer ("siginfo", annex
, writebuf
, offset
, len
,
11434 xfered_len
, PACKET_qXfer_siginfo_write
);
11437 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11440 return remote_read_qxfer ("statictrace", annex
,
11441 readbuf
, offset
, len
, xfered_len
,
11442 PACKET_qXfer_statictrace_read
);
11444 return TARGET_XFER_E_IO
;
11447 /* Only handle flash writes. */
11448 if (writebuf
!= NULL
)
11452 case TARGET_OBJECT_FLASH
:
11453 return remote_flash_write (offset
, len
, xfered_len
,
11457 return TARGET_XFER_E_IO
;
11461 /* Map pre-existing objects onto letters. DO NOT do this for new
11462 objects!!! Instead specify new query packets. */
11465 case TARGET_OBJECT_AVR
:
11469 case TARGET_OBJECT_AUXV
:
11470 gdb_assert (annex
== NULL
);
11471 return remote_read_qxfer
11472 ("auxv", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_auxv
);
11474 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11475 return remote_read_qxfer
11476 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11477 PACKET_qXfer_features
);
11479 case TARGET_OBJECT_LIBRARIES
:
11480 return remote_read_qxfer
11481 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11482 PACKET_qXfer_libraries
);
11484 case TARGET_OBJECT_LIBRARIES_SVR4
:
11485 return remote_read_qxfer
11486 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11487 PACKET_qXfer_libraries_svr4
);
11489 case TARGET_OBJECT_MEMORY_MAP
:
11490 gdb_assert (annex
== NULL
);
11491 return remote_read_qxfer
11492 ("memory-map", annex
, readbuf
, offset
, len
, xfered_len
,
11493 PACKET_qXfer_memory_map
);
11495 case TARGET_OBJECT_OSDATA
:
11496 /* Should only get here if we're connected. */
11497 gdb_assert (rs
->remote_desc
);
11498 return remote_read_qxfer
11499 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11500 PACKET_qXfer_osdata
);
11502 case TARGET_OBJECT_THREADS
:
11503 gdb_assert (annex
== NULL
);
11504 return remote_read_qxfer
11505 ("threads", annex
, readbuf
, offset
, len
, xfered_len
,
11506 PACKET_qXfer_threads
);
11508 case TARGET_OBJECT_TRACEFRAME_INFO
:
11509 gdb_assert (annex
== NULL
);
11510 return remote_read_qxfer
11511 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11512 PACKET_qXfer_traceframe_info
);
11514 case TARGET_OBJECT_FDPIC
:
11515 return remote_read_qxfer
11516 ("fdpic", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_fdpic
);
11518 case TARGET_OBJECT_OPENVMS_UIB
:
11519 return remote_read_qxfer
11520 ("uib", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_uib
);
11522 case TARGET_OBJECT_BTRACE
:
11523 return remote_read_qxfer
11524 ("btrace", annex
, readbuf
, offset
, len
, xfered_len
,
11525 PACKET_qXfer_btrace
);
11527 case TARGET_OBJECT_BTRACE_CONF
:
11528 return remote_read_qxfer
11529 ("btrace-conf", annex
, readbuf
, offset
, len
, xfered_len
,
11530 PACKET_qXfer_btrace_conf
);
11532 case TARGET_OBJECT_EXEC_FILE
:
11533 return remote_read_qxfer
11534 ("exec-file", annex
, readbuf
, offset
, len
, xfered_len
,
11535 PACKET_qXfer_exec_file
);
11538 return TARGET_XFER_E_IO
;
11541 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11542 large enough let the caller deal with it. */
11543 if (len
< get_remote_packet_size ())
11544 return TARGET_XFER_E_IO
;
11545 len
= get_remote_packet_size ();
11547 /* Except for querying the minimum buffer size, target must be open. */
11548 if (!rs
->remote_desc
)
11549 error (_("remote query is only available after target open"));
11551 gdb_assert (annex
!= NULL
);
11552 gdb_assert (readbuf
!= NULL
);
11554 p2
= rs
->buf
.data ();
11556 *p2
++ = query_type
;
11558 /* We used one buffer char for the remote protocol q command and
11559 another for the query type. As the remote protocol encapsulation
11560 uses 4 chars plus one extra in case we are debugging
11561 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11564 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11566 /* Bad caller may have sent forbidden characters. */
11567 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11572 gdb_assert (annex
[i
] == '\0');
11574 i
= putpkt (rs
->buf
);
11576 return TARGET_XFER_E_IO
;
11578 getpkt (&rs
->buf
, 0);
11579 strcpy ((char *) readbuf
, rs
->buf
.data ());
11581 *xfered_len
= strlen ((char *) readbuf
);
11582 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11585 /* Implementation of to_get_memory_xfer_limit. */
11588 remote_target::get_memory_xfer_limit ()
11590 return get_memory_write_packet_size ();
11594 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11595 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11596 CORE_ADDR
*found_addrp
)
11598 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
11599 struct remote_state
*rs
= get_remote_state ();
11600 int max_size
= get_memory_write_packet_size ();
11602 /* Number of packet bytes used to encode the pattern;
11603 this could be more than PATTERN_LEN due to escape characters. */
11604 int escaped_pattern_len
;
11605 /* Amount of pattern that was encodable in the packet. */
11606 int used_pattern_len
;
11609 ULONGEST found_addr
;
11611 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11613 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11617 /* Don't go to the target if we don't have to. This is done before
11618 checking packet_support to avoid the possibility that a success for this
11619 edge case means the facility works in general. */
11620 if (pattern_len
> search_space_len
)
11622 if (pattern_len
== 0)
11624 *found_addrp
= start_addr
;
11628 /* If we already know the packet isn't supported, fall back to the simple
11629 way of searching memory. */
11631 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11633 /* Target doesn't provided special support, fall back and use the
11634 standard support (copy memory and do the search here). */
11635 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11636 pattern
, pattern_len
, found_addrp
);
11639 /* Make sure the remote is pointing at the right process. */
11640 set_general_process ();
11642 /* Insert header. */
11643 i
= snprintf (rs
->buf
.data (), max_size
,
11644 "qSearch:memory:%s;%s;",
11645 phex_nz (start_addr
, addr_size
),
11646 phex_nz (search_space_len
, sizeof (search_space_len
)));
11647 max_size
-= (i
+ 1);
11649 /* Escape as much data as fits into rs->buf. */
11650 escaped_pattern_len
=
11651 remote_escape_output (pattern
, pattern_len
, 1,
11652 (gdb_byte
*) rs
->buf
.data () + i
,
11653 &used_pattern_len
, max_size
);
11655 /* Bail if the pattern is too large. */
11656 if (used_pattern_len
!= pattern_len
)
11657 error (_("Pattern is too large to transmit to remote target."));
11659 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
11660 || getpkt_sane (&rs
->buf
, 0) < 0
11661 || m_features
.packet_ok (rs
->buf
, PACKET_qSearch_memory
) != PACKET_OK
)
11663 /* The request may not have worked because the command is not
11664 supported. If so, fall back to the simple way. */
11665 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11667 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11668 pattern
, pattern_len
, found_addrp
);
11673 if (rs
->buf
[0] == '0')
11675 else if (rs
->buf
[0] == '1')
11678 if (rs
->buf
[1] != ',')
11679 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11680 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
11681 *found_addrp
= found_addr
;
11684 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11690 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
11692 struct remote_state
*rs
= get_remote_state ();
11693 char *p
= rs
->buf
.data ();
11695 if (!rs
->remote_desc
)
11696 error (_("remote rcmd is only available after target open"));
11698 /* Send a NULL command across as an empty command. */
11699 if (command
== NULL
)
11702 /* The query prefix. */
11703 strcpy (rs
->buf
.data (), "qRcmd,");
11704 p
= strchr (rs
->buf
.data (), '\0');
11706 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
11707 > get_remote_packet_size ())
11708 error (_("\"monitor\" command ``%s'' is too long."), command
);
11710 /* Encode the actual command. */
11711 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
11713 if (putpkt (rs
->buf
) < 0)
11714 error (_("Communication problem with target."));
11716 /* get/display the response */
11721 /* XXX - see also remote_get_noisy_reply(). */
11722 QUIT
; /* Allow user to bail out with ^C. */
11724 if (getpkt_sane (&rs
->buf
, 0) == -1)
11726 /* Timeout. Continue to (try to) read responses.
11727 This is better than stopping with an error, assuming the stub
11728 is still executing the (long) monitor command.
11729 If needed, the user can interrupt gdb using C-c, obtaining
11730 an effect similar to stop on timeout. */
11733 buf
= rs
->buf
.data ();
11734 if (buf
[0] == '\0')
11735 error (_("Target does not support this command."));
11736 if (buf
[0] == 'O' && buf
[1] != 'K')
11738 remote_console_output (buf
+ 1); /* 'O' message from stub. */
11741 if (strcmp (buf
, "OK") == 0)
11743 if (strlen (buf
) == 3 && buf
[0] == 'E'
11744 && isxdigit (buf
[1]) && isxdigit (buf
[2]))
11746 error (_("Protocol error with Rcmd"));
11748 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
11750 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
11752 gdb_putc (c
, outbuf
);
11758 std::vector
<mem_region
>
11759 remote_target::memory_map ()
11761 std::vector
<mem_region
> result
;
11762 gdb::optional
<gdb::char_vector
> text
11763 = target_read_stralloc (current_inferior ()->top_target (),
11764 TARGET_OBJECT_MEMORY_MAP
, NULL
);
11767 result
= parse_memory_map (text
->data ());
11772 /* Set of callbacks used to implement the 'maint packet' command. */
11774 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
11776 /* Called before the packet is sent. BUF is the packet content before
11777 the protocol specific prefix, suffix, and escaping is added. */
11779 void sending (gdb::array_view
<const char> &buf
) override
11781 gdb_puts ("sending: ");
11782 print_packet (buf
);
11786 /* Called with BUF, the reply from the remote target. */
11788 void received (gdb::array_view
<const char> &buf
) override
11790 gdb_puts ("received: \"");
11791 print_packet (buf
);
11797 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
11798 '\x??' with '??' replaced by the hexadecimal value of the byte. */
11801 print_packet (gdb::array_view
<const char> &buf
)
11805 for (int i
= 0; i
< buf
.size (); ++i
)
11807 gdb_byte c
= buf
[i
];
11809 gdb_putc (c
, &stb
);
11811 gdb_printf (&stb
, "\\x%02x", (unsigned char) c
);
11814 gdb_puts (stb
.string ().c_str ());
11818 /* See remote.h. */
11821 send_remote_packet (gdb::array_view
<const char> &buf
,
11822 send_remote_packet_callbacks
*callbacks
)
11824 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
11825 error (_("a remote packet must not be empty"));
11827 remote_target
*remote
= get_current_remote_target ();
11828 if (remote
== nullptr)
11829 error (_("packets can only be sent to a remote target"));
11831 callbacks
->sending (buf
);
11833 remote
->putpkt_binary (buf
.data (), buf
.size ());
11834 remote_state
*rs
= remote
->get_remote_state ();
11835 int bytes
= remote
->getpkt_sane (&rs
->buf
, 0);
11838 error (_("error while fetching packet from remote target"));
11840 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
11841 callbacks
->received (view
);
11844 /* Entry point for the 'maint packet' command. */
11847 cli_packet_command (const char *args
, int from_tty
)
11849 cli_packet_command_callbacks cb
;
11850 gdb::array_view
<const char> view
11851 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
11852 send_remote_packet (view
, &cb
);
11856 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
11858 static void display_thread_info (struct gdb_ext_thread_info
*info
);
11860 static void threadset_test_cmd (char *cmd
, int tty
);
11862 static void threadalive_test (char *cmd
, int tty
);
11864 static void threadlist_test_cmd (char *cmd
, int tty
);
11866 int get_and_display_threadinfo (threadref
*ref
);
11868 static void threadinfo_test_cmd (char *cmd
, int tty
);
11870 static int thread_display_step (threadref
*ref
, void *context
);
11872 static void threadlist_update_test_cmd (char *cmd
, int tty
);
11874 static void init_remote_threadtests (void);
11876 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
11879 threadset_test_cmd (const char *cmd
, int tty
)
11881 int sample_thread
= SAMPLE_THREAD
;
11883 gdb_printf (_("Remote threadset test\n"));
11884 set_general_thread (sample_thread
);
11889 threadalive_test (const char *cmd
, int tty
)
11891 int sample_thread
= SAMPLE_THREAD
;
11892 int pid
= inferior_ptid
.pid ();
11893 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
11895 if (remote_thread_alive (ptid
))
11896 gdb_printf ("PASS: Thread alive test\n");
11898 gdb_printf ("FAIL: Thread alive test\n");
11901 void output_threadid (char *title
, threadref
*ref
);
11904 output_threadid (char *title
, threadref
*ref
)
11908 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
11910 gdb_printf ("%s %s\n", title
, (&hexid
[0]));
11914 threadlist_test_cmd (const char *cmd
, int tty
)
11917 threadref nextthread
;
11918 int done
, result_count
;
11919 threadref threadlist
[3];
11921 gdb_printf ("Remote Threadlist test\n");
11922 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
11923 &result_count
, &threadlist
[0]))
11924 gdb_printf ("FAIL: threadlist test\n");
11927 threadref
*scan
= threadlist
;
11928 threadref
*limit
= scan
+ result_count
;
11930 while (scan
< limit
)
11931 output_threadid (" thread ", scan
++);
11936 display_thread_info (struct gdb_ext_thread_info
*info
)
11938 output_threadid ("Threadid: ", &info
->threadid
);
11939 gdb_printf ("Name: %s\n ", info
->shortname
);
11940 gdb_printf ("State: %s\n", info
->display
);
11941 gdb_printf ("other: %s\n\n", info
->more_display
);
11945 get_and_display_threadinfo (threadref
*ref
)
11949 struct gdb_ext_thread_info threadinfo
;
11951 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
11952 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
11953 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
11954 display_thread_info (&threadinfo
);
11959 threadinfo_test_cmd (const char *cmd
, int tty
)
11961 int athread
= SAMPLE_THREAD
;
11965 int_to_threadref (&thread
, athread
);
11966 gdb_printf ("Remote Threadinfo test\n");
11967 if (!get_and_display_threadinfo (&thread
))
11968 gdb_printf ("FAIL cannot get thread info\n");
11972 thread_display_step (threadref
*ref
, void *context
)
11974 /* output_threadid(" threadstep ",ref); *//* simple test */
11975 return get_and_display_threadinfo (ref
);
11979 threadlist_update_test_cmd (const char *cmd
, int tty
)
11981 gdb_printf ("Remote Threadlist update test\n");
11982 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
11986 init_remote_threadtests (void)
11988 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
11989 _("Fetch and print the remote list of "
11990 "thread identifiers, one pkt only."));
11991 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
11992 _("Fetch and display info about one thread."));
11993 add_com ("tset", class_obscure
, threadset_test_cmd
,
11994 _("Test setting to a different thread."));
11995 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
11996 _("Iterate through updating all remote thread info."));
11997 add_com ("talive", class_obscure
, threadalive_test
,
11998 _("Remote thread alive test."));
12003 /* Convert a thread ID to a string. */
12006 remote_target::pid_to_str (ptid_t ptid
)
12008 if (ptid
== null_ptid
)
12009 return normal_pid_to_str (ptid
);
12010 else if (ptid
.is_pid ())
12012 /* Printing an inferior target id. */
12014 /* When multi-process extensions are off, there's no way in the
12015 remote protocol to know the remote process id, if there's any
12016 at all. There's one exception --- when we're connected with
12017 target extended-remote, and we manually attached to a process
12018 with "attach PID". We don't record anywhere a flag that
12019 allows us to distinguish that case from the case of
12020 connecting with extended-remote and the stub already being
12021 attached to a process, and reporting yes to qAttached, hence
12022 no smart special casing here. */
12023 if (!m_features
.remote_multi_process_p ())
12024 return "Remote target";
12026 return normal_pid_to_str (ptid
);
12030 if (magic_null_ptid
== ptid
)
12031 return "Thread <main>";
12032 else if (m_features
.remote_multi_process_p ())
12033 if (ptid
.lwp () == 0)
12034 return normal_pid_to_str (ptid
);
12036 return string_printf ("Thread %d.%ld",
12037 ptid
.pid (), ptid
.lwp ());
12039 return string_printf ("Thread %ld", ptid
.lwp ());
12043 /* Get the address of the thread local variable in OBJFILE which is
12044 stored at OFFSET within the thread local storage for thread PTID. */
12047 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
12050 if (m_features
.packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
12052 struct remote_state
*rs
= get_remote_state ();
12053 char *p
= rs
->buf
.data ();
12054 char *endp
= p
+ get_remote_packet_size ();
12055 enum packet_result result
;
12057 strcpy (p
, "qGetTLSAddr:");
12059 p
= write_ptid (p
, endp
, ptid
);
12061 p
+= hexnumstr (p
, offset
);
12063 p
+= hexnumstr (p
, lm
);
12067 getpkt (&rs
->buf
, 0);
12068 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTLSAddr
);
12069 if (result
== PACKET_OK
)
12073 unpack_varlen_hex (rs
->buf
.data (), &addr
);
12076 else if (result
== PACKET_UNKNOWN
)
12077 throw_error (TLS_GENERIC_ERROR
,
12078 _("Remote target doesn't support qGetTLSAddr packet"));
12080 throw_error (TLS_GENERIC_ERROR
,
12081 _("Remote target failed to process qGetTLSAddr request"));
12084 throw_error (TLS_GENERIC_ERROR
,
12085 _("TLS not supported or disabled on this target"));
12090 /* Provide thread local base, i.e. Thread Information Block address.
12091 Returns 1 if ptid is found and thread_local_base is non zero. */
12094 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
12096 if (m_features
.packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
12098 struct remote_state
*rs
= get_remote_state ();
12099 char *p
= rs
->buf
.data ();
12100 char *endp
= p
+ get_remote_packet_size ();
12101 enum packet_result result
;
12103 strcpy (p
, "qGetTIBAddr:");
12105 p
= write_ptid (p
, endp
, ptid
);
12109 getpkt (&rs
->buf
, 0);
12110 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTIBAddr
);
12111 if (result
== PACKET_OK
)
12114 unpack_varlen_hex (rs
->buf
.data (), &val
);
12116 *addr
= (CORE_ADDR
) val
;
12119 else if (result
== PACKET_UNKNOWN
)
12120 error (_("Remote target doesn't support qGetTIBAddr packet"));
12122 error (_("Remote target failed to process qGetTIBAddr request"));
12125 error (_("qGetTIBAddr not supported or disabled on this target"));
12130 /* Support for inferring a target description based on the current
12131 architecture and the size of a 'g' packet. While the 'g' packet
12132 can have any size (since optional registers can be left off the
12133 end), some sizes are easily recognizable given knowledge of the
12134 approximate architecture. */
12136 struct remote_g_packet_guess
12138 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
12145 const struct target_desc
*tdesc
;
12148 struct remote_g_packet_data
12150 std::vector
<remote_g_packet_guess
> guesses
;
12153 static const registry
<gdbarch
>::key
<struct remote_g_packet_data
>
12154 remote_g_packet_data_handle
;
12156 static struct remote_g_packet_data
*
12157 get_g_packet_data (struct gdbarch
*gdbarch
)
12159 struct remote_g_packet_data
*data
12160 = remote_g_packet_data_handle
.get (gdbarch
);
12161 if (data
== nullptr)
12162 data
= remote_g_packet_data_handle
.emplace (gdbarch
);
12167 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12168 const struct target_desc
*tdesc
)
12170 struct remote_g_packet_data
*data
= get_g_packet_data (gdbarch
);
12172 gdb_assert (tdesc
!= NULL
);
12174 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12175 if (guess
.bytes
== bytes
)
12176 internal_error (_("Duplicate g packet description added for size %d"),
12179 data
->guesses
.emplace_back (bytes
, tdesc
);
12182 /* Return true if remote_read_description would do anything on this target
12183 and architecture, false otherwise. */
12186 remote_read_description_p (struct target_ops
*target
)
12188 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12190 return !data
->guesses
.empty ();
12193 const struct target_desc
*
12194 remote_target::read_description ()
12196 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12198 /* Do not try this during initial connection, when we do not know
12199 whether there is a running but stopped thread. */
12200 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12201 return beneath ()->read_description ();
12203 if (!data
->guesses
.empty ())
12205 int bytes
= send_g_packet ();
12207 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12208 if (guess
.bytes
== bytes
)
12209 return guess
.tdesc
;
12211 /* We discard the g packet. A minor optimization would be to
12212 hold on to it, and fill the register cache once we have selected
12213 an architecture, but it's too tricky to do safely. */
12216 return beneath ()->read_description ();
12219 /* Remote file transfer support. This is host-initiated I/O, not
12220 target-initiated; for target-initiated, see remote-fileio.c. */
12222 /* If *LEFT is at least the length of STRING, copy STRING to
12223 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12224 decrease *LEFT. Otherwise raise an error. */
12227 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12229 int len
= strlen (string
);
12232 error (_("Packet too long for target."));
12234 memcpy (*buffer
, string
, len
);
12238 /* NUL-terminate the buffer as a convenience, if there is
12244 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12245 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12246 decrease *LEFT. Otherwise raise an error. */
12249 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12252 if (2 * len
> *left
)
12253 error (_("Packet too long for target."));
12255 bin2hex (bytes
, *buffer
, len
);
12256 *buffer
+= 2 * len
;
12259 /* NUL-terminate the buffer as a convenience, if there is
12265 /* If *LEFT is large enough, convert VALUE to hex and add it to
12266 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12267 decrease *LEFT. Otherwise raise an error. */
12270 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12272 int len
= hexnumlen (value
);
12275 error (_("Packet too long for target."));
12277 hexnumstr (*buffer
, value
);
12281 /* NUL-terminate the buffer as a convenience, if there is
12287 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12288 value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
12289 was included, and *ATTACHMENT to point to the start of the annex
12290 if any. The length of the packet isn't needed here; there may
12291 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12293 Return 0 if the packet could be parsed, -1 if it could not. If
12294 -1 is returned, the other variables may not be initialized. */
12297 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12298 fileio_error
*remote_errno
, const char **attachment
)
12302 *remote_errno
= FILEIO_SUCCESS
;
12303 *attachment
= NULL
;
12305 if (buffer
[0] != 'F')
12309 *retcode
= strtol (&buffer
[1], &p
, 16);
12310 if (errno
!= 0 || p
== &buffer
[1])
12313 /* Check for ",errno". */
12317 *remote_errno
= (fileio_error
) strtol (p
+ 1, &p2
, 16);
12318 if (errno
!= 0 || p
+ 1 == p2
)
12323 /* Check for ";attachment". If there is no attachment, the
12324 packet should end here. */
12327 *attachment
= p
+ 1;
12330 else if (*p
== '\0')
12336 /* Send a prepared I/O packet to the target and read its response.
12337 The prepared packet is in the global RS->BUF before this function
12338 is called, and the answer is there when we return.
12340 COMMAND_BYTES is the length of the request to send, which may include
12341 binary data. WHICH_PACKET is the packet configuration to check
12342 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12343 is set to the error number and -1 is returned. Otherwise the value
12344 returned by the function is returned.
12346 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12347 attachment is expected; an error will be reported if there's a
12348 mismatch. If one is found, *ATTACHMENT will be set to point into
12349 the packet buffer and *ATTACHMENT_LEN will be set to the
12350 attachment's length. */
12353 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12354 fileio_error
*remote_errno
, const char **attachment
,
12355 int *attachment_len
)
12357 struct remote_state
*rs
= get_remote_state ();
12358 int ret
, bytes_read
;
12359 const char *attachment_tmp
;
12361 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
12363 *remote_errno
= FILEIO_ENOSYS
;
12367 putpkt_binary (rs
->buf
.data (), command_bytes
);
12368 bytes_read
= getpkt_sane (&rs
->buf
, 0);
12370 /* If it timed out, something is wrong. Don't try to parse the
12372 if (bytes_read
< 0)
12374 *remote_errno
= FILEIO_EINVAL
;
12378 switch (m_features
.packet_ok (rs
->buf
, which_packet
))
12381 *remote_errno
= FILEIO_EINVAL
;
12383 case PACKET_UNKNOWN
:
12384 *remote_errno
= FILEIO_ENOSYS
;
12390 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12393 *remote_errno
= FILEIO_EINVAL
;
12397 /* Make sure we saw an attachment if and only if we expected one. */
12398 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12399 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12401 *remote_errno
= FILEIO_EINVAL
;
12405 /* If an attachment was found, it must point into the packet buffer;
12406 work out how many bytes there were. */
12407 if (attachment_tmp
!= NULL
)
12409 *attachment
= attachment_tmp
;
12410 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12416 /* See declaration.h. */
12419 readahead_cache::invalidate ()
12424 /* See declaration.h. */
12427 readahead_cache::invalidate_fd (int fd
)
12429 if (this->fd
== fd
)
12433 /* Set the filesystem remote_hostio functions that take FILENAME
12434 arguments will use. Return 0 on success, or -1 if an error
12435 occurs (and set *REMOTE_ERRNO). */
12438 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12439 fileio_error
*remote_errno
)
12441 struct remote_state
*rs
= get_remote_state ();
12442 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12443 char *p
= rs
->buf
.data ();
12444 int left
= get_remote_packet_size () - 1;
12448 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12451 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12454 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12456 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12457 remote_buffer_add_string (&p
, &left
, arg
);
12459 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12460 remote_errno
, NULL
, NULL
);
12462 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12466 rs
->fs_pid
= required_pid
;
12471 /* Implementation of to_fileio_open. */
12474 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12475 int flags
, int mode
, int warn_if_slow
,
12476 fileio_error
*remote_errno
)
12478 struct remote_state
*rs
= get_remote_state ();
12479 char *p
= rs
->buf
.data ();
12480 int left
= get_remote_packet_size () - 1;
12484 static int warning_issued
= 0;
12486 gdb_printf (_("Reading %s from remote target...\n"),
12489 if (!warning_issued
)
12491 warning (_("File transfers from remote targets can be slow."
12492 " Use \"set sysroot\" to access files locally"
12494 warning_issued
= 1;
12498 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12501 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12503 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12504 strlen (filename
));
12505 remote_buffer_add_string (&p
, &left
, ",");
12507 remote_buffer_add_int (&p
, &left
, flags
);
12508 remote_buffer_add_string (&p
, &left
, ",");
12510 remote_buffer_add_int (&p
, &left
, mode
);
12512 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12513 remote_errno
, NULL
, NULL
);
12517 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12518 int flags
, int mode
, int warn_if_slow
,
12519 fileio_error
*remote_errno
)
12521 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12525 /* Implementation of to_fileio_pwrite. */
12528 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12529 ULONGEST offset
, fileio_error
*remote_errno
)
12531 struct remote_state
*rs
= get_remote_state ();
12532 char *p
= rs
->buf
.data ();
12533 int left
= get_remote_packet_size ();
12536 rs
->readahead_cache
.invalidate_fd (fd
);
12538 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12540 remote_buffer_add_int (&p
, &left
, fd
);
12541 remote_buffer_add_string (&p
, &left
, ",");
12543 remote_buffer_add_int (&p
, &left
, offset
);
12544 remote_buffer_add_string (&p
, &left
, ",");
12546 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12547 (get_remote_packet_size ()
12548 - (p
- rs
->buf
.data ())));
12550 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12551 remote_errno
, NULL
, NULL
);
12555 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12556 ULONGEST offset
, fileio_error
*remote_errno
)
12558 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12561 /* Helper for the implementation of to_fileio_pread. Read the file
12562 from the remote side with vFile:pread. */
12565 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12566 ULONGEST offset
, fileio_error
*remote_errno
)
12568 struct remote_state
*rs
= get_remote_state ();
12569 char *p
= rs
->buf
.data ();
12570 const char *attachment
;
12571 int left
= get_remote_packet_size ();
12572 int ret
, attachment_len
;
12575 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12577 remote_buffer_add_int (&p
, &left
, fd
);
12578 remote_buffer_add_string (&p
, &left
, ",");
12580 remote_buffer_add_int (&p
, &left
, len
);
12581 remote_buffer_add_string (&p
, &left
, ",");
12583 remote_buffer_add_int (&p
, &left
, offset
);
12585 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12586 remote_errno
, &attachment
,
12592 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12594 if (read_len
!= ret
)
12595 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12600 /* See declaration.h. */
12603 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12607 && this->offset
<= offset
12608 && offset
< this->offset
+ this->bufsize
)
12610 ULONGEST max
= this->offset
+ this->bufsize
;
12612 if (offset
+ len
> max
)
12613 len
= max
- offset
;
12615 memcpy (read_buf
, this->buf
+ offset
- this->offset
, len
);
12622 /* Implementation of to_fileio_pread. */
12625 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12626 ULONGEST offset
, fileio_error
*remote_errno
)
12629 struct remote_state
*rs
= get_remote_state ();
12630 readahead_cache
*cache
= &rs
->readahead_cache
;
12632 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
12635 cache
->hit_count
++;
12637 remote_debug_printf ("readahead cache hit %s",
12638 pulongest (cache
->hit_count
));
12642 cache
->miss_count
++;
12644 remote_debug_printf ("readahead cache miss %s",
12645 pulongest (cache
->miss_count
));
12648 cache
->offset
= offset
;
12649 cache
->bufsize
= get_remote_packet_size ();
12650 cache
->buf
= (gdb_byte
*) xrealloc (cache
->buf
, cache
->bufsize
);
12652 ret
= remote_hostio_pread_vFile (cache
->fd
, cache
->buf
, cache
->bufsize
,
12653 cache
->offset
, remote_errno
);
12656 cache
->invalidate_fd (fd
);
12660 cache
->bufsize
= ret
;
12661 return cache
->pread (fd
, read_buf
, len
, offset
);
12665 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12666 ULONGEST offset
, fileio_error
*remote_errno
)
12668 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
12671 /* Implementation of to_fileio_close. */
12674 remote_target::remote_hostio_close (int fd
, fileio_error
*remote_errno
)
12676 struct remote_state
*rs
= get_remote_state ();
12677 char *p
= rs
->buf
.data ();
12678 int left
= get_remote_packet_size () - 1;
12680 rs
->readahead_cache
.invalidate_fd (fd
);
12682 remote_buffer_add_string (&p
, &left
, "vFile:close:");
12684 remote_buffer_add_int (&p
, &left
, fd
);
12686 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
12687 remote_errno
, NULL
, NULL
);
12691 remote_target::fileio_close (int fd
, fileio_error
*remote_errno
)
12693 return remote_hostio_close (fd
, remote_errno
);
12696 /* Implementation of to_fileio_unlink. */
12699 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
12700 fileio_error
*remote_errno
)
12702 struct remote_state
*rs
= get_remote_state ();
12703 char *p
= rs
->buf
.data ();
12704 int left
= get_remote_packet_size () - 1;
12706 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12709 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
12711 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12712 strlen (filename
));
12714 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
12715 remote_errno
, NULL
, NULL
);
12719 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
12720 fileio_error
*remote_errno
)
12722 return remote_hostio_unlink (inf
, filename
, remote_errno
);
12725 /* Implementation of to_fileio_readlink. */
12727 gdb::optional
<std::string
>
12728 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
12729 fileio_error
*remote_errno
)
12731 struct remote_state
*rs
= get_remote_state ();
12732 char *p
= rs
->buf
.data ();
12733 const char *attachment
;
12734 int left
= get_remote_packet_size ();
12735 int len
, attachment_len
;
12738 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12741 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
12743 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12744 strlen (filename
));
12746 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
12747 remote_errno
, &attachment
,
12753 std::string
ret (len
, '\0');
12755 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12756 (gdb_byte
*) &ret
[0], len
);
12757 if (read_len
!= len
)
12758 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
12763 /* Implementation of to_fileio_fstat. */
12766 remote_target::fileio_fstat (int fd
, struct stat
*st
, fileio_error
*remote_errno
)
12768 struct remote_state
*rs
= get_remote_state ();
12769 char *p
= rs
->buf
.data ();
12770 int left
= get_remote_packet_size ();
12771 int attachment_len
, ret
;
12772 const char *attachment
;
12773 struct fio_stat fst
;
12776 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
12778 remote_buffer_add_int (&p
, &left
, fd
);
12780 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
12781 remote_errno
, &attachment
,
12785 if (*remote_errno
!= FILEIO_ENOSYS
)
12788 /* Strictly we should return -1, ENOSYS here, but when
12789 "set sysroot remote:" was implemented in August 2008
12790 BFD's need for a stat function was sidestepped with
12791 this hack. This was not remedied until March 2015
12792 so we retain the previous behavior to avoid breaking
12795 Note that the memset is a March 2015 addition; older
12796 GDBs set st_size *and nothing else* so the structure
12797 would have garbage in all other fields. This might
12798 break something but retaining the previous behavior
12799 here would be just too wrong. */
12801 memset (st
, 0, sizeof (struct stat
));
12802 st
->st_size
= INT_MAX
;
12806 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12807 (gdb_byte
*) &fst
, sizeof (fst
));
12809 if (read_len
!= ret
)
12810 error (_("vFile:fstat returned %d, but %d bytes."), ret
, read_len
);
12812 if (read_len
!= sizeof (fst
))
12813 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12814 read_len
, (int) sizeof (fst
));
12816 remote_fileio_to_host_stat (&fst
, st
);
12821 /* Implementation of to_filesystem_is_local. */
12824 remote_target::filesystem_is_local ()
12826 /* Valgrind GDB presents itself as a remote target but works
12827 on the local filesystem: it does not implement remote get
12828 and users are not expected to set a sysroot. To handle
12829 this case we treat the remote filesystem as local if the
12830 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12831 does not support vFile:open. */
12832 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
12834 packet_support ps
= m_features
.packet_support (PACKET_vFile_open
);
12836 if (ps
== PACKET_SUPPORT_UNKNOWN
)
12839 fileio_error remote_errno
;
12841 /* Try opening a file to probe support. The supplied
12842 filename is irrelevant, we only care about whether
12843 the stub recognizes the packet or not. */
12844 fd
= remote_hostio_open (NULL
, "just probing",
12845 FILEIO_O_RDONLY
, 0700, 0,
12849 remote_hostio_close (fd
, &remote_errno
);
12851 ps
= m_features
.packet_support (PACKET_vFile_open
);
12854 if (ps
== PACKET_DISABLE
)
12856 static int warning_issued
= 0;
12858 if (!warning_issued
)
12860 warning (_("remote target does not support file"
12861 " transfer, attempting to access files"
12862 " from local filesystem."));
12863 warning_issued
= 1;
12874 remote_hostio_error (fileio_error errnum
)
12876 int host_error
= fileio_error_to_host (errnum
);
12878 if (host_error
== -1)
12879 error (_("Unknown remote I/O error %d"), errnum
);
12881 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
12884 /* A RAII wrapper around a remote file descriptor. */
12886 class scoped_remote_fd
12889 scoped_remote_fd (remote_target
*remote
, int fd
)
12890 : m_remote (remote
), m_fd (fd
)
12894 ~scoped_remote_fd ()
12900 fileio_error remote_errno
;
12901 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
12905 /* Swallow exception before it escapes the dtor. If
12906 something goes wrong, likely the connection is gone,
12907 and there's nothing else that can be done. */
12912 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
12914 /* Release ownership of the file descriptor, and return it. */
12915 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
12922 /* Return the owned file descriptor. */
12923 int get () const noexcept
12929 /* The remote target. */
12930 remote_target
*m_remote
;
12932 /* The owned remote I/O file descriptor. */
12937 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
12939 remote_target
*remote
= get_current_remote_target ();
12941 if (remote
== nullptr)
12942 error (_("command can only be used with remote target"));
12944 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
12948 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
12951 int retcode
, bytes
, io_size
;
12952 fileio_error remote_errno
;
12953 int bytes_in_buffer
;
12957 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
12959 perror_with_name (local_file
);
12961 scoped_remote_fd fd
12962 (this, remote_hostio_open (NULL
,
12963 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
12965 0700, 0, &remote_errno
));
12966 if (fd
.get () == -1)
12967 remote_hostio_error (remote_errno
);
12969 /* Send up to this many bytes at once. They won't all fit in the
12970 remote packet limit, so we'll transfer slightly fewer. */
12971 io_size
= get_remote_packet_size ();
12972 gdb::byte_vector
buffer (io_size
);
12974 bytes_in_buffer
= 0;
12977 while (bytes_in_buffer
|| !saw_eof
)
12981 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
12982 io_size
- bytes_in_buffer
,
12986 if (ferror (file
.get ()))
12987 error (_("Error reading %s."), local_file
);
12990 /* EOF. Unless there is something still in the
12991 buffer from the last iteration, we are done. */
12993 if (bytes_in_buffer
== 0)
13001 bytes
+= bytes_in_buffer
;
13002 bytes_in_buffer
= 0;
13004 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
13005 offset
, &remote_errno
);
13008 remote_hostio_error (remote_errno
);
13009 else if (retcode
== 0)
13010 error (_("Remote write of %d bytes returned 0!"), bytes
);
13011 else if (retcode
< bytes
)
13013 /* Short write. Save the rest of the read data for the next
13015 bytes_in_buffer
= bytes
- retcode
;
13016 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
13022 if (remote_hostio_close (fd
.release (), &remote_errno
))
13023 remote_hostio_error (remote_errno
);
13026 gdb_printf (_("Successfully sent file \"%s\".\n"), local_file
);
13030 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
13032 remote_target
*remote
= get_current_remote_target ();
13034 if (remote
== nullptr)
13035 error (_("command can only be used with remote target"));
13037 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
13041 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
13044 fileio_error remote_errno
;
13045 int bytes
, io_size
;
13048 scoped_remote_fd fd
13049 (this, remote_hostio_open (NULL
,
13050 remote_file
, FILEIO_O_RDONLY
, 0, 0,
13052 if (fd
.get () == -1)
13053 remote_hostio_error (remote_errno
);
13055 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
13057 perror_with_name (local_file
);
13059 /* Send up to this many bytes at once. They won't all fit in the
13060 remote packet limit, so we'll transfer slightly fewer. */
13061 io_size
= get_remote_packet_size ();
13062 gdb::byte_vector
buffer (io_size
);
13067 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
13070 /* Success, but no bytes, means end-of-file. */
13073 remote_hostio_error (remote_errno
);
13077 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
13079 perror_with_name (local_file
);
13082 if (remote_hostio_close (fd
.release (), &remote_errno
))
13083 remote_hostio_error (remote_errno
);
13086 gdb_printf (_("Successfully fetched file \"%s\".\n"), remote_file
);
13090 remote_file_delete (const char *remote_file
, int from_tty
)
13092 remote_target
*remote
= get_current_remote_target ();
13094 if (remote
== nullptr)
13095 error (_("command can only be used with remote target"));
13097 remote
->remote_file_delete (remote_file
, from_tty
);
13101 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
13104 fileio_error remote_errno
;
13106 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
13108 remote_hostio_error (remote_errno
);
13111 gdb_printf (_("Successfully deleted file \"%s\".\n"), remote_file
);
13115 remote_put_command (const char *args
, int from_tty
)
13118 error_no_arg (_("file to put"));
13120 gdb_argv
argv (args
);
13121 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13122 error (_("Invalid parameters to remote put"));
13124 remote_file_put (argv
[0], argv
[1], from_tty
);
13128 remote_get_command (const char *args
, int from_tty
)
13131 error_no_arg (_("file to get"));
13133 gdb_argv
argv (args
);
13134 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13135 error (_("Invalid parameters to remote get"));
13137 remote_file_get (argv
[0], argv
[1], from_tty
);
13141 remote_delete_command (const char *args
, int from_tty
)
13144 error_no_arg (_("file to delete"));
13146 gdb_argv
argv (args
);
13147 if (argv
[0] == NULL
|| argv
[1] != NULL
)
13148 error (_("Invalid parameters to remote delete"));
13150 remote_file_delete (argv
[0], from_tty
);
13154 remote_target::can_execute_reverse ()
13156 if (m_features
.packet_support (PACKET_bs
) == PACKET_ENABLE
13157 || m_features
.packet_support (PACKET_bc
) == PACKET_ENABLE
)
13164 remote_target::supports_non_stop ()
13170 remote_target::supports_disable_randomization ()
13172 /* Only supported in extended mode. */
13177 remote_target::supports_multi_process ()
13179 return m_features
.remote_multi_process_p ();
13183 remote_target::remote_supports_cond_tracepoints ()
13185 return (m_features
.packet_support (PACKET_ConditionalTracepoints
)
13190 remote_target::supports_evaluation_of_breakpoint_conditions ()
13192 return (m_features
.packet_support (PACKET_ConditionalBreakpoints
)
13197 remote_target::remote_supports_fast_tracepoints ()
13199 return m_features
.packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13203 remote_target::remote_supports_static_tracepoints ()
13205 return m_features
.packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13209 remote_target::remote_supports_install_in_trace ()
13211 return m_features
.packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13215 remote_target::supports_enable_disable_tracepoint ()
13217 return (m_features
.packet_support (PACKET_EnableDisableTracepoints_feature
)
13222 remote_target::supports_string_tracing ()
13224 return m_features
.packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13228 remote_target::can_run_breakpoint_commands ()
13230 return m_features
.packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13234 remote_target::trace_init ()
13236 struct remote_state
*rs
= get_remote_state ();
13239 remote_get_noisy_reply ();
13240 if (strcmp (rs
->buf
.data (), "OK") != 0)
13241 error (_("Target does not support this command."));
13244 /* Recursive routine to walk through command list including loops, and
13245 download packets for each command. */
13248 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13249 struct command_line
*cmds
)
13251 struct remote_state
*rs
= get_remote_state ();
13252 struct command_line
*cmd
;
13254 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13256 QUIT
; /* Allow user to bail out with ^C. */
13257 strcpy (rs
->buf
.data (), "QTDPsrc:");
13258 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13259 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13260 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13262 remote_get_noisy_reply ();
13263 if (strcmp (rs
->buf
.data (), "OK"))
13264 warning (_("Target does not support source download."));
13266 if (cmd
->control_type
== while_control
13267 || cmd
->control_type
== while_stepping_control
)
13269 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13271 QUIT
; /* Allow user to bail out with ^C. */
13272 strcpy (rs
->buf
.data (), "QTDPsrc:");
13273 encode_source_string (num
, addr
, "cmd", "end",
13274 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13275 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13277 remote_get_noisy_reply ();
13278 if (strcmp (rs
->buf
.data (), "OK"))
13279 warning (_("Target does not support source download."));
13285 remote_target::download_tracepoint (struct bp_location
*loc
)
13289 std::vector
<std::string
> tdp_actions
;
13290 std::vector
<std::string
> stepping_actions
;
13292 struct breakpoint
*b
= loc
->owner
;
13293 struct tracepoint
*t
= (struct tracepoint
*) b
;
13294 struct remote_state
*rs
= get_remote_state ();
13296 const char *err_msg
= _("Tracepoint packet too large for target.");
13299 /* We use a buffer other than rs->buf because we'll build strings
13300 across multiple statements, and other statements in between could
13302 gdb::char_vector
buf (get_remote_packet_size ());
13304 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13306 tpaddr
= loc
->address
;
13307 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13308 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13309 b
->number
, addrbuf
, /* address */
13310 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13311 t
->step_count
, t
->pass_count
);
13313 if (ret
< 0 || ret
>= buf
.size ())
13314 error ("%s", err_msg
);
13316 /* Fast tracepoints are mostly handled by the target, but we can
13317 tell the target how big of an instruction block should be moved
13319 if (b
->type
== bp_fast_tracepoint
)
13321 /* Only test for support at download time; we may not know
13322 target capabilities at definition time. */
13323 if (remote_supports_fast_tracepoints ())
13325 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13328 size_left
= buf
.size () - strlen (buf
.data ());
13329 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13331 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13333 if (ret
< 0 || ret
>= size_left
)
13334 error ("%s", err_msg
);
13337 /* If it passed validation at definition but fails now,
13338 something is very wrong. */
13339 internal_error (_("Fast tracepoint not valid during download"));
13342 /* Fast tracepoints are functionally identical to regular
13343 tracepoints, so don't take lack of support as a reason to
13344 give up on the trace run. */
13345 warning (_("Target does not support fast tracepoints, "
13346 "downloading %d as regular tracepoint"), b
->number
);
13348 else if (b
->type
== bp_static_tracepoint
13349 || b
->type
== bp_static_marker_tracepoint
)
13351 /* Only test for support at download time; we may not know
13352 target capabilities at definition time. */
13353 if (remote_supports_static_tracepoints ())
13355 struct static_tracepoint_marker marker
;
13357 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13359 size_left
= buf
.size () - strlen (buf
.data ());
13360 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13363 if (ret
< 0 || ret
>= size_left
)
13364 error ("%s", err_msg
);
13367 error (_("Static tracepoint not valid during download"));
13370 /* Fast tracepoints are functionally identical to regular
13371 tracepoints, so don't take lack of support as a reason
13372 to give up on the trace run. */
13373 error (_("Target does not support static tracepoints"));
13375 /* If the tracepoint has a conditional, make it into an agent
13376 expression and append to the definition. */
13379 /* Only test support at download time, we may not know target
13380 capabilities at definition time. */
13381 if (remote_supports_cond_tracepoints ())
13383 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13386 size_left
= buf
.size () - strlen (buf
.data ());
13388 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13389 size_left
, ":X%x,", aexpr
->len
);
13391 if (ret
< 0 || ret
>= size_left
)
13392 error ("%s", err_msg
);
13394 size_left
= buf
.size () - strlen (buf
.data ());
13396 /* Two bytes to encode each aexpr byte, plus the terminating
13398 if (aexpr
->len
* 2 + 1 > size_left
)
13399 error ("%s", err_msg
);
13401 pkt
= buf
.data () + strlen (buf
.data ());
13403 for (int ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
13404 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13408 warning (_("Target does not support conditional tracepoints, "
13409 "ignoring tp %d cond"), b
->number
);
13412 if (b
->commands
|| !default_collect
.empty ())
13414 size_left
= buf
.size () - strlen (buf
.data ());
13416 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13419 if (ret
< 0 || ret
>= size_left
)
13420 error ("%s", err_msg
);
13423 putpkt (buf
.data ());
13424 remote_get_noisy_reply ();
13425 if (strcmp (rs
->buf
.data (), "OK"))
13426 error (_("Target does not support tracepoints."));
13428 /* do_single_steps (t); */
13429 for (auto action_it
= tdp_actions
.begin ();
13430 action_it
!= tdp_actions
.end (); action_it
++)
13432 QUIT
; /* Allow user to bail out with ^C. */
13434 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13435 || !stepping_actions
.empty ());
13437 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13438 b
->number
, addrbuf
, /* address */
13439 action_it
->c_str (),
13440 has_more
? '-' : 0);
13442 if (ret
< 0 || ret
>= buf
.size ())
13443 error ("%s", err_msg
);
13445 putpkt (buf
.data ());
13446 remote_get_noisy_reply ();
13447 if (strcmp (rs
->buf
.data (), "OK"))
13448 error (_("Error on target while setting tracepoints."));
13451 for (auto action_it
= stepping_actions
.begin ();
13452 action_it
!= stepping_actions
.end (); action_it
++)
13454 QUIT
; /* Allow user to bail out with ^C. */
13456 bool is_first
= action_it
== stepping_actions
.begin ();
13457 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13459 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13460 b
->number
, addrbuf
, /* address */
13461 is_first
? "S" : "",
13462 action_it
->c_str (),
13463 has_more
? "-" : "");
13465 if (ret
< 0 || ret
>= buf
.size ())
13466 error ("%s", err_msg
);
13468 putpkt (buf
.data ());
13469 remote_get_noisy_reply ();
13470 if (strcmp (rs
->buf
.data (), "OK"))
13471 error (_("Error on target while setting tracepoints."));
13474 if (m_features
.packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13476 if (b
->locspec
!= nullptr)
13478 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13480 if (ret
< 0 || ret
>= buf
.size ())
13481 error ("%s", err_msg
);
13483 const char *str
= b
->locspec
->to_string ();
13484 encode_source_string (b
->number
, loc
->address
, "at", str
,
13485 buf
.data () + strlen (buf
.data ()),
13486 buf
.size () - strlen (buf
.data ()));
13487 putpkt (buf
.data ());
13488 remote_get_noisy_reply ();
13489 if (strcmp (rs
->buf
.data (), "OK"))
13490 warning (_("Target does not support source download."));
13492 if (b
->cond_string
)
13494 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13496 if (ret
< 0 || ret
>= buf
.size ())
13497 error ("%s", err_msg
);
13499 encode_source_string (b
->number
, loc
->address
,
13500 "cond", b
->cond_string
.get (),
13501 buf
.data () + strlen (buf
.data ()),
13502 buf
.size () - strlen (buf
.data ()));
13503 putpkt (buf
.data ());
13504 remote_get_noisy_reply ();
13505 if (strcmp (rs
->buf
.data (), "OK"))
13506 warning (_("Target does not support source download."));
13508 remote_download_command_source (b
->number
, loc
->address
,
13509 breakpoint_commands (b
));
13514 remote_target::can_download_tracepoint ()
13516 struct remote_state
*rs
= get_remote_state ();
13517 struct trace_status
*ts
;
13520 /* Don't try to install tracepoints until we've relocated our
13521 symbols, and fetched and merged the target's tracepoint list with
13523 if (rs
->starting_up
)
13526 ts
= current_trace_status ();
13527 status
= get_trace_status (ts
);
13529 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13532 /* If we are in a tracing experiment, but remote stub doesn't support
13533 installing tracepoint in trace, we have to return. */
13534 if (!remote_supports_install_in_trace ())
13542 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13544 struct remote_state
*rs
= get_remote_state ();
13547 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13548 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13550 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13551 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13552 >= get_remote_packet_size ())
13553 error (_("Trace state variable name too long for tsv definition packet"));
13554 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13557 remote_get_noisy_reply ();
13558 if (rs
->buf
[0] == '\0')
13559 error (_("Target does not support this command."));
13560 if (strcmp (rs
->buf
.data (), "OK") != 0)
13561 error (_("Error on target while downloading trace state variable."));
13565 remote_target::enable_tracepoint (struct bp_location
*location
)
13567 struct remote_state
*rs
= get_remote_state ();
13569 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
13570 location
->owner
->number
,
13571 phex (location
->address
, sizeof (CORE_ADDR
)));
13573 remote_get_noisy_reply ();
13574 if (rs
->buf
[0] == '\0')
13575 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
13576 if (strcmp (rs
->buf
.data (), "OK") != 0)
13577 error (_("Error on target while enabling tracepoint."));
13581 remote_target::disable_tracepoint (struct bp_location
*location
)
13583 struct remote_state
*rs
= get_remote_state ();
13585 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
13586 location
->owner
->number
,
13587 phex (location
->address
, sizeof (CORE_ADDR
)));
13589 remote_get_noisy_reply ();
13590 if (rs
->buf
[0] == '\0')
13591 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
13592 if (strcmp (rs
->buf
.data (), "OK") != 0)
13593 error (_("Error on target while disabling tracepoint."));
13597 remote_target::trace_set_readonly_regions ()
13600 bfd_size_type size
;
13604 bfd
*abfd
= current_program_space
->exec_bfd ();
13607 return; /* No information to give. */
13609 struct remote_state
*rs
= get_remote_state ();
13611 strcpy (rs
->buf
.data (), "QTro");
13612 offset
= strlen (rs
->buf
.data ());
13613 for (s
= abfd
->sections
; s
; s
= s
->next
)
13615 char tmp1
[40], tmp2
[40];
13618 if ((s
->flags
& SEC_LOAD
) == 0
13619 /* || (s->flags & SEC_CODE) == 0 */
13620 || (s
->flags
& SEC_READONLY
) == 0)
13624 vma
= bfd_section_vma (s
);
13625 size
= bfd_section_size (s
);
13626 bfd_sprintf_vma (abfd
, tmp1
, vma
);
13627 bfd_sprintf_vma (abfd
, tmp2
, vma
+ size
);
13628 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
13629 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
13631 if (m_features
.packet_support (PACKET_qXfer_traceframe_info
)
13634 Too many sections for read-only sections definition packet."));
13637 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
13639 offset
+= sec_length
;
13644 getpkt (&rs
->buf
, 0);
13649 remote_target::trace_start ()
13651 struct remote_state
*rs
= get_remote_state ();
13653 putpkt ("QTStart");
13654 remote_get_noisy_reply ();
13655 if (rs
->buf
[0] == '\0')
13656 error (_("Target does not support this command."));
13657 if (strcmp (rs
->buf
.data (), "OK") != 0)
13658 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13662 remote_target::get_trace_status (struct trace_status
*ts
)
13664 /* Initialize it just to avoid a GCC false warning. */
13666 enum packet_result result
;
13667 struct remote_state
*rs
= get_remote_state ();
13669 if (m_features
.packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
13672 /* FIXME we need to get register block size some other way. */
13673 trace_regblock_size
13674 = rs
->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet
;
13676 putpkt ("qTStatus");
13680 p
= remote_get_noisy_reply ();
13682 catch (const gdb_exception_error
&ex
)
13684 if (ex
.error
!= TARGET_CLOSE_ERROR
)
13686 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
13692 result
= m_features
.packet_ok (p
, PACKET_qTStatus
);
13694 /* If the remote target doesn't do tracing, flag it. */
13695 if (result
== PACKET_UNKNOWN
)
13698 /* We're working with a live target. */
13699 ts
->filename
= NULL
;
13702 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
13704 /* Function 'parse_trace_status' sets default value of each field of
13705 'ts' at first, so we don't have to do it here. */
13706 parse_trace_status (p
, ts
);
13708 return ts
->running
;
13712 remote_target::get_tracepoint_status (struct breakpoint
*bp
,
13713 struct uploaded_tp
*utp
)
13715 struct remote_state
*rs
= get_remote_state ();
13717 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
13718 size_t size
= get_remote_packet_size ();
13723 tp
->traceframe_usage
= 0;
13724 for (bp_location
&loc
: tp
->locations ())
13726 /* If the tracepoint was never downloaded, don't go asking for
13728 if (tp
->number_on_target
== 0)
13730 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
13731 phex_nz (loc
.address
, 0));
13733 reply
= remote_get_noisy_reply ();
13734 if (reply
&& *reply
)
13737 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13743 utp
->hit_count
= 0;
13744 utp
->traceframe_usage
= 0;
13745 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
13746 phex_nz (utp
->addr
, 0));
13748 reply
= remote_get_noisy_reply ();
13749 if (reply
&& *reply
)
13752 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13758 remote_target::trace_stop ()
13760 struct remote_state
*rs
= get_remote_state ();
13763 remote_get_noisy_reply ();
13764 if (rs
->buf
[0] == '\0')
13765 error (_("Target does not support this command."));
13766 if (strcmp (rs
->buf
.data (), "OK") != 0)
13767 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13771 remote_target::trace_find (enum trace_find_type type
, int num
,
13772 CORE_ADDR addr1
, CORE_ADDR addr2
,
13775 struct remote_state
*rs
= get_remote_state ();
13776 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
13778 int target_frameno
= -1, target_tracept
= -1;
13780 /* Lookups other than by absolute frame number depend on the current
13781 trace selected, so make sure it is correct on the remote end
13783 if (type
!= tfind_number
)
13784 set_remote_traceframe ();
13786 p
= rs
->buf
.data ();
13787 strcpy (p
, "QTFrame:");
13788 p
= strchr (p
, '\0');
13792 xsnprintf (p
, endbuf
- p
, "%x", num
);
13795 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
13798 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
13801 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
13802 phex_nz (addr2
, 0));
13804 case tfind_outside
:
13805 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
13806 phex_nz (addr2
, 0));
13809 error (_("Unknown trace find type %d"), type
);
13813 reply
= remote_get_noisy_reply ();
13814 if (*reply
== '\0')
13815 error (_("Target does not support this command."));
13817 while (reply
&& *reply
)
13822 target_frameno
= (int) strtol (p
, &reply
, 16);
13824 error (_("Unable to parse trace frame number"));
13825 /* Don't update our remote traceframe number cache on failure
13826 to select a remote traceframe. */
13827 if (target_frameno
== -1)
13832 target_tracept
= (int) strtol (p
, &reply
, 16);
13834 error (_("Unable to parse tracepoint number"));
13836 case 'O': /* "OK"? */
13837 if (reply
[1] == 'K' && reply
[2] == '\0')
13840 error (_("Bogus reply from target: %s"), reply
);
13843 error (_("Bogus reply from target: %s"), reply
);
13846 *tpp
= target_tracept
;
13848 rs
->remote_traceframe_number
= target_frameno
;
13849 return target_frameno
;
13853 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
13855 struct remote_state
*rs
= get_remote_state ();
13859 set_remote_traceframe ();
13861 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
13863 reply
= remote_get_noisy_reply ();
13864 if (reply
&& *reply
)
13868 unpack_varlen_hex (reply
+ 1, &uval
);
13869 *val
= (LONGEST
) uval
;
13877 remote_target::save_trace_data (const char *filename
)
13879 struct remote_state
*rs
= get_remote_state ();
13882 p
= rs
->buf
.data ();
13883 strcpy (p
, "QTSave:");
13885 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
13886 >= get_remote_packet_size ())
13887 error (_("Remote file name too long for trace save packet"));
13888 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
13891 reply
= remote_get_noisy_reply ();
13892 if (*reply
== '\0')
13893 error (_("Target does not support this command."));
13894 if (strcmp (reply
, "OK") != 0)
13895 error (_("Bogus reply from target: %s"), reply
);
13899 /* This is basically a memory transfer, but needs to be its own packet
13900 because we don't know how the target actually organizes its trace
13901 memory, plus we want to be able to ask for as much as possible, but
13902 not be unhappy if we don't get as much as we ask for. */
13905 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
13907 struct remote_state
*rs
= get_remote_state ();
13912 p
= rs
->buf
.data ();
13913 strcpy (p
, "qTBuffer:");
13915 p
+= hexnumstr (p
, offset
);
13917 p
+= hexnumstr (p
, len
);
13921 reply
= remote_get_noisy_reply ();
13922 if (reply
&& *reply
)
13924 /* 'l' by itself means we're at the end of the buffer and
13925 there is nothing more to get. */
13929 /* Convert the reply into binary. Limit the number of bytes to
13930 convert according to our passed-in buffer size, rather than
13931 what was returned in the packet; if the target is
13932 unexpectedly generous and gives us a bigger reply than we
13933 asked for, we don't want to crash. */
13934 rslt
= hex2bin (reply
, buf
, len
);
13938 /* Something went wrong, flag as an error. */
13943 remote_target::set_disconnected_tracing (int val
)
13945 struct remote_state
*rs
= get_remote_state ();
13947 if (m_features
.packet_support (PACKET_DisconnectedTracing_feature
)
13952 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13953 "QTDisconnected:%x", val
);
13955 reply
= remote_get_noisy_reply ();
13956 if (*reply
== '\0')
13957 error (_("Target does not support this command."));
13958 if (strcmp (reply
, "OK") != 0)
13959 error (_("Bogus reply from target: %s"), reply
);
13962 warning (_("Target does not support disconnected tracing."));
13966 remote_target::core_of_thread (ptid_t ptid
)
13968 thread_info
*info
= this->find_thread (ptid
);
13970 if (info
!= NULL
&& info
->priv
!= NULL
)
13971 return get_remote_thread_info (info
)->core
;
13977 remote_target::set_circular_trace_buffer (int val
)
13979 struct remote_state
*rs
= get_remote_state ();
13982 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13983 "QTBuffer:circular:%x", val
);
13985 reply
= remote_get_noisy_reply ();
13986 if (*reply
== '\0')
13987 error (_("Target does not support this command."));
13988 if (strcmp (reply
, "OK") != 0)
13989 error (_("Bogus reply from target: %s"), reply
);
13993 remote_target::traceframe_info ()
13995 gdb::optional
<gdb::char_vector
> text
13996 = target_read_stralloc (current_inferior ()->top_target (),
13997 TARGET_OBJECT_TRACEFRAME_INFO
,
14000 return parse_traceframe_info (text
->data ());
14005 /* Handle the qTMinFTPILen packet. Returns the minimum length of
14006 instruction on which a fast tracepoint may be placed. Returns -1
14007 if the packet is not supported, and 0 if the minimum instruction
14008 length is unknown. */
14011 remote_target::get_min_fast_tracepoint_insn_len ()
14013 struct remote_state
*rs
= get_remote_state ();
14016 /* If we're not debugging a process yet, the IPA can't be
14018 if (!target_has_execution ())
14021 /* Make sure the remote is pointing at the right process. */
14022 set_general_process ();
14024 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
14026 reply
= remote_get_noisy_reply ();
14027 if (*reply
== '\0')
14031 ULONGEST min_insn_len
;
14033 unpack_varlen_hex (reply
, &min_insn_len
);
14035 return (int) min_insn_len
;
14040 remote_target::set_trace_buffer_size (LONGEST val
)
14042 if (m_features
.packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
14044 struct remote_state
*rs
= get_remote_state ();
14045 char *buf
= rs
->buf
.data ();
14046 char *endbuf
= buf
+ get_remote_packet_size ();
14047 enum packet_result result
;
14049 gdb_assert (val
>= 0 || val
== -1);
14050 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
14051 /* Send -1 as literal "-1" to avoid host size dependency. */
14055 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
14058 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
14061 remote_get_noisy_reply ();
14062 result
= m_features
.packet_ok (rs
->buf
, PACKET_QTBuffer_size
);
14064 if (result
!= PACKET_OK
)
14065 warning (_("Bogus reply from target: %s"), rs
->buf
.data ());
14070 remote_target::set_trace_notes (const char *user
, const char *notes
,
14071 const char *stop_notes
)
14073 struct remote_state
*rs
= get_remote_state ();
14075 char *buf
= rs
->buf
.data ();
14076 char *endbuf
= buf
+ get_remote_packet_size ();
14079 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
14082 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
14083 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
14089 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
14090 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
14096 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
14097 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
14101 /* Ensure the buffer is terminated. */
14105 reply
= remote_get_noisy_reply ();
14106 if (*reply
== '\0')
14109 if (strcmp (reply
, "OK") != 0)
14110 error (_("Bogus reply from target: %s"), reply
);
14116 remote_target::use_agent (bool use
)
14118 if (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
14120 struct remote_state
*rs
= get_remote_state ();
14122 /* If the stub supports QAgent. */
14123 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
14125 getpkt (&rs
->buf
, 0);
14127 if (strcmp (rs
->buf
.data (), "OK") == 0)
14138 remote_target::can_use_agent ()
14140 return (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
14143 struct btrace_target_info
14145 /* The ptid of the traced thread. */
14148 /* The obtained branch trace configuration. */
14149 struct btrace_config conf
;
14152 /* Reset our idea of our target's btrace configuration. */
14155 remote_btrace_reset (remote_state
*rs
)
14157 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14160 /* Synchronize the configuration with the target. */
14163 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14165 struct remote_state
*rs
;
14166 char *buf
, *pos
, *endbuf
;
14168 rs
= get_remote_state ();
14169 buf
= rs
->buf
.data ();
14170 endbuf
= buf
+ get_remote_packet_size ();
14172 if (m_features
.packet_support (PACKET_Qbtrace_conf_bts_size
) == PACKET_ENABLE
14173 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14176 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14177 packets_descriptions
[PACKET_Qbtrace_conf_bts_size
].name
,
14181 getpkt (&rs
->buf
, 0);
14183 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_bts_size
)
14186 if (buf
[0] == 'E' && buf
[1] == '.')
14187 error (_("Failed to configure the BTS buffer size: %s"), buf
+ 2);
14189 error (_("Failed to configure the BTS buffer size."));
14192 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14195 if (m_features
.packet_support (PACKET_Qbtrace_conf_pt_size
) == PACKET_ENABLE
14196 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14199 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14200 packets_descriptions
[PACKET_Qbtrace_conf_pt_size
].name
,
14204 getpkt (&rs
->buf
, 0);
14206 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_size
)
14209 if (buf
[0] == 'E' && buf
[1] == '.')
14210 error (_("Failed to configure the trace buffer size: %s"), buf
+ 2);
14212 error (_("Failed to configure the trace buffer size."));
14215 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14219 /* Read TP's btrace configuration from the target and store it into CONF. */
14222 btrace_read_config (thread_info
*tp
, struct btrace_config
*conf
)
14224 /* target_read_stralloc relies on INFERIOR_PTID. */
14225 scoped_restore_current_thread restore_thread
;
14226 switch_to_thread (tp
);
14228 gdb::optional
<gdb::char_vector
> xml
14229 = target_read_stralloc (current_inferior ()->top_target (),
14230 TARGET_OBJECT_BTRACE_CONF
, "");
14232 parse_xml_btrace_conf (conf
, xml
->data ());
14235 /* Maybe reopen target btrace. */
14238 remote_target::remote_btrace_maybe_reopen ()
14240 struct remote_state
*rs
= get_remote_state ();
14241 int btrace_target_pushed
= 0;
14242 #if !defined (HAVE_LIBIPT)
14246 /* Don't bother walking the entirety of the remote thread list when
14247 we know the feature isn't supported by the remote. */
14248 if (m_features
.packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
14251 for (thread_info
*tp
: all_non_exited_threads (this))
14253 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
14254 btrace_read_config (tp
, &rs
->btrace_config
);
14256 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
14259 #if !defined (HAVE_LIBIPT)
14260 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
14265 warning (_("Target is recording using Intel Processor Trace "
14266 "but support was disabled at compile time."));
14271 #endif /* !defined (HAVE_LIBIPT) */
14273 /* Push target, once, but before anything else happens. This way our
14274 changes to the threads will be cleaned up by unpushing the target
14275 in case btrace_read_config () throws. */
14276 if (!btrace_target_pushed
)
14278 btrace_target_pushed
= 1;
14279 record_btrace_push_target ();
14280 gdb_printf (_("Target is recording using %s.\n"),
14281 btrace_format_string (rs
->btrace_config
.format
));
14284 tp
->btrace
.target
= XCNEW (struct btrace_target_info
);
14285 tp
->btrace
.target
->ptid
= tp
->ptid
;
14286 tp
->btrace
.target
->conf
= rs
->btrace_config
;
14290 /* Enable branch tracing. */
14292 struct btrace_target_info
*
14293 remote_target::enable_btrace (thread_info
*tp
,
14294 const struct btrace_config
*conf
)
14296 struct btrace_target_info
*tinfo
= NULL
;
14297 struct packet_config
*packet
= NULL
;
14298 struct remote_state
*rs
= get_remote_state ();
14299 char *buf
= rs
->buf
.data ();
14300 char *endbuf
= buf
+ get_remote_packet_size ();
14302 unsigned int which_packet
;
14303 switch (conf
->format
)
14305 case BTRACE_FORMAT_BTS
:
14306 which_packet
= PACKET_Qbtrace_bts
;
14308 case BTRACE_FORMAT_PT
:
14309 which_packet
= PACKET_Qbtrace_pt
;
14312 internal_error (_("Bad branch btrace format: %u."),
14313 (unsigned int) conf
->format
);
14316 packet
= &m_features
.m_protocol_packets
[which_packet
];
14317 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
14318 error (_("Target does not support branch tracing."));
14320 btrace_sync_conf (conf
);
14322 ptid_t ptid
= tp
->ptid
;
14323 set_general_thread (ptid
);
14325 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14326 packets_descriptions
[which_packet
].name
);
14328 getpkt (&rs
->buf
, 0);
14330 if (m_features
.packet_ok (rs
->buf
, which_packet
) == PACKET_ERROR
)
14332 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14333 error (_("Could not enable branch tracing for %s: %s"),
14334 target_pid_to_str (ptid
).c_str (), &rs
->buf
[2]);
14336 error (_("Could not enable branch tracing for %s."),
14337 target_pid_to_str (ptid
).c_str ());
14340 tinfo
= XCNEW (struct btrace_target_info
);
14341 tinfo
->ptid
= ptid
;
14343 /* If we fail to read the configuration, we lose some information, but the
14344 tracing itself is not impacted. */
14347 btrace_read_config (tp
, &tinfo
->conf
);
14349 catch (const gdb_exception_error
&err
)
14351 if (err
.message
!= NULL
)
14352 warning ("%s", err
.what ());
14358 /* Disable branch tracing. */
14361 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
14363 struct remote_state
*rs
= get_remote_state ();
14364 char *buf
= rs
->buf
.data ();
14365 char *endbuf
= buf
+ get_remote_packet_size ();
14367 if (m_features
.packet_support (PACKET_Qbtrace_off
) != PACKET_ENABLE
)
14368 error (_("Target does not support branch tracing."));
14370 set_general_thread (tinfo
->ptid
);
14372 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14373 packets_descriptions
[PACKET_Qbtrace_off
].name
);
14375 getpkt (&rs
->buf
, 0);
14377 if (m_features
.packet_ok (rs
->buf
, PACKET_Qbtrace_off
) == PACKET_ERROR
)
14379 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14380 error (_("Could not disable branch tracing for %s: %s"),
14381 target_pid_to_str (tinfo
->ptid
).c_str (), &rs
->buf
[2]);
14383 error (_("Could not disable branch tracing for %s."),
14384 target_pid_to_str (tinfo
->ptid
).c_str ());
14390 /* Teardown branch tracing. */
14393 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
14395 /* We must not talk to the target during teardown. */
14399 /* Read the branch trace. */
14402 remote_target::read_btrace (struct btrace_data
*btrace
,
14403 struct btrace_target_info
*tinfo
,
14404 enum btrace_read_type type
)
14408 if (m_features
.packet_support (PACKET_qXfer_btrace
) != PACKET_ENABLE
)
14409 error (_("Target does not support branch tracing."));
14411 #if !defined(HAVE_LIBEXPAT)
14412 error (_("Cannot process branch tracing result. XML parsing not supported."));
14417 case BTRACE_READ_ALL
:
14420 case BTRACE_READ_NEW
:
14423 case BTRACE_READ_DELTA
:
14427 internal_error (_("Bad branch tracing read type: %u."),
14428 (unsigned int) type
);
14431 gdb::optional
<gdb::char_vector
> xml
14432 = target_read_stralloc (current_inferior ()->top_target (),
14433 TARGET_OBJECT_BTRACE
, annex
);
14435 return BTRACE_ERR_UNKNOWN
;
14437 parse_xml_btrace (btrace
, xml
->data ());
14439 return BTRACE_ERR_NONE
;
14442 const struct btrace_config
*
14443 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
14445 return &tinfo
->conf
;
14449 remote_target::augmented_libraries_svr4_read ()
14452 (m_features
.packet_support (PACKET_augmented_libraries_svr4_read_feature
)
14456 /* Implementation of to_load. */
14459 remote_target::load (const char *name
, int from_tty
)
14461 generic_load (name
, from_tty
);
14464 /* Accepts an integer PID; returns a string representing a file that
14465 can be opened on the remote side to get the symbols for the child
14466 process. Returns NULL if the operation is not supported. */
14469 remote_target::pid_to_exec_file (int pid
)
14471 static gdb::optional
<gdb::char_vector
> filename
;
14472 char *annex
= NULL
;
14474 if (m_features
.packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
14477 inferior
*inf
= find_inferior_pid (this, pid
);
14479 internal_error (_("not currently attached to process %d"), pid
);
14481 if (!inf
->fake_pid_p
)
14483 const int annex_size
= 9;
14485 annex
= (char *) alloca (annex_size
);
14486 xsnprintf (annex
, annex_size
, "%x", pid
);
14489 filename
= target_read_stralloc (current_inferior ()->top_target (),
14490 TARGET_OBJECT_EXEC_FILE
, annex
);
14492 return filename
? filename
->data () : nullptr;
14495 /* Implement the to_can_do_single_step target_ops method. */
14498 remote_target::can_do_single_step ()
14500 /* We can only tell whether target supports single step or not by
14501 supported s and S vCont actions if the stub supports vContSupported
14502 feature. If the stub doesn't support vContSupported feature,
14503 we have conservatively to think target doesn't supports single
14505 if (m_features
.packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
14507 struct remote_state
*rs
= get_remote_state ();
14509 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
14515 /* Implementation of the to_execution_direction method for the remote
14518 enum exec_direction_kind
14519 remote_target::execution_direction ()
14521 struct remote_state
*rs
= get_remote_state ();
14523 return rs
->last_resume_exec_dir
;
14526 /* Return pointer to the thread_info struct which corresponds to
14527 THREAD_HANDLE (having length HANDLE_LEN). */
14530 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
14534 for (thread_info
*tp
: all_non_exited_threads (this))
14536 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14538 if (tp
->inf
== inf
&& priv
!= NULL
)
14540 if (handle_len
!= priv
->thread_handle
.size ())
14541 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
14542 handle_len
, priv
->thread_handle
.size ());
14543 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
14553 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
14555 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14556 return priv
->thread_handle
;
14560 remote_target::can_async_p ()
14562 /* This flag should be checked in the common target.c code. */
14563 gdb_assert (target_async_permitted
);
14565 /* We're async whenever the serial device can. */
14566 struct remote_state
*rs
= get_remote_state ();
14567 return serial_can_async_p (rs
->remote_desc
);
14571 remote_target::is_async_p ()
14573 /* We're async whenever the serial device is. */
14574 struct remote_state
*rs
= get_remote_state ();
14575 return serial_is_async_p (rs
->remote_desc
);
14578 /* Pass the SERIAL event on and up to the client. One day this code
14579 will be able to delay notifying the client of an event until the
14580 point where an entire packet has been received. */
14582 static serial_event_ftype remote_async_serial_handler
;
14585 remote_async_serial_handler (struct serial
*scb
, void *context
)
14587 /* Don't propogate error information up to the client. Instead let
14588 the client find out about the error by querying the target. */
14589 inferior_event_handler (INF_REG_EVENT
);
14593 remote_async_inferior_event_handler (gdb_client_data data
)
14595 inferior_event_handler (INF_REG_EVENT
);
14599 remote_target::async_wait_fd ()
14601 struct remote_state
*rs
= get_remote_state ();
14602 return rs
->remote_desc
->fd
;
14606 remote_target::async (bool enable
)
14608 struct remote_state
*rs
= get_remote_state ();
14612 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
14614 /* If there are pending events in the stop reply queue tell the
14615 event loop to process them. */
14616 if (!rs
->stop_reply_queue
.empty ())
14617 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
14618 /* For simplicity, below we clear the pending events token
14619 without remembering whether it is marked, so here we always
14620 mark it. If there's actually no pending notification to
14621 process, this ends up being a no-op (other than a spurious
14622 event-loop wakeup). */
14623 if (target_is_non_stop_p ())
14624 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14628 serial_async (rs
->remote_desc
, NULL
, NULL
);
14629 /* If the core is disabling async, it doesn't want to be
14630 disturbed with target events. Clear all async event sources
14632 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
14633 if (target_is_non_stop_p ())
14634 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14638 /* Implementation of the to_thread_events method. */
14641 remote_target::thread_events (int enable
)
14643 struct remote_state
*rs
= get_remote_state ();
14644 size_t size
= get_remote_packet_size ();
14646 if (m_features
.packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
14649 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
14651 getpkt (&rs
->buf
, 0);
14653 switch (m_features
.packet_ok (rs
->buf
, PACKET_QThreadEvents
))
14656 if (strcmp (rs
->buf
.data (), "OK") != 0)
14657 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
14660 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
14662 case PACKET_UNKNOWN
:
14668 show_remote_cmd (const char *args
, int from_tty
)
14670 /* We can't just use cmd_show_list here, because we want to skip
14671 the redundant "show remote Z-packet" and the legacy aliases. */
14672 struct cmd_list_element
*list
= remote_show_cmdlist
;
14673 struct ui_out
*uiout
= current_uiout
;
14675 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
14676 for (; list
!= NULL
; list
= list
->next
)
14677 if (strcmp (list
->name
, "Z-packet") == 0)
14679 else if (list
->type
== not_set_cmd
)
14680 /* Alias commands are exactly like the original, except they
14681 don't have the normal type. */
14685 ui_out_emit_tuple
option_emitter (uiout
, "option");
14687 uiout
->field_string ("name", list
->name
);
14688 uiout
->text (": ");
14689 if (list
->type
== show_cmd
)
14690 do_show_command (NULL
, from_tty
, list
);
14692 cmd_func (list
, NULL
, from_tty
);
14697 /* Function to be called whenever a new objfile (shlib) is detected. */
14699 remote_new_objfile (struct objfile
*objfile
)
14701 /* The objfile change happened in that program space. */
14702 program_space
*pspace
= current_program_space
;
14704 /* The affected program space is possibly shared by multiple inferiors.
14705 Consider sending a qSymbol packet for each of the inferiors using that
14707 for (inferior
*inf
: all_inferiors ())
14709 if (inf
->pspace
!= pspace
)
14712 /* Check whether the inferior's process target is a remote target. */
14713 remote_target
*remote
= as_remote_target (inf
->process_target ());
14714 if (remote
== nullptr)
14717 /* When we are attaching or handling a fork child and the shared library
14718 subsystem reads the list of loaded libraries, we receive new objfile
14719 events in between each found library. The libraries are read in an
14720 undefined order, so if we gave the remote side a chance to look up
14721 symbols between each objfile, we might give it an inconsistent picture
14722 of the inferior. It could appear that a library A appears loaded but
14723 a library B does not, even though library A requires library B. That
14724 would present a state that couldn't normally exist in the inferior.
14726 So, skip these events, we'll give the remote a chance to look up
14727 symbols once all the loaded libraries and their symbols are known to
14729 if (inf
->in_initial_library_scan
)
14732 if (!remote
->has_execution (inf
))
14735 /* Need to switch to a specific thread, because remote_check_symbols will
14736 set the general thread using INFERIOR_PTID.
14738 It's possible to have inferiors with no thread here, because we are
14739 called very early in the connection process, while the inferior is
14740 being set up, before threads are added. Just skip it, start_remote_1
14741 also calls remote_check_symbols when it's done setting things up. */
14742 thread_info
*thread
= any_thread_of_inferior (inf
);
14743 if (thread
!= nullptr)
14745 scoped_restore_current_thread restore_thread
;
14746 switch_to_thread (thread
);
14747 remote
->remote_check_symbols ();
14752 /* Pull all the tracepoints defined on the target and create local
14753 data structures representing them. We don't want to create real
14754 tracepoints yet, we don't want to mess up the user's existing
14758 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
14760 struct remote_state
*rs
= get_remote_state ();
14763 /* Ask for a first packet of tracepoint definition. */
14765 getpkt (&rs
->buf
, 0);
14766 p
= rs
->buf
.data ();
14767 while (*p
&& *p
!= 'l')
14769 parse_tracepoint_definition (p
, utpp
);
14770 /* Ask for another packet of tracepoint definition. */
14772 getpkt (&rs
->buf
, 0);
14773 p
= rs
->buf
.data ();
14779 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
14781 struct remote_state
*rs
= get_remote_state ();
14784 /* Ask for a first packet of variable definition. */
14786 getpkt (&rs
->buf
, 0);
14787 p
= rs
->buf
.data ();
14788 while (*p
&& *p
!= 'l')
14790 parse_tsv_definition (p
, utsvp
);
14791 /* Ask for another packet of variable definition. */
14793 getpkt (&rs
->buf
, 0);
14794 p
= rs
->buf
.data ();
14799 /* The "set/show range-stepping" show hook. */
14802 show_range_stepping (struct ui_file
*file
, int from_tty
,
14803 struct cmd_list_element
*c
,
14807 _("Debugger's willingness to use range stepping "
14808 "is %s.\n"), value
);
14811 /* Return true if the vCont;r action is supported by the remote
14815 remote_target::vcont_r_supported ()
14817 return (m_features
.packet_support (PACKET_vCont
) == PACKET_ENABLE
14818 && get_remote_state ()->supports_vCont
.r
);
14821 /* The "set/show range-stepping" set hook. */
14824 set_range_stepping (const char *ignore_args
, int from_tty
,
14825 struct cmd_list_element
*c
)
14827 /* When enabling, check whether range stepping is actually supported
14828 by the target, and warn if not. */
14829 if (use_range_stepping
)
14831 remote_target
*remote
= get_current_remote_target ();
14833 || !remote
->vcont_r_supported ())
14834 warning (_("Range stepping is not supported by the current target"));
14839 show_remote_debug (struct ui_file
*file
, int from_tty
,
14840 struct cmd_list_element
*c
, const char *value
)
14842 gdb_printf (file
, _("Debugging of remote protocol is %s.\n"),
14847 show_remote_timeout (struct ui_file
*file
, int from_tty
,
14848 struct cmd_list_element
*c
, const char *value
)
14851 _("Timeout limit to wait for target to respond is %s.\n"),
14855 /* Implement the "supports_memory_tagging" target_ops method. */
14858 remote_target::supports_memory_tagging ()
14860 return m_features
.remote_memory_tagging_p ();
14863 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
14866 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14867 size_t len
, int type
)
14869 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14871 std::string request
= string_printf ("qMemTags:%s,%s:%s",
14872 phex_nz (address
, addr_size
),
14873 phex_nz (len
, sizeof (len
)),
14874 phex_nz (type
, sizeof (type
)));
14876 strcpy (packet
.data (), request
.c_str ());
14879 /* Parse the qMemTags packet reply into TAGS.
14881 Return true if successful, false otherwise. */
14884 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
14885 gdb::byte_vector
&tags
)
14887 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
14890 /* Copy the tag data. */
14891 tags
= hex2bin (reply
.data () + 1);
14896 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
14899 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14900 size_t len
, int type
,
14901 const gdb::byte_vector
&tags
)
14903 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14905 /* Put together the main packet, address and length. */
14906 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
14907 phex_nz (address
, addr_size
),
14908 phex_nz (len
, sizeof (len
)),
14909 phex_nz (type
, sizeof (type
)));
14910 request
+= bin2hex (tags
.data (), tags
.size ());
14912 /* Check if we have exceeded the maximum packet size. */
14913 if (packet
.size () < request
.length ())
14914 error (_("Contents too big for packet QMemTags."));
14916 strcpy (packet
.data (), request
.c_str ());
14919 /* Implement the "fetch_memtags" target_ops method. */
14922 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
14923 gdb::byte_vector
&tags
, int type
)
14925 /* Make sure the qMemTags packet is supported. */
14926 if (!m_features
.remote_memory_tagging_p ())
14927 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
14929 struct remote_state
*rs
= get_remote_state ();
14931 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
14934 getpkt (&rs
->buf
, 0);
14936 return parse_fetch_memtags_reply (rs
->buf
, tags
);
14939 /* Implement the "store_memtags" target_ops method. */
14942 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
14943 const gdb::byte_vector
&tags
, int type
)
14945 /* Make sure the QMemTags packet is supported. */
14946 if (!m_features
.remote_memory_tagging_p ())
14947 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
14949 struct remote_state
*rs
= get_remote_state ();
14951 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
14954 getpkt (&rs
->buf
, 0);
14956 /* Verify if the request was successful. */
14957 return packet_check_result (rs
->buf
.data ()) == PACKET_OK
;
14960 /* Return true if remote target T is non-stop. */
14963 remote_target_is_non_stop_p (remote_target
*t
)
14965 scoped_restore_current_thread restore_thread
;
14966 switch_to_target_no_thread (t
);
14968 return target_is_non_stop_p ();
14973 namespace selftests
{
14976 test_memory_tagging_functions ()
14978 remote_target remote
;
14980 struct packet_config
*config
14981 = &remote
.m_features
.m_protocol_packets
[PACKET_memory_tagging_feature
];
14983 scoped_restore restore_memtag_support_
14984 = make_scoped_restore (&config
->support
);
14986 /* Test memory tagging packet support. */
14987 config
->support
= PACKET_SUPPORT_UNKNOWN
;
14988 SELF_CHECK (remote
.supports_memory_tagging () == false);
14989 config
->support
= PACKET_DISABLE
;
14990 SELF_CHECK (remote
.supports_memory_tagging () == false);
14991 config
->support
= PACKET_ENABLE
;
14992 SELF_CHECK (remote
.supports_memory_tagging () == true);
14994 /* Setup testing. */
14995 gdb::char_vector packet
;
14996 gdb::byte_vector tags
, bv
;
14997 std::string expected
, reply
;
14998 packet
.resize (32000);
15000 /* Test creating a qMemTags request. */
15002 expected
= "qMemTags:0,0:0";
15003 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
15004 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
15006 expected
= "qMemTags:deadbeef,10:1";
15007 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
15008 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
15010 /* Test parsing a qMemTags reply. */
15012 /* Error reply, tags vector unmodified. */
15014 strcpy (packet
.data (), reply
.c_str ());
15016 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
15017 SELF_CHECK (tags
.size () == 0);
15019 /* Valid reply, tags vector updated. */
15023 for (int i
= 0; i
< 5; i
++)
15026 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
15027 strcpy (packet
.data (), reply
.c_str ());
15029 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
15030 SELF_CHECK (tags
.size () == 5);
15032 for (int i
= 0; i
< 5; i
++)
15033 SELF_CHECK (tags
[i
] == i
);
15035 /* Test creating a QMemTags request. */
15037 /* Empty tag data. */
15039 expected
= "QMemTags:0,0:0:";
15040 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
15041 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15042 expected
.length ()) == 0);
15044 /* Non-empty tag data. */
15046 for (int i
= 0; i
< 5; i
++)
15047 tags
.push_back (i
);
15048 expected
= "QMemTags:deadbeef,ff:1:0001020304";
15049 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
15050 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15051 expected
.length ()) == 0);
15054 } // namespace selftests
15055 #endif /* GDB_SELF_TEST */
15057 void _initialize_remote ();
15059 _initialize_remote ()
15061 add_target (remote_target_info
, remote_target::open
);
15062 add_target (extended_remote_target_info
, extended_remote_target::open
);
15064 /* Hook into new objfile notification. */
15065 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
15068 init_remote_threadtests ();
15071 /* set/show remote ... */
15073 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
15074 Remote protocol specific variables.\n\
15075 Configure various remote-protocol specific variables such as\n\
15076 the packets being used."),
15077 &remote_set_cmdlist
,
15078 0 /* allow-unknown */, &setlist
);
15079 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
15080 Remote protocol specific variables.\n\
15081 Configure various remote-protocol specific variables such as\n\
15082 the packets being used."),
15083 &remote_show_cmdlist
,
15084 0 /* allow-unknown */, &showlist
);
15086 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
15087 Compare section data on target to the exec file.\n\
15088 Argument is a single section name (default: all loaded sections).\n\
15089 To compare only read-only loaded sections, specify the -r option."),
15092 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
15093 Send an arbitrary packet to a remote target.\n\
15094 maintenance packet TEXT\n\
15095 If GDB is talking to an inferior via the GDB serial protocol, then\n\
15096 this command sends the string TEXT to the inferior, and displays the\n\
15097 response packet. GDB supplies the initial `$' character, and the\n\
15098 terminating `#' character and checksum."),
15101 set_show_commands remotebreak_cmds
15102 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
15103 Set whether to send break if interrupted."), _("\
15104 Show whether to send break if interrupted."), _("\
15105 If set, a break, instead of a cntrl-c, is sent to the remote target."),
15106 set_remotebreak
, show_remotebreak
,
15107 &setlist
, &showlist
);
15108 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
15109 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
15111 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
15112 interrupt_sequence_modes
, &interrupt_sequence_mode
,
15114 Set interrupt sequence to remote target."), _("\
15115 Show interrupt sequence to remote target."), _("\
15116 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
15117 NULL
, show_interrupt_sequence
,
15118 &remote_set_cmdlist
,
15119 &remote_show_cmdlist
);
15121 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
15122 &interrupt_on_connect
, _("\
15123 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15124 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15125 If set, interrupt sequence is sent to remote target."),
15127 &remote_set_cmdlist
, &remote_show_cmdlist
);
15129 /* Install commands for configuring memory read/write packets. */
15131 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
15132 Set the maximum number of bytes per memory write packet (deprecated)."),
15134 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
15135 Show the maximum number of bytes per memory write packet (deprecated)."),
15137 add_cmd ("memory-write-packet-size", no_class
,
15138 set_memory_write_packet_size
, _("\
15139 Set the maximum number of bytes per memory-write packet.\n\
15140 Specify the number of bytes in a packet or 0 (zero) for the\n\
15141 default packet size. The actual limit is further reduced\n\
15142 dependent on the target. Specify \"fixed\" to disable the\n\
15143 further restriction and \"limit\" to enable that restriction."),
15144 &remote_set_cmdlist
);
15145 add_cmd ("memory-read-packet-size", no_class
,
15146 set_memory_read_packet_size
, _("\
15147 Set the maximum number of bytes per memory-read packet.\n\
15148 Specify the number of bytes in a packet or 0 (zero) for the\n\
15149 default packet size. The actual limit is further reduced\n\
15150 dependent on the target. Specify \"fixed\" to disable the\n\
15151 further restriction and \"limit\" to enable that restriction."),
15152 &remote_set_cmdlist
);
15153 add_cmd ("memory-write-packet-size", no_class
,
15154 show_memory_write_packet_size
,
15155 _("Show the maximum number of bytes per memory-write packet."),
15156 &remote_show_cmdlist
);
15157 add_cmd ("memory-read-packet-size", no_class
,
15158 show_memory_read_packet_size
,
15159 _("Show the maximum number of bytes per memory-read packet."),
15160 &remote_show_cmdlist
);
15162 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
15163 &remote_hw_watchpoint_limit
, _("\
15164 Set the maximum number of target hardware watchpoints."), _("\
15165 Show the maximum number of target hardware watchpoints."), _("\
15166 Specify \"unlimited\" for unlimited hardware watchpoints."),
15167 NULL
, show_hardware_watchpoint_limit
,
15168 &remote_set_cmdlist
,
15169 &remote_show_cmdlist
);
15170 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
15172 &remote_hw_watchpoint_length_limit
, _("\
15173 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
15174 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
15175 Specify \"unlimited\" to allow watchpoints of unlimited size."),
15176 NULL
, show_hardware_watchpoint_length_limit
,
15177 &remote_set_cmdlist
, &remote_show_cmdlist
);
15178 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
15179 &remote_hw_breakpoint_limit
, _("\
15180 Set the maximum number of target hardware breakpoints."), _("\
15181 Show the maximum number of target hardware breakpoints."), _("\
15182 Specify \"unlimited\" for unlimited hardware breakpoints."),
15183 NULL
, show_hardware_breakpoint_limit
,
15184 &remote_set_cmdlist
, &remote_show_cmdlist
);
15186 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
15187 &remote_address_size
, _("\
15188 Set the maximum size of the address (in bits) in a memory packet."), _("\
15189 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
15191 NULL
, /* FIXME: i18n: */
15192 &setlist
, &showlist
);
15194 init_all_packet_configs ();
15196 add_packet_config_cmd (PACKET_X
, "X", "binary-download", 1);
15198 add_packet_config_cmd (PACKET_vCont
, "vCont", "verbose-resume", 0);
15200 add_packet_config_cmd (PACKET_QPassSignals
, "QPassSignals", "pass-signals",
15203 add_packet_config_cmd (PACKET_QCatchSyscalls
, "QCatchSyscalls",
15204 "catch-syscalls", 0);
15206 add_packet_config_cmd (PACKET_QProgramSignals
, "QProgramSignals",
15207 "program-signals", 0);
15209 add_packet_config_cmd (PACKET_QSetWorkingDir
, "QSetWorkingDir",
15210 "set-working-dir", 0);
15212 add_packet_config_cmd (PACKET_QStartupWithShell
, "QStartupWithShell",
15213 "startup-with-shell", 0);
15215 add_packet_config_cmd (PACKET_QEnvironmentHexEncoded
,"QEnvironmentHexEncoded",
15216 "environment-hex-encoded", 0);
15218 add_packet_config_cmd (PACKET_QEnvironmentReset
, "QEnvironmentReset",
15219 "environment-reset", 0);
15221 add_packet_config_cmd (PACKET_QEnvironmentUnset
, "QEnvironmentUnset",
15222 "environment-unset", 0);
15224 add_packet_config_cmd (PACKET_qSymbol
, "qSymbol", "symbol-lookup", 0);
15226 add_packet_config_cmd (PACKET_P
, "P", "set-register", 1);
15228 add_packet_config_cmd (PACKET_p
, "p", "fetch-register", 1);
15230 add_packet_config_cmd (PACKET_Z0
, "Z0", "software-breakpoint", 0);
15232 add_packet_config_cmd (PACKET_Z1
, "Z1", "hardware-breakpoint", 0);
15234 add_packet_config_cmd (PACKET_Z2
, "Z2", "write-watchpoint", 0);
15236 add_packet_config_cmd (PACKET_Z3
, "Z3", "read-watchpoint", 0);
15238 add_packet_config_cmd (PACKET_Z4
, "Z4", "access-watchpoint", 0);
15240 add_packet_config_cmd (PACKET_qXfer_auxv
, "qXfer:auxv:read",
15241 "read-aux-vector", 0);
15243 add_packet_config_cmd (PACKET_qXfer_exec_file
, "qXfer:exec-file:read",
15244 "pid-to-exec-file", 0);
15246 add_packet_config_cmd (PACKET_qXfer_features
,
15247 "qXfer:features:read", "target-features", 0);
15249 add_packet_config_cmd (PACKET_qXfer_libraries
, "qXfer:libraries:read",
15250 "library-info", 0);
15252 add_packet_config_cmd (PACKET_qXfer_libraries_svr4
,
15253 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
15255 add_packet_config_cmd (PACKET_qXfer_memory_map
, "qXfer:memory-map:read",
15258 add_packet_config_cmd (PACKET_qXfer_osdata
, "qXfer:osdata:read", "osdata", 0);
15260 add_packet_config_cmd (PACKET_qXfer_threads
, "qXfer:threads:read", "threads",
15263 add_packet_config_cmd (PACKET_qXfer_siginfo_read
, "qXfer:siginfo:read",
15264 "read-siginfo-object", 0);
15266 add_packet_config_cmd (PACKET_qXfer_siginfo_write
, "qXfer:siginfo:write",
15267 "write-siginfo-object", 0);
15269 add_packet_config_cmd (PACKET_qXfer_traceframe_info
,
15270 "qXfer:traceframe-info:read", "traceframe-info", 0);
15272 add_packet_config_cmd (PACKET_qXfer_uib
, "qXfer:uib:read",
15273 "unwind-info-block", 0);
15275 add_packet_config_cmd (PACKET_qGetTLSAddr
, "qGetTLSAddr",
15276 "get-thread-local-storage-address", 0);
15278 add_packet_config_cmd (PACKET_qGetTIBAddr
, "qGetTIBAddr",
15279 "get-thread-information-block-address", 0);
15281 add_packet_config_cmd (PACKET_bc
, "bc", "reverse-continue", 0);
15283 add_packet_config_cmd (PACKET_bs
, "bs", "reverse-step", 0);
15285 add_packet_config_cmd (PACKET_qSupported
, "qSupported", "supported-packets",
15288 add_packet_config_cmd (PACKET_qSearch_memory
, "qSearch:memory",
15289 "search-memory", 0);
15291 add_packet_config_cmd (PACKET_qTStatus
, "qTStatus", "trace-status", 0);
15293 add_packet_config_cmd (PACKET_vFile_setfs
, "vFile:setfs", "hostio-setfs", 0);
15295 add_packet_config_cmd (PACKET_vFile_open
, "vFile:open", "hostio-open", 0);
15297 add_packet_config_cmd (PACKET_vFile_pread
, "vFile:pread", "hostio-pread", 0);
15299 add_packet_config_cmd (PACKET_vFile_pwrite
, "vFile:pwrite", "hostio-pwrite",
15302 add_packet_config_cmd (PACKET_vFile_close
, "vFile:close", "hostio-close", 0);
15304 add_packet_config_cmd (PACKET_vFile_unlink
, "vFile:unlink", "hostio-unlink",
15307 add_packet_config_cmd (PACKET_vFile_readlink
, "vFile:readlink",
15308 "hostio-readlink", 0);
15310 add_packet_config_cmd (PACKET_vFile_fstat
, "vFile:fstat", "hostio-fstat", 0);
15312 add_packet_config_cmd (PACKET_vAttach
, "vAttach", "attach", 0);
15314 add_packet_config_cmd (PACKET_vRun
, "vRun", "run", 0);
15316 add_packet_config_cmd (PACKET_QStartNoAckMode
, "QStartNoAckMode", "noack", 0);
15318 add_packet_config_cmd (PACKET_vKill
, "vKill", "kill", 0);
15320 add_packet_config_cmd (PACKET_qAttached
, "qAttached", "query-attached", 0);
15322 add_packet_config_cmd (PACKET_ConditionalTracepoints
,
15323 "ConditionalTracepoints", "conditional-tracepoints",
15326 add_packet_config_cmd (PACKET_ConditionalBreakpoints
,
15327 "ConditionalBreakpoints", "conditional-breakpoints",
15330 add_packet_config_cmd (PACKET_BreakpointCommands
, "BreakpointCommands",
15331 "breakpoint-commands", 0);
15333 add_packet_config_cmd (PACKET_FastTracepoints
, "FastTracepoints",
15334 "fast-tracepoints", 0);
15336 add_packet_config_cmd (PACKET_TracepointSource
, "TracepointSource",
15337 "TracepointSource", 0);
15339 add_packet_config_cmd (PACKET_QAllow
, "QAllow", "allow", 0);
15341 add_packet_config_cmd (PACKET_StaticTracepoints
, "StaticTracepoints",
15342 "static-tracepoints", 0);
15344 add_packet_config_cmd (PACKET_InstallInTrace
, "InstallInTrace",
15345 "install-in-trace", 0);
15347 add_packet_config_cmd (PACKET_qXfer_statictrace_read
,
15348 "qXfer:statictrace:read", "read-sdata-object", 0);
15350 add_packet_config_cmd (PACKET_qXfer_fdpic
, "qXfer:fdpic:read",
15351 "read-fdpic-loadmap", 0);
15353 add_packet_config_cmd (PACKET_QDisableRandomization
, "QDisableRandomization",
15354 "disable-randomization", 0);
15356 add_packet_config_cmd (PACKET_QAgent
, "QAgent", "agent", 0);
15358 add_packet_config_cmd (PACKET_QTBuffer_size
, "QTBuffer:size",
15359 "trace-buffer-size", 0);
15361 add_packet_config_cmd (PACKET_Qbtrace_off
, "Qbtrace:off", "disable-btrace",
15364 add_packet_config_cmd (PACKET_Qbtrace_bts
, "Qbtrace:bts", "enable-btrace-bts",
15367 add_packet_config_cmd (PACKET_Qbtrace_pt
, "Qbtrace:pt", "enable-btrace-pt",
15370 add_packet_config_cmd (PACKET_qXfer_btrace
, "qXfer:btrace", "read-btrace", 0);
15372 add_packet_config_cmd (PACKET_qXfer_btrace_conf
, "qXfer:btrace-conf",
15373 "read-btrace-conf", 0);
15375 add_packet_config_cmd (PACKET_Qbtrace_conf_bts_size
, "Qbtrace-conf:bts:size",
15376 "btrace-conf-bts-size", 0);
15378 add_packet_config_cmd (PACKET_multiprocess_feature
, "multiprocess-feature",
15379 "multiprocess-feature", 0);
15381 add_packet_config_cmd (PACKET_swbreak_feature
, "swbreak-feature",
15382 "swbreak-feature", 0);
15384 add_packet_config_cmd (PACKET_hwbreak_feature
, "hwbreak-feature",
15385 "hwbreak-feature", 0);
15387 add_packet_config_cmd (PACKET_fork_event_feature
, "fork-event-feature",
15388 "fork-event-feature", 0);
15390 add_packet_config_cmd (PACKET_vfork_event_feature
, "vfork-event-feature",
15391 "vfork-event-feature", 0);
15393 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_size
, "Qbtrace-conf:pt:size",
15394 "btrace-conf-pt-size", 0);
15396 add_packet_config_cmd (PACKET_vContSupported
, "vContSupported",
15397 "verbose-resume-supported", 0);
15399 add_packet_config_cmd (PACKET_exec_event_feature
, "exec-event-feature",
15400 "exec-event-feature", 0);
15402 add_packet_config_cmd (PACKET_vCtrlC
, "vCtrlC", "ctrl-c", 0);
15404 add_packet_config_cmd (PACKET_QThreadEvents
, "QThreadEvents", "thread-events",
15407 add_packet_config_cmd (PACKET_no_resumed
, "N stop reply",
15408 "no-resumed-stop-reply", 0);
15410 add_packet_config_cmd (PACKET_memory_tagging_feature
,
15411 "memory-tagging-feature", "memory-tagging-feature", 0);
15413 /* Assert that we've registered "set remote foo-packet" commands
15414 for all packet configs. */
15418 for (i
= 0; i
< PACKET_MAX
; i
++)
15420 /* Ideally all configs would have a command associated. Some
15421 still don't though. */
15426 case PACKET_QNonStop
:
15427 case PACKET_EnableDisableTracepoints_feature
:
15428 case PACKET_tracenz_feature
:
15429 case PACKET_DisconnectedTracing_feature
:
15430 case PACKET_augmented_libraries_svr4_read_feature
:
15432 /* Additions to this list need to be well justified:
15433 pre-existing packets are OK; new packets are not. */
15441 /* This catches both forgetting to add a config command, and
15442 forgetting to remove a packet from the exception list. */
15443 gdb_assert (excepted
== (packets_descriptions
[i
].name
== NULL
));
15447 /* Keep the old ``set remote Z-packet ...'' working. Each individual
15448 Z sub-packet has its own set and show commands, but users may
15449 have sets to this variable in their .gdbinit files (or in their
15451 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
15452 &remote_Z_packet_detect
, _("\
15453 Set use of remote protocol `Z' packets."), _("\
15454 Show use of remote protocol `Z' packets."), _("\
15455 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
15457 set_remote_protocol_Z_packet_cmd
,
15458 show_remote_protocol_Z_packet_cmd
,
15459 /* FIXME: i18n: Use of remote protocol
15460 `Z' packets is %s. */
15461 &remote_set_cmdlist
, &remote_show_cmdlist
);
15463 add_basic_prefix_cmd ("remote", class_files
, _("\
15464 Manipulate files on the remote system.\n\
15465 Transfer files to and from the remote target system."),
15467 0 /* allow-unknown */, &cmdlist
);
15469 add_cmd ("put", class_files
, remote_put_command
,
15470 _("Copy a local file to the remote system."),
15473 add_cmd ("get", class_files
, remote_get_command
,
15474 _("Copy a remote file to the local system."),
15477 add_cmd ("delete", class_files
, remote_delete_command
,
15478 _("Delete a remote file."),
15481 add_setshow_string_noescape_cmd ("exec-file", class_files
,
15482 &remote_exec_file_var
, _("\
15483 Set the remote pathname for \"run\"."), _("\
15484 Show the remote pathname for \"run\"."), NULL
,
15485 set_remote_exec_file
,
15486 show_remote_exec_file
,
15487 &remote_set_cmdlist
,
15488 &remote_show_cmdlist
);
15490 add_setshow_boolean_cmd ("range-stepping", class_run
,
15491 &use_range_stepping
, _("\
15492 Enable or disable range stepping."), _("\
15493 Show whether target-assisted range stepping is enabled."), _("\
15494 If on, and the target supports it, when stepping a source line, GDB\n\
15495 tells the target to step the corresponding range of addresses itself instead\n\
15496 of issuing multiple single-steps. This speeds up source level\n\
15497 stepping. If off, GDB always issues single-steps, even if range\n\
15498 stepping is supported by the target. The default is on."),
15499 set_range_stepping
,
15500 show_range_stepping
,
15504 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
15505 Set watchdog timer."), _("\
15506 Show watchdog timer."), _("\
15507 When non-zero, this timeout is used instead of waiting forever for a target\n\
15508 to finish a low-level step or continue operation. If the specified amount\n\
15509 of time passes without a response from the target, an error occurs."),
15512 &setlist
, &showlist
);
15514 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
15515 &remote_packet_max_chars
, _("\
15516 Set the maximum number of characters to display for each remote packet."), _("\
15517 Show the maximum number of characters to display for each remote packet."), _("\
15518 Specify \"unlimited\" to display all the characters."),
15519 NULL
, show_remote_packet_max_chars
,
15520 &setdebuglist
, &showdebuglist
);
15522 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
15523 _("Set debugging of remote protocol."),
15524 _("Show debugging of remote protocol."),
15526 When enabled, each packet sent or received with the remote target\n\
15530 &setdebuglist
, &showdebuglist
);
15532 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
15533 &remote_timeout
, _("\
15534 Set timeout limit to wait for target to respond."), _("\
15535 Show timeout limit to wait for target to respond."), _("\
15536 This value is used to set the time limit for gdb to wait for a response\n\
15537 from the target."),
15539 show_remote_timeout
,
15540 &setlist
, &showlist
);
15542 /* Eventually initialize fileio. See fileio.c */
15543 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
15546 selftests::register_test ("remote_memory_tagging",
15547 selftests::test_memory_tagging_functions
);