libunwindstack: add Regs::Clone.

Add a way to copy a Regs object.

Bug: http://b/77296294
Test: libunwindstack_test
Change-Id: I28aff510aa3e1b677d5ae46dc3bfe7652817ce52
This commit is contained in:
Josh Gao 2018-04-20 11:51:14 -07:00
parent 22dc27b9fa
commit 2f37a15472
15 changed files with 77 additions and 0 deletions

View file

@ -197,4 +197,8 @@ bool RegsArm::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_mem
return true;
}
Regs* RegsArm::Clone() {
return new RegsArm(*this);
}
} // namespace unwindstack

View file

@ -148,4 +148,8 @@ bool RegsArm64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_m
return true;
}
Regs* RegsArm64::Clone() {
return new RegsArm64(*this);
}
} // namespace unwindstack

View file

@ -173,4 +173,8 @@ bool RegsMips::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_me
return true;
}
Regs* RegsMips::Clone() {
return new RegsMips(*this);
}
} // namespace unwindstack

View file

@ -160,4 +160,8 @@ bool RegsMips64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_
return true;
}
Regs* RegsMips64::Clone() {
return new RegsMips64(*this);
}
} // namespace unwindstack

View file

@ -179,4 +179,8 @@ bool RegsX86::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_mem
return false;
}
Regs* RegsX86::Clone() {
return new RegsX86(*this);
}
} // namespace unwindstack

View file

@ -168,4 +168,8 @@ bool RegsX86_64::StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_
return true;
}
Regs* RegsX86_64::Clone() {
return new RegsX86_64(*this);
}
} // namespace unwindstack

View file

@ -73,6 +73,8 @@ class Regs {
uint16_t total_regs() { return total_regs_; }
virtual Regs* Clone() = 0;
static ArchEnum CurrentArch();
static Regs* RemoteGet(pid_t pid);
static Regs* CreateFromUcontext(ArchEnum arch, void* ucontext);

View file

@ -50,6 +50,8 @@ class RegsArm : public RegsImpl<uint32_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -50,6 +50,8 @@ class RegsArm64 : public RegsImpl<uint64_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -50,6 +50,8 @@ class RegsMips : public RegsImpl<uint32_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -50,6 +50,8 @@ class RegsMips64 : public RegsImpl<uint64_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -53,6 +53,8 @@ class RegsX86 : public RegsImpl<uint32_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -53,6 +53,8 @@ class RegsX86_64 : public RegsImpl<uint64_t> {
void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Regs* Clone() override final;
static Regs* Read(void* data);
static Regs* CreateFromUcontext(void* ucontext);

View file

@ -58,6 +58,8 @@ class RegsFake : public Regs {
void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; }
void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; }
Regs* Clone() override { return nullptr; }
private:
ArchEnum fake_arch_ = ARCH_UNKNOWN;
uint64_t fake_pc_ = 0;
@ -83,6 +85,8 @@ class RegsImplFake : public RegsImpl<TypeParam> {
bool SetPcFromReturnAddress(Memory*) override { return false; }
bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
Regs* Clone() override { return nullptr; }
private:
uint64_t fake_pc_ = 0;
uint64_t fake_sp_ = 0;

View file

@ -286,4 +286,39 @@ TEST_F(RegsTest, machine_type) {
EXPECT_EQ(ARCH_MIPS64, mips64_regs.Arch());
}
template <typename RegisterType>
void clone_test(Regs* regs) {
RegisterType* register_values = reinterpret_cast<RegisterType*>(regs->RawData());
int num_regs = regs->total_regs();
for (int i = 0; i < num_regs; ++i) {
register_values[i] = i;
}
std::unique_ptr<Regs> clone(regs->Clone());
ASSERT_EQ(regs->total_regs(), clone->total_regs());
RegisterType* clone_values = reinterpret_cast<RegisterType*>(clone->RawData());
for (int i = 0; i < num_regs; ++i) {
EXPECT_EQ(register_values[i], clone_values[i]);
EXPECT_NE(&register_values[i], &clone_values[i]);
}
}
TEST_F(RegsTest, clone) {
std::vector<std::unique_ptr<Regs>> regs;
regs.emplace_back(new RegsArm());
regs.emplace_back(new RegsArm64());
regs.emplace_back(new RegsX86());
regs.emplace_back(new RegsX86_64());
regs.emplace_back(new RegsMips());
regs.emplace_back(new RegsMips64());
for (auto& r : regs) {
if (r->Is32Bit()) {
clone_test<uint32_t>(r.get());
} else {
clone_test<uint64_t>(r.get());
}
}
}
} // namespace unwindstack