Tuesday, August 24, 2010

Why Does The Segmentation Fault Occur on Linux / UNIX Systems?

SkyHi @ Tuesday, August 24, 2010
According to wikipedia:

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

Usually signal #11 (SIGSEGV) set, which is defined in the header file signal.h file. The default action for a program upon receiving SIGSEGV is abnormal termination. This action will end the process, but may generate a core file (also known as core dump) to aid debugging, or perform some other platform-dependent action. A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally.

Segmentation fault can also occur under following circumstances:

a) A buggy program / command, which can be only fixed by applying patch.

b) It can also appear when you try to access an array beyond the end of an array under C programming.

c) Inside a chrooted jail this can occur when critical shared libs, config file or /dev/ entry missing.

d) Sometime hardware or faulty memory or driver can also create problem.

e) Maintain suggested environment for all computer equipment (overheating can also generate this problem).

Suggestions to debug Segmentation Fault errors


To debug this kind of error try one or all of the following techniques :

  • Use gdb to track exact source of problem.
  • Make sure correct hardware installed and configured.
  • Always apply all patches and use updated system.
  • Make sure all dependencies installed inside jail.
  • Turn on core dumping for supported services such as Apache.
  • Use strace which is a useful diagnostic, instructional, and debugging tool.
  • Google and find out if there is a solution to problem.
  • Fix your C program for logical errors such as pointer, null pointer, arrays and so on.
  • Analyze core dump file generated by your system using gdb

Further readings: