diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp index e79bca314..3b2f38e08 100644 --- a/libbacktrace/UnwindStack.cpp +++ b/libbacktrace/UnwindStack.cpp @@ -160,7 +160,8 @@ bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* // one extra function call appearing in the unwind. unwindstack::RegsGetLocal(regs.get()); } else { - regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), ucontext)); + regs.reset( + unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), ucontext)); } error_ = BACKTRACE_UNWIND_NO_ERROR; @@ -177,10 +178,10 @@ std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offse bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) { std::unique_ptr regs; if (context == nullptr) { - uint32_t machine_type; - regs.reset(unwindstack::Regs::RemoteGet(Tid(), &machine_type)); + regs.reset(unwindstack::Regs::RemoteGet(Tid())); } else { - regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), context)); + regs.reset( + unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), context)); } error_ = BACKTRACE_UNWIND_NO_ERROR; diff --git a/libunwindstack/Regs.cpp b/libunwindstack/Regs.cpp index dea7b87ce..4d09c1b0c 100644 --- a/libunwindstack/Regs.cpp +++ b/libunwindstack/Regs.cpp @@ -56,6 +56,10 @@ bool RegsImpl::GetReturnAddressFromDefault(Memory* memory, uint64_t RegsArm::RegsArm() : RegsImpl(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {} +uint32_t RegsArm::MachineType() { + return EM_ARM; +} + uint64_t RegsArm::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { if (!elf->valid()) { return rel_pc; @@ -90,6 +94,10 @@ void RegsArm::SetFromRaw() { RegsArm64::RegsArm64() : RegsImpl(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {} +uint32_t RegsArm64::MachineType() { + return EM_AARCH64; +} + uint64_t RegsArm64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { if (!elf->valid()) { return rel_pc; @@ -109,6 +117,10 @@ void RegsArm64::SetFromRaw() { RegsX86::RegsX86() : RegsImpl(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {} +uint32_t RegsX86::MachineType() { + return EM_386; +} + uint64_t RegsX86::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { if (!elf->valid()) { return rel_pc; @@ -128,6 +140,10 @@ void RegsX86::SetFromRaw() { RegsX86_64::RegsX86_64() : RegsImpl(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {} +uint32_t RegsX86_64::MachineType() { + return EM_X86_64; +} + uint64_t RegsX86_64::GetAdjustedPc(uint64_t rel_pc, Elf* elf) { if (!elf->valid()) { return rel_pc; @@ -212,7 +228,7 @@ static Regs* ReadX86_64(void* remote_data) { // This function assumes that reg_data is already aligned to a 64 bit value. // If not this could crash with an unaligned access. -Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) { +Regs* Regs::RemoteGet(pid_t pid) { // Make the buffer large enough to contain the largest registers type. std::vector buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t)); struct iovec io; @@ -225,16 +241,12 @@ Regs* Regs::RemoteGet(pid_t pid, uint32_t* machine_type) { switch (io.iov_len) { case sizeof(x86_user_regs): - *machine_type = EM_386; return ReadX86(buffer.data()); case sizeof(x86_64_user_regs): - *machine_type = EM_X86_64; return ReadX86_64(buffer.data()); case sizeof(arm_user_regs): - *machine_type = EM_ARM; return ReadArm(buffer.data()); case sizeof(arm64_user_regs): - *machine_type = EM_AARCH64; return ReadArm64(buffer.data()); } return nullptr; @@ -320,7 +332,7 @@ Regs* Regs::CreateFromUcontext(uint32_t machine_type, void* ucontext) { return nullptr; } -uint32_t Regs::GetMachineType() { +uint32_t Regs::CurrentMachineType() { #if defined(__arm__) return EM_ARM; #elif defined(__aarch64__) diff --git a/libunwindstack/include/unwindstack/Regs.h b/libunwindstack/include/unwindstack/Regs.h index 78e2c0dc0..ed4d38ae6 100644 --- a/libunwindstack/include/unwindstack/Regs.h +++ b/libunwindstack/include/unwindstack/Regs.h @@ -49,6 +49,8 @@ class Regs { : total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {} virtual ~Regs() = default; + virtual uint32_t MachineType() = 0; + virtual void* RawData() = 0; virtual uint64_t pc() = 0; virtual uint64_t sp() = 0; @@ -64,8 +66,8 @@ class Regs { uint16_t sp_reg() { return sp_reg_; } uint16_t total_regs() { return total_regs_; } - static uint32_t GetMachineType(); - static Regs* RemoteGet(pid_t pid, uint32_t* machine_type); + static uint32_t CurrentMachineType(); + static Regs* RemoteGet(pid_t pid); static Regs* CreateFromUcontext(uint32_t machine_type, void* ucontext); static Regs* CreateFromLocal(); @@ -105,6 +107,8 @@ class RegsArm : public RegsImpl { RegsArm(); virtual ~RegsArm() = default; + virtual uint32_t MachineType() override final; + uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; void SetFromRaw() override; @@ -117,6 +121,8 @@ class RegsArm64 : public RegsImpl { RegsArm64(); virtual ~RegsArm64() = default; + virtual uint32_t MachineType() override final; + uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; void SetFromRaw() override; @@ -129,6 +135,8 @@ class RegsX86 : public RegsImpl { RegsX86(); virtual ~RegsX86() = default; + virtual uint32_t MachineType() override final; + uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; void SetFromRaw() override; @@ -143,6 +151,8 @@ class RegsX86_64 : public RegsImpl { RegsX86_64(); virtual ~RegsX86_64() = default; + virtual uint32_t MachineType() override final; + uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; void SetFromRaw() override; diff --git a/libunwindstack/tests/RegsFake.h b/libunwindstack/tests/RegsFake.h index 6669d7dba..c76ecaaea 100644 --- a/libunwindstack/tests/RegsFake.h +++ b/libunwindstack/tests/RegsFake.h @@ -31,6 +31,8 @@ class RegsFake : public RegsImpl { : RegsImpl(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} virtual ~RegsFake() = default; + uint32_t MachineType() override { return 0; } + uint64_t GetAdjustedPc(uint64_t, Elf*) override { return 0; } void SetFromRaw() override {} bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } diff --git a/libunwindstack/tests/RegsTest.cpp b/libunwindstack/tests/RegsTest.cpp index e6de56aa3..f549a50d6 100644 --- a/libunwindstack/tests/RegsTest.cpp +++ b/libunwindstack/tests/RegsTest.cpp @@ -58,6 +58,8 @@ class RegsTestImpl : public RegsImpl { : RegsImpl(total_regs, regs_sp, return_loc) {} virtual ~RegsTestImpl() = default; + uint32_t MachineType() override { return 0; } + uint64_t GetAdjustedPc(uint64_t, Elf*) override { return 0; } void SetFromRaw() override {} bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp index 3c69e2a49..2fc3a3880 100644 --- a/libunwindstack/tests/UnwindTest.cpp +++ b/libunwindstack/tests/UnwindTest.cpp @@ -206,8 +206,7 @@ TEST(UnwindTest, remote) { RemoteMaps maps(pid); ASSERT_TRUE(maps.Parse()); MemoryRemote memory(pid); - uint32_t machine_type; - std::unique_ptr regs(Regs::RemoteGet(pid, &machine_type)); + std::unique_ptr regs(Regs::RemoteGet(pid)); ASSERT_TRUE(regs.get() != nullptr); VerifyUnwind(pid, &memory, &maps, regs.get(), kFunctionOrder); @@ -254,7 +253,7 @@ TEST(UnwindTest, from_context) { LocalMaps maps; ASSERT_TRUE(maps.Parse()); - std::unique_ptr regs(Regs::CreateFromUcontext(Regs::GetMachineType(), ucontext)); + std::unique_ptr regs(Regs::CreateFromUcontext(Regs::CurrentMachineType(), ucontext)); MemoryLocal memory; VerifyUnwind(tid.load(), &memory, &maps, regs.get(), kFunctionOrder); @@ -293,8 +292,7 @@ static void RemoteThroughSignal(unsigned int sa_flags) { RemoteMaps maps(pid); ASSERT_TRUE(maps.Parse()); MemoryRemote memory(pid); - uint32_t machine_type; - std::unique_ptr regs(Regs::RemoteGet(pid, &machine_type)); + std::unique_ptr regs(Regs::RemoteGet(pid)); ASSERT_TRUE(regs.get() != nullptr); VerifyUnwind(pid, &memory, &maps, regs.get(), kFunctionSignalOrder); diff --git a/libunwindstack/tools/unwind.cpp b/libunwindstack/tools/unwind.cpp index 642105ada..c1077f8b6 100644 --- a/libunwindstack/tools/unwind.cpp +++ b/libunwindstack/tools/unwind.cpp @@ -62,8 +62,7 @@ void DoUnwind(pid_t pid) { return; } - uint32_t machine_type; - unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid, &machine_type); + unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid); if (regs == nullptr) { printf("Unable to get remote reg data\n"); return; @@ -71,7 +70,7 @@ void DoUnwind(pid_t pid) { bool bits32 = true; printf("ABI: "); - switch (machine_type) { + switch (regs->MachineType()) { case EM_ARM: printf("arm"); break;