1# 使用HiDebug获取调试信息(C/C++)
2
3HiDebug模块对外提供应用调试功能。
4
5## 接口说明
6| 接口名                          | 描述                              |
7| ------------------------------- | --------------------------------- |
8| OH_HiDebug_GetSystemCpuUsage    | 获取系统的CPU资源占用情况百分比。 |
9| OH_HiDebug_GetAppCpuUsage       | 获取进程的CPU使用率百分比。       |
10| OH_HiDebug_GetAppThreadCpuUsage | 获取应用所有线程CPU使用情况。     |
11| OH_HiDebug_FreeThreadCpuUsage   | 释放线程数据结构。                |
12| OH_HiDebug_GetSystemMemInfo     | 获取系统内存信息。                |
13| OH_HiDebug_GetAppNativeMemInfo  | 获取应用程序进程的内存信息。      |
14| OH_HiDebug_GetAppMemoryLimit    | 获取应用程序进程的内存限制。      |
15| OH_HiDebug_StartAppTraceCapture | 启动应用trace采集。               |
16| OH_HiDebug_StopAppTraceCapture  | 停止应用trace采集。               |
17| OH_HiDebug_GetGraphicsMemory    | 获取应用显存大小。          |
18
19API接口的具体使用说明(参数使用限制、具体取值范围等)请参考[HiDebug](../reference/apis-performance-analysis-kit/_hi_debug.md)。
20
21## 开发步骤
22下文将展示如何在应用内增加一个按钮,并单击该按钮以调用Hidebug Ndk接口。
23
241. 新建Native C++工程,目录结构如下:
25
26   ```yml
27   entry:
28     src:
29       main:
30         cpp:
31           - types:
32               libentry:
33                 - index.d.ts
34           - CMakeLists.txt
35           - napi_init.cpp
36         ets:
37           - entryability:
38               - EntryAbility.ts
39           - pages:
40               - Index.ets
41   ```
42
432. 编辑"CMakeLists.txt"文件,添加库依赖:
44
45   ```cmake
46   # 新增动态库依赖libhiappevent_ndk.z.solibhilog_ndk.z.so(日志输出)
47   target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhidebug.so)
48   ```
49
503. 编辑"napi_init.cpp"文件,导入依赖的文件,并定义LOG_TAG及测试方法:
51
52   本示例中以OH_HiDebug_GetSystemCpuUsage接口为例,调用该接口并输出返回值,其他接口请参考[HiDebug](../reference/apis-performance-analysis-kit/_hi_debug.md)。
53
54   ```c++
55   #include "napi/native_api.h"
56   #include "hilog/log.h"
57   #include "hidebug/hidebug.h"
58
59   #undef LOG_TAG
60   #define LOG_TAG "testTag"
61
62   static napi_value TestHidebugNdk(napi_env env, napi_callback_info info)
63   {
64       double cpuUsage = OH_HiDebug_GetSystemCpuUsage();
65       OH_LOG_INFO(LogType::LOG_APP, "GetSystemCpuUsage: %{public}f", cpuUsage);
66       return 0;
67   }
68   ```
69
704. 将TestHidebugNdk注册为ArkTS接口:
71
72   编辑"napi_init.cpp"文件,将TestHidebugNdk注册为ArkTS接口:
73
74   ```c++
75   static napi_value Init(napi_env env, napi_value exports)
76   {
77       napi_property_descriptor desc[] = {
78           { "testHidebugNdk", nullptr, TestHidebugNdk, nullptr, nullptr, nullptr, napi_default, nullptr }
79       };
80       napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
81       return exports;
82   }
83   ```
84
85   编辑"index.d.ts"文件,定义ArkTS接口:
86
87   ```typescript
88   export const testHidebugNdk: () => void;
89   ```
90
915. 编辑"Index.ets"文件,给文本Text组件添加一个点击事件,示例代码如下:
92
93   ```ts
94   import testNapi from 'libentry.so'
95
96   @Entry
97   @Component
98   struct Index {
99     @State message: string = 'Hello World'
100
101     build() {
102       Row() {
103         Column() {
104           Text(this.message)
105             .fontSize(50)
106             .fontWeight(FontWeight.Bold)
107             .onClick(testNapi.testHidebugNdk);//添加点击事件,触发testHidebugNdk方法。
108         }
109         .width('100%')
110       }
111       .height('100%')
112     }
113   }
114   ```
115
1166. 点击DevEco Studio界面中的运行按钮,运行应用工程,点击"Hello world"文本。
117
1187. 在DevEco Studio的底部,切换到“Log”窗口,设置日志的过滤条件为“testTag”。
119   此时窗口将显示通过OH_HiDebug_GetSystemCpuUsage()接口获取的CPU使用率的相关日志。
120
121   ```Text
122   09-10 09:40:26.755 17221-17221/com.example.myapplication I A00000/testTag: GetSystemCpuUsage: 0.083904
123   ```
124
125