1# Using HiDebug (C/C++)
2
3HiDebug provides APIs for application debugging.
4
5## Available APIs
6| API                         | Description                             |
7| ------------------------------- | --------------------------------- |
8| OH_HiDebug_GetSystemCpuUsage    | Obtains the CPU usage of the system.|
9| OH_HiDebug_GetAppCpuUsage       | Obtains the CPU usage of an application.      |
10| OH_HiDebug_GetAppThreadCpuUsage | Obtains the CPU usage of all threads of an application.    |
11| OH_HiDebug_FreeThreadCpuUsage   | Releases the thread data structure.               |
12| OH_HiDebug_GetSystemMemInfo     | Obtains system memory information.               |
13| OH_HiDebug_GetAppNativeMemInfo  | Obtains the memory information of an application.     |
14| OH_HiDebug_GetAppMemoryLimit    | Obtains the memory limit of an application.     |
15| OH_HiDebug_StartAppTraceCapture | Starts application trace collection.              |
16| OH_HiDebug_StopAppTraceCapture  | Stops application trace collection.              |
17| OH_HiDebug_GetGraphicsMemory    | Obtains the size of the GPU memory.         |
18
19For details about how to use the APIs (such as parameter usage restrictions and value ranges), see [HiDebug](../reference/apis-performance-analysis-kit/_hi_debug.md).
20
21## How to Develop
22The following describes how to add a button in the application and click the button to call the HiDebug APIs.
23
241. Create a native C++ project. The directory structure is as follows:
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. In the **CMakeLists.txt** file, add the dependencies.
44
45   ```cmake
46   # Add libhiappevent_ndk.z.so and libhilog_ndk.z.so (log output).
47   target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhidebug.so)
48   ```
49
503. Import the dependencies to the **napi_init.cpp** file, and define **LOG_TAG** and the test method.
51
52   The following calls **OH_HiDebug_GetSystemCpuUsage()** and outputs the return value. For details about how to use other APIs, see [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. Register **TestHidebugNdk** as an ArkTS API.
71
72   In the **napi_init.cpp** file, register **TestHidebugNdk** as an ArkTS API.
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   In the **index.d.ts** file, define the ArkTS API.
86
87   ```typescript
88   export const testHidebugNdk: () => void;
89   ```
90
915. In the **Index.ets** file, add a click event to the **Text** component. The sample code is as follows:
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);// Add a click event to trigger testHidebugNdk().
108         }
109         .width('100%')
110       }
111       .height('100%')
112     }
113   }
114   ```
115
1166. Click the **Run** button in DevEco Studio to run the project, and click "Hello world".
117
1187. At the bottom of DevEco Studio, switch to the **Log** tab and set the filter criteria to **testTag**.
119   Then, the CPU usage logs obtained using **OH_HiDebug_GetSystemCpuUsage()** are displayed in the window.
120
121   ```Text
122   09-10 09:40:26.755 17221-17221/com.example.myapplication I A00000/testTag: GetSystemCpuUsage: 0.083904
123   ```
124