Python core dump system for post-mortem debugging
Project description
Dpdb - Python Core Dump System
A comprehensive Python "core dump" system that captures execution state (variables, frames, stack traces) for post-mortem debugging, similar to C++ core dumps.
Features
- Automatic Core Dumps: Automatically create core dumps when unhandled exceptions occur
- Manual Core Dumps: Create core dumps at any point in your code
- Post-Mortem Debugger: Interactive debugger to analyze core dumps with rich terminal UI
- Command-Line Interface: Run programs with automatic crash handling or debug existing core dumps
- Adaptive Theme Detection: Automatically detects light/dark terminal themes for optimal color schemes
- Complete State Capture: Captures local variables, global variables, stack frames, and code context
- Persistent Storage: Core dumps are saved as pickle files for later analysis
- pdb-like Interface: Familiar debugging commands for easy use
Quick Start
0. Installation
Requirements:
- Python 3.8+
- Rich - For terminal UI and formatting
- term_background (optional) - For improved terminal theme detection
# Basic installation
pip install coredump-debugger
# With optional dependencies for better theme detection
pip install coredump-debugger[dev]
The debugger will work without term_background but will have more accurate theme detection when it's available.
After installation, the dpdb command will be available in your system PATH. The package automatically creates a console script entry point that installs the dpdb executable.
1. Command-Line Usage
Dpdb provides a command-line interface similar to pdb for running programs with automatic crash handling:
# Run a Python program with automatic core dump generation
dpdb my_program.py arg1 arg2
# Debug an existing core dump file
dpdb crash_dump.pkl
# Get help
dpdb --help
Mode 1: Program Execution with Crash Handling
dpdb my_program.py arg1 arg2
This automatically installs the global core dump handler and runs your program. If the program crashes, a core dump file will be created automatically.
Mode 2: Core Dump Debugging
dpdb crash_dump.pkl
This loads an existing core dump file and starts the interactive debugger.
2. Programmatic API
Global Exception Handler
Just add dpdb.install_exception_handler("path/to/core_dump.pkl") to your code, and a core dump will be created and saved automatically when an exception occurs.
import dpdb
# Install automatic core dump generation
dpdb.install_exception_handler("my_app_crash.pkl")
# Your application code here
def my_function():
x = 42
y = "hello"
# This will create a core dump if an exception occurs
result = 1 / 0 # ZeroDivisionError
my_function()
Manual Core Dump
import dpdb
def debug_point():
local_var = "debug info"
data = [1, 2, 3, 4, 5]
# Create a manual core dump
dump = dpdb.CoreDumpGenerator.create_from_current_stack()
dpdb.save_core_dump(dump, "debug_dump.pkl")
debug_point()
3. Post-Mortem Debugging
Attach Dpdb to a core dump file:
# Debug a core dump file
dpdb my_app_crash.pkl
# Or using the module
python -m dpdb my_app_crash.pkl
Command-Line Interface
The dpdb command provides a powerful interface for both running programs with crash protection and debugging existing core dumps.
Usage Patterns
# Run a program with automatic crash handling
dpdb my_script.py arg1 arg2
# Debug an existing core dump
dpdb crash_dump.pkl
# Get help
dpdb --help
dpdb -h
Examples
Running a Program with Crash Protection:
# Run a script that might crash
dpdb my_data_processor.py input.csv output.json
# Run with multiple arguments
dpdb my_web_server.py --port 8080 --debug
# The program runs normally, but if it crashes, a core dump is created
Debugging a Core Dump:
# After a crash, debug the generated core dump
dpdb main_process_12345_crash.pkl
# This starts the interactive debugger with full source code access
How It Works
-
Program Execution Mode: When you run
dpdb my_program.py, the system:- Installs the global core dump handler
- Loads and executes your program in the same process
- Preserves all source code information for debugging
- Creates a core dump file if the program crashes
-
Core Dump Debugging Mode: When you run
dpdb crash_dump.pkl, the system:- Loads the core dump file
- Starts the interactive debugger
- Provides full access to the crash state and source code
Advantages Over Subprocess Approaches
- Source Code Preservation: Full source code context is available in the debugger
- Same Process Execution: The global handler works correctly because everything runs in the same process
- Clean Implementation: No temporary files or complex subprocess management
- Argument Handling: Programs receive the correct
sys.argvarguments - Elegant Design: Direct module loading and execution
Post-Mortem Debugger Commands
The interactive debugger provides the following commands with rich terminal formatting:
Navigation Commands
up [count]oru [count]- Move up the stack (towards caller)down [count]ord [count]- Move down the stack (towards callee)whereorworbt- Show stack trace with current frame indicatorframe [num]orf [num]- Select frame by numberframes- Show detailed frame information
Information Commands
listorl- Show source code context for current framelonglistorll- Show extended source code for current framelocals- Show local variables in current frameglobals- Show global variables in current frameargsora- Show function argumentsinfo- Show core dump information (timestamp, Python version, etc.)source [object]- Show source code for object
Evaluation Commands
p <expression>- Print expression valuepp <expression>- Pretty-print expression valuewhatis <expression>- Show expression type<statement>- Execute Python code directly
Display Commands
display <expression>- Add expression to auto-display listundisplay [expression]- Remove from auto-display list
Advanced Commands
interact- Start interactive Python interpreter
Control Commands
helporh- Show help informationquitorq- Exit the debugger
Theme Detection and Customization
The debugger automatically detects your terminal's theme (light or dark) and adjusts colors accordingly for optimal readability. The detection uses multiple methods:
Automatic Detection Methods
- term_background library - Most reliable method when available
- Rich's color system detection - Leverages Rich's built-in terminal capabilities
- COLORFGBG environment variable - Standard terminal background indicator
- Terminal program identification - Detects known terminals with light defaults
- Various theme environment variables - Checks for theme-related settings
Manual Theme Override
You can override the automatic detection by setting an environment variable:
# Force light theme
export DPDB_THEME=light
# Force dark theme
export DPDB_THEME=dark
# Let the system auto-detect (default)
unset DPDB_THEME
Color Schemes
- Dark theme: Uses bright colors (cyan, yellow, green, magenta) on dark backgrounds
- Light theme: Uses darker colors (blue, dark_orange, dark_green, purple) on light backgrounds
API Reference
Command-Line Interface
dpdb <program.py> [args...]- Run program with automatic crash handlingdpdb <dump_file>- Debug an existing core dumpdpdb --help- Show usage information
CoreDumpGenerator
create_from_exception(exc_type, exc_value, exc_traceback)- Create core dump from exceptioncreate_from_current_stack()- Create core dump from current call stack
Functions
save_core_dump(core_dump, filename)- Save core dump to fileload_core_dump(filename)- Load core dump from fileinstall_exception_handler(dump_filename)- Install automatic exception handlerinstall_global_handler()- Install global handler for all processesuninstall_global_handler()- Uninstall global handler and restore normal behavior
PostMortemDebugger
__init__(core_dump)- Initialize with a core dumprun()- Start interactive debugging session
Multiprocessing Support
The core dump system provides excellent support for multiprocessing applications through a global handler that automatically applies core dump handling to ALL processes, including those created by frameworks you can't modify:
import dpdb
import multiprocessing as mp
# Install global handler once at the start of your program
dpdb.install_global_handler()
# Now ALL processes will automatically generate core dumps on crashes
# No code modification needed!
def worker_function(data):
# Your worker code here
result = process_data(data)
return result
# Standard multiprocessing - no changes needed
process = mp.Process(target=worker_function, args=(data,))
process.start()
process.join()
# Works with frameworks like accelerate, torch.distributed, etc.
# that control process creation
Framework Compatibility
The global handler works seamlessly with existing frameworks:
import dpdb
# Install once at the start of your program
dpdb.install_global_handler()
# Now accelerate, torch.distributed, etc. will automatically
# generate core dumps for crashed processes
from accelerate import Accelerator
accelerator = Accelerator()
# All processes launched by accelerate will have core dump support
Limitations
- Some objects cannot be serialized (e.g., file handles, network connections)
- Very large objects may consume significant disk space
- Core dumps capture state at the time of creation, not live debugging
- Thread-local storage may not be fully captured in multi-threaded applications
Security Considerations
Core dumps may contain sensitive information (passwords, tokens, etc.), so
- Store core dump files securely
- Be cautious when sharing core dump files
License
This code is provided under GPLv3.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file coredump_debugger-0.0.4.tar.gz.
File metadata
- Download URL: coredump_debugger-0.0.4.tar.gz
- Upload date:
- Size: 31.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
affd512790a1a68e826f16ba670fcae3e1b2e3ee7d5867bd350473a9ff7bb62a
|
|
| MD5 |
8e71f57deffd755831a3f6f986885661
|
|
| BLAKE2b-256 |
b55d3b2534c7cd58d6484ed04976eb703033b4246d2ec643612e1e45378106aa
|
File details
Details for the file coredump_debugger-0.0.4-py3-none-any.whl.
File metadata
- Download URL: coredump_debugger-0.0.4-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ef34e7c620147e12ddf37ce82d8adbf1602cd184b06a0dc181b95be1ee5d572
|
|
| MD5 |
043f7c8b66d0cf47d8744627e2c8e6ec
|
|
| BLAKE2b-256 |
c87118bd9adf005b6e79d41f1cb9f2b89f7ee4f1d9b4040c41e670588a3ffc26
|