Commit 187b041e authored by Simon Marchi's avatar Simon Marchi
Browse files

gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps

Today, GDB only allows a single displaced stepping operation to happen
per inferior at a time.  There is a single displaced stepping buffer per
inferior, whose address is fixed (obtained with
gdbarch_displaced_step_location), managed by infrun.c.

In the case of the AMD ROCm target [1] (in the context of which this
work has been done), it is typical to have thousands of threads (or
waves, in SMT terminology) executing the same code, hitting the same
breakpoint (possibly conditional) and needing to to displaced step it at
the same time.  The limitation of only one displaced step executing at a
any given time becomes a real bottleneck.

To fix this bottleneck, we want to make it possible for threads of a
same inferior to execute multiple displaced steps in parallel.  This
patch builds the foundation for that.

In essence, this patch moves the task of preparing a displaced step and
cleaning up after to gdbarch functions.  This allows using different
schemes for allocating and managing displaced stepping buffers for
different platforms.  The gdbarch decides how to assign a buffer to a
thread that needs to execute a displaced step.

On the ROCm target, we are able to allocate one displaced stepping
buffer per thread, so a thread will never have to wait to execute a
displaced step.

On Linux, the entry point of the executable if used as the displaced
stepping buffer, since we assume that this code won't get used after
startup.  From what I saw (I checked with a binary generated against
glibc and musl), on AMD64 we have enough space there to fit two
displaced stepping buffers.  A subsequent patch makes AMD64/Linux use
two buffers.

In addition to having multiple displaced stepping buffers, there is also
the idea of sharing displaced stepping buffers between threads.  Two
threads doing displaced steps for the same PC could use the same buffer
at the same time.  Two threads stepping over the same instruction (same
opcode) at two different PCs may also be able to share a displaced
stepping buffer.  This is an idea for future patches, but the
architecture built by this patch is made to allow this.

Now, the implementation details.  The main part of this patch is moving
the responsibility of preparing and finishing a displaced step to the
gdbarch.  Before this patch, preparing a displaced step is driven by the
displaced_step_prepare_throw function.  It does some calls to the
gdbarch to do some low-level operations, but the high-level logic is
there.  The steps are roughly:

- Ask the gdbarch for the displaced step buffer location
- Save the existing bytes in the displaced step buffer
- Ask the gdbarch to copy the instruction into the displaced step buffer
- Set the pc of the thread to the beginning of the displaced step buffer

Similarly, the "fixup" phase, executed after the instruction was
successfully single-stepped, is driven by the infrun code (function
displaced_step_finish).  The steps are roughly:

- Restore the original bytes in the displaced stepping buffer
- Ask the gdbarch to fixup the instruction result (adjust the target's
  registers or memory to do as if the instruction had been executed in
  its original location)

The displaced_step_inferior_state::step_thread field indicates which
thread (if any) is currently using the displaced stepping buffer, so it
is used by displaced_step_prepare_throw to check if the displaced
stepping buffer is free to use or not.

This patch defers the whole task of preparing and cleaning up after a
displaced step to the gdbarch.  Two new main gdbarch methods are added,
with the following semantics:

  - gdbarch_displaced_step_prepare: Prepare for the given thread to
    execute a displaced step of the instruction located at its current PC.
    Upon return, everything should be ready for GDB to resume the thread
    (with either a single step or continue, as indicated by
    gdbarch_displaced_step_hw_singlestep) to make it displaced step the
    instruction.

  - gdbarch_displaced_step_finish: Called when the thread stopped after
    having started a displaced step.  Verify if the instruction was
    executed, if so apply any fixup required to compensate for the fact
    that the instruction was executed at a different place than its
    original pc.  Release any resources that were allocated for this
    displaced step.  Upon return, everything should be ready for GDB to
    resume the thread in its "normal" code path.

The displaced_step_prepare_throw function now pretty much just offloads
to gdbarch_displaced_step_prepare and the displaced_step_finish function
offloads to gdbarch_displaced_step_finish.

The gdbarch_displaced_step_location method is now unnecessary, so is
removed.  Indeed, the core of GDB doesn't know how many displaced step
buffers there are nor where they are.

