Skip to content

Example project for generating, analyzing, and managing core dumps for Linux C/C++ (gcc) programs.. 🇰🇷 Linux C/C++(gcc) 프로그램의 코어 덤프 생성, 분석, 관리 자동화 예제

License

Notifications You must be signed in to change notification settings

JayTwoLab/linux-crash-core-dump

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux Coredump

Korean README

  • Example project for generating, analyzing, and managing core dumps for Linux C/C++ (gcc) programs.


File Overview

  • main.cpp
    • A simple C++ example that intentionally causes a segmentation fault.

      int main() {
        std::string *ptr = NULL;
        ptr->clear(); // crash here
        return 0;
      }
    • Debug build example:

      g++ -g -O0 -Wall -Wextra -o hello main.cpp
    • Using AddressSanitizer / UBSanitizer:

      g++ -g -O0 -Wall -Wextra -fsanitize=address,undefined -fno-omit-frame-pointer -o hello main.cpp
    • AddressSanitizer (ASan): Detects memory errors at runtime

      • Detectable issues:
        • Heap/stack buffer overflow
        • Use-after-free
        • Double free
        • Memory leaks
        • Stack overflow
      • Characteristics:
        • Monitors memory access at runtime
        • Provides precise stack traces
        • Has performance overhead (~2–3x)
    • UndefinedBehaviorSanitizer (UBSan): Detects undefined behavior in C++

      • Detectable issues:
        • Integer overflow
        • Invalid casts
        • Null pointer dereference
        • Out-of-bounds access
        • Misaligned memory access
        • Invalid enum values
      • Characteristics:
        • Effective for detecting logic errors
        • Lighter than ASan
        • Can warn before crash


  • setup_core_dump_systemwide.sh
    • Configures the system to generate core dump files globally (requires sudo).
    sudo ./setup_core_dump_systemwide.sh
    • Core file name pattern: core.<exe>.<pid>.<time>
      • <exe> : executable name
      • <pid> : process ID
      • <time> : epoch time
        • Convert with:
          date -d @<time> '+%Y-%m-%d %H:%M:%S %Z'


  • run_hello_with_core.sh
    • Executes the hello program and generates a core dump.
    • Requires system-wide core dump configuration.
    • Set executable path before use:
    WORKDIR="/home/jaytwo/workspace/coredump-workspace"
    EXEC="${WORKDIR}/hello"


  • run_hello_with_core_daemon.sh
    • Runs the hello program repeatedly in daemon mode.
      • Automatically restarts on exit
      • Enables core dumps and ASAN/UBSAN
      • Log file is rotated when exceeding 10MB (keeps last 10,000 lines)
      MAX_LOG_SIZE=10485760 # 10MB
      MAX_LOG_LINES=10000
      if [ -f "${LOG}" ] && [ $(stat -c%s "${LOG}") -ge $MAX_LOG_SIZE ]; then
          tail -n $MAX_LOG_LINES "${LOG}" > "${LOG}.tmp" && mv "${LOG}.tmp" "${LOG}"
          echo "[$(date '+%F %T')] log trimmed to last $MAX_LOG_LINES lines" >> "${LOG}"
      fi
    • Configure executable path:
      WORKDIR="/home/jaytwo/workspace/coredump-workspace"
      EXEC="${WORKDIR}/hello"


  • gdb_hello_core.sh
    • Script to analyze core dump using gdb.
    ./gdb_hello_core.sh <core_dump_file>
    • Set executable path:
    WORKDIR="/home/jaytwo/workspace/coredump-workspace"
    EXEC="${WORKDIR}/hello"


  • list_core_with_time.sh
    • Lists core files in the current directory with human-readable timestamps.


Usage Example

  1. Configure core dump system

    sudo ./setup_core_dump_systemwide.sh
  2. Build executable with debug symbols

    g++ -g -O0 -Wall -Wextra -o hello main.cpp
  3. Generate core dump

    ./run_hello_with_core.sh
  4. List core dump files

    ./list_core_with_time.sh
  5. Analyze core dump

    ./gdb_hello_core.sh core.hello.<pid>.<time>
    • gdb commands:
      • r : run
      • bt full : full backtrace with local variables
      • list : show source lines
  6. Run as daemon

    ./run_hello_with_core_daemon.sh

Automatic Startup Setup

About

Example project for generating, analyzing, and managing core dumps for Linux C/C++ (gcc) programs.. 🇰🇷 Linux C/C++(gcc) 프로그램의 코어 덤프 생성, 분석, 관리 자동화 예제

Resources

License

Stars

Watchers

Forks

Packages

No packages published