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 <cstdint>
17 #include <dlfcn.h>
18 #include <optional>
19
20 #ifdef PANDA_TOOLING_ASIO
21 #include "connection/asio/asio_server.h"
22 #else
23 #include "connection/ohos_ws/ohos_ws_server.h"
24 #endif // PANDA_TOOLING_ASIO
25
26 #include "init.h"
27 #include "inspector.h"
28
29 #ifdef PANDA_TOOLING_ASIO
30 using InspectorWebSocketServer = ark::tooling::inspector::AsioServer;
31 #else
32 using InspectorWebSocketServer = ark::tooling::inspector::OhosWsServer;
33 #endif // PANDA_TOOLING_ASIO
34
35
36 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
37 std::optional<ark::tooling::inspector::Inspector> g_inspector;
38
39 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
40 static ark::Runtime::DebugSessionHandle g_debugSession;
41
42 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
43 static InspectorWebSocketServer g_server;
44
StartDebugger(uint32_t port,bool breakOnStart)45 int StartDebugger(uint32_t port, bool breakOnStart)
46 {
47 if (g_inspector) {
48 LOG(ERROR, DEBUGGER) << "Debugger has already been started";
49 return 1;
50 }
51 if (!g_server.Start(port)) {
52 LOG(ERROR, DEBUGGER) << "call g_server.Start failed";
53 return 1;
54 }
55 g_debugSession = ark::Runtime::GetCurrent()->StartDebugSession();
56 g_inspector.emplace(g_server, g_debugSession->GetDebugger(), breakOnStart);
57 return 0;
58 }
59
InitializeInspector(std::shared_ptr<void> endPoint,bool breakOnStart)60 void InitializeInspector(std::shared_ptr<void> endPoint, bool breakOnStart)
61 {
62 if (g_inspector) {
63 g_server.Stop();
64 }
65 g_server.InitEndPoint(endPoint);
66 g_debugSession = ark::Runtime::GetCurrent()->StartDebugSession();
67 if (!g_inspector) {
68 LOG(INFO, DEBUGGER) << "InitializeInspector started";
69 g_debugSession = ark::Runtime::GetCurrent()->StartDebugSession();
70 g_inspector.emplace(g_server, g_debugSession->GetDebugger(), breakOnStart);
71 }
72 }
73
StopInspector()74 void StopInspector()
75 {
76 if (!g_inspector) {
77 LOG(ERROR, DEBUGGER) << "Debugger has not been started";
78 return;
79 }
80
81 if (!g_server.Stop()) {
82 return;
83 }
84 g_inspector.reset();
85 g_debugSession.reset();
86 }
87
StopDebugger()88 int StopDebugger()
89 {
90 if (!g_inspector) {
91 LOG(ERROR, DEBUGGER) << "Debugger has not been started";
92 return 1;
93 }
94
95 if (!g_server.Stop()) {
96 return 1;
97 }
98 g_inspector.reset();
99 g_debugSession.reset();
100 return 0;
101 }
102
WaitForDebugger()103 void WaitForDebugger()
104 {
105 if (!g_inspector) {
106 LOG(ERROR, DEBUGGER) << "Debugger has not been started";
107 return;
108 }
109
110 g_inspector->WaitForDebugger();
111 }
112
HandleMessage(std::string && msg)113 void HandleMessage(std::string &&msg)
114 {
115 LOG(ERROR, DEBUGGER) << "Static OnMessage start";
116 g_inspector->Run(msg);
117 }