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