1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef DFX_ARK_H
17800b99b8Sopenharmony_ci#define DFX_ARK_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <cstdint>
20800b99b8Sopenharmony_ci#include <mutex>
21800b99b8Sopenharmony_ci#include <securec.h>
22800b99b8Sopenharmony_ci#include <string>
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_ci#include "dfx_frame.h"
25800b99b8Sopenharmony_ci#include "dfx_map.h"
26800b99b8Sopenharmony_ci#include "dfx_memory.h"
27800b99b8Sopenharmony_ci
28800b99b8Sopenharmony_cinamespace panda {
29800b99b8Sopenharmony_cinamespace ecmascript {
30800b99b8Sopenharmony_ciconstexpr uint16_t FUNCTIONNAME_MAX = 1024;
31800b99b8Sopenharmony_ciconstexpr uint16_t URL_MAX = 1024;
32800b99b8Sopenharmony_ciconstexpr uint16_t BUF_SIZE_MAX = FUNCTIONNAME_MAX + 32;
33800b99b8Sopenharmony_ci
34800b99b8Sopenharmony_citypedef bool (*ReadMemFunc)(void *, uintptr_t, uintptr_t *);
35800b99b8Sopenharmony_ci
36800b99b8Sopenharmony_cistruct JsFrame {
37800b99b8Sopenharmony_ci    char functionName[FUNCTIONNAME_MAX];
38800b99b8Sopenharmony_ci    char url[URL_MAX];
39800b99b8Sopenharmony_ci    int32_t line;
40800b99b8Sopenharmony_ci    int32_t column;
41800b99b8Sopenharmony_ci};
42800b99b8Sopenharmony_ci
43800b99b8Sopenharmony_cistruct JsFunction {
44800b99b8Sopenharmony_ci    char functionName[FUNCTIONNAME_MAX];
45800b99b8Sopenharmony_ci    char url[URL_MAX];
46800b99b8Sopenharmony_ci    int32_t line = 0;
47800b99b8Sopenharmony_ci    int32_t column = 0;
48800b99b8Sopenharmony_ci    uintptr_t codeBegin;
49800b99b8Sopenharmony_ci    uintptr_t codeSize;
50800b99b8Sopenharmony_ci    std::string ToString()
51800b99b8Sopenharmony_ci    {
52800b99b8Sopenharmony_ci        char buff[BUF_SIZE_MAX] = {0};
53800b99b8Sopenharmony_ci        if (snprintf_s(buff, sizeof(buff), sizeof(buff) - 1, "%s:[url:%s:%d:%d]",
54800b99b8Sopenharmony_ci            functionName, url, line, column) < 0) {
55800b99b8Sopenharmony_ci            return std::string("Unknown Function:[url:Unknown URL]");
56800b99b8Sopenharmony_ci        }
57800b99b8Sopenharmony_ci        return std::string(buff);
58800b99b8Sopenharmony_ci    }
59800b99b8Sopenharmony_ci};
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_cistruct ArkUnwindParam {
62800b99b8Sopenharmony_ci    void *ctx;
63800b99b8Sopenharmony_ci    ReadMemFunc readMem;
64800b99b8Sopenharmony_ci    uintptr_t *fp;
65800b99b8Sopenharmony_ci    uintptr_t *sp;
66800b99b8Sopenharmony_ci    uintptr_t *pc;
67800b99b8Sopenharmony_ci    uintptr_t *methodId;
68800b99b8Sopenharmony_ci    bool *isJsFrame;
69800b99b8Sopenharmony_ci    std::vector<uintptr_t>& jitCache;
70800b99b8Sopenharmony_ci    ArkUnwindParam(void *ctx, ReadMemFunc readMem, uintptr_t *fp, uintptr_t *sp, uintptr_t *pc,
71800b99b8Sopenharmony_ci        uintptr_t *methodId, bool *isJsFrame, std::vector<uintptr_t>& jitCache)
72800b99b8Sopenharmony_ci        : ctx(ctx), readMem(readMem), fp(fp), sp(sp), pc(pc),
73800b99b8Sopenharmony_ci          methodId(methodId), isJsFrame(isJsFrame), jitCache(jitCache) {}
74800b99b8Sopenharmony_ci};
75800b99b8Sopenharmony_ci}
76800b99b8Sopenharmony_ci}
77800b99b8Sopenharmony_ci
78800b99b8Sopenharmony_cinamespace OHOS {
79800b99b8Sopenharmony_cinamespace HiviewDFX {
80800b99b8Sopenharmony_ciusing JsFrame = panda::ecmascript::JsFrame;
81800b99b8Sopenharmony_ciusing JsFunction = panda::ecmascript::JsFunction;
82800b99b8Sopenharmony_ciusing ReadMemFunc = panda::ecmascript::ReadMemFunc;
83800b99b8Sopenharmony_ciusing ArkUnwindParam = panda::ecmascript::ArkUnwindParam;
84800b99b8Sopenharmony_ci
85800b99b8Sopenharmony_ciclass DfxArk {
86800b99b8Sopenharmony_cipublic:
87800b99b8Sopenharmony_ci    static int GetArkNativeFrameInfo(int pid, uintptr_t& pc, uintptr_t& fp, uintptr_t& sp,
88800b99b8Sopenharmony_ci                                     JsFrame* frames, size_t& size);
89800b99b8Sopenharmony_ci
90800b99b8Sopenharmony_ci    static int StepArkFrame(void *obj, OHOS::HiviewDFX::ReadMemFunc readMemFn,
91800b99b8Sopenharmony_ci        uintptr_t *fp, uintptr_t *sp, uintptr_t *pc, uintptr_t* methodid, bool *isJsFrame);
92800b99b8Sopenharmony_ci
93800b99b8Sopenharmony_ci    static int StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam* arkPrama);
94800b99b8Sopenharmony_ci
95800b99b8Sopenharmony_ci    static int JitCodeWriteFile(void* ctx, OHOS::HiviewDFX::ReadMemFunc readMemFn, int fd,
96800b99b8Sopenharmony_ci        const uintptr_t* const jitCodeArray, const size_t jitSize);
97800b99b8Sopenharmony_ci
98800b99b8Sopenharmony_ci    static int ParseArkFileInfo(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, const char* name,
99800b99b8Sopenharmony_ci        uintptr_t extractorPtr, JsFunction *jsFunction);
100800b99b8Sopenharmony_ci    static int ParseArkFrameInfoLocal(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, uintptr_t loadOffset,
101800b99b8Sopenharmony_ci        JsFunction *jsFunction);
102800b99b8Sopenharmony_ci    static int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
103800b99b8Sopenharmony_ci        uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
104800b99b8Sopenharmony_ci    static int ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t methodid, uintptr_t mapBase, uintptr_t loadOffset,
105800b99b8Sopenharmony_ci        uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction);
106800b99b8Sopenharmony_ci    static int TranslateArkFrameInfo(uint8_t *data, uint64_t dataSize, JsFunction *jsFunction);
107800b99b8Sopenharmony_ci
108800b99b8Sopenharmony_ci    static int ArkCreateJsSymbolExtractor(uintptr_t* extractorPtr);
109800b99b8Sopenharmony_ci    static int ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr);
110800b99b8Sopenharmony_ci
111800b99b8Sopenharmony_ci    static int ArkDestoryLocal();
112800b99b8Sopenharmony_ci};
113800b99b8Sopenharmony_ci} // namespace HiviewDFX
114800b99b8Sopenharmony_ci} // namespace OHOS
115800b99b8Sopenharmony_ci
116800b99b8Sopenharmony_ci#endif
117