How to Debug Segmentation Fault in C Program Using GDB?

Segmentation fault is a type of error that occurs when a program trying to access an invalid memory or protected memory. Kernel gets notifying this access violation and then kernel sends the default signal handler SIGSEG to terminate the program.

GDB is a debugging to debug the C, C++ program. so in this tutorial, we will use GDB to debug the segmentation fault with some GDB commands.

What is Segmentation Fault

It is a type of error that occurs due to the following conditions.

  • When you are trying to access a memory address that doesn't belong to you.
  • You are trying to access memory that is protected.
  • You already free the memory and trying to access it.
  • NULL pointer dereference.

Now We will look into this error and explain to you how to find the segmentation fault in the program.

This example C program has segmentation fault.

//This Program give Segmentation fault
#include<stdio.h>
#include<stdlib.h>

int main()
{
	int *ptr=NULL;

	*ptr=1;
	printf("value =%d",*ptr);
	return 0;
}
# This will generate an executable binary  
$ gcc test.c -o test

# Run the test binary with the below command and it will give a segmentation fault
$./test
Segmentation fault (core dumped)

segmentation fault

As you see the program giving the segmental fault. follow the below-debugging steps:

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

  • This flag (-g ) includes the debug symbols in the executable binary.
  • Start GDB tool with below command.
# Create the test binary with debug symbol
$ gcc test.c -o test -g
# Launch gdb with test program $ gdb test

compile program with debug symbol

Step-2: Run Program With GDB Command

Start program execution with the below command.

(gdb) run
# Program execute and you will get below info on the terminal

Starting program: /home/tutorial/c-test/test 

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400522 in main (argc=1, argv=0x7fffffffddc8) at test.c:8
8		*ptr=1;

run program through gdb

Step-3: Debug Program With GDB Command

As you see, we are getting SIGSEGV signal from the OS because we are trying to access invalid memory access.

backtrace or bt --> It shows the stack frames. stack frames contain information about how one function is called to another function.
frame  --> To switch the particular frame

(gdb) bt
#0  0x0000000000400522 in main (argc=1, argv=0x7fffffffddc8) at test.c:8

# It is showing a frame 0 and will check this frame 0 with the below command

(gdb) frame 0
#0  0x0000000000400522 in main (argc=1, argv=0x7fffffffddc8) at test.c:8
8		*ptr=1;

(gdb) print ptr
$1 = (int *) 0x0

setup breakpoints in gdb

frame 0 provides info that line 8 (*ptr=1) is causing the issue so we checked the ptr value (address of the pointer variable) and it is a NULL pointer(0x0).

In our program, We are trying to write a value at the NULL pointer. that's why getting the segmentation fault.

Add comment


Security code
Refresh