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 <sys/types.h> 21 #include <unistd.h> 22 23 #include <string> 24 #include <vector> 25 26 #include <unwindstack/MapInfo.h> 27 28 namespace unwindstack { 29 30 // Special flag to indicate a map is in /dev/. However, a map in 31 // /dev/ashmem/... does not set this flag. 32 static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000; 33 34 class Maps { 35 public: 36 Maps() = default; 37 virtual ~Maps(); 38 39 MapInfo* Find(uint64_t pc); 40 41 bool ParseLine(const char* line, MapInfo* map_info); 42 43 virtual bool Parse(); 44 GetMapsFile()45 virtual const std::string GetMapsFile() const { return ""; } 46 47 typedef std::vector<MapInfo>::iterator iterator; begin()48 iterator begin() { return maps_.begin(); } end()49 iterator end() { return maps_.end(); } 50 51 typedef std::vector<MapInfo>::const_iterator const_iterator; begin()52 const_iterator begin() const { return maps_.begin(); } end()53 const_iterator end() const { return maps_.end(); } 54 Total()55 size_t Total() { return maps_.size(); } 56 57 protected: 58 std::vector<MapInfo> maps_; 59 }; 60 61 class RemoteMaps : public Maps { 62 public: RemoteMaps(pid_t pid)63 RemoteMaps(pid_t pid) : pid_(pid) {} 64 virtual ~RemoteMaps() = default; 65 66 virtual const std::string GetMapsFile() const override; 67 68 private: 69 pid_t pid_; 70 }; 71 72 class LocalMaps : public RemoteMaps { 73 public: LocalMaps()74 LocalMaps() : RemoteMaps(getpid()) {} 75 virtual ~LocalMaps() = default; 76 }; 77 78 class BufferMaps : public Maps { 79 public: BufferMaps(const char * buffer)80 BufferMaps(const char* buffer) : buffer_(buffer) {} 81 virtual ~BufferMaps() = default; 82 83 bool Parse() override; 84 85 private: 86 const char* buffer_; 87 }; 88 89 class FileMaps : public Maps { 90 public: FileMaps(const std::string & file)91 FileMaps(const std::string& file) : file_(file) {} 92 virtual ~FileMaps() = default; 93 GetMapsFile()94 const std::string GetMapsFile() const override { return file_; } 95 96 protected: 97 const std::string file_; 98 }; 99 100 class OfflineMaps : public FileMaps { 101 public: OfflineMaps(const std::string & file)102 OfflineMaps(const std::string& file) : FileMaps(file) {} 103 virtual ~OfflineMaps() = default; 104 105 bool Parse() override; 106 }; 107 108 } // namespace unwindstack 109 110 #endif // _LIBUNWINDSTACK_MAPS_H 111