1# Using HiChecker (ArkTS) 2 3## Overview 4 5HiChecker is provided to check issues that may be easily ignored during application development. Such issues include time-consuming thread calling and ability resource leakage in application processes. The issues are recorded in logs or lead to process crashes so that you can find and rectify them. 6 7## Basic Concepts 8 9**Rule constants**: HiChecker uses constants to indicate existing alarm rules or check rules. A constant indicates a rule. You can add or delete rules using HiChecker APIs. 10 11## Working Principles 12 131. The application calls HiChecker APIs to add, remove, query, and modify rules. 14 152. When a time-consuming call or ability resource leakage occurs, HiChecker reports an event based on the rule triggered. 16 17## Constraints 18 19- Currently, the alarm rules supports only logs (default) and app crashes. 20- HiChecker supports stack unwinding in C but not in JavaScript. 21 22## Available APIs 23 24The check APIs are provided by the HiChecker module. For details about the APIs, see [HiChecker](../reference/apis-performance-analysis-kit/js-apis-hichecker.md). 25 26| API | Description | 27| -------- | -------- | 28| hichecker.addCheckRule(rule: bigint) | Adds a rule. | 29| hichecker.removeCheckRule(rule: bigint) | Removes a rule. | 30| hichecker.containsCheckRule(rule: bigint) | Queries a rule. | 31 32## How to Develop 33 34After the application startup execution page is loaded, the check starts. After the service is complete, the check stops. 35 361. Create an ArkTS application project. In the **Project** window, click **entry > src > main > ets > entryability** to open the **EntryAbility.ets** file. After the page is loaded, call the HiChecker to add check rules. The sample code is as follows: 37 38 ```ts 39 import { window } from '@kit.ArkUI'; 40 import { image } from '@kit.ImageKit'; 41 import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 42 import { hichecker, hilog } from '@kit.PerformanceAnalysisKit'; 43 44 export default class EntryAbility extends UIAbility { 45 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 46 // Add a check rule. For details about the rule, see HiChecker. 47 hichecker.addCheckRule(hichecker.RULE_CAUTION_PRINT_LOG|hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); 48 let filePath: string = this.context.cacheDir + '/test.JPG'; 49 const imageSourceApi: image.ImageSource = image.createImageSource(filePath); 50 const imagePackerApi = image.createImagePacker(); 51 let packOpts: image.PackingOption = { format:"image/jpeg", quality:98 }; 52 imagePackerApi.packing(imageSourceApi, packOpts); 53 // The preceding codes trigger the check rule through the image subsystem. 54 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreateend'); 55 } 56 57 onDestroy() { 58 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 59 } 60 61 onWindowStageCreate(windowStage: window.WindowStage) { 62 // Main window is created, set main page for this ability 63 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 64 65 windowStage.loadContent('pages/Index', (err, data) => { 66 if (err.code) { 67 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 68 return; 69 } 70 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 71 }); 72 } 73 74 onWindowStageDestroy() { 75 // Main window is destroyed, and UI related resources are released. 76 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 77 } 78 79 onForeground() { 80 // Ability is brought to foreground. 81 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 82 } 83 84 onBackground() { 85 // Ability is back to background. 86 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 87 } 88 } 89 ``` 90 912. Run the following commands in sequence in the shell: 92 93 ```shell 94 hdc shell 95 hilog|grep -i hichecker 96 ``` 97 98 After the HAP is installed, the check begins. If the following call stack information is displayed in the shell window, the check is successful (The call stack triggers the check rule). 99 100 ```shell 101 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: StackTrace: 102 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #00 pc 00003d33 /system/lib/chipset-pub-sdk/libbacktrace_local.so(OHOS::HiviewDFX::GetBacktrace(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&, bool, unsigned int)+90)(7c7c81050ce2a86bf0a22943c25773db) 103 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #01 pc 00003a57 /system/lib/chipset-pub-sdk/libdfx_dumpcatcher.z.so(OHOS::HiviewDFX::DfxDumpCatcher::DoDumpCurrTid(unsigned int, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&, unsigned int)+26)(8e2b59ecc6cc00dd2135f9d84c23a724) 104 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #02 pc 000042a1 /system/lib/chipset-pub-sdk/libdfx_dumpcatcher.z.so(OHOS::HiviewDFX::DfxDumpCatcher::DoDumpLocalLocked(int, int, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&, unsigned int)+40)(8e2b59ecc6cc00dd2135f9d84c23a724) 105 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #03 pc 0000452d /system/lib/chipset-pub-sdk/libdfx_dumpcatcher.z.so(OHOS::HiviewDFX::DfxDumpCatcher::DumpCatch(int, int, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&, unsigned int, bool)+188)(8e2b59ecc6cc00dd2135f9d84c23a724) 106 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #04 pc 00002cd7 /system/lib/platformsdk/libhichecker.so(OHOS::HiviewDFX::HiChecker::NotifySlowProcess(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&)+90)(03f34822b1baca8c33fb87a2cd1817cd) 107 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #05 pc 0004045d /system/lib/platformsdk/libimage_napi.z.so(f765998ba1f5185405f9735d86b0c7b7) 108 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #06 pc 00040f43 /system/lib/platformsdk/libimage_napi.z.so(f765998ba1f5185405f9735d86b0c7b7) 109 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #07 pc 0002b0c5 /system/lib/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+448)(a84fbb767fd826946623779c608395bf) 110 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #08 pc 001e7597 /system/lib/platformsdk/libark_jsruntime.so(panda::ecmascript::EcmaInterpreter::RunInternal(panda::ecmascript::JSThread*, unsigned char const*, unsigned long long*)+14710)(106c552f6ce4420b9feac95e8b21b792) 111 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #09 pc 001e043b /system/lib/platformsdk/libark_jsruntime.so(panda::ecmascript::EcmaInterpreter::Execute(panda::ecmascript::EcmaRuntimeCallInfo*)+986)(106c552f6ce4420b9feac95e8b21b792) 112 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #10 pc 0029ec99 /system/lib/platformsdk/libark_jsruntime.so(panda::ecmascript::JSFunction::Call(panda::ecmascript::EcmaRuntimeCallInfo*)+388)(106c552f6ce4420b9feac95e8b21b792) 113 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #11 pc 0034b14b /system/lib/platformsdk/libark_jsruntime.so(panda::FunctionRef::CallForNapi(panda::ecmascript::EcmaVM const*, panda::JSValueRef*, panda::JSValueRef* const*, int)+918)(106c552f6ce4420b9feac95e8b21b792) 114 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #12 pc 0003d343 /system/lib/platformsdk/libace_napi.z.so(napi_call_function+198)(a84fbb767fd826946623779c608395bf) 115 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #13 pc 000283e1 /system/lib/platformsdk/libuiabilitykit_native.z.so(OHOS::AbilityRuntime::JsUIAbility::CallObjectMethod(char const*, napi_value__* const*, unsigned int, bool, bool)+648)(118b2c16051d37927837ed0ce7874f1d) 116 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #14 pc 00027c0d /system/lib/platformsdk/libuiabilitykit_native.z.so(OHOS::AbilityRuntime::JsUIAbility::OnStart(OHOS::AAFwk::Want const&, OHOS::sptr<OHOS::AAFwk::SessionInfo>)+616)(118b2c16051d37927837ed0ce7874f1d) 117 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #15 pc 000548fd /system/lib/platformsdk/libuiabilitykit_native.z.so(OHOS::AbilityRuntime::UIAbilityImpl::Start(OHOS::AAFwk::Want const&, OHOS::sptr<OHOS::AAFwk::SessionInfo>)+396)(118b2c16051d37927837ed0ce7874f1d) 118 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #16 pc 0005549d /system/lib/platformsdk/libuiabilitykit_native.z.so(OHOS::AbilityRuntime::UIAbilityImpl::HandleAbilityTransaction(OHOS::AAFwk::Want const&, OHOS::AAFwk::LifeCycleStateInfo const&, OHOS::sptr<OHOS::AAFwk::SessionInfo>)+764)(118b2c16051d37927837ed0ce7874f1d) 119 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #17 pc 000359b3 /system/lib/platformsdk/libability_thread.z.so(OHOS::AbilityRuntime::UIAbilityThread::HandleAbilityTransaction(OHOS::AAFwk::Want const&, OHOS::AAFwk::LifeCycleStateInfo const&, OHOS::sptr<OHOS::AAFwk::SessionInfo>)+466)(889aa126c2fb643cd2ec6dbd64adddd1) 120 08-05 23:11:07.206 1799 1799 I C02d0b/HICHECKER: #18 pc 00039c2d /system/lib/platformsdk/libability_thread.z.so(889aa126c2fb643cd2ec6dbd64adddd1) 121 ``` 122