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