• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
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 #ifndef HOOK_RECORD_H
16 #define HOOK_RECORD_H
17 #include <memory>
18 #include <string>
19 #include <vector>
20 #include "hook_common.h"
21 #include "perf_event_record.h"
22 #include "native_hook_config.pb.h"
23 #include "native_hook_result.pb.h"
24 #include "native_hook_result.pbencoder.h"
25 #include "nocopyable.h"
26 #include "utilities.h"
27 #include "logging.h"
28 
29 namespace OHOS::Developtools::NativeDaemon {
30 const std::string MMAP_FILE_PAGE_PREFIX = "FilePage:";
31 const std::string PR_SET_VMA_PREFIX = "Anonymous:";
32 using NativeHookProto = std::variant<NativeHookData*, OHOS::Developtools::Profiler::ProtoEncoder::NativeHookData*>;
33 
34 struct RawStack {
35     std::unique_ptr<uint8_t[]> baseStackData = nullptr; // save the shared memory data
36     BaseStackRawData* stackContext = nullptr; // points to the foundation type data
37     union {
38         uint8_t* stackData; //cannot initialize mutiple members of union
39         const char* jsStackData = nullptr;
40     };
41     uint8_t* data = nullptr; // fp mode data is ip, dwarf mode data is regs
42     uint32_t stackSize = 0;
43     uint8_t fpDepth = 0; // fp mode fpDepth is ip depth, dwarf mode is invalid
44     bool reportFlag = false;
45     bool reduceStackFlag = false;
ResetRawStack46     void Reset()
47     {
48         baseStackData = nullptr;
49         stackContext = nullptr;
50         data = nullptr;
51         stackData = nullptr;
52         stackSize = 0;
53         fpDepth = 0;
54         reportFlag = false;
55         reduceStackFlag = false;
56     }
57 };
58 
59 struct SerializeInfo {
60     std::vector<CallFrame>* callFrames = nullptr;
61     uint32_t stackMapId = 0;
62     std::string tagName = "";
63     NativeHookConfig* config = nullptr;
64 };
65 
66 using RawStackPtr = std::shared_ptr<RawStack>;
67 
68 class HookRecord {
69 public:
70     HookRecord() = default;
HookRecord(RawStackPtr rawStack)71     HookRecord(RawStackPtr rawStack) : rawStack_(rawStack){};
72     virtual uint16_t GetType();
SerializeData(NativeHookProto stackData,SerializeInfo & hookInfo)73     virtual void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo){};
74     virtual void Reset();
75     virtual bool IsValid();
76     virtual uint64_t GetAddr();
GetRawStack()77     RawStackPtr GetRawStack()
78     {
79         return rawStack_;
80     }
81     virtual void SetAddr(uint64_t addr);
82 
83     template <typename T>
84     void SetEventFrame(T* event, SerializeInfo& hookInfo);
85 
86     template <typename T>
87     void SetFrameInfo(T& frame, CallFrame& callFrame, NativeHookConfig* config);
88 
89     template <typename T>
90     void SetSize(T* event);
91 
92     virtual ~HookRecord() = default;
93     RawStackPtr rawStack_ = nullptr;
94 };
95 
96 using HookRecordPtr = STD_PTR(shared, OHOS::Developtools::NativeDaemon::HookRecord);
97 
98 class FreeRecord : public HookRecord {
99 public:
100     FreeRecord() = default;
FreeRecord(RawStackPtr rawStack)101     FreeRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
102     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
103 };
104 
105 class FreeRecordSimp : public HookRecord {
106 public:
107     FreeRecordSimp() = default;
FreeRecordSimp(uint64_t freeAddr)108     FreeRecordSimp(uint64_t freeAddr) : freeAddr_(freeAddr) {};
GetType()109     uint16_t GetType() override
110     {
111         return FREE_MSG_SIMP;
112     }
Reset()113     void Reset() override
114     {
115         freeAddr_ = 0;
116     }
GetAddr()117     uint64_t GetAddr() override
118     {
119         return freeAddr_;
120     }
SetAddr(uint64_t freeAddr)121     void SetAddr(uint64_t freeAddr) override
122     {
123         freeAddr_ = freeAddr;
124     }
IsValid()125     bool IsValid() override
126     {
127         return (freeAddr_ != 0);
128     }
129 
130 private:
131     uint64_t freeAddr_ = 0;
132 };
133 
134 class MallocRecord : public HookRecord {
135 public:
136     MallocRecord() = default;
MallocRecord(RawStackPtr rawStack)137     MallocRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
138     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
139 };
140 
141 class MmapRecord : public HookRecord {
142 public:
143     MmapRecord() = default;
MmapRecord(RawStackPtr rawStack)144     MmapRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
145     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
146 };
147 
148 class MmapFilePageRecord : public HookRecord {
149 public:
MmapFilePageRecord(RawStackPtr rawStack)150     MmapFilePageRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
151     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
152 };
153 
154 class MunmapRecord : public HookRecord {
155 public:
156     MunmapRecord() = default;
MunmapRecord(RawStackPtr rawStack)157     MunmapRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
158     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
159 };
160 
161 class PrSetVmaRecord : public HookRecord {
162 public:
PrSetVmaRecord(RawStackPtr rawStack)163     PrSetVmaRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
164     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
165 };
166 
167 class MemoryUsingRecord : public HookRecord {
168 public:
MemoryUsingRecord(RawStackPtr rawStack)169     MemoryUsingRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
170     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
171 };
172 
173 class MemoryUnusingRecord : public HookRecord {
174 public:
MemoryUnusingRecord(RawStackPtr rawStack)175     MemoryUnusingRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
176     void SerializeData(NativeHookProto stackData, SerializeInfo& hookInfo) override;
177 };
178 
179 class NmdRecord : public HookRecord {
180 public:
NmdRecord(RawStackPtr rawStack)181     NmdRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
182 };
183 
184 class JsRecord : public HookRecord {
185 public:
186     JsRecord() = default;
JsRecord(RawStackPtr rawStack)187     JsRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
188 };
189 
190 class TagRecord : public HookRecord {
191 public:
TagRecord(RawStackPtr rawStack)192     TagRecord(RawStackPtr rawStack) : HookRecord(rawStack) {};
193 };
194 }
195 #endif //HOOK_RECORD_H