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 srcin GDB to get a TUI (text UI). -
quitto 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
No comments:
Post a Comment
If you have any doubts, please let me know.