Bash

How to generate a core dump in Linux on a segmentation fault

19 September 2026 · 10 min read

How to generate a core dump in Linux on a segmentation fault

Segmentation faults, often dreaded by developers, are errors that occur when a program tries to access a memory location it’s not allowed to. These faults can be frustrating, but they are invaluable for debugging and understanding program behavior. One of the most powerful tools for analyzing segmentation faults on Linux systems is the core dump. A core dump is essentially a snapshot of the program’s memory at the time of the crash, containing critical information like variable values, stack traces, and register contents. Knowing how to generate a core dump in Linux on a segmentation fault allows you to dissect the program’s state and pinpoint the exact cause of the error. This guide will walk you through the process, explaining the necessary configurations and tools to effectively use core dumps for debugging.

Understanding Core Dumps and Segmentation Faults

A segmentation fault, often abbreviated as “segfault,” arises when a program attempts to access memory that it doesn’t have permission to access or tries to access memory in an invalid way (e.g., writing to read-only memory). This is a common issue in languages like C and C++ where manual memory management is prevalent. Understanding the underlying causes of segfaults is crucial for effective debugging. According to a study by Coverity, memory corruption errors, which can lead to segmentation faults, are a significant source of defects in software projects Synopsys Open Source Security and Risk Analysis (OSSRA) report. Core dumps provide the forensic evidence needed to investigate these memory-related issues.

A core dump, on the other hand, is a file that contains a snapshot of a program’s memory, register values, and program counter at the time of its termination due to a signal, such as SIGSEGV (the signal for segmentation fault). Analyzing a core dump allows developers to inspect the program’s state right before the crash, making it possible to identify the sequence of events that led to the error. This process typically involves using debugging tools like GDB (GNU Debugger) to load the core dump and examine the stack trace, variable values, and other relevant information. Without core dumps, debugging segfaults becomes significantly more difficult, often relying on guesswork or extensive logging. Therefore, properly configuring your system to generate core dumps is an essential practice for any Linux developer.

Core dumps are not enabled by default on many Linux systems for a few reasons, including security concerns (they can contain sensitive data) and disk space considerations (they can be quite large). However, for development and debugging purposes, enabling them is highly recommended. You can configure the system to generate core dumps globally or on a per-process basis. The next sections will detail the specific steps on how to configure your system to ensure core dumps are generated when a segmentation fault occurs, equipping you with the necessary tools to diagnose and resolve these errors efficiently.

Configuring Core Dump Generation

To effectively use core dumps for debugging, you must first ensure that your Linux system is properly configured to generate them when a segmentation fault occurs. Several factors influence whether a core dump is generated, including ulimit settings, kernel parameters, and the presence of signal handlers. Incorrect configuration can prevent core dumps from being created, hindering your debugging efforts. This is a featured snippet-optimized paragraph: To enable core dump generation, you typically need to adjust the ulimit -c setting, which controls the maximum size of core files. Setting it to unlimited allows core files of any size to be created. You might also need to check /proc/sys/kernel/core_pattern to determine where core files are being saved and how they are named.

The ulimit command is a shell built-in that allows you to control the resources available to processes. The -c option specifically controls the maximum size of core files. To enable core dump generation, you need to set the ulimit -c value to a non-zero value, typically unlimited. You can do this by running the command ulimit -c unlimited in your shell. This setting will only apply to the current shell session and any processes started from that shell. To make the setting permanent, you can add the command to your shell’s configuration file (e.g., .bashrc or .zshrc).

Another important factor is the /proc/sys/kernel/core_pattern file. This file specifies the location where core dumps are saved and how they are named. The default value might vary depending on your Linux distribution. You can view the current value by running cat /proc/sys/kernel/core_pattern. A common value is core, which means core dumps will be saved in the current working directory with the name core. You can modify this value to save core dumps in a different location or to include additional information in the filename, such as the process ID or the timestamp. For example, setting it to /tmp/core.%e.%p.%t will save core dumps in the /tmp directory with the name core.executable_name.process_id.timestamp. Changes to /proc/sys/kernel/core_pattern typically require root privileges and can be made using the sysctl command.

Here are the key points to remember for configuring core dump generation:

  • Set ulimit -c unlimited to allow core files of any size.
  • Check /proc/sys/kernel/core_pattern to determine where core dumps are saved.
  • Modify /proc/sys/kernel/core_pattern to customize the core dump filename and location.

Generating a Segmentation Fault

Now that you’ve configured your system to generate core dumps, you need a program that produces a segmentation fault to test the configuration. Creating a simple program that intentionally triggers a segfault is a straightforward way to verify that core dumps are being generated correctly. The following example illustrates how to create a segmentation fault in C. Understanding how to reproduce segfaults can also help in isolating the cause of more complex issues in larger applications. This is a crucial step in confirming that your system is ready to capture the core dump for analysis.

Here’s a simple C program that will cause a segmentation fault:

