XuNeo opened a new pull request, #14851:
URL: https://github.com/apache/nuttx/pull/14851

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   This PR is based on https://github.com/apache/nuttx/pull/14843 to minimize 
conflicts. I will rebase once that one is merged. The changes are from commit 
679e38df843b869207cc6c6701851a1206a2312b.
   
   In this PR, @anjiahao1 introduces a powerful tool to automatically detect 
memory leak based on offline core dump or online gdb connection. There are 
follow up commits that optimizes memleak detection speed.
   
   The memfrag from @Gary-Hobson is used to calculate memory fragmentation 
rate, so we can have a relatively unified number to check the fragmentation 
rate.
   
   The memmap from @Gary-Hobson  can visually show the memory status.
   
   This PR also includes miscellaneous bug fixes for utils module such as 
compatibility for older version of GDB etc.
   
   
   ## Impact
   
   New feature and bug fixes.
   
   ## Testing
   
   
   I tested on qemu arm64 with below additional configuration. Note that `-g3` 
is needed in order to parse the macro values which are needed by the tool.
   
   ```patch
   +CONFIG_MM_BACKTRACE=16
   +CONFIG_MM_BACKTRACE_DEFAULT=y
   +CONFIG_MM_BACKTRACE_SKIP=1
   +CONFIG_MM_HEAP_MEMPOOL_BACKTRACE_SKIP=3
   +CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=256
   +CONFIG_DEBUG_SYMBOLS_LEVEL="-g3"
   +CONFIG_DEBUG_MM=y
   ```
   
   1. compile and launch
   
   ```
   cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -Bbuild -GNinja 
-DBOARD_CONFIG=boards/arm64/qemu/qemu-armv8a/configs/nsh_smp nuttx
   
    qemu-system-aarch64 -smp 4 -cpu cortex-a53 -semihosting -nographic -machine 
virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on 
-serial chardev:con -mon chardev=con,mode=readline -kernel build/nuttx -gdb 
tcp::1127
   ```
   2. Connect GDB and do analysis
   `gdb-multiarch build/nuttx -ex "tar rem:1127" -ex "source 
nuttx/tools/gdb/__init__.py"`
   3. execute `memleak` command
   ```bash
   (gdb) memleak
   Searching for leaked memory, please wait a moment
   Searching in global variables 0x40433000 ~ 0x40448000
   Searching in grey memory
   Search all memory use 0.03 seconds
   
   Leak catch!, use '*' mark pid is not exist:
      CNT   PID        Size    Sequence            Address Callstack
        2     0         160           0         0x40454b80
   *    1     4         256          34         0x40459290  
[0x000000000402c09d8] <mm_malloc+48>       
/home/neo/projects/nuttx/nuttx/mm/mm_heap/mm_malloc.c:186
                                                            
[0x000000000402c153c] <malloc+28>          
/home/neo/projects/nuttx/nuttx/mm/umm_heap/umm_malloc.c:66
                                                            
[0x000000000402f15bc] <hello_main+16>      
/home/neo/projects/nuttx/apps/examples/hello/hello_main.c:39
                                                            
[0x000000000402d3458] <nxtask_startup+48>  
/home/neo/projects/nuttx/nuttx/libs/libc/sched/task_startup.c:72
                                                            
[0x000000000402cdf90] <nxtask_start+208>   
/home/neo/projects/nuttx/nuttx/sched/task/task_start.c:75
   
   Alloc 3 count,have 2 some backtrace leak, total leak memory is 256 bytes
   Finished in 0.06 seconds
   (gdb)
   
   ```
   
   Note the leak is generated by example in hello_main.c:
   ```patch
   --- a/examples/hello/hello_main.c
   +++ b/examples/hello/hello_main.c
   @@ -35,6 +35,6 @@
    
    int main(int argc, FAR char *argv[])
    {
   -  printf("Hello, World!!\n");
   +  printf("Hello, World!!: %p\n", malloc(123));
      return 0;
    }
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to