To keep the existing behavior for existing architectures, the logic that
was previously implemented in infrun.c for preparing and finishing a
displaced step is moved to displaced-stepping.c, to the
displaced_step_buffer class.  Architectures are modified to implement
the new gdbarch methods using this class.  The behavior is not expected
to change.

The other important change (which arises from the above) is that the
core of GDB no longer prevents concurrent displaced steps.  Before this
patch, start_step_over walks the global step over chain and tries to
initiate a step over (whether it is in-line or displaced).  It follows
these rules:

  - if an in-line step is in progress (in any inferior), don't start any
    other step over
  - if a displaced step is in progress for an inferior, don't start
    another displaced step for that inferior

After starting a displaced step for a given inferior, it won't start
another displaced step for that inferior.

In the new code, start_step_over simply tries to initiate step overs for
all the threads in the list.  But because threads may be added back to
the global list as it iterates the global list, trying to initiate step
overs, start_step_over now starts by stealing the global queue into a
local queue and iterates on the local queue.  In the typical case, each
thread will either:

  - have initiated a displaced step and be resumed
  - have been added back by the global step over queue by
    displaced_step_prepare_throw, because the gdbarch will have returned
    that there aren't enough resources (i.e. buffers) to initiate a
    displaced step for that thread

Lastly, if start_step_over initiates an in-line step, it stops
iterating, and moves back whatever remaining threads it had in its local
step over queue to the global step over queue.

Two other gdbarch methods are added, to handle some slightly annoying
corner cases.  They feel awkwardly specific to these cases, but I don't
see any way around them:

  - gdbarch_displaced_step_copy_insn_closure_by_addr: in
    arm_pc_is_thumb, arm-tdep.c wants to get the closure for a given
    buffer address.

  - gdbarch_displaced_step_restore_all_in_ptid: when a process forks
    (at least on Linux), the address space is copied.  If some displaced
    step buffers were in use at the time of the fork, we need to restore
    the original bytes in the child's address space.

These two adjustments are also made in infrun.c:

  - prepare_for_detach: there may be multiple threads doing displaced
    steps when we detach, so wait until all of them are done

  - handle_inferior_event: when we handle a fork event for a given
    thread, it's possible that other threads are doing a displaced step at
    the same time.  Make sure to restore the displaced step buffer
    contents in the child for them.

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

