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 "gdb/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 serial target in 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 void files_info () override
;
431 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
433 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
434 enum remove_bp_reason
) override
;
437 bool stopped_by_sw_breakpoint () override
;
438 bool supports_stopped_by_sw_breakpoint () override
;
440 bool stopped_by_hw_breakpoint () override
;
442 bool supports_stopped_by_hw_breakpoint () override
;
444 bool stopped_by_watchpoint () override
;
446 bool stopped_data_address (CORE_ADDR
*) override
;
448 bool watchpoint_addr_within_range (CORE_ADDR
, CORE_ADDR
, int) override
;
450 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
452 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
454 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
456 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
458 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
459 struct expression
*) override
;
461 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
462 struct expression
*) override
;
464 void kill () override
;
466 void load (const char *, int) override
;
468 void mourn_inferior () override
;
470 void pass_signals (gdb::array_view
<const unsigned char>) override
;
472 int set_syscall_catchpoint (int, bool, int,
473 gdb::array_view
<const int>) override
;
475 void program_signals (gdb::array_view
<const unsigned char>) override
;
477 bool thread_alive (ptid_t ptid
) override
;
479 const char *thread_name (struct thread_info
*) override
;
481 void update_thread_list () override
;
483 std::string
pid_to_str (ptid_t
) override
;
485 const char *extra_thread_info (struct thread_info
*) override
;
487 ptid_t
get_ada_task_ptid (long lwp
, ULONGEST thread
) override
;
489 thread_info
*thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
491 inferior
*inf
) override
;
493 gdb::byte_vector
thread_info_to_thread_handle (struct thread_info
*tp
)
496 void stop (ptid_t
) override
;
498 void interrupt () override
;
500 void pass_ctrlc () override
;
502 enum target_xfer_status
xfer_partial (enum target_object object
,
505 const gdb_byte
*writebuf
,
506 ULONGEST offset
, ULONGEST len
,
507 ULONGEST
*xfered_len
) override
;
509 ULONGEST
get_memory_xfer_limit () override
;
511 void rcmd (const char *command
, struct ui_file
*output
) override
;
513 char *pid_to_exec_file (int pid
) override
;
515 void log_command (const char *cmd
) override
517 serial_log_command (this, cmd
);
520 CORE_ADDR
get_thread_local_address (ptid_t ptid
,
521 CORE_ADDR load_module_addr
,
522 CORE_ADDR offset
) override
;
524 bool can_execute_reverse () override
;
526 std::vector
<mem_region
> memory_map () override
;
528 void flash_erase (ULONGEST address
, LONGEST length
) override
;
530 void flash_done () override
;
532 const struct target_desc
*read_description () override
;
534 int search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
535 const gdb_byte
*pattern
, ULONGEST pattern_len
,
536 CORE_ADDR
*found_addrp
) override
;
538 bool can_async_p () override
;
540 bool is_async_p () override
;
542 void async (int) override
;
544 int async_wait_fd () override
;
546 void thread_events (int) override
;
548 int can_do_single_step () override
;
550 void terminal_inferior () override
;
552 void terminal_ours () override
;
554 bool supports_non_stop () override
;
556 bool supports_multi_process () override
;
558 bool supports_disable_randomization () override
;
560 bool filesystem_is_local () override
;
563 int fileio_open (struct inferior
*inf
, const char *filename
,
564 int flags
, int mode
, int warn_if_slow
,
565 int *target_errno
) override
;
567 int fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
568 ULONGEST offset
, int *target_errno
) override
;
570 int fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
571 ULONGEST offset
, int *target_errno
) override
;
573 int fileio_fstat (int fd
, struct stat
*sb
, int *target_errno
) override
;
575 int fileio_close (int fd
, int *target_errno
) override
;
577 int fileio_unlink (struct inferior
*inf
,
578 const char *filename
,
579 int *target_errno
) override
;
581 gdb::optional
<std::string
>
582 fileio_readlink (struct inferior
*inf
,
583 const char *filename
,
584 int *target_errno
) override
;
586 bool supports_enable_disable_tracepoint () override
;
588 bool supports_string_tracing () override
;
590 bool supports_evaluation_of_breakpoint_conditions () override
;
592 bool can_run_breakpoint_commands () override
;
594 void trace_init () override
;
596 void download_tracepoint (struct bp_location
*location
) override
;
598 bool can_download_tracepoint () override
;
600 void download_trace_state_variable (const trace_state_variable
&tsv
) override
;
602 void enable_tracepoint (struct bp_location
*location
) override
;
604 void disable_tracepoint (struct bp_location
*location
) override
;
606 void trace_set_readonly_regions () override
;
608 void trace_start () override
;
610 int get_trace_status (struct trace_status
*ts
) override
;
612 void get_tracepoint_status (struct breakpoint
*tp
, struct uploaded_tp
*utp
)
615 void trace_stop () override
;
617 int trace_find (enum trace_find_type type
, int num
,
618 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
) override
;
620 bool get_trace_state_variable_value (int tsv
, LONGEST
*val
) override
;
622 int save_trace_data (const char *filename
) override
;
624 int upload_tracepoints (struct uploaded_tp
**utpp
) override
;
626 int upload_trace_state_variables (struct uploaded_tsv
**utsvp
) override
;
628 LONGEST
get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
) override
;
630 int get_min_fast_tracepoint_insn_len () override
;
632 void set_disconnected_tracing (int val
) override
;
634 void set_circular_trace_buffer (int val
) override
;
636 void set_trace_buffer_size (LONGEST val
) override
;
638 bool set_trace_notes (const char *user
, const char *notes
,
639 const char *stopnotes
) override
;
641 int core_of_thread (ptid_t ptid
) override
;
643 int verify_memory (const gdb_byte
*data
,
644 CORE_ADDR memaddr
, ULONGEST size
) override
;
647 bool get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
) override
;
649 void set_permissions () override
;
651 bool static_tracepoint_marker_at (CORE_ADDR
,
652 struct static_tracepoint_marker
*marker
)
655 std::vector
<static_tracepoint_marker
>
656 static_tracepoint_markers_by_strid (const char *id
) override
;
658 traceframe_info_up
traceframe_info () override
;
660 bool use_agent (bool use
) override
;
661 bool can_use_agent () override
;
663 struct btrace_target_info
*enable_btrace (ptid_t ptid
,
664 const struct btrace_config
*conf
) override
;
666 void disable_btrace (struct btrace_target_info
*tinfo
) override
;
668 void teardown_btrace (struct btrace_target_info
*tinfo
) override
;
670 enum btrace_error
read_btrace (struct btrace_data
*data
,
671 struct btrace_target_info
*btinfo
,
672 enum btrace_read_type type
) override
;
674 const struct btrace_config
*btrace_conf (const struct btrace_target_info
*) override
;
675 bool augmented_libraries_svr4_read () override
;
676 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
677 void follow_exec (inferior
*, ptid_t
, const char *) override
;
678 int insert_fork_catchpoint (int) override
;
679 int remove_fork_catchpoint (int) override
;
680 int insert_vfork_catchpoint (int) override
;
681 int remove_vfork_catchpoint (int) override
;
682 int insert_exec_catchpoint (int) override
;
683 int remove_exec_catchpoint (int) override
;
684 enum exec_direction_kind
execution_direction () override
;
686 bool supports_memory_tagging () override
;
688 bool fetch_memtags (CORE_ADDR address
, size_t len
,
689 gdb::byte_vector
&tags
, int type
) override
;
691 bool store_memtags (CORE_ADDR address
, size_t len
,
692 const gdb::byte_vector
&tags
, int type
) override
;
694 public: /* Remote specific methods. */
696 void remote_download_command_source (int num
, ULONGEST addr
,
697 struct command_line
*cmds
);
699 void remote_file_put (const char *local_file
, const char *remote_file
,
701 void remote_file_get (const char *remote_file
, const char *local_file
,
703 void remote_file_delete (const char *remote_file
, int from_tty
);
705 int remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
706 ULONGEST offset
, int *remote_errno
);
707 int remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
708 ULONGEST offset
, int *remote_errno
);
709 int remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
710 ULONGEST offset
, int *remote_errno
);
712 int remote_hostio_send_command (int command_bytes
, int which_packet
,
713 int *remote_errno
, const char **attachment
,
714 int *attachment_len
);
715 int remote_hostio_set_filesystem (struct inferior
*inf
,
717 /* We should get rid of this and use fileio_open directly. */
718 int remote_hostio_open (struct inferior
*inf
, const char *filename
,
719 int flags
, int mode
, int warn_if_slow
,
721 int remote_hostio_close (int fd
, int *remote_errno
);
723 int remote_hostio_unlink (inferior
*inf
, const char *filename
,
726 struct remote_state
*get_remote_state ();
728 long get_remote_packet_size (void);
729 long get_memory_packet_size (struct memory_packet_config
*config
);
731 long get_memory_write_packet_size ();
732 long get_memory_read_packet_size ();
734 char *append_pending_thread_resumptions (char *p
, char *endp
,
736 static void open_1 (const char *name
, int from_tty
, int extended_p
);
737 void start_remote (int from_tty
, int extended_p
);
738 void remote_detach_1 (struct inferior
*inf
, int from_tty
);
740 char *append_resumption (char *p
, char *endp
,
741 ptid_t ptid
, int step
, gdb_signal siggnal
);
742 int remote_resume_with_vcont (ptid_t ptid
, int step
,
745 thread_info
*add_current_inferior_and_thread (const char *wait_status
);
747 ptid_t
wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
748 target_wait_flags options
);
749 ptid_t
wait_as (ptid_t ptid
, target_waitstatus
*status
,
750 target_wait_flags options
);
752 ptid_t
process_stop_reply (struct stop_reply
*stop_reply
,
753 target_waitstatus
*status
);
755 ptid_t select_thread_for_ambiguous_stop_reply
756 (const struct target_waitstatus
&status
);
758 void remote_notice_new_inferior (ptid_t currthread
, bool executing
);
760 void print_one_stopped_thread (thread_info
*thread
);
761 void process_initial_stop_replies (int from_tty
);
763 thread_info
*remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
766 void btrace_sync_conf (const btrace_config
*conf
);
768 void remote_btrace_maybe_reopen ();
770 void remove_new_fork_children (threads_listing_context
*context
);
771 void kill_new_fork_children (inferior
*inf
);
772 void discard_pending_stop_replies (struct inferior
*inf
);
773 int stop_reply_queue_length ();
775 void check_pending_events_prevent_wildcard_vcont
776 (bool *may_global_wildcard_vcont
);
778 void discard_pending_stop_replies_in_queue ();
779 struct stop_reply
*remote_notif_remove_queued_reply (ptid_t ptid
);
780 struct stop_reply
*queued_stop_reply (ptid_t ptid
);
781 int peek_stop_reply (ptid_t ptid
);
782 void remote_parse_stop_reply (const char *buf
, stop_reply
*event
);
784 void remote_stop_ns (ptid_t ptid
);
785 void remote_interrupt_as ();
786 void remote_interrupt_ns ();
788 char *remote_get_noisy_reply ();
789 int remote_query_attached (int pid
);
790 inferior
*remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
793 ptid_t
remote_current_thread (ptid_t oldpid
);
794 ptid_t
get_current_thread (const char *wait_status
);
796 void set_thread (ptid_t ptid
, int gen
);
797 void set_general_thread (ptid_t ptid
);
798 void set_continue_thread (ptid_t ptid
);
799 void set_general_process ();
801 char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
803 int remote_unpack_thread_info_response (const char *pkt
, threadref
*expectedref
,
804 gdb_ext_thread_info
*info
);
805 int remote_get_threadinfo (threadref
*threadid
, int fieldset
,
806 gdb_ext_thread_info
*info
);
808 int parse_threadlist_response (const char *pkt
, int result_limit
,
809 threadref
*original_echo
,
810 threadref
*resultlist
,
812 int remote_get_threadlist (int startflag
, threadref
*nextthread
,
813 int result_limit
, int *done
, int *result_count
,
814 threadref
*threadlist
);
816 int remote_threadlist_iterator (rmt_thread_action stepfunction
,
817 void *context
, int looplimit
);
819 int remote_get_threads_with_ql (threads_listing_context
*context
);
820 int remote_get_threads_with_qxfer (threads_listing_context
*context
);
821 int remote_get_threads_with_qthreadinfo (threads_listing_context
*context
);
823 void extended_remote_restart ();
827 void remote_check_symbols ();
829 void remote_supported_packet (const struct protocol_feature
*feature
,
830 enum packet_support support
,
831 const char *argument
);
833 void remote_query_supported ();
835 void remote_packet_size (const protocol_feature
*feature
,
836 packet_support support
, const char *value
);
838 void remote_serial_quit_handler ();
840 void remote_detach_pid (int pid
);
842 void remote_vcont_probe ();
844 void remote_resume_with_hc (ptid_t ptid
, int step
,
847 void send_interrupt_sequence ();
848 void interrupt_query ();
850 void remote_notif_get_pending_events (notif_client
*nc
);
852 int fetch_register_using_p (struct regcache
*regcache
,
854 int send_g_packet ();
855 void process_g_packet (struct regcache
*regcache
);
856 void fetch_registers_using_g (struct regcache
*regcache
);
857 int store_register_using_P (const struct regcache
*regcache
,
859 void store_registers_using_G (const struct regcache
*regcache
);
861 void set_remote_traceframe ();
863 void check_binary_download (CORE_ADDR addr
);
865 target_xfer_status
remote_write_bytes_aux (const char *header
,
867 const gdb_byte
*myaddr
,
870 ULONGEST
*xfered_len_units
,
874 target_xfer_status
remote_write_bytes (CORE_ADDR memaddr
,
875 const gdb_byte
*myaddr
, ULONGEST len
,
876 int unit_size
, ULONGEST
*xfered_len
);
878 target_xfer_status
remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
880 int unit_size
, ULONGEST
*xfered_len_units
);
882 target_xfer_status
remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
886 ULONGEST
*xfered_len
);
888 target_xfer_status
remote_read_bytes (CORE_ADDR memaddr
,
889 gdb_byte
*myaddr
, ULONGEST len
,
891 ULONGEST
*xfered_len
);
893 packet_result
remote_send_printf (const char *format
, ...)
894 ATTRIBUTE_PRINTF (2, 3);
896 target_xfer_status
remote_flash_write (ULONGEST address
,
897 ULONGEST length
, ULONGEST
*xfered_len
,
898 const gdb_byte
*data
);
900 int readchar (int timeout
);
902 void remote_serial_write (const char *str
, int len
);
904 int putpkt (const char *buf
);
905 int putpkt_binary (const char *buf
, int cnt
);
907 int putpkt (const gdb::char_vector
&buf
)
909 return putpkt (buf
.data ());
913 long read_frame (gdb::char_vector
*buf_p
);
914 void getpkt (gdb::char_vector
*buf
, int forever
);
915 int getpkt_or_notif_sane_1 (gdb::char_vector
*buf
, int forever
,
916 int expecting_notif
, int *is_notif
);
917 int getpkt_sane (gdb::char_vector
*buf
, int forever
);
918 int getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
920 int remote_vkill (int pid
);
921 void remote_kill_k ();
923 void extended_remote_disable_randomization (int val
);
924 int extended_remote_run (const std::string
&args
);
926 void send_environment_packet (const char *action
,
930 void extended_remote_environment_support ();
931 void extended_remote_set_inferior_cwd ();
933 target_xfer_status
remote_write_qxfer (const char *object_name
,
935 const gdb_byte
*writebuf
,
936 ULONGEST offset
, LONGEST len
,
937 ULONGEST
*xfered_len
,
938 struct packet_config
*packet
);
940 target_xfer_status
remote_read_qxfer (const char *object_name
,
942 gdb_byte
*readbuf
, ULONGEST offset
,
944 ULONGEST
*xfered_len
,
945 struct packet_config
*packet
);
947 void push_stop_reply (struct stop_reply
*new_event
);
949 bool vcont_r_supported ();
953 bool start_remote_1 (int from_tty
, int extended_p
);
955 /* The remote state. Don't reference this directly. Use the
956 get_remote_state method instead. */
957 remote_state m_remote_state
;
960 static const target_info extended_remote_target_info
= {
962 N_("Extended remote serial target in gdb-specific protocol"),
966 /* Set up the extended remote target by extending the standard remote
967 target and adding to it. */
969 class extended_remote_target final
: public remote_target
972 const target_info
&info () const override
973 { return extended_remote_target_info
; }
975 /* Open an extended-remote connection. */
976 static void open (const char *, int);
978 bool can_create_inferior () override
{ return true; }
979 void create_inferior (const char *, const std::string
&,
980 char **, int) override
;
982 void detach (inferior
*, int) override
;
984 bool can_attach () override
{ return true; }
985 void attach (const char *, int) override
;
987 void post_attach (int) override
;
988 bool supports_disable_randomization () override
;
991 struct stop_reply
: public notif_event
995 /* The identifier of the thread about this event */
998 /* The remote state this event is associated with. When the remote
999 connection, represented by a remote_state object, is closed,
1000 all the associated stop_reply events should be released. */
1001 struct remote_state
*rs
;
1003 struct target_waitstatus ws
;
1005 /* The architecture associated with the expedited registers. */
1008 /* Expedited registers. This makes remote debugging a bit more
1009 efficient for those targets that provide critical registers as
1010 part of their normal status mechanism (as another roundtrip to
1011 fetch them is avoided). */
1012 std::vector
<cached_reg_t
> regcache
;
1014 enum target_stop_reason stop_reason
;
1016 CORE_ADDR watch_data_address
;
1024 is_remote_target (process_stratum_target
*target
)
1026 remote_target
*rt
= dynamic_cast<remote_target
*> (target
);
1027 return rt
!= nullptr;
1030 /* Per-program-space data key. */
1031 static const struct program_space_key
<char, gdb::xfree_deleter
<char>>
1034 /* The variable registered as the control variable used by the
1035 remote exec-file commands. While the remote exec-file setting is
1036 per-program-space, the set/show machinery uses this as the
1037 location of the remote exec-file value. */
1038 static std::string remote_exec_file_var
;
1040 /* The size to align memory write packets, when practical. The protocol
1041 does not guarantee any alignment, and gdb will generate short
1042 writes and unaligned writes, but even as a best-effort attempt this
1043 can improve bulk transfers. For instance, if a write is misaligned
1044 relative to the target's data bus, the stub may need to make an extra
1045 round trip fetching data from the target. This doesn't make a
1046 huge difference, but it's easy to do, so we try to be helpful.
1048 The alignment chosen is arbitrary; usually data bus width is
1049 important here, not the possibly larger cache line size. */
1050 enum { REMOTE_ALIGN_WRITES
= 16 };
1052 /* Prototypes for local functions. */
1054 static int hexnumlen (ULONGEST num
);
1056 static int stubhex (int ch
);
1058 static int hexnumstr (char *, ULONGEST
);
1060 static int hexnumnstr (char *, ULONGEST
, int);
1062 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
1064 static int stub_unpack_int (const char *buff
, int fieldlength
);
1066 struct packet_config
;
1068 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
1070 struct cmd_list_element
*c
,
1073 static ptid_t
read_ptid (const char *buf
, const char **obuf
);
1075 static void remote_async_inferior_event_handler (gdb_client_data
);
1077 static bool remote_read_description_p (struct target_ops
*target
);
1079 static void remote_console_output (const char *msg
);
1081 static void remote_btrace_reset (remote_state
*rs
);
1083 static void remote_unpush_and_throw (remote_target
*target
);
1087 static struct cmd_list_element
*remote_cmdlist
;
1089 /* For "set remote" and "show remote". */
1091 static struct cmd_list_element
*remote_set_cmdlist
;
1092 static struct cmd_list_element
*remote_show_cmdlist
;
1094 /* Controls whether GDB is willing to use range stepping. */
1096 static bool use_range_stepping
= true;
1098 /* From the remote target's point of view, each thread is in one of these three
1100 enum class resume_state
1102 /* Not resumed - we haven't been asked to resume this thread. */
1105 /* We have been asked to resume this thread, but haven't sent a vCont action
1106 for it yet. We'll need to consider it next time commit_resume is
1108 RESUMED_PENDING_VCONT
,
1110 /* We have been asked to resume this thread, and we have sent a vCont action
1115 /* Information about a thread's pending vCont-resume. Used when a thread is in
1116 the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
1117 stores this information which is then picked up by
1118 remote_target::commit_resume to know which is the proper action for this
1119 thread to include in the vCont packet. */
1120 struct resumed_pending_vcont_info
1122 /* True if the last resume call for this thread was a step request, false
1123 if a continue request. */
1126 /* The signal specified in the last resume call for this thread. */
1130 /* Private data that we'll store in (struct thread_info)->priv. */
1131 struct remote_thread_info
: public private_thread_info
1137 /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
1138 sequence of bytes. */
1139 gdb::byte_vector thread_handle
;
1141 /* Whether the target stopped for a breakpoint/watchpoint. */
1142 enum target_stop_reason stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
1144 /* This is set to the data address of the access causing the target
1145 to stop for a watchpoint. */
1146 CORE_ADDR watch_data_address
= 0;
1148 /* Get the thread's resume state. */
1149 enum resume_state
get_resume_state () const
1151 return m_resume_state
;
1154 /* Put the thread in the NOT_RESUMED state. */
1155 void set_not_resumed ()
1157 m_resume_state
= resume_state::NOT_RESUMED
;
1160 /* Put the thread in the RESUMED_PENDING_VCONT state. */
1161 void set_resumed_pending_vcont (bool step
, gdb_signal sig
)
1163 m_resume_state
= resume_state::RESUMED_PENDING_VCONT
;
1164 m_resumed_pending_vcont_info
.step
= step
;
1165 m_resumed_pending_vcont_info
.sig
= sig
;
1168 /* Get the information this thread's pending vCont-resumption.
1170 Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
1172 const struct resumed_pending_vcont_info
&resumed_pending_vcont_info () const
1174 gdb_assert (m_resume_state
== resume_state::RESUMED_PENDING_VCONT
);
1176 return m_resumed_pending_vcont_info
;
1179 /* Put the thread in the VCONT_RESUMED state. */
1182 m_resume_state
= resume_state::RESUMED
;
1186 /* Resume state for this thread. This is used to implement vCont action
1187 coalescing (only when the target operates in non-stop mode).
1189 remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
1190 which notes that this thread must be considered in the next commit_resume
1193 remote_target::commit_resume sends a vCont packet with actions for the
1194 threads in the RESUMED_PENDING_VCONT state and moves them to the
1195 VCONT_RESUMED state.
1197 When reporting a stop to the core for a thread, that thread is moved back
1198 to the NOT_RESUMED state. */
1199 enum resume_state m_resume_state
= resume_state::NOT_RESUMED
;
1201 /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
1202 struct resumed_pending_vcont_info m_resumed_pending_vcont_info
;
1205 remote_state::remote_state ()
1210 remote_state::~remote_state ()
1212 xfree (this->last_pass_packet
);
1213 xfree (this->last_program_signals_packet
);
1214 xfree (this->finished_object
);
1215 xfree (this->finished_annex
);
1218 /* Utility: generate error from an incoming stub packet. */
1220 trace_error (char *buf
)
1223 return; /* not an error msg */
1226 case '1': /* malformed packet error */
1227 if (*++buf
== '0') /* general case: */
1228 error (_("remote.c: error in outgoing packet."));
1230 error (_("remote.c: error in outgoing packet at field #%ld."),
1231 strtol (buf
, NULL
, 16));
1233 error (_("Target returns error code '%s'."), buf
);
1237 /* Utility: wait for reply from stub, while accepting "O" packets. */
1240 remote_target::remote_get_noisy_reply ()
1242 struct remote_state
*rs
= get_remote_state ();
1244 do /* Loop on reply from remote stub. */
1248 QUIT
; /* Allow user to bail out with ^C. */
1249 getpkt (&rs
->buf
, 0);
1250 buf
= rs
->buf
.data ();
1253 else if (startswith (buf
, "qRelocInsn:"))
1256 CORE_ADDR from
, to
, org_to
;
1258 int adjusted_size
= 0;
1261 p
= buf
+ strlen ("qRelocInsn:");
1262 pp
= unpack_varlen_hex (p
, &ul
);
1264 error (_("invalid qRelocInsn packet: %s"), buf
);
1268 unpack_varlen_hex (p
, &ul
);
1275 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
1278 catch (const gdb_exception
&ex
)
1280 if (ex
.error
== MEMORY_ERROR
)
1282 /* Propagate memory errors silently back to the
1283 target. The stub may have limited the range of
1284 addresses we can write to, for example. */
1288 /* Something unexpectedly bad happened. Be verbose
1289 so we can tell what, and propagate the error back
1290 to the stub, so it doesn't get stuck waiting for
1292 exception_fprintf (gdb_stderr
, ex
,
1293 _("warning: relocating instruction: "));
1300 adjusted_size
= to
- org_to
;
1302 xsnprintf (buf
, rs
->buf
.size (), "qRelocInsn:%x", adjusted_size
);
1306 else if (buf
[0] == 'O' && buf
[1] != 'K')
1307 remote_console_output (buf
+ 1); /* 'O' message from stub */
1309 return buf
; /* Here's the actual reply. */
1314 struct remote_arch_state
*
1315 remote_state::get_remote_arch_state (struct gdbarch
*gdbarch
)
1317 remote_arch_state
*rsa
;
1319 auto it
= this->m_arch_states
.find (gdbarch
);
1320 if (it
== this->m_arch_states
.end ())
1322 auto p
= this->m_arch_states
.emplace (std::piecewise_construct
,
1323 std::forward_as_tuple (gdbarch
),
1324 std::forward_as_tuple (gdbarch
));
1325 rsa
= &p
.first
->second
;
1327 /* Make sure that the packet buffer is plenty big enough for
1328 this architecture. */
1329 if (this->buf
.size () < rsa
->remote_packet_size
)
1330 this->buf
.resize (2 * rsa
->remote_packet_size
);
1338 /* Fetch the global remote target state. */
1341 remote_target::get_remote_state ()
1343 /* Make sure that the remote architecture state has been
1344 initialized, because doing so might reallocate rs->buf. Any
1345 function which calls getpkt also needs to be mindful of changes
1346 to rs->buf, but this call limits the number of places which run
1348 m_remote_state
.get_remote_arch_state (target_gdbarch ());
1350 return &m_remote_state
;
1353 /* Fetch the remote exec-file from the current program space. */
1356 get_remote_exec_file (void)
1358 char *remote_exec_file
;
1360 remote_exec_file
= remote_pspace_data
.get (current_program_space
);
1361 if (remote_exec_file
== NULL
)
1364 return remote_exec_file
;
1367 /* Set the remote exec file for PSPACE. */
1370 set_pspace_remote_exec_file (struct program_space
*pspace
,
1371 const char *remote_exec_file
)
1373 char *old_file
= remote_pspace_data
.get (pspace
);
1376 remote_pspace_data
.set (pspace
, xstrdup (remote_exec_file
));
1379 /* The "set/show remote exec-file" set command hook. */
1382 set_remote_exec_file (const char *ignored
, int from_tty
,
1383 struct cmd_list_element
*c
)
1385 set_pspace_remote_exec_file (current_program_space
,
1386 remote_exec_file_var
.c_str ());
1389 /* The "set/show remote exec-file" show command hook. */
1392 show_remote_exec_file (struct ui_file
*file
, int from_tty
,
1393 struct cmd_list_element
*cmd
, const char *value
)
1395 fprintf_filtered (file
, "%s\n", get_remote_exec_file ());
1399 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
1401 int regnum
, num_remote_regs
, offset
;
1402 struct packet_reg
**remote_regs
;
1404 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1406 struct packet_reg
*r
= ®s
[regnum
];
1408 if (register_size (gdbarch
, regnum
) == 0)
1409 /* Do not try to fetch zero-sized (placeholder) registers. */
1412 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
1417 /* Define the g/G packet format as the contents of each register
1418 with a remote protocol number, in order of ascending protocol
1421 remote_regs
= XALLOCAVEC (struct packet_reg
*, gdbarch_num_regs (gdbarch
));
1422 for (num_remote_regs
= 0, regnum
= 0;
1423 regnum
< gdbarch_num_regs (gdbarch
);
1425 if (regs
[regnum
].pnum
!= -1)
1426 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
1428 std::sort (remote_regs
, remote_regs
+ num_remote_regs
,
1429 [] (const packet_reg
*a
, const packet_reg
*b
)
1430 { return a
->pnum
< b
->pnum
; });
1432 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
1434 remote_regs
[regnum
]->in_g_packet
= 1;
1435 remote_regs
[regnum
]->offset
= offset
;
1436 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
1442 /* Given the architecture described by GDBARCH, return the remote
1443 protocol register's number and the register's offset in the g/G
1444 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
1445 If the target does not have a mapping for REGNUM, return false,
1446 otherwise, return true. */
1449 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
1450 int *pnum
, int *poffset
)
1452 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
1454 std::vector
<packet_reg
> regs (gdbarch_num_regs (gdbarch
));
1456 map_regcache_remote_table (gdbarch
, regs
.data ());
1458 *pnum
= regs
[regnum
].pnum
;
1459 *poffset
= regs
[regnum
].offset
;
1464 remote_arch_state::remote_arch_state (struct gdbarch
*gdbarch
)
1466 /* Use the architecture to build a regnum<->pnum table, which will be
1467 1:1 unless a feature set specifies otherwise. */
1468 this->regs
.reset (new packet_reg
[gdbarch_num_regs (gdbarch
)] ());
1470 /* Record the maximum possible size of the g packet - it may turn out
1472 this->sizeof_g_packet
1473 = map_regcache_remote_table (gdbarch
, this->regs
.get ());
1475 /* Default maximum number of characters in a packet body. Many
1476 remote stubs have a hardwired buffer size of 400 bytes
1477 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
1478 as the maximum packet-size to ensure that the packet and an extra
1479 NUL character can always fit in the buffer. This stops GDB
1480 trashing stubs that try to squeeze an extra NUL into what is
1481 already a full buffer (As of 1999-12-04 that was most stubs). */
1482 this->remote_packet_size
= 400 - 1;
1484 /* This one is filled in when a ``g'' packet is received. */
1485 this->actual_register_packet_size
= 0;
1487 /* Should rsa->sizeof_g_packet needs more space than the
1488 default, adjust the size accordingly. Remember that each byte is
1489 encoded as two characters. 32 is the overhead for the packet
1490 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
1491 (``$NN:G...#NN'') is a better guess, the below has been padded a
1493 if (this->sizeof_g_packet
> ((this->remote_packet_size
- 32) / 2))
1494 this->remote_packet_size
= (this->sizeof_g_packet
* 2 + 32);
1497 /* Get a pointer to the current remote target. If not connected to a
1498 remote target, return NULL. */
1500 static remote_target
*
1501 get_current_remote_target ()
1503 target_ops
*proc_target
= current_inferior ()->process_target ();
1504 return dynamic_cast<remote_target
*> (proc_target
);
1507 /* Return the current allowed size of a remote packet. This is
1508 inferred from the current architecture, and should be used to
1509 limit the length of outgoing packets. */
1511 remote_target::get_remote_packet_size ()
1513 struct remote_state
*rs
= get_remote_state ();
1514 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1516 if (rs
->explicit_packet_size
)
1517 return rs
->explicit_packet_size
;
1519 return rsa
->remote_packet_size
;
1522 static struct packet_reg
*
1523 packet_reg_from_regnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1526 if (regnum
< 0 && regnum
>= gdbarch_num_regs (gdbarch
))
1530 struct packet_reg
*r
= &rsa
->regs
[regnum
];
1532 gdb_assert (r
->regnum
== regnum
);
1537 static struct packet_reg
*
1538 packet_reg_from_pnum (struct gdbarch
*gdbarch
, struct remote_arch_state
*rsa
,
1543 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1545 struct packet_reg
*r
= &rsa
->regs
[i
];
1547 if (r
->pnum
== pnum
)
1553 /* Allow the user to specify what sequence to send to the remote
1554 when he requests a program interruption: Although ^C is usually
1555 what remote systems expect (this is the default, here), it is
1556 sometimes preferable to send a break. On other systems such
1557 as the Linux kernel, a break followed by g, which is Magic SysRq g
1558 is required in order to interrupt the execution. */
1559 const char interrupt_sequence_control_c
[] = "Ctrl-C";
1560 const char interrupt_sequence_break
[] = "BREAK";
1561 const char interrupt_sequence_break_g
[] = "BREAK-g";
1562 static const char *const interrupt_sequence_modes
[] =
1564 interrupt_sequence_control_c
,
1565 interrupt_sequence_break
,
1566 interrupt_sequence_break_g
,
1569 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
1572 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
1573 struct cmd_list_element
*c
,
1576 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
1577 fprintf_filtered (file
,
1578 _("Send the ASCII ETX character (Ctrl-c) "
1579 "to the remote target to interrupt the "
1580 "execution of the program.\n"));
1581 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
1582 fprintf_filtered (file
,
1583 _("send a break signal to the remote target "
1584 "to interrupt the execution of the program.\n"));
1585 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
1586 fprintf_filtered (file
,
1587 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
1588 "the remote target to interrupt the execution "
1589 "of Linux kernel.\n"));
1591 internal_error (__FILE__
, __LINE__
,
1592 _("Invalid value for interrupt_sequence_mode: %s."),
1593 interrupt_sequence_mode
);
1596 /* This boolean variable specifies whether interrupt_sequence is sent
1597 to the remote target when gdb connects to it.
1598 This is mostly needed when you debug the Linux kernel: The Linux kernel
1599 expects BREAK g which is Magic SysRq g for connecting gdb. */
1600 static bool interrupt_on_connect
= false;
1602 /* This variable is used to implement the "set/show remotebreak" commands.
1603 Since these commands are now deprecated in favor of "set/show remote
1604 interrupt-sequence", it no longer has any effect on the code. */
1605 static bool remote_break
;
1608 set_remotebreak (const char *args
, int from_tty
, struct cmd_list_element
*c
)
1611 interrupt_sequence_mode
= interrupt_sequence_break
;
1613 interrupt_sequence_mode
= interrupt_sequence_control_c
;
1617 show_remotebreak (struct ui_file
*file
, int from_tty
,
1618 struct cmd_list_element
*c
,
1623 /* This variable sets the number of bits in an address that are to be
1624 sent in a memory ("M" or "m") packet. Normally, after stripping
1625 leading zeros, the entire address would be sent. This variable
1626 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
1627 initial implementation of remote.c restricted the address sent in
1628 memory packets to ``host::sizeof long'' bytes - (typically 32
1629 bits). Consequently, for 64 bit targets, the upper 32 bits of an
1630 address was never sent. Since fixing this bug may cause a break in
1631 some remote targets this variable is principally provided to
1632 facilitate backward compatibility. */
1634 static unsigned int remote_address_size
;
1637 /* User configurable variables for the number of characters in a
1638 memory read/write packet. MIN (rsa->remote_packet_size,
1639 rsa->sizeof_g_packet) is the default. Some targets need smaller
1640 values (fifo overruns, et.al.) and some users need larger values
1641 (speed up transfers). The variables ``preferred_*'' (the user
1642 request), ``current_*'' (what was actually set) and ``forced_*''
1643 (Positive - a soft limit, negative - a hard limit). */
1645 struct memory_packet_config
1652 /* The default max memory-write-packet-size, when the setting is
1653 "fixed". The 16k is historical. (It came from older GDB's using
1654 alloca for buffers and the knowledge (folklore?) that some hosts
1655 don't cope very well with large alloca calls.) */
1656 #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
1658 /* The minimum remote packet size for memory transfers. Ensures we
1659 can write at least one byte. */
1660 #define MIN_MEMORY_PACKET_SIZE 20
1662 /* Get the memory packet size, assuming it is fixed. */
1665 get_fixed_memory_packet_size (struct memory_packet_config
*config
)
1667 gdb_assert (config
->fixed_p
);
1669 if (config
->size
<= 0)
1670 return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
;
1672 return config
->size
;
1675 /* Compute the current size of a read/write packet. Since this makes
1676 use of ``actual_register_packet_size'' the computation is dynamic. */
1679 remote_target::get_memory_packet_size (struct memory_packet_config
*config
)
1681 struct remote_state
*rs
= get_remote_state ();
1682 remote_arch_state
*rsa
= rs
->get_remote_arch_state (target_gdbarch ());
1685 if (config
->fixed_p
)
1686 what_they_get
= get_fixed_memory_packet_size (config
);
1689 what_they_get
= get_remote_packet_size ();
1690 /* Limit the packet to the size specified by the user. */
1691 if (config
->size
> 0
1692 && what_they_get
> config
->size
)
1693 what_they_get
= config
->size
;
1695 /* Limit it to the size of the targets ``g'' response unless we have
1696 permission from the stub to use a larger packet size. */
1697 if (rs
->explicit_packet_size
== 0
1698 && rsa
->actual_register_packet_size
> 0
1699 && what_they_get
> rsa
->actual_register_packet_size
)
1700 what_they_get
= rsa
->actual_register_packet_size
;
1702 if (what_they_get
< MIN_MEMORY_PACKET_SIZE
)
1703 what_they_get
= MIN_MEMORY_PACKET_SIZE
;
1705 /* Make sure there is room in the global buffer for this packet
1706 (including its trailing NUL byte). */
1707 if (rs
->buf
.size () < what_they_get
+ 1)
1708 rs
->buf
.resize (2 * what_they_get
);
1710 return what_they_get
;
1713 /* Update the size of a read/write packet. If they user wants
1714 something really big then do a sanity check. */
1717 set_memory_packet_size (const char *args
, struct memory_packet_config
*config
)
1719 int fixed_p
= config
->fixed_p
;
1720 long size
= config
->size
;
1723 error (_("Argument required (integer, `fixed' or `limited')."));
1724 else if (strcmp (args
, "hard") == 0
1725 || strcmp (args
, "fixed") == 0)
1727 else if (strcmp (args
, "soft") == 0
1728 || strcmp (args
, "limit") == 0)
1734 size
= strtoul (args
, &end
, 0);
1736 error (_("Invalid %s (bad syntax)."), config
->name
);
1738 /* Instead of explicitly capping the size of a packet to or
1739 disallowing it, the user is allowed to set the size to
1740 something arbitrarily large. */
1744 if (fixed_p
&& !config
->fixed_p
)
1746 /* So that the query shows the correct value. */
1747 long query_size
= (size
<= 0
1748 ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
1751 if (! query (_("The target may not be able to correctly handle a %s\n"
1752 "of %ld bytes. Change the packet size? "),
1753 config
->name
, query_size
))
1754 error (_("Packet size not changed."));
1756 /* Update the config. */
1757 config
->fixed_p
= fixed_p
;
1758 config
->size
= size
;
1762 show_memory_packet_size (struct memory_packet_config
*config
)
1764 if (config
->size
== 0)
1765 printf_filtered (_("The %s is 0 (default). "), config
->name
);
1767 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
1768 if (config
->fixed_p
)
1769 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
1770 get_fixed_memory_packet_size (config
));
1773 remote_target
*remote
= get_current_remote_target ();
1776 printf_filtered (_("Packets are limited to %ld bytes.\n"),
1777 remote
->get_memory_packet_size (config
));
1779 puts_filtered ("The actual limit will be further reduced "
1780 "dependent on the target.\n");
1784 /* FIXME: needs to be per-remote-target. */
1785 static struct memory_packet_config memory_write_packet_config
=
1787 "memory-write-packet-size",
1791 set_memory_write_packet_size (const char *args
, int from_tty
)
1793 set_memory_packet_size (args
, &memory_write_packet_config
);
1797 show_memory_write_packet_size (const char *args
, int from_tty
)
1799 show_memory_packet_size (&memory_write_packet_config
);
1802 /* Show the number of hardware watchpoints that can be used. */
1805 show_hardware_watchpoint_limit (struct ui_file
*file
, int from_tty
,
1806 struct cmd_list_element
*c
,
1809 fprintf_filtered (file
, _("The maximum number of target hardware "
1810 "watchpoints is %s.\n"), value
);
1813 /* Show the length limit (in bytes) for hardware watchpoints. */
1816 show_hardware_watchpoint_length_limit (struct ui_file
*file
, int from_tty
,
1817 struct cmd_list_element
*c
,
1820 fprintf_filtered (file
, _("The maximum length (in bytes) of a target "
1821 "hardware watchpoint is %s.\n"), value
);
1824 /* Show the number of hardware breakpoints that can be used. */
1827 show_hardware_breakpoint_limit (struct ui_file
*file
, int from_tty
,
1828 struct cmd_list_element
*c
,
1831 fprintf_filtered (file
, _("The maximum number of target hardware "
1832 "breakpoints is %s.\n"), value
);
1835 /* Controls the maximum number of characters to display in the debug output
1836 for each remote packet. The remaining characters are omitted. */
1838 static int remote_packet_max_chars
= 512;
1840 /* Show the maximum number of characters to display for each remote packet
1841 when remote debugging is enabled. */
1844 show_remote_packet_max_chars (struct ui_file
*file
, int from_tty
,
1845 struct cmd_list_element
*c
,
1848 fprintf_filtered (file
, _("Number of remote packet characters to "
1849 "display is %s.\n"), value
);
1853 remote_target::get_memory_write_packet_size ()
1855 return get_memory_packet_size (&memory_write_packet_config
);
1858 /* FIXME: needs to be per-remote-target. */
1859 static struct memory_packet_config memory_read_packet_config
=
1861 "memory-read-packet-size",
1865 set_memory_read_packet_size (const char *args
, int from_tty
)
1867 set_memory_packet_size (args
, &memory_read_packet_config
);
1871 show_memory_read_packet_size (const char *args
, int from_tty
)
1873 show_memory_packet_size (&memory_read_packet_config
);
1877 remote_target::get_memory_read_packet_size ()
1879 long size
= get_memory_packet_size (&memory_read_packet_config
);
1881 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1882 extra buffer size argument before the memory read size can be
1883 increased beyond this. */
1884 if (size
> get_remote_packet_size ())
1885 size
= get_remote_packet_size ();
1891 struct packet_config
1896 /* If auto, GDB auto-detects support for this packet or feature,
1897 either through qSupported, or by trying the packet and looking
1898 at the response. If true, GDB assumes the target supports this
1899 packet. If false, the packet is disabled. Configs that don't
1900 have an associated command always have this set to auto. */
1901 enum auto_boolean detect
;
1903 /* The "show remote foo-packet" command created for this packet. */
1904 cmd_list_element
*show_cmd
;
1906 /* Does the target support this packet? */
1907 enum packet_support support
;
1910 static enum packet_support
packet_config_support (struct packet_config
*config
);
1911 static enum packet_support
packet_support (int packet
);
1914 show_packet_config_cmd (ui_file
*file
, struct packet_config
*config
)
1916 const char *support
= "internal-error";
1918 switch (packet_config_support (config
))
1921 support
= "enabled";
1923 case PACKET_DISABLE
:
1924 support
= "disabled";
1926 case PACKET_SUPPORT_UNKNOWN
:
1927 support
= "unknown";
1930 switch (config
->detect
)
1932 case AUTO_BOOLEAN_AUTO
:
1933 fprintf_filtered (file
,
1934 _("Support for the `%s' packet "
1935 "is auto-detected, currently %s.\n"),
1936 config
->name
, support
);
1938 case AUTO_BOOLEAN_TRUE
:
1939 case AUTO_BOOLEAN_FALSE
:
1940 fprintf_filtered (file
,
1941 _("Support for the `%s' packet is currently %s.\n"),
1942 config
->name
, support
);
1948 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1949 const char *title
, int legacy
)
1951 config
->name
= name
;
1952 config
->title
= title
;
1953 gdb::unique_xmalloc_ptr
<char> set_doc
1954 = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
1956 gdb::unique_xmalloc_ptr
<char> show_doc
1957 = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
1959 /* set/show TITLE-packet {auto,on,off} */
1960 gdb::unique_xmalloc_ptr
<char> cmd_name
= xstrprintf ("%s-packet", title
);
1961 set_show_commands cmds
1962 = add_setshow_auto_boolean_cmd (cmd_name
.release (), class_obscure
,
1963 &config
->detect
, set_doc
.get (),
1964 show_doc
.get (), NULL
, /* help_doc */
1966 show_remote_protocol_packet_cmd
,
1967 &remote_set_cmdlist
, &remote_show_cmdlist
);
1968 config
->show_cmd
= cmds
.show
;
1970 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1973 /* It's not clear who should take ownership of this string, so, for
1974 now, make it static, and give copies to each of the add_alias_cmd
1976 static gdb::unique_xmalloc_ptr
<char> legacy_name
1977 = xstrprintf ("%s-packet", name
);
1978 add_alias_cmd (legacy_name
.get (), cmds
.set
, class_obscure
, 0,
1979 &remote_set_cmdlist
);
1980 add_alias_cmd (legacy_name
.get (), cmds
.show
, class_obscure
, 0,
1981 &remote_show_cmdlist
);
1985 static enum packet_result
1986 packet_check_result (const char *buf
)
1990 /* The stub recognized the packet request. Check that the
1991 operation succeeded. */
1993 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1995 /* "Enn" - definitely an error. */
1996 return PACKET_ERROR
;
1998 /* Always treat "E." as an error. This will be used for
1999 more verbose error messages, such as E.memtypes. */
2000 if (buf
[0] == 'E' && buf
[1] == '.')
2001 return PACKET_ERROR
;
2003 /* The packet may or may not be OK. Just assume it is. */
2007 /* The stub does not support the packet. */
2008 return PACKET_UNKNOWN
;
2011 static enum packet_result
2012 packet_check_result (const gdb::char_vector
&buf
)
2014 return packet_check_result (buf
.data ());
2017 static enum packet_result
2018 packet_ok (const char *buf
, struct packet_config
*config
)
2020 enum packet_result result
;
2022 if (config
->detect
!= AUTO_BOOLEAN_TRUE
2023 && config
->support
== PACKET_DISABLE
)
2024 internal_error (__FILE__
, __LINE__
,
2025 _("packet_ok: attempt to use a disabled packet"));
2027 result
= packet_check_result (buf
);
2032 /* The stub recognized the packet request. */
2033 if (config
->support
== PACKET_SUPPORT_UNKNOWN
)
2035 remote_debug_printf ("Packet %s (%s) is supported",
2036 config
->name
, config
->title
);
2037 config
->support
= PACKET_ENABLE
;
2040 case PACKET_UNKNOWN
:
2041 /* The stub does not support the packet. */
2042 if (config
->detect
== AUTO_BOOLEAN_AUTO
2043 && config
->support
== PACKET_ENABLE
)
2045 /* If the stub previously indicated that the packet was
2046 supported then there is a protocol error. */
2047 error (_("Protocol error: %s (%s) conflicting enabled responses."),
2048 config
->name
, config
->title
);
2050 else if (config
->detect
== AUTO_BOOLEAN_TRUE
)
2052 /* The user set it wrong. */
2053 error (_("Enabled packet %s (%s) not recognized by stub"),
2054 config
->name
, config
->title
);
2057 remote_debug_printf ("Packet %s (%s) is NOT supported",
2058 config
->name
, config
->title
);
2059 config
->support
= PACKET_DISABLE
;
2066 static enum packet_result
2067 packet_ok (const gdb::char_vector
&buf
, struct packet_config
*config
)
2069 return packet_ok (buf
.data (), config
);
2086 PACKET_vFile_pwrite
,
2088 PACKET_vFile_unlink
,
2089 PACKET_vFile_readlink
,
2092 PACKET_qXfer_features
,
2093 PACKET_qXfer_exec_file
,
2094 PACKET_qXfer_libraries
,
2095 PACKET_qXfer_libraries_svr4
,
2096 PACKET_qXfer_memory_map
,
2097 PACKET_qXfer_osdata
,
2098 PACKET_qXfer_threads
,
2099 PACKET_qXfer_statictrace_read
,
2100 PACKET_qXfer_traceframe_info
,
2106 PACKET_QPassSignals
,
2107 PACKET_QCatchSyscalls
,
2108 PACKET_QProgramSignals
,
2109 PACKET_QSetWorkingDir
,
2110 PACKET_QStartupWithShell
,
2111 PACKET_QEnvironmentHexEncoded
,
2112 PACKET_QEnvironmentReset
,
2113 PACKET_QEnvironmentUnset
,
2115 PACKET_qSearch_memory
,
2118 PACKET_QStartNoAckMode
,
2120 PACKET_qXfer_siginfo_read
,
2121 PACKET_qXfer_siginfo_write
,
2124 /* Support for conditional tracepoints. */
2125 PACKET_ConditionalTracepoints
,
2127 /* Support for target-side breakpoint conditions. */
2128 PACKET_ConditionalBreakpoints
,
2130 /* Support for target-side breakpoint commands. */
2131 PACKET_BreakpointCommands
,
2133 /* Support for fast tracepoints. */
2134 PACKET_FastTracepoints
,
2136 /* Support for static tracepoints. */
2137 PACKET_StaticTracepoints
,
2139 /* Support for installing tracepoints while a trace experiment is
2141 PACKET_InstallInTrace
,
2145 PACKET_TracepointSource
,
2148 PACKET_QDisableRandomization
,
2150 PACKET_QTBuffer_size
,
2154 PACKET_qXfer_btrace
,
2156 /* Support for the QNonStop packet. */
2159 /* Support for the QThreadEvents packet. */
2160 PACKET_QThreadEvents
,
2162 /* Support for multi-process extensions. */
2163 PACKET_multiprocess_feature
,
2165 /* Support for enabling and disabling tracepoints while a trace
2166 experiment is running. */
2167 PACKET_EnableDisableTracepoints_feature
,
2169 /* Support for collecting strings using the tracenz bytecode. */
2170 PACKET_tracenz_feature
,
2172 /* Support for continuing to run a trace experiment while GDB is
2174 PACKET_DisconnectedTracing_feature
,
2176 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
2177 PACKET_augmented_libraries_svr4_read_feature
,
2179 /* Support for the qXfer:btrace-conf:read packet. */
2180 PACKET_qXfer_btrace_conf
,
2182 /* Support for the Qbtrace-conf:bts:size packet. */
2183 PACKET_Qbtrace_conf_bts_size
,
2185 /* Support for swbreak+ feature. */
2186 PACKET_swbreak_feature
,
2188 /* Support for hwbreak+ feature. */
2189 PACKET_hwbreak_feature
,
2191 /* Support for fork events. */
2192 PACKET_fork_event_feature
,
2194 /* Support for vfork events. */
2195 PACKET_vfork_event_feature
,
2197 /* Support for the Qbtrace-conf:pt:size packet. */
2198 PACKET_Qbtrace_conf_pt_size
,
2200 /* Support for exec events. */
2201 PACKET_exec_event_feature
,
2203 /* Support for query supported vCont actions. */
2204 PACKET_vContSupported
,
2206 /* Support remote CTRL-C. */
2209 /* Support TARGET_WAITKIND_NO_RESUMED. */
2212 /* Support for memory tagging, allocation tag fetch/store
2213 packets and the tag violation stop replies. */
2214 PACKET_memory_tagging_feature
,
2219 /* FIXME: needs to be per-remote-target. Ignoring this for now,
2220 assuming all remote targets are the same server (thus all support
2221 the same packets). */
2222 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
2224 /* Returns the packet's corresponding "set remote foo-packet" command
2225 state. See struct packet_config for more details. */
2227 static enum auto_boolean
2228 packet_set_cmd_state (int packet
)
2230 return remote_protocol_packets
[packet
].detect
;
2233 /* Returns whether a given packet or feature is supported. This takes
2234 into account the state of the corresponding "set remote foo-packet"
2235 command, which may be used to bypass auto-detection. */
2237 static enum packet_support
2238 packet_config_support (struct packet_config
*config
)
2240 switch (config
->detect
)
2242 case AUTO_BOOLEAN_TRUE
:
2243 return PACKET_ENABLE
;
2244 case AUTO_BOOLEAN_FALSE
:
2245 return PACKET_DISABLE
;
2246 case AUTO_BOOLEAN_AUTO
:
2247 return config
->support
;
2249 gdb_assert_not_reached ("bad switch");
2253 /* Same as packet_config_support, but takes the packet's enum value as
2256 static enum packet_support
2257 packet_support (int packet
)
2259 struct packet_config
*config
= &remote_protocol_packets
[packet
];
2261 return packet_config_support (config
);
2265 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
2266 struct cmd_list_element
*c
,
2269 struct packet_config
*packet
;
2270 gdb_assert (c
->var
.has_value ());
2272 for (packet
= remote_protocol_packets
;
2273 packet
< &remote_protocol_packets
[PACKET_MAX
];
2276 if (c
== packet
->show_cmd
)
2278 show_packet_config_cmd (file
, packet
);
2282 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
2286 /* Should we try one of the 'Z' requests? */
2290 Z_PACKET_SOFTWARE_BP
,
2291 Z_PACKET_HARDWARE_BP
,
2298 /* For compatibility with older distributions. Provide a ``set remote
2299 Z-packet ...'' command that updates all the Z packet types. */
2301 static enum auto_boolean remote_Z_packet_detect
;
2304 set_remote_protocol_Z_packet_cmd (const char *args
, int from_tty
,
2305 struct cmd_list_element
*c
)
2309 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2310 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
2314 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
2315 struct cmd_list_element
*c
,
2320 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2322 show_packet_config_cmd (file
, &remote_protocol_packets
[PACKET_Z0
+ i
]);
2326 /* Returns true if the multi-process extensions are in effect. */
2329 remote_multi_process_p (struct remote_state
*rs
)
2331 return packet_support (PACKET_multiprocess_feature
) == PACKET_ENABLE
;
2334 /* Returns true if fork events are supported. */
2337 remote_fork_event_p (struct remote_state
*rs
)
2339 return packet_support (PACKET_fork_event_feature
) == PACKET_ENABLE
;
2342 /* Returns true if vfork events are supported. */
2345 remote_vfork_event_p (struct remote_state
*rs
)
2347 return packet_support (PACKET_vfork_event_feature
) == PACKET_ENABLE
;
2350 /* Returns true if exec events are supported. */
2353 remote_exec_event_p (struct remote_state
*rs
)
2355 return packet_support (PACKET_exec_event_feature
) == PACKET_ENABLE
;
2358 /* Returns true if memory tagging is supported, false otherwise. */
2361 remote_memory_tagging_p ()
2363 return packet_support (PACKET_memory_tagging_feature
) == PACKET_ENABLE
;
2366 /* Insert fork catchpoint target routine. If fork events are enabled
2367 then return success, nothing more to do. */
2370 remote_target::insert_fork_catchpoint (int pid
)
2372 struct remote_state
*rs
= get_remote_state ();
2374 return !remote_fork_event_p (rs
);
2377 /* Remove fork catchpoint target routine. Nothing to do, just
2381 remote_target::remove_fork_catchpoint (int pid
)
2386 /* Insert vfork catchpoint target routine. If vfork events are enabled
2387 then return success, nothing more to do. */
2390 remote_target::insert_vfork_catchpoint (int pid
)
2392 struct remote_state
*rs
= get_remote_state ();
2394 return !remote_vfork_event_p (rs
);
2397 /* Remove vfork catchpoint target routine. Nothing to do, just
2401 remote_target::remove_vfork_catchpoint (int pid
)
2406 /* Insert exec catchpoint target routine. If exec events are
2407 enabled, just return success. */
2410 remote_target::insert_exec_catchpoint (int pid
)
2412 struct remote_state
*rs
= get_remote_state ();
2414 return !remote_exec_event_p (rs
);
2417 /* Remove exec catchpoint target routine. Nothing to do, just
2421 remote_target::remove_exec_catchpoint (int pid
)
2428 /* Take advantage of the fact that the TID field is not used, to tag
2429 special ptids with it set to != 0. */
2430 static const ptid_t
magic_null_ptid (42000, -1, 1);
2431 static const ptid_t
not_sent_ptid (42000, -2, 1);
2432 static const ptid_t
any_thread_ptid (42000, 0, 1);
2434 /* Find out if the stub attached to PID (and hence GDB should offer to
2435 detach instead of killing it when bailing out). */
2438 remote_target::remote_query_attached (int pid
)
2440 struct remote_state
*rs
= get_remote_state ();
2441 size_t size
= get_remote_packet_size ();
2443 if (packet_support (PACKET_qAttached
) == PACKET_DISABLE
)
2446 if (remote_multi_process_p (rs
))
2447 xsnprintf (rs
->buf
.data (), size
, "qAttached:%x", pid
);
2449 xsnprintf (rs
->buf
.data (), size
, "qAttached");
2452 getpkt (&rs
->buf
, 0);
2454 switch (packet_ok (rs
->buf
,
2455 &remote_protocol_packets
[PACKET_qAttached
]))
2458 if (strcmp (rs
->buf
.data (), "1") == 0)
2462 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
2464 case PACKET_UNKNOWN
:
2471 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
2472 has been invented by GDB, instead of reported by the target. Since
2473 we can be connected to a remote system before before knowing about
2474 any inferior, mark the target with execution when we find the first
2475 inferior. If ATTACHED is 1, then we had just attached to this
2476 inferior. If it is 0, then we just created this inferior. If it
2477 is -1, then try querying the remote stub to find out if it had
2478 attached to the inferior or not. If TRY_OPEN_EXEC is true then
2479 attempt to open this inferior's executable as the main executable
2480 if no main executable is open already. */
2483 remote_target::remote_add_inferior (bool fake_pid_p
, int pid
, int attached
,
2486 struct inferior
*inf
;
2488 /* Check whether this process we're learning about is to be
2489 considered attached, or if is to be considered to have been
2490 spawned by the stub. */
2492 attached
= remote_query_attached (pid
);
2494 if (gdbarch_has_global_solist (target_gdbarch ()))
2496 /* If the target shares code across all inferiors, then every
2497 attach adds a new inferior. */
2498 inf
= add_inferior (pid
);
2500 /* ... and every inferior is bound to the same program space.
2501 However, each inferior may still have its own address
2503 inf
->aspace
= maybe_new_address_space ();
2504 inf
->pspace
= current_program_space
;
2508 /* In the traditional debugging scenario, there's a 1-1 match
2509 between program/address spaces. We simply bind the inferior
2510 to the program space's address space. */
2511 inf
= current_inferior ();
2513 /* However, if the current inferior is already bound to a
2514 process, find some other empty inferior. */
2518 for (inferior
*it
: all_inferiors ())
2527 /* Since all inferiors were already bound to a process, add
2529 inf
= add_inferior_with_spaces ();
2531 switch_to_inferior_no_thread (inf
);
2532 inf
->push_target (this);
2533 inferior_appeared (inf
, pid
);
2536 inf
->attach_flag
= attached
;
2537 inf
->fake_pid_p
= fake_pid_p
;
2539 /* If no main executable is currently open then attempt to
2540 open the file that was executed to create this inferior. */
2541 if (try_open_exec
&& get_exec_file (0) == NULL
)
2542 exec_file_locate_attach (pid
, 0, 1);
2544 /* Check for exec file mismatch, and let the user solve it. */
2545 validate_exec_file (1);
2550 static remote_thread_info
*get_remote_thread_info (thread_info
*thread
);
2551 static remote_thread_info
*get_remote_thread_info (remote_target
*target
,
2554 /* Add thread PTID to GDB's thread list. Tag it as executing/running
2555 according to EXECUTING and RUNNING respectively. If SILENT_P (or the
2556 remote_state::starting_up flag) is true then the new thread is added
2557 silently, otherwise the new thread will be announced to the user. */
2560 remote_target::remote_add_thread (ptid_t ptid
, bool running
, bool executing
,
2563 struct remote_state
*rs
= get_remote_state ();
2564 struct thread_info
*thread
;
2566 /* GDB historically didn't pull threads in the initial connection
2567 setup. If the remote target doesn't even have a concept of
2568 threads (e.g., a bare-metal target), even if internally we
2569 consider that a single-threaded target, mentioning a new thread
2570 might be confusing to the user. Be silent then, preserving the
2571 age old behavior. */
2572 if (rs
->starting_up
|| silent_p
)
2573 thread
= add_thread_silent (this, ptid
);
2575 thread
= add_thread (this, ptid
);
2577 /* We start by assuming threads are resumed. That state then gets updated
2578 when we process a matching stop reply. */
2579 get_remote_thread_info (thread
)->set_resumed ();
2581 set_executing (this, ptid
, executing
);
2582 set_running (this, ptid
, running
);
2587 /* Come here when we learn about a thread id from the remote target.
2588 It may be the first time we hear about such thread, so take the
2589 opportunity to add it to GDB's thread list. In case this is the
2590 first time we're noticing its corresponding inferior, add it to
2591 GDB's inferior list as well. EXECUTING indicates whether the
2592 thread is (internally) executing or stopped. */
2595 remote_target::remote_notice_new_inferior (ptid_t currthread
, bool executing
)
2597 /* In non-stop mode, we assume new found threads are (externally)
2598 running until proven otherwise with a stop reply. In all-stop,
2599 we can only get here if all threads are stopped. */
2600 bool running
= target_is_non_stop_p ();
2602 /* If this is a new thread, add it to GDB's thread list.
2603 If we leave it up to WFI to do this, bad things will happen. */
2605 thread_info
*tp
= find_thread_ptid (this, currthread
);
2606 if (tp
!= NULL
&& tp
->state
== THREAD_EXITED
)
2608 /* We're seeing an event on a thread id we knew had exited.
2609 This has to be a new thread reusing the old id. Add it. */
2610 remote_add_thread (currthread
, running
, executing
, false);
2614 if (!in_thread_list (this, currthread
))
2616 struct inferior
*inf
= NULL
;
2617 int pid
= currthread
.pid ();
2619 if (inferior_ptid
.is_pid ()
2620 && pid
== inferior_ptid
.pid ())
2622 /* inferior_ptid has no thread member yet. This can happen
2623 with the vAttach -> remote_wait,"TAAthread:" path if the
2624 stub doesn't support qC. This is the first stop reported
2625 after an attach, so this is the main thread. Update the
2626 ptid in the thread list. */
2627 if (in_thread_list (this, ptid_t (pid
)))
2628 thread_change_ptid (this, inferior_ptid
, currthread
);
2632 = remote_add_thread (currthread
, running
, executing
, false);
2633 switch_to_thread (thr
);
2638 if (magic_null_ptid
== inferior_ptid
)
2640 /* inferior_ptid is not set yet. This can happen with the
2641 vRun -> remote_wait,"TAAthread:" path if the stub
2642 doesn't support qC. This is the first stop reported
2643 after an attach, so this is the main thread. Update the
2644 ptid in the thread list. */
2645 thread_change_ptid (this, inferior_ptid
, currthread
);
2649 /* When connecting to a target remote, or to a target
2650 extended-remote which already was debugging an inferior, we
2651 may not know about it yet. Add it before adding its child
2652 thread, so notifications are emitted in a sensible order. */
2653 if (find_inferior_pid (this, currthread
.pid ()) == NULL
)
2655 struct remote_state
*rs
= get_remote_state ();
2656 bool fake_pid_p
= !remote_multi_process_p (rs
);
2658 inf
= remote_add_inferior (fake_pid_p
,
2659 currthread
.pid (), -1, 1);
2662 /* This is really a new thread. Add it. */
2663 thread_info
*new_thr
2664 = remote_add_thread (currthread
, running
, executing
, false);
2666 /* If we found a new inferior, let the common code do whatever
2667 it needs to with it (e.g., read shared libraries, insert
2668 breakpoints), unless we're just setting up an all-stop
2672 struct remote_state
*rs
= get_remote_state ();
2674 if (!rs
->starting_up
)
2675 notice_new_inferior (new_thr
, executing
, 0);
2680 /* Return THREAD's private thread data, creating it if necessary. */
2682 static remote_thread_info
*
2683 get_remote_thread_info (thread_info
*thread
)
2685 gdb_assert (thread
!= NULL
);
2687 if (thread
->priv
== NULL
)
2688 thread
->priv
.reset (new remote_thread_info
);
2690 return static_cast<remote_thread_info
*> (thread
->priv
.get ());
2693 /* Return PTID's private thread data, creating it if necessary. */
2695 static remote_thread_info
*
2696 get_remote_thread_info (remote_target
*target
, ptid_t ptid
)
2698 thread_info
*thr
= find_thread_ptid (target
, ptid
);
2699 return get_remote_thread_info (thr
);
2702 /* Call this function as a result of
2703 1) A halt indication (T packet) containing a thread id
2704 2) A direct query of currthread
2705 3) Successful execution of set thread */
2708 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
2710 rs
->general_thread
= currthread
;
2713 /* If 'QPassSignals' is supported, tell the remote stub what signals
2714 it can simply pass through to the inferior without reporting. */
2717 remote_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2719 if (packet_support (PACKET_QPassSignals
) != PACKET_DISABLE
)
2721 char *pass_packet
, *p
;
2723 struct remote_state
*rs
= get_remote_state ();
2725 gdb_assert (pass_signals
.size () < 256);
2726 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2728 if (pass_signals
[i
])
2731 pass_packet
= (char *) xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
2732 strcpy (pass_packet
, "QPassSignals:");
2733 p
= pass_packet
+ strlen (pass_packet
);
2734 for (size_t i
= 0; i
< pass_signals
.size (); i
++)
2736 if (pass_signals
[i
])
2739 *p
++ = tohex (i
>> 4);
2740 *p
++ = tohex (i
& 15);
2749 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
2751 putpkt (pass_packet
);
2752 getpkt (&rs
->buf
, 0);
2753 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
2754 xfree (rs
->last_pass_packet
);
2755 rs
->last_pass_packet
= pass_packet
;
2758 xfree (pass_packet
);
2762 /* If 'QCatchSyscalls' is supported, tell the remote stub
2763 to report syscalls to GDB. */
2766 remote_target::set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
2767 gdb::array_view
<const int> syscall_counts
)
2769 const char *catch_packet
;
2770 enum packet_result result
;
2773 if (packet_support (PACKET_QCatchSyscalls
) == PACKET_DISABLE
)
2775 /* Not supported. */
2779 if (needed
&& any_count
== 0)
2781 /* Count how many syscalls are to be caught. */
2782 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2784 if (syscall_counts
[i
] != 0)
2789 remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
2790 pid
, needed
, any_count
, n_sysno
);
2792 std::string built_packet
;
2795 /* Prepare a packet with the sysno list, assuming max 8+1
2796 characters for a sysno. If the resulting packet size is too
2797 big, fallback on the non-selective packet. */
2798 const int maxpktsz
= strlen ("QCatchSyscalls:1") + n_sysno
* 9 + 1;
2799 built_packet
.reserve (maxpktsz
);
2800 built_packet
= "QCatchSyscalls:1";
2803 /* Add in each syscall to be caught. */
2804 for (size_t i
= 0; i
< syscall_counts
.size (); i
++)
2806 if (syscall_counts
[i
] != 0)
2807 string_appendf (built_packet
, ";%zx", i
);
2810 if (built_packet
.size () > get_remote_packet_size ())
2812 /* catch_packet too big. Fallback to less efficient
2813 non selective mode, with GDB doing the filtering. */
2814 catch_packet
= "QCatchSyscalls:1";
2817 catch_packet
= built_packet
.c_str ();
2820 catch_packet
= "QCatchSyscalls:0";
2822 struct remote_state
*rs
= get_remote_state ();
2824 putpkt (catch_packet
);
2825 getpkt (&rs
->buf
, 0);
2826 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QCatchSyscalls
]);
2827 if (result
== PACKET_OK
)
2833 /* If 'QProgramSignals' is supported, tell the remote stub what
2834 signals it should pass through to the inferior when detaching. */
2837 remote_target::program_signals (gdb::array_view
<const unsigned char> signals
)
2839 if (packet_support (PACKET_QProgramSignals
) != PACKET_DISABLE
)
2843 struct remote_state
*rs
= get_remote_state ();
2845 gdb_assert (signals
.size () < 256);
2846 for (size_t i
= 0; i
< signals
.size (); i
++)
2851 packet
= (char *) xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
2852 strcpy (packet
, "QProgramSignals:");
2853 p
= packet
+ strlen (packet
);
2854 for (size_t i
= 0; i
< signals
.size (); i
++)
2856 if (signal_pass_state (i
))
2859 *p
++ = tohex (i
>> 4);
2860 *p
++ = tohex (i
& 15);
2869 if (!rs
->last_program_signals_packet
2870 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
2873 getpkt (&rs
->buf
, 0);
2874 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QProgramSignals
]);
2875 xfree (rs
->last_program_signals_packet
);
2876 rs
->last_program_signals_packet
= packet
;
2883 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
2884 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
2885 thread. If GEN is set, set the general thread, if not, then set
2886 the step/continue thread. */
2888 remote_target::set_thread (ptid_t ptid
, int gen
)
2890 struct remote_state
*rs
= get_remote_state ();
2891 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
2892 char *buf
= rs
->buf
.data ();
2893 char *endbuf
= buf
+ get_remote_packet_size ();
2899 *buf
++ = gen
? 'g' : 'c';
2900 if (ptid
== magic_null_ptid
)
2901 xsnprintf (buf
, endbuf
- buf
, "0");
2902 else if (ptid
== any_thread_ptid
)
2903 xsnprintf (buf
, endbuf
- buf
, "0");
2904 else if (ptid
== minus_one_ptid
)
2905 xsnprintf (buf
, endbuf
- buf
, "-1");
2907 write_ptid (buf
, endbuf
, ptid
);
2909 getpkt (&rs
->buf
, 0);
2911 rs
->general_thread
= ptid
;
2913 rs
->continue_thread
= ptid
;
2917 remote_target::set_general_thread (ptid_t ptid
)
2919 set_thread (ptid
, 1);
2923 remote_target::set_continue_thread (ptid_t ptid
)
2925 set_thread (ptid
, 0);
2928 /* Change the remote current process. Which thread within the process
2929 ends up selected isn't important, as long as it is the same process
2930 as what INFERIOR_PTID points to.
2932 This comes from that fact that there is no explicit notion of
2933 "selected process" in the protocol. The selected process for
2934 general operations is the process the selected general thread
2938 remote_target::set_general_process ()
2940 struct remote_state
*rs
= get_remote_state ();
2942 /* If the remote can't handle multiple processes, don't bother. */
2943 if (!remote_multi_process_p (rs
))
2946 /* We only need to change the remote current thread if it's pointing
2947 at some other process. */
2948 if (rs
->general_thread
.pid () != inferior_ptid
.pid ())
2949 set_general_thread (inferior_ptid
);
2953 /* Return nonzero if this is the main thread that we made up ourselves
2954 to model non-threaded targets as single-threaded. */
2957 remote_thread_always_alive (ptid_t ptid
)
2959 if (ptid
== magic_null_ptid
)
2960 /* The main thread is always alive. */
2963 if (ptid
.pid () != 0 && ptid
.lwp () == 0)
2964 /* The main thread is always alive. This can happen after a
2965 vAttach, if the remote side doesn't support
2972 /* Return nonzero if the thread PTID is still alive on the remote
2976 remote_target::thread_alive (ptid_t ptid
)
2978 struct remote_state
*rs
= get_remote_state ();
2981 /* Check if this is a thread that we made up ourselves to model
2982 non-threaded targets as single-threaded. */
2983 if (remote_thread_always_alive (ptid
))
2986 p
= rs
->buf
.data ();
2987 endp
= p
+ get_remote_packet_size ();
2990 write_ptid (p
, endp
, ptid
);
2993 getpkt (&rs
->buf
, 0);
2994 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
2997 /* Return a pointer to a thread name if we know it and NULL otherwise.
2998 The thread_info object owns the memory for the name. */
3001 remote_target::thread_name (struct thread_info
*info
)
3003 if (info
->priv
!= NULL
)
3005 const std::string
&name
= get_remote_thread_info (info
)->name
;
3006 return !name
.empty () ? name
.c_str () : NULL
;
3012 /* About these extended threadlist and threadinfo packets. They are
3013 variable length packets but, the fields within them are often fixed
3014 length. They are redundant enough to send over UDP as is the
3015 remote protocol in general. There is a matching unit test module
3018 /* WARNING: This threadref data structure comes from the remote O.S.,
3019 libstub protocol encoding, and remote.c. It is not particularly
3022 /* Right now, the internal structure is int. We want it to be bigger.
3023 Plan to fix this. */
3025 typedef int gdb_threadref
; /* Internal GDB thread reference. */
3027 /* gdb_ext_thread_info is an internal GDB data structure which is
3028 equivalent to the reply of the remote threadinfo packet. */
3030 struct gdb_ext_thread_info
3032 threadref threadid
; /* External form of thread reference. */
3033 int active
; /* Has state interesting to GDB?
3035 char display
[256]; /* Brief state display, name,
3036 blocked/suspended. */
3037 char shortname
[32]; /* To be used to name threads. */
3038 char more_display
[256]; /* Long info, statistics, queue depth,
3042 /* The volume of remote transfers can be limited by submitting
3043 a mask containing bits specifying the desired information.
3044 Use a union of these values as the 'selection' parameter to
3045 get_thread_info. FIXME: Make these TAG names more thread specific. */
3047 #define TAG_THREADID 1
3048 #define TAG_EXISTS 2
3049 #define TAG_DISPLAY 4
3050 #define TAG_THREADNAME 8
3051 #define TAG_MOREDISPLAY 16
3053 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
3055 static const char *unpack_nibble (const char *buf
, int *val
);
3057 static const char *unpack_byte (const char *buf
, int *value
);
3059 static char *pack_int (char *buf
, int value
);
3061 static const char *unpack_int (const char *buf
, int *value
);
3063 static const char *unpack_string (const char *src
, char *dest
, int length
);
3065 static char *pack_threadid (char *pkt
, threadref
*id
);
3067 static const char *unpack_threadid (const char *inbuf
, threadref
*id
);
3069 void int_to_threadref (threadref
*id
, int value
);
3071 static int threadref_to_int (threadref
*ref
);
3073 static void copy_threadref (threadref
*dest
, threadref
*src
);
3075 static int threadmatch (threadref
*dest
, threadref
*src
);
3077 static char *pack_threadinfo_request (char *pkt
, int mode
,
3080 static char *pack_threadlist_request (char *pkt
, int startflag
,
3082 threadref
*nextthread
);
3084 static int remote_newthread_step (threadref
*ref
, void *context
);
3087 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
3088 buffer we're allowed to write to. Returns
3089 BUF+CHARACTERS_WRITTEN. */
3092 remote_target::write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
3095 struct remote_state
*rs
= get_remote_state ();
3097 if (remote_multi_process_p (rs
))
3101 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
3103 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
3107 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
3109 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
3114 /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
3115 last parsed char. Returns null_ptid if no thread id is found, and
3116 throws an error if the thread id has an invalid format. */
3119 read_ptid (const char *buf
, const char **obuf
)
3121 const char *p
= buf
;
3123 ULONGEST pid
= 0, tid
= 0;
3127 /* Multi-process ptid. */
3128 pp
= unpack_varlen_hex (p
+ 1, &pid
);
3130 error (_("invalid remote ptid: %s"), p
);
3133 pp
= unpack_varlen_hex (p
+ 1, &tid
);
3136 return ptid_t (pid
, tid
);
3139 /* No multi-process. Just a tid. */
3140 pp
= unpack_varlen_hex (p
, &tid
);
3142 /* Return null_ptid when no thread id is found. */
3150 /* Since the stub is not sending a process id, then default to
3151 what's in inferior_ptid, unless it's null at this point. If so,
3152 then since there's no way to know the pid of the reported
3153 threads, use the magic number. */
3154 if (inferior_ptid
== null_ptid
)
3155 pid
= magic_null_ptid
.pid ();
3157 pid
= inferior_ptid
.pid ();
3161 return ptid_t (pid
, tid
);
3167 if (ch
>= 'a' && ch
<= 'f')
3168 return ch
- 'a' + 10;
3169 if (ch
>= '0' && ch
<= '9')
3171 if (ch
>= 'A' && ch
<= 'F')
3172 return ch
- 'A' + 10;
3177 stub_unpack_int (const char *buff
, int fieldlength
)
3184 nibble
= stubhex (*buff
++);
3188 retval
= retval
<< 4;
3194 unpack_nibble (const char *buf
, int *val
)
3196 *val
= fromhex (*buf
++);
3201 unpack_byte (const char *buf
, int *value
)
3203 *value
= stub_unpack_int (buf
, 2);
3208 pack_int (char *buf
, int value
)
3210 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
3211 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
3212 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
3213 buf
= pack_hex_byte (buf
, (value
& 0xff));
3218 unpack_int (const char *buf
, int *value
)
3220 *value
= stub_unpack_int (buf
, 8);
3224 #if 0 /* Currently unused, uncomment when needed. */
3225 static char *pack_string (char *pkt
, char *string
);
3228 pack_string (char *pkt
, char *string
)
3233 len
= strlen (string
);
3235 len
= 200; /* Bigger than most GDB packets, junk??? */
3236 pkt
= pack_hex_byte (pkt
, len
);
3240 if ((ch
== '\0') || (ch
== '#'))
3241 ch
= '*'; /* Protect encapsulation. */
3246 #endif /* 0 (unused) */
3249 unpack_string (const char *src
, char *dest
, int length
)
3258 pack_threadid (char *pkt
, threadref
*id
)
3261 unsigned char *altid
;
3263 altid
= (unsigned char *) id
;
3264 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
3266 pkt
= pack_hex_byte (pkt
, *altid
++);
3272 unpack_threadid (const char *inbuf
, threadref
*id
)
3275 const char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
3278 altref
= (char *) id
;
3280 while (inbuf
< limit
)
3282 x
= stubhex (*inbuf
++);
3283 y
= stubhex (*inbuf
++);
3284 *altref
++ = (x
<< 4) | y
;
3289 /* Externally, threadrefs are 64 bits but internally, they are still
3290 ints. This is due to a mismatch of specifications. We would like
3291 to use 64bit thread references internally. This is an adapter
3295 int_to_threadref (threadref
*id
, int value
)
3297 unsigned char *scan
;
3299 scan
= (unsigned char *) id
;
3305 *scan
++ = (value
>> 24) & 0xff;
3306 *scan
++ = (value
>> 16) & 0xff;
3307 *scan
++ = (value
>> 8) & 0xff;
3308 *scan
++ = (value
& 0xff);
3312 threadref_to_int (threadref
*ref
)
3315 unsigned char *scan
;
3321 value
= (value
<< 8) | ((*scan
++) & 0xff);
3326 copy_threadref (threadref
*dest
, threadref
*src
)
3329 unsigned char *csrc
, *cdest
;
3331 csrc
= (unsigned char *) src
;
3332 cdest
= (unsigned char *) dest
;
3339 threadmatch (threadref
*dest
, threadref
*src
)
3341 /* Things are broken right now, so just assume we got a match. */
3343 unsigned char *srcp
, *destp
;
3345 srcp
= (char *) src
;
3346 destp
= (char *) dest
;
3350 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
3357 threadid:1, # always request threadid
3364 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
3367 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
3369 *pkt
++ = 'q'; /* Info Query */
3370 *pkt
++ = 'P'; /* process or thread info */
3371 pkt
= pack_int (pkt
, mode
); /* mode */
3372 pkt
= pack_threadid (pkt
, id
); /* threadid */
3373 *pkt
= '\0'; /* terminate */
3377 /* These values tag the fields in a thread info response packet. */
3378 /* Tagging the fields allows us to request specific fields and to
3379 add more fields as time goes by. */
3381 #define TAG_THREADID 1 /* Echo the thread identifier. */
3382 #define TAG_EXISTS 2 /* Is this process defined enough to
3383 fetch registers and its stack? */
3384 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
3385 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
3386 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
3390 remote_target::remote_unpack_thread_info_response (const char *pkt
,
3391 threadref
*expectedref
,
3392 gdb_ext_thread_info
*info
)
3394 struct remote_state
*rs
= get_remote_state ();
3398 const char *limit
= pkt
+ rs
->buf
.size (); /* Plausible parsing limit. */
3401 /* info->threadid = 0; FIXME: implement zero_threadref. */
3403 info
->display
[0] = '\0';
3404 info
->shortname
[0] = '\0';
3405 info
->more_display
[0] = '\0';
3407 /* Assume the characters indicating the packet type have been
3409 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
3410 pkt
= unpack_threadid (pkt
, &ref
);
3413 warning (_("Incomplete response to threadinfo request."));
3414 if (!threadmatch (&ref
, expectedref
))
3415 { /* This is an answer to a different request. */
3416 warning (_("ERROR RMT Thread info mismatch."));
3419 copy_threadref (&info
->threadid
, &ref
);
3421 /* Loop on tagged fields , try to bail if something goes wrong. */
3423 /* Packets are terminated with nulls. */
3424 while ((pkt
< limit
) && mask
&& *pkt
)
3426 pkt
= unpack_int (pkt
, &tag
); /* tag */
3427 pkt
= unpack_byte (pkt
, &length
); /* length */
3428 if (!(tag
& mask
)) /* Tags out of synch with mask. */
3430 warning (_("ERROR RMT: threadinfo tag mismatch."));
3434 if (tag
== TAG_THREADID
)
3438 warning (_("ERROR RMT: length of threadid is not 16."));
3442 pkt
= unpack_threadid (pkt
, &ref
);
3443 mask
= mask
& ~TAG_THREADID
;
3446 if (tag
== TAG_EXISTS
)
3448 info
->active
= stub_unpack_int (pkt
, length
);
3450 mask
= mask
& ~(TAG_EXISTS
);
3453 warning (_("ERROR RMT: 'exists' length too long."));
3459 if (tag
== TAG_THREADNAME
)
3461 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
3462 mask
= mask
& ~TAG_THREADNAME
;
3465 if (tag
== TAG_DISPLAY
)
3467 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
3468 mask
= mask
& ~TAG_DISPLAY
;
3471 if (tag
== TAG_MOREDISPLAY
)
3473 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
3474 mask
= mask
& ~TAG_MOREDISPLAY
;
3477 warning (_("ERROR RMT: unknown thread info tag."));
3478 break; /* Not a tag we know about. */
3484 remote_target::remote_get_threadinfo (threadref
*threadid
,
3486 gdb_ext_thread_info
*info
)
3488 struct remote_state
*rs
= get_remote_state ();
3491 pack_threadinfo_request (rs
->buf
.data (), fieldset
, threadid
);
3493 getpkt (&rs
->buf
, 0);
3495 if (rs
->buf
[0] == '\0')
3498 result
= remote_unpack_thread_info_response (&rs
->buf
[2],
3503 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
3506 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
3507 threadref
*nextthread
)
3509 *pkt
++ = 'q'; /* info query packet */
3510 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
3511 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
3512 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
3513 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
3518 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
3521 remote_target::parse_threadlist_response (const char *pkt
, int result_limit
,
3522 threadref
*original_echo
,
3523 threadref
*resultlist
,
3526 struct remote_state
*rs
= get_remote_state ();
3527 int count
, resultcount
, done
;
3530 /* Assume the 'q' and 'M chars have been stripped. */
3531 const char *limit
= pkt
+ (rs
->buf
.size () - BUF_THREAD_ID_SIZE
);
3532 /* done parse past here */
3533 pkt
= unpack_byte (pkt
, &count
); /* count field */
3534 pkt
= unpack_nibble (pkt
, &done
);
3535 /* The first threadid is the argument threadid. */
3536 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
3537 while ((count
-- > 0) && (pkt
< limit
))
3539 pkt
= unpack_threadid (pkt
, resultlist
++);
3540 if (resultcount
++ >= result_limit
)
3548 /* Fetch the next batch of threads from the remote. Returns -1 if the
3549 qL packet is not supported, 0 on error and 1 on success. */
3552 remote_target::remote_get_threadlist (int startflag
, threadref
*nextthread
,
3553 int result_limit
, int *done
, int *result_count
,
3554 threadref
*threadlist
)
3556 struct remote_state
*rs
= get_remote_state ();
3559 /* Truncate result limit to be smaller than the packet size. */
3560 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
3561 >= get_remote_packet_size ())
3562 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
3564 pack_threadlist_request (rs
->buf
.data (), startflag
, result_limit
,
3567 getpkt (&rs
->buf
, 0);
3568 if (rs
->buf
[0] == '\0')
3570 /* Packet not supported. */
3575 parse_threadlist_response (&rs
->buf
[2], result_limit
,
3576 &rs
->echo_nextthread
, threadlist
, done
);
3578 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
3580 /* FIXME: This is a good reason to drop the packet. */
3581 /* Possibly, there is a duplicate response. */
3583 retransmit immediatly - race conditions
3584 retransmit after timeout - yes
3586 wait for packet, then exit
3588 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
3589 return 0; /* I choose simply exiting. */
3591 if (*result_count
<= 0)
3595 warning (_("RMT ERROR : failed to get remote thread list."));
3598 return result
; /* break; */
3600 if (*result_count
> result_limit
)
3603 warning (_("RMT ERROR: threadlist response longer than requested."));
3609 /* Fetch the list of remote threads, with the qL packet, and call
3610 STEPFUNCTION for each thread found. Stops iterating and returns 1
3611 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
3612 STEPFUNCTION returns false. If the packet is not supported,
3616 remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction
,
3617 void *context
, int looplimit
)
3619 struct remote_state
*rs
= get_remote_state ();
3620 int done
, i
, result_count
;
3628 if (loopcount
++ > looplimit
)
3631 warning (_("Remote fetch threadlist -infinite loop-."));
3634 result
= remote_get_threadlist (startflag
, &rs
->nextthread
,
3635 MAXTHREADLISTRESULTS
,
3636 &done
, &result_count
,
3637 rs
->resultthreadlist
);
3640 /* Clear for later iterations. */
3642 /* Setup to resume next batch of thread references, set nextthread. */
3643 if (result_count
>= 1)
3644 copy_threadref (&rs
->nextthread
,
3645 &rs
->resultthreadlist
[result_count
- 1]);
3647 while (result_count
--)
3649 if (!(*stepfunction
) (&rs
->resultthreadlist
[i
++], context
))
3659 /* A thread found on the remote target. */
3663 explicit thread_item (ptid_t ptid_
)
3667 thread_item (thread_item
&&other
) = default;
3668 thread_item
&operator= (thread_item
&&other
) = default;
3670 DISABLE_COPY_AND_ASSIGN (thread_item
);
3672 /* The thread's PTID. */
3675 /* The thread's extra info. */
3678 /* The thread's name. */
3681 /* The core the thread was running on. -1 if not known. */
3684 /* The thread handle associated with the thread. */
3685 gdb::byte_vector thread_handle
;
3688 /* Context passed around to the various methods listing remote
3689 threads. As new threads are found, they're added to the ITEMS
3692 struct threads_listing_context
3694 /* Return true if this object contains an entry for a thread with ptid
3697 bool contains_thread (ptid_t ptid
) const
3699 auto match_ptid
= [&] (const thread_item
&item
)
3701 return item
.ptid
== ptid
;
3704 auto it
= std::find_if (this->items
.begin (),
3708 return it
!= this->items
.end ();
3711 /* Remove the thread with ptid PTID. */
3713 void remove_thread (ptid_t ptid
)
3715 auto match_ptid
= [&] (const thread_item
&item
)
3717 return item
.ptid
== ptid
;
3720 auto it
= std::remove_if (this->items
.begin (),
3724 if (it
!= this->items
.end ())
3725 this->items
.erase (it
);
3728 /* The threads found on the remote target. */
3729 std::vector
<thread_item
> items
;
3733 remote_newthread_step (threadref
*ref
, void *data
)
3735 struct threads_listing_context
*context
3736 = (struct threads_listing_context
*) data
;
3737 int pid
= inferior_ptid
.pid ();
3738 int lwp
= threadref_to_int (ref
);
3739 ptid_t
ptid (pid
, lwp
);
3741 context
->items
.emplace_back (ptid
);
3743 return 1; /* continue iterator */
3746 #define CRAZY_MAX_THREADS 1000
3749 remote_target::remote_current_thread (ptid_t oldpid
)
3751 struct remote_state
*rs
= get_remote_state ();
3754 getpkt (&rs
->buf
, 0);
3755 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
3760 result
= read_ptid (&rs
->buf
[2], &obuf
);
3762 remote_debug_printf ("warning: garbage in qC reply");
3770 /* List remote threads using the deprecated qL packet. */
3773 remote_target::remote_get_threads_with_ql (threads_listing_context
*context
)
3775 if (remote_threadlist_iterator (remote_newthread_step
, context
,
3776 CRAZY_MAX_THREADS
) >= 0)
3782 #if defined(HAVE_LIBEXPAT)
3785 start_thread (struct gdb_xml_parser
*parser
,
3786 const struct gdb_xml_element
*element
,
3788 std::vector
<gdb_xml_value
> &attributes
)
3790 struct threads_listing_context
*data
3791 = (struct threads_listing_context
*) user_data
;
3792 struct gdb_xml_value
*attr
;
3794 char *id
= (char *) xml_find_attribute (attributes
, "id")->value
.get ();
3795 ptid_t ptid
= read_ptid (id
, NULL
);
3797 data
->items
.emplace_back (ptid
);
3798 thread_item
&item
= data
->items
.back ();
3800 attr
= xml_find_attribute (attributes
, "core");
3802 item
.core
= *(ULONGEST
*) attr
->value
.get ();
3804 attr
= xml_find_attribute (attributes
, "name");
3806 item
.name
= (const char *) attr
->value
.get ();
3808 attr
= xml_find_attribute (attributes
, "handle");
3810 item
.thread_handle
= hex2bin ((const char *) attr
->value
.get ());
3814 end_thread (struct gdb_xml_parser
*parser
,
3815 const struct gdb_xml_element
*element
,
3816 void *user_data
, const char *body_text
)
3818 struct threads_listing_context
*data
3819 = (struct threads_listing_context
*) user_data
;
3821 if (body_text
!= NULL
&& *body_text
!= '\0')
3822 data
->items
.back ().extra
= body_text
;
3825 const struct gdb_xml_attribute thread_attributes
[] = {
3826 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
3827 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
3828 { "name", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
3829 { "handle", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
3830 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
3833 const struct gdb_xml_element thread_children
[] = {
3834 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3837 const struct gdb_xml_element threads_children
[] = {
3838 { "thread", thread_attributes
, thread_children
,
3839 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
3840 start_thread
, end_thread
},
3841 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3844 const struct gdb_xml_element threads_elements
[] = {
3845 { "threads", NULL
, threads_children
,
3846 GDB_XML_EF_NONE
, NULL
, NULL
},
3847 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3852 /* List remote threads using qXfer:threads:read. */
3855 remote_target::remote_get_threads_with_qxfer (threads_listing_context
*context
)
3857 #if defined(HAVE_LIBEXPAT)
3858 if (packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
3860 gdb::optional
<gdb::char_vector
> xml
3861 = target_read_stralloc (this, TARGET_OBJECT_THREADS
, NULL
);
3863 if (xml
&& (*xml
)[0] != '\0')
3865 gdb_xml_parse_quick (_("threads"), "threads.dtd",
3866 threads_elements
, xml
->data (), context
);
3876 /* List remote threads using qfThreadInfo/qsThreadInfo. */
3879 remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context
*context
)
3881 struct remote_state
*rs
= get_remote_state ();
3883 if (rs
->use_threadinfo_query
)
3887 putpkt ("qfThreadInfo");
3888 getpkt (&rs
->buf
, 0);
3889 bufp
= rs
->buf
.data ();
3890 if (bufp
[0] != '\0') /* q packet recognized */
3892 while (*bufp
++ == 'm') /* reply contains one or more TID */
3896 ptid_t ptid
= read_ptid (bufp
, &bufp
);
3897 context
->items
.emplace_back (ptid
);
3899 while (*bufp
++ == ','); /* comma-separated list */
3900 putpkt ("qsThreadInfo");
3901 getpkt (&rs
->buf
, 0);
3902 bufp
= rs
->buf
.data ();
3908 /* Packet not recognized. */
3909 rs
->use_threadinfo_query
= 0;
3916 /* Return true if INF only has one non-exited thread. */
3919 has_single_non_exited_thread (inferior
*inf
)
3922 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
3928 /* Implement the to_update_thread_list function for the remote
3932 remote_target::update_thread_list ()
3934 struct threads_listing_context context
;
3937 /* We have a few different mechanisms to fetch the thread list. Try
3938 them all, starting with the most preferred one first, falling
3939 back to older methods. */
3940 if (remote_get_threads_with_qxfer (&context
)
3941 || remote_get_threads_with_qthreadinfo (&context
)
3942 || remote_get_threads_with_ql (&context
))
3946 if (context
.items
.empty ()
3947 && remote_thread_always_alive (inferior_ptid
))
3949 /* Some targets don't really support threads, but still
3950 reply an (empty) thread list in response to the thread
3951 listing packets, instead of replying "packet not
3952 supported". Exit early so we don't delete the main
3957 /* CONTEXT now holds the current thread list on the remote
3958 target end. Delete GDB-side threads no longer found on the
3960 for (thread_info
*tp
: all_threads_safe ())
3962 if (tp
->inf
->process_target () != this)
3965 if (!context
.contains_thread (tp
->ptid
))
3967 /* Do not remove the thread if it is the last thread in
3968 the inferior. This situation happens when we have a
3969 pending exit process status to process. Otherwise we
3970 may end up with a seemingly live inferior (i.e. pid
3971 != 0) that has no threads. */
3972 if (has_single_non_exited_thread (tp
->inf
))
3980 /* Remove any unreported fork child threads from CONTEXT so
3981 that we don't interfere with follow fork, which is where
3982 creation of such threads is handled. */
3983 remove_new_fork_children (&context
);
3985 /* And now add threads we don't know about yet to our list. */
3986 for (thread_item
&item
: context
.items
)
3988 if (item
.ptid
!= null_ptid
)
3990 /* In non-stop mode, we assume new found threads are
3991 executing until proven otherwise with a stop reply.
3992 In all-stop, we can only get here if all threads are
3994 bool executing
= target_is_non_stop_p ();
3996 remote_notice_new_inferior (item
.ptid
, executing
);
3998 thread_info
*tp
= find_thread_ptid (this, item
.ptid
);
3999 remote_thread_info
*info
= get_remote_thread_info (tp
);
4000 info
->core
= item
.core
;
4001 info
->extra
= std::move (item
.extra
);
4002 info
->name
= std::move (item
.name
);
4003 info
->thread_handle
= std::move (item
.thread_handle
);
4010 /* If no thread listing method is supported, then query whether
4011 each known thread is alive, one by one, with the T packet.
4012 If the target doesn't support threads at all, then this is a
4013 no-op. See remote_thread_alive. */
4019 * Collect a descriptive string about the given thread.
4020 * The target may say anything it wants to about the thread
4021 * (typically info about its blocked / runnable state, name, etc.).
4022 * This string will appear in the info threads display.
4024 * Optional: targets are not required to implement this function.
4028 remote_target::extra_thread_info (thread_info
*tp
)
4030 struct remote_state
*rs
= get_remote_state ();
4033 struct gdb_ext_thread_info threadinfo
;
4035 if (rs
->remote_desc
== 0) /* paranoia */
4036 internal_error (__FILE__
, __LINE__
,
4037 _("remote_threads_extra_info"));
4039 if (tp
->ptid
== magic_null_ptid
4040 || (tp
->ptid
.pid () != 0 && tp
->ptid
.lwp () == 0))
4041 /* This is the main thread which was added by GDB. The remote
4042 server doesn't know about it. */
4045 std::string
&extra
= get_remote_thread_info (tp
)->extra
;
4047 /* If already have cached info, use it. */
4048 if (!extra
.empty ())
4049 return extra
.c_str ();
4051 if (packet_support (PACKET_qXfer_threads
) == PACKET_ENABLE
)
4053 /* If we're using qXfer:threads:read, then the extra info is
4054 included in the XML. So if we didn't have anything cached,
4055 it's because there's really no extra info. */
4059 if (rs
->use_threadextra_query
)
4061 char *b
= rs
->buf
.data ();
4062 char *endb
= b
+ get_remote_packet_size ();
4064 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
4066 write_ptid (b
, endb
, tp
->ptid
);
4069 getpkt (&rs
->buf
, 0);
4070 if (rs
->buf
[0] != 0)
4072 extra
.resize (strlen (rs
->buf
.data ()) / 2);
4073 hex2bin (rs
->buf
.data (), (gdb_byte
*) &extra
[0], extra
.size ());
4074 return extra
.c_str ();
4078 /* If the above query fails, fall back to the old method. */
4079 rs
->use_threadextra_query
= 0;
4080 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4081 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4082 int_to_threadref (&id
, tp
->ptid
.lwp ());
4083 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
4084 if (threadinfo
.active
)
4086 if (*threadinfo
.shortname
)
4087 string_appendf (extra
, " Name: %s", threadinfo
.shortname
);
4088 if (*threadinfo
.display
)
4090 if (!extra
.empty ())
4092 string_appendf (extra
, " State: %s", threadinfo
.display
);
4094 if (*threadinfo
.more_display
)
4096 if (!extra
.empty ())
4098 string_appendf (extra
, " Priority: %s", threadinfo
.more_display
);
4100 return extra
.c_str ();
4107 remote_target::static_tracepoint_marker_at (CORE_ADDR addr
,
4108 struct static_tracepoint_marker
*marker
)
4110 struct remote_state
*rs
= get_remote_state ();
4111 char *p
= rs
->buf
.data ();
4113 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
4115 p
+= hexnumstr (p
, addr
);
4117 getpkt (&rs
->buf
, 0);
4118 p
= rs
->buf
.data ();
4121 error (_("Remote failure reply: %s"), p
);
4125 parse_static_tracepoint_marker_definition (p
, NULL
, marker
);
4132 std::vector
<static_tracepoint_marker
>
4133 remote_target::static_tracepoint_markers_by_strid (const char *strid
)
4135 struct remote_state
*rs
= get_remote_state ();
4136 std::vector
<static_tracepoint_marker
> markers
;
4138 static_tracepoint_marker marker
;
4140 /* Ask for a first packet of static tracepoint marker
4143 getpkt (&rs
->buf
, 0);
4144 p
= rs
->buf
.data ();
4146 error (_("Remote failure reply: %s"), p
);
4152 parse_static_tracepoint_marker_definition (p
, &p
, &marker
);
4154 if (strid
== NULL
|| marker
.str_id
== strid
)
4155 markers
.push_back (std::move (marker
));
4157 while (*p
++ == ','); /* comma-separated list */
4158 /* Ask for another packet of static tracepoint definition. */
4160 getpkt (&rs
->buf
, 0);
4161 p
= rs
->buf
.data ();
4168 /* Implement the to_get_ada_task_ptid function for the remote targets. */
4171 remote_target::get_ada_task_ptid (long lwp
, ULONGEST thread
)
4173 return ptid_t (inferior_ptid
.pid (), lwp
);
4177 /* Restart the remote side; this is an extended protocol operation. */
4180 remote_target::extended_remote_restart ()
4182 struct remote_state
*rs
= get_remote_state ();
4184 /* Send the restart command; for reasons I don't understand the
4185 remote side really expects a number after the "R". */
4186 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "R%x", 0);
4189 remote_fileio_reset ();
4192 /* Clean up connection to a remote debugger. */
4195 remote_target::close ()
4197 /* Make sure we leave stdin registered in the event loop. */
4200 trace_reset_local_state ();
4205 remote_target::~remote_target ()
4207 struct remote_state
*rs
= get_remote_state ();
4209 /* Check for NULL because we may get here with a partially
4210 constructed target/connection. */
4211 if (rs
->remote_desc
== nullptr)
4214 serial_close (rs
->remote_desc
);
4216 /* We are destroying the remote target, so we should discard
4217 everything of this target. */
4218 discard_pending_stop_replies_in_queue ();
4220 if (rs
->remote_async_inferior_event_token
)
4221 delete_async_event_handler (&rs
->remote_async_inferior_event_token
);
4223 delete rs
->notif_state
;
4226 /* Query the remote side for the text, data and bss offsets. */
4229 remote_target::get_offsets ()
4231 struct remote_state
*rs
= get_remote_state ();
4234 int lose
, num_segments
= 0, do_sections
, do_segments
;
4235 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
4237 if (current_program_space
->symfile_object_file
== NULL
)
4240 putpkt ("qOffsets");
4241 getpkt (&rs
->buf
, 0);
4242 buf
= rs
->buf
.data ();
4244 if (buf
[0] == '\000')
4245 return; /* Return silently. Stub doesn't support
4249 warning (_("Remote failure reply: %s"), buf
);
4253 /* Pick up each field in turn. This used to be done with scanf, but
4254 scanf will make trouble if CORE_ADDR size doesn't match
4255 conversion directives correctly. The following code will work
4256 with any size of CORE_ADDR. */
4257 text_addr
= data_addr
= bss_addr
= 0;
4261 if (startswith (ptr
, "Text="))
4264 /* Don't use strtol, could lose on big values. */
4265 while (*ptr
&& *ptr
!= ';')
4266 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4268 if (startswith (ptr
, ";Data="))
4271 while (*ptr
&& *ptr
!= ';')
4272 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4277 if (!lose
&& startswith (ptr
, ";Bss="))
4280 while (*ptr
&& *ptr
!= ';')
4281 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
4283 if (bss_addr
!= data_addr
)
4284 warning (_("Target reported unsupported offsets: %s"), buf
);
4289 else if (startswith (ptr
, "TextSeg="))
4292 /* Don't use strtol, could lose on big values. */
4293 while (*ptr
&& *ptr
!= ';')
4294 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
4297 if (startswith (ptr
, ";DataSeg="))
4300 while (*ptr
&& *ptr
!= ';')
4301 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
4309 error (_("Malformed response to offset query, %s"), buf
);
4310 else if (*ptr
!= '\0')
4311 warning (_("Target reported unsupported offsets: %s"), buf
);
4313 objfile
*objf
= current_program_space
->symfile_object_file
;
4314 section_offsets offs
= objf
->section_offsets
;
4316 symfile_segment_data_up data
= get_symfile_segment_data (objf
->obfd
);
4317 do_segments
= (data
!= NULL
);
4318 do_sections
= num_segments
== 0;
4320 if (num_segments
> 0)
4322 segments
[0] = text_addr
;
4323 segments
[1] = data_addr
;
4325 /* If we have two segments, we can still try to relocate everything
4326 by assuming that the .text and .data offsets apply to the whole
4327 text and data segments. Convert the offsets given in the packet
4328 to base addresses for symfile_map_offsets_to_segments. */
4329 else if (data
!= nullptr && data
->segments
.size () == 2)
4331 segments
[0] = data
->segments
[0].base
+ text_addr
;
4332 segments
[1] = data
->segments
[1].base
+ data_addr
;
4335 /* If the object file has only one segment, assume that it is text
4336 rather than data; main programs with no writable data are rare,
4337 but programs with no code are useless. Of course the code might
4338 have ended up in the data segment... to detect that we would need
4339 the permissions here. */
4340 else if (data
&& data
->segments
.size () == 1)
4342 segments
[0] = data
->segments
[0].base
+ text_addr
;
4345 /* There's no way to relocate by segment. */
4351 int ret
= symfile_map_offsets_to_segments (objf
->obfd
,
4353 num_segments
, segments
);
4355 if (ret
== 0 && !do_sections
)
4356 error (_("Can not handle qOffsets TextSeg "
4357 "response with this symbol file"));
4365 offs
[SECT_OFF_TEXT (objf
)] = text_addr
;
4367 /* This is a temporary kludge to force data and bss to use the
4368 same offsets because that's what nlmconv does now. The real
4369 solution requires changes to the stub and remote.c that I
4370 don't have time to do right now. */
4372 offs
[SECT_OFF_DATA (objf
)] = data_addr
;
4373 offs
[SECT_OFF_BSS (objf
)] = data_addr
;
4376 objfile_relocate (objf
, offs
);
4379 /* Send interrupt_sequence to remote target. */
4382 remote_target::send_interrupt_sequence ()
4384 struct remote_state
*rs
= get_remote_state ();
4386 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
4387 remote_serial_write ("\x03", 1);
4388 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
4389 serial_send_break (rs
->remote_desc
);
4390 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
4392 serial_send_break (rs
->remote_desc
);
4393 remote_serial_write ("g", 1);
4396 internal_error (__FILE__
, __LINE__
,
4397 _("Invalid value for interrupt_sequence_mode: %s."),
4398 interrupt_sequence_mode
);
4402 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
4403 and extract the PTID. Returns NULL_PTID if not found. */
4406 stop_reply_extract_thread (const char *stop_reply
)
4408 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
4412 /* Txx r:val ; r:val (...) */
4415 /* Look for "register" named "thread". */
4420 p1
= strchr (p
, ':');
4424 if (strncmp (p
, "thread", p1
- p
) == 0)
4425 return read_ptid (++p1
, &p
);
4427 p1
= strchr (p
, ';');
4439 /* Determine the remote side's current thread. If we have a stop
4440 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
4441 "thread" register we can extract the current thread from. If not,
4442 ask the remote which is the current thread with qC. The former
4443 method avoids a roundtrip. */
4446 remote_target::get_current_thread (const char *wait_status
)
4448 ptid_t ptid
= null_ptid
;
4450 /* Note we don't use remote_parse_stop_reply as that makes use of
4451 the target architecture, which we haven't yet fully determined at
4453 if (wait_status
!= NULL
)
4454 ptid
= stop_reply_extract_thread (wait_status
);
4455 if (ptid
== null_ptid
)
4456 ptid
= remote_current_thread (inferior_ptid
);
4461 /* Query the remote target for which is the current thread/process,
4462 add it to our tables, and update INFERIOR_PTID. The caller is
4463 responsible for setting the state such that the remote end is ready
4464 to return the current thread.
4466 This function is called after handling the '?' or 'vRun' packets,
4467 whose response is a stop reply from which we can also try
4468 extracting the thread. If the target doesn't support the explicit
4469 qC query, we infer the current thread from that stop reply, passed
4470 in in WAIT_STATUS, which may be NULL.
4472 The function returns pointer to the main thread of the inferior. */
4475 remote_target::add_current_inferior_and_thread (const char *wait_status
)
4477 struct remote_state
*rs
= get_remote_state ();
4478 bool fake_pid_p
= false;
4480 switch_to_no_thread ();
4482 /* Now, if we have thread information, update the current thread's
4484 ptid_t curr_ptid
= get_current_thread (wait_status
);
4486 if (curr_ptid
!= null_ptid
)
4488 if (!remote_multi_process_p (rs
))
4493 /* Without this, some commands which require an active target
4494 (such as kill) won't work. This variable serves (at least)
4495 double duty as both the pid of the target process (if it has
4496 such), and as a flag indicating that a target is active. */
4497 curr_ptid
= magic_null_ptid
;
4501 remote_add_inferior (fake_pid_p
, curr_ptid
.pid (), -1, 1);
4503 /* Add the main thread and switch to it. Don't try reading
4504 registers yet, since we haven't fetched the target description
4506 thread_info
*tp
= add_thread_silent (this, curr_ptid
);
4507 switch_to_thread_no_regs (tp
);
4512 /* Print info about a thread that was found already stopped on
4516 remote_target::print_one_stopped_thread (thread_info
*thread
)
4518 target_waitstatus ws
;
4520 /* If there is a pending waitstatus, use it. If there isn't it's because
4521 the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
4522 and process_initial_stop_replies decided it wasn't interesting to save
4523 and report to the core. */
4524 if (thread
->has_pending_waitstatus ())
4526 ws
= thread
->pending_waitstatus ();
4527 thread
->clear_pending_waitstatus ();
4531 ws
.set_stopped (GDB_SIGNAL_0
);
4534 switch_to_thread (thread
);
4535 thread
->set_stop_pc (get_frame_pc (get_current_frame ()));
4536 set_current_sal_from_frame (get_current_frame ());
4538 /* For "info program". */
4539 set_last_target_status (this, thread
->ptid
, ws
);
4541 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4543 enum gdb_signal sig
= ws
.sig ();
4545 if (signal_print_state (sig
))
4546 gdb::observers::signal_received
.notify (sig
);
4548 gdb::observers::normal_stop
.notify (NULL
, 1);
4551 /* Process all initial stop replies the remote side sent in response
4552 to the ? packet. These indicate threads that were already stopped
4553 on initial connection. We mark these threads as stopped and print
4554 their current frame before giving the user the prompt. */
4557 remote_target::process_initial_stop_replies (int from_tty
)
4559 int pending_stop_replies
= stop_reply_queue_length ();
4560 struct thread_info
*selected
= NULL
;
4561 struct thread_info
*lowest_stopped
= NULL
;
4562 struct thread_info
*first
= NULL
;
4564 /* This is only used when the target is non-stop. */
4565 gdb_assert (target_is_non_stop_p ());
4567 /* Consume the initial pending events. */
4568 while (pending_stop_replies
-- > 0)
4570 ptid_t waiton_ptid
= minus_one_ptid
;
4572 struct target_waitstatus ws
;
4573 int ignore_event
= 0;
4575 event_ptid
= target_wait (waiton_ptid
, &ws
, TARGET_WNOHANG
);
4577 print_target_wait_results (waiton_ptid
, event_ptid
, ws
);
4581 case TARGET_WAITKIND_IGNORE
:
4582 case TARGET_WAITKIND_NO_RESUMED
:
4583 case TARGET_WAITKIND_SIGNALLED
:
4584 case TARGET_WAITKIND_EXITED
:
4585 /* We shouldn't see these, but if we do, just ignore. */
4586 remote_debug_printf ("event ignored");
4597 thread_info
*evthread
= find_thread_ptid (this, event_ptid
);
4599 if (ws
.kind () == TARGET_WAITKIND_STOPPED
)
4601 enum gdb_signal sig
= ws
.sig ();
4603 /* Stubs traditionally report SIGTRAP as initial signal,
4604 instead of signal 0. Suppress it. */
4605 if (sig
== GDB_SIGNAL_TRAP
)
4607 evthread
->set_stop_signal (sig
);
4608 ws
.set_stopped (sig
);
4611 if (ws
.kind () != TARGET_WAITKIND_STOPPED
4612 || ws
.sig () != GDB_SIGNAL_0
)
4613 evthread
->set_pending_waitstatus (ws
);
4615 set_executing (this, event_ptid
, false);
4616 set_running (this, event_ptid
, false);
4617 get_remote_thread_info (evthread
)->set_not_resumed ();
4620 /* "Notice" the new inferiors before anything related to
4621 registers/memory. */
4622 for (inferior
*inf
: all_non_exited_inferiors (this))
4624 inf
->needs_setup
= 1;
4628 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4629 notice_new_inferior (thread
, thread
->state
== THREAD_RUNNING
,
4634 /* If all-stop on top of non-stop, pause all threads. Note this
4635 records the threads' stop pc, so must be done after "noticing"
4640 /* At this point, the remote target is not async. It needs to be for
4641 the poll in stop_all_threads to consider events from it, so enable
4643 gdb_assert (!this->is_async_p ());
4644 SCOPE_EXIT
{ target_async (0); };
4646 stop_all_threads ();
4649 /* If all threads of an inferior were already stopped, we
4650 haven't setup the inferior yet. */
4651 for (inferior
*inf
: all_non_exited_inferiors (this))
4653 if (inf
->needs_setup
)
4655 thread_info
*thread
= any_live_thread_of_inferior (inf
);
4656 switch_to_thread_no_regs (thread
);
4662 /* Now go over all threads that are stopped, and print their current
4663 frame. If all-stop, then if there's a signalled thread, pick
4665 for (thread_info
*thread
: all_non_exited_threads (this))
4671 thread
->set_running (false);
4672 else if (thread
->state
!= THREAD_STOPPED
)
4675 if (selected
== nullptr && thread
->has_pending_waitstatus ())
4678 if (lowest_stopped
== NULL
4679 || thread
->inf
->num
< lowest_stopped
->inf
->num
4680 || thread
->per_inf_num
< lowest_stopped
->per_inf_num
)
4681 lowest_stopped
= thread
;
4684 print_one_stopped_thread (thread
);
4687 /* In all-stop, we only print the status of one thread, and leave
4688 others with their status pending. */
4691 thread_info
*thread
= selected
;
4693 thread
= lowest_stopped
;
4697 print_one_stopped_thread (thread
);
4701 /* Mark a remote_target as marking (by setting the starting_up flag within
4702 its remote_state) for the lifetime of this object. The reference count
4703 on the remote target is temporarily incremented, to prevent the target
4704 being deleted under our feet. */
4706 struct scoped_mark_target_starting
4708 /* Constructor, TARGET is the target to be marked as starting, its
4709 reference count will be incremented. */
4710 scoped_mark_target_starting (remote_target
*target
)
4711 : m_remote_target (target
)
4713 m_remote_target
->incref ();
4714 remote_state
*rs
= m_remote_target
->get_remote_state ();
4715 rs
->starting_up
= true;
4718 /* Destructor, mark the target being worked on as no longer starting, and
4719 decrement the reference count. */
4720 ~scoped_mark_target_starting ()
4722 remote_state
*rs
= m_remote_target
->get_remote_state ();
4723 rs
->starting_up
= false;
4724 decref_target (m_remote_target
);
4729 /* The target on which we are operating. */
4730 remote_target
*m_remote_target
;
4733 /* Helper for remote_target::start_remote, start the remote connection and
4734 sync state. Return true if everything goes OK, otherwise, return false.
4735 This function exists so that the scoped_restore created within it will
4736 expire before we return to remote_target::start_remote. */
4739 remote_target::start_remote_1 (int from_tty
, int extended_p
)
4741 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
4743 struct remote_state
*rs
= get_remote_state ();
4744 struct packet_config
*noack_config
;
4746 /* Signal other parts that we're going through the initial setup,
4747 and so things may not be stable yet. E.g., we don't try to
4748 install tracepoints until we've relocated symbols. Also, a
4749 Ctrl-C before we're connected and synced up can't interrupt the
4750 target. Instead, it offers to drop the (potentially wedged)
4752 scoped_mark_target_starting
target_is_starting (this);
4756 if (interrupt_on_connect
)
4757 send_interrupt_sequence ();
4759 /* Ack any packet which the remote side has already sent. */
4760 remote_serial_write ("+", 1);
4762 /* The first packet we send to the target is the optional "supported
4763 packets" request. If the target can answer this, it will tell us
4764 which later probes to skip. */
4765 remote_query_supported ();
4767 /* If the stub wants to get a QAllow, compose one and send it. */
4768 if (packet_support (PACKET_QAllow
) != PACKET_DISABLE
)
4771 /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
4772 unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
4773 as a reply to known packet. For packet "vFile:setfs:" it is an
4774 invalid reply and GDB would return error in
4775 remote_hostio_set_filesystem, making remote files access impossible.
4776 Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
4777 other "vFile" packets get correctly detected even on gdbserver < 7.7. */
4779 const char v_mustreplyempty
[] = "vMustReplyEmpty";
4781 putpkt (v_mustreplyempty
);
4782 getpkt (&rs
->buf
, 0);
4783 if (strcmp (rs
->buf
.data (), "OK") == 0)
4784 remote_protocol_packets
[PACKET_vFile_setfs
].support
= PACKET_DISABLE
;
4785 else if (strcmp (rs
->buf
.data (), "") != 0)
4786 error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty
,
4790 /* Next, we possibly activate noack mode.
4792 If the QStartNoAckMode packet configuration is set to AUTO,
4793 enable noack mode if the stub reported a wish for it with
4796 If set to TRUE, then enable noack mode even if the stub didn't
4797 report it in qSupported. If the stub doesn't reply OK, the
4798 session ends with an error.
4800 If FALSE, then don't activate noack mode, regardless of what the
4801 stub claimed should be the default with qSupported. */
4803 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
4804 if (packet_config_support (noack_config
) != PACKET_DISABLE
)
4806 putpkt ("QStartNoAckMode");
4807 getpkt (&rs
->buf
, 0);
4808 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
4814 /* Tell the remote that we are using the extended protocol. */
4816 getpkt (&rs
->buf
, 0);
4819 /* Let the target know which signals it is allowed to pass down to
4821 update_signals_program_target ();
4823 /* Next, if the target can specify a description, read it. We do
4824 this before anything involving memory or registers. */
4825 target_find_description ();
4827 /* Next, now that we know something about the target, update the
4828 address spaces in the program spaces. */
4829 update_address_spaces ();
4831 /* On OSs where the list of libraries is global to all
4832 processes, we fetch them early. */
4833 if (gdbarch_has_global_solist (target_gdbarch ()))
4834 solib_add (NULL
, from_tty
, auto_solib_add
);
4836 if (target_is_non_stop_p ())
4838 if (packet_support (PACKET_QNonStop
) != PACKET_ENABLE
)
4839 error (_("Non-stop mode requested, but remote "
4840 "does not support non-stop"));
4842 putpkt ("QNonStop:1");
4843 getpkt (&rs
->buf
, 0);
4845 if (strcmp (rs
->buf
.data (), "OK") != 0)
4846 error (_("Remote refused setting non-stop mode with: %s"),
4849 /* Find about threads and processes the stub is already
4850 controlling. We default to adding them in the running state.
4851 The '?' query below will then tell us about which threads are
4853 this->update_thread_list ();
4855 else if (packet_support (PACKET_QNonStop
) == PACKET_ENABLE
)
4857 /* Don't assume that the stub can operate in all-stop mode.
4858 Request it explicitly. */
4859 putpkt ("QNonStop:0");
4860 getpkt (&rs
->buf
, 0);
4862 if (strcmp (rs
->buf
.data (), "OK") != 0)
4863 error (_("Remote refused setting all-stop mode with: %s"),
4867 /* Upload TSVs regardless of whether the target is running or not. The
4868 remote stub, such as GDBserver, may have some predefined or builtin
4869 TSVs, even if the target is not running. */
4870 if (get_trace_status (current_trace_status ()) != -1)
4872 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
4874 upload_trace_state_variables (&uploaded_tsvs
);
4875 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
4878 /* Check whether the target is running now. */
4880 getpkt (&rs
->buf
, 0);
4882 if (!target_is_non_stop_p ())
4884 char *wait_status
= NULL
;
4886 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
4889 error (_("The target is not running (try extended-remote?)"));
4894 /* Save the reply for later. */
4895 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
4896 strcpy (wait_status
, rs
->buf
.data ());
4899 /* Fetch thread list. */
4900 target_update_thread_list ();
4902 /* Let the stub know that we want it to return the thread. */
4903 set_continue_thread (minus_one_ptid
);
4905 if (thread_count (this) == 0)
4907 /* Target has no concept of threads at all. GDB treats
4908 non-threaded target as single-threaded; add a main
4910 thread_info
*tp
= add_current_inferior_and_thread (wait_status
);
4911 get_remote_thread_info (tp
)->set_resumed ();
4915 /* We have thread information; select the thread the target
4916 says should be current. If we're reconnecting to a
4917 multi-threaded program, this will ideally be the thread
4918 that last reported an event before GDB disconnected. */
4919 ptid_t curr_thread
= get_current_thread (wait_status
);
4920 if (curr_thread
== null_ptid
)
4922 /* Odd... The target was able to list threads, but not
4923 tell us which thread was current (no "thread"
4924 register in T stop reply?). Just pick the first
4925 thread in the thread list then. */
4927 remote_debug_printf ("warning: couldn't determine remote "
4928 "current thread; picking first in list.");
4930 for (thread_info
*tp
: all_non_exited_threads (this,
4933 switch_to_thread (tp
);
4938 switch_to_thread (find_thread_ptid (this, curr_thread
));
4941 /* init_wait_for_inferior should be called before get_offsets in order
4942 to manage `inserted' flag in bp loc in a correct state.
4943 breakpoint_init_inferior, called from init_wait_for_inferior, set
4944 `inserted' flag to 0, while before breakpoint_re_set, called from
4945 start_remote, set `inserted' flag to 1. In the initialization of
4946 inferior, breakpoint_init_inferior should be called first, and then
4947 breakpoint_re_set can be called. If this order is broken, state of
4948 `inserted' flag is wrong, and cause some problems on breakpoint
4950 init_wait_for_inferior ();
4952 get_offsets (); /* Get text, data & bss offsets. */
4954 /* If we could not find a description using qXfer, and we know
4955 how to do it some other way, try again. This is not
4956 supported for non-stop; it could be, but it is tricky if
4957 there are no stopped threads when we connect. */
4958 if (remote_read_description_p (this)
4959 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
4961 target_clear_description ();
4962 target_find_description ();
4965 /* Use the previously fetched status. */
4966 gdb_assert (wait_status
!= NULL
);
4967 struct notif_event
*reply
4968 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
4969 push_stop_reply ((struct stop_reply
*) reply
);
4971 ::start_remote (from_tty
); /* Initialize gdb process mechanisms. */
4975 /* Clear WFI global state. Do this before finding about new
4976 threads and inferiors, and setting the current inferior.
4977 Otherwise we would clear the proceed status of the current
4978 inferior when we want its stop_soon state to be preserved
4979 (see notice_new_inferior). */
4980 init_wait_for_inferior ();
4982 /* In non-stop, we will either get an "OK", meaning that there
4983 are no stopped threads at this time; or, a regular stop
4984 reply. In the latter case, there may be more than one thread
4985 stopped --- we pull them all out using the vStopped
4987 if (strcmp (rs
->buf
.data (), "OK") != 0)
4989 struct notif_client
*notif
= ¬if_client_stop
;
4991 /* remote_notif_get_pending_replies acks this one, and gets
4993 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
4994 = remote_notif_parse (this, notif
, rs
->buf
.data ());
4995 remote_notif_get_pending_events (notif
);
4998 if (thread_count (this) == 0)
5001 error (_("The target is not running (try extended-remote?)"));
5005 /* Report all signals during attach/startup. */
5008 /* If there are already stopped threads, mark them stopped and
5009 report their stops before giving the prompt to the user. */
5010 process_initial_stop_replies (from_tty
);
5012 if (target_can_async_p ())
5016 /* If we connected to a live target, do some additional setup. */
5017 if (target_has_execution ())
5019 /* No use without a symbol-file. */
5020 if (current_program_space
->symfile_object_file
)
5021 remote_check_symbols ();
5024 /* Possibly the target has been engaged in a trace run started
5025 previously; find out where things are at. */
5026 if (get_trace_status (current_trace_status ()) != -1)
5028 struct uploaded_tp
*uploaded_tps
= NULL
;
5030 if (current_trace_status ()->running
)
5031 printf_filtered (_("Trace is already running on the target.\n"));
5033 upload_tracepoints (&uploaded_tps
);
5035 merge_uploaded_tracepoints (&uploaded_tps
);
5038 /* Possibly the target has been engaged in a btrace record started
5039 previously; find out where things are at. */
5040 remote_btrace_maybe_reopen ();
5045 /* Start the remote connection and sync state. */
5048 remote_target::start_remote (int from_tty
, int extended_p
)
5050 if (start_remote_1 (from_tty
, extended_p
)
5051 && breakpoints_should_be_inserted_now ())
5052 insert_breakpoints ();
5056 remote_target::connection_string ()
5058 remote_state
*rs
= get_remote_state ();
5060 if (rs
->remote_desc
->name
!= NULL
)
5061 return rs
->remote_desc
->name
;
5066 /* Open a connection to a remote debugger.
5067 NAME is the filename used for communication. */
5070 remote_target::open (const char *name
, int from_tty
)
5072 open_1 (name
, from_tty
, 0);
5075 /* Open a connection to a remote debugger using the extended
5076 remote gdb protocol. NAME is the filename used for communication. */
5079 extended_remote_target::open (const char *name
, int from_tty
)
5081 open_1 (name
, from_tty
, 1 /*extended_p */);
5084 /* Reset all packets back to "unknown support". Called when opening a
5085 new connection to a remote target. */
5088 reset_all_packet_configs_support (void)
5092 for (i
= 0; i
< PACKET_MAX
; i
++)
5093 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5096 /* Initialize all packet configs. */
5099 init_all_packet_configs (void)
5103 for (i
= 0; i
< PACKET_MAX
; i
++)
5105 remote_protocol_packets
[i
].detect
= AUTO_BOOLEAN_AUTO
;
5106 remote_protocol_packets
[i
].support
= PACKET_SUPPORT_UNKNOWN
;
5110 /* Symbol look-up. */
5113 remote_target::remote_check_symbols ()
5118 /* The remote side has no concept of inferiors that aren't running
5119 yet, it only knows about running processes. If we're connected
5120 but our current inferior is not running, we should not invite the
5121 remote target to request symbol lookups related to its
5122 (unrelated) current process. */
5123 if (!target_has_execution ())
5126 if (packet_support (PACKET_qSymbol
) == PACKET_DISABLE
)
5129 /* Make sure the remote is pointing at the right process. Note
5130 there's no way to select "no process". */
5131 set_general_process ();
5133 /* Allocate a message buffer. We can't reuse the input buffer in RS,
5134 because we need both at the same time. */
5135 gdb::char_vector
msg (get_remote_packet_size ());
5136 gdb::char_vector
reply (get_remote_packet_size ());
5138 /* Invite target to request symbol lookups. */
5140 putpkt ("qSymbol::");
5142 packet_ok (reply
, &remote_protocol_packets
[PACKET_qSymbol
]);
5144 while (startswith (reply
.data (), "qSymbol:"))
5146 struct bound_minimal_symbol sym
;
5149 end
= hex2bin (tmp
, reinterpret_cast <gdb_byte
*> (msg
.data ()),
5152 sym
= lookup_minimal_symbol (msg
.data (), NULL
, NULL
);
5153 if (sym
.minsym
== NULL
)
5154 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol::%s",
5158 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
5159 CORE_ADDR sym_addr
= BMSYMBOL_VALUE_ADDRESS (sym
);
5161 /* If this is a function address, return the start of code
5162 instead of any data function descriptor. */
5163 sym_addr
= gdbarch_convert_from_func_ptr_addr
5164 (target_gdbarch (), sym_addr
, current_inferior ()->top_target ());
5166 xsnprintf (msg
.data (), get_remote_packet_size (), "qSymbol:%s:%s",
5167 phex_nz (sym_addr
, addr_size
), &reply
[8]);
5170 putpkt (msg
.data ());
5175 static struct serial
*
5176 remote_serial_open (const char *name
)
5178 static int udp_warning
= 0;
5180 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
5181 of in ser-tcp.c, because it is the remote protocol assuming that the
5182 serial connection is reliable and not the serial connection promising
5184 if (!udp_warning
&& startswith (name
, "udp:"))
5186 warning (_("The remote protocol may be unreliable over UDP.\n"
5187 "Some events may be lost, rendering further debugging "
5192 return serial_open (name
);
5195 /* Inform the target of our permission settings. The permission flags
5196 work without this, but if the target knows the settings, it can do
5197 a couple things. First, it can add its own check, to catch cases
5198 that somehow manage to get by the permissions checks in target
5199 methods. Second, if the target is wired to disallow particular
5200 settings (for instance, a system in the field that is not set up to
5201 be able to stop at a breakpoint), it can object to any unavailable
5205 remote_target::set_permissions ()
5207 struct remote_state
*rs
= get_remote_state ();
5209 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAllow:"
5210 "WriteReg:%x;WriteMem:%x;"
5211 "InsertBreak:%x;InsertTrace:%x;"
5212 "InsertFastTrace:%x;Stop:%x",
5213 may_write_registers
, may_write_memory
,
5214 may_insert_breakpoints
, may_insert_tracepoints
,
5215 may_insert_fast_tracepoints
, may_stop
);
5217 getpkt (&rs
->buf
, 0);
5219 /* If the target didn't like the packet, warn the user. Do not try
5220 to undo the user's settings, that would just be maddening. */
5221 if (strcmp (rs
->buf
.data (), "OK") != 0)
5222 warning (_("Remote refused setting permissions with: %s"),
5226 /* This type describes each known response to the qSupported
5228 struct protocol_feature
5230 /* The name of this protocol feature. */
5233 /* The default for this protocol feature. */
5234 enum packet_support default_support
;
5236 /* The function to call when this feature is reported, or after
5237 qSupported processing if the feature is not supported.
5238 The first argument points to this structure. The second
5239 argument indicates whether the packet requested support be
5240 enabled, disabled, or probed (or the default, if this function
5241 is being called at the end of processing and this feature was
5242 not reported). The third argument may be NULL; if not NULL, it
5243 is a NUL-terminated string taken from the packet following
5244 this feature's name and an equals sign. */
5245 void (*func
) (remote_target
*remote
, const struct protocol_feature
*,
5246 enum packet_support
, const char *);
5248 /* The corresponding packet for this feature. Only used if
5249 FUNC is remote_supported_packet. */
5254 remote_supported_packet (remote_target
*remote
,
5255 const struct protocol_feature
*feature
,
5256 enum packet_support support
,
5257 const char *argument
)
5261 warning (_("Remote qSupported response supplied an unexpected value for"
5262 " \"%s\"."), feature
->name
);
5266 remote_protocol_packets
[feature
->packet
].support
= support
;
5270 remote_target::remote_packet_size (const protocol_feature
*feature
,
5271 enum packet_support support
, const char *value
)
5273 struct remote_state
*rs
= get_remote_state ();
5278 if (support
!= PACKET_ENABLE
)
5281 if (value
== NULL
|| *value
== '\0')
5283 warning (_("Remote target reported \"%s\" without a size."),
5289 packet_size
= strtol (value
, &value_end
, 16);
5290 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
5292 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
5293 feature
->name
, value
);
5297 /* Record the new maximum packet size. */
5298 rs
->explicit_packet_size
= packet_size
;
5302 remote_packet_size (remote_target
*remote
, const protocol_feature
*feature
,
5303 enum packet_support support
, const char *value
)
5305 remote
->remote_packet_size (feature
, support
, value
);
5308 static const struct protocol_feature remote_protocol_features
[] = {
5309 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
5310 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
5311 PACKET_qXfer_auxv
},
5312 { "qXfer:exec-file:read", PACKET_DISABLE
, remote_supported_packet
,
5313 PACKET_qXfer_exec_file
},
5314 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
5315 PACKET_qXfer_features
},
5316 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
5317 PACKET_qXfer_libraries
},
5318 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
5319 PACKET_qXfer_libraries_svr4
},
5320 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
5321 remote_supported_packet
, PACKET_augmented_libraries_svr4_read_feature
},
5322 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
5323 PACKET_qXfer_memory_map
},
5324 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
5325 PACKET_qXfer_osdata
},
5326 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
5327 PACKET_qXfer_threads
},
5328 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
5329 PACKET_qXfer_traceframe_info
},
5330 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
5331 PACKET_QPassSignals
},
5332 { "QCatchSyscalls", PACKET_DISABLE
, remote_supported_packet
,
5333 PACKET_QCatchSyscalls
},
5334 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
5335 PACKET_QProgramSignals
},
5336 { "QSetWorkingDir", PACKET_DISABLE
, remote_supported_packet
,
5337 PACKET_QSetWorkingDir
},
5338 { "QStartupWithShell", PACKET_DISABLE
, remote_supported_packet
,
5339 PACKET_QStartupWithShell
},
5340 { "QEnvironmentHexEncoded", PACKET_DISABLE
, remote_supported_packet
,
5341 PACKET_QEnvironmentHexEncoded
},
5342 { "QEnvironmentReset", PACKET_DISABLE
, remote_supported_packet
,
5343 PACKET_QEnvironmentReset
},
5344 { "QEnvironmentUnset", PACKET_DISABLE
, remote_supported_packet
,
5345 PACKET_QEnvironmentUnset
},
5346 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
5347 PACKET_QStartNoAckMode
},
5348 { "multiprocess", PACKET_DISABLE
, remote_supported_packet
,
5349 PACKET_multiprocess_feature
},
5350 { "QNonStop", PACKET_DISABLE
, remote_supported_packet
, PACKET_QNonStop
},
5351 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
5352 PACKET_qXfer_siginfo_read
},
5353 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
5354 PACKET_qXfer_siginfo_write
},
5355 { "ConditionalTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5356 PACKET_ConditionalTracepoints
},
5357 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_supported_packet
,
5358 PACKET_ConditionalBreakpoints
},
5359 { "BreakpointCommands", PACKET_DISABLE
, remote_supported_packet
,
5360 PACKET_BreakpointCommands
},
5361 { "FastTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5362 PACKET_FastTracepoints
},
5363 { "StaticTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5364 PACKET_StaticTracepoints
},
5365 {"InstallInTrace", PACKET_DISABLE
, remote_supported_packet
,
5366 PACKET_InstallInTrace
},
5367 { "DisconnectedTracing", PACKET_DISABLE
, remote_supported_packet
,
5368 PACKET_DisconnectedTracing_feature
},
5369 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
5371 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
5373 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
5374 PACKET_TracepointSource
},
5375 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
5377 { "EnableDisableTracepoints", PACKET_DISABLE
, remote_supported_packet
,
5378 PACKET_EnableDisableTracepoints_feature
},
5379 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
5380 PACKET_qXfer_fdpic
},
5381 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
5383 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
5384 PACKET_QDisableRandomization
},
5385 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
5386 { "QTBuffer:size", PACKET_DISABLE
,
5387 remote_supported_packet
, PACKET_QTBuffer_size
},
5388 { "tracenz", PACKET_DISABLE
, remote_supported_packet
, PACKET_tracenz_feature
},
5389 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
5390 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
5391 { "Qbtrace:pt", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_pt
},
5392 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
5393 PACKET_qXfer_btrace
},
5394 { "qXfer:btrace-conf:read", PACKET_DISABLE
, remote_supported_packet
,
5395 PACKET_qXfer_btrace_conf
},
5396 { "Qbtrace-conf:bts:size", PACKET_DISABLE
, remote_supported_packet
,
5397 PACKET_Qbtrace_conf_bts_size
},
5398 { "swbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_swbreak_feature
},
5399 { "hwbreak", PACKET_DISABLE
, remote_supported_packet
, PACKET_hwbreak_feature
},
5400 { "fork-events", PACKET_DISABLE
, remote_supported_packet
,
5401 PACKET_fork_event_feature
},
5402 { "vfork-events", PACKET_DISABLE
, remote_supported_packet
,
5403 PACKET_vfork_event_feature
},
5404 { "exec-events", PACKET_DISABLE
, remote_supported_packet
,
5405 PACKET_exec_event_feature
},
5406 { "Qbtrace-conf:pt:size", PACKET_DISABLE
, remote_supported_packet
,
5407 PACKET_Qbtrace_conf_pt_size
},
5408 { "vContSupported", PACKET_DISABLE
, remote_supported_packet
, PACKET_vContSupported
},
5409 { "QThreadEvents", PACKET_DISABLE
, remote_supported_packet
, PACKET_QThreadEvents
},
5410 { "no-resumed", PACKET_DISABLE
, remote_supported_packet
, PACKET_no_resumed
},
5411 { "memory-tagging", PACKET_DISABLE
, remote_supported_packet
,
5412 PACKET_memory_tagging_feature
},
5415 static char *remote_support_xml
;
5417 /* Register string appended to "xmlRegisters=" in qSupported query. */
5420 register_remote_support_xml (const char *xml
)
5422 #if defined(HAVE_LIBEXPAT)
5423 if (remote_support_xml
== NULL
)
5424 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
5427 char *copy
= xstrdup (remote_support_xml
+ 13);
5429 char *p
= strtok_r (copy
, ",", &saveptr
);
5433 if (strcmp (p
, xml
) == 0)
5440 while ((p
= strtok_r (NULL
, ",", &saveptr
)) != NULL
);
5443 remote_support_xml
= reconcat (remote_support_xml
,
5444 remote_support_xml
, ",", xml
,
5451 remote_query_supported_append (std::string
*msg
, const char *append
)
5455 msg
->append (append
);
5459 remote_target::remote_query_supported ()
5461 struct remote_state
*rs
= get_remote_state ();
5464 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
5466 /* The packet support flags are handled differently for this packet
5467 than for most others. We treat an error, a disabled packet, and
5468 an empty response identically: any features which must be reported
5469 to be used will be automatically disabled. An empty buffer
5470 accomplishes this, since that is also the representation for a list
5471 containing no features. */
5474 if (packet_support (PACKET_qSupported
) != PACKET_DISABLE
)
5478 if (packet_set_cmd_state (PACKET_multiprocess_feature
) != AUTO_BOOLEAN_FALSE
)
5479 remote_query_supported_append (&q
, "multiprocess+");
5481 if (packet_set_cmd_state (PACKET_swbreak_feature
) != AUTO_BOOLEAN_FALSE
)
5482 remote_query_supported_append (&q
, "swbreak+");
5483 if (packet_set_cmd_state (PACKET_hwbreak_feature
) != AUTO_BOOLEAN_FALSE
)
5484 remote_query_supported_append (&q
, "hwbreak+");
5486 remote_query_supported_append (&q
, "qRelocInsn+");
5488 if (packet_set_cmd_state (PACKET_fork_event_feature
)
5489 != AUTO_BOOLEAN_FALSE
)
5490 remote_query_supported_append (&q
, "fork-events+");
5491 if (packet_set_cmd_state (PACKET_vfork_event_feature
)
5492 != AUTO_BOOLEAN_FALSE
)
5493 remote_query_supported_append (&q
, "vfork-events+");
5494 if (packet_set_cmd_state (PACKET_exec_event_feature
)
5495 != AUTO_BOOLEAN_FALSE
)
5496 remote_query_supported_append (&q
, "exec-events+");
5498 if (packet_set_cmd_state (PACKET_vContSupported
) != AUTO_BOOLEAN_FALSE
)
5499 remote_query_supported_append (&q
, "vContSupported+");
5501 if (packet_set_cmd_state (PACKET_QThreadEvents
) != AUTO_BOOLEAN_FALSE
)
5502 remote_query_supported_append (&q
, "QThreadEvents+");
5504 if (packet_set_cmd_state (PACKET_no_resumed
) != AUTO_BOOLEAN_FALSE
)
5505 remote_query_supported_append (&q
, "no-resumed+");
5507 if (packet_set_cmd_state (PACKET_memory_tagging_feature
)
5508 != AUTO_BOOLEAN_FALSE
)
5509 remote_query_supported_append (&q
, "memory-tagging+");
5511 /* Keep this one last to work around a gdbserver <= 7.10 bug in
5512 the qSupported:xmlRegisters=i386 handling. */
5513 if (remote_support_xml
!= NULL
5514 && packet_support (PACKET_qXfer_features
) != PACKET_DISABLE
)
5515 remote_query_supported_append (&q
, remote_support_xml
);
5517 q
= "qSupported:" + q
;
5518 putpkt (q
.c_str ());
5520 getpkt (&rs
->buf
, 0);
5522 /* If an error occured, warn, but do not return - just reset the
5523 buffer to empty and go on to disable features. */
5524 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
5527 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
5532 memset (seen
, 0, sizeof (seen
));
5534 next
= rs
->buf
.data ();
5537 enum packet_support is_supported
;
5538 char *p
, *end
, *name_end
, *value
;
5540 /* First separate out this item from the rest of the packet. If
5541 there's another item after this, we overwrite the separator
5542 (terminated strings are much easier to work with). */
5544 end
= strchr (p
, ';');
5547 end
= p
+ strlen (p
);
5557 warning (_("empty item in \"qSupported\" response"));
5562 name_end
= strchr (p
, '=');
5565 /* This is a name=value entry. */
5566 is_supported
= PACKET_ENABLE
;
5567 value
= name_end
+ 1;
5576 is_supported
= PACKET_ENABLE
;
5580 is_supported
= PACKET_DISABLE
;
5584 is_supported
= PACKET_SUPPORT_UNKNOWN
;
5588 warning (_("unrecognized item \"%s\" "
5589 "in \"qSupported\" response"), p
);
5595 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5596 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
5598 const struct protocol_feature
*feature
;
5601 feature
= &remote_protocol_features
[i
];
5602 feature
->func (this, feature
, is_supported
, value
);
5607 /* If we increased the packet size, make sure to increase the global
5608 buffer size also. We delay this until after parsing the entire
5609 qSupported packet, because this is the same buffer we were
5611 if (rs
->buf
.size () < rs
->explicit_packet_size
)
5612 rs
->buf
.resize (rs
->explicit_packet_size
);
5614 /* Handle the defaults for unmentioned features. */
5615 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
5618 const struct protocol_feature
*feature
;
5620 feature
= &remote_protocol_features
[i
];
5621 feature
->func (this, feature
, feature
->default_support
, NULL
);
5625 /* Serial QUIT handler for the remote serial descriptor.
5627 Defers handling a Ctrl-C until we're done with the current
5628 command/response packet sequence, unless:
5630 - We're setting up the connection. Don't send a remote interrupt
5631 request, as we're not fully synced yet. Quit immediately
5634 - The target has been resumed in the foreground
5635 (target_terminal::is_ours is false) with a synchronous resume
5636 packet, and we're blocked waiting for the stop reply, thus a
5637 Ctrl-C should be immediately sent to the target.
5639 - We get a second Ctrl-C while still within the same serial read or
5640 write. In that case the serial is seemingly wedged --- offer to
5643 - We see a second Ctrl-C without target response, after having
5644 previously interrupted the target. In that case the target/stub
5645 is probably wedged --- offer to quit/disconnect.
5649 remote_target::remote_serial_quit_handler ()
5651 struct remote_state
*rs
= get_remote_state ();
5653 if (check_quit_flag ())
5655 /* If we're starting up, we're not fully synced yet. Quit
5657 if (rs
->starting_up
)
5659 else if (rs
->got_ctrlc_during_io
)
5661 if (query (_("The target is not responding to GDB commands.\n"
5662 "Stop debugging it? ")))
5663 remote_unpush_and_throw (this);
5665 /* If ^C has already been sent once, offer to disconnect. */
5666 else if (!target_terminal::is_ours () && rs
->ctrlc_pending_p
)
5668 /* All-stop protocol, and blocked waiting for stop reply. Send
5669 an interrupt request. */
5670 else if (!target_terminal::is_ours () && rs
->waiting_for_stop_reply
)
5671 target_interrupt ();
5673 rs
->got_ctrlc_during_io
= 1;
5677 /* The remote_target that is current while the quit handler is
5678 overridden with remote_serial_quit_handler. */
5679 static remote_target
*curr_quit_handler_target
;
5682 remote_serial_quit_handler ()
5684 curr_quit_handler_target
->remote_serial_quit_handler ();
5687 /* Remove the remote target from the target stack of each inferior
5688 that is using it. Upper targets depend on it so remove them
5692 remote_unpush_target (remote_target
*target
)
5694 /* We have to unpush the target from all inferiors, even those that
5696 scoped_restore_current_inferior restore_current_inferior
;
5698 for (inferior
*inf
: all_inferiors (target
))
5700 switch_to_inferior_no_thread (inf
);
5701 pop_all_targets_at_and_above (process_stratum
);
5702 generic_mourn_inferior ();
5705 /* Don't rely on target_close doing this when the target is popped
5706 from the last remote inferior above, because something may be
5707 holding a reference to the target higher up on the stack, meaning
5708 target_close won't be called yet. We lost the connection to the
5709 target, so clear these now, otherwise we may later throw
5710 TARGET_CLOSE_ERROR while trying to tell the remote target to
5712 fileio_handles_invalidate_target (target
);
5716 remote_unpush_and_throw (remote_target
*target
)
5718 remote_unpush_target (target
);
5719 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
5723 remote_target::open_1 (const char *name
, int from_tty
, int extended_p
)
5725 remote_target
*curr_remote
= get_current_remote_target ();
5728 error (_("To open a remote debug connection, you need to specify what\n"
5729 "serial device is attached to the remote system\n"
5730 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
5732 /* If we're connected to a running target, target_preopen will kill it.
5733 Ask this question first, before target_preopen has a chance to kill
5735 if (curr_remote
!= NULL
&& !target_has_execution ())
5738 && !query (_("Already connected to a remote target. Disconnect? ")))
5739 error (_("Still connected."));
5742 /* Here the possibly existing remote target gets unpushed. */
5743 target_preopen (from_tty
);
5745 remote_fileio_reset ();
5746 reopen_exec_file ();
5747 reread_symbols (from_tty
);
5749 remote_target
*remote
5750 = (extended_p
? new extended_remote_target () : new remote_target ());
5751 target_ops_up
target_holder (remote
);
5753 remote_state
*rs
= remote
->get_remote_state ();
5755 /* See FIXME above. */
5756 if (!target_async_permitted
)
5757 rs
->wait_forever_enabled_p
= 1;
5759 rs
->remote_desc
= remote_serial_open (name
);
5760 if (!rs
->remote_desc
)
5761 perror_with_name (name
);
5763 if (baud_rate
!= -1)
5765 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
5767 /* The requested speed could not be set. Error out to
5768 top level after closing remote_desc. Take care to
5769 set remote_desc to NULL to avoid closing remote_desc
5771 serial_close (rs
->remote_desc
);
5772 rs
->remote_desc
= NULL
;
5773 perror_with_name (name
);
5777 serial_setparity (rs
->remote_desc
, serial_parity
);
5778 serial_raw (rs
->remote_desc
);
5780 /* If there is something sitting in the buffer we might take it as a
5781 response to a command, which would be bad. */
5782 serial_flush_input (rs
->remote_desc
);
5786 puts_filtered ("Remote debugging using ");
5787 puts_filtered (name
);
5788 puts_filtered ("\n");
5791 /* Switch to using the remote target now. */
5792 current_inferior ()->push_target (std::move (target_holder
));
5794 /* Register extra event sources in the event loop. */
5795 rs
->remote_async_inferior_event_token
5796 = create_async_event_handler (remote_async_inferior_event_handler
, nullptr,
5798 rs
->notif_state
= remote_notif_state_allocate (remote
);
5800 /* Reset the target state; these things will be queried either by
5801 remote_query_supported or as they are needed. */
5802 reset_all_packet_configs_support ();
5803 rs
->explicit_packet_size
= 0;
5805 rs
->extended
= extended_p
;
5806 rs
->waiting_for_stop_reply
= 0;
5807 rs
->ctrlc_pending_p
= 0;
5808 rs
->got_ctrlc_during_io
= 0;
5810 rs
->general_thread
= not_sent_ptid
;
5811 rs
->continue_thread
= not_sent_ptid
;
5812 rs
->remote_traceframe_number
= -1;
5814 rs
->last_resume_exec_dir
= EXEC_FORWARD
;
5816 /* Probe for ability to use "ThreadInfo" query, as required. */
5817 rs
->use_threadinfo_query
= 1;
5818 rs
->use_threadextra_query
= 1;
5820 rs
->readahead_cache
.invalidate ();
5822 if (target_async_permitted
)
5824 /* FIXME: cagney/1999-09-23: During the initial connection it is
5825 assumed that the target is already ready and able to respond to
5826 requests. Unfortunately remote_start_remote() eventually calls
5827 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
5828 around this. Eventually a mechanism that allows
5829 wait_for_inferior() to expect/get timeouts will be
5831 rs
->wait_forever_enabled_p
= 0;
5834 /* First delete any symbols previously loaded from shared libraries. */
5835 no_shared_libraries (NULL
, 0);
5837 /* Start the remote connection. If error() or QUIT, discard this
5838 target (we'd otherwise be in an inconsistent state) and then
5839 propogate the error on up the exception chain. This ensures that
5840 the caller doesn't stumble along blindly assuming that the
5841 function succeeded. The CLI doesn't have this problem but other
5842 UI's, such as MI do.
5844 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
5845 this function should return an error indication letting the
5846 caller restore the previous state. Unfortunately the command
5847 ``target remote'' is directly wired to this function making that
5848 impossible. On a positive note, the CLI side of this problem has
5849 been fixed - the function set_cmd_context() makes it possible for
5850 all the ``target ....'' commands to share a common callback
5851 function. See cli-dump.c. */
5856 remote
->start_remote (from_tty
, extended_p
);
5858 catch (const gdb_exception
&ex
)
5860 /* Pop the partially set up target - unless something else did
5861 already before throwing the exception. */
5862 if (ex
.error
!= TARGET_CLOSE_ERROR
)
5863 remote_unpush_target (remote
);
5868 remote_btrace_reset (rs
);
5870 if (target_async_permitted
)
5871 rs
->wait_forever_enabled_p
= 1;
5874 /* Determine if WS represents a fork status. */
5877 is_fork_status (target_waitkind kind
)
5879 return (kind
== TARGET_WAITKIND_FORKED
5880 || kind
== TARGET_WAITKIND_VFORKED
);
5883 /* Return THREAD's pending status if it is a pending fork parent, else
5886 static const target_waitstatus
*
5887 thread_pending_fork_status (struct thread_info
*thread
)
5889 const target_waitstatus
&ws
5890 = (thread
->has_pending_waitstatus ()
5891 ? thread
->pending_waitstatus ()
5892 : thread
->pending_follow
);
5894 if (!is_fork_status (ws
.kind ()))
5900 /* Detach the specified process. */
5903 remote_target::remote_detach_pid (int pid
)
5905 struct remote_state
*rs
= get_remote_state ();
5907 /* This should not be necessary, but the handling for D;PID in
5908 GDBserver versions prior to 8.2 incorrectly assumes that the
5909 selected process points to the same process we're detaching,
5910 leading to misbehavior (and possibly GDBserver crashing) when it
5911 does not. Since it's easy and cheap, work around it by forcing
5912 GDBserver to select GDB's current process. */
5913 set_general_process ();
5915 if (remote_multi_process_p (rs
))
5916 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "D;%x", pid
);
5918 strcpy (rs
->buf
.data (), "D");
5921 getpkt (&rs
->buf
, 0);
5923 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
5925 else if (rs
->buf
[0] == '\0')
5926 error (_("Remote doesn't know how to detach"));
5928 error (_("Can't detach process."));
5931 /* This detaches a program to which we previously attached, using
5932 inferior_ptid to identify the process. After this is done, GDB
5933 can be used to debug some other program. We better not have left
5934 any breakpoints in the target program or it'll die when it hits
5938 remote_target::remote_detach_1 (inferior
*inf
, int from_tty
)
5940 int pid
= inferior_ptid
.pid ();
5941 struct remote_state
*rs
= get_remote_state ();
5944 if (!target_has_execution ())
5945 error (_("No process to detach from."));
5947 target_announce_detach (from_tty
);
5949 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
5951 /* If we're in breakpoints-always-inserted mode, or the inferior
5952 is running, we have to remove breakpoints before detaching.
5953 We don't do this in common code instead because not all
5954 targets support removing breakpoints while the target is
5955 running. The remote target / gdbserver does, though. */
5956 remove_breakpoints_inf (current_inferior ());
5959 /* Tell the remote target to detach. */
5960 remote_detach_pid (pid
);
5962 /* Exit only if this is the only active inferior. */
5963 if (from_tty
&& !rs
->extended
&& number_of_live_inferiors (this) == 1)
5964 puts_filtered (_("Ending remote debugging.\n"));
5966 /* See if any thread of the inferior we are detaching has a pending fork
5967 status. In that case, we must detach from the child resulting from
5969 for (thread_info
*thread
: inf
->non_exited_threads ())
5971 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
5976 remote_detach_pid (ws
->child_ptid ().pid ());
5979 /* Check also for any pending fork events in the stop reply queue. */
5980 remote_notif_get_pending_events (¬if_client_stop
);
5981 for (stop_reply_up
&reply
: rs
->stop_reply_queue
)
5983 if (reply
->ptid
.pid () != pid
)
5986 if (!is_fork_status (reply
->ws
.kind ()))
5989 remote_detach_pid (reply
->ws
.child_ptid ().pid ());
5992 thread_info
*tp
= find_thread_ptid (this, inferior_ptid
);
5994 /* Check to see if we are detaching a fork parent. Note that if we
5995 are detaching a fork child, tp == NULL. */
5996 is_fork_parent
= (tp
!= NULL
5997 && tp
->pending_follow
.kind () == TARGET_WAITKIND_FORKED
);
5999 /* If doing detach-on-fork, we don't mourn, because that will delete
6000 breakpoints that should be available for the followed inferior. */
6001 if (!is_fork_parent
)
6003 /* Save the pid as a string before mourning, since that will
6004 unpush the remote target, and we need the string after. */
6005 std::string infpid
= target_pid_to_str (ptid_t (pid
));
6007 target_mourn_inferior (inferior_ptid
);
6008 if (print_inferior_events
)
6009 printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
6010 inf
->num
, infpid
.c_str ());
6014 switch_to_no_thread ();
6015 detach_inferior (current_inferior ());
6020 remote_target::detach (inferior
*inf
, int from_tty
)
6022 remote_detach_1 (inf
, from_tty
);
6026 extended_remote_target::detach (inferior
*inf
, int from_tty
)
6028 remote_detach_1 (inf
, from_tty
);
6031 /* Target follow-fork function for remote targets. On entry, and
6032 at return, the current inferior is the fork parent.
6034 Note that although this is currently only used for extended-remote,
6035 it is named remote_follow_fork in anticipation of using it for the
6036 remote target as well. */
6039 remote_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
6040 target_waitkind fork_kind
, bool follow_child
,
6043 process_stratum_target::follow_fork (child_inf
, child_ptid
,
6044 fork_kind
, follow_child
, detach_fork
);
6046 struct remote_state
*rs
= get_remote_state ();
6048 if ((fork_kind
== TARGET_WAITKIND_FORKED
&& remote_fork_event_p (rs
))
6049 || (fork_kind
== TARGET_WAITKIND_VFORKED
&& remote_vfork_event_p (rs
)))
6051 /* When following the parent and detaching the child, we detach
6052 the child here. For the case of following the child and
6053 detaching the parent, the detach is done in the target-
6054 independent follow fork code in infrun.c. We can't use
6055 target_detach when detaching an unfollowed child because
6056 the client side doesn't know anything about the child. */
6057 if (detach_fork
&& !follow_child
)
6059 /* Detach the fork child. */
6060 remote_detach_pid (child_ptid
.pid ());
6065 /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
6066 in the program space of the new inferior. */
6069 remote_target::follow_exec (inferior
*follow_inf
, ptid_t ptid
,
6070 const char *execd_pathname
)
6072 process_stratum_target::follow_exec (follow_inf
, ptid
, execd_pathname
);
6074 /* We know that this is a target file name, so if it has the "target:"
6075 prefix we strip it off before saving it in the program space. */
6076 if (is_target_filename (execd_pathname
))
6077 execd_pathname
+= strlen (TARGET_SYSROOT_PREFIX
);
6079 set_pspace_remote_exec_file (follow_inf
->pspace
, execd_pathname
);
6082 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
6085 remote_target::disconnect (const char *args
, int from_tty
)
6088 error (_("Argument given to \"disconnect\" when remotely debugging."));
6090 /* Make sure we unpush even the extended remote targets. Calling
6091 target_mourn_inferior won't unpush, and
6092 remote_target::mourn_inferior won't unpush if there is more than
6093 one inferior left. */
6094 remote_unpush_target (this);
6097 puts_filtered ("Ending remote debugging.\n");
6100 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
6101 be chatty about it. */
6104 extended_remote_target::attach (const char *args
, int from_tty
)
6106 struct remote_state
*rs
= get_remote_state ();
6108 char *wait_status
= NULL
;
6110 pid
= parse_pid_to_attach (args
);
6112 /* Remote PID can be freely equal to getpid, do not check it here the same
6113 way as in other targets. */
6115 if (packet_support (PACKET_vAttach
) == PACKET_DISABLE
)
6116 error (_("This target does not support attaching to a process"));
6118 target_announce_attach (from_tty
, pid
);
6120 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vAttach;%x", pid
);
6122 getpkt (&rs
->buf
, 0);
6124 switch (packet_ok (rs
->buf
,
6125 &remote_protocol_packets
[PACKET_vAttach
]))
6128 if (!target_is_non_stop_p ())
6130 /* Save the reply for later. */
6131 wait_status
= (char *) alloca (strlen (rs
->buf
.data ()) + 1);
6132 strcpy (wait_status
, rs
->buf
.data ());
6134 else if (strcmp (rs
->buf
.data (), "OK") != 0)
6135 error (_("Attaching to %s failed with: %s"),
6136 target_pid_to_str (ptid_t (pid
)).c_str (),
6139 case PACKET_UNKNOWN
:
6140 error (_("This target does not support attaching to a process"));
6142 error (_("Attaching to %s failed"),
6143 target_pid_to_str (ptid_t (pid
)).c_str ());
6146 switch_to_inferior_no_thread (remote_add_inferior (false, pid
, 1, 0));
6148 inferior_ptid
= ptid_t (pid
);
6150 if (target_is_non_stop_p ())
6152 /* Get list of threads. */
6153 update_thread_list ();
6155 thread_info
*thread
= first_thread_of_inferior (current_inferior ());
6156 if (thread
!= nullptr)
6157 switch_to_thread (thread
);
6159 /* Invalidate our notion of the remote current thread. */
6160 record_currthread (rs
, minus_one_ptid
);
6164 /* Now, if we have thread information, update the main thread's
6166 ptid_t curr_ptid
= remote_current_thread (ptid_t (pid
));
6168 /* Add the main thread to the thread list. We add the thread
6169 silently in this case (the final true parameter). */
6170 thread_info
*thr
= remote_add_thread (curr_ptid
, true, true, true);
6172 switch_to_thread (thr
);
6175 /* Next, if the target can specify a description, read it. We do
6176 this before anything involving memory or registers. */
6177 target_find_description ();
6179 if (!target_is_non_stop_p ())
6181 /* Use the previously fetched status. */
6182 gdb_assert (wait_status
!= NULL
);
6184 struct notif_event
*reply
6185 = remote_notif_parse (this, ¬if_client_stop
, wait_status
);
6187 push_stop_reply ((struct stop_reply
*) reply
);
6189 if (target_can_async_p ())
6194 gdb_assert (wait_status
== NULL
);
6196 gdb_assert (target_can_async_p ());
6201 /* Implementation of the to_post_attach method. */
6204 extended_remote_target::post_attach (int pid
)
6206 /* Get text, data & bss offsets. */
6209 /* In certain cases GDB might not have had the chance to start
6210 symbol lookup up until now. This could happen if the debugged
6211 binary is not using shared libraries, the vsyscall page is not
6212 present (on Linux) and the binary itself hadn't changed since the
6213 debugging process was started. */
6214 if (current_program_space
->symfile_object_file
!= NULL
)
6215 remote_check_symbols();
6219 /* Check for the availability of vCont. This function should also check
6223 remote_target::remote_vcont_probe ()
6225 remote_state
*rs
= get_remote_state ();
6228 strcpy (rs
->buf
.data (), "vCont?");
6230 getpkt (&rs
->buf
, 0);
6231 buf
= rs
->buf
.data ();
6233 /* Make sure that the features we assume are supported. */
6234 if (startswith (buf
, "vCont"))
6237 int support_c
, support_C
;
6239 rs
->supports_vCont
.s
= 0;
6240 rs
->supports_vCont
.S
= 0;
6243 rs
->supports_vCont
.t
= 0;
6244 rs
->supports_vCont
.r
= 0;
6245 while (p
&& *p
== ';')
6248 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6249 rs
->supports_vCont
.s
= 1;
6250 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6251 rs
->supports_vCont
.S
= 1;
6252 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6254 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6256 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6257 rs
->supports_vCont
.t
= 1;
6258 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
6259 rs
->supports_vCont
.r
= 1;
6261 p
= strchr (p
, ';');
6264 /* If c, and C are not all supported, we can't use vCont. Clearing
6265 BUF will make packet_ok disable the packet. */
6266 if (!support_c
|| !support_C
)
6270 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vCont
]);
6271 rs
->supports_vCont_probed
= true;
6274 /* Helper function for building "vCont" resumptions. Write a
6275 resumption to P. ENDP points to one-passed-the-end of the buffer
6276 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
6277 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
6278 resumed thread should be single-stepped and/or signalled. If PTID
6279 equals minus_one_ptid, then all threads are resumed; if PTID
6280 represents a process, then all threads of the process are resumed;
6281 the thread to be stepped and/or signalled is given in the global
6285 remote_target::append_resumption (char *p
, char *endp
,
6286 ptid_t ptid
, int step
, gdb_signal siggnal
)
6288 struct remote_state
*rs
= get_remote_state ();
6290 if (step
&& siggnal
!= GDB_SIGNAL_0
)
6291 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
6293 /* GDB is willing to range step. */
6294 && use_range_stepping
6295 /* Target supports range stepping. */
6296 && rs
->supports_vCont
.r
6297 /* We don't currently support range stepping multiple
6298 threads with a wildcard (though the protocol allows it,
6299 so stubs shouldn't make an active effort to forbid
6301 && !(remote_multi_process_p (rs
) && ptid
.is_pid ()))
6303 struct thread_info
*tp
;
6305 if (ptid
== minus_one_ptid
)
6307 /* If we don't know about the target thread's tid, then
6308 we're resuming magic_null_ptid (see caller). */
6309 tp
= find_thread_ptid (this, magic_null_ptid
);
6312 tp
= find_thread_ptid (this, ptid
);
6313 gdb_assert (tp
!= NULL
);
6315 if (tp
->control
.may_range_step
)
6317 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6319 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
6320 phex_nz (tp
->control
.step_range_start
,
6322 phex_nz (tp
->control
.step_range_end
,
6326 p
+= xsnprintf (p
, endp
- p
, ";s");
6329 p
+= xsnprintf (p
, endp
- p
, ";s");
6330 else if (siggnal
!= GDB_SIGNAL_0
)
6331 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
6333 p
+= xsnprintf (p
, endp
- p
, ";c");
6335 if (remote_multi_process_p (rs
) && ptid
.is_pid ())
6339 /* All (-1) threads of process. */
6340 nptid
= ptid_t (ptid
.pid (), -1);
6342 p
+= xsnprintf (p
, endp
- p
, ":");
6343 p
= write_ptid (p
, endp
, nptid
);
6345 else if (ptid
!= minus_one_ptid
)
6347 p
+= xsnprintf (p
, endp
- p
, ":");
6348 p
= write_ptid (p
, endp
, ptid
);
6354 /* Clear the thread's private info on resume. */
6357 resume_clear_thread_private_info (struct thread_info
*thread
)
6359 if (thread
->priv
!= NULL
)
6361 remote_thread_info
*priv
= get_remote_thread_info (thread
);
6363 priv
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6364 priv
->watch_data_address
= 0;
6368 /* Append a vCont continue-with-signal action for threads that have a
6369 non-zero stop signal. */
6372 remote_target::append_pending_thread_resumptions (char *p
, char *endp
,
6375 for (thread_info
*thread
: all_non_exited_threads (this, ptid
))
6376 if (inferior_ptid
!= thread
->ptid
6377 && thread
->stop_signal () != GDB_SIGNAL_0
)
6379 p
= append_resumption (p
, endp
, thread
->ptid
,
6380 0, thread
->stop_signal ());
6381 thread
->set_stop_signal (GDB_SIGNAL_0
);
6382 resume_clear_thread_private_info (thread
);
6388 /* Set the target running, using the packets that use Hc
6392 remote_target::remote_resume_with_hc (ptid_t ptid
, int step
,
6395 struct remote_state
*rs
= get_remote_state ();
6398 rs
->last_sent_signal
= siggnal
;
6399 rs
->last_sent_step
= step
;
6401 /* The c/s/C/S resume packets use Hc, so set the continue
6403 if (ptid
== minus_one_ptid
)
6404 set_continue_thread (any_thread_ptid
);
6406 set_continue_thread (ptid
);
6408 for (thread_info
*thread
: all_non_exited_threads (this))
6409 resume_clear_thread_private_info (thread
);
6411 buf
= rs
->buf
.data ();
6412 if (::execution_direction
== EXEC_REVERSE
)
6414 /* We don't pass signals to the target in reverse exec mode. */
6415 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
6416 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
6419 if (step
&& packet_support (PACKET_bs
) == PACKET_DISABLE
)
6420 error (_("Remote reverse-step not supported."));
6421 if (!step
&& packet_support (PACKET_bc
) == PACKET_DISABLE
)
6422 error (_("Remote reverse-continue not supported."));
6424 strcpy (buf
, step
? "bs" : "bc");
6426 else if (siggnal
!= GDB_SIGNAL_0
)
6428 buf
[0] = step
? 'S' : 'C';
6429 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
6430 buf
[2] = tohex (((int) siggnal
) & 0xf);
6434 strcpy (buf
, step
? "s" : "c");
6439 /* Resume the remote inferior by using a "vCont" packet. The thread
6440 to be resumed is PTID; STEP and SIGGNAL indicate whether the
6441 resumed thread should be single-stepped and/or signalled. If PTID
6442 equals minus_one_ptid, then all threads are resumed; the thread to
6443 be stepped and/or signalled is given in the global INFERIOR_PTID.
6444 This function returns non-zero iff it resumes the inferior.
6446 This function issues a strict subset of all possible vCont commands
6450 remote_target::remote_resume_with_vcont (ptid_t ptid
, int step
,
6451 enum gdb_signal siggnal
)
6453 struct remote_state
*rs
= get_remote_state ();
6457 /* No reverse execution actions defined for vCont. */
6458 if (::execution_direction
== EXEC_REVERSE
)
6461 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
6462 remote_vcont_probe ();
6464 if (packet_support (PACKET_vCont
) == PACKET_DISABLE
)
6467 p
= rs
->buf
.data ();
6468 endp
= p
+ get_remote_packet_size ();
6470 /* If we could generate a wider range of packets, we'd have to worry
6471 about overflowing BUF. Should there be a generic
6472 "multi-part-packet" packet? */
6474 p
+= xsnprintf (p
, endp
- p
, "vCont");
6476 if (ptid
== magic_null_ptid
)
6478 /* MAGIC_NULL_PTID means that we don't have any active threads,
6479 so we don't have any TID numbers the inferior will
6480 understand. Make sure to only send forms that do not specify
6482 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
6484 else if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
6486 /* Resume all threads (of all processes, or of a single
6487 process), with preference for INFERIOR_PTID. This assumes
6488 inferior_ptid belongs to the set of all threads we are about
6490 if (step
|| siggnal
!= GDB_SIGNAL_0
)
6492 /* Step inferior_ptid, with or without signal. */
6493 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
6496 /* Also pass down any pending signaled resumption for other
6497 threads not the current. */
6498 p
= append_pending_thread_resumptions (p
, endp
, ptid
);
6500 /* And continue others without a signal. */
6501 append_resumption (p
, endp
, ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
6505 /* Scheduler locking; resume only PTID. */
6506 append_resumption (p
, endp
, ptid
, step
, siggnal
);
6509 gdb_assert (strlen (rs
->buf
.data ()) < get_remote_packet_size ());
6512 if (target_is_non_stop_p ())
6514 /* In non-stop, the stub replies to vCont with "OK". The stop
6515 reply will be reported asynchronously by means of a `%Stop'
6517 getpkt (&rs
->buf
, 0);
6518 if (strcmp (rs
->buf
.data (), "OK") != 0)
6519 error (_("Unexpected vCont reply in non-stop mode: %s"),
6526 /* Tell the remote machine to resume. */
6529 remote_target::resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
6531 struct remote_state
*rs
= get_remote_state ();
6533 /* When connected in non-stop mode, the core resumes threads
6534 individually. Resuming remote threads directly in target_resume
6535 would thus result in sending one packet per thread. Instead, to
6536 minimize roundtrip latency, here we just store the resume
6537 request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
6538 resumption will be done in remote_target::commit_resume, where we'll be
6539 able to do vCont action coalescing. */
6540 if (target_is_non_stop_p () && ::execution_direction
!= EXEC_REVERSE
)
6542 remote_thread_info
*remote_thr
;
6544 if (minus_one_ptid
== ptid
|| ptid
.is_pid ())
6545 remote_thr
= get_remote_thread_info (this, inferior_ptid
);
6547 remote_thr
= get_remote_thread_info (this, ptid
);
6549 /* We don't expect the core to ask to resume an already resumed (from
6550 its point of view) thread. */
6551 gdb_assert (remote_thr
->get_resume_state () == resume_state::NOT_RESUMED
);
6553 remote_thr
->set_resumed_pending_vcont (step
, siggnal
);
6557 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
6558 (explained in remote-notif.c:handle_notification) so
6559 remote_notif_process is not called. We need find a place where
6560 it is safe to start a 'vNotif' sequence. It is good to do it
6561 before resuming inferior, because inferior was stopped and no RSP
6562 traffic at that moment. */
6563 if (!target_is_non_stop_p ())
6564 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
6566 rs
->last_resume_exec_dir
= ::execution_direction
;
6568 /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
6569 if (!remote_resume_with_vcont (ptid
, step
, siggnal
))
6570 remote_resume_with_hc (ptid
, step
, siggnal
);
6572 /* Update resumed state tracked by the remote target. */
6573 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
6574 get_remote_thread_info (tp
)->set_resumed ();
6576 /* We are about to start executing the inferior, let's register it
6577 with the event loop. NOTE: this is the one place where all the
6578 execution commands end up. We could alternatively do this in each
6579 of the execution commands in infcmd.c. */
6580 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
6581 into infcmd.c in order to allow inferior function calls to work
6582 NOT asynchronously. */
6583 if (target_can_async_p ())
6586 /* We've just told the target to resume. The remote server will
6587 wait for the inferior to stop, and then send a stop reply. In
6588 the mean time, we can't start another command/query ourselves
6589 because the stub wouldn't be ready to process it. This applies
6590 only to the base all-stop protocol, however. In non-stop (which
6591 only supports vCont), the stub replies with an "OK", and is
6592 immediate able to process further serial input. */
6593 if (!target_is_non_stop_p ())
6594 rs
->waiting_for_stop_reply
= 1;
6597 /* Private per-inferior info for target remote processes. */
6599 struct remote_inferior
: public private_inferior
6601 /* Whether we can send a wildcard vCont for this process. */
6602 bool may_wildcard_vcont
= true;
6605 /* Get the remote private inferior data associated to INF. */
6607 static remote_inferior
*
6608 get_remote_inferior (inferior
*inf
)
6610 if (inf
->priv
== NULL
)
6611 inf
->priv
.reset (new remote_inferior
);
6613 return static_cast<remote_inferior
*> (inf
->priv
.get ());
6616 /* Class used to track the construction of a vCont packet in the
6617 outgoing packet buffer. This is used to send multiple vCont
6618 packets if we have more actions than would fit a single packet. */
6623 explicit vcont_builder (remote_target
*remote
)
6630 void push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
);
6635 /* The remote target. */
6636 remote_target
*m_remote
;
6638 /* Pointer to the first action. P points here if no action has been
6640 char *m_first_action
;
6642 /* Where the next action will be appended. */
6645 /* The end of the buffer. Must never write past this. */
6649 /* Prepare the outgoing buffer for a new vCont packet. */
6652 vcont_builder::restart ()
6654 struct remote_state
*rs
= m_remote
->get_remote_state ();
6656 m_p
= rs
->buf
.data ();
6657 m_endp
= m_p
+ m_remote
->get_remote_packet_size ();
6658 m_p
+= xsnprintf (m_p
, m_endp
- m_p
, "vCont");
6659 m_first_action
= m_p
;
6662 /* If the vCont packet being built has any action, send it to the
6666 vcont_builder::flush ()
6668 struct remote_state
*rs
;
6670 if (m_p
== m_first_action
)
6673 rs
= m_remote
->get_remote_state ();
6674 m_remote
->putpkt (rs
->buf
);
6675 m_remote
->getpkt (&rs
->buf
, 0);
6676 if (strcmp (rs
->buf
.data (), "OK") != 0)
6677 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
.data ());
6680 /* The largest action is range-stepping, with its two addresses. This
6681 is more than sufficient. If a new, bigger action is created, it'll
6682 quickly trigger a failed assertion in append_resumption (and we'll
6684 #define MAX_ACTION_SIZE 200
6686 /* Append a new vCont action in the outgoing packet being built. If
6687 the action doesn't fit the packet along with previous actions, push
6688 what we've got so far to the remote end and start over a new vCont
6689 packet (with the new action). */
6692 vcont_builder::push_action (ptid_t ptid
, bool step
, gdb_signal siggnal
)
6694 char buf
[MAX_ACTION_SIZE
+ 1];
6696 char *endp
= m_remote
->append_resumption (buf
, buf
+ sizeof (buf
),
6697 ptid
, step
, siggnal
);
6699 /* Check whether this new action would fit in the vCont packet along
6700 with previous actions. If not, send what we've got so far and
6701 start a new vCont packet. */
6702 size_t rsize
= endp
- buf
;
6703 if (rsize
> m_endp
- m_p
)
6708 /* Should now fit. */
6709 gdb_assert (rsize
<= m_endp
- m_p
);
6712 memcpy (m_p
, buf
, rsize
);
6717 /* to_commit_resume implementation. */
6720 remote_target::commit_resumed ()
6722 /* If connected in all-stop mode, we'd send the remote resume
6723 request directly from remote_resume. Likewise if
6724 reverse-debugging, as there are no defined vCont actions for
6725 reverse execution. */
6726 if (!target_is_non_stop_p () || ::execution_direction
== EXEC_REVERSE
)
6729 /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
6730 instead of resuming all threads of each process individually.
6731 However, if any thread of a process must remain halted, we can't
6732 send wildcard resumes and must send one action per thread.
6734 Care must be taken to not resume threads/processes the server
6735 side already told us are stopped, but the core doesn't know about
6736 yet, because the events are still in the vStopped notification
6739 #1 => vCont s:p1.1;c
6741 #3 <= %Stopped T05 p1.1
6746 #8 (infrun handles the stop for p1.1 and continues stepping)
6747 #9 => vCont s:p1.1;c
6749 The last vCont above would resume thread p1.2 by mistake, because
6750 the server has no idea that the event for p1.2 had not been
6753 The server side must similarly ignore resume actions for the
6754 thread that has a pending %Stopped notification (and any other
6755 threads with events pending), until GDB acks the notification
6756 with vStopped. Otherwise, e.g., the following case is
6759 #1 => g (or any other packet)
6761 #3 <= %Stopped T05 p1.2
6762 #4 => vCont s:p1.1;c
6765 Above, the server must not resume thread p1.2. GDB can't know
6766 that p1.2 stopped until it acks the %Stopped notification, and
6767 since from GDB's perspective all threads should be running, it
6770 Finally, special care must also be given to handling fork/vfork
6771 events. A (v)fork event actually tells us that two processes
6772 stopped -- the parent and the child. Until we follow the fork,
6773 we must not resume the child. Therefore, if we have a pending
6774 fork follow, we must not send a global wildcard resume action
6775 (vCont;c). We can still send process-wide wildcards though. */
6777 /* Start by assuming a global wildcard (vCont;c) is possible. */
6778 bool may_global_wildcard_vcont
= true;
6780 /* And assume every process is individually wildcard-able too. */
6781 for (inferior
*inf
: all_non_exited_inferiors (this))
6783 remote_inferior
*priv
= get_remote_inferior (inf
);
6785 priv
->may_wildcard_vcont
= true;
6788 /* Check for any pending events (not reported or processed yet) and
6789 disable process and global wildcard resumes appropriately. */
6790 check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont
);
6792 bool any_pending_vcont_resume
= false;
6794 for (thread_info
*tp
: all_non_exited_threads (this))
6796 remote_thread_info
*priv
= get_remote_thread_info (tp
);
6798 /* If a thread of a process is not meant to be resumed, then we
6799 can't wildcard that process. */
6800 if (priv
->get_resume_state () == resume_state::NOT_RESUMED
)
6802 get_remote_inferior (tp
->inf
)->may_wildcard_vcont
= false;
6804 /* And if we can't wildcard a process, we can't wildcard
6805 everything either. */
6806 may_global_wildcard_vcont
= false;
6810 if (priv
->get_resume_state () == resume_state::RESUMED_PENDING_VCONT
)
6811 any_pending_vcont_resume
= true;
6813 /* If a thread is the parent of an unfollowed fork, then we
6814 can't do a global wildcard, as that would resume the fork
6816 if (thread_pending_fork_status (tp
) != nullptr)
6817 may_global_wildcard_vcont
= false;
6820 /* We didn't have any resumed thread pending a vCont resume, so nothing to
6822 if (!any_pending_vcont_resume
)
6825 /* Now let's build the vCont packet(s). Actions must be appended
6826 from narrower to wider scopes (thread -> process -> global). If
6827 we end up with too many actions for a single packet vcont_builder
6828 flushes the current vCont packet to the remote side and starts a
6830 struct vcont_builder
vcont_builder (this);
6832 /* Threads first. */
6833 for (thread_info
*tp
: all_non_exited_threads (this))
6835 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6837 /* If the thread was previously vCont-resumed, no need to send a specific
6838 action for it. If we didn't receive a resume request for it, don't
6839 send an action for it either. */
6840 if (remote_thr
->get_resume_state () != resume_state::RESUMED_PENDING_VCONT
)
6843 gdb_assert (!thread_is_in_step_over_chain (tp
));
6845 /* We should never be commit-resuming a thread that has a stop reply.
6846 Otherwise, we would end up reporting a stop event for a thread while
6847 it is running on the remote target. */
6848 remote_state
*rs
= get_remote_state ();
6849 for (const auto &stop_reply
: rs
->stop_reply_queue
)
6850 gdb_assert (stop_reply
->ptid
!= tp
->ptid
);
6852 const resumed_pending_vcont_info
&info
6853 = remote_thr
->resumed_pending_vcont_info ();
6855 /* Check if we need to send a specific action for this thread. If not,
6856 it will be included in a wildcard resume instead. */
6857 if (info
.step
|| info
.sig
!= GDB_SIGNAL_0
6858 || !get_remote_inferior (tp
->inf
)->may_wildcard_vcont
)
6859 vcont_builder
.push_action (tp
->ptid
, info
.step
, info
.sig
);
6861 remote_thr
->set_resumed ();
6864 /* Now check whether we can send any process-wide wildcard. This is
6865 to avoid sending a global wildcard in the case nothing is
6866 supposed to be resumed. */
6867 bool any_process_wildcard
= false;
6869 for (inferior
*inf
: all_non_exited_inferiors (this))
6871 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
6873 any_process_wildcard
= true;
6878 if (any_process_wildcard
)
6880 /* If all processes are wildcard-able, then send a single "c"
6881 action, otherwise, send an "all (-1) threads of process"
6882 continue action for each running process, if any. */
6883 if (may_global_wildcard_vcont
)
6885 vcont_builder
.push_action (minus_one_ptid
,
6886 false, GDB_SIGNAL_0
);
6890 for (inferior
*inf
: all_non_exited_inferiors (this))
6892 if (get_remote_inferior (inf
)->may_wildcard_vcont
)
6894 vcont_builder
.push_action (ptid_t (inf
->pid
),
6895 false, GDB_SIGNAL_0
);
6901 vcont_builder
.flush ();
6904 /* Implementation of target_has_pending_events. */
6907 remote_target::has_pending_events ()
6909 if (target_can_async_p ())
6911 remote_state
*rs
= get_remote_state ();
6913 if (async_event_handler_marked (rs
->remote_async_inferior_event_token
))
6916 /* Note that BUFCNT can be negative, indicating sticky
6918 if (rs
->remote_desc
->bufcnt
!= 0)
6926 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
6927 thread, all threads of a remote process, or all threads of all
6931 remote_target::remote_stop_ns (ptid_t ptid
)
6933 struct remote_state
*rs
= get_remote_state ();
6934 char *p
= rs
->buf
.data ();
6935 char *endp
= p
+ get_remote_packet_size ();
6937 /* If any thread that needs to stop was resumed but pending a vCont
6938 resume, generate a phony stop_reply. However, first check
6939 whether the thread wasn't resumed with a signal. Generating a
6940 phony stop in that case would result in losing the signal. */
6941 bool needs_commit
= false;
6942 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
6944 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6946 if (remote_thr
->get_resume_state ()
6947 == resume_state::RESUMED_PENDING_VCONT
)
6949 const resumed_pending_vcont_info
&info
6950 = remote_thr
->resumed_pending_vcont_info ();
6951 if (info
.sig
!= GDB_SIGNAL_0
)
6953 /* This signal must be forwarded to the inferior. We
6954 could commit-resume just this thread, but its simpler
6955 to just commit-resume everything. */
6956 needs_commit
= true;
6965 for (thread_info
*tp
: all_non_exited_threads (this, ptid
))
6967 remote_thread_info
*remote_thr
= get_remote_thread_info (tp
);
6969 if (remote_thr
->get_resume_state ()
6970 == resume_state::RESUMED_PENDING_VCONT
)
6972 remote_debug_printf ("Enqueueing phony stop reply for thread pending "
6973 "vCont-resume (%d, %ld, %s)", tp
->ptid
.pid(),
6975 pulongest (tp
->ptid
.tid ()));
6977 /* Check that the thread wasn't resumed with a signal.
6978 Generating a phony stop would result in losing the
6980 const resumed_pending_vcont_info
&info
6981 = remote_thr
->resumed_pending_vcont_info ();
6982 gdb_assert (info
.sig
== GDB_SIGNAL_0
);
6984 stop_reply
*sr
= new stop_reply ();
6985 sr
->ptid
= tp
->ptid
;
6987 sr
->ws
.set_stopped (GDB_SIGNAL_0
);
6988 sr
->arch
= tp
->inf
->gdbarch
;
6989 sr
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
6990 sr
->watch_data_address
= 0;
6992 this->push_stop_reply (sr
);
6994 /* Pretend that this thread was actually resumed on the
6995 remote target, then stopped. If we leave it in the
6996 RESUMED_PENDING_VCONT state and the commit_resumed
6997 method is called while the stop reply is still in the
6998 queue, we'll end up reporting a stop event to the core
6999 for that thread while it is running on the remote
7000 target... that would be bad. */
7001 remote_thr
->set_resumed ();
7005 /* FIXME: This supports_vCont_probed check is a workaround until
7006 packet_support is per-connection. */
7007 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
7008 || !rs
->supports_vCont_probed
)
7009 remote_vcont_probe ();
7011 if (!rs
->supports_vCont
.t
)
7012 error (_("Remote server does not support stopping threads"));
7014 if (ptid
== minus_one_ptid
7015 || (!remote_multi_process_p (rs
) && ptid
.is_pid ()))
7016 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
7021 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
7024 /* All (-1) threads of process. */
7025 nptid
= ptid_t (ptid
.pid (), -1);
7028 /* Small optimization: if we already have a stop reply for
7029 this thread, no use in telling the stub we want this
7031 if (peek_stop_reply (ptid
))
7037 write_ptid (p
, endp
, nptid
);
7040 /* In non-stop, we get an immediate OK reply. The stop reply will
7041 come in asynchronously by notification. */
7043 getpkt (&rs
->buf
, 0);
7044 if (strcmp (rs
->buf
.data (), "OK") != 0)
7045 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
).c_str (),
7049 /* All-stop version of target_interrupt. Sends a break or a ^C to
7050 interrupt the remote target. It is undefined which thread of which
7051 process reports the interrupt. */
7054 remote_target::remote_interrupt_as ()
7056 struct remote_state
*rs
= get_remote_state ();
7058 rs
->ctrlc_pending_p
= 1;
7060 /* If the inferior is stopped already, but the core didn't know
7061 about it yet, just ignore the request. The pending stop events
7062 will be collected in remote_wait. */
7063 if (stop_reply_queue_length () > 0)
7066 /* Send interrupt_sequence to remote target. */
7067 send_interrupt_sequence ();
7070 /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
7071 the remote target. It is undefined which thread of which process
7072 reports the interrupt. Throws an error if the packet is not
7073 supported by the server. */
7076 remote_target::remote_interrupt_ns ()
7078 struct remote_state
*rs
= get_remote_state ();
7079 char *p
= rs
->buf
.data ();
7080 char *endp
= p
+ get_remote_packet_size ();
7082 xsnprintf (p
, endp
- p
, "vCtrlC");
7084 /* In non-stop, we get an immediate OK reply. The stop reply will
7085 come in asynchronously by notification. */
7087 getpkt (&rs
->buf
, 0);
7089 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vCtrlC
]))
7093 case PACKET_UNKNOWN
:
7094 error (_("No support for interrupting the remote target."));
7096 error (_("Interrupting target failed: %s"), rs
->buf
.data ());
7100 /* Implement the to_stop function for the remote targets. */
7103 remote_target::stop (ptid_t ptid
)
7105 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7107 if (target_is_non_stop_p ())
7108 remote_stop_ns (ptid
);
7111 /* We don't currently have a way to transparently pause the
7112 remote target in all-stop mode. Interrupt it instead. */
7113 remote_interrupt_as ();
7117 /* Implement the to_interrupt function for the remote targets. */
7120 remote_target::interrupt ()
7122 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7124 if (target_is_non_stop_p ())
7125 remote_interrupt_ns ();
7127 remote_interrupt_as ();
7130 /* Implement the to_pass_ctrlc function for the remote targets. */
7133 remote_target::pass_ctrlc ()
7135 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7137 struct remote_state
*rs
= get_remote_state ();
7139 /* If we're starting up, we're not fully synced yet. Quit
7141 if (rs
->starting_up
)
7143 /* If ^C has already been sent once, offer to disconnect. */
7144 else if (rs
->ctrlc_pending_p
)
7147 target_interrupt ();
7150 /* Ask the user what to do when an interrupt is received. */
7153 remote_target::interrupt_query ()
7155 struct remote_state
*rs
= get_remote_state ();
7157 if (rs
->waiting_for_stop_reply
&& rs
->ctrlc_pending_p
)
7159 if (query (_("The target is not responding to interrupt requests.\n"
7160 "Stop debugging it? ")))
7162 remote_unpush_target (this);
7163 throw_error (TARGET_CLOSE_ERROR
, _("Disconnected from target."));
7168 if (query (_("Interrupted while waiting for the program.\n"
7169 "Give up waiting? ")))
7174 /* Enable/disable target terminal ownership. Most targets can use
7175 terminal groups to control terminal ownership. Remote targets are
7176 different in that explicit transfer of ownership to/from GDB/target
7180 remote_target::terminal_inferior ()
7182 /* NOTE: At this point we could also register our selves as the
7183 recipient of all input. Any characters typed could then be
7184 passed on down to the target. */
7188 remote_target::terminal_ours ()
7193 remote_console_output (const char *msg
)
7197 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
7200 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
7204 gdb_stdtarg
->puts (tb
);
7206 gdb_stdtarg
->flush ();
7209 /* Return the length of the stop reply queue. */
7212 remote_target::stop_reply_queue_length ()
7214 remote_state
*rs
= get_remote_state ();
7215 return rs
->stop_reply_queue
.size ();
7219 remote_notif_stop_parse (remote_target
*remote
,
7220 struct notif_client
*self
, const char *buf
,
7221 struct notif_event
*event
)
7223 remote
->remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
7227 remote_notif_stop_ack (remote_target
*remote
,
7228 struct notif_client
*self
, const char *buf
,
7229 struct notif_event
*event
)
7231 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
7234 putpkt (remote
, self
->ack_command
);
7236 /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
7237 the notification. It was left in the queue because we need to
7238 acknowledge it and pull the rest of the notifications out. */
7239 if (stop_reply
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7240 remote
->push_stop_reply (stop_reply
);
7244 remote_notif_stop_can_get_pending_events (remote_target
*remote
,
7245 struct notif_client
*self
)
7247 /* We can't get pending events in remote_notif_process for
7248 notification stop, and we have to do this in remote_wait_ns
7249 instead. If we fetch all queued events from stub, remote stub
7250 may exit and we have no chance to process them back in
7252 remote_state
*rs
= remote
->get_remote_state ();
7253 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7257 stop_reply::~stop_reply ()
7259 for (cached_reg_t
®
: regcache
)
7263 static notif_event_up
7264 remote_notif_stop_alloc_reply ()
7266 return notif_event_up (new struct stop_reply ());
7269 /* A client of notification Stop. */
7271 struct notif_client notif_client_stop
=
7275 remote_notif_stop_parse
,
7276 remote_notif_stop_ack
,
7277 remote_notif_stop_can_get_pending_events
,
7278 remote_notif_stop_alloc_reply
,
7282 /* If CONTEXT contains any fork child threads that have not been
7283 reported yet, remove them from the CONTEXT list. If such a
7284 thread exists it is because we are stopped at a fork catchpoint
7285 and have not yet called follow_fork, which will set up the
7286 host-side data structures for the new process. */
7289 remote_target::remove_new_fork_children (threads_listing_context
*context
)
7291 struct notif_client
*notif
= ¬if_client_stop
;
7293 /* For any threads stopped at a fork event, remove the corresponding
7294 fork child threads from the CONTEXT list. */
7295 for (thread_info
*thread
: all_non_exited_threads (this))
7297 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
7302 context
->remove_thread (ws
->child_ptid ());
7305 /* Check for any pending fork events (not reported or processed yet)
7306 in process PID and remove those fork child threads from the
7307 CONTEXT list as well. */
7308 remote_notif_get_pending_events (notif
);
7309 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7310 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7311 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
7312 || event
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
7313 context
->remove_thread (event
->ws
.child_ptid ());
7316 /* Check whether any event pending in the vStopped queue would prevent a
7317 global or process wildcard vCont action. Set *may_global_wildcard to
7318 false if we can't do a global wildcard (vCont;c), and clear the event
7319 inferior's may_wildcard_vcont flag if we can't do a process-wide
7320 wildcard resume (vCont;c:pPID.-1). */
7323 remote_target::check_pending_events_prevent_wildcard_vcont
7324 (bool *may_global_wildcard
)
7326 struct notif_client
*notif
= ¬if_client_stop
;
7328 remote_notif_get_pending_events (notif
);
7329 for (auto &event
: get_remote_state ()->stop_reply_queue
)
7331 if (event
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
7332 || event
->ws
.kind () == TARGET_WAITKIND_NO_HISTORY
)
7335 if (event
->ws
.kind () == TARGET_WAITKIND_FORKED
7336 || event
->ws
.kind () == TARGET_WAITKIND_VFORKED
)
7337 *may_global_wildcard
= false;
7339 /* This may be the first time we heard about this process.
7340 Regardless, we must not do a global wildcard resume, otherwise
7341 we'd resume this process too. */
7342 *may_global_wildcard
= false;
7343 if (event
->ptid
!= null_ptid
)
7345 inferior
*inf
= find_inferior_ptid (this, event
->ptid
);
7347 get_remote_inferior (inf
)->may_wildcard_vcont
= false;
7352 /* Discard all pending stop replies of inferior INF. */
7355 remote_target::discard_pending_stop_replies (struct inferior
*inf
)
7357 struct stop_reply
*reply
;
7358 struct remote_state
*rs
= get_remote_state ();
7359 struct remote_notif_state
*rns
= rs
->notif_state
;
7361 /* This function can be notified when an inferior exists. When the
7362 target is not remote, the notification state is NULL. */
7363 if (rs
->remote_desc
== NULL
)
7366 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
7368 /* Discard the in-flight notification. */
7369 if (reply
!= NULL
&& reply
->ptid
.pid () == inf
->pid
)
7371 /* Leave the notification pending, since the server expects that
7372 we acknowledge it with vStopped. But clear its contents, so
7373 that later on when we acknowledge it, we also discard it. */
7375 ("discarding in-flight notification: ptid: %s, ws: %s\n",
7376 reply
->ptid
.to_string().c_str(),
7377 reply
->ws
.to_string ().c_str ());
7378 reply
->ws
.set_ignore ();
7381 /* Discard the stop replies we have already pulled with
7383 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7384 rs
->stop_reply_queue
.end (),
7385 [=] (const stop_reply_up
&event
)
7387 return event
->ptid
.pid () == inf
->pid
;
7389 for (auto it
= iter
; it
!= rs
->stop_reply_queue
.end (); ++it
)
7391 ("discarding queued stop reply: ptid: %s, ws: %s\n",
7392 reply
->ptid
.to_string().c_str(),
7393 reply
->ws
.to_string ().c_str ());
7394 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7397 /* Discard the stop replies for RS in stop_reply_queue. */
7400 remote_target::discard_pending_stop_replies_in_queue ()
7402 remote_state
*rs
= get_remote_state ();
7404 /* Discard the stop replies we have already pulled with
7406 auto iter
= std::remove_if (rs
->stop_reply_queue
.begin (),
7407 rs
->stop_reply_queue
.end (),
7408 [=] (const stop_reply_up
&event
)
7410 return event
->rs
== rs
;
7412 rs
->stop_reply_queue
.erase (iter
, rs
->stop_reply_queue
.end ());
7415 /* Remove the first reply in 'stop_reply_queue' which matches
7419 remote_target::remote_notif_remove_queued_reply (ptid_t ptid
)
7421 remote_state
*rs
= get_remote_state ();
7423 auto iter
= std::find_if (rs
->stop_reply_queue
.begin (),
7424 rs
->stop_reply_queue
.end (),
7425 [=] (const stop_reply_up
&event
)
7427 return event
->ptid
.matches (ptid
);
7429 struct stop_reply
*result
;
7430 if (iter
== rs
->stop_reply_queue
.end ())
7434 result
= iter
->release ();
7435 rs
->stop_reply_queue
.erase (iter
);
7439 fprintf_unfiltered (gdb_stdlog
,
7440 "notif: discard queued event: 'Stop' in %s\n",
7441 target_pid_to_str (ptid
).c_str ());
7446 /* Look for a queued stop reply belonging to PTID. If one is found,
7447 remove it from the queue, and return it. Returns NULL if none is
7448 found. If there are still queued events left to process, tell the
7449 event loop to get back to target_wait soon. */
7452 remote_target::queued_stop_reply (ptid_t ptid
)
7454 remote_state
*rs
= get_remote_state ();
7455 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
7457 if (!rs
->stop_reply_queue
.empty () && target_can_async_p ())
7459 /* There's still at least an event left. */
7460 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7466 /* Push a fully parsed stop reply in the stop reply queue. Since we
7467 know that we now have at least one queued event left to pass to the
7468 core side, tell the event loop to get back to target_wait soon. */
7471 remote_target::push_stop_reply (struct stop_reply
*new_event
)
7473 remote_state
*rs
= get_remote_state ();
7474 rs
->stop_reply_queue
.push_back (stop_reply_up (new_event
));
7477 fprintf_unfiltered (gdb_stdlog
,
7478 "notif: push 'Stop' %s to queue %d\n",
7479 target_pid_to_str (new_event
->ptid
).c_str (),
7480 int (rs
->stop_reply_queue
.size ()));
7482 /* Mark the pending event queue only if async mode is currently enabled.
7483 If async mode is not currently enabled, then, if it later becomes
7484 enabled, and there are events in this queue, we will mark the event
7485 token at that point, see remote_target::async. */
7486 if (target_is_async_p ())
7487 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
7490 /* Returns true if we have a stop reply for PTID. */
7493 remote_target::peek_stop_reply (ptid_t ptid
)
7495 remote_state
*rs
= get_remote_state ();
7496 for (auto &event
: rs
->stop_reply_queue
)
7497 if (ptid
== event
->ptid
7498 && event
->ws
.kind () == TARGET_WAITKIND_STOPPED
)
7503 /* Helper for remote_parse_stop_reply. Return nonzero if the substring
7504 starting with P and ending with PEND matches PREFIX. */
7507 strprefix (const char *p
, const char *pend
, const char *prefix
)
7509 for ( ; p
< pend
; p
++, prefix
++)
7512 return *prefix
== '\0';
7515 /* Parse the stop reply in BUF. Either the function succeeds, and the
7516 result is stored in EVENT, or throws an error. */
7519 remote_target::remote_parse_stop_reply (const char *buf
, stop_reply
*event
)
7521 remote_arch_state
*rsa
= NULL
;
7526 event
->ptid
= null_ptid
;
7527 event
->rs
= get_remote_state ();
7528 event
->ws
.set_ignore ();
7529 event
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
7530 event
->regcache
.clear ();
7535 case 'T': /* Status with PC, SP, FP, ... */
7536 /* Expedited reply, containing Signal, {regno, reg} repeat. */
7537 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
7539 n... = register number
7540 r... = register contents
7543 p
= &buf
[3]; /* after Txx */
7549 p1
= strchr (p
, ':');
7551 error (_("Malformed packet(a) (missing colon): %s\n\
7555 error (_("Malformed packet(a) (missing register number): %s\n\
7559 /* Some "registers" are actually extended stop information.
7560 Note if you're adding a new entry here: GDB 7.9 and
7561 earlier assume that all register "numbers" that start
7562 with an hex digit are real register numbers. Make sure
7563 the server only sends such a packet if it knows the
7564 client understands it. */
7566 if (strprefix (p
, p1
, "thread"))
7567 event
->ptid
= read_ptid (++p1
, &p
);
7568 else if (strprefix (p
, p1
, "syscall_entry"))
7572 p
= unpack_varlen_hex (++p1
, &sysno
);
7573 event
->ws
.set_syscall_entry ((int) sysno
);
7575 else if (strprefix (p
, p1
, "syscall_return"))
7579 p
= unpack_varlen_hex (++p1
, &sysno
);
7580 event
->ws
.set_syscall_return ((int) sysno
);
7582 else if (strprefix (p
, p1
, "watch")
7583 || strprefix (p
, p1
, "rwatch")
7584 || strprefix (p
, p1
, "awatch"))
7586 event
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
7587 p
= unpack_varlen_hex (++p1
, &addr
);
7588 event
->watch_data_address
= (CORE_ADDR
) addr
;
7590 else if (strprefix (p
, p1
, "swbreak"))
7592 event
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
7594 /* Make sure the stub doesn't forget to indicate support
7596 if (packet_support (PACKET_swbreak_feature
) != PACKET_ENABLE
)
7597 error (_("Unexpected swbreak stop reason"));
7599 /* The value part is documented as "must be empty",
7600 though we ignore it, in case we ever decide to make
7601 use of it in a backward compatible way. */
7602 p
= strchrnul (p1
+ 1, ';');
7604 else if (strprefix (p
, p1
, "hwbreak"))
7606 event
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
7608 /* Make sure the stub doesn't forget to indicate support
7610 if (packet_support (PACKET_hwbreak_feature
) != PACKET_ENABLE
)
7611 error (_("Unexpected hwbreak stop reason"));
7614 p
= strchrnul (p1
+ 1, ';');
7616 else if (strprefix (p
, p1
, "library"))
7618 event
->ws
.set_loaded ();
7619 p
= strchrnul (p1
+ 1, ';');
7621 else if (strprefix (p
, p1
, "replaylog"))
7623 event
->ws
.set_no_history ();
7624 /* p1 will indicate "begin" or "end", but it makes
7625 no difference for now, so ignore it. */
7626 p
= strchrnul (p1
+ 1, ';');
7628 else if (strprefix (p
, p1
, "core"))
7632 p
= unpack_varlen_hex (++p1
, &c
);
7635 else if (strprefix (p
, p1
, "fork"))
7636 event
->ws
.set_forked (read_ptid (++p1
, &p
));
7637 else if (strprefix (p
, p1
, "vfork"))
7638 event
->ws
.set_vforked (read_ptid (++p1
, &p
));
7639 else if (strprefix (p
, p1
, "vforkdone"))
7641 event
->ws
.set_vfork_done ();
7642 p
= strchrnul (p1
+ 1, ';');
7644 else if (strprefix (p
, p1
, "exec"))
7649 /* Determine the length of the execd pathname. */
7650 p
= unpack_varlen_hex (++p1
, &ignored
);
7651 pathlen
= (p
- p1
) / 2;
7653 /* Save the pathname for event reporting and for
7654 the next run command. */
7655 gdb::unique_xmalloc_ptr
<char> pathname
7656 ((char *) xmalloc (pathlen
+ 1));
7657 hex2bin (p1
, (gdb_byte
*) pathname
.get (), pathlen
);
7658 pathname
.get ()[pathlen
] = '\0';
7660 /* This is freed during event handling. */
7661 event
->ws
.set_execd (std::move (pathname
));
7663 /* Skip the registers included in this packet, since
7664 they may be for an architecture different from the
7665 one used by the original program. */
7668 else if (strprefix (p
, p1
, "create"))
7670 event
->ws
.set_thread_created ();
7671 p
= strchrnul (p1
+ 1, ';');
7680 p
= strchrnul (p1
+ 1, ';');
7685 /* Maybe a real ``P'' register number. */
7686 p_temp
= unpack_varlen_hex (p
, &pnum
);
7687 /* If the first invalid character is the colon, we got a
7688 register number. Otherwise, it's an unknown stop
7692 /* If we haven't parsed the event's thread yet, find
7693 it now, in order to find the architecture of the
7694 reported expedited registers. */
7695 if (event
->ptid
== null_ptid
)
7697 /* If there is no thread-id information then leave
7698 the event->ptid as null_ptid. Later in
7699 process_stop_reply we will pick a suitable
7701 const char *thr
= strstr (p1
+ 1, ";thread:");
7703 event
->ptid
= read_ptid (thr
+ strlen (";thread:"),
7710 = (event
->ptid
== null_ptid
7712 : find_inferior_ptid (this, event
->ptid
));
7713 /* If this is the first time we learn anything
7714 about this process, skip the registers
7715 included in this packet, since we don't yet
7716 know which architecture to use to parse them.
7717 We'll determine the architecture later when
7718 we process the stop reply and retrieve the
7719 target description, via
7720 remote_notice_new_inferior ->
7721 post_create_inferior. */
7724 p
= strchrnul (p1
+ 1, ';');
7729 event
->arch
= inf
->gdbarch
;
7730 rsa
= event
->rs
->get_remote_arch_state (event
->arch
);
7734 = packet_reg_from_pnum (event
->arch
, rsa
, pnum
);
7735 cached_reg_t cached_reg
;
7738 error (_("Remote sent bad register number %s: %s\n\
7740 hex_string (pnum
), p
, buf
);
7742 cached_reg
.num
= reg
->regnum
;
7743 cached_reg
.data
= (gdb_byte
*)
7744 xmalloc (register_size (event
->arch
, reg
->regnum
));
7747 fieldsize
= hex2bin (p
, cached_reg
.data
,
7748 register_size (event
->arch
, reg
->regnum
));
7750 if (fieldsize
< register_size (event
->arch
, reg
->regnum
))
7751 warning (_("Remote reply is too short: %s"), buf
);
7753 event
->regcache
.push_back (cached_reg
);
7757 /* Not a number. Silently skip unknown optional
7759 p
= strchrnul (p1
+ 1, ';');
7764 error (_("Remote register badly formatted: %s\nhere: %s"),
7769 if (event
->ws
.kind () != TARGET_WAITKIND_IGNORE
)
7773 case 'S': /* Old style status, just signal only. */
7777 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
7778 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
7779 event
->ws
.set_stopped ((enum gdb_signal
) sig
);
7781 event
->ws
.set_stopped (GDB_SIGNAL_UNKNOWN
);
7784 case 'w': /* Thread exited. */
7788 p
= unpack_varlen_hex (&buf
[1], &value
);
7789 event
->ws
.set_thread_exited (value
);
7791 error (_("stop reply packet badly formatted: %s"), buf
);
7792 event
->ptid
= read_ptid (++p
, NULL
);
7795 case 'W': /* Target exited. */
7800 /* GDB used to accept only 2 hex chars here. Stubs should
7801 only send more if they detect GDB supports multi-process
7803 p
= unpack_varlen_hex (&buf
[1], &value
);
7807 /* The remote process exited. */
7808 event
->ws
.set_exited (value
);
7812 /* The remote process exited with a signal. */
7813 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
7814 event
->ws
.set_signalled ((enum gdb_signal
) value
);
7816 event
->ws
.set_signalled (GDB_SIGNAL_UNKNOWN
);
7819 /* If no process is specified, return null_ptid, and let the
7820 caller figure out the right process to use. */
7830 else if (startswith (p
, "process:"))
7834 p
+= sizeof ("process:") - 1;
7835 unpack_varlen_hex (p
, &upid
);
7839 error (_("unknown stop reply packet: %s"), buf
);
7842 error (_("unknown stop reply packet: %s"), buf
);
7843 event
->ptid
= ptid_t (pid
);
7847 event
->ws
.set_no_resumed ();
7848 event
->ptid
= minus_one_ptid
;
7853 /* When the stub wants to tell GDB about a new notification reply, it
7854 sends a notification (%Stop, for example). Those can come it at
7855 any time, hence, we have to make sure that any pending
7856 putpkt/getpkt sequence we're making is finished, before querying
7857 the stub for more events with the corresponding ack command
7858 (vStopped, for example). E.g., if we started a vStopped sequence
7859 immediately upon receiving the notification, something like this
7867 1.6) <-- (registers reply to step #1.3)
7869 Obviously, the reply in step #1.6 would be unexpected to a vStopped
7872 To solve this, whenever we parse a %Stop notification successfully,
7873 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
7874 doing whatever we were doing:
7880 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
7881 2.5) <-- (registers reply to step #2.3)
7883 Eventually after step #2.5, we return to the event loop, which
7884 notices there's an event on the
7885 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
7886 associated callback --- the function below. At this point, we're
7887 always safe to start a vStopped sequence. :
7890 2.7) <-- T05 thread:2
7896 remote_target::remote_notif_get_pending_events (notif_client
*nc
)
7898 struct remote_state
*rs
= get_remote_state ();
7900 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
7903 fprintf_unfiltered (gdb_stdlog
,
7904 "notif: process: '%s' ack pending event\n",
7908 nc
->ack (this, nc
, rs
->buf
.data (),
7909 rs
->notif_state
->pending_event
[nc
->id
]);
7910 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
7914 getpkt (&rs
->buf
, 0);
7915 if (strcmp (rs
->buf
.data (), "OK") == 0)
7918 remote_notif_ack (this, nc
, rs
->buf
.data ());
7924 fprintf_unfiltered (gdb_stdlog
,
7925 "notif: process: '%s' no pending reply\n",
7930 /* Wrapper around remote_target::remote_notif_get_pending_events to
7931 avoid having to export the whole remote_target class. */
7934 remote_notif_get_pending_events (remote_target
*remote
, notif_client
*nc
)
7936 remote
->remote_notif_get_pending_events (nc
);
7939 /* Called from process_stop_reply when the stop packet we are responding
7940 to didn't include a process-id or thread-id. STATUS is the stop event
7941 we are responding to.
7943 It is the task of this function to select a suitable thread (or process)
7944 and return its ptid, this is the thread (or process) we will assume the
7945 stop event came from.
7947 In some cases there isn't really any choice about which thread (or
7948 process) is selected, a basic remote with a single process containing a
7949 single thread might choose not to send any process-id or thread-id in
7950 its stop packets, this function will select and return the one and only
7953 However, if a target supports multiple threads (or processes) and still
7954 doesn't include a thread-id (or process-id) in its stop packet then
7955 first, this is a badly behaving target, and second, we're going to have
7956 to select a thread (or process) at random and use that. This function
7957 will print a warning to the user if it detects that there is the
7958 possibility that GDB is guessing which thread (or process) to
7961 Note that this is called before GDB fetches the updated thread list from the
7962 target. So it's possible for the stop reply to be ambiguous and for GDB to
7963 not realize it. For example, if there's initially one thread, the target
7964 spawns a second thread, and then sends a stop reply without an id that
7965 concerns the first thread. GDB will assume the stop reply is about the
7966 first thread - the only thread it knows about - without printing a warning.
7967 Anyway, if the remote meant for the stop reply to be about the second thread,
7968 then it would be really broken, because GDB doesn't know about that thread
7972 remote_target::select_thread_for_ambiguous_stop_reply
7973 (const target_waitstatus
&status
)
7975 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
7977 /* Some stop events apply to all threads in an inferior, while others
7978 only apply to a single thread. */
7979 bool process_wide_stop
7980 = (status
.kind () == TARGET_WAITKIND_EXITED
7981 || status
.kind () == TARGET_WAITKIND_SIGNALLED
);
7983 remote_debug_printf ("process_wide_stop = %d", process_wide_stop
);
7985 thread_info
*first_resumed_thread
= nullptr;
7986 bool ambiguous
= false;
7988 /* Consider all non-exited threads of the target, find the first resumed
7990 for (thread_info
*thr
: all_non_exited_threads (this))
7992 remote_thread_info
*remote_thr
= get_remote_thread_info (thr
);
7994 if (remote_thr
->get_resume_state () != resume_state::RESUMED
)
7997 if (first_resumed_thread
== nullptr)
7998 first_resumed_thread
= thr
;
7999 else if (!process_wide_stop
8000 || first_resumed_thread
->ptid
.pid () != thr
->ptid
.pid ())
8004 gdb_assert (first_resumed_thread
!= nullptr);
8006 remote_debug_printf ("first resumed thread is %s",
8007 pid_to_str (first_resumed_thread
->ptid
).c_str ());
8008 remote_debug_printf ("is this guess ambiguous? = %d", ambiguous
);
8010 /* Warn if the remote target is sending ambiguous stop replies. */
8013 static bool warned
= false;
8017 /* If you are seeing this warning then the remote target has
8018 stopped without specifying a thread-id, but the target
8019 does have multiple threads (or inferiors), and so GDB is
8020 having to guess which thread stopped.
8022 Examples of what might cause this are the target sending
8023 and 'S' stop packet, or a 'T' stop packet and not
8024 including a thread-id.
8026 Additionally, the target might send a 'W' or 'X packet
8027 without including a process-id, when the target has
8028 multiple running inferiors. */
8029 if (process_wide_stop
)
8030 warning (_("multi-inferior target stopped without "
8031 "sending a process-id, using first "
8032 "non-exited inferior"));
8034 warning (_("multi-threaded target stopped without "
8035 "sending a thread-id, using first "
8036 "non-exited thread"));
8041 /* If this is a stop for all threads then don't use a particular threads
8042 ptid, instead create a new ptid where only the pid field is set. */
8043 if (process_wide_stop
)
8044 return ptid_t (first_resumed_thread
->ptid
.pid ());
8046 return first_resumed_thread
->ptid
;
8049 /* Called when it is decided that STOP_REPLY holds the info of the
8050 event that is to be returned to the core. This function always
8051 destroys STOP_REPLY. */
8054 remote_target::process_stop_reply (struct stop_reply
*stop_reply
,
8055 struct target_waitstatus
*status
)
8057 *status
= stop_reply
->ws
;
8058 ptid_t ptid
= stop_reply
->ptid
;
8060 /* If no thread/process was reported by the stub then select a suitable
8062 if (ptid
== null_ptid
)
8063 ptid
= select_thread_for_ambiguous_stop_reply (*status
);
8064 gdb_assert (ptid
!= null_ptid
);
8066 if (status
->kind () != TARGET_WAITKIND_EXITED
8067 && status
->kind () != TARGET_WAITKIND_SIGNALLED
8068 && status
->kind () != TARGET_WAITKIND_NO_RESUMED
)
8070 /* Expedited registers. */
8071 if (!stop_reply
->regcache
.empty ())
8073 struct regcache
*regcache
8074 = get_thread_arch_regcache (this, ptid
, stop_reply
->arch
);
8076 for (cached_reg_t
®
: stop_reply
->regcache
)
8078 regcache
->raw_supply (reg
.num
, reg
.data
);
8082 stop_reply
->regcache
.clear ();
8085 remote_notice_new_inferior (ptid
, false);
8086 remote_thread_info
*remote_thr
= get_remote_thread_info (this, ptid
);
8087 remote_thr
->core
= stop_reply
->core
;
8088 remote_thr
->stop_reason
= stop_reply
->stop_reason
;
8089 remote_thr
->watch_data_address
= stop_reply
->watch_data_address
;
8091 if (target_is_non_stop_p ())
8093 /* If the target works in non-stop mode, a stop-reply indicates that
8094 only this thread stopped. */
8095 remote_thr
->set_not_resumed ();
8099 /* If the target works in all-stop mode, a stop-reply indicates that
8100 all the target's threads stopped. */
8101 for (thread_info
*tp
: all_non_exited_threads (this))
8102 get_remote_thread_info (tp
)->set_not_resumed ();
8110 /* The non-stop mode version of target_wait. */
8113 remote_target::wait_ns (ptid_t ptid
, struct target_waitstatus
*status
,
8114 target_wait_flags options
)
8116 struct remote_state
*rs
= get_remote_state ();
8117 struct stop_reply
*stop_reply
;
8121 /* If in non-stop mode, get out of getpkt even if a
8122 notification is received. */
8124 ret
= getpkt_or_notif_sane (&rs
->buf
, 0 /* forever */, &is_notif
);
8127 if (ret
!= -1 && !is_notif
)
8130 case 'E': /* Error of some sort. */
8131 /* We're out of sync with the target now. Did it continue
8132 or not? We can't tell which thread it was in non-stop,
8133 so just ignore this. */
8134 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
8136 case 'O': /* Console output. */
8137 remote_console_output (&rs
->buf
[1]);
8140 warning (_("Invalid remote reply: %s"), rs
->buf
.data ());
8144 /* Acknowledge a pending stop reply that may have arrived in the
8146 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
8147 remote_notif_get_pending_events (¬if_client_stop
);
8149 /* If indeed we noticed a stop reply, we're done. */
8150 stop_reply
= queued_stop_reply (ptid
);
8151 if (stop_reply
!= NULL
)
8152 return process_stop_reply (stop_reply
, status
);
8154 /* Still no event. If we're just polling for an event, then
8155 return to the event loop. */
8156 if (options
& TARGET_WNOHANG
)
8158 status
->set_ignore ();
8159 return minus_one_ptid
;
8162 /* Otherwise do a blocking wait. */
8163 ret
= getpkt_or_notif_sane (&rs
->buf
, 1 /* forever */, &is_notif
);
8167 /* Return the first resumed thread. */
8170 first_remote_resumed_thread (remote_target
*target
)
8172 for (thread_info
*tp
: all_non_exited_threads (target
, minus_one_ptid
))
8178 /* Wait until the remote machine stops, then return, storing status in
8179 STATUS just as `wait' would. */
8182 remote_target::wait_as (ptid_t ptid
, target_waitstatus
*status
,
8183 target_wait_flags options
)
8185 struct remote_state
*rs
= get_remote_state ();
8186 ptid_t event_ptid
= null_ptid
;
8188 struct stop_reply
*stop_reply
;
8192 status
->set_ignore ();
8194 stop_reply
= queued_stop_reply (ptid
);
8195 if (stop_reply
!= NULL
)
8197 /* None of the paths that push a stop reply onto the queue should
8198 have set the waiting_for_stop_reply flag. */
8199 gdb_assert (!rs
->waiting_for_stop_reply
);
8200 event_ptid
= process_stop_reply (stop_reply
, status
);
8204 int forever
= ((options
& TARGET_WNOHANG
) == 0
8205 && rs
->wait_forever_enabled_p
);
8207 if (!rs
->waiting_for_stop_reply
)
8209 status
->set_no_resumed ();
8210 return minus_one_ptid
;
8213 /* FIXME: cagney/1999-09-27: If we're in async mode we should
8214 _never_ wait for ever -> test on target_is_async_p().
8215 However, before we do that we need to ensure that the caller
8216 knows how to take the target into/out of async mode. */
8218 int ret
= getpkt_or_notif_sane (&rs
->buf
, forever
, &is_notif
);
8220 /* GDB gets a notification. Return to core as this event is
8222 if (ret
!= -1 && is_notif
)
8223 return minus_one_ptid
;
8225 if (ret
== -1 && (options
& TARGET_WNOHANG
) != 0)
8226 return minus_one_ptid
;
8228 buf
= rs
->buf
.data ();
8230 /* Assume that the target has acknowledged Ctrl-C unless we receive
8231 an 'F' or 'O' packet. */
8232 if (buf
[0] != 'F' && buf
[0] != 'O')
8233 rs
->ctrlc_pending_p
= 0;
8237 case 'E': /* Error of some sort. */
8238 /* We're out of sync with the target now. Did it continue or
8239 not? Not is more likely, so report a stop. */
8240 rs
->waiting_for_stop_reply
= 0;
8242 warning (_("Remote failure reply: %s"), buf
);
8243 status
->set_stopped (GDB_SIGNAL_0
);
8245 case 'F': /* File-I/O request. */
8246 /* GDB may access the inferior memory while handling the File-I/O
8247 request, but we don't want GDB accessing memory while waiting
8248 for a stop reply. See the comments in putpkt_binary. Set
8249 waiting_for_stop_reply to 0 temporarily. */
8250 rs
->waiting_for_stop_reply
= 0;
8251 remote_fileio_request (this, buf
, rs
->ctrlc_pending_p
);
8252 rs
->ctrlc_pending_p
= 0;
8253 /* GDB handled the File-I/O request, and the target is running
8254 again. Keep waiting for events. */
8255 rs
->waiting_for_stop_reply
= 1;
8257 case 'N': case 'T': case 'S': case 'X': case 'W':
8259 /* There is a stop reply to handle. */
8260 rs
->waiting_for_stop_reply
= 0;
8263 = (struct stop_reply
*) remote_notif_parse (this,
8267 event_ptid
= process_stop_reply (stop_reply
, status
);
8270 case 'O': /* Console output. */
8271 remote_console_output (buf
+ 1);
8274 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
8276 /* Zero length reply means that we tried 'S' or 'C' and the
8277 remote system doesn't support it. */
8278 target_terminal::ours_for_output ();
8280 ("Can't send signals to this remote system. %s not sent.\n",
8281 gdb_signal_to_name (rs
->last_sent_signal
));
8282 rs
->last_sent_signal
= GDB_SIGNAL_0
;
8283 target_terminal::inferior ();
8285 strcpy (buf
, rs
->last_sent_step
? "s" : "c");
8291 warning (_("Invalid remote reply: %s"), buf
);
8296 if (status
->kind () == TARGET_WAITKIND_NO_RESUMED
)
8297 return minus_one_ptid
;
8298 else if (status
->kind () == TARGET_WAITKIND_IGNORE
)
8300 /* Nothing interesting happened. If we're doing a non-blocking
8301 poll, we're done. Otherwise, go back to waiting. */
8302 if (options
& TARGET_WNOHANG
)
8303 return minus_one_ptid
;
8307 else if (status
->kind () != TARGET_WAITKIND_EXITED
8308 && status
->kind () != TARGET_WAITKIND_SIGNALLED
)
8310 if (event_ptid
!= null_ptid
)
8311 record_currthread (rs
, event_ptid
);
8313 event_ptid
= first_remote_resumed_thread (this);
8317 /* A process exit. Invalidate our notion of current thread. */
8318 record_currthread (rs
, minus_one_ptid
);
8319 /* It's possible that the packet did not include a pid. */
8320 if (event_ptid
== null_ptid
)
8321 event_ptid
= first_remote_resumed_thread (this);
8322 /* EVENT_PTID could still be NULL_PTID. Double-check. */
8323 if (event_ptid
== null_ptid
)
8324 event_ptid
= magic_null_ptid
;
8330 /* Wait until the remote machine stops, then return, storing status in
8331 STATUS just as `wait' would. */
8334 remote_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
8335 target_wait_flags options
)
8337 REMOTE_SCOPED_DEBUG_ENTER_EXIT
;
8339 remote_state
*rs
= get_remote_state ();
8341 /* Start by clearing the flag that asks for our wait method to be called,
8342 we'll mark it again at the end if needed. If the target is not in
8343 async mode then the async token should not be marked. */
8344 if (target_is_async_p ())
8345 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
8347 gdb_assert (!async_event_handler_marked
8348 (rs
->remote_async_inferior_event_token
));
8352 if (target_is_non_stop_p ())
8353 event_ptid
= wait_ns (ptid
, status
, options
);
8355 event_ptid
= wait_as (ptid
, status
, options
);
8357 if (target_is_async_p ())
8359 /* If there are events left in the queue, or unacknowledged
8360 notifications, then tell the event loop to call us again. */
8361 if (!rs
->stop_reply_queue
.empty ()
8362 || rs
->notif_state
->pending_event
[notif_client_stop
.id
] != nullptr)
8363 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
8369 /* Fetch a single register using a 'p' packet. */
8372 remote_target::fetch_register_using_p (struct regcache
*regcache
,
8375 struct gdbarch
*gdbarch
= regcache
->arch ();
8376 struct remote_state
*rs
= get_remote_state ();
8378 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8381 if (packet_support (PACKET_p
) == PACKET_DISABLE
)
8384 if (reg
->pnum
== -1)
8387 p
= rs
->buf
.data ();
8389 p
+= hexnumstr (p
, reg
->pnum
);
8392 getpkt (&rs
->buf
, 0);
8394 buf
= rs
->buf
.data ();
8396 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_p
]))
8400 case PACKET_UNKNOWN
:
8403 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
8404 gdbarch_register_name (regcache
->arch (),
8409 /* If this register is unfetchable, tell the regcache. */
8412 regcache
->raw_supply (reg
->regnum
, NULL
);
8416 /* Otherwise, parse and supply the value. */
8422 error (_("fetch_register_using_p: early buf termination"));
8424 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8427 regcache
->raw_supply (reg
->regnum
, regp
);
8431 /* Fetch the registers included in the target's 'g' packet. */
8434 remote_target::send_g_packet ()
8436 struct remote_state
*rs
= get_remote_state ();
8439 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "g");
8441 getpkt (&rs
->buf
, 0);
8442 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8443 error (_("Could not read registers; remote failure reply '%s'"),
8446 /* We can get out of synch in various cases. If the first character
8447 in the buffer is not a hex character, assume that has happened
8448 and try to fetch another packet to read. */
8449 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
8450 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
8451 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
8452 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
8454 remote_debug_printf ("Bad register packet; fetching a new packet");
8455 getpkt (&rs
->buf
, 0);
8458 buf_len
= strlen (rs
->buf
.data ());
8460 /* Sanity check the received packet. */
8461 if (buf_len
% 2 != 0)
8462 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
.data ());
8468 remote_target::process_g_packet (struct regcache
*regcache
)
8470 struct gdbarch
*gdbarch
= regcache
->arch ();
8471 struct remote_state
*rs
= get_remote_state ();
8472 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8477 buf_len
= strlen (rs
->buf
.data ());
8479 /* Further sanity checks, with knowledge of the architecture. */
8480 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
8481 error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
8483 rsa
->sizeof_g_packet
, buf_len
/ 2,
8486 /* Save the size of the packet sent to us by the target. It is used
8487 as a heuristic when determining the max size of packets that the
8488 target can safely receive. */
8489 if (rsa
->actual_register_packet_size
== 0)
8490 rsa
->actual_register_packet_size
= buf_len
;
8492 /* If this is smaller than we guessed the 'g' packet would be,
8493 update our records. A 'g' reply that doesn't include a register's
8494 value implies either that the register is not available, or that
8495 the 'p' packet must be used. */
8496 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
8498 long sizeof_g_packet
= buf_len
/ 2;
8500 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8502 long offset
= rsa
->regs
[i
].offset
;
8503 long reg_size
= register_size (gdbarch
, i
);
8505 if (rsa
->regs
[i
].pnum
== -1)
8508 if (offset
>= sizeof_g_packet
)
8509 rsa
->regs
[i
].in_g_packet
= 0;
8510 else if (offset
+ reg_size
> sizeof_g_packet
)
8511 error (_("Truncated register %d in remote 'g' packet"), i
);
8513 rsa
->regs
[i
].in_g_packet
= 1;
8516 /* Looks valid enough, we can assume this is the correct length
8517 for a 'g' packet. It's important not to adjust
8518 rsa->sizeof_g_packet if we have truncated registers otherwise
8519 this "if" won't be run the next time the method is called
8520 with a packet of the same size and one of the internal errors
8521 below will trigger instead. */
8522 rsa
->sizeof_g_packet
= sizeof_g_packet
;
8525 regs
= (char *) alloca (rsa
->sizeof_g_packet
);
8527 /* Unimplemented registers read as all bits zero. */
8528 memset (regs
, 0, rsa
->sizeof_g_packet
);
8530 /* Reply describes registers byte by byte, each byte encoded as two
8531 hex characters. Suck them all up, then supply them to the
8532 register cacheing/storage mechanism. */
8534 p
= rs
->buf
.data ();
8535 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
8537 if (p
[0] == 0 || p
[1] == 0)
8538 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
8539 internal_error (__FILE__
, __LINE__
,
8540 _("unexpected end of 'g' packet reply"));
8542 if (p
[0] == 'x' && p
[1] == 'x')
8543 regs
[i
] = 0; /* 'x' */
8545 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
8549 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8551 struct packet_reg
*r
= &rsa
->regs
[i
];
8552 long reg_size
= register_size (gdbarch
, i
);
8556 if ((r
->offset
+ reg_size
) * 2 > strlen (rs
->buf
.data ()))
8557 /* This shouldn't happen - we adjusted in_g_packet above. */
8558 internal_error (__FILE__
, __LINE__
,
8559 _("unexpected end of 'g' packet reply"));
8560 else if (rs
->buf
[r
->offset
* 2] == 'x')
8562 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
.data ()));
8563 /* The register isn't available, mark it as such (at
8564 the same time setting the value to zero). */
8565 regcache
->raw_supply (r
->regnum
, NULL
);
8568 regcache
->raw_supply (r
->regnum
, regs
+ r
->offset
);
8574 remote_target::fetch_registers_using_g (struct regcache
*regcache
)
8577 process_g_packet (regcache
);
8580 /* Make the remote selected traceframe match GDB's selected
8584 remote_target::set_remote_traceframe ()
8587 struct remote_state
*rs
= get_remote_state ();
8589 if (rs
->remote_traceframe_number
== get_traceframe_number ())
8592 /* Avoid recursion, remote_trace_find calls us again. */
8593 rs
->remote_traceframe_number
= get_traceframe_number ();
8595 newnum
= target_trace_find (tfind_number
,
8596 get_traceframe_number (), 0, 0, NULL
);
8598 /* Should not happen. If it does, all bets are off. */
8599 if (newnum
!= get_traceframe_number ())
8600 warning (_("could not set remote traceframe"));
8604 remote_target::fetch_registers (struct regcache
*regcache
, int regnum
)
8606 struct gdbarch
*gdbarch
= regcache
->arch ();
8607 struct remote_state
*rs
= get_remote_state ();
8608 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8611 set_remote_traceframe ();
8612 set_general_thread (regcache
->ptid ());
8616 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8618 gdb_assert (reg
!= NULL
);
8620 /* If this register might be in the 'g' packet, try that first -
8621 we are likely to read more than one register. If this is the
8622 first 'g' packet, we might be overly optimistic about its
8623 contents, so fall back to 'p'. */
8624 if (reg
->in_g_packet
)
8626 fetch_registers_using_g (regcache
);
8627 if (reg
->in_g_packet
)
8631 if (fetch_register_using_p (regcache
, reg
))
8634 /* This register is not available. */
8635 regcache
->raw_supply (reg
->regnum
, NULL
);
8640 fetch_registers_using_g (regcache
);
8642 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8643 if (!rsa
->regs
[i
].in_g_packet
)
8644 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
8646 /* This register is not available. */
8647 regcache
->raw_supply (i
, NULL
);
8651 /* Prepare to store registers. Since we may send them all (using a
8652 'G' request), we have to read out the ones we don't want to change
8656 remote_target::prepare_to_store (struct regcache
*regcache
)
8658 struct remote_state
*rs
= get_remote_state ();
8659 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8662 /* Make sure the entire registers array is valid. */
8663 switch (packet_support (PACKET_P
))
8665 case PACKET_DISABLE
:
8666 case PACKET_SUPPORT_UNKNOWN
:
8667 /* Make sure all the necessary registers are cached. */
8668 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8669 if (rsa
->regs
[i
].in_g_packet
)
8670 regcache
->raw_update (rsa
->regs
[i
].regnum
);
8677 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
8678 packet was not recognized. */
8681 remote_target::store_register_using_P (const struct regcache
*regcache
,
8684 struct gdbarch
*gdbarch
= regcache
->arch ();
8685 struct remote_state
*rs
= get_remote_state ();
8686 /* Try storing a single register. */
8687 char *buf
= rs
->buf
.data ();
8688 gdb_byte
*regp
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
->regnum
));
8691 if (packet_support (PACKET_P
) == PACKET_DISABLE
)
8694 if (reg
->pnum
== -1)
8697 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
8698 p
= buf
+ strlen (buf
);
8699 regcache
->raw_collect (reg
->regnum
, regp
);
8700 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
8702 getpkt (&rs
->buf
, 0);
8704 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
8709 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
8710 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
.data ());
8711 case PACKET_UNKNOWN
:
8714 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
8718 /* Store register REGNUM, or all registers if REGNUM == -1, from the
8719 contents of the register cache buffer. FIXME: ignores errors. */
8722 remote_target::store_registers_using_G (const struct regcache
*regcache
)
8724 struct remote_state
*rs
= get_remote_state ();
8725 remote_arch_state
*rsa
= rs
->get_remote_arch_state (regcache
->arch ());
8729 /* Extract all the registers in the regcache copying them into a
8734 regs
= (gdb_byte
*) alloca (rsa
->sizeof_g_packet
);
8735 memset (regs
, 0, rsa
->sizeof_g_packet
);
8736 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
8738 struct packet_reg
*r
= &rsa
->regs
[i
];
8741 regcache
->raw_collect (r
->regnum
, regs
+ r
->offset
);
8745 /* Command describes registers byte by byte,
8746 each byte encoded as two hex characters. */
8747 p
= rs
->buf
.data ();
8749 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
8751 getpkt (&rs
->buf
, 0);
8752 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
8753 error (_("Could not write registers; remote failure reply '%s'"),
8757 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
8758 of the register cache buffer. FIXME: ignores errors. */
8761 remote_target::store_registers (struct regcache
*regcache
, int regnum
)
8763 struct gdbarch
*gdbarch
= regcache
->arch ();
8764 struct remote_state
*rs
= get_remote_state ();
8765 remote_arch_state
*rsa
= rs
->get_remote_arch_state (gdbarch
);
8768 set_remote_traceframe ();
8769 set_general_thread (regcache
->ptid ());
8773 packet_reg
*reg
= packet_reg_from_regnum (gdbarch
, rsa
, regnum
);
8775 gdb_assert (reg
!= NULL
);
8777 /* Always prefer to store registers using the 'P' packet if
8778 possible; we often change only a small number of registers.
8779 Sometimes we change a larger number; we'd need help from a
8780 higher layer to know to use 'G'. */
8781 if (store_register_using_P (regcache
, reg
))
8784 /* For now, don't complain if we have no way to write the
8785 register. GDB loses track of unavailable registers too
8786 easily. Some day, this may be an error. We don't have
8787 any way to read the register, either... */
8788 if (!reg
->in_g_packet
)
8791 store_registers_using_G (regcache
);
8795 store_registers_using_G (regcache
);
8797 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
8798 if (!rsa
->regs
[i
].in_g_packet
)
8799 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
8800 /* See above for why we do not issue an error here. */
8805 /* Return the number of hex digits in num. */
8808 hexnumlen (ULONGEST num
)
8812 for (i
= 0; num
!= 0; i
++)
8815 return std::max (i
, 1);
8818 /* Set BUF to the minimum number of hex digits representing NUM. */
8821 hexnumstr (char *buf
, ULONGEST num
)
8823 int len
= hexnumlen (num
);
8825 return hexnumnstr (buf
, num
, len
);
8829 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
8832 hexnumnstr (char *buf
, ULONGEST num
, int width
)
8838 for (i
= width
- 1; i
>= 0; i
--)
8840 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
8847 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
8850 remote_address_masked (CORE_ADDR addr
)
8852 unsigned int address_size
= remote_address_size
;
8854 /* If "remoteaddresssize" was not set, default to target address size. */
8856 address_size
= gdbarch_addr_bit (target_gdbarch ());
8858 if (address_size
> 0
8859 && address_size
< (sizeof (ULONGEST
) * 8))
8861 /* Only create a mask when that mask can safely be constructed
8862 in a ULONGEST variable. */
8865 mask
= (mask
<< address_size
) - 1;
8871 /* Determine whether the remote target supports binary downloading.
8872 This is accomplished by sending a no-op memory write of zero length
8873 to the target at the specified address. It does not suffice to send
8874 the whole packet, since many stubs strip the eighth bit and
8875 subsequently compute a wrong checksum, which causes real havoc with
8878 NOTE: This can still lose if the serial line is not eight-bit
8879 clean. In cases like this, the user should clear "remote
8883 remote_target::check_binary_download (CORE_ADDR addr
)
8885 struct remote_state
*rs
= get_remote_state ();
8887 switch (packet_support (PACKET_X
))
8889 case PACKET_DISABLE
:
8893 case PACKET_SUPPORT_UNKNOWN
:
8897 p
= rs
->buf
.data ();
8899 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8901 p
+= hexnumstr (p
, (ULONGEST
) 0);
8905 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
8906 getpkt (&rs
->buf
, 0);
8908 if (rs
->buf
[0] == '\0')
8910 remote_debug_printf ("binary downloading NOT supported by target");
8911 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
8915 remote_debug_printf ("binary downloading supported by target");
8916 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
8923 /* Helper function to resize the payload in order to try to get a good
8924 alignment. We try to write an amount of data such that the next write will
8925 start on an address aligned on REMOTE_ALIGN_WRITES. */
8928 align_for_efficient_write (int todo
, CORE_ADDR memaddr
)
8930 return ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
8933 /* Write memory data directly to the remote machine.
8934 This does not inform the data cache; the data cache uses this.
8935 HEADER is the starting part of the packet.
8936 MEMADDR is the address in the remote memory space.
8937 MYADDR is the address of the buffer in our space.
8938 LEN_UNITS is the number of addressable units to write.
8939 UNIT_SIZE is the length in bytes of an addressable unit.
8940 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
8941 should send data as binary ('X'), or hex-encoded ('M').
8943 The function creates packet of the form
8944 <HEADER><ADDRESS>,<LENGTH>:<DATA>
8946 where encoding of <DATA> is terminated by PACKET_FORMAT.
8948 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
8951 Return the transferred status, error or OK (an
8952 'enum target_xfer_status' value). Save the number of addressable units
8953 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
8955 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
8956 exchange between gdb and the stub could look like (?? in place of the
8962 -> $M1000,3:eeeeffffeeee#??
8966 <- eeeeffffeeeedddd */
8969 remote_target::remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
8970 const gdb_byte
*myaddr
,
8973 ULONGEST
*xfered_len_units
,
8974 char packet_format
, int use_length
)
8976 struct remote_state
*rs
= get_remote_state ();
8982 int payload_capacity_bytes
;
8983 int payload_length_bytes
;
8985 if (packet_format
!= 'X' && packet_format
!= 'M')
8986 internal_error (__FILE__
, __LINE__
,
8987 _("remote_write_bytes_aux: bad packet format"));
8990 return TARGET_XFER_EOF
;
8992 payload_capacity_bytes
= get_memory_write_packet_size ();
8994 /* The packet buffer will be large enough for the payload;
8995 get_memory_packet_size ensures this. */
8998 /* Compute the size of the actual payload by subtracting out the
8999 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
9001 payload_capacity_bytes
-= strlen ("$,:#NN");
9003 /* The comma won't be used. */
9004 payload_capacity_bytes
+= 1;
9005 payload_capacity_bytes
-= strlen (header
);
9006 payload_capacity_bytes
-= hexnumlen (memaddr
);
9008 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
9010 strcat (rs
->buf
.data (), header
);
9011 p
= rs
->buf
.data () + strlen (header
);
9013 /* Compute a best guess of the number of bytes actually transfered. */
9014 if (packet_format
== 'X')
9016 /* Best guess at number of bytes that will fit. */
9017 todo_units
= std::min (len_units
,
9018 (ULONGEST
) payload_capacity_bytes
/ unit_size
);
9020 payload_capacity_bytes
-= hexnumlen (todo_units
);
9021 todo_units
= std::min (todo_units
, payload_capacity_bytes
/ unit_size
);
9025 /* Number of bytes that will fit. */
9027 = std::min (len_units
,
9028 (ULONGEST
) (payload_capacity_bytes
/ unit_size
) / 2);
9030 payload_capacity_bytes
-= hexnumlen (todo_units
);
9031 todo_units
= std::min (todo_units
,
9032 (payload_capacity_bytes
/ unit_size
) / 2);
9035 if (todo_units
<= 0)
9036 internal_error (__FILE__
, __LINE__
,
9037 _("minimum packet size too small to write data"));
9039 /* If we already need another packet, then try to align the end
9040 of this packet to a useful boundary. */
9041 if (todo_units
> 2 * REMOTE_ALIGN_WRITES
&& todo_units
< len_units
)
9042 todo_units
= align_for_efficient_write (todo_units
, memaddr
);
9044 /* Append "<memaddr>". */
9045 memaddr
= remote_address_masked (memaddr
);
9046 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9053 /* Append the length and retain its location and size. It may need to be
9054 adjusted once the packet body has been created. */
9056 plenlen
= hexnumstr (p
, (ULONGEST
) todo_units
);
9064 /* Append the packet body. */
9065 if (packet_format
== 'X')
9067 /* Binary mode. Send target system values byte by byte, in
9068 increasing byte addresses. Only escape certain critical
9070 payload_length_bytes
=
9071 remote_escape_output (myaddr
, todo_units
, unit_size
, (gdb_byte
*) p
,
9072 &units_written
, payload_capacity_bytes
);
9074 /* If not all TODO units fit, then we'll need another packet. Make
9075 a second try to keep the end of the packet aligned. Don't do
9076 this if the packet is tiny. */
9077 if (units_written
< todo_units
&& units_written
> 2 * REMOTE_ALIGN_WRITES
)
9081 new_todo_units
= align_for_efficient_write (units_written
, memaddr
);
9083 if (new_todo_units
!= units_written
)
9084 payload_length_bytes
=
9085 remote_escape_output (myaddr
, new_todo_units
, unit_size
,
9086 (gdb_byte
*) p
, &units_written
,
9087 payload_capacity_bytes
);
9090 p
+= payload_length_bytes
;
9091 if (use_length
&& units_written
< todo_units
)
9093 /* Escape chars have filled up the buffer prematurely,
9094 and we have actually sent fewer units than planned.
9095 Fix-up the length field of the packet. Use the same
9096 number of characters as before. */
9097 plen
+= hexnumnstr (plen
, (ULONGEST
) units_written
,
9099 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
9104 /* Normal mode: Send target system values byte by byte, in
9105 increasing byte addresses. Each byte is encoded as a two hex
9107 p
+= 2 * bin2hex (myaddr
, p
, todo_units
* unit_size
);
9108 units_written
= todo_units
;
9111 putpkt_binary (rs
->buf
.data (), (int) (p
- rs
->buf
.data ()));
9112 getpkt (&rs
->buf
, 0);
9114 if (rs
->buf
[0] == 'E')
9115 return TARGET_XFER_E_IO
;
9117 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
9118 send fewer units than we'd planned. */
9119 *xfered_len_units
= (ULONGEST
) units_written
;
9120 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9123 /* Write memory data directly to the remote machine.
9124 This does not inform the data cache; the data cache uses this.
9125 MEMADDR is the address in the remote memory space.
9126 MYADDR is the address of the buffer in our space.
9127 LEN is the number of bytes.
9129 Return the transferred status, error or OK (an
9130 'enum target_xfer_status' value). Save the number of bytes
9131 transferred in *XFERED_LEN. Only transfer a single packet. */
9134 remote_target::remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
,
9135 ULONGEST len
, int unit_size
,
9136 ULONGEST
*xfered_len
)
9138 const char *packet_format
= NULL
;
9140 /* Check whether the target supports binary download. */
9141 check_binary_download (memaddr
);
9143 switch (packet_support (PACKET_X
))
9146 packet_format
= "X";
9148 case PACKET_DISABLE
:
9149 packet_format
= "M";
9151 case PACKET_SUPPORT_UNKNOWN
:
9152 internal_error (__FILE__
, __LINE__
,
9153 _("remote_write_bytes: bad internal state"));
9155 internal_error (__FILE__
, __LINE__
, _("bad switch"));
9158 return remote_write_bytes_aux (packet_format
,
9159 memaddr
, myaddr
, len
, unit_size
, xfered_len
,
9160 packet_format
[0], 1);
9163 /* Read memory data directly from the remote machine.
9164 This does not use the data cache; the data cache uses this.
9165 MEMADDR is the address in the remote memory space.
9166 MYADDR is the address of the buffer in our space.
9167 LEN_UNITS is the number of addressable memory units to read..
9168 UNIT_SIZE is the length in bytes of an addressable unit.
9170 Return the transferred status, error or OK (an
9171 'enum target_xfer_status' value). Save the number of bytes
9172 transferred in *XFERED_LEN_UNITS.
9174 See the comment of remote_write_bytes_aux for an example of
9175 memory read/write exchange between gdb and the stub. */
9178 remote_target::remote_read_bytes_1 (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
9180 int unit_size
, ULONGEST
*xfered_len_units
)
9182 struct remote_state
*rs
= get_remote_state ();
9183 int buf_size_bytes
; /* Max size of packet output buffer. */
9188 buf_size_bytes
= get_memory_read_packet_size ();
9189 /* The packet buffer will be large enough for the payload;
9190 get_memory_packet_size ensures this. */
9192 /* Number of units that will fit. */
9193 todo_units
= std::min (len_units
,
9194 (ULONGEST
) (buf_size_bytes
/ unit_size
) / 2);
9196 /* Construct "m"<memaddr>","<len>". */
9197 memaddr
= remote_address_masked (memaddr
);
9198 p
= rs
->buf
.data ();
9200 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
9202 p
+= hexnumstr (p
, (ULONGEST
) todo_units
);
9205 getpkt (&rs
->buf
, 0);
9206 if (rs
->buf
[0] == 'E'
9207 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
9208 && rs
->buf
[3] == '\0')
9209 return TARGET_XFER_E_IO
;
9210 /* Reply describes memory byte by byte, each byte encoded as two hex
9212 p
= rs
->buf
.data ();
9213 decoded_bytes
= hex2bin (p
, myaddr
, todo_units
* unit_size
);
9214 /* Return what we have. Let higher layers handle partial reads. */
9215 *xfered_len_units
= (ULONGEST
) (decoded_bytes
/ unit_size
);
9216 return (*xfered_len_units
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
9219 /* Using the set of read-only target sections of remote, read live
9222 For interface/parameters/return description see target.h,
9226 remote_target::remote_xfer_live_readonly_partial (gdb_byte
*readbuf
,
9230 ULONGEST
*xfered_len
)
9232 const struct target_section
*secp
;
9234 secp
= target_section_by_addr (this, memaddr
);
9236 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
9238 ULONGEST memend
= memaddr
+ len
;
9240 const target_section_table
*table
= target_get_section_table (this);
9241 for (const target_section
&p
: *table
)
9243 if (memaddr
>= p
.addr
)
9245 if (memend
<= p
.endaddr
)
9247 /* Entire transfer is within this section. */
9248 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9251 else if (memaddr
>= p
.endaddr
)
9253 /* This section ends before the transfer starts. */
9258 /* This section overlaps the transfer. Just do half. */
9259 len
= p
.endaddr
- memaddr
;
9260 return remote_read_bytes_1 (memaddr
, readbuf
, len
, unit_size
,
9267 return TARGET_XFER_EOF
;
9270 /* Similar to remote_read_bytes_1, but it reads from the remote stub
9271 first if the requested memory is unavailable in traceframe.
9272 Otherwise, fall back to remote_read_bytes_1. */
9275 remote_target::remote_read_bytes (CORE_ADDR memaddr
,
9276 gdb_byte
*myaddr
, ULONGEST len
, int unit_size
,
9277 ULONGEST
*xfered_len
)
9280 return TARGET_XFER_EOF
;
9282 if (get_traceframe_number () != -1)
9284 std::vector
<mem_range
> available
;
9286 /* If we fail to get the set of available memory, then the
9287 target does not support querying traceframe info, and so we
9288 attempt reading from the traceframe anyway (assuming the
9289 target implements the old QTro packet then). */
9290 if (traceframe_available_memory (&available
, memaddr
, len
))
9292 if (available
.empty () || available
[0].start
!= memaddr
)
9294 enum target_xfer_status res
;
9296 /* Don't read into the traceframe's available
9298 if (!available
.empty ())
9300 LONGEST oldlen
= len
;
9302 len
= available
[0].start
- memaddr
;
9303 gdb_assert (len
<= oldlen
);
9306 /* This goes through the topmost target again. */
9307 res
= remote_xfer_live_readonly_partial (myaddr
, memaddr
,
9308 len
, unit_size
, xfered_len
);
9309 if (res
== TARGET_XFER_OK
)
9310 return TARGET_XFER_OK
;
9313 /* No use trying further, we know some memory starting
9314 at MEMADDR isn't available. */
9316 return (*xfered_len
!= 0) ?
9317 TARGET_XFER_UNAVAILABLE
: TARGET_XFER_EOF
;
9321 /* Don't try to read more than how much is available, in
9322 case the target implements the deprecated QTro packet to
9323 cater for older GDBs (the target's knowledge of read-only
9324 sections may be outdated by now). */
9325 len
= available
[0].length
;
9329 return remote_read_bytes_1 (memaddr
, myaddr
, len
, unit_size
, xfered_len
);
9334 /* Sends a packet with content determined by the printf format string
9335 FORMAT and the remaining arguments, then gets the reply. Returns
9336 whether the packet was a success, a failure, or unknown. */
9339 remote_target::remote_send_printf (const char *format
, ...)
9341 struct remote_state
*rs
= get_remote_state ();
9342 int max_size
= get_remote_packet_size ();
9345 va_start (ap
, format
);
9348 int size
= vsnprintf (rs
->buf
.data (), max_size
, format
, ap
);
9352 if (size
>= max_size
)
9353 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
9355 if (putpkt (rs
->buf
) < 0)
9356 error (_("Communication problem with target."));
9359 getpkt (&rs
->buf
, 0);
9361 return packet_check_result (rs
->buf
);
9364 /* Flash writing can take quite some time. We'll set
9365 effectively infinite timeout for flash operations.
9366 In future, we'll need to decide on a better approach. */
9367 static const int remote_flash_timeout
= 1000;
9370 remote_target::flash_erase (ULONGEST address
, LONGEST length
)
9372 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
9373 enum packet_result ret
;
9374 scoped_restore restore_timeout
9375 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9377 ret
= remote_send_printf ("vFlashErase:%s,%s",
9378 phex (address
, addr_size
),
9382 case PACKET_UNKNOWN
:
9383 error (_("Remote target does not support flash erase"));
9385 error (_("Error erasing flash with vFlashErase packet"));
9392 remote_target::remote_flash_write (ULONGEST address
,
9393 ULONGEST length
, ULONGEST
*xfered_len
,
9394 const gdb_byte
*data
)
9396 scoped_restore restore_timeout
9397 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9398 return remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 1,
9403 remote_target::flash_done ()
9407 scoped_restore restore_timeout
9408 = make_scoped_restore (&remote_timeout
, remote_flash_timeout
);
9410 ret
= remote_send_printf ("vFlashDone");
9414 case PACKET_UNKNOWN
:
9415 error (_("Remote target does not support vFlashDone"));
9417 error (_("Error finishing flash operation"));
9424 remote_target::files_info ()
9426 puts_filtered ("Debugging a target over a serial line.\n");
9429 /* Stuff for dealing with the packets which are part of this protocol.
9430 See comment at top of file for details. */
9432 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
9433 error to higher layers. Called when a serial error is detected.
9434 The exception message is STRING, followed by a colon and a blank,
9435 the system error message for errno at function entry and final dot
9436 for output compatibility with throw_perror_with_name. */
9439 unpush_and_perror (remote_target
*target
, const char *string
)
9441 int saved_errno
= errno
;
9443 remote_unpush_target (target
);
9444 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
9445 safe_strerror (saved_errno
));
9448 /* Read a single character from the remote end. The current quit
9449 handler is overridden to avoid quitting in the middle of packet
9450 sequence, as that would break communication with the remote server.
9451 See remote_serial_quit_handler for more detail. */
9454 remote_target::readchar (int timeout
)
9457 struct remote_state
*rs
= get_remote_state ();
9460 scoped_restore restore_quit_target
9461 = make_scoped_restore (&curr_quit_handler_target
, this);
9462 scoped_restore restore_quit
9463 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9465 rs
->got_ctrlc_during_io
= 0;
9467 ch
= serial_readchar (rs
->remote_desc
, timeout
);
9469 if (rs
->got_ctrlc_during_io
)
9476 switch ((enum serial_rc
) ch
)
9479 remote_unpush_target (this);
9480 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
9483 unpush_and_perror (this, _("Remote communication error. "
9484 "Target disconnected."));
9486 case SERIAL_TIMEOUT
:
9492 /* Wrapper for serial_write that closes the target and throws if
9493 writing fails. The current quit handler is overridden to avoid
9494 quitting in the middle of packet sequence, as that would break
9495 communication with the remote server. See
9496 remote_serial_quit_handler for more detail. */
9499 remote_target::remote_serial_write (const char *str
, int len
)
9501 struct remote_state
*rs
= get_remote_state ();
9503 scoped_restore restore_quit_target
9504 = make_scoped_restore (&curr_quit_handler_target
, this);
9505 scoped_restore restore_quit
9506 = make_scoped_restore (&quit_handler
, ::remote_serial_quit_handler
);
9508 rs
->got_ctrlc_during_io
= 0;
9510 if (serial_write (rs
->remote_desc
, str
, len
))
9512 unpush_and_perror (this, _("Remote communication error. "
9513 "Target disconnected."));
9516 if (rs
->got_ctrlc_during_io
)
9520 /* Return a string representing an escaped version of BUF, of len N.
9521 E.g. \n is converted to \\n, \t to \\t, etc. */
9524 escape_buffer (const char *buf
, int n
)
9528 stb
.putstrn (buf
, n
, '\\');
9529 return std::move (stb
.string ());
9533 remote_target::putpkt (const char *buf
)
9535 return putpkt_binary (buf
, strlen (buf
));
9538 /* Wrapper around remote_target::putpkt to avoid exporting
9542 putpkt (remote_target
*remote
, const char *buf
)
9544 return remote
->putpkt (buf
);
9547 /* Send a packet to the remote machine, with error checking. The data
9548 of the packet is in BUF. The string in BUF can be at most
9549 get_remote_packet_size () - 5 to account for the $, # and checksum,
9550 and for a possible /0 if we are debugging (remote_debug) and want
9551 to print the sent packet as a string. */
9554 remote_target::putpkt_binary (const char *buf
, int cnt
)
9556 struct remote_state
*rs
= get_remote_state ();
9558 unsigned char csum
= 0;
9559 gdb::def_vector
<char> data (cnt
+ 6);
9560 char *buf2
= data
.data ();
9566 /* Catch cases like trying to read memory or listing threads while
9567 we're waiting for a stop reply. The remote server wouldn't be
9568 ready to handle this request, so we'd hang and timeout. We don't
9569 have to worry about this in synchronous mode, because in that
9570 case it's not possible to issue a command while the target is
9571 running. This is not a problem in non-stop mode, because in that
9572 case, the stub is always ready to process serial input. */
9573 if (!target_is_non_stop_p ()
9574 && target_is_async_p ()
9575 && rs
->waiting_for_stop_reply
)
9577 error (_("Cannot execute this command while the target is running.\n"
9578 "Use the \"interrupt\" command to stop the target\n"
9579 "and then try again."));
9582 /* Copy the packet into buffer BUF2, encapsulating it
9583 and giving it a checksum. */
9588 for (i
= 0; i
< cnt
; i
++)
9594 *p
++ = tohex ((csum
>> 4) & 0xf);
9595 *p
++ = tohex (csum
& 0xf);
9597 /* Send it over and over until we get a positive ack. */
9605 int len
= (int) (p
- buf2
);
9608 if (remote_packet_max_chars
< 0)
9611 max_chars
= remote_packet_max_chars
;
9614 = escape_buffer (buf2
, std::min (len
, max_chars
));
9616 if (len
> max_chars
)
9617 remote_debug_printf_nofunc
9618 ("Sending packet: %s [%d bytes omitted]", str
.c_str (),
9621 remote_debug_printf_nofunc ("Sending packet: %s", str
.c_str ());
9623 remote_serial_write (buf2
, p
- buf2
);
9625 /* If this is a no acks version of the remote protocol, send the
9626 packet and move on. */
9630 /* Read until either a timeout occurs (-2) or '+' is read.
9631 Handle any notification that arrives in the mean time. */
9634 ch
= readchar (remote_timeout
);
9639 remote_debug_printf_nofunc ("Received Ack");
9642 remote_debug_printf_nofunc ("Received Nak");
9644 case SERIAL_TIMEOUT
:
9648 break; /* Retransmit buffer. */
9651 remote_debug_printf ("Packet instead of Ack, ignoring it");
9652 /* It's probably an old response sent because an ACK
9653 was lost. Gobble up the packet and ack it so it
9654 doesn't get retransmitted when we resend this
9657 remote_serial_write ("+", 1);
9658 continue; /* Now, go look for +. */
9665 /* If we got a notification, handle it, and go back to looking
9667 /* We've found the start of a notification. Now
9668 collect the data. */
9669 val
= read_frame (&rs
->buf
);
9672 remote_debug_printf_nofunc
9673 (" Notification received: %s",
9674 escape_buffer (rs
->buf
.data (), val
).c_str ());
9676 handle_notification (rs
->notif_state
, rs
->buf
.data ());
9677 /* We're in sync now, rewait for the ack. */
9681 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9687 remote_debug_printf_nofunc ("Junk: %c%s", ch
& 0177,
9691 break; /* Here to retransmit. */
9695 /* This is wrong. If doing a long backtrace, the user should be
9696 able to get out next time we call QUIT, without anything as
9697 violent as interrupt_query. If we want to provide a way out of
9698 here without getting to the next QUIT, it should be based on
9699 hitting ^C twice as in remote_wait. */
9711 /* Come here after finding the start of a frame when we expected an
9712 ack. Do our best to discard the rest of this packet. */
9715 remote_target::skip_frame ()
9721 c
= readchar (remote_timeout
);
9724 case SERIAL_TIMEOUT
:
9725 /* Nothing we can do. */
9728 /* Discard the two bytes of checksum and stop. */
9729 c
= readchar (remote_timeout
);
9731 c
= readchar (remote_timeout
);
9734 case '*': /* Run length encoding. */
9735 /* Discard the repeat count. */
9736 c
= readchar (remote_timeout
);
9741 /* A regular character. */
9747 /* Come here after finding the start of the frame. Collect the rest
9748 into *BUF, verifying the checksum, length, and handling run-length
9749 compression. NUL terminate the buffer. If there is not enough room,
9752 Returns -1 on error, number of characters in buffer (ignoring the
9753 trailing NULL) on success. (could be extended to return one of the
9754 SERIAL status indications). */
9757 remote_target::read_frame (gdb::char_vector
*buf_p
)
9762 char *buf
= buf_p
->data ();
9763 struct remote_state
*rs
= get_remote_state ();
9770 c
= readchar (remote_timeout
);
9773 case SERIAL_TIMEOUT
:
9774 remote_debug_printf ("Timeout in mid-packet, retrying");
9778 remote_debug_printf ("Saw new packet start in middle of old one");
9779 return -1; /* Start a new packet, count retries. */
9783 unsigned char pktcsum
;
9789 check_0
= readchar (remote_timeout
);
9791 check_1
= readchar (remote_timeout
);
9793 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
9795 remote_debug_printf ("Timeout in checksum, retrying");
9798 else if (check_0
< 0 || check_1
< 0)
9800 remote_debug_printf ("Communication error in checksum");
9804 /* Don't recompute the checksum; with no ack packets we
9805 don't have any way to indicate a packet retransmission
9810 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
9811 if (csum
== pktcsum
)
9815 ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
9816 pktcsum
, csum
, escape_buffer (buf
, bc
).c_str ());
9818 /* Number of characters in buffer ignoring trailing
9822 case '*': /* Run length encoding. */
9827 c
= readchar (remote_timeout
);
9829 repeat
= c
- ' ' + 3; /* Compute repeat count. */
9831 /* The character before ``*'' is repeated. */
9833 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
9835 if (bc
+ repeat
- 1 >= buf_p
->size () - 1)
9837 /* Make some more room in the buffer. */
9838 buf_p
->resize (buf_p
->size () + repeat
);
9839 buf
= buf_p
->data ();
9842 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
9848 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
9852 if (bc
>= buf_p
->size () - 1)
9854 /* Make some more room in the buffer. */
9855 buf_p
->resize (buf_p
->size () * 2);
9856 buf
= buf_p
->data ();
9866 /* Set this to the maximum number of seconds to wait instead of waiting forever
9867 in target_wait(). If this timer times out, then it generates an error and
9868 the command is aborted. This replaces most of the need for timeouts in the
9869 GDB test suite, and makes it possible to distinguish between a hung target
9870 and one with slow communications. */
9872 static int watchdog
= 0;
9874 show_watchdog (struct ui_file
*file
, int from_tty
,
9875 struct cmd_list_element
*c
, const char *value
)
9877 fprintf_filtered (file
, _("Watchdog timer is %s.\n"), value
);
9880 /* Read a packet from the remote machine, with error checking, and
9881 store it in *BUF. Resize *BUF if necessary to hold the result. If
9882 FOREVER, wait forever rather than timing out; this is used (in
9883 synchronous mode) to wait for a target that is is executing user
9885 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
9886 don't have to change all the calls to getpkt to deal with the
9887 return value, because at the moment I don't know what the right
9888 thing to do it for those. */
9891 remote_target::getpkt (gdb::char_vector
*buf
, int forever
)
9893 getpkt_sane (buf
, forever
);
9897 /* Read a packet from the remote machine, with error checking, and
9898 store it in *BUF. Resize *BUF if necessary to hold the result. If
9899 FOREVER, wait forever rather than timing out; this is used (in
9900 synchronous mode) to wait for a target that is is executing user
9901 code to stop. If FOREVER == 0, this function is allowed to time
9902 out gracefully and return an indication of this to the caller.
9903 Otherwise return the number of bytes read. If EXPECTING_NOTIF,
9904 consider receiving a notification enough reason to return to the
9905 caller. *IS_NOTIF is an output boolean that indicates whether *BUF
9906 holds a notification or not (a regular packet). */
9909 remote_target::getpkt_or_notif_sane_1 (gdb::char_vector
*buf
,
9910 int forever
, int expecting_notif
,
9913 struct remote_state
*rs
= get_remote_state ();
9919 strcpy (buf
->data (), "timeout");
9922 timeout
= watchdog
> 0 ? watchdog
: -1;
9923 else if (expecting_notif
)
9924 timeout
= 0; /* There should already be a char in the buffer. If
9927 timeout
= remote_timeout
;
9931 /* Process any number of notifications, and then return when
9935 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
9937 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
9939 /* This can loop forever if the remote side sends us
9940 characters continuously, but if it pauses, we'll get
9941 SERIAL_TIMEOUT from readchar because of timeout. Then
9942 we'll count that as a retry.
9944 Note that even when forever is set, we will only wait
9945 forever prior to the start of a packet. After that, we
9946 expect characters to arrive at a brisk pace. They should
9947 show up within remote_timeout intervals. */
9949 c
= readchar (timeout
);
9950 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
9952 if (c
== SERIAL_TIMEOUT
)
9954 if (expecting_notif
)
9955 return -1; /* Don't complain, it's normal to not get
9956 anything in this case. */
9958 if (forever
) /* Watchdog went off? Kill the target. */
9960 remote_unpush_target (this);
9961 throw_error (TARGET_CLOSE_ERROR
,
9962 _("Watchdog timeout has expired. "
9963 "Target detached."));
9966 remote_debug_printf ("Timed out.");
9970 /* We've found the start of a packet or notification.
9971 Now collect the data. */
9972 val
= read_frame (buf
);
9977 remote_serial_write ("-", 1);
9980 if (tries
> MAX_TRIES
)
9982 /* We have tried hard enough, and just can't receive the
9983 packet/notification. Give up. */
9984 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
9986 /* Skip the ack char if we're in no-ack mode. */
9987 if (!rs
->noack_mode
)
9988 remote_serial_write ("+", 1);
9992 /* If we got an ordinary packet, return that to our caller. */
9999 if (remote_packet_max_chars
< 0)
10002 max_chars
= remote_packet_max_chars
;
10005 = escape_buffer (buf
->data (),
10006 std::min (val
, max_chars
));
10008 if (val
> max_chars
)
10009 remote_debug_printf_nofunc
10010 ("Packet received: %s [%d bytes omitted]", str
.c_str (),
10013 remote_debug_printf_nofunc ("Packet received: %s",
10017 /* Skip the ack char if we're in no-ack mode. */
10018 if (!rs
->noack_mode
)
10019 remote_serial_write ("+", 1);
10020 if (is_notif
!= NULL
)
10025 /* If we got a notification, handle it, and go back to looking
10029 gdb_assert (c
== '%');
10031 remote_debug_printf_nofunc
10032 (" Notification received: %s",
10033 escape_buffer (buf
->data (), val
).c_str ());
10035 if (is_notif
!= NULL
)
10038 handle_notification (rs
->notif_state
, buf
->data ());
10040 /* Notifications require no acknowledgement. */
10042 if (expecting_notif
)
10049 remote_target::getpkt_sane (gdb::char_vector
*buf
, int forever
)
10051 return getpkt_or_notif_sane_1 (buf
, forever
, 0, NULL
);
10055 remote_target::getpkt_or_notif_sane (gdb::char_vector
*buf
, int forever
,
10058 return getpkt_or_notif_sane_1 (buf
, forever
, 1, is_notif
);
10061 /* Kill any new fork children of inferior INF that haven't been
10062 processed by follow_fork. */
10065 remote_target::kill_new_fork_children (inferior
*inf
)
10067 remote_state
*rs
= get_remote_state ();
10068 struct notif_client
*notif
= ¬if_client_stop
;
10070 /* Kill the fork child threads of any threads in inferior INF that are stopped
10071 at a fork event. */
10072 for (thread_info
*thread
: inf
->non_exited_threads ())
10074 const target_waitstatus
*ws
= thread_pending_fork_status (thread
);
10079 int child_pid
= ws
->child_ptid ().pid ();
10080 int res
= remote_vkill (child_pid
);
10083 error (_("Can't kill fork child process %d"), child_pid
);
10086 /* Check for any pending fork events (not reported or processed yet)
10087 in inferior INF and kill those fork child threads as well. */
10088 remote_notif_get_pending_events (notif
);
10089 for (auto &event
: rs
->stop_reply_queue
)
10091 if (event
->ptid
.pid () != inf
->pid
)
10094 if (!is_fork_status (event
->ws
.kind ()))
10097 int child_pid
= event
->ws
.child_ptid ().pid ();
10098 int res
= remote_vkill (child_pid
);
10101 error (_("Can't kill fork child process %d"), child_pid
);
10106 /* Target hook to kill the current inferior. */
10109 remote_target::kill ()
10112 inferior
*inf
= find_inferior_pid (this, inferior_ptid
.pid ());
10113 struct remote_state
*rs
= get_remote_state ();
10115 gdb_assert (inf
!= nullptr);
10117 if (packet_support (PACKET_vKill
) != PACKET_DISABLE
)
10119 /* If we're stopped while forking and we haven't followed yet,
10120 kill the child task. We need to do this before killing the
10121 parent task because if this is a vfork then the parent will
10123 kill_new_fork_children (inf
);
10125 res
= remote_vkill (inf
->pid
);
10128 target_mourn_inferior (inferior_ptid
);
10133 /* If we are in 'target remote' mode and we are killing the only
10134 inferior, then we will tell gdbserver to exit and unpush the
10136 if (res
== -1 && !remote_multi_process_p (rs
)
10137 && number_of_live_inferiors (this) == 1)
10141 /* We've killed the remote end, we get to mourn it. If we are
10142 not in extended mode, mourning the inferior also unpushes
10143 remote_ops from the target stack, which closes the remote
10145 target_mourn_inferior (inferior_ptid
);
10150 error (_("Can't kill process"));
10153 /* Send a kill request to the target using the 'vKill' packet. */
10156 remote_target::remote_vkill (int pid
)
10158 if (packet_support (PACKET_vKill
) == PACKET_DISABLE
)
10161 remote_state
*rs
= get_remote_state ();
10163 /* Tell the remote target to detach. */
10164 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "vKill;%x", pid
);
10166 getpkt (&rs
->buf
, 0);
10168 switch (packet_ok (rs
->buf
,
10169 &remote_protocol_packets
[PACKET_vKill
]))
10175 case PACKET_UNKNOWN
:
10178 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
10182 /* Send a kill request to the target using the 'k' packet. */
10185 remote_target::remote_kill_k ()
10187 /* Catch errors so the user can quit from gdb even when we
10188 aren't on speaking terms with the remote system. */
10193 catch (const gdb_exception_error
&ex
)
10195 if (ex
.error
== TARGET_CLOSE_ERROR
)
10197 /* If we got an (EOF) error that caused the target
10198 to go away, then we're done, that's what we wanted.
10199 "k" is susceptible to cause a premature EOF, given
10200 that the remote server isn't actually required to
10201 reply to "k", and it can happen that it doesn't
10202 even get to reply ACK to the "k". */
10206 /* Otherwise, something went wrong. We didn't actually kill
10207 the target. Just propagate the exception, and let the
10208 user or higher layers decide what to do. */
10214 remote_target::mourn_inferior ()
10216 struct remote_state
*rs
= get_remote_state ();
10218 /* We're no longer interested in notification events of an inferior
10219 that exited or was killed/detached. */
10220 discard_pending_stop_replies (current_inferior ());
10222 /* In 'target remote' mode with one inferior, we close the connection. */
10223 if (!rs
->extended
&& number_of_live_inferiors (this) <= 1)
10225 remote_unpush_target (this);
10229 /* In case we got here due to an error, but we're going to stay
10231 rs
->waiting_for_stop_reply
= 0;
10233 /* If the current general thread belonged to the process we just
10234 detached from or has exited, the remote side current general
10235 thread becomes undefined. Considering a case like this:
10237 - We just got here due to a detach.
10238 - The process that we're detaching from happens to immediately
10239 report a global breakpoint being hit in non-stop mode, in the
10240 same thread we had selected before.
10241 - GDB attaches to this process again.
10242 - This event happens to be the next event we handle.
10244 GDB would consider that the current general thread didn't need to
10245 be set on the stub side (with Hg), since for all it knew,
10246 GENERAL_THREAD hadn't changed.
10248 Notice that although in all-stop mode, the remote server always
10249 sets the current thread to the thread reporting the stop event,
10250 that doesn't happen in non-stop mode; in non-stop, the stub *must
10251 not* change the current thread when reporting a breakpoint hit,
10252 due to the decoupling of event reporting and event handling.
10254 To keep things simple, we always invalidate our notion of the
10256 record_currthread (rs
, minus_one_ptid
);
10258 /* Call common code to mark the inferior as not running. */
10259 generic_mourn_inferior ();
10263 extended_remote_target::supports_disable_randomization ()
10265 return packet_support (PACKET_QDisableRandomization
) == PACKET_ENABLE
;
10269 remote_target::extended_remote_disable_randomization (int val
)
10271 struct remote_state
*rs
= get_remote_state ();
10274 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10275 "QDisableRandomization:%x", val
);
10277 reply
= remote_get_noisy_reply ();
10278 if (*reply
== '\0')
10279 error (_("Target does not support QDisableRandomization."));
10280 if (strcmp (reply
, "OK") != 0)
10281 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
10285 remote_target::extended_remote_run (const std::string
&args
)
10287 struct remote_state
*rs
= get_remote_state ();
10289 const char *remote_exec_file
= get_remote_exec_file ();
10291 /* If the user has disabled vRun support, or we have detected that
10292 support is not available, do not try it. */
10293 if (packet_support (PACKET_vRun
) == PACKET_DISABLE
)
10296 strcpy (rs
->buf
.data (), "vRun;");
10297 len
= strlen (rs
->buf
.data ());
10299 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
10300 error (_("Remote file name too long for run packet"));
10301 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
.data () + len
,
10302 strlen (remote_exec_file
));
10304 if (!args
.empty ())
10308 gdb_argv
argv (args
.c_str ());
10309 for (i
= 0; argv
[i
] != NULL
; i
++)
10311 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
10312 error (_("Argument list too long for run packet"));
10313 rs
->buf
[len
++] = ';';
10314 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
.data () + len
,
10319 rs
->buf
[len
++] = '\0';
10322 getpkt (&rs
->buf
, 0);
10324 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]))
10327 /* We have a wait response. All is well. */
10329 case PACKET_UNKNOWN
:
10332 if (remote_exec_file
[0] == '\0')
10333 error (_("Running the default executable on the remote target failed; "
10334 "try \"set remote exec-file\"?"));
10336 error (_("Running \"%s\" on the remote target failed"),
10339 gdb_assert_not_reached ("bad switch");
10343 /* Helper function to send set/unset environment packets. ACTION is
10344 either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
10345 or "QEnvironmentUnsetVariable". VALUE is the variable to be
10349 remote_target::send_environment_packet (const char *action
,
10350 const char *packet
,
10353 remote_state
*rs
= get_remote_state ();
10355 /* Convert the environment variable to an hex string, which
10356 is the best format to be transmitted over the wire. */
10357 std::string encoded_value
= bin2hex ((const gdb_byte
*) value
,
10360 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10361 "%s:%s", packet
, encoded_value
.c_str ());
10364 getpkt (&rs
->buf
, 0);
10365 if (strcmp (rs
->buf
.data (), "OK") != 0)
10366 warning (_("Unable to %s environment variable '%s' on remote."),
10370 /* Helper function to handle the QEnvironment* packets. */
10373 remote_target::extended_remote_environment_support ()
10375 remote_state
*rs
= get_remote_state ();
10377 if (packet_support (PACKET_QEnvironmentReset
) != PACKET_DISABLE
)
10379 putpkt ("QEnvironmentReset");
10380 getpkt (&rs
->buf
, 0);
10381 if (strcmp (rs
->buf
.data (), "OK") != 0)
10382 warning (_("Unable to reset environment on remote."));
10385 gdb_environ
*e
= ¤t_inferior ()->environment
;
10387 if (packet_support (PACKET_QEnvironmentHexEncoded
) != PACKET_DISABLE
)
10388 for (const std::string
&el
: e
->user_set_env ())
10389 send_environment_packet ("set", "QEnvironmentHexEncoded",
10392 if (packet_support (PACKET_QEnvironmentUnset
) != PACKET_DISABLE
)
10393 for (const std::string
&el
: e
->user_unset_env ())
10394 send_environment_packet ("unset", "QEnvironmentUnset", el
.c_str ());
10397 /* Helper function to set the current working directory for the
10398 inferior in the remote target. */
10401 remote_target::extended_remote_set_inferior_cwd ()
10403 if (packet_support (PACKET_QSetWorkingDir
) != PACKET_DISABLE
)
10405 const std::string
&inferior_cwd
= current_inferior ()->cwd ();
10406 remote_state
*rs
= get_remote_state ();
10408 if (!inferior_cwd
.empty ())
10410 std::string hexpath
10411 = bin2hex ((const gdb_byte
*) inferior_cwd
.data (),
10412 inferior_cwd
.size ());
10414 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10415 "QSetWorkingDir:%s", hexpath
.c_str ());
10419 /* An empty inferior_cwd means that the user wants us to
10420 reset the remote server's inferior's cwd. */
10421 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10422 "QSetWorkingDir:");
10426 getpkt (&rs
->buf
, 0);
10427 if (packet_ok (rs
->buf
,
10428 &remote_protocol_packets
[PACKET_QSetWorkingDir
])
10431 Remote replied unexpectedly while setting the inferior's working\n\
10438 /* In the extended protocol we want to be able to do things like
10439 "run" and have them basically work as expected. So we need
10440 a special create_inferior function. We support changing the
10441 executable file and the command line arguments, but not the
10445 extended_remote_target::create_inferior (const char *exec_file
,
10446 const std::string
&args
,
10447 char **env
, int from_tty
)
10451 struct remote_state
*rs
= get_remote_state ();
10452 const char *remote_exec_file
= get_remote_exec_file ();
10454 /* If running asynchronously, register the target file descriptor
10455 with the event loop. */
10456 if (target_can_async_p ())
10459 /* Disable address space randomization if requested (and supported). */
10460 if (supports_disable_randomization ())
10461 extended_remote_disable_randomization (disable_randomization
);
10463 /* If startup-with-shell is on, we inform gdbserver to start the
10464 remote inferior using a shell. */
10465 if (packet_support (PACKET_QStartupWithShell
) != PACKET_DISABLE
)
10467 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
10468 "QStartupWithShell:%d", startup_with_shell
? 1 : 0);
10470 getpkt (&rs
->buf
, 0);
10471 if (strcmp (rs
->buf
.data (), "OK") != 0)
10473 Remote replied unexpectedly while setting startup-with-shell: %s"),
10477 extended_remote_environment_support ();
10479 extended_remote_set_inferior_cwd ();
10481 /* Now restart the remote server. */
10482 run_worked
= extended_remote_run (args
) != -1;
10485 /* vRun was not supported. Fail if we need it to do what the
10487 if (remote_exec_file
[0])
10488 error (_("Remote target does not support \"set remote exec-file\""));
10489 if (!args
.empty ())
10490 error (_("Remote target does not support \"set args\" or run ARGS"));
10492 /* Fall back to "R". */
10493 extended_remote_restart ();
10496 /* vRun's success return is a stop reply. */
10497 stop_reply
= run_worked
? rs
->buf
.data () : NULL
;
10498 add_current_inferior_and_thread (stop_reply
);
10500 /* Get updated offsets, if the stub uses qOffsets. */
10505 /* Given a location's target info BP_TGT and the packet buffer BUF, output
10506 the list of conditions (in agent expression bytecode format), if any, the
10507 target needs to evaluate. The output is placed into the packet buffer
10508 started from BUF and ended at BUF_END. */
10511 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
10512 struct bp_target_info
*bp_tgt
, char *buf
,
10515 if (bp_tgt
->conditions
.empty ())
10518 buf
+= strlen (buf
);
10519 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
10522 /* Send conditions to the target. */
10523 for (agent_expr
*aexpr
: bp_tgt
->conditions
)
10525 xsnprintf (buf
, buf_end
- buf
, "X%x,", aexpr
->len
);
10526 buf
+= strlen (buf
);
10527 for (int i
= 0; i
< aexpr
->len
; ++i
)
10528 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10535 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
10536 struct bp_target_info
*bp_tgt
, char *buf
)
10538 if (bp_tgt
->tcommands
.empty ())
10541 buf
+= strlen (buf
);
10543 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
10544 buf
+= strlen (buf
);
10546 /* Concatenate all the agent expressions that are commands into the
10548 for (agent_expr
*aexpr
: bp_tgt
->tcommands
)
10550 sprintf (buf
, "X%x,", aexpr
->len
);
10551 buf
+= strlen (buf
);
10552 for (int i
= 0; i
< aexpr
->len
; ++i
)
10553 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
10558 /* Insert a breakpoint. On targets that have software breakpoint
10559 support, we ask the remote target to do the work; on targets
10560 which don't, we insert a traditional memory breakpoint. */
10563 remote_target::insert_breakpoint (struct gdbarch
*gdbarch
,
10564 struct bp_target_info
*bp_tgt
)
10566 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
10567 If it succeeds, then set the support to PACKET_ENABLE. If it
10568 fails, and the user has explicitly requested the Z support then
10569 report an error, otherwise, mark it disabled and go on. */
10571 if (packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10573 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10574 struct remote_state
*rs
;
10577 /* Make sure the remote is pointing at the right process, if
10579 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10580 set_general_process ();
10582 rs
= get_remote_state ();
10583 p
= rs
->buf
.data ();
10584 endbuf
= p
+ get_remote_packet_size ();
10589 addr
= (ULONGEST
) remote_address_masked (addr
);
10590 p
+= hexnumstr (p
, addr
);
10591 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10593 if (supports_evaluation_of_breakpoint_conditions ())
10594 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10596 if (can_run_breakpoint_commands ())
10597 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10600 getpkt (&rs
->buf
, 0);
10602 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
10608 case PACKET_UNKNOWN
:
10613 /* If this breakpoint has target-side commands but this stub doesn't
10614 support Z0 packets, throw error. */
10615 if (!bp_tgt
->tcommands
.empty ())
10616 throw_error (NOT_SUPPORTED_ERROR
, _("\
10617 Target doesn't support breakpoints that have target side commands."));
10619 return memory_insert_breakpoint (this, gdbarch
, bp_tgt
);
10623 remote_target::remove_breakpoint (struct gdbarch
*gdbarch
,
10624 struct bp_target_info
*bp_tgt
,
10625 enum remove_bp_reason reason
)
10627 CORE_ADDR addr
= bp_tgt
->placed_address
;
10628 struct remote_state
*rs
= get_remote_state ();
10630 if (packet_support (PACKET_Z0
) != PACKET_DISABLE
)
10632 char *p
= rs
->buf
.data ();
10633 char *endbuf
= p
+ get_remote_packet_size ();
10635 /* Make sure the remote is pointing at the right process, if
10637 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10638 set_general_process ();
10644 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
10645 p
+= hexnumstr (p
, addr
);
10646 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->kind
);
10649 getpkt (&rs
->buf
, 0);
10651 return (rs
->buf
[0] == 'E');
10654 return memory_remove_breakpoint (this, gdbarch
, bp_tgt
, reason
);
10657 static enum Z_packet_type
10658 watchpoint_to_Z_packet (int type
)
10663 return Z_PACKET_WRITE_WP
;
10666 return Z_PACKET_READ_WP
;
10669 return Z_PACKET_ACCESS_WP
;
10672 internal_error (__FILE__
, __LINE__
,
10673 _("hw_bp_to_z: bad watchpoint type %d"), type
);
10678 remote_target::insert_watchpoint (CORE_ADDR addr
, int len
,
10679 enum target_hw_bp_type type
, struct expression
*cond
)
10681 struct remote_state
*rs
= get_remote_state ();
10682 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10684 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10686 if (packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10689 /* Make sure the remote is pointing at the right process, if
10691 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10692 set_general_process ();
10694 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "Z%x,", packet
);
10695 p
= strchr (rs
->buf
.data (), '\0');
10696 addr
= remote_address_masked (addr
);
10697 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10698 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10701 getpkt (&rs
->buf
, 0);
10703 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
10707 case PACKET_UNKNOWN
:
10712 internal_error (__FILE__
, __LINE__
,
10713 _("remote_insert_watchpoint: reached end of function"));
10717 remote_target::watchpoint_addr_within_range (CORE_ADDR addr
,
10718 CORE_ADDR start
, int length
)
10720 CORE_ADDR diff
= remote_address_masked (addr
- start
);
10722 return diff
< length
;
10727 remote_target::remove_watchpoint (CORE_ADDR addr
, int len
,
10728 enum target_hw_bp_type type
, struct expression
*cond
)
10730 struct remote_state
*rs
= get_remote_state ();
10731 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
10733 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
10735 if (packet_support (PACKET_Z0
+ packet
) == PACKET_DISABLE
)
10738 /* Make sure the remote is pointing at the right process, if
10740 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10741 set_general_process ();
10743 xsnprintf (rs
->buf
.data (), endbuf
- rs
->buf
.data (), "z%x,", packet
);
10744 p
= strchr (rs
->buf
.data (), '\0');
10745 addr
= remote_address_masked (addr
);
10746 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10747 xsnprintf (p
, endbuf
- p
, ",%x", len
);
10749 getpkt (&rs
->buf
, 0);
10751 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
10754 case PACKET_UNKNOWN
:
10759 internal_error (__FILE__
, __LINE__
,
10760 _("remote_remove_watchpoint: reached end of function"));
10764 static int remote_hw_watchpoint_limit
= -1;
10765 static int remote_hw_watchpoint_length_limit
= -1;
10766 static int remote_hw_breakpoint_limit
= -1;
10769 remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
10771 if (remote_hw_watchpoint_length_limit
== 0)
10773 else if (remote_hw_watchpoint_length_limit
< 0)
10775 else if (len
<= remote_hw_watchpoint_length_limit
)
10782 remote_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int ot
)
10784 if (type
== bp_hardware_breakpoint
)
10786 if (remote_hw_breakpoint_limit
== 0)
10788 else if (remote_hw_breakpoint_limit
< 0)
10790 else if (cnt
<= remote_hw_breakpoint_limit
)
10795 if (remote_hw_watchpoint_limit
== 0)
10797 else if (remote_hw_watchpoint_limit
< 0)
10801 else if (cnt
<= remote_hw_watchpoint_limit
)
10807 /* The to_stopped_by_sw_breakpoint method of target remote. */
10810 remote_target::stopped_by_sw_breakpoint ()
10812 struct thread_info
*thread
= inferior_thread ();
10814 return (thread
->priv
!= NULL
10815 && (get_remote_thread_info (thread
)->stop_reason
10816 == TARGET_STOPPED_BY_SW_BREAKPOINT
));
10819 /* The to_supports_stopped_by_sw_breakpoint method of target
10823 remote_target::supports_stopped_by_sw_breakpoint ()
10825 return (packet_support (PACKET_swbreak_feature
) == PACKET_ENABLE
);
10828 /* The to_stopped_by_hw_breakpoint method of target remote. */
10831 remote_target::stopped_by_hw_breakpoint ()
10833 struct thread_info
*thread
= inferior_thread ();
10835 return (thread
->priv
!= NULL
10836 && (get_remote_thread_info (thread
)->stop_reason
10837 == TARGET_STOPPED_BY_HW_BREAKPOINT
));
10840 /* The to_supports_stopped_by_hw_breakpoint method of target
10844 remote_target::supports_stopped_by_hw_breakpoint ()
10846 return (packet_support (PACKET_hwbreak_feature
) == PACKET_ENABLE
);
10850 remote_target::stopped_by_watchpoint ()
10852 struct thread_info
*thread
= inferior_thread ();
10854 return (thread
->priv
!= NULL
10855 && (get_remote_thread_info (thread
)->stop_reason
10856 == TARGET_STOPPED_BY_WATCHPOINT
));
10860 remote_target::stopped_data_address (CORE_ADDR
*addr_p
)
10862 struct thread_info
*thread
= inferior_thread ();
10864 if (thread
->priv
!= NULL
10865 && (get_remote_thread_info (thread
)->stop_reason
10866 == TARGET_STOPPED_BY_WATCHPOINT
))
10868 *addr_p
= get_remote_thread_info (thread
)->watch_data_address
;
10877 remote_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
10878 struct bp_target_info
*bp_tgt
)
10880 CORE_ADDR addr
= bp_tgt
->reqstd_address
;
10881 struct remote_state
*rs
;
10885 if (packet_support (PACKET_Z1
) == PACKET_DISABLE
)
10888 /* Make sure the remote is pointing at the right process, if
10890 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10891 set_general_process ();
10893 rs
= get_remote_state ();
10894 p
= rs
->buf
.data ();
10895 endbuf
= p
+ get_remote_packet_size ();
10901 addr
= remote_address_masked (addr
);
10902 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10903 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
10905 if (supports_evaluation_of_breakpoint_conditions ())
10906 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
10908 if (can_run_breakpoint_commands ())
10909 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
10912 getpkt (&rs
->buf
, 0);
10914 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
10917 if (rs
->buf
[1] == '.')
10919 message
= strchr (&rs
->buf
[2], '.');
10921 error (_("Remote failure reply: %s"), message
+ 1);
10924 case PACKET_UNKNOWN
:
10929 internal_error (__FILE__
, __LINE__
,
10930 _("remote_insert_hw_breakpoint: reached end of function"));
10935 remote_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
10936 struct bp_target_info
*bp_tgt
)
10939 struct remote_state
*rs
= get_remote_state ();
10940 char *p
= rs
->buf
.data ();
10941 char *endbuf
= p
+ get_remote_packet_size ();
10943 if (packet_support (PACKET_Z1
) == PACKET_DISABLE
)
10946 /* Make sure the remote is pointing at the right process, if
10948 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
10949 set_general_process ();
10955 addr
= remote_address_masked (bp_tgt
->placed_address
);
10956 p
+= hexnumstr (p
, (ULONGEST
) addr
);
10957 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->kind
);
10960 getpkt (&rs
->buf
, 0);
10962 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
10965 case PACKET_UNKNOWN
:
10970 internal_error (__FILE__
, __LINE__
,
10971 _("remote_remove_hw_breakpoint: reached end of function"));
10974 /* Verify memory using the "qCRC:" request. */
10977 remote_target::verify_memory (const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
10979 struct remote_state
*rs
= get_remote_state ();
10980 unsigned long host_crc
, target_crc
;
10983 /* It doesn't make sense to use qCRC if the remote target is
10984 connected but not running. */
10985 if (target_has_execution ()
10986 && packet_support (PACKET_qCRC
) != PACKET_DISABLE
)
10988 enum packet_result result
;
10990 /* Make sure the remote is pointing at the right process. */
10991 set_general_process ();
10993 /* FIXME: assumes lma can fit into long. */
10994 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
10995 (long) lma
, (long) size
);
10998 /* Be clever; compute the host_crc before waiting for target
11000 host_crc
= xcrc32 (data
, size
, 0xffffffff);
11002 getpkt (&rs
->buf
, 0);
11004 result
= packet_ok (rs
->buf
,
11005 &remote_protocol_packets
[PACKET_qCRC
]);
11006 if (result
== PACKET_ERROR
)
11008 else if (result
== PACKET_OK
)
11010 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
11011 target_crc
= target_crc
* 16 + fromhex (*tmp
);
11013 return (host_crc
== target_crc
);
11017 return simple_verify_memory (this, data
, lma
, size
);
11020 /* compare-sections command
11022 With no arguments, compares each loadable section in the exec bfd
11023 with the same memory range on the target, and reports mismatches.
11024 Useful for verifying the image on the target against the exec file. */
11027 compare_sections_command (const char *args
, int from_tty
)
11030 const char *sectname
;
11031 bfd_size_type size
;
11034 int mismatched
= 0;
11038 if (!current_program_space
->exec_bfd ())
11039 error (_("command cannot be used without an exec file"));
11041 if (args
!= NULL
&& strcmp (args
, "-r") == 0)
11047 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
11049 if (!(s
->flags
& SEC_LOAD
))
11050 continue; /* Skip non-loadable section. */
11052 if (read_only
&& (s
->flags
& SEC_READONLY
) == 0)
11053 continue; /* Skip writeable sections */
11055 size
= bfd_section_size (s
);
11057 continue; /* Skip zero-length section. */
11059 sectname
= bfd_section_name (s
);
11060 if (args
&& strcmp (args
, sectname
) != 0)
11061 continue; /* Not the section selected by user. */
11063 matched
= 1; /* Do this section. */
11066 gdb::byte_vector
sectdata (size
);
11067 bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
11068 sectdata
.data (), 0, size
);
11070 res
= target_verify_memory (sectdata
.data (), lma
, size
);
11073 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
11074 paddress (target_gdbarch (), lma
),
11075 paddress (target_gdbarch (), lma
+ size
));
11077 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
11078 paddress (target_gdbarch (), lma
),
11079 paddress (target_gdbarch (), lma
+ size
));
11081 printf_filtered ("matched.\n");
11084 printf_filtered ("MIS-MATCHED!\n");
11088 if (mismatched
> 0)
11089 warning (_("One or more sections of the target image does not match\n\
11090 the loaded file\n"));
11091 if (args
&& !matched
)
11092 printf_filtered (_("No loaded section named '%s'.\n"), args
);
11095 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
11096 into remote target. The number of bytes written to the remote
11097 target is returned, or -1 for error. */
11100 remote_target::remote_write_qxfer (const char *object_name
,
11101 const char *annex
, const gdb_byte
*writebuf
,
11102 ULONGEST offset
, LONGEST len
,
11103 ULONGEST
*xfered_len
,
11104 struct packet_config
*packet
)
11108 struct remote_state
*rs
= get_remote_state ();
11109 int max_size
= get_memory_write_packet_size ();
11111 if (packet_config_support (packet
) == PACKET_DISABLE
)
11112 return TARGET_XFER_E_IO
;
11114 /* Insert header. */
11115 i
= snprintf (rs
->buf
.data (), max_size
,
11116 "qXfer:%s:write:%s:%s:",
11117 object_name
, annex
? annex
: "",
11118 phex_nz (offset
, sizeof offset
));
11119 max_size
-= (i
+ 1);
11121 /* Escape as much data as fits into rs->buf. */
11122 buf_len
= remote_escape_output
11123 (writebuf
, len
, 1, (gdb_byte
*) rs
->buf
.data () + i
, &max_size
, max_size
);
11125 if (putpkt_binary (rs
->buf
.data (), i
+ buf_len
) < 0
11126 || getpkt_sane (&rs
->buf
, 0) < 0
11127 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11128 return TARGET_XFER_E_IO
;
11130 unpack_varlen_hex (rs
->buf
.data (), &n
);
11133 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11136 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
11137 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
11138 number of bytes read is returned, or 0 for EOF, or -1 for error.
11139 The number of bytes read may be less than LEN without indicating an
11140 EOF. PACKET is checked and updated to indicate whether the remote
11141 target supports this object. */
11144 remote_target::remote_read_qxfer (const char *object_name
,
11146 gdb_byte
*readbuf
, ULONGEST offset
,
11148 ULONGEST
*xfered_len
,
11149 struct packet_config
*packet
)
11151 struct remote_state
*rs
= get_remote_state ();
11152 LONGEST i
, n
, packet_len
;
11154 if (packet_config_support (packet
) == PACKET_DISABLE
)
11155 return TARGET_XFER_E_IO
;
11157 /* Check whether we've cached an end-of-object packet that matches
11159 if (rs
->finished_object
)
11161 if (strcmp (object_name
, rs
->finished_object
) == 0
11162 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
11163 && offset
== rs
->finished_offset
)
11164 return TARGET_XFER_EOF
;
11167 /* Otherwise, we're now reading something different. Discard
11169 xfree (rs
->finished_object
);
11170 xfree (rs
->finished_annex
);
11171 rs
->finished_object
= NULL
;
11172 rs
->finished_annex
= NULL
;
11175 /* Request only enough to fit in a single packet. The actual data
11176 may not, since we don't know how much of it will need to be escaped;
11177 the target is free to respond with slightly less data. We subtract
11178 five to account for the response type and the protocol frame. */
11179 n
= std::min
<LONGEST
> (get_remote_packet_size () - 5, len
);
11180 snprintf (rs
->buf
.data (), get_remote_packet_size () - 4,
11181 "qXfer:%s:read:%s:%s,%s",
11182 object_name
, annex
? annex
: "",
11183 phex_nz (offset
, sizeof offset
),
11184 phex_nz (n
, sizeof n
));
11185 i
= putpkt (rs
->buf
);
11187 return TARGET_XFER_E_IO
;
11190 packet_len
= getpkt_sane (&rs
->buf
, 0);
11191 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11192 return TARGET_XFER_E_IO
;
11194 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
11195 error (_("Unknown remote qXfer reply: %s"), rs
->buf
.data ());
11197 /* 'm' means there is (or at least might be) more data after this
11198 batch. That does not make sense unless there's at least one byte
11199 of data in this reply. */
11200 if (rs
->buf
[0] == 'm' && packet_len
== 1)
11201 error (_("Remote qXfer reply contained no data."));
11203 /* Got some data. */
11204 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
.data () + 1,
11205 packet_len
- 1, readbuf
, n
);
11207 /* 'l' is an EOF marker, possibly including a final block of data,
11208 or possibly empty. If we have the final block of a non-empty
11209 object, record this fact to bypass a subsequent partial read. */
11210 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
11212 rs
->finished_object
= xstrdup (object_name
);
11213 rs
->finished_annex
= xstrdup (annex
? annex
: "");
11214 rs
->finished_offset
= offset
+ i
;
11218 return TARGET_XFER_EOF
;
11222 return TARGET_XFER_OK
;
11226 enum target_xfer_status
11227 remote_target::xfer_partial (enum target_object object
,
11228 const char *annex
, gdb_byte
*readbuf
,
11229 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
11230 ULONGEST
*xfered_len
)
11232 struct remote_state
*rs
;
11236 int unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
11238 set_remote_traceframe ();
11239 set_general_thread (inferior_ptid
);
11241 rs
= get_remote_state ();
11243 /* Handle memory using the standard memory routines. */
11244 if (object
== TARGET_OBJECT_MEMORY
)
11246 /* If the remote target is connected but not running, we should
11247 pass this request down to a lower stratum (e.g. the executable
11249 if (!target_has_execution ())
11250 return TARGET_XFER_EOF
;
11252 if (writebuf
!= NULL
)
11253 return remote_write_bytes (offset
, writebuf
, len
, unit_size
,
11256 return remote_read_bytes (offset
, readbuf
, len
, unit_size
,
11260 /* Handle extra signal info using qxfer packets. */
11261 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
11264 return remote_read_qxfer ("siginfo", annex
, readbuf
, offset
, len
,
11265 xfered_len
, &remote_protocol_packets
11266 [PACKET_qXfer_siginfo_read
]);
11268 return remote_write_qxfer ("siginfo", annex
,
11269 writebuf
, offset
, len
, xfered_len
,
11270 &remote_protocol_packets
11271 [PACKET_qXfer_siginfo_write
]);
11274 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
11277 return remote_read_qxfer ("statictrace", annex
,
11278 readbuf
, offset
, len
, xfered_len
,
11279 &remote_protocol_packets
11280 [PACKET_qXfer_statictrace_read
]);
11282 return TARGET_XFER_E_IO
;
11285 /* Only handle flash writes. */
11286 if (writebuf
!= NULL
)
11290 case TARGET_OBJECT_FLASH
:
11291 return remote_flash_write (offset
, len
, xfered_len
,
11295 return TARGET_XFER_E_IO
;
11299 /* Map pre-existing objects onto letters. DO NOT do this for new
11300 objects!!! Instead specify new query packets. */
11303 case TARGET_OBJECT_AVR
:
11307 case TARGET_OBJECT_AUXV
:
11308 gdb_assert (annex
== NULL
);
11309 return remote_read_qxfer ("auxv", annex
, readbuf
, offset
, len
,
11311 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
11313 case TARGET_OBJECT_AVAILABLE_FEATURES
:
11314 return remote_read_qxfer
11315 ("features", annex
, readbuf
, offset
, len
, xfered_len
,
11316 &remote_protocol_packets
[PACKET_qXfer_features
]);
11318 case TARGET_OBJECT_LIBRARIES
:
11319 return remote_read_qxfer
11320 ("libraries", annex
, readbuf
, offset
, len
, xfered_len
,
11321 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
11323 case TARGET_OBJECT_LIBRARIES_SVR4
:
11324 return remote_read_qxfer
11325 ("libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
11326 &remote_protocol_packets
[PACKET_qXfer_libraries_svr4
]);
11328 case TARGET_OBJECT_MEMORY_MAP
:
11329 gdb_assert (annex
== NULL
);
11330 return remote_read_qxfer ("memory-map", annex
, readbuf
, offset
, len
,
11332 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
11334 case TARGET_OBJECT_OSDATA
:
11335 /* Should only get here if we're connected. */
11336 gdb_assert (rs
->remote_desc
);
11337 return remote_read_qxfer
11338 ("osdata", annex
, readbuf
, offset
, len
, xfered_len
,
11339 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
11341 case TARGET_OBJECT_THREADS
:
11342 gdb_assert (annex
== NULL
);
11343 return remote_read_qxfer ("threads", annex
, readbuf
, offset
, len
,
11345 &remote_protocol_packets
[PACKET_qXfer_threads
]);
11347 case TARGET_OBJECT_TRACEFRAME_INFO
:
11348 gdb_assert (annex
== NULL
);
11349 return remote_read_qxfer
11350 ("traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
11351 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
11353 case TARGET_OBJECT_FDPIC
:
11354 return remote_read_qxfer ("fdpic", annex
, readbuf
, offset
, len
,
11356 &remote_protocol_packets
[PACKET_qXfer_fdpic
]);
11358 case TARGET_OBJECT_OPENVMS_UIB
:
11359 return remote_read_qxfer ("uib", annex
, readbuf
, offset
, len
,
11361 &remote_protocol_packets
[PACKET_qXfer_uib
]);
11363 case TARGET_OBJECT_BTRACE
:
11364 return remote_read_qxfer ("btrace", annex
, readbuf
, offset
, len
,
11366 &remote_protocol_packets
[PACKET_qXfer_btrace
]);
11368 case TARGET_OBJECT_BTRACE_CONF
:
11369 return remote_read_qxfer ("btrace-conf", annex
, readbuf
, offset
,
11371 &remote_protocol_packets
[PACKET_qXfer_btrace_conf
]);
11373 case TARGET_OBJECT_EXEC_FILE
:
11374 return remote_read_qxfer ("exec-file", annex
, readbuf
, offset
,
11376 &remote_protocol_packets
[PACKET_qXfer_exec_file
]);
11379 return TARGET_XFER_E_IO
;
11382 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
11383 large enough let the caller deal with it. */
11384 if (len
< get_remote_packet_size ())
11385 return TARGET_XFER_E_IO
;
11386 len
= get_remote_packet_size ();
11388 /* Except for querying the minimum buffer size, target must be open. */
11389 if (!rs
->remote_desc
)
11390 error (_("remote query is only available after target open"));
11392 gdb_assert (annex
!= NULL
);
11393 gdb_assert (readbuf
!= NULL
);
11395 p2
= rs
->buf
.data ();
11397 *p2
++ = query_type
;
11399 /* We used one buffer char for the remote protocol q command and
11400 another for the query type. As the remote protocol encapsulation
11401 uses 4 chars plus one extra in case we are debugging
11402 (remote_debug), we have PBUFZIZ - 7 left to pack the query
11405 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
11407 /* Bad caller may have sent forbidden characters. */
11408 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
11413 gdb_assert (annex
[i
] == '\0');
11415 i
= putpkt (rs
->buf
);
11417 return TARGET_XFER_E_IO
;
11419 getpkt (&rs
->buf
, 0);
11420 strcpy ((char *) readbuf
, rs
->buf
.data ());
11422 *xfered_len
= strlen ((char *) readbuf
);
11423 return (*xfered_len
!= 0) ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
11426 /* Implementation of to_get_memory_xfer_limit. */
11429 remote_target::get_memory_xfer_limit ()
11431 return get_memory_write_packet_size ();
11435 remote_target::search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
11436 const gdb_byte
*pattern
, ULONGEST pattern_len
,
11437 CORE_ADDR
*found_addrp
)
11439 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
11440 struct remote_state
*rs
= get_remote_state ();
11441 int max_size
= get_memory_write_packet_size ();
11442 struct packet_config
*packet
=
11443 &remote_protocol_packets
[PACKET_qSearch_memory
];
11444 /* Number of packet bytes used to encode the pattern;
11445 this could be more than PATTERN_LEN due to escape characters. */
11446 int escaped_pattern_len
;
11447 /* Amount of pattern that was encodable in the packet. */
11448 int used_pattern_len
;
11451 ULONGEST found_addr
;
11453 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
11455 return (target_read (this, TARGET_OBJECT_MEMORY
, NULL
, result
, addr
, len
)
11459 /* Don't go to the target if we don't have to. This is done before
11460 checking packet_config_support to avoid the possibility that a
11461 success for this edge case means the facility works in
11463 if (pattern_len
> search_space_len
)
11465 if (pattern_len
== 0)
11467 *found_addrp
= start_addr
;
11471 /* If we already know the packet isn't supported, fall back to the simple
11472 way of searching memory. */
11474 if (packet_config_support (packet
) == PACKET_DISABLE
)
11476 /* Target doesn't provided special support, fall back and use the
11477 standard support (copy memory and do the search here). */
11478 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11479 pattern
, pattern_len
, found_addrp
);
11482 /* Make sure the remote is pointing at the right process. */
11483 set_general_process ();
11485 /* Insert header. */
11486 i
= snprintf (rs
->buf
.data (), max_size
,
11487 "qSearch:memory:%s;%s;",
11488 phex_nz (start_addr
, addr_size
),
11489 phex_nz (search_space_len
, sizeof (search_space_len
)));
11490 max_size
-= (i
+ 1);
11492 /* Escape as much data as fits into rs->buf. */
11493 escaped_pattern_len
=
11494 remote_escape_output (pattern
, pattern_len
, 1,
11495 (gdb_byte
*) rs
->buf
.data () + i
,
11496 &used_pattern_len
, max_size
);
11498 /* Bail if the pattern is too large. */
11499 if (used_pattern_len
!= pattern_len
)
11500 error (_("Pattern is too large to transmit to remote target."));
11502 if (putpkt_binary (rs
->buf
.data (), i
+ escaped_pattern_len
) < 0
11503 || getpkt_sane (&rs
->buf
, 0) < 0
11504 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
11506 /* The request may not have worked because the command is not
11507 supported. If so, fall back to the simple way. */
11508 if (packet_config_support (packet
) == PACKET_DISABLE
)
11510 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
11511 pattern
, pattern_len
, found_addrp
);
11516 if (rs
->buf
[0] == '0')
11518 else if (rs
->buf
[0] == '1')
11521 if (rs
->buf
[1] != ',')
11522 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11523 unpack_varlen_hex (&rs
->buf
[2], &found_addr
);
11524 *found_addrp
= found_addr
;
11527 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
.data ());
11533 remote_target::rcmd (const char *command
, struct ui_file
*outbuf
)
11535 struct remote_state
*rs
= get_remote_state ();
11536 char *p
= rs
->buf
.data ();
11538 if (!rs
->remote_desc
)
11539 error (_("remote rcmd is only available after target open"));
11541 /* Send a NULL command across as an empty command. */
11542 if (command
== NULL
)
11545 /* The query prefix. */
11546 strcpy (rs
->buf
.data (), "qRcmd,");
11547 p
= strchr (rs
->buf
.data (), '\0');
11549 if ((strlen (rs
->buf
.data ()) + strlen (command
) * 2 + 8/*misc*/)
11550 > get_remote_packet_size ())
11551 error (_("\"monitor\" command ``%s'' is too long."), command
);
11553 /* Encode the actual command. */
11554 bin2hex ((const gdb_byte
*) command
, p
, strlen (command
));
11556 if (putpkt (rs
->buf
) < 0)
11557 error (_("Communication problem with target."));
11559 /* get/display the response */
11564 /* XXX - see also remote_get_noisy_reply(). */
11565 QUIT
; /* Allow user to bail out with ^C. */
11567 if (getpkt_sane (&rs
->buf
, 0) == -1)
11569 /* Timeout. Continue to (try to) read responses.
11570 This is better than stopping with an error, assuming the stub
11571 is still executing the (long) monitor command.
11572 If needed, the user can interrupt gdb using C-c, obtaining
11573 an effect similar to stop on timeout. */
11576 buf
= rs
->buf
.data ();
11577 if (buf
[0] == '\0')
11578 error (_("Target does not support this command."));
11579 if (buf
[0] == 'O' && buf
[1] != 'K')
11581 remote_console_output (buf
+ 1); /* 'O' message from stub. */
11584 if (strcmp (buf
, "OK") == 0)
11586 if (strlen (buf
) == 3 && buf
[0] == 'E'
11587 && isdigit (buf
[1]) && isdigit (buf
[2]))
11589 error (_("Protocol error with Rcmd"));
11591 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
11593 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
11595 fputc_unfiltered (c
, outbuf
);
11601 std::vector
<mem_region
>
11602 remote_target::memory_map ()
11604 std::vector
<mem_region
> result
;
11605 gdb::optional
<gdb::char_vector
> text
11606 = target_read_stralloc (current_inferior ()->top_target (),
11607 TARGET_OBJECT_MEMORY_MAP
, NULL
);
11610 result
= parse_memory_map (text
->data ());
11615 /* Set of callbacks used to implement the 'maint packet' command. */
11617 struct cli_packet_command_callbacks
: public send_remote_packet_callbacks
11619 /* Called before the packet is sent. BUF is the packet content before
11620 the protocol specific prefix, suffix, and escaping is added. */
11622 void sending (gdb::array_view
<const char> &buf
) override
11624 puts_filtered ("sending: ");
11625 print_packet (buf
);
11626 puts_filtered ("\n");
11629 /* Called with BUF, the reply from the remote target. */
11631 void received (gdb::array_view
<const char> &buf
) override
11633 puts_filtered ("received: \"");
11634 print_packet (buf
);
11635 puts_filtered ("\"\n");
11640 /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
11641 '\x??' with '??' replaced by the hexadecimal value of the byte. */
11644 print_packet (gdb::array_view
<const char> &buf
)
11648 for (int i
= 0; i
< buf
.size (); ++i
)
11650 gdb_byte c
= buf
[i
];
11652 fputc_unfiltered (c
, &stb
);
11654 fprintf_unfiltered (&stb
, "\\x%02x", (unsigned char) c
);
11657 puts_filtered (stb
.string ().c_str ());
11661 /* See remote.h. */
11664 send_remote_packet (gdb::array_view
<const char> &buf
,
11665 send_remote_packet_callbacks
*callbacks
)
11667 if (buf
.size () == 0 || buf
.data ()[0] == '\0')
11668 error (_("a remote packet must not be empty"));
11670 remote_target
*remote
= get_current_remote_target ();
11671 if (remote
== nullptr)
11672 error (_("packets can only be sent to a remote target"));
11674 callbacks
->sending (buf
);
11676 remote
->putpkt_binary (buf
.data (), buf
.size ());
11677 remote_state
*rs
= remote
->get_remote_state ();
11678 int bytes
= remote
->getpkt_sane (&rs
->buf
, 0);
11681 error (_("error while fetching packet from remote target"));
11683 gdb::array_view
<const char> view (&rs
->buf
[0], bytes
);
11684 callbacks
->received (view
);
11687 /* Entry point for the 'maint packet' command. */
11690 cli_packet_command (const char *args
, int from_tty
)
11692 cli_packet_command_callbacks cb
;
11693 gdb::array_view
<const char> view
11694 = gdb::make_array_view (args
, args
== nullptr ? 0 : strlen (args
));
11695 send_remote_packet (view
, &cb
);
11699 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
11701 static void display_thread_info (struct gdb_ext_thread_info
*info
);
11703 static void threadset_test_cmd (char *cmd
, int tty
);
11705 static void threadalive_test (char *cmd
, int tty
);
11707 static void threadlist_test_cmd (char *cmd
, int tty
);
11709 int get_and_display_threadinfo (threadref
*ref
);
11711 static void threadinfo_test_cmd (char *cmd
, int tty
);
11713 static int thread_display_step (threadref
*ref
, void *context
);
11715 static void threadlist_update_test_cmd (char *cmd
, int tty
);
11717 static void init_remote_threadtests (void);
11719 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
11722 threadset_test_cmd (const char *cmd
, int tty
)
11724 int sample_thread
= SAMPLE_THREAD
;
11726 printf_filtered (_("Remote threadset test\n"));
11727 set_general_thread (sample_thread
);
11732 threadalive_test (const char *cmd
, int tty
)
11734 int sample_thread
= SAMPLE_THREAD
;
11735 int pid
= inferior_ptid
.pid ();
11736 ptid_t ptid
= ptid_t (pid
, sample_thread
, 0);
11738 if (remote_thread_alive (ptid
))
11739 printf_filtered ("PASS: Thread alive test\n");
11741 printf_filtered ("FAIL: Thread alive test\n");
11744 void output_threadid (char *title
, threadref
*ref
);
11747 output_threadid (char *title
, threadref
*ref
)
11751 pack_threadid (&hexid
[0], ref
); /* Convert thread id into hex. */
11753 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
11757 threadlist_test_cmd (const char *cmd
, int tty
)
11760 threadref nextthread
;
11761 int done
, result_count
;
11762 threadref threadlist
[3];
11764 printf_filtered ("Remote Threadlist test\n");
11765 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
11766 &result_count
, &threadlist
[0]))
11767 printf_filtered ("FAIL: threadlist test\n");
11770 threadref
*scan
= threadlist
;
11771 threadref
*limit
= scan
+ result_count
;
11773 while (scan
< limit
)
11774 output_threadid (" thread ", scan
++);
11779 display_thread_info (struct gdb_ext_thread_info
*info
)
11781 output_threadid ("Threadid: ", &info
->threadid
);
11782 printf_filtered ("Name: %s\n ", info
->shortname
);
11783 printf_filtered ("State: %s\n", info
->display
);
11784 printf_filtered ("other: %s\n\n", info
->more_display
);
11788 get_and_display_threadinfo (threadref
*ref
)
11792 struct gdb_ext_thread_info threadinfo
;
11794 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
11795 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
11796 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
11797 display_thread_info (&threadinfo
);
11802 threadinfo_test_cmd (const char *cmd
, int tty
)
11804 int athread
= SAMPLE_THREAD
;
11808 int_to_threadref (&thread
, athread
);
11809 printf_filtered ("Remote Threadinfo test\n");
11810 if (!get_and_display_threadinfo (&thread
))
11811 printf_filtered ("FAIL cannot get thread info\n");
11815 thread_display_step (threadref
*ref
, void *context
)
11817 /* output_threadid(" threadstep ",ref); *//* simple test */
11818 return get_and_display_threadinfo (ref
);
11822 threadlist_update_test_cmd (const char *cmd
, int tty
)
11824 printf_filtered ("Remote Threadlist update test\n");
11825 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
11829 init_remote_threadtests (void)
11831 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
11832 _("Fetch and print the remote list of "
11833 "thread identifiers, one pkt only."));
11834 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
11835 _("Fetch and display info about one thread."));
11836 add_com ("tset", class_obscure
, threadset_test_cmd
,
11837 _("Test setting to a different thread."));
11838 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
11839 _("Iterate through updating all remote thread info."));
11840 add_com ("talive", class_obscure
, threadalive_test
,
11841 _("Remote thread alive test."));
11846 /* Convert a thread ID to a string. */
11849 remote_target::pid_to_str (ptid_t ptid
)
11851 struct remote_state
*rs
= get_remote_state ();
11853 if (ptid
== null_ptid
)
11854 return normal_pid_to_str (ptid
);
11855 else if (ptid
.is_pid ())
11857 /* Printing an inferior target id. */
11859 /* When multi-process extensions are off, there's no way in the
11860 remote protocol to know the remote process id, if there's any
11861 at all. There's one exception --- when we're connected with
11862 target extended-remote, and we manually attached to a process
11863 with "attach PID". We don't record anywhere a flag that
11864 allows us to distinguish that case from the case of
11865 connecting with extended-remote and the stub already being
11866 attached to a process, and reporting yes to qAttached, hence
11867 no smart special casing here. */
11868 if (!remote_multi_process_p (rs
))
11869 return "Remote target";
11871 return normal_pid_to_str (ptid
);
11875 if (magic_null_ptid
== ptid
)
11876 return "Thread <main>";
11877 else if (remote_multi_process_p (rs
))
11878 if (ptid
.lwp () == 0)
11879 return normal_pid_to_str (ptid
);
11881 return string_printf ("Thread %d.%ld",
11882 ptid
.pid (), ptid
.lwp ());
11884 return string_printf ("Thread %ld", ptid
.lwp ());
11888 /* Get the address of the thread local variable in OBJFILE which is
11889 stored at OFFSET within the thread local storage for thread PTID. */
11892 remote_target::get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
,
11895 if (packet_support (PACKET_qGetTLSAddr
) != PACKET_DISABLE
)
11897 struct remote_state
*rs
= get_remote_state ();
11898 char *p
= rs
->buf
.data ();
11899 char *endp
= p
+ get_remote_packet_size ();
11900 enum packet_result result
;
11902 strcpy (p
, "qGetTLSAddr:");
11904 p
= write_ptid (p
, endp
, ptid
);
11906 p
+= hexnumstr (p
, offset
);
11908 p
+= hexnumstr (p
, lm
);
11912 getpkt (&rs
->buf
, 0);
11913 result
= packet_ok (rs
->buf
,
11914 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
11915 if (result
== PACKET_OK
)
11919 unpack_varlen_hex (rs
->buf
.data (), &addr
);
11922 else if (result
== PACKET_UNKNOWN
)
11923 throw_error (TLS_GENERIC_ERROR
,
11924 _("Remote target doesn't support qGetTLSAddr packet"));
11926 throw_error (TLS_GENERIC_ERROR
,
11927 _("Remote target failed to process qGetTLSAddr request"));
11930 throw_error (TLS_GENERIC_ERROR
,
11931 _("TLS not supported or disabled on this target"));
11936 /* Provide thread local base, i.e. Thread Information Block address.
11937 Returns 1 if ptid is found and thread_local_base is non zero. */
11940 remote_target::get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
11942 if (packet_support (PACKET_qGetTIBAddr
) != PACKET_DISABLE
)
11944 struct remote_state
*rs
= get_remote_state ();
11945 char *p
= rs
->buf
.data ();
11946 char *endp
= p
+ get_remote_packet_size ();
11947 enum packet_result result
;
11949 strcpy (p
, "qGetTIBAddr:");
11951 p
= write_ptid (p
, endp
, ptid
);
11955 getpkt (&rs
->buf
, 0);
11956 result
= packet_ok (rs
->buf
,
11957 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
11958 if (result
== PACKET_OK
)
11961 unpack_varlen_hex (rs
->buf
.data (), &val
);
11963 *addr
= (CORE_ADDR
) val
;
11966 else if (result
== PACKET_UNKNOWN
)
11967 error (_("Remote target doesn't support qGetTIBAddr packet"));
11969 error (_("Remote target failed to process qGetTIBAddr request"));
11972 error (_("qGetTIBAddr not supported or disabled on this target"));
11977 /* Support for inferring a target description based on the current
11978 architecture and the size of a 'g' packet. While the 'g' packet
11979 can have any size (since optional registers can be left off the
11980 end), some sizes are easily recognizable given knowledge of the
11981 approximate architecture. */
11983 struct remote_g_packet_guess
11985 remote_g_packet_guess (int bytes_
, const struct target_desc
*tdesc_
)
11992 const struct target_desc
*tdesc
;
11995 struct remote_g_packet_data
: public allocate_on_obstack
11997 std::vector
<remote_g_packet_guess
> guesses
;
12000 static struct gdbarch_data
*remote_g_packet_data_handle
;
12003 remote_g_packet_data_init (struct obstack
*obstack
)
12005 return new (obstack
) remote_g_packet_data
;
12009 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
12010 const struct target_desc
*tdesc
)
12012 struct remote_g_packet_data
*data
12013 = ((struct remote_g_packet_data
*)
12014 gdbarch_data (gdbarch
, remote_g_packet_data_handle
));
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
12034 = ((struct remote_g_packet_data
*)
12035 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
));
12037 return !data
->guesses
.empty ();
12040 const struct target_desc
*
12041 remote_target::read_description ()
12043 struct remote_g_packet_data
*data
12044 = ((struct remote_g_packet_data
*)
12045 gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
));
12047 /* Do not try this during initial connection, when we do not know
12048 whether there is a running but stopped thread. */
12049 if (!target_has_execution () || inferior_ptid
== null_ptid
)
12050 return beneath ()->read_description ();
12052 if (!data
->guesses
.empty ())
12054 int bytes
= send_g_packet ();
12056 for (const remote_g_packet_guess
&guess
: data
->guesses
)
12057 if (guess
.bytes
== bytes
)
12058 return guess
.tdesc
;
12060 /* We discard the g packet. A minor optimization would be to
12061 hold on to it, and fill the register cache once we have selected
12062 an architecture, but it's too tricky to do safely. */
12065 return beneath ()->read_description ();
12068 /* Remote file transfer support. This is host-initiated I/O, not
12069 target-initiated; for target-initiated, see remote-fileio.c. */
12071 /* If *LEFT is at least the length of STRING, copy STRING to
12072 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12073 decrease *LEFT. Otherwise raise an error. */
12076 remote_buffer_add_string (char **buffer
, int *left
, const char *string
)
12078 int len
= strlen (string
);
12081 error (_("Packet too long for target."));
12083 memcpy (*buffer
, string
, len
);
12087 /* NUL-terminate the buffer as a convenience, if there is
12093 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
12094 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12095 decrease *LEFT. Otherwise raise an error. */
12098 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
12101 if (2 * len
> *left
)
12102 error (_("Packet too long for target."));
12104 bin2hex (bytes
, *buffer
, len
);
12105 *buffer
+= 2 * len
;
12108 /* NUL-terminate the buffer as a convenience, if there is
12114 /* If *LEFT is large enough, convert VALUE to hex and add it to
12115 *BUFFER, update *BUFFER to point to the new end of the buffer, and
12116 decrease *LEFT. Otherwise raise an error. */
12119 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
12121 int len
= hexnumlen (value
);
12124 error (_("Packet too long for target."));
12126 hexnumstr (*buffer
, value
);
12130 /* NUL-terminate the buffer as a convenience, if there is
12136 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
12137 value, *REMOTE_ERRNO to the remote error number or zero if none
12138 was included, and *ATTACHMENT to point to the start of the annex
12139 if any. The length of the packet isn't needed here; there may
12140 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
12142 Return 0 if the packet could be parsed, -1 if it could not. If
12143 -1 is returned, the other variables may not be initialized. */
12146 remote_hostio_parse_result (const char *buffer
, int *retcode
,
12147 int *remote_errno
, const char **attachment
)
12152 *attachment
= NULL
;
12154 if (buffer
[0] != 'F')
12158 *retcode
= strtol (&buffer
[1], &p
, 16);
12159 if (errno
!= 0 || p
== &buffer
[1])
12162 /* Check for ",errno". */
12166 *remote_errno
= strtol (p
+ 1, &p2
, 16);
12167 if (errno
!= 0 || p
+ 1 == p2
)
12172 /* Check for ";attachment". If there is no attachment, the
12173 packet should end here. */
12176 *attachment
= p
+ 1;
12179 else if (*p
== '\0')
12185 /* Send a prepared I/O packet to the target and read its response.
12186 The prepared packet is in the global RS->BUF before this function
12187 is called, and the answer is there when we return.
12189 COMMAND_BYTES is the length of the request to send, which may include
12190 binary data. WHICH_PACKET is the packet configuration to check
12191 before attempting a packet. If an error occurs, *REMOTE_ERRNO
12192 is set to the error number and -1 is returned. Otherwise the value
12193 returned by the function is returned.
12195 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
12196 attachment is expected; an error will be reported if there's a
12197 mismatch. If one is found, *ATTACHMENT will be set to point into
12198 the packet buffer and *ATTACHMENT_LEN will be set to the
12199 attachment's length. */
12202 remote_target::remote_hostio_send_command (int command_bytes
, int which_packet
,
12203 int *remote_errno
, const char **attachment
,
12204 int *attachment_len
)
12206 struct remote_state
*rs
= get_remote_state ();
12207 int ret
, bytes_read
;
12208 const char *attachment_tmp
;
12210 if (packet_support (which_packet
) == PACKET_DISABLE
)
12212 *remote_errno
= FILEIO_ENOSYS
;
12216 putpkt_binary (rs
->buf
.data (), command_bytes
);
12217 bytes_read
= getpkt_sane (&rs
->buf
, 0);
12219 /* If it timed out, something is wrong. Don't try to parse the
12221 if (bytes_read
< 0)
12223 *remote_errno
= FILEIO_EINVAL
;
12227 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
12230 *remote_errno
= FILEIO_EINVAL
;
12232 case PACKET_UNKNOWN
:
12233 *remote_errno
= FILEIO_ENOSYS
;
12239 if (remote_hostio_parse_result (rs
->buf
.data (), &ret
, remote_errno
,
12242 *remote_errno
= FILEIO_EINVAL
;
12246 /* Make sure we saw an attachment if and only if we expected one. */
12247 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
12248 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
12250 *remote_errno
= FILEIO_EINVAL
;
12254 /* If an attachment was found, it must point into the packet buffer;
12255 work out how many bytes there were. */
12256 if (attachment_tmp
!= NULL
)
12258 *attachment
= attachment_tmp
;
12259 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
.data ());
12265 /* See declaration.h. */
12268 readahead_cache::invalidate ()
12273 /* See declaration.h. */
12276 readahead_cache::invalidate_fd (int fd
)
12278 if (this->fd
== fd
)
12282 /* Set the filesystem remote_hostio functions that take FILENAME
12283 arguments will use. Return 0 on success, or -1 if an error
12284 occurs (and set *REMOTE_ERRNO). */
12287 remote_target::remote_hostio_set_filesystem (struct inferior
*inf
,
12290 struct remote_state
*rs
= get_remote_state ();
12291 int required_pid
= (inf
== NULL
|| inf
->fake_pid_p
) ? 0 : inf
->pid
;
12292 char *p
= rs
->buf
.data ();
12293 int left
= get_remote_packet_size () - 1;
12297 if (packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12300 if (rs
->fs_pid
!= -1 && required_pid
== rs
->fs_pid
)
12303 remote_buffer_add_string (&p
, &left
, "vFile:setfs:");
12305 xsnprintf (arg
, sizeof (arg
), "%x", required_pid
);
12306 remote_buffer_add_string (&p
, &left
, arg
);
12308 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_setfs
,
12309 remote_errno
, NULL
, NULL
);
12311 if (packet_support (PACKET_vFile_setfs
) == PACKET_DISABLE
)
12315 rs
->fs_pid
= required_pid
;
12320 /* Implementation of to_fileio_open. */
12323 remote_target::remote_hostio_open (inferior
*inf
, const char *filename
,
12324 int flags
, int mode
, int warn_if_slow
,
12327 struct remote_state
*rs
= get_remote_state ();
12328 char *p
= rs
->buf
.data ();
12329 int left
= get_remote_packet_size () - 1;
12333 static int warning_issued
= 0;
12335 printf_unfiltered (_("Reading %s from remote target...\n"),
12338 if (!warning_issued
)
12340 warning (_("File transfers from remote targets can be slow."
12341 " Use \"set sysroot\" to access files locally"
12343 warning_issued
= 1;
12347 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12350 remote_buffer_add_string (&p
, &left
, "vFile:open:");
12352 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12353 strlen (filename
));
12354 remote_buffer_add_string (&p
, &left
, ",");
12356 remote_buffer_add_int (&p
, &left
, flags
);
12357 remote_buffer_add_string (&p
, &left
, ",");
12359 remote_buffer_add_int (&p
, &left
, mode
);
12361 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_open
,
12362 remote_errno
, NULL
, NULL
);
12366 remote_target::fileio_open (struct inferior
*inf
, const char *filename
,
12367 int flags
, int mode
, int warn_if_slow
,
12370 return remote_hostio_open (inf
, filename
, flags
, mode
, warn_if_slow
,
12374 /* Implementation of to_fileio_pwrite. */
12377 remote_target::remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12378 ULONGEST offset
, int *remote_errno
)
12380 struct remote_state
*rs
= get_remote_state ();
12381 char *p
= rs
->buf
.data ();
12382 int left
= get_remote_packet_size ();
12385 rs
->readahead_cache
.invalidate_fd (fd
);
12387 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
12389 remote_buffer_add_int (&p
, &left
, fd
);
12390 remote_buffer_add_string (&p
, &left
, ",");
12392 remote_buffer_add_int (&p
, &left
, offset
);
12393 remote_buffer_add_string (&p
, &left
, ",");
12395 p
+= remote_escape_output (write_buf
, len
, 1, (gdb_byte
*) p
, &out_len
,
12396 (get_remote_packet_size ()
12397 - (p
- rs
->buf
.data ())));
12399 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pwrite
,
12400 remote_errno
, NULL
, NULL
);
12404 remote_target::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
12405 ULONGEST offset
, int *remote_errno
)
12407 return remote_hostio_pwrite (fd
, write_buf
, len
, offset
, remote_errno
);
12410 /* Helper for the implementation of to_fileio_pread. Read the file
12411 from the remote side with vFile:pread. */
12414 remote_target::remote_hostio_pread_vFile (int fd
, gdb_byte
*read_buf
, int len
,
12415 ULONGEST offset
, int *remote_errno
)
12417 struct remote_state
*rs
= get_remote_state ();
12418 char *p
= rs
->buf
.data ();
12419 const char *attachment
;
12420 int left
= get_remote_packet_size ();
12421 int ret
, attachment_len
;
12424 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
12426 remote_buffer_add_int (&p
, &left
, fd
);
12427 remote_buffer_add_string (&p
, &left
, ",");
12429 remote_buffer_add_int (&p
, &left
, len
);
12430 remote_buffer_add_string (&p
, &left
, ",");
12432 remote_buffer_add_int (&p
, &left
, offset
);
12434 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_pread
,
12435 remote_errno
, &attachment
,
12441 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12443 if (read_len
!= ret
)
12444 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
12449 /* See declaration.h. */
12452 readahead_cache::pread (int fd
, gdb_byte
*read_buf
, size_t len
,
12456 && this->offset
<= offset
12457 && offset
< this->offset
+ this->bufsize
)
12459 ULONGEST max
= this->offset
+ this->bufsize
;
12461 if (offset
+ len
> max
)
12462 len
= max
- offset
;
12464 memcpy (read_buf
, this->buf
+ offset
- this->offset
, len
);
12471 /* Implementation of to_fileio_pread. */
12474 remote_target::remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12475 ULONGEST offset
, int *remote_errno
)
12478 struct remote_state
*rs
= get_remote_state ();
12479 readahead_cache
*cache
= &rs
->readahead_cache
;
12481 ret
= cache
->pread (fd
, read_buf
, len
, offset
);
12484 cache
->hit_count
++;
12486 remote_debug_printf ("readahead cache hit %s",
12487 pulongest (cache
->hit_count
));
12491 cache
->miss_count
++;
12493 remote_debug_printf ("readahead cache miss %s",
12494 pulongest (cache
->miss_count
));
12497 cache
->offset
= offset
;
12498 cache
->bufsize
= get_remote_packet_size ();
12499 cache
->buf
= (gdb_byte
*) xrealloc (cache
->buf
, cache
->bufsize
);
12501 ret
= remote_hostio_pread_vFile (cache
->fd
, cache
->buf
, cache
->bufsize
,
12502 cache
->offset
, remote_errno
);
12505 cache
->invalidate_fd (fd
);
12509 cache
->bufsize
= ret
;
12510 return cache
->pread (fd
, read_buf
, len
, offset
);
12514 remote_target::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
12515 ULONGEST offset
, int *remote_errno
)
12517 return remote_hostio_pread (fd
, read_buf
, len
, offset
, remote_errno
);
12520 /* Implementation of to_fileio_close. */
12523 remote_target::remote_hostio_close (int fd
, int *remote_errno
)
12525 struct remote_state
*rs
= get_remote_state ();
12526 char *p
= rs
->buf
.data ();
12527 int left
= get_remote_packet_size () - 1;
12529 rs
->readahead_cache
.invalidate_fd (fd
);
12531 remote_buffer_add_string (&p
, &left
, "vFile:close:");
12533 remote_buffer_add_int (&p
, &left
, fd
);
12535 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_close
,
12536 remote_errno
, NULL
, NULL
);
12540 remote_target::fileio_close (int fd
, int *remote_errno
)
12542 return remote_hostio_close (fd
, remote_errno
);
12545 /* Implementation of to_fileio_unlink. */
12548 remote_target::remote_hostio_unlink (inferior
*inf
, const char *filename
,
12551 struct remote_state
*rs
= get_remote_state ();
12552 char *p
= rs
->buf
.data ();
12553 int left
= get_remote_packet_size () - 1;
12555 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12558 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
12560 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12561 strlen (filename
));
12563 return remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_unlink
,
12564 remote_errno
, NULL
, NULL
);
12568 remote_target::fileio_unlink (struct inferior
*inf
, const char *filename
,
12571 return remote_hostio_unlink (inf
, filename
, remote_errno
);
12574 /* Implementation of to_fileio_readlink. */
12576 gdb::optional
<std::string
>
12577 remote_target::fileio_readlink (struct inferior
*inf
, const char *filename
,
12580 struct remote_state
*rs
= get_remote_state ();
12581 char *p
= rs
->buf
.data ();
12582 const char *attachment
;
12583 int left
= get_remote_packet_size ();
12584 int len
, attachment_len
;
12587 if (remote_hostio_set_filesystem (inf
, remote_errno
) != 0)
12590 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
12592 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
12593 strlen (filename
));
12595 len
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_readlink
,
12596 remote_errno
, &attachment
,
12602 std::string
ret (len
, '\0');
12604 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12605 (gdb_byte
*) &ret
[0], len
);
12606 if (read_len
!= len
)
12607 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
12612 /* Implementation of to_fileio_fstat. */
12615 remote_target::fileio_fstat (int fd
, struct stat
*st
, int *remote_errno
)
12617 struct remote_state
*rs
= get_remote_state ();
12618 char *p
= rs
->buf
.data ();
12619 int left
= get_remote_packet_size ();
12620 int attachment_len
, ret
;
12621 const char *attachment
;
12622 struct fio_stat fst
;
12625 remote_buffer_add_string (&p
, &left
, "vFile:fstat:");
12627 remote_buffer_add_int (&p
, &left
, fd
);
12629 ret
= remote_hostio_send_command (p
- rs
->buf
.data (), PACKET_vFile_fstat
,
12630 remote_errno
, &attachment
,
12634 if (*remote_errno
!= FILEIO_ENOSYS
)
12637 /* Strictly we should return -1, ENOSYS here, but when
12638 "set sysroot remote:" was implemented in August 2008
12639 BFD's need for a stat function was sidestepped with
12640 this hack. This was not remedied until March 2015
12641 so we retain the previous behavior to avoid breaking
12644 Note that the memset is a March 2015 addition; older
12645 GDBs set st_size *and nothing else* so the structure
12646 would have garbage in all other fields. This might
12647 break something but retaining the previous behavior
12648 here would be just too wrong. */
12650 memset (st
, 0, sizeof (struct stat
));
12651 st
->st_size
= INT_MAX
;
12655 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
12656 (gdb_byte
*) &fst
, sizeof (fst
));
12658 if (read_len
!= ret
)
12659 error (_("vFile:fstat returned %d, but %d bytes."), ret
, read_len
);
12661 if (read_len
!= sizeof (fst
))
12662 error (_("vFile:fstat returned %d bytes, but expecting %d."),
12663 read_len
, (int) sizeof (fst
));
12665 remote_fileio_to_host_stat (&fst
, st
);
12670 /* Implementation of to_filesystem_is_local. */
12673 remote_target::filesystem_is_local ()
12675 /* Valgrind GDB presents itself as a remote target but works
12676 on the local filesystem: it does not implement remote get
12677 and users are not expected to set a sysroot. To handle
12678 this case we treat the remote filesystem as local if the
12679 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
12680 does not support vFile:open. */
12681 if (gdb_sysroot
== TARGET_SYSROOT_PREFIX
)
12683 enum packet_support ps
= packet_support (PACKET_vFile_open
);
12685 if (ps
== PACKET_SUPPORT_UNKNOWN
)
12687 int fd
, remote_errno
;
12689 /* Try opening a file to probe support. The supplied
12690 filename is irrelevant, we only care about whether
12691 the stub recognizes the packet or not. */
12692 fd
= remote_hostio_open (NULL
, "just probing",
12693 FILEIO_O_RDONLY
, 0700, 0,
12697 remote_hostio_close (fd
, &remote_errno
);
12699 ps
= packet_support (PACKET_vFile_open
);
12702 if (ps
== PACKET_DISABLE
)
12704 static int warning_issued
= 0;
12706 if (!warning_issued
)
12708 warning (_("remote target does not support file"
12709 " transfer, attempting to access files"
12710 " from local filesystem."));
12711 warning_issued
= 1;
12722 remote_fileio_errno_to_host (int errnum
)
12728 case FILEIO_ENOENT
:
12736 case FILEIO_EACCES
:
12738 case FILEIO_EFAULT
:
12742 case FILEIO_EEXIST
:
12744 case FILEIO_ENODEV
:
12746 case FILEIO_ENOTDIR
:
12748 case FILEIO_EISDIR
:
12750 case FILEIO_EINVAL
:
12752 case FILEIO_ENFILE
:
12754 case FILEIO_EMFILE
:
12758 case FILEIO_ENOSPC
:
12760 case FILEIO_ESPIPE
:
12764 case FILEIO_ENOSYS
:
12766 case FILEIO_ENAMETOOLONG
:
12767 return ENAMETOOLONG
;
12773 remote_hostio_error (int errnum
)
12775 int host_error
= remote_fileio_errno_to_host (errnum
);
12777 if (host_error
== -1)
12778 error (_("Unknown remote I/O error %d"), errnum
);
12780 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
12783 /* A RAII wrapper around a remote file descriptor. */
12785 class scoped_remote_fd
12788 scoped_remote_fd (remote_target
*remote
, int fd
)
12789 : m_remote (remote
), m_fd (fd
)
12793 ~scoped_remote_fd ()
12800 m_remote
->remote_hostio_close (m_fd
, &remote_errno
);
12804 /* Swallow exception before it escapes the dtor. If
12805 something goes wrong, likely the connection is gone,
12806 and there's nothing else that can be done. */
12811 DISABLE_COPY_AND_ASSIGN (scoped_remote_fd
);
12813 /* Release ownership of the file descriptor, and return it. */
12814 ATTRIBUTE_UNUSED_RESULT
int release () noexcept
12821 /* Return the owned file descriptor. */
12822 int get () const noexcept
12828 /* The remote target. */
12829 remote_target
*m_remote
;
12831 /* The owned remote I/O file descriptor. */
12836 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
12838 remote_target
*remote
= get_current_remote_target ();
12840 if (remote
== nullptr)
12841 error (_("command can only be used with remote target"));
12843 remote
->remote_file_put (local_file
, remote_file
, from_tty
);
12847 remote_target::remote_file_put (const char *local_file
, const char *remote_file
,
12850 int retcode
, remote_errno
, bytes
, io_size
;
12851 int bytes_in_buffer
;
12855 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "rb");
12857 perror_with_name (local_file
);
12859 scoped_remote_fd fd
12860 (this, remote_hostio_open (NULL
,
12861 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
12863 0700, 0, &remote_errno
));
12864 if (fd
.get () == -1)
12865 remote_hostio_error (remote_errno
);
12867 /* Send up to this many bytes at once. They won't all fit in the
12868 remote packet limit, so we'll transfer slightly fewer. */
12869 io_size
= get_remote_packet_size ();
12870 gdb::byte_vector
buffer (io_size
);
12872 bytes_in_buffer
= 0;
12875 while (bytes_in_buffer
|| !saw_eof
)
12879 bytes
= fread (buffer
.data () + bytes_in_buffer
, 1,
12880 io_size
- bytes_in_buffer
,
12884 if (ferror (file
.get ()))
12885 error (_("Error reading %s."), local_file
);
12888 /* EOF. Unless there is something still in the
12889 buffer from the last iteration, we are done. */
12891 if (bytes_in_buffer
== 0)
12899 bytes
+= bytes_in_buffer
;
12900 bytes_in_buffer
= 0;
12902 retcode
= remote_hostio_pwrite (fd
.get (), buffer
.data (), bytes
,
12903 offset
, &remote_errno
);
12906 remote_hostio_error (remote_errno
);
12907 else if (retcode
== 0)
12908 error (_("Remote write of %d bytes returned 0!"), bytes
);
12909 else if (retcode
< bytes
)
12911 /* Short write. Save the rest of the read data for the next
12913 bytes_in_buffer
= bytes
- retcode
;
12914 memmove (buffer
.data (), buffer
.data () + retcode
, bytes_in_buffer
);
12920 if (remote_hostio_close (fd
.release (), &remote_errno
))
12921 remote_hostio_error (remote_errno
);
12924 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
12928 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
12930 remote_target
*remote
= get_current_remote_target ();
12932 if (remote
== nullptr)
12933 error (_("command can only be used with remote target"));
12935 remote
->remote_file_get (remote_file
, local_file
, from_tty
);
12939 remote_target::remote_file_get (const char *remote_file
, const char *local_file
,
12942 int remote_errno
, bytes
, io_size
;
12945 scoped_remote_fd fd
12946 (this, remote_hostio_open (NULL
,
12947 remote_file
, FILEIO_O_RDONLY
, 0, 0,
12949 if (fd
.get () == -1)
12950 remote_hostio_error (remote_errno
);
12952 gdb_file_up file
= gdb_fopen_cloexec (local_file
, "wb");
12954 perror_with_name (local_file
);
12956 /* Send up to this many bytes at once. They won't all fit in the
12957 remote packet limit, so we'll transfer slightly fewer. */
12958 io_size
= get_remote_packet_size ();
12959 gdb::byte_vector
buffer (io_size
);
12964 bytes
= remote_hostio_pread (fd
.get (), buffer
.data (), io_size
, offset
,
12967 /* Success, but no bytes, means end-of-file. */
12970 remote_hostio_error (remote_errno
);
12974 bytes
= fwrite (buffer
.data (), 1, bytes
, file
.get ());
12976 perror_with_name (local_file
);
12979 if (remote_hostio_close (fd
.release (), &remote_errno
))
12980 remote_hostio_error (remote_errno
);
12983 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
12987 remote_file_delete (const char *remote_file
, int from_tty
)
12989 remote_target
*remote
= get_current_remote_target ();
12991 if (remote
== nullptr)
12992 error (_("command can only be used with remote target"));
12994 remote
->remote_file_delete (remote_file
, from_tty
);
12998 remote_target::remote_file_delete (const char *remote_file
, int from_tty
)
13000 int retcode
, remote_errno
;
13002 retcode
= remote_hostio_unlink (NULL
, remote_file
, &remote_errno
);
13004 remote_hostio_error (remote_errno
);
13007 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
13011 remote_put_command (const char *args
, int from_tty
)
13014 error_no_arg (_("file to put"));
13016 gdb_argv
argv (args
);
13017 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13018 error (_("Invalid parameters to remote put"));
13020 remote_file_put (argv
[0], argv
[1], from_tty
);
13024 remote_get_command (const char *args
, int from_tty
)
13027 error_no_arg (_("file to get"));
13029 gdb_argv
argv (args
);
13030 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
13031 error (_("Invalid parameters to remote get"));
13033 remote_file_get (argv
[0], argv
[1], from_tty
);
13037 remote_delete_command (const char *args
, int from_tty
)
13040 error_no_arg (_("file to delete"));
13042 gdb_argv
argv (args
);
13043 if (argv
[0] == NULL
|| argv
[1] != NULL
)
13044 error (_("Invalid parameters to remote delete"));
13046 remote_file_delete (argv
[0], from_tty
);
13050 remote_target::can_execute_reverse ()
13052 if (packet_support (PACKET_bs
) == PACKET_ENABLE
13053 || packet_support (PACKET_bc
) == PACKET_ENABLE
)
13060 remote_target::supports_non_stop ()
13066 remote_target::supports_disable_randomization ()
13068 /* Only supported in extended mode. */
13073 remote_target::supports_multi_process ()
13075 struct remote_state
*rs
= get_remote_state ();
13077 return remote_multi_process_p (rs
);
13081 remote_supports_cond_tracepoints ()
13083 return packet_support (PACKET_ConditionalTracepoints
) == PACKET_ENABLE
;
13087 remote_target::supports_evaluation_of_breakpoint_conditions ()
13089 return packet_support (PACKET_ConditionalBreakpoints
) == PACKET_ENABLE
;
13093 remote_supports_fast_tracepoints ()
13095 return packet_support (PACKET_FastTracepoints
) == PACKET_ENABLE
;
13099 remote_supports_static_tracepoints ()
13101 return packet_support (PACKET_StaticTracepoints
) == PACKET_ENABLE
;
13105 remote_supports_install_in_trace ()
13107 return packet_support (PACKET_InstallInTrace
) == PACKET_ENABLE
;
13111 remote_target::supports_enable_disable_tracepoint ()
13113 return (packet_support (PACKET_EnableDisableTracepoints_feature
)
13118 remote_target::supports_string_tracing ()
13120 return packet_support (PACKET_tracenz_feature
) == PACKET_ENABLE
;
13124 remote_target::can_run_breakpoint_commands ()
13126 return packet_support (PACKET_BreakpointCommands
) == PACKET_ENABLE
;
13130 remote_target::trace_init ()
13132 struct remote_state
*rs
= get_remote_state ();
13135 remote_get_noisy_reply ();
13136 if (strcmp (rs
->buf
.data (), "OK") != 0)
13137 error (_("Target does not support this command."));
13140 /* Recursive routine to walk through command list including loops, and
13141 download packets for each command. */
13144 remote_target::remote_download_command_source (int num
, ULONGEST addr
,
13145 struct command_line
*cmds
)
13147 struct remote_state
*rs
= get_remote_state ();
13148 struct command_line
*cmd
;
13150 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
13152 QUIT
; /* Allow user to bail out with ^C. */
13153 strcpy (rs
->buf
.data (), "QTDPsrc:");
13154 encode_source_string (num
, addr
, "cmd", cmd
->line
,
13155 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13156 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13158 remote_get_noisy_reply ();
13159 if (strcmp (rs
->buf
.data (), "OK"))
13160 warning (_("Target does not support source download."));
13162 if (cmd
->control_type
== while_control
13163 || cmd
->control_type
== while_stepping_control
)
13165 remote_download_command_source (num
, addr
, cmd
->body_list_0
.get ());
13167 QUIT
; /* Allow user to bail out with ^C. */
13168 strcpy (rs
->buf
.data (), "QTDPsrc:");
13169 encode_source_string (num
, addr
, "cmd", "end",
13170 rs
->buf
.data () + strlen (rs
->buf
.data ()),
13171 rs
->buf
.size () - strlen (rs
->buf
.data ()));
13173 remote_get_noisy_reply ();
13174 if (strcmp (rs
->buf
.data (), "OK"))
13175 warning (_("Target does not support source download."));
13181 remote_target::download_tracepoint (struct bp_location
*loc
)
13185 std::vector
<std::string
> tdp_actions
;
13186 std::vector
<std::string
> stepping_actions
;
13188 struct breakpoint
*b
= loc
->owner
;
13189 struct tracepoint
*t
= (struct tracepoint
*) b
;
13190 struct remote_state
*rs
= get_remote_state ();
13192 const char *err_msg
= _("Tracepoint packet too large for target.");
13195 /* We use a buffer other than rs->buf because we'll build strings
13196 across multiple statements, and other statements in between could
13198 gdb::char_vector
buf (get_remote_packet_size ());
13200 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
13202 tpaddr
= loc
->address
;
13203 strcpy (addrbuf
, phex (tpaddr
, sizeof (CORE_ADDR
)));
13204 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:%x:%s:%c:%lx:%x",
13205 b
->number
, addrbuf
, /* address */
13206 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
13207 t
->step_count
, t
->pass_count
);
13209 if (ret
< 0 || ret
>= buf
.size ())
13210 error ("%s", err_msg
);
13212 /* Fast tracepoints are mostly handled by the target, but we can
13213 tell the target how big of an instruction block should be moved
13215 if (b
->type
== bp_fast_tracepoint
)
13217 /* Only test for support at download time; we may not know
13218 target capabilities at definition time. */
13219 if (remote_supports_fast_tracepoints ())
13221 if (gdbarch_fast_tracepoint_valid_at (loc
->gdbarch
, tpaddr
,
13224 size_left
= buf
.size () - strlen (buf
.data ());
13225 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13227 gdb_insn_length (loc
->gdbarch
, tpaddr
));
13229 if (ret
< 0 || ret
>= size_left
)
13230 error ("%s", err_msg
);
13233 /* If it passed validation at definition but fails now,
13234 something is very wrong. */
13235 internal_error (__FILE__
, __LINE__
,
13236 _("Fast tracepoint not "
13237 "valid during download"));
13240 /* Fast tracepoints are functionally identical to regular
13241 tracepoints, so don't take lack of support as a reason to
13242 give up on the trace run. */
13243 warning (_("Target does not support fast tracepoints, "
13244 "downloading %d as regular tracepoint"), b
->number
);
13246 else if (b
->type
== bp_static_tracepoint
)
13248 /* Only test for support at download time; we may not know
13249 target capabilities at definition time. */
13250 if (remote_supports_static_tracepoints ())
13252 struct static_tracepoint_marker marker
;
13254 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
13256 size_left
= buf
.size () - strlen (buf
.data ());
13257 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13260 if (ret
< 0 || ret
>= size_left
)
13261 error ("%s", err_msg
);
13264 error (_("Static tracepoint not valid during download"));
13267 /* Fast tracepoints are functionally identical to regular
13268 tracepoints, so don't take lack of support as a reason
13269 to give up on the trace run. */
13270 error (_("Target does not support static tracepoints"));
13272 /* If the tracepoint has a conditional, make it into an agent
13273 expression and append to the definition. */
13276 /* Only test support at download time, we may not know target
13277 capabilities at definition time. */
13278 if (remote_supports_cond_tracepoints ())
13280 agent_expr_up aexpr
= gen_eval_for_expr (tpaddr
,
13283 size_left
= buf
.size () - strlen (buf
.data ());
13285 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13286 size_left
, ":X%x,", aexpr
->len
);
13288 if (ret
< 0 || ret
>= size_left
)
13289 error ("%s", err_msg
);
13291 size_left
= buf
.size () - strlen (buf
.data ());
13293 /* Two bytes to encode each aexpr byte, plus the terminating
13295 if (aexpr
->len
* 2 + 1 > size_left
)
13296 error ("%s", err_msg
);
13298 pkt
= buf
.data () + strlen (buf
.data ());
13300 for (int ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
13301 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
13305 warning (_("Target does not support conditional tracepoints, "
13306 "ignoring tp %d cond"), b
->number
);
13309 if (b
->commands
|| !default_collect
.empty ())
13311 size_left
= buf
.size () - strlen (buf
.data ());
13313 ret
= snprintf (buf
.data () + strlen (buf
.data ()),
13316 if (ret
< 0 || ret
>= size_left
)
13317 error ("%s", err_msg
);
13320 putpkt (buf
.data ());
13321 remote_get_noisy_reply ();
13322 if (strcmp (rs
->buf
.data (), "OK"))
13323 error (_("Target does not support tracepoints."));
13325 /* do_single_steps (t); */
13326 for (auto action_it
= tdp_actions
.begin ();
13327 action_it
!= tdp_actions
.end (); action_it
++)
13329 QUIT
; /* Allow user to bail out with ^C. */
13331 bool has_more
= ((action_it
+ 1) != tdp_actions
.end ()
13332 || !stepping_actions
.empty ());
13334 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%c",
13335 b
->number
, addrbuf
, /* address */
13336 action_it
->c_str (),
13337 has_more
? '-' : 0);
13339 if (ret
< 0 || ret
>= buf
.size ())
13340 error ("%s", err_msg
);
13342 putpkt (buf
.data ());
13343 remote_get_noisy_reply ();
13344 if (strcmp (rs
->buf
.data (), "OK"))
13345 error (_("Error on target while setting tracepoints."));
13348 for (auto action_it
= stepping_actions
.begin ();
13349 action_it
!= stepping_actions
.end (); action_it
++)
13351 QUIT
; /* Allow user to bail out with ^C. */
13353 bool is_first
= action_it
== stepping_actions
.begin ();
13354 bool has_more
= (action_it
+ 1) != stepping_actions
.end ();
13356 ret
= snprintf (buf
.data (), buf
.size (), "QTDP:-%x:%s:%s%s%s",
13357 b
->number
, addrbuf
, /* address */
13358 is_first
? "S" : "",
13359 action_it
->c_str (),
13360 has_more
? "-" : "");
13362 if (ret
< 0 || ret
>= buf
.size ())
13363 error ("%s", err_msg
);
13365 putpkt (buf
.data ());
13366 remote_get_noisy_reply ();
13367 if (strcmp (rs
->buf
.data (), "OK"))
13368 error (_("Error on target while setting tracepoints."));
13371 if (packet_support (PACKET_TracepointSource
) == PACKET_ENABLE
)
13373 if (b
->location
!= NULL
)
13375 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13377 if (ret
< 0 || ret
>= buf
.size ())
13378 error ("%s", err_msg
);
13380 encode_source_string (b
->number
, loc
->address
, "at",
13381 event_location_to_string (b
->location
.get ()),
13382 buf
.data () + strlen (buf
.data ()),
13383 buf
.size () - strlen (buf
.data ()));
13384 putpkt (buf
.data ());
13385 remote_get_noisy_reply ();
13386 if (strcmp (rs
->buf
.data (), "OK"))
13387 warning (_("Target does not support source download."));
13389 if (b
->cond_string
)
13391 ret
= snprintf (buf
.data (), buf
.size (), "QTDPsrc:");
13393 if (ret
< 0 || ret
>= buf
.size ())
13394 error ("%s", err_msg
);
13396 encode_source_string (b
->number
, loc
->address
,
13397 "cond", b
->cond_string
.get (),
13398 buf
.data () + strlen (buf
.data ()),
13399 buf
.size () - strlen (buf
.data ()));
13400 putpkt (buf
.data ());
13401 remote_get_noisy_reply ();
13402 if (strcmp (rs
->buf
.data (), "OK"))
13403 warning (_("Target does not support source download."));
13405 remote_download_command_source (b
->number
, loc
->address
,
13406 breakpoint_commands (b
));
13411 remote_target::can_download_tracepoint ()
13413 struct remote_state
*rs
= get_remote_state ();
13414 struct trace_status
*ts
;
13417 /* Don't try to install tracepoints until we've relocated our
13418 symbols, and fetched and merged the target's tracepoint list with
13420 if (rs
->starting_up
)
13423 ts
= current_trace_status ();
13424 status
= get_trace_status (ts
);
13426 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
13429 /* If we are in a tracing experiment, but remote stub doesn't support
13430 installing tracepoint in trace, we have to return. */
13431 if (!remote_supports_install_in_trace ())
13439 remote_target::download_trace_state_variable (const trace_state_variable
&tsv
)
13441 struct remote_state
*rs
= get_remote_state ();
13444 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
13445 tsv
.number
, phex ((ULONGEST
) tsv
.initial_value
, 8),
13447 p
= rs
->buf
.data () + strlen (rs
->buf
.data ());
13448 if ((p
- rs
->buf
.data ()) + tsv
.name
.length () * 2
13449 >= get_remote_packet_size ())
13450 error (_("Trace state variable name too long for tsv definition packet"));
13451 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
.name
.data ()), p
, tsv
.name
.length ());
13454 remote_get_noisy_reply ();
13455 if (rs
->buf
[0] == '\0')
13456 error (_("Target does not support this command."));
13457 if (strcmp (rs
->buf
.data (), "OK") != 0)
13458 error (_("Error on target while downloading trace state variable."));
13462 remote_target::enable_tracepoint (struct bp_location
*location
)
13464 struct remote_state
*rs
= get_remote_state ();
13466 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTEnable:%x:%s",
13467 location
->owner
->number
,
13468 phex (location
->address
, sizeof (CORE_ADDR
)));
13470 remote_get_noisy_reply ();
13471 if (rs
->buf
[0] == '\0')
13472 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
13473 if (strcmp (rs
->buf
.data (), "OK") != 0)
13474 error (_("Error on target while enabling tracepoint."));
13478 remote_target::disable_tracepoint (struct bp_location
*location
)
13480 struct remote_state
*rs
= get_remote_state ();
13482 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QTDisable:%x:%s",
13483 location
->owner
->number
,
13484 phex (location
->address
, sizeof (CORE_ADDR
)));
13486 remote_get_noisy_reply ();
13487 if (rs
->buf
[0] == '\0')
13488 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
13489 if (strcmp (rs
->buf
.data (), "OK") != 0)
13490 error (_("Error on target while disabling tracepoint."));
13494 remote_target::trace_set_readonly_regions ()
13497 bfd_size_type size
;
13502 if (!current_program_space
->exec_bfd ())
13503 return; /* No information to give. */
13505 struct remote_state
*rs
= get_remote_state ();
13507 strcpy (rs
->buf
.data (), "QTro");
13508 offset
= strlen (rs
->buf
.data ());
13509 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
13511 char tmp1
[40], tmp2
[40];
13514 if ((s
->flags
& SEC_LOAD
) == 0 ||
13515 /* (s->flags & SEC_CODE) == 0 || */
13516 (s
->flags
& SEC_READONLY
) == 0)
13520 vma
= bfd_section_vma (s
);
13521 size
= bfd_section_size (s
);
13522 sprintf_vma (tmp1
, vma
);
13523 sprintf_vma (tmp2
, vma
+ size
);
13524 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
13525 if (offset
+ sec_length
+ 1 > rs
->buf
.size ())
13527 if (packet_support (PACKET_qXfer_traceframe_info
) != PACKET_ENABLE
)
13529 Too many sections for read-only sections definition packet."));
13532 xsnprintf (rs
->buf
.data () + offset
, rs
->buf
.size () - offset
, ":%s,%s",
13534 offset
+= sec_length
;
13539 getpkt (&rs
->buf
, 0);
13544 remote_target::trace_start ()
13546 struct remote_state
*rs
= get_remote_state ();
13548 putpkt ("QTStart");
13549 remote_get_noisy_reply ();
13550 if (rs
->buf
[0] == '\0')
13551 error (_("Target does not support this command."));
13552 if (strcmp (rs
->buf
.data (), "OK") != 0)
13553 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13557 remote_target::get_trace_status (struct trace_status
*ts
)
13559 /* Initialize it just to avoid a GCC false warning. */
13561 enum packet_result result
;
13562 struct remote_state
*rs
= get_remote_state ();
13564 if (packet_support (PACKET_qTStatus
) == PACKET_DISABLE
)
13567 /* FIXME we need to get register block size some other way. */
13568 trace_regblock_size
13569 = rs
->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet
;
13571 putpkt ("qTStatus");
13575 p
= remote_get_noisy_reply ();
13577 catch (const gdb_exception_error
&ex
)
13579 if (ex
.error
!= TARGET_CLOSE_ERROR
)
13581 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
13587 result
= packet_ok (p
, &remote_protocol_packets
[PACKET_qTStatus
]);
13589 /* If the remote target doesn't do tracing, flag it. */
13590 if (result
== PACKET_UNKNOWN
)
13593 /* We're working with a live target. */
13594 ts
->filename
= NULL
;
13597 error (_("Bogus trace status reply from target: %s"), rs
->buf
.data ());
13599 /* Function 'parse_trace_status' sets default value of each field of
13600 'ts' at first, so we don't have to do it here. */
13601 parse_trace_status (p
, ts
);
13603 return ts
->running
;
13607 remote_target::get_tracepoint_status (struct breakpoint
*bp
,
13608 struct uploaded_tp
*utp
)
13610 struct remote_state
*rs
= get_remote_state ();
13612 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
13613 size_t size
= get_remote_packet_size ();
13618 tp
->traceframe_usage
= 0;
13619 for (bp_location
*loc
: tp
->locations ())
13621 /* If the tracepoint was never downloaded, don't go asking for
13623 if (tp
->number_on_target
== 0)
13625 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", tp
->number_on_target
,
13626 phex_nz (loc
->address
, 0));
13628 reply
= remote_get_noisy_reply ();
13629 if (reply
&& *reply
)
13632 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13638 utp
->hit_count
= 0;
13639 utp
->traceframe_usage
= 0;
13640 xsnprintf (rs
->buf
.data (), size
, "qTP:%x:%s", utp
->number
,
13641 phex_nz (utp
->addr
, 0));
13643 reply
= remote_get_noisy_reply ();
13644 if (reply
&& *reply
)
13647 parse_tracepoint_status (reply
+ 1, bp
, utp
);
13653 remote_target::trace_stop ()
13655 struct remote_state
*rs
= get_remote_state ();
13658 remote_get_noisy_reply ();
13659 if (rs
->buf
[0] == '\0')
13660 error (_("Target does not support this command."));
13661 if (strcmp (rs
->buf
.data (), "OK") != 0)
13662 error (_("Bogus reply from target: %s"), rs
->buf
.data ());
13666 remote_target::trace_find (enum trace_find_type type
, int num
,
13667 CORE_ADDR addr1
, CORE_ADDR addr2
,
13670 struct remote_state
*rs
= get_remote_state ();
13671 char *endbuf
= rs
->buf
.data () + get_remote_packet_size ();
13673 int target_frameno
= -1, target_tracept
= -1;
13675 /* Lookups other than by absolute frame number depend on the current
13676 trace selected, so make sure it is correct on the remote end
13678 if (type
!= tfind_number
)
13679 set_remote_traceframe ();
13681 p
= rs
->buf
.data ();
13682 strcpy (p
, "QTFrame:");
13683 p
= strchr (p
, '\0');
13687 xsnprintf (p
, endbuf
- p
, "%x", num
);
13690 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
13693 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
13696 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
13697 phex_nz (addr2
, 0));
13699 case tfind_outside
:
13700 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
13701 phex_nz (addr2
, 0));
13704 error (_("Unknown trace find type %d"), type
);
13708 reply
= remote_get_noisy_reply ();
13709 if (*reply
== '\0')
13710 error (_("Target does not support this command."));
13712 while (reply
&& *reply
)
13717 target_frameno
= (int) strtol (p
, &reply
, 16);
13719 error (_("Unable to parse trace frame number"));
13720 /* Don't update our remote traceframe number cache on failure
13721 to select a remote traceframe. */
13722 if (target_frameno
== -1)
13727 target_tracept
= (int) strtol (p
, &reply
, 16);
13729 error (_("Unable to parse tracepoint number"));
13731 case 'O': /* "OK"? */
13732 if (reply
[1] == 'K' && reply
[2] == '\0')
13735 error (_("Bogus reply from target: %s"), reply
);
13738 error (_("Bogus reply from target: %s"), reply
);
13741 *tpp
= target_tracept
;
13743 rs
->remote_traceframe_number
= target_frameno
;
13744 return target_frameno
;
13748 remote_target::get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
13750 struct remote_state
*rs
= get_remote_state ();
13754 set_remote_traceframe ();
13756 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTV:%x", tsvnum
);
13758 reply
= remote_get_noisy_reply ();
13759 if (reply
&& *reply
)
13763 unpack_varlen_hex (reply
+ 1, &uval
);
13764 *val
= (LONGEST
) uval
;
13772 remote_target::save_trace_data (const char *filename
)
13774 struct remote_state
*rs
= get_remote_state ();
13777 p
= rs
->buf
.data ();
13778 strcpy (p
, "QTSave:");
13780 if ((p
- rs
->buf
.data ()) + strlen (filename
) * 2
13781 >= get_remote_packet_size ())
13782 error (_("Remote file name too long for trace save packet"));
13783 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
13786 reply
= remote_get_noisy_reply ();
13787 if (*reply
== '\0')
13788 error (_("Target does not support this command."));
13789 if (strcmp (reply
, "OK") != 0)
13790 error (_("Bogus reply from target: %s"), reply
);
13794 /* This is basically a memory transfer, but needs to be its own packet
13795 because we don't know how the target actually organizes its trace
13796 memory, plus we want to be able to ask for as much as possible, but
13797 not be unhappy if we don't get as much as we ask for. */
13800 remote_target::get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
13802 struct remote_state
*rs
= get_remote_state ();
13807 p
= rs
->buf
.data ();
13808 strcpy (p
, "qTBuffer:");
13810 p
+= hexnumstr (p
, offset
);
13812 p
+= hexnumstr (p
, len
);
13816 reply
= remote_get_noisy_reply ();
13817 if (reply
&& *reply
)
13819 /* 'l' by itself means we're at the end of the buffer and
13820 there is nothing more to get. */
13824 /* Convert the reply into binary. Limit the number of bytes to
13825 convert according to our passed-in buffer size, rather than
13826 what was returned in the packet; if the target is
13827 unexpectedly generous and gives us a bigger reply than we
13828 asked for, we don't want to crash. */
13829 rslt
= hex2bin (reply
, buf
, len
);
13833 /* Something went wrong, flag as an error. */
13838 remote_target::set_disconnected_tracing (int val
)
13840 struct remote_state
*rs
= get_remote_state ();
13842 if (packet_support (PACKET_DisconnectedTracing_feature
) == PACKET_ENABLE
)
13846 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13847 "QTDisconnected:%x", val
);
13849 reply
= remote_get_noisy_reply ();
13850 if (*reply
== '\0')
13851 error (_("Target does not support this command."));
13852 if (strcmp (reply
, "OK") != 0)
13853 error (_("Bogus reply from target: %s"), reply
);
13856 warning (_("Target does not support disconnected tracing."));
13860 remote_target::core_of_thread (ptid_t ptid
)
13862 thread_info
*info
= find_thread_ptid (this, ptid
);
13864 if (info
!= NULL
&& info
->priv
!= NULL
)
13865 return get_remote_thread_info (info
)->core
;
13871 remote_target::set_circular_trace_buffer (int val
)
13873 struct remote_state
*rs
= get_remote_state ();
13876 xsnprintf (rs
->buf
.data (), get_remote_packet_size (),
13877 "QTBuffer:circular:%x", val
);
13879 reply
= remote_get_noisy_reply ();
13880 if (*reply
== '\0')
13881 error (_("Target does not support this command."));
13882 if (strcmp (reply
, "OK") != 0)
13883 error (_("Bogus reply from target: %s"), reply
);
13887 remote_target::traceframe_info ()
13889 gdb::optional
<gdb::char_vector
> text
13890 = target_read_stralloc (current_inferior ()->top_target (),
13891 TARGET_OBJECT_TRACEFRAME_INFO
,
13894 return parse_traceframe_info (text
->data ());
13899 /* Handle the qTMinFTPILen packet. Returns the minimum length of
13900 instruction on which a fast tracepoint may be placed. Returns -1
13901 if the packet is not supported, and 0 if the minimum instruction
13902 length is unknown. */
13905 remote_target::get_min_fast_tracepoint_insn_len ()
13907 struct remote_state
*rs
= get_remote_state ();
13910 /* If we're not debugging a process yet, the IPA can't be
13912 if (!target_has_execution ())
13915 /* Make sure the remote is pointing at the right process. */
13916 set_general_process ();
13918 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "qTMinFTPILen");
13920 reply
= remote_get_noisy_reply ();
13921 if (*reply
== '\0')
13925 ULONGEST min_insn_len
;
13927 unpack_varlen_hex (reply
, &min_insn_len
);
13929 return (int) min_insn_len
;
13934 remote_target::set_trace_buffer_size (LONGEST val
)
13936 if (packet_support (PACKET_QTBuffer_size
) != PACKET_DISABLE
)
13938 struct remote_state
*rs
= get_remote_state ();
13939 char *buf
= rs
->buf
.data ();
13940 char *endbuf
= buf
+ get_remote_packet_size ();
13941 enum packet_result result
;
13943 gdb_assert (val
>= 0 || val
== -1);
13944 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
13945 /* Send -1 as literal "-1" to avoid host size dependency. */
13949 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
13952 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
13955 remote_get_noisy_reply ();
13956 result
= packet_ok (rs
->buf
,
13957 &remote_protocol_packets
[PACKET_QTBuffer_size
]);
13959 if (result
!= PACKET_OK
)
13960 warning (_("Bogus reply from target: %s"), rs
->buf
.data ());
13965 remote_target::set_trace_notes (const char *user
, const char *notes
,
13966 const char *stop_notes
)
13968 struct remote_state
*rs
= get_remote_state ();
13970 char *buf
= rs
->buf
.data ();
13971 char *endbuf
= buf
+ get_remote_packet_size ();
13974 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
13977 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
13978 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
13984 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
13985 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
13991 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
13992 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
13996 /* Ensure the buffer is terminated. */
14000 reply
= remote_get_noisy_reply ();
14001 if (*reply
== '\0')
14004 if (strcmp (reply
, "OK") != 0)
14005 error (_("Bogus reply from target: %s"), reply
);
14011 remote_target::use_agent (bool use
)
14013 if (packet_support (PACKET_QAgent
) != PACKET_DISABLE
)
14015 struct remote_state
*rs
= get_remote_state ();
14017 /* If the stub supports QAgent. */
14018 xsnprintf (rs
->buf
.data (), get_remote_packet_size (), "QAgent:%d", use
);
14020 getpkt (&rs
->buf
, 0);
14022 if (strcmp (rs
->buf
.data (), "OK") == 0)
14033 remote_target::can_use_agent ()
14035 return (packet_support (PACKET_QAgent
) != PACKET_DISABLE
);
14038 struct btrace_target_info
14040 /* The ptid of the traced thread. */
14043 /* The obtained branch trace configuration. */
14044 struct btrace_config conf
;
14047 /* Reset our idea of our target's btrace configuration. */
14050 remote_btrace_reset (remote_state
*rs
)
14052 memset (&rs
->btrace_config
, 0, sizeof (rs
->btrace_config
));
14055 /* Synchronize the configuration with the target. */
14058 remote_target::btrace_sync_conf (const btrace_config
*conf
)
14060 struct packet_config
*packet
;
14061 struct remote_state
*rs
;
14062 char *buf
, *pos
, *endbuf
;
14064 rs
= get_remote_state ();
14065 buf
= rs
->buf
.data ();
14066 endbuf
= buf
+ get_remote_packet_size ();
14068 packet
= &remote_protocol_packets
[PACKET_Qbtrace_conf_bts_size
];
14069 if (packet_config_support (packet
) == PACKET_ENABLE
14070 && conf
->bts
.size
!= rs
->btrace_config
.bts
.size
)
14073 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x", packet
->name
,
14077 getpkt (&rs
->buf
, 0);
14079 if (packet_ok (buf
, packet
) == PACKET_ERROR
)
14081 if (buf
[0] == 'E' && buf
[1] == '.')
14082 error (_("Failed to configure the BTS buffer size: %s"), buf
+ 2);
14084 error (_("Failed to configure the BTS buffer size."));
14087 rs
->btrace_config
.bts
.size
= conf
->bts
.size
;
14090 packet
= &remote_protocol_packets
[PACKET_Qbtrace_conf_pt_size
];
14091 if (packet_config_support (packet
) == PACKET_ENABLE
14092 && conf
->pt
.size
!= rs
->btrace_config
.pt
.size
)
14095 pos
+= xsnprintf (pos
, endbuf
- pos
, "%s=0x%x", packet
->name
,
14099 getpkt (&rs
->buf
, 0);
14101 if (packet_ok (buf
, packet
) == PACKET_ERROR
)
14103 if (buf
[0] == 'E' && buf
[1] == '.')
14104 error (_("Failed to configure the trace buffer size: %s"), buf
+ 2);
14106 error (_("Failed to configure the trace buffer size."));
14109 rs
->btrace_config
.pt
.size
= conf
->pt
.size
;
14113 /* Read the current thread's btrace configuration from the target and
14114 store it into CONF. */
14117 btrace_read_config (struct btrace_config
*conf
)
14119 gdb::optional
<gdb::char_vector
> xml
14120 = target_read_stralloc (current_inferior ()->top_target (),
14121 TARGET_OBJECT_BTRACE_CONF
, "");
14123 parse_xml_btrace_conf (conf
, xml
->data ());
14126 /* Maybe reopen target btrace. */
14129 remote_target::remote_btrace_maybe_reopen ()
14131 struct remote_state
*rs
= get_remote_state ();
14132 int btrace_target_pushed
= 0;
14133 #if !defined (HAVE_LIBIPT)
14137 /* Don't bother walking the entirety of the remote thread list when
14138 we know the feature isn't supported by the remote. */
14139 if (packet_support (PACKET_qXfer_btrace_conf
) != PACKET_ENABLE
)
14142 scoped_restore_current_thread restore_thread
;
14144 for (thread_info
*tp
: all_non_exited_threads (this))
14146 set_general_thread (tp
->ptid
);
14148 memset (&rs
->btrace_config
, 0x00, sizeof (struct btrace_config
));
14149 btrace_read_config (&rs
->btrace_config
);
14151 if (rs
->btrace_config
.format
== BTRACE_FORMAT_NONE
)
14154 #if !defined (HAVE_LIBIPT)
14155 if (rs
->btrace_config
.format
== BTRACE_FORMAT_PT
)
14160 warning (_("Target is recording using Intel Processor Trace "
14161 "but support was disabled at compile time."));
14166 #endif /* !defined (HAVE_LIBIPT) */
14168 /* Push target, once, but before anything else happens. This way our
14169 changes to the threads will be cleaned up by unpushing the target
14170 in case btrace_read_config () throws. */
14171 if (!btrace_target_pushed
)
14173 btrace_target_pushed
= 1;
14174 record_btrace_push_target ();
14175 printf_filtered (_("Target is recording using %s.\n"),
14176 btrace_format_string (rs
->btrace_config
.format
));
14179 tp
->btrace
.target
= XCNEW (struct btrace_target_info
);
14180 tp
->btrace
.target
->ptid
= tp
->ptid
;
14181 tp
->btrace
.target
->conf
= rs
->btrace_config
;
14185 /* Enable branch tracing. */
14187 struct btrace_target_info
*
14188 remote_target::enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
14190 struct btrace_target_info
*tinfo
= NULL
;
14191 struct packet_config
*packet
= NULL
;
14192 struct remote_state
*rs
= get_remote_state ();
14193 char *buf
= rs
->buf
.data ();
14194 char *endbuf
= buf
+ get_remote_packet_size ();
14196 switch (conf
->format
)
14198 case BTRACE_FORMAT_BTS
:
14199 packet
= &remote_protocol_packets
[PACKET_Qbtrace_bts
];
14202 case BTRACE_FORMAT_PT
:
14203 packet
= &remote_protocol_packets
[PACKET_Qbtrace_pt
];
14207 if (packet
== NULL
|| packet_config_support (packet
) != PACKET_ENABLE
)
14208 error (_("Target does not support branch tracing."));
14210 btrace_sync_conf (conf
);
14212 set_general_thread (ptid
);
14214 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
14216 getpkt (&rs
->buf
, 0);
14218 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
14220 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14221 error (_("Could not enable branch tracing for %s: %s"),
14222 target_pid_to_str (ptid
).c_str (), &rs
->buf
[2]);
14224 error (_("Could not enable branch tracing for %s."),
14225 target_pid_to_str (ptid
).c_str ());
14228 tinfo
= XCNEW (struct btrace_target_info
);
14229 tinfo
->ptid
= ptid
;
14231 /* If we fail to read the configuration, we lose some information, but the
14232 tracing itself is not impacted. */
14235 btrace_read_config (&tinfo
->conf
);
14237 catch (const gdb_exception_error
&err
)
14239 if (err
.message
!= NULL
)
14240 warning ("%s", err
.what ());
14246 /* Disable branch tracing. */
14249 remote_target::disable_btrace (struct btrace_target_info
*tinfo
)
14251 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_off
];
14252 struct remote_state
*rs
= get_remote_state ();
14253 char *buf
= rs
->buf
.data ();
14254 char *endbuf
= buf
+ get_remote_packet_size ();
14256 if (packet_config_support (packet
) != PACKET_ENABLE
)
14257 error (_("Target does not support branch tracing."));
14259 set_general_thread (tinfo
->ptid
);
14261 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
14263 getpkt (&rs
->buf
, 0);
14265 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
14267 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
14268 error (_("Could not disable branch tracing for %s: %s"),
14269 target_pid_to_str (tinfo
->ptid
).c_str (), &rs
->buf
[2]);
14271 error (_("Could not disable branch tracing for %s."),
14272 target_pid_to_str (tinfo
->ptid
).c_str ());
14278 /* Teardown branch tracing. */
14281 remote_target::teardown_btrace (struct btrace_target_info
*tinfo
)
14283 /* We must not talk to the target during teardown. */
14287 /* Read the branch trace. */
14290 remote_target::read_btrace (struct btrace_data
*btrace
,
14291 struct btrace_target_info
*tinfo
,
14292 enum btrace_read_type type
)
14294 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_qXfer_btrace
];
14297 if (packet_config_support (packet
) != PACKET_ENABLE
)
14298 error (_("Target does not support branch tracing."));
14300 #if !defined(HAVE_LIBEXPAT)
14301 error (_("Cannot process branch tracing result. XML parsing not supported."));
14306 case BTRACE_READ_ALL
:
14309 case BTRACE_READ_NEW
:
14312 case BTRACE_READ_DELTA
:
14316 internal_error (__FILE__
, __LINE__
,
14317 _("Bad branch tracing read type: %u."),
14318 (unsigned int) type
);
14321 gdb::optional
<gdb::char_vector
> xml
14322 = target_read_stralloc (current_inferior ()->top_target (),
14323 TARGET_OBJECT_BTRACE
, annex
);
14325 return BTRACE_ERR_UNKNOWN
;
14327 parse_xml_btrace (btrace
, xml
->data ());
14329 return BTRACE_ERR_NONE
;
14332 const struct btrace_config
*
14333 remote_target::btrace_conf (const struct btrace_target_info
*tinfo
)
14335 return &tinfo
->conf
;
14339 remote_target::augmented_libraries_svr4_read ()
14341 return (packet_support (PACKET_augmented_libraries_svr4_read_feature
)
14345 /* Implementation of to_load. */
14348 remote_target::load (const char *name
, int from_tty
)
14350 generic_load (name
, from_tty
);
14353 /* Accepts an integer PID; returns a string representing a file that
14354 can be opened on the remote side to get the symbols for the child
14355 process. Returns NULL if the operation is not supported. */
14358 remote_target::pid_to_exec_file (int pid
)
14360 static gdb::optional
<gdb::char_vector
> filename
;
14361 char *annex
= NULL
;
14363 if (packet_support (PACKET_qXfer_exec_file
) != PACKET_ENABLE
)
14366 inferior
*inf
= find_inferior_pid (this, pid
);
14368 internal_error (__FILE__
, __LINE__
,
14369 _("not currently attached to process %d"), pid
);
14371 if (!inf
->fake_pid_p
)
14373 const int annex_size
= 9;
14375 annex
= (char *) alloca (annex_size
);
14376 xsnprintf (annex
, annex_size
, "%x", pid
);
14379 filename
= target_read_stralloc (current_inferior ()->top_target (),
14380 TARGET_OBJECT_EXEC_FILE
, annex
);
14382 return filename
? filename
->data () : nullptr;
14385 /* Implement the to_can_do_single_step target_ops method. */
14388 remote_target::can_do_single_step ()
14390 /* We can only tell whether target supports single step or not by
14391 supported s and S vCont actions if the stub supports vContSupported
14392 feature. If the stub doesn't support vContSupported feature,
14393 we have conservatively to think target doesn't supports single
14395 if (packet_support (PACKET_vContSupported
) == PACKET_ENABLE
)
14397 struct remote_state
*rs
= get_remote_state ();
14399 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
14400 remote_vcont_probe ();
14402 return rs
->supports_vCont
.s
&& rs
->supports_vCont
.S
;
14408 /* Implementation of the to_execution_direction method for the remote
14411 enum exec_direction_kind
14412 remote_target::execution_direction ()
14414 struct remote_state
*rs
= get_remote_state ();
14416 return rs
->last_resume_exec_dir
;
14419 /* Return pointer to the thread_info struct which corresponds to
14420 THREAD_HANDLE (having length HANDLE_LEN). */
14423 remote_target::thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
14427 for (thread_info
*tp
: all_non_exited_threads (this))
14429 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14431 if (tp
->inf
== inf
&& priv
!= NULL
)
14433 if (handle_len
!= priv
->thread_handle
.size ())
14434 error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
14435 handle_len
, priv
->thread_handle
.size ());
14436 if (memcmp (thread_handle
, priv
->thread_handle
.data (),
14446 remote_target::thread_info_to_thread_handle (struct thread_info
*tp
)
14448 remote_thread_info
*priv
= get_remote_thread_info (tp
);
14449 return priv
->thread_handle
;
14453 remote_target::can_async_p ()
14455 /* This flag should be checked in the common target.c code. */
14456 gdb_assert (target_async_permitted
);
14458 /* We're async whenever the serial device can. */
14459 struct remote_state
*rs
= get_remote_state ();
14460 return serial_can_async_p (rs
->remote_desc
);
14464 remote_target::is_async_p ()
14466 /* We're async whenever the serial device is. */
14467 struct remote_state
*rs
= get_remote_state ();
14468 return serial_is_async_p (rs
->remote_desc
);
14471 /* Pass the SERIAL event on and up to the client. One day this code
14472 will be able to delay notifying the client of an event until the
14473 point where an entire packet has been received. */
14475 static serial_event_ftype remote_async_serial_handler
;
14478 remote_async_serial_handler (struct serial
*scb
, void *context
)
14480 /* Don't propogate error information up to the client. Instead let
14481 the client find out about the error by querying the target. */
14482 inferior_event_handler (INF_REG_EVENT
);
14486 remote_async_inferior_event_handler (gdb_client_data data
)
14488 inferior_event_handler (INF_REG_EVENT
);
14492 remote_target::async_wait_fd ()
14494 struct remote_state
*rs
= get_remote_state ();
14495 return rs
->remote_desc
->fd
;
14499 remote_target::async (int enable
)
14501 struct remote_state
*rs
= get_remote_state ();
14505 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
14507 /* If there are pending events in the stop reply queue tell the
14508 event loop to process them. */
14509 if (!rs
->stop_reply_queue
.empty ())
14510 mark_async_event_handler (rs
->remote_async_inferior_event_token
);
14511 /* For simplicity, below we clear the pending events token
14512 without remembering whether it is marked, so here we always
14513 mark it. If there's actually no pending notification to
14514 process, this ends up being a no-op (other than a spurious
14515 event-loop wakeup). */
14516 if (target_is_non_stop_p ())
14517 mark_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14521 serial_async (rs
->remote_desc
, NULL
, NULL
);
14522 /* If the core is disabling async, it doesn't want to be
14523 disturbed with target events. Clear all async event sources
14525 clear_async_event_handler (rs
->remote_async_inferior_event_token
);
14526 if (target_is_non_stop_p ())
14527 clear_async_event_handler (rs
->notif_state
->get_pending_events_token
);
14531 /* Implementation of the to_thread_events method. */
14534 remote_target::thread_events (int enable
)
14536 struct remote_state
*rs
= get_remote_state ();
14537 size_t size
= get_remote_packet_size ();
14539 if (packet_support (PACKET_QThreadEvents
) == PACKET_DISABLE
)
14542 xsnprintf (rs
->buf
.data (), size
, "QThreadEvents:%x", enable
? 1 : 0);
14544 getpkt (&rs
->buf
, 0);
14546 switch (packet_ok (rs
->buf
,
14547 &remote_protocol_packets
[PACKET_QThreadEvents
]))
14550 if (strcmp (rs
->buf
.data (), "OK") != 0)
14551 error (_("Remote refused setting thread events: %s"), rs
->buf
.data ());
14554 warning (_("Remote failure reply: %s"), rs
->buf
.data ());
14556 case PACKET_UNKNOWN
:
14562 show_remote_cmd (const char *args
, int from_tty
)
14564 /* We can't just use cmd_show_list here, because we want to skip
14565 the redundant "show remote Z-packet" and the legacy aliases. */
14566 struct cmd_list_element
*list
= remote_show_cmdlist
;
14567 struct ui_out
*uiout
= current_uiout
;
14569 ui_out_emit_tuple
tuple_emitter (uiout
, "showlist");
14570 for (; list
!= NULL
; list
= list
->next
)
14571 if (strcmp (list
->name
, "Z-packet") == 0)
14573 else if (list
->type
== not_set_cmd
)
14574 /* Alias commands are exactly like the original, except they
14575 don't have the normal type. */
14579 ui_out_emit_tuple
option_emitter (uiout
, "option");
14581 uiout
->field_string ("name", list
->name
);
14582 uiout
->text (": ");
14583 if (list
->type
== show_cmd
)
14584 do_show_command (NULL
, from_tty
, list
);
14586 cmd_func (list
, NULL
, from_tty
);
14591 /* Function to be called whenever a new objfile (shlib) is detected. */
14593 remote_new_objfile (struct objfile
*objfile
)
14595 remote_target
*remote
= get_current_remote_target ();
14597 /* First, check whether the current inferior's process target is a remote
14599 if (remote
== nullptr)
14602 /* When we are attaching or handling a fork child and the shared library
14603 subsystem reads the list of loaded libraries, we receive new objfile
14604 events in between each found library. The libraries are read in an
14605 undefined order, so if we gave the remote side a chance to look up
14606 symbols between each objfile, we might give it an inconsistent picture
14607 of the inferior. It could appear that a library A appears loaded but
14608 a library B does not, even though library A requires library B. That
14609 would present a state that couldn't normally exist in the inferior.
14611 So, skip these events, we'll give the remote a chance to look up symbols
14612 once all the loaded libraries and their symbols are known to GDB. */
14613 if (current_inferior ()->in_initial_library_scan
)
14616 remote
->remote_check_symbols ();
14619 /* Pull all the tracepoints defined on the target and create local
14620 data structures representing them. We don't want to create real
14621 tracepoints yet, we don't want to mess up the user's existing
14625 remote_target::upload_tracepoints (struct uploaded_tp
**utpp
)
14627 struct remote_state
*rs
= get_remote_state ();
14630 /* Ask for a first packet of tracepoint definition. */
14632 getpkt (&rs
->buf
, 0);
14633 p
= rs
->buf
.data ();
14634 while (*p
&& *p
!= 'l')
14636 parse_tracepoint_definition (p
, utpp
);
14637 /* Ask for another packet of tracepoint definition. */
14639 getpkt (&rs
->buf
, 0);
14640 p
= rs
->buf
.data ();
14646 remote_target::upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
14648 struct remote_state
*rs
= get_remote_state ();
14651 /* Ask for a first packet of variable definition. */
14653 getpkt (&rs
->buf
, 0);
14654 p
= rs
->buf
.data ();
14655 while (*p
&& *p
!= 'l')
14657 parse_tsv_definition (p
, utsvp
);
14658 /* Ask for another packet of variable definition. */
14660 getpkt (&rs
->buf
, 0);
14661 p
= rs
->buf
.data ();
14666 /* The "set/show range-stepping" show hook. */
14669 show_range_stepping (struct ui_file
*file
, int from_tty
,
14670 struct cmd_list_element
*c
,
14673 fprintf_filtered (file
,
14674 _("Debugger's willingness to use range stepping "
14675 "is %s.\n"), value
);
14678 /* Return true if the vCont;r action is supported by the remote
14682 remote_target::vcont_r_supported ()
14684 if (packet_support (PACKET_vCont
) == PACKET_SUPPORT_UNKNOWN
)
14685 remote_vcont_probe ();
14687 return (packet_support (PACKET_vCont
) == PACKET_ENABLE
14688 && get_remote_state ()->supports_vCont
.r
);
14691 /* The "set/show range-stepping" set hook. */
14694 set_range_stepping (const char *ignore_args
, int from_tty
,
14695 struct cmd_list_element
*c
)
14697 /* When enabling, check whether range stepping is actually supported
14698 by the target, and warn if not. */
14699 if (use_range_stepping
)
14701 remote_target
*remote
= get_current_remote_target ();
14703 || !remote
->vcont_r_supported ())
14704 warning (_("Range stepping is not supported by the current target"));
14709 show_remote_debug (struct ui_file
*file
, int from_tty
,
14710 struct cmd_list_element
*c
, const char *value
)
14712 fprintf_filtered (file
, _("Debugging of remote protocol is %s.\n"),
14717 show_remote_timeout (struct ui_file
*file
, int from_tty
,
14718 struct cmd_list_element
*c
, const char *value
)
14720 fprintf_filtered (file
,
14721 _("Timeout limit to wait for target to respond is %s.\n"),
14725 /* Implement the "supports_memory_tagging" target_ops method. */
14728 remote_target::supports_memory_tagging ()
14730 return remote_memory_tagging_p ();
14733 /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
14736 create_fetch_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14737 size_t len
, int type
)
14739 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14741 std::string request
= string_printf ("qMemTags:%s,%s:%s",
14742 phex_nz (address
, addr_size
),
14743 phex_nz (len
, sizeof (len
)),
14744 phex_nz (type
, sizeof (type
)));
14746 strcpy (packet
.data (), request
.c_str ());
14749 /* Parse the qMemTags packet reply into TAGS.
14751 Return true if successful, false otherwise. */
14754 parse_fetch_memtags_reply (const gdb::char_vector
&reply
,
14755 gdb::byte_vector
&tags
)
14757 if (reply
.empty () || reply
[0] == 'E' || reply
[0] != 'm')
14760 /* Copy the tag data. */
14761 tags
= hex2bin (reply
.data () + 1);
14766 /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
14769 create_store_memtags_request (gdb::char_vector
&packet
, CORE_ADDR address
,
14770 size_t len
, int type
,
14771 const gdb::byte_vector
&tags
)
14773 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
14775 /* Put together the main packet, address and length. */
14776 std::string request
= string_printf ("QMemTags:%s,%s:%s:",
14777 phex_nz (address
, addr_size
),
14778 phex_nz (len
, sizeof (len
)),
14779 phex_nz (type
, sizeof (type
)));
14780 request
+= bin2hex (tags
.data (), tags
.size ());
14782 /* Check if we have exceeded the maximum packet size. */
14783 if (packet
.size () < request
.length ())
14784 error (_("Contents too big for packet QMemTags."));
14786 strcpy (packet
.data (), request
.c_str ());
14789 /* Implement the "fetch_memtags" target_ops method. */
14792 remote_target::fetch_memtags (CORE_ADDR address
, size_t len
,
14793 gdb::byte_vector
&tags
, int type
)
14795 /* Make sure the qMemTags packet is supported. */
14796 if (!remote_memory_tagging_p ())
14797 gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
14799 struct remote_state
*rs
= get_remote_state ();
14801 create_fetch_memtags_request (rs
->buf
, address
, len
, type
);
14804 getpkt (&rs
->buf
, 0);
14806 return parse_fetch_memtags_reply (rs
->buf
, tags
);
14809 /* Implement the "store_memtags" target_ops method. */
14812 remote_target::store_memtags (CORE_ADDR address
, size_t len
,
14813 const gdb::byte_vector
&tags
, int type
)
14815 /* Make sure the QMemTags packet is supported. */
14816 if (!remote_memory_tagging_p ())
14817 gdb_assert_not_reached ("remote store_memtags called with packet disabled");
14819 struct remote_state
*rs
= get_remote_state ();
14821 create_store_memtags_request (rs
->buf
, address
, len
, type
, tags
);
14824 getpkt (&rs
->buf
, 0);
14826 /* Verify if the request was successful. */
14827 return packet_check_result (rs
->buf
.data ()) == PACKET_OK
;
14830 /* Return true if remote target T is non-stop. */
14833 remote_target_is_non_stop_p (remote_target
*t
)
14835 scoped_restore_current_thread restore_thread
;
14836 switch_to_target_no_thread (t
);
14838 return target_is_non_stop_p ();
14843 namespace selftests
{
14846 test_memory_tagging_functions ()
14848 remote_target remote
;
14850 struct packet_config
*config
14851 = &remote_protocol_packets
[PACKET_memory_tagging_feature
];
14853 scoped_restore restore_memtag_support_
14854 = make_scoped_restore (&config
->support
);
14856 /* Test memory tagging packet support. */
14857 config
->support
= PACKET_SUPPORT_UNKNOWN
;
14858 SELF_CHECK (remote
.supports_memory_tagging () == false);
14859 config
->support
= PACKET_DISABLE
;
14860 SELF_CHECK (remote
.supports_memory_tagging () == false);
14861 config
->support
= PACKET_ENABLE
;
14862 SELF_CHECK (remote
.supports_memory_tagging () == true);
14864 /* Setup testing. */
14865 gdb::char_vector packet
;
14866 gdb::byte_vector tags
, bv
;
14867 std::string expected
, reply
;
14868 packet
.resize (32000);
14870 /* Test creating a qMemTags request. */
14872 expected
= "qMemTags:0,0:0";
14873 create_fetch_memtags_request (packet
, 0x0, 0x0, 0);
14874 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14876 expected
= "qMemTags:deadbeef,10:1";
14877 create_fetch_memtags_request (packet
, 0xdeadbeef, 16, 1);
14878 SELF_CHECK (strcmp (packet
.data (), expected
.c_str ()) == 0);
14880 /* Test parsing a qMemTags reply. */
14882 /* Error reply, tags vector unmodified. */
14884 strcpy (packet
.data (), reply
.c_str ());
14886 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == false);
14887 SELF_CHECK (tags
.size () == 0);
14889 /* Valid reply, tags vector updated. */
14893 for (int i
= 0; i
< 5; i
++)
14896 reply
= "m" + bin2hex (bv
.data (), bv
.size ());
14897 strcpy (packet
.data (), reply
.c_str ());
14899 SELF_CHECK (parse_fetch_memtags_reply (packet
, tags
) == true);
14900 SELF_CHECK (tags
.size () == 5);
14902 for (int i
= 0; i
< 5; i
++)
14903 SELF_CHECK (tags
[i
] == i
);
14905 /* Test creating a QMemTags request. */
14907 /* Empty tag data. */
14909 expected
= "QMemTags:0,0:0:";
14910 create_store_memtags_request (packet
, 0x0, 0x0, 0, tags
);
14911 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
14912 expected
.length ()) == 0);
14914 /* Non-empty tag data. */
14916 for (int i
= 0; i
< 5; i
++)
14917 tags
.push_back (i
);
14918 expected
= "QMemTags:deadbeef,ff:1:0001020304";
14919 create_store_memtags_request (packet
, 0xdeadbeef, 255, 1, tags
);
14920 SELF_CHECK (memcmp (packet
.data (), expected
.c_str (),
14921 expected
.length ()) == 0);
14924 } // namespace selftests
14925 #endif /* GDB_SELF_TEST */
14927 void _initialize_remote ();
14929 _initialize_remote ()
14931 /* architecture specific data */
14932 remote_g_packet_data_handle
=
14933 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
14935 add_target (remote_target_info
, remote_target::open
);
14936 add_target (extended_remote_target_info
, extended_remote_target::open
);
14938 /* Hook into new objfile notification. */
14939 gdb::observers::new_objfile
.attach (remote_new_objfile
, "remote");
14942 init_remote_threadtests ();
14945 /* set/show remote ... */
14947 add_basic_prefix_cmd ("remote", class_maintenance
, _("\
14948 Remote protocol specific variables.\n\
14949 Configure various remote-protocol specific variables such as\n\
14950 the packets being used."),
14951 &remote_set_cmdlist
,
14952 0 /* allow-unknown */, &setlist
);
14953 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
14954 Remote protocol specific variables.\n\
14955 Configure various remote-protocol specific variables such as\n\
14956 the packets being used."),
14957 &remote_show_cmdlist
,
14958 0 /* allow-unknown */, &showlist
);
14960 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
14961 Compare section data on target to the exec file.\n\
14962 Argument is a single section name (default: all loaded sections).\n\
14963 To compare only read-only loaded sections, specify the -r option."),
14966 add_cmd ("packet", class_maintenance
, cli_packet_command
, _("\
14967 Send an arbitrary packet to a remote target.\n\
14968 maintenance packet TEXT\n\
14969 If GDB is talking to an inferior via the GDB serial protocol, then\n\
14970 this command sends the string TEXT to the inferior, and displays the\n\
14971 response packet. GDB supplies the initial `$' character, and the\n\
14972 terminating `#' character and checksum."),
14975 set_show_commands remotebreak_cmds
14976 = add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
14977 Set whether to send break if interrupted."), _("\
14978 Show whether to send break if interrupted."), _("\
14979 If set, a break, instead of a cntrl-c, is sent to the remote target."),
14980 set_remotebreak
, show_remotebreak
,
14981 &setlist
, &showlist
);
14982 deprecate_cmd (remotebreak_cmds
.set
, "set remote interrupt-sequence");
14983 deprecate_cmd (remotebreak_cmds
.show
, "show remote interrupt-sequence");
14985 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
14986 interrupt_sequence_modes
, &interrupt_sequence_mode
,
14988 Set interrupt sequence to remote target."), _("\
14989 Show interrupt sequence to remote target."), _("\
14990 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
14991 NULL
, show_interrupt_sequence
,
14992 &remote_set_cmdlist
,
14993 &remote_show_cmdlist
);
14995 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
14996 &interrupt_on_connect
, _("\
14997 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
14998 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
14999 If set, interrupt sequence is sent to remote target."),
15001 &remote_set_cmdlist
, &remote_show_cmdlist
);
15003 /* Install commands for configuring memory read/write packets. */
15005 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
15006 Set the maximum number of bytes per memory write packet (deprecated)."),
15008 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
15009 Show the maximum number of bytes per memory write packet (deprecated)."),
15011 add_cmd ("memory-write-packet-size", no_class
,
15012 set_memory_write_packet_size
, _("\
15013 Set the maximum number of bytes per memory-write packet.\n\
15014 Specify the number of bytes in a packet or 0 (zero) for the\n\
15015 default packet size. The actual limit is further reduced\n\
15016 dependent on the target. Specify ``fixed'' to disable the\n\
15017 further restriction and ``limit'' to enable that restriction."),
15018 &remote_set_cmdlist
);
15019 add_cmd ("memory-read-packet-size", no_class
,
15020 set_memory_read_packet_size
, _("\
15021 Set the maximum number of bytes per memory-read packet.\n\
15022 Specify the number of bytes in a packet or 0 (zero) for the\n\
15023 default packet size. The actual limit is further reduced\n\
15024 dependent on the target. Specify ``fixed'' to disable the\n\
15025 further restriction and ``limit'' to enable that restriction."),
15026 &remote_set_cmdlist
);
15027 add_cmd ("memory-write-packet-size", no_class
,
15028 show_memory_write_packet_size
,
15029 _("Show the maximum number of bytes per memory-write packet."),
15030 &remote_show_cmdlist
);
15031 add_cmd ("memory-read-packet-size", no_class
,
15032 show_memory_read_packet_size
,
15033 _("Show the maximum number of bytes per memory-read packet."),
15034 &remote_show_cmdlist
);
15036 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class
,
15037 &remote_hw_watchpoint_limit
, _("\
15038 Set the maximum number of target hardware watchpoints."), _("\
15039 Show the maximum number of target hardware watchpoints."), _("\
15040 Specify \"unlimited\" for unlimited hardware watchpoints."),
15041 NULL
, show_hardware_watchpoint_limit
,
15042 &remote_set_cmdlist
,
15043 &remote_show_cmdlist
);
15044 add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
15046 &remote_hw_watchpoint_length_limit
, _("\
15047 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
15048 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
15049 Specify \"unlimited\" to allow watchpoints of unlimited size."),
15050 NULL
, show_hardware_watchpoint_length_limit
,
15051 &remote_set_cmdlist
, &remote_show_cmdlist
);
15052 add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class
,
15053 &remote_hw_breakpoint_limit
, _("\
15054 Set the maximum number of target hardware breakpoints."), _("\
15055 Show the maximum number of target hardware breakpoints."), _("\
15056 Specify \"unlimited\" for unlimited hardware breakpoints."),
15057 NULL
, show_hardware_breakpoint_limit
,
15058 &remote_set_cmdlist
, &remote_show_cmdlist
);
15060 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
15061 &remote_address_size
, _("\
15062 Set the maximum size of the address (in bits) in a memory packet."), _("\
15063 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
15065 NULL
, /* FIXME: i18n: */
15066 &setlist
, &showlist
);
15068 init_all_packet_configs ();
15070 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
15071 "X", "binary-download", 1);
15073 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
15074 "vCont", "verbose-resume", 0);
15076 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
15077 "QPassSignals", "pass-signals", 0);
15079 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QCatchSyscalls
],
15080 "QCatchSyscalls", "catch-syscalls", 0);
15082 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QProgramSignals
],
15083 "QProgramSignals", "program-signals", 0);
15085 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QSetWorkingDir
],
15086 "QSetWorkingDir", "set-working-dir", 0);
15088 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartupWithShell
],
15089 "QStartupWithShell", "startup-with-shell", 0);
15091 add_packet_config_cmd (&remote_protocol_packets
15092 [PACKET_QEnvironmentHexEncoded
],
15093 "QEnvironmentHexEncoded", "environment-hex-encoded",
15096 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QEnvironmentReset
],
15097 "QEnvironmentReset", "environment-reset",
15100 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QEnvironmentUnset
],
15101 "QEnvironmentUnset", "environment-unset",
15104 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
15105 "qSymbol", "symbol-lookup", 0);
15107 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
15108 "P", "set-register", 1);
15110 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
15111 "p", "fetch-register", 1);
15113 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
15114 "Z0", "software-breakpoint", 0);
15116 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
15117 "Z1", "hardware-breakpoint", 0);
15119 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
15120 "Z2", "write-watchpoint", 0);
15122 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
15123 "Z3", "read-watchpoint", 0);
15125 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
15126 "Z4", "access-watchpoint", 0);
15128 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
15129 "qXfer:auxv:read", "read-aux-vector", 0);
15131 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_exec_file
],
15132 "qXfer:exec-file:read", "pid-to-exec-file", 0);
15134 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
15135 "qXfer:features:read", "target-features", 0);
15137 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
15138 "qXfer:libraries:read", "library-info", 0);
15140 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries_svr4
],
15141 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
15143 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
15144 "qXfer:memory-map:read", "memory-map", 0);
15146 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
15147 "qXfer:osdata:read", "osdata", 0);
15149 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
15150 "qXfer:threads:read", "threads", 0);
15152 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
15153 "qXfer:siginfo:read", "read-siginfo-object", 0);
15155 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
15156 "qXfer:siginfo:write", "write-siginfo-object", 0);
15158 add_packet_config_cmd
15159 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
15160 "qXfer:traceframe-info:read", "traceframe-info", 0);
15162 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_uib
],
15163 "qXfer:uib:read", "unwind-info-block", 0);
15165 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
15166 "qGetTLSAddr", "get-thread-local-storage-address",
15169 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
15170 "qGetTIBAddr", "get-thread-information-block-address",
15173 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
15174 "bc", "reverse-continue", 0);
15176 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
15177 "bs", "reverse-step", 0);
15179 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
15180 "qSupported", "supported-packets", 0);
15182 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
15183 "qSearch:memory", "search-memory", 0);
15185 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qTStatus
],
15186 "qTStatus", "trace-status", 0);
15188 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_setfs
],
15189 "vFile:setfs", "hostio-setfs", 0);
15191 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
15192 "vFile:open", "hostio-open", 0);
15194 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
15195 "vFile:pread", "hostio-pread", 0);
15197 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
15198 "vFile:pwrite", "hostio-pwrite", 0);
15200 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
15201 "vFile:close", "hostio-close", 0);
15203 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
15204 "vFile:unlink", "hostio-unlink", 0);
15206 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_readlink
],
15207 "vFile:readlink", "hostio-readlink", 0);
15209 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_fstat
],
15210 "vFile:fstat", "hostio-fstat", 0);
15212 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
15213 "vAttach", "attach", 0);
15215 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
15218 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
15219 "QStartNoAckMode", "noack", 0);
15221 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
15222 "vKill", "kill", 0);
15224 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
15225 "qAttached", "query-attached", 0);
15227 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
15228 "ConditionalTracepoints",
15229 "conditional-tracepoints", 0);
15231 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalBreakpoints
],
15232 "ConditionalBreakpoints",
15233 "conditional-breakpoints", 0);
15235 add_packet_config_cmd (&remote_protocol_packets
[PACKET_BreakpointCommands
],
15236 "BreakpointCommands",
15237 "breakpoint-commands", 0);
15239 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
15240 "FastTracepoints", "fast-tracepoints", 0);
15242 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
15243 "TracepointSource", "TracepointSource", 0);
15245 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
15246 "QAllow", "allow", 0);
15248 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
15249 "StaticTracepoints", "static-tracepoints", 0);
15251 add_packet_config_cmd (&remote_protocol_packets
[PACKET_InstallInTrace
],
15252 "InstallInTrace", "install-in-trace", 0);
15254 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
15255 "qXfer:statictrace:read", "read-sdata-object", 0);
15257 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_fdpic
],
15258 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
15260 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QDisableRandomization
],
15261 "QDisableRandomization", "disable-randomization", 0);
15263 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAgent
],
15264 "QAgent", "agent", 0);
15266 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QTBuffer_size
],
15267 "QTBuffer:size", "trace-buffer-size", 0);
15269 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_off
],
15270 "Qbtrace:off", "disable-btrace", 0);
15272 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_bts
],
15273 "Qbtrace:bts", "enable-btrace-bts", 0);
15275 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_pt
],
15276 "Qbtrace:pt", "enable-btrace-pt", 0);
15278 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace
],
15279 "qXfer:btrace", "read-btrace", 0);
15281 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace_conf
],
15282 "qXfer:btrace-conf", "read-btrace-conf", 0);
15284 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_conf_bts_size
],
15285 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
15287 add_packet_config_cmd (&remote_protocol_packets
[PACKET_multiprocess_feature
],
15288 "multiprocess-feature", "multiprocess-feature", 0);
15290 add_packet_config_cmd (&remote_protocol_packets
[PACKET_swbreak_feature
],
15291 "swbreak-feature", "swbreak-feature", 0);
15293 add_packet_config_cmd (&remote_protocol_packets
[PACKET_hwbreak_feature
],
15294 "hwbreak-feature", "hwbreak-feature", 0);
15296 add_packet_config_cmd (&remote_protocol_packets
[PACKET_fork_event_feature
],
15297 "fork-event-feature", "fork-event-feature", 0);
15299 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vfork_event_feature
],
15300 "vfork-event-feature", "vfork-event-feature", 0);
15302 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_conf_pt_size
],
15303 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
15305 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vContSupported
],
15306 "vContSupported", "verbose-resume-supported", 0);
15308 add_packet_config_cmd (&remote_protocol_packets
[PACKET_exec_event_feature
],
15309 "exec-event-feature", "exec-event-feature", 0);
15311 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCtrlC
],
15312 "vCtrlC", "ctrl-c", 0);
15314 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QThreadEvents
],
15315 "QThreadEvents", "thread-events", 0);
15317 add_packet_config_cmd (&remote_protocol_packets
[PACKET_no_resumed
],
15318 "N stop reply", "no-resumed-stop-reply", 0);
15320 add_packet_config_cmd (&remote_protocol_packets
[PACKET_memory_tagging_feature
],
15321 "memory-tagging-feature", "memory-tagging-feature", 0);
15323 /* Assert that we've registered "set remote foo-packet" commands
15324 for all packet configs. */
15328 for (i
= 0; i
< PACKET_MAX
; i
++)
15330 /* Ideally all configs would have a command associated. Some
15331 still don't though. */
15336 case PACKET_QNonStop
:
15337 case PACKET_EnableDisableTracepoints_feature
:
15338 case PACKET_tracenz_feature
:
15339 case PACKET_DisconnectedTracing_feature
:
15340 case PACKET_augmented_libraries_svr4_read_feature
:
15342 /* Additions to this list need to be well justified:
15343 pre-existing packets are OK; new packets are not. */
15351 /* This catches both forgetting to add a config command, and
15352 forgetting to remove a packet from the exception list. */
15353 gdb_assert (excepted
== (remote_protocol_packets
[i
].name
== NULL
));
15357 /* Keep the old ``set remote Z-packet ...'' working. Each individual
15358 Z sub-packet has its own set and show commands, but users may
15359 have sets to this variable in their .gdbinit files (or in their
15361 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
15362 &remote_Z_packet_detect
, _("\
15363 Set use of remote protocol `Z' packets."), _("\
15364 Show use of remote protocol `Z' packets."), _("\
15365 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
15367 set_remote_protocol_Z_packet_cmd
,
15368 show_remote_protocol_Z_packet_cmd
,
15369 /* FIXME: i18n: Use of remote protocol
15370 `Z' packets is %s. */
15371 &remote_set_cmdlist
, &remote_show_cmdlist
);
15373 add_basic_prefix_cmd ("remote", class_files
, _("\
15374 Manipulate files on the remote system.\n\
15375 Transfer files to and from the remote target system."),
15377 0 /* allow-unknown */, &cmdlist
);
15379 add_cmd ("put", class_files
, remote_put_command
,
15380 _("Copy a local file to the remote system."),
15383 add_cmd ("get", class_files
, remote_get_command
,
15384 _("Copy a remote file to the local system."),
15387 add_cmd ("delete", class_files
, remote_delete_command
,
15388 _("Delete a remote file."),
15391 add_setshow_string_noescape_cmd ("exec-file", class_files
,
15392 &remote_exec_file_var
, _("\
15393 Set the remote pathname for \"run\"."), _("\
15394 Show the remote pathname for \"run\"."), NULL
,
15395 set_remote_exec_file
,
15396 show_remote_exec_file
,
15397 &remote_set_cmdlist
,
15398 &remote_show_cmdlist
);
15400 add_setshow_boolean_cmd ("range-stepping", class_run
,
15401 &use_range_stepping
, _("\
15402 Enable or disable range stepping."), _("\
15403 Show whether target-assisted range stepping is enabled."), _("\
15404 If on, and the target supports it, when stepping a source line, GDB\n\
15405 tells the target to step the corresponding range of addresses itself instead\n\
15406 of issuing multiple single-steps. This speeds up source level\n\
15407 stepping. If off, GDB always issues single-steps, even if range\n\
15408 stepping is supported by the target. The default is on."),
15409 set_range_stepping
,
15410 show_range_stepping
,
15414 add_setshow_zinteger_cmd ("watchdog", class_maintenance
, &watchdog
, _("\
15415 Set watchdog timer."), _("\
15416 Show watchdog timer."), _("\
15417 When non-zero, this timeout is used instead of waiting forever for a target\n\
15418 to finish a low-level step or continue operation. If the specified amount\n\
15419 of time passes without a response from the target, an error occurs."),
15422 &setlist
, &showlist
);
15424 add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class
,
15425 &remote_packet_max_chars
, _("\
15426 Set the maximum number of characters to display for each remote packet."), _("\
15427 Show the maximum number of characters to display for each remote packet."), _("\
15428 Specify \"unlimited\" to display all the characters."),
15429 NULL
, show_remote_packet_max_chars
,
15430 &setdebuglist
, &showdebuglist
);
15432 add_setshow_boolean_cmd ("remote", no_class
, &remote_debug
,
15433 _("Set debugging of remote protocol."),
15434 _("Show debugging of remote protocol."),
15436 When enabled, each packet sent or received with the remote target\n\
15440 &setdebuglist
, &showdebuglist
);
15442 add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class
,
15443 &remote_timeout
, _("\
15444 Set timeout limit to wait for target to respond."), _("\
15445 Show timeout limit to wait for target to respond."), _("\
15446 This value is used to set the time limit for gdb to wait for a response\n\
15447 from the target."),
15449 show_remote_timeout
,
15450 &setlist
, &showlist
);
15452 /* Eventually initialize fileio. See fileio.c */
15453 initialize_remote_fileio (&remote_set_cmdlist
, &remote_show_cmdlist
);
15456 selftests::register_test ("remote_memory_tagging",
15457 selftests::test_memory_tagging_functions
);