1 /* Select target systems and architectures at runtime for GDB.
3 Copyright (C) 1990-2024 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "target-dcache.h"
28 #include "observable.h"
36 #include "target-descriptions.h"
37 #include "gdbthread.h"
40 #include "inline-frame.h"
41 #include "tracepoint.h"
42 #include "gdbsupport/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"
55 #include "cli/cli-decode.h"
57 static void generic_tls_error (void) ATTRIBUTE_NORETURN
;
59 static void default_rcmd (struct target_ops
*, const char *, struct ui_file
*);
61 static int default_verify_memory (struct target_ops
*self
,
63 CORE_ADDR memaddr
, ULONGEST size
);
65 static void tcomplain (void) ATTRIBUTE_NORETURN
;
67 /* Mapping between target_info objects (which have address identity)
68 and corresponding open/factory function/callback. Each add_target
69 call adds one entry to this map, and registers a "target
70 TARGET_NAME" command that when invoked calls the factory registered
71 here. The target_info object is associated with the command via
72 the command's context. */
73 static std::unordered_map
<const target_info
*, target_open_ftype
*>
76 /* The singleton debug target. */
78 static struct target_ops
*the_debug_target
;
80 /* Command list for target. */
82 static struct cmd_list_element
*targetlist
= NULL
;
86 bool trust_readonly
= false;
88 /* Nonzero if we should show true memory content including
89 memory breakpoint inserted by gdb. */
91 static int show_memory_breakpoints
= 0;
93 /* These globals control whether GDB attempts to perform these
94 operations; they are useful for targets that need to prevent
95 inadvertent disruption, such as in non-stop mode. */
97 bool may_write_registers
= true;
99 bool may_write_memory
= true;
101 bool may_insert_breakpoints
= true;
103 bool may_insert_tracepoints
= true;
105 bool may_insert_fast_tracepoints
= true;
107 bool may_stop
= true;
109 /* Non-zero if we want to see trace of target level stuff. */
111 static unsigned int targetdebug
= 0;
114 set_targetdebug (const char *args
, int from_tty
, struct cmd_list_element
*c
)
117 current_inferior ()->push_target (the_debug_target
);
119 current_inferior ()->unpush_target (the_debug_target
);
123 show_targetdebug (struct ui_file
*file
, int from_tty
,
124 struct cmd_list_element
*c
, const char *value
)
126 gdb_printf (file
, _("Target debugging is %s.\n"), value
);
132 for (target_ops
*t
= current_inferior ()->top_target ();
135 if (t
->has_memory ())
144 for (target_ops
*t
= current_inferior ()->top_target ();
154 target_has_registers ()
156 for (target_ops
*t
= current_inferior ()->top_target ();
159 if (t
->has_registers ())
166 target_has_execution (inferior
*inf
)
169 inf
= current_inferior ();
171 for (target_ops
*t
= inf
->top_target ();
173 t
= inf
->find_target_beneath (t
))
174 if (t
->has_execution (inf
))
183 return current_inferior ()->top_target ()->shortname ();
189 target_attach_no_wait ()
191 return current_inferior ()->top_target ()->attach_no_wait ();
197 target_post_attach (int pid
)
199 return current_inferior ()->top_target ()->post_attach (pid
);
205 target_prepare_to_store (regcache
*regcache
)
207 return current_inferior ()->top_target ()->prepare_to_store (regcache
);
213 target_supports_enable_disable_tracepoint ()
215 target_ops
*target
= current_inferior ()->top_target ();
217 return target
->supports_enable_disable_tracepoint ();
221 target_supports_string_tracing ()
223 return current_inferior ()->top_target ()->supports_string_tracing ();
229 target_supports_evaluation_of_breakpoint_conditions ()
231 target_ops
*target
= current_inferior ()->top_target ();
233 return target
->supports_evaluation_of_breakpoint_conditions ();
239 target_supports_dumpcore ()
241 return current_inferior ()->top_target ()->supports_dumpcore ();
247 target_dumpcore (const char *filename
)
249 return current_inferior ()->top_target ()->dumpcore (filename
);
255 target_can_run_breakpoint_commands ()
257 return current_inferior ()->top_target ()->can_run_breakpoint_commands ();
265 return current_inferior ()->top_target ()->files_info ();
271 target_insert_fork_catchpoint (int pid
)
273 return current_inferior ()->top_target ()->insert_fork_catchpoint (pid
);
279 target_remove_fork_catchpoint (int pid
)
281 return current_inferior ()->top_target ()->remove_fork_catchpoint (pid
);
287 target_insert_vfork_catchpoint (int pid
)
289 return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid
);
295 target_remove_vfork_catchpoint (int pid
)
297 return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid
);
303 target_insert_exec_catchpoint (int pid
)
305 return current_inferior ()->top_target ()->insert_exec_catchpoint (pid
);
311 target_remove_exec_catchpoint (int pid
)
313 return current_inferior ()->top_target ()->remove_exec_catchpoint (pid
);
319 target_set_syscall_catchpoint (int pid
, bool needed
, int any_count
,
320 gdb::array_view
<const int> syscall_counts
)
322 target_ops
*target
= current_inferior ()->top_target ();
324 return target
->set_syscall_catchpoint (pid
, needed
, any_count
,
331 target_rcmd (const char *command
, struct ui_file
*outbuf
)
333 return current_inferior ()->top_target ()->rcmd (command
, outbuf
);
339 target_can_lock_scheduler ()
341 target_ops
*target
= current_inferior ()->top_target ();
343 return (target
->get_thread_control_capabilities ()& tc_schedlock
) != 0;
349 target_can_async_p ()
351 return target_can_async_p (current_inferior ()->top_target ());
357 target_can_async_p (struct target_ops
*target
)
359 if (!target_async_permitted
)
361 return target
->can_async_p ();
369 bool result
= current_inferior ()->top_target ()->is_async_p ();
370 gdb_assert (target_async_permitted
|| !result
);
375 target_execution_direction ()
377 return current_inferior ()->top_target ()->execution_direction ();
383 target_extra_thread_info (thread_info
*tp
)
385 return current_inferior ()->top_target ()->extra_thread_info (tp
);
391 target_pid_to_exec_file (int pid
)
393 return current_inferior ()->top_target ()->pid_to_exec_file (pid
);
399 target_thread_architecture (ptid_t ptid
)
401 return current_inferior ()->top_target ()->thread_architecture (ptid
);
407 target_find_memory_regions (find_memory_region_ftype func
, void *data
)
409 return current_inferior ()->top_target ()->find_memory_regions (func
, data
);
414 gdb::unique_xmalloc_ptr
<char>
415 target_make_corefile_notes (bfd
*bfd
, int *size_p
)
417 return current_inferior ()->top_target ()->make_corefile_notes (bfd
, size_p
);
421 target_get_bookmark (const char *args
, int from_tty
)
423 return current_inferior ()->top_target ()->get_bookmark (args
, from_tty
);
427 target_goto_bookmark (const gdb_byte
*arg
, int from_tty
)
429 return current_inferior ()->top_target ()->goto_bookmark (arg
, from_tty
);
435 target_stopped_by_watchpoint ()
437 return current_inferior ()->top_target ()->stopped_by_watchpoint ();
443 target_stopped_by_sw_breakpoint ()
445 return current_inferior ()->top_target ()->stopped_by_sw_breakpoint ();
449 target_supports_stopped_by_sw_breakpoint ()
451 target_ops
*target
= current_inferior ()->top_target ();
453 return target
->supports_stopped_by_sw_breakpoint ();
457 target_stopped_by_hw_breakpoint ()
459 return current_inferior ()->top_target ()->stopped_by_hw_breakpoint ();
463 target_supports_stopped_by_hw_breakpoint ()
465 target_ops
*target
= current_inferior ()->top_target ();
467 return target
->supports_stopped_by_hw_breakpoint ();
473 target_have_steppable_watchpoint ()
475 return current_inferior ()->top_target ()->have_steppable_watchpoint ();
481 target_can_use_hardware_watchpoint (bptype type
, int cnt
, int othertype
)
483 target_ops
*target
= current_inferior ()->top_target ();
485 return target
->can_use_hw_breakpoint (type
, cnt
, othertype
);
491 target_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
493 target_ops
*target
= current_inferior ()->top_target ();
495 return target
->region_ok_for_hw_watchpoint (addr
, len
);
500 target_can_do_single_step ()
502 return current_inferior ()->top_target ()->can_do_single_step ();
508 target_insert_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
511 target_ops
*target
= current_inferior ()->top_target ();
513 return target
->insert_watchpoint (addr
, len
, type
, cond
);
519 target_remove_watchpoint (CORE_ADDR addr
, int len
, target_hw_bp_type type
,
522 target_ops
*target
= current_inferior ()->top_target ();
524 return target
->remove_watchpoint (addr
, len
, type
, cond
);
530 target_insert_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
532 target_ops
*target
= current_inferior ()->top_target ();
534 return target
->insert_hw_breakpoint (gdbarch
, bp_tgt
);
540 target_remove_hw_breakpoint (gdbarch
*gdbarch
, bp_target_info
*bp_tgt
)
542 target_ops
*target
= current_inferior ()->top_target ();
544 return target
->remove_hw_breakpoint (gdbarch
, bp_tgt
);
550 target_can_accel_watchpoint_condition (CORE_ADDR addr
, int len
, int type
,
553 target_ops
*target
= current_inferior ()->top_target ();
555 return target
->can_accel_watchpoint_condition (addr
, len
, type
, cond
);
561 target_can_execute_reverse ()
563 return current_inferior ()->top_target ()->can_execute_reverse ();
567 target_get_ada_task_ptid (long lwp
, ULONGEST tid
)
569 return current_inferior ()->top_target ()->get_ada_task_ptid (lwp
, tid
);
573 target_filesystem_is_local ()
575 return current_inferior ()->top_target ()->filesystem_is_local ();
581 return current_inferior ()->top_target ()->trace_init ();
585 target_download_tracepoint (bp_location
*location
)
587 return current_inferior ()->top_target ()->download_tracepoint (location
);
591 target_can_download_tracepoint ()
593 return current_inferior ()->top_target ()->can_download_tracepoint ();
597 target_download_trace_state_variable (const trace_state_variable
&tsv
)
599 target_ops
*target
= current_inferior ()->top_target ();
601 return target
->download_trace_state_variable (tsv
);
605 target_enable_tracepoint (bp_location
*loc
)
607 return current_inferior ()->top_target ()->enable_tracepoint (loc
);
611 target_disable_tracepoint (bp_location
*loc
)
613 return current_inferior ()->top_target ()->disable_tracepoint (loc
);
617 target_trace_start ()
619 return current_inferior ()->top_target ()->trace_start ();
623 target_trace_set_readonly_regions ()
625 return current_inferior ()->top_target ()->trace_set_readonly_regions ();
629 target_get_trace_status (trace_status
*ts
)
631 return current_inferior ()->top_target ()->get_trace_status (ts
);
635 target_get_tracepoint_status (tracepoint
*tp
, uploaded_tp
*utp
)
637 return current_inferior ()->top_target ()->get_tracepoint_status (tp
, utp
);
643 return current_inferior ()->top_target ()->trace_stop ();
647 target_trace_find (trace_find_type type
, int num
,
648 CORE_ADDR addr1
, CORE_ADDR addr2
, int *tpp
)
650 target_ops
*target
= current_inferior ()->top_target ();
652 return target
->trace_find (type
, num
, addr1
, addr2
, tpp
);
656 target_get_trace_state_variable_value (int tsv
, LONGEST
*val
)
658 target_ops
*target
= current_inferior ()->top_target ();
660 return target
->get_trace_state_variable_value (tsv
, val
);
664 target_save_trace_data (const char *filename
)
666 return current_inferior ()->top_target ()->save_trace_data (filename
);
670 target_upload_tracepoints (uploaded_tp
**utpp
)
672 return current_inferior ()->top_target ()->upload_tracepoints (utpp
);
676 target_upload_trace_state_variables (uploaded_tsv
**utsvp
)
678 target_ops
*target
= current_inferior ()->top_target ();
680 return target
->upload_trace_state_variables (utsvp
);
684 target_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
686 target_ops
*target
= current_inferior ()->top_target ();
688 return target
->get_raw_trace_data (buf
, offset
, len
);
692 target_get_min_fast_tracepoint_insn_len ()
694 target_ops
*target
= current_inferior ()->top_target ();
696 return target
->get_min_fast_tracepoint_insn_len ();
700 target_set_disconnected_tracing (int val
)
702 return current_inferior ()->top_target ()->set_disconnected_tracing (val
);
706 target_set_circular_trace_buffer (int val
)
708 return current_inferior ()->top_target ()->set_circular_trace_buffer (val
);
712 target_set_trace_buffer_size (LONGEST val
)
714 return current_inferior ()->top_target ()->set_trace_buffer_size (val
);
718 target_set_trace_notes (const char *user
, const char *notes
,
719 const char *stopnotes
)
721 target_ops
*target
= current_inferior ()->top_target ();
723 return target
->set_trace_notes (user
, notes
, stopnotes
);
727 target_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
729 return current_inferior ()->top_target ()->get_tib_address (ptid
, addr
);
733 target_set_permissions ()
735 return current_inferior ()->top_target ()->set_permissions ();
739 target_static_tracepoint_marker_at (CORE_ADDR addr
,
740 static_tracepoint_marker
*marker
)
742 target_ops
*target
= current_inferior ()->top_target ();
744 return target
->static_tracepoint_marker_at (addr
, marker
);
747 std::vector
<static_tracepoint_marker
>
748 target_static_tracepoint_markers_by_strid (const char *marker_id
)
750 target_ops
*target
= current_inferior ()->top_target ();
752 return target
->static_tracepoint_markers_by_strid (marker_id
);
756 target_traceframe_info ()
758 return current_inferior ()->top_target ()->traceframe_info ();
762 target_use_agent (bool use
)
764 return current_inferior ()->top_target ()->use_agent (use
);
768 target_can_use_agent ()
770 return current_inferior ()->top_target ()->can_use_agent ();
774 target_augmented_libraries_svr4_read ()
776 return current_inferior ()->top_target ()->augmented_libraries_svr4_read ();
780 target_supports_memory_tagging ()
782 return current_inferior ()->top_target ()->supports_memory_tagging ();
786 target_fetch_memtags (CORE_ADDR address
, size_t len
, gdb::byte_vector
&tags
,
789 return current_inferior ()->top_target ()->fetch_memtags (address
, len
, tags
, type
);
793 target_store_memtags (CORE_ADDR address
, size_t len
,
794 const gdb::byte_vector
&tags
, int type
)
796 return current_inferior ()->top_target ()->store_memtags (address
, len
, tags
, type
);
800 target_fetch_x86_xsave_layout ()
802 return current_inferior ()->top_target ()->fetch_x86_xsave_layout ();
806 target_log_command (const char *p
)
808 return current_inferior ()->top_target ()->log_command (p
);
811 /* This is used to implement the various target commands. */
814 open_target (const char *args
, int from_tty
, struct cmd_list_element
*command
)
816 auto *ti
= static_cast<target_info
*> (command
->context ());
817 target_open_ftype
*func
= target_factories
[ti
];
820 gdb_printf (gdb_stdlog
, "-> %s->open (...)\n",
823 func (args
, from_tty
);
826 gdb_printf (gdb_stdlog
, "<- %s->open (%s, %d)\n",
827 ti
->shortname
, args
, from_tty
);
833 add_target (const target_info
&t
, target_open_ftype
*func
,
834 completer_ftype
*completer
)
836 struct cmd_list_element
*c
;
838 auto &func_slot
= target_factories
[&t
];
839 if (func_slot
!= nullptr)
840 internal_error (_("target already added (\"%s\")."), t
.shortname
);
843 if (targetlist
== NULL
)
844 add_basic_prefix_cmd ("target", class_run
, _("\
845 Connect to a target machine or process.\n\
846 The first argument is the type or protocol of the target machine.\n\
847 Remaining arguments are interpreted by the target protocol. For more\n\
848 information on the arguments for a particular protocol, type\n\
849 `help target ' followed by the protocol name."),
850 &targetlist
, 0, &cmdlist
);
851 c
= add_cmd (t
.shortname
, no_class
, t
.doc
, &targetlist
);
852 c
->set_context ((void *) &t
);
853 c
->func
= open_target
;
854 if (completer
!= NULL
)
855 set_cmd_completer (c
, completer
);
861 add_deprecated_target_alias (const target_info
&tinfo
, const char *alias
)
863 struct cmd_list_element
*c
;
865 /* If we use add_alias_cmd, here, we do not get the deprecated warning,
867 c
= add_cmd (alias
, no_class
, tinfo
.doc
, &targetlist
);
868 c
->func
= open_target
;
869 c
->set_context ((void *) &tinfo
);
870 gdb::unique_xmalloc_ptr
<char> alt
871 = xstrprintf ("target %s", tinfo
.shortname
);
872 deprecate_cmd (c
, alt
.release ());
881 /* If the commit_resume_state of the to-be-killed-inferior's process stratum
882 is true, and this inferior is the last live inferior with resumed threads
883 of that target, then we want to leave commit_resume_state to false, as the
884 target won't have any resumed threads anymore. We achieve this with
885 this scoped_disable_commit_resumed. On construction, it will set the flag
886 to false. On destruction, it will only set it to true if there are resumed
888 scoped_disable_commit_resumed
disable ("killing");
889 current_inferior ()->top_target ()->kill ();
893 target_load (const char *arg
, int from_tty
)
895 target_dcache_invalidate (current_program_space
->aspace
);
896 current_inferior ()->top_target ()->load (arg
, from_tty
);
901 target_terminal_state
target_terminal::m_terminal_state
902 = target_terminal_state::is_ours
;
904 /* See target/target.h. */
907 target_terminal::init (void)
909 current_inferior ()->top_target ()->terminal_init ();
911 m_terminal_state
= target_terminal_state::is_ours
;
914 /* See target/target.h. */
917 target_terminal::inferior (void)
919 struct ui
*ui
= current_ui
;
921 /* A background resume (``run&'') should leave GDB in control of the
923 if (ui
->prompt_state
!= PROMPT_BLOCKED
)
926 /* Since we always run the inferior in the main console (unless "set
927 inferior-tty" is in effect), when some UI other than the main one
928 calls target_terminal::inferior, then we leave the main UI's
929 terminal settings as is. */
933 /* If GDB is resuming the inferior in the foreground, install
934 inferior's terminal modes. */
936 struct inferior
*inf
= current_inferior ();
938 if (inf
->terminal_state
!= target_terminal_state::is_inferior
)
940 current_inferior ()->top_target ()->terminal_inferior ();
941 inf
->terminal_state
= target_terminal_state::is_inferior
;
944 m_terminal_state
= target_terminal_state::is_inferior
;
946 /* If the user hit C-c before, pretend that it was hit right
948 if (check_quit_flag ())
949 target_pass_ctrlc ();
952 /* See target/target.h. */
955 target_terminal::restore_inferior (void)
957 struct ui
*ui
= current_ui
;
959 /* See target_terminal::inferior(). */
960 if (ui
->prompt_state
!= PROMPT_BLOCKED
|| ui
!= main_ui
)
963 /* Restore the terminal settings of inferiors that were in the
964 foreground but are now ours_for_output due to a temporary
965 target_target::ours_for_output() call. */
968 scoped_restore_current_inferior restore_inferior
;
970 for (::inferior
*inf
: all_inferiors ())
972 if (inf
->terminal_state
== target_terminal_state::is_ours_for_output
)
974 set_current_inferior (inf
);
975 current_inferior ()->top_target ()->terminal_inferior ();
976 inf
->terminal_state
= target_terminal_state::is_inferior
;
981 m_terminal_state
= target_terminal_state::is_inferior
;
983 /* If the user hit C-c before, pretend that it was hit right
985 if (check_quit_flag ())
986 target_pass_ctrlc ();
989 /* Switch terminal state to DESIRED_STATE, either is_ours, or
990 is_ours_for_output. */
993 target_terminal_is_ours_kind (target_terminal_state desired_state
)
995 scoped_restore_current_inferior restore_inferior
;
997 /* Must do this in two passes. First, have all inferiors save the
998 current terminal settings. Then, after all inferiors have add a
999 chance to safely save the terminal settings, restore GDB's
1000 terminal settings. */
1002 for (inferior
*inf
: all_inferiors ())
1004 if (inf
->terminal_state
== target_terminal_state::is_inferior
)
1006 set_current_inferior (inf
);
1007 current_inferior ()->top_target ()->terminal_save_inferior ();
1011 for (inferior
*inf
: all_inferiors ())
1013 /* Note we don't check is_inferior here like above because we
1014 need to handle 'is_ours_for_output -> is_ours' too. Careful
1015 to never transition from 'is_ours' to 'is_ours_for_output',
1017 if (inf
->terminal_state
!= target_terminal_state::is_ours
1018 && inf
->terminal_state
!= desired_state
)
1020 set_current_inferior (inf
);
1021 if (desired_state
== target_terminal_state::is_ours
)
1022 current_inferior ()->top_target ()->terminal_ours ();
1023 else if (desired_state
== target_terminal_state::is_ours_for_output
)
1024 current_inferior ()->top_target ()->terminal_ours_for_output ();
1026 gdb_assert_not_reached ("unhandled desired state");
1027 inf
->terminal_state
= desired_state
;
1032 /* See target/target.h. */
1035 target_terminal::ours ()
1037 struct ui
*ui
= current_ui
;
1039 /* See target_terminal::inferior. */
1043 if (m_terminal_state
== target_terminal_state::is_ours
)
1046 target_terminal_is_ours_kind (target_terminal_state::is_ours
);
1047 m_terminal_state
= target_terminal_state::is_ours
;
1050 /* See target/target.h. */
1053 target_terminal::ours_for_output ()
1055 struct ui
*ui
= current_ui
;
1057 /* See target_terminal::inferior. */
1061 if (!target_terminal::is_inferior ())
1064 target_terminal_is_ours_kind (target_terminal_state::is_ours_for_output
);
1065 target_terminal::m_terminal_state
= target_terminal_state::is_ours_for_output
;
1068 /* See target/target.h. */
1071 target_terminal::info (const char *arg
, int from_tty
)
1073 current_inferior ()->top_target ()->terminal_info (arg
, from_tty
);
1079 target_supports_terminal_ours (void)
1081 /* The current top target is the target at the top of the target
1082 stack of the current inferior. While normally there's always an
1083 inferior, we must check for nullptr here because we can get here
1084 very early during startup, before the initial inferior is first
1086 inferior
*inf
= current_inferior ();
1090 return inf
->top_target ()->supports_terminal_ours ();
1096 error (_("You can't do that when your target is `%s'"),
1097 current_inferior ()->top_target ()->shortname ());
1103 error (_("You can't do that without a process to debug."));
1107 default_terminal_info (struct target_ops
*self
, const char *args
, int from_tty
)
1109 gdb_printf (_("No saved terminal information.\n"));
1112 /* A default implementation for the to_get_ada_task_ptid target method.
1114 This function builds the PTID by using both LWP and TID as part of
1115 the PTID lwp and tid elements. The pid used is the pid of the
1119 default_get_ada_task_ptid (struct target_ops
*self
, long lwp
, ULONGEST tid
)
1121 return ptid_t (inferior_ptid
.pid (), lwp
, tid
);
1124 static enum exec_direction_kind
1125 default_execution_direction (struct target_ops
*self
)
1127 if (!target_can_execute_reverse ())
1128 return EXEC_FORWARD
;
1129 else if (!target_can_async_p ())
1130 return EXEC_FORWARD
;
1132 gdb_assert_not_reached ("\
1133 to_execution_direction must be implemented for reverse async");
1139 target_ops_ref_policy::decref (target_ops
*t
)
1142 if (t
->refcount () == 0)
1144 if (t
->stratum () == process_stratum
)
1145 connection_list_remove (as_process_stratum_target (t
));
1147 for (inferior
*inf
: all_inferiors ())
1148 gdb_assert (!inf
->target_is_pushed (t
));
1150 fileio_handles_invalidate_target (t
);
1155 gdb_printf (gdb_stdlog
, "closing target\n");
1162 target_stack::push (target_ops
*t
)
1164 /* We must create a new reference first. It is possible that T is
1165 already pushed on this target stack, in which case we will first
1166 unpush it below, before re-pushing it. If we don't increment the
1167 reference count now, then when we unpush it, we might end up deleting
1168 T, which is not good. */
1169 auto ref
= target_ops_ref::new_reference (t
);
1171 strata stratum
= t
->stratum ();
1173 /* If there's already a target at this stratum, remove it. */
1175 if (m_stack
[stratum
].get () != nullptr)
1176 unpush (m_stack
[stratum
].get ());
1178 /* Now add the new one. */
1179 m_stack
[stratum
] = std::move (ref
);
1181 if (m_top
< stratum
)
1184 if (stratum
== process_stratum
)
1185 connection_list_add (as_process_stratum_target (t
));
1191 target_stack::unpush (target_ops
*t
)
1193 gdb_assert (t
!= NULL
);
1195 strata stratum
= t
->stratum ();
1197 if (stratum
== dummy_stratum
)
1198 internal_error (_("Attempt to unpush the dummy target"));
1200 /* Look for the specified target. Note that a target can only occur
1201 once in the target stack. */
1203 if (m_stack
[stratum
] != t
)
1205 /* If T wasn't pushed, quit. Only open targets should be
1210 if (m_top
== stratum
)
1211 m_top
= this->find_beneath (t
)->stratum ();
1213 /* Move the target reference off the target stack, this sets the pointer
1214 held in m_stack to nullptr, and places the reference in ref. When
1215 ref goes out of scope its reference count will be decremented, which
1216 might cause the target to close.
1218 We have to do it this way, and not just set the value in m_stack to
1219 nullptr directly, because doing so would decrement the reference
1220 count first, which might close the target, and closing the target
1221 does a check that the target is not on any inferiors target_stack. */
1222 auto ref
= std::move (m_stack
[stratum
]);
1228 target_unpusher::operator() (struct target_ops
*ops
) const
1230 current_inferior ()->unpush_target (ops
);
1233 /* Default implementation of to_get_thread_local_address. */
1236 generic_tls_error (void)
1238 throw_error (TLS_GENERIC_ERROR
,
1239 _("Cannot find thread-local variables on this target"));
1242 /* Using the objfile specified in OBJFILE, find the address for the
1243 current thread's thread-local storage with offset OFFSET. */
1245 target_translate_tls_address (struct objfile
*objfile
, CORE_ADDR offset
)
1247 volatile CORE_ADDR addr
= 0;
1248 struct target_ops
*target
= current_inferior ()->top_target ();
1249 gdbarch
*gdbarch
= current_inferior ()->arch ();
1251 /* If OBJFILE is a separate debug object file, look for the
1252 original object file. */
1253 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
1254 objfile
= objfile
->separate_debug_objfile_backlink
;
1256 if (gdbarch_fetch_tls_load_module_address_p (gdbarch
))
1258 ptid_t ptid
= inferior_ptid
;
1264 /* Fetch the load module address for this objfile. */
1265 lm_addr
= gdbarch_fetch_tls_load_module_address (gdbarch
,
1268 if (gdbarch_get_thread_local_address_p (gdbarch
))
1269 addr
= gdbarch_get_thread_local_address (gdbarch
, ptid
, lm_addr
,
1272 addr
= target
->get_thread_local_address (ptid
, lm_addr
, offset
);
1274 /* If an error occurred, print TLS related messages here. Otherwise,
1275 throw the error to some higher catcher. */
1276 catch (const gdb_exception
&ex
)
1278 int objfile_is_library
= (objfile
->flags
& OBJF_SHARED
);
1282 case TLS_NO_LIBRARY_SUPPORT_ERROR
:
1283 error (_("Cannot find thread-local variables "
1284 "in this thread library."));
1286 case TLS_LOAD_MODULE_NOT_FOUND_ERROR
:
1287 if (objfile_is_library
)
1288 error (_("Cannot find shared library `%s' in dynamic"
1289 " linker's load module list"), objfile_name (objfile
));
1291 error (_("Cannot find executable file `%s' in dynamic"
1292 " linker's load module list"), objfile_name (objfile
));
1294 case TLS_NOT_ALLOCATED_YET_ERROR
:
1295 if (objfile_is_library
)
1296 error (_("The inferior has not yet allocated storage for"
1297 " thread-local variables in\n"
1298 "the shared library `%s'\n"
1300 objfile_name (objfile
),
1301 target_pid_to_str (ptid
).c_str ());
1303 error (_("The inferior has not yet allocated storage for"
1304 " thread-local variables in\n"
1305 "the executable `%s'\n"
1307 objfile_name (objfile
),
1308 target_pid_to_str (ptid
).c_str ());
1310 case TLS_GENERIC_ERROR
:
1311 if (objfile_is_library
)
1312 error (_("Cannot find thread-local storage for %s, "
1313 "shared library %s:\n%s"),
1314 target_pid_to_str (ptid
).c_str (),
1315 objfile_name (objfile
), ex
.what ());
1317 error (_("Cannot find thread-local storage for %s, "
1318 "executable file %s:\n%s"),
1319 target_pid_to_str (ptid
).c_str (),
1320 objfile_name (objfile
), ex
.what ());
1329 error (_("Cannot find thread-local variables on this target"));
1335 target_xfer_status_to_string (enum target_xfer_status status
)
1337 #define CASE(X) case X: return #X
1340 CASE(TARGET_XFER_E_IO
);
1341 CASE(TARGET_XFER_UNAVAILABLE
);
1349 const std::vector
<target_section
> *
1350 target_get_section_table (struct target_ops
*target
)
1352 return target
->get_section_table ();
1355 /* Find a section containing ADDR. */
1357 const struct target_section
*
1358 target_section_by_addr (struct target_ops
*target
, CORE_ADDR addr
)
1360 const std::vector
<target_section
> *table
= target_get_section_table (target
);
1365 for (const target_section
&secp
: *table
)
1367 if (addr
>= secp
.addr
&& addr
< secp
.endaddr
)
1375 const std::vector
<target_section
> *
1376 default_get_section_table ()
1378 return ¤t_program_space
->target_sections ();
1381 /* Helper for the memory xfer routines. Checks the attributes of the
1382 memory region of MEMADDR against the read or write being attempted.
1383 If the access is permitted returns true, otherwise returns false.
1384 REGION_P is an optional output parameter. If not-NULL, it is
1385 filled with a pointer to the memory region of MEMADDR. REG_LEN
1386 returns LEN trimmed to the end of the region. This is how much the
1387 caller can continue requesting, if the access is permitted. A
1388 single xfer request must not straddle memory region boundaries. */
1391 memory_xfer_check_region (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1392 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*reg_len
,
1393 struct mem_region
**region_p
)
1395 struct mem_region
*region
;
1397 region
= lookup_mem_region (memaddr
);
1399 if (region_p
!= NULL
)
1402 switch (region
->attrib
.mode
)
1405 if (writebuf
!= NULL
)
1410 if (readbuf
!= NULL
)
1415 /* We only support writing to flash during "load" for now. */
1416 if (writebuf
!= NULL
)
1417 error (_("Writing to flash memory forbidden in this context"));
1424 /* region->hi == 0 means there's no upper bound. */
1425 if (memaddr
+ len
< region
->hi
|| region
->hi
== 0)
1428 *reg_len
= region
->hi
- memaddr
;
1433 /* Read memory from more than one valid target. A core file, for
1434 instance, could have some of memory but delegate other bits to
1435 the target below it. So, we must manually try all targets. */
1437 enum target_xfer_status
1438 raw_memory_xfer_partial (struct target_ops
*ops
, gdb_byte
*readbuf
,
1439 const gdb_byte
*writebuf
, ULONGEST memaddr
, LONGEST len
,
1440 ULONGEST
*xfered_len
)
1442 enum target_xfer_status res
;
1446 res
= ops
->xfer_partial (TARGET_OBJECT_MEMORY
, NULL
,
1447 readbuf
, writebuf
, memaddr
, len
,
1449 if (res
== TARGET_XFER_OK
)
1452 /* Stop if the target reports that the memory is not available. */
1453 if (res
== TARGET_XFER_UNAVAILABLE
)
1456 /* Don't continue past targets which have all the memory.
1457 At one time, this code was necessary to read data from
1458 executables / shared libraries when data for the requested
1459 addresses weren't available in the core file. But now the
1460 core target handles this case itself. */
1461 if (ops
->has_all_memory ())
1464 ops
= ops
->beneath ();
1466 while (ops
!= NULL
);
1468 /* The cache works at the raw memory level. Make sure the cache
1469 gets updated with raw contents no matter what kind of memory
1470 object was originally being written. Note we do write-through
1471 first, so that if it fails, we don't write to the cache contents
1472 that never made it to the target. */
1473 if (writebuf
!= NULL
1474 && inferior_ptid
!= null_ptid
1475 && target_dcache_init_p (current_program_space
->aspace
)
1476 && (stack_cache_enabled_p () || code_cache_enabled_p ()))
1478 DCACHE
*dcache
= target_dcache_get (current_program_space
->aspace
);
1480 /* Note that writing to an area of memory which wasn't present
1481 in the cache doesn't cause it to be loaded in. */
1482 dcache_update (dcache
, res
, memaddr
, writebuf
, *xfered_len
);
1488 /* Perform a partial memory transfer.
1489 For docs see target.h, to_xfer_partial. */
1491 static enum target_xfer_status
1492 memory_xfer_partial_1 (struct target_ops
*ops
, enum target_object object
,
1493 gdb_byte
*readbuf
, const gdb_byte
*writebuf
, ULONGEST memaddr
,
1494 ULONGEST len
, ULONGEST
*xfered_len
)
1496 enum target_xfer_status res
;
1498 struct mem_region
*region
;
1499 struct inferior
*inf
;
1501 /* For accesses to unmapped overlay sections, read directly from
1502 files. Must do this first, as MEMADDR may need adjustment. */
1503 if (readbuf
!= NULL
&& overlay_debugging
)
1505 struct obj_section
*section
= find_pc_overlay (memaddr
);
1507 if (pc_in_unmapped_range (memaddr
, section
))
1509 const std::vector
<target_section
> *table
= target_get_section_table (ops
);
1510 const char *section_name
= section
->the_bfd_section
->name
;
1512 memaddr
= overlay_mapped_address (memaddr
, section
);
1514 auto match_cb
= [=] (const struct target_section
*s
)
1516 return (strcmp (section_name
, s
->the_bfd_section
->name
) == 0);
1519 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1520 memaddr
, len
, xfered_len
,
1525 /* Try the executable files, if "trust-readonly-sections" is set. */
1526 if (readbuf
!= NULL
&& trust_readonly
)
1528 const struct target_section
*secp
1529 = target_section_by_addr (ops
, memaddr
);
1531 && (bfd_section_flags (secp
->the_bfd_section
) & SEC_READONLY
))
1533 const std::vector
<target_section
> *table
= target_get_section_table (ops
);
1534 return section_table_xfer_memory_partial (readbuf
, writebuf
,
1535 memaddr
, len
, xfered_len
,
1540 /* Try GDB's internal data cache. */
1542 if (!memory_xfer_check_region (readbuf
, writebuf
, memaddr
, len
, ®_len
,
1544 return TARGET_XFER_E_IO
;
1546 if (inferior_ptid
!= null_ptid
)
1547 inf
= current_inferior ();
1553 /* The dcache reads whole cache lines; that doesn't play well
1554 with reading from a trace buffer, because reading outside of
1555 the collected memory range fails. */
1556 && get_traceframe_number () == -1
1557 && (region
->attrib
.cache
1558 || (stack_cache_enabled_p () && object
== TARGET_OBJECT_STACK_MEMORY
)
1559 || (code_cache_enabled_p () && object
== TARGET_OBJECT_CODE_MEMORY
)))
1562 = target_dcache_get_or_init (current_program_space
->aspace
);
1564 return dcache_read_memory_partial (ops
, dcache
, memaddr
, readbuf
,
1565 reg_len
, xfered_len
);
1568 /* If none of those methods found the memory we wanted, fall back
1569 to a target partial transfer. Normally a single call to
1570 to_xfer_partial is enough; if it doesn't recognize an object
1571 it will call the to_xfer_partial of the next target down.
1572 But for memory this won't do. Memory is the only target
1573 object which can be read from more than one valid target.
1574 A core file, for instance, could have some of memory but
1575 delegate other bits to the target below it. So, we must
1576 manually try all targets. */
1578 res
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, memaddr
, reg_len
,
1581 /* If we still haven't got anything, return the last error. We
1586 /* Perform a partial memory transfer. For docs see target.h,
1589 static enum target_xfer_status
1590 memory_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1591 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1592 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1594 enum target_xfer_status res
;
1596 /* Zero length requests are ok and require no work. */
1598 return TARGET_XFER_EOF
;
1600 memaddr
= gdbarch_remove_non_address_bits (current_inferior ()->arch (),
1603 /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
1604 breakpoint insns, thus hiding out from higher layers whether
1605 there are software breakpoints inserted in the code stream. */
1606 if (readbuf
!= NULL
)
1608 res
= memory_xfer_partial_1 (ops
, object
, readbuf
, NULL
, memaddr
, len
,
1611 if (res
== TARGET_XFER_OK
&& !show_memory_breakpoints
)
1612 breakpoint_xfer_memory (readbuf
, NULL
, NULL
, memaddr
, *xfered_len
);
1616 /* A large write request is likely to be partially satisfied
1617 by memory_xfer_partial_1. We will continually malloc
1618 and free a copy of the entire write request for breakpoint
1619 shadow handling even though we only end up writing a small
1620 subset of it. Cap writes to a limit specified by the target
1621 to mitigate this. */
1622 len
= std::min (ops
->get_memory_xfer_limit (), len
);
1624 gdb::byte_vector
buf (writebuf
, writebuf
+ len
);
1625 breakpoint_xfer_memory (NULL
, buf
.data (), writebuf
, memaddr
, len
);
1626 res
= memory_xfer_partial_1 (ops
, object
, NULL
, buf
.data (), memaddr
, len
,
1633 scoped_restore_tmpl
<int>
1634 make_scoped_restore_show_memory_breakpoints (int show
)
1636 return make_scoped_restore (&show_memory_breakpoints
, show
);
1639 /* For docs see target.h, to_xfer_partial. */
1641 enum target_xfer_status
1642 target_xfer_partial (struct target_ops
*ops
,
1643 enum target_object object
, const char *annex
,
1644 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1645 ULONGEST offset
, ULONGEST len
,
1646 ULONGEST
*xfered_len
)
1648 enum target_xfer_status retval
;
1650 /* Transfer is done when LEN is zero. */
1652 return TARGET_XFER_EOF
;
1654 if (writebuf
&& !may_write_memory
)
1655 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1656 core_addr_to_string_nz (offset
), plongest (len
));
1660 /* If this is a memory transfer, let the memory-specific code
1661 have a look at it instead. Memory transfers are more
1663 if (object
== TARGET_OBJECT_MEMORY
|| object
== TARGET_OBJECT_STACK_MEMORY
1664 || object
== TARGET_OBJECT_CODE_MEMORY
)
1665 retval
= memory_xfer_partial (ops
, object
, readbuf
,
1666 writebuf
, offset
, len
, xfered_len
);
1667 else if (object
== TARGET_OBJECT_RAW_MEMORY
)
1669 /* Skip/avoid accessing the target if the memory region
1670 attributes block the access. Check this here instead of in
1671 raw_memory_xfer_partial as otherwise we'd end up checking
1672 this twice in the case of the memory_xfer_partial path is
1673 taken; once before checking the dcache, and another in the
1674 tail call to raw_memory_xfer_partial. */
1675 if (!memory_xfer_check_region (readbuf
, writebuf
, offset
, len
, &len
,
1677 return TARGET_XFER_E_IO
;
1679 /* Request the normal memory object from other layers. */
1680 retval
= raw_memory_xfer_partial (ops
, readbuf
, writebuf
, offset
, len
,
1684 retval
= ops
->xfer_partial (object
, annex
, readbuf
,
1685 writebuf
, offset
, len
, xfered_len
);
1689 const unsigned char *myaddr
= NULL
;
1691 gdb_printf (gdb_stdlog
,
1692 "%s:target_xfer_partial "
1693 "(%d, %s, %s, %s, %s, %s) = %d, %s",
1696 (annex
? annex
: "(null)"),
1697 host_address_to_string (readbuf
),
1698 host_address_to_string (writebuf
),
1699 core_addr_to_string_nz (offset
),
1700 pulongest (len
), retval
,
1701 pulongest (*xfered_len
));
1707 if (retval
== TARGET_XFER_OK
&& myaddr
!= NULL
)
1711 gdb_puts (", bytes =", gdb_stdlog
);
1712 for (i
= 0; i
< *xfered_len
; i
++)
1714 if ((((intptr_t) &(myaddr
[i
])) & 0xf) == 0)
1716 if (targetdebug
< 2 && i
> 0)
1718 gdb_printf (gdb_stdlog
, " ...");
1721 gdb_printf (gdb_stdlog
, "\n");
1724 gdb_printf (gdb_stdlog
, " %02x", myaddr
[i
] & 0xff);
1728 gdb_putc ('\n', gdb_stdlog
);
1731 /* Check implementations of to_xfer_partial update *XFERED_LEN
1732 properly. Do assertion after printing debug messages, so that we
1733 can find more clues on assertion failure from debugging messages. */
1734 if (retval
== TARGET_XFER_OK
|| retval
== TARGET_XFER_UNAVAILABLE
)
1735 gdb_assert (*xfered_len
> 0);
1740 /* Read LEN bytes of target memory at address MEMADDR, placing the
1741 results in GDB's memory at MYADDR. Returns either 0 for success or
1742 -1 if any error occurs.
1744 If an error occurs, no guarantee is made about the contents of the data at
1745 MYADDR. In particular, the caller should not depend upon partial reads
1746 filling the buffer with good data. There is no way for the caller to know
1747 how much good data might have been transfered anyway. Callers that can
1748 deal with partial reads should call target_read (which will retry until
1749 it makes no progress, and then return how much was transferred). */
1752 target_read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1754 if (target_read (current_inferior ()->top_target (),
1755 TARGET_OBJECT_MEMORY
, NULL
,
1756 myaddr
, memaddr
, len
) == len
)
1762 /* See target/target.h. */
1765 target_read_uint32 (CORE_ADDR memaddr
, uint32_t *result
)
1770 r
= target_read_memory (memaddr
, buf
, sizeof buf
);
1773 *result
= extract_unsigned_integer
1775 gdbarch_byte_order (current_inferior ()->arch ()));
1779 /* Like target_read_memory, but specify explicitly that this is a read
1780 from the target's raw memory. That is, this read bypasses the
1781 dcache, breakpoint shadowing, etc. */
1784 target_read_raw_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1786 if (target_read (current_inferior ()->top_target (),
1787 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1788 myaddr
, memaddr
, len
) == len
)
1794 /* Like target_read_memory, but specify explicitly that this is a read from
1795 the target's stack. This may trigger different cache behavior. */
1798 target_read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1800 if (target_read (current_inferior ()->top_target (),
1801 TARGET_OBJECT_STACK_MEMORY
, NULL
,
1802 myaddr
, memaddr
, len
) == len
)
1808 /* Like target_read_memory, but specify explicitly that this is a read from
1809 the target's code. This may trigger different cache behavior. */
1812 target_read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
1814 if (target_read (current_inferior ()->top_target (),
1815 TARGET_OBJECT_CODE_MEMORY
, NULL
,
1816 myaddr
, memaddr
, len
) == len
)
1822 /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1823 Returns either 0 for success or -1 if any error occurs. If an
1824 error occurs, no guarantee is made about how much data got written.
1825 Callers that can deal with partial writes should call
1829 target_write_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1831 if (target_write (current_inferior ()->top_target (),
1832 TARGET_OBJECT_MEMORY
, NULL
,
1833 myaddr
, memaddr
, len
) == len
)
1839 /* Write LEN bytes from MYADDR to target raw memory at address
1840 MEMADDR. Returns either 0 for success or -1 if any error occurs.
1841 If an error occurs, no guarantee is made about how much data got
1842 written. Callers that can deal with partial writes should call
1846 target_write_raw_memory (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ssize_t len
)
1848 if (target_write (current_inferior ()->top_target (),
1849 TARGET_OBJECT_RAW_MEMORY
, NULL
,
1850 myaddr
, memaddr
, len
) == len
)
1856 /* Fetch the target's memory map. */
1858 std::vector
<mem_region
>
1859 target_memory_map (void)
1861 target_ops
*target
= current_inferior ()->top_target ();
1862 std::vector
<mem_region
> result
= target
->memory_map ();
1863 if (result
.empty ())
1866 std::sort (result
.begin (), result
.end ());
1868 /* Check that regions do not overlap. Simultaneously assign
1869 a numbering for the "mem" commands to use to refer to
1871 mem_region
*last_one
= NULL
;
1872 for (size_t ix
= 0; ix
< result
.size (); ix
++)
1874 mem_region
*this_one
= &result
[ix
];
1875 this_one
->number
= ix
;
1877 if (last_one
!= NULL
&& last_one
->hi
> this_one
->lo
)
1879 warning (_("Overlapping regions in memory map: ignoring"));
1880 return std::vector
<mem_region
> ();
1883 last_one
= this_one
;
1890 target_flash_erase (ULONGEST address
, LONGEST length
)
1892 current_inferior ()->top_target ()->flash_erase (address
, length
);
1896 target_flash_done (void)
1898 current_inferior ()->top_target ()->flash_done ();
1902 show_trust_readonly (struct ui_file
*file
, int from_tty
,
1903 struct cmd_list_element
*c
, const char *value
)
1906 _("Mode for reading from readonly sections is %s.\n"),
1910 /* Target vector read/write partial wrapper functions. */
1912 static enum target_xfer_status
1913 target_read_partial (struct target_ops
*ops
,
1914 enum target_object object
,
1915 const char *annex
, gdb_byte
*buf
,
1916 ULONGEST offset
, ULONGEST len
,
1917 ULONGEST
*xfered_len
)
1919 return target_xfer_partial (ops
, object
, annex
, buf
, NULL
, offset
, len
,
1923 static enum target_xfer_status
1924 target_write_partial (struct target_ops
*ops
,
1925 enum target_object object
,
1926 const char *annex
, const gdb_byte
*buf
,
1927 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
)
1929 return target_xfer_partial (ops
, object
, annex
, NULL
, buf
, offset
, len
,
1933 /* Wrappers to perform the full transfer. */
1935 /* For docs on target_read see target.h. */
1938 target_read (struct target_ops
*ops
,
1939 enum target_object object
,
1940 const char *annex
, gdb_byte
*buf
,
1941 ULONGEST offset
, LONGEST len
)
1943 LONGEST xfered_total
= 0;
1946 /* If we are reading from a memory object, find the length of an addressable
1947 unit for that architecture. */
1948 if (object
== TARGET_OBJECT_MEMORY
1949 || object
== TARGET_OBJECT_STACK_MEMORY
1950 || object
== TARGET_OBJECT_CODE_MEMORY
1951 || object
== TARGET_OBJECT_RAW_MEMORY
)
1952 unit_size
= gdbarch_addressable_memory_unit_size
1953 (current_inferior ()->arch ());
1955 while (xfered_total
< len
)
1957 ULONGEST xfered_partial
;
1958 enum target_xfer_status status
;
1960 status
= target_read_partial (ops
, object
, annex
,
1961 buf
+ xfered_total
* unit_size
,
1962 offset
+ xfered_total
, len
- xfered_total
,
1965 /* Call an observer, notifying them of the xfer progress? */
1966 if (status
== TARGET_XFER_EOF
)
1967 return xfered_total
;
1968 else if (status
== TARGET_XFER_OK
)
1970 xfered_total
+= xfered_partial
;
1974 return TARGET_XFER_E_IO
;
1980 /* Assuming that the entire [begin, end) range of memory cannot be
1981 read, try to read whatever subrange is possible to read.
1983 The function returns, in RESULT, either zero or one memory block.
1984 If there's a readable subrange at the beginning, it is completely
1985 read and returned. Any further readable subrange will not be read.
1986 Otherwise, if there's a readable subrange at the end, it will be
1987 completely read and returned. Any readable subranges before it
1988 (obviously, not starting at the beginning), will be ignored. In
1989 other cases -- either no readable subrange, or readable subrange(s)
1990 that is neither at the beginning, or end, nothing is returned.
1992 The purpose of this function is to handle a read across a boundary
1993 of accessible memory in a case when memory map is not available.
1994 The above restrictions are fine for this case, but will give
1995 incorrect results if the memory is 'patchy'. However, supporting
1996 'patchy' memory would require trying to read every single byte,
1997 and it seems unacceptable solution. Explicit memory map is
1998 recommended for this case -- and target_read_memory_robust will
1999 take care of reading multiple ranges then. */
2002 read_whatever_is_readable (struct target_ops
*ops
,
2003 const ULONGEST begin
, const ULONGEST end
,
2005 std::vector
<memory_read_result
> *result
)
2007 ULONGEST current_begin
= begin
;
2008 ULONGEST current_end
= end
;
2010 ULONGEST xfered_len
;
2012 /* If we previously failed to read 1 byte, nothing can be done here. */
2013 if (end
- begin
<= 1)
2016 gdb::unique_xmalloc_ptr
<gdb_byte
> buf ((gdb_byte
*) xmalloc (end
- begin
));
2018 /* Check that either first or the last byte is readable, and give up
2019 if not. This heuristic is meant to permit reading accessible memory
2020 at the boundary of accessible region. */
2021 if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2022 buf
.get (), begin
, 1, &xfered_len
) == TARGET_XFER_OK
)
2027 else if (target_read_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2028 buf
.get () + (end
- begin
) - 1, end
- 1, 1,
2029 &xfered_len
) == TARGET_XFER_OK
)
2037 /* Loop invariant is that the [current_begin, current_end) was previously
2038 found to be not readable as a whole.
2040 Note loop condition -- if the range has 1 byte, we can't divide the range
2041 so there's no point trying further. */
2042 while (current_end
- current_begin
> 1)
2044 ULONGEST first_half_begin
, first_half_end
;
2045 ULONGEST second_half_begin
, second_half_end
;
2047 ULONGEST middle
= current_begin
+ (current_end
- current_begin
) / 2;
2051 first_half_begin
= current_begin
;
2052 first_half_end
= middle
;
2053 second_half_begin
= middle
;
2054 second_half_end
= current_end
;
2058 first_half_begin
= middle
;
2059 first_half_end
= current_end
;
2060 second_half_begin
= current_begin
;
2061 second_half_end
= middle
;
2064 xfer
= target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
,
2065 buf
.get () + (first_half_begin
- begin
) * unit_size
,
2067 first_half_end
- first_half_begin
);
2069 if (xfer
== first_half_end
- first_half_begin
)
2071 /* This half reads up fine. So, the error must be in the
2073 current_begin
= second_half_begin
;
2074 current_end
= second_half_end
;
2078 /* This half is not readable. Because we've tried one byte, we
2079 know some part of this half if actually readable. Go to the next
2080 iteration to divide again and try to read.
2082 We don't handle the other half, because this function only tries
2083 to read a single readable subrange. */
2084 current_begin
= first_half_begin
;
2085 current_end
= first_half_end
;
2091 /* The [begin, current_begin) range has been read. */
2092 result
->emplace_back (begin
, current_end
, std::move (buf
));
2096 /* The [current_end, end) range has been read. */
2097 LONGEST region_len
= end
- current_end
;
2099 gdb::unique_xmalloc_ptr
<gdb_byte
> data
2100 ((gdb_byte
*) xmalloc (region_len
* unit_size
));
2101 memcpy (data
.get (), buf
.get () + (current_end
- begin
) * unit_size
,
2102 region_len
* unit_size
);
2103 result
->emplace_back (current_end
, end
, std::move (data
));
2107 std::vector
<memory_read_result
>
2108 read_memory_robust (struct target_ops
*ops
,
2109 const ULONGEST offset
, const LONGEST len
)
2111 std::vector
<memory_read_result
> result
;
2113 = gdbarch_addressable_memory_unit_size (current_inferior ()->arch ());
2115 LONGEST xfered_total
= 0;
2116 while (xfered_total
< len
)
2118 struct mem_region
*region
= lookup_mem_region (offset
+ xfered_total
);
2121 /* If there is no explicit region, a fake one should be created. */
2122 gdb_assert (region
);
2124 if (region
->hi
== 0)
2125 region_len
= len
- xfered_total
;
2127 region_len
= region
->hi
- offset
;
2129 if (region
->attrib
.mode
== MEM_NONE
|| region
->attrib
.mode
== MEM_WO
)
2131 /* Cannot read this region. Note that we can end up here only
2132 if the region is explicitly marked inaccessible, or
2133 'inaccessible-by-default' is in effect. */
2134 xfered_total
+= region_len
;
2138 LONGEST to_read
= std::min (len
- xfered_total
, region_len
);
2139 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
2140 ((gdb_byte
*) xmalloc (to_read
* unit_size
));
2142 LONGEST xfered_partial
=
2143 target_read (ops
, TARGET_OBJECT_MEMORY
, NULL
, buffer
.get (),
2144 offset
+ xfered_total
, to_read
);
2145 /* Call an observer, notifying them of the xfer progress? */
2146 if (xfered_partial
<= 0)
2148 /* Got an error reading full chunk. See if maybe we can read
2150 read_whatever_is_readable (ops
, offset
+ xfered_total
,
2151 offset
+ xfered_total
+ to_read
,
2152 unit_size
, &result
);
2153 xfered_total
+= to_read
;
2157 result
.emplace_back (offset
+ xfered_total
,
2158 offset
+ xfered_total
+ xfered_partial
,
2159 std::move (buffer
));
2160 xfered_total
+= xfered_partial
;
2170 /* An alternative to target_write with progress callbacks. */
2173 target_write_with_progress (struct target_ops
*ops
,
2174 enum target_object object
,
2175 const char *annex
, const gdb_byte
*buf
,
2176 ULONGEST offset
, LONGEST len
,
2177 void (*progress
) (ULONGEST
, void *), void *baton
)
2179 LONGEST xfered_total
= 0;
2182 /* If we are writing to a memory object, find the length of an addressable
2183 unit for that architecture. */
2184 if (object
== TARGET_OBJECT_MEMORY
2185 || object
== TARGET_OBJECT_STACK_MEMORY
2186 || object
== TARGET_OBJECT_CODE_MEMORY
2187 || object
== TARGET_OBJECT_RAW_MEMORY
)
2188 unit_size
= gdbarch_addressable_memory_unit_size
2189 (current_inferior ()->arch ());
2191 /* Give the progress callback a chance to set up. */
2193 (*progress
) (0, baton
);
2195 while (xfered_total
< len
)
2197 ULONGEST xfered_partial
;
2198 enum target_xfer_status status
;
2200 status
= target_write_partial (ops
, object
, annex
,
2201 buf
+ xfered_total
* unit_size
,
2202 offset
+ xfered_total
, len
- xfered_total
,
2205 if (status
!= TARGET_XFER_OK
)
2206 return status
== TARGET_XFER_EOF
? xfered_total
: TARGET_XFER_E_IO
;
2209 (*progress
) (xfered_partial
, baton
);
2211 xfered_total
+= xfered_partial
;
2217 /* For docs on target_write see target.h. */
2220 target_write (struct target_ops
*ops
,
2221 enum target_object object
,
2222 const char *annex
, const gdb_byte
*buf
,
2223 ULONGEST offset
, LONGEST len
)
2225 return target_write_with_progress (ops
, object
, annex
, buf
, offset
, len
,
2229 /* Help for target_read_alloc and target_read_stralloc. See their comments
2232 template <typename T
>
2233 std::optional
<gdb::def_vector
<T
>>
2234 target_read_alloc_1 (struct target_ops
*ops
, enum target_object object
,
2237 gdb::def_vector
<T
> buf
;
2239 const int chunk
= 4096;
2241 /* This function does not have a length parameter; it reads the
2242 entire OBJECT). Also, it doesn't support objects fetched partly
2243 from one target and partly from another (in a different stratum,
2244 e.g. a core file and an executable). Both reasons make it
2245 unsuitable for reading memory. */
2246 gdb_assert (object
!= TARGET_OBJECT_MEMORY
);
2248 /* Start by reading up to 4K at a time. The target will throttle
2249 this number down if necessary. */
2252 ULONGEST xfered_len
;
2253 enum target_xfer_status status
;
2255 buf
.resize (buf_pos
+ chunk
);
2257 status
= target_read_partial (ops
, object
, annex
,
2258 (gdb_byte
*) &buf
[buf_pos
],
2262 if (status
== TARGET_XFER_EOF
)
2264 /* Read all there was. */
2265 buf
.resize (buf_pos
);
2268 else if (status
!= TARGET_XFER_OK
)
2270 /* An error occurred. */
2274 buf_pos
+= xfered_len
;
2282 std::optional
<gdb::byte_vector
>
2283 target_read_alloc (struct target_ops
*ops
, enum target_object object
,
2286 return target_read_alloc_1
<gdb_byte
> (ops
, object
, annex
);
2291 std::optional
<gdb::char_vector
>
2292 target_read_stralloc (struct target_ops
*ops
, enum target_object object
,
2295 std::optional
<gdb::char_vector
> buf
2296 = target_read_alloc_1
<char> (ops
, object
, annex
);
2301 if (buf
->empty () || buf
->back () != '\0')
2302 buf
->push_back ('\0');
2304 /* Check for embedded NUL bytes; but allow trailing NULs. */
2305 for (auto it
= std::find (buf
->begin (), buf
->end (), '\0');
2306 it
!= buf
->end (); it
++)
2309 warning (_("target object %d, annex %s, "
2310 "contained unexpected null characters"),
2311 (int) object
, annex
? annex
: "(none)");
2318 /* Memory transfer methods. */
2321 get_target_memory (struct target_ops
*ops
, CORE_ADDR addr
, gdb_byte
*buf
,
2324 /* This method is used to read from an alternate, non-current
2325 target. This read must bypass the overlay support (as symbols
2326 don't match this target), and GDB's internal cache (wrong cache
2327 for this target). */
2328 if (target_read (ops
, TARGET_OBJECT_RAW_MEMORY
, NULL
, buf
, addr
, len
)
2330 memory_error (TARGET_XFER_E_IO
, addr
);
2334 get_target_memory_unsigned (struct target_ops
*ops
, CORE_ADDR addr
,
2335 int len
, enum bfd_endian byte_order
)
2337 gdb_byte buf
[sizeof (ULONGEST
)];
2339 gdb_assert (len
<= sizeof (buf
));
2340 get_target_memory (ops
, addr
, buf
, len
);
2341 return extract_unsigned_integer (buf
, len
, byte_order
);
2347 target_insert_breakpoint (struct gdbarch
*gdbarch
,
2348 struct bp_target_info
*bp_tgt
)
2350 if (!may_insert_breakpoints
)
2352 warning (_("May not insert breakpoints"));
2356 target_ops
*target
= current_inferior ()->top_target ();
2358 return target
->insert_breakpoint (gdbarch
, bp_tgt
);
2364 target_remove_breakpoint (struct gdbarch
*gdbarch
,
2365 struct bp_target_info
*bp_tgt
,
2366 enum remove_bp_reason reason
)
2368 /* This is kind of a weird case to handle, but the permission might
2369 have been changed after breakpoints were inserted - in which case
2370 we should just take the user literally and assume that any
2371 breakpoints should be left in place. */
2372 if (!may_insert_breakpoints
)
2374 warning (_("May not remove breakpoints"));
2378 target_ops
*target
= current_inferior ()->top_target ();
2380 return target
->remove_breakpoint (gdbarch
, bp_tgt
, reason
);
2384 info_target_command (const char *args
, int from_tty
)
2386 int has_all_mem
= 0;
2388 if (current_program_space
->symfile_object_file
!= NULL
)
2390 objfile
*objf
= current_program_space
->symfile_object_file
;
2391 gdb_printf (_("Symbols from \"%s\".\n"),
2392 objfile_name (objf
));
2395 for (target_ops
*t
= current_inferior ()->top_target ();
2399 if (!t
->has_memory ())
2402 if ((int) (t
->stratum ()) <= (int) dummy_stratum
)
2405 gdb_printf (_("\tWhile running this, "
2406 "GDB does not access memory from...\n"));
2407 gdb_printf ("%s:\n", t
->longname ());
2409 has_all_mem
= t
->has_all_memory ();
2413 /* This function is called before any new inferior is created, e.g.
2414 by running a program, attaching, or connecting to a target.
2415 It cleans up any state from previous invocations which might
2416 change between runs. This is a subset of what target_preopen
2417 resets (things which might change between targets). */
2420 target_pre_inferior (int from_tty
)
2422 /* Clear out solib state. Otherwise the solib state of the previous
2423 inferior might have survived and is entirely wrong for the new
2424 target. This has been observed on GNU/Linux using glibc 2.3. How
2436 Cannot access memory at address 0xdeadbeef
2439 /* In some OSs, the shared library list is the same/global/shared
2440 across inferiors. If code is shared between processes, so are
2441 memory regions and features. */
2442 if (!gdbarch_has_global_solist (current_inferior ()->arch ()))
2444 no_shared_libraries (NULL
, from_tty
);
2446 invalidate_target_mem_regions ();
2448 target_clear_description ();
2451 /* attach_flag may be set if the previous process associated with
2452 the inferior was attached to. */
2453 current_inferior ()->attach_flag
= false;
2455 current_inferior ()->highest_thread_num
= 0;
2457 update_previous_thread ();
2459 agent_capability_invalidate ();
2462 /* This is to be called by the open routine before it does
2466 target_preopen (int from_tty
)
2470 if (current_inferior ()->pid
!= 0)
2473 || !target_has_execution ()
2474 || query (_("A program is being debugged already. Kill it? ")))
2476 /* Core inferiors actually should be detached, not
2478 if (target_has_execution ())
2481 target_detach (current_inferior (), 0);
2484 error (_("Program not killed."));
2487 /* Release reference to old previous thread. */
2488 update_previous_thread ();
2490 /* Calling target_kill may remove the target from the stack. But if
2491 it doesn't (which seems like a win for UDI), remove it now. */
2492 /* Leave the exec target, though. The user may be switching from a
2493 live process to a core of the same program. */
2494 current_inferior ()->pop_all_targets_above (file_stratum
);
2496 target_pre_inferior (from_tty
);
2502 target_detach (inferior
*inf
, int from_tty
)
2504 /* Thread's don't need to be resumed until the end of this function. */
2505 scoped_disable_commit_resumed
disable_commit_resumed ("detaching");
2507 /* After we have detached, we will clear the register cache for this inferior
2508 by calling registers_changed_ptid. We must save the pid_ptid before
2509 detaching, as the target detach method will clear inf->pid. */
2510 ptid_t save_pid_ptid
= ptid_t (inf
->pid
);
2512 /* As long as some to_detach implementations rely on the current_inferior
2513 (either directly, or indirectly, like through reading memory), INF needs
2514 to be the current inferior. When that requirement will become no longer
2515 true, then we can remove this assertion. */
2516 gdb_assert (inf
== current_inferior ());
2518 prepare_for_detach ();
2520 gdb::observers::inferior_pre_detach
.notify (inf
);
2522 /* Hold a strong reference because detaching may unpush the
2524 auto proc_target_ref
= target_ops_ref::new_reference (inf
->process_target ());
2526 current_inferior ()->top_target ()->detach (inf
, from_tty
);
2528 process_stratum_target
*proc_target
2529 = as_process_stratum_target (proc_target_ref
.get ());
2531 registers_changed_ptid (proc_target
, save_pid_ptid
);
2533 /* We have to ensure we have no frame cache left. Normally,
2534 registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
2535 inferior_ptid matches save_pid_ptid, but in our case, it does not
2536 call it, as inferior_ptid has been reset. */
2537 reinit_frame_cache ();
2539 disable_commit_resumed
.reset_and_commit ();
2543 target_disconnect (const char *args
, int from_tty
)
2545 /* If we're in breakpoints-always-inserted mode or if breakpoints
2546 are global across processes, we have to remove them before
2548 remove_breakpoints ();
2550 current_inferior ()->top_target ()->disconnect (args
, from_tty
);
2553 /* See target/target.h. */
2556 target_wait (ptid_t ptid
, struct target_waitstatus
*status
,
2557 target_wait_flags options
)
2559 target_ops
*target
= current_inferior ()->top_target ();
2560 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
2562 gdb_assert (!proc_target
->commit_resumed_state
);
2564 if (!target_can_async_p (target
))
2565 gdb_assert ((options
& TARGET_WNOHANG
) == 0);
2569 gdb::observers::target_pre_wait
.notify (ptid
);
2570 ptid_t event_ptid
= target
->wait (ptid
, status
, options
);
2571 gdb::observers::target_post_wait
.notify (event_ptid
);
2576 gdb::observers::target_post_wait
.notify (null_ptid
);
2584 default_target_wait (struct target_ops
*ops
,
2585 ptid_t ptid
, struct target_waitstatus
*status
,
2586 target_wait_flags options
)
2588 status
->set_ignore ();
2589 return minus_one_ptid
;
2593 target_pid_to_str (ptid_t ptid
)
2595 return current_inferior ()->top_target ()->pid_to_str (ptid
);
2599 target_thread_name (struct thread_info
*info
)
2601 gdb_assert (info
->inf
== current_inferior ());
2603 return current_inferior ()->top_target ()->thread_name (info
);
2606 struct thread_info
*
2607 target_thread_handle_to_thread_info (const gdb_byte
*thread_handle
,
2609 struct inferior
*inf
)
2611 target_ops
*target
= current_inferior ()->top_target ();
2613 return target
->thread_handle_to_thread_info (thread_handle
, handle_len
, inf
);
2618 gdb::array_view
<const gdb_byte
>
2619 target_thread_info_to_thread_handle (struct thread_info
*tip
)
2621 target_ops
*target
= current_inferior ()->top_target ();
2623 return target
->thread_info_to_thread_handle (tip
);
2627 target_resume (ptid_t scope_ptid
, int step
, enum gdb_signal signal
)
2629 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
2630 gdb_assert (!curr_target
->commit_resumed_state
);
2632 gdb_assert (inferior_ptid
!= null_ptid
);
2633 gdb_assert (inferior_ptid
.matches (scope_ptid
));
2635 target_dcache_invalidate (current_program_space
->aspace
);
2637 current_inferior ()->top_target ()->resume (scope_ptid
, step
, signal
);
2639 registers_changed_ptid (curr_target
, scope_ptid
);
2640 /* We only set the internal executing state here. The user/frontend
2641 running state is set at a higher level. This also clears the
2642 thread's stop_pc as side effect. */
2643 set_executing (curr_target
, scope_ptid
, true);
2644 clear_inline_frame_state (curr_target
, scope_ptid
);
2646 if (target_can_async_p ())
2647 target_async (true);
2653 target_commit_resumed ()
2655 gdb_assert (current_inferior ()->process_target ()->commit_resumed_state
);
2656 current_inferior ()->top_target ()->commit_resumed ();
2662 target_has_pending_events ()
2664 return current_inferior ()->top_target ()->has_pending_events ();
2668 target_pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2670 current_inferior ()->top_target ()->pass_signals (pass_signals
);
2674 target_program_signals (gdb::array_view
<const unsigned char> program_signals
)
2676 current_inferior ()->top_target ()->program_signals (program_signals
);
2680 default_follow_fork (struct target_ops
*self
, inferior
*child_inf
,
2681 ptid_t child_ptid
, target_waitkind fork_kind
,
2682 bool follow_child
, bool detach_fork
)
2684 /* Some target returned a fork event, but did not know how to follow it. */
2685 internal_error (_("could not find a target to follow fork"));
2689 default_follow_clone (struct target_ops
*self
, ptid_t child_ptid
)
2691 /* Some target returned a clone event, but did not know how to follow it. */
2692 internal_error (_("could not find a target to follow clone"));
2698 target_follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
2699 target_waitkind fork_kind
, bool follow_child
,
2702 target_ops
*target
= current_inferior ()->top_target ();
2704 /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
2706 if (child_inf
!= nullptr)
2708 gdb_assert (follow_child
|| !detach_fork
);
2709 gdb_assert (child_inf
->pid
== child_ptid
.pid ());
2712 gdb_assert (!follow_child
&& detach_fork
);
2714 return target
->follow_fork (child_inf
, child_ptid
, fork_kind
, follow_child
,
2721 target_follow_exec (inferior
*follow_inf
, ptid_t ptid
,
2722 const char *execd_pathname
)
2724 current_inferior ()->top_target ()->follow_exec (follow_inf
, ptid
,
2729 default_mourn_inferior (struct target_ops
*self
)
2731 internal_error (_("could not find a target to follow mourn inferior"));
2735 target_mourn_inferior (ptid_t ptid
)
2737 gdb_assert (ptid
.pid () == inferior_ptid
.pid ());
2738 current_inferior ()->top_target ()->mourn_inferior ();
2741 /* Look for a target which can describe architectural features, starting
2742 from TARGET. If we find one, return its description. */
2744 const struct target_desc
*
2745 target_read_description (struct target_ops
*target
)
2747 return target
->read_description ();
2751 /* Default implementation of memory-searching. */
2754 default_search_memory (struct target_ops
*self
,
2755 CORE_ADDR start_addr
, ULONGEST search_space_len
,
2756 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2757 CORE_ADDR
*found_addrp
)
2759 auto read_memory
= [=] (CORE_ADDR addr
, gdb_byte
*result
, size_t len
)
2761 return target_read (current_inferior ()->top_target (),
2762 TARGET_OBJECT_MEMORY
, NULL
,
2763 result
, addr
, len
) == len
;
2766 /* Start over from the top of the target stack. */
2767 return simple_search_memory (read_memory
, start_addr
, search_space_len
,
2768 pattern
, pattern_len
, found_addrp
);
2771 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2772 sequence of bytes in PATTERN with length PATTERN_LEN.
2774 The result is 1 if found, 0 if not found, and -1 if there was an error
2775 requiring halting of the search (e.g. memory read error).
2776 If the pattern is found the address is recorded in FOUND_ADDRP. */
2779 target_search_memory (CORE_ADDR start_addr
, ULONGEST search_space_len
,
2780 const gdb_byte
*pattern
, ULONGEST pattern_len
,
2781 CORE_ADDR
*found_addrp
)
2783 target_ops
*target
= current_inferior ()->top_target ();
2785 return target
->search_memory (start_addr
, search_space_len
, pattern
,
2786 pattern_len
, found_addrp
);
2789 /* Look through the currently pushed targets. If none of them will
2790 be able to restart the currently running process, issue an error
2794 target_require_runnable (void)
2796 for (target_ops
*t
= current_inferior ()->top_target ();
2800 /* If this target knows how to create a new program, then
2801 assume we will still be able to after killing the current
2802 one. Either killing and mourning will not pop T, or else
2803 find_default_run_target will find it again. */
2804 if (t
->can_create_inferior ())
2807 /* Do not worry about targets at certain strata that can not
2808 create inferiors. Assume they will be pushed again if
2809 necessary, and continue to the process_stratum. */
2810 if (t
->stratum () > process_stratum
)
2813 error (_("The \"%s\" target does not support \"run\". "
2814 "Try \"help target\" or \"continue\"."),
2818 /* This function is only called if the target is running. In that
2819 case there should have been a process_stratum target and it
2820 should either know how to create inferiors, or not... */
2821 internal_error (_("No targets found"));
2824 /* Whether GDB is allowed to fall back to the default run target for
2825 "run", "attach", etc. when no target is connected yet. */
2826 static bool auto_connect_native_target
= true;
2829 show_auto_connect_native_target (struct ui_file
*file
, int from_tty
,
2830 struct cmd_list_element
*c
, const char *value
)
2833 _("Whether GDB may automatically connect to the "
2834 "native target is %s.\n"),
2838 /* A pointer to the target that can respond to "run" or "attach".
2839 Native targets are always singletons and instantiated early at GDB
2841 static target_ops
*the_native_target
;
2846 set_native_target (target_ops
*target
)
2848 if (the_native_target
!= NULL
)
2849 internal_error (_("native target already set (\"%s\")."),
2850 the_native_target
->longname ());
2852 the_native_target
= target
;
2858 get_native_target ()
2860 return the_native_target
;
2863 /* Look through the list of possible targets for a target that can
2864 execute a run or attach command without any other data. This is
2865 used to locate the default process stratum.
2867 If DO_MESG is not NULL, the result is always valid (error() is
2868 called for errors); else, return NULL on error. */
2870 static struct target_ops
*
2871 find_default_run_target (const char *do_mesg
)
2873 if (auto_connect_native_target
&& the_native_target
!= NULL
)
2874 return the_native_target
;
2876 if (do_mesg
!= NULL
)
2877 error (_("Don't know how to %s. Try \"help target\"."), do_mesg
);
2884 find_attach_target (void)
2886 /* If a target on the current stack can attach, use it. */
2887 for (target_ops
*t
= current_inferior ()->top_target ();
2891 if (t
->can_attach ())
2895 /* Otherwise, use the default run target for attaching. */
2896 return find_default_run_target ("attach");
2902 find_run_target (void)
2904 /* If a target on the current stack can run, use it. */
2905 for (target_ops
*t
= current_inferior ()->top_target ();
2909 if (t
->can_create_inferior ())
2913 /* Otherwise, use the default run target. */
2914 return find_default_run_target ("run");
2918 target_ops::info_proc (const char *args
, enum info_proc_what what
)
2923 /* Implement the "info proc" command. */
2926 target_info_proc (const char *args
, enum info_proc_what what
)
2928 struct target_ops
*t
;
2930 /* If we're already connected to something that can get us OS
2931 related data, use it. Otherwise, try using the native
2933 t
= find_target_at (process_stratum
);
2935 t
= find_default_run_target (NULL
);
2937 for (; t
!= NULL
; t
= t
->beneath ())
2939 if (t
->info_proc (args
, what
))
2942 gdb_printf (gdb_stdlog
,
2943 "target_info_proc (\"%s\", %d)\n", args
, what
);
2953 find_default_supports_disable_randomization (struct target_ops
*self
)
2955 struct target_ops
*t
;
2957 t
= find_default_run_target (NULL
);
2959 return t
->supports_disable_randomization ();
2964 target_supports_disable_randomization (void)
2966 return current_inferior ()->top_target ()->supports_disable_randomization ();
2969 /* See target/target.h. */
2972 target_supports_multi_process (void)
2974 return current_inferior ()->top_target ()->supports_multi_process ();
2979 std::optional
<gdb::char_vector
>
2980 target_get_osdata (const char *type
)
2982 struct target_ops
*t
;
2984 /* If we're already connected to something that can get us OS
2985 related data, use it. Otherwise, try using the native
2987 t
= find_target_at (process_stratum
);
2989 t
= find_default_run_target ("get OS data");
2994 return target_read_stralloc (t
, TARGET_OBJECT_OSDATA
, type
);
3000 target_ops::beneath () const
3002 return current_inferior ()->find_target_beneath (this);
3006 target_ops::close ()
3011 target_ops::can_attach ()
3017 target_ops::attach (const char *, int)
3019 gdb_assert_not_reached ("target_ops::attach called");
3023 target_ops::can_create_inferior ()
3029 target_ops::create_inferior (const char *, const std::string
&,
3032 gdb_assert_not_reached ("target_ops::create_inferior called");
3036 target_ops::can_run ()
3044 for (target_ops
*t
= current_inferior ()->top_target ();
3055 /* Target file operations. */
3057 static struct target_ops
*
3058 default_fileio_target (void)
3060 struct target_ops
*t
;
3062 /* If we're already connected to something that can perform
3063 file I/O, use it. Otherwise, try using the native target. */
3064 t
= find_target_at (process_stratum
);
3067 return find_default_run_target ("file I/O");
3070 /* File handle for target file operations. */
3074 /* The target on which this file is open. NULL if the target is
3075 meanwhile closed while the handle is open. */
3078 /* The file descriptor on the target. */
3081 /* Check whether this fileio_fh_t represents a closed file. */
3084 return target_fd
< 0;
3088 /* Vector of currently open file handles. The value returned by
3089 target_fileio_open and passed as the FD argument to other
3090 target_fileio_* functions is an index into this vector. This
3091 vector's entries are never freed; instead, files are marked as
3092 closed, and the handle becomes available for reuse. */
3093 static std::vector
<fileio_fh_t
> fileio_fhandles
;
3095 /* Index into fileio_fhandles of the lowest handle that might be
3096 closed. This permits handle reuse without searching the whole
3097 list each time a new file is opened. */
3098 static int lowest_closed_fd
;
3103 fileio_handles_invalidate_target (target_ops
*targ
)
3105 for (fileio_fh_t
&fh
: fileio_fhandles
)
3106 if (fh
.target
== targ
)
3110 /* Acquire a target fileio file descriptor. */
3113 acquire_fileio_fd (target_ops
*target
, int target_fd
)
3115 /* Search for closed handles to reuse. */
3116 for (; lowest_closed_fd
< fileio_fhandles
.size (); lowest_closed_fd
++)
3118 fileio_fh_t
&fh
= fileio_fhandles
[lowest_closed_fd
];
3120 if (fh
.is_closed ())
3124 /* Push a new handle if no closed handles were found. */
3125 if (lowest_closed_fd
== fileio_fhandles
.size ())
3126 fileio_fhandles
.push_back (fileio_fh_t
{target
, target_fd
});
3128 fileio_fhandles
[lowest_closed_fd
] = {target
, target_fd
};
3130 /* Should no longer be marked closed. */
3131 gdb_assert (!fileio_fhandles
[lowest_closed_fd
].is_closed ());
3133 /* Return its index, and start the next lookup at
3135 return lowest_closed_fd
++;
3138 /* Release a target fileio file descriptor. */
3141 release_fileio_fd (int fd
, fileio_fh_t
*fh
)
3144 lowest_closed_fd
= std::min (lowest_closed_fd
, fd
);
3147 /* Return a pointer to the fileio_fhandle_t corresponding to FD. */
3149 static fileio_fh_t
*
3150 fileio_fd_to_fh (int fd
)
3152 return &fileio_fhandles
[fd
];
3156 /* Default implementations of file i/o methods. We don't want these
3157 to delegate automatically, because we need to know which target
3158 supported the method, in order to call it directly from within
3159 pread/pwrite, etc. */
3162 target_ops::fileio_open (struct inferior
*inf
, const char *filename
,
3163 int flags
, int mode
, int warn_if_slow
,
3164 fileio_error
*target_errno
)
3166 *target_errno
= FILEIO_ENOSYS
;
3171 target_ops::fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3172 ULONGEST offset
, fileio_error
*target_errno
)
3174 *target_errno
= FILEIO_ENOSYS
;
3179 target_ops::fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3180 ULONGEST offset
, fileio_error
*target_errno
)
3182 *target_errno
= FILEIO_ENOSYS
;
3187 target_ops::fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
)
3189 *target_errno
= FILEIO_ENOSYS
;
3194 target_ops::fileio_close (int fd
, fileio_error
*target_errno
)
3196 *target_errno
= FILEIO_ENOSYS
;
3201 target_ops::fileio_unlink (struct inferior
*inf
, const char *filename
,
3202 fileio_error
*target_errno
)
3204 *target_errno
= FILEIO_ENOSYS
;
3208 std::optional
<std::string
>
3209 target_ops::fileio_readlink (struct inferior
*inf
, const char *filename
,
3210 fileio_error
*target_errno
)
3212 *target_errno
= FILEIO_ENOSYS
;
3219 target_fileio_open (struct inferior
*inf
, const char *filename
,
3220 int flags
, int mode
, bool warn_if_slow
, fileio_error
*target_errno
)
3222 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3224 int fd
= t
->fileio_open (inf
, filename
, flags
, mode
,
3225 warn_if_slow
, target_errno
);
3227 if (fd
== -1 && *target_errno
== FILEIO_ENOSYS
)
3233 fd
= acquire_fileio_fd (t
, fd
);
3236 gdb_printf (gdb_stdlog
,
3237 "target_fileio_open (%d,%s,0x%x,0%o,%d)"
3239 inf
== NULL
? 0 : inf
->num
,
3240 filename
, flags
, mode
,
3242 fd
!= -1 ? 0 : *target_errno
);
3246 *target_errno
= FILEIO_ENOSYS
;
3253 target_fileio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
3254 ULONGEST offset
, fileio_error
*target_errno
)
3256 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3259 if (fh
->is_closed ())
3260 *target_errno
= FILEIO_EBADF
;
3261 else if (fh
->target
== NULL
)
3262 *target_errno
= FILEIO_EIO
;
3264 ret
= fh
->target
->fileio_pwrite (fh
->target_fd
, write_buf
,
3265 len
, offset
, target_errno
);
3268 gdb_printf (gdb_stdlog
,
3269 "target_fileio_pwrite (%d,...,%d,%s) "
3271 fd
, len
, pulongest (offset
),
3272 ret
, ret
!= -1 ? 0 : *target_errno
);
3279 target_fileio_pread (int fd
, gdb_byte
*read_buf
, int len
,
3280 ULONGEST offset
, fileio_error
*target_errno
)
3282 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3285 if (fh
->is_closed ())
3286 *target_errno
= FILEIO_EBADF
;
3287 else if (fh
->target
== NULL
)
3288 *target_errno
= FILEIO_EIO
;
3290 ret
= fh
->target
->fileio_pread (fh
->target_fd
, read_buf
,
3291 len
, offset
, target_errno
);
3294 gdb_printf (gdb_stdlog
,
3295 "target_fileio_pread (%d,...,%d,%s) "
3297 fd
, len
, pulongest (offset
),
3298 ret
, ret
!= -1 ? 0 : *target_errno
);
3305 target_fileio_fstat (int fd
, struct stat
*sb
, fileio_error
*target_errno
)
3307 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3310 if (fh
->is_closed ())
3311 *target_errno
= FILEIO_EBADF
;
3312 else if (fh
->target
== NULL
)
3313 *target_errno
= FILEIO_EIO
;
3315 ret
= fh
->target
->fileio_fstat (fh
->target_fd
, sb
, target_errno
);
3318 gdb_printf (gdb_stdlog
,
3319 "target_fileio_fstat (%d) = %d (%d)\n",
3320 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3327 target_fileio_close (int fd
, fileio_error
*target_errno
)
3329 fileio_fh_t
*fh
= fileio_fd_to_fh (fd
);
3332 if (fh
->is_closed ())
3333 *target_errno
= FILEIO_EBADF
;
3336 if (fh
->target
!= NULL
)
3337 ret
= fh
->target
->fileio_close (fh
->target_fd
,
3341 release_fileio_fd (fd
, fh
);
3345 gdb_printf (gdb_stdlog
,
3346 "target_fileio_close (%d) = %d (%d)\n",
3347 fd
, ret
, ret
!= -1 ? 0 : *target_errno
);
3354 target_fileio_unlink (struct inferior
*inf
, const char *filename
,
3355 fileio_error
*target_errno
)
3357 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3359 int ret
= t
->fileio_unlink (inf
, filename
, target_errno
);
3361 if (ret
== -1 && *target_errno
== FILEIO_ENOSYS
)
3365 gdb_printf (gdb_stdlog
,
3366 "target_fileio_unlink (%d,%s)"
3368 inf
== NULL
? 0 : inf
->num
, filename
,
3369 ret
, ret
!= -1 ? 0 : *target_errno
);
3373 *target_errno
= FILEIO_ENOSYS
;
3379 std::optional
<std::string
>
3380 target_fileio_readlink (struct inferior
*inf
, const char *filename
,
3381 fileio_error
*target_errno
)
3383 for (target_ops
*t
= default_fileio_target (); t
!= NULL
; t
= t
->beneath ())
3385 std::optional
<std::string
> ret
3386 = t
->fileio_readlink (inf
, filename
, target_errno
);
3388 if (!ret
.has_value () && *target_errno
== FILEIO_ENOSYS
)
3392 gdb_printf (gdb_stdlog
,
3393 "target_fileio_readlink (%d,%s)"
3395 inf
== NULL
? 0 : inf
->num
,
3396 filename
, ret
? ret
->c_str () : "(nil)",
3397 ret
? 0 : *target_errno
);
3401 *target_errno
= FILEIO_ENOSYS
;
3405 /* Like scoped_fd, but specific to target fileio. */
3407 class scoped_target_fd
3410 explicit scoped_target_fd (int fd
) noexcept
3415 ~scoped_target_fd ()
3419 fileio_error target_errno
;
3421 target_fileio_close (m_fd
, &target_errno
);
3425 DISABLE_COPY_AND_ASSIGN (scoped_target_fd
);
3427 int get () const noexcept
3436 /* Read target file FILENAME, in the filesystem as seen by INF. If
3437 INF is NULL, use the filesystem seen by the debugger (GDB or, for
3438 remote targets, the remote stub). Store the result in *BUF_P and
3439 return the size of the transferred data. PADDING additional bytes
3440 are available in *BUF_P. This is a helper function for
3441 target_fileio_read_alloc; see the declaration of that function for
3442 more information. */
3445 target_fileio_read_alloc_1 (struct inferior
*inf
, const char *filename
,
3446 gdb_byte
**buf_p
, int padding
)
3448 size_t buf_alloc
, buf_pos
;
3451 fileio_error target_errno
;
3453 scoped_target_fd
fd (target_fileio_open (inf
, filename
, FILEIO_O_RDONLY
,
3454 0700, false, &target_errno
));
3455 if (fd
.get () == -1)
3458 /* Start by reading up to 4K at a time. The target will throttle
3459 this number down if necessary. */
3461 buf
= (gdb_byte
*) xmalloc (buf_alloc
);
3465 n
= target_fileio_pread (fd
.get (), &buf
[buf_pos
],
3466 buf_alloc
- buf_pos
- padding
, buf_pos
,
3470 /* An error occurred. */
3476 /* Read all there was. */
3486 /* If the buffer is filling up, expand it. */
3487 if (buf_alloc
< buf_pos
* 2)
3490 buf
= (gdb_byte
*) xrealloc (buf
, buf_alloc
);
3500 target_fileio_read_alloc (struct inferior
*inf
, const char *filename
,
3503 return target_fileio_read_alloc_1 (inf
, filename
, buf_p
, 0);
3508 gdb::unique_xmalloc_ptr
<char>
3509 target_fileio_read_stralloc (struct inferior
*inf
, const char *filename
)
3513 LONGEST i
, transferred
;
3515 transferred
= target_fileio_read_alloc_1 (inf
, filename
, &buffer
, 1);
3516 bufstr
= (char *) buffer
;
3518 if (transferred
< 0)
3519 return gdb::unique_xmalloc_ptr
<char> (nullptr);
3521 if (transferred
== 0)
3522 return make_unique_xstrdup ("");
3524 bufstr
[transferred
] = 0;
3526 /* Check for embedded NUL bytes; but allow trailing NULs. */
3527 for (i
= strlen (bufstr
); i
< transferred
; i
++)
3530 warning (_("target file %s "
3531 "contained unexpected null characters"),
3536 return gdb::unique_xmalloc_ptr
<char> (bufstr
);
3541 default_region_ok_for_hw_watchpoint (struct target_ops
*self
,
3542 CORE_ADDR addr
, int len
)
3544 gdbarch
*arch
= current_inferior ()->arch ();
3545 return (len
<= gdbarch_ptr_bit (arch
) / TARGET_CHAR_BIT
);
3549 default_watchpoint_addr_within_range (struct target_ops
*target
,
3551 CORE_ADDR start
, int length
)
3553 return addr
>= start
&& addr
< start
+ length
;
3559 target_stack::find_beneath (const target_ops
*t
) const
3561 /* Look for a non-empty slot at stratum levels beneath T's. */
3562 for (int stratum
= t
->stratum () - 1; stratum
>= 0; --stratum
)
3563 if (m_stack
[stratum
].get () != NULL
)
3564 return m_stack
[stratum
].get ();
3572 find_target_at (enum strata stratum
)
3574 return current_inferior ()->target_at (stratum
);
3582 target_announce_detach (int from_tty
)
3585 const char *exec_file
;
3590 pid
= inferior_ptid
.pid ();
3591 exec_file
= get_exec_file (0);
3592 if (exec_file
== nullptr)
3593 gdb_printf ("Detaching from pid %s\n",
3594 target_pid_to_str (ptid_t (pid
)).c_str ());
3596 gdb_printf (_("Detaching from program: %s, %s\n"), exec_file
,
3597 target_pid_to_str (ptid_t (pid
)).c_str ());
3603 target_announce_attach (int from_tty
, int pid
)
3608 const char *exec_file
= get_exec_file (0);
3610 if (exec_file
!= nullptr)
3611 gdb_printf ("Attaching to program: %s, %s\n", exec_file
,
3612 target_pid_to_str (ptid_t (pid
)).c_str ());
3614 gdb_printf ("Attaching to %s\n",
3615 target_pid_to_str (ptid_t (pid
)).c_str ());
3618 /* The inferior process has died. Long live the inferior! */
3621 generic_mourn_inferior (void)
3623 inferior
*inf
= current_inferior ();
3625 switch_to_no_thread ();
3627 /* Mark breakpoints uninserted in case something tries to delete a
3628 breakpoint while we delete the inferior's threads (which would
3629 fail, since the inferior is long gone). */
3630 mark_breakpoints_out (inf
->pspace
);
3633 exit_inferior (inf
);
3635 /* Note this wipes step-resume breakpoints, so needs to be done
3636 after exit_inferior, which ends up referencing the step-resume
3637 breakpoints through clear_thread_inferior_resources. */
3638 breakpoint_init_inferior (inf
, inf_exited
);
3640 registers_changed ();
3642 reopen_exec_file ();
3643 reinit_frame_cache ();
3645 if (deprecated_detach_hook
)
3646 deprecated_detach_hook ();
3649 /* Convert a normal process ID to a string. Returns the string in a
3653 normal_pid_to_str (ptid_t ptid
)
3655 return string_printf ("process %d", ptid
.pid ());
3659 default_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3661 return normal_pid_to_str (ptid
);
3664 /* Error-catcher for target_find_memory_regions. */
3666 dummy_find_memory_regions (struct target_ops
*self
,
3667 find_memory_region_ftype ignore1
, void *ignore2
)
3669 error (_("Command not implemented for this target."));
3673 /* Error-catcher for target_make_corefile_notes. */
3674 static gdb::unique_xmalloc_ptr
<char>
3675 dummy_make_corefile_notes (struct target_ops
*self
,
3676 bfd
*ignore1
, int *ignore2
)
3678 error (_("Command not implemented for this target."));
3682 #include "target-delegates.c"
3684 /* The initial current target, so that there is always a semi-valid
3687 static dummy_target the_dummy_target
;
3694 return &the_dummy_target
;
3697 static const target_info dummy_target_info
= {
3704 dummy_target::stratum () const
3706 return dummy_stratum
;
3710 debug_target::stratum () const
3712 return debug_stratum
;
3716 dummy_target::info () const
3718 return dummy_target_info
;
3722 debug_target::info () const
3724 return beneath ()->info ();
3730 target_thread_alive (ptid_t ptid
)
3732 return current_inferior ()->top_target ()->thread_alive (ptid
);
3736 target_update_thread_list (void)
3738 current_inferior ()->top_target ()->update_thread_list ();
3742 target_stop (ptid_t ptid
)
3744 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
3746 gdb_assert (!proc_target
->commit_resumed_state
);
3750 warning (_("May not interrupt or stop the target, ignoring attempt"));
3754 current_inferior ()->top_target ()->stop (ptid
);
3762 warning (_("May not interrupt or stop the target, ignoring attempt"));
3766 current_inferior ()->top_target ()->interrupt ();
3772 target_pass_ctrlc (void)
3774 /* Pass the Ctrl-C to the first target that has a thread
3776 for (inferior
*inf
: all_inferiors ())
3778 target_ops
*proc_target
= inf
->process_target ();
3779 if (proc_target
== NULL
)
3782 for (thread_info
*thr
: inf
->non_exited_threads ())
3784 /* A thread can be THREAD_STOPPED and executing, while
3785 running an infcall. */
3786 if (thr
->state
== THREAD_RUNNING
|| thr
->executing ())
3788 /* We can get here quite deep in target layers. Avoid
3789 switching thread context or anything that would
3790 communicate with the target (e.g., to fetch
3791 registers), or flushing e.g., the frame cache. We
3792 just switch inferior in order to be able to call
3793 through the target_stack. */
3794 scoped_restore_current_inferior restore_inferior
;
3795 set_current_inferior (inf
);
3796 current_inferior ()->top_target ()->pass_ctrlc ();
3806 default_target_pass_ctrlc (struct target_ops
*ops
)
3808 target_interrupt ();
3811 /* See target/target.h. */
3814 target_stop_and_wait (ptid_t ptid
)
3816 struct target_waitstatus status
;
3817 bool was_non_stop
= non_stop
;
3822 target_wait (ptid
, &status
, 0);
3824 non_stop
= was_non_stop
;
3827 /* See target/target.h. */
3830 target_continue_no_signal (ptid_t ptid
)
3832 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3835 /* See target/target.h. */
3838 target_continue (ptid_t ptid
, enum gdb_signal signal
)
3840 target_resume (ptid
, 0, signal
);
3843 /* Concatenate ELEM to LIST, a comma-separated list. */
3846 str_comma_list_concat_elem (std::string
*list
, const char *elem
)
3848 if (!list
->empty ())
3849 list
->append (", ");
3851 list
->append (elem
);
3854 /* Helper for target_options_to_string. If OPT is present in
3855 TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
3856 OPT is removed from TARGET_OPTIONS. */
3859 do_option (target_wait_flags
*target_options
, std::string
*ret
,
3860 target_wait_flag opt
, const char *opt_str
)
3862 if ((*target_options
& opt
) != 0)
3864 str_comma_list_concat_elem (ret
, opt_str
);
3865 *target_options
&= ~opt
;
3872 target_options_to_string (target_wait_flags target_options
)
3876 #define DO_TARG_OPTION(OPT) \
3877 do_option (&target_options, &ret, OPT, #OPT)
3879 DO_TARG_OPTION (TARGET_WNOHANG
);
3881 if (target_options
!= 0)
3882 str_comma_list_concat_elem (&ret
, "unknown???");
3888 target_fetch_registers (struct regcache
*regcache
, int regno
)
3890 current_inferior ()->top_target ()->fetch_registers (regcache
, regno
);
3892 regcache
->debug_print_register ("target_fetch_registers", regno
);
3896 target_store_registers (struct regcache
*regcache
, int regno
)
3898 if (!may_write_registers
)
3899 error (_("Writing to registers is not allowed (regno %d)"), regno
);
3901 current_inferior ()->top_target ()->store_registers (regcache
, regno
);
3904 regcache
->debug_print_register ("target_store_registers", regno
);
3909 target_core_of_thread (ptid_t ptid
)
3911 return current_inferior ()->top_target ()->core_of_thread (ptid
);
3915 simple_verify_memory (struct target_ops
*ops
,
3916 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
3918 LONGEST total_xfered
= 0;
3920 while (total_xfered
< size
)
3922 ULONGEST xfered_len
;
3923 enum target_xfer_status status
;
3925 ULONGEST howmuch
= std::min
<ULONGEST
> (sizeof (buf
), size
- total_xfered
);
3927 status
= target_xfer_partial (ops
, TARGET_OBJECT_MEMORY
, NULL
,
3928 buf
, NULL
, lma
+ total_xfered
, howmuch
,
3930 if (status
== TARGET_XFER_OK
3931 && memcmp (data
+ total_xfered
, buf
, xfered_len
) == 0)
3933 total_xfered
+= xfered_len
;
3942 /* Default implementation of memory verification. */
3945 default_verify_memory (struct target_ops
*self
,
3946 const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3948 /* Start over from the top of the target stack. */
3949 return simple_verify_memory (current_inferior ()->top_target (),
3950 data
, memaddr
, size
);
3954 target_verify_memory (const gdb_byte
*data
, CORE_ADDR memaddr
, ULONGEST size
)
3956 target_ops
*target
= current_inferior ()->top_target ();
3958 return target
->verify_memory (data
, memaddr
, size
);
3961 /* The documentation for this function is in its prototype declaration in
3965 target_insert_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3966 enum target_hw_bp_type rw
)
3968 target_ops
*target
= current_inferior ()->top_target ();
3970 return target
->insert_mask_watchpoint (addr
, mask
, rw
);
3973 /* The documentation for this function is in its prototype declaration in
3977 target_remove_mask_watchpoint (CORE_ADDR addr
, CORE_ADDR mask
,
3978 enum target_hw_bp_type rw
)
3980 target_ops
*target
= current_inferior ()->top_target ();
3982 return target
->remove_mask_watchpoint (addr
, mask
, rw
);
3985 /* The documentation for this function is in its prototype declaration
3989 target_masked_watch_num_registers (CORE_ADDR addr
, CORE_ADDR mask
)
3991 target_ops
*target
= current_inferior ()->top_target ();
3993 return target
->masked_watch_num_registers (addr
, mask
);
3996 /* The documentation for this function is in its prototype declaration
4000 target_ranged_break_num_registers (void)
4002 return current_inferior ()->top_target ()->ranged_break_num_registers ();
4007 struct btrace_target_info
*
4008 target_enable_btrace (thread_info
*tp
, const struct btrace_config
*conf
)
4010 return current_inferior ()->top_target ()->enable_btrace (tp
, conf
);
4016 target_disable_btrace (struct btrace_target_info
*btinfo
)
4018 current_inferior ()->top_target ()->disable_btrace (btinfo
);
4024 target_teardown_btrace (struct btrace_target_info
*btinfo
)
4026 current_inferior ()->top_target ()->teardown_btrace (btinfo
);
4032 target_read_btrace (struct btrace_data
*btrace
,
4033 struct btrace_target_info
*btinfo
,
4034 enum btrace_read_type type
)
4036 target_ops
*target
= current_inferior ()->top_target ();
4038 return target
->read_btrace (btrace
, btinfo
, type
);
4043 const struct btrace_config
*
4044 target_btrace_conf (const struct btrace_target_info
*btinfo
)
4046 return current_inferior ()->top_target ()->btrace_conf (btinfo
);
4052 target_stop_recording (void)
4054 current_inferior ()->top_target ()->stop_recording ();
4060 target_save_record (const char *filename
)
4062 current_inferior ()->top_target ()->save_record (filename
);
4068 target_supports_delete_record ()
4070 return current_inferior ()->top_target ()->supports_delete_record ();
4076 target_delete_record (void)
4078 current_inferior ()->top_target ()->delete_record ();
4084 target_record_method (ptid_t ptid
)
4086 return current_inferior ()->top_target ()->record_method (ptid
);
4092 target_record_is_replaying (ptid_t ptid
)
4094 return current_inferior ()->top_target ()->record_is_replaying (ptid
);
4100 target_record_will_replay (ptid_t ptid
, int dir
)
4102 return current_inferior ()->top_target ()->record_will_replay (ptid
, dir
);
4108 target_record_stop_replaying (void)
4110 current_inferior ()->top_target ()->record_stop_replaying ();
4116 target_goto_record_begin (void)
4118 current_inferior ()->top_target ()->goto_record_begin ();
4124 target_goto_record_end (void)
4126 current_inferior ()->top_target ()->goto_record_end ();
4132 target_goto_record (ULONGEST insn
)
4134 current_inferior ()->top_target ()->goto_record (insn
);
4140 target_insn_history (int size
, gdb_disassembly_flags flags
)
4142 current_inferior ()->top_target ()->insn_history (size
, flags
);
4148 target_insn_history_from (ULONGEST from
, int size
,
4149 gdb_disassembly_flags flags
)
4151 current_inferior ()->top_target ()->insn_history_from (from
, size
, flags
);
4157 target_insn_history_range (ULONGEST begin
, ULONGEST end
,
4158 gdb_disassembly_flags flags
)
4160 current_inferior ()->top_target ()->insn_history_range (begin
, end
, flags
);
4166 target_call_history (int size
, record_print_flags flags
)
4168 current_inferior ()->top_target ()->call_history (size
, flags
);
4174 target_call_history_from (ULONGEST begin
, int size
, record_print_flags flags
)
4176 current_inferior ()->top_target ()->call_history_from (begin
, size
, flags
);
4182 target_call_history_range (ULONGEST begin
, ULONGEST end
, record_print_flags flags
)
4184 current_inferior ()->top_target ()->call_history_range (begin
, end
, flags
);
4189 const struct frame_unwind
*
4190 target_get_unwinder (void)
4192 return current_inferior ()->top_target ()->get_unwinder ();
4197 const struct frame_unwind
*
4198 target_get_tailcall_unwinder (void)
4200 return current_inferior ()->top_target ()->get_tailcall_unwinder ();
4206 target_prepare_to_generate_core (void)
4208 current_inferior ()->top_target ()->prepare_to_generate_core ();
4214 target_done_generating_core (void)
4216 current_inferior ()->top_target ()->done_generating_core ();
4221 static char targ_desc
[] =
4222 "Names of targets and files being debugged.\nShows the entire \
4223 stack of targets currently in use (including the exec-file,\n\
4224 core-file, and process, if any), as well as the symbol file name.";
4227 default_rcmd (struct target_ops
*self
, const char *command
,
4228 struct ui_file
*output
)
4230 error (_("\"monitor\" command not supported by this target."));
4234 do_monitor_command (const char *cmd
, int from_tty
)
4236 target_rcmd (cmd
, gdb_stdtarg
);
4239 /* Erases all the memory regions marked as flash. CMD and FROM_TTY are
4243 flash_erase_command (const char *cmd
, int from_tty
)
4245 /* Used to communicate termination of flash operations to the target. */
4246 bool found_flash_region
= false;
4247 gdbarch
*gdbarch
= current_inferior ()->arch ();
4249 std::vector
<mem_region
> mem_regions
= target_memory_map ();
4251 /* Iterate over all memory regions. */
4252 for (const mem_region
&m
: mem_regions
)
4254 /* Is this a flash memory region? */
4255 if (m
.attrib
.mode
== MEM_FLASH
)
4257 found_flash_region
= true;
4258 target_flash_erase (m
.lo
, m
.hi
- m
.lo
);
4260 ui_out_emit_tuple
tuple_emitter (current_uiout
, "erased-regions");
4262 current_uiout
->message (_("Erasing flash memory region at address "));
4263 current_uiout
->field_core_addr ("address", gdbarch
, m
.lo
);
4264 current_uiout
->message (", size = ");
4265 current_uiout
->field_string ("size", hex_string (m
.hi
- m
.lo
));
4266 current_uiout
->message ("\n");
4270 /* Did we do any flash operations? If so, we need to finalize them. */
4271 if (found_flash_region
)
4272 target_flash_done ();
4274 current_uiout
->message (_("No flash memory regions found.\n"));
4277 /* Print the name of each layers of our target stack. */
4280 maintenance_print_target_stack (const char *cmd
, int from_tty
)
4282 gdb_printf (_("The current target stack is:\n"));
4284 for (target_ops
*t
= current_inferior ()->top_target ();
4288 if (t
->stratum () == debug_stratum
)
4290 gdb_printf (" - %s (%s)\n", t
->shortname (), t
->longname ());
4297 target_async (bool enable
)
4299 /* If we are trying to enable async mode then it must be the case that
4300 async mode is possible for this target. */
4301 gdb_assert (!enable
|| target_can_async_p ());
4302 infrun_async (enable
);
4303 current_inferior ()->top_target ()->async (enable
);
4309 target_thread_events (int enable
)
4311 current_inferior ()->top_target ()->thread_events (enable
);
4317 target_supports_set_thread_options (gdb_thread_options options
)
4319 inferior
*inf
= current_inferior ();
4320 return inf
->top_target ()->supports_set_thread_options (options
);
4323 /* Controls if targets can report that they can/are async. This is
4324 just for maintainers to use when debugging gdb. */
4325 bool target_async_permitted
= true;
4328 set_maint_target_async (bool permitted
)
4330 if (have_live_inferiors ())
4331 error (_("Cannot change this setting while the inferior is running."));
4333 target_async_permitted
= permitted
;
4337 get_maint_target_async ()
4339 return target_async_permitted
;
4343 show_maint_target_async (ui_file
*file
, int from_tty
,
4344 cmd_list_element
*c
, const char *value
)
4347 _("Controlling the inferior in "
4348 "asynchronous mode is %s.\n"), value
);
4351 /* Return true if the target operates in non-stop mode even with "set
4355 target_always_non_stop_p (void)
4357 return current_inferior ()->top_target ()->always_non_stop_p ();
4363 target_is_non_stop_p ()
4366 || target_non_stop_enabled
== AUTO_BOOLEAN_TRUE
4367 || (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
4368 && target_always_non_stop_p ()))
4369 && target_can_async_p ());
4375 exists_non_stop_target ()
4377 if (target_is_non_stop_p ())
4380 scoped_restore_current_thread restore_thread
;
4382 for (inferior
*inf
: all_inferiors ())
4384 switch_to_inferior_no_thread (inf
);
4385 if (target_is_non_stop_p ())
4392 /* Controls if targets can report that they always run in non-stop
4393 mode. This is just for maintainers to use when debugging gdb. */
4394 enum auto_boolean target_non_stop_enabled
= AUTO_BOOLEAN_AUTO
;
4396 /* Set callback for maint target-non-stop setting. */
4399 set_maint_target_non_stop (auto_boolean enabled
)
4401 if (have_live_inferiors ())
4402 error (_("Cannot change this setting while the inferior is running."));
4404 target_non_stop_enabled
= enabled
;
4407 /* Get callback for maint target-non-stop setting. */
4410 get_maint_target_non_stop ()
4412 return target_non_stop_enabled
;
4416 show_maint_target_non_stop (ui_file
*file
, int from_tty
,
4417 cmd_list_element
*c
, const char *value
)
4419 if (target_non_stop_enabled
== AUTO_BOOLEAN_AUTO
)
4421 _("Whether the target is always in non-stop mode "
4422 "is %s (currently %s).\n"), value
,
4423 target_always_non_stop_p () ? "on" : "off");
4426 _("Whether the target is always in non-stop mode "
4427 "is %s.\n"), value
);
4430 /* Temporary copies of permission settings. */
4432 static bool may_write_registers_1
= true;
4433 static bool may_write_memory_1
= true;
4434 static bool may_insert_breakpoints_1
= true;
4435 static bool may_insert_tracepoints_1
= true;
4436 static bool may_insert_fast_tracepoints_1
= true;
4437 static bool may_stop_1
= true;
4439 /* Make the user-set values match the real values again. */
4442 update_target_permissions (void)
4444 may_write_registers_1
= may_write_registers
;
4445 may_write_memory_1
= may_write_memory
;
4446 may_insert_breakpoints_1
= may_insert_breakpoints
;
4447 may_insert_tracepoints_1
= may_insert_tracepoints
;
4448 may_insert_fast_tracepoints_1
= may_insert_fast_tracepoints
;
4449 may_stop_1
= may_stop
;
4452 /* The one function handles (most of) the permission flags in the same
4456 set_target_permissions (const char *args
, int from_tty
,
4457 struct cmd_list_element
*c
)
4459 if (target_has_execution ())
4461 update_target_permissions ();
4462 error (_("Cannot change this setting while the inferior is running."));
4465 /* Make the real values match the user-changed values. */
4466 may_insert_breakpoints
= may_insert_breakpoints_1
;
4467 may_insert_tracepoints
= may_insert_tracepoints_1
;
4468 may_insert_fast_tracepoints
= may_insert_fast_tracepoints_1
;
4469 may_stop
= may_stop_1
;
4470 update_observer_mode ();
4473 /* Set some permissions independently of observer mode. */
4476 set_write_memory_registers_permission (const char *args
, int from_tty
,
4477 struct cmd_list_element
*c
)
4479 /* Make the real values match the user-changed values. */
4480 may_write_memory
= may_write_memory_1
;
4481 may_write_registers
= may_write_registers_1
;
4482 update_observer_mode ();
4485 void _initialize_target ();
4488 _initialize_target ()
4490 the_debug_target
= new debug_target ();
4492 add_info ("target", info_target_command
, targ_desc
);
4493 add_info ("files", info_target_command
, targ_desc
);
4495 add_setshow_zuinteger_cmd ("target", class_maintenance
, &targetdebug
, _("\
4496 Set target debugging."), _("\
4497 Show target debugging."), _("\
4498 When non-zero, target debugging is enabled. Higher numbers are more\n\
4502 &setdebuglist
, &showdebuglist
);
4504 add_setshow_boolean_cmd ("trust-readonly-sections", class_support
,
4505 &trust_readonly
, _("\
4506 Set mode for reading from readonly sections."), _("\
4507 Show mode for reading from readonly sections."), _("\
4508 When this mode is on, memory reads from readonly sections (such as .text)\n\
4509 will be read from the object file instead of from the target. This will\n\
4510 result in significant performance improvement for remote targets."),
4512 show_trust_readonly
,
4513 &setlist
, &showlist
);
4515 add_com ("monitor", class_obscure
, do_monitor_command
,
4516 _("Send a command to the remote monitor (remote targets only)."));
4518 add_cmd ("target-stack", class_maintenance
, maintenance_print_target_stack
,
4519 _("Print the name of each layer of the internal target stack."),
4520 &maintenanceprintlist
);
4522 add_setshow_boolean_cmd ("target-async", no_class
,
4524 Set whether gdb controls the inferior in asynchronous mode."), _("\
4525 Show whether gdb controls the inferior in asynchronous mode."), _("\
4526 Tells gdb whether to control the inferior in asynchronous mode."),
4527 set_maint_target_async
,
4528 get_maint_target_async
,
4529 show_maint_target_async
,
4530 &maintenance_set_cmdlist
,
4531 &maintenance_show_cmdlist
);
4533 add_setshow_auto_boolean_cmd ("target-non-stop", no_class
,
4535 Set whether gdb always controls the inferior in non-stop mode."), _("\
4536 Show whether gdb always controls the inferior in non-stop mode."), _("\
4537 Tells gdb whether to control the inferior in non-stop mode."),
4538 set_maint_target_non_stop
,
4539 get_maint_target_non_stop
,
4540 show_maint_target_non_stop
,
4541 &maintenance_set_cmdlist
,
4542 &maintenance_show_cmdlist
);
4544 add_setshow_boolean_cmd ("may-write-registers", class_support
,
4545 &may_write_registers_1
, _("\
4546 Set permission to write into registers."), _("\
4547 Show permission to write into registers."), _("\
4548 When this permission is on, GDB may write into the target's registers.\n\
4549 Otherwise, any sort of write attempt will result in an error."),
4550 set_write_memory_registers_permission
, NULL
,
4551 &setlist
, &showlist
);
4553 add_setshow_boolean_cmd ("may-write-memory", class_support
,
4554 &may_write_memory_1
, _("\
4555 Set permission to write into target memory."), _("\
4556 Show permission to write into target memory."), _("\
4557 When this permission is on, GDB may write into the target's memory.\n\
4558 Otherwise, any sort of write attempt will result in an error."),
4559 set_write_memory_registers_permission
, NULL
,
4560 &setlist
, &showlist
);
4562 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support
,
4563 &may_insert_breakpoints_1
, _("\
4564 Set permission to insert breakpoints in the target."), _("\
4565 Show permission to insert breakpoints in the target."), _("\
4566 When this permission is on, GDB may insert breakpoints in the program.\n\
4567 Otherwise, any sort of insertion attempt will result in an error."),
4568 set_target_permissions
, NULL
,
4569 &setlist
, &showlist
);
4571 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support
,
4572 &may_insert_tracepoints_1
, _("\
4573 Set permission to insert tracepoints in the target."), _("\
4574 Show permission to insert tracepoints in the target."), _("\
4575 When this permission is on, GDB may insert tracepoints in the program.\n\
4576 Otherwise, any sort of insertion attempt will result in an error."),
4577 set_target_permissions
, NULL
,
4578 &setlist
, &showlist
);
4580 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support
,
4581 &may_insert_fast_tracepoints_1
, _("\
4582 Set permission to insert fast tracepoints in the target."), _("\
4583 Show permission to insert fast tracepoints in the target."), _("\
4584 When this permission is on, GDB may insert fast tracepoints.\n\
4585 Otherwise, any sort of insertion attempt will result in an error."),
4586 set_target_permissions
, NULL
,
4587 &setlist
, &showlist
);
4589 add_setshow_boolean_cmd ("may-interrupt", class_support
,
4591 Set permission to interrupt or signal the target."), _("\
4592 Show permission to interrupt or signal the target."), _("\
4593 When this permission is on, GDB may interrupt/stop the target's execution.\n\
4594 Otherwise, any attempt to interrupt or stop will be ignored."),
4595 set_target_permissions
, NULL
,
4596 &setlist
, &showlist
);
4598 add_com ("flash-erase", no_class
, flash_erase_command
,
4599 _("Erase all flash memory regions."));
4601 add_setshow_boolean_cmd ("auto-connect-native-target", class_support
,
4602 &auto_connect_native_target
, _("\
4603 Set whether GDB may automatically connect to the native target."), _("\
4604 Show whether GDB may automatically connect to the native target."), _("\
4605 When on, and GDB is not connected to a target yet, GDB\n\
4606 attempts \"run\" and other commands with the native target."),
4607 NULL
, show_auto_connect_native_target
,
4608 &setlist
, &showlist
);