• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common/dumper_opts.h"
16 #include <algorithm>
17 #include "string_ex.h"
18 #include "dump_controller.h"
19 #include "dump_utils.h"
20 #include "hilog_wrapper.h"
21 #include "util/config_utils.h"
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 static const std::string PATH_SEPARATOR = "/";
26 }
27 
DumperOpts()28 DumperOpts::DumperOpts()
29 {
30     Reset();
31 }
32 
Reset()33 void DumperOpts::Reset()
34 {
35     isDumpCpuFreq_ = false;
36     isDumpCpuUsage_ = false;
37     cpuUsagePid_ = -1;
38     isDumpLog_ = false;
39     logArgs_.clear();
40     isDumpMem_ = false;
41     memPid_ = -1;
42     isDumpStorage_ = false;
43     isDumpNet_ = false;
44     isDumpList_ = false;
45     isDumpService_ = false;
46     isDumpSystemAbility_ = false;
47     abilitieNames_.clear();
48     abilitieArgs_.clear();
49     isDumpSystem_ = false;
50     systemArgs_.clear();
51     isDumpProcesses_ = false;
52     processPid_ = -1;
53     isFaultLog_ = false;
54     timeout_ = DEFAULT_TIMEOUT;
55     limitSize_ = DEFAULT_LIMITSIZE;
56     path_.clear(); // for zip
57     isAppendix_ = false;
58     isTest_ = false;
59 }
60 
operator =(const DumperOpts & opts)61 DumperOpts& DumperOpts::operator = (const DumperOpts& opts)
62 {
63     Reset();
64     isDumpCpuFreq_ = opts.isDumpCpuFreq_;
65     isDumpCpuUsage_ = opts.isDumpCpuUsage_;
66     cpuUsagePid_ = opts.cpuUsagePid_;
67     isDumpLog_ = opts.isDumpLog_;
68     logArgs_.assign((opts.logArgs_).begin(), (opts.logArgs_).end());
69     isDumpMem_ = opts.isDumpMem_;
70     memPid_ = opts.memPid_;
71     isDumpStorage_ = opts.isDumpStorage_;
72     isDumpNet_ = opts.isDumpNet_;
73     isDumpList_ = opts.isDumpList_;
74     isDumpService_ = opts.isDumpService_;
75     isDumpSystemAbility_ = opts.isDumpSystemAbility_;
76     abilitieNames_.assign((opts.abilitieNames_).begin(), (opts.abilitieNames_).end());
77     abilitieArgs_.assign((opts.abilitieArgs_).begin(), (opts.abilitieArgs_).end());
78     isDumpSystem_ = opts.isDumpSystem_;
79     systemArgs_ = opts.systemArgs_;
80     isDumpProcesses_ = opts.isDumpProcesses_;
81     processPid_ = opts.processPid_;
82     isFaultLog_ = opts.isFaultLog_;
83     timeout_ = opts.timeout_;
84     limitSize_ = opts.limitSize_;
85     path_ = opts.path_;
86     isAppendix_ = opts.isAppendix_;
87     isTest_ = opts.isTest_;
88     return *this;
89 }
90 
AddSelectAll()91 void DumperOpts::AddSelectAll()
92 {
93     isDumpCpuFreq_ = true;
94     isDumpCpuUsage_ = true;
95     isDumpLog_ = true;
96     isDumpMem_ = true;
97     isDumpStorage_ = true;
98     isDumpNet_ = true;
99     isDumpService_ = true;
100     isDumpSystemAbility_ = true;
101     isDumpSystem_ = true;
102     isDumpProcesses_ = true;
103     isFaultLog_ = true;
104     isAppendix_ = true;
105 }
106 
IsDumpZip() const107 bool DumperOpts::IsDumpZip() const
108 {
109     return DumpCommonUtils::StartWith(path_, PATH_SEPARATOR);
110 }
111 
IsSelectAny() const112 bool DumperOpts::IsSelectAny() const
113 {
114     if (isDumpCpuFreq_ || isDumpCpuUsage_) {
115         return true;
116     }
117     if (isDumpLog_ || isFaultLog_) {
118         return true;
119     }
120     if (isDumpMem_) {
121         return true;
122     }
123     if (isDumpStorage_) {
124         return true;
125     }
126     if (isDumpNet_) {
127         return true;
128     }
129     if (isDumpService_ || isDumpSystemAbility_ || isDumpSystem_) {
130         return true;
131     }
132     if (isDumpProcesses_) {
133         return true;
134     }
135     if (isTest_) {
136         return true;
137     }
138     DUMPER_HILOGD(MODULE_COMMON, "debug|select nothing.");
139     return false;
140 }
141 
CheckOptions(std::string & errStr) const142 bool DumperOpts::CheckOptions(std::string& errStr) const
143 {
144     if (cpuUsagePid_ < -1) {
145         errStr = std::to_string(cpuUsagePid_);
146         return false;
147     }
148     if (memPid_ < -1) {
149         errStr = std::to_string(memPid_);
150         return false;
151     }
152     if (isDumpList_ && ((!isDumpService_) && (!isDumpSystemAbility_) && (!isDumpSystem_))) {
153         errStr = "-1";
154         return false;
155     }
156     std::string path = TrimStr(path_);
157     if ((!path.empty()) && (!DumpCommonUtils::StartWith(path, PATH_SEPARATOR))) {
158         errStr = path_;
159         return false;
160     }
161     for (size_t i = 0; i < abilitieNames_.size(); i++) {
162         if (!DumpUtils::StrToId(abilitieNames_[i])) {
163             errStr = abilitieNames_[i];
164             return false;
165         }
166     }
167     std::vector<std::string> systemList;
168     ConfigUtils::GetSectionNames(ConfigUtils::CONFIG_GROUP_SYSTEM_, systemList);
169     for (size_t i = 0; i < systemArgs_.size(); i++) {
170         if (std::find(systemList.begin(), systemList.end(), systemArgs_[i]) == systemList.end()) {
171             errStr = systemArgs_[i];
172             return false;
173         }
174     }
175     if (processPid_ < -1) {
176         errStr = std::to_string(processPid_);
177         return false;
178     }
179     if (timeout_ < 1) {
180         errStr = std::to_string(timeout_);
181         return false;
182     }
183     if (limitSize_ < 1) {
184         errStr = std::to_string(limitSize_);
185         return false;
186     }
187     return true;
188 }
189 
Dump() const190 void DumperOpts::Dump() const
191 {
192     DUMPER_HILOGD(MODULE_COMMON, "debug|===============[DumperOpts]=============");
193     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpCpuFreq=%{public}d", isDumpCpuFreq_);
194     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpCpuUsage=%{public}d, cpuUsagePid_=%{public}d",
195         isDumpCpuUsage_, cpuUsagePid_);
196     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpLog=%{public}d", isDumpLog_);
197     for (size_t i = 0; i < logArgs_.size(); i++) {
198         DUMPER_HILOGD(MODULE_COMMON, "debug|    logArgs[%{public}zu]_=%{public}s", i, logArgs_[i].c_str());
199     }
200     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpMem=%{public}d, memPid=%{public}d", isDumpMem_, memPid_);
201     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpStorage=%{public}d", isDumpStorage_);
202     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpNet=%{public}d", isDumpNet_);
203     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpList=%{public}d,"
204         " isDumpService=%{public}d, isDumpSystemAbility=%{public}d, isDumpSystem=%{public}d",
205         isDumpList_, isDumpService_, isDumpSystemAbility_, isDumpSystem_);
206     for (size_t i = 0; i < systemArgs_.size(); i++) {
207         DUMPER_HILOGD(MODULE_COMMON, "debug|    systemArgs[%{public}zu]=%{public}s", i, systemArgs_[i].c_str());
208     }
209     for (size_t i = 0; i < abilitieNames_.size(); i++) {
210         DUMPER_HILOGD(MODULE_COMMON, "debug|    abilitieNames[%{public}zu]=%{public}s", i, abilitieNames_[i].c_str());
211     }
212     for (size_t i = 0; i < abilitieArgs_.size(); i++) {
213         DUMPER_HILOGD(MODULE_COMMON, "debug|    abilitieArgs[%{public}zu]=%{public}s", i, abilitieArgs_[i].c_str());
214     }
215     DUMPER_HILOGD(MODULE_COMMON, "debug|isDumpProcesses=%{public}d, processPid=%{public}d",
216         isDumpProcesses_, processPid_);
217     DUMPER_HILOGD(MODULE_COMMON, "debug|isFaultLog=%{public}d", isFaultLog_);
218     DUMPER_HILOGD(MODULE_COMMON, "debug|timeout=%{public}d", timeout_);
219     DUMPER_HILOGD(MODULE_COMMON, "debug|limitSize=%{public}d", limitSize_);
220     DUMPER_HILOGD(MODULE_COMMON, "debug|path=%{public}s", path_.c_str());
221     DUMPER_HILOGD(MODULE_COMMON, "debug|isAppendix=%{public}d", isAppendix_);
222     DUMPER_HILOGD(MODULE_COMMON, "debug|isTest=%{public}d", isTest_);
223 }
224 } // namespace HiviewDFX
225 } // namespace OHOS
226