Loading...

C essentialsDebugging & Safety

Debugging with GDB & sanitizers

15 min lesson

Next
  • Debugging is the process of finding and fixing bugs, and GDB plus sanitizers are two of the most powerful tools for doing it in C.
  • These tools matter because C does not protect you from mistakes like out-of-bounds access or NULL dereferences, so you need strong tools to catch them.
  • Think of GDB as a microscope that lets you pause a program and inspect it from the inside, and sanitizers as a smoke alarm that goes off the moment something dangerous happens.
  • GDB is the GNU Debugger; it lets you run a program step by step, set breakpoints, print variables, and inspect the call stack.
  • A breakpoint is a line where GDB pauses execution so you can look around: break main.c:12.
  • Sanitizers are compiler features that add runtime checks; -fsanitize=address catches memory errors and -fsanitize=undefined catches undefined behavior.
  • The computer, when built with sanitizers, inserts extra checks into the code that report problems with file and line numbers.
  • The syntax is: gcc -g -fsanitize=address,undefined main.c -o app then gdb ./app or ./app.
  • A realistic use: a program that crashes with a segfault; GDB shows which line, and a sanitizer build shows exactly what memory error happened.
  • A surprising detail: sanitizer builds are slower and larger, so use them during testing, not in the final release.
  • The example program has a subtle bug: it writes past the end of an array; the comments explain how a sanitizer build would report it and how to fix it.
Keep in mind
  • Compile with -g so GDB can show source lines and variable names.
  • Use -fsanitize=address,undefined during testing to catch memory and UB bugs early.
  • Set breakpoints and step through code to see what actually happens, not what you think happens.
  • Read the sanitizer report top-down; the first line usually names the bug.
  • Debugging is a skill, not a talent. You are becoming a real engineer!
main.c
#include <stdio.h>

int main(void) {
    // An array of 3 ints, indices 0 to 2
    int values[3] = {1, 2, 3};

    // This loop is a bug on purpose: it writes index 3, out of bounds
    // A sanitizer build will stop here and report a buffer overflow
    for (int i = 0; i <= 3; i++) {
        // For i == 3 this writes past the end of the array
        values[i] = i * 10;
    }

    // Print the safe values we can read
    printf("%d %d %d\n", values[0], values[1], values[2]);
    return 0;
}

What happens: 0 10 20

Breaking it down

Build with -g

gcc -g main.c -o app

Adds debug information so GDB can show source lines and variable names. Without it, GDB shows only addresses.

Breakpoint

break main.c:10

Tells GDB to pause on that line. You can then inspect variables and step forward one line at a time.

Sanitizer build

-fsanitize=address,undefined

Adds runtime checks for memory errors and undefined behavior. Bugs are reported with file and line numbers.

The bug

for (int i = 0; i <= 3; i++)

The condition should be i < 3. With <=, the loop writes values[3], which is past the end of the array.

The fix

for (int i = 0; i < 3; i++)

Stops at index 2, the last valid one. The sanitizer no longer reports an error.

Debugging tools

ToolPurposeExample
gcc -gAdd debug infogcc -g main.c
GDBStep and inspectgdb ./app
breakPause on a linebreak main.c:10
printShow a variableprint values[0]
ASanCatch memory errors-fsanitize=address
UBSanCatch undefined behavior-fsanitize=undefined

A helpful analogy

GDB is like a microscope that lets you pause a film and examine a single frame in detail. Sanitizers are like a smoke alarm that goes off the moment something dangerous happens. The microscope helps you understand; the alarm keeps you safe.

Nesting rules

  • 1.Compile with -g for GDB; without it, variable and line info is missing.
  • 2.Use sanitizers during development and testing, not for release builds.
  • 3.Fix the first error reported; later errors are often caused by the first one.
  • 4.Use gdb ./app and then run, not gdb app.c, to debug the compiled program.

Common mistakes

Debugging without -g

Avoid

gcc main.c -o app gdb ./app

Use instead

gcc -g main.c -o app gdb ./app

Without -g, GDB cannot show source lines or variable names, so stepping and printing are much less useful.

Ignoring the first sanitizer error

Avoid

jump to the last error in the log

Use instead

read the first error and fix it

Later errors are often consequences of the first one. Fixing the first error often makes the rest disappear.

Shipping a sanitizer build

Avoid

release built with -fsanitize=address

Use instead

test with sanitizers, release without

Sanitizer builds are slower and larger and require the sanitizer runtime. Use them for testing only.

Quick tips

  • •Learn four GDB commands first: run, break, next, print.
  • •Use backtrace (bt) in GDB to see how the program got to the current line.
  • •Build a sanitizer version side by side with the normal version for testing.
  • •Try changing <= to < in the example and run with -fsanitize=address to see the error disappear.
  • •Read sanitizer messages from top to bottom; the first line names the error type.
Previous
Next