🛠️ MemScope-C: The High-Performance System Memory Profiler
Context Develop a low-level memory profiling utility written in C for Linux-based systems. The tool must interface directly with process memory and system calls to provide deep visibility into allocation patterns, heap health, and resource exhaustion without significantly degrading the target process's performance.
Objective Create a robust diagnostic engine capable of attaching to live processes, intercepting memory allocation routines, and identifying leaks. The utility must provide developers with the ability to capture memory snapshots, compare them to find growth trends, and export actionable reports.
Style Technical, rigorous, and performance-optimized. The tool should follow Unix philosophy standards, providing a powerful Command-Line Interface (CLI) with an interactive mode for real-time analysis.
Tone Analytical and precise. Output should prioritize raw data accuracy and clarity, essential for debugging complex system-level memory issues.
Audience Systems programmers, embedded engineers, and backend developers requiring a high-fidelity alternative to Valgrind with lower overhead and live-attachment capabilities.
Response Requirements
- Core Profiling Logic:
- Process attachment mechanism using
ptraceor/procfilesystem polling. - Heap analysis and allocation tracking via symbol hooking (e.g.,
LD_PRELOADormalloc_hook). - Memory leak detection paired with stack trace capturing for root cause identification.
- Process attachment mechanism using
- Analysis & Visualization:
- Snapshot comparison logic to isolate memory regressions between two points in time.
- Filtering engine to suppress "noise" from known libraries or small, transient allocations.
- Real-time statistics visualization within the interactive CLI.
- Operational Features:
- Multi-format report generation (JSON for automation, Text/Markdown for humans).
- Signal handling (e.g.,
SIGINT,SIGTERM) to ensure clean detachment and data integrity. - Support for custom allocator hooking to track non-standard memory managers.