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 isReceivedSigInt_ = false;
48 timeInterval_ = 0;
49 memPid_ = -1;
50 isDumpStorage_ = false;
51 storagePid_ = -1;
52 isDumpNet_ = false;
53 netPid_ = -1;
54 isDumpList_ = false;
55 isDumpService_ = false;
56 isDumpSystemAbility_ = false;
57 abilitieNames_.clear();
58 abilitieArgs_.clear();
59 isDumpSystem_ = false;
60 systemArgs_.clear();
61 isDumpProcesses_ = false;
62 processPid_ = -1;
63 isFaultLog_ = false;
64 path_.clear(); // for zip
65 isAppendix_ = false;
66 isShowSmaps_ = false;
67 isShowSmapsInfo_ = false;
68 isDumpJsHeapMem_ = false;
69 isDumpJsHeapMemGC_ = false;
70 isDumpJsHeapLeakobj_ = false;
71 isDumpCjHeapMem_ = false;
72 isDumpCjHeapMemGC_ = false;
73 dumpJsHeapMemPid_ = 0;
74 dumpCjHeapMemPid_ = 0;
75 threadId_ = 0;
76 ipcStatPid_ = -1;
77 isDumpAllIpc_ = false;
78 isDumpIpc_ = false;
79 isDumpIpcStartStat_ = false;
80 isDumpIpcStopStat_ = false;
81 isDumpIpcStat_ = false;
82 dumpJsRawHeap_ = false;
83 dumpMemPrune_ = false;
84 showAshmem_ = false;
85 showDmaBuf_ = false;
86 }
87
operator =(const DumperOpts & opts)88 DumperOpts& DumperOpts::operator = (const DumperOpts& opts)
89 {
90 Reset();
91 isDumpCpuFreq_ = opts.isDumpCpuFreq_;
92 isDumpCpuUsage_ = opts.isDumpCpuUsage_;
93 cpuUsagePid_ = opts.cpuUsagePid_;
94 isDumpLog_ = opts.isDumpLog_;
95 logArgs_.assign((opts.logArgs_).begin(), (opts.logArgs_).end());
96 isDumpMem_ = opts.isDumpMem_;
97 isReceivedSigInt_ = opts.isReceivedSigInt_;
98 timeInterval_ = opts.timeInterval_;
99 memPid_ = opts.memPid_;
100 isDumpStorage_ = opts.isDumpStorage_;
101 storagePid_ = opts.storagePid_;
102 isDumpNet_ = opts.isDumpNet_;
103 netPid_ = opts.netPid_;
104 isDumpList_ = opts.isDumpList_;
105 isDumpService_ = opts.isDumpService_;
106 isDumpSystemAbility_ = opts.isDumpSystemAbility_;
107 abilitieNames_.assign((opts.abilitieNames_).begin(), (opts.abilitieNames_).end());
108 abilitieArgs_.assign((opts.abilitieArgs_).begin(), (opts.abilitieArgs_).end());
109 isDumpSystem_ = opts.isDumpSystem_;
110 systemArgs_ = opts.systemArgs_;
111 isDumpProcesses_ = opts.isDumpProcesses_;
112 processPid_ = opts.processPid_;
113 isFaultLog_ = opts.isFaultLog_;
114 path_ = opts.path_;
115 isAppendix_ = opts.isAppendix_;
116 isShowSmaps_ = opts.isShowSmaps_;
117 isShowSmapsInfo_ = opts.isShowSmapsInfo_;
118 isDumpJsHeapMem_ = opts.isDumpJsHeapMem_;
119 isDumpJsHeapMemGC_ = opts.isDumpJsHeapMemGC_;
120 isDumpJsHeapLeakobj_ = opts.isDumpJsHeapLeakobj_;
121 isDumpCjHeapMem_ = opts.isDumpCjHeapMem_;
122 isDumpCjHeapMemGC_ = opts.isDumpCjHeapMemGC_;
123 dumpJsHeapMemPid_ = opts.dumpJsHeapMemPid_;
124 dumpCjHeapMemPid_ = opts.dumpCjHeapMemPid_;
125 threadId_ = opts.threadId_;
126 ipcStatPid_ = opts.ipcStatPid_;
127 isDumpAllIpc_ = opts.isDumpAllIpc_;
128 isDumpIpc_ = opts.isDumpIpc_;
129 isDumpIpcStartStat_ = opts.isDumpIpcStartStat_;
130 isDumpIpcStopStat_ = opts.isDumpIpcStopStat_;
131 isDumpIpcStat_ = opts.isDumpIpcStat_;
132 dumpJsRawHeap_ = opts.dumpJsRawHeap_;
133 dumpMemPrune_ = opts.dumpMemPrune_;
134 showAshmem_ = opts.showAshmem_;
135 showDmaBuf_ = opts.showDmaBuf_;
136 return *this;
137 }
138
AddSelectAll()139 void DumperOpts::AddSelectAll()
140 {
141 isDumpCpuFreq_ = true;
142 isDumpCpuUsage_ = true;
143 isDumpLog_ = true;
144 isDumpMem_ = true;
145 isDumpStorage_ = true;
146 isDumpNet_ = true;
147 isDumpService_ = true;
148 isDumpSystemAbility_ = true;
149 isDumpSystem_ = true;
150 isDumpProcesses_ = true;
151 isFaultLog_ = true;
152 isAppendix_ = true;
153 }
154
IsDumpZip() const155 bool DumperOpts::IsDumpZip() const
156 {
157 return DumpCommonUtils::StartWith(path_, PATH_SEPARATOR);
158 }
159
IsSelectAny() const160 bool DumperOpts::IsSelectAny() const
161 {
162 if (isDumpCpuFreq_ || isDumpCpuUsage_) {
163 return true;
164 }
165 if (isDumpLog_ || isFaultLog_) {
166 return true;
167 }
168 if (isDumpMem_) {
169 return true;
170 }
171 if (isDumpStorage_) {
172 return true;
173 }
174 if (isDumpNet_) {
175 return true;
176 }
177 if (isDumpService_ || isDumpSystemAbility_ || isDumpSystem_) {
178 return true;
179 }
180 if (isDumpProcesses_) {
181 return true;
182 }
183 if (isShowSmaps_) {
184 return true;
185 }
186 if (isDumpJsHeapMem_) {
187 return true;
188 }
189 if (isDumpCjHeapMem_) {
190 return true;
191 }
192 if (isDumpIpc_) {
193 return true;
194 }
195 DUMPER_HILOGE(MODULE_COMMON, "select nothing.");
196 return false;
197 }
198
CheckRemainingOptions(std::string & errStr) const199 bool DumperOpts::CheckRemainingOptions(std::string& errStr) const
200 {
201 if (processPid_ < -1) {
202 errStr = std::to_string(processPid_);
203 return false;
204 }
205 if (storagePid_ < -1) {
206 errStr = std::to_string(storagePid_);
207 return false;
208 }
209 if (netPid_ < -1) {
210 errStr = std::to_string(netPid_);
211 return false;
212 }
213 if (dumpJsHeapMemPid_ < 0) {
214 errStr = std::to_string(dumpJsHeapMemPid_);
215 return false;
216 }
217 if (dumpCjHeapMemPid_ < 0) {
218 errStr = std::to_string(dumpCjHeapMemPid_);
219 return false;
220 }
221 if (threadId_ < 0) {
222 errStr = std::to_string(threadId_);
223 return false;
224 }
225 if (timeInterval_ < 0) {
226 errStr = std::to_string(timeInterval_);
227 return false;
228 }
229 if (ipcStatPid_ < -1) {
230 errStr = std::to_string(ipcStatPid_);
231 return false;
232 }
233 return true;
234 }
235
CheckOptions(std::string & errStr) const236 bool DumperOpts::CheckOptions(std::string& errStr) const
237 {
238 if (cpuUsagePid_ < -1) {
239 errStr = std::to_string(cpuUsagePid_);
240 return false;
241 }
242 if (memPid_ < -1) {
243 errStr = std::to_string(memPid_);
244 return false;
245 }
246 if (isDumpList_ && ((!isDumpService_) && (!isDumpSystemAbility_) && (!isDumpSystem_))) {
247 errStr = "-l";
248 return false;
249 }
250 std::string path = TrimStr(path_);
251 if ((!path.empty()) && (!DumpCommonUtils::StartWith(path, PATH_SEPARATOR))) {
252 errStr = path_;
253 return false;
254 }
255 for (size_t i = 0; i < abilitieNames_.size(); i++) {
256 if (DumpUtils::StrToId(abilitieNames_[i]) == -1) {
257 errStr = abilitieNames_[i];
258 return false;
259 }
260 }
261 std::vector<std::string> systemList;
262 ConfigUtils::GetSectionNames(ConfigUtils::CONFIG_GROUP_SYSTEM_, systemList);
263 for (size_t i = 0; i < systemArgs_.size(); i++) {
264 if (std::find(systemList.begin(), systemList.end(), systemArgs_[i]) == systemList.end()) {
265 errStr = systemArgs_[i];
266 return false;
267 }
268 }
269 return CheckRemainingOptions(errStr);
270 }
271 } // namespace HiviewDFX
272 } // namespace OHOS
273