• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 订阅应用查杀事件(ArkTS)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @shead-master-->
6<!--Designer: @peterhuangyu-->
7<!--Tester: @gcw_KuLfPSbe-->
8<!--Adviser: @foryourself-->
9
10## 应用查杀事件规格说明
11
12请参考[应用查杀事件介绍](./hiappevent-watcher-app-killed-events.md)。
13
14## 接口说明
15
16API接口的具体使用说明(参数使用限制、具体取值范围等)请参考[HiAppEvent](../reference/apis-performance-analysis-kit/capi-hiappevent-h.md)。
17
18| 接口名                                              | 描述                                         |
19| --------------------------------------------------- | -------------------------------------------- |
20| addWatcher(watcher: Watcher): AppEventPackageHolder | 添加应用事件观察者,以添加对应用事件的订阅。 |
21| removeWatcher(watcher: Watcher): void               | 移除应用事件观察者,以移除对应用事件的订阅。 |
22
23## 开发步骤
24
25为确保开发阶段顺利接收事件回调,建议采用以下方案:创建新的Native C++工程,在ArkTs代码中实现订阅,搭配C++代码的故障注入代码构造故障以触发应用查杀事件。
26
271. 编辑工程中的“entry > src > main > ets  > entryability > EntryAbility.ets”文件,导入依赖模块:
28
29   ```ts
30   import { hiAppEvent } from '@kit.PerformanceAnalysisKit';
31   ```
32
332. 编辑工程中的“entry > src > main > ets  > entryability > EntryAbility.ets”文件,在onCreate函数中添加系统事件的订阅,示例代码如下:
34
35   ```ts
36   hiAppEvent.addWatcher({
37     // 开发者可以自定义观察者名称,系统会使用名称来标识不同的观察者
38     name: "watcher",
39     // 开发者可以订阅感兴趣的系统事件,此处是订阅了应用查杀事件
40     appEventFilters: [
41       {
42         domain: hiAppEvent.domain.OS,
43         names: [hiAppEvent.event.APP_KILLED]
44       }
45     ],
46     // 开发者可以自行实现订阅实时回调函数,以便对订阅获取到的事件数据进行自定义处理
47     onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => {
48       hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`);
49       for (const eventGroup of appEventGroups) {
50         // 开发者可以根据事件集合中的事件名称区分不同的系统事件
51         hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`);
52         for (const eventInfo of eventGroup.appEventInfos) {
53           // 开发者可以对事件集合中的事件数据进行自定义处理,此处是将事件数据打印在日志中
54           hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.domain=${eventInfo.domain}`);
55           hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.name=${eventInfo.name}`);
56           // 开发者可以获取到应用查杀事件发生的时间戳
57           hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.time=${eventInfo.params['time']}`);
58           // 开发者可以获取到应用的前后台状态
59           hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.foreground=${eventInfo.params['foreground']}`);
60           // 开发者可以获取到应用查杀事件发生的原因
61           hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.params.reason=${eventInfo.params['reason']}`);
62         }
63       }
64     }
65   });
66   ```
67
683. 以下为故障注入功能,需要使用C++代码实现,编辑"napi_init.cpp",新增以下代码:
69
70   ```C++
71   #include <thread>
72
73   static void NativeLeak()
74   {
75       constexpr int leak_size_per_time = 500000;
76       while (true) {
77           char *p = (char *)malloc(leak_size_per_time + 1);
78           if (!p) {
79               break;
80           }
81           memset(p, 'a', leak_size_per_time);
82           std::this_thread::sleep_for(std::chrono::milliseconds(10));
83       }
84   }
85
86   static napi_value Leak(napi_env env, napi_callback_info info) {
87   	std::thread t1(NativeLeak);
88   	t1.detach();
89       return {};
90   }
91   ```
92
934. 编辑"napi_init.cpp"文件,将Leak注册为ArkTS接口:
94
95   ```c++
96   static napi_value Init(napi_env env, napi_value exports)
97   {
98       napi_property_descriptor desc[] = {
99           { "leak", nullptr, Leak, nullptr, nullptr, nullptr, napi_default, nullptr }, // 新增这行
100       };
101       napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
102       return exports;
103   }
104   ```
105
1065. 编辑"index.d.ts"文件,定义ArkTS接口:
107
108   ```ts
109   export const leak: () => void;
110   ```
111
1126. 编辑工程中的“entry > src > main > ets  > pages > Index.ets”文件,在build下增加OnClick功能,并调用Leak接口的示例代码:
113
114   ```ts
115   import { hilog } from '@kit.PerformanceAnalysisKit';
116   import testNapi from 'libentry.so';
117
118   const DOMAIN = 0x0000;
119
120   @Entry
121   @Component
122   struct Index {
123     @State message: string = 'Start To Leak';
124
125     build() {
126       Row() {
127         Column() {
128           Text(this.message)
129             .fontSize($r('app.float.page_text_font_size'))
130             .fontWeight(FontWeight.Bold)
131             .onClick(() => {
132               if (this.message != 'Leaking') {
133                 this.message = 'Leaking';
134                 hilog.info(DOMAIN, 'testTag', 'Start leaking');
135                 testNapi.leak();
136               }
137             })
138         }
139         .width('100%')
140       }
141       .height('100%')
142     }
143   }
144   ```
145
1467. 点击DevEco Studio界面中的运行按钮,运行应用工程,点击屏幕中间的“Start To Leak”按钮,等待2-3分钟,待触发RssThresholdKiller查杀。
147
1488. 应用被查杀后,重新打开应用,会触发查杀事件上报,系统会回调应用的onReceive函数,可以在Log窗口看到对系统事件数据的处理日志。
149
150   应用查杀事件采样栈示例:
151
152   ```text
153   HiAppEvent eventInfo.domain=OS
154   HiAppEvent eventInfo.name=APP_KILLED
155   HiAppEvent eventInfo.params.time=1717597063727
156   HiAppEvent eventInfo.params.reason="RssThresholdKiller"
157   HiAppEvent eventInfo.params.foreground=true
158   ```
159