• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_ARK_H
17 #define DFX_ARK_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <securec.h>
22 #include <string>
23 #include <vector>
24 
25 namespace panda {
26 namespace ecmascript {
27 constexpr uint16_t FUNCTIONNAME_MAX = 1024;
28 constexpr uint16_t PACKAGENAME_MAX = 1024;
29 constexpr uint16_t URL_MAX = 1024;
30 constexpr uint16_t BUF_SIZE_MAX = FUNCTIONNAME_MAX + 32;
31 
32 typedef bool (*ReadMemFunc)(void *, uintptr_t, uintptr_t *);
33 
34 struct JsFrame {
35     char functionName[FUNCTIONNAME_MAX];
36     char packageName[PACKAGENAME_MAX];
37     char url[URL_MAX];
38     int32_t line;
39     int32_t column;
40 };
41 
42 struct JsFunction {
43     char functionName[FUNCTIONNAME_MAX];
44     char packageName[PACKAGENAME_MAX];
45     char url[URL_MAX];
46     int32_t line = 0;
47     int32_t column = 0;
48     uintptr_t codeBegin;
49     uintptr_t codeSize;
ToStringJsFunction50     std::string ToString()
51     {
52         char buff[BUF_SIZE_MAX] = {0};
53         if (snprintf_s(buff, sizeof(buff), sizeof(buff) - 1, "%s:[url:%s:%d:%d]",
54             functionName, url, line, column) < 0) {
55             return std::string("Unknown Function:[url:Unknown URL]");
56         }
57         return std::string(buff);
58     }
59 };
60 
61 struct ArkUnwindParam {
62     void *ctx;
63     ReadMemFunc readMem;
64     uintptr_t *fp;
65     uintptr_t *sp;
66     uintptr_t *pc;
67     uintptr_t *methodId;
68     bool *isJsFrame;
69     std::vector<uintptr_t>& jitCache;
ArkUnwindParamArkUnwindParam70     ArkUnwindParam(void *ctx, ReadMemFunc readMem, uintptr_t *fp, uintptr_t *sp, uintptr_t *pc,
71         uintptr_t *methodId, bool *isJsFrame, std::vector<uintptr_t>& jitCache)
72         : ctx(ctx), readMem(readMem), fp(fp), sp(sp), pc(pc),
73           methodId(methodId), isJsFrame(isJsFrame), jitCache(jitCache) {}
74 };
75 
76 struct ArkStepParam {
77     uintptr_t *fp;
78     uintptr_t *sp;
79     uintptr_t *pc;
80     bool *isJsFrame;
81 
ArkStepParamArkStepParam82     ArkStepParam(uintptr_t *fp, uintptr_t *sp, uintptr_t *pc, bool *isJsFrame)
83         : fp(fp), sp(sp), pc(pc), isJsFrame(isJsFrame) {}
84 };
85 }
86 }
87 
88 namespace OHOS {
89 namespace HiviewDFX {
90 using JsFrame = panda::ecmascript::JsFrame;
91 using JsFunction = panda::ecmascript::JsFunction;
92 using ReadMemFunc = panda::ecmascript::ReadMemFunc;
93 using ArkUnwindParam = panda::ecmascript::ArkUnwindParam;
94 using ArkStepParam = panda::ecmascript::ArkStepParam;
95 
96 enum class ArkFunction {
97     ARK_LIB_NAME,
98     ARK_CREATE_JS_SYMBOL_EXTRACTOR,
99     ARK_DESTORY_JS_SYMBOL_EXTRACTOR,
100     ARK_CREATE_LOCAL,
101     ARK_DESTROY_LOCAL,
102     ARK_PARSE_JS_FILE_INFO,
103     ARK_PARSE_JS_FRAME_INFO_LOCAL,
104     ARK_PARSE_JS_FRAME_INFO,
105     STEP_ARK,
106     STEP_ARK_WITH_RECORD_JIT,
107     ARK_WRITE_JIT_CODE,
108 };
109 
110 class DfxArk {
111 public:
112     static DfxArk& Instance();
113     /**
114      * @brief step ark frame
115      *
116      * @param obj memory pointer object
117      * @param readMemFn read memory function
118      * @param fp fp register
119      * @param sp sp register
120      * @param pc pc register
121      * @param isJsFrame isJsFrame variable
122      * @return if succeed return 1, otherwise return -1
123     */
124     int StepArkFrame(void *obj, OHOS::HiviewDFX::ReadMemFunc readMemFn,
125         OHOS::HiviewDFX::ArkStepParam* arkParam);
126 
127     /**
128      * @brief step ark frame with jit
129      *
130      * @param arkParam ark param
131      * @return if succeed return 1, otherwise return -1
132     */
133     int StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam* arkParam);
134 
135     /**
136      * @brief jit code write file
137      *
138      * @param ctx memory pointer object
139      * @param readMemFn read memory function
140      * @param fd file descriptor
141      * @param jitCodeArray jit code array
142      * @param jitSize jit size
143      * @return if succeed return 1, otherwise return -1
144     */
145     int JitCodeWriteFile(void* ctx, OHOS::HiviewDFX::ReadMemFunc readMemFn, int fd,
146         const uintptr_t* const jitCodeArray, const size_t jitSize);
147 
148     /**
149      * @brief parse ark file info
150      *
151      * @param byteCodePc byteCode Pc
152      * @param mapBase map base address
153      * @param name map name
154      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
155      * @param jsFunction jsFunction variable
156      * @return if succeed return 1, otherwise return -1
157     */
158     int ParseArkFileInfo(uintptr_t byteCodePc, uintptr_t mapBase, const char* name,
159         uintptr_t extractorPtr, JsFunction *jsFunction);
160 
161     /**
162      * @brief parse ark file info by local
163      *
164      * @param byteCodePc byteCode Pc
165      * @param mapBase map base address
166      * @param loadOffset map offset
167      * @param jsFunction jsFunction variable
168      * @return if succeed return 1, otherwise return -1
169     */
170     int ParseArkFrameInfoLocal(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
171         JsFunction *jsFunction);
172 
173     /**
174      * @brief Parse Ark Frame Info, for hiperf/hiProfile
175      *
176      * @param byteCodePc byteCode Pc
177      * @param mapBase map base address
178      * @param loadOffset map offset
179      * @param data abc data
180      * @param dataSize abc data size
181      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
182      * @param jsFunction jsFunction variable
183      * @return if succeed return 1, otherwise return -1
184     */
185     int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
186         uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
187 
188     /**
189      * @brief create ark js symbol extracrot
190      *
191      * @param extractorPtr extractorPtr variable
192      * @return if succeed return 1, otherwise return -1
193     */
194     int ArkCreateJsSymbolExtractor(uintptr_t* extractorPtr);
195     /**
196      * @brief destory ark js symbol extracrot
197      *
198      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
199      * @return if succeed return 1, otherwise return -1
200     */
201     int ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr);
202 
203     /**
204      * @brief create ark object by local
205      *
206      * @return if succeed return 1, otherwise return -1
207     */
208     int ArkCreateLocal();
209     /**
210      * @brief destory ark object by local
211      *
212      * @return if succeed return 1, otherwise return -1
213     */
214     int ArkDestroyLocal();
215 
216     /**
217      * @brief init ark function.
218      *
219      * @param arkFunction target function.
220      * @return
221      */
222     bool InitArkFunction(ArkFunction arkFunction);
223 private:
224     DfxArk() = default;
225     ~DfxArk() = default;
226     DfxArk(const DfxArk&) = delete;
227     DfxArk& operator=(const DfxArk&) = delete;
228     bool GetLibArkHandle(void);
229     bool DlsymArkFunc(const char* funcName, void** dlsymFuncName);
230 
231     void* handle_ = nullptr;
232     using StepArkFn = int (*)(void*, OHOS::HiviewDFX::ReadMemFunc, OHOS::HiviewDFX::ArkStepParam*);
233     using StepArkWithJitFn = int (*)(OHOS::HiviewDFX::ArkUnwindParam*);
234     using JitCodeWriteFileFn = int (*)(void*, OHOS::HiviewDFX::ReadMemFunc, int, const uintptr_t* const, const size_t);
235     using ParseArkFileInfoFn = int (*)(uintptr_t, uintptr_t, const char*, uintptr_t, JsFunction*);
236     using ParseArkFrameInfoLocalFn = int (*)(uintptr_t, uintptr_t, uintptr_t, JsFunction*);
237     using ParseArkFrameInfoFn = int (*)(uintptr_t, uintptr_t, uintptr_t, uint8_t*,
238                                         uint64_t, uintptr_t, JsFunction*);
239     using ArkCreateJsSymbolExtractorFn = int (*)(uintptr_t*);
240     using ArkDestoryJsSymbolExtractorFn = int (*)(uintptr_t);
241     using ArkCreateLocalFn = int (*)();
242     using ArkDestroyLocalFn = int (*)();
243     std::mutex arkMutex_;
244     StepArkFn stepArkFn_ = nullptr;
245     StepArkWithJitFn stepArkWithJitFn_ = nullptr;
246     JitCodeWriteFileFn jitCodeWriteFileFn_ = nullptr;
247     ParseArkFileInfoFn parseArkFileInfoFn_ = nullptr;
248     ParseArkFrameInfoLocalFn parseArkFrameInfoLocalFn_ = nullptr;
249     ParseArkFrameInfoFn parseArkFrameInfoFn_ = nullptr;
250     ArkCreateJsSymbolExtractorFn arkCreateJsSymbolExtractorFn_ = nullptr;
251     ArkDestoryJsSymbolExtractorFn arkDestoryJsSymbolExtractorFn_ = nullptr;
252     ArkDestroyLocalFn arkDestroyLocalFn_ = nullptr;
253     ArkCreateLocalFn arkCreateLocalFn_ = nullptr;
254 };
255 } // namespace HiviewDFX
256 } // namespace OHOS
257 
258 #endif
259