GDB Debugger
The GDB debugger (GNU Debugger) is a powerful tool used in Linux to debug programs written in
languages like C, C++, and others. It helps you understand what your program is doing while it runs—
or why it crashes.
What is GDB?
GDB (GNU Debugger) is part of the GNU Project and is widely used on Linux systems to:
• Debug runtime errors
• Trace program execution
• Inspect variables and memory
• Analyze crashes (segmentation faults)
Compile Program for Debugging
Before using GDB, compile your program with the -g flag:
gcc -g program.c -o program
This includes debugging symbols.
Starting GDB
gdb ./program
Basic GDB Commands
Run the Program
run
Set Breakpoints
break main // at main function
break 10 // at line 10
break function_name // at function
View Source Code
list
Step Execution
next // execute next line (skip function details)
step // go inside function
Print Variable Values
print x
Continue Execution
continue
Exit GDB
quit
Example Debugging
Sample Code (with bug)
#include <stdio.h>
int main() {
int a = 5, b = 0;
int c = a / b; // error: division by zero
printf("%d\n", c);
return 0;
Debug Steps
gcc -g test.c -o test
gdb ./test
(gdb) run
When it crashes:
(gdb) bt // backtrace
(gdb) print a
(gdb) print b
Useful Advanced Commands
Backtrace (Call Stack)
bt
Watch Variable Changes
watch x
Inspect Memory
x/10x &x
Change Variable Value
set var x = 10
Core Dump Debugging
If program crashes:
ulimit -c unlimited
./program // crash generates core file
gdb ./program core
Why GDB is Important (especially for you )
Since you're working on embedded systems and C/C++, GDB is essential for:
• Debugging firmware logic
• Tracking pointer errors
• Understanding memory issues
• Analyzing real-time behavior (with gdbserver in embedded targets)