• 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 #include "dfx_ark.h"
17 
18 #include <algorithm>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <dlfcn.h>
22 #include <pthread.h>
23 
24 #include "dfx_define.h"
25 #include "dfx_log.h"
26 #include "string_util.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 #undef LOG_DOMAIN
32 #undef LOG_TAG
33 #define LOG_DOMAIN 0xD002D11
34 #define LOG_TAG "DfxArk"
35 
36 constexpr auto ARK_LIB_NAME = "libark_jsruntime.so";
37 }
38 
GetLibArkHandle()39 bool DfxArk::GetLibArkHandle()
40 {
41     if (handle_ != nullptr) {
42         return true;
43     }
44     handle_ = dlopen(ARK_LIB_NAME, RTLD_LAZY);
45     if (handle_ == nullptr) {
46         DFXLOGU("Failed to load library(%{public}s).", dlerror());
47         return false;
48     }
49     return true;
50 }
51 
Instance()52 DfxArk& DfxArk::Instance()
53 {
54     static DfxArk instance;
55     return instance;
56 }
57 
DlsymArkFunc(const char * funcName,void ** dlsymFuncName)58 bool DfxArk::DlsymArkFunc(const char* funcName, void** dlsymFuncName)
59 {
60     if (dlsymFuncName == nullptr) {
61         return false;
62     }
63     if (*dlsymFuncName != nullptr) {
64         return true;
65     }
66     if (!GetLibArkHandle()) {
67         return false;
68     }
69     *dlsymFuncName = dlsym(handle_, (funcName));
70     if (*dlsymFuncName == nullptr) {
71         DFXLOGE("Failed to dlsym(%{public}s), error: %{public}s", funcName, dlerror());
72         return false;
73     }
74     return true;
75 }
76 
InitArkFunction(ArkFunction arkFunction)77 bool DfxArk::InitArkFunction(ArkFunction arkFunction)
78 {
79     std::unique_lock<std::mutex> lock(arkMutex_);
80     switch (arkFunction) {
81         case ArkFunction::ARK_CREATE_JS_SYMBOL_EXTRACTOR:
82             return DlsymArkFunc("ark_create_js_symbol_extractor",
83                 reinterpret_cast<void**>(&arkCreateJsSymbolExtractorFn_));
84         case ArkFunction::ARK_DESTORY_JS_SYMBOL_EXTRACTOR:
85             return DlsymArkFunc("ark_destory_js_symbol_extractor",
86                 reinterpret_cast<void**>(&arkDestoryJsSymbolExtractorFn_));
87         case ArkFunction::ARK_CREATE_LOCAL:
88             return DlsymArkFunc("ark_create_local", reinterpret_cast<void**>(&arkCreateLocalFn_));
89         case ArkFunction::ARK_DESTROY_LOCAL:
90             return DlsymArkFunc("ark_destroy_local", reinterpret_cast<void**>(&arkDestroyLocalFn_));
91         case ArkFunction::ARK_PARSE_JS_FILE_INFO:
92             return DlsymArkFunc("ark_parse_js_file_info", reinterpret_cast<void**>(&parseArkFileInfoFn_));
93         case ArkFunction::ARK_PARSE_JS_FRAME_INFO_LOCAL:
94             return DlsymArkFunc("ark_parse_js_frame_info_local", reinterpret_cast<void**>(&parseArkFrameInfoLocalFn_));
95         case ArkFunction::ARK_PARSE_JS_FRAME_INFO:
96             return DlsymArkFunc("ark_parse_js_frame_info", reinterpret_cast<void**>(&parseArkFrameInfoFn_));
97         case ArkFunction::STEP_ARK:
98             return DlsymArkFunc("step_ark", reinterpret_cast<void**>(&stepArkFn_));
99         case ArkFunction::STEP_ARK_WITH_RECORD_JIT:
100             return DlsymArkFunc("step_ark_with_record_jit", reinterpret_cast<void**>(&stepArkWithJitFn_));
101         case ArkFunction::ARK_WRITE_JIT_CODE:
102             return DlsymArkFunc("ark_write_jit_code", reinterpret_cast<void**>(&jitCodeWriteFileFn_));
103         default:
104             return false;
105     }
106 }
107 
ArkCreateJsSymbolExtractor(uintptr_t * extractorPtr)108 int DfxArk::ArkCreateJsSymbolExtractor(uintptr_t* extractorPtr)
109 {
110     if (arkCreateJsSymbolExtractorFn_ != nullptr || InitArkFunction(ArkFunction::ARK_CREATE_JS_SYMBOL_EXTRACTOR)) {
111         return arkCreateJsSymbolExtractorFn_(extractorPtr);
112     }
113     return -1;
114 }
115 
ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr)116 int DfxArk::ArkDestoryJsSymbolExtractor(uintptr_t extractorPtr)
117 {
118     if (arkDestoryJsSymbolExtractorFn_ != nullptr || InitArkFunction(ArkFunction::ARK_DESTORY_JS_SYMBOL_EXTRACTOR)) {
119         return arkDestoryJsSymbolExtractorFn_(extractorPtr);
120     }
121     return -1;
122 }
123 
ArkCreateLocal()124 int DfxArk::ArkCreateLocal()
125 {
126     if (arkCreateLocalFn_ != nullptr || InitArkFunction(ArkFunction::ARK_CREATE_LOCAL)) {
127         return arkCreateLocalFn_();
128     }
129     return -1;
130 }
131 
ArkDestroyLocal()132 int DfxArk::ArkDestroyLocal()
133 {
134     if (arkDestroyLocalFn_ != nullptr || InitArkFunction(ArkFunction::ARK_DESTROY_LOCAL)) {
135         return arkDestroyLocalFn_();
136     }
137     return -1;
138 }
139 
ParseArkFileInfo(uintptr_t byteCodePc,uintptr_t mapBase,const char * name,uintptr_t extractorPtr,JsFunction * jsFunction)140 int DfxArk::ParseArkFileInfo(uintptr_t byteCodePc, uintptr_t mapBase, const char* name,
141     uintptr_t extractorPtr, JsFunction *jsFunction)
142 {
143     if (parseArkFileInfoFn_ != nullptr || InitArkFunction(ArkFunction::ARK_PARSE_JS_FILE_INFO)) {
144         return parseArkFileInfoFn_(byteCodePc, mapBase, name, extractorPtr, jsFunction);
145     }
146     return -1;
147 }
148 
ParseArkFrameInfoLocal(uintptr_t byteCodePc,uintptr_t mapBase,uintptr_t offset,JsFunction * jsFunction)149 int DfxArk::ParseArkFrameInfoLocal(uintptr_t byteCodePc, uintptr_t mapBase,
150     uintptr_t offset, JsFunction *jsFunction)
151 {
152     if (parseArkFrameInfoLocalFn_ != nullptr || InitArkFunction(ArkFunction::ARK_PARSE_JS_FRAME_INFO_LOCAL)) {
153         return parseArkFrameInfoLocalFn_(byteCodePc, mapBase, offset, jsFunction);
154     }
155     return -1;
156 }
157 
ParseArkFrameInfo(uintptr_t byteCodePc,uintptr_t mapBase,uintptr_t loadOffset,uint8_t * data,uint64_t dataSize,uintptr_t extractorPtr,JsFunction * jsFunction)158 int DfxArk::ParseArkFrameInfo(uintptr_t byteCodePc, uintptr_t mapBase, uintptr_t loadOffset,
159     uint8_t *data, uint64_t dataSize, uintptr_t extractorPtr, JsFunction *jsFunction)
160 {
161     if (parseArkFrameInfoFn_ != nullptr || InitArkFunction(ArkFunction::ARK_PARSE_JS_FRAME_INFO)) {
162         return parseArkFrameInfoFn_(byteCodePc, mapBase, loadOffset, data, dataSize,
163             extractorPtr, jsFunction);
164     }
165     return -1;
166 }
167 
StepArkFrame(void * obj,OHOS::HiviewDFX::ReadMemFunc readMemFn,OHOS::HiviewDFX::ArkStepParam * arkParam)168 int DfxArk::StepArkFrame(void *obj, OHOS::HiviewDFX::ReadMemFunc readMemFn,
169     OHOS::HiviewDFX::ArkStepParam* arkParam)
170 {
171     if (stepArkFn_ != nullptr || InitArkFunction(ArkFunction::STEP_ARK)) {
172         return stepArkFn_(obj, readMemFn, arkParam);
173     }
174     return -1;
175 }
176 
StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam * arkParam)177 int DfxArk::StepArkFrameWithJit(OHOS::HiviewDFX::ArkUnwindParam* arkParam)
178 {
179     if (stepArkWithJitFn_ != nullptr || InitArkFunction(ArkFunction::STEP_ARK_WITH_RECORD_JIT)) {
180         return stepArkWithJitFn_(arkParam);
181     }
182     return -1;
183 }
184 
JitCodeWriteFile(void * ctx,OHOS::HiviewDFX::ReadMemFunc readMemFn,int fd,const uintptr_t * const jitCodeArray,const size_t jitSize)185 int DfxArk::JitCodeWriteFile(void* ctx, OHOS::HiviewDFX::ReadMemFunc readMemFn, int fd,
186     const uintptr_t* const jitCodeArray, const size_t jitSize)
187 {
188     if (jitCodeWriteFileFn_ != nullptr || InitArkFunction(ArkFunction::ARK_WRITE_JIT_CODE)) {
189         return jitCodeWriteFileFn_(ctx, readMemFn, fd, jitCodeArray, jitSize);
190     }
191     return -1;
192 }
193 } // namespace HiviewDFX
194 } // namespace OHOS
195