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

 $ 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 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/, for GNU/Linux 2.6.32, BuildID[sha1]=a809bdb50d85d34feba9ccad8677ec15ee7d3882, not stripped

$ gdb ./understand_gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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 “i686-linux-gnu”.
Type “show configuration” for configuration details.
For bug reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type “help”.
Type “apropos word” to search for commands related to “word”…
Reading symbols from ./understand_gdb…done.

 (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.

Related :   c program for creating a text file from string in buffer

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 
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question