• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_JSVM_H
17 #define DFX_JSVM_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <securec.h>
22 #include <string>
23 #include <vector>
24 
25 namespace jsvm {
26 constexpr uint16_t FUNCTION_NAME_MAX = 1024;
27 
28 using ReadMemFunc = bool (*)(void *, uintptr_t, uintptr_t *);
29 
30 struct JsvmFunction {
31     char functionName[FUNCTION_NAME_MAX];
32 };
33 
34 struct JsvmStepParam {
35     uintptr_t *fp;
36     uintptr_t *sp;
37     uintptr_t *pc;
38     bool *isJsvmFrame;
39 
JsvmStepParamJsvmStepParam40     JsvmStepParam(uintptr_t *fp, uintptr_t *sp, uintptr_t *pc, bool *isJsvmFrame)
41         : fp(fp), sp(sp), pc(pc), isJsvmFrame(isJsvmFrame) {}
42 };
43 }
44 
45 namespace OHOS {
46 namespace HiviewDFX {
47 using JsvmFunction = jsvm::JsvmFunction;
48 using ReadMemFunc = jsvm::ReadMemFunc;
49 using JsvmStepParam = jsvm::JsvmStepParam;
50 
51 class DfxJsvm {
52 public:
53     static DfxJsvm& Instance();
54     /**
55      * @brief step Jsvm frame
56      *
57      * @param obj memory pointer object
58      * @param readMemFn read memory function
59      * @param jsvmParam praram of step jsvm
60      * @return if succeed return 1, otherwise return -1
61     */
62     int StepJsvmFrame(void *obj, ReadMemFunc readMemFn, JsvmStepParam* jsvmParam);
63     /**
64      * @brief Parse Jsvm Frame Info, for hiperf/hiProfile
65      *
66      * @param pc program counter
67      * @param extractorPtr extractorPtr from JsvmCreateJsSymbolExtractor
68      * @param JsvmFunction jsvmFunction variable
69      * @return if succeed return 1, otherwise return -1
70     */
71     int ParseJsvmFrameInfo(uintptr_t pc, uintptr_t extractorPtr, JsvmFunction *jsvmFunction);
72 
73     /**
74      * @brief create Jsvm js symbol extracrot
75      *
76      * @param extractorPtr extractorPtr variable
77      * @param pid dump target process pid
78      * @return if succeed return 1, otherwise return -1
79     */
80     int JsvmCreateJsSymbolExtractor(uintptr_t* extractorPtr, uint32_t pid);
81     /**
82      * @brief destroy Jsvm js symbol extracrot
83      *
84      * @param extractorPtr extractorPtr from JsvmCreateJsSymbolExtractor
85      * @return if succeed return 1, otherwise return -1
86     */
87     int JsvmDestroyJsSymbolExtractor(uintptr_t extractorPtr);
88 
89 private:
90     DfxJsvm() = default;
91     ~DfxJsvm() = default;
92     DfxJsvm(const DfxJsvm&) = delete;
93     DfxJsvm& operator=(const DfxJsvm&) = delete;
94     bool GetLibJsvmHandle(void);
95     template <typename FuncName>
96     void DlsymJsvmFunc(const char* funcName, FuncName& dlsymFuncName);
97     void* handle_ = nullptr;
98     using StepJsvmFn = int (*)(void*, ReadMemFunc, JsvmStepParam*);
99     using ParseJsvmFrameInfoFn = int (*)(uintptr_t, uintptr_t, JsvmFunction*);
100     using JsvmCreateJsSymbolExtractorFn = int (*)(uintptr_t*, uint32_t);
101     using JsvmDestroyJsSymbolExtractorFn = int (*)(uintptr_t);
102     std::mutex mutex_;
103     StepJsvmFn stepJsvmFn_ = nullptr;
104     ParseJsvmFrameInfoFn parseJsvmFrameInfoFn_ = nullptr;
105     JsvmCreateJsSymbolExtractorFn jsvmCreateJsSymbolExtractorFn_ = nullptr;
106     JsvmDestroyJsSymbolExtractorFn jsvmDestroyJsSymbolExtractorFn_ = nullptr;
107 };
108 } // namespace HiviewDFX
109 } // namespace OHOS
110 
111 #endif
112