1 /*
2 * Copyright (c) 2021-2024 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.h"
17 #include "logger.h"
18
19 namespace ark {
20 DfxController *DfxController::dfxController_ = nullptr;
21 os::memory::Mutex DfxController::mutex_; // NOLINT(fuchsia-statically-constructed-objects)
22
23 /* static */
SetDefaultOption()24 void DfxController::SetDefaultOption()
25 {
26 ASSERT(IsInitialized());
27 for (auto option = DfxOptionHandler::DfxOption(0); option < DfxOptionHandler::END_FLAG;
28 option = DfxOptionHandler::DfxOption(option + 1)) {
29 switch (option) {
30 #ifdef PANDA_TARGET_UNIX
31 case DfxOptionHandler::COMPILER_NULLCHECK:
32 dfxController_->optionMap_[DfxOptionHandler::COMPILER_NULLCHECK] = 1;
33 break;
34 case DfxOptionHandler::REFERENCE_DUMP:
35 dfxController_->optionMap_[DfxOptionHandler::REFERENCE_DUMP] = 1;
36 break;
37 case DfxOptionHandler::SIGNAL_CATCHER:
38 dfxController_->optionMap_[DfxOptionHandler::SIGNAL_CATCHER] = 1;
39 break;
40 case DfxOptionHandler::SIGNAL_HANDLER:
41 dfxController_->optionMap_[DfxOptionHandler::SIGNAL_HANDLER] = 1;
42 break;
43 case DfxOptionHandler::ARK_SIGQUIT:
44 dfxController_->optionMap_[DfxOptionHandler::ARK_SIGQUIT] = 1;
45 break;
46 case DfxOptionHandler::ARK_SIGUSR1:
47 dfxController_->optionMap_[DfxOptionHandler::ARK_SIGUSR1] = 1;
48 break;
49 case DfxOptionHandler::ARK_SIGUSR2:
50 dfxController_->optionMap_[DfxOptionHandler::ARK_SIGUSR2] = 1;
51 break;
52 case DfxOptionHandler::MOBILE_LOG:
53 dfxController_->optionMap_[DfxOptionHandler::MOBILE_LOG] = 1;
54 break;
55 case DfxOptionHandler::HUNG_UPDATE:
56 dfxController_->optionMap_[DfxOptionHandler::HUNG_UPDATE] = 0;
57 break;
58 #endif
59 case DfxOptionHandler::DFXLOG:
60 dfxController_->optionMap_[DfxOptionHandler::DFXLOG] = 0;
61 break;
62 default:
63 break;
64 }
65 }
66 }
67
68 /* static */
ResetOptionValueFromString(const std::string & s)69 void DfxController::ResetOptionValueFromString(const std::string &s)
70 {
71 size_t lastPos = s.find_first_not_of(';', 0);
72 size_t pos = s.find(';', lastPos);
73 while (lastPos != std::string::npos) {
74 std::string arg = s.substr(lastPos, pos - lastPos);
75 lastPos = s.find_first_not_of(';', pos);
76 pos = s.find(';', lastPos);
77 std::string optionStr = arg.substr(0, arg.find(':'));
78 uint8_t value = static_cast<uint8_t>(std::stoi(arg.substr(arg.find(':') + 1)));
79 auto dfxOption = DfxOptionHandler::DfxOptionFromString(optionStr);
80 if (dfxOption != DfxOptionHandler::END_FLAG) {
81 DfxController::SetOptionValue(dfxOption, value);
82 #ifdef PANDA_TARGET_UNIX
83 if (dfxOption == DfxOptionHandler::MOBILE_LOG) {
84 Logger::SetMobileLogOpenFlag(value != 0);
85 }
86 #endif
87 } else {
88 LOG(ERROR, DFX) << "Unknown Option " << optionStr;
89 }
90 }
91 }
92
93 /* static */
PrintDfxOptionValues()94 void DfxController::PrintDfxOptionValues()
95 {
96 ASSERT(IsInitialized());
97 for (auto &iter : dfxController_->optionMap_) {
98 LOG(ERROR, DFX) << "DFX option: " << DfxOptionHandler::StringFromDfxOption(iter.first)
99 << ", option values: " << std::to_string(iter.second);
100 }
101 }
102
103 /* static */
Initialize(std::map<DfxOptionHandler::DfxOption,uint8_t> optionMap)104 void DfxController::Initialize(std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap)
105 {
106 if (IsInitialized()) {
107 dfxController_->SetDefaultOption();
108 return;
109 }
110 {
111 os::memory::LockHolder lock(mutex_);
112 if (IsInitialized()) {
113 dfxController_->SetDefaultOption();
114 return;
115 }
116 dfxController_ = new DfxController(std::move(optionMap));
117 }
118 }
119
SetOption()120 std::map<DfxOptionHandler::DfxOption, uint8_t> SetOption()
121 {
122 std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap;
123 for (auto option = DfxOptionHandler::DfxOption(0); option < DfxOptionHandler::END_FLAG;
124 option = DfxOptionHandler::DfxOption(option + 1)) {
125 switch (option) {
126 #ifdef PANDA_TARGET_UNIX
127 case DfxOptionHandler::COMPILER_NULLCHECK:
128 optionMap[DfxOptionHandler::COMPILER_NULLCHECK] = 1;
129 break;
130 case DfxOptionHandler::REFERENCE_DUMP:
131 optionMap[DfxOptionHandler::REFERENCE_DUMP] = 1;
132 break;
133 case DfxOptionHandler::SIGNAL_CATCHER:
134 optionMap[DfxOptionHandler::SIGNAL_CATCHER] = 1;
135 break;
136 case DfxOptionHandler::SIGNAL_HANDLER:
137 optionMap[DfxOptionHandler::SIGNAL_HANDLER] = 1;
138 break;
139 case DfxOptionHandler::ARK_SIGQUIT:
140 optionMap[DfxOptionHandler::ARK_SIGQUIT] = 1;
141 break;
142 case DfxOptionHandler::ARK_SIGUSR1:
143 optionMap[DfxOptionHandler::ARK_SIGUSR1] = 1;
144 break;
145 case DfxOptionHandler::ARK_SIGUSR2:
146 optionMap[DfxOptionHandler::ARK_SIGUSR2] = 1;
147 break;
148 case DfxOptionHandler::MOBILE_LOG:
149 optionMap[DfxOptionHandler::MOBILE_LOG] = 1;
150 break;
151 case DfxOptionHandler::HUNG_UPDATE:
152 optionMap[DfxOptionHandler::HUNG_UPDATE] = 0;
153 break;
154 #endif
155 case DfxOptionHandler::DFXLOG:
156 optionMap[DfxOptionHandler::DFXLOG] = 0;
157 break;
158 default:
159 break;
160 }
161 }
162 return optionMap;
163 }
164
165 /* static */
Initialize()166 void DfxController::Initialize()
167 {
168 if (IsInitialized()) {
169 dfxController_->SetDefaultOption();
170 return;
171 }
172 {
173 os::memory::LockHolder lock(mutex_);
174 if (IsInitialized()) {
175 dfxController_->SetDefaultOption();
176 return;
177 }
178
179 dfxController_ = new DfxController(SetOption());
180 }
181 }
182
183 /* static */
Destroy()184 void DfxController::Destroy()
185 {
186 if (!IsInitialized()) {
187 return;
188 }
189 DfxController *d = nullptr;
190 {
191 os::memory::LockHolder lock(mutex_);
192 if (!IsInitialized()) {
193 return;
194 }
195 d = dfxController_;
196 dfxController_ = nullptr;
197 }
198 delete d;
199 }
200
201 } // namespace ark
202