Stop guessing, start looking
Until now, when a program printed the wrong thing you added printfs and ran it again. A debugger lets you pause the program at any line and look at everything: every variable, every function call that led there, even the machine instructions. Every professional uses one, and in Windows security work you will live inside debuggers such as x64dbg and WinDbg. They all have the same core ideas you are about to learn.
The Debug button
Press Debug in the toolbar (or F6). The course server compiles your program with debug information (gcc -g), runs it under GDB (the GNU debugger) and records every line it executes. Then you replay that recording in the Debugger tab.
Because it is a recording, you can also go backwards, something most debuggers can't do. Input for the program comes from the Input tab, exactly like Run.
Moving through the program
| Button | Key | What it does |
|---|---|---|
| Step over | F10 | run the current line, including any function it calls, and stop at the next line |
| Step into | F11 | if the line calls one of *your* functions, stop at that function's first line |
| Step out | Shift+F11 | run until the current function returns to its caller |
| Continue | F5 | run until the next breakpoint |
| Step back | Shift+F10 | go one step back in time |
| Stop | Shift+F5 | leave the debugger and edit again |
The slider jumps anywhere in the recording.
Breakpoints
A breakpoint says "stop here". Click left of a line number (or press F9 on a line) to set one: a red dot appears. Continue runs until a line with a breakpoint is about to execute. When you start debugging and have breakpoints, it starts at the first one.
What you can see
- Variables: the local variables and arguments of the current function. Values that changed in the last step are highlighted. A variable that hasn't been assigned yet shows garbage: whatever bytes were in that memory before. That is what "uninitialized" really means.
- Call stack: which function called which.
main()at the bottom, the current function on top. Click a frame to see *its* variables and the line where it is waiting for the call to return. - Output: everything printed so far. What the last step printed is highlighted.
- Disassembly: the machine instructions gcc generated for the current function; the current line's instructions are highlighted. You will learn to read these in the assembly course.
Crashes
When the program crashes, the recording ends on the exact line that crashed, highlighted in red, with the reason:
| Signal | Usually means |
|---|---|
SIGSEGV (segmentation fault, access violation) | reading or writing memory you don't own: a NULL or dangling pointer, an index far out of bounds |
SIGSEGV deep in a recursion | stack overflow: the call stack shows the same function hundreds of times |
SIGFPE | integer division by zero |
The same ideas in every debugger
| Here | GDB command | x64dbg / WinDbg |
|---|---|---|
| set a breakpoint | break main.c:12 | F2 / bp |
| step over | next | F8 / p |
| step into | step | F7 / t |
| step out | finish | Ctrl+F9 / gu |
| continue | continue | F9 / g |
| variables | info locals, print x | Locals window / dv |
| call stack | backtrace | Call Stack window / k |
This lesson's program
main.c contains five small functions, each with one bug. The program reads a command from stdin and runs one test: put avg, percent, find, vowels or sum in the Input tab, then press Debug to see that test run.
Some bugs are easy to spot by reading. Use the debugger anyway: the point of this lesson is building the habit.
-O0 or with optimizations. Click a line in your code and the matching instructions light up.