Linux Tactic

Mastering GDB: Essential Commands for Effective Debugging

Introduction to GDB

As software developers, we all aspire to write error-free code. However, we know that bugs and errors are always lurking in our programs.

Debugging is an essential process in software development, and tools like GDB are crucial in facilitating this process. In this article, we will learn about GDB- a powerful and popular debugger used to identify and fix errors in C++, C, and Fortran programs.

Overview of GDB Debugger

GDB is a command-line based debugger used to identify and fix errors in programs written in various programming languages like C++, C, and Fortran. The debugger allows developers to observe how their program executes and identify memory issues like segmentation faults, and

run-time errors.

Need for GDB Debugger

Errors occur in programs for various reasons, such as syntax errors or logical errors. These errors can cause programs to crash, or they can have unintended consequences.

Debugging programs is the process of finding and fixing these errors. Debuggers like GDB are essential for developers since they come with features that allow for effective debugging and resolve issues that may take hours to find by reviewing source code.

Invoking the GDB Debugger

Invoking GDB involves launching the debugger in the terminal window and providing it with an executable file. Here is how you can initiate the GDB debugger:

Ways to Invoke GDB

One can invoke the GDB debugger in more than one way. One can use the command line or Terminal window.

Use the following command to launch the GDB debugger: gdb < file >. For instance, gdb sample1 will launch the debugger and select the file named sample1.

You can use GDB with an executable file that has been compiled with the -g option.

Attaching Executable Files

The compiler generates an executable file when you compile a program written in languages like C++. The -g option instructs the compiler to produce a binary file (executable) that can

run with the debugger.

Here is an example of compiling a C++ program with -g option. $ g++ -g -o sample1 sample1.cpp

This instruction- “g++ -g -o sample1 sample1.cpp- produces an executable file called ‘sample1’ that can be executed with the GDB debugger.

Alternatively, you can compile the code and launch the debugger at the same time by using the following command in your terminal window:

$ gdb < file >

For instance, gdb sample1.cpp will compile sample1.cpp and launch GDB debugger once the .exe file is created.


Debugging is an integral part of software development. Even experienced programmers make mistakes, making debugging essential in identifying unintended consequences and logic flaws in a program.

GDB has proved to be a popular debugger that facilitates the debugging of various programming languages. One of GDB’s greatest benefits is its powerful features like setting breakpoints, accessing memory addresses, and customizing instruction trace for effective debugging.

By following the methods and examples above, you’ll be well on your way to effectively debugging your programs.

Common GDB Commands

As we have previously discussed, GDB is a powerful debugger used to identify and fix errors in C++, C, and Fortran programs. In this section, we will delve into some common GDB commands that facilitate the debugging process.

Quitting GDB

Exiting the GDB debugger can be done in one of two ways: by typing ‘quit’ or ‘q’ in the command line, or by pressing Ctrl+D. To exit the debugger, type ‘quit’ or ‘q,’ and press Enter.

Exiting will terminate the current debug session and return control to the terminal.

Running Program Functions


run a function while debugging in GDB, we use the ‘

run’ command followed by the function name and its arguments (if any). For context, we use the ‘getSquare’ function to illustrate how this is done:



using namespace std;

int getSquare(int n) {

return n * n;


int main() {

int num = 7;

cout << "The square of " << num << " is " << getSquare(num) << endl;

return 0;



Here is how we

run the getSquare function while debugging:


Start the GDB debugger in your terminal window and load the executable file by typing ‘gdb ‘ in the command-line interface. 2.

Add a breakpoint by typing the ‘break’ command in the command line;


break 7


Here, the number 7 represents the seventh line of our code, which contains the function getSquare. 3.

Run the program using the ‘

run’ command:




4. Once the program stops at the breakpoint, we can now

run the ‘getSquare function’ as shown below:


run 13


Here, we give our argument of “13” within the

run command. This will output “The square of 13 is 169.”

Accessing Help

GDB comes with a ‘help’ command to provide the developer with essential information about how to use the debugger. Using the ‘help’ command opens a manual page that contains everything you need to know about GDB.

To access help, type ‘help’ followed by the command you are seeking assistance with. For instance, if you want help with the ‘

run’ command, type ‘help


Adding Breakpoints

A breakpoint is a debugging mechanism that enables the developer to stop the execution of a program at a particular point in the code. Adding a breakpoint in GDB involves specifying the line number or function name where you want the program execution to stop.

There are four ways of adding breakpoints in GDB:

1. To add a breakpoint at line 7 in the code:


break 7


2. To add a breakpoint at getSquare function:


break getSquare


3. To add a breakpoint with a particular condition:


break file_name.cpp:8 if i==3


Here, we add a breakpoint at line eight of file_name.cpp and stop the execution of the program when the condition is met (i.e., i==3).

4. To add a hardware-assisted breakpoint, we type:




This command adds a breakpoint that uses hardware features to stop the execution of a program at specific line numbers.

Continuing Execution

To continue the execution of a program during the debugging process, we use the ‘continue’ command. Once the program stops at a breakpoint, we can continue the execution using the ‘continue’ command.

This command enables the program to execute without stopping until it reaches the next breakpoint or the end of the program. The ‘continue’ command can also be abbreviated as ‘c.’

Printing Next Execution Line

Continuing from a breakpoint can be frustrating, not knowing which line of the code is being executed. Here, we use the ‘next’ command to print out the next line of the program being executed.

This is helpful when trying to analyze small sections of code for bugs. The command can also be abbreviated as ‘n.’

Deleting Breakpoints and Checkpoints

In GDB, we can delete breakpoints or checkpoints. Checkpoints allow us to return the program to a pre-specified point in time, back to a checkpoint we set in the past.

To delete breakpoints or checkpoints, we can use the ‘delete’ or ‘d’ command followed by the breakpoint number. For instance, typing ‘delete 1’ commands GDB to delete the breakpoint with ID 1.

To remove all breakpoints or checkpoints at once, type ‘delete’ followed by a star (*) as shown:


delete *


This command deletes all breakpoints or checkpoints in your code. Further, one can use the ‘info’ command to view active breakpoints in their code.

Set Arguments

The ‘set args’ command sets the argument list for the program being debugged. When using this command, we want to set the

runtime arguments for our program at the point of entry.

For instance, if we use the ‘getSquare function’ for negative numbers, we will need to pass them as arguments. Here’s an example of setting arguments:


set args -4


This method sets the arguments for our program as negative four. If the program requires more than one argument, it should be concatenated with spaces between each argument.

We can also view the arguments we have set using the ‘show args’ command.


Mastering the GDB debugger is essential in understanding how C++, C, and Fortran programs work. The above commands are some of the most common commands that developers use daily in their debugging processes.

By mastering these GDB commands, developers can save time by efficiently identifying bugs and their root causes. These commands improve overall software development, ensuring the software

runs seamlessly with no errors.

In conclusion, GDB is an important tool for developers to identify and fix errors in programs written in C++, C, and Fortran. The article explored some of the common GDB commands, including adding and deleting breakpoints,

running program functions, and accessing help, among other important commands.

By mastering these commands, developers can improve their debugging skills, save time, prevent bugs, and enhance overall software development. The importance of debugging in software development cannot be overstated, and GDB provides critical features for effective debugging.

Overall, GDB and its commands serve as a powerful tool for developers that can ultimately lead to faster and more efficient production of quality software.

Popular Posts