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 // DfxController is designed to help developers enable/disable or set level of some DFX capabilities. 16 // 17 // Developers can change the option value of each capability included in DfxOptionHandler::DfxOption. 18 // 19 // DFX Capabilities Supported: 20 // |-----------------------------------------------------------------------------------------------------------------| 21 // | Dfx Option | Ark Option | Prop `ark.dfx.options` | How option values work | 22 // |--------------------|--------------------|------------------------|----------------------------------------------| 23 // | COMPILER_NULLCHECK | compiler-nullcheck | compiler-nullcheck | 0/1(default) compiler implicit null check | 24 // | REFERENCE_DUMP | reference-dump | reference-dump | 0/1(default) dump refs statistic info | 25 // | SIGNAL_CATCHER | signal-catcher | signal-catcher | 0/1(default) signal catcher | 26 // | SIGNAL_HANDLER | signal-handler | signal-handler | 0/1(default) signal handler | 27 // | ARKSIGQUIT | sigquit-flag | sigquit | 0/1(default) sigquit action | 28 // | ARKSIGUSR1 | sigusr1-flag | sigusr1 | 0/1(default) sigusr1 action | 29 // | ARKSIGUSR2 | sigusr2-flag | sigusr2 | 0/1(default) sigusr2 action | 30 // | MOBILE_LOG | mlog-flag | mlog | 0/1(default) ark log printed in mlog | 31 // | DFXLOG | dfx-log | dfx-log | 0(default)/1 ark dfx log | 32 // |--------------------|--------------------|------------------------|----------------------------------------------| 33 // Note: if option value can only be set to 0/1, it means disable/enable, else enable in multiple levels. 34 // 35 // For developers who want to controll your own DFX capability, add your dfx option in DfxOptionHandler::DfxOption, 36 // improve code related to DfxController initialize and runtime options. 37 #ifndef PANDA_LIBPANDABASE_UTILS_DFX_H 38 #define PANDA_LIBPANDABASE_UTILS_DFX_H 39 40 #include "os/mutex.h" 41 #include "os/dfx_option.h" 42 43 #include <cstdint> 44 #include <map> 45 46 #include <atomic> 47 48 namespace panda { 49 50 using DfxOptionHandler = os::dfx_option::DfxOptionHandler; 51 52 class DfxController { 53 public: 54 static void Initialize(std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap); 55 56 PANDA_PUBLIC_API static void Initialize(); 57 IsInitialized()58 static bool IsInitialized() 59 { 60 return dfxController_ != nullptr; 61 } 62 63 PANDA_PUBLIC_API static void Destroy(); 64 GetOptionValue(DfxOptionHandler::DfxOption dfxOption)65 static uint8_t GetOptionValue(DfxOptionHandler::DfxOption dfxOption) 66 { 67 ASSERT(IsInitialized()); 68 return dfxController_->optionMap_[dfxOption]; 69 } 70 SetOptionValue(DfxOptionHandler::DfxOption dfxOption,uint8_t value)71 static void SetOptionValue(DfxOptionHandler::DfxOption dfxOption, uint8_t value) 72 { 73 ASSERT(IsInitialized()); 74 dfxController_->optionMap_[dfxOption] = value; 75 } 76 77 PANDA_PUBLIC_API static void PrintDfxOptionValues(); 78 79 PANDA_PUBLIC_API static void ResetOptionValueFromString(const std::string &s); 80 81 private: 82 static void SetDefaultOption(); 83 DfxController(std::map<DfxOptionHandler::DfxOption,uint8_t> optionMap)84 explicit DfxController(std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap) : optionMap_(std::move(optionMap)) 85 { 86 } 87 88 ~DfxController() = default; 89 90 std::map<DfxOptionHandler::DfxOption, uint8_t> optionMap_; 91 PANDA_PUBLIC_API static DfxController *dfxController_; 92 93 static os::memory::Mutex mutex_; 94 95 NO_COPY_SEMANTIC(DfxController); 96 NO_MOVE_SEMANTIC(DfxController); 97 }; 98 99 } // namespace panda 100 101 #endif // PANDA_LIBPANDABASE_UTILS_DFX_H 102