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 USE_MINGW 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 <string> 34 #include <vector> 35 36 struct backtrace_map_t { 37 uintptr_t start = 0; 38 uintptr_t end = 0; 39 uintptr_t offset = 0; 40 uintptr_t load_base = 0; 41 int flags = 0; 42 std::string name; 43 }; 44 45 class BacktraceMap { 46 public: 47 // If uncached is true, then parse the current process map as of the call. 48 // Passing a map created with uncached set to true to Backtrace::Create() 49 // is unsupported. 50 static BacktraceMap* Create(pid_t pid, bool uncached = false); 51 52 static BacktraceMap* Create(pid_t pid, const std::vector<backtrace_map_t>& maps); 53 54 virtual ~BacktraceMap(); 55 56 // Fill in the map data structure for the given address. 57 virtual void FillIn(uintptr_t addr, backtrace_map_t* map); 58 59 // The flags returned are the same flags as used by the mmap call. 60 // The values are PROT_*. GetFlags(uintptr_t pc)61 int GetFlags(uintptr_t pc) { 62 backtrace_map_t map; 63 FillIn(pc, &map); 64 if (IsValid(map)) { 65 return map.flags; 66 } 67 return PROT_NONE; 68 } 69 IsReadable(uintptr_t pc)70 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; } IsWritable(uintptr_t pc)71 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; } IsExecutable(uintptr_t pc)72 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; } 73 74 // In order to use the iterators on this object, a caller must 75 // call the LockIterator and UnlockIterator function to guarantee 76 // that the data does not change while it's being used. LockIterator()77 virtual void LockIterator() {} UnlockIterator()78 virtual void UnlockIterator() {} 79 80 typedef std::deque<backtrace_map_t>::iterator iterator; begin()81 iterator begin() { return maps_.begin(); } end()82 iterator end() { return maps_.end(); } 83 84 typedef std::deque<backtrace_map_t>::const_iterator const_iterator; begin()85 const_iterator begin() const { return maps_.begin(); } end()86 const_iterator end() const { return maps_.end(); } 87 88 virtual bool Build(); 89 IsValid(const backtrace_map_t & map)90 static inline bool IsValid(const backtrace_map_t& map) { 91 return map.end > 0; 92 } 93 GetRelativePc(const backtrace_map_t & map,uintptr_t pc)94 static uintptr_t GetRelativePc(const backtrace_map_t& map, uintptr_t pc) { 95 if (IsValid(map)) { 96 return pc - map.start + map.load_base; 97 } else { 98 return pc; 99 } 100 } 101 102 protected: 103 BacktraceMap(pid_t pid); 104 105 virtual bool ParseLine(const char* line, backtrace_map_t* map); 106 107 std::deque<backtrace_map_t> maps_; 108 pid_t pid_; 109 }; 110 111 class ScopedBacktraceMapIteratorLock { 112 public: ScopedBacktraceMapIteratorLock(BacktraceMap * map)113 explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) { 114 map->LockIterator(); 115 } 116 ~ScopedBacktraceMapIteratorLock()117 ~ScopedBacktraceMapIteratorLock() { 118 map_->UnlockIterator(); 119 } 120 121 private: 122 BacktraceMap* map_; 123 }; 124 125 #endif // _BACKTRACE_BACKTRACE_MAP_H 126