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/dump_cfg.h"
16 #include "hilog_wrapper.h"
17 namespace OHOS {
18 namespace HiviewDFX {
DumpCfg()19 DumpCfg::DumpCfg()
20 {
21 }
22 
~DumpCfg()23 DumpCfg::~DumpCfg()
24 {
25     childs_.clear();
26     parent_ = nullptr;
27 }
28 
operator =(const DumpCfg & dumpCfg)29 DumpCfg& DumpCfg::operator=(const DumpCfg& dumpCfg)
30 {
31     name_ = dumpCfg.name_;
32     desc_ = dumpCfg.desc_;
33     target_ = dumpCfg.target_;
34     section_ = dumpCfg.section_;
35     class_ = dumpCfg.class_;
36     level_ = dumpCfg.level_;
37     loop_ = dumpCfg.loop_;
38     filterCfg_ = dumpCfg.filterCfg_;
39     args_ = dumpCfg.args_;
40     return *this;
41 }
42 
Dump(int deep) const43 void DumpCfg::Dump(int deep) const
44 {
45     Dump(*this, deep);
46 }
47 
IsGroup() const48 bool DumpCfg::IsGroup() const
49 {
50     return ((class_ == DumperConstant::GROUP) || (!childs_.empty()));
51 }
52 
IsDumper() const53 bool DumpCfg::IsDumper() const
54 {
55     return IsDumper(class_);
56 }
57 
IsFilter() const58 bool DumpCfg::IsFilter() const
59 {
60     return IsFilter(class_);
61 }
62 
IsLevel() const63 bool DumpCfg::IsLevel() const
64 {
65     return IsLevel(level_);
66 }
67 
CanLoop() const68 bool DumpCfg::CanLoop() const
69 {
70     return CanLoop(loop_);
71 }
72 
IsOutput() const73 bool DumpCfg::IsOutput() const
74 {
75     return IsOutput(class_);
76 }
77 
HasPid() const78 bool DumpCfg::HasPid() const
79 {
80     return HasPid(target_);
81 }
82 
HasCpuId() const83 bool DumpCfg::HasCpuId() const
84 {
85     return HasCpuId(target_);
86 }
87 
Create()88 std::shared_ptr<DumpCfg> DumpCfg::Create()
89 {
90     return std::make_shared<DumpCfg>();
91 }
92 
IsDumper(int cls)93 bool DumpCfg::IsDumper(int cls)
94 {
95     return ((cls > DumperConstant::DUMPER_BEGIN) && (cls < DumperConstant::DUMPER_END));
96 }
97 
IsFilter(int cls)98 bool DumpCfg::IsFilter(int cls)
99 {
100     return ((cls > DumperConstant::FILTER_BEGIN) && (cls < DumperConstant::FILTER_END));
101 }
102 
IsLevel(int level)103 bool DumpCfg::IsLevel(int level)
104 {
105     return ((level > DumperConstant::LEVEL_BEGIN) && (level < DumperConstant::LEVEL_END));
106 }
107 
CanLoop(int loop)108 bool DumpCfg::CanLoop(int loop)
109 {
110     return ((loop != DumperConstant::NONE) && (loop > 0));
111 }
112 
IsOutput(int cls)113 bool DumpCfg::IsOutput(int cls)
114 {
115     return ((cls > DumperConstant::OUTPUT_BEGIN) && (cls < DumperConstant::OUTPUT_END));
116 }
117 
HasPid(const std::string & target)118 bool DumpCfg::HasPid(const std::string &target)
119 {
120     return (target.find("%pid") != std::string::npos);
121 }
122 
HasCpuId(const std::string & target)123 bool DumpCfg::HasCpuId(const std::string &target)
124 {
125     return (target.find("%cpuid") != std::string::npos);
126 }
127 
Dump(const DumpCfg & cfg,int nest)128 void DumpCfg::Dump(const DumpCfg& cfg, int nest)
129 {
130     DUMPER_HILOGD(MODULE_COMMON, "debug|*********[%{public}d]********", nest);
131     DUMPER_HILOGD(MODULE_COMMON, "debug|section=%{public}s,"
132         " class=%{public}d[%{public}s], level=%{public}d[%{public}s],"
133         " name=%{public}s, type=%{public}d[%{public}s], expand_=%{public}d, target=%{public}s,"
134         " desc=%{public}s,"
135         " filterCfg=%{public}s",
136         cfg.section_.c_str(),
137         cfg.class_, ToStr(cfg.class_).c_str(), cfg.level_, ToStr(cfg.level_).c_str(),
138         cfg.name_.c_str(), cfg.type_, ToStr(cfg.type_).c_str(), cfg.expand_, cfg.target_.c_str(),
139         cfg.desc_.c_str(),
140         cfg.filterCfg_.c_str());
141     if (cfg.args_ != nullptr) {
142         cfg.args_->Dump();
143     }
144     if (cfg.parent_ != nullptr) {
145         DUMPER_HILOGD(MODULE_COMMON, "debug|parent...name=%{public}s, desc=%{public}s",
146             cfg.parent_->name_.c_str(), cfg.parent_->desc_.c_str());
147     } else {
148         for (auto dumpCfg : cfg.childs_) {
149             dumpCfg->Dump(nest+1);
150         }
151     }
152 }
153 
ToStr(int type)154 std::string DumpCfg::ToStr(int type)
155 {
156     if (type == DumperConstant::NONE) {
157         return "none";
158     } else if (type == DumperConstant::GROUP) {
159         return "group";
160     } else if ((type >= DumperConstant::DUMPER_BEGIN) && (type <= DumperConstant::DUMPER_END)) {
161         return ToDumperStr(type);
162     } else if ((type >= DumperConstant::FILTER_BEGIN) && (type <= DumperConstant::FILTER_END)) {
163         return ToFilterStr(type);
164     } else if ((type >= DumperConstant::OUTPUT_BEGIN) && (type <= DumperConstant::OUTPUT_END)) {
165         return ToOutputStr(type);
166     } else if ((type >= DumperConstant::LEVEL_BEGIN) && (type <= DumperConstant::LEVEL_END)) {
167         return ToLevelStr(type);
168     } else if ((type >= DumperConstant::GROUPTYPE_BEGIN) && (type <= DumperConstant::GROUPTYPE_END)) {
169         return ToTypeStr(type);
170     } else if (type == DumperConstant::LOOP) {
171         return "loop";
172     }
173     return "unknown_type";
174 };
175 
ToDumperStr(int type)176 std::string DumpCfg::ToDumperStr(int type)
177 {
178     if (type == DumperConstant::CPU_DUMPER) {
179         return "cpu_dumper";
180     } else if (type == DumperConstant::FILE_DUMPER) {
181         return "file_dumper";
182     } else if (type == DumperConstant::ENV_PARAM_DUMPER) {
183         return "env_dumper";
184     } else if (type == DumperConstant::CMD_DUMPER) {
185         return "cmd_dumper";
186     } else if (type == DumperConstant::PROPERTIES_DUMPER) {
187         return "prop_dumper";
188     } else if (type == DumperConstant::API_DUMPER) {
189         return "api_dumper";
190     } else if (type == DumperConstant::LIST_DUMPER) {
191         return "list_dumper";
192     } else if (type == DumperConstant::VERSION_DUMPER) {
193         return "ver_dumper";
194     } else if (type == DumperConstant::SA_DUMPER) {
195         return "sa_dumper";
196     } else if (type == DumperConstant::MEMORY_DUMPER) {
197         return "mem_dumper";
198     } else if (type == DumperConstant::JSHEAP_MEMORY_DUMPER) {
199         return "dumper_jsheap_memory";
200     } else if (type == DumperConstant::STACK_DUMPER) {
201         return "stack_dumper";
202     } else if (type == DumperConstant::TRAFFIC_DUMPER) {
203         return "traffic_dumper";
204     } else if (type == DumperConstant::IPC_STAT_DUMPER) {
205         return "ipc_stat_dumper";
206     }
207     return "unknown_dumper";
208 }
209 
ToFilterStr(int type)210 std::string DumpCfg::ToFilterStr(int type)
211 {
212     if (type == DumperConstant::COLUMN_ROWS_FILTER) {
213         return "col_row_filter";
214     } else if (type == DumperConstant::FILE_FORMAT_DUMP_FILTER) {
215         return "file_format_dump_filter";
216     }
217     return "unknown_filter";
218 }
219 
ToOutputStr(int type)220 std::string DumpCfg::ToOutputStr(int type)
221 {
222     if (type == DumperConstant::STD_OUTPUT) {
223         return "std_output";
224     } else if (type == DumperConstant::FILE_OUTPUT) {
225         return "file_output";
226     } else if (type == DumperConstant::FD_OUTPUT) {
227         return "fd_output";
228     } else if (type == DumperConstant::ZIP_OUTPUT) {
229         return "zip_output";
230     }
231     return "unknown_output";
232 }
233 
ToLevelStr(int type)234 std::string DumpCfg::ToLevelStr(int type)
235 {
236     if (type == DumperConstant::LEVEL_NONE) {
237         return "level_none";
238     } else if (type == DumperConstant::LEVEL_MIDDLE) {
239         return "level_middle";
240     } else if (type == DumperConstant::LEVEL_HIGH) {
241         return "level_high";
242     } else if (type == DumperConstant::LEVEL_ALL) {
243         return "level_all";
244     }
245     return "level_unknown";
246 }
247 
ToTypeStr(int type)248 std::string DumpCfg::ToTypeStr(int type)
249 {
250     if (type == DumperConstant::LEVEL_NONE) {
251         return "type_none";
252     } else if (type == DumperConstant::GROUPTYPE_PID) {
253         return "type_pid";
254     } else if (type == DumperConstant::GROUPTYPE_CPUID) {
255         return "type_cpuid";
256     }
257     return "type_unknown";
258 }
259 } // namespace HiviewDFX
260 } // namespace OHOS
261