How to use gdb for debugging application programs ( On Ubuntu Linux using C program )

In this post, we will create a simple C program, compile it with debugging enabled and then debug it using GDB debugger to identify where exactly the problem is occurring while the program is executing.

$ vim understand_gdb.c 
#include <stdio.h>

int main(int argc, char **argv) {
        char *str = "Hello World";
        int *p = NULL;
        printf("%s: understanding gdb\n", str);
        *p = 10;
        printf("%d: understanding gdb\n", *p);
        return 0;
}

To prepare our program for debugging with gdb, we must compile it with the -g flag.

 $  gcc -g -o understand_gdb understand_gdb.c 
$ file understand_gdb

understand_gdb: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=38757c93699e4a52fb5ebfb66b8b141503d78e98, for GNU/Linux 3.2.0, with debug_info, not stripped

Now, we will start the gdb to debug this C program as,

$ gdb ./understand_gdb

Reading symbols from ./understand_gdb...done.

After this you will get the gdb console as see below, where you can type command help to check which are other commands we can use with gdb..

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

set a break point at line number 1 as,

(gdb) break 1
Breakpoint 1 at 0x804842c: file understand_gdb.c, line 1.

Now, we need to run the program, so as it starts the execution, and since we have put the breakpoint at line 1, we expect it to stop at line 1 as,

 (gdb) run
Starting program: understand_gdb 

Breakpoint 1, main (argc=1, argv=0xbfffefc4) at understand_gdb.c:4
4		char *str = "Hello World";

As we can see, the first line of the program execution is “char *str = “Hello World”” which is actually 4th line in the source code, hence it has printed number 4 in source code line.

Now, type “next” to continue program execution 1 by 1 line as,

 (gdb) next
5		int *p = NULL;

We can also type the shortform of “next” with “n” as,

 (gdb) n
6		printf("%s: understanding gdb\n", str);
(gdb) n
Hello World: understanding gdb
7		*p = 10;
 (gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x08048450 in main (argc=1, argv=0xbfffefc4) at understand_gdb.c:7
7		*p = 10;

As, we can see the program received segmentation fault interrupt at line number 7, where we are trying to assign integer value to unallocated or Null pointer memory.

(gdb) n

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) n
The program is not being run.
 (gdb) quit 

Leave a Comment