Debug Applications with gdb Command In Linux – POFTUT

Debug Applications with gdb Command In Linux


gdb is the short form of GNU Debugger. A debugger is a tool used to search and find and get detailed information about bugs in application binaries. gdb is popular in the Linux community which is used by most of the IDE, Programming tools event in Android IDE’s. In this tutorial, we will look at how to start and use the basic features of gdb .

Example Code

During this tutorial, we will use the following simple application code which is written in C Programming language. This code just calculates factorial.

#include <stdio.h> 

int main(void) { 
 int fact = 1; 
 int number=5; 

 for (int j = 1; j <= number; j++) { 
   fact = fact * j; 
   printf("%d\n",fact); 
 } 

}

Install gdb For Ubuntu, Debian, Mint, Kali

We can install gdb for Ubuntu, Debian, Mint and Kali with the following lines.

$ sudo apt install gdb

Install gdb For CentOS, RHEL, Fedora

Installation for gdb in RPM based distros like CentOS, RHEL and Fedora

$ sudo yum install gdb

Compile Application

We will start by compiling our example application. We will use GCC or GNU Compiler Collection which is a defacto compiler for the Linux environment. gcc can be installed with the following line if not installed.

$ sudo apt install gcc

Now we will compile our application just providing the source code file name which is poftut.c in this case.

$ gcc poftut.c -o poftut

Compile Application with Debug Info

We can debug all ready compiled applications but there is some useful option that can be used to provide more information about the application while debugging. We can enable debugging information which can be used to match debugging code with source code and provides more detailed information. We can use -g option while compiling with gcc like below.

$ gcc -g poftut.c -o poftut
Compile Application with Debug Info
Compile Application with Debug Info

Setup Debugging By Running Executable

We can start applications for debugging with different methods but the most basic and practical way is starting with the application name. We will start poftut app like below.

$ gdb poftut
Start with Executable Name
Start with Executable Name

We can see gdb interactive shell is started after loading app and related debugging information.

LEARN MORE  Introduction To Java Programming Language

Start Debugging

We can start debugging the given application by using run  command. But keep in mind that this will start process run which will end in this case without a problem. While running the standard output will be put to the screen. and the last thing is exit status  which is normal in this case.

run
Start Debugging
Start Debugging

Inspect Crashes

One of the use case for gbd is inspecting crashes to find the cause. The application can be exited by crashing and in this case crash information is get by gdb . We can list crash information with backtrace  command like below.

backtrace
Inspect Crashes
Inspect Crashes

Breakpoints

In big applications, we generally need to inspect the flow of the application. While inspecting we need to look specific situation which is important for us. We can set breakpoints that will stop the execution of the application. We can check the current register and memory values easily. In this example, we set a break for the line printf.

break printf

and we run

run
Breakpoints
Breakpoints

List Breakpoints

We can list existing breakpoints and their hit count with the info break command like below.

info break
List Breakpoints
List Breakpoints

List Register Values

We can list current register values with info registers command like below. Keep in mind that register values are very volatile and changes in each step in general.

info registers
List Register Values
List Register Values

Stepping

During debugging we generally want to go one step further which will change current memory and register values. We can use step  command for this.

step
Stepping
Stepping

We can also spefiy the step count which is 3 in this case.

step 3
step 3
step 3

Leave a Comment