blob: ab23194e513161015e2b5d1cc106d78b3e1c73d2 [file] [log] [blame]
Christopher Ferris53a3c9b2017-05-10 18:34:15 -07001/*
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 Ferrisd06001d2017-11-30 18:56:01 -080022#include <unwindstack/Elf.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/Memory.h>
24#include <unwindstack/Regs.h>
25
26namespace unwindstack {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070027
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070028class RegsFake : public Regs {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070029 public:
30 RegsFake(uint16_t total_regs, uint16_t sp_reg)
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070031 : Regs(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070032 virtual ~RegsFake() = default;
33
Christopher Ferrisd06001d2017-11-30 18:56:01 -080034 ArchEnum Arch() override { return fake_arch_; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070035 void* RawData() override { return nullptr; }
36 uint64_t pc() override { return fake_pc_; }
37 uint64_t sp() override { return fake_sp_; }
38 bool SetPcFromReturnAddress(Memory*) override {
39 if (!fake_return_address_valid_) {
40 return false;
41 }
42 fake_pc_ = fake_return_address_;
43 return true;
44 }
45
Josh Gao6f580d82017-09-22 12:57:15 -070046 void IterateRegisters(std::function<void(const char*, uint64_t)>) override {}
47
Christopher Ferris150db122017-12-20 18:49:01 -080048 bool Is32Bit() { return false; }
Christopher Ferris02fdb562017-12-08 15:04:49 -080049
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080050 uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 2; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070051
52 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
53
54 void SetFromRaw() override {}
55
Christopher Ferrisd06001d2017-11-30 18:56:01 -080056 void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070057 void FakeSetPc(uint64_t pc) { fake_pc_ = pc; }
58 void FakeSetSp(uint64_t sp) { fake_sp_ = sp; }
Christopher Ferrise762f1f2018-02-06 14:51:48 -080059 void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070060 void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; }
61 void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; }
62
63 private:
Christopher Ferrisd06001d2017-11-30 18:56:01 -080064 ArchEnum fake_arch_ = ARCH_UNKNOWN;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070065 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
71template <typename TypeParam>
72class RegsImplFake : public RegsImpl<TypeParam> {
73 public:
74 RegsImplFake(uint16_t total_regs, uint16_t sp_reg)
75 : RegsImpl<TypeParam>(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
76 virtual ~RegsImplFake() = default;
77
Christopher Ferrisd06001d2017-11-30 18:56:01 -080078 ArchEnum Arch() override { return ARCH_UNKNOWN; }
Josh Gao0953ecd2017-08-25 13:55:06 -070079
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -080080 uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 0; }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070081 void SetFromRaw() override {}
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070082 bool SetPcFromReturnAddress(Memory*) override { return false; }
Christopher Ferriseb4a6db2017-07-19 12:37:45 -070083 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070084
85 void FakeSetPc(uint64_t pc) { this->pc_ = pc; }
86 void FakeSetSp(uint64_t sp) { this->sp_ = sp; }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070087};
88
Christopher Ferrisd226a512017-07-14 10:37:19 -070089} // namespace unwindstack
90
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070091#endif // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H