A Basic Introduction on GDB Tutorial with Example

This tutorial will help you to learn GDB (GNU debugger) and run some useful gdb commands to debug the C factorial program.

What is GDB

GDB is referred to as the GNU debugger. it is a powerful tool for debugging C and C++ programs. You can easily run the GDB tool on Unix or Linux based systems and debug the executable binary which is created after the compilation of the programs.

The basic feature of GDB (GNU debugger)

  • You can run the program step by step.
  • Set the breakpoint where the execution of the program will stop.
  • Examine the variable`s value at any point in time.
  • Backtrace the stack frame.

Installation of GDB

On Linux based system, you have to run the below command on your terminal.

# Install gdb command
$ sudo apt-get install gdb
# Check the gdb version $ gdb --version

installation gdb on ubuntu

Now we are ready to start debugging our C program with gdb.

Step-1: Create the C program

We have an example C program which calculates the factorial of a given number.

#Calculate the Factorial of given number
#include<stdio.h>
int main(){

    int i,num,result;
    printf("enter the number: ");
    scanf("%d",&num);

    for(i=1;i<=num;i++){
        result= i*result;
    }

    printf("factorial of the number %d =%d\n",num,result);

    return 0;
}
Now compile and run the program with below command.
$ gcc factorial.c -o factorial
# This command create the factorial executable binary.
$./factorial

compiled and run program

As you see the output of the program is not correct. so let's start to debug the program with GDB.

 

Step-2: Compile the C program with the -g option and start GDB 

We have to compile the program with the -g option to enable the debug symbol.

$ gcc factorial.c -o factorial -g
# It create the factorial binary with debug symbol

$ gdb factorial
# gdb will start and show "Reading symbols from factorial...done.".it means your binary contains debug symbol. 

compile program with debug symbol

 

Step-3:Display and Set the Breakpoint

We run the list and break gdb command on the terminal.

# list or l  -->  Used to display the program.
(gdb) list

# break or b <line_number or function_name >  --> Used to set breakpoint
# This command set breakpoint at line number 10
(gdb) break 10

setup breakpoint

Step-4: Start the program execution in GDB

After setting up the breakpoints, the very first time you need to run the program with the below command.

(gdb) run
# Now Program getting started and asking for the input
# after getting input program halt at the line no 10

run program through gdb

 

Step-5: Examine the value of variables

Now you can print the value of the variable

print or p <variable_name> --> Show the value of the variable

(gdb) print i
# This will print the value of i

(gdb) print num
# This will print the value of num

(gdb) print result
# This will print the value of the result

examine variable

As you see the value of the result variable is showing some garbage value because it is uninitialized so getting a wrong output of factorial of 3.

To get correct output initialized the variable result =1

Add comment


Security code
Refresh