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 "parse_plugin_config.h"
17
18 #include "cpu_plugin_config_standard.pb.h"
19 #include "diskio_plugin_config_standard.pb.h"
20 #include "hidump_plugin_config_standard.pb.h"
21 #include "hiebpf_plugin_config_standard.pb.h"
22 #include "hilog_plugin_config_standard.pb.h"
23 #include "hiperf_plugin_config_standard.pb.h"
24 #include "hisysevent_plugin_config_standard.pb.h"
25 #include "arkts_plugin_config_standard.pb.h"
26 #include "memory_plugin_common_standard.pb.h"
27 #include "memory_plugin_config_standard.pb.h"
28 #include "native_hook_config_standard.pb.h"
29 #include "network_plugin_config_standard.pb.h"
30 #include "process_plugin_config_standard.pb.h"
31 #include "trace_plugin_config_standard.pb.h"
32
33 namespace {
34 constexpr int REMAINDER = 2;
35 }
36
ParsePluginConfig()37 ParsePluginConfig::ParsePluginConfig()
38 {
39 parser_.AllowUnknownField(true);
40 }
41
GetInstance()42 ParsePluginConfig& ParsePluginConfig::GetInstance()
43 {
44 static ParsePluginConfig parsePluginConfig;
45 return parsePluginConfig;
46 }
47
GetPluginsConfig(std::string & content)48 std::string ParsePluginConfig::GetPluginsConfig(std::string& content)
49 {
50 std::string pluginConfig = "";
51 std::string pluginName = "";
52 size_t beginPos = 0;
53 size_t endPos = 0;
54 for (int i = 0; content.size() > 0; i++) {
55 // 先获取pluginName,再获取configData
56 std::string destStr = (i % REMAINDER) ? "config_data" : "plugin_name";
57 beginPos = content.find(destStr);
58 if (beginPos == std::string::npos) {
59 break;
60 }
61 pluginConfig += content.substr(0, beginPos);
62 content = content.substr(beginPos + destStr.size(), content.size());
63 destStr = (i % REMAINDER) ? "{" : "\"";
64 beginPos = content.find(destStr);
65 if (beginPos == std::string::npos) {
66 break;
67 }
68 content = content.substr(beginPos + 1, content.size());
69 destStr = (i % REMAINDER) ? "}" : "\"";
70 endPos = content.find(destStr);
71 if (endPos == std::string::npos) {
72 break;
73 }
74 std::string contentStr = content.substr(0, endPos);
75 if (i % REMAINDER == 0) { // set plugin-name
76 pluginName = contentStr;
77
78 if (pluginName == "") {
79 return "";
80 }
81 pluginConfig += "name: \"" + pluginName + "\"";
82 } else { // save config_data
83 pluginConfigMap.insert({pluginName, contentStr});
84 pluginConfig += "config_data: \"\"";
85 }
86
87 content = content.substr(endPos + 1, content.size());
88 }
89
90 pluginConfig += content;
91 return pluginConfig;
92 }
93
SetSerializePluginsConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)94 bool ParsePluginConfig::SetSerializePluginsConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
95 {
96 bool ret = false;
97 if (pluginConfigMap.count(pluginName) == 0) {
98 printf("unknown plugin: %s\n", pluginName.c_str());
99 return ret;
100 }
101
102 // 将pluginConfigMap中保存的configData序列化后写入pluginConfig
103 if (pluginName == "cpu-plugin") {
104 ret = SetSerializeCpuConfig(pluginName, pluginConfig);
105 } else if (pluginName == "diskio-plugin") {
106 ret = SetSerializeDiskioConfig(pluginName, pluginConfig);
107 } else if (pluginName == "ftrace-plugin") {
108 ret = SetSerializeFtraceConfig(pluginName, pluginConfig);
109 } else if (pluginName == "hidump-plugin") {
110 ret = SetSerializeHidumpConfig(pluginName, pluginConfig);
111 } else if (pluginName == "hiebpf-plugin") {
112 ret = SetSerializeHiebpfConfig(pluginName, pluginConfig);
113 } else if (pluginName == "hilog-plugin") {
114 ret = SetSerializeHilogConfig(pluginName, pluginConfig);
115 } else if (pluginName == "memory-plugin") {
116 ret = SetSerializeMemoryConfig(pluginName, pluginConfig);
117 } else if (pluginName == "nativehook") {
118 ret = SetSerializeHookConfig(pluginName, pluginConfig);
119 } else if (pluginName == "network-plugin") {
120 ret = SetSerializeNetworkConfig(pluginName, pluginConfig);
121 } else if (pluginName == "process-plugin") {
122 ret = SetSerializeProcessConfig(pluginName, pluginConfig);
123 } else if (pluginName == "hiperf-plugin") {
124 ret = SetSerializeHiperfConfig(pluginName, pluginConfig);
125 } else if (pluginName == "hisysevent-plugin") {
126 ret = SetSerializeHisyseventConfig(pluginName, pluginConfig);
127 } else if (pluginName == "arkts-plugin") {
128 ret = SetSerializeArkTSConfig(pluginName, pluginConfig);
129 } else {
130 printf("unsupport plugin: %s\n", pluginName.c_str());
131 }
132
133 return ret;
134 }
135
SetSerializeCpuConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)136 bool ParsePluginConfig::SetSerializeCpuConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
137 {
138 std::string configData = pluginConfigMap[pluginName];
139 auto cpuConfigNolite = std::make_unique<ForStandard::CpuConfig>();
140 if (!parser_.ParseFromString(configData, cpuConfigNolite.get())) {
141 printf("cpu parse failed!\n");
142 return false;
143 }
144
145 std::vector<uint8_t> configDataVec(cpuConfigNolite->ByteSizeLong());
146 if (cpuConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
147 printf("cpu serialize failed!\n");
148 return false;
149 }
150 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
151 return true;
152 }
153
SetSerializeDiskioConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)154 bool ParsePluginConfig::SetSerializeDiskioConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
155 {
156 std::string configData = pluginConfigMap[pluginName];
157 auto diskioConfigNolite = std::make_unique<ForStandard::DiskioConfig>();
158 if (!parser_.ParseFromString(configData, diskioConfigNolite.get())) {
159 printf("diskio parse failed!\n");
160 return false;
161 }
162
163 std::vector<uint8_t> configDataVec(diskioConfigNolite->ByteSizeLong());
164 if (diskioConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
165 printf("diskio serialize failed!\n");
166 return false;
167 }
168 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
169 return true;
170 }
171
SetSerializeFtraceConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)172 bool ParsePluginConfig::SetSerializeFtraceConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
173 {
174 std::string configData = pluginConfigMap[pluginName];
175 auto ftraceConfigNolite = std::make_unique<ForStandard::TracePluginConfig>();
176 if (!parser_.ParseFromString(configData, ftraceConfigNolite.get())) {
177 printf("ftrace parse failed!\n");
178 return false;
179 }
180
181 std::vector<uint8_t> configNoLiteDataVec(ftraceConfigNolite->ByteSizeLong());
182 if (ftraceConfigNolite->SerializeToArray(configNoLiteDataVec.data(), configNoLiteDataVec.size()) <= 0) {
183 printf("ftrace serialize failed!\n");
184 return false;
185 }
186
187 pluginConfig.set_config_data((const void*)configNoLiteDataVec.data(), configNoLiteDataVec.size());
188 return true;
189 }
190
SetSerializeHidumpConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)191 bool ParsePluginConfig::SetSerializeHidumpConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
192 {
193 std::string configData = pluginConfigMap[pluginName];
194 auto hidumpConfigNolite = std::make_unique<ForStandard::HidumpConfig>();
195 if (!parser_.ParseFromString(configData, hidumpConfigNolite.get())) {
196 printf("hidump parse failed!\n");
197 return false;
198 }
199
200 std::vector<uint8_t> configDataVec(hidumpConfigNolite->ByteSizeLong());
201 if (hidumpConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
202 printf("hidump serialize failed!\n");
203 return false;
204 }
205 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
206 return true;
207 }
208
SetSerializeHiebpfConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)209 bool ParsePluginConfig::SetSerializeHiebpfConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
210 {
211 auto iter = pluginConfigMap.find(pluginName);
212 if (iter == pluginConfigMap.end()) {
213 printf("find %s failed\n", pluginName.c_str());
214 return false;
215 }
216 auto hiebpfConfigNolite = std::make_unique<ForStandard::HiebpfConfig>();
217 if (hiebpfConfigNolite == nullptr) {
218 printf("hiebpfConfigNolite is nullptr\n");
219 return false;
220 }
221 if (!parser_.ParseFromString(iter->second, hiebpfConfigNolite.get())) {
222 printf("hiebpf config parse failed!\n");
223 return false;
224 }
225 std::vector<uint8_t> config(hiebpfConfigNolite->ByteSizeLong());
226 if (hiebpfConfigNolite->SerializeToArray(config.data(), config.size()) <= 0) {
227 printf("hiebpf serialize failed!\n");
228 return false;
229 }
230 pluginConfig.set_config_data(static_cast<const void*>(config.data()), config.size());
231 return true;
232 }
233
SetSerializeHilogConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)234 bool ParsePluginConfig::SetSerializeHilogConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
235 {
236 std::string configData = pluginConfigMap[pluginName];
237 auto hilogConfigNolite = std::make_unique<ForStandard::HilogConfig>();
238 if (!parser_.ParseFromString(configData, hilogConfigNolite.get())) {
239 printf("hilog parse failed!\n");
240 return false;
241 }
242
243 std::vector<uint8_t> configDataVec(hilogConfigNolite->ByteSizeLong());
244 if (hilogConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
245 printf("hilog serialize failed!\n");
246 return false;
247 }
248 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
249 return true;
250 }
251
SetSerializeMemoryConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)252 bool ParsePluginConfig::SetSerializeMemoryConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
253 {
254 std::string configData = pluginConfigMap[pluginName];
255 auto memoryConfigNolite = std::make_unique<ForStandard::MemoryConfig>();
256 if (!parser_.ParseFromString(configData, memoryConfigNolite.get())) {
257 printf("memory parse failed!\n");
258 return false;
259 }
260
261 std::vector<uint8_t> configDataVec(memoryConfigNolite->ByteSizeLong());
262 if (memoryConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
263 printf("memory serialize failed!\n");
264 return false;
265 }
266 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
267 return true;
268 }
269
SetSerializeHookConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)270 bool ParsePluginConfig::SetSerializeHookConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
271 {
272 std::string configData = pluginConfigMap[pluginName];
273 auto hookConfigNolite = std::make_unique<ForStandard::NativeHookConfig>();
274 if (!parser_.ParseFromString(configData, hookConfigNolite.get())) {
275 printf("nativedaemon parse failed!\n");
276 return false;
277 }
278
279 std::vector<uint8_t> configDataVec(hookConfigNolite->ByteSizeLong());
280 if (hookConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
281 printf("nativedaemon serialize failed!\n");
282 return false;
283 }
284 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
285 return true;
286 }
287
SetSerializeNetworkConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)288 bool ParsePluginConfig::SetSerializeNetworkConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
289 {
290 std::string configData = pluginConfigMap[pluginName];
291 auto networkConfigNolite = std::make_unique<ForStandard::NetworkConfig>();
292 if (!parser_.ParseFromString(configData, networkConfigNolite.get())) {
293 printf("network parse failed!\n");
294 return false;
295 }
296
297 std::vector<uint8_t> configDataVec(networkConfigNolite->ByteSizeLong());
298 if (networkConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
299 printf("network serialize failed!\n");
300 return false;
301 }
302 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
303 return true;
304 }
305
SetSerializeProcessConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)306 bool ParsePluginConfig::SetSerializeProcessConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
307 {
308 std::string configData = pluginConfigMap[pluginName];
309 auto processConfigNolite = std::make_unique<ForStandard::ProcessConfig>();
310 if (!parser_.ParseFromString(configData, processConfigNolite.get())) {
311 printf("process parse failed!\n");
312 return false;
313 }
314
315 std::vector<uint8_t> configDataVec(processConfigNolite->ByteSizeLong());
316 if (processConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
317 printf("process serialize failed!\n");
318 return false;
319 }
320 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
321 return true;
322 }
323
SetSerializeHiperfConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)324 bool ParsePluginConfig::SetSerializeHiperfConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
325 {
326 std::string configData = pluginConfigMap[pluginName];
327 auto hiperfConfigNolite = std::make_unique<ForStandard::HiperfPluginConfig>();
328 if (!parser_.ParseFromString(configData, hiperfConfigNolite.get())) {
329 printf("hiperf config parse failed!\n");
330 return false;
331 }
332
333 std::vector<uint8_t> configDataVec(hiperfConfigNolite->ByteSizeLong());
334 if (hiperfConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
335 printf("hiperf config failed!\n");
336 return false;
337 }
338 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
339 return true;
340 }
341
SetSerializeHisyseventConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)342 bool ParsePluginConfig::SetSerializeHisyseventConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
343 {
344 std::string configData = pluginConfigMap[pluginName];
345 auto hisyseventConfigNolite = std::make_unique<ForStandard::HisyseventConfig>();
346 if (!parser_.ParseFromString(configData, hisyseventConfigNolite.get())) {
347 printf("NODE hisysevent parse failed!\n");
348 return false;
349 }
350
351 std::vector<uint8_t> configDataVec(hisyseventConfigNolite->ByteSizeLong());
352 if (hisyseventConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
353 printf("NODE hisysevent serialize failed!\n");
354 return false;
355 }
356 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
357 return true;
358 }
359
SetSerializeArkTSConfig(const std::string & pluginName,ProfilerPluginConfig & pluginConfig)360 bool ParsePluginConfig::SetSerializeArkTSConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
361 {
362 std::string configData = pluginConfigMap[pluginName];
363 auto arkTSConfigNolite = std::make_unique<ForStandard::ArkTSConfig>();
364 if (!parser_.ParseFromString(configData, arkTSConfigNolite.get())) {
365 return false;
366 }
367
368 std::vector<uint8_t> configDataVec(arkTSConfigNolite->ByteSizeLong());
369 if (arkTSConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
370 return false;
371 }
372 pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
373 return true;
374 }
375