1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include "dfx_config.h"
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cstdio>
19800b99b8Sopenharmony_ci#include <cstdlib>
20800b99b8Sopenharmony_ci#include <mutex>
21800b99b8Sopenharmony_ci#include <securec.h>
22800b99b8Sopenharmony_ci#include <string>
23800b99b8Sopenharmony_ci#include "dfx_define.h"
24800b99b8Sopenharmony_ci#include "dfx_log.h"
25800b99b8Sopenharmony_ci#include "string_util.h"
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_cinamespace OHOS {
28800b99b8Sopenharmony_cinamespace HiviewDFX {
29800b99b8Sopenharmony_cinamespace {
30800b99b8Sopenharmony_ci#undef LOG_DOMAIN
31800b99b8Sopenharmony_ci#undef LOG_TAG
32800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11
33800b99b8Sopenharmony_ci#define LOG_TAG "DfxConfig"
34800b99b8Sopenharmony_ci
35800b99b8Sopenharmony_ciconst char FAULTLOGGER_CONF_PATH[] = "/system/etc/faultlogger.conf";
36800b99b8Sopenharmony_ciconst int CONF_LINE_SIZE = 256;
37800b99b8Sopenharmony_ci}
38800b99b8Sopenharmony_ci
39800b99b8Sopenharmony_ciDfxConfigInfo& DfxConfig::GetConfig()
40800b99b8Sopenharmony_ci{
41800b99b8Sopenharmony_ci    static DfxConfigInfo config;
42800b99b8Sopenharmony_ci    static std::once_flag flag;
43800b99b8Sopenharmony_ci    std::call_once(flag, [&] {
44800b99b8Sopenharmony_ci        ReadConfig(config);
45800b99b8Sopenharmony_ci    });
46800b99b8Sopenharmony_ci    return config;
47800b99b8Sopenharmony_ci}
48800b99b8Sopenharmony_ci
49800b99b8Sopenharmony_civoid DfxConfig::ParserConfig(DfxConfigInfo& config, const std::string& key, const std::string& value)
50800b99b8Sopenharmony_ci{
51800b99b8Sopenharmony_ci    do {
52800b99b8Sopenharmony_ci        if (key.compare("displayRigister") == 0) {
53800b99b8Sopenharmony_ci            if (value.compare("false") == 0) {
54800b99b8Sopenharmony_ci                config.displayRegister = false;
55800b99b8Sopenharmony_ci            } else {
56800b99b8Sopenharmony_ci                config.displayRegister = true;
57800b99b8Sopenharmony_ci            }
58800b99b8Sopenharmony_ci            break;
59800b99b8Sopenharmony_ci        }
60800b99b8Sopenharmony_ci        if (key.compare("displayBacktrace") == 0) {
61800b99b8Sopenharmony_ci            if (value.compare("false") == 0) {
62800b99b8Sopenharmony_ci                config.displayBacktrace = false;
63800b99b8Sopenharmony_ci            } else {
64800b99b8Sopenharmony_ci                config.displayBacktrace = true;
65800b99b8Sopenharmony_ci            }
66800b99b8Sopenharmony_ci            break;
67800b99b8Sopenharmony_ci        }
68800b99b8Sopenharmony_ci        if (key.compare("displayMaps") == 0) {
69800b99b8Sopenharmony_ci            if (value.compare("false") == 0) {
70800b99b8Sopenharmony_ci                config.displayMaps = false;
71800b99b8Sopenharmony_ci            } else {
72800b99b8Sopenharmony_ci                config.displayMaps = true;
73800b99b8Sopenharmony_ci            }
74800b99b8Sopenharmony_ci            break;
75800b99b8Sopenharmony_ci        }
76800b99b8Sopenharmony_ci        if (key.compare("displayFaultStack.switch") == 0) {
77800b99b8Sopenharmony_ci            if (value.compare("false") == 0) {
78800b99b8Sopenharmony_ci                config.displayFaultStack = false;
79800b99b8Sopenharmony_ci            } else {
80800b99b8Sopenharmony_ci                config.displayFaultStack = true;
81800b99b8Sopenharmony_ci            }
82800b99b8Sopenharmony_ci            break;
83800b99b8Sopenharmony_ci        }
84800b99b8Sopenharmony_ci        if (key.compare("dumpOtherThreads") == 0) {
85800b99b8Sopenharmony_ci            if (value.compare("false") == 0) {
86800b99b8Sopenharmony_ci                config.dumpOtherThreads = false;
87800b99b8Sopenharmony_ci            } else {
88800b99b8Sopenharmony_ci                config.dumpOtherThreads = true;
89800b99b8Sopenharmony_ci            }
90800b99b8Sopenharmony_ci            break;
91800b99b8Sopenharmony_ci        }
92800b99b8Sopenharmony_ci        if (key.compare("displayFaultStack.lowAddressStep") == 0) {
93800b99b8Sopenharmony_ci            unsigned int lowAddressStep = static_cast<unsigned int>(atoi(value.data()));
94800b99b8Sopenharmony_ci            if (lowAddressStep != 0) {
95800b99b8Sopenharmony_ci                config.lowAddressStep = lowAddressStep;
96800b99b8Sopenharmony_ci            }
97800b99b8Sopenharmony_ci            break;
98800b99b8Sopenharmony_ci        }
99800b99b8Sopenharmony_ci        if (key.compare("displayFaultStack.highAddressStep") == 0) {
100800b99b8Sopenharmony_ci            unsigned int highAddressStep = static_cast<unsigned int>(atoi(value.data()));
101800b99b8Sopenharmony_ci            if (highAddressStep != 0) {
102800b99b8Sopenharmony_ci                config.highAddressStep = highAddressStep;
103800b99b8Sopenharmony_ci            }
104800b99b8Sopenharmony_ci            break;
105800b99b8Sopenharmony_ci        }
106800b99b8Sopenharmony_ci        if (key.compare("maxFrameNums") == 0) {
107800b99b8Sopenharmony_ci            unsigned int maxFrameNums = static_cast<unsigned int>(atoi(value.data()));
108800b99b8Sopenharmony_ci            if (maxFrameNums != 0) {
109800b99b8Sopenharmony_ci                config.maxFrameNums = maxFrameNums;
110800b99b8Sopenharmony_ci            }
111800b99b8Sopenharmony_ci            break;
112800b99b8Sopenharmony_ci        }
113800b99b8Sopenharmony_ci    } while (0);
114800b99b8Sopenharmony_ci}
115800b99b8Sopenharmony_ci
116800b99b8Sopenharmony_civoid DfxConfig::ReadConfig(DfxConfigInfo& config)
117800b99b8Sopenharmony_ci{
118800b99b8Sopenharmony_ci    do {
119800b99b8Sopenharmony_ci        FILE *fp = nullptr;
120800b99b8Sopenharmony_ci        char codeBuffer[CONF_LINE_SIZE] = {0};
121800b99b8Sopenharmony_ci        fp = fopen(FAULTLOGGER_CONF_PATH, "r");
122800b99b8Sopenharmony_ci        if (fp == nullptr) {
123800b99b8Sopenharmony_ci            break;
124800b99b8Sopenharmony_ci        }
125800b99b8Sopenharmony_ci        while (!feof(fp)) {
126800b99b8Sopenharmony_ci            (void)memset_s(codeBuffer, sizeof(codeBuffer), '\0', sizeof(codeBuffer));
127800b99b8Sopenharmony_ci            if (fgets(codeBuffer, CONF_LINE_SIZE - 1, fp) == nullptr) {
128800b99b8Sopenharmony_ci                continue;
129800b99b8Sopenharmony_ci            }
130800b99b8Sopenharmony_ci            std::string line(codeBuffer);
131800b99b8Sopenharmony_ci            std::string::size_type newLinePos = line.find_first_of("\n");
132800b99b8Sopenharmony_ci            if (newLinePos != line.npos) {
133800b99b8Sopenharmony_ci                line.resize(newLinePos);
134800b99b8Sopenharmony_ci            }
135800b99b8Sopenharmony_ci            std::string::size_type equalSignPos = line.find_first_of("=");
136800b99b8Sopenharmony_ci            if (equalSignPos != line.npos) {
137800b99b8Sopenharmony_ci                std::string key = line.substr(0, equalSignPos);
138800b99b8Sopenharmony_ci                std::string value = line.substr(equalSignPos + 1);
139800b99b8Sopenharmony_ci                Trim(key);
140800b99b8Sopenharmony_ci                Trim(value);
141800b99b8Sopenharmony_ci                ParserConfig(config, key, value);
142800b99b8Sopenharmony_ci            }
143800b99b8Sopenharmony_ci        }
144800b99b8Sopenharmony_ci        (void)fclose(fp);
145800b99b8Sopenharmony_ci    } while (0);
146800b99b8Sopenharmony_ci}
147800b99b8Sopenharmony_ci} // namespace HiviewDFX
148800b99b8Sopenharmony_ci} // namespace OHOS
149