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