What is GDB?

Key takeaways:

  • GDB is an essential tool for debugging across various programming languages, including C and C++.

  • The ability to set breakpoints and step through code allows for meticulous inspection of program flow and variable states.

  • While lacking a GUI, GDB’s command-line interface offers powerful and precise control over debugging tasks.

GDB, also known as the GNU Project Debugger, is a free debugging tool that allows the user to see the underlying workings of any program. This debugger mainly works on Unix systems and helps debug code in multiple languages, like:

It is a well-crafted debugger that lets users iterate through the code lines individually while updating them with information about values in registers and other assembly-level information. It informs the user about the errors in the code as well. Even though most applications and debuggers nowadays rely on a Graphical User Interface (GUI), GDB does not have one. It shows text-based outputs on the console.

Features of GDB

The following are a few main features of GDB explained in detail.

1. Breaking through the program

GDB allows the user to add breakpoints to the program so that the user can dive deep into it at a specific time interval.

Let's examine C code and its corresponding assembly code to understand the concept of breaking through the program.

main.c
main.c

To break the program at a specific part of the code, there are three ways:

Method 1: Line break

Breaking at a specific line is one way to break the code. To perform that operation, the syntax is

break filename.c:line_number
Example

In the above-mentioned main.c file if the user wants to break the program at line 7, then the command will be something like this:

break main.c:7

Method 2: Function break

Function breaks are used if the user wants to break at the start of a function. To perform the function break, the syntax will be:

break file_name:function_name

Example

In the above-mentioned main.c file if the user wants to break the program at the main function, then the command will be something like this:

break main.c:main

Method 3: Instruction break

An instruction break requires the user to have the address of the line where they want to break the program. The syntax for this command will be break *PC where PC is the Program Counter/address of the command.

Example

Suppose line 7 has an address of 0x1ffa. The command for breaking the program at this instruction will be something like this:

break *0x1ffa

Other helpful commands

The command to get information about all the breakpoints in the code is info breakpoints. Through this command, the user gets the list of breakpoints in the program, with each breakpoint assigned a number.

The user can enable and disable breakpoints through the enable breakpoint_number and disable breakpoint_number.

If a user wants to remove a breakpoint completely, they can use the delete breakpoint_number command.

To resume the working of a program after a breakpoint, the continue command is used.

2. Stepping through the program

This feature allows the user to go through the program line by line to better understand the backend working of the code. By stepping through the program, the user can see the values in each register to better identify the problems in the code.

There are three ways to step through an interrupted program, and these three methods are defined below:

Method 1: Line-by-line stepping

s is used to step through a program line-by-line if the program has been interrupted by a breakpoint.

Method 2: Instruction-by-instruction stepping

si is used to step through a program instruction-by-instruction. The si command goes to the next compiled instruction when it is run.

Method 3: Function stepping

The n command is used for function stepping. Each function is a source line; the n command will take the user to the next source line once executed.

3. Inspecting variables

The user can inspect the registers in the code for ambiguous values or errors through the breakpoints. To print the values at any point during the compilations. There are two main ways to inspect the registers. Both the methods and their corresponding commands are explained below:

Method 1: Print variable values

Using print statements, the user can print the value of certain variables to debug the program more efficiently. The following command is used for this.

print variable_name
Example

In the main.c file to get the value b; after breaking at line 7, we can just write the command print b. This will output the value 1.

Method 2: Print register values

To see the value in a register, use the print $register_name command. To see the values in all the registers, use the following command.

info registers

Summary of Main Features and their Methods

Features

Methods

Example Commands

Setting breakpoints

Line break

break filename.c:line_number


Function break

break filename.c:function_name

Instruction break

break *address

Stepping through the program

Line-by-line

s


Instruction-by-instruction

si

Function stepping

n

Inspecting values

Print variable

print variable_name


Print register

print $register_name

Print all registers

info registers

If you're interested in learning how to run GDB and view a stack trace with it, check out our detailed lesson on the “GNU Project Debugger (GDB).”

This Answer provides a thorough overview of GDB (GNU Project Debugger), highlighting its key features and methods for effective debugging. By mastering GDB, users can enhance their debugging skills and improve their programming efficiency.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we start a process in GDB?

To initiate a process in GDB, follow these steps:

gcc -g -o codename codename.c  #Compile code
gdb ./codename #Start GDB with the code
(gdb) run  #Execute the code file within GDB

What is gcc and GDB?

gcc (GNU Compiler Collection) is a compiler for C, C++, and other languages. GDB (GNU Debugger) is a tool for debugging programs compiled with gcc and other compilers.


Is GDB a compiler?

No, GDB is not a compiler. It is a debugger used to analyze and debug programs compiled by compilers like gcc.


Can GDB run Python?

Yes, GDB can run Python scripts and supports using Python for custom commands and extensions.


Does VS Code use GDB?

Yes, VS Code can use GDB for debugging C and C++ programs.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved