- Example project for generating, analyzing, and managing core dumps for Linux C/C++ (gcc) programs.
- 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)
- Detectable issues:
-
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
- Detectable issues:
-
- 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'
- Convert with:
- 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"
- Runs the hello program repeatedly in daemon mode.
- 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.
-
Configure core dump system
sudo ./setup_core_dump_systemwide.sh
-
Build executable with debug symbols
g++ -g -O0 -Wall -Wextra -o hello main.cpp
-
Generate core dump
./run_hello_with_core.sh
-
List core dump files
./list_core_with_time.sh
-
Analyze core dump
./gdb_hello_core.sh core.hello.<pid>.<time>
- gdb commands:
r: runbt full: full backtrace with local variableslist: show source lines
- gdb commands:
-
Run as daemon
./run_hello_with_core_daemon.sh
- Installation guide: INSTALL.md