• 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 "tooling/dynamic/client/session/session.h"
17 
18 #include "tooling/dynamic/client/manager/message_manager.h"
19 
20 using PtJson = panda::ecmascript::tooling::PtJson;
21 using Result = panda::ecmascript::tooling::Result;
22 namespace OHOS::ArkCompiler::Toolchain {
23 uv_async_t* g_socketSignal;
SocketMessageThread(void * arg)24 void SocketMessageThread(void *arg)
25 {
26     int sessionId = *(uint32_t *)arg;
27     Session *session = SessionManager::getInstance().GetSessionById(sessionId);
28 
29     session->SocketMessageLoop();
30 }
31 
Session(uint32_t sessionId,std::string & sockInfo)32 Session::Session(uint32_t sessionId, std::string& sockInfo)
33     : sessionId_(sessionId), sockInfo_(sockInfo), domainManager_(sessionId), breakpoint_(sessionId),
34       stackManager_(sessionId), variableManager_(sessionId), sourceManager_(sessionId), watchManager_(sessionId)
35 {
36 }
37 
SocketMessageLoop()38 void Session::SocketMessageLoop()
39 {
40     while (cliSocket_.IsConnected()) {
41         std::string decMessage = cliSocket_.Decode();
42         uint32_t len = decMessage.length();
43         if (len == 0) {
44             continue;
45         }
46         LOGI("arkdb [%{public}u] message = %{public}s", sessionId_, decMessage.c_str());
47 
48         MessageManager::getInstance().MessagePush(sessionId_, decMessage);
49 
50         if (uv_is_active(reinterpret_cast<uv_handle_t*>(g_socketSignal))) {
51             uv_async_send(g_socketSignal);
52         }
53     }
54 }
55 
CreateSocket()56 int Session::CreateSocket()
57 {
58     uint32_t port = 0;
59     if (Utils::StrToUInt(sockInfo_.c_str(), &port)) {
60         if ((port <= 0) || (port >= 65535)) { // 65535: max port
61             LOGE("arkdb:InitToolchainWebSocketForPort the port = %{public}d is wrong.", port);
62             return -1;
63         }
64         if (!cliSocket_.InitToolchainWebSocketForPort(port, 5)) { // 5: five times
65             LOGE("arkdb:InitToolchainWebSocketForPort failed");
66             return -1;
67         }
68     } else {
69         if (!cliSocket_.InitToolchainWebSocketForSockName(sockInfo_)) {
70             LOGE("arkdb:InitToolchainWebSocketForSockName failed");
71             return -1;
72         }
73     }
74 
75     if (!cliSocket_.ClientSendWSUpgradeReq()) {
76         LOGE("arkdb:ClientSendWSUpgradeReq failed");
77         return -1;
78     }
79     if (!cliSocket_.ClientRecvWSUpgradeRsp()) {
80         LOGE("arkdb:ClientRecvWSUpgradeRsp failed");
81         return -1;
82     }
83 
84     return 0;
85 }
86 
Start()87 int Session::Start()
88 {
89     if (CreateSocket()) {
90         return -1;
91     }
92 
93     uv_thread_create(&socketTid_, SocketMessageThread, &sessionId_);
94     return 0;
95 }
96 
Stop()97 int Session::Stop()
98 {
99     cliSocket_.Close();
100     return 0;
101 }
102 
CreateSessionById(uint32_t sessionId,std::string & sockInfo)103 int SessionManager::CreateSessionById(uint32_t sessionId, std::string& sockInfo)
104 {
105     sessions_[sessionId] = std::make_unique<Session>(sessionId, sockInfo);
106     if (sessions_[sessionId]->Start()) {
107         sessions_[sessionId] = nullptr;
108         return -1;
109     }
110 
111     return 0;
112 }
113 
CreateNewSession(std::string & sockInfo)114 int SessionManager::CreateNewSession(std::string& sockInfo)
115 {
116     uint32_t sessionId = MAX_SESSION_NUM;
117     for (uint32_t i = 0; i < MAX_SESSION_NUM; ++i) {
118         if (sessions_[i] == nullptr) {
119             if (sessionId == MAX_SESSION_NUM) {
120                 sessionId = i;
121             }
122             continue;
123         }
124         if (sessions_[i]->GetSockInfo() == sockInfo) {
125             return -1;
126         }
127     }
128 
129     if (sessionId < MAX_SESSION_NUM) {
130         return CreateSessionById(sessionId, sockInfo);
131     }
132 
133     return -1;
134 }
135 
CreateDefaultSession(std::string & sockInfo)136 int SessionManager::CreateDefaultSession(std::string& sockInfo)
137 {
138     return CreateSessionById(0, sockInfo);
139 }
140 
DelSessionById(uint32_t sessionId)141 int SessionManager::DelSessionById(uint32_t sessionId)
142 {
143     Session *session = GetSessionById(sessionId);
144     if (session == nullptr) {
145         return -1;
146     }
147     session->Stop();
148     sessions_[sessionId] = nullptr;
149 
150     if (sessionId == currentSessionId_) {
151         currentSessionId_ = 0;
152         std::cout << "session switch to 0" << std::endl;
153     }
154 
155     return 0;
156 }
157 
SessionList()158 int SessionManager::SessionList()
159 {
160     for (uint32_t i = 0; i < MAX_SESSION_NUM; ++i) {
161         if (sessions_[i] != nullptr) {
162             std::string flag = (i == currentSessionId_) ? "* " : "  ";
163             std::string sockState = sessions_[i]->GetSocketStateString();
164             std::cout << flag << i << ": ";
165             std::cout << std::setw(32) << std::left << sessions_[i]->GetSockInfo(); // 32: max length of socket info
166             std::cout << sockState << std::endl;
167         }
168     }
169     return 0;
170 }
171 
SessionSwitch(uint32_t sessionId)172 int SessionManager::SessionSwitch(uint32_t sessionId)
173 {
174     Session *session = GetSessionById(sessionId);
175     if (session == nullptr) {
176         return -1;
177     }
178     currentSessionId_ = sessionId;
179     return 0;
180 }
181 
CmdForAllSessions(CmdForAllCB callback)182 void SessionManager::CmdForAllSessions(CmdForAllCB callback)
183 {
184     for (uint32_t sessionId = 0; sessionId < MAX_SESSION_NUM; ++sessionId) {
185         if (sessions_[sessionId] != nullptr) {
186             std::cout << "Executing command in session " << sessionId << ":" << std::endl;
187             callback(sessionId);
188         }
189     }
190 }
191 
CreateTestSession(std::string & sockInfo)192 int SessionManager::CreateTestSession(std::string& sockInfo)
193 {
194     uint32_t sessionId = 0;
195     sessions_[sessionId] = std::make_unique<Session>(sessionId, sockInfo);
196     if (sessions_[sessionId]->CreateSocket()) {
197         sessions_[sessionId] = nullptr;
198         return -1;
199     }
200 
201     return 0;
202 }
203 }