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 (arch, regnum);
374 /* char *name; == gdbarch_register_name (arch, 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
);
416 void create_async_event_handler ()
418 gdb_assert (m_async_event_handler_token
== nullptr);
419 m_async_event_handler_token
420 = ::create_async_event_handler ([] (gdb_client_data data
)
422 inferior_event_handler (INF_REG_EVENT
);
427 void mark_async_event_handler ()
429 gdb_assert (this->is_async_p ());
430 ::mark_async_event_handler (m_async_event_handler_token
);
433 void clear_async_event_handler ()
434 { ::clear_async_event_handler (m_async_event_handler_token
); }
436 bool async_event_handler_marked () const
437 { return ::async_event_handler_marked (m_async_event_handler_token
); }
439 void delete_async_event_handler ()
441 if (m_async_event_handler_token
!= nullptr)
442 ::delete_async_event_handler (&m_async_event_handler_token
);
445 bool is_async_p () const
447 /* We're async whenever the serial device is. */
448 gdb_assert (this->remote_desc
!= nullptr);
449 return serial_is_async_p (this->remote_desc
);
452 bool can_async_p () const
454 /* We can async whenever the serial device can. */
455 gdb_assert (this->remote_desc
!= nullptr);
456 return serial_can_async_p (this->remote_desc
);
461 /* A buffer to use for incoming packets, and its current size. The
462 buffer is grown dynamically for larger incoming packets.
463 Outgoing packets may also be constructed in this buffer.
464 The size of the buffer is always at least REMOTE_PACKET_SIZE;
465 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
467 gdb::char_vector buf
;
469 /* True if we're going through initial connection setup (finding out
470 about the remote side's threads, relocating symbols, etc.). */
471 bool starting_up
= false;
473 /* If we negotiated packet size explicitly (and thus can bypass
474 heuristics for the largest packet size that will not overflow
475 a buffer in the stub), this will be set to that packet size.
476 Otherwise zero, meaning to use the guessed size. */
477 long explicit_packet_size
= 0;
479 /* True, if in no ack mode. That is, neither GDB nor the stub will
480 expect acks from each other. The connection is assumed to be
482 bool noack_mode
= false;
484 /* True if we're connected in extended remote mode. */
485 bool extended
= false;
487 /* True if we resumed the target and we're waiting for the target to
488 stop. In the mean time, we can't start another command/query.
489 The remote server wouldn't be ready to process it, so we'd
490 timeout waiting for a reply that would never come and eventually
491 we'd close the connection. This can happen in asynchronous mode
492 because we allow GDB commands while the target is running. */
493 bool waiting_for_stop_reply
= false;
495 /* The status of the stub support for the various vCont actions. */
496 vCont_action_support supports_vCont
;
498 /* True if the user has pressed Ctrl-C, but the target hasn't
499 responded to that. */
500 bool ctrlc_pending_p
= false;
502 /* True if we saw a Ctrl-C while reading or writing from/to the
503 remote descriptor. At that point it is not safe to send a remote
504 interrupt packet, so we instead remember we saw the Ctrl-C and
505 process it once we're done with sending/receiving the current
506 packet, which should be shortly. If however that takes too long,
507 and the user presses Ctrl-C again, we offer to disconnect. */
508 bool got_ctrlc_during_io
= false;
510 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
511 remote_open knows that we don't have a file open when the program
513 struct serial
*remote_desc
= nullptr;
515 /* These are the threads which we last sent to the remote system. The
516 TID member will be -1 for all or -2 for not sent yet. */
517 ptid_t general_thread
= null_ptid
;
518 ptid_t continue_thread
= null_ptid
;
520 /* This is the traceframe which we last selected on the remote system.
521 It will be -1 if no traceframe is selected. */
522 int remote_traceframe_number
= -1;
524 char *last_pass_packet
= nullptr;
526 /* The last QProgramSignals packet sent to the target. We bypass
527 sending a new program signals list down to the target if the new
528 packet is exactly the same as the last we sent. IOW, we only let
529 the target know about program signals list changes. */
530 char *last_program_signals_packet
= nullptr;
532 gdb_signal last_sent_signal
= GDB_SIGNAL_0
;
534 bool last_sent_step
= false;
536 /* The execution direction of the last resume we got. */
537 exec_direction_kind last_resume_exec_dir
= EXEC_FORWARD
;
539 char *finished_object
= nullptr;
540 char *finished_annex
= nullptr;
541 ULONGEST finished_offset
= 0;
543 /* Should we try the 'ThreadInfo' query packet?
545 This variable (NOT available to the user: auto-detect only!)
546 determines whether GDB will use the new, simpler "ThreadInfo"
547 query or the older, more complex syntax for thread queries.
548 This is an auto-detect variable (set to true at each connect,
549 and set to false when the target fails to recognize it). */
550 bool use_threadinfo_query
= false;
551 bool use_threadextra_query
= false;
553 threadref echo_nextthread
{};
554 threadref nextthread
{};
555 threadref resultthreadlist
[MAXTHREADLISTRESULTS
] {};
557 /* The state of remote notification. */
558 struct remote_notif_state
*notif_state
= nullptr;
560 /* The branch trace configuration. */
561 struct btrace_config btrace_config
{};
563 /* The argument to the last "vFile:setfs:" packet we sent, used
564 to avoid sending repeated unnecessary "vFile:setfs:" packets.
565 Initialized to -1 to indicate that no "vFile:setfs:" packet
566 has yet been sent. */
569 /* A readahead cache for vFile:pread. Often, reading a binary
570 involves a sequence of small reads. E.g., when parsing an ELF
571 file. A readahead cache helps mostly the case of remote
572 debugging on a connection with higher latency, due to the
573 request/reply nature of the RSP. We only cache data for a single
574 file descriptor at a time. */
575 struct readahead_cache readahead_cache
;
577 /* The list of already fetched and acknowledged stop events. This
578 queue is used for notification Stop, and other notifications
579 don't need queue for their events, because the notification
580 events of Stop can't be consumed immediately, so that events
581 should be queued first, and be consumed by remote_wait_{ns,as}
582 one per time. Other notifications can consume their events
583 immediately, so queue is not needed for them. */
584 std::vector
<stop_reply_up
> stop_reply_queue
;
586 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
587 ``forever'' still use the normal timeout mechanism. This is
588 currently used by the ASYNC code to guarentee that target reads
589 during the initial connect always time-out. Once getpkt has been
590 modified to return a timeout indication and, in turn
591 remote_wait()/wait_for_inferior() have gained a timeout parameter
593 bool wait_forever_enabled_p
= true;
596 /* Asynchronous signal handle registered as event loop source for
597 when we have pending events ready to be passed to the core. */
598 async_event_handler
*m_async_event_handler_token
= nullptr;
600 /* Mapping of remote protocol data for each gdbarch. Usually there
601 is only one entry here, though we may see more with stubs that
602 support multi-process. */
603 std::unordered_map
<struct gdbarch
*, remote_arch_state
>
607 static const target_info remote_target_info
= {
609 N_("Remote target using gdb-specific protocol"),
613 /* Description of a remote packet. */
615 struct packet_description
617 /* Name of the packet used for gdb output. */
620 /* Title of the packet, used by the set/show remote name-packet
621 commands to identify the individual packages and gdb output. */
625 /* Configuration of a remote packet. */
629 /* If auto, GDB auto-detects support for this packet or feature,
630 either through qSupported, or by trying the packet and looking
631 at the response. If true, GDB assumes the target supports this
632 packet. If false, the packet is disabled. Configs that don't
633 have an associated command always have this set to auto. */
634 enum auto_boolean detect
;
636 /* Does the target support this packet? */
637 enum packet_support support
;
640 /* User configurable variables for the number of characters in a
641 memory read/write packet. MIN (rsa->remote_packet_size,
642 rsa->sizeof_g_packet) is the default. Some targets need smaller
643 values (fifo overruns, et.al.) and some users need larger values
644 (speed up transfers). The variables ``preferred_*'' (the user
645 request), ``current_*'' (what was actually set) and ``forced_*''
646 (Positive - a soft limit, negative - a hard limit). */
648 struct memory_packet_config
655 /* These global variables contain the default configuration for every new
656 remote_feature object. */
657 static memory_packet_config memory_read_packet_config
=
659 "memory-read-packet-size",
661 static memory_packet_config memory_write_packet_config
=
663 "memory-write-packet-size",
666 /* This global array contains packet descriptions (name and title). */
667 static packet_description packets_descriptions
[PACKET_MAX
];
668 /* This global array contains the default configuration for every new
669 per-remote target array. */
670 static packet_config remote_protocol_packets
[PACKET_MAX
];
672 /* Description of a remote target's features. It stores the configuration
673 and provides functions to determine supported features of the target. */
675 struct remote_features
679 m_memory_read_packet_config
= memory_read_packet_config
;
680 m_memory_write_packet_config
= memory_write_packet_config
;
682 std::copy (std::begin (remote_protocol_packets
),
683 std::end (remote_protocol_packets
),
684 std::begin (m_protocol_packets
));
686 ~remote_features () = default;
688 DISABLE_COPY_AND_ASSIGN (remote_features
);
690 /* Returns whether a given packet defined by its enum value is supported. */
691 enum packet_support
packet_support (int) const;
693 /* Returns the packet's corresponding "set remote foo-packet" command
694 state. See struct packet_config for more details. */
695 enum auto_boolean
packet_set_cmd_state (int packet
) const
696 { return m_protocol_packets
[packet
].detect
; }
698 /* Returns true if the multi-process extensions are in effect. */
699 int remote_multi_process_p () const
700 { return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
; }
702 /* Returns true if fork events are supported. */
703 int remote_fork_event_p () const
704 { return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
; }
706 /* Returns true if vfork events are supported. */
707 int remote_vfork_event_p () const
708 { return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
; }
710 /* Returns true if exec events are supported. */
711 int remote_exec_event_p () const
712 { return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
; }
714 /* Returns true if memory tagging is supported, false otherwise. */
715 bool remote_memory_tagging_p () const
716 { return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
; }
718 /* Reset all packets back to "unknown support". Called when opening a
719 new connection to a remote target. */
720 void reset_all_packet_configs_support ();
722 /* Check result value in BUF for packet WHICH_PACKET and update the packet's
723 support configuration accordingly. */
724 packet_result
packet_ok (const char *buf
, const int which_packet
);
725 packet_result
packet_ok (const gdb::char_vector
&buf
, const int which_packet
);
727 /* Configuration of a remote target's memory read packet. */
728 memory_packet_config m_memory_read_packet_config
;
729 /* Configuration of a remote target's memory write packet. */
730 memory_packet_config m_memory_write_packet_config
;
732 /* The per-remote target array which stores a remote's packet
734 packet_config m_protocol_packets
[PACKET_MAX
];
737 class remote_target
: public process_stratum_target
740 remote_target () = default;
741 ~remote_target () override
;
743 const target_info
&info () const override
744 { return remote_target_info
; }
746 const char *connection_string () override
;
748 thread_control_capabilities
get_thread_control_capabilities () override
749 { return tc_schedlock
; }
751 /* Open a remote connection. */
752 static void open (const char *, int);
754 void close () override
;
756 void detach (inferior
*, int) override
;
757 void disconnect (const char *, int) override
;
759 void commit_resumed () override
;
760 void resume (ptid_t
, int, enum gdb_signal
) override
;
761 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
762 bool has_pending_events () override
;
764 void fetch_registers (struct regcache
*, int) override
;
765 void store_registers (struct regcache
*, int) override
;
766 void prepare_to_store (struct regcache
*) override
;
768 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
770 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
771 enum remove_bp_reason
) override
;
774 bool stopped_by_sw_breakpoint () override
;
775 bool supports_stopped_by_sw_breakpoint () override
;
777 bool stopped_by_hw_breakpoint () override
;
779 bool supports_stopped_by_hw_breakpoint () override
;
781 bool stopped_by_watchpoint () override
;
783 bool stopped_data_address (CORE_ADDR
*) override
;
785 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
787 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
789 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
791 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
793 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
795 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
796 struct expression
*) override
;
798 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
799 struct expression
*) override
;
801 void kill () override
;
803 void load (const char *, int) override
;
805 void mourn_inferior () override
;
807 void pass_signals (gdb::array_view
<const unsigned char>) override
;
809 int set_syscall_catchpoint (int, bool, int,
810 gdb::array_view
<const int>) override
;
812 void program_signals (gdb::array_view
<const unsigned char>) override
;
814 bool thread_alive (ptid_t ptid
) override
;
816 const char *thread_name (struct thread_info
*) override
;
818 void update_thread_list () override
;
820 std::string
pid_to_str (ptid_t
) override
;
822 const char *extra_thread_info (struct thread_info
*) override
;
824 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
826 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
828 inferior
*inf
) override
;
830 gdb::array_view
<const gdb_byte
> thread_info_to_thread_handle (struct thread_info
*tp
)
833 void stop (ptid_t
) override
;
835 void interrupt () override
;
837 void pass_ctrlc () override
;
839 enum target_xfer_status
xfer_partial (enum target_object object
,
842 const gdb_byte
*writebuf
,
843 ULONGEST offset
, ULONGEST len
,
844 ULONGEST
*xfered_len
) override
;
846 ULONGEST
get_memory_xfer_limit () override
;
848 void rcmd (const char *command
, struct ui_file
*output
) override
;
850 const char *pid_to_exec_file (int pid
) override
;
852 void log_command (const char *cmd
) override
854 serial_log_command (this, cmd
);
857 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
858 CORE_ADDR load_module_addr
,
859 CORE_ADDR offset
) override
;
861 bool can_execute_reverse () override
;
863 std::vector
<mem_region
> memory_map () override
;
865 void flash_erase (ULONGEST address
, LONGEST length
) override
;
867 void flash_done () override
;
869 const struct target_desc
*read_description () override
;
871 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
872 const gdb_byte
*pattern
, ULONGEST pattern_len
,
873 CORE_ADDR
*found_addrp
) override
;
875 bool can_async_p () override
;
877 bool is_async_p () override
;
879 void async (bool) override
;
881 int async_wait_fd () override
;
883 void thread_events (int) override
;
885 int can_do_single_step () override
;
887 void terminal_inferior () override
;
889 void terminal_ours () override
;
891 bool supports_non_stop () override
;
893 bool supports_multi_process () override
;
895 bool supports_disable_randomization () override
;
897 bool filesystem_is_local () override
;
900 int fileio_open (struct inferior
*inf
, const char *filename
,
901 int flags
, int mode
, int warn_if_slow
,
902 fileio_error
*target_errno
) override
;
904 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
905 ULONGEST offset
, fileio_error
*target_errno
) override
;
907 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
908 ULONGEST offset
, fileio_error
*target_errno
) override
;
910 int fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
) override
;
912 int fileio_close (int fd
, fileio_error
*target_errno
) override
;
914 int fileio_unlink (struct inferior
*inf
,
915 const char *filename
,
916 fileio_error
*target_errno
) override
;
918 gdb::optional
<std::string
>
919 fileio_readlink (struct inferior
*inf
,
920 const char *filename
,
921 fileio_error
*target_errno
) override
;
923 bool supports_enable_disable_tracepoint () override
;
925 bool supports_string_tracing () override
;
927 int remote_supports_cond_tracepoints ();
929 bool supports_evaluation_of_breakpoint_conditions () override
;
931 int remote_supports_fast_tracepoints ();
933 int remote_supports_static_tracepoints ();
935 int remote_supports_install_in_trace ();
937 bool can_run_breakpoint_commands () override
;
939 void trace_init () override
;
941 void download_tracepoint (struct bp_location
*location
) override
;
943 bool can_download_tracepoint () override
;
945 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
947 void enable_tracepoint (struct bp_location
*location
) override
;
949 void disable_tracepoint (struct bp_location
*location
) override
;
951 void trace_set_readonly_regions () override
;
953 void trace_start () override
;
955 int get_trace_status (struct trace_status
*ts
) override
;
957 void get_tracepoint_status (tracepoint
*tp
, struct uploaded_tp
*utp
)
960 void trace_stop () override
;
962 int trace_find (enum trace_find_type type
, int num
,
963 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
965 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
967 int save_trace_data (const char *filename
) override
;
969 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
971 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
973 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
975 int get_min_fast_tracepoint_insn_len () override
;
977 void set_disconnected_tracing (int val
) override
;
979 void set_circular_trace_buffer (int val
) override
;
981 void set_trace_buffer_size (LONGEST val
) override
;
983 bool set_trace_notes (const char *user
, const char *notes
,
984 const char *stopnotes
) override
;
986 int core_of_thread (ptid_t ptid
) override
;
988 int verify_memory (const gdb_byte
*data
,
989 CORE_ADDR memaddr
, ULONGEST size
) override
;
992 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
994 void set_permissions () override
;
996 bool static_tracepoint_marker_at (CORE_ADDR
,
997 struct static_tracepoint_marker
*marker
)
1000 std::vector
<static_tracepoint_marker
>
1001 static_tracepoint_markers_by_strid (const char *id
) override
;
1003 traceframe_info_up
traceframe_info () override
;
1005 bool use_agent (bool use
) override
;
1006 bool can_use_agent () override
;
1008 struct btrace_target_info
*
1009 enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
) override
;
1011 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
1013 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
1015 enum btrace_error
read_btrace (struct btrace_data
*data
,
1016 struct btrace_target_info
*btinfo
,
1017 enum btrace_read_type type
) override
;
1019 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
1020 bool augmented_libraries_svr4_read () override
;
1021 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
1022 void follow_exec (inferior
*, ptid_t
, const char *) override
;
1023 int insert_fork_catchpoint (int) override
;
1024 int remove_fork_catchpoint (int) override
;
1025 int insert_vfork_catchpoint (int) override
;
1026 int remove_vfork_catchpoint (int) override
;
1027 int insert_exec_catchpoint (int) override
;
1028 int remove_exec_catchpoint (int) override
;
1029 enum exec_direction_kind
execution_direction () override
;
1031 bool supports_memory_tagging () override
;
1033 bool fetch_memtags (CORE_ADDR address
, size_t len
,
1034 gdb::byte_vector
&tags
, int type
) override
;
1036 bool store_memtags (CORE_ADDR address
, size_t len
,
1037 const gdb::byte_vector
&tags
, int type
) override
;
1039 public: /* Remote specific methods. */
1041 void remote_download_command_source (int num
, ULONGEST addr
,
1042 struct command_line
*cmds
);
1044 void remote_file_put (const char *local_file
, const char *remote_file
,
1046 void remote_file_get (const char *remote_file
, const char *local_file
,
1048 void remote_file_delete (const char *remote_file
, int from_tty
);
1050 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
1051 ULONGEST offset
, fileio_error
*remote_errno
);
1052 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
1053 ULONGEST offset
, fileio_error
*remote_errno
);
1054 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
1055 ULONGEST offset
, fileio_error
*remote_errno
);
1057 int remote_hostio_send_command (int command_bytes
, int which_packet
,
1058 fileio_error
*remote_errno
, const char **attachment
,
1059 int *attachment_len
);
1060 int remote_hostio_set_filesystem (struct inferior
*inf
,
1061 fileio_error
*remote_errno
);
1062 /* We should get rid of this and use fileio_open directly. */
1063 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
1064 int flags
, int mode
, int warn_if_slow
,
1065 fileio_error
*remote_errno
);
1066 int remote_hostio_close (int fd
, fileio_error
*remote_errno
);
1068 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
1069 fileio_error
*remote_errno
);
1071 struct remote_state
*get_remote_state ();
1073 long get_remote_packet_size (void);
1074 long get_memory_packet_size (struct memory_packet_config
*config
);
1076 long get_memory_write_packet_size ();
1077 long get_memory_read_packet_size ();
1079 char *append_pending_thread_resumptions (char *p
, char *endp
,
1081 static void open_1 (const char *name
, int from_tty
, int extended_p
);
1082 void start_remote (int from_tty
, int extended_p
);
1083 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
1085 char *append_resumption (char *p
, char *endp
,
1086 ptid_t ptid
, int step
, gdb_signal siggnal
);
1087 int remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
1088 gdb_signal siggnal
);
1090 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
1092 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
1093 target_wait_flags options
);
1094 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
1095 target_wait_flags options
);
1097 ptid_t
process_stop_reply (struct stop_reply
*stop_reply
,
1098 target_waitstatus
*status
);
1100 ptid_t select_thread_for_ambiguous_stop_reply
1101 (const struct target_waitstatus
&status
);
1103 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
1105 void print_one_stopped_thread (thread_info
*thread
);
1106 void process_initial_stop_replies (int from_tty
);
1108 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
1111 void btrace_sync_conf (const btrace_config
*conf
);
1113 void remote_btrace_maybe_reopen ();
1115 void remove_new_fork_children (threads_listing_context
*context
);
1116 void kill_new_fork_children (inferior
*inf
);
1117 void discard_pending_stop_replies (struct inferior
*inf
);
1118 int stop_reply_queue_length ();
1120 void check_pending_events_prevent_wildcard_vcont
1121 (bool *may_global_wildcard_vcont
);
1123 void discard_pending_stop_replies_in_queue ();
1124 struct stop_reply
*remote_notif_remove_queued_reply (ptid_t ptid
);
1125 struct stop_reply
*queued_stop_reply (ptid_t ptid
);
1126 int peek_stop_reply (ptid_t ptid
);
1127 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
1129 void remote_stop_ns (ptid_t ptid
);
1130 void remote_interrupt_as ();
1131 void remote_interrupt_ns ();
1133 char *remote_get_noisy_reply ();
1134 int remote_query_attached (int pid
);
1135 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
1138 ptid_t
remote_current_thread (ptid_t oldpid
);
1139 ptid_t
get_current_thread (const char *wait_status
);
1141 void set_thread (ptid_t ptid
, int gen
);
1142 void set_general_thread (ptid_t ptid
);
1143 void set_continue_thread (ptid_t ptid
);
1144 void set_general_process ();
1146 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
1148 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
1149 gdb_ext_thread_info
*info
);
1150 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
1151 gdb_ext_thread_info
*info
);
1153 int parse_threadlist_response (const char *pkt
, int result_limit
,
1154 threadref
*original_echo
,
1155 threadref
*resultlist
,
1157 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
1158 int result_limit
, int *done
, int *result_count
,
1159 threadref
*threadlist
);
1161 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1162 void *context
, int looplimit
);
1164 int remote_get_threads_with_ql (threads_listing_context
*context
);
1165 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
1166 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
1168 void extended_remote_restart ();
1170 void get_offsets ();
1172 void remote_check_symbols ();
1174 void remote_supported_packet (const struct protocol_feature
*feature
,
1175 enum packet_support support
,
1176 const char *argument
);
1178 void remote_query_supported ();
1180 void remote_packet_size (const protocol_feature
*feature
,
1181 packet_support support
, const char *value
);
1183 void remote_serial_quit_handler ();
1185 void remote_detach_pid (int pid
);
1187 void remote_vcont_probe ();
1189 void remote_resume_with_hc (ptid_t ptid
, int step
,
1190 gdb_signal siggnal
);
1192 void send_interrupt_sequence ();
1193 void interrupt_query ();
1195 void remote_notif_get_pending_events (const notif_client
*nc
);
1197 int fetch_register_using_p (struct regcache
*regcache
,
1199 int send_g_packet ();
1200 void process_g_packet (struct regcache
*regcache
);
1201 void fetch_registers_using_g (struct regcache
*regcache
);
1202 int store_register_using_P (const struct regcache
*regcache
,
1204 void store_registers_using_G (const struct regcache
*regcache
);
1206 void set_remote_traceframe ();
1208 void check_binary_download (CORE_ADDR addr
);
1210 target_xfer_status
remote_write_bytes_aux (const char *header
,
1212 const gdb_byte
*myaddr
,
1215 ULONGEST
*xfered_len_units
,
1219 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
1220 const gdb_byte
*myaddr
, ULONGEST len
,
1221 int unit_size
, ULONGEST
*xfered_len
);
1223 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
1225 int unit_size
, ULONGEST
*xfered_len_units
);
1227 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
1231 ULONGEST
*xfered_len
);
1233 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
1234 gdb_byte
*myaddr
, ULONGEST len
,
1236 ULONGEST
*xfered_len
);
1238 packet_result
remote_send_printf (const char *format
, ...)
1239 ATTRIBUTE_PRINTF (2, 3);
1241 target_xfer_status
remote_flash_write (ULONGEST address
,
1242 ULONGEST length
, ULONGEST
*xfered_len
,
1243 const gdb_byte
*data
);
1245 int readchar (int timeout
);
1247 void remote_serial_write (const char *str
, int len
);
1249 int putpkt (const char *buf
);
1250 int putpkt_binary (const char *buf
, int cnt
);
1252 int putpkt (const gdb::char_vector
&buf
)
1254 return putpkt (buf
.data ());
1258 long read_frame (gdb::char_vector
*buf_p
);
1259 int getpkt (gdb::char_vector
*buf
, bool forever
= false,
1260 bool *is_notif
= nullptr);
1261 int remote_vkill (int pid
);
1262 void remote_kill_k ();
1264 void extended_remote_disable_randomization (int val
);
1265 int extended_remote_run (const std::string
&args
);
1267 void send_environment_packet (const char *action
,
1271 void extended_remote_environment_support ();
1272 void extended_remote_set_inferior_cwd ();
1274 target_xfer_status
remote_write_qxfer (const char *object_name
,
1276 const gdb_byte
*writebuf
,
1277 ULONGEST offset
, LONGEST len
,
1278 ULONGEST
*xfered_len
,
1279 const unsigned int which_packet
);
1281 target_xfer_status
remote_read_qxfer (const char *object_name
,
1283 gdb_byte
*readbuf
, ULONGEST offset
,
1285 ULONGEST
*xfered_len
,
1286 const unsigned int which_packet
);
1288 void push_stop_reply (struct stop_reply
*new_event
);
1290 bool vcont_r_supported ();
1292 remote_features m_features
;
1296 bool start_remote_1 (int from_tty
, int extended_p
);
1298 /* The remote state. Don't reference this directly. Use the
1299 get_remote_state method instead. */
1300 remote_state m_remote_state
;
1303 static const target_info extended_remote_target_info
= {
1305 N_("Extended remote target using gdb-specific protocol"),
1309 /* Set up the extended remote target by extending the standard remote
1310 target and adding to it. */
1312 class extended_remote_target final
: public remote_target
1315 const target_info
&info () const override
1316 { return extended_remote_target_info
; }
1318 /* Open an extended-remote connection. */
1319 static void open (const char *, int);
1321 bool can_create_inferior () override
{ return true; }
1322 void create_inferior (const char *, const std::string
&,
1323 char **, int) override
;
1325 void detach (inferior
*, int) override
;
1327 bool can_attach () override
{ return true; }
1328 void attach (const char *, int) override
;
1330 void post_attach (int) override
;
1331 bool supports_disable_randomization () override
;
1334 struct stop_reply
: public notif_event
1338 /* The identifier of the thread about this event */
1341 /* The remote state this event is associated with. When the remote
1342 connection, represented by a remote_state object, is closed,
1343 all the associated stop_reply events should be released. */
1344 struct remote_state
*rs
;
1346 struct target_waitstatus ws
;
1348 /* The architecture associated with the expedited registers. */
1351 /* Expedited registers. This makes remote debugging a bit more
1352 efficient for those targets that provide critical registers as
1353 part of their normal status mechanism (as another roundtrip to
1354 fetch them is avoided). */
1355 std::vector
<cached_reg_t
> regcache
;
1357 enum target_stop_reason stop_reason
;
1359 CORE_ADDR watch_data_address
;
1364 /* Return TARGET as a remote_target if it is one, else nullptr. */
1366 static remote_target
*
1367 as_remote_target (process_stratum_target
*target
)
1369 return dynamic_cast<remote_target
*> (target
);
1375 is_remote_target (process_stratum_target
*target
)
1377 return as_remote_target (target
) != nullptr;
1380 /* Per-program-space data key. */
1381 static const registry
<program_space
>::key
<char, gdb::xfree_deleter
<char>>
1384 /* The variable registered as the control variable used by the
1385 remote exec-file commands. While the remote exec-file setting is
1386 per-program-space, the set/show machinery uses this as the
1387 location of the remote exec-file value. */
1388 static std::string remote_exec_file_var
;
1390 /* The size to align memory write packets, when practical. The protocol
1391 does not guarantee any alignment, and gdb will generate short
1392 writes and unaligned writes, but even as a best-effort attempt this
1393 can improve bulk transfers. For instance, if a write is misaligned
1394 relative to the target's data bus, the stub may need to make an extra
1395 round trip fetching data from the target. This doesn't make a
1396 huge difference, but it's easy to do, so we try to be helpful.
1398 The alignment chosen is arbitrary; usually data bus width is
1399 important here, not the possibly larger cache line size. */
1400 enum { REMOTE_ALIGN_WRITES
= 16 };
1402 /* Prototypes for local functions. */
1404 static int hexnumlen (ULONGEST num
);
1406 static int stubhex (int ch
);
1408 static int hexnumstr (char *, ULONGEST
);
1410 static int hexnumnstr (char *, ULONGEST
, int);
1412 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1414 static int stub_unpack_int (const char *buff
, int fieldlength
);
1416 static void set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
1417 cmd_list_element
*c
);
1419 static void show_packet_config_cmd (ui_file
*file
,
1420 const unsigned int which_packet
,
1421 remote_target
*remote
);
1423 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1425 struct cmd_list_element
*c
,
1428 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1430 static bool remote_read_description_p (struct target_ops
*target
);
1432 static void remote_console_output (const char *msg
);
1434 static void remote_btrace_reset (remote_state
*rs
);
1436 static void remote_unpush_and_throw (remote_target
*target
);
1440 static struct cmd_list_element
*remote_cmdlist
;
1442 /* For "set remote" and "show remote". */
1444 static struct cmd_list_element
*remote_set_cmdlist
;
1445 static struct cmd_list_element
*remote_show_cmdlist
;
1447 /* Controls whether GDB is willing to use range stepping. */
1449 static bool use_range_stepping
= true;
1451 /* From the remote target's point of view, each thread is in one of these three
1453 enum class resume_state
1455 /* Not resumed - we haven't been asked to resume this thread. */
1458 /* We have been asked to resume this thread, but haven't sent a vCont action
1459 for it yet. We'll need to consider it next time commit_resume is
1461 RESUMED_PENDING_VCONT
,
1463 /* We have been asked to resume this thread, and we have sent a vCont action
1468 /* Information about a thread's pending vCont-resume. Used when a thread is in
1469 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1470 stores this information which is then picked up by
1471 remote_target::commit_resume to know which is the proper action for this
1472 thread to include in the vCont packet. */
1473 struct resumed_pending_vcont_info
1475 /* True if the last resume call for this thread was a step request, false
1476 if a continue request. */
1479 /* The signal specified in the last resume call for this thread. */
1483 /* Private data that we'll store in (struct thread_info)->priv. */
1484 struct remote_thread_info
: public private_thread_info
1490 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1491 sequence of bytes. */
1492 gdb::byte_vector thread_handle
;
1494 /* Whether the target stopped for a breakpoint/watchpoint. */
1495 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1497 /* This is set to the data address of the access causing the target
1498 to stop for a watchpoint. */
1499 CORE_ADDR watch_data_address
= 0;
1501 /* Get the thread's resume state. */
1502 enum resume_state
get_resume_state () const
1504 return m_resume_state
;
1507 /* Put the thread in the NOT_RESUMED state. */
1508 void set_not_resumed ()
1510 m_resume_state
= resume_state::NOT_RESUMED
;
1513 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1514 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1516 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1517 m_resumed_pending_vcont_info
.step
= step
;
1518 m_resumed_pending_vcont_info
.sig
= sig
;
1521 /* Get the information this thread's pending vCont-resumption.
1523 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1525 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1527 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1529 return m_resumed_pending_vcont_info
;
1532 /* Put the thread in the VCONT_RESUMED state. */
1535 m_resume_state
= resume_state::RESUMED
;
1539 /* Resume state for this thread. This is used to implement vCont action
1540 coalescing (only when the target operates in non-stop mode).
1542 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1543 which notes that this thread must be considered in the next commit_resume
1546 remote_target::commit_resume sends a vCont packet with actions for the
1547 threads in the RESUMED_PENDING_VCONT state and moves them to the
1548 VCONT_RESUMED state.
1550 When reporting a stop to the core for a thread, that thread is moved back
1551 to the NOT_RESUMED state. */
1552 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1554 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1555 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1558 remote_state::remote_state ()
1563 remote_state::~remote_state ()
1565 xfree (this->last_pass_packet
);
1566 xfree (this->last_program_signals_packet
);
1567 xfree (this->finished_object
);
1568 xfree (this->finished_annex
);
1571 /* Utility: generate error from an incoming stub packet. */
1573 trace_error (char *buf
)
1576 return; /* not an error msg */
1579 case '1': /* malformed packet error */
1580 if (*++buf
== '0') /* general case: */
1581 error (_("remote.c: error in outgoing packet."));
1583 error (_("remote.c: error in outgoing packet at field #%ld."),
1584 strtol (buf
, NULL
, 16));
1586 error (_("Target returns error code '%s'."), buf
);
1590 /* Utility: wait for reply from stub, while accepting "O" packets. */
1593 remote_target::remote_get_noisy_reply ()
1595 struct remote_state
*rs
= get_remote_state ();
1597 do /* Loop on reply from remote stub. */
1601 QUIT
; /* Allow user to bail out with ^C. */
1603 buf
= rs
->buf
.data ();
1606 else if (startswith (buf
, "qRelocInsn:"))
1609 CORE_ADDR from
, to
, org_to
;
1611 int adjusted_size
= 0;
1614 p
= buf
+ strlen ("qRelocInsn:");
1615 pp
= unpack_varlen_hex (p
, &ul
);
1617 error (_("invalid qRelocInsn packet: %s"), buf
);
1621 unpack_varlen_hex (p
, &ul
);
1628 gdbarch_relocate_instruction (current_inferior ()->arch (),
1632 catch (const gdb_exception
&ex
)
1634 if (ex
.error
== MEMORY_ERROR
)
1636 /* Propagate memory errors silently back to the
1637 target. The stub may have limited the range of
1638 addresses we can write to, for example. */
1642 /* Something unexpectedly bad happened. Be verbose
1643 so we can tell what, and propagate the error back
1644 to the stub, so it doesn't get stuck waiting for
1646 exception_fprintf (gdb_stderr
, ex
,
1647 _("warning: relocating instruction: "));
1654 adjusted_size
= to
- org_to
;
1656 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1660 else if (buf
[0] == 'O' && buf
[1] != 'K')
1661 remote_console_output (buf
+ 1); /* 'O' message from stub */
1663 return buf
; /* Here's the actual reply. */
1668 struct remote_arch_state
*
1669 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1671 remote_arch_state
*rsa
;
1673 auto it
= this->m_arch_states
.find (gdbarch
);
1674 if (it
== this->m_arch_states
.end ())
1676 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1677 std::forward_as_tuple (gdbarch
),
1678 std::forward_as_tuple (gdbarch
));
1679 rsa
= &p
.first
->second
;
1681 /* Make sure that the packet buffer is plenty big enough for
1682 this architecture. */
1683 if (this->buf
.size () < rsa
->remote_packet_size
)
1684 this->buf
.resize (2 * rsa
->remote_packet_size
);
1692 /* Fetch the global remote target state. */
1695 remote_target::get_remote_state ()
1697 /* Make sure that the remote architecture state has been
1698 initialized, because doing so might reallocate rs->buf. Any
1699 function which calls getpkt also needs to be mindful of changes
1700 to rs->buf, but this call limits the number of places which run
1702 m_remote_state
.get_remote_arch_state (current_inferior ()->arch ());
1704 return &m_remote_state
;
1707 /* Fetch the remote exec-file from the current program space. */
1710 get_remote_exec_file (void)
1712 char *remote_exec_file
;
1714 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1715 if (remote_exec_file
== NULL
)
1718 return remote_exec_file
;
1721 /* Set the remote exec file for PSPACE. */
1724 set_pspace_remote_exec_file (struct program_space
*pspace
,
1725 const char *remote_exec_file
)
1727 char *old_file
= remote_pspace_data
.get (pspace
);
1730 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1733 /* The "set/show remote exec-file" set command hook. */
1736 set_remote_exec_file (const char *ignored
, int from_tty
,
1737 struct cmd_list_element
*c
)
1739 set_pspace_remote_exec_file (current_program_space
,
1740 remote_exec_file_var
.c_str ());
1743 /* The "set/show remote exec-file" show command hook. */
1746 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1747 struct cmd_list_element
*cmd
, const char *value
)
1749 gdb_printf (file
, "%s\n", get_remote_exec_file ());
1753 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1755 int regnum
, num_remote_regs
, offset
;
1756 struct packet_reg
**remote_regs
;
1758 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1760 struct packet_reg
*r
= ®s
[regnum
];
1762 if (register_size (gdbarch
, regnum
) == 0)
1763 /* Do not try to fetch zero-sized (placeholder) registers. */
1766 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1771 /* Define the g/G packet format as the contents of each register
1772 with a remote protocol number, in order of ascending protocol
1775 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1776 for (num_remote_regs
= 0, regnum
= 0;
1777 regnum
< gdbarch_num_regs (gdbarch
);
1779 if (regs
[regnum
].pnum
!= -1)
1780 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1782 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1783 [] (const packet_reg
*a
, const packet_reg
*b
)
1784 { return a
->pnum
< b
->pnum
; });
1786 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1788 remote_regs
[regnum
]->in_g_packet
= 1;
1789 remote_regs
[regnum
]->offset
= offset
;
1790 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1796 /* Given the architecture described by GDBARCH, return the remote
1797 protocol register's number and the register's offset in the g/G
1798 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1799 If the target does not have a mapping for REGNUM, return false,
1800 otherwise, return true. */
1803 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1804 int *pnum
, int *poffset
)
1806 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1808 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1810 map_regcache_remote_table (gdbarch
, regs
.data ());
1812 *pnum
= regs
[regnum
].pnum
;
1813 *poffset
= regs
[regnum
].offset
;
1818 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1820 /* Use the architecture to build a regnum<->pnum table, which will be
1821 1:1 unless a feature set specifies otherwise. */
1822 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1824 /* Record the maximum possible size of the g packet - it may turn out
1826 this->sizeof_g_packet
1827 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1829 /* Default maximum number of characters in a packet body. Many
1830 remote stubs have a hardwired buffer size of 400 bytes
1831 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1832 as the maximum packet-size to ensure that the packet and an extra
1833 NUL character can always fit in the buffer. This stops GDB
1834 trashing stubs that try to squeeze an extra NUL into what is
1835 already a full buffer (As of 1999-12-04 that was most stubs). */
1836 this->remote_packet_size
= 400 - 1;
1838 /* This one is filled in when a ``g'' packet is received. */
1839 this->actual_register_packet_size
= 0;
1841 /* Should rsa->sizeof_g_packet needs more space than the
1842 default, adjust the size accordingly. Remember that each byte is
1843 encoded as two characters. 32 is the overhead for the packet
1844 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1845 (``$NN:G...#NN'') is a better guess, the below has been padded a
1847 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1848 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1851 /* Get a pointer to the current remote target. If not connected to a
1852 remote target, return NULL. */
1854 static remote_target
*
1855 get_current_remote_target ()
1857 target_ops
*proc_target
= current_inferior ()->process_target ();
1858 return dynamic_cast<remote_target
*> (proc_target
);
1861 /* Return the current allowed size of a remote packet. This is
1862 inferred from the current architecture, and should be used to
1863 limit the length of outgoing packets. */
1865 remote_target::get_remote_packet_size ()
1867 struct remote_state
*rs
= get_remote_state ();
1868 remote_arch_state
*rsa
1869 = rs
->get_remote_arch_state (current_inferior ()->arch ());
1871 if (rs
->explicit_packet_size
)
1872 return rs
->explicit_packet_size
;
1874 return rsa
->remote_packet_size
;
1877 static struct packet_reg
*
1878 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1881 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
1885 struct packet_reg
*r
= &rsa
->regs
[regnum
];
1887 gdb_assert (r
->regnum
== regnum
);
1892 static struct packet_reg
*
1893 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1898 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1900 struct packet_reg
*r
= &rsa
->regs
[i
];
1902 if (r
->pnum
== pnum
)
1908 /* Allow the user to specify what sequence to send to the remote
1909 when he requests a program interruption: Although ^C is usually
1910 what remote systems expect (this is the default, here), it is
1911 sometimes preferable to send a break. On other systems such
1912 as the Linux kernel, a break followed by g, which is Magic SysRq g
1913 is required in order to interrupt the execution. */
1914 const char interrupt_sequence_control_c
[] = "Ctrl-C";
1915 const char interrupt_sequence_break
[] = "BREAK";
1916 const char interrupt_sequence_break_g
[] = "BREAK-g";
1917 static const char *const interrupt_sequence_modes
[] =
1919 interrupt_sequence_control_c
,
1920 interrupt_sequence_break
,
1921 interrupt_sequence_break_g
,
1924 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
1927 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
1928 struct cmd_list_element
*c
,
1931 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
1933 _("Send the ASCII ETX character (Ctrl-c) "
1934 "to the remote target to interrupt the "
1935 "execution of the program.\n"));
1936 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
1938 _("send a break signal to the remote target "
1939 "to interrupt the execution of the program.\n"));
1940 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
1942 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1943 "the remote target to interrupt the execution "
1944 "of Linux kernel.\n"));
1946 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
1947 interrupt_sequence_mode
);
1950 /* This boolean variable specifies whether interrupt_sequence is sent
1951 to the remote target when gdb connects to it.
1952 This is mostly needed when you debug the Linux kernel: The Linux kernel
1953 expects BREAK g which is Magic SysRq g for connecting gdb. */
1954 static bool interrupt_on_connect
= false;
1956 /* This variable is used to implement the "set/show remotebreak" commands.
1957 Since these commands are now deprecated in favor of "set/show remote
1958 interrupt-sequence", it no longer has any effect on the code. */
1959 static bool remote_break
;
1962 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
1965 interrupt_sequence_mode
= interrupt_sequence_break
;
1967 interrupt_sequence_mode
= interrupt_sequence_control_c
;
1971 show_remotebreak (struct ui_file
*file
, int from_tty
,
1972 struct cmd_list_element
*c
,
1977 /* This variable sets the number of bits in an address that are to be
1978 sent in a memory ("M" or "m") packet. Normally, after stripping
1979 leading zeros, the entire address would be sent. This variable
1980 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1981 initial implementation of remote.c restricted the address sent in
1982 memory packets to ``host::sizeof long'' bytes - (typically 32
1983 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1984 address was never sent. Since fixing this bug may cause a break in
1985 some remote targets this variable is principally provided to
1986 facilitate backward compatibility. */
1988 static unsigned int remote_address_size
;
1991 /* The default max memory-write-packet-size, when the setting is
1992 "fixed". The 16k is historical. (It came from older GDB's using
1993 alloca for buffers and the knowledge (folklore?) that some hosts
1994 don't cope very well with large alloca calls.) */
1995 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
1997 /* The minimum remote packet size for memory transfers. Ensures we
1998 can write at least one byte. */
1999 #define MIN_MEMORY_PACKET_SIZE 20
2001 /* Get the memory packet size, assuming it is fixed. */
2004 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
2006 gdb_assert (config
->fixed_p
);
2008 if (config
->size
<= 0)
2009 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
2011 return config
->size
;
2014 /* Compute the current size of a read/write packet. Since this makes
2015 use of ``actual_register_packet_size'' the computation is dynamic. */
2018 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
2020 struct remote_state
*rs
= get_remote_state ();
2021 remote_arch_state
*rsa
2022 = rs
->get_remote_arch_state (current_inferior ()->arch ());
2025 if (config
->fixed_p
)
2026 what_they_get
= get_fixed_memory_packet_size (config
);
2029 what_they_get
= get_remote_packet_size ();
2030 /* Limit the packet to the size specified by the user. */
2031 if (config
->size
> 0
2032 && what_they_get
> config
->size
)
2033 what_they_get
= config
->size
;
2035 /* Limit it to the size of the targets ``g'' response unless we have
2036 permission from the stub to use a larger packet size. */
2037 if (rs
->explicit_packet_size
== 0
2038 && rsa
->actual_register_packet_size
> 0
2039 && what_they_get
> rsa
->actual_register_packet_size
)
2040 what_they_get
= rsa
->actual_register_packet_size
;
2042 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
2043 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
2045 /* Make sure there is room in the global buffer for this packet
2046 (including its trailing NUL byte). */
2047 if (rs
->buf
.size () < what_they_get
+ 1)
2048 rs
->buf
.resize (2 * what_they_get
);
2050 return what_they_get
;
2053 /* Update the size of a read/write packet. If they user wants
2054 something really big then do a sanity check. */
2057 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
,
2058 bool target_connected
)
2060 int fixed_p
= config
->fixed_p
;
2061 long size
= config
->size
;
2064 error (_("Argument required (integer, \"fixed\" or \"limit\")."));
2065 else if (strcmp (args
, "hard") == 0
2066 || strcmp (args
, "fixed") == 0)
2068 else if (strcmp (args
, "soft") == 0
2069 || strcmp (args
, "limit") == 0)
2075 size
= strtoul (args
, &end
, 0);
2077 error (_("Invalid %s (bad syntax)."), config
->name
);
2079 /* Instead of explicitly capping the size of a packet to or
2080 disallowing it, the user is allowed to set the size to
2081 something arbitrarily large. */
2085 if (fixed_p
&& !config
->fixed_p
)
2087 /* So that the query shows the correct value. */
2088 long query_size
= (size
<= 0
2089 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
2092 if (target_connected
2093 && !query (_("The target may not be able to correctly handle a %s\n"
2094 "of %ld bytes. Change the packet size? "),
2095 config
->name
, query_size
))
2096 error (_("Packet size not changed."));
2097 else if (!target_connected
2098 && !query (_("Future remote targets may not be able to "
2099 "correctly handle a %s\nof %ld bytes. Change the "
2100 "packet size for future remote targets? "),
2101 config
->name
, query_size
))
2102 error (_("Packet size not changed."));
2104 /* Update the config. */
2105 config
->fixed_p
= fixed_p
;
2106 config
->size
= size
;
2108 const char *target_type
= get_target_type_name (target_connected
);
2109 gdb_printf (_("The %s %s is set to \"%s\".\n"), config
->name
, target_type
,
2114 /* Show the memory-read or write-packet size configuration CONFIG of the
2115 target REMOTE. If REMOTE is nullptr, the default configuration for future
2116 remote targets should be passed in CONFIG. */
2119 show_memory_packet_size (memory_packet_config
*config
, remote_target
*remote
)
2121 const char *target_type
= get_target_type_name (remote
!= nullptr);
2123 if (config
->size
== 0)
2124 gdb_printf (_("The %s %s is 0 (default). "), config
->name
, target_type
);
2126 gdb_printf (_("The %s %s is %ld. "), config
->name
, target_type
,
2129 if (config
->fixed_p
)
2130 gdb_printf (_("Packets are fixed at %ld bytes.\n"),
2131 get_fixed_memory_packet_size (config
));
2134 if (remote
!= nullptr)
2135 gdb_printf (_("Packets are limited to %ld bytes.\n"),
2136 remote
->get_memory_packet_size (config
));
2138 gdb_puts ("The actual limit will be further reduced "
2139 "dependent on the target.\n");
2143 /* Configure the memory-write-packet size of the currently selected target. If
2144 no target is available, the default configuration for future remote targets
2148 set_memory_write_packet_size (const char *args
, int from_tty
)
2150 remote_target
*remote
= get_current_remote_target ();
2151 if (remote
!= nullptr)
2153 set_memory_packet_size
2154 (args
, &remote
->m_features
.m_memory_write_packet_config
, true);
2158 memory_packet_config
* config
= &memory_write_packet_config
;
2159 set_memory_packet_size (args
, config
, false);
2163 /* Display the memory-write-packet size of the currently selected target. If
2164 no target is available, the default configuration for future remote targets
2168 show_memory_write_packet_size (const char *args
, int from_tty
)
2170 remote_target
*remote
= get_current_remote_target ();
2171 if (remote
!= nullptr)
2172 show_memory_packet_size (&remote
->m_features
.m_memory_write_packet_config
,
2175 show_memory_packet_size (&memory_write_packet_config
, nullptr);
2178 /* Show the number of hardware watchpoints that can be used. */
2181 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
2182 struct cmd_list_element
*c
,
2185 gdb_printf (file
, _("The maximum number of target hardware "
2186 "watchpoints is %s.\n"), value
);
2189 /* Show the length limit (in bytes) for hardware watchpoints. */
2192 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
2193 struct cmd_list_element
*c
,
2196 gdb_printf (file
, _("The maximum length (in bytes) of a target "
2197 "hardware watchpoint is %s.\n"), value
);
2200 /* Show the number of hardware breakpoints that can be used. */
2203 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
2204 struct cmd_list_element
*c
,
2207 gdb_printf (file
, _("The maximum number of target hardware "
2208 "breakpoints is %s.\n"), value
);
2211 /* Controls the maximum number of characters to display in the debug output
2212 for each remote packet. The remaining characters are omitted. */
2214 static int remote_packet_max_chars
= 512;
2216 /* Show the maximum number of characters to display for each remote packet
2217 when remote debugging is enabled. */
2220 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
2221 struct cmd_list_element
*c
,
2224 gdb_printf (file
, _("Number of remote packet characters to "
2225 "display is %s.\n"), value
);
2229 remote_target::get_memory_write_packet_size ()
2231 return get_memory_packet_size (&m_features
.m_memory_write_packet_config
);
2234 /* Configure the memory-read-packet size of the currently selected target. If
2235 no target is available, the default configuration for future remote targets
2239 set_memory_read_packet_size (const char *args
, int from_tty
)
2241 remote_target
*remote
= get_current_remote_target ();
2242 if (remote
!= nullptr)
2243 set_memory_packet_size
2244 (args
, &remote
->m_features
.m_memory_read_packet_config
, true);
2247 memory_packet_config
* config
= &memory_read_packet_config
;
2248 set_memory_packet_size (args
, config
, false);
2253 /* Display the memory-read-packet size of the currently selected target. If
2254 no target is available, the default configuration for future remote targets
2258 show_memory_read_packet_size (const char *args
, int from_tty
)
2260 remote_target
*remote
= get_current_remote_target ();
2261 if (remote
!= nullptr)
2262 show_memory_packet_size (&remote
->m_features
.m_memory_read_packet_config
,
2265 show_memory_packet_size (&memory_read_packet_config
, nullptr);
2269 remote_target::get_memory_read_packet_size ()
2271 long size
= get_memory_packet_size (&m_features
.m_memory_read_packet_config
);
2273 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
2274 extra buffer size argument before the memory read size can be
2275 increased beyond this. */
2276 if (size
> get_remote_packet_size ())
2277 size
= get_remote_packet_size ();
2281 static enum packet_support
packet_config_support (const packet_config
*config
);
2285 set_remote_protocol_packet_cmd (const char *args
, int from_tty
,
2286 cmd_list_element
*c
)
2288 remote_target
*remote
= get_current_remote_target ();
2289 gdb_assert (c
->var
.has_value ());
2291 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2292 const int packet_idx
= std::distance (remote_protocol_packets
,
2295 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2297 const char *name
= packets_descriptions
[packet_idx
].name
;
2298 const auto_boolean value
= c
->var
->get
<auto_boolean
> ();
2299 const char *support
= get_packet_support_name (value
);
2300 const char *target_type
= get_target_type_name (remote
!= nullptr);
2302 if (remote
!= nullptr)
2303 remote
->m_features
.m_protocol_packets
[packet_idx
].detect
= value
;
2305 remote_protocol_packets
[packet_idx
].detect
= value
;
2307 gdb_printf (_("Support for the '%s' packet %s is set to \"%s\".\n"), name
,
2308 target_type
, support
);
2312 internal_error (_("Could not find config for %s"), c
->name
);
2316 show_packet_config_cmd (ui_file
*file
, const unsigned int which_packet
,
2317 remote_target
*remote
)
2319 const char *support
= "internal-error";
2320 const char *target_type
= get_target_type_name (remote
!= nullptr);
2322 packet_config
*config
;
2323 if (remote
!= nullptr)
2324 config
= &remote
->m_features
.m_protocol_packets
[which_packet
];
2326 config
= &remote_protocol_packets
[which_packet
];
2328 switch (packet_config_support (config
))
2331 support
= "enabled";
2333 case PACKET_DISABLE
:
2334 support
= "disabled";
2336 case PACKET_SUPPORT_UNKNOWN
:
2337 support
= "unknown";
2340 switch (config
->detect
)
2342 case AUTO_BOOLEAN_AUTO
:
2344 _("Support for the '%s' packet %s is \"auto\", "
2346 packets_descriptions
[which_packet
].name
, target_type
,
2349 case AUTO_BOOLEAN_TRUE
:
2350 case AUTO_BOOLEAN_FALSE
:
2352 _("Support for the '%s' packet %s is \"%s\".\n"),
2353 packets_descriptions
[which_packet
].name
, target_type
,
2354 get_packet_support_name (config
->detect
));
2360 add_packet_config_cmd (const unsigned int which_packet
, const char *name
,
2361 const char *title
, int legacy
)
2363 packets_descriptions
[which_packet
].name
= name
;
2364 packets_descriptions
[which_packet
].title
= title
;
2366 packet_config
*config
= &remote_protocol_packets
[which_packet
];
2368 gdb::unique_xmalloc_ptr
<char> set_doc
2369 = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
2371 gdb::unique_xmalloc_ptr
<char> show_doc
2372 = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
2374 /* set/show TITLE-packet {auto,on,off} */
2375 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
2376 set_show_commands cmds
2377 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
2378 &config
->detect
, set_doc
.get (),
2379 show_doc
.get (), NULL
, /* help_doc */
2380 set_remote_protocol_packet_cmd
,
2381 show_remote_protocol_packet_cmd
,
2382 &remote_set_cmdlist
, &remote_show_cmdlist
);
2383 cmds
.show
->set_context (config
);
2384 cmds
.set
->set_context (config
);
2386 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
2389 /* It's not clear who should take ownership of the LEGACY_NAME string
2390 created below, so, for now, place the string into a static vector
2391 which ensures the strings is released when GDB exits. */
2392 static std::vector
<gdb::unique_xmalloc_ptr
<char>> legacy_names
;
2393 gdb::unique_xmalloc_ptr
<char> legacy_name
2394 = xstrprintf ("%s-packet", name
);
2395 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
2396 &remote_set_cmdlist
);
2397 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
2398 &remote_show_cmdlist
);
2399 legacy_names
.emplace_back (std::move (legacy_name
));
2403 static enum packet_result
2404 packet_check_result (const char *buf
)
2408 /* The stub recognized the packet request. Check that the
2409 operation succeeded. */
2411 && isxdigit (buf
[1]) && isxdigit (buf
[2])
2413 /* "Enn" - definitely an error. */
2414 return PACKET_ERROR
;
2416 /* Always treat "E." as an error. This will be used for
2417 more verbose error messages, such as E.memtypes. */
2418 if (buf
[0] == 'E' && buf
[1] == '.')
2419 return PACKET_ERROR
;
2421 /* The packet may or may not be OK. Just assume it is. */
2425 /* The stub does not support the packet. */
2426 return PACKET_UNKNOWN
;
2429 static enum packet_result
2430 packet_check_result (const gdb::char_vector
&buf
)
2432 return packet_check_result (buf
.data ());
2436 remote_features::packet_ok (const char *buf
, const int which_packet
)
2438 packet_config
*config
= &m_protocol_packets
[which_packet
];
2439 packet_description
*descr
= &packets_descriptions
[which_packet
];
2441 enum packet_result result
;
2443 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2444 && config
->support
== PACKET_DISABLE
)
2445 internal_error (_("packet_ok: attempt to use a disabled packet"));
2447 result
= packet_check_result (buf
);
2452 /* The stub recognized the packet request. */
2453 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2455 remote_debug_printf ("Packet %s (%s) is supported",
2456 descr
->name
, descr
->title
);
2457 config
->support
= PACKET_ENABLE
;
2460 case PACKET_UNKNOWN
:
2461 /* The stub does not support the packet. */
2462 if (config
->detect
== AUTO_BOOLEAN_AUTO
2463 && config
->support
== PACKET_ENABLE
)
2465 /* If the stub previously indicated that the packet was
2466 supported then there is a protocol error. */
2467 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2468 descr
->name
, descr
->title
);
2470 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2472 /* The user set it wrong. */
2473 error (_("Enabled packet %s (%s) not recognized by stub"),
2474 descr
->name
, descr
->title
);
2477 remote_debug_printf ("Packet %s (%s) is NOT supported", descr
->name
,
2479 config
->support
= PACKET_DISABLE
;
2487 remote_features::packet_ok (const gdb::char_vector
&buf
, const int which_packet
)
2489 return packet_ok (buf
.data (), which_packet
);
2492 /* Returns whether a given packet or feature is supported. This takes
2493 into account the state of the corresponding "set remote foo-packet"
2494 command, which may be used to bypass auto-detection. */
2496 static enum packet_support
2497 packet_config_support (const packet_config
*config
)
2499 switch (config
->detect
)
2501 case AUTO_BOOLEAN_TRUE
:
2502 return PACKET_ENABLE
;
2503 case AUTO_BOOLEAN_FALSE
:
2504 return PACKET_DISABLE
;
2505 case AUTO_BOOLEAN_AUTO
:
2506 return config
->support
;
2508 gdb_assert_not_reached ("bad switch");
2513 remote_features::packet_support (int packet
) const
2515 const packet_config
*config
= &m_protocol_packets
[packet
];
2516 return packet_config_support (config
);
2520 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2521 struct cmd_list_element
*c
,
2524 remote_target
*remote
= get_current_remote_target ();
2525 gdb_assert (c
->var
.has_value ());
2527 auto *default_config
= static_cast<packet_config
*> (c
->context ());
2528 const int packet_idx
= std::distance (remote_protocol_packets
,
2531 if (packet_idx
>= 0 && packet_idx
< PACKET_MAX
)
2533 show_packet_config_cmd (file
, packet_idx
, remote
);
2536 internal_error (_("Could not find config for %s"), c
->name
);
2539 /* Should we try one of the 'Z' requests? */
2543 Z_PACKET_SOFTWARE_BP
,
2544 Z_PACKET_HARDWARE_BP
,
2551 /* For compatibility with older distributions. Provide a ``set remote
2552 Z-packet ...'' command that updates all the Z packet types. */
2554 static enum auto_boolean remote_Z_packet_detect
;
2557 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2558 struct cmd_list_element
*c
)
2560 remote_target
*remote
= get_current_remote_target ();
2563 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2565 if (remote
!= nullptr)
2566 remote
->m_features
.m_protocol_packets
[PACKET_Z0
+ i
].detect
2567 = remote_Z_packet_detect
;
2569 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2572 const char *support
= get_packet_support_name (remote_Z_packet_detect
);
2573 const char *target_type
= get_target_type_name (remote
!= nullptr);
2574 gdb_printf (_("Use of Z packets %s is set to \"%s\".\n"), target_type
,
2580 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2581 struct cmd_list_element
*c
,
2584 remote_target
*remote
= get_current_remote_target ();
2587 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2588 show_packet_config_cmd (file
, PACKET_Z0
+ i
, remote
);
2591 /* Insert fork catchpoint target routine. If fork events are enabled
2592 then return success, nothing more to do. */
2595 remote_target::insert_fork_catchpoint (int pid
)
2597 return !m_features
.remote_fork_event_p ();
2600 /* Remove fork catchpoint target routine. Nothing to do, just
2604 remote_target::remove_fork_catchpoint (int pid
)
2609 /* Insert vfork catchpoint target routine. If vfork events are enabled
2610 then return success, nothing more to do. */
2613 remote_target::insert_vfork_catchpoint (int pid
)
2615 return !m_features
.remote_vfork_event_p ();
2618 /* Remove vfork catchpoint target routine. Nothing to do, just
2622 remote_target::remove_vfork_catchpoint (int pid
)
2627 /* Insert exec catchpoint target routine. If exec events are
2628 enabled, just return success. */
2631 remote_target::insert_exec_catchpoint (int pid
)
2633 return !m_features
.remote_exec_event_p ();
2636 /* Remove exec catchpoint target routine. Nothing to do, just
2640 remote_target::remove_exec_catchpoint (int pid
)
2647 /* Take advantage of the fact that the TID field is not used, to tag
2648 special ptids with it set to != 0. */
2649 static const ptid_t
magic_null_ptid (42000, -1, 1);
2650 static const ptid_t
not_sent_ptid (42000, -2, 1);
2651 static const ptid_t
any_thread_ptid (42000, 0, 1);
2653 /* Find out if the stub attached to PID (and hence GDB should offer to
2654 detach instead of killing it when bailing out). */
2657 remote_target::remote_query_attached (int pid
)
2659 struct remote_state
*rs
= get_remote_state ();
2660 size_t size
= get_remote_packet_size ();
2662 if (m_features
.packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2665 if (m_features
.remote_multi_process_p ())
2666 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2668 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2673 switch (m_features
.packet_ok (rs
->buf
, PACKET_qAttached
))
2676 if (strcmp (rs
->buf
.data (), "1") == 0)
2680 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
2682 case PACKET_UNKNOWN
:
2689 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2690 has been invented by GDB, instead of reported by the target. Since
2691 we can be connected to a remote system before before knowing about
2692 any inferior, mark the target with execution when we find the first
2693 inferior. If ATTACHED is 1, then we had just attached to this
2694 inferior. If it is 0, then we just created this inferior. If it
2695 is -1, then try querying the remote stub to find out if it had
2696 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2697 attempt to open this inferior's executable as the main executable
2698 if no main executable is open already. */
2701 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2704 struct inferior
*inf
;
2706 /* Check whether this process we're learning about is to be
2707 considered attached, or if is to be considered to have been
2708 spawned by the stub. */
2710 attached
= remote_query_attached (pid
);
2712 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
2714 /* If the target shares code across all inferiors, then every
2715 attach adds a new inferior. */
2716 inf
= add_inferior (pid
);
2718 /* ... and every inferior is bound to the same program space.
2719 However, each inferior may still have its own address
2721 inf
->aspace
= maybe_new_address_space ();
2722 inf
->pspace
= current_program_space
;
2726 /* In the traditional debugging scenario, there's a 1-1 match
2727 between program/address spaces. We simply bind the inferior
2728 to the program space's address space. */
2729 inf
= current_inferior ();
2731 /* However, if the current inferior is already bound to a
2732 process, find some other empty inferior. */
2736 for (inferior
*it
: all_inferiors ())
2745 /* Since all inferiors were already bound to a process, add
2747 inf
= add_inferior_with_spaces ();
2749 switch_to_inferior_no_thread (inf
);
2750 inf
->push_target (this);
2751 inferior_appeared (inf
, pid
);
2754 inf
->attach_flag
= attached
;
2755 inf
->fake_pid_p
= fake_pid_p
;
2757 /* If no main executable is currently open then attempt to
2758 open the file that was executed to create this inferior. */
2759 if (try_open_exec
&& get_exec_file (0) == NULL
)
2760 exec_file_locate_attach (pid
, 0, 1);
2762 /* Check for exec file mismatch, and let the user solve it. */
2763 validate_exec_file (1);
2768 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2769 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2772 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2773 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2774 remote_state::starting_up flag) is true then the new thread is added
2775 silently, otherwise the new thread will be announced to the user. */
2778 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2781 struct remote_state
*rs
= get_remote_state ();
2782 struct thread_info
*thread
;
2784 /* GDB historically didn't pull threads in the initial connection
2785 setup. If the remote target doesn't even have a concept of
2786 threads (e.g., a bare-metal target), even if internally we
2787 consider that a single-threaded target, mentioning a new thread
2788 might be confusing to the user. Be silent then, preserving the
2789 age old behavior. */
2790 if (rs
->starting_up
|| silent_p
)
2791 thread
= add_thread_silent (this, ptid
);
2793 thread
= add_thread (this, ptid
);
2795 /* We start by assuming threads are resumed. That state then gets updated
2796 when we process a matching stop reply. */
2797 get_remote_thread_info (thread
)->set_resumed ();
2799 set_executing (this, ptid
, executing
);
2800 set_running (this, ptid
, running
);
2805 /* Come here when we learn about a thread id from the remote target.
2806 It may be the first time we hear about such thread, so take the
2807 opportunity to add it to GDB's thread list. In case this is the
2808 first time we're noticing its corresponding inferior, add it to
2809 GDB's inferior list as well. EXECUTING indicates whether the
2810 thread is (internally) executing or stopped. */
2813 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2815 /* In non-stop mode, we assume new found threads are (externally)
2816 running until proven otherwise with a stop reply. In all-stop,
2817 we can only get here if all threads are stopped. */
2818 bool running
= target_is_non_stop_p ();
2820 /* If this is a new thread, add it to GDB's thread list.
2821 If we leave it up to WFI to do this, bad things will happen. */
2823 thread_info
*tp
= this->find_thread (currthread
);
2824 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2826 /* We're seeing an event on a thread id we knew had exited.
2827 This has to be a new thread reusing the old id. Add it. */
2828 remote_add_thread (currthread
, running
, executing
, false);
2832 if (!in_thread_list (this, currthread
))
2834 struct inferior
*inf
= NULL
;
2835 int pid
= currthread
.pid ();
2837 if (inferior_ptid
.is_pid ()
2838 && pid
== inferior_ptid
.pid ())
2840 /* inferior_ptid has no thread member yet. This can happen
2841 with the vAttach -> remote_wait,"TAAthread:" path if the
2842 stub doesn't support qC. This is the first stop reported
2843 after an attach, so this is the main thread. Update the
2844 ptid in the thread list. */
2845 if (in_thread_list (this, ptid_t (pid
)))
2846 thread_change_ptid (this, inferior_ptid
, currthread
);
2850 = remote_add_thread (currthread
, running
, executing
, false);
2851 switch_to_thread (thr
);
2856 if (magic_null_ptid
== inferior_ptid
)
2858 /* inferior_ptid is not set yet. This can happen with the
2859 vRun -> remote_wait,"TAAthread:" path if the stub
2860 doesn't support qC. This is the first stop reported
2861 after an attach, so this is the main thread. Update the
2862 ptid in the thread list. */
2863 thread_change_ptid (this, inferior_ptid
, currthread
);
2867 /* When connecting to a target remote, or to a target
2868 extended-remote which already was debugging an inferior, we
2869 may not know about it yet. Add it before adding its child
2870 thread, so notifications are emitted in a sensible order. */
2871 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
2873 bool fake_pid_p
= !m_features
.remote_multi_process_p ();
2875 inf
= remote_add_inferior (fake_pid_p
,
2876 currthread
.pid (), -1, 1);
2879 /* This is really a new thread. Add it. */
2880 thread_info
*new_thr
2881 = remote_add_thread (currthread
, running
, executing
, false);
2883 /* If we found a new inferior, let the common code do whatever
2884 it needs to with it (e.g., read shared libraries, insert
2885 breakpoints), unless we're just setting up an all-stop
2889 struct remote_state
*rs
= get_remote_state ();
2891 if (!rs
->starting_up
)
2892 notice_new_inferior (new_thr
, executing
, 0);
2897 /* Return THREAD's private thread data, creating it if necessary. */
2899 static remote_thread_info
*
2900 get_remote_thread_info (thread_info
*thread
)
2902 gdb_assert (thread
!= NULL
);
2904 if (thread
->priv
== NULL
)
2905 thread
->priv
.reset (new remote_thread_info
);
2907 return gdb::checked_static_cast
<remote_thread_info
*> (thread
->priv
.get ());
2910 /* Return PTID's private thread data, creating it if necessary. */
2912 static remote_thread_info
*
2913 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
2915 thread_info
*thr
= target
->find_thread (ptid
);
2916 return get_remote_thread_info (thr
);
2919 /* Call this function as a result of
2920 1) A halt indication (T packet) containing a thread id
2921 2) A direct query of currthread
2922 3) Successful execution of set thread */
2925 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
2927 rs
->general_thread
= currthread
;
2930 /* If 'QPassSignals' is supported, tell the remote stub what signals
2931 it can simply pass through to the inferior without reporting. */
2934 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2936 if (m_features
.packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
2938 char *pass_packet
, *p
;
2940 struct remote_state
*rs
= get_remote_state ();
2942 gdb_assert (pass_signals
.size () < 256);
2943 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2945 if (pass_signals
[i
])
2948 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
2949 strcpy (pass_packet
, "QPassSignals:");
2950 p
= pass_packet
+ strlen (pass_packet
);
2951 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2953 if (pass_signals
[i
])
2956 *p
++ = tohex (i
>> 4);
2957 *p
++ = tohex (i
& 15);
2966 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
2968 putpkt (pass_packet
);
2970 m_features
.packet_ok (rs
->buf
, PACKET_QPassSignals
);
2971 xfree (rs
->last_pass_packet
);
2972 rs
->last_pass_packet
= pass_packet
;
2975 xfree (pass_packet
);
2979 /* If 'QCatchSyscalls' is supported, tell the remote stub
2980 to report syscalls to GDB. */
2983 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
2984 gdb::array_view
<const int> syscall_counts
)
2986 const char *catch_packet
;
2987 enum packet_result result
;
2990 if (m_features
.packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
2992 /* Not supported. */
2996 if (needed
&& any_count
== 0)
2998 /* Count how many syscalls are to be caught. */
2999 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
3001 if (syscall_counts
[i
] != 0)
3006 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
3007 pid
, needed
, any_count
, n_sysno
);
3009 std::string built_packet
;
3012 /* Prepare a packet with the sysno list, assuming max 8+1
3013 characters for a sysno. If the resulting packet size is too
3014 big, fallback on the non-selective packet. */
3015 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
3016 built_packet
.reserve (maxpktsz
);
3017 built_packet
= "QCatchSyscalls:1";
3020 /* Add in each syscall to be caught. */
3021 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
3023 if (syscall_counts
[i
] != 0)
3024 string_appendf (built_packet
, ";%zx", i
);
3027 if (built_packet
.size () > get_remote_packet_size ())
3029 /* catch_packet too big. Fallback to less efficient
3030 non selective mode, with GDB doing the filtering. */
3031 catch_packet
= "QCatchSyscalls:1";
3034 catch_packet
= built_packet
.c_str ();
3037 catch_packet
= "QCatchSyscalls:0";
3039 struct remote_state
*rs
= get_remote_state ();
3041 putpkt (catch_packet
);
3043 result
= m_features
.packet_ok (rs
->buf
, PACKET_QCatchSyscalls
);
3044 if (result
== PACKET_OK
)
3050 /* If 'QProgramSignals' is supported, tell the remote stub what
3051 signals it should pass through to the inferior when detaching. */
3054 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
3056 if (m_features
.packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
3060 struct remote_state
*rs
= get_remote_state ();
3062 gdb_assert (signals
.size () < 256);
3063 for (size_t i
= 0; i
< signals
.size (); i
++)
3068 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
3069 strcpy (packet
, "QProgramSignals:");
3070 p
= packet
+ strlen (packet
);
3071 for (size_t i
= 0; i
< signals
.size (); i
++)
3073 if (signal_pass_state (i
))
3076 *p
++ = tohex (i
>> 4);
3077 *p
++ = tohex (i
& 15);
3086 if (!rs
->last_program_signals_packet
3087 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
3091 m_features
.packet_ok (rs
->buf
, PACKET_QProgramSignals
);
3092 xfree (rs
->last_program_signals_packet
);
3093 rs
->last_program_signals_packet
= packet
;
3100 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
3101 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
3102 thread. If GEN is set, set the general thread, if not, then set
3103 the step/continue thread. */
3105 remote_target::set_thread (ptid_t ptid
, int gen
)
3107 struct remote_state
*rs
= get_remote_state ();
3108 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
3109 char *buf
= rs
->buf
.data ();
3110 char *endbuf
= buf
+ get_remote_packet_size ();
3116 *buf
++ = gen
? 'g' : 'c';
3117 if (ptid
== magic_null_ptid
)
3118 xsnprintf (buf
, endbuf
- buf
, "0");
3119 else if (ptid
== any_thread_ptid
)
3120 xsnprintf (buf
, endbuf
- buf
, "0");
3121 else if (ptid
== minus_one_ptid
)
3122 xsnprintf (buf
, endbuf
- buf
, "-1");
3124 write_ptid (buf
, endbuf
, ptid
);
3128 rs
->general_thread
= ptid
;
3130 rs
->continue_thread
= ptid
;
3134 remote_target::set_general_thread (ptid_t ptid
)
3136 set_thread (ptid
, 1);
3140 remote_target::set_continue_thread (ptid_t ptid
)
3142 set_thread (ptid
, 0);
3145 /* Change the remote current process. Which thread within the process
3146 ends up selected isn't important, as long as it is the same process
3147 as what INFERIOR_PTID points to.
3149 This comes from that fact that there is no explicit notion of
3150 "selected process" in the protocol. The selected process for
3151 general operations is the process the selected general thread
3155 remote_target::set_general_process ()
3157 /* If the remote can't handle multiple processes, don't bother. */
3158 if (!m_features
.remote_multi_process_p ())
3161 remote_state
*rs
= get_remote_state ();
3163 /* We only need to change the remote current thread if it's pointing
3164 at some other process. */
3165 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
3166 set_general_thread (inferior_ptid
);
3170 /* Return nonzero if this is the main thread that we made up ourselves
3171 to model non-threaded targets as single-threaded. */
3174 remote_thread_always_alive (ptid_t ptid
)
3176 if (ptid
== magic_null_ptid
)
3177 /* The main thread is always alive. */
3180 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
3181 /* The main thread is always alive. This can happen after a
3182 vAttach, if the remote side doesn't support
3189 /* Return nonzero if the thread PTID is still alive on the remote
3193 remote_target::thread_alive (ptid_t ptid
)
3195 struct remote_state
*rs
= get_remote_state ();
3198 /* Check if this is a thread that we made up ourselves to model
3199 non-threaded targets as single-threaded. */
3200 if (remote_thread_always_alive (ptid
))
3203 p
= rs
->buf
.data ();
3204 endp
= p
+ get_remote_packet_size ();
3207 write_ptid (p
, endp
, ptid
);
3211 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
3214 /* Return a pointer to a thread name if we know it and NULL otherwise.
3215 The thread_info object owns the memory for the name. */
3218 remote_target::thread_name (struct thread_info
*info
)
3220 if (info
->priv
!= NULL
)
3222 const std::string
&name
= get_remote_thread_info (info
)->name
;
3223 return !name
.empty () ? name
.c_str () : NULL
;
3229 /* About these extended threadlist and threadinfo packets. They are
3230 variable length packets but, the fields within them are often fixed
3231 length. They are redundant enough to send over UDP as is the
3232 remote protocol in general. There is a matching unit test module
3235 /* WARNING: This threadref data structure comes from the remote O.S.,
3236 libstub protocol encoding, and remote.c. It is not particularly
3239 /* Right now, the internal structure is int. We want it to be bigger.
3240 Plan to fix this. */
3242 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3244 /* gdb_ext_thread_info is an internal GDB data structure which is
3245 equivalent to the reply of the remote threadinfo packet. */
3247 struct gdb_ext_thread_info
3249 threadref threadid
; /* External form of thread reference. */
3250 int active
; /* Has state interesting to GDB?
3252 char display
[256]; /* Brief state display, name,
3253 blocked/suspended. */
3254 char shortname
[32]; /* To be used to name threads. */
3255 char more_display
[256]; /* Long info, statistics, queue depth,
3259 /* The volume of remote transfers can be limited by submitting
3260 a mask containing bits specifying the desired information.
3261 Use a union of these values as the 'selection' parameter to
3262 get_thread_info. FIXME: Make these TAG names more thread specific. */
3264 #define TAG_THREADID 1
3265 #define TAG_EXISTS 2
3266 #define TAG_DISPLAY 4
3267 #define TAG_THREADNAME 8
3268 #define TAG_MOREDISPLAY 16
3270 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3272 static const char *unpack_nibble (const char *buf
, int *val
);
3274 static const char *unpack_byte (const char *buf
, int *value
);
3276 static char *pack_int (char *buf
, int value
);
3278 static const char *unpack_int (const char *buf
, int *value
);
3280 static const char *unpack_string (const char *src
, char *dest
, int length
);
3282 static char *pack_threadid (char *pkt
, threadref
*id
);
3284 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3286 void int_to_threadref (threadref
*id
, int value
);
3288 static int threadref_to_int (threadref
*ref
);
3290 static void copy_threadref (threadref
*dest
, threadref
*src
);
3292 static int threadmatch (threadref
*dest
, threadref
*src
);
3294 static char *pack_threadinfo_request (char *pkt
, int mode
,
3297 static char *pack_threadlist_request (char *pkt
, int startflag
,
3299 threadref
*nextthread
);
3301 static int remote_newthread_step (threadref
*ref
, void *context
);
3304 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3305 buffer we're allowed to write to. Returns
3306 BUF+CHARACTERS_WRITTEN. */
3309 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3313 if (m_features
.remote_multi_process_p ())
3317 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3319 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3323 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3325 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3330 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3331 last parsed char. Returns null_ptid if no thread id is found, and
3332 throws an error if the thread id has an invalid format. */
3335 read_ptid (const char *buf
, const char **obuf
)
3337 const char *p
= buf
;
3339 ULONGEST pid
= 0, tid
= 0;
3343 /* Multi-process ptid. */
3344 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3346 error (_("invalid remote ptid: %s"), p
);
3349 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3352 return ptid_t (pid
, tid
);
3355 /* No multi-process. Just a tid. */
3356 pp
= unpack_varlen_hex (p
, &tid
);
3358 /* Return null_ptid when no thread id is found. */
3366 /* Since the stub is not sending a process id, default to what's
3367 current_inferior, unless it doesn't have a PID yet. If so,
3368 then since there's no way to know the pid of the reported
3369 threads, use the magic number. */
3370 inferior
*inf
= current_inferior ();
3372 pid
= magic_null_ptid
.pid ();
3378 return ptid_t (pid
, tid
);
3384 if (ch
>= 'a' && ch
<= 'f')
3385 return ch
- 'a' + 10;
3386 if (ch
>= '0' && ch
<= '9')
3388 if (ch
>= 'A' && ch
<= 'F')
3389 return ch
- 'A' + 10;
3394 stub_unpack_int (const char *buff
, int fieldlength
)
3401 nibble
= stubhex (*buff
++);
3405 retval
= retval
<< 4;
3411 unpack_nibble (const char *buf
, int *val
)
3413 *val
= fromhex (*buf
++);
3418 unpack_byte (const char *buf
, int *value
)
3420 *value
= stub_unpack_int (buf
, 2);
3425 pack_int (char *buf
, int value
)
3427 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3428 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3429 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3430 buf
= pack_hex_byte (buf
, (value
& 0xff));
3435 unpack_int (const char *buf
, int *value
)
3437 *value
= stub_unpack_int (buf
, 8);
3441 #if 0 /* Currently unused, uncomment when needed. */
3442 static char *pack_string (char *pkt
, char *string
);
3445 pack_string (char *pkt
, char *string
)
3450 len
= strlen (string
);
3452 len
= 200; /* Bigger than most GDB packets, junk??? */
3453 pkt
= pack_hex_byte (pkt
, len
);
3457 if ((ch
== '\0') || (ch
== '#'))
3458 ch
= '*'; /* Protect encapsulation. */
3463 #endif /* 0 (unused) */
3466 unpack_string (const char *src
, char *dest
, int length
)
3475 pack_threadid (char *pkt
, threadref
*id
)
3478 unsigned char *altid
;
3480 altid
= (unsigned char *) id
;
3481 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3483 pkt
= pack_hex_byte (pkt
, *altid
++);
3489 unpack_threadid (const char *inbuf
, threadref
*id
)
3492 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3495 altref
= (char *) id
;
3497 while (inbuf
< limit
)
3499 x
= stubhex (*inbuf
++);
3500 y
= stubhex (*inbuf
++);
3501 *altref
++ = (x
<< 4) | y
;
3506 /* Externally, threadrefs are 64 bits but internally, they are still
3507 ints. This is due to a mismatch of specifications. We would like
3508 to use 64bit thread references internally. This is an adapter
3512 int_to_threadref (threadref
*id
, int value
)
3514 unsigned char *scan
;
3516 scan
= (unsigned char *) id
;
3522 *scan
++ = (value
>> 24) & 0xff;
3523 *scan
++ = (value
>> 16) & 0xff;
3524 *scan
++ = (value
>> 8) & 0xff;
3525 *scan
++ = (value
& 0xff);
3529 threadref_to_int (threadref
*ref
)
3532 unsigned char *scan
;
3538 value
= (value
<< 8) | ((*scan
++) & 0xff);
3543 copy_threadref (threadref
*dest
, threadref
*src
)
3546 unsigned char *csrc
, *cdest
;
3548 csrc
= (unsigned char *) src
;
3549 cdest
= (unsigned char *) dest
;
3556 threadmatch (threadref
*dest
, threadref
*src
)
3558 /* Things are broken right now, so just assume we got a match. */
3560 unsigned char *srcp
, *destp
;
3562 srcp
= (char *) src
;
3563 destp
= (char *) dest
;
3567 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3574 threadid:1, # always request threadid
3581 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3584 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3586 *pkt
++ = 'q'; /* Info Query */
3587 *pkt
++ = 'P'; /* process or thread info */
3588 pkt
= pack_int (pkt
, mode
); /* mode */
3589 pkt
= pack_threadid (pkt
, id
); /* threadid */
3590 *pkt
= '\0'; /* terminate */
3594 /* These values tag the fields in a thread info response packet. */
3595 /* Tagging the fields allows us to request specific fields and to
3596 add more fields as time goes by. */
3598 #define TAG_THREADID 1 /* Echo the thread identifier. */
3599 #define TAG_EXISTS 2 /* Is this process defined enough to
3600 fetch registers and its stack? */
3601 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3602 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3603 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3607 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3608 threadref
*expectedref
,
3609 gdb_ext_thread_info
*info
)
3611 struct remote_state
*rs
= get_remote_state ();
3615 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3618 /* info->threadid = 0; FIXME: implement zero_threadref. */
3620 info
->display
[0] = '\0';
3621 info
->shortname
[0] = '\0';
3622 info
->more_display
[0] = '\0';
3624 /* Assume the characters indicating the packet type have been
3626 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3627 pkt
= unpack_threadid (pkt
, &ref
);
3630 warning (_("Incomplete response to threadinfo request."));
3631 if (!threadmatch (&ref
, expectedref
))
3632 { /* This is an answer to a different request. */
3633 warning (_("ERROR RMT Thread info mismatch."));
3636 copy_threadref (&info
->threadid
, &ref
);
3638 /* Loop on tagged fields , try to bail if something goes wrong. */
3640 /* Packets are terminated with nulls. */
3641 while ((pkt
< limit
) && mask
&& *pkt
)
3643 pkt
= unpack_int (pkt
, &tag
); /* tag */
3644 pkt
= unpack_byte (pkt
, &length
); /* length */
3645 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3647 warning (_("ERROR RMT: threadinfo tag mismatch."));
3651 if (tag
== TAG_THREADID
)
3655 warning (_("ERROR RMT: length of threadid is not 16."));
3659 pkt
= unpack_threadid (pkt
, &ref
);
3660 mask
= mask
& ~TAG_THREADID
;
3663 if (tag
== TAG_EXISTS
)
3665 info
->active
= stub_unpack_int (pkt
, length
);
3667 mask
= mask
& ~(TAG_EXISTS
);
3670 warning (_("ERROR RMT: 'exists' length too long."));
3676 if (tag
== TAG_THREADNAME
)
3678 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3679 mask
= mask
& ~TAG_THREADNAME
;
3682 if (tag
== TAG_DISPLAY
)
3684 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3685 mask
= mask
& ~TAG_DISPLAY
;
3688 if (tag
== TAG_MOREDISPLAY
)
3690 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3691 mask
= mask
& ~TAG_MOREDISPLAY
;
3694 warning (_("ERROR RMT: unknown thread info tag."));
3695 break; /* Not a tag we know about. */
3701 remote_target::remote_get_threadinfo (threadref
*threadid
,
3703 gdb_ext_thread_info
*info
)
3705 struct remote_state
*rs
= get_remote_state ();
3708 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3712 if (rs
->buf
[0] == '\0')
3715 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3720 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3723 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3724 threadref
*nextthread
)
3726 *pkt
++ = 'q'; /* info query packet */
3727 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3728 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3729 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3730 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3735 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3738 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3739 threadref
*original_echo
,
3740 threadref
*resultlist
,
3743 struct remote_state
*rs
= get_remote_state ();
3744 int count
, resultcount
, done
;
3747 /* Assume the 'q' and 'M chars have been stripped. */
3748 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3749 /* done parse past here */
3750 pkt
= unpack_byte (pkt
, &count
); /* count field */
3751 pkt
= unpack_nibble (pkt
, &done
);
3752 /* The first threadid is the argument threadid. */
3753 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3754 while ((count
-- > 0) && (pkt
< limit
))
3756 pkt
= unpack_threadid (pkt
, resultlist
++);
3757 if (resultcount
++ >= result_limit
)
3765 /* Fetch the next batch of threads from the remote. Returns -1 if the
3766 qL packet is not supported, 0 on error and 1 on success. */
3769 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3770 int result_limit
, int *done
, int *result_count
,
3771 threadref
*threadlist
)
3773 struct remote_state
*rs
= get_remote_state ();
3776 /* Truncate result limit to be smaller than the packet size. */
3777 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3778 >= get_remote_packet_size ())
3779 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3781 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3785 if (rs
->buf
[0] == '\0')
3787 /* Packet not supported. */
3792 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3793 &rs
->echo_nextthread
, threadlist
, done
);
3795 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3797 /* FIXME: This is a good reason to drop the packet. */
3798 /* Possibly, there is a duplicate response. */
3800 retransmit immediatly - race conditions
3801 retransmit after timeout - yes
3803 wait for packet, then exit
3805 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3806 return 0; /* I choose simply exiting. */
3808 if (*result_count
<= 0)
3812 warning (_("RMT ERROR : failed to get remote thread list."));
3815 return result
; /* break; */
3817 if (*result_count
> result_limit
)
3820 warning (_("RMT ERROR: threadlist response longer than requested."));
3826 /* Fetch the list of remote threads, with the qL packet, and call
3827 STEPFUNCTION for each thread found. Stops iterating and returns 1
3828 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3829 STEPFUNCTION returns false. If the packet is not supported,
3833 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3834 void *context
, int looplimit
)
3836 struct remote_state
*rs
= get_remote_state ();
3837 int done
, i
, result_count
;
3845 if (loopcount
++ > looplimit
)
3848 warning (_("Remote fetch threadlist -infinite loop-."));
3851 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3852 MAXTHREADLISTRESULTS
,
3853 &done
, &result_count
,
3854 rs
->resultthreadlist
);
3857 /* Clear for later iterations. */
3859 /* Setup to resume next batch of thread references, set nextthread. */
3860 if (result_count
>= 1)
3861 copy_threadref (&rs
->nextthread
,
3862 &rs
->resultthreadlist
[result_count
- 1]);
3864 while (result_count
--)
3866 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
3876 /* A thread found on the remote target. */
3880 explicit thread_item (ptid_t ptid_
)
3884 thread_item (thread_item
&&other
) = default;
3885 thread_item
&operator= (thread_item
&&other
) = default;
3887 DISABLE_COPY_AND_ASSIGN (thread_item
);
3889 /* The thread's PTID. */
3892 /* The thread's extra info. */
3895 /* The thread's name. */
3898 /* The core the thread was running on. -1 if not known. */
3901 /* The thread handle associated with the thread. */
3902 gdb::byte_vector thread_handle
;
3905 /* Context passed around to the various methods listing remote
3906 threads. As new threads are found, they're added to the ITEMS
3909 struct threads_listing_context
3911 /* Return true if this object contains an entry for a thread with ptid
3914 bool contains_thread (ptid_t ptid
) const
3916 auto match_ptid
= [&] (const thread_item
&item
)
3918 return item
.ptid
== ptid
;
3921 auto it
= std::find_if (this->items
.begin (),
3925 return it
!= this->items
.end ();
3928 /* Remove the thread with ptid PTID. */
3930 void remove_thread (ptid_t ptid
)
3932 auto match_ptid
= [&] (const thread_item
&item
)
3934 return item
.ptid
== ptid
;
3937 auto it
= std::remove_if (this->items
.begin (),
3941 if (it
!= this->items
.end ())
3942 this->items
.erase (it
);
3945 /* The threads found on the remote target. */
3946 std::vector
<thread_item
> items
;
3950 remote_newthread_step (threadref
*ref
, void *data
)
3952 struct threads_listing_context
*context
3953 = (struct threads_listing_context
*) data
;
3954 int pid
= inferior_ptid
.pid ();
3955 int lwp
= threadref_to_int (ref
);
3956 ptid_t
ptid (pid
, lwp
);
3958 context
->items
.emplace_back (ptid
);
3960 return 1; /* continue iterator */
3963 #define CRAZY_MAX_THREADS 1000
3966 remote_target::remote_current_thread (ptid_t oldpid
)
3968 struct remote_state
*rs
= get_remote_state ();
3972 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
3977 result
= read_ptid (&rs
->buf
[2], &obuf
);
3979 remote_debug_printf ("warning: garbage in qC reply");
3987 /* List remote threads using the deprecated qL packet. */
3990 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
3992 if (remote_threadlist_iterator (remote_newthread_step
, context
,
3993 CRAZY_MAX_THREADS
) >= 0)
3999 #if defined(HAVE_LIBEXPAT)
4002 start_thread (struct gdb_xml_parser
*parser
,
4003 const struct gdb_xml_element
*element
,
4005 std::vector
<gdb_xml_value
> &attributes
)
4007 struct threads_listing_context
*data
4008 = (struct threads_listing_context
*) user_data
;
4009 struct gdb_xml_value
*attr
;
4011 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
4012 ptid_t ptid
= read_ptid (id
, NULL
);
4014 data
->items
.emplace_back (ptid
);
4015 thread_item
&item
= data
->items
.back ();
4017 attr
= xml_find_attribute (attributes
, "core");
4019 item
.core
= *(ULONGEST
*) attr
->value
.get ();
4021 attr
= xml_find_attribute (attributes
, "name");
4023 item
.name
= (const char *) attr
->value
.get ();
4025 attr
= xml_find_attribute (attributes
, "handle");
4027 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
4031 end_thread (struct gdb_xml_parser
*parser
,
4032 const struct gdb_xml_element
*element
,
4033 void *user_data
, const char *body_text
)
4035 struct threads_listing_context
*data
4036 = (struct threads_listing_context
*) user_data
;
4038 if (body_text
!= NULL
&& *body_text
!= '\0')
4039 data
->items
.back ().extra
= body_text
;
4042 const struct gdb_xml_attribute thread_attributes
[] = {
4043 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
4044 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
4045 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4046 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
4047 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
4050 const struct gdb_xml_element thread_children
[] = {
4051 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4054 const struct gdb_xml_element threads_children
[] = {
4055 { "thread", thread_attributes
, thread_children
,
4056 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
4057 start_thread
, end_thread
},
4058 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4061 const struct gdb_xml_element threads_elements
[] = {
4062 { "threads", NULL
, threads_children
,
4063 GDB_XML_EF_NONE
, NULL
, NULL
},
4064 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
4069 /* List remote threads using qXfer:threads:read. */
4072 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
4074 #if defined(HAVE_LIBEXPAT)
4075 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4077 gdb::optional
<gdb::char_vector
> xml
4078 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
4080 if (xml
&& (*xml
)[0] != '\0')
4082 gdb_xml_parse_quick (_("threads"), "threads.dtd",
4083 threads_elements
, xml
->data (), context
);
4093 /* List remote threads using qfThreadInfo/qsThreadInfo. */
4096 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
4098 struct remote_state
*rs
= get_remote_state ();
4100 if (rs
->use_threadinfo_query
)
4104 putpkt ("qfThreadInfo");
4106 bufp
= rs
->buf
.data ();
4107 if (bufp
[0] != '\0') /* q packet recognized */
4109 while (*bufp
++ == 'm') /* reply contains one or more TID */
4113 ptid_t ptid
= read_ptid (bufp
, &bufp
);
4114 context
->items
.emplace_back (ptid
);
4116 while (*bufp
++ == ','); /* comma-separated list */
4117 putpkt ("qsThreadInfo");
4119 bufp
= rs
->buf
.data ();
4125 /* Packet not recognized. */
4126 rs
->use_threadinfo_query
= 0;
4133 /* Return true if INF only has one non-exited thread. */
4136 has_single_non_exited_thread (inferior
*inf
)
4139 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
4145 /* Implement the to_update_thread_list function for the remote
4149 remote_target::update_thread_list ()
4151 struct threads_listing_context context
;
4154 /* We have a few different mechanisms to fetch the thread list. Try
4155 them all, starting with the most preferred one first, falling
4156 back to older methods. */
4157 if (remote_get_threads_with_qxfer (&context
)
4158 || remote_get_threads_with_qthreadinfo (&context
)
4159 || remote_get_threads_with_ql (&context
))
4163 if (context
.items
.empty ()
4164 && remote_thread_always_alive (inferior_ptid
))
4166 /* Some targets don't really support threads, but still
4167 reply an (empty) thread list in response to the thread
4168 listing packets, instead of replying "packet not
4169 supported". Exit early so we don't delete the main
4174 /* CONTEXT now holds the current thread list on the remote
4175 target end. Delete GDB-side threads no longer found on the
4177 for (thread_info
*tp
: all_threads_safe ())
4179 if (tp
->inf
->process_target () != this)
4182 if (!context
.contains_thread (tp
->ptid
))
4184 /* Do not remove the thread if it is the last thread in
4185 the inferior. This situation happens when we have a
4186 pending exit process status to process. Otherwise we
4187 may end up with a seemingly live inferior (i.e. pid
4188 != 0) that has no threads. */
4189 if (has_single_non_exited_thread (tp
->inf
))
4197 /* Remove any unreported fork child threads from CONTEXT so
4198 that we don't interfere with follow fork, which is where
4199 creation of such threads is handled. */
4200 remove_new_fork_children (&context
);
4202 /* And now add threads we don't know about yet to our list. */
4203 for (thread_item
&item
: context
.items
)
4205 if (item
.ptid
!= null_ptid
)
4207 /* In non-stop mode, we assume new found threads are
4208 executing until proven otherwise with a stop reply.
4209 In all-stop, we can only get here if all threads are
4211 bool executing
= target_is_non_stop_p ();
4213 remote_notice_new_inferior (item
.ptid
, executing
);
4215 thread_info
*tp
= this->find_thread (item
.ptid
);
4216 remote_thread_info
*info
= get_remote_thread_info (tp
);
4217 info
->core
= item
.core
;
4218 info
->extra
= std::move (item
.extra
);
4219 info
->name
= std::move (item
.name
);
4220 info
->thread_handle
= std::move (item
.thread_handle
);
4227 /* If no thread listing method is supported, then query whether
4228 each known thread is alive, one by one, with the T packet.
4229 If the target doesn't support threads at all, then this is a
4230 no-op. See remote_thread_alive. */
4236 * Collect a descriptive string about the given thread.
4237 * The target may say anything it wants to about the thread
4238 * (typically info about its blocked / runnable state, name, etc.).
4239 * This string will appear in the info threads display.
4241 * Optional: targets are not required to implement this function.
4245 remote_target::extra_thread_info (thread_info
*tp
)
4247 struct remote_state
*rs
= get_remote_state ();
4250 struct gdb_ext_thread_info threadinfo
;
4252 if (rs
->remote_desc
== 0) /* paranoia */
4253 internal_error (_("remote_threads_extra_info"));
4255 if (tp
->ptid
== magic_null_ptid
4256 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4257 /* This is the main thread which was added by GDB. The remote
4258 server doesn't know about it. */
4261 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4263 /* If already have cached info, use it. */
4264 if (!extra
.empty ())
4265 return extra
.c_str ();
4267 if (m_features
.packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4269 /* If we're using qXfer:threads:read, then the extra info is
4270 included in the XML. So if we didn't have anything cached,
4271 it's because there's really no extra info. */
4275 if (rs
->use_threadextra_query
)
4277 char *b
= rs
->buf
.data ();
4278 char *endb
= b
+ get_remote_packet_size ();
4280 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4282 write_ptid (b
, endb
, tp
->ptid
);
4286 if (rs
->buf
[0] != 0)
4288 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4289 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4290 return extra
.c_str ();
4294 /* If the above query fails, fall back to the old method. */
4295 rs
->use_threadextra_query
= 0;
4296 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4297 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4298 int_to_threadref (&id
, tp
->ptid
.lwp ());
4299 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4300 if (threadinfo
.active
)
4302 if (*threadinfo
.shortname
)
4303 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4304 if (*threadinfo
.display
)
4306 if (!extra
.empty ())
4308 string_appendf (extra
, " State: %s", threadinfo
.display
);
4310 if (*threadinfo
.more_display
)
4312 if (!extra
.empty ())
4314 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4316 return extra
.c_str ();
4323 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4324 struct static_tracepoint_marker
*marker
)
4326 struct remote_state
*rs
= get_remote_state ();
4327 char *p
= rs
->buf
.data ();
4329 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4331 p
+= hexnumstr (p
, addr
);
4334 p
= rs
->buf
.data ();
4337 error (_("Remote failure reply: %s"), p
);
4341 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4348 std::vector
<static_tracepoint_marker
>
4349 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4351 struct remote_state
*rs
= get_remote_state ();
4352 std::vector
<static_tracepoint_marker
> markers
;
4354 static_tracepoint_marker marker
;
4356 /* Ask for a first packet of static tracepoint marker
4360 p
= rs
->buf
.data ();
4362 error (_("Remote failure reply: %s"), p
);
4368 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4370 if (strid
== NULL
|| marker
.str_id
== strid
)
4371 markers
.push_back (std::move (marker
));
4373 while (*p
++ == ','); /* comma-separated list */
4374 /* Ask for another packet of static tracepoint definition. */
4377 p
= rs
->buf
.data ();
4384 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4387 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4389 return ptid_t (inferior_ptid
.pid (), lwp
);
4393 /* Restart the remote side; this is an extended protocol operation. */
4396 remote_target::extended_remote_restart ()
4398 struct remote_state
*rs
= get_remote_state ();
4400 /* Send the restart command; for reasons I don't understand the
4401 remote side really expects a number after the "R". */
4402 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4405 remote_fileio_reset ();
4408 /* Clean up connection to a remote debugger. */
4411 remote_target::close ()
4413 /* Make sure we leave stdin registered in the event loop. */
4416 trace_reset_local_state ();
4421 remote_target::~remote_target ()
4423 struct remote_state
*rs
= get_remote_state ();
4425 /* Check for NULL because we may get here with a partially
4426 constructed target/connection. */
4427 if (rs
->remote_desc
== nullptr)
4430 serial_close (rs
->remote_desc
);
4432 /* We are destroying the remote target, so we should discard
4433 everything of this target. */
4434 discard_pending_stop_replies_in_queue ();
4436 rs
->delete_async_event_handler ();
4438 delete rs
->notif_state
;
4441 /* Query the remote side for the text, data and bss offsets. */
4444 remote_target::get_offsets ()
4446 struct remote_state
*rs
= get_remote_state ();
4449 int lose
, num_segments
= 0, do_sections
, do_segments
;
4450 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4452 if (current_program_space
->symfile_object_file
== NULL
)
4455 putpkt ("qOffsets");
4457 buf
= rs
->buf
.data ();
4459 if (buf
[0] == '\000')
4460 return; /* Return silently. Stub doesn't support
4464 warning (_("Remote failure reply: %s"), buf
);
4468 /* Pick up each field in turn. This used to be done with scanf, but
4469 scanf will make trouble if CORE_ADDR size doesn't match
4470 conversion directives correctly. The following code will work
4471 with any size of CORE_ADDR. */
4472 text_addr
= data_addr
= bss_addr
= 0;
4476 if (startswith (ptr
, "Text="))
4479 /* Don't use strtol, could lose on big values. */
4480 while (*ptr
&& *ptr
!= ';')
4481 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4483 if (startswith (ptr
, ";Data="))
4486 while (*ptr
&& *ptr
!= ';')
4487 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4492 if (!lose
&& startswith (ptr
, ";Bss="))
4495 while (*ptr
&& *ptr
!= ';')
4496 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4498 if (bss_addr
!= data_addr
)
4499 warning (_("Target reported unsupported offsets: %s"), buf
);
4504 else if (startswith (ptr
, "TextSeg="))
4507 /* Don't use strtol, could lose on big values. */
4508 while (*ptr
&& *ptr
!= ';')
4509 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4512 if (startswith (ptr
, ";DataSeg="))
4515 while (*ptr
&& *ptr
!= ';')
4516 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4524 error (_("Malformed response to offset query, %s"), buf
);
4525 else if (*ptr
!= '\0')
4526 warning (_("Target reported unsupported offsets: %s"), buf
);
4528 objfile
*objf
= current_program_space
->symfile_object_file
;
4529 section_offsets offs
= objf
->section_offsets
;
4531 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
.get ());
4532 do_segments
= (data
!= NULL
);
4533 do_sections
= num_segments
== 0;
4535 if (num_segments
> 0)
4537 segments
[0] = text_addr
;
4538 segments
[1] = data_addr
;
4540 /* If we have two segments, we can still try to relocate everything
4541 by assuming that the .text and .data offsets apply to the whole
4542 text and data segments. Convert the offsets given in the packet
4543 to base addresses for symfile_map_offsets_to_segments. */
4544 else if (data
!= nullptr && data
->segments
.size () == 2)
4546 segments
[0] = data
->segments
[0].base
+ text_addr
;
4547 segments
[1] = data
->segments
[1].base
+ data_addr
;
4550 /* If the object file has only one segment, assume that it is text
4551 rather than data; main programs with no writable data are rare,
4552 but programs with no code are useless. Of course the code might
4553 have ended up in the data segment... to detect that we would need
4554 the permissions here. */
4555 else if (data
&& data
->segments
.size () == 1)
4557 segments
[0] = data
->segments
[0].base
+ text_addr
;
4560 /* There's no way to relocate by segment. */
4566 int ret
= symfile_map_offsets_to_segments (objf
->obfd
.get (),
4568 num_segments
, segments
);
4570 if (ret
== 0 && !do_sections
)
4571 error (_("Can not handle qOffsets TextSeg "
4572 "response with this symbol file"));
4580 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4582 /* This is a temporary kludge to force data and bss to use the
4583 same offsets because that's what nlmconv does now. The real
4584 solution requires changes to the stub and remote.c that I
4585 don't have time to do right now. */
4587 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4588 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4591 objfile_relocate (objf
, offs
);
4594 /* Send interrupt_sequence to remote target. */
4597 remote_target::send_interrupt_sequence ()
4599 struct remote_state
*rs
= get_remote_state ();
4601 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4602 remote_serial_write ("\x03", 1);
4603 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4604 serial_send_break (rs
->remote_desc
);
4605 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4607 serial_send_break (rs
->remote_desc
);
4608 remote_serial_write ("g", 1);
4611 internal_error (_("Invalid value for interrupt_sequence_mode: %s."),
4612 interrupt_sequence_mode
);
4616 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4617 and extract the PTID. Returns NULL_PTID if not found. */
4620 stop_reply_extract_thread (const char *stop_reply
)
4622 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4626 /* Txx r:val ; r:val (...) */
4629 /* Look for "register" named "thread". */
4634 p1
= strchr (p
, ':');
4638 if (strncmp (p
, "thread", p1
- p
) == 0)
4639 return read_ptid (++p1
, &p
);
4641 p1
= strchr (p
, ';');
4653 /* Determine the remote side's current thread. If we have a stop
4654 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4655 "thread" register we can extract the current thread from. If not,
4656 ask the remote which is the current thread with qC. The former
4657 method avoids a roundtrip. */
4660 remote_target::get_current_thread (const char *wait_status
)
4662 ptid_t ptid
= null_ptid
;
4664 /* Note we don't use remote_parse_stop_reply as that makes use of
4665 the target architecture, which we haven't yet fully determined at
4667 if (wait_status
!= NULL
)
4668 ptid
= stop_reply_extract_thread (wait_status
);
4669 if (ptid
== null_ptid
)
4670 ptid
= remote_current_thread (inferior_ptid
);
4675 /* Query the remote target for which is the current thread/process,
4676 add it to our tables, and update INFERIOR_PTID. The caller is
4677 responsible for setting the state such that the remote end is ready
4678 to return the current thread.
4680 This function is called after handling the '?' or 'vRun' packets,
4681 whose response is a stop reply from which we can also try
4682 extracting the thread. If the target doesn't support the explicit
4683 qC query, we infer the current thread from that stop reply, passed
4684 in in WAIT_STATUS, which may be NULL.
4686 The function returns pointer to the main thread of the inferior. */
4689 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4691 bool fake_pid_p
= false;
4693 switch_to_no_thread ();
4695 /* Now, if we have thread information, update the current thread's
4697 ptid_t curr_ptid
= get_current_thread (wait_status
);
4699 if (curr_ptid
!= null_ptid
)
4701 if (!m_features
.remote_multi_process_p ())
4706 /* Without this, some commands which require an active target
4707 (such as kill) won't work. This variable serves (at least)
4708 double duty as both the pid of the target process (if it has
4709 such), and as a flag indicating that a target is active. */
4710 curr_ptid
= magic_null_ptid
;
4714 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4716 /* Add the main thread and switch to it. Don't try reading
4717 registers yet, since we haven't fetched the target description
4719 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4720 switch_to_thread_no_regs (tp
);
4725 /* Print info about a thread that was found already stopped on
4729 remote_target::print_one_stopped_thread (thread_info
*thread
)
4731 target_waitstatus ws
;
4733 /* If there is a pending waitstatus, use it. If there isn't it's because
4734 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4735 and process_initial_stop_replies decided it wasn't interesting to save
4736 and report to the core. */
4737 if (thread
->has_pending_waitstatus ())
4739 ws
= thread
->pending_waitstatus ();
4740 thread
->clear_pending_waitstatus ();
4744 ws
.set_stopped (GDB_SIGNAL_0
);
4747 switch_to_thread (thread
);
4748 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4749 set_current_sal_from_frame (get_current_frame ());
4751 /* For "info program". */
4752 set_last_target_status (this, thread
->ptid
, ws
);
4754 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4756 enum gdb_signal sig
= ws
.sig ();
4758 if (signal_print_state (sig
))
4759 notify_signal_received (sig
);
4762 notify_normal_stop (nullptr, 1);
4765 /* Process all initial stop replies the remote side sent in response
4766 to the ? packet. These indicate threads that were already stopped
4767 on initial connection. We mark these threads as stopped and print
4768 their current frame before giving the user the prompt. */
4771 remote_target::process_initial_stop_replies (int from_tty
)
4773 int pending_stop_replies
= stop_reply_queue_length ();
4774 struct thread_info
*selected
= NULL
;
4775 struct thread_info
*lowest_stopped
= NULL
;
4776 struct thread_info
*first
= NULL
;
4778 /* This is only used when the target is non-stop. */
4779 gdb_assert (target_is_non_stop_p ());
4781 /* Consume the initial pending events. */
4782 while (pending_stop_replies
-- > 0)
4784 ptid_t waiton_ptid
= minus_one_ptid
;
4786 struct target_waitstatus ws
;
4787 int ignore_event
= 0;
4789 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4791 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4795 case TARGET_WAITKIND_IGNORE
:
4796 case TARGET_WAITKIND_NO_RESUMED
:
4797 case TARGET_WAITKIND_SIGNALLED
:
4798 case TARGET_WAITKIND_EXITED
:
4799 /* We shouldn't see these, but if we do, just ignore. */
4800 remote_debug_printf ("event ignored");
4811 thread_info
*evthread
= this->find_thread (event_ptid
);
4813 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4815 enum gdb_signal sig
= ws
.sig ();
4817 /* Stubs traditionally report SIGTRAP as initial signal,
4818 instead of signal 0. Suppress it. */
4819 if (sig
== GDB_SIGNAL_TRAP
)
4821 evthread
->set_stop_signal (sig
);
4822 ws
.set_stopped (sig
);
4825 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4826 || ws
.sig () != GDB_SIGNAL_0
)
4827 evthread
->set_pending_waitstatus (ws
);
4829 set_executing (this, event_ptid
, false);
4830 set_running (this, event_ptid
, false);
4831 get_remote_thread_info (evthread
)->set_not_resumed ();
4834 /* "Notice" the new inferiors before anything related to
4835 registers/memory. */
4836 for (inferior
*inf
: all_non_exited_inferiors (this))
4838 inf
->needs_setup
= true;
4842 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4843 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4848 /* If all-stop on top of non-stop, pause all threads. Note this
4849 records the threads' stop pc, so must be done after "noticing"
4854 /* At this point, the remote target is not async. It needs to be for
4855 the poll in stop_all_threads to consider events from it, so enable
4857 gdb_assert (!this->is_async_p ());
4858 SCOPE_EXIT
{ target_async (false); };
4859 target_async (true);
4860 stop_all_threads ("remote connect in all-stop");
4863 /* If all threads of an inferior were already stopped, we
4864 haven't setup the inferior yet. */
4865 for (inferior
*inf
: all_non_exited_inferiors (this))
4867 if (inf
->needs_setup
)
4869 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4870 switch_to_thread_no_regs (thread
);
4876 /* Now go over all threads that are stopped, and print their current
4877 frame. If all-stop, then if there's a signalled thread, pick
4879 for (thread_info
*thread
: all_non_exited_threads (this))
4885 thread
->set_running (false);
4886 else if (thread
->state
!= THREAD_STOPPED
)
4889 if (selected
== nullptr && thread
->has_pending_waitstatus ())
4892 if (lowest_stopped
== NULL
4893 || thread
->inf
->num
< lowest_stopped
->inf
->num
4894 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
4895 lowest_stopped
= thread
;
4898 print_one_stopped_thread (thread
);
4901 /* In all-stop, we only print the status of one thread, and leave
4902 others with their status pending. */
4905 thread_info
*thread
= selected
;
4907 thread
= lowest_stopped
;
4911 print_one_stopped_thread (thread
);
4915 /* Mark a remote_target as starting (by setting the starting_up flag within
4916 its remote_state) for the lifetime of this object. The reference count
4917 on the remote target is temporarily incremented, to prevent the target
4918 being deleted under our feet. */
4920 struct scoped_mark_target_starting
4922 /* Constructor, TARGET is the target to be marked as starting, its
4923 reference count will be incremented. */
4924 scoped_mark_target_starting (remote_target
*target
)
4925 : m_remote_target (remote_target_ref::new_reference (target
)),
4926 m_restore_starting_up (set_starting_up_flag (target
))
4931 /* Helper function, set the starting_up flag on TARGET and return an
4932 object which, when it goes out of scope, will restore the previous
4933 value of the starting_up flag. */
4934 static scoped_restore_tmpl
<bool>
4935 set_starting_up_flag (remote_target
*target
)
4937 remote_state
*rs
= target
->get_remote_state ();
4938 gdb_assert (!rs
->starting_up
);
4939 return make_scoped_restore (&rs
->starting_up
, true);
4942 /* A gdb::ref_ptr pointer to a remote_target. */
4943 using remote_target_ref
= gdb::ref_ptr
<remote_target
, target_ops_ref_policy
>;
4945 /* A reference to the target on which we are operating. */
4946 remote_target_ref m_remote_target
;
4948 /* An object which restores the previous value of the starting_up flag
4949 when it goes out of scope. */
4950 scoped_restore_tmpl
<bool> m_restore_starting_up
;
4953 /* Helper for remote_target::start_remote, start the remote connection and
4954 sync state. Return true if everything goes OK, otherwise, return false.
4955 This function exists so that the scoped_restore created within it will
4956 expire before we return to remote_target::start_remote. */
4959 remote_target::start_remote_1 (int from_tty
, int extended_p
)
4961 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
4963 struct remote_state
*rs
= get_remote_state ();
4965 /* Signal other parts that we're going through the initial setup,
4966 and so things may not be stable yet. E.g., we don't try to
4967 install tracepoints until we've relocated symbols. Also, a
4968 Ctrl-C before we're connected and synced up can't interrupt the
4969 target. Instead, it offers to drop the (potentially wedged)
4971 scoped_mark_target_starting
target_is_starting (this);
4975 if (interrupt_on_connect
)
4976 send_interrupt_sequence ();
4978 /* Ack any packet which the remote side has already sent. */
4979 remote_serial_write ("+", 1);
4981 /* The first packet we send to the target is the optional "supported
4982 packets" request. If the target can answer this, it will tell us
4983 which later probes to skip. */
4984 remote_query_supported ();
4986 /* Check vCont support and set the remote state's vCont_action_support
4988 remote_vcont_probe ();
4990 /* If the stub wants to get a QAllow, compose one and send it. */
4991 if (m_features
.packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
4994 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4995 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4996 as a reply to known packet. For packet "vFile:setfs:" it is an
4997 invalid reply and GDB would return error in
4998 remote_hostio_set_filesystem, making remote files access impossible.
4999 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
5000 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
5002 const char v_mustreplyempty
[] = "vMustReplyEmpty";
5004 putpkt (v_mustreplyempty
);
5006 if (strcmp (rs
->buf
.data (), "OK") == 0)
5008 m_features
.m_protocol_packets
[PACKET_vFile_setfs
].support
5011 else if (strcmp (rs
->buf
.data (), "") != 0)
5012 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
5016 /* Next, we possibly activate noack mode.
5018 If the QStartNoAckMode packet configuration is set to AUTO,
5019 enable noack mode if the stub reported a wish for it with
5022 If set to TRUE, then enable noack mode even if the stub didn't
5023 report it in qSupported. If the stub doesn't reply OK, the
5024 session ends with an error.
5026 If FALSE, then don't activate noack mode, regardless of what the
5027 stub claimed should be the default with qSupported. */
5029 if (m_features
.packet_support (PACKET_QStartNoAckMode
) != PACKET_DISABLE
)
5031 putpkt ("QStartNoAckMode");
5033 if (m_features
.packet_ok (rs
->buf
, PACKET_QStartNoAckMode
) == PACKET_OK
)
5039 /* Tell the remote that we are using the extended protocol. */
5044 /* Let the target know which signals it is allowed to pass down to
5046 update_signals_program_target ();
5048 /* Next, if the target can specify a description, read it. We do
5049 this before anything involving memory or registers. */
5050 target_find_description ();
5052 /* Next, now that we know something about the target, update the
5053 address spaces in the program spaces. */
5054 update_address_spaces ();
5056 /* On OSs where the list of libraries is global to all
5057 processes, we fetch them early. */
5058 if (gdbarch_has_global_solist (current_inferior ()->arch ()))
5059 solib_add (NULL
, from_tty
, auto_solib_add
);
5061 if (target_is_non_stop_p ())
5063 if (m_features
.packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
5064 error (_("Non-stop mode requested, but remote "
5065 "does not support non-stop"));
5067 putpkt ("QNonStop:1");
5070 if (strcmp (rs
->buf
.data (), "OK") != 0)
5071 error (_("Remote refused setting non-stop mode with: %s"),
5074 /* Find about threads and processes the stub is already
5075 controlling. We default to adding them in the running state.
5076 The '?' query below will then tell us about which threads are
5078 this->update_thread_list ();
5080 else if (m_features
.packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
5082 /* Don't assume that the stub can operate in all-stop mode.
5083 Request it explicitly. */
5084 putpkt ("QNonStop:0");
5087 if (strcmp (rs
->buf
.data (), "OK") != 0)
5088 error (_("Remote refused setting all-stop mode with: %s"),
5092 /* Upload TSVs regardless of whether the target is running or not. The
5093 remote stub, such as GDBserver, may have some predefined or builtin
5094 TSVs, even if the target is not running. */
5095 if (get_trace_status (current_trace_status ()) != -1)
5097 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
5099 upload_trace_state_variables (&uploaded_tsvs
);
5100 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
5103 /* Check whether the target is running now. */
5107 if (!target_is_non_stop_p ())
5109 char *wait_status
= NULL
;
5111 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
5114 error (_("The target is not running (try extended-remote?)"));
5119 /* Save the reply for later. */
5120 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
5121 strcpy (wait_status
, rs
->buf
.data ());
5124 /* Fetch thread list. */
5125 target_update_thread_list ();
5127 /* Let the stub know that we want it to return the thread. */
5128 set_continue_thread (minus_one_ptid
);
5130 if (thread_count (this) == 0)
5132 /* Target has no concept of threads at all. GDB treats
5133 non-threaded target as single-threaded; add a main
5135 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
5136 get_remote_thread_info (tp
)->set_resumed ();
5140 /* We have thread information; select the thread the target
5141 says should be current. If we're reconnecting to a
5142 multi-threaded program, this will ideally be the thread
5143 that last reported an event before GDB disconnected. */
5144 ptid_t curr_thread
= get_current_thread (wait_status
);
5145 if (curr_thread
== null_ptid
)
5147 /* Odd... The target was able to list threads, but not
5148 tell us which thread was current (no "thread"
5149 register in T stop reply?). Just pick the first
5150 thread in the thread list then. */
5152 remote_debug_printf ("warning: couldn't determine remote "
5153 "current thread; picking first in list.");
5155 for (thread_info
*tp
: all_non_exited_threads (this,
5158 switch_to_thread (tp
);
5163 switch_to_thread (this->find_thread (curr_thread
));
5166 /* init_wait_for_inferior should be called before get_offsets in order
5167 to manage `inserted' flag in bp loc in a correct state.
5168 breakpoint_init_inferior, called from init_wait_for_inferior, set
5169 `inserted' flag to 0, while before breakpoint_re_set, called from
5170 start_remote, set `inserted' flag to 1. In the initialization of
5171 inferior, breakpoint_init_inferior should be called first, and then
5172 breakpoint_re_set can be called. If this order is broken, state of
5173 `inserted' flag is wrong, and cause some problems on breakpoint
5175 init_wait_for_inferior ();
5177 get_offsets (); /* Get text, data & bss offsets. */
5179 /* If we could not find a description using qXfer, and we know
5180 how to do it some other way, try again. This is not
5181 supported for non-stop; it could be, but it is tricky if
5182 there are no stopped threads when we connect. */
5183 if (remote_read_description_p (this)
5184 && gdbarch_target_desc (current_inferior ()->arch ()) == NULL
)
5186 target_clear_description ();
5187 target_find_description ();
5190 /* Use the previously fetched status. */
5191 gdb_assert (wait_status
!= NULL
);
5192 struct notif_event
*reply
5193 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
5194 push_stop_reply ((struct stop_reply
*) reply
);
5196 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
5200 /* Clear WFI global state. Do this before finding about new
5201 threads and inferiors, and setting the current inferior.
5202 Otherwise we would clear the proceed status of the current
5203 inferior when we want its stop_soon state to be preserved
5204 (see notice_new_inferior). */
5205 init_wait_for_inferior ();
5207 /* In non-stop, we will either get an "OK", meaning that there
5208 are no stopped threads at this time; or, a regular stop
5209 reply. In the latter case, there may be more than one thread
5210 stopped --- we pull them all out using the vStopped
5212 if (strcmp (rs
->buf
.data (), "OK") != 0)
5214 const notif_client
*notif
= ¬if_client_stop
;
5216 /* remote_notif_get_pending_replies acks this one, and gets
5218 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
5219 = remote_notif_parse (this, notif
, rs
->buf
.data ());
5220 remote_notif_get_pending_events (notif
);
5223 if (thread_count (this) == 0)
5226 error (_("The target is not running (try extended-remote?)"));
5230 /* Report all signals during attach/startup. */
5233 /* If there are already stopped threads, mark them stopped and
5234 report their stops before giving the prompt to the user. */
5235 process_initial_stop_replies (from_tty
);
5237 if (target_can_async_p ())
5238 target_async (true);
5241 /* Give the target a chance to look up symbols. */
5242 for (inferior
*inf
: all_inferiors (this))
5244 /* The inferiors that exist at this point were created from what
5245 was found already running on the remote side, so we know they
5247 gdb_assert (this->has_execution (inf
));
5249 /* No use without a symbol-file. */
5250 if (inf
->pspace
->symfile_object_file
== nullptr)
5253 /* Need to switch to a specific thread, because remote_check_symbols
5254 uses INFERIOR_PTID to set the general thread. */
5255 scoped_restore_current_thread restore_thread
;
5256 thread_info
*thread
= any_thread_of_inferior (inf
);
5257 switch_to_thread (thread
);
5258 this->remote_check_symbols ();
5261 /* Possibly the target has been engaged in a trace run started
5262 previously; find out where things are at. */
5263 if (get_trace_status (current_trace_status ()) != -1)
5265 struct uploaded_tp
*uploaded_tps
= NULL
;
5267 if (current_trace_status ()->running
)
5268 gdb_printf (_("Trace is already running on the target.\n"));
5270 upload_tracepoints (&uploaded_tps
);
5272 merge_uploaded_tracepoints (&uploaded_tps
);
5275 /* Possibly the target has been engaged in a btrace record started
5276 previously; find out where things are at. */
5277 remote_btrace_maybe_reopen ();
5282 /* Start the remote connection and sync state. */
5285 remote_target::start_remote (int from_tty
, int extended_p
)
5287 if (start_remote_1 (from_tty
, extended_p
)
5288 && breakpoints_should_be_inserted_now ())
5289 insert_breakpoints ();
5293 remote_target::connection_string ()
5295 remote_state
*rs
= get_remote_state ();
5297 if (rs
->remote_desc
->name
!= NULL
)
5298 return rs
->remote_desc
->name
;
5303 /* Open a connection to a remote debugger.
5304 NAME is the filename used for communication. */
5307 remote_target::open (const char *name
, int from_tty
)
5309 open_1 (name
, from_tty
, 0);
5312 /* Open a connection to a remote debugger using the extended
5313 remote gdb protocol. NAME is the filename used for communication. */
5316 extended_remote_target::open (const char *name
, int from_tty
)
5318 open_1 (name
, from_tty
, 1 /*extended_p */);
5322 remote_features::reset_all_packet_configs_support ()
5326 for (i
= 0; i
< PACKET_MAX
; i
++)
5327 m_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5330 /* Initialize all packet configs. */
5333 init_all_packet_configs (void)
5337 for (i
= 0; i
< PACKET_MAX
; i
++)
5339 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5340 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5344 /* Symbol look-up. */
5347 remote_target::remote_check_symbols ()
5352 /* It doesn't make sense to send a qSymbol packet for an inferior that
5353 doesn't have execution, because the remote side doesn't know about
5354 inferiors without execution. */
5355 gdb_assert (target_has_execution ());
5357 if (m_features
.packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5360 /* Make sure the remote is pointing at the right process. Note
5361 there's no way to select "no process". */
5362 set_general_process ();
5364 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5365 because we need both at the same time. */
5366 gdb::char_vector
msg (get_remote_packet_size ());
5367 gdb::char_vector
reply (get_remote_packet_size ());
5369 /* Invite target to request symbol lookups. */
5371 putpkt ("qSymbol::");
5373 m_features
.packet_ok (reply
, PACKET_qSymbol
);
5375 while (startswith (reply
.data (), "qSymbol:"))
5377 struct bound_minimal_symbol sym
;
5380 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5383 sym
= lookup_minimal_symbol (msg
.data (), NULL
, NULL
);
5384 if (sym
.minsym
== NULL
)
5385 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5389 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
5390 CORE_ADDR sym_addr
= sym
.value_address ();
5392 /* If this is a function address, return the start of code
5393 instead of any data function descriptor. */
5394 sym_addr
= gdbarch_convert_from_func_ptr_addr
5395 (current_inferior ()->arch (), sym_addr
,
5396 current_inferior ()->top_target ());
5398 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5399 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5402 putpkt (msg
.data ());
5407 static struct serial
*
5408 remote_serial_open (const char *name
)
5410 static int udp_warning
= 0;
5412 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5413 of in ser-tcp.c, because it is the remote protocol assuming that the
5414 serial connection is reliable and not the serial connection promising
5416 if (!udp_warning
&& startswith (name
, "udp:"))
5418 warning (_("The remote protocol may be unreliable over UDP.\n"
5419 "Some events may be lost, rendering further debugging "
5424 return serial_open (name
);
5427 /* Inform the target of our permission settings. The permission flags
5428 work without this, but if the target knows the settings, it can do
5429 a couple things. First, it can add its own check, to catch cases
5430 that somehow manage to get by the permissions checks in target
5431 methods. Second, if the target is wired to disallow particular
5432 settings (for instance, a system in the field that is not set up to
5433 be able to stop at a breakpoint), it can object to any unavailable
5437 remote_target::set_permissions ()
5439 struct remote_state
*rs
= get_remote_state ();
5441 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5442 "WriteReg:%x;WriteMem:%x;"
5443 "InsertBreak:%x;InsertTrace:%x;"
5444 "InsertFastTrace:%x;Stop:%x",
5445 may_write_registers
, may_write_memory
,
5446 may_insert_breakpoints
, may_insert_tracepoints
,
5447 may_insert_fast_tracepoints
, may_stop
);
5451 /* If the target didn't like the packet, warn the user. Do not try
5452 to undo the user's settings, that would just be maddening. */
5453 if (strcmp (rs
->buf
.data (), "OK") != 0)
5454 warning (_("Remote refused setting permissions with: %s"),
5458 /* This type describes each known response to the qSupported
5460 struct protocol_feature
5462 /* The name of this protocol feature. */
5465 /* The default for this protocol feature. */
5466 enum packet_support default_support
;
5468 /* The function to call when this feature is reported, or after
5469 qSupported processing if the feature is not supported.
5470 The first argument points to this structure. The second
5471 argument indicates whether the packet requested support be
5472 enabled, disabled, or probed (or the default, if this function
5473 is being called at the end of processing and this feature was
5474 not reported). The third argument may be NULL; if not NULL, it
5475 is a NUL-terminated string taken from the packet following
5476 this feature's name and an equals sign. */
5477 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5478 enum packet_support
, const char *);
5480 /* The corresponding packet for this feature. Only used if
5481 FUNC is remote_supported_packet. */
5486 remote_supported_packet (remote_target
*remote
,
5487 const struct protocol_feature
*feature
,
5488 enum packet_support support
,
5489 const char *argument
)
5493 warning (_("Remote qSupported response supplied an unexpected value for"
5494 " \"%s\"."), feature
->name
);
5498 remote
->m_features
.m_protocol_packets
[feature
->packet
].support
= support
;
5502 remote_target::remote_packet_size (const protocol_feature
*feature
,
5503 enum packet_support support
, const char *value
)
5505 struct remote_state
*rs
= get_remote_state ();
5510 if (support
!= PACKET_ENABLE
)
5513 if (value
== NULL
|| *value
== '\0')
5515 warning (_("Remote target reported \"%s\" without a size."),
5521 packet_size
= strtol (value
, &value_end
, 16);
5522 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5524 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5525 feature
->name
, value
);
5529 /* Record the new maximum packet size. */
5530 rs
->explicit_packet_size
= packet_size
;
5534 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5535 enum packet_support support
, const char *value
)
5537 remote
->remote_packet_size (feature
, support
, value
);
5540 static const struct protocol_feature remote_protocol_features
[] = {
5541 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5542 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5543 PACKET_qXfer_auxv
},
5544 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5545 PACKET_qXfer_exec_file
},
5546 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5547 PACKET_qXfer_features
},
5548 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5549 PACKET_qXfer_libraries
},
5550 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5551 PACKET_qXfer_libraries_svr4
},
5552 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5553 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5554 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5555 PACKET_qXfer_memory_map
},
5556 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5557 PACKET_qXfer_osdata
},
5558 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5559 PACKET_qXfer_threads
},
5560 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5561 PACKET_qXfer_traceframe_info
},
5562 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5563 PACKET_QPassSignals
},
5564 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5565 PACKET_QCatchSyscalls
},
5566 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5567 PACKET_QProgramSignals
},
5568 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5569 PACKET_QSetWorkingDir
},
5570 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5571 PACKET_QStartupWithShell
},
5572 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5573 PACKET_QEnvironmentHexEncoded
},
5574 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5575 PACKET_QEnvironmentReset
},
5576 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5577 PACKET_QEnvironmentUnset
},
5578 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5579 PACKET_QStartNoAckMode
},
5580 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5581 PACKET_multiprocess_feature
},
5582 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5583 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5584 PACKET_qXfer_siginfo_read
},
5585 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5586 PACKET_qXfer_siginfo_write
},
5587 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5588 PACKET_ConditionalTracepoints
},
5589 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5590 PACKET_ConditionalBreakpoints
},
5591 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5592 PACKET_BreakpointCommands
},
5593 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5594 PACKET_FastTracepoints
},
5595 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5596 PACKET_StaticTracepoints
},
5597 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5598 PACKET_InstallInTrace
},
5599 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5600 PACKET_DisconnectedTracing_feature
},
5601 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5603 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5605 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5606 PACKET_TracepointSource
},
5607 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5609 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5610 PACKET_EnableDisableTracepoints_feature
},
5611 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5612 PACKET_qXfer_fdpic
},
5613 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5615 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5616 PACKET_QDisableRandomization
},
5617 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5618 { "QTBuffer:size", PACKET_DISABLE
,
5619 remote_supported_packet
, PACKET_QTBuffer_size
},
5620 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5621 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5622 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5623 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5624 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5625 PACKET_qXfer_btrace
},
5626 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5627 PACKET_qXfer_btrace_conf
},
5628 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5629 PACKET_Qbtrace_conf_bts_size
},
5630 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5631 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5632 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5633 PACKET_fork_event_feature
},
5634 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5635 PACKET_vfork_event_feature
},
5636 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5637 PACKET_exec_event_feature
},
5638 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5639 PACKET_Qbtrace_conf_pt_size
},
5640 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5641 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5642 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5643 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5644 PACKET_memory_tagging_feature
},
5647 static char *remote_support_xml
;
5649 /* Register string appended to "xmlRegisters=" in qSupported query. */
5652 register_remote_support_xml (const char *xml
)
5654 #if defined(HAVE_LIBEXPAT)
5655 if (remote_support_xml
== NULL
)
5656 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5659 char *copy
= xstrdup (remote_support_xml
+ 13);
5661 char *p
= strtok_r (copy
, ",", &saveptr
);
5665 if (strcmp (p
, xml
) == 0)
5672 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5675 remote_support_xml
= reconcat (remote_support_xml
,
5676 remote_support_xml
, ",", xml
,
5683 remote_query_supported_append (std::string
*msg
, const char *append
)
5687 msg
->append (append
);
5691 remote_target::remote_query_supported ()
5693 struct remote_state
*rs
= get_remote_state ();
5696 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5698 /* The packet support flags are handled differently for this packet
5699 than for most others. We treat an error, a disabled packet, and
5700 an empty response identically: any features which must be reported
5701 to be used will be automatically disabled. An empty buffer
5702 accomplishes this, since that is also the representation for a list
5703 containing no features. */
5706 if (m_features
.packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5710 if (m_features
.packet_set_cmd_state (PACKET_multiprocess_feature
)
5711 != AUTO_BOOLEAN_FALSE
)
5712 remote_query_supported_append (&q
, "multiprocess+");
5714 if (m_features
.packet_set_cmd_state (PACKET_swbreak_feature
)
5715 != AUTO_BOOLEAN_FALSE
)
5716 remote_query_supported_append (&q
, "swbreak+");
5718 if (m_features
.packet_set_cmd_state (PACKET_hwbreak_feature
)
5719 != AUTO_BOOLEAN_FALSE
)
5720 remote_query_supported_append (&q
, "hwbreak+");
5722 remote_query_supported_append (&q
, "qRelocInsn+");
5724 if (m_features
.packet_set_cmd_state (PACKET_fork_event_feature
)
5725 != AUTO_BOOLEAN_FALSE
)
5726 remote_query_supported_append (&q
, "fork-events+");
5728 if (m_features
.packet_set_cmd_state (PACKET_vfork_event_feature
)
5729 != AUTO_BOOLEAN_FALSE
)
5730 remote_query_supported_append (&q
, "vfork-events+");
5732 if (m_features
.packet_set_cmd_state (PACKET_exec_event_feature
)
5733 != AUTO_BOOLEAN_FALSE
)
5734 remote_query_supported_append (&q
, "exec-events+");
5736 if (m_features
.packet_set_cmd_state (PACKET_vContSupported
)
5737 != AUTO_BOOLEAN_FALSE
)
5738 remote_query_supported_append (&q
, "vContSupported+");
5740 if (m_features
.packet_set_cmd_state (PACKET_QThreadEvents
)
5741 != AUTO_BOOLEAN_FALSE
)
5742 remote_query_supported_append (&q
, "QThreadEvents+");
5744 if (m_features
.packet_set_cmd_state (PACKET_no_resumed
)
5745 != AUTO_BOOLEAN_FALSE
)
5746 remote_query_supported_append (&q
, "no-resumed+");
5748 if (m_features
.packet_set_cmd_state (PACKET_memory_tagging_feature
)
5749 != AUTO_BOOLEAN_FALSE
)
5750 remote_query_supported_append (&q
, "memory-tagging+");
5752 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5753 the qSupported:xmlRegisters=i386 handling. */
5754 if (remote_support_xml
!= NULL
5755 && (m_features
.packet_support (PACKET_qXfer_features
)
5757 remote_query_supported_append (&q
, remote_support_xml
);
5759 q
= "qSupported:" + q
;
5760 putpkt (q
.c_str ());
5764 /* If an error occurred, warn, but do not return - just reset the
5765 buffer to empty and go on to disable features. */
5766 if (m_features
.packet_ok (rs
->buf
, PACKET_qSupported
) == PACKET_ERROR
)
5768 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
5773 memset (seen
, 0, sizeof (seen
));
5775 next
= rs
->buf
.data ();
5778 enum packet_support is_supported
;
5779 char *p
, *end
, *name_end
, *value
;
5781 /* First separate out this item from the rest of the packet. If
5782 there's another item after this, we overwrite the separator
5783 (terminated strings are much easier to work with). */
5785 end
= strchr (p
, ';');
5788 end
= p
+ strlen (p
);
5798 warning (_("empty item in \"qSupported\" response"));
5803 name_end
= strchr (p
, '=');
5806 /* This is a name=value entry. */
5807 is_supported
= PACKET_ENABLE
;
5808 value
= name_end
+ 1;
5817 is_supported
= PACKET_ENABLE
;
5821 is_supported
= PACKET_DISABLE
;
5825 is_supported
= PACKET_SUPPORT_UNKNOWN
;
5829 warning (_("unrecognized item \"%s\" "
5830 "in \"qSupported\" response"), p
);
5836 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5837 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
5839 const struct protocol_feature
*feature
;
5842 feature
= &remote_protocol_features
[i
];
5843 feature
->func (this, feature
, is_supported
, value
);
5848 /* If we increased the packet size, make sure to increase the global
5849 buffer size also. We delay this until after parsing the entire
5850 qSupported packet, because this is the same buffer we were
5852 if (rs
->buf
.size () < rs
->explicit_packet_size
)
5853 rs
->buf
.resize (rs
->explicit_packet_size
);
5855 /* Handle the defaults for unmentioned features. */
5856 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5859 const struct protocol_feature
*feature
;
5861 feature
= &remote_protocol_features
[i
];
5862 feature
->func (this, feature
, feature
->default_support
, NULL
);
5866 /* Serial QUIT handler for the remote serial descriptor.
5868 Defers handling a Ctrl-C until we're done with the current
5869 command/response packet sequence, unless:
5871 - We're setting up the connection. Don't send a remote interrupt
5872 request, as we're not fully synced yet. Quit immediately
5875 - The target has been resumed in the foreground
5876 (target_terminal::is_ours is false) with a synchronous resume
5877 packet, and we're blocked waiting for the stop reply, thus a
5878 Ctrl-C should be immediately sent to the target.
5880 - We get a second Ctrl-C while still within the same serial read or
5881 write. In that case the serial is seemingly wedged --- offer to
5884 - We see a second Ctrl-C without target response, after having
5885 previously interrupted the target. In that case the target/stub
5886 is probably wedged --- offer to quit/disconnect.
5890 remote_target::remote_serial_quit_handler ()
5892 struct remote_state
*rs
= get_remote_state ();
5894 if (check_quit_flag ())
5896 /* If we're starting up, we're not fully synced yet. Quit
5898 if (rs
->starting_up
)
5900 else if (rs
->got_ctrlc_during_io
)
5902 if (query (_("The target is not responding to GDB commands.\n"
5903 "Stop debugging it? ")))
5904 remote_unpush_and_throw (this);
5906 /* If ^C has already been sent once, offer to disconnect. */
5907 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
5909 /* All-stop protocol, and blocked waiting for stop reply. Send
5910 an interrupt request. */
5911 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
5912 target_interrupt ();
5914 rs
->got_ctrlc_during_io
= 1;
5918 /* The remote_target that is current while the quit handler is
5919 overridden with remote_serial_quit_handler. */
5920 static remote_target
*curr_quit_handler_target
;
5923 remote_serial_quit_handler ()
5925 curr_quit_handler_target
->remote_serial_quit_handler ();
5928 /* Remove the remote target from the target stack of each inferior
5929 that is using it. Upper targets depend on it so remove them
5933 remote_unpush_target (remote_target
*target
)
5935 /* We have to unpush the target from all inferiors, even those that
5937 scoped_restore_current_inferior restore_current_inferior
;
5939 for (inferior
*inf
: all_inferiors (target
))
5941 switch_to_inferior_no_thread (inf
);
5942 inf
->pop_all_targets_at_and_above (process_stratum
);
5943 generic_mourn_inferior ();
5946 /* Don't rely on target_close doing this when the target is popped
5947 from the last remote inferior above, because something may be
5948 holding a reference to the target higher up on the stack, meaning
5949 target_close won't be called yet. We lost the connection to the
5950 target, so clear these now, otherwise we may later throw
5951 TARGET_CLOSE_ERROR while trying to tell the remote target to
5953 fileio_handles_invalidate_target (target
);
5957 remote_unpush_and_throw (remote_target
*target
)
5959 remote_unpush_target (target
);
5960 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
5964 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
5966 remote_target
*curr_remote
= get_current_remote_target ();
5969 error (_("To open a remote debug connection, you need to specify what\n"
5970 "serial device is attached to the remote system\n"
5971 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
5973 /* If we're connected to a running target, target_preopen will kill it.
5974 Ask this question first, before target_preopen has a chance to kill
5976 if (curr_remote
!= NULL
&& !target_has_execution ())
5979 && !query (_("Already connected to a remote target. Disconnect? ")))
5980 error (_("Still connected."));
5983 /* Here the possibly existing remote target gets unpushed. */
5984 target_preopen (from_tty
);
5986 remote_fileio_reset ();
5987 reopen_exec_file ();
5988 reread_symbols (from_tty
);
5990 remote_target
*remote
5991 = (extended_p
? new extended_remote_target () : new remote_target ());
5992 target_ops_up
target_holder (remote
);
5994 remote_state
*rs
= remote
->get_remote_state ();
5996 /* See FIXME above. */
5997 if (!target_async_permitted
)
5998 rs
->wait_forever_enabled_p
= true;
6000 rs
->remote_desc
= remote_serial_open (name
);
6001 if (!rs
->remote_desc
)
6002 perror_with_name (name
);
6004 if (baud_rate
!= -1)
6006 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
6008 /* The requested speed could not be set. Error out to
6009 top level after closing remote_desc. Take care to
6010 set remote_desc to NULL to avoid closing remote_desc
6012 serial_close (rs
->remote_desc
);
6013 rs
->remote_desc
= NULL
;
6014 perror_with_name (name
);
6018 serial_setparity (rs
->remote_desc
, serial_parity
);
6019 serial_raw (rs
->remote_desc
);
6021 /* If there is something sitting in the buffer we might take it as a
6022 response to a command, which would be bad. */
6023 serial_flush_input (rs
->remote_desc
);
6027 gdb_puts ("Remote debugging using ");
6032 /* Switch to using the remote target now. */
6033 current_inferior ()->push_target (std::move (target_holder
));
6035 /* Register extra event sources in the event loop. */
6036 rs
->create_async_event_handler ();
6038 rs
->notif_state
= remote_notif_state_allocate (remote
);
6040 /* Reset the target state; these things will be queried either by
6041 remote_query_supported or as they are needed. */
6042 remote
->m_features
.reset_all_packet_configs_support ();
6043 rs
->explicit_packet_size
= 0;
6045 rs
->extended
= extended_p
;
6046 rs
->waiting_for_stop_reply
= 0;
6047 rs
->ctrlc_pending_p
= 0;
6048 rs
->got_ctrlc_during_io
= 0;
6050 rs
->general_thread
= not_sent_ptid
;
6051 rs
->continue_thread
= not_sent_ptid
;
6052 rs
->remote_traceframe_number
= -1;
6054 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
6056 /* Probe for ability to use "ThreadInfo" query, as required. */
6057 rs
->use_threadinfo_query
= 1;
6058 rs
->use_threadextra_query
= 1;
6060 rs
->readahead_cache
.invalidate ();
6062 if (target_async_permitted
)
6064 /* FIXME: cagney/1999-09-23: During the initial connection it is
6065 assumed that the target is already ready and able to respond to
6066 requests. Unfortunately remote_start_remote() eventually calls
6067 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
6068 around this. Eventually a mechanism that allows
6069 wait_for_inferior() to expect/get timeouts will be
6071 rs
->wait_forever_enabled_p
= false;
6074 /* First delete any symbols previously loaded from shared libraries. */
6075 no_shared_libraries (NULL
, 0);
6077 /* Start the remote connection. If error() or QUIT, discard this
6078 target (we'd otherwise be in an inconsistent state) and then
6079 propogate the error on up the exception chain. This ensures that
6080 the caller doesn't stumble along blindly assuming that the
6081 function succeeded. The CLI doesn't have this problem but other
6082 UI's, such as MI do.
6084 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
6085 this function should return an error indication letting the
6086 caller restore the previous state. Unfortunately the command
6087 ``target remote'' is directly wired to this function making that
6088 impossible. On a positive note, the CLI side of this problem has
6089 been fixed - the function set_cmd_context() makes it possible for
6090 all the ``target ....'' commands to share a common callback
6091 function. See cli-dump.c. */
6096 remote
->start_remote (from_tty
, extended_p
);
6098 catch (const gdb_exception
&ex
)
6100 /* Pop the partially set up target - unless something else did
6101 already before throwing the exception. */
6102 if (ex
.error
!= TARGET_CLOSE_ERROR
)
6103 remote_unpush_target (remote
);
6108 remote_btrace_reset (rs
);
6110 if (target_async_permitted
)
6111 rs
->wait_forever_enabled_p
= true;
6114 /* Determine if WS represents a fork status. */
6117 is_fork_status (target_waitkind kind
)
6119 return (kind
== TARGET_WAITKIND_FORKED
6120 || kind
== TARGET_WAITKIND_VFORKED
);
6123 /* Return THREAD's pending status if it is a pending fork parent, else
6126 static const target_waitstatus
*
6127 thread_pending_fork_status (struct thread_info
*thread
)
6129 const target_waitstatus
&ws
6130 = (thread
->has_pending_waitstatus ()
6131 ? thread
->pending_waitstatus ()
6132 : thread
->pending_follow
);
6134 if (!is_fork_status (ws
.kind ()))
6140 /* Detach the specified process. */
6143 remote_target::remote_detach_pid (int pid
)
6145 struct remote_state
*rs
= get_remote_state ();
6147 /* This should not be necessary, but the handling for D;PID in
6148 GDBserver versions prior to 8.2 incorrectly assumes that the
6149 selected process points to the same process we're detaching,
6150 leading to misbehavior (and possibly GDBserver crashing) when it
6151 does not. Since it's easy and cheap, work around it by forcing
6152 GDBserver to select GDB's current process. */
6153 set_general_process ();
6155 if (m_features
.remote_multi_process_p ())
6156 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
6158 strcpy (rs
->buf
.data (), "D");
6163 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
6165 else if (rs
->buf
[0] == '\0')
6166 error (_("Remote doesn't know how to detach"));
6169 /* It is possible that we have an unprocessed exit event for this
6170 pid. If this is the case then we can ignore the failure to detach
6171 and just pretend that the detach worked, as far as the user is
6172 concerned, the process exited immediately after the detach. */
6173 bool process_has_already_exited
= false;
6174 remote_notif_get_pending_events (¬if_client_stop
);
6175 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6177 if (reply
->ptid
.pid () != pid
)
6180 enum target_waitkind kind
= reply
->ws
.kind ();
6181 if (kind
== TARGET_WAITKIND_EXITED
6182 || kind
== TARGET_WAITKIND_SIGNALLED
)
6184 process_has_already_exited
= true;
6186 ("detach failed, but process already exited");
6191 if (!process_has_already_exited
)
6192 error (_("can't detach process: %s"), (char *) rs
->buf
.data ());
6196 /* This detaches a program to which we previously attached, using
6197 inferior_ptid to identify the process. After this is done, GDB
6198 can be used to debug some other program. We better not have left
6199 any breakpoints in the target program or it'll die when it hits
6203 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
6205 int pid
= inferior_ptid
.pid ();
6206 struct remote_state
*rs
= get_remote_state ();
6209 if (!target_has_execution ())
6210 error (_("No process to detach from."));
6212 target_announce_detach (from_tty
);
6214 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
6216 /* If we're in breakpoints-always-inserted mode, or the inferior
6217 is running, we have to remove breakpoints before detaching.
6218 We don't do this in common code instead because not all
6219 targets support removing breakpoints while the target is
6220 running. The remote target / gdbserver does, though. */
6221 remove_breakpoints_inf (current_inferior ());
6224 /* Tell the remote target to detach. */
6225 remote_detach_pid (pid
);
6227 /* Exit only if this is the only active inferior. */
6228 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
6229 gdb_puts (_("Ending remote debugging.\n"));
6231 /* See if any thread of the inferior we are detaching has a pending fork
6232 status. In that case, we must detach from the child resulting from
6234 for (thread_info
*thread
: inf
->non_exited_threads ())
6236 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
6241 remote_detach_pid (ws
->child_ptid ().pid ());
6244 /* Check also for any pending fork events in the stop reply queue. */
6245 remote_notif_get_pending_events (¬if_client_stop
);
6246 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6248 if (reply
->ptid
.pid () != pid
)
6251 if (!is_fork_status (reply
->ws
.kind ()))
6254 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
6257 thread_info
*tp
= this->find_thread (inferior_ptid
);
6259 /* Check to see if we are detaching a fork parent. Note that if we
6260 are detaching a fork child, tp == NULL. */
6261 is_fork_parent
= (tp
!= NULL
6262 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
6264 /* If doing detach-on-fork, we don't mourn, because that will delete
6265 breakpoints that should be available for the followed inferior. */
6266 if (!is_fork_parent
)
6268 /* Save the pid as a string before mourning, since that will
6269 unpush the remote target, and we need the string after. */
6270 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6272 target_mourn_inferior (inferior_ptid
);
6273 if (print_inferior_events
)
6274 gdb_printf (_("[Inferior %d (%s) detached]\n"),
6275 inf
->num
, infpid
.c_str ());
6279 switch_to_no_thread ();
6280 detach_inferior (current_inferior ());
6285 remote_target::detach (inferior
*inf
, int from_tty
)
6287 remote_detach_1 (inf
, from_tty
);
6291 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6293 remote_detach_1 (inf
, from_tty
);
6296 /* Target follow-fork function for remote targets. On entry, and
6297 at return, the current inferior is the fork parent.
6299 Note that although this is currently only used for extended-remote,
6300 it is named remote_follow_fork in anticipation of using it for the
6301 remote target as well. */
6304 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6305 target_waitkind fork_kind
, bool follow_child
,
6308 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6309 fork_kind
, follow_child
, detach_fork
);
6311 if ((fork_kind
== TARGET_WAITKIND_FORKED
6312 && m_features
.remote_fork_event_p ())
6313 || (fork_kind
== TARGET_WAITKIND_VFORKED
6314 && m_features
.remote_vfork_event_p ()))
6316 /* When following the parent and detaching the child, we detach
6317 the child here. For the case of following the child and
6318 detaching the parent, the detach is done in the target-
6319 independent follow fork code in infrun.c. We can't use
6320 target_detach when detaching an unfollowed child because
6321 the client side doesn't know anything about the child. */
6322 if (detach_fork
&& !follow_child
)
6324 /* Detach the fork child. */
6325 remote_detach_pid (child_ptid
.pid ());
6330 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6331 in the program space of the new inferior. */
6334 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6335 const char *execd_pathname
)
6337 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6339 /* We know that this is a target file name, so if it has the "target:"
6340 prefix we strip it off before saving it in the program space. */
6341 if (is_target_filename (execd_pathname
))
6342 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6344 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6347 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6350 remote_target::disconnect (const char *args
, int from_tty
)
6353 error (_("Argument given to \"disconnect\" when remotely debugging."));
6355 /* Make sure we unpush even the extended remote targets. Calling
6356 target_mourn_inferior won't unpush, and
6357 remote_target::mourn_inferior won't unpush if there is more than
6358 one inferior left. */
6359 remote_unpush_target (this);
6362 gdb_puts ("Ending remote debugging.\n");
6365 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6366 be chatty about it. */
6369 extended_remote_target::attach (const char *args
, int from_tty
)
6371 struct remote_state
*rs
= get_remote_state ();
6373 char *wait_status
= NULL
;
6375 pid
= parse_pid_to_attach (args
);
6377 /* Remote PID can be freely equal to getpid, do not check it here the same
6378 way as in other targets. */
6380 if (m_features
.packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6381 error (_("This target does not support attaching to a process"));
6383 target_announce_attach (from_tty
, pid
);
6385 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6389 switch (m_features
.packet_ok (rs
->buf
, PACKET_vAttach
))
6392 if (!target_is_non_stop_p ())
6394 /* Save the reply for later. */
6395 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6396 strcpy (wait_status
, rs
->buf
.data ());
6398 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6399 error (_("Attaching to %s failed with: %s"),
6400 target_pid_to_str (ptid_t (pid
)).c_str (),
6403 case PACKET_UNKNOWN
:
6404 error (_("This target does not support attaching to a process"));
6406 error (_("Attaching to %s failed"),
6407 target_pid_to_str (ptid_t (pid
)).c_str ());
6410 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6412 inferior_ptid
= ptid_t (pid
);
6414 if (target_is_non_stop_p ())
6416 /* Get list of threads. */
6417 update_thread_list ();
6419 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6420 if (thread
!= nullptr)
6421 switch_to_thread (thread
);
6423 /* Invalidate our notion of the remote current thread. */
6424 record_currthread (rs
, minus_one_ptid
);
6428 /* Now, if we have thread information, update the main thread's
6430 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6432 /* Add the main thread to the thread list. We add the thread
6433 silently in this case (the final true parameter). */
6434 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6436 switch_to_thread (thr
);
6439 /* Next, if the target can specify a description, read it. We do
6440 this before anything involving memory or registers. */
6441 target_find_description ();
6443 if (!target_is_non_stop_p ())
6445 /* Use the previously fetched status. */
6446 gdb_assert (wait_status
!= NULL
);
6448 struct notif_event
*reply
6449 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6451 push_stop_reply ((struct stop_reply
*) reply
);
6455 gdb_assert (wait_status
== NULL
);
6457 gdb_assert (target_can_async_p ());
6461 /* Implementation of the to_post_attach method. */
6464 extended_remote_target::post_attach (int pid
)
6466 /* Get text, data & bss offsets. */
6469 /* In certain cases GDB might not have had the chance to start
6470 symbol lookup up until now. This could happen if the debugged
6471 binary is not using shared libraries, the vsyscall page is not
6472 present (on Linux) and the binary itself hadn't changed since the
6473 debugging process was started. */
6474 if (current_program_space
->symfile_object_file
!= NULL
)
6475 remote_check_symbols();
6479 /* Check for the availability of vCont. This function should also check
6483 remote_target::remote_vcont_probe ()
6485 remote_state
*rs
= get_remote_state ();
6488 strcpy (rs
->buf
.data (), "vCont?");
6491 buf
= rs
->buf
.data ();
6493 /* Make sure that the features we assume are supported. */
6494 if (startswith (buf
, "vCont"))
6497 int support_c
, support_C
;
6499 rs
->supports_vCont
.s
= 0;
6500 rs
->supports_vCont
.S
= 0;
6503 rs
->supports_vCont
.t
= 0;
6504 rs
->supports_vCont
.r
= 0;
6505 while (p
&& *p
== ';')
6508 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6509 rs
->supports_vCont
.s
= 1;
6510 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6511 rs
->supports_vCont
.S
= 1;
6512 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6514 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6516 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6517 rs
->supports_vCont
.t
= 1;
6518 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6519 rs
->supports_vCont
.r
= 1;
6521 p
= strchr (p
, ';');
6524 /* If c, and C are not all supported, we can't use vCont. Clearing
6525 BUF will make packet_ok disable the packet. */
6526 if (!support_c
|| !support_C
)
6530 m_features
.packet_ok (rs
->buf
, PACKET_vCont
);
6533 /* Helper function for building "vCont" resumptions. Write a
6534 resumption to P. ENDP points to one-passed-the-end of the buffer
6535 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6536 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6537 resumed thread should be single-stepped and/or signalled. If PTID
6538 equals minus_one_ptid, then all threads are resumed; if PTID
6539 represents a process, then all threads of the process are
6543 remote_target::append_resumption (char *p
, char *endp
,
6544 ptid_t ptid
, int step
, gdb_signal siggnal
)
6546 struct remote_state
*rs
= get_remote_state ();
6548 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6549 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6551 /* GDB is willing to range step. */
6552 && use_range_stepping
6553 /* Target supports range stepping. */
6554 && rs
->supports_vCont
.r
6555 /* We don't currently support range stepping multiple
6556 threads with a wildcard (though the protocol allows it,
6557 so stubs shouldn't make an active effort to forbid
6559 && !(m_features
.remote_multi_process_p () && ptid
.is_pid ()))
6561 struct thread_info
*tp
;
6563 if (ptid
== minus_one_ptid
)
6565 /* If we don't know about the target thread's tid, then
6566 we're resuming magic_null_ptid (see caller). */
6567 tp
= this->find_thread (magic_null_ptid
);
6570 tp
= this->find_thread (ptid
);
6571 gdb_assert (tp
!= NULL
);
6573 if (tp
->control
.may_range_step
)
6575 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
6577 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6578 phex_nz (tp
->control
.step_range_start
,
6580 phex_nz (tp
->control
.step_range_end
,
6584 p
+= xsnprintf (p
, endp
- p
, ";s");
6587 p
+= xsnprintf (p
, endp
- p
, ";s");
6588 else if (siggnal
!= GDB_SIGNAL_0
)
6589 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6591 p
+= xsnprintf (p
, endp
- p
, ";c");
6593 if (m_features
.remote_multi_process_p () && ptid
.is_pid ())
6597 /* All (-1) threads of process. */
6598 nptid
= ptid_t (ptid
.pid (), -1);
6600 p
+= xsnprintf (p
, endp
- p
, ":");
6601 p
= write_ptid (p
, endp
, nptid
);
6603 else if (ptid
!= minus_one_ptid
)
6605 p
+= xsnprintf (p
, endp
- p
, ":");
6606 p
= write_ptid (p
, endp
, ptid
);
6612 /* Clear the thread's private info on resume. */
6615 resume_clear_thread_private_info (struct thread_info
*thread
)
6617 if (thread
->priv
!= NULL
)
6619 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6621 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6622 priv
->watch_data_address
= 0;
6626 /* Append a vCont continue-with-signal action for threads that have a
6627 non-zero stop signal. */
6630 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6633 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6634 if (inferior_ptid
!= thread
->ptid
6635 && thread
->stop_signal () != GDB_SIGNAL_0
)
6637 p
= append_resumption (p
, endp
, thread
->ptid
,
6638 0, thread
->stop_signal ());
6639 thread
->set_stop_signal (GDB_SIGNAL_0
);
6640 resume_clear_thread_private_info (thread
);
6646 /* Set the target running, using the packets that use Hc
6650 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6653 struct remote_state
*rs
= get_remote_state ();
6656 rs
->last_sent_signal
= siggnal
;
6657 rs
->last_sent_step
= step
;
6659 /* The c/s/C/S resume packets use Hc, so set the continue
6661 if (ptid
== minus_one_ptid
)
6662 set_continue_thread (any_thread_ptid
);
6664 set_continue_thread (ptid
);
6666 for (thread_info
*thread
: all_non_exited_threads (this))
6667 resume_clear_thread_private_info (thread
);
6669 buf
= rs
->buf
.data ();
6670 if (::execution_direction
== EXEC_REVERSE
)
6672 /* We don't pass signals to the target in reverse exec mode. */
6673 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6674 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6677 if (step
&& m_features
.packet_support (PACKET_bs
) == PACKET_DISABLE
)
6678 error (_("Remote reverse-step not supported."));
6679 if (!step
&& m_features
.packet_support (PACKET_bc
) == PACKET_DISABLE
)
6680 error (_("Remote reverse-continue not supported."));
6682 strcpy (buf
, step
? "bs" : "bc");
6684 else if (siggnal
!= GDB_SIGNAL_0
)
6686 buf
[0] = step
? 'S' : 'C';
6687 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6688 buf
[2] = tohex (((int) siggnal
) & 0xf);
6692 strcpy (buf
, step
? "s" : "c");
6697 /* Resume the remote inferior by using a "vCont" packet. SCOPE_PTID,
6698 STEP, and SIGGNAL have the same meaning as in target_resume. This
6699 function returns non-zero iff it resumes the inferior.
6701 This function issues a strict subset of all possible vCont commands
6705 remote_target::remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
6706 enum gdb_signal siggnal
)
6708 struct remote_state
*rs
= get_remote_state ();
6712 /* No reverse execution actions defined for vCont. */
6713 if (::execution_direction
== EXEC_REVERSE
)
6716 if (m_features
.packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6719 p
= rs
->buf
.data ();
6720 endp
= p
+ get_remote_packet_size ();
6722 /* If we could generate a wider range of packets, we'd have to worry
6723 about overflowing BUF. Should there be a generic
6724 "multi-part-packet" packet? */
6726 p
+= xsnprintf (p
, endp
- p
, "vCont");
6728 if (scope_ptid
== magic_null_ptid
)
6730 /* MAGIC_NULL_PTID means that we don't have any active threads,
6731 so we don't have any TID numbers the inferior will
6732 understand. Make sure to only send forms that do not specify
6734 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6736 else if (scope_ptid
== minus_one_ptid
|| scope_ptid
.is_pid ())
6738 /* Resume all threads (of all processes, or of a single
6739 process), with preference for INFERIOR_PTID. This assumes
6740 inferior_ptid belongs to the set of all threads we are about
6742 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6744 /* Step inferior_ptid, with or without signal. */
6745 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6748 /* Also pass down any pending signaled resumption for other
6749 threads not the current. */
6750 p
= append_pending_thread_resumptions (p
, endp
, scope_ptid
);
6752 /* And continue others without a signal. */
6753 append_resumption (p
, endp
, scope_ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
6757 /* Scheduler locking; resume only SCOPE_PTID. */
6758 append_resumption (p
, endp
, scope_ptid
, step
, siggnal
);
6761 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
6764 if (target_is_non_stop_p ())
6766 /* In non-stop, the stub replies to vCont with "OK". The stop
6767 reply will be reported asynchronously by means of a `%Stop'
6770 if (strcmp (rs
->buf
.data (), "OK") != 0)
6771 error (_("Unexpected vCont reply in non-stop mode: %s"),
6778 /* Tell the remote machine to resume. */
6781 remote_target::resume (ptid_t scope_ptid
, int step
, enum gdb_signal siggnal
)
6783 struct remote_state
*rs
= get_remote_state ();
6785 /* When connected in non-stop mode, the core resumes threads
6786 individually. Resuming remote threads directly in target_resume
6787 would thus result in sending one packet per thread. Instead, to
6788 minimize roundtrip latency, here we just store the resume
6789 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
6790 resumption will be done in remote_target::commit_resume, where we'll be
6791 able to do vCont action coalescing. */
6792 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
6794 remote_thread_info
*remote_thr
6795 = get_remote_thread_info (inferior_thread ());
6797 /* We don't expect the core to ask to resume an already resumed (from
6798 its point of view) thread. */
6799 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
6801 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
6803 /* There's actually nothing that says that the core can't
6804 request a wildcard resume in non-stop mode, though. It's
6805 just that we know it doesn't currently, so we don't bother
6807 gdb_assert (scope_ptid
== inferior_ptid
);
6811 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6812 (explained in remote-notif.c:handle_notification) so
6813 remote_notif_process is not called. We need find a place where
6814 it is safe to start a 'vNotif' sequence. It is good to do it
6815 before resuming inferior, because inferior was stopped and no RSP
6816 traffic at that moment. */
6817 if (!target_is_non_stop_p ())
6818 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
6820 rs
->last_resume_exec_dir
= ::execution_direction
;
6822 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6823 if (!remote_resume_with_vcont (scope_ptid
, step
, siggnal
))
6824 remote_resume_with_hc (scope_ptid
, step
, siggnal
);
6826 /* Update resumed state tracked by the remote target. */
6827 for (thread_info
*tp
: all_non_exited_threads (this, scope_ptid
))
6828 get_remote_thread_info (tp
)->set_resumed ();
6830 /* We've just told the target to resume. The remote server will
6831 wait for the inferior to stop, and then send a stop reply. In
6832 the mean time, we can't start another command/query ourselves
6833 because the stub wouldn't be ready to process it. This applies
6834 only to the base all-stop protocol, however. In non-stop (which
6835 only supports vCont), the stub replies with an "OK", and is
6836 immediate able to process further serial input. */
6837 if (!target_is_non_stop_p ())
6838 rs
->waiting_for_stop_reply
= 1;
6841 /* Private per-inferior info for target remote processes. */
6843 struct remote_inferior
: public private_inferior
6845 /* Whether we can send a wildcard vCont for this process. */
6846 bool may_wildcard_vcont
= true;
6849 /* Get the remote private inferior data associated to INF. */
6851 static remote_inferior
*
6852 get_remote_inferior (inferior
*inf
)
6854 if (inf
->priv
== NULL
)
6855 inf
->priv
.reset (new remote_inferior
);
6857 return gdb::checked_static_cast
<remote_inferior
*> (inf
->priv
.get ());
6860 /* Class used to track the construction of a vCont packet in the
6861 outgoing packet buffer. This is used to send multiple vCont
6862 packets if we have more actions than would fit a single packet. */
6867 explicit vcont_builder (remote_target
*remote
)
6874 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
6879 /* The remote target. */
6880 remote_target
*m_remote
;
6882 /* Pointer to the first action. P points here if no action has been
6884 char *m_first_action
;
6886 /* Where the next action will be appended. */
6889 /* The end of the buffer. Must never write past this. */
6893 /* Prepare the outgoing buffer for a new vCont packet. */
6896 vcont_builder::restart ()
6898 struct remote_state
*rs
= m_remote
->get_remote_state ();
6900 m_p
= rs
->buf
.data ();
6901 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
6902 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
6903 m_first_action
= m_p
;
6906 /* If the vCont packet being built has any action, send it to the
6910 vcont_builder::flush ()
6912 struct remote_state
*rs
;
6914 if (m_p
== m_first_action
)
6917 rs
= m_remote
->get_remote_state ();
6918 m_remote
->putpkt (rs
->buf
);
6919 m_remote
->getpkt (&rs
->buf
);
6920 if (strcmp (rs
->buf
.data (), "OK") != 0)
6921 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
6924 /* The largest action is range-stepping, with its two addresses. This
6925 is more than sufficient. If a new, bigger action is created, it'll
6926 quickly trigger a failed assertion in append_resumption (and we'll
6928 #define MAX_ACTION_SIZE 200
6930 /* Append a new vCont action in the outgoing packet being built. If
6931 the action doesn't fit the packet along with previous actions, push
6932 what we've got so far to the remote end and start over a new vCont
6933 packet (with the new action). */
6936 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
6938 char buf
[MAX_ACTION_SIZE
+ 1];
6940 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
6941 ptid
, step
, siggnal
);
6943 /* Check whether this new action would fit in the vCont packet along
6944 with previous actions. If not, send what we've got so far and
6945 start a new vCont packet. */
6946 size_t rsize
= endp
- buf
;
6947 if (rsize
> m_endp
- m_p
)
6952 /* Should now fit. */
6953 gdb_assert (rsize
<= m_endp
- m_p
);
6956 memcpy (m_p
, buf
, rsize
);
6961 /* to_commit_resume implementation. */
6964 remote_target::commit_resumed ()
6966 /* If connected in all-stop mode, we'd send the remote resume
6967 request directly from remote_resume. Likewise if
6968 reverse-debugging, as there are no defined vCont actions for
6969 reverse execution. */
6970 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
6973 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6974 instead of resuming all threads of each process individually.
6975 However, if any thread of a process must remain halted, we can't
6976 send wildcard resumes and must send one action per thread.
6978 Care must be taken to not resume threads/processes the server
6979 side already told us are stopped, but the core doesn't know about
6980 yet, because the events are still in the vStopped notification
6983 #1 => vCont s:p1.1;c
6985 #3 <= %Stopped T05 p1.1
6990 #8 (infrun handles the stop for p1.1 and continues stepping)
6991 #9 => vCont s:p1.1;c
6993 The last vCont above would resume thread p1.2 by mistake, because
6994 the server has no idea that the event for p1.2 had not been
6997 The server side must similarly ignore resume actions for the
6998 thread that has a pending %Stopped notification (and any other
6999 threads with events pending), until GDB acks the notification
7000 with vStopped. Otherwise, e.g., the following case is
7003 #1 => g (or any other packet)
7005 #3 <= %Stopped T05 p1.2
7006 #4 => vCont s:p1.1;c
7009 Above, the server must not resume thread p1.2. GDB can't know
7010 that p1.2 stopped until it acks the %Stopped notification, and
7011 since from GDB's perspective all threads should be running, it
7014 Finally, special care must also be given to handling fork/vfork
7015 events. A (v)fork event actually tells us that two processes
7016 stopped -- the parent and the child. Until we follow the fork,
7017 we must not resume the child. Therefore, if we have a pending
7018 fork follow, we must not send a global wildcard resume action
7019 (vCont;c). We can still send process-wide wildcards though. */
7021 /* Start by assuming a global wildcard (vCont;c) is possible. */
7022 bool may_global_wildcard_vcont
= true;
7024 /* And assume every process is individually wildcard-able too. */
7025 for (inferior
*inf
: all_non_exited_inferiors (this))
7027 remote_inferior
*priv
= get_remote_inferior (inf
);
7029 priv
->may_wildcard_vcont
= true;
7032 /* Check for any pending events (not reported or processed yet) and
7033 disable process and global wildcard resumes appropriately. */
7034 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
7036 bool any_pending_vcont_resume
= false;
7038 for (thread_info
*tp
: all_non_exited_threads (this))
7040 remote_thread_info
*priv
= get_remote_thread_info (tp
);
7042 /* If a thread of a process is not meant to be resumed, then we
7043 can't wildcard that process. */
7044 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
7046 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
7048 /* And if we can't wildcard a process, we can't wildcard
7049 everything either. */
7050 may_global_wildcard_vcont
= false;
7054 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
7055 any_pending_vcont_resume
= true;
7057 /* If a thread is the parent of an unfollowed fork, then we
7058 can't do a global wildcard, as that would resume the fork
7060 if (thread_pending_fork_status (tp
) != nullptr)
7061 may_global_wildcard_vcont
= false;
7064 /* We didn't have any resumed thread pending a vCont resume, so nothing to
7066 if (!any_pending_vcont_resume
)
7069 /* Now let's build the vCont packet(s). Actions must be appended
7070 from narrower to wider scopes (thread -> process -> global). If
7071 we end up with too many actions for a single packet vcont_builder
7072 flushes the current vCont packet to the remote side and starts a
7074 struct vcont_builder
vcont_builder (this);
7076 /* Threads first. */
7077 for (thread_info
*tp
: all_non_exited_threads (this))
7079 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7081 /* If the thread was previously vCont-resumed, no need to send a specific
7082 action for it. If we didn't receive a resume request for it, don't
7083 send an action for it either. */
7084 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
7087 gdb_assert (!thread_is_in_step_over_chain (tp
));
7089 /* We should never be commit-resuming a thread that has a stop reply.
7090 Otherwise, we would end up reporting a stop event for a thread while
7091 it is running on the remote target. */
7092 remote_state
*rs
= get_remote_state ();
7093 for (const auto &stop_reply
: rs
->stop_reply_queue
)
7094 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
7096 const resumed_pending_vcont_info
&info
7097 = remote_thr
->resumed_pending_vcont_info ();
7099 /* Check if we need to send a specific action for this thread. If not,
7100 it will be included in a wildcard resume instead. */
7101 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
7102 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
7103 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
7105 remote_thr
->set_resumed ();
7108 /* Now check whether we can send any process-wide wildcard. This is
7109 to avoid sending a global wildcard in the case nothing is
7110 supposed to be resumed. */
7111 bool any_process_wildcard
= false;
7113 for (inferior
*inf
: all_non_exited_inferiors (this))
7115 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7117 any_process_wildcard
= true;
7122 if (any_process_wildcard
)
7124 /* If all processes are wildcard-able, then send a single "c"
7125 action, otherwise, send an "all (-1) threads of process"
7126 continue action for each running process, if any. */
7127 if (may_global_wildcard_vcont
)
7129 vcont_builder
.push_action (minus_one_ptid
,
7130 false, GDB_SIGNAL_0
);
7134 for (inferior
*inf
: all_non_exited_inferiors (this))
7136 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
7138 vcont_builder
.push_action (ptid_t (inf
->pid
),
7139 false, GDB_SIGNAL_0
);
7145 vcont_builder
.flush ();
7148 /* Implementation of target_has_pending_events. */
7151 remote_target::has_pending_events ()
7153 if (target_can_async_p ())
7155 remote_state
*rs
= get_remote_state ();
7157 if (rs
->async_event_handler_marked ())
7160 /* Note that BUFCNT can be negative, indicating sticky
7162 if (rs
->remote_desc
->bufcnt
!= 0)
7170 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
7171 thread, all threads of a remote process, or all threads of all
7175 remote_target::remote_stop_ns (ptid_t ptid
)
7177 struct remote_state
*rs
= get_remote_state ();
7178 char *p
= rs
->buf
.data ();
7179 char *endp
= p
+ get_remote_packet_size ();
7181 /* If any thread that needs to stop was resumed but pending a vCont
7182 resume, generate a phony stop_reply. However, first check
7183 whether the thread wasn't resumed with a signal. Generating a
7184 phony stop in that case would result in losing the signal. */
7185 bool needs_commit
= false;
7186 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7188 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7190 if (remote_thr
->get_resume_state ()
7191 == resume_state::RESUMED_PENDING_VCONT
)
7193 const resumed_pending_vcont_info
&info
7194 = remote_thr
->resumed_pending_vcont_info ();
7195 if (info
.sig
!= GDB_SIGNAL_0
)
7197 /* This signal must be forwarded to the inferior. We
7198 could commit-resume just this thread, but its simpler
7199 to just commit-resume everything. */
7200 needs_commit
= true;
7209 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
7211 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
7213 if (remote_thr
->get_resume_state ()
7214 == resume_state::RESUMED_PENDING_VCONT
)
7216 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
7217 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
7219 pulongest (tp
->ptid
.tid ()));
7221 /* Check that the thread wasn't resumed with a signal.
7222 Generating a phony stop would result in losing the
7224 const resumed_pending_vcont_info
&info
7225 = remote_thr
->resumed_pending_vcont_info ();
7226 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
7228 stop_reply
*sr
= new stop_reply ();
7229 sr
->ptid
= tp
->ptid
;
7231 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
7232 sr
->arch
= tp
->inf
->arch ();
7233 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7234 sr
->watch_data_address
= 0;
7236 this->push_stop_reply (sr
);
7238 /* Pretend that this thread was actually resumed on the
7239 remote target, then stopped. If we leave it in the
7240 RESUMED_PENDING_VCONT state and the commit_resumed
7241 method is called while the stop reply is still in the
7242 queue, we'll end up reporting a stop event to the core
7243 for that thread while it is running on the remote
7244 target... that would be bad. */
7245 remote_thr
->set_resumed ();
7249 if (!rs
->supports_vCont
.t
)
7250 error (_("Remote server does not support stopping threads"));
7252 if (ptid
== minus_one_ptid
7253 || (!m_features
.remote_multi_process_p () && ptid
.is_pid ()))
7254 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7259 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7262 /* All (-1) threads of process. */
7263 nptid
= ptid_t (ptid
.pid (), -1);
7266 /* Small optimization: if we already have a stop reply for
7267 this thread, no use in telling the stub we want this
7269 if (peek_stop_reply (ptid
))
7275 write_ptid (p
, endp
, nptid
);
7278 /* In non-stop, we get an immediate OK reply. The stop reply will
7279 come in asynchronously by notification. */
7282 if (strcmp (rs
->buf
.data (), "OK") != 0)
7283 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7287 /* All-stop version of target_interrupt. Sends a break or a ^C to
7288 interrupt the remote target. It is undefined which thread of which
7289 process reports the interrupt. */
7292 remote_target::remote_interrupt_as ()
7294 struct remote_state
*rs
= get_remote_state ();
7296 rs
->ctrlc_pending_p
= 1;
7298 /* If the inferior is stopped already, but the core didn't know
7299 about it yet, just ignore the request. The pending stop events
7300 will be collected in remote_wait. */
7301 if (stop_reply_queue_length () > 0)
7304 /* Send interrupt_sequence to remote target. */
7305 send_interrupt_sequence ();
7308 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7309 the remote target. It is undefined which thread of which process
7310 reports the interrupt. Throws an error if the packet is not
7311 supported by the server. */
7314 remote_target::remote_interrupt_ns ()
7316 struct remote_state
*rs
= get_remote_state ();
7317 char *p
= rs
->buf
.data ();
7318 char *endp
= p
+ get_remote_packet_size ();
7320 xsnprintf (p
, endp
- p
, "vCtrlC");
7322 /* In non-stop, we get an immediate OK reply. The stop reply will
7323 come in asynchronously by notification. */
7327 switch (m_features
.packet_ok (rs
->buf
, PACKET_vCtrlC
))
7331 case PACKET_UNKNOWN
:
7332 error (_("No support for interrupting the remote target."));
7334 error (_("Interrupting target failed: %s"), rs
->buf
.data ());
7338 /* Implement the to_stop function for the remote targets. */
7341 remote_target::stop (ptid_t ptid
)
7343 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7345 if (target_is_non_stop_p ())
7346 remote_stop_ns (ptid
);
7349 /* We don't currently have a way to transparently pause the
7350 remote target in all-stop mode. Interrupt it instead. */
7351 remote_interrupt_as ();
7355 /* Implement the to_interrupt function for the remote targets. */
7358 remote_target::interrupt ()
7360 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7362 if (target_is_non_stop_p ())
7363 remote_interrupt_ns ();
7365 remote_interrupt_as ();
7368 /* Implement the to_pass_ctrlc function for the remote targets. */
7371 remote_target::pass_ctrlc ()
7373 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7375 struct remote_state
*rs
= get_remote_state ();
7377 /* If we're starting up, we're not fully synced yet. Quit
7379 if (rs
->starting_up
)
7381 /* If ^C has already been sent once, offer to disconnect. */
7382 else if (rs
->ctrlc_pending_p
)
7385 target_interrupt ();
7388 /* Ask the user what to do when an interrupt is received. */
7391 remote_target::interrupt_query ()
7393 struct remote_state
*rs
= get_remote_state ();
7395 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7397 if (query (_("The target is not responding to interrupt requests.\n"
7398 "Stop debugging it? ")))
7400 remote_unpush_target (this);
7401 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7406 if (query (_("Interrupted while waiting for the program.\n"
7407 "Give up waiting? ")))
7412 /* Enable/disable target terminal ownership. Most targets can use
7413 terminal groups to control terminal ownership. Remote targets are
7414 different in that explicit transfer of ownership to/from GDB/target
7418 remote_target::terminal_inferior ()
7420 /* NOTE: At this point we could also register our selves as the
7421 recipient of all input. Any characters typed could then be
7422 passed on down to the target. */
7426 remote_target::terminal_ours ()
7431 remote_console_output (const char *msg
)
7435 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7438 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7442 gdb_stdtarg
->puts (tb
);
7444 gdb_stdtarg
->flush ();
7447 /* Return the length of the stop reply queue. */
7450 remote_target::stop_reply_queue_length ()
7452 remote_state
*rs
= get_remote_state ();
7453 return rs
->stop_reply_queue
.size ();
7457 remote_notif_stop_parse (remote_target
*remote
,
7458 const notif_client
*self
, const char *buf
,
7459 struct notif_event
*event
)
7461 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7465 remote_notif_stop_ack (remote_target
*remote
,
7466 const notif_client
*self
, const char *buf
,
7467 struct notif_event
*event
)
7469 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
7472 putpkt (remote
, self
->ack_command
);
7474 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7475 the notification. It was left in the queue because we need to
7476 acknowledge it and pull the rest of the notifications out. */
7477 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7478 remote
->push_stop_reply (stop_reply
);
7482 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7483 const notif_client
*self
)
7485 /* We can't get pending events in remote_notif_process for
7486 notification stop, and we have to do this in remote_wait_ns
7487 instead. If we fetch all queued events from stub, remote stub
7488 may exit and we have no chance to process them back in
7490 remote_state
*rs
= remote
->get_remote_state ();
7491 rs
->mark_async_event_handler ();
7495 stop_reply::~stop_reply ()
7497 for (cached_reg_t
®
: regcache
)
7501 static notif_event_up
7502 remote_notif_stop_alloc_reply ()
7504 return notif_event_up (new struct stop_reply ());
7507 /* A client of notification Stop. */
7509 const notif_client notif_client_stop
=
7513 remote_notif_stop_parse
,
7514 remote_notif_stop_ack
,
7515 remote_notif_stop_can_get_pending_events
,
7516 remote_notif_stop_alloc_reply
,
7520 /* If CONTEXT contains any fork child threads that have not been
7521 reported yet, remove them from the CONTEXT list. If such a
7522 thread exists it is because we are stopped at a fork catchpoint
7523 and have not yet called follow_fork, which will set up the
7524 host-side data structures for the new process. */
7527 remote_target::remove_new_fork_children (threads_listing_context
*context
)
7529 const notif_client
*notif
= ¬if_client_stop
;
7531 /* For any threads stopped at a fork event, remove the corresponding
7532 fork child threads from the CONTEXT list. */
7533 for (thread_info
*thread
: all_non_exited_threads (this))
7535 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
7540 context
->remove_thread (ws
->child_ptid ());
7543 /* Check for any pending fork events (not reported or processed yet)
7544 in process PID and remove those fork child threads from the
7545 CONTEXT list as well. */
7546 remote_notif_get_pending_events (notif
);
7547 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7548 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7549 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7550 context
->remove_thread (event
->ws
.child_ptid ());
7551 else if (event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7552 context
->remove_thread (event
->ptid
);
7555 /* Check whether any event pending in the vStopped queue would prevent a
7556 global or process wildcard vCont action. Set *may_global_wildcard to
7557 false if we can't do a global wildcard (vCont;c), and clear the event
7558 inferior's may_wildcard_vcont flag if we can't do a process-wide
7559 wildcard resume (vCont;c:pPID.-1). */
7562 remote_target::check_pending_events_prevent_wildcard_vcont
7563 (bool *may_global_wildcard
)
7565 const notif_client
*notif
= ¬if_client_stop
;
7567 remote_notif_get_pending_events (notif
);
7568 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7570 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7571 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7574 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7575 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7576 *may_global_wildcard
= false;
7578 /* This may be the first time we heard about this process.
7579 Regardless, we must not do a global wildcard resume, otherwise
7580 we'd resume this process too. */
7581 *may_global_wildcard
= false;
7582 if (event
->ptid
!= null_ptid
)
7584 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7586 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7591 /* Discard all pending stop replies of inferior INF. */
7594 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7596 struct stop_reply
*reply
;
7597 struct remote_state
*rs
= get_remote_state ();
7598 struct remote_notif_state
*rns
= rs
->notif_state
;
7600 /* This function can be notified when an inferior exists. When the
7601 target is not remote, the notification state is NULL. */
7602 if (rs
->remote_desc
== NULL
)
7605 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
7607 /* Discard the in-flight notification. */
7608 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7610 /* Leave the notification pending, since the server expects that
7611 we acknowledge it with vStopped. But clear its contents, so
7612 that later on when we acknowledge it, we also discard it. */
7614 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7615 reply
->ptid
.to_string().c_str(),
7616 reply
->ws
.to_string ().c_str ());
7617 reply
->ws
.set_ignore ();
7620 /* Discard the stop replies we have already pulled with
7622 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7623 rs
->stop_reply_queue
.end (),
7624 [=] (const stop_reply_up
&event
)
7626 return event
->ptid
.pid () == inf
->pid
;
7628 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7630 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7631 (*it
)->ptid
.to_string().c_str(),
7632 (*it
)->ws
.to_string ().c_str ());
7633 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7636 /* Discard the stop replies for RS in stop_reply_queue. */
7639 remote_target::discard_pending_stop_replies_in_queue ()
7641 remote_state
*rs
= get_remote_state ();
7643 /* Discard the stop replies we have already pulled with
7645 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7646 rs
->stop_reply_queue
.end (),
7647 [=] (const stop_reply_up
&event
)
7649 return event
->rs
== rs
;
7651 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7654 /* Remove the first reply in 'stop_reply_queue' which matches
7658 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7660 remote_state
*rs
= get_remote_state ();
7662 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7663 rs
->stop_reply_queue
.end (),
7664 [=] (const stop_reply_up
&event
)
7666 return event
->ptid
.matches (ptid
);
7668 struct stop_reply
*result
;
7669 if (iter
== rs
->stop_reply_queue
.end ())
7673 result
= iter
->release ();
7674 rs
->stop_reply_queue
.erase (iter
);
7678 gdb_printf (gdb_stdlog
,
7679 "notif: discard queued event: 'Stop' in %s\n",
7680 ptid
.to_string ().c_str ());
7685 /* Look for a queued stop reply belonging to PTID. If one is found,
7686 remove it from the queue, and return it. Returns NULL if none is
7687 found. If there are still queued events left to process, tell the
7688 event loop to get back to target_wait soon. */
7691 remote_target::queued_stop_reply (ptid_t ptid
)
7693 remote_state
*rs
= get_remote_state ();
7694 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
7696 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7698 /* There's still at least an event left. */
7699 rs
->mark_async_event_handler ();
7705 /* Push a fully parsed stop reply in the stop reply queue. Since we
7706 know that we now have at least one queued event left to pass to the
7707 core side, tell the event loop to get back to target_wait soon. */
7710 remote_target::push_stop_reply (struct stop_reply
*new_event
)
7712 remote_state
*rs
= get_remote_state ();
7713 rs
->stop_reply_queue
.push_back (stop_reply_up (new_event
));
7716 gdb_printf (gdb_stdlog
,
7717 "notif: push 'Stop' %s to queue %d\n",
7718 new_event
->ptid
.to_string ().c_str (),
7719 int (rs
->stop_reply_queue
.size ()));
7721 /* Mark the pending event queue only if async mode is currently enabled.
7722 If async mode is not currently enabled, then, if it later becomes
7723 enabled, and there are events in this queue, we will mark the event
7724 token at that point, see remote_target::async. */
7725 if (target_is_async_p ())
7726 rs
->mark_async_event_handler ();
7729 /* Returns true if we have a stop reply for PTID. */
7732 remote_target::peek_stop_reply (ptid_t ptid
)
7734 remote_state
*rs
= get_remote_state ();
7735 for (auto &event
: rs
->stop_reply_queue
)
7736 if (ptid
== event
->ptid
7737 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7742 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7743 starting with P and ending with PEND matches PREFIX. */
7746 strprefix (const char *p
, const char *pend
, const char *prefix
)
7748 for ( ; p
< pend
; p
++, prefix
++)
7751 return *prefix
== '\0';
7754 /* Parse the stop reply in BUF. Either the function succeeds, and the
7755 result is stored in EVENT, or throws an error. */
7758 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
7760 remote_arch_state
*rsa
= NULL
;
7765 event
->ptid
= null_ptid
;
7766 event
->rs
= get_remote_state ();
7767 event
->ws
.set_ignore ();
7768 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7769 event
->regcache
.clear ();
7774 case 'T': /* Status with PC, SP, FP, ... */
7775 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7776 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7778 n... = register number
7779 r... = register contents
7782 p
= &buf
[3]; /* after Txx */
7788 p1
= strchr (p
, ':');
7790 error (_("Malformed packet(a) (missing colon): %s\n\
7794 error (_("Malformed packet(a) (missing register number): %s\n\
7798 /* Some "registers" are actually extended stop information.
7799 Note if you're adding a new entry here: GDB 7.9 and
7800 earlier assume that all register "numbers" that start
7801 with an hex digit are real register numbers. Make sure
7802 the server only sends such a packet if it knows the
7803 client understands it. */
7805 if (strprefix (p
, p1
, "thread"))
7806 event
->ptid
= read_ptid (++p1
, &p
);
7807 else if (strprefix (p
, p1
, "syscall_entry"))
7811 p
= unpack_varlen_hex (++p1
, &sysno
);
7812 event
->ws
.set_syscall_entry ((int) sysno
);
7814 else if (strprefix (p
, p1
, "syscall_return"))
7818 p
= unpack_varlen_hex (++p1
, &sysno
);
7819 event
->ws
.set_syscall_return ((int) sysno
);
7821 else if (strprefix (p
, p1
, "watch")
7822 || strprefix (p
, p1
, "rwatch")
7823 || strprefix (p
, p1
, "awatch"))
7825 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
7826 p
= unpack_varlen_hex (++p1
, &addr
);
7827 event
->watch_data_address
= (CORE_ADDR
) addr
;
7829 else if (strprefix (p
, p1
, "swbreak"))
7831 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
7833 /* Make sure the stub doesn't forget to indicate support
7835 if (m_features
.packet_support (PACKET_swbreak_feature
)
7837 error (_("Unexpected swbreak stop reason"));
7839 /* The value part is documented as "must be empty",
7840 though we ignore it, in case we ever decide to make
7841 use of it in a backward compatible way. */
7842 p
= strchrnul (p1
+ 1, ';');
7844 else if (strprefix (p
, p1
, "hwbreak"))
7846 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
7848 /* Make sure the stub doesn't forget to indicate support
7850 if (m_features
.packet_support (PACKET_hwbreak_feature
)
7852 error (_("Unexpected hwbreak stop reason"));
7855 p
= strchrnul (p1
+ 1, ';');
7857 else if (strprefix (p
, p1
, "library"))
7859 event
->ws
.set_loaded ();
7860 p
= strchrnul (p1
+ 1, ';');
7862 else if (strprefix (p
, p1
, "replaylog"))
7864 event
->ws
.set_no_history ();
7865 /* p1 will indicate "begin" or "end", but it makes
7866 no difference for now, so ignore it. */
7867 p
= strchrnul (p1
+ 1, ';');
7869 else if (strprefix (p
, p1
, "core"))
7873 p
= unpack_varlen_hex (++p1
, &c
);
7876 else if (strprefix (p
, p1
, "fork"))
7877 event
->ws
.set_forked (read_ptid (++p1
, &p
));
7878 else if (strprefix (p
, p1
, "vfork"))
7879 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
7880 else if (strprefix (p
, p1
, "vforkdone"))
7882 event
->ws
.set_vfork_done ();
7883 p
= strchrnul (p1
+ 1, ';');
7885 else if (strprefix (p
, p1
, "exec"))
7890 /* Determine the length of the execd pathname. */
7891 p
= unpack_varlen_hex (++p1
, &ignored
);
7892 pathlen
= (p
- p1
) / 2;
7894 /* Save the pathname for event reporting and for
7895 the next run command. */
7896 gdb::unique_xmalloc_ptr
<char> pathname
7897 ((char *) xmalloc (pathlen
+ 1));
7898 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
7899 pathname
.get ()[pathlen
] = '\0';
7901 /* This is freed during event handling. */
7902 event
->ws
.set_execd (std::move (pathname
));
7904 /* Skip the registers included in this packet, since
7905 they may be for an architecture different from the
7906 one used by the original program. */
7909 else if (strprefix (p
, p1
, "create"))
7911 event
->ws
.set_thread_created ();
7912 p
= strchrnul (p1
+ 1, ';');
7921 p
= strchrnul (p1
+ 1, ';');
7926 /* Maybe a real ``P'' register number. */
7927 p_temp
= unpack_varlen_hex (p
, &pnum
);
7928 /* If the first invalid character is the colon, we got a
7929 register number. Otherwise, it's an unknown stop
7933 /* If we haven't parsed the event's thread yet, find
7934 it now, in order to find the architecture of the
7935 reported expedited registers. */
7936 if (event
->ptid
== null_ptid
)
7938 /* If there is no thread-id information then leave
7939 the event->ptid as null_ptid. Later in
7940 process_stop_reply we will pick a suitable
7942 const char *thr
= strstr (p1
+ 1, ";thread:");
7944 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
7951 = (event
->ptid
== null_ptid
7953 : find_inferior_ptid (this, event
->ptid
));
7954 /* If this is the first time we learn anything
7955 about this process, skip the registers
7956 included in this packet, since we don't yet
7957 know which architecture to use to parse them.
7958 We'll determine the architecture later when
7959 we process the stop reply and retrieve the
7960 target description, via
7961 remote_notice_new_inferior ->
7962 post_create_inferior. */
7965 p
= strchrnul (p1
+ 1, ';');
7970 event
->arch
= inf
->arch ();
7971 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
7975 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
7976 cached_reg_t cached_reg
;
7979 error (_("Remote sent bad register number %s: %s\n\
7981 hex_string (pnum
), p
, buf
);
7983 cached_reg
.num
= reg
->regnum
;
7984 cached_reg
.data
= (gdb_byte
*)
7985 xmalloc (register_size (event
->arch
, reg
->regnum
));
7988 fieldsize
= hex2bin (p
, cached_reg
.data
,
7989 register_size (event
->arch
, reg
->regnum
));
7991 if (fieldsize
< register_size (event
->arch
, reg
->regnum
))
7992 warning (_("Remote reply is too short: %s"), buf
);
7994 event
->regcache
.push_back (cached_reg
);
7998 /* Not a number. Silently skip unknown optional
8000 p
= strchrnul (p1
+ 1, ';');
8005 error (_("Remote register badly formatted: %s\nhere: %s"),
8010 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
8014 case 'S': /* Old style status, just signal only. */
8018 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
8019 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
8020 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
8022 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
8025 case 'w': /* Thread exited. */
8029 p
= unpack_varlen_hex (&buf
[1], &value
);
8030 event
->ws
.set_thread_exited (value
);
8032 error (_("stop reply packet badly formatted: %s"), buf
);
8033 event
->ptid
= read_ptid (++p
, NULL
);
8036 case 'W': /* Target exited. */
8041 /* GDB used to accept only 2 hex chars here. Stubs should
8042 only send more if they detect GDB supports multi-process
8044 p
= unpack_varlen_hex (&buf
[1], &value
);
8048 /* The remote process exited. */
8049 event
->ws
.set_exited (value
);
8053 /* The remote process exited with a signal. */
8054 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
8055 event
->ws
.set_signalled ((enum gdb_signal
) value
);
8057 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
8060 /* If no process is specified, return null_ptid, and let the
8061 caller figure out the right process to use. */
8071 else if (startswith (p
, "process:"))
8075 p
+= sizeof ("process:") - 1;
8076 unpack_varlen_hex (p
, &upid
);
8080 error (_("unknown stop reply packet: %s"), buf
);
8083 error (_("unknown stop reply packet: %s"), buf
);
8084 event
->ptid
= ptid_t (pid
);
8088 event
->ws
.set_no_resumed ();
8089 event
->ptid
= minus_one_ptid
;
8094 /* When the stub wants to tell GDB about a new notification reply, it
8095 sends a notification (%Stop, for example). Those can come it at
8096 any time, hence, we have to make sure that any pending
8097 putpkt/getpkt sequence we're making is finished, before querying
8098 the stub for more events with the corresponding ack command
8099 (vStopped, for example). E.g., if we started a vStopped sequence
8100 immediately upon receiving the notification, something like this
8108 1.6) <-- (registers reply to step #1.3)
8110 Obviously, the reply in step #1.6 would be unexpected to a vStopped
8113 To solve this, whenever we parse a %Stop notification successfully,
8114 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
8115 doing whatever we were doing:
8121 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
8122 2.5) <-- (registers reply to step #2.3)
8124 Eventually after step #2.5, we return to the event loop, which
8125 notices there's an event on the
8126 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
8127 associated callback --- the function below. At this point, we're
8128 always safe to start a vStopped sequence. :
8131 2.7) <-- T05 thread:2
8137 remote_target::remote_notif_get_pending_events (const notif_client
*nc
)
8139 struct remote_state
*rs
= get_remote_state ();
8141 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
8144 gdb_printf (gdb_stdlog
,
8145 "notif: process: '%s' ack pending event\n",
8149 nc
->ack (this, nc
, rs
->buf
.data (),
8150 rs
->notif_state
->pending_event
[nc
->id
]);
8151 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
8156 if (strcmp (rs
->buf
.data (), "OK") == 0)
8159 remote_notif_ack (this, nc
, rs
->buf
.data ());
8165 gdb_printf (gdb_stdlog
,
8166 "notif: process: '%s' no pending reply\n",
8171 /* Wrapper around remote_target::remote_notif_get_pending_events to
8172 avoid having to export the whole remote_target class. */
8175 remote_notif_get_pending_events (remote_target
*remote
, const notif_client
*nc
)
8177 remote
->remote_notif_get_pending_events (nc
);
8180 /* Called from process_stop_reply when the stop packet we are responding
8181 to didn't include a process-id or thread-id. STATUS is the stop event
8182 we are responding to.
8184 It is the task of this function to select a suitable thread (or process)
8185 and return its ptid, this is the thread (or process) we will assume the
8186 stop event came from.
8188 In some cases there isn't really any choice about which thread (or
8189 process) is selected, a basic remote with a single process containing a
8190 single thread might choose not to send any process-id or thread-id in
8191 its stop packets, this function will select and return the one and only
8194 However, if a target supports multiple threads (or processes) and still
8195 doesn't include a thread-id (or process-id) in its stop packet then
8196 first, this is a badly behaving target, and second, we're going to have
8197 to select a thread (or process) at random and use that. This function
8198 will print a warning to the user if it detects that there is the
8199 possibility that GDB is guessing which thread (or process) to
8202 Note that this is called before GDB fetches the updated thread list from the
8203 target. So it's possible for the stop reply to be ambiguous and for GDB to
8204 not realize it. For example, if there's initially one thread, the target
8205 spawns a second thread, and then sends a stop reply without an id that
8206 concerns the first thread. GDB will assume the stop reply is about the
8207 first thread - the only thread it knows about - without printing a warning.
8208 Anyway, if the remote meant for the stop reply to be about the second thread,
8209 then it would be really broken, because GDB doesn't know about that thread
8213 remote_target::select_thread_for_ambiguous_stop_reply
8214 (const target_waitstatus
&status
)
8216 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8218 /* Some stop events apply to all threads in an inferior, while others
8219 only apply to a single thread. */
8220 bool process_wide_stop
8221 = (status
.kind () == TARGET_WAITKIND_EXITED
8222 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
8224 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
8226 thread_info
*first_resumed_thread
= nullptr;
8227 bool ambiguous
= false;
8229 /* Consider all non-exited threads of the target, find the first resumed
8231 for (thread_info
*thr
: all_non_exited_threads (this))
8233 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
8235 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
8238 if (first_resumed_thread
== nullptr)
8239 first_resumed_thread
= thr
;
8240 else if (!process_wide_stop
8241 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8245 gdb_assert (first_resumed_thread
!= nullptr);
8247 remote_debug_printf ("first resumed thread is %s",
8248 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8249 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8251 /* Warn if the remote target is sending ambiguous stop replies. */
8254 static bool warned
= false;
8258 /* If you are seeing this warning then the remote target has
8259 stopped without specifying a thread-id, but the target
8260 does have multiple threads (or inferiors), and so GDB is
8261 having to guess which thread stopped.
8263 Examples of what might cause this are the target sending
8264 and 'S' stop packet, or a 'T' stop packet and not
8265 including a thread-id.
8267 Additionally, the target might send a 'W' or 'X packet
8268 without including a process-id, when the target has
8269 multiple running inferiors. */
8270 if (process_wide_stop
)
8271 warning (_("multi-inferior target stopped without "
8272 "sending a process-id, using first "
8273 "non-exited inferior"));
8275 warning (_("multi-threaded target stopped without "
8276 "sending a thread-id, using first "
8277 "non-exited thread"));
8282 /* If this is a stop for all threads then don't use a particular threads
8283 ptid, instead create a new ptid where only the pid field is set. */
8284 if (process_wide_stop
)
8285 return ptid_t (first_resumed_thread
->ptid
.pid ());
8287 return first_resumed_thread
->ptid
;
8290 /* Called when it is decided that STOP_REPLY holds the info of the
8291 event that is to be returned to the core. This function always
8292 destroys STOP_REPLY. */
8295 remote_target::process_stop_reply (struct stop_reply
*stop_reply
,
8296 struct target_waitstatus
*status
)
8298 *status
= stop_reply
->ws
;
8299 ptid_t ptid
= stop_reply
->ptid
;
8301 /* If no thread/process was reported by the stub then select a suitable
8303 if (ptid
== null_ptid
)
8304 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8305 gdb_assert (ptid
!= null_ptid
);
8307 if (status
->kind () != TARGET_WAITKIND_EXITED
8308 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8309 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8311 /* Expedited registers. */
8312 if (!stop_reply
->regcache
.empty ())
8314 struct regcache
*regcache
8315 = get_thread_arch_regcache (this, ptid
, stop_reply
->arch
);
8317 for (cached_reg_t
®
: stop_reply
->regcache
)
8319 regcache
->raw_supply (reg
.num
, reg
.data
);
8323 stop_reply
->regcache
.clear ();
8326 remote_notice_new_inferior (ptid
, false);
8327 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8328 remote_thr
->core
= stop_reply
->core
;
8329 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8330 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8332 if (target_is_non_stop_p ())
8334 /* If the target works in non-stop mode, a stop-reply indicates that
8335 only this thread stopped. */
8336 remote_thr
->set_not_resumed ();
8340 /* If the target works in all-stop mode, a stop-reply indicates that
8341 all the target's threads stopped. */
8342 for (thread_info
*tp
: all_non_exited_threads (this))
8343 get_remote_thread_info (tp
)->set_not_resumed ();
8351 /* The non-stop mode version of target_wait. */
8354 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8355 target_wait_flags options
)
8357 struct remote_state
*rs
= get_remote_state ();
8358 struct stop_reply
*stop_reply
;
8360 bool is_notif
= false;
8362 /* If in non-stop mode, get out of getpkt even if a
8363 notification is received. */
8365 ret
= getpkt (&rs
->buf
, false /* forever */, &is_notif
);
8368 if (ret
!= -1 && !is_notif
)
8371 case 'E': /* Error of some sort. */
8372 /* We're out of sync with the target now. Did it continue
8373 or not? We can't tell which thread it was in non-stop,
8374 so just ignore this. */
8375 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8377 case 'O': /* Console output. */
8378 remote_console_output (&rs
->buf
[1]);
8381 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8385 /* Acknowledge a pending stop reply that may have arrived in the
8387 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8388 remote_notif_get_pending_events (¬if_client_stop
);
8390 /* If indeed we noticed a stop reply, we're done. */
8391 stop_reply
= queued_stop_reply (ptid
);
8392 if (stop_reply
!= NULL
)
8393 return process_stop_reply (stop_reply
, status
);
8395 /* Still no event. If we're just polling for an event, then
8396 return to the event loop. */
8397 if (options
& TARGET_WNOHANG
)
8399 status
->set_ignore ();
8400 return minus_one_ptid
;
8403 /* Otherwise do a blocking wait. */
8404 ret
= getpkt (&rs
->buf
, true /* forever */, &is_notif
);
8408 /* Return the first resumed thread. */
8411 first_remote_resumed_thread (remote_target
*target
)
8413 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8419 /* Wait until the remote machine stops, then return, storing status in
8420 STATUS just as `wait' would. */
8423 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8424 target_wait_flags options
)
8426 struct remote_state
*rs
= get_remote_state ();
8427 ptid_t event_ptid
= null_ptid
;
8429 struct stop_reply
*stop_reply
;
8433 status
->set_ignore ();
8435 stop_reply
= queued_stop_reply (ptid
);
8436 if (stop_reply
!= NULL
)
8438 /* None of the paths that push a stop reply onto the queue should
8439 have set the waiting_for_stop_reply flag. */
8440 gdb_assert (!rs
->waiting_for_stop_reply
);
8441 event_ptid
= process_stop_reply (stop_reply
, status
);
8445 bool forever
= ((options
& TARGET_WNOHANG
) == 0
8446 && rs
->wait_forever_enabled_p
);
8448 if (!rs
->waiting_for_stop_reply
)
8450 status
->set_no_resumed ();
8451 return minus_one_ptid
;
8454 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8455 _never_ wait for ever -> test on target_is_async_p().
8456 However, before we do that we need to ensure that the caller
8457 knows how to take the target into/out of async mode. */
8459 int ret
= getpkt (&rs
->buf
, forever
, &is_notif
);
8461 /* GDB gets a notification. Return to core as this event is
8463 if (ret
!= -1 && is_notif
)
8464 return minus_one_ptid
;
8466 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8467 return minus_one_ptid
;
8469 buf
= rs
->buf
.data ();
8471 /* Assume that the target has acknowledged Ctrl-C unless we receive
8472 an 'F' or 'O' packet. */
8473 if (buf
[0] != 'F' && buf
[0] != 'O')
8474 rs
->ctrlc_pending_p
= 0;
8478 case 'E': /* Error of some sort. */
8479 /* We're out of sync with the target now. Did it continue or
8480 not? Not is more likely, so report a stop. */
8481 rs
->waiting_for_stop_reply
= 0;
8483 warning (_("Remote failure reply: %s"), buf
);
8484 status
->set_stopped (GDB_SIGNAL_0
);
8486 case 'F': /* File-I/O request. */
8487 /* GDB may access the inferior memory while handling the File-I/O
8488 request, but we don't want GDB accessing memory while waiting
8489 for a stop reply. See the comments in putpkt_binary. Set
8490 waiting_for_stop_reply to 0 temporarily. */
8491 rs
->waiting_for_stop_reply
= 0;
8492 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8493 rs
->ctrlc_pending_p
= 0;
8494 /* GDB handled the File-I/O request, and the target is running
8495 again. Keep waiting for events. */
8496 rs
->waiting_for_stop_reply
= 1;
8498 case 'N': case 'T': case 'S': case 'X': case 'W':
8500 /* There is a stop reply to handle. */
8501 rs
->waiting_for_stop_reply
= 0;
8504 = (struct stop_reply
*) remote_notif_parse (this,
8508 event_ptid
= process_stop_reply (stop_reply
, status
);
8511 case 'O': /* Console output. */
8512 remote_console_output (buf
+ 1);
8515 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8517 /* Zero length reply means that we tried 'S' or 'C' and the
8518 remote system doesn't support it. */
8519 target_terminal::ours_for_output ();
8521 ("Can't send signals to this remote system. %s not sent.\n",
8522 gdb_signal_to_name (rs
->last_sent_signal
));
8523 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8524 target_terminal::inferior ();
8526 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8532 warning (_("Invalid remote reply: %s"), buf
);
8537 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8538 return minus_one_ptid
;
8539 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8541 /* Nothing interesting happened. If we're doing a non-blocking
8542 poll, we're done. Otherwise, go back to waiting. */
8543 if (options
& TARGET_WNOHANG
)
8544 return minus_one_ptid
;
8548 else if (status
->kind () != TARGET_WAITKIND_EXITED
8549 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8551 if (event_ptid
!= null_ptid
)
8552 record_currthread (rs
, event_ptid
);
8554 event_ptid
= first_remote_resumed_thread (this);
8558 /* A process exit. Invalidate our notion of current thread. */
8559 record_currthread (rs
, minus_one_ptid
);
8560 /* It's possible that the packet did not include a pid. */
8561 if (event_ptid
== null_ptid
)
8562 event_ptid
= first_remote_resumed_thread (this);
8563 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8564 if (event_ptid
== null_ptid
)
8565 event_ptid
= magic_null_ptid
;
8571 /* Wait until the remote machine stops, then return, storing status in
8572 STATUS just as `wait' would. */
8575 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8576 target_wait_flags options
)
8578 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8580 remote_state
*rs
= get_remote_state ();
8582 /* Start by clearing the flag that asks for our wait method to be called,
8583 we'll mark it again at the end if needed. If the target is not in
8584 async mode then the async token should not be marked. */
8585 if (target_is_async_p ())
8586 rs
->clear_async_event_handler ();
8588 gdb_assert (!rs
->async_event_handler_marked ());
8592 if (target_is_non_stop_p ())
8593 event_ptid
= wait_ns (ptid
, status
, options
);
8595 event_ptid
= wait_as (ptid
, status
, options
);
8597 if (target_is_async_p ())
8599 /* If there are events left in the queue, or unacknowledged
8600 notifications, then tell the event loop to call us again. */
8601 if (!rs
->stop_reply_queue
.empty ()
8602 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8603 rs
->mark_async_event_handler ();
8609 /* Fetch a single register using a 'p' packet. */
8612 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8615 struct gdbarch
*gdbarch
= regcache
->arch ();
8616 struct remote_state
*rs
= get_remote_state ();
8618 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8621 if (m_features
.packet_support (PACKET_p
) == PACKET_DISABLE
)
8624 if (reg
->pnum
== -1)
8627 p
= rs
->buf
.data ();
8629 p
+= hexnumstr (p
, reg
->pnum
);
8634 buf
= rs
->buf
.data ();
8636 switch (m_features
.packet_ok (rs
->buf
, PACKET_p
))
8640 case PACKET_UNKNOWN
:
8643 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8644 gdbarch_register_name (regcache
->arch (), reg
->regnum
),
8648 /* If this register is unfetchable, tell the regcache. */
8651 regcache
->raw_supply (reg
->regnum
, NULL
);
8655 /* Otherwise, parse and supply the value. */
8661 error (_("fetch_register_using_p: early buf termination"));
8663 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8666 regcache
->raw_supply (reg
->regnum
, regp
);
8670 /* Fetch the registers included in the target's 'g' packet. */
8673 remote_target::send_g_packet ()
8675 struct remote_state
*rs
= get_remote_state ();
8678 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8681 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8682 error (_("Could not read registers; remote failure reply '%s'"),
8685 /* We can get out of synch in various cases. If the first character
8686 in the buffer is not a hex character, assume that has happened
8687 and try to fetch another packet to read. */
8688 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8689 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8690 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8691 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8693 remote_debug_printf ("Bad register packet; fetching a new packet");
8697 buf_len
= strlen (rs
->buf
.data ());
8699 /* Sanity check the received packet. */
8700 if (buf_len
% 2 != 0)
8701 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8707 remote_target::process_g_packet (struct regcache
*regcache
)
8709 struct gdbarch
*gdbarch
= regcache
->arch ();
8710 struct remote_state
*rs
= get_remote_state ();
8711 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8716 buf_len
= strlen (rs
->buf
.data ());
8718 /* Further sanity checks, with knowledge of the architecture. */
8719 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8720 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8722 rsa
->sizeof_g_packet
, buf_len
/ 2,
8725 /* Save the size of the packet sent to us by the target. It is used
8726 as a heuristic when determining the max size of packets that the
8727 target can safely receive. */
8728 if (rsa
->actual_register_packet_size
== 0)
8729 rsa
->actual_register_packet_size
= buf_len
;
8731 /* If this is smaller than we guessed the 'g' packet would be,
8732 update our records. A 'g' reply that doesn't include a register's
8733 value implies either that the register is not available, or that
8734 the 'p' packet must be used. */
8735 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8737 long sizeof_g_packet
= buf_len
/ 2;
8739 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8741 long offset
= rsa
->regs
[i
].offset
;
8742 long reg_size
= register_size (gdbarch
, i
);
8744 if (rsa
->regs
[i
].pnum
== -1)
8747 if (offset
>= sizeof_g_packet
)
8748 rsa
->regs
[i
].in_g_packet
= 0;
8749 else if (offset
+ reg_size
> sizeof_g_packet
)
8750 error (_("Truncated register %d in remote 'g' packet"), i
);
8752 rsa
->regs
[i
].in_g_packet
= 1;
8755 /* Looks valid enough, we can assume this is the correct length
8756 for a 'g' packet. It's important not to adjust
8757 rsa->sizeof_g_packet if we have truncated registers otherwise
8758 this "if" won't be run the next time the method is called
8759 with a packet of the same size and one of the internal errors
8760 below will trigger instead. */
8761 rsa
->sizeof_g_packet
= sizeof_g_packet
;
8764 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
8766 /* Unimplemented registers read as all bits zero. */
8767 memset (regs
, 0, rsa
->sizeof_g_packet
);
8769 /* Reply describes registers byte by byte, each byte encoded as two
8770 hex characters. Suck them all up, then supply them to the
8771 register cacheing/storage mechanism. */
8773 p
= rs
->buf
.data ();
8774 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
8776 if (p
[0] == 0 || p
[1] == 0)
8777 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
8778 internal_error (_("unexpected end of 'g' packet reply"));
8780 if (p
[0] == 'x' && p
[1] == 'x')
8781 regs
[i
] = 0; /* 'x' */
8783 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8787 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8789 struct packet_reg
*r
= &rsa
->regs
[i
];
8790 long reg_size
= register_size (gdbarch
, i
);
8794 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
8795 /* This shouldn't happen - we adjusted in_g_packet above. */
8796 internal_error (_("unexpected end of 'g' packet reply"));
8797 else if (rs
->buf
[r
->offset
* 2] == 'x')
8799 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
8800 /* The register isn't available, mark it as such (at
8801 the same time setting the value to zero). */
8802 regcache
->raw_supply (r
->regnum
, NULL
);
8805 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
8811 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
8814 process_g_packet (regcache
);
8817 /* Make the remote selected traceframe match GDB's selected
8821 remote_target::set_remote_traceframe ()
8824 struct remote_state
*rs
= get_remote_state ();
8826 if (rs
->remote_traceframe_number
== get_traceframe_number ())
8829 /* Avoid recursion, remote_trace_find calls us again. */
8830 rs
->remote_traceframe_number
= get_traceframe_number ();
8832 newnum
= target_trace_find (tfind_number
,
8833 get_traceframe_number (), 0, 0, NULL
);
8835 /* Should not happen. If it does, all bets are off. */
8836 if (newnum
!= get_traceframe_number ())
8837 warning (_("could not set remote traceframe"));
8841 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
8843 struct gdbarch
*gdbarch
= regcache
->arch ();
8844 struct remote_state
*rs
= get_remote_state ();
8845 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8848 set_remote_traceframe ();
8849 set_general_thread (regcache
->ptid ());
8853 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8855 gdb_assert (reg
!= NULL
);
8857 /* If this register might be in the 'g' packet, try that first -
8858 we are likely to read more than one register. If this is the
8859 first 'g' packet, we might be overly optimistic about its
8860 contents, so fall back to 'p'. */
8861 if (reg
->in_g_packet
)
8863 fetch_registers_using_g (regcache
);
8864 if (reg
->in_g_packet
)
8868 if (fetch_register_using_p (regcache
, reg
))
8871 /* This register is not available. */
8872 regcache
->raw_supply (reg
->regnum
, NULL
);
8877 fetch_registers_using_g (regcache
);
8879 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8880 if (!rsa
->regs
[i
].in_g_packet
)
8881 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
8883 /* This register is not available. */
8884 regcache
->raw_supply (i
, NULL
);
8888 /* Prepare to store registers. Since we may send them all (using a
8889 'G' request), we have to read out the ones we don't want to change
8893 remote_target::prepare_to_store (struct regcache
*regcache
)
8895 struct remote_state
*rs
= get_remote_state ();
8896 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8899 /* Make sure the entire registers array is valid. */
8900 switch (m_features
.packet_support (PACKET_P
))
8902 case PACKET_DISABLE
:
8903 case PACKET_SUPPORT_UNKNOWN
:
8904 /* Make sure all the necessary registers are cached. */
8905 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8906 if (rsa
->regs
[i
].in_g_packet
)
8907 regcache
->raw_update (rsa
->regs
[i
].regnum
);
8914 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
8915 packet was not recognized. */
8918 remote_target::store_register_using_P (const struct regcache
*regcache
,
8921 struct gdbarch
*gdbarch
= regcache
->arch ();
8922 struct remote_state
*rs
= get_remote_state ();
8923 /* Try storing a single register. */
8924 char *buf
= rs
->buf
.data ();
8925 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8928 if (m_features
.packet_support (PACKET_P
) == PACKET_DISABLE
)
8931 if (reg
->pnum
== -1)
8934 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
8935 p
= buf
+ strlen (buf
);
8936 regcache
->raw_collect (reg
->regnum
, regp
);
8937 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
8941 switch (m_features
.packet_ok (rs
->buf
, PACKET_P
))
8946 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8947 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
.data ());
8948 case PACKET_UNKNOWN
:
8951 internal_error (_("Bad result from packet_ok"));
8955 /* Store register REGNUM, or all registers if REGNUM == -1, from the
8956 contents of the register cache buffer. FIXME: ignores errors. */
8959 remote_target::store_registers_using_G (const struct regcache
*regcache
)
8961 struct remote_state
*rs
= get_remote_state ();
8962 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8966 /* Extract all the registers in the regcache copying them into a
8971 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
8972 memset (regs
, 0, rsa
->sizeof_g_packet
);
8973 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8975 struct packet_reg
*r
= &rsa
->regs
[i
];
8978 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
8982 /* Command describes registers byte by byte,
8983 each byte encoded as two hex characters. */
8984 p
= rs
->buf
.data ();
8986 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
8989 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8990 error (_("Could not write registers; remote failure reply '%s'"),
8994 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8995 of the register cache buffer. FIXME: ignores errors. */
8998 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
9000 struct gdbarch
*gdbarch
= regcache
->arch ();
9001 struct remote_state
*rs
= get_remote_state ();
9002 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
9005 set_remote_traceframe ();
9006 set_general_thread (regcache
->ptid ());
9010 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
9012 gdb_assert (reg
!= NULL
);
9014 /* Always prefer to store registers using the 'P' packet if
9015 possible; we often change only a small number of registers.
9016 Sometimes we change a larger number; we'd need help from a
9017 higher layer to know to use 'G'. */
9018 if (store_register_using_P (regcache
, reg
))
9021 /* For now, don't complain if we have no way to write the
9022 register. GDB loses track of unavailable registers too
9023 easily. Some day, this may be an error. We don't have
9024 any way to read the register, either... */
9025 if (!reg
->in_g_packet
)
9028 store_registers_using_G (regcache
);
9032 store_registers_using_G (regcache
);
9034 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
9035 if (!rsa
->regs
[i
].in_g_packet
)
9036 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
9037 /* See above for why we do not issue an error here. */
9042 /* Return the number of hex digits in num. */
9045 hexnumlen (ULONGEST num
)
9049 for (i
= 0; num
!= 0; i
++)
9052 return std::max (i
, 1);
9055 /* Set BUF to the minimum number of hex digits representing NUM. */
9058 hexnumstr (char *buf
, ULONGEST num
)
9060 int len
= hexnumlen (num
);
9062 return hexnumnstr (buf
, num
, len
);
9066 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
9069 hexnumnstr (char *buf
, ULONGEST num
, int width
)
9075 for (i
= width
- 1; i
>= 0; i
--)
9077 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
9084 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
9087 remote_address_masked (CORE_ADDR addr
)
9089 unsigned int address_size
= remote_address_size
;
9091 /* If "remoteaddresssize" was not set, default to target address size. */
9093 address_size
= gdbarch_addr_bit (current_inferior ()->arch ());
9095 if (address_size
> 0
9096 && address_size
< (sizeof (ULONGEST
) * 8))
9098 /* Only create a mask when that mask can safely be constructed
9099 in a ULONGEST variable. */
9102 mask
= (mask
<< address_size
) - 1;
9108 /* Determine whether the remote target supports binary downloading.
9109 This is accomplished by sending a no-op memory write of zero length
9110 to the target at the specified address. It does not suffice to send
9111 the whole packet, since many stubs strip the eighth bit and
9112 subsequently compute a wrong checksum, which causes real havoc with
9115 NOTE: This can still lose if the serial line is not eight-bit
9116 clean. In cases like this, the user should clear "remote
9120 remote_target::check_binary_download (CORE_ADDR addr
)
9122 struct remote_state
*rs
= get_remote_state ();
9124 switch (m_features
.packet_support (PACKET_X
))
9126 case PACKET_DISABLE
:
9130 case PACKET_SUPPORT_UNKNOWN
:
9134 p
= rs
->buf
.data ();
9136 p
+= hexnumstr (p
, (ULONGEST
) addr
);
9138 p
+= hexnumstr (p
, (ULONGEST
) 0);
9142 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9145 if (rs
->buf
[0] == '\0')
9147 remote_debug_printf ("binary downloading NOT supported by target");
9148 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
9152 remote_debug_printf ("binary downloading supported by target");
9153 m_features
.m_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
9160 /* Helper function to resize the payload in order to try to get a good
9161 alignment. We try to write an amount of data such that the next write will
9162 start on an address aligned on REMOTE_ALIGN_WRITES. */
9165 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
9167 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
9170 /* Write memory data directly to the remote machine.
9171 This does not inform the data cache; the data cache uses this.
9172 HEADER is the starting part of the packet.
9173 MEMADDR is the address in the remote memory space.
9174 MYADDR is the address of the buffer in our space.
9175 LEN_UNITS is the number of addressable units to write.
9176 UNIT_SIZE is the length in bytes of an addressable unit.
9177 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
9178 should send data as binary ('X'), or hex-encoded ('M').
9180 The function creates packet of the form
9181 <HEADER><ADDRESS>,<LENGTH>:<DATA>
9183 where encoding of <DATA> is terminated by PACKET_FORMAT.
9185 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
9188 Return the transferred status, error or OK (an
9189 'enum target_xfer_status' value). Save the number of addressable units
9190 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
9192 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
9193 exchange between gdb and the stub could look like (?? in place of the
9199 -> $M1000,3:eeeeffffeeee#??
9203 <- eeeeffffeeeedddd */
9206 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
9207 const gdb_byte
*myaddr
,
9210 ULONGEST
*xfered_len_units
,
9211 char packet_format
, int use_length
)
9213 struct remote_state
*rs
= get_remote_state ();
9219 int payload_capacity_bytes
;
9220 int payload_length_bytes
;
9222 if (packet_format
!= 'X' && packet_format
!= 'M')
9223 internal_error (_("remote_write_bytes_aux: bad packet format"));
9226 return TARGET_XFER_EOF
;
9228 payload_capacity_bytes
= get_memory_write_packet_size ();
9230 /* The packet buffer will be large enough for the payload;
9231 get_memory_packet_size ensures this. */
9234 /* Compute the size of the actual payload by subtracting out the
9235 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9237 payload_capacity_bytes
-= strlen ("$,:#NN");
9239 /* The comma won't be used. */
9240 payload_capacity_bytes
+= 1;
9241 payload_capacity_bytes
-= strlen (header
);
9242 payload_capacity_bytes
-= hexnumlen (memaddr
);
9244 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9246 strcat (rs
->buf
.data (), header
);
9247 p
= rs
->buf
.data () + strlen (header
);
9249 /* Compute a best guess of the number of bytes actually transfered. */
9250 if (packet_format
== 'X')
9252 /* Best guess at number of bytes that will fit. */
9253 todo_units
= std::min (len_units
,
9254 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9256 payload_capacity_bytes
-= hexnumlen (todo_units
);
9257 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9261 /* Number of bytes that will fit. */
9263 = std::min (len_units
,
9264 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9266 payload_capacity_bytes
-= hexnumlen (todo_units
);
9267 todo_units
= std::min (todo_units
,
9268 (payload_capacity_bytes
/ unit_size
) / 2);
9271 if (todo_units
<= 0)
9272 internal_error (_("minimum packet size too small to write data"));
9274 /* If we already need another packet, then try to align the end
9275 of this packet to a useful boundary. */
9276 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9277 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9279 /* Append "<memaddr>". */
9280 memaddr
= remote_address_masked (memaddr
);
9281 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9288 /* Append the length and retain its location and size. It may need to be
9289 adjusted once the packet body has been created. */
9291 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9299 /* Append the packet body. */
9300 if (packet_format
== 'X')
9302 /* Binary mode. Send target system values byte by byte, in
9303 increasing byte addresses. Only escape certain critical
9305 payload_length_bytes
=
9306 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9307 &units_written
, payload_capacity_bytes
);
9309 /* If not all TODO units fit, then we'll need another packet. Make
9310 a second try to keep the end of the packet aligned. Don't do
9311 this if the packet is tiny. */
9312 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9316 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9318 if (new_todo_units
!= units_written
)
9319 payload_length_bytes
=
9320 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9321 (gdb_byte
*) p
, &units_written
,
9322 payload_capacity_bytes
);
9325 p
+= payload_length_bytes
;
9326 if (use_length
&& units_written
< todo_units
)
9328 /* Escape chars have filled up the buffer prematurely,
9329 and we have actually sent fewer units than planned.
9330 Fix-up the length field of the packet. Use the same
9331 number of characters as before. */
9332 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9334 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9339 /* Normal mode: Send target system values byte by byte, in
9340 increasing byte addresses. Each byte is encoded as a two hex
9342 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9343 units_written
= todo_units
;
9346 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9349 if (rs
->buf
[0] == 'E')
9350 return TARGET_XFER_E_IO
;
9352 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9353 send fewer units than we'd planned. */
9354 *xfered_len_units
= (ULONGEST
) units_written
;
9355 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9358 /* Write memory data directly to the remote machine.
9359 This does not inform the data cache; the data cache uses this.
9360 MEMADDR is the address in the remote memory space.
9361 MYADDR is the address of the buffer in our space.
9362 LEN is the number of bytes.
9364 Return the transferred status, error or OK (an
9365 'enum target_xfer_status' value). Save the number of bytes
9366 transferred in *XFERED_LEN. Only transfer a single packet. */
9369 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9370 ULONGEST len
, int unit_size
,
9371 ULONGEST
*xfered_len
)
9373 const char *packet_format
= NULL
;
9375 /* Check whether the target supports binary download. */
9376 check_binary_download (memaddr
);
9378 switch (m_features
.packet_support (PACKET_X
))
9381 packet_format
= "X";
9383 case PACKET_DISABLE
:
9384 packet_format
= "M";
9386 case PACKET_SUPPORT_UNKNOWN
:
9387 internal_error (_("remote_write_bytes: bad internal state"));
9389 internal_error (_("bad switch"));
9392 return remote_write_bytes_aux (packet_format
,
9393 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9394 packet_format
[0], 1);
9397 /* Read memory data directly from the remote machine.
9398 This does not use the data cache; the data cache uses this.
9399 MEMADDR is the address in the remote memory space.
9400 MYADDR is the address of the buffer in our space.
9401 LEN_UNITS is the number of addressable memory units to read..
9402 UNIT_SIZE is the length in bytes of an addressable unit.
9404 Return the transferred status, error or OK (an
9405 'enum target_xfer_status' value). Save the number of bytes
9406 transferred in *XFERED_LEN_UNITS.
9408 See the comment of remote_write_bytes_aux for an example of
9409 memory read/write exchange between gdb and the stub. */
9412 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9414 int unit_size
, ULONGEST
*xfered_len_units
)
9416 struct remote_state
*rs
= get_remote_state ();
9417 int buf_size_bytes
; /* Max size of packet output buffer. */
9422 buf_size_bytes
= get_memory_read_packet_size ();
9423 /* The packet buffer will be large enough for the payload;
9424 get_memory_packet_size ensures this. */
9426 /* Number of units that will fit. */
9427 todo_units
= std::min (len_units
,
9428 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9430 /* Construct "m"<memaddr>","<len>". */
9431 memaddr
= remote_address_masked (memaddr
);
9432 p
= rs
->buf
.data ();
9434 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9436 p
+= hexnumstr (p
, (ULONGEST
) todo_units
);
9440 if (rs
->buf
[0] == 'E'
9441 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
9442 && rs
->buf
[3] == '\0')
9443 return TARGET_XFER_E_IO
;
9444 /* Reply describes memory byte by byte, each byte encoded as two hex
9446 p
= rs
->buf
.data ();
9447 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9448 /* Return what we have. Let higher layers handle partial reads. */
9449 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9450 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9453 /* Using the set of read-only target sections of remote, read live
9456 For interface/parameters/return description see target.h,
9460 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9464 ULONGEST
*xfered_len
)
9466 const struct target_section
*secp
;
9468 secp
= target_section_by_addr (this, memaddr
);
9470 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9472 ULONGEST memend
= memaddr
+ len
;
9474 const std::vector
<target_section
> *table
9475 = target_get_section_table (this);
9476 for (const target_section
&p
: *table
)
9478 if (memaddr
>= p
.addr
)
9480 if (memend
<= p
.endaddr
)
9482 /* Entire transfer is within this section. */
9483 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9486 else if (memaddr
>= p
.endaddr
)
9488 /* This section ends before the transfer starts. */
9493 /* This section overlaps the transfer. Just do half. */
9494 len
= p
.endaddr
- memaddr
;
9495 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9502 return TARGET_XFER_EOF
;
9505 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9506 first if the requested memory is unavailable in traceframe.
9507 Otherwise, fall back to remote_read_bytes_1. */
9510 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9511 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9512 ULONGEST
*xfered_len
)
9515 return TARGET_XFER_EOF
;
9517 if (get_traceframe_number () != -1)
9519 std::vector
<mem_range
> available
;
9521 /* If we fail to get the set of available memory, then the
9522 target does not support querying traceframe info, and so we
9523 attempt reading from the traceframe anyway (assuming the
9524 target implements the old QTro packet then). */
9525 if (traceframe_available_memory (&available
, memaddr
, len
))
9527 if (available
.empty () || available
[0].start
!= memaddr
)
9529 enum target_xfer_status res
;
9531 /* Don't read into the traceframe's available
9533 if (!available
.empty ())
9535 LONGEST oldlen
= len
;
9537 len
= available
[0].start
- memaddr
;
9538 gdb_assert (len
<= oldlen
);
9541 /* This goes through the topmost target again. */
9542 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9543 len
, unit_size
, xfered_len
);
9544 if (res
== TARGET_XFER_OK
)
9545 return TARGET_XFER_OK
;
9548 /* No use trying further, we know some memory starting
9549 at MEMADDR isn't available. */
9551 return (*xfered_len
!= 0) ?
9552 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9556 /* Don't try to read more than how much is available, in
9557 case the target implements the deprecated QTro packet to
9558 cater for older GDBs (the target's knowledge of read-only
9559 sections may be outdated by now). */
9560 len
= available
[0].length
;
9564 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9569 /* Sends a packet with content determined by the printf format string
9570 FORMAT and the remaining arguments, then gets the reply. Returns
9571 whether the packet was a success, a failure, or unknown. */
9574 remote_target::remote_send_printf (const char *format
, ...)
9576 struct remote_state
*rs
= get_remote_state ();
9577 int max_size
= get_remote_packet_size ();
9580 va_start (ap
, format
);
9583 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9587 if (size
>= max_size
)
9588 internal_error (_("Too long remote packet."));
9590 if (putpkt (rs
->buf
) < 0)
9591 error (_("Communication problem with target."));
9596 return packet_check_result (rs
->buf
);
9599 /* Flash writing can take quite some time. We'll set
9600 effectively infinite timeout for flash operations.
9601 In future, we'll need to decide on a better approach. */
9602 static const int remote_flash_timeout
= 1000;
9605 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9607 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
9608 enum packet_result ret
;
9609 scoped_restore restore_timeout
9610 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9612 ret
= remote_send_printf ("vFlashErase:%s,%s",
9613 phex (address
, addr_size
),
9617 case PACKET_UNKNOWN
:
9618 error (_("Remote target does not support flash erase"));
9620 error (_("Error erasing flash with vFlashErase packet"));
9627 remote_target::remote_flash_write (ULONGEST address
,
9628 ULONGEST length
, ULONGEST
*xfered_len
,
9629 const gdb_byte
*data
)
9631 scoped_restore restore_timeout
9632 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9633 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9638 remote_target::flash_done ()
9642 scoped_restore restore_timeout
9643 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9645 ret
= remote_send_printf ("vFlashDone");
9649 case PACKET_UNKNOWN
:
9650 error (_("Remote target does not support vFlashDone"));
9652 error (_("Error finishing flash operation"));
9659 /* Stuff for dealing with the packets which are part of this protocol.
9660 See comment at top of file for details. */
9662 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
9663 error to higher layers. Called when a serial error is detected.
9664 The exception message is STRING, followed by a colon and a blank,
9665 the system error message for errno at function entry and final dot
9666 for output compatibility with throw_perror_with_name. */
9669 unpush_and_perror (remote_target
*target
, const char *string
)
9671 int saved_errno
= errno
;
9673 remote_unpush_target (target
);
9674 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
9675 safe_strerror (saved_errno
));
9678 /* Read a single character from the remote end. The current quit
9679 handler is overridden to avoid quitting in the middle of packet
9680 sequence, as that would break communication with the remote server.
9681 See remote_serial_quit_handler for more detail. */
9684 remote_target::readchar (int timeout
)
9687 struct remote_state
*rs
= get_remote_state ();
9690 scoped_restore restore_quit_target
9691 = make_scoped_restore (&curr_quit_handler_target
, this);
9692 scoped_restore restore_quit
9693 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9695 rs
->got_ctrlc_during_io
= 0;
9697 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9699 if (rs
->got_ctrlc_during_io
)
9706 switch ((enum serial_rc
) ch
)
9709 remote_unpush_target (this);
9710 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
9713 unpush_and_perror (this, _("Remote communication error. "
9714 "Target disconnected"));
9716 case SERIAL_TIMEOUT
:
9722 /* Wrapper for serial_write that closes the target and throws if
9723 writing fails. The current quit handler is overridden to avoid
9724 quitting in the middle of packet sequence, as that would break
9725 communication with the remote server. See
9726 remote_serial_quit_handler for more detail. */
9729 remote_target::remote_serial_write (const char *str
, int len
)
9731 struct remote_state
*rs
= get_remote_state ();
9733 scoped_restore restore_quit_target
9734 = make_scoped_restore (&curr_quit_handler_target
, this);
9735 scoped_restore restore_quit
9736 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9738 rs
->got_ctrlc_during_io
= 0;
9740 if (serial_write (rs
->remote_desc
, str
, len
))
9742 unpush_and_perror (this, _("Remote communication error. "
9743 "Target disconnected"));
9746 if (rs
->got_ctrlc_during_io
)
9750 /* Return a string representing an escaped version of BUF, of len N.
9751 E.g. \n is converted to \\n, \t to \\t, etc. */
9754 escape_buffer (const char *buf
, int n
)
9758 stb
.putstrn (buf
, n
, '\\');
9759 return stb
.release ();
9763 remote_target::putpkt (const char *buf
)
9765 return putpkt_binary (buf
, strlen (buf
));
9768 /* Wrapper around remote_target::putpkt to avoid exporting
9772 putpkt (remote_target
*remote
, const char *buf
)
9774 return remote
->putpkt (buf
);
9777 /* Send a packet to the remote machine, with error checking. The data
9778 of the packet is in BUF. The string in BUF can be at most
9779 get_remote_packet_size () - 5 to account for the $, # and checksum,
9780 and for a possible /0 if we are debugging (remote_debug) and want
9781 to print the sent packet as a string. */
9784 remote_target::putpkt_binary (const char *buf
, int cnt
)
9786 struct remote_state
*rs
= get_remote_state ();
9788 unsigned char csum
= 0;
9789 gdb::def_vector
<char> data (cnt
+ 6);
9790 char *buf2
= data
.data ();
9796 /* Catch cases like trying to read memory or listing threads while
9797 we're waiting for a stop reply. The remote server wouldn't be
9798 ready to handle this request, so we'd hang and timeout. We don't
9799 have to worry about this in synchronous mode, because in that
9800 case it's not possible to issue a command while the target is
9801 running. This is not a problem in non-stop mode, because in that
9802 case, the stub is always ready to process serial input. */
9803 if (!target_is_non_stop_p ()
9804 && target_is_async_p ()
9805 && rs
->waiting_for_stop_reply
)
9807 error (_("Cannot execute this command while the target is running.\n"
9808 "Use the \"interrupt\" command to stop the target\n"
9809 "and then try again."));
9812 /* Copy the packet into buffer BUF2, encapsulating it
9813 and giving it a checksum. */
9818 for (i
= 0; i
< cnt
; i
++)
9824 *p
++ = tohex ((csum
>> 4) & 0xf);
9825 *p
++ = tohex (csum
& 0xf);
9827 /* Send it over and over until we get a positive ack. */
9835 int len
= (int) (p
- buf2
);
9838 if (remote_packet_max_chars
< 0)
9841 max_chars
= remote_packet_max_chars
;
9844 = escape_buffer (buf2
, std::min (len
, max_chars
));
9846 if (len
> max_chars
)
9847 remote_debug_printf_nofunc
9848 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
9851 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
9853 remote_serial_write (buf2
, p
- buf2
);
9855 /* If this is a no acks version of the remote protocol, send the
9856 packet and move on. */
9860 /* Read until either a timeout occurs (-2) or '+' is read.
9861 Handle any notification that arrives in the mean time. */
9864 ch
= readchar (remote_timeout
);
9869 remote_debug_printf_nofunc ("Received Ack");
9872 remote_debug_printf_nofunc ("Received Nak");
9874 case SERIAL_TIMEOUT
:
9878 break; /* Retransmit buffer. */
9881 remote_debug_printf ("Packet instead of Ack, ignoring it");
9882 /* It's probably an old response sent because an ACK
9883 was lost. Gobble up the packet and ack it so it
9884 doesn't get retransmitted when we resend this
9887 remote_serial_write ("+", 1);
9888 continue; /* Now, go look for +. */
9895 /* If we got a notification, handle it, and go back to looking
9897 /* We've found the start of a notification. Now
9898 collect the data. */
9899 val
= read_frame (&rs
->buf
);
9902 remote_debug_printf_nofunc
9903 (" Notification received: %s",
9904 escape_buffer (rs
->buf
.data (), val
).c_str ());
9906 handle_notification (rs
->notif_state
, rs
->buf
.data ());
9907 /* We're in sync now, rewait for the ack. */
9911 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9917 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9921 break; /* Here to retransmit. */
9925 /* This is wrong. If doing a long backtrace, the user should be
9926 able to get out next time we call QUIT, without anything as
9927 violent as interrupt_query. If we want to provide a way out of
9928 here without getting to the next QUIT, it should be based on
9929 hitting ^C twice as in remote_wait. */
9941 /* Come here after finding the start of a frame when we expected an
9942 ack. Do our best to discard the rest of this packet. */
9945 remote_target::skip_frame ()
9951 c
= readchar (remote_timeout
);
9954 case SERIAL_TIMEOUT
:
9955 /* Nothing we can do. */
9958 /* Discard the two bytes of checksum and stop. */
9959 c
= readchar (remote_timeout
);
9961 c
= readchar (remote_timeout
);
9964 case '*': /* Run length encoding. */
9965 /* Discard the repeat count. */
9966 c
= readchar (remote_timeout
);
9971 /* A regular character. */
9977 /* Come here after finding the start of the frame. Collect the rest
9978 into *BUF, verifying the checksum, length, and handling run-length
9979 compression. NUL terminate the buffer. If there is not enough room,
9982 Returns -1 on error, number of characters in buffer (ignoring the
9983 trailing NULL) on success. (could be extended to return one of the
9984 SERIAL status indications). */
9987 remote_target::read_frame (gdb::char_vector
*buf_p
)
9992 char *buf
= buf_p
->data ();
9993 struct remote_state
*rs
= get_remote_state ();
10000 c
= readchar (remote_timeout
);
10003 case SERIAL_TIMEOUT
:
10004 remote_debug_printf ("Timeout in mid-packet, retrying");
10008 remote_debug_printf ("Saw new packet start in middle of old one");
10009 return -1; /* Start a new packet, count retries. */
10013 unsigned char pktcsum
;
10019 check_0
= readchar (remote_timeout
);
10021 check_1
= readchar (remote_timeout
);
10023 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
10025 remote_debug_printf ("Timeout in checksum, retrying");
10028 else if (check_0
< 0 || check_1
< 0)
10030 remote_debug_printf ("Communication error in checksum");
10034 /* Don't recompute the checksum; with no ack packets we
10035 don't have any way to indicate a packet retransmission
10037 if (rs
->noack_mode
)
10040 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
10041 if (csum
== pktcsum
)
10044 remote_debug_printf
10045 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
10046 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
10048 /* Number of characters in buffer ignoring trailing
10052 case '*': /* Run length encoding. */
10057 c
= readchar (remote_timeout
);
10059 repeat
= c
- ' ' + 3; /* Compute repeat count. */
10061 /* The character before ``*'' is repeated. */
10063 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
10065 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
10067 /* Make some more room in the buffer. */
10068 buf_p
->resize (buf_p
->size () + repeat
);
10069 buf
= buf_p
->data ();
10072 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
10078 gdb_printf (_("Invalid run length encoding: %s\n"), buf
);
10082 if (bc
>= buf_p
->size () - 1)
10084 /* Make some more room in the buffer. */
10085 buf_p
->resize (buf_p
->size () * 2);
10086 buf
= buf_p
->data ();
10096 /* Set this to the maximum number of seconds to wait instead of waiting forever
10097 in target_wait(). If this timer times out, then it generates an error and
10098 the command is aborted. This replaces most of the need for timeouts in the
10099 GDB test suite, and makes it possible to distinguish between a hung target
10100 and one with slow communications. */
10102 static int watchdog
= 0;
10104 show_watchdog (struct ui_file
*file
, int from_tty
,
10105 struct cmd_list_element
*c
, const char *value
)
10107 gdb_printf (file
, _("Watchdog timer is %s.\n"), value
);
10110 /* Read a packet from the remote machine, with error checking, and
10111 store it in *BUF. Resize *BUF if necessary to hold the result. If
10112 FOREVER, wait forever rather than timing out; this is used (in
10113 synchronous mode) to wait for a target that is is executing user
10114 code to stop. If FOREVER == false, this function is allowed to time
10115 out gracefully and return an indication of this to the caller.
10116 Otherwise return the number of bytes read. If IS_NOTIF is not
10117 NULL, then consider receiving a notification enough reason to
10118 return to the caller. In this case, *IS_NOTIF is an output boolean
10119 that indicates whether *BUF holds a notification or not (a regular
10123 remote_target::getpkt (gdb::char_vector
*buf
, bool forever
, bool *is_notif
)
10125 struct remote_state
*rs
= get_remote_state ();
10131 strcpy (buf
->data (), "timeout");
10134 timeout
= watchdog
> 0 ? watchdog
: -1;
10135 else if (is_notif
!= nullptr)
10136 timeout
= 0; /* There should already be a char in the buffer. If
10139 timeout
= remote_timeout
;
10141 #define MAX_TRIES 3
10143 /* Process any number of notifications, and then return when
10144 we get a packet. */
10147 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
10149 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
10151 /* This can loop forever if the remote side sends us
10152 characters continuously, but if it pauses, we'll get
10153 SERIAL_TIMEOUT from readchar because of timeout. Then
10154 we'll count that as a retry.
10156 Note that even when forever is set, we will only wait
10157 forever prior to the start of a packet. After that, we
10158 expect characters to arrive at a brisk pace. They should
10159 show up within remote_timeout intervals. */
10161 c
= readchar (timeout
);
10162 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
10164 if (c
== SERIAL_TIMEOUT
)
10166 if (is_notif
!= nullptr)
10167 return -1; /* Don't complain, it's normal to not get
10168 anything in this case. */
10170 if (forever
) /* Watchdog went off? Kill the target. */
10172 remote_unpush_target (this);
10173 throw_error (TARGET_CLOSE_ERROR
,
10174 _("Watchdog timeout has expired. "
10175 "Target detached."));
10178 remote_debug_printf ("Timed out.");
10182 /* We've found the start of a packet or notification.
10183 Now collect the data. */
10184 val
= read_frame (buf
);
10189 remote_serial_write ("-", 1);
10192 if (tries
> MAX_TRIES
)
10194 /* We have tried hard enough, and just can't receive the
10195 packet/notification. Give up. */
10196 gdb_printf (_("Ignoring packet error, continuing...\n"));
10198 /* Skip the ack char if we're in no-ack mode. */
10199 if (!rs
->noack_mode
)
10200 remote_serial_write ("+", 1);
10204 /* If we got an ordinary packet, return that to our caller. */
10211 if (remote_packet_max_chars
< 0)
10214 max_chars
= remote_packet_max_chars
;
10217 = escape_buffer (buf
->data (),
10218 std::min (val
, max_chars
));
10220 if (val
> max_chars
)
10221 remote_debug_printf_nofunc
10222 ("Packet received: %s [%d bytes omitted]", str
.c_str (),
10225 remote_debug_printf_nofunc ("Packet received: %s",
10229 /* Skip the ack char if we're in no-ack mode. */
10230 if (!rs
->noack_mode
)
10231 remote_serial_write ("+", 1);
10232 if (is_notif
!= NULL
)
10237 /* If we got a notification, handle it, and go back to looking
10241 gdb_assert (c
== '%');
10243 remote_debug_printf_nofunc
10244 (" Notification received: %s",
10245 escape_buffer (buf
->data (), val
).c_str ());
10247 if (is_notif
!= NULL
)
10250 handle_notification (rs
->notif_state
, buf
->data ());
10252 /* Notifications require no acknowledgement. */
10254 if (is_notif
!= nullptr)
10260 /* Kill any new fork children of inferior INF that haven't been
10261 processed by follow_fork. */
10264 remote_target::kill_new_fork_children (inferior
*inf
)
10266 remote_state
*rs
= get_remote_state ();
10267 const notif_client
*notif
= ¬if_client_stop
;
10269 /* Kill the fork child threads of any threads in inferior INF that are stopped
10270 at a fork event. */
10271 for (thread_info
*thread
: inf
->non_exited_threads ())
10273 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10278 int child_pid
= ws
->child_ptid ().pid ();
10279 int res
= remote_vkill (child_pid
);
10282 error (_("Can't kill fork child process %d"), child_pid
);
10285 /* Check for any pending fork events (not reported or processed yet)
10286 in inferior INF and kill those fork child threads as well. */
10287 remote_notif_get_pending_events (notif
);
10288 for (auto &event
: rs
->stop_reply_queue
)
10290 if (event
->ptid
.pid () != inf
->pid
)
10293 if (!is_fork_status (event
->ws
.kind ()))
10296 int child_pid
= event
->ws
.child_ptid ().pid ();
10297 int res
= remote_vkill (child_pid
);
10300 error (_("Can't kill fork child process %d"), child_pid
);
10305 /* Target hook to kill the current inferior. */
10308 remote_target::kill ()
10311 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10313 gdb_assert (inf
!= nullptr);
10315 if (m_features
.packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10317 /* If we're stopped while forking and we haven't followed yet,
10318 kill the child task. We need to do this before killing the
10319 parent task because if this is a vfork then the parent will
10321 kill_new_fork_children (inf
);
10323 res
= remote_vkill (inf
->pid
);
10326 target_mourn_inferior (inferior_ptid
);
10331 /* If we are in 'target remote' mode and we are killing the only
10332 inferior, then we will tell gdbserver to exit and unpush the
10334 if (res
== -1 && !m_features
.remote_multi_process_p ()
10335 && number_of_live_inferiors (this) == 1)
10339 /* We've killed the remote end, we get to mourn it. If we are
10340 not in extended mode, mourning the inferior also unpushes
10341 remote_ops from the target stack, which closes the remote
10343 target_mourn_inferior (inferior_ptid
);
10348 error (_("Can't kill process"));
10351 /* Send a kill request to the target using the 'vKill' packet. */
10354 remote_target::remote_vkill (int pid
)
10356 if (m_features
.packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10359 remote_state
*rs
= get_remote_state ();
10361 /* Tell the remote target to detach. */
10362 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10366 switch (m_features
.packet_ok (rs
->buf
, PACKET_vKill
))
10372 case PACKET_UNKNOWN
:
10375 internal_error (_("Bad result from packet_ok"));
10379 /* Send a kill request to the target using the 'k' packet. */
10382 remote_target::remote_kill_k ()
10384 /* Catch errors so the user can quit from gdb even when we
10385 aren't on speaking terms with the remote system. */
10390 catch (const gdb_exception_error
&ex
)
10392 if (ex
.error
== TARGET_CLOSE_ERROR
)
10394 /* If we got an (EOF) error that caused the target
10395 to go away, then we're done, that's what we wanted.
10396 "k" is susceptible to cause a premature EOF, given
10397 that the remote server isn't actually required to
10398 reply to "k", and it can happen that it doesn't
10399 even get to reply ACK to the "k". */
10403 /* Otherwise, something went wrong. We didn't actually kill
10404 the target. Just propagate the exception, and let the
10405 user or higher layers decide what to do. */
10411 remote_target::mourn_inferior ()
10413 struct remote_state
*rs
= get_remote_state ();
10415 /* We're no longer interested in notification events of an inferior
10416 that exited or was killed/detached. */
10417 discard_pending_stop_replies (current_inferior ());
10419 /* In 'target remote' mode with one inferior, we close the connection. */
10420 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10422 remote_unpush_target (this);
10426 /* In case we got here due to an error, but we're going to stay
10428 rs
->waiting_for_stop_reply
= 0;
10430 /* If the current general thread belonged to the process we just
10431 detached from or has exited, the remote side current general
10432 thread becomes undefined. Considering a case like this:
10434 - We just got here due to a detach.
10435 - The process that we're detaching from happens to immediately
10436 report a global breakpoint being hit in non-stop mode, in the
10437 same thread we had selected before.
10438 - GDB attaches to this process again.
10439 - This event happens to be the next event we handle.
10441 GDB would consider that the current general thread didn't need to
10442 be set on the stub side (with Hg), since for all it knew,
10443 GENERAL_THREAD hadn't changed.
10445 Notice that although in all-stop mode, the remote server always
10446 sets the current thread to the thread reporting the stop event,
10447 that doesn't happen in non-stop mode; in non-stop, the stub *must
10448 not* change the current thread when reporting a breakpoint hit,
10449 due to the decoupling of event reporting and event handling.
10451 To keep things simple, we always invalidate our notion of the
10453 record_currthread (rs
, minus_one_ptid
);
10455 /* Call common code to mark the inferior as not running. */
10456 generic_mourn_inferior ();
10460 extended_remote_target::supports_disable_randomization ()
10462 return (m_features
.packet_support (PACKET_QDisableRandomization
)
10467 remote_target::extended_remote_disable_randomization (int val
)
10469 struct remote_state
*rs
= get_remote_state ();
10472 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10473 "QDisableRandomization:%x", val
);
10475 reply
= remote_get_noisy_reply ();
10476 if (*reply
== '\0')
10477 error (_("Target does not support QDisableRandomization."));
10478 if (strcmp (reply
, "OK") != 0)
10479 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10483 remote_target::extended_remote_run (const std::string
&args
)
10485 struct remote_state
*rs
= get_remote_state ();
10487 const char *remote_exec_file
= get_remote_exec_file ();
10489 /* If the user has disabled vRun support, or we have detected that
10490 support is not available, do not try it. */
10491 if (m_features
.packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10494 strcpy (rs
->buf
.data (), "vRun;");
10495 len
= strlen (rs
->buf
.data ());
10497 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10498 error (_("Remote file name too long for run packet"));
10499 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10500 strlen (remote_exec_file
));
10502 if (!args
.empty ())
10506 gdb_argv
argv (args
.c_str ());
10507 for (i
= 0; argv
[i
] != NULL
; i
++)
10509 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10510 error (_("Argument list too long for run packet"));
10511 rs
->buf
[len
++] = ';';
10512 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10517 rs
->buf
[len
++] = '\0';
10522 switch (m_features
.packet_ok (rs
->buf
, PACKET_vRun
))
10525 /* We have a wait response. All is well. */
10527 case PACKET_UNKNOWN
:
10530 if (remote_exec_file
[0] == '\0')
10531 error (_("Running the default executable on the remote target failed; "
10532 "try \"set remote exec-file\"?"));
10534 error (_("Running \"%s\" on the remote target failed"),
10537 gdb_assert_not_reached ("bad switch");
10541 /* Helper function to send set/unset environment packets. ACTION is
10542 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10543 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10547 remote_target::send_environment_packet (const char *action
,
10548 const char *packet
,
10551 remote_state
*rs
= get_remote_state ();
10553 /* Convert the environment variable to an hex string, which
10554 is the best format to be transmitted over the wire. */
10555 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10558 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10559 "%s:%s", packet
, encoded_value
.c_str ());
10563 if (strcmp (rs
->buf
.data (), "OK") != 0)
10564 warning (_("Unable to %s environment variable '%s' on remote."),
10568 /* Helper function to handle the QEnvironment* packets. */
10571 remote_target::extended_remote_environment_support ()
10573 remote_state
*rs
= get_remote_state ();
10575 if (m_features
.packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10577 putpkt ("QEnvironmentReset");
10579 if (strcmp (rs
->buf
.data (), "OK") != 0)
10580 warning (_("Unable to reset environment on remote."));
10583 gdb_environ
*e
= ¤t_inferior ()->environment
;
10585 if (m_features
.packet_support (PACKET_QEnvironmentHexEncoded
)
10588 for (const std::string
&el
: e
->user_set_env ())
10589 send_environment_packet ("set", "QEnvironmentHexEncoded",
10594 if (m_features
.packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10595 for (const std::string
&el
: e
->user_unset_env ())
10596 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10599 /* Helper function to set the current working directory for the
10600 inferior in the remote target. */
10603 remote_target::extended_remote_set_inferior_cwd ()
10605 if (m_features
.packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10607 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10608 remote_state
*rs
= get_remote_state ();
10610 if (!inferior_cwd
.empty ())
10612 std::string hexpath
10613 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10614 inferior_cwd
.size ());
10616 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10617 "QSetWorkingDir:%s", hexpath
.c_str ());
10621 /* An empty inferior_cwd means that the user wants us to
10622 reset the remote server's inferior's cwd. */
10623 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10624 "QSetWorkingDir:");
10629 if (m_features
.packet_ok (rs
->buf
, PACKET_QSetWorkingDir
) != PACKET_OK
)
10631 Remote replied unexpectedly while setting the inferior's working\n\
10638 /* In the extended protocol we want to be able to do things like
10639 "run" and have them basically work as expected. So we need
10640 a special create_inferior function. We support changing the
10641 executable file and the command line arguments, but not the
10645 extended_remote_target::create_inferior (const char *exec_file
,
10646 const std::string
&args
,
10647 char **env
, int from_tty
)
10651 struct remote_state
*rs
= get_remote_state ();
10652 const char *remote_exec_file
= get_remote_exec_file ();
10654 /* If running asynchronously, register the target file descriptor
10655 with the event loop. */
10656 if (target_can_async_p ())
10657 target_async (true);
10659 /* Disable address space randomization if requested (and supported). */
10660 if (supports_disable_randomization ())
10661 extended_remote_disable_randomization (disable_randomization
);
10663 /* If startup-with-shell is on, we inform gdbserver to start the
10664 remote inferior using a shell. */
10665 if (m_features
.packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10667 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10668 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
10671 if (strcmp (rs
->buf
.data (), "OK") != 0)
10673 Remote replied unexpectedly while setting startup-with-shell: %s"),
10677 extended_remote_environment_support ();
10679 extended_remote_set_inferior_cwd ();
10681 /* Now restart the remote server. */
10682 run_worked
= extended_remote_run (args
) != -1;
10685 /* vRun was not supported. Fail if we need it to do what the
10687 if (remote_exec_file
[0])
10688 error (_("Remote target does not support \"set remote exec-file\""));
10689 if (!args
.empty ())
10690 error (_("Remote target does not support \"set args\" or run ARGS"));
10692 /* Fall back to "R". */
10693 extended_remote_restart ();
10696 /* vRun's success return is a stop reply. */
10697 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
10698 add_current_inferior_and_thread (stop_reply
);
10700 /* Get updated offsets, if the stub uses qOffsets. */
10705 /* Given a location's target info BP_TGT and the packet buffer BUF, output
10706 the list of conditions (in agent expression bytecode format), if any, the
10707 target needs to evaluate. The output is placed into the packet buffer
10708 started from BUF and ended at BUF_END. */
10711 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
10712 struct bp_target_info
*bp_tgt
, char *buf
,
10715 if (bp_tgt
->conditions
.empty ())
10718 buf
+= strlen (buf
);
10719 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
10722 /* Send conditions to the target. */
10723 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
10725 xsnprintf (buf
, buf_end
- buf
, "X%x,", (int) aexpr
->buf
.size ());
10726 buf
+= strlen (buf
);
10727 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
10728 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10735 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
10736 struct bp_target_info
*bp_tgt
, char *buf
)
10738 if (bp_tgt
->tcommands
.empty ())
10741 buf
+= strlen (buf
);
10743 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
10744 buf
+= strlen (buf
);
10746 /* Concatenate all the agent expressions that are commands into the
10748 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
10750 sprintf (buf
, "X%x,", (int) aexpr
->buf
.size ());
10751 buf
+= strlen (buf
);
10752 for (int i
= 0; i
< aexpr
->buf
.size (); ++i
)
10753 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10758 /* Insert a breakpoint. On targets that have software breakpoint
10759 support, we ask the remote target to do the work; on targets
10760 which don't, we insert a traditional memory breakpoint. */
10763 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
10764 struct bp_target_info
*bp_tgt
)
10766 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10767 If it succeeds, then set the support to PACKET_ENABLE. If it
10768 fails, and the user has explicitly requested the Z support then
10769 report an error, otherwise, mark it disabled and go on. */
10771 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10773 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10774 struct remote_state
*rs
;
10777 /* Make sure the remote is pointing at the right process, if
10779 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
10780 set_general_process ();
10782 rs
= get_remote_state ();
10783 p
= rs
->buf
.data ();
10784 endbuf
= p
+ get_remote_packet_size ();
10789 addr
= (ULONGEST
) remote_address_masked (addr
);
10790 p
+= hexnumstr (p
, addr
);
10791 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10793 if (supports_evaluation_of_breakpoint_conditions ())
10794 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10796 if (can_run_breakpoint_commands ())
10797 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10802 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z0
))
10808 case PACKET_UNKNOWN
:
10813 /* If this breakpoint has target-side commands but this stub doesn't
10814 support Z0 packets, throw error. */
10815 if (!bp_tgt
->tcommands
.empty ())
10816 throw_error (NOT_SUPPORTED_ERROR
, _("\
10817 Target doesn't support breakpoints that have target side commands."));
10819 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
10823 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
10824 struct bp_target_info
*bp_tgt
,
10825 enum remove_bp_reason reason
)
10827 CORE_ADDR addr
= bp_tgt
->placed_address
;
10828 struct remote_state
*rs
= get_remote_state ();
10830 if (m_features
.packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10832 char *p
= rs
->buf
.data ();
10833 char *endbuf
= p
+ get_remote_packet_size ();
10835 /* Make sure the remote is pointing at the right process, if
10837 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
10838 set_general_process ();
10844 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
10845 p
+= hexnumstr (p
, addr
);
10846 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10851 return (rs
->buf
[0] == 'E');
10854 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
10857 static enum Z_packet_type
10858 watchpoint_to_Z_packet (int type
)
10863 return Z_PACKET_WRITE_WP
;
10866 return Z_PACKET_READ_WP
;
10869 return Z_PACKET_ACCESS_WP
;
10872 internal_error (_("hw_bp_to_z: bad watchpoint type %d"), type
);
10877 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
10878 enum target_hw_bp_type type
, struct expression
*cond
)
10880 struct remote_state
*rs
= get_remote_state ();
10881 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10883 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10885 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
10886 + to_underlying (packet
))) == PACKET_DISABLE
)
10889 /* Make sure the remote is pointing at the right process, if
10891 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
10892 set_general_process ();
10894 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
10895 p
= strchr (rs
->buf
.data (), '\0');
10896 addr
= remote_address_masked (addr
);
10897 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10898 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10903 switch (m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
10904 + to_underlying (packet
))))
10908 case PACKET_UNKNOWN
:
10913 internal_error (_("remote_insert_watchpoint: reached end of function"));
10917 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
10918 CORE_ADDR start
, int length
)
10920 CORE_ADDR diff
= remote_address_masked (addr
- start
);
10922 return diff
< length
;
10927 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
10928 enum target_hw_bp_type type
, struct expression
*cond
)
10930 struct remote_state
*rs
= get_remote_state ();
10931 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10933 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10935 if (m_features
.packet_support ((to_underlying (PACKET_Z0
)
10936 + to_underlying (packet
))) == PACKET_DISABLE
)
10939 /* Make sure the remote is pointing at the right process, if
10941 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
10942 set_general_process ();
10944 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
10945 p
= strchr (rs
->buf
.data (), '\0');
10946 addr
= remote_address_masked (addr
);
10947 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10948 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10952 switch (m_features
.packet_ok (rs
->buf
, (to_underlying (PACKET_Z0
)
10953 + to_underlying (packet
))))
10956 case PACKET_UNKNOWN
:
10961 internal_error (_("remote_remove_watchpoint: reached end of function"));
10965 static int remote_hw_watchpoint_limit
= -1;
10966 static int remote_hw_watchpoint_length_limit
= -1;
10967 static int remote_hw_breakpoint_limit
= -1;
10970 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
10972 if (remote_hw_watchpoint_length_limit
== 0)
10974 else if (remote_hw_watchpoint_length_limit
< 0)
10976 else if (len
<= remote_hw_watchpoint_length_limit
)
10983 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
10985 if (type
== bp_hardware_breakpoint
)
10987 if (remote_hw_breakpoint_limit
== 0)
10989 else if (remote_hw_breakpoint_limit
< 0)
10991 else if (cnt
<= remote_hw_breakpoint_limit
)
10996 if (remote_hw_watchpoint_limit
== 0)
10998 else if (remote_hw_watchpoint_limit
< 0)
11002 else if (cnt
<= remote_hw_watchpoint_limit
)
11008 /* The to_stopped_by_sw_breakpoint method of target remote. */
11011 remote_target::stopped_by_sw_breakpoint ()
11013 struct thread_info
*thread
= inferior_thread ();
11015 return (thread
->priv
!= NULL
11016 && (get_remote_thread_info (thread
)->stop_reason
11017 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
11020 /* The to_supports_stopped_by_sw_breakpoint method of target
11024 remote_target::supports_stopped_by_sw_breakpoint ()
11026 return (m_features
.packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
11029 /* The to_stopped_by_hw_breakpoint method of target remote. */
11032 remote_target::stopped_by_hw_breakpoint ()
11034 struct thread_info
*thread
= inferior_thread ();
11036 return (thread
->priv
!= NULL
11037 && (get_remote_thread_info (thread
)->stop_reason
11038 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
11041 /* The to_supports_stopped_by_hw_breakpoint method of target
11045 remote_target::supports_stopped_by_hw_breakpoint ()
11047 return (m_features
.packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
11051 remote_target::stopped_by_watchpoint ()
11053 struct thread_info
*thread
= inferior_thread ();
11055 return (thread
->priv
!= NULL
11056 && (get_remote_thread_info (thread
)->stop_reason
11057 == TARGET_STOPPED_BY_WATCHPOINT
));
11061 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
11063 struct thread_info
*thread
= inferior_thread ();
11065 if (thread
->priv
!= NULL
11066 && (get_remote_thread_info (thread
)->stop_reason
11067 == TARGET_STOPPED_BY_WATCHPOINT
))
11069 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
11078 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
11079 struct bp_target_info
*bp_tgt
)
11081 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
11082 struct remote_state
*rs
;
11086 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11089 /* Make sure the remote is pointing at the right process, if
11091 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11092 set_general_process ();
11094 rs
= get_remote_state ();
11095 p
= rs
->buf
.data ();
11096 endbuf
= p
+ get_remote_packet_size ();
11102 addr
= remote_address_masked (addr
);
11103 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11104 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11106 if (supports_evaluation_of_breakpoint_conditions ())
11107 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
11109 if (can_run_breakpoint_commands ())
11110 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
11115 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11118 if (rs
->buf
[1] == '.')
11120 message
= strchr (&rs
->buf
[2], '.');
11122 error (_("Remote failure reply: %s"), message
+ 1);
11125 case PACKET_UNKNOWN
:
11130 internal_error (_("remote_insert_hw_breakpoint: reached end of function"));
11135 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
11136 struct bp_target_info
*bp_tgt
)
11139 struct remote_state
*rs
= get_remote_state ();
11140 char *p
= rs
->buf
.data ();
11141 char *endbuf
= p
+ get_remote_packet_size ();
11143 if (m_features
.packet_support (PACKET_Z1
) == PACKET_DISABLE
)
11146 /* Make sure the remote is pointing at the right process, if
11148 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
11149 set_general_process ();
11155 addr
= remote_address_masked (bp_tgt
->placed_address
);
11156 p
+= hexnumstr (p
, (ULONGEST
) addr
);
11157 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
11162 switch (m_features
.packet_ok (rs
->buf
, PACKET_Z1
))
11165 case PACKET_UNKNOWN
:
11170 internal_error (_("remote_remove_hw_breakpoint: reached end of function"));
11173 /* Verify memory using the "qCRC:" request. */
11176 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
11178 struct remote_state
*rs
= get_remote_state ();
11179 unsigned long host_crc
, target_crc
;
11182 /* It doesn't make sense to use qCRC if the remote target is
11183 connected but not running. */
11184 if (target_has_execution ()
11185 && m_features
.packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
11187 enum packet_result result
;
11189 /* Make sure the remote is pointing at the right process. */
11190 set_general_process ();
11192 /* FIXME: assumes lma can fit into long. */
11193 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
11194 (long) lma
, (long) size
);
11197 /* Be clever; compute the host_crc before waiting for target
11199 host_crc
= xcrc32 (data
, size
, 0xffffffff);
11203 result
= m_features
.packet_ok (rs
->buf
, PACKET_qCRC
);
11204 if (result
== PACKET_ERROR
)
11206 else if (result
== PACKET_OK
)
11208 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11209 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11211 return (host_crc
== target_crc
);
11215 return simple_verify_memory (this, data
, lma
, size
);
11218 /* compare-sections command
11220 With no arguments, compares each loadable section in the exec bfd
11221 with the same memory range on the target, and reports mismatches.
11222 Useful for verifying the image on the target against the exec file. */
11225 compare_sections_command (const char *args
, int from_tty
)
11228 const char *sectname
;
11229 bfd_size_type size
;
11232 int mismatched
= 0;
11236 if (!current_program_space
->exec_bfd ())
11237 error (_("command cannot be used without an exec file"));
11239 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11245 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11247 if (!(s
->flags
& SEC_LOAD
))
11248 continue; /* Skip non-loadable section. */
11250 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11251 continue; /* Skip writeable sections */
11253 size
= bfd_section_size (s
);
11255 continue; /* Skip zero-length section. */
11257 sectname
= bfd_section_name (s
);
11258 if (args
&& strcmp (args
, sectname
) != 0)
11259 continue; /* Not the section selected by user. */
11261 matched
= 1; /* Do this section. */
11264 gdb::byte_vector
sectdata (size
);
11265 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11266 sectdata
.data (), 0, size
);
11268 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11271 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11272 paddress (current_inferior ()->arch (), lma
),
11273 paddress (current_inferior ()->arch (), lma
+ size
));
11275 gdb_printf ("Section %s, range %s -- %s: ", sectname
,
11276 paddress (current_inferior ()->arch (), lma
),
11277 paddress (current_inferior ()->arch (), lma
+ size
));
11279 gdb_printf ("matched.\n");
11282 gdb_printf ("MIS-MATCHED!\n");
11286 if (mismatched
> 0)
11287 warning (_("One or more sections of the target image does "
11288 "not match the loaded file"));
11289 if (args
&& !matched
)
11290 gdb_printf (_("No loaded section named '%s'.\n"), args
);
11293 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11294 into remote target. The number of bytes written to the remote
11295 target is returned, or -1 for error. */
11298 remote_target::remote_write_qxfer (const char *object_name
,
11299 const char *annex
, const gdb_byte
*writebuf
,
11300 ULONGEST offset
, LONGEST len
,
11301 ULONGEST
*xfered_len
,
11302 const unsigned int which_packet
)
11306 struct remote_state
*rs
= get_remote_state ();
11307 int max_size
= get_memory_write_packet_size ();
11309 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11310 return TARGET_XFER_E_IO
;
11312 /* Insert header. */
11313 i
= snprintf (rs
->buf
.data (), max_size
,
11314 "qXfer:%s:write:%s:%s:",
11315 object_name
, annex
? annex
: "",
11316 phex_nz (offset
, sizeof offset
));
11317 max_size
-= (i
+ 1);
11319 /* Escape as much data as fits into rs->buf. */
11320 buf_len
= remote_escape_output
11321 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11323 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11324 || getpkt (&rs
->buf
) < 0
11325 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11326 return TARGET_XFER_E_IO
;
11328 unpack_varlen_hex (rs
->buf
.data (), &n
);
11331 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11334 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11335 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11336 number of bytes read is returned, or 0 for EOF, or -1 for error.
11337 The number of bytes read may be less than LEN without indicating an
11338 EOF. PACKET is checked and updated to indicate whether the remote
11339 target supports this object. */
11342 remote_target::remote_read_qxfer (const char *object_name
,
11344 gdb_byte
*readbuf
, ULONGEST offset
,
11346 ULONGEST
*xfered_len
,
11347 const unsigned int which_packet
)
11349 struct remote_state
*rs
= get_remote_state ();
11350 LONGEST i
, n
, packet_len
;
11352 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
11353 return TARGET_XFER_E_IO
;
11355 /* Check whether we've cached an end-of-object packet that matches
11357 if (rs
->finished_object
)
11359 if (strcmp (object_name
, rs
->finished_object
) == 0
11360 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11361 && offset
== rs
->finished_offset
)
11362 return TARGET_XFER_EOF
;
11365 /* Otherwise, we're now reading something different. Discard
11367 xfree (rs
->finished_object
);
11368 xfree (rs
->finished_annex
);
11369 rs
->finished_object
= NULL
;
11370 rs
->finished_annex
= NULL
;
11373 /* Request only enough to fit in a single packet. The actual data
11374 may not, since we don't know how much of it will need to be escaped;
11375 the target is free to respond with slightly less data. We subtract
11376 five to account for the response type and the protocol frame. */
11377 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11378 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11379 "qXfer:%s:read:%s:%s,%s",
11380 object_name
, annex
? annex
: "",
11381 phex_nz (offset
, sizeof offset
),
11382 phex_nz (n
, sizeof n
));
11383 i
= putpkt (rs
->buf
);
11385 return TARGET_XFER_E_IO
;
11388 packet_len
= getpkt (&rs
->buf
);
11390 || m_features
.packet_ok (rs
->buf
, which_packet
) != PACKET_OK
)
11391 return TARGET_XFER_E_IO
;
11393 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11394 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11396 /* 'm' means there is (or at least might be) more data after this
11397 batch. That does not make sense unless there's at least one byte
11398 of data in this reply. */
11399 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11400 error (_("Remote qXfer reply contained no data."));
11402 /* Got some data. */
11403 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11404 packet_len
- 1, readbuf
, n
);
11406 /* 'l' is an EOF marker, possibly including a final block of data,
11407 or possibly empty. If we have the final block of a non-empty
11408 object, record this fact to bypass a subsequent partial read. */
11409 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11411 rs
->finished_object
= xstrdup (object_name
);
11412 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11413 rs
->finished_offset
= offset
+ i
;
11417 return TARGET_XFER_EOF
;
11421 return TARGET_XFER_OK
;
11425 enum target_xfer_status
11426 remote_target::xfer_partial (enum target_object object
,
11427 const char *annex
, gdb_byte
*readbuf
,
11428 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11429 ULONGEST
*xfered_len
)
11431 struct remote_state
*rs
;
11436 = gdbarch_addressable_memory_unit_size (current_inferior ()->arch ());
11438 set_remote_traceframe ();
11439 set_general_thread (inferior_ptid
);
11441 rs
= get_remote_state ();
11443 /* Handle memory using the standard memory routines. */
11444 if (object
== TARGET_OBJECT_MEMORY
)
11446 /* If the remote target is connected but not running, we should
11447 pass this request down to a lower stratum (e.g. the executable
11449 if (!target_has_execution ())
11450 return TARGET_XFER_EOF
;
11452 if (writebuf
!= NULL
)
11453 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11456 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11460 /* Handle extra signal info using qxfer packets. */
11461 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11464 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11465 xfered_len
, PACKET_qXfer_siginfo_read
);
11467 return remote_write_qxfer ("siginfo", annex
, writebuf
, offset
, len
,
11468 xfered_len
, PACKET_qXfer_siginfo_write
);
11471 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11474 return remote_read_qxfer ("statictrace", annex
,
11475 readbuf
, offset
, len
, xfered_len
,
11476 PACKET_qXfer_statictrace_read
);
11478 return TARGET_XFER_E_IO
;
11481 /* Only handle flash writes. */
11482 if (writebuf
!= NULL
)
11486 case TARGET_OBJECT_FLASH
:
11487 return remote_flash_write (offset
, len
, xfered_len
,
11491 return TARGET_XFER_E_IO
;
11495 /* Map pre-existing objects onto letters. DO NOT do this for new
11496 objects!!! Instead specify new query packets. */
11499 case TARGET_OBJECT_AVR
:
11503 case TARGET_OBJECT_AUXV
:
11504 gdb_assert (annex
== NULL
);
11505 return remote_read_qxfer
11506 ("auxv", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_auxv
);
11508 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11509 return remote_read_qxfer
11510 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11511 PACKET_qXfer_features
);
11513 case TARGET_OBJECT_LIBRARIES
:
11514 return remote_read_qxfer
11515 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11516 PACKET_qXfer_libraries
);
11518 case TARGET_OBJECT_LIBRARIES_SVR4
:
11519 return remote_read_qxfer
11520 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11521 PACKET_qXfer_libraries_svr4
);
11523 case TARGET_OBJECT_MEMORY_MAP
:
11524 gdb_assert (annex
== NULL
);
11525 return remote_read_qxfer
11526 ("memory-map", annex
, readbuf
, offset
, len
, xfered_len
,
11527 PACKET_qXfer_memory_map
);
11529 case TARGET_OBJECT_OSDATA
:
11530 /* Should only get here if we're connected. */
11531 gdb_assert (rs
->remote_desc
);
11532 return remote_read_qxfer
11533 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11534 PACKET_qXfer_osdata
);
11536 case TARGET_OBJECT_THREADS
:
11537 gdb_assert (annex
== NULL
);
11538 return remote_read_qxfer
11539 ("threads", annex
, readbuf
, offset
, len
, xfered_len
,
11540 PACKET_qXfer_threads
);
11542 case TARGET_OBJECT_TRACEFRAME_INFO
:
11543 gdb_assert (annex
== NULL
);
11544 return remote_read_qxfer
11545 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11546 PACKET_qXfer_traceframe_info
);
11548 case TARGET_OBJECT_FDPIC
:
11549 return remote_read_qxfer
11550 ("fdpic", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_fdpic
);
11552 case TARGET_OBJECT_OPENVMS_UIB
:
11553 return remote_read_qxfer
11554 ("uib", annex
, readbuf
, offset
, len
, xfered_len
, PACKET_qXfer_uib
);
11556 case TARGET_OBJECT_BTRACE
:
11557 return remote_read_qxfer
11558 ("btrace", annex
, readbuf
, offset
, len
, xfered_len
,
11559 PACKET_qXfer_btrace
);
11561 case TARGET_OBJECT_BTRACE_CONF
:
11562 return remote_read_qxfer
11563 ("btrace-conf", annex
, readbuf
, offset
, len
, xfered_len
,
11564 PACKET_qXfer_btrace_conf
);
11566 case TARGET_OBJECT_EXEC_FILE
:
11567 return remote_read_qxfer
11568 ("exec-file", annex
, readbuf
, offset
, len
, xfered_len
,
11569 PACKET_qXfer_exec_file
);
11572 return TARGET_XFER_E_IO
;
11575 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11576 large enough let the caller deal with it. */
11577 if (len
< get_remote_packet_size ())
11578 return TARGET_XFER_E_IO
;
11579 len
= get_remote_packet_size ();
11581 /* Except for querying the minimum buffer size, target must be open. */
11582 if (!rs
->remote_desc
)
11583 error (_("remote query is only available after target open"));
11585 gdb_assert (annex
!= NULL
);
11586 gdb_assert (readbuf
!= NULL
);
11588 p2
= rs
->buf
.data ();
11590 *p2
++ = query_type
;
11592 /* We used one buffer char for the remote protocol q command and
11593 another for the query type. As the remote protocol encapsulation
11594 uses 4 chars plus one extra in case we are debugging
11595 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11598 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11600 /* Bad caller may have sent forbidden characters. */
11601 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11606 gdb_assert (annex
[i
] == '\0');
11608 i
= putpkt (rs
->buf
);
11610 return TARGET_XFER_E_IO
;
11613 strcpy ((char *) readbuf
, rs
->buf
.data ());
11615 *xfered_len
= strlen ((char *) readbuf
);
11616 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11619 /* Implementation of to_get_memory_xfer_limit. */
11622 remote_target::get_memory_xfer_limit ()
11624 return get_memory_write_packet_size ();
11628 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11629 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11630 CORE_ADDR
*found_addrp
)
11632 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
11633 struct remote_state
*rs
= get_remote_state ();
11634 int max_size
= get_memory_write_packet_size ();
11636 /* Number of packet bytes used to encode the pattern;
11637 this could be more than PATTERN_LEN due to escape characters. */
11638 int escaped_pattern_len
;
11639 /* Amount of pattern that was encodable in the packet. */
11640 int used_pattern_len
;
11643 ULONGEST found_addr
;
11645 auto read_memory
= [this] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11647 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11651 /* Don't go to the target if we don't have to. This is done before
11652 checking packet_support to avoid the possibility that a success for this
11653 edge case means the facility works in general. */
11654 if (pattern_len
> search_space_len
)
11656 if (pattern_len
== 0)
11658 *found_addrp
= start_addr
;
11662 /* If we already know the packet isn't supported, fall back to the simple
11663 way of searching memory. */
11665 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11667 /* Target doesn't provided special support, fall back and use the
11668 standard support (copy memory and do the search here). */
11669 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11670 pattern
, pattern_len
, found_addrp
);
11673 /* Make sure the remote is pointing at the right process. */
11674 set_general_process ();
11676 /* Insert header. */
11677 i
= snprintf (rs
->buf
.data (), max_size
,
11678 "qSearch:memory:%s;%s;",
11679 phex_nz (start_addr
, addr_size
),
11680 phex_nz (search_space_len
, sizeof (search_space_len
)));
11681 max_size
-= (i
+ 1);
11683 /* Escape as much data as fits into rs->buf. */
11684 escaped_pattern_len
=
11685 remote_escape_output (pattern
, pattern_len
, 1,
11686 (gdb_byte
*) rs
->buf
.data () + i
,
11687 &used_pattern_len
, max_size
);
11689 /* Bail if the pattern is too large. */
11690 if (used_pattern_len
!= pattern_len
)
11691 error (_("Pattern is too large to transmit to remote target."));
11693 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
11694 || getpkt (&rs
->buf
) < 0
11695 || m_features
.packet_ok (rs
->buf
, PACKET_qSearch_memory
) != PACKET_OK
)
11697 /* The request may not have worked because the command is not
11698 supported. If so, fall back to the simple way. */
11699 if (m_features
.packet_support (PACKET_qSearch_memory
) == PACKET_DISABLE
)
11701 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11702 pattern
, pattern_len
, found_addrp
);
11707 if (rs
->buf
[0] == '0')
11709 else if (rs
->buf
[0] == '1')
11712 if (rs
->buf
[1] != ',')
11713 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11714 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
11715 *found_addrp
= found_addr
;
11718 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11724 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
11726 struct remote_state
*rs
= get_remote_state ();
11727 char *p
= rs
->buf
.data ();
11729 if (!rs
->remote_desc
)
11730 error (_("remote rcmd is only available after target open"));
11732 /* Send a NULL command across as an empty command. */
11733 if (command
== NULL
)
11736 /* The query prefix. */
11737 strcpy (rs
->buf
.data (), "qRcmd,");
11738 p
= strchr (rs
->buf
.data (), '\0');
11740 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
11741 > get_remote_packet_size ())
11742 error (_("\"monitor\" command ``%s'' is too long."), command
);
11744 /* Encode the actual command. */
11745 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
11747 if (putpkt (rs
->buf
) < 0)
11748 error (_("Communication problem with target."));
11750 /* get/display the response */
11755 /* XXX - see also remote_get_noisy_reply(). */
11756 QUIT
; /* Allow user to bail out with ^C. */
11758 if (getpkt (&rs
->buf
) == -1)
11760 /* Timeout. Continue to (try to) read responses.
11761 This is better than stopping with an error, assuming the stub
11762 is still executing the (long) monitor command.
11763 If needed, the user can interrupt gdb using C-c, obtaining
11764 an effect similar to stop on timeout. */
11767 buf
= rs
->buf
.data ();
11768 if (buf
[0] == '\0')
11769 error (_("Target does not support this command."));
11770 if (buf
[0] == 'O' && buf
[1] != 'K')
11772 remote_console_output (buf
+ 1); /* 'O' message from stub. */
11775 if (strcmp (buf
, "OK") == 0)
11777 if (strlen (buf
) == 3 && buf
[0] == 'E'
11778 && isxdigit (buf
[1]) && isxdigit (buf
[2]))
11780 error (_("Protocol error with Rcmd"));
11782 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
11784 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
11786 gdb_putc (c
, outbuf
);
11792 std::vector
<mem_region
>
11793 remote_target::memory_map ()
11795 std::vector
<mem_region
> result
;
11796 gdb::optional
<gdb::char_vector
> text
11797 = target_read_stralloc (current_inferior ()->top_target (),
11798 TARGET_OBJECT_MEMORY_MAP
, NULL
);
11801 result
= parse_memory_map (text
->data ());
11806 /* Set of callbacks used to implement the 'maint packet' command. */
11808 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
11810 /* Called before the packet is sent. BUF is the packet content before
11811 the protocol specific prefix, suffix, and escaping is added. */
11813 void sending (gdb::array_view
<const char> &buf
) override
11815 gdb_puts ("sending: ");
11816 print_packet (buf
);
11820 /* Called with BUF, the reply from the remote target. */
11822 void received (gdb::array_view
<const char> &buf
) override
11824 gdb_puts ("received: \"");
11825 print_packet (buf
);
11831 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
11832 '\x??' with '??' replaced by the hexadecimal value of the byte. */
11835 print_packet (gdb::array_view
<const char> &buf
)
11839 for (int i
= 0; i
< buf
.size (); ++i
)
11841 gdb_byte c
= buf
[i
];
11843 gdb_putc (c
, &stb
);
11845 gdb_printf (&stb
, "\\x%02x", (unsigned char) c
);
11848 gdb_puts (stb
.string ().c_str ());
11852 /* See remote.h. */
11855 send_remote_packet (gdb::array_view
<const char> &buf
,
11856 send_remote_packet_callbacks
*callbacks
)
11858 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
11859 error (_("a remote packet must not be empty"));
11861 remote_target
*remote
= get_current_remote_target ();
11862 if (remote
== nullptr)
11863 error (_("packets can only be sent to a remote target"));
11865 callbacks
->sending (buf
);
11867 remote
->putpkt_binary (buf
.data (), buf
.size ());
11868 remote_state
*rs
= remote
->get_remote_state ();
11869 int bytes
= remote
->getpkt (&rs
->buf
);
11872 error (_("error while fetching packet from remote target"));
11874 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
11875 callbacks
->received (view
);
11878 /* Entry point for the 'maint packet' command. */
11881 cli_packet_command (const char *args
, int from_tty
)
11883 cli_packet_command_callbacks cb
;
11884 gdb::array_view
<const char> view
11885 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
11886 send_remote_packet (view
, &cb
);
11890 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
11892 static void display_thread_info (struct gdb_ext_thread_info
*info
);
11894 static void threadset_test_cmd (char *cmd
, int tty
);
11896 static void threadalive_test (char *cmd
, int tty
);
11898 static void threadlist_test_cmd (char *cmd
, int tty
);
11900 int get_and_display_threadinfo (threadref
*ref
);
11902 static void threadinfo_test_cmd (char *cmd
, int tty
);
11904 static int thread_display_step (threadref
*ref
, void *context
);
11906 static void threadlist_update_test_cmd (char *cmd
, int tty
);
11908 static void init_remote_threadtests (void);
11910 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
11913 threadset_test_cmd (const char *cmd
, int tty
)
11915 int sample_thread
= SAMPLE_THREAD
;
11917 gdb_printf (_("Remote threadset test\n"));
11918 set_general_thread (sample_thread
);
11923 threadalive_test (const char *cmd
, int tty
)
11925 int sample_thread
= SAMPLE_THREAD
;
11926 int pid
= inferior_ptid
.pid ();
11927 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
11929 if (remote_thread_alive (ptid
))
11930 gdb_printf ("PASS: Thread alive test\n");
11932 gdb_printf ("FAIL: Thread alive test\n");
11935 void output_threadid (char *title
, threadref
*ref
);
11938 output_threadid (char *title
, threadref
*ref
)
11942 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
11944 gdb_printf ("%s %s\n", title
, (&hexid
[0]));
11948 threadlist_test_cmd (const char *cmd
, int tty
)
11951 threadref nextthread
;
11952 int done
, result_count
;
11953 threadref threadlist
[3];
11955 gdb_printf ("Remote Threadlist test\n");
11956 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
11957 &result_count
, &threadlist
[0]))
11958 gdb_printf ("FAIL: threadlist test\n");
11961 threadref
*scan
= threadlist
;
11962 threadref
*limit
= scan
+ result_count
;
11964 while (scan
< limit
)
11965 output_threadid (" thread ", scan
++);
11970 display_thread_info (struct gdb_ext_thread_info
*info
)
11972 output_threadid ("Threadid: ", &info
->threadid
);
11973 gdb_printf ("Name: %s\n ", info
->shortname
);
11974 gdb_printf ("State: %s\n", info
->display
);
11975 gdb_printf ("other: %s\n\n", info
->more_display
);
11979 get_and_display_threadinfo (threadref
*ref
)
11983 struct gdb_ext_thread_info threadinfo
;
11985 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
11986 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
11987 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
11988 display_thread_info (&threadinfo
);
11993 threadinfo_test_cmd (const char *cmd
, int tty
)
11995 int athread
= SAMPLE_THREAD
;
11999 int_to_threadref (&thread
, athread
);
12000 gdb_printf ("Remote Threadinfo test\n");
12001 if (!get_and_display_threadinfo (&thread
))
12002 gdb_printf ("FAIL cannot get thread info\n");
12006 thread_display_step (threadref
*ref
, void *context
)
12008 /* output_threadid(" threadstep ",ref); *//* simple test */
12009 return get_and_display_threadinfo (ref
);
12013 threadlist_update_test_cmd (const char *cmd
, int tty
)
12015 gdb_printf ("Remote Threadlist update test\n");
12016 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
12020 init_remote_threadtests (void)
12022 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
12023 _("Fetch and print the remote list of "
12024 "thread identifiers, one pkt only."));
12025 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
12026 _("Fetch and display info about one thread."));
12027 add_com ("tset", class_obscure
, threadset_test_cmd
,
12028 _("Test setting to a different thread."));
12029 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
12030 _("Iterate through updating all remote thread info."));
12031 add_com ("talive", class_obscure
, threadalive_test
,
12032 _("Remote thread alive test."));
12037 /* Convert a thread ID to a string. */
12040 remote_target::pid_to_str (ptid_t ptid
)
12042 if (ptid
== null_ptid
)
12043 return normal_pid_to_str (ptid
);
12044 else if (ptid
.is_pid ())
12046 /* Printing an inferior target id. */
12048 /* When multi-process extensions are off, there's no way in the
12049 remote protocol to know the remote process id, if there's any
12050 at all. There's one exception --- when we're connected with
12051 target extended-remote, and we manually attached to a process
12052 with "attach PID". We don't record anywhere a flag that
12053 allows us to distinguish that case from the case of
12054 connecting with extended-remote and the stub already being
12055 attached to a process, and reporting yes to qAttached, hence
12056 no smart special casing here. */
12057 if (!m_features
.remote_multi_process_p ())
12058 return "Remote target";
12060 return normal_pid_to_str (ptid
);
12064 if (magic_null_ptid
== ptid
)
12065 return "Thread <main>";
12066 else if (m_features
.remote_multi_process_p ())
12067 if (ptid
.lwp () == 0)
12068 return normal_pid_to_str (ptid
);
12070 return string_printf ("Thread %d.%ld",
12071 ptid
.pid (), ptid
.lwp ());
12073 return string_printf ("Thread %ld", ptid
.lwp ());
12077 /* Get the address of the thread local variable in OBJFILE which is
12078 stored at OFFSET within the thread local storage for thread PTID. */
12081 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
12084 if (m_features
.packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
12086 struct remote_state
*rs
= get_remote_state ();
12087 char *p
= rs
->buf
.data ();
12088 char *endp
= p
+ get_remote_packet_size ();
12089 enum packet_result result
;
12091 strcpy (p
, "qGetTLSAddr:");
12093 p
= write_ptid (p
, endp
, ptid
);
12095 p
+= hexnumstr (p
, offset
);
12097 p
+= hexnumstr (p
, lm
);
12102 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTLSAddr
);
12103 if (result
== PACKET_OK
)
12107 unpack_varlen_hex (rs
->buf
.data (), &addr
);
12110 else if (result
== PACKET_UNKNOWN
)
12111 throw_error (TLS_GENERIC_ERROR
,
12112 _("Remote target doesn't support qGetTLSAddr packet"));
12114 throw_error (TLS_GENERIC_ERROR
,
12115 _("Remote target failed to process qGetTLSAddr request"));
12118 throw_error (TLS_GENERIC_ERROR
,
12119 _("TLS not supported or disabled on this target"));
12124 /* Provide thread local base, i.e. Thread Information Block address.
12125 Returns 1 if ptid is found and thread_local_base is non zero. */
12128 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
12130 if (m_features
.packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
12132 struct remote_state
*rs
= get_remote_state ();
12133 char *p
= rs
->buf
.data ();
12134 char *endp
= p
+ get_remote_packet_size ();
12135 enum packet_result result
;
12137 strcpy (p
, "qGetTIBAddr:");
12139 p
= write_ptid (p
, endp
, ptid
);
12144 result
= m_features
.packet_ok (rs
->buf
, PACKET_qGetTIBAddr
);
12145 if (result
== PACKET_OK
)
12148 unpack_varlen_hex (rs
->buf
.data (), &val
);
12150 *addr
= (CORE_ADDR
) val
;
12153 else if (result
== PACKET_UNKNOWN
)
12154 error (_("Remote target doesn't support qGetTIBAddr packet"));
12156 error (_("Remote target failed to process qGetTIBAddr request"));
12159 error (_("qGetTIBAddr not supported or disabled on this target"));
12164 /* Support for inferring a target description based on the current
12165 architecture and the size of a 'g' packet. While the 'g' packet
12166 can have any size (since optional registers can be left off the
12167 end), some sizes are easily recognizable given knowledge of the
12168 approximate architecture. */
12170 struct remote_g_packet_guess
12172 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
12179 const struct target_desc
*tdesc
;
12182 struct remote_g_packet_data
12184 std::vector
<remote_g_packet_guess
> guesses
;
12187 static const registry
<gdbarch
>::key
<struct remote_g_packet_data
>
12188 remote_g_packet_data_handle
;
12190 static struct remote_g_packet_data
*
12191 get_g_packet_data (struct gdbarch
*gdbarch
)
12193 struct remote_g_packet_data
*data
12194 = remote_g_packet_data_handle
.get (gdbarch
);
12195 if (data
== nullptr)
12196 data
= remote_g_packet_data_handle
.emplace (gdbarch
);
12201 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12202 const struct target_desc
*tdesc
)
12204 struct remote_g_packet_data
*data
= get_g_packet_data (gdbarch
);
12206 gdb_assert (tdesc
!= NULL
);
12208 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12209 if (guess
.bytes
== bytes
)
12210 internal_error (_("Duplicate g packet description added for size %d"),
12213 data
->guesses
.emplace_back (bytes
, tdesc
);
12216 /* Return true if remote_read_description would do anything on this target
12217 and architecture, false otherwise. */
12220 remote_read_description_p (struct target_ops
*target
)
12222 remote_g_packet_data
*data
= get_g_packet_data (current_inferior ()->arch ());
12224 return !data
->guesses
.empty ();
12227 const struct target_desc
*
12228 remote_target::read_description ()
12230 remote_g_packet_data
*data
= get_g_packet_data (current_inferior ()->arch ());
12232 /* Do not try this during initial connection, when we do not know
12233 whether there is a running but stopped thread. */
12234 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12235 return beneath ()->read_description ();
12237 if (!data
->guesses
.empty ())
12239 int bytes
= send_g_packet ();
12241 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12242 if (guess
.bytes
== bytes
)
12243 return guess
.tdesc
;
12245 /* We discard the g packet. A minor optimization would be to
12246 hold on to it, and fill the register cache once we have selected
12247 an architecture, but it's too tricky to do safely. */
12250 return beneath ()->read_description ();
12253 /* Remote file transfer support. This is host-initiated I/O, not
12254 target-initiated; for target-initiated, see remote-fileio.c. */
12256 /* If *LEFT is at least the length of STRING, copy STRING to
12257 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12258 decrease *LEFT. Otherwise raise an error. */
12261 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12263 int len
= strlen (string
);
12266 error (_("Packet too long for target."));
12268 memcpy (*buffer
, string
, len
);
12272 /* NUL-terminate the buffer as a convenience, if there is
12278 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12279 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12280 decrease *LEFT. Otherwise raise an error. */
12283 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12286 if (2 * len
> *left
)
12287 error (_("Packet too long for target."));
12289 bin2hex (bytes
, *buffer
, len
);
12290 *buffer
+= 2 * len
;
12293 /* NUL-terminate the buffer as a convenience, if there is
12299 /* If *LEFT is large enough, convert VALUE to hex and add it to
12300 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12301 decrease *LEFT. Otherwise raise an error. */
12304 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12306 int len
= hexnumlen (value
);
12309 error (_("Packet too long for target."));
12311 hexnumstr (*buffer
, value
);
12315 /* NUL-terminate the buffer as a convenience, if there is
12321 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12322 value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
12323 was included, and *ATTACHMENT to point to the start of the annex
12324 if any. The length of the packet isn't needed here; there may
12325 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12327 Return 0 if the packet could be parsed, -1 if it could not. If
12328 -1 is returned, the other variables may not be initialized. */
12331 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12332 fileio_error
*remote_errno
, const char **attachment
)
12336 *remote_errno
= FILEIO_SUCCESS
;
12337 *attachment
= NULL
;
12339 if (buffer
[0] != 'F')
12343 *retcode
= strtol (&buffer
[1], &p
, 16);
12344 if (errno
!= 0 || p
== &buffer
[1])
12347 /* Check for ",errno". */
12351 *remote_errno
= (fileio_error
) strtol (p
+ 1, &p2
, 16);
12352 if (errno
!= 0 || p
+ 1 == p2
)
12357 /* Check for ";attachment". If there is no attachment, the
12358 packet should end here. */
12361 *attachment
= p
+ 1;
12364 else if (*p
== '\0')
12370 /* Send a prepared I/O packet to the target and read its response.
12371 The prepared packet is in the global RS->BUF before this function
12372 is called, and the answer is there when we return.
12374 COMMAND_BYTES is the length of the request to send, which may include
12375 binary data. WHICH_PACKET is the packet configuration to check
12376 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12377 is set to the error number and -1 is returned. Otherwise the value
12378 returned by the function is returned.
12380 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12381 attachment is expected; an error will be reported if there's a
12382 mismatch. If one is found, *ATTACHMENT will be set to point into
12383 the packet buffer and *ATTACHMENT_LEN will be set to the
12384 attachment's length. */
12387 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12388 fileio_error
*remote_errno
, const char **attachment
,
12389 int *attachment_len
)
12391 struct remote_state
*rs
= get_remote_state ();
12392 int ret
, bytes_read
;
12393 const char *attachment_tmp
;
12395 if (m_features
.packet_support (which_packet
) == PACKET_DISABLE
)
12397 *remote_errno
= FILEIO_ENOSYS
;
12401 putpkt_binary (rs
->buf
.data (), command_bytes
);
12402 bytes_read
= getpkt (&rs
->buf
);
12404 /* If it timed out, something is wrong. Don't try to parse the
12406 if (bytes_read
< 0)
12408 *remote_errno
= FILEIO_EINVAL
;
12412 switch (m_features
.packet_ok (rs
->buf
, which_packet
))
12415 *remote_errno
= FILEIO_EINVAL
;
12417 case PACKET_UNKNOWN
:
12418 *remote_errno
= FILEIO_ENOSYS
;
12424 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12427 *remote_errno
= FILEIO_EINVAL
;
12431 /* Make sure we saw an attachment if and only if we expected one. */
12432 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12433 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12435 *remote_errno
= FILEIO_EINVAL
;
12439 /* If an attachment was found, it must point into the packet buffer;
12440 work out how many bytes there were. */
12441 if (attachment_tmp
!= NULL
)
12443 *attachment
= attachment_tmp
;
12444 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12450 /* See declaration.h. */
12453 readahead_cache::invalidate ()
12458 /* See declaration.h. */
12461 readahead_cache::invalidate_fd (int fd
)
12463 if (this->fd
== fd
)
12467 /* Set the filesystem remote_hostio functions that take FILENAME
12468 arguments will use. Return 0 on success, or -1 if an error
12469 occurs (and set *REMOTE_ERRNO). */
12472 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12473 fileio_error
*remote_errno
)
12475 struct remote_state
*rs
= get_remote_state ();
12476 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12477 char *p
= rs
->buf
.data ();
12478 int left
= get_remote_packet_size () - 1;
12482 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12485 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12488 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12490 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12491 remote_buffer_add_string (&p
, &left
, arg
);
12493 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12494 remote_errno
, NULL
, NULL
);
12496 if (m_features
.packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12500 rs
->fs_pid
= required_pid
;
12505 /* Implementation of to_fileio_open. */
12508 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12509 int flags
, int mode
, int warn_if_slow
,
12510 fileio_error
*remote_errno
)
12512 struct remote_state
*rs
= get_remote_state ();
12513 char *p
= rs
->buf
.data ();
12514 int left
= get_remote_packet_size () - 1;
12518 static int warning_issued
= 0;
12520 gdb_printf (_("Reading %s from remote target...\n"),
12523 if (!warning_issued
)
12525 warning (_("File transfers from remote targets can be slow."
12526 " Use \"set sysroot\" to access files locally"
12528 warning_issued
= 1;
12532 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12535 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12537 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12538 strlen (filename
));
12539 remote_buffer_add_string (&p
, &left
, ",");
12541 remote_buffer_add_int (&p
, &left
, flags
);
12542 remote_buffer_add_string (&p
, &left
, ",");
12544 remote_buffer_add_int (&p
, &left
, mode
);
12546 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12547 remote_errno
, NULL
, NULL
);
12551 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12552 int flags
, int mode
, int warn_if_slow
,
12553 fileio_error
*remote_errno
)
12555 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12559 /* Implementation of to_fileio_pwrite. */
12562 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12563 ULONGEST offset
, fileio_error
*remote_errno
)
12565 struct remote_state
*rs
= get_remote_state ();
12566 char *p
= rs
->buf
.data ();
12567 int left
= get_remote_packet_size ();
12570 rs
->readahead_cache
.invalidate_fd (fd
);
12572 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12574 remote_buffer_add_int (&p
, &left
, fd
);
12575 remote_buffer_add_string (&p
, &left
, ",");
12577 remote_buffer_add_int (&p
, &left
, offset
);
12578 remote_buffer_add_string (&p
, &left
, ",");
12580 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12581 (get_remote_packet_size ()
12582 - (p
- rs
->buf
.data ())));
12584 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12585 remote_errno
, NULL
, NULL
);
12589 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12590 ULONGEST offset
, fileio_error
*remote_errno
)
12592 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12595 /* Helper for the implementation of to_fileio_pread. Read the file
12596 from the remote side with vFile:pread. */
12599 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12600 ULONGEST offset
, fileio_error
*remote_errno
)
12602 struct remote_state
*rs
= get_remote_state ();
12603 char *p
= rs
->buf
.data ();
12604 const char *attachment
;
12605 int left
= get_remote_packet_size ();
12606 int ret
, attachment_len
;
12609 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12611 remote_buffer_add_int (&p
, &left
, fd
);
12612 remote_buffer_add_string (&p
, &left
, ",");
12614 remote_buffer_add_int (&p
, &left
, len
);
12615 remote_buffer_add_string (&p
, &left
, ",");
12617 remote_buffer_add_int (&p
, &left
, offset
);
12619 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12620 remote_errno
, &attachment
,
12626 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12628 if (read_len
!= ret
)
12629 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12634 /* See declaration.h. */
12637 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12641 && this->offset
<= offset
12642 && offset
< this->offset
+ this->buf
.size ())
12644 ULONGEST max
= this->offset
+ this->buf
.size ();
12646 if (offset
+ len
> max
)
12647 len
= max
- offset
;
12649 memcpy (read_buf
, &this->buf
[offset
- this->offset
], len
);
12656 /* Implementation of to_fileio_pread. */
12659 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12660 ULONGEST offset
, fileio_error
*remote_errno
)
12663 struct remote_state
*rs
= get_remote_state ();
12664 readahead_cache
*cache
= &rs
->readahead_cache
;
12666 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
12669 cache
->hit_count
++;
12671 remote_debug_printf ("readahead cache hit %s",
12672 pulongest (cache
->hit_count
));
12676 cache
->miss_count
++;
12678 remote_debug_printf ("readahead cache miss %s",
12679 pulongest (cache
->miss_count
));
12682 cache
->offset
= offset
;
12683 cache
->buf
.resize (get_remote_packet_size ());
12685 ret
= remote_hostio_pread_vFile (cache
->fd
, &cache
->buf
[0],
12686 cache
->buf
.size (),
12687 cache
->offset
, remote_errno
);
12690 cache
->invalidate_fd (fd
);
12694 cache
->buf
.resize (ret
);
12695 return cache
->pread (fd
, read_buf
, len
, offset
);
12699 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12700 ULONGEST offset
, fileio_error
*remote_errno
)
12702 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
12705 /* Implementation of to_fileio_close. */
12708 remote_target::remote_hostio_close (int fd
, fileio_error
*remote_errno
)
12710 struct remote_state
*rs
= get_remote_state ();
12711 char *p
= rs
->buf
.data ();
12712 int left
= get_remote_packet_size () - 1;
12714 rs
->readahead_cache
.invalidate_fd (fd
);
12716 remote_buffer_add_string (&p
, &left
, "vFile:close:");
12718 remote_buffer_add_int (&p
, &left
, fd
);
12720 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
12721 remote_errno
, NULL
, NULL
);
12725 remote_target::fileio_close (int fd
, fileio_error
*remote_errno
)
12727 return remote_hostio_close (fd
, remote_errno
);
12730 /* Implementation of to_fileio_unlink. */
12733 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
12734 fileio_error
*remote_errno
)
12736 struct remote_state
*rs
= get_remote_state ();
12737 char *p
= rs
->buf
.data ();
12738 int left
= get_remote_packet_size () - 1;
12740 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12743 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
12745 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12746 strlen (filename
));
12748 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
12749 remote_errno
, NULL
, NULL
);
12753 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
12754 fileio_error
*remote_errno
)
12756 return remote_hostio_unlink (inf
, filename
, remote_errno
);
12759 /* Implementation of to_fileio_readlink. */
12761 gdb::optional
<std::string
>
12762 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
12763 fileio_error
*remote_errno
)
12765 struct remote_state
*rs
= get_remote_state ();
12766 char *p
= rs
->buf
.data ();
12767 const char *attachment
;
12768 int left
= get_remote_packet_size ();
12769 int len
, attachment_len
;
12772 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12775 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
12777 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12778 strlen (filename
));
12780 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
12781 remote_errno
, &attachment
,
12787 std::string
ret (len
, '\0');
12789 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12790 (gdb_byte
*) &ret
[0], len
);
12791 if (read_len
!= len
)
12792 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
12797 /* Implementation of to_fileio_fstat. */
12800 remote_target::fileio_fstat (int fd
, struct stat
*st
, fileio_error
*remote_errno
)
12802 struct remote_state
*rs
= get_remote_state ();
12803 char *p
= rs
->buf
.data ();
12804 int left
= get_remote_packet_size ();
12805 int attachment_len
, ret
;
12806 const char *attachment
;
12807 struct fio_stat fst
;
12810 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
12812 remote_buffer_add_int (&p
, &left
, fd
);
12814 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
12815 remote_errno
, &attachment
,
12819 if (*remote_errno
!= FILEIO_ENOSYS
)
12822 /* Strictly we should return -1, ENOSYS here, but when
12823 "set sysroot remote:" was implemented in August 2008
12824 BFD's need for a stat function was sidestepped with
12825 this hack. This was not remedied until March 2015
12826 so we retain the previous behavior to avoid breaking
12829 Note that the memset is a March 2015 addition; older
12830 GDBs set st_size *and nothing else* so the structure
12831 would have garbage in all other fields. This might
12832 break something but retaining the previous behavior
12833 here would be just too wrong. */
12835 memset (st
, 0, sizeof (struct stat
));
12836 st
->st_size
= INT_MAX
;
12840 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12841 (gdb_byte
*) &fst
, sizeof (fst
));
12843 if (read_len
!= ret
)
12844 error (_("vFile:fstat returned %d, but %d bytes."), ret
, read_len
);
12846 if (read_len
!= sizeof (fst
))
12847 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12848 read_len
, (int) sizeof (fst
));
12850 remote_fileio_to_host_stat (&fst
, st
);
12855 /* Implementation of to_filesystem_is_local. */
12858 remote_target::filesystem_is_local ()
12860 /* Valgrind GDB presents itself as a remote target but works
12861 on the local filesystem: it does not implement remote get
12862 and users are not expected to set a sysroot. To handle
12863 this case we treat the remote filesystem as local if the
12864 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12865 does not support vFile:open. */
12866 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
12868 packet_support ps
= m_features
.packet_support (PACKET_vFile_open
);
12870 if (ps
== PACKET_SUPPORT_UNKNOWN
)
12873 fileio_error remote_errno
;
12875 /* Try opening a file to probe support. The supplied
12876 filename is irrelevant, we only care about whether
12877 the stub recognizes the packet or not. */
12878 fd
= remote_hostio_open (NULL
, "just probing",
12879 FILEIO_O_RDONLY
, 0700, 0,
12883 remote_hostio_close (fd
, &remote_errno
);
12885 ps
= m_features
.packet_support (PACKET_vFile_open
);
12888 if (ps
== PACKET_DISABLE
)
12890 static int warning_issued
= 0;
12892 if (!warning_issued
)
12894 warning (_("remote target does not support file"
12895 " transfer, attempting to access files"
12896 " from local filesystem."));
12897 warning_issued
= 1;
12908 remote_hostio_error (fileio_error errnum
)
12910 int host_error
= fileio_error_to_host (errnum
);
12912 if (host_error
== -1)
12913 error (_("Unknown remote I/O error %d"), errnum
);
12915 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
12918 /* A RAII wrapper around a remote file descriptor. */
12920 class scoped_remote_fd
12923 scoped_remote_fd (remote_target
*remote
, int fd
)
12924 : m_remote (remote
), m_fd (fd
)
12928 ~scoped_remote_fd ()
12934 fileio_error remote_errno
;
12935 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
12939 /* Swallow exception before it escapes the dtor. If
12940 something goes wrong, likely the connection is gone,
12941 and there's nothing else that can be done. */
12946 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
12948 /* Release ownership of the file descriptor, and return it. */
12949 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
12956 /* Return the owned file descriptor. */
12957 int get () const noexcept
12963 /* The remote target. */
12964 remote_target
*m_remote
;
12966 /* The owned remote I/O file descriptor. */
12971 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
12973 remote_target
*remote
= get_current_remote_target ();
12975 if (remote
== nullptr)
12976 error (_("command can only be used with remote target"));
12978 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
12982 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
12985 int retcode
, bytes
, io_size
;
12986 fileio_error remote_errno
;
12987 int bytes_in_buffer
;
12991 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
12993 perror_with_name (local_file
);
12995 scoped_remote_fd fd
12996 (this, remote_hostio_open (NULL
,
12997 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
12999 0700, 0, &remote_errno
));
13000 if (fd
.get () == -1)
13001 remote_hostio_error (remote_errno
);
13003 /* Send up to this many bytes at once. They won't all fit in the
13004 remote packet limit, so we'll transfer slightly fewer. */
13005 io_size
= get_remote_packet_size ();
13006 gdb::byte_vector
buffer (io_size
);
13008 bytes_in_buffer
= 0;
13011 while (bytes_in_buffer
|| !saw_eof
)
13015 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
13016 io_size
- bytes_in_buffer
,
13020 if (ferror (file
.get ()))
13021 error (_("Error reading %s."), local_file
);
13024 /* EOF. Unless there is something still in the
13025 buffer from the last iteration, we are done. */
13027 if (bytes_in_buffer
== 0)
13035 bytes
+= bytes_in_buffer
;
13036 bytes_in_buffer
= 0;
13038 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
13039 offset
, &remote_errno
);
13042 remote_hostio_error (remote_errno
);
13043 else if (retcode
== 0)
13044 error (_("Remote write of %d bytes returned 0!"), bytes
);
13045 else if (retcode
< bytes
)
13047 /* Short write. Save the rest of the read data for the next
13049 bytes_in_buffer
= bytes
- retcode
;
13050 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
13056 if (remote_hostio_close (fd
.release (), &remote_errno
))
13057 remote_hostio_error (remote_errno
);
13060 gdb_printf (_("Successfully sent file \"%s\".\n"), local_file
);
13064 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
13066 remote_target
*remote
= get_current_remote_target ();
13068 if (remote
== nullptr)
13069 error (_("command can only be used with remote target"));
13071 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
13075 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
13078 fileio_error remote_errno
;
13079 int bytes
, io_size
;
13082 scoped_remote_fd fd
13083 (this, remote_hostio_open (NULL
,
13084 remote_file
, FILEIO_O_RDONLY
, 0, 0,
13086 if (fd
.get () == -1)
13087 remote_hostio_error (remote_errno
);
13089 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
13091 perror_with_name (local_file
);
13093 /* Send up to this many bytes at once. They won't all fit in the
13094 remote packet limit, so we'll transfer slightly fewer. */
13095 io_size
= get_remote_packet_size ();
13096 gdb::byte_vector
buffer (io_size
);
13101 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
13104 /* Success, but no bytes, means end-of-file. */
13107 remote_hostio_error (remote_errno
);
13111 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
13113 perror_with_name (local_file
);
13116 if (remote_hostio_close (fd
.release (), &remote_errno
))
13117 remote_hostio_error (remote_errno
);
13120 gdb_printf (_("Successfully fetched file \"%s\".\n"), remote_file
);
13124 remote_file_delete (const char *remote_file
, int from_tty
)
13126 remote_target
*remote
= get_current_remote_target ();
13128 if (remote
== nullptr)
13129 error (_("command can only be used with remote target"));
13131 remote
->remote_file_delete (remote_file
, from_tty
);
13135 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
13138 fileio_error remote_errno
;
13140 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
13142 remote_hostio_error (remote_errno
);
13145 gdb_printf (_("Successfully deleted file \"%s\".\n"), remote_file
);
13149 remote_put_command (const char *args
, int from_tty
)
13152 error_no_arg (_("file to put"));
13154 gdb_argv
argv (args
);
13155 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13156 error (_("Invalid parameters to remote put"));
13158 remote_file_put (argv
[0], argv
[1], from_tty
);
13162 remote_get_command (const char *args
, int from_tty
)
13165 error_no_arg (_("file to get"));
13167 gdb_argv
argv (args
);
13168 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13169 error (_("Invalid parameters to remote get"));
13171 remote_file_get (argv
[0], argv
[1], from_tty
);
13175 remote_delete_command (const char *args
, int from_tty
)
13178 error_no_arg (_("file to delete"));
13180 gdb_argv
argv (args
);
13181 if (argv
[0] == NULL
|| argv
[1] != NULL
)
13182 error (_("Invalid parameters to remote delete"));
13184 remote_file_delete (argv
[0], from_tty
);
13188 remote_target::can_execute_reverse ()
13190 if (m_features
.packet_support (PACKET_bs
) == PACKET_ENABLE
13191 || m_features
.packet_support (PACKET_bc
) == PACKET_ENABLE
)
13198 remote_target::supports_non_stop ()
13204 remote_target::supports_disable_randomization ()
13206 /* Only supported in extended mode. */
13211 remote_target::supports_multi_process ()
13213 return m_features
.remote_multi_process_p ();
13217 remote_target::remote_supports_cond_tracepoints ()
13219 return (m_features
.packet_support (PACKET_ConditionalTracepoints
)
13224 remote_target::supports_evaluation_of_breakpoint_conditions ()
13226 return (m_features
.packet_support (PACKET_ConditionalBreakpoints
)
13231 remote_target::remote_supports_fast_tracepoints ()
13233 return m_features
.packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13237 remote_target::remote_supports_static_tracepoints ()
13239 return m_features
.packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13243 remote_target::remote_supports_install_in_trace ()
13245 return m_features
.packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13249 remote_target::supports_enable_disable_tracepoint ()
13251 return (m_features
.packet_support (PACKET_EnableDisableTracepoints_feature
)
13256 remote_target::supports_string_tracing ()
13258 return m_features
.packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13262 remote_target::can_run_breakpoint_commands ()
13264 return m_features
.packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13268 remote_target::trace_init ()
13270 struct remote_state
*rs
= get_remote_state ();
13273 remote_get_noisy_reply ();
13274 if (strcmp (rs
->buf
.data (), "OK") != 0)
13275 error (_("Target does not support this command."));
13278 /* Recursive routine to walk through command list including loops, and
13279 download packets for each command. */
13282 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13283 struct command_line
*cmds
)
13285 struct remote_state
*rs
= get_remote_state ();
13286 struct command_line
*cmd
;
13288 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13290 QUIT
; /* Allow user to bail out with ^C. */
13291 strcpy (rs
->buf
.data (), "QTDPsrc:");
13292 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13293 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13294 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13296 remote_get_noisy_reply ();
13297 if (strcmp (rs
->buf
.data (), "OK"))
13298 warning (_("Target does not support source download."));
13300 if (cmd
->control_type
== while_control
13301 || cmd
->control_type
== while_stepping_control
)
13303 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13305 QUIT
; /* Allow user to bail out with ^C. */
13306 strcpy (rs
->buf
.data (), "QTDPsrc:");
13307 encode_source_string (num
, addr
, "cmd", "end",
13308 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13309 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13311 remote_get_noisy_reply ();
13312 if (strcmp (rs
->buf
.data (), "OK"))
13313 warning (_("Target does not support source download."));
13319 remote_target::download_tracepoint (struct bp_location
*loc
)
13323 std::vector
<std::string
> tdp_actions
;
13324 std::vector
<std::string
> stepping_actions
;
13326 struct breakpoint
*b
= loc
->owner
;
13327 tracepoint
*t
= gdb::checked_static_cast
<tracepoint
*> (b
);
13328 struct remote_state
*rs
= get_remote_state ();
13330 const char *err_msg
= _("Tracepoint packet too large for target.");
13333 /* We use a buffer other than rs->buf because we'll build strings
13334 across multiple statements, and other statements in between could
13336 gdb::char_vector
buf (get_remote_packet_size ());
13338 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13340 tpaddr
= loc
->address
;
13341 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13342 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13343 b
->number
, addrbuf
, /* address */
13344 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13345 t
->step_count
, t
->pass_count
);
13347 if (ret
< 0 || ret
>= buf
.size ())
13348 error ("%s", err_msg
);
13350 /* Fast tracepoints are mostly handled by the target, but we can
13351 tell the target how big of an instruction block should be moved
13353 if (b
->type
== bp_fast_tracepoint
)
13355 /* Only test for support at download time; we may not know
13356 target capabilities at definition time. */
13357 if (remote_supports_fast_tracepoints ())
13359 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13362 size_left
= buf
.size () - strlen (buf
.data ());
13363 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13365 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13367 if (ret
< 0 || ret
>= size_left
)
13368 error ("%s", err_msg
);
13371 /* If it passed validation at definition but fails now,
13372 something is very wrong. */
13373 internal_error (_("Fast tracepoint not valid during download"));
13376 /* Fast tracepoints are functionally identical to regular
13377 tracepoints, so don't take lack of support as a reason to
13378 give up on the trace run. */
13379 warning (_("Target does not support fast tracepoints, "
13380 "downloading %d as regular tracepoint"), b
->number
);
13382 else if (b
->type
== bp_static_tracepoint
13383 || b
->type
== bp_static_marker_tracepoint
)
13385 /* Only test for support at download time; we may not know
13386 target capabilities at definition time. */
13387 if (remote_supports_static_tracepoints ())
13389 struct static_tracepoint_marker marker
;
13391 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13393 size_left
= buf
.size () - strlen (buf
.data ());
13394 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13397 if (ret
< 0 || ret
>= size_left
)
13398 error ("%s", err_msg
);
13401 error (_("Static tracepoint not valid during download"));
13404 /* Fast tracepoints are functionally identical to regular
13405 tracepoints, so don't take lack of support as a reason
13406 to give up on the trace run. */
13407 error (_("Target does not support static tracepoints"));
13409 /* If the tracepoint has a conditional, make it into an agent
13410 expression and append to the definition. */
13413 /* Only test support at download time, we may not know target
13414 capabilities at definition time. */
13415 if (remote_supports_cond_tracepoints ())
13417 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13420 size_left
= buf
.size () - strlen (buf
.data ());
13422 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13423 size_left
, ":X%x,", (int) aexpr
->buf
.size ());
13425 if (ret
< 0 || ret
>= size_left
)
13426 error ("%s", err_msg
);
13428 size_left
= buf
.size () - strlen (buf
.data ());
13430 /* Two bytes to encode each aexpr byte, plus the terminating
13432 if (aexpr
->buf
.size () * 2 + 1 > size_left
)
13433 error ("%s", err_msg
);
13435 pkt
= buf
.data () + strlen (buf
.data ());
13437 for (int ndx
= 0; ndx
< aexpr
->buf
.size (); ++ndx
)
13438 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13442 warning (_("Target does not support conditional tracepoints, "
13443 "ignoring tp %d cond"), b
->number
);
13446 if (b
->commands
|| !default_collect
.empty ())
13448 size_left
= buf
.size () - strlen (buf
.data ());
13450 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13453 if (ret
< 0 || ret
>= size_left
)
13454 error ("%s", err_msg
);
13457 putpkt (buf
.data ());
13458 remote_get_noisy_reply ();
13459 if (strcmp (rs
->buf
.data (), "OK"))
13460 error (_("Target does not support tracepoints."));
13462 /* do_single_steps (t); */
13463 for (auto action_it
= tdp_actions
.begin ();
13464 action_it
!= tdp_actions
.end (); action_it
++)
13466 QUIT
; /* Allow user to bail out with ^C. */
13468 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13469 || !stepping_actions
.empty ());
13471 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13472 b
->number
, addrbuf
, /* address */
13473 action_it
->c_str (),
13474 has_more
? '-' : 0);
13476 if (ret
< 0 || ret
>= buf
.size ())
13477 error ("%s", err_msg
);
13479 putpkt (buf
.data ());
13480 remote_get_noisy_reply ();
13481 if (strcmp (rs
->buf
.data (), "OK"))
13482 error (_("Error on target while setting tracepoints."));
13485 for (auto action_it
= stepping_actions
.begin ();
13486 action_it
!= stepping_actions
.end (); action_it
++)
13488 QUIT
; /* Allow user to bail out with ^C. */
13490 bool is_first
= action_it
== stepping_actions
.begin ();
13491 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13493 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13494 b
->number
, addrbuf
, /* address */
13495 is_first
? "S" : "",
13496 action_it
->c_str (),
13497 has_more
? "-" : "");
13499 if (ret
< 0 || ret
>= buf
.size ())
13500 error ("%s", err_msg
);
13502 putpkt (buf
.data ());
13503 remote_get_noisy_reply ();
13504 if (strcmp (rs
->buf
.data (), "OK"))
13505 error (_("Error on target while setting tracepoints."));
13508 if (m_features
.packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13510 if (b
->locspec
!= nullptr)
13512 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13514 if (ret
< 0 || ret
>= buf
.size ())
13515 error ("%s", err_msg
);
13517 const char *str
= b
->locspec
->to_string ();
13518 encode_source_string (b
->number
, loc
->address
, "at", str
,
13519 buf
.data () + strlen (buf
.data ()),
13520 buf
.size () - strlen (buf
.data ()));
13521 putpkt (buf
.data ());
13522 remote_get_noisy_reply ();
13523 if (strcmp (rs
->buf
.data (), "OK"))
13524 warning (_("Target does not support source download."));
13526 if (b
->cond_string
)
13528 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13530 if (ret
< 0 || ret
>= buf
.size ())
13531 error ("%s", err_msg
);
13533 encode_source_string (b
->number
, loc
->address
,
13534 "cond", b
->cond_string
.get (),
13535 buf
.data () + strlen (buf
.data ()),
13536 buf
.size () - strlen (buf
.data ()));
13537 putpkt (buf
.data ());
13538 remote_get_noisy_reply ();
13539 if (strcmp (rs
->buf
.data (), "OK"))
13540 warning (_("Target does not support source download."));
13542 remote_download_command_source (b
->number
, loc
->address
,
13543 breakpoint_commands (b
));
13548 remote_target::can_download_tracepoint ()
13550 struct remote_state
*rs
= get_remote_state ();
13551 struct trace_status
*ts
;
13554 /* Don't try to install tracepoints until we've relocated our
13555 symbols, and fetched and merged the target's tracepoint list with
13557 if (rs
->starting_up
)
13560 ts
= current_trace_status ();
13561 status
= get_trace_status (ts
);
13563 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13566 /* If we are in a tracing experiment, but remote stub doesn't support
13567 installing tracepoint in trace, we have to return. */
13568 if (!remote_supports_install_in_trace ())
13576 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13578 struct remote_state
*rs
= get_remote_state ();
13581 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13582 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13584 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13585 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13586 >= get_remote_packet_size ())
13587 error (_("Trace state variable name too long for tsv definition packet"));
13588 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13591 remote_get_noisy_reply ();
13592 if (rs
->buf
[0] == '\0')
13593 error (_("Target does not support this command."));
13594 if (strcmp (rs
->buf
.data (), "OK") != 0)
13595 error (_("Error on target while downloading trace state variable."));
13599 remote_target::enable_tracepoint (struct bp_location
*location
)
13601 struct remote_state
*rs
= get_remote_state ();
13603 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
13604 location
->owner
->number
,
13605 phex (location
->address
, sizeof (CORE_ADDR
)));
13607 remote_get_noisy_reply ();
13608 if (rs
->buf
[0] == '\0')
13609 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
13610 if (strcmp (rs
->buf
.data (), "OK") != 0)
13611 error (_("Error on target while enabling tracepoint."));
13615 remote_target::disable_tracepoint (struct bp_location
*location
)
13617 struct remote_state
*rs
= get_remote_state ();
13619 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
13620 location
->owner
->number
,
13621 phex (location
->address
, sizeof (CORE_ADDR
)));
13623 remote_get_noisy_reply ();
13624 if (rs
->buf
[0] == '\0')
13625 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
13626 if (strcmp (rs
->buf
.data (), "OK") != 0)
13627 error (_("Error on target while disabling tracepoint."));
13631 remote_target::trace_set_readonly_regions ()
13634 bfd_size_type size
;
13638 bfd
*abfd
= current_program_space
->exec_bfd ();
13641 return; /* No information to give. */
13643 struct remote_state
*rs
= get_remote_state ();
13645 strcpy (rs
->buf
.data (), "QTro");
13646 offset
= strlen (rs
->buf
.data ());
13647 for (s
= abfd
->sections
; s
; s
= s
->next
)
13649 char tmp1
[40], tmp2
[40];
13652 if ((s
->flags
& SEC_LOAD
) == 0
13653 /* || (s->flags & SEC_CODE) == 0 */
13654 || (s
->flags
& SEC_READONLY
) == 0)
13658 vma
= bfd_section_vma (s
);
13659 size
= bfd_section_size (s
);
13660 bfd_sprintf_vma (abfd
, tmp1
, vma
);
13661 bfd_sprintf_vma (abfd
, tmp2
, vma
+ size
);
13662 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
13663 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
13665 if (m_features
.packet_support (PACKET_qXfer_traceframe_info
)
13668 Too many sections for read-only sections definition packet."));
13671 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
13673 offset
+= sec_length
;
13683 remote_target::trace_start ()
13685 struct remote_state
*rs
= get_remote_state ();
13687 putpkt ("QTStart");
13688 remote_get_noisy_reply ();
13689 if (rs
->buf
[0] == '\0')
13690 error (_("Target does not support this command."));
13691 if (strcmp (rs
->buf
.data (), "OK") != 0)
13692 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13696 remote_target::get_trace_status (struct trace_status
*ts
)
13698 /* Initialize it just to avoid a GCC false warning. */
13700 enum packet_result result
;
13701 struct remote_state
*rs
= get_remote_state ();
13703 if (m_features
.packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
13706 /* FIXME we need to get register block size some other way. */
13707 trace_regblock_size
13708 = rs
->get_remote_arch_state (current_inferior ()->arch ())->sizeof_g_packet
;
13710 putpkt ("qTStatus");
13714 p
= remote_get_noisy_reply ();
13716 catch (const gdb_exception_error
&ex
)
13718 if (ex
.error
!= TARGET_CLOSE_ERROR
)
13720 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
13726 result
= m_features
.packet_ok (p
, PACKET_qTStatus
);
13728 /* If the remote target doesn't do tracing, flag it. */
13729 if (result
== PACKET_UNKNOWN
)
13732 /* We're working with a live target. */
13733 ts
->filename
= NULL
;
13736 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
13738 /* Function 'parse_trace_status' sets default value of each field of
13739 'ts' at first, so we don't have to do it here. */
13740 parse_trace_status (p
, ts
);
13742 return ts
->running
;
13746 remote_target::get_tracepoint_status (tracepoint
*tp
,
13747 struct uploaded_tp
*utp
)
13749 struct remote_state
*rs
= get_remote_state ();
13751 size_t size
= get_remote_packet_size ();
13756 tp
->traceframe_usage
= 0;
13757 for (bp_location
&loc
: tp
->locations ())
13759 /* If the tracepoint was never downloaded, don't go asking for
13761 if (tp
->number_on_target
== 0)
13763 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
13764 phex_nz (loc
.address
, 0));
13766 reply
= remote_get_noisy_reply ();
13767 if (reply
&& *reply
)
13770 parse_tracepoint_status (reply
+ 1, tp
, utp
);
13776 utp
->hit_count
= 0;
13777 utp
->traceframe_usage
= 0;
13778 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
13779 phex_nz (utp
->addr
, 0));
13781 reply
= remote_get_noisy_reply ();
13782 if (reply
&& *reply
)
13785 parse_tracepoint_status (reply
+ 1, tp
, utp
);
13791 remote_target::trace_stop ()
13793 struct remote_state
*rs
= get_remote_state ();
13796 remote_get_noisy_reply ();
13797 if (rs
->buf
[0] == '\0')
13798 error (_("Target does not support this command."));
13799 if (strcmp (rs
->buf
.data (), "OK") != 0)
13800 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13804 remote_target::trace_find (enum trace_find_type type
, int num
,
13805 CORE_ADDR addr1
, CORE_ADDR addr2
,
13808 struct remote_state
*rs
= get_remote_state ();
13809 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
13811 int target_frameno
= -1, target_tracept
= -1;
13813 /* Lookups other than by absolute frame number depend on the current
13814 trace selected, so make sure it is correct on the remote end
13816 if (type
!= tfind_number
)
13817 set_remote_traceframe ();
13819 p
= rs
->buf
.data ();
13820 strcpy (p
, "QTFrame:");
13821 p
= strchr (p
, '\0');
13825 xsnprintf (p
, endbuf
- p
, "%x", num
);
13828 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
13831 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
13834 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
13835 phex_nz (addr2
, 0));
13837 case tfind_outside
:
13838 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
13839 phex_nz (addr2
, 0));
13842 error (_("Unknown trace find type %d"), type
);
13846 reply
= remote_get_noisy_reply ();
13847 if (*reply
== '\0')
13848 error (_("Target does not support this command."));
13850 while (reply
&& *reply
)
13855 target_frameno
= (int) strtol (p
, &reply
, 16);
13857 error (_("Unable to parse trace frame number"));
13858 /* Don't update our remote traceframe number cache on failure
13859 to select a remote traceframe. */
13860 if (target_frameno
== -1)
13865 target_tracept
= (int) strtol (p
, &reply
, 16);
13867 error (_("Unable to parse tracepoint number"));
13869 case 'O': /* "OK"? */
13870 if (reply
[1] == 'K' && reply
[2] == '\0')
13873 error (_("Bogus reply from target: %s"), reply
);
13876 error (_("Bogus reply from target: %s"), reply
);
13879 *tpp
= target_tracept
;
13881 rs
->remote_traceframe_number
= target_frameno
;
13882 return target_frameno
;
13886 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
13888 struct remote_state
*rs
= get_remote_state ();
13892 set_remote_traceframe ();
13894 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
13896 reply
= remote_get_noisy_reply ();
13897 if (reply
&& *reply
)
13901 unpack_varlen_hex (reply
+ 1, &uval
);
13902 *val
= (LONGEST
) uval
;
13910 remote_target::save_trace_data (const char *filename
)
13912 struct remote_state
*rs
= get_remote_state ();
13915 p
= rs
->buf
.data ();
13916 strcpy (p
, "QTSave:");
13918 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
13919 >= get_remote_packet_size ())
13920 error (_("Remote file name too long for trace save packet"));
13921 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
13924 reply
= remote_get_noisy_reply ();
13925 if (*reply
== '\0')
13926 error (_("Target does not support this command."));
13927 if (strcmp (reply
, "OK") != 0)
13928 error (_("Bogus reply from target: %s"), reply
);
13932 /* This is basically a memory transfer, but needs to be its own packet
13933 because we don't know how the target actually organizes its trace
13934 memory, plus we want to be able to ask for as much as possible, but
13935 not be unhappy if we don't get as much as we ask for. */
13938 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
13940 struct remote_state
*rs
= get_remote_state ();
13945 p
= rs
->buf
.data ();
13946 strcpy (p
, "qTBuffer:");
13948 p
+= hexnumstr (p
, offset
);
13950 p
+= hexnumstr (p
, len
);
13954 reply
= remote_get_noisy_reply ();
13955 if (reply
&& *reply
)
13957 /* 'l' by itself means we're at the end of the buffer and
13958 there is nothing more to get. */
13962 /* Convert the reply into binary. Limit the number of bytes to
13963 convert according to our passed-in buffer size, rather than
13964 what was returned in the packet; if the target is
13965 unexpectedly generous and gives us a bigger reply than we
13966 asked for, we don't want to crash. */
13967 rslt
= hex2bin (reply
, buf
, len
);
13971 /* Something went wrong, flag as an error. */
13976 remote_target::set_disconnected_tracing (int val
)
13978 struct remote_state
*rs
= get_remote_state ();
13980 if (m_features
.packet_support (PACKET_DisconnectedTracing_feature
)
13985 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13986 "QTDisconnected:%x", val
);
13988 reply
= remote_get_noisy_reply ();
13989 if (*reply
== '\0')
13990 error (_("Target does not support this command."));
13991 if (strcmp (reply
, "OK") != 0)
13992 error (_("Bogus reply from target: %s"), reply
);
13995 warning (_("Target does not support disconnected tracing."));
13999 remote_target::core_of_thread (ptid_t ptid
)
14001 thread_info
*info
= this->find_thread (ptid
);
14003 if (info
!= NULL
&& info
->priv
!= NULL
)
14004 return get_remote_thread_info (info
)->core
;
14010 remote_target::set_circular_trace_buffer (int val
)
14012 struct remote_state
*rs
= get_remote_state ();
14015 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
14016 "QTBuffer:circular:%x", val
);
14018 reply
= remote_get_noisy_reply ();
14019 if (*reply
== '\0')
14020 error (_("Target does not support this command."));
14021 if (strcmp (reply
, "OK") != 0)
14022 error (_("Bogus reply from target: %s"), reply
);
14026 remote_target::traceframe_info ()
14028 gdb::optional
<gdb::char_vector
> text
14029 = target_read_stralloc (current_inferior ()->top_target (),
14030 TARGET_OBJECT_TRACEFRAME_INFO
,
14033 return parse_traceframe_info (text
->data ());
14038 /* Handle the qTMinFTPILen packet. Returns the minimum length of
14039 instruction on which a fast tracepoint may be placed. Returns -1
14040 if the packet is not supported, and 0 if the minimum instruction
14041 length is unknown. */
14044 remote_target::get_min_fast_tracepoint_insn_len ()
14046 struct remote_state
*rs
= get_remote_state ();
14049 /* If we're not debugging a process yet, the IPA can't be
14051 if (!target_has_execution ())
14054 /* Make sure the remote is pointing at the right process. */
14055 set_general_process ();
14057 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
14059 reply
= remote_get_noisy_reply ();
14060 if (*reply
== '\0')
14064 ULONGEST min_insn_len
;
14066 unpack_varlen_hex (reply
, &min_insn_len
);
14068 return (int) min_insn_len
;
14073 remote_target::set_trace_buffer_size (LONGEST val
)
14075 if (m_features
.packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
14077 struct remote_state
*rs
= get_remote_state ();
14078 char *buf
= rs
->buf
.data ();
14079 char *endbuf
= buf
+ get_remote_packet_size ();
14080 enum packet_result result
;
14082 gdb_assert (val
>= 0 || val
== -1);
14083 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
14084 /* Send -1 as literal "-1" to avoid host size dependency. */
14088 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
14091 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
14094 remote_get_noisy_reply ();
14095 result
= m_features
.packet_ok (rs
->buf
, PACKET_QTBuffer_size
);
14097 if (result
!= PACKET_OK
)
14098 warning (_("Bogus reply from target: %s"), rs
->buf
.data ());
14103 remote_target::set_trace_notes (const char *user
, const char *notes
,
14104 const char *stop_notes
)
14106 struct remote_state
*rs
= get_remote_state ();
14108 char *buf
= rs
->buf
.data ();
14109 char *endbuf
= buf
+ get_remote_packet_size ();
14112 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
14115 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
14116 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
14122 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
14123 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
14129 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
14130 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
14134 /* Ensure the buffer is terminated. */
14138 reply
= remote_get_noisy_reply ();
14139 if (*reply
== '\0')
14142 if (strcmp (reply
, "OK") != 0)
14143 error (_("Bogus reply from target: %s"), reply
);
14149 remote_target::use_agent (bool use
)
14151 if (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
14153 struct remote_state
*rs
= get_remote_state ();
14155 /* If the stub supports QAgent. */
14156 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
14160 if (strcmp (rs
->buf
.data (), "OK") == 0)
14171 remote_target::can_use_agent ()
14173 return (m_features
.packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
14176 #if defined (HAVE_LIBEXPAT)
14178 /* Check the btrace document version. */
14181 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
14182 const struct gdb_xml_element
*element
,
14184 std::vector
<gdb_xml_value
> &attributes
)
14186 const char *version
14187 = (const char *) xml_find_attribute (attributes
, "version")->value
.get ();
14189 if (strcmp (version
, "1.0") != 0)
14190 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
14193 /* Parse a btrace "block" xml record. */
14196 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
14197 const struct gdb_xml_element
*element
,
14199 std::vector
<gdb_xml_value
> &attributes
)
14201 struct btrace_data
*btrace
;
14202 ULONGEST
*begin
, *end
;
14204 btrace
= (struct btrace_data
*) user_data
;
14206 switch (btrace
->format
)
14208 case BTRACE_FORMAT_BTS
:
14211 case BTRACE_FORMAT_NONE
:
14212 btrace
->format
= BTRACE_FORMAT_BTS
;
14213 btrace
->variant
.bts
.blocks
= new std::vector
<btrace_block
>;
14217 gdb_xml_error (parser
, _("Btrace format error."));
14220 begin
= (ULONGEST
*) xml_find_attribute (attributes
, "begin")->value
.get ();
14221 end
= (ULONGEST
*) xml_find_attribute (attributes
, "end")->value
.get ();
14222 btrace
->variant
.bts
.blocks
->emplace_back (*begin
, *end
);
14225 /* Parse a "raw" xml record. */
14228 parse_xml_raw (struct gdb_xml_parser
*parser
, const char *body_text
,
14229 gdb_byte
**pdata
, size_t *psize
)
14234 len
= strlen (body_text
);
14236 gdb_xml_error (parser
, _("Bad raw data size."));
14240 gdb::unique_xmalloc_ptr
<gdb_byte
> data ((gdb_byte
*) xmalloc (size
));
14243 /* We use hex encoding - see gdbsupport/rsp-low.h. */
14251 if (hi
== 0 || lo
== 0)
14252 gdb_xml_error (parser
, _("Bad hex encoding."));
14254 *bin
++ = fromhex (hi
) * 16 + fromhex (lo
);
14258 *pdata
= data
.release ();
14262 /* Parse a btrace pt-config "cpu" xml record. */
14265 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser
*parser
,
14266 const struct gdb_xml_element
*element
,
14268 std::vector
<gdb_xml_value
> &attributes
)
14270 struct btrace_data
*btrace
;
14271 const char *vendor
;
14272 ULONGEST
*family
, *model
, *stepping
;
14275 = (const char *) xml_find_attribute (attributes
, "vendor")->value
.get ();
14277 = (ULONGEST
*) xml_find_attribute (attributes
, "family")->value
.get ();
14279 = (ULONGEST
*) xml_find_attribute (attributes
, "model")->value
.get ();
14281 = (ULONGEST
*) xml_find_attribute (attributes
, "stepping")->value
.get ();
14283 btrace
= (struct btrace_data
*) user_data
;
14285 if (strcmp (vendor
, "GenuineIntel") == 0)
14286 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_INTEL
;
14288 btrace
->variant
.pt
.config
.cpu
.family
= *family
;
14289 btrace
->variant
.pt
.config
.cpu
.model
= *model
;
14290 btrace
->variant
.pt
.config
.cpu
.stepping
= *stepping
;
14293 /* Parse a btrace pt "raw" xml record. */
14296 parse_xml_btrace_pt_raw (struct gdb_xml_parser
*parser
,
14297 const struct gdb_xml_element
*element
,
14298 void *user_data
, const char *body_text
)
14300 struct btrace_data
*btrace
;
14302 btrace
= (struct btrace_data
*) user_data
;
14303 parse_xml_raw (parser
, body_text
, &btrace
->variant
.pt
.data
,
14304 &btrace
->variant
.pt
.size
);
14307 /* Parse a btrace "pt" xml record. */
14310 parse_xml_btrace_pt (struct gdb_xml_parser
*parser
,
14311 const struct gdb_xml_element
*element
,
14313 std::vector
<gdb_xml_value
> &attributes
)
14315 struct btrace_data
*btrace
;
14317 btrace
= (struct btrace_data
*) user_data
;
14318 btrace
->format
= BTRACE_FORMAT_PT
;
14319 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_UNKNOWN
;
14320 btrace
->variant
.pt
.data
= NULL
;
14321 btrace
->variant
.pt
.size
= 0;
14324 static const struct gdb_xml_attribute block_attributes
[] = {
14325 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14326 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14327 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14330 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes
[] = {
14331 { "vendor", GDB_XML_AF_NONE
, NULL
, NULL
},
14332 { "family", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14333 { "model", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14334 { "stepping", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
14335 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14338 static const struct gdb_xml_element btrace_pt_config_children
[] = {
14339 { "cpu", btrace_pt_config_cpu_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14340 parse_xml_btrace_pt_config_cpu
, NULL
},
14341 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14344 static const struct gdb_xml_element btrace_pt_children
[] = {
14345 { "pt-config", NULL
, btrace_pt_config_children
, GDB_XML_EF_OPTIONAL
, NULL
,
14347 { "raw", NULL
, NULL
, GDB_XML_EF_OPTIONAL
, NULL
, parse_xml_btrace_pt_raw
},
14348 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14351 static const struct gdb_xml_attribute btrace_attributes
[] = {
14352 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
14353 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14356 static const struct gdb_xml_element btrace_children
[] = {
14357 { "block", block_attributes
, NULL
,
14358 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
14359 { "pt", NULL
, btrace_pt_children
, GDB_XML_EF_OPTIONAL
, parse_xml_btrace_pt
,
14361 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14364 static const struct gdb_xml_element btrace_elements
[] = {
14365 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
14366 check_xml_btrace_version
, NULL
},
14367 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14370 #endif /* defined (HAVE_LIBEXPAT) */
14372 /* Parse a branch trace xml document XML into DATA. */
14375 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
14377 #if defined (HAVE_LIBEXPAT)
14380 btrace_data result
;
14381 result
.format
= BTRACE_FORMAT_NONE
;
14383 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
14386 error (_("Error parsing branch trace."));
14388 /* Keep parse results. */
14389 *btrace
= std::move (result
);
14391 #else /* !defined (HAVE_LIBEXPAT) */
14393 error (_("Cannot process branch trace. XML support was disabled at "
14396 #endif /* !defined (HAVE_LIBEXPAT) */
14399 #if defined (HAVE_LIBEXPAT)
14401 /* Parse a btrace-conf "bts" xml record. */
14404 parse_xml_btrace_conf_bts (struct gdb_xml_parser
*parser
,
14405 const struct gdb_xml_element
*element
,
14407 std::vector
<gdb_xml_value
> &attributes
)
14409 struct btrace_config
*conf
;
14410 struct gdb_xml_value
*size
;
14412 conf
= (struct btrace_config
*) user_data
;
14413 conf
->format
= BTRACE_FORMAT_BTS
;
14414 conf
->bts
.size
= 0;
14416 size
= xml_find_attribute (attributes
, "size");
14418 conf
->bts
.size
= (unsigned int) *(ULONGEST
*) size
->value
.get ();
14421 /* Parse a btrace-conf "pt" xml record. */
14424 parse_xml_btrace_conf_pt (struct gdb_xml_parser
*parser
,
14425 const struct gdb_xml_element
*element
,
14427 std::vector
<gdb_xml_value
> &attributes
)
14429 struct btrace_config
*conf
;
14430 struct gdb_xml_value
*size
;
14432 conf
= (struct btrace_config
*) user_data
;
14433 conf
->format
= BTRACE_FORMAT_PT
;
14436 size
= xml_find_attribute (attributes
, "size");
14438 conf
->pt
.size
= (unsigned int) *(ULONGEST
*) size
->value
.get ();
14441 static const struct gdb_xml_attribute btrace_conf_pt_attributes
[] = {
14442 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
14443 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14446 static const struct gdb_xml_attribute btrace_conf_bts_attributes
[] = {
14447 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
14448 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14451 static const struct gdb_xml_element btrace_conf_children
[] = {
14452 { "bts", btrace_conf_bts_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14453 parse_xml_btrace_conf_bts
, NULL
},
14454 { "pt", btrace_conf_pt_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
14455 parse_xml_btrace_conf_pt
, NULL
},
14456 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14459 static const struct gdb_xml_attribute btrace_conf_attributes
[] = {
14460 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
14461 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
14464 static const struct gdb_xml_element btrace_conf_elements
[] = {
14465 { "btrace-conf", btrace_conf_attributes
, btrace_conf_children
,
14466 GDB_XML_EF_NONE
, NULL
, NULL
},
14467 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
14470 #endif /* defined (HAVE_LIBEXPAT) */
14472 /* Parse a branch trace configuration xml document XML into CONF. */
14475 parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
)
14477 #if defined (HAVE_LIBEXPAT)
14480 errcode
= gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
14481 btrace_conf_elements
, xml
, conf
);
14483 error (_("Error parsing branch trace configuration."));
14485 #else /* !defined (HAVE_LIBEXPAT) */
14487 error (_("Cannot process the branch trace configuration. XML support "
14488 "was disabled at compile time."));
14490 #endif /* !defined (HAVE_LIBEXPAT) */
14493 /* Reset our idea of our target's btrace configuration. */
14496 remote_btrace_reset (remote_state
*rs
)
14498 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14501 /* Synchronize the configuration with the target. */
14504 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14506 struct remote_state
*rs
;
14507 char *buf
, *pos
, *endbuf
;
14509 rs
= get_remote_state ();
14510 buf
= rs
->buf
.data ();
14511 endbuf
= buf
+ get_remote_packet_size ();
14513 if (m_features
.packet_support (PACKET_Qbtrace_conf_bts_size
) == PACKET_ENABLE
14514 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14517 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14518 packets_descriptions
[PACKET_Qbtrace_conf_bts_size
].name
,
14524 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_bts_size
)
14527 if (buf
[0] == 'E' && buf
[1] == '.')
14528 error (_("Failed to configure the BTS buffer size: %s"), buf
+ 2);
14530 error (_("Failed to configure the BTS buffer size."));
14533 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14536 if (m_features
.packet_support (PACKET_Qbtrace_conf_pt_size
) == PACKET_ENABLE
14537 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14540 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x",
14541 packets_descriptions
[PACKET_Qbtrace_conf_pt_size
].name
,
14547 if (m_features
.packet_ok (buf
, PACKET_Qbtrace_conf_pt_size
)
14550 if (buf
[0] == 'E' && buf
[1] == '.')
14551 error (_("Failed to configure the trace buffer size: %s"), buf
+ 2);
14553 error (_("Failed to configure the trace buffer size."));
14556 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14560 /* Read TP's btrace configuration from the target and store it into CONF. */
14563 btrace_read_config (thread_info
*tp
, btrace_config
*conf
)
14565 /* target_read_stralloc relies on INFERIOR_PTID. */
14566 scoped_restore_current_thread restore_thread
;
14567 switch_to_thread (tp
);
14569 gdb::optional
<gdb::char_vector
> xml
14570 = target_read_stralloc (current_inferior ()->top_target (),
14571 TARGET_OBJECT_BTRACE_CONF
, "");
14573 parse_xml_btrace_conf (conf
, xml
->data ());
14576 /* Maybe reopen target btrace. */
14579 remote_target::remote_btrace_maybe_reopen ()
14581 struct remote_state
*rs
= get_remote_state ();
14582 int btrace_target_pushed
= 0;
14583 #if !defined (HAVE_LIBIPT)
14587 /* Don't bother walking the entirety of the remote thread list when
14588 we know the feature isn't supported by the remote. */
14589 if (m_features
.packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
14592 for (thread_info
*tp
: all_non_exited_threads (this))
14594 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
14595 btrace_read_config (tp
, &rs
->btrace_config
);
14597 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
14600 #if !defined (HAVE_LIBIPT)
14601 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
14606 warning (_("Target is recording using Intel Processor Trace "
14607 "but support was disabled at compile time."));
14612 #endif /* !defined (HAVE_LIBIPT) */
14614 /* Push target, once, but before anything else happens. This way our
14615 changes to the threads will be cleaned up by unpushing the target
14616 in case btrace_read_config () throws. */
14617 if (!btrace_target_pushed
)
14619 btrace_target_pushed
= 1;
14620 record_btrace_push_target ();
14621 gdb_printf (_("Target is recording using %s.\n"),
14622 btrace_format_string (rs
->btrace_config
.format
));
14626 = new btrace_target_info
{ tp
->ptid
, rs
->btrace_config
};
14630 /* Enable branch tracing. */
14632 struct btrace_target_info
*
14633 remote_target::enable_btrace (thread_info
*tp
,
14634 const struct btrace_config
*conf
)
14636 struct packet_config
*packet
= NULL
;
14637 struct remote_state
*rs
= get_remote_state ();
14638 char *buf
= rs
->buf
.data ();
14639 char *endbuf
= buf
+ get_remote_packet_size ();
14641 unsigned int which_packet
;
14642 switch (conf
->format
)
14644 case BTRACE_FORMAT_BTS
:
14645 which_packet
= PACKET_Qbtrace_bts
;
14647 case BTRACE_FORMAT_PT
:
14648 which_packet
= PACKET_Qbtrace_pt
;
14651 internal_error (_("Bad branch btrace format: %u."),
14652 (unsigned int) conf
->format
);
14655 packet
= &m_features
.m_protocol_packets
[which_packet
];
14656 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
14657 error (_("Target does not support branch tracing."));
14659 btrace_sync_conf (conf
);
14661 ptid_t ptid
= tp
->ptid
;
14662 set_general_thread (ptid
);
14664 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14665 packets_descriptions
[which_packet
].name
);
14669 if (m_features
.packet_ok (rs
->buf
, which_packet
) == PACKET_ERROR
)
14671 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14672 error (_("Could not enable branch tracing for %s: %s"),
14673 target_pid_to_str (ptid
).c_str (), &rs
->buf
[2]);
14675 error (_("Could not enable branch tracing for %s."),
14676 target_pid_to_str (ptid
).c_str ());
14679 btrace_target_info
*tinfo
= new btrace_target_info
{ ptid
};
14681 /* If we fail to read the configuration, we lose some information, but the
14682 tracing itself is not impacted. */
14685 btrace_read_config (tp
, &tinfo
->conf
);
14687 catch (const gdb_exception_error
&err
)
14689 if (err
.message
!= NULL
)
14690 warning ("%s", err
.what ());
14696 /* Disable branch tracing. */
14699 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
14701 struct remote_state
*rs
= get_remote_state ();
14702 char *buf
= rs
->buf
.data ();
14703 char *endbuf
= buf
+ get_remote_packet_size ();
14705 if (m_features
.packet_support (PACKET_Qbtrace_off
) != PACKET_ENABLE
)
14706 error (_("Target does not support branch tracing."));
14708 set_general_thread (tinfo
->ptid
);
14710 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s",
14711 packets_descriptions
[PACKET_Qbtrace_off
].name
);
14715 if (m_features
.packet_ok (rs
->buf
, PACKET_Qbtrace_off
) == PACKET_ERROR
)
14717 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14718 error (_("Could not disable branch tracing for %s: %s"),
14719 target_pid_to_str (tinfo
->ptid
).c_str (), &rs
->buf
[2]);
14721 error (_("Could not disable branch tracing for %s."),
14722 target_pid_to_str (tinfo
->ptid
).c_str ());
14728 /* Teardown branch tracing. */
14731 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
14733 /* We must not talk to the target during teardown. */
14737 /* Read the branch trace. */
14740 remote_target::read_btrace (struct btrace_data
*btrace
,
14741 struct btrace_target_info
*tinfo
,
14742 enum btrace_read_type type
)
14746 if (m_features
.packet_support (PACKET_qXfer_btrace
) != PACKET_ENABLE
)
14747 error (_("Target does not support branch tracing."));
14749 #if !defined(HAVE_LIBEXPAT)
14750 error (_("Cannot process branch tracing result. XML parsing not supported."));
14755 case BTRACE_READ_ALL
:
14758 case BTRACE_READ_NEW
:
14761 case BTRACE_READ_DELTA
:
14765 internal_error (_("Bad branch tracing read type: %u."),
14766 (unsigned int) type
);
14769 gdb::optional
<gdb::char_vector
> xml
14770 = target_read_stralloc (current_inferior ()->top_target (),
14771 TARGET_OBJECT_BTRACE
, annex
);
14773 return BTRACE_ERR_UNKNOWN
;
14775 parse_xml_btrace (btrace
, xml
->data ());
14777 return BTRACE_ERR_NONE
;
14780 const struct btrace_config
*
14781 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
14783 return &tinfo
->conf
;
14787 remote_target::augmented_libraries_svr4_read ()
14790 (m_features
.packet_support (PACKET_augmented_libraries_svr4_read_feature
)
14794 /* Implementation of to_load. */
14797 remote_target::load (const char *name
, int from_tty
)
14799 generic_load (name
, from_tty
);
14802 /* Accepts an integer PID; returns a string representing a file that
14803 can be opened on the remote side to get the symbols for the child
14804 process. Returns NULL if the operation is not supported. */
14807 remote_target::pid_to_exec_file (int pid
)
14809 static gdb::optional
<gdb::char_vector
> filename
;
14810 char *annex
= NULL
;
14812 if (m_features
.packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
14815 inferior
*inf
= find_inferior_pid (this, pid
);
14817 internal_error (_("not currently attached to process %d"), pid
);
14819 if (!inf
->fake_pid_p
)
14821 const int annex_size
= 9;
14823 annex
= (char *) alloca (annex_size
);
14824 xsnprintf (annex
, annex_size
, "%x", pid
);
14827 filename
= target_read_stralloc (current_inferior ()->top_target (),
14828 TARGET_OBJECT_EXEC_FILE
, annex
);
14830 return filename
? filename
->data () : nullptr;
14833 /* Implement the to_can_do_single_step target_ops method. */
14836 remote_target::can_do_single_step ()
14838 /* We can only tell whether target supports single step or not by
14839 supported s and S vCont actions if the stub supports vContSupported
14840 feature. If the stub doesn't support vContSupported feature,
14841 we have conservatively to think target doesn't supports single
14843 if (m_features
.packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
14845 struct remote_state
*rs
= get_remote_state ();
14847 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
14853 /* Implementation of the to_execution_direction method for the remote
14856 enum exec_direction_kind
14857 remote_target::execution_direction ()
14859 struct remote_state
*rs
= get_remote_state ();
14861 return rs
->last_resume_exec_dir
;
14864 /* Return pointer to the thread_info struct which corresponds to
14865 THREAD_HANDLE (having length HANDLE_LEN). */
14868 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
14872 for (thread_info
*tp
: all_non_exited_threads (this))
14874 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14876 if (tp
->inf
== inf
&& priv
!= NULL
)
14878 if (handle_len
!= priv
->thread_handle
.size ())
14879 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
14880 handle_len
, priv
->thread_handle
.size ());
14881 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
14890 gdb::array_view
<const gdb_byte
>
14891 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
14893 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14894 return priv
->thread_handle
;
14898 remote_target::can_async_p ()
14900 /* This flag should be checked in the common target.c code. */
14901 gdb_assert (target_async_permitted
);
14903 /* We're async whenever the serial device can. */
14904 return get_remote_state ()->can_async_p ();
14908 remote_target::is_async_p ()
14910 /* We're async whenever the serial device is. */
14911 return get_remote_state ()->is_async_p ();
14914 /* Pass the SERIAL event on and up to the client. One day this code
14915 will be able to delay notifying the client of an event until the
14916 point where an entire packet has been received. */
14918 static serial_event_ftype remote_async_serial_handler
;
14921 remote_async_serial_handler (struct serial
*scb
, void *context
)
14923 /* Don't propogate error information up to the client. Instead let
14924 the client find out about the error by querying the target. */
14925 inferior_event_handler (INF_REG_EVENT
);
14929 remote_target::async_wait_fd ()
14931 struct remote_state
*rs
= get_remote_state ();
14932 return rs
->remote_desc
->fd
;
14936 remote_target::async (bool enable
)
14938 struct remote_state
*rs
= get_remote_state ();
14942 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
14944 /* If there are pending events in the stop reply queue tell the
14945 event loop to process them. */
14946 if (!rs
->stop_reply_queue
.empty ())
14947 rs
->mark_async_event_handler ();
14949 /* For simplicity, below we clear the pending events token
14950 without remembering whether it is marked, so here we always
14951 mark it. If there's actually no pending notification to
14952 process, this ends up being a no-op (other than a spurious
14953 event-loop wakeup). */
14954 if (target_is_non_stop_p ())
14955 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14959 serial_async (rs
->remote_desc
, NULL
, NULL
);
14960 /* If the core is disabling async, it doesn't want to be
14961 disturbed with target events. Clear all async event sources
14963 rs
->clear_async_event_handler ();
14965 if (target_is_non_stop_p ())
14966 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14970 /* Implementation of the to_thread_events method. */
14973 remote_target::thread_events (int enable
)
14975 struct remote_state
*rs
= get_remote_state ();
14976 size_t size
= get_remote_packet_size ();
14978 if (m_features
.packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
14981 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
14985 switch (m_features
.packet_ok (rs
->buf
, PACKET_QThreadEvents
))
14988 if (strcmp (rs
->buf
.data (), "OK") != 0)
14989 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
14992 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
14994 case PACKET_UNKNOWN
:
15000 show_remote_cmd (const char *args
, int from_tty
)
15002 /* We can't just use cmd_show_list here, because we want to skip
15003 the redundant "show remote Z-packet" and the legacy aliases. */
15004 struct cmd_list_element
*list
= remote_show_cmdlist
;
15005 struct ui_out
*uiout
= current_uiout
;
15007 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
15008 for (; list
!= NULL
; list
= list
->next
)
15009 if (strcmp (list
->name
, "Z-packet") == 0)
15011 else if (list
->type
== not_set_cmd
)
15012 /* Alias commands are exactly like the original, except they
15013 don't have the normal type. */
15017 ui_out_emit_tuple
option_emitter (uiout
, "option");
15019 uiout
->field_string ("name", list
->name
);
15020 uiout
->text (": ");
15021 if (list
->type
== show_cmd
)
15022 do_show_command (NULL
, from_tty
, list
);
15024 cmd_func (list
, NULL
, from_tty
);
15028 /* Some change happened in PSPACE's objfile list (obfiles added or removed),
15029 offer all inferiors using that program space a change to look up symbols. */
15032 remote_objfile_changed_check_symbols (program_space
*pspace
)
15034 /* The affected program space is possibly shared by multiple inferiors.
15035 Consider sending a qSymbol packet for each of the inferiors using that
15037 for (inferior
*inf
: all_inferiors ())
15039 if (inf
->pspace
!= pspace
)
15042 /* Check whether the inferior's process target is a remote target. */
15043 remote_target
*remote
= as_remote_target (inf
->process_target ());
15044 if (remote
== nullptr)
15047 /* When we are attaching or handling a fork child and the shared library
15048 subsystem reads the list of loaded libraries, we receive new objfile
15049 events in between each found library. The libraries are read in an
15050 undefined order, so if we gave the remote side a chance to look up
15051 symbols between each objfile, we might give it an inconsistent picture
15052 of the inferior. It could appear that a library A appears loaded but
15053 a library B does not, even though library A requires library B. That
15054 would present a state that couldn't normally exist in the inferior.
15056 So, skip these events, we'll give the remote a chance to look up
15057 symbols once all the loaded libraries and their symbols are known to
15059 if (inf
->in_initial_library_scan
)
15062 if (!remote
->has_execution (inf
))
15065 /* Need to switch to a specific thread, because remote_check_symbols will
15066 set the general thread using INFERIOR_PTID.
15068 It's possible to have inferiors with no thread here, because we are
15069 called very early in the connection process, while the inferior is
15070 being set up, before threads are added. Just skip it, start_remote_1
15071 also calls remote_check_symbols when it's done setting things up. */
15072 thread_info
*thread
= any_thread_of_inferior (inf
);
15073 if (thread
!= nullptr)
15075 scoped_restore_current_thread restore_thread
;
15076 switch_to_thread (thread
);
15077 remote
->remote_check_symbols ();
15082 /* Function to be called whenever a new objfile (shlib) is detected. */
15085 remote_new_objfile (struct objfile
*objfile
)
15087 remote_objfile_changed_check_symbols (objfile
->pspace
);
15090 /* Pull all the tracepoints defined on the target and create local
15091 data structures representing them. We don't want to create real
15092 tracepoints yet, we don't want to mess up the user's existing
15096 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
15098 struct remote_state
*rs
= get_remote_state ();
15101 /* Ask for a first packet of tracepoint definition. */
15104 p
= rs
->buf
.data ();
15105 while (*p
&& *p
!= 'l')
15107 parse_tracepoint_definition (p
, utpp
);
15108 /* Ask for another packet of tracepoint definition. */
15111 p
= rs
->buf
.data ();
15117 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
15119 struct remote_state
*rs
= get_remote_state ();
15122 /* Ask for a first packet of variable definition. */
15125 p
= rs
->buf
.data ();
15126 while (*p
&& *p
!= 'l')
15128 parse_tsv_definition (p
, utsvp
);
15129 /* Ask for another packet of variable definition. */
15132 p
= rs
->buf
.data ();
15137 /* The "set/show range-stepping" show hook. */
15140 show_range_stepping (struct ui_file
*file
, int from_tty
,
15141 struct cmd_list_element
*c
,
15145 _("Debugger's willingness to use range stepping "
15146 "is %s.\n"), value
);
15149 /* Return true if the vCont;r action is supported by the remote
15153 remote_target::vcont_r_supported ()
15155 return (m_features
.packet_support (PACKET_vCont
) == PACKET_ENABLE
15156 && get_remote_state ()->supports_vCont
.r
);
15159 /* The "set/show range-stepping" set hook. */
15162 set_range_stepping (const char *ignore_args
, int from_tty
,
15163 struct cmd_list_element
*c
)
15165 /* When enabling, check whether range stepping is actually supported
15166 by the target, and warn if not. */
15167 if (use_range_stepping
)
15169 remote_target
*remote
= get_current_remote_target ();
15171 || !remote
->vcont_r_supported ())
15172 warning (_("Range stepping is not supported by the current target"));
15177 show_remote_debug (struct ui_file
*file
, int from_tty
,
15178 struct cmd_list_element
*c
, const char *value
)
15180 gdb_printf (file
, _("Debugging of remote protocol is %s.\n"),
15185 show_remote_timeout (struct ui_file
*file
, int from_tty
,
15186 struct cmd_list_element
*c
, const char *value
)
15189 _("Timeout limit to wait for target to respond is %s.\n"),
15193 /* Implement the "supports_memory_tagging" target_ops method. */
15196 remote_target::supports_memory_tagging ()
15198 return m_features
.remote_memory_tagging_p ();
15201 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
15204 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
15205 size_t len
, int type
)
15207 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
15209 std::string request
= string_printf ("qMemTags:%s,%s:%s",
15210 phex_nz (address
, addr_size
),
15211 phex_nz (len
, sizeof (len
)),
15212 phex_nz (type
, sizeof (type
)));
15214 strcpy (packet
.data (), request
.c_str ());
15217 /* Parse the qMemTags packet reply into TAGS.
15219 Return true if successful, false otherwise. */
15222 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
15223 gdb::byte_vector
&tags
)
15225 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
15228 /* Copy the tag data. */
15229 tags
= hex2bin (reply
.data () + 1);
15234 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
15237 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
15238 size_t len
, int type
,
15239 const gdb::byte_vector
&tags
)
15241 int addr_size
= gdbarch_addr_bit (current_inferior ()->arch ()) / 8;
15243 /* Put together the main packet, address and length. */
15244 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
15245 phex_nz (address
, addr_size
),
15246 phex_nz (len
, sizeof (len
)),
15247 phex_nz (type
, sizeof (type
)));
15248 request
+= bin2hex (tags
.data (), tags
.size ());
15250 /* Check if we have exceeded the maximum packet size. */
15251 if (packet
.size () < request
.length ())
15252 error (_("Contents too big for packet QMemTags."));
15254 strcpy (packet
.data (), request
.c_str ());
15257 /* Implement the "fetch_memtags" target_ops method. */
15260 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
15261 gdb::byte_vector
&tags
, int type
)
15263 /* Make sure the qMemTags packet is supported. */
15264 if (!m_features
.remote_memory_tagging_p ())
15265 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
15267 struct remote_state
*rs
= get_remote_state ();
15269 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
15274 return parse_fetch_memtags_reply (rs
->buf
, tags
);
15277 /* Implement the "store_memtags" target_ops method. */
15280 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
15281 const gdb::byte_vector
&tags
, int type
)
15283 /* Make sure the QMemTags packet is supported. */
15284 if (!m_features
.remote_memory_tagging_p ())
15285 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
15287 struct remote_state
*rs
= get_remote_state ();
15289 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
15294 /* Verify if the request was successful. */
15295 return packet_check_result (rs
->buf
.data ()) == PACKET_OK
;
15298 /* Return true if remote target T is non-stop. */
15301 remote_target_is_non_stop_p (remote_target
*t
)
15303 scoped_restore_current_thread restore_thread
;
15304 switch_to_target_no_thread (t
);
15306 return target_is_non_stop_p ();
15311 namespace selftests
{
15314 test_memory_tagging_functions ()
15316 remote_target remote
;
15318 struct packet_config
*config
15319 = &remote
.m_features
.m_protocol_packets
[PACKET_memory_tagging_feature
];
15321 scoped_restore restore_memtag_support_
15322 = make_scoped_restore (&config
->support
);
15324 /* Test memory tagging packet support. */
15325 config
->support
= PACKET_SUPPORT_UNKNOWN
;
15326 SELF_CHECK (remote
.supports_memory_tagging () == false);
15327 config
->support
= PACKET_DISABLE
;
15328 SELF_CHECK (remote
.supports_memory_tagging () == false);
15329 config
->support
= PACKET_ENABLE
;
15330 SELF_CHECK (remote
.supports_memory_tagging () == true);
15332 /* Setup testing. */
15333 gdb::char_vector packet
;
15334 gdb::byte_vector tags
, bv
;
15335 std::string expected
, reply
;
15336 packet
.resize (32000);
15338 /* Test creating a qMemTags request. */
15340 expected
= "qMemTags:0,0:0";
15341 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
15342 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
15344 expected
= "qMemTags:deadbeef,10:1";
15345 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
15346 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
15348 /* Test parsing a qMemTags reply. */
15350 /* Error reply, tags vector unmodified. */
15352 strcpy (packet
.data (), reply
.c_str ());
15354 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
15355 SELF_CHECK (tags
.size () == 0);
15357 /* Valid reply, tags vector updated. */
15361 for (int i
= 0; i
< 5; i
++)
15364 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
15365 strcpy (packet
.data (), reply
.c_str ());
15367 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
15368 SELF_CHECK (tags
.size () == 5);
15370 for (int i
= 0; i
< 5; i
++)
15371 SELF_CHECK (tags
[i
] == i
);
15373 /* Test creating a QMemTags request. */
15375 /* Empty tag data. */
15377 expected
= "QMemTags:0,0:0:";
15378 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
15379 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15380 expected
.length ()) == 0);
15382 /* Non-empty tag data. */
15384 for (int i
= 0; i
< 5; i
++)
15385 tags
.push_back (i
);
15386 expected
= "QMemTags:deadbeef,ff:1:0001020304";
15387 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
15388 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
15389 expected
.length ()) == 0);
15392 } // namespace selftests
15393 #endif /* GDB_SELF_TEST */
15395 void _initialize_remote ();
15397 _initialize_remote ()
15399 add_target (remote_target_info
, remote_target::open
);
15400 add_target (extended_remote_target_info
, extended_remote_target::open
);
15402 /* Hook into new objfile notification. */
15403 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
15404 gdb::observers::all_objfiles_removed
.attach
15405 (remote_objfile_changed_check_symbols
, "remote");
15408 init_remote_threadtests ();
15411 /* set/show remote ... */
15413 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
15414 Remote protocol specific variables.\n\
15415 Configure various remote-protocol specific variables such as\n\
15416 the packets being used."),
15417 &remote_set_cmdlist
,
15418 0 /* allow-unknown */, &setlist
);
15419 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
15420 Remote protocol specific variables.\n\
15421 Configure various remote-protocol specific variables such as\n\
15422 the packets being used."),
15423 &remote_show_cmdlist
,
15424 0 /* allow-unknown */, &showlist
);
15426 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
15427 Compare section data on target to the exec file.\n\
15428 Argument is a single section name (default: all loaded sections).\n\
15429 To compare only read-only loaded sections, specify the -r option."),
15432 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
15433 Send an arbitrary packet to a remote target.\n\
15434 maintenance packet TEXT\n\
15435 If GDB is talking to an inferior via the GDB serial protocol, then\n\
15436 this command sends the string TEXT to the inferior, and displays the\n\
15437 response packet. GDB supplies the initial `$' character, and the\n\
15438 terminating `#' character and checksum."),
15441 set_show_commands remotebreak_cmds
15442 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
15443 Set whether to send break if interrupted."), _("\
15444 Show whether to send break if interrupted."), _("\
15445 If set, a break, instead of a cntrl-c, is sent to the remote target."),
15446 set_remotebreak
, show_remotebreak
,
15447 &setlist
, &showlist
);
15448 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
15449 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
15451 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
15452 interrupt_sequence_modes
, &interrupt_sequence_mode
,
15454 Set interrupt sequence to remote target."), _("\
15455 Show interrupt sequence to remote target."), _("\
15456 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
15457 NULL
, show_interrupt_sequence
,
15458 &remote_set_cmdlist
,
15459 &remote_show_cmdlist
);
15461 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
15462 &interrupt_on_connect
, _("\
15463 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15464 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
15465 If set, interrupt sequence is sent to remote target."),
15467 &remote_set_cmdlist
, &remote_show_cmdlist
);
15469 /* Install commands for configuring memory read/write packets. */
15471 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
15472 Set the maximum number of bytes per memory write packet (deprecated)."),
15474 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
15475 Show the maximum number of bytes per memory write packet (deprecated)."),
15477 add_cmd ("memory-write-packet-size", no_class
,
15478 set_memory_write_packet_size
, _("\
15479 Set the maximum number of bytes per memory-write packet.\n\
15480 Specify the number of bytes in a packet or 0 (zero) for the\n\
15481 default packet size. The actual limit is further reduced\n\
15482 dependent on the target. Specify \"fixed\" to disable the\n\
15483 further restriction and \"limit\" to enable that restriction."),
15484 &remote_set_cmdlist
);
15485 add_cmd ("memory-read-packet-size", no_class
,
15486 set_memory_read_packet_size
, _("\
15487 Set the maximum number of bytes per memory-read packet.\n\
15488 Specify the number of bytes in a packet or 0 (zero) for the\n\
15489 default packet size. The actual limit is further reduced\n\
15490 dependent on the target. Specify \"fixed\" to disable the\n\
15491 further restriction and \"limit\" to enable that restriction."),
15492 &remote_set_cmdlist
);
15493 add_cmd ("memory-write-packet-size", no_class
,
15494 show_memory_write_packet_size
,
15495 _("Show the maximum number of bytes per memory-write packet."),
15496 &remote_show_cmdlist
);
15497 add_cmd ("memory-read-packet-size", no_class
,
15498 show_memory_read_packet_size
,
15499 _("Show the maximum number of bytes per memory-read packet."),
15500 &remote_show_cmdlist
);
15502 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
15503 &remote_hw_watchpoint_limit
, _("\
15504 Set the maximum number of target hardware watchpoints."), _("\
15505 Show the maximum number of target hardware watchpoints."), _("\
15506 Specify \"unlimited\" for unlimited hardware watchpoints."),
15507 NULL
, show_hardware_watchpoint_limit
,
15508 &remote_set_cmdlist
,
15509 &remote_show_cmdlist
);
15510 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
15512 &remote_hw_watchpoint_length_limit
, _("\
15513 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
15514 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
15515 Specify \"unlimited\" to allow watchpoints of unlimited size."),
15516 NULL
, show_hardware_watchpoint_length_limit
,
15517 &remote_set_cmdlist
, &remote_show_cmdlist
);
15518 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
15519 &remote_hw_breakpoint_limit
, _("\
15520 Set the maximum number of target hardware breakpoints."), _("\
15521 Show the maximum number of target hardware breakpoints."), _("\
15522 Specify \"unlimited\" for unlimited hardware breakpoints."),
15523 NULL
, show_hardware_breakpoint_limit
,
15524 &remote_set_cmdlist
, &remote_show_cmdlist
);
15526 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
15527 &remote_address_size
, _("\
15528 Set the maximum size of the address (in bits) in a memory packet."), _("\
15529 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
15531 NULL
, /* FIXME: i18n: */
15532 &setlist
, &showlist
);
15534 init_all_packet_configs ();
15536 add_packet_config_cmd (PACKET_X
, "X", "binary-download", 1);
15538 add_packet_config_cmd (PACKET_vCont
, "vCont", "verbose-resume", 0);
15540 add_packet_config_cmd (PACKET_QPassSignals
, "QPassSignals", "pass-signals",
15543 add_packet_config_cmd (PACKET_QCatchSyscalls
, "QCatchSyscalls",
15544 "catch-syscalls", 0);
15546 add_packet_config_cmd (PACKET_QProgramSignals
, "QProgramSignals",
15547 "program-signals", 0);
15549 add_packet_config_cmd (PACKET_QSetWorkingDir
, "QSetWorkingDir",
15550 "set-working-dir", 0);
15552 add_packet_config_cmd (PACKET_QStartupWithShell
, "QStartupWithShell",
15553 "startup-with-shell", 0);
15555 add_packet_config_cmd (PACKET_QEnvironmentHexEncoded
,"QEnvironmentHexEncoded",
15556 "environment-hex-encoded", 0);
15558 add_packet_config_cmd (PACKET_QEnvironmentReset
, "QEnvironmentReset",
15559 "environment-reset", 0);
15561 add_packet_config_cmd (PACKET_QEnvironmentUnset
, "QEnvironmentUnset",
15562 "environment-unset", 0);
15564 add_packet_config_cmd (PACKET_qSymbol
, "qSymbol", "symbol-lookup", 0);
15566 add_packet_config_cmd (PACKET_P
, "P", "set-register", 1);
15568 add_packet_config_cmd (PACKET_p
, "p", "fetch-register", 1);
15570 add_packet_config_cmd (PACKET_Z0
, "Z0", "software-breakpoint", 0);
15572 add_packet_config_cmd (PACKET_Z1
, "Z1", "hardware-breakpoint", 0);
15574 add_packet_config_cmd (PACKET_Z2
, "Z2", "write-watchpoint", 0);
15576 add_packet_config_cmd (PACKET_Z3
, "Z3", "read-watchpoint", 0);
15578 add_packet_config_cmd (PACKET_Z4
, "Z4", "access-watchpoint", 0);
15580 add_packet_config_cmd (PACKET_qXfer_auxv
, "qXfer:auxv:read",
15581 "read-aux-vector", 0);
15583 add_packet_config_cmd (PACKET_qXfer_exec_file
, "qXfer:exec-file:read",
15584 "pid-to-exec-file", 0);
15586 add_packet_config_cmd (PACKET_qXfer_features
,
15587 "qXfer:features:read", "target-features", 0);
15589 add_packet_config_cmd (PACKET_qXfer_libraries
, "qXfer:libraries:read",
15590 "library-info", 0);
15592 add_packet_config_cmd (PACKET_qXfer_libraries_svr4
,
15593 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
15595 add_packet_config_cmd (PACKET_qXfer_memory_map
, "qXfer:memory-map:read",
15598 add_packet_config_cmd (PACKET_qXfer_osdata
, "qXfer:osdata:read", "osdata", 0);
15600 add_packet_config_cmd (PACKET_qXfer_threads
, "qXfer:threads:read", "threads",
15603 add_packet_config_cmd (PACKET_qXfer_siginfo_read
, "qXfer:siginfo:read",
15604 "read-siginfo-object", 0);
15606 add_packet_config_cmd (PACKET_qXfer_siginfo_write
, "qXfer:siginfo:write",
15607 "write-siginfo-object", 0);
15609 add_packet_config_cmd (PACKET_qXfer_traceframe_info
,
15610 "qXfer:traceframe-info:read", "traceframe-info", 0);
15612 add_packet_config_cmd (PACKET_qXfer_uib
, "qXfer:uib:read",
15613 "unwind-info-block", 0);
15615 add_packet_config_cmd (PACKET_qGetTLSAddr
, "qGetTLSAddr",
15616 "get-thread-local-storage-address", 0);
15618 add_packet_config_cmd (PACKET_qGetTIBAddr
, "qGetTIBAddr",
15619 "get-thread-information-block-address", 0);
15621 add_packet_config_cmd (PACKET_bc
, "bc", "reverse-continue", 0);
15623 add_packet_config_cmd (PACKET_bs
, "bs", "reverse-step", 0);
15625 add_packet_config_cmd (PACKET_qSupported
, "qSupported", "supported-packets",
15628 add_packet_config_cmd (PACKET_qSearch_memory
, "qSearch:memory",
15629 "search-memory", 0);
15631 add_packet_config_cmd (PACKET_qTStatus
, "qTStatus", "trace-status", 0);
15633 add_packet_config_cmd (PACKET_vFile_setfs
, "vFile:setfs", "hostio-setfs", 0);
15635 add_packet_config_cmd (PACKET_vFile_open
, "vFile:open", "hostio-open", 0);
15637 add_packet_config_cmd (PACKET_vFile_pread
, "vFile:pread", "hostio-pread", 0);
15639 add_packet_config_cmd (PACKET_vFile_pwrite
, "vFile:pwrite", "hostio-pwrite",
15642 add_packet_config_cmd (PACKET_vFile_close
, "vFile:close", "hostio-close", 0);
15644 add_packet_config_cmd (PACKET_vFile_unlink
, "vFile:unlink", "hostio-unlink",
15647 add_packet_config_cmd (PACKET_vFile_readlink
, "vFile:readlink",
15648 "hostio-readlink", 0);
15650 add_packet_config_cmd (PACKET_vFile_fstat
, "vFile:fstat", "hostio-fstat", 0);
15652 add_packet_config_cmd (PACKET_vAttach
, "vAttach", "attach", 0);
15654 add_packet_config_cmd (PACKET_vRun
, "vRun", "run", 0);
15656 add_packet_config_cmd (PACKET_QStartNoAckMode
, "QStartNoAckMode", "noack", 0);
15658 add_packet_config_cmd (PACKET_vKill
, "vKill", "kill", 0);
15660 add_packet_config_cmd (PACKET_qAttached
, "qAttached", "query-attached", 0);
15662 add_packet_config_cmd (PACKET_ConditionalTracepoints
,
15663 "ConditionalTracepoints", "conditional-tracepoints",
15666 add_packet_config_cmd (PACKET_ConditionalBreakpoints
,
15667 "ConditionalBreakpoints", "conditional-breakpoints",
15670 add_packet_config_cmd (PACKET_BreakpointCommands
, "BreakpointCommands",
15671 "breakpoint-commands", 0);
15673 add_packet_config_cmd (PACKET_FastTracepoints
, "FastTracepoints",
15674 "fast-tracepoints", 0);
15676 add_packet_config_cmd (PACKET_TracepointSource
, "TracepointSource",
15677 "TracepointSource", 0);
15679 add_packet_config_cmd (PACKET_QAllow
, "QAllow", "allow", 0);
15681 add_packet_config_cmd (PACKET_StaticTracepoints
, "StaticTracepoints",
15682 "static-tracepoints", 0);
15684 add_packet_config_cmd (PACKET_InstallInTrace
, "InstallInTrace",
15685 "install-in-trace", 0);
15687 add_packet_config_cmd (PACKET_qXfer_statictrace_read
,
15688 "qXfer:statictrace:read", "read-sdata-object", 0);
15690 add_packet_config_cmd (PACKET_qXfer_fdpic
, "qXfer:fdpic:read",
15691 "read-fdpic-loadmap", 0);
15693 add_packet_config_cmd (PACKET_QDisableRandomization
, "QDisableRandomization",
15694 "disable-randomization", 0);
15696 add_packet_config_cmd (PACKET_QAgent
, "QAgent", "agent", 0);
15698 add_packet_config_cmd (PACKET_QTBuffer_size
, "QTBuffer:size",
15699 "trace-buffer-size", 0);
15701 add_packet_config_cmd (PACKET_Qbtrace_off
, "Qbtrace:off", "disable-btrace",
15704 add_packet_config_cmd (PACKET_Qbtrace_bts
, "Qbtrace:bts", "enable-btrace-bts",
15707 add_packet_config_cmd (PACKET_Qbtrace_pt
, "Qbtrace:pt", "enable-btrace-pt",
15710 add_packet_config_cmd (PACKET_qXfer_btrace
, "qXfer:btrace", "read-btrace", 0);
15712 add_packet_config_cmd (PACKET_qXfer_btrace_conf
, "qXfer:btrace-conf",
15713 "read-btrace-conf", 0);
15715 add_packet_config_cmd (PACKET_Qbtrace_conf_bts_size
, "Qbtrace-conf:bts:size",
15716 "btrace-conf-bts-size", 0);
15718 add_packet_config_cmd (PACKET_multiprocess_feature
, "multiprocess-feature",
15719 "multiprocess-feature", 0);
15721 add_packet_config_cmd (PACKET_swbreak_feature
, "swbreak-feature",
15722 "swbreak-feature", 0);
15724 add_packet_config_cmd (PACKET_hwbreak_feature
, "hwbreak-feature",
15725 "hwbreak-feature", 0);
15727 add_packet_config_cmd (PACKET_fork_event_feature
, "fork-event-feature",
15728 "fork-event-feature", 0);
15730 add_packet_config_cmd (PACKET_vfork_event_feature
, "vfork-event-feature",
15731 "vfork-event-feature", 0);
15733 add_packet_config_cmd (PACKET_Qbtrace_conf_pt_size
, "Qbtrace-conf:pt:size",
15734 "btrace-conf-pt-size", 0);
15736 add_packet_config_cmd (PACKET_vContSupported
, "vContSupported",
15737 "verbose-resume-supported", 0);
15739 add_packet_config_cmd (PACKET_exec_event_feature
, "exec-event-feature",
15740 "exec-event-feature", 0);
15742 add_packet_config_cmd (PACKET_vCtrlC
, "vCtrlC", "ctrl-c", 0);
15744 add_packet_config_cmd (PACKET_QThreadEvents
, "QThreadEvents", "thread-events",
15747 add_packet_config_cmd (PACKET_no_resumed
, "N stop reply",
15748 "no-resumed-stop-reply", 0);
15750 add_packet_config_cmd (PACKET_memory_tagging_feature
,
15751 "memory-tagging-feature", "memory-tagging-feature", 0);
15753 /* Assert that we've registered "set remote foo-packet" commands
15754 for all packet configs. */
15758 for (i
= 0; i
< PACKET_MAX
; i
++)
15760 /* Ideally all configs would have a command associated. Some
15761 still don't though. */
15766 case PACKET_QNonStop
:
15767 case PACKET_EnableDisableTracepoints_feature
:
15768 case PACKET_tracenz_feature
:
15769 case PACKET_DisconnectedTracing_feature
:
15770 case PACKET_augmented_libraries_svr4_read_feature
:
15772 /* Additions to this list need to be well justified:
15773 pre-existing packets are OK; new packets are not. */
15781 /* This catches both forgetting to add a config command, and
15782 forgetting to remove a packet from the exception list. */
15783 gdb_assert (excepted
== (packets_descriptions
[i
].name
== NULL
));
15787 /* Keep the old ``set remote Z-packet ...'' working. Each individual
15788 Z sub-packet has its own set and show commands, but users may
15789 have sets to this variable in their .gdbinit files (or in their
15791 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
15792 &remote_Z_packet_detect
, _("\
15793 Set use of remote protocol `Z' packets."), _("\
15794 Show use of remote protocol `Z' packets."), _("\
15795 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
15797 set_remote_protocol_Z_packet_cmd
,
15798 show_remote_protocol_Z_packet_cmd
,
15799 /* FIXME: i18n: Use of remote protocol
15800 `Z' packets is %s. */
15801 &remote_set_cmdlist
, &remote_show_cmdlist
);
15803 add_basic_prefix_cmd ("remote", class_files
, _("\
15804 Manipulate files on the remote system.\n\
15805 Transfer files to and from the remote target system."),
15807 0 /* allow-unknown */, &cmdlist
);
15809 add_cmd ("put", class_files
, remote_put_command
,
15810 _("Copy a local file to the remote system."),
15813 add_cmd ("get", class_files
, remote_get_command
,
15814 _("Copy a remote file to the local system."),
15817 add_cmd ("delete", class_files
, remote_delete_command
,
15818 _("Delete a remote file."),
15821 add_setshow_string_noescape_cmd ("exec-file", class_files
,
15822 &remote_exec_file_var
, _("\
15823 Set the remote pathname for \"run\"."), _("\
15824 Show the remote pathname for \"run\"."), NULL
,
15825 set_remote_exec_file
,
15826 show_remote_exec_file
,
15827 &remote_set_cmdlist
,
15828 &remote_show_cmdlist
);
15830 add_setshow_boolean_cmd ("range-stepping", class_run
,
15831 &use_range_stepping
, _("\
15832 Enable or disable range stepping."), _("\
15833 Show whether target-assisted range stepping is enabled."), _("\
15834 If on, and the target supports it, when stepping a source line, GDB\n\
15835 tells the target to step the corresponding range of addresses itself instead\n\
15836 of issuing multiple single-steps. This speeds up source level\n\
15837 stepping. If off, GDB always issues single-steps, even if range\n\
15838 stepping is supported by the target. The default is on."),
15839 set_range_stepping
,
15840 show_range_stepping
,
15844 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
15845 Set watchdog timer."), _("\
15846 Show watchdog timer."), _("\
15847 When non-zero, this timeout is used instead of waiting forever for a target\n\
15848 to finish a low-level step or continue operation. If the specified amount\n\
15849 of time passes without a response from the target, an error occurs."),
15852 &setlist
, &showlist
);
15854 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
15855 &remote_packet_max_chars
, _("\
15856 Set the maximum number of characters to display for each remote packet."), _("\
15857 Show the maximum number of characters to display for each remote packet."), _("\
15858 Specify \"unlimited\" to display all the characters."),
15859 NULL
, show_remote_packet_max_chars
,
15860 &setdebuglist
, &showdebuglist
);
15862 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
15863 _("Set debugging of remote protocol."),
15864 _("Show debugging of remote protocol."),
15866 When enabled, each packet sent or received with the remote target\n\
15870 &setdebuglist
, &showdebuglist
);
15872 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
15873 &remote_timeout
, _("\
15874 Set timeout limit to wait for target to respond."), _("\
15875 Show timeout limit to wait for target to respond."), _("\
15876 This value is used to set the time limit for gdb to wait for a response\n\
15877 from the target."),
15879 show_remote_timeout
,
15880 &setlist
, &showlist
);
15882 /* Eventually initialize fileio. See fileio.c */
15883 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
15886 selftests::register_test ("remote_memory_tagging",
15887 selftests::test_memory_tagging_functions
);