Skip to content
AgileLogics
5 min read

Debugging with GDB and Valgrind

Prerequisites: Build tools: Make

GDB steps through a running program, inspecting variables at the exact moment something goes wrong:

gcc -g program.c -o program   # -g keeps debug info
gdb ./program

(gdb) break main
(gdb) run
(gdb) next            # step to the next line
(gdb) print someVar    # inspect a variable
(gdb) backtrace        # show the call stack after a crash

Valgrind runs a program and reports memory errors gdb won't catch on its own — leaks, use-after-free, and reads of uninitialized memory:

valgrind --leak-check=full ./program

Common mistakes

  • Compiling without -g and then trying to debug — without debug symbols, gdb can't show source lines or meaningful variable names.