1 /*
2 * Copyright (c) 2022 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 "agent/tracing_impl.h"
17
18 #include "base/pt_events.h"
19 #include "protocol_channel.h"
20
21 #include "ecmascript/napi/include/dfx_jsnapi.h"
22
23 namespace panda::ecmascript::tooling {
Dispatch(const DispatchRequest & request)24 void TracingImpl::DispatcherImpl::Dispatch(const DispatchRequest &request)
25 {
26 static std::unordered_map<std::string, AgentHandler> dispatcherTable {
27 { "end", &TracingImpl::DispatcherImpl::End },
28 { "getCategories", &TracingImpl::DispatcherImpl::GetCategories },
29 { "recordClockSyncMarker", &TracingImpl::DispatcherImpl::RecordClockSyncMarker },
30 { "requestMemoryDump", &TracingImpl::DispatcherImpl::RequestMemoryDump },
31 { "start", &TracingImpl::DispatcherImpl::Start }
32 };
33
34 const std::string &method = request.GetMethod();
35 LOG_DEBUGGER(DEBUG) << "dispatch [" << method << "] to TracingImpl";
36 auto entry = dispatcherTable.find(method);
37 if (entry != dispatcherTable.end() && entry->second != nullptr) {
38 (this->*(entry->second))(request);
39 } else {
40 SendResponse(request, DispatchResponse::Fail("Unknown method: " + method));
41 }
42 }
43
End(const DispatchRequest & request)44 void TracingImpl::DispatcherImpl::End(const DispatchRequest &request)
45 {
46 DispatchResponse response = tracing_->End();
47 SendResponse(request, response);
48 }
49
GetCategories(const DispatchRequest & request)50 void TracingImpl::DispatcherImpl::GetCategories(const DispatchRequest &request)
51 {
52 std::vector<std::string> categories;
53 DispatchResponse response = tracing_->GetCategories(categories);
54 SendResponse(request, response);
55 }
56
RecordClockSyncMarker(const DispatchRequest & request)57 void TracingImpl::DispatcherImpl::RecordClockSyncMarker(const DispatchRequest &request)
58 {
59 std::string syncId;
60 DispatchResponse response = tracing_->RecordClockSyncMarker(syncId);
61 SendResponse(request, response);
62 }
63
RequestMemoryDump(const DispatchRequest & request)64 void TracingImpl::DispatcherImpl::RequestMemoryDump(const DispatchRequest &request)
65 {
66 std::unique_ptr<RequestMemoryDumpParams> params =
67 RequestMemoryDumpParams::Create(request.GetParams());
68 std::string dumpGuid;
69 bool success = false;
70 DispatchResponse response = tracing_->RequestMemoryDump(std::move(params), dumpGuid, success);
71 SendResponse(request, response);
72 }
73
Start(const DispatchRequest & request)74 void TracingImpl::DispatcherImpl::Start(const DispatchRequest &request)
75 {
76 std::unique_ptr<StartParams> params =
77 StartParams::Create(request.GetParams());
78 DispatchResponse response = tracing_->Start(std::move(params));
79 SendResponse(request, response);
80 }
81
AllowNotify() const82 bool TracingImpl::Frontend::AllowNotify() const
83 {
84 return channel_ != nullptr;
85 }
86
BufferUsage()87 void TracingImpl::Frontend::BufferUsage()
88 {
89 if (!AllowNotify()) {
90 return;
91 }
92
93 tooling::BufferUsage bufferUsage;
94 channel_->SendNotification(bufferUsage);
95 }
96
DataCollected()97 void TracingImpl::Frontend::DataCollected()
98 {
99 if (!AllowNotify()) {
100 return;
101 }
102
103 tooling::DataCollected dataCollected;
104 channel_->SendNotification(dataCollected);
105 }
106
TracingComplete()107 void TracingImpl::Frontend::TracingComplete()
108 {
109 if (!AllowNotify()) {
110 return;
111 }
112
113 tooling::TracingComplete tracingComplete;
114 channel_->SendNotification(tracingComplete);
115 }
116
End()117 DispatchResponse TracingImpl::End()
118 {
119 return DispatchResponse::Fail("End not support now.");
120 }
121
GetCategories(std::vector<std::string> categories)122 DispatchResponse TracingImpl::GetCategories([[maybe_unused]] std::vector<std::string> categories)
123 {
124 return DispatchResponse::Fail("GetCategories not support now.");
125 }
126
RecordClockSyncMarker(std::string syncId)127 DispatchResponse TracingImpl::RecordClockSyncMarker([[maybe_unused]] std::string syncId)
128 {
129 return DispatchResponse::Fail("RecordClockSyncMarker not support now.");
130 }
131
RequestMemoryDump(std::unique_ptr<RequestMemoryDumpParams> params,std::string dumpGuid,bool success)132 DispatchResponse TracingImpl::RequestMemoryDump([[maybe_unused]] std::unique_ptr<RequestMemoryDumpParams> params,
133 [[maybe_unused]] std::string dumpGuid, [[maybe_unused]] bool success)
134 {
135 return DispatchResponse::Fail("RequestMemoryDump not support now.");
136 }
137
Start(std::unique_ptr<StartParams> params)138 DispatchResponse TracingImpl::Start([[maybe_unused]] std::unique_ptr<StartParams> params)
139 {
140 return DispatchResponse::Fail("Start not support now.");
141 }
142 } // namespace panda::ecmascript::tooling