Lesson 25: gdb — Debugging Native Code and Core Dumps
Your C++ program crashes with a segfault — null pointer dereference, stack overflow, buffer overflow. How do you find the line that caused it? gdb (GNU Debugger) is the standard tool for debugging native code on Linux. It lets you stop at breakpoints, step line by line, inspect variable values, and
gdb is like a detective who can freeze time. When a program crashes it asks: 'what was the value of this variable at the moment of the crash? Who called this function? Where exactly were we?' A core dump is a memory snapshot saved at the moment of the crash — like a crime scene photo you can examine hours after the event occurred.
- gdb
- The standard debugger for C/C++ on Linux. Enables step-by-step execution, breakpoints, variable inspection, and core dump analysis.
- breakpoint
- An instruction to gdb to stop execution when reaching a specific line of code. Lets you inspect program state at a precise point.
- backtrace
- A gdb command that displays the current call stack: the list of all functions called and not yet returned, from newest to oldest.
- core dump
- A file created when a program crashes, containing a snapshot of the program's memory, registers, and call stack at the moment of the crash. gdb can analyze it post-mortem.
- watchpoint
- Like a breakpoint but on a variable: gdb stops every time the variable changes value. Useful for finding where in the code a value changes when it should not.