• 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 
17 #include <functional>
18 #include <string>
19 
20 #include "library_loader.h"
21 #include "common/log_wrapper.h"
22 #include "init_static.h"
23 
24 #if defined(UNIX_PLATFORM)
25 #include <dlfcn.h>
26 #elif defined(WINDOWS_PLATFORM)
27 #include <windows.h>
28 #ifdef ERROR
29 #undef ERROR
30 #endif
31 #else
32 #error "Unsupported platform"
33 #endif
34 
35 namespace OHOS::ArkCompiler::Toolchain {
36 
37 
38 using InitializeInspectorFunc = void(*)(std::shared_ptr<void>, bool);
39 using HandleMessageFunc = void(*)(std::string&&);
40 using StopInspectorFunc = void(*)();
41 using WaitForDebuggerFunc = void(*)();
42 using StartDebuggerFunc = void(*)(uint32_t, bool);
43 using StopDebuggerFunc = void(*)();
44 
45 static void* g_debuggerHandle = nullptr;
46 
47 static InitializeInspectorFunc g_initializeInspectorForStatic = nullptr;
48 static HandleMessageFunc g_handleMessageForStatic = nullptr;
49 static StopInspectorFunc g_stopInspectorForStatic = nullptr;
50 static WaitForDebuggerFunc g_waitForDebuggerForStatic = nullptr;
51 static StartDebuggerFunc g_startDebuggerForStatic = nullptr;
52 static StopDebuggerFunc g_stopDebuggerForStatic = nullptr;
53 
InitializeArkFunctionsForStatic()54 bool InitializeArkFunctionsForStatic()
55 {
56     if (g_debuggerHandle) {
57         return true;
58     }
59     g_debuggerHandle = Load("libarkinspector.so");
60     if (g_debuggerHandle == nullptr) {
61         return false;
62     }
63     g_initializeInspectorForStatic = reinterpret_cast<InitializeInspectorFunc>(
64         ResolveSymbol(g_debuggerHandle, "InitializeInspector"));
65     if (g_initializeInspectorForStatic == nullptr) {
66         return false;
67     }
68     g_startDebuggerForStatic = reinterpret_cast<StartDebuggerFunc>(
69         ResolveSymbol(g_debuggerHandle, "StartDebugger"));
70     if (g_startDebuggerForStatic == nullptr) {
71         return false;
72     }
73     g_stopDebuggerForStatic = reinterpret_cast<StopDebuggerFunc>(
74         ResolveSymbol(g_debuggerHandle, "StopDebugger"));
75     if (g_stopDebuggerForStatic == nullptr) {
76         return false;
77     }
78     g_handleMessageForStatic = reinterpret_cast<HandleMessageFunc>(
79         ResolveSymbol(g_debuggerHandle, "HandleMessage"));
80     if (g_handleMessageForStatic == nullptr) {
81         return false;
82     }
83     g_stopInspectorForStatic = reinterpret_cast<StopInspectorFunc>(
84         ResolveSymbol(g_debuggerHandle, "StopInspector"));
85     if (g_stopInspectorForStatic == nullptr) {
86         return false;
87     }
88     g_waitForDebuggerForStatic = reinterpret_cast<WaitForDebuggerFunc>(
89         ResolveSymbol(g_debuggerHandle, "WaitForDebugger"));
90     if (g_waitForDebuggerForStatic == nullptr) {
91         return false;
92     }
93     return true;
94 }
95 
HandleMessage(std::string && message)96 void HandleMessage(std::string&& message)
97 {
98     g_handleMessageForStatic(std::move(message));
99 }
100 
StopDebuggerForStatic()101 int StopDebuggerForStatic()
102 {
103     g_stopInspectorForStatic();
104     return 0;
105 }
106 
StartDebuggerForStatic(std::shared_ptr<void> endpoint,bool breakOnStart)107 bool StartDebuggerForStatic(std::shared_ptr<void> endpoint, bool breakOnStart)
108 {
109     if (endpoint == nullptr) {
110         LOGE("StartDebuggerForStatic Endpoint == nullptr");
111     }
112     g_initializeInspectorForStatic(endpoint, breakOnStart);
113     return true;
114 }
WaitForDebuggerForStatic()115 void WaitForDebuggerForStatic()
116 {
117     g_waitForDebuggerForStatic();
118 }
119 
StartDebuggerInitForStatic(uint32_t port,bool breakOnStart)120 int StartDebuggerInitForStatic(uint32_t port, bool breakOnStart)
121 {
122     if (!InitializeArkFunctionsForStatic()) {
123         LOGE("StartDebuggerInitForStatic Error");
124     }
125     g_startDebuggerForStatic(port, breakOnStart);
126     return 1;
127 }
128 
StopDebuggerInitForStatic()129 int StopDebuggerInitForStatic()
130 {
131     if (!InitializeArkFunctionsForStatic()) {
132         LOGE("StopDebuggerInitForStatic Error");
133     }
134     g_stopDebuggerForStatic();
135     return 1;
136 }
137 }
138