/valgrind
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
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill valgrind --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/valgrind
Context preview
The summary Claude sees to decide when to auto-load this skill.
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
SKILL.md
valgrind.SKILL.mdname: valgrind
description: 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.
Valgrind
Purpose
Guide agents through Valgrind tools: Memcheck for memory errors, Cachegrind for cache simulation, Callgrind for call graphs, and Massif for heap profiling.
Triggers
- "My program has a memory leak / use-after-free"
- "I can't use ASan — can I use Valgrind instead?"
- "How do I profile cache behaviour without perf?"
- "How do I visualize call graphs with Callgrind?"
- "How do I profile heap allocation patterns?"
- "Valgrind reports errors in third-party code I can't fix"
Workflow
1. Memcheck — memory error detection
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) |
2. Understanding Memcheck output
==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)
- **Invalid read/write**: out-of-bounds access; check array bounds
- **Use of uninitialised value**: read before write; use `--track-origins=yes`
- **Invalid free / double free**: mismatched malloc/free; check ownership
- **Definitely lost**: reachable via no pointers; clear leak
- **Indirectly lost**: lost through a chain; usually means one root leak
- **Possibly lost**: might be pointing into the middle of a block; often FP with custom allocators
3. Leak kinds
| 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.
4. Suppressions
# 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 ./prog5. Cachegrind — cache simulation
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 rate
- `D1mr` / `DLmr`: L1/LL data read miss rate
- `D1mw` / `DLmw`: L1/LL data write miss rate
6. Callgrind — call graph profiling
valgrind --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.
7. Massif — heap profiling
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.
8. Performance considerations
Valgrind Memcheck runs ~10-50x slower than native. Mitigations:
- Use a shorter representative workload
- Use `--error-exitcode=1` to fail fast in CI
- Use ASan (`-fsanitize=address`) for faster memory checking during development
- Reserve Valgrind for cases where ASan can't be used (old toolchains, production-like environments)
For a comparison of Valgrind vs ASan, see [references/valgrind-vs-asan.md](references/valgrind-vs-asan.md).
Related skills
- Use `skills/runtimes/sanitizers` for faster ASan/UBSan alternatives
- Use `skills/profilers/linux-perf` for CPU-level profiling (faster than Cachegrind)
- Use `skills/profilers/flamegraphs` to visualise Callgrind output
Read more
name: valgrind description: 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.
Valgrind
Purpose
Guide agents through Valgrind tools: Memcheck for memory errors, Cachegrind for cache simulation, Callgrind for call graphs, and Massif for heap profiling.
Triggers
- "My program has a memory leak / use-after-free"
- "I can't use ASan — can I use Valgrind instead?"
- "How do I profile cache behaviour without perf?"
- "How do I visualize call graphs with Callgrind?"
- "How do I profile heap allocation patterns?"
- "Valgrind reports errors in third-party code I can't fix"
Workflow
1. Memcheck — memory error detection
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) |
2. Understanding Memcheck output
==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)
- **Invalid read/write**: out-of-bounds access; check array bounds
- **Use of uninitialised value**: read before write; use `--track-origins=yes`
- **Invalid free / double free**: mismatched malloc/free; check ownership
- **Definitely lost**: reachable via no pointers; clear leak
- **Indirectly lost**: lost through a chain; usually means one root leak
- **Possibly lost**: might be pointing into the middle of a block; often FP with custom allocators
3. Leak kinds
| 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.
4. Suppressions
# 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 ./prog5. Cachegrind — cache simulation
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 rate
- `D1mr` / `DLmr`: L1/LL data read miss rate
- `D1mw` / `DLmw`: L1/LL data write miss rate
6. Callgrind — call graph profiling
valgrind --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.
7. Massif — heap profiling
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.
8. Performance considerations
Valgrind Memcheck runs ~10-50x slower than native. Mitigations:
- Use a shorter representative workload
- Use `--error-exitcode=1` to fail fast in CI
- Use ASan (`-fsanitize=address`) for faster memory checking during development
- Reserve Valgrind for cases where ASan can't be used (old toolchains, production-like environments)
For a comparison of Valgrind vs ASan, see [references/valgrind-vs-asan.md](references/valgrind-vs-asan.md).
Related skills
- Use `skills/runtimes/sanitizers` for faster ASan/UBSan alternatives
- Use `skills/profilers/linux-perf` for CPU-level profiling (faster than Cachegrind)
- Use `skills/profilers/flamegraphs` to visualise Callgrind output
A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.
Repo: mohitmishra786/low-level-dev-skills
Other skills on low-level-dev-skills.
- /custom-allocators
Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator,
Open skill - /numa-programming
NUMA programming skill for multi-socket memory locality. Use when detecting NUMA topology, binding processes with numactl, using libnuma API, building NUMA-aware data structures, or measuring remote access penalties. Activates on queries about numactl, libnuma, NUMA topology,
Open skill - /af-xdp
AF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
Open skill - /dpdk
DPDK skill for userspace packet I/O. Use when initializing EAL, configuring PMD drivers, using mbuf pools and rte_ring, setting up huge pages, RSS, or testpmd validation. Activates on queries about DPDK, EAL, rte_eth_rx_burst, hugepages, PMD, or testpmd.
Open skill - /io-uring
io_uring skill for Linux async I/O. Use when building high-performance servers with liburing, multi-shot operations, provided buffers, fixed files, zero-copy send, or tokio-uring. Activates on queries about io_uring, SQE/CQE, liburing, IORING_OP_PROVIDE_BUFFERS, or io_uring vs
Open skill - /adc-dac-baremetal
Bare-metal ADC and DAC skill. Use when configuring analog sampling, DMA-driven ADC, calibration, or DAC output on MCUs. Activates on queries about ADC bare-metal, sampling time, DMA ADC, or DAC channel setup.
Open skill

