1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2022 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 "gdb-stabs.h"
34 #include "gdbthread.h"
36 #include "remote-notif.h"
39 #include "observable.h"
41 #include "cli/cli-decode.h"
42 #include "cli/cli-setshow.h"
43 #include "target-descriptions.h"
45 #include "gdbsupport/filestuff.h"
46 #include "gdbsupport/rsp-low.h"
50 #include "gdbsupport/gdb_sys_time.h"
52 #include "gdbsupport/event-loop.h"
53 #include "event-top.h"
61 #include "remote-fileio.h"
62 #include "gdbsupport/fileio.h"
64 #include "xml-support.h"
66 #include "memory-map.h"
68 #include "tracepoint.h"
71 #include "gdbsupport/agent.h"
73 #include "record-btrace.h"
75 #include "gdbsupport/scoped_restore.h"
76 #include "gdbsupport/environ.h"
77 #include "gdbsupport/byte-vector.h"
78 #include "gdbsupport/search.h"
80 #include <unordered_map>
81 #include "async-event.h"
82 #include "gdbsupport/selftest.h"
84 /* The remote target. */
86 static const char remote_doc
[] = N_("\
87 Use a remote computer via a serial line, using a gdb-specific protocol.\n\
88 Specify the serial device it is connected to\n\
89 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
93 bool remote_debug
= false;
95 #define OPAQUETHREADBYTES 8
97 /* a 64 bit opaque identifier */
98 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
100 struct gdb_ext_thread_info
;
101 struct threads_listing_context
;
102 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
103 struct protocol_feature
;
107 typedef std::unique_ptr
<stop_reply
> stop_reply_up
;
109 /* Generic configuration support for packets the stub optionally
110 supports. Allows the user to specify the use of the packet as well
111 as allowing GDB to auto-detect support in the remote stub. */
115 PACKET_SUPPORT_UNKNOWN
= 0,
120 /* Analyze a packet's return value and update the packet config
130 struct threads_listing_context
;
132 /* Stub vCont actions support.
134 Each field is a boolean flag indicating whether the stub reports
135 support for the corresponding action. */
137 struct vCont_action_support
152 /* About this many threadids fit in a packet. */
154 #define MAXTHREADLISTRESULTS 32
156 /* Data for the vFile:pread readahead cache. */
158 struct readahead_cache
160 /* Invalidate the readahead cache. */
163 /* Invalidate the readahead cache if it is holding data for FD. */
164 void invalidate_fd (int fd
);
166 /* Serve pread from the readahead cache. Returns number of bytes
167 read, or 0 if the request can't be served from the cache. */
168 int pread (int fd
, gdb_byte
*read_buf
, size_t len
, ULONGEST offset
);
170 /* The file descriptor for the file that is being cached. -1 if the
174 /* The offset into the file that the cache buffer corresponds
178 /* The buffer holding the cache contents. */
179 gdb_byte
*buf
= nullptr;
180 /* The buffer's size. We try to read as much as fits into a packet
184 /* Cache hit and miss counters. */
185 ULONGEST hit_count
= 0;
186 ULONGEST miss_count
= 0;
189 /* Description of the remote protocol for a given architecture. */
193 long offset
; /* Offset into G packet. */
194 long regnum
; /* GDB's internal register number. */
195 LONGEST pnum
; /* Remote protocol register number. */
196 int in_g_packet
; /* Always part of G packet. */
197 /* long size in bytes; == register_size (target_gdbarch (), regnum);
199 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
203 struct remote_arch_state
205 explicit remote_arch_state (struct gdbarch
*gdbarch
);
207 /* Description of the remote protocol registers. */
208 long sizeof_g_packet
;
210 /* Description of the remote protocol registers indexed by REGNUM
211 (making an array gdbarch_num_regs in size). */
212 std::unique_ptr
<packet_reg
[]> regs
;
214 /* This is the size (in chars) of the first response to the ``g''
215 packet. It is used as a heuristic when determining the maximum
216 size of memory-read and memory-write packets. A target will
217 typically only reserve a buffer large enough to hold the ``g''
218 packet. The size does not include packet overhead (headers and
220 long actual_register_packet_size
;
222 /* This is the maximum size (in chars) of a non read/write packet.
223 It is also used as a cap on the size of read/write packets. */
224 long remote_packet_size
;
227 /* Description of the remote protocol state for the currently
228 connected target. This is per-target state, and independent of the
229 selected architecture. */
238 /* Get the remote arch state for GDBARCH. */
239 struct remote_arch_state
*get_remote_arch_state (struct gdbarch
*gdbarch
);
243 /* A buffer to use for incoming packets, and its current size. The
244 buffer is grown dynamically for larger incoming packets.
245 Outgoing packets may also be constructed in this buffer.
246 The size of the buffer is always at least REMOTE_PACKET_SIZE;
247 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
249 gdb::char_vector buf
;
251 /* True if we're going through initial connection setup (finding out
252 about the remote side's threads, relocating symbols, etc.). */
253 bool starting_up
= false;
255 /* If we negotiated packet size explicitly (and thus can bypass
256 heuristics for the largest packet size that will not overflow
257 a buffer in the stub), this will be set to that packet size.
258 Otherwise zero, meaning to use the guessed size. */
259 long explicit_packet_size
= 0;
261 /* True, if in no ack mode. That is, neither GDB nor the stub will
262 expect acks from each other. The connection is assumed to be
264 bool noack_mode
= false;
266 /* True if we're connected in extended remote mode. */
267 bool extended
= false;
269 /* True if we resumed the target and we're waiting for the target to
270 stop. In the mean time, we can't start another command/query.
271 The remote server wouldn't be ready to process it, so we'd
272 timeout waiting for a reply that would never come and eventually
273 we'd close the connection. This can happen in asynchronous mode
274 because we allow GDB commands while the target is running. */
275 bool waiting_for_stop_reply
= false;
277 /* The status of the stub support for the various vCont actions. */
278 vCont_action_support supports_vCont
;
279 /* Whether vCont support was probed already. This is a workaround
280 until packet_support is per-connection. */
281 bool supports_vCont_probed
;
283 /* True if the user has pressed Ctrl-C, but the target hasn't
284 responded to that. */
285 bool ctrlc_pending_p
= false;
287 /* True if we saw a Ctrl-C while reading or writing from/to the
288 remote descriptor. At that point it is not safe to send a remote
289 interrupt packet, so we instead remember we saw the Ctrl-C and
290 process it once we're done with sending/receiving the current
291 packet, which should be shortly. If however that takes too long,
292 and the user presses Ctrl-C again, we offer to disconnect. */
293 bool got_ctrlc_during_io
= false;
295 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
296 remote_open knows that we don't have a file open when the program
298 struct serial
*remote_desc
= nullptr;
300 /* These are the threads which we last sent to the remote system. The
301 TID member will be -1 for all or -2 for not sent yet. */
302 ptid_t general_thread
= null_ptid
;
303 ptid_t continue_thread
= null_ptid
;
305 /* This is the traceframe which we last selected on the remote system.
306 It will be -1 if no traceframe is selected. */
307 int remote_traceframe_number
= -1;
309 char *last_pass_packet
= nullptr;
311 /* The last QProgramSignals packet sent to the target. We bypass
312 sending a new program signals list down to the target if the new
313 packet is exactly the same as the last we sent. IOW, we only let
314 the target know about program signals list changes. */
315 char *last_program_signals_packet
= nullptr;
317 gdb_signal last_sent_signal
= GDB_SIGNAL_0
;
319 bool last_sent_step
= false;
321 /* The execution direction of the last resume we got. */
322 exec_direction_kind last_resume_exec_dir
= EXEC_FORWARD
;
324 char *finished_object
= nullptr;
325 char *finished_annex
= nullptr;
326 ULONGEST finished_offset
= 0;
328 /* Should we try the 'ThreadInfo' query packet?
330 This variable (NOT available to the user: auto-detect only!)
331 determines whether GDB will use the new, simpler "ThreadInfo"
332 query or the older, more complex syntax for thread queries.
333 This is an auto-detect variable (set to true at each connect,
334 and set to false when the target fails to recognize it). */
335 bool use_threadinfo_query
= false;
336 bool use_threadextra_query
= false;
338 threadref echo_nextthread
{};
339 threadref nextthread
{};
340 threadref resultthreadlist
[MAXTHREADLISTRESULTS
] {};
342 /* The state of remote notification. */
343 struct remote_notif_state
*notif_state
= nullptr;
345 /* The branch trace configuration. */
346 struct btrace_config btrace_config
{};
348 /* The argument to the last "vFile:setfs:" packet we sent, used
349 to avoid sending repeated unnecessary "vFile:setfs:" packets.
350 Initialized to -1 to indicate that no "vFile:setfs:" packet
351 has yet been sent. */
354 /* A readahead cache for vFile:pread. Often, reading a binary
355 involves a sequence of small reads. E.g., when parsing an ELF
356 file. A readahead cache helps mostly the case of remote
357 debugging on a connection with higher latency, due to the
358 request/reply nature of the RSP. We only cache data for a single
359 file descriptor at a time. */
360 struct readahead_cache readahead_cache
;
362 /* The list of already fetched and acknowledged stop events. This
363 queue is used for notification Stop, and other notifications
364 don't need queue for their events, because the notification
365 events of Stop can't be consumed immediately, so that events
366 should be queued first, and be consumed by remote_wait_{ns,as}
367 one per time. Other notifications can consume their events
368 immediately, so queue is not needed for them. */
369 std::vector
<stop_reply_up
> stop_reply_queue
;
371 /* Asynchronous signal handle registered as event loop source for
372 when we have pending events ready to be passed to the core. */
373 struct async_event_handler
*remote_async_inferior_event_token
= nullptr;
375 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
376 ``forever'' still use the normal timeout mechanism. This is
377 currently used by the ASYNC code to guarentee that target reads
378 during the initial connect always time-out. Once getpkt has been
379 modified to return a timeout indication and, in turn
380 remote_wait()/wait_for_inferior() have gained a timeout parameter
382 int wait_forever_enabled_p
= 1;
385 /* Mapping of remote protocol data for each gdbarch. Usually there
386 is only one entry here, though we may see more with stubs that
387 support multi-process. */
388 std::unordered_map
<struct gdbarch
*, remote_arch_state
>
392 static const target_info remote_target_info
= {
394 N_("Remote target using gdb-specific protocol"),
398 class remote_target
: public process_stratum_target
401 remote_target () = default;
402 ~remote_target () override
;
404 const target_info
&info () const override
405 { return remote_target_info
; }
407 const char *connection_string () override
;
409 thread_control_capabilities
get_thread_control_capabilities () override
410 { return tc_schedlock
; }
412 /* Open a remote connection. */
413 static void open (const char *, int);
415 void close () override
;
417 void detach (inferior
*, int) override
;
418 void disconnect (const char *, int) override
;
420 void commit_resumed () override
;
421 void resume (ptid_t
, int, enum gdb_signal
) override
;
422 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
423 bool has_pending_events () override
;
425 void fetch_registers (struct regcache
*, int) override
;
426 void store_registers (struct regcache
*, int) override
;
427 void prepare_to_store (struct regcache
*) override
;
429 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
431 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
432 enum remove_bp_reason
) override
;
435 bool stopped_by_sw_breakpoint () override
;
436 bool supports_stopped_by_sw_breakpoint () override
;
438 bool stopped_by_hw_breakpoint () override
;
440 bool supports_stopped_by_hw_breakpoint () override
;
442 bool stopped_by_watchpoint () override
;
444 bool stopped_data_address (CORE_ADDR
*) override
;
446 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
448 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
450 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
452 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
454 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
456 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
457 struct expression
*) override
;
459 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
460 struct expression
*) override
;
462 void kill () override
;
464 void load (const char *, int) override
;
466 void mourn_inferior () override
;
468 void pass_signals (gdb::array_view
<const unsigned char>) override
;
470 int set_syscall_catchpoint (int, bool, int,
471 gdb::array_view
<const int>) override
;
473 void program_signals (gdb::array_view
<const unsigned char>) override
;
475 bool thread_alive (ptid_t ptid
) override
;
477 const char *thread_name (struct thread_info
*) override
;
479 void update_thread_list () override
;
481 std::string
pid_to_str (ptid_t
) override
;
483 const char *extra_thread_info (struct thread_info
*) override
;
485 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
487 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
489 inferior
*inf
) override
;
491 gdb::byte_vector
thread_info_to_thread_handle (struct thread_info
*tp
)
494 void stop (ptid_t
) override
;
496 void interrupt () override
;
498 void pass_ctrlc () override
;
500 enum target_xfer_status
xfer_partial (enum target_object object
,
503 const gdb_byte
*writebuf
,
504 ULONGEST offset
, ULONGEST len
,
505 ULONGEST
*xfered_len
) override
;
507 ULONGEST
get_memory_xfer_limit () override
;
509 void rcmd (const char *command
, struct ui_file
*output
) override
;
511 const char *pid_to_exec_file (int pid
) override
;
513 void log_command (const char *cmd
) override
515 serial_log_command (this, cmd
);
518 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
519 CORE_ADDR load_module_addr
,
520 CORE_ADDR offset
) override
;
522 bool can_execute_reverse () override
;
524 std::vector
<mem_region
> memory_map () override
;
526 void flash_erase (ULONGEST address
, LONGEST length
) override
;
528 void flash_done () override
;
530 const struct target_desc
*read_description () override
;
532 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
533 const gdb_byte
*pattern
, ULONGEST pattern_len
,
534 CORE_ADDR
*found_addrp
) override
;
536 bool can_async_p () override
;
538 bool is_async_p () override
;
540 void async (bool) override
;
542 int async_wait_fd () override
;
544 void thread_events (int) override
;
546 int can_do_single_step () override
;
548 void terminal_inferior () override
;
550 void terminal_ours () override
;
552 bool supports_non_stop () override
;
554 bool supports_multi_process () override
;
556 bool supports_disable_randomization () override
;
558 bool filesystem_is_local () override
;
561 int fileio_open (struct inferior
*inf
, const char *filename
,
562 int flags
, int mode
, int warn_if_slow
,
563 fileio_error
*target_errno
) override
;
565 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
566 ULONGEST offset
, fileio_error
*target_errno
) override
;
568 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
569 ULONGEST offset
, fileio_error
*target_errno
) override
;
571 int fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
) override
;
573 int fileio_close (int fd
, fileio_error
*target_errno
) override
;
575 int fileio_unlink (struct inferior
*inf
,
576 const char *filename
,
577 fileio_error
*target_errno
) override
;
579 gdb::optional
<std::string
>
580 fileio_readlink (struct inferior
*inf
,
581 const char *filename
,
582 fileio_error
*target_errno
) override
;
584 bool supports_enable_disable_tracepoint () override
;
586 bool supports_string_tracing () override
;
588 bool supports_evaluation_of_breakpoint_conditions () override
;
590 bool can_run_breakpoint_commands () override
;
592 void trace_init () override
;
594 void download_tracepoint (struct bp_location
*location
) override
;
596 bool can_download_tracepoint () override
;
598 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
600 void enable_tracepoint (struct bp_location
*location
) override
;
602 void disable_tracepoint (struct bp_location
*location
) override
;
604 void trace_set_readonly_regions () override
;
606 void trace_start () override
;
608 int get_trace_status (struct trace_status
*ts
) override
;
610 void get_tracepoint_status (struct breakpoint
*tp
, struct uploaded_tp
*utp
)
613 void trace_stop () override
;
615 int trace_find (enum trace_find_type type
, int num
,
616 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
618 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
620 int save_trace_data (const char *filename
) override
;
622 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
624 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
626 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
628 int get_min_fast_tracepoint_insn_len () override
;
630 void set_disconnected_tracing (int val
) override
;
632 void set_circular_trace_buffer (int val
) override
;
634 void set_trace_buffer_size (LONGEST val
) override
;
636 bool set_trace_notes (const char *user
, const char *notes
,
637 const char *stopnotes
) override
;
639 int core_of_thread (ptid_t ptid
) override
;
641 int verify_memory (const gdb_byte
*data
,
642 CORE_ADDR memaddr
, ULONGEST size
) override
;
645 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
647 void set_permissions () override
;
649 bool static_tracepoint_marker_at (CORE_ADDR
,
650 struct static_tracepoint_marker
*marker
)
653 std::vector
<static_tracepoint_marker
>
654 static_tracepoint_markers_by_strid (const char *id
) override
;
656 traceframe_info_up
traceframe_info () override
;
658 bool use_agent (bool use
) override
;
659 bool can_use_agent () override
;
661 struct btrace_target_info
*
662 enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
) override
;
664 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
666 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
668 enum btrace_error
read_btrace (struct btrace_data
*data
,
669 struct btrace_target_info
*btinfo
,
670 enum btrace_read_type type
) override
;
672 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
673 bool augmented_libraries_svr4_read () override
;
674 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
675 void follow_exec (inferior
*, ptid_t
, const char *) override
;
676 int insert_fork_catchpoint (int) override
;
677 int remove_fork_catchpoint (int) override
;
678 int insert_vfork_catchpoint (int) override
;
679 int remove_vfork_catchpoint (int) override
;
680 int insert_exec_catchpoint (int) override
;
681 int remove_exec_catchpoint (int) override
;
682 enum exec_direction_kind
execution_direction () override
;
684 bool supports_memory_tagging () override
;
686 bool fetch_memtags (CORE_ADDR address
, size_t len
,
687 gdb::byte_vector
&tags
, int type
) override
;
689 bool store_memtags (CORE_ADDR address
, size_t len
,
690 const gdb::byte_vector
&tags
, int type
) override
;
692 public: /* Remote specific methods. */
694 void remote_download_command_source (int num
, ULONGEST addr
,
695 struct command_line
*cmds
);
697 void remote_file_put (const char *local_file
, const char *remote_file
,
699 void remote_file_get (const char *remote_file
, const char *local_file
,
701 void remote_file_delete (const char *remote_file
, int from_tty
);
703 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
704 ULONGEST offset
, fileio_error
*remote_errno
);
705 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
706 ULONGEST offset
, fileio_error
*remote_errno
);
707 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
708 ULONGEST offset
, fileio_error
*remote_errno
);
710 int remote_hostio_send_command (int command_bytes
, int which_packet
,
711 fileio_error
*remote_errno
, const char **attachment
,
712 int *attachment_len
);
713 int remote_hostio_set_filesystem (struct inferior
*inf
,
714 fileio_error
*remote_errno
);
715 /* We should get rid of this and use fileio_open directly. */
716 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
717 int flags
, int mode
, int warn_if_slow
,
718 fileio_error
*remote_errno
);
719 int remote_hostio_close (int fd
, fileio_error
*remote_errno
);
721 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
722 fileio_error
*remote_errno
);
724 struct remote_state
*get_remote_state ();
726 long get_remote_packet_size (void);
727 long get_memory_packet_size (struct memory_packet_config
*config
);
729 long get_memory_write_packet_size ();
730 long get_memory_read_packet_size ();
732 char *append_pending_thread_resumptions (char *p
, char *endp
,
734 static void open_1 (const char *name
, int from_tty
, int extended_p
);
735 void start_remote (int from_tty
, int extended_p
);
736 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
738 char *append_resumption (char *p
, char *endp
,
739 ptid_t ptid
, int step
, gdb_signal siggnal
);
740 int remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
743 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
745 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
746 target_wait_flags options
);
747 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
748 target_wait_flags options
);
750 ptid_t
process_stop_reply (struct stop_reply
*stop_reply
,
751 target_waitstatus
*status
);
753 ptid_t select_thread_for_ambiguous_stop_reply
754 (const struct target_waitstatus
&status
);
756 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
758 void print_one_stopped_thread (thread_info
*thread
);
759 void process_initial_stop_replies (int from_tty
);
761 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
764 void btrace_sync_conf (const btrace_config
*conf
);
766 void remote_btrace_maybe_reopen ();
768 void remove_new_fork_children (threads_listing_context
*context
);
769 void kill_new_fork_children (inferior
*inf
);
770 void discard_pending_stop_replies (struct inferior
*inf
);
771 int stop_reply_queue_length ();
773 void check_pending_events_prevent_wildcard_vcont
774 (bool *may_global_wildcard_vcont
);
776 void discard_pending_stop_replies_in_queue ();
777 struct stop_reply
*remote_notif_remove_queued_reply (ptid_t ptid
);
778 struct stop_reply
*queued_stop_reply (ptid_t ptid
);
779 int peek_stop_reply (ptid_t ptid
);
780 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
782 void remote_stop_ns (ptid_t ptid
);
783 void remote_interrupt_as ();
784 void remote_interrupt_ns ();
786 char *remote_get_noisy_reply ();
787 int remote_query_attached (int pid
);
788 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
791 ptid_t
remote_current_thread (ptid_t oldpid
);
792 ptid_t
get_current_thread (const char *wait_status
);
794 void set_thread (ptid_t ptid
, int gen
);
795 void set_general_thread (ptid_t ptid
);
796 void set_continue_thread (ptid_t ptid
);
797 void set_general_process ();
799 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
801 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
802 gdb_ext_thread_info
*info
);
803 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
804 gdb_ext_thread_info
*info
);
806 int parse_threadlist_response (const char *pkt
, int result_limit
,
807 threadref
*original_echo
,
808 threadref
*resultlist
,
810 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
811 int result_limit
, int *done
, int *result_count
,
812 threadref
*threadlist
);
814 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
815 void *context
, int looplimit
);
817 int remote_get_threads_with_ql (threads_listing_context
*context
);
818 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
819 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
821 void extended_remote_restart ();
825 void remote_check_symbols ();
827 void remote_supported_packet (const struct protocol_feature
*feature
,
828 enum packet_support support
,
829 const char *argument
);
831 void remote_query_supported ();
833 void remote_packet_size (const protocol_feature
*feature
,
834 packet_support support
, const char *value
);
836 void remote_serial_quit_handler ();
838 void remote_detach_pid (int pid
);
840 void remote_vcont_probe ();
842 void remote_resume_with_hc (ptid_t ptid
, int step
,
845 void send_interrupt_sequence ();
846 void interrupt_query ();
848 void remote_notif_get_pending_events (notif_client
*nc
);
850 int fetch_register_using_p (struct regcache
*regcache
,
852 int send_g_packet ();
853 void process_g_packet (struct regcache
*regcache
);
854 void fetch_registers_using_g (struct regcache
*regcache
);
855 int store_register_using_P (const struct regcache
*regcache
,
857 void store_registers_using_G (const struct regcache
*regcache
);
859 void set_remote_traceframe ();
861 void check_binary_download (CORE_ADDR addr
);
863 target_xfer_status
remote_write_bytes_aux (const char *header
,
865 const gdb_byte
*myaddr
,
868 ULONGEST
*xfered_len_units
,
872 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
873 const gdb_byte
*myaddr
, ULONGEST len
,
874 int unit_size
, ULONGEST
*xfered_len
);
876 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
878 int unit_size
, ULONGEST
*xfered_len_units
);
880 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
884 ULONGEST
*xfered_len
);
886 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
887 gdb_byte
*myaddr
, ULONGEST len
,
889 ULONGEST
*xfered_len
);
891 packet_result
remote_send_printf (const char *format
, ...)
892 ATTRIBUTE_PRINTF (2, 3);
894 target_xfer_status
remote_flash_write (ULONGEST address
,
895 ULONGEST length
, ULONGEST
*xfered_len
,
896 const gdb_byte
*data
);
898 int readchar (int timeout
);
900 void remote_serial_write (const char *str
, int len
);
902 int putpkt (const char *buf
);
903 int putpkt_binary (const char *buf
, int cnt
);
905 int putpkt (const gdb::char_vector
&buf
)
907 return putpkt (buf
.data ());
911 long read_frame (gdb::char_vector
*buf_p
);
912 void getpkt (gdb::char_vector
*buf
, int forever
);
913 int getpkt_or_notif_sane_1 (gdb::char_vector
*buf
, int forever
,
914 int expecting_notif
, int *is_notif
);
915 int getpkt_sane (gdb::char_vector
*buf
, int forever
);
916 int getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
918 int remote_vkill (int pid
);
919 void remote_kill_k ();
921 void extended_remote_disable_randomization (int val
);
922 int extended_remote_run (const std::string
&args
);
924 void send_environment_packet (const char *action
,
928 void extended_remote_environment_support ();
929 void extended_remote_set_inferior_cwd ();
931 target_xfer_status
remote_write_qxfer (const char *object_name
,
933 const gdb_byte
*writebuf
,
934 ULONGEST offset
, LONGEST len
,
935 ULONGEST
*xfered_len
,
936 struct packet_config
*packet
);
938 target_xfer_status
remote_read_qxfer (const char *object_name
,
940 gdb_byte
*readbuf
, ULONGEST offset
,
942 ULONGEST
*xfered_len
,
943 struct packet_config
*packet
);
945 void push_stop_reply (struct stop_reply
*new_event
);
947 bool vcont_r_supported ();
951 bool start_remote_1 (int from_tty
, int extended_p
);
953 /* The remote state. Don't reference this directly. Use the
954 get_remote_state method instead. */
955 remote_state m_remote_state
;
958 static const target_info extended_remote_target_info
= {
960 N_("Extended remote target using gdb-specific protocol"),
964 /* Set up the extended remote target by extending the standard remote
965 target and adding to it. */
967 class extended_remote_target final
: public remote_target
970 const target_info
&info () const override
971 { return extended_remote_target_info
; }
973 /* Open an extended-remote connection. */
974 static void open (const char *, int);
976 bool can_create_inferior () override
{ return true; }
977 void create_inferior (const char *, const std::string
&,
978 char **, int) override
;
980 void detach (inferior
*, int) override
;
982 bool can_attach () override
{ return true; }
983 void attach (const char *, int) override
;
985 void post_attach (int) override
;
986 bool supports_disable_randomization () override
;
989 struct stop_reply
: public notif_event
993 /* The identifier of the thread about this event */
996 /* The remote state this event is associated with. When the remote
997 connection, represented by a remote_state object, is closed,
998 all the associated stop_reply events should be released. */
999 struct remote_state
*rs
;
1001 struct target_waitstatus ws
;
1003 /* The architecture associated with the expedited registers. */
1006 /* Expedited registers. This makes remote debugging a bit more
1007 efficient for those targets that provide critical registers as
1008 part of their normal status mechanism (as another roundtrip to
1009 fetch them is avoided). */
1010 std::vector
<cached_reg_t
> regcache
;
1012 enum target_stop_reason stop_reason
;
1014 CORE_ADDR watch_data_address
;
1019 /* Return TARGET as a remote_target if it is one, else nullptr. */
1021 static remote_target
*
1022 as_remote_target (process_stratum_target
*target
)
1024 return dynamic_cast<remote_target
*> (target
);
1030 is_remote_target (process_stratum_target
*target
)
1032 return as_remote_target (target
) != nullptr;
1035 /* Per-program-space data key. */
1036 static const registry
<program_space
>::key
<char, gdb::xfree_deleter
<char>>
1039 /* The variable registered as the control variable used by the
1040 remote exec-file commands. While the remote exec-file setting is
1041 per-program-space, the set/show machinery uses this as the
1042 location of the remote exec-file value. */
1043 static std::string remote_exec_file_var
;
1045 /* The size to align memory write packets, when practical. The protocol
1046 does not guarantee any alignment, and gdb will generate short
1047 writes and unaligned writes, but even as a best-effort attempt this
1048 can improve bulk transfers. For instance, if a write is misaligned
1049 relative to the target's data bus, the stub may need to make an extra
1050 round trip fetching data from the target. This doesn't make a
1051 huge difference, but it's easy to do, so we try to be helpful.
1053 The alignment chosen is arbitrary; usually data bus width is
1054 important here, not the possibly larger cache line size. */
1055 enum { REMOTE_ALIGN_WRITES
= 16 };
1057 /* Prototypes for local functions. */
1059 static int hexnumlen (ULONGEST num
);
1061 static int stubhex (int ch
);
1063 static int hexnumstr (char *, ULONGEST
);
1065 static int hexnumnstr (char *, ULONGEST
, int);
1067 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1069 static int stub_unpack_int (const char *buff
, int fieldlength
);
1071 struct packet_config
;
1073 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1075 struct cmd_list_element
*c
,
1078 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1080 static void remote_async_inferior_event_handler (gdb_client_data
);
1082 static bool remote_read_description_p (struct target_ops
*target
);
1084 static void remote_console_output (const char *msg
);
1086 static void remote_btrace_reset (remote_state
*rs
);
1088 static void remote_unpush_and_throw (remote_target
*target
);
1092 static struct cmd_list_element
*remote_cmdlist
;
1094 /* For "set remote" and "show remote". */
1096 static struct cmd_list_element
*remote_set_cmdlist
;
1097 static struct cmd_list_element
*remote_show_cmdlist
;
1099 /* Controls whether GDB is willing to use range stepping. */
1101 static bool use_range_stepping
= true;
1103 /* From the remote target's point of view, each thread is in one of these three
1105 enum class resume_state
1107 /* Not resumed - we haven't been asked to resume this thread. */
1110 /* We have been asked to resume this thread, but haven't sent a vCont action
1111 for it yet. We'll need to consider it next time commit_resume is
1113 RESUMED_PENDING_VCONT
,
1115 /* We have been asked to resume this thread, and we have sent a vCont action
1120 /* Information about a thread's pending vCont-resume. Used when a thread is in
1121 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1122 stores this information which is then picked up by
1123 remote_target::commit_resume to know which is the proper action for this
1124 thread to include in the vCont packet. */
1125 struct resumed_pending_vcont_info
1127 /* True if the last resume call for this thread was a step request, false
1128 if a continue request. */
1131 /* The signal specified in the last resume call for this thread. */
1135 /* Private data that we'll store in (struct thread_info)->priv. */
1136 struct remote_thread_info
: public private_thread_info
1142 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1143 sequence of bytes. */
1144 gdb::byte_vector thread_handle
;
1146 /* Whether the target stopped for a breakpoint/watchpoint. */
1147 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1149 /* This is set to the data address of the access causing the target
1150 to stop for a watchpoint. */
1151 CORE_ADDR watch_data_address
= 0;
1153 /* Get the thread's resume state. */
1154 enum resume_state
get_resume_state () const
1156 return m_resume_state
;
1159 /* Put the thread in the NOT_RESUMED state. */
1160 void set_not_resumed ()
1162 m_resume_state
= resume_state::NOT_RESUMED
;
1165 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1166 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1168 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1169 m_resumed_pending_vcont_info
.step
= step
;
1170 m_resumed_pending_vcont_info
.sig
= sig
;
1173 /* Get the information this thread's pending vCont-resumption.
1175 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1177 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1179 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1181 return m_resumed_pending_vcont_info
;
1184 /* Put the thread in the VCONT_RESUMED state. */
1187 m_resume_state
= resume_state::RESUMED
;
1191 /* Resume state for this thread. This is used to implement vCont action
1192 coalescing (only when the target operates in non-stop mode).
1194 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1195 which notes that this thread must be considered in the next commit_resume
1198 remote_target::commit_resume sends a vCont packet with actions for the
1199 threads in the RESUMED_PENDING_VCONT state and moves them to the
1200 VCONT_RESUMED state.
1202 When reporting a stop to the core for a thread, that thread is moved back
1203 to the NOT_RESUMED state. */
1204 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1206 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1207 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1210 remote_state::remote_state ()
1215 remote_state::~remote_state ()
1217 xfree (this->last_pass_packet
);
1218 xfree (this->last_program_signals_packet
);
1219 xfree (this->finished_object
);
1220 xfree (this->finished_annex
);
1223 /* Utility: generate error from an incoming stub packet. */
1225 trace_error (char *buf
)
1228 return; /* not an error msg */
1231 case '1': /* malformed packet error */
1232 if (*++buf
== '0') /* general case: */
1233 error (_("remote.c: error in outgoing packet."));
1235 error (_("remote.c: error in outgoing packet at field #%ld."),
1236 strtol (buf
, NULL
, 16));
1238 error (_("Target returns error code '%s'."), buf
);
1242 /* Utility: wait for reply from stub, while accepting "O" packets. */
1245 remote_target::remote_get_noisy_reply ()
1247 struct remote_state
*rs
= get_remote_state ();
1249 do /* Loop on reply from remote stub. */
1253 QUIT
; /* Allow user to bail out with ^C. */
1254 getpkt (&rs
->buf
, 0);
1255 buf
= rs
->buf
.data ();
1258 else if (startswith (buf
, "qRelocInsn:"))
1261 CORE_ADDR from
, to
, org_to
;
1263 int adjusted_size
= 0;
1266 p
= buf
+ strlen ("qRelocInsn:");
1267 pp
= unpack_varlen_hex (p
, &ul
);
1269 error (_("invalid qRelocInsn packet: %s"), buf
);
1273 unpack_varlen_hex (p
, &ul
);
1280 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
1283 catch (const gdb_exception
&ex
)
1285 if (ex
.error
== MEMORY_ERROR
)
1287 /* Propagate memory errors silently back to the
1288 target. The stub may have limited the range of
1289 addresses we can write to, for example. */
1293 /* Something unexpectedly bad happened. Be verbose
1294 so we can tell what, and propagate the error back
1295 to the stub, so it doesn't get stuck waiting for
1297 exception_fprintf (gdb_stderr
, ex
,
1298 _("warning: relocating instruction: "));
1305 adjusted_size
= to
- org_to
;
1307 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1311 else if (buf
[0] == 'O' && buf
[1] != 'K')
1312 remote_console_output (buf
+ 1); /* 'O' message from stub */
1314 return buf
; /* Here's the actual reply. */
1319 struct remote_arch_state
*
1320 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1322 remote_arch_state
*rsa
;
1324 auto it
= this->m_arch_states
.find (gdbarch
);
1325 if (it
== this->m_arch_states
.end ())
1327 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1328 std::forward_as_tuple (gdbarch
),
1329 std::forward_as_tuple (gdbarch
));
1330 rsa
= &p
.first
->second
;
1332 /* Make sure that the packet buffer is plenty big enough for
1333 this architecture. */
1334 if (this->buf
.size () < rsa
->remote_packet_size
)
1335 this->buf
.resize (2 * rsa
->remote_packet_size
);
1343 /* Fetch the global remote target state. */
1346 remote_target::get_remote_state ()
1348 /* Make sure that the remote architecture state has been
1349 initialized, because doing so might reallocate rs->buf. Any
1350 function which calls getpkt also needs to be mindful of changes
1351 to rs->buf, but this call limits the number of places which run
1353 m_remote_state
.get_remote_arch_state (target_gdbarch ());
1355 return &m_remote_state
;
1358 /* Fetch the remote exec-file from the current program space. */
1361 get_remote_exec_file (void)
1363 char *remote_exec_file
;
1365 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1366 if (remote_exec_file
== NULL
)
1369 return remote_exec_file
;
1372 /* Set the remote exec file for PSPACE. */
1375 set_pspace_remote_exec_file (struct program_space
*pspace
,
1376 const char *remote_exec_file
)
1378 char *old_file
= remote_pspace_data
.get (pspace
);
1381 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1384 /* The "set/show remote exec-file" set command hook. */
1387 set_remote_exec_file (const char *ignored
, int from_tty
,
1388 struct cmd_list_element
*c
)
1390 set_pspace_remote_exec_file (current_program_space
,
1391 remote_exec_file_var
.c_str ());
1394 /* The "set/show remote exec-file" show command hook. */
1397 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1398 struct cmd_list_element
*cmd
, const char *value
)
1400 gdb_printf (file
, "%s\n", get_remote_exec_file ());
1404 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1406 int regnum
, num_remote_regs
, offset
;
1407 struct packet_reg
**remote_regs
;
1409 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1411 struct packet_reg
*r
= ®s
[regnum
];
1413 if (register_size (gdbarch
, regnum
) == 0)
1414 /* Do not try to fetch zero-sized (placeholder) registers. */
1417 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1422 /* Define the g/G packet format as the contents of each register
1423 with a remote protocol number, in order of ascending protocol
1426 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1427 for (num_remote_regs
= 0, regnum
= 0;
1428 regnum
< gdbarch_num_regs (gdbarch
);
1430 if (regs
[regnum
].pnum
!= -1)
1431 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1433 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1434 [] (const packet_reg
*a
, const packet_reg
*b
)
1435 { return a
->pnum
< b
->pnum
; });
1437 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1439 remote_regs
[regnum
]->in_g_packet
= 1;
1440 remote_regs
[regnum
]->offset
= offset
;
1441 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1447 /* Given the architecture described by GDBARCH, return the remote
1448 protocol register's number and the register's offset in the g/G
1449 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1450 If the target does not have a mapping for REGNUM, return false,
1451 otherwise, return true. */
1454 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1455 int *pnum
, int *poffset
)
1457 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1459 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1461 map_regcache_remote_table (gdbarch
, regs
.data ());
1463 *pnum
= regs
[regnum
].pnum
;
1464 *poffset
= regs
[regnum
].offset
;
1469 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1471 /* Use the architecture to build a regnum<->pnum table, which will be
1472 1:1 unless a feature set specifies otherwise. */
1473 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1475 /* Record the maximum possible size of the g packet - it may turn out
1477 this->sizeof_g_packet
1478 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1480 /* Default maximum number of characters in a packet body. Many
1481 remote stubs have a hardwired buffer size of 400 bytes
1482 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1483 as the maximum packet-size to ensure that the packet and an extra
1484 NUL character can always fit in the buffer. This stops GDB
1485 trashing stubs that try to squeeze an extra NUL into what is
1486 already a full buffer (As of 1999-12-04 that was most stubs). */
1487 this->remote_packet_size
= 400 - 1;
1489 /* This one is filled in when a ``g'' packet is received. */
1490 this->actual_register_packet_size
= 0;
1492 /* Should rsa->sizeof_g_packet needs more space than the
1493 default, adjust the size accordingly. Remember that each byte is
1494 encoded as two characters. 32 is the overhead for the packet
1495 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1496 (``$NN:G...#NN'') is a better guess, the below has been padded a
1498 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1499 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1502 /* Get a pointer to the current remote target. If not connected to a
1503 remote target, return NULL. */
1505 static remote_target
*
1506 get_current_remote_target ()
1508 target_ops
*proc_target
= current_inferior ()->process_target ();
1509 return dynamic_cast<remote_target
*> (proc_target
);
1512 /* Return the current allowed size of a remote packet. This is
1513 inferred from the current architecture, and should be used to
1514 limit the length of outgoing packets. */
1516 remote_target::get_remote_packet_size ()
1518 struct remote_state
*rs
= get_remote_state ();
1519 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1521 if (rs
->explicit_packet_size
)
1522 return rs
->explicit_packet_size
;
1524 return rsa
->remote_packet_size
;
1527 static struct packet_reg
*
1528 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1531 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
1535 struct packet_reg
*r
= &rsa
->regs
[regnum
];
1537 gdb_assert (r
->regnum
== regnum
);
1542 static struct packet_reg
*
1543 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1548 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1550 struct packet_reg
*r
= &rsa
->regs
[i
];
1552 if (r
->pnum
== pnum
)
1558 /* Allow the user to specify what sequence to send to the remote
1559 when he requests a program interruption: Although ^C is usually
1560 what remote systems expect (this is the default, here), it is
1561 sometimes preferable to send a break. On other systems such
1562 as the Linux kernel, a break followed by g, which is Magic SysRq g
1563 is required in order to interrupt the execution. */
1564 const char interrupt_sequence_control_c
[] = "Ctrl-C";
1565 const char interrupt_sequence_break
[] = "BREAK";
1566 const char interrupt_sequence_break_g
[] = "BREAK-g";
1567 static const char *const interrupt_sequence_modes
[] =
1569 interrupt_sequence_control_c
,
1570 interrupt_sequence_break
,
1571 interrupt_sequence_break_g
,
1574 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
1577 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
1578 struct cmd_list_element
*c
,
1581 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
1583 _("Send the ASCII ETX character (Ctrl-c) "
1584 "to the remote target to interrupt the "
1585 "execution of the program.\n"));
1586 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
1588 _("send a break signal to the remote target "
1589 "to interrupt the execution of the program.\n"));
1590 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
1592 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1593 "the remote target to interrupt the execution "
1594 "of Linux kernel.\n"));
1596 internal_error (__FILE__
, __LINE__
,
1597 _("Invalid value for interrupt_sequence_mode: %s."),
1598 interrupt_sequence_mode
);
1601 /* This boolean variable specifies whether interrupt_sequence is sent
1602 to the remote target when gdb connects to it.
1603 This is mostly needed when you debug the Linux kernel: The Linux kernel
1604 expects BREAK g which is Magic SysRq g for connecting gdb. */
1605 static bool interrupt_on_connect
= false;
1607 /* This variable is used to implement the "set/show remotebreak" commands.
1608 Since these commands are now deprecated in favor of "set/show remote
1609 interrupt-sequence", it no longer has any effect on the code. */
1610 static bool remote_break
;
1613 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
1616 interrupt_sequence_mode
= interrupt_sequence_break
;
1618 interrupt_sequence_mode
= interrupt_sequence_control_c
;
1622 show_remotebreak (struct ui_file
*file
, int from_tty
,
1623 struct cmd_list_element
*c
,
1628 /* This variable sets the number of bits in an address that are to be
1629 sent in a memory ("M" or "m") packet. Normally, after stripping
1630 leading zeros, the entire address would be sent. This variable
1631 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1632 initial implementation of remote.c restricted the address sent in
1633 memory packets to ``host::sizeof long'' bytes - (typically 32
1634 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1635 address was never sent. Since fixing this bug may cause a break in
1636 some remote targets this variable is principally provided to
1637 facilitate backward compatibility. */
1639 static unsigned int remote_address_size
;
1642 /* User configurable variables for the number of characters in a
1643 memory read/write packet. MIN (rsa->remote_packet_size,
1644 rsa->sizeof_g_packet) is the default. Some targets need smaller
1645 values (fifo overruns, et.al.) and some users need larger values
1646 (speed up transfers). The variables ``preferred_*'' (the user
1647 request), ``current_*'' (what was actually set) and ``forced_*''
1648 (Positive - a soft limit, negative - a hard limit). */
1650 struct memory_packet_config
1657 /* The default max memory-write-packet-size, when the setting is
1658 "fixed". The 16k is historical. (It came from older GDB's using
1659 alloca for buffers and the knowledge (folklore?) that some hosts
1660 don't cope very well with large alloca calls.) */
1661 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
1663 /* The minimum remote packet size for memory transfers. Ensures we
1664 can write at least one byte. */
1665 #define MIN_MEMORY_PACKET_SIZE 20
1667 /* Get the memory packet size, assuming it is fixed. */
1670 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
1672 gdb_assert (config
->fixed_p
);
1674 if (config
->size
<= 0)
1675 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
1677 return config
->size
;
1680 /* Compute the current size of a read/write packet. Since this makes
1681 use of ``actual_register_packet_size'' the computation is dynamic. */
1684 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
1686 struct remote_state
*rs
= get_remote_state ();
1687 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1690 if (config
->fixed_p
)
1691 what_they_get
= get_fixed_memory_packet_size (config
);
1694 what_they_get
= get_remote_packet_size ();
1695 /* Limit the packet to the size specified by the user. */
1696 if (config
->size
> 0
1697 && what_they_get
> config
->size
)
1698 what_they_get
= config
->size
;
1700 /* Limit it to the size of the targets ``g'' response unless we have
1701 permission from the stub to use a larger packet size. */
1702 if (rs
->explicit_packet_size
== 0
1703 && rsa
->actual_register_packet_size
> 0
1704 && what_they_get
> rsa
->actual_register_packet_size
)
1705 what_they_get
= rsa
->actual_register_packet_size
;
1707 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
1708 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
1710 /* Make sure there is room in the global buffer for this packet
1711 (including its trailing NUL byte). */
1712 if (rs
->buf
.size () < what_they_get
+ 1)
1713 rs
->buf
.resize (2 * what_they_get
);
1715 return what_they_get
;
1718 /* Update the size of a read/write packet. If they user wants
1719 something really big then do a sanity check. */
1722 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
)
1724 int fixed_p
= config
->fixed_p
;
1725 long size
= config
->size
;
1728 error (_("Argument required (integer, `fixed' or `limited')."));
1729 else if (strcmp (args
, "hard") == 0
1730 || strcmp (args
, "fixed") == 0)
1732 else if (strcmp (args
, "soft") == 0
1733 || strcmp (args
, "limit") == 0)
1739 size
= strtoul (args
, &end
, 0);
1741 error (_("Invalid %s (bad syntax)."), config
->name
);
1743 /* Instead of explicitly capping the size of a packet to or
1744 disallowing it, the user is allowed to set the size to
1745 something arbitrarily large. */
1749 if (fixed_p
&& !config
->fixed_p
)
1751 /* So that the query shows the correct value. */
1752 long query_size
= (size
<= 0
1753 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
1756 if (! query (_("The target may not be able to correctly handle a %s\n"
1757 "of %ld bytes. Change the packet size? "),
1758 config
->name
, query_size
))
1759 error (_("Packet size not changed."));
1761 /* Update the config. */
1762 config
->fixed_p
= fixed_p
;
1763 config
->size
= size
;
1767 show_memory_packet_size (struct memory_packet_config
*config
)
1769 if (config
->size
== 0)
1770 gdb_printf (_("The %s is 0 (default). "), config
->name
);
1772 gdb_printf (_("The %s is %ld. "), config
->name
, config
->size
);
1773 if (config
->fixed_p
)
1774 gdb_printf (_("Packets are fixed at %ld bytes.\n"),
1775 get_fixed_memory_packet_size (config
));
1778 remote_target
*remote
= get_current_remote_target ();
1781 gdb_printf (_("Packets are limited to %ld bytes.\n"),
1782 remote
->get_memory_packet_size (config
));
1784 gdb_puts ("The actual limit will be further reduced "
1785 "dependent on the target.\n");
1789 /* FIXME: needs to be per-remote-target. */
1790 static struct memory_packet_config memory_write_packet_config
=
1792 "memory-write-packet-size",
1796 set_memory_write_packet_size (const char *args
, int from_tty
)
1798 set_memory_packet_size (args
, &memory_write_packet_config
);
1802 show_memory_write_packet_size (const char *args
, int from_tty
)
1804 show_memory_packet_size (&memory_write_packet_config
);
1807 /* Show the number of hardware watchpoints that can be used. */
1810 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
1811 struct cmd_list_element
*c
,
1814 gdb_printf (file
, _("The maximum number of target hardware "
1815 "watchpoints is %s.\n"), value
);
1818 /* Show the length limit (in bytes) for hardware watchpoints. */
1821 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
1822 struct cmd_list_element
*c
,
1825 gdb_printf (file
, _("The maximum length (in bytes) of a target "
1826 "hardware watchpoint is %s.\n"), value
);
1829 /* Show the number of hardware breakpoints that can be used. */
1832 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
1833 struct cmd_list_element
*c
,
1836 gdb_printf (file
, _("The maximum number of target hardware "
1837 "breakpoints is %s.\n"), value
);
1840 /* Controls the maximum number of characters to display in the debug output
1841 for each remote packet. The remaining characters are omitted. */
1843 static int remote_packet_max_chars
= 512;
1845 /* Show the maximum number of characters to display for each remote packet
1846 when remote debugging is enabled. */
1849 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
1850 struct cmd_list_element
*c
,
1853 gdb_printf (file
, _("Number of remote packet characters to "
1854 "display is %s.\n"), value
);
1858 remote_target::get_memory_write_packet_size ()
1860 return get_memory_packet_size (&memory_write_packet_config
);
1863 /* FIXME: needs to be per-remote-target. */
1864 static struct memory_packet_config memory_read_packet_config
=
1866 "memory-read-packet-size",
1870 set_memory_read_packet_size (const char *args
, int from_tty
)
1872 set_memory_packet_size (args
, &memory_read_packet_config
);
1876 show_memory_read_packet_size (const char *args
, int from_tty
)
1878 show_memory_packet_size (&memory_read_packet_config
);
1882 remote_target::get_memory_read_packet_size ()
1884 long size
= get_memory_packet_size (&memory_read_packet_config
);
1886 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1887 extra buffer size argument before the memory read size can be
1888 increased beyond this. */
1889 if (size
> get_remote_packet_size ())
1890 size
= get_remote_packet_size ();
1896 struct packet_config
1901 /* If auto, GDB auto-detects support for this packet or feature,
1902 either through qSupported, or by trying the packet and looking
1903 at the response. If true, GDB assumes the target supports this
1904 packet. If false, the packet is disabled. Configs that don't
1905 have an associated command always have this set to auto. */
1906 enum auto_boolean detect
;
1908 /* The "show remote foo-packet" command created for this packet. */
1909 cmd_list_element
*show_cmd
;
1911 /* Does the target support this packet? */
1912 enum packet_support support
;
1915 static enum packet_support
packet_config_support (struct packet_config
*config
);
1916 static enum packet_support
packet_support (int packet
);
1919 show_packet_config_cmd (ui_file
*file
, struct packet_config
*config
)
1921 const char *support
= "internal-error";
1923 switch (packet_config_support (config
))
1926 support
= "enabled";
1928 case PACKET_DISABLE
:
1929 support
= "disabled";
1931 case PACKET_SUPPORT_UNKNOWN
:
1932 support
= "unknown";
1935 switch (config
->detect
)
1937 case AUTO_BOOLEAN_AUTO
:
1939 _("Support for the `%s' packet "
1940 "is auto-detected, currently %s.\n"),
1941 config
->name
, support
);
1943 case AUTO_BOOLEAN_TRUE
:
1944 case AUTO_BOOLEAN_FALSE
:
1946 _("Support for the `%s' packet is currently %s.\n"),
1947 config
->name
, support
);
1953 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1954 const char *title
, int legacy
)
1956 config
->name
= name
;
1957 config
->title
= title
;
1958 gdb::unique_xmalloc_ptr
<char> set_doc
1959 = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
1961 gdb::unique_xmalloc_ptr
<char> show_doc
1962 = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
1964 /* set/show TITLE-packet {auto,on,off} */
1965 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
1966 set_show_commands cmds
1967 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
1968 &config
->detect
, set_doc
.get (),
1969 show_doc
.get (), NULL
, /* help_doc */
1971 show_remote_protocol_packet_cmd
,
1972 &remote_set_cmdlist
, &remote_show_cmdlist
);
1973 config
->show_cmd
= cmds
.show
;
1975 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1978 /* It's not clear who should take ownership of the LEGACY_NAME string
1979 created below, so, for now, place the string into a static vector
1980 which ensures the strings is released when GDB exits. */
1981 static std::vector
<gdb::unique_xmalloc_ptr
<char>> legacy_names
;
1982 gdb::unique_xmalloc_ptr
<char> legacy_name
1983 = xstrprintf ("%s-packet", name
);
1984 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
1985 &remote_set_cmdlist
);
1986 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
1987 &remote_show_cmdlist
);
1988 legacy_names
.emplace_back (std::move (legacy_name
));
1992 static enum packet_result
1993 packet_check_result (const char *buf
)
1997 /* The stub recognized the packet request. Check that the
1998 operation succeeded. */
2000 && isxdigit (buf
[1]) && isxdigit (buf
[2])
2002 /* "Enn" - definitely an error. */
2003 return PACKET_ERROR
;
2005 /* Always treat "E." as an error. This will be used for
2006 more verbose error messages, such as E.memtypes. */
2007 if (buf
[0] == 'E' && buf
[1] == '.')
2008 return PACKET_ERROR
;
2010 /* The packet may or may not be OK. Just assume it is. */
2014 /* The stub does not support the packet. */
2015 return PACKET_UNKNOWN
;
2018 static enum packet_result
2019 packet_check_result (const gdb::char_vector
&buf
)
2021 return packet_check_result (buf
.data ());
2024 static enum packet_result
2025 packet_ok (const char *buf
, struct packet_config
*config
)
2027 enum packet_result result
;
2029 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2030 && config
->support
== PACKET_DISABLE
)
2031 internal_error (__FILE__
, __LINE__
,
2032 _("packet_ok: attempt to use a disabled packet"));
2034 result
= packet_check_result (buf
);
2039 /* The stub recognized the packet request. */
2040 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2042 remote_debug_printf ("Packet %s (%s) is supported",
2043 config
->name
, config
->title
);
2044 config
->support
= PACKET_ENABLE
;
2047 case PACKET_UNKNOWN
:
2048 /* The stub does not support the packet. */
2049 if (config
->detect
== AUTO_BOOLEAN_AUTO
2050 && config
->support
== PACKET_ENABLE
)
2052 /* If the stub previously indicated that the packet was
2053 supported then there is a protocol error. */
2054 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2055 config
->name
, config
->title
);
2057 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2059 /* The user set it wrong. */
2060 error (_("Enabled packet %s (%s) not recognized by stub"),
2061 config
->name
, config
->title
);
2064 remote_debug_printf ("Packet %s (%s) is NOT supported",
2065 config
->name
, config
->title
);
2066 config
->support
= PACKET_DISABLE
;
2073 static enum packet_result
2074 packet_ok (const gdb::char_vector
&buf
, struct packet_config
*config
)
2076 return packet_ok (buf
.data (), config
);
2093 PACKET_vFile_pwrite
,
2095 PACKET_vFile_unlink
,
2096 PACKET_vFile_readlink
,
2099 PACKET_qXfer_features
,
2100 PACKET_qXfer_exec_file
,
2101 PACKET_qXfer_libraries
,
2102 PACKET_qXfer_libraries_svr4
,
2103 PACKET_qXfer_memory_map
,
2104 PACKET_qXfer_osdata
,
2105 PACKET_qXfer_threads
,
2106 PACKET_qXfer_statictrace_read
,
2107 PACKET_qXfer_traceframe_info
,
2113 PACKET_QPassSignals
,
2114 PACKET_QCatchSyscalls
,
2115 PACKET_QProgramSignals
,
2116 PACKET_QSetWorkingDir
,
2117 PACKET_QStartupWithShell
,
2118 PACKET_QEnvironmentHexEncoded
,
2119 PACKET_QEnvironmentReset
,
2120 PACKET_QEnvironmentUnset
,
2122 PACKET_qSearch_memory
,
2125 PACKET_QStartNoAckMode
,
2127 PACKET_qXfer_siginfo_read
,
2128 PACKET_qXfer_siginfo_write
,
2131 /* Support for conditional tracepoints. */
2132 PACKET_ConditionalTracepoints
,
2134 /* Support for target-side breakpoint conditions. */
2135 PACKET_ConditionalBreakpoints
,
2137 /* Support for target-side breakpoint commands. */
2138 PACKET_BreakpointCommands
,
2140 /* Support for fast tracepoints. */
2141 PACKET_FastTracepoints
,
2143 /* Support for static tracepoints. */
2144 PACKET_StaticTracepoints
,
2146 /* Support for installing tracepoints while a trace experiment is
2148 PACKET_InstallInTrace
,
2152 PACKET_TracepointSource
,
2155 PACKET_QDisableRandomization
,
2157 PACKET_QTBuffer_size
,
2161 PACKET_qXfer_btrace
,
2163 /* Support for the QNonStop packet. */
2166 /* Support for the QThreadEvents packet. */
2167 PACKET_QThreadEvents
,
2169 /* Support for multi-process extensions. */
2170 PACKET_multiprocess_feature
,
2172 /* Support for enabling and disabling tracepoints while a trace
2173 experiment is running. */
2174 PACKET_EnableDisableTracepoints_feature
,
2176 /* Support for collecting strings using the tracenz bytecode. */
2177 PACKET_tracenz_feature
,
2179 /* Support for continuing to run a trace experiment while GDB is
2181 PACKET_DisconnectedTracing_feature
,
2183 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
2184 PACKET_augmented_libraries_svr4_read_feature
,
2186 /* Support for the qXfer:btrace-conf:read packet. */
2187 PACKET_qXfer_btrace_conf
,
2189 /* Support for the Qbtrace-conf:bts:size packet. */
2190 PACKET_Qbtrace_conf_bts_size
,
2192 /* Support for swbreak+ feature. */
2193 PACKET_swbreak_feature
,
2195 /* Support for hwbreak+ feature. */
2196 PACKET_hwbreak_feature
,
2198 /* Support for fork events. */
2199 PACKET_fork_event_feature
,
2201 /* Support for vfork events. */
2202 PACKET_vfork_event_feature
,
2204 /* Support for the Qbtrace-conf:pt:size packet. */
2205 PACKET_Qbtrace_conf_pt_size
,
2207 /* Support for exec events. */
2208 PACKET_exec_event_feature
,
2210 /* Support for query supported vCont actions. */
2211 PACKET_vContSupported
,
2213 /* Support remote CTRL-C. */
2216 /* Support TARGET_WAITKIND_NO_RESUMED. */
2219 /* Support for memory tagging, allocation tag fetch/store
2220 packets and the tag violation stop replies. */
2221 PACKET_memory_tagging_feature
,
2226 /* FIXME: needs to be per-remote-target. Ignoring this for now,
2227 assuming all remote targets are the same server (thus all support
2228 the same packets). */
2229 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
2231 /* Returns the packet's corresponding "set remote foo-packet" command
2232 state. See struct packet_config for more details. */
2234 static enum auto_boolean
2235 packet_set_cmd_state (int packet
)
2237 return remote_protocol_packets
[packet
].detect
;
2240 /* Returns whether a given packet or feature is supported. This takes
2241 into account the state of the corresponding "set remote foo-packet"
2242 command, which may be used to bypass auto-detection. */
2244 static enum packet_support
2245 packet_config_support (struct packet_config
*config
)
2247 switch (config
->detect
)
2249 case AUTO_BOOLEAN_TRUE
:
2250 return PACKET_ENABLE
;
2251 case AUTO_BOOLEAN_FALSE
:
2252 return PACKET_DISABLE
;
2253 case AUTO_BOOLEAN_AUTO
:
2254 return config
->support
;
2256 gdb_assert_not_reached ("bad switch");
2260 /* Same as packet_config_support, but takes the packet's enum value as
2263 static enum packet_support
2264 packet_support (int packet
)
2266 struct packet_config
*config
= &remote_protocol_packets
[packet
];
2268 return packet_config_support (config
);
2272 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2273 struct cmd_list_element
*c
,
2276 struct packet_config
*packet
;
2277 gdb_assert (c
->var
.has_value ());
2279 for (packet
= remote_protocol_packets
;
2280 packet
< &remote_protocol_packets
[PACKET_MAX
];
2283 if (c
== packet
->show_cmd
)
2285 show_packet_config_cmd (file
, packet
);
2289 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
2293 /* Should we try one of the 'Z' requests? */
2297 Z_PACKET_SOFTWARE_BP
,
2298 Z_PACKET_HARDWARE_BP
,
2305 /* For compatibility with older distributions. Provide a ``set remote
2306 Z-packet ...'' command that updates all the Z packet types. */
2308 static enum auto_boolean remote_Z_packet_detect
;
2311 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2312 struct cmd_list_element
*c
)
2316 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2317 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2321 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2322 struct cmd_list_element
*c
,
2327 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2329 show_packet_config_cmd (file
, &remote_protocol_packets
[PACKET_Z0
+ i
]);
2333 /* Returns true if the multi-process extensions are in effect. */
2336 remote_multi_process_p (struct remote_state
*rs
)
2338 return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
;
2341 /* Returns true if fork events are supported. */
2344 remote_fork_event_p (struct remote_state
*rs
)
2346 return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
;
2349 /* Returns true if vfork events are supported. */
2352 remote_vfork_event_p (struct remote_state
*rs
)
2354 return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
;
2357 /* Returns true if exec events are supported. */
2360 remote_exec_event_p (struct remote_state
*rs
)
2362 return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
;
2365 /* Returns true if memory tagging is supported, false otherwise. */
2368 remote_memory_tagging_p ()
2370 return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
;
2373 /* Insert fork catchpoint target routine. If fork events are enabled
2374 then return success, nothing more to do. */
2377 remote_target::insert_fork_catchpoint (int pid
)
2379 struct remote_state
*rs
= get_remote_state ();
2381 return !remote_fork_event_p (rs
);
2384 /* Remove fork catchpoint target routine. Nothing to do, just
2388 remote_target::remove_fork_catchpoint (int pid
)
2393 /* Insert vfork catchpoint target routine. If vfork events are enabled
2394 then return success, nothing more to do. */
2397 remote_target::insert_vfork_catchpoint (int pid
)
2399 struct remote_state
*rs
= get_remote_state ();
2401 return !remote_vfork_event_p (rs
);
2404 /* Remove vfork catchpoint target routine. Nothing to do, just
2408 remote_target::remove_vfork_catchpoint (int pid
)
2413 /* Insert exec catchpoint target routine. If exec events are
2414 enabled, just return success. */
2417 remote_target::insert_exec_catchpoint (int pid
)
2419 struct remote_state
*rs
= get_remote_state ();
2421 return !remote_exec_event_p (rs
);
2424 /* Remove exec catchpoint target routine. Nothing to do, just
2428 remote_target::remove_exec_catchpoint (int pid
)
2435 /* Take advantage of the fact that the TID field is not used, to tag
2436 special ptids with it set to != 0. */
2437 static const ptid_t
magic_null_ptid (42000, -1, 1);
2438 static const ptid_t
not_sent_ptid (42000, -2, 1);
2439 static const ptid_t
any_thread_ptid (42000, 0, 1);
2441 /* Find out if the stub attached to PID (and hence GDB should offer to
2442 detach instead of killing it when bailing out). */
2445 remote_target::remote_query_attached (int pid
)
2447 struct remote_state
*rs
= get_remote_state ();
2448 size_t size
= get_remote_packet_size ();
2450 if (packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2453 if (remote_multi_process_p (rs
))
2454 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2456 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2459 getpkt (&rs
->buf
, 0);
2461 switch (packet_ok (rs
->buf
,
2462 &remote_protocol_packets
[PACKET_qAttached
]))
2465 if (strcmp (rs
->buf
.data (), "1") == 0)
2469 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
2471 case PACKET_UNKNOWN
:
2478 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2479 has been invented by GDB, instead of reported by the target. Since
2480 we can be connected to a remote system before before knowing about
2481 any inferior, mark the target with execution when we find the first
2482 inferior. If ATTACHED is 1, then we had just attached to this
2483 inferior. If it is 0, then we just created this inferior. If it
2484 is -1, then try querying the remote stub to find out if it had
2485 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2486 attempt to open this inferior's executable as the main executable
2487 if no main executable is open already. */
2490 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2493 struct inferior
*inf
;
2495 /* Check whether this process we're learning about is to be
2496 considered attached, or if is to be considered to have been
2497 spawned by the stub. */
2499 attached
= remote_query_attached (pid
);
2501 if (gdbarch_has_global_solist (target_gdbarch ()))
2503 /* If the target shares code across all inferiors, then every
2504 attach adds a new inferior. */
2505 inf
= add_inferior (pid
);
2507 /* ... and every inferior is bound to the same program space.
2508 However, each inferior may still have its own address
2510 inf
->aspace
= maybe_new_address_space ();
2511 inf
->pspace
= current_program_space
;
2515 /* In the traditional debugging scenario, there's a 1-1 match
2516 between program/address spaces. We simply bind the inferior
2517 to the program space's address space. */
2518 inf
= current_inferior ();
2520 /* However, if the current inferior is already bound to a
2521 process, find some other empty inferior. */
2525 for (inferior
*it
: all_inferiors ())
2534 /* Since all inferiors were already bound to a process, add
2536 inf
= add_inferior_with_spaces ();
2538 switch_to_inferior_no_thread (inf
);
2539 inf
->push_target (this);
2540 inferior_appeared (inf
, pid
);
2543 inf
->attach_flag
= attached
;
2544 inf
->fake_pid_p
= fake_pid_p
;
2546 /* If no main executable is currently open then attempt to
2547 open the file that was executed to create this inferior. */
2548 if (try_open_exec
&& get_exec_file (0) == NULL
)
2549 exec_file_locate_attach (pid
, 0, 1);
2551 /* Check for exec file mismatch, and let the user solve it. */
2552 validate_exec_file (1);
2557 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2558 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2561 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2562 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2563 remote_state::starting_up flag) is true then the new thread is added
2564 silently, otherwise the new thread will be announced to the user. */
2567 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2570 struct remote_state
*rs
= get_remote_state ();
2571 struct thread_info
*thread
;
2573 /* GDB historically didn't pull threads in the initial connection
2574 setup. If the remote target doesn't even have a concept of
2575 threads (e.g., a bare-metal target), even if internally we
2576 consider that a single-threaded target, mentioning a new thread
2577 might be confusing to the user. Be silent then, preserving the
2578 age old behavior. */
2579 if (rs
->starting_up
|| silent_p
)
2580 thread
= add_thread_silent (this, ptid
);
2582 thread
= add_thread (this, ptid
);
2584 /* We start by assuming threads are resumed. That state then gets updated
2585 when we process a matching stop reply. */
2586 get_remote_thread_info (thread
)->set_resumed ();
2588 set_executing (this, ptid
, executing
);
2589 set_running (this, ptid
, running
);
2594 /* Come here when we learn about a thread id from the remote target.
2595 It may be the first time we hear about such thread, so take the
2596 opportunity to add it to GDB's thread list. In case this is the
2597 first time we're noticing its corresponding inferior, add it to
2598 GDB's inferior list as well. EXECUTING indicates whether the
2599 thread is (internally) executing or stopped. */
2602 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2604 /* In non-stop mode, we assume new found threads are (externally)
2605 running until proven otherwise with a stop reply. In all-stop,
2606 we can only get here if all threads are stopped. */
2607 bool running
= target_is_non_stop_p ();
2609 /* If this is a new thread, add it to GDB's thread list.
2610 If we leave it up to WFI to do this, bad things will happen. */
2612 thread_info
*tp
= find_thread_ptid (this, currthread
);
2613 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2615 /* We're seeing an event on a thread id we knew had exited.
2616 This has to be a new thread reusing the old id. Add it. */
2617 remote_add_thread (currthread
, running
, executing
, false);
2621 if (!in_thread_list (this, currthread
))
2623 struct inferior
*inf
= NULL
;
2624 int pid
= currthread
.pid ();
2626 if (inferior_ptid
.is_pid ()
2627 && pid
== inferior_ptid
.pid ())
2629 /* inferior_ptid has no thread member yet. This can happen
2630 with the vAttach -> remote_wait,"TAAthread:" path if the
2631 stub doesn't support qC. This is the first stop reported
2632 after an attach, so this is the main thread. Update the
2633 ptid in the thread list. */
2634 if (in_thread_list (this, ptid_t (pid
)))
2635 thread_change_ptid (this, inferior_ptid
, currthread
);
2639 = remote_add_thread (currthread
, running
, executing
, false);
2640 switch_to_thread (thr
);
2645 if (magic_null_ptid
== inferior_ptid
)
2647 /* inferior_ptid is not set yet. This can happen with the
2648 vRun -> remote_wait,"TAAthread:" path if the stub
2649 doesn't support qC. This is the first stop reported
2650 after an attach, so this is the main thread. Update the
2651 ptid in the thread list. */
2652 thread_change_ptid (this, inferior_ptid
, currthread
);
2656 /* When connecting to a target remote, or to a target
2657 extended-remote which already was debugging an inferior, we
2658 may not know about it yet. Add it before adding its child
2659 thread, so notifications are emitted in a sensible order. */
2660 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
2662 struct remote_state
*rs
= get_remote_state ();
2663 bool fake_pid_p
= !remote_multi_process_p (rs
);
2665 inf
= remote_add_inferior (fake_pid_p
,
2666 currthread
.pid (), -1, 1);
2669 /* This is really a new thread. Add it. */
2670 thread_info
*new_thr
2671 = remote_add_thread (currthread
, running
, executing
, false);
2673 /* If we found a new inferior, let the common code do whatever
2674 it needs to with it (e.g., read shared libraries, insert
2675 breakpoints), unless we're just setting up an all-stop
2679 struct remote_state
*rs
= get_remote_state ();
2681 if (!rs
->starting_up
)
2682 notice_new_inferior (new_thr
, executing
, 0);
2687 /* Return THREAD's private thread data, creating it if necessary. */
2689 static remote_thread_info
*
2690 get_remote_thread_info (thread_info
*thread
)
2692 gdb_assert (thread
!= NULL
);
2694 if (thread
->priv
== NULL
)
2695 thread
->priv
.reset (new remote_thread_info
);
2697 return static_cast<remote_thread_info
*> (thread
->priv
.get ());
2700 /* Return PTID's private thread data, creating it if necessary. */
2702 static remote_thread_info
*
2703 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
2705 thread_info
*thr
= find_thread_ptid (target
, ptid
);
2706 return get_remote_thread_info (thr
);
2709 /* Call this function as a result of
2710 1) A halt indication (T packet) containing a thread id
2711 2) A direct query of currthread
2712 3) Successful execution of set thread */
2715 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
2717 rs
->general_thread
= currthread
;
2720 /* If 'QPassSignals' is supported, tell the remote stub what signals
2721 it can simply pass through to the inferior without reporting. */
2724 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2726 if (packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
2728 char *pass_packet
, *p
;
2730 struct remote_state
*rs
= get_remote_state ();
2732 gdb_assert (pass_signals
.size () < 256);
2733 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2735 if (pass_signals
[i
])
2738 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
2739 strcpy (pass_packet
, "QPassSignals:");
2740 p
= pass_packet
+ strlen (pass_packet
);
2741 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2743 if (pass_signals
[i
])
2746 *p
++ = tohex (i
>> 4);
2747 *p
++ = tohex (i
& 15);
2756 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
2758 putpkt (pass_packet
);
2759 getpkt (&rs
->buf
, 0);
2760 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
2761 xfree (rs
->last_pass_packet
);
2762 rs
->last_pass_packet
= pass_packet
;
2765 xfree (pass_packet
);
2769 /* If 'QCatchSyscalls' is supported, tell the remote stub
2770 to report syscalls to GDB. */
2773 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
2774 gdb::array_view
<const int> syscall_counts
)
2776 const char *catch_packet
;
2777 enum packet_result result
;
2780 if (packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
2782 /* Not supported. */
2786 if (needed
&& any_count
== 0)
2788 /* Count how many syscalls are to be caught. */
2789 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2791 if (syscall_counts
[i
] != 0)
2796 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
2797 pid
, needed
, any_count
, n_sysno
);
2799 std::string built_packet
;
2802 /* Prepare a packet with the sysno list, assuming max 8+1
2803 characters for a sysno. If the resulting packet size is too
2804 big, fallback on the non-selective packet. */
2805 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
2806 built_packet
.reserve (maxpktsz
);
2807 built_packet
= "QCatchSyscalls:1";
2810 /* Add in each syscall to be caught. */
2811 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2813 if (syscall_counts
[i
] != 0)
2814 string_appendf (built_packet
, ";%zx", i
);
2817 if (built_packet
.size () > get_remote_packet_size ())
2819 /* catch_packet too big. Fallback to less efficient
2820 non selective mode, with GDB doing the filtering. */
2821 catch_packet
= "QCatchSyscalls:1";
2824 catch_packet
= built_packet
.c_str ();
2827 catch_packet
= "QCatchSyscalls:0";
2829 struct remote_state
*rs
= get_remote_state ();
2831 putpkt (catch_packet
);
2832 getpkt (&rs
->buf
, 0);
2833 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QCatchSyscalls
]);
2834 if (result
== PACKET_OK
)
2840 /* If 'QProgramSignals' is supported, tell the remote stub what
2841 signals it should pass through to the inferior when detaching. */
2844 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
2846 if (packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
2850 struct remote_state
*rs
= get_remote_state ();
2852 gdb_assert (signals
.size () < 256);
2853 for (size_t i
= 0; i
< signals
.size (); i
++)
2858 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
2859 strcpy (packet
, "QProgramSignals:");
2860 p
= packet
+ strlen (packet
);
2861 for (size_t i
= 0; i
< signals
.size (); i
++)
2863 if (signal_pass_state (i
))
2866 *p
++ = tohex (i
>> 4);
2867 *p
++ = tohex (i
& 15);
2876 if (!rs
->last_program_signals_packet
2877 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
2880 getpkt (&rs
->buf
, 0);
2881 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QProgramSignals
]);
2882 xfree (rs
->last_program_signals_packet
);
2883 rs
->last_program_signals_packet
= packet
;
2890 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2891 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2892 thread. If GEN is set, set the general thread, if not, then set
2893 the step/continue thread. */
2895 remote_target::set_thread (ptid_t ptid
, int gen
)
2897 struct remote_state
*rs
= get_remote_state ();
2898 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
2899 char *buf
= rs
->buf
.data ();
2900 char *endbuf
= buf
+ get_remote_packet_size ();
2906 *buf
++ = gen
? 'g' : 'c';
2907 if (ptid
== magic_null_ptid
)
2908 xsnprintf (buf
, endbuf
- buf
, "0");
2909 else if (ptid
== any_thread_ptid
)
2910 xsnprintf (buf
, endbuf
- buf
, "0");
2911 else if (ptid
== minus_one_ptid
)
2912 xsnprintf (buf
, endbuf
- buf
, "-1");
2914 write_ptid (buf
, endbuf
, ptid
);
2916 getpkt (&rs
->buf
, 0);
2918 rs
->general_thread
= ptid
;
2920 rs
->continue_thread
= ptid
;
2924 remote_target::set_general_thread (ptid_t ptid
)
2926 set_thread (ptid
, 1);
2930 remote_target::set_continue_thread (ptid_t ptid
)
2932 set_thread (ptid
, 0);
2935 /* Change the remote current process. Which thread within the process
2936 ends up selected isn't important, as long as it is the same process
2937 as what INFERIOR_PTID points to.
2939 This comes from that fact that there is no explicit notion of
2940 "selected process" in the protocol. The selected process for
2941 general operations is the process the selected general thread
2945 remote_target::set_general_process ()
2947 struct remote_state
*rs
= get_remote_state ();
2949 /* If the remote can't handle multiple processes, don't bother. */
2950 if (!remote_multi_process_p (rs
))
2953 /* We only need to change the remote current thread if it's pointing
2954 at some other process. */
2955 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
2956 set_general_thread (inferior_ptid
);
2960 /* Return nonzero if this is the main thread that we made up ourselves
2961 to model non-threaded targets as single-threaded. */
2964 remote_thread_always_alive (ptid_t ptid
)
2966 if (ptid
== magic_null_ptid
)
2967 /* The main thread is always alive. */
2970 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
2971 /* The main thread is always alive. This can happen after a
2972 vAttach, if the remote side doesn't support
2979 /* Return nonzero if the thread PTID is still alive on the remote
2983 remote_target::thread_alive (ptid_t ptid
)
2985 struct remote_state
*rs
= get_remote_state ();
2988 /* Check if this is a thread that we made up ourselves to model
2989 non-threaded targets as single-threaded. */
2990 if (remote_thread_always_alive (ptid
))
2993 p
= rs
->buf
.data ();
2994 endp
= p
+ get_remote_packet_size ();
2997 write_ptid (p
, endp
, ptid
);
3000 getpkt (&rs
->buf
, 0);
3001 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
3004 /* Return a pointer to a thread name if we know it and NULL otherwise.
3005 The thread_info object owns the memory for the name. */
3008 remote_target::thread_name (struct thread_info
*info
)
3010 if (info
->priv
!= NULL
)
3012 const std::string
&name
= get_remote_thread_info (info
)->name
;
3013 return !name
.empty () ? name
.c_str () : NULL
;
3019 /* About these extended threadlist and threadinfo packets. They are
3020 variable length packets but, the fields within them are often fixed
3021 length. They are redundant enough to send over UDP as is the
3022 remote protocol in general. There is a matching unit test module
3025 /* WARNING: This threadref data structure comes from the remote O.S.,
3026 libstub protocol encoding, and remote.c. It is not particularly
3029 /* Right now, the internal structure is int. We want it to be bigger.
3030 Plan to fix this. */
3032 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3034 /* gdb_ext_thread_info is an internal GDB data structure which is
3035 equivalent to the reply of the remote threadinfo packet. */
3037 struct gdb_ext_thread_info
3039 threadref threadid
; /* External form of thread reference. */
3040 int active
; /* Has state interesting to GDB?
3042 char display
[256]; /* Brief state display, name,
3043 blocked/suspended. */
3044 char shortname
[32]; /* To be used to name threads. */
3045 char more_display
[256]; /* Long info, statistics, queue depth,
3049 /* The volume of remote transfers can be limited by submitting
3050 a mask containing bits specifying the desired information.
3051 Use a union of these values as the 'selection' parameter to
3052 get_thread_info. FIXME: Make these TAG names more thread specific. */
3054 #define TAG_THREADID 1
3055 #define TAG_EXISTS 2
3056 #define TAG_DISPLAY 4
3057 #define TAG_THREADNAME 8
3058 #define TAG_MOREDISPLAY 16
3060 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3062 static const char *unpack_nibble (const char *buf
, int *val
);
3064 static const char *unpack_byte (const char *buf
, int *value
);
3066 static char *pack_int (char *buf
, int value
);
3068 static const char *unpack_int (const char *buf
, int *value
);
3070 static const char *unpack_string (const char *src
, char *dest
, int length
);
3072 static char *pack_threadid (char *pkt
, threadref
*id
);
3074 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3076 void int_to_threadref (threadref
*id
, int value
);
3078 static int threadref_to_int (threadref
*ref
);
3080 static void copy_threadref (threadref
*dest
, threadref
*src
);
3082 static int threadmatch (threadref
*dest
, threadref
*src
);
3084 static char *pack_threadinfo_request (char *pkt
, int mode
,
3087 static char *pack_threadlist_request (char *pkt
, int startflag
,
3089 threadref
*nextthread
);
3091 static int remote_newthread_step (threadref
*ref
, void *context
);
3094 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3095 buffer we're allowed to write to. Returns
3096 BUF+CHARACTERS_WRITTEN. */
3099 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3102 struct remote_state
*rs
= get_remote_state ();
3104 if (remote_multi_process_p (rs
))
3108 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3110 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3114 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3116 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3121 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3122 last parsed char. Returns null_ptid if no thread id is found, and
3123 throws an error if the thread id has an invalid format. */
3126 read_ptid (const char *buf
, const char **obuf
)
3128 const char *p
= buf
;
3130 ULONGEST pid
= 0, tid
= 0;
3134 /* Multi-process ptid. */
3135 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3137 error (_("invalid remote ptid: %s"), p
);
3140 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3143 return ptid_t (pid
, tid
);
3146 /* No multi-process. Just a tid. */
3147 pp
= unpack_varlen_hex (p
, &tid
);
3149 /* Return null_ptid when no thread id is found. */
3157 /* Since the stub is not sending a process id, default to what's
3158 current_inferior, unless it doesn't have a PID yet. If so,
3159 then since there's no way to know the pid of the reported
3160 threads, use the magic number. */
3161 inferior
*inf
= current_inferior ();
3163 pid
= magic_null_ptid
.pid ();
3169 return ptid_t (pid
, tid
);
3175 if (ch
>= 'a' && ch
<= 'f')
3176 return ch
- 'a' + 10;
3177 if (ch
>= '0' && ch
<= '9')
3179 if (ch
>= 'A' && ch
<= 'F')
3180 return ch
- 'A' + 10;
3185 stub_unpack_int (const char *buff
, int fieldlength
)
3192 nibble
= stubhex (*buff
++);
3196 retval
= retval
<< 4;
3202 unpack_nibble (const char *buf
, int *val
)
3204 *val
= fromhex (*buf
++);
3209 unpack_byte (const char *buf
, int *value
)
3211 *value
= stub_unpack_int (buf
, 2);
3216 pack_int (char *buf
, int value
)
3218 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3219 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3220 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3221 buf
= pack_hex_byte (buf
, (value
& 0xff));
3226 unpack_int (const char *buf
, int *value
)
3228 *value
= stub_unpack_int (buf
, 8);
3232 #if 0 /* Currently unused, uncomment when needed. */
3233 static char *pack_string (char *pkt
, char *string
);
3236 pack_string (char *pkt
, char *string
)
3241 len
= strlen (string
);
3243 len
= 200; /* Bigger than most GDB packets, junk??? */
3244 pkt
= pack_hex_byte (pkt
, len
);
3248 if ((ch
== '\0') || (ch
== '#'))
3249 ch
= '*'; /* Protect encapsulation. */
3254 #endif /* 0 (unused) */
3257 unpack_string (const char *src
, char *dest
, int length
)
3266 pack_threadid (char *pkt
, threadref
*id
)
3269 unsigned char *altid
;
3271 altid
= (unsigned char *) id
;
3272 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3274 pkt
= pack_hex_byte (pkt
, *altid
++);
3280 unpack_threadid (const char *inbuf
, threadref
*id
)
3283 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3286 altref
= (char *) id
;
3288 while (inbuf
< limit
)
3290 x
= stubhex (*inbuf
++);
3291 y
= stubhex (*inbuf
++);
3292 *altref
++ = (x
<< 4) | y
;
3297 /* Externally, threadrefs are 64 bits but internally, they are still
3298 ints. This is due to a mismatch of specifications. We would like
3299 to use 64bit thread references internally. This is an adapter
3303 int_to_threadref (threadref
*id
, int value
)
3305 unsigned char *scan
;
3307 scan
= (unsigned char *) id
;
3313 *scan
++ = (value
>> 24) & 0xff;
3314 *scan
++ = (value
>> 16) & 0xff;
3315 *scan
++ = (value
>> 8) & 0xff;
3316 *scan
++ = (value
& 0xff);
3320 threadref_to_int (threadref
*ref
)
3323 unsigned char *scan
;
3329 value
= (value
<< 8) | ((*scan
++) & 0xff);
3334 copy_threadref (threadref
*dest
, threadref
*src
)
3337 unsigned char *csrc
, *cdest
;
3339 csrc
= (unsigned char *) src
;
3340 cdest
= (unsigned char *) dest
;
3347 threadmatch (threadref
*dest
, threadref
*src
)
3349 /* Things are broken right now, so just assume we got a match. */
3351 unsigned char *srcp
, *destp
;
3353 srcp
= (char *) src
;
3354 destp
= (char *) dest
;
3358 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3365 threadid:1, # always request threadid
3372 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3375 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3377 *pkt
++ = 'q'; /* Info Query */
3378 *pkt
++ = 'P'; /* process or thread info */
3379 pkt
= pack_int (pkt
, mode
); /* mode */
3380 pkt
= pack_threadid (pkt
, id
); /* threadid */
3381 *pkt
= '\0'; /* terminate */
3385 /* These values tag the fields in a thread info response packet. */
3386 /* Tagging the fields allows us to request specific fields and to
3387 add more fields as time goes by. */
3389 #define TAG_THREADID 1 /* Echo the thread identifier. */
3390 #define TAG_EXISTS 2 /* Is this process defined enough to
3391 fetch registers and its stack? */
3392 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3393 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3394 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3398 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3399 threadref
*expectedref
,
3400 gdb_ext_thread_info
*info
)
3402 struct remote_state
*rs
= get_remote_state ();
3406 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3409 /* info->threadid = 0; FIXME: implement zero_threadref. */
3411 info
->display
[0] = '\0';
3412 info
->shortname
[0] = '\0';
3413 info
->more_display
[0] = '\0';
3415 /* Assume the characters indicating the packet type have been
3417 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3418 pkt
= unpack_threadid (pkt
, &ref
);
3421 warning (_("Incomplete response to threadinfo request."));
3422 if (!threadmatch (&ref
, expectedref
))
3423 { /* This is an answer to a different request. */
3424 warning (_("ERROR RMT Thread info mismatch."));
3427 copy_threadref (&info
->threadid
, &ref
);
3429 /* Loop on tagged fields , try to bail if something goes wrong. */
3431 /* Packets are terminated with nulls. */
3432 while ((pkt
< limit
) && mask
&& *pkt
)
3434 pkt
= unpack_int (pkt
, &tag
); /* tag */
3435 pkt
= unpack_byte (pkt
, &length
); /* length */
3436 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3438 warning (_("ERROR RMT: threadinfo tag mismatch."));
3442 if (tag
== TAG_THREADID
)
3446 warning (_("ERROR RMT: length of threadid is not 16."));
3450 pkt
= unpack_threadid (pkt
, &ref
);
3451 mask
= mask
& ~TAG_THREADID
;
3454 if (tag
== TAG_EXISTS
)
3456 info
->active
= stub_unpack_int (pkt
, length
);
3458 mask
= mask
& ~(TAG_EXISTS
);
3461 warning (_("ERROR RMT: 'exists' length too long."));
3467 if (tag
== TAG_THREADNAME
)
3469 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3470 mask
= mask
& ~TAG_THREADNAME
;
3473 if (tag
== TAG_DISPLAY
)
3475 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3476 mask
= mask
& ~TAG_DISPLAY
;
3479 if (tag
== TAG_MOREDISPLAY
)
3481 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3482 mask
= mask
& ~TAG_MOREDISPLAY
;
3485 warning (_("ERROR RMT: unknown thread info tag."));
3486 break; /* Not a tag we know about. */
3492 remote_target::remote_get_threadinfo (threadref
*threadid
,
3494 gdb_ext_thread_info
*info
)
3496 struct remote_state
*rs
= get_remote_state ();
3499 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3501 getpkt (&rs
->buf
, 0);
3503 if (rs
->buf
[0] == '\0')
3506 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3511 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3514 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3515 threadref
*nextthread
)
3517 *pkt
++ = 'q'; /* info query packet */
3518 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3519 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3520 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3521 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3526 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3529 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3530 threadref
*original_echo
,
3531 threadref
*resultlist
,
3534 struct remote_state
*rs
= get_remote_state ();
3535 int count
, resultcount
, done
;
3538 /* Assume the 'q' and 'M chars have been stripped. */
3539 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3540 /* done parse past here */
3541 pkt
= unpack_byte (pkt
, &count
); /* count field */
3542 pkt
= unpack_nibble (pkt
, &done
);
3543 /* The first threadid is the argument threadid. */
3544 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3545 while ((count
-- > 0) && (pkt
< limit
))
3547 pkt
= unpack_threadid (pkt
, resultlist
++);
3548 if (resultcount
++ >= result_limit
)
3556 /* Fetch the next batch of threads from the remote. Returns -1 if the
3557 qL packet is not supported, 0 on error and 1 on success. */
3560 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3561 int result_limit
, int *done
, int *result_count
,
3562 threadref
*threadlist
)
3564 struct remote_state
*rs
= get_remote_state ();
3567 /* Truncate result limit to be smaller than the packet size. */
3568 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3569 >= get_remote_packet_size ())
3570 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3572 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3575 getpkt (&rs
->buf
, 0);
3576 if (rs
->buf
[0] == '\0')
3578 /* Packet not supported. */
3583 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3584 &rs
->echo_nextthread
, threadlist
, done
);
3586 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3588 /* FIXME: This is a good reason to drop the packet. */
3589 /* Possibly, there is a duplicate response. */
3591 retransmit immediatly - race conditions
3592 retransmit after timeout - yes
3594 wait for packet, then exit
3596 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3597 return 0; /* I choose simply exiting. */
3599 if (*result_count
<= 0)
3603 warning (_("RMT ERROR : failed to get remote thread list."));
3606 return result
; /* break; */
3608 if (*result_count
> result_limit
)
3611 warning (_("RMT ERROR: threadlist response longer than requested."));
3617 /* Fetch the list of remote threads, with the qL packet, and call
3618 STEPFUNCTION for each thread found. Stops iterating and returns 1
3619 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3620 STEPFUNCTION returns false. If the packet is not supported,
3624 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3625 void *context
, int looplimit
)
3627 struct remote_state
*rs
= get_remote_state ();
3628 int done
, i
, result_count
;
3636 if (loopcount
++ > looplimit
)
3639 warning (_("Remote fetch threadlist -infinite loop-."));
3642 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3643 MAXTHREADLISTRESULTS
,
3644 &done
, &result_count
,
3645 rs
->resultthreadlist
);
3648 /* Clear for later iterations. */
3650 /* Setup to resume next batch of thread references, set nextthread. */
3651 if (result_count
>= 1)
3652 copy_threadref (&rs
->nextthread
,
3653 &rs
->resultthreadlist
[result_count
- 1]);
3655 while (result_count
--)
3657 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
3667 /* A thread found on the remote target. */
3671 explicit thread_item (ptid_t ptid_
)
3675 thread_item (thread_item
&&other
) = default;
3676 thread_item
&operator= (thread_item
&&other
) = default;
3678 DISABLE_COPY_AND_ASSIGN (thread_item
);
3680 /* The thread's PTID. */
3683 /* The thread's extra info. */
3686 /* The thread's name. */
3689 /* The core the thread was running on. -1 if not known. */
3692 /* The thread handle associated with the thread. */
3693 gdb::byte_vector thread_handle
;
3696 /* Context passed around to the various methods listing remote
3697 threads. As new threads are found, they're added to the ITEMS
3700 struct threads_listing_context
3702 /* Return true if this object contains an entry for a thread with ptid
3705 bool contains_thread (ptid_t ptid
) const
3707 auto match_ptid
= [&] (const thread_item
&item
)
3709 return item
.ptid
== ptid
;
3712 auto it
= std::find_if (this->items
.begin (),
3716 return it
!= this->items
.end ();
3719 /* Remove the thread with ptid PTID. */
3721 void remove_thread (ptid_t ptid
)
3723 auto match_ptid
= [&] (const thread_item
&item
)
3725 return item
.ptid
== ptid
;
3728 auto it
= std::remove_if (this->items
.begin (),
3732 if (it
!= this->items
.end ())
3733 this->items
.erase (it
);
3736 /* The threads found on the remote target. */
3737 std::vector
<thread_item
> items
;
3741 remote_newthread_step (threadref
*ref
, void *data
)
3743 struct threads_listing_context
*context
3744 = (struct threads_listing_context
*) data
;
3745 int pid
= inferior_ptid
.pid ();
3746 int lwp
= threadref_to_int (ref
);
3747 ptid_t
ptid (pid
, lwp
);
3749 context
->items
.emplace_back (ptid
);
3751 return 1; /* continue iterator */
3754 #define CRAZY_MAX_THREADS 1000
3757 remote_target::remote_current_thread (ptid_t oldpid
)
3759 struct remote_state
*rs
= get_remote_state ();
3762 getpkt (&rs
->buf
, 0);
3763 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
3768 result
= read_ptid (&rs
->buf
[2], &obuf
);
3770 remote_debug_printf ("warning: garbage in qC reply");
3778 /* List remote threads using the deprecated qL packet. */
3781 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
3783 if (remote_threadlist_iterator (remote_newthread_step
, context
,
3784 CRAZY_MAX_THREADS
) >= 0)
3790 #if defined(HAVE_LIBEXPAT)
3793 start_thread (struct gdb_xml_parser
*parser
,
3794 const struct gdb_xml_element
*element
,
3796 std::vector
<gdb_xml_value
> &attributes
)
3798 struct threads_listing_context
*data
3799 = (struct threads_listing_context
*) user_data
;
3800 struct gdb_xml_value
*attr
;
3802 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
3803 ptid_t ptid
= read_ptid (id
, NULL
);
3805 data
->items
.emplace_back (ptid
);
3806 thread_item
&item
= data
->items
.back ();
3808 attr
= xml_find_attribute (attributes
, "core");
3810 item
.core
= *(ULONGEST
*) attr
->value
.get ();
3812 attr
= xml_find_attribute (attributes
, "name");
3814 item
.name
= (const char *) attr
->value
.get ();
3816 attr
= xml_find_attribute (attributes
, "handle");
3818 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
3822 end_thread (struct gdb_xml_parser
*parser
,
3823 const struct gdb_xml_element
*element
,
3824 void *user_data
, const char *body_text
)
3826 struct threads_listing_context
*data
3827 = (struct threads_listing_context
*) user_data
;
3829 if (body_text
!= NULL
&& *body_text
!= '\0')
3830 data
->items
.back ().extra
= body_text
;
3833 const struct gdb_xml_attribute thread_attributes
[] = {
3834 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
3835 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
3836 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
3837 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
3838 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
3841 const struct gdb_xml_element thread_children
[] = {
3842 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3845 const struct gdb_xml_element threads_children
[] = {
3846 { "thread", thread_attributes
, thread_children
,
3847 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
3848 start_thread
, end_thread
},
3849 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3852 const struct gdb_xml_element threads_elements
[] = {
3853 { "threads", NULL
, threads_children
,
3854 GDB_XML_EF_NONE
, NULL
, NULL
},
3855 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3860 /* List remote threads using qXfer:threads:read. */
3863 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
3865 #if defined(HAVE_LIBEXPAT)
3866 if (packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
3868 gdb::optional
<gdb::char_vector
> xml
3869 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
3871 if (xml
&& (*xml
)[0] != '\0')
3873 gdb_xml_parse_quick (_("threads"), "threads.dtd",
3874 threads_elements
, xml
->data (), context
);
3884 /* List remote threads using qfThreadInfo/qsThreadInfo. */
3887 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
3889 struct remote_state
*rs
= get_remote_state ();
3891 if (rs
->use_threadinfo_query
)
3895 putpkt ("qfThreadInfo");
3896 getpkt (&rs
->buf
, 0);
3897 bufp
= rs
->buf
.data ();
3898 if (bufp
[0] != '\0') /* q packet recognized */
3900 while (*bufp
++ == 'm') /* reply contains one or more TID */
3904 ptid_t ptid
= read_ptid (bufp
, &bufp
);
3905 context
->items
.emplace_back (ptid
);
3907 while (*bufp
++ == ','); /* comma-separated list */
3908 putpkt ("qsThreadInfo");
3909 getpkt (&rs
->buf
, 0);
3910 bufp
= rs
->buf
.data ();
3916 /* Packet not recognized. */
3917 rs
->use_threadinfo_query
= 0;
3924 /* Return true if INF only has one non-exited thread. */
3927 has_single_non_exited_thread (inferior
*inf
)
3930 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
3936 /* Implement the to_update_thread_list function for the remote
3940 remote_target::update_thread_list ()
3942 struct threads_listing_context context
;
3945 /* We have a few different mechanisms to fetch the thread list. Try
3946 them all, starting with the most preferred one first, falling
3947 back to older methods. */
3948 if (remote_get_threads_with_qxfer (&context
)
3949 || remote_get_threads_with_qthreadinfo (&context
)
3950 || remote_get_threads_with_ql (&context
))
3954 if (context
.items
.empty ()
3955 && remote_thread_always_alive (inferior_ptid
))
3957 /* Some targets don't really support threads, but still
3958 reply an (empty) thread list in response to the thread
3959 listing packets, instead of replying "packet not
3960 supported". Exit early so we don't delete the main
3965 /* CONTEXT now holds the current thread list on the remote
3966 target end. Delete GDB-side threads no longer found on the
3968 for (thread_info
*tp
: all_threads_safe ())
3970 if (tp
->inf
->process_target () != this)
3973 if (!context
.contains_thread (tp
->ptid
))
3975 /* Do not remove the thread if it is the last thread in
3976 the inferior. This situation happens when we have a
3977 pending exit process status to process. Otherwise we
3978 may end up with a seemingly live inferior (i.e. pid
3979 != 0) that has no threads. */
3980 if (has_single_non_exited_thread (tp
->inf
))
3988 /* Remove any unreported fork child threads from CONTEXT so
3989 that we don't interfere with follow fork, which is where
3990 creation of such threads is handled. */
3991 remove_new_fork_children (&context
);
3993 /* And now add threads we don't know about yet to our list. */
3994 for (thread_item
&item
: context
.items
)
3996 if (item
.ptid
!= null_ptid
)
3998 /* In non-stop mode, we assume new found threads are
3999 executing until proven otherwise with a stop reply.
4000 In all-stop, we can only get here if all threads are
4002 bool executing
= target_is_non_stop_p ();
4004 remote_notice_new_inferior (item
.ptid
, executing
);
4006 thread_info
*tp
= find_thread_ptid (this, item
.ptid
);
4007 remote_thread_info
*info
= get_remote_thread_info (tp
);
4008 info
->core
= item
.core
;
4009 info
->extra
= std::move (item
.extra
);
4010 info
->name
= std::move (item
.name
);
4011 info
->thread_handle
= std::move (item
.thread_handle
);
4018 /* If no thread listing method is supported, then query whether
4019 each known thread is alive, one by one, with the T packet.
4020 If the target doesn't support threads at all, then this is a
4021 no-op. See remote_thread_alive. */
4027 * Collect a descriptive string about the given thread.
4028 * The target may say anything it wants to about the thread
4029 * (typically info about its blocked / runnable state, name, etc.).
4030 * This string will appear in the info threads display.
4032 * Optional: targets are not required to implement this function.
4036 remote_target::extra_thread_info (thread_info
*tp
)
4038 struct remote_state
*rs
= get_remote_state ();
4041 struct gdb_ext_thread_info threadinfo
;
4043 if (rs
->remote_desc
== 0) /* paranoia */
4044 internal_error (__FILE__
, __LINE__
,
4045 _("remote_threads_extra_info"));
4047 if (tp
->ptid
== magic_null_ptid
4048 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4049 /* This is the main thread which was added by GDB. The remote
4050 server doesn't know about it. */
4053 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4055 /* If already have cached info, use it. */
4056 if (!extra
.empty ())
4057 return extra
.c_str ();
4059 if (packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4061 /* If we're using qXfer:threads:read, then the extra info is
4062 included in the XML. So if we didn't have anything cached,
4063 it's because there's really no extra info. */
4067 if (rs
->use_threadextra_query
)
4069 char *b
= rs
->buf
.data ();
4070 char *endb
= b
+ get_remote_packet_size ();
4072 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4074 write_ptid (b
, endb
, tp
->ptid
);
4077 getpkt (&rs
->buf
, 0);
4078 if (rs
->buf
[0] != 0)
4080 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4081 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4082 return extra
.c_str ();
4086 /* If the above query fails, fall back to the old method. */
4087 rs
->use_threadextra_query
= 0;
4088 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4089 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4090 int_to_threadref (&id
, tp
->ptid
.lwp ());
4091 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4092 if (threadinfo
.active
)
4094 if (*threadinfo
.shortname
)
4095 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4096 if (*threadinfo
.display
)
4098 if (!extra
.empty ())
4100 string_appendf (extra
, " State: %s", threadinfo
.display
);
4102 if (*threadinfo
.more_display
)
4104 if (!extra
.empty ())
4106 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4108 return extra
.c_str ();
4115 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4116 struct static_tracepoint_marker
*marker
)
4118 struct remote_state
*rs
= get_remote_state ();
4119 char *p
= rs
->buf
.data ();
4121 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4123 p
+= hexnumstr (p
, addr
);
4125 getpkt (&rs
->buf
, 0);
4126 p
= rs
->buf
.data ();
4129 error (_("Remote failure reply: %s"), p
);
4133 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4140 std::vector
<static_tracepoint_marker
>
4141 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4143 struct remote_state
*rs
= get_remote_state ();
4144 std::vector
<static_tracepoint_marker
> markers
;
4146 static_tracepoint_marker marker
;
4148 /* Ask for a first packet of static tracepoint marker
4151 getpkt (&rs
->buf
, 0);
4152 p
= rs
->buf
.data ();
4154 error (_("Remote failure reply: %s"), p
);
4160 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4162 if (strid
== NULL
|| marker
.str_id
== strid
)
4163 markers
.push_back (std::move (marker
));
4165 while (*p
++ == ','); /* comma-separated list */
4166 /* Ask for another packet of static tracepoint definition. */
4168 getpkt (&rs
->buf
, 0);
4169 p
= rs
->buf
.data ();
4176 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4179 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4181 return ptid_t (inferior_ptid
.pid (), lwp
);
4185 /* Restart the remote side; this is an extended protocol operation. */
4188 remote_target::extended_remote_restart ()
4190 struct remote_state
*rs
= get_remote_state ();
4192 /* Send the restart command; for reasons I don't understand the
4193 remote side really expects a number after the "R". */
4194 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4197 remote_fileio_reset ();
4200 /* Clean up connection to a remote debugger. */
4203 remote_target::close ()
4205 /* Make sure we leave stdin registered in the event loop. */
4208 trace_reset_local_state ();
4213 remote_target::~remote_target ()
4215 struct remote_state
*rs
= get_remote_state ();
4217 /* Check for NULL because we may get here with a partially
4218 constructed target/connection. */
4219 if (rs
->remote_desc
== nullptr)
4222 serial_close (rs
->remote_desc
);
4224 /* We are destroying the remote target, so we should discard
4225 everything of this target. */
4226 discard_pending_stop_replies_in_queue ();
4228 if (rs
->remote_async_inferior_event_token
)
4229 delete_async_event_handler (&rs
->remote_async_inferior_event_token
);
4231 delete rs
->notif_state
;
4234 /* Query the remote side for the text, data and bss offsets. */
4237 remote_target::get_offsets ()
4239 struct remote_state
*rs
= get_remote_state ();
4242 int lose
, num_segments
= 0, do_sections
, do_segments
;
4243 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4245 if (current_program_space
->symfile_object_file
== NULL
)
4248 putpkt ("qOffsets");
4249 getpkt (&rs
->buf
, 0);
4250 buf
= rs
->buf
.data ();
4252 if (buf
[0] == '\000')
4253 return; /* Return silently. Stub doesn't support
4257 warning (_("Remote failure reply: %s"), buf
);
4261 /* Pick up each field in turn. This used to be done with scanf, but
4262 scanf will make trouble if CORE_ADDR size doesn't match
4263 conversion directives correctly. The following code will work
4264 with any size of CORE_ADDR. */
4265 text_addr
= data_addr
= bss_addr
= 0;
4269 if (startswith (ptr
, "Text="))
4272 /* Don't use strtol, could lose on big values. */
4273 while (*ptr
&& *ptr
!= ';')
4274 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4276 if (startswith (ptr
, ";Data="))
4279 while (*ptr
&& *ptr
!= ';')
4280 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4285 if (!lose
&& startswith (ptr
, ";Bss="))
4288 while (*ptr
&& *ptr
!= ';')
4289 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4291 if (bss_addr
!= data_addr
)
4292 warning (_("Target reported unsupported offsets: %s"), buf
);
4297 else if (startswith (ptr
, "TextSeg="))
4300 /* Don't use strtol, could lose on big values. */
4301 while (*ptr
&& *ptr
!= ';')
4302 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4305 if (startswith (ptr
, ";DataSeg="))
4308 while (*ptr
&& *ptr
!= ';')
4309 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4317 error (_("Malformed response to offset query, %s"), buf
);
4318 else if (*ptr
!= '\0')
4319 warning (_("Target reported unsupported offsets: %s"), buf
);
4321 objfile
*objf
= current_program_space
->symfile_object_file
;
4322 section_offsets offs
= objf
->section_offsets
;
4324 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
.get ());
4325 do_segments
= (data
!= NULL
);
4326 do_sections
= num_segments
== 0;
4328 if (num_segments
> 0)
4330 segments
[0] = text_addr
;
4331 segments
[1] = data_addr
;
4333 /* If we have two segments, we can still try to relocate everything
4334 by assuming that the .text and .data offsets apply to the whole
4335 text and data segments. Convert the offsets given in the packet
4336 to base addresses for symfile_map_offsets_to_segments. */
4337 else if (data
!= nullptr && data
->segments
.size () == 2)
4339 segments
[0] = data
->segments
[0].base
+ text_addr
;
4340 segments
[1] = data
->segments
[1].base
+ data_addr
;
4343 /* If the object file has only one segment, assume that it is text
4344 rather than data; main programs with no writable data are rare,
4345 but programs with no code are useless. Of course the code might
4346 have ended up in the data segment... to detect that we would need
4347 the permissions here. */
4348 else if (data
&& data
->segments
.size () == 1)
4350 segments
[0] = data
->segments
[0].base
+ text_addr
;
4353 /* There's no way to relocate by segment. */
4359 int ret
= symfile_map_offsets_to_segments (objf
->obfd
.get (),
4361 num_segments
, segments
);
4363 if (ret
== 0 && !do_sections
)
4364 error (_("Can not handle qOffsets TextSeg "
4365 "response with this symbol file"));
4373 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4375 /* This is a temporary kludge to force data and bss to use the
4376 same offsets because that's what nlmconv does now. The real
4377 solution requires changes to the stub and remote.c that I
4378 don't have time to do right now. */
4380 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4381 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4384 objfile_relocate (objf
, offs
);
4387 /* Send interrupt_sequence to remote target. */
4390 remote_target::send_interrupt_sequence ()
4392 struct remote_state
*rs
= get_remote_state ();
4394 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4395 remote_serial_write ("\x03", 1);
4396 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4397 serial_send_break (rs
->remote_desc
);
4398 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4400 serial_send_break (rs
->remote_desc
);
4401 remote_serial_write ("g", 1);
4404 internal_error (__FILE__
, __LINE__
,
4405 _("Invalid value for interrupt_sequence_mode: %s."),
4406 interrupt_sequence_mode
);
4410 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4411 and extract the PTID. Returns NULL_PTID if not found. */
4414 stop_reply_extract_thread (const char *stop_reply
)
4416 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4420 /* Txx r:val ; r:val (...) */
4423 /* Look for "register" named "thread". */
4428 p1
= strchr (p
, ':');
4432 if (strncmp (p
, "thread", p1
- p
) == 0)
4433 return read_ptid (++p1
, &p
);
4435 p1
= strchr (p
, ';');
4447 /* Determine the remote side's current thread. If we have a stop
4448 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4449 "thread" register we can extract the current thread from. If not,
4450 ask the remote which is the current thread with qC. The former
4451 method avoids a roundtrip. */
4454 remote_target::get_current_thread (const char *wait_status
)
4456 ptid_t ptid
= null_ptid
;
4458 /* Note we don't use remote_parse_stop_reply as that makes use of
4459 the target architecture, which we haven't yet fully determined at
4461 if (wait_status
!= NULL
)
4462 ptid
= stop_reply_extract_thread (wait_status
);
4463 if (ptid
== null_ptid
)
4464 ptid
= remote_current_thread (inferior_ptid
);
4469 /* Query the remote target for which is the current thread/process,
4470 add it to our tables, and update INFERIOR_PTID. The caller is
4471 responsible for setting the state such that the remote end is ready
4472 to return the current thread.
4474 This function is called after handling the '?' or 'vRun' packets,
4475 whose response is a stop reply from which we can also try
4476 extracting the thread. If the target doesn't support the explicit
4477 qC query, we infer the current thread from that stop reply, passed
4478 in in WAIT_STATUS, which may be NULL.
4480 The function returns pointer to the main thread of the inferior. */
4483 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4485 struct remote_state
*rs
= get_remote_state ();
4486 bool fake_pid_p
= false;
4488 switch_to_no_thread ();
4490 /* Now, if we have thread information, update the current thread's
4492 ptid_t curr_ptid
= get_current_thread (wait_status
);
4494 if (curr_ptid
!= null_ptid
)
4496 if (!remote_multi_process_p (rs
))
4501 /* Without this, some commands which require an active target
4502 (such as kill) won't work. This variable serves (at least)
4503 double duty as both the pid of the target process (if it has
4504 such), and as a flag indicating that a target is active. */
4505 curr_ptid
= magic_null_ptid
;
4509 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4511 /* Add the main thread and switch to it. Don't try reading
4512 registers yet, since we haven't fetched the target description
4514 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4515 switch_to_thread_no_regs (tp
);
4520 /* Print info about a thread that was found already stopped on
4524 remote_target::print_one_stopped_thread (thread_info
*thread
)
4526 target_waitstatus ws
;
4528 /* If there is a pending waitstatus, use it. If there isn't it's because
4529 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4530 and process_initial_stop_replies decided it wasn't interesting to save
4531 and report to the core. */
4532 if (thread
->has_pending_waitstatus ())
4534 ws
= thread
->pending_waitstatus ();
4535 thread
->clear_pending_waitstatus ();
4539 ws
.set_stopped (GDB_SIGNAL_0
);
4542 switch_to_thread (thread
);
4543 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4544 set_current_sal_from_frame (get_current_frame ());
4546 /* For "info program". */
4547 set_last_target_status (this, thread
->ptid
, ws
);
4549 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4551 enum gdb_signal sig
= ws
.sig ();
4553 if (signal_print_state (sig
))
4554 gdb::observers::signal_received
.notify (sig
);
4556 gdb::observers::normal_stop
.notify (NULL
, 1);
4559 /* Process all initial stop replies the remote side sent in response
4560 to the ? packet. These indicate threads that were already stopped
4561 on initial connection. We mark these threads as stopped and print
4562 their current frame before giving the user the prompt. */
4565 remote_target::process_initial_stop_replies (int from_tty
)
4567 int pending_stop_replies
= stop_reply_queue_length ();
4568 struct thread_info
*selected
= NULL
;
4569 struct thread_info
*lowest_stopped
= NULL
;
4570 struct thread_info
*first
= NULL
;
4572 /* This is only used when the target is non-stop. */
4573 gdb_assert (target_is_non_stop_p ());
4575 /* Consume the initial pending events. */
4576 while (pending_stop_replies
-- > 0)
4578 ptid_t waiton_ptid
= minus_one_ptid
;
4580 struct target_waitstatus ws
;
4581 int ignore_event
= 0;
4583 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4585 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4589 case TARGET_WAITKIND_IGNORE
:
4590 case TARGET_WAITKIND_NO_RESUMED
:
4591 case TARGET_WAITKIND_SIGNALLED
:
4592 case TARGET_WAITKIND_EXITED
:
4593 /* We shouldn't see these, but if we do, just ignore. */
4594 remote_debug_printf ("event ignored");
4605 thread_info
*evthread
= find_thread_ptid (this, event_ptid
);
4607 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4609 enum gdb_signal sig
= ws
.sig ();
4611 /* Stubs traditionally report SIGTRAP as initial signal,
4612 instead of signal 0. Suppress it. */
4613 if (sig
== GDB_SIGNAL_TRAP
)
4615 evthread
->set_stop_signal (sig
);
4616 ws
.set_stopped (sig
);
4619 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4620 || ws
.sig () != GDB_SIGNAL_0
)
4621 evthread
->set_pending_waitstatus (ws
);
4623 set_executing (this, event_ptid
, false);
4624 set_running (this, event_ptid
, false);
4625 get_remote_thread_info (evthread
)->set_not_resumed ();
4628 /* "Notice" the new inferiors before anything related to
4629 registers/memory. */
4630 for (inferior
*inf
: all_non_exited_inferiors (this))
4632 inf
->needs_setup
= 1;
4636 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4637 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4642 /* If all-stop on top of non-stop, pause all threads. Note this
4643 records the threads' stop pc, so must be done after "noticing"
4648 /* At this point, the remote target is not async. It needs to be for
4649 the poll in stop_all_threads to consider events from it, so enable
4651 gdb_assert (!this->is_async_p ());
4652 SCOPE_EXIT
{ target_async (false); };
4653 target_async (true);
4654 stop_all_threads ("remote connect in all-stop");
4657 /* If all threads of an inferior were already stopped, we
4658 haven't setup the inferior yet. */
4659 for (inferior
*inf
: all_non_exited_inferiors (this))
4661 if (inf
->needs_setup
)
4663 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4664 switch_to_thread_no_regs (thread
);
4670 /* Now go over all threads that are stopped, and print their current
4671 frame. If all-stop, then if there's a signalled thread, pick
4673 for (thread_info
*thread
: all_non_exited_threads (this))
4679 thread
->set_running (false);
4680 else if (thread
->state
!= THREAD_STOPPED
)
4683 if (selected
== nullptr && thread
->has_pending_waitstatus ())
4686 if (lowest_stopped
== NULL
4687 || thread
->inf
->num
< lowest_stopped
->inf
->num
4688 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
4689 lowest_stopped
= thread
;
4692 print_one_stopped_thread (thread
);
4695 /* In all-stop, we only print the status of one thread, and leave
4696 others with their status pending. */
4699 thread_info
*thread
= selected
;
4701 thread
= lowest_stopped
;
4705 print_one_stopped_thread (thread
);
4709 /* Mark a remote_target as marking (by setting the starting_up flag within
4710 its remote_state) for the lifetime of this object. The reference count
4711 on the remote target is temporarily incremented, to prevent the target
4712 being deleted under our feet. */
4714 struct scoped_mark_target_starting
4716 /* Constructor, TARGET is the target to be marked as starting, its
4717 reference count will be incremented. */
4718 scoped_mark_target_starting (remote_target
*target
)
4719 : m_remote_target (target
)
4721 m_remote_target
->incref ();
4722 remote_state
*rs
= m_remote_target
->get_remote_state ();
4723 rs
->starting_up
= true;
4726 /* Destructor, mark the target being worked on as no longer starting, and
4727 decrement the reference count. */
4728 ~scoped_mark_target_starting ()
4730 remote_state
*rs
= m_remote_target
->get_remote_state ();
4731 rs
->starting_up
= false;
4732 decref_target (m_remote_target
);
4737 /* The target on which we are operating. */
4738 remote_target
*m_remote_target
;
4741 /* Helper for remote_target::start_remote, start the remote connection and
4742 sync state. Return true if everything goes OK, otherwise, return false.
4743 This function exists so that the scoped_restore created within it will
4744 expire before we return to remote_target::start_remote. */
4747 remote_target::start_remote_1 (int from_tty
, int extended_p
)
4749 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
4751 struct remote_state
*rs
= get_remote_state ();
4752 struct packet_config
*noack_config
;
4754 /* Signal other parts that we're going through the initial setup,
4755 and so things may not be stable yet. E.g., we don't try to
4756 install tracepoints until we've relocated symbols. Also, a
4757 Ctrl-C before we're connected and synced up can't interrupt the
4758 target. Instead, it offers to drop the (potentially wedged)
4760 scoped_mark_target_starting
target_is_starting (this);
4764 if (interrupt_on_connect
)
4765 send_interrupt_sequence ();
4767 /* Ack any packet which the remote side has already sent. */
4768 remote_serial_write ("+", 1);
4770 /* The first packet we send to the target is the optional "supported
4771 packets" request. If the target can answer this, it will tell us
4772 which later probes to skip. */
4773 remote_query_supported ();
4775 /* If the stub wants to get a QAllow, compose one and send it. */
4776 if (packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
4779 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4780 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4781 as a reply to known packet. For packet "vFile:setfs:" it is an
4782 invalid reply and GDB would return error in
4783 remote_hostio_set_filesystem, making remote files access impossible.
4784 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4785 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4787 const char v_mustreplyempty
[] = "vMustReplyEmpty";
4789 putpkt (v_mustreplyempty
);
4790 getpkt (&rs
->buf
, 0);
4791 if (strcmp (rs
->buf
.data (), "OK") == 0)
4792 remote_protocol_packets
[PACKET_vFile_setfs
].support
= PACKET_DISABLE
;
4793 else if (strcmp (rs
->buf
.data (), "") != 0)
4794 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
4798 /* Next, we possibly activate noack mode.
4800 If the QStartNoAckMode packet configuration is set to AUTO,
4801 enable noack mode if the stub reported a wish for it with
4804 If set to TRUE, then enable noack mode even if the stub didn't
4805 report it in qSupported. If the stub doesn't reply OK, the
4806 session ends with an error.
4808 If FALSE, then don't activate noack mode, regardless of what the
4809 stub claimed should be the default with qSupported. */
4811 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
4812 if (packet_config_support (noack_config
) != PACKET_DISABLE
)
4814 putpkt ("QStartNoAckMode");
4815 getpkt (&rs
->buf
, 0);
4816 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
4822 /* Tell the remote that we are using the extended protocol. */
4824 getpkt (&rs
->buf
, 0);
4827 /* Let the target know which signals it is allowed to pass down to
4829 update_signals_program_target ();
4831 /* Next, if the target can specify a description, read it. We do
4832 this before anything involving memory or registers. */
4833 target_find_description ();
4835 /* Next, now that we know something about the target, update the
4836 address spaces in the program spaces. */
4837 update_address_spaces ();
4839 /* On OSs where the list of libraries is global to all
4840 processes, we fetch them early. */
4841 if (gdbarch_has_global_solist (target_gdbarch ()))
4842 solib_add (NULL
, from_tty
, auto_solib_add
);
4844 if (target_is_non_stop_p ())
4846 if (packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
4847 error (_("Non-stop mode requested, but remote "
4848 "does not support non-stop"));
4850 putpkt ("QNonStop:1");
4851 getpkt (&rs
->buf
, 0);
4853 if (strcmp (rs
->buf
.data (), "OK") != 0)
4854 error (_("Remote refused setting non-stop mode with: %s"),
4857 /* Find about threads and processes the stub is already
4858 controlling. We default to adding them in the running state.
4859 The '?' query below will then tell us about which threads are
4861 this->update_thread_list ();
4863 else if (packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
4865 /* Don't assume that the stub can operate in all-stop mode.
4866 Request it explicitly. */
4867 putpkt ("QNonStop:0");
4868 getpkt (&rs
->buf
, 0);
4870 if (strcmp (rs
->buf
.data (), "OK") != 0)
4871 error (_("Remote refused setting all-stop mode with: %s"),
4875 /* Upload TSVs regardless of whether the target is running or not. The
4876 remote stub, such as GDBserver, may have some predefined or builtin
4877 TSVs, even if the target is not running. */
4878 if (get_trace_status (current_trace_status ()) != -1)
4880 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
4882 upload_trace_state_variables (&uploaded_tsvs
);
4883 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
4886 /* Check whether the target is running now. */
4888 getpkt (&rs
->buf
, 0);
4890 if (!target_is_non_stop_p ())
4892 char *wait_status
= NULL
;
4894 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
4897 error (_("The target is not running (try extended-remote?)"));
4902 /* Save the reply for later. */
4903 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
4904 strcpy (wait_status
, rs
->buf
.data ());
4907 /* Fetch thread list. */
4908 target_update_thread_list ();
4910 /* Let the stub know that we want it to return the thread. */
4911 set_continue_thread (minus_one_ptid
);
4913 if (thread_count (this) == 0)
4915 /* Target has no concept of threads at all. GDB treats
4916 non-threaded target as single-threaded; add a main
4918 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
4919 get_remote_thread_info (tp
)->set_resumed ();
4923 /* We have thread information; select the thread the target
4924 says should be current. If we're reconnecting to a
4925 multi-threaded program, this will ideally be the thread
4926 that last reported an event before GDB disconnected. */
4927 ptid_t curr_thread
= get_current_thread (wait_status
);
4928 if (curr_thread
== null_ptid
)
4930 /* Odd... The target was able to list threads, but not
4931 tell us which thread was current (no "thread"
4932 register in T stop reply?). Just pick the first
4933 thread in the thread list then. */
4935 remote_debug_printf ("warning: couldn't determine remote "
4936 "current thread; picking first in list.");
4938 for (thread_info
*tp
: all_non_exited_threads (this,
4941 switch_to_thread (tp
);
4946 switch_to_thread (find_thread_ptid (this, curr_thread
));
4949 /* init_wait_for_inferior should be called before get_offsets in order
4950 to manage `inserted' flag in bp loc in a correct state.
4951 breakpoint_init_inferior, called from init_wait_for_inferior, set
4952 `inserted' flag to 0, while before breakpoint_re_set, called from
4953 start_remote, set `inserted' flag to 1. In the initialization of
4954 inferior, breakpoint_init_inferior should be called first, and then
4955 breakpoint_re_set can be called. If this order is broken, state of
4956 `inserted' flag is wrong, and cause some problems on breakpoint
4958 init_wait_for_inferior ();
4960 get_offsets (); /* Get text, data & bss offsets. */
4962 /* If we could not find a description using qXfer, and we know
4963 how to do it some other way, try again. This is not
4964 supported for non-stop; it could be, but it is tricky if
4965 there are no stopped threads when we connect. */
4966 if (remote_read_description_p (this)
4967 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
4969 target_clear_description ();
4970 target_find_description ();
4973 /* Use the previously fetched status. */
4974 gdb_assert (wait_status
!= NULL
);
4975 struct notif_event
*reply
4976 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
4977 push_stop_reply ((struct stop_reply
*) reply
);
4979 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
4983 /* Clear WFI global state. Do this before finding about new
4984 threads and inferiors, and setting the current inferior.
4985 Otherwise we would clear the proceed status of the current
4986 inferior when we want its stop_soon state to be preserved
4987 (see notice_new_inferior). */
4988 init_wait_for_inferior ();
4990 /* In non-stop, we will either get an "OK", meaning that there
4991 are no stopped threads at this time; or, a regular stop
4992 reply. In the latter case, there may be more than one thread
4993 stopped --- we pull them all out using the vStopped
4995 if (strcmp (rs
->buf
.data (), "OK") != 0)
4997 struct notif_client
*notif
= ¬if_client_stop
;
4999 /* remote_notif_get_pending_replies acks this one, and gets
5001 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
5002 = remote_notif_parse (this, notif
, rs
->buf
.data ());
5003 remote_notif_get_pending_events (notif
);
5006 if (thread_count (this) == 0)
5009 error (_("The target is not running (try extended-remote?)"));
5013 /* Report all signals during attach/startup. */
5016 /* If there are already stopped threads, mark them stopped and
5017 report their stops before giving the prompt to the user. */
5018 process_initial_stop_replies (from_tty
);
5020 if (target_can_async_p ())
5021 target_async (true);
5024 /* Give the target a chance to look up symbols. */
5025 for (inferior
*inf
: all_inferiors (this))
5027 /* The inferiors that exist at this point were created from what
5028 was found already running on the remote side, so we know they
5030 gdb_assert (this->has_execution (inf
));
5032 /* No use without a symbol-file. */
5033 if (inf
->pspace
->symfile_object_file
== nullptr)
5036 /* Need to switch to a specific thread, because remote_check_symbols
5037 uses INFERIOR_PTID to set the general thread. */
5038 scoped_restore_current_thread restore_thread
;
5039 thread_info
*thread
= any_thread_of_inferior (inf
);
5040 switch_to_thread (thread
);
5041 this->remote_check_symbols ();
5044 /* Possibly the target has been engaged in a trace run started
5045 previously; find out where things are at. */
5046 if (get_trace_status (current_trace_status ()) != -1)
5048 struct uploaded_tp
*uploaded_tps
= NULL
;
5050 if (current_trace_status ()->running
)
5051 gdb_printf (_("Trace is already running on the target.\n"));
5053 upload_tracepoints (&uploaded_tps
);
5055 merge_uploaded_tracepoints (&uploaded_tps
);
5058 /* Possibly the target has been engaged in a btrace record started
5059 previously; find out where things are at. */
5060 remote_btrace_maybe_reopen ();
5065 /* Start the remote connection and sync state. */
5068 remote_target::start_remote (int from_tty
, int extended_p
)
5070 if (start_remote_1 (from_tty
, extended_p
)
5071 && breakpoints_should_be_inserted_now ())
5072 insert_breakpoints ();
5076 remote_target::connection_string ()
5078 remote_state
*rs
= get_remote_state ();
5080 if (rs
->remote_desc
->name
!= NULL
)
5081 return rs
->remote_desc
->name
;
5086 /* Open a connection to a remote debugger.
5087 NAME is the filename used for communication. */
5090 remote_target::open (const char *name
, int from_tty
)
5092 open_1 (name
, from_tty
, 0);
5095 /* Open a connection to a remote debugger using the extended
5096 remote gdb protocol. NAME is the filename used for communication. */
5099 extended_remote_target::open (const char *name
, int from_tty
)
5101 open_1 (name
, from_tty
, 1 /*extended_p */);
5104 /* Reset all packets back to "unknown support". Called when opening a
5105 new connection to a remote target. */
5108 reset_all_packet_configs_support (void)
5112 for (i
= 0; i
< PACKET_MAX
; i
++)
5113 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5116 /* Initialize all packet configs. */
5119 init_all_packet_configs (void)
5123 for (i
= 0; i
< PACKET_MAX
; i
++)
5125 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5126 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5130 /* Symbol look-up. */
5133 remote_target::remote_check_symbols ()
5138 /* It doesn't make sense to send a qSymbol packet for an inferior that
5139 doesn't have execution, because the remote side doesn't know about
5140 inferiors without execution. */
5141 gdb_assert (target_has_execution ());
5143 if (packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5146 /* Make sure the remote is pointing at the right process. Note
5147 there's no way to select "no process". */
5148 set_general_process ();
5150 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5151 because we need both at the same time. */
5152 gdb::char_vector
msg (get_remote_packet_size ());
5153 gdb::char_vector
reply (get_remote_packet_size ());
5155 /* Invite target to request symbol lookups. */
5157 putpkt ("qSymbol::");
5159 packet_ok (reply
, &remote_protocol_packets
[PACKET_qSymbol
]);
5161 while (startswith (reply
.data (), "qSymbol:"))
5163 struct bound_minimal_symbol sym
;
5166 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5169 sym
= lookup_minimal_symbol (msg
.data (), NULL
, NULL
);
5170 if (sym
.minsym
== NULL
)
5171 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5175 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
5176 CORE_ADDR sym_addr
= sym
.value_address ();
5178 /* If this is a function address, return the start of code
5179 instead of any data function descriptor. */
5180 sym_addr
= gdbarch_convert_from_func_ptr_addr
5181 (target_gdbarch (), sym_addr
, current_inferior ()->top_target ());
5183 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5184 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5187 putpkt (msg
.data ());
5192 static struct serial
*
5193 remote_serial_open (const char *name
)
5195 static int udp_warning
= 0;
5197 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5198 of in ser-tcp.c, because it is the remote protocol assuming that the
5199 serial connection is reliable and not the serial connection promising
5201 if (!udp_warning
&& startswith (name
, "udp:"))
5203 warning (_("The remote protocol may be unreliable over UDP.\n"
5204 "Some events may be lost, rendering further debugging "
5209 return serial_open (name
);
5212 /* Inform the target of our permission settings. The permission flags
5213 work without this, but if the target knows the settings, it can do
5214 a couple things. First, it can add its own check, to catch cases
5215 that somehow manage to get by the permissions checks in target
5216 methods. Second, if the target is wired to disallow particular
5217 settings (for instance, a system in the field that is not set up to
5218 be able to stop at a breakpoint), it can object to any unavailable
5222 remote_target::set_permissions ()
5224 struct remote_state
*rs
= get_remote_state ();
5226 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5227 "WriteReg:%x;WriteMem:%x;"
5228 "InsertBreak:%x;InsertTrace:%x;"
5229 "InsertFastTrace:%x;Stop:%x",
5230 may_write_registers
, may_write_memory
,
5231 may_insert_breakpoints
, may_insert_tracepoints
,
5232 may_insert_fast_tracepoints
, may_stop
);
5234 getpkt (&rs
->buf
, 0);
5236 /* If the target didn't like the packet, warn the user. Do not try
5237 to undo the user's settings, that would just be maddening. */
5238 if (strcmp (rs
->buf
.data (), "OK") != 0)
5239 warning (_("Remote refused setting permissions with: %s"),
5243 /* This type describes each known response to the qSupported
5245 struct protocol_feature
5247 /* The name of this protocol feature. */
5250 /* The default for this protocol feature. */
5251 enum packet_support default_support
;
5253 /* The function to call when this feature is reported, or after
5254 qSupported processing if the feature is not supported.
5255 The first argument points to this structure. The second
5256 argument indicates whether the packet requested support be
5257 enabled, disabled, or probed (or the default, if this function
5258 is being called at the end of processing and this feature was
5259 not reported). The third argument may be NULL; if not NULL, it
5260 is a NUL-terminated string taken from the packet following
5261 this feature's name and an equals sign. */
5262 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5263 enum packet_support
, const char *);
5265 /* The corresponding packet for this feature. Only used if
5266 FUNC is remote_supported_packet. */
5271 remote_supported_packet (remote_target
*remote
,
5272 const struct protocol_feature
*feature
,
5273 enum packet_support support
,
5274 const char *argument
)
5278 warning (_("Remote qSupported response supplied an unexpected value for"
5279 " \"%s\"."), feature
->name
);
5283 remote_protocol_packets
[feature
->packet
].support
= support
;
5287 remote_target::remote_packet_size (const protocol_feature
*feature
,
5288 enum packet_support support
, const char *value
)
5290 struct remote_state
*rs
= get_remote_state ();
5295 if (support
!= PACKET_ENABLE
)
5298 if (value
== NULL
|| *value
== '\0')
5300 warning (_("Remote target reported \"%s\" without a size."),
5306 packet_size
= strtol (value
, &value_end
, 16);
5307 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5309 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5310 feature
->name
, value
);
5314 /* Record the new maximum packet size. */
5315 rs
->explicit_packet_size
= packet_size
;
5319 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5320 enum packet_support support
, const char *value
)
5322 remote
->remote_packet_size (feature
, support
, value
);
5325 static const struct protocol_feature remote_protocol_features
[] = {
5326 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5327 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5328 PACKET_qXfer_auxv
},
5329 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5330 PACKET_qXfer_exec_file
},
5331 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5332 PACKET_qXfer_features
},
5333 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5334 PACKET_qXfer_libraries
},
5335 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5336 PACKET_qXfer_libraries_svr4
},
5337 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5338 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5339 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5340 PACKET_qXfer_memory_map
},
5341 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5342 PACKET_qXfer_osdata
},
5343 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5344 PACKET_qXfer_threads
},
5345 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5346 PACKET_qXfer_traceframe_info
},
5347 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5348 PACKET_QPassSignals
},
5349 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5350 PACKET_QCatchSyscalls
},
5351 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5352 PACKET_QProgramSignals
},
5353 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5354 PACKET_QSetWorkingDir
},
5355 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5356 PACKET_QStartupWithShell
},
5357 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5358 PACKET_QEnvironmentHexEncoded
},
5359 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5360 PACKET_QEnvironmentReset
},
5361 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5362 PACKET_QEnvironmentUnset
},
5363 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5364 PACKET_QStartNoAckMode
},
5365 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5366 PACKET_multiprocess_feature
},
5367 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5368 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5369 PACKET_qXfer_siginfo_read
},
5370 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5371 PACKET_qXfer_siginfo_write
},
5372 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5373 PACKET_ConditionalTracepoints
},
5374 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5375 PACKET_ConditionalBreakpoints
},
5376 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5377 PACKET_BreakpointCommands
},
5378 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5379 PACKET_FastTracepoints
},
5380 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5381 PACKET_StaticTracepoints
},
5382 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5383 PACKET_InstallInTrace
},
5384 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5385 PACKET_DisconnectedTracing_feature
},
5386 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5388 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5390 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5391 PACKET_TracepointSource
},
5392 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5394 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5395 PACKET_EnableDisableTracepoints_feature
},
5396 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5397 PACKET_qXfer_fdpic
},
5398 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5400 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5401 PACKET_QDisableRandomization
},
5402 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5403 { "QTBuffer:size", PACKET_DISABLE
,
5404 remote_supported_packet
, PACKET_QTBuffer_size
},
5405 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5406 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5407 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5408 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5409 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5410 PACKET_qXfer_btrace
},
5411 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5412 PACKET_qXfer_btrace_conf
},
5413 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5414 PACKET_Qbtrace_conf_bts_size
},
5415 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5416 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5417 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5418 PACKET_fork_event_feature
},
5419 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5420 PACKET_vfork_event_feature
},
5421 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5422 PACKET_exec_event_feature
},
5423 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5424 PACKET_Qbtrace_conf_pt_size
},
5425 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5426 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5427 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5428 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5429 PACKET_memory_tagging_feature
},
5432 static char *remote_support_xml
;
5434 /* Register string appended to "xmlRegisters=" in qSupported query. */
5437 register_remote_support_xml (const char *xml
)
5439 #if defined(HAVE_LIBEXPAT)
5440 if (remote_support_xml
== NULL
)
5441 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5444 char *copy
= xstrdup (remote_support_xml
+ 13);
5446 char *p
= strtok_r (copy
, ",", &saveptr
);
5450 if (strcmp (p
, xml
) == 0)
5457 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5460 remote_support_xml
= reconcat (remote_support_xml
,
5461 remote_support_xml
, ",", xml
,
5468 remote_query_supported_append (std::string
*msg
, const char *append
)
5472 msg
->append (append
);
5476 remote_target::remote_query_supported ()
5478 struct remote_state
*rs
= get_remote_state ();
5481 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5483 /* The packet support flags are handled differently for this packet
5484 than for most others. We treat an error, a disabled packet, and
5485 an empty response identically: any features which must be reported
5486 to be used will be automatically disabled. An empty buffer
5487 accomplishes this, since that is also the representation for a list
5488 containing no features. */
5491 if (packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5495 if (packet_set_cmd_state (PACKET_multiprocess_feature
) != AUTO_BOOLEAN_FALSE
)
5496 remote_query_supported_append (&q
, "multiprocess+");
5498 if (packet_set_cmd_state (PACKET_swbreak_feature
) != AUTO_BOOLEAN_FALSE
)
5499 remote_query_supported_append (&q
, "swbreak+");
5500 if (packet_set_cmd_state (PACKET_hwbreak_feature
) != AUTO_BOOLEAN_FALSE
)
5501 remote_query_supported_append (&q
, "hwbreak+");
5503 remote_query_supported_append (&q
, "qRelocInsn+");
5505 if (packet_set_cmd_state (PACKET_fork_event_feature
)
5506 != AUTO_BOOLEAN_FALSE
)
5507 remote_query_supported_append (&q
, "fork-events+");
5508 if (packet_set_cmd_state (PACKET_vfork_event_feature
)
5509 != AUTO_BOOLEAN_FALSE
)
5510 remote_query_supported_append (&q
, "vfork-events+");
5511 if (packet_set_cmd_state (PACKET_exec_event_feature
)
5512 != AUTO_BOOLEAN_FALSE
)
5513 remote_query_supported_append (&q
, "exec-events+");
5515 if (packet_set_cmd_state (PACKET_vContSupported
) != AUTO_BOOLEAN_FALSE
)
5516 remote_query_supported_append (&q
, "vContSupported+");
5518 if (packet_set_cmd_state (PACKET_QThreadEvents
) != AUTO_BOOLEAN_FALSE
)
5519 remote_query_supported_append (&q
, "QThreadEvents+");
5521 if (packet_set_cmd_state (PACKET_no_resumed
) != AUTO_BOOLEAN_FALSE
)
5522 remote_query_supported_append (&q
, "no-resumed+");
5524 if (packet_set_cmd_state (PACKET_memory_tagging_feature
)
5525 != AUTO_BOOLEAN_FALSE
)
5526 remote_query_supported_append (&q
, "memory-tagging+");
5528 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5529 the qSupported:xmlRegisters=i386 handling. */
5530 if (remote_support_xml
!= NULL
5531 && packet_support (PACKET_qXfer_features
) != PACKET_DISABLE
)
5532 remote_query_supported_append (&q
, remote_support_xml
);
5534 q
= "qSupported:" + q
;
5535 putpkt (q
.c_str ());
5537 getpkt (&rs
->buf
, 0);
5539 /* If an error occured, warn, but do not return - just reset the
5540 buffer to empty and go on to disable features. */
5541 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
5544 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
5549 memset (seen
, 0, sizeof (seen
));
5551 next
= rs
->buf
.data ();
5554 enum packet_support is_supported
;
5555 char *p
, *end
, *name_end
, *value
;
5557 /* First separate out this item from the rest of the packet. If
5558 there's another item after this, we overwrite the separator
5559 (terminated strings are much easier to work with). */
5561 end
= strchr (p
, ';');
5564 end
= p
+ strlen (p
);
5574 warning (_("empty item in \"qSupported\" response"));
5579 name_end
= strchr (p
, '=');
5582 /* This is a name=value entry. */
5583 is_supported
= PACKET_ENABLE
;
5584 value
= name_end
+ 1;
5593 is_supported
= PACKET_ENABLE
;
5597 is_supported
= PACKET_DISABLE
;
5601 is_supported
= PACKET_SUPPORT_UNKNOWN
;
5605 warning (_("unrecognized item \"%s\" "
5606 "in \"qSupported\" response"), p
);
5612 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5613 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
5615 const struct protocol_feature
*feature
;
5618 feature
= &remote_protocol_features
[i
];
5619 feature
->func (this, feature
, is_supported
, value
);
5624 /* If we increased the packet size, make sure to increase the global
5625 buffer size also. We delay this until after parsing the entire
5626 qSupported packet, because this is the same buffer we were
5628 if (rs
->buf
.size () < rs
->explicit_packet_size
)
5629 rs
->buf
.resize (rs
->explicit_packet_size
);
5631 /* Handle the defaults for unmentioned features. */
5632 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5635 const struct protocol_feature
*feature
;
5637 feature
= &remote_protocol_features
[i
];
5638 feature
->func (this, feature
, feature
->default_support
, NULL
);
5642 /* Serial QUIT handler for the remote serial descriptor.
5644 Defers handling a Ctrl-C until we're done with the current
5645 command/response packet sequence, unless:
5647 - We're setting up the connection. Don't send a remote interrupt
5648 request, as we're not fully synced yet. Quit immediately
5651 - The target has been resumed in the foreground
5652 (target_terminal::is_ours is false) with a synchronous resume
5653 packet, and we're blocked waiting for the stop reply, thus a
5654 Ctrl-C should be immediately sent to the target.
5656 - We get a second Ctrl-C while still within the same serial read or
5657 write. In that case the serial is seemingly wedged --- offer to
5660 - We see a second Ctrl-C without target response, after having
5661 previously interrupted the target. In that case the target/stub
5662 is probably wedged --- offer to quit/disconnect.
5666 remote_target::remote_serial_quit_handler ()
5668 struct remote_state
*rs
= get_remote_state ();
5670 if (check_quit_flag ())
5672 /* If we're starting up, we're not fully synced yet. Quit
5674 if (rs
->starting_up
)
5676 else if (rs
->got_ctrlc_during_io
)
5678 if (query (_("The target is not responding to GDB commands.\n"
5679 "Stop debugging it? ")))
5680 remote_unpush_and_throw (this);
5682 /* If ^C has already been sent once, offer to disconnect. */
5683 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
5685 /* All-stop protocol, and blocked waiting for stop reply. Send
5686 an interrupt request. */
5687 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
5688 target_interrupt ();
5690 rs
->got_ctrlc_during_io
= 1;
5694 /* The remote_target that is current while the quit handler is
5695 overridden with remote_serial_quit_handler. */
5696 static remote_target
*curr_quit_handler_target
;
5699 remote_serial_quit_handler ()
5701 curr_quit_handler_target
->remote_serial_quit_handler ();
5704 /* Remove the remote target from the target stack of each inferior
5705 that is using it. Upper targets depend on it so remove them
5709 remote_unpush_target (remote_target
*target
)
5711 /* We have to unpush the target from all inferiors, even those that
5713 scoped_restore_current_inferior restore_current_inferior
;
5715 for (inferior
*inf
: all_inferiors (target
))
5717 switch_to_inferior_no_thread (inf
);
5718 pop_all_targets_at_and_above (process_stratum
);
5719 generic_mourn_inferior ();
5722 /* Don't rely on target_close doing this when the target is popped
5723 from the last remote inferior above, because something may be
5724 holding a reference to the target higher up on the stack, meaning
5725 target_close won't be called yet. We lost the connection to the
5726 target, so clear these now, otherwise we may later throw
5727 TARGET_CLOSE_ERROR while trying to tell the remote target to
5729 fileio_handles_invalidate_target (target
);
5733 remote_unpush_and_throw (remote_target
*target
)
5735 remote_unpush_target (target
);
5736 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
5740 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
5742 remote_target
*curr_remote
= get_current_remote_target ();
5745 error (_("To open a remote debug connection, you need to specify what\n"
5746 "serial device is attached to the remote system\n"
5747 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
5749 /* If we're connected to a running target, target_preopen will kill it.
5750 Ask this question first, before target_preopen has a chance to kill
5752 if (curr_remote
!= NULL
&& !target_has_execution ())
5755 && !query (_("Already connected to a remote target. Disconnect? ")))
5756 error (_("Still connected."));
5759 /* Here the possibly existing remote target gets unpushed. */
5760 target_preopen (from_tty
);
5762 remote_fileio_reset ();
5763 reopen_exec_file ();
5764 reread_symbols (from_tty
);
5766 remote_target
*remote
5767 = (extended_p
? new extended_remote_target () : new remote_target ());
5768 target_ops_up
target_holder (remote
);
5770 remote_state
*rs
= remote
->get_remote_state ();
5772 /* See FIXME above. */
5773 if (!target_async_permitted
)
5774 rs
->wait_forever_enabled_p
= 1;
5776 rs
->remote_desc
= remote_serial_open (name
);
5777 if (!rs
->remote_desc
)
5778 perror_with_name (name
);
5780 if (baud_rate
!= -1)
5782 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
5784 /* The requested speed could not be set. Error out to
5785 top level after closing remote_desc. Take care to
5786 set remote_desc to NULL to avoid closing remote_desc
5788 serial_close (rs
->remote_desc
);
5789 rs
->remote_desc
= NULL
;
5790 perror_with_name (name
);
5794 serial_setparity (rs
->remote_desc
, serial_parity
);
5795 serial_raw (rs
->remote_desc
);
5797 /* If there is something sitting in the buffer we might take it as a
5798 response to a command, which would be bad. */
5799 serial_flush_input (rs
->remote_desc
);
5803 gdb_puts ("Remote debugging using ");
5808 /* Switch to using the remote target now. */
5809 current_inferior ()->push_target (std::move (target_holder
));
5811 /* Register extra event sources in the event loop. */
5812 rs
->remote_async_inferior_event_token
5813 = create_async_event_handler (remote_async_inferior_event_handler
, nullptr,
5815 rs
->notif_state
= remote_notif_state_allocate (remote
);
5817 /* Reset the target state; these things will be queried either by
5818 remote_query_supported or as they are needed. */
5819 reset_all_packet_configs_support ();
5820 rs
->explicit_packet_size
= 0;
5822 rs
->extended
= extended_p
;
5823 rs
->waiting_for_stop_reply
= 0;
5824 rs
->ctrlc_pending_p
= 0;
5825 rs
->got_ctrlc_during_io
= 0;
5827 rs
->general_thread
= not_sent_ptid
;
5828 rs
->continue_thread
= not_sent_ptid
;
5829 rs
->remote_traceframe_number
= -1;
5831 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
5833 /* Probe for ability to use "ThreadInfo" query, as required. */
5834 rs
->use_threadinfo_query
= 1;
5835 rs
->use_threadextra_query
= 1;
5837 rs
->readahead_cache
.invalidate ();
5839 if (target_async_permitted
)
5841 /* FIXME: cagney/1999-09-23: During the initial connection it is
5842 assumed that the target is already ready and able to respond to
5843 requests. Unfortunately remote_start_remote() eventually calls
5844 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
5845 around this. Eventually a mechanism that allows
5846 wait_for_inferior() to expect/get timeouts will be
5848 rs
->wait_forever_enabled_p
= 0;
5851 /* First delete any symbols previously loaded from shared libraries. */
5852 no_shared_libraries (NULL
, 0);
5854 /* Start the remote connection. If error() or QUIT, discard this
5855 target (we'd otherwise be in an inconsistent state) and then
5856 propogate the error on up the exception chain. This ensures that
5857 the caller doesn't stumble along blindly assuming that the
5858 function succeeded. The CLI doesn't have this problem but other
5859 UI's, such as MI do.
5861 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5862 this function should return an error indication letting the
5863 caller restore the previous state. Unfortunately the command
5864 ``target remote'' is directly wired to this function making that
5865 impossible. On a positive note, the CLI side of this problem has
5866 been fixed - the function set_cmd_context() makes it possible for
5867 all the ``target ....'' commands to share a common callback
5868 function. See cli-dump.c. */
5873 remote
->start_remote (from_tty
, extended_p
);
5875 catch (const gdb_exception
&ex
)
5877 /* Pop the partially set up target - unless something else did
5878 already before throwing the exception. */
5879 if (ex
.error
!= TARGET_CLOSE_ERROR
)
5880 remote_unpush_target (remote
);
5885 remote_btrace_reset (rs
);
5887 if (target_async_permitted
)
5888 rs
->wait_forever_enabled_p
= 1;
5891 /* Determine if WS represents a fork status. */
5894 is_fork_status (target_waitkind kind
)
5896 return (kind
== TARGET_WAITKIND_FORKED
5897 || kind
== TARGET_WAITKIND_VFORKED
);
5900 /* Return THREAD's pending status if it is a pending fork parent, else
5903 static const target_waitstatus
*
5904 thread_pending_fork_status (struct thread_info
*thread
)
5906 const target_waitstatus
&ws
5907 = (thread
->has_pending_waitstatus ()
5908 ? thread
->pending_waitstatus ()
5909 : thread
->pending_follow
);
5911 if (!is_fork_status (ws
.kind ()))
5917 /* Detach the specified process. */
5920 remote_target::remote_detach_pid (int pid
)
5922 struct remote_state
*rs
= get_remote_state ();
5924 /* This should not be necessary, but the handling for D;PID in
5925 GDBserver versions prior to 8.2 incorrectly assumes that the
5926 selected process points to the same process we're detaching,
5927 leading to misbehavior (and possibly GDBserver crashing) when it
5928 does not. Since it's easy and cheap, work around it by forcing
5929 GDBserver to select GDB's current process. */
5930 set_general_process ();
5932 if (remote_multi_process_p (rs
))
5933 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
5935 strcpy (rs
->buf
.data (), "D");
5938 getpkt (&rs
->buf
, 0);
5940 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
5942 else if (rs
->buf
[0] == '\0')
5943 error (_("Remote doesn't know how to detach"));
5945 error (_("Can't detach process."));
5948 /* This detaches a program to which we previously attached, using
5949 inferior_ptid to identify the process. After this is done, GDB
5950 can be used to debug some other program. We better not have left
5951 any breakpoints in the target program or it'll die when it hits
5955 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
5957 int pid
= inferior_ptid
.pid ();
5958 struct remote_state
*rs
= get_remote_state ();
5961 if (!target_has_execution ())
5962 error (_("No process to detach from."));
5964 target_announce_detach (from_tty
);
5966 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
5968 /* If we're in breakpoints-always-inserted mode, or the inferior
5969 is running, we have to remove breakpoints before detaching.
5970 We don't do this in common code instead because not all
5971 targets support removing breakpoints while the target is
5972 running. The remote target / gdbserver does, though. */
5973 remove_breakpoints_inf (current_inferior ());
5976 /* Tell the remote target to detach. */
5977 remote_detach_pid (pid
);
5979 /* Exit only if this is the only active inferior. */
5980 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
5981 gdb_puts (_("Ending remote debugging.\n"));
5983 /* See if any thread of the inferior we are detaching has a pending fork
5984 status. In that case, we must detach from the child resulting from
5986 for (thread_info
*thread
: inf
->non_exited_threads ())
5988 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
5993 remote_detach_pid (ws
->child_ptid ().pid ());
5996 /* Check also for any pending fork events in the stop reply queue. */
5997 remote_notif_get_pending_events (¬if_client_stop
);
5998 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
6000 if (reply
->ptid
.pid () != pid
)
6003 if (!is_fork_status (reply
->ws
.kind ()))
6006 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
6009 thread_info
*tp
= find_thread_ptid (this, inferior_ptid
);
6011 /* Check to see if we are detaching a fork parent. Note that if we
6012 are detaching a fork child, tp == NULL. */
6013 is_fork_parent
= (tp
!= NULL
6014 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
6016 /* If doing detach-on-fork, we don't mourn, because that will delete
6017 breakpoints that should be available for the followed inferior. */
6018 if (!is_fork_parent
)
6020 /* Save the pid as a string before mourning, since that will
6021 unpush the remote target, and we need the string after. */
6022 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6024 target_mourn_inferior (inferior_ptid
);
6025 if (print_inferior_events
)
6026 gdb_printf (_("[Inferior %d (%s) detached]\n"),
6027 inf
->num
, infpid
.c_str ());
6031 switch_to_no_thread ();
6032 detach_inferior (current_inferior ());
6037 remote_target::detach (inferior
*inf
, int from_tty
)
6039 remote_detach_1 (inf
, from_tty
);
6043 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6045 remote_detach_1 (inf
, from_tty
);
6048 /* Target follow-fork function for remote targets. On entry, and
6049 at return, the current inferior is the fork parent.
6051 Note that although this is currently only used for extended-remote,
6052 it is named remote_follow_fork in anticipation of using it for the
6053 remote target as well. */
6056 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6057 target_waitkind fork_kind
, bool follow_child
,
6060 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6061 fork_kind
, follow_child
, detach_fork
);
6063 struct remote_state
*rs
= get_remote_state ();
6065 if ((fork_kind
== TARGET_WAITKIND_FORKED
&& remote_fork_event_p (rs
))
6066 || (fork_kind
== TARGET_WAITKIND_VFORKED
&& remote_vfork_event_p (rs
)))
6068 /* When following the parent and detaching the child, we detach
6069 the child here. For the case of following the child and
6070 detaching the parent, the detach is done in the target-
6071 independent follow fork code in infrun.c. We can't use
6072 target_detach when detaching an unfollowed child because
6073 the client side doesn't know anything about the child. */
6074 if (detach_fork
&& !follow_child
)
6076 /* Detach the fork child. */
6077 remote_detach_pid (child_ptid
.pid ());
6082 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6083 in the program space of the new inferior. */
6086 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6087 const char *execd_pathname
)
6089 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6091 /* We know that this is a target file name, so if it has the "target:"
6092 prefix we strip it off before saving it in the program space. */
6093 if (is_target_filename (execd_pathname
))
6094 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6096 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6099 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6102 remote_target::disconnect (const char *args
, int from_tty
)
6105 error (_("Argument given to \"disconnect\" when remotely debugging."));
6107 /* Make sure we unpush even the extended remote targets. Calling
6108 target_mourn_inferior won't unpush, and
6109 remote_target::mourn_inferior won't unpush if there is more than
6110 one inferior left. */
6111 remote_unpush_target (this);
6114 gdb_puts ("Ending remote debugging.\n");
6117 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6118 be chatty about it. */
6121 extended_remote_target::attach (const char *args
, int from_tty
)
6123 struct remote_state
*rs
= get_remote_state ();
6125 char *wait_status
= NULL
;
6127 pid
= parse_pid_to_attach (args
);
6129 /* Remote PID can be freely equal to getpid, do not check it here the same
6130 way as in other targets. */
6132 if (packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6133 error (_("This target does not support attaching to a process"));
6135 target_announce_attach (from_tty
, pid
);
6137 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6139 getpkt (&rs
->buf
, 0);
6141 switch (packet_ok (rs
->buf
,
6142 &remote_protocol_packets
[PACKET_vAttach
]))
6145 if (!target_is_non_stop_p ())
6147 /* Save the reply for later. */
6148 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6149 strcpy (wait_status
, rs
->buf
.data ());
6151 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6152 error (_("Attaching to %s failed with: %s"),
6153 target_pid_to_str (ptid_t (pid
)).c_str (),
6156 case PACKET_UNKNOWN
:
6157 error (_("This target does not support attaching to a process"));
6159 error (_("Attaching to %s failed"),
6160 target_pid_to_str (ptid_t (pid
)).c_str ());
6163 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6165 inferior_ptid
= ptid_t (pid
);
6167 if (target_is_non_stop_p ())
6169 /* Get list of threads. */
6170 update_thread_list ();
6172 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6173 if (thread
!= nullptr)
6174 switch_to_thread (thread
);
6176 /* Invalidate our notion of the remote current thread. */
6177 record_currthread (rs
, minus_one_ptid
);
6181 /* Now, if we have thread information, update the main thread's
6183 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6185 /* Add the main thread to the thread list. We add the thread
6186 silently in this case (the final true parameter). */
6187 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6189 switch_to_thread (thr
);
6192 /* Next, if the target can specify a description, read it. We do
6193 this before anything involving memory or registers. */
6194 target_find_description ();
6196 if (!target_is_non_stop_p ())
6198 /* Use the previously fetched status. */
6199 gdb_assert (wait_status
!= NULL
);
6201 struct notif_event
*reply
6202 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6204 push_stop_reply ((struct stop_reply
*) reply
);
6208 gdb_assert (wait_status
== NULL
);
6210 gdb_assert (target_can_async_p ());
6214 /* Implementation of the to_post_attach method. */
6217 extended_remote_target::post_attach (int pid
)
6219 /* Get text, data & bss offsets. */
6222 /* In certain cases GDB might not have had the chance to start
6223 symbol lookup up until now. This could happen if the debugged
6224 binary is not using shared libraries, the vsyscall page is not
6225 present (on Linux) and the binary itself hadn't changed since the
6226 debugging process was started. */
6227 if (current_program_space
->symfile_object_file
!= NULL
)
6228 remote_check_symbols();
6232 /* Check for the availability of vCont. This function should also check
6236 remote_target::remote_vcont_probe ()
6238 remote_state
*rs
= get_remote_state ();
6241 strcpy (rs
->buf
.data (), "vCont?");
6243 getpkt (&rs
->buf
, 0);
6244 buf
= rs
->buf
.data ();
6246 /* Make sure that the features we assume are supported. */
6247 if (startswith (buf
, "vCont"))
6250 int support_c
, support_C
;
6252 rs
->supports_vCont
.s
= 0;
6253 rs
->supports_vCont
.S
= 0;
6256 rs
->supports_vCont
.t
= 0;
6257 rs
->supports_vCont
.r
= 0;
6258 while (p
&& *p
== ';')
6261 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6262 rs
->supports_vCont
.s
= 1;
6263 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6264 rs
->supports_vCont
.S
= 1;
6265 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6267 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6269 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6270 rs
->supports_vCont
.t
= 1;
6271 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6272 rs
->supports_vCont
.r
= 1;
6274 p
= strchr (p
, ';');
6277 /* If c, and C are not all supported, we can't use vCont. Clearing
6278 BUF will make packet_ok disable the packet. */
6279 if (!support_c
|| !support_C
)
6283 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vCont
]);
6284 rs
->supports_vCont_probed
= true;
6287 /* Helper function for building "vCont" resumptions. Write a
6288 resumption to P. ENDP points to one-passed-the-end of the buffer
6289 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6290 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6291 resumed thread should be single-stepped and/or signalled. If PTID
6292 equals minus_one_ptid, then all threads are resumed; if PTID
6293 represents a process, then all threads of the process are
6297 remote_target::append_resumption (char *p
, char *endp
,
6298 ptid_t ptid
, int step
, gdb_signal siggnal
)
6300 struct remote_state
*rs
= get_remote_state ();
6302 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6303 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6305 /* GDB is willing to range step. */
6306 && use_range_stepping
6307 /* Target supports range stepping. */
6308 && rs
->supports_vCont
.r
6309 /* We don't currently support range stepping multiple
6310 threads with a wildcard (though the protocol allows it,
6311 so stubs shouldn't make an active effort to forbid
6313 && !(remote_multi_process_p (rs
) && ptid
.is_pid ()))
6315 struct thread_info
*tp
;
6317 if (ptid
== minus_one_ptid
)
6319 /* If we don't know about the target thread's tid, then
6320 we're resuming magic_null_ptid (see caller). */
6321 tp
= find_thread_ptid (this, magic_null_ptid
);
6324 tp
= find_thread_ptid (this, ptid
);
6325 gdb_assert (tp
!= NULL
);
6327 if (tp
->control
.may_range_step
)
6329 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6331 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6332 phex_nz (tp
->control
.step_range_start
,
6334 phex_nz (tp
->control
.step_range_end
,
6338 p
+= xsnprintf (p
, endp
- p
, ";s");
6341 p
+= xsnprintf (p
, endp
- p
, ";s");
6342 else if (siggnal
!= GDB_SIGNAL_0
)
6343 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6345 p
+= xsnprintf (p
, endp
- p
, ";c");
6347 if (remote_multi_process_p (rs
) && ptid
.is_pid ())
6351 /* All (-1) threads of process. */
6352 nptid
= ptid_t (ptid
.pid (), -1);
6354 p
+= xsnprintf (p
, endp
- p
, ":");
6355 p
= write_ptid (p
, endp
, nptid
);
6357 else if (ptid
!= minus_one_ptid
)
6359 p
+= xsnprintf (p
, endp
- p
, ":");
6360 p
= write_ptid (p
, endp
, ptid
);
6366 /* Clear the thread's private info on resume. */
6369 resume_clear_thread_private_info (struct thread_info
*thread
)
6371 if (thread
->priv
!= NULL
)
6373 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6375 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6376 priv
->watch_data_address
= 0;
6380 /* Append a vCont continue-with-signal action for threads that have a
6381 non-zero stop signal. */
6384 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6387 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6388 if (inferior_ptid
!= thread
->ptid
6389 && thread
->stop_signal () != GDB_SIGNAL_0
)
6391 p
= append_resumption (p
, endp
, thread
->ptid
,
6392 0, thread
->stop_signal ());
6393 thread
->set_stop_signal (GDB_SIGNAL_0
);
6394 resume_clear_thread_private_info (thread
);
6400 /* Set the target running, using the packets that use Hc
6404 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6407 struct remote_state
*rs
= get_remote_state ();
6410 rs
->last_sent_signal
= siggnal
;
6411 rs
->last_sent_step
= step
;
6413 /* The c/s/C/S resume packets use Hc, so set the continue
6415 if (ptid
== minus_one_ptid
)
6416 set_continue_thread (any_thread_ptid
);
6418 set_continue_thread (ptid
);
6420 for (thread_info
*thread
: all_non_exited_threads (this))
6421 resume_clear_thread_private_info (thread
);
6423 buf
= rs
->buf
.data ();
6424 if (::execution_direction
== EXEC_REVERSE
)
6426 /* We don't pass signals to the target in reverse exec mode. */
6427 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6428 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6431 if (step
&& packet_support (PACKET_bs
) == PACKET_DISABLE
)
6432 error (_("Remote reverse-step not supported."));
6433 if (!step
&& packet_support (PACKET_bc
) == PACKET_DISABLE
)
6434 error (_("Remote reverse-continue not supported."));
6436 strcpy (buf
, step
? "bs" : "bc");
6438 else if (siggnal
!= GDB_SIGNAL_0
)
6440 buf
[0] = step
? 'S' : 'C';
6441 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6442 buf
[2] = tohex (((int) siggnal
) & 0xf);
6446 strcpy (buf
, step
? "s" : "c");
6451 /* Resume the remote inferior by using a "vCont" packet. SCOPE_PTID,
6452 STEP, and SIGGNAL have the same meaning as in target_resume. This
6453 function returns non-zero iff it resumes the inferior.
6455 This function issues a strict subset of all possible vCont commands
6459 remote_target::remote_resume_with_vcont (ptid_t scope_ptid
, int step
,
6460 enum gdb_signal siggnal
)
6462 struct remote_state
*rs
= get_remote_state ();
6466 /* No reverse execution actions defined for vCont. */
6467 if (::execution_direction
== EXEC_REVERSE
)
6470 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
6471 remote_vcont_probe ();
6473 if (packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6476 p
= rs
->buf
.data ();
6477 endp
= p
+ get_remote_packet_size ();
6479 /* If we could generate a wider range of packets, we'd have to worry
6480 about overflowing BUF. Should there be a generic
6481 "multi-part-packet" packet? */
6483 p
+= xsnprintf (p
, endp
- p
, "vCont");
6485 if (scope_ptid
== magic_null_ptid
)
6487 /* MAGIC_NULL_PTID means that we don't have any active threads,
6488 so we don't have any TID numbers the inferior will
6489 understand. Make sure to only send forms that do not specify
6491 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6493 else if (scope_ptid
== minus_one_ptid
|| scope_ptid
.is_pid ())
6495 /* Resume all threads (of all processes, or of a single
6496 process), with preference for INFERIOR_PTID. This assumes
6497 inferior_ptid belongs to the set of all threads we are about
6499 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6501 /* Step inferior_ptid, with or without signal. */
6502 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6505 /* Also pass down any pending signaled resumption for other
6506 threads not the current. */
6507 p
= append_pending_thread_resumptions (p
, endp
, scope_ptid
);
6509 /* And continue others without a signal. */
6510 append_resumption (p
, endp
, scope_ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
6514 /* Scheduler locking; resume only SCOPE_PTID. */
6515 append_resumption (p
, endp
, scope_ptid
, step
, siggnal
);
6518 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
6521 if (target_is_non_stop_p ())
6523 /* In non-stop, the stub replies to vCont with "OK". The stop
6524 reply will be reported asynchronously by means of a `%Stop'
6526 getpkt (&rs
->buf
, 0);
6527 if (strcmp (rs
->buf
.data (), "OK") != 0)
6528 error (_("Unexpected vCont reply in non-stop mode: %s"),
6535 /* Tell the remote machine to resume. */
6538 remote_target::resume (ptid_t scope_ptid
, int step
, enum gdb_signal siggnal
)
6540 struct remote_state
*rs
= get_remote_state ();
6542 /* When connected in non-stop mode, the core resumes threads
6543 individually. Resuming remote threads directly in target_resume
6544 would thus result in sending one packet per thread. Instead, to
6545 minimize roundtrip latency, here we just store the resume
6546 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
6547 resumption will be done in remote_target::commit_resume, where we'll be
6548 able to do vCont action coalescing. */
6549 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
6551 remote_thread_info
*remote_thr
6552 = get_remote_thread_info (inferior_thread ());
6554 /* We don't expect the core to ask to resume an already resumed (from
6555 its point of view) thread. */
6556 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
6558 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
6560 /* There's actually nothing that says that the core can't
6561 request a wildcard resume in non-stop mode, though. It's
6562 just that we know it doesn't currently, so we don't bother
6564 gdb_assert (scope_ptid
== inferior_ptid
);
6568 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6569 (explained in remote-notif.c:handle_notification) so
6570 remote_notif_process is not called. We need find a place where
6571 it is safe to start a 'vNotif' sequence. It is good to do it
6572 before resuming inferior, because inferior was stopped and no RSP
6573 traffic at that moment. */
6574 if (!target_is_non_stop_p ())
6575 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
6577 rs
->last_resume_exec_dir
= ::execution_direction
;
6579 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6580 if (!remote_resume_with_vcont (scope_ptid
, step
, siggnal
))
6581 remote_resume_with_hc (scope_ptid
, step
, siggnal
);
6583 /* Update resumed state tracked by the remote target. */
6584 for (thread_info
*tp
: all_non_exited_threads (this, scope_ptid
))
6585 get_remote_thread_info (tp
)->set_resumed ();
6587 /* We've just told the target to resume. The remote server will
6588 wait for the inferior to stop, and then send a stop reply. In
6589 the mean time, we can't start another command/query ourselves
6590 because the stub wouldn't be ready to process it. This applies
6591 only to the base all-stop protocol, however. In non-stop (which
6592 only supports vCont), the stub replies with an "OK", and is
6593 immediate able to process further serial input. */
6594 if (!target_is_non_stop_p ())
6595 rs
->waiting_for_stop_reply
= 1;
6598 /* Private per-inferior info for target remote processes. */
6600 struct remote_inferior
: public private_inferior
6602 /* Whether we can send a wildcard vCont for this process. */
6603 bool may_wildcard_vcont
= true;
6606 /* Get the remote private inferior data associated to INF. */
6608 static remote_inferior
*
6609 get_remote_inferior (inferior
*inf
)
6611 if (inf
->priv
== NULL
)
6612 inf
->priv
.reset (new remote_inferior
);
6614 return static_cast<remote_inferior
*> (inf
->priv
.get ());
6617 /* Class used to track the construction of a vCont packet in the
6618 outgoing packet buffer. This is used to send multiple vCont
6619 packets if we have more actions than would fit a single packet. */
6624 explicit vcont_builder (remote_target
*remote
)
6631 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
6636 /* The remote target. */
6637 remote_target
*m_remote
;
6639 /* Pointer to the first action. P points here if no action has been
6641 char *m_first_action
;
6643 /* Where the next action will be appended. */
6646 /* The end of the buffer. Must never write past this. */
6650 /* Prepare the outgoing buffer for a new vCont packet. */
6653 vcont_builder::restart ()
6655 struct remote_state
*rs
= m_remote
->get_remote_state ();
6657 m_p
= rs
->buf
.data ();
6658 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
6659 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
6660 m_first_action
= m_p
;
6663 /* If the vCont packet being built has any action, send it to the
6667 vcont_builder::flush ()
6669 struct remote_state
*rs
;
6671 if (m_p
== m_first_action
)
6674 rs
= m_remote
->get_remote_state ();
6675 m_remote
->putpkt (rs
->buf
);
6676 m_remote
->getpkt (&rs
->buf
, 0);
6677 if (strcmp (rs
->buf
.data (), "OK") != 0)
6678 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
6681 /* The largest action is range-stepping, with its two addresses. This
6682 is more than sufficient. If a new, bigger action is created, it'll
6683 quickly trigger a failed assertion in append_resumption (and we'll
6685 #define MAX_ACTION_SIZE 200
6687 /* Append a new vCont action in the outgoing packet being built. If
6688 the action doesn't fit the packet along with previous actions, push
6689 what we've got so far to the remote end and start over a new vCont
6690 packet (with the new action). */
6693 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
6695 char buf
[MAX_ACTION_SIZE
+ 1];
6697 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
6698 ptid
, step
, siggnal
);
6700 /* Check whether this new action would fit in the vCont packet along
6701 with previous actions. If not, send what we've got so far and
6702 start a new vCont packet. */
6703 size_t rsize
= endp
- buf
;
6704 if (rsize
> m_endp
- m_p
)
6709 /* Should now fit. */
6710 gdb_assert (rsize
<= m_endp
- m_p
);
6713 memcpy (m_p
, buf
, rsize
);
6718 /* to_commit_resume implementation. */
6721 remote_target::commit_resumed ()
6723 /* If connected in all-stop mode, we'd send the remote resume
6724 request directly from remote_resume. Likewise if
6725 reverse-debugging, as there are no defined vCont actions for
6726 reverse execution. */
6727 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
6730 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6731 instead of resuming all threads of each process individually.
6732 However, if any thread of a process must remain halted, we can't
6733 send wildcard resumes and must send one action per thread.
6735 Care must be taken to not resume threads/processes the server
6736 side already told us are stopped, but the core doesn't know about
6737 yet, because the events are still in the vStopped notification
6740 #1 => vCont s:p1.1;c
6742 #3 <= %Stopped T05 p1.1
6747 #8 (infrun handles the stop for p1.1 and continues stepping)
6748 #9 => vCont s:p1.1;c
6750 The last vCont above would resume thread p1.2 by mistake, because
6751 the server has no idea that the event for p1.2 had not been
6754 The server side must similarly ignore resume actions for the
6755 thread that has a pending %Stopped notification (and any other
6756 threads with events pending), until GDB acks the notification
6757 with vStopped. Otherwise, e.g., the following case is
6760 #1 => g (or any other packet)
6762 #3 <= %Stopped T05 p1.2
6763 #4 => vCont s:p1.1;c
6766 Above, the server must not resume thread p1.2. GDB can't know
6767 that p1.2 stopped until it acks the %Stopped notification, and
6768 since from GDB's perspective all threads should be running, it
6771 Finally, special care must also be given to handling fork/vfork
6772 events. A (v)fork event actually tells us that two processes
6773 stopped -- the parent and the child. Until we follow the fork,
6774 we must not resume the child. Therefore, if we have a pending
6775 fork follow, we must not send a global wildcard resume action
6776 (vCont;c). We can still send process-wide wildcards though. */
6778 /* Start by assuming a global wildcard (vCont;c) is possible. */
6779 bool may_global_wildcard_vcont
= true;
6781 /* And assume every process is individually wildcard-able too. */
6782 for (inferior
*inf
: all_non_exited_inferiors (this))
6784 remote_inferior
*priv
= get_remote_inferior (inf
);
6786 priv
->may_wildcard_vcont
= true;
6789 /* Check for any pending events (not reported or processed yet) and
6790 disable process and global wildcard resumes appropriately. */
6791 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
6793 bool any_pending_vcont_resume
= false;
6795 for (thread_info
*tp
: all_non_exited_threads (this))
6797 remote_thread_info
*priv
= get_remote_thread_info (tp
);
6799 /* If a thread of a process is not meant to be resumed, then we
6800 can't wildcard that process. */
6801 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
6803 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
6805 /* And if we can't wildcard a process, we can't wildcard
6806 everything either. */
6807 may_global_wildcard_vcont
= false;
6811 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
6812 any_pending_vcont_resume
= true;
6814 /* If a thread is the parent of an unfollowed fork, then we
6815 can't do a global wildcard, as that would resume the fork
6817 if (thread_pending_fork_status (tp
) != nullptr)
6818 may_global_wildcard_vcont
= false;
6821 /* We didn't have any resumed thread pending a vCont resume, so nothing to
6823 if (!any_pending_vcont_resume
)
6826 /* Now let's build the vCont packet(s). Actions must be appended
6827 from narrower to wider scopes (thread -> process -> global). If
6828 we end up with too many actions for a single packet vcont_builder
6829 flushes the current vCont packet to the remote side and starts a
6831 struct vcont_builder
vcont_builder (this);
6833 /* Threads first. */
6834 for (thread_info
*tp
: all_non_exited_threads (this))
6836 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6838 /* If the thread was previously vCont-resumed, no need to send a specific
6839 action for it. If we didn't receive a resume request for it, don't
6840 send an action for it either. */
6841 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
6844 gdb_assert (!thread_is_in_step_over_chain (tp
));
6846 /* We should never be commit-resuming a thread that has a stop reply.
6847 Otherwise, we would end up reporting a stop event for a thread while
6848 it is running on the remote target. */
6849 remote_state
*rs
= get_remote_state ();
6850 for (const auto &stop_reply
: rs
->stop_reply_queue
)
6851 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
6853 const resumed_pending_vcont_info
&info
6854 = remote_thr
->resumed_pending_vcont_info ();
6856 /* Check if we need to send a specific action for this thread. If not,
6857 it will be included in a wildcard resume instead. */
6858 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
6859 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
6860 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
6862 remote_thr
->set_resumed ();
6865 /* Now check whether we can send any process-wide wildcard. This is
6866 to avoid sending a global wildcard in the case nothing is
6867 supposed to be resumed. */
6868 bool any_process_wildcard
= false;
6870 for (inferior
*inf
: all_non_exited_inferiors (this))
6872 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
6874 any_process_wildcard
= true;
6879 if (any_process_wildcard
)
6881 /* If all processes are wildcard-able, then send a single "c"
6882 action, otherwise, send an "all (-1) threads of process"
6883 continue action for each running process, if any. */
6884 if (may_global_wildcard_vcont
)
6886 vcont_builder
.push_action (minus_one_ptid
,
6887 false, GDB_SIGNAL_0
);
6891 for (inferior
*inf
: all_non_exited_inferiors (this))
6893 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
6895 vcont_builder
.push_action (ptid_t (inf
->pid
),
6896 false, GDB_SIGNAL_0
);
6902 vcont_builder
.flush ();
6905 /* Implementation of target_has_pending_events. */
6908 remote_target::has_pending_events ()
6910 if (target_can_async_p ())
6912 remote_state
*rs
= get_remote_state ();
6914 if (async_event_handler_marked (rs
->remote_async_inferior_event_token
))
6917 /* Note that BUFCNT can be negative, indicating sticky
6919 if (rs
->remote_desc
->bufcnt
!= 0)
6927 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6928 thread, all threads of a remote process, or all threads of all
6932 remote_target::remote_stop_ns (ptid_t ptid
)
6934 struct remote_state
*rs
= get_remote_state ();
6935 char *p
= rs
->buf
.data ();
6936 char *endp
= p
+ get_remote_packet_size ();
6938 /* If any thread that needs to stop was resumed but pending a vCont
6939 resume, generate a phony stop_reply. However, first check
6940 whether the thread wasn't resumed with a signal. Generating a
6941 phony stop in that case would result in losing the signal. */
6942 bool needs_commit
= false;
6943 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
6945 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6947 if (remote_thr
->get_resume_state ()
6948 == resume_state::RESUMED_PENDING_VCONT
)
6950 const resumed_pending_vcont_info
&info
6951 = remote_thr
->resumed_pending_vcont_info ();
6952 if (info
.sig
!= GDB_SIGNAL_0
)
6954 /* This signal must be forwarded to the inferior. We
6955 could commit-resume just this thread, but its simpler
6956 to just commit-resume everything. */
6957 needs_commit
= true;
6966 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
6968 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6970 if (remote_thr
->get_resume_state ()
6971 == resume_state::RESUMED_PENDING_VCONT
)
6973 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
6974 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
6976 pulongest (tp
->ptid
.tid ()));
6978 /* Check that the thread wasn't resumed with a signal.
6979 Generating a phony stop would result in losing the
6981 const resumed_pending_vcont_info
&info
6982 = remote_thr
->resumed_pending_vcont_info ();
6983 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
6985 stop_reply
*sr
= new stop_reply ();
6986 sr
->ptid
= tp
->ptid
;
6988 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
6989 sr
->arch
= tp
->inf
->gdbarch
;
6990 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6991 sr
->watch_data_address
= 0;
6993 this->push_stop_reply (sr
);
6995 /* Pretend that this thread was actually resumed on the
6996 remote target, then stopped. If we leave it in the
6997 RESUMED_PENDING_VCONT state and the commit_resumed
6998 method is called while the stop reply is still in the
6999 queue, we'll end up reporting a stop event to the core
7000 for that thread while it is running on the remote
7001 target... that would be bad. */
7002 remote_thr
->set_resumed ();
7006 /* FIXME: This supports_vCont_probed check is a workaround until
7007 packet_support is per-connection. */
7008 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
7009 || !rs
->supports_vCont_probed
)
7010 remote_vcont_probe ();
7012 if (!rs
->supports_vCont
.t
)
7013 error (_("Remote server does not support stopping threads"));
7015 if (ptid
== minus_one_ptid
7016 || (!remote_multi_process_p (rs
) && ptid
.is_pid ()))
7017 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7022 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7025 /* All (-1) threads of process. */
7026 nptid
= ptid_t (ptid
.pid (), -1);
7029 /* Small optimization: if we already have a stop reply for
7030 this thread, no use in telling the stub we want this
7032 if (peek_stop_reply (ptid
))
7038 write_ptid (p
, endp
, nptid
);
7041 /* In non-stop, we get an immediate OK reply. The stop reply will
7042 come in asynchronously by notification. */
7044 getpkt (&rs
->buf
, 0);
7045 if (strcmp (rs
->buf
.data (), "OK") != 0)
7046 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7050 /* All-stop version of target_interrupt. Sends a break or a ^C to
7051 interrupt the remote target. It is undefined which thread of which
7052 process reports the interrupt. */
7055 remote_target::remote_interrupt_as ()
7057 struct remote_state
*rs
= get_remote_state ();
7059 rs
->ctrlc_pending_p
= 1;
7061 /* If the inferior is stopped already, but the core didn't know
7062 about it yet, just ignore the request. The pending stop events
7063 will be collected in remote_wait. */
7064 if (stop_reply_queue_length () > 0)
7067 /* Send interrupt_sequence to remote target. */
7068 send_interrupt_sequence ();
7071 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7072 the remote target. It is undefined which thread of which process
7073 reports the interrupt. Throws an error if the packet is not
7074 supported by the server. */
7077 remote_target::remote_interrupt_ns ()
7079 struct remote_state
*rs
= get_remote_state ();
7080 char *p
= rs
->buf
.data ();
7081 char *endp
= p
+ get_remote_packet_size ();
7083 xsnprintf (p
, endp
- p
, "vCtrlC");
7085 /* In non-stop, we get an immediate OK reply. The stop reply will
7086 come in asynchronously by notification. */
7088 getpkt (&rs
->buf
, 0);
7090 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vCtrlC
]))
7094 case PACKET_UNKNOWN
:
7095 error (_("No support for interrupting the remote target."));
7097 error (_("Interrupting target failed: %s"), rs
->buf
.data ());
7101 /* Implement the to_stop function for the remote targets. */
7104 remote_target::stop (ptid_t ptid
)
7106 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7108 if (target_is_non_stop_p ())
7109 remote_stop_ns (ptid
);
7112 /* We don't currently have a way to transparently pause the
7113 remote target in all-stop mode. Interrupt it instead. */
7114 remote_interrupt_as ();
7118 /* Implement the to_interrupt function for the remote targets. */
7121 remote_target::interrupt ()
7123 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7125 if (target_is_non_stop_p ())
7126 remote_interrupt_ns ();
7128 remote_interrupt_as ();
7131 /* Implement the to_pass_ctrlc function for the remote targets. */
7134 remote_target::pass_ctrlc ()
7136 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7138 struct remote_state
*rs
= get_remote_state ();
7140 /* If we're starting up, we're not fully synced yet. Quit
7142 if (rs
->starting_up
)
7144 /* If ^C has already been sent once, offer to disconnect. */
7145 else if (rs
->ctrlc_pending_p
)
7148 target_interrupt ();
7151 /* Ask the user what to do when an interrupt is received. */
7154 remote_target::interrupt_query ()
7156 struct remote_state
*rs
= get_remote_state ();
7158 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7160 if (query (_("The target is not responding to interrupt requests.\n"
7161 "Stop debugging it? ")))
7163 remote_unpush_target (this);
7164 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7169 if (query (_("Interrupted while waiting for the program.\n"
7170 "Give up waiting? ")))
7175 /* Enable/disable target terminal ownership. Most targets can use
7176 terminal groups to control terminal ownership. Remote targets are
7177 different in that explicit transfer of ownership to/from GDB/target
7181 remote_target::terminal_inferior ()
7183 /* NOTE: At this point we could also register our selves as the
7184 recipient of all input. Any characters typed could then be
7185 passed on down to the target. */
7189 remote_target::terminal_ours ()
7194 remote_console_output (const char *msg
)
7198 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7201 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7205 gdb_stdtarg
->puts (tb
);
7207 gdb_stdtarg
->flush ();
7210 /* Return the length of the stop reply queue. */
7213 remote_target::stop_reply_queue_length ()
7215 remote_state
*rs
= get_remote_state ();
7216 return rs
->stop_reply_queue
.size ();
7220 remote_notif_stop_parse (remote_target
*remote
,
7221 struct notif_client
*self
, const char *buf
,
7222 struct notif_event
*event
)
7224 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7228 remote_notif_stop_ack (remote_target
*remote
,
7229 struct notif_client
*self
, const char *buf
,
7230 struct notif_event
*event
)
7232 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
7235 putpkt (remote
, self
->ack_command
);
7237 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7238 the notification. It was left in the queue because we need to
7239 acknowledge it and pull the rest of the notifications out. */
7240 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7241 remote
->push_stop_reply (stop_reply
);
7245 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7246 struct notif_client
*self
)
7248 /* We can't get pending events in remote_notif_process for
7249 notification stop, and we have to do this in remote_wait_ns
7250 instead. If we fetch all queued events from stub, remote stub
7251 may exit and we have no chance to process them back in
7253 remote_state
*rs
= remote
->get_remote_state ();
7254 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7258 stop_reply::~stop_reply ()
7260 for (cached_reg_t
®
: regcache
)
7264 static notif_event_up
7265 remote_notif_stop_alloc_reply ()
7267 return notif_event_up (new struct stop_reply ());
7270 /* A client of notification Stop. */
7272 struct notif_client notif_client_stop
=
7276 remote_notif_stop_parse
,
7277 remote_notif_stop_ack
,
7278 remote_notif_stop_can_get_pending_events
,
7279 remote_notif_stop_alloc_reply
,
7283 /* If CONTEXT contains any fork child threads that have not been
7284 reported yet, remove them from the CONTEXT list. If such a
7285 thread exists it is because we are stopped at a fork catchpoint
7286 and have not yet called follow_fork, which will set up the
7287 host-side data structures for the new process. */
7290 remote_target::remove_new_fork_children (threads_listing_context
*context
)
7292 struct notif_client
*notif
= ¬if_client_stop
;
7294 /* For any threads stopped at a fork event, remove the corresponding
7295 fork child threads from the CONTEXT list. */
7296 for (thread_info
*thread
: all_non_exited_threads (this))
7298 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
7303 context
->remove_thread (ws
->child_ptid ());
7306 /* Check for any pending fork events (not reported or processed yet)
7307 in process PID and remove those fork child threads from the
7308 CONTEXT list as well. */
7309 remote_notif_get_pending_events (notif
);
7310 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7311 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7312 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7313 context
->remove_thread (event
->ws
.child_ptid ());
7314 else if (event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7315 context
->remove_thread (event
->ptid
);
7318 /* Check whether any event pending in the vStopped queue would prevent a
7319 global or process wildcard vCont action. Set *may_global_wildcard to
7320 false if we can't do a global wildcard (vCont;c), and clear the event
7321 inferior's may_wildcard_vcont flag if we can't do a process-wide
7322 wildcard resume (vCont;c:pPID.-1). */
7325 remote_target::check_pending_events_prevent_wildcard_vcont
7326 (bool *may_global_wildcard
)
7328 struct notif_client
*notif
= ¬if_client_stop
;
7330 remote_notif_get_pending_events (notif
);
7331 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7333 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7334 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7337 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7338 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7339 *may_global_wildcard
= false;
7341 /* This may be the first time we heard about this process.
7342 Regardless, we must not do a global wildcard resume, otherwise
7343 we'd resume this process too. */
7344 *may_global_wildcard
= false;
7345 if (event
->ptid
!= null_ptid
)
7347 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7349 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7354 /* Discard all pending stop replies of inferior INF. */
7357 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7359 struct stop_reply
*reply
;
7360 struct remote_state
*rs
= get_remote_state ();
7361 struct remote_notif_state
*rns
= rs
->notif_state
;
7363 /* This function can be notified when an inferior exists. When the
7364 target is not remote, the notification state is NULL. */
7365 if (rs
->remote_desc
== NULL
)
7368 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
7370 /* Discard the in-flight notification. */
7371 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7373 /* Leave the notification pending, since the server expects that
7374 we acknowledge it with vStopped. But clear its contents, so
7375 that later on when we acknowledge it, we also discard it. */
7377 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7378 reply
->ptid
.to_string().c_str(),
7379 reply
->ws
.to_string ().c_str ());
7380 reply
->ws
.set_ignore ();
7383 /* Discard the stop replies we have already pulled with
7385 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7386 rs
->stop_reply_queue
.end (),
7387 [=] (const stop_reply_up
&event
)
7389 return event
->ptid
.pid () == inf
->pid
;
7391 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7393 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7394 reply
->ptid
.to_string().c_str(),
7395 reply
->ws
.to_string ().c_str ());
7396 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7399 /* Discard the stop replies for RS in stop_reply_queue. */
7402 remote_target::discard_pending_stop_replies_in_queue ()
7404 remote_state
*rs
= get_remote_state ();
7406 /* Discard the stop replies we have already pulled with
7408 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7409 rs
->stop_reply_queue
.end (),
7410 [=] (const stop_reply_up
&event
)
7412 return event
->rs
== rs
;
7414 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7417 /* Remove the first reply in 'stop_reply_queue' which matches
7421 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7423 remote_state
*rs
= get_remote_state ();
7425 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7426 rs
->stop_reply_queue
.end (),
7427 [=] (const stop_reply_up
&event
)
7429 return event
->ptid
.matches (ptid
);
7431 struct stop_reply
*result
;
7432 if (iter
== rs
->stop_reply_queue
.end ())
7436 result
= iter
->release ();
7437 rs
->stop_reply_queue
.erase (iter
);
7441 gdb_printf (gdb_stdlog
,
7442 "notif: discard queued event: 'Stop' in %s\n",
7443 ptid
.to_string ().c_str ());
7448 /* Look for a queued stop reply belonging to PTID. If one is found,
7449 remove it from the queue, and return it. Returns NULL if none is
7450 found. If there are still queued events left to process, tell the
7451 event loop to get back to target_wait soon. */
7454 remote_target::queued_stop_reply (ptid_t ptid
)
7456 remote_state
*rs
= get_remote_state ();
7457 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
7459 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7461 /* There's still at least an event left. */
7462 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7468 /* Push a fully parsed stop reply in the stop reply queue. Since we
7469 know that we now have at least one queued event left to pass to the
7470 core side, tell the event loop to get back to target_wait soon. */
7473 remote_target::push_stop_reply (struct stop_reply
*new_event
)
7475 remote_state
*rs
= get_remote_state ();
7476 rs
->stop_reply_queue
.push_back (stop_reply_up (new_event
));
7479 gdb_printf (gdb_stdlog
,
7480 "notif: push 'Stop' %s to queue %d\n",
7481 new_event
->ptid
.to_string ().c_str (),
7482 int (rs
->stop_reply_queue
.size ()));
7484 /* Mark the pending event queue only if async mode is currently enabled.
7485 If async mode is not currently enabled, then, if it later becomes
7486 enabled, and there are events in this queue, we will mark the event
7487 token at that point, see remote_target::async. */
7488 if (target_is_async_p ())
7489 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7492 /* Returns true if we have a stop reply for PTID. */
7495 remote_target::peek_stop_reply (ptid_t ptid
)
7497 remote_state
*rs
= get_remote_state ();
7498 for (auto &event
: rs
->stop_reply_queue
)
7499 if (ptid
== event
->ptid
7500 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7505 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7506 starting with P and ending with PEND matches PREFIX. */
7509 strprefix (const char *p
, const char *pend
, const char *prefix
)
7511 for ( ; p
< pend
; p
++, prefix
++)
7514 return *prefix
== '\0';
7517 /* Parse the stop reply in BUF. Either the function succeeds, and the
7518 result is stored in EVENT, or throws an error. */
7521 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
7523 remote_arch_state
*rsa
= NULL
;
7528 event
->ptid
= null_ptid
;
7529 event
->rs
= get_remote_state ();
7530 event
->ws
.set_ignore ();
7531 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7532 event
->regcache
.clear ();
7537 case 'T': /* Status with PC, SP, FP, ... */
7538 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7539 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7541 n... = register number
7542 r... = register contents
7545 p
= &buf
[3]; /* after Txx */
7551 p1
= strchr (p
, ':');
7553 error (_("Malformed packet(a) (missing colon): %s\n\
7557 error (_("Malformed packet(a) (missing register number): %s\n\
7561 /* Some "registers" are actually extended stop information.
7562 Note if you're adding a new entry here: GDB 7.9 and
7563 earlier assume that all register "numbers" that start
7564 with an hex digit are real register numbers. Make sure
7565 the server only sends such a packet if it knows the
7566 client understands it. */
7568 if (strprefix (p
, p1
, "thread"))
7569 event
->ptid
= read_ptid (++p1
, &p
);
7570 else if (strprefix (p
, p1
, "syscall_entry"))
7574 p
= unpack_varlen_hex (++p1
, &sysno
);
7575 event
->ws
.set_syscall_entry ((int) sysno
);
7577 else if (strprefix (p
, p1
, "syscall_return"))
7581 p
= unpack_varlen_hex (++p1
, &sysno
);
7582 event
->ws
.set_syscall_return ((int) sysno
);
7584 else if (strprefix (p
, p1
, "watch")
7585 || strprefix (p
, p1
, "rwatch")
7586 || strprefix (p
, p1
, "awatch"))
7588 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
7589 p
= unpack_varlen_hex (++p1
, &addr
);
7590 event
->watch_data_address
= (CORE_ADDR
) addr
;
7592 else if (strprefix (p
, p1
, "swbreak"))
7594 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
7596 /* Make sure the stub doesn't forget to indicate support
7598 if (packet_support (PACKET_swbreak_feature
) != PACKET_ENABLE
)
7599 error (_("Unexpected swbreak stop reason"));
7601 /* The value part is documented as "must be empty",
7602 though we ignore it, in case we ever decide to make
7603 use of it in a backward compatible way. */
7604 p
= strchrnul (p1
+ 1, ';');
7606 else if (strprefix (p
, p1
, "hwbreak"))
7608 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
7610 /* Make sure the stub doesn't forget to indicate support
7612 if (packet_support (PACKET_hwbreak_feature
) != PACKET_ENABLE
)
7613 error (_("Unexpected hwbreak stop reason"));
7616 p
= strchrnul (p1
+ 1, ';');
7618 else if (strprefix (p
, p1
, "library"))
7620 event
->ws
.set_loaded ();
7621 p
= strchrnul (p1
+ 1, ';');
7623 else if (strprefix (p
, p1
, "replaylog"))
7625 event
->ws
.set_no_history ();
7626 /* p1 will indicate "begin" or "end", but it makes
7627 no difference for now, so ignore it. */
7628 p
= strchrnul (p1
+ 1, ';');
7630 else if (strprefix (p
, p1
, "core"))
7634 p
= unpack_varlen_hex (++p1
, &c
);
7637 else if (strprefix (p
, p1
, "fork"))
7638 event
->ws
.set_forked (read_ptid (++p1
, &p
));
7639 else if (strprefix (p
, p1
, "vfork"))
7640 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
7641 else if (strprefix (p
, p1
, "vforkdone"))
7643 event
->ws
.set_vfork_done ();
7644 p
= strchrnul (p1
+ 1, ';');
7646 else if (strprefix (p
, p1
, "exec"))
7651 /* Determine the length of the execd pathname. */
7652 p
= unpack_varlen_hex (++p1
, &ignored
);
7653 pathlen
= (p
- p1
) / 2;
7655 /* Save the pathname for event reporting and for
7656 the next run command. */
7657 gdb::unique_xmalloc_ptr
<char> pathname
7658 ((char *) xmalloc (pathlen
+ 1));
7659 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
7660 pathname
.get ()[pathlen
] = '\0';
7662 /* This is freed during event handling. */
7663 event
->ws
.set_execd (std::move (pathname
));
7665 /* Skip the registers included in this packet, since
7666 they may be for an architecture different from the
7667 one used by the original program. */
7670 else if (strprefix (p
, p1
, "create"))
7672 event
->ws
.set_thread_created ();
7673 p
= strchrnul (p1
+ 1, ';');
7682 p
= strchrnul (p1
+ 1, ';');
7687 /* Maybe a real ``P'' register number. */
7688 p_temp
= unpack_varlen_hex (p
, &pnum
);
7689 /* If the first invalid character is the colon, we got a
7690 register number. Otherwise, it's an unknown stop
7694 /* If we haven't parsed the event's thread yet, find
7695 it now, in order to find the architecture of the
7696 reported expedited registers. */
7697 if (event
->ptid
== null_ptid
)
7699 /* If there is no thread-id information then leave
7700 the event->ptid as null_ptid. Later in
7701 process_stop_reply we will pick a suitable
7703 const char *thr
= strstr (p1
+ 1, ";thread:");
7705 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
7712 = (event
->ptid
== null_ptid
7714 : find_inferior_ptid (this, event
->ptid
));
7715 /* If this is the first time we learn anything
7716 about this process, skip the registers
7717 included in this packet, since we don't yet
7718 know which architecture to use to parse them.
7719 We'll determine the architecture later when
7720 we process the stop reply and retrieve the
7721 target description, via
7722 remote_notice_new_inferior ->
7723 post_create_inferior. */
7726 p
= strchrnul (p1
+ 1, ';');
7731 event
->arch
= inf
->gdbarch
;
7732 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
7736 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
7737 cached_reg_t cached_reg
;
7740 error (_("Remote sent bad register number %s: %s\n\
7742 hex_string (pnum
), p
, buf
);
7744 cached_reg
.num
= reg
->regnum
;
7745 cached_reg
.data
= (gdb_byte
*)
7746 xmalloc (register_size (event
->arch
, reg
->regnum
));
7749 fieldsize
= hex2bin (p
, cached_reg
.data
,
7750 register_size (event
->arch
, reg
->regnum
));
7752 if (fieldsize
< register_size (event
->arch
, reg
->regnum
))
7753 warning (_("Remote reply is too short: %s"), buf
);
7755 event
->regcache
.push_back (cached_reg
);
7759 /* Not a number. Silently skip unknown optional
7761 p
= strchrnul (p1
+ 1, ';');
7766 error (_("Remote register badly formatted: %s\nhere: %s"),
7771 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7775 case 'S': /* Old style status, just signal only. */
7779 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
7780 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
7781 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
7783 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
7786 case 'w': /* Thread exited. */
7790 p
= unpack_varlen_hex (&buf
[1], &value
);
7791 event
->ws
.set_thread_exited (value
);
7793 error (_("stop reply packet badly formatted: %s"), buf
);
7794 event
->ptid
= read_ptid (++p
, NULL
);
7797 case 'W': /* Target exited. */
7802 /* GDB used to accept only 2 hex chars here. Stubs should
7803 only send more if they detect GDB supports multi-process
7805 p
= unpack_varlen_hex (&buf
[1], &value
);
7809 /* The remote process exited. */
7810 event
->ws
.set_exited (value
);
7814 /* The remote process exited with a signal. */
7815 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
7816 event
->ws
.set_signalled ((enum gdb_signal
) value
);
7818 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
7821 /* If no process is specified, return null_ptid, and let the
7822 caller figure out the right process to use. */
7832 else if (startswith (p
, "process:"))
7836 p
+= sizeof ("process:") - 1;
7837 unpack_varlen_hex (p
, &upid
);
7841 error (_("unknown stop reply packet: %s"), buf
);
7844 error (_("unknown stop reply packet: %s"), buf
);
7845 event
->ptid
= ptid_t (pid
);
7849 event
->ws
.set_no_resumed ();
7850 event
->ptid
= minus_one_ptid
;
7855 /* When the stub wants to tell GDB about a new notification reply, it
7856 sends a notification (%Stop, for example). Those can come it at
7857 any time, hence, we have to make sure that any pending
7858 putpkt/getpkt sequence we're making is finished, before querying
7859 the stub for more events with the corresponding ack command
7860 (vStopped, for example). E.g., if we started a vStopped sequence
7861 immediately upon receiving the notification, something like this
7869 1.6) <-- (registers reply to step #1.3)
7871 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7874 To solve this, whenever we parse a %Stop notification successfully,
7875 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7876 doing whatever we were doing:
7882 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7883 2.5) <-- (registers reply to step #2.3)
7885 Eventually after step #2.5, we return to the event loop, which
7886 notices there's an event on the
7887 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7888 associated callback --- the function below. At this point, we're
7889 always safe to start a vStopped sequence. :
7892 2.7) <-- T05 thread:2
7898 remote_target::remote_notif_get_pending_events (notif_client
*nc
)
7900 struct remote_state
*rs
= get_remote_state ();
7902 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
7905 gdb_printf (gdb_stdlog
,
7906 "notif: process: '%s' ack pending event\n",
7910 nc
->ack (this, nc
, rs
->buf
.data (),
7911 rs
->notif_state
->pending_event
[nc
->id
]);
7912 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
7916 getpkt (&rs
->buf
, 0);
7917 if (strcmp (rs
->buf
.data (), "OK") == 0)
7920 remote_notif_ack (this, nc
, rs
->buf
.data ());
7926 gdb_printf (gdb_stdlog
,
7927 "notif: process: '%s' no pending reply\n",
7932 /* Wrapper around remote_target::remote_notif_get_pending_events to
7933 avoid having to export the whole remote_target class. */
7936 remote_notif_get_pending_events (remote_target
*remote
, notif_client
*nc
)
7938 remote
->remote_notif_get_pending_events (nc
);
7941 /* Called from process_stop_reply when the stop packet we are responding
7942 to didn't include a process-id or thread-id. STATUS is the stop event
7943 we are responding to.
7945 It is the task of this function to select a suitable thread (or process)
7946 and return its ptid, this is the thread (or process) we will assume the
7947 stop event came from.
7949 In some cases there isn't really any choice about which thread (or
7950 process) is selected, a basic remote with a single process containing a
7951 single thread might choose not to send any process-id or thread-id in
7952 its stop packets, this function will select and return the one and only
7955 However, if a target supports multiple threads (or processes) and still
7956 doesn't include a thread-id (or process-id) in its stop packet then
7957 first, this is a badly behaving target, and second, we're going to have
7958 to select a thread (or process) at random and use that. This function
7959 will print a warning to the user if it detects that there is the
7960 possibility that GDB is guessing which thread (or process) to
7963 Note that this is called before GDB fetches the updated thread list from the
7964 target. So it's possible for the stop reply to be ambiguous and for GDB to
7965 not realize it. For example, if there's initially one thread, the target
7966 spawns a second thread, and then sends a stop reply without an id that
7967 concerns the first thread. GDB will assume the stop reply is about the
7968 first thread - the only thread it knows about - without printing a warning.
7969 Anyway, if the remote meant for the stop reply to be about the second thread,
7970 then it would be really broken, because GDB doesn't know about that thread
7974 remote_target::select_thread_for_ambiguous_stop_reply
7975 (const target_waitstatus
&status
)
7977 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7979 /* Some stop events apply to all threads in an inferior, while others
7980 only apply to a single thread. */
7981 bool process_wide_stop
7982 = (status
.kind () == TARGET_WAITKIND_EXITED
7983 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
7985 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
7987 thread_info
*first_resumed_thread
= nullptr;
7988 bool ambiguous
= false;
7990 /* Consider all non-exited threads of the target, find the first resumed
7992 for (thread_info
*thr
: all_non_exited_threads (this))
7994 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
7996 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
7999 if (first_resumed_thread
== nullptr)
8000 first_resumed_thread
= thr
;
8001 else if (!process_wide_stop
8002 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8006 gdb_assert (first_resumed_thread
!= nullptr);
8008 remote_debug_printf ("first resumed thread is %s",
8009 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8010 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8012 /* Warn if the remote target is sending ambiguous stop replies. */
8015 static bool warned
= false;
8019 /* If you are seeing this warning then the remote target has
8020 stopped without specifying a thread-id, but the target
8021 does have multiple threads (or inferiors), and so GDB is
8022 having to guess which thread stopped.
8024 Examples of what might cause this are the target sending
8025 and 'S' stop packet, or a 'T' stop packet and not
8026 including a thread-id.
8028 Additionally, the target might send a 'W' or 'X packet
8029 without including a process-id, when the target has
8030 multiple running inferiors. */
8031 if (process_wide_stop
)
8032 warning (_("multi-inferior target stopped without "
8033 "sending a process-id, using first "
8034 "non-exited inferior"));
8036 warning (_("multi-threaded target stopped without "
8037 "sending a thread-id, using first "
8038 "non-exited thread"));
8043 /* If this is a stop for all threads then don't use a particular threads
8044 ptid, instead create a new ptid where only the pid field is set. */
8045 if (process_wide_stop
)
8046 return ptid_t (first_resumed_thread
->ptid
.pid ());
8048 return first_resumed_thread
->ptid
;
8051 /* Called when it is decided that STOP_REPLY holds the info of the
8052 event that is to be returned to the core. This function always
8053 destroys STOP_REPLY. */
8056 remote_target::process_stop_reply (struct stop_reply
*stop_reply
,
8057 struct target_waitstatus
*status
)
8059 *status
= stop_reply
->ws
;
8060 ptid_t ptid
= stop_reply
->ptid
;
8062 /* If no thread/process was reported by the stub then select a suitable
8064 if (ptid
== null_ptid
)
8065 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8066 gdb_assert (ptid
!= null_ptid
);
8068 if (status
->kind () != TARGET_WAITKIND_EXITED
8069 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8070 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8072 /* Expedited registers. */
8073 if (!stop_reply
->regcache
.empty ())
8075 struct regcache
*regcache
8076 = get_thread_arch_regcache (this, ptid
, stop_reply
->arch
);
8078 for (cached_reg_t
®
: stop_reply
->regcache
)
8080 regcache
->raw_supply (reg
.num
, reg
.data
);
8084 stop_reply
->regcache
.clear ();
8087 remote_notice_new_inferior (ptid
, false);
8088 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8089 remote_thr
->core
= stop_reply
->core
;
8090 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8091 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8093 if (target_is_non_stop_p ())
8095 /* If the target works in non-stop mode, a stop-reply indicates that
8096 only this thread stopped. */
8097 remote_thr
->set_not_resumed ();
8101 /* If the target works in all-stop mode, a stop-reply indicates that
8102 all the target's threads stopped. */
8103 for (thread_info
*tp
: all_non_exited_threads (this))
8104 get_remote_thread_info (tp
)->set_not_resumed ();
8112 /* The non-stop mode version of target_wait. */
8115 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8116 target_wait_flags options
)
8118 struct remote_state
*rs
= get_remote_state ();
8119 struct stop_reply
*stop_reply
;
8123 /* If in non-stop mode, get out of getpkt even if a
8124 notification is received. */
8126 ret
= getpkt_or_notif_sane (&rs
->buf
, 0 /* forever */, &is_notif
);
8129 if (ret
!= -1 && !is_notif
)
8132 case 'E': /* Error of some sort. */
8133 /* We're out of sync with the target now. Did it continue
8134 or not? We can't tell which thread it was in non-stop,
8135 so just ignore this. */
8136 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8138 case 'O': /* Console output. */
8139 remote_console_output (&rs
->buf
[1]);
8142 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8146 /* Acknowledge a pending stop reply that may have arrived in the
8148 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8149 remote_notif_get_pending_events (¬if_client_stop
);
8151 /* If indeed we noticed a stop reply, we're done. */
8152 stop_reply
= queued_stop_reply (ptid
);
8153 if (stop_reply
!= NULL
)
8154 return process_stop_reply (stop_reply
, status
);
8156 /* Still no event. If we're just polling for an event, then
8157 return to the event loop. */
8158 if (options
& TARGET_WNOHANG
)
8160 status
->set_ignore ();
8161 return minus_one_ptid
;
8164 /* Otherwise do a blocking wait. */
8165 ret
= getpkt_or_notif_sane (&rs
->buf
, 1 /* forever */, &is_notif
);
8169 /* Return the first resumed thread. */
8172 first_remote_resumed_thread (remote_target
*target
)
8174 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8180 /* Wait until the remote machine stops, then return, storing status in
8181 STATUS just as `wait' would. */
8184 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8185 target_wait_flags options
)
8187 struct remote_state
*rs
= get_remote_state ();
8188 ptid_t event_ptid
= null_ptid
;
8190 struct stop_reply
*stop_reply
;
8194 status
->set_ignore ();
8196 stop_reply
= queued_stop_reply (ptid
);
8197 if (stop_reply
!= NULL
)
8199 /* None of the paths that push a stop reply onto the queue should
8200 have set the waiting_for_stop_reply flag. */
8201 gdb_assert (!rs
->waiting_for_stop_reply
);
8202 event_ptid
= process_stop_reply (stop_reply
, status
);
8206 int forever
= ((options
& TARGET_WNOHANG
) == 0
8207 && rs
->wait_forever_enabled_p
);
8209 if (!rs
->waiting_for_stop_reply
)
8211 status
->set_no_resumed ();
8212 return minus_one_ptid
;
8215 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8216 _never_ wait for ever -> test on target_is_async_p().
8217 However, before we do that we need to ensure that the caller
8218 knows how to take the target into/out of async mode. */
8220 int ret
= getpkt_or_notif_sane (&rs
->buf
, forever
, &is_notif
);
8222 /* GDB gets a notification. Return to core as this event is
8224 if (ret
!= -1 && is_notif
)
8225 return minus_one_ptid
;
8227 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8228 return minus_one_ptid
;
8230 buf
= rs
->buf
.data ();
8232 /* Assume that the target has acknowledged Ctrl-C unless we receive
8233 an 'F' or 'O' packet. */
8234 if (buf
[0] != 'F' && buf
[0] != 'O')
8235 rs
->ctrlc_pending_p
= 0;
8239 case 'E': /* Error of some sort. */
8240 /* We're out of sync with the target now. Did it continue or
8241 not? Not is more likely, so report a stop. */
8242 rs
->waiting_for_stop_reply
= 0;
8244 warning (_("Remote failure reply: %s"), buf
);
8245 status
->set_stopped (GDB_SIGNAL_0
);
8247 case 'F': /* File-I/O request. */
8248 /* GDB may access the inferior memory while handling the File-I/O
8249 request, but we don't want GDB accessing memory while waiting
8250 for a stop reply. See the comments in putpkt_binary. Set
8251 waiting_for_stop_reply to 0 temporarily. */
8252 rs
->waiting_for_stop_reply
= 0;
8253 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8254 rs
->ctrlc_pending_p
= 0;
8255 /* GDB handled the File-I/O request, and the target is running
8256 again. Keep waiting for events. */
8257 rs
->waiting_for_stop_reply
= 1;
8259 case 'N': case 'T': case 'S': case 'X': case 'W':
8261 /* There is a stop reply to handle. */
8262 rs
->waiting_for_stop_reply
= 0;
8265 = (struct stop_reply
*) remote_notif_parse (this,
8269 event_ptid
= process_stop_reply (stop_reply
, status
);
8272 case 'O': /* Console output. */
8273 remote_console_output (buf
+ 1);
8276 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8278 /* Zero length reply means that we tried 'S' or 'C' and the
8279 remote system doesn't support it. */
8280 target_terminal::ours_for_output ();
8282 ("Can't send signals to this remote system. %s not sent.\n",
8283 gdb_signal_to_name (rs
->last_sent_signal
));
8284 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8285 target_terminal::inferior ();
8287 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8293 warning (_("Invalid remote reply: %s"), buf
);
8298 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8299 return minus_one_ptid
;
8300 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8302 /* Nothing interesting happened. If we're doing a non-blocking
8303 poll, we're done. Otherwise, go back to waiting. */
8304 if (options
& TARGET_WNOHANG
)
8305 return minus_one_ptid
;
8309 else if (status
->kind () != TARGET_WAITKIND_EXITED
8310 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8312 if (event_ptid
!= null_ptid
)
8313 record_currthread (rs
, event_ptid
);
8315 event_ptid
= first_remote_resumed_thread (this);
8319 /* A process exit. Invalidate our notion of current thread. */
8320 record_currthread (rs
, minus_one_ptid
);
8321 /* It's possible that the packet did not include a pid. */
8322 if (event_ptid
== null_ptid
)
8323 event_ptid
= first_remote_resumed_thread (this);
8324 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8325 if (event_ptid
== null_ptid
)
8326 event_ptid
= magic_null_ptid
;
8332 /* Wait until the remote machine stops, then return, storing status in
8333 STATUS just as `wait' would. */
8336 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8337 target_wait_flags options
)
8339 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8341 remote_state
*rs
= get_remote_state ();
8343 /* Start by clearing the flag that asks for our wait method to be called,
8344 we'll mark it again at the end if needed. If the target is not in
8345 async mode then the async token should not be marked. */
8346 if (target_is_async_p ())
8347 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
8349 gdb_assert (!async_event_handler_marked
8350 (rs
->remote_async_inferior_event_token
));
8354 if (target_is_non_stop_p ())
8355 event_ptid
= wait_ns (ptid
, status
, options
);
8357 event_ptid
= wait_as (ptid
, status
, options
);
8359 if (target_is_async_p ())
8361 /* If there are events left in the queue, or unacknowledged
8362 notifications, then tell the event loop to call us again. */
8363 if (!rs
->stop_reply_queue
.empty ()
8364 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8365 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
8371 /* Fetch a single register using a 'p' packet. */
8374 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8377 struct gdbarch
*gdbarch
= regcache
->arch ();
8378 struct remote_state
*rs
= get_remote_state ();
8380 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8383 if (packet_support (PACKET_p
) == PACKET_DISABLE
)
8386 if (reg
->pnum
== -1)
8389 p
= rs
->buf
.data ();
8391 p
+= hexnumstr (p
, reg
->pnum
);
8394 getpkt (&rs
->buf
, 0);
8396 buf
= rs
->buf
.data ();
8398 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_p
]))
8402 case PACKET_UNKNOWN
:
8405 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8406 gdbarch_register_name (regcache
->arch (),
8411 /* If this register is unfetchable, tell the regcache. */
8414 regcache
->raw_supply (reg
->regnum
, NULL
);
8418 /* Otherwise, parse and supply the value. */
8424 error (_("fetch_register_using_p: early buf termination"));
8426 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8429 regcache
->raw_supply (reg
->regnum
, regp
);
8433 /* Fetch the registers included in the target's 'g' packet. */
8436 remote_target::send_g_packet ()
8438 struct remote_state
*rs
= get_remote_state ();
8441 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8443 getpkt (&rs
->buf
, 0);
8444 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8445 error (_("Could not read registers; remote failure reply '%s'"),
8448 /* We can get out of synch in various cases. If the first character
8449 in the buffer is not a hex character, assume that has happened
8450 and try to fetch another packet to read. */
8451 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8452 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8453 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8454 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8456 remote_debug_printf ("Bad register packet; fetching a new packet");
8457 getpkt (&rs
->buf
, 0);
8460 buf_len
= strlen (rs
->buf
.data ());
8462 /* Sanity check the received packet. */
8463 if (buf_len
% 2 != 0)
8464 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8470 remote_target::process_g_packet (struct regcache
*regcache
)
8472 struct gdbarch
*gdbarch
= regcache
->arch ();
8473 struct remote_state
*rs
= get_remote_state ();
8474 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8479 buf_len
= strlen (rs
->buf
.data ());
8481 /* Further sanity checks, with knowledge of the architecture. */
8482 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8483 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8485 rsa
->sizeof_g_packet
, buf_len
/ 2,
8488 /* Save the size of the packet sent to us by the target. It is used
8489 as a heuristic when determining the max size of packets that the
8490 target can safely receive. */
8491 if (rsa
->actual_register_packet_size
== 0)
8492 rsa
->actual_register_packet_size
= buf_len
;
8494 /* If this is smaller than we guessed the 'g' packet would be,
8495 update our records. A 'g' reply that doesn't include a register's
8496 value implies either that the register is not available, or that
8497 the 'p' packet must be used. */
8498 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8500 long sizeof_g_packet
= buf_len
/ 2;
8502 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8504 long offset
= rsa
->regs
[i
].offset
;
8505 long reg_size
= register_size (gdbarch
, i
);
8507 if (rsa
->regs
[i
].pnum
== -1)
8510 if (offset
>= sizeof_g_packet
)
8511 rsa
->regs
[i
].in_g_packet
= 0;
8512 else if (offset
+ reg_size
> sizeof_g_packet
)
8513 error (_("Truncated register %d in remote 'g' packet"), i
);
8515 rsa
->regs
[i
].in_g_packet
= 1;
8518 /* Looks valid enough, we can assume this is the correct length
8519 for a 'g' packet. It's important not to adjust
8520 rsa->sizeof_g_packet if we have truncated registers otherwise
8521 this "if" won't be run the next time the method is called
8522 with a packet of the same size and one of the internal errors
8523 below will trigger instead. */
8524 rsa
->sizeof_g_packet
= sizeof_g_packet
;
8527 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
8529 /* Unimplemented registers read as all bits zero. */
8530 memset (regs
, 0, rsa
->sizeof_g_packet
);
8532 /* Reply describes registers byte by byte, each byte encoded as two
8533 hex characters. Suck them all up, then supply them to the
8534 register cacheing/storage mechanism. */
8536 p
= rs
->buf
.data ();
8537 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
8539 if (p
[0] == 0 || p
[1] == 0)
8540 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
8541 internal_error (__FILE__
, __LINE__
,
8542 _("unexpected end of 'g' packet reply"));
8544 if (p
[0] == 'x' && p
[1] == 'x')
8545 regs
[i
] = 0; /* 'x' */
8547 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8551 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8553 struct packet_reg
*r
= &rsa
->regs
[i
];
8554 long reg_size
= register_size (gdbarch
, i
);
8558 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
8559 /* This shouldn't happen - we adjusted in_g_packet above. */
8560 internal_error (__FILE__
, __LINE__
,
8561 _("unexpected end of 'g' packet reply"));
8562 else if (rs
->buf
[r
->offset
* 2] == 'x')
8564 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
8565 /* The register isn't available, mark it as such (at
8566 the same time setting the value to zero). */
8567 regcache
->raw_supply (r
->regnum
, NULL
);
8570 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
8576 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
8579 process_g_packet (regcache
);
8582 /* Make the remote selected traceframe match GDB's selected
8586 remote_target::set_remote_traceframe ()
8589 struct remote_state
*rs
= get_remote_state ();
8591 if (rs
->remote_traceframe_number
== get_traceframe_number ())
8594 /* Avoid recursion, remote_trace_find calls us again. */
8595 rs
->remote_traceframe_number
= get_traceframe_number ();
8597 newnum
= target_trace_find (tfind_number
,
8598 get_traceframe_number (), 0, 0, NULL
);
8600 /* Should not happen. If it does, all bets are off. */
8601 if (newnum
!= get_traceframe_number ())
8602 warning (_("could not set remote traceframe"));
8606 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
8608 struct gdbarch
*gdbarch
= regcache
->arch ();
8609 struct remote_state
*rs
= get_remote_state ();
8610 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8613 set_remote_traceframe ();
8614 set_general_thread (regcache
->ptid ());
8618 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8620 gdb_assert (reg
!= NULL
);
8622 /* If this register might be in the 'g' packet, try that first -
8623 we are likely to read more than one register. If this is the
8624 first 'g' packet, we might be overly optimistic about its
8625 contents, so fall back to 'p'. */
8626 if (reg
->in_g_packet
)
8628 fetch_registers_using_g (regcache
);
8629 if (reg
->in_g_packet
)
8633 if (fetch_register_using_p (regcache
, reg
))
8636 /* This register is not available. */
8637 regcache
->raw_supply (reg
->regnum
, NULL
);
8642 fetch_registers_using_g (regcache
);
8644 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8645 if (!rsa
->regs
[i
].in_g_packet
)
8646 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
8648 /* This register is not available. */
8649 regcache
->raw_supply (i
, NULL
);
8653 /* Prepare to store registers. Since we may send them all (using a
8654 'G' request), we have to read out the ones we don't want to change
8658 remote_target::prepare_to_store (struct regcache
*regcache
)
8660 struct remote_state
*rs
= get_remote_state ();
8661 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8664 /* Make sure the entire registers array is valid. */
8665 switch (packet_support (PACKET_P
))
8667 case PACKET_DISABLE
:
8668 case PACKET_SUPPORT_UNKNOWN
:
8669 /* Make sure all the necessary registers are cached. */
8670 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8671 if (rsa
->regs
[i
].in_g_packet
)
8672 regcache
->raw_update (rsa
->regs
[i
].regnum
);
8679 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
8680 packet was not recognized. */
8683 remote_target::store_register_using_P (const struct regcache
*regcache
,
8686 struct gdbarch
*gdbarch
= regcache
->arch ();
8687 struct remote_state
*rs
= get_remote_state ();
8688 /* Try storing a single register. */
8689 char *buf
= rs
->buf
.data ();
8690 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8693 if (packet_support (PACKET_P
) == PACKET_DISABLE
)
8696 if (reg
->pnum
== -1)
8699 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
8700 p
= buf
+ strlen (buf
);
8701 regcache
->raw_collect (reg
->regnum
, regp
);
8702 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
8704 getpkt (&rs
->buf
, 0);
8706 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
8711 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8712 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
.data ());
8713 case PACKET_UNKNOWN
:
8716 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
8720 /* Store register REGNUM, or all registers if REGNUM == -1, from the
8721 contents of the register cache buffer. FIXME: ignores errors. */
8724 remote_target::store_registers_using_G (const struct regcache
*regcache
)
8726 struct remote_state
*rs
= get_remote_state ();
8727 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8731 /* Extract all the registers in the regcache copying them into a
8736 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
8737 memset (regs
, 0, rsa
->sizeof_g_packet
);
8738 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8740 struct packet_reg
*r
= &rsa
->regs
[i
];
8743 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
8747 /* Command describes registers byte by byte,
8748 each byte encoded as two hex characters. */
8749 p
= rs
->buf
.data ();
8751 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
8753 getpkt (&rs
->buf
, 0);
8754 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8755 error (_("Could not write registers; remote failure reply '%s'"),
8759 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8760 of the register cache buffer. FIXME: ignores errors. */
8763 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
8765 struct gdbarch
*gdbarch
= regcache
->arch ();
8766 struct remote_state
*rs
= get_remote_state ();
8767 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8770 set_remote_traceframe ();
8771 set_general_thread (regcache
->ptid ());
8775 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8777 gdb_assert (reg
!= NULL
);
8779 /* Always prefer to store registers using the 'P' packet if
8780 possible; we often change only a small number of registers.
8781 Sometimes we change a larger number; we'd need help from a
8782 higher layer to know to use 'G'. */
8783 if (store_register_using_P (regcache
, reg
))
8786 /* For now, don't complain if we have no way to write the
8787 register. GDB loses track of unavailable registers too
8788 easily. Some day, this may be an error. We don't have
8789 any way to read the register, either... */
8790 if (!reg
->in_g_packet
)
8793 store_registers_using_G (regcache
);
8797 store_registers_using_G (regcache
);
8799 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8800 if (!rsa
->regs
[i
].in_g_packet
)
8801 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
8802 /* See above for why we do not issue an error here. */
8807 /* Return the number of hex digits in num. */
8810 hexnumlen (ULONGEST num
)
8814 for (i
= 0; num
!= 0; i
++)
8817 return std::max (i
, 1);
8820 /* Set BUF to the minimum number of hex digits representing NUM. */
8823 hexnumstr (char *buf
, ULONGEST num
)
8825 int len
= hexnumlen (num
);
8827 return hexnumnstr (buf
, num
, len
);
8831 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
8834 hexnumnstr (char *buf
, ULONGEST num
, int width
)
8840 for (i
= width
- 1; i
>= 0; i
--)
8842 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
8849 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
8852 remote_address_masked (CORE_ADDR addr
)
8854 unsigned int address_size
= remote_address_size
;
8856 /* If "remoteaddresssize" was not set, default to target address size. */
8858 address_size
= gdbarch_addr_bit (target_gdbarch ());
8860 if (address_size
> 0
8861 && address_size
< (sizeof (ULONGEST
) * 8))
8863 /* Only create a mask when that mask can safely be constructed
8864 in a ULONGEST variable. */
8867 mask
= (mask
<< address_size
) - 1;
8873 /* Determine whether the remote target supports binary downloading.
8874 This is accomplished by sending a no-op memory write of zero length
8875 to the target at the specified address. It does not suffice to send
8876 the whole packet, since many stubs strip the eighth bit and
8877 subsequently compute a wrong checksum, which causes real havoc with
8880 NOTE: This can still lose if the serial line is not eight-bit
8881 clean. In cases like this, the user should clear "remote
8885 remote_target::check_binary_download (CORE_ADDR addr
)
8887 struct remote_state
*rs
= get_remote_state ();
8889 switch (packet_support (PACKET_X
))
8891 case PACKET_DISABLE
:
8895 case PACKET_SUPPORT_UNKNOWN
:
8899 p
= rs
->buf
.data ();
8901 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8903 p
+= hexnumstr (p
, (ULONGEST
) 0);
8907 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
8908 getpkt (&rs
->buf
, 0);
8910 if (rs
->buf
[0] == '\0')
8912 remote_debug_printf ("binary downloading NOT supported by target");
8913 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
8917 remote_debug_printf ("binary downloading supported by target");
8918 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
8925 /* Helper function to resize the payload in order to try to get a good
8926 alignment. We try to write an amount of data such that the next write will
8927 start on an address aligned on REMOTE_ALIGN_WRITES. */
8930 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
8932 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
8935 /* Write memory data directly to the remote machine.
8936 This does not inform the data cache; the data cache uses this.
8937 HEADER is the starting part of the packet.
8938 MEMADDR is the address in the remote memory space.
8939 MYADDR is the address of the buffer in our space.
8940 LEN_UNITS is the number of addressable units to write.
8941 UNIT_SIZE is the length in bytes of an addressable unit.
8942 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8943 should send data as binary ('X'), or hex-encoded ('M').
8945 The function creates packet of the form
8946 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8948 where encoding of <DATA> is terminated by PACKET_FORMAT.
8950 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8953 Return the transferred status, error or OK (an
8954 'enum target_xfer_status' value). Save the number of addressable units
8955 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8957 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8958 exchange between gdb and the stub could look like (?? in place of the
8964 -> $M1000,3:eeeeffffeeee#??
8968 <- eeeeffffeeeedddd */
8971 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
8972 const gdb_byte
*myaddr
,
8975 ULONGEST
*xfered_len_units
,
8976 char packet_format
, int use_length
)
8978 struct remote_state
*rs
= get_remote_state ();
8984 int payload_capacity_bytes
;
8985 int payload_length_bytes
;
8987 if (packet_format
!= 'X' && packet_format
!= 'M')
8988 internal_error (__FILE__
, __LINE__
,
8989 _("remote_write_bytes_aux: bad packet format"));
8992 return TARGET_XFER_EOF
;
8994 payload_capacity_bytes
= get_memory_write_packet_size ();
8996 /* The packet buffer will be large enough for the payload;
8997 get_memory_packet_size ensures this. */
9000 /* Compute the size of the actual payload by subtracting out the
9001 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9003 payload_capacity_bytes
-= strlen ("$,:#NN");
9005 /* The comma won't be used. */
9006 payload_capacity_bytes
+= 1;
9007 payload_capacity_bytes
-= strlen (header
);
9008 payload_capacity_bytes
-= hexnumlen (memaddr
);
9010 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9012 strcat (rs
->buf
.data (), header
);
9013 p
= rs
->buf
.data () + strlen (header
);
9015 /* Compute a best guess of the number of bytes actually transfered. */
9016 if (packet_format
== 'X')
9018 /* Best guess at number of bytes that will fit. */
9019 todo_units
= std::min (len_units
,
9020 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9022 payload_capacity_bytes
-= hexnumlen (todo_units
);
9023 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9027 /* Number of bytes that will fit. */
9029 = std::min (len_units
,
9030 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9032 payload_capacity_bytes
-= hexnumlen (todo_units
);
9033 todo_units
= std::min (todo_units
,
9034 (payload_capacity_bytes
/ unit_size
) / 2);
9037 if (todo_units
<= 0)
9038 internal_error (__FILE__
, __LINE__
,
9039 _("minimum packet size too small to write data"));
9041 /* If we already need another packet, then try to align the end
9042 of this packet to a useful boundary. */
9043 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9044 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9046 /* Append "<memaddr>". */
9047 memaddr
= remote_address_masked (memaddr
);
9048 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9055 /* Append the length and retain its location and size. It may need to be
9056 adjusted once the packet body has been created. */
9058 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9066 /* Append the packet body. */
9067 if (packet_format
== 'X')
9069 /* Binary mode. Send target system values byte by byte, in
9070 increasing byte addresses. Only escape certain critical
9072 payload_length_bytes
=
9073 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9074 &units_written
, payload_capacity_bytes
);
9076 /* If not all TODO units fit, then we'll need another packet. Make
9077 a second try to keep the end of the packet aligned. Don't do
9078 this if the packet is tiny. */
9079 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9083 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9085 if (new_todo_units
!= units_written
)
9086 payload_length_bytes
=
9087 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9088 (gdb_byte
*) p
, &units_written
,
9089 payload_capacity_bytes
);
9092 p
+= payload_length_bytes
;
9093 if (use_length
&& units_written
< todo_units
)
9095 /* Escape chars have filled up the buffer prematurely,
9096 and we have actually sent fewer units than planned.
9097 Fix-up the length field of the packet. Use the same
9098 number of characters as before. */
9099 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9101 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9106 /* Normal mode: Send target system values byte by byte, in
9107 increasing byte addresses. Each byte is encoded as a two hex
9109 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9110 units_written
= todo_units
;
9113 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9114 getpkt (&rs
->buf
, 0);
9116 if (rs
->buf
[0] == 'E')
9117 return TARGET_XFER_E_IO
;
9119 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9120 send fewer units than we'd planned. */
9121 *xfered_len_units
= (ULONGEST
) units_written
;
9122 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9125 /* Write memory data directly to the remote machine.
9126 This does not inform the data cache; the data cache uses this.
9127 MEMADDR is the address in the remote memory space.
9128 MYADDR is the address of the buffer in our space.
9129 LEN is the number of bytes.
9131 Return the transferred status, error or OK (an
9132 'enum target_xfer_status' value). Save the number of bytes
9133 transferred in *XFERED_LEN. Only transfer a single packet. */
9136 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9137 ULONGEST len
, int unit_size
,
9138 ULONGEST
*xfered_len
)
9140 const char *packet_format
= NULL
;
9142 /* Check whether the target supports binary download. */
9143 check_binary_download (memaddr
);
9145 switch (packet_support (PACKET_X
))
9148 packet_format
= "X";
9150 case PACKET_DISABLE
:
9151 packet_format
= "M";
9153 case PACKET_SUPPORT_UNKNOWN
:
9154 internal_error (__FILE__
, __LINE__
,
9155 _("remote_write_bytes: bad internal state"));
9157 internal_error (__FILE__
, __LINE__
, _("bad switch"));
9160 return remote_write_bytes_aux (packet_format
,
9161 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9162 packet_format
[0], 1);
9165 /* Read memory data directly from the remote machine.
9166 This does not use the data cache; the data cache uses this.
9167 MEMADDR is the address in the remote memory space.
9168 MYADDR is the address of the buffer in our space.
9169 LEN_UNITS is the number of addressable memory units to read..
9170 UNIT_SIZE is the length in bytes of an addressable unit.
9172 Return the transferred status, error or OK (an
9173 'enum target_xfer_status' value). Save the number of bytes
9174 transferred in *XFERED_LEN_UNITS.
9176 See the comment of remote_write_bytes_aux for an example of
9177 memory read/write exchange between gdb and the stub. */
9180 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9182 int unit_size
, ULONGEST
*xfered_len_units
)
9184 struct remote_state
*rs
= get_remote_state ();
9185 int buf_size_bytes
; /* Max size of packet output buffer. */
9190 buf_size_bytes
= get_memory_read_packet_size ();
9191 /* The packet buffer will be large enough for the payload;
9192 get_memory_packet_size ensures this. */
9194 /* Number of units that will fit. */
9195 todo_units
= std::min (len_units
,
9196 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9198 /* Construct "m"<memaddr>","<len>". */
9199 memaddr
= remote_address_masked (memaddr
);
9200 p
= rs
->buf
.data ();
9202 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9204 p
+= hexnumstr (p
, (ULONGEST
) todo_units
);
9207 getpkt (&rs
->buf
, 0);
9208 if (rs
->buf
[0] == 'E'
9209 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
9210 && rs
->buf
[3] == '\0')
9211 return TARGET_XFER_E_IO
;
9212 /* Reply describes memory byte by byte, each byte encoded as two hex
9214 p
= rs
->buf
.data ();
9215 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9216 /* Return what we have. Let higher layers handle partial reads. */
9217 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9218 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9221 /* Using the set of read-only target sections of remote, read live
9224 For interface/parameters/return description see target.h,
9228 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9232 ULONGEST
*xfered_len
)
9234 const struct target_section
*secp
;
9236 secp
= target_section_by_addr (this, memaddr
);
9238 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9240 ULONGEST memend
= memaddr
+ len
;
9242 const target_section_table
*table
= target_get_section_table (this);
9243 for (const target_section
&p
: *table
)
9245 if (memaddr
>= p
.addr
)
9247 if (memend
<= p
.endaddr
)
9249 /* Entire transfer is within this section. */
9250 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9253 else if (memaddr
>= p
.endaddr
)
9255 /* This section ends before the transfer starts. */
9260 /* This section overlaps the transfer. Just do half. */
9261 len
= p
.endaddr
- memaddr
;
9262 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9269 return TARGET_XFER_EOF
;
9272 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9273 first if the requested memory is unavailable in traceframe.
9274 Otherwise, fall back to remote_read_bytes_1. */
9277 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9278 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9279 ULONGEST
*xfered_len
)
9282 return TARGET_XFER_EOF
;
9284 if (get_traceframe_number () != -1)
9286 std::vector
<mem_range
> available
;
9288 /* If we fail to get the set of available memory, then the
9289 target does not support querying traceframe info, and so we
9290 attempt reading from the traceframe anyway (assuming the
9291 target implements the old QTro packet then). */
9292 if (traceframe_available_memory (&available
, memaddr
, len
))
9294 if (available
.empty () || available
[0].start
!= memaddr
)
9296 enum target_xfer_status res
;
9298 /* Don't read into the traceframe's available
9300 if (!available
.empty ())
9302 LONGEST oldlen
= len
;
9304 len
= available
[0].start
- memaddr
;
9305 gdb_assert (len
<= oldlen
);
9308 /* This goes through the topmost target again. */
9309 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9310 len
, unit_size
, xfered_len
);
9311 if (res
== TARGET_XFER_OK
)
9312 return TARGET_XFER_OK
;
9315 /* No use trying further, we know some memory starting
9316 at MEMADDR isn't available. */
9318 return (*xfered_len
!= 0) ?
9319 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9323 /* Don't try to read more than how much is available, in
9324 case the target implements the deprecated QTro packet to
9325 cater for older GDBs (the target's knowledge of read-only
9326 sections may be outdated by now). */
9327 len
= available
[0].length
;
9331 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9336 /* Sends a packet with content determined by the printf format string
9337 FORMAT and the remaining arguments, then gets the reply. Returns
9338 whether the packet was a success, a failure, or unknown. */
9341 remote_target::remote_send_printf (const char *format
, ...)
9343 struct remote_state
*rs
= get_remote_state ();
9344 int max_size
= get_remote_packet_size ();
9347 va_start (ap
, format
);
9350 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9354 if (size
>= max_size
)
9355 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
9357 if (putpkt (rs
->buf
) < 0)
9358 error (_("Communication problem with target."));
9361 getpkt (&rs
->buf
, 0);
9363 return packet_check_result (rs
->buf
);
9366 /* Flash writing can take quite some time. We'll set
9367 effectively infinite timeout for flash operations.
9368 In future, we'll need to decide on a better approach. */
9369 static const int remote_flash_timeout
= 1000;
9372 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9374 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
9375 enum packet_result ret
;
9376 scoped_restore restore_timeout
9377 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9379 ret
= remote_send_printf ("vFlashErase:%s,%s",
9380 phex (address
, addr_size
),
9384 case PACKET_UNKNOWN
:
9385 error (_("Remote target does not support flash erase"));
9387 error (_("Error erasing flash with vFlashErase packet"));
9394 remote_target::remote_flash_write (ULONGEST address
,
9395 ULONGEST length
, ULONGEST
*xfered_len
,
9396 const gdb_byte
*data
)
9398 scoped_restore restore_timeout
9399 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9400 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9405 remote_target::flash_done ()
9409 scoped_restore restore_timeout
9410 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9412 ret
= remote_send_printf ("vFlashDone");
9416 case PACKET_UNKNOWN
:
9417 error (_("Remote target does not support vFlashDone"));
9419 error (_("Error finishing flash operation"));
9426 /* Stuff for dealing with the packets which are part of this protocol.
9427 See comment at top of file for details. */
9429 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
9430 error to higher layers. Called when a serial error is detected.
9431 The exception message is STRING, followed by a colon and a blank,
9432 the system error message for errno at function entry and final dot
9433 for output compatibility with throw_perror_with_name. */
9436 unpush_and_perror (remote_target
*target
, const char *string
)
9438 int saved_errno
= errno
;
9440 remote_unpush_target (target
);
9441 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
9442 safe_strerror (saved_errno
));
9445 /* Read a single character from the remote end. The current quit
9446 handler is overridden to avoid quitting in the middle of packet
9447 sequence, as that would break communication with the remote server.
9448 See remote_serial_quit_handler for more detail. */
9451 remote_target::readchar (int timeout
)
9454 struct remote_state
*rs
= get_remote_state ();
9457 scoped_restore restore_quit_target
9458 = make_scoped_restore (&curr_quit_handler_target
, this);
9459 scoped_restore restore_quit
9460 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9462 rs
->got_ctrlc_during_io
= 0;
9464 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9466 if (rs
->got_ctrlc_during_io
)
9473 switch ((enum serial_rc
) ch
)
9476 remote_unpush_target (this);
9477 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
9480 unpush_and_perror (this, _("Remote communication error. "
9481 "Target disconnected."));
9483 case SERIAL_TIMEOUT
:
9489 /* Wrapper for serial_write that closes the target and throws if
9490 writing fails. The current quit handler is overridden to avoid
9491 quitting in the middle of packet sequence, as that would break
9492 communication with the remote server. See
9493 remote_serial_quit_handler for more detail. */
9496 remote_target::remote_serial_write (const char *str
, int len
)
9498 struct remote_state
*rs
= get_remote_state ();
9500 scoped_restore restore_quit_target
9501 = make_scoped_restore (&curr_quit_handler_target
, this);
9502 scoped_restore restore_quit
9503 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9505 rs
->got_ctrlc_during_io
= 0;
9507 if (serial_write (rs
->remote_desc
, str
, len
))
9509 unpush_and_perror (this, _("Remote communication error. "
9510 "Target disconnected."));
9513 if (rs
->got_ctrlc_during_io
)
9517 /* Return a string representing an escaped version of BUF, of len N.
9518 E.g. \n is converted to \\n, \t to \\t, etc. */
9521 escape_buffer (const char *buf
, int n
)
9525 stb
.putstrn (buf
, n
, '\\');
9526 return stb
.release ();
9530 remote_target::putpkt (const char *buf
)
9532 return putpkt_binary (buf
, strlen (buf
));
9535 /* Wrapper around remote_target::putpkt to avoid exporting
9539 putpkt (remote_target
*remote
, const char *buf
)
9541 return remote
->putpkt (buf
);
9544 /* Send a packet to the remote machine, with error checking. The data
9545 of the packet is in BUF. The string in BUF can be at most
9546 get_remote_packet_size () - 5 to account for the $, # and checksum,
9547 and for a possible /0 if we are debugging (remote_debug) and want
9548 to print the sent packet as a string. */
9551 remote_target::putpkt_binary (const char *buf
, int cnt
)
9553 struct remote_state
*rs
= get_remote_state ();
9555 unsigned char csum
= 0;
9556 gdb::def_vector
<char> data (cnt
+ 6);
9557 char *buf2
= data
.data ();
9563 /* Catch cases like trying to read memory or listing threads while
9564 we're waiting for a stop reply. The remote server wouldn't be
9565 ready to handle this request, so we'd hang and timeout. We don't
9566 have to worry about this in synchronous mode, because in that
9567 case it's not possible to issue a command while the target is
9568 running. This is not a problem in non-stop mode, because in that
9569 case, the stub is always ready to process serial input. */
9570 if (!target_is_non_stop_p ()
9571 && target_is_async_p ()
9572 && rs
->waiting_for_stop_reply
)
9574 error (_("Cannot execute this command while the target is running.\n"
9575 "Use the \"interrupt\" command to stop the target\n"
9576 "and then try again."));
9579 /* Copy the packet into buffer BUF2, encapsulating it
9580 and giving it a checksum. */
9585 for (i
= 0; i
< cnt
; i
++)
9591 *p
++ = tohex ((csum
>> 4) & 0xf);
9592 *p
++ = tohex (csum
& 0xf);
9594 /* Send it over and over until we get a positive ack. */
9602 int len
= (int) (p
- buf2
);
9605 if (remote_packet_max_chars
< 0)
9608 max_chars
= remote_packet_max_chars
;
9611 = escape_buffer (buf2
, std::min (len
, max_chars
));
9613 if (len
> max_chars
)
9614 remote_debug_printf_nofunc
9615 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
9618 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
9620 remote_serial_write (buf2
, p
- buf2
);
9622 /* If this is a no acks version of the remote protocol, send the
9623 packet and move on. */
9627 /* Read until either a timeout occurs (-2) or '+' is read.
9628 Handle any notification that arrives in the mean time. */
9631 ch
= readchar (remote_timeout
);
9636 remote_debug_printf_nofunc ("Received Ack");
9639 remote_debug_printf_nofunc ("Received Nak");
9641 case SERIAL_TIMEOUT
:
9645 break; /* Retransmit buffer. */
9648 remote_debug_printf ("Packet instead of Ack, ignoring it");
9649 /* It's probably an old response sent because an ACK
9650 was lost. Gobble up the packet and ack it so it
9651 doesn't get retransmitted when we resend this
9654 remote_serial_write ("+", 1);
9655 continue; /* Now, go look for +. */
9662 /* If we got a notification, handle it, and go back to looking
9664 /* We've found the start of a notification. Now
9665 collect the data. */
9666 val
= read_frame (&rs
->buf
);
9669 remote_debug_printf_nofunc
9670 (" Notification received: %s",
9671 escape_buffer (rs
->buf
.data (), val
).c_str ());
9673 handle_notification (rs
->notif_state
, rs
->buf
.data ());
9674 /* We're in sync now, rewait for the ack. */
9678 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9684 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9688 break; /* Here to retransmit. */
9692 /* This is wrong. If doing a long backtrace, the user should be
9693 able to get out next time we call QUIT, without anything as
9694 violent as interrupt_query. If we want to provide a way out of
9695 here without getting to the next QUIT, it should be based on
9696 hitting ^C twice as in remote_wait. */
9708 /* Come here after finding the start of a frame when we expected an
9709 ack. Do our best to discard the rest of this packet. */
9712 remote_target::skip_frame ()
9718 c
= readchar (remote_timeout
);
9721 case SERIAL_TIMEOUT
:
9722 /* Nothing we can do. */
9725 /* Discard the two bytes of checksum and stop. */
9726 c
= readchar (remote_timeout
);
9728 c
= readchar (remote_timeout
);
9731 case '*': /* Run length encoding. */
9732 /* Discard the repeat count. */
9733 c
= readchar (remote_timeout
);
9738 /* A regular character. */
9744 /* Come here after finding the start of the frame. Collect the rest
9745 into *BUF, verifying the checksum, length, and handling run-length
9746 compression. NUL terminate the buffer. If there is not enough room,
9749 Returns -1 on error, number of characters in buffer (ignoring the
9750 trailing NULL) on success. (could be extended to return one of the
9751 SERIAL status indications). */
9754 remote_target::read_frame (gdb::char_vector
*buf_p
)
9759 char *buf
= buf_p
->data ();
9760 struct remote_state
*rs
= get_remote_state ();
9767 c
= readchar (remote_timeout
);
9770 case SERIAL_TIMEOUT
:
9771 remote_debug_printf ("Timeout in mid-packet, retrying");
9775 remote_debug_printf ("Saw new packet start in middle of old one");
9776 return -1; /* Start a new packet, count retries. */
9780 unsigned char pktcsum
;
9786 check_0
= readchar (remote_timeout
);
9788 check_1
= readchar (remote_timeout
);
9790 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
9792 remote_debug_printf ("Timeout in checksum, retrying");
9795 else if (check_0
< 0 || check_1
< 0)
9797 remote_debug_printf ("Communication error in checksum");
9801 /* Don't recompute the checksum; with no ack packets we
9802 don't have any way to indicate a packet retransmission
9807 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
9808 if (csum
== pktcsum
)
9812 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
9813 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
9815 /* Number of characters in buffer ignoring trailing
9819 case '*': /* Run length encoding. */
9824 c
= readchar (remote_timeout
);
9826 repeat
= c
- ' ' + 3; /* Compute repeat count. */
9828 /* The character before ``*'' is repeated. */
9830 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
9832 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
9834 /* Make some more room in the buffer. */
9835 buf_p
->resize (buf_p
->size () + repeat
);
9836 buf
= buf_p
->data ();
9839 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
9845 gdb_printf (_("Invalid run length encoding: %s\n"), buf
);
9849 if (bc
>= buf_p
->size () - 1)
9851 /* Make some more room in the buffer. */
9852 buf_p
->resize (buf_p
->size () * 2);
9853 buf
= buf_p
->data ();
9863 /* Set this to the maximum number of seconds to wait instead of waiting forever
9864 in target_wait(). If this timer times out, then it generates an error and
9865 the command is aborted. This replaces most of the need for timeouts in the
9866 GDB test suite, and makes it possible to distinguish between a hung target
9867 and one with slow communications. */
9869 static int watchdog
= 0;
9871 show_watchdog (struct ui_file
*file
, int from_tty
,
9872 struct cmd_list_element
*c
, const char *value
)
9874 gdb_printf (file
, _("Watchdog timer is %s.\n"), value
);
9877 /* Read a packet from the remote machine, with error checking, and
9878 store it in *BUF. Resize *BUF if necessary to hold the result. If
9879 FOREVER, wait forever rather than timing out; this is used (in
9880 synchronous mode) to wait for a target that is is executing user
9882 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9883 don't have to change all the calls to getpkt to deal with the
9884 return value, because at the moment I don't know what the right
9885 thing to do it for those. */
9888 remote_target::getpkt (gdb::char_vector
*buf
, int forever
)
9890 getpkt_sane (buf
, forever
);
9894 /* Read a packet from the remote machine, with error checking, and
9895 store it in *BUF. Resize *BUF if necessary to hold the result. If
9896 FOREVER, wait forever rather than timing out; this is used (in
9897 synchronous mode) to wait for a target that is is executing user
9898 code to stop. If FOREVER == 0, this function is allowed to time
9899 out gracefully and return an indication of this to the caller.
9900 Otherwise return the number of bytes read. If EXPECTING_NOTIF,
9901 consider receiving a notification enough reason to return to the
9902 caller. *IS_NOTIF is an output boolean that indicates whether *BUF
9903 holds a notification or not (a regular packet). */
9906 remote_target::getpkt_or_notif_sane_1 (gdb::char_vector
*buf
,
9907 int forever
, int expecting_notif
,
9910 struct remote_state
*rs
= get_remote_state ();
9916 strcpy (buf
->data (), "timeout");
9919 timeout
= watchdog
> 0 ? watchdog
: -1;
9920 else if (expecting_notif
)
9921 timeout
= 0; /* There should already be a char in the buffer. If
9924 timeout
= remote_timeout
;
9928 /* Process any number of notifications, and then return when
9932 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
9934 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
9936 /* This can loop forever if the remote side sends us
9937 characters continuously, but if it pauses, we'll get
9938 SERIAL_TIMEOUT from readchar because of timeout. Then
9939 we'll count that as a retry.
9941 Note that even when forever is set, we will only wait
9942 forever prior to the start of a packet. After that, we
9943 expect characters to arrive at a brisk pace. They should
9944 show up within remote_timeout intervals. */
9946 c
= readchar (timeout
);
9947 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
9949 if (c
== SERIAL_TIMEOUT
)
9951 if (expecting_notif
)
9952 return -1; /* Don't complain, it's normal to not get
9953 anything in this case. */
9955 if (forever
) /* Watchdog went off? Kill the target. */
9957 remote_unpush_target (this);
9958 throw_error (TARGET_CLOSE_ERROR
,
9959 _("Watchdog timeout has expired. "
9960 "Target detached."));
9963 remote_debug_printf ("Timed out.");
9967 /* We've found the start of a packet or notification.
9968 Now collect the data. */
9969 val
= read_frame (buf
);
9974 remote_serial_write ("-", 1);
9977 if (tries
> MAX_TRIES
)
9979 /* We have tried hard enough, and just can't receive the
9980 packet/notification. Give up. */
9981 gdb_printf (_("Ignoring packet error, continuing...\n"));
9983 /* Skip the ack char if we're in no-ack mode. */
9984 if (!rs
->noack_mode
)
9985 remote_serial_write ("+", 1);
9989 /* If we got an ordinary packet, return that to our caller. */
9996 if (remote_packet_max_chars
< 0)
9999 max_chars
= remote_packet_max_chars
;
10002 = escape_buffer (buf
->data (),
10003 std::min (val
, max_chars
));
10005 if (val
> max_chars
)
10006 remote_debug_printf_nofunc
10007 ("Packet received: %s [%d bytes omitted]", str
.c_str (),
10010 remote_debug_printf_nofunc ("Packet received: %s",
10014 /* Skip the ack char if we're in no-ack mode. */
10015 if (!rs
->noack_mode
)
10016 remote_serial_write ("+", 1);
10017 if (is_notif
!= NULL
)
10022 /* If we got a notification, handle it, and go back to looking
10026 gdb_assert (c
== '%');
10028 remote_debug_printf_nofunc
10029 (" Notification received: %s",
10030 escape_buffer (buf
->data (), val
).c_str ());
10032 if (is_notif
!= NULL
)
10035 handle_notification (rs
->notif_state
, buf
->data ());
10037 /* Notifications require no acknowledgement. */
10039 if (expecting_notif
)
10046 remote_target::getpkt_sane (gdb::char_vector
*buf
, int forever
)
10048 return getpkt_or_notif_sane_1 (buf
, forever
, 0, NULL
);
10052 remote_target::getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
10055 return getpkt_or_notif_sane_1 (buf
, forever
, 1, is_notif
);
10058 /* Kill any new fork children of inferior INF that haven't been
10059 processed by follow_fork. */
10062 remote_target::kill_new_fork_children (inferior
*inf
)
10064 remote_state
*rs
= get_remote_state ();
10065 struct notif_client
*notif
= ¬if_client_stop
;
10067 /* Kill the fork child threads of any threads in inferior INF that are stopped
10068 at a fork event. */
10069 for (thread_info
*thread
: inf
->non_exited_threads ())
10071 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10076 int child_pid
= ws
->child_ptid ().pid ();
10077 int res
= remote_vkill (child_pid
);
10080 error (_("Can't kill fork child process %d"), child_pid
);
10083 /* Check for any pending fork events (not reported or processed yet)
10084 in inferior INF and kill those fork child threads as well. */
10085 remote_notif_get_pending_events (notif
);
10086 for (auto &event
: rs
->stop_reply_queue
)
10088 if (event
->ptid
.pid () != inf
->pid
)
10091 if (!is_fork_status (event
->ws
.kind ()))
10094 int child_pid
= event
->ws
.child_ptid ().pid ();
10095 int res
= remote_vkill (child_pid
);
10098 error (_("Can't kill fork child process %d"), child_pid
);
10103 /* Target hook to kill the current inferior. */
10106 remote_target::kill ()
10109 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10110 struct remote_state
*rs
= get_remote_state ();
10112 gdb_assert (inf
!= nullptr);
10114 if (packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10116 /* If we're stopped while forking and we haven't followed yet,
10117 kill the child task. We need to do this before killing the
10118 parent task because if this is a vfork then the parent will
10120 kill_new_fork_children (inf
);
10122 res
= remote_vkill (inf
->pid
);
10125 target_mourn_inferior (inferior_ptid
);
10130 /* If we are in 'target remote' mode and we are killing the only
10131 inferior, then we will tell gdbserver to exit and unpush the
10133 if (res
== -1 && !remote_multi_process_p (rs
)
10134 && number_of_live_inferiors (this) == 1)
10138 /* We've killed the remote end, we get to mourn it. If we are
10139 not in extended mode, mourning the inferior also unpushes
10140 remote_ops from the target stack, which closes the remote
10142 target_mourn_inferior (inferior_ptid
);
10147 error (_("Can't kill process"));
10150 /* Send a kill request to the target using the 'vKill' packet. */
10153 remote_target::remote_vkill (int pid
)
10155 if (packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10158 remote_state
*rs
= get_remote_state ();
10160 /* Tell the remote target to detach. */
10161 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10163 getpkt (&rs
->buf
, 0);
10165 switch (packet_ok (rs
->buf
,
10166 &remote_protocol_packets
[PACKET_vKill
]))
10172 case PACKET_UNKNOWN
:
10175 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
10179 /* Send a kill request to the target using the 'k' packet. */
10182 remote_target::remote_kill_k ()
10184 /* Catch errors so the user can quit from gdb even when we
10185 aren't on speaking terms with the remote system. */
10190 catch (const gdb_exception_error
&ex
)
10192 if (ex
.error
== TARGET_CLOSE_ERROR
)
10194 /* If we got an (EOF) error that caused the target
10195 to go away, then we're done, that's what we wanted.
10196 "k" is susceptible to cause a premature EOF, given
10197 that the remote server isn't actually required to
10198 reply to "k", and it can happen that it doesn't
10199 even get to reply ACK to the "k". */
10203 /* Otherwise, something went wrong. We didn't actually kill
10204 the target. Just propagate the exception, and let the
10205 user or higher layers decide what to do. */
10211 remote_target::mourn_inferior ()
10213 struct remote_state
*rs
= get_remote_state ();
10215 /* We're no longer interested in notification events of an inferior
10216 that exited or was killed/detached. */
10217 discard_pending_stop_replies (current_inferior ());
10219 /* In 'target remote' mode with one inferior, we close the connection. */
10220 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10222 remote_unpush_target (this);
10226 /* In case we got here due to an error, but we're going to stay
10228 rs
->waiting_for_stop_reply
= 0;
10230 /* If the current general thread belonged to the process we just
10231 detached from or has exited, the remote side current general
10232 thread becomes undefined. Considering a case like this:
10234 - We just got here due to a detach.
10235 - The process that we're detaching from happens to immediately
10236 report a global breakpoint being hit in non-stop mode, in the
10237 same thread we had selected before.
10238 - GDB attaches to this process again.
10239 - This event happens to be the next event we handle.
10241 GDB would consider that the current general thread didn't need to
10242 be set on the stub side (with Hg), since for all it knew,
10243 GENERAL_THREAD hadn't changed.
10245 Notice that although in all-stop mode, the remote server always
10246 sets the current thread to the thread reporting the stop event,
10247 that doesn't happen in non-stop mode; in non-stop, the stub *must
10248 not* change the current thread when reporting a breakpoint hit,
10249 due to the decoupling of event reporting and event handling.
10251 To keep things simple, we always invalidate our notion of the
10253 record_currthread (rs
, minus_one_ptid
);
10255 /* Call common code to mark the inferior as not running. */
10256 generic_mourn_inferior ();
10260 extended_remote_target::supports_disable_randomization ()
10262 return packet_support (PACKET_QDisableRandomization
) == PACKET_ENABLE
;
10266 remote_target::extended_remote_disable_randomization (int val
)
10268 struct remote_state
*rs
= get_remote_state ();
10271 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10272 "QDisableRandomization:%x", val
);
10274 reply
= remote_get_noisy_reply ();
10275 if (*reply
== '\0')
10276 error (_("Target does not support QDisableRandomization."));
10277 if (strcmp (reply
, "OK") != 0)
10278 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10282 remote_target::extended_remote_run (const std::string
&args
)
10284 struct remote_state
*rs
= get_remote_state ();
10286 const char *remote_exec_file
= get_remote_exec_file ();
10288 /* If the user has disabled vRun support, or we have detected that
10289 support is not available, do not try it. */
10290 if (packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10293 strcpy (rs
->buf
.data (), "vRun;");
10294 len
= strlen (rs
->buf
.data ());
10296 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10297 error (_("Remote file name too long for run packet"));
10298 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10299 strlen (remote_exec_file
));
10301 if (!args
.empty ())
10305 gdb_argv
argv (args
.c_str ());
10306 for (i
= 0; argv
[i
] != NULL
; i
++)
10308 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10309 error (_("Argument list too long for run packet"));
10310 rs
->buf
[len
++] = ';';
10311 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10316 rs
->buf
[len
++] = '\0';
10319 getpkt (&rs
->buf
, 0);
10321 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]))
10324 /* We have a wait response. All is well. */
10326 case PACKET_UNKNOWN
:
10329 if (remote_exec_file
[0] == '\0')
10330 error (_("Running the default executable on the remote target failed; "
10331 "try \"set remote exec-file\"?"));
10333 error (_("Running \"%s\" on the remote target failed"),
10336 gdb_assert_not_reached ("bad switch");
10340 /* Helper function to send set/unset environment packets. ACTION is
10341 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10342 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10346 remote_target::send_environment_packet (const char *action
,
10347 const char *packet
,
10350 remote_state
*rs
= get_remote_state ();
10352 /* Convert the environment variable to an hex string, which
10353 is the best format to be transmitted over the wire. */
10354 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10357 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10358 "%s:%s", packet
, encoded_value
.c_str ());
10361 getpkt (&rs
->buf
, 0);
10362 if (strcmp (rs
->buf
.data (), "OK") != 0)
10363 warning (_("Unable to %s environment variable '%s' on remote."),
10367 /* Helper function to handle the QEnvironment* packets. */
10370 remote_target::extended_remote_environment_support ()
10372 remote_state
*rs
= get_remote_state ();
10374 if (packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10376 putpkt ("QEnvironmentReset");
10377 getpkt (&rs
->buf
, 0);
10378 if (strcmp (rs
->buf
.data (), "OK") != 0)
10379 warning (_("Unable to reset environment on remote."));
10382 gdb_environ
*e
= ¤t_inferior ()->environment
;
10384 if (packet_support (PACKET_QEnvironmentHexEncoded
) != PACKET_DISABLE
)
10385 for (const std::string
&el
: e
->user_set_env ())
10386 send_environment_packet ("set", "QEnvironmentHexEncoded",
10389 if (packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10390 for (const std::string
&el
: e
->user_unset_env ())
10391 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10394 /* Helper function to set the current working directory for the
10395 inferior in the remote target. */
10398 remote_target::extended_remote_set_inferior_cwd ()
10400 if (packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10402 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10403 remote_state
*rs
= get_remote_state ();
10405 if (!inferior_cwd
.empty ())
10407 std::string hexpath
10408 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10409 inferior_cwd
.size ());
10411 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10412 "QSetWorkingDir:%s", hexpath
.c_str ());
10416 /* An empty inferior_cwd means that the user wants us to
10417 reset the remote server's inferior's cwd. */
10418 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10419 "QSetWorkingDir:");
10423 getpkt (&rs
->buf
, 0);
10424 if (packet_ok (rs
->buf
,
10425 &remote_protocol_packets
[PACKET_QSetWorkingDir
])
10428 Remote replied unexpectedly while setting the inferior's working\n\
10435 /* In the extended protocol we want to be able to do things like
10436 "run" and have them basically work as expected. So we need
10437 a special create_inferior function. We support changing the
10438 executable file and the command line arguments, but not the
10442 extended_remote_target::create_inferior (const char *exec_file
,
10443 const std::string
&args
,
10444 char **env
, int from_tty
)
10448 struct remote_state
*rs
= get_remote_state ();
10449 const char *remote_exec_file
= get_remote_exec_file ();
10451 /* If running asynchronously, register the target file descriptor
10452 with the event loop. */
10453 if (target_can_async_p ())
10454 target_async (true);
10456 /* Disable address space randomization if requested (and supported). */
10457 if (supports_disable_randomization ())
10458 extended_remote_disable_randomization (disable_randomization
);
10460 /* If startup-with-shell is on, we inform gdbserver to start the
10461 remote inferior using a shell. */
10462 if (packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10464 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10465 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
10467 getpkt (&rs
->buf
, 0);
10468 if (strcmp (rs
->buf
.data (), "OK") != 0)
10470 Remote replied unexpectedly while setting startup-with-shell: %s"),
10474 extended_remote_environment_support ();
10476 extended_remote_set_inferior_cwd ();
10478 /* Now restart the remote server. */
10479 run_worked
= extended_remote_run (args
) != -1;
10482 /* vRun was not supported. Fail if we need it to do what the
10484 if (remote_exec_file
[0])
10485 error (_("Remote target does not support \"set remote exec-file\""));
10486 if (!args
.empty ())
10487 error (_("Remote target does not support \"set args\" or run ARGS"));
10489 /* Fall back to "R". */
10490 extended_remote_restart ();
10493 /* vRun's success return is a stop reply. */
10494 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
10495 add_current_inferior_and_thread (stop_reply
);
10497 /* Get updated offsets, if the stub uses qOffsets. */
10502 /* Given a location's target info BP_TGT and the packet buffer BUF, output
10503 the list of conditions (in agent expression bytecode format), if any, the
10504 target needs to evaluate. The output is placed into the packet buffer
10505 started from BUF and ended at BUF_END. */
10508 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
10509 struct bp_target_info
*bp_tgt
, char *buf
,
10512 if (bp_tgt
->conditions
.empty ())
10515 buf
+= strlen (buf
);
10516 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
10519 /* Send conditions to the target. */
10520 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
10522 xsnprintf (buf
, buf_end
- buf
, "X%x,", aexpr
->len
);
10523 buf
+= strlen (buf
);
10524 for (int i
= 0; i
< aexpr
->len
; ++i
)
10525 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10532 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
10533 struct bp_target_info
*bp_tgt
, char *buf
)
10535 if (bp_tgt
->tcommands
.empty ())
10538 buf
+= strlen (buf
);
10540 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
10541 buf
+= strlen (buf
);
10543 /* Concatenate all the agent expressions that are commands into the
10545 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
10547 sprintf (buf
, "X%x,", aexpr
->len
);
10548 buf
+= strlen (buf
);
10549 for (int i
= 0; i
< aexpr
->len
; ++i
)
10550 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10555 /* Insert a breakpoint. On targets that have software breakpoint
10556 support, we ask the remote target to do the work; on targets
10557 which don't, we insert a traditional memory breakpoint. */
10560 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
10561 struct bp_target_info
*bp_tgt
)
10563 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10564 If it succeeds, then set the support to PACKET_ENABLE. If it
10565 fails, and the user has explicitly requested the Z support then
10566 report an error, otherwise, mark it disabled and go on. */
10568 if (packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10570 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10571 struct remote_state
*rs
;
10574 /* Make sure the remote is pointing at the right process, if
10576 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10577 set_general_process ();
10579 rs
= get_remote_state ();
10580 p
= rs
->buf
.data ();
10581 endbuf
= p
+ get_remote_packet_size ();
10586 addr
= (ULONGEST
) remote_address_masked (addr
);
10587 p
+= hexnumstr (p
, addr
);
10588 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10590 if (supports_evaluation_of_breakpoint_conditions ())
10591 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10593 if (can_run_breakpoint_commands ())
10594 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10597 getpkt (&rs
->buf
, 0);
10599 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
10605 case PACKET_UNKNOWN
:
10610 /* If this breakpoint has target-side commands but this stub doesn't
10611 support Z0 packets, throw error. */
10612 if (!bp_tgt
->tcommands
.empty ())
10613 throw_error (NOT_SUPPORTED_ERROR
, _("\
10614 Target doesn't support breakpoints that have target side commands."));
10616 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
10620 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
10621 struct bp_target_info
*bp_tgt
,
10622 enum remove_bp_reason reason
)
10624 CORE_ADDR addr
= bp_tgt
->placed_address
;
10625 struct remote_state
*rs
= get_remote_state ();
10627 if (packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10629 char *p
= rs
->buf
.data ();
10630 char *endbuf
= p
+ get_remote_packet_size ();
10632 /* Make sure the remote is pointing at the right process, if
10634 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10635 set_general_process ();
10641 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
10642 p
+= hexnumstr (p
, addr
);
10643 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10646 getpkt (&rs
->buf
, 0);
10648 return (rs
->buf
[0] == 'E');
10651 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
10654 static enum Z_packet_type
10655 watchpoint_to_Z_packet (int type
)
10660 return Z_PACKET_WRITE_WP
;
10663 return Z_PACKET_READ_WP
;
10666 return Z_PACKET_ACCESS_WP
;
10669 internal_error (__FILE__
, __LINE__
,
10670 _("hw_bp_to_z: bad watchpoint type %d"), type
);
10675 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
10676 enum target_hw_bp_type type
, struct expression
*cond
)
10678 struct remote_state
*rs
= get_remote_state ();
10679 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10681 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10683 if (packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10686 /* Make sure the remote is pointing at the right process, if
10688 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10689 set_general_process ();
10691 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
10692 p
= strchr (rs
->buf
.data (), '\0');
10693 addr
= remote_address_masked (addr
);
10694 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10695 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10698 getpkt (&rs
->buf
, 0);
10700 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
10704 case PACKET_UNKNOWN
:
10709 internal_error (__FILE__
, __LINE__
,
10710 _("remote_insert_watchpoint: reached end of function"));
10714 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
10715 CORE_ADDR start
, int length
)
10717 CORE_ADDR diff
= remote_address_masked (addr
- start
);
10719 return diff
< length
;
10724 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
10725 enum target_hw_bp_type type
, struct expression
*cond
)
10727 struct remote_state
*rs
= get_remote_state ();
10728 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10730 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10732 if (packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10735 /* Make sure the remote is pointing at the right process, if
10737 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10738 set_general_process ();
10740 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
10741 p
= strchr (rs
->buf
.data (), '\0');
10742 addr
= remote_address_masked (addr
);
10743 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10744 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10746 getpkt (&rs
->buf
, 0);
10748 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
10751 case PACKET_UNKNOWN
:
10756 internal_error (__FILE__
, __LINE__
,
10757 _("remote_remove_watchpoint: reached end of function"));
10761 static int remote_hw_watchpoint_limit
= -1;
10762 static int remote_hw_watchpoint_length_limit
= -1;
10763 static int remote_hw_breakpoint_limit
= -1;
10766 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
10768 if (remote_hw_watchpoint_length_limit
== 0)
10770 else if (remote_hw_watchpoint_length_limit
< 0)
10772 else if (len
<= remote_hw_watchpoint_length_limit
)
10779 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
10781 if (type
== bp_hardware_breakpoint
)
10783 if (remote_hw_breakpoint_limit
== 0)
10785 else if (remote_hw_breakpoint_limit
< 0)
10787 else if (cnt
<= remote_hw_breakpoint_limit
)
10792 if (remote_hw_watchpoint_limit
== 0)
10794 else if (remote_hw_watchpoint_limit
< 0)
10798 else if (cnt
<= remote_hw_watchpoint_limit
)
10804 /* The to_stopped_by_sw_breakpoint method of target remote. */
10807 remote_target::stopped_by_sw_breakpoint ()
10809 struct thread_info
*thread
= inferior_thread ();
10811 return (thread
->priv
!= NULL
10812 && (get_remote_thread_info (thread
)->stop_reason
10813 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
10816 /* The to_supports_stopped_by_sw_breakpoint method of target
10820 remote_target::supports_stopped_by_sw_breakpoint ()
10822 return (packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
10825 /* The to_stopped_by_hw_breakpoint method of target remote. */
10828 remote_target::stopped_by_hw_breakpoint ()
10830 struct thread_info
*thread
= inferior_thread ();
10832 return (thread
->priv
!= NULL
10833 && (get_remote_thread_info (thread
)->stop_reason
10834 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
10837 /* The to_supports_stopped_by_hw_breakpoint method of target
10841 remote_target::supports_stopped_by_hw_breakpoint ()
10843 return (packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
10847 remote_target::stopped_by_watchpoint ()
10849 struct thread_info
*thread
= inferior_thread ();
10851 return (thread
->priv
!= NULL
10852 && (get_remote_thread_info (thread
)->stop_reason
10853 == TARGET_STOPPED_BY_WATCHPOINT
));
10857 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
10859 struct thread_info
*thread
= inferior_thread ();
10861 if (thread
->priv
!= NULL
10862 && (get_remote_thread_info (thread
)->stop_reason
10863 == TARGET_STOPPED_BY_WATCHPOINT
))
10865 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
10874 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
10875 struct bp_target_info
*bp_tgt
)
10877 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10878 struct remote_state
*rs
;
10882 if (packet_support (PACKET_Z1
) == PACKET_DISABLE
)
10885 /* Make sure the remote is pointing at the right process, if
10887 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10888 set_general_process ();
10890 rs
= get_remote_state ();
10891 p
= rs
->buf
.data ();
10892 endbuf
= p
+ get_remote_packet_size ();
10898 addr
= remote_address_masked (addr
);
10899 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10900 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
10902 if (supports_evaluation_of_breakpoint_conditions ())
10903 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10905 if (can_run_breakpoint_commands ())
10906 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10909 getpkt (&rs
->buf
, 0);
10911 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
10914 if (rs
->buf
[1] == '.')
10916 message
= strchr (&rs
->buf
[2], '.');
10918 error (_("Remote failure reply: %s"), message
+ 1);
10921 case PACKET_UNKNOWN
:
10926 internal_error (__FILE__
, __LINE__
,
10927 _("remote_insert_hw_breakpoint: reached end of function"));
10932 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
10933 struct bp_target_info
*bp_tgt
)
10936 struct remote_state
*rs
= get_remote_state ();
10937 char *p
= rs
->buf
.data ();
10938 char *endbuf
= p
+ get_remote_packet_size ();
10940 if (packet_support (PACKET_Z1
) == PACKET_DISABLE
)
10943 /* Make sure the remote is pointing at the right process, if
10945 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10946 set_general_process ();
10952 addr
= remote_address_masked (bp_tgt
->placed_address
);
10953 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10954 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
10957 getpkt (&rs
->buf
, 0);
10959 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
10962 case PACKET_UNKNOWN
:
10967 internal_error (__FILE__
, __LINE__
,
10968 _("remote_remove_hw_breakpoint: reached end of function"));
10971 /* Verify memory using the "qCRC:" request. */
10974 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
10976 struct remote_state
*rs
= get_remote_state ();
10977 unsigned long host_crc
, target_crc
;
10980 /* It doesn't make sense to use qCRC if the remote target is
10981 connected but not running. */
10982 if (target_has_execution ()
10983 && packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
10985 enum packet_result result
;
10987 /* Make sure the remote is pointing at the right process. */
10988 set_general_process ();
10990 /* FIXME: assumes lma can fit into long. */
10991 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
10992 (long) lma
, (long) size
);
10995 /* Be clever; compute the host_crc before waiting for target
10997 host_crc
= xcrc32 (data
, size
, 0xffffffff);
10999 getpkt (&rs
->buf
, 0);
11001 result
= packet_ok (rs
->buf
,
11002 &remote_protocol_packets
[PACKET_qCRC
]);
11003 if (result
== PACKET_ERROR
)
11005 else if (result
== PACKET_OK
)
11007 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11008 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11010 return (host_crc
== target_crc
);
11014 return simple_verify_memory (this, data
, lma
, size
);
11017 /* compare-sections command
11019 With no arguments, compares each loadable section in the exec bfd
11020 with the same memory range on the target, and reports mismatches.
11021 Useful for verifying the image on the target against the exec file. */
11024 compare_sections_command (const char *args
, int from_tty
)
11027 const char *sectname
;
11028 bfd_size_type size
;
11031 int mismatched
= 0;
11035 if (!current_program_space
->exec_bfd ())
11036 error (_("command cannot be used without an exec file"));
11038 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11044 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11046 if (!(s
->flags
& SEC_LOAD
))
11047 continue; /* Skip non-loadable section. */
11049 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11050 continue; /* Skip writeable sections */
11052 size
= bfd_section_size (s
);
11054 continue; /* Skip zero-length section. */
11056 sectname
= bfd_section_name (s
);
11057 if (args
&& strcmp (args
, sectname
) != 0)
11058 continue; /* Not the section selected by user. */
11060 matched
= 1; /* Do this section. */
11063 gdb::byte_vector
sectdata (size
);
11064 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11065 sectdata
.data (), 0, size
);
11067 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11070 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11071 paddress (target_gdbarch (), lma
),
11072 paddress (target_gdbarch (), lma
+ size
));
11074 gdb_printf ("Section %s, range %s -- %s: ", sectname
,
11075 paddress (target_gdbarch (), lma
),
11076 paddress (target_gdbarch (), lma
+ size
));
11078 gdb_printf ("matched.\n");
11081 gdb_printf ("MIS-MATCHED!\n");
11085 if (mismatched
> 0)
11086 warning (_("One or more sections of the target image does not match\n\
11087 the loaded file\n"));
11088 if (args
&& !matched
)
11089 gdb_printf (_("No loaded section named '%s'.\n"), args
);
11092 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11093 into remote target. The number of bytes written to the remote
11094 target is returned, or -1 for error. */
11097 remote_target::remote_write_qxfer (const char *object_name
,
11098 const char *annex
, const gdb_byte
*writebuf
,
11099 ULONGEST offset
, LONGEST len
,
11100 ULONGEST
*xfered_len
,
11101 struct packet_config
*packet
)
11105 struct remote_state
*rs
= get_remote_state ();
11106 int max_size
= get_memory_write_packet_size ();
11108 if (packet_config_support (packet
) == PACKET_DISABLE
)
11109 return TARGET_XFER_E_IO
;
11111 /* Insert header. */
11112 i
= snprintf (rs
->buf
.data (), max_size
,
11113 "qXfer:%s:write:%s:%s:",
11114 object_name
, annex
? annex
: "",
11115 phex_nz (offset
, sizeof offset
));
11116 max_size
-= (i
+ 1);
11118 /* Escape as much data as fits into rs->buf. */
11119 buf_len
= remote_escape_output
11120 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11122 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11123 || getpkt_sane (&rs
->buf
, 0) < 0
11124 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11125 return TARGET_XFER_E_IO
;
11127 unpack_varlen_hex (rs
->buf
.data (), &n
);
11130 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11133 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11134 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11135 number of bytes read is returned, or 0 for EOF, or -1 for error.
11136 The number of bytes read may be less than LEN without indicating an
11137 EOF. PACKET is checked and updated to indicate whether the remote
11138 target supports this object. */
11141 remote_target::remote_read_qxfer (const char *object_name
,
11143 gdb_byte
*readbuf
, ULONGEST offset
,
11145 ULONGEST
*xfered_len
,
11146 struct packet_config
*packet
)
11148 struct remote_state
*rs
= get_remote_state ();
11149 LONGEST i
, n
, packet_len
;
11151 if (packet_config_support (packet
) == PACKET_DISABLE
)
11152 return TARGET_XFER_E_IO
;
11154 /* Check whether we've cached an end-of-object packet that matches
11156 if (rs
->finished_object
)
11158 if (strcmp (object_name
, rs
->finished_object
) == 0
11159 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11160 && offset
== rs
->finished_offset
)
11161 return TARGET_XFER_EOF
;
11164 /* Otherwise, we're now reading something different. Discard
11166 xfree (rs
->finished_object
);
11167 xfree (rs
->finished_annex
);
11168 rs
->finished_object
= NULL
;
11169 rs
->finished_annex
= NULL
;
11172 /* Request only enough to fit in a single packet. The actual data
11173 may not, since we don't know how much of it will need to be escaped;
11174 the target is free to respond with slightly less data. We subtract
11175 five to account for the response type and the protocol frame. */
11176 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11177 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11178 "qXfer:%s:read:%s:%s,%s",
11179 object_name
, annex
? annex
: "",
11180 phex_nz (offset
, sizeof offset
),
11181 phex_nz (n
, sizeof n
));
11182 i
= putpkt (rs
->buf
);
11184 return TARGET_XFER_E_IO
;
11187 packet_len
= getpkt_sane (&rs
->buf
, 0);
11188 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11189 return TARGET_XFER_E_IO
;
11191 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11192 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11194 /* 'm' means there is (or at least might be) more data after this
11195 batch. That does not make sense unless there's at least one byte
11196 of data in this reply. */
11197 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11198 error (_("Remote qXfer reply contained no data."));
11200 /* Got some data. */
11201 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11202 packet_len
- 1, readbuf
, n
);
11204 /* 'l' is an EOF marker, possibly including a final block of data,
11205 or possibly empty. If we have the final block of a non-empty
11206 object, record this fact to bypass a subsequent partial read. */
11207 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11209 rs
->finished_object
= xstrdup (object_name
);
11210 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11211 rs
->finished_offset
= offset
+ i
;
11215 return TARGET_XFER_EOF
;
11219 return TARGET_XFER_OK
;
11223 enum target_xfer_status
11224 remote_target::xfer_partial (enum target_object object
,
11225 const char *annex
, gdb_byte
*readbuf
,
11226 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11227 ULONGEST
*xfered_len
)
11229 struct remote_state
*rs
;
11233 int unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
11235 set_remote_traceframe ();
11236 set_general_thread (inferior_ptid
);
11238 rs
= get_remote_state ();
11240 /* Handle memory using the standard memory routines. */
11241 if (object
== TARGET_OBJECT_MEMORY
)
11243 /* If the remote target is connected but not running, we should
11244 pass this request down to a lower stratum (e.g. the executable
11246 if (!target_has_execution ())
11247 return TARGET_XFER_EOF
;
11249 if (writebuf
!= NULL
)
11250 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11253 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11257 /* Handle extra signal info using qxfer packets. */
11258 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11261 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11262 xfered_len
, &remote_protocol_packets
11263 [PACKET_qXfer_siginfo_read
]);
11265 return remote_write_qxfer ("siginfo", annex
,
11266 writebuf
, offset
, len
, xfered_len
,
11267 &remote_protocol_packets
11268 [PACKET_qXfer_siginfo_write
]);
11271 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11274 return remote_read_qxfer ("statictrace", annex
,
11275 readbuf
, offset
, len
, xfered_len
,
11276 &remote_protocol_packets
11277 [PACKET_qXfer_statictrace_read
]);
11279 return TARGET_XFER_E_IO
;
11282 /* Only handle flash writes. */
11283 if (writebuf
!= NULL
)
11287 case TARGET_OBJECT_FLASH
:
11288 return remote_flash_write (offset
, len
, xfered_len
,
11292 return TARGET_XFER_E_IO
;
11296 /* Map pre-existing objects onto letters. DO NOT do this for new
11297 objects!!! Instead specify new query packets. */
11300 case TARGET_OBJECT_AVR
:
11304 case TARGET_OBJECT_AUXV
:
11305 gdb_assert (annex
== NULL
);
11306 return remote_read_qxfer ("auxv", annex
, readbuf
, offset
, len
,
11308 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
11310 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11311 return remote_read_qxfer
11312 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11313 &remote_protocol_packets
[PACKET_qXfer_features
]);
11315 case TARGET_OBJECT_LIBRARIES
:
11316 return remote_read_qxfer
11317 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11318 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
11320 case TARGET_OBJECT_LIBRARIES_SVR4
:
11321 return remote_read_qxfer
11322 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11323 &remote_protocol_packets
[PACKET_qXfer_libraries_svr4
]);
11325 case TARGET_OBJECT_MEMORY_MAP
:
11326 gdb_assert (annex
== NULL
);
11327 return remote_read_qxfer ("memory-map", annex
, readbuf
, offset
, len
,
11329 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
11331 case TARGET_OBJECT_OSDATA
:
11332 /* Should only get here if we're connected. */
11333 gdb_assert (rs
->remote_desc
);
11334 return remote_read_qxfer
11335 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11336 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
11338 case TARGET_OBJECT_THREADS
:
11339 gdb_assert (annex
== NULL
);
11340 return remote_read_qxfer ("threads", annex
, readbuf
, offset
, len
,
11342 &remote_protocol_packets
[PACKET_qXfer_threads
]);
11344 case TARGET_OBJECT_TRACEFRAME_INFO
:
11345 gdb_assert (annex
== NULL
);
11346 return remote_read_qxfer
11347 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11348 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
11350 case TARGET_OBJECT_FDPIC
:
11351 return remote_read_qxfer ("fdpic", annex
, readbuf
, offset
, len
,
11353 &remote_protocol_packets
[PACKET_qXfer_fdpic
]);
11355 case TARGET_OBJECT_OPENVMS_UIB
:
11356 return remote_read_qxfer ("uib", annex
, readbuf
, offset
, len
,
11358 &remote_protocol_packets
[PACKET_qXfer_uib
]);
11360 case TARGET_OBJECT_BTRACE
:
11361 return remote_read_qxfer ("btrace", annex
, readbuf
, offset
, len
,
11363 &remote_protocol_packets
[PACKET_qXfer_btrace
]);
11365 case TARGET_OBJECT_BTRACE_CONF
:
11366 return remote_read_qxfer ("btrace-conf", annex
, readbuf
, offset
,
11368 &remote_protocol_packets
[PACKET_qXfer_btrace_conf
]);
11370 case TARGET_OBJECT_EXEC_FILE
:
11371 return remote_read_qxfer ("exec-file", annex
, readbuf
, offset
,
11373 &remote_protocol_packets
[PACKET_qXfer_exec_file
]);
11376 return TARGET_XFER_E_IO
;
11379 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11380 large enough let the caller deal with it. */
11381 if (len
< get_remote_packet_size ())
11382 return TARGET_XFER_E_IO
;
11383 len
= get_remote_packet_size ();
11385 /* Except for querying the minimum buffer size, target must be open. */
11386 if (!rs
->remote_desc
)
11387 error (_("remote query is only available after target open"));
11389 gdb_assert (annex
!= NULL
);
11390 gdb_assert (readbuf
!= NULL
);
11392 p2
= rs
->buf
.data ();
11394 *p2
++ = query_type
;
11396 /* We used one buffer char for the remote protocol q command and
11397 another for the query type. As the remote protocol encapsulation
11398 uses 4 chars plus one extra in case we are debugging
11399 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11402 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11404 /* Bad caller may have sent forbidden characters. */
11405 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11410 gdb_assert (annex
[i
] == '\0');
11412 i
= putpkt (rs
->buf
);
11414 return TARGET_XFER_E_IO
;
11416 getpkt (&rs
->buf
, 0);
11417 strcpy ((char *) readbuf
, rs
->buf
.data ());
11419 *xfered_len
= strlen ((char *) readbuf
);
11420 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11423 /* Implementation of to_get_memory_xfer_limit. */
11426 remote_target::get_memory_xfer_limit ()
11428 return get_memory_write_packet_size ();
11432 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11433 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11434 CORE_ADDR
*found_addrp
)
11436 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
11437 struct remote_state
*rs
= get_remote_state ();
11438 int max_size
= get_memory_write_packet_size ();
11439 struct packet_config
*packet
=
11440 &remote_protocol_packets
[PACKET_qSearch_memory
];
11441 /* Number of packet bytes used to encode the pattern;
11442 this could be more than PATTERN_LEN due to escape characters. */
11443 int escaped_pattern_len
;
11444 /* Amount of pattern that was encodable in the packet. */
11445 int used_pattern_len
;
11448 ULONGEST found_addr
;
11450 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11452 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11456 /* Don't go to the target if we don't have to. This is done before
11457 checking packet_config_support to avoid the possibility that a
11458 success for this edge case means the facility works in
11460 if (pattern_len
> search_space_len
)
11462 if (pattern_len
== 0)
11464 *found_addrp
= start_addr
;
11468 /* If we already know the packet isn't supported, fall back to the simple
11469 way of searching memory. */
11471 if (packet_config_support (packet
) == PACKET_DISABLE
)
11473 /* Target doesn't provided special support, fall back and use the
11474 standard support (copy memory and do the search here). */
11475 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11476 pattern
, pattern_len
, found_addrp
);
11479 /* Make sure the remote is pointing at the right process. */
11480 set_general_process ();
11482 /* Insert header. */
11483 i
= snprintf (rs
->buf
.data (), max_size
,
11484 "qSearch:memory:%s;%s;",
11485 phex_nz (start_addr
, addr_size
),
11486 phex_nz (search_space_len
, sizeof (search_space_len
)));
11487 max_size
-= (i
+ 1);
11489 /* Escape as much data as fits into rs->buf. */
11490 escaped_pattern_len
=
11491 remote_escape_output (pattern
, pattern_len
, 1,
11492 (gdb_byte
*) rs
->buf
.data () + i
,
11493 &used_pattern_len
, max_size
);
11495 /* Bail if the pattern is too large. */
11496 if (used_pattern_len
!= pattern_len
)
11497 error (_("Pattern is too large to transmit to remote target."));
11499 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
11500 || getpkt_sane (&rs
->buf
, 0) < 0
11501 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11503 /* The request may not have worked because the command is not
11504 supported. If so, fall back to the simple way. */
11505 if (packet_config_support (packet
) == PACKET_DISABLE
)
11507 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11508 pattern
, pattern_len
, found_addrp
);
11513 if (rs
->buf
[0] == '0')
11515 else if (rs
->buf
[0] == '1')
11518 if (rs
->buf
[1] != ',')
11519 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11520 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
11521 *found_addrp
= found_addr
;
11524 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11530 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
11532 struct remote_state
*rs
= get_remote_state ();
11533 char *p
= rs
->buf
.data ();
11535 if (!rs
->remote_desc
)
11536 error (_("remote rcmd is only available after target open"));
11538 /* Send a NULL command across as an empty command. */
11539 if (command
== NULL
)
11542 /* The query prefix. */
11543 strcpy (rs
->buf
.data (), "qRcmd,");
11544 p
= strchr (rs
->buf
.data (), '\0');
11546 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
11547 > get_remote_packet_size ())
11548 error (_("\"monitor\" command ``%s'' is too long."), command
);
11550 /* Encode the actual command. */
11551 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
11553 if (putpkt (rs
->buf
) < 0)
11554 error (_("Communication problem with target."));
11556 /* get/display the response */
11561 /* XXX - see also remote_get_noisy_reply(). */
11562 QUIT
; /* Allow user to bail out with ^C. */
11564 if (getpkt_sane (&rs
->buf
, 0) == -1)
11566 /* Timeout. Continue to (try to) read responses.
11567 This is better than stopping with an error, assuming the stub
11568 is still executing the (long) monitor command.
11569 If needed, the user can interrupt gdb using C-c, obtaining
11570 an effect similar to stop on timeout. */
11573 buf
= rs
->buf
.data ();
11574 if (buf
[0] == '\0')
11575 error (_("Target does not support this command."));
11576 if (buf
[0] == 'O' && buf
[1] != 'K')
11578 remote_console_output (buf
+ 1); /* 'O' message from stub. */
11581 if (strcmp (buf
, "OK") == 0)
11583 if (strlen (buf
) == 3 && buf
[0] == 'E'
11584 && isxdigit (buf
[1]) && isxdigit (buf
[2]))
11586 error (_("Protocol error with Rcmd"));
11588 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
11590 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
11592 gdb_putc (c
, outbuf
);
11598 std::vector
<mem_region
>
11599 remote_target::memory_map ()
11601 std::vector
<mem_region
> result
;
11602 gdb::optional
<gdb::char_vector
> text
11603 = target_read_stralloc (current_inferior ()->top_target (),
11604 TARGET_OBJECT_MEMORY_MAP
, NULL
);
11607 result
= parse_memory_map (text
->data ());
11612 /* Set of callbacks used to implement the 'maint packet' command. */
11614 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
11616 /* Called before the packet is sent. BUF is the packet content before
11617 the protocol specific prefix, suffix, and escaping is added. */
11619 void sending (gdb::array_view
<const char> &buf
) override
11621 gdb_puts ("sending: ");
11622 print_packet (buf
);
11626 /* Called with BUF, the reply from the remote target. */
11628 void received (gdb::array_view
<const char> &buf
) override
11630 gdb_puts ("received: \"");
11631 print_packet (buf
);
11637 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
11638 '\x??' with '??' replaced by the hexadecimal value of the byte. */
11641 print_packet (gdb::array_view
<const char> &buf
)
11645 for (int i
= 0; i
< buf
.size (); ++i
)
11647 gdb_byte c
= buf
[i
];
11649 gdb_putc (c
, &stb
);
11651 gdb_printf (&stb
, "\\x%02x", (unsigned char) c
);
11654 gdb_puts (stb
.string ().c_str ());
11658 /* See remote.h. */
11661 send_remote_packet (gdb::array_view
<const char> &buf
,
11662 send_remote_packet_callbacks
*callbacks
)
11664 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
11665 error (_("a remote packet must not be empty"));
11667 remote_target
*remote
= get_current_remote_target ();
11668 if (remote
== nullptr)
11669 error (_("packets can only be sent to a remote target"));
11671 callbacks
->sending (buf
);
11673 remote
->putpkt_binary (buf
.data (), buf
.size ());
11674 remote_state
*rs
= remote
->get_remote_state ();
11675 int bytes
= remote
->getpkt_sane (&rs
->buf
, 0);
11678 error (_("error while fetching packet from remote target"));
11680 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
11681 callbacks
->received (view
);
11684 /* Entry point for the 'maint packet' command. */
11687 cli_packet_command (const char *args
, int from_tty
)
11689 cli_packet_command_callbacks cb
;
11690 gdb::array_view
<const char> view
11691 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
11692 send_remote_packet (view
, &cb
);
11696 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
11698 static void display_thread_info (struct gdb_ext_thread_info
*info
);
11700 static void threadset_test_cmd (char *cmd
, int tty
);
11702 static void threadalive_test (char *cmd
, int tty
);
11704 static void threadlist_test_cmd (char *cmd
, int tty
);
11706 int get_and_display_threadinfo (threadref
*ref
);
11708 static void threadinfo_test_cmd (char *cmd
, int tty
);
11710 static int thread_display_step (threadref
*ref
, void *context
);
11712 static void threadlist_update_test_cmd (char *cmd
, int tty
);
11714 static void init_remote_threadtests (void);
11716 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
11719 threadset_test_cmd (const char *cmd
, int tty
)
11721 int sample_thread
= SAMPLE_THREAD
;
11723 gdb_printf (_("Remote threadset test\n"));
11724 set_general_thread (sample_thread
);
11729 threadalive_test (const char *cmd
, int tty
)
11731 int sample_thread
= SAMPLE_THREAD
;
11732 int pid
= inferior_ptid
.pid ();
11733 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
11735 if (remote_thread_alive (ptid
))
11736 gdb_printf ("PASS: Thread alive test\n");
11738 gdb_printf ("FAIL: Thread alive test\n");
11741 void output_threadid (char *title
, threadref
*ref
);
11744 output_threadid (char *title
, threadref
*ref
)
11748 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
11750 gdb_printf ("%s %s\n", title
, (&hexid
[0]));
11754 threadlist_test_cmd (const char *cmd
, int tty
)
11757 threadref nextthread
;
11758 int done
, result_count
;
11759 threadref threadlist
[3];
11761 gdb_printf ("Remote Threadlist test\n");
11762 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
11763 &result_count
, &threadlist
[0]))
11764 gdb_printf ("FAIL: threadlist test\n");
11767 threadref
*scan
= threadlist
;
11768 threadref
*limit
= scan
+ result_count
;
11770 while (scan
< limit
)
11771 output_threadid (" thread ", scan
++);
11776 display_thread_info (struct gdb_ext_thread_info
*info
)
11778 output_threadid ("Threadid: ", &info
->threadid
);
11779 gdb_printf ("Name: %s\n ", info
->shortname
);
11780 gdb_printf ("State: %s\n", info
->display
);
11781 gdb_printf ("other: %s\n\n", info
->more_display
);
11785 get_and_display_threadinfo (threadref
*ref
)
11789 struct gdb_ext_thread_info threadinfo
;
11791 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
11792 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
11793 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
11794 display_thread_info (&threadinfo
);
11799 threadinfo_test_cmd (const char *cmd
, int tty
)
11801 int athread
= SAMPLE_THREAD
;
11805 int_to_threadref (&thread
, athread
);
11806 gdb_printf ("Remote Threadinfo test\n");
11807 if (!get_and_display_threadinfo (&thread
))
11808 gdb_printf ("FAIL cannot get thread info\n");
11812 thread_display_step (threadref
*ref
, void *context
)
11814 /* output_threadid(" threadstep ",ref); *//* simple test */
11815 return get_and_display_threadinfo (ref
);
11819 threadlist_update_test_cmd (const char *cmd
, int tty
)
11821 gdb_printf ("Remote Threadlist update test\n");
11822 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
11826 init_remote_threadtests (void)
11828 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
11829 _("Fetch and print the remote list of "
11830 "thread identifiers, one pkt only."));
11831 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
11832 _("Fetch and display info about one thread."));
11833 add_com ("tset", class_obscure
, threadset_test_cmd
,
11834 _("Test setting to a different thread."));
11835 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
11836 _("Iterate through updating all remote thread info."));
11837 add_com ("talive", class_obscure
, threadalive_test
,
11838 _("Remote thread alive test."));
11843 /* Convert a thread ID to a string. */
11846 remote_target::pid_to_str (ptid_t ptid
)
11848 struct remote_state
*rs
= get_remote_state ();
11850 if (ptid
== null_ptid
)
11851 return normal_pid_to_str (ptid
);
11852 else if (ptid
.is_pid ())
11854 /* Printing an inferior target id. */
11856 /* When multi-process extensions are off, there's no way in the
11857 remote protocol to know the remote process id, if there's any
11858 at all. There's one exception --- when we're connected with
11859 target extended-remote, and we manually attached to a process
11860 with "attach PID". We don't record anywhere a flag that
11861 allows us to distinguish that case from the case of
11862 connecting with extended-remote and the stub already being
11863 attached to a process, and reporting yes to qAttached, hence
11864 no smart special casing here. */
11865 if (!remote_multi_process_p (rs
))
11866 return "Remote target";
11868 return normal_pid_to_str (ptid
);
11872 if (magic_null_ptid
== ptid
)
11873 return "Thread <main>";
11874 else if (remote_multi_process_p (rs
))
11875 if (ptid
.lwp () == 0)
11876 return normal_pid_to_str (ptid
);
11878 return string_printf ("Thread %d.%ld",
11879 ptid
.pid (), ptid
.lwp ());
11881 return string_printf ("Thread %ld", ptid
.lwp ());
11885 /* Get the address of the thread local variable in OBJFILE which is
11886 stored at OFFSET within the thread local storage for thread PTID. */
11889 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
11892 if (packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
11894 struct remote_state
*rs
= get_remote_state ();
11895 char *p
= rs
->buf
.data ();
11896 char *endp
= p
+ get_remote_packet_size ();
11897 enum packet_result result
;
11899 strcpy (p
, "qGetTLSAddr:");
11901 p
= write_ptid (p
, endp
, ptid
);
11903 p
+= hexnumstr (p
, offset
);
11905 p
+= hexnumstr (p
, lm
);
11909 getpkt (&rs
->buf
, 0);
11910 result
= packet_ok (rs
->buf
,
11911 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
11912 if (result
== PACKET_OK
)
11916 unpack_varlen_hex (rs
->buf
.data (), &addr
);
11919 else if (result
== PACKET_UNKNOWN
)
11920 throw_error (TLS_GENERIC_ERROR
,
11921 _("Remote target doesn't support qGetTLSAddr packet"));
11923 throw_error (TLS_GENERIC_ERROR
,
11924 _("Remote target failed to process qGetTLSAddr request"));
11927 throw_error (TLS_GENERIC_ERROR
,
11928 _("TLS not supported or disabled on this target"));
11933 /* Provide thread local base, i.e. Thread Information Block address.
11934 Returns 1 if ptid is found and thread_local_base is non zero. */
11937 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
11939 if (packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
11941 struct remote_state
*rs
= get_remote_state ();
11942 char *p
= rs
->buf
.data ();
11943 char *endp
= p
+ get_remote_packet_size ();
11944 enum packet_result result
;
11946 strcpy (p
, "qGetTIBAddr:");
11948 p
= write_ptid (p
, endp
, ptid
);
11952 getpkt (&rs
->buf
, 0);
11953 result
= packet_ok (rs
->buf
,
11954 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
11955 if (result
== PACKET_OK
)
11958 unpack_varlen_hex (rs
->buf
.data (), &val
);
11960 *addr
= (CORE_ADDR
) val
;
11963 else if (result
== PACKET_UNKNOWN
)
11964 error (_("Remote target doesn't support qGetTIBAddr packet"));
11966 error (_("Remote target failed to process qGetTIBAddr request"));
11969 error (_("qGetTIBAddr not supported or disabled on this target"));
11974 /* Support for inferring a target description based on the current
11975 architecture and the size of a 'g' packet. While the 'g' packet
11976 can have any size (since optional registers can be left off the
11977 end), some sizes are easily recognizable given knowledge of the
11978 approximate architecture. */
11980 struct remote_g_packet_guess
11982 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
11989 const struct target_desc
*tdesc
;
11992 struct remote_g_packet_data
11994 std::vector
<remote_g_packet_guess
> guesses
;
11997 static const registry
<gdbarch
>::key
<struct remote_g_packet_data
>
11998 remote_g_packet_data_handle
;
12000 static struct remote_g_packet_data
*
12001 get_g_packet_data (struct gdbarch
*gdbarch
)
12003 struct remote_g_packet_data
*data
12004 = remote_g_packet_data_handle
.get (gdbarch
);
12005 if (data
== nullptr)
12006 data
= remote_g_packet_data_handle
.emplace (gdbarch
);
12011 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12012 const struct target_desc
*tdesc
)
12014 struct remote_g_packet_data
*data
= get_g_packet_data (gdbarch
);
12016 gdb_assert (tdesc
!= NULL
);
12018 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12019 if (guess
.bytes
== bytes
)
12020 internal_error (__FILE__
, __LINE__
,
12021 _("Duplicate g packet description added for size %d"),
12024 data
->guesses
.emplace_back (bytes
, tdesc
);
12027 /* Return true if remote_read_description would do anything on this target
12028 and architecture, false otherwise. */
12031 remote_read_description_p (struct target_ops
*target
)
12033 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12035 return !data
->guesses
.empty ();
12038 const struct target_desc
*
12039 remote_target::read_description ()
12041 struct remote_g_packet_data
*data
= get_g_packet_data (target_gdbarch ());
12043 /* Do not try this during initial connection, when we do not know
12044 whether there is a running but stopped thread. */
12045 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12046 return beneath ()->read_description ();
12048 if (!data
->guesses
.empty ())
12050 int bytes
= send_g_packet ();
12052 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12053 if (guess
.bytes
== bytes
)
12054 return guess
.tdesc
;
12056 /* We discard the g packet. A minor optimization would be to
12057 hold on to it, and fill the register cache once we have selected
12058 an architecture, but it's too tricky to do safely. */
12061 return beneath ()->read_description ();
12064 /* Remote file transfer support. This is host-initiated I/O, not
12065 target-initiated; for target-initiated, see remote-fileio.c. */
12067 /* If *LEFT is at least the length of STRING, copy STRING to
12068 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12069 decrease *LEFT. Otherwise raise an error. */
12072 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12074 int len
= strlen (string
);
12077 error (_("Packet too long for target."));
12079 memcpy (*buffer
, string
, len
);
12083 /* NUL-terminate the buffer as a convenience, if there is
12089 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12090 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12091 decrease *LEFT. Otherwise raise an error. */
12094 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12097 if (2 * len
> *left
)
12098 error (_("Packet too long for target."));
12100 bin2hex (bytes
, *buffer
, len
);
12101 *buffer
+= 2 * len
;
12104 /* NUL-terminate the buffer as a convenience, if there is
12110 /* If *LEFT is large enough, convert VALUE to hex and add it to
12111 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12112 decrease *LEFT. Otherwise raise an error. */
12115 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12117 int len
= hexnumlen (value
);
12120 error (_("Packet too long for target."));
12122 hexnumstr (*buffer
, value
);
12126 /* NUL-terminate the buffer as a convenience, if there is
12132 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12133 value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
12134 was included, and *ATTACHMENT to point to the start of the annex
12135 if any. The length of the packet isn't needed here; there may
12136 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12138 Return 0 if the packet could be parsed, -1 if it could not. If
12139 -1 is returned, the other variables may not be initialized. */
12142 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12143 fileio_error
*remote_errno
, const char **attachment
)
12147 *remote_errno
= FILEIO_SUCCESS
;
12148 *attachment
= NULL
;
12150 if (buffer
[0] != 'F')
12154 *retcode
= strtol (&buffer
[1], &p
, 16);
12155 if (errno
!= 0 || p
== &buffer
[1])
12158 /* Check for ",errno". */
12162 *remote_errno
= (fileio_error
) strtol (p
+ 1, &p2
, 16);
12163 if (errno
!= 0 || p
+ 1 == p2
)
12168 /* Check for ";attachment". If there is no attachment, the
12169 packet should end here. */
12172 *attachment
= p
+ 1;
12175 else if (*p
== '\0')
12181 /* Send a prepared I/O packet to the target and read its response.
12182 The prepared packet is in the global RS->BUF before this function
12183 is called, and the answer is there when we return.
12185 COMMAND_BYTES is the length of the request to send, which may include
12186 binary data. WHICH_PACKET is the packet configuration to check
12187 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12188 is set to the error number and -1 is returned. Otherwise the value
12189 returned by the function is returned.
12191 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12192 attachment is expected; an error will be reported if there's a
12193 mismatch. If one is found, *ATTACHMENT will be set to point into
12194 the packet buffer and *ATTACHMENT_LEN will be set to the
12195 attachment's length. */
12198 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12199 fileio_error
*remote_errno
, const char **attachment
,
12200 int *attachment_len
)
12202 struct remote_state
*rs
= get_remote_state ();
12203 int ret
, bytes_read
;
12204 const char *attachment_tmp
;
12206 if (packet_support (which_packet
) == PACKET_DISABLE
)
12208 *remote_errno
= FILEIO_ENOSYS
;
12212 putpkt_binary (rs
->buf
.data (), command_bytes
);
12213 bytes_read
= getpkt_sane (&rs
->buf
, 0);
12215 /* If it timed out, something is wrong. Don't try to parse the
12217 if (bytes_read
< 0)
12219 *remote_errno
= FILEIO_EINVAL
;
12223 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
12226 *remote_errno
= FILEIO_EINVAL
;
12228 case PACKET_UNKNOWN
:
12229 *remote_errno
= FILEIO_ENOSYS
;
12235 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12238 *remote_errno
= FILEIO_EINVAL
;
12242 /* Make sure we saw an attachment if and only if we expected one. */
12243 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12244 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12246 *remote_errno
= FILEIO_EINVAL
;
12250 /* If an attachment was found, it must point into the packet buffer;
12251 work out how many bytes there were. */
12252 if (attachment_tmp
!= NULL
)
12254 *attachment
= attachment_tmp
;
12255 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12261 /* See declaration.h. */
12264 readahead_cache::invalidate ()
12269 /* See declaration.h. */
12272 readahead_cache::invalidate_fd (int fd
)
12274 if (this->fd
== fd
)
12278 /* Set the filesystem remote_hostio functions that take FILENAME
12279 arguments will use. Return 0 on success, or -1 if an error
12280 occurs (and set *REMOTE_ERRNO). */
12283 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12284 fileio_error
*remote_errno
)
12286 struct remote_state
*rs
= get_remote_state ();
12287 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12288 char *p
= rs
->buf
.data ();
12289 int left
= get_remote_packet_size () - 1;
12293 if (packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12296 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12299 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12301 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12302 remote_buffer_add_string (&p
, &left
, arg
);
12304 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12305 remote_errno
, NULL
, NULL
);
12307 if (packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12311 rs
->fs_pid
= required_pid
;
12316 /* Implementation of to_fileio_open. */
12319 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12320 int flags
, int mode
, int warn_if_slow
,
12321 fileio_error
*remote_errno
)
12323 struct remote_state
*rs
= get_remote_state ();
12324 char *p
= rs
->buf
.data ();
12325 int left
= get_remote_packet_size () - 1;
12329 static int warning_issued
= 0;
12331 gdb_printf (_("Reading %s from remote target...\n"),
12334 if (!warning_issued
)
12336 warning (_("File transfers from remote targets can be slow."
12337 " Use \"set sysroot\" to access files locally"
12339 warning_issued
= 1;
12343 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12346 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12348 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12349 strlen (filename
));
12350 remote_buffer_add_string (&p
, &left
, ",");
12352 remote_buffer_add_int (&p
, &left
, flags
);
12353 remote_buffer_add_string (&p
, &left
, ",");
12355 remote_buffer_add_int (&p
, &left
, mode
);
12357 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12358 remote_errno
, NULL
, NULL
);
12362 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12363 int flags
, int mode
, int warn_if_slow
,
12364 fileio_error
*remote_errno
)
12366 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12370 /* Implementation of to_fileio_pwrite. */
12373 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12374 ULONGEST offset
, fileio_error
*remote_errno
)
12376 struct remote_state
*rs
= get_remote_state ();
12377 char *p
= rs
->buf
.data ();
12378 int left
= get_remote_packet_size ();
12381 rs
->readahead_cache
.invalidate_fd (fd
);
12383 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12385 remote_buffer_add_int (&p
, &left
, fd
);
12386 remote_buffer_add_string (&p
, &left
, ",");
12388 remote_buffer_add_int (&p
, &left
, offset
);
12389 remote_buffer_add_string (&p
, &left
, ",");
12391 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12392 (get_remote_packet_size ()
12393 - (p
- rs
->buf
.data ())));
12395 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12396 remote_errno
, NULL
, NULL
);
12400 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12401 ULONGEST offset
, fileio_error
*remote_errno
)
12403 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12406 /* Helper for the implementation of to_fileio_pread. Read the file
12407 from the remote side with vFile:pread. */
12410 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12411 ULONGEST offset
, fileio_error
*remote_errno
)
12413 struct remote_state
*rs
= get_remote_state ();
12414 char *p
= rs
->buf
.data ();
12415 const char *attachment
;
12416 int left
= get_remote_packet_size ();
12417 int ret
, attachment_len
;
12420 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12422 remote_buffer_add_int (&p
, &left
, fd
);
12423 remote_buffer_add_string (&p
, &left
, ",");
12425 remote_buffer_add_int (&p
, &left
, len
);
12426 remote_buffer_add_string (&p
, &left
, ",");
12428 remote_buffer_add_int (&p
, &left
, offset
);
12430 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12431 remote_errno
, &attachment
,
12437 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12439 if (read_len
!= ret
)
12440 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12445 /* See declaration.h. */
12448 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12452 && this->offset
<= offset
12453 && offset
< this->offset
+ this->bufsize
)
12455 ULONGEST max
= this->offset
+ this->bufsize
;
12457 if (offset
+ len
> max
)
12458 len
= max
- offset
;
12460 memcpy (read_buf
, this->buf
+ offset
- this->offset
, len
);
12467 /* Implementation of to_fileio_pread. */
12470 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12471 ULONGEST offset
, fileio_error
*remote_errno
)
12474 struct remote_state
*rs
= get_remote_state ();
12475 readahead_cache
*cache
= &rs
->readahead_cache
;
12477 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
12480 cache
->hit_count
++;
12482 remote_debug_printf ("readahead cache hit %s",
12483 pulongest (cache
->hit_count
));
12487 cache
->miss_count
++;
12489 remote_debug_printf ("readahead cache miss %s",
12490 pulongest (cache
->miss_count
));
12493 cache
->offset
= offset
;
12494 cache
->bufsize
= get_remote_packet_size ();
12495 cache
->buf
= (gdb_byte
*) xrealloc (cache
->buf
, cache
->bufsize
);
12497 ret
= remote_hostio_pread_vFile (cache
->fd
, cache
->buf
, cache
->bufsize
,
12498 cache
->offset
, remote_errno
);
12501 cache
->invalidate_fd (fd
);
12505 cache
->bufsize
= ret
;
12506 return cache
->pread (fd
, read_buf
, len
, offset
);
12510 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12511 ULONGEST offset
, fileio_error
*remote_errno
)
12513 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
12516 /* Implementation of to_fileio_close. */
12519 remote_target::remote_hostio_close (int fd
, fileio_error
*remote_errno
)
12521 struct remote_state
*rs
= get_remote_state ();
12522 char *p
= rs
->buf
.data ();
12523 int left
= get_remote_packet_size () - 1;
12525 rs
->readahead_cache
.invalidate_fd (fd
);
12527 remote_buffer_add_string (&p
, &left
, "vFile:close:");
12529 remote_buffer_add_int (&p
, &left
, fd
);
12531 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
12532 remote_errno
, NULL
, NULL
);
12536 remote_target::fileio_close (int fd
, fileio_error
*remote_errno
)
12538 return remote_hostio_close (fd
, remote_errno
);
12541 /* Implementation of to_fileio_unlink. */
12544 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
12545 fileio_error
*remote_errno
)
12547 struct remote_state
*rs
= get_remote_state ();
12548 char *p
= rs
->buf
.data ();
12549 int left
= get_remote_packet_size () - 1;
12551 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12554 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
12556 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12557 strlen (filename
));
12559 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
12560 remote_errno
, NULL
, NULL
);
12564 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
12565 fileio_error
*remote_errno
)
12567 return remote_hostio_unlink (inf
, filename
, remote_errno
);
12570 /* Implementation of to_fileio_readlink. */
12572 gdb::optional
<std::string
>
12573 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
12574 fileio_error
*remote_errno
)
12576 struct remote_state
*rs
= get_remote_state ();
12577 char *p
= rs
->buf
.data ();
12578 const char *attachment
;
12579 int left
= get_remote_packet_size ();
12580 int len
, attachment_len
;
12583 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12586 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
12588 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12589 strlen (filename
));
12591 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
12592 remote_errno
, &attachment
,
12598 std::string
ret (len
, '\0');
12600 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12601 (gdb_byte
*) &ret
[0], len
);
12602 if (read_len
!= len
)
12603 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
12608 /* Implementation of to_fileio_fstat. */
12611 remote_target::fileio_fstat (int fd
, struct stat
*st
, fileio_error
*remote_errno
)
12613 struct remote_state
*rs
= get_remote_state ();
12614 char *p
= rs
->buf
.data ();
12615 int left
= get_remote_packet_size ();
12616 int attachment_len
, ret
;
12617 const char *attachment
;
12618 struct fio_stat fst
;
12621 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
12623 remote_buffer_add_int (&p
, &left
, fd
);
12625 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
12626 remote_errno
, &attachment
,
12630 if (*remote_errno
!= FILEIO_ENOSYS
)
12633 /* Strictly we should return -1, ENOSYS here, but when
12634 "set sysroot remote:" was implemented in August 2008
12635 BFD's need for a stat function was sidestepped with
12636 this hack. This was not remedied until March 2015
12637 so we retain the previous behavior to avoid breaking
12640 Note that the memset is a March 2015 addition; older
12641 GDBs set st_size *and nothing else* so the structure
12642 would have garbage in all other fields. This might
12643 break something but retaining the previous behavior
12644 here would be just too wrong. */
12646 memset (st
, 0, sizeof (struct stat
));
12647 st
->st_size
= INT_MAX
;
12651 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12652 (gdb_byte
*) &fst
, sizeof (fst
));
12654 if (read_len
!= ret
)
12655 error (_("vFile:fstat returned %d, but %d bytes."), ret
, read_len
);
12657 if (read_len
!= sizeof (fst
))
12658 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12659 read_len
, (int) sizeof (fst
));
12661 remote_fileio_to_host_stat (&fst
, st
);
12666 /* Implementation of to_filesystem_is_local. */
12669 remote_target::filesystem_is_local ()
12671 /* Valgrind GDB presents itself as a remote target but works
12672 on the local filesystem: it does not implement remote get
12673 and users are not expected to set a sysroot. To handle
12674 this case we treat the remote filesystem as local if the
12675 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12676 does not support vFile:open. */
12677 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
12679 enum packet_support ps
= packet_support (PACKET_vFile_open
);
12681 if (ps
== PACKET_SUPPORT_UNKNOWN
)
12684 fileio_error remote_errno
;
12686 /* Try opening a file to probe support. The supplied
12687 filename is irrelevant, we only care about whether
12688 the stub recognizes the packet or not. */
12689 fd
= remote_hostio_open (NULL
, "just probing",
12690 FILEIO_O_RDONLY
, 0700, 0,
12694 remote_hostio_close (fd
, &remote_errno
);
12696 ps
= packet_support (PACKET_vFile_open
);
12699 if (ps
== PACKET_DISABLE
)
12701 static int warning_issued
= 0;
12703 if (!warning_issued
)
12705 warning (_("remote target does not support file"
12706 " transfer, attempting to access files"
12707 " from local filesystem."));
12708 warning_issued
= 1;
12719 remote_hostio_error (fileio_error errnum
)
12721 int host_error
= fileio_error_to_host (errnum
);
12723 if (host_error
== -1)
12724 error (_("Unknown remote I/O error %d"), errnum
);
12726 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
12729 /* A RAII wrapper around a remote file descriptor. */
12731 class scoped_remote_fd
12734 scoped_remote_fd (remote_target
*remote
, int fd
)
12735 : m_remote (remote
), m_fd (fd
)
12739 ~scoped_remote_fd ()
12745 fileio_error remote_errno
;
12746 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
12750 /* Swallow exception before it escapes the dtor. If
12751 something goes wrong, likely the connection is gone,
12752 and there's nothing else that can be done. */
12757 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
12759 /* Release ownership of the file descriptor, and return it. */
12760 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
12767 /* Return the owned file descriptor. */
12768 int get () const noexcept
12774 /* The remote target. */
12775 remote_target
*m_remote
;
12777 /* The owned remote I/O file descriptor. */
12782 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
12784 remote_target
*remote
= get_current_remote_target ();
12786 if (remote
== nullptr)
12787 error (_("command can only be used with remote target"));
12789 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
12793 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
12796 int retcode
, bytes
, io_size
;
12797 fileio_error remote_errno
;
12798 int bytes_in_buffer
;
12802 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
12804 perror_with_name (local_file
);
12806 scoped_remote_fd fd
12807 (this, remote_hostio_open (NULL
,
12808 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
12810 0700, 0, &remote_errno
));
12811 if (fd
.get () == -1)
12812 remote_hostio_error (remote_errno
);
12814 /* Send up to this many bytes at once. They won't all fit in the
12815 remote packet limit, so we'll transfer slightly fewer. */
12816 io_size
= get_remote_packet_size ();
12817 gdb::byte_vector
buffer (io_size
);
12819 bytes_in_buffer
= 0;
12822 while (bytes_in_buffer
|| !saw_eof
)
12826 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
12827 io_size
- bytes_in_buffer
,
12831 if (ferror (file
.get ()))
12832 error (_("Error reading %s."), local_file
);
12835 /* EOF. Unless there is something still in the
12836 buffer from the last iteration, we are done. */
12838 if (bytes_in_buffer
== 0)
12846 bytes
+= bytes_in_buffer
;
12847 bytes_in_buffer
= 0;
12849 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
12850 offset
, &remote_errno
);
12853 remote_hostio_error (remote_errno
);
12854 else if (retcode
== 0)
12855 error (_("Remote write of %d bytes returned 0!"), bytes
);
12856 else if (retcode
< bytes
)
12858 /* Short write. Save the rest of the read data for the next
12860 bytes_in_buffer
= bytes
- retcode
;
12861 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
12867 if (remote_hostio_close (fd
.release (), &remote_errno
))
12868 remote_hostio_error (remote_errno
);
12871 gdb_printf (_("Successfully sent file \"%s\".\n"), local_file
);
12875 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
12877 remote_target
*remote
= get_current_remote_target ();
12879 if (remote
== nullptr)
12880 error (_("command can only be used with remote target"));
12882 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
12886 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
12889 fileio_error remote_errno
;
12890 int bytes
, io_size
;
12893 scoped_remote_fd fd
12894 (this, remote_hostio_open (NULL
,
12895 remote_file
, FILEIO_O_RDONLY
, 0, 0,
12897 if (fd
.get () == -1)
12898 remote_hostio_error (remote_errno
);
12900 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
12902 perror_with_name (local_file
);
12904 /* Send up to this many bytes at once. They won't all fit in the
12905 remote packet limit, so we'll transfer slightly fewer. */
12906 io_size
= get_remote_packet_size ();
12907 gdb::byte_vector
buffer (io_size
);
12912 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
12915 /* Success, but no bytes, means end-of-file. */
12918 remote_hostio_error (remote_errno
);
12922 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
12924 perror_with_name (local_file
);
12927 if (remote_hostio_close (fd
.release (), &remote_errno
))
12928 remote_hostio_error (remote_errno
);
12931 gdb_printf (_("Successfully fetched file \"%s\".\n"), remote_file
);
12935 remote_file_delete (const char *remote_file
, int from_tty
)
12937 remote_target
*remote
= get_current_remote_target ();
12939 if (remote
== nullptr)
12940 error (_("command can only be used with remote target"));
12942 remote
->remote_file_delete (remote_file
, from_tty
);
12946 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
12949 fileio_error remote_errno
;
12951 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
12953 remote_hostio_error (remote_errno
);
12956 gdb_printf (_("Successfully deleted file \"%s\".\n"), remote_file
);
12960 remote_put_command (const char *args
, int from_tty
)
12963 error_no_arg (_("file to put"));
12965 gdb_argv
argv (args
);
12966 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
12967 error (_("Invalid parameters to remote put"));
12969 remote_file_put (argv
[0], argv
[1], from_tty
);
12973 remote_get_command (const char *args
, int from_tty
)
12976 error_no_arg (_("file to get"));
12978 gdb_argv
argv (args
);
12979 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
12980 error (_("Invalid parameters to remote get"));
12982 remote_file_get (argv
[0], argv
[1], from_tty
);
12986 remote_delete_command (const char *args
, int from_tty
)
12989 error_no_arg (_("file to delete"));
12991 gdb_argv
argv (args
);
12992 if (argv
[0] == NULL
|| argv
[1] != NULL
)
12993 error (_("Invalid parameters to remote delete"));
12995 remote_file_delete (argv
[0], from_tty
);
12999 remote_target::can_execute_reverse ()
13001 if (packet_support (PACKET_bs
) == PACKET_ENABLE
13002 || packet_support (PACKET_bc
) == PACKET_ENABLE
)
13009 remote_target::supports_non_stop ()
13015 remote_target::supports_disable_randomization ()
13017 /* Only supported in extended mode. */
13022 remote_target::supports_multi_process ()
13024 struct remote_state
*rs
= get_remote_state ();
13026 return remote_multi_process_p (rs
);
13030 remote_supports_cond_tracepoints ()
13032 return packet_support (PACKET_ConditionalTracepoints
) == PACKET_ENABLE
;
13036 remote_target::supports_evaluation_of_breakpoint_conditions ()
13038 return packet_support (PACKET_ConditionalBreakpoints
) == PACKET_ENABLE
;
13042 remote_supports_fast_tracepoints ()
13044 return packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13048 remote_supports_static_tracepoints ()
13050 return packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13054 remote_supports_install_in_trace ()
13056 return packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13060 remote_target::supports_enable_disable_tracepoint ()
13062 return (packet_support (PACKET_EnableDisableTracepoints_feature
)
13067 remote_target::supports_string_tracing ()
13069 return packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13073 remote_target::can_run_breakpoint_commands ()
13075 return packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13079 remote_target::trace_init ()
13081 struct remote_state
*rs
= get_remote_state ();
13084 remote_get_noisy_reply ();
13085 if (strcmp (rs
->buf
.data (), "OK") != 0)
13086 error (_("Target does not support this command."));
13089 /* Recursive routine to walk through command list including loops, and
13090 download packets for each command. */
13093 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13094 struct command_line
*cmds
)
13096 struct remote_state
*rs
= get_remote_state ();
13097 struct command_line
*cmd
;
13099 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13101 QUIT
; /* Allow user to bail out with ^C. */
13102 strcpy (rs
->buf
.data (), "QTDPsrc:");
13103 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13104 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13105 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13107 remote_get_noisy_reply ();
13108 if (strcmp (rs
->buf
.data (), "OK"))
13109 warning (_("Target does not support source download."));
13111 if (cmd
->control_type
== while_control
13112 || cmd
->control_type
== while_stepping_control
)
13114 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13116 QUIT
; /* Allow user to bail out with ^C. */
13117 strcpy (rs
->buf
.data (), "QTDPsrc:");
13118 encode_source_string (num
, addr
, "cmd", "end",
13119 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13120 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13122 remote_get_noisy_reply ();
13123 if (strcmp (rs
->buf
.data (), "OK"))
13124 warning (_("Target does not support source download."));
13130 remote_target::download_tracepoint (struct bp_location
*loc
)
13134 std::vector
<std::string
> tdp_actions
;
13135 std::vector
<std::string
> stepping_actions
;
13137 struct breakpoint
*b
= loc
->owner
;
13138 struct tracepoint
*t
= (struct tracepoint
*) b
;
13139 struct remote_state
*rs
= get_remote_state ();
13141 const char *err_msg
= _("Tracepoint packet too large for target.");
13144 /* We use a buffer other than rs->buf because we'll build strings
13145 across multiple statements, and other statements in between could
13147 gdb::char_vector
buf (get_remote_packet_size ());
13149 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13151 tpaddr
= loc
->address
;
13152 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13153 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13154 b
->number
, addrbuf
, /* address */
13155 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13156 t
->step_count
, t
->pass_count
);
13158 if (ret
< 0 || ret
>= buf
.size ())
13159 error ("%s", err_msg
);
13161 /* Fast tracepoints are mostly handled by the target, but we can
13162 tell the target how big of an instruction block should be moved
13164 if (b
->type
== bp_fast_tracepoint
)
13166 /* Only test for support at download time; we may not know
13167 target capabilities at definition time. */
13168 if (remote_supports_fast_tracepoints ())
13170 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13173 size_left
= buf
.size () - strlen (buf
.data ());
13174 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13176 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13178 if (ret
< 0 || ret
>= size_left
)
13179 error ("%s", err_msg
);
13182 /* If it passed validation at definition but fails now,
13183 something is very wrong. */
13184 internal_error (__FILE__
, __LINE__
,
13185 _("Fast tracepoint not "
13186 "valid during download"));
13189 /* Fast tracepoints are functionally identical to regular
13190 tracepoints, so don't take lack of support as a reason to
13191 give up on the trace run. */
13192 warning (_("Target does not support fast tracepoints, "
13193 "downloading %d as regular tracepoint"), b
->number
);
13195 else if (b
->type
== bp_static_tracepoint
13196 || b
->type
== bp_static_marker_tracepoint
)
13198 /* Only test for support at download time; we may not know
13199 target capabilities at definition time. */
13200 if (remote_supports_static_tracepoints ())
13202 struct static_tracepoint_marker marker
;
13204 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13206 size_left
= buf
.size () - strlen (buf
.data ());
13207 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13210 if (ret
< 0 || ret
>= size_left
)
13211 error ("%s", err_msg
);
13214 error (_("Static tracepoint not valid during download"));
13217 /* Fast tracepoints are functionally identical to regular
13218 tracepoints, so don't take lack of support as a reason
13219 to give up on the trace run. */
13220 error (_("Target does not support static tracepoints"));
13222 /* If the tracepoint has a conditional, make it into an agent
13223 expression and append to the definition. */
13226 /* Only test support at download time, we may not know target
13227 capabilities at definition time. */
13228 if (remote_supports_cond_tracepoints ())
13230 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13233 size_left
= buf
.size () - strlen (buf
.data ());
13235 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13236 size_left
, ":X%x,", aexpr
->len
);
13238 if (ret
< 0 || ret
>= size_left
)
13239 error ("%s", err_msg
);
13241 size_left
= buf
.size () - strlen (buf
.data ());
13243 /* Two bytes to encode each aexpr byte, plus the terminating
13245 if (aexpr
->len
* 2 + 1 > size_left
)
13246 error ("%s", err_msg
);
13248 pkt
= buf
.data () + strlen (buf
.data ());
13250 for (int ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
13251 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13255 warning (_("Target does not support conditional tracepoints, "
13256 "ignoring tp %d cond"), b
->number
);
13259 if (b
->commands
|| !default_collect
.empty ())
13261 size_left
= buf
.size () - strlen (buf
.data ());
13263 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13266 if (ret
< 0 || ret
>= size_left
)
13267 error ("%s", err_msg
);
13270 putpkt (buf
.data ());
13271 remote_get_noisy_reply ();
13272 if (strcmp (rs
->buf
.data (), "OK"))
13273 error (_("Target does not support tracepoints."));
13275 /* do_single_steps (t); */
13276 for (auto action_it
= tdp_actions
.begin ();
13277 action_it
!= tdp_actions
.end (); action_it
++)
13279 QUIT
; /* Allow user to bail out with ^C. */
13281 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13282 || !stepping_actions
.empty ());
13284 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13285 b
->number
, addrbuf
, /* address */
13286 action_it
->c_str (),
13287 has_more
? '-' : 0);
13289 if (ret
< 0 || ret
>= buf
.size ())
13290 error ("%s", err_msg
);
13292 putpkt (buf
.data ());
13293 remote_get_noisy_reply ();
13294 if (strcmp (rs
->buf
.data (), "OK"))
13295 error (_("Error on target while setting tracepoints."));
13298 for (auto action_it
= stepping_actions
.begin ();
13299 action_it
!= stepping_actions
.end (); action_it
++)
13301 QUIT
; /* Allow user to bail out with ^C. */
13303 bool is_first
= action_it
== stepping_actions
.begin ();
13304 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13306 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13307 b
->number
, addrbuf
, /* address */
13308 is_first
? "S" : "",
13309 action_it
->c_str (),
13310 has_more
? "-" : "");
13312 if (ret
< 0 || ret
>= buf
.size ())
13313 error ("%s", err_msg
);
13315 putpkt (buf
.data ());
13316 remote_get_noisy_reply ();
13317 if (strcmp (rs
->buf
.data (), "OK"))
13318 error (_("Error on target while setting tracepoints."));
13321 if (packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13323 if (b
->locspec
!= nullptr)
13325 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13327 if (ret
< 0 || ret
>= buf
.size ())
13328 error ("%s", err_msg
);
13330 const char *str
= b
->locspec
->to_string ();
13331 encode_source_string (b
->number
, loc
->address
, "at", str
,
13332 buf
.data () + strlen (buf
.data ()),
13333 buf
.size () - strlen (buf
.data ()));
13334 putpkt (buf
.data ());
13335 remote_get_noisy_reply ();
13336 if (strcmp (rs
->buf
.data (), "OK"))
13337 warning (_("Target does not support source download."));
13339 if (b
->cond_string
)
13341 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13343 if (ret
< 0 || ret
>= buf
.size ())
13344 error ("%s", err_msg
);
13346 encode_source_string (b
->number
, loc
->address
,
13347 "cond", b
->cond_string
.get (),
13348 buf
.data () + strlen (buf
.data ()),
13349 buf
.size () - strlen (buf
.data ()));
13350 putpkt (buf
.data ());
13351 remote_get_noisy_reply ();
13352 if (strcmp (rs
->buf
.data (), "OK"))
13353 warning (_("Target does not support source download."));
13355 remote_download_command_source (b
->number
, loc
->address
,
13356 breakpoint_commands (b
));
13361 remote_target::can_download_tracepoint ()
13363 struct remote_state
*rs
= get_remote_state ();
13364 struct trace_status
*ts
;
13367 /* Don't try to install tracepoints until we've relocated our
13368 symbols, and fetched and merged the target's tracepoint list with
13370 if (rs
->starting_up
)
13373 ts
= current_trace_status ();
13374 status
= get_trace_status (ts
);
13376 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13379 /* If we are in a tracing experiment, but remote stub doesn't support
13380 installing tracepoint in trace, we have to return. */
13381 if (!remote_supports_install_in_trace ())
13389 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13391 struct remote_state
*rs
= get_remote_state ();
13394 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13395 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13397 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13398 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13399 >= get_remote_packet_size ())
13400 error (_("Trace state variable name too long for tsv definition packet"));
13401 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13404 remote_get_noisy_reply ();
13405 if (rs
->buf
[0] == '\0')
13406 error (_("Target does not support this command."));
13407 if (strcmp (rs
->buf
.data (), "OK") != 0)
13408 error (_("Error on target while downloading trace state variable."));
13412 remote_target::enable_tracepoint (struct bp_location
*location
)
13414 struct remote_state
*rs
= get_remote_state ();
13416 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
13417 location
->owner
->number
,
13418 phex (location
->address
, sizeof (CORE_ADDR
)));
13420 remote_get_noisy_reply ();
13421 if (rs
->buf
[0] == '\0')
13422 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
13423 if (strcmp (rs
->buf
.data (), "OK") != 0)
13424 error (_("Error on target while enabling tracepoint."));
13428 remote_target::disable_tracepoint (struct bp_location
*location
)
13430 struct remote_state
*rs
= get_remote_state ();
13432 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
13433 location
->owner
->number
,
13434 phex (location
->address
, sizeof (CORE_ADDR
)));
13436 remote_get_noisy_reply ();
13437 if (rs
->buf
[0] == '\0')
13438 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
13439 if (strcmp (rs
->buf
.data (), "OK") != 0)
13440 error (_("Error on target while disabling tracepoint."));
13444 remote_target::trace_set_readonly_regions ()
13447 bfd_size_type size
;
13451 bfd
*abfd
= current_program_space
->exec_bfd ();
13454 return; /* No information to give. */
13456 struct remote_state
*rs
= get_remote_state ();
13458 strcpy (rs
->buf
.data (), "QTro");
13459 offset
= strlen (rs
->buf
.data ());
13460 for (s
= abfd
->sections
; s
; s
= s
->next
)
13462 char tmp1
[40], tmp2
[40];
13465 if ((s
->flags
& SEC_LOAD
) == 0
13466 /* || (s->flags & SEC_CODE) == 0 */
13467 || (s
->flags
& SEC_READONLY
) == 0)
13471 vma
= bfd_section_vma (s
);
13472 size
= bfd_section_size (s
);
13473 bfd_sprintf_vma (abfd
, tmp1
, vma
);
13474 bfd_sprintf_vma (abfd
, tmp2
, vma
+ size
);
13475 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
13476 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
13478 if (packet_support (PACKET_qXfer_traceframe_info
) != PACKET_ENABLE
)
13480 Too many sections for read-only sections definition packet."));
13483 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
13485 offset
+= sec_length
;
13490 getpkt (&rs
->buf
, 0);
13495 remote_target::trace_start ()
13497 struct remote_state
*rs
= get_remote_state ();
13499 putpkt ("QTStart");
13500 remote_get_noisy_reply ();
13501 if (rs
->buf
[0] == '\0')
13502 error (_("Target does not support this command."));
13503 if (strcmp (rs
->buf
.data (), "OK") != 0)
13504 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13508 remote_target::get_trace_status (struct trace_status
*ts
)
13510 /* Initialize it just to avoid a GCC false warning. */
13512 enum packet_result result
;
13513 struct remote_state
*rs
= get_remote_state ();
13515 if (packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
13518 /* FIXME we need to get register block size some other way. */
13519 trace_regblock_size
13520 = rs
->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet
;
13522 putpkt ("qTStatus");
13526 p
= remote_get_noisy_reply ();
13528 catch (const gdb_exception_error
&ex
)
13530 if (ex
.error
!= TARGET_CLOSE_ERROR
)
13532 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
13538 result
= packet_ok (p
, &remote_protocol_packets
[PACKET_qTStatus
]);
13540 /* If the remote target doesn't do tracing, flag it. */
13541 if (result
== PACKET_UNKNOWN
)
13544 /* We're working with a live target. */
13545 ts
->filename
= NULL
;
13548 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
13550 /* Function 'parse_trace_status' sets default value of each field of
13551 'ts' at first, so we don't have to do it here. */
13552 parse_trace_status (p
, ts
);
13554 return ts
->running
;
13558 remote_target::get_tracepoint_status (struct breakpoint
*bp
,
13559 struct uploaded_tp
*utp
)
13561 struct remote_state
*rs
= get_remote_state ();
13563 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
13564 size_t size
= get_remote_packet_size ();
13569 tp
->traceframe_usage
= 0;
13570 for (bp_location
*loc
: tp
->locations ())
13572 /* If the tracepoint was never downloaded, don't go asking for
13574 if (tp
->number_on_target
== 0)
13576 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
13577 phex_nz (loc
->address
, 0));
13579 reply
= remote_get_noisy_reply ();
13580 if (reply
&& *reply
)
13583 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13589 utp
->hit_count
= 0;
13590 utp
->traceframe_usage
= 0;
13591 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
13592 phex_nz (utp
->addr
, 0));
13594 reply
= remote_get_noisy_reply ();
13595 if (reply
&& *reply
)
13598 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13604 remote_target::trace_stop ()
13606 struct remote_state
*rs
= get_remote_state ();
13609 remote_get_noisy_reply ();
13610 if (rs
->buf
[0] == '\0')
13611 error (_("Target does not support this command."));
13612 if (strcmp (rs
->buf
.data (), "OK") != 0)
13613 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13617 remote_target::trace_find (enum trace_find_type type
, int num
,
13618 CORE_ADDR addr1
, CORE_ADDR addr2
,
13621 struct remote_state
*rs
= get_remote_state ();
13622 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
13624 int target_frameno
= -1, target_tracept
= -1;
13626 /* Lookups other than by absolute frame number depend on the current
13627 trace selected, so make sure it is correct on the remote end
13629 if (type
!= tfind_number
)
13630 set_remote_traceframe ();
13632 p
= rs
->buf
.data ();
13633 strcpy (p
, "QTFrame:");
13634 p
= strchr (p
, '\0');
13638 xsnprintf (p
, endbuf
- p
, "%x", num
);
13641 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
13644 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
13647 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
13648 phex_nz (addr2
, 0));
13650 case tfind_outside
:
13651 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
13652 phex_nz (addr2
, 0));
13655 error (_("Unknown trace find type %d"), type
);
13659 reply
= remote_get_noisy_reply ();
13660 if (*reply
== '\0')
13661 error (_("Target does not support this command."));
13663 while (reply
&& *reply
)
13668 target_frameno
= (int) strtol (p
, &reply
, 16);
13670 error (_("Unable to parse trace frame number"));
13671 /* Don't update our remote traceframe number cache on failure
13672 to select a remote traceframe. */
13673 if (target_frameno
== -1)
13678 target_tracept
= (int) strtol (p
, &reply
, 16);
13680 error (_("Unable to parse tracepoint number"));
13682 case 'O': /* "OK"? */
13683 if (reply
[1] == 'K' && reply
[2] == '\0')
13686 error (_("Bogus reply from target: %s"), reply
);
13689 error (_("Bogus reply from target: %s"), reply
);
13692 *tpp
= target_tracept
;
13694 rs
->remote_traceframe_number
= target_frameno
;
13695 return target_frameno
;
13699 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
13701 struct remote_state
*rs
= get_remote_state ();
13705 set_remote_traceframe ();
13707 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
13709 reply
= remote_get_noisy_reply ();
13710 if (reply
&& *reply
)
13714 unpack_varlen_hex (reply
+ 1, &uval
);
13715 *val
= (LONGEST
) uval
;
13723 remote_target::save_trace_data (const char *filename
)
13725 struct remote_state
*rs
= get_remote_state ();
13728 p
= rs
->buf
.data ();
13729 strcpy (p
, "QTSave:");
13731 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
13732 >= get_remote_packet_size ())
13733 error (_("Remote file name too long for trace save packet"));
13734 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
13737 reply
= remote_get_noisy_reply ();
13738 if (*reply
== '\0')
13739 error (_("Target does not support this command."));
13740 if (strcmp (reply
, "OK") != 0)
13741 error (_("Bogus reply from target: %s"), reply
);
13745 /* This is basically a memory transfer, but needs to be its own packet
13746 because we don't know how the target actually organizes its trace
13747 memory, plus we want to be able to ask for as much as possible, but
13748 not be unhappy if we don't get as much as we ask for. */
13751 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
13753 struct remote_state
*rs
= get_remote_state ();
13758 p
= rs
->buf
.data ();
13759 strcpy (p
, "qTBuffer:");
13761 p
+= hexnumstr (p
, offset
);
13763 p
+= hexnumstr (p
, len
);
13767 reply
= remote_get_noisy_reply ();
13768 if (reply
&& *reply
)
13770 /* 'l' by itself means we're at the end of the buffer and
13771 there is nothing more to get. */
13775 /* Convert the reply into binary. Limit the number of bytes to
13776 convert according to our passed-in buffer size, rather than
13777 what was returned in the packet; if the target is
13778 unexpectedly generous and gives us a bigger reply than we
13779 asked for, we don't want to crash. */
13780 rslt
= hex2bin (reply
, buf
, len
);
13784 /* Something went wrong, flag as an error. */
13789 remote_target::set_disconnected_tracing (int val
)
13791 struct remote_state
*rs
= get_remote_state ();
13793 if (packet_support (PACKET_DisconnectedTracing_feature
) == PACKET_ENABLE
)
13797 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13798 "QTDisconnected:%x", val
);
13800 reply
= remote_get_noisy_reply ();
13801 if (*reply
== '\0')
13802 error (_("Target does not support this command."));
13803 if (strcmp (reply
, "OK") != 0)
13804 error (_("Bogus reply from target: %s"), reply
);
13807 warning (_("Target does not support disconnected tracing."));
13811 remote_target::core_of_thread (ptid_t ptid
)
13813 thread_info
*info
= find_thread_ptid (this, ptid
);
13815 if (info
!= NULL
&& info
->priv
!= NULL
)
13816 return get_remote_thread_info (info
)->core
;
13822 remote_target::set_circular_trace_buffer (int val
)
13824 struct remote_state
*rs
= get_remote_state ();
13827 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13828 "QTBuffer:circular:%x", val
);
13830 reply
= remote_get_noisy_reply ();
13831 if (*reply
== '\0')
13832 error (_("Target does not support this command."));
13833 if (strcmp (reply
, "OK") != 0)
13834 error (_("Bogus reply from target: %s"), reply
);
13838 remote_target::traceframe_info ()
13840 gdb::optional
<gdb::char_vector
> text
13841 = target_read_stralloc (current_inferior ()->top_target (),
13842 TARGET_OBJECT_TRACEFRAME_INFO
,
13845 return parse_traceframe_info (text
->data ());
13850 /* Handle the qTMinFTPILen packet. Returns the minimum length of
13851 instruction on which a fast tracepoint may be placed. Returns -1
13852 if the packet is not supported, and 0 if the minimum instruction
13853 length is unknown. */
13856 remote_target::get_min_fast_tracepoint_insn_len ()
13858 struct remote_state
*rs
= get_remote_state ();
13861 /* If we're not debugging a process yet, the IPA can't be
13863 if (!target_has_execution ())
13866 /* Make sure the remote is pointing at the right process. */
13867 set_general_process ();
13869 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
13871 reply
= remote_get_noisy_reply ();
13872 if (*reply
== '\0')
13876 ULONGEST min_insn_len
;
13878 unpack_varlen_hex (reply
, &min_insn_len
);
13880 return (int) min_insn_len
;
13885 remote_target::set_trace_buffer_size (LONGEST val
)
13887 if (packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
13889 struct remote_state
*rs
= get_remote_state ();
13890 char *buf
= rs
->buf
.data ();
13891 char *endbuf
= buf
+ get_remote_packet_size ();
13892 enum packet_result result
;
13894 gdb_assert (val
>= 0 || val
== -1);
13895 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
13896 /* Send -1 as literal "-1" to avoid host size dependency. */
13900 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
13903 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
13906 remote_get_noisy_reply ();
13907 result
= packet_ok (rs
->buf
,
13908 &remote_protocol_packets
[PACKET_QTBuffer_size
]);
13910 if (result
!= PACKET_OK
)
13911 warning (_("Bogus reply from target: %s"), rs
->buf
.data ());
13916 remote_target::set_trace_notes (const char *user
, const char *notes
,
13917 const char *stop_notes
)
13919 struct remote_state
*rs
= get_remote_state ();
13921 char *buf
= rs
->buf
.data ();
13922 char *endbuf
= buf
+ get_remote_packet_size ();
13925 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
13928 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
13929 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
13935 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
13936 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
13942 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
13943 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
13947 /* Ensure the buffer is terminated. */
13951 reply
= remote_get_noisy_reply ();
13952 if (*reply
== '\0')
13955 if (strcmp (reply
, "OK") != 0)
13956 error (_("Bogus reply from target: %s"), reply
);
13962 remote_target::use_agent (bool use
)
13964 if (packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
13966 struct remote_state
*rs
= get_remote_state ();
13968 /* If the stub supports QAgent. */
13969 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
13971 getpkt (&rs
->buf
, 0);
13973 if (strcmp (rs
->buf
.data (), "OK") == 0)
13984 remote_target::can_use_agent ()
13986 return (packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
13989 struct btrace_target_info
13991 /* The ptid of the traced thread. */
13994 /* The obtained branch trace configuration. */
13995 struct btrace_config conf
;
13998 /* Reset our idea of our target's btrace configuration. */
14001 remote_btrace_reset (remote_state
*rs
)
14003 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14006 /* Synchronize the configuration with the target. */
14009 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14011 struct packet_config
*packet
;
14012 struct remote_state
*rs
;
14013 char *buf
, *pos
, *endbuf
;
14015 rs
= get_remote_state ();
14016 buf
= rs
->buf
.data ();
14017 endbuf
= buf
+ get_remote_packet_size ();
14019 packet
= &remote_protocol_packets
[PACKET_Qbtrace_conf_bts_size
];
14020 if (packet_config_support (packet
) == PACKET_ENABLE
14021 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14024 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x", packet
->name
,
14028 getpkt (&rs
->buf
, 0);
14030 if (packet_ok (buf
, packet
) == PACKET_ERROR
)
14032 if (buf
[0] == 'E' && buf
[1] == '.')
14033 error (_("Failed to configure the BTS buffer size: %s"), buf
+ 2);
14035 error (_("Failed to configure the BTS buffer size."));
14038 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14041 packet
= &remote_protocol_packets
[PACKET_Qbtrace_conf_pt_size
];
14042 if (packet_config_support (packet
) == PACKET_ENABLE
14043 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14046 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x", packet
->name
,
14050 getpkt (&rs
->buf
, 0);
14052 if (packet_ok (buf
, packet
) == PACKET_ERROR
)
14054 if (buf
[0] == 'E' && buf
[1] == '.')
14055 error (_("Failed to configure the trace buffer size: %s"), buf
+ 2);
14057 error (_("Failed to configure the trace buffer size."));
14060 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14064 /* Read TP's btrace configuration from the target and store it into CONF. */
14067 btrace_read_config (thread_info
*tp
, struct btrace_config
*conf
)
14069 /* target_read_stralloc relies on INFERIOR_PTID. */
14070 scoped_restore_current_thread restore_thread
;
14071 switch_to_thread (tp
);
14073 gdb::optional
<gdb::char_vector
> xml
14074 = target_read_stralloc (current_inferior ()->top_target (),
14075 TARGET_OBJECT_BTRACE_CONF
, "");
14077 parse_xml_btrace_conf (conf
, xml
->data ());
14080 /* Maybe reopen target btrace. */
14083 remote_target::remote_btrace_maybe_reopen ()
14085 struct remote_state
*rs
= get_remote_state ();
14086 int btrace_target_pushed
= 0;
14087 #if !defined (HAVE_LIBIPT)
14091 /* Don't bother walking the entirety of the remote thread list when
14092 we know the feature isn't supported by the remote. */
14093 if (packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
14096 for (thread_info
*tp
: all_non_exited_threads (this))
14098 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
14099 btrace_read_config (tp
, &rs
->btrace_config
);
14101 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
14104 #if !defined (HAVE_LIBIPT)
14105 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
14110 warning (_("Target is recording using Intel Processor Trace "
14111 "but support was disabled at compile time."));
14116 #endif /* !defined (HAVE_LIBIPT) */
14118 /* Push target, once, but before anything else happens. This way our
14119 changes to the threads will be cleaned up by unpushing the target
14120 in case btrace_read_config () throws. */
14121 if (!btrace_target_pushed
)
14123 btrace_target_pushed
= 1;
14124 record_btrace_push_target ();
14125 gdb_printf (_("Target is recording using %s.\n"),
14126 btrace_format_string (rs
->btrace_config
.format
));
14129 tp
->btrace
.target
= XCNEW (struct btrace_target_info
);
14130 tp
->btrace
.target
->ptid
= tp
->ptid
;
14131 tp
->btrace
.target
->conf
= rs
->btrace_config
;
14135 /* Enable branch tracing. */
14137 struct btrace_target_info
*
14138 remote_target::enable_btrace (thread_info
*tp
,
14139 const struct btrace_config
*conf
)
14141 struct btrace_target_info
*tinfo
= NULL
;
14142 struct packet_config
*packet
= NULL
;
14143 struct remote_state
*rs
= get_remote_state ();
14144 char *buf
= rs
->buf
.data ();
14145 char *endbuf
= buf
+ get_remote_packet_size ();
14147 switch (conf
->format
)
14149 case BTRACE_FORMAT_BTS
:
14150 packet
= &remote_protocol_packets
[PACKET_Qbtrace_bts
];
14153 case BTRACE_FORMAT_PT
:
14154 packet
= &remote_protocol_packets
[PACKET_Qbtrace_pt
];
14158 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
14159 error (_("Target does not support branch tracing."));
14161 btrace_sync_conf (conf
);
14163 ptid_t ptid
= tp
->ptid
;
14164 set_general_thread (ptid
);
14166 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
14168 getpkt (&rs
->buf
, 0);
14170 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
14172 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14173 error (_("Could not enable branch tracing for %s: %s"),
14174 target_pid_to_str (ptid
).c_str (), &rs
->buf
[2]);
14176 error (_("Could not enable branch tracing for %s."),
14177 target_pid_to_str (ptid
).c_str ());
14180 tinfo
= XCNEW (struct btrace_target_info
);
14181 tinfo
->ptid
= ptid
;
14183 /* If we fail to read the configuration, we lose some information, but the
14184 tracing itself is not impacted. */
14187 btrace_read_config (tp
, &tinfo
->conf
);
14189 catch (const gdb_exception_error
&err
)
14191 if (err
.message
!= NULL
)
14192 warning ("%s", err
.what ());
14198 /* Disable branch tracing. */
14201 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
14203 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_off
];
14204 struct remote_state
*rs
= get_remote_state ();
14205 char *buf
= rs
->buf
.data ();
14206 char *endbuf
= buf
+ get_remote_packet_size ();
14208 if (packet_config_support (packet
) != PACKET_ENABLE
)
14209 error (_("Target does not support branch tracing."));
14211 set_general_thread (tinfo
->ptid
);
14213 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
14215 getpkt (&rs
->buf
, 0);
14217 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
14219 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14220 error (_("Could not disable branch tracing for %s: %s"),
14221 target_pid_to_str (tinfo
->ptid
).c_str (), &rs
->buf
[2]);
14223 error (_("Could not disable branch tracing for %s."),
14224 target_pid_to_str (tinfo
->ptid
).c_str ());
14230 /* Teardown branch tracing. */
14233 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
14235 /* We must not talk to the target during teardown. */
14239 /* Read the branch trace. */
14242 remote_target::read_btrace (struct btrace_data
*btrace
,
14243 struct btrace_target_info
*tinfo
,
14244 enum btrace_read_type type
)
14246 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_qXfer_btrace
];
14249 if (packet_config_support (packet
) != PACKET_ENABLE
)
14250 error (_("Target does not support branch tracing."));
14252 #if !defined(HAVE_LIBEXPAT)
14253 error (_("Cannot process branch tracing result. XML parsing not supported."));
14258 case BTRACE_READ_ALL
:
14261 case BTRACE_READ_NEW
:
14264 case BTRACE_READ_DELTA
:
14268 internal_error (__FILE__
, __LINE__
,
14269 _("Bad branch tracing read type: %u."),
14270 (unsigned int) type
);
14273 gdb::optional
<gdb::char_vector
> xml
14274 = target_read_stralloc (current_inferior ()->top_target (),
14275 TARGET_OBJECT_BTRACE
, annex
);
14277 return BTRACE_ERR_UNKNOWN
;
14279 parse_xml_btrace (btrace
, xml
->data ());
14281 return BTRACE_ERR_NONE
;
14284 const struct btrace_config
*
14285 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
14287 return &tinfo
->conf
;
14291 remote_target::augmented_libraries_svr4_read ()
14293 return (packet_support (PACKET_augmented_libraries_svr4_read_feature
)
14297 /* Implementation of to_load. */
14300 remote_target::load (const char *name
, int from_tty
)
14302 generic_load (name
, from_tty
);
14305 /* Accepts an integer PID; returns a string representing a file that
14306 can be opened on the remote side to get the symbols for the child
14307 process. Returns NULL if the operation is not supported. */
14310 remote_target::pid_to_exec_file (int pid
)
14312 static gdb::optional
<gdb::char_vector
> filename
;
14313 char *annex
= NULL
;
14315 if (packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
14318 inferior
*inf
= find_inferior_pid (this, pid
);
14320 internal_error (__FILE__
, __LINE__
,
14321 _("not currently attached to process %d"), pid
);
14323 if (!inf
->fake_pid_p
)
14325 const int annex_size
= 9;
14327 annex
= (char *) alloca (annex_size
);
14328 xsnprintf (annex
, annex_size
, "%x", pid
);
14331 filename
= target_read_stralloc (current_inferior ()->top_target (),
14332 TARGET_OBJECT_EXEC_FILE
, annex
);
14334 return filename
? filename
->data () : nullptr;
14337 /* Implement the to_can_do_single_step target_ops method. */
14340 remote_target::can_do_single_step ()
14342 /* We can only tell whether target supports single step or not by
14343 supported s and S vCont actions if the stub supports vContSupported
14344 feature. If the stub doesn't support vContSupported feature,
14345 we have conservatively to think target doesn't supports single
14347 if (packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
14349 struct remote_state
*rs
= get_remote_state ();
14351 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
14352 remote_vcont_probe ();
14354 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
14360 /* Implementation of the to_execution_direction method for the remote
14363 enum exec_direction_kind
14364 remote_target::execution_direction ()
14366 struct remote_state
*rs
= get_remote_state ();
14368 return rs
->last_resume_exec_dir
;
14371 /* Return pointer to the thread_info struct which corresponds to
14372 THREAD_HANDLE (having length HANDLE_LEN). */
14375 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
14379 for (thread_info
*tp
: all_non_exited_threads (this))
14381 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14383 if (tp
->inf
== inf
&& priv
!= NULL
)
14385 if (handle_len
!= priv
->thread_handle
.size ())
14386 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
14387 handle_len
, priv
->thread_handle
.size ());
14388 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
14398 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
14400 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14401 return priv
->thread_handle
;
14405 remote_target::can_async_p ()
14407 /* This flag should be checked in the common target.c code. */
14408 gdb_assert (target_async_permitted
);
14410 /* We're async whenever the serial device can. */
14411 struct remote_state
*rs
= get_remote_state ();
14412 return serial_can_async_p (rs
->remote_desc
);
14416 remote_target::is_async_p ()
14418 /* We're async whenever the serial device is. */
14419 struct remote_state
*rs
= get_remote_state ();
14420 return serial_is_async_p (rs
->remote_desc
);
14423 /* Pass the SERIAL event on and up to the client. One day this code
14424 will be able to delay notifying the client of an event until the
14425 point where an entire packet has been received. */
14427 static serial_event_ftype remote_async_serial_handler
;
14430 remote_async_serial_handler (struct serial
*scb
, void *context
)
14432 /* Don't propogate error information up to the client. Instead let
14433 the client find out about the error by querying the target. */
14434 inferior_event_handler (INF_REG_EVENT
);
14438 remote_async_inferior_event_handler (gdb_client_data data
)
14440 inferior_event_handler (INF_REG_EVENT
);
14444 remote_target::async_wait_fd ()
14446 struct remote_state
*rs
= get_remote_state ();
14447 return rs
->remote_desc
->fd
;
14451 remote_target::async (bool enable
)
14453 struct remote_state
*rs
= get_remote_state ();
14457 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
14459 /* If there are pending events in the stop reply queue tell the
14460 event loop to process them. */
14461 if (!rs
->stop_reply_queue
.empty ())
14462 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
14463 /* For simplicity, below we clear the pending events token
14464 without remembering whether it is marked, so here we always
14465 mark it. If there's actually no pending notification to
14466 process, this ends up being a no-op (other than a spurious
14467 event-loop wakeup). */
14468 if (target_is_non_stop_p ())
14469 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14473 serial_async (rs
->remote_desc
, NULL
, NULL
);
14474 /* If the core is disabling async, it doesn't want to be
14475 disturbed with target events. Clear all async event sources
14477 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
14478 if (target_is_non_stop_p ())
14479 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14483 /* Implementation of the to_thread_events method. */
14486 remote_target::thread_events (int enable
)
14488 struct remote_state
*rs
= get_remote_state ();
14489 size_t size
= get_remote_packet_size ();
14491 if (packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
14494 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
14496 getpkt (&rs
->buf
, 0);
14498 switch (packet_ok (rs
->buf
,
14499 &remote_protocol_packets
[PACKET_QThreadEvents
]))
14502 if (strcmp (rs
->buf
.data (), "OK") != 0)
14503 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
14506 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
14508 case PACKET_UNKNOWN
:
14514 show_remote_cmd (const char *args
, int from_tty
)
14516 /* We can't just use cmd_show_list here, because we want to skip
14517 the redundant "show remote Z-packet" and the legacy aliases. */
14518 struct cmd_list_element
*list
= remote_show_cmdlist
;
14519 struct ui_out
*uiout
= current_uiout
;
14521 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
14522 for (; list
!= NULL
; list
= list
->next
)
14523 if (strcmp (list
->name
, "Z-packet") == 0)
14525 else if (list
->type
== not_set_cmd
)
14526 /* Alias commands are exactly like the original, except they
14527 don't have the normal type. */
14531 ui_out_emit_tuple
option_emitter (uiout
, "option");
14533 uiout
->field_string ("name", list
->name
);
14534 uiout
->text (": ");
14535 if (list
->type
== show_cmd
)
14536 do_show_command (NULL
, from_tty
, list
);
14538 cmd_func (list
, NULL
, from_tty
);
14543 /* Function to be called whenever a new objfile (shlib) is detected. */
14545 remote_new_objfile (struct objfile
*objfile
)
14547 /* The objfile change happened in that program space. */
14548 program_space
*pspace
= current_program_space
;
14550 /* The affected program space is possibly shared by multiple inferiors.
14551 Consider sending a qSymbol packet for each of the inferiors using that
14553 for (inferior
*inf
: all_inferiors ())
14555 if (inf
->pspace
!= pspace
)
14558 /* Check whether the inferior's process target is a remote target. */
14559 remote_target
*remote
= as_remote_target (inf
->process_target ());
14560 if (remote
== nullptr)
14563 /* When we are attaching or handling a fork child and the shared library
14564 subsystem reads the list of loaded libraries, we receive new objfile
14565 events in between each found library. The libraries are read in an
14566 undefined order, so if we gave the remote side a chance to look up
14567 symbols between each objfile, we might give it an inconsistent picture
14568 of the inferior. It could appear that a library A appears loaded but
14569 a library B does not, even though library A requires library B. That
14570 would present a state that couldn't normally exist in the inferior.
14572 So, skip these events, we'll give the remote a chance to look up
14573 symbols once all the loaded libraries and their symbols are known to
14575 if (inf
->in_initial_library_scan
)
14578 if (!remote
->has_execution (inf
))
14581 /* Need to switch to a specific thread, because remote_check_symbols will
14582 set the general thread using INFERIOR_PTID.
14584 It's possible to have inferiors with no thread here, because we are
14585 called very early in the connection process, while the inferior is
14586 being set up, before threads are added. Just skip it, start_remote_1
14587 also calls remote_check_symbols when it's done setting things up. */
14588 thread_info
*thread
= any_thread_of_inferior (inf
);
14589 if (thread
!= nullptr)
14591 scoped_restore_current_thread restore_thread
;
14592 switch_to_thread (thread
);
14593 remote
->remote_check_symbols ();
14598 /* Pull all the tracepoints defined on the target and create local
14599 data structures representing them. We don't want to create real
14600 tracepoints yet, we don't want to mess up the user's existing
14604 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
14606 struct remote_state
*rs
= get_remote_state ();
14609 /* Ask for a first packet of tracepoint definition. */
14611 getpkt (&rs
->buf
, 0);
14612 p
= rs
->buf
.data ();
14613 while (*p
&& *p
!= 'l')
14615 parse_tracepoint_definition (p
, utpp
);
14616 /* Ask for another packet of tracepoint definition. */
14618 getpkt (&rs
->buf
, 0);
14619 p
= rs
->buf
.data ();
14625 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
14627 struct remote_state
*rs
= get_remote_state ();
14630 /* Ask for a first packet of variable definition. */
14632 getpkt (&rs
->buf
, 0);
14633 p
= rs
->buf
.data ();
14634 while (*p
&& *p
!= 'l')
14636 parse_tsv_definition (p
, utsvp
);
14637 /* Ask for another packet of variable definition. */
14639 getpkt (&rs
->buf
, 0);
14640 p
= rs
->buf
.data ();
14645 /* The "set/show range-stepping" show hook. */
14648 show_range_stepping (struct ui_file
*file
, int from_tty
,
14649 struct cmd_list_element
*c
,
14653 _("Debugger's willingness to use range stepping "
14654 "is %s.\n"), value
);
14657 /* Return true if the vCont;r action is supported by the remote
14661 remote_target::vcont_r_supported ()
14663 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
14664 remote_vcont_probe ();
14666 return (packet_support (PACKET_vCont
) == PACKET_ENABLE
14667 && get_remote_state ()->supports_vCont
.r
);
14670 /* The "set/show range-stepping" set hook. */
14673 set_range_stepping (const char *ignore_args
, int from_tty
,
14674 struct cmd_list_element
*c
)
14676 /* When enabling, check whether range stepping is actually supported
14677 by the target, and warn if not. */
14678 if (use_range_stepping
)
14680 remote_target
*remote
= get_current_remote_target ();
14682 || !remote
->vcont_r_supported ())
14683 warning (_("Range stepping is not supported by the current target"));
14688 show_remote_debug (struct ui_file
*file
, int from_tty
,
14689 struct cmd_list_element
*c
, const char *value
)
14691 gdb_printf (file
, _("Debugging of remote protocol is %s.\n"),
14696 show_remote_timeout (struct ui_file
*file
, int from_tty
,
14697 struct cmd_list_element
*c
, const char *value
)
14700 _("Timeout limit to wait for target to respond is %s.\n"),
14704 /* Implement the "supports_memory_tagging" target_ops method. */
14707 remote_target::supports_memory_tagging ()
14709 return remote_memory_tagging_p ();
14712 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
14715 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14716 size_t len
, int type
)
14718 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14720 std::string request
= string_printf ("qMemTags:%s,%s:%s",
14721 phex_nz (address
, addr_size
),
14722 phex_nz (len
, sizeof (len
)),
14723 phex_nz (type
, sizeof (type
)));
14725 strcpy (packet
.data (), request
.c_str ());
14728 /* Parse the qMemTags packet reply into TAGS.
14730 Return true if successful, false otherwise. */
14733 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
14734 gdb::byte_vector
&tags
)
14736 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
14739 /* Copy the tag data. */
14740 tags
= hex2bin (reply
.data () + 1);
14745 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
14748 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14749 size_t len
, int type
,
14750 const gdb::byte_vector
&tags
)
14752 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14754 /* Put together the main packet, address and length. */
14755 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
14756 phex_nz (address
, addr_size
),
14757 phex_nz (len
, sizeof (len
)),
14758 phex_nz (type
, sizeof (type
)));
14759 request
+= bin2hex (tags
.data (), tags
.size ());
14761 /* Check if we have exceeded the maximum packet size. */
14762 if (packet
.size () < request
.length ())
14763 error (_("Contents too big for packet QMemTags."));
14765 strcpy (packet
.data (), request
.c_str ());
14768 /* Implement the "fetch_memtags" target_ops method. */
14771 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
14772 gdb::byte_vector
&tags
, int type
)
14774 /* Make sure the qMemTags packet is supported. */
14775 if (!remote_memory_tagging_p ())
14776 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
14778 struct remote_state
*rs
= get_remote_state ();
14780 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
14783 getpkt (&rs
->buf
, 0);
14785 return parse_fetch_memtags_reply (rs
->buf
, tags
);
14788 /* Implement the "store_memtags" target_ops method. */
14791 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
14792 const gdb::byte_vector
&tags
, int type
)
14794 /* Make sure the QMemTags packet is supported. */
14795 if (!remote_memory_tagging_p ())
14796 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
14798 struct remote_state
*rs
= get_remote_state ();
14800 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
14803 getpkt (&rs
->buf
, 0);
14805 /* Verify if the request was successful. */
14806 return packet_check_result (rs
->buf
.data ()) == PACKET_OK
;
14809 /* Return true if remote target T is non-stop. */
14812 remote_target_is_non_stop_p (remote_target
*t
)
14814 scoped_restore_current_thread restore_thread
;
14815 switch_to_target_no_thread (t
);
14817 return target_is_non_stop_p ();
14822 namespace selftests
{
14825 test_memory_tagging_functions ()
14827 remote_target remote
;
14829 struct packet_config
*config
14830 = &remote_protocol_packets
[PACKET_memory_tagging_feature
];
14832 scoped_restore restore_memtag_support_
14833 = make_scoped_restore (&config
->support
);
14835 /* Test memory tagging packet support. */
14836 config
->support
= PACKET_SUPPORT_UNKNOWN
;
14837 SELF_CHECK (remote
.supports_memory_tagging () == false);
14838 config
->support
= PACKET_DISABLE
;
14839 SELF_CHECK (remote
.supports_memory_tagging () == false);
14840 config
->support
= PACKET_ENABLE
;
14841 SELF_CHECK (remote
.supports_memory_tagging () == true);
14843 /* Setup testing. */
14844 gdb::char_vector packet
;
14845 gdb::byte_vector tags
, bv
;
14846 std::string expected
, reply
;
14847 packet
.resize (32000);
14849 /* Test creating a qMemTags request. */
14851 expected
= "qMemTags:0,0:0";
14852 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
14853 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14855 expected
= "qMemTags:deadbeef,10:1";
14856 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
14857 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14859 /* Test parsing a qMemTags reply. */
14861 /* Error reply, tags vector unmodified. */
14863 strcpy (packet
.data (), reply
.c_str ());
14865 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
14866 SELF_CHECK (tags
.size () == 0);
14868 /* Valid reply, tags vector updated. */
14872 for (int i
= 0; i
< 5; i
++)
14875 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
14876 strcpy (packet
.data (), reply
.c_str ());
14878 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
14879 SELF_CHECK (tags
.size () == 5);
14881 for (int i
= 0; i
< 5; i
++)
14882 SELF_CHECK (tags
[i
] == i
);
14884 /* Test creating a QMemTags request. */
14886 /* Empty tag data. */
14888 expected
= "QMemTags:0,0:0:";
14889 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
14890 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
14891 expected
.length ()) == 0);
14893 /* Non-empty tag data. */
14895 for (int i
= 0; i
< 5; i
++)
14896 tags
.push_back (i
);
14897 expected
= "QMemTags:deadbeef,ff:1:0001020304";
14898 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
14899 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
14900 expected
.length ()) == 0);
14903 } // namespace selftests
14904 #endif /* GDB_SELF_TEST */
14906 void _initialize_remote ();
14908 _initialize_remote ()
14910 add_target (remote_target_info
, remote_target::open
);
14911 add_target (extended_remote_target_info
, extended_remote_target::open
);
14913 /* Hook into new objfile notification. */
14914 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
14917 init_remote_threadtests ();
14920 /* set/show remote ... */
14922 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
14923 Remote protocol specific variables.\n\
14924 Configure various remote-protocol specific variables such as\n\
14925 the packets being used."),
14926 &remote_set_cmdlist
,
14927 0 /* allow-unknown */, &setlist
);
14928 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
14929 Remote protocol specific variables.\n\
14930 Configure various remote-protocol specific variables such as\n\
14931 the packets being used."),
14932 &remote_show_cmdlist
,
14933 0 /* allow-unknown */, &showlist
);
14935 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
14936 Compare section data on target to the exec file.\n\
14937 Argument is a single section name (default: all loaded sections).\n\
14938 To compare only read-only loaded sections, specify the -r option."),
14941 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
14942 Send an arbitrary packet to a remote target.\n\
14943 maintenance packet TEXT\n\
14944 If GDB is talking to an inferior via the GDB serial protocol, then\n\
14945 this command sends the string TEXT to the inferior, and displays the\n\
14946 response packet. GDB supplies the initial `$' character, and the\n\
14947 terminating `#' character and checksum."),
14950 set_show_commands remotebreak_cmds
14951 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
14952 Set whether to send break if interrupted."), _("\
14953 Show whether to send break if interrupted."), _("\
14954 If set, a break, instead of a cntrl-c, is sent to the remote target."),
14955 set_remotebreak
, show_remotebreak
,
14956 &setlist
, &showlist
);
14957 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
14958 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
14960 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
14961 interrupt_sequence_modes
, &interrupt_sequence_mode
,
14963 Set interrupt sequence to remote target."), _("\
14964 Show interrupt sequence to remote target."), _("\
14965 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14966 NULL
, show_interrupt_sequence
,
14967 &remote_set_cmdlist
,
14968 &remote_show_cmdlist
);
14970 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
14971 &interrupt_on_connect
, _("\
14972 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
14973 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
14974 If set, interrupt sequence is sent to remote target."),
14976 &remote_set_cmdlist
, &remote_show_cmdlist
);
14978 /* Install commands for configuring memory read/write packets. */
14980 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
14981 Set the maximum number of bytes per memory write packet (deprecated)."),
14983 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
14984 Show the maximum number of bytes per memory write packet (deprecated)."),
14986 add_cmd ("memory-write-packet-size", no_class
,
14987 set_memory_write_packet_size
, _("\
14988 Set the maximum number of bytes per memory-write packet.\n\
14989 Specify the number of bytes in a packet or 0 (zero) for the\n\
14990 default packet size. The actual limit is further reduced\n\
14991 dependent on the target. Specify ``fixed'' to disable the\n\
14992 further restriction and ``limit'' to enable that restriction."),
14993 &remote_set_cmdlist
);
14994 add_cmd ("memory-read-packet-size", no_class
,
14995 set_memory_read_packet_size
, _("\
14996 Set the maximum number of bytes per memory-read packet.\n\
14997 Specify the number of bytes in a packet or 0 (zero) for the\n\
14998 default packet size. The actual limit is further reduced\n\
14999 dependent on the target. Specify ``fixed'' to disable the\n\
15000 further restriction and ``limit'' to enable that restriction."),
15001 &remote_set_cmdlist
);
15002 add_cmd ("memory-write-packet-size", no_class
,
15003 show_memory_write_packet_size
,
15004 _("Show the maximum number of bytes per memory-write packet."),
15005 &remote_show_cmdlist
);
15006 add_cmd ("memory-read-packet-size", no_class
,
15007 show_memory_read_packet_size
,
15008 _("Show the maximum number of bytes per memory-read packet."),
15009 &remote_show_cmdlist
);
15011 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
15012 &remote_hw_watchpoint_limit
, _("\
15013 Set the maximum number of target hardware watchpoints."), _("\
15014 Show the maximum number of target hardware watchpoints."), _("\
15015 Specify \"unlimited\" for unlimited hardware watchpoints."),
15016 NULL
, show_hardware_watchpoint_limit
,
15017 &remote_set_cmdlist
,
15018 &remote_show_cmdlist
);
15019 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
15021 &remote_hw_watchpoint_length_limit
, _("\
15022 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
15023 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
15024 Specify \"unlimited\" to allow watchpoints of unlimited size."),
15025 NULL
, show_hardware_watchpoint_length_limit
,
15026 &remote_set_cmdlist
, &remote_show_cmdlist
);
15027 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
15028 &remote_hw_breakpoint_limit
, _("\
15029 Set the maximum number of target hardware breakpoints."), _("\
15030 Show the maximum number of target hardware breakpoints."), _("\
15031 Specify \"unlimited\" for unlimited hardware breakpoints."),
15032 NULL
, show_hardware_breakpoint_limit
,
15033 &remote_set_cmdlist
, &remote_show_cmdlist
);
15035 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
15036 &remote_address_size
, _("\
15037 Set the maximum size of the address (in bits) in a memory packet."), _("\
15038 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
15040 NULL
, /* FIXME: i18n: */
15041 &setlist
, &showlist
);
15043 init_all_packet_configs ();
15045 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
15046 "X", "binary-download", 1);
15048 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
15049 "vCont", "verbose-resume", 0);
15051 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
15052 "QPassSignals", "pass-signals", 0);
15054 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QCatchSyscalls
],
15055 "QCatchSyscalls", "catch-syscalls", 0);
15057 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QProgramSignals
],
15058 "QProgramSignals", "program-signals", 0);
15060 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QSetWorkingDir
],
15061 "QSetWorkingDir", "set-working-dir", 0);
15063 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartupWithShell
],
15064 "QStartupWithShell", "startup-with-shell", 0);
15066 add_packet_config_cmd (&remote_protocol_packets
15067 [PACKET_QEnvironmentHexEncoded
],
15068 "QEnvironmentHexEncoded", "environment-hex-encoded",
15071 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QEnvironmentReset
],
15072 "QEnvironmentReset", "environment-reset",
15075 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QEnvironmentUnset
],
15076 "QEnvironmentUnset", "environment-unset",
15079 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
15080 "qSymbol", "symbol-lookup", 0);
15082 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
15083 "P", "set-register", 1);
15085 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
15086 "p", "fetch-register", 1);
15088 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
15089 "Z0", "software-breakpoint", 0);
15091 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
15092 "Z1", "hardware-breakpoint", 0);
15094 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
15095 "Z2", "write-watchpoint", 0);
15097 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
15098 "Z3", "read-watchpoint", 0);
15100 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
15101 "Z4", "access-watchpoint", 0);
15103 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
15104 "qXfer:auxv:read", "read-aux-vector", 0);
15106 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_exec_file
],
15107 "qXfer:exec-file:read", "pid-to-exec-file", 0);
15109 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
15110 "qXfer:features:read", "target-features", 0);
15112 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
15113 "qXfer:libraries:read", "library-info", 0);
15115 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries_svr4
],
15116 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
15118 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
15119 "qXfer:memory-map:read", "memory-map", 0);
15121 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
15122 "qXfer:osdata:read", "osdata", 0);
15124 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
15125 "qXfer:threads:read", "threads", 0);
15127 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
15128 "qXfer:siginfo:read", "read-siginfo-object", 0);
15130 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
15131 "qXfer:siginfo:write", "write-siginfo-object", 0);
15133 add_packet_config_cmd
15134 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
15135 "qXfer:traceframe-info:read", "traceframe-info", 0);
15137 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_uib
],
15138 "qXfer:uib:read", "unwind-info-block", 0);
15140 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
15141 "qGetTLSAddr", "get-thread-local-storage-address",
15144 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
15145 "qGetTIBAddr", "get-thread-information-block-address",
15148 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
15149 "bc", "reverse-continue", 0);
15151 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
15152 "bs", "reverse-step", 0);
15154 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
15155 "qSupported", "supported-packets", 0);
15157 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
15158 "qSearch:memory", "search-memory", 0);
15160 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qTStatus
],
15161 "qTStatus", "trace-status", 0);
15163 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_setfs
],
15164 "vFile:setfs", "hostio-setfs", 0);
15166 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
15167 "vFile:open", "hostio-open", 0);
15169 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
15170 "vFile:pread", "hostio-pread", 0);
15172 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
15173 "vFile:pwrite", "hostio-pwrite", 0);
15175 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
15176 "vFile:close", "hostio-close", 0);
15178 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
15179 "vFile:unlink", "hostio-unlink", 0);
15181 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_readlink
],
15182 "vFile:readlink", "hostio-readlink", 0);
15184 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_fstat
],
15185 "vFile:fstat", "hostio-fstat", 0);
15187 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
15188 "vAttach", "attach", 0);
15190 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
15193 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
15194 "QStartNoAckMode", "noack", 0);
15196 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
15197 "vKill", "kill", 0);
15199 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
15200 "qAttached", "query-attached", 0);
15202 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
15203 "ConditionalTracepoints",
15204 "conditional-tracepoints", 0);
15206 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalBreakpoints
],
15207 "ConditionalBreakpoints",
15208 "conditional-breakpoints", 0);
15210 add_packet_config_cmd (&remote_protocol_packets
[PACKET_BreakpointCommands
],
15211 "BreakpointCommands",
15212 "breakpoint-commands", 0);
15214 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
15215 "FastTracepoints", "fast-tracepoints", 0);
15217 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
15218 "TracepointSource", "TracepointSource", 0);
15220 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
15221 "QAllow", "allow", 0);
15223 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
15224 "StaticTracepoints", "static-tracepoints", 0);
15226 add_packet_config_cmd (&remote_protocol_packets
[PACKET_InstallInTrace
],
15227 "InstallInTrace", "install-in-trace", 0);
15229 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
15230 "qXfer:statictrace:read", "read-sdata-object", 0);
15232 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_fdpic
],
15233 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
15235 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QDisableRandomization
],
15236 "QDisableRandomization", "disable-randomization", 0);
15238 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAgent
],
15239 "QAgent", "agent", 0);
15241 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QTBuffer_size
],
15242 "QTBuffer:size", "trace-buffer-size", 0);
15244 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_off
],
15245 "Qbtrace:off", "disable-btrace", 0);
15247 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_bts
],
15248 "Qbtrace:bts", "enable-btrace-bts", 0);
15250 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_pt
],
15251 "Qbtrace:pt", "enable-btrace-pt", 0);
15253 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace
],
15254 "qXfer:btrace", "read-btrace", 0);
15256 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace_conf
],
15257 "qXfer:btrace-conf", "read-btrace-conf", 0);
15259 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_conf_bts_size
],
15260 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
15262 add_packet_config_cmd (&remote_protocol_packets
[PACKET_multiprocess_feature
],
15263 "multiprocess-feature", "multiprocess-feature", 0);
15265 add_packet_config_cmd (&remote_protocol_packets
[PACKET_swbreak_feature
],
15266 "swbreak-feature", "swbreak-feature", 0);
15268 add_packet_config_cmd (&remote_protocol_packets
[PACKET_hwbreak_feature
],
15269 "hwbreak-feature", "hwbreak-feature", 0);
15271 add_packet_config_cmd (&remote_protocol_packets
[PACKET_fork_event_feature
],
15272 "fork-event-feature", "fork-event-feature", 0);
15274 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vfork_event_feature
],
15275 "vfork-event-feature", "vfork-event-feature", 0);
15277 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_conf_pt_size
],
15278 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
15280 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vContSupported
],
15281 "vContSupported", "verbose-resume-supported", 0);
15283 add_packet_config_cmd (&remote_protocol_packets
[PACKET_exec_event_feature
],
15284 "exec-event-feature", "exec-event-feature", 0);
15286 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCtrlC
],
15287 "vCtrlC", "ctrl-c", 0);
15289 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QThreadEvents
],
15290 "QThreadEvents", "thread-events", 0);
15292 add_packet_config_cmd (&remote_protocol_packets
[PACKET_no_resumed
],
15293 "N stop reply", "no-resumed-stop-reply", 0);
15295 add_packet_config_cmd (&remote_protocol_packets
[PACKET_memory_tagging_feature
],
15296 "memory-tagging-feature", "memory-tagging-feature", 0);
15298 /* Assert that we've registered "set remote foo-packet" commands
15299 for all packet configs. */
15303 for (i
= 0; i
< PACKET_MAX
; i
++)
15305 /* Ideally all configs would have a command associated. Some
15306 still don't though. */
15311 case PACKET_QNonStop
:
15312 case PACKET_EnableDisableTracepoints_feature
:
15313 case PACKET_tracenz_feature
:
15314 case PACKET_DisconnectedTracing_feature
:
15315 case PACKET_augmented_libraries_svr4_read_feature
:
15317 /* Additions to this list need to be well justified:
15318 pre-existing packets are OK; new packets are not. */
15326 /* This catches both forgetting to add a config command, and
15327 forgetting to remove a packet from the exception list. */
15328 gdb_assert (excepted
== (remote_protocol_packets
[i
].name
== NULL
));
15332 /* Keep the old ``set remote Z-packet ...'' working. Each individual
15333 Z sub-packet has its own set and show commands, but users may
15334 have sets to this variable in their .gdbinit files (or in their
15336 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
15337 &remote_Z_packet_detect
, _("\
15338 Set use of remote protocol `Z' packets."), _("\
15339 Show use of remote protocol `Z' packets."), _("\
15340 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
15342 set_remote_protocol_Z_packet_cmd
,
15343 show_remote_protocol_Z_packet_cmd
,
15344 /* FIXME: i18n: Use of remote protocol
15345 `Z' packets is %s. */
15346 &remote_set_cmdlist
, &remote_show_cmdlist
);
15348 add_basic_prefix_cmd ("remote", class_files
, _("\
15349 Manipulate files on the remote system.\n\
15350 Transfer files to and from the remote target system."),
15352 0 /* allow-unknown */, &cmdlist
);
15354 add_cmd ("put", class_files
, remote_put_command
,
15355 _("Copy a local file to the remote system."),
15358 add_cmd ("get", class_files
, remote_get_command
,
15359 _("Copy a remote file to the local system."),
15362 add_cmd ("delete", class_files
, remote_delete_command
,
15363 _("Delete a remote file."),
15366 add_setshow_string_noescape_cmd ("exec-file", class_files
,
15367 &remote_exec_file_var
, _("\
15368 Set the remote pathname for \"run\"."), _("\
15369 Show the remote pathname for \"run\"."), NULL
,
15370 set_remote_exec_file
,
15371 show_remote_exec_file
,
15372 &remote_set_cmdlist
,
15373 &remote_show_cmdlist
);
15375 add_setshow_boolean_cmd ("range-stepping", class_run
,
15376 &use_range_stepping
, _("\
15377 Enable or disable range stepping."), _("\
15378 Show whether target-assisted range stepping is enabled."), _("\
15379 If on, and the target supports it, when stepping a source line, GDB\n\
15380 tells the target to step the corresponding range of addresses itself instead\n\
15381 of issuing multiple single-steps. This speeds up source level\n\
15382 stepping. If off, GDB always issues single-steps, even if range\n\
15383 stepping is supported by the target. The default is on."),
15384 set_range_stepping
,
15385 show_range_stepping
,
15389 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
15390 Set watchdog timer."), _("\
15391 Show watchdog timer."), _("\
15392 When non-zero, this timeout is used instead of waiting forever for a target\n\
15393 to finish a low-level step or continue operation. If the specified amount\n\
15394 of time passes without a response from the target, an error occurs."),
15397 &setlist
, &showlist
);
15399 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
15400 &remote_packet_max_chars
, _("\
15401 Set the maximum number of characters to display for each remote packet."), _("\
15402 Show the maximum number of characters to display for each remote packet."), _("\
15403 Specify \"unlimited\" to display all the characters."),
15404 NULL
, show_remote_packet_max_chars
,
15405 &setdebuglist
, &showdebuglist
);
15407 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
15408 _("Set debugging of remote protocol."),
15409 _("Show debugging of remote protocol."),
15411 When enabled, each packet sent or received with the remote target\n\
15415 &setdebuglist
, &showdebuglist
);
15417 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
15418 &remote_timeout
, _("\
15419 Set timeout limit to wait for target to respond."), _("\
15420 Show timeout limit to wait for target to respond."), _("\
15421 This value is used to set the time limit for gdb to wait for a response\n\
15422 from the target."),
15424 show_remote_timeout
,
15425 &setlist
, &showlist
);
15427 /* Eventually initialize fileio. See fileio.c */
15428 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
15431 selftests::register_test ("remote_memory_tagging",
15432 selftests::test_memory_tagging_functions
);