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