Advertising Time
Click the card below and give us a follow before you go!
1. Introduction to GDB
GDB (GNU Debugger) is a powerful open-source debugging tool that helps developers debug programs written in languages such as C/C++. Mastering the basic usage of GDB is very beneficial for both beginners and experienced programmers. This article will take you into the world of GDB, from basics to practical skills, allowing you to quickly get started with debugging programs.
GDB is part of the GNU project and is developed by the Free Software Foundation (FSF). It supports multiple operating system platforms, such as Linux, Unix, and Windows. The main function of GDB is to help developers view the state of the program during execution, including the values of variables and the execution flow, thus locating and fixing errors in the program.
2. Installing GDB
Most Linux distributions come with GDB pre-installed, and you can easily install it via the package manager. For example, on Ubuntu, you can use the following command to install:
sudo apt update
sudo apt install gdb
On Windows, you can obtain GDB by installing tools such as MinGW or Cygwin.
3. Compiling Programs
To use GDB to debug a program, you need to include debugging information during compilation. For example, using GCC, compile with the<span>-g</span>
option:
gcc -g -o my_program my_program.c
The<span>-g</span>
option tells the compiler to generate debugging information,<span>-o my_program</span>
specifies the output executable file name as<span>my_program</span>
, and<span>my_program.c</span>
is the source code file.
4. Starting GDB
After compiling the program, you can start GDB. Enter the following command in the terminal:
gdb ./my_program
This will enter the GDB interactive interface, waiting for your debugging commands.
5. Basic Debugging Commands
1. View Source Code
In GDB, you can use the<span>list</span>
command to view the source code. For example:
list main
This will display the source code near the<span>main</span>
function. You can also specify a line number to view code at a specific location, such as:
list 10
2. Set Breakpoints
Breakpoints are a very important tool during debugging. Use the<span>break</span>
command to set a breakpoint at a specified location. For example:
break main
This will set a breakpoint at the entry of the<span>main</span>
function. You can also set a breakpoint by line number, such as:
break 20
3. Run the Program
After setting breakpoints, use the<span>run</span>
command to start running the program. The program will pause execution when it hits a breakpoint, waiting for your further actions.
4. View Variables
When the program is paused, you can use the<span>print</span>
command to view the value of a variable. For example:
print my_variable
This will display the current value of the<span>my_variable</span>
variable.
5. Step Execution
Use the<span>next</span>
command to execute the program step by step, executing one line of source code at a time. If the current line is a function call,<span>next</span>
will skip the function body and directly execute the next line. If you want to step into the function for debugging, you can use the<span>step</span>
command.
6. Continue Execution
If you want the program to continue executing until it hits the next breakpoint or the program ends, you can use the<span>continue</span>
command.
7. View Call Stack
When the program is paused at a breakpoint, you can use the<span>backtrace</span>
or its shorthand<span>bt</span>
command to view the current call stack information. This helps to understand the execution flow of the program.
6. Debugging Example
Suppose we have the following simple C program<span>example.c</span>
:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int x = 10;
int y = 20;
int result = add(x, y);
printf("The result is: %d\n", result);
return 0;
}
Compile and start GDB:
gcc -g -o example example.c
gdb ./example
Set breakpoints in the<span>main</span>
function and the<span>add</span>
function:
break main
break add
Run the program:
run
The program will pause at the entry of the<span>main</span>
function. Use the<span>next</span>
command to execute to the<span>add</span>
function call, then use the<span>step</span>
command to step into the<span>add</span>
function. In the<span>add</span>
function, you can view the values of the variables<span>a</span>
and<span>b</span>
, and then continue executing until the program ends.
7. Conclusion
GDB is a feature-rich debugging tool, and mastering its basic usage can greatly improve debugging efficiency. This article introduced the installation of GDB, compiling programs, starting GDB, and some basic debugging commands. Of course, GDB’s capabilities go beyond this; it also supports conditional breakpoints, watch variables, multithreaded debugging, and other advanced features. As you delve deeper into GDB, you will discover its power in debugging complex programs. We hope this article helps you quickly get started with GDB, making your programming journey smoother.