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_MAPS_H 18 #define _LIBUNWINDSTACK_MAPS_H 19 20 #include <pthread.h> 21 #include <sys/types.h> 22 #include <unistd.h> 23 24 #include <memory> 25 #include <string> 26 #include <vector> 27 28 #include <unwindstack/MapInfo.h> 29 30 namespace unwindstack { 31 32 // Special flag to indicate a map is in /dev/. However, a map in 33 // /dev/ashmem/... does not set this flag. 34 static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000; 35 // Special flag to indicate that this map represents an elf file 36 // created by ART for use with the gdb jit debug interface. 37 // This should only ever appear in offline maps data. 38 static constexpr int MAPS_FLAGS_JIT_SYMFILE_MAP = 0x4000; 39 40 class Maps { 41 public: 42 virtual ~Maps() = default; 43 44 Maps() = default; 45 46 // Maps are not copyable but movable, because they own pointers to MapInfo 47 // objects. 48 Maps(const Maps&) = delete; 49 Maps& operator=(const Maps&) = delete; 50 Maps(Maps&&) = default; 51 Maps& operator=(Maps&&) = default; 52 53 virtual MapInfo* Find(uint64_t pc); 54 55 virtual bool Parse(); 56 GetMapsFile()57 virtual const std::string GetMapsFile() const { return ""; } 58 59 void Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name, 60 uint64_t load_bias); 61 62 void Sort(); 63 64 typedef std::vector<std::unique_ptr<MapInfo>>::iterator iterator; begin()65 iterator begin() { return maps_.begin(); } end()66 iterator end() { return maps_.end(); } 67 68 typedef std::vector<std::unique_ptr<MapInfo>>::const_iterator const_iterator; begin()69 const_iterator begin() const { return maps_.begin(); } end()70 const_iterator end() const { return maps_.end(); } 71 Total()72 size_t Total() { return maps_.size(); } 73 Get(size_t index)74 MapInfo* Get(size_t index) { 75 if (index >= maps_.size()) return nullptr; 76 return maps_[index].get(); 77 } 78 79 protected: 80 std::vector<std::unique_ptr<MapInfo>> maps_; 81 }; 82 83 class RemoteMaps : public Maps { 84 public: RemoteMaps(pid_t pid)85 RemoteMaps(pid_t pid) : pid_(pid) {} 86 virtual ~RemoteMaps() = default; 87 88 virtual const std::string GetMapsFile() const override; 89 90 private: 91 pid_t pid_; 92 }; 93 94 class LocalMaps : public RemoteMaps { 95 public: LocalMaps()96 LocalMaps() : RemoteMaps(getpid()) {} 97 virtual ~LocalMaps() = default; 98 }; 99 100 class LocalUpdatableMaps : public Maps { 101 public: 102 LocalUpdatableMaps(); 103 virtual ~LocalUpdatableMaps() = default; 104 105 MapInfo* Find(uint64_t pc) override; 106 107 bool Parse() override; 108 109 const std::string GetMapsFile() const override; 110 111 bool Reparse(/*out*/ bool* any_changed = nullptr); 112 113 protected: 114 std::vector<std::unique_ptr<MapInfo>> saved_maps_; 115 116 pthread_rwlock_t maps_rwlock_; 117 }; 118 119 class BufferMaps : public Maps { 120 public: BufferMaps(const char * buffer)121 BufferMaps(const char* buffer) : buffer_(buffer) {} 122 virtual ~BufferMaps() = default; 123 124 bool Parse() override; 125 126 private: 127 const char* buffer_; 128 }; 129 130 class FileMaps : public Maps { 131 public: FileMaps(const std::string & file)132 FileMaps(const std::string& file) : file_(file) {} 133 virtual ~FileMaps() = default; 134 GetMapsFile()135 const std::string GetMapsFile() const override { return file_; } 136 137 protected: 138 const std::string file_; 139 }; 140 141 } // namespace unwindstack 142 143 #endif // _LIBUNWINDSTACK_MAPS_H 144