1 /* Displaced stepping related things.
3 Copyright (C) 2020-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef DISPLACED_STEPPING_H
21 #define DISPLACED_STEPPING_H
23 #include "gdbsupport/array-view.h"
24 #include "gdbsupport/byte-vector.h"
29 /* True if we are debugging displaced stepping. */
31 extern bool debug_displaced
;
33 /* Print a "displaced" debug statement. */
35 #define displaced_debug_printf(fmt, ...) \
36 debug_prefixed_printf_cond (debug_displaced, "displaced",fmt, ##__VA_ARGS__)
38 enum displaced_step_prepare_status
40 /* A displaced stepping buffer was successfully allocated and prepared. */
41 DISPLACED_STEP_PREPARE_STATUS_OK
,
43 /* This particular instruction can't be displaced stepped, GDB should fall
44 back on in-line stepping. */
45 DISPLACED_STEP_PREPARE_STATUS_CANT
,
47 /* Not enough resources are available at this time, try again later. */
48 DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
,
51 enum displaced_step_finish_status
53 /* Either the instruction was stepped and fixed up, or the specified thread
54 wasn't executing a displaced step (in which case there's nothing to
56 DISPLACED_STEP_FINISH_STATUS_OK
,
58 /* The thread started a displaced step, but didn't complete it. */
59 DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
,
62 /* Data returned by a gdbarch displaced_step_copy_insn method, to be passed to
63 the matching displaced_step_fixup method. */
65 struct displaced_step_copy_insn_closure
67 virtual ~displaced_step_copy_insn_closure () = 0;
70 using displaced_step_copy_insn_closure_up
71 = std::unique_ptr
<displaced_step_copy_insn_closure
>;
73 /* A simple displaced step closure that contains only a byte buffer. */
75 struct buf_displaced_step_copy_insn_closure
: displaced_step_copy_insn_closure
77 buf_displaced_step_copy_insn_closure (int buf_size
)
81 /* The content of this buffer is up to the user of the class, but typically
82 original instruction bytes, used during fixup to determine what needs to
87 /* Per-inferior displaced stepping state. */
89 struct displaced_step_inferior_state
91 displaced_step_inferior_state ()
96 /* Put this object back in its original state. */
99 failed_before
= false;
100 in_progress_count
= 0;
104 /* True if preparing a displaced step ever failed. If so, we won't
105 try displaced stepping for this inferior again. */
108 /* Number of displaced steps in progress for this inferior. */
109 unsigned int in_progress_count
;
111 /* If true, this tells GDB that it's not worth asking the gdbarch displaced
112 stepping implementation to prepare a displaced step, because it would
113 return UNAVAILABLE. This is set and reset by the gdbarch in the
114 displaced_step_prepare and displaced_step_finish methods. */
118 /* Per-thread displaced stepping state. */
120 struct displaced_step_thread_state
122 /* Return true if this thread is currently executing a displaced step. */
123 bool in_progress () const
125 return m_original_gdbarch
!= nullptr;
128 /* Return the gdbarch of the thread prior to the step. */
129 gdbarch
*get_original_gdbarch () const
131 return m_original_gdbarch
;
134 /* Mark this thread as currently executing a displaced step.
136 ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
138 void set (gdbarch
*original_gdbarch
)
140 m_original_gdbarch
= original_gdbarch
;
143 /* Mark this thread as no longer executing a displaced step. */
146 m_original_gdbarch
= nullptr;
150 gdbarch
*m_original_gdbarch
= nullptr;
153 /* Control access to multiple displaced stepping buffers at fixed addresses. */
155 struct displaced_step_buffers
157 explicit displaced_step_buffers (gdb::array_view
<CORE_ADDR
> buffer_addrs
)
159 gdb_assert (buffer_addrs
.size () > 0);
161 m_buffers
.reserve (buffer_addrs
.size ());
163 for (CORE_ADDR buffer_addr
: buffer_addrs
)
164 m_buffers
.emplace_back (buffer_addr
);
167 displaced_step_prepare_status
prepare (thread_info
*thread
,
168 CORE_ADDR
&displaced_pc
);
170 displaced_step_finish_status
finish (gdbarch
*arch
, thread_info
*thread
,
173 const displaced_step_copy_insn_closure
*
174 copy_insn_closure_by_addr (CORE_ADDR addr
);
176 void restore_in_ptid (ptid_t ptid
);
180 /* State of a single buffer. */
182 struct displaced_step_buffer
184 explicit displaced_step_buffer (CORE_ADDR addr
)
188 /* Address of the buffer. */
189 const CORE_ADDR addr
;
191 /* The original PC of the instruction currently being stepped. */
192 CORE_ADDR original_pc
= 0;
194 /* If set, the thread currently using the buffer. If unset, the buffer is not
196 thread_info
*current_thread
= nullptr;
198 /* Saved copy of the bytes in the displaced buffer, to be restored once the
199 buffer is no longer used. */
200 gdb::byte_vector saved_copy
;
202 /* Closure obtained from gdbarch_displaced_step_copy_insn, to be passed to
203 gdbarch_displaced_step_fixup_insn. */
204 displaced_step_copy_insn_closure_up copy_insn_closure
;
207 std::vector
<displaced_step_buffer
> m_buffers
;
210 #endif /* DISPLACED_STEPPING_H */