How to debug a program using GNU Debugger

GNU Debugger or GDB

GNU Debugger, also known as gdb, allows us to mask through the code while it executes or what a program was trying to do at the moment before it crashed. In this article, we will learn about one Source Code Debugging tool for Linux Programs, GNU Debugger or GDB.

Four things that GDB help us to do to catch flaws in the source code.

  • Change the code and to experiment with the modified code instantaneously.
  • Examine the crash or when program was stopped.
  • Start the program, specifying arguments that may affect the general behavior.
  • Stop the program on specified conditions


With GDB we can debug programs in C and C++ and it provides partial support for programs in D, Modula-2, Fortran etc.,

[root@linuxhelp ~]#gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type " show copying" 
and " show warranty"  for details.
This GDB was configured as " x86_64-redhat-linux-gnu" .

For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/

List of GDB Commands

Command Description
run (r) To run a program
quit (q) To exit form gdb prompt
list (l) To list the lines of codes
break (b) To set break points
next (n) To go to next line
info breakpoint To view information of available breakpoints

Sample Program

Here we have used multiplication.c to debug using gdb. GDB operates only on executable file (binary file) produced by compilation process.

#include  
int mul (int x, int y) { 
    int z  
    z = x * y  
    return z  
} 
int main() { 
    int j, k, q  
    printf(" 
Please enter the first number: " )  
    scanf(" %d" , & j)  
    printf(" Please enter the second number: " )  
    scanf(" %d" , & k)  
    q = mul (j, k)  
    printf(" The Multiplication is %d" , q)  
    return 0  
}

To compile a file

gcc is used for compiling a program before its going to debug. If gcc is not installed we can install it by typing yum or apt-get command based on your distributions. After compiling by using gcc command a new executable file will be created under your present directory. We can use this file only into gdb prompt for debugging.

[root@linuxhelp Desktop]# ls
multiply.c  test
[root@linuxhelp Desktop]# gcc -g multiply.c -o multiply
[root@linuxhelp Desktop]# ls
multiply  multiply.c  test

To debug a file in gdb

The output file (source code) compiled form gcc can be attached to gdb by any of the following two ways

By specifying the file as an argument to gdb command

[root@linuxhelp Desktop]# gdb multiply
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type " show copying" 
and " show warranty"  for details.
This GDB was configured as " x86_64-redhat-linux-gnu" .
For bug reporting instructions, please see:
< http://www.gnu.org/software/gdb/bugs/> ...
Reading symbols from /root/Desktop/multiply...done.
(gdb)

By opening the output file inside the gdb prompt using file command

[root@linuxhelp Desktop]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type " show copying" 
and " show warranty"  for details.
This GDB was configured as " x86_64-redhat-linux-gnu" .
For bug reporting instructions, please see:
< http://www.gnu.org/software/gdb/bugs/> .
(gdb) file multiply
Reading symbols from /root/Desktop/multiply...done.
(gdb)

To run a program inside gdb

After attaching the output file inside the gdb prompt we simply run the program using the run (r) command to see the output. This command will simply run the program end exits.

(gdb) run
Starting program: /root/Desktop/multiply 

Please enter the first value: 2
Please enter the second value: 3
Multiplication of the given values is 6

Program exited normally.
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64

To list the codes inside the gdb prompt

By using list (l) command we can able to view the source code that attached to the gdb prompt. By typing list command we can able to view upto 10 lines only, if we need to view more lines we can type list command again and again until the line ends.

(gdb) list
2    
3    int mul (int x, int y) { 
4        int z  
5        z = x * y  
6        return z  
7    } 
8    
9    int main() { 
10        int j, k, q  
11        printf(" 
Please enter the first number: " )  
(gdb) list
12        scanf(" %d" , & j)  
13        printf(" Please enter the second number: " )  
14        scanf(" %d" , & k)  
15        q = mul (j, k)  
16        printf(" The Multiplication is %d

" , q)  
17        return 0  
18    }
(gdb) list
Line number 19 out of range  multiply.c has 18 lines.

To set a break point inside a program

While debugging a program or source code we need to set a break point inside the code. Break point tells gdb to pause the program in a particular functions or source code. By setting break point we can able to stop the program in specified line, and from that line we can able to execute the program by line by line specifying the next command. Next command used to execute the current line and moves for the next line. Since we can able to set break point to main function or any function or any specified line by specifying the following command.

(gdb) break main
Breakpoint 1 at 0x400555: file multiply.c, line 11.
(gdb) run
Starting program: /root/Desktop/multiply 

Breakpoint 1, main () at multiply.c:11
11    printf(" 
Please enter the first value: " ) 
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64
(gdb) next

12    scanf(" %d" , & j) 
(gdb) next
Please enter the first value: 2
13    printf(" Please enter the second value: " ) 
(gdb) next
14    scanf(" %d" , & k) 
(gdb) next
Please enter the second value: 3
15    q=mul(j,k) 
(gdb) next
16    printf(" Multiplication of the given values is %d

" , q) 
(gdb) next
Multiplication of the given values is 6

17    return 0 
(gdb) next
18    }

To set a break point in a specified line or function

We can able to set a break point by specifying the line number or by specifying the function name as follows. Here I am going to set a break point for line number 14 and multiplication function respectively.

(gdb) break multiply.c:14
Breakpoint 2 at 0x4005ab: file multiply.c, line 14.
(gdb) break mul
Breakpoint 3 at 0x40053e: file multiply.c, line 4.

To Modify variables

We can able to modify the output variable by set the output value manually by using set command. While we are executing the source code by line by line we enter pass some values to the code to be saved in the variables. We can specify a command to view those stores values in variables by the following command. We can able to re-run the program by again typing run command. Continue command is used to continue the execution process to the next break point.

(gdb) info locals
j = 2
k = 3
q = 6
(gdb) set q=10
(gdb) continue
Continuing.
Multiplication of the given values is 10

Program exited normally.

To disable and delete break points

After debugging completed we need to disable or delete the breakpoints before executing the program. To view information about break points we can specify a command info breakpoints and we can able to disable or delete as follows.

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400555 in main at multiply.c:11
    breakpoint already hit 1 time
2       breakpoint     keep y   0x00000000004005ab in main at multiply.c:5
    breakpoint already hit 1 time
(gdb) disable 1
(gdb) disable 2
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x0000000000400555 in main at multiply.c:11
    breakpoint already hit 1 time
2       breakpoint     keep n   0x00000000004005ab in main at multiply.c:5
    breakpoint already hit 1 time
(gdb) delete 1
(gdb) delete 2
(gdb) info breakpoints
No breakpoints or watchpoints.
Tag : GNU Debugger
FAQ
Q
How to show the current instruction while single-stepping instructions in GNU Debugger?
A
Use the following command (gdb) display/i $pc for it.
Q
What is the essential difference between GDB and gcc?
A
Both GNU Debugger and gcc are almost the same in their features.
Q
How do I print an STL container for GNU Debugger?
A
You can look "https://sourceware.org/gdb/wiki/STLSupport" for more support.
Q
Is there any way to set commands to be run on a segfault for GDB?
A
Without GDB you can setup using Linux Kernel "/proc/sys/kernel/core_pattern"
Q
What Languages does GDB Support?
A
Following are the list of languages that GDB support



Ada

Assembly

C

C++

D

Fortran

Go

Objective-C

OpenCL

Modula-2

Pascal

Rust