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 <mutex>
21 #include <securec.h>
22 #include <string>
23 #include "dfx_define.h"
24 #include "dfx_log.h"
25 #include "string_util.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 #undef LOG_DOMAIN
31 #undef LOG_TAG
32 #define LOG_DOMAIN 0xD002D11
33 #define LOG_TAG "DfxConfig"
34
35 const char FAULTLOGGER_CONF_PATH[] = "/system/etc/faultlogger.conf";
36 const int CONF_LINE_SIZE = 256;
37 }
38
GetConfig()39 DfxConfigInfo& DfxConfig::GetConfig()
40 {
41 static DfxConfigInfo config;
42 static std::once_flag flag;
43 std::call_once(flag, [&] {
44 ReadConfig(config);
45 });
46 return config;
47 }
48
ParserConfig(DfxConfigInfo & config,const std::string & key,const std::string & value)49 void DfxConfig::ParserConfig(DfxConfigInfo& config, const std::string& key, const std::string& value)
50 {
51 do {
52 if ((key.compare("faultlogLogPersist") == 0)) {
53 if (value.compare("false") == 0) {
54 config.logPersist = false;
55 } else {
56 config.logPersist = true;
57 }
58 break;
59 }
60 if (key.compare("displayRigister") == 0) {
61 if (value.compare("false") == 0) {
62 config.displayRegister = false;
63 } else {
64 config.displayRegister = true;
65 }
66 break;
67 }
68 if (key.compare("displayBacktrace") == 0) {
69 if (value.compare("false") == 0) {
70 config.displayBacktrace = false;
71 } else {
72 config.displayBacktrace = true;
73 }
74 break;
75 }
76 if (key.compare("displayMaps") == 0) {
77 if (value.compare("false") == 0) {
78 config.displayMaps = false;
79 } else {
80 config.displayMaps = true;
81 }
82 break;
83 }
84 if (key.compare("displayFaultStack.switch") == 0) {
85 if (value.compare("false") == 0) {
86 config.displayFaultStack = false;
87 } else {
88 config.displayFaultStack = true;
89 }
90 break;
91 }
92 if (key.compare("dumpOtherThreads") == 0) {
93 if (value.compare("false") == 0) {
94 config.dumpOtherThreads = false;
95 } else {
96 config.dumpOtherThreads = true;
97 }
98 break;
99 }
100 if (key.compare("displayFaultStack.lowAddressStep") == 0) {
101 unsigned int lowAddressStep = static_cast<unsigned int>(atoi(value.data()));
102 if (lowAddressStep != 0) {
103 config.lowAddressStep = lowAddressStep;
104 }
105 break;
106 }
107 if (key.compare("displayFaultStack.highAddressStep") == 0) {
108 unsigned int highAddressStep = static_cast<unsigned int>(atoi(value.data()));
109 if (highAddressStep != 0) {
110 config.highAddressStep = highAddressStep;
111 }
112 break;
113 }
114 if (key.compare("maxFrameNums") == 0) {
115 unsigned int maxFrameNums = static_cast<unsigned int>(atoi(value.data()));
116 if (maxFrameNums != 0) {
117 config.maxFrameNums = maxFrameNums;
118 }
119 break;
120 }
121 if (key.compare("writeSleepTime") == 0) {
122 unsigned int writeSleepTime = static_cast<unsigned int>(atoi(value.data()));
123 if (writeSleepTime != 0) {
124 config.writeSleepTime = writeSleepTime;
125 }
126 break;
127 }
128 } while (0);
129 }
130
ReadConfig(DfxConfigInfo & config)131 void DfxConfig::ReadConfig(DfxConfigInfo& config)
132 {
133 do {
134 FILE *fp = nullptr;
135 char codeBuffer[CONF_LINE_SIZE] = {0};
136 fp = fopen(FAULTLOGGER_CONF_PATH, "r");
137 if (fp == nullptr) {
138 break;
139 }
140 while (!feof(fp)) {
141 (void)memset_s(codeBuffer, sizeof(codeBuffer), '\0', sizeof(codeBuffer));
142 if (fgets(codeBuffer, CONF_LINE_SIZE - 1, fp) == nullptr) {
143 continue;
144 }
145 std::string line(codeBuffer);
146 std::string::size_type newLinePos = line.find_first_of("\n");
147 if (newLinePos != line.npos) {
148 line.resize(newLinePos);
149 }
150 std::string::size_type equalSignPos = line.find_first_of("=");
151 if (equalSignPos != line.npos) {
152 std::string key = line.substr(0, equalSignPos);
153 std::string value = line.substr(equalSignPos + 1);
154 Trim(key);
155 Trim(value);
156 ParserConfig(config, key, value);
157 }
158 }
159 (void)fclose(fp);
160 } while (0);
161 }
162 } // namespace HiviewDFX
163 } // namespace OHOS
164