• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 1024;
37 }
38 
GetConfig()39 DfxConfigInfo& DfxConfig::GetConfig()
40 {
41     static DfxConfigInfo config;
42     static std::once_flag initFlag;
43     std::call_once(initFlag, [&] {
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     } while (0);
122 }
123 
ReadConfig(DfxConfigInfo & config)124 void DfxConfig::ReadConfig(DfxConfigInfo& config)
125 {
126     do {
127         FILE *fp = nullptr;
128         char codeBuffer[CONF_LINE_SIZE] = {0};
129         fp = fopen(FAULTLOGGER_CONF_PATH, "r");
130         if (fp == nullptr) {
131             break;
132         }
133         while (!feof(fp)) {
134             (void)memset_s(codeBuffer, sizeof(codeBuffer), '\0', sizeof(codeBuffer));
135             if (fgets(codeBuffer, CONF_LINE_SIZE - 1, fp) == nullptr) {
136                 continue;
137             }
138             std::string line(codeBuffer);
139             std::string::size_type newLinePos = line.find_first_of("\n");
140             if (newLinePos != line.npos) {
141                 line.resize(newLinePos);
142             }
143             std::string::size_type equalSignPos = line.find_first_of("=");
144             if (equalSignPos != line.npos) {
145                 std::string key = line.substr(0, equalSignPos);
146                 std::string value = line.substr(equalSignPos + 1);
147                 Trim(key);
148                 Trim(value);
149                 ParserConfig(config, key, value);
150             }
151         }
152         (void)fclose(fp);
153     } while (0);
154 }
155 } // namespace HiviewDFX
156 } // namespace OHOS
157