blob: 75fc9d02dd860dfa475639c33e757f79ba9fa60b [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
Christopher Ferris78133452019-03-14 13:44:38 -070026#include "Check.h"
27
Christopher Ferrisd226a512017-07-14 10:37:19 -070028namespace unwindstack {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070029
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070030class RegsFake : public Regs {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070031 public:
Yabin Cui11e96fe2018-03-14 18:16:22 -070032 RegsFake(uint16_t total_regs) : Regs(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070033 virtual ~RegsFake() = default;
34
Christopher Ferrisd06001d2017-11-30 18:56:01 -080035 ArchEnum Arch() override { return fake_arch_; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070036 void* RawData() override { return nullptr; }
37 uint64_t pc() override { return fake_pc_; }
38 uint64_t sp() override { return fake_sp_; }
Yabin Cui11e96fe2018-03-14 18:16:22 -070039 void set_pc(uint64_t pc) override { fake_pc_ = pc; }
40 void set_sp(uint64_t sp) override { fake_sp_ = sp; }
41
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070042 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
Josh Gao6f580d82017-09-22 12:57:15 -070050 void IterateRegisters(std::function<void(const char*, uint64_t)>) override {}
51
Christopher Ferris78133452019-03-14 13:44:38 -070052 bool Is32Bit() {
53 CHECK(fake_arch_ != ARCH_UNKNOWN);
54 return fake_arch_ == ARCH_ARM || fake_arch_ == ARCH_X86 || fake_arch_ == ARCH_MIPS;
55 }
Christopher Ferris02fdb562017-12-08 15:04:49 -080056
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070057 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
58
Christopher Ferrisd06001d2017-11-30 18:56:01 -080059 void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; }
Christopher Ferrise762f1f2018-02-06 14:51:48 -080060 void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070061 void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; }
62 void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; }
63
Josh Gao2f37a152018-04-20 11:51:14 -070064 Regs* Clone() override { return nullptr; }
65
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070066 private:
Christopher Ferrisd06001d2017-11-30 18:56:01 -080067 ArchEnum fake_arch_ = ARCH_UNKNOWN;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070068 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
74template <typename TypeParam>
75class RegsImplFake : public RegsImpl<TypeParam> {
76 public:
Yabin Cui11e96fe2018-03-14 18:16:22 -070077 RegsImplFake(uint16_t total_regs)
78 : RegsImpl<TypeParam>(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070079 virtual ~RegsImplFake() = default;
80
Christopher Ferrisd06001d2017-11-30 18:56:01 -080081 ArchEnum Arch() override { return ARCH_UNKNOWN; }
Yabin Cui11e96fe2018-03-14 18:16:22 -070082 uint64_t pc() override { return fake_pc_; }
83 uint64_t sp() override { return fake_sp_; }
84 void set_pc(uint64_t pc) override { fake_pc_ = pc; }
85 void set_sp(uint64_t sp) override { fake_sp_ = sp; }
Josh Gao0953ecd2017-08-25 13:55:06 -070086
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070087 bool SetPcFromReturnAddress(Memory*) override { return false; }
Christopher Ferriseb4a6db2017-07-19 12:37:45 -070088 bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070089
Josh Gao2f37a152018-04-20 11:51:14 -070090 Regs* Clone() override { return nullptr; }
91
Yabin Cui11e96fe2018-03-14 18:16:22 -070092 private:
93 uint64_t fake_pc_ = 0;
94 uint64_t fake_sp_ = 0;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070095};
96
Christopher Ferrisd226a512017-07-14 10:37:19 -070097} // namespace unwindstack
98
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070099#endif // _LIBUNWINDSTACK_TESTS_REGS_FAKE_H