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 #include "util/config_utils.h"
16 #include "directory_ex.h"
17 #include "hilog_wrapper.h"
18 #include "dump_common_utils.h"
19 #include "dump_utils.h"
20 #include "parameter.h"
21 #include "common/dumper_constant.h"
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 constexpr int ROOT_UID = 0;
26 constexpr int BMS_UID = 1000;
27 constexpr int APP_FIRST_UID = 10000;
28 static const std::string SMAPS_PATH = "smaps/";
29 static const std::string SMAPS_PATH_START = "/proc/";
30 static const std::string SMAPS_PATH_END = "/smaps";
31 } // namespace
32
ConfigUtils(const std::shared_ptr<DumperParameter> & param)33 ConfigUtils::ConfigUtils(const std::shared_ptr<DumperParameter> ¶m) : dumperParam_(param)
34 {
35 }
36
~ConfigUtils()37 ConfigUtils::~ConfigUtils()
38 {
39 pidInfos_.clear();
40 cpuInfos_.clear();
41 currentPidInfos_.clear();
42 currentPidInfo_.Reset();
43 }
44
GetDumperConfigs(const std::shared_ptr<DumperParameter> & param)45 DumpStatus ConfigUtils::GetDumperConfigs(const std::shared_ptr<DumperParameter> ¶m)
46 {
47 DUMPER_HILOGD(MODULE_COMMON, "enter|");
48 DumpStatus ret = DumpStatus::DUMP_FAIL;
49
50 if (param != nullptr) {
51 ConfigUtils configUtils(param);
52 ret = configUtils.GetDumperConfigs();
53 }
54
55 if (ret == DumpStatus::DUMP_OK) {
56 auto dumpCfgs = param->GetExecutorConfigList();
57 for (size_t i = 0; i < dumpCfgs.size(); i++) {
58 dumpCfgs[i]->Dump();
59 }
60 }
61
62 DUMPER_HILOGD(MODULE_COMMON, "leave|ret=%{public}d", ret);
63 return ret;
64 }
65
GetDumperConfigs()66 DumpStatus ConfigUtils::GetDumperConfigs()
67 {
68 DUMPER_HILOGD(MODULE_COMMON, "enter|");
69
70 DumpCommonUtils::GetPidInfos(pidInfos_);
71 DumpCommonUtils::GetCpuInfos(cpuInfos_);
72 DUMPER_HILOGD(MODULE_COMMON, "debug|pidInfos=%{public}zu, cpuInfos=%{public}zu",
73 pidInfos_.size(), cpuInfos_.size());
74
75 std::vector<std::shared_ptr<DumpCfg>> dumpCfgs;
76
77 currentPidInfo_.Reset();
78 currentPidInfos_.clear();
79
80 HandleDumpSystem(dumpCfgs);
81 HandleDumpCpuFreq(dumpCfgs); // cpuid
82 HandleDumpCpuUsage(dumpCfgs); // pid
83 HandleDumpMem(dumpCfgs);
84 HandleDumpMemShowMaps(dumpCfgs);
85 HandleDumpLog(dumpCfgs);
86 HandleDumpStorage(dumpCfgs);
87 HandleDumpNet(dumpCfgs);
88 HandleDumpList(dumpCfgs);
89 HandleDumpAbility(dumpCfgs);
90 HandleDumpService(dumpCfgs);
91 HandleDumpProcesses(dumpCfgs);
92 HandleDumpFaultLog(dumpCfgs);
93 HandleDumpAppendix(dumpCfgs);
94 DUMPER_HILOGD(MODULE_COMMON, "debug|dumpCfgs=%{public}zu", dumpCfgs.size());
95 dumperParam_->SetExecutorConfigList(dumpCfgs);
96 DUMPER_HILOGD(MODULE_COMMON, "leave|");
97 return DumpStatus::DUMP_OK;
98 }
99
GetSectionNames(const std::string & name,std::vector<std::string> & nameList)100 DumpStatus ConfigUtils::GetSectionNames(const std::string &name, std::vector<std::string> &nameList)
101 {
102 std::vector<std::string> tmpUse;
103 GetGroupNames(name, tmpUse);
104 std::transform(tmpUse.begin(), tmpUse.end(), std::back_inserter(nameList),
105 [](std::string &a) { return GetSectionName(a); });
106 DumpUtils::RemoveDuplicateString(nameList); // remove duplicate log names
107 return DumpStatus::DUMP_OK;
108 }
109
GetGroupNames(const std::string & name,std::vector<std::string> & nameList)110 DumpStatus ConfigUtils::GetGroupNames(const std::string &name, std::vector<std::string> &nameList)
111 {
112 bool check = !name.empty();
113 for (int i = 0; i < groupSum_; i++) {
114 if (groups_[i].name_.empty()) {
115 continue;
116 }
117 if (check && (groups_[i].name_.find(name) != 0)) {
118 continue;
119 }
120 nameList.push_back(groups_[i].name_);
121 }
122 return DumpStatus::DUMP_OK;
123 }
124
GetSectionName(const std::string & name)125 std::string ConfigUtils::GetSectionName(const std::string &name)
126 {
127 std::string ret;
128 std::size_t pos = name.find_last_of(CONFIG_NAME_SPLIT);
129 if (pos != std::string::npos) {
130 ret = name.substr(pos + 1);
131 }
132 return ret;
133 }
134
MergePidInfos(std::vector<DumpCommonUtils::PidInfo> & pidInfos,int pid)135 bool ConfigUtils::MergePidInfos(std::vector<DumpCommonUtils::PidInfo> &pidInfos, int pid)
136 {
137 pidInfos.clear();
138 if (pid < 0) {
139 currentPidInfo_.pid_ = pid;
140 currentPidInfo_.uid_ = -1;
141 pidInfos.assign(pidInfos_.begin(), pidInfos_.end());
142 } else {
143 if (DumpCommonUtils::GetProcessInfo(pid, currentPidInfo_)) {
144 pidInfos.push_back(currentPidInfo_);
145 }
146 }
147 return true;
148 }
149
HandleDumpLog(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)150 bool ConfigUtils::HandleDumpLog(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
151 {
152 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
153 if (!dumperOpts.isDumpLog_) {
154 return false;
155 }
156
157 DUMPER_HILOGD(MODULE_COMMON, "debug|log");
158 currentPidInfo_.Reset();
159 currentPidInfos_.clear();
160
161 auto args = OptionArgs::Create();
162 args->SetStrList(dumperOpts.logArgs_);
163 for (size_t i = 0; i < dumperOpts.logArgs_.size(); i++) {
164 std::string name = CONFIG_GROUP_LOG_ + dumperOpts.logArgs_[i];
165 GetConfig(name, dumpCfgs, args);
166 }
167
168 currentPidInfos_.clear();
169 currentPidInfo_.Reset();
170 return true;
171 }
172
HandleDumpList(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)173 bool ConfigUtils::HandleDumpList(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
174 {
175 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
176 if (!dumperOpts.isDumpList_) {
177 return false;
178 }
179
180 DUMPER_HILOGD(MODULE_COMMON, "debug|list");
181 currentPidInfo_.Reset();
182 currentPidInfos_.clear();
183
184 if (dumperOpts.isDumpSystemAbility_) {
185 DUMPER_HILOGD(MODULE_COMMON, "debug|list ability");
186 std::shared_ptr<OptionArgs> args;
187 GetConfig(CONFIG_DUMPER_LIST_SYSTEM_ABILITY, dumpCfgs, args);
188 }
189
190 if (dumperOpts.isDumpService_) {
191 DUMPER_HILOGD(MODULE_COMMON, "debug|list service");
192 std::shared_ptr<OptionArgs> args;
193 GetConfig(CONFIG_DUMPER_LIST_SERVICE, dumpCfgs, args);
194 }
195
196 if (dumperOpts.isDumpSystem_) {
197 DUMPER_HILOGD(MODULE_COMMON, "debug|list system");
198 std::shared_ptr<OptionArgs> args;
199 GetConfig(CONFIG_DUMPER_LIST_SYSTEM, dumpCfgs, args);
200 }
201
202 currentPidInfos_.clear();
203 currentPidInfo_.Reset();
204 return true;
205 }
206
HandleDumpService(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)207 bool ConfigUtils::HandleDumpService(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
208 {
209 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
210 if (dumperOpts.isDumpList_ || (!dumperOpts.isDumpService_)) {
211 return false;
212 }
213
214 DUMPER_HILOGD(MODULE_COMMON, "debug|service");
215 currentPidInfo_.Reset();
216 currentPidInfos_.clear();
217
218 std::shared_ptr<OptionArgs> args;
219 GetConfig(CONFIG_GROUP_SERVICE, dumpCfgs, args);
220
221 currentPidInfos_.clear();
222 currentPidInfo_.Reset();
223 return true;
224 }
225
HandleDumpAbility(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)226 bool ConfigUtils::HandleDumpAbility(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
227 {
228 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
229 if (dumperOpts.isDumpList_ || (!dumperOpts.isDumpSystemAbility_)) {
230 return false;
231 }
232
233 DUMPER_HILOGD(MODULE_COMMON, "debug|ability");
234 currentPidInfo_.Reset();
235 currentPidInfos_.clear();
236
237 auto args = OptionArgs::Create();
238 args->SetNamesAndArgs(dumperOpts.abilitieNames_, dumperOpts.abilitieArgs_);
239 GetConfig(CONFIG_GROUP_ABILITY, dumpCfgs, args);
240
241 currentPidInfos_.clear();
242 currentPidInfo_.Reset();
243 return true;
244 }
245
HandleDumpSystem(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)246 bool ConfigUtils::HandleDumpSystem(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
247 {
248 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
249 if (dumperOpts.isDumpList_ || (!dumperOpts.isDumpSystem_)) {
250 return false;
251 }
252
253 DUMPER_HILOGD(MODULE_COMMON, "debug|system");
254 currentPidInfo_.Reset();
255 currentPidInfos_.clear();
256
257 if (dumperOpts.systemArgs_.empty()) {
258 std::shared_ptr<OptionArgs> args;
259 GetConfig(CONFIG_GROUP_SYSTEM_BASE, dumpCfgs, args);
260 GetConfig(CONFIG_GROUP_SYSTEM_SYSTEM, dumpCfgs, args);
261 isDumpSystemSystem = true;
262 return true;
263 }
264
265 auto args = OptionArgs::Create();
266 args->SetStrList(dumperOpts.systemArgs_);
267 for (size_t i = 0; i < dumperOpts.systemArgs_.size(); i++) {
268 std::string name = CONFIG_GROUP_SYSTEM_ + dumperOpts.systemArgs_[i];
269 GetConfig(name, dumpCfgs, args);
270 if (name == CONFIG_GROUP_SYSTEM_SYSTEM) {
271 isDumpSystemSystem = true;
272 }
273 }
274
275 currentPidInfos_.clear();
276 currentPidInfo_.Reset();
277 return true;
278 }
279
HandleDumpCpuFreq(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)280 bool ConfigUtils::HandleDumpCpuFreq(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
281 {
282 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
283 if (!dumperOpts.isDumpCpuFreq_) {
284 return false;
285 }
286
287 DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpSystem=%{public}d", isDumpSystemSystem);
288 if (isDumpSystemSystem) {
289 return false;
290 }
291
292 DUMPER_HILOGD(MODULE_COMMON, "debug|cpu freq");
293 currentPidInfo_.Reset();
294 currentPidInfos_.clear();
295
296 std::shared_ptr<OptionArgs> args;
297 GetConfig(CONFIG_GROUP_CPU_FREQ, dumpCfgs, args);
298
299 currentPidInfos_.clear();
300 currentPidInfo_.Reset();
301 return true;
302 }
303
HandleDumpCpuUsage(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)304 bool ConfigUtils::HandleDumpCpuUsage(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
305 {
306 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
307 if (!dumperOpts.isDumpCpuUsage_) {
308 return false;
309 }
310
311 DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpSystem=%{public}d, cpuUsagePid=%{public}d",
312 isDumpSystemSystem, dumperOpts.cpuUsagePid_);
313 if (isDumpSystemSystem && (dumperOpts.cpuUsagePid_ < 0)) {
314 return false;
315 }
316
317 DUMPER_HILOGD(MODULE_COMMON, "debug|cpu usage");
318 currentPidInfo_.Reset();
319 currentPidInfos_.clear();
320 MergePidInfos(currentPidInfos_, dumperOpts.cpuUsagePid_);
321
322 std::shared_ptr<OptionArgs> args;
323 GetConfig(CONFIG_GROUP_CPU_USAGE, dumpCfgs, args);
324
325 currentPidInfos_.clear();
326 currentPidInfo_.Reset();
327 return true;
328 }
329
HandleDumpMem(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)330 bool ConfigUtils::HandleDumpMem(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
331 {
332 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
333 if (!dumperOpts.isDumpMem_) {
334 return false;
335 }
336
337 DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpSystem=%{public}d, memPid=%{public}d",
338 isDumpSystemSystem, dumperOpts.memPid_);
339 if (isDumpSystemSystem && (dumperOpts.memPid_ < 0)) {
340 return false;
341 }
342
343 DUMPER_HILOGD(MODULE_COMMON, "debug|mem");
344 HandleDumpMemCommon(dumperParam_, dumpCfgs);
345 return true;
346 }
347
HandleDumpMemShowMaps(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)348 bool ConfigUtils::HandleDumpMemShowMaps(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
349 {
350 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
351 if (!dumperOpts.isShowSmaps_) {
352 return false;
353 }
354
355 DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpSystem=%{public}d, memPid=%{public}d",
356 isDumpSystemSystem, dumperOpts.memPid_);
357 if (isDumpSystemSystem && (dumperOpts.memPid_ < 0)) {
358 return false;
359 }
360
361 DUMPER_HILOGD(MODULE_COMMON, "debug|mem-smaps");
362 HandleDumpMemCommon(dumperParam_, dumpCfgs);
363 return true;
364 }
365
HandleDumpMemCommon(std::shared_ptr<DumperParameter> dumpParam,std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)366 void ConfigUtils::HandleDumpMemCommon(std::shared_ptr<DumperParameter> dumpParam,
367 std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
368 {
369 currentPidInfo_.Reset();
370 currentPidInfos_.clear();
371 MergePidInfos(currentPidInfos_, dumpParam->GetOpts().memPid_);
372 std::shared_ptr<OptionArgs> args;
373 GetConfig(CONFIG_GROUP_MEMORY, dumpCfgs, args);
374 currentPidInfos_.clear();
375 currentPidInfo_.Reset();
376 }
377
HandleDumpStorage(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)378 bool ConfigUtils::HandleDumpStorage(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
379 {
380 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
381 if (!dumperOpts.isDumpStorage_) {
382 return false;
383 }
384
385 DUMPER_HILOGD(MODULE_COMMON, "debug|storage");
386 currentPidInfo_.Reset();
387 currentPidInfos_.clear();
388
389 std::shared_ptr<OptionArgs> args;
390 GetConfig(CONFIG_GROUP_STORAGE, dumpCfgs, args);
391
392 currentPidInfos_.clear();
393 currentPidInfo_.Reset();
394 return true;
395 }
396
HandleDumpNet(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)397 bool ConfigUtils::HandleDumpNet(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
398 {
399 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
400 if (!dumperOpts.isDumpNet_) {
401 return false;
402 }
403
404 DUMPER_HILOGD(MODULE_COMMON, "debug|net");
405 currentPidInfo_.Reset();
406 currentPidInfos_.clear();
407
408 std::shared_ptr<OptionArgs> args;
409 GetConfig(CONFIG_GROUP_NET, dumpCfgs, args);
410
411 currentPidInfos_.clear();
412 currentPidInfo_.Reset();
413 return true;
414 }
415
HandleDumpProcesses(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)416 bool ConfigUtils::HandleDumpProcesses(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
417 {
418 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
419 std::string path = dumperOpts.path_;
420 if (!dumperOpts.isDumpProcesses_) {
421 return false;
422 }
423
424 DUMPER_HILOGD(MODULE_COMMON, "debug|processes");
425 currentPidInfo_.Reset();
426 currentPidInfos_.clear();
427 MergePidInfos(currentPidInfos_, dumperOpts.processPid_);
428
429 std::string mode = GetBuildType();
430 std::shared_ptr<OptionArgs> args;
431 if (mode == RELEASE_MODE) { // release mode
432 if (dumperOpts.processPid_ < 0) {
433 GetConfig(CONFIG_GROUP_PROCESSES, dumpCfgs, args);
434 } else {
435 GetConfig(CONFIG_GROUP_PROCESSES_PID, dumpCfgs, args);
436 }
437 } else { // engine mode
438 if (dumperOpts.processPid_ < 0) {
439 GetConfig(CONFIG_GROUP_PROCESSES_ENG, dumpCfgs, args);
440 } else {
441 GetConfig(CONFIG_GROUP_PROCESSES_PID_ENG, dumpCfgs, args);
442 }
443
444 if (dumperOpts.IsDumpZip()) {
445 CopySmaps();
446 }
447 }
448
449 currentPidInfos_.clear();
450 currentPidInfo_.Reset();
451 return true;
452 }
453
HandleDumpFaultLog(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)454 bool ConfigUtils::HandleDumpFaultLog(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
455 {
456 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
457 if (!dumperOpts.isFaultLog_) {
458 return false;
459 }
460
461 DUMPER_HILOGD(MODULE_COMMON, "debug|fault log");
462 currentPidInfo_.Reset();
463 currentPidInfos_.clear();
464
465 std::shared_ptr<OptionArgs> args;
466 GetConfig(CONFIG_GROUP_FAULT_LOG, dumpCfgs, args);
467
468 currentPidInfos_.clear();
469 currentPidInfo_.Reset();
470 return true;
471 }
472
HandleDumpAppendix(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs)473 bool ConfigUtils::HandleDumpAppendix(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs)
474 {
475 const DumperOpts &dumperOpts = dumperParam_->GetOpts();
476 if (!dumperOpts.isAppendix_) {
477 return false;
478 }
479
480 DUMPER_HILOGD(MODULE_COMMON, "debug|appendix");
481 currentPidInfo_.Reset();
482 currentPidInfos_.clear();
483
484 MergePidInfos(currentPidInfos_, -1);
485 std::shared_ptr<OptionArgs> args;
486
487 GetConfig(CONFIG_GROUP_LOG_KERNEL, dumpCfgs, args);
488 GetConfig(CONFIG_GROUP_LOG_INIT, dumpCfgs, args);
489 GetConfig(CONFIG_GROUP_LOG_HILOG, dumpCfgs, args);
490 int callingUid = dumperParam_->GetUid();
491 if (callingUid == ROOT_UID || callingUid == BMS_UID) {
492 GetConfig(CONFIG_GROUP_STACK, dumpCfgs, args);
493 } else {
494 DUMPER_HILOGE(MODULE_COMMON, "No permission to perform dump stack operation, uid=%d", callingUid);
495 }
496 currentPidInfos_.clear();
497 currentPidInfo_.Reset();
498 return true;
499 }
500
GetDumpLevelByPid(int uid,const DumpCommonUtils::PidInfo & pidInfo)501 int ConfigUtils::GetDumpLevelByPid(int uid, const DumpCommonUtils::PidInfo &pidInfo)
502 {
503 int ret = DumperConstant::LEVEL_NONE;
504 if (uid == ROOT_UID) {
505 ret = DumperConstant::LEVEL_HIGH;
506 } else if (uid < APP_FIRST_UID) {
507 ret = DumperConstant::LEVEL_MIDDLE;
508 } else {
509 if (uid == pidInfo.uid_) {
510 ret = DumperConstant::LEVEL_MIDDLE;
511 }
512 }
513 return ret;
514 }
515
GetConfig(const std::string & name,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args)516 DumpStatus ConfigUtils::GetConfig(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
517 std::shared_ptr<OptionArgs> args)
518 {
519 DumpStatus ret = DumpStatus::DUMP_FAIL;
520 if (name.find(CONFIG_DUMPER_) == 0) {
521 DUMPER_HILOGD(MODULE_COMMON, "debug|dumper, name=%{public}s", name.c_str());
522 ret = GetDumper(name, result, args);
523 } else if (name.find(CONFIG_GROUP_) == 0) {
524 DUMPER_HILOGD(MODULE_COMMON, "debug|group, name=%{public}s", name.c_str());
525 ret = GetGroup(name, result, args);
526 } else {
527 DUMPER_HILOGE(MODULE_COMMON, "error|name=%{public}s", name.c_str());
528 }
529 return ret;
530 }
531
GetDumper(int index,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args,int level)532 DumpStatus ConfigUtils::GetDumper(int index, std::vector<std::shared_ptr<DumpCfg>> &result,
533 std::shared_ptr<OptionArgs> args, int level)
534 {
535 if ((index < 0) || (index >= dumperSum_)) {
536 return DumpStatus::DUMP_INVALID_ARG;
537 }
538 auto itemlist = dumpers_[index].list_;
539 auto itemsize = dumpers_[index].size_;
540 for (int i = 0; i < itemsize; i++) {
541 if (DumpCfg::IsFilter(itemlist[i].class_) && DumpCfg::IsLevel(level)) {
542 if ((itemlist[i].level_ != DumperConstant::LEVEL_ALL) && (itemlist[i].level_ != level)) {
543 continue;
544 }
545 }
546 auto dumpCfg = DumpCfg::Create();
547 dumpCfg->name_ = itemlist[i].name_;
548 dumpCfg->desc_ = itemlist[i].desc_;
549 dumpCfg->target_ = itemlist[i].target_;
550 dumpCfg->section_ = itemlist[i].section_;
551 dumpCfg->class_ = itemlist[i].class_;
552 dumpCfg->level_ = itemlist[i].level_;
553 dumpCfg->loop_ = itemlist[i].loop_;
554 dumpCfg->filterCfg_ = itemlist[i].filterCfg_;
555 dumpCfg->args_ = dumpCfg->IsDumper() ? args : nullptr;
556 result.push_back(dumpCfg);
557 }
558 return DumpStatus::DUMP_OK;
559 }
560
GetDumper(const std::string & name,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args,int level)561 DumpStatus ConfigUtils::GetDumper(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
562 std::shared_ptr<OptionArgs> args, int level)
563 {
564 DumpStatus ret = DumpStatus::DUMP_FAIL;
565 int index = -1;
566 for (int i = 0; i < dumperSum_; i++) {
567 if (dumpers_[i].name_.empty()) {
568 continue;
569 }
570 if (name != dumpers_[i].name_) {
571 continue;
572 }
573 index = i;
574 break;
575 }
576 if (index > -1) {
577 ret = GetDumper(index, result, args, level);
578 }
579 return ret;
580 }
581
GetGroupSimple(const GroupCfg & groupCfg,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args,int level,int nest)582 DumpStatus ConfigUtils::GetGroupSimple(const GroupCfg &groupCfg, std::vector<std::shared_ptr<DumpCfg>> &result,
583 std::shared_ptr<OptionArgs> args, int level, int nest)
584 {
585 if (nest > NEST_MAX) {
586 return DumpStatus::DUMP_INVALID_ARG;
587 }
588 if ((groupCfg.list_ == nullptr) || (groupCfg.size_ < 1)) {
589 return DumpStatus::DUMP_OK;
590 }
591
592 auto dumpGroup = DumpCfg::Create();
593 if (groupCfg.expand_) {
594 dumpGroup->class_ = DumperConstant::GROUP;
595 dumpGroup->name_ = groupCfg.name_;
596 dumpGroup->desc_ = groupCfg.desc_;
597 dumpGroup->type_ = groupCfg.type_;
598 dumpGroup->expand_ = groupCfg.expand_;
599 result.push_back(dumpGroup);
600 }
601 auto &outlist = (groupCfg.expand_) ? dumpGroup->childs_ : result;
602
603 for (int i = 0; i < groupCfg.size_; i++) {
604 if (groupCfg.list_[i].empty()) {
605 continue;
606 }
607 if (DumpCommonUtils::StartWith(groupCfg.list_[i], CONFIG_DUMPER_)) {
608 GetDumper(groupCfg.list_[i], outlist, args, level);
609 } else if (DumpCommonUtils::StartWith(groupCfg.list_[i], CONFIG_MINIGROUP_)) {
610 GetGroup(groupCfg.list_[i], outlist, args, level, nest + 1);
611 } else {
612 DUMPER_HILOGE(MODULE_COMMON, "error|name=%{public}s", groupCfg.name_.c_str());
613 return DumpStatus::DUMP_INVALID_ARG;
614 }
615 }
616
617 return DumpStatus::DUMP_OK;
618 }
619
GetGroup(int index,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args,int level,int nest)620 DumpStatus ConfigUtils::GetGroup(int index, std::vector<std::shared_ptr<DumpCfg>> &result,
621 std::shared_ptr<OptionArgs> args, int level, int nest)
622 {
623 if (nest > NEST_MAX) {
624 return DumpStatus::DUMP_INVALID_ARG;
625 }
626 auto dumpGroup = DumpCfg::Create();
627 dumpGroup->class_ = DumperConstant::GROUP;
628 dumpGroup->name_ = groups_[index].name_;
629 dumpGroup->desc_ = groups_[index].desc_;
630 dumpGroup->type_ = groups_[index].type_;
631 dumpGroup->expand_ = groups_[index].expand_;
632 result.push_back(dumpGroup);
633 if (dumpGroup->expand_ && (dumpGroup->type_ == DumperConstant::GROUPTYPE_PID)) {
634 for (auto pidInfo : currentPidInfos_) {
635 int newLevel = GetDumpLevelByPid(dumperParam_->GetUid(), pidInfo);
636 if (newLevel == DumperConstant::LEVEL_NONE) {
637 continue;
638 }
639 auto newArgs = OptionArgs::Clone(args);
640 newArgs->SetPid(pidInfo.pid_, pidInfo.uid_);
641 GetGroupSimple(groups_[index], dumpGroup->childs_, newArgs, newLevel, nest);
642 }
643 } else if (dumpGroup->expand_ && (dumpGroup->type_ == DumperConstant::GROUPTYPE_CPUID)) {
644 for (auto cpuInfo : cpuInfos_) {
645 auto newArgs = OptionArgs::Clone(args);
646 newArgs->SetCpuId(cpuInfo.id_);
647 GetGroupSimple(groups_[index], dumpGroup->childs_, newArgs, level, nest);
648 }
649 } else if (dumpGroup->type_ == DumperConstant::GROUPTYPE_PID) {
650 int newLevel = GetDumpLevelByPid(dumperParam_->GetUid(), currentPidInfo_);
651 if (newLevel != DumperConstant::LEVEL_NONE) {
652 auto newArgs = OptionArgs::Clone(args);
653 newArgs->SetPid(currentPidInfo_.pid_, currentPidInfo_.uid_);
654 GetGroupSimple(groups_[index], dumpGroup->childs_, newArgs, level, nest);
655 }
656 } else if (dumpGroup->type_ == DumperConstant::GROUPTYPE_CPUID) {
657 auto newArgs = OptionArgs::Clone(args);
658 newArgs->SetCpuId(-1);
659 GetGroupSimple(groups_[index], dumpGroup->childs_, newArgs, level, nest);
660 } else if (dumpGroup->type_ == DumperConstant::NONE) {
661 GetGroupSimple(groups_[index], dumpGroup->childs_, args, level, nest);
662 } else {
663 DUMPER_HILOGE(MODULE_COMMON, "error|type=%{public}d", dumpGroup->type_);
664 return DumpStatus::DUMP_INVALID_ARG;
665 }
666 return DumpStatus::DUMP_OK;
667 }
668
GetGroup(const std::string & name,std::vector<std::shared_ptr<DumpCfg>> & result,std::shared_ptr<OptionArgs> args,int level,int nest)669 DumpStatus ConfigUtils::GetGroup(const std::string &name, std::vector<std::shared_ptr<DumpCfg>> &result,
670 std::shared_ptr<OptionArgs> args, int level, int nest)
671 {
672 if (nest > NEST_MAX) {
673 return DumpStatus::DUMP_INVALID_ARG;
674 }
675 DumpStatus ret = DumpStatus::DUMP_FAIL;
676 int index = -1;
677 // find group
678 for (int i = 0; i < groupSum_; i++) {
679 if (groups_[i].name_.empty()) {
680 continue;
681 }
682 if (name != groups_[i].name_) {
683 continue;
684 }
685 index = i;
686 break;
687 }
688
689 // add dump config to tmpUse
690 std::vector<std::shared_ptr<DumpCfg>> tmpUse;
691 if (index > -1) {
692 ret = GetGroup(index, tmpUse, args, level);
693 }
694
695 if (nest) {
696 result.insert(result.end(), tmpUse.begin(), tmpUse.end());
697 } else {
698 // add section & add config to result
699 SetSection(tmpUse, GetSectionName(name));
700 ConvertTreeToList(tmpUse, result);
701 }
702
703 return ret;
704 }
705
ConvertTreeToList(std::vector<std::shared_ptr<DumpCfg>> & tree,std::vector<std::shared_ptr<DumpCfg>> & list,int nest)706 void ConfigUtils::ConvertTreeToList(std::vector<std::shared_ptr<DumpCfg>> &tree,
707 std::vector<std::shared_ptr<DumpCfg>> &list, int nest)
708 {
709 if (nest > NEST_MAX) {
710 return;
711 }
712
713 std::vector<std::shared_ptr<DumpCfg>> tmpUsed;
714 for (auto item : tree) {
715 if (item == nullptr) {
716 continue;
717 }
718 tmpUsed.push_back(item);
719 if (item->childs_.empty()) {
720 continue;
721 }
722 for (auto child : item->childs_) {
723 child->parent_ = item; // after point to parent, childs must be cleared.
724 }
725 ConvertTreeToList(item->childs_, tmpUsed, nest + 1);
726 item->childs_.clear(); // must clear
727 }
728
729 list.insert(list.end(), tmpUsed.begin(), tmpUsed.end());
730 }
731
SetSection(std::vector<std::shared_ptr<DumpCfg>> & dumpCfgs,const std::string & section,int nest)732 void ConfigUtils::SetSection(std::vector<std::shared_ptr<DumpCfg>> &dumpCfgs, const std::string §ion, int nest)
733 {
734 if (nest > NEST_MAX) {
735 return;
736 }
737
738 for (auto dumpCfg : dumpCfgs) {
739 if (dumpCfg == nullptr) {
740 continue;
741 }
742 if (dumpCfg->IsDumper()) {
743 dumpCfg->section_ = section;
744 }
745 if (dumpCfg->childs_.empty()) {
746 continue;
747 }
748 SetSection(dumpCfg->childs_, section, nest + 1);
749 }
750 }
751
CopySmaps()752 bool ConfigUtils::CopySmaps()
753 {
754 DUMPER_HILOGD(MODULE_COMMON, "CopySmaps enter|");
755
756 std::shared_ptr<RawParam> callback = dumperParam_->getClientCallback();
757 if (callback == nullptr) {
758 DUMPER_HILOGD(MODULE_COMMON, "CopySmaps leave|callback");
759 return false;
760 }
761
762 callback->SetProgressEnabled(true);
763 std::string logFolder = callback->GetFolder();
764 int uid = dumperParam_->GetUid();
765 for (auto &pidInfo : currentPidInfos_) {
766 int newLevel = GetDumpLevelByPid(uid, pidInfo);
767 if (newLevel == DumperConstant::LEVEL_NONE) {
768 continue;
769 }
770 if (callback->IsCanceled()) {
771 DUMPER_HILOGD(MODULE_COMMON, "CopySmaps debug|Canceled");
772 break;
773 }
774 callback->UpdateProgress(0);
775 std::string pid = std::to_string(pidInfo.pid_);
776 std::string desfolder = logFolder + SMAPS_PATH + pidInfo.name_ + "-" + pid;
777 std::string src = SMAPS_PATH_START + pid + SMAPS_PATH_END;
778 std::string des = desfolder + SMAPS_PATH_END;
779 ForceCreateDirectory(IncludeTrailingPathDelimiter(desfolder));
780 DumpUtils::CopyFile(src, des);
781 }
782
783 DUMPER_HILOGD(MODULE_COMMON, "CopySmaps leave|true");
784 return true;
785 }
786 } // namespace HiviewDFX
787 } // namespace OHOS
788