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