+ bool use_threadinfo_query = false;
+ bool use_threadextra_query = false;
+
+ threadref echo_nextthread {};
+ threadref nextthread {};
+ threadref resultthreadlist[MAXTHREADLISTRESULTS] {};
+
+ /* The state of remote notification. */
+ struct remote_notif_state *notif_state = nullptr;
+
+ /* The branch trace configuration. */
+ struct btrace_config btrace_config {};
+
+ /* The argument to the last "vFile:setfs:" packet we sent, used
+ to avoid sending repeated unnecessary "vFile:setfs:" packets.
+ Initialized to -1 to indicate that no "vFile:setfs:" packet
+ has yet been sent. */
+ int fs_pid = -1;
+
+ /* A readahead cache for vFile:pread. Often, reading a binary
+ involves a sequence of small reads. E.g., when parsing an ELF
+ file. A readahead cache helps mostly the case of remote
+ debugging on a connection with higher latency, due to the
+ request/reply nature of the RSP. We only cache data for a single
+ file descriptor at a time. */
+ struct readahead_cache readahead_cache;
+
+ /* The list of already fetched and acknowledged stop events. This
+ queue is used for notification Stop, and other notifications
+ don't need queue for their events, because the notification
+ events of Stop can't be consumed immediately, so that events
+ should be queued first, and be consumed by remote_wait_{ns,as}
+ one per time. Other notifications can consume their events
+ immediately, so queue is not needed for them. */
+ std::vector<stop_reply_up> stop_reply_queue;
+
+ /* Asynchronous signal handle registered as event loop source for
+ when we have pending events ready to be passed to the core. */
+ struct async_event_handler *remote_async_inferior_event_token = nullptr;
+
+ /* FIXME: cagney/1999-09-23: Even though getpkt was called with
+ ``forever'' still use the normal timeout mechanism. This is
+ currently used by the ASYNC code to guarentee that target reads
+ during the initial connect always time-out. Once getpkt has been
+ modified to return a timeout indication and, in turn
+ remote_wait()/wait_for_inferior() have gained a timeout parameter
+ this can go away. */
+ int wait_forever_enabled_p = 1;
+
+private:
+ /* Mapping of remote protocol data for each gdbarch. Usually there
+ is only one entry here, though we may see more with stubs that
+ support multi-process. */
+ std::unordered_map<struct gdbarch *, remote_arch_state>
+ m_arch_states;
+};
+
+static const target_info remote_target_info = {
+ "remote",
+ N_("Remote serial target in gdb-specific protocol"),
+ remote_doc
+};
+
+class remote_target : public process_stratum_target
+{
+public:
+ remote_target () = default;
+ ~remote_target () override;
+
+ const target_info &info () const override
+ { return remote_target_info; }
+
+ thread_control_capabilities get_thread_control_capabilities () override
+ { return tc_schedlock; }
+
+ /* Open a remote connection. */
+ static void open (const char *, int);
+
+ void close () override;
+
+ void detach (inferior *, int) override;
+ void disconnect (const char *, int) override;
+
+ void commit_resume () override;
+ void resume (ptid_t, int, enum gdb_signal) override;
+ ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
+
+ void fetch_registers (struct regcache *, int) override;
+ void store_registers (struct regcache *, int) override;
+ void prepare_to_store (struct regcache *) override;
+
+ void files_info () override;
+
+ int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
+
+ int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
+ enum remove_bp_reason) override;
+
+
+ bool stopped_by_sw_breakpoint () override;
+ bool supports_stopped_by_sw_breakpoint () override;
+
+ bool stopped_by_hw_breakpoint () override;
+
+ bool supports_stopped_by_hw_breakpoint () override;
+
+ bool stopped_by_watchpoint () override;
+
+ bool stopped_data_address (CORE_ADDR *) override;
+
+ bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
+
+ int can_use_hw_breakpoint (enum bptype, int, int) override;
+
+ int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
+
+ int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
+
+ int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
+
+ int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
+ struct expression *) override;
+
+ int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
+ struct expression *) override;
+
+ void kill () override;
+
+ void load (const char *, int) override;
+
+ void mourn_inferior () override;
+
+ void pass_signals (gdb::array_view<const unsigned char>) override;
+
+ int set_syscall_catchpoint (int, bool, int,
+ gdb::array_view<const int>) override;
+
+ void program_signals (gdb::array_view<const unsigned char>) override;
+
+ bool thread_alive (ptid_t ptid) override;
+
+ const char *thread_name (struct thread_info *) override;
+
+ void update_thread_list () override;
+
+ std::string pid_to_str (ptid_t) override;
+
+ const char *extra_thread_info (struct thread_info *) override;
+
+ ptid_t get_ada_task_ptid (long lwp, long thread) override;
+
+ thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle,
+ int handle_len,
+ inferior *inf) override;
+
+ gdb::byte_vector thread_info_to_thread_handle (struct thread_info *tp)
+ override;
+
+ void stop (ptid_t) override;
+
+ void interrupt () override;
+
+ void pass_ctrlc () override;
+
+ enum target_xfer_status xfer_partial (enum target_object object,
+ const char *annex,
+ gdb_byte *readbuf,
+ const gdb_byte *writebuf,
+ ULONGEST offset, ULONGEST len,
+ ULONGEST *xfered_len) override;
+
+ ULONGEST get_memory_xfer_limit () override;
+
+ void rcmd (const char *command, struct ui_file *output) override;
+
+ char *pid_to_exec_file (int pid) override;
+
+ void log_command (const char *cmd) override
+ {
+ serial_log_command (this, cmd);
+ }
+
+ CORE_ADDR get_thread_local_address (ptid_t ptid,
+ CORE_ADDR load_module_addr,
+ CORE_ADDR offset) override;
+
+ bool can_execute_reverse () override;
+
+ std::vector<mem_region> memory_map () override;
+
+ void flash_erase (ULONGEST address, LONGEST length) override;
+
+ void flash_done () override;
+
+ const struct target_desc *read_description () override;
+
+ int search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
+ const gdb_byte *pattern, ULONGEST pattern_len,
+ CORE_ADDR *found_addrp) override;
+
+ bool can_async_p () override;
+
+ bool is_async_p () override;
+
+ void async (int) override;
+
+ void thread_events (int) override;
+
+ int can_do_single_step () override;
+
+ void terminal_inferior () override;
+
+ void terminal_ours () override;
+
+ bool supports_non_stop () override;
+
+ bool supports_multi_process () override;
+
+ bool supports_disable_randomization () override;
+
+ bool filesystem_is_local () override;
+
+
+ int fileio_open (struct inferior *inf, const char *filename,
+ int flags, int mode, int warn_if_slow,
+ int *target_errno) override;
+
+ int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
+ ULONGEST offset, int *target_errno) override;
+
+ int fileio_pread (int fd, gdb_byte *read_buf, int len,
+ ULONGEST offset, int *target_errno) override;
+
+ int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
+
+ int fileio_close (int fd, int *target_errno) override;
+
+ int fileio_unlink (struct inferior *inf,
+ const char *filename,
+ int *target_errno) override;
+
+ gdb::optional<std::string>
+ fileio_readlink (struct inferior *inf,
+ const char *filename,
+ int *target_errno) override;
+
+ bool supports_enable_disable_tracepoint () override;
+
+ bool supports_string_tracing () override;
+
+ bool supports_evaluation_of_breakpoint_conditions () override;
+
+ bool can_run_breakpoint_commands () override;
+
+ void trace_init () override;
+
+ void download_tracepoint (struct bp_location *location) override;
+
+ bool can_download_tracepoint () override;
+
+ void download_trace_state_variable (const trace_state_variable &tsv) override;
+
+ void enable_tracepoint (struct bp_location *location) override;
+
+ void disable_tracepoint (struct bp_location *location) override;
+
+ void trace_set_readonly_regions () override;
+
+ void trace_start () override;
+
+ int get_trace_status (struct trace_status *ts) override;
+
+ void get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
+ override;
+
+ void trace_stop () override;
+
+ int trace_find (enum trace_find_type type, int num,
+ CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
+
+ bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
+
+ int save_trace_data (const char *filename) override;
+
+ int upload_tracepoints (struct uploaded_tp **utpp) override;
+
+ int upload_trace_state_variables (struct uploaded_tsv **utsvp) override;
+
+ LONGEST get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) override;
+
+ int get_min_fast_tracepoint_insn_len () override;
+
+ void set_disconnected_tracing (int val) override;
+
+ void set_circular_trace_buffer (int val) override;
+
+ void set_trace_buffer_size (LONGEST val) override;
+
+ bool set_trace_notes (const char *user, const char *notes,
+ const char *stopnotes) override;
+
+ int core_of_thread (ptid_t ptid) override;
+
+ int verify_memory (const gdb_byte *data,
+ CORE_ADDR memaddr, ULONGEST size) override;
+
+
+ bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override;
+
+ void set_permissions () override;
+
+ bool static_tracepoint_marker_at (CORE_ADDR,
+ struct static_tracepoint_marker *marker)
+ override;
+
+ std::vector<static_tracepoint_marker>
+ static_tracepoint_markers_by_strid (const char *id) override;
+
+ traceframe_info_up traceframe_info () override;
+
+ bool use_agent (bool use) override;
+ bool can_use_agent () override;
+
+ struct btrace_target_info *enable_btrace (ptid_t ptid,
+ const struct btrace_config *conf) override;
+
+ void disable_btrace (struct btrace_target_info *tinfo) override;
+
+ void teardown_btrace (struct btrace_target_info *tinfo) override;
+
+ enum btrace_error read_btrace (struct btrace_data *data,
+ struct btrace_target_info *btinfo,
+ enum btrace_read_type type) override;
+
+ const struct btrace_config *btrace_conf (const struct btrace_target_info *) override;
+ bool augmented_libraries_svr4_read () override;
+ int follow_fork (int, int) override;
+ void follow_exec (struct inferior *, const char *) override;
+ int insert_fork_catchpoint (int) override;
+ int remove_fork_catchpoint (int) override;
+ int insert_vfork_catchpoint (int) override;
+ int remove_vfork_catchpoint (int) override;
+ int insert_exec_catchpoint (int) override;
+ int remove_exec_catchpoint (int) override;
+ enum exec_direction_kind execution_direction () override;
+
+public: /* Remote specific methods. */
+
+ void remote_download_command_source (int num, ULONGEST addr,
+ struct command_line *cmds);
+
+ void remote_file_put (const char *local_file, const char *remote_file,
+ int from_tty);
+ void remote_file_get (const char *remote_file, const char *local_file,
+ int from_tty);
+ void remote_file_delete (const char *remote_file, int from_tty);
+
+ int remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
+ ULONGEST offset, int *remote_errno);
+ int remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
+ ULONGEST offset, int *remote_errno);
+ int remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
+ ULONGEST offset, int *remote_errno);
+
+ int remote_hostio_send_command (int command_bytes, int which_packet,
+ int *remote_errno, char **attachment,
+ int *attachment_len);
+ int remote_hostio_set_filesystem (struct inferior *inf,
+ int *remote_errno);
+ /* We should get rid of this and use fileio_open directly. */
+ int remote_hostio_open (struct inferior *inf, const char *filename,
+ int flags, int mode, int warn_if_slow,
+ int *remote_errno);
+ int remote_hostio_close (int fd, int *remote_errno);
+
+ int remote_hostio_unlink (inferior *inf, const char *filename,
+ int *remote_errno);
+
+ struct remote_state *get_remote_state ();
+
+ long get_remote_packet_size (void);
+ long get_memory_packet_size (struct memory_packet_config *config);
+
+ long get_memory_write_packet_size ();
+ long get_memory_read_packet_size ();
+
+ char *append_pending_thread_resumptions (char *p, char *endp,
+ ptid_t ptid);
+ static void open_1 (const char *name, int from_tty, int extended_p);
+ void start_remote (int from_tty, int extended_p);
+ void remote_detach_1 (struct inferior *inf, int from_tty);
+
+ char *append_resumption (char *p, char *endp,
+ ptid_t ptid, int step, gdb_signal siggnal);
+ int remote_resume_with_vcont (ptid_t ptid, int step,
+ gdb_signal siggnal);
+
+ void add_current_inferior_and_thread (char *wait_status);
+
+ ptid_t wait_ns (ptid_t ptid, struct target_waitstatus *status,
+ int options);
+ ptid_t wait_as (ptid_t ptid, target_waitstatus *status,
+ int options);
+
+ ptid_t process_stop_reply (struct stop_reply *stop_reply,
+ target_waitstatus *status);
+
+ void remote_notice_new_inferior (ptid_t currthread, int executing);
+
+ void process_initial_stop_replies (int from_tty);
+
+ thread_info *remote_add_thread (ptid_t ptid, bool running, bool executing);
+
+ void btrace_sync_conf (const btrace_config *conf);
+
+ void remote_btrace_maybe_reopen ();
+
+ void remove_new_fork_children (threads_listing_context *context);
+ void kill_new_fork_children (int pid);
+ void discard_pending_stop_replies (struct inferior *inf);
+ int stop_reply_queue_length ();
+
+ void check_pending_events_prevent_wildcard_vcont
+ (int *may_global_wildcard_vcont);
+
+ void discard_pending_stop_replies_in_queue ();
+ struct stop_reply *remote_notif_remove_queued_reply (ptid_t ptid);
+ struct stop_reply *queued_stop_reply (ptid_t ptid);
+ int peek_stop_reply (ptid_t ptid);
+ void remote_parse_stop_reply (const char *buf, stop_reply *event);
+
+ void remote_stop_ns (ptid_t ptid);
+ void remote_interrupt_as ();
+ void remote_interrupt_ns ();
+
+ char *remote_get_noisy_reply ();
+ int remote_query_attached (int pid);
+ inferior *remote_add_inferior (bool fake_pid_p, int pid, int attached,
+ int try_open_exec);
+
+ ptid_t remote_current_thread (ptid_t oldpid);
+ ptid_t get_current_thread (char *wait_status);
+
+ void set_thread (ptid_t ptid, int gen);
+ void set_general_thread (ptid_t ptid);
+ void set_continue_thread (ptid_t ptid);
+ void set_general_process ();
+
+ char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
+
+ int remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
+ gdb_ext_thread_info *info);
+ int remote_get_threadinfo (threadref *threadid, int fieldset,
+ gdb_ext_thread_info *info);
+
+ int parse_threadlist_response (char *pkt, int result_limit,
+ threadref *original_echo,
+ threadref *resultlist,
+ int *doneflag);
+ int remote_get_threadlist (int startflag, threadref *nextthread,
+ int result_limit, int *done, int *result_count,
+ threadref *threadlist);
+
+ int remote_threadlist_iterator (rmt_thread_action stepfunction,
+ void *context, int looplimit);
+
+ int remote_get_threads_with_ql (threads_listing_context *context);
+ int remote_get_threads_with_qxfer (threads_listing_context *context);
+ int remote_get_threads_with_qthreadinfo (threads_listing_context *context);
+
+ void extended_remote_restart ();
+
+ void get_offsets ();
+
+ void remote_check_symbols ();
+
+ void remote_supported_packet (const struct protocol_feature *feature,
+ enum packet_support support,
+ const char *argument);
+
+ void remote_query_supported ();
+
+ void remote_packet_size (const protocol_feature *feature,
+ packet_support support, const char *value);
+
+ void remote_serial_quit_handler ();
+
+ void remote_detach_pid (int pid);
+
+ void remote_vcont_probe ();
+
+ void remote_resume_with_hc (ptid_t ptid, int step,
+ gdb_signal siggnal);
+
+ void send_interrupt_sequence ();
+ void interrupt_query ();
+
+ void remote_notif_get_pending_events (notif_client *nc);
+
+ int fetch_register_using_p (struct regcache *regcache,
+ packet_reg *reg);
+ int send_g_packet ();
+ void process_g_packet (struct regcache *regcache);
+ void fetch_registers_using_g (struct regcache *regcache);
+ int store_register_using_P (const struct regcache *regcache,
+ packet_reg *reg);
+ void store_registers_using_G (const struct regcache *regcache);
+
+ void set_remote_traceframe ();
+
+ void check_binary_download (CORE_ADDR addr);
+
+ target_xfer_status remote_write_bytes_aux (const char *header,
+ CORE_ADDR memaddr,
+ const gdb_byte *myaddr,
+ ULONGEST len_units,
+ int unit_size,
+ ULONGEST *xfered_len_units,
+ char packet_format,
+ int use_length);
+
+ target_xfer_status remote_write_bytes (CORE_ADDR memaddr,
+ const gdb_byte *myaddr, ULONGEST len,
+ int unit_size, ULONGEST *xfered_len);
+
+ target_xfer_status remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr,
+ ULONGEST len_units,
+ int unit_size, ULONGEST *xfered_len_units);
+
+ target_xfer_status remote_xfer_live_readonly_partial (gdb_byte *readbuf,
+ ULONGEST memaddr,
+ ULONGEST len,
+ int unit_size,
+ ULONGEST *xfered_len);
+
+ target_xfer_status remote_read_bytes (CORE_ADDR memaddr,
+ gdb_byte *myaddr, ULONGEST len,
+ int unit_size,
+ ULONGEST *xfered_len);
+
+ packet_result remote_send_printf (const char *format, ...)
+ ATTRIBUTE_PRINTF (2, 3);
+
+ target_xfer_status remote_flash_write (ULONGEST address,
+ ULONGEST length, ULONGEST *xfered_len,
+ const gdb_byte *data);
+
+ int readchar (int timeout);
+
+ void remote_serial_write (const char *str, int len);
+
+ int putpkt (const char *buf);
+ int putpkt_binary (const char *buf, int cnt);
+
+ int putpkt (const gdb::char_vector &buf)
+ {
+ return putpkt (buf.data ());
+ }
+
+ void skip_frame ();
+ long read_frame (gdb::char_vector *buf_p);
+ void getpkt (gdb::char_vector *buf, int forever);
+ int getpkt_or_notif_sane_1 (gdb::char_vector *buf, int forever,
+ int expecting_notif, int *is_notif);
+ int getpkt_sane (gdb::char_vector *buf, int forever);
+ int getpkt_or_notif_sane (gdb::char_vector *buf, int forever,
+ int *is_notif);
+ int remote_vkill (int pid);
+ void remote_kill_k ();
+
+ void extended_remote_disable_randomization (int val);
+ int extended_remote_run (const std::string &args);
+
+ void send_environment_packet (const char *action,
+ const char *packet,
+ const char *value);
+
+ void extended_remote_environment_support ();
+ void extended_remote_set_inferior_cwd ();
+
+ target_xfer_status remote_write_qxfer (const char *object_name,
+ const char *annex,
+ const gdb_byte *writebuf,
+ ULONGEST offset, LONGEST len,
+ ULONGEST *xfered_len,
+ struct packet_config *packet);
+
+ target_xfer_status remote_read_qxfer (const char *object_name,
+ const char *annex,
+ gdb_byte *readbuf, ULONGEST offset,
+ LONGEST len,
+ ULONGEST *xfered_len,
+ struct packet_config *packet);
+
+ void push_stop_reply (struct stop_reply *new_event);
+
+ bool vcont_r_supported ();
+
+ void packet_command (const char *args, int from_tty);
+
+private: /* data fields */
+
+ /* The remote state. Don't reference this directly. Use the
+ get_remote_state method instead. */
+ remote_state m_remote_state;
+};
+
+static const target_info extended_remote_target_info = {
+ "extended-remote",
+ N_("Extended remote serial target in gdb-specific protocol"),
+ remote_doc
+};
+
+/* Set up the extended remote target by extending the standard remote
+ target and adding to it. */
+
+class extended_remote_target final : public remote_target
+{
+public:
+ const target_info &info () const override
+ { return extended_remote_target_info; }
+
+ /* Open an extended-remote connection. */
+ static void open (const char *, int);
+
+ bool can_create_inferior () override { return true; }
+ void create_inferior (const char *, const std::string &,
+ char **, int) override;
+
+ void detach (inferior *, int) override;
+
+ bool can_attach () override { return true; }
+ void attach (const char *, int) override;
+
+ void post_attach (int) override;
+ bool supports_disable_randomization () override;
+};
+
+/* Per-program-space data key. */
+static const struct program_space_key<char, gdb::xfree_deleter<char>>
+ remote_pspace_data;
+
+/* The variable registered as the control variable used by the
+ remote exec-file commands. While the remote exec-file setting is
+ per-program-space, the set/show machinery uses this as the
+ location of the remote exec-file value. */
+static char *remote_exec_file_var;
+
+/* The size to align memory write packets, when practical. The protocol
+ does not guarantee any alignment, and gdb will generate short
+ writes and unaligned writes, but even as a best-effort attempt this
+ can improve bulk transfers. For instance, if a write is misaligned
+ relative to the target's data bus, the stub may need to make an extra
+ round trip fetching data from the target. This doesn't make a
+ huge difference, but it's easy to do, so we try to be helpful.
+
+ The alignment chosen is arbitrary; usually data bus width is
+ important here, not the possibly larger cache line size. */
+enum { REMOTE_ALIGN_WRITES = 16 };
+
+/* Prototypes for local functions. */
+
+static int hexnumlen (ULONGEST num);
+
+static int stubhex (int ch);
+
+static int hexnumstr (char *, ULONGEST);
+
+static int hexnumnstr (char *, ULONGEST, int);
+
+static CORE_ADDR remote_address_masked (CORE_ADDR);
+
+static void print_packet (const char *);
+
+static int stub_unpack_int (char *buff, int fieldlength);
+
+struct packet_config;
+
+static void show_packet_config_cmd (struct packet_config *config);
+
+static void show_remote_protocol_packet_cmd (struct ui_file *file,
+ int from_tty,
+ struct cmd_list_element *c,
+ const char *value);
+
+static ptid_t read_ptid (const char *buf, const char **obuf);
+
+static void remote_async_inferior_event_handler (gdb_client_data);
+
+static bool remote_read_description_p (struct target_ops *target);
+
+static void remote_console_output (const char *msg);
+
+static void remote_btrace_reset (remote_state *rs);
+
+static void remote_unpush_and_throw (void);
+
+/* For "remote". */