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