Step Into Labs

Step Into C · Lesson 16 · Debugging

Debugging

Breakpoints, stepping, variables, the call stack and crashes, with a real debugger.

This lesson is free. Open it in the app to write and run the code, with the real compiler and debugger. No sign-up needed.
Open the lesson

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.

Watch out
The highlighted line has not run yet. The debugger stops *before* a line, so the variables show the state just before it executes. Step once to see its effect.

Moving through the program

ButtonKeyWhat it does
Step overF10run the current line, including any function it calls, and stop at the next line
Step intoF11if the line calls one of *your* functions, stop at that function's first line
Step outShift+F11run until the current function returns to its caller
ContinueF5run until the next breakpoint
Step backShift+F10go one step back in time
StopShift+F5leave 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

Crashes

When the program crashes, the recording ends on the exact line that crashed, highlighted in red, with the reason:

SignalUsually 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 recursionstack overflow: the call stack shows the same function hundreds of times
SIGFPEinteger division by zero

The same ideas in every debugger

HereGDB commandx64dbg / WinDbg
set a breakpointbreak main.c:12F2 / bp
step overnextF8 / p
step intostepF7 / t
step outfinishCtrl+F9 / gu
continuecontinueF9 / g
variablesinfo locals, print xLocals window / dv
call stackbacktraceCall 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.

Watch out
The Assembly tab (next to Debugger) shows what gcc made of your code at -O0 or with optimizations. Click a line in your code and the matching instructions light up.

Missions

  1. One too many

    Put avg in the Input tab and run it: the average of 10, 20, 30, 40 comes out as 275.00. Set a breakpoint on the sum += values[i]; line, press Debug, then Continue (F5) a few times while watching i and sum. Which value of i should never happen? Fix the loop so it prints average = 25.00.

  2. Where did it go?

    Input percent: percent(3, 4) should be 75 and percent(1, 8) should be 12, but both print 0. Step into percent and think about what part / whole is for two ints. Reorder the arithmetic so the result is right, still using only integers.

  3. The crash

    Input find: the program crashes. Press Debug and Continue: the recording ends on the red line. Look at u in the Variables panel, and at the call stack to see how you got there. Make print_level handle a missing user by printing mallory: no such user instead.

  4. Counting nothing

    Input vowels: "debugger" has 3 vowels, but the program prints 0. Step into count_vowels and watch count (the pointer) and *count. Which one is the ++ actually changing? Fix it.

  5. Too deep

    Input sum: sum_to(4) works, then sum_to(5) crashes. Debug it, step into sum_to a few times, and look at the call stack and at n. Why does the recursion never stop for odd numbers? Fix the base case so it prints sum_to(4) = 6 and sum_to(5) = 9.

This lesson is free. Open it in the app to write and run the code, with the real compiler and debugger. No sign-up needed.
Open the lesson