1e41f4b71Sopenharmony_ci# HiChecker Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciHiChecker is a framework provided by OpenHarmony for checking code errors and runtime results. It can be used for checking runtime errors during application and system development. This section applies only to the standard system. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Development Guidelines 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci### Use Cases 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciHiChecker is provided for you to check issues that may be easily ignored during development of OpenHarmony applications (including system-built and third-party applications). Such issues include calling of time-consuming functions by key application threads, event distribution and execution timeout in application processes, and ability resource leakage in application processes. The issues are recorded in logs or lead to process crashes explicitly so that you can notice them and take correction measures. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### Available APIs 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciHiChecker provides the APIs listed in the following table. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci **Table 1** HiChecker APIs 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| **API**| **Description**| 24e41f4b71Sopenharmony_ci| -------- | -------- | 25e41f4b71Sopenharmony_ci| uint_64_t RULE_CAUTION_PRINT_LOG<br>= 1<<63; | Defines an alarm rule, which is programmed to record a log when an alarm is generated.| 26e41f4b71Sopenharmony_ci| uint_64_t RULE_CAUTION_TRIGGER_CRASH = 1<<62; | Defines an alarm rule, which is programmed to force the application to exit when an alarm is generated.| 27e41f4b71Sopenharmony_ci| uint_64_t RULE_THREAD_CHECK_SLOW_PROCESS = 1; | Defines a check rule, which is programmed to check whether any time-consuming function is called.| 28e41f4b71Sopenharmony_ci| uint_64_t RULE_CHECK_SLOW_EVENT = 1<<32; | Defines a check rule, which is programmed to check whether the event distribution or processing time has exceeded the specified time threshold.| 29e41f4b71Sopenharmony_ci| uint_64_t RULE_CHECK_ABILITY_CONNECTION_LEAK = 1<<33; | Defines a check rule, which is programmed to check ability leakage.| 30e41f4b71Sopenharmony_ci| AddRule(uint_64_t rule) : void | Adds one or more rules. HiChecker detects unexpected operations or gives feedback based on the added rules.| 31e41f4b71Sopenharmony_ci| RemoveRule(uint_64_t rule) : void | Removes one or more rules. The removed rules will no longer take effect.| 32e41f4b71Sopenharmony_ci| GetRule() : uint_64_t | Obtains a collection of thread, process, and alarm rules that have been added.| 33e41f4b71Sopenharmony_ci| Contains(uint_64_t rule) : bool | Checks whether the collection of added rules contains a specific rule. If a thread-level rule is specified, the system only checks whether it is contained in the current thread.| 34e41f4b71Sopenharmony_ci| NotifySlowProcess(std::string tag) : void | Notifies your application of a slow process so that your application avoids calling it directly in key threads.| 35e41f4b71Sopenharmony_ci| NotifySlowEvent(std::string tag) : void | Notifies your application that event distribution or execution has timed out.| 36e41f4b71Sopenharmony_ci| NotifyAbilityConnectionLeak(Caution caution) : void | Notifies your application that AbilityConnection leakage has occurred.| 37e41f4b71Sopenharmony_ci| GetTriggerRule() : uint_64_t | Obtains the rule that triggers the current alarm.| 38e41f4b71Sopenharmony_ci| GetCautionMsg() : std::string | Obtains the alarm message.| 39e41f4b71Sopenharmony_ci| GetStackTrace() : std::string | Obtains the stack when an alarm is triggered.| 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci### Development Example 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciC++ 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci1. Include the following HiChecker header file in the code file: 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci ``` 49e41f4b71Sopenharmony_ci #include "hichecker.h" 50e41f4b71Sopenharmony_ci ``` 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci For a non-DFX subsystem, add the **HiviewDFX** field. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci using namespace OHOS::HiviewDFX; 57e41f4b71Sopenharmony_ci ``` 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci Use related APIs through static calls. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci ``` 63e41f4b71Sopenharmony_ci HiChecker::AddRule(Rule::RULE_THREAD_CHECK_SLOW_PROCESS); // Add a rule. 64e41f4b71Sopenharmony_ci HiChecker::AddRule(Rule::RULE_CHECK_SLOW_EVENT | Rule::RULE_CAUTION_PRINT_LOG); // Add multiple rules. 65e41f4b71Sopenharmony_ci HiChecker::Contains(Rule::RULE_CAUTION_PRINT_LOG); // true 66e41f4b71Sopenharmony_ci HiChecker::GetRule(); //RULE_THREAD_CHECK_SLOW_PROCESS | RULE_CHECK_SLOW_EVENT | RULE_CAUTION_PRINT_LOG 67e41f4b71Sopenharmony_ci ``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci When a rule is triggered, an alarm is generated based on the rule, and a log is recorded by default. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci - RULE_CAUTION_PRINT_LOG 72e41f4b71Sopenharmony_ci The log prints information such as the rule, thread ID, thread name, and stack that triggers the alarm. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci - RULE_CAUTION_TRIGGER_CRASH 75e41f4b71Sopenharmony_ci The process exits directly, and the log prints the exit prompt and other auxiliary information. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci Usage of the **Notify** APIs: 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci - NotifySlowProcess(std::string tag) 80e41f4b71Sopenharmony_ci Notifies your application that a slow process has been called. The following is an example of the input arguments: 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci ``` 84e41f4b71Sopenharmony_ci "threadId: xx,threadName:xx,actualTime:xx,delayTime:xx" 85e41f4b71Sopenharmony_ci ``` 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci - NotifySlowEvent(std::string tag) 88e41f4b71Sopenharmony_ci Notifies your application that event distribution or execution has timed out. The following is an example of the input arguments: 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci ``` 92e41f4b71Sopenharmony_ci "threadId: xx,threadName:xx,eventName:xx,actualTime:xx,delayTime:xx" 93e41f4b71Sopenharmony_ci ``` 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci - NotifyAbilityConnectionLeak(Caution caution) 96e41f4b71Sopenharmony_ci Notifies your application that AbilityConnection leakage has occurred. The following example shows that a **Caution** instance is passed into this API. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci Caution caution(Rule::RULE_CHECK_ABILITY_CONNECTION_LEAK , cautionMessage, stackTrace) 101e41f4b71Sopenharmony_ci // cautionMessage is similar to other Notify APIs. 102e41f4b71Sopenharmony_ci // stackTrace indicates the stack information when leakage occurs. 103e41f4b71Sopenharmony_ci ``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci2. Add the subsystem dependency to the **BUILD.gn** file that has imported the HiChecker module. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ``` 108e41f4b71Sopenharmony_ci include_dirs = [ "//base/hiviewdfx/interfaces/innerkits/libhichecker/include" ] 109e41f4b71Sopenharmony_ci external_deps = [ "hichecker_native:libhichecker" ] 110e41f4b71Sopenharmony_ci ``` 111