• 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 class DfxArk {
97 public:
98     static DfxArk& Instance();
99     /**
100      * @brief step ark frame
101      *
102      * @param obj memory pointer object
103      * @param readMemFn read memory function
104      * @param fp fp register
105      * @param sp sp register
106      * @param pc pc register
107      * @param isJsFrame isJsFrame variable
108      * @return if succeed return 1, otherwise return -1
109     */
110     int StepArkFrame(void *obj, OHOS::HiviewDFX::ReadMemFunc readMemFn,
111         OHOS::HiviewDFX::ArkStepParam* arkParam);
112 
113     /**
114      * @brief step ark frame with jit
115      *
116      * @param arkParam ark param
117      * @return if succeed return 1, otherwise return -1
118     */
119     int StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam* arkParam);
120 
121     /**
122      * @brief jit code write file
123      *
124      * @param ctx memory pointer object
125      * @param readMemFn read memory function
126      * @param fd file descriptor
127      * @param jitCodeArray jit code array
128      * @param jitSize jit size
129      * @return if succeed return 1, otherwise return -1
130     */
131     int JitCodeWriteFile(void* ctx, OHOS::HiviewDFX::ReadMemFunc readMemFn, int fd,
132         const uintptr_t* const jitCodeArray, const size_t jitSize);
133 
134     /**
135      * @brief parse ark file info
136      *
137      * @param byteCodePc byteCode Pc
138      * @param mapBase map base address
139      * @param name map name
140      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
141      * @param jsFunction jsFunction variable
142      * @return if succeed return 1, otherwise return -1
143     */
144     int ParseArkFileInfo(uintptr_t byteCodePc, uintptr_t mapBase, const char* name,
145         uintptr_t extractorPtr, JsFunction *jsFunction);
146 
147     /**
148      * @brief parse ark file info by local
149      *
150      * @param byteCodePc byteCode Pc
151      * @param mapBase map base address
152      * @param loadOffset map offset
153      * @param jsFunction jsFunction variable
154      * @return if succeed return 1, otherwise return -1
155     */
156     int ParseArkFrameInfoLocal(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
157         JsFunction *jsFunction);
158 
159     /**
160      * @brief Parse Ark Frame Info, for hiperf/hiProfile
161      *
162      * @param byteCodePc byteCode Pc
163      * @param mapBase map base address
164      * @param loadOffset map offset
165      * @param data abc data
166      * @param dataSize abc data size
167      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
168      * @param jsFunction jsFunction variable
169      * @return if succeed return 1, otherwise return -1
170     */
171     int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
172         uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
173 
174     /**
175      * @brief create ark js symbol extracrot
176      *
177      * @param extractorPtr extractorPtr variable
178      * @return if succeed return 1, otherwise return -1
179     */
180     int ArkCreateJsSymbolExtractor(uintptr_t* extractorPtr);
181     /**
182      * @brief destory ark js symbol extracrot
183      *
184      * @param extractorPtr extractorPtr from ArkCreateJsSymbolExtractor
185      * @return if succeed return 1, otherwise return -1
186     */
187     int ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr);
188 
189     /**
190      * @brief create ark object by local
191      *
192      * @return if succeed return 1, otherwise return -1
193     */
194     int ArkCreateLocal();
195     /**
196      * @brief destory ark object by local
197      *
198      * @return if succeed return 1, otherwise return -1
199     */
200     int ArkDestroyLocal();
201 private:
202     DfxArk() = default;
203     ~DfxArk() = default;
204     DfxArk(const DfxArk&) = delete;
205     DfxArk& operator=(const DfxArk&) = delete;
206     bool GetLibArkHandle(void);
207     template <typename FuncName>
208     void DlsymArkFunc(const char* funcName, FuncName& dlsymFuncName);
209     void* handle_ = nullptr;
210     using StepArkFn = int (*)(void*, OHOS::HiviewDFX::ReadMemFunc, OHOS::HiviewDFX::ArkStepParam*);
211     using StepArkWithJitFn = int (*)(OHOS::HiviewDFX::ArkUnwindParam*);
212     using JitCodeWriteFileFn = int (*)(void*, OHOS::HiviewDFX::ReadMemFunc, int, const uintptr_t* const, const size_t);
213     using ParseArkFileInfoFn = int (*)(uintptr_t, uintptr_t, const char*, uintptr_t, JsFunction*);
214     using ParseArkFrameInfoLocalFn = int (*)(uintptr_t, uintptr_t, uintptr_t, JsFunction*);
215     using ParseArkFrameInfoFn = int (*)(uintptr_t, uintptr_t, uintptr_t, uint8_t*,
216                                         uint64_t, uintptr_t, JsFunction*);
217     using ArkCreateJsSymbolExtractorFn = int (*)(uintptr_t*);
218     using ArkDestoryJsSymbolExtractorFn = int (*)(uintptr_t);
219     using ArkCreateLocalFn = int (*)();
220     using ArkDestroyLocalFn = int (*)();
221     std::mutex arkMutex_;
222     StepArkFn stepArkFn_ = nullptr;
223     StepArkWithJitFn stepArkWithJitFn_ = nullptr;
224     JitCodeWriteFileFn jitCodeWriteFileFn_ = nullptr;
225     ParseArkFileInfoFn parseArkFileInfoFn_ = nullptr;
226     ParseArkFrameInfoLocalFn parseArkFrameInfoLocalFn_ = nullptr;
227     ParseArkFrameInfoFn parseArkFrameInfoFn_ = nullptr;
228     ArkCreateJsSymbolExtractorFn arkCreateJsSymbolExtractorFn_ = nullptr;
229     ArkDestoryJsSymbolExtractorFn arkDestoryJsSymbolExtractorFn_ = nullptr;
230     ArkDestroyLocalFn arkDestroyLocalFn_ = nullptr;
231     ArkCreateLocalFn arkCreateLocalFn_ = nullptr;
232 };
233 } // namespace HiviewDFX
234 } // namespace OHOS
235 
236 #endif
237