GDB Debugging Guide
give me a complete guide to gdp for debugging C programs
Here's a complete guide to using GDP (GNU Debugger for C programs) to debug C code, covering
everything from installation to advanced features. GDP is essentially GDB (GNU Debugger) but
sometimes customized for embedded development — this guide focuses on GDB, which is what most
people mean when they talk about debugging C with GDP.
🧰 1. Installation
On most systems, GDB is available via package managers.
Linux
bash
sudo apt update && sudo apt install gdb
macOS
bash
brew install gdb
⚠️ macOS requires code-signing GDB. Instructions here.
Windows
Use MSYS2 or install via MinGW:
bash
pacman -S mingw-w64-x86_64-gdb
🏗️ 2. Compiling Your C Program with Debug Symbols
To debug properly, compile with the `-g` flag:
bash
gcc -g -o myprog myprog.c
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/7
Do not use `-O2` or higher optimization during debugging, as it can inline/remove variables.
🧭 3. Starting GDP/GDB
bash
gdb ./myprog
You’ll see a prompt like:
scss
(gdb)
🚦 4. Basic Commands
Command Description
`run` Start the program
`break main` Set a breakpoint at `main()`
`break filename:line` Break at a specific line
`next` Step over to the next line
`step` Step into the function
`continue` Resume program execution
`print var` Print the value of `var`
`list` Show source code around current line
`backtrace` or `bt` Show the call stack
`quit` Exit GDB
🧪 5. Debugging a Crash (e.g., Segmentation Fault)
1. Compile with `-g`
2. Run:
bash
gdb ./myprog
(gdb) run
3. After crash, GDB halts and shows the signal:
nginx
Program received signal SIGSEGV...
4. Use:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/7
bash
(gdb) backtrace
To see the call stack and where the crash occurred.
🧠 6. Inspecting Variables and Memory
bash
(gdb) print var
(gdb) print *ptr
(gdb) x/4xw &var # Examine memory
(gdb) display var # Show var value at every stop
📍 7. Breakpoints (Advanced)
bash
(gdb) break function_name
(gdb) break file.c:25
(gdb) break file.c:25 if x > 10
(gdb) info breakpoints
(gdb) delete 1 # Delete breakpoint 1
🔄 8. Changing Values at Runtime
bash
(gdb) set var x = 99
(gdb) set *ptr = 0xDEADBEEF
⏱️ 9. Watchpoints
Watch for changes in variables:
bash
(gdb) watch var
(gdb) watch *ptr
🧳 10. Running with Arguments
bash
(gdb) set args arg1 arg2
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/7
(gdb) run
Or start with:
bash
gdb --args ./myprog arg1 arg2
🧵 11. Multi-threaded Debugging
Use:
bash
info threads
thread 2 # Switch to thread 2
🧪 12. Testing Memory Leaks with Valgrind (Bonus)
For memory issues:
bash
valgrind ./myprog
Or debug with:
bash
valgrind --vgdb=yes --vgdb-error=0 ./myprog
And connect with GDB.
📜 13. Using GDB Scripts
Automate with `.gdbinit`:
bash
# .gdbinit
break main
run
🧩 14. Visual GDB Frontends (Optional)
DDD – classic GUI for GDB
GDB Dashboard – better CLI interface
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/7
VS Code – use the C/C++ extension
Eclipse CDT – good for large projects
🔧 15. Common Tips
Use `info locals` to see all local variables.
Use `frame n` to move in the call stack.
Use `break abort` to catch abnormal exits.
Use `finish` to run until current function returns.
🧹 16. Exit GDB
bash
(gdb) quit
🧩 Optional: Embedded GDP (GDB for Embedded)
If you're using GDP for embedded (not standard Linux), it often involves:
Remote targets (`target remote`)
OpenOCD or ST-Link servers
Scripts like `.gdbinit` to connect to hardware
Let me know if you're using GDP for bare-metal embedded debugging or just regular C programs on
your computer, and I can tailor the guide further.
Would you like a PDF or cheat sheet version of this?
what does this do: gdb: rimuovere le informazioni di debug
• strip rimuove tutte le info di debug da un file oggetto/eseguibile
strip miosorgente.o
La riga che hai indicato:
nginx
strip miosorgente.o
significa che stai rimuovendo tutte le informazioni di debug dal file oggetto (o eseguibile)
`miosorgente.o`.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/7
🔍 Dettaglio: Che cos'è `strip`?
`strip` è un comando Unix/Linux usato per rimuovere simboli non essenziali, in particolare:
Simboli di debug (`-g` flag in compilazione)
Tabelle dei simboli
Altre informazioni usate dal debugger (come GDB)
📦 Esempio pratico
1. Compili il tuo programma con il debug:
bash
gcc -g -o miofile miofile.c
2. Il file `miofile` contiene simboli di debug:
GDB può mostrare variabili, funzioni, righe di codice.
3. Poi usi `strip`:
bash
strip miofile
Ora:
Il file è più leggero
Ma GDB non può più mostrarti nomi di variabili o codice sorgente
È molto più difficile da debuggare
🧠 Quando usare `strip`?
✅ Utile quando:
Vuoi distribuire un eseguibile più piccolo
Vuoi nascondere i dettagli interni del tuo programma
Vuoi evitare reverse engineering facile
❌ Non usarlo se:
Hai ancora bisogno di fare debugging
Vuoi analizzare crash o problemi con GDB
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/7
🛠️ Alternativa: `strip --strip-debug`
Se vuoi solo rimuovere le informazioni di debug, ma mantenere alcuni simboli (per esempio per stack
trace), puoi usare:
bash
strip --strip-debug miofile
🔒 Pro tip
Puoi anche tenere due versioni:
bash
# Versione per debug
gcc -g -o miofile-debug miofile.c
# Versione per distribuzione
cp miofile-debug miofile-release
strip miofile-release
Fammi sapere se vuoi un esempio passo-passo con GDB e `strip` per vedere la differenza.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/7