From 39fb3e362cf71d0542a5807c97039cd474ef3716 Mon Sep 17 00:00:00 2001 From: Hedges Date: Wed, 6 Jun 2018 05:20:47 +0100 Subject: GDB Stub Improvements (#508) * GDB Stub should work now. * Applied clang-format. * Replaced htonll with swap64. * Tidy up. --- src/core/arm/unicorn/arm_unicorn.cpp | 38 +++++++++++++++++++++++++++++++++++- src/core/arm/unicorn/arm_unicorn.h | 4 ++++ 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'src/core/arm/unicorn') diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp index c0cc62f03..ce6c5616d 100644 --- a/src/core/arm/unicorn/arm_unicorn.cpp +++ b/src/core/arm/unicorn/arm_unicorn.cpp @@ -35,6 +35,17 @@ LoadDll LoadDll::g_load_dll; } \ } while (0) +static void CodeHook(uc_engine* uc, uint64_t address, uint32_t size, void* user_data) { + GDBStub::BreakpointAddress bkpt = + GDBStub::GetNextBreakpointFromAddress(address, GDBStub::BreakpointType::Execute); + if (GDBStub::IsMemoryBreak() || + (bkpt.type != GDBStub::BreakpointType::None && address == bkpt.address)) { + auto core = static_cast(user_data); + core->RecordBreak(bkpt); + uc_emu_stop(uc); + } +} + static void InterruptHook(uc_engine* uc, u32 intNo, void* user_data) { u32 esr{}; CHECKED(uc_reg_read(uc, UC_ARM64_REG_ESR, &esr)); @@ -67,6 +78,10 @@ ARM_Unicorn::ARM_Unicorn() { uc_hook hook{}; CHECKED(uc_hook_add(uc, &hook, UC_HOOK_INTR, (void*)InterruptHook, this, 0, -1)); CHECKED(uc_hook_add(uc, &hook, UC_HOOK_MEM_INVALID, (void*)UnmappedMemoryHook, this, 0, -1)); + if (GDBStub::IsServerEnabled()) { + CHECKED(uc_hook_add(uc, &hook, UC_HOOK_CODE, (void*)CodeHook, this, 0, -1)); + last_bkpt_hit = false; + } } ARM_Unicorn::~ARM_Unicorn() { @@ -155,7 +170,11 @@ void ARM_Unicorn::SetTlsAddress(VAddr base) { } void ARM_Unicorn::Run() { - ExecuteInstructions(std::max(CoreTiming::GetDowncount(), 0)); + if (GDBStub::IsServerEnabled()) { + ExecuteInstructions(std::max(4000000, 0)); + } else { + ExecuteInstructions(std::max(CoreTiming::GetDowncount(), 0)); + } } void ARM_Unicorn::Step() { @@ -168,6 +187,18 @@ void ARM_Unicorn::ExecuteInstructions(int num_instructions) { MICROPROFILE_SCOPE(ARM_Jit); CHECKED(uc_emu_start(uc, GetPC(), 1ULL << 63, 0, num_instructions)); CoreTiming::AddTicks(num_instructions); + if (GDBStub::IsServerEnabled()) { + if (last_bkpt_hit) { + uc_reg_write(uc, UC_ARM64_REG_PC, &last_bkpt.address); + } + Kernel::Thread* thread = Kernel::GetCurrentThread(); + SaveContext(thread->context); + if (last_bkpt_hit) { + last_bkpt_hit = false; + GDBStub::Break(); + } + GDBStub::SendTrap(thread, 5); + } } void ARM_Unicorn::SaveContext(ARM_Interface::ThreadContext& ctx) { @@ -233,3 +264,8 @@ void ARM_Unicorn::PrepareReschedule() { } void ARM_Unicorn::ClearInstructionCache() {} + +void ARM_Unicorn::RecordBreak(GDBStub::BreakpointAddress bkpt) { + last_bkpt = bkpt; + last_bkpt_hit = true; +} diff --git a/src/core/arm/unicorn/arm_unicorn.h b/src/core/arm/unicorn/arm_unicorn.h index b99b58e4c..a482a2aa3 100644 --- a/src/core/arm/unicorn/arm_unicorn.h +++ b/src/core/arm/unicorn/arm_unicorn.h @@ -7,6 +7,7 @@ #include #include "common/common_types.h" #include "core/arm/arm_interface.h" +#include "core/gdbstub/gdbstub.h" class ARM_Unicorn final : public ARM_Interface { public: @@ -35,7 +36,10 @@ public: void Step() override; void ClearInstructionCache() override; void PageTableChanged() override{}; + void RecordBreak(GDBStub::BreakpointAddress bkpt); private: uc_engine* uc{}; + GDBStub::BreakpointAddress last_bkpt{}; + bool last_bkpt_hit; }; -- cgit v1.2.3