1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DFX_MAP_H 17 #define DFX_MAP_H 18 19 #include <atomic> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <sys/stat.h> 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 class DfxElf; 28 class DfxHap; 29 30 class DfxMap { 31 public: 32 static std::shared_ptr<DfxMap> Create(const std::string& vma); 33 static void PermsToProts(const std::string perms, uint32_t& prots, uint32_t& flag); 34 static void FormatMapName(pid_t pid, std::string& mapName); 35 static std::string UnFormatMapName(const std::string& mapName); 36 37 DfxMap() = default; DfxMap(uint64_t begin,uint64_t end,uint64_t offset,const std::string & perms,const std::string & name)38 DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 39 const std::string& perms, const std::string& name) 40 : begin(begin), end(end), offset(offset), perms(perms), name(name) {} DfxMap(uint64_t begin,uint64_t end,uint64_t offset,uint32_t prots,const std::string & name)41 DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 42 uint32_t prots, const std::string& name) 43 : begin(begin), end(end), offset(offset), prots(prots), name(name) {} 44 45 bool Parse(const char* buf, size_t size); 46 bool IsMapExec(); 47 bool IsArkExecutable(); 48 bool IsJsvmExecutable(); 49 bool IsVdsoMap(); 50 const std::shared_ptr<DfxHap> GetHap(); 51 const std::shared_ptr<DfxElf> GetElf(pid_t pid = 0); 52 std::string GetElfName(); 53 uint64_t GetRelPc(uint64_t pc); 54 std::string ToString() const; 55 56 uint64_t begin = 0; 57 uint64_t end = 0; 58 uint64_t offset = 0; 59 uint32_t prots = 0; 60 uint32_t flag = 0; 61 uint64_t major = 0; 62 uint64_t minor = 0; 63 ino_t inode = 0; 64 std::string perms = ""; // 5:rwxp 65 std::string name = ""; 66 std::shared_ptr<DfxElf> elf = nullptr; 67 std::shared_ptr<DfxHap> hap = nullptr; 68 std::shared_ptr<DfxMap> prevMap = nullptr; 69 uint64_t elfOffset = 0; 70 uint64_t elfStartOffset = 0; 71 int32_t symbolFileIndex = -1; // symbols file index 72 // use for find 73 inline bool operator==(const std::string &sname) const 74 { 75 return this->name == sname; 76 } 77 78 inline bool operator<(const DfxMap &other) const 79 { 80 return this->end < other.end; 81 } 82 Contain(uint64_t pc)83 bool Contain(uint64_t pc) const 84 { 85 return (pc >= begin && pc < end); 86 } 87 88 // The range [first, last) must be partitioned with respect to the expression 89 // !(value < element) or !comp(value, element) ValueLessThen(uint64_t vaddr,const DfxMap & a)90 static bool ValueLessThen(uint64_t vaddr, const DfxMap &a) 91 { 92 return vaddr < a.begin; 93 } ValueLessEqual(uint64_t vaddr,const DfxMap & a)94 static bool ValueLessEqual(uint64_t vaddr, const DfxMap &a) 95 { 96 return vaddr <= a.begin; 97 } FileOffsetFromAddr(uint64_t vaddr)98 uint64_t FileOffsetFromAddr(uint64_t vaddr) const 99 { 100 // real vaddr - real map begin = addr offset in section 101 // section offset + page off set = file offset 102 return vaddr - begin + offset; 103 } 104 }; 105 } // namespace HiviewDFX 106 } // namespace OHOS 107 108 #endif 109