1 /* 2 * Copyright (C) 2014 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 _BACKTRACE_BACKTRACE_MAP_H 18 #define _BACKTRACE_BACKTRACE_MAP_H 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 #ifdef _WIN32 23 // MINGW does not define these constants. 24 #define PROT_NONE 0 25 #define PROT_READ 0x1 26 #define PROT_WRITE 0x2 27 #define PROT_EXEC 0x4 28 #else 29 #include <sys/mman.h> 30 #endif 31 32 #include <deque> 33 #include <iterator> 34 #include <memory> 35 #include <string> 36 #include <vector> 37 38 // Forward declaration. 39 struct backtrace_stackinfo_t; 40 41 // Special flag to indicate a map is in /dev/. However, a map in 42 // /dev/ashmem/... does not set this flag. 43 static constexpr int PROT_DEVICE_MAP = 0x8000; 44 // Special flag to indicate that this map represents an elf file 45 // created by ART for use with the gdb jit debug interface. 46 // This should only ever appear in offline maps data. 47 static constexpr int PROT_JIT_SYMFILE_MAP = 0x4000; 48 49 struct backtrace_map_t { 50 uint64_t start = 0; 51 uint64_t end = 0; 52 uint64_t offset = 0; 53 uint64_t load_bias = 0; 54 int flags = 0; 55 std::string name; 56 57 // Returns `name` if non-empty, or `<anonymous:0x...>` otherwise. 58 std::string Name() const; 59 }; 60 61 namespace unwindstack { 62 class Memory; 63 } 64 65 class BacktraceMap { 66 public: 67 // If uncached is true, then parse the current process map as of the call. 68 // Passing a map created with uncached set to true to Backtrace::Create() 69 // is unsupported. 70 static BacktraceMap* Create(pid_t pid, bool uncached = false); 71 72 virtual ~BacktraceMap(); 73 74 class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> { 75 public: iterator(BacktraceMap * map,size_t index)76 iterator(BacktraceMap* map, size_t index) : map_(map), index_(index) {} 77 78 iterator& operator++() { 79 index_++; 80 return *this; 81 } 82 const iterator operator++(int increment) { 83 index_ += increment; 84 return *this; 85 } 86 iterator& operator--() { 87 index_--; 88 return *this; 89 } 90 const iterator operator--(int decrement) { 91 index_ -= decrement; 92 return *this; 93 } 94 95 bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; } 96 bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; } 97 98 const backtrace_map_t* operator*() { 99 if (index_ >= map_->size()) { 100 return nullptr; 101 } 102 backtrace_map_t* map = &map_->maps_[index_]; 103 if (map->load_bias == static_cast<uint64_t>(-1)) { 104 map->load_bias = map_->GetLoadBias(index_); 105 } 106 return map; 107 } 108 109 private: 110 BacktraceMap* map_ = nullptr; 111 size_t index_ = 0; 112 }; 113 begin()114 iterator begin() { return iterator(this, 0); } end()115 iterator end() { return iterator(this, maps_.size()); } 116 117 // Fill in the map data structure for the given address. 118 virtual void FillIn(uint64_t addr, backtrace_map_t* map); 119 120 // Get BuildId of ELF file at the given address, or empty string on failure. GetBuildId(uint64_t)121 virtual std::string GetBuildId(uint64_t /*pc*/) { return std::string(); } 122 123 // Only supported with the new unwinder. GetFunctionName(uint64_t,uint64_t *)124 virtual std::string GetFunctionName(uint64_t /*pc*/, uint64_t* /*offset*/) { return ""; } GetProcessMemory()125 virtual std::shared_ptr<unwindstack::Memory> GetProcessMemory() { return nullptr; } 126 127 // The flags returned are the same flags as used by the mmap call. 128 // The values are PROT_*. GetFlags(uint64_t pc)129 int GetFlags(uint64_t pc) { 130 backtrace_map_t map; 131 FillIn(pc, &map); 132 if (IsValid(map)) { 133 return map.flags; 134 } 135 return PROT_NONE; 136 } 137 IsReadable(uint64_t pc)138 bool IsReadable(uint64_t pc) { return GetFlags(pc) & PROT_READ; } IsWritable(uint64_t pc)139 bool IsWritable(uint64_t pc) { return GetFlags(pc) & PROT_WRITE; } IsExecutable(uint64_t pc)140 bool IsExecutable(uint64_t pc) { return GetFlags(pc) & PROT_EXEC; } 141 142 // In order to use the iterators on this object, a caller must 143 // call the LockIterator and UnlockIterator function to guarantee 144 // that the data does not change while it's being used. LockIterator()145 virtual void LockIterator() {} UnlockIterator()146 virtual void UnlockIterator() {} 147 size()148 size_t size() const { return maps_.size(); } 149 150 virtual bool Build(); 151 IsValid(const backtrace_map_t & map)152 static inline bool IsValid(const backtrace_map_t& map) { 153 return map.end > 0; 154 } 155 SetSuffixesToIgnore(std::vector<std::string> suffixes)156 void SetSuffixesToIgnore(std::vector<std::string> suffixes) { 157 suffixes_to_ignore_.insert(suffixes_to_ignore_.end(), suffixes.begin(), suffixes.end()); 158 } 159 GetSuffixesToIgnore()160 const std::vector<std::string>& GetSuffixesToIgnore() { return suffixes_to_ignore_; } 161 162 // Disabling the resolving of names results in the function name being 163 // set to an empty string and the function offset being set to zero 164 // in the frame data when unwinding. SetResolveNames(bool resolve)165 void SetResolveNames(bool resolve) { resolve_names_ = resolve; } 166 ResolveNames()167 bool ResolveNames() { return resolve_names_; } 168 169 protected: 170 BacktraceMap(pid_t pid); 171 GetLoadBias(size_t)172 virtual uint64_t GetLoadBias(size_t /* index */) { return 0; } 173 174 pid_t pid_; 175 std::deque<backtrace_map_t> maps_; 176 std::vector<std::string> suffixes_to_ignore_; 177 bool resolve_names_ = true; 178 }; 179 180 class ScopedBacktraceMapIteratorLock { 181 public: ScopedBacktraceMapIteratorLock(BacktraceMap * map)182 explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) { 183 map->LockIterator(); 184 } 185 ~ScopedBacktraceMapIteratorLock()186 ~ScopedBacktraceMapIteratorLock() { 187 map_->UnlockIterator(); 188 } 189 190 private: 191 BacktraceMap* map_; 192 }; 193 194 #endif // _BACKTRACE_BACKTRACE_MAP_H 195