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 29 class DfxMap { 30 public: 31 static std::shared_ptr<DfxMap> Create(const std::string buf, int size); 32 static void PermsToProts(const std::string perms, uint32_t& prots, uint32_t& flag); 33 34 DfxMap() = default; DfxMap(uint64_t begin,uint64_t end,uint64_t offset,const std::string & perms,const std::string & name)35 DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 36 const std::string& perms, const std::string& name) 37 : 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)38 DfxMap(uint64_t begin, uint64_t end, uint64_t offset, 39 uint32_t prots, const std::string& name) 40 : begin(begin), end(end), offset(offset), prots(prots), name(name) {} 41 42 bool Parse(const std::string buf, int size); 43 bool IsMapExec(); 44 bool IsArkExecutable(); 45 const std::shared_ptr<DfxElf> GetElf(); 46 std::string GetElfName(); 47 uint64_t GetRelPc(uint64_t pc); 48 std::string ToString(); 49 50 uint64_t begin = 0; 51 uint64_t end = 0; 52 uint64_t offset = 0; 53 uint32_t prots = 0; 54 uint32_t flag = 0; 55 uint64_t major = 0; 56 uint64_t minor = 0; 57 ino_t inode = 0; 58 std::string perms = ""; // 5:rwxp 59 std::string name = ""; 60 std::shared_ptr<DfxElf> elf = nullptr; 61 std::shared_ptr<DfxMap> prevMap = nullptr; 62 uint64_t elfOffset = 0; 63 uint64_t elfStartOffset = 0; 64 65 // use for find 66 inline bool operator==(const std::string &name) const 67 { 68 return this->name == name; 69 } 70 71 inline bool operator<(const DfxMap &other) const 72 { 73 return this->end < other.end; 74 } 75 Contain(uint64_t pc)76 bool Contain(uint64_t pc) const 77 { 78 return (pc >= begin && pc < end); 79 } 80 81 // The range [first, last) must be partitioned with respect to the expression 82 // !(value < element) or !comp(value, element) ValueLessThen(uint64_t vaddr,const DfxMap & a)83 static bool ValueLessThen(uint64_t vaddr, const DfxMap &a) 84 { 85 return vaddr < a.begin; 86 } ValueLessEqual(uint64_t vaddr,const DfxMap & a)87 static bool ValueLessEqual(uint64_t vaddr, const DfxMap &a) 88 { 89 return vaddr <= a.begin; 90 } FileOffsetFromAddr(uint64_t vaddr)91 uint64_t FileOffsetFromAddr(uint64_t vaddr) const 92 { 93 // real vaddr - real map begin = addr offset in section 94 // section offset + page off set = file offset 95 return vaddr - begin + offset; 96 } 97 }; 98 } // namespace HiviewDFX 99 } // namespace OHOS 100 101 #endif 102