1 /*
2 * Copyright (c) 2021 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
16 #include "dfx_config.h"
17
18 #include <cstdio>
19 #include <cstdlib>
20 #include <securec.h>
21 #include <string>
22 #include "dfx_define.h"
23 #include "dfx_log.h"
24
25 static const char FAULTLOGGER_CONF_PATH[] = "/system/etc/faultlogger.conf";
26
27 namespace OHOS {
28 namespace HiviewDFX {
GetInstance()29 DfxConfig &DfxConfig::GetInstance()
30 {
31 static DfxConfig instance;
32 return instance;
33 }
34
SetDisplayBacktrace(bool displayBacktrace)35 void DfxConfig::SetDisplayBacktrace(bool displayBacktrace)
36 {
37 displayBacktrace_ = displayBacktrace;
38 }
39
GetDisplayBacktrace() const40 bool DfxConfig::GetDisplayBacktrace() const
41 {
42 return displayBacktrace_;
43 }
44
SetDisplayMaps(bool displayMaps)45 void DfxConfig::SetDisplayMaps(bool displayMaps)
46 {
47 displayMaps_ = displayMaps;
48 }
49
GetDisplayMaps() const50 bool DfxConfig::GetDisplayMaps() const
51 {
52 return displayMaps_;
53 }
54
SetDisplayFaultStack(bool displayFaultStack)55 void DfxConfig::SetDisplayFaultStack(bool displayFaultStack)
56 {
57 displayFaultStack_ = displayFaultStack;
58 }
59
GetDisplayFaultStack() const60 bool DfxConfig::GetDisplayFaultStack() const
61 {
62 return displayFaultStack_;
63 }
64
SetFaultStackLowAddressStep(unsigned int lowAddressStep)65 void DfxConfig::SetFaultStackLowAddressStep(unsigned int lowAddressStep)
66 {
67 if (lowAddressStep == 0) {
68 return ;
69 }
70 lowAddressStep_ = lowAddressStep;
71 }
72
GetFaultStackLowAddressStep() const73 unsigned int DfxConfig::GetFaultStackLowAddressStep() const
74 {
75 return lowAddressStep_;
76 }
77
SetFaultStackHighAddressStep(unsigned int highAddressStep)78 void DfxConfig::SetFaultStackHighAddressStep(unsigned int highAddressStep)
79 {
80 if (highAddressStep == 0) {
81 return ;
82 }
83 highAddressStep_ = highAddressStep;
84 }
85
GetFaultStackHighAddressStep() const86 unsigned int DfxConfig::GetFaultStackHighAddressStep() const
87 {
88 return highAddressStep_;
89 }
90
SetDisplayRegister(bool displayRegister)91 void DfxConfig::SetDisplayRegister(bool displayRegister)
92 {
93 displayRegister_ = displayRegister;
94 }
95
GetDisplayRegister() const96 bool DfxConfig::GetDisplayRegister() const
97 {
98 return displayRegister_;
99 }
100
SetLogPersist(bool logPersist)101 void DfxConfig::SetLogPersist(bool logPersist)
102 {
103 logPersist_ = logPersist;
104 }
105
GetLogPersist() const106 bool DfxConfig::GetLogPersist() const
107 {
108 return logPersist_;
109 }
110
SetDumpOtherThreads(bool dumpOtherThreads)111 void DfxConfig::SetDumpOtherThreads(bool dumpOtherThreads)
112 {
113 dumpOtherThreads_ = dumpOtherThreads;
114 }
115
GetDumpOtherThreads() const116 bool DfxConfig::GetDumpOtherThreads() const
117 {
118 return dumpOtherThreads_;
119 }
120
ParserConfig(const std::string & key,const std::string & value)121 void DfxConfig::ParserConfig(const std::string& key, const std::string& value)
122 {
123 unsigned int lowAddressStep = 0;
124 unsigned int highAddressStep = 0;
125 do {
126 if ((key.compare("faultlogLogPersist") == 0) && (value.compare("true") == 0)) {
127 SetLogPersist(true);
128 break;
129 }
130 if ((key.compare("displayRigister") == 0) && (value.compare("false") == 0)) {
131 SetDisplayRegister(false);
132 break;
133 }
134 if ((key.compare("displayBacktrace") == 0) && (value.compare("false") == 0)) {
135 SetDisplayBacktrace(false);
136 break;
137 }
138 if ((key.compare("displayMaps") == 0) && (value.compare("false") == 0)) {
139 SetDisplayMaps(false);
140 break;
141 }
142 if ((key.compare("displayFaultStack.switch") == 0) && (value.compare("false") == 0)) {
143 SetDisplayFaultStack(false);
144 break;
145 }
146 if (key.compare("displayFaultStack.lowAddressStep") == 0) {
147 lowAddressStep = static_cast<unsigned int>(atoi(value.data()));
148 SetFaultStackLowAddressStep(lowAddressStep);
149 break;
150 }
151 if (key.compare("displayFaultStack.highAddressStep") == 0) {
152 highAddressStep = static_cast<unsigned int>(atoi(value.data()));
153 SetFaultStackHighAddressStep(highAddressStep);
154 break;
155 }
156 if ((key.compare("dumpOtherThreads") == 0) && (value.compare("true") == 0)) {
157 SetDumpOtherThreads(true);
158 break;
159 }
160 } while (0);
161 }
162
ReadConfig()163 void DfxConfig::ReadConfig()
164 {
165 do {
166 FILE *fp = nullptr;
167 char codeBuffer[CONF_LINE_SIZE] = {0};
168 fp = fopen(FAULTLOGGER_CONF_PATH, "r");
169 if (fp == nullptr) {
170 break;
171 }
172 while (!feof(fp)) {
173 errno_t err = memset_s(codeBuffer, sizeof(codeBuffer), '\0', sizeof(codeBuffer));
174 if (err != EOK) {
175 DfxLogError("%s :: memset_s codeBuffer failed..", __func__);
176 }
177 if (fgets(codeBuffer, CONF_LINE_SIZE -1, fp) == nullptr) {
178 continue;
179 }
180 std::string line(codeBuffer);
181 std::string key;
182 std::string value;
183 std::string::size_type newLinePos = line.find_first_of("\n");
184 if (newLinePos != line.npos) {
185 line = line.substr(0, newLinePos);
186 }
187 std::string::size_type equalSignPos = line.find_first_of("=");
188 if (equalSignPos != line.npos) {
189 key = line.substr(0, equalSignPos);
190 value = line.substr(equalSignPos + 1);
191 Trim(key);
192 Trim(value);
193 ParserConfig(key, value);
194 }
195 }
196 (void)fclose(fp);
197 } while (0);
198 }
199
Trim(std::string & s)200 void DfxConfig::Trim(std::string& s)
201 {
202 if (s.empty()) {
203 return;
204 }
205 size_t n = s.find_first_not_of(" \r\n\t");
206 if (n != std::string::npos) {
207 s.erase(0, n);
208 }
209 n = s.find_last_not_of(" \r\n\t");
210 if (n != std::string::npos) {
211 s.erase(n + 1, s.size() - n);
212 }
213 }
214 } // namespace HiviewDFX
215 } // namespace OHOS
216