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