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
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.
The following are a few main features of GDB explained in detail.
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.
To break the program at a specific part of the code, there are three ways:
Breaking at a specific line is one way to break the code. To perform that operation, the syntax is
break filename.c:line_number
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
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
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
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.
Suppose line 7 has an address of 0x1ffa. The command for breaking the program at this instruction will be something like this:
break *0x1ffa
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.
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:
s
is used to step through a program line-by-line if the program has been interrupted by a breakpoint.
si
is used to step through a program instruction-by-instruction. The si
command goes to the next compiled instruction when it is run.
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.
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:
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
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
.
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
Features | Methods | Example Commands |
Setting breakpoints | Line break |
|
Function break |
| |
Instruction break |
| |
Stepping through the program | Line-by-line |
|
Instruction-by-instruction |
| |
Function stepping |
| |
Inspecting values | Print variable |
|
Print register |
| |
Print all 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.
Haven’t found what you were looking for? Contact Us
Free Resources