• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "connect_inspector.h"
17 #include <mutex>
18 #include "common/log_wrapper.h"
19 namespace OHOS::ArkCompiler::Toolchain {
20 std::mutex g_connectMutex;
21 std::unique_ptr<ConnectInspector> g_inspector = nullptr;
22 static constexpr char CONNECTED_MESSAGE[] = "connected";
23 static constexpr char OPEN_MESSAGE[] = "layoutOpen";
24 static constexpr char CLOSE_MESSAGE[] = "layoutClose";
25 static constexpr char REQUEST_MESSAGE[] = "tree";
26 static constexpr char STOPDEBUGGER_MESSAGE[] = "stopDebugger";
27 std::function<void(bool)> g_SetConnectCallBack;
28 
HandleDebugManager(void * const server)29 void* HandleDebugManager(void* const server)
30 {
31     if (server == nullptr) {
32         LOGE("HandleDebugManager server nullptr");
33         return nullptr;
34     }
35 #if defined(IOS_PLATFORM) || defined(MAC_PLATFORM)
36     pthread_setname_np("OS_DbgConnThread");
37 #else
38     pthread_setname_np(pthread_self(), "OS_DbgConnThread");
39 #endif
40 
41     static_cast<ConnectServer*>(server)->RunServer();
42     return nullptr;
43 }
44 
OnMessage(const std::string & message)45 void OnMessage(const std::string& message)
46 {
47     std::lock_guard<std::mutex> lock(g_connectMutex);
48     if (message.empty()) {
49         LOGE("message is empty");
50         return;
51     }
52 
53     LOGI("ConnectServer OnMessage: %{public}s", message.c_str());
54     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
55         g_inspector->ideMsgQueue_.push(message);
56         if (message.find(CONNECTED_MESSAGE, 0) != std::string::npos) {
57             g_inspector->waitingForDebugger_ = false;
58             if (g_SetConnectCallBack != nullptr) {
59                 g_SetConnectCallBack(true);
60             }
61             for (auto& info : g_inspector->infoBuffer_) {
62                 g_inspector->connectServer_->SendMessage(info.second);
63             }
64         }
65         if (message.find(OPEN_MESSAGE, 0) != std::string::npos) {
66             if (g_inspector->setSwitchStatus_ != nullptr) {
67                 LOGI("layoutOpen start");
68                 g_inspector->setSwitchStatus_(true);
69             }
70         }
71         if (message.find(CLOSE_MESSAGE, 0) != std::string::npos) {
72             if (g_SetConnectCallBack != nullptr) {
73                 g_SetConnectCallBack(false);
74             }
75             if (g_inspector->setSwitchStatus_ != nullptr) {
76                 LOGI("layoutClose start");
77                 g_inspector->setSwitchStatus_(false);
78             }
79         }
80         if (message.find(REQUEST_MESSAGE, 0) != std::string::npos) {
81             if (g_inspector->createLayoutInfo_ != nullptr) {
82                 LOGI("tree start");
83                 g_inspector->createLayoutInfo_(g_inspector->instanceId_);
84             }
85         }
86         if (message.find(STOPDEBUGGER_MESSAGE, 0) != std::string::npos) {
87             g_inspector->waitingForDebugger_ = true;
88             if (g_inspector->setDebugMode_ != nullptr) {
89                 LOGI("stopDebugger start");
90                 g_inspector->setDebugMode_();
91             }
92         }
93     }
94 }
95 
SetSwitchCallBack(const std::function<void (bool)> & setSwitchStatus,const std::function<void (int32_t)> & createLayoutInfo,int32_t instanceId)96 void SetSwitchCallBack(const std::function<void(bool)>& setSwitchStatus,
97     const std::function<void(int32_t)>& createLayoutInfo, int32_t instanceId)
98 {
99     std::lock_guard<std::mutex> lock(g_connectMutex);
100     if (g_inspector != nullptr) {
101         g_inspector->setSwitchStatus_ = setSwitchStatus;
102         g_inspector->createLayoutInfo_ = createLayoutInfo;
103         g_inspector->instanceId_ = instanceId;
104     }
105 }
106 
SetConnectCallback(const std::function<void (bool)> & callback)107 void SetConnectCallback(const std::function<void(bool)>& callback)
108 {
109     g_SetConnectCallBack = callback;
110 }
111 
112 // stop debugger but the application continues to run
SetDebugModeCallBack(const std::function<void ()> & setDebugMode)113 void SetDebugModeCallBack(const std::function<void()>& setDebugMode)
114 {
115     std::lock_guard<std::mutex> lock(g_connectMutex);
116     if (g_inspector != nullptr) {
117         g_inspector->setDebugMode_ = setDebugMode;
118     }
119 }
120 
ResetService()121 void ResetService()
122 {
123     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
124         g_inspector->connectServer_->StopServer();
125         g_inspector->connectServer_.reset();
126     }
127 }
128 
StartServerForSocketPair(int socketfd)129 void StartServerForSocketPair(int socketfd)
130 {
131     LOGI("StartServerForSocketPair, socketfd = %{private}d", socketfd);
132     if (g_inspector == nullptr) {
133         g_inspector = std::make_unique<ConnectInspector>();
134     }
135     g_inspector->connectServer_ = std::make_unique<ConnectServer>(socketfd,
136         std::bind(&OnMessage, std::placeholders::_1));
137 
138     pthread_t tid;
139     if (pthread_create(&tid, nullptr, &HandleDebugManager,
140         static_cast<void*>(g_inspector->connectServer_.get())) != 0) {
141         LOGE("pthread_create fail!");
142         ResetService();
143         return;
144     }
145 }
146 
StartServer(const std::string & componentName)147 void StartServer(const std::string& componentName)
148 {
149     LOGI("StartServer, componentName = %{private}s", componentName.c_str());
150     g_inspector = std::make_unique<ConnectInspector>();
151     g_inspector->connectServer_ = std::make_unique<ConnectServer>(componentName,
152         std::bind(&OnMessage, std::placeholders::_1));
153 
154     pthread_t tid;
155     if (pthread_create(&tid, nullptr, &HandleDebugManager,
156         static_cast<void*>(g_inspector->connectServer_.get())) != 0) {
157         LOGE("pthread_create fail!");
158         ResetService();
159         return;
160     }
161 }
162 
StopServer(const std::string & componentName)163 void StopServer([[maybe_unused]] const std::string& componentName)
164 {
165     LOGI("StopServer, componentName = %{private}s", componentName.c_str());
166     ResetService();
167 }
168 
StoreMessage(int32_t instanceId,const std::string & message)169 void StoreMessage(int32_t instanceId, const std::string& message)
170 {
171     std::lock_guard<std::mutex> lock(g_connectMutex);
172     if (g_inspector == nullptr) {
173         g_inspector = std::make_unique<ConnectInspector>();
174     }
175     if (g_inspector->infoBuffer_.count(instanceId) == 1) {
176         LOGE("The message with the current instance id has existed.");
177         return;
178     }
179     g_inspector->infoBuffer_[instanceId] = message;
180 }
181 
StoreInspectorInfo(const std::string & jsonTreeStr,const std::string & jsonSnapshotStr)182 void StoreInspectorInfo(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr)
183 {
184     std::lock_guard<std::mutex> lock(g_connectMutex);
185     g_inspector->layoutInspectorInfo_.tree = jsonTreeStr;
186     g_inspector->layoutInspectorInfo_.snapShot = jsonSnapshotStr;
187 }
188 
RemoveMessage(int32_t instanceId)189 void RemoveMessage(int32_t instanceId)
190 {
191     std::lock_guard<std::mutex> lock(g_connectMutex);
192     if (g_inspector->infoBuffer_.count(instanceId) != 1) {
193         LOGE("The message with the current instance id does not exist.");
194         return;
195     }
196     g_inspector->infoBuffer_.erase(instanceId);
197 }
198 
SendLayoutMessage(const std::string & message)199 void SendLayoutMessage(const std::string& message)
200 {
201     LOGI("SendLayoutMessage start to send message");
202     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
203         g_inspector->connectServer_->SendMessage(message);
204     }
205 }
206 
SendMessage(const std::string & message)207 void SendMessage(const std::string& message)
208 {
209     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr && !g_inspector->waitingForDebugger_) {
210         g_inspector->connectServer_->SendMessage(message);
211     }
212 }
213 
WaitForConnection()214 bool WaitForConnection()
215 {
216     if (g_inspector == nullptr) {
217         return true;
218     }
219     return g_inspector->waitingForDebugger_;
220 }
221 } // OHOS::ArkCompiler::Toolchain
222