0Pricing
C++ Academy · Lesson

Using gdb and lldb

Step through programs.

Using gdb and lldb is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Interactive Debuggers

gdb (GNU) and lldb (LLVM) let you pause a running program, inspect variables, and step through code line by line. Their commands are similar.

Compile with Debug Info

Always build with -g (and ideally -O0) so the debugger can map machine code back to your source.

g++ -g -O0 main.cpp -o app
# then
gdb ./app

Setting Breakpoints

A breakpoint pauses execution at a chosen location.

  • gdb: break main.cpp:10 or break myFunc
  • lldb: breakpoint set --file main.cpp --line 10 or b myFunc

Running the Program

Start execution with run (or r). The program stops at the first breakpoint it hits, ready for inspection.

Stepping Through Code

Core stepping commands (same names in gdb and lldb):

  • next / n: run one line, stepping over calls
  • step / s: step into a call
  • finish: run until current function returns
  • continue / c: resume until next breakpoint

Inspecting Variables

Print values to understand program state.

  • gdb: print x, print arr[2]
  • lldb: frame variable or p x

A Sample Session Target

Consider this program; you might break at the loop and print sum each iteration.

#include <iostream>

int main() {
    int sum = 0;
    for (int i = 1; i <= 3; ++i) {
        sum += i;
    }
    std::cout << sum << "\n";
    return 0;
}

The Call Stack

When stopped, backtrace (gdb) or bt (lldb) shows the chain of function calls. Use frame N to switch to a caller and inspect its locals.

Watchpoints

A watchpoint stops the program when a variable changes, helping find where unexpected modifications happen.

  • gdb: watch counter
  • lldb: watchpoint set variable counter

Conditional Breakpoints

Break only when a condition holds, avoiding manual stepping.

  • gdb: break loop.cpp:5 if i == 99
  • lldb: breakpoint set --line 5 --condition "i == 99"

Debugging Crashes

Run the crashing program under the debugger; on a segfault it stops at the faulting line. Then use bt to see how you got there. You can also analyze a core dump after the fact.

Quick Check

Recall the stepping commands.

Recap

You learned interactive debugging.

  • Build with -g -O0
  • Set breakpoints, run, then next/step/finish
  • Inspect with print/frame variable and backtrace
  • Watchpoints and conditional breakpoints target tricky bugs

Frequently asked questions

Is the “Using gdb and lldb” lesson free?

Yes — the full text of “Using gdb and lldb” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Using gdb and lldb”?

Step through programs. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Using gdb and lldb” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using gdb and lldb
  2. AddressSanitizer
  3. UBSan and TSan
  4. Valgrind Basics
← Back to C++ Academy