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 35 struct backtrace_map_t { 36 uintptr_t start; 37 uintptr_t end; 38 int flags; 39 std::string name; 40 }; 41 42 class BacktraceMap { 43 public: 44 // If uncached is true, then parse the current process map as of the call. 45 // Passing a map created with uncached set to true to Backtrace::Create() 46 // is unsupported. 47 static BacktraceMap* Create(pid_t pid, bool uncached = false); 48 49 virtual ~BacktraceMap(); 50 51 // Get the map data structure for the given address. 52 virtual const backtrace_map_t* Find(uintptr_t addr); 53 54 // The flags returned are the same flags as used by the mmap call. 55 // The values are PROT_*. GetFlags(uintptr_t pc)56 int GetFlags(uintptr_t pc) { 57 const backtrace_map_t* map = Find(pc); 58 if (map) { 59 return map->flags; 60 } 61 return PROT_NONE; 62 } 63 IsReadable(uintptr_t pc)64 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; } IsWritable(uintptr_t pc)65 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; } IsExecutable(uintptr_t pc)66 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; } 67 68 typedef std::deque<backtrace_map_t>::iterator iterator; begin()69 iterator begin() { return maps_.begin(); } end()70 iterator end() { return maps_.end(); } 71 72 typedef std::deque<backtrace_map_t>::const_iterator const_iterator; begin()73 const_iterator begin() const { return maps_.begin(); } end()74 const_iterator end() const { return maps_.end(); } 75 76 virtual bool Build(); 77 78 protected: 79 BacktraceMap(pid_t pid); 80 81 virtual bool ParseLine(const char* line, backtrace_map_t* map); 82 83 std::deque<backtrace_map_t> maps_; 84 pid_t pid_; 85 }; 86 87 #endif // _BACKTRACE_BACKTRACE_MAP_H 88