| 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 | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 22 | #include <unwindstack/Elf.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 23 | #include <unwindstack/Memory.h> | 
|  | 24 | #include <unwindstack/Regs.h> | 
|  | 25 |  | 
|  | 26 | namespace unwindstack { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 27 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 28 | class RegsFake : public Regs { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 29 | public: | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 30 | RegsFake(uint16_t total_regs) : Regs(total_regs, 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 | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 33 | ArchEnum Arch() override { return fake_arch_; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 34 | void* RawData() override { return nullptr; } | 
|  | 35 | uint64_t pc() override { return fake_pc_; } | 
|  | 36 | uint64_t sp() override { return fake_sp_; } | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 37 | void set_pc(uint64_t pc) override { fake_pc_ = pc; } | 
|  | 38 | void set_sp(uint64_t sp) override { fake_sp_ = sp; } | 
|  | 39 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 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 |  | 
| Josh Gao | 6f580d8 | 2017-09-22 12:57:15 -0700 | [diff] [blame] | 48 | void IterateRegisters(std::function<void(const char*, uint64_t)>) override {} | 
|  | 49 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 50 | bool Is32Bit() { return false; } | 
| Christopher Ferris | 02fdb56 | 2017-12-08 15:04:49 -0800 | [diff] [blame] | 51 |  | 
| Christopher Ferris | a2ec50b | 2018-02-21 15:39:07 -0800 | [diff] [blame] | 52 | uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 2; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 53 |  | 
|  | 54 | bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } | 
|  | 55 |  | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 56 | void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; } | 
| Christopher Ferris | e762f1f | 2018-02-06 14:51:48 -0800 | [diff] [blame] | 57 | void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 58 | void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; } | 
|  | 59 | void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; } | 
|  | 60 |  | 
|  | 61 | private: | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 62 | ArchEnum fake_arch_ = ARCH_UNKNOWN; | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 63 | uint64_t fake_pc_ = 0; | 
|  | 64 | uint64_t fake_sp_ = 0; | 
|  | 65 | bool fake_return_address_valid_ = false; | 
|  | 66 | uint64_t fake_return_address_ = 0; | 
|  | 67 | }; | 
|  | 68 |  | 
|  | 69 | template <typename TypeParam> | 
|  | 70 | class RegsImplFake : public RegsImpl<TypeParam> { | 
|  | 71 | public: | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 72 | RegsImplFake(uint16_t total_regs) | 
|  | 73 | : RegsImpl<TypeParam>(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {} | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 74 | virtual ~RegsImplFake() = default; | 
|  | 75 |  | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 76 | ArchEnum Arch() override { return ARCH_UNKNOWN; } | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 77 | uint64_t pc() override { return fake_pc_; } | 
|  | 78 | uint64_t sp() override { return fake_sp_; } | 
|  | 79 | void set_pc(uint64_t pc) override { fake_pc_ = pc; } | 
|  | 80 | void set_sp(uint64_t sp) override { fake_sp_ = sp; } | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 81 |  | 
| Christopher Ferris | a2ec50b | 2018-02-21 15:39:07 -0800 | [diff] [blame] | 82 | uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 0; } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 83 | bool SetPcFromReturnAddress(Memory*) override { return false; } | 
| Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 84 | bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 85 |  | 
| Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 86 | private: | 
|  | 87 | uint64_t fake_pc_ = 0; | 
|  | 88 | uint64_t fake_sp_ = 0; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 89 | }; | 
|  | 90 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 91 | }  // namespace unwindstack | 
|  | 92 |  | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 93 | #endif  // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H |