Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDB Plugin Upgrade part2: memory leak detection and other memory tools #14851

Merged
merged 22 commits into from
Nov 21, 2024

Conversation

XuNeo
Copy link
Contributor

@XuNeo XuNeo commented Nov 19, 2024

Note: Please adhere to Contributing Guidelines.

Summary

This PR is based on #14843 to minimize conflicts. I will rebase once that one is merged. The changes are from commit 679e38d.

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.

+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
  1. Connect GDB and do analysis
    gdb-multiarch build/nuttx -ex "tar rem:1127" -ex "source nuttx/tools/gdb/__init__.py"
  2. execute memleak command
(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:

--- 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;
 }

Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@github-actions github-actions bot added the Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. label Nov 19, 2024
Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link
Contributor

@hartmannathan hartmannathan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very cool! I can't wait to try it! Thanks

tools/gdb/macros.py Outdated Show resolved Hide resolved
tools/gdb/macros.py Outdated Show resolved Hide resolved
tools/gdb/macros.py Outdated Show resolved Hide resolved
tools/gdb/macros.py Outdated Show resolved Hide resolved
tools/gdb/macros.py Outdated Show resolved Hide resolved
tools/gdb/memdump.py Outdated Show resolved Hide resolved
tools/gdb/stack.py Outdated Show resolved Hide resolved
tools/gdb/utils.py Outdated Show resolved Hide resolved
tools/gdb/utils.py Outdated Show resolved Hide resolved
tools/gdb/utils.py Outdated Show resolved Hide resolved
anjiahao1 and others added 21 commits November 20, 2024 14:43
For coredump, gdb-stub, the thread command is natively supported.

Signed-off-by: xuxingliang <[email protected]>
Display all memory nodes in a picture, which can be used to help analyze memory fragmentation

Signed-off-by: yinshengkai <[email protected]>
1. Avoid to_bytes by using memoryview directly.
2. No need to call gdb to cast to char *.
3. Cache the memory data without invoke gdb in every iteration.
4. Do code cleanup.

memleak speed improved from 261.93 seconds to 29.9 seconds for x4b
usecase.

Signed-off-by: xuxingliang <[email protected]>
Read the whole memory costs additional time. When the number memory nodes is
small, test on qemu shows that read memory directly is faster.

Signed-off-by: xuxingliang <[email protected]>
"on" string is always in the word 'notification'. Should use "is on" instead.

Signed-off-by: xuxingliang <[email protected]>
The macro could be eliminated by optimization.

Signed-off-by: xuxingliang <[email protected]>
1. Remove Nx prefix for nuttx unique commands.
2. Add docstring for most of the commands in order to show help message.
3. Add 'init_once' method for Memmap command. The prerequisite is checked the moment it's used.

Signed-off-by: xuxingliang <[email protected]>
For the prebuilt arm-none-eabi-gdb, there's no python builtin module
available.

Signed-off-by: xuxingliang <[email protected]>
information prefer from gdb to python class

Signed-off-by: buxiasen <[email protected]>
Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@XuNeo XuNeo marked this pull request as ready for review November 20, 2024 07:18
@xiaoxiang781216 xiaoxiang781216 merged commit 1518f8b into apache:master Nov 21, 2024
27 checks passed
@XuNeo XuNeo deleted the gdb-plugin-memleak branch November 22, 2024 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Tooling Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants