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 MemMapItem { 24 public: 25 uint64_t begin_ = 0; 26 uint64_t end_ = 0; 27 uint16_t type_ = 0; // rwx : PROT_READ | PROT_WRITE | PROT_EXEC 28 uint16_t flags = 0; // ps : MAP_PRIVATE | MAP_SHARED 29 uint64_t pageoffset_ = 0; 30 uint64_t major_ = 0; 31 uint64_t minor_ = 0; 32 uint64_t inode = 0; 33 std::string name_; 34 std::string_view nameHold_; 35 MemMapItem()36 MemMapItem() {} MemMapItem(uint64_t begin,uint64_t end,uint64_t offset,const std::string & name)37 MemMapItem(uint64_t begin, uint64_t end, uint64_t offset, const std::string &name) 38 : begin_(begin), end_(end), pageoffset_(offset), name_(name), 39 nameHold_(OHOS::Developtools::NativeDaemon::memHolder.HoldStringView(name)) 40 { 41 } 42 43 // use for find 44 inline bool operator==(const std::string &name) const 45 { 46 return name_ == name; 47 } 48 49 inline bool operator<(const MemMapItem &other) const 50 { 51 return end_ < other.end_; 52 } 53 FileOffsetFromAddr(uint64_t addr)54 uint64_t FileOffsetFromAddr(uint64_t addr) const 55 { 56 // real vaddr - real map begin = addr offset in section 57 // section offset + page off set = file offset 58 return addr - begin_ + pageoffset_; 59 } 60 // debug only ToString()61 const std::string ToString() const 62 { 63 std::stringstream sstream; 64 sstream << "0x" << std::hex << begin_; 65 sstream << "-0x" << std::hex << end_; 66 sstream << " type 0x" << std::hex << type_; 67 sstream << " flags 0x" << std::hex << flags; 68 sstream << " pageoffset 0x" << std::hex << pageoffset_; 69 sstream << " " << name_; 70 return sstream.str(); 71 } GreaterSort(const MemMapItem & a,const MemMapItem & b)72 static bool GreaterSort(const MemMapItem &a, const MemMapItem &b) 73 { 74 return (a.begin_ > b.begin_); 75 } LessSort(const MemMapItem & a,const MemMapItem & b)76 static bool LessSort(const MemMapItem &a, const MemMapItem &b) 77 { 78 return (a.begin_ < b.begin_); 79 } 80 // The range [first, last) must be partitioned with respect to the expression !(value < element) 81 // or !comp(value, element) ValueLessThan(uint64_t vaddr,const MemMapItem & a)82 static bool ValueLessThan(uint64_t vaddr, const MemMapItem &a) 83 { 84 return vaddr <= a.begin_; 85 } Contain(uint64_t addr)86 bool Contain(uint64_t addr) const 87 { 88 return addr >= begin_ and addr < end_; 89 } 90 }; 91 #endif 92