| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [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_REGS_H | 
 | 18 | #define _LIBUNWINDSTACK_REGS_H | 
 | 19 |  | 
 | 20 | #include <stdint.h> | 
 | 21 |  | 
 | 22 | #include <vector> | 
 | 23 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | // Forward declarations. | 
 | 25 | class Elf; | 
 | 26 | struct MapInfo; | 
 | 27 |  | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 28 | class Regs { | 
 | 29 |  public: | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 30 |   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 Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 45 |   virtual ~Regs() = default; | 
 | 46 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 47 |   virtual void* RawData() = 0; | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 48 |   virtual uint64_t pc() = 0; | 
 | 49 |   virtual uint64_t sp() = 0; | 
 | 50 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 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 |  | 
 | 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 Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 62 |  protected: | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 63 |   uint16_t total_regs_; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 64 |   uint16_t sp_reg_; | 
 | 65 |   Location return_loc_; | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 66 | }; | 
 | 67 |  | 
 | 68 | template <typename AddressType> | 
 | 69 | class RegsTmpl : public Regs { | 
 | 70 |  public: | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 71 |   RegsTmpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc) | 
 | 72 |       : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {} | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 73 |   virtual ~RegsTmpl() = default; | 
 | 74 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 75 |   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 Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 84 |  | 
 | 85 |   inline AddressType& operator[](size_t reg) { return regs_[reg]; } | 
 | 86 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 87 |   void* RawData() override { return regs_.data(); } | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 88 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 89 |  protected: | 
 | 90 |   AddressType pc_; | 
 | 91 |   AddressType sp_; | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 92 |   std::vector<AddressType> regs_; | 
 | 93 | }; | 
 | 94 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 95 | class RegsArm : public RegsTmpl<uint32_t> { | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 96 |  public: | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 97 |   RegsArm(); | 
 | 98 |   virtual ~RegsArm() = default; | 
 | 99 |  | 
 | 100 |   uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override; | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 101 | }; | 
 | 102 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 103 | class RegsArm64 : public RegsTmpl<uint64_t> { | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 104 |  public: | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 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; | 
| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 125 | }; | 
 | 126 |  | 
 | 127 | #endif  // _LIBUNWINDSTACK_REGS_H |