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 18API接口的具体使用说明(参数使用限制、具体取值范围等)请参考[HiDebug](../reference/apis-performance-analysis-kit/_hi_debug.md)。 19 20## 开发步骤 21下文将展示如何在应用内增加一个按钮,并单击该按钮以调用Hidebug Ndk接口。 22 231. 新建Native C++工程,目录结构如下: 24 25 ```yml 26 entry: 27 src: 28 main: 29 cpp: 30 - types: 31 libentry: 32 - index.d.ts 33 - CMakeLists.txt 34 - napi_init.cpp 35 ets: 36 - entryability: 37 - EntryAbility.ts 38 - pages: 39 - Index.ets 40 ``` 41 422. 编辑"CMakeLists.txt"文件,添加库依赖: 43 44 ```cmake 45 # 新增动态库依赖libhiappevent_ndk.z.so和libhilog_ndk.z.so(日志输出) 46 target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhidebug.so) 47 ``` 48 493. 编辑"napi_init.cpp"文件,导入依赖的文件,并定义LOG_TAG及测试方法: 50 51 本示例中以OH_HiDebug_GetSystemCpuUsage接口为例,调用该接口并输出返回值,其他接口请参考[HiDebug](../reference/apis-performance-analysis-kit/_hi_debug.md)。 52 53 ```c++ 54 #include "napi/native_api.h" 55 #include "hilog/log.h" 56 #include "hidebug/hidebug.h" 57 58 #undef LOG_TAG 59 #define LOG_TAG "testTag" 60 61 static napi_value TestHidebugNdk(napi_env env, napi_callback_info info) 62 { 63 double cpuUsage = OH_HiDebug_GetSystemCpuUsage(); 64 OH_LOG_INFO(LogType::LOG_APP, "GetSystemCpuUsage: %{public}f", cpuUsage); 65 return 0; 66 } 67 ``` 68 694. 将TestHidebugNdk注册为ArkTS接口: 70 71 编辑"napi_init.cpp"文件,将TestHidebugNdk注册为ArkTS接口: 72 73 ```c++ 74 static napi_value Init(napi_env env, napi_value exports) 75 { 76 napi_property_descriptor desc[] = { 77 { "testHidebugNdk", nullptr, TestHidebugNdk, nullptr, nullptr, nullptr, napi_default, nullptr } 78 }; 79 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 80 return exports; 81 } 82 ``` 83 84 编辑"index.d.ts"文件,定义ArkTS接口: 85 86 ```typescript 87 export const testHidebugNdk: () => void; 88 ``` 89 905. 编辑"Index.ets"文件,给文本Text组件添加一个点击事件,示例代码如下: 91 92 ```ts 93 import testNapi from 'libentry.so' 94 95 @Entry 96 @Component 97 struct Index { 98 @State message: string = 'Hello World' 99 100 build() { 101 Row() { 102 Column() { 103 Text(this.message) 104 .fontSize(50) 105 .fontWeight(FontWeight.Bold) 106 .onClick(testNapi.testHidebugNdk);//添加点击事件,触发testHidebugNdk方法。 107 } 108 .width('100%') 109 } 110 .height('100%') 111 } 112 } 113 ``` 114 1156. 点击IDE界面中的运行按钮,运行应用工程,点击"Hello world"文本。 116 1177. 在DevEco Studio的底部,切换到“Log”窗口,设置日志的过滤条件为“testTag”。 118 此时窗口将显示通过OH_HiDebug_GetSystemCpuUsage()接口获取的CPU使用率的相关日志。 119 120 ```Text 121 09-10 09:40:26.755 17221-17221/com.example.myapplication I A00000/testTag: GetSystemCpuUsage: 0.083904 122 ``` 123 124