1 /* Select target systems and architectures at runtime for GDB.
3 Copyright (C) 1990-2024 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "target-dcache.h"
29 #include "observable.h"
37 #include "target-descriptions.h"
38 #include "gdbthread.h"
41 #include "inline-frame.h"
42 #include "tracepoint.h"
43 #include "gdbsupport/fileio.h"
44 #include "gdbsupport/agent.h"
46 #include "target-debug.h"
48 #include "event-top.h"
50 #include "gdbsupport/byte-vector.h"
51 #include "gdbsupport/search.h"
53 #include <unordered_map>
54 #include "target-connection.h"
56 #include "cli/cli-decode.h"
58 static void generic_tls_error (void) ATTRIBUTE_NORETURN
;
60 static void default_rcmd (struct target_ops
*, const char *, struct ui_file
*);
62 static int default_verify_memory (struct target_ops
*self
,
64 CORE_ADDR memaddr
, ULONGEST size
);
66 static void tcomplain (void) ATTRIBUTE_NORETURN
;
68 /* Mapping between target_info objects (which have address identity)
69 and corresponding open/factory function/callback. Each add_target
70 call adds one entry to this map, and registers a "target
71 TARGET_NAME" command that when invoked calls the factory registered
72 here. The target_info object is associated with the command via
73 the command's context. */
74 static std::unordered_map
<const target_info
*, target_open_ftype
*>
77 /* The singleton debug target. */
79 static struct target_ops
*the_debug_target
;
81 /* Command list for target. */
83 static struct cmd_list_element
*targetlist
= NULL
;
87 bool trust_readonly
= false;
89 /* Nonzero if we should show true memory content including
90 memory breakpoint inserted by gdb. */
92 static int show_memory_breakpoints
= 0;
94 /* These globals control whether GDB attempts to perform these
95 operations; they are useful for targets that need to prevent
96 inadvertent disruption, such as in non-stop mode. */
98 bool may_write_registers
= true;
100 bool may_write_memory
= true;
102 bool may_insert_breakpoints
= true;
104 bool may_insert_tracepoints
= true;
106 bool may_insert_fast_tracepoints
= true;
108 bool may_stop
= true;
110 /* Non-zero if we want to see trace of target level stuff. */
112 static unsigned int targetdebug
= 0;
115 set_targetdebug (const char *args
, int from_tty
, struct cmd_list_element
*c
)
118 current_inferior ()->push_target (the_debug_target
);
120 current_inferior ()->unpush_target (the_debug_target
);
124 show_targetdebug (struct ui_file
*file
, int from_tty
,
125 struct cmd_list_element
*c
, const char *value
)
127 gdb_printf (file
, _("Target debugging is %s.\n"), value
);
133 for (target_ops
*t
= current_inferior ()->top_target ();
136 if (t
->has_memory ())
145 for (target_ops
*t
= current_inferior ()->top_target ();
155 target_has_registers ()
157 for (target_ops
*t
= current_inferior ()->top_target ();
160 if (t
->has_registers ())
167 target_has_execution (inferior
*inf
)
170 inf
= current_inferior ();
172 for (target_ops
*t
= inf
->top_target ();
174 t
= inf
->find_target_beneath (t
))
175 if (t
->has_execution (inf
))
184 return current_inferior ()->top_target ()->shortname ();
190 target_attach_no_wait ()
192 return current_inferior ()->top_target ()->attach_no_wait ();
198 target_post_attach (int pid
)
200 return current_inferior ()->top_target ()->post_attach (pid
);
206 target_prepare_to_store (regcache
*regcache
)
208 return current_inferior ()->top_target ()->prepare_to_store (regcache
);
214 target_supports_enable_disable_tracepoint ()
216 target_ops
*target
= current_inferior ()->top_target ();
218 return target
->supports_enable_disable_tracepoint ();
222 target_supports_string_tracing ()
224 return current_inferior ()->top_target ()->supports_string_tracing ();
230 target_supports_evaluation_of_breakpoint_conditions ()
232 target_ops
*target
= current_inferior ()->top_target ();
234 return target
->supports_evaluation_of_breakpoint_conditions ();
240 target_supports_dumpcore ()
242 return current_inferior ()->top_target ()->supports_dumpcore ();
248 target_dumpcore (const char *filename
)
250 return current_inferior ()->top_target ()->dumpcore (filename
);
256 target_can_run_breakpoint_commands ()
258 return current_inferior ()->top_target ()->can_run_breakpoint_commands ();
266 return current_inferior ()->top_target ()->files_info ();
272 target_insert_fork_catchpoint (int pid
)
274 return current_inferior ()->top_target ()->insert_fork_catchpoint (pid
);
280 target_remove_fork_catchpoint (int pid
)
282 return current_inferior ()->top_target ()->remove_fork_catchpoint (pid
);
288 target_insert_vfork_catchpoint (int pid
)
290 return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid
);
296 target_remove_vfork_catchpoint (int pid
)
298 return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid
);
304 target_insert_exec_catchpoint (int pid
)
306 return current_inferior ()->top_target ()->insert_exec_catchpoint (pid
);
312 target_remove_exec_catchpoint (int pid
)
314 return current_inferior ()->top_target ()->remove_exec_catchpoint (pid
);
320 target_set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
321 gdb::array_view
<const int> syscall_counts
)
323 target_ops
*target
= current_inferior ()->top_target ();
325 return target
->set_syscall_catchpoint (pid
, needed
, any_count
,
332 target_rcmd (const char *command
, struct ui_file
*outbuf
)
334 return current_inferior ()->top_target ()->rcmd (command
, outbuf
);
340 target_can_lock_scheduler ()
342 target_ops
*target
= current_inferior ()->top_target ();
344 return (target
->get_thread_control_capabilities ()& tc_schedlock
) != 0;
350 target_can_async_p ()
352 return target_can_async_p (current_inferior ()->top_target ());
358 target_can_async_p (struct target_ops
*target
)
360 if (!target_async_permitted
)
362 return target
->can_async_p ();
370 bool result
= current_inferior ()->top_target ()->is_async_p ();
371 gdb_assert (target_async_permitted
|| !result
);
376 target_execution_direction ()
378 return current_inferior ()->top_target ()->execution_direction ();
384 target_extra_thread_info (thread_info
*tp
)
386 return current_inferior ()->top_target ()->extra_thread_info (tp
);
392 target_pid_to_exec_file (int pid
)
394 return current_inferior ()->top_target ()->pid_to_exec_file (pid
);
400 target_thread_architecture (ptid_t ptid
)
402 return current_inferior ()->top_target ()->thread_architecture (ptid
);
408 target_find_memory_regions (find_memory_region_ftype func
, void *data
)
410 return current_inferior ()->top_target ()->find_memory_regions (func
, data
);
415 gdb::unique_xmalloc_ptr
<char>
416 target_make_corefile_notes (bfd
*bfd
, int *size_p
)
418 return current_inferior ()->top_target ()->make_corefile_notes (bfd
, size_p
);
422 target_get_bookmark (const char *args
, int from_tty
)
424 return current_inferior ()->top_target ()->get_bookmark (args
, from_tty
);
428 target_goto_bookmark (const gdb_byte
*arg
, int from_tty
)
430 return current_inferior ()->top_target ()->goto_bookmark (arg
, from_tty
);
436 target_stopped_by_watchpoint ()
438 return current_inferior ()->top_target ()->stopped_by_watchpoint ();
444 target_stopped_by_sw_breakpoint ()
446 return current_inferior ()->top_target ()->stopped_by_sw_breakpoint ();
450 target_supports_stopped_by_sw_breakpoint ()
452 target_ops
*target
= current_inferior ()->top_target ();
454 return target
->supports_stopped_by_sw_breakpoint ();
458 target_stopped_by_hw_breakpoint ()
460 return current_inferior ()->top_target ()->stopped_by_hw_breakpoint ();
464 target_supports_stopped_by_hw_breakpoint ()
466 target_ops
*target
= current_inferior ()->top_target ();
468 return target
->supports_stopped_by_hw_breakpoint ();
474 target_have_steppable_watchpoint ()
476 return current_inferior ()->top_target ()->have_steppable_watchpoint ();
482 target_can_use_hardware_watchpoint (bptype type
, int cnt
, int othertype
)
484 target_ops
*target
= current_inferior ()->top_target ();
486 return target
->can_use_hw_breakpoint (type
, cnt
, othertype
);
492 target_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
494 target_ops
*target
= current_inferior ()->top_target ();
496 return target
->region_ok_for_hw_watchpoint (addr
, len
);
501 target_can_do_single_step ()
503 return current_inferior ()->top_target ()->can_do_single_step ();
509 target_insert_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
512 target_ops
*target
= current_inferior ()->top_target ();
514 return target
->insert_watchpoint (addr
, len
, type
, cond
);
520 target_remove_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
523 target_ops
*target
= current_inferior ()->top_target ();
525 return target
->remove_watchpoint (addr
, len
, type
, cond
);
531 target_insert_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
533 target_ops
*target
= current_inferior ()->top_target ();
535 return target
->insert_hw_breakpoint (gdbarch
, bp_tgt
);
541 target_remove_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
543 target_ops
*target
= current_inferior ()->top_target ();
545 return target
->remove_hw_breakpoint (gdbarch
, bp_tgt
);
551 target_can_accel_watchpoint_condition (CORE_ADDR addr
, int len
, int type
,
554 target_ops
*target
= current_inferior ()->top_target ();
556 return target
->can_accel_watchpoint_condition (addr
, len
, type
, cond
);
562 target_can_execute_reverse ()
564 return current_inferior ()->top_target ()->can_execute_reverse ();
568 target_get_ada_task_ptid (long lwp
, ULONGEST tid
)
570 return current_inferior ()->top_target ()->get_ada_task_ptid (lwp
, tid
);
574 target_filesystem_is_local ()
576 return current_inferior ()->top_target ()->filesystem_is_local ();
582 return current_inferior ()->top_target ()->trace_init ();
586 target_download_tracepoint (bp_location
*location
)
588 return current_inferior ()->top_target ()->download_tracepoint (location
);
592 target_can_download_tracepoint ()
594 return current_inferior ()->top_target ()->can_download_tracepoint ();
598 target_download_trace_state_variable (const trace_state_variable
&tsv
)
600 target_ops
*target
= current_inferior ()->top_target ();
602 return target
->download_trace_state_variable (tsv
);
606 target_enable_tracepoint (bp_location
*loc
)
608 return current_inferior ()->top_target ()->enable_tracepoint (loc
);
612 target_disable_tracepoint (bp_location
*loc
)
614 return current_inferior ()->top_target ()->disable_tracepoint (loc
);
618 target_trace_start ()
620 return current_inferior ()->top_target ()->trace_start ();
624 target_trace_set_readonly_regions ()
626 return current_inferior ()->top_target ()->trace_set_readonly_regions ();
630 target_get_trace_status (trace_status
*ts
)
632 return current_inferior ()->top_target ()->get_trace_status (ts
);
636 target_get_tracepoint_status (tracepoint
*tp
, uploaded_tp
*utp
)
638 return current_inferior ()->top_target ()->get_tracepoint_status (tp
, utp
);
644 return current_inferior ()->top_target ()->trace_stop ();
648 target_trace_find (trace_find_type type
, int num
,
649 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
651 target_ops
*target
= current_inferior ()->top_target ();
653 return target
->trace_find (type
, num
, addr1
, addr2
, tpp
);
657 target_get_trace_state_variable_value (int tsv
, LONGEST
*val
)
659 target_ops
*target
= current_inferior ()->top_target ();
661 return target
->get_trace_state_variable_value (tsv
, val
);
665 target_save_trace_data (const char *filename
)
667 return current_inferior ()->top_target ()->save_trace_data (filename
);
671 target_upload_tracepoints (uploaded_tp
**utpp
)
673 return current_inferior ()->top_target ()->upload_tracepoints (utpp
);
677 target_upload_trace_state_variables (uploaded_tsv
**utsvp
)
679 target_ops
*target
= current_inferior ()->top_target ();
681 return target
->upload_trace_state_variables (utsvp
);
685 target_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
687 target_ops
*target
= current_inferior ()->top_target ();
689 return target
->get_raw_trace_data (buf
, offset
, len
);
693 target_get_min_fast_tracepoint_insn_len ()
695 target_ops
*target
= current_inferior ()->top_target ();
697 return target
->get_min_fast_tracepoint_insn_len ();
701 target_set_disconnected_tracing (int val
)
703 return current_inferior ()->top_target ()->set_disconnected_tracing (val
);
707 target_set_circular_trace_buffer (int val
)
709 return current_inferior ()->top_target ()->set_circular_trace_buffer (val
);
713 target_set_trace_buffer_size (LONGEST val
)
715 return current_inferior ()->top_target ()->set_trace_buffer_size (val
);
719 target_set_trace_notes (const char *user
, const char *notes
,
720 const char *stopnotes
)
722 target_ops
*target
= current_inferior ()->top_target ();
724 return target
->set_trace_notes (user
, notes
, stopnotes
);
728 target_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
730 return current_inferior ()->top_target ()->get_tib_address (ptid
, addr
);
734 target_set_permissions ()
736 return current_inferior ()->top_target ()->set_permissions ();
740 target_static_tracepoint_marker_at (CORE_ADDR addr
,
741 static_tracepoint_marker
*marker
)
743 target_ops
*target
= current_inferior ()->top_target ();
745 return target
->static_tracepoint_marker_at (addr
, marker
);
748 std::vector
<static_tracepoint_marker
>
749 target_static_tracepoint_markers_by_strid (const char *marker_id
)
751 target_ops
*target
= current_inferior ()->top_target ();
753 return target
->static_tracepoint_markers_by_strid (marker_id
);
757 target_traceframe_info ()
759 return current_inferior ()->top_target ()->traceframe_info ();
763 target_use_agent (bool use
)
765 return current_inferior ()->top_target ()->use_agent (use
);
769 target_can_use_agent ()
771 return current_inferior ()->top_target ()->can_use_agent ();
775 target_augmented_libraries_svr4_read ()
777 return current_inferior ()->top_target ()->augmented_libraries_svr4_read ();
781 target_supports_memory_tagging ()
783 return current_inferior ()->top_target ()->supports_memory_tagging ();
787 target_fetch_memtags (CORE_ADDR address
, size_t len
, gdb::byte_vector
&tags
,
790 return current_inferior ()->top_target ()->fetch_memtags (address
, len
, tags
, type
);
794 target_store_memtags (CORE_ADDR address
, size_t len
,
795 const gdb::byte_vector
&tags
, int type
)
797 return current_inferior ()->top_target ()->store_memtags (address
, len
, tags
, type
);
801 target_fetch_x86_xsave_layout ()
803 return current_inferior ()->top_target ()->fetch_x86_xsave_layout ();
807 target_log_command (const char *p
)
809 return current_inferior ()->top_target ()->log_command (p
);
812 /* This is used to implement the various target commands. */
815 open_target (const char *args
, int from_tty
, struct cmd_list_element
*command
)
817 auto *ti
= static_cast<target_info
*> (command
->context ());
818 target_open_ftype
*func
= target_factories
[ti
];
821 gdb_printf (gdb_stdlog
, "-> %s->open (...)\n",
824 func (args
, from_tty
);
827 gdb_printf (gdb_stdlog
, "<- %s->open (%s, %d)\n",
828 ti
->shortname
, args
, from_tty
);
834 add_target (const target_info
&t
, target_open_ftype
*func
,
835 completer_ftype
*completer
)
837 struct cmd_list_element
*c
;
839 auto &func_slot
= target_factories
[&t
];
840 if (func_slot
!= nullptr)
841 internal_error (_("target already added (\"%s\")."), t
.shortname
);
844 if (targetlist
== NULL
)
845 add_basic_prefix_cmd ("target", class_run
, _("\
846 Connect to a target machine or process.\n\
847 The first argument is the type or protocol of the target machine.\n\
848 Remaining arguments are interpreted by the target protocol. For more\n\
849 information on the arguments for a particular protocol, type\n\
850 `help target ' followed by the protocol name."),
851 &targetlist
, 0, &cmdlist
);
852 c
= add_cmd (t
.shortname
, no_class
, t
.doc
, &targetlist
);
853 c
->set_context ((void *) &t
);
854 c
->func
= open_target
;
855 if (completer
!= NULL
)
856 set_cmd_completer (c
, completer
);
862 add_deprecated_target_alias (const target_info
&tinfo
, const char *alias
)
864 struct cmd_list_element
*c
;
866 /* If we use add_alias_cmd, here, we do not get the deprecated warning,
868 c
= add_cmd (alias
, no_class
, tinfo
.doc
, &targetlist
);
869 c
->func
= open_target
;
870 c
->set_context ((void *) &tinfo
);
871 gdb::unique_xmalloc_ptr
<char> alt
872 = xstrprintf ("target %s", tinfo
.shortname
);
873 deprecate_cmd (c
, alt
.release ());
882 /* If the commit_resume_state of the to-be-killed-inferior's process stratum
883 is true, and this inferior is the last live inferior with resumed threads
884 of that target, then we want to leave commit_resume_state to false, as the
885 target won't have any resumed threads anymore. We achieve this with
886 this scoped_disable_commit_resumed. On construction, it will set the flag
887 to false. On destruction, it will only set it to true if there are resumed
889 scoped_disable_commit_resumed
disable ("killing");
890 current_inferior ()->top_target ()->kill ();
894 target_load (const char *arg
, int from_tty
)
896 target_dcache_invalidate (current_program_space
->aspace
);
897 current_inferior ()->top_target ()->load (arg
, from_tty
);
902 target_terminal_state
target_terminal::m_terminal_state
903 = target_terminal_state::is_ours
;
905 /* See target/target.h. */
908 target_terminal::init (void)
910 current_inferior ()->top_target ()->terminal_init ();
912 m_terminal_state
= target_terminal_state::is_ours
;
915 /* See target/target.h. */
918 target_terminal::inferior (void)
920 struct ui
*ui
= current_ui
;
922 /* A background resume (``run&'') should leave GDB in control of the
924 if (ui
->prompt_state
!= PROMPT_BLOCKED
)
927 /* Since we always run the inferior in the main console (unless "set
928 inferior-tty" is in effect), when some UI other than the main one
929 calls target_terminal::inferior, then we leave the main UI's
930 terminal settings as is. */
934 /* If GDB is resuming the inferior in the foreground, install
935 inferior's terminal modes. */
937 struct inferior
*inf
= current_inferior ();
939 if (inf
->terminal_state
!= target_terminal_state::is_inferior
)
941 current_inferior ()->top_target ()->terminal_inferior ();
942 inf
->terminal_state
= target_terminal_state::is_inferior
;
945 m_terminal_state
= target_terminal_state::is_inferior
;
947 /* If the user hit C-c before, pretend that it was hit right
949 if (check_quit_flag ())
950 target_pass_ctrlc ();
953 /* See target/target.h. */
956 target_terminal::restore_inferior (void)
958 struct ui
*ui
= current_ui
;
960 /* See target_terminal::inferior(). */
961 if (ui
->prompt_state
!= PROMPT_BLOCKED
|| ui
!= main_ui
)
964 /* Restore the terminal settings of inferiors that were in the
965 foreground but are now ours_for_output due to a temporary
966 target_target::ours_for_output() call. */
969 scoped_restore_current_inferior restore_inferior
;
971 for (::inferior
*inf
: all_inferiors ())
973 if (inf
->terminal_state
== target_terminal_state::is_ours_for_output
)
975 set_current_inferior (inf
);
976 current_inferior ()->top_target ()->terminal_inferior ();
977 inf
->terminal_state
= target_terminal_state::is_inferior
;
982 m_terminal_state
= target_terminal_state::is_inferior
;
984 /* If the user hit C-c before, pretend that it was hit right
986 if (check_quit_flag ())
987 target_pass_ctrlc ();
990 /* Switch terminal state to DESIRED_STATE, either is_ours, or
991 is_ours_for_output. */
994 target_terminal_is_ours_kind (target_terminal_state desired_state
)
996 scoped_restore_current_inferior restore_inferior
;
998 /* Must do this in two passes. First, have all inferiors save the
999 current terminal settings. Then, after all inferiors have add a
1000 chance to safely save the terminal settings, restore GDB's
1001 terminal settings. */
1003 for (inferior
*inf
: all_inferiors ())
1005 if (inf
->terminal_state
== target_terminal_state::is_inferior
)
1007 set_current_inferior (inf
);
1008 current_inferior ()->top_target ()->terminal_save_inferior ();
1012 for (inferior
*inf
: all_inferiors ())
1014 /* Note we don't check is_inferior here like above because we
1015 need to handle 'is_ours_for_output -> is_ours' too. Careful
1016 to never transition from 'is_ours' to 'is_ours_for_output',
1018 if (inf
->terminal_state
!= target_terminal_state::is_ours
1019 && inf
->terminal_state
!= desired_state
)
1021 set_current_inferior (inf
);
1022 if (desired_state
== target_terminal_state::is_ours
)
1023 current_inferior ()->top_target ()->terminal_ours ();
1024 else if (desired_state
== target_terminal_state::is_ours_for_output
)
1025 current_inferior ()->top_target ()->terminal_ours_for_output ();
1027 gdb_assert_not_reached ("unhandled desired state");
1028 inf
->terminal_state
= desired_state
;
1033 /* See target/target.h. */
1036 target_terminal::ours ()
1038 struct ui
*ui
= current_ui
;
1040 /* See target_terminal::inferior. */
1044 if (m_terminal_state
== target_terminal_state::is_ours
)
1047 target_terminal_is_ours_kind (target_terminal_state::is_ours
);
1048 m_terminal_state
= target_terminal_state::is_ours
;
1051 /* See target/target.h. */
1054 target_terminal::ours_for_output ()
1056 struct ui
*ui
= current_ui
;
1058 /* See target_terminal::inferior. */
1062 if (!target_terminal::is_inferior ())
1065 target_terminal_is_ours_kind (target_terminal_state::is_ours_for_output
);
1066 target_terminal::m_terminal_state
= target_terminal_state::is_ours_for_output
;
1069 /* See target/target.h. */
1072 target_terminal::info (const char *arg
, int from_tty
)
1074 current_inferior ()->top_target ()->terminal_info (arg
, from_tty
);
1080 target_supports_terminal_ours (void)
1082 /* The current top target is the target at the top of the target
1083 stack of the current inferior. While normally there's always an
1084 inferior, we must check for nullptr here because we can get here
1085 very early during startup, before the initial inferior is first
1087 inferior
*inf
= current_inferior ();
1091 return inf
->top_target ()->supports_terminal_ours ();
1097 error (_("You can't do that when your target is `%s'"),
1098 current_inferior ()->top_target ()->shortname ());
1104 error (_("You can't do that without a process to debug."));
1108 default_terminal_info (struct target_ops
*self
, const char *args
, int from_tty
)
1110 gdb_printf (_("No saved terminal information.\n"));
1113 /* A default implementation for the to_get_ada_task_ptid target method.
1115 This function builds the PTID by using both LWP and TID as part of
1116 the PTID lwp and tid elements. The pid used is the pid of the
1120 default_get_ada_task_ptid (struct target_ops
*self
, long lwp
, ULONGEST tid
)
1122 return ptid_t (inferior_ptid
.pid (), lwp
, tid
);
1125 static enum exec_direction_kind
1126 default_execution_direction (struct target_ops
*self
)
1128 if (!target_can_execute_reverse ())
1129 return EXEC_FORWARD
;
1130 else if (!target_can_async_p ())
1131 return EXEC_FORWARD
;
1133 gdb_assert_not_reached ("\
1134 to_execution_direction must be implemented for reverse async");
1140 target_ops_ref_policy::decref (target_ops
*t
)
1143 if (t
->refcount () == 0)
1145 if (t
->stratum () == process_stratum
)
1146 connection_list_remove (as_process_stratum_target (t
));
1148 for (inferior
*inf
: all_inferiors ())
1149 gdb_assert (!inf
->target_is_pushed (t
));
1151 fileio_handles_invalidate_target (t
);
1156 gdb_printf (gdb_stdlog
, "closing target\n");
1163 target_stack::push (target_ops
*t
)
1165 /* We must create a new reference first. It is possible that T is
1166 already pushed on this target stack, in which case we will first
1167 unpush it below, before re-pushing it. If we don't increment the
1168 reference count now, then when we unpush it, we might end up deleting
1169 T, which is not good. */
1170 auto ref
= target_ops_ref::new_reference (t
);
1172 strata stratum
= t
->stratum ();
1174 /* If there's already a target at this stratum, remove it. */
1176 if (m_stack
[stratum
].get () != nullptr)
1177 unpush (m_stack
[stratum
].get ());
1179 /* Now add the new one. */
1180 m_stack
[stratum
] = std::move (ref
);
1182 if (m_top
< stratum
)
1185 if (stratum
== process_stratum
)
1186 connection_list_add (as_process_stratum_target (t
));
1192 target_stack::unpush (target_ops
*t
)
1194 gdb_assert (t
!= NULL
);
1196 strata stratum
= t
->stratum ();
1198 if (stratum
== dummy_stratum
)
1199 internal_error (_("Attempt to unpush the dummy target"));
1201 /* Look for the specified target. Note that a target can only occur
1202 once in the target stack. */
1204 if (m_stack
[stratum
] != t
)
1206 /* If T wasn't pushed, quit. Only open targets should be
1211 if (m_top
== stratum
)
1212 m_top
= this->find_beneath (t
)->stratum ();
1214 /* Move the target reference off the target stack, this sets the pointer
1215 held in m_stack to nullptr, and places the reference in ref. When
1216 ref goes out of scope its reference count will be decremented, which
1217 might cause the target to close.
1219 We have to do it this way, and not just set the value in m_stack to
1220 nullptr directly, because doing so would decrement the reference
1221 count first, which might close the target, and closing the target
1222 does a check that the target is not on any inferiors target_stack. */
1223 auto ref
= std::move (m_stack
[stratum
]);
1229 target_unpusher::operator() (struct target_ops
*ops
) const
1231 current_inferior ()->unpush_target (ops
);
1234 /* Default implementation of to_get_thread_local_address. */
1237 generic_tls_error (void)
1239 throw_error (TLS_GENERIC_ERROR
,
1240 _("Cannot find thread-local variables on this target"));
1243 /* Using the objfile specified in OBJFILE, find the address for the
1244 current thread's thread-local storage with offset OFFSET. */
1246 target_translate_tls_address (struct objfile
*objfile
, CORE_ADDR offset
)
1248 volatile CORE_ADDR addr
= 0;
1249 struct target_ops
*target
= current_inferior ()->top_target ();
1250 gdbarch
*gdbarch
= current_inferior ()->arch ();
1252 /* If OBJFILE is a separate debug object file, look for the
1253 original object file. */
1254 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
1255 objfile
= objfile
->separate_debug_objfile_backlink
;
1257 if (gdbarch_fetch_tls_load_module_address_p (gdbarch
))
1259 ptid_t ptid
= inferior_ptid
;
1265 /* Fetch the load module address for this objfile. */
1266 lm_addr
= gdbarch_fetch_tls_load_module_address (gdbarch
,
1269 if (gdbarch_get_thread_local_address_p (gdbarch
))
1270 addr
= gdbarch_get_thread_local_address (gdbarch
, ptid
, lm_addr
,
1273 addr
= target
->get_thread_local_address (ptid
, lm_addr
, offset
);
1275 /* If an error occurred, print TLS related messages here. Otherwise,
1276 throw the error to some higher catcher. */
1277 catch (const gdb_exception
&ex
)
1279 int objfile_is_library
= (objfile
->flags
& OBJF_SHARED
);
1283 case TLS_NO_LIBRARY_SUPPORT_ERROR
:
1284 error (_("Cannot find thread-local variables "
1285 "in this thread library."));
1287 case TLS_LOAD_MODULE_NOT_FOUND_ERROR
:
1288 if (objfile_is_library
)
1289 error (_("Cannot find shared library `%s' in dynamic"
1290 " linker's load module list"), objfile_name (objfile
));
1292 error (_("Cannot find executable file `%s' in dynamic"
1293 " linker's load module list"), objfile_name (objfile
));
1295 case TLS_NOT_ALLOCATED_YET_ERROR
:
1296 if (objfile_is_library
)
1297 error (_("The inferior has not yet allocated storage for"
1298 " thread-local variables in\n"
1299 "the shared library `%s'\n"
1301 objfile_name (objfile
),
1302 target_pid_to_str (ptid
).c_str ());
1304 error (_("The inferior has not yet allocated storage for"
1305 " thread-local variables in\n"
1306 "the executable `%s'\n"
1308 objfile_name (objfile
),
1309 target_pid_to_str (ptid
).c_str ());
1311 case TLS_GENERIC_ERROR
:
1312 if (objfile_is_library
)
1313 error (_("Cannot find thread-local storage for %s, "
1314 "shared library %s:\n%s"),
1315 target_pid_to_str (ptid
).c_str (),
1316 objfile_name (objfile
), ex
.what ());
1318 error (_("Cannot find thread-local storage for %s, "
1319 "executable file %s:\n%s"),
1320 target_pid_to_str (ptid
).c_str (),
1321 objfile_name (objfile
), ex
.what ());
1330 error (_("Cannot find thread-local variables on this target"));
1336 target_xfer_status_to_string (enum target_xfer_status status
)
1338 #define CASE(X) case X: return #X
1341 CASE(TARGET_XFER_E_IO
);
1342 CASE(TARGET_XFER_UNAVAILABLE
);
1350 const std::vector
<target_section
> *
1351 target_get_section_table (struct target_ops
*target
)
1353 return target
->get_section_table ();
1356 /* Find a section containing ADDR. */
1358 const struct target_section
*
1359 target_section_by_addr (struct target_ops
*target
, CORE_ADDR addr
)
1361 const std::vector
<target_section
> *table
= target_get_section_table (target
);
1366 for (const target_section
&secp
: *table
)
1368 if (addr
>= secp
.addr
&& addr
< secp
.endaddr
)
1376 const std::vector
<target_section
> *
1377 default_get_section_table ()
1379 return ¤t_program_space
->target_sections ();
1382 /* Helper for the memory xfer routines. Checks the attributes of the
1383 memory region of MEMADDR against the read or write being attempted.
1384 If the access is permitted returns true, otherwise returns false.
1385 REGION_P is an optional output parameter. If not-NULL, it is
1386 filled with a pointer to the memory region of MEMADDR. REG_LEN
1387 returns LEN trimmed to the end of the region. This is how much the
1388 caller can continue requesting, if the access is permitted. A
1389 single xfer request must not straddle memory region boundaries. */
1392 memory_xfer_check_region (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1393 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*reg_len
,
1394 struct mem_region
**region_p
)
1396 struct mem_region
*region
;
1398 region
= lookup_mem_region (memaddr
);
1400 if (region_p
!= NULL
)
1403 switch (region
->attrib
.mode
)
1406 if (writebuf
!= NULL
)
1411 if (readbuf
!= NULL
)
1416 /* We only support writing to flash during "load" for now. */
1417 if (writebuf
!= NULL
)
1418 error (_("Writing to flash memory forbidden in this context"));
1425 /* region->hi == 0 means there's no upper bound. */
1426 if (memaddr
+ len
< region
->hi
|| region
->hi
== 0)
1429 *reg_len
= region
->hi
- memaddr
;
1434 /* Read memory from more than one valid target. A core file, for
1435 instance, could have some of memory but delegate other bits to
1436 the target below it. So, we must manually try all targets. */
1438 enum target_xfer_status
1439 raw_memory_xfer_partial (struct target_ops
*ops
, gdb_byte
*readbuf
,
1440 const gdb_byte
*writebuf
, ULONGEST memaddr
, LONGEST len
,
1441 ULONGEST
*xfered_len
)
1443 enum target_xfer_status res
;
1447 res
= ops
->xfer_partial (TARGET_OBJECT_MEMORY
, NULL
,
1448 readbuf
, writebuf
, memaddr
, len
,
1450 if (res
== TARGET_XFER_OK
)
1453 /* Stop if the target reports that the memory is not available. */
1454 if (res
== TARGET_XFER_UNAVAILABLE
)
1457 /* Don't continue past targets which have all the memory.
1458 At one time, this code was necessary to read data from
1459 executables / shared libraries when data for the requested
1460 addresses weren't available in the core file. But now the
1461 core target handles this case itself. */
1462 if (ops
->has_all_memory ())
1465 ops
= ops
->beneath ();
1467 while (ops
!= NULL
);
1469 /* The cache works at the raw memory level. Make sure the cache
1470 gets updated with raw contents no matter what kind of memory
1471 object was originally being written. Note we do write-through
1472 first, so that if it fails, we don't write to the cache contents
1473 that never made it to the target. */
1474 if (writebuf
!= NULL
1475 && inferior_ptid
!= null_ptid
1476 && target_dcache_init_p (current_program_space
->aspace
)
1477 && (stack_cache_enabled_p () || code_cache_enabled_p ()))
1479 DCACHE
*dcache
= target_dcache_get (current_program_space
->aspace
);
1481 /* Note that writing to an area of memory which wasn't present
1482 in the cache doesn't cause it to be loaded in. */
1483 dcache_update (dcache
, res
, memaddr
, writebuf
, *xfered_len
);
1489 /* Perform a partial memory transfer.
1490 For docs see target.h, to_xfer_partial. */
1492 static enum target_xfer_status
1493 memory_xfer_partial_1 (struct target_ops
*ops
, enum target_object object
,
1494 gdb_byte
*readbuf
, const gdb_byte
*writebuf
, ULONGEST memaddr
,
1495 ULONGEST len
, ULONGEST
*xfered_len
)
1497 enum target_xfer_status res
;
1499 struct mem_region
*region
;
1500 struct inferior
*inf
;
1502 /* For accesses to unmapped overlay sections, read directly from
1503 files. Must do this first, as MEMADDR may need adjustment. */
1504 if (readbuf
!= NULL
&& overlay_debugging
)
1506 struct obj_section
*section
= find_pc_overlay (memaddr
);
1508 if (pc_in_unmapped_range (memaddr
, section
))
1510 const std::vector
<target_section
> *table
= target_get_section_table (ops
);
1511 const char *section_name
= section
->the_bfd_section
->name
;
1513 memaddr
= overlay_mapped_address (memaddr
, section
);
1515 auto match_cb
= [=] (const struct target_section
*s
)
1517 return (strcmp (section_name
, s
->the_bfd_section
->name
) == 0);
1520 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1521 memaddr
, len
, xfered_len
,
1526 /* Try the executable files, if "trust-readonly-sections" is set. */
1527 if (readbuf
!= NULL
&& trust_readonly
)
1529 const struct target_section
*secp
1530 = target_section_by_addr (ops
, memaddr
);
1532 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
1534 const std::vector
<target_section
> *table
= target_get_section_table (ops
);
1535 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1536 memaddr
, len
, xfered_len
,
1541 /* Try GDB's internal data cache. */
1543 if (!memory_xfer_check_region (readbuf
, writebuf
, memaddr
, len
, ®_len
,
1545 return TARGET_XFER_E_IO
;
1547 if (inferior_ptid
!= null_ptid
)
1548 inf
= current_inferior ();
1554 /* The dcache reads whole cache lines; that doesn't play well
1555 with reading from a trace buffer, because reading outside of
1556 the collected memory range fails. */
1557 && get_traceframe_number () == -1
1558 && (region
->attrib
.cache
1559 || (stack_cache_enabled_p () && object
== TARGET_OBJECT_STACK_MEMORY
)
1560 || (code_cache_enabled_p () && object
== TARGET_OBJECT_CODE_MEMORY
)))
1563 = target_dcache_get_or_init (current_program_space
->aspace
);
1565 return dcache_read_memory_partial (ops
, dcache
, memaddr
, readbuf
,
1566 reg_len
, xfered_len
);
1569 /* If none of those methods found the memory we wanted, fall back
1570 to a target partial transfer. Normally a single call to
1571 to_xfer_partial is enough; if it doesn't recognize an object
1572 it will call the to_xfer_partial of the next target down.
1573 But for memory this won't do. Memory is the only target
1574 object which can be read from more than one valid target.
1575 A core file, for instance, could have some of memory but
1576 delegate other bits to the target below it. So, we must
1577 manually try all targets. */
1579 res
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, memaddr
, reg_len
,
1582 /* If we still haven't got anything, return the last error. We
1587 /* Perform a partial memory transfer. For docs see target.h,
1590 static enum target_xfer_status
1591 memory_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1592 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1593 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1595 enum target_xfer_status res
;
1597 /* Zero length requests are ok and require no work. */
1599 return TARGET_XFER_EOF
;
1601 memaddr
= gdbarch_remove_non_address_bits (current_inferior ()->arch (),
1604 /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1605 breakpoint insns, thus hiding out from higher layers whether
1606 there are software breakpoints inserted in the code stream. */
1607 if (readbuf
!= NULL
)
1609 res
= memory_xfer_partial_1 (ops
, object
, readbuf
, NULL
, memaddr
, len
,
1612 if (res
== TARGET_XFER_OK
&& !show_memory_breakpoints
)
1613 breakpoint_xfer_memory (readbuf
, NULL
, NULL
, memaddr
, *xfered_len
);
1617 /* A large write request is likely to be partially satisfied
1618 by memory_xfer_partial_1. We will continually malloc
1619 and free a copy of the entire write request for breakpoint
1620 shadow handling even though we only end up writing a small
1621 subset of it. Cap writes to a limit specified by the target
1622 to mitigate this. */
1623 len
= std::min (ops
->get_memory_xfer_limit (), len
);
1625 gdb::byte_vector
buf (writebuf
, writebuf
+ len
);
1626 breakpoint_xfer_memory (NULL
, buf
.data (), writebuf
, memaddr
, len
);
1627 res
= memory_xfer_partial_1 (ops
, object
, NULL
, buf
.data (), memaddr
, len
,
1634 scoped_restore_tmpl
<int>
1635 make_scoped_restore_show_memory_breakpoints (int show
)
1637 return make_scoped_restore (&show_memory_breakpoints
, show
);
1640 /* For docs see target.h, to_xfer_partial. */
1642 enum target_xfer_status
1643 target_xfer_partial (struct target_ops
*ops
,
1644 enum target_object object
, const char *annex
,
1645 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1646 ULONGEST offset
, ULONGEST len
,
1647 ULONGEST
*xfered_len
)
1649 enum target_xfer_status retval
;
1651 /* Transfer is done when LEN is zero. */
1653 return TARGET_XFER_EOF
;
1655 if (writebuf
&& !may_write_memory
)
1656 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1657 core_addr_to_string_nz (offset
), plongest (len
));
1661 /* If this is a memory transfer, let the memory-specific code
1662 have a look at it instead. Memory transfers are more
1664 if (object
== TARGET_OBJECT_MEMORY
|| object
== TARGET_OBJECT_STACK_MEMORY
1665 || object
== TARGET_OBJECT_CODE_MEMORY
)
1666 retval
= memory_xfer_partial (ops
, object
, readbuf
,
1667 writebuf
, offset
, len
, xfered_len
);
1668 else if (object
== TARGET_OBJECT_RAW_MEMORY
)
1670 /* Skip/avoid accessing the target if the memory region
1671 attributes block the access. Check this here instead of in
1672 raw_memory_xfer_partial as otherwise we'd end up checking
1673 this twice in the case of the memory_xfer_partial path is
1674 taken; once before checking the dcache, and another in the
1675 tail call to raw_memory_xfer_partial. */
1676 if (!memory_xfer_check_region (readbuf
, writebuf
, offset
, len
, &len
,
1678 return TARGET_XFER_E_IO
;
1680 /* Request the normal memory object from other layers. */
1681 retval
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, offset
, len
,
1685 retval
= ops
->xfer_partial (object
, annex
, readbuf
,
1686 writebuf
, offset
, len
, xfered_len
);
1690 const unsigned char *myaddr
= NULL
;
1692 gdb_printf (gdb_stdlog
,
1693 "%s:target_xfer_partial "
1694 "(%d, %s, %s, %s, %s, %s) = %d, %s",
1697 (annex
? annex
: "(null)"),
1698 host_address_to_string (readbuf
),
1699 host_address_to_string (writebuf
),
1700 core_addr_to_string_nz (offset
),
1701 pulongest (len
), retval
,
1702 pulongest (*xfered_len
));
1708 if (retval
== TARGET_XFER_OK
&& myaddr
!= NULL
)
1712 gdb_puts (", bytes =", gdb_stdlog
);
1713 for (i
= 0; i
< *xfered_len
; i
++)
1715 if ((((intptr_t) &(myaddr
[i
])) & 0xf) == 0)
1717 if (targetdebug
< 2 && i
> 0)
1719 gdb_printf (gdb_stdlog
, " ...");
1722 gdb_printf (gdb_stdlog
, "\n");
1725 gdb_printf (gdb_stdlog
, " %02x", myaddr
[i
] & 0xff);
1729 gdb_putc ('\n', gdb_stdlog
);
1732 /* Check implementations of to_xfer_partial update *XFERED_LEN
1733 properly. Do assertion after printing debug messages, so that we
1734 can find more clues on assertion failure from debugging messages. */
1735 if (retval
== TARGET_XFER_OK
|| retval
== TARGET_XFER_UNAVAILABLE
)
1736 gdb_assert (*xfered_len
> 0);
1741 /* Read LEN bytes of target memory at address MEMADDR, placing the
1742 results in GDB's memory at MYADDR. Returns either 0 for success or
1743 -1 if any error occurs.
1745 If an error occurs, no guarantee is made about the contents of the data at
1746 MYADDR. In particular, the caller should not depend upon partial reads
1747 filling the buffer with good data. There is no way for the caller to know
1748 how much good data might have been transfered anyway. Callers that can
1749 deal with partial reads should call target_read (which will retry until
1750 it makes no progress, and then return how much was transferred). */
1753 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1755 if (target_read (current_inferior ()->top_target (),
1756 TARGET_OBJECT_MEMORY
, NULL
,
1757 myaddr
, memaddr
, len
) == len
)
1763 /* See target/target.h. */
1766 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
1771 r
= target_read_memory (memaddr
, buf
, sizeof buf
);
1774 *result
= extract_unsigned_integer
1776 gdbarch_byte_order (current_inferior ()->arch ()));
1780 /* Like target_read_memory, but specify explicitly that this is a read
1781 from the target's raw memory. That is, this read bypasses the
1782 dcache, breakpoint shadowing, etc. */
1785 target_read_raw_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1787 if (target_read (current_inferior ()->top_target (),
1788 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1789 myaddr
, memaddr
, len
) == len
)
1795 /* Like target_read_memory, but specify explicitly that this is a read from
1796 the target's stack. This may trigger different cache behavior. */
1799 target_read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1801 if (target_read (current_inferior ()->top_target (),
1802 TARGET_OBJECT_STACK_MEMORY
, NULL
,
1803 myaddr
, memaddr
, len
) == len
)
1809 /* Like target_read_memory, but specify explicitly that this is a read from
1810 the target's code. This may trigger different cache behavior. */
1813 target_read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1815 if (target_read (current_inferior ()->top_target (),
1816 TARGET_OBJECT_CODE_MEMORY
, NULL
,
1817 myaddr
, memaddr
, len
) == len
)
1823 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1824 Returns either 0 for success or -1 if any error occurs. If an
1825 error occurs, no guarantee is made about how much data got written.
1826 Callers that can deal with partial writes should call
1830 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1832 if (target_write (current_inferior ()->top_target (),
1833 TARGET_OBJECT_MEMORY
, NULL
,
1834 myaddr
, memaddr
, len
) == len
)
1840 /* Write LEN bytes from MYADDR to target raw memory at address
1841 MEMADDR. Returns either 0 for success or -1 if any error occurs.
1842 If an error occurs, no guarantee is made about how much data got
1843 written. Callers that can deal with partial writes should call
1847 target_write_raw_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1849 if (target_write (current_inferior ()->top_target (),
1850 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1851 myaddr
, memaddr
, len
) == len
)
1857 /* Fetch the target's memory map. */
1859 std::vector
<mem_region
>
1860 target_memory_map (void)
1862 target_ops
*target
= current_inferior ()->top_target ();
1863 std::vector
<mem_region
> result
= target
->memory_map ();
1864 if (result
.empty ())
1867 std::sort (result
.begin (), result
.end ());
1869 /* Check that regions do not overlap. Simultaneously assign
1870 a numbering for the "mem" commands to use to refer to
1872 mem_region
*last_one
= NULL
;
1873 for (size_t ix
= 0; ix
< result
.size (); ix
++)
1875 mem_region
*this_one
= &result
[ix
];
1876 this_one
->number
= ix
;
1878 if (last_one
!= NULL
&& last_one
->hi
> this_one
->lo
)
1880 warning (_("Overlapping regions in memory map: ignoring"));
1881 return std::vector
<mem_region
> ();
1884 last_one
= this_one
;
1891 target_flash_erase (ULONGEST address
, LONGEST length
)
1893 current_inferior ()->top_target ()->flash_erase (address
, length
);
1897 target_flash_done (void)
1899 current_inferior ()->top_target ()->flash_done ();
1903 show_trust_readonly (struct ui_file
*file
, int from_tty
,
1904 struct cmd_list_element
*c
, const char *value
)
1907 _("Mode for reading from readonly sections is %s.\n"),
1911 /* Target vector read/write partial wrapper functions. */
1913 static enum target_xfer_status
1914 target_read_partial (struct target_ops
*ops
,
1915 enum target_object object
,
1916 const char *annex
, gdb_byte
*buf
,
1917 ULONGEST offset
, ULONGEST len
,
1918 ULONGEST
*xfered_len
)
1920 return target_xfer_partial (ops
, object
, annex
, buf
, NULL
, offset
, len
,
1924 static enum target_xfer_status
1925 target_write_partial (struct target_ops
*ops
,
1926 enum target_object object
,
1927 const char *annex
, const gdb_byte
*buf
,
1928 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
1930 return target_xfer_partial (ops
, object
, annex
, NULL
, buf
, offset
, len
,
1934 /* Wrappers to perform the full transfer. */
1936 /* For docs on target_read see target.h. */
1939 target_read (struct target_ops
*ops
,
1940 enum target_object object
,
1941 const char *annex
, gdb_byte
*buf
,
1942 ULONGEST offset
, LONGEST len
)
1944 LONGEST xfered_total
= 0;
1947 /* If we are reading from a memory object, find the length of an addressable
1948 unit for that architecture. */
1949 if (object
== TARGET_OBJECT_MEMORY
1950 || object
== TARGET_OBJECT_STACK_MEMORY
1951 || object
== TARGET_OBJECT_CODE_MEMORY
1952 || object
== TARGET_OBJECT_RAW_MEMORY
)
1953 unit_size
= gdbarch_addressable_memory_unit_size
1954 (current_inferior ()->arch ());
1956 while (xfered_total
< len
)
1958 ULONGEST xfered_partial
;
1959 enum target_xfer_status status
;
1961 status
= target_read_partial (ops
, object
, annex
,
1962 buf
+ xfered_total
* unit_size
,
1963 offset
+ xfered_total
, len
- xfered_total
,
1966 /* Call an observer, notifying them of the xfer progress? */
1967 if (status
== TARGET_XFER_EOF
)
1968 return xfered_total
;
1969 else if (status
== TARGET_XFER_OK
)
1971 xfered_total
+= xfered_partial
;
1975 return TARGET_XFER_E_IO
;
1981 /* Assuming that the entire [begin, end) range of memory cannot be
1982 read, try to read whatever subrange is possible to read.
1984 The function returns, in RESULT, either zero or one memory block.
1985 If there's a readable subrange at the beginning, it is completely
1986 read and returned. Any further readable subrange will not be read.
1987 Otherwise, if there's a readable subrange at the end, it will be
1988 completely read and returned. Any readable subranges before it
1989 (obviously, not starting at the beginning), will be ignored. In
1990 other cases -- either no readable subrange, or readable subrange(s)
1991 that is neither at the beginning, or end, nothing is returned.
1993 The purpose of this function is to handle a read across a boundary
1994 of accessible memory in a case when memory map is not available.
1995 The above restrictions are fine for this case, but will give
1996 incorrect results if the memory is 'patchy'. However, supporting
1997 'patchy' memory would require trying to read every single byte,
1998 and it seems unacceptable solution. Explicit memory map is
1999 recommended for this case -- and target_read_memory_robust will
2000 take care of reading multiple ranges then. */
2003 read_whatever_is_readable (struct target_ops
*ops
,
2004 const ULONGEST begin
, const ULONGEST end
,
2006 std::vector
<memory_read_result
> *result
)
2008 ULONGEST current_begin
= begin
;
2009 ULONGEST current_end
= end
;
2011 ULONGEST xfered_len
;
2013 /* If we previously failed to read 1 byte, nothing can be done here. */
2014 if (end
- begin
<= 1)
2017 gdb::unique_xmalloc_ptr
<gdb_byte
> buf ((gdb_byte
*) xmalloc (end
- begin
));
2019 /* Check that either first or the last byte is readable, and give up
2020 if not. This heuristic is meant to permit reading accessible memory
2021 at the boundary of accessible region. */
2022 if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2023 buf
.get (), begin
, 1, &xfered_len
) == TARGET_XFER_OK
)
2028 else if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2029 buf
.get () + (end
- begin
) - 1, end
- 1, 1,
2030 &xfered_len
) == TARGET_XFER_OK
)
2038 /* Loop invariant is that the [current_begin, current_end) was previously
2039 found to be not readable as a whole.
2041 Note loop condition -- if the range has 1 byte, we can't divide the range
2042 so there's no point trying further. */
2043 while (current_end
- current_begin
> 1)
2045 ULONGEST first_half_begin
, first_half_end
;
2046 ULONGEST second_half_begin
, second_half_end
;
2048 ULONGEST middle
= current_begin
+ (current_end
- current_begin
) / 2;
2052 first_half_begin
= current_begin
;
2053 first_half_end
= middle
;
2054 second_half_begin
= middle
;
2055 second_half_end
= current_end
;
2059 first_half_begin
= middle
;
2060 first_half_end
= current_end
;
2061 second_half_begin
= current_begin
;
2062 second_half_end
= middle
;
2065 xfer
= target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2066 buf
.get () + (first_half_begin
- begin
) * unit_size
,
2068 first_half_end
- first_half_begin
);
2070 if (xfer
== first_half_end
- first_half_begin
)
2072 /* This half reads up fine. So, the error must be in the
2074 current_begin
= second_half_begin
;
2075 current_end
= second_half_end
;
2079 /* This half is not readable. Because we've tried one byte, we
2080 know some part of this half if actually readable. Go to the next
2081 iteration to divide again and try to read.
2083 We don't handle the other half, because this function only tries
2084 to read a single readable subrange. */
2085 current_begin
= first_half_begin
;
2086 current_end
= first_half_end
;
2092 /* The [begin, current_begin) range has been read. */
2093 result
->emplace_back (begin
, current_end
, std::move (buf
));
2097 /* The [current_end, end) range has been read. */
2098 LONGEST region_len
= end
- current_end
;
2100 gdb::unique_xmalloc_ptr
<gdb_byte
> data
2101 ((gdb_byte
*) xmalloc (region_len
* unit_size
));
2102 memcpy (data
.get (), buf
.get () + (current_end
- begin
) * unit_size
,
2103 region_len
* unit_size
);
2104 result
->emplace_back (current_end
, end
, std::move (data
));
2108 std::vector
<memory_read_result
>
2109 read_memory_robust (struct target_ops
*ops
,
2110 const ULONGEST offset
, const LONGEST len
)
2112 std::vector
<memory_read_result
> result
;
2114 = gdbarch_addressable_memory_unit_size (current_inferior ()->arch ());
2116 LONGEST xfered_total
= 0;
2117 while (xfered_total
< len
)
2119 struct mem_region
*region
= lookup_mem_region (offset
+ xfered_total
);
2122 /* If there is no explicit region, a fake one should be created. */
2123 gdb_assert (region
);
2125 if (region
->hi
== 0)
2126 region_len
= len
- xfered_total
;
2128 region_len
= region
->hi
- offset
;
2130 if (region
->attrib
.mode
== MEM_NONE
|| region
->attrib
.mode
== MEM_WO
)
2132 /* Cannot read this region. Note that we can end up here only
2133 if the region is explicitly marked inaccessible, or
2134 'inaccessible-by-default' is in effect. */
2135 xfered_total
+= region_len
;
2139 LONGEST to_read
= std::min (len
- xfered_total
, region_len
);
2140 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
2141 ((gdb_byte
*) xmalloc (to_read
* unit_size
));
2143 LONGEST xfered_partial
=
2144 target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
, buffer
.get (),
2145 offset
+ xfered_total
, to_read
);
2146 /* Call an observer, notifying them of the xfer progress? */
2147 if (xfered_partial
<= 0)
2149 /* Got an error reading full chunk. See if maybe we can read
2151 read_whatever_is_readable (ops
, offset
+ xfered_total
,
2152 offset
+ xfered_total
+ to_read
,
2153 unit_size
, &result
);
2154 xfered_total
+= to_read
;
2158 result
.emplace_back (offset
+ xfered_total
,
2159 offset
+ xfered_total
+ xfered_partial
,
2160 std::move (buffer
));
2161 xfered_total
+= xfered_partial
;
2171 /* An alternative to target_write with progress callbacks. */
2174 target_write_with_progress (struct target_ops
*ops
,
2175 enum target_object object
,
2176 const char *annex
, const gdb_byte
*buf
,
2177 ULONGEST offset
, LONGEST len
,
2178 void (*progress
) (ULONGEST
, void *), void *baton
)
2180 LONGEST xfered_total
= 0;
2183 /* If we are writing to a memory object, find the length of an addressable
2184 unit for that architecture. */
2185 if (object
== TARGET_OBJECT_MEMORY
2186 || object
== TARGET_OBJECT_STACK_MEMORY
2187 || object
== TARGET_OBJECT_CODE_MEMORY
2188 || object
== TARGET_OBJECT_RAW_MEMORY
)
2189 unit_size
= gdbarch_addressable_memory_unit_size
2190 (current_inferior ()->arch ());
2192 /* Give the progress callback a chance to set up. */
2194 (*progress
) (0, baton
);
2196 while (xfered_total
< len
)
2198 ULONGEST xfered_partial
;
2199 enum target_xfer_status status
;
2201 status
= target_write_partial (ops
, object
, annex
,
2202 buf
+ xfered_total
* unit_size
,
2203 offset
+ xfered_total
, len
- xfered_total
,
2206 if (status
!= TARGET_XFER_OK
)
2207 return status
== TARGET_XFER_EOF
? xfered_total
: TARGET_XFER_E_IO
;
2210 (*progress
) (xfered_partial
, baton
);
2212 xfered_total
+= xfered_partial
;
2218 /* For docs on target_write see target.h. */
2221 target_write (struct target_ops
*ops
,
2222 enum target_object object
,
2223 const char *annex
, const gdb_byte
*buf
,
2224 ULONGEST offset
, LONGEST len
)
2226 return target_write_with_progress (ops
, object
, annex
, buf
, offset
, len
,
2230 /* Help for target_read_alloc and target_read_stralloc. See their comments
2233 template <typename T
>
2234 std::optional
<gdb::def_vector
<T
>>
2235 target_read_alloc_1 (struct target_ops
*ops
, enum target_object object
,
2238 gdb::def_vector
<T
> buf
;
2240 const int chunk
= 4096;
2242 /* This function does not have a length parameter; it reads the
2243 entire OBJECT). Also, it doesn't support objects fetched partly
2244 from one target and partly from another (in a different stratum,
2245 e.g. a core file and an executable). Both reasons make it
2246 unsuitable for reading memory. */
2247 gdb_assert (object
!= TARGET_OBJECT_MEMORY
);
2249 /* Start by reading up to 4K at a time. The target will throttle
2250 this number down if necessary. */
2253 ULONGEST xfered_len
;
2254 enum target_xfer_status status
;
2256 buf
.resize (buf_pos
+ chunk
);
2258 status
= target_read_partial (ops
, object
, annex
,
2259 (gdb_byte
*) &buf
[buf_pos
],
2263 if (status
== TARGET_XFER_EOF
)
2265 /* Read all there was. */
2266 buf
.resize (buf_pos
);
2269 else if (status
!= TARGET_XFER_OK
)
2271 /* An error occurred. */
2275 buf_pos
+= xfered_len
;
2283 std::optional
<gdb::byte_vector
>
2284 target_read_alloc (struct target_ops
*ops
, enum target_object object
,
2287 return target_read_alloc_1
<gdb_byte
> (ops
, object
, annex
);
2292 std::optional
<gdb::char_vector
>
2293 target_read_stralloc (struct target_ops
*ops
, enum target_object object
,
2296 std::optional
<gdb::char_vector
> buf
2297 = target_read_alloc_1
<char> (ops
, object
, annex
);
2302 if (buf
->empty () || buf
->back () != '\0')
2303 buf
->push_back ('\0');
2305 /* Check for embedded NUL bytes; but allow trailing NULs. */
2306 for (auto it
= std::find (buf
->begin (), buf
->end (), '\0');
2307 it
!= buf
->end (); it
++)
2310 warning (_("target object %d, annex %s, "
2311 "contained unexpected null characters"),
2312 (int) object
, annex
? annex
: "(none)");
2319 /* Memory transfer methods. */
2322 get_target_memory (struct target_ops
*ops
, CORE_ADDR addr
, gdb_byte
*buf
,
2325 /* This method is used to read from an alternate, non-current
2326 target. This read must bypass the overlay support (as symbols
2327 don't match this target), and GDB's internal cache (wrong cache
2328 for this target). */
2329 if (target_read (ops
, TARGET_OBJECT_RAW_MEMORY
, NULL
, buf
, addr
, len
)
2331 memory_error (TARGET_XFER_E_IO
, addr
);
2335 get_target_memory_unsigned (struct target_ops
*ops
, CORE_ADDR addr
,
2336 int len
, enum bfd_endian byte_order
)
2338 gdb_byte buf
[sizeof (ULONGEST
)];
2340 gdb_assert (len
<= sizeof (buf
));
2341 get_target_memory (ops
, addr
, buf
, len
);
2342 return extract_unsigned_integer (buf
, len
, byte_order
);
2348 target_insert_breakpoint (struct gdbarch
*gdbarch
,
2349 struct bp_target_info
*bp_tgt
)
2351 if (!may_insert_breakpoints
)
2353 warning (_("May not insert breakpoints"));
2357 target_ops
*target
= current_inferior ()->top_target ();
2359 return target
->insert_breakpoint (gdbarch
, bp_tgt
);
2365 target_remove_breakpoint (struct gdbarch
*gdbarch
,
2366 struct bp_target_info
*bp_tgt
,
2367 enum remove_bp_reason reason
)
2369 /* This is kind of a weird case to handle, but the permission might
2370 have been changed after breakpoints were inserted - in which case
2371 we should just take the user literally and assume that any
2372 breakpoints should be left in place. */
2373 if (!may_insert_breakpoints
)
2375 warning (_("May not remove breakpoints"));
2379 target_ops
*target
= current_inferior ()->top_target ();
2381 return target
->remove_breakpoint (gdbarch
, bp_tgt
, reason
);
2385 info_target_command (const char *args
, int from_tty
)
2387 int has_all_mem
= 0;
2389 if (current_program_space
->symfile_object_file
!= NULL
)
2391 objfile
*objf
= current_program_space
->symfile_object_file
;
2392 gdb_printf (_("Symbols from \"%s\".\n"),
2393 objfile_name (objf
));
2396 for (target_ops
*t
= current_inferior ()->top_target ();
2400 if (!t
->has_memory ())
2403 if ((int) (t
->stratum ()) <= (int) dummy_stratum
)
2406 gdb_printf (_("\tWhile running this, "
2407 "GDB does not access memory from...\n"));
2408 gdb_printf ("%s:\n", t
->longname ());
2410 has_all_mem
= t
->has_all_memory ();
2414 /* This function is called before any new inferior is created, e.g.
2415 by running a program, attaching, or connecting to a target.
2416 It cleans up any state from previous invocations which might
2417 change between runs. This is a subset of what target_preopen
2418 resets (things which might change between targets). */
2421 target_pre_inferior (int from_tty
)
2423 /* Clear out solib state. Otherwise the solib state of the previous
2424 inferior might have survived and is entirely wrong for the new
2425 target. This has been observed on GNU/Linux using glibc 2.3. How
2437 Cannot access memory at address 0xdeadbeef
2440 /* In some OSs, the shared library list is the same/global/shared
2441 across inferiors. If code is shared between processes, so are
2442 memory regions and features. */
2443 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
2445 no_shared_libraries (NULL
, from_tty
);
2447 invalidate_target_mem_regions ();
2449 target_clear_description ();
2452 /* attach_flag may be set if the previous process associated with
2453 the inferior was attached to. */
2454 current_inferior ()->attach_flag
= false;
2456 current_inferior ()->highest_thread_num
= 0;
2458 update_previous_thread ();
2460 agent_capability_invalidate ();
2463 /* This is to be called by the open routine before it does
2467 target_preopen (int from_tty
)
2471 if (current_inferior ()->pid
!= 0)
2474 || !target_has_execution ()
2475 || query (_("A program is being debugged already. Kill it? ")))
2477 /* Core inferiors actually should be detached, not
2479 if (target_has_execution ())
2482 target_detach (current_inferior (), 0);
2485 error (_("Program not killed."));
2488 /* Release reference to old previous thread. */
2489 update_previous_thread ();
2491 /* Calling target_kill may remove the target from the stack. But if
2492 it doesn't (which seems like a win for UDI), remove it now. */
2493 /* Leave the exec target, though. The user may be switching from a
2494 live process to a core of the same program. */
2495 current_inferior ()->pop_all_targets_above (file_stratum
);
2497 target_pre_inferior (from_tty
);
2503 target_detach (inferior
*inf
, int from_tty
)
2505 /* Thread's don't need to be resumed until the end of this function. */
2506 scoped_disable_commit_resumed
disable_commit_resumed ("detaching");
2508 /* After we have detached, we will clear the register cache for this inferior
2509 by calling registers_changed_ptid. We must save the pid_ptid before
2510 detaching, as the target detach method will clear inf->pid. */
2511 ptid_t save_pid_ptid
= ptid_t (inf
->pid
);
2513 /* As long as some to_detach implementations rely on the current_inferior
2514 (either directly, or indirectly, like through reading memory), INF needs
2515 to be the current inferior. When that requirement will become no longer
2516 true, then we can remove this assertion. */
2517 gdb_assert (inf
== current_inferior ());
2519 prepare_for_detach ();
2521 gdb::observers::inferior_pre_detach
.notify (inf
);
2523 /* Hold a strong reference because detaching may unpush the
2525 auto proc_target_ref
= target_ops_ref::new_reference (inf
->process_target ());
2527 current_inferior ()->top_target ()->detach (inf
, from_tty
);
2529 process_stratum_target
*proc_target
2530 = as_process_stratum_target (proc_target_ref
.get ());
2532 registers_changed_ptid (proc_target
, save_pid_ptid
);
2534 /* We have to ensure we have no frame cache left. Normally,
2535 registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
2536 inferior_ptid matches save_pid_ptid, but in our case, it does not
2537 call it, as inferior_ptid has been reset. */
2538 reinit_frame_cache ();
2540 disable_commit_resumed
.reset_and_commit ();
2544 target_disconnect (const char *args
, int from_tty
)
2546 /* If we're in breakpoints-always-inserted mode or if breakpoints
2547 are global across processes, we have to remove them before
2549 remove_breakpoints ();
2551 current_inferior ()->top_target ()->disconnect (args
, from_tty
);
2554 /* See target/target.h. */
2557 target_wait (ptid_t ptid
, struct target_waitstatus
*status
,
2558 target_wait_flags options
)
2560 target_ops
*target
= current_inferior ()->top_target ();
2561 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
2563 gdb_assert (!proc_target
->commit_resumed_state
);
2565 if (!target_can_async_p (target
))
2566 gdb_assert ((options
& TARGET_WNOHANG
) == 0);
2570 gdb::observers::target_pre_wait
.notify (ptid
);
2571 ptid_t event_ptid
= target
->wait (ptid
, status
, options
);
2572 gdb::observers::target_post_wait
.notify (event_ptid
);
2577 gdb::observers::target_post_wait
.notify (null_ptid
);
2585 default_target_wait (struct target_ops
*ops
,
2586 ptid_t ptid
, struct target_waitstatus
*status
,
2587 target_wait_flags options
)
2589 status
->set_ignore ();
2590 return minus_one_ptid
;
2594 target_pid_to_str (ptid_t ptid
)
2596 return current_inferior ()->top_target ()->pid_to_str (ptid
);
2600 target_thread_name (struct thread_info
*info
)
2602 gdb_assert (info
->inf
== current_inferior ());
2604 return current_inferior ()->top_target ()->thread_name (info
);
2607 struct thread_info
*
2608 target_thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
2610 struct inferior
*inf
)
2612 target_ops
*target
= current_inferior ()->top_target ();
2614 return target
->thread_handle_to_thread_info (thread_handle
, handle_len
, inf
);
2619 gdb::array_view
<const gdb_byte
>
2620 target_thread_info_to_thread_handle (struct thread_info
*tip
)
2622 target_ops
*target
= current_inferior ()->top_target ();
2624 return target
->thread_info_to_thread_handle (tip
);
2628 target_resume (ptid_t scope_ptid
, int step
, enum gdb_signal signal
)
2630 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
2631 gdb_assert (!curr_target
->commit_resumed_state
);
2633 gdb_assert (inferior_ptid
!= null_ptid
);
2634 gdb_assert (inferior_ptid
.matches (scope_ptid
));
2636 target_dcache_invalidate (current_program_space
->aspace
);
2638 current_inferior ()->top_target ()->resume (scope_ptid
, step
, signal
);
2640 registers_changed_ptid (curr_target
, scope_ptid
);
2641 /* We only set the internal executing state here. The user/frontend
2642 running state is set at a higher level. This also clears the
2643 thread's stop_pc as side effect. */
2644 set_executing (curr_target
, scope_ptid
, true);
2645 clear_inline_frame_state (curr_target
, scope_ptid
);
2647 if (target_can_async_p ())
2648 target_async (true);
2654 target_commit_resumed ()
2656 gdb_assert (current_inferior ()->process_target ()->commit_resumed_state
);
2657 current_inferior ()->top_target ()->commit_resumed ();
2663 target_has_pending_events ()
2665 return current_inferior ()->top_target ()->has_pending_events ();
2669 target_pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2671 current_inferior ()->top_target ()->pass_signals (pass_signals
);
2675 target_program_signals (gdb::array_view
<const unsigned char> program_signals
)
2677 current_inferior ()->top_target ()->program_signals (program_signals
);
2681 default_follow_fork (struct target_ops
*self
, inferior
*child_inf
,
2682 ptid_t child_ptid
, target_waitkind fork_kind
,
2683 bool follow_child
, bool detach_fork
)
2685 /* Some target returned a fork event, but did not know how to follow it. */
2686 internal_error (_("could not find a target to follow fork"));
2690 default_follow_clone (struct target_ops
*self
, ptid_t child_ptid
)
2692 /* Some target returned a clone event, but did not know how to follow it. */
2693 internal_error (_("could not find a target to follow clone"));
2699 target_follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
2700 target_waitkind fork_kind
, bool follow_child
,
2703 target_ops
*target
= current_inferior ()->top_target ();
2705 /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
2707 if (child_inf
!= nullptr)
2709 gdb_assert (follow_child
|| !detach_fork
);
2710 gdb_assert (child_inf
->pid
== child_ptid
.pid ());
2713 gdb_assert (!follow_child
&& detach_fork
);
2715 return target
->follow_fork (child_inf
, child_ptid
, fork_kind
, follow_child
,
2722 target_follow_exec (inferior
*follow_inf
, ptid_t ptid
,
2723 const char *execd_pathname
)
2725 current_inferior ()->top_target ()->follow_exec (follow_inf
, ptid
,
2730 default_mourn_inferior (struct target_ops
*self
)
2732 internal_error (_("could not find a target to follow mourn inferior"));
2736 target_mourn_inferior (ptid_t ptid
)
2738 gdb_assert (ptid
.pid () == inferior_ptid
.pid ());
2739 current_inferior ()->top_target ()->mourn_inferior ();
2742 /* Look for a target which can describe architectural features, starting
2743 from TARGET. If we find one, return its description. */
2745 const struct target_desc
*
2746 target_read_description (struct target_ops
*target
)
2748 return target
->read_description ();
2752 /* Default implementation of memory-searching. */
2755 default_search_memory (struct target_ops
*self
,
2756 CORE_ADDR start_addr
, ULONGEST search_space_len
,
2757 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2758 CORE_ADDR
*found_addrp
)
2760 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
2762 return target_read (current_inferior ()->top_target (),
2763 TARGET_OBJECT_MEMORY
, NULL
,
2764 result
, addr
, len
) == len
;
2767 /* Start over from the top of the target stack. */
2768 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
2769 pattern
, pattern_len
, found_addrp
);
2772 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2773 sequence of bytes in PATTERN with length PATTERN_LEN.
2775 The result is 1 if found, 0 if not found, and -1 if there was an error
2776 requiring halting of the search (e.g. memory read error).
2777 If the pattern is found the address is recorded in FOUND_ADDRP. */
2780 target_search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
2781 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2782 CORE_ADDR
*found_addrp
)
2784 target_ops
*target
= current_inferior ()->top_target ();
2786 return target
->search_memory (start_addr
, search_space_len
, pattern
,
2787 pattern_len
, found_addrp
);
2790 /* Look through the currently pushed targets. If none of them will
2791 be able to restart the currently running process, issue an error
2795 target_require_runnable (void)
2797 for (target_ops
*t
= current_inferior ()->top_target ();
2801 /* If this target knows how to create a new program, then
2802 assume we will still be able to after killing the current
2803 one. Either killing and mourning will not pop T, or else
2804 find_default_run_target will find it again. */
2805 if (t
->can_create_inferior ())
2808 /* Do not worry about targets at certain strata that can not
2809 create inferiors. Assume they will be pushed again if
2810 necessary, and continue to the process_stratum. */
2811 if (t
->stratum () > process_stratum
)
2814 error (_("The \"%s\" target does not support \"run\". "
2815 "Try \"help target\" or \"continue\"."),
2819 /* This function is only called if the target is running. In that
2820 case there should have been a process_stratum target and it
2821 should either know how to create inferiors, or not... */
2822 internal_error (_("No targets found"));
2825 /* Whether GDB is allowed to fall back to the default run target for
2826 "run", "attach", etc. when no target is connected yet. */
2827 static bool auto_connect_native_target
= true;
2830 show_auto_connect_native_target (struct ui_file
*file
, int from_tty
,
2831 struct cmd_list_element
*c
, const char *value
)
2834 _("Whether GDB may automatically connect to the "
2835 "native target is %s.\n"),
2839 /* A pointer to the target that can respond to "run" or "attach".
2840 Native targets are always singletons and instantiated early at GDB
2842 static target_ops
*the_native_target
;
2847 set_native_target (target_ops
*target
)
2849 if (the_native_target
!= NULL
)
2850 internal_error (_("native target already set (\"%s\")."),
2851 the_native_target
->longname ());
2853 the_native_target
= target
;
2859 get_native_target ()
2861 return the_native_target
;
2864 /* Look through the list of possible targets for a target that can
2865 execute a run or attach command without any other data. This is
2866 used to locate the default process stratum.
2868 If DO_MESG is not NULL, the result is always valid (error() is
2869 called for errors); else, return NULL on error. */
2871 static struct target_ops
*
2872 find_default_run_target (const char *do_mesg
)
2874 if (auto_connect_native_target
&& the_native_target
!= NULL
)
2875 return the_native_target
;
2877 if (do_mesg
!= NULL
)
2878 error (_("Don't know how to %s. Try \"help target\"."), do_mesg
);
2885 find_attach_target (void)
2887 /* If a target on the current stack can attach, use it. */
2888 for (target_ops
*t
= current_inferior ()->top_target ();
2892 if (t
->can_attach ())
2896 /* Otherwise, use the default run target for attaching. */
2897 return find_default_run_target ("attach");
2903 find_run_target (void)
2905 /* If a target on the current stack can run, use it. */
2906 for (target_ops
*t
= current_inferior ()->top_target ();
2910 if (t
->can_create_inferior ())
2914 /* Otherwise, use the default run target. */
2915 return find_default_run_target ("run");
2919 target_ops::info_proc (const char *args
, enum info_proc_what what
)
2924 /* Implement the "info proc" command. */
2927 target_info_proc (const char *args
, enum info_proc_what what
)
2929 struct target_ops
*t
;
2931 /* If we're already connected to something that can get us OS
2932 related data, use it. Otherwise, try using the native
2934 t
= find_target_at (process_stratum
);
2936 t
= find_default_run_target (NULL
);
2938 for (; t
!= NULL
; t
= t
->beneath ())
2940 if (t
->info_proc (args
, what
))
2943 gdb_printf (gdb_stdlog
,
2944 "target_info_proc (\"%s\", %d)\n", args
, what
);
2954 find_default_supports_disable_randomization (struct target_ops
*self
)
2956 struct target_ops
*t
;
2958 t
= find_default_run_target (NULL
);
2960 return t
->supports_disable_randomization ();
2965 target_supports_disable_randomization (void)
2967 return current_inferior ()->top_target ()->supports_disable_randomization ();
2970 /* See target/target.h. */
2973 target_supports_multi_process (void)
2975 return current_inferior ()->top_target ()->supports_multi_process ();
2980 std::optional
<gdb::char_vector
>
2981 target_get_osdata (const char *type
)
2983 struct target_ops
*t
;
2985 /* If we're already connected to something that can get us OS
2986 related data, use it. Otherwise, try using the native
2988 t
= find_target_at (process_stratum
);
2990 t
= find_default_run_target ("get OS data");
2995 return target_read_stralloc (t
, TARGET_OBJECT_OSDATA
, type
);
3001 target_ops::beneath () const
3003 return current_inferior ()->find_target_beneath (this);
3007 target_ops::close ()
3012 target_ops::can_attach ()
3018 target_ops::attach (const char *, int)
3020 gdb_assert_not_reached ("target_ops::attach called");
3024 target_ops::can_create_inferior ()
3030 target_ops::create_inferior (const char *, const std::string
&,
3033 gdb_assert_not_reached ("target_ops::create_inferior called");
3037 target_ops::can_run ()
3045 for (target_ops
*t
= current_inferior ()->top_target ();
3056 /* Target file operations. */
3058 static struct target_ops
*
3059 default_fileio_target (void)
3061 struct target_ops
*t
;
3063 /* If we're already connected to something that can perform
3064 file I/O, use it. Otherwise, try using the native target. */
3065 t
= find_target_at (process_stratum
);
3068 return find_default_run_target ("file I/O");
3071 /* File handle for target file operations. */
3075 /* The target on which this file is open. NULL if the target is
3076 meanwhile closed while the handle is open. */
3079 /* The file descriptor on the target. */
3082 /* Check whether this fileio_fh_t represents a closed file. */
3085 return target_fd
< 0;
3089 /* Vector of currently open file handles. The value returned by
3090 target_fileio_open and passed as the FD argument to other
3091 target_fileio_* functions is an index into this vector. This
3092 vector's entries are never freed; instead, files are marked as
3093 closed, and the handle becomes available for reuse. */
3094 static std::vector
<fileio_fh_t
> fileio_fhandles
;
3096 /* Index into fileio_fhandles of the lowest handle that might be
3097 closed. This permits handle reuse without searching the whole
3098 list each time a new file is opened. */
3099 static int lowest_closed_fd
;
3104 fileio_handles_invalidate_target (target_ops
*targ
)
3106 for (fileio_fh_t
&fh
: fileio_fhandles
)
3107 if (fh
.target
== targ
)
3111 /* Acquire a target fileio file descriptor. */
3114 acquire_fileio_fd (target_ops
*target
, int target_fd
)
3116 /* Search for closed handles to reuse. */
3117 for (; lowest_closed_fd
< fileio_fhandles
.size (); lowest_closed_fd
++)
3119 fileio_fh_t
&fh
= fileio_fhandles
[lowest_closed_fd
];
3121 if (fh
.is_closed ())
3125 /* Push a new handle if no closed handles were found. */
3126 if (lowest_closed_fd
== fileio_fhandles
.size ())
3127 fileio_fhandles
.push_back (fileio_fh_t
{target
, target_fd
});
3129 fileio_fhandles
[lowest_closed_fd
] = {target
, target_fd
};
3131 /* Should no longer be marked closed. */
3132 gdb_assert (!fileio_fhandles
[lowest_closed_fd
].is_closed ());
3134 /* Return its index, and start the next lookup at
3136 return lowest_closed_fd
++;
3139 /* Release a target fileio file descriptor. */
3142 release_fileio_fd (int fd
, fileio_fh_t
*fh
)
3145 lowest_closed_fd
= std::min (lowest_closed_fd
, fd
);
3148 /* Return a pointer to the fileio_fhandle_t corresponding to FD. */
3150 static fileio_fh_t
*
3151 fileio_fd_to_fh (int fd
)
3153 return &fileio_fhandles
[fd
];
3157 /* Default implementations of file i/o methods. We don't want these
3158 to delegate automatically, because we need to know which target
3159 supported the method, in order to call it directly from within
3160 pread/pwrite, etc. */
3163 target_ops::fileio_open (struct inferior
*inf
, const char *filename
,
3164 int flags
, int mode
, int warn_if_slow
,
3165 fileio_error
*target_errno
)
3167 *target_errno
= FILEIO_ENOSYS
;
3172 target_ops::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3173 ULONGEST offset
, fileio_error
*target_errno
)
3175 *target_errno
= FILEIO_ENOSYS
;
3180 target_ops::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3181 ULONGEST offset
, fileio_error
*target_errno
)
3183 *target_errno
= FILEIO_ENOSYS
;
3188 target_ops::fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
)
3190 *target_errno
= FILEIO_ENOSYS
;
3195 target_ops::fileio_close (int fd
, fileio_error
*target_errno
)
3197 *target_errno
= FILEIO_ENOSYS
;
3202 target_ops::fileio_unlink (struct inferior
*inf
, const char *filename
,
3203 fileio_error
*target_errno
)
3205 *target_errno
= FILEIO_ENOSYS
;
3209 std::optional
<std::string
>
3210 target_ops::fileio_readlink (struct inferior
*inf
, const char *filename
,
3211 fileio_error
*target_errno
)
3213 *target_errno
= FILEIO_ENOSYS
;
3220 target_fileio_open (struct inferior
*inf
, const char *filename
,
3221 int flags
, int mode
, bool warn_if_slow
, fileio_error
*target_errno
)
3223 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3225 int fd
= t
->fileio_open (inf
, filename
, flags
, mode
,
3226 warn_if_slow
, target_errno
);
3228 if (fd
== -1 && *target_errno
== FILEIO_ENOSYS
)
3234 fd
= acquire_fileio_fd (t
, fd
);
3237 gdb_printf (gdb_stdlog
,
3238 "target_fileio_open (%d,%s,0x%x,0%o,%d)"
3240 inf
== NULL
? 0 : inf
->num
,
3241 filename
, flags
, mode
,
3243 fd
!= -1 ? 0 : *target_errno
);
3247 *target_errno
= FILEIO_ENOSYS
;
3254 target_fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3255 ULONGEST offset
, fileio_error
*target_errno
)
3257 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3260 if (fh
->is_closed ())
3261 *target_errno
= FILEIO_EBADF
;
3262 else if (fh
->target
== NULL
)
3263 *target_errno
= FILEIO_EIO
;
3265 ret
= fh
->target
->fileio_pwrite (fh
->target_fd
, write_buf
,
3266 len
, offset
, target_errno
);
3269 gdb_printf (gdb_stdlog
,
3270 "target_fileio_pwrite (%d,...,%d,%s) "
3272 fd
, len
, pulongest (offset
),
3273 ret
, ret
!= -1 ? 0 : *target_errno
);
3280 target_fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3281 ULONGEST offset
, fileio_error
*target_errno
)
3283 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3286 if (fh
->is_closed ())
3287 *target_errno
= FILEIO_EBADF
;
3288 else if (fh
->target
== NULL
)
3289 *target_errno
= FILEIO_EIO
;
3291 ret
= fh
->target
->fileio_pread (fh
->target_fd
, read_buf
,
3292 len
, offset
, target_errno
);
3295 gdb_printf (gdb_stdlog
,
3296 "target_fileio_pread (%d,...,%d,%s) "
3298 fd
, len
, pulongest (offset
),
3299 ret
, ret
!= -1 ? 0 : *target_errno
);
3306 target_fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
)
3308 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3311 if (fh
->is_closed ())
3312 *target_errno
= FILEIO_EBADF
;
3313 else if (fh
->target
== NULL
)
3314 *target_errno
= FILEIO_EIO
;
3316 ret
= fh
->target
->fileio_fstat (fh
->target_fd
, sb
, target_errno
);
3319 gdb_printf (gdb_stdlog
,
3320 "target_fileio_fstat (%d) = %d (%d)\n",
3321 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3328 target_fileio_close (int fd
, fileio_error
*target_errno
)
3330 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3333 if (fh
->is_closed ())
3334 *target_errno
= FILEIO_EBADF
;
3337 if (fh
->target
!= NULL
)
3338 ret
= fh
->target
->fileio_close (fh
->target_fd
,
3342 release_fileio_fd (fd
, fh
);
3346 gdb_printf (gdb_stdlog
,
3347 "target_fileio_close (%d) = %d (%d)\n",
3348 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3355 target_fileio_unlink (struct inferior
*inf
, const char *filename
,
3356 fileio_error
*target_errno
)
3358 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3360 int ret
= t
->fileio_unlink (inf
, filename
, target_errno
);
3362 if (ret
== -1 && *target_errno
== FILEIO_ENOSYS
)
3366 gdb_printf (gdb_stdlog
,
3367 "target_fileio_unlink (%d,%s)"
3369 inf
== NULL
? 0 : inf
->num
, filename
,
3370 ret
, ret
!= -1 ? 0 : *target_errno
);
3374 *target_errno
= FILEIO_ENOSYS
;
3380 std::optional
<std::string
>
3381 target_fileio_readlink (struct inferior
*inf
, const char *filename
,
3382 fileio_error
*target_errno
)
3384 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3386 std::optional
<std::string
> ret
3387 = t
->fileio_readlink (inf
, filename
, target_errno
);
3389 if (!ret
.has_value () && *target_errno
== FILEIO_ENOSYS
)
3393 gdb_printf (gdb_stdlog
,
3394 "target_fileio_readlink (%d,%s)"
3396 inf
== NULL
? 0 : inf
->num
,
3397 filename
, ret
? ret
->c_str () : "(nil)",
3398 ret
? 0 : *target_errno
);
3402 *target_errno
= FILEIO_ENOSYS
;
3406 /* Like scoped_fd, but specific to target fileio. */
3408 class scoped_target_fd
3411 explicit scoped_target_fd (int fd
) noexcept
3416 ~scoped_target_fd ()
3420 fileio_error target_errno
;
3422 target_fileio_close (m_fd
, &target_errno
);
3426 DISABLE_COPY_AND_ASSIGN (scoped_target_fd
);
3428 int get () const noexcept
3437 /* Read target file FILENAME, in the filesystem as seen by INF. If
3438 INF is NULL, use the filesystem seen by the debugger (GDB or, for
3439 remote targets, the remote stub). Store the result in *BUF_P and
3440 return the size of the transferred data. PADDING additional bytes
3441 are available in *BUF_P. This is a helper function for
3442 target_fileio_read_alloc; see the declaration of that function for
3443 more information. */
3446 target_fileio_read_alloc_1 (struct inferior
*inf
, const char *filename
,
3447 gdb_byte
**buf_p
, int padding
)
3449 size_t buf_alloc
, buf_pos
;
3452 fileio_error target_errno
;
3454 scoped_target_fd
fd (target_fileio_open (inf
, filename
, FILEIO_O_RDONLY
,
3455 0700, false, &target_errno
));
3456 if (fd
.get () == -1)
3459 /* Start by reading up to 4K at a time. The target will throttle
3460 this number down if necessary. */
3462 buf
= (gdb_byte
*) xmalloc (buf_alloc
);
3466 n
= target_fileio_pread (fd
.get (), &buf
[buf_pos
],
3467 buf_alloc
- buf_pos
- padding
, buf_pos
,
3471 /* An error occurred. */
3477 /* Read all there was. */
3487 /* If the buffer is filling up, expand it. */
3488 if (buf_alloc
< buf_pos
* 2)
3491 buf
= (gdb_byte
*) xrealloc (buf
, buf_alloc
);
3501 target_fileio_read_alloc (struct inferior
*inf
, const char *filename
,
3504 return target_fileio_read_alloc_1 (inf
, filename
, buf_p
, 0);
3509 gdb::unique_xmalloc_ptr
<char>
3510 target_fileio_read_stralloc (struct inferior
*inf
, const char *filename
)
3514 LONGEST i
, transferred
;
3516 transferred
= target_fileio_read_alloc_1 (inf
, filename
, &buffer
, 1);
3517 bufstr
= (char *) buffer
;
3519 if (transferred
< 0)
3520 return gdb::unique_xmalloc_ptr
<char> (nullptr);
3522 if (transferred
== 0)
3523 return make_unique_xstrdup ("");
3525 bufstr
[transferred
] = 0;
3527 /* Check for embedded NUL bytes; but allow trailing NULs. */
3528 for (i
= strlen (bufstr
); i
< transferred
; i
++)
3531 warning (_("target file %s "
3532 "contained unexpected null characters"),
3537 return gdb::unique_xmalloc_ptr
<char> (bufstr
);
3542 default_region_ok_for_hw_watchpoint (struct target_ops
*self
,
3543 CORE_ADDR addr
, int len
)
3545 gdbarch
*arch
= current_inferior ()->arch ();
3546 return (len
<= gdbarch_ptr_bit (arch
) / TARGET_CHAR_BIT
);
3550 default_watchpoint_addr_within_range (struct target_ops
*target
,
3552 CORE_ADDR start
, int length
)
3554 return addr
>= start
&& addr
< start
+ length
;
3560 target_stack::find_beneath (const target_ops
*t
) const
3562 /* Look for a non-empty slot at stratum levels beneath T's. */
3563 for (int stratum
= t
->stratum () - 1; stratum
>= 0; --stratum
)
3564 if (m_stack
[stratum
].get () != NULL
)
3565 return m_stack
[stratum
].get ();
3573 find_target_at (enum strata stratum
)
3575 return current_inferior ()->target_at (stratum
);
3583 target_announce_detach (int from_tty
)
3586 const char *exec_file
;
3591 pid
= inferior_ptid
.pid ();
3592 exec_file
= get_exec_file (0);
3593 if (exec_file
== nullptr)
3594 gdb_printf ("Detaching from pid %s\n",
3595 target_pid_to_str (ptid_t (pid
)).c_str ());
3597 gdb_printf (_("Detaching from program: %s, %s\n"), exec_file
,
3598 target_pid_to_str (ptid_t (pid
)).c_str ());
3604 target_announce_attach (int from_tty
, int pid
)
3609 const char *exec_file
= get_exec_file (0);
3611 if (exec_file
!= nullptr)
3612 gdb_printf ("Attaching to program: %s, %s\n", exec_file
,
3613 target_pid_to_str (ptid_t (pid
)).c_str ());
3615 gdb_printf ("Attaching to %s\n",
3616 target_pid_to_str (ptid_t (pid
)).c_str ());
3619 /* The inferior process has died. Long live the inferior! */
3622 generic_mourn_inferior (void)
3624 inferior
*inf
= current_inferior ();
3626 switch_to_no_thread ();
3628 /* Mark breakpoints uninserted in case something tries to delete a
3629 breakpoint while we delete the inferior's threads (which would
3630 fail, since the inferior is long gone). */
3631 mark_breakpoints_out (inf
->pspace
);
3634 exit_inferior (inf
);
3636 /* Note this wipes step-resume breakpoints, so needs to be done
3637 after exit_inferior, which ends up referencing the step-resume
3638 breakpoints through clear_thread_inferior_resources. */
3639 breakpoint_init_inferior (inf
, inf_exited
);
3641 registers_changed ();
3643 reopen_exec_file ();
3644 reinit_frame_cache ();
3646 if (deprecated_detach_hook
)
3647 deprecated_detach_hook ();
3650 /* Convert a normal process ID to a string. Returns the string in a
3654 normal_pid_to_str (ptid_t ptid
)
3656 return string_printf ("process %d", ptid
.pid ());
3660 default_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3662 return normal_pid_to_str (ptid
);
3665 /* Error-catcher for target_find_memory_regions. */
3667 dummy_find_memory_regions (struct target_ops
*self
,
3668 find_memory_region_ftype ignore1
, void *ignore2
)
3670 error (_("Command not implemented for this target."));
3674 /* Error-catcher for target_make_corefile_notes. */
3675 static gdb::unique_xmalloc_ptr
<char>
3676 dummy_make_corefile_notes (struct target_ops
*self
,
3677 bfd
*ignore1
, int *ignore2
)
3679 error (_("Command not implemented for this target."));
3683 #include "target-delegates.c"
3685 /* The initial current target, so that there is always a semi-valid
3688 static dummy_target the_dummy_target
;
3695 return &the_dummy_target
;
3698 static const target_info dummy_target_info
= {
3705 dummy_target::stratum () const
3707 return dummy_stratum
;
3711 debug_target::stratum () const
3713 return debug_stratum
;
3717 dummy_target::info () const
3719 return dummy_target_info
;
3723 debug_target::info () const
3725 return beneath ()->info ();
3731 target_thread_alive (ptid_t ptid
)
3733 return current_inferior ()->top_target ()->thread_alive (ptid
);
3737 target_update_thread_list (void)
3739 current_inferior ()->top_target ()->update_thread_list ();
3743 target_stop (ptid_t ptid
)
3745 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
3747 gdb_assert (!proc_target
->commit_resumed_state
);
3751 warning (_("May not interrupt or stop the target, ignoring attempt"));
3755 current_inferior ()->top_target ()->stop (ptid
);
3763 warning (_("May not interrupt or stop the target, ignoring attempt"));
3767 current_inferior ()->top_target ()->interrupt ();
3773 target_pass_ctrlc (void)
3775 /* Pass the Ctrl-C to the first target that has a thread
3777 for (inferior
*inf
: all_inferiors ())
3779 target_ops
*proc_target
= inf
->process_target ();
3780 if (proc_target
== NULL
)
3783 for (thread_info
*thr
: inf
->non_exited_threads ())
3785 /* A thread can be THREAD_STOPPED and executing, while
3786 running an infcall. */
3787 if (thr
->state
== THREAD_RUNNING
|| thr
->executing ())
3789 /* We can get here quite deep in target layers. Avoid
3790 switching thread context or anything that would
3791 communicate with the target (e.g., to fetch
3792 registers), or flushing e.g., the frame cache. We
3793 just switch inferior in order to be able to call
3794 through the target_stack. */
3795 scoped_restore_current_inferior restore_inferior
;
3796 set_current_inferior (inf
);
3797 current_inferior ()->top_target ()->pass_ctrlc ();
3807 default_target_pass_ctrlc (struct target_ops
*ops
)
3809 target_interrupt ();
3812 /* See target/target.h. */
3815 target_stop_and_wait (ptid_t ptid
)
3817 struct target_waitstatus status
;
3818 bool was_non_stop
= non_stop
;
3823 target_wait (ptid
, &status
, 0);
3825 non_stop
= was_non_stop
;
3828 /* See target/target.h. */
3831 target_continue_no_signal (ptid_t ptid
)
3833 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3836 /* See target/target.h. */
3839 target_continue (ptid_t ptid
, enum gdb_signal signal
)
3841 target_resume (ptid
, 0, signal
);
3844 /* Concatenate ELEM to LIST, a comma-separated list. */
3847 str_comma_list_concat_elem (std::string
*list
, const char *elem
)
3849 if (!list
->empty ())
3850 list
->append (", ");
3852 list
->append (elem
);
3855 /* Helper for target_options_to_string. If OPT is present in
3856 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3857 OPT is removed from TARGET_OPTIONS. */
3860 do_option (target_wait_flags
*target_options
, std::string
*ret
,
3861 target_wait_flag opt
, const char *opt_str
)
3863 if ((*target_options
& opt
) != 0)
3865 str_comma_list_concat_elem (ret
, opt_str
);
3866 *target_options
&= ~opt
;
3873 target_options_to_string (target_wait_flags target_options
)
3877 #define DO_TARG_OPTION(OPT) \
3878 do_option (&target_options, &ret, OPT, #OPT)
3880 DO_TARG_OPTION (TARGET_WNOHANG
);
3882 if (target_options
!= 0)
3883 str_comma_list_concat_elem (&ret
, "unknown???");
3889 target_fetch_registers (struct regcache
*regcache
, int regno
)
3891 current_inferior ()->top_target ()->fetch_registers (regcache
, regno
);
3893 regcache
->debug_print_register ("target_fetch_registers", regno
);
3897 target_store_registers (struct regcache
*regcache
, int regno
)
3899 if (!may_write_registers
)
3900 error (_("Writing to registers is not allowed (regno %d)"), regno
);
3902 current_inferior ()->top_target ()->store_registers (regcache
, regno
);
3905 regcache
->debug_print_register ("target_store_registers", regno
);
3910 target_core_of_thread (ptid_t ptid
)
3912 return current_inferior ()->top_target ()->core_of_thread (ptid
);
3916 simple_verify_memory (struct target_ops
*ops
,
3917 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
3919 LONGEST total_xfered
= 0;
3921 while (total_xfered
< size
)
3923 ULONGEST xfered_len
;
3924 enum target_xfer_status status
;
3926 ULONGEST howmuch
= std::min
<ULONGEST
> (sizeof (buf
), size
- total_xfered
);
3928 status
= target_xfer_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
3929 buf
, NULL
, lma
+ total_xfered
, howmuch
,
3931 if (status
== TARGET_XFER_OK
3932 && memcmp (data
+ total_xfered
, buf
, xfered_len
) == 0)
3934 total_xfered
+= xfered_len
;
3943 /* Default implementation of memory verification. */
3946 default_verify_memory (struct target_ops
*self
,
3947 const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3949 /* Start over from the top of the target stack. */
3950 return simple_verify_memory (current_inferior ()->top_target (),
3951 data
, memaddr
, size
);
3955 target_verify_memory (const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3957 target_ops
*target
= current_inferior ()->top_target ();
3959 return target
->verify_memory (data
, memaddr
, size
);
3962 /* The documentation for this function is in its prototype declaration in
3966 target_insert_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3967 enum target_hw_bp_type rw
)
3969 target_ops
*target
= current_inferior ()->top_target ();
3971 return target
->insert_mask_watchpoint (addr
, mask
, rw
);
3974 /* The documentation for this function is in its prototype declaration in
3978 target_remove_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3979 enum target_hw_bp_type rw
)
3981 target_ops
*target
= current_inferior ()->top_target ();
3983 return target
->remove_mask_watchpoint (addr
, mask
, rw
);
3986 /* The documentation for this function is in its prototype declaration
3990 target_masked_watch_num_registers (CORE_ADDR addr
, CORE_ADDR mask
)
3992 target_ops
*target
= current_inferior ()->top_target ();
3994 return target
->masked_watch_num_registers (addr
, mask
);
3997 /* The documentation for this function is in its prototype declaration
4001 target_ranged_break_num_registers (void)
4003 return current_inferior ()->top_target ()->ranged_break_num_registers ();
4008 struct btrace_target_info
*
4009 target_enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
)
4011 return current_inferior ()->top_target ()->enable_btrace (tp
, conf
);
4017 target_disable_btrace (struct btrace_target_info
*btinfo
)
4019 current_inferior ()->top_target ()->disable_btrace (btinfo
);
4025 target_teardown_btrace (struct btrace_target_info
*btinfo
)
4027 current_inferior ()->top_target ()->teardown_btrace (btinfo
);
4033 target_read_btrace (struct btrace_data
*btrace
,
4034 struct btrace_target_info
*btinfo
,
4035 enum btrace_read_type type
)
4037 target_ops
*target
= current_inferior ()->top_target ();
4039 return target
->read_btrace (btrace
, btinfo
, type
);
4044 const struct btrace_config
*
4045 target_btrace_conf (const struct btrace_target_info
*btinfo
)
4047 return current_inferior ()->top_target ()->btrace_conf (btinfo
);
4053 target_stop_recording (void)
4055 current_inferior ()->top_target ()->stop_recording ();
4061 target_save_record (const char *filename
)
4063 current_inferior ()->top_target ()->save_record (filename
);
4069 target_supports_delete_record ()
4071 return current_inferior ()->top_target ()->supports_delete_record ();
4077 target_delete_record (void)
4079 current_inferior ()->top_target ()->delete_record ();
4085 target_record_method (ptid_t ptid
)
4087 return current_inferior ()->top_target ()->record_method (ptid
);
4093 target_record_is_replaying (ptid_t ptid
)
4095 return current_inferior ()->top_target ()->record_is_replaying (ptid
);
4101 target_record_will_replay (ptid_t ptid
, int dir
)
4103 return current_inferior ()->top_target ()->record_will_replay (ptid
, dir
);
4109 target_record_stop_replaying (void)
4111 current_inferior ()->top_target ()->record_stop_replaying ();
4117 target_goto_record_begin (void)
4119 current_inferior ()->top_target ()->goto_record_begin ();
4125 target_goto_record_end (void)
4127 current_inferior ()->top_target ()->goto_record_end ();
4133 target_goto_record (ULONGEST insn
)
4135 current_inferior ()->top_target ()->goto_record (insn
);
4141 target_insn_history (int size
, gdb_disassembly_flags flags
)
4143 current_inferior ()->top_target ()->insn_history (size
, flags
);
4149 target_insn_history_from (ULONGEST from
, int size
,
4150 gdb_disassembly_flags flags
)
4152 current_inferior ()->top_target ()->insn_history_from (from
, size
, flags
);
4158 target_insn_history_range (ULONGEST begin
, ULONGEST end
,
4159 gdb_disassembly_flags flags
)
4161 current_inferior ()->top_target ()->insn_history_range (begin
, end
, flags
);
4167 target_call_history (int size
, record_print_flags flags
)
4169 current_inferior ()->top_target ()->call_history (size
, flags
);
4175 target_call_history_from (ULONGEST begin
, int size
, record_print_flags flags
)
4177 current_inferior ()->top_target ()->call_history_from (begin
, size
, flags
);
4183 target_call_history_range (ULONGEST begin
, ULONGEST end
, record_print_flags flags
)
4185 current_inferior ()->top_target ()->call_history_range (begin
, end
, flags
);
4190 const struct frame_unwind
*
4191 target_get_unwinder (void)
4193 return current_inferior ()->top_target ()->get_unwinder ();
4198 const struct frame_unwind
*
4199 target_get_tailcall_unwinder (void)
4201 return current_inferior ()->top_target ()->get_tailcall_unwinder ();
4207 target_prepare_to_generate_core (void)
4209 current_inferior ()->top_target ()->prepare_to_generate_core ();
4215 target_done_generating_core (void)
4217 current_inferior ()->top_target ()->done_generating_core ();
4222 static char targ_desc
[] =
4223 "Names of targets and files being debugged.\nShows the entire \
4224 stack of targets currently in use (including the exec-file,\n\
4225 core-file, and process, if any), as well as the symbol file name.";
4228 default_rcmd (struct target_ops
*self
, const char *command
,
4229 struct ui_file
*output
)
4231 error (_("\"monitor\" command not supported by this target."));
4235 do_monitor_command (const char *cmd
, int from_tty
)
4237 target_rcmd (cmd
, gdb_stdtarg
);
4240 /* Erases all the memory regions marked as flash. CMD and FROM_TTY are
4244 flash_erase_command (const char *cmd
, int from_tty
)
4246 /* Used to communicate termination of flash operations to the target. */
4247 bool found_flash_region
= false;
4248 gdbarch
*gdbarch
= current_inferior ()->arch ();
4250 std::vector
<mem_region
> mem_regions
= target_memory_map ();
4252 /* Iterate over all memory regions. */
4253 for (const mem_region
&m
: mem_regions
)
4255 /* Is this a flash memory region? */
4256 if (m
.attrib
.mode
== MEM_FLASH
)
4258 found_flash_region
= true;
4259 target_flash_erase (m
.lo
, m
.hi
- m
.lo
);
4261 ui_out_emit_tuple
tuple_emitter (current_uiout
, "erased-regions");
4263 current_uiout
->message (_("Erasing flash memory region at address "));
4264 current_uiout
->field_core_addr ("address", gdbarch
, m
.lo
);
4265 current_uiout
->message (", size = ");
4266 current_uiout
->field_string ("size", hex_string (m
.hi
- m
.lo
));
4267 current_uiout
->message ("\n");
4271 /* Did we do any flash operations? If so, we need to finalize them. */
4272 if (found_flash_region
)
4273 target_flash_done ();
4275 current_uiout
->message (_("No flash memory regions found.\n"));
4278 /* Print the name of each layers of our target stack. */
4281 maintenance_print_target_stack (const char *cmd
, int from_tty
)
4283 gdb_printf (_("The current target stack is:\n"));
4285 for (target_ops
*t
= current_inferior ()->top_target ();
4289 if (t
->stratum () == debug_stratum
)
4291 gdb_printf (" - %s (%s)\n", t
->shortname (), t
->longname ());
4298 target_async (bool enable
)
4300 /* If we are trying to enable async mode then it must be the case that
4301 async mode is possible for this target. */
4302 gdb_assert (!enable
|| target_can_async_p ());
4303 infrun_async (enable
);
4304 current_inferior ()->top_target ()->async (enable
);
4310 target_thread_events (int enable
)
4312 current_inferior ()->top_target ()->thread_events (enable
);
4318 target_supports_set_thread_options (gdb_thread_options options
)
4320 inferior
*inf
= current_inferior ();
4321 return inf
->top_target ()->supports_set_thread_options (options
);
4324 /* Controls if targets can report that they can/are async. This is
4325 just for maintainers to use when debugging gdb. */
4326 bool target_async_permitted
= true;
4329 set_maint_target_async (bool permitted
)
4331 if (have_live_inferiors ())
4332 error (_("Cannot change this setting while the inferior is running."));
4334 target_async_permitted
= permitted
;
4338 get_maint_target_async ()
4340 return target_async_permitted
;
4344 show_maint_target_async (ui_file
*file
, int from_tty
,
4345 cmd_list_element
*c
, const char *value
)
4348 _("Controlling the inferior in "
4349 "asynchronous mode is %s.\n"), value
);
4352 /* Return true if the target operates in non-stop mode even with "set
4356 target_always_non_stop_p (void)
4358 return current_inferior ()->top_target ()->always_non_stop_p ();
4364 target_is_non_stop_p ()
4367 || target_non_stop_enabled
== AUTO_BOOLEAN_TRUE
4368 || (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
4369 && target_always_non_stop_p ()))
4370 && target_can_async_p ());
4376 exists_non_stop_target ()
4378 if (target_is_non_stop_p ())
4381 scoped_restore_current_thread restore_thread
;
4383 for (inferior
*inf
: all_inferiors ())
4385 switch_to_inferior_no_thread (inf
);
4386 if (target_is_non_stop_p ())
4393 /* Controls if targets can report that they always run in non-stop
4394 mode. This is just for maintainers to use when debugging gdb. */
4395 enum auto_boolean target_non_stop_enabled
= AUTO_BOOLEAN_AUTO
;
4397 /* Set callback for maint target-non-stop setting. */
4400 set_maint_target_non_stop (auto_boolean enabled
)
4402 if (have_live_inferiors ())
4403 error (_("Cannot change this setting while the inferior is running."));
4405 target_non_stop_enabled
= enabled
;
4408 /* Get callback for maint target-non-stop setting. */
4411 get_maint_target_non_stop ()
4413 return target_non_stop_enabled
;
4417 show_maint_target_non_stop (ui_file
*file
, int from_tty
,
4418 cmd_list_element
*c
, const char *value
)
4420 if (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
)
4422 _("Whether the target is always in non-stop mode "
4423 "is %s (currently %s).\n"), value
,
4424 target_always_non_stop_p () ? "on" : "off");
4427 _("Whether the target is always in non-stop mode "
4428 "is %s.\n"), value
);
4431 /* Temporary copies of permission settings. */
4433 static bool may_write_registers_1
= true;
4434 static bool may_write_memory_1
= true;
4435 static bool may_insert_breakpoints_1
= true;
4436 static bool may_insert_tracepoints_1
= true;
4437 static bool may_insert_fast_tracepoints_1
= true;
4438 static bool may_stop_1
= true;
4440 /* Make the user-set values match the real values again. */
4443 update_target_permissions (void)
4445 may_write_registers_1
= may_write_registers
;
4446 may_write_memory_1
= may_write_memory
;
4447 may_insert_breakpoints_1
= may_insert_breakpoints
;
4448 may_insert_tracepoints_1
= may_insert_tracepoints
;
4449 may_insert_fast_tracepoints_1
= may_insert_fast_tracepoints
;
4450 may_stop_1
= may_stop
;
4453 /* The one function handles (most of) the permission flags in the same
4457 set_target_permissions (const char *args
, int from_tty
,
4458 struct cmd_list_element
*c
)
4460 if (target_has_execution ())
4462 update_target_permissions ();
4463 error (_("Cannot change this setting while the inferior is running."));
4466 /* Make the real values match the user-changed values. */
4467 may_insert_breakpoints
= may_insert_breakpoints_1
;
4468 may_insert_tracepoints
= may_insert_tracepoints_1
;
4469 may_insert_fast_tracepoints
= may_insert_fast_tracepoints_1
;
4470 may_stop
= may_stop_1
;
4471 update_observer_mode ();
4474 /* Set some permissions independently of observer mode. */
4477 set_write_memory_registers_permission (const char *args
, int from_tty
,
4478 struct cmd_list_element
*c
)
4480 /* Make the real values match the user-changed values. */
4481 may_write_memory
= may_write_memory_1
;
4482 may_write_registers
= may_write_registers_1
;
4483 update_observer_mode ();
4486 void _initialize_target ();
4489 _initialize_target ()
4491 the_debug_target
= new debug_target ();
4493 add_info ("target", info_target_command
, targ_desc
);
4494 add_info ("files", info_target_command
, targ_desc
);
4496 add_setshow_zuinteger_cmd ("target", class_maintenance
, &targetdebug
, _("\
4497 Set target debugging."), _("\
4498 Show target debugging."), _("\
4499 When non-zero, target debugging is enabled. Higher numbers are more\n\
4503 &setdebuglist
, &showdebuglist
);
4505 add_setshow_boolean_cmd ("trust-readonly-sections", class_support
,
4506 &trust_readonly
, _("\
4507 Set mode for reading from readonly sections."), _("\
4508 Show mode for reading from readonly sections."), _("\
4509 When this mode is on, memory reads from readonly sections (such as .text)\n\
4510 will be read from the object file instead of from the target. This will\n\
4511 result in significant performance improvement for remote targets."),
4513 show_trust_readonly
,
4514 &setlist
, &showlist
);
4516 add_com ("monitor", class_obscure
, do_monitor_command
,
4517 _("Send a command to the remote monitor (remote targets only)."));
4519 add_cmd ("target-stack", class_maintenance
, maintenance_print_target_stack
,
4520 _("Print the name of each layer of the internal target stack."),
4521 &maintenanceprintlist
);
4523 add_setshow_boolean_cmd ("target-async", no_class
,
4525 Set whether gdb controls the inferior in asynchronous mode."), _("\
4526 Show whether gdb controls the inferior in asynchronous mode."), _("\
4527 Tells gdb whether to control the inferior in asynchronous mode."),
4528 set_maint_target_async
,
4529 get_maint_target_async
,
4530 show_maint_target_async
,
4531 &maintenance_set_cmdlist
,
4532 &maintenance_show_cmdlist
);
4534 add_setshow_auto_boolean_cmd ("target-non-stop", no_class
,
4536 Set whether gdb always controls the inferior in non-stop mode."), _("\
4537 Show whether gdb always controls the inferior in non-stop mode."), _("\
4538 Tells gdb whether to control the inferior in non-stop mode."),
4539 set_maint_target_non_stop
,
4540 get_maint_target_non_stop
,
4541 show_maint_target_non_stop
,
4542 &maintenance_set_cmdlist
,
4543 &maintenance_show_cmdlist
);
4545 add_setshow_boolean_cmd ("may-write-registers", class_support
,
4546 &may_write_registers_1
, _("\
4547 Set permission to write into registers."), _("\
4548 Show permission to write into registers."), _("\
4549 When this permission is on, GDB may write into the target's registers.\n\
4550 Otherwise, any sort of write attempt will result in an error."),
4551 set_write_memory_registers_permission
, NULL
,
4552 &setlist
, &showlist
);
4554 add_setshow_boolean_cmd ("may-write-memory", class_support
,
4555 &may_write_memory_1
, _("\
4556 Set permission to write into target memory."), _("\
4557 Show permission to write into target memory."), _("\
4558 When this permission is on, GDB may write into the target's memory.\n\
4559 Otherwise, any sort of write attempt will result in an error."),
4560 set_write_memory_registers_permission
, NULL
,
4561 &setlist
, &showlist
);
4563 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support
,
4564 &may_insert_breakpoints_1
, _("\
4565 Set permission to insert breakpoints in the target."), _("\
4566 Show permission to insert breakpoints in the target."), _("\
4567 When this permission is on, GDB may insert breakpoints in the program.\n\
4568 Otherwise, any sort of insertion attempt will result in an error."),
4569 set_target_permissions
, NULL
,
4570 &setlist
, &showlist
);
4572 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support
,
4573 &may_insert_tracepoints_1
, _("\
4574 Set permission to insert tracepoints in the target."), _("\
4575 Show permission to insert tracepoints in the target."), _("\
4576 When this permission is on, GDB may insert tracepoints in the program.\n\
4577 Otherwise, any sort of insertion attempt will result in an error."),
4578 set_target_permissions
, NULL
,
4579 &setlist
, &showlist
);
4581 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support
,
4582 &may_insert_fast_tracepoints_1
, _("\
4583 Set permission to insert fast tracepoints in the target."), _("\
4584 Show permission to insert fast tracepoints in the target."), _("\
4585 When this permission is on, GDB may insert fast tracepoints.\n\
4586 Otherwise, any sort of insertion attempt will result in an error."),
4587 set_target_permissions
, NULL
,
4588 &setlist
, &showlist
);
4590 add_setshow_boolean_cmd ("may-interrupt", class_support
,
4592 Set permission to interrupt or signal the target."), _("\
4593 Show permission to interrupt or signal the target."), _("\
4594 When this permission is on, GDB may interrupt/stop the target's execution.\n\
4595 Otherwise, any attempt to interrupt or stop will be ignored."),
4596 set_target_permissions
, NULL
,
4597 &setlist
, &showlist
);
4599 add_com ("flash-erase", no_class
, flash_erase_command
,
4600 _("Erase all flash memory regions."));
4602 add_setshow_boolean_cmd ("auto-connect-native-target", class_support
,
4603 &auto_connect_native_target
, _("\
4604 Set whether GDB may automatically connect to the native target."), _("\
4605 Show whether GDB may automatically connect to the native target."), _("\
4606 When on, and GDB is not connected to a target yet, GDB\n\
4607 attempts \"run\" and other commands with the native target."),
4608 NULL
, show_auto_connect_native_target
,
4609 &setlist
, &showlist
);