1 /*
2 * Copyright (c) 2021 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 "dispatcher.h"
17
18 #include "agent/debugger_impl.h"
19 #include "agent/runtime_impl.h"
20 #ifdef ECMASCRIPT_SUPPORT_HEAPPROFILER
21 #include "agent/heapprofiler_impl.h"
22 #endif
23 #ifdef ECMASCRIPT_SUPPORT_CPUPROFILER
24 #include "agent/profiler_impl.h"
25 #endif
26 #include "agent/tracing_impl.h"
27 #include "protocol_channel.h"
28
29 namespace panda::ecmascript::tooling {
DispatchRequest(const std::string & message)30 DispatchRequest::DispatchRequest(const std::string &message)
31 {
32 std::unique_ptr<PtJson> json = PtJson::Parse(message);
33 if (json == nullptr) {
34 code_ = RequestCode::JSON_PARSE_ERROR;
35 LOG_DEBUGGER(ERROR) << "json parse error";
36 return;
37 }
38 if (!json->IsObject()) {
39 code_ = RequestCode::PARAMS_FORMAT_ERROR;
40 LOG_DEBUGGER(ERROR) << "json parse format error";
41 json->ReleaseRoot();
42 return;
43 }
44
45 Result ret;
46 int32_t callId;
47 ret = json->GetInt("id", &callId);
48 if (ret != Result::SUCCESS) {
49 code_ = RequestCode::PARSE_ID_ERROR;
50 LOG_DEBUGGER(ERROR) << "parse id error";
51 return;
52 }
53 callId_ = callId;
54
55 std::string wholeMethod;
56 ret = json->GetString("method", &wholeMethod);
57 if (ret != Result::SUCCESS) {
58 code_ = RequestCode::PARSE_METHOD_ERROR;
59 LOG_DEBUGGER(ERROR) << "parse method error";
60 return;
61 }
62 std::string::size_type length = wholeMethod.length();
63 std::string::size_type indexPoint;
64 indexPoint = wholeMethod.find_first_of('.', 0);
65 if (indexPoint == std::string::npos || indexPoint == 0 || indexPoint == length - 1) {
66 code_ = RequestCode::METHOD_FORMAT_ERROR;
67 LOG_DEBUGGER(ERROR) << "method format error: " << wholeMethod;
68 return;
69 }
70 domain_ = wholeMethod.substr(0, indexPoint);
71 method_ = wholeMethod.substr(indexPoint + 1, length);
72
73 LOG_DEBUGGER(DEBUG) << "id: " << callId_ << ", domain: " << domain_ << ", method: " << method_;
74
75 std::unique_ptr<PtJson> params;
76 ret = json->GetObject("params", ¶ms);
77 if (ret == Result::NOT_EXIST) {
78 return;
79 }
80 if (ret == Result::TYPE_ERROR) {
81 code_ = RequestCode::PARAMS_FORMAT_ERROR;
82 LOG_DEBUGGER(ERROR) << "params format error";
83 return;
84 }
85 params_ = std::move(params);
86 }
87
~DispatchRequest()88 DispatchRequest::~DispatchRequest()
89 {
90 params_->ReleaseRoot();
91 }
92
Create(ResponseCode code,const std::string & msg)93 DispatchResponse DispatchResponse::Create(ResponseCode code, const std::string &msg)
94 {
95 DispatchResponse response;
96 response.code_ = code;
97 response.errorMsg_ = msg;
98 return response;
99 }
100
Create(std::optional<std::string> error)101 DispatchResponse DispatchResponse::Create(std::optional<std::string> error)
102 {
103 DispatchResponse response;
104 if (error.has_value()) {
105 response.code_ = ResponseCode::NOK;
106 response.errorMsg_ = error.value();
107 }
108 return response;
109 }
110
Ok()111 DispatchResponse DispatchResponse::Ok()
112 {
113 return DispatchResponse();
114 }
115
Fail(const std::string & message)116 DispatchResponse DispatchResponse::Fail(const std::string &message)
117 {
118 DispatchResponse response;
119 response.code_ = ResponseCode::NOK;
120 response.errorMsg_ = message;
121 return response;
122 }
123
SendResponse(const DispatchRequest & request,const DispatchResponse & response,const PtBaseReturns & result)124 void DispatcherBase::SendResponse(const DispatchRequest &request, const DispatchResponse &response,
125 const PtBaseReturns &result)
126 {
127 if (channel_ != nullptr) {
128 channel_->SendResponse(request, response, result);
129 }
130 }
131
Dispatcher(const EcmaVM * vm,ProtocolChannel * channel)132 Dispatcher::Dispatcher(const EcmaVM *vm, ProtocolChannel *channel)
133 {
134 // profiler
135 #ifdef ECMASCRIPT_SUPPORT_CPUPROFILER
136 auto profiler = std::make_unique<ProfilerImpl>(vm, channel);
137 dispatchers_["Profiler"] =
138 std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(profiler));
139 #endif
140 #ifdef ECMASCRIPT_SUPPORT_HEAPPROFILER
141 auto heapProfiler = std::make_unique<HeapProfilerImpl>(vm, channel);
142 dispatchers_["HeapProfiler"] =
143 std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(heapProfiler));
144 #endif
145 auto tracing = std::make_unique<TracingImpl>(vm, channel);
146 dispatchers_["Tracing"] =
147 std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
148
149 // debugger
150 auto runtime = std::make_unique<RuntimeImpl>(vm, channel);
151 auto debugger = std::make_unique<DebuggerImpl>(vm, channel, runtime.get());
152 dispatchers_["Runtime"] =
153 std::make_unique<RuntimeImpl::DispatcherImpl>(channel, std::move(runtime));
154 dispatchers_["Debugger"] =
155 std::make_unique<DebuggerImpl::DispatcherImpl>(channel, std::move(debugger));
156 }
157
Dispatch(const DispatchRequest & request)158 void Dispatcher::Dispatch(const DispatchRequest &request)
159 {
160 if (!request.IsValid()) {
161 LOG_DEBUGGER(ERROR) << "Unknown request";
162 return;
163 }
164 const std::string &domain = request.GetDomain();
165 auto dispatcher = dispatchers_.find(domain);
166 if (dispatcher != dispatchers_.end()) {
167 dispatcher->second->Dispatch(request);
168 } else {
169 LOG_DEBUGGER(ERROR) << "unknown domain: " << domain;
170 }
171 }
172 } // namespace panda::ecmascript::tooling
173