<< Back

How to Use GDB

On This Page

  1. Introduction
  2. Preparing to use GDB
  3. Example Program for Debugging
  4. Tracing Code with GDB
  5. Commonly Used GBD Commands
  6. More Information

A. Introduction

This document describes how to use GDB for the course CS-11: Introduction to Programming Concepts and Methodology, C++.

GDB is a GNU debugger that allows us to trace a program while it runs, or to see what the program was doing when it crashed. As such it is a valuable aid in debugging software. The software is available as part of your Cygwin installation. On Linux computers, GDB is usually installed along with GCC/G++. The GDB manual, Debugging with GDB, is available online.

GDB was installed on the Mac as well, prior to OS X Mavericks. Starting with OS X Mavericks, GDB was replaced by LLDB, which works with clang.

B. Example Program for Debugging

As an example, here is a simple program we can trace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

// Returns the square of a number
double square(double number);

// Displays a message to the console
void log(string funName, double value);

int main() {
    double number = 5;
    log("main", number);
    double result = square(5);
    log("main", result);

    return 0;
}

double square(double number) {
    log("square", number);
    double result = number * number;
    log("square", result);
    return result;
}

void log(string funName, double value) {
    cout << "In " << funName << "() the value is ";
    cout << value << endl;
}

We save the file as "example.cpp" (without the double quotes) in the home folder under our login name.

C. Preparing to use GDB

  1. Before we can use GDB, we must first compile the program we want to debug using the -g option:
    g++ -g -W -Wall -pedantic -o programName sourceFile.cpp
    
    For the example:
    g++ -g -W -Wall -pedantic -o example example.cpp
    
  2. Next, we load the program into the debugger:
    gdb programName
    
    For the example:
    gdb example
    
  3. Once we have loaded the program to debug, GDB will present some information and then show the (gdb) prompt:
    (gdb) 
    

D. Tracing Code with GDB

One a program is compiled and loaded into GDB, we can start tracing code.

  1. Set a breakpoint using: break functionName or break filename:line.
    (gdb) break main
    Breakpoint 1 at 0x4011bf: file example.cpp, line 11.
    (gdb) break 16
    Breakpoint 2 at 0x401283: file example.cpp, line 16.
    
  2. Execute the program by typing: run
    (gdb) run
    Starting program: /home/Student/example
    [New Thread 9888.0x2204]
    [New Thread 9888.0x24cc]
    
    Breakpoint 1, main () at example.cpp:11
    11          double number = 5;
    
  3. Type "next" or "step" to see the next line
    (gdb) next
    12          log("main", number);
    (gdb) step
    log (funName=..., value=5) at example.cpp:27
    27          cout << "In " << funName << "() the value is ";
    
  4. Use "watch" to print the value of a variable whenever it changes
    (gdb) watch value
    Hardware watchpoint 3: value
    
  5. Type continue to resume running until the next breakpoint is reached
    (gdb) continue
    Continuing.
    In main() the value is 5
    
    Watchpoint 3 deleted because the program has left the block in
    which its expression is valid.
    0x00401205 in main () at example.cpp:12
    12          log("main", number);
    
  6. Type quit to exit GDB.

E. Commonly Used GBD Commands

F. More Information

<< Back