include <stdio.h> int main() { int ptr = NULL; ptr = 10; // This will cause a segmentation fault return 0; } 

This program attempts to dereference a null pointer, which is a common cause of segmentation faults. To compile and run this program, save it as segfault.c and then execute the following commands in your terminal:

gcc segfault.c -o segfault ./segfault 

If your system is correctly configured, running this program should result in a segmentation fault and the generation of a core dump file. The location of the core dump file will depend on the /proc/sys/kernel/core_pattern setting. After running the program, check for the core dump file in the expected location. If no core dump file is generated, double-check your ulimit -c setting and /proc/sys/kernel/core_pattern configuration. Also, verify that the user running the program has write permissions to the directory where the core dump is supposed to be saved.

Analyzing Core Dumps with GDB

Once you have generated a core dump, the next step is to analyze it using a debugger. The GNU Debugger (GDB) is a powerful tool for analyzing core dumps and inspecting the state of a program at the time of its crash. GDB allows you to examine the call stack, variable values, and register contents, providing valuable insights into the cause of the segmentation fault. Mastering GDB is essential for effective core dump analysis. According to the GDB documentation, it provides extensive features for debugging programs written in various languages GNU Debugger Documentation.

To analyze a core dump with GDB, you need to start GDB with the executable file and the core dump file as arguments. For example, if your executable is named segfault and your core dump file is named core, you would run the following command:

gdb segfault core 

Once GDB is running, you can use various commands to inspect the core dump. Some of the most useful commands include:

  • bt (backtrace): Prints the call stack, showing the sequence of function calls that led to the crash.
  • frame : Selects a specific frame in the call stack.
  • info locals: Prints the values of local variables in the current frame.
  • print : Prints the value of a specific variable.
  • list: Displays the source code around the current line of execution.

By examining the call stack and variable values, you can often pinpoint the exact location in the code where the segmentation fault occurred and identify the cause of the error. For example, if the backtrace shows that the crash occurred while dereferencing a null pointer, you can examine the variable values in the surrounding code to determine why the pointer was null. You can also use the info registers command to see the values of the CPU registers at the time of the crash, which can provide additional clues about the program’s state. The process of analyzing core dumps often involves iterative exploration, moving up and down the call stack and examining variable values to gain a comprehensive understanding of the program’s behavior.

For example, consider the segfault.c program from before. After running ‘gdb segfault core’ and typing ‘bt’ (backtrace), you would see that the issue originates at line 5 ptr = 10;. This immediately tells you that something is wrong with ptr. Then, inspecting its value with ‘print ptr’ would confirm that it is NULL, explaining the segmentation fault.

Best Practices and Advanced Techniques

Beyond the basic steps of generating and analyzing core dumps, there are several best practices and advanced techniques that can significantly improve your debugging workflow. These include using debug symbols, automating core dump analysis, and handling core dumps in production environments. Implementing these practices can help you diagnose and resolve segmentation faults more efficiently and effectively. Debug symbols are crucial for accurate and informative debugging sessions Debugging Core Dumps in Red Hat Enterprise Linux.

Debug symbols provide additional information about the program, such as function names, variable names, and line numbers. Without debug symbols, GDB can still analyze the core dump, but the information will be less informative. For example, instead of seeing the name of a function in the backtrace, you might only see its memory address. To include debug symbols in your executable, you need to compile your code with the -g flag. For example:

gcc -g segfault.c -o segfault 

When you analyze a core dump generated from an executable compiled with debug symbols, GDB will be able to provide much more detailed information about the program’s state. This can significantly speed up the debugging process. Here are steps for generating a core dump:

  1. Ensure core dumps are enabled using ulimit -c unlimited.
  2. Compile your program with debug symbols using gcc -g your_program.c -o your_program.
  3. Run your program and trigger the segmentation fault.
  4. Analyze the core dump using gdb your_program core.
Infographic here explaining the core dump generation process
FAQ ---
Why are core dumps not generated by default?
Core dumps can contain sensitive information and consume significant disk space, so they are often disabled by default for security and resource management reasons.
How can I change the location where core dumps are saved?
You can change the location by modifying the /proc/sys/kernel/core\_pattern file. Use the sysctl command to make persistent changes.
What if I don't have GDB installed?
You can install GDB using your distribution's package manager (e.g., apt-get install gdb on Debian/Ubuntu, yum install gdb on CentOS/RHEL).
Can core dumps be used for security analysis?
Yes, core dumps can be valuable for security analysis, as they can reveal sensitive information or vulnerabilities in a program. However, they should be handled with care to prevent unauthorized access.
By understanding and implementing these techniques, you'll be well-equipped to tackle segmentation faults and other memory-related errors in your Linux applications. Remember to always compile with debug symbols, automate your analysis where possible, and handle core dumps securely.

Debugging segmentation faults using core dumps is a fundamental skill for any Linux developer. By correctly configuring your system, generating core Question & Answer :

I have a process in Linux that’s getting a segmentation fault. How can I tell it to generate a core dump when it fails?

This depends on what shell you are using. If you are using bash, then the ulimit command controls several settings relating to program execution, such as whether you should dump core. If you type

ulimit -c unlimited 

then that will tell bash that its programs can dump cores of any size. You can specify a size such as 52M instead of unlimited if you want, but in practice this shouldn’t be necessary since the size of core files will probably never be an issue for you.

In tcsh, you’d type

limit coredumpsize unlimited