1# Subscribing to Address Sanitizer Events (ArkTS) 2 3## Available APIs 4 5For details about how to use the APIs (such as parameter usage constraints and value ranges), see [Application Event Logging](../reference/apis-performance-analysis-kit/js-apis-hiviewdfx-hiappevent.md). 6 7| API | **Description** | 8| --------------------------------------------------- | -------------------------------------------- | 9| addWatcher(watcher: Watcher): AppEventPackageHolder | Adds a watcher to subscribe to application events.| 10| removeWatcher(watcher: Watcher): void | Removes a watcher to unsubscribe from application events.| 11 12## **How to Develop** 13 14The following describes how to subscribe to an address sanitizer event for an array bounds write. 15 161. Create a native C++ project. The directory structure is as follows: 17 18 ```yml 19 entry: 20 src: 21 main: 22 cpp: 23 - types: 24 libentry: 25 - index.d.ts 26 - CMakeLists.txt 27 - napi_init.cpp 28 ets: 29 - entryability: 30 - EntryAbility.ets 31 - pages: 32 - Index.ets 33 ``` 34 352. In the **entry/src/main/ets/entryability/EntryAbility.ets** file of the project, import the dependent modules. 36 37 ```ts 38 import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit'; 39 ``` 40 413. In the **entry/src/main/ets/entryability/EntryAbility.ets** file of the project, add a watcher in **onCreate()** to subscribe to system events. The sample code is as follows: 42 43 ```ts 44 hiAppEvent.addWatcher({ 45 // Set the watcher name. The system identifies different watchers based on their names. 46 name: "watcher", 47 // Add the system events to watch, for example, the address sanitizer event. 48 appEventFilters: [ 49 { 50 domain: hiAppEvent.domain.OS, 51 names: [hiAppEvent.event.ADDRESS_SANITIZER] 52 } 53 ], 54 // Implement a callback for the registered system event so that you can apply custom processing to the event data obtained. 55 onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => { 56 hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`); 57 for (const eventGroup of appEventGroups) { 58 // The event name uniquely identifies a system event. 59 hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`); 60 for (const eventInfo of eventGroup.appEventInfos) { 61 // Customize how to process the event data obtained, for example, print the event data in the log. 62 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.domain=${eventInfo.domain}`); 63 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.name=${eventInfo.name}`); 64 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.eventType=${eventInfo.eventType}`); 65 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.time=${eventInfo.params['time']}`); 66 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.bundle_version=${eventInfo.params['bundle_version']}`); 67 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.bundle_name=${eventInfo.params['bundle_name']}`); 68 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.pid=${eventInfo.params['pid']}`); 69 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.uid=${eventInfo.params['uid']}`); 70 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.type=${eventInfo.params['type']}`); 71 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.external_log=${JSON.stringify(eventInfo.params['external_log'])}`); 72 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.log_over_limit=${eventInfo.params['log_over_limit']}`); 73 } 74 } 75 } 76 }); 77 ``` 78 794. Edit the **entry/src/main/cpp/types/libentry/index.d.ets** file. The sample code is as follows: 80 81 ```ts 82 export const test: () => void; 83 ``` 84 855. In the **entry/src/main/cpp/napi_init.cpp** file, implement the array bounds write scenario and provide Node-API for the application layer code to call. The sample code is as follows: 86 87 ```c++ 88 #include "napi/native_api.h" 89 90 static napi_value Test(napi_env env, napi_callback_info info) 91 { 92 int a[10]; 93 // Construct the array bounds write. 94 a[10] = 1; 95 return {}; 96 } 97 98 EXTERN_C_START 99 static napi_value Init(napi_env env, napi_value exports) 100 { 101 napi_property_descriptor desc[] = { 102 { "test", nullptr, Test, nullptr, nullptr, nullptr, napi_default, nullptr } 103 }; 104 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 105 return exports; 106 } 107 EXTERN_C_END 108 109 static napi_module demoModule = { 110 .nm_version = 1, 111 .nm_flags = 0, 112 .nm_filename = nullptr, 113 .nm_register_func = Init, 114 .nm_modname = "entry", 115 .nm_priv = ((void*)0), 116 .reserved = { 0 }, 117 }; 118 119 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) 120 { 121 napi_module_register(&demoModule); 122 } 123 ``` 124 1256. In the **entry/src/main/ets/pages/Index.ets** file, add a button to trigger the address sanitizer event. 126 127 ```ts 128 import testNapi from 'libentry.so' 129 130 @Entry 131 @Component 132 struct Index { 133 build() { 134 Row() { 135 Column() { 136 Button("address-sanitizer").onClick(() => { 137 testNapi.test(); 138 }) 139 } 140 .width('100%') 141 } 142 .height('100%') 143 } 144 } 145 ``` 146 1477. In DevEco Studio, choose **entry**, click **Edit Configurations**, click **Diagnostics**, select **Address Sanitizer**, and click **OK**. Click the **Run** button to run the project. Then, click the **address-sanitizer** button to trigger an address sanitizer event. The application crashes. After restarting the application, you can view the following event information in the **Log** window. 148 149 ```text 150 HiAppEvent onReceive: domain=OS 151 HiAppEvent eventName=ADDRESS_SANITIZER 152 HiAppEvent eventInfo.domain=OS 153 HiAppEvent eventInfo.name=ADDRESS_SANITIZER 154 HiAppEvent eventInfo.eventType=1 155 HiAppEvent eventInfo.time=1713161197957 156 HiAppEvent eventInfo.bundle_version=1.0.0 157 HiAppEvent eventInfo.bundle_name=com.example.myapplication 158 HiAppEvent eventInfo.pid=12889 159 HiAppEvent eventInfo.uid=20020140 160 HiAppEvent eventInfo.type=stack-buffer-overflow 161 HiAppEvent eventInfo.external_log=["/data/storage/el2/log/hiappevent/ADDRESS_SANITIZER_1713161197960_12889.log"] 162 HiAppEvent eventInfo.log_over_limit=false 163 ``` 164