1 /* 2 * Copyright (c) 2021 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 MEMMAPITEM_H 17 #define MEMMAPITEM_H 18 19 #include <sstream> 20 #include <string> 21 #include <utilities.h> 22 23 class MemMaps { 24 public: MemMaps()25 MemMaps() {} MemMaps(uint64_t begin,uint64_t end,uint64_t offset,uint32_t type,uint32_t filePathId,const std::string & name)26 MemMaps(uint64_t begin, uint64_t end, uint64_t offset, uint32_t type, uint32_t filePathId, const std::string& name) 27 : soBegin_(begin), soEnd_(end), filePathId_(filePathId), name_(name) 28 { 29 maps_.emplace_back(begin, end, static_cast<uint16_t>(type), offset, name_); 30 } 31 uint64_t soBegin_; 32 uint64_t soEnd_; 33 uint32_t filePathId_ {0}; // for maps item filePath id 34 std::string name_; 35 bool isReported_ {false}; // indicates whether information about item has been reported 36 37 class MemMapItem { 38 public: MemMapItem()39 MemMapItem() {} MemMapItem(uint64_t begin,uint64_t end,uint16_t type,uint64_t offset,std::string_view nameHold)40 MemMapItem(uint64_t begin, uint64_t end, uint16_t type, uint64_t offset, std::string_view nameHold) 41 : begin_(begin), end_(end), type_(type), pageoffset_(offset), nameHold_(nameHold) {} 42 uint64_t begin_ {0}; 43 uint64_t end_ {0}; 44 uint16_t type_ {0}; // rwx : PROT_READ | PROT_WRITE | PROT_EXEC 45 uint16_t flags_ {0}; // ps : MAP_PRIVATE | MAP_SHARED 46 uint64_t pageoffset_ {0}; 47 std::string_view nameHold_; 48 FileOffsetFromAddr(uint64_t addr)49 uint64_t FileOffsetFromAddr(uint64_t addr) const 50 { 51 // real vaddr - real map begin = addr offset in section 52 // section offset + page off set = file offset 53 return addr - begin_ + pageoffset_; 54 } 55 }; 56 57 std::vector<MemMapItem> maps_; 58 }; 59 60 class MemMapItem { 61 public: 62 uint64_t begin_ = 0; 63 uint64_t end_ = 0; 64 uint16_t type_ = 0; // rwx : PROT_READ | PROT_WRITE | PROT_EXEC 65 uint16_t flags = 0; // ps : MAP_PRIVATE | MAP_SHARED 66 uint64_t pageoffset_ = 0; 67 uint64_t major_ = 0; 68 uint64_t minor_ = 0; 69 uint64_t inode = 0; 70 uint32_t filePathId_ = 0; // for maps item filePath id 71 std::string name_; 72 std::string_view nameHold_; 73 bool isReported = false; // indicates whether information about memMapItem has been reported 74 MemMapItem()75 MemMapItem() {} MemMapItem(uint64_t begin,uint64_t end,uint64_t offset,const std::string & name)76 MemMapItem(uint64_t begin, uint64_t end, uint64_t offset, const std::string &name) 77 : begin_(begin), end_(end), pageoffset_(offset), name_(name), 78 nameHold_(OHOS::Developtools::NativeDaemon::memHolder.HoldStringView(name)) 79 { 80 } 81 82 // use for find 83 inline bool operator==(const std::string &name) const 84 { 85 return name_ == name; 86 } 87 88 inline bool operator<(const MemMapItem &other) const 89 { 90 return end_ < other.end_; 91 } 92 FileOffsetFromAddr(uint64_t addr)93 uint64_t FileOffsetFromAddr(uint64_t addr) const 94 { 95 // real vaddr - real map begin = addr offset in section 96 // section offset + page off set = file offset 97 return addr - begin_ + pageoffset_; 98 } 99 // debug only ToString()100 const std::string ToString() const 101 { 102 std::stringstream sstream; 103 sstream << "0x" << std::hex << begin_; 104 sstream << "-0x" << std::hex << end_; 105 sstream << " type 0x" << std::hex << type_; 106 sstream << " flags 0x" << std::hex << flags; 107 sstream << " pageoffset 0x" << std::hex << pageoffset_; 108 sstream << " " << name_; 109 return sstream.str(); 110 } GreaterSort(const MemMapItem & a,const MemMapItem & b)111 static bool GreaterSort(const MemMapItem &a, const MemMapItem &b) 112 { 113 return (a.begin_ > b.begin_); 114 } LessSort(const MemMapItem & a,const MemMapItem & b)115 static bool LessSort(const MemMapItem &a, const MemMapItem &b) 116 { 117 return (a.begin_ < b.begin_); 118 } 119 // The range [first, last) must be partitioned with respect to the expression !(value < element) 120 // or !comp(value, element) ValueLessThan(uint64_t vaddr,const MemMapItem & a)121 static bool ValueLessThan(uint64_t vaddr, const MemMapItem &a) 122 { 123 return vaddr <= a.begin_; 124 } Contain(uint64_t addr)125 bool Contain(uint64_t addr) const 126 { 127 return addr >= begin_ and addr < end_; 128 } 129 }; 130 #endif 131