Skip to content

Instantly share code, notes, and snippets.

@jeffreylovitz
Created November 25, 2019 11:35
Show Gist options
  • Save jeffreylovitz/00f2303691afc1ac9bcdc5a4d2828720 to your computer and use it in GitHub Desktop.
Save jeffreylovitz/00f2303691afc1ac9bcdc5a4d2828720 to your computer and use it in GitHub Desktop.

Valgrind + GDB control

When a memory error is encountered, execution will pause and live debugging can be performed using GDB.

valgrind --vgdb-error=0 redis-server /path/to/redis.conf

Valgrind + function call + runtime profiler (Callgrind)

Generate call graphs that can be visualized to detect program bottlenecks. (I use KCacheGrind for visualization.)

valgrind --tool=callgrind redis-server /path/to/redis.conf

Valgrind + Heap profiler (Massif)

Generate heap profiles that can be visualized to track heap memory consumption.

valgrind --tool=massif redis-server /path/to/redis.conf

Valgrind + race condition detector (Helgrind)

Attempt to track log race conditions encountered during execution. (Note - generates a lot of false positives) The sample here is a script like server_tee.sh that logs output and prints to stdout.

valgrind --tool=helgrind --free-is-write=yes --ignore-thread-creation=yes --check-stack-refs=no redis-server /path/to/redis.conf 2> >(tee $1)
# Run as ./server_tee.sh leakfile.log
# Will run Redis server under Valgrind, logging output to stdout and the provided file name.
valgrind --leak-check=full --show-leak-kinds=all --show-possibly-lost=yes redis-server /path/to/redis.conf 2> >(tee $1)
# Sample Valgrind suppression file
{
<server suppression: lzf_unitialized_hash_table>
Memcheck:Cond
fun:lzf_compress
}
{
<server suppression: lzf_unitialized_hash_table>
Memcheck:Value4
fun:lzf_compress
}
{
<server suppression: lzf_unitialized_hash_table>
Memcheck:Value8
fun:lzf_compress
}
{
<lua leaks - assume Redis manages these>
Memcheck:Leak
...
fun:lua*
}
{
<Valgrind dlopen bug>
Memcheck:Leak
...
fun:_dl_open
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment