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 storagePid_ = -1;
50 isDumpNet_ = false;
51 netPid_ = -1;
52 isDumpList_ = false;
53 isDumpService_ = false;
54 isDumpSystemAbility_ = false;
55 abilitieNames_.clear();
56 abilitieArgs_.clear();
57 isDumpSystem_ = false;
58 systemArgs_.clear();
59 isDumpProcesses_ = false;
60 processPid_ = -1;
61 isFaultLog_ = false;
62 timeout_ = DEFAULT_TIMEOUT;
63 limitSize_ = DEFAULT_LIMITSIZE;
64 path_.clear(); // for zip
65 isAppendix_ = false;
66 isTest_ = false;
67 isShowSmaps_ = false;
68 isShowSmapsInfo_ = false;
69 }
70
operator =(const DumperOpts & opts)71 DumperOpts& DumperOpts::operator = (const DumperOpts& opts)
72 {
73 Reset();
74 isDumpCpuFreq_ = opts.isDumpCpuFreq_;
75 isDumpCpuUsage_ = opts.isDumpCpuUsage_;
76 cpuUsagePid_ = opts.cpuUsagePid_;
77 isDumpLog_ = opts.isDumpLog_;
78 logArgs_.assign((opts.logArgs_).begin(), (opts.logArgs_).end());
79 isDumpMem_ = opts.isDumpMem_;
80 memPid_ = opts.memPid_;
81 isDumpStorage_ = opts.isDumpStorage_;
82 storagePid_ = opts.storagePid_;
83 isDumpNet_ = opts.isDumpNet_;
84 netPid_ = opts.netPid_;
85 isDumpList_ = opts.isDumpList_;
86 isDumpService_ = opts.isDumpService_;
87 isDumpSystemAbility_ = opts.isDumpSystemAbility_;
88 abilitieNames_.assign((opts.abilitieNames_).begin(), (opts.abilitieNames_).end());
89 abilitieArgs_.assign((opts.abilitieArgs_).begin(), (opts.abilitieArgs_).end());
90 isDumpSystem_ = opts.isDumpSystem_;
91 systemArgs_ = opts.systemArgs_;
92 isDumpProcesses_ = opts.isDumpProcesses_;
93 processPid_ = opts.processPid_;
94 isFaultLog_ = opts.isFaultLog_;
95 timeout_ = opts.timeout_;
96 limitSize_ = opts.limitSize_;
97 path_ = opts.path_;
98 isAppendix_ = opts.isAppendix_;
99 isTest_ = opts.isTest_;
100 isShowSmaps_ = opts.isShowSmaps_;
101 isShowSmapsInfo_ = opts.isShowSmapsInfo_;
102 return *this;
103 }
104
AddSelectAll()105 void DumperOpts::AddSelectAll()
106 {
107 isDumpCpuFreq_ = true;
108 isDumpCpuUsage_ = true;
109 isDumpLog_ = true;
110 isDumpMem_ = true;
111 isDumpStorage_ = true;
112 isDumpNet_ = true;
113 isDumpService_ = true;
114 isDumpSystemAbility_ = true;
115 isDumpSystem_ = true;
116 isDumpProcesses_ = true;
117 isFaultLog_ = true;
118 isAppendix_ = true;
119 }
120
IsDumpZip() const121 bool DumperOpts::IsDumpZip() const
122 {
123 return DumpCommonUtils::StartWith(path_, PATH_SEPARATOR);
124 }
125
IsSelectAny() const126 bool DumperOpts::IsSelectAny() const
127 {
128 if (isDumpCpuFreq_ || isDumpCpuUsage_) {
129 return true;
130 }
131 if (isDumpLog_ || isFaultLog_) {
132 return true;
133 }
134 if (isDumpMem_) {
135 return true;
136 }
137 if (isDumpStorage_) {
138 return true;
139 }
140 if (isDumpNet_) {
141 return true;
142 }
143 if (isDumpService_ || isDumpSystemAbility_ || isDumpSystem_) {
144 return true;
145 }
146 if (isDumpProcesses_) {
147 return true;
148 }
149 if (isTest_) {
150 return true;
151 }
152 if (isShowSmaps_) {
153 return true;
154 }
155 DUMPER_HILOGE(MODULE_COMMON, "select nothing.");
156 return false;
157 }
158
CheckOptions(std::string & errStr) const159 bool DumperOpts::CheckOptions(std::string& errStr) const
160 {
161 if (cpuUsagePid_ < -1) {
162 errStr = std::to_string(cpuUsagePid_);
163 return false;
164 }
165 if (memPid_ < -1) {
166 errStr = std::to_string(memPid_);
167 return false;
168 }
169 if (isDumpList_ && ((!isDumpService_) && (!isDumpSystemAbility_) && (!isDumpSystem_))) {
170 errStr = "-1";
171 return false;
172 }
173 std::string path = TrimStr(path_);
174 if ((!path.empty()) && (!DumpCommonUtils::StartWith(path, PATH_SEPARATOR))) {
175 errStr = path_;
176 return false;
177 }
178 for (size_t i = 0; i < abilitieNames_.size(); i++) {
179 if (DumpUtils::StrToId(abilitieNames_[i]) == -1) {
180 errStr = abilitieNames_[i];
181 return false;
182 }
183 }
184 std::vector<std::string> systemList;
185 ConfigUtils::GetSectionNames(ConfigUtils::CONFIG_GROUP_SYSTEM_, systemList);
186 for (size_t i = 0; i < systemArgs_.size(); i++) {
187 if (std::find(systemList.begin(), systemList.end(), systemArgs_[i]) == systemList.end()) {
188 errStr = systemArgs_[i];
189 return false;
190 }
191 }
192 if (processPid_ < -1) {
193 errStr = std::to_string(processPid_);
194 return false;
195 }
196 if (timeout_ < 1) {
197 errStr = std::to_string(timeout_);
198 return false;
199 }
200 if (limitSize_ < 1) {
201 errStr = std::to_string(limitSize_);
202 return false;
203 }
204 if (storagePid_ < -1) {
205 errStr = std::to_string(storagePid_);
206 return false;
207 }
208 if (netPid_ < -1) {
209 errStr = std::to_string(netPid_);
210 return false;
211 }
212 return true;
213 }
214 } // namespace HiviewDFX
215 } // namespace OHOS
216