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_REGS_H 18 #define _LIBUNWINDSTACK_REGS_H 19 20 #include <stdint.h> 21 22 #include <vector> 23 24 // Forward declarations. 25 class Elf; 26 struct MapInfo; 27 28 class Regs { 29 public: 30 enum LocationEnum : uint8_t { 31 LOCATION_UNKNOWN = 0, 32 LOCATION_REGISTER, 33 LOCATION_SP_OFFSET, 34 }; 35 36 struct Location { LocationLocation37 Location(LocationEnum type, int16_t value) : type(type), value(value) {} 38 39 LocationEnum type; 40 int16_t value; 41 }; 42 Regs(uint16_t total_regs,uint16_t sp_reg,const Location & return_loc)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) {} 45 virtual ~Regs() = default; 46 47 virtual void* RawData() = 0; 48 virtual uint64_t pc() = 0; 49 virtual uint64_t sp() = 0; 50 51 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 sp_reg()57 uint16_t sp_reg() { return sp_reg_; } total_regs()58 uint16_t total_regs() { return total_regs_; } 59 60 static Regs* RemoteGet(pid_t pid, uint32_t* machine_type); 61 62 protected: 63 uint16_t total_regs_; 64 uint16_t sp_reg_; 65 Location return_loc_; 66 }; 67 68 template <typename AddressType> 69 class RegsTmpl : public Regs { 70 public: RegsTmpl(uint16_t total_regs,uint16_t sp_reg,Location return_loc)71 RegsTmpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc) 72 : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {} 73 virtual ~RegsTmpl() = default; 74 75 uint64_t GetRelPc(Elf* elf, const MapInfo* map_info) override; 76 77 bool GetReturnAddressFromDefault(Memory* memory, uint64_t* value) override; 78 pc()79 uint64_t pc() override { return pc_; } sp()80 uint64_t sp() override { return sp_; } 81 set_pc(AddressType pc)82 void set_pc(AddressType pc) { pc_ = pc; } set_sp(AddressType sp)83 void set_sp(AddressType sp) { sp_ = sp; } 84 85 inline AddressType& operator[](size_t reg) { return regs_[reg]; } 86 RawData()87 void* RawData() override { return regs_.data(); } 88 89 protected: 90 AddressType pc_; 91 AddressType sp_; 92 std::vector<AddressType> regs_; 93 }; 94 95 class RegsArm : public RegsTmpl<uint32_t> { 96 public: 97 RegsArm(); 98 virtual ~RegsArm() = default; 99 100 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; 101 }; 102 103 class RegsArm64 : public RegsTmpl<uint64_t> { 104 public: 105 RegsArm64(); 106 virtual ~RegsArm64() = default; 107 108 uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; 109 }; 110 111 class 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 119 class 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; 125 }; 126 127 #endif // _LIBUNWINDSTACK_REGS_H 128