Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 22 | #include <unwindstack/Memory.h> |
| 23 | #include <unwindstack/Regs.h> |
| 24 | |
| 25 | namespace unwindstack { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 26 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 27 | class RegsFake : public Regs { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 28 | public: |
| 29 | RegsFake(uint16_t total_regs, uint16_t sp_reg) |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 30 | : Regs(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 31 | virtual ~RegsFake() = default; |
| 32 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 33 | uint32_t MachineType() override { return fake_type_; } |
| 34 | void* RawData() override { return nullptr; } |
| 35 | uint64_t pc() override { return fake_pc_; } |
| 36 | uint64_t sp() override { return fake_sp_; } |
| 37 | bool SetPcFromReturnAddress(Memory*) override { |
| 38 | if (!fake_return_address_valid_) { |
| 39 | return false; |
| 40 | } |
| 41 | fake_pc_ = fake_return_address_; |
| 42 | return true; |
| 43 | } |
| 44 | |
Josh Gao | 6f580d8 | 2017-09-22 12:57:15 -0700 | [diff] [blame] | 45 | void IterateRegisters(std::function<void(const char*, uint64_t)>) override {} |
| 46 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 47 | uint64_t GetAdjustedPc(uint64_t rel_pc, Elf*) override { return rel_pc - 2; } |
| 48 | |
| 49 | bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } |
| 50 | |
| 51 | void SetFromRaw() override {} |
| 52 | |
| 53 | void FakeSetMachineType(uint32_t type) { fake_type_ = type; } |
| 54 | void FakeSetPc(uint64_t pc) { fake_pc_ = pc; } |
| 55 | void FakeSetSp(uint64_t sp) { fake_sp_ = sp; } |
| 56 | void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; } |
| 57 | void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; } |
| 58 | |
| 59 | private: |
| 60 | uint32_t fake_type_ = 0; |
| 61 | uint64_t fake_pc_ = 0; |
| 62 | uint64_t fake_sp_ = 0; |
| 63 | bool fake_return_address_valid_ = false; |
| 64 | uint64_t fake_return_address_ = 0; |
| 65 | }; |
| 66 | |
| 67 | template <typename TypeParam> |
| 68 | class RegsImplFake : public RegsImpl<TypeParam> { |
| 69 | public: |
| 70 | RegsImplFake(uint16_t total_regs, uint16_t sp_reg) |
| 71 | : RegsImpl<TypeParam>(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} |
| 72 | virtual ~RegsImplFake() = default; |
| 73 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 74 | uint32_t MachineType() override { return 0; } |
| 75 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 76 | uint64_t GetAdjustedPc(uint64_t, Elf*) override { return 0; } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 77 | void SetFromRaw() override {} |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 78 | bool SetPcFromReturnAddress(Memory*) override { return false; } |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 79 | bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 80 | |
| 81 | void FakeSetPc(uint64_t pc) { this->pc_ = pc; } |
| 82 | void FakeSetSp(uint64_t sp) { this->sp_ = sp; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 85 | } // namespace unwindstack |
| 86 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 87 | #endif // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H |