1 /* Select target systems and architectures at runtime for GDB.
3 Copyright (C) 1990-2021 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"
36 #include "target-descriptions.h"
37 #include "gdbthread.h"
40 #include "inline-frame.h"
41 #include "tracepoint.h"
42 #include "gdb/fileio.h"
43 #include "gdbsupport/agent.h"
45 #include "target-debug.h"
47 #include "event-top.h"
49 #include "gdbsupport/byte-vector.h"
50 #include "gdbsupport/search.h"
52 #include <unordered_map>
53 #include "target-connection.h"
56 static void generic_tls_error (void) ATTRIBUTE_NORETURN
;
58 static void default_terminal_info (struct target_ops
*, const char *, int);
60 static int default_watchpoint_addr_within_range (struct target_ops
*,
61 CORE_ADDR
, CORE_ADDR
, int);
63 static int default_region_ok_for_hw_watchpoint (struct target_ops
*,
66 static void default_rcmd (struct target_ops
*, const char *, struct ui_file
*);
68 static ptid_t
default_get_ada_task_ptid (struct target_ops
*self
,
71 static void default_mourn_inferior (struct target_ops
*self
);
73 static int default_search_memory (struct target_ops
*ops
,
75 ULONGEST search_space_len
,
76 const gdb_byte
*pattern
,
78 CORE_ADDR
*found_addrp
);
80 static int default_verify_memory (struct target_ops
*self
,
82 CORE_ADDR memaddr
, ULONGEST size
);
84 static void tcomplain (void) ATTRIBUTE_NORETURN
;
86 static struct target_ops
*find_default_run_target (const char *);
88 static int dummy_find_memory_regions (struct target_ops
*self
,
89 find_memory_region_ftype ignore1
,
92 static gdb::unique_xmalloc_ptr
<char> dummy_make_corefile_notes
93 (struct target_ops
*self
, bfd
*ignore1
, int *ignore2
);
95 static std::string
default_pid_to_str (struct target_ops
*ops
, ptid_t ptid
);
97 static enum exec_direction_kind default_execution_direction
98 (struct target_ops
*self
);
100 /* Mapping between target_info objects (which have address identity)
101 and corresponding open/factory function/callback. Each add_target
102 call adds one entry to this map, and registers a "target
103 TARGET_NAME" command that when invoked calls the factory registered
104 here. The target_info object is associated with the command via
105 the command's context. */
106 static std::unordered_map
<const target_info
*, target_open_ftype
*>
109 /* The singleton debug target. */
111 static struct target_ops
*the_debug_target
;
113 /* Command list for target. */
115 static struct cmd_list_element
*targetlist
= NULL
;
117 /* True if we should trust readonly sections from the
118 executable when reading memory. */
120 static bool trust_readonly
= false;
122 /* Nonzero if we should show true memory content including
123 memory breakpoint inserted by gdb. */
125 static int show_memory_breakpoints
= 0;
127 /* These globals control whether GDB attempts to perform these
128 operations; they are useful for targets that need to prevent
129 inadvertent disruption, such as in non-stop mode. */
131 bool may_write_registers
= true;
133 bool may_write_memory
= true;
135 bool may_insert_breakpoints
= true;
137 bool may_insert_tracepoints
= true;
139 bool may_insert_fast_tracepoints
= true;
141 bool may_stop
= true;
143 /* Non-zero if we want to see trace of target level stuff. */
145 static unsigned int targetdebug
= 0;
148 set_targetdebug (const char *args
, int from_tty
, struct cmd_list_element
*c
)
151 current_inferior ()->push_target (the_debug_target
);
153 current_inferior ()->unpush_target (the_debug_target
);
157 show_targetdebug (struct ui_file
*file
, int from_tty
,
158 struct cmd_list_element
*c
, const char *value
)
160 fprintf_filtered (file
, _("Target debugging is %s.\n"), value
);
166 for (target_ops
*t
= current_inferior ()->top_target ();
169 if (t
->has_memory ())
178 for (target_ops
*t
= current_inferior ()->top_target ();
188 target_has_registers ()
190 for (target_ops
*t
= current_inferior ()->top_target ();
193 if (t
->has_registers ())
200 target_has_execution (inferior
*inf
)
203 inf
= current_inferior ();
205 for (target_ops
*t
= inf
->top_target ();
207 t
= inf
->find_target_beneath (t
))
208 if (t
->has_execution (inf
))
217 return current_inferior ()->top_target ()->shortname ();
223 target_attach_no_wait ()
225 return current_inferior ()->top_target ()->attach_no_wait ();
231 target_post_attach (int pid
)
233 return current_inferior ()->top_target ()->post_attach (pid
);
239 target_prepare_to_store (regcache
*regcache
)
241 return current_inferior ()->top_target ()->prepare_to_store (regcache
);
247 target_supports_enable_disable_tracepoint ()
249 target_ops
*target
= current_inferior ()->top_target ();
251 return target
->supports_enable_disable_tracepoint ();
255 target_supports_string_tracing ()
257 return current_inferior ()->top_target ()->supports_string_tracing ();
263 target_supports_evaluation_of_breakpoint_conditions ()
265 target_ops
*target
= current_inferior ()->top_target ();
267 return target
->supports_evaluation_of_breakpoint_conditions ();
273 target_supports_dumpcore ()
275 return current_inferior ()->top_target ()->supports_dumpcore ();
281 target_dumpcore (const char *filename
)
283 return current_inferior ()->top_target ()->dumpcore (filename
);
289 target_can_run_breakpoint_commands ()
291 return current_inferior ()->top_target ()->can_run_breakpoint_commands ();
299 return current_inferior ()->top_target ()->files_info ();
305 target_post_startup_inferior (ptid_t ptid
)
307 return current_inferior ()->top_target ()->post_startup_inferior (ptid
);
313 target_insert_fork_catchpoint (int pid
)
315 return current_inferior ()->top_target ()->insert_fork_catchpoint (pid
);
321 target_remove_fork_catchpoint (int pid
)
323 return current_inferior ()->top_target ()->remove_fork_catchpoint (pid
);
329 target_insert_vfork_catchpoint (int pid
)
331 return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid
);
337 target_remove_vfork_catchpoint (int pid
)
339 return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid
);
345 target_insert_exec_catchpoint (int pid
)
347 return current_inferior ()->top_target ()->insert_exec_catchpoint (pid
);
353 target_remove_exec_catchpoint (int pid
)
355 return current_inferior ()->top_target ()->remove_exec_catchpoint (pid
);
361 target_set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
362 gdb::array_view
<const int> syscall_counts
)
364 target_ops
*target
= current_inferior ()->top_target ();
366 return target
->set_syscall_catchpoint (pid
, needed
, any_count
,
373 target_rcmd (const char *command
, struct ui_file
*outbuf
)
375 return current_inferior ()->top_target ()->rcmd (command
, outbuf
);
381 target_can_lock_scheduler ()
383 target_ops
*target
= current_inferior ()->top_target ();
385 return (target
->get_thread_control_capabilities ()& tc_schedlock
) != 0;
391 target_can_async_p ()
393 return current_inferior ()->top_target ()->can_async_p ();
401 return current_inferior ()->top_target ()->is_async_p ();
405 target_execution_direction ()
407 return current_inferior ()->top_target ()->execution_direction ();
413 target_extra_thread_info (thread_info
*tp
)
415 return current_inferior ()->top_target ()->extra_thread_info (tp
);
421 target_pid_to_exec_file (int pid
)
423 return current_inferior ()->top_target ()->pid_to_exec_file (pid
);
429 target_thread_architecture (ptid_t ptid
)
431 return current_inferior ()->top_target ()->thread_architecture (ptid
);
437 target_find_memory_regions (find_memory_region_ftype func
, void *data
)
439 return current_inferior ()->top_target ()->find_memory_regions (func
, data
);
444 gdb::unique_xmalloc_ptr
<char>
445 target_make_corefile_notes (bfd
*bfd
, int *size_p
)
447 return current_inferior ()->top_target ()->make_corefile_notes (bfd
, size_p
);
451 target_get_bookmark (const char *args
, int from_tty
)
453 return current_inferior ()->top_target ()->get_bookmark (args
, from_tty
);
457 target_goto_bookmark (const gdb_byte
*arg
, int from_tty
)
459 return current_inferior ()->top_target ()->goto_bookmark (arg
, from_tty
);
465 target_stopped_by_watchpoint ()
467 return current_inferior ()->top_target ()->stopped_by_watchpoint ();
473 target_stopped_by_sw_breakpoint ()
475 return current_inferior ()->top_target ()->stopped_by_sw_breakpoint ();
479 target_supports_stopped_by_sw_breakpoint ()
481 target_ops
*target
= current_inferior ()->top_target ();
483 return target
->supports_stopped_by_sw_breakpoint ();
487 target_stopped_by_hw_breakpoint ()
489 return current_inferior ()->top_target ()->stopped_by_hw_breakpoint ();
493 target_supports_stopped_by_hw_breakpoint ()
495 target_ops
*target
= current_inferior ()->top_target ();
497 return target
->supports_stopped_by_hw_breakpoint ();
503 target_have_steppable_watchpoint ()
505 return current_inferior ()->top_target ()->have_steppable_watchpoint ();
511 target_can_use_hardware_watchpoint (bptype type
, int cnt
, int othertype
)
513 target_ops
*target
= current_inferior ()->top_target ();
515 return target
->can_use_hw_breakpoint (type
, cnt
, othertype
);
521 target_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
523 target_ops
*target
= current_inferior ()->top_target ();
525 return target
->region_ok_for_hw_watchpoint (addr
, len
);
530 target_can_do_single_step ()
532 return current_inferior ()->top_target ()->can_do_single_step ();
538 target_insert_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
541 target_ops
*target
= current_inferior ()->top_target ();
543 return target
->insert_watchpoint (addr
, len
, type
, cond
);
549 target_remove_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
552 target_ops
*target
= current_inferior ()->top_target ();
554 return target
->remove_watchpoint (addr
, len
, type
, cond
);
560 target_insert_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
562 target_ops
*target
= current_inferior ()->top_target ();
564 return target
->insert_hw_breakpoint (gdbarch
, bp_tgt
);
570 target_remove_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
572 target_ops
*target
= current_inferior ()->top_target ();
574 return target
->remove_hw_breakpoint (gdbarch
, bp_tgt
);
580 target_can_accel_watchpoint_condition (CORE_ADDR addr
, int len
, int type
,
583 target_ops
*target
= current_inferior ()->top_target ();
585 return target
->can_accel_watchpoint_condition (addr
, len
, type
, cond
);
591 target_can_execute_reverse ()
593 return current_inferior ()->top_target ()->can_execute_reverse ();
597 target_get_ada_task_ptid (long lwp
, long tid
)
599 return current_inferior ()->top_target ()->get_ada_task_ptid (lwp
, tid
);
603 target_filesystem_is_local ()
605 return current_inferior ()->top_target ()->filesystem_is_local ();
611 return current_inferior ()->top_target ()->trace_init ();
615 target_download_tracepoint (bp_location
*location
)
617 return current_inferior ()->top_target ()->download_tracepoint (location
);
621 target_can_download_tracepoint ()
623 return current_inferior ()->top_target ()->can_download_tracepoint ();
627 target_download_trace_state_variable (const trace_state_variable
&tsv
)
629 target_ops
*target
= current_inferior ()->top_target ();
631 return target
->download_trace_state_variable (tsv
);
635 target_enable_tracepoint (bp_location
*loc
)
637 return current_inferior ()->top_target ()->enable_tracepoint (loc
);
641 target_disable_tracepoint (bp_location
*loc
)
643 return current_inferior ()->top_target ()->disable_tracepoint (loc
);
647 target_trace_start ()
649 return current_inferior ()->top_target ()->trace_start ();
653 target_trace_set_readonly_regions ()
655 return current_inferior ()->top_target ()->trace_set_readonly_regions ();
659 target_get_trace_status (trace_status
*ts
)
661 return current_inferior ()->top_target ()->get_trace_status (ts
);
665 target_get_tracepoint_status (breakpoint
*tp
, uploaded_tp
*utp
)
667 return current_inferior ()->top_target ()->get_tracepoint_status (tp
, utp
);
673 return current_inferior ()->top_target ()->trace_stop ();
677 target_trace_find (trace_find_type type
, int num
,
678 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
680 target_ops
*target
= current_inferior ()->top_target ();
682 return target
->trace_find (type
, num
, addr1
, addr2
, tpp
);
686 target_get_trace_state_variable_value (int tsv
, LONGEST
*val
)
688 target_ops
*target
= current_inferior ()->top_target ();
690 return target
->get_trace_state_variable_value (tsv
, val
);
694 target_save_trace_data (const char *filename
)
696 return current_inferior ()->top_target ()->save_trace_data (filename
);
700 target_upload_tracepoints (uploaded_tp
**utpp
)
702 return current_inferior ()->top_target ()->upload_tracepoints (utpp
);
706 target_upload_trace_state_variables (uploaded_tsv
**utsvp
)
708 target_ops
*target
= current_inferior ()->top_target ();
710 return target
->upload_trace_state_variables (utsvp
);
714 target_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
716 target_ops
*target
= current_inferior ()->top_target ();
718 return target
->get_raw_trace_data (buf
, offset
, len
);
722 target_get_min_fast_tracepoint_insn_len ()
724 target_ops
*target
= current_inferior ()->top_target ();
726 return target
->get_min_fast_tracepoint_insn_len ();
730 target_set_disconnected_tracing (int val
)
732 return current_inferior ()->top_target ()->set_disconnected_tracing (val
);
736 target_set_circular_trace_buffer (int val
)
738 return current_inferior ()->top_target ()->set_circular_trace_buffer (val
);
742 target_set_trace_buffer_size (LONGEST val
)
744 return current_inferior ()->top_target ()->set_trace_buffer_size (val
);
748 target_set_trace_notes (const char *user
, const char *notes
,
749 const char *stopnotes
)
751 target_ops
*target
= current_inferior ()->top_target ();
753 return target
->set_trace_notes (user
, notes
, stopnotes
);
757 target_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
759 return current_inferior ()->top_target ()->get_tib_address (ptid
, addr
);
763 target_set_permissions ()
765 return current_inferior ()->top_target ()->set_permissions ();
769 target_static_tracepoint_marker_at (CORE_ADDR addr
,
770 static_tracepoint_marker
*marker
)
772 target_ops
*target
= current_inferior ()->top_target ();
774 return target
->static_tracepoint_marker_at (addr
, marker
);
777 std::vector
<static_tracepoint_marker
>
778 target_static_tracepoint_markers_by_strid (const char *marker_id
)
780 target_ops
*target
= current_inferior ()->top_target ();
782 return target
->static_tracepoint_markers_by_strid (marker_id
);
786 target_traceframe_info ()
788 return current_inferior ()->top_target ()->traceframe_info ();
792 target_use_agent (bool use
)
794 return current_inferior ()->top_target ()->use_agent (use
);
798 target_can_use_agent ()
800 return current_inferior ()->top_target ()->can_use_agent ();
804 target_augmented_libraries_svr4_read ()
806 return current_inferior ()->top_target ()->augmented_libraries_svr4_read ();
810 target_supports_memory_tagging ()
812 return current_inferior ()->top_target ()->supports_memory_tagging ();
816 target_fetch_memtags (CORE_ADDR address
, size_t len
, gdb::byte_vector
&tags
,
819 return current_inferior ()->top_target ()->fetch_memtags (address
, len
, tags
, type
);
823 target_store_memtags (CORE_ADDR address
, size_t len
,
824 const gdb::byte_vector
&tags
, int type
)
826 return current_inferior ()->top_target ()->store_memtags (address
, len
, tags
, type
);
830 target_log_command (const char *p
)
832 return current_inferior ()->top_target ()->log_command (p
);
835 /* This is used to implement the various target commands. */
838 open_target (const char *args
, int from_tty
, struct cmd_list_element
*command
)
840 auto *ti
= static_cast<target_info
*> (get_cmd_context (command
));
841 target_open_ftype
*func
= target_factories
[ti
];
844 fprintf_unfiltered (gdb_stdlog
, "-> %s->open (...)\n",
847 func (args
, from_tty
);
850 fprintf_unfiltered (gdb_stdlog
, "<- %s->open (%s, %d)\n",
851 ti
->shortname
, args
, from_tty
);
857 add_target (const target_info
&t
, target_open_ftype
*func
,
858 completer_ftype
*completer
)
860 struct cmd_list_element
*c
;
862 auto &func_slot
= target_factories
[&t
];
863 if (func_slot
!= nullptr)
864 internal_error (__FILE__
, __LINE__
,
865 _("target already added (\"%s\")."), t
.shortname
);
868 if (targetlist
== NULL
)
869 add_basic_prefix_cmd ("target", class_run
, _("\
870 Connect to a target machine or process.\n\
871 The first argument is the type or protocol of the target machine.\n\
872 Remaining arguments are interpreted by the target protocol. For more\n\
873 information on the arguments for a particular protocol, type\n\
874 `help target ' followed by the protocol name."),
875 &targetlist
, "target ", 0, &cmdlist
);
876 c
= add_cmd (t
.shortname
, no_class
, t
.doc
, &targetlist
);
877 set_cmd_context (c
, (void *) &t
);
878 set_cmd_sfunc (c
, open_target
);
879 if (completer
!= NULL
)
880 set_cmd_completer (c
, completer
);
886 add_deprecated_target_alias (const target_info
&tinfo
, const char *alias
)
888 struct cmd_list_element
*c
;
891 /* If we use add_alias_cmd, here, we do not get the deprecated warning,
893 c
= add_cmd (alias
, no_class
, tinfo
.doc
, &targetlist
);
894 set_cmd_sfunc (c
, open_target
);
895 set_cmd_context (c
, (void *) &tinfo
);
896 alt
= xstrprintf ("target %s", tinfo
.shortname
);
897 deprecate_cmd (c
, alt
);
905 current_inferior ()->top_target ()->kill ();
909 target_load (const char *arg
, int from_tty
)
911 target_dcache_invalidate ();
912 current_inferior ()->top_target ()->load (arg
, from_tty
);
917 target_terminal_state
target_terminal::m_terminal_state
918 = target_terminal_state::is_ours
;
920 /* See target/target.h. */
923 target_terminal::init (void)
925 current_inferior ()->top_target ()->terminal_init ();
927 m_terminal_state
= target_terminal_state::is_ours
;
930 /* See target/target.h. */
933 target_terminal::inferior (void)
935 struct ui
*ui
= current_ui
;
937 /* A background resume (``run&'') should leave GDB in control of the
939 if (ui
->prompt_state
!= PROMPT_BLOCKED
)
942 /* Since we always run the inferior in the main console (unless "set
943 inferior-tty" is in effect), when some UI other than the main one
944 calls target_terminal::inferior, then we leave the main UI's
945 terminal settings as is. */
949 /* If GDB is resuming the inferior in the foreground, install
950 inferior's terminal modes. */
952 struct inferior
*inf
= current_inferior ();
954 if (inf
->terminal_state
!= target_terminal_state::is_inferior
)
956 current_inferior ()->top_target ()->terminal_inferior ();
957 inf
->terminal_state
= target_terminal_state::is_inferior
;
960 m_terminal_state
= target_terminal_state::is_inferior
;
962 /* If the user hit C-c before, pretend that it was hit right
964 if (check_quit_flag ())
965 target_pass_ctrlc ();
968 /* See target/target.h. */
971 target_terminal::restore_inferior (void)
973 struct ui
*ui
= current_ui
;
975 /* See target_terminal::inferior(). */
976 if (ui
->prompt_state
!= PROMPT_BLOCKED
|| ui
!= main_ui
)
979 /* Restore the terminal settings of inferiors that were in the
980 foreground but are now ours_for_output due to a temporary
981 target_target::ours_for_output() call. */
984 scoped_restore_current_inferior restore_inferior
;
986 for (::inferior
*inf
: all_inferiors ())
988 if (inf
->terminal_state
== target_terminal_state::is_ours_for_output
)
990 set_current_inferior (inf
);
991 current_inferior ()->top_target ()->terminal_inferior ();
992 inf
->terminal_state
= target_terminal_state::is_inferior
;
997 m_terminal_state
= target_terminal_state::is_inferior
;
999 /* If the user hit C-c before, pretend that it was hit right
1001 if (check_quit_flag ())
1002 target_pass_ctrlc ();
1005 /* Switch terminal state to DESIRED_STATE, either is_ours, or
1006 is_ours_for_output. */
1009 target_terminal_is_ours_kind (target_terminal_state desired_state
)
1011 scoped_restore_current_inferior restore_inferior
;
1013 /* Must do this in two passes. First, have all inferiors save the
1014 current terminal settings. Then, after all inferiors have add a
1015 chance to safely save the terminal settings, restore GDB's
1016 terminal settings. */
1018 for (inferior
*inf
: all_inferiors ())
1020 if (inf
->terminal_state
== target_terminal_state::is_inferior
)
1022 set_current_inferior (inf
);
1023 current_inferior ()->top_target ()->terminal_save_inferior ();
1027 for (inferior
*inf
: all_inferiors ())
1029 /* Note we don't check is_inferior here like above because we
1030 need to handle 'is_ours_for_output -> is_ours' too. Careful
1031 to never transition from 'is_ours' to 'is_ours_for_output',
1033 if (inf
->terminal_state
!= target_terminal_state::is_ours
1034 && inf
->terminal_state
!= desired_state
)
1036 set_current_inferior (inf
);
1037 if (desired_state
== target_terminal_state::is_ours
)
1038 current_inferior ()->top_target ()->terminal_ours ();
1039 else if (desired_state
== target_terminal_state::is_ours_for_output
)
1040 current_inferior ()->top_target ()->terminal_ours_for_output ();
1042 gdb_assert_not_reached ("unhandled desired state");
1043 inf
->terminal_state
= desired_state
;
1048 /* See target/target.h. */
1051 target_terminal::ours ()
1053 struct ui
*ui
= current_ui
;
1055 /* See target_terminal::inferior. */
1059 if (m_terminal_state
== target_terminal_state::is_ours
)
1062 target_terminal_is_ours_kind (target_terminal_state::is_ours
);
1063 m_terminal_state
= target_terminal_state::is_ours
;
1066 /* See target/target.h. */
1069 target_terminal::ours_for_output ()
1071 struct ui
*ui
= current_ui
;
1073 /* See target_terminal::inferior. */
1077 if (!target_terminal::is_inferior ())
1080 target_terminal_is_ours_kind (target_terminal_state::is_ours_for_output
);
1081 target_terminal::m_terminal_state
= target_terminal_state::is_ours_for_output
;
1084 /* See target/target.h. */
1087 target_terminal::info (const char *arg
, int from_tty
)
1089 current_inferior ()->top_target ()->terminal_info (arg
, from_tty
);
1095 target_supports_terminal_ours (void)
1097 /* The current top target is the target at the top of the target
1098 stack of the current inferior. While normally there's always an
1099 inferior, we must check for nullptr here because we can get here
1100 very early during startup, before the initial inferior is first
1102 inferior
*inf
= current_inferior ();
1106 return inf
->top_target ()->supports_terminal_ours ();
1112 error (_("You can't do that when your target is `%s'"),
1113 current_inferior ()->top_target ()->shortname ());
1119 error (_("You can't do that without a process to debug."));
1123 default_terminal_info (struct target_ops
*self
, const char *args
, int from_tty
)
1125 printf_unfiltered (_("No saved terminal information.\n"));
1128 /* A default implementation for the to_get_ada_task_ptid target method.
1130 This function builds the PTID by using both LWP and TID as part of
1131 the PTID lwp and tid elements. The pid used is the pid of the
1135 default_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long tid
)
1137 return ptid_t (inferior_ptid
.pid (), lwp
, tid
);
1140 static enum exec_direction_kind
1141 default_execution_direction (struct target_ops
*self
)
1143 if (!target_can_execute_reverse ())
1144 return EXEC_FORWARD
;
1145 else if (!target_can_async_p ())
1146 return EXEC_FORWARD
;
1148 gdb_assert_not_reached ("\
1149 to_execution_direction must be implemented for reverse async");
1155 decref_target (target_ops
*t
)
1158 if (t
->refcount () == 0)
1160 if (t
->stratum () == process_stratum
)
1161 connection_list_remove (as_process_stratum_target (t
));
1169 target_stack::push (target_ops
*t
)
1173 strata stratum
= t
->stratum ();
1175 if (stratum
== process_stratum
)
1176 connection_list_add (as_process_stratum_target (t
));
1178 /* If there's already a target at this stratum, remove it. */
1180 if (m_stack
[stratum
] != NULL
)
1181 unpush (m_stack
[stratum
]);
1183 /* Now add the new one. */
1184 m_stack
[stratum
] = t
;
1186 if (m_top
< stratum
)
1193 target_stack::unpush (target_ops
*t
)
1195 gdb_assert (t
!= NULL
);
1197 strata stratum
= t
->stratum ();
1199 if (stratum
== dummy_stratum
)
1200 internal_error (__FILE__
, __LINE__
,
1201 _("Attempt to unpush the dummy target"));
1203 /* Look for the specified target. Note that a target can only occur
1204 once in the target stack. */
1206 if (m_stack
[stratum
] != t
)
1208 /* If T wasn't pushed, quit. Only open targets should be
1213 /* Unchain the target. */
1214 m_stack
[stratum
] = NULL
;
1216 if (m_top
== stratum
)
1217 m_top
= t
->beneath ()->stratum ();
1219 /* Finally close the target, if there are no inferiors
1220 referencing this target still. Note we do this after unchaining,
1221 so any target method calls from within the target_close
1222 implementation don't end up in T anymore. Do leave the target
1223 open if we have are other inferiors referencing this target
1230 /* Unpush TARGET and assert that it worked. */
1233 unpush_target_and_assert (struct target_ops
*target
)
1235 if (!current_inferior ()->unpush_target (target
))
1237 fprintf_unfiltered (gdb_stderr
,
1238 "pop_all_targets couldn't find target %s\n",
1239 target
->shortname ());
1240 internal_error (__FILE__
, __LINE__
,
1241 _("failed internal consistency check"));
1246 pop_all_targets_above (enum strata above_stratum
)
1248 while ((int) (current_inferior ()->top_target ()->stratum ())
1249 > (int) above_stratum
)
1250 unpush_target_and_assert (current_inferior ()->top_target ());
1256 pop_all_targets_at_and_above (enum strata stratum
)
1258 while ((int) (current_inferior ()->top_target ()->stratum ())
1260 unpush_target_and_assert (current_inferior ()->top_target ());
1264 pop_all_targets (void)
1266 pop_all_targets_above (dummy_stratum
);
1270 target_unpusher::operator() (struct target_ops
*ops
) const
1272 current_inferior ()->unpush_target (ops
);
1275 /* Default implementation of to_get_thread_local_address. */
1278 generic_tls_error (void)
1280 throw_error (TLS_GENERIC_ERROR
,
1281 _("Cannot find thread-local variables on this target"));
1284 /* Using the objfile specified in OBJFILE, find the address for the
1285 current thread's thread-local storage with offset OFFSET. */
1287 target_translate_tls_address (struct objfile
*objfile
, CORE_ADDR offset
)
1289 volatile CORE_ADDR addr
= 0;
1290 struct target_ops
*target
= current_inferior ()->top_target ();
1291 struct gdbarch
*gdbarch
= target_gdbarch ();
1293 if (gdbarch_fetch_tls_load_module_address_p (gdbarch
))
1295 ptid_t ptid
= inferior_ptid
;
1301 /* Fetch the load module address for this objfile. */
1302 lm_addr
= gdbarch_fetch_tls_load_module_address (gdbarch
,
1305 if (gdbarch_get_thread_local_address_p (gdbarch
))
1306 addr
= gdbarch_get_thread_local_address (gdbarch
, ptid
, lm_addr
,
1309 addr
= target
->get_thread_local_address (ptid
, lm_addr
, offset
);
1311 /* If an error occurred, print TLS related messages here. Otherwise,
1312 throw the error to some higher catcher. */
1313 catch (const gdb_exception
&ex
)
1315 int objfile_is_library
= (objfile
->flags
& OBJF_SHARED
);
1319 case TLS_NO_LIBRARY_SUPPORT_ERROR
:
1320 error (_("Cannot find thread-local variables "
1321 "in this thread library."));
1323 case TLS_LOAD_MODULE_NOT_FOUND_ERROR
:
1324 if (objfile_is_library
)
1325 error (_("Cannot find shared library `%s' in dynamic"
1326 " linker's load module list"), objfile_name (objfile
));
1328 error (_("Cannot find executable file `%s' in dynamic"
1329 " linker's load module list"), objfile_name (objfile
));
1331 case TLS_NOT_ALLOCATED_YET_ERROR
:
1332 if (objfile_is_library
)
1333 error (_("The inferior has not yet allocated storage for"
1334 " thread-local variables in\n"
1335 "the shared library `%s'\n"
1337 objfile_name (objfile
),
1338 target_pid_to_str (ptid
).c_str ());
1340 error (_("The inferior has not yet allocated storage for"
1341 " thread-local variables in\n"
1342 "the executable `%s'\n"
1344 objfile_name (objfile
),
1345 target_pid_to_str (ptid
).c_str ());
1347 case TLS_GENERIC_ERROR
:
1348 if (objfile_is_library
)
1349 error (_("Cannot find thread-local storage for %s, "
1350 "shared library %s:\n%s"),
1351 target_pid_to_str (ptid
).c_str (),
1352 objfile_name (objfile
), ex
.what ());
1354 error (_("Cannot find thread-local storage for %s, "
1355 "executable file %s:\n%s"),
1356 target_pid_to_str (ptid
).c_str (),
1357 objfile_name (objfile
), ex
.what ());
1366 error (_("Cannot find thread-local variables on this target"));
1372 target_xfer_status_to_string (enum target_xfer_status status
)
1374 #define CASE(X) case X: return #X
1377 CASE(TARGET_XFER_E_IO
);
1378 CASE(TARGET_XFER_UNAVAILABLE
);
1388 gdb::unique_xmalloc_ptr
<char>
1389 target_read_string (CORE_ADDR memaddr
, int len
, int *bytes_read
)
1391 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
1394 if (bytes_read
== nullptr)
1395 bytes_read
= &ignore
;
1397 /* Note that the endian-ness does not matter here. */
1398 int errcode
= read_string (memaddr
, -1, 1, len
, BFD_ENDIAN_LITTLE
,
1399 &buffer
, bytes_read
);
1403 return gdb::unique_xmalloc_ptr
<char> ((char *) buffer
.release ());
1406 const target_section_table
*
1407 target_get_section_table (struct target_ops
*target
)
1409 return target
->get_section_table ();
1412 /* Find a section containing ADDR. */
1414 const struct target_section
*
1415 target_section_by_addr (struct target_ops
*target
, CORE_ADDR addr
)
1417 const target_section_table
*table
= target_get_section_table (target
);
1422 for (const target_section
&secp
: *table
)
1424 if (addr
>= secp
.addr
&& addr
< secp
.endaddr
)
1432 const target_section_table
*
1433 default_get_section_table ()
1435 return ¤t_program_space
->target_sections ();
1438 /* Helper for the memory xfer routines. Checks the attributes of the
1439 memory region of MEMADDR against the read or write being attempted.
1440 If the access is permitted returns true, otherwise returns false.
1441 REGION_P is an optional output parameter. If not-NULL, it is
1442 filled with a pointer to the memory region of MEMADDR. REG_LEN
1443 returns LEN trimmed to the end of the region. This is how much the
1444 caller can continue requesting, if the access is permitted. A
1445 single xfer request must not straddle memory region boundaries. */
1448 memory_xfer_check_region (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1449 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*reg_len
,
1450 struct mem_region
**region_p
)
1452 struct mem_region
*region
;
1454 region
= lookup_mem_region (memaddr
);
1456 if (region_p
!= NULL
)
1459 switch (region
->attrib
.mode
)
1462 if (writebuf
!= NULL
)
1467 if (readbuf
!= NULL
)
1472 /* We only support writing to flash during "load" for now. */
1473 if (writebuf
!= NULL
)
1474 error (_("Writing to flash memory forbidden in this context"));
1481 /* region->hi == 0 means there's no upper bound. */
1482 if (memaddr
+ len
< region
->hi
|| region
->hi
== 0)
1485 *reg_len
= region
->hi
- memaddr
;
1490 /* Read memory from more than one valid target. A core file, for
1491 instance, could have some of memory but delegate other bits to
1492 the target below it. So, we must manually try all targets. */
1494 enum target_xfer_status
1495 raw_memory_xfer_partial (struct target_ops
*ops
, gdb_byte
*readbuf
,
1496 const gdb_byte
*writebuf
, ULONGEST memaddr
, LONGEST len
,
1497 ULONGEST
*xfered_len
)
1499 enum target_xfer_status res
;
1503 res
= ops
->xfer_partial (TARGET_OBJECT_MEMORY
, NULL
,
1504 readbuf
, writebuf
, memaddr
, len
,
1506 if (res
== TARGET_XFER_OK
)
1509 /* Stop if the target reports that the memory is not available. */
1510 if (res
== TARGET_XFER_UNAVAILABLE
)
1513 /* Don't continue past targets which have all the memory.
1514 At one time, this code was necessary to read data from
1515 executables / shared libraries when data for the requested
1516 addresses weren't available in the core file. But now the
1517 core target handles this case itself. */
1518 if (ops
->has_all_memory ())
1521 ops
= ops
->beneath ();
1523 while (ops
!= NULL
);
1525 /* The cache works at the raw memory level. Make sure the cache
1526 gets updated with raw contents no matter what kind of memory
1527 object was originally being written. Note we do write-through
1528 first, so that if it fails, we don't write to the cache contents
1529 that never made it to the target. */
1530 if (writebuf
!= NULL
1531 && inferior_ptid
!= null_ptid
1532 && target_dcache_init_p ()
1533 && (stack_cache_enabled_p () || code_cache_enabled_p ()))
1535 DCACHE
*dcache
= target_dcache_get ();
1537 /* Note that writing to an area of memory which wasn't present
1538 in the cache doesn't cause it to be loaded in. */
1539 dcache_update (dcache
, res
, memaddr
, writebuf
, *xfered_len
);
1545 /* Perform a partial memory transfer.
1546 For docs see target.h, to_xfer_partial. */
1548 static enum target_xfer_status
1549 memory_xfer_partial_1 (struct target_ops
*ops
, enum target_object object
,
1550 gdb_byte
*readbuf
, const gdb_byte
*writebuf
, ULONGEST memaddr
,
1551 ULONGEST len
, ULONGEST
*xfered_len
)
1553 enum target_xfer_status res
;
1555 struct mem_region
*region
;
1556 struct inferior
*inf
;
1558 /* For accesses to unmapped overlay sections, read directly from
1559 files. Must do this first, as MEMADDR may need adjustment. */
1560 if (readbuf
!= NULL
&& overlay_debugging
)
1562 struct obj_section
*section
= find_pc_overlay (memaddr
);
1564 if (pc_in_unmapped_range (memaddr
, section
))
1566 const target_section_table
*table
= target_get_section_table (ops
);
1567 const char *section_name
= section
->the_bfd_section
->name
;
1569 memaddr
= overlay_mapped_address (memaddr
, section
);
1571 auto match_cb
= [=] (const struct target_section
*s
)
1573 return (strcmp (section_name
, s
->the_bfd_section
->name
) == 0);
1576 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1577 memaddr
, len
, xfered_len
,
1582 /* Try the executable files, if "trust-readonly-sections" is set. */
1583 if (readbuf
!= NULL
&& trust_readonly
)
1585 const struct target_section
*secp
1586 = target_section_by_addr (ops
, memaddr
);
1588 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
1590 const target_section_table
*table
= target_get_section_table (ops
);
1591 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1592 memaddr
, len
, xfered_len
,
1597 /* Try GDB's internal data cache. */
1599 if (!memory_xfer_check_region (readbuf
, writebuf
, memaddr
, len
, ®_len
,
1601 return TARGET_XFER_E_IO
;
1603 if (inferior_ptid
!= null_ptid
)
1604 inf
= current_inferior ();
1610 /* The dcache reads whole cache lines; that doesn't play well
1611 with reading from a trace buffer, because reading outside of
1612 the collected memory range fails. */
1613 && get_traceframe_number () == -1
1614 && (region
->attrib
.cache
1615 || (stack_cache_enabled_p () && object
== TARGET_OBJECT_STACK_MEMORY
)
1616 || (code_cache_enabled_p () && object
== TARGET_OBJECT_CODE_MEMORY
)))
1618 DCACHE
*dcache
= target_dcache_get_or_init ();
1620 return dcache_read_memory_partial (ops
, dcache
, memaddr
, readbuf
,
1621 reg_len
, xfered_len
);
1624 /* If none of those methods found the memory we wanted, fall back
1625 to a target partial transfer. Normally a single call to
1626 to_xfer_partial is enough; if it doesn't recognize an object
1627 it will call the to_xfer_partial of the next target down.
1628 But for memory this won't do. Memory is the only target
1629 object which can be read from more than one valid target.
1630 A core file, for instance, could have some of memory but
1631 delegate other bits to the target below it. So, we must
1632 manually try all targets. */
1634 res
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, memaddr
, reg_len
,
1637 /* If we still haven't got anything, return the last error. We
1642 /* Perform a partial memory transfer. For docs see target.h,
1645 static enum target_xfer_status
1646 memory_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1647 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1648 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1650 enum target_xfer_status res
;
1652 /* Zero length requests are ok and require no work. */
1654 return TARGET_XFER_EOF
;
1656 memaddr
= address_significant (target_gdbarch (), memaddr
);
1658 /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1659 breakpoint insns, thus hiding out from higher layers whether
1660 there are software breakpoints inserted in the code stream. */
1661 if (readbuf
!= NULL
)
1663 res
= memory_xfer_partial_1 (ops
, object
, readbuf
, NULL
, memaddr
, len
,
1666 if (res
== TARGET_XFER_OK
&& !show_memory_breakpoints
)
1667 breakpoint_xfer_memory (readbuf
, NULL
, NULL
, memaddr
, *xfered_len
);
1671 /* A large write request is likely to be partially satisfied
1672 by memory_xfer_partial_1. We will continually malloc
1673 and free a copy of the entire write request for breakpoint
1674 shadow handling even though we only end up writing a small
1675 subset of it. Cap writes to a limit specified by the target
1676 to mitigate this. */
1677 len
= std::min (ops
->get_memory_xfer_limit (), len
);
1679 gdb::byte_vector
buf (writebuf
, writebuf
+ len
);
1680 breakpoint_xfer_memory (NULL
, buf
.data (), writebuf
, memaddr
, len
);
1681 res
= memory_xfer_partial_1 (ops
, object
, NULL
, buf
.data (), memaddr
, len
,
1688 scoped_restore_tmpl
<int>
1689 make_scoped_restore_show_memory_breakpoints (int show
)
1691 return make_scoped_restore (&show_memory_breakpoints
, show
);
1694 /* For docs see target.h, to_xfer_partial. */
1696 enum target_xfer_status
1697 target_xfer_partial (struct target_ops
*ops
,
1698 enum target_object object
, const char *annex
,
1699 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1700 ULONGEST offset
, ULONGEST len
,
1701 ULONGEST
*xfered_len
)
1703 enum target_xfer_status retval
;
1705 /* Transfer is done when LEN is zero. */
1707 return TARGET_XFER_EOF
;
1709 if (writebuf
&& !may_write_memory
)
1710 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1711 core_addr_to_string_nz (offset
), plongest (len
));
1715 /* If this is a memory transfer, let the memory-specific code
1716 have a look at it instead. Memory transfers are more
1718 if (object
== TARGET_OBJECT_MEMORY
|| object
== TARGET_OBJECT_STACK_MEMORY
1719 || object
== TARGET_OBJECT_CODE_MEMORY
)
1720 retval
= memory_xfer_partial (ops
, object
, readbuf
,
1721 writebuf
, offset
, len
, xfered_len
);
1722 else if (object
== TARGET_OBJECT_RAW_MEMORY
)
1724 /* Skip/avoid accessing the target if the memory region
1725 attributes block the access. Check this here instead of in
1726 raw_memory_xfer_partial as otherwise we'd end up checking
1727 this twice in the case of the memory_xfer_partial path is
1728 taken; once before checking the dcache, and another in the
1729 tail call to raw_memory_xfer_partial. */
1730 if (!memory_xfer_check_region (readbuf
, writebuf
, offset
, len
, &len
,
1732 return TARGET_XFER_E_IO
;
1734 /* Request the normal memory object from other layers. */
1735 retval
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, offset
, len
,
1739 retval
= ops
->xfer_partial (object
, annex
, readbuf
,
1740 writebuf
, offset
, len
, xfered_len
);
1744 const unsigned char *myaddr
= NULL
;
1746 fprintf_unfiltered (gdb_stdlog
,
1747 "%s:target_xfer_partial "
1748 "(%d, %s, %s, %s, %s, %s) = %d, %s",
1751 (annex
? annex
: "(null)"),
1752 host_address_to_string (readbuf
),
1753 host_address_to_string (writebuf
),
1754 core_addr_to_string_nz (offset
),
1755 pulongest (len
), retval
,
1756 pulongest (*xfered_len
));
1762 if (retval
== TARGET_XFER_OK
&& myaddr
!= NULL
)
1766 fputs_unfiltered (", bytes =", gdb_stdlog
);
1767 for (i
= 0; i
< *xfered_len
; i
++)
1769 if ((((intptr_t) &(myaddr
[i
])) & 0xf) == 0)
1771 if (targetdebug
< 2 && i
> 0)
1773 fprintf_unfiltered (gdb_stdlog
, " ...");
1776 fprintf_unfiltered (gdb_stdlog
, "\n");
1779 fprintf_unfiltered (gdb_stdlog
, " %02x", myaddr
[i
] & 0xff);
1783 fputc_unfiltered ('\n', gdb_stdlog
);
1786 /* Check implementations of to_xfer_partial update *XFERED_LEN
1787 properly. Do assertion after printing debug messages, so that we
1788 can find more clues on assertion failure from debugging messages. */
1789 if (retval
== TARGET_XFER_OK
|| retval
== TARGET_XFER_UNAVAILABLE
)
1790 gdb_assert (*xfered_len
> 0);
1795 /* Read LEN bytes of target memory at address MEMADDR, placing the
1796 results in GDB's memory at MYADDR. Returns either 0 for success or
1797 -1 if any error occurs.
1799 If an error occurs, no guarantee is made about the contents of the data at
1800 MYADDR. In particular, the caller should not depend upon partial reads
1801 filling the buffer with good data. There is no way for the caller to know
1802 how much good data might have been transfered anyway. Callers that can
1803 deal with partial reads should call target_read (which will retry until
1804 it makes no progress, and then return how much was transferred). */
1807 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1809 if (target_read (current_inferior ()->top_target (),
1810 TARGET_OBJECT_MEMORY
, NULL
,
1811 myaddr
, memaddr
, len
) == len
)
1817 /* See target/target.h. */
1820 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
1825 r
= target_read_memory (memaddr
, buf
, sizeof buf
);
1828 *result
= extract_unsigned_integer (buf
, sizeof buf
,
1829 gdbarch_byte_order (target_gdbarch ()));
1833 /* Like target_read_memory, but specify explicitly that this is a read
1834 from the target's raw memory. That is, this read bypasses the
1835 dcache, breakpoint shadowing, etc. */
1838 target_read_raw_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1840 if (target_read (current_inferior ()->top_target (),
1841 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1842 myaddr
, memaddr
, len
) == len
)
1848 /* Like target_read_memory, but specify explicitly that this is a read from
1849 the target's stack. This may trigger different cache behavior. */
1852 target_read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1854 if (target_read (current_inferior ()->top_target (),
1855 TARGET_OBJECT_STACK_MEMORY
, NULL
,
1856 myaddr
, memaddr
, len
) == len
)
1862 /* Like target_read_memory, but specify explicitly that this is a read from
1863 the target's code. This may trigger different cache behavior. */
1866 target_read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1868 if (target_read (current_inferior ()->top_target (),
1869 TARGET_OBJECT_CODE_MEMORY
, NULL
,
1870 myaddr
, memaddr
, len
) == len
)
1876 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1877 Returns either 0 for success or -1 if any error occurs. If an
1878 error occurs, no guarantee is made about how much data got written.
1879 Callers that can deal with partial writes should call
1883 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1885 if (target_write (current_inferior ()->top_target (),
1886 TARGET_OBJECT_MEMORY
, NULL
,
1887 myaddr
, memaddr
, len
) == len
)
1893 /* Write LEN bytes from MYADDR to target raw memory at address
1894 MEMADDR. Returns either 0 for success or -1 if any error occurs.
1895 If an error occurs, no guarantee is made about how much data got
1896 written. Callers that can deal with partial writes should call
1900 target_write_raw_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1902 if (target_write (current_inferior ()->top_target (),
1903 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1904 myaddr
, memaddr
, len
) == len
)
1910 /* Fetch the target's memory map. */
1912 std::vector
<mem_region
>
1913 target_memory_map (void)
1915 target_ops
*target
= current_inferior ()->top_target ();
1916 std::vector
<mem_region
> result
= target
->memory_map ();
1917 if (result
.empty ())
1920 std::sort (result
.begin (), result
.end ());
1922 /* Check that regions do not overlap. Simultaneously assign
1923 a numbering for the "mem" commands to use to refer to
1925 mem_region
*last_one
= NULL
;
1926 for (size_t ix
= 0; ix
< result
.size (); ix
++)
1928 mem_region
*this_one
= &result
[ix
];
1929 this_one
->number
= ix
;
1931 if (last_one
!= NULL
&& last_one
->hi
> this_one
->lo
)
1933 warning (_("Overlapping regions in memory map: ignoring"));
1934 return std::vector
<mem_region
> ();
1937 last_one
= this_one
;
1944 target_flash_erase (ULONGEST address
, LONGEST length
)
1946 current_inferior ()->top_target ()->flash_erase (address
, length
);
1950 target_flash_done (void)
1952 current_inferior ()->top_target ()->flash_done ();
1956 show_trust_readonly (struct ui_file
*file
, int from_tty
,
1957 struct cmd_list_element
*c
, const char *value
)
1959 fprintf_filtered (file
,
1960 _("Mode for reading from readonly sections is %s.\n"),
1964 /* Target vector read/write partial wrapper functions. */
1966 static enum target_xfer_status
1967 target_read_partial (struct target_ops
*ops
,
1968 enum target_object object
,
1969 const char *annex
, gdb_byte
*buf
,
1970 ULONGEST offset
, ULONGEST len
,
1971 ULONGEST
*xfered_len
)
1973 return target_xfer_partial (ops
, object
, annex
, buf
, NULL
, offset
, len
,
1977 static enum target_xfer_status
1978 target_write_partial (struct target_ops
*ops
,
1979 enum target_object object
,
1980 const char *annex
, const gdb_byte
*buf
,
1981 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
1983 return target_xfer_partial (ops
, object
, annex
, NULL
, buf
, offset
, len
,
1987 /* Wrappers to perform the full transfer. */
1989 /* For docs on target_read see target.h. */
1992 target_read (struct target_ops
*ops
,
1993 enum target_object object
,
1994 const char *annex
, gdb_byte
*buf
,
1995 ULONGEST offset
, LONGEST len
)
1997 LONGEST xfered_total
= 0;
2000 /* If we are reading from a memory object, find the length of an addressable
2001 unit for that architecture. */
2002 if (object
== TARGET_OBJECT_MEMORY
2003 || object
== TARGET_OBJECT_STACK_MEMORY
2004 || object
== TARGET_OBJECT_CODE_MEMORY
2005 || object
== TARGET_OBJECT_RAW_MEMORY
)
2006 unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
2008 while (xfered_total
< len
)
2010 ULONGEST xfered_partial
;
2011 enum target_xfer_status status
;
2013 status
= target_read_partial (ops
, object
, annex
,
2014 buf
+ xfered_total
* unit_size
,
2015 offset
+ xfered_total
, len
- xfered_total
,
2018 /* Call an observer, notifying them of the xfer progress? */
2019 if (status
== TARGET_XFER_EOF
)
2020 return xfered_total
;
2021 else if (status
== TARGET_XFER_OK
)
2023 xfered_total
+= xfered_partial
;
2027 return TARGET_XFER_E_IO
;
2033 /* Assuming that the entire [begin, end) range of memory cannot be
2034 read, try to read whatever subrange is possible to read.
2036 The function returns, in RESULT, either zero or one memory block.
2037 If there's a readable subrange at the beginning, it is completely
2038 read and returned. Any further readable subrange will not be read.
2039 Otherwise, if there's a readable subrange at the end, it will be
2040 completely read and returned. Any readable subranges before it
2041 (obviously, not starting at the beginning), will be ignored. In
2042 other cases -- either no readable subrange, or readable subrange(s)
2043 that is neither at the beginning, or end, nothing is returned.
2045 The purpose of this function is to handle a read across a boundary
2046 of accessible memory in a case when memory map is not available.
2047 The above restrictions are fine for this case, but will give
2048 incorrect results if the memory is 'patchy'. However, supporting
2049 'patchy' memory would require trying to read every single byte,
2050 and it seems unacceptable solution. Explicit memory map is
2051 recommended for this case -- and target_read_memory_robust will
2052 take care of reading multiple ranges then. */
2055 read_whatever_is_readable (struct target_ops
*ops
,
2056 const ULONGEST begin
, const ULONGEST end
,
2058 std::vector
<memory_read_result
> *result
)
2060 ULONGEST current_begin
= begin
;
2061 ULONGEST current_end
= end
;
2063 ULONGEST xfered_len
;
2065 /* If we previously failed to read 1 byte, nothing can be done here. */
2066 if (end
- begin
<= 1)
2069 gdb::unique_xmalloc_ptr
<gdb_byte
> buf ((gdb_byte
*) xmalloc (end
- begin
));
2071 /* Check that either first or the last byte is readable, and give up
2072 if not. This heuristic is meant to permit reading accessible memory
2073 at the boundary of accessible region. */
2074 if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2075 buf
.get (), begin
, 1, &xfered_len
) == TARGET_XFER_OK
)
2080 else if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2081 buf
.get () + (end
- begin
) - 1, end
- 1, 1,
2082 &xfered_len
) == TARGET_XFER_OK
)
2090 /* Loop invariant is that the [current_begin, current_end) was previously
2091 found to be not readable as a whole.
2093 Note loop condition -- if the range has 1 byte, we can't divide the range
2094 so there's no point trying further. */
2095 while (current_end
- current_begin
> 1)
2097 ULONGEST first_half_begin
, first_half_end
;
2098 ULONGEST second_half_begin
, second_half_end
;
2100 ULONGEST middle
= current_begin
+ (current_end
- current_begin
) / 2;
2104 first_half_begin
= current_begin
;
2105 first_half_end
= middle
;
2106 second_half_begin
= middle
;
2107 second_half_end
= current_end
;
2111 first_half_begin
= middle
;
2112 first_half_end
= current_end
;
2113 second_half_begin
= current_begin
;
2114 second_half_end
= middle
;
2117 xfer
= target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2118 buf
.get () + (first_half_begin
- begin
) * unit_size
,
2120 first_half_end
- first_half_begin
);
2122 if (xfer
== first_half_end
- first_half_begin
)
2124 /* This half reads up fine. So, the error must be in the
2126 current_begin
= second_half_begin
;
2127 current_end
= second_half_end
;
2131 /* This half is not readable. Because we've tried one byte, we
2132 know some part of this half if actually readable. Go to the next
2133 iteration to divide again and try to read.
2135 We don't handle the other half, because this function only tries
2136 to read a single readable subrange. */
2137 current_begin
= first_half_begin
;
2138 current_end
= first_half_end
;
2144 /* The [begin, current_begin) range has been read. */
2145 result
->emplace_back (begin
, current_end
, std::move (buf
));
2149 /* The [current_end, end) range has been read. */
2150 LONGEST region_len
= end
- current_end
;
2152 gdb::unique_xmalloc_ptr
<gdb_byte
> data
2153 ((gdb_byte
*) xmalloc (region_len
* unit_size
));
2154 memcpy (data
.get (), buf
.get () + (current_end
- begin
) * unit_size
,
2155 region_len
* unit_size
);
2156 result
->emplace_back (current_end
, end
, std::move (data
));
2160 std::vector
<memory_read_result
>
2161 read_memory_robust (struct target_ops
*ops
,
2162 const ULONGEST offset
, const LONGEST len
)
2164 std::vector
<memory_read_result
> result
;
2165 int unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
2167 LONGEST xfered_total
= 0;
2168 while (xfered_total
< len
)
2170 struct mem_region
*region
= lookup_mem_region (offset
+ xfered_total
);
2173 /* If there is no explicit region, a fake one should be created. */
2174 gdb_assert (region
);
2176 if (region
->hi
== 0)
2177 region_len
= len
- xfered_total
;
2179 region_len
= region
->hi
- offset
;
2181 if (region
->attrib
.mode
== MEM_NONE
|| region
->attrib
.mode
== MEM_WO
)
2183 /* Cannot read this region. Note that we can end up here only
2184 if the region is explicitly marked inaccessible, or
2185 'inaccessible-by-default' is in effect. */
2186 xfered_total
+= region_len
;
2190 LONGEST to_read
= std::min (len
- xfered_total
, region_len
);
2191 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
2192 ((gdb_byte
*) xmalloc (to_read
* unit_size
));
2194 LONGEST xfered_partial
=
2195 target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
, buffer
.get (),
2196 offset
+ xfered_total
, to_read
);
2197 /* Call an observer, notifying them of the xfer progress? */
2198 if (xfered_partial
<= 0)
2200 /* Got an error reading full chunk. See if maybe we can read
2202 read_whatever_is_readable (ops
, offset
+ xfered_total
,
2203 offset
+ xfered_total
+ to_read
,
2204 unit_size
, &result
);
2205 xfered_total
+= to_read
;
2209 result
.emplace_back (offset
+ xfered_total
,
2210 offset
+ xfered_total
+ xfered_partial
,
2211 std::move (buffer
));
2212 xfered_total
+= xfered_partial
;
2222 /* An alternative to target_write with progress callbacks. */
2225 target_write_with_progress (struct target_ops
*ops
,
2226 enum target_object object
,
2227 const char *annex
, const gdb_byte
*buf
,
2228 ULONGEST offset
, LONGEST len
,
2229 void (*progress
) (ULONGEST
, void *), void *baton
)
2231 LONGEST xfered_total
= 0;
2234 /* If we are writing to a memory object, find the length of an addressable
2235 unit for that architecture. */
2236 if (object
== TARGET_OBJECT_MEMORY
2237 || object
== TARGET_OBJECT_STACK_MEMORY
2238 || object
== TARGET_OBJECT_CODE_MEMORY
2239 || object
== TARGET_OBJECT_RAW_MEMORY
)
2240 unit_size
= gdbarch_addressable_memory_unit_size (target_gdbarch ());
2242 /* Give the progress callback a chance to set up. */
2244 (*progress
) (0, baton
);
2246 while (xfered_total
< len
)
2248 ULONGEST xfered_partial
;
2249 enum target_xfer_status status
;
2251 status
= target_write_partial (ops
, object
, annex
,
2252 buf
+ xfered_total
* unit_size
,
2253 offset
+ xfered_total
, len
- xfered_total
,
2256 if (status
!= TARGET_XFER_OK
)
2257 return status
== TARGET_XFER_EOF
? xfered_total
: TARGET_XFER_E_IO
;
2260 (*progress
) (xfered_partial
, baton
);
2262 xfered_total
+= xfered_partial
;
2268 /* For docs on target_write see target.h. */
2271 target_write (struct target_ops
*ops
,
2272 enum target_object object
,
2273 const char *annex
, const gdb_byte
*buf
,
2274 ULONGEST offset
, LONGEST len
)
2276 return target_write_with_progress (ops
, object
, annex
, buf
, offset
, len
,
2280 /* Help for target_read_alloc and target_read_stralloc. See their comments
2283 template <typename T
>
2284 gdb::optional
<gdb::def_vector
<T
>>
2285 target_read_alloc_1 (struct target_ops
*ops
, enum target_object object
,
2288 gdb::def_vector
<T
> buf
;
2290 const int chunk
= 4096;
2292 /* This function does not have a length parameter; it reads the
2293 entire OBJECT). Also, it doesn't support objects fetched partly
2294 from one target and partly from another (in a different stratum,
2295 e.g. a core file and an executable). Both reasons make it
2296 unsuitable for reading memory. */
2297 gdb_assert (object
!= TARGET_OBJECT_MEMORY
);
2299 /* Start by reading up to 4K at a time. The target will throttle
2300 this number down if necessary. */
2303 ULONGEST xfered_len
;
2304 enum target_xfer_status status
;
2306 buf
.resize (buf_pos
+ chunk
);
2308 status
= target_read_partial (ops
, object
, annex
,
2309 (gdb_byte
*) &buf
[buf_pos
],
2313 if (status
== TARGET_XFER_EOF
)
2315 /* Read all there was. */
2316 buf
.resize (buf_pos
);
2319 else if (status
!= TARGET_XFER_OK
)
2321 /* An error occurred. */
2325 buf_pos
+= xfered_len
;
2333 gdb::optional
<gdb::byte_vector
>
2334 target_read_alloc (struct target_ops
*ops
, enum target_object object
,
2337 return target_read_alloc_1
<gdb_byte
> (ops
, object
, annex
);
2342 gdb::optional
<gdb::char_vector
>
2343 target_read_stralloc (struct target_ops
*ops
, enum target_object object
,
2346 gdb::optional
<gdb::char_vector
> buf
2347 = target_read_alloc_1
<char> (ops
, object
, annex
);
2352 if (buf
->empty () || buf
->back () != '\0')
2353 buf
->push_back ('\0');
2355 /* Check for embedded NUL bytes; but allow trailing NULs. */
2356 for (auto it
= std::find (buf
->begin (), buf
->end (), '\0');
2357 it
!= buf
->end (); it
++)
2360 warning (_("target object %d, annex %s, "
2361 "contained unexpected null characters"),
2362 (int) object
, annex
? annex
: "(none)");
2369 /* Memory transfer methods. */
2372 get_target_memory (struct target_ops
*ops
, CORE_ADDR addr
, gdb_byte
*buf
,
2375 /* This method is used to read from an alternate, non-current
2376 target. This read must bypass the overlay support (as symbols
2377 don't match this target), and GDB's internal cache (wrong cache
2378 for this target). */
2379 if (target_read (ops
, TARGET_OBJECT_RAW_MEMORY
, NULL
, buf
, addr
, len
)
2381 memory_error (TARGET_XFER_E_IO
, addr
);
2385 get_target_memory_unsigned (struct target_ops
*ops
, CORE_ADDR addr
,
2386 int len
, enum bfd_endian byte_order
)
2388 gdb_byte buf
[sizeof (ULONGEST
)];
2390 gdb_assert (len
<= sizeof (buf
));
2391 get_target_memory (ops
, addr
, buf
, len
);
2392 return extract_unsigned_integer (buf
, len
, byte_order
);
2398 target_insert_breakpoint (struct gdbarch
*gdbarch
,
2399 struct bp_target_info
*bp_tgt
)
2401 if (!may_insert_breakpoints
)
2403 warning (_("May not insert breakpoints"));
2407 target_ops
*target
= current_inferior ()->top_target ();
2409 return target
->insert_breakpoint (gdbarch
, bp_tgt
);
2415 target_remove_breakpoint (struct gdbarch
*gdbarch
,
2416 struct bp_target_info
*bp_tgt
,
2417 enum remove_bp_reason reason
)
2419 /* This is kind of a weird case to handle, but the permission might
2420 have been changed after breakpoints were inserted - in which case
2421 we should just take the user literally and assume that any
2422 breakpoints should be left in place. */
2423 if (!may_insert_breakpoints
)
2425 warning (_("May not remove breakpoints"));
2429 target_ops
*target
= current_inferior ()->top_target ();
2431 return target
->remove_breakpoint (gdbarch
, bp_tgt
, reason
);
2435 info_target_command (const char *args
, int from_tty
)
2437 int has_all_mem
= 0;
2439 if (current_program_space
->symfile_object_file
!= NULL
)
2441 objfile
*objf
= current_program_space
->symfile_object_file
;
2442 printf_unfiltered (_("Symbols from \"%s\".\n"),
2443 objfile_name (objf
));
2446 for (target_ops
*t
= current_inferior ()->top_target ();
2450 if (!t
->has_memory ())
2453 if ((int) (t
->stratum ()) <= (int) dummy_stratum
)
2456 printf_unfiltered (_("\tWhile running this, "
2457 "GDB does not access memory from...\n"));
2458 printf_unfiltered ("%s:\n", t
->longname ());
2460 has_all_mem
= t
->has_all_memory ();
2464 /* This function is called before any new inferior is created, e.g.
2465 by running a program, attaching, or connecting to a target.
2466 It cleans up any state from previous invocations which might
2467 change between runs. This is a subset of what target_preopen
2468 resets (things which might change between targets). */
2471 target_pre_inferior (int from_tty
)
2473 /* Clear out solib state. Otherwise the solib state of the previous
2474 inferior might have survived and is entirely wrong for the new
2475 target. This has been observed on GNU/Linux using glibc 2.3. How
2487 Cannot access memory at address 0xdeadbeef
2490 /* In some OSs, the shared library list is the same/global/shared
2491 across inferiors. If code is shared between processes, so are
2492 memory regions and features. */
2493 if (!gdbarch_has_global_solist (target_gdbarch ()))
2495 no_shared_libraries (NULL
, from_tty
);
2497 invalidate_target_mem_regions ();
2499 target_clear_description ();
2502 /* attach_flag may be set if the previous process associated with
2503 the inferior was attached to. */
2504 current_inferior ()->attach_flag
= 0;
2506 current_inferior ()->highest_thread_num
= 0;
2508 agent_capability_invalidate ();
2511 /* This is to be called by the open routine before it does
2515 target_preopen (int from_tty
)
2519 if (current_inferior ()->pid
!= 0)
2522 || !target_has_execution ()
2523 || query (_("A program is being debugged already. Kill it? ")))
2525 /* Core inferiors actually should be detached, not
2527 if (target_has_execution ())
2530 target_detach (current_inferior (), 0);
2533 error (_("Program not killed."));
2536 /* Calling target_kill may remove the target from the stack. But if
2537 it doesn't (which seems like a win for UDI), remove it now. */
2538 /* Leave the exec target, though. The user may be switching from a
2539 live process to a core of the same program. */
2540 pop_all_targets_above (file_stratum
);
2542 target_pre_inferior (from_tty
);
2548 target_detach (inferior
*inf
, int from_tty
)
2550 /* After we have detached, we will clear the register cache for this inferior
2551 by calling registers_changed_ptid. We must save the pid_ptid before
2552 detaching, as the target detach method will clear inf->pid. */
2553 ptid_t save_pid_ptid
= ptid_t (inf
->pid
);
2555 /* As long as some to_detach implementations rely on the current_inferior
2556 (either directly, or indirectly, like through target_gdbarch or by
2557 reading memory), INF needs to be the current inferior. When that
2558 requirement will become no longer true, then we can remove this
2560 gdb_assert (inf
== current_inferior ());
2562 prepare_for_detach ();
2564 /* Hold a strong reference because detaching may unpush the
2566 auto proc_target_ref
= target_ops_ref::new_reference (inf
->process_target ());
2568 current_inferior ()->top_target ()->detach (inf
, from_tty
);
2570 process_stratum_target
*proc_target
2571 = as_process_stratum_target (proc_target_ref
.get ());
2573 registers_changed_ptid (proc_target
, save_pid_ptid
);
2575 /* We have to ensure we have no frame cache left. Normally,
2576 registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
2577 inferior_ptid matches save_pid_ptid, but in our case, it does not
2578 call it, as inferior_ptid has been reset. */
2579 reinit_frame_cache ();
2583 target_disconnect (const char *args
, int from_tty
)
2585 /* If we're in breakpoints-always-inserted mode or if breakpoints
2586 are global across processes, we have to remove them before
2588 remove_breakpoints ();
2590 current_inferior ()->top_target ()->disconnect (args
, from_tty
);
2593 /* See target/target.h. */
2596 target_wait (ptid_t ptid
, struct target_waitstatus
*status
,
2597 target_wait_flags options
)
2599 target_ops
*target
= current_inferior ()->top_target ();
2600 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
2602 gdb_assert (!proc_target
->commit_resumed_state
);
2604 if (!target
->can_async_p ())
2605 gdb_assert ((options
& TARGET_WNOHANG
) == 0);
2607 return target
->wait (ptid
, status
, options
);
2613 default_target_wait (struct target_ops
*ops
,
2614 ptid_t ptid
, struct target_waitstatus
*status
,
2615 target_wait_flags options
)
2617 status
->kind
= TARGET_WAITKIND_IGNORE
;
2618 return minus_one_ptid
;
2622 target_pid_to_str (ptid_t ptid
)
2624 return current_inferior ()->top_target ()->pid_to_str (ptid
);
2628 target_thread_name (struct thread_info
*info
)
2630 gdb_assert (info
->inf
== current_inferior ());
2632 return current_inferior ()->top_target ()->thread_name (info
);
2635 struct thread_info
*
2636 target_thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
2638 struct inferior
*inf
)
2640 target_ops
*target
= current_inferior ()->top_target ();
2642 return target
->thread_handle_to_thread_info (thread_handle
, handle_len
, inf
);
2648 target_thread_info_to_thread_handle (struct thread_info
*tip
)
2650 target_ops
*target
= current_inferior ()->top_target ();
2652 return target
->thread_info_to_thread_handle (tip
);
2656 target_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
2658 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
2659 gdb_assert (!curr_target
->commit_resumed_state
);
2661 target_dcache_invalidate ();
2663 current_inferior ()->top_target ()->resume (ptid
, step
, signal
);
2665 registers_changed_ptid (curr_target
, ptid
);
2666 /* We only set the internal executing state here. The user/frontend
2667 running state is set at a higher level. This also clears the
2668 thread's stop_pc as side effect. */
2669 set_executing (curr_target
, ptid
, true);
2670 clear_inline_frame_state (curr_target
, ptid
);
2676 target_commit_resumed ()
2678 gdb_assert (current_inferior ()->process_target ()->commit_resumed_state
);
2679 current_inferior ()->top_target ()->commit_resumed ();
2685 target_has_pending_events ()
2687 return current_inferior ()->top_target ()->has_pending_events ();
2691 target_pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2693 current_inferior ()->top_target ()->pass_signals (pass_signals
);
2697 target_program_signals (gdb::array_view
<const unsigned char> program_signals
)
2699 current_inferior ()->top_target ()->program_signals (program_signals
);
2703 default_follow_fork (struct target_ops
*self
, bool follow_child
,
2706 /* Some target returned a fork event, but did not know how to follow it. */
2707 internal_error (__FILE__
, __LINE__
,
2708 _("could not find a target to follow fork"));
2711 /* Look through the list of possible targets for a target that can
2715 target_follow_fork (bool follow_child
, bool detach_fork
)
2717 target_ops
*target
= current_inferior ()->top_target ();
2719 return target
->follow_fork (follow_child
, detach_fork
);
2722 /* Target wrapper for follow exec hook. */
2725 target_follow_exec (struct inferior
*inf
, const char *execd_pathname
)
2727 current_inferior ()->top_target ()->follow_exec (inf
, execd_pathname
);
2731 default_mourn_inferior (struct target_ops
*self
)
2733 internal_error (__FILE__
, __LINE__
,
2734 _("could not find a target to follow mourn inferior"));
2738 target_mourn_inferior (ptid_t ptid
)
2740 gdb_assert (ptid
.pid () == inferior_ptid
.pid ());
2741 current_inferior ()->top_target ()->mourn_inferior ();
2743 /* We no longer need to keep handles on any of the object files.
2744 Make sure to release them to avoid unnecessarily locking any
2745 of them while we're not actually debugging. */
2746 bfd_cache_close_all ();
2749 /* Look for a target which can describe architectural features, starting
2750 from TARGET. If we find one, return its description. */
2752 const struct target_desc
*
2753 target_read_description (struct target_ops
*target
)
2755 return target
->read_description ();
2759 /* Default implementation of memory-searching. */
2762 default_search_memory (struct target_ops
*self
,
2763 CORE_ADDR start_addr
, ULONGEST search_space_len
,
2764 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2765 CORE_ADDR
*found_addrp
)
2767 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
2769 return target_read (current_inferior ()->top_target (),
2770 TARGET_OBJECT_MEMORY
, NULL
,
2771 result
, addr
, len
) == len
;
2774 /* Start over from the top of the target stack. */
2775 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
2776 pattern
, pattern_len
, found_addrp
);
2779 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2780 sequence of bytes in PATTERN with length PATTERN_LEN.
2782 The result is 1 if found, 0 if not found, and -1 if there was an error
2783 requiring halting of the search (e.g. memory read error).
2784 If the pattern is found the address is recorded in FOUND_ADDRP. */
2787 target_search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
2788 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2789 CORE_ADDR
*found_addrp
)
2791 target_ops
*target
= current_inferior ()->top_target ();
2793 return target
->search_memory (start_addr
, search_space_len
, pattern
,
2794 pattern_len
, found_addrp
);
2797 /* Look through the currently pushed targets. If none of them will
2798 be able to restart the currently running process, issue an error
2802 target_require_runnable (void)
2804 for (target_ops
*t
= current_inferior ()->top_target ();
2808 /* If this target knows how to create a new program, then
2809 assume we will still be able to after killing the current
2810 one. Either killing and mourning will not pop T, or else
2811 find_default_run_target will find it again. */
2812 if (t
->can_create_inferior ())
2815 /* Do not worry about targets at certain strata that can not
2816 create inferiors. Assume they will be pushed again if
2817 necessary, and continue to the process_stratum. */
2818 if (t
->stratum () > process_stratum
)
2821 error (_("The \"%s\" target does not support \"run\". "
2822 "Try \"help target\" or \"continue\"."),
2826 /* This function is only called if the target is running. In that
2827 case there should have been a process_stratum target and it
2828 should either know how to create inferiors, or not... */
2829 internal_error (__FILE__
, __LINE__
, _("No targets found"));
2832 /* Whether GDB is allowed to fall back to the default run target for
2833 "run", "attach", etc. when no target is connected yet. */
2834 static bool auto_connect_native_target
= true;
2837 show_auto_connect_native_target (struct ui_file
*file
, int from_tty
,
2838 struct cmd_list_element
*c
, const char *value
)
2840 fprintf_filtered (file
,
2841 _("Whether GDB may automatically connect to the "
2842 "native target is %s.\n"),
2846 /* A pointer to the target that can respond to "run" or "attach".
2847 Native targets are always singletons and instantiated early at GDB
2849 static target_ops
*the_native_target
;
2854 set_native_target (target_ops
*target
)
2856 if (the_native_target
!= NULL
)
2857 internal_error (__FILE__
, __LINE__
,
2858 _("native target already set (\"%s\")."),
2859 the_native_target
->longname ());
2861 the_native_target
= target
;
2867 get_native_target ()
2869 return the_native_target
;
2872 /* Look through the list of possible targets for a target that can
2873 execute a run or attach command without any other data. This is
2874 used to locate the default process stratum.
2876 If DO_MESG is not NULL, the result is always valid (error() is
2877 called for errors); else, return NULL on error. */
2879 static struct target_ops
*
2880 find_default_run_target (const char *do_mesg
)
2882 if (auto_connect_native_target
&& the_native_target
!= NULL
)
2883 return the_native_target
;
2885 if (do_mesg
!= NULL
)
2886 error (_("Don't know how to %s. Try \"help target\"."), do_mesg
);
2893 find_attach_target (void)
2895 /* If a target on the current stack can attach, use it. */
2896 for (target_ops
*t
= current_inferior ()->top_target ();
2900 if (t
->can_attach ())
2904 /* Otherwise, use the default run target for attaching. */
2905 return find_default_run_target ("attach");
2911 find_run_target (void)
2913 /* If a target on the current stack can run, use it. */
2914 for (target_ops
*t
= current_inferior ()->top_target ();
2918 if (t
->can_create_inferior ())
2922 /* Otherwise, use the default run target. */
2923 return find_default_run_target ("run");
2927 target_ops::info_proc (const char *args
, enum info_proc_what what
)
2932 /* Implement the "info proc" command. */
2935 target_info_proc (const char *args
, enum info_proc_what what
)
2937 struct target_ops
*t
;
2939 /* If we're already connected to something that can get us OS
2940 related data, use it. Otherwise, try using the native
2942 t
= find_target_at (process_stratum
);
2944 t
= find_default_run_target (NULL
);
2946 for (; t
!= NULL
; t
= t
->beneath ())
2948 if (t
->info_proc (args
, what
))
2951 fprintf_unfiltered (gdb_stdlog
,
2952 "target_info_proc (\"%s\", %d)\n", args
, what
);
2962 find_default_supports_disable_randomization (struct target_ops
*self
)
2964 struct target_ops
*t
;
2966 t
= find_default_run_target (NULL
);
2968 return t
->supports_disable_randomization ();
2973 target_supports_disable_randomization (void)
2975 return current_inferior ()->top_target ()->supports_disable_randomization ();
2978 /* See target/target.h. */
2981 target_supports_multi_process (void)
2983 return current_inferior ()->top_target ()->supports_multi_process ();
2988 gdb::optional
<gdb::char_vector
>
2989 target_get_osdata (const char *type
)
2991 struct target_ops
*t
;
2993 /* If we're already connected to something that can get us OS
2994 related data, use it. Otherwise, try using the native
2996 t
= find_target_at (process_stratum
);
2998 t
= find_default_run_target ("get OS data");
3003 return target_read_stralloc (t
, TARGET_OBJECT_OSDATA
, type
);
3006 /* Determine the current address space of thread PTID. */
3008 struct address_space
*
3009 target_thread_address_space (ptid_t ptid
)
3011 struct address_space
*aspace
;
3013 aspace
= current_inferior ()->top_target ()->thread_address_space (ptid
);
3014 gdb_assert (aspace
!= NULL
);
3022 target_ops::beneath () const
3024 return current_inferior ()->find_target_beneath (this);
3028 target_ops::close ()
3033 target_ops::can_attach ()
3039 target_ops::attach (const char *, int)
3041 gdb_assert_not_reached ("target_ops::attach called");
3045 target_ops::can_create_inferior ()
3051 target_ops::create_inferior (const char *, const std::string
&,
3054 gdb_assert_not_reached ("target_ops::create_inferior called");
3058 target_ops::can_run ()
3066 for (target_ops
*t
= current_inferior ()->top_target ();
3077 /* Target file operations. */
3079 static struct target_ops
*
3080 default_fileio_target (void)
3082 struct target_ops
*t
;
3084 /* If we're already connected to something that can perform
3085 file I/O, use it. Otherwise, try using the native target. */
3086 t
= find_target_at (process_stratum
);
3089 return find_default_run_target ("file I/O");
3092 /* File handle for target file operations. */
3096 /* The target on which this file is open. NULL if the target is
3097 meanwhile closed while the handle is open. */
3100 /* The file descriptor on the target. */
3103 /* Check whether this fileio_fh_t represents a closed file. */
3106 return target_fd
< 0;
3110 /* Vector of currently open file handles. The value returned by
3111 target_fileio_open and passed as the FD argument to other
3112 target_fileio_* functions is an index into this vector. This
3113 vector's entries are never freed; instead, files are marked as
3114 closed, and the handle becomes available for reuse. */
3115 static std::vector
<fileio_fh_t
> fileio_fhandles
;
3117 /* Index into fileio_fhandles of the lowest handle that might be
3118 closed. This permits handle reuse without searching the whole
3119 list each time a new file is opened. */
3120 static int lowest_closed_fd
;
3122 /* Invalidate the target associated with open handles that were open
3123 on target TARG, since we're about to close (and maybe destroy) the
3124 target. The handles remain open from the client's perspective, but
3125 trying to do anything with them other than closing them will fail
3129 fileio_handles_invalidate_target (target_ops
*targ
)
3131 for (fileio_fh_t
&fh
: fileio_fhandles
)
3132 if (fh
.target
== targ
)
3136 /* Acquire a target fileio file descriptor. */
3139 acquire_fileio_fd (target_ops
*target
, int target_fd
)
3141 /* Search for closed handles to reuse. */
3142 for (; lowest_closed_fd
< fileio_fhandles
.size (); lowest_closed_fd
++)
3144 fileio_fh_t
&fh
= fileio_fhandles
[lowest_closed_fd
];
3146 if (fh
.is_closed ())
3150 /* Push a new handle if no closed handles were found. */
3151 if (lowest_closed_fd
== fileio_fhandles
.size ())
3152 fileio_fhandles
.push_back (fileio_fh_t
{target
, target_fd
});
3154 fileio_fhandles
[lowest_closed_fd
] = {target
, target_fd
};
3156 /* Should no longer be marked closed. */
3157 gdb_assert (!fileio_fhandles
[lowest_closed_fd
].is_closed ());
3159 /* Return its index, and start the next lookup at
3161 return lowest_closed_fd
++;
3164 /* Release a target fileio file descriptor. */
3167 release_fileio_fd (int fd
, fileio_fh_t
*fh
)
3170 lowest_closed_fd
= std::min (lowest_closed_fd
, fd
);
3173 /* Return a pointer to the fileio_fhandle_t corresponding to FD. */
3175 static fileio_fh_t
*
3176 fileio_fd_to_fh (int fd
)
3178 return &fileio_fhandles
[fd
];
3182 /* Default implementations of file i/o methods. We don't want these
3183 to delegate automatically, because we need to know which target
3184 supported the method, in order to call it directly from within
3185 pread/pwrite, etc. */
3188 target_ops::fileio_open (struct inferior
*inf
, const char *filename
,
3189 int flags
, int mode
, int warn_if_slow
,
3192 *target_errno
= FILEIO_ENOSYS
;
3197 target_ops::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3198 ULONGEST offset
, int *target_errno
)
3200 *target_errno
= FILEIO_ENOSYS
;
3205 target_ops::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3206 ULONGEST offset
, int *target_errno
)
3208 *target_errno
= FILEIO_ENOSYS
;
3213 target_ops::fileio_fstat (int fd
, struct stat
*sb
, int *target_errno
)
3215 *target_errno
= FILEIO_ENOSYS
;
3220 target_ops::fileio_close (int fd
, int *target_errno
)
3222 *target_errno
= FILEIO_ENOSYS
;
3227 target_ops::fileio_unlink (struct inferior
*inf
, const char *filename
,
3230 *target_errno
= FILEIO_ENOSYS
;
3234 gdb::optional
<std::string
>
3235 target_ops::fileio_readlink (struct inferior
*inf
, const char *filename
,
3238 *target_errno
= FILEIO_ENOSYS
;
3245 target_fileio_open (struct inferior
*inf
, const char *filename
,
3246 int flags
, int mode
, bool warn_if_slow
, int *target_errno
)
3248 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3250 int fd
= t
->fileio_open (inf
, filename
, flags
, mode
,
3251 warn_if_slow
, target_errno
);
3253 if (fd
== -1 && *target_errno
== FILEIO_ENOSYS
)
3259 fd
= acquire_fileio_fd (t
, fd
);
3262 fprintf_unfiltered (gdb_stdlog
,
3263 "target_fileio_open (%d,%s,0x%x,0%o,%d)"
3265 inf
== NULL
? 0 : inf
->num
,
3266 filename
, flags
, mode
,
3268 fd
!= -1 ? 0 : *target_errno
);
3272 *target_errno
= FILEIO_ENOSYS
;
3279 target_fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3280 ULONGEST offset
, int *target_errno
)
3282 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3285 if (fh
->is_closed ())
3286 *target_errno
= EBADF
;
3287 else if (fh
->target
== NULL
)
3288 *target_errno
= EIO
;
3290 ret
= fh
->target
->fileio_pwrite (fh
->target_fd
, write_buf
,
3291 len
, offset
, target_errno
);
3294 fprintf_unfiltered (gdb_stdlog
,
3295 "target_fileio_pwrite (%d,...,%d,%s) "
3297 fd
, len
, pulongest (offset
),
3298 ret
, ret
!= -1 ? 0 : *target_errno
);
3305 target_fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3306 ULONGEST offset
, int *target_errno
)
3308 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3311 if (fh
->is_closed ())
3312 *target_errno
= EBADF
;
3313 else if (fh
->target
== NULL
)
3314 *target_errno
= EIO
;
3316 ret
= fh
->target
->fileio_pread (fh
->target_fd
, read_buf
,
3317 len
, offset
, target_errno
);
3320 fprintf_unfiltered (gdb_stdlog
,
3321 "target_fileio_pread (%d,...,%d,%s) "
3323 fd
, len
, pulongest (offset
),
3324 ret
, ret
!= -1 ? 0 : *target_errno
);
3331 target_fileio_fstat (int fd
, struct stat
*sb
, int *target_errno
)
3333 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3336 if (fh
->is_closed ())
3337 *target_errno
= EBADF
;
3338 else if (fh
->target
== NULL
)
3339 *target_errno
= EIO
;
3341 ret
= fh
->target
->fileio_fstat (fh
->target_fd
, sb
, target_errno
);
3344 fprintf_unfiltered (gdb_stdlog
,
3345 "target_fileio_fstat (%d) = %d (%d)\n",
3346 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3353 target_fileio_close (int fd
, int *target_errno
)
3355 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3358 if (fh
->is_closed ())
3359 *target_errno
= EBADF
;
3362 if (fh
->target
!= NULL
)
3363 ret
= fh
->target
->fileio_close (fh
->target_fd
,
3367 release_fileio_fd (fd
, fh
);
3371 fprintf_unfiltered (gdb_stdlog
,
3372 "target_fileio_close (%d) = %d (%d)\n",
3373 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3380 target_fileio_unlink (struct inferior
*inf
, const char *filename
,
3383 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3385 int ret
= t
->fileio_unlink (inf
, filename
, target_errno
);
3387 if (ret
== -1 && *target_errno
== FILEIO_ENOSYS
)
3391 fprintf_unfiltered (gdb_stdlog
,
3392 "target_fileio_unlink (%d,%s)"
3394 inf
== NULL
? 0 : inf
->num
, filename
,
3395 ret
, ret
!= -1 ? 0 : *target_errno
);
3399 *target_errno
= FILEIO_ENOSYS
;
3405 gdb::optional
<std::string
>
3406 target_fileio_readlink (struct inferior
*inf
, const char *filename
,
3409 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3411 gdb::optional
<std::string
> ret
3412 = t
->fileio_readlink (inf
, filename
, target_errno
);
3414 if (!ret
.has_value () && *target_errno
== FILEIO_ENOSYS
)
3418 fprintf_unfiltered (gdb_stdlog
,
3419 "target_fileio_readlink (%d,%s)"
3421 inf
== NULL
? 0 : inf
->num
,
3422 filename
, ret
? ret
->c_str () : "(nil)",
3423 ret
? 0 : *target_errno
);
3427 *target_errno
= FILEIO_ENOSYS
;
3431 /* Like scoped_fd, but specific to target fileio. */
3433 class scoped_target_fd
3436 explicit scoped_target_fd (int fd
) noexcept
3441 ~scoped_target_fd ()
3447 target_fileio_close (m_fd
, &target_errno
);
3451 DISABLE_COPY_AND_ASSIGN (scoped_target_fd
);
3453 int get () const noexcept
3462 /* Read target file FILENAME, in the filesystem as seen by INF. If
3463 INF is NULL, use the filesystem seen by the debugger (GDB or, for
3464 remote targets, the remote stub). Store the result in *BUF_P and
3465 return the size of the transferred data. PADDING additional bytes
3466 are available in *BUF_P. This is a helper function for
3467 target_fileio_read_alloc; see the declaration of that function for
3468 more information. */
3471 target_fileio_read_alloc_1 (struct inferior
*inf
, const char *filename
,
3472 gdb_byte
**buf_p
, int padding
)
3474 size_t buf_alloc
, buf_pos
;
3479 scoped_target_fd
fd (target_fileio_open (inf
, filename
, FILEIO_O_RDONLY
,
3480 0700, false, &target_errno
));
3481 if (fd
.get () == -1)
3484 /* Start by reading up to 4K at a time. The target will throttle
3485 this number down if necessary. */
3487 buf
= (gdb_byte
*) xmalloc (buf_alloc
);
3491 n
= target_fileio_pread (fd
.get (), &buf
[buf_pos
],
3492 buf_alloc
- buf_pos
- padding
, buf_pos
,
3496 /* An error occurred. */
3502 /* Read all there was. */
3512 /* If the buffer is filling up, expand it. */
3513 if (buf_alloc
< buf_pos
* 2)
3516 buf
= (gdb_byte
*) xrealloc (buf
, buf_alloc
);
3526 target_fileio_read_alloc (struct inferior
*inf
, const char *filename
,
3529 return target_fileio_read_alloc_1 (inf
, filename
, buf_p
, 0);
3534 gdb::unique_xmalloc_ptr
<char>
3535 target_fileio_read_stralloc (struct inferior
*inf
, const char *filename
)
3539 LONGEST i
, transferred
;
3541 transferred
= target_fileio_read_alloc_1 (inf
, filename
, &buffer
, 1);
3542 bufstr
= (char *) buffer
;
3544 if (transferred
< 0)
3545 return gdb::unique_xmalloc_ptr
<char> (nullptr);
3547 if (transferred
== 0)
3548 return make_unique_xstrdup ("");
3550 bufstr
[transferred
] = 0;
3552 /* Check for embedded NUL bytes; but allow trailing NULs. */
3553 for (i
= strlen (bufstr
); i
< transferred
; i
++)
3556 warning (_("target file %s "
3557 "contained unexpected null characters"),
3562 return gdb::unique_xmalloc_ptr
<char> (bufstr
);
3567 default_region_ok_for_hw_watchpoint (struct target_ops
*self
,
3568 CORE_ADDR addr
, int len
)
3570 return (len
<= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT
);
3574 default_watchpoint_addr_within_range (struct target_ops
*target
,
3576 CORE_ADDR start
, int length
)
3578 return addr
>= start
&& addr
< start
+ length
;
3584 target_stack::find_beneath (const target_ops
*t
) const
3586 /* Look for a non-empty slot at stratum levels beneath T's. */
3587 for (int stratum
= t
->stratum () - 1; stratum
>= 0; --stratum
)
3588 if (m_stack
[stratum
] != NULL
)
3589 return m_stack
[stratum
];
3597 find_target_at (enum strata stratum
)
3599 return current_inferior ()->target_at (stratum
);
3607 target_announce_detach (int from_tty
)
3610 const char *exec_file
;
3615 exec_file
= get_exec_file (0);
3616 if (exec_file
== NULL
)
3619 pid
= inferior_ptid
.pid ();
3620 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
3621 target_pid_to_str (ptid_t (pid
)).c_str ());
3624 /* The inferior process has died. Long live the inferior! */
3627 generic_mourn_inferior (void)
3629 inferior
*inf
= current_inferior ();
3631 switch_to_no_thread ();
3633 /* Mark breakpoints uninserted in case something tries to delete a
3634 breakpoint while we delete the inferior's threads (which would
3635 fail, since the inferior is long gone). */
3636 mark_breakpoints_out ();
3639 exit_inferior (inf
);
3641 /* Note this wipes step-resume breakpoints, so needs to be done
3642 after exit_inferior, which ends up referencing the step-resume
3643 breakpoints through clear_thread_inferior_resources. */
3644 breakpoint_init_inferior (inf_exited
);
3646 registers_changed ();
3648 reopen_exec_file ();
3649 reinit_frame_cache ();
3651 if (deprecated_detach_hook
)
3652 deprecated_detach_hook ();
3655 /* Convert a normal process ID to a string. Returns the string in a
3659 normal_pid_to_str (ptid_t ptid
)
3661 return string_printf ("process %d", ptid
.pid ());
3665 default_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3667 return normal_pid_to_str (ptid
);
3670 /* Error-catcher for target_find_memory_regions. */
3672 dummy_find_memory_regions (struct target_ops
*self
,
3673 find_memory_region_ftype ignore1
, void *ignore2
)
3675 error (_("Command not implemented for this target."));
3679 /* Error-catcher for target_make_corefile_notes. */
3680 static gdb::unique_xmalloc_ptr
<char>
3681 dummy_make_corefile_notes (struct target_ops
*self
,
3682 bfd
*ignore1
, int *ignore2
)
3684 error (_("Command not implemented for this target."));
3688 #include "target-delegates.c"
3690 /* The initial current target, so that there is always a semi-valid
3693 static dummy_target the_dummy_target
;
3700 return &the_dummy_target
;
3703 static const target_info dummy_target_info
= {
3710 dummy_target::stratum () const
3712 return dummy_stratum
;
3716 debug_target::stratum () const
3718 return debug_stratum
;
3722 dummy_target::info () const
3724 return dummy_target_info
;
3728 debug_target::info () const
3730 return beneath ()->info ();
3736 target_close (struct target_ops
*targ
)
3738 gdb_assert (!current_inferior ()->target_is_pushed (targ
));
3740 fileio_handles_invalidate_target (targ
);
3745 fprintf_unfiltered (gdb_stdlog
, "target_close ()\n");
3749 target_thread_alive (ptid_t ptid
)
3751 return current_inferior ()->top_target ()->thread_alive (ptid
);
3755 target_update_thread_list (void)
3757 current_inferior ()->top_target ()->update_thread_list ();
3761 target_stop (ptid_t ptid
)
3763 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
3765 gdb_assert (!proc_target
->commit_resumed_state
);
3769 warning (_("May not interrupt or stop the target, ignoring attempt"));
3773 current_inferior ()->top_target ()->stop (ptid
);
3781 warning (_("May not interrupt or stop the target, ignoring attempt"));
3785 current_inferior ()->top_target ()->interrupt ();
3791 target_pass_ctrlc (void)
3793 /* Pass the Ctrl-C to the first target that has a thread
3795 for (inferior
*inf
: all_inferiors ())
3797 target_ops
*proc_target
= inf
->process_target ();
3798 if (proc_target
== NULL
)
3801 for (thread_info
*thr
: inf
->non_exited_threads ())
3803 /* A thread can be THREAD_STOPPED and executing, while
3804 running an infcall. */
3805 if (thr
->state
== THREAD_RUNNING
|| thr
->executing
)
3807 /* We can get here quite deep in target layers. Avoid
3808 switching thread context or anything that would
3809 communicate with the target (e.g., to fetch
3810 registers), or flushing e.g., the frame cache. We
3811 just switch inferior in order to be able to call
3812 through the target_stack. */
3813 scoped_restore_current_inferior restore_inferior
;
3814 set_current_inferior (inf
);
3815 current_inferior ()->top_target ()->pass_ctrlc ();
3825 default_target_pass_ctrlc (struct target_ops
*ops
)
3827 target_interrupt ();
3830 /* See target/target.h. */
3833 target_stop_and_wait (ptid_t ptid
)
3835 struct target_waitstatus status
;
3836 bool was_non_stop
= non_stop
;
3841 memset (&status
, 0, sizeof (status
));
3842 target_wait (ptid
, &status
, 0);
3844 non_stop
= was_non_stop
;
3847 /* See target/target.h. */
3850 target_continue_no_signal (ptid_t ptid
)
3852 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3855 /* See target/target.h. */
3858 target_continue (ptid_t ptid
, enum gdb_signal signal
)
3860 target_resume (ptid
, 0, signal
);
3863 /* Concatenate ELEM to LIST, a comma-separated list. */
3866 str_comma_list_concat_elem (std::string
*list
, const char *elem
)
3868 if (!list
->empty ())
3869 list
->append (", ");
3871 list
->append (elem
);
3874 /* Helper for target_options_to_string. If OPT is present in
3875 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3876 OPT is removed from TARGET_OPTIONS. */
3879 do_option (target_wait_flags
*target_options
, std::string
*ret
,
3880 target_wait_flag opt
, const char *opt_str
)
3882 if ((*target_options
& opt
) != 0)
3884 str_comma_list_concat_elem (ret
, opt_str
);
3885 *target_options
&= ~opt
;
3892 target_options_to_string (target_wait_flags target_options
)
3896 #define DO_TARG_OPTION(OPT) \
3897 do_option (&target_options, &ret, OPT, #OPT)
3899 DO_TARG_OPTION (TARGET_WNOHANG
);
3901 if (target_options
!= 0)
3902 str_comma_list_concat_elem (&ret
, "unknown???");
3908 target_fetch_registers (struct regcache
*regcache
, int regno
)
3910 current_inferior ()->top_target ()->fetch_registers (regcache
, regno
);
3912 regcache
->debug_print_register ("target_fetch_registers", regno
);
3916 target_store_registers (struct regcache
*regcache
, int regno
)
3918 if (!may_write_registers
)
3919 error (_("Writing to registers is not allowed (regno %d)"), regno
);
3921 current_inferior ()->top_target ()->store_registers (regcache
, regno
);
3924 regcache
->debug_print_register ("target_store_registers", regno
);
3929 target_core_of_thread (ptid_t ptid
)
3931 return current_inferior ()->top_target ()->core_of_thread (ptid
);
3935 simple_verify_memory (struct target_ops
*ops
,
3936 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
3938 LONGEST total_xfered
= 0;
3940 while (total_xfered
< size
)
3942 ULONGEST xfered_len
;
3943 enum target_xfer_status status
;
3945 ULONGEST howmuch
= std::min
<ULONGEST
> (sizeof (buf
), size
- total_xfered
);
3947 status
= target_xfer_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
3948 buf
, NULL
, lma
+ total_xfered
, howmuch
,
3950 if (status
== TARGET_XFER_OK
3951 && memcmp (data
+ total_xfered
, buf
, xfered_len
) == 0)
3953 total_xfered
+= xfered_len
;
3962 /* Default implementation of memory verification. */
3965 default_verify_memory (struct target_ops
*self
,
3966 const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3968 /* Start over from the top of the target stack. */
3969 return simple_verify_memory (current_inferior ()->top_target (),
3970 data
, memaddr
, size
);
3974 target_verify_memory (const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3976 target_ops
*target
= current_inferior ()->top_target ();
3978 return target
->verify_memory (data
, memaddr
, size
);
3981 /* The documentation for this function is in its prototype declaration in
3985 target_insert_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3986 enum target_hw_bp_type rw
)
3988 target_ops
*target
= current_inferior ()->top_target ();
3990 return target
->insert_mask_watchpoint (addr
, mask
, rw
);
3993 /* The documentation for this function is in its prototype declaration in
3997 target_remove_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3998 enum target_hw_bp_type rw
)
4000 target_ops
*target
= current_inferior ()->top_target ();
4002 return target
->remove_mask_watchpoint (addr
, mask
, rw
);
4005 /* The documentation for this function is in its prototype declaration
4009 target_masked_watch_num_registers (CORE_ADDR addr
, CORE_ADDR mask
)
4011 target_ops
*target
= current_inferior ()->top_target ();
4013 return target
->masked_watch_num_registers (addr
, mask
);
4016 /* The documentation for this function is in its prototype declaration
4020 target_ranged_break_num_registers (void)
4022 return current_inferior ()->top_target ()->ranged_break_num_registers ();
4027 struct btrace_target_info
*
4028 target_enable_btrace (ptid_t ptid
, const struct btrace_config
*conf
)
4030 return current_inferior ()->top_target ()->enable_btrace (ptid
, conf
);
4036 target_disable_btrace (struct btrace_target_info
*btinfo
)
4038 current_inferior ()->top_target ()->disable_btrace (btinfo
);
4044 target_teardown_btrace (struct btrace_target_info
*btinfo
)
4046 current_inferior ()->top_target ()->teardown_btrace (btinfo
);
4052 target_read_btrace (struct btrace_data
*btrace
,
4053 struct btrace_target_info
*btinfo
,
4054 enum btrace_read_type type
)
4056 target_ops
*target
= current_inferior ()->top_target ();
4058 return target
->read_btrace (btrace
, btinfo
, type
);
4063 const struct btrace_config
*
4064 target_btrace_conf (const struct btrace_target_info
*btinfo
)
4066 return current_inferior ()->top_target ()->btrace_conf (btinfo
);
4072 target_stop_recording (void)
4074 current_inferior ()->top_target ()->stop_recording ();
4080 target_save_record (const char *filename
)
4082 current_inferior ()->top_target ()->save_record (filename
);
4088 target_supports_delete_record ()
4090 return current_inferior ()->top_target ()->supports_delete_record ();
4096 target_delete_record (void)
4098 current_inferior ()->top_target ()->delete_record ();
4104 target_record_method (ptid_t ptid
)
4106 return current_inferior ()->top_target ()->record_method (ptid
);
4112 target_record_is_replaying (ptid_t ptid
)
4114 return current_inferior ()->top_target ()->record_is_replaying (ptid
);
4120 target_record_will_replay (ptid_t ptid
, int dir
)
4122 return current_inferior ()->top_target ()->record_will_replay (ptid
, dir
);
4128 target_record_stop_replaying (void)
4130 current_inferior ()->top_target ()->record_stop_replaying ();
4136 target_goto_record_begin (void)
4138 current_inferior ()->top_target ()->goto_record_begin ();
4144 target_goto_record_end (void)
4146 current_inferior ()->top_target ()->goto_record_end ();
4152 target_goto_record (ULONGEST insn
)
4154 current_inferior ()->top_target ()->goto_record (insn
);
4160 target_insn_history (int size
, gdb_disassembly_flags flags
)
4162 current_inferior ()->top_target ()->insn_history (size
, flags
);
4168 target_insn_history_from (ULONGEST from
, int size
,
4169 gdb_disassembly_flags flags
)
4171 current_inferior ()->top_target ()->insn_history_from (from
, size
, flags
);
4177 target_insn_history_range (ULONGEST begin
, ULONGEST end
,
4178 gdb_disassembly_flags flags
)
4180 current_inferior ()->top_target ()->insn_history_range (begin
, end
, flags
);
4186 target_call_history (int size
, record_print_flags flags
)
4188 current_inferior ()->top_target ()->call_history (size
, flags
);
4194 target_call_history_from (ULONGEST begin
, int size
, record_print_flags flags
)
4196 current_inferior ()->top_target ()->call_history_from (begin
, size
, flags
);
4202 target_call_history_range (ULONGEST begin
, ULONGEST end
, record_print_flags flags
)
4204 current_inferior ()->top_target ()->call_history_range (begin
, end
, flags
);
4209 const struct frame_unwind
*
4210 target_get_unwinder (void)
4212 return current_inferior ()->top_target ()->get_unwinder ();
4217 const struct frame_unwind
*
4218 target_get_tailcall_unwinder (void)
4220 return current_inferior ()->top_target ()->get_tailcall_unwinder ();
4226 target_prepare_to_generate_core (void)
4228 current_inferior ()->top_target ()->prepare_to_generate_core ();
4234 target_done_generating_core (void)
4236 current_inferior ()->top_target ()->done_generating_core ();
4241 static char targ_desc
[] =
4242 "Names of targets and files being debugged.\nShows the entire \
4243 stack of targets currently in use (including the exec-file,\n\
4244 core-file, and process, if any), as well as the symbol file name.";
4247 default_rcmd (struct target_ops
*self
, const char *command
,
4248 struct ui_file
*output
)
4250 error (_("\"monitor\" command not supported by this target."));
4254 do_monitor_command (const char *cmd
, int from_tty
)
4256 target_rcmd (cmd
, gdb_stdtarg
);
4259 /* Erases all the memory regions marked as flash. CMD and FROM_TTY are
4263 flash_erase_command (const char *cmd
, int from_tty
)
4265 /* Used to communicate termination of flash operations to the target. */
4266 bool found_flash_region
= false;
4267 struct gdbarch
*gdbarch
= target_gdbarch ();
4269 std::vector
<mem_region
> mem_regions
= target_memory_map ();
4271 /* Iterate over all memory regions. */
4272 for (const mem_region
&m
: mem_regions
)
4274 /* Is this a flash memory region? */
4275 if (m
.attrib
.mode
== MEM_FLASH
)
4277 found_flash_region
= true;
4278 target_flash_erase (m
.lo
, m
.hi
- m
.lo
);
4280 ui_out_emit_tuple
tuple_emitter (current_uiout
, "erased-regions");
4282 current_uiout
->message (_("Erasing flash memory region at address "));
4283 current_uiout
->field_core_addr ("address", gdbarch
, m
.lo
);
4284 current_uiout
->message (", size = ");
4285 current_uiout
->field_string ("size", hex_string (m
.hi
- m
.lo
));
4286 current_uiout
->message ("\n");
4290 /* Did we do any flash operations? If so, we need to finalize them. */
4291 if (found_flash_region
)
4292 target_flash_done ();
4294 current_uiout
->message (_("No flash memory regions found.\n"));
4297 /* Print the name of each layers of our target stack. */
4300 maintenance_print_target_stack (const char *cmd
, int from_tty
)
4302 printf_filtered (_("The current target stack is:\n"));
4304 for (target_ops
*t
= current_inferior ()->top_target ();
4308 if (t
->stratum () == debug_stratum
)
4310 printf_filtered (" - %s (%s)\n", t
->shortname (), t
->longname ());
4317 target_async (int enable
)
4319 infrun_async (enable
);
4320 current_inferior ()->top_target ()->async (enable
);
4326 target_thread_events (int enable
)
4328 current_inferior ()->top_target ()->thread_events (enable
);
4331 /* Controls if targets can report that they can/are async. This is
4332 just for maintainers to use when debugging gdb. */
4333 bool target_async_permitted
= true;
4335 /* The set command writes to this variable. If the inferior is
4336 executing, target_async_permitted is *not* updated. */
4337 static bool target_async_permitted_1
= true;
4340 maint_set_target_async_command (const char *args
, int from_tty
,
4341 struct cmd_list_element
*c
)
4343 if (have_live_inferiors ())
4345 target_async_permitted_1
= target_async_permitted
;
4346 error (_("Cannot change this setting while the inferior is running."));
4349 target_async_permitted
= target_async_permitted_1
;
4353 maint_show_target_async_command (struct ui_file
*file
, int from_tty
,
4354 struct cmd_list_element
*c
,
4357 fprintf_filtered (file
,
4358 _("Controlling the inferior in "
4359 "asynchronous mode is %s.\n"), value
);
4362 /* Return true if the target operates in non-stop mode even with "set
4366 target_always_non_stop_p (void)
4368 return current_inferior ()->top_target ()->always_non_stop_p ();
4374 target_is_non_stop_p ()
4377 || target_non_stop_enabled
== AUTO_BOOLEAN_TRUE
4378 || (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
4379 && target_always_non_stop_p ()))
4380 && target_can_async_p ());
4386 exists_non_stop_target ()
4388 if (target_is_non_stop_p ())
4391 scoped_restore_current_thread restore_thread
;
4393 for (inferior
*inf
: all_inferiors ())
4395 switch_to_inferior_no_thread (inf
);
4396 if (target_is_non_stop_p ())
4403 /* Controls if targets can report that they always run in non-stop
4404 mode. This is just for maintainers to use when debugging gdb. */
4405 enum auto_boolean target_non_stop_enabled
= AUTO_BOOLEAN_AUTO
;
4407 /* The set command writes to this variable. If the inferior is
4408 executing, target_non_stop_enabled is *not* updated. */
4409 static enum auto_boolean target_non_stop_enabled_1
= AUTO_BOOLEAN_AUTO
;
4411 /* Implementation of "maint set target-non-stop". */
4414 maint_set_target_non_stop_command (const char *args
, int from_tty
,
4415 struct cmd_list_element
*c
)
4417 if (have_live_inferiors ())
4419 target_non_stop_enabled_1
= target_non_stop_enabled
;
4420 error (_("Cannot change this setting while the inferior is running."));
4423 target_non_stop_enabled
= target_non_stop_enabled_1
;
4426 /* Implementation of "maint show target-non-stop". */
4429 maint_show_target_non_stop_command (struct ui_file
*file
, int from_tty
,
4430 struct cmd_list_element
*c
,
4433 if (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
)
4434 fprintf_filtered (file
,
4435 _("Whether the target is always in non-stop mode "
4436 "is %s (currently %s).\n"), value
,
4437 target_always_non_stop_p () ? "on" : "off");
4439 fprintf_filtered (file
,
4440 _("Whether the target is always in non-stop mode "
4441 "is %s.\n"), value
);
4444 /* Temporary copies of permission settings. */
4446 static bool may_write_registers_1
= true;
4447 static bool may_write_memory_1
= true;
4448 static bool may_insert_breakpoints_1
= true;
4449 static bool may_insert_tracepoints_1
= true;
4450 static bool may_insert_fast_tracepoints_1
= true;
4451 static bool may_stop_1
= true;
4453 /* Make the user-set values match the real values again. */
4456 update_target_permissions (void)
4458 may_write_registers_1
= may_write_registers
;
4459 may_write_memory_1
= may_write_memory
;
4460 may_insert_breakpoints_1
= may_insert_breakpoints
;
4461 may_insert_tracepoints_1
= may_insert_tracepoints
;
4462 may_insert_fast_tracepoints_1
= may_insert_fast_tracepoints
;
4463 may_stop_1
= may_stop
;
4466 /* The one function handles (most of) the permission flags in the same
4470 set_target_permissions (const char *args
, int from_tty
,
4471 struct cmd_list_element
*c
)
4473 if (target_has_execution ())
4475 update_target_permissions ();
4476 error (_("Cannot change this setting while the inferior is running."));
4479 /* Make the real values match the user-changed values. */
4480 may_write_registers
= may_write_registers_1
;
4481 may_insert_breakpoints
= may_insert_breakpoints_1
;
4482 may_insert_tracepoints
= may_insert_tracepoints_1
;
4483 may_insert_fast_tracepoints
= may_insert_fast_tracepoints_1
;
4484 may_stop
= may_stop_1
;
4485 update_observer_mode ();
4488 /* Set memory write permission independently of observer mode. */
4491 set_write_memory_permission (const char *args
, int from_tty
,
4492 struct cmd_list_element
*c
)
4494 /* Make the real values match the user-changed values. */
4495 may_write_memory
= may_write_memory_1
;
4496 update_observer_mode ();
4499 void _initialize_target ();
4502 _initialize_target ()
4504 the_debug_target
= new debug_target ();
4506 add_info ("target", info_target_command
, targ_desc
);
4507 add_info ("files", info_target_command
, targ_desc
);
4509 add_setshow_zuinteger_cmd ("target", class_maintenance
, &targetdebug
, _("\
4510 Set target debugging."), _("\
4511 Show target debugging."), _("\
4512 When non-zero, target debugging is enabled. Higher numbers are more\n\
4516 &setdebuglist
, &showdebuglist
);
4518 add_setshow_boolean_cmd ("trust-readonly-sections", class_support
,
4519 &trust_readonly
, _("\
4520 Set mode for reading from readonly sections."), _("\
4521 Show mode for reading from readonly sections."), _("\
4522 When this mode is on, memory reads from readonly sections (such as .text)\n\
4523 will be read from the object file instead of from the target. This will\n\
4524 result in significant performance improvement for remote targets."),
4526 show_trust_readonly
,
4527 &setlist
, &showlist
);
4529 add_com ("monitor", class_obscure
, do_monitor_command
,
4530 _("Send a command to the remote monitor (remote targets only)."));
4532 add_cmd ("target-stack", class_maintenance
, maintenance_print_target_stack
,
4533 _("Print the name of each layer of the internal target stack."),
4534 &maintenanceprintlist
);
4536 add_setshow_boolean_cmd ("target-async", no_class
,
4537 &target_async_permitted_1
, _("\
4538 Set whether gdb controls the inferior in asynchronous mode."), _("\
4539 Show whether gdb controls the inferior in asynchronous mode."), _("\
4540 Tells gdb whether to control the inferior in asynchronous mode."),
4541 maint_set_target_async_command
,
4542 maint_show_target_async_command
,
4543 &maintenance_set_cmdlist
,
4544 &maintenance_show_cmdlist
);
4546 add_setshow_auto_boolean_cmd ("target-non-stop", no_class
,
4547 &target_non_stop_enabled_1
, _("\
4548 Set whether gdb always controls the inferior in non-stop mode."), _("\
4549 Show whether gdb always controls the inferior in non-stop mode."), _("\
4550 Tells gdb whether to control the inferior in non-stop mode."),
4551 maint_set_target_non_stop_command
,
4552 maint_show_target_non_stop_command
,
4553 &maintenance_set_cmdlist
,
4554 &maintenance_show_cmdlist
);
4556 add_setshow_boolean_cmd ("may-write-registers", class_support
,
4557 &may_write_registers_1
, _("\
4558 Set permission to write into registers."), _("\
4559 Show permission to write into registers."), _("\
4560 When this permission is on, GDB may write into the target's registers.\n\
4561 Otherwise, any sort of write attempt will result in an error."),
4562 set_target_permissions
, NULL
,
4563 &setlist
, &showlist
);
4565 add_setshow_boolean_cmd ("may-write-memory", class_support
,
4566 &may_write_memory_1
, _("\
4567 Set permission to write into target memory."), _("\
4568 Show permission to write into target memory."), _("\
4569 When this permission is on, GDB may write into the target's memory.\n\
4570 Otherwise, any sort of write attempt will result in an error."),
4571 set_write_memory_permission
, NULL
,
4572 &setlist
, &showlist
);
4574 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support
,
4575 &may_insert_breakpoints_1
, _("\
4576 Set permission to insert breakpoints in the target."), _("\
4577 Show permission to insert breakpoints in the target."), _("\
4578 When this permission is on, GDB may insert breakpoints in the program.\n\
4579 Otherwise, any sort of insertion attempt will result in an error."),
4580 set_target_permissions
, NULL
,
4581 &setlist
, &showlist
);
4583 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support
,
4584 &may_insert_tracepoints_1
, _("\
4585 Set permission to insert tracepoints in the target."), _("\
4586 Show permission to insert tracepoints in the target."), _("\
4587 When this permission is on, GDB may insert tracepoints in the program.\n\
4588 Otherwise, any sort of insertion attempt will result in an error."),
4589 set_target_permissions
, NULL
,
4590 &setlist
, &showlist
);
4592 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support
,
4593 &may_insert_fast_tracepoints_1
, _("\
4594 Set permission to insert fast tracepoints in the target."), _("\
4595 Show permission to insert fast tracepoints in the target."), _("\
4596 When this permission is on, GDB may insert fast tracepoints.\n\
4597 Otherwise, any sort of insertion attempt will result in an error."),
4598 set_target_permissions
, NULL
,
4599 &setlist
, &showlist
);
4601 add_setshow_boolean_cmd ("may-interrupt", class_support
,
4603 Set permission to interrupt or signal the target."), _("\
4604 Show permission to interrupt or signal the target."), _("\
4605 When this permission is on, GDB may interrupt/stop the target's execution.\n\
4606 Otherwise, any attempt to interrupt or stop will be ignored."),
4607 set_target_permissions
, NULL
,
4608 &setlist
, &showlist
);
4610 add_com ("flash-erase", no_class
, flash_erase_command
,
4611 _("Erase all flash memory regions."));
4613 add_setshow_boolean_cmd ("auto-connect-native-target", class_support
,
4614 &auto_connect_native_target
, _("\
4615 Set whether GDB may automatically connect to the native target."), _("\
4616 Show whether GDB may automatically connect to the native target."), _("\
4617 When on, and GDB is not connected to a target yet, GDB\n\
4618 attempts \"run\" and other commands with the native target."),
4619 NULL
, show_auto_connect_native_target
,
4620 &setlist
, &showlist
);