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