• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18For 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).
19
20## How to Develop
21The following describes how to add a button in the application and click the button to call the HiDebug APIs.
22
231. Create a native C++ project. The directory structure is as follows:
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. In the **CMakeLists.txt** file, add the dependencies.
43
44   ```cmake
45   # Add libhiappevent_ndk.z.so and libhilog_ndk.z.so (log output).
46   target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhidebug.so)
47   ```
48
493. Import the dependencies to the **napi_init.cpp** file, and define **LOG_TAG** and the test method.
50
51   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).
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. Register **TestHidebugNdk** as an ArkTS API.
70
71   In the **napi_init.cpp** file, register **TestHidebugNdk** as an ArkTS API.
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   In the **index.d.ts** file, define the ArkTS API.
85
86   ```typescript
87   export const testHidebugNdk: () => void;
88   ```
89
905. In the **Index.ets** file, add a click event to the **Text** component. The sample code is as follows:
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);// Add a click event to trigger testHidebugNdk().
107         }
108         .width('100%')
109       }
110       .height('100%')
111     }
112   }
113   ```
114
1156. Click the **Run** button in DevEco Studio to run the project, and click "Hello world".
116
1177. At the bottom of DevEco Studio, switch to the **Log** tab and set the filter criteria to **testTag**.
118   Then, the CPU usage logs obtained using **OH_HiDebug_GetSystemCpuUsage()** are displayed in the window.
119
120   ```Text
121   09-10 09:40:26.755 17221-17221/com.example.myapplication I A00000/testTag: GetSystemCpuUsage: 0.083904
122   ```
123