DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • OpenCV Integration With Live 360 Video for Robotics
  • Why We Use FreeBSD Over Linux: A CTO’s Perspective

Trending

  • Contextual AI Integration for Agile Product Teams
  • How to Format Articles for DZone
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • How Clojure Shapes Teams and Products
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Debugging Core Dump Files on Linux - A Detailed Guide

Debugging Core Dump Files on Linux - A Detailed Guide

This guide explains how to enable, generate, and debug core dumps in Linux using tools like GDB, Valgrind, and Crash Utility to analyze core dumps

By 
Srinivas Chippagiri user avatar
Srinivas Chippagiri
DZone Core CORE ·
Apr. 29, 25 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

Core dumps play a key roll role in debugging programs that exit abnormally. They preserve a state of a program at failure, and with them, programmers can view and identify causes of failures. In this article, a walkthrough is taken through a step-by-step exercise of enabling, creating, and checking out core dumps in Linux and touches on high-end tools and techniques for debugging sophisticated failures, and enables quick diagnoses and resolution.

1. Enabling Core Dumps in Linux

Check and Set ulimit

  1. Check for current value for core dumps:

    Shell
     
    ulimit -c


    Output:

    Shell
     
    0


    A value of 0 informs us that core dumps have been disabled..

  2. Enable core dumps temporarily:

    Shell
     
    ulimit -c unlimited


    Check again:

    Shell
     
    ulimit -c


    Output:

    Shell
     
    unlimited


  3. To make it persistent, add the following in /etc/security/limits.conf:

    Shell
     
    *    soft    core    unlimited
    *    hard    core    unlimited


  4. Configure location for core dumps:

    Shell
     
    sudo sysctl -w kernel.core_pattern=/var/dumps/core.%e.%p


    • %e: Program name

    • %p: Process ID

  5. Make it persistent: Add the following in  /etc/sysctl.conf:

    Shell
     
    kernel.core_pattern=/var/dumps/core.%e.%p


    Reload configuration:

    Shell
     
    sudo sysctl -p


2. Generate Core Dumps for Testing

C Program to cause Segfault

Create a testing program:

Shell
 
#include <stdio.h>
int main() {
    int *ptr = NULL;  // Null pointer
    *ptr = 1;         // Segmentation fault
    return 0;
}


Make a build of it:

Shell
 
gcc -g -o crash_test crash_test.c


Execute the program:

Shell
 
./crash_test


Output:

Shell
 
Segmentation fault (core dumped)


Check for location of the core dump:

Shell
 
ls /var/dumps


Example:

Shell
 
core.crash_test.12345


3. Analyze with GDB

Load the Core Dump

Shell
 
gdb ./crash_test /var/dumps/core.crash_test.12345


Basic Analysis

Get Backtrace

Shell
 
bt


Output:

Shell
 
#0  0x0000000000401132 in main () at crash_test.c:4
4       *ptr = 1;


The backtrace identifies that the crash happened at line 4.

Examine Variables

Shell
 
info locals


Output:

Shell
 
ptr = (int *) 0x0


The variable ptr is a null pointer, and a segmentation fault is confirmed.

Disassemble the Code

Shell
 
disassemble main


Output:

Shell
 
Dump of assembler code for function main:
   0x000000000040112a <+0>:  mov    %rsp,%rbp
   0x000000000040112d <+3>:  movl   $0x0,-0x4(%rbp)
   0x0000000000401134 <+10>: movl   $0x1,(%rax)

Displays actual assembly instruction at location of crash.


Check Registers

Shell
 
info registers


Output:

Shell
 
rax            0x0      0
rbx            0x0      0
rcx            0x0      0


Register rax is 0, showing the null pointer dereference.

4. Debugging Multithreaded Applications

Check Threads

Shell
 
info threads


Output:

Shell
 
  Id   Target Id         Frame
* 1    Thread 0x7f64c56 (LWP 12345) "crash_test" main () at crash_test.c:4


Switch to a Specific Thread

Shell
 
thread 1


Get Backtrace for All Threads

Shell
 
thread apply all bt


5. Using Advanced Tools

Valgrind – Analysis of Memory Issue

Shell
 
valgrind --tool=memcheck ./crash_test


Output:

Shell
 
Invalid write of size 4
   at 0x401132: main (crash_test.c:4)
 Address 0x0 is not stack'd, malloc'd or (recently) free'd

Confirms an invalid access in memory.


ELFutils for Symbol Inspection

Shell
 
eu-readelf -a /var/dumps/core.crash_test.12345


Output:

Shell
 
Program Headers:
  LOAD 0x000000 0x004000 0x004000 0x1234 bytes


Displays sections and symbol information in the core file.

Crash Utility for Kernel Dumps

Shell
 
sudo crash /usr/lib/debug/vmlinux /var/crash/core

Use for kernel-space core dumps.


Generate Core Dumps for Running Processes

Shell
 
gcore <PID>


6. Debugging Specific Issues

Segmentation Faults

Shell
 
info frame
x/16xw $sp

Check near stack pointer in memory.

Heap Corruption

Check for heap corruption with Valgrind or AddressSanitizer:

Shell
 
gcc -fsanitize=address -g -o crash_test crash_test.c
./crash_test


Shared Libraries Mismatch

Shell
 
info shared
ldd ./crash_test

Check shared libraries loaded in a proper manner.


7. Best Practices for Core Dump Debugging

  • Save Symbols Independently: Deploy stripped binaries for production and store debug symbols in a secure manner.
  • Automate Dump: Employ systemd-coredump for efficient dump management.
  • Analyze Logs: Enable full application logs for tracking run-time faults.
  • Redact Sensitive Information: Remove sensitive information in shared core dumps.
  • Test in Debugging: Employ debug build with full symbols for in-depth debugging.

8. Conclusion

Debugging a core dump in Linux is a logical progression and utilizes tools such as GDB, Valgrind, and Crash Utility. By careful examination of backtraces, state of memory, and register values, developers can pinpoint root causes and remedy them in no time. Best practices will yield more efficient diagnostics and rapid resolution for production-critical environments.

Linux (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • OpenCV Integration With Live 360 Video for Robotics
  • Why We Use FreeBSD Over Linux: A CTO’s Perspective

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: