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 | |
Josh Gao | 6f580d8 | 2017-09-22 12:57:15 -0700 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <string> |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 | namespace unwindstack { |
| 27 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 28 | // Forward declarations. |
| 29 | class Elf; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 30 | enum ArchEnum : uint8_t; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 | class Memory; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 32 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 33 | class Regs { |
| 34 | public: |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 35 | enum LocationEnum : uint8_t { |
| 36 | LOCATION_UNKNOWN = 0, |
| 37 | LOCATION_REGISTER, |
| 38 | LOCATION_SP_OFFSET, |
| 39 | }; |
| 40 | |
| 41 | struct Location { |
| 42 | Location(LocationEnum type, int16_t value) : type(type), value(value) {} |
| 43 | |
| 44 | LocationEnum type; |
| 45 | int16_t value; |
| 46 | }; |
| 47 | |
| 48 | Regs(uint16_t total_regs, uint16_t sp_reg, const Location& return_loc) |
| 49 | : total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {} |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 50 | virtual ~Regs() = default; |
| 51 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 52 | virtual ArchEnum Arch() = 0; |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 53 | |
Christopher Ferris | 02fdb56 | 2017-12-08 15:04:49 -0800 | [diff] [blame] | 54 | virtual bool Format32Bit() = 0; |
| 55 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 56 | virtual void* RawData() = 0; |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 57 | virtual uint64_t pc() = 0; |
| 58 | virtual uint64_t sp() = 0; |
| 59 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 60 | virtual uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) = 0; |
| 61 | |
Christopher Ferris | eb4a6db | 2017-07-19 12:37:45 -0700 | [diff] [blame] | 62 | virtual bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) = 0; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 63 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 64 | virtual void SetFromRaw() = 0; |
| 65 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 66 | virtual bool SetPcFromReturnAddress(Memory* process_memory) = 0; |
| 67 | |
Josh Gao | 6f580d8 | 2017-09-22 12:57:15 -0700 | [diff] [blame] | 68 | virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) = 0; |
| 69 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 70 | uint16_t sp_reg() { return sp_reg_; } |
| 71 | uint16_t total_regs() { return total_regs_; } |
| 72 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 73 | static ArchEnum CurrentArch(); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 74 | static Regs* RemoteGet(pid_t pid); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 75 | static Regs* CreateFromUcontext(ArchEnum arch, void* ucontext); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 76 | static Regs* CreateFromLocal(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 77 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 78 | protected: |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 79 | uint16_t total_regs_; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 80 | uint16_t sp_reg_; |
| 81 | Location return_loc_; |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | template <typename AddressType> |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 85 | class RegsImpl : public Regs { |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 86 | public: |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 87 | RegsImpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc) |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 88 | : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {} |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 89 | virtual ~RegsImpl() = default; |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 90 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 91 | uint64_t pc() override { return pc_; } |
| 92 | uint64_t sp() override { return sp_; } |
| 93 | |
| 94 | void set_pc(AddressType pc) { pc_ = pc; } |
| 95 | void set_sp(AddressType sp) { sp_ = sp; } |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 96 | |
Christopher Ferris | 02fdb56 | 2017-12-08 15:04:49 -0800 | [diff] [blame] | 97 | bool Format32Bit() override { return sizeof(AddressType) == sizeof(uint32_t); } |
| 98 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 99 | inline AddressType& operator[](size_t reg) { return regs_[reg]; } |
| 100 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 101 | void* RawData() override { return regs_.data(); } |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 102 | |
Josh Gao | 6f580d8 | 2017-09-22 12:57:15 -0700 | [diff] [blame] | 103 | virtual void IterateRegisters(std::function<void(const char*, uint64_t)> fn) override { |
| 104 | for (size_t i = 0; i < regs_.size(); ++i) { |
| 105 | fn(std::to_string(i).c_str(), regs_[i]); |
| 106 | } |
| 107 | } |
| 108 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 109 | protected: |
| 110 | AddressType pc_; |
| 111 | AddressType sp_; |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 112 | std::vector<AddressType> regs_; |
| 113 | }; |
| 114 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 115 | } // namespace unwindstack |
| 116 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 117 | #endif // _LIBUNWINDSTACK_REGS_H |