1 /* Displaced stepping related things.
3 Copyright (C) 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "displaced-stepping.h"
23 #include "cli/cli-cmds.h"
27 #include "gdbthread.h"
30 #include "target/target.h"
32 /* Default destructor for displaced_step_copy_insn_closure. */
34 displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
37 bool debug_displaced
= false;
40 show_debug_displaced (struct ui_file
*file
, int from_tty
,
41 struct cmd_list_element
*c
, const char *value
)
43 fprintf_filtered (file
, _("Displace stepping debugging is %s.\n"), value
);
46 displaced_step_prepare_status
47 displaced_step_buffer::prepare (thread_info
*thread
, CORE_ADDR
&displaced_pc
)
49 gdb_assert (!thread
->displaced_step_state
.in_progress ());
51 /* Is a thread currently using the buffer? */
52 if (m_current_thread
!= nullptr)
54 /* If so, it better not be this thread. */
55 gdb_assert (thread
!= m_current_thread
);
56 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
59 regcache
*regcache
= get_thread_regcache (thread
);
60 const address_space
*aspace
= regcache
->aspace ();
61 gdbarch
*arch
= regcache
->arch ();
62 ULONGEST len
= gdbarch_max_insn_length (arch
);
64 if (breakpoint_in_range_p (aspace
, m_addr
, len
))
66 /* There's a breakpoint set in the scratch pad location range
67 (which is usually around the entry point). We'd either
68 install it before resuming, which would overwrite/corrupt the
69 scratch pad, or if it was already inserted, this displaced
70 step would overwrite it. The latter is OK in the sense that
71 we already assume that no thread is going to execute the code
72 in the scratch pad range (after initial startup) anyway, but
73 the former is unacceptable. Simply punt and fallback to
74 stepping over this breakpoint in-line. */
75 displaced_debug_printf ("breakpoint set in scratch pad. "
76 "Stepping over breakpoint in-line instead.");
78 return DISPLACED_STEP_PREPARE_STATUS_CANT
;
81 m_original_pc
= regcache_read_pc (regcache
);
82 displaced_pc
= m_addr
;
84 /* Save the original contents of the displaced stepping buffer. */
85 m_saved_copy
.resize (len
);
87 int status
= target_read_memory (m_addr
, m_saved_copy
.data (), len
);
89 throw_error (MEMORY_ERROR
,
90 _("Error accessing memory address %s (%s) for "
91 "displaced-stepping scratch space."),
92 paddress (arch
, m_addr
), safe_strerror (status
));
94 displaced_debug_printf ("saved %s: %s",
95 paddress (arch
, m_addr
),
96 displaced_step_dump_bytes
97 (m_saved_copy
.data (), len
).c_str ());
99 /* Save this in a local variable first, so it's released if code below
101 displaced_step_copy_insn_closure_up copy_insn_closure
102 = gdbarch_displaced_step_copy_insn (arch
, m_original_pc
, m_addr
, regcache
);
104 if (copy_insn_closure
== nullptr)
106 /* The architecture doesn't know how or want to displaced step
107 this instruction or instruction sequence. Fallback to
108 stepping over the breakpoint in-line. */
109 return DISPLACED_STEP_PREPARE_STATUS_CANT
;
112 /* Resume execution at the copy. */
113 regcache_write_pc (regcache
, m_addr
);
115 /* This marks the buffer as being in use. */
116 m_current_thread
= thread
;
118 /* Save this, now that we know everything went fine. */
119 m_copy_insn_closure
= std::move (copy_insn_closure
);
121 /* Tell infrun not to try preparing a displaced step again for this inferior. */
122 thread
->inf
->displaced_step_state
.unavailable
= true;
124 return DISPLACED_STEP_PREPARE_STATUS_OK
;
128 write_memory_ptid (ptid_t ptid
, CORE_ADDR memaddr
,
129 const gdb_byte
*myaddr
, int len
)
131 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
133 inferior_ptid
= ptid
;
134 write_memory (memaddr
, myaddr
, len
);
138 displaced_step_instruction_executed_successfully (gdbarch
*arch
,
141 if (signal
!= GDB_SIGNAL_TRAP
)
144 if (target_stopped_by_watchpoint ())
146 if (gdbarch_have_nonsteppable_watchpoint (arch
)
147 || target_have_steppable_watchpoint ())
154 displaced_step_finish_status
155 displaced_step_buffer::finish (gdbarch
*arch
, thread_info
*thread
,
158 gdb_assert (thread
->displaced_step_state
.in_progress ());
159 gdb_assert (thread
== m_current_thread
);
161 /* Move this to a local variable so it's released in case something goes
163 displaced_step_copy_insn_closure_up copy_insn_closure
164 = std::move (m_copy_insn_closure
);
165 gdb_assert (copy_insn_closure
!= nullptr);
167 /* Reset M_CURRENT_THREAD immediately to mark the buffer as available, in case
168 something goes wrong below. */
169 m_current_thread
= nullptr;
171 /* Now that a buffer gets freed, tell infrun it can ask us to prepare a displaced
172 step again for this inferior. Do that here in case something goes wrong
174 thread
->inf
->displaced_step_state
.unavailable
= false;
176 ULONGEST len
= gdbarch_max_insn_length (arch
);
178 write_memory_ptid (thread
->ptid
, m_addr
,
179 m_saved_copy
.data (), len
);
181 displaced_debug_printf ("restored %s %s",
182 target_pid_to_str (thread
->ptid
).c_str (),
183 paddress (arch
, m_addr
));
185 regcache
*rc
= get_thread_regcache (thread
);
187 bool instruction_executed_successfully
188 = displaced_step_instruction_executed_successfully (arch
, sig
);
190 if (instruction_executed_successfully
)
192 gdbarch_displaced_step_fixup (arch
, copy_insn_closure
.get (), m_original_pc
,
194 return DISPLACED_STEP_FINISH_STATUS_OK
;
198 /* Since the instruction didn't complete, all we can do is relocate the
200 CORE_ADDR pc
= regcache_read_pc (rc
);
201 pc
= m_original_pc
+ (pc
- m_addr
);
202 regcache_write_pc (rc
, pc
);
203 return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
;
207 const displaced_step_copy_insn_closure
*
208 displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr
)
211 return m_copy_insn_closure
.get ();
217 displaced_step_buffer::restore_in_ptid (ptid_t ptid
)
219 if (m_current_thread
!= nullptr)
221 regcache
*regcache
= get_thread_regcache (m_current_thread
);
222 gdbarch
*arch
= regcache
->arch ();
223 ULONGEST len
= gdbarch_max_insn_length (arch
);
225 write_memory_ptid (ptid
, m_addr
, m_saved_copy
.data (), len
);
227 displaced_debug_printf ("restored in ptid %s %s",
228 target_pid_to_str (ptid
).c_str (),
229 paddress (arch
, m_addr
));
233 void _initialize_displaced_stepping ();
235 _initialize_displaced_stepping ()
237 add_setshow_boolean_cmd ("displaced", class_maintenance
,
238 &debug_displaced
, _("\
239 Set displaced stepping debugging."), _("\
240 Show displaced stepping debugging."), _("\
241 When non-zero, displaced stepping specific debugging is enabled."),
243 show_debug_displaced
,
244 &setdebuglist
, &showdebuglist
);