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> |
| 25 | |
| 26 | #include <android-base/unique_fd.h> |
| 27 | |
| 28 | constexpr bool kMemoryStatsEnabled = true; |
| 29 | |
| 30 | class Memory { |
| 31 | public: |
| 32 | Memory() = default; |
| 33 | virtual ~Memory() = default; |
| 34 | |
| 35 | virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX); |
| 36 | |
| 37 | virtual bool Read(uint64_t addr, void* dst, size_t size) = 0; |
| 38 | |
| 39 | inline bool Read(uint64_t addr, void* start, void* field, size_t size) { |
| 40 | return Read(addr + reinterpret_cast<uintptr_t>(field) - reinterpret_cast<uintptr_t>(start), |
| 41 | field, size); |
| 42 | } |
| 43 | |
| 44 | inline bool Read32(uint64_t addr, uint32_t* dst) { |
| 45 | return Read(addr, dst, sizeof(uint32_t)); |
| 46 | } |
| 47 | |
| 48 | inline bool Read64(uint64_t addr, uint64_t* dst) { |
| 49 | return Read(addr, dst, sizeof(uint64_t)); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class MemoryFileAtOffset : public Memory { |
| 54 | public: |
| 55 | MemoryFileAtOffset() = default; |
| 56 | virtual ~MemoryFileAtOffset(); |
| 57 | |
| 58 | bool Init(const std::string& file, uint64_t offset); |
| 59 | |
| 60 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 61 | |
| 62 | protected: |
| 63 | size_t size_ = 0; |
| 64 | size_t offset_ = 0; |
| 65 | uint8_t* data_ = nullptr; |
| 66 | }; |
| 67 | |
| 68 | class MemoryOffline : public MemoryFileAtOffset { |
| 69 | public: |
| 70 | MemoryOffline() = default; |
| 71 | virtual ~MemoryOffline() = default; |
| 72 | |
| 73 | bool Init(const std::string& file, uint64_t offset); |
| 74 | |
| 75 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 76 | |
| 77 | private: |
| 78 | uint64_t start_; |
| 79 | }; |
| 80 | |
| 81 | class MemoryRemote : public Memory { |
| 82 | public: |
| 83 | MemoryRemote(pid_t pid) : pid_(pid) {} |
| 84 | virtual ~MemoryRemote() = default; |
| 85 | |
| 86 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 87 | |
| 88 | pid_t pid() { return pid_; } |
| 89 | |
| 90 | private: |
| 91 | pid_t pid_; |
| 92 | }; |
| 93 | |
| 94 | class MemoryLocal : public Memory { |
| 95 | public: |
| 96 | MemoryLocal() = default; |
| 97 | virtual ~MemoryLocal() = default; |
| 98 | |
| 99 | bool Read(uint64_t addr, void* dst, size_t size) override; |
| 100 | }; |
| 101 | |
| 102 | class MemoryRange : public Memory { |
| 103 | public: |
| 104 | MemoryRange(Memory* memory, uint64_t begin, uint64_t end) |
| 105 | : memory_(memory), begin_(begin), length_(end - begin_) {} |
| 106 | virtual ~MemoryRange() { delete memory_; } |
| 107 | |
| 108 | inline bool Read(uint64_t addr, void* dst, size_t size) override { |
| 109 | if (addr + size <= length_) { |
| 110 | return memory_->Read(addr + begin_, dst, size); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | Memory* memory_; |
| 117 | uint64_t begin_; |
| 118 | uint64_t length_; |
| 119 | }; |
| 120 | |
| 121 | #endif // _LIBUNWINDSTACK_MEMORY_H |