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_vector buf
;
359 /* Cache hit and miss counters. */
360 ULONGEST hit_count
= 0;
361 ULONGEST miss_count
= 0;
364 /* Description of the remote protocol for a given architecture. */
368 long offset
; /* Offset into G packet. */
369 long regnum
; /* GDB's internal register number. */
370 LONGEST pnum
; /* Remote protocol register number. */
371 int in_g_packet
; /* Always part of G packet. */
372 /* long size in bytes; == register_size (target_gdbarch (), regnum);
374 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
378 struct remote_arch_state
380 explicit remote_arch_state (struct gdbarch
*gdbarch
);
382 /* Description of the remote protocol registers. */
383 long sizeof_g_packet
;
385 /* Description of the remote protocol registers indexed by REGNUM
386 (making an array gdbarch_num_regs in size). */
387 std::unique_ptr
<packet_reg
[]> regs
;
389 /* This is the size (in chars) of the first response to the ``g''
390 packet. It is used as a heuristic when determining the maximum
391 size of memory-read and memory-write packets. A target will
392 typically only reserve a buffer large enough to hold the ``g''
393 packet. The size does not include packet overhead (headers and
395 long actual_register_packet_size
;
397 /* This is the maximum size (in chars) of a non read/write packet.
398 It is also used as a cap on the size of read/write packets. */
399 long remote_packet_size
;
402 /* Description of the remote protocol state for the currently
403 connected target. This is per-target state, and independent of the
404 selected architecture. */
413 /* Get the remote arch state for GDBARCH. */
414 struct remote_arch_state
*get_remote_arch_state (struct gdbarch
*gdbarch
);
418 /* A buffer to use for incoming packets, and its current size. The
419 buffer is grown dynamically for larger incoming packets.
420 Outgoing packets may also be constructed in this buffer.
421 The size of the buffer is always at least REMOTE_PACKET_SIZE;
422 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
424 gdb::char_vector buf
;
426 /* True if we're going through initial connection setup (finding out
427 about the remote side's threads, relocating symbols, etc.). */
428 bool starting_up
= false;
430 /* If we negotiated packet size explicitly (and thus can bypass
431 heuristics for the largest packet size that will not overflow
432 a buffer in the stub), this will be set to that packet size.
433 Otherwise zero, meaning to use the guessed size. */
434 long explicit_packet_size
= 0;
436 /* True, if in no ack mode. That is, neither GDB nor the stub will
437 expect acks from each other. The connection is assumed to be
439 bool noack_mode
= false;
441 /* True if we're connected in extended remote mode. */
442 bool extended
= false;
444 /* True if we resumed the target and we're waiting for the target to
445 stop. In the mean time, we can't start another command/query.
446 The remote server wouldn't be ready to process it, so we'd
447 timeout waiting for a reply that would never come and eventually
448 we'd close the connection. This can happen in asynchronous mode
449 because we allow GDB commands while the target is running. */
450 bool waiting_for_stop_reply
= false;
452 /* The status of the stub support for the various vCont actions. */
453 vCont_action_support supports_vCont
;
455 /* True if the user has pressed Ctrl-C, but the target hasn't
456 responded to that. */
457 bool ctrlc_pending_p
= false;
459 /* True if we saw a Ctrl-C while reading or writing from/to the
460 remote descriptor. At that point it is not safe to send a remote
461 interrupt packet, so we instead remember we saw the Ctrl-C and
462 process it once we're done with sending/receiving the current
463 packet, which should be shortly. If however that takes too long,
464 and the user presses Ctrl-C again, we offer to disconnect. */
465 bool got_ctrlc_during_io
= false;
467 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
468 remote_open knows that we don't have a file open when the program
470 struct serial
*remote_desc
= nullptr;
472 /* These are the threads which we last sent to the remote system. The
473 TID member will be -1 for all or -2 for not sent yet. */
474 ptid_t general_thread
= null_ptid
;
475 ptid_t continue_thread
= null_ptid
;
477 /* This is the traceframe which we last selected on the remote system.
478 It will be -1 if no traceframe is selected. */
479 int remote_traceframe_number
= -1;
481 char *last_pass_packet
= nullptr;
483 /* The last QProgramSignals packet sent to the target. We bypass
484 sending a new program signals list down to the target if the new
485 packet is exactly the same as the last we sent. IOW, we only let
486 the target know about program signals list changes. */
487 char *last_program_signals_packet
= nullptr;
489 gdb_signal last_sent_signal
= GDB_SIGNAL_0
;
491 bool last_sent_step
= false;
493 /* The execution direction of the last resume we got. */
494 exec_direction_kind last_resume_exec_dir
= EXEC_FORWARD
;
496 char *finished_object
= nullptr;
497 char *finished_annex
= nullptr;
498 ULONGEST finished_offset
= 0;
500 /* Should we try the 'ThreadInfo' query packet?
502 This variable (NOT available to the user: auto-detect only!)
503 determines whether GDB will use the new, simpler "ThreadInfo"
504 query or the older, more complex syntax for thread queries.
505 This is an auto-detect variable (set to true at each connect,
506 and set to false when the target fails to recognize it). */
507 bool use_threadinfo_query
= false;
508 bool use_threadextra_query
= false;
510 threadref echo_nextthread
{};
511 threadref nextthread
{};
512 threadref resultthreadlist
[MAXTHREADLISTRESULTS
] {};
514 /* The state of remote notification. */
515 struct remote_notif_state
*notif_state
= nullptr;
517 /* The branch trace configuration. */
518 struct btrace_config btrace_config
{};
520 /* The argument to the last "vFile:setfs:" packet we sent, used
521 to avoid sending repeated unnecessary "vFile:setfs:" packets.
522 Initialized to -1 to indicate that no "vFile:setfs:" packet
523 has yet been sent. */
526 /* A readahead cache for vFile:pread. Often, reading a binary
527 involves a sequence of small reads. E.g., when parsing an ELF
528 file. A readahead cache helps mostly the case of remote
529 debugging on a connection with higher latency, due to the
530 request/reply nature of the RSP. We only cache data for a single
531 file descriptor at a time. */
532 struct readahead_cache readahead_cache
;
534 /* The list of already fetched and acknowledged stop events. This
535 queue is used for notification Stop, and other notifications
536 don't need queue for their events, because the notification
537 events of Stop can't be consumed immediately, so that events
538 should be queued first, and be consumed by remote_wait_{ns,as}
539 one per time. Other notifications can consume their events
540 immediately, so queue is not needed for them. */
541 std::vector
<stop_reply_up
> stop_reply_queue
;
543 /* Asynchronous signal handle registered as event loop source for
544 when we have pending events ready to be passed to the core. */
545 struct async_event_handler
*remote_async_inferior_event_token
= nullptr;
547 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
548 ``forever'' still use the normal timeout mechanism. This is
549 currently used by the ASYNC code to guarentee that target reads
550 during the initial connect always time-out. Once getpkt has been
551 modified to return a timeout indication and, in turn
552 remote_wait()/wait_for_inferior() have gained a timeout parameter
554 int wait_forever_enabled_p
= 1;
557 /* Mapping of remote protocol data for each gdbarch. Usually there
558 is only one entry here, though we may see more with stubs that
559 support multi-process. */
560 std::unordered_map
<struct gdbarch
*, remote_arch_state
>
564 static const target_info remote_target_info
= {
566 N_("Remote target using gdb-specific protocol"),
570 /* Description of a remote packet. */
572 struct packet_description
574 /* Name of the packet used for gdb output. */
577 /* Title of the packet, used by the set/show remote name-packet
578 commands to identify the individual packages and gdb output. */
582 /* Configuration of a remote packet. */
586 /* If auto, GDB auto-detects support for this packet or feature,
587 either through qSupported, or by trying the packet and looking
588 at the response. If true, GDB assumes the target supports this
589 packet. If false, the packet is disabled. Configs that don't
590 have an associated command always have this set to auto. */
591 enum auto_boolean detect
;
593 /* Does the target support this packet? */
594 enum packet_support support
;
597 /* User configurable variables for the number of characters in a
598 memory read/write packet. MIN (rsa->remote_packet_size,
599 rsa->sizeof_g_packet) is the default. Some targets need smaller
600 values (fifo overruns, et.al.) and some users need larger values
601 (speed up transfers). The variables ``preferred_*'' (the user
602 request), ``current_*'' (what was actually set) and ``forced_*''
603 (Positive - a soft limit, negative - a hard limit). */
605 struct memory_packet_config
612 /* These global variables contain the default configuration for every new
613 remote_feature object. */
614 static memory_packet_config memory_read_packet_config
=
616 "memory-read-packet-size",
618 static memory_packet_config memory_write_packet_config
=
620 "memory-write-packet-size",
623 /* This global array contains packet descriptions (name and title). */
624 static packet_description packets_descriptions
[PACKET_MAX
];
625 /* This global array contains the default configuration for every new
626 per-remote target array. */
627 static packet_config remote_protocol_packets
[PACKET_MAX
];
629 /* Description of a remote target's features. It stores the configuration
630 and provides functions to determine supported features of the target. */
632 struct remote_features
636 m_memory_read_packet_config
= memory_read_packet_config
;
637 m_memory_write_packet_config
= memory_write_packet_config
;
639 std::copy (std::begin (remote_protocol_packets
),
640 std::end (remote_protocol_packets
),
641 std::begin (m_protocol_packets
));
643 ~remote_features () = default;
645 DISABLE_COPY_AND_ASSIGN (remote_features
);
647 /* Returns whether a given packet defined by its enum value is supported. */
648 enum packet_support
packet_support (int) const;
650 /* Returns the packet's corresponding "set remote foo-packet" command
651 state. See struct packet_config for more details. */
652 enum auto_boolean
packet_set_cmd_state (int packet
) const
653 { return m_protocol_packets
[packet
].detect
; }
655 /* Returns true if the multi-process extensions are in effect. */
656 int remote_multi_process_p () const
657 { return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
; }
659 /* Returns true if fork events are supported. */
660 int remote_fork_event_p () const
661 { return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
; }
663 /* Returns true if vfork events are supported. */
664 int remote_vfork_event_p () const
665 { return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
; }
667 /* Returns true if exec events are supported. */
668 int remote_exec_event_p () const
669 { return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
; }
671 /* Returns true if memory tagging is supported, false otherwise. */
672 bool remote_memory_tagging_p () const
673 { return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
; }
675 /* Reset all packets back to "unknown support". Called when opening a
676 new connection to a remote target. */
677 void reset_all_packet_configs_support ();
679 /* Check result value in BUF for packet WHICH_PACKET and update the packet's
680 support configuration accordingly. */
681 packet_result
packet_ok (const char *buf
, const int which_packet
);
682 packet_result
packet_ok (const gdb::char_vector
&buf
, const int which_packet
);
684 /* Configuration of a remote target's memory read packet. */
685 memory_packet_config m_memory_read_packet_config
;
686 /* Configuration of a remote target's memory write packet. */
687 memory_packet_config m_memory_write_packet_config
;
689 /* The per-remote target array which stores a remote's packet
691 packet_config m_protocol_packets
[PACKET_MAX
];
694 class remote_target
: public process_stratum_target
697 remote_target () = default;
698 ~remote_target () override
;
700 const target_info
&info () const override
701 { return remote_target_info
; }
703 const char *connection_string () override
;
705 thread_control_capabilities
get_thread_control_capabilities () override
706 { return tc_schedlock
; }
708 /* Open a remote connection. */
709 static void open (const char *, int);
711 void close () override
;
713 void detach (inferior
*, int) override
;
714 void disconnect (const char *, int) override
;
716 void commit_resumed () override
;
717 void resume (ptid_t
, int, enum gdb_signal
) override
;
718 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
719 bool has_pending_events () override
;
721 void fetch_registers (struct regcache
*, int) override
;
722 void store_registers (struct regcache
*, int) override
;
723 void prepare_to_store (struct regcache
*) override
;
725 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
727 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
728 enum remove_bp_reason
) override
;
731 bool stopped_by_sw_breakpoint () override
;
732 bool supports_stopped_by_sw_breakpoint () override
;
734 bool stopped_by_hw_breakpoint () override
;
736 bool supports_stopped_by_hw_breakpoint () override
;
738 bool stopped_by_watchpoint () override
;
740 bool stopped_data_address (CORE_ADDR
*) override
;
742 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
744 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
746 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
748 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
750 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
752 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
753 struct expression
*) override
;
755 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
756 struct expression
*) override
;
758 void kill () override
;
760 void load (const char *, int) override
;
762 void mourn_inferior () override
;
764 void pass_signals (gdb::array_view
<const unsigned char>) override
;
766 int set_syscall_catchpoint (int, bool, int,
767 gdb::array_view
<const int>) override
;
769 void program_signals (gdb::array_view
<const unsigned char>) override
;
771 bool thread_alive (ptid_t ptid
) override
;
773 const char *thread_name (struct thread_info
*) override
;
775 void update_thread_list () override
;
777 std::string
pid_to_str (ptid_t
) override
;
779 const char *extra_thread_info (struct thread_info
*) override
;
781 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
783 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
785 inferior
*inf
) override
;
787 gdb::array_view
<const gdb_byte
> thread_info_to_thread_handle (struct thread_info
*tp
)
790 void stop (ptid_t
) override
;
792 void interrupt () override
;
794 void pass_ctrlc () override
;
796 enum target_xfer_status
xfer_partial (enum target_object object
,
799 const gdb_byte
*writebuf
,
800 ULONGEST offset
, ULONGEST len
,
801 ULONGEST
*xfered_len
) override
;
803 ULONGEST
get_memory_xfer_limit () override
;
805 void rcmd (const char *command
, struct ui_file
*output
) override
;
807 const char *pid_to_exec_file (int pid
) override
;
809 void log_command (const char *cmd
) override
811 serial_log_command (this, cmd
);
814 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
815 CORE_ADDR load_module_addr
,
816 CORE_ADDR offset
) override
;
818 bool can_execute_reverse () override
;
820 std::vector
<mem_region
> memory_map () override
;
822 void flash_erase (ULONGEST address
, LONGEST length
) override
;
824 void flash_done () override
;
826 const struct target_desc
*read_description () override
;
828 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
829 const gdb_byte
*pattern
, ULONGEST pattern_len
,
830 CORE_ADDR
*found_addrp
) override
;
832 bool can_async_p () override
;
834 bool is_async_p () override
;
836 void async (bool) override
;
838 int async_wait_fd () override
;
840 void thread_events (int) override
;
842 int can_do_single_step () override
;
844 void terminal_inferior () override
;
846 void terminal_ours () override
;
848 bool supports_non_stop () override
;
850 bool supports_multi_process () override
;
852 bool supports_disable_randomization () override
;
854 bool filesystem_is_local () override
;
857 int fileio_open (struct inferior
*inf
, const char *filename
,
858 int flags
, int mode
, int warn_if_slow
,
859 fileio_error
*target_errno
) override
;
861 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
862 ULONGEST offset
, fileio_error
*target_errno
) override
;
864 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
865 ULONGEST offset
, fileio_error
*target_errno
) override
;
867 int fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
) override
;
869 int fileio_close (int fd
, fileio_error
*target_errno
) override
;
871 int fileio_unlink (struct inferior
*inf
,
872 const char *filename
,
873 fileio_error
*target_errno
) override
;
875 gdb::optional
<std::string
>
876 fileio_readlink (struct inferior
*inf
,
877 const char *filename
,
878 fileio_error
*target_errno
) override
;
880 bool supports_enable_disable_tracepoint () override
;
882 bool supports_string_tracing () override
;
884 int remote_supports_cond_tracepoints ();
886 bool supports_evaluation_of_breakpoint_conditions () override
;
888 int remote_supports_fast_tracepoints ();
890 int remote_supports_static_tracepoints ();
892 int remote_supports_install_in_trace ();
894 bool can_run_breakpoint_commands () override
;
896 void trace_init () override
;
898 void download_tracepoint (struct bp_location
*location
) override
;
900 bool can_download_tracepoint () override
;
902 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
904 void enable_tracepoint (struct bp_location
*location
) override
;
906 void disable_tracepoint (struct bp_location
*location
) override
;
908 void trace_set_readonly_regions () override
;
910 void trace_start () override
;
912 int get_trace_status (struct trace_status
*ts
) override
;
914 void get_tracepoint_status (struct breakpoint
*tp
, struct uploaded_tp
*utp
)
917 void trace_stop () override
;
919 int trace_find (enum trace_find_type type
, int num
,
920 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
922 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
924 int save_trace_data (const char *filename
) override
;
926 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
928 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
930 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
932 int get_min_fast_tracepoint_insn_len () override
;
934 void set_disconnected_tracing (int val
) override
;
936 void set_circular_trace_buffer (int val
) override
;
938 void set_trace_buffer_size (LONGEST val
) override
;
940 bool set_trace_notes (const char *user
, const char *notes
,
941 const char *stopnotes
) override
;
943 int core_of_thread (ptid_t ptid
) override
;
945 int verify_memory (const gdb_byte
*data
,
946 CORE_ADDR memaddr
, ULONGEST size
) override
;
949 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
951 void set_permissions () override
;
953 bool static_tracepoint_marker_at (CORE_ADDR
,
954 struct static_tracepoint_marker
*marker
)
957 std::vector
<static_tracepoint_marker
>
958 static_tracepoint_markers_by_strid (const char *id
) override
;
960 traceframe_info_up
traceframe_info () override
;
962 bool use_agent (bool use
) override
;
963 bool can_use_agent () override
;
965 struct btrace_target_info
*
966 enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
) override
;
968 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
970 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
972 enum btrace_error
read_btrace (struct btrace_data
*data
,
973 struct btrace_target_info
*btinfo
,
974 enum btrace_read_type type
) override
;
976 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
977 bool augmented_libraries_svr4_read () override
;
978 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
979 void follow_exec (inferior
*, ptid_t
, const char *) override
;
980 int insert_fork_catchpoint (int) override
;
981 int remove_fork_catchpoint (int) override
;
982 int insert_vfork_catchpoint (int) override
;
983 int remove_vfork_catchpoint (int) override
;
984 int insert_exec_catchpoint (int) override
;
985 int remove_exec_catchpoint (int) override
;
986 enum exec_direction_kind
execution_direction () override
;
988 bool supports_memory_tagging () override
;
990 bool fetch_memtags (CORE_ADDR address
, size_t len
,
991 gdb::byte_vector
&tags
, int type
) override
;
993 bool store_memtags (CORE_ADDR address
, size_t len
,
994 const gdb::byte_vector
&tags
, int type
) override
;
996 public: /* Remote specific methods. */
998 void remote_download_command_source (int num
, ULONGEST addr
,
999 struct command_line
*cmds
);
1001 void remote_file_put (const char *local_file
, const char *remote_file
,
1003 void remote_file_get (const char *remote_file
, const char *local_file
,
1005 void remote_file_delete (const char *remote_file
, int from_tty
);
1007 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
1008 ULONGEST offset
, fileio_error
*remote_errno
);
1009 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
1010 ULONGEST offset
, fileio_error
*remote_errno
);
1011 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
1012 ULONGEST offset
, fileio_error
*remote_errno
);
1014 int remote_hostio_send_command (int command_bytes
, int which_packet
,
1015 fileio_error
*remote_errno
, const char **attachment
,
1016 int *attachment_len
);
1017 int remote_hostio_set_filesystem (struct inferior
*inf
,
1018 fileio_error
*remote_errno
);
1019 /* We should get rid of this and use fileio_open directly. */
1020 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
1021 int flags
, int mode
, int warn_if_slow
,
1022 fileio_error
*remote_errno
);
1023 int remote_hostio_close (int fd
, fileio_error
*remote_errno
);
1025 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
1026 fileio_error
*remote_errno
);
1028 struct remote_state
*get_remote_state ();
1030 long get_remote_packet_size (void);
1031 long get_memory_packet_size (struct memory_packet_config
*config
);
1033 long get_memory_write_packet_size ();
1034 long get_memory_read_packet_size ();
1036 char *append_pending_thread_resumptions (char *p
, char *endp
,
1038 static void open_1 (const char *name
, int from_tty
, int extended_p
);
1039 void start_remote (int from_tty
, int extended_p
);
1040 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
1042 char *append_resumption (char *p
, char *endp
,
1043 ptid_t ptid
, int step
, gdb_signal siggnal
);
1044 int remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
1045 gdb_signal siggnal
);
1047 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
1049 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
1050 target_wait_flags options
);
1051 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
1052 target_wait_flags options
);
1054 ptid_t
process_stop_reply (struct stop_reply
*stop_reply
,
1055 target_waitstatus
*status
);
1057 ptid_t select_thread_for_ambiguous_stop_reply
1058 (const struct target_waitstatus
&status
);
1060 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
1062 void print_one_stopped_thread (thread_info
*thread
);
1063 void process_initial_stop_replies (int from_tty
);
1065 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
1068 void btrace_sync_conf (const btrace_config
*conf
);
1070 void remote_btrace_maybe_reopen ();
1072 void remove_new_fork_children (threads_listing_context
*context
);
1073 void kill_new_fork_children (inferior
*inf
);
1074 void discard_pending_stop_replies (struct inferior
*inf
);
1075 int stop_reply_queue_length ();
1077 void check_pending_events_prevent_wildcard_vcont
1078 (bool *may_global_wildcard_vcont
);
1080 void discard_pending_stop_replies_in_queue ();
1081 struct stop_reply
*remote_notif_remove_queued_reply (ptid_t ptid
);
1082 struct stop_reply
*queued_stop_reply (ptid_t ptid
);
1083 int peek_stop_reply (ptid_t ptid
);
1084 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
1086 void remote_stop_ns (ptid_t ptid
);
1087 void remote_interrupt_as ();
1088 void remote_interrupt_ns ();
1090 char *remote_get_noisy_reply ();
1091 int remote_query_attached (int pid
);
1092 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
1095 ptid_t
remote_current_thread (ptid_t oldpid
);
1096 ptid_t
get_current_thread (const char *wait_status
);
1098 void set_thread (ptid_t ptid
, int gen
);
1099 void set_general_thread (ptid_t ptid
);
1100 void set_continue_thread (ptid_t ptid
);
1101 void set_general_process ();
1103 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
1105 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
1106 gdb_ext_thread_info
*info
);
1107 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
1108 gdb_ext_thread_info
*info
);
1110 int parse_threadlist_response (const char *pkt
, int result_limit
,
1111 threadref
*original_echo
,
1112 threadref
*resultlist
,
1114 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
1115 int result_limit
, int *done
, int *result_count
,
1116 threadref
*threadlist
);
1118 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1119 void *context
, int looplimit
);
1121 int remote_get_threads_with_ql (threads_listing_context
*context
);
1122 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
1123 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
1125 void extended_remote_restart ();
1127 void get_offsets ();
1129 void remote_check_symbols ();
1131 void remote_supported_packet (const struct protocol_feature
*feature
,
1132 enum packet_support support
,
1133 const char *argument
);
1135 void remote_query_supported ();
1137 void remote_packet_size (const protocol_feature
*feature
,
1138 packet_support support
, const char *value
);
1140 void remote_serial_quit_handler ();
1142 void remote_detach_pid (int pid
);
1144 void remote_vcont_probe ();
1146 void remote_resume_with_hc (ptid_t ptid
, int step
,
1147 gdb_signal siggnal
);
1149 void send_interrupt_sequence ();
1150 void interrupt_query ();
1152 void remote_notif_get_pending_events (const notif_client
*nc
);
1154 int fetch_register_using_p (struct regcache
*regcache
,
1156 int send_g_packet ();
1157 void process_g_packet (struct regcache
*regcache
);
1158 void fetch_registers_using_g (struct regcache
*regcache
);
1159 int store_register_using_P (const struct regcache
*regcache
,
1161 void store_registers_using_G (const struct regcache
*regcache
);
1163 void set_remote_traceframe ();
1165 void check_binary_download (CORE_ADDR addr
);
1167 target_xfer_status
remote_write_bytes_aux (const char *header
,
1169 const gdb_byte
*myaddr
,
1172 ULONGEST
*xfered_len_units
,
1176 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
1177 const gdb_byte
*myaddr
, ULONGEST len
,
1178 int unit_size
, ULONGEST
*xfered_len
);
1180 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
1182 int unit_size
, ULONGEST
*xfered_len_units
);
1184 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
1188 ULONGEST
*xfered_len
);
1190 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
1191 gdb_byte
*myaddr
, ULONGEST len
,
1193 ULONGEST
*xfered_len
);
1195 packet_result
remote_send_printf (const char *format
, ...)
1196 ATTRIBUTE_PRINTF (2, 3);
1198 target_xfer_status
remote_flash_write (ULONGEST address
,
1199 ULONGEST length
, ULONGEST
*xfered_len
,
1200 const gdb_byte
*data
);
1202 int readchar (int timeout
);
1204 void remote_serial_write (const char *str
, int len
);
1206 int putpkt (const char *buf
);
1207 int putpkt_binary (const char *buf
, int cnt
);
1209 int putpkt (const gdb::char_vector
&buf
)
1211 return putpkt (buf
.data ());
1215 long read_frame (gdb::char_vector
*buf_p
);
1216 int getpkt_or_notif_sane_1 (gdb::char_vector
*buf
, int forever
,
1217 int expecting_notif
, int *is_notif
);
1218 int getpkt (gdb::char_vector
*buf
, int forever
);
1219 int getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
1221 int remote_vkill (int pid
);
1222 void remote_kill_k ();
1224 void extended_remote_disable_randomization (int val
);
1225 int extended_remote_run (const std::string
&args
);
1227 void send_environment_packet (const char *action
,
1231 void extended_remote_environment_support ();
1232 void extended_remote_set_inferior_cwd ();
1234 target_xfer_status
remote_write_qxfer (const char *object_name
,
1236 const gdb_byte
*writebuf
,
1237 ULONGEST offset
, LONGEST len
,
1238 ULONGEST
*xfered_len
,
1239 const unsigned int which_packet
);
1241 target_xfer_status
remote_read_qxfer (const char *object_name
,
1243 gdb_byte
*readbuf
, ULONGEST offset
,
1245 ULONGEST
*xfered_len
,
1246 const unsigned int which_packet
);
1248 void push_stop_reply (struct stop_reply
*new_event
);
1250 bool vcont_r_supported ();
1252 remote_features m_features
;
1256 bool start_remote_1 (int from_tty
, int extended_p
);
1258 /* The remote state. Don't reference this directly. Use the
1259 get_remote_state method instead. */
1260 remote_state m_remote_state
;
1263 static const target_info extended_remote_target_info
= {
1265 N_("Extended remote target using gdb-specific protocol"),
1269 /* Set up the extended remote target by extending the standard remote
1270 target and adding to it. */
1272 class extended_remote_target final
: public remote_target
1275 const target_info
&info () const override
1276 { return extended_remote_target_info
; }
1278 /* Open an extended-remote connection. */
1279 static void open (const char *, int);
1281 bool can_create_inferior () override
{ return true; }
1282 void create_inferior (const char *, const std::string
&,
1283 char **, int) override
;
1285 void detach (inferior
*, int) override
;
1287 bool can_attach () override
{ return true; }
1288 void attach (const char *, int) override
;
1290 void post_attach (int) override
;
1291 bool supports_disable_randomization () override
;
1294 struct stop_reply
: public notif_event
1298 /* The identifier of the thread about this event */
1301 /* The remote state this event is associated with. When the remote
1302 connection, represented by a remote_state object, is closed,
1303 all the associated stop_reply events should be released. */
1304 struct remote_state
*rs
;
1306 struct target_waitstatus ws
;
1308 /* The architecture associated with the expedited registers. */
1311 /* Expedited registers. This makes remote debugging a bit more
1312 efficient for those targets that provide critical registers as
1313 part of their normal status mechanism (as another roundtrip to
1314 fetch them is avoided). */
1315 std::vector
<cached_reg_t
> regcache
;
1317 enum target_stop_reason stop_reason
;
1319 CORE_ADDR watch_data_address
;
1324 /* Return TARGET as a remote_target if it is one, else nullptr. */
1326 static remote_target
*
1327 as_remote_target (process_stratum_target
*target
)
1329 return dynamic_cast<remote_target
*> (target
);
1335 is_remote_target (process_stratum_target
*target
)
1337 return as_remote_target (target
) != nullptr;
1340 /* Per-program-space data key. */
1341 static const registry
<program_space
>::key
<char, gdb::xfree_deleter
<char>>
1344 /* The variable registered as the control variable used by the
1345 remote exec-file commands. While the remote exec-file setting is
1346 per-program-space, the set/show machinery uses this as the
1347 location of the remote exec-file value. */
1348 static std::string remote_exec_file_var
;
1350 /* The size to align memory write packets, when practical. The protocol
1351 does not guarantee any alignment, and gdb will generate short
1352 writes and unaligned writes, but even as a best-effort attempt this
1353 can improve bulk transfers. For instance, if a write is misaligned
1354 relative to the target's data bus, the stub may need to make an extra
1355 round trip fetching data from the target. This doesn't make a
1356 huge difference, but it's easy to do, so we try to be helpful.
1358 The alignment chosen is arbitrary; usually data bus width is
1359 important here, not the possibly larger cache line size. */
1360 enum { REMOTE_ALIGN_WRITES
= 16 };
1362 /* Prototypes for local functions. */
1364 static int hexnumlen (ULONGEST num
);
1366 static int stubhex (int ch
);
1368 static int hexnumstr (char *, ULONGEST
);
1370 static int hexnumnstr (char *, ULONGEST
, int);
1372 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1374 static int stub_unpack_int (const char *buff
, int fieldlength
);
1376 static void set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
1377 cmd_list_element
*c
);
1379 static void show_packet_config_cmd (ui_file
*file
,
1380 const unsigned int which_packet
,
1381 remote_target
*remote
);
1383 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1385 struct cmd_list_element
*c
,
1388 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1390 static void remote_async_inferior_event_handler (gdb_client_data
);
1392 static bool remote_read_description_p (struct target_ops
*target
);
1394 static void remote_console_output (const char *msg
);
1396 static void remote_btrace_reset (remote_state
*rs
);
1398 static void remote_unpush_and_throw (remote_target
*target
);
1402 static struct cmd_list_element
*remote_cmdlist
;
1404 /* For "set remote" and "show remote". */
1406 static struct cmd_list_element
*remote_set_cmdlist
;
1407 static struct cmd_list_element
*remote_show_cmdlist
;
1409 /* Controls whether GDB is willing to use range stepping. */
1411 static bool use_range_stepping
= true;
1413 /* From the remote target's point of view, each thread is in one of these three
1415 enum class resume_state
1417 /* Not resumed - we haven't been asked to resume this thread. */
1420 /* We have been asked to resume this thread, but haven't sent a vCont action
1421 for it yet. We'll need to consider it next time commit_resume is
1423 RESUMED_PENDING_VCONT
,
1425 /* We have been asked to resume this thread, and we have sent a vCont action
1430 /* Information about a thread's pending vCont-resume. Used when a thread is in
1431 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1432 stores this information which is then picked up by
1433 remote_target::commit_resume to know which is the proper action for this
1434 thread to include in the vCont packet. */
1435 struct resumed_pending_vcont_info
1437 /* True if the last resume call for this thread was a step request, false
1438 if a continue request. */
1441 /* The signal specified in the last resume call for this thread. */
1445 /* Private data that we'll store in (struct thread_info)->priv. */
1446 struct remote_thread_info
: public private_thread_info
1452 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1453 sequence of bytes. */
1454 gdb::byte_vector thread_handle
;
1456 /* Whether the target stopped for a breakpoint/watchpoint. */
1457 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1459 /* This is set to the data address of the access causing the target
1460 to stop for a watchpoint. */
1461 CORE_ADDR watch_data_address
= 0;
1463 /* Get the thread's resume state. */
1464 enum resume_state
get_resume_state () const
1466 return m_resume_state
;
1469 /* Put the thread in the NOT_RESUMED state. */
1470 void set_not_resumed ()
1472 m_resume_state
= resume_state::NOT_RESUMED
;
1475 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1476 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1478 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1479 m_resumed_pending_vcont_info
.step
= step
;
1480 m_resumed_pending_vcont_info
.sig
= sig
;
1483 /* Get the information this thread's pending vCont-resumption.
1485 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1487 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1489 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1491 return m_resumed_pending_vcont_info
;
1494 /* Put the thread in the VCONT_RESUMED state. */
1497 m_resume_state
= resume_state::RESUMED
;
1501 /* Resume state for this thread. This is used to implement vCont action
1502 coalescing (only when the target operates in non-stop mode).
1504 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1505 which notes that this thread must be considered in the next commit_resume
1508 remote_target::commit_resume sends a vCont packet with actions for the
1509 threads in the RESUMED_PENDING_VCONT state and moves them to the
1510 VCONT_RESUMED state.
1512 When reporting a stop to the core for a thread, that thread is moved back
1513 to the NOT_RESUMED state. */
1514 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1516 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1517 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1520 remote_state::remote_state ()
1525 remote_state::~remote_state ()
1527 xfree (this->last_pass_packet
);
1528 xfree (this->last_program_signals_packet
);
1529 xfree (this->finished_object
);
1530 xfree (this->finished_annex
);
1533 /* Utility: generate error from an incoming stub packet. */
1535 trace_error (char *buf
)
1538 return; /* not an error msg */
1541 case '1': /* malformed packet error */
1542 if (*++buf
== '0') /* general case: */
1543 error (_("remote.c: error in outgoing packet."));
1545 error (_("remote.c: error in outgoing packet at field #%ld."),
1546 strtol (buf
, NULL
, 16));
1548 error (_("Target returns error code '%s'."), buf
);
1552 /* Utility: wait for reply from stub, while accepting "O" packets. */
1555 remote_target::remote_get_noisy_reply ()
1557 struct remote_state
*rs
= get_remote_state ();
1559 do /* Loop on reply from remote stub. */
1563 QUIT
; /* Allow user to bail out with ^C. */
1564 getpkt (&rs
->buf
, 0);
1565 buf
= rs
->buf
.data ();
1568 else if (startswith (buf
, "qRelocInsn:"))
1571 CORE_ADDR from
, to
, org_to
;
1573 int adjusted_size
= 0;
1576 p
= buf
+ strlen ("qRelocInsn:");
1577 pp
= unpack_varlen_hex (p
, &ul
);
1579 error (_("invalid qRelocInsn packet: %s"), buf
);
1583 unpack_varlen_hex (p
, &ul
);
1590 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
1593 catch (const gdb_exception
&ex
)
1595 if (ex
.error
== MEMORY_ERROR
)
1597 /* Propagate memory errors silently back to the
1598 target. The stub may have limited the range of
1599 addresses we can write to, for example. */
1603 /* Something unexpectedly bad happened. Be verbose
1604 so we can tell what, and propagate the error back
1605 to the stub, so it doesn't get stuck waiting for
1607 exception_fprintf (gdb_stderr
, ex
,
1608 _("warning: relocating instruction: "));
1615 adjusted_size
= to
- org_to
;
1617 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1621 else if (buf
[0] == 'O' && buf
[1] != 'K')
1622 remote_console_output (buf
+ 1); /* 'O' message from stub */
1624 return buf
; /* Here's the actual reply. */
1629 struct remote_arch_state
*
1630 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1632 remote_arch_state
*rsa
;
1634 auto it
= this->m_arch_states
.find (gdbarch
);
1635 if (it
== this->m_arch_states
.end ())
1637 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1638 std::forward_as_tuple (gdbarch
),
1639 std::forward_as_tuple (gdbarch
));
1640 rsa
= &p
.first
->second
;
1642 /* Make sure that the packet buffer is plenty big enough for
1643 this architecture. */
1644 if (this->buf
.size () < rsa
->remote_packet_size
)
1645 this->buf
.resize (2 * rsa
->remote_packet_size
);
1653 /* Fetch the global remote target state. */
1656 remote_target::get_remote_state ()
1658 /* Make sure that the remote architecture state has been
1659 initialized, because doing so might reallocate rs->buf. Any
1660 function which calls getpkt also needs to be mindful of changes
1661 to rs->buf, but this call limits the number of places which run
1663 m_remote_state
.get_remote_arch_state (target_gdbarch ());
1665 return &m_remote_state
;
1668 /* Fetch the remote exec-file from the current program space. */
1671 get_remote_exec_file (void)
1673 char *remote_exec_file
;
1675 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1676 if (remote_exec_file
== NULL
)
1679 return remote_exec_file
;
1682 /* Set the remote exec file for PSPACE. */
1685 set_pspace_remote_exec_file (struct program_space
*pspace
,
1686 const char *remote_exec_file
)
1688 char *old_file
= remote_pspace_data
.get (pspace
);
1691 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1694 /* The "set/show remote exec-file" set command hook. */
1697 set_remote_exec_file (const char *ignored
, int from_tty
,
1698 struct cmd_list_element
*c
)
1700 set_pspace_remote_exec_file (current_program_space
,
1701 remote_exec_file_var
.c_str ());
1704 /* The "set/show remote exec-file" show command hook. */
1707 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1708 struct cmd_list_element
*cmd
, const char *value
)
1710 gdb_printf (file
, "%s\n", get_remote_exec_file ());
1714 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1716 int regnum
, num_remote_regs
, offset
;
1717 struct packet_reg
**remote_regs
;
1719 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1721 struct packet_reg
*r
= ®s
[regnum
];
1723 if (register_size (gdbarch
, regnum
) == 0)
1724 /* Do not try to fetch zero-sized (placeholder) registers. */
1727 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1732 /* Define the g/G packet format as the contents of each register
1733 with a remote protocol number, in order of ascending protocol
1736 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1737 for (num_remote_regs
= 0, regnum
= 0;
1738 regnum
< gdbarch_num_regs (gdbarch
);
1740 if (regs
[regnum
].pnum
!= -1)
1741 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1743 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1744 [] (const packet_reg
*a
, const packet_reg
*b
)
1745 { return a
->pnum
< b
->pnum
; });
1747 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1749 remote_regs
[regnum
]->in_g_packet
= 1;
1750 remote_regs
[regnum
]->offset
= offset
;
1751 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1757 /* Given the architecture described by GDBARCH, return the remote
1758 protocol register's number and the register's offset in the g/G
1759 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1760 If the target does not have a mapping for REGNUM, return false,
1761 otherwise, return true. */
1764 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1765 int *pnum
, int *poffset
)
1767 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1769 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1771 map_regcache_remote_table (gdbarch
, regs
.data ());
1773 *pnum
= regs
[regnum
].pnum
;
1774 *poffset
= regs
[regnum
].offset
;
1779 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1781 /* Use the architecture to build a regnum<->pnum table, which will be
1782 1:1 unless a feature set specifies otherwise. */
1783 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1785 /* Record the maximum possible size of the g packet - it may turn out
1787 this->sizeof_g_packet
1788 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1790 /* Default maximum number of characters in a packet body. Many
1791 remote stubs have a hardwired buffer size of 400 bytes
1792 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1793 as the maximum packet-size to ensure that the packet and an extra
1794 NUL character can always fit in the buffer. This stops GDB
1795 trashing stubs that try to squeeze an extra NUL into what is
1796 already a full buffer (As of 1999-12-04 that was most stubs). */
1797 this->remote_packet_size
= 400 - 1;
1799 /* This one is filled in when a ``g'' packet is received. */
1800 this->actual_register_packet_size
= 0;
1802 /* Should rsa->sizeof_g_packet needs more space than the
1803 default, adjust the size accordingly. Remember that each byte is
1804 encoded as two characters. 32 is the overhead for the packet
1805 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1806 (``$NN:G...#NN'') is a better guess, the below has been padded a
1808 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1809 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1812 /* Get a pointer to the current remote target. If not connected to a
1813 remote target, return NULL. */
1815 static remote_target
*
1816 get_current_remote_target ()
1818 target_ops
*proc_target
= current_inferior ()->process_target ();
1819 return dynamic_cast<remote_target
*> (proc_target
);
1822 /* Return the current allowed size of a remote packet. This is
1823 inferred from the current architecture, and should be used to
1824 limit the length of outgoing packets. */
1826 remote_target::get_remote_packet_size ()
1828 struct remote_state
*rs
= get_remote_state ();
1829 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1831 if (rs
->explicit_packet_size
)
1832 return rs
->explicit_packet_size
;
1834 return rsa
->remote_packet_size
;
1837 static struct packet_reg
*
1838 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1841 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
1845 struct packet_reg
*r
= &rsa
->regs
[regnum
];
1847 gdb_assert (r
->regnum
== regnum
);
1852 static struct packet_reg
*
1853 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1858 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1860 struct packet_reg
*r
= &rsa
->regs
[i
];
1862 if (r
->pnum
== pnum
)
1868 /* Allow the user to specify what sequence to send to the remote
1869 when he requests a program interruption: Although ^C is usually
1870 what remote systems expect (this is the default, here), it is
1871 sometimes preferable to send a break. On other systems such
1872 as the Linux kernel, a break followed by g, which is Magic SysRq g
1873 is required in order to interrupt the execution. */
1874 const char interrupt_sequence_control_c
[] = "Ctrl-C";
1875 const char interrupt_sequence_break
[] = "BREAK";
1876 const char interrupt_sequence_break_g
[] = "BREAK-g";
1877 static const char *const interrupt_sequence_modes
[] =
1879 interrupt_sequence_control_c
,
1880 interrupt_sequence_break
,
1881 interrupt_sequence_break_g
,
1884 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
1887 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
1888 struct cmd_list_element
*c
,
1891 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
1893 _("Send the ASCII ETX character (Ctrl-c) "
1894 "to the remote target to interrupt the "
1895 "execution of the program.\n"));
1896 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
1898 _("send a break signal to the remote target "
1899 "to interrupt the execution of the program.\n"));
1900 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
1902 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1903 "the remote target to interrupt the execution "
1904 "of Linux kernel.\n"));
1906 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
1907 interrupt_sequence_mode
);
1910 /* This boolean variable specifies whether interrupt_sequence is sent
1911 to the remote target when gdb connects to it.
1912 This is mostly needed when you debug the Linux kernel: The Linux kernel
1913 expects BREAK g which is Magic SysRq g for connecting gdb. */
1914 static bool interrupt_on_connect
= false;
1916 /* This variable is used to implement the "set/show remotebreak" commands.
1917 Since these commands are now deprecated in favor of "set/show remote
1918 interrupt-sequence", it no longer has any effect on the code. */
1919 static bool remote_break
;
1922 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
1925 interrupt_sequence_mode
= interrupt_sequence_break
;
1927 interrupt_sequence_mode
= interrupt_sequence_control_c
;
1931 show_remotebreak (struct ui_file
*file
, int from_tty
,
1932 struct cmd_list_element
*c
,
1937 /* This variable sets the number of bits in an address that are to be
1938 sent in a memory ("M" or "m") packet. Normally, after stripping
1939 leading zeros, the entire address would be sent. This variable
1940 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1941 initial implementation of remote.c restricted the address sent in
1942 memory packets to ``host::sizeof long'' bytes - (typically 32
1943 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1944 address was never sent. Since fixing this bug may cause a break in
1945 some remote targets this variable is principally provided to
1946 facilitate backward compatibility. */
1948 static unsigned int remote_address_size
;
1951 /* The default max memory-write-packet-size, when the setting is
1952 "fixed". The 16k is historical. (It came from older GDB's using
1953 alloca for buffers and the knowledge (folklore?) that some hosts
1954 don't cope very well with large alloca calls.) */
1955 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
1957 /* The minimum remote packet size for memory transfers. Ensures we
1958 can write at least one byte. */
1959 #define MIN_MEMORY_PACKET_SIZE 20
1961 /* Get the memory packet size, assuming it is fixed. */
1964 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
1966 gdb_assert (config
->fixed_p
);
1968 if (config
->size
<= 0)
1969 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
1971 return config
->size
;
1974 /* Compute the current size of a read/write packet. Since this makes
1975 use of ``actual_register_packet_size'' the computation is dynamic. */
1978 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
1980 struct remote_state
*rs
= get_remote_state ();
1981 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1984 if (config
->fixed_p
)
1985 what_they_get
= get_fixed_memory_packet_size (config
);
1988 what_they_get
= get_remote_packet_size ();
1989 /* Limit the packet to the size specified by the user. */
1990 if (config
->size
> 0
1991 && what_they_get
> config
->size
)
1992 what_they_get
= config
->size
;
1994 /* Limit it to the size of the targets ``g'' response unless we have
1995 permission from the stub to use a larger packet size. */
1996 if (rs
->explicit_packet_size
== 0
1997 && rsa
->actual_register_packet_size
> 0
1998 && what_they_get
> rsa
->actual_register_packet_size
)
1999 what_they_get
= rsa
->actual_register_packet_size
;
2001 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
2002 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
2004 /* Make sure there is room in the global buffer for this packet
2005 (including its trailing NUL byte). */
2006 if (rs
->buf
.size () < what_they_get
+ 1)
2007 rs
->buf
.resize (2 * what_they_get
);
2009 return what_they_get
;
2012 /* Update the size of a read/write packet. If they user wants
2013 something really big then do a sanity check. */
2016 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
,
2017 bool target_connected
)
2019 int fixed_p
= config
->fixed_p
;
2020 long size
= config
->size
;
2023 error (_("Argument required (integer, \"fixed\" or \"limit\")."));
2024 else if (strcmp (args
, "hard") == 0
2025 || strcmp (args
, "fixed") == 0)
2027 else if (strcmp (args
, "soft") == 0
2028 || strcmp (args
, "limit") == 0)
2034 size
= strtoul (args
, &end
, 0);
2036 error (_("Invalid %s (bad syntax)."), config
->name
);
2038 /* Instead of explicitly capping the size of a packet to or
2039 disallowing it, the user is allowed to set the size to
2040 something arbitrarily large. */
2044 if (fixed_p
&& !config
->fixed_p
)
2046 /* So that the query shows the correct value. */
2047 long query_size
= (size
<= 0
2048 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
2051 if (target_connected
2052 && !query (_("The target may not be able to correctly handle a %s\n"
2053 "of %ld bytes. Change the packet size? "),
2054 config
->name
, query_size
))
2055 error (_("Packet size not changed."));
2056 else if (!target_connected
2057 && !query (_("Future remote targets may not be able to "
2058 "correctly handle a %s\nof %ld bytes. Change the "
2059 "packet size for future remote targets? "),
2060 config
->name
, query_size
))
2061 error (_("Packet size not changed."));
2063 /* Update the config. */
2064 config
->fixed_p
= fixed_p
;
2065 config
->size
= size
;
2067 const char *target_type
= get_target_type_name (target_connected
);
2068 gdb_printf (_("The %s %s is set to \"%s\".\n"), config
->name
, target_type
,
2073 /* Show the memory-read or write-packet size configuration CONFIG of the
2074 target REMOTE. If REMOTE is nullptr, the default configuration for future
2075 remote targets should be passed in CONFIG. */
2078 show_memory_packet_size (memory_packet_config
*config
, remote_target
*remote
)
2080 const char *target_type
= get_target_type_name (remote
!= nullptr);
2082 if (config
->size
== 0)
2083 gdb_printf (_("The %s %s is 0 (default). "), config
->name
, target_type
);
2085 gdb_printf (_("The %s %s is %ld. "), config
->name
, target_type
,
2088 if (config
->fixed_p
)
2089 gdb_printf (_("Packets are fixed at %ld bytes.\n"),
2090 get_fixed_memory_packet_size (config
));
2093 if (remote
!= nullptr)
2094 gdb_printf (_("Packets are limited to %ld bytes.\n"),
2095 remote
->get_memory_packet_size (config
));
2097 gdb_puts ("The actual limit will be further reduced "
2098 "dependent on the target.\n");
2102 /* Configure the memory-write-packet size of the currently selected target. If
2103 no target is available, the default configuration for future remote targets
2107 set_memory_write_packet_size (const char *args
, int from_tty
)
2109 remote_target
*remote
= get_current_remote_target ();
2110 if (remote
!= nullptr)
2112 set_memory_packet_size
2113 (args
, &remote
->m_features
.m_memory_write_packet_config
, true);
2117 memory_packet_config
* config
= &memory_write_packet_config
;
2118 set_memory_packet_size (args
, config
, false);
2122 /* Display the memory-write-packet size of the currently selected target. If
2123 no target is available, the default configuration for future remote targets
2127 show_memory_write_packet_size (const char *args
, int from_tty
)
2129 remote_target
*remote
= get_current_remote_target ();
2130 if (remote
!= nullptr)
2131 show_memory_packet_size (&remote
->m_features
.m_memory_write_packet_config
,
2134 show_memory_packet_size (&memory_write_packet_config
, nullptr);
2137 /* Show the number of hardware watchpoints that can be used. */
2140 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
2141 struct cmd_list_element
*c
,
2144 gdb_printf (file
, _("The maximum number of target hardware "
2145 "watchpoints is %s.\n"), value
);
2148 /* Show the length limit (in bytes) for hardware watchpoints. */
2151 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
2152 struct cmd_list_element
*c
,
2155 gdb_printf (file
, _("The maximum length (in bytes) of a target "
2156 "hardware watchpoint is %s.\n"), value
);
2159 /* Show the number of hardware breakpoints that can be used. */
2162 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
2163 struct cmd_list_element
*c
,
2166 gdb_printf (file
, _("The maximum number of target hardware "
2167 "breakpoints is %s.\n"), value
);
2170 /* Controls the maximum number of characters to display in the debug output
2171 for each remote packet. The remaining characters are omitted. */
2173 static int remote_packet_max_chars
= 512;
2175 /* Show the maximum number of characters to display for each remote packet
2176 when remote debugging is enabled. */
2179 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
2180 struct cmd_list_element
*c
,
2183 gdb_printf (file
, _("Number of remote packet characters to "
2184 "display is %s.\n"), value
);
2188 remote_target::get_memory_write_packet_size ()
2190 return get_memory_packet_size (&m_features
.m_memory_write_packet_config
);
2193 /* Configure the memory-read-packet size of the currently selected target. If
2194 no target is available, the default configuration for future remote targets
2198 set_memory_read_packet_size (const char *args
, int from_tty
)
2200 remote_target
*remote
= get_current_remote_target ();
2201 if (remote
!= nullptr)
2202 set_memory_packet_size
2203 (args
, &remote
->m_features
.m_memory_read_packet_config
, true);
2206 memory_packet_config
* config
= &memory_read_packet_config
;
2207 set_memory_packet_size (args
, config
, false);
2212 /* Display the memory-read-packet size of the currently selected target. If
2213 no target is available, the default configuration for future remote targets
2217 show_memory_read_packet_size (const char *args
, int from_tty
)
2219 remote_target
*remote
= get_current_remote_target ();
2220 if (remote
!= nullptr)
2221 show_memory_packet_size (&remote
->m_features
.m_memory_read_packet_config
,
2224 show_memory_packet_size (&memory_read_packet_config
, nullptr);
2228 remote_target::get_memory_read_packet_size ()
2230 long size
= get_memory_packet_size (&m_features
.m_memory_read_packet_config
);
2232 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
2233 extra buffer size argument before the memory read size can be
2234 increased beyond this. */
2235 if (size
> get_remote_packet_size ())
2236 size
= get_remote_packet_size ();
2240 static enum packet_support
packet_config_support (const packet_config
*config
);
2244 set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
2245 cmd_list_element
*c
)
2247 remote_target
*remote
= get_current_remote_target ();
2248 gdb_assert (c
->var
.has_value ());
2250 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2251 const int packet_idx
= std::distance (remote_protocol_packets
,
2254 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2256 const char *name
= packets_descriptions
[packet_idx
].name
;
2257 const auto_boolean value
= c
->var
->get
<auto_boolean
> ();
2258 const char *support
= get_packet_support_name (value
);
2259 const char *target_type
= get_target_type_name (remote
!= nullptr);
2261 if (remote
!= nullptr)
2262 remote
->m_features
.m_protocol_packets
[packet_idx
].detect
= value
;
2264 remote_protocol_packets
[packet_idx
].detect
= value
;
2266 gdb_printf (_("Support for the '%s' packet %s is set to \"%s\".\n"), name
,
2267 target_type
, support
);
2271 internal_error (_("Could not find config for %s"), c
->name
);
2275 show_packet_config_cmd (ui_file
*file
, const unsigned int which_packet
,
2276 remote_target
*remote
)
2278 const char *support
= "internal-error";
2279 const char *target_type
= get_target_type_name (remote
!= nullptr);
2281 packet_config
*config
;
2282 if (remote
!= nullptr)
2283 config
= &remote
->m_features
.m_protocol_packets
[which_packet
];
2285 config
= &remote_protocol_packets
[which_packet
];
2287 switch (packet_config_support (config
))
2290 support
= "enabled";
2292 case PACKET_DISABLE
:
2293 support
= "disabled";
2295 case PACKET_SUPPORT_UNKNOWN
:
2296 support
= "unknown";
2299 switch (config
->detect
)
2301 case AUTO_BOOLEAN_AUTO
:
2303 _("Support for the '%s' packet %s is \"auto\", "
2305 packets_descriptions
[which_packet
].name
, target_type
,
2308 case AUTO_BOOLEAN_TRUE
:
2309 case AUTO_BOOLEAN_FALSE
:
2311 _("Support for the '%s' packet %s is \"%s\".\n"),
2312 packets_descriptions
[which_packet
].name
, target_type
,
2313 get_packet_support_name (config
->detect
));
2319 add_packet_config_cmd (const unsigned int which_packet
, const char *name
,
2320 const char *title
, int legacy
)
2322 packets_descriptions
[which_packet
].name
= name
;
2323 packets_descriptions
[which_packet
].title
= title
;
2325 packet_config
*config
= &remote_protocol_packets
[which_packet
];
2327 gdb::unique_xmalloc_ptr
<char> set_doc
2328 = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
2330 gdb::unique_xmalloc_ptr
<char> show_doc
2331 = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
2333 /* set/show TITLE-packet {auto,on,off} */
2334 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
2335 set_show_commands cmds
2336 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
2337 &config
->detect
, set_doc
.get (),
2338 show_doc
.get (), NULL
, /* help_doc */
2339 set_remote_protocol_packet_cmd
,
2340 show_remote_protocol_packet_cmd
,
2341 &remote_set_cmdlist
, &remote_show_cmdlist
);
2342 cmds
.show
->set_context (config
);
2343 cmds
.set
->set_context (config
);
2345 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
2348 /* It's not clear who should take ownership of the LEGACY_NAME string
2349 created below, so, for now, place the string into a static vector
2350 which ensures the strings is released when GDB exits. */
2351 static std::vector
<gdb::unique_xmalloc_ptr
<char>> legacy_names
;
2352 gdb::unique_xmalloc_ptr
<char> legacy_name
2353 = xstrprintf ("%s-packet", name
);
2354 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
2355 &remote_set_cmdlist
);
2356 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
2357 &remote_show_cmdlist
);
2358 legacy_names
.emplace_back (std::move (legacy_name
));
2362 static enum packet_result
2363 packet_check_result (const char *buf
)
2367 /* The stub recognized the packet request. Check that the
2368 operation succeeded. */
2370 && isxdigit (buf
[1]) && isxdigit (buf
[2])
2372 /* "Enn" - definitely an error. */
2373 return PACKET_ERROR
;
2375 /* Always treat "E." as an error. This will be used for
2376 more verbose error messages, such as E.memtypes. */
2377 if (buf
[0] == 'E' && buf
[1] == '.')
2378 return PACKET_ERROR
;
2380 /* The packet may or may not be OK. Just assume it is. */
2384 /* The stub does not support the packet. */
2385 return PACKET_UNKNOWN
;
2388 static enum packet_result
2389 packet_check_result (const gdb::char_vector
&buf
)
2391 return packet_check_result (buf
.data ());
2395 remote_features::packet_ok (const char *buf
, const int which_packet
)
2397 packet_config
*config
= &m_protocol_packets
[which_packet
];
2398 packet_description
*descr
= &packets_descriptions
[which_packet
];
2400 enum packet_result result
;
2402 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2403 && config
->support
== PACKET_DISABLE
)
2404 internal_error (_("packet_ok: attempt to use a disabled packet"));
2406 result
= packet_check_result (buf
);
2411 /* The stub recognized the packet request. */
2412 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2414 remote_debug_printf ("Packet %s (%s) is supported",
2415 descr
->name
, descr
->title
);
2416 config
->support
= PACKET_ENABLE
;
2419 case PACKET_UNKNOWN
:
2420 /* The stub does not support the packet. */
2421 if (config
->detect
== AUTO_BOOLEAN_AUTO
2422 && config
->support
== PACKET_ENABLE
)
2424 /* If the stub previously indicated that the packet was
2425 supported then there is a protocol error. */
2426 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2427 descr
->name
, descr
->title
);
2429 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2431 /* The user set it wrong. */
2432 error (_("Enabled packet %s (%s) not recognized by stub"),
2433 descr
->name
, descr
->title
);
2436 remote_debug_printf ("Packet %s (%s) is NOT supported", descr
->name
,
2438 config
->support
= PACKET_DISABLE
;
2446 remote_features::packet_ok (const gdb::char_vector
&buf
, const int which_packet
)
2448 return packet_ok (buf
.data (), which_packet
);
2451 /* Returns whether a given packet or feature is supported. This takes
2452 into account the state of the corresponding "set remote foo-packet"
2453 command, which may be used to bypass auto-detection. */
2455 static enum packet_support
2456 packet_config_support (const packet_config
*config
)
2458 switch (config
->detect
)
2460 case AUTO_BOOLEAN_TRUE
:
2461 return PACKET_ENABLE
;
2462 case AUTO_BOOLEAN_FALSE
:
2463 return PACKET_DISABLE
;
2464 case AUTO_BOOLEAN_AUTO
:
2465 return config
->support
;
2467 gdb_assert_not_reached ("bad switch");
2472 remote_features::packet_support (int packet
) const
2474 const packet_config
*config
= &m_protocol_packets
[packet
];
2475 return packet_config_support (config
);
2479 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2480 struct cmd_list_element
*c
,
2483 remote_target
*remote
= get_current_remote_target ();
2484 gdb_assert (c
->var
.has_value ());
2486 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2487 const int packet_idx
= std::distance (remote_protocol_packets
,
2490 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2492 show_packet_config_cmd (file
, packet_idx
, remote
);
2495 internal_error (_("Could not find config for %s"), c
->name
);
2498 /* Should we try one of the 'Z' requests? */
2502 Z_PACKET_SOFTWARE_BP
,
2503 Z_PACKET_HARDWARE_BP
,
2510 /* For compatibility with older distributions. Provide a ``set remote
2511 Z-packet ...'' command that updates all the Z packet types. */
2513 static enum auto_boolean remote_Z_packet_detect
;
2516 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2517 struct cmd_list_element
*c
)
2519 remote_target
*remote
= get_current_remote_target ();
2522 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2524 if (remote
!= nullptr)
2525 remote
->m_features
.m_protocol_packets
[PACKET_Z0
+ i
].detect
2526 = remote_Z_packet_detect
;
2528 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2531 const char *support
= get_packet_support_name (remote_Z_packet_detect
);
2532 const char *target_type
= get_target_type_name (remote
!= nullptr);
2533 gdb_printf (_("Use of Z packets %s is set to \"%s\".\n"), target_type
,
2539 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2540 struct cmd_list_element
*c
,
2543 remote_target
*remote
= get_current_remote_target ();
2546 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2547 show_packet_config_cmd (file
, PACKET_Z0
+ i
, remote
);
2550 /* Insert fork catchpoint target routine. If fork events are enabled
2551 then return success, nothing more to do. */
2554 remote_target::insert_fork_catchpoint (int pid
)
2556 return !m_features
.remote_fork_event_p ();
2559 /* Remove fork catchpoint target routine. Nothing to do, just
2563 remote_target::remove_fork_catchpoint (int pid
)
2568 /* Insert vfork catchpoint target routine. If vfork events are enabled
2569 then return success, nothing more to do. */
2572 remote_target::insert_vfork_catchpoint (int pid
)
2574 return !m_features
.remote_vfork_event_p ();
2577 /* Remove vfork catchpoint target routine. Nothing to do, just
2581 remote_target::remove_vfork_catchpoint (int pid
)
2586 /* Insert exec catchpoint target routine. If exec events are
2587 enabled, just return success. */
2590 remote_target::insert_exec_catchpoint (int pid
)
2592 return !m_features
.remote_exec_event_p ();
2595 /* Remove exec catchpoint target routine. Nothing to do, just
2599 remote_target::remove_exec_catchpoint (int pid
)
2606 /* Take advantage of the fact that the TID field is not used, to tag
2607 special ptids with it set to != 0. */
2608 static const ptid_t
magic_null_ptid (42000, -1, 1);
2609 static const ptid_t
not_sent_ptid (42000, -2, 1);
2610 static const ptid_t
any_thread_ptid (42000, 0, 1);
2612 /* Find out if the stub attached to PID (and hence GDB should offer to
2613 detach instead of killing it when bailing out). */
2616 remote_target::remote_query_attached (int pid
)
2618 struct remote_state
*rs
= get_remote_state ();
2619 size_t size
= get_remote_packet_size ();
2621 if (m_features
.packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2624 if (m_features
.remote_multi_process_p ())
2625 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2627 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2630 getpkt (&rs
->buf
, 0);
2632 switch (m_features
.packet_ok (rs
->buf
, PACKET_qAttached
))
2635 if (strcmp (rs
->buf
.data (), "1") == 0)
2639 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
2641 case PACKET_UNKNOWN
:
2648 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2649 has been invented by GDB, instead of reported by the target. Since
2650 we can be connected to a remote system before before knowing about
2651 any inferior, mark the target with execution when we find the first
2652 inferior. If ATTACHED is 1, then we had just attached to this
2653 inferior. If it is 0, then we just created this inferior. If it
2654 is -1, then try querying the remote stub to find out if it had
2655 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2656 attempt to open this inferior's executable as the main executable
2657 if no main executable is open already. */
2660 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2663 struct inferior
*inf
;
2665 /* Check whether this process we're learning about is to be
2666 considered attached, or if is to be considered to have been
2667 spawned by the stub. */
2669 attached
= remote_query_attached (pid
);
2671 if (gdbarch_has_global_solist (target_gdbarch ()))
2673 /* If the target shares code across all inferiors, then every
2674 attach adds a new inferior. */
2675 inf
= add_inferior (pid
);
2677 /* ... and every inferior is bound to the same program space.
2678 However, each inferior may still have its own address
2680 inf
->aspace
= maybe_new_address_space ();
2681 inf
->pspace
= current_program_space
;
2685 /* In the traditional debugging scenario, there's a 1-1 match
2686 between program/address spaces. We simply bind the inferior
2687 to the program space's address space. */
2688 inf
= current_inferior ();
2690 /* However, if the current inferior is already bound to a
2691 process, find some other empty inferior. */
2695 for (inferior
*it
: all_inferiors ())
2704 /* Since all inferiors were already bound to a process, add
2706 inf
= add_inferior_with_spaces ();
2708 switch_to_inferior_no_thread (inf
);
2709 inf
->push_target (this);
2710 inferior_appeared (inf
, pid
);
2713 inf
->attach_flag
= attached
;
2714 inf
->fake_pid_p
= fake_pid_p
;
2716 /* If no main executable is currently open then attempt to
2717 open the file that was executed to create this inferior. */
2718 if (try_open_exec
&& get_exec_file (0) == NULL
)
2719 exec_file_locate_attach (pid
, 0, 1);
2721 /* Check for exec file mismatch, and let the user solve it. */
2722 validate_exec_file (1);
2727 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2728 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2731 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2732 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2733 remote_state::starting_up flag) is true then the new thread is added
2734 silently, otherwise the new thread will be announced to the user. */
2737 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2740 struct remote_state
*rs
= get_remote_state ();
2741 struct thread_info
*thread
;
2743 /* GDB historically didn't pull threads in the initial connection
2744 setup. If the remote target doesn't even have a concept of
2745 threads (e.g., a bare-metal target), even if internally we
2746 consider that a single-threaded target, mentioning a new thread
2747 might be confusing to the user. Be silent then, preserving the
2748 age old behavior. */
2749 if (rs
->starting_up
|| silent_p
)
2750 thread
= add_thread_silent (this, ptid
);
2752 thread
= add_thread (this, ptid
);
2754 /* We start by assuming threads are resumed. That state then gets updated
2755 when we process a matching stop reply. */
2756 get_remote_thread_info (thread
)->set_resumed ();
2758 set_executing (this, ptid
, executing
);
2759 set_running (this, ptid
, running
);
2764 /* Come here when we learn about a thread id from the remote target.
2765 It may be the first time we hear about such thread, so take the
2766 opportunity to add it to GDB's thread list. In case this is the
2767 first time we're noticing its corresponding inferior, add it to
2768 GDB's inferior list as well. EXECUTING indicates whether the
2769 thread is (internally) executing or stopped. */
2772 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2774 /* In non-stop mode, we assume new found threads are (externally)
2775 running until proven otherwise with a stop reply. In all-stop,
2776 we can only get here if all threads are stopped. */
2777 bool running
= target_is_non_stop_p ();
2779 /* If this is a new thread, add it to GDB's thread list.
2780 If we leave it up to WFI to do this, bad things will happen. */
2782 thread_info
*tp
= this->find_thread (currthread
);
2783 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2785 /* We're seeing an event on a thread id we knew had exited.
2786 This has to be a new thread reusing the old id. Add it. */
2787 remote_add_thread (currthread
, running
, executing
, false);
2791 if (!in_thread_list (this, currthread
))
2793 struct inferior
*inf
= NULL
;
2794 int pid
= currthread
.pid ();
2796 if (inferior_ptid
.is_pid ()
2797 && pid
== inferior_ptid
.pid ())
2799 /* inferior_ptid has no thread member yet. This can happen
2800 with the vAttach -> remote_wait,"TAAthread:" path if the
2801 stub doesn't support qC. This is the first stop reported
2802 after an attach, so this is the main thread. Update the
2803 ptid in the thread list. */
2804 if (in_thread_list (this, ptid_t (pid
)))
2805 thread_change_ptid (this, inferior_ptid
, currthread
);
2809 = remote_add_thread (currthread
, running
, executing
, false);
2810 switch_to_thread (thr
);
2815 if (magic_null_ptid
== inferior_ptid
)
2817 /* inferior_ptid is not set yet. This can happen with the
2818 vRun -> remote_wait,"TAAthread:" path if the stub
2819 doesn't support qC. This is the first stop reported
2820 after an attach, so this is the main thread. Update the
2821 ptid in the thread list. */
2822 thread_change_ptid (this, inferior_ptid
, currthread
);
2826 /* When connecting to a target remote, or to a target
2827 extended-remote which already was debugging an inferior, we
2828 may not know about it yet. Add it before adding its child
2829 thread, so notifications are emitted in a sensible order. */
2830 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
2832 bool fake_pid_p
= !m_features
.remote_multi_process_p ();
2834 inf
= remote_add_inferior (fake_pid_p
,
2835 currthread
.pid (), -1, 1);
2838 /* This is really a new thread. Add it. */
2839 thread_info
*new_thr
2840 = remote_add_thread (currthread
, running
, executing
, false);
2842 /* If we found a new inferior, let the common code do whatever
2843 it needs to with it (e.g., read shared libraries, insert
2844 breakpoints), unless we're just setting up an all-stop
2848 struct remote_state
*rs
= get_remote_state ();
2850 if (!rs
->starting_up
)
2851 notice_new_inferior (new_thr
, executing
, 0);
2856 /* Return THREAD's private thread data, creating it if necessary. */
2858 static remote_thread_info
*
2859 get_remote_thread_info (thread_info
*thread
)
2861 gdb_assert (thread
!= NULL
);
2863 if (thread
->priv
== NULL
)
2864 thread
->priv
.reset (new remote_thread_info
);
2866 return gdb::checked_static_cast
<remote_thread_info
*> (thread
->priv
.get ());
2869 /* Return PTID's private thread data, creating it if necessary. */
2871 static remote_thread_info
*
2872 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
2874 thread_info
*thr
= target
->find_thread (ptid
);
2875 return get_remote_thread_info (thr
);
2878 /* Call this function as a result of
2879 1) A halt indication (T packet) containing a thread id
2880 2) A direct query of currthread
2881 3) Successful execution of set thread */
2884 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
2886 rs
->general_thread
= currthread
;
2889 /* If 'QPassSignals' is supported, tell the remote stub what signals
2890 it can simply pass through to the inferior without reporting. */
2893 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2895 if (m_features
.packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
2897 char *pass_packet
, *p
;
2899 struct remote_state
*rs
= get_remote_state ();
2901 gdb_assert (pass_signals
.size () < 256);
2902 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2904 if (pass_signals
[i
])
2907 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
2908 strcpy (pass_packet
, "QPassSignals:");
2909 p
= pass_packet
+ strlen (pass_packet
);
2910 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2912 if (pass_signals
[i
])
2915 *p
++ = tohex (i
>> 4);
2916 *p
++ = tohex (i
& 15);
2925 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
2927 putpkt (pass_packet
);
2928 getpkt (&rs
->buf
, 0);
2929 m_features
.packet_ok (rs
->buf
, PACKET_QPassSignals
);
2930 xfree (rs
->last_pass_packet
);
2931 rs
->last_pass_packet
= pass_packet
;
2934 xfree (pass_packet
);
2938 /* If 'QCatchSyscalls' is supported, tell the remote stub
2939 to report syscalls to GDB. */
2942 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
2943 gdb::array_view
<const int> syscall_counts
)
2945 const char *catch_packet
;
2946 enum packet_result result
;
2949 if (m_features
.packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
2951 /* Not supported. */
2955 if (needed
&& any_count
== 0)
2957 /* Count how many syscalls are to be caught. */
2958 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2960 if (syscall_counts
[i
] != 0)
2965 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
2966 pid
, needed
, any_count
, n_sysno
);
2968 std::string built_packet
;
2971 /* Prepare a packet with the sysno list, assuming max 8+1
2972 characters for a sysno. If the resulting packet size is too
2973 big, fallback on the non-selective packet. */
2974 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
2975 built_packet
.reserve (maxpktsz
);
2976 built_packet
= "QCatchSyscalls:1";
2979 /* Add in each syscall to be caught. */
2980 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2982 if (syscall_counts
[i
] != 0)
2983 string_appendf (built_packet
, ";%zx", i
);
2986 if (built_packet
.size () > get_remote_packet_size ())
2988 /* catch_packet too big. Fallback to less efficient
2989 non selective mode, with GDB doing the filtering. */
2990 catch_packet
= "QCatchSyscalls:1";
2993 catch_packet
= built_packet
.c_str ();
2996 catch_packet
= "QCatchSyscalls:0";
2998 struct remote_state
*rs
= get_remote_state ();
3000 putpkt (catch_packet
);
3001 getpkt (&rs
->buf
, 0);
3002 result
= m_features
.packet_ok (rs
->buf
, PACKET_QCatchSyscalls
);
3003 if (result
== PACKET_OK
)
3009 /* If 'QProgramSignals' is supported, tell the remote stub what
3010 signals it should pass through to the inferior when detaching. */
3013 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
3015 if (m_features
.packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
3019 struct remote_state
*rs
= get_remote_state ();
3021 gdb_assert (signals
.size () < 256);
3022 for (size_t i
= 0; i
< signals
.size (); i
++)
3027 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
3028 strcpy (packet
, "QProgramSignals:");
3029 p
= packet
+ strlen (packet
);
3030 for (size_t i
= 0; i
< signals
.size (); i
++)
3032 if (signal_pass_state (i
))
3035 *p
++ = tohex (i
>> 4);
3036 *p
++ = tohex (i
& 15);
3045 if (!rs
->last_program_signals_packet
3046 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
3049 getpkt (&rs
->buf
, 0);
3050 m_features
.packet_ok (rs
->buf
, PACKET_QProgramSignals
);
3051 xfree (rs
->last_program_signals_packet
);
3052 rs
->last_program_signals_packet
= packet
;
3059 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
3060 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
3061 thread. If GEN is set, set the general thread, if not, then set
3062 the step/continue thread. */
3064 remote_target::set_thread (ptid_t ptid
, int gen
)
3066 struct remote_state
*rs
= get_remote_state ();
3067 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
3068 char *buf
= rs
->buf
.data ();
3069 char *endbuf
= buf
+ get_remote_packet_size ();
3075 *buf
++ = gen
? 'g' : 'c';
3076 if (ptid
== magic_null_ptid
)
3077 xsnprintf (buf
, endbuf
- buf
, "0");
3078 else if (ptid
== any_thread_ptid
)
3079 xsnprintf (buf
, endbuf
- buf
, "0");
3080 else if (ptid
== minus_one_ptid
)
3081 xsnprintf (buf
, endbuf
- buf
, "-1");
3083 write_ptid (buf
, endbuf
, ptid
);
3085 getpkt (&rs
->buf
, 0);
3087 rs
->general_thread
= ptid
;
3089 rs
->continue_thread
= ptid
;
3093 remote_target::set_general_thread (ptid_t ptid
)
3095 set_thread (ptid
, 1);
3099 remote_target::set_continue_thread (ptid_t ptid
)
3101 set_thread (ptid
, 0);
3104 /* Change the remote current process. Which thread within the process
3105 ends up selected isn't important, as long as it is the same process
3106 as what INFERIOR_PTID points to.
3108 This comes from that fact that there is no explicit notion of
3109 "selected process" in the protocol. The selected process for
3110 general operations is the process the selected general thread
3114 remote_target::set_general_process ()
3116 /* If the remote can't handle multiple processes, don't bother. */
3117 if (!m_features
.remote_multi_process_p ())
3120 remote_state
*rs
= get_remote_state ();
3122 /* We only need to change the remote current thread if it's pointing
3123 at some other process. */
3124 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
3125 set_general_thread (inferior_ptid
);
3129 /* Return nonzero if this is the main thread that we made up ourselves
3130 to model non-threaded targets as single-threaded. */
3133 remote_thread_always_alive (ptid_t ptid
)
3135 if (ptid
== magic_null_ptid
)
3136 /* The main thread is always alive. */
3139 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
3140 /* The main thread is always alive. This can happen after a
3141 vAttach, if the remote side doesn't support
3148 /* Return nonzero if the thread PTID is still alive on the remote
3152 remote_target::thread_alive (ptid_t ptid
)
3154 struct remote_state
*rs
= get_remote_state ();
3157 /* Check if this is a thread that we made up ourselves to model
3158 non-threaded targets as single-threaded. */
3159 if (remote_thread_always_alive (ptid
))
3162 p
= rs
->buf
.data ();
3163 endp
= p
+ get_remote_packet_size ();
3166 write_ptid (p
, endp
, ptid
);
3169 getpkt (&rs
->buf
, 0);
3170 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
3173 /* Return a pointer to a thread name if we know it and NULL otherwise.
3174 The thread_info object owns the memory for the name. */
3177 remote_target::thread_name (struct thread_info
*info
)
3179 if (info
->priv
!= NULL
)
3181 const std::string
&name
= get_remote_thread_info (info
)->name
;
3182 return !name
.empty () ? name
.c_str () : NULL
;
3188 /* About these extended threadlist and threadinfo packets. They are
3189 variable length packets but, the fields within them are often fixed
3190 length. They are redundant enough to send over UDP as is the
3191 remote protocol in general. There is a matching unit test module
3194 /* WARNING: This threadref data structure comes from the remote O.S.,
3195 libstub protocol encoding, and remote.c. It is not particularly
3198 /* Right now, the internal structure is int. We want it to be bigger.
3199 Plan to fix this. */
3201 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3203 /* gdb_ext_thread_info is an internal GDB data structure which is
3204 equivalent to the reply of the remote threadinfo packet. */
3206 struct gdb_ext_thread_info
3208 threadref threadid
; /* External form of thread reference. */
3209 int active
; /* Has state interesting to GDB?
3211 char display
[256]; /* Brief state display, name,
3212 blocked/suspended. */
3213 char shortname
[32]; /* To be used to name threads. */
3214 char more_display
[256]; /* Long info, statistics, queue depth,
3218 /* The volume of remote transfers can be limited by submitting
3219 a mask containing bits specifying the desired information.
3220 Use a union of these values as the 'selection' parameter to
3221 get_thread_info. FIXME: Make these TAG names more thread specific. */
3223 #define TAG_THREADID 1
3224 #define TAG_EXISTS 2
3225 #define TAG_DISPLAY 4
3226 #define TAG_THREADNAME 8
3227 #define TAG_MOREDISPLAY 16
3229 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3231 static const char *unpack_nibble (const char *buf
, int *val
);
3233 static const char *unpack_byte (const char *buf
, int *value
);
3235 static char *pack_int (char *buf
, int value
);
3237 static const char *unpack_int (const char *buf
, int *value
);
3239 static const char *unpack_string (const char *src
, char *dest
, int length
);
3241 static char *pack_threadid (char *pkt
, threadref
*id
);
3243 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3245 void int_to_threadref (threadref
*id
, int value
);
3247 static int threadref_to_int (threadref
*ref
);
3249 static void copy_threadref (threadref
*dest
, threadref
*src
);
3251 static int threadmatch (threadref
*dest
, threadref
*src
);
3253 static char *pack_threadinfo_request (char *pkt
, int mode
,
3256 static char *pack_threadlist_request (char *pkt
, int startflag
,
3258 threadref
*nextthread
);
3260 static int remote_newthread_step (threadref
*ref
, void *context
);
3263 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3264 buffer we're allowed to write to. Returns
3265 BUF+CHARACTERS_WRITTEN. */
3268 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3272 if (m_features
.remote_multi_process_p ())
3276 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3278 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3282 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3284 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3289 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3290 last parsed char. Returns null_ptid if no thread id is found, and
3291 throws an error if the thread id has an invalid format. */
3294 read_ptid (const char *buf
, const char **obuf
)
3296 const char *p
= buf
;
3298 ULONGEST pid
= 0, tid
= 0;
3302 /* Multi-process ptid. */
3303 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3305 error (_("invalid remote ptid: %s"), p
);
3308 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3311 return ptid_t (pid
, tid
);
3314 /* No multi-process. Just a tid. */
3315 pp
= unpack_varlen_hex (p
, &tid
);
3317 /* Return null_ptid when no thread id is found. */
3325 /* Since the stub is not sending a process id, default to what's
3326 current_inferior, unless it doesn't have a PID yet. If so,
3327 then since there's no way to know the pid of the reported
3328 threads, use the magic number. */
3329 inferior
*inf
= current_inferior ();
3331 pid
= magic_null_ptid
.pid ();
3337 return ptid_t (pid
, tid
);
3343 if (ch
>= 'a' && ch
<= 'f')
3344 return ch
- 'a' + 10;
3345 if (ch
>= '0' && ch
<= '9')
3347 if (ch
>= 'A' && ch
<= 'F')
3348 return ch
- 'A' + 10;
3353 stub_unpack_int (const char *buff
, int fieldlength
)
3360 nibble
= stubhex (*buff
++);
3364 retval
= retval
<< 4;
3370 unpack_nibble (const char *buf
, int *val
)
3372 *val
= fromhex (*buf
++);
3377 unpack_byte (const char *buf
, int *value
)
3379 *value
= stub_unpack_int (buf
, 2);
3384 pack_int (char *buf
, int value
)
3386 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3387 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3388 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3389 buf
= pack_hex_byte (buf
, (value
& 0xff));
3394 unpack_int (const char *buf
, int *value
)
3396 *value
= stub_unpack_int (buf
, 8);
3400 #if 0 /* Currently unused, uncomment when needed. */
3401 static char *pack_string (char *pkt
, char *string
);
3404 pack_string (char *pkt
, char *string
)
3409 len
= strlen (string
);
3411 len
= 200; /* Bigger than most GDB packets, junk??? */
3412 pkt
= pack_hex_byte (pkt
, len
);
3416 if ((ch
== '\0') || (ch
== '#'))
3417 ch
= '*'; /* Protect encapsulation. */
3422 #endif /* 0 (unused) */
3425 unpack_string (const char *src
, char *dest
, int length
)
3434 pack_threadid (char *pkt
, threadref
*id
)
3437 unsigned char *altid
;
3439 altid
= (unsigned char *) id
;
3440 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3442 pkt
= pack_hex_byte (pkt
, *altid
++);
3448 unpack_threadid (const char *inbuf
, threadref
*id
)
3451 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3454 altref
= (char *) id
;
3456 while (inbuf
< limit
)
3458 x
= stubhex (*inbuf
++);
3459 y
= stubhex (*inbuf
++);
3460 *altref
++ = (x
<< 4) | y
;
3465 /* Externally, threadrefs are 64 bits but internally, they are still
3466 ints. This is due to a mismatch of specifications. We would like
3467 to use 64bit thread references internally. This is an adapter
3471 int_to_threadref (threadref
*id
, int value
)
3473 unsigned char *scan
;
3475 scan
= (unsigned char *) id
;
3481 *scan
++ = (value
>> 24) & 0xff;
3482 *scan
++ = (value
>> 16) & 0xff;
3483 *scan
++ = (value
>> 8) & 0xff;
3484 *scan
++ = (value
& 0xff);
3488 threadref_to_int (threadref
*ref
)
3491 unsigned char *scan
;
3497 value
= (value
<< 8) | ((*scan
++) & 0xff);
3502 copy_threadref (threadref
*dest
, threadref
*src
)
3505 unsigned char *csrc
, *cdest
;
3507 csrc
= (unsigned char *) src
;
3508 cdest
= (unsigned char *) dest
;
3515 threadmatch (threadref
*dest
, threadref
*src
)
3517 /* Things are broken right now, so just assume we got a match. */
3519 unsigned char *srcp
, *destp
;
3521 srcp
= (char *) src
;
3522 destp
= (char *) dest
;
3526 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3533 threadid:1, # always request threadid
3540 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3543 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3545 *pkt
++ = 'q'; /* Info Query */
3546 *pkt
++ = 'P'; /* process or thread info */
3547 pkt
= pack_int (pkt
, mode
); /* mode */
3548 pkt
= pack_threadid (pkt
, id
); /* threadid */
3549 *pkt
= '\0'; /* terminate */
3553 /* These values tag the fields in a thread info response packet. */
3554 /* Tagging the fields allows us to request specific fields and to
3555 add more fields as time goes by. */
3557 #define TAG_THREADID 1 /* Echo the thread identifier. */
3558 #define TAG_EXISTS 2 /* Is this process defined enough to
3559 fetch registers and its stack? */
3560 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3561 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3562 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3566 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3567 threadref
*expectedref
,
3568 gdb_ext_thread_info
*info
)
3570 struct remote_state
*rs
= get_remote_state ();
3574 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3577 /* info->threadid = 0; FIXME: implement zero_threadref. */
3579 info
->display
[0] = '\0';
3580 info
->shortname
[0] = '\0';
3581 info
->more_display
[0] = '\0';
3583 /* Assume the characters indicating the packet type have been
3585 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3586 pkt
= unpack_threadid (pkt
, &ref
);
3589 warning (_("Incomplete response to threadinfo request."));
3590 if (!threadmatch (&ref
, expectedref
))
3591 { /* This is an answer to a different request. */
3592 warning (_("ERROR RMT Thread info mismatch."));
3595 copy_threadref (&info
->threadid
, &ref
);
3597 /* Loop on tagged fields , try to bail if something goes wrong. */
3599 /* Packets are terminated with nulls. */
3600 while ((pkt
< limit
) && mask
&& *pkt
)
3602 pkt
= unpack_int (pkt
, &tag
); /* tag */
3603 pkt
= unpack_byte (pkt
, &length
); /* length */
3604 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3606 warning (_("ERROR RMT: threadinfo tag mismatch."));
3610 if (tag
== TAG_THREADID
)
3614 warning (_("ERROR RMT: length of threadid is not 16."));
3618 pkt
= unpack_threadid (pkt
, &ref
);
3619 mask
= mask
& ~TAG_THREADID
;
3622 if (tag
== TAG_EXISTS
)
3624 info
->active
= stub_unpack_int (pkt
, length
);
3626 mask
= mask
& ~(TAG_EXISTS
);
3629 warning (_("ERROR RMT: 'exists' length too long."));
3635 if (tag
== TAG_THREADNAME
)
3637 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3638 mask
= mask
& ~TAG_THREADNAME
;
3641 if (tag
== TAG_DISPLAY
)
3643 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3644 mask
= mask
& ~TAG_DISPLAY
;
3647 if (tag
== TAG_MOREDISPLAY
)
3649 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3650 mask
= mask
& ~TAG_MOREDISPLAY
;
3653 warning (_("ERROR RMT: unknown thread info tag."));
3654 break; /* Not a tag we know about. */
3660 remote_target::remote_get_threadinfo (threadref
*threadid
,
3662 gdb_ext_thread_info
*info
)
3664 struct remote_state
*rs
= get_remote_state ();
3667 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3669 getpkt (&rs
->buf
, 0);
3671 if (rs
->buf
[0] == '\0')
3674 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3679 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3682 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3683 threadref
*nextthread
)
3685 *pkt
++ = 'q'; /* info query packet */
3686 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3687 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3688 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3689 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3694 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3697 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3698 threadref
*original_echo
,
3699 threadref
*resultlist
,
3702 struct remote_state
*rs
= get_remote_state ();
3703 int count
, resultcount
, done
;
3706 /* Assume the 'q' and 'M chars have been stripped. */
3707 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3708 /* done parse past here */
3709 pkt
= unpack_byte (pkt
, &count
); /* count field */
3710 pkt
= unpack_nibble (pkt
, &done
);
3711 /* The first threadid is the argument threadid. */
3712 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3713 while ((count
-- > 0) && (pkt
< limit
))
3715 pkt
= unpack_threadid (pkt
, resultlist
++);
3716 if (resultcount
++ >= result_limit
)
3724 /* Fetch the next batch of threads from the remote. Returns -1 if the
3725 qL packet is not supported, 0 on error and 1 on success. */
3728 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3729 int result_limit
, int *done
, int *result_count
,
3730 threadref
*threadlist
)
3732 struct remote_state
*rs
= get_remote_state ();
3735 /* Truncate result limit to be smaller than the packet size. */
3736 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3737 >= get_remote_packet_size ())
3738 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3740 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3743 getpkt (&rs
->buf
, 0);
3744 if (rs
->buf
[0] == '\0')
3746 /* Packet not supported. */
3751 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3752 &rs
->echo_nextthread
, threadlist
, done
);
3754 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3756 /* FIXME: This is a good reason to drop the packet. */
3757 /* Possibly, there is a duplicate response. */
3759 retransmit immediatly - race conditions
3760 retransmit after timeout - yes
3762 wait for packet, then exit
3764 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3765 return 0; /* I choose simply exiting. */
3767 if (*result_count
<= 0)
3771 warning (_("RMT ERROR : failed to get remote thread list."));
3774 return result
; /* break; */
3776 if (*result_count
> result_limit
)
3779 warning (_("RMT ERROR: threadlist response longer than requested."));
3785 /* Fetch the list of remote threads, with the qL packet, and call
3786 STEPFUNCTION for each thread found. Stops iterating and returns 1
3787 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3788 STEPFUNCTION returns false. If the packet is not supported,
3792 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3793 void *context
, int looplimit
)
3795 struct remote_state
*rs
= get_remote_state ();
3796 int done
, i
, result_count
;
3804 if (loopcount
++ > looplimit
)
3807 warning (_("Remote fetch threadlist -infinite loop-."));
3810 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3811 MAXTHREADLISTRESULTS
,
3812 &done
, &result_count
,
3813 rs
->resultthreadlist
);
3816 /* Clear for later iterations. */
3818 /* Setup to resume next batch of thread references, set nextthread. */
3819 if (result_count
>= 1)
3820 copy_threadref (&rs
->nextthread
,
3821 &rs
->resultthreadlist
[result_count
- 1]);
3823 while (result_count
--)
3825 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
3835 /* A thread found on the remote target. */
3839 explicit thread_item (ptid_t ptid_
)
3843 thread_item (thread_item
&&other
) = default;
3844 thread_item
&operator= (thread_item
&&other
) = default;
3846 DISABLE_COPY_AND_ASSIGN (thread_item
);
3848 /* The thread's PTID. */
3851 /* The thread's extra info. */
3854 /* The thread's name. */
3857 /* The core the thread was running on. -1 if not known. */
3860 /* The thread handle associated with the thread. */
3861 gdb::byte_vector thread_handle
;
3864 /* Context passed around to the various methods listing remote
3865 threads. As new threads are found, they're added to the ITEMS
3868 struct threads_listing_context
3870 /* Return true if this object contains an entry for a thread with ptid
3873 bool contains_thread (ptid_t ptid
) const
3875 auto match_ptid
= [&] (const thread_item
&item
)
3877 return item
.ptid
== ptid
;
3880 auto it
= std::find_if (this->items
.begin (),
3884 return it
!= this->items
.end ();
3887 /* Remove the thread with ptid PTID. */
3889 void remove_thread (ptid_t ptid
)
3891 auto match_ptid
= [&] (const thread_item
&item
)
3893 return item
.ptid
== ptid
;
3896 auto it
= std::remove_if (this->items
.begin (),
3900 if (it
!= this->items
.end ())
3901 this->items
.erase (it
);
3904 /* The threads found on the remote target. */
3905 std::vector
<thread_item
> items
;
3909 remote_newthread_step (threadref
*ref
, void *data
)
3911 struct threads_listing_context
*context
3912 = (struct threads_listing_context
*) data
;
3913 int pid
= inferior_ptid
.pid ();
3914 int lwp
= threadref_to_int (ref
);
3915 ptid_t
ptid (pid
, lwp
);
3917 context
->items
.emplace_back (ptid
);
3919 return 1; /* continue iterator */
3922 #define CRAZY_MAX_THREADS 1000
3925 remote_target::remote_current_thread (ptid_t oldpid
)
3927 struct remote_state
*rs
= get_remote_state ();
3930 getpkt (&rs
->buf
, 0);
3931 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
3936 result
= read_ptid (&rs
->buf
[2], &obuf
);
3938 remote_debug_printf ("warning: garbage in qC reply");
3946 /* List remote threads using the deprecated qL packet. */
3949 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
3951 if (remote_threadlist_iterator (remote_newthread_step
, context
,
3952 CRAZY_MAX_THREADS
) >= 0)
3958 #if defined(HAVE_LIBEXPAT)
3961 start_thread (struct gdb_xml_parser
*parser
,
3962 const struct gdb_xml_element
*element
,
3964 std::vector
<gdb_xml_value
> &attributes
)
3966 struct threads_listing_context
*data
3967 = (struct threads_listing_context
*) user_data
;
3968 struct gdb_xml_value
*attr
;
3970 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
3971 ptid_t ptid
= read_ptid (id
, NULL
);
3973 data
->items
.emplace_back (ptid
);
3974 thread_item
&item
= data
->items
.back ();
3976 attr
= xml_find_attribute (attributes
, "core");
3978 item
.core
= *(ULONGEST
*) attr
->value
.get ();
3980 attr
= xml_find_attribute (attributes
, "name");
3982 item
.name
= (const char *) attr
->value
.get ();
3984 attr
= xml_find_attribute (attributes
, "handle");
3986 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
3990 end_thread (struct gdb_xml_parser
*parser
,
3991 const struct gdb_xml_element
*element
,
3992 void *user_data
, const char *body_text
)
3994 struct threads_listing_context
*data
3995 = (struct threads_listing_context
*) user_data
;
3997 if (body_text
!= NULL
&& *body_text
!= '\0')
3998 data
->items
.back ().extra
= body_text
;
4001 const struct gdb_xml_attribute thread_attributes
[] = {
4002 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
4003 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
4004 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4005 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4006 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
4009 const struct gdb_xml_element thread_children
[] = {
4010 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4013 const struct gdb_xml_element threads_children
[] = {
4014 { "thread", thread_attributes
, thread_children
,
4015 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
4016 start_thread
, end_thread
},
4017 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4020 const struct gdb_xml_element threads_elements
[] = {
4021 { "threads", NULL
, threads_children
,
4022 GDB_XML_EF_NONE
, NULL
, NULL
},
4023 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4028 /* List remote threads using qXfer:threads:read. */
4031 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
4033 #if defined(HAVE_LIBEXPAT)
4034 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4036 gdb::optional
<gdb::char_vector
> xml
4037 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
4039 if (xml
&& (*xml
)[0] != '\0')
4041 gdb_xml_parse_quick (_("threads"), "threads.dtd",
4042 threads_elements
, xml
->data (), context
);
4052 /* List remote threads using qfThreadInfo/qsThreadInfo. */
4055 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
4057 struct remote_state
*rs
= get_remote_state ();
4059 if (rs
->use_threadinfo_query
)
4063 putpkt ("qfThreadInfo");
4064 getpkt (&rs
->buf
, 0);
4065 bufp
= rs
->buf
.data ();
4066 if (bufp
[0] != '\0') /* q packet recognized */
4068 while (*bufp
++ == 'm') /* reply contains one or more TID */
4072 ptid_t ptid
= read_ptid (bufp
, &bufp
);
4073 context
->items
.emplace_back (ptid
);
4075 while (*bufp
++ == ','); /* comma-separated list */
4076 putpkt ("qsThreadInfo");
4077 getpkt (&rs
->buf
, 0);
4078 bufp
= rs
->buf
.data ();
4084 /* Packet not recognized. */
4085 rs
->use_threadinfo_query
= 0;
4092 /* Return true if INF only has one non-exited thread. */
4095 has_single_non_exited_thread (inferior
*inf
)
4098 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
4104 /* Implement the to_update_thread_list function for the remote
4108 remote_target::update_thread_list ()
4110 struct threads_listing_context context
;
4113 /* We have a few different mechanisms to fetch the thread list. Try
4114 them all, starting with the most preferred one first, falling
4115 back to older methods. */
4116 if (remote_get_threads_with_qxfer (&context
)
4117 || remote_get_threads_with_qthreadinfo (&context
)
4118 || remote_get_threads_with_ql (&context
))
4122 if (context
.items
.empty ()
4123 && remote_thread_always_alive (inferior_ptid
))
4125 /* Some targets don't really support threads, but still
4126 reply an (empty) thread list in response to the thread
4127 listing packets, instead of replying "packet not
4128 supported". Exit early so we don't delete the main
4133 /* CONTEXT now holds the current thread list on the remote
4134 target end. Delete GDB-side threads no longer found on the
4136 for (thread_info
*tp
: all_threads_safe ())
4138 if (tp
->inf
->process_target () != this)
4141 if (!context
.contains_thread (tp
->ptid
))
4143 /* Do not remove the thread if it is the last thread in
4144 the inferior. This situation happens when we have a
4145 pending exit process status to process. Otherwise we
4146 may end up with a seemingly live inferior (i.e. pid
4147 != 0) that has no threads. */
4148 if (has_single_non_exited_thread (tp
->inf
))
4156 /* Remove any unreported fork child threads from CONTEXT so
4157 that we don't interfere with follow fork, which is where
4158 creation of such threads is handled. */
4159 remove_new_fork_children (&context
);
4161 /* And now add threads we don't know about yet to our list. */
4162 for (thread_item
&item
: context
.items
)
4164 if (item
.ptid
!= null_ptid
)
4166 /* In non-stop mode, we assume new found threads are
4167 executing until proven otherwise with a stop reply.
4168 In all-stop, we can only get here if all threads are
4170 bool executing
= target_is_non_stop_p ();
4172 remote_notice_new_inferior (item
.ptid
, executing
);
4174 thread_info
*tp
= this->find_thread (item
.ptid
);
4175 remote_thread_info
*info
= get_remote_thread_info (tp
);
4176 info
->core
= item
.core
;
4177 info
->extra
= std::move (item
.extra
);
4178 info
->name
= std::move (item
.name
);
4179 info
->thread_handle
= std::move (item
.thread_handle
);
4186 /* If no thread listing method is supported, then query whether
4187 each known thread is alive, one by one, with the T packet.
4188 If the target doesn't support threads at all, then this is a
4189 no-op. See remote_thread_alive. */
4195 * Collect a descriptive string about the given thread.
4196 * The target may say anything it wants to about the thread
4197 * (typically info about its blocked / runnable state, name, etc.).
4198 * This string will appear in the info threads display.
4200 * Optional: targets are not required to implement this function.
4204 remote_target::extra_thread_info (thread_info
*tp
)
4206 struct remote_state
*rs
= get_remote_state ();
4209 struct gdb_ext_thread_info threadinfo
;
4211 if (rs
->remote_desc
== 0) /* paranoia */
4212 internal_error (_("remote_threads_extra_info"));
4214 if (tp
->ptid
== magic_null_ptid
4215 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4216 /* This is the main thread which was added by GDB. The remote
4217 server doesn't know about it. */
4220 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4222 /* If already have cached info, use it. */
4223 if (!extra
.empty ())
4224 return extra
.c_str ();
4226 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4228 /* If we're using qXfer:threads:read, then the extra info is
4229 included in the XML. So if we didn't have anything cached,
4230 it's because there's really no extra info. */
4234 if (rs
->use_threadextra_query
)
4236 char *b
= rs
->buf
.data ();
4237 char *endb
= b
+ get_remote_packet_size ();
4239 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4241 write_ptid (b
, endb
, tp
->ptid
);
4244 getpkt (&rs
->buf
, 0);
4245 if (rs
->buf
[0] != 0)
4247 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4248 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4249 return extra
.c_str ();
4253 /* If the above query fails, fall back to the old method. */
4254 rs
->use_threadextra_query
= 0;
4255 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4256 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4257 int_to_threadref (&id
, tp
->ptid
.lwp ());
4258 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4259 if (threadinfo
.active
)
4261 if (*threadinfo
.shortname
)
4262 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4263 if (*threadinfo
.display
)
4265 if (!extra
.empty ())
4267 string_appendf (extra
, " State: %s", threadinfo
.display
);
4269 if (*threadinfo
.more_display
)
4271 if (!extra
.empty ())
4273 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4275 return extra
.c_str ();
4282 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4283 struct static_tracepoint_marker
*marker
)
4285 struct remote_state
*rs
= get_remote_state ();
4286 char *p
= rs
->buf
.data ();
4288 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4290 p
+= hexnumstr (p
, addr
);
4292 getpkt (&rs
->buf
, 0);
4293 p
= rs
->buf
.data ();
4296 error (_("Remote failure reply: %s"), p
);
4300 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4307 std::vector
<static_tracepoint_marker
>
4308 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4310 struct remote_state
*rs
= get_remote_state ();
4311 std::vector
<static_tracepoint_marker
> markers
;
4313 static_tracepoint_marker marker
;
4315 /* Ask for a first packet of static tracepoint marker
4318 getpkt (&rs
->buf
, 0);
4319 p
= rs
->buf
.data ();
4321 error (_("Remote failure reply: %s"), p
);
4327 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4329 if (strid
== NULL
|| marker
.str_id
== strid
)
4330 markers
.push_back (std::move (marker
));
4332 while (*p
++ == ','); /* comma-separated list */
4333 /* Ask for another packet of static tracepoint definition. */
4335 getpkt (&rs
->buf
, 0);
4336 p
= rs
->buf
.data ();
4343 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4346 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4348 return ptid_t (inferior_ptid
.pid (), lwp
);
4352 /* Restart the remote side; this is an extended protocol operation. */
4355 remote_target::extended_remote_restart ()
4357 struct remote_state
*rs
= get_remote_state ();
4359 /* Send the restart command; for reasons I don't understand the
4360 remote side really expects a number after the "R". */
4361 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4364 remote_fileio_reset ();
4367 /* Clean up connection to a remote debugger. */
4370 remote_target::close ()
4372 /* Make sure we leave stdin registered in the event loop. */
4375 trace_reset_local_state ();
4380 remote_target::~remote_target ()
4382 struct remote_state
*rs
= get_remote_state ();
4384 /* Check for NULL because we may get here with a partially
4385 constructed target/connection. */
4386 if (rs
->remote_desc
== nullptr)
4389 serial_close (rs
->remote_desc
);
4391 /* We are destroying the remote target, so we should discard
4392 everything of this target. */
4393 discard_pending_stop_replies_in_queue ();
4395 if (rs
->remote_async_inferior_event_token
)
4396 delete_async_event_handler (&rs
->remote_async_inferior_event_token
);
4398 delete rs
->notif_state
;
4401 /* Query the remote side for the text, data and bss offsets. */
4404 remote_target::get_offsets ()
4406 struct remote_state
*rs
= get_remote_state ();
4409 int lose
, num_segments
= 0, do_sections
, do_segments
;
4410 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4412 if (current_program_space
->symfile_object_file
== NULL
)
4415 putpkt ("qOffsets");
4416 getpkt (&rs
->buf
, 0);
4417 buf
= rs
->buf
.data ();
4419 if (buf
[0] == '\000')
4420 return; /* Return silently. Stub doesn't support
4424 warning (_("Remote failure reply: %s"), buf
);
4428 /* Pick up each field in turn. This used to be done with scanf, but
4429 scanf will make trouble if CORE_ADDR size doesn't match
4430 conversion directives correctly. The following code will work
4431 with any size of CORE_ADDR. */
4432 text_addr
= data_addr
= bss_addr
= 0;
4436 if (startswith (ptr
, "Text="))
4439 /* Don't use strtol, could lose on big values. */
4440 while (*ptr
&& *ptr
!= ';')
4441 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4443 if (startswith (ptr
, ";Data="))
4446 while (*ptr
&& *ptr
!= ';')
4447 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4452 if (!lose
&& startswith (ptr
, ";Bss="))
4455 while (*ptr
&& *ptr
!= ';')
4456 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4458 if (bss_addr
!= data_addr
)
4459 warning (_("Target reported unsupported offsets: %s"), buf
);
4464 else if (startswith (ptr
, "TextSeg="))
4467 /* Don't use strtol, could lose on big values. */
4468 while (*ptr
&& *ptr
!= ';')
4469 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4472 if (startswith (ptr
, ";DataSeg="))
4475 while (*ptr
&& *ptr
!= ';')
4476 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4484 error (_("Malformed response to offset query, %s"), buf
);
4485 else if (*ptr
!= '\0')
4486 warning (_("Target reported unsupported offsets: %s"), buf
);
4488 objfile
*objf
= current_program_space
->symfile_object_file
;
4489 section_offsets offs
= objf
->section_offsets
;
4491 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
.get ());
4492 do_segments
= (data
!= NULL
);
4493 do_sections
= num_segments
== 0;
4495 if (num_segments
> 0)
4497 segments
[0] = text_addr
;
4498 segments
[1] = data_addr
;
4500 /* If we have two segments, we can still try to relocate everything
4501 by assuming that the .text and .data offsets apply to the whole
4502 text and data segments. Convert the offsets given in the packet
4503 to base addresses for symfile_map_offsets_to_segments. */
4504 else if (data
!= nullptr && data
->segments
.size () == 2)
4506 segments
[0] = data
->segments
[0].base
+ text_addr
;
4507 segments
[1] = data
->segments
[1].base
+ data_addr
;
4510 /* If the object file has only one segment, assume that it is text
4511 rather than data; main programs with no writable data are rare,
4512 but programs with no code are useless. Of course the code might
4513 have ended up in the data segment... to detect that we would need
4514 the permissions here. */
4515 else if (data
&& data
->segments
.size () == 1)
4517 segments
[0] = data
->segments
[0].base
+ text_addr
;
4520 /* There's no way to relocate by segment. */
4526 int ret
= symfile_map_offsets_to_segments (objf
->obfd
.get (),
4528 num_segments
, segments
);
4530 if (ret
== 0 && !do_sections
)
4531 error (_("Can not handle qOffsets TextSeg "
4532 "response with this symbol file"));
4540 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4542 /* This is a temporary kludge to force data and bss to use the
4543 same offsets because that's what nlmconv does now. The real
4544 solution requires changes to the stub and remote.c that I
4545 don't have time to do right now. */
4547 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4548 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4551 objfile_relocate (objf
, offs
);
4554 /* Send interrupt_sequence to remote target. */
4557 remote_target::send_interrupt_sequence ()
4559 struct remote_state
*rs
= get_remote_state ();
4561 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4562 remote_serial_write ("\x03", 1);
4563 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4564 serial_send_break (rs
->remote_desc
);
4565 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4567 serial_send_break (rs
->remote_desc
);
4568 remote_serial_write ("g", 1);
4571 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
4572 interrupt_sequence_mode
);
4576 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4577 and extract the PTID. Returns NULL_PTID if not found. */
4580 stop_reply_extract_thread (const char *stop_reply
)
4582 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4586 /* Txx r:val ; r:val (...) */
4589 /* Look for "register" named "thread". */
4594 p1
= strchr (p
, ':');
4598 if (strncmp (p
, "thread", p1
- p
) == 0)
4599 return read_ptid (++p1
, &p
);
4601 p1
= strchr (p
, ';');
4613 /* Determine the remote side's current thread. If we have a stop
4614 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4615 "thread" register we can extract the current thread from. If not,
4616 ask the remote which is the current thread with qC. The former
4617 method avoids a roundtrip. */
4620 remote_target::get_current_thread (const char *wait_status
)
4622 ptid_t ptid
= null_ptid
;
4624 /* Note we don't use remote_parse_stop_reply as that makes use of
4625 the target architecture, which we haven't yet fully determined at
4627 if (wait_status
!= NULL
)
4628 ptid
= stop_reply_extract_thread (wait_status
);
4629 if (ptid
== null_ptid
)
4630 ptid
= remote_current_thread (inferior_ptid
);
4635 /* Query the remote target for which is the current thread/process,
4636 add it to our tables, and update INFERIOR_PTID. The caller is
4637 responsible for setting the state such that the remote end is ready
4638 to return the current thread.
4640 This function is called after handling the '?' or 'vRun' packets,
4641 whose response is a stop reply from which we can also try
4642 extracting the thread. If the target doesn't support the explicit
4643 qC query, we infer the current thread from that stop reply, passed
4644 in in WAIT_STATUS, which may be NULL.
4646 The function returns pointer to the main thread of the inferior. */
4649 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4651 bool fake_pid_p
= false;
4653 switch_to_no_thread ();
4655 /* Now, if we have thread information, update the current thread's
4657 ptid_t curr_ptid
= get_current_thread (wait_status
);
4659 if (curr_ptid
!= null_ptid
)
4661 if (!m_features
.remote_multi_process_p ())
4666 /* Without this, some commands which require an active target
4667 (such as kill) won't work. This variable serves (at least)
4668 double duty as both the pid of the target process (if it has
4669 such), and as a flag indicating that a target is active. */
4670 curr_ptid
= magic_null_ptid
;
4674 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4676 /* Add the main thread and switch to it. Don't try reading
4677 registers yet, since we haven't fetched the target description
4679 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4680 switch_to_thread_no_regs (tp
);
4685 /* Print info about a thread that was found already stopped on
4689 remote_target::print_one_stopped_thread (thread_info
*thread
)
4691 target_waitstatus ws
;
4693 /* If there is a pending waitstatus, use it. If there isn't it's because
4694 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4695 and process_initial_stop_replies decided it wasn't interesting to save
4696 and report to the core. */
4697 if (thread
->has_pending_waitstatus ())
4699 ws
= thread
->pending_waitstatus ();
4700 thread
->clear_pending_waitstatus ();
4704 ws
.set_stopped (GDB_SIGNAL_0
);
4707 switch_to_thread (thread
);
4708 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4709 set_current_sal_from_frame (get_current_frame ());
4711 /* For "info program". */
4712 set_last_target_status (this, thread
->ptid
, ws
);
4714 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4716 enum gdb_signal sig
= ws
.sig ();
4718 if (signal_print_state (sig
))
4719 notify_signal_received (sig
);
4722 notify_normal_stop (nullptr, 1);
4725 /* Process all initial stop replies the remote side sent in response
4726 to the ? packet. These indicate threads that were already stopped
4727 on initial connection. We mark these threads as stopped and print
4728 their current frame before giving the user the prompt. */
4731 remote_target::process_initial_stop_replies (int from_tty
)
4733 int pending_stop_replies
= stop_reply_queue_length ();
4734 struct thread_info
*selected
= NULL
;
4735 struct thread_info
*lowest_stopped
= NULL
;
4736 struct thread_info
*first
= NULL
;
4738 /* This is only used when the target is non-stop. */
4739 gdb_assert (target_is_non_stop_p ());
4741 /* Consume the initial pending events. */
4742 while (pending_stop_replies
-- > 0)
4744 ptid_t waiton_ptid
= minus_one_ptid
;
4746 struct target_waitstatus ws
;
4747 int ignore_event
= 0;
4749 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4751 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4755 case TARGET_WAITKIND_IGNORE
:
4756 case TARGET_WAITKIND_NO_RESUMED
:
4757 case TARGET_WAITKIND_SIGNALLED
:
4758 case TARGET_WAITKIND_EXITED
:
4759 /* We shouldn't see these, but if we do, just ignore. */
4760 remote_debug_printf ("event ignored");
4771 thread_info
*evthread
= this->find_thread (event_ptid
);
4773 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4775 enum gdb_signal sig
= ws
.sig ();
4777 /* Stubs traditionally report SIGTRAP as initial signal,
4778 instead of signal 0. Suppress it. */
4779 if (sig
== GDB_SIGNAL_TRAP
)
4781 evthread
->set_stop_signal (sig
);
4782 ws
.set_stopped (sig
);
4785 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4786 || ws
.sig () != GDB_SIGNAL_0
)
4787 evthread
->set_pending_waitstatus (ws
);
4789 set_executing (this, event_ptid
, false);
4790 set_running (this, event_ptid
, false);
4791 get_remote_thread_info (evthread
)->set_not_resumed ();
4794 /* "Notice" the new inferiors before anything related to
4795 registers/memory. */
4796 for (inferior
*inf
: all_non_exited_inferiors (this))
4798 inf
->needs_setup
= true;
4802 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4803 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4808 /* If all-stop on top of non-stop, pause all threads. Note this
4809 records the threads' stop pc, so must be done after "noticing"
4814 /* At this point, the remote target is not async. It needs to be for
4815 the poll in stop_all_threads to consider events from it, so enable
4817 gdb_assert (!this->is_async_p ());
4818 SCOPE_EXIT
{ target_async (false); };
4819 target_async (true);
4820 stop_all_threads ("remote connect in all-stop");
4823 /* If all threads of an inferior were already stopped, we
4824 haven't setup the inferior yet. */
4825 for (inferior
*inf
: all_non_exited_inferiors (this))
4827 if (inf
->needs_setup
)
4829 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4830 switch_to_thread_no_regs (thread
);
4836 /* Now go over all threads that are stopped, and print their current
4837 frame. If all-stop, then if there's a signalled thread, pick
4839 for (thread_info
*thread
: all_non_exited_threads (this))
4845 thread
->set_running (false);
4846 else if (thread
->state
!= THREAD_STOPPED
)
4849 if (selected
== nullptr && thread
->has_pending_waitstatus ())
4852 if (lowest_stopped
== NULL
4853 || thread
->inf
->num
< lowest_stopped
->inf
->num
4854 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
4855 lowest_stopped
= thread
;
4858 print_one_stopped_thread (thread
);
4861 /* In all-stop, we only print the status of one thread, and leave
4862 others with their status pending. */
4865 thread_info
*thread
= selected
;
4867 thread
= lowest_stopped
;
4871 print_one_stopped_thread (thread
);
4875 /* Mark a remote_target as starting (by setting the starting_up flag within
4876 its remote_state) for the lifetime of this object. The reference count
4877 on the remote target is temporarily incremented, to prevent the target
4878 being deleted under our feet. */
4880 struct scoped_mark_target_starting
4882 /* Constructor, TARGET is the target to be marked as starting, its
4883 reference count will be incremented. */
4884 scoped_mark_target_starting (remote_target
*target
)
4885 : m_remote_target (remote_target_ref::new_reference (target
)),
4886 m_restore_starting_up (set_starting_up_flag (target
))
4891 /* Helper function, set the starting_up flag on TARGET and return an
4892 object which, when it goes out of scope, will restore the previous
4893 value of the starting_up flag. */
4894 static scoped_restore_tmpl
<bool>
4895 set_starting_up_flag (remote_target
*target
)
4897 remote_state
*rs
= target
->get_remote_state ();
4898 gdb_assert (!rs
->starting_up
);
4899 return make_scoped_restore (&rs
->starting_up
, true);
4902 /* A gdb::ref_ptr pointer to a remote_target. */
4903 using remote_target_ref
= gdb::ref_ptr
<remote_target
, target_ops_ref_policy
>;
4905 /* A reference to the target on which we are operating. */
4906 remote_target_ref m_remote_target
;
4908 /* An object which restores the previous value of the starting_up flag
4909 when it goes out of scope. */
4910 scoped_restore_tmpl
<bool> m_restore_starting_up
;
4913 /* Helper for remote_target::start_remote, start the remote connection and
4914 sync state. Return true if everything goes OK, otherwise, return false.
4915 This function exists so that the scoped_restore created within it will
4916 expire before we return to remote_target::start_remote. */
4919 remote_target::start_remote_1 (int from_tty
, int extended_p
)
4921 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
4923 struct remote_state
*rs
= get_remote_state ();
4925 /* Signal other parts that we're going through the initial setup,
4926 and so things may not be stable yet. E.g., we don't try to
4927 install tracepoints until we've relocated symbols. Also, a
4928 Ctrl-C before we're connected and synced up can't interrupt the
4929 target. Instead, it offers to drop the (potentially wedged)
4931 scoped_mark_target_starting
target_is_starting (this);
4935 if (interrupt_on_connect
)
4936 send_interrupt_sequence ();
4938 /* Ack any packet which the remote side has already sent. */
4939 remote_serial_write ("+", 1);
4941 /* The first packet we send to the target is the optional "supported
4942 packets" request. If the target can answer this, it will tell us
4943 which later probes to skip. */
4944 remote_query_supported ();
4946 /* Check vCont support and set the remote state's vCont_action_support
4948 remote_vcont_probe ();
4950 /* If the stub wants to get a QAllow, compose one and send it. */
4951 if (m_features
.packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
4954 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4955 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4956 as a reply to known packet. For packet "vFile:setfs:" it is an
4957 invalid reply and GDB would return error in
4958 remote_hostio_set_filesystem, making remote files access impossible.
4959 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4960 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4962 const char v_mustreplyempty
[] = "vMustReplyEmpty";
4964 putpkt (v_mustreplyempty
);
4965 getpkt (&rs
->buf
, 0);
4966 if (strcmp (rs
->buf
.data (), "OK") == 0)
4968 m_features
.m_protocol_packets
[PACKET_vFile_setfs
].support
4971 else if (strcmp (rs
->buf
.data (), "") != 0)
4972 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
4976 /* Next, we possibly activate noack mode.
4978 If the QStartNoAckMode packet configuration is set to AUTO,
4979 enable noack mode if the stub reported a wish for it with
4982 If set to TRUE, then enable noack mode even if the stub didn't
4983 report it in qSupported. If the stub doesn't reply OK, the
4984 session ends with an error.
4986 If FALSE, then don't activate noack mode, regardless of what the
4987 stub claimed should be the default with qSupported. */
4989 if (m_features
.packet_support (PACKET_QStartNoAckMode
) != PACKET_DISABLE
)
4991 putpkt ("QStartNoAckMode");
4992 getpkt (&rs
->buf
, 0);
4993 if (m_features
.packet_ok (rs
->buf
, PACKET_QStartNoAckMode
) == PACKET_OK
)
4999 /* Tell the remote that we are using the extended protocol. */
5001 getpkt (&rs
->buf
, 0);
5004 /* Let the target know which signals it is allowed to pass down to
5006 update_signals_program_target ();
5008 /* Next, if the target can specify a description, read it. We do
5009 this before anything involving memory or registers. */
5010 target_find_description ();
5012 /* Next, now that we know something about the target, update the
5013 address spaces in the program spaces. */
5014 update_address_spaces ();
5016 /* On OSs where the list of libraries is global to all
5017 processes, we fetch them early. */
5018 if (gdbarch_has_global_solist (target_gdbarch ()))
5019 solib_add (NULL
, from_tty
, auto_solib_add
);
5021 if (target_is_non_stop_p ())
5023 if (m_features
.packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
5024 error (_("Non-stop mode requested, but remote "
5025 "does not support non-stop"));
5027 putpkt ("QNonStop:1");
5028 getpkt (&rs
->buf
, 0);
5030 if (strcmp (rs
->buf
.data (), "OK") != 0)
5031 error (_("Remote refused setting non-stop mode with: %s"),
5034 /* Find about threads and processes the stub is already
5035 controlling. We default to adding them in the running state.
5036 The '?' query below will then tell us about which threads are
5038 this->update_thread_list ();
5040 else if (m_features
.packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
5042 /* Don't assume that the stub can operate in all-stop mode.
5043 Request it explicitly. */
5044 putpkt ("QNonStop:0");
5045 getpkt (&rs
->buf
, 0);
5047 if (strcmp (rs
->buf
.data (), "OK") != 0)
5048 error (_("Remote refused setting all-stop mode with: %s"),
5052 /* Upload TSVs regardless of whether the target is running or not. The
5053 remote stub, such as GDBserver, may have some predefined or builtin
5054 TSVs, even if the target is not running. */
5055 if (get_trace_status (current_trace_status ()) != -1)
5057 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
5059 upload_trace_state_variables (&uploaded_tsvs
);
5060 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
5063 /* Check whether the target is running now. */
5065 getpkt (&rs
->buf
, 0);
5067 if (!target_is_non_stop_p ())
5069 char *wait_status
= NULL
;
5071 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
5074 error (_("The target is not running (try extended-remote?)"));
5079 /* Save the reply for later. */
5080 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
5081 strcpy (wait_status
, rs
->buf
.data ());
5084 /* Fetch thread list. */
5085 target_update_thread_list ();
5087 /* Let the stub know that we want it to return the thread. */
5088 set_continue_thread (minus_one_ptid
);
5090 if (thread_count (this) == 0)
5092 /* Target has no concept of threads at all. GDB treats
5093 non-threaded target as single-threaded; add a main
5095 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
5096 get_remote_thread_info (tp
)->set_resumed ();
5100 /* We have thread information; select the thread the target
5101 says should be current. If we're reconnecting to a
5102 multi-threaded program, this will ideally be the thread
5103 that last reported an event before GDB disconnected. */
5104 ptid_t curr_thread
= get_current_thread (wait_status
);
5105 if (curr_thread
== null_ptid
)
5107 /* Odd... The target was able to list threads, but not
5108 tell us which thread was current (no "thread"
5109 register in T stop reply?). Just pick the first
5110 thread in the thread list then. */
5112 remote_debug_printf ("warning: couldn't determine remote "
5113 "current thread; picking first in list.");
5115 for (thread_info
*tp
: all_non_exited_threads (this,
5118 switch_to_thread (tp
);
5123 switch_to_thread (this->find_thread (curr_thread
));
5126 /* init_wait_for_inferior should be called before get_offsets in order
5127 to manage `inserted' flag in bp loc in a correct state.
5128 breakpoint_init_inferior, called from init_wait_for_inferior, set
5129 `inserted' flag to 0, while before breakpoint_re_set, called from
5130 start_remote, set `inserted' flag to 1. In the initialization of
5131 inferior, breakpoint_init_inferior should be called first, and then
5132 breakpoint_re_set can be called. If this order is broken, state of
5133 `inserted' flag is wrong, and cause some problems on breakpoint
5135 init_wait_for_inferior ();
5137 get_offsets (); /* Get text, data & bss offsets. */
5139 /* If we could not find a description using qXfer, and we know
5140 how to do it some other way, try again. This is not
5141 supported for non-stop; it could be, but it is tricky if
5142 there are no stopped threads when we connect. */
5143 if (remote_read_description_p (this)
5144 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
5146 target_clear_description ();
5147 target_find_description ();
5150 /* Use the previously fetched status. */
5151 gdb_assert (wait_status
!= NULL
);
5152 struct notif_event
*reply
5153 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
5154 push_stop_reply ((struct stop_reply
*) reply
);
5156 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
5160 /* Clear WFI global state. Do this before finding about new
5161 threads and inferiors, and setting the current inferior.
5162 Otherwise we would clear the proceed status of the current
5163 inferior when we want its stop_soon state to be preserved
5164 (see notice_new_inferior). */
5165 init_wait_for_inferior ();
5167 /* In non-stop, we will either get an "OK", meaning that there
5168 are no stopped threads at this time; or, a regular stop
5169 reply. In the latter case, there may be more than one thread
5170 stopped --- we pull them all out using the vStopped
5172 if (strcmp (rs
->buf
.data (), "OK") != 0)
5174 const notif_client
*notif
= ¬if_client_stop
;
5176 /* remote_notif_get_pending_replies acks this one, and gets
5178 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
5179 = remote_notif_parse (this, notif
, rs
->buf
.data ());
5180 remote_notif_get_pending_events (notif
);
5183 if (thread_count (this) == 0)
5186 error (_("The target is not running (try extended-remote?)"));
5190 /* Report all signals during attach/startup. */
5193 /* If there are already stopped threads, mark them stopped and
5194 report their stops before giving the prompt to the user. */
5195 process_initial_stop_replies (from_tty
);
5197 if (target_can_async_p ())
5198 target_async (true);
5201 /* Give the target a chance to look up symbols. */
5202 for (inferior
*inf
: all_inferiors (this))
5204 /* The inferiors that exist at this point were created from what
5205 was found already running on the remote side, so we know they
5207 gdb_assert (this->has_execution (inf
));
5209 /* No use without a symbol-file. */
5210 if (inf
->pspace
->symfile_object_file
== nullptr)
5213 /* Need to switch to a specific thread, because remote_check_symbols
5214 uses INFERIOR_PTID to set the general thread. */
5215 scoped_restore_current_thread restore_thread
;
5216 thread_info
*thread
= any_thread_of_inferior (inf
);
5217 switch_to_thread (thread
);
5218 this->remote_check_symbols ();
5221 /* Possibly the target has been engaged in a trace run started
5222 previously; find out where things are at. */
5223 if (get_trace_status (current_trace_status ()) != -1)
5225 struct uploaded_tp
*uploaded_tps
= NULL
;
5227 if (current_trace_status ()->running
)
5228 gdb_printf (_("Trace is already running on the target.\n"));
5230 upload_tracepoints (&uploaded_tps
);
5232 merge_uploaded_tracepoints (&uploaded_tps
);
5235 /* Possibly the target has been engaged in a btrace record started
5236 previously; find out where things are at. */
5237 remote_btrace_maybe_reopen ();
5242 /* Start the remote connection and sync state. */
5245 remote_target::start_remote (int from_tty
, int extended_p
)
5247 if (start_remote_1 (from_tty
, extended_p
)
5248 && breakpoints_should_be_inserted_now ())
5249 insert_breakpoints ();
5253 remote_target::connection_string ()
5255 remote_state
*rs
= get_remote_state ();
5257 if (rs
->remote_desc
->name
!= NULL
)
5258 return rs
->remote_desc
->name
;
5263 /* Open a connection to a remote debugger.
5264 NAME is the filename used for communication. */
5267 remote_target::open (const char *name
, int from_tty
)
5269 open_1 (name
, from_tty
, 0);
5272 /* Open a connection to a remote debugger using the extended
5273 remote gdb protocol. NAME is the filename used for communication. */
5276 extended_remote_target::open (const char *name
, int from_tty
)
5278 open_1 (name
, from_tty
, 1 /*extended_p */);
5282 remote_features::reset_all_packet_configs_support ()
5286 for (i
= 0; i
< PACKET_MAX
; i
++)
5287 m_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5290 /* Initialize all packet configs. */
5293 init_all_packet_configs (void)
5297 for (i
= 0; i
< PACKET_MAX
; i
++)
5299 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5300 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5304 /* Symbol look-up. */
5307 remote_target::remote_check_symbols ()
5312 /* It doesn't make sense to send a qSymbol packet for an inferior that
5313 doesn't have execution, because the remote side doesn't know about
5314 inferiors without execution. */
5315 gdb_assert (target_has_execution ());
5317 if (m_features
.packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5320 /* Make sure the remote is pointing at the right process. Note
5321 there's no way to select "no process". */
5322 set_general_process ();
5324 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5325 because we need both at the same time. */
5326 gdb::char_vector
msg (get_remote_packet_size ());
5327 gdb::char_vector
reply (get_remote_packet_size ());
5329 /* Invite target to request symbol lookups. */
5331 putpkt ("qSymbol::");
5333 m_features
.packet_ok (reply
, PACKET_qSymbol
);
5335 while (startswith (reply
.data (), "qSymbol:"))
5337 struct bound_minimal_symbol sym
;
5340 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5343 sym
= lookup_minimal_symbol (msg
.data (), NULL
, NULL
);
5344 if (sym
.minsym
== NULL
)
5345 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5349 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
5350 CORE_ADDR sym_addr
= sym
.value_address ();
5352 /* If this is a function address, return the start of code
5353 instead of any data function descriptor. */
5354 sym_addr
= gdbarch_convert_from_func_ptr_addr
5355 (target_gdbarch (), sym_addr
, current_inferior ()->top_target ());
5357 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5358 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5361 putpkt (msg
.data ());
5366 static struct serial
*
5367 remote_serial_open (const char *name
)
5369 static int udp_warning
= 0;
5371 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5372 of in ser-tcp.c, because it is the remote protocol assuming that the
5373 serial connection is reliable and not the serial connection promising
5375 if (!udp_warning
&& startswith (name
, "udp:"))
5377 warning (_("The remote protocol may be unreliable over UDP.\n"
5378 "Some events may be lost, rendering further debugging "
5383 return serial_open (name
);
5386 /* Inform the target of our permission settings. The permission flags
5387 work without this, but if the target knows the settings, it can do
5388 a couple things. First, it can add its own check, to catch cases
5389 that somehow manage to get by the permissions checks in target
5390 methods. Second, if the target is wired to disallow particular
5391 settings (for instance, a system in the field that is not set up to
5392 be able to stop at a breakpoint), it can object to any unavailable
5396 remote_target::set_permissions ()
5398 struct remote_state
*rs
= get_remote_state ();
5400 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5401 "WriteReg:%x;WriteMem:%x;"
5402 "InsertBreak:%x;InsertTrace:%x;"
5403 "InsertFastTrace:%x;Stop:%x",
5404 may_write_registers
, may_write_memory
,
5405 may_insert_breakpoints
, may_insert_tracepoints
,
5406 may_insert_fast_tracepoints
, may_stop
);
5408 getpkt (&rs
->buf
, 0);
5410 /* If the target didn't like the packet, warn the user. Do not try
5411 to undo the user's settings, that would just be maddening. */
5412 if (strcmp (rs
->buf
.data (), "OK") != 0)
5413 warning (_("Remote refused setting permissions with: %s"),
5417 /* This type describes each known response to the qSupported
5419 struct protocol_feature
5421 /* The name of this protocol feature. */
5424 /* The default for this protocol feature. */
5425 enum packet_support default_support
;
5427 /* The function to call when this feature is reported, or after
5428 qSupported processing if the feature is not supported.
5429 The first argument points to this structure. The second
5430 argument indicates whether the packet requested support be
5431 enabled, disabled, or probed (or the default, if this function
5432 is being called at the end of processing and this feature was
5433 not reported). The third argument may be NULL; if not NULL, it
5434 is a NUL-terminated string taken from the packet following
5435 this feature's name and an equals sign. */
5436 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5437 enum packet_support
, const char *);
5439 /* The corresponding packet for this feature. Only used if
5440 FUNC is remote_supported_packet. */
5445 remote_supported_packet (remote_target
*remote
,
5446 const struct protocol_feature
*feature
,
5447 enum packet_support support
,
5448 const char *argument
)
5452 warning (_("Remote qSupported response supplied an unexpected value for"
5453 " \"%s\"."), feature
->name
);
5457 remote
->m_features
.m_protocol_packets
[feature
->packet
].support
= support
;
5461 remote_target::remote_packet_size (const protocol_feature
*feature
,
5462 enum packet_support support
, const char *value
)
5464 struct remote_state
*rs
= get_remote_state ();
5469 if (support
!= PACKET_ENABLE
)
5472 if (value
== NULL
|| *value
== '\0')
5474 warning (_("Remote target reported \"%s\" without a size."),
5480 packet_size
= strtol (value
, &value_end
, 16);
5481 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5483 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5484 feature
->name
, value
);
5488 /* Record the new maximum packet size. */
5489 rs
->explicit_packet_size
= packet_size
;
5493 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5494 enum packet_support support
, const char *value
)
5496 remote
->remote_packet_size (feature
, support
, value
);
5499 static const struct protocol_feature remote_protocol_features
[] = {
5500 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5501 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5502 PACKET_qXfer_auxv
},
5503 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5504 PACKET_qXfer_exec_file
},
5505 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5506 PACKET_qXfer_features
},
5507 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5508 PACKET_qXfer_libraries
},
5509 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5510 PACKET_qXfer_libraries_svr4
},
5511 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5512 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5513 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5514 PACKET_qXfer_memory_map
},
5515 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5516 PACKET_qXfer_osdata
},
5517 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5518 PACKET_qXfer_threads
},
5519 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5520 PACKET_qXfer_traceframe_info
},
5521 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5522 PACKET_QPassSignals
},
5523 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5524 PACKET_QCatchSyscalls
},
5525 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5526 PACKET_QProgramSignals
},
5527 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5528 PACKET_QSetWorkingDir
},
5529 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5530 PACKET_QStartupWithShell
},
5531 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5532 PACKET_QEnvironmentHexEncoded
},
5533 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5534 PACKET_QEnvironmentReset
},
5535 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5536 PACKET_QEnvironmentUnset
},
5537 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5538 PACKET_QStartNoAckMode
},
5539 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5540 PACKET_multiprocess_feature
},
5541 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5542 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5543 PACKET_qXfer_siginfo_read
},
5544 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5545 PACKET_qXfer_siginfo_write
},
5546 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5547 PACKET_ConditionalTracepoints
},
5548 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5549 PACKET_ConditionalBreakpoints
},
5550 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5551 PACKET_BreakpointCommands
},
5552 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5553 PACKET_FastTracepoints
},
5554 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5555 PACKET_StaticTracepoints
},
5556 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5557 PACKET_InstallInTrace
},
5558 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5559 PACKET_DisconnectedTracing_feature
},
5560 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5562 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5564 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5565 PACKET_TracepointSource
},
5566 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5568 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5569 PACKET_EnableDisableTracepoints_feature
},
5570 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5571 PACKET_qXfer_fdpic
},
5572 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5574 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5575 PACKET_QDisableRandomization
},
5576 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5577 { "QTBuffer:size", PACKET_DISABLE
,
5578 remote_supported_packet
, PACKET_QTBuffer_size
},
5579 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5580 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5581 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5582 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5583 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5584 PACKET_qXfer_btrace
},
5585 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5586 PACKET_qXfer_btrace_conf
},
5587 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5588 PACKET_Qbtrace_conf_bts_size
},
5589 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5590 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5591 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5592 PACKET_fork_event_feature
},
5593 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5594 PACKET_vfork_event_feature
},
5595 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5596 PACKET_exec_event_feature
},
5597 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5598 PACKET_Qbtrace_conf_pt_size
},
5599 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5600 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5601 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5602 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5603 PACKET_memory_tagging_feature
},
5606 static char *remote_support_xml
;
5608 /* Register string appended to "xmlRegisters=" in qSupported query. */
5611 register_remote_support_xml (const char *xml
)
5613 #if defined(HAVE_LIBEXPAT)
5614 if (remote_support_xml
== NULL
)
5615 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5618 char *copy
= xstrdup (remote_support_xml
+ 13);
5620 char *p
= strtok_r (copy
, ",", &saveptr
);
5624 if (strcmp (p
, xml
) == 0)
5631 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5634 remote_support_xml
= reconcat (remote_support_xml
,
5635 remote_support_xml
, ",", xml
,
5642 remote_query_supported_append (std::string
*msg
, const char *append
)
5646 msg
->append (append
);
5650 remote_target::remote_query_supported ()
5652 struct remote_state
*rs
= get_remote_state ();
5655 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5657 /* The packet support flags are handled differently for this packet
5658 than for most others. We treat an error, a disabled packet, and
5659 an empty response identically: any features which must be reported
5660 to be used will be automatically disabled. An empty buffer
5661 accomplishes this, since that is also the representation for a list
5662 containing no features. */
5665 if (m_features
.packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5669 if (m_features
.packet_set_cmd_state (PACKET_multiprocess_feature
)
5670 != AUTO_BOOLEAN_FALSE
)
5671 remote_query_supported_append (&q
, "multiprocess+");
5673 if (m_features
.packet_set_cmd_state (PACKET_swbreak_feature
)
5674 != AUTO_BOOLEAN_FALSE
)
5675 remote_query_supported_append (&q
, "swbreak+");
5677 if (m_features
.packet_set_cmd_state (PACKET_hwbreak_feature
)
5678 != AUTO_BOOLEAN_FALSE
)
5679 remote_query_supported_append (&q
, "hwbreak+");
5681 remote_query_supported_append (&q
, "qRelocInsn+");
5683 if (m_features
.packet_set_cmd_state (PACKET_fork_event_feature
)
5684 != AUTO_BOOLEAN_FALSE
)
5685 remote_query_supported_append (&q
, "fork-events+");
5687 if (m_features
.packet_set_cmd_state (PACKET_vfork_event_feature
)
5688 != AUTO_BOOLEAN_FALSE
)
5689 remote_query_supported_append (&q
, "vfork-events+");
5691 if (m_features
.packet_set_cmd_state (PACKET_exec_event_feature
)
5692 != AUTO_BOOLEAN_FALSE
)
5693 remote_query_supported_append (&q
, "exec-events+");
5695 if (m_features
.packet_set_cmd_state (PACKET_vContSupported
)
5696 != AUTO_BOOLEAN_FALSE
)
5697 remote_query_supported_append (&q
, "vContSupported+");
5699 if (m_features
.packet_set_cmd_state (PACKET_QThreadEvents
)
5700 != AUTO_BOOLEAN_FALSE
)
5701 remote_query_supported_append (&q
, "QThreadEvents+");
5703 if (m_features
.packet_set_cmd_state (PACKET_no_resumed
)
5704 != AUTO_BOOLEAN_FALSE
)
5705 remote_query_supported_append (&q
, "no-resumed+");
5707 if (m_features
.packet_set_cmd_state (PACKET_memory_tagging_feature
)
5708 != AUTO_BOOLEAN_FALSE
)
5709 remote_query_supported_append (&q
, "memory-tagging+");
5711 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5712 the qSupported:xmlRegisters=i386 handling. */
5713 if (remote_support_xml
!= NULL
5714 && (m_features
.packet_support (PACKET_qXfer_features
)
5716 remote_query_supported_append (&q
, remote_support_xml
);
5718 q
= "qSupported:" + q
;
5719 putpkt (q
.c_str ());
5721 getpkt (&rs
->buf
, 0);
5723 /* If an error occurred, warn, but do not return - just reset the
5724 buffer to empty and go on to disable features. */
5725 if (m_features
.packet_ok (rs
->buf
, PACKET_qSupported
) == PACKET_ERROR
)
5727 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
5732 memset (seen
, 0, sizeof (seen
));
5734 next
= rs
->buf
.data ();
5737 enum packet_support is_supported
;
5738 char *p
, *end
, *name_end
, *value
;
5740 /* First separate out this item from the rest of the packet. If
5741 there's another item after this, we overwrite the separator
5742 (terminated strings are much easier to work with). */
5744 end
= strchr (p
, ';');
5747 end
= p
+ strlen (p
);
5757 warning (_("empty item in \"qSupported\" response"));
5762 name_end
= strchr (p
, '=');
5765 /* This is a name=value entry. */
5766 is_supported
= PACKET_ENABLE
;
5767 value
= name_end
+ 1;
5776 is_supported
= PACKET_ENABLE
;
5780 is_supported
= PACKET_DISABLE
;
5784 is_supported
= PACKET_SUPPORT_UNKNOWN
;
5788 warning (_("unrecognized item \"%s\" "
5789 "in \"qSupported\" response"), p
);
5795 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5796 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
5798 const struct protocol_feature
*feature
;
5801 feature
= &remote_protocol_features
[i
];
5802 feature
->func (this, feature
, is_supported
, value
);
5807 /* If we increased the packet size, make sure to increase the global
5808 buffer size also. We delay this until after parsing the entire
5809 qSupported packet, because this is the same buffer we were
5811 if (rs
->buf
.size () < rs
->explicit_packet_size
)
5812 rs
->buf
.resize (rs
->explicit_packet_size
);
5814 /* Handle the defaults for unmentioned features. */
5815 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5818 const struct protocol_feature
*feature
;
5820 feature
= &remote_protocol_features
[i
];
5821 feature
->func (this, feature
, feature
->default_support
, NULL
);
5825 /* Serial QUIT handler for the remote serial descriptor.
5827 Defers handling a Ctrl-C until we're done with the current
5828 command/response packet sequence, unless:
5830 - We're setting up the connection. Don't send a remote interrupt
5831 request, as we're not fully synced yet. Quit immediately
5834 - The target has been resumed in the foreground
5835 (target_terminal::is_ours is false) with a synchronous resume
5836 packet, and we're blocked waiting for the stop reply, thus a
5837 Ctrl-C should be immediately sent to the target.
5839 - We get a second Ctrl-C while still within the same serial read or
5840 write. In that case the serial is seemingly wedged --- offer to
5843 - We see a second Ctrl-C without target response, after having
5844 previously interrupted the target. In that case the target/stub
5845 is probably wedged --- offer to quit/disconnect.
5849 remote_target::remote_serial_quit_handler ()
5851 struct remote_state
*rs
= get_remote_state ();
5853 if (check_quit_flag ())
5855 /* If we're starting up, we're not fully synced yet. Quit
5857 if (rs
->starting_up
)
5859 else if (rs
->got_ctrlc_during_io
)
5861 if (query (_("The target is not responding to GDB commands.\n"
5862 "Stop debugging it? ")))
5863 remote_unpush_and_throw (this);
5865 /* If ^C has already been sent once, offer to disconnect. */
5866 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
5868 /* All-stop protocol, and blocked waiting for stop reply. Send
5869 an interrupt request. */
5870 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
5871 target_interrupt ();
5873 rs
->got_ctrlc_during_io
= 1;
5877 /* The remote_target that is current while the quit handler is
5878 overridden with remote_serial_quit_handler. */
5879 static remote_target
*curr_quit_handler_target
;
5882 remote_serial_quit_handler ()
5884 curr_quit_handler_target
->remote_serial_quit_handler ();
5887 /* Remove the remote target from the target stack of each inferior
5888 that is using it. Upper targets depend on it so remove them
5892 remote_unpush_target (remote_target
*target
)
5894 /* We have to unpush the target from all inferiors, even those that
5896 scoped_restore_current_inferior restore_current_inferior
;
5898 for (inferior
*inf
: all_inferiors (target
))
5900 switch_to_inferior_no_thread (inf
);
5901 inf
->pop_all_targets_at_and_above (process_stratum
);
5902 generic_mourn_inferior ();
5905 /* Don't rely on target_close doing this when the target is popped
5906 from the last remote inferior above, because something may be
5907 holding a reference to the target higher up on the stack, meaning
5908 target_close won't be called yet. We lost the connection to the
5909 target, so clear these now, otherwise we may later throw
5910 TARGET_CLOSE_ERROR while trying to tell the remote target to
5912 fileio_handles_invalidate_target (target
);
5916 remote_unpush_and_throw (remote_target
*target
)
5918 remote_unpush_target (target
);
5919 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
5923 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
5925 remote_target
*curr_remote
= get_current_remote_target ();
5928 error (_("To open a remote debug connection, you need to specify what\n"
5929 "serial device is attached to the remote system\n"
5930 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
5932 /* If we're connected to a running target, target_preopen will kill it.
5933 Ask this question first, before target_preopen has a chance to kill
5935 if (curr_remote
!= NULL
&& !target_has_execution ())
5938 && !query (_("Already connected to a remote target. Disconnect? ")))
5939 error (_("Still connected."));
5942 /* Here the possibly existing remote target gets unpushed. */
5943 target_preopen (from_tty
);
5945 remote_fileio_reset ();
5946 reopen_exec_file ();
5947 reread_symbols (from_tty
);
5949 remote_target
*remote
5950 = (extended_p
? new extended_remote_target () : new remote_target ());
5951 target_ops_up
target_holder (remote
);
5953 remote_state
*rs
= remote
->get_remote_state ();
5955 /* See FIXME above. */
5956 if (!target_async_permitted
)
5957 rs
->wait_forever_enabled_p
= 1;
5959 rs
->remote_desc
= remote_serial_open (name
);
5960 if (!rs
->remote_desc
)
5961 perror_with_name (name
);
5963 if (baud_rate
!= -1)
5965 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
5967 /* The requested speed could not be set. Error out to
5968 top level after closing remote_desc. Take care to
5969 set remote_desc to NULL to avoid closing remote_desc
5971 serial_close (rs
->remote_desc
);
5972 rs
->remote_desc
= NULL
;
5973 perror_with_name (name
);
5977 serial_setparity (rs
->remote_desc
, serial_parity
);
5978 serial_raw (rs
->remote_desc
);
5980 /* If there is something sitting in the buffer we might take it as a
5981 response to a command, which would be bad. */
5982 serial_flush_input (rs
->remote_desc
);
5986 gdb_puts ("Remote debugging using ");
5991 /* Switch to using the remote target now. */
5992 current_inferior ()->push_target (std::move (target_holder
));
5994 /* Register extra event sources in the event loop. */
5995 rs
->remote_async_inferior_event_token
5996 = create_async_event_handler (remote_async_inferior_event_handler
, nullptr,
5998 rs
->notif_state
= remote_notif_state_allocate (remote
);
6000 /* Reset the target state; these things will be queried either by
6001 remote_query_supported or as they are needed. */
6002 remote
->m_features
.reset_all_packet_configs_support ();
6003 rs
->explicit_packet_size
= 0;
6005 rs
->extended
= extended_p
;
6006 rs
->waiting_for_stop_reply
= 0;
6007 rs
->ctrlc_pending_p
= 0;
6008 rs
->got_ctrlc_during_io
= 0;
6010 rs
->general_thread
= not_sent_ptid
;
6011 rs
->continue_thread
= not_sent_ptid
;
6012 rs
->remote_traceframe_number
= -1;
6014 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
6016 /* Probe for ability to use "ThreadInfo" query, as required. */
6017 rs
->use_threadinfo_query
= 1;
6018 rs
->use_threadextra_query
= 1;
6020 rs
->readahead_cache
.invalidate ();
6022 if (target_async_permitted
)
6024 /* FIXME: cagney/1999-09-23: During the initial connection it is
6025 assumed that the target is already ready and able to respond to
6026 requests. Unfortunately remote_start_remote() eventually calls
6027 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
6028 around this. Eventually a mechanism that allows
6029 wait_for_inferior() to expect/get timeouts will be
6031 rs
->wait_forever_enabled_p
= 0;
6034 /* First delete any symbols previously loaded from shared libraries. */
6035 no_shared_libraries (NULL
, 0);
6037 /* Start the remote connection. If error() or QUIT, discard this
6038 target (we'd otherwise be in an inconsistent state) and then
6039 propogate the error on up the exception chain. This ensures that
6040 the caller doesn't stumble along blindly assuming that the
6041 function succeeded. The CLI doesn't have this problem but other
6042 UI's, such as MI do.
6044 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
6045 this function should return an error indication letting the
6046 caller restore the previous state. Unfortunately the command
6047 ``target remote'' is directly wired to this function making that
6048 impossible. On a positive note, the CLI side of this problem has
6049 been fixed - the function set_cmd_context() makes it possible for
6050 all the ``target ....'' commands to share a common callback
6051 function. See cli-dump.c. */
6056 remote
->start_remote (from_tty
, extended_p
);
6058 catch (const gdb_exception
&ex
)
6060 /* Pop the partially set up target - unless something else did
6061 already before throwing the exception. */
6062 if (ex
.error
!= TARGET_CLOSE_ERROR
)
6063 remote_unpush_target (remote
);
6068 remote_btrace_reset (rs
);
6070 if (target_async_permitted
)
6071 rs
->wait_forever_enabled_p
= 1;
6074 /* Determine if WS represents a fork status. */
6077 is_fork_status (target_waitkind kind
)
6079 return (kind
== TARGET_WAITKIND_FORKED
6080 || kind
== TARGET_WAITKIND_VFORKED
);
6083 /* Return THREAD's pending status if it is a pending fork parent, else
6086 static const target_waitstatus
*
6087 thread_pending_fork_status (struct thread_info
*thread
)
6089 const target_waitstatus
&ws
6090 = (thread
->has_pending_waitstatus ()
6091 ? thread
->pending_waitstatus ()
6092 : thread
->pending_follow
);
6094 if (!is_fork_status (ws
.kind ()))
6100 /* Detach the specified process. */
6103 remote_target::remote_detach_pid (int pid
)
6105 struct remote_state
*rs
= get_remote_state ();
6107 /* This should not be necessary, but the handling for D;PID in
6108 GDBserver versions prior to 8.2 incorrectly assumes that the
6109 selected process points to the same process we're detaching,
6110 leading to misbehavior (and possibly GDBserver crashing) when it
6111 does not. Since it's easy and cheap, work around it by forcing
6112 GDBserver to select GDB's current process. */
6113 set_general_process ();
6115 if (m_features
.remote_multi_process_p ())
6116 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
6118 strcpy (rs
->buf
.data (), "D");
6121 getpkt (&rs
->buf
, 0);
6123 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
6125 else if (rs
->buf
[0] == '\0')
6126 error (_("Remote doesn't know how to detach"));
6128 error (_("Can't detach process."));
6131 /* This detaches a program to which we previously attached, using
6132 inferior_ptid to identify the process. After this is done, GDB
6133 can be used to debug some other program. We better not have left
6134 any breakpoints in the target program or it'll die when it hits
6138 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
6140 int pid
= inferior_ptid
.pid ();
6141 struct remote_state
*rs
= get_remote_state ();
6144 if (!target_has_execution ())
6145 error (_("No process to detach from."));
6147 target_announce_detach (from_tty
);
6149 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
6151 /* If we're in breakpoints-always-inserted mode, or the inferior
6152 is running, we have to remove breakpoints before detaching.
6153 We don't do this in common code instead because not all
6154 targets support removing breakpoints while the target is
6155 running. The remote target / gdbserver does, though. */
6156 remove_breakpoints_inf (current_inferior ());
6159 /* Tell the remote target to detach. */
6160 remote_detach_pid (pid
);
6162 /* Exit only if this is the only active inferior. */
6163 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
6164 gdb_puts (_("Ending remote debugging.\n"));
6166 /* See if any thread of the inferior we are detaching has a pending fork
6167 status. In that case, we must detach from the child resulting from
6169 for (thread_info
*thread
: inf
->non_exited_threads ())
6171 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
6176 remote_detach_pid (ws
->child_ptid ().pid ());
6179 /* Check also for any pending fork events in the stop reply queue. */
6180 remote_notif_get_pending_events (¬if_client_stop
);
6181 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6183 if (reply
->ptid
.pid () != pid
)
6186 if (!is_fork_status (reply
->ws
.kind ()))
6189 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
6192 thread_info
*tp
= this->find_thread (inferior_ptid
);
6194 /* Check to see if we are detaching a fork parent. Note that if we
6195 are detaching a fork child, tp == NULL. */
6196 is_fork_parent
= (tp
!= NULL
6197 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
6199 /* If doing detach-on-fork, we don't mourn, because that will delete
6200 breakpoints that should be available for the followed inferior. */
6201 if (!is_fork_parent
)
6203 /* Save the pid as a string before mourning, since that will
6204 unpush the remote target, and we need the string after. */
6205 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6207 target_mourn_inferior (inferior_ptid
);
6208 if (print_inferior_events
)
6209 gdb_printf (_("[Inferior %d (%s) detached]\n"),
6210 inf
->num
, infpid
.c_str ());
6214 switch_to_no_thread ();
6215 detach_inferior (current_inferior ());
6220 remote_target::detach (inferior
*inf
, int from_tty
)
6222 remote_detach_1 (inf
, from_tty
);
6226 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6228 remote_detach_1 (inf
, from_tty
);
6231 /* Target follow-fork function for remote targets. On entry, and
6232 at return, the current inferior is the fork parent.
6234 Note that although this is currently only used for extended-remote,
6235 it is named remote_follow_fork in anticipation of using it for the
6236 remote target as well. */
6239 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6240 target_waitkind fork_kind
, bool follow_child
,
6243 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6244 fork_kind
, follow_child
, detach_fork
);
6246 if ((fork_kind
== TARGET_WAITKIND_FORKED
6247 && m_features
.remote_fork_event_p ())
6248 || (fork_kind
== TARGET_WAITKIND_VFORKED
6249 && m_features
.remote_vfork_event_p ()))
6251 /* When following the parent and detaching the child, we detach
6252 the child here. For the case of following the child and
6253 detaching the parent, the detach is done in the target-
6254 independent follow fork code in infrun.c. We can't use
6255 target_detach when detaching an unfollowed child because
6256 the client side doesn't know anything about the child. */
6257 if (detach_fork
&& !follow_child
)
6259 /* Detach the fork child. */
6260 remote_detach_pid (child_ptid
.pid ());
6265 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6266 in the program space of the new inferior. */
6269 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6270 const char *execd_pathname
)
6272 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6274 /* We know that this is a target file name, so if it has the "target:"
6275 prefix we strip it off before saving it in the program space. */
6276 if (is_target_filename (execd_pathname
))
6277 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6279 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6282 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6285 remote_target::disconnect (const char *args
, int from_tty
)
6288 error (_("Argument given to \"disconnect\" when remotely debugging."));
6290 /* Make sure we unpush even the extended remote targets. Calling
6291 target_mourn_inferior won't unpush, and
6292 remote_target::mourn_inferior won't unpush if there is more than
6293 one inferior left. */
6294 remote_unpush_target (this);
6297 gdb_puts ("Ending remote debugging.\n");
6300 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6301 be chatty about it. */
6304 extended_remote_target::attach (const char *args
, int from_tty
)
6306 struct remote_state
*rs
= get_remote_state ();
6308 char *wait_status
= NULL
;
6310 pid
= parse_pid_to_attach (args
);
6312 /* Remote PID can be freely equal to getpid, do not check it here the same
6313 way as in other targets. */
6315 if (m_features
.packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6316 error (_("This target does not support attaching to a process"));
6318 target_announce_attach (from_tty
, pid
);
6320 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6322 getpkt (&rs
->buf
, 0);
6324 switch (m_features
.packet_ok (rs
->buf
, PACKET_vAttach
))
6327 if (!target_is_non_stop_p ())
6329 /* Save the reply for later. */
6330 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6331 strcpy (wait_status
, rs
->buf
.data ());
6333 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6334 error (_("Attaching to %s failed with: %s"),
6335 target_pid_to_str (ptid_t (pid
)).c_str (),
6338 case PACKET_UNKNOWN
:
6339 error (_("This target does not support attaching to a process"));
6341 error (_("Attaching to %s failed"),
6342 target_pid_to_str (ptid_t (pid
)).c_str ());
6345 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6347 inferior_ptid
= ptid_t (pid
);
6349 if (target_is_non_stop_p ())
6351 /* Get list of threads. */
6352 update_thread_list ();
6354 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6355 if (thread
!= nullptr)
6356 switch_to_thread (thread
);
6358 /* Invalidate our notion of the remote current thread. */
6359 record_currthread (rs
, minus_one_ptid
);
6363 /* Now, if we have thread information, update the main thread's
6365 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6367 /* Add the main thread to the thread list. We add the thread
6368 silently in this case (the final true parameter). */
6369 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6371 switch_to_thread (thr
);
6374 /* Next, if the target can specify a description, read it. We do
6375 this before anything involving memory or registers. */
6376 target_find_description ();
6378 if (!target_is_non_stop_p ())
6380 /* Use the previously fetched status. */
6381 gdb_assert (wait_status
!= NULL
);
6383 struct notif_event
*reply
6384 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6386 push_stop_reply ((struct stop_reply
*) reply
);
6390 gdb_assert (wait_status
== NULL
);
6392 gdb_assert (target_can_async_p ());
6396 /* Implementation of the to_post_attach method. */
6399 extended_remote_target::post_attach (int pid
)
6401 /* Get text, data & bss offsets. */
6404 /* In certain cases GDB might not have had the chance to start
6405 symbol lookup up until now. This could happen if the debugged
6406 binary is not using shared libraries, the vsyscall page is not
6407 present (on Linux) and the binary itself hadn't changed since the
6408 debugging process was started. */
6409 if (current_program_space
->symfile_object_file
!= NULL
)
6410 remote_check_symbols();
6414 /* Check for the availability of vCont. This function should also check
6418 remote_target::remote_vcont_probe ()
6420 remote_state
*rs
= get_remote_state ();
6423 strcpy (rs
->buf
.data (), "vCont?");
6425 getpkt (&rs
->buf
, 0);
6426 buf
= rs
->buf
.data ();
6428 /* Make sure that the features we assume are supported. */
6429 if (startswith (buf
, "vCont"))
6432 int support_c
, support_C
;
6434 rs
->supports_vCont
.s
= 0;
6435 rs
->supports_vCont
.S
= 0;
6438 rs
->supports_vCont
.t
= 0;
6439 rs
->supports_vCont
.r
= 0;
6440 while (p
&& *p
== ';')
6443 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6444 rs
->supports_vCont
.s
= 1;
6445 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6446 rs
->supports_vCont
.S
= 1;
6447 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6449 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6451 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6452 rs
->supports_vCont
.t
= 1;
6453 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6454 rs
->supports_vCont
.r
= 1;
6456 p
= strchr (p
, ';');
6459 /* If c, and C are not all supported, we can't use vCont. Clearing
6460 BUF will make packet_ok disable the packet. */
6461 if (!support_c
|| !support_C
)
6465 m_features
.packet_ok (rs
->buf
, PACKET_vCont
);
6468 /* Helper function for building "vCont" resumptions. Write a
6469 resumption to P. ENDP points to one-passed-the-end of the buffer
6470 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6471 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6472 resumed thread should be single-stepped and/or signalled. If PTID
6473 equals minus_one_ptid, then all threads are resumed; if PTID
6474 represents a process, then all threads of the process are
6478 remote_target::append_resumption (char *p
, char *endp
,
6479 ptid_t ptid
, int step
, gdb_signal siggnal
)
6481 struct remote_state
*rs
= get_remote_state ();
6483 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6484 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6486 /* GDB is willing to range step. */
6487 && use_range_stepping
6488 /* Target supports range stepping. */
6489 && rs
->supports_vCont
.r
6490 /* We don't currently support range stepping multiple
6491 threads with a wildcard (though the protocol allows it,
6492 so stubs shouldn't make an active effort to forbid
6494 && !(m_features
.remote_multi_process_p () && ptid
.is_pid ()))
6496 struct thread_info
*tp
;
6498 if (ptid
== minus_one_ptid
)
6500 /* If we don't know about the target thread's tid, then
6501 we're resuming magic_null_ptid (see caller). */
6502 tp
= this->find_thread (magic_null_ptid
);
6505 tp
= this->find_thread (ptid
);
6506 gdb_assert (tp
!= NULL
);
6508 if (tp
->control
.may_range_step
)
6510 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6512 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6513 phex_nz (tp
->control
.step_range_start
,
6515 phex_nz (tp
->control
.step_range_end
,
6519 p
+= xsnprintf (p
, endp
- p
, ";s");
6522 p
+= xsnprintf (p
, endp
- p
, ";s");
6523 else if (siggnal
!= GDB_SIGNAL_0
)
6524 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6526 p
+= xsnprintf (p
, endp
- p
, ";c");
6528 if (m_features
.remote_multi_process_p () && ptid
.is_pid ())
6532 /* All (-1) threads of process. */
6533 nptid
= ptid_t (ptid
.pid (), -1);
6535 p
+= xsnprintf (p
, endp
- p
, ":");
6536 p
= write_ptid (p
, endp
, nptid
);
6538 else if (ptid
!= minus_one_ptid
)
6540 p
+= xsnprintf (p
, endp
- p
, ":");
6541 p
= write_ptid (p
, endp
, ptid
);
6547 /* Clear the thread's private info on resume. */
6550 resume_clear_thread_private_info (struct thread_info
*thread
)
6552 if (thread
->priv
!= NULL
)
6554 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6556 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6557 priv
->watch_data_address
= 0;
6561 /* Append a vCont continue-with-signal action for threads that have a
6562 non-zero stop signal. */
6565 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6568 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6569 if (inferior_ptid
!= thread
->ptid
6570 && thread
->stop_signal () != GDB_SIGNAL_0
)
6572 p
= append_resumption (p
, endp
, thread
->ptid
,
6573 0, thread
->stop_signal ());
6574 thread
->set_stop_signal (GDB_SIGNAL_0
);
6575 resume_clear_thread_private_info (thread
);
6581 /* Set the target running, using the packets that use Hc
6585 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6588 struct remote_state
*rs
= get_remote_state ();
6591 rs
->last_sent_signal
= siggnal
;
6592 rs
->last_sent_step
= step
;
6594 /* The c/s/C/S resume packets use Hc, so set the continue
6596 if (ptid
== minus_one_ptid
)
6597 set_continue_thread (any_thread_ptid
);
6599 set_continue_thread (ptid
);
6601 for (thread_info
*thread
: all_non_exited_threads (this))
6602 resume_clear_thread_private_info (thread
);
6604 buf
= rs
->buf
.data ();
6605 if (::execution_direction
== EXEC_REVERSE
)
6607 /* We don't pass signals to the target in reverse exec mode. */
6608 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6609 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6612 if (step
&& m_features
.packet_support (PACKET_bs
) == PACKET_DISABLE
)
6613 error (_("Remote reverse-step not supported."));
6614 if (!step
&& m_features
.packet_support (PACKET_bc
) == PACKET_DISABLE
)
6615 error (_("Remote reverse-continue not supported."));
6617 strcpy (buf
, step
? "bs" : "bc");
6619 else if (siggnal
!= GDB_SIGNAL_0
)
6621 buf
[0] = step
? 'S' : 'C';
6622 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6623 buf
[2] = tohex (((int) siggnal
) & 0xf);
6627 strcpy (buf
, step
? "s" : "c");
6632 /* Resume the remote inferior by using a "vCont" packet. SCOPE_PTID,
6633 STEP, and SIGGNAL have the same meaning as in target_resume. This
6634 function returns non-zero iff it resumes the inferior.
6636 This function issues a strict subset of all possible vCont commands
6640 remote_target::remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
6641 enum gdb_signal siggnal
)
6643 struct remote_state
*rs
= get_remote_state ();
6647 /* No reverse execution actions defined for vCont. */
6648 if (::execution_direction
== EXEC_REVERSE
)
6651 if (m_features
.packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6654 p
= rs
->buf
.data ();
6655 endp
= p
+ get_remote_packet_size ();
6657 /* If we could generate a wider range of packets, we'd have to worry
6658 about overflowing BUF. Should there be a generic
6659 "multi-part-packet" packet? */
6661 p
+= xsnprintf (p
, endp
- p
, "vCont");
6663 if (scope_ptid
== magic_null_ptid
)
6665 /* MAGIC_NULL_PTID means that we don't have any active threads,
6666 so we don't have any TID numbers the inferior will
6667 understand. Make sure to only send forms that do not specify
6669 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6671 else if (scope_ptid
== minus_one_ptid
|| scope_ptid
.is_pid ())
6673 /* Resume all threads (of all processes, or of a single
6674 process), with preference for INFERIOR_PTID. This assumes
6675 inferior_ptid belongs to the set of all threads we are about
6677 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6679 /* Step inferior_ptid, with or without signal. */
6680 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6683 /* Also pass down any pending signaled resumption for other
6684 threads not the current. */
6685 p
= append_pending_thread_resumptions (p
, endp
, scope_ptid
);
6687 /* And continue others without a signal. */
6688 append_resumption (p
, endp
, scope_ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
6692 /* Scheduler locking; resume only SCOPE_PTID. */
6693 append_resumption (p
, endp
, scope_ptid
, step
, siggnal
);
6696 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
6699 if (target_is_non_stop_p ())
6701 /* In non-stop, the stub replies to vCont with "OK". The stop
6702 reply will be reported asynchronously by means of a `%Stop'
6704 getpkt (&rs
->buf
, 0);
6705 if (strcmp (rs
->buf
.data (), "OK") != 0)
6706 error (_("Unexpected vCont reply in non-stop mode: %s"),
6713 /* Tell the remote machine to resume. */
6716 remote_target::resume (ptid_t scope_ptid
, int step
, enum gdb_signal siggnal
)
6718 struct remote_state
*rs
= get_remote_state ();
6720 /* When connected in non-stop mode, the core resumes threads
6721 individually. Resuming remote threads directly in target_resume
6722 would thus result in sending one packet per thread. Instead, to
6723 minimize roundtrip latency, here we just store the resume
6724 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
6725 resumption will be done in remote_target::commit_resume, where we'll be
6726 able to do vCont action coalescing. */
6727 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
6729 remote_thread_info
*remote_thr
6730 = get_remote_thread_info (inferior_thread ());
6732 /* We don't expect the core to ask to resume an already resumed (from
6733 its point of view) thread. */
6734 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
6736 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
6738 /* There's actually nothing that says that the core can't
6739 request a wildcard resume in non-stop mode, though. It's
6740 just that we know it doesn't currently, so we don't bother
6742 gdb_assert (scope_ptid
== inferior_ptid
);
6746 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6747 (explained in remote-notif.c:handle_notification) so
6748 remote_notif_process is not called. We need find a place where
6749 it is safe to start a 'vNotif' sequence. It is good to do it
6750 before resuming inferior, because inferior was stopped and no RSP
6751 traffic at that moment. */
6752 if (!target_is_non_stop_p ())
6753 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
6755 rs
->last_resume_exec_dir
= ::execution_direction
;
6757 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6758 if (!remote_resume_with_vcont (scope_ptid
, step
, siggnal
))
6759 remote_resume_with_hc (scope_ptid
, step
, siggnal
);
6761 /* Update resumed state tracked by the remote target. */
6762 for (thread_info
*tp
: all_non_exited_threads (this, scope_ptid
))
6763 get_remote_thread_info (tp
)->set_resumed ();
6765 /* We've just told the target to resume. The remote server will
6766 wait for the inferior to stop, and then send a stop reply. In
6767 the mean time, we can't start another command/query ourselves
6768 because the stub wouldn't be ready to process it. This applies
6769 only to the base all-stop protocol, however. In non-stop (which
6770 only supports vCont), the stub replies with an "OK", and is
6771 immediate able to process further serial input. */
6772 if (!target_is_non_stop_p ())
6773 rs
->waiting_for_stop_reply
= 1;
6776 /* Private per-inferior info for target remote processes. */
6778 struct remote_inferior
: public private_inferior
6780 /* Whether we can send a wildcard vCont for this process. */
6781 bool may_wildcard_vcont
= true;
6784 /* Get the remote private inferior data associated to INF. */
6786 static remote_inferior
*
6787 get_remote_inferior (inferior
*inf
)
6789 if (inf
->priv
== NULL
)
6790 inf
->priv
.reset (new remote_inferior
);
6792 return gdb::checked_static_cast
<remote_inferior
*> (inf
->priv
.get ());
6795 /* Class used to track the construction of a vCont packet in the
6796 outgoing packet buffer. This is used to send multiple vCont
6797 packets if we have more actions than would fit a single packet. */
6802 explicit vcont_builder (remote_target
*remote
)
6809 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
6814 /* The remote target. */
6815 remote_target
*m_remote
;
6817 /* Pointer to the first action. P points here if no action has been
6819 char *m_first_action
;
6821 /* Where the next action will be appended. */
6824 /* The end of the buffer. Must never write past this. */
6828 /* Prepare the outgoing buffer for a new vCont packet. */
6831 vcont_builder::restart ()
6833 struct remote_state
*rs
= m_remote
->get_remote_state ();
6835 m_p
= rs
->buf
.data ();
6836 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
6837 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
6838 m_first_action
= m_p
;
6841 /* If the vCont packet being built has any action, send it to the
6845 vcont_builder::flush ()
6847 struct remote_state
*rs
;
6849 if (m_p
== m_first_action
)
6852 rs
= m_remote
->get_remote_state ();
6853 m_remote
->putpkt (rs
->buf
);
6854 m_remote
->getpkt (&rs
->buf
, 0);
6855 if (strcmp (rs
->buf
.data (), "OK") != 0)
6856 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
6859 /* The largest action is range-stepping, with its two addresses. This
6860 is more than sufficient. If a new, bigger action is created, it'll
6861 quickly trigger a failed assertion in append_resumption (and we'll
6863 #define MAX_ACTION_SIZE 200
6865 /* Append a new vCont action in the outgoing packet being built. If
6866 the action doesn't fit the packet along with previous actions, push
6867 what we've got so far to the remote end and start over a new vCont
6868 packet (with the new action). */
6871 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
6873 char buf
[MAX_ACTION_SIZE
+ 1];
6875 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
6876 ptid
, step
, siggnal
);
6878 /* Check whether this new action would fit in the vCont packet along
6879 with previous actions. If not, send what we've got so far and
6880 start a new vCont packet. */
6881 size_t rsize
= endp
- buf
;
6882 if (rsize
> m_endp
- m_p
)
6887 /* Should now fit. */
6888 gdb_assert (rsize
<= m_endp
- m_p
);
6891 memcpy (m_p
, buf
, rsize
);
6896 /* to_commit_resume implementation. */
6899 remote_target::commit_resumed ()
6901 /* If connected in all-stop mode, we'd send the remote resume
6902 request directly from remote_resume. Likewise if
6903 reverse-debugging, as there are no defined vCont actions for
6904 reverse execution. */
6905 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
6908 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6909 instead of resuming all threads of each process individually.
6910 However, if any thread of a process must remain halted, we can't
6911 send wildcard resumes and must send one action per thread.
6913 Care must be taken to not resume threads/processes the server
6914 side already told us are stopped, but the core doesn't know about
6915 yet, because the events are still in the vStopped notification
6918 #1 => vCont s:p1.1;c
6920 #3 <= %Stopped T05 p1.1
6925 #8 (infrun handles the stop for p1.1 and continues stepping)
6926 #9 => vCont s:p1.1;c
6928 The last vCont above would resume thread p1.2 by mistake, because
6929 the server has no idea that the event for p1.2 had not been
6932 The server side must similarly ignore resume actions for the
6933 thread that has a pending %Stopped notification (and any other
6934 threads with events pending), until GDB acks the notification
6935 with vStopped. Otherwise, e.g., the following case is
6938 #1 => g (or any other packet)
6940 #3 <= %Stopped T05 p1.2
6941 #4 => vCont s:p1.1;c
6944 Above, the server must not resume thread p1.2. GDB can't know
6945 that p1.2 stopped until it acks the %Stopped notification, and
6946 since from GDB's perspective all threads should be running, it
6949 Finally, special care must also be given to handling fork/vfork
6950 events. A (v)fork event actually tells us that two processes
6951 stopped -- the parent and the child. Until we follow the fork,
6952 we must not resume the child. Therefore, if we have a pending
6953 fork follow, we must not send a global wildcard resume action
6954 (vCont;c). We can still send process-wide wildcards though. */
6956 /* Start by assuming a global wildcard (vCont;c) is possible. */
6957 bool may_global_wildcard_vcont
= true;
6959 /* And assume every process is individually wildcard-able too. */
6960 for (inferior
*inf
: all_non_exited_inferiors (this))
6962 remote_inferior
*priv
= get_remote_inferior (inf
);
6964 priv
->may_wildcard_vcont
= true;
6967 /* Check for any pending events (not reported or processed yet) and
6968 disable process and global wildcard resumes appropriately. */
6969 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
6971 bool any_pending_vcont_resume
= false;
6973 for (thread_info
*tp
: all_non_exited_threads (this))
6975 remote_thread_info
*priv
= get_remote_thread_info (tp
);
6977 /* If a thread of a process is not meant to be resumed, then we
6978 can't wildcard that process. */
6979 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
6981 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
6983 /* And if we can't wildcard a process, we can't wildcard
6984 everything either. */
6985 may_global_wildcard_vcont
= false;
6989 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
6990 any_pending_vcont_resume
= true;
6992 /* If a thread is the parent of an unfollowed fork, then we
6993 can't do a global wildcard, as that would resume the fork
6995 if (thread_pending_fork_status (tp
) != nullptr)
6996 may_global_wildcard_vcont
= false;
6999 /* We didn't have any resumed thread pending a vCont resume, so nothing to
7001 if (!any_pending_vcont_resume
)
7004 /* Now let's build the vCont packet(s). Actions must be appended
7005 from narrower to wider scopes (thread -> process -> global). If
7006 we end up with too many actions for a single packet vcont_builder
7007 flushes the current vCont packet to the remote side and starts a
7009 struct vcont_builder
vcont_builder (this);
7011 /* Threads first. */
7012 for (thread_info
*tp
: all_non_exited_threads (this))
7014 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7016 /* If the thread was previously vCont-resumed, no need to send a specific
7017 action for it. If we didn't receive a resume request for it, don't
7018 send an action for it either. */
7019 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
7022 gdb_assert (!thread_is_in_step_over_chain (tp
));
7024 /* We should never be commit-resuming a thread that has a stop reply.
7025 Otherwise, we would end up reporting a stop event for a thread while
7026 it is running on the remote target. */
7027 remote_state
*rs
= get_remote_state ();
7028 for (const auto &stop_reply
: rs
->stop_reply_queue
)
7029 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
7031 const resumed_pending_vcont_info
&info
7032 = remote_thr
->resumed_pending_vcont_info ();
7034 /* Check if we need to send a specific action for this thread. If not,
7035 it will be included in a wildcard resume instead. */
7036 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
7037 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
7038 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
7040 remote_thr
->set_resumed ();
7043 /* Now check whether we can send any process-wide wildcard. This is
7044 to avoid sending a global wildcard in the case nothing is
7045 supposed to be resumed. */
7046 bool any_process_wildcard
= false;
7048 for (inferior
*inf
: all_non_exited_inferiors (this))
7050 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7052 any_process_wildcard
= true;
7057 if (any_process_wildcard
)
7059 /* If all processes are wildcard-able, then send a single "c"
7060 action, otherwise, send an "all (-1) threads of process"
7061 continue action for each running process, if any. */
7062 if (may_global_wildcard_vcont
)
7064 vcont_builder
.push_action (minus_one_ptid
,
7065 false, GDB_SIGNAL_0
);
7069 for (inferior
*inf
: all_non_exited_inferiors (this))
7071 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7073 vcont_builder
.push_action (ptid_t (inf
->pid
),
7074 false, GDB_SIGNAL_0
);
7080 vcont_builder
.flush ();
7083 /* Implementation of target_has_pending_events. */
7086 remote_target::has_pending_events ()
7088 if (target_can_async_p ())
7090 remote_state
*rs
= get_remote_state ();
7092 if (async_event_handler_marked (rs
->remote_async_inferior_event_token
))
7095 /* Note that BUFCNT can be negative, indicating sticky
7097 if (rs
->remote_desc
->bufcnt
!= 0)
7105 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
7106 thread, all threads of a remote process, or all threads of all
7110 remote_target::remote_stop_ns (ptid_t ptid
)
7112 struct remote_state
*rs
= get_remote_state ();
7113 char *p
= rs
->buf
.data ();
7114 char *endp
= p
+ get_remote_packet_size ();
7116 /* If any thread that needs to stop was resumed but pending a vCont
7117 resume, generate a phony stop_reply. However, first check
7118 whether the thread wasn't resumed with a signal. Generating a
7119 phony stop in that case would result in losing the signal. */
7120 bool needs_commit
= false;
7121 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7123 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7125 if (remote_thr
->get_resume_state ()
7126 == resume_state::RESUMED_PENDING_VCONT
)
7128 const resumed_pending_vcont_info
&info
7129 = remote_thr
->resumed_pending_vcont_info ();
7130 if (info
.sig
!= GDB_SIGNAL_0
)
7132 /* This signal must be forwarded to the inferior. We
7133 could commit-resume just this thread, but its simpler
7134 to just commit-resume everything. */
7135 needs_commit
= true;
7144 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7146 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7148 if (remote_thr
->get_resume_state ()
7149 == resume_state::RESUMED_PENDING_VCONT
)
7151 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
7152 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
7154 pulongest (tp
->ptid
.tid ()));
7156 /* Check that the thread wasn't resumed with a signal.
7157 Generating a phony stop would result in losing the
7159 const resumed_pending_vcont_info
&info
7160 = remote_thr
->resumed_pending_vcont_info ();
7161 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
7163 stop_reply
*sr
= new stop_reply ();
7164 sr
->ptid
= tp
->ptid
;
7166 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
7167 sr
->arch
= tp
->inf
->gdbarch
;
7168 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7169 sr
->watch_data_address
= 0;
7171 this->push_stop_reply (sr
);
7173 /* Pretend that this thread was actually resumed on the
7174 remote target, then stopped. If we leave it in the
7175 RESUMED_PENDING_VCONT state and the commit_resumed
7176 method is called while the stop reply is still in the
7177 queue, we'll end up reporting a stop event to the core
7178 for that thread while it is running on the remote
7179 target... that would be bad. */
7180 remote_thr
->set_resumed ();
7184 if (!rs
->supports_vCont
.t
)
7185 error (_("Remote server does not support stopping threads"));
7187 if (ptid
== minus_one_ptid
7188 || (!m_features
.remote_multi_process_p () && ptid
.is_pid ()))
7189 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7194 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7197 /* All (-1) threads of process. */
7198 nptid
= ptid_t (ptid
.pid (), -1);
7201 /* Small optimization: if we already have a stop reply for
7202 this thread, no use in telling the stub we want this
7204 if (peek_stop_reply (ptid
))
7210 write_ptid (p
, endp
, nptid
);
7213 /* In non-stop, we get an immediate OK reply. The stop reply will
7214 come in asynchronously by notification. */
7216 getpkt (&rs
->buf
, 0);
7217 if (strcmp (rs
->buf
.data (), "OK") != 0)
7218 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7222 /* All-stop version of target_interrupt. Sends a break or a ^C to
7223 interrupt the remote target. It is undefined which thread of which
7224 process reports the interrupt. */
7227 remote_target::remote_interrupt_as ()
7229 struct remote_state
*rs
= get_remote_state ();
7231 rs
->ctrlc_pending_p
= 1;
7233 /* If the inferior is stopped already, but the core didn't know
7234 about it yet, just ignore the request. The pending stop events
7235 will be collected in remote_wait. */
7236 if (stop_reply_queue_length () > 0)
7239 /* Send interrupt_sequence to remote target. */
7240 send_interrupt_sequence ();
7243 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7244 the remote target. It is undefined which thread of which process
7245 reports the interrupt. Throws an error if the packet is not
7246 supported by the server. */
7249 remote_target::remote_interrupt_ns ()
7251 struct remote_state
*rs
= get_remote_state ();
7252 char *p
= rs
->buf
.data ();
7253 char *endp
= p
+ get_remote_packet_size ();
7255 xsnprintf (p
, endp
- p
, "vCtrlC");
7257 /* In non-stop, we get an immediate OK reply. The stop reply will
7258 come in asynchronously by notification. */
7260 getpkt (&rs
->buf
, 0);
7262 switch (m_features
.packet_ok (rs
->buf
, PACKET_vCtrlC
))
7266 case PACKET_UNKNOWN
:
7267 error (_("No support for interrupting the remote target."));
7269 error (_("Interrupting target failed: %s"), rs
->buf
.data ());
7273 /* Implement the to_stop function for the remote targets. */
7276 remote_target::stop (ptid_t ptid
)
7278 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7280 if (target_is_non_stop_p ())
7281 remote_stop_ns (ptid
);
7284 /* We don't currently have a way to transparently pause the
7285 remote target in all-stop mode. Interrupt it instead. */
7286 remote_interrupt_as ();
7290 /* Implement the to_interrupt function for the remote targets. */
7293 remote_target::interrupt ()
7295 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7297 if (target_is_non_stop_p ())
7298 remote_interrupt_ns ();
7300 remote_interrupt_as ();
7303 /* Implement the to_pass_ctrlc function for the remote targets. */
7306 remote_target::pass_ctrlc ()
7308 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7310 struct remote_state
*rs
= get_remote_state ();
7312 /* If we're starting up, we're not fully synced yet. Quit
7314 if (rs
->starting_up
)
7316 /* If ^C has already been sent once, offer to disconnect. */
7317 else if (rs
->ctrlc_pending_p
)
7320 target_interrupt ();
7323 /* Ask the user what to do when an interrupt is received. */
7326 remote_target::interrupt_query ()
7328 struct remote_state
*rs
= get_remote_state ();
7330 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7332 if (query (_("The target is not responding to interrupt requests.\n"
7333 "Stop debugging it? ")))
7335 remote_unpush_target (this);
7336 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7341 if (query (_("Interrupted while waiting for the program.\n"
7342 "Give up waiting? ")))
7347 /* Enable/disable target terminal ownership. Most targets can use
7348 terminal groups to control terminal ownership. Remote targets are
7349 different in that explicit transfer of ownership to/from GDB/target
7353 remote_target::terminal_inferior ()
7355 /* NOTE: At this point we could also register our selves as the
7356 recipient of all input. Any characters typed could then be
7357 passed on down to the target. */
7361 remote_target::terminal_ours ()
7366 remote_console_output (const char *msg
)
7370 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7373 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7377 gdb_stdtarg
->puts (tb
);
7379 gdb_stdtarg
->flush ();
7382 /* Return the length of the stop reply queue. */
7385 remote_target::stop_reply_queue_length ()
7387 remote_state
*rs
= get_remote_state ();
7388 return rs
->stop_reply_queue
.size ();
7392 remote_notif_stop_parse (remote_target
*remote
,
7393 const notif_client
*self
, const char *buf
,
7394 struct notif_event
*event
)
7396 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7400 remote_notif_stop_ack (remote_target
*remote
,
7401 const notif_client
*self
, const char *buf
,
7402 struct notif_event
*event
)
7404 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
7407 putpkt (remote
, self
->ack_command
);
7409 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7410 the notification. It was left in the queue because we need to
7411 acknowledge it and pull the rest of the notifications out. */
7412 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7413 remote
->push_stop_reply (stop_reply
);
7417 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7418 const notif_client
*self
)
7420 /* We can't get pending events in remote_notif_process for
7421 notification stop, and we have to do this in remote_wait_ns
7422 instead. If we fetch all queued events from stub, remote stub
7423 may exit and we have no chance to process them back in
7425 remote_state
*rs
= remote
->get_remote_state ();
7426 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7430 stop_reply::~stop_reply ()
7432 for (cached_reg_t
®
: regcache
)
7436 static notif_event_up
7437 remote_notif_stop_alloc_reply ()
7439 return notif_event_up (new struct stop_reply ());
7442 /* A client of notification Stop. */
7444 const notif_client notif_client_stop
=
7448 remote_notif_stop_parse
,
7449 remote_notif_stop_ack
,
7450 remote_notif_stop_can_get_pending_events
,
7451 remote_notif_stop_alloc_reply
,
7455 /* If CONTEXT contains any fork child threads that have not been
7456 reported yet, remove them from the CONTEXT list. If such a
7457 thread exists it is because we are stopped at a fork catchpoint
7458 and have not yet called follow_fork, which will set up the
7459 host-side data structures for the new process. */
7462 remote_target::remove_new_fork_children (threads_listing_context
*context
)
7464 const notif_client
*notif
= ¬if_client_stop
;
7466 /* For any threads stopped at a fork event, remove the corresponding
7467 fork child threads from the CONTEXT list. */
7468 for (thread_info
*thread
: all_non_exited_threads (this))
7470 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
7475 context
->remove_thread (ws
->child_ptid ());
7478 /* Check for any pending fork events (not reported or processed yet)
7479 in process PID and remove those fork child threads from the
7480 CONTEXT list as well. */
7481 remote_notif_get_pending_events (notif
);
7482 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7483 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7484 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7485 context
->remove_thread (event
->ws
.child_ptid ());
7486 else if (event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7487 context
->remove_thread (event
->ptid
);
7490 /* Check whether any event pending in the vStopped queue would prevent a
7491 global or process wildcard vCont action. Set *may_global_wildcard to
7492 false if we can't do a global wildcard (vCont;c), and clear the event
7493 inferior's may_wildcard_vcont flag if we can't do a process-wide
7494 wildcard resume (vCont;c:pPID.-1). */
7497 remote_target::check_pending_events_prevent_wildcard_vcont
7498 (bool *may_global_wildcard
)
7500 const notif_client
*notif
= ¬if_client_stop
;
7502 remote_notif_get_pending_events (notif
);
7503 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7505 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7506 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7509 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7510 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7511 *may_global_wildcard
= false;
7513 /* This may be the first time we heard about this process.
7514 Regardless, we must not do a global wildcard resume, otherwise
7515 we'd resume this process too. */
7516 *may_global_wildcard
= false;
7517 if (event
->ptid
!= null_ptid
)
7519 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7521 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7526 /* Discard all pending stop replies of inferior INF. */
7529 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7531 struct stop_reply
*reply
;
7532 struct remote_state
*rs
= get_remote_state ();
7533 struct remote_notif_state
*rns
= rs
->notif_state
;
7535 /* This function can be notified when an inferior exists. When the
7536 target is not remote, the notification state is NULL. */
7537 if (rs
->remote_desc
== NULL
)
7540 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
7542 /* Discard the in-flight notification. */
7543 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7545 /* Leave the notification pending, since the server expects that
7546 we acknowledge it with vStopped. But clear its contents, so
7547 that later on when we acknowledge it, we also discard it. */
7549 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7550 reply
->ptid
.to_string().c_str(),
7551 reply
->ws
.to_string ().c_str ());
7552 reply
->ws
.set_ignore ();
7555 /* Discard the stop replies we have already pulled with
7557 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7558 rs
->stop_reply_queue
.end (),
7559 [=] (const stop_reply_up
&event
)
7561 return event
->ptid
.pid () == inf
->pid
;
7563 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7565 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7566 (*it
)->ptid
.to_string().c_str(),
7567 (*it
)->ws
.to_string ().c_str ());
7568 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7571 /* Discard the stop replies for RS in stop_reply_queue. */
7574 remote_target::discard_pending_stop_replies_in_queue ()
7576 remote_state
*rs
= get_remote_state ();
7578 /* Discard the stop replies we have already pulled with
7580 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7581 rs
->stop_reply_queue
.end (),
7582 [=] (const stop_reply_up
&event
)
7584 return event
->rs
== rs
;
7586 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7589 /* Remove the first reply in 'stop_reply_queue' which matches
7593 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7595 remote_state
*rs
= get_remote_state ();
7597 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7598 rs
->stop_reply_queue
.end (),
7599 [=] (const stop_reply_up
&event
)
7601 return event
->ptid
.matches (ptid
);
7603 struct stop_reply
*result
;
7604 if (iter
== rs
->stop_reply_queue
.end ())
7608 result
= iter
->release ();
7609 rs
->stop_reply_queue
.erase (iter
);
7613 gdb_printf (gdb_stdlog
,
7614 "notif: discard queued event: 'Stop' in %s\n",
7615 ptid
.to_string ().c_str ());
7620 /* Look for a queued stop reply belonging to PTID. If one is found,
7621 remove it from the queue, and return it. Returns NULL if none is
7622 found. If there are still queued events left to process, tell the
7623 event loop to get back to target_wait soon. */
7626 remote_target::queued_stop_reply (ptid_t ptid
)
7628 remote_state
*rs
= get_remote_state ();
7629 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
7631 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7633 /* There's still at least an event left. */
7634 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7640 /* Push a fully parsed stop reply in the stop reply queue. Since we
7641 know that we now have at least one queued event left to pass to the
7642 core side, tell the event loop to get back to target_wait soon. */
7645 remote_target::push_stop_reply (struct stop_reply
*new_event
)
7647 remote_state
*rs
= get_remote_state ();
7648 rs
->stop_reply_queue
.push_back (stop_reply_up (new_event
));
7651 gdb_printf (gdb_stdlog
,
7652 "notif: push 'Stop' %s to queue %d\n",
7653 new_event
->ptid
.to_string ().c_str (),
7654 int (rs
->stop_reply_queue
.size ()));
7656 /* Mark the pending event queue only if async mode is currently enabled.
7657 If async mode is not currently enabled, then, if it later becomes
7658 enabled, and there are events in this queue, we will mark the event
7659 token at that point, see remote_target::async. */
7660 if (target_is_async_p ())
7661 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7664 /* Returns true if we have a stop reply for PTID. */
7667 remote_target::peek_stop_reply (ptid_t ptid
)
7669 remote_state
*rs
= get_remote_state ();
7670 for (auto &event
: rs
->stop_reply_queue
)
7671 if (ptid
== event
->ptid
7672 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7677 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7678 starting with P and ending with PEND matches PREFIX. */
7681 strprefix (const char *p
, const char *pend
, const char *prefix
)
7683 for ( ; p
< pend
; p
++, prefix
++)
7686 return *prefix
== '\0';
7689 /* Parse the stop reply in BUF. Either the function succeeds, and the
7690 result is stored in EVENT, or throws an error. */
7693 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
7695 remote_arch_state
*rsa
= NULL
;
7700 event
->ptid
= null_ptid
;
7701 event
->rs
= get_remote_state ();
7702 event
->ws
.set_ignore ();
7703 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7704 event
->regcache
.clear ();
7709 case 'T': /* Status with PC, SP, FP, ... */
7710 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7711 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7713 n... = register number
7714 r... = register contents
7717 p
= &buf
[3]; /* after Txx */
7723 p1
= strchr (p
, ':');
7725 error (_("Malformed packet(a) (missing colon): %s\n\
7729 error (_("Malformed packet(a) (missing register number): %s\n\
7733 /* Some "registers" are actually extended stop information.
7734 Note if you're adding a new entry here: GDB 7.9 and
7735 earlier assume that all register "numbers" that start
7736 with an hex digit are real register numbers. Make sure
7737 the server only sends such a packet if it knows the
7738 client understands it. */
7740 if (strprefix (p
, p1
, "thread"))
7741 event
->ptid
= read_ptid (++p1
, &p
);
7742 else if (strprefix (p
, p1
, "syscall_entry"))
7746 p
= unpack_varlen_hex (++p1
, &sysno
);
7747 event
->ws
.set_syscall_entry ((int) sysno
);
7749 else if (strprefix (p
, p1
, "syscall_return"))
7753 p
= unpack_varlen_hex (++p1
, &sysno
);
7754 event
->ws
.set_syscall_return ((int) sysno
);
7756 else if (strprefix (p
, p1
, "watch")
7757 || strprefix (p
, p1
, "rwatch")
7758 || strprefix (p
, p1
, "awatch"))
7760 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
7761 p
= unpack_varlen_hex (++p1
, &addr
);
7762 event
->watch_data_address
= (CORE_ADDR
) addr
;
7764 else if (strprefix (p
, p1
, "swbreak"))
7766 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
7768 /* Make sure the stub doesn't forget to indicate support
7770 if (m_features
.packet_support (PACKET_swbreak_feature
)
7772 error (_("Unexpected swbreak stop reason"));
7774 /* The value part is documented as "must be empty",
7775 though we ignore it, in case we ever decide to make
7776 use of it in a backward compatible way. */
7777 p
= strchrnul (p1
+ 1, ';');
7779 else if (strprefix (p
, p1
, "hwbreak"))
7781 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
7783 /* Make sure the stub doesn't forget to indicate support
7785 if (m_features
.packet_support (PACKET_hwbreak_feature
)
7787 error (_("Unexpected hwbreak stop reason"));
7790 p
= strchrnul (p1
+ 1, ';');
7792 else if (strprefix (p
, p1
, "library"))
7794 event
->ws
.set_loaded ();
7795 p
= strchrnul (p1
+ 1, ';');
7797 else if (strprefix (p
, p1
, "replaylog"))
7799 event
->ws
.set_no_history ();
7800 /* p1 will indicate "begin" or "end", but it makes
7801 no difference for now, so ignore it. */
7802 p
= strchrnul (p1
+ 1, ';');
7804 else if (strprefix (p
, p1
, "core"))
7808 p
= unpack_varlen_hex (++p1
, &c
);
7811 else if (strprefix (p
, p1
, "fork"))
7812 event
->ws
.set_forked (read_ptid (++p1
, &p
));
7813 else if (strprefix (p
, p1
, "vfork"))
7814 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
7815 else if (strprefix (p
, p1
, "vforkdone"))
7817 event
->ws
.set_vfork_done ();
7818 p
= strchrnul (p1
+ 1, ';');
7820 else if (strprefix (p
, p1
, "exec"))
7825 /* Determine the length of the execd pathname. */
7826 p
= unpack_varlen_hex (++p1
, &ignored
);
7827 pathlen
= (p
- p1
) / 2;
7829 /* Save the pathname for event reporting and for
7830 the next run command. */
7831 gdb::unique_xmalloc_ptr
<char> pathname
7832 ((char *) xmalloc (pathlen
+ 1));
7833 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
7834 pathname
.get ()[pathlen
] = '\0';
7836 /* This is freed during event handling. */
7837 event
->ws
.set_execd (std::move (pathname
));
7839 /* Skip the registers included in this packet, since
7840 they may be for an architecture different from the
7841 one used by the original program. */
7844 else if (strprefix (p
, p1
, "create"))
7846 event
->ws
.set_thread_created ();
7847 p
= strchrnul (p1
+ 1, ';');
7856 p
= strchrnul (p1
+ 1, ';');
7861 /* Maybe a real ``P'' register number. */
7862 p_temp
= unpack_varlen_hex (p
, &pnum
);
7863 /* If the first invalid character is the colon, we got a
7864 register number. Otherwise, it's an unknown stop
7868 /* If we haven't parsed the event's thread yet, find
7869 it now, in order to find the architecture of the
7870 reported expedited registers. */
7871 if (event
->ptid
== null_ptid
)
7873 /* If there is no thread-id information then leave
7874 the event->ptid as null_ptid. Later in
7875 process_stop_reply we will pick a suitable
7877 const char *thr
= strstr (p1
+ 1, ";thread:");
7879 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
7886 = (event
->ptid
== null_ptid
7888 : find_inferior_ptid (this, event
->ptid
));
7889 /* If this is the first time we learn anything
7890 about this process, skip the registers
7891 included in this packet, since we don't yet
7892 know which architecture to use to parse them.
7893 We'll determine the architecture later when
7894 we process the stop reply and retrieve the
7895 target description, via
7896 remote_notice_new_inferior ->
7897 post_create_inferior. */
7900 p
= strchrnul (p1
+ 1, ';');
7905 event
->arch
= inf
->gdbarch
;
7906 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
7910 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
7911 cached_reg_t cached_reg
;
7914 error (_("Remote sent bad register number %s: %s\n\
7916 hex_string (pnum
), p
, buf
);
7918 cached_reg
.num
= reg
->regnum
;
7919 cached_reg
.data
= (gdb_byte
*)
7920 xmalloc (register_size (event
->arch
, reg
->regnum
));
7923 fieldsize
= hex2bin (p
, cached_reg
.data
,
7924 register_size (event
->arch
, reg
->regnum
));
7926 if (fieldsize
< register_size (event
->arch
, reg
->regnum
))
7927 warning (_("Remote reply is too short: %s"), buf
);
7929 event
->regcache
.push_back (cached_reg
);
7933 /* Not a number. Silently skip unknown optional
7935 p
= strchrnul (p1
+ 1, ';');
7940 error (_("Remote register badly formatted: %s\nhere: %s"),
7945 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7949 case 'S': /* Old style status, just signal only. */
7953 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
7954 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
7955 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
7957 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
7960 case 'w': /* Thread exited. */
7964 p
= unpack_varlen_hex (&buf
[1], &value
);
7965 event
->ws
.set_thread_exited (value
);
7967 error (_("stop reply packet badly formatted: %s"), buf
);
7968 event
->ptid
= read_ptid (++p
, NULL
);
7971 case 'W': /* Target exited. */
7976 /* GDB used to accept only 2 hex chars here. Stubs should
7977 only send more if they detect GDB supports multi-process
7979 p
= unpack_varlen_hex (&buf
[1], &value
);
7983 /* The remote process exited. */
7984 event
->ws
.set_exited (value
);
7988 /* The remote process exited with a signal. */
7989 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
7990 event
->ws
.set_signalled ((enum gdb_signal
) value
);
7992 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
7995 /* If no process is specified, return null_ptid, and let the
7996 caller figure out the right process to use. */
8006 else if (startswith (p
, "process:"))
8010 p
+= sizeof ("process:") - 1;
8011 unpack_varlen_hex (p
, &upid
);
8015 error (_("unknown stop reply packet: %s"), buf
);
8018 error (_("unknown stop reply packet: %s"), buf
);
8019 event
->ptid
= ptid_t (pid
);
8023 event
->ws
.set_no_resumed ();
8024 event
->ptid
= minus_one_ptid
;
8029 /* When the stub wants to tell GDB about a new notification reply, it
8030 sends a notification (%Stop, for example). Those can come it at
8031 any time, hence, we have to make sure that any pending
8032 putpkt/getpkt sequence we're making is finished, before querying
8033 the stub for more events with the corresponding ack command
8034 (vStopped, for example). E.g., if we started a vStopped sequence
8035 immediately upon receiving the notification, something like this
8043 1.6) <-- (registers reply to step #1.3)
8045 Obviously, the reply in step #1.6 would be unexpected to a vStopped
8048 To solve this, whenever we parse a %Stop notification successfully,
8049 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
8050 doing whatever we were doing:
8056 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
8057 2.5) <-- (registers reply to step #2.3)
8059 Eventually after step #2.5, we return to the event loop, which
8060 notices there's an event on the
8061 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
8062 associated callback --- the function below. At this point, we're
8063 always safe to start a vStopped sequence. :
8066 2.7) <-- T05 thread:2
8072 remote_target::remote_notif_get_pending_events (const notif_client
*nc
)
8074 struct remote_state
*rs
= get_remote_state ();
8076 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
8079 gdb_printf (gdb_stdlog
,
8080 "notif: process: '%s' ack pending event\n",
8084 nc
->ack (this, nc
, rs
->buf
.data (),
8085 rs
->notif_state
->pending_event
[nc
->id
]);
8086 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
8090 getpkt (&rs
->buf
, 0);
8091 if (strcmp (rs
->buf
.data (), "OK") == 0)
8094 remote_notif_ack (this, nc
, rs
->buf
.data ());
8100 gdb_printf (gdb_stdlog
,
8101 "notif: process: '%s' no pending reply\n",
8106 /* Wrapper around remote_target::remote_notif_get_pending_events to
8107 avoid having to export the whole remote_target class. */
8110 remote_notif_get_pending_events (remote_target
*remote
, const notif_client
*nc
)
8112 remote
->remote_notif_get_pending_events (nc
);
8115 /* Called from process_stop_reply when the stop packet we are responding
8116 to didn't include a process-id or thread-id. STATUS is the stop event
8117 we are responding to.
8119 It is the task of this function to select a suitable thread (or process)
8120 and return its ptid, this is the thread (or process) we will assume the
8121 stop event came from.
8123 In some cases there isn't really any choice about which thread (or
8124 process) is selected, a basic remote with a single process containing a
8125 single thread might choose not to send any process-id or thread-id in
8126 its stop packets, this function will select and return the one and only
8129 However, if a target supports multiple threads (or processes) and still
8130 doesn't include a thread-id (or process-id) in its stop packet then
8131 first, this is a badly behaving target, and second, we're going to have
8132 to select a thread (or process) at random and use that. This function
8133 will print a warning to the user if it detects that there is the
8134 possibility that GDB is guessing which thread (or process) to
8137 Note that this is called before GDB fetches the updated thread list from the
8138 target. So it's possible for the stop reply to be ambiguous and for GDB to
8139 not realize it. For example, if there's initially one thread, the target
8140 spawns a second thread, and then sends a stop reply without an id that
8141 concerns the first thread. GDB will assume the stop reply is about the
8142 first thread - the only thread it knows about - without printing a warning.
8143 Anyway, if the remote meant for the stop reply to be about the second thread,
8144 then it would be really broken, because GDB doesn't know about that thread
8148 remote_target::select_thread_for_ambiguous_stop_reply
8149 (const target_waitstatus
&status
)
8151 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8153 /* Some stop events apply to all threads in an inferior, while others
8154 only apply to a single thread. */
8155 bool process_wide_stop
8156 = (status
.kind () == TARGET_WAITKIND_EXITED
8157 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
8159 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
8161 thread_info
*first_resumed_thread
= nullptr;
8162 bool ambiguous
= false;
8164 /* Consider all non-exited threads of the target, find the first resumed
8166 for (thread_info
*thr
: all_non_exited_threads (this))
8168 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
8170 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
8173 if (first_resumed_thread
== nullptr)
8174 first_resumed_thread
= thr
;
8175 else if (!process_wide_stop
8176 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8180 gdb_assert (first_resumed_thread
!= nullptr);
8182 remote_debug_printf ("first resumed thread is %s",
8183 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8184 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8186 /* Warn if the remote target is sending ambiguous stop replies. */
8189 static bool warned
= false;
8193 /* If you are seeing this warning then the remote target has
8194 stopped without specifying a thread-id, but the target
8195 does have multiple threads (or inferiors), and so GDB is
8196 having to guess which thread stopped.
8198 Examples of what might cause this are the target sending
8199 and 'S' stop packet, or a 'T' stop packet and not
8200 including a thread-id.
8202 Additionally, the target might send a 'W' or 'X packet
8203 without including a process-id, when the target has
8204 multiple running inferiors. */
8205 if (process_wide_stop
)
8206 warning (_("multi-inferior target stopped without "
8207 "sending a process-id, using first "
8208 "non-exited inferior"));
8210 warning (_("multi-threaded target stopped without "
8211 "sending a thread-id, using first "
8212 "non-exited thread"));
8217 /* If this is a stop for all threads then don't use a particular threads
8218 ptid, instead create a new ptid where only the pid field is set. */
8219 if (process_wide_stop
)
8220 return ptid_t (first_resumed_thread
->ptid
.pid ());
8222 return first_resumed_thread
->ptid
;
8225 /* Called when it is decided that STOP_REPLY holds the info of the
8226 event that is to be returned to the core. This function always
8227 destroys STOP_REPLY. */
8230 remote_target::process_stop_reply (struct stop_reply
*stop_reply
,
8231 struct target_waitstatus
*status
)
8233 *status
= stop_reply
->ws
;
8234 ptid_t ptid
= stop_reply
->ptid
;
8236 /* If no thread/process was reported by the stub then select a suitable
8238 if (ptid
== null_ptid
)
8239 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8240 gdb_assert (ptid
!= null_ptid
);
8242 if (status
->kind () != TARGET_WAITKIND_EXITED
8243 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8244 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8246 /* Expedited registers. */
8247 if (!stop_reply
->regcache
.empty ())
8249 struct regcache
*regcache
8250 = get_thread_arch_regcache (this, ptid
, stop_reply
->arch
);
8252 for (cached_reg_t
®
: stop_reply
->regcache
)
8254 regcache
->raw_supply (reg
.num
, reg
.data
);
8258 stop_reply
->regcache
.clear ();
8261 remote_notice_new_inferior (ptid
, false);
8262 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8263 remote_thr
->core
= stop_reply
->core
;
8264 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8265 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8267 if (target_is_non_stop_p ())
8269 /* If the target works in non-stop mode, a stop-reply indicates that
8270 only this thread stopped. */
8271 remote_thr
->set_not_resumed ();
8275 /* If the target works in all-stop mode, a stop-reply indicates that
8276 all the target's threads stopped. */
8277 for (thread_info
*tp
: all_non_exited_threads (this))
8278 get_remote_thread_info (tp
)->set_not_resumed ();
8286 /* The non-stop mode version of target_wait. */
8289 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8290 target_wait_flags options
)
8292 struct remote_state
*rs
= get_remote_state ();
8293 struct stop_reply
*stop_reply
;
8297 /* If in non-stop mode, get out of getpkt even if a
8298 notification is received. */
8300 ret
= getpkt_or_notif_sane (&rs
->buf
, 0 /* forever */, &is_notif
);
8303 if (ret
!= -1 && !is_notif
)
8306 case 'E': /* Error of some sort. */
8307 /* We're out of sync with the target now. Did it continue
8308 or not? We can't tell which thread it was in non-stop,
8309 so just ignore this. */
8310 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8312 case 'O': /* Console output. */
8313 remote_console_output (&rs
->buf
[1]);
8316 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8320 /* Acknowledge a pending stop reply that may have arrived in the
8322 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8323 remote_notif_get_pending_events (¬if_client_stop
);
8325 /* If indeed we noticed a stop reply, we're done. */
8326 stop_reply
= queued_stop_reply (ptid
);
8327 if (stop_reply
!= NULL
)
8328 return process_stop_reply (stop_reply
, status
);
8330 /* Still no event. If we're just polling for an event, then
8331 return to the event loop. */
8332 if (options
& TARGET_WNOHANG
)
8334 status
->set_ignore ();
8335 return minus_one_ptid
;
8338 /* Otherwise do a blocking wait. */
8339 ret
= getpkt_or_notif_sane (&rs
->buf
, 1 /* forever */, &is_notif
);
8343 /* Return the first resumed thread. */
8346 first_remote_resumed_thread (remote_target
*target
)
8348 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8354 /* Wait until the remote machine stops, then return, storing status in
8355 STATUS just as `wait' would. */
8358 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8359 target_wait_flags options
)
8361 struct remote_state
*rs
= get_remote_state ();
8362 ptid_t event_ptid
= null_ptid
;
8364 struct stop_reply
*stop_reply
;
8368 status
->set_ignore ();
8370 stop_reply
= queued_stop_reply (ptid
);
8371 if (stop_reply
!= NULL
)
8373 /* None of the paths that push a stop reply onto the queue should
8374 have set the waiting_for_stop_reply flag. */
8375 gdb_assert (!rs
->waiting_for_stop_reply
);
8376 event_ptid
= process_stop_reply (stop_reply
, status
);
8380 int forever
= ((options
& TARGET_WNOHANG
) == 0
8381 && rs
->wait_forever_enabled_p
);
8383 if (!rs
->waiting_for_stop_reply
)
8385 status
->set_no_resumed ();
8386 return minus_one_ptid
;
8389 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8390 _never_ wait for ever -> test on target_is_async_p().
8391 However, before we do that we need to ensure that the caller
8392 knows how to take the target into/out of async mode. */
8394 int ret
= getpkt_or_notif_sane (&rs
->buf
, forever
, &is_notif
);
8396 /* GDB gets a notification. Return to core as this event is
8398 if (ret
!= -1 && is_notif
)
8399 return minus_one_ptid
;
8401 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8402 return minus_one_ptid
;
8404 buf
= rs
->buf
.data ();
8406 /* Assume that the target has acknowledged Ctrl-C unless we receive
8407 an 'F' or 'O' packet. */
8408 if (buf
[0] != 'F' && buf
[0] != 'O')
8409 rs
->ctrlc_pending_p
= 0;
8413 case 'E': /* Error of some sort. */
8414 /* We're out of sync with the target now. Did it continue or
8415 not? Not is more likely, so report a stop. */
8416 rs
->waiting_for_stop_reply
= 0;
8418 warning (_("Remote failure reply: %s"), buf
);
8419 status
->set_stopped (GDB_SIGNAL_0
);
8421 case 'F': /* File-I/O request. */
8422 /* GDB may access the inferior memory while handling the File-I/O
8423 request, but we don't want GDB accessing memory while waiting
8424 for a stop reply. See the comments in putpkt_binary. Set
8425 waiting_for_stop_reply to 0 temporarily. */
8426 rs
->waiting_for_stop_reply
= 0;
8427 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8428 rs
->ctrlc_pending_p
= 0;
8429 /* GDB handled the File-I/O request, and the target is running
8430 again. Keep waiting for events. */
8431 rs
->waiting_for_stop_reply
= 1;
8433 case 'N': case 'T': case 'S': case 'X': case 'W':
8435 /* There is a stop reply to handle. */
8436 rs
->waiting_for_stop_reply
= 0;
8439 = (struct stop_reply
*) remote_notif_parse (this,
8443 event_ptid
= process_stop_reply (stop_reply
, status
);
8446 case 'O': /* Console output. */
8447 remote_console_output (buf
+ 1);
8450 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8452 /* Zero length reply means that we tried 'S' or 'C' and the
8453 remote system doesn't support it. */
8454 target_terminal::ours_for_output ();
8456 ("Can't send signals to this remote system. %s not sent.\n",
8457 gdb_signal_to_name (rs
->last_sent_signal
));
8458 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8459 target_terminal::inferior ();
8461 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8467 warning (_("Invalid remote reply: %s"), buf
);
8472 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8473 return minus_one_ptid
;
8474 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8476 /* Nothing interesting happened. If we're doing a non-blocking
8477 poll, we're done. Otherwise, go back to waiting. */
8478 if (options
& TARGET_WNOHANG
)
8479 return minus_one_ptid
;
8483 else if (status
->kind () != TARGET_WAITKIND_EXITED
8484 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8486 if (event_ptid
!= null_ptid
)
8487 record_currthread (rs
, event_ptid
);
8489 event_ptid
= first_remote_resumed_thread (this);
8493 /* A process exit. Invalidate our notion of current thread. */
8494 record_currthread (rs
, minus_one_ptid
);
8495 /* It's possible that the packet did not include a pid. */
8496 if (event_ptid
== null_ptid
)
8497 event_ptid
= first_remote_resumed_thread (this);
8498 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8499 if (event_ptid
== null_ptid
)
8500 event_ptid
= magic_null_ptid
;
8506 /* Wait until the remote machine stops, then return, storing status in
8507 STATUS just as `wait' would. */
8510 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8511 target_wait_flags options
)
8513 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8515 remote_state
*rs
= get_remote_state ();
8517 /* Start by clearing the flag that asks for our wait method to be called,
8518 we'll mark it again at the end if needed. If the target is not in
8519 async mode then the async token should not be marked. */
8520 if (target_is_async_p ())
8521 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
8523 gdb_assert (!async_event_handler_marked
8524 (rs
->remote_async_inferior_event_token
));
8528 if (target_is_non_stop_p ())
8529 event_ptid
= wait_ns (ptid
, status
, options
);
8531 event_ptid
= wait_as (ptid
, status
, options
);
8533 if (target_is_async_p ())
8535 /* If there are events left in the queue, or unacknowledged
8536 notifications, then tell the event loop to call us again. */
8537 if (!rs
->stop_reply_queue
.empty ()
8538 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8539 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
8545 /* Fetch a single register using a 'p' packet. */
8548 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8551 struct gdbarch
*gdbarch
= regcache
->arch ();
8552 struct remote_state
*rs
= get_remote_state ();
8554 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8557 if (m_features
.packet_support (PACKET_p
) == PACKET_DISABLE
)
8560 if (reg
->pnum
== -1)
8563 p
= rs
->buf
.data ();
8565 p
+= hexnumstr (p
, reg
->pnum
);
8568 getpkt (&rs
->buf
, 0);
8570 buf
= rs
->buf
.data ();
8572 switch (m_features
.packet_ok (rs
->buf
, PACKET_p
))
8576 case PACKET_UNKNOWN
:
8579 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8580 gdbarch_register_name (regcache
->arch (),
8585 /* If this register is unfetchable, tell the regcache. */
8588 regcache
->raw_supply (reg
->regnum
, NULL
);
8592 /* Otherwise, parse and supply the value. */
8598 error (_("fetch_register_using_p: early buf termination"));
8600 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8603 regcache
->raw_supply (reg
->regnum
, regp
);
8607 /* Fetch the registers included in the target's 'g' packet. */
8610 remote_target::send_g_packet ()
8612 struct remote_state
*rs
= get_remote_state ();
8615 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8617 getpkt (&rs
->buf
, 0);
8618 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8619 error (_("Could not read registers; remote failure reply '%s'"),
8622 /* We can get out of synch in various cases. If the first character
8623 in the buffer is not a hex character, assume that has happened
8624 and try to fetch another packet to read. */
8625 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8626 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8627 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8628 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8630 remote_debug_printf ("Bad register packet; fetching a new packet");
8631 getpkt (&rs
->buf
, 0);
8634 buf_len
= strlen (rs
->buf
.data ());
8636 /* Sanity check the received packet. */
8637 if (buf_len
% 2 != 0)
8638 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8644 remote_target::process_g_packet (struct regcache
*regcache
)
8646 struct gdbarch
*gdbarch
= regcache
->arch ();
8647 struct remote_state
*rs
= get_remote_state ();
8648 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8653 buf_len
= strlen (rs
->buf
.data ());
8655 /* Further sanity checks, with knowledge of the architecture. */
8656 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8657 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8659 rsa
->sizeof_g_packet
, buf_len
/ 2,
8662 /* Save the size of the packet sent to us by the target. It is used
8663 as a heuristic when determining the max size of packets that the
8664 target can safely receive. */
8665 if (rsa
->actual_register_packet_size
== 0)
8666 rsa
->actual_register_packet_size
= buf_len
;
8668 /* If this is smaller than we guessed the 'g' packet would be,
8669 update our records. A 'g' reply that doesn't include a register's
8670 value implies either that the register is not available, or that
8671 the 'p' packet must be used. */
8672 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8674 long sizeof_g_packet
= buf_len
/ 2;
8676 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8678 long offset
= rsa
->regs
[i
].offset
;
8679 long reg_size
= register_size (gdbarch
, i
);
8681 if (rsa
->regs
[i
].pnum
== -1)
8684 if (offset
>= sizeof_g_packet
)
8685 rsa
->regs
[i
].in_g_packet
= 0;
8686 else if (offset
+ reg_size
> sizeof_g_packet
)
8687 error (_("Truncated register %d in remote 'g' packet"), i
);
8689 rsa
->regs
[i
].in_g_packet
= 1;
8692 /* Looks valid enough, we can assume this is the correct length
8693 for a 'g' packet. It's important not to adjust
8694 rsa->sizeof_g_packet if we have truncated registers otherwise
8695 this "if" won't be run the next time the method is called
8696 with a packet of the same size and one of the internal errors
8697 below will trigger instead. */
8698 rsa
->sizeof_g_packet
= sizeof_g_packet
;
8701 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
8703 /* Unimplemented registers read as all bits zero. */
8704 memset (regs
, 0, rsa
->sizeof_g_packet
);
8706 /* Reply describes registers byte by byte, each byte encoded as two
8707 hex characters. Suck them all up, then supply them to the
8708 register cacheing/storage mechanism. */
8710 p
= rs
->buf
.data ();
8711 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
8713 if (p
[0] == 0 || p
[1] == 0)
8714 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
8715 internal_error (_("unexpected end of 'g' packet reply"));
8717 if (p
[0] == 'x' && p
[1] == 'x')
8718 regs
[i
] = 0; /* 'x' */
8720 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8724 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8726 struct packet_reg
*r
= &rsa
->regs
[i
];
8727 long reg_size
= register_size (gdbarch
, i
);
8731 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
8732 /* This shouldn't happen - we adjusted in_g_packet above. */
8733 internal_error (_("unexpected end of 'g' packet reply"));
8734 else if (rs
->buf
[r
->offset
* 2] == 'x')
8736 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
8737 /* The register isn't available, mark it as such (at
8738 the same time setting the value to zero). */
8739 regcache
->raw_supply (r
->regnum
, NULL
);
8742 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
8748 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
8751 process_g_packet (regcache
);
8754 /* Make the remote selected traceframe match GDB's selected
8758 remote_target::set_remote_traceframe ()
8761 struct remote_state
*rs
= get_remote_state ();
8763 if (rs
->remote_traceframe_number
== get_traceframe_number ())
8766 /* Avoid recursion, remote_trace_find calls us again. */
8767 rs
->remote_traceframe_number
= get_traceframe_number ();
8769 newnum
= target_trace_find (tfind_number
,
8770 get_traceframe_number (), 0, 0, NULL
);
8772 /* Should not happen. If it does, all bets are off. */
8773 if (newnum
!= get_traceframe_number ())
8774 warning (_("could not set remote traceframe"));
8778 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
8780 struct gdbarch
*gdbarch
= regcache
->arch ();
8781 struct remote_state
*rs
= get_remote_state ();
8782 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8785 set_remote_traceframe ();
8786 set_general_thread (regcache
->ptid ());
8790 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8792 gdb_assert (reg
!= NULL
);
8794 /* If this register might be in the 'g' packet, try that first -
8795 we are likely to read more than one register. If this is the
8796 first 'g' packet, we might be overly optimistic about its
8797 contents, so fall back to 'p'. */
8798 if (reg
->in_g_packet
)
8800 fetch_registers_using_g (regcache
);
8801 if (reg
->in_g_packet
)
8805 if (fetch_register_using_p (regcache
, reg
))
8808 /* This register is not available. */
8809 regcache
->raw_supply (reg
->regnum
, NULL
);
8814 fetch_registers_using_g (regcache
);
8816 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8817 if (!rsa
->regs
[i
].in_g_packet
)
8818 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
8820 /* This register is not available. */
8821 regcache
->raw_supply (i
, NULL
);
8825 /* Prepare to store registers. Since we may send them all (using a
8826 'G' request), we have to read out the ones we don't want to change
8830 remote_target::prepare_to_store (struct regcache
*regcache
)
8832 struct remote_state
*rs
= get_remote_state ();
8833 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8836 /* Make sure the entire registers array is valid. */
8837 switch (m_features
.packet_support (PACKET_P
))
8839 case PACKET_DISABLE
:
8840 case PACKET_SUPPORT_UNKNOWN
:
8841 /* Make sure all the necessary registers are cached. */
8842 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8843 if (rsa
->regs
[i
].in_g_packet
)
8844 regcache
->raw_update (rsa
->regs
[i
].regnum
);
8851 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
8852 packet was not recognized. */
8855 remote_target::store_register_using_P (const struct regcache
*regcache
,
8858 struct gdbarch
*gdbarch
= regcache
->arch ();
8859 struct remote_state
*rs
= get_remote_state ();
8860 /* Try storing a single register. */
8861 char *buf
= rs
->buf
.data ();
8862 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8865 if (m_features
.packet_support (PACKET_P
) == PACKET_DISABLE
)
8868 if (reg
->pnum
== -1)
8871 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
8872 p
= buf
+ strlen (buf
);
8873 regcache
->raw_collect (reg
->regnum
, regp
);
8874 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
8876 getpkt (&rs
->buf
, 0);
8878 switch (m_features
.packet_ok (rs
->buf
, PACKET_P
))
8883 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8884 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
.data ());
8885 case PACKET_UNKNOWN
:
8888 internal_error (_("Bad result from packet_ok"));
8892 /* Store register REGNUM, or all registers if REGNUM == -1, from the
8893 contents of the register cache buffer. FIXME: ignores errors. */
8896 remote_target::store_registers_using_G (const struct regcache
*regcache
)
8898 struct remote_state
*rs
= get_remote_state ();
8899 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8903 /* Extract all the registers in the regcache copying them into a
8908 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
8909 memset (regs
, 0, rsa
->sizeof_g_packet
);
8910 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8912 struct packet_reg
*r
= &rsa
->regs
[i
];
8915 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
8919 /* Command describes registers byte by byte,
8920 each byte encoded as two hex characters. */
8921 p
= rs
->buf
.data ();
8923 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
8925 getpkt (&rs
->buf
, 0);
8926 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8927 error (_("Could not write registers; remote failure reply '%s'"),
8931 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8932 of the register cache buffer. FIXME: ignores errors. */
8935 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
8937 struct gdbarch
*gdbarch
= regcache
->arch ();
8938 struct remote_state
*rs
= get_remote_state ();
8939 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8942 set_remote_traceframe ();
8943 set_general_thread (regcache
->ptid ());
8947 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8949 gdb_assert (reg
!= NULL
);
8951 /* Always prefer to store registers using the 'P' packet if
8952 possible; we often change only a small number of registers.
8953 Sometimes we change a larger number; we'd need help from a
8954 higher layer to know to use 'G'. */
8955 if (store_register_using_P (regcache
, reg
))
8958 /* For now, don't complain if we have no way to write the
8959 register. GDB loses track of unavailable registers too
8960 easily. Some day, this may be an error. We don't have
8961 any way to read the register, either... */
8962 if (!reg
->in_g_packet
)
8965 store_registers_using_G (regcache
);
8969 store_registers_using_G (regcache
);
8971 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8972 if (!rsa
->regs
[i
].in_g_packet
)
8973 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
8974 /* See above for why we do not issue an error here. */
8979 /* Return the number of hex digits in num. */
8982 hexnumlen (ULONGEST num
)
8986 for (i
= 0; num
!= 0; i
++)
8989 return std::max (i
, 1);
8992 /* Set BUF to the minimum number of hex digits representing NUM. */
8995 hexnumstr (char *buf
, ULONGEST num
)
8997 int len
= hexnumlen (num
);
8999 return hexnumnstr (buf
, num
, len
);
9003 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
9006 hexnumnstr (char *buf
, ULONGEST num
, int width
)
9012 for (i
= width
- 1; i
>= 0; i
--)
9014 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
9021 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
9024 remote_address_masked (CORE_ADDR addr
)
9026 unsigned int address_size
= remote_address_size
;
9028 /* If "remoteaddresssize" was not set, default to target address size. */
9030 address_size
= gdbarch_addr_bit (target_gdbarch ());
9032 if (address_size
> 0
9033 && address_size
< (sizeof (ULONGEST
) * 8))
9035 /* Only create a mask when that mask can safely be constructed
9036 in a ULONGEST variable. */
9039 mask
= (mask
<< address_size
) - 1;
9045 /* Determine whether the remote target supports binary downloading.
9046 This is accomplished by sending a no-op memory write of zero length
9047 to the target at the specified address. It does not suffice to send
9048 the whole packet, since many stubs strip the eighth bit and
9049 subsequently compute a wrong checksum, which causes real havoc with
9052 NOTE: This can still lose if the serial line is not eight-bit
9053 clean. In cases like this, the user should clear "remote
9057 remote_target::check_binary_download (CORE_ADDR addr
)
9059 struct remote_state
*rs
= get_remote_state ();
9061 switch (m_features
.packet_support (PACKET_X
))
9063 case PACKET_DISABLE
:
9067 case PACKET_SUPPORT_UNKNOWN
:
9071 p
= rs
->buf
.data ();
9073 p
+= hexnumstr (p
, (ULONGEST
) addr
);
9075 p
+= hexnumstr (p
, (ULONGEST
) 0);
9079 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9080 getpkt (&rs
->buf
, 0);
9082 if (rs
->buf
[0] == '\0')
9084 remote_debug_printf ("binary downloading NOT supported by target");
9085 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
9089 remote_debug_printf ("binary downloading supported by target");
9090 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
9097 /* Helper function to resize the payload in order to try to get a good
9098 alignment. We try to write an amount of data such that the next write will
9099 start on an address aligned on REMOTE_ALIGN_WRITES. */
9102 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
9104 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
9107 /* Write memory data directly to the remote machine.
9108 This does not inform the data cache; the data cache uses this.
9109 HEADER is the starting part of the packet.
9110 MEMADDR is the address in the remote memory space.
9111 MYADDR is the address of the buffer in our space.
9112 LEN_UNITS is the number of addressable units to write.
9113 UNIT_SIZE is the length in bytes of an addressable unit.
9114 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
9115 should send data as binary ('X'), or hex-encoded ('M').
9117 The function creates packet of the form
9118 <HEADER><ADDRESS>,<LENGTH>:<DATA>
9120 where encoding of <DATA> is terminated by PACKET_FORMAT.
9122 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
9125 Return the transferred status, error or OK (an
9126 'enum target_xfer_status' value). Save the number of addressable units
9127 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
9129 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
9130 exchange between gdb and the stub could look like (?? in place of the
9136 -> $M1000,3:eeeeffffeeee#??
9140 <- eeeeffffeeeedddd */
9143 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
9144 const gdb_byte
*myaddr
,
9147 ULONGEST
*xfered_len_units
,
9148 char packet_format
, int use_length
)
9150 struct remote_state
*rs
= get_remote_state ();
9156 int payload_capacity_bytes
;
9157 int payload_length_bytes
;
9159 if (packet_format
!= 'X' && packet_format
!= 'M')
9160 internal_error (_("remote_write_bytes_aux: bad packet format"));
9163 return TARGET_XFER_EOF
;
9165 payload_capacity_bytes
= get_memory_write_packet_size ();
9167 /* The packet buffer will be large enough for the payload;
9168 get_memory_packet_size ensures this. */
9171 /* Compute the size of the actual payload by subtracting out the
9172 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9174 payload_capacity_bytes
-= strlen ("$,:#NN");
9176 /* The comma won't be used. */
9177 payload_capacity_bytes
+= 1;
9178 payload_capacity_bytes
-= strlen (header
);
9179 payload_capacity_bytes
-= hexnumlen (memaddr
);
9181 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9183 strcat (rs
->buf
.data (), header
);
9184 p
= rs
->buf
.data () + strlen (header
);
9186 /* Compute a best guess of the number of bytes actually transfered. */
9187 if (packet_format
== 'X')
9189 /* Best guess at number of bytes that will fit. */
9190 todo_units
= std::min (len_units
,
9191 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9193 payload_capacity_bytes
-= hexnumlen (todo_units
);
9194 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9198 /* Number of bytes that will fit. */
9200 = std::min (len_units
,
9201 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9203 payload_capacity_bytes
-= hexnumlen (todo_units
);
9204 todo_units
= std::min (todo_units
,
9205 (payload_capacity_bytes
/ unit_size
) / 2);
9208 if (todo_units
<= 0)
9209 internal_error (_("minimum packet size too small to write data"));
9211 /* If we already need another packet, then try to align the end
9212 of this packet to a useful boundary. */
9213 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9214 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9216 /* Append "<memaddr>". */
9217 memaddr
= remote_address_masked (memaddr
);
9218 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9225 /* Append the length and retain its location and size. It may need to be
9226 adjusted once the packet body has been created. */
9228 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9236 /* Append the packet body. */
9237 if (packet_format
== 'X')
9239 /* Binary mode. Send target system values byte by byte, in
9240 increasing byte addresses. Only escape certain critical
9242 payload_length_bytes
=
9243 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9244 &units_written
, payload_capacity_bytes
);
9246 /* If not all TODO units fit, then we'll need another packet. Make
9247 a second try to keep the end of the packet aligned. Don't do
9248 this if the packet is tiny. */
9249 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9253 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9255 if (new_todo_units
!= units_written
)
9256 payload_length_bytes
=
9257 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9258 (gdb_byte
*) p
, &units_written
,
9259 payload_capacity_bytes
);
9262 p
+= payload_length_bytes
;
9263 if (use_length
&& units_written
< todo_units
)
9265 /* Escape chars have filled up the buffer prematurely,
9266 and we have actually sent fewer units than planned.
9267 Fix-up the length field of the packet. Use the same
9268 number of characters as before. */
9269 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9271 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9276 /* Normal mode: Send target system values byte by byte, in
9277 increasing byte addresses. Each byte is encoded as a two hex
9279 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9280 units_written
= todo_units
;
9283 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9284 getpkt (&rs
->buf
, 0);
9286 if (rs
->buf
[0] == 'E')
9287 return TARGET_XFER_E_IO
;
9289 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9290 send fewer units than we'd planned. */
9291 *xfered_len_units
= (ULONGEST
) units_written
;
9292 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9295 /* Write memory data directly to the remote machine.
9296 This does not inform the data cache; the data cache uses this.
9297 MEMADDR is the address in the remote memory space.
9298 MYADDR is the address of the buffer in our space.
9299 LEN is the number of bytes.
9301 Return the transferred status, error or OK (an
9302 'enum target_xfer_status' value). Save the number of bytes
9303 transferred in *XFERED_LEN. Only transfer a single packet. */
9306 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9307 ULONGEST len
, int unit_size
,
9308 ULONGEST
*xfered_len
)
9310 const char *packet_format
= NULL
;
9312 /* Check whether the target supports binary download. */
9313 check_binary_download (memaddr
);
9315 switch (m_features
.packet_support (PACKET_X
))
9318 packet_format
= "X";
9320 case PACKET_DISABLE
:
9321 packet_format
= "M";
9323 case PACKET_SUPPORT_UNKNOWN
:
9324 internal_error (_("remote_write_bytes: bad internal state"));
9326 internal_error (_("bad switch"));
9329 return remote_write_bytes_aux (packet_format
,
9330 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9331 packet_format
[0], 1);
9334 /* Read memory data directly from the remote machine.
9335 This does not use the data cache; the data cache uses this.
9336 MEMADDR is the address in the remote memory space.
9337 MYADDR is the address of the buffer in our space.
9338 LEN_UNITS is the number of addressable memory units to read..
9339 UNIT_SIZE is the length in bytes of an addressable unit.
9341 Return the transferred status, error or OK (an
9342 'enum target_xfer_status' value). Save the number of bytes
9343 transferred in *XFERED_LEN_UNITS.
9345 See the comment of remote_write_bytes_aux for an example of
9346 memory read/write exchange between gdb and the stub. */
9349 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9351 int unit_size
, ULONGEST
*xfered_len_units
)
9353 struct remote_state
*rs
= get_remote_state ();
9354 int buf_size_bytes
; /* Max size of packet output buffer. */
9359 buf_size_bytes
= get_memory_read_packet_size ();
9360 /* The packet buffer will be large enough for the payload;
9361 get_memory_packet_size ensures this. */
9363 /* Number of units that will fit. */
9364 todo_units
= std::min (len_units
,
9365 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9367 /* Construct "m"<memaddr>","<len>". */
9368 memaddr
= remote_address_masked (memaddr
);
9369 p
= rs
->buf
.data ();
9371 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9373 p
+= hexnumstr (p
, (ULONGEST
) todo_units
);
9376 getpkt (&rs
->buf
, 0);
9377 if (rs
->buf
[0] == 'E'
9378 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
9379 && rs
->buf
[3] == '\0')
9380 return TARGET_XFER_E_IO
;
9381 /* Reply describes memory byte by byte, each byte encoded as two hex
9383 p
= rs
->buf
.data ();
9384 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9385 /* Return what we have. Let higher layers handle partial reads. */
9386 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9387 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9390 /* Using the set of read-only target sections of remote, read live
9393 For interface/parameters/return description see target.h,
9397 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9401 ULONGEST
*xfered_len
)
9403 const struct target_section
*secp
;
9405 secp
= target_section_by_addr (this, memaddr
);
9407 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9409 ULONGEST memend
= memaddr
+ len
;
9411 const target_section_table
*table
= target_get_section_table (this);
9412 for (const target_section
&p
: *table
)
9414 if (memaddr
>= p
.addr
)
9416 if (memend
<= p
.endaddr
)
9418 /* Entire transfer is within this section. */
9419 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9422 else if (memaddr
>= p
.endaddr
)
9424 /* This section ends before the transfer starts. */
9429 /* This section overlaps the transfer. Just do half. */
9430 len
= p
.endaddr
- memaddr
;
9431 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9438 return TARGET_XFER_EOF
;
9441 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9442 first if the requested memory is unavailable in traceframe.
9443 Otherwise, fall back to remote_read_bytes_1. */
9446 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9447 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9448 ULONGEST
*xfered_len
)
9451 return TARGET_XFER_EOF
;
9453 if (get_traceframe_number () != -1)
9455 std::vector
<mem_range
> available
;
9457 /* If we fail to get the set of available memory, then the
9458 target does not support querying traceframe info, and so we
9459 attempt reading from the traceframe anyway (assuming the
9460 target implements the old QTro packet then). */
9461 if (traceframe_available_memory (&available
, memaddr
, len
))
9463 if (available
.empty () || available
[0].start
!= memaddr
)
9465 enum target_xfer_status res
;
9467 /* Don't read into the traceframe's available
9469 if (!available
.empty ())
9471 LONGEST oldlen
= len
;
9473 len
= available
[0].start
- memaddr
;
9474 gdb_assert (len
<= oldlen
);
9477 /* This goes through the topmost target again. */
9478 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9479 len
, unit_size
, xfered_len
);
9480 if (res
== TARGET_XFER_OK
)
9481 return TARGET_XFER_OK
;
9484 /* No use trying further, we know some memory starting
9485 at MEMADDR isn't available. */
9487 return (*xfered_len
!= 0) ?
9488 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9492 /* Don't try to read more than how much is available, in
9493 case the target implements the deprecated QTro packet to
9494 cater for older GDBs (the target's knowledge of read-only
9495 sections may be outdated by now). */
9496 len
= available
[0].length
;
9500 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9505 /* Sends a packet with content determined by the printf format string
9506 FORMAT and the remaining arguments, then gets the reply. Returns
9507 whether the packet was a success, a failure, or unknown. */
9510 remote_target::remote_send_printf (const char *format
, ...)
9512 struct remote_state
*rs
= get_remote_state ();
9513 int max_size
= get_remote_packet_size ();
9516 va_start (ap
, format
);
9519 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9523 if (size
>= max_size
)
9524 internal_error (_("Too long remote packet."));
9526 if (putpkt (rs
->buf
) < 0)
9527 error (_("Communication problem with target."));
9530 getpkt (&rs
->buf
, 0);
9532 return packet_check_result (rs
->buf
);
9535 /* Flash writing can take quite some time. We'll set
9536 effectively infinite timeout for flash operations.
9537 In future, we'll need to decide on a better approach. */
9538 static const int remote_flash_timeout
= 1000;
9541 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9543 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
9544 enum packet_result ret
;
9545 scoped_restore restore_timeout
9546 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9548 ret
= remote_send_printf ("vFlashErase:%s,%s",
9549 phex (address
, addr_size
),
9553 case PACKET_UNKNOWN
:
9554 error (_("Remote target does not support flash erase"));
9556 error (_("Error erasing flash with vFlashErase packet"));
9563 remote_target::remote_flash_write (ULONGEST address
,
9564 ULONGEST length
, ULONGEST
*xfered_len
,
9565 const gdb_byte
*data
)
9567 scoped_restore restore_timeout
9568 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9569 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9574 remote_target::flash_done ()
9578 scoped_restore restore_timeout
9579 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9581 ret
= remote_send_printf ("vFlashDone");
9585 case PACKET_UNKNOWN
:
9586 error (_("Remote target does not support vFlashDone"));
9588 error (_("Error finishing flash operation"));
9595 /* Stuff for dealing with the packets which are part of this protocol.
9596 See comment at top of file for details. */
9598 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
9599 error to higher layers. Called when a serial error is detected.
9600 The exception message is STRING, followed by a colon and a blank,
9601 the system error message for errno at function entry and final dot
9602 for output compatibility with throw_perror_with_name. */
9605 unpush_and_perror (remote_target
*target
, const char *string
)
9607 int saved_errno
= errno
;
9609 remote_unpush_target (target
);
9610 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
9611 safe_strerror (saved_errno
));
9614 /* Read a single character from the remote end. The current quit
9615 handler is overridden to avoid quitting in the middle of packet
9616 sequence, as that would break communication with the remote server.
9617 See remote_serial_quit_handler for more detail. */
9620 remote_target::readchar (int timeout
)
9623 struct remote_state
*rs
= get_remote_state ();
9626 scoped_restore restore_quit_target
9627 = make_scoped_restore (&curr_quit_handler_target
, this);
9628 scoped_restore restore_quit
9629 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9631 rs
->got_ctrlc_during_io
= 0;
9633 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9635 if (rs
->got_ctrlc_during_io
)
9642 switch ((enum serial_rc
) ch
)
9645 remote_unpush_target (this);
9646 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
9649 unpush_and_perror (this, _("Remote communication error. "
9650 "Target disconnected"));
9652 case SERIAL_TIMEOUT
:
9658 /* Wrapper for serial_write that closes the target and throws if
9659 writing fails. The current quit handler is overridden to avoid
9660 quitting in the middle of packet sequence, as that would break
9661 communication with the remote server. See
9662 remote_serial_quit_handler for more detail. */
9665 remote_target::remote_serial_write (const char *str
, int len
)
9667 struct remote_state
*rs
= get_remote_state ();
9669 scoped_restore restore_quit_target
9670 = make_scoped_restore (&curr_quit_handler_target
, this);
9671 scoped_restore restore_quit
9672 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9674 rs
->got_ctrlc_during_io
= 0;
9676 if (serial_write (rs
->remote_desc
, str
, len
))
9678 unpush_and_perror (this, _("Remote communication error. "
9679 "Target disconnected"));
9682 if (rs
->got_ctrlc_during_io
)
9686 /* Return a string representing an escaped version of BUF, of len N.
9687 E.g. \n is converted to \\n, \t to \\t, etc. */
9690 escape_buffer (const char *buf
, int n
)
9694 stb
.putstrn (buf
, n
, '\\');
9695 return stb
.release ();
9699 remote_target::putpkt (const char *buf
)
9701 return putpkt_binary (buf
, strlen (buf
));
9704 /* Wrapper around remote_target::putpkt to avoid exporting
9708 putpkt (remote_target
*remote
, const char *buf
)
9710 return remote
->putpkt (buf
);
9713 /* Send a packet to the remote machine, with error checking. The data
9714 of the packet is in BUF. The string in BUF can be at most
9715 get_remote_packet_size () - 5 to account for the $, # and checksum,
9716 and for a possible /0 if we are debugging (remote_debug) and want
9717 to print the sent packet as a string. */
9720 remote_target::putpkt_binary (const char *buf
, int cnt
)
9722 struct remote_state
*rs
= get_remote_state ();
9724 unsigned char csum
= 0;
9725 gdb::def_vector
<char> data (cnt
+ 6);
9726 char *buf2
= data
.data ();
9732 /* Catch cases like trying to read memory or listing threads while
9733 we're waiting for a stop reply. The remote server wouldn't be
9734 ready to handle this request, so we'd hang and timeout. We don't
9735 have to worry about this in synchronous mode, because in that
9736 case it's not possible to issue a command while the target is
9737 running. This is not a problem in non-stop mode, because in that
9738 case, the stub is always ready to process serial input. */
9739 if (!target_is_non_stop_p ()
9740 && target_is_async_p ()
9741 && rs
->waiting_for_stop_reply
)
9743 error (_("Cannot execute this command while the target is running.\n"
9744 "Use the \"interrupt\" command to stop the target\n"
9745 "and then try again."));
9748 /* Copy the packet into buffer BUF2, encapsulating it
9749 and giving it a checksum. */
9754 for (i
= 0; i
< cnt
; i
++)
9760 *p
++ = tohex ((csum
>> 4) & 0xf);
9761 *p
++ = tohex (csum
& 0xf);
9763 /* Send it over and over until we get a positive ack. */
9771 int len
= (int) (p
- buf2
);
9774 if (remote_packet_max_chars
< 0)
9777 max_chars
= remote_packet_max_chars
;
9780 = escape_buffer (buf2
, std::min (len
, max_chars
));
9782 if (len
> max_chars
)
9783 remote_debug_printf_nofunc
9784 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
9787 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
9789 remote_serial_write (buf2
, p
- buf2
);
9791 /* If this is a no acks version of the remote protocol, send the
9792 packet and move on. */
9796 /* Read until either a timeout occurs (-2) or '+' is read.
9797 Handle any notification that arrives in the mean time. */
9800 ch
= readchar (remote_timeout
);
9805 remote_debug_printf_nofunc ("Received Ack");
9808 remote_debug_printf_nofunc ("Received Nak");
9810 case SERIAL_TIMEOUT
:
9814 break; /* Retransmit buffer. */
9817 remote_debug_printf ("Packet instead of Ack, ignoring it");
9818 /* It's probably an old response sent because an ACK
9819 was lost. Gobble up the packet and ack it so it
9820 doesn't get retransmitted when we resend this
9823 remote_serial_write ("+", 1);
9824 continue; /* Now, go look for +. */
9831 /* If we got a notification, handle it, and go back to looking
9833 /* We've found the start of a notification. Now
9834 collect the data. */
9835 val
= read_frame (&rs
->buf
);
9838 remote_debug_printf_nofunc
9839 (" Notification received: %s",
9840 escape_buffer (rs
->buf
.data (), val
).c_str ());
9842 handle_notification (rs
->notif_state
, rs
->buf
.data ());
9843 /* We're in sync now, rewait for the ack. */
9847 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9853 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9857 break; /* Here to retransmit. */
9861 /* This is wrong. If doing a long backtrace, the user should be
9862 able to get out next time we call QUIT, without anything as
9863 violent as interrupt_query. If we want to provide a way out of
9864 here without getting to the next QUIT, it should be based on
9865 hitting ^C twice as in remote_wait. */
9877 /* Come here after finding the start of a frame when we expected an
9878 ack. Do our best to discard the rest of this packet. */
9881 remote_target::skip_frame ()
9887 c
= readchar (remote_timeout
);
9890 case SERIAL_TIMEOUT
:
9891 /* Nothing we can do. */
9894 /* Discard the two bytes of checksum and stop. */
9895 c
= readchar (remote_timeout
);
9897 c
= readchar (remote_timeout
);
9900 case '*': /* Run length encoding. */
9901 /* Discard the repeat count. */
9902 c
= readchar (remote_timeout
);
9907 /* A regular character. */
9913 /* Come here after finding the start of the frame. Collect the rest
9914 into *BUF, verifying the checksum, length, and handling run-length
9915 compression. NUL terminate the buffer. If there is not enough room,
9918 Returns -1 on error, number of characters in buffer (ignoring the
9919 trailing NULL) on success. (could be extended to return one of the
9920 SERIAL status indications). */
9923 remote_target::read_frame (gdb::char_vector
*buf_p
)
9928 char *buf
= buf_p
->data ();
9929 struct remote_state
*rs
= get_remote_state ();
9936 c
= readchar (remote_timeout
);
9939 case SERIAL_TIMEOUT
:
9940 remote_debug_printf ("Timeout in mid-packet, retrying");
9944 remote_debug_printf ("Saw new packet start in middle of old one");
9945 return -1; /* Start a new packet, count retries. */
9949 unsigned char pktcsum
;
9955 check_0
= readchar (remote_timeout
);
9957 check_1
= readchar (remote_timeout
);
9959 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
9961 remote_debug_printf ("Timeout in checksum, retrying");
9964 else if (check_0
< 0 || check_1
< 0)
9966 remote_debug_printf ("Communication error in checksum");
9970 /* Don't recompute the checksum; with no ack packets we
9971 don't have any way to indicate a packet retransmission
9976 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
9977 if (csum
== pktcsum
)
9981 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
9982 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
9984 /* Number of characters in buffer ignoring trailing
9988 case '*': /* Run length encoding. */
9993 c
= readchar (remote_timeout
);
9995 repeat
= c
- ' ' + 3; /* Compute repeat count. */
9997 /* The character before ``*'' is repeated. */
9999 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
10001 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
10003 /* Make some more room in the buffer. */
10004 buf_p
->resize (buf_p
->size () + repeat
);
10005 buf
= buf_p
->data ();
10008 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
10014 gdb_printf (_("Invalid run length encoding: %s\n"), buf
);
10018 if (bc
>= buf_p
->size () - 1)
10020 /* Make some more room in the buffer. */
10021 buf_p
->resize (buf_p
->size () * 2);
10022 buf
= buf_p
->data ();
10032 /* Set this to the maximum number of seconds to wait instead of waiting forever
10033 in target_wait(). If this timer times out, then it generates an error and
10034 the command is aborted. This replaces most of the need for timeouts in the
10035 GDB test suite, and makes it possible to distinguish between a hung target
10036 and one with slow communications. */
10038 static int watchdog
= 0;
10040 show_watchdog (struct ui_file
*file
, int from_tty
,
10041 struct cmd_list_element
*c
, const char *value
)
10043 gdb_printf (file
, _("Watchdog timer is %s.\n"), value
);
10046 /* Read a packet from the remote machine, with error checking, and
10047 store it in *BUF. Resize *BUF if necessary to hold the result. If
10048 FOREVER, wait forever rather than timing out; this is used (in
10049 synchronous mode) to wait for a target that is is executing user
10050 code to stop. If FOREVER == 0, this function is allowed to time
10051 out gracefully and return an indication of this to the caller.
10052 Otherwise return the number of bytes read. If EXPECTING_NOTIF,
10053 consider receiving a notification enough reason to return to the
10054 caller. *IS_NOTIF is an output boolean that indicates whether *BUF
10055 holds a notification or not (a regular packet). */
10058 remote_target::getpkt_or_notif_sane_1 (gdb::char_vector
*buf
,
10059 int forever
, int expecting_notif
,
10062 struct remote_state
*rs
= get_remote_state ();
10068 strcpy (buf
->data (), "timeout");
10071 timeout
= watchdog
> 0 ? watchdog
: -1;
10072 else if (expecting_notif
)
10073 timeout
= 0; /* There should already be a char in the buffer. If
10076 timeout
= remote_timeout
;
10078 #define MAX_TRIES 3
10080 /* Process any number of notifications, and then return when
10081 we get a packet. */
10084 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
10086 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
10088 /* This can loop forever if the remote side sends us
10089 characters continuously, but if it pauses, we'll get
10090 SERIAL_TIMEOUT from readchar because of timeout. Then
10091 we'll count that as a retry.
10093 Note that even when forever is set, we will only wait
10094 forever prior to the start of a packet. After that, we
10095 expect characters to arrive at a brisk pace. They should
10096 show up within remote_timeout intervals. */
10098 c
= readchar (timeout
);
10099 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
10101 if (c
== SERIAL_TIMEOUT
)
10103 if (expecting_notif
)
10104 return -1; /* Don't complain, it's normal to not get
10105 anything in this case. */
10107 if (forever
) /* Watchdog went off? Kill the target. */
10109 remote_unpush_target (this);
10110 throw_error (TARGET_CLOSE_ERROR
,
10111 _("Watchdog timeout has expired. "
10112 "Target detached."));
10115 remote_debug_printf ("Timed out.");
10119 /* We've found the start of a packet or notification.
10120 Now collect the data. */
10121 val
= read_frame (buf
);
10126 remote_serial_write ("-", 1);
10129 if (tries
> MAX_TRIES
)
10131 /* We have tried hard enough, and just can't receive the
10132 packet/notification. Give up. */
10133 gdb_printf (_("Ignoring packet error, continuing...\n"));
10135 /* Skip the ack char if we're in no-ack mode. */
10136 if (!rs
->noack_mode
)
10137 remote_serial_write ("+", 1);
10141 /* If we got an ordinary packet, return that to our caller. */
10148 if (remote_packet_max_chars
< 0)
10151 max_chars
= remote_packet_max_chars
;
10154 = escape_buffer (buf
->data (),
10155 std::min (val
, max_chars
));
10157 if (val
> max_chars
)
10158 remote_debug_printf_nofunc
10159 ("Packet received: %s [%d bytes omitted]", str
.c_str (),
10162 remote_debug_printf_nofunc ("Packet received: %s",
10166 /* Skip the ack char if we're in no-ack mode. */
10167 if (!rs
->noack_mode
)
10168 remote_serial_write ("+", 1);
10169 if (is_notif
!= NULL
)
10174 /* If we got a notification, handle it, and go back to looking
10178 gdb_assert (c
== '%');
10180 remote_debug_printf_nofunc
10181 (" Notification received: %s",
10182 escape_buffer (buf
->data (), val
).c_str ());
10184 if (is_notif
!= NULL
)
10187 handle_notification (rs
->notif_state
, buf
->data ());
10189 /* Notifications require no acknowledgement. */
10191 if (expecting_notif
)
10197 /* Read a packet from the remote machine, with error checking, and
10198 store it in *BUF. Resize *BUF if necessary to hold the result. If
10199 FOREVER, wait forever rather than timing out; this is used (in
10200 synchronous mode) to wait for a target that is is executing user
10204 remote_target::getpkt (gdb::char_vector
*buf
, int forever
)
10206 return getpkt_or_notif_sane_1 (buf
, forever
, 0, NULL
);
10210 remote_target::getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
10213 return getpkt_or_notif_sane_1 (buf
, forever
, 1, is_notif
);
10216 /* Kill any new fork children of inferior INF that haven't been
10217 processed by follow_fork. */
10220 remote_target::kill_new_fork_children (inferior
*inf
)
10222 remote_state
*rs
= get_remote_state ();
10223 const notif_client
*notif
= ¬if_client_stop
;
10225 /* Kill the fork child threads of any threads in inferior INF that are stopped
10226 at a fork event. */
10227 for (thread_info
*thread
: inf
->non_exited_threads ())
10229 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10234 int child_pid
= ws
->child_ptid ().pid ();
10235 int res
= remote_vkill (child_pid
);
10238 error (_("Can't kill fork child process %d"), child_pid
);
10241 /* Check for any pending fork events (not reported or processed yet)
10242 in inferior INF and kill those fork child threads as well. */
10243 remote_notif_get_pending_events (notif
);
10244 for (auto &event
: rs
->stop_reply_queue
)
10246 if (event
->ptid
.pid () != inf
->pid
)
10249 if (!is_fork_status (event
->ws
.kind ()))
10252 int child_pid
= event
->ws
.child_ptid ().pid ();
10253 int res
= remote_vkill (child_pid
);
10256 error (_("Can't kill fork child process %d"), child_pid
);
10261 /* Target hook to kill the current inferior. */
10264 remote_target::kill ()
10267 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10269 gdb_assert (inf
!= nullptr);
10271 if (m_features
.packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10273 /* If we're stopped while forking and we haven't followed yet,
10274 kill the child task. We need to do this before killing the
10275 parent task because if this is a vfork then the parent will
10277 kill_new_fork_children (inf
);
10279 res
= remote_vkill (inf
->pid
);
10282 target_mourn_inferior (inferior_ptid
);
10287 /* If we are in 'target remote' mode and we are killing the only
10288 inferior, then we will tell gdbserver to exit and unpush the
10290 if (res
== -1 && !m_features
.remote_multi_process_p ()
10291 && number_of_live_inferiors (this) == 1)
10295 /* We've killed the remote end, we get to mourn it. If we are
10296 not in extended mode, mourning the inferior also unpushes
10297 remote_ops from the target stack, which closes the remote
10299 target_mourn_inferior (inferior_ptid
);
10304 error (_("Can't kill process"));
10307 /* Send a kill request to the target using the 'vKill' packet. */
10310 remote_target::remote_vkill (int pid
)
10312 if (m_features
.packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10315 remote_state
*rs
= get_remote_state ();
10317 /* Tell the remote target to detach. */
10318 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10320 getpkt (&rs
->buf
, 0);
10322 switch (m_features
.packet_ok (rs
->buf
, PACKET_vKill
))
10328 case PACKET_UNKNOWN
:
10331 internal_error (_("Bad result from packet_ok"));
10335 /* Send a kill request to the target using the 'k' packet. */
10338 remote_target::remote_kill_k ()
10340 /* Catch errors so the user can quit from gdb even when we
10341 aren't on speaking terms with the remote system. */
10346 catch (const gdb_exception_error
&ex
)
10348 if (ex
.error
== TARGET_CLOSE_ERROR
)
10350 /* If we got an (EOF) error that caused the target
10351 to go away, then we're done, that's what we wanted.
10352 "k" is susceptible to cause a premature EOF, given
10353 that the remote server isn't actually required to
10354 reply to "k", and it can happen that it doesn't
10355 even get to reply ACK to the "k". */
10359 /* Otherwise, something went wrong. We didn't actually kill
10360 the target. Just propagate the exception, and let the
10361 user or higher layers decide what to do. */
10367 remote_target::mourn_inferior ()
10369 struct remote_state
*rs
= get_remote_state ();
10371 /* We're no longer interested in notification events of an inferior
10372 that exited or was killed/detached. */
10373 discard_pending_stop_replies (current_inferior ());
10375 /* In 'target remote' mode with one inferior, we close the connection. */
10376 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10378 remote_unpush_target (this);
10382 /* In case we got here due to an error, but we're going to stay
10384 rs
->waiting_for_stop_reply
= 0;
10386 /* If the current general thread belonged to the process we just
10387 detached from or has exited, the remote side current general
10388 thread becomes undefined. Considering a case like this:
10390 - We just got here due to a detach.
10391 - The process that we're detaching from happens to immediately
10392 report a global breakpoint being hit in non-stop mode, in the
10393 same thread we had selected before.
10394 - GDB attaches to this process again.
10395 - This event happens to be the next event we handle.
10397 GDB would consider that the current general thread didn't need to
10398 be set on the stub side (with Hg), since for all it knew,
10399 GENERAL_THREAD hadn't changed.
10401 Notice that although in all-stop mode, the remote server always
10402 sets the current thread to the thread reporting the stop event,
10403 that doesn't happen in non-stop mode; in non-stop, the stub *must
10404 not* change the current thread when reporting a breakpoint hit,
10405 due to the decoupling of event reporting and event handling.
10407 To keep things simple, we always invalidate our notion of the
10409 record_currthread (rs
, minus_one_ptid
);
10411 /* Call common code to mark the inferior as not running. */
10412 generic_mourn_inferior ();
10416 extended_remote_target::supports_disable_randomization ()
10418 return (m_features
.packet_support (PACKET_QDisableRandomization
)
10423 remote_target::extended_remote_disable_randomization (int val
)
10425 struct remote_state
*rs
= get_remote_state ();
10428 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10429 "QDisableRandomization:%x", val
);
10431 reply
= remote_get_noisy_reply ();
10432 if (*reply
== '\0')
10433 error (_("Target does not support QDisableRandomization."));
10434 if (strcmp (reply
, "OK") != 0)
10435 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10439 remote_target::extended_remote_run (const std::string
&args
)
10441 struct remote_state
*rs
= get_remote_state ();
10443 const char *remote_exec_file
= get_remote_exec_file ();
10445 /* If the user has disabled vRun support, or we have detected that
10446 support is not available, do not try it. */
10447 if (m_features
.packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10450 strcpy (rs
->buf
.data (), "vRun;");
10451 len
= strlen (rs
->buf
.data ());
10453 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10454 error (_("Remote file name too long for run packet"));
10455 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10456 strlen (remote_exec_file
));
10458 if (!args
.empty ())
10462 gdb_argv
argv (args
.c_str ());
10463 for (i
= 0; argv
[i
] != NULL
; i
++)
10465 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10466 error (_("Argument list too long for run packet"));
10467 rs
->buf
[len
++] = ';';
10468 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10473 rs
->buf
[len
++] = '\0';
10476 getpkt (&rs
->buf
, 0);
10478 switch (m_features
.packet_ok (rs
->buf
, PACKET_vRun
))
10481 /* We have a wait response. All is well. */
10483 case PACKET_UNKNOWN
:
10486 if (remote_exec_file
[0] == '\0')
10487 error (_("Running the default executable on the remote target failed; "
10488 "try \"set remote exec-file\"?"));
10490 error (_("Running \"%s\" on the remote target failed"),
10493 gdb_assert_not_reached ("bad switch");
10497 /* Helper function to send set/unset environment packets. ACTION is
10498 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10499 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10503 remote_target::send_environment_packet (const char *action
,
10504 const char *packet
,
10507 remote_state
*rs
= get_remote_state ();
10509 /* Convert the environment variable to an hex string, which
10510 is the best format to be transmitted over the wire. */
10511 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10514 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10515 "%s:%s", packet
, encoded_value
.c_str ());
10518 getpkt (&rs
->buf
, 0);
10519 if (strcmp (rs
->buf
.data (), "OK") != 0)
10520 warning (_("Unable to %s environment variable '%s' on remote."),
10524 /* Helper function to handle the QEnvironment* packets. */
10527 remote_target::extended_remote_environment_support ()
10529 remote_state
*rs
= get_remote_state ();
10531 if (m_features
.packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10533 putpkt ("QEnvironmentReset");
10534 getpkt (&rs
->buf
, 0);
10535 if (strcmp (rs
->buf
.data (), "OK") != 0)
10536 warning (_("Unable to reset environment on remote."));
10539 gdb_environ
*e
= ¤t_inferior ()->environment
;
10541 if (m_features
.packet_support (PACKET_QEnvironmentHexEncoded
)
10544 for (const std::string
&el
: e
->user_set_env ())
10545 send_environment_packet ("set", "QEnvironmentHexEncoded",
10550 if (m_features
.packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10551 for (const std::string
&el
: e
->user_unset_env ())
10552 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10555 /* Helper function to set the current working directory for the
10556 inferior in the remote target. */
10559 remote_target::extended_remote_set_inferior_cwd ()
10561 if (m_features
.packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10563 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10564 remote_state
*rs
= get_remote_state ();
10566 if (!inferior_cwd
.empty ())
10568 std::string hexpath
10569 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10570 inferior_cwd
.size ());
10572 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10573 "QSetWorkingDir:%s", hexpath
.c_str ());
10577 /* An empty inferior_cwd means that the user wants us to
10578 reset the remote server's inferior's cwd. */
10579 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10580 "QSetWorkingDir:");
10584 getpkt (&rs
->buf
, 0);
10585 if (m_features
.packet_ok (rs
->buf
, PACKET_QSetWorkingDir
) != PACKET_OK
)
10587 Remote replied unexpectedly while setting the inferior's working\n\
10594 /* In the extended protocol we want to be able to do things like
10595 "run" and have them basically work as expected. So we need
10596 a special create_inferior function. We support changing the
10597 executable file and the command line arguments, but not the
10601 extended_remote_target::create_inferior (const char *exec_file
,
10602 const std::string
&args
,
10603 char **env
, int from_tty
)
10607 struct remote_state
*rs
= get_remote_state ();
10608 const char *remote_exec_file
= get_remote_exec_file ();
10610 /* If running asynchronously, register the target file descriptor
10611 with the event loop. */
10612 if (target_can_async_p ())
10613 target_async (true);
10615 /* Disable address space randomization if requested (and supported). */
10616 if (supports_disable_randomization ())
10617 extended_remote_disable_randomization (disable_randomization
);
10619 /* If startup-with-shell is on, we inform gdbserver to start the
10620 remote inferior using a shell. */
10621 if (m_features
.packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10623 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10624 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
10626 getpkt (&rs
->buf
, 0);
10627 if (strcmp (rs
->buf
.data (), "OK") != 0)
10629 Remote replied unexpectedly while setting startup-with-shell: %s"),
10633 extended_remote_environment_support ();
10635 extended_remote_set_inferior_cwd ();
10637 /* Now restart the remote server. */
10638 run_worked
= extended_remote_run (args
) != -1;
10641 /* vRun was not supported. Fail if we need it to do what the
10643 if (remote_exec_file
[0])
10644 error (_("Remote target does not support \"set remote exec-file\""));
10645 if (!args
.empty ())
10646 error (_("Remote target does not support \"set args\" or run ARGS"));
10648 /* Fall back to "R". */
10649 extended_remote_restart ();
10652 /* vRun's success return is a stop reply. */
10653 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
10654 add_current_inferior_and_thread (stop_reply
);
10656 /* Get updated offsets, if the stub uses qOffsets. */
10661 /* Given a location's target info BP_TGT and the packet buffer BUF, output
10662 the list of conditions (in agent expression bytecode format), if any, the
10663 target needs to evaluate. The output is placed into the packet buffer
10664 started from BUF and ended at BUF_END. */
10667 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
10668 struct bp_target_info
*bp_tgt
, char *buf
,
10671 if (bp_tgt
->conditions
.empty ())
10674 buf
+= strlen (buf
);
10675 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
10678 /* Send conditions to the target. */
10679 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
10681 xsnprintf (buf
, buf_end
- buf
, "X%x,", (int) aexpr
->buf
.size ());
10682 buf
+= strlen (buf
);
10683 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
10684 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10691 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
10692 struct bp_target_info
*bp_tgt
, char *buf
)
10694 if (bp_tgt
->tcommands
.empty ())
10697 buf
+= strlen (buf
);
10699 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
10700 buf
+= strlen (buf
);
10702 /* Concatenate all the agent expressions that are commands into the
10704 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
10706 sprintf (buf
, "X%x,", (int) aexpr
->buf
.size ());
10707 buf
+= strlen (buf
);
10708 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
10709 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10714 /* Insert a breakpoint. On targets that have software breakpoint
10715 support, we ask the remote target to do the work; on targets
10716 which don't, we insert a traditional memory breakpoint. */
10719 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
10720 struct bp_target_info
*bp_tgt
)
10722 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10723 If it succeeds, then set the support to PACKET_ENABLE. If it
10724 fails, and the user has explicitly requested the Z support then
10725 report an error, otherwise, mark it disabled and go on. */
10727 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10729 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10730 struct remote_state
*rs
;
10733 /* Make sure the remote is pointing at the right process, if
10735 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10736 set_general_process ();
10738 rs
= get_remote_state ();
10739 p
= rs
->buf
.data ();
10740 endbuf
= p
+ get_remote_packet_size ();
10745 addr
= (ULONGEST
) remote_address_masked (addr
);
10746 p
+= hexnumstr (p
, addr
);
10747 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10749 if (supports_evaluation_of_breakpoint_conditions ())
10750 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10752 if (can_run_breakpoint_commands ())
10753 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10756 getpkt (&rs
->buf
, 0);
10758 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z0
))
10764 case PACKET_UNKNOWN
:
10769 /* If this breakpoint has target-side commands but this stub doesn't
10770 support Z0 packets, throw error. */
10771 if (!bp_tgt
->tcommands
.empty ())
10772 throw_error (NOT_SUPPORTED_ERROR
, _("\
10773 Target doesn't support breakpoints that have target side commands."));
10775 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
10779 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
10780 struct bp_target_info
*bp_tgt
,
10781 enum remove_bp_reason reason
)
10783 CORE_ADDR addr
= bp_tgt
->placed_address
;
10784 struct remote_state
*rs
= get_remote_state ();
10786 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10788 char *p
= rs
->buf
.data ();
10789 char *endbuf
= p
+ get_remote_packet_size ();
10791 /* Make sure the remote is pointing at the right process, if
10793 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10794 set_general_process ();
10800 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
10801 p
+= hexnumstr (p
, addr
);
10802 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10805 getpkt (&rs
->buf
, 0);
10807 return (rs
->buf
[0] == 'E');
10810 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
10813 static enum Z_packet_type
10814 watchpoint_to_Z_packet (int type
)
10819 return Z_PACKET_WRITE_WP
;
10822 return Z_PACKET_READ_WP
;
10825 return Z_PACKET_ACCESS_WP
;
10828 internal_error (_("hw_bp_to_z: bad watchpoint type %d"), type
);
10833 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
10834 enum target_hw_bp_type type
, struct expression
*cond
)
10836 struct remote_state
*rs
= get_remote_state ();
10837 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10839 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10841 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
10842 + to_underlying (packet
))) == PACKET_DISABLE
)
10845 /* Make sure the remote is pointing at the right process, if
10847 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10848 set_general_process ();
10850 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
10851 p
= strchr (rs
->buf
.data (), '\0');
10852 addr
= remote_address_masked (addr
);
10853 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10854 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10857 getpkt (&rs
->buf
, 0);
10859 switch (m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
10860 + to_underlying (packet
))))
10864 case PACKET_UNKNOWN
:
10869 internal_error (_("remote_insert_watchpoint: reached end of function"));
10873 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
10874 CORE_ADDR start
, int length
)
10876 CORE_ADDR diff
= remote_address_masked (addr
- start
);
10878 return diff
< length
;
10883 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
10884 enum target_hw_bp_type type
, struct expression
*cond
)
10886 struct remote_state
*rs
= get_remote_state ();
10887 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10889 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10891 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
10892 + to_underlying (packet
))) == PACKET_DISABLE
)
10895 /* Make sure the remote is pointing at the right process, if
10897 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10898 set_general_process ();
10900 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
10901 p
= strchr (rs
->buf
.data (), '\0');
10902 addr
= remote_address_masked (addr
);
10903 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10904 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10906 getpkt (&rs
->buf
, 0);
10908 switch (m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
10909 + to_underlying (packet
))))
10912 case PACKET_UNKNOWN
:
10917 internal_error (_("remote_remove_watchpoint: reached end of function"));
10921 static int remote_hw_watchpoint_limit
= -1;
10922 static int remote_hw_watchpoint_length_limit
= -1;
10923 static int remote_hw_breakpoint_limit
= -1;
10926 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
10928 if (remote_hw_watchpoint_length_limit
== 0)
10930 else if (remote_hw_watchpoint_length_limit
< 0)
10932 else if (len
<= remote_hw_watchpoint_length_limit
)
10939 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
10941 if (type
== bp_hardware_breakpoint
)
10943 if (remote_hw_breakpoint_limit
== 0)
10945 else if (remote_hw_breakpoint_limit
< 0)
10947 else if (cnt
<= remote_hw_breakpoint_limit
)
10952 if (remote_hw_watchpoint_limit
== 0)
10954 else if (remote_hw_watchpoint_limit
< 0)
10958 else if (cnt
<= remote_hw_watchpoint_limit
)
10964 /* The to_stopped_by_sw_breakpoint method of target remote. */
10967 remote_target::stopped_by_sw_breakpoint ()
10969 struct thread_info
*thread
= inferior_thread ();
10971 return (thread
->priv
!= NULL
10972 && (get_remote_thread_info (thread
)->stop_reason
10973 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
10976 /* The to_supports_stopped_by_sw_breakpoint method of target
10980 remote_target::supports_stopped_by_sw_breakpoint ()
10982 return (m_features
.packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
10985 /* The to_stopped_by_hw_breakpoint method of target remote. */
10988 remote_target::stopped_by_hw_breakpoint ()
10990 struct thread_info
*thread
= inferior_thread ();
10992 return (thread
->priv
!= NULL
10993 && (get_remote_thread_info (thread
)->stop_reason
10994 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
10997 /* The to_supports_stopped_by_hw_breakpoint method of target
11001 remote_target::supports_stopped_by_hw_breakpoint ()
11003 return (m_features
.packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
11007 remote_target::stopped_by_watchpoint ()
11009 struct thread_info
*thread
= inferior_thread ();
11011 return (thread
->priv
!= NULL
11012 && (get_remote_thread_info (thread
)->stop_reason
11013 == TARGET_STOPPED_BY_WATCHPOINT
));
11017 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
11019 struct thread_info
*thread
= inferior_thread ();
11021 if (thread
->priv
!= NULL
11022 && (get_remote_thread_info (thread
)->stop_reason
11023 == TARGET_STOPPED_BY_WATCHPOINT
))
11025 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
11034 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
11035 struct bp_target_info
*bp_tgt
)
11037 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
11038 struct remote_state
*rs
;
11042 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11045 /* Make sure the remote is pointing at the right process, if
11047 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
11048 set_general_process ();
11050 rs
= get_remote_state ();
11051 p
= rs
->buf
.data ();
11052 endbuf
= p
+ get_remote_packet_size ();
11058 addr
= remote_address_masked (addr
);
11059 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11060 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11062 if (supports_evaluation_of_breakpoint_conditions ())
11063 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
11065 if (can_run_breakpoint_commands ())
11066 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
11069 getpkt (&rs
->buf
, 0);
11071 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11074 if (rs
->buf
[1] == '.')
11076 message
= strchr (&rs
->buf
[2], '.');
11078 error (_("Remote failure reply: %s"), message
+ 1);
11081 case PACKET_UNKNOWN
:
11086 internal_error (_("remote_insert_hw_breakpoint: reached end of function"));
11091 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
11092 struct bp_target_info
*bp_tgt
)
11095 struct remote_state
*rs
= get_remote_state ();
11096 char *p
= rs
->buf
.data ();
11097 char *endbuf
= p
+ get_remote_packet_size ();
11099 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11102 /* Make sure the remote is pointing at the right process, if
11104 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
11105 set_general_process ();
11111 addr
= remote_address_masked (bp_tgt
->placed_address
);
11112 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11113 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11116 getpkt (&rs
->buf
, 0);
11118 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11121 case PACKET_UNKNOWN
:
11126 internal_error (_("remote_remove_hw_breakpoint: reached end of function"));
11129 /* Verify memory using the "qCRC:" request. */
11132 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
11134 struct remote_state
*rs
= get_remote_state ();
11135 unsigned long host_crc
, target_crc
;
11138 /* It doesn't make sense to use qCRC if the remote target is
11139 connected but not running. */
11140 if (target_has_execution ()
11141 && m_features
.packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
11143 enum packet_result result
;
11145 /* Make sure the remote is pointing at the right process. */
11146 set_general_process ();
11148 /* FIXME: assumes lma can fit into long. */
11149 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
11150 (long) lma
, (long) size
);
11153 /* Be clever; compute the host_crc before waiting for target
11155 host_crc
= xcrc32 (data
, size
, 0xffffffff);
11157 getpkt (&rs
->buf
, 0);
11159 result
= m_features
.packet_ok (rs
->buf
, PACKET_qCRC
);
11160 if (result
== PACKET_ERROR
)
11162 else if (result
== PACKET_OK
)
11164 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11165 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11167 return (host_crc
== target_crc
);
11171 return simple_verify_memory (this, data
, lma
, size
);
11174 /* compare-sections command
11176 With no arguments, compares each loadable section in the exec bfd
11177 with the same memory range on the target, and reports mismatches.
11178 Useful for verifying the image on the target against the exec file. */
11181 compare_sections_command (const char *args
, int from_tty
)
11184 const char *sectname
;
11185 bfd_size_type size
;
11188 int mismatched
= 0;
11192 if (!current_program_space
->exec_bfd ())
11193 error (_("command cannot be used without an exec file"));
11195 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11201 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11203 if (!(s
->flags
& SEC_LOAD
))
11204 continue; /* Skip non-loadable section. */
11206 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11207 continue; /* Skip writeable sections */
11209 size
= bfd_section_size (s
);
11211 continue; /* Skip zero-length section. */
11213 sectname
= bfd_section_name (s
);
11214 if (args
&& strcmp (args
, sectname
) != 0)
11215 continue; /* Not the section selected by user. */
11217 matched
= 1; /* Do this section. */
11220 gdb::byte_vector
sectdata (size
);
11221 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11222 sectdata
.data (), 0, size
);
11224 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11227 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11228 paddress (target_gdbarch (), lma
),
11229 paddress (target_gdbarch (), lma
+ size
));
11231 gdb_printf ("Section %s, range %s -- %s: ", sectname
,
11232 paddress (target_gdbarch (), lma
),
11233 paddress (target_gdbarch (), lma
+ size
));
11235 gdb_printf ("matched.\n");
11238 gdb_printf ("MIS-MATCHED!\n");
11242 if (mismatched
> 0)
11243 warning (_("One or more sections of the target image does "
11244 "not match the loaded file"));
11245 if (args
&& !matched
)
11246 gdb_printf (_("No loaded section named '%s'.\n"), args
);
11249 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11250 into remote target. The number of bytes written to the remote
11251 target is returned, or -1 for error. */
11254 remote_target::remote_write_qxfer (const char *object_name
,
11255 const char *annex
, const gdb_byte
*writebuf
,
11256 ULONGEST offset
, LONGEST len
,
11257 ULONGEST
*xfered_len
,
11258 const unsigned int which_packet
)
11262 struct remote_state
*rs
= get_remote_state ();
11263 int max_size
= get_memory_write_packet_size ();
11265 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11266 return TARGET_XFER_E_IO
;
11268 /* Insert header. */
11269 i
= snprintf (rs
->buf
.data (), max_size
,
11270 "qXfer:%s:write:%s:%s:",
11271 object_name
, annex
? annex
: "",
11272 phex_nz (offset
, sizeof offset
));
11273 max_size
-= (i
+ 1);
11275 /* Escape as much data as fits into rs->buf. */
11276 buf_len
= remote_escape_output
11277 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11279 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11280 || getpkt (&rs
->buf
, 0) < 0
11281 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11282 return TARGET_XFER_E_IO
;
11284 unpack_varlen_hex (rs
->buf
.data (), &n
);
11287 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11290 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11291 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11292 number of bytes read is returned, or 0 for EOF, or -1 for error.
11293 The number of bytes read may be less than LEN without indicating an
11294 EOF. PACKET is checked and updated to indicate whether the remote
11295 target supports this object. */
11298 remote_target::remote_read_qxfer (const char *object_name
,
11300 gdb_byte
*readbuf
, ULONGEST offset
,
11302 ULONGEST
*xfered_len
,
11303 const unsigned int which_packet
)
11305 struct remote_state
*rs
= get_remote_state ();
11306 LONGEST i
, n
, packet_len
;
11308 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11309 return TARGET_XFER_E_IO
;
11311 /* Check whether we've cached an end-of-object packet that matches
11313 if (rs
->finished_object
)
11315 if (strcmp (object_name
, rs
->finished_object
) == 0
11316 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11317 && offset
== rs
->finished_offset
)
11318 return TARGET_XFER_EOF
;
11321 /* Otherwise, we're now reading something different. Discard
11323 xfree (rs
->finished_object
);
11324 xfree (rs
->finished_annex
);
11325 rs
->finished_object
= NULL
;
11326 rs
->finished_annex
= NULL
;
11329 /* Request only enough to fit in a single packet. The actual data
11330 may not, since we don't know how much of it will need to be escaped;
11331 the target is free to respond with slightly less data. We subtract
11332 five to account for the response type and the protocol frame. */
11333 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11334 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11335 "qXfer:%s:read:%s:%s,%s",
11336 object_name
, annex
? annex
: "",
11337 phex_nz (offset
, sizeof offset
),
11338 phex_nz (n
, sizeof n
));
11339 i
= putpkt (rs
->buf
);
11341 return TARGET_XFER_E_IO
;
11344 packet_len
= getpkt (&rs
->buf
, 0);
11346 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11347 return TARGET_XFER_E_IO
;
11349 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11350 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11352 /* 'm' means there is (or at least might be) more data after this
11353 batch. That does not make sense unless there's at least one byte
11354 of data in this reply. */
11355 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11356 error (_("Remote qXfer reply contained no data."));
11358 /* Got some data. */
11359 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11360 packet_len
- 1, readbuf
, n
);
11362 /* 'l' is an EOF marker, possibly including a final block of data,
11363 or possibly empty. If we have the final block of a non-empty
11364 object, record this fact to bypass a subsequent partial read. */
11365 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11367 rs
->finished_object
= xstrdup (object_name
);
11368 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11369 rs
->finished_offset
= offset
+ i
;
11373 return TARGET_XFER_EOF
;
11377 return TARGET_XFER_OK
;
11381 enum target_xfer_status
11382 remote_target::xfer_partial (enum target_object object
,
11383 const char *annex
, gdb_byte
*readbuf
,
11384 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11385 ULONGEST
*xfered_len
)
11387 struct remote_state
*rs
;
11391 int unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
11393 set_remote_traceframe ();
11394 set_general_thread (inferior_ptid
);
11396 rs
= get_remote_state ();
11398 /* Handle memory using the standard memory routines. */
11399 if (object
== TARGET_OBJECT_MEMORY
)
11401 /* If the remote target is connected but not running, we should
11402 pass this request down to a lower stratum (e.g. the executable
11404 if (!target_has_execution ())
11405 return TARGET_XFER_EOF
;
11407 if (writebuf
!= NULL
)
11408 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11411 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11415 /* Handle extra signal info using qxfer packets. */
11416 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11419 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11420 xfered_len
, PACKET_qXfer_siginfo_read
);
11422 return remote_write_qxfer ("siginfo", annex
, writebuf
, offset
, len
,
11423 xfered_len
, PACKET_qXfer_siginfo_write
);
11426 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11429 return remote_read_qxfer ("statictrace", annex
,
11430 readbuf
, offset
, len
, xfered_len
,
11431 PACKET_qXfer_statictrace_read
);
11433 return TARGET_XFER_E_IO
;
11436 /* Only handle flash writes. */
11437 if (writebuf
!= NULL
)
11441 case TARGET_OBJECT_FLASH
:
11442 return remote_flash_write (offset
, len
, xfered_len
,
11446 return TARGET_XFER_E_IO
;
11450 /* Map pre-existing objects onto letters. DO NOT do this for new
11451 objects!!! Instead specify new query packets. */
11454 case TARGET_OBJECT_AVR
:
11458 case TARGET_OBJECT_AUXV
:
11459 gdb_assert (annex
== NULL
);
11460 return remote_read_qxfer
11461 ("auxv", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_auxv
);
11463 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11464 return remote_read_qxfer
11465 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11466 PACKET_qXfer_features
);
11468 case TARGET_OBJECT_LIBRARIES
:
11469 return remote_read_qxfer
11470 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11471 PACKET_qXfer_libraries
);
11473 case TARGET_OBJECT_LIBRARIES_SVR4
:
11474 return remote_read_qxfer
11475 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11476 PACKET_qXfer_libraries_svr4
);
11478 case TARGET_OBJECT_MEMORY_MAP
:
11479 gdb_assert (annex
== NULL
);
11480 return remote_read_qxfer
11481 ("memory-map", annex
, readbuf
, offset
, len
, xfered_len
,
11482 PACKET_qXfer_memory_map
);
11484 case TARGET_OBJECT_OSDATA
:
11485 /* Should only get here if we're connected. */
11486 gdb_assert (rs
->remote_desc
);
11487 return remote_read_qxfer
11488 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11489 PACKET_qXfer_osdata
);
11491 case TARGET_OBJECT_THREADS
:
11492 gdb_assert (annex
== NULL
);
11493 return remote_read_qxfer
11494 ("threads", annex
, readbuf
, offset
, len
, xfered_len
,
11495 PACKET_qXfer_threads
);
11497 case TARGET_OBJECT_TRACEFRAME_INFO
:
11498 gdb_assert (annex
== NULL
);
11499 return remote_read_qxfer
11500 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11501 PACKET_qXfer_traceframe_info
);
11503 case TARGET_OBJECT_FDPIC
:
11504 return remote_read_qxfer
11505 ("fdpic", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_fdpic
);
11507 case TARGET_OBJECT_OPENVMS_UIB
:
11508 return remote_read_qxfer
11509 ("uib", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_uib
);
11511 case TARGET_OBJECT_BTRACE
:
11512 return remote_read_qxfer
11513 ("btrace", annex
, readbuf
, offset
, len
, xfered_len
,
11514 PACKET_qXfer_btrace
);
11516 case TARGET_OBJECT_BTRACE_CONF
:
11517 return remote_read_qxfer
11518 ("btrace-conf", annex
, readbuf
, offset
, len
, xfered_len
,
11519 PACKET_qXfer_btrace_conf
);
11521 case TARGET_OBJECT_EXEC_FILE
:
11522 return remote_read_qxfer
11523 ("exec-file", annex
, readbuf
, offset
, len
, xfered_len
,
11524 PACKET_qXfer_exec_file
);
11527 return TARGET_XFER_E_IO
;
11530 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11531 large enough let the caller deal with it. */
11532 if (len
< get_remote_packet_size ())
11533 return TARGET_XFER_E_IO
;
11534 len
= get_remote_packet_size ();
11536 /* Except for querying the minimum buffer size, target must be open. */
11537 if (!rs
->remote_desc
)
11538 error (_("remote query is only available after target open"));
11540 gdb_assert (annex
!= NULL
);
11541 gdb_assert (readbuf
!= NULL
);
11543 p2
= rs
->buf
.data ();
11545 *p2
++ = query_type
;
11547 /* We used one buffer char for the remote protocol q command and
11548 another for the query type. As the remote protocol encapsulation
11549 uses 4 chars plus one extra in case we are debugging
11550 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11553 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11555 /* Bad caller may have sent forbidden characters. */
11556 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11561 gdb_assert (annex
[i
] == '\0');
11563 i
= putpkt (rs
->buf
);
11565 return TARGET_XFER_E_IO
;
11567 getpkt (&rs
->buf
, 0);
11568 strcpy ((char *) readbuf
, rs
->buf
.data ());
11570 *xfered_len
= strlen ((char *) readbuf
);
11571 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11574 /* Implementation of to_get_memory_xfer_limit. */
11577 remote_target::get_memory_xfer_limit ()
11579 return get_memory_write_packet_size ();
11583 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11584 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11585 CORE_ADDR
*found_addrp
)
11587 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
11588 struct remote_state
*rs
= get_remote_state ();
11589 int max_size
= get_memory_write_packet_size ();
11591 /* Number of packet bytes used to encode the pattern;
11592 this could be more than PATTERN_LEN due to escape characters. */
11593 int escaped_pattern_len
;
11594 /* Amount of pattern that was encodable in the packet. */
11595 int used_pattern_len
;
11598 ULONGEST found_addr
;
11600 auto read_memory
= [this] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11602 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11606 /* Don't go to the target if we don't have to. This is done before
11607 checking packet_support to avoid the possibility that a success for this
11608 edge case means the facility works in general. */
11609 if (pattern_len
> search_space_len
)
11611 if (pattern_len
== 0)
11613 *found_addrp
= start_addr
;
11617 /* If we already know the packet isn't supported, fall back to the simple
11618 way of searching memory. */
11620 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11622 /* Target doesn't provided special support, fall back and use the
11623 standard support (copy memory and do the search here). */
11624 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11625 pattern
, pattern_len
, found_addrp
);
11628 /* Make sure the remote is pointing at the right process. */
11629 set_general_process ();
11631 /* Insert header. */
11632 i
= snprintf (rs
->buf
.data (), max_size
,
11633 "qSearch:memory:%s;%s;",
11634 phex_nz (start_addr
, addr_size
),
11635 phex_nz (search_space_len
, sizeof (search_space_len
)));
11636 max_size
-= (i
+ 1);
11638 /* Escape as much data as fits into rs->buf. */
11639 escaped_pattern_len
=
11640 remote_escape_output (pattern
, pattern_len
, 1,
11641 (gdb_byte
*) rs
->buf
.data () + i
,
11642 &used_pattern_len
, max_size
);
11644 /* Bail if the pattern is too large. */
11645 if (used_pattern_len
!= pattern_len
)
11646 error (_("Pattern is too large to transmit to remote target."));
11648 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
11649 || getpkt (&rs
->buf
, 0) < 0
11650 || m_features
.packet_ok (rs
->buf
, PACKET_qSearch_memory
) != PACKET_OK
)
11652 /* The request may not have worked because the command is not
11653 supported. If so, fall back to the simple way. */
11654 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11656 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11657 pattern
, pattern_len
, found_addrp
);
11662 if (rs
->buf
[0] == '0')
11664 else if (rs
->buf
[0] == '1')
11667 if (rs
->buf
[1] != ',')
11668 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11669 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
11670 *found_addrp
= found_addr
;
11673 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11679 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
11681 struct remote_state
*rs
= get_remote_state ();
11682 char *p
= rs
->buf
.data ();
11684 if (!rs
->remote_desc
)
11685 error (_("remote rcmd is only available after target open"));
11687 /* Send a NULL command across as an empty command. */
11688 if (command
== NULL
)
11691 /* The query prefix. */
11692 strcpy (rs
->buf
.data (), "qRcmd,");
11693 p
= strchr (rs
->buf
.data (), '\0');
11695 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
11696 > get_remote_packet_size ())
11697 error (_("\"monitor\" command ``%s'' is too long."), command
);
11699 /* Encode the actual command. */
11700 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
11702 if (putpkt (rs
->buf
) < 0)
11703 error (_("Communication problem with target."));
11705 /* get/display the response */
11710 /* XXX - see also remote_get_noisy_reply(). */
11711 QUIT
; /* Allow user to bail out with ^C. */
11713 if (getpkt (&rs
->buf
, 0) == -1)
11715 /* Timeout. Continue to (try to) read responses.
11716 This is better than stopping with an error, assuming the stub
11717 is still executing the (long) monitor command.
11718 If needed, the user can interrupt gdb using C-c, obtaining
11719 an effect similar to stop on timeout. */
11722 buf
= rs
->buf
.data ();
11723 if (buf
[0] == '\0')
11724 error (_("Target does not support this command."));
11725 if (buf
[0] == 'O' && buf
[1] != 'K')
11727 remote_console_output (buf
+ 1); /* 'O' message from stub. */
11730 if (strcmp (buf
, "OK") == 0)
11732 if (strlen (buf
) == 3 && buf
[0] == 'E'
11733 && isxdigit (buf
[1]) && isxdigit (buf
[2]))
11735 error (_("Protocol error with Rcmd"));
11737 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
11739 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
11741 gdb_putc (c
, outbuf
);
11747 std::vector
<mem_region
>
11748 remote_target::memory_map ()
11750 std::vector
<mem_region
> result
;
11751 gdb::optional
<gdb::char_vector
> text
11752 = target_read_stralloc (current_inferior ()->top_target (),
11753 TARGET_OBJECT_MEMORY_MAP
, NULL
);
11756 result
= parse_memory_map (text
->data ());
11761 /* Set of callbacks used to implement the 'maint packet' command. */
11763 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
11765 /* Called before the packet is sent. BUF is the packet content before
11766 the protocol specific prefix, suffix, and escaping is added. */
11768 void sending (gdb::array_view
<const char> &buf
) override
11770 gdb_puts ("sending: ");
11771 print_packet (buf
);
11775 /* Called with BUF, the reply from the remote target. */
11777 void received (gdb::array_view
<const char> &buf
) override
11779 gdb_puts ("received: \"");
11780 print_packet (buf
);
11786 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
11787 '\x??' with '??' replaced by the hexadecimal value of the byte. */
11790 print_packet (gdb::array_view
<const char> &buf
)
11794 for (int i
= 0; i
< buf
.size (); ++i
)
11796 gdb_byte c
= buf
[i
];
11798 gdb_putc (c
, &stb
);
11800 gdb_printf (&stb
, "\\x%02x", (unsigned char) c
);
11803 gdb_puts (stb
.string ().c_str ());
11807 /* See remote.h. */
11810 send_remote_packet (gdb::array_view
<const char> &buf
,
11811 send_remote_packet_callbacks
*callbacks
)
11813 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
11814 error (_("a remote packet must not be empty"));
11816 remote_target
*remote
= get_current_remote_target ();
11817 if (remote
== nullptr)
11818 error (_("packets can only be sent to a remote target"));
11820 callbacks
->sending (buf
);
11822 remote
->putpkt_binary (buf
.data (), buf
.size ());
11823 remote_state
*rs
= remote
->get_remote_state ();
11824 int bytes
= remote
->getpkt (&rs
->buf
, 0);
11827 error (_("error while fetching packet from remote target"));
11829 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
11830 callbacks
->received (view
);
11833 /* Entry point for the 'maint packet' command. */
11836 cli_packet_command (const char *args
, int from_tty
)
11838 cli_packet_command_callbacks cb
;
11839 gdb::array_view
<const char> view
11840 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
11841 send_remote_packet (view
, &cb
);
11845 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
11847 static void display_thread_info (struct gdb_ext_thread_info
*info
);
11849 static void threadset_test_cmd (char *cmd
, int tty
);
11851 static void threadalive_test (char *cmd
, int tty
);
11853 static void threadlist_test_cmd (char *cmd
, int tty
);
11855 int get_and_display_threadinfo (threadref
*ref
);
11857 static void threadinfo_test_cmd (char *cmd
, int tty
);
11859 static int thread_display_step (threadref
*ref
, void *context
);
11861 static void threadlist_update_test_cmd (char *cmd
, int tty
);
11863 static void init_remote_threadtests (void);
11865 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
11868 threadset_test_cmd (const char *cmd
, int tty
)
11870 int sample_thread
= SAMPLE_THREAD
;
11872 gdb_printf (_("Remote threadset test\n"));
11873 set_general_thread (sample_thread
);
11878 threadalive_test (const char *cmd
, int tty
)
11880 int sample_thread
= SAMPLE_THREAD
;
11881 int pid
= inferior_ptid
.pid ();
11882 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
11884 if (remote_thread_alive (ptid
))
11885 gdb_printf ("PASS: Thread alive test\n");
11887 gdb_printf ("FAIL: Thread alive test\n");
11890 void output_threadid (char *title
, threadref
*ref
);
11893 output_threadid (char *title
, threadref
*ref
)
11897 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
11899 gdb_printf ("%s %s\n", title
, (&hexid
[0]));
11903 threadlist_test_cmd (const char *cmd
, int tty
)
11906 threadref nextthread
;
11907 int done
, result_count
;
11908 threadref threadlist
[3];
11910 gdb_printf ("Remote Threadlist test\n");
11911 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
11912 &result_count
, &threadlist
[0]))
11913 gdb_printf ("FAIL: threadlist test\n");
11916 threadref
*scan
= threadlist
;
11917 threadref
*limit
= scan
+ result_count
;
11919 while (scan
< limit
)
11920 output_threadid (" thread ", scan
++);
11925 display_thread_info (struct gdb_ext_thread_info
*info
)
11927 output_threadid ("Threadid: ", &info
->threadid
);
11928 gdb_printf ("Name: %s\n ", info
->shortname
);
11929 gdb_printf ("State: %s\n", info
->display
);
11930 gdb_printf ("other: %s\n\n", info
->more_display
);
11934 get_and_display_threadinfo (threadref
*ref
)
11938 struct gdb_ext_thread_info threadinfo
;
11940 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
11941 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
11942 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
11943 display_thread_info (&threadinfo
);
11948 threadinfo_test_cmd (const char *cmd
, int tty
)
11950 int athread
= SAMPLE_THREAD
;
11954 int_to_threadref (&thread
, athread
);
11955 gdb_printf ("Remote Threadinfo test\n");
11956 if (!get_and_display_threadinfo (&thread
))
11957 gdb_printf ("FAIL cannot get thread info\n");
11961 thread_display_step (threadref
*ref
, void *context
)
11963 /* output_threadid(" threadstep ",ref); *//* simple test */
11964 return get_and_display_threadinfo (ref
);
11968 threadlist_update_test_cmd (const char *cmd
, int tty
)
11970 gdb_printf ("Remote Threadlist update test\n");
11971 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
11975 init_remote_threadtests (void)
11977 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
11978 _("Fetch and print the remote list of "
11979 "thread identifiers, one pkt only."));
11980 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
11981 _("Fetch and display info about one thread."));
11982 add_com ("tset", class_obscure
, threadset_test_cmd
,
11983 _("Test setting to a different thread."));
11984 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
11985 _("Iterate through updating all remote thread info."));
11986 add_com ("talive", class_obscure
, threadalive_test
,
11987 _("Remote thread alive test."));
11992 /* Convert a thread ID to a string. */
11995 remote_target::pid_to_str (ptid_t ptid
)
11997 if (ptid
== null_ptid
)
11998 return normal_pid_to_str (ptid
);
11999 else if (ptid
.is_pid ())
12001 /* Printing an inferior target id. */
12003 /* When multi-process extensions are off, there's no way in the
12004 remote protocol to know the remote process id, if there's any
12005 at all. There's one exception --- when we're connected with
12006 target extended-remote, and we manually attached to a process
12007 with "attach PID". We don't record anywhere a flag that
12008 allows us to distinguish that case from the case of
12009 connecting with extended-remote and the stub already being
12010 attached to a process, and reporting yes to qAttached, hence
12011 no smart special casing here. */
12012 if (!m_features
.remote_multi_process_p ())
12013 return "Remote target";
12015 return normal_pid_to_str (ptid
);
12019 if (magic_null_ptid
== ptid
)
12020 return "Thread <main>";
12021 else if (m_features
.remote_multi_process_p ())
12022 if (ptid
.lwp () == 0)
12023 return normal_pid_to_str (ptid
);
12025 return string_printf ("Thread %d.%ld",
12026 ptid
.pid (), ptid
.lwp ());
12028 return string_printf ("Thread %ld", ptid
.lwp ());
12032 /* Get the address of the thread local variable in OBJFILE which is
12033 stored at OFFSET within the thread local storage for thread PTID. */
12036 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
12039 if (m_features
.packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
12041 struct remote_state
*rs
= get_remote_state ();
12042 char *p
= rs
->buf
.data ();
12043 char *endp
= p
+ get_remote_packet_size ();
12044 enum packet_result result
;
12046 strcpy (p
, "qGetTLSAddr:");
12048 p
= write_ptid (p
, endp
, ptid
);
12050 p
+= hexnumstr (p
, offset
);
12052 p
+= hexnumstr (p
, lm
);
12056 getpkt (&rs
->buf
, 0);
12057 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTLSAddr
);
12058 if (result
== PACKET_OK
)
12062 unpack_varlen_hex (rs
->buf
.data (), &addr
);
12065 else if (result
== PACKET_UNKNOWN
)
12066 throw_error (TLS_GENERIC_ERROR
,
12067 _("Remote target doesn't support qGetTLSAddr packet"));
12069 throw_error (TLS_GENERIC_ERROR
,
12070 _("Remote target failed to process qGetTLSAddr request"));
12073 throw_error (TLS_GENERIC_ERROR
,
12074 _("TLS not supported or disabled on this target"));
12079 /* Provide thread local base, i.e. Thread Information Block address.
12080 Returns 1 if ptid is found and thread_local_base is non zero. */
12083 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
12085 if (m_features
.packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
12087 struct remote_state
*rs
= get_remote_state ();
12088 char *p
= rs
->buf
.data ();
12089 char *endp
= p
+ get_remote_packet_size ();
12090 enum packet_result result
;
12092 strcpy (p
, "qGetTIBAddr:");
12094 p
= write_ptid (p
, endp
, ptid
);
12098 getpkt (&rs
->buf
, 0);
12099 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTIBAddr
);
12100 if (result
== PACKET_OK
)
12103 unpack_varlen_hex (rs
->buf
.data (), &val
);
12105 *addr
= (CORE_ADDR
) val
;
12108 else if (result
== PACKET_UNKNOWN
)
12109 error (_("Remote target doesn't support qGetTIBAddr packet"));
12111 error (_("Remote target failed to process qGetTIBAddr request"));
12114 error (_("qGetTIBAddr not supported or disabled on this target"));
12119 /* Support for inferring a target description based on the current
12120 architecture and the size of a 'g' packet. While the 'g' packet
12121 can have any size (since optional registers can be left off the
12122 end), some sizes are easily recognizable given knowledge of the
12123 approximate architecture. */
12125 struct remote_g_packet_guess
12127 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
12134 const struct target_desc
*tdesc
;
12137 struct remote_g_packet_data
12139 std::vector
<remote_g_packet_guess
> guesses
;
12142 static const registry
<gdbarch
>::key
<struct remote_g_packet_data
>
12143 remote_g_packet_data_handle
;
12145 static struct remote_g_packet_data
*
12146 get_g_packet_data (struct gdbarch
*gdbarch
)
12148 struct remote_g_packet_data
*data
12149 = remote_g_packet_data_handle
.get (gdbarch
);
12150 if (data
== nullptr)
12151 data
= remote_g_packet_data_handle
.emplace (gdbarch
);
12156 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12157 const struct target_desc
*tdesc
)
12159 struct remote_g_packet_data
*data
= get_g_packet_data (gdbarch
);
12161 gdb_assert (tdesc
!= NULL
);
12163 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12164 if (guess
.bytes
== bytes
)
12165 internal_error (_("Duplicate g packet description added for size %d"),
12168 data
->guesses
.emplace_back (bytes
, tdesc
);
12171 /* Return true if remote_read_description would do anything on this target
12172 and architecture, false otherwise. */
12175 remote_read_description_p (struct target_ops
*target
)
12177 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12179 return !data
->guesses
.empty ();
12182 const struct target_desc
*
12183 remote_target::read_description ()
12185 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12187 /* Do not try this during initial connection, when we do not know
12188 whether there is a running but stopped thread. */
12189 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12190 return beneath ()->read_description ();
12192 if (!data
->guesses
.empty ())
12194 int bytes
= send_g_packet ();
12196 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12197 if (guess
.bytes
== bytes
)
12198 return guess
.tdesc
;
12200 /* We discard the g packet. A minor optimization would be to
12201 hold on to it, and fill the register cache once we have selected
12202 an architecture, but it's too tricky to do safely. */
12205 return beneath ()->read_description ();
12208 /* Remote file transfer support. This is host-initiated I/O, not
12209 target-initiated; for target-initiated, see remote-fileio.c. */
12211 /* If *LEFT is at least the length of STRING, copy STRING to
12212 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12213 decrease *LEFT. Otherwise raise an error. */
12216 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12218 int len
= strlen (string
);
12221 error (_("Packet too long for target."));
12223 memcpy (*buffer
, string
, len
);
12227 /* NUL-terminate the buffer as a convenience, if there is
12233 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12234 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12235 decrease *LEFT. Otherwise raise an error. */
12238 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12241 if (2 * len
> *left
)
12242 error (_("Packet too long for target."));
12244 bin2hex (bytes
, *buffer
, len
);
12245 *buffer
+= 2 * len
;
12248 /* NUL-terminate the buffer as a convenience, if there is
12254 /* If *LEFT is large enough, convert VALUE to hex and add it to
12255 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12256 decrease *LEFT. Otherwise raise an error. */
12259 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12261 int len
= hexnumlen (value
);
12264 error (_("Packet too long for target."));
12266 hexnumstr (*buffer
, value
);
12270 /* NUL-terminate the buffer as a convenience, if there is
12276 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12277 value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
12278 was included, and *ATTACHMENT to point to the start of the annex
12279 if any. The length of the packet isn't needed here; there may
12280 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12282 Return 0 if the packet could be parsed, -1 if it could not. If
12283 -1 is returned, the other variables may not be initialized. */
12286 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12287 fileio_error
*remote_errno
, const char **attachment
)
12291 *remote_errno
= FILEIO_SUCCESS
;
12292 *attachment
= NULL
;
12294 if (buffer
[0] != 'F')
12298 *retcode
= strtol (&buffer
[1], &p
, 16);
12299 if (errno
!= 0 || p
== &buffer
[1])
12302 /* Check for ",errno". */
12306 *remote_errno
= (fileio_error
) strtol (p
+ 1, &p2
, 16);
12307 if (errno
!= 0 || p
+ 1 == p2
)
12312 /* Check for ";attachment". If there is no attachment, the
12313 packet should end here. */
12316 *attachment
= p
+ 1;
12319 else if (*p
== '\0')
12325 /* Send a prepared I/O packet to the target and read its response.
12326 The prepared packet is in the global RS->BUF before this function
12327 is called, and the answer is there when we return.
12329 COMMAND_BYTES is the length of the request to send, which may include
12330 binary data. WHICH_PACKET is the packet configuration to check
12331 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12332 is set to the error number and -1 is returned. Otherwise the value
12333 returned by the function is returned.
12335 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12336 attachment is expected; an error will be reported if there's a
12337 mismatch. If one is found, *ATTACHMENT will be set to point into
12338 the packet buffer and *ATTACHMENT_LEN will be set to the
12339 attachment's length. */
12342 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12343 fileio_error
*remote_errno
, const char **attachment
,
12344 int *attachment_len
)
12346 struct remote_state
*rs
= get_remote_state ();
12347 int ret
, bytes_read
;
12348 const char *attachment_tmp
;
12350 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
12352 *remote_errno
= FILEIO_ENOSYS
;
12356 putpkt_binary (rs
->buf
.data (), command_bytes
);
12357 bytes_read
= getpkt (&rs
->buf
, 0);
12359 /* If it timed out, something is wrong. Don't try to parse the
12361 if (bytes_read
< 0)
12363 *remote_errno
= FILEIO_EINVAL
;
12367 switch (m_features
.packet_ok (rs
->buf
, which_packet
))
12370 *remote_errno
= FILEIO_EINVAL
;
12372 case PACKET_UNKNOWN
:
12373 *remote_errno
= FILEIO_ENOSYS
;
12379 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12382 *remote_errno
= FILEIO_EINVAL
;
12386 /* Make sure we saw an attachment if and only if we expected one. */
12387 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12388 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12390 *remote_errno
= FILEIO_EINVAL
;
12394 /* If an attachment was found, it must point into the packet buffer;
12395 work out how many bytes there were. */
12396 if (attachment_tmp
!= NULL
)
12398 *attachment
= attachment_tmp
;
12399 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12405 /* See declaration.h. */
12408 readahead_cache::invalidate ()
12413 /* See declaration.h. */
12416 readahead_cache::invalidate_fd (int fd
)
12418 if (this->fd
== fd
)
12422 /* Set the filesystem remote_hostio functions that take FILENAME
12423 arguments will use. Return 0 on success, or -1 if an error
12424 occurs (and set *REMOTE_ERRNO). */
12427 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12428 fileio_error
*remote_errno
)
12430 struct remote_state
*rs
= get_remote_state ();
12431 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12432 char *p
= rs
->buf
.data ();
12433 int left
= get_remote_packet_size () - 1;
12437 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12440 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12443 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12445 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12446 remote_buffer_add_string (&p
, &left
, arg
);
12448 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12449 remote_errno
, NULL
, NULL
);
12451 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12455 rs
->fs_pid
= required_pid
;
12460 /* Implementation of to_fileio_open. */
12463 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12464 int flags
, int mode
, int warn_if_slow
,
12465 fileio_error
*remote_errno
)
12467 struct remote_state
*rs
= get_remote_state ();
12468 char *p
= rs
->buf
.data ();
12469 int left
= get_remote_packet_size () - 1;
12473 static int warning_issued
= 0;
12475 gdb_printf (_("Reading %s from remote target...\n"),
12478 if (!warning_issued
)
12480 warning (_("File transfers from remote targets can be slow."
12481 " Use \"set sysroot\" to access files locally"
12483 warning_issued
= 1;
12487 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12490 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12492 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12493 strlen (filename
));
12494 remote_buffer_add_string (&p
, &left
, ",");
12496 remote_buffer_add_int (&p
, &left
, flags
);
12497 remote_buffer_add_string (&p
, &left
, ",");
12499 remote_buffer_add_int (&p
, &left
, mode
);
12501 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12502 remote_errno
, NULL
, NULL
);
12506 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12507 int flags
, int mode
, int warn_if_slow
,
12508 fileio_error
*remote_errno
)
12510 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12514 /* Implementation of to_fileio_pwrite. */
12517 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12518 ULONGEST offset
, fileio_error
*remote_errno
)
12520 struct remote_state
*rs
= get_remote_state ();
12521 char *p
= rs
->buf
.data ();
12522 int left
= get_remote_packet_size ();
12525 rs
->readahead_cache
.invalidate_fd (fd
);
12527 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12529 remote_buffer_add_int (&p
, &left
, fd
);
12530 remote_buffer_add_string (&p
, &left
, ",");
12532 remote_buffer_add_int (&p
, &left
, offset
);
12533 remote_buffer_add_string (&p
, &left
, ",");
12535 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12536 (get_remote_packet_size ()
12537 - (p
- rs
->buf
.data ())));
12539 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12540 remote_errno
, NULL
, NULL
);
12544 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12545 ULONGEST offset
, fileio_error
*remote_errno
)
12547 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12550 /* Helper for the implementation of to_fileio_pread. Read the file
12551 from the remote side with vFile:pread. */
12554 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12555 ULONGEST offset
, fileio_error
*remote_errno
)
12557 struct remote_state
*rs
= get_remote_state ();
12558 char *p
= rs
->buf
.data ();
12559 const char *attachment
;
12560 int left
= get_remote_packet_size ();
12561 int ret
, attachment_len
;
12564 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12566 remote_buffer_add_int (&p
, &left
, fd
);
12567 remote_buffer_add_string (&p
, &left
, ",");
12569 remote_buffer_add_int (&p
, &left
, len
);
12570 remote_buffer_add_string (&p
, &left
, ",");
12572 remote_buffer_add_int (&p
, &left
, offset
);
12574 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12575 remote_errno
, &attachment
,
12581 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12583 if (read_len
!= ret
)
12584 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12589 /* See declaration.h. */
12592 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12596 && this->offset
<= offset
12597 && offset
< this->offset
+ this->buf
.size ())
12599 ULONGEST max
= this->offset
+ this->buf
.size ();
12601 if (offset
+ len
> max
)
12602 len
= max
- offset
;
12604 memcpy (read_buf
, &this->buf
[offset
- this->offset
], len
);
12611 /* Implementation of to_fileio_pread. */
12614 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12615 ULONGEST offset
, fileio_error
*remote_errno
)
12618 struct remote_state
*rs
= get_remote_state ();
12619 readahead_cache
*cache
= &rs
->readahead_cache
;
12621 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
12624 cache
->hit_count
++;
12626 remote_debug_printf ("readahead cache hit %s",
12627 pulongest (cache
->hit_count
));
12631 cache
->miss_count
++;
12633 remote_debug_printf ("readahead cache miss %s",
12634 pulongest (cache
->miss_count
));
12637 cache
->offset
= offset
;
12638 cache
->buf
.resize (get_remote_packet_size ());
12640 ret
= remote_hostio_pread_vFile (cache
->fd
, &cache
->buf
[0],
12641 cache
->buf
.size (),
12642 cache
->offset
, remote_errno
);
12645 cache
->invalidate_fd (fd
);
12649 cache
->buf
.resize (ret
);
12650 return cache
->pread (fd
, read_buf
, len
, offset
);
12654 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12655 ULONGEST offset
, fileio_error
*remote_errno
)
12657 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
12660 /* Implementation of to_fileio_close. */
12663 remote_target::remote_hostio_close (int fd
, fileio_error
*remote_errno
)
12665 struct remote_state
*rs
= get_remote_state ();
12666 char *p
= rs
->buf
.data ();
12667 int left
= get_remote_packet_size () - 1;
12669 rs
->readahead_cache
.invalidate_fd (fd
);
12671 remote_buffer_add_string (&p
, &left
, "vFile:close:");
12673 remote_buffer_add_int (&p
, &left
, fd
);
12675 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
12676 remote_errno
, NULL
, NULL
);
12680 remote_target::fileio_close (int fd
, fileio_error
*remote_errno
)
12682 return remote_hostio_close (fd
, remote_errno
);
12685 /* Implementation of to_fileio_unlink. */
12688 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
12689 fileio_error
*remote_errno
)
12691 struct remote_state
*rs
= get_remote_state ();
12692 char *p
= rs
->buf
.data ();
12693 int left
= get_remote_packet_size () - 1;
12695 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12698 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
12700 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12701 strlen (filename
));
12703 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
12704 remote_errno
, NULL
, NULL
);
12708 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
12709 fileio_error
*remote_errno
)
12711 return remote_hostio_unlink (inf
, filename
, remote_errno
);
12714 /* Implementation of to_fileio_readlink. */
12716 gdb::optional
<std::string
>
12717 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
12718 fileio_error
*remote_errno
)
12720 struct remote_state
*rs
= get_remote_state ();
12721 char *p
= rs
->buf
.data ();
12722 const char *attachment
;
12723 int left
= get_remote_packet_size ();
12724 int len
, attachment_len
;
12727 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12730 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
12732 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12733 strlen (filename
));
12735 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
12736 remote_errno
, &attachment
,
12742 std::string
ret (len
, '\0');
12744 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12745 (gdb_byte
*) &ret
[0], len
);
12746 if (read_len
!= len
)
12747 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
12752 /* Implementation of to_fileio_fstat. */
12755 remote_target::fileio_fstat (int fd
, struct stat
*st
, fileio_error
*remote_errno
)
12757 struct remote_state
*rs
= get_remote_state ();
12758 char *p
= rs
->buf
.data ();
12759 int left
= get_remote_packet_size ();
12760 int attachment_len
, ret
;
12761 const char *attachment
;
12762 struct fio_stat fst
;
12765 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
12767 remote_buffer_add_int (&p
, &left
, fd
);
12769 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
12770 remote_errno
, &attachment
,
12774 if (*remote_errno
!= FILEIO_ENOSYS
)
12777 /* Strictly we should return -1, ENOSYS here, but when
12778 "set sysroot remote:" was implemented in August 2008
12779 BFD's need for a stat function was sidestepped with
12780 this hack. This was not remedied until March 2015
12781 so we retain the previous behavior to avoid breaking
12784 Note that the memset is a March 2015 addition; older
12785 GDBs set st_size *and nothing else* so the structure
12786 would have garbage in all other fields. This might
12787 break something but retaining the previous behavior
12788 here would be just too wrong. */
12790 memset (st
, 0, sizeof (struct stat
));
12791 st
->st_size
= INT_MAX
;
12795 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12796 (gdb_byte
*) &fst
, sizeof (fst
));
12798 if (read_len
!= ret
)
12799 error (_("vFile:fstat returned %d, but %d bytes."), ret
, read_len
);
12801 if (read_len
!= sizeof (fst
))
12802 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12803 read_len
, (int) sizeof (fst
));
12805 remote_fileio_to_host_stat (&fst
, st
);
12810 /* Implementation of to_filesystem_is_local. */
12813 remote_target::filesystem_is_local ()
12815 /* Valgrind GDB presents itself as a remote target but works
12816 on the local filesystem: it does not implement remote get
12817 and users are not expected to set a sysroot. To handle
12818 this case we treat the remote filesystem as local if the
12819 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12820 does not support vFile:open. */
12821 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
12823 packet_support ps
= m_features
.packet_support (PACKET_vFile_open
);
12825 if (ps
== PACKET_SUPPORT_UNKNOWN
)
12828 fileio_error remote_errno
;
12830 /* Try opening a file to probe support. The supplied
12831 filename is irrelevant, we only care about whether
12832 the stub recognizes the packet or not. */
12833 fd
= remote_hostio_open (NULL
, "just probing",
12834 FILEIO_O_RDONLY
, 0700, 0,
12838 remote_hostio_close (fd
, &remote_errno
);
12840 ps
= m_features
.packet_support (PACKET_vFile_open
);
12843 if (ps
== PACKET_DISABLE
)
12845 static int warning_issued
= 0;
12847 if (!warning_issued
)
12849 warning (_("remote target does not support file"
12850 " transfer, attempting to access files"
12851 " from local filesystem."));
12852 warning_issued
= 1;
12863 remote_hostio_error (fileio_error errnum
)
12865 int host_error
= fileio_error_to_host (errnum
);
12867 if (host_error
== -1)
12868 error (_("Unknown remote I/O error %d"), errnum
);
12870 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
12873 /* A RAII wrapper around a remote file descriptor. */
12875 class scoped_remote_fd
12878 scoped_remote_fd (remote_target
*remote
, int fd
)
12879 : m_remote (remote
), m_fd (fd
)
12883 ~scoped_remote_fd ()
12889 fileio_error remote_errno
;
12890 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
12894 /* Swallow exception before it escapes the dtor. If
12895 something goes wrong, likely the connection is gone,
12896 and there's nothing else that can be done. */
12901 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
12903 /* Release ownership of the file descriptor, and return it. */
12904 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
12911 /* Return the owned file descriptor. */
12912 int get () const noexcept
12918 /* The remote target. */
12919 remote_target
*m_remote
;
12921 /* The owned remote I/O file descriptor. */
12926 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
12928 remote_target
*remote
= get_current_remote_target ();
12930 if (remote
== nullptr)
12931 error (_("command can only be used with remote target"));
12933 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
12937 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
12940 int retcode
, bytes
, io_size
;
12941 fileio_error remote_errno
;
12942 int bytes_in_buffer
;
12946 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
12948 perror_with_name (local_file
);
12950 scoped_remote_fd fd
12951 (this, remote_hostio_open (NULL
,
12952 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
12954 0700, 0, &remote_errno
));
12955 if (fd
.get () == -1)
12956 remote_hostio_error (remote_errno
);
12958 /* Send up to this many bytes at once. They won't all fit in the
12959 remote packet limit, so we'll transfer slightly fewer. */
12960 io_size
= get_remote_packet_size ();
12961 gdb::byte_vector
buffer (io_size
);
12963 bytes_in_buffer
= 0;
12966 while (bytes_in_buffer
|| !saw_eof
)
12970 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
12971 io_size
- bytes_in_buffer
,
12975 if (ferror (file
.get ()))
12976 error (_("Error reading %s."), local_file
);
12979 /* EOF. Unless there is something still in the
12980 buffer from the last iteration, we are done. */
12982 if (bytes_in_buffer
== 0)
12990 bytes
+= bytes_in_buffer
;
12991 bytes_in_buffer
= 0;
12993 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
12994 offset
, &remote_errno
);
12997 remote_hostio_error (remote_errno
);
12998 else if (retcode
== 0)
12999 error (_("Remote write of %d bytes returned 0!"), bytes
);
13000 else if (retcode
< bytes
)
13002 /* Short write. Save the rest of the read data for the next
13004 bytes_in_buffer
= bytes
- retcode
;
13005 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
13011 if (remote_hostio_close (fd
.release (), &remote_errno
))
13012 remote_hostio_error (remote_errno
);
13015 gdb_printf (_("Successfully sent file \"%s\".\n"), local_file
);
13019 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
13021 remote_target
*remote
= get_current_remote_target ();
13023 if (remote
== nullptr)
13024 error (_("command can only be used with remote target"));
13026 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
13030 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
13033 fileio_error remote_errno
;
13034 int bytes
, io_size
;
13037 scoped_remote_fd fd
13038 (this, remote_hostio_open (NULL
,
13039 remote_file
, FILEIO_O_RDONLY
, 0, 0,
13041 if (fd
.get () == -1)
13042 remote_hostio_error (remote_errno
);
13044 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
13046 perror_with_name (local_file
);
13048 /* Send up to this many bytes at once. They won't all fit in the
13049 remote packet limit, so we'll transfer slightly fewer. */
13050 io_size
= get_remote_packet_size ();
13051 gdb::byte_vector
buffer (io_size
);
13056 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
13059 /* Success, but no bytes, means end-of-file. */
13062 remote_hostio_error (remote_errno
);
13066 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
13068 perror_with_name (local_file
);
13071 if (remote_hostio_close (fd
.release (), &remote_errno
))
13072 remote_hostio_error (remote_errno
);
13075 gdb_printf (_("Successfully fetched file \"%s\".\n"), remote_file
);
13079 remote_file_delete (const char *remote_file
, int from_tty
)
13081 remote_target
*remote
= get_current_remote_target ();
13083 if (remote
== nullptr)
13084 error (_("command can only be used with remote target"));
13086 remote
->remote_file_delete (remote_file
, from_tty
);
13090 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
13093 fileio_error remote_errno
;
13095 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
13097 remote_hostio_error (remote_errno
);
13100 gdb_printf (_("Successfully deleted file \"%s\".\n"), remote_file
);
13104 remote_put_command (const char *args
, int from_tty
)
13107 error_no_arg (_("file to put"));
13109 gdb_argv
argv (args
);
13110 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13111 error (_("Invalid parameters to remote put"));
13113 remote_file_put (argv
[0], argv
[1], from_tty
);
13117 remote_get_command (const char *args
, int from_tty
)
13120 error_no_arg (_("file to get"));
13122 gdb_argv
argv (args
);
13123 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13124 error (_("Invalid parameters to remote get"));
13126 remote_file_get (argv
[0], argv
[1], from_tty
);
13130 remote_delete_command (const char *args
, int from_tty
)
13133 error_no_arg (_("file to delete"));
13135 gdb_argv
argv (args
);
13136 if (argv
[0] == NULL
|| argv
[1] != NULL
)
13137 error (_("Invalid parameters to remote delete"));
13139 remote_file_delete (argv
[0], from_tty
);
13143 remote_target::can_execute_reverse ()
13145 if (m_features
.packet_support (PACKET_bs
) == PACKET_ENABLE
13146 || m_features
.packet_support (PACKET_bc
) == PACKET_ENABLE
)
13153 remote_target::supports_non_stop ()
13159 remote_target::supports_disable_randomization ()
13161 /* Only supported in extended mode. */
13166 remote_target::supports_multi_process ()
13168 return m_features
.remote_multi_process_p ();
13172 remote_target::remote_supports_cond_tracepoints ()
13174 return (m_features
.packet_support (PACKET_ConditionalTracepoints
)
13179 remote_target::supports_evaluation_of_breakpoint_conditions ()
13181 return (m_features
.packet_support (PACKET_ConditionalBreakpoints
)
13186 remote_target::remote_supports_fast_tracepoints ()
13188 return m_features
.packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13192 remote_target::remote_supports_static_tracepoints ()
13194 return m_features
.packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13198 remote_target::remote_supports_install_in_trace ()
13200 return m_features
.packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13204 remote_target::supports_enable_disable_tracepoint ()
13206 return (m_features
.packet_support (PACKET_EnableDisableTracepoints_feature
)
13211 remote_target::supports_string_tracing ()
13213 return m_features
.packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13217 remote_target::can_run_breakpoint_commands ()
13219 return m_features
.packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13223 remote_target::trace_init ()
13225 struct remote_state
*rs
= get_remote_state ();
13228 remote_get_noisy_reply ();
13229 if (strcmp (rs
->buf
.data (), "OK") != 0)
13230 error (_("Target does not support this command."));
13233 /* Recursive routine to walk through command list including loops, and
13234 download packets for each command. */
13237 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13238 struct command_line
*cmds
)
13240 struct remote_state
*rs
= get_remote_state ();
13241 struct command_line
*cmd
;
13243 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13245 QUIT
; /* Allow user to bail out with ^C. */
13246 strcpy (rs
->buf
.data (), "QTDPsrc:");
13247 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13248 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13249 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13251 remote_get_noisy_reply ();
13252 if (strcmp (rs
->buf
.data (), "OK"))
13253 warning (_("Target does not support source download."));
13255 if (cmd
->control_type
== while_control
13256 || cmd
->control_type
== while_stepping_control
)
13258 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13260 QUIT
; /* Allow user to bail out with ^C. */
13261 strcpy (rs
->buf
.data (), "QTDPsrc:");
13262 encode_source_string (num
, addr
, "cmd", "end",
13263 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13264 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13266 remote_get_noisy_reply ();
13267 if (strcmp (rs
->buf
.data (), "OK"))
13268 warning (_("Target does not support source download."));
13274 remote_target::download_tracepoint (struct bp_location
*loc
)
13278 std::vector
<std::string
> tdp_actions
;
13279 std::vector
<std::string
> stepping_actions
;
13281 struct breakpoint
*b
= loc
->owner
;
13282 struct tracepoint
*t
= (struct tracepoint
*) b
;
13283 struct remote_state
*rs
= get_remote_state ();
13285 const char *err_msg
= _("Tracepoint packet too large for target.");
13288 /* We use a buffer other than rs->buf because we'll build strings
13289 across multiple statements, and other statements in between could
13291 gdb::char_vector
buf (get_remote_packet_size ());
13293 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13295 tpaddr
= loc
->address
;
13296 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13297 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13298 b
->number
, addrbuf
, /* address */
13299 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13300 t
->step_count
, t
->pass_count
);
13302 if (ret
< 0 || ret
>= buf
.size ())
13303 error ("%s", err_msg
);
13305 /* Fast tracepoints are mostly handled by the target, but we can
13306 tell the target how big of an instruction block should be moved
13308 if (b
->type
== bp_fast_tracepoint
)
13310 /* Only test for support at download time; we may not know
13311 target capabilities at definition time. */
13312 if (remote_supports_fast_tracepoints ())
13314 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13317 size_left
= buf
.size () - strlen (buf
.data ());
13318 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13320 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13322 if (ret
< 0 || ret
>= size_left
)
13323 error ("%s", err_msg
);
13326 /* If it passed validation at definition but fails now,
13327 something is very wrong. */
13328 internal_error (_("Fast tracepoint not valid during download"));
13331 /* Fast tracepoints are functionally identical to regular
13332 tracepoints, so don't take lack of support as a reason to
13333 give up on the trace run. */
13334 warning (_("Target does not support fast tracepoints, "
13335 "downloading %d as regular tracepoint"), b
->number
);
13337 else if (b
->type
== bp_static_tracepoint
13338 || b
->type
== bp_static_marker_tracepoint
)
13340 /* Only test for support at download time; we may not know
13341 target capabilities at definition time. */
13342 if (remote_supports_static_tracepoints ())
13344 struct static_tracepoint_marker marker
;
13346 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13348 size_left
= buf
.size () - strlen (buf
.data ());
13349 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13352 if (ret
< 0 || ret
>= size_left
)
13353 error ("%s", err_msg
);
13356 error (_("Static tracepoint not valid during download"));
13359 /* Fast tracepoints are functionally identical to regular
13360 tracepoints, so don't take lack of support as a reason
13361 to give up on the trace run. */
13362 error (_("Target does not support static tracepoints"));
13364 /* If the tracepoint has a conditional, make it into an agent
13365 expression and append to the definition. */
13368 /* Only test support at download time, we may not know target
13369 capabilities at definition time. */
13370 if (remote_supports_cond_tracepoints ())
13372 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13375 size_left
= buf
.size () - strlen (buf
.data ());
13377 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13378 size_left
, ":X%x,", (int) aexpr
->buf
.size ());
13380 if (ret
< 0 || ret
>= size_left
)
13381 error ("%s", err_msg
);
13383 size_left
= buf
.size () - strlen (buf
.data ());
13385 /* Two bytes to encode each aexpr byte, plus the terminating
13387 if (aexpr
->buf
.size () * 2 + 1 > size_left
)
13388 error ("%s", err_msg
);
13390 pkt
= buf
.data () + strlen (buf
.data ());
13392 for (int ndx
= 0; ndx
< aexpr
->buf
.size (); ++ndx
)
13393 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13397 warning (_("Target does not support conditional tracepoints, "
13398 "ignoring tp %d cond"), b
->number
);
13401 if (b
->commands
|| !default_collect
.empty ())
13403 size_left
= buf
.size () - strlen (buf
.data ());
13405 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13408 if (ret
< 0 || ret
>= size_left
)
13409 error ("%s", err_msg
);
13412 putpkt (buf
.data ());
13413 remote_get_noisy_reply ();
13414 if (strcmp (rs
->buf
.data (), "OK"))
13415 error (_("Target does not support tracepoints."));
13417 /* do_single_steps (t); */
13418 for (auto action_it
= tdp_actions
.begin ();
13419 action_it
!= tdp_actions
.end (); action_it
++)
13421 QUIT
; /* Allow user to bail out with ^C. */
13423 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13424 || !stepping_actions
.empty ());
13426 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13427 b
->number
, addrbuf
, /* address */
13428 action_it
->c_str (),
13429 has_more
? '-' : 0);
13431 if (ret
< 0 || ret
>= buf
.size ())
13432 error ("%s", err_msg
);
13434 putpkt (buf
.data ());
13435 remote_get_noisy_reply ();
13436 if (strcmp (rs
->buf
.data (), "OK"))
13437 error (_("Error on target while setting tracepoints."));
13440 for (auto action_it
= stepping_actions
.begin ();
13441 action_it
!= stepping_actions
.end (); action_it
++)
13443 QUIT
; /* Allow user to bail out with ^C. */
13445 bool is_first
= action_it
== stepping_actions
.begin ();
13446 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13448 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13449 b
->number
, addrbuf
, /* address */
13450 is_first
? "S" : "",
13451 action_it
->c_str (),
13452 has_more
? "-" : "");
13454 if (ret
< 0 || ret
>= buf
.size ())
13455 error ("%s", err_msg
);
13457 putpkt (buf
.data ());
13458 remote_get_noisy_reply ();
13459 if (strcmp (rs
->buf
.data (), "OK"))
13460 error (_("Error on target while setting tracepoints."));
13463 if (m_features
.packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13465 if (b
->locspec
!= nullptr)
13467 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13469 if (ret
< 0 || ret
>= buf
.size ())
13470 error ("%s", err_msg
);
13472 const char *str
= b
->locspec
->to_string ();
13473 encode_source_string (b
->number
, loc
->address
, "at", str
,
13474 buf
.data () + strlen (buf
.data ()),
13475 buf
.size () - strlen (buf
.data ()));
13476 putpkt (buf
.data ());
13477 remote_get_noisy_reply ();
13478 if (strcmp (rs
->buf
.data (), "OK"))
13479 warning (_("Target does not support source download."));
13481 if (b
->cond_string
)
13483 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13485 if (ret
< 0 || ret
>= buf
.size ())
13486 error ("%s", err_msg
);
13488 encode_source_string (b
->number
, loc
->address
,
13489 "cond", b
->cond_string
.get (),
13490 buf
.data () + strlen (buf
.data ()),
13491 buf
.size () - strlen (buf
.data ()));
13492 putpkt (buf
.data ());
13493 remote_get_noisy_reply ();
13494 if (strcmp (rs
->buf
.data (), "OK"))
13495 warning (_("Target does not support source download."));
13497 remote_download_command_source (b
->number
, loc
->address
,
13498 breakpoint_commands (b
));
13503 remote_target::can_download_tracepoint ()
13505 struct remote_state
*rs
= get_remote_state ();
13506 struct trace_status
*ts
;
13509 /* Don't try to install tracepoints until we've relocated our
13510 symbols, and fetched and merged the target's tracepoint list with
13512 if (rs
->starting_up
)
13515 ts
= current_trace_status ();
13516 status
= get_trace_status (ts
);
13518 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13521 /* If we are in a tracing experiment, but remote stub doesn't support
13522 installing tracepoint in trace, we have to return. */
13523 if (!remote_supports_install_in_trace ())
13531 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13533 struct remote_state
*rs
= get_remote_state ();
13536 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13537 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13539 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13540 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13541 >= get_remote_packet_size ())
13542 error (_("Trace state variable name too long for tsv definition packet"));
13543 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13546 remote_get_noisy_reply ();
13547 if (rs
->buf
[0] == '\0')
13548 error (_("Target does not support this command."));
13549 if (strcmp (rs
->buf
.data (), "OK") != 0)
13550 error (_("Error on target while downloading trace state variable."));
13554 remote_target::enable_tracepoint (struct bp_location
*location
)
13556 struct remote_state
*rs
= get_remote_state ();
13558 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
13559 location
->owner
->number
,
13560 phex (location
->address
, sizeof (CORE_ADDR
)));
13562 remote_get_noisy_reply ();
13563 if (rs
->buf
[0] == '\0')
13564 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
13565 if (strcmp (rs
->buf
.data (), "OK") != 0)
13566 error (_("Error on target while enabling tracepoint."));
13570 remote_target::disable_tracepoint (struct bp_location
*location
)
13572 struct remote_state
*rs
= get_remote_state ();
13574 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
13575 location
->owner
->number
,
13576 phex (location
->address
, sizeof (CORE_ADDR
)));
13578 remote_get_noisy_reply ();
13579 if (rs
->buf
[0] == '\0')
13580 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
13581 if (strcmp (rs
->buf
.data (), "OK") != 0)
13582 error (_("Error on target while disabling tracepoint."));
13586 remote_target::trace_set_readonly_regions ()
13589 bfd_size_type size
;
13593 bfd
*abfd
= current_program_space
->exec_bfd ();
13596 return; /* No information to give. */
13598 struct remote_state
*rs
= get_remote_state ();
13600 strcpy (rs
->buf
.data (), "QTro");
13601 offset
= strlen (rs
->buf
.data ());
13602 for (s
= abfd
->sections
; s
; s
= s
->next
)
13604 char tmp1
[40], tmp2
[40];
13607 if ((s
->flags
& SEC_LOAD
) == 0
13608 /* || (s->flags & SEC_CODE) == 0 */
13609 || (s
->flags
& SEC_READONLY
) == 0)
13613 vma
= bfd_section_vma (s
);
13614 size
= bfd_section_size (s
);
13615 bfd_sprintf_vma (abfd
, tmp1
, vma
);
13616 bfd_sprintf_vma (abfd
, tmp2
, vma
+ size
);
13617 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
13618 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
13620 if (m_features
.packet_support (PACKET_qXfer_traceframe_info
)
13623 Too many sections for read-only sections definition packet."));
13626 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
13628 offset
+= sec_length
;
13633 getpkt (&rs
->buf
, 0);
13638 remote_target::trace_start ()
13640 struct remote_state
*rs
= get_remote_state ();
13642 putpkt ("QTStart");
13643 remote_get_noisy_reply ();
13644 if (rs
->buf
[0] == '\0')
13645 error (_("Target does not support this command."));
13646 if (strcmp (rs
->buf
.data (), "OK") != 0)
13647 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13651 remote_target::get_trace_status (struct trace_status
*ts
)
13653 /* Initialize it just to avoid a GCC false warning. */
13655 enum packet_result result
;
13656 struct remote_state
*rs
= get_remote_state ();
13658 if (m_features
.packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
13661 /* FIXME we need to get register block size some other way. */
13662 trace_regblock_size
13663 = rs
->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet
;
13665 putpkt ("qTStatus");
13669 p
= remote_get_noisy_reply ();
13671 catch (const gdb_exception_error
&ex
)
13673 if (ex
.error
!= TARGET_CLOSE_ERROR
)
13675 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
13681 result
= m_features
.packet_ok (p
, PACKET_qTStatus
);
13683 /* If the remote target doesn't do tracing, flag it. */
13684 if (result
== PACKET_UNKNOWN
)
13687 /* We're working with a live target. */
13688 ts
->filename
= NULL
;
13691 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
13693 /* Function 'parse_trace_status' sets default value of each field of
13694 'ts' at first, so we don't have to do it here. */
13695 parse_trace_status (p
, ts
);
13697 return ts
->running
;
13701 remote_target::get_tracepoint_status (struct breakpoint
*bp
,
13702 struct uploaded_tp
*utp
)
13704 struct remote_state
*rs
= get_remote_state ();
13706 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
13707 size_t size
= get_remote_packet_size ();
13712 tp
->traceframe_usage
= 0;
13713 for (bp_location
&loc
: tp
->locations ())
13715 /* If the tracepoint was never downloaded, don't go asking for
13717 if (tp
->number_on_target
== 0)
13719 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
13720 phex_nz (loc
.address
, 0));
13722 reply
= remote_get_noisy_reply ();
13723 if (reply
&& *reply
)
13726 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13732 utp
->hit_count
= 0;
13733 utp
->traceframe_usage
= 0;
13734 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
13735 phex_nz (utp
->addr
, 0));
13737 reply
= remote_get_noisy_reply ();
13738 if (reply
&& *reply
)
13741 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13747 remote_target::trace_stop ()
13749 struct remote_state
*rs
= get_remote_state ();
13752 remote_get_noisy_reply ();
13753 if (rs
->buf
[0] == '\0')
13754 error (_("Target does not support this command."));
13755 if (strcmp (rs
->buf
.data (), "OK") != 0)
13756 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13760 remote_target::trace_find (enum trace_find_type type
, int num
,
13761 CORE_ADDR addr1
, CORE_ADDR addr2
,
13764 struct remote_state
*rs
= get_remote_state ();
13765 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
13767 int target_frameno
= -1, target_tracept
= -1;
13769 /* Lookups other than by absolute frame number depend on the current
13770 trace selected, so make sure it is correct on the remote end
13772 if (type
!= tfind_number
)
13773 set_remote_traceframe ();
13775 p
= rs
->buf
.data ();
13776 strcpy (p
, "QTFrame:");
13777 p
= strchr (p
, '\0');
13781 xsnprintf (p
, endbuf
- p
, "%x", num
);
13784 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
13787 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
13790 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
13791 phex_nz (addr2
, 0));
13793 case tfind_outside
:
13794 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
13795 phex_nz (addr2
, 0));
13798 error (_("Unknown trace find type %d"), type
);
13802 reply
= remote_get_noisy_reply ();
13803 if (*reply
== '\0')
13804 error (_("Target does not support this command."));
13806 while (reply
&& *reply
)
13811 target_frameno
= (int) strtol (p
, &reply
, 16);
13813 error (_("Unable to parse trace frame number"));
13814 /* Don't update our remote traceframe number cache on failure
13815 to select a remote traceframe. */
13816 if (target_frameno
== -1)
13821 target_tracept
= (int) strtol (p
, &reply
, 16);
13823 error (_("Unable to parse tracepoint number"));
13825 case 'O': /* "OK"? */
13826 if (reply
[1] == 'K' && reply
[2] == '\0')
13829 error (_("Bogus reply from target: %s"), reply
);
13832 error (_("Bogus reply from target: %s"), reply
);
13835 *tpp
= target_tracept
;
13837 rs
->remote_traceframe_number
= target_frameno
;
13838 return target_frameno
;
13842 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
13844 struct remote_state
*rs
= get_remote_state ();
13848 set_remote_traceframe ();
13850 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
13852 reply
= remote_get_noisy_reply ();
13853 if (reply
&& *reply
)
13857 unpack_varlen_hex (reply
+ 1, &uval
);
13858 *val
= (LONGEST
) uval
;
13866 remote_target::save_trace_data (const char *filename
)
13868 struct remote_state
*rs
= get_remote_state ();
13871 p
= rs
->buf
.data ();
13872 strcpy (p
, "QTSave:");
13874 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
13875 >= get_remote_packet_size ())
13876 error (_("Remote file name too long for trace save packet"));
13877 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
13880 reply
= remote_get_noisy_reply ();
13881 if (*reply
== '\0')
13882 error (_("Target does not support this command."));
13883 if (strcmp (reply
, "OK") != 0)
13884 error (_("Bogus reply from target: %s"), reply
);
13888 /* This is basically a memory transfer, but needs to be its own packet
13889 because we don't know how the target actually organizes its trace
13890 memory, plus we want to be able to ask for as much as possible, but
13891 not be unhappy if we don't get as much as we ask for. */
13894 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
13896 struct remote_state
*rs
= get_remote_state ();
13901 p
= rs
->buf
.data ();
13902 strcpy (p
, "qTBuffer:");
13904 p
+= hexnumstr (p
, offset
);
13906 p
+= hexnumstr (p
, len
);
13910 reply
= remote_get_noisy_reply ();
13911 if (reply
&& *reply
)
13913 /* 'l' by itself means we're at the end of the buffer and
13914 there is nothing more to get. */
13918 /* Convert the reply into binary. Limit the number of bytes to
13919 convert according to our passed-in buffer size, rather than
13920 what was returned in the packet; if the target is
13921 unexpectedly generous and gives us a bigger reply than we
13922 asked for, we don't want to crash. */
13923 rslt
= hex2bin (reply
, buf
, len
);
13927 /* Something went wrong, flag as an error. */
13932 remote_target::set_disconnected_tracing (int val
)
13934 struct remote_state
*rs
= get_remote_state ();
13936 if (m_features
.packet_support (PACKET_DisconnectedTracing_feature
)
13941 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13942 "QTDisconnected:%x", val
);
13944 reply
= remote_get_noisy_reply ();
13945 if (*reply
== '\0')
13946 error (_("Target does not support this command."));
13947 if (strcmp (reply
, "OK") != 0)
13948 error (_("Bogus reply from target: %s"), reply
);
13951 warning (_("Target does not support disconnected tracing."));
13955 remote_target::core_of_thread (ptid_t ptid
)
13957 thread_info
*info
= this->find_thread (ptid
);
13959 if (info
!= NULL
&& info
->priv
!= NULL
)
13960 return get_remote_thread_info (info
)->core
;
13966 remote_target::set_circular_trace_buffer (int val
)
13968 struct remote_state
*rs
= get_remote_state ();
13971 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13972 "QTBuffer:circular:%x", val
);
13974 reply
= remote_get_noisy_reply ();
13975 if (*reply
== '\0')
13976 error (_("Target does not support this command."));
13977 if (strcmp (reply
, "OK") != 0)
13978 error (_("Bogus reply from target: %s"), reply
);
13982 remote_target::traceframe_info ()
13984 gdb::optional
<gdb::char_vector
> text
13985 = target_read_stralloc (current_inferior ()->top_target (),
13986 TARGET_OBJECT_TRACEFRAME_INFO
,
13989 return parse_traceframe_info (text
->data ());
13994 /* Handle the qTMinFTPILen packet. Returns the minimum length of
13995 instruction on which a fast tracepoint may be placed. Returns -1
13996 if the packet is not supported, and 0 if the minimum instruction
13997 length is unknown. */
14000 remote_target::get_min_fast_tracepoint_insn_len ()
14002 struct remote_state
*rs
= get_remote_state ();
14005 /* If we're not debugging a process yet, the IPA can't be
14007 if (!target_has_execution ())
14010 /* Make sure the remote is pointing at the right process. */
14011 set_general_process ();
14013 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
14015 reply
= remote_get_noisy_reply ();
14016 if (*reply
== '\0')
14020 ULONGEST min_insn_len
;
14022 unpack_varlen_hex (reply
, &min_insn_len
);
14024 return (int) min_insn_len
;
14029 remote_target::set_trace_buffer_size (LONGEST val
)
14031 if (m_features
.packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
14033 struct remote_state
*rs
= get_remote_state ();
14034 char *buf
= rs
->buf
.data ();
14035 char *endbuf
= buf
+ get_remote_packet_size ();
14036 enum packet_result result
;
14038 gdb_assert (val
>= 0 || val
== -1);
14039 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
14040 /* Send -1 as literal "-1" to avoid host size dependency. */
14044 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
14047 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
14050 remote_get_noisy_reply ();
14051 result
= m_features
.packet_ok (rs
->buf
, PACKET_QTBuffer_size
);
14053 if (result
!= PACKET_OK
)
14054 warning (_("Bogus reply from target: %s"), rs
->buf
.data ());
14059 remote_target::set_trace_notes (const char *user
, const char *notes
,
14060 const char *stop_notes
)
14062 struct remote_state
*rs
= get_remote_state ();
14064 char *buf
= rs
->buf
.data ();
14065 char *endbuf
= buf
+ get_remote_packet_size ();
14068 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
14071 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
14072 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
14078 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
14079 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
14085 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
14086 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
14090 /* Ensure the buffer is terminated. */
14094 reply
= remote_get_noisy_reply ();
14095 if (*reply
== '\0')
14098 if (strcmp (reply
, "OK") != 0)
14099 error (_("Bogus reply from target: %s"), reply
);
14105 remote_target::use_agent (bool use
)
14107 if (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
14109 struct remote_state
*rs
= get_remote_state ();
14111 /* If the stub supports QAgent. */
14112 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
14114 getpkt (&rs
->buf
, 0);
14116 if (strcmp (rs
->buf
.data (), "OK") == 0)
14127 remote_target::can_use_agent ()
14129 return (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
14132 struct btrace_target_info
14134 /* The ptid of the traced thread. */
14137 /* The obtained branch trace configuration. */
14138 struct btrace_config conf
;
14141 /* Reset our idea of our target's btrace configuration. */
14144 remote_btrace_reset (remote_state
*rs
)
14146 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14149 /* Synchronize the configuration with the target. */
14152 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14154 struct remote_state
*rs
;
14155 char *buf
, *pos
, *endbuf
;
14157 rs
= get_remote_state ();
14158 buf
= rs
->buf
.data ();
14159 endbuf
= buf
+ get_remote_packet_size ();
14161 if (m_features
.packet_support (PACKET_Qbtrace_conf_bts_size
) == PACKET_ENABLE
14162 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14165 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14166 packets_descriptions
[PACKET_Qbtrace_conf_bts_size
].name
,
14170 getpkt (&rs
->buf
, 0);
14172 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_bts_size
)
14175 if (buf
[0] == 'E' && buf
[1] == '.')
14176 error (_("Failed to configure the BTS buffer size: %s"), buf
+ 2);
14178 error (_("Failed to configure the BTS buffer size."));
14181 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14184 if (m_features
.packet_support (PACKET_Qbtrace_conf_pt_size
) == PACKET_ENABLE
14185 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14188 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14189 packets_descriptions
[PACKET_Qbtrace_conf_pt_size
].name
,
14193 getpkt (&rs
->buf
, 0);
14195 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_size
)
14198 if (buf
[0] == 'E' && buf
[1] == '.')
14199 error (_("Failed to configure the trace buffer size: %s"), buf
+ 2);
14201 error (_("Failed to configure the trace buffer size."));
14204 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14208 /* Read TP's btrace configuration from the target and store it into CONF. */
14211 btrace_read_config (thread_info
*tp
, struct btrace_config
*conf
)
14213 /* target_read_stralloc relies on INFERIOR_PTID. */
14214 scoped_restore_current_thread restore_thread
;
14215 switch_to_thread (tp
);
14217 gdb::optional
<gdb::char_vector
> xml
14218 = target_read_stralloc (current_inferior ()->top_target (),
14219 TARGET_OBJECT_BTRACE_CONF
, "");
14221 parse_xml_btrace_conf (conf
, xml
->data ());
14224 /* Maybe reopen target btrace. */
14227 remote_target::remote_btrace_maybe_reopen ()
14229 struct remote_state
*rs
= get_remote_state ();
14230 int btrace_target_pushed
= 0;
14231 #if !defined (HAVE_LIBIPT)
14235 /* Don't bother walking the entirety of the remote thread list when
14236 we know the feature isn't supported by the remote. */
14237 if (m_features
.packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
14240 for (thread_info
*tp
: all_non_exited_threads (this))
14242 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
14243 btrace_read_config (tp
, &rs
->btrace_config
);
14245 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
14248 #if !defined (HAVE_LIBIPT)
14249 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
14254 warning (_("Target is recording using Intel Processor Trace "
14255 "but support was disabled at compile time."));
14260 #endif /* !defined (HAVE_LIBIPT) */
14262 /* Push target, once, but before anything else happens. This way our
14263 changes to the threads will be cleaned up by unpushing the target
14264 in case btrace_read_config () throws. */
14265 if (!btrace_target_pushed
)
14267 btrace_target_pushed
= 1;
14268 record_btrace_push_target ();
14269 gdb_printf (_("Target is recording using %s.\n"),
14270 btrace_format_string (rs
->btrace_config
.format
));
14273 tp
->btrace
.target
= XCNEW (struct btrace_target_info
);
14274 tp
->btrace
.target
->ptid
= tp
->ptid
;
14275 tp
->btrace
.target
->conf
= rs
->btrace_config
;
14279 /* Enable branch tracing. */
14281 struct btrace_target_info
*
14282 remote_target::enable_btrace (thread_info
*tp
,
14283 const struct btrace_config
*conf
)
14285 struct btrace_target_info
*tinfo
= NULL
;
14286 struct packet_config
*packet
= NULL
;
14287 struct remote_state
*rs
= get_remote_state ();
14288 char *buf
= rs
->buf
.data ();
14289 char *endbuf
= buf
+ get_remote_packet_size ();
14291 unsigned int which_packet
;
14292 switch (conf
->format
)
14294 case BTRACE_FORMAT_BTS
:
14295 which_packet
= PACKET_Qbtrace_bts
;
14297 case BTRACE_FORMAT_PT
:
14298 which_packet
= PACKET_Qbtrace_pt
;
14301 internal_error (_("Bad branch btrace format: %u."),
14302 (unsigned int) conf
->format
);
14305 packet
= &m_features
.m_protocol_packets
[which_packet
];
14306 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
14307 error (_("Target does not support branch tracing."));
14309 btrace_sync_conf (conf
);
14311 ptid_t ptid
= tp
->ptid
;
14312 set_general_thread (ptid
);
14314 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14315 packets_descriptions
[which_packet
].name
);
14317 getpkt (&rs
->buf
, 0);
14319 if (m_features
.packet_ok (rs
->buf
, which_packet
) == PACKET_ERROR
)
14321 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14322 error (_("Could not enable branch tracing for %s: %s"),
14323 target_pid_to_str (ptid
).c_str (), &rs
->buf
[2]);
14325 error (_("Could not enable branch tracing for %s."),
14326 target_pid_to_str (ptid
).c_str ());
14329 tinfo
= XCNEW (struct btrace_target_info
);
14330 tinfo
->ptid
= ptid
;
14332 /* If we fail to read the configuration, we lose some information, but the
14333 tracing itself is not impacted. */
14336 btrace_read_config (tp
, &tinfo
->conf
);
14338 catch (const gdb_exception_error
&err
)
14340 if (err
.message
!= NULL
)
14341 warning ("%s", err
.what ());
14347 /* Disable branch tracing. */
14350 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
14352 struct remote_state
*rs
= get_remote_state ();
14353 char *buf
= rs
->buf
.data ();
14354 char *endbuf
= buf
+ get_remote_packet_size ();
14356 if (m_features
.packet_support (PACKET_Qbtrace_off
) != PACKET_ENABLE
)
14357 error (_("Target does not support branch tracing."));
14359 set_general_thread (tinfo
->ptid
);
14361 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14362 packets_descriptions
[PACKET_Qbtrace_off
].name
);
14364 getpkt (&rs
->buf
, 0);
14366 if (m_features
.packet_ok (rs
->buf
, PACKET_Qbtrace_off
) == PACKET_ERROR
)
14368 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14369 error (_("Could not disable branch tracing for %s: %s"),
14370 target_pid_to_str (tinfo
->ptid
).c_str (), &rs
->buf
[2]);
14372 error (_("Could not disable branch tracing for %s."),
14373 target_pid_to_str (tinfo
->ptid
).c_str ());
14379 /* Teardown branch tracing. */
14382 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
14384 /* We must not talk to the target during teardown. */
14388 /* Read the branch trace. */
14391 remote_target::read_btrace (struct btrace_data
*btrace
,
14392 struct btrace_target_info
*tinfo
,
14393 enum btrace_read_type type
)
14397 if (m_features
.packet_support (PACKET_qXfer_btrace
) != PACKET_ENABLE
)
14398 error (_("Target does not support branch tracing."));
14400 #if !defined(HAVE_LIBEXPAT)
14401 error (_("Cannot process branch tracing result. XML parsing not supported."));
14406 case BTRACE_READ_ALL
:
14409 case BTRACE_READ_NEW
:
14412 case BTRACE_READ_DELTA
:
14416 internal_error (_("Bad branch tracing read type: %u."),
14417 (unsigned int) type
);
14420 gdb::optional
<gdb::char_vector
> xml
14421 = target_read_stralloc (current_inferior ()->top_target (),
14422 TARGET_OBJECT_BTRACE
, annex
);
14424 return BTRACE_ERR_UNKNOWN
;
14426 parse_xml_btrace (btrace
, xml
->data ());
14428 return BTRACE_ERR_NONE
;
14431 const struct btrace_config
*
14432 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
14434 return &tinfo
->conf
;
14438 remote_target::augmented_libraries_svr4_read ()
14441 (m_features
.packet_support (PACKET_augmented_libraries_svr4_read_feature
)
14445 /* Implementation of to_load. */
14448 remote_target::load (const char *name
, int from_tty
)
14450 generic_load (name
, from_tty
);
14453 /* Accepts an integer PID; returns a string representing a file that
14454 can be opened on the remote side to get the symbols for the child
14455 process. Returns NULL if the operation is not supported. */
14458 remote_target::pid_to_exec_file (int pid
)
14460 static gdb::optional
<gdb::char_vector
> filename
;
14461 char *annex
= NULL
;
14463 if (m_features
.packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
14466 inferior
*inf
= find_inferior_pid (this, pid
);
14468 internal_error (_("not currently attached to process %d"), pid
);
14470 if (!inf
->fake_pid_p
)
14472 const int annex_size
= 9;
14474 annex
= (char *) alloca (annex_size
);
14475 xsnprintf (annex
, annex_size
, "%x", pid
);
14478 filename
= target_read_stralloc (current_inferior ()->top_target (),
14479 TARGET_OBJECT_EXEC_FILE
, annex
);
14481 return filename
? filename
->data () : nullptr;
14484 /* Implement the to_can_do_single_step target_ops method. */
14487 remote_target::can_do_single_step ()
14489 /* We can only tell whether target supports single step or not by
14490 supported s and S vCont actions if the stub supports vContSupported
14491 feature. If the stub doesn't support vContSupported feature,
14492 we have conservatively to think target doesn't supports single
14494 if (m_features
.packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
14496 struct remote_state
*rs
= get_remote_state ();
14498 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
14504 /* Implementation of the to_execution_direction method for the remote
14507 enum exec_direction_kind
14508 remote_target::execution_direction ()
14510 struct remote_state
*rs
= get_remote_state ();
14512 return rs
->last_resume_exec_dir
;
14515 /* Return pointer to the thread_info struct which corresponds to
14516 THREAD_HANDLE (having length HANDLE_LEN). */
14519 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
14523 for (thread_info
*tp
: all_non_exited_threads (this))
14525 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14527 if (tp
->inf
== inf
&& priv
!= NULL
)
14529 if (handle_len
!= priv
->thread_handle
.size ())
14530 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
14531 handle_len
, priv
->thread_handle
.size ());
14532 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
14541 gdb::array_view
<const gdb_byte
>
14542 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
14544 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14545 return priv
->thread_handle
;
14549 remote_target::can_async_p ()
14551 /* This flag should be checked in the common target.c code. */
14552 gdb_assert (target_async_permitted
);
14554 /* We're async whenever the serial device can. */
14555 struct remote_state
*rs
= get_remote_state ();
14556 return serial_can_async_p (rs
->remote_desc
);
14560 remote_target::is_async_p ()
14562 /* We're async whenever the serial device is. */
14563 struct remote_state
*rs
= get_remote_state ();
14564 return serial_is_async_p (rs
->remote_desc
);
14567 /* Pass the SERIAL event on and up to the client. One day this code
14568 will be able to delay notifying the client of an event until the
14569 point where an entire packet has been received. */
14571 static serial_event_ftype remote_async_serial_handler
;
14574 remote_async_serial_handler (struct serial
*scb
, void *context
)
14576 /* Don't propogate error information up to the client. Instead let
14577 the client find out about the error by querying the target. */
14578 inferior_event_handler (INF_REG_EVENT
);
14582 remote_async_inferior_event_handler (gdb_client_data data
)
14584 inferior_event_handler (INF_REG_EVENT
);
14588 remote_target::async_wait_fd ()
14590 struct remote_state
*rs
= get_remote_state ();
14591 return rs
->remote_desc
->fd
;
14595 remote_target::async (bool enable
)
14597 struct remote_state
*rs
= get_remote_state ();
14601 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
14603 /* If there are pending events in the stop reply queue tell the
14604 event loop to process them. */
14605 if (!rs
->stop_reply_queue
.empty ())
14606 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
14607 /* For simplicity, below we clear the pending events token
14608 without remembering whether it is marked, so here we always
14609 mark it. If there's actually no pending notification to
14610 process, this ends up being a no-op (other than a spurious
14611 event-loop wakeup). */
14612 if (target_is_non_stop_p ())
14613 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14617 serial_async (rs
->remote_desc
, NULL
, NULL
);
14618 /* If the core is disabling async, it doesn't want to be
14619 disturbed with target events. Clear all async event sources
14621 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
14622 if (target_is_non_stop_p ())
14623 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14627 /* Implementation of the to_thread_events method. */
14630 remote_target::thread_events (int enable
)
14632 struct remote_state
*rs
= get_remote_state ();
14633 size_t size
= get_remote_packet_size ();
14635 if (m_features
.packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
14638 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
14640 getpkt (&rs
->buf
, 0);
14642 switch (m_features
.packet_ok (rs
->buf
, PACKET_QThreadEvents
))
14645 if (strcmp (rs
->buf
.data (), "OK") != 0)
14646 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
14649 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
14651 case PACKET_UNKNOWN
:
14657 show_remote_cmd (const char *args
, int from_tty
)
14659 /* We can't just use cmd_show_list here, because we want to skip
14660 the redundant "show remote Z-packet" and the legacy aliases. */
14661 struct cmd_list_element
*list
= remote_show_cmdlist
;
14662 struct ui_out
*uiout
= current_uiout
;
14664 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
14665 for (; list
!= NULL
; list
= list
->next
)
14666 if (strcmp (list
->name
, "Z-packet") == 0)
14668 else if (list
->type
== not_set_cmd
)
14669 /* Alias commands are exactly like the original, except they
14670 don't have the normal type. */
14674 ui_out_emit_tuple
option_emitter (uiout
, "option");
14676 uiout
->field_string ("name", list
->name
);
14677 uiout
->text (": ");
14678 if (list
->type
== show_cmd
)
14679 do_show_command (NULL
, from_tty
, list
);
14681 cmd_func (list
, NULL
, from_tty
);
14686 /* Function to be called whenever a new objfile (shlib) is detected. */
14688 remote_new_objfile (struct objfile
*objfile
)
14690 /* The objfile change happened in that program space. */
14691 program_space
*pspace
= current_program_space
;
14693 /* The affected program space is possibly shared by multiple inferiors.
14694 Consider sending a qSymbol packet for each of the inferiors using that
14696 for (inferior
*inf
: all_inferiors ())
14698 if (inf
->pspace
!= pspace
)
14701 /* Check whether the inferior's process target is a remote target. */
14702 remote_target
*remote
= as_remote_target (inf
->process_target ());
14703 if (remote
== nullptr)
14706 /* When we are attaching or handling a fork child and the shared library
14707 subsystem reads the list of loaded libraries, we receive new objfile
14708 events in between each found library. The libraries are read in an
14709 undefined order, so if we gave the remote side a chance to look up
14710 symbols between each objfile, we might give it an inconsistent picture
14711 of the inferior. It could appear that a library A appears loaded but
14712 a library B does not, even though library A requires library B. That
14713 would present a state that couldn't normally exist in the inferior.
14715 So, skip these events, we'll give the remote a chance to look up
14716 symbols once all the loaded libraries and their symbols are known to
14718 if (inf
->in_initial_library_scan
)
14721 if (!remote
->has_execution (inf
))
14724 /* Need to switch to a specific thread, because remote_check_symbols will
14725 set the general thread using INFERIOR_PTID.
14727 It's possible to have inferiors with no thread here, because we are
14728 called very early in the connection process, while the inferior is
14729 being set up, before threads are added. Just skip it, start_remote_1
14730 also calls remote_check_symbols when it's done setting things up. */
14731 thread_info
*thread
= any_thread_of_inferior (inf
);
14732 if (thread
!= nullptr)
14734 scoped_restore_current_thread restore_thread
;
14735 switch_to_thread (thread
);
14736 remote
->remote_check_symbols ();
14741 /* Pull all the tracepoints defined on the target and create local
14742 data structures representing them. We don't want to create real
14743 tracepoints yet, we don't want to mess up the user's existing
14747 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
14749 struct remote_state
*rs
= get_remote_state ();
14752 /* Ask for a first packet of tracepoint definition. */
14754 getpkt (&rs
->buf
, 0);
14755 p
= rs
->buf
.data ();
14756 while (*p
&& *p
!= 'l')
14758 parse_tracepoint_definition (p
, utpp
);
14759 /* Ask for another packet of tracepoint definition. */
14761 getpkt (&rs
->buf
, 0);
14762 p
= rs
->buf
.data ();
14768 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
14770 struct remote_state
*rs
= get_remote_state ();
14773 /* Ask for a first packet of variable definition. */
14775 getpkt (&rs
->buf
, 0);
14776 p
= rs
->buf
.data ();
14777 while (*p
&& *p
!= 'l')
14779 parse_tsv_definition (p
, utsvp
);
14780 /* Ask for another packet of variable definition. */
14782 getpkt (&rs
->buf
, 0);
14783 p
= rs
->buf
.data ();
14788 /* The "set/show range-stepping" show hook. */
14791 show_range_stepping (struct ui_file
*file
, int from_tty
,
14792 struct cmd_list_element
*c
,
14796 _("Debugger's willingness to use range stepping "
14797 "is %s.\n"), value
);
14800 /* Return true if the vCont;r action is supported by the remote
14804 remote_target::vcont_r_supported ()
14806 return (m_features
.packet_support (PACKET_vCont
) == PACKET_ENABLE
14807 && get_remote_state ()->supports_vCont
.r
);
14810 /* The "set/show range-stepping" set hook. */
14813 set_range_stepping (const char *ignore_args
, int from_tty
,
14814 struct cmd_list_element
*c
)
14816 /* When enabling, check whether range stepping is actually supported
14817 by the target, and warn if not. */
14818 if (use_range_stepping
)
14820 remote_target
*remote
= get_current_remote_target ();
14822 || !remote
->vcont_r_supported ())
14823 warning (_("Range stepping is not supported by the current target"));
14828 show_remote_debug (struct ui_file
*file
, int from_tty
,
14829 struct cmd_list_element
*c
, const char *value
)
14831 gdb_printf (file
, _("Debugging of remote protocol is %s.\n"),
14836 show_remote_timeout (struct ui_file
*file
, int from_tty
,
14837 struct cmd_list_element
*c
, const char *value
)
14840 _("Timeout limit to wait for target to respond is %s.\n"),
14844 /* Implement the "supports_memory_tagging" target_ops method. */
14847 remote_target::supports_memory_tagging ()
14849 return m_features
.remote_memory_tagging_p ();
14852 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
14855 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14856 size_t len
, int type
)
14858 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14860 std::string request
= string_printf ("qMemTags:%s,%s:%s",
14861 phex_nz (address
, addr_size
),
14862 phex_nz (len
, sizeof (len
)),
14863 phex_nz (type
, sizeof (type
)));
14865 strcpy (packet
.data (), request
.c_str ());
14868 /* Parse the qMemTags packet reply into TAGS.
14870 Return true if successful, false otherwise. */
14873 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
14874 gdb::byte_vector
&tags
)
14876 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
14879 /* Copy the tag data. */
14880 tags
= hex2bin (reply
.data () + 1);
14885 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
14888 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14889 size_t len
, int type
,
14890 const gdb::byte_vector
&tags
)
14892 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14894 /* Put together the main packet, address and length. */
14895 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
14896 phex_nz (address
, addr_size
),
14897 phex_nz (len
, sizeof (len
)),
14898 phex_nz (type
, sizeof (type
)));
14899 request
+= bin2hex (tags
.data (), tags
.size ());
14901 /* Check if we have exceeded the maximum packet size. */
14902 if (packet
.size () < request
.length ())
14903 error (_("Contents too big for packet QMemTags."));
14905 strcpy (packet
.data (), request
.c_str ());
14908 /* Implement the "fetch_memtags" target_ops method. */
14911 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
14912 gdb::byte_vector
&tags
, int type
)
14914 /* Make sure the qMemTags packet is supported. */
14915 if (!m_features
.remote_memory_tagging_p ())
14916 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
14918 struct remote_state
*rs
= get_remote_state ();
14920 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
14923 getpkt (&rs
->buf
, 0);
14925 return parse_fetch_memtags_reply (rs
->buf
, tags
);
14928 /* Implement the "store_memtags" target_ops method. */
14931 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
14932 const gdb::byte_vector
&tags
, int type
)
14934 /* Make sure the QMemTags packet is supported. */
14935 if (!m_features
.remote_memory_tagging_p ())
14936 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
14938 struct remote_state
*rs
= get_remote_state ();
14940 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
14943 getpkt (&rs
->buf
, 0);
14945 /* Verify if the request was successful. */
14946 return packet_check_result (rs
->buf
.data ()) == PACKET_OK
;
14949 /* Return true if remote target T is non-stop. */
14952 remote_target_is_non_stop_p (remote_target
*t
)
14954 scoped_restore_current_thread restore_thread
;
14955 switch_to_target_no_thread (t
);
14957 return target_is_non_stop_p ();
14962 namespace selftests
{
14965 test_memory_tagging_functions ()
14967 remote_target remote
;
14969 struct packet_config
*config
14970 = &remote
.m_features
.m_protocol_packets
[PACKET_memory_tagging_feature
];
14972 scoped_restore restore_memtag_support_
14973 = make_scoped_restore (&config
->support
);
14975 /* Test memory tagging packet support. */
14976 config
->support
= PACKET_SUPPORT_UNKNOWN
;
14977 SELF_CHECK (remote
.supports_memory_tagging () == false);
14978 config
->support
= PACKET_DISABLE
;
14979 SELF_CHECK (remote
.supports_memory_tagging () == false);
14980 config
->support
= PACKET_ENABLE
;
14981 SELF_CHECK (remote
.supports_memory_tagging () == true);
14983 /* Setup testing. */
14984 gdb::char_vector packet
;
14985 gdb::byte_vector tags
, bv
;
14986 std::string expected
, reply
;
14987 packet
.resize (32000);
14989 /* Test creating a qMemTags request. */
14991 expected
= "qMemTags:0,0:0";
14992 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
14993 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14995 expected
= "qMemTags:deadbeef,10:1";
14996 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
14997 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14999 /* Test parsing a qMemTags reply. */
15001 /* Error reply, tags vector unmodified. */
15003 strcpy (packet
.data (), reply
.c_str ());
15005 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
15006 SELF_CHECK (tags
.size () == 0);
15008 /* Valid reply, tags vector updated. */
15012 for (int i
= 0; i
< 5; i
++)
15015 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
15016 strcpy (packet
.data (), reply
.c_str ());
15018 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
15019 SELF_CHECK (tags
.size () == 5);
15021 for (int i
= 0; i
< 5; i
++)
15022 SELF_CHECK (tags
[i
] == i
);
15024 /* Test creating a QMemTags request. */
15026 /* Empty tag data. */
15028 expected
= "QMemTags:0,0:0:";
15029 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
15030 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15031 expected
.length ()) == 0);
15033 /* Non-empty tag data. */
15035 for (int i
= 0; i
< 5; i
++)
15036 tags
.push_back (i
);
15037 expected
= "QMemTags:deadbeef,ff:1:0001020304";
15038 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
15039 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15040 expected
.length ()) == 0);
15043 } // namespace selftests
15044 #endif /* GDB_SELF_TEST */
15046 void _initialize_remote ();
15048 _initialize_remote ()
15050 add_target (remote_target_info
, remote_target::open
);
15051 add_target (extended_remote_target_info
, extended_remote_target::open
);
15053 /* Hook into new objfile notification. */
15054 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
15057 init_remote_threadtests ();
15060 /* set/show remote ... */
15062 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
15063 Remote protocol specific variables.\n\
15064 Configure various remote-protocol specific variables such as\n\
15065 the packets being used."),
15066 &remote_set_cmdlist
,
15067 0 /* allow-unknown */, &setlist
);
15068 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
15069 Remote protocol specific variables.\n\
15070 Configure various remote-protocol specific variables such as\n\
15071 the packets being used."),
15072 &remote_show_cmdlist
,
15073 0 /* allow-unknown */, &showlist
);
15075 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
15076 Compare section data on target to the exec file.\n\
15077 Argument is a single section name (default: all loaded sections).\n\
15078 To compare only read-only loaded sections, specify the -r option."),
15081 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
15082 Send an arbitrary packet to a remote target.\n\
15083 maintenance packet TEXT\n\
15084 If GDB is talking to an inferior via the GDB serial protocol, then\n\
15085 this command sends the string TEXT to the inferior, and displays the\n\
15086 response packet. GDB supplies the initial `$' character, and the\n\
15087 terminating `#' character and checksum."),
15090 set_show_commands remotebreak_cmds
15091 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
15092 Set whether to send break if interrupted."), _("\
15093 Show whether to send break if interrupted."), _("\
15094 If set, a break, instead of a cntrl-c, is sent to the remote target."),
15095 set_remotebreak
, show_remotebreak
,
15096 &setlist
, &showlist
);
15097 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
15098 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
15100 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
15101 interrupt_sequence_modes
, &interrupt_sequence_mode
,
15103 Set interrupt sequence to remote target."), _("\
15104 Show interrupt sequence to remote target."), _("\
15105 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
15106 NULL
, show_interrupt_sequence
,
15107 &remote_set_cmdlist
,
15108 &remote_show_cmdlist
);
15110 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
15111 &interrupt_on_connect
, _("\
15112 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15113 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15114 If set, interrupt sequence is sent to remote target."),
15116 &remote_set_cmdlist
, &remote_show_cmdlist
);
15118 /* Install commands for configuring memory read/write packets. */
15120 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
15121 Set the maximum number of bytes per memory write packet (deprecated)."),
15123 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
15124 Show the maximum number of bytes per memory write packet (deprecated)."),
15126 add_cmd ("memory-write-packet-size", no_class
,
15127 set_memory_write_packet_size
, _("\
15128 Set the maximum number of bytes per memory-write packet.\n\
15129 Specify the number of bytes in a packet or 0 (zero) for the\n\
15130 default packet size. The actual limit is further reduced\n\
15131 dependent on the target. Specify \"fixed\" to disable the\n\
15132 further restriction and \"limit\" to enable that restriction."),
15133 &remote_set_cmdlist
);
15134 add_cmd ("memory-read-packet-size", no_class
,
15135 set_memory_read_packet_size
, _("\
15136 Set the maximum number of bytes per memory-read packet.\n\
15137 Specify the number of bytes in a packet or 0 (zero) for the\n\
15138 default packet size. The actual limit is further reduced\n\
15139 dependent on the target. Specify \"fixed\" to disable the\n\
15140 further restriction and \"limit\" to enable that restriction."),
15141 &remote_set_cmdlist
);
15142 add_cmd ("memory-write-packet-size", no_class
,
15143 show_memory_write_packet_size
,
15144 _("Show the maximum number of bytes per memory-write packet."),
15145 &remote_show_cmdlist
);
15146 add_cmd ("memory-read-packet-size", no_class
,
15147 show_memory_read_packet_size
,
15148 _("Show the maximum number of bytes per memory-read packet."),
15149 &remote_show_cmdlist
);
15151 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
15152 &remote_hw_watchpoint_limit
, _("\
15153 Set the maximum number of target hardware watchpoints."), _("\
15154 Show the maximum number of target hardware watchpoints."), _("\
15155 Specify \"unlimited\" for unlimited hardware watchpoints."),
15156 NULL
, show_hardware_watchpoint_limit
,
15157 &remote_set_cmdlist
,
15158 &remote_show_cmdlist
);
15159 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
15161 &remote_hw_watchpoint_length_limit
, _("\
15162 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
15163 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
15164 Specify \"unlimited\" to allow watchpoints of unlimited size."),
15165 NULL
, show_hardware_watchpoint_length_limit
,
15166 &remote_set_cmdlist
, &remote_show_cmdlist
);
15167 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
15168 &remote_hw_breakpoint_limit
, _("\
15169 Set the maximum number of target hardware breakpoints."), _("\
15170 Show the maximum number of target hardware breakpoints."), _("\
15171 Specify \"unlimited\" for unlimited hardware breakpoints."),
15172 NULL
, show_hardware_breakpoint_limit
,
15173 &remote_set_cmdlist
, &remote_show_cmdlist
);
15175 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
15176 &remote_address_size
, _("\
15177 Set the maximum size of the address (in bits) in a memory packet."), _("\
15178 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
15180 NULL
, /* FIXME: i18n: */
15181 &setlist
, &showlist
);
15183 init_all_packet_configs ();
15185 add_packet_config_cmd (PACKET_X
, "X", "binary-download", 1);
15187 add_packet_config_cmd (PACKET_vCont
, "vCont", "verbose-resume", 0);
15189 add_packet_config_cmd (PACKET_QPassSignals
, "QPassSignals", "pass-signals",
15192 add_packet_config_cmd (PACKET_QCatchSyscalls
, "QCatchSyscalls",
15193 "catch-syscalls", 0);
15195 add_packet_config_cmd (PACKET_QProgramSignals
, "QProgramSignals",
15196 "program-signals", 0);
15198 add_packet_config_cmd (PACKET_QSetWorkingDir
, "QSetWorkingDir",
15199 "set-working-dir", 0);
15201 add_packet_config_cmd (PACKET_QStartupWithShell
, "QStartupWithShell",
15202 "startup-with-shell", 0);
15204 add_packet_config_cmd (PACKET_QEnvironmentHexEncoded
,"QEnvironmentHexEncoded",
15205 "environment-hex-encoded", 0);
15207 add_packet_config_cmd (PACKET_QEnvironmentReset
, "QEnvironmentReset",
15208 "environment-reset", 0);
15210 add_packet_config_cmd (PACKET_QEnvironmentUnset
, "QEnvironmentUnset",
15211 "environment-unset", 0);
15213 add_packet_config_cmd (PACKET_qSymbol
, "qSymbol", "symbol-lookup", 0);
15215 add_packet_config_cmd (PACKET_P
, "P", "set-register", 1);
15217 add_packet_config_cmd (PACKET_p
, "p", "fetch-register", 1);
15219 add_packet_config_cmd (PACKET_Z0
, "Z0", "software-breakpoint", 0);
15221 add_packet_config_cmd (PACKET_Z1
, "Z1", "hardware-breakpoint", 0);
15223 add_packet_config_cmd (PACKET_Z2
, "Z2", "write-watchpoint", 0);
15225 add_packet_config_cmd (PACKET_Z3
, "Z3", "read-watchpoint", 0);
15227 add_packet_config_cmd (PACKET_Z4
, "Z4", "access-watchpoint", 0);
15229 add_packet_config_cmd (PACKET_qXfer_auxv
, "qXfer:auxv:read",
15230 "read-aux-vector", 0);
15232 add_packet_config_cmd (PACKET_qXfer_exec_file
, "qXfer:exec-file:read",
15233 "pid-to-exec-file", 0);
15235 add_packet_config_cmd (PACKET_qXfer_features
,
15236 "qXfer:features:read", "target-features", 0);
15238 add_packet_config_cmd (PACKET_qXfer_libraries
, "qXfer:libraries:read",
15239 "library-info", 0);
15241 add_packet_config_cmd (PACKET_qXfer_libraries_svr4
,
15242 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
15244 add_packet_config_cmd (PACKET_qXfer_memory_map
, "qXfer:memory-map:read",
15247 add_packet_config_cmd (PACKET_qXfer_osdata
, "qXfer:osdata:read", "osdata", 0);
15249 add_packet_config_cmd (PACKET_qXfer_threads
, "qXfer:threads:read", "threads",
15252 add_packet_config_cmd (PACKET_qXfer_siginfo_read
, "qXfer:siginfo:read",
15253 "read-siginfo-object", 0);
15255 add_packet_config_cmd (PACKET_qXfer_siginfo_write
, "qXfer:siginfo:write",
15256 "write-siginfo-object", 0);
15258 add_packet_config_cmd (PACKET_qXfer_traceframe_info
,
15259 "qXfer:traceframe-info:read", "traceframe-info", 0);
15261 add_packet_config_cmd (PACKET_qXfer_uib
, "qXfer:uib:read",
15262 "unwind-info-block", 0);
15264 add_packet_config_cmd (PACKET_qGetTLSAddr
, "qGetTLSAddr",
15265 "get-thread-local-storage-address", 0);
15267 add_packet_config_cmd (PACKET_qGetTIBAddr
, "qGetTIBAddr",
15268 "get-thread-information-block-address", 0);
15270 add_packet_config_cmd (PACKET_bc
, "bc", "reverse-continue", 0);
15272 add_packet_config_cmd (PACKET_bs
, "bs", "reverse-step", 0);
15274 add_packet_config_cmd (PACKET_qSupported
, "qSupported", "supported-packets",
15277 add_packet_config_cmd (PACKET_qSearch_memory
, "qSearch:memory",
15278 "search-memory", 0);
15280 add_packet_config_cmd (PACKET_qTStatus
, "qTStatus", "trace-status", 0);
15282 add_packet_config_cmd (PACKET_vFile_setfs
, "vFile:setfs", "hostio-setfs", 0);
15284 add_packet_config_cmd (PACKET_vFile_open
, "vFile:open", "hostio-open", 0);
15286 add_packet_config_cmd (PACKET_vFile_pread
, "vFile:pread", "hostio-pread", 0);
15288 add_packet_config_cmd (PACKET_vFile_pwrite
, "vFile:pwrite", "hostio-pwrite",
15291 add_packet_config_cmd (PACKET_vFile_close
, "vFile:close", "hostio-close", 0);
15293 add_packet_config_cmd (PACKET_vFile_unlink
, "vFile:unlink", "hostio-unlink",
15296 add_packet_config_cmd (PACKET_vFile_readlink
, "vFile:readlink",
15297 "hostio-readlink", 0);
15299 add_packet_config_cmd (PACKET_vFile_fstat
, "vFile:fstat", "hostio-fstat", 0);
15301 add_packet_config_cmd (PACKET_vAttach
, "vAttach", "attach", 0);
15303 add_packet_config_cmd (PACKET_vRun
, "vRun", "run", 0);
15305 add_packet_config_cmd (PACKET_QStartNoAckMode
, "QStartNoAckMode", "noack", 0);
15307 add_packet_config_cmd (PACKET_vKill
, "vKill", "kill", 0);
15309 add_packet_config_cmd (PACKET_qAttached
, "qAttached", "query-attached", 0);
15311 add_packet_config_cmd (PACKET_ConditionalTracepoints
,
15312 "ConditionalTracepoints", "conditional-tracepoints",
15315 add_packet_config_cmd (PACKET_ConditionalBreakpoints
,
15316 "ConditionalBreakpoints", "conditional-breakpoints",
15319 add_packet_config_cmd (PACKET_BreakpointCommands
, "BreakpointCommands",
15320 "breakpoint-commands", 0);
15322 add_packet_config_cmd (PACKET_FastTracepoints
, "FastTracepoints",
15323 "fast-tracepoints", 0);
15325 add_packet_config_cmd (PACKET_TracepointSource
, "TracepointSource",
15326 "TracepointSource", 0);
15328 add_packet_config_cmd (PACKET_QAllow
, "QAllow", "allow", 0);
15330 add_packet_config_cmd (PACKET_StaticTracepoints
, "StaticTracepoints",
15331 "static-tracepoints", 0);
15333 add_packet_config_cmd (PACKET_InstallInTrace
, "InstallInTrace",
15334 "install-in-trace", 0);
15336 add_packet_config_cmd (PACKET_qXfer_statictrace_read
,
15337 "qXfer:statictrace:read", "read-sdata-object", 0);
15339 add_packet_config_cmd (PACKET_qXfer_fdpic
, "qXfer:fdpic:read",
15340 "read-fdpic-loadmap", 0);
15342 add_packet_config_cmd (PACKET_QDisableRandomization
, "QDisableRandomization",
15343 "disable-randomization", 0);
15345 add_packet_config_cmd (PACKET_QAgent
, "QAgent", "agent", 0);
15347 add_packet_config_cmd (PACKET_QTBuffer_size
, "QTBuffer:size",
15348 "trace-buffer-size", 0);
15350 add_packet_config_cmd (PACKET_Qbtrace_off
, "Qbtrace:off", "disable-btrace",
15353 add_packet_config_cmd (PACKET_Qbtrace_bts
, "Qbtrace:bts", "enable-btrace-bts",
15356 add_packet_config_cmd (PACKET_Qbtrace_pt
, "Qbtrace:pt", "enable-btrace-pt",
15359 add_packet_config_cmd (PACKET_qXfer_btrace
, "qXfer:btrace", "read-btrace", 0);
15361 add_packet_config_cmd (PACKET_qXfer_btrace_conf
, "qXfer:btrace-conf",
15362 "read-btrace-conf", 0);
15364 add_packet_config_cmd (PACKET_Qbtrace_conf_bts_size
, "Qbtrace-conf:bts:size",
15365 "btrace-conf-bts-size", 0);
15367 add_packet_config_cmd (PACKET_multiprocess_feature
, "multiprocess-feature",
15368 "multiprocess-feature", 0);
15370 add_packet_config_cmd (PACKET_swbreak_feature
, "swbreak-feature",
15371 "swbreak-feature", 0);
15373 add_packet_config_cmd (PACKET_hwbreak_feature
, "hwbreak-feature",
15374 "hwbreak-feature", 0);
15376 add_packet_config_cmd (PACKET_fork_event_feature
, "fork-event-feature",
15377 "fork-event-feature", 0);
15379 add_packet_config_cmd (PACKET_vfork_event_feature
, "vfork-event-feature",
15380 "vfork-event-feature", 0);
15382 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_size
, "Qbtrace-conf:pt:size",
15383 "btrace-conf-pt-size", 0);
15385 add_packet_config_cmd (PACKET_vContSupported
, "vContSupported",
15386 "verbose-resume-supported", 0);
15388 add_packet_config_cmd (PACKET_exec_event_feature
, "exec-event-feature",
15389 "exec-event-feature", 0);
15391 add_packet_config_cmd (PACKET_vCtrlC
, "vCtrlC", "ctrl-c", 0);
15393 add_packet_config_cmd (PACKET_QThreadEvents
, "QThreadEvents", "thread-events",
15396 add_packet_config_cmd (PACKET_no_resumed
, "N stop reply",
15397 "no-resumed-stop-reply", 0);
15399 add_packet_config_cmd (PACKET_memory_tagging_feature
,
15400 "memory-tagging-feature", "memory-tagging-feature", 0);
15402 /* Assert that we've registered "set remote foo-packet" commands
15403 for all packet configs. */
15407 for (i
= 0; i
< PACKET_MAX
; i
++)
15409 /* Ideally all configs would have a command associated. Some
15410 still don't though. */
15415 case PACKET_QNonStop
:
15416 case PACKET_EnableDisableTracepoints_feature
:
15417 case PACKET_tracenz_feature
:
15418 case PACKET_DisconnectedTracing_feature
:
15419 case PACKET_augmented_libraries_svr4_read_feature
:
15421 /* Additions to this list need to be well justified:
15422 pre-existing packets are OK; new packets are not. */
15430 /* This catches both forgetting to add a config command, and
15431 forgetting to remove a packet from the exception list. */
15432 gdb_assert (excepted
== (packets_descriptions
[i
].name
== NULL
));
15436 /* Keep the old ``set remote Z-packet ...'' working. Each individual
15437 Z sub-packet has its own set and show commands, but users may
15438 have sets to this variable in their .gdbinit files (or in their
15440 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
15441 &remote_Z_packet_detect
, _("\
15442 Set use of remote protocol `Z' packets."), _("\
15443 Show use of remote protocol `Z' packets."), _("\
15444 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
15446 set_remote_protocol_Z_packet_cmd
,
15447 show_remote_protocol_Z_packet_cmd
,
15448 /* FIXME: i18n: Use of remote protocol
15449 `Z' packets is %s. */
15450 &remote_set_cmdlist
, &remote_show_cmdlist
);
15452 add_basic_prefix_cmd ("remote", class_files
, _("\
15453 Manipulate files on the remote system.\n\
15454 Transfer files to and from the remote target system."),
15456 0 /* allow-unknown */, &cmdlist
);
15458 add_cmd ("put", class_files
, remote_put_command
,
15459 _("Copy a local file to the remote system."),
15462 add_cmd ("get", class_files
, remote_get_command
,
15463 _("Copy a remote file to the local system."),
15466 add_cmd ("delete", class_files
, remote_delete_command
,
15467 _("Delete a remote file."),
15470 add_setshow_string_noescape_cmd ("exec-file", class_files
,
15471 &remote_exec_file_var
, _("\
15472 Set the remote pathname for \"run\"."), _("\
15473 Show the remote pathname for \"run\"."), NULL
,
15474 set_remote_exec_file
,
15475 show_remote_exec_file
,
15476 &remote_set_cmdlist
,
15477 &remote_show_cmdlist
);
15479 add_setshow_boolean_cmd ("range-stepping", class_run
,
15480 &use_range_stepping
, _("\
15481 Enable or disable range stepping."), _("\
15482 Show whether target-assisted range stepping is enabled."), _("\
15483 If on, and the target supports it, when stepping a source line, GDB\n\
15484 tells the target to step the corresponding range of addresses itself instead\n\
15485 of issuing multiple single-steps. This speeds up source level\n\
15486 stepping. If off, GDB always issues single-steps, even if range\n\
15487 stepping is supported by the target. The default is on."),
15488 set_range_stepping
,
15489 show_range_stepping
,
15493 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
15494 Set watchdog timer."), _("\
15495 Show watchdog timer."), _("\
15496 When non-zero, this timeout is used instead of waiting forever for a target\n\
15497 to finish a low-level step or continue operation. If the specified amount\n\
15498 of time passes without a response from the target, an error occurs."),
15501 &setlist
, &showlist
);
15503 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
15504 &remote_packet_max_chars
, _("\
15505 Set the maximum number of characters to display for each remote packet."), _("\
15506 Show the maximum number of characters to display for each remote packet."), _("\
15507 Specify \"unlimited\" to display all the characters."),
15508 NULL
, show_remote_packet_max_chars
,
15509 &setdebuglist
, &showdebuglist
);
15511 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
15512 _("Set debugging of remote protocol."),
15513 _("Show debugging of remote protocol."),
15515 When enabled, each packet sent or received with the remote target\n\
15519 &setdebuglist
, &showdebuglist
);
15521 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
15522 &remote_timeout
, _("\
15523 Set timeout limit to wait for target to respond."), _("\
15524 Show timeout limit to wait for target to respond."), _("\
15525 This value is used to set the time limit for gdb to wait for a response\n\
15526 from the target."),
15528 show_remote_timeout
,
15529 &setlist
, &showlist
);
15531 /* Eventually initialize fileio. See fileio.c */
15532 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
15535 selftests::register_test ("remote_memory_tagging",
15536 selftests::test_memory_tagging_functions
);