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