Debug Applications with gdb In Linux

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/debug-applications-with-gdb-in-linux/
PINTEREST
PINTEREST
INSTAGRAM

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

Example Code

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

Install gdb For Ubuntu, Debian, Mint, Kali

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

Install gdb For CentOS, RHEL, Fedora

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

Compile Application

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

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

LEARN MORE  How To Install and Use Cygwin With Terminal and Ssh Examples

Compile Application with Debug Info

We can debug all ready compiled applications but there is some useful option which 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.

Compile Application with Debug Info

Compile Application with Debug Info

Start with Executable Name

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

Start with Executable Name

Start with Executable Name

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

Start Debugging

We can start debugging 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.

Start Debugging

Start Debugging

Inspect Crashes

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

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 which will stop the execution of the application. We can check current register and memory values easily. In this example we set a break for the line printf.

and we run

LEARN MORE  Php Tutorial

Breakpoints

Breakpoints

List Breakpoints

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

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.

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.

Stepping

Stepping

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

step 3

step 3

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)