1# Using the CLI 2 3In addition to calling APIs to check the memory used by user-mode processes, you can run CLI commands to collect memory statistics, check for memory leaks, and check memory integrity. 4 5``` 6--mwatch: initializes memory debugging, registers signals, and outputs memory debugging information through the serial port. 7--mrecord <f_path>: initializes memory debugging, registers signals, and saves the memory debugging information to the f_path file. If the f_path file fails to be created, output the memory debugging information through the serial port. 8``` 9 10If the process to debug does not exit, you can use the signal mechanism to obtain the corresponding information: 11 12``` 13kill -35 <pid> # Check the thread-level heap memory usage. 14kill -36 <pid> # Check for heap memory leaks. 15kill -37 <pid> # Check whether the head node of the heap memory is complete. 16``` 17 18## Sample Code<a name="section13793104782316"></a> 19 20The sample code constructs a memory problem and uses the command line to perform memory debugging. 21 22``` 23#include <pthread.h> 24#include <stdlib.h> 25#include <stdio.h> 26 27#define MALLOC_LEAK_SIZE 0x300 28 29void func(void) { 30 char *ptr = malloc(MALLOC_LEAK_SIZE); 31 memset(ptr, '3', MALLOC_LEAK_SIZE); 32} 33 34int main() 35{ 36 char *ptr = malloc(MALLOC_LEAK_SIZE); 37 memset(ptr, '1', MALLOC_LEAK_SIZE); 38 func(); 39 while (1); 40} 41``` 42 43## Compilation 44 45For details, see [Calling APIs](kernel-small-debug-user-guide-use-api.md#compilation). 46 47## Running the mwatch Command 48 49``` 50OHOS # ./mem_check --mwatch // Run the task command to obtain the mem_check process PID, which is 4. 51OHOS # 52OHOS # kill -35 4 // Check heap memory statistics. 53OHOS # 54==PID:4== Heap memory statistics(bytes): 55 [Check point]: 56 #00: <arm_signal_process+0x5c>[0x58dfc] -> /lib/libc.so 57 58 [TID: 18, Used: 0x640] 59 60==PID:4== Total heap: 0x640 byte(s), Peak: 0x640 byte(s) 61 62OHOS # kill -36 4 // Check for heap memory leaks. 63OHOS # 64==PID:4== Detected memory leak(s): 65 [Check point]: 66 #00: <check_leak+0x1c4>[0x2da4c] -> /lib/libc.so 67 #01: <arm_signal_process+0x5c>[0x58dfc] -> /lib/libc.so 68 69 [TID:18 Leak:0x320 byte(s)] Allocated from: 70 #00: <main+0x14>[0x724] -> mem_check 71 #01: <(null)+0x2555a9dc>[0x219dc] -> /lib/libc.so 72 73 [TID:18 Leak:0x320 byte(s)] Allocated from: 74 #00: <func+0x14>[0x6ec] -> mem_check 75 #01: <main+0x30>[0x740] -> mem_check 76 #02: <(null)+0x2555a9dc>[0x219dc] -> /lib/libc.so 77 78==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 79 80OHOS # kill -37 4 // Check the integrity of the head node of the heap memory. 81OHOS # 82Check heap integrity ok! 83``` 84 85## Call Stack Parsing<a name="section1880675510221"></a> 86 87Save the debugging information to the **test.txt** file and use the script to parse the information to obtain the number of line where the memory leak occurs. 88 89``` 90$ ./parse_mem_info.sh test.txt mem_check 91Compiler is [gcc/llvm]: llvm 92Now using addr2line ... 93 94==PID:4== Detected memory leak(s): 95 [Check point]: 96 #00: <check_leak+0x1c4>[0x2da4c] -> /lib/libc.so 97 #01: <arm_signal_process+0x5c>[0x58dfc] -> /lib/libc.so 98 99 [TID:18 Leak:0x320 byte(s)] Allocated from: 100 #00: <main+0x14>[0x724] at /usr1/xxx/TEST_ELF/mem_check.c:14 101 #01: <(null)+0x2555a9dc>[0x219dc] -> /lib/libc.so 102 103 [TID:18 Leak:0x320 byte(s)] Allocated from: 104 #00: <func+0x14>[0x6ec] at /usr1/xxx/TEST_ELF/mem_check.c:8 105 #01: <main+0x30>[0x740] at /usr1/xxx/TEST_ELF/mem_check.c:19 106 #02: <(null)+0x2555a9dc>[0x219dc] -> /lib/libc.so 107 108==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 109``` 110 111## Running the mrecord Command 112 1131. Run the user program and specify the path of the file that stores the memory debugging information. 114 115 ``` 116 OHOS # ./mem_check --mrecord /storage/check.txt 117 ``` 118 1192. Run the **kill -35 <_pid_\>** command to collect statistics on the memory information. The information is exported to a file. Run the **cat** command to view the information. 120 121 ``` 122 OHOS # kill -35 4 123 OHOS # Memory statistics information saved in /storage/pid(4)_check.txt 124 125 OHOS # cat /storage/pid(4)_check.txt 126 127 ==PID:4== Heap memory statistics(bytes): 128 [Check point]: 129 #00: <arm_signal_process+0x5c>[0x5973c] -> /lib/libc.so 130 131 [TID: 18, Used: 0x640] 132 133 ==PID:4== Total heap: 0x640 byte(s), Peak: 0x640 byte(s) 134 ``` 135 1363. Run the **kill -36 <_pid_\>** command to check memory integrity. The information is exported to a file. Run the **cat** command to view the information. 137 138 ``` 139 OHOS # kill -36 4 140 OHOS # Leak check information saved in /storage/pid(4)_check.txt 141 142 OHOS # cat /storage/pid(4)_check.txt 143 144 ==PID:4== Heap memory statistics(bytes): 145 [Check point]: 146 #00: <arm_signal_process+0x5c>[0x5973c] -> /lib/libc.so 147 148 [TID: 18, Used: 0x640] 149 150 ==PID:4== Total heap: 0x640 byte(s), Peak: 0x640 byte(s) 151 152 ==PID:4== Detected memory leak(s): 153 [Check point]: 154 #00: <check_leak+0x1c4>[0x2e38c] -> /lib/libc.so 155 #01: <arm_signal_process+0x5c>[0x5973c] -> /lib/libc.so 156 157 [TID:18 Leak:0x320 byte(s)] Allocated from: 158 #00: <main+0x14>[0x724] -> mem_check 159 #01: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 160 161 [TID:18 Leak:0x320 byte(s)] Allocated from: 162 #00: <func+0x14>[0x6ec] -> mem_check 163 #01: <main+0x30>[0x740] -> mem_check 164 #02: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 165 166 ==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 167 ``` 168 1694. Run the **kill -9 <_pid_\>** command to kill the current process. After the process exits, a memory integrity check is performed by default. The check result is output to a file. You can run the **cat** command to view it. 170 171 ``` 172 OHOS # kill -9 4 173 OHOS # Leak check information saved in /storage/pid(4)_check.txt 174 175 Check heap integrity ok! 176 177 OHOS # cat /storage/pid(4)_check.txt 178 OHOS # 179 ==PID:4== Heap memory statistics(bytes): 180 [Check point]: 181 #00: <arm_signal_process+0x5c>[0x5973c] -> /lib/libc.so 182 183 [TID: 18, Used: 0x640] 184 185 ==PID:4== Total heap: 0x640 byte(s), Peak: 0x640 byte(s) 186 187 ==PID:4== Detected memory leak(s): 188 [Check point]: 189 #00: <check_leak+0x1c4>[0x2e38c] -> /lib/libc.so 190 #01: <arm_signal_process+0x5c>[0x5973c] -> /lib/libc.so 191 192 [TID:18 Leak:0x320 byte(s)] Allocated from: 193 #00: <main+0x14>[0x724] -> mem_check 194 #01: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 195 196 [TID:18 Leak:0x320 byte(s)] Allocated from: 197 #00: <func+0x14>[0x6ec] -> mem_check 198 #01: <main+0x30>[0x740] -> mem_check 199 #02: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 200 201 ==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 202 203 ==PID:4== Detected memory leak(s): 204 [Check point]: 205 #00: <check_leak+0x1c4>[0x2e38c] -> /lib/libc.so 206 #01: <exit+0x28>[0x11b2c] -> /lib/libc.so 207 208 [TID:18 Leak:0x320 byte(s)] Allocated from: 209 #00: <main+0x14>[0x724] -> mem_check 210 #01: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 211 212 [TID:18 Leak:0x320 byte(s)] Allocated from: 213 #00: <func+0x14>[0x6ec] -> mem_check 214 #01: <main+0x30>[0x740] -> mem_check 215 #02: <(null)+0x1fdd231c>[0x2231c] -> /lib/libc.so 216 217 ==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 218 ``` 219 220 221> **NOTE**<br/> 222>The preceding information recorded gradually is added to the file specified during initialization. Therefore, running the **cat** command can also display the historical information in the file. 223 224