gdb/ChangeLog:

	* displaced-stepping.h (struct
	displaced_step_copy_insn_closure): Adjust comments.
	(struct displaced_step_inferior_state) <step_thread,
	step_gdbarch, step_closure, step_original, step_copy,
	step_saved_copy>: Remove fields.
	(struct displaced_step_thread_state): New.
	(struct displaced_step_buffer): New.
	* displaced-stepping.c (displaced_step_buffer::prepare): New.
	(write_memory_ptid): Move from infrun.c.
	(displaced_step_instruction_executed_successfully): New,
	factored out of displaced_step_finish.
	(displaced_step_buffer::finish): New.
	(displaced_step_buffer::copy_insn_closure_by_addr): New.
	(displaced_step_buffer::restore_in_ptid): New.
	* gdbarch.sh (displaced_step_location): Remove.
	(displaced_step_prepare, displaced_step_finish,
	displaced_step_copy_insn_closure_by_addr,
	displaced_step_restore_all_in_ptid): New.
	* gdbarch.c: Re-generate.
	* gdbarch.h: Re-generate.
	* gdbthread.h (class thread_info) <displaced_step_state>: New
	field.
	(thread_step_over_chain_remove): New declaration.
	(thread_step_over_chain_next): New declaration.
	(thread_step_over_chain_length): New declaration.
	* thread.c (thread_step_over_chain_remove): Make non-static.
	(thread_step_over_chain_next): New.
	(global_thread_step_over_chain_next): Use
	thread_step_over_chain_next.
	(thread_step_over_chain_length): New.
	(global_thread_step_over_chain_enqueue): Add debug print.
	(global_thread_step_over_chain_remove): Add debug print.
	* infrun.h (get_displaced_step_copy_insn_closure_by_addr):
	Remove.
	* infrun.c (get_displaced_stepping_state): New.
	(displaced_step_in_progress_any_inferior): Remove.
	(displaced_step_in_progress_thread): Adjust.
	(displaced_step_in_progress): Adjust.
	(displaced_step_in_progress_any_thread): New.
	(get_displaced_step_copy_insn_closure_by_addr): Remove.
	(gdbarch_supports_displaced_stepping): Use
	gdbarch_displaced_step_prepare_p.
	(displaced_step_reset): Change parameter from inferior to
	thread.
	(displaced_step_prepare_throw): Implement using
	gdbarch_displaced_step_prepare.
	(write_memory_ptid): Move to displaced-step.c.
	(displaced_step_restore): Remove.
	(displaced_step_finish): Implement using
	gdbarch_displaced_step_finish.
	(start_step_over): Allow starting more than one displaced step.
	(prepare_for_detach): Handle possibly multiple threads doing
	displaced steps.
	(handle_inferior_event): Handle possibility that fork event
	happens while another thread displaced steps.
	* linux-tdep.h (linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter.
	* linux-tdep.c (struct linux_info) <disp_step_buf>: New field.
	(linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter,
	register displaced step methods if true.
	(_initialize_linux_tdep): Register inferior_execd observer.
	* amd64-linux-tdep.c (amd64_linux_init_abi_common): Add
	supports_displaced_step parameter, adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	(amd64_linux_init_abi): Adjust call to
	amd64_linux_init_abi_common.
	(amd64_x32_linux_init_abi): Likewise.
	* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-linux-tdep.c (arm_linux_init_abi): Likewise.
	* i386-linux-tdep.c (i386_linux_init_abi): Likewise.
	* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust call to
	linux_init_abi.
	* arc-linux-tdep.c (arc_linux_init_osabi): Likewise.
	* bfin-linux-tdep.c (bfin_linux_init_abi): Likewise.
	* cris-linux-tdep.c (cris_linux_init_abi): Likewise.
	* csky-linux-tdep.c (csky_linux_init_abi): Likewise.
	* frv-linux-tdep.c (frv_linux_init_abi): Likewise.
	* hppa-linux-tdep.c (hppa_linux_init_abi): Likewise.
	* ia64-linux-tdep.c (ia64_linux_init_abi): Likewise.
	* m32r-linux-tdep.c (m32r_linux_init_abi): Likewise.
	* m68k-linux-tdep.c (m68k_linux_init_abi): Likewise.
	* microblaze-linux-tdep.c (microblaze_linux_init_abi): Likewise.
	* mips-linux-tdep.c (mips_linux_init_abi): Likewise.
	* mn10300-linux-tdep.c (am33_linux_init_osabi): Likewise.
	* nios2-linux-tdep.c (nios2_linux_init_abi): Likewise.
	* or1k-linux-tdep.c (or1k_linux_init_abi): Likewise.
	* riscv-linux-tdep.c (riscv_linux_init_abi): Likewise.
	* s390-linux-tdep.c (s390_linux_init_abi_any): Likewise.
	* sh-linux-tdep.c (sh_linux_init_abi): Likewise.
	* sparc-linux-tdep.c (sparc32_linux_init_abi): Likewise.
	* sparc64-linux-tdep.c (sparc64_linux_init_abi): Likewise.
	* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Likewise.
	* tilegx-linux-tdep.c (tilegx_linux_init_abi): Likewise.
	* xtensa-linux-tdep.c (xtensa_linux_init_abi): Likewise.
	* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-tdep.c (arm_pc_is_thumb): Call
	gdbarch_displaced_step_copy_insn_closure_by_addr instead of
	get_displaced_step_copy_insn_closure_by_addr.
	* rs6000-aix-tdep.c (rs6000_aix_init_osabi): Adjust calls to
	clear gdbarch methods.
	* rs6000-tdep.c (struct ppc_inferior_data): New structure.
	(get_ppc_per_inferior): New function.
	(ppc_displaced_step_prepare): New function.
	(ppc_displaced_step_finish): New function.
	(ppc_displaced_step_restore_all_in_ptid): New function.
	(rs6000_gdbarch_init): Register new gdbarch methods.
	* s390-tdep.c (s390_gdbarch_init): Don't call
	set_gdbarch_displaced_step_location, set new gdbarch methods.

gdb/testsuite/ChangeLog:

	* gdb.arch/amd64-disp-step-avx.exp: Adjust pattern.
	* gdb.threads/forking-threads-plus-breakpoint.exp: Likewise.
	* gdb.threads/non-stop-fair-events.exp: Likewise.

Change-Id: I387cd235a442d0620ec43608fd3dc0097fcbf8c8
parent c7acb87b
2020-12-04 Simon Marchi <simon.marchi@efficios.com>
* displaced-stepping.h (struct
displaced_step_copy_insn_closure): Adjust comments.
(struct displaced_step_inferior_state) <step_thread,
step_gdbarch, step_closure, step_original, step_copy,
step_saved_copy>: Remove fields.
(struct displaced_step_thread_state): New.
(struct displaced_step_buffer): New.
* displaced-stepping.c (displaced_step_buffer::prepare): New.
(write_memory_ptid): Move from infrun.c.
(displaced_step_instruction_executed_successfully): New,
factored out of displaced_step_finish.
(displaced_step_buffer::finish): New.
(displaced_step_buffer::copy_insn_closure_by_addr): New.
(displaced_step_buffer::restore_in_ptid): New.
* gdbarch.sh (displaced_step_location): Remove.
(displaced_step_prepare, displaced_step_finish,
displaced_step_copy_insn_closure_by_addr,
displaced_step_restore_all_in_ptid): New.
* gdbarch.c: Re-generate.
* gdbarch.h: Re-generate.
* gdbthread.h (class thread_info) <displaced_step_state>: New
field.
(thread_step_over_chain_remove): New declaration.
(thread_step_over_chain_next): New declaration.
(thread_step_over_chain_length): New declaration.
* thread.c (thread_step_over_chain_remove): Make non-static.
(thread_step_over_chain_next): New.
(global_thread_step_over_chain_next): Use
thread_step_over_chain_next.
(thread_step_over_chain_length): New.
(global_thread_step_over_chain_enqueue): Add debug print.
(global_thread_step_over_chain_remove): Add debug print.
* infrun.h (get_displaced_step_copy_insn_closure_by_addr):
Remove.
* infrun.c (get_displaced_stepping_state): New.
(displaced_step_in_progress_any_inferior): Remove.
(displaced_step_in_progress_thread): Adjust.
(displaced_step_in_progress): Adjust.
(displaced_step_in_progress_any_thread): New.
(get_displaced_step_copy_insn_closure_by_addr): Remove.
(gdbarch_supports_displaced_stepping): Use
gdbarch_displaced_step_prepare_p.
(displaced_step_reset): Change parameter from inferior to
thread.
(displaced_step_prepare_throw): Implement using
gdbarch_displaced_step_prepare.
(write_memory_ptid): Move to displaced-step.c.
(displaced_step_restore): Remove.
(displaced_step_finish): Implement using
gdbarch_displaced_step_finish.
(start_step_over): Allow starting more than one displaced step.
(prepare_for_detach): Handle possibly multiple threads doing
displaced steps.
(handle_inferior_event): Handle possibility that fork event
happens while another thread displaced steps.
* linux-tdep.h (linux_displaced_step_prepare): New.
(linux_displaced_step_finish): New.
(linux_displaced_step_copy_insn_closure_by_addr): New.
(linux_displaced_step_restore_all_in_ptid): New.
(linux_init_abi): Add supports_displaced_step parameter.
* linux-tdep.c (struct linux_info) <disp_step_buf>: New field.
(linux_displaced_step_prepare): New.
(linux_displaced_step_finish): New.
(linux_displaced_step_copy_insn_closure_by_addr): New.
(linux_displaced_step_restore_all_in_ptid): New.
(linux_init_abi): Add supports_displaced_step parameter,
register displaced step methods if true.
(_initialize_linux_tdep): Register inferior_execd observer.
* amd64-linux-tdep.c (amd64_linux_init_abi_common): Add
supports_displaced_step parameter, adjust call to
linux_init_abi. Remove call to
set_gdbarch_displaced_step_location.
(amd64_linux_init_abi): Adjust call to
amd64_linux_init_abi_common.
(amd64_x32_linux_init_abi): Likewise.
* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust call to
linux_init_abi. Remove call to
set_gdbarch_displaced_step_location.
* arm-linux-tdep.c (arm_linux_init_abi): Likewise.
* i386-linux-tdep.c (i386_linux_init_abi): Likewise.
* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust call to
linux_init_abi.
* arc-linux-tdep.c (arc_linux_init_osabi): Likewise.
* bfin-linux-tdep.c (bfin_linux_init_abi): Likewise.
* cris-linux-tdep.c (cris_linux_init_abi): Likewise.
* csky-linux-tdep.c (csky_linux_init_abi): Likewise.
* frv-linux-tdep.c (frv_linux_init_abi): Likewise.
* hppa-linux-tdep.c (hppa_linux_init_abi): Likewise.
* ia64-linux-tdep.c (ia64_linux_init_abi): Likewise.
* m32r-linux-tdep.c (m32r_linux_init_abi): Likewise.
* m68k-linux-tdep.c (m68k_linux_init_abi): Likewise.
* microblaze-linux-tdep.c (microblaze_linux_init_abi): Likewise.
* mips-linux-tdep.c (mips_linux_init_abi): Likewise.
* mn10300-linux-tdep.c (am33_linux_init_osabi): Likewise.
* nios2-linux-tdep.c (nios2_linux_init_abi): Likewise.
* or1k-linux-tdep.c (or1k_linux_init_abi): Likewise.
* riscv-linux-tdep.c (riscv_linux_init_abi): Likewise.
* s390-linux-tdep.c (s390_linux_init_abi_any): Likewise.
* sh-linux-tdep.c (sh_linux_init_abi): Likewise.
* sparc-linux-tdep.c (sparc32_linux_init_abi): Likewise.
* sparc64-linux-tdep.c (sparc64_linux_init_abi): Likewise.
* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Likewise.
* tilegx-linux-tdep.c (tilegx_linux_init_abi): Likewise.
* xtensa-linux-tdep.c (xtensa_linux_init_abi): Likewise.
* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust call to
linux_init_abi. Remove call to
set_gdbarch_displaced_step_location.
* arm-tdep.c (arm_pc_is_thumb): Call
gdbarch_displaced_step_copy_insn_closure_by_addr instead of
get_displaced_step_copy_insn_closure_by_addr.
* rs6000-aix-tdep.c (rs6000_aix_init_osabi): Adjust calls to
clear gdbarch methods.
* rs6000-tdep.c (struct ppc_inferior_data): New structure.
(get_ppc_per_inferior): New function.
(ppc_displaced_step_prepare): New function.
(ppc_displaced_step_finish): New function.
(ppc_displaced_step_restore_all_in_ptid): New function.
(rs6000_gdbarch_init): Register new gdbarch methods.
* s390-tdep.c (s390_gdbarch_init): Don't call
set_gdbarch_displaced_step_location, set new gdbarch methods.
2020-12-04 Simon Marchi <simon.marchi@efficios.com>
* Makefile.in (COMMON_SFILES): Add displaced-stepping.c.
......
......@@ -1445,7 +1445,7 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
tdep->lowest_pc = 0x8000;
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, true);
set_solib_svr4_fetch_link_map_offsets (gdbarch,
svr4_lp64_fetch_link_map_offsets);
......@@ -1658,7 +1658,6 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
set_gdbarch_displaced_step_copy_insn (gdbarch,
aarch64_displaced_step_copy_insn);
set_gdbarch_displaced_step_fixup (gdbarch, aarch64_displaced_step_fixup);
set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
set_gdbarch_displaced_step_hw_singlestep (gdbarch,
aarch64_displaced_step_hw_singlestep);
......
......@@ -356,7 +356,7 @@ alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
struct gdbarch_tdep *tdep;
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, false);
/* Hook into the DWARF CFI frame unwinder. */
alpha_dwarf2_init_abi (info, gdbarch);
......
......@@ -1795,11 +1795,12 @@ amd64_dtrace_parse_probe_argument (struct gdbarch *gdbarch,
}
static void
amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch,
bool supports_displaced_step)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, supports_displaced_step);
tdep->sigtramp_p = amd64_linux_sigtramp_p;
tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
......@@ -1839,8 +1840,6 @@ amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
set_gdbarch_displaced_step_copy_insn (gdbarch,
amd64_displaced_step_copy_insn);
set_gdbarch_displaced_step_fixup (gdbarch, amd64_displaced_step_fixup);
set_gdbarch_displaced_step_location (gdbarch,
linux_displaced_step_location);
set_gdbarch_process_record (gdbarch, i386_process_record);
set_gdbarch_process_record_signal (gdbarch, amd64_linux_record_signal);
......@@ -1881,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
if (!valid_p)
return;
amd64_linux_init_abi_common (info, gdbarch);
amd64_linux_init_abi_common (info, gdbarch, true);
/* Initialize the amd64_linux_record_tdep. */
/* These values are the size of the type that will be used in a system
......@@ -2096,7 +2095,7 @@ amd64_x32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
if (!valid_p)
return;
amd64_linux_init_abi_common (info, gdbarch);
amd64_linux_init_abi_common (info, gdbarch, false);
/* Initialize the amd64_x32_linux_record_tdep. */
/* These values are the size of the type that will be used in a system
......
......@@ -439,7 +439,7 @@ arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
*/
tdep->jb_pc = 15;
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, false);
/* Set up target dependent GDB architecture entries. */
set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
......
......@@ -1721,7 +1721,7 @@ arm_linux_init_abi (struct gdbarch_info info,
NULL };
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, true);
tdep->lowest_pc = 0x8000;
if (info.byte_order_for_code == BFD_ENDIAN_BIG)
......@@ -1807,7 +1807,6 @@ arm_linux_init_abi (struct gdbarch_info info,
set_gdbarch_displaced_step_copy_insn (gdbarch,
arm_linux_displaced_step_copy_insn);
set_gdbarch_displaced_step_fixup (gdbarch, arm_displaced_step_fixup);
set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
/* Reversible debugging, process record. */
set_gdbarch_process_record (gdbarch, arm_process_record);
......
......@@ -433,9 +433,12 @@ arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
{
struct bound_minimal_symbol sym;
char type;
arm_displaced_step_copy_insn_closure *dsc
= ((arm_displaced_step_copy_insn_closure * )
get_displaced_step_copy_insn_closure_by_addr (memaddr));
arm_displaced_step_copy_insn_closure *dsc = nullptr;
if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
dsc = ((arm_displaced_step_copy_insn_closure * )
gdbarch_displaced_step_copy_insn_closure_by_addr
(gdbarch, current_inferior (), memaddr));
/* If checking the mode of displaced instruction in copy area, the mode
should be determined by instruction on the original address. */
......
......@@ -150,7 +150,7 @@ bfin_linux_get_syscall_number (struct gdbarch *gdbarch,
static void
bfin_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, false);
/* Set the sigtramp frame sniffer. */
tramp_frame_prepend_unwinder (gdbarch, &bfin_linux_sigframe);
......
......@@ -35,7 +35,7 @@ cris_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, false);
if (tdep->cris_version == 32)
/* Threaded debugging is only supported on CRISv32 for now. */
......
......@@ -233,7 +233,7 @@ csky_linux_rt_sigreturn_tramp_frame = {
static void
csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
linux_init_abi (info, gdbarch);
linux_init_abi (info, gdbarch, false);
/* Shared library handling. */
set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
......
......@@ -19,9 +19,15 @@
#include "defs.h"
#include "displaced-stepping.h"
#include "cli/cli-cmds.h"
#include "cli/cli-cmds.h"
#include "command.h"
#include "gdbarch.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "inferior.h"
#include "regcache.h"
#include "target/target.h"
/* Default destructor for displaced_step_copy_insn_closure. */
......@@ -37,6 +43,193 @@ show_debug_displaced (struct ui_file *file, int from_tty,
fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
}
displaced_step_prepare_status
displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
{
gdb_assert (!thread->displaced_step_state.in_progress ());
/* Is a thread currently using the buffer? */
if (m_current_thread != nullptr)
{
/* If so, it better not be this thread. */
gdb_assert (thread != m_current_thread);
return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
}
regcache *regcache = get_thread_regcache (thread);
const address_space *aspace = regcache->aspace ();
gdbarch *arch = regcache->arch ();
ULONGEST len = gdbarch_max_insn_length (arch);
if (breakpoint_in_range_p (aspace, m_addr, len))
{
/* There's a breakpoint set in the scratch pad location range
(which is usually around the entry point). We'd either
install it before resuming, which would overwrite/corrupt the
scratch pad, or if it was already inserted, this displaced
step would overwrite it. The latter is OK in the sense that
we already assume that no thread is going to execute the code
in the scratch pad range (after initial startup) anyway, but
the former is unacceptable. Simply punt and fallback to
stepping over this breakpoint in-line. */
displaced_debug_printf ("breakpoint set in scratch pad. "
"Stepping over breakpoint in-line instead.");
return DISPLACED_STEP_PREPARE_STATUS_CANT;
}
m_original_pc = regcache_read_pc (regcache);
displaced_pc = m_addr;
/* Save the original contents of the displaced stepping buffer. */
m_saved_copy.resize (len);
int status = target_read_memory (m_addr, m_saved_copy.data (), len);
if (status != 0)
throw_error (MEMORY_ERROR,
_("Error accessing memory address %s (%s) for "
"displaced-stepping scratch space."),
paddress (arch, m_addr), safe_strerror (status));
displaced_debug_printf ("saved %s: %s",
paddress (arch, m_addr),
displaced_step_dump_bytes
(m_saved_copy.data (), len).c_str ());
/* Save this in a local variable first, so it's released if code below
throws. */
displaced_step_copy_insn_closure_up copy_insn_closure
= gdbarch_displaced_step_copy_insn (arch, m_original_pc, m_addr, regcache);
if (copy_insn_closure == nullptr)
{
/* The architecture doesn't know how or want to displaced step
this instruction or instruction sequence. Fallback to
stepping over the breakpoint in-line. */
return DISPLACED_STEP_PREPARE_STATUS_CANT;
}
/* Resume execution at the copy. */
regcache_write_pc (regcache, m_addr);
/* This marks the buffer as being in use. */
m_current_thread = thread;
/* Save this, now that we know everything went fine. */
m_copy_insn_closure = std::move (copy_insn_closure);
/* Tell infrun not to try preparing a displaced step again for this inferior. */
thread->inf->displaced_step_state.unavailable = true;
return DISPLACED_STEP_PREPARE_STATUS_OK;
}
static void
write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
const gdb_byte *myaddr, int len)
{
scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
inferior_ptid = ptid;
write_memory (memaddr, myaddr, len);
}
static bool
displaced_step_instruction_executed_successfully (gdbarch *arch,
gdb_signal signal)
{
if (signal != GDB_SIGNAL_TRAP)
return false;
if (target_stopped_by_watchpoint ())
{
if (gdbarch_have_nonsteppable_watchpoint (arch)
|| target_have_steppable_watchpoint ())
return false;
}
return true;
}
displaced_step_finish_status
displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
gdb_signal sig)
{
gdb_assert (thread->displaced_step_state.in_progress ());
gdb_assert (thread == m_current_thread);
/* Move this to a local variable so it's released in case something goes
wrong. */
displaced_step_copy_insn_closure_up copy_insn_closure
= std::move (m_copy_insn_closure);
gdb_assert (copy_insn_closure != nullptr);
/* Reset M_CURRENT_THREAD immediately to mark the buffer as available, in case
something goes wrong below. */
m_current_thread = nullptr;
/* Now that a buffer gets freed, tell infrun it can ask us to prepare a displaced
step again for this inferior. Do that here in case something goes wrong
below. */
thread->inf->displaced_step_state.unavailable = false;
ULONGEST len = gdbarch_max_insn_length (arch);
write_memory_ptid (thread->ptid, m_addr,
m_saved_copy.data (), len);
displaced_debug_printf ("restored %s %s",
target_pid_to_str (thread->ptid).c_str (),
paddress (arch, m_addr));
regcache *rc = get_thread_regcache (thread);
bool instruction_executed_successfully
= displaced_step_instruction_executed_successfully (arch, sig);
if (instruction_executed_successfully)
{
gdbarch_displaced_step_fixup (arch, copy_insn_closure.get (), m_original_pc,
m_addr, rc);
return DISPLACED_STEP_FINISH_STATUS_OK;
}
else
{
/* Since the instruction didn't complete, all we can do is relocate the
PC. */
CORE_ADDR pc = regcache_read_pc (rc);
pc = m_original_pc + (pc - m_addr);
regcache_write_pc (rc, pc);
return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
}
}
const displaced_step_copy_insn_closure *
displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr)
{
if (addr == m_addr)
return m_copy_insn_closure.get ();
else
return nullptr;
}
void
displaced_step_buffer::restore_in_ptid (ptid_t ptid)
{
if (m_current_thread != nullptr)
{
regcache *regcache = get_thread_regcache (m_current_thread);
gdbarch *arch = regcache->arch ();
ULONGEST len = gdbarch_max_insn_length (arch);
write_memory_ptid (ptid, m_addr, m_saved_copy.data (), len);
displaced_debug_printf ("restored in ptid %s %s",
target_pid_to_str (ptid).c_str (),
paddress (arch, m_addr));
}
}
void _initialize_displaced_stepping ();
void
_initialize_displaced_stepping ()
......
......@@ -22,6 +22,7 @@
#include "gdbsupport/byte-vector.h"
struct gdbarch;
struct thread_info;
/* True if we are debugging displaced stepping. */
......@@ -62,7 +63,8 @@ enum displaced_step_finish_status
DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
};
/* Base class for displaced stepping closures (the arch-specific data). */
/* Data returned by a gdbarch displaced_step_copy_insn method, to be passed to
the matching displaced_step_fixup method. */
struct displaced_step_copy_insn_closure
{
......@@ -80,6 +82,9 @@ struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
: buf (buf_size)
{}
/* The content of this buffer is up to the user of the class, but typically
original instruction bytes, used during fixup to determine what needs to
be fixed up. */
gdb::byte_vector buf;
};
......@@ -95,37 +100,95 @@ struct displaced_step_inferior_state
/* Put this object back in its original state. */
void reset ()
{
failed_before = 0;
step_thread = nullptr;
step_gdbarch = nullptr;
step_closure.reset ();
step_original = 0;
step_copy = 0;
step_saved_copy.clear ();
failed_before = false;
in_progress_count = 0;
unavailable = false;
}
/* True if preparing a displaced step ever failed. If so, we won't
try displaced stepping for this inferior again. */
int failed_before;
bool failed_before;
/* If this is not nullptr, this is the thread carrying out a
displaced single-step in process PID. This thread's state will
require fixing up once it has completed its step. */
thread_info *step_thread;
/* Number of displaced steps in progress for this inferior. */
unsigned int in_progress_count;
/* The architecture the thread had when we stepped it. */
gdbarch *step_gdbarch;
/* If true, this tells GDB that it's not worth asking the gdbarch displaced
stepping implementation to prepare a displaced step, because it would
return UNAVAILABLE. This is set and reset by the gdbarch in the
displaced_step_prepare and displaced_step_finish methods. */
bool unavailable;
};
/* The closure provided gdbarch_displaced_step_copy_insn, to be used
for post-step cleanup. */
displaced_step_copy_insn_closure_up step_closure;
/* Per-thread displaced stepping state. */
/* The address of the original instruction, and the copy we
made. */
CORE_ADDR step_original, step_copy;
struct displaced_step_thread_state
{
/* Return true if this thread is currently executing a displaced step. */
bool in_progress () const
{
return m_original_gdbarch != nullptr;
}
/* Return the gdbarch of the thread prior to the step. */
gdbarch *get_original_gdbarch () const
{
return m_original_gdbarch;
}
/* Mark this thread as currently executing a displaced step.
ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
is executed). */
void set (gdbarch *original_gdbarch)
{
m_original_gdbarch = original_gdbarch;
}
/* Mark this thread as no longer executing a displaced step. */
void reset ()
{
m_original_gdbarch = nullptr;
}
private:
gdbarch *m_original_gdbarch = nullptr;
};
/* Manage access to a single displaced stepping buffer. */
struct displaced_step_buffer
{
explicit displaced_step_buffer (CORE_ADDR buffer_addr)
: m_addr (buffer_addr)
{}
displaced_step_prepare_status prepare (thread_info *thread,
CORE_ADDR &displaced_pc);
displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
gdb_signal sig);
const displaced_step_copy_insn_closure *
copy_insn_closure_by_addr (CORE_ADDR addr);
void restore_in_ptid (ptid_t ptid);
private:
/* Original PC of the instruction being displaced-stepped in this buffer. */
CORE_ADDR m_original_pc = 0;
/* Address of the buffer. */
const CORE_ADDR m_addr;
/* If set, the thread currently using the buffer. */
thread_info *m_current_thread = nullptr;