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 #include "js_runtime_common.h"
17
18 #include "connect_server_manager.h"
19 #include "hilog_tag_wrapper.h"
20 #include "js_environment.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 namespace {
25 constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so";
26 }
GetInstance()27 JsRuntimeCommon& JsRuntimeCommon::GetInstance()
28 {
29 static JsRuntimeCommon JsRuntimeCommon;
30 return JsRuntimeCommon;
31 }
32
JsRuntimeCommon()33 JsRuntimeCommon::JsRuntimeCommon() {}
34
~JsRuntimeCommon()35 JsRuntimeCommon::~JsRuntimeCommon() {}
36
IsDebugMode()37 bool JsRuntimeCommon::IsDebugMode()
38 {
39 return debugMode_.load();
40 }
41
IsDebugApp()42 bool JsRuntimeCommon::IsDebugApp()
43 {
44 return debugApp_.load();
45 }
46
IsNativeStart()47 bool JsRuntimeCommon::IsNativeStart()
48 {
49 return nativeStart_.load();
50 }
51
SetDebugMode(bool isDebugMode)52 void JsRuntimeCommon::SetDebugMode(bool isDebugMode)
53 {
54 debugMode_.store(isDebugMode);
55 }
56
SetDebugApp(bool isDebugApp)57 void JsRuntimeCommon::SetDebugApp(bool isDebugApp)
58 {
59 debugApp_.store(isDebugApp);
60 }
61
SetNativeStart(bool isNativeStart)62 void JsRuntimeCommon::SetNativeStart(bool isNativeStart)
63 {
64 nativeStart_.store(isNativeStart);
65 }
66
StartDebuggerModule(bool isDebugApp,bool isNativeStart)67 void JsRuntimeCommon::StartDebuggerModule(bool isDebugApp, bool isNativeStart)
68 {
69 debugMode_.store(true);
70 debugApp_.store(isDebugApp);
71 nativeStart_.store(isNativeStart);
72 }
73
StartDebugMode(NativeEngine * nativeEngine,const std::string & threadName)74 napi_status JsRuntimeCommon::StartDebugMode(NativeEngine* nativeEngine, const std::string& threadName)
75 {
76 if (nativeEngine == nullptr) {
77 TAG_LOGE(AAFwkTag::JSRUNTIME, "null nativeEngine");
78 return napi_status::napi_invalid_arg;
79 }
80 TAG_LOGI(AAFwkTag::JSRUNTIME, "debug mode is %{public}d, debug app is %{public}d", IsDebugMode(), IsDebugApp());
81 auto arkNativeEngine = static_cast<NativeEngine*>(nativeEngine);
82 auto instanceId = panda::DFXJSNApi::GetCurrentThreadId();
83 TAG_LOGI(AAFwkTag::JSRUNTIME, "Create instanceId is %{public}d", instanceId);
84 std::string instanceName = threadName + "_" + std::to_string(instanceId);
85 bool isAddInstance = ConnectServerManager::Get().AddInstance(instanceId, instanceId, instanceName);
86 if (IsNativeStart()) {
87 TAG_LOGE(AAFwkTag::JSRUNTIME, "native: true, set isAddInstance: false");
88 isAddInstance = false;
89 }
90 auto postTask = [nativeEngine](std::function<void()>&& callback) {
91 nativeEngine->CallDebuggerPostTaskFunc(std::move(callback));
92 };
93 panda::JSNApi::DebugOption debugOption = {ARK_DEBUGGER_LIB_PATH, isAddInstance};
94 auto vm = const_cast<EcmaVM*>(arkNativeEngine->GetEcmaVm());
95 ConnectServerManager::Get().StoreDebuggerInfo(
96 instanceId, reinterpret_cast<void*>(vm), debugOption, postTask, IsDebugApp());
97 panda::JSNApi::NotifyDebugMode(instanceId, vm, debugOption, instanceId, postTask, IsDebugApp());
98 return napi_status::napi_ok;
99 }
100
StopDebugMode(NativeEngine * nativeEngine)101 napi_status JsRuntimeCommon::StopDebugMode(NativeEngine* nativeEngine)
102 {
103 if (nativeEngine == nullptr) {
104 TAG_LOGE(AAFwkTag::JSRUNTIME, "null nativeEngine");
105 return napi_status::napi_invalid_arg;
106 }
107 auto instanceId = panda::DFXJSNApi::GetCurrentThreadId();
108 TAG_LOGI(AAFwkTag::JSRUNTIME, "destroy instanceId is %{public}d", instanceId);
109 ConnectServerManager::Get().RemoveInstance(instanceId);
110 auto arkNativeEngine = static_cast<NativeEngine*>(nativeEngine);
111 auto vm = const_cast<EcmaVM*>(arkNativeEngine->GetEcmaVm());
112 panda::JSNApi::StopDebugger(vm);
113 return napi_status::napi_ok;
114 }
115 } // namespace AbilityRuntime
116 } // namespace OHOS
117