1# Calling APIs 2 3 4## Sample Code<a name="section5490173592518"></a> 5 6The sample code explicitly calls the related APIs of the memory debugging module to check the memory. 7 8``` 9#include <pthread.h> 10#include <stdlib.h> 11#include <stdio.h> 12#include <debug.h> // Header file that includes the declaration of the memory debugging APIs 13 14#define MALLOC_LEAK_SIZE 0x300 15 16void func(void) { 17 char *ptr = malloc(MALLOC_LEAK_SIZE); 18 memset(ptr, '3', MALLOC_LEAK_SIZE); 19} 20 21int main() 22{ 23 mem_check_init(NULL); // Output the memory debugging information through the serial port. This function must be called before the user program requests the heap memory for the first time (generally called at the entry of the main function). Otherwise, the debugging information is inaccurate. 24 // mem_check_init("/storage/mem_debug.txt"); // Output the memory debugging information to the /storage/mem_debug.txt file. If the file fails to be created, output the information through the serial port. 25 char *ptr = malloc(MALLOC_LEAK_SIZE); 26 memset(ptr, '1', MALLOC_LEAK_SIZE); 27 28 watch_mem(); // Obtain the thread-level memory statistics in the current code. 29 func(); 30 check_heap_integrity(); // Check the integrity of the heap memory nodes. 31 check_leak(); // Check whether a heap memory leak occurs in the current code. (Generally, the check result is accurate before the application exits. If the check is performed after the calling of malloc and before the calling of free, the result is inaccurate.) 32 return 0; 33} 34``` 35 36## Compilation 37 38``` 39$ clang -o mem_check mem_check.c -funwind-tables -rdynamic -g -mfloat-abi=softfp -mcpu=cortex-a7 -mfpu=neon-vfpv4 -target arm-liteos --sysroot=/home/<user-name>/directory/out/hispark_taurus/ipcamera_hispark_taurus/sysroot $(clang -mfloat-abi=softfp -mcpu=cortex-a7 -mfpu=neon-vfpv4 -target arm-liteos -print-file-name=libunwind.a) 40``` 41 42> **NOTE** <br/> 43>- In this example, the compiler path is written into an environment variable in the **.bashrc** file. 44>- When compiling user programs and required libraries, add the option **-funwind-tables -rdynamic -g** for stack backtracking. 45>- The **-mfloat-abi=softfp**, **-mcpu=cortex-a7**, and **-mfpu=neon-vfpv4** options specify the floating-point calculation optimization, chip architecture, and FPU, which must be the same as the compilation options used by the libc library. Otherwise, the libc library file cannot be found during the link time. 46>- **-target arm-liteos** specifies the path of the library files related to the compiler. 47>- **--sysroot=/home/<user-name\>/directory/out/hispark\_taurus/ipcamera\_hispark\_taurus/sysroot** specifies the root directory of the compiler library files. In this example, the OpenHarmony project code is stored in **/home/<user-name\>/directory**. The **out/hispark\_taurus/ipcamera\_hispark\_taurus** directory indicates the product specified by the **hb set** command during compilation. In this example, **ipcamera\_hispark\_taurus** is the product specified. 48>- **$\(clang -mfloat-abi=softfp -mcpu=cortex-a7 -mfpu=neon-vfpv4 -target arm-liteos -print-file-name=libunwind.a\)** specifies the path of the unwind library. 49 50## Debugging Information 51 52``` 53OHOS # ./mem_check 54OHOS # 55==PID:4== Heap memory statistics(bytes): // Heap memory statistics 56 [Check point]: // Call stack of the check point 57 #00: <main+0x38>[0x86c] -> mem_check 58 #01: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 59 60 [TID: 18, Used: 0x320] // The heap memory is occupied by thread No. 18. The current process has only one thread. 61 62==PID:4== Total heap: 0x320 byte(s), Peak: 0x320 byte(s) 63 64Check heap integrity ok! // Heap memory integrity check 65 66==PID:4== Detected memory leak(s): // Memory leak information and call stack 67 [Check point]: 68 #00: <check_leak+0x1c4>[0x2da4c] -> /lib/libc.so 69 #01: <main+0x44>[0x878] -> mem_check 70 71 [TID:18 Leak:0x320 byte(s)] Allocated from: 72 #00: <main+0x1c>[0x850] -> mem_check 73 #01: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 74 75 [TID:18 Leak:0x320 byte(s)] Allocated from: 76 #00: <func+0x14>[0x810] -> mem_check 77 #01: <main+0x3c>[0x870] -> mem_check 78 #02: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 79 80==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 81 82==PID:4== Detected memory leak(s): 83 [Check point]: 84 #00: <check_leak+0x1c4>[0x2da4c] -> /lib/libc.so 85 #01: <exit+0x28>[0x111ec] -> /lib/libc.so 86 87 [TID:18 Leak:0x320 byte(s)] Allocated from: 88 #00: <main+0x1c>[0x850] -> mem_check 89 #01: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 90 91 [TID:18 Leak:0x320 byte(s)] Allocated from: 92 #00: <func+0x14>[0x810] -> mem_check 93 #01: <main+0x3c>[0x870] -> mem_check 94 #02: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 95 96==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 97 98Check heap integrity ok! 99``` 100 101## Call Stack Parsing 102 103The **parse\_mem\_info.sh** script in **kernel/liteos\_a/tools/scripts/parse\_memory/** can be used to parse the call stack. You can use the script to convert the debug information into specific source code line number. In the following command, **mem\_debug.txt** stores the memory debugging information, and **elf1** and **elf2** are the executable and linkable format \(ELF\) files to parse. 104 105``` 106$ ./parse_mem_info.sh mem_debug.txt elf1 elf2 elf3 ... 107``` 108 109Example: 110 111``` 112$ ./parse_mem_info.sh mem_debug.txt mem_check 113Compiler is [gcc/llvm]: llvm 114Now using addr2line ... 115 116==PID:4== Heap memory statistics(bytes): 117 [Check point]: 118 #00: <main+0x38>[0x86c] at /usr1/xxx/TEST_ELF/mem_check.c:22 119 #01: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 120 121 [TID: 18, Used: 0x320] 122 123==PID:4== Total heap: 0x320 byte(s), Peak: 0x320 byte(s) 124 125Check heap integrity ok! 126 127==PID:4== Detected memory leak(s): 128 [Check point]: 129 #00: <check_leak+0x1c4>[0x2da4c] -> /lib/libc.so 130 #01: <main+0x44>[0x878] at /usr1/xxx/TEST_ELF/mem_check.c:28 131 132 [TID:18 Leak:0x320 byte(s)] Allocated from: 133 #00: <main+0x1c>[0x850] at /usr1/xxx/TEST_ELF/mem_check.c:17 134 #01: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 135 136 [TID:18 Leak:0x320 byte(s)] Allocated from: 137 #00: <func+0x14>[0x810] at /usr1/xxx/TEST_ELF/mem_check.c:9 138 #01: <main+0x3c>[0x870] at /usr1/xxx/TEST_ELF/mem_check.c:24 139 #02: <(null)+0x24baf9dc>[0x219dc] -> /lib/libc.so 140 141==PID:4== SUMMARY: 0x640 byte(s) leaked in 2 allocation(s). 142``` 143 144