GDB(GNU Project Debugger) is a debugger, which is a program that runs other programs to help us know actually what happens while the program is executed. GDB is one of the most commonly used debugger for UNIX systems. All you need to have is a basic idea of x86 architecture and assembly programming. You may head into my page on x86 assembly for this.
Basically, GDB helps us identify the following issues:
a) When the core is dumped, the GDB helps us identify which line caused the crash.
b) If an error occurs while executing a function, which line calls the statement and the parameters can be identified.
c) The values of the variables at a particular point of execution can be identified.
d) The result of a particular expression in the program can be identified.
I am using GDB-peda for this. To install GDB-peda in your Linux system, type the following commands in your terminal.
To install the libraries, follow the command:
sudo apt-get install libncurses5-dev
Now download the repository from github.
git clone https://github.com/longld/peda.git ~/peda echo “source ~/peda/peda.py” >> ~/.gdbinit
The second command is to get direct access to GDB-peda while opening the debugger.
Once we have our GDB-peda installed, we can start debugging our programs.
To get the debugging information while compiling, add the GDB flag ( -g) to it.
gcc -g -o filename filename.c
Now to start GDB, simply type:
To get the entire list of functions, type
Now we will go through some of the basic commands to use GDB-peda
pd <function> : to print the disasembly of the specified function
b *(function/address) : to set a breakpoint at the specified function or address
r : to run the program till the breakpoint or end
ni : single step without descending into any functions i.e., if encounters a function executes the entire function in a single step
si : single step to descend into the functions
help (h) : to get help on gdb commands
c (continue) : continue till the next breakpoint or end
delete : to delete all breakpoints or the specified one
print <$register/address/variable> : to print the value at the specified address or variable
.x/s <$register/address> : to print the string at the given address [s in the above command can be replaced with x (hex), wx (integer) as preferred]
You can also use the GDB commands for the same. Some of the GDB commands are:
disas main – to get the assembly code for the main function
set disassembly-flavor intel – to convert disassembly to more user friendly format
b main – Puts a breakpoint at the beginning of the program
b – Puts a breakpoint at the current line
b N – Puts a breakpoint at line N
b +N – Puts a breakpoint N lines down from the current line
b fn – Puts a breakpoint at the beginning of function “fn”
d N – Deletes breakpoint number N
info break – list breakpoints
r – Runs the program until a breakpoint or error
c – Continues running the program until the next breakpoint or error
f – Runs until the current function is finished
s – Runs the next line of the program
s N – Runs the next N lines of the program
n – Like s, but it does not step into functions
u N – Runs until you get N lines in front of the current line
p var – Prints the current value of the variable “var”
bt – Prints a stack trace
u – Goes up a level in the stack
d – Goes down a level in the stack
q – Quits gdb
Now you can interpret your programs using the GDB and start debugging.