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