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