This guide will help you learn how to use GDB (GNU Debugger) to debug C++ programs by setting breakpoints and examining values.

Table of Contents

  1. Introduction to GDB
  2. Compiling with Debugging Information
  3. Starting GDB
  4. Setting Breakpoints
  5. Running the Program
  6. Examining Values
  7. Stepping Through Code
  8. Practical Examples
  9. Common GDB Commands

Introduction to GDB

GDB (GNU Debugger) is a powerful tool that allows developers to:

  • Run programs in a controlled environment
  • Stop execution at specific points (breakpoints)
  • Examine what’s happening during program execution
  • Change variables to test different scenarios

Compiling with Debugging Information

Before you can effectively use GDB, you need to compile your program with debugging information:

g++ -g -o program_name source_file.cpp

The -g flag adds debugging information that GDB needs.

Starting GDB

Start GDB with your compiled program:

gdb ./program_name

You’ll see the GDB prompt: (gdb)

Setting Breakpoints

Breakpoints pause execution at specific locations. There are several ways to set breakpoints:

  1. By function name:
(gdb) break printValue
  1. By file and line number:
(gdb) break debug1.cpp:5
  1. By line number in the current file:
(gdb) break 5

To list all breakpoints:

(gdb) info breakpoints

To delete a breakpoint:

(gdb) delete 1  # Delete breakpoint #1

Running the Program

Start your program:

(gdb) run

The program will run until it hits a breakpoint, encounters an error, or completes execution.

Examining Values

Once the program is paused at a breakpoint, you can examine values:

  1. Print a variable’s value:
(gdb) print value  # Print the variable 'value'
  1. Print an expression:
(gdb) print value + 10
  1. Display a variable’s value (updates automatically):
(gdb) display value

Stepping Through Code

Navigate through your program:

  1. Step to the next line (will step into functions):
(gdb) step
  1. Next line (will not step into functions):
(gdb) next
  1. Continue execution until the next breakpoint:
(gdb) continue
  1. Finish the current function:
(gdb) finish

Practical Examples

Let’s use our example files to demonstrate GDB usage.

Example 1: Debug the First Program

Using debug1.cpp:

#include <iostream>

void printValue(int value)
{
    std::cout << value << '\n';
}

int main()
{
    printValue(5);

    return 0;
}

Debugging session:

$ g++ -g -o debug1 debug1.cpp
$ gdb ./debug1
(gdb) break main
(gdb) run
# Program stops at main()
(gdb) step
# Steps into printValue(5)
(gdb) print value
$1 = 5
(gdb) continue
# Program finishes

Example 2: Debug with Multiple Function Calls

Using debug2.cpp:

#include <iostream>

void printValue(int value)
{
    std::cout << value << '\n';
}

int main()
{
    printValue(5);
    printValue(6);
    printValue(7);

    return 0;
}

Debugging session:

$ g++ -g -o debug2 debug2.cpp
$ gdb ./debug2
(gdb) break printValue
(gdb) run
# Program stops at first call to printValue
(gdb) print value
$1 = 5
(gdb) continue
# Program stops at second call to printValue
(gdb) print value
$2 = 6
(gdb) continue
# Program stops at third call to printValue
(gdb) print value
$3 = 7
(gdb) continue
# Program finishes

Common GDB Commands

Command Description
help Get help on GDB commands
break or b Set a breakpoint
run or r Start program execution
continue or c Continue execution until next breakpoint
next or n Execute next line without stepping into functions
step or s Execute next line, stepping into functions
print or p Print variable value
display Set automatic display of a variable
info locals Show all local variables
info breakpoints List all breakpoints
delete or d Delete breakpoints
quit or q Exit GDB
backtrace or bt Show call stack
finish Run until the current function returns
watch Set watchpoint on variable

Final Tips

  • Use up and down arrow keys to navigate command history
  • Use ctrl+L to clear the screen
  • Try layout src to see source code alongside GDB commands
  • Use tab completion for commands and variable names
  • Add -Wall when compiling to see all warnings