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 
24 #include "dfx_frame.h"
25 #include "dfx_map.h"
26 #include "dfx_memory.h"
27 
28 namespace panda {
29 namespace ecmascript {
30 constexpr uint16_t FUNCTIONNAME_MAX = 1024;
31 constexpr uint16_t URL_MAX = 1024;
32 constexpr uint16_t BUF_SIZE_MAX = FUNCTIONNAME_MAX + 32;
33 
34 typedef bool (*ReadMemFunc)(void *, uintptr_t, uintptr_t *);
35 
36 struct JsFrame {
37     char functionName[FUNCTIONNAME_MAX];
38     char url[URL_MAX];
39     int32_t line;
40     int32_t column;
41 };
42 
43 struct JsFunction {
44     char functionName[FUNCTIONNAME_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 }
77 
78 namespace OHOS {
79 namespace HiviewDFX {
80 using JsFrame = panda::ecmascript::JsFrame;
81 using JsFunction = panda::ecmascript::JsFunction;
82 using ReadMemFunc = panda::ecmascript::ReadMemFunc;
83 using ArkUnwindParam = panda::ecmascript::ArkUnwindParam;
84 
85 class DfxArk {
86 public:
87     static int GetArkNativeFrameInfo(int pid, uintptr_t& pc, uintptr_t& fp, uintptr_t& sp,
88                                      JsFrame* frames, size_t& size);
89 
90     static int StepArkFrame(void *obj, OHOS::HiviewDFX::ReadMemFunc readMemFn,
91         uintptr_t *fp, uintptr_t *sp, uintptr_t *pc, uintptr_t* methodid, bool *isJsFrame);
92 
93     static int StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam* arkPrama);
94 
95     static int JitCodeWriteFile(void* ctx, OHOS::HiviewDFX::ReadMemFunc readMemFn, int fd,
96         const uintptr_t* const jitCodeArray, const size_t jitSize);
97 
98     static int ParseArkFileInfo(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, const char* name,
99         uintptr_t extractorPtr, JsFunction *jsFunction);
100     static int ParseArkFrameInfoLocal(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, uintptr_t loadOffset,
101         JsFunction *jsFunction);
102     static int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
103         uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
104     static int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, uintptr_t loadOffset,
105         uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
106     static int TranslateArkFrameInfo(uint8_t *data, uint64_t dataSize, JsFunction *jsFunction);
107 
108     static int ArkCreateJsSymbolExtractor(uintptr_t* extractorPtr);
109     static int ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr);
110     static int ArkCreateLocal();
111     static int ArkDestroyLocal();
112 };
113 } // namespace HiviewDFX
114 } // namespace OHOS
115 
116 #endif
117