blob: 718fc858133427e6af459b37db9273196d7d70c2 [file] [log] [blame]
Christopher Ferris723cf9b2017-01-19 20:08:48 -08001/*
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_REGS_H
18#define _LIBUNWINDSTACK_REGS_H
19
20#include <stdint.h>
21
22#include <vector>
23
Christopher Ferris3958f802017-02-01 15:44:40 -080024// Forward declarations.
25class Elf;
26struct MapInfo;
27
Christopher Ferris723cf9b2017-01-19 20:08:48 -080028class Regs {
29 public:
Christopher Ferris3958f802017-02-01 15:44:40 -080030 enum LocationEnum : uint8_t {
31 LOCATION_UNKNOWN = 0,
32 LOCATION_REGISTER,
33 LOCATION_SP_OFFSET,
34 };
35
36 struct Location {
37 Location(LocationEnum type, int16_t value) : type(type), value(value) {}
38
39 LocationEnum type;
40 int16_t value;
41 };
42
43 Regs(uint16_t total_regs, uint16_t sp_reg, const Location& return_loc)
44 : total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {}
Christopher Ferris723cf9b2017-01-19 20:08:48 -080045 virtual ~Regs() = default;
46
Christopher Ferris3958f802017-02-01 15:44:40 -080047 virtual void* RawData() = 0;
Christopher Ferris723cf9b2017-01-19 20:08:48 -080048 virtual uint64_t pc() = 0;
49 virtual uint64_t sp() = 0;
50
Christopher Ferris3958f802017-02-01 15:44:40 -080051 virtual bool GetReturnAddressFromDefault(Memory* memory, uint64_t* value) = 0;
52
53 virtual uint64_t GetRelPc(Elf* elf, const MapInfo* map_info) = 0;
54
55 virtual uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) = 0;
56
57 uint16_t sp_reg() { return sp_reg_; }
58 uint16_t total_regs() { return total_regs_; }
59
60 static Regs* RemoteGet(pid_t pid, uint32_t* machine_type);
61
Christopher Ferris723cf9b2017-01-19 20:08:48 -080062 protected:
Christopher Ferris723cf9b2017-01-19 20:08:48 -080063 uint16_t total_regs_;
Christopher Ferris3958f802017-02-01 15:44:40 -080064 uint16_t sp_reg_;
65 Location return_loc_;
Christopher Ferris723cf9b2017-01-19 20:08:48 -080066};
67
68template <typename AddressType>
69class RegsTmpl : public Regs {
70 public:
Christopher Ferris3958f802017-02-01 15:44:40 -080071 RegsTmpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc)
72 : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {}
Christopher Ferris723cf9b2017-01-19 20:08:48 -080073 virtual ~RegsTmpl() = default;
74
Christopher Ferris3958f802017-02-01 15:44:40 -080075 uint64_t GetRelPc(Elf* elf, const MapInfo* map_info) override;
76
77 bool GetReturnAddressFromDefault(Memory* memory, uint64_t* value) override;
78
79 uint64_t pc() override { return pc_; }
80 uint64_t sp() override { return sp_; }
81
82 void set_pc(AddressType pc) { pc_ = pc; }
83 void set_sp(AddressType sp) { sp_ = sp; }
Christopher Ferris723cf9b2017-01-19 20:08:48 -080084
85 inline AddressType& operator[](size_t reg) { return regs_[reg]; }
86
Christopher Ferris3958f802017-02-01 15:44:40 -080087 void* RawData() override { return regs_.data(); }
Christopher Ferris723cf9b2017-01-19 20:08:48 -080088
Christopher Ferris3958f802017-02-01 15:44:40 -080089 protected:
90 AddressType pc_;
91 AddressType sp_;
Christopher Ferris723cf9b2017-01-19 20:08:48 -080092 std::vector<AddressType> regs_;
93};
94
Christopher Ferris3958f802017-02-01 15:44:40 -080095class RegsArm : public RegsTmpl<uint32_t> {
Christopher Ferris723cf9b2017-01-19 20:08:48 -080096 public:
Christopher Ferris3958f802017-02-01 15:44:40 -080097 RegsArm();
98 virtual ~RegsArm() = default;
99
100 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800101};
102
Christopher Ferris3958f802017-02-01 15:44:40 -0800103class RegsArm64 : public RegsTmpl<uint64_t> {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800104 public:
Christopher Ferris3958f802017-02-01 15:44:40 -0800105 RegsArm64();
106 virtual ~RegsArm64() = default;
107
108 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
109};
110
111class RegsX86 : public RegsTmpl<uint32_t> {
112 public:
113 RegsX86();
114 virtual ~RegsX86() = default;
115
116 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
117};
118
119class RegsX86_64 : public RegsTmpl<uint64_t> {
120 public:
121 RegsX86_64();
122 virtual ~RegsX86_64() = default;
123
124 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800125};
126
127#endif // _LIBUNWINDSTACK_REGS_H