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_MEMORY_H |
| 18 | #define _LIBUNWINDSTACK_MEMORY_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <string> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 25 | #include <vector> |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 26 | |
| 27 | class Memory { |
| 28 | public: |
| 29 | Memory() = default; |
| 30 | virtual ~Memory() = default; |
| 31 | |
| 32 | virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX); |
| 33 | |
| 34 | virtual bool Read(uint64_t addr, void* dst, size_t size) = 0; |
| 35 | |
| 36 | inline bool Read(uint64_t addr, void* start, void* field, size_t size) { |
| 37 | return Read(addr + reinterpret_cast<uintptr_t>(field) - reinterpret_cast<uintptr_t>(start), |
| 38 | field, size); |
| 39 | } |
| 40 | |
| 41 | inline bool Read32(uint64_t addr, uint32_t* dst) { |
| 42 | return Read(addr, dst, sizeof(uint32_t)); |
| 43 | } |
| 44 | |
| 45 | inline bool Read64(uint64_t addr, uint64_t* dst) { |
| 46 | return Read(addr, dst, sizeof(uint64_t)); |
| 47 | } |
| 48 | }; |
| 49 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 50 | class MemoryBuffer : public Memory { |
| 51 | public: |
| 52 | MemoryBuffer() = default; |
| 53 | virtual ~MemoryBuffer() = default; |
| 54 | |
| 55 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 56 | |
| 57 | uint8_t* GetPtr(size_t offset); |
| 58 | |
| 59 | void Resize(size_t size) { raw_.resize(size); } |
| 60 | |
| 61 | uint64_t Size() { return raw_.size(); } |
| 62 | |
| 63 | private: |
| 64 | std::vector<uint8_t> raw_; |
| 65 | }; |
| 66 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 67 | class MemoryFileAtOffset : public Memory { |
| 68 | public: |
| 69 | MemoryFileAtOffset() = default; |
| 70 | virtual ~MemoryFileAtOffset(); |
| 71 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 72 | bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX); |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 73 | |
| 74 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 75 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 76 | void Clear(); |
| 77 | |
Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 78 | protected: |
| 79 | size_t size_ = 0; |
| 80 | size_t offset_ = 0; |
| 81 | uint8_t* data_ = nullptr; |
| 82 | }; |
| 83 | |
| 84 | class MemoryOffline : public MemoryFileAtOffset { |
| 85 | public: |
| 86 | MemoryOffline() = default; |
| 87 | virtual ~MemoryOffline() = default; |
| 88 | |
| 89 | bool Init(const std::string& file, uint64_t offset); |
| 90 | |
| 91 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 92 | |
| 93 | private: |
| 94 | uint64_t start_; |
| 95 | }; |
| 96 | |
| 97 | class MemoryRemote : public Memory { |
| 98 | public: |
| 99 | MemoryRemote(pid_t pid) : pid_(pid) {} |
| 100 | virtual ~MemoryRemote() = default; |
| 101 | |
| 102 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 103 | |
| 104 | pid_t pid() { return pid_; } |
| 105 | |
| 106 | private: |
| 107 | pid_t pid_; |
| 108 | }; |
| 109 | |
| 110 | class MemoryLocal : public Memory { |
| 111 | public: |
| 112 | MemoryLocal() = default; |
| 113 | virtual ~MemoryLocal() = default; |
| 114 | |
| 115 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 116 | }; |
| 117 | |
| 118 | class MemoryRange : public Memory { |
| 119 | public: |
| 120 | MemoryRange(Memory* memory, uint64_t begin, uint64_t end) |
| 121 | : memory_(memory), begin_(begin), length_(end - begin_) {} |
| 122 | virtual ~MemoryRange() { delete memory_; } |
| 123 | |
| 124 | inline bool Read(uint64_t addr, void* dst, size_t size) override { |
| 125 | if (addr + size <= length_) { |
| 126 | return memory_->Read(addr + begin_, dst, size); |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | Memory* memory_; |
| 133 | uint64_t begin_; |
| 134 | uint64_t length_; |
| 135 | }; |
| 136 | |
| 137 | #endif // _LIBUNWINDSTACK_MEMORY_H |