1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _LIBUNWINDSTACK_TESTS_REGS_FAKE_H 18 #define _LIBUNWINDSTACK_TESTS_REGS_FAKE_H 19 20 #include <stdint.h> 21 22 #include <unwindstack/Elf.h> 23 #include <unwindstack/Memory.h> 24 #include <unwindstack/Regs.h> 25 26 #include "Check.h" 27 28 namespace unwindstack { 29 30 class RegsFake : public Regs { 31 public: RegsFake(uint16_t total_regs)32 RegsFake(uint16_t total_regs) : Regs(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} 33 virtual ~RegsFake() = default; 34 Arch()35 ArchEnum Arch() override { return fake_arch_; } RawData()36 void* RawData() override { return nullptr; } pc()37 uint64_t pc() override { return fake_pc_; } sp()38 uint64_t sp() override { return fake_sp_; } set_pc(uint64_t pc)39 void set_pc(uint64_t pc) override { fake_pc_ = pc; } set_sp(uint64_t sp)40 void set_sp(uint64_t sp) override { fake_sp_ = sp; } 41 SetPcFromReturnAddress(Memory *)42 bool SetPcFromReturnAddress(Memory*) override { 43 if (!fake_return_address_valid_) { 44 return false; 45 } 46 fake_pc_ = fake_return_address_; 47 return true; 48 } 49 IterateRegisters(std::function<void (const char *,uint64_t)>)50 void IterateRegisters(std::function<void(const char*, uint64_t)>) override {} 51 Is32Bit()52 bool Is32Bit() { 53 CHECK(fake_arch_ != ARCH_UNKNOWN); 54 return fake_arch_ == ARCH_ARM || fake_arch_ == ARCH_X86 || fake_arch_ == ARCH_MIPS; 55 } 56 StepIfSignalHandler(uint64_t,Elf *,Memory *)57 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } 58 FakeSetArch(ArchEnum arch)59 void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; } FakeSetDexPc(uint64_t dex_pc)60 void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; } FakeSetReturnAddress(uint64_t return_address)61 void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; } FakeSetReturnAddressValid(bool valid)62 void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; } 63 Clone()64 Regs* Clone() override { return nullptr; } 65 66 private: 67 ArchEnum fake_arch_ = ARCH_UNKNOWN; 68 uint64_t fake_pc_ = 0; 69 uint64_t fake_sp_ = 0; 70 bool fake_return_address_valid_ = false; 71 uint64_t fake_return_address_ = 0; 72 }; 73 74 template <typename TypeParam> 75 class RegsImplFake : public RegsImpl<TypeParam> { 76 public: RegsImplFake(uint16_t total_regs)77 RegsImplFake(uint16_t total_regs) 78 : RegsImpl<TypeParam>(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} 79 virtual ~RegsImplFake() = default; 80 Arch()81 ArchEnum Arch() override { return ARCH_UNKNOWN; } pc()82 uint64_t pc() override { return fake_pc_; } sp()83 uint64_t sp() override { return fake_sp_; } set_pc(uint64_t pc)84 void set_pc(uint64_t pc) override { fake_pc_ = pc; } set_sp(uint64_t sp)85 void set_sp(uint64_t sp) override { fake_sp_ = sp; } set_pseudo_reg(uint64_t reg)86 void set_pseudo_reg(uint64_t reg) { fake_pseudo_reg_ = reg; } 87 SetPcFromReturnAddress(Memory *)88 bool SetPcFromReturnAddress(Memory*) override { return false; } StepIfSignalHandler(uint64_t,Elf *,Memory *)89 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } 90 SetPseudoRegister(uint16_t reg,uint64_t value)91 bool SetPseudoRegister(uint16_t reg, uint64_t value) override { 92 if (fake_pseudo_reg_ != reg) { 93 return false; 94 } 95 fake_pseudo_reg_value_ = value; 96 return true; 97 } GetPseudoRegister(uint16_t reg,uint64_t * value)98 bool GetPseudoRegister(uint16_t reg, uint64_t* value) override { 99 if (fake_pseudo_reg_ != reg) { 100 return false; 101 } 102 *value = fake_pseudo_reg_value_; 103 return true; 104 } 105 Clone()106 Regs* Clone() override { return nullptr; } 107 108 private: 109 uint64_t fake_pc_ = 0; 110 uint64_t fake_sp_ = 0; 111 uint16_t fake_pseudo_reg_ = 0; 112 uint64_t fake_pseudo_reg_value_ = 0; 113 }; 114 115 } // namespace unwindstack 116 117 #endif // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H 118