GDB Debugger Guide: Enhance Development Efficiency

Hello everyone! Today I want to share with you a very useful tool in C++ development – the GDB debugger. As a programmer with years of C++ development experience, I understand how important debugging is for program development. Proper use of GDB can not only help us quickly locate bugs but also deepen our understanding of the program’s operating mechanisms.

What is GDB?

GDB (GNU Debugger) is the most commonly used debugging tool on the Linux platform, allowing us to pause the program during execution, view variable values, set breakpoints, and more. Imagine GDB as a “microscope” that lets us observe every detail of the program’s execution.

Getting Started with GDB

First, we need to include debugging information when compiling the program. Let me demonstrate with a simple example:

// example.cpp
#include <iostream>

int calculate(int a, int b) {
    int result = a + b;
    return result;
}

int main() {
    int x = 10;
    int y = 20;
    int sum = calculate(x, y);
    std::cout << "Sum is: " << sum << std::endl;
    return0;
}

Compilation command:

g++ -g example.cpp -o example

Tip: The -g parameter adds debugging information to the executable file, which is necessary for using GDB.

Basic Debugging Commands

1. Start and Run

gdb example    # Start GDB
(gdb) run      # Run the program
(gdb) start    # Pause at the main function

2. Breakpoint Operations

Breakpoints are the most commonly used feature in debugging, like setting up a “roadblock” in the program that makes it stop at a specified location:

(gdb) break main                 # Set a breakpoint at the main function
(gdb) break example.cpp:12       # Set a breakpoint at line 12 of the source file
(gdb) info breakpoints          # View all breakpoints
(gdb) delete 1                  # Delete breakpoint 1
(gdb) disable 2                 # Disable breakpoint 2
(gdb) enable 2                  # Enable breakpoint 2

3. Program Execution Control

(gdb) next (or n)              # Execute the next line (does not enter the function)
(gdb) step (or s)              # Execute the next line (will enter the function)
(gdb) continue (or c)          # Continue execution until the next breakpoint
(gdb) finish                   # Run until the current function returns

4. Viewing Variables and Expressions

(gdb) print x                  # Print the value of variable x
(gdb) display x               # Set variable x to automatically display
(gdb) whatis x               # View the type of variable x
(gdb) info locals            # View all local variables

Advanced Debugging Techniques

1. Conditional Breakpoints

Conditional breakpoints are particularly useful when we need to pause the program only under specific conditions:

(gdb) break 10 if x > 5      # Pause at line 10 if x>5

2. Watchpoints

Watchpoints can pause the program when the value of a variable changes:

(gdb) watch x                # Pause when the value of variable x changes

3. Backtrace the Call Stack

When an exception occurs, checking the call stack is very important:

(gdb) backtrace (or bt)      # Display the current call stack

Note: Remember to keep the source code during debugging and ensure that the debug version is used during compilation (with the -g parameter).

Practical Debugging Tips

  1. Set Breakpoints Wisely: Do not set too many breakpoints, as this can complicate the debugging process.

  2. Use Conditional Breakpoints: Conditional breakpoints are more effective than regular breakpoints for issues in loops.

  3. Keep Source Code Clean: Good code formatting helps with debugging.

  4. Document the Debugging Process: Debugging complex bugs may require multiple attempts, so documenting the process is important.

Mini Exercise

Try to use GDB to solve the following problem:

int findMax(int arr[], int size) {
    int max = arr[0];
    for(int i = 0; i <= size; i++) {  // There is a bug here
        if(arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

Can you find the bug in this code using GDB? (Hint: Array out of bounds)

Conclusion

Today we learned the basic usage of GDB, from simple breakpoint settings to advanced applications like conditional breakpoints and watchpoints. GDB is a powerful tool, and mastering it can greatly enhance our debugging efficiency. Remember, debugging is not only a process of solving problems but also a great opportunity to learn and understand the code.

I hope this tutorial helps you! Next time, we will explore more advanced features of GDB. If you have any questions, feel free to discuss in the comments section.

Happy debugging!

Leave a Comment