GDB & Valgrind

GDB and Valgrind are tools which may help you debugging your program and finding possible memory leaks and such.


$ gcc -g3 -Wall -Wextra -std=c99 -o main main.c


$ gdb main

Manual / Help

(gdb) help |command|

Breakpoints at Line

(gdb) breakpoint main.c:5

Run Program

(gdb) run

Watch Variables

(gdb) watch my_var

Print Variable

(gdb) print/d my_var

NOTE: Here, you may use d for int, s for char*, c for char, etc.

Show Breakpoints / Watchpoints

(gdb) info breakpoints
(gdb) info watchpoints

Delete (Breakpoint)

(gdb) delete my_var

Finish Function

(gdb) finish

Step by Step / Instruction by Instruction

(gdb) step
(gdb) next


(gdb) stacktrace
(gdb) where


After above compilation, run:

$ valgrind --leak-check=full -v ./main

NOTE: The -v flag will give some verbose output. Feel free to remove that flag if it's too much information for you.

Blog Comments powered by Disqus.