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