skills/profilers/valgrind/SKILL.md
Valgrind profiler skill for memory error detection and cache profiling. Use when running Memcheck to find heap corruption, use-after-free, memory leaks, or uninitialised reads; or Cachegrind/Callgrind for cache simulation and function-level profiling. Activates on queries about valgrind, memcheck, heap leaks, use-after-free without sanitizers, cachegrind, callgrind, KCachegrind, or massif memory profiling.
npx skillsauth add mohitmishra786/low-level-dev-skills valgrindInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Guide agents through Valgrind tools: Memcheck for memory errors, Cachegrind for cache simulation, Callgrind for call graphs, and Massif for heap profiling.
Compile with -g -O1 for best results. -O0 is also fine; avoid -O2+ which can produce false positives.
valgrind --tool=memcheck \
--leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--error-exitcode=1 \
./prog [args]
Key flags:
| Flag | Default | Effect |
|------|---------|--------|
| --leak-check=full | summary | Full leak details |
| --show-leak-kinds=all | definite | Show all leak kinds |
| --track-origins=yes | no | Show where uninit values came from (slow) |
| --error-exitcode=N | 0 | Exit N if errors found (CI integration) |
| --log-file=file | stderr | Save report to file |
| --suppressions=file | none | Suppress known FPs |
| --gen-suppressions=yes | no | Print suppression directives for errors |
| --max-stackframe=N | 2000000 | Increase for deep stacks |
| --malloc-fill=0xAB | off | Fill allocated memory (detect uninit use) |
| --free-fill=0xCD | off | Fill freed memory (detect use-after-free) |
==12345== Invalid read of size 4
==12345== at 0x4007A2: foo (main.c:15)
==12345== by 0x400846: main (main.c:30)
==12345== Address 0x5204040 is 0 bytes after a block of size 40 alloc'd
==12345== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck.so)
==12345== by 0x40074B: main (main.c:25)
--track-origins=yes| Kind | Meaning | |------|---------| | Definitely lost | No pointer to block | | Indirectly lost | Lost via another lost block | | Possibly lost | Pointer into middle of block | | Still reachable | Pointer exists at exit; not a leak but never freed |
For library code: --show-leak-kinds=definite,indirect reduces noise from still-reachable.
# Generate suppression for current error
valgrind --gen-suppressions=yes ./prog 2>&1 | grep -A20 '{'
# Example suppression file (valgrind.supp)
{
openssl_uninit
Memcheck:Cond
fun:SHA256_Init
...
}
# Use suppression file
valgrind --suppressions=valgrind.supp ./prog
valgrind --tool=cachegrind ./prog
# Output: cachegrind.out.PID
# Annotate source
cg_annotate cachegrind.out.12345 --auto=yes
# Diff two runs
cg_diff cachegrind.out.before cachegrind.out.after
Key metrics:
I1mr / ILmr: L1/LL instruction cache miss rateD1mr / DLmr: L1/LL data read miss rateD1mw / DLmw: L1/LL data write miss ratevalgrind --tool=callgrind --callgrind-out-file=callgrind.out ./prog
# Analyse
callgrind_annotate callgrind.out
# Visualise in KCachegrind (GUI)
kcachegrind callgrind.out
Callgrind is slower than perf but works without root and provides exact call counts.
valgrind --tool=massif ./prog
# Visualise
ms_print massif.out.PID | less
# GUI
massif-visualizer massif.out.PID
Massif shows heap usage over time; useful for finding peak allocation sites and tracking gradual leaks.
Valgrind Memcheck runs ~10-50x slower than native. Mitigations:
--error-exitcode=1 to fail fast in CI-fsanitize=address) for faster memory checking during developmentFor a comparison of Valgrind vs ASan, see references/valgrind-vs-asan.md.
skills/runtimes/sanitizers for faster ASan/UBSan alternativesskills/profilers/linux-perf for CPU-level profiling (faster than Cachegrind)skills/profilers/flamegraphs to visualise Callgrind outputdevelopment
Zig testing skill for writing and running tests. Use when using zig build test, writing comptime tests, using test filters, working with test allocators to detect leaks, or using Zig's built-in fuzz testing (0.14+). Activates on queries about Zig tests, zig test, zig build test, comptime testing, test allocators, Zig fuzz testing, or detecting memory leaks in Zig tests.
development
Zig debugging skill. Use when debugging Zig programs with GDB or LLDB, interpreting Zig runtime panics, using std.debug.print for tracing, configuring debug builds, or debugging Zig programs in VS Code. Activates on queries about debugging Zig, Zig panics, zig gdb, zig lldb, std.debug.print, Zig stack traces, or Zig error return traces.
tools
Zig cross-compilation skill. Use when cross-compiling Zig programs to different targets, using Zig's built-in cross-compilation for embedded, WASM, Windows, ARM, or using zig cc to cross-compile C code without a system cross-toolchain. Activates on queries about Zig cross-compilation, zig target triples, zig cc cross-compile, Zig embedded targets, or Zig WASM.
development
Zig comptime skill for compile-time evaluation and metaprogramming. Use when using comptime parameters, comptime types, generics via anytype, comptime reflection with @typeInfo, or metaprogramming patterns that replace C++ templates. Activates on queries about Zig comptime, compile-time evaluation, Zig generics, anytype, @typeInfo, comptime types, or Zig metaprogramming.