• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace unwindstack {
27 
28 class RegsFake : public Regs {
29  public:
RegsFake(uint16_t total_regs)30   RegsFake(uint16_t total_regs) : Regs(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
31   virtual ~RegsFake() = default;
32 
Arch()33   ArchEnum Arch() override { return fake_arch_; }
RawData()34   void* RawData() override { return nullptr; }
pc()35   uint64_t pc() override { return fake_pc_; }
sp()36   uint64_t sp() override { return fake_sp_; }
set_pc(uint64_t pc)37   void set_pc(uint64_t pc) override { fake_pc_ = pc; }
set_sp(uint64_t sp)38   void set_sp(uint64_t sp) override { fake_sp_ = sp; }
39 
SetPcFromReturnAddress(Memory *)40   bool SetPcFromReturnAddress(Memory*) override {
41     if (!fake_return_address_valid_) {
42       return false;
43     }
44     fake_pc_ = fake_return_address_;
45     return true;
46   }
47 
IterateRegisters(std::function<void (const char *,uint64_t)>)48   void IterateRegisters(std::function<void(const char*, uint64_t)>) override {}
49 
Is32Bit()50   bool Is32Bit() { return false; }
51 
GetPcAdjustment(uint64_t,Elf *)52   uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 2; }
53 
StepIfSignalHandler(uint64_t,Elf *,Memory *)54   bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
55 
FakeSetArch(ArchEnum arch)56   void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; }
FakeSetDexPc(uint64_t dex_pc)57   void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; }
FakeSetReturnAddress(uint64_t return_address)58   void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; }
FakeSetReturnAddressValid(bool valid)59   void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; }
60 
Clone()61   Regs* Clone() override { return nullptr; }
62 
63  private:
64   ArchEnum fake_arch_ = ARCH_UNKNOWN;
65   uint64_t fake_pc_ = 0;
66   uint64_t fake_sp_ = 0;
67   bool fake_return_address_valid_ = false;
68   uint64_t fake_return_address_ = 0;
69 };
70 
71 template <typename TypeParam>
72 class RegsImplFake : public RegsImpl<TypeParam> {
73  public:
RegsImplFake(uint16_t total_regs)74   RegsImplFake(uint16_t total_regs)
75       : RegsImpl<TypeParam>(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
76   virtual ~RegsImplFake() = default;
77 
Arch()78   ArchEnum Arch() override { return ARCH_UNKNOWN; }
pc()79   uint64_t pc() override { return fake_pc_; }
sp()80   uint64_t sp() override { return fake_sp_; }
set_pc(uint64_t pc)81   void set_pc(uint64_t pc) override { fake_pc_ = pc; }
set_sp(uint64_t sp)82   void set_sp(uint64_t sp) override { fake_sp_ = sp; }
83 
GetPcAdjustment(uint64_t,Elf *)84   uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 0; }
SetPcFromReturnAddress(Memory *)85   bool SetPcFromReturnAddress(Memory*) override { return false; }
StepIfSignalHandler(uint64_t,Elf *,Memory *)86   bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
87 
Clone()88   Regs* Clone() override { return nullptr; }
89 
90  private:
91   uint64_t fake_pc_ = 0;
92   uint64_t fake_sp_ = 0;
93 };
94 
95 }  // namespace unwindstack
96 
97 #endif  // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H
98