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