Using Valgrind for dynamic code analysis
Overview
Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools. [Valgrind Home ]
Please also refer to the manual: Valgrind.
How to use
Parameters are following
valgrind --leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--verbose \
--log-file=valgrind-out.txt \
./executable exampleParam1
Install Valgrind
tbi@tbi-lintp:~/ sudo apt install valgrind
Build your application as you would normally do.
tbi@tbi-lintp:~/goal/projects/goal_pnio/01_simple_io/gcc$ make
Start Valgrind (optionally with root-rights) with the application and his parameters.
tbi@tbi-lintp:~/goal/projects/goal_pnio/01_simple_io/gcc$ sudo valgrind --tool=helgrind ./build/linux_irj45_clang/goal_linux_irj45_clang.bin -i enx0050b6e76fe7
You can now check the functionality of the application as you would normally do. After your tests, you can simply kill the application using STRG+C
and take a look at the found issue.
Analysis of the Log
The log is printed directly onto the command line, along the GOAL logging and is mostly self-explained.
Troubleshooting
Q: Valgrind reports an error at start with the Binary
A: If valgrind reports an error (something like Valgrind: debuginfo reader: ensure_valid failed:
) you might have to modify the compiler flags at tools/make/gcc/PLATFORM/config_goal, like shown below:
diff --git a/tools/make/gcc/linux_irj45_x86/config_goal b/tools/make/gcc/linux_irj45_x86/config_goal
index 424ba322e..712dfc4e7 100644
--- a/tools/make/gcc/linux_irj45_x86/config_goal
+++ b/tools/make/gcc/linux_irj45_x86/config_goal
@@ -20,7 +20,7 @@ else
CFLAGS_COMMON += -fPIC
endif
-CFLAGS_COMMON += -Dgcc -ggdb -g3 -O0 -m32 -pthread -D_GNU_SOURCE -Wall -Wextra -Werror
+CFLAGS_COMMON += -Dgcc -gdwarf-4 -O0 -m32 -pthread -D_GNU_SOURCE -Wall -Wextra