Here's a practical, minimal guide for using GDB in Ubuntu terminal — from launching to setting breakpoints and navigating your code.
🧰 Step 1: Compile with Debug Symbols
Use -g
and disable optimizations (-O0
preferred for debugging):
🚀 Step 2: Launch GDB
You'll see a (gdb)
prompt.
🎯 Step 3: Set Breakpoints
You can set breakpoints in three main ways:
✅ a. At a function
✅ b. At a specific file and line
✅ c. At a method in a class
If the symbol is mangled (C++), GDB usually demangles automatically, especially if compiled with
-g
.
▶️ Step 4: Run the Program
🧠Step 5: Navigate
Command | Meaning |
---|---|
next or n | Step over current line |
step or s | Step into function call |
finish | Run until current function returns |
continue or c | Continue until next breakpoint |
list | Show source code around current line |
info locals | Show all local variables |
print x | Print variable x |
⛏️ Step 6: Manage Breakpoints
List breakpoints:
Delete a breakpoint:
Disable/Enable breakpoint temporarily:
🧪 Example Session
🧠Pro Tips
-
Press
<TAB>
for autocompletion. -
Use
layout src
in GDB to get a TUI (text UI). -
quit
to exit.
🪟 Bonus: GDB Text UI Mode
In GDB:
→ Shows source code window with current line highlighted. Use arrow keys or n
, s
, etc.
Let me know if you'd like:
-
Watchpoints (for memory change)
-
Debugging core dumps
-
Pretty printing with
gdbinit
-
Automating GDB session with scripts
Comments
Post a Comment
If you have any doubts, please let me know.