• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 panda {
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<os::memory::Mutex> lock(mutex_);
112         if (IsInitialized()) {
113             dfxController_->SetDefaultOption();
114             return;
115         }
116         dfxController_ = new DfxController(std::move(optionMap));
117     }
118 }
119 
120 /* static */
Initialize()121 void DfxController::Initialize()
122 {
123     if (IsInitialized()) {
124         dfxController_->SetDefaultOption();
125         return;
126     }
127     {
128         os::memory::LockHolder<os::memory::Mutex> lock(mutex_);
129         if (IsInitialized()) {
130             dfxController_->SetDefaultOption();
131             return;
132         }
133         std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap;
134         for (auto option = DfxOptionHandler::DfxOption(0); option < DfxOptionHandler::END_FLAG;
135              option = DfxOptionHandler::DfxOption(option + 1)) {
136             switch (option) {
137 #ifdef PANDA_TARGET_UNIX
138                 case DfxOptionHandler::COMPILER_NULLCHECK:
139                     optionMap[DfxOptionHandler::COMPILER_NULLCHECK] = 1;
140                     break;
141                 case DfxOptionHandler::REFERENCE_DUMP:
142                     optionMap[DfxOptionHandler::REFERENCE_DUMP] = 1;
143                     break;
144                 case DfxOptionHandler::SIGNAL_CATCHER:
145                     optionMap[DfxOptionHandler::SIGNAL_CATCHER] = 1;
146                     break;
147                 case DfxOptionHandler::SIGNAL_HANDLER:
148                     optionMap[DfxOptionHandler::SIGNAL_HANDLER] = 1;
149                     break;
150                 case DfxOptionHandler::ARK_SIGQUIT:
151                     optionMap[DfxOptionHandler::ARK_SIGQUIT] = 1;
152                     break;
153                 case DfxOptionHandler::ARK_SIGUSR1:
154                     optionMap[DfxOptionHandler::ARK_SIGUSR1] = 1;
155                     break;
156                 case DfxOptionHandler::ARK_SIGUSR2:
157                     optionMap[DfxOptionHandler::ARK_SIGUSR2] = 1;
158                     break;
159                 case DfxOptionHandler::MOBILE_LOG:
160                     optionMap[DfxOptionHandler::MOBILE_LOG] = 1;
161                     break;
162                 case DfxOptionHandler::HUNG_UPDATE:
163                     optionMap[DfxOptionHandler::HUNG_UPDATE] = 0;
164                     break;
165 #endif
166                 case DfxOptionHandler::DFXLOG:
167                     optionMap[DfxOptionHandler::DFXLOG] = 0;
168                     break;
169                 default:
170                     break;
171             }
172         }
173         dfxController_ = new DfxController(std::move(optionMap));
174     }
175 }
176 
177 /* static */
Destroy()178 void DfxController::Destroy()
179 {
180     if (!IsInitialized()) {
181         return;
182     }
183     DfxController *d = nullptr;
184     {
185         os::memory::LockHolder<os::memory::Mutex> lock(mutex_);
186         if (!IsInitialized()) {
187             return;
188         }
189         d = dfxController_;
190         dfxController_ = nullptr;
191     }
192     delete d;
193 }
194 
195 }  // namespace panda
196