Skip to content
7amo10Public

About

A Lightweight, Production-Ready Memory Debugging Library for C/C++.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

MemRogue Logo

MemRogue

A Lightweight, Production-Ready Memory Debugging Library for C/C++

Version License: MIT CI C Standard Platform Tests

Quick Start • Documentation • Installation • Examples • Contributing


Overview

MemRogue is a high-performance memory debugging and leak detection library designed for C/C++ applications. It provides transparent interception of memory allocation functions, comprehensive leak detection, and detailed reporting with stack traces—all with minimal runtime overhead.

Key Features

Feature Description
🔍 Allocation Tracking Intercepts malloc, free, calloc, realloc, new, delete
🚨 Leak Detection Identifies unfreed memory with detailed allocation context
📍 Stack Traces Captures full backtraces for each allocation point
⚡ Double-Free Detection Catches use-after-free and double-free errors
❌ Invalid Free Detection Detects frees of non-allocated pointers
📊 Multiple Export Formats Text, JSON, CSV output for integration with analysis tools
🎯 Sampling Mode Configurable sampling to reduce overhead in production
🔧 Zero Code Changes Works via LD_PRELOAD without recompilation
🧵 Thread-Safe Full support for multi-threaded applications

Quick Start

1. Build MemRogue

git clone https://github.com/7amo10/MemRogue.git
cd MemRogue
mkdir build && cd build
cmake ..
make

2. Run Your Application with MemRogue

# Easy way - use the memrogue wrapper script
./scripts/memrogue ./your_application

# Or with LD_PRELOAD directly
LD_PRELOAD=./build/lib/libmemrogue_intercept.so ./your_application

# Enable verbose output
./scripts/memrogue -v ./your_application

# Save report to JSON file
./scripts/memrogue -f json -o leaks.json ./your_application

3. Analyze the Results

# Generate a detailed report
./build/bin/memrogue-report --format=text --output=report.txt

# Export to JSON for programmatic analysis
./build/bin/memrogue-report --format=json --output=report.json

# Export to CSV for spreadsheet analysis
./build/bin/memrogue-report --format=csv --output=report.csv

Installation

Quick Install (Recommended)

# Clone and build
git clone https://github.com/7amo10/MemRogue.git
cd MemRogue
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)

# Install system-wide (requires sudo)
sudo ../scripts/install.sh --system

# Or install for current user only (no sudo needed)
../scripts/install.sh --user

After installation, use MemRogue anywhere:

memrogue ./your_application

Installation Methods

Method 1: Install Script (Recommended)

The install script automatically detects your system and installs MemRogue:

# System-wide installation (/usr/local)
sudo ./scripts/install.sh --system

# User-local installation (~/.local)
./scripts/install.sh --user

# Custom prefix
./scripts/install.sh --prefix=/opt/memrogue

# Interactive mode (asks for options)
./scripts/install.sh

Supported Distributions:

  • Debian/Ubuntu
  • RHEL/CentOS/Fedora
  • Arch Linux
  • Alpine Linux
  • Any Linux with GCC and CMake

Method 2: CMake Install

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local
make -j$(nproc)
sudo make install

Method 3: Package Managers

Debian/Ubuntu (.deb):

# Build the package
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
cpack -G DEB

# Install
sudo dpkg -i memrogue-1.0.0-Linux.deb

Fedora/RHEL/CentOS (.rpm):

# Build the package
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
cpack -G RPM

# Install
sudo rpm -ivh memrogue-1.0.0-Linux.rpm

From Source (Manual):

# Build
git clone https://github.com/7amo10/MemRogue.git
cd MemRogue
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)

# Install
sudo make install
sudo ldconfig

Uninstallation

# If installed via install script
sudo ./scripts/uninstall.sh --system
# Or for user installation
./scripts/uninstall.sh --user

# If installed via CMake
sudo make uninstall

# If installed via package manager
sudo dpkg -r memrogue        # Debian/Ubuntu
sudo rpm -e memrogue         # Fedora/RHEL

Prerequisites

Requirement Minimum Version Notes
GCC 7.0+ Or Clang 6.0+
CMake 3.15+ Build system
glibc 2.17+ Linux C library
pthreads - Thread support

Installing Prerequisites:

# Debian/Ubuntu
sudo apt-get install build-essential cmake

# Fedora/RHEL
sudo dnf install gcc gcc-c++ cmake make

# Arch Linux
sudo pacman -S base-devel cmake

# Alpine
apk add build-base cmake

CMake Options

Option Default Description
CMAKE_BUILD_TYPE Debug Build type: Debug, Release, RelWithDebInfo
MEMROGUE_BUILD_TESTS ON Build unit tests
MEMROGUE_BUILD_EXAMPLES ON Build example applications
MEMROGUE_BUILD_BENCHMARKS ON Build performance benchmarks
MEMROGUE_ENABLE_COVERAGE OFF Enable code coverage reporting

Verifying Installation

# Check if memrogue is available
which memrogue
memrogue --version

# Test with a simple program
cat > test.c << 'EOF'
#include <stdlib.h>
int main() {
    void *p = malloc(100);  // Intentional leak
    return 0;
}
EOF
gcc -o test test.c
memrogue ./test

Usage

Method 1: memrogue Wrapper (Easiest)

The memrogue command is the easiest way to debug your applications:

# Basic usage
memrogue ./your_application

# With arguments
memrogue ./your_application arg1 arg2

# Save report to JSON file
memrogue -f json -o report.json ./your_application

# Verbose mode with detailed output
memrogue -v ./your_application

# Quick check (quiet mode, summary only)
memrogue -q --summary ./your_application

# Custom stack trace depth
memrogue -d 32 ./your_application

# Analyze existing JSON report
memrogue --analyze report.json

memrogue Options:

Option Description
-h, --help Show help message
-v, --verbose Enable verbose output
-q, --quiet Suppress output (errors only)
-o, --output=FILE Write report to file
-f, --format=FMT Output format: text, json, csv
-d, --depth=N Stack trace depth (default: 16)
--no-leaks Disable leak detection
--summary Show only summary

Method 2: LD_PRELOAD (Direct)

Use LD_PRELOAD directly for more control:

LD_PRELOAD=/usr/local/lib/libmemrogue_intercept.so ./your_application

Method 2: Link at Compile Time

For tighter integration, link MemRogue directly:

// your_app.c
#include <memrogue_tracker.h>
#include <memrogue_leak_detector.h>

int main() {
    // Initialize tracker
    MemrogueTracker* tracker = tracker_create();
    
    // Your application code...
    void* ptr = malloc(100);
    // ... use ptr ...
    free(ptr);
    
    // Check for leaks
    LeakReport* report = leak_detector_analyze(tracker);
    leak_report_print(report);
    
    // Cleanup
    leak_report_free(report);
    tracker_destroy(tracker);
    return 0;
}

Compile with:

gcc -o your_app your_app.c -lmemrogue_core -L/path/to/memrogue/lib -I/path/to/memrogue/include

Configuration

MemRogue is configured entirely through environment variables, requiring no code changes:

Core Settings

Variable Default Description
MEMROGUE_ENABLED 1 Enable/disable tracking (0 or 1)
MEMROGUE_OUTPUT stderr Output file path or stderr/stdout
MEMROGUE_VERBOSITY 1 Verbosity level (0=quiet, 1=normal, 2=verbose, 3=debug)
MEMROGUE_REPORT_ON_EXIT 1 Generate report on program exit

Detection Settings

Variable Default Description
MEMROGUE_DETECT_DOUBLE_FREE 1 Enable double-free detection
MEMROGUE_DETECT_INVALID_FREE 1 Enable invalid-free detection

Stack Trace Settings

Variable Default Description
MEMROGUE_BACKTRACE 1 Enable stack trace capture
MEMROGUE_MAX_DEPTH 16 Maximum stack trace depth

Performance Tuning

Variable Default Description
MEMROGUE_SAMPLE_RATE 100 Sampling rate percentage (1-100)
MEMROGUE_SAMPLING_MODE random Sampling mode: random or deterministic

Example Configurations

Development (Maximum Detail)

export MEMROGUE_ENABLED=1
export MEMROGUE_VERBOSITY=3
export MEMROGUE_BACKTRACE=1
export MEMROGUE_MAX_DEPTH=32
export MEMROGUE_SAMPLE_RATE=100

Production (Minimal Overhead)

export MEMROGUE_ENABLED=1
export MEMROGUE_VERBOSITY=0
export MEMROGUE_BACKTRACE=0
export MEMROGUE_SAMPLE_RATE=10
export MEMROGUE_SAMPLING_MODE=random

CLI Tool

MemRogue includes memrogue-report for analyzing and exporting memory reports:

Usage: memrogue-report [OPTIONS] [INPUT_FILE]

Options:
  -f, --format FORMAT    Output format: text, json, csv, summary (default: text)
  -o, --output FILE      Output file (default: stdout)
  -v, --verbose          Enable verbose output
  -q, --quiet            Suppress non-essential output
  -h, --help             Show this help message
  -V, --version          Show version information

Exit Codes:
  0   Success
  1   General error
  2   Usage/argument error
  3   I/O error
  4   Parse error
  5   Memory allocation error
  10  Leaks found (with --exit-on-leak)

Examples:
  memrogue-report --format=json -o report.json leaks.bin
  memrogue-report --format=csv --output=analysis.csv
  memrogue-report --format=summary --verbose

Output Formats

Text Format

Human-readable format ideal for development:

================================================================================
                           MEMROGUE LEAK REPORT
================================================================================

Summary:
  Total allocations tracked: 1,247
  Total deallocations:       1,244
  Memory leaks detected:     3
  Total leaked bytes:        2,048

--------------------------------------------------------------------------------
LEAK #1: 1024 bytes at 0x7f8b4c001000
--------------------------------------------------------------------------------
  Allocated at:
    #0  malloc (memrogue_intercept.c:45)
    #1  create_buffer (buffer.c:23)
    #2  initialize_system (main.c:156)
    #3  main (main.c:42)

JSON Format

Structured format for programmatic analysis:

{
  "version": "1.0",
  "summary": {
    "total_allocations": 1247,
    "total_deallocations": 1244,
    "leak_count": 3,
    "total_leaked_bytes": 2048
  },
  "leaks": [
    {
      "address": "0x7f8b4c001000",
      "size": 1024,
      "backtrace": [
        {"function": "malloc", "file": "memrogue_intercept.c", "line": 45},
        {"function": "create_buffer", "file": "buffer.c", "line": 23}
      ]
    }
  ]
}

CSV Format

Tabular format for spreadsheet analysis (RFC 4180 compliant):

address,size,allocation_time,source_file,source_line,function_name,backtrace
0x7f8b4c001000,1024,1699876543.123456,buffer.c,23,create_buffer,"malloc;create_buffer;initialize_system;main"
0x7f8b4c002000,512,1699876543.234567,utils.c,89,allocate_string,"malloc;allocate_string;parse_config;main"

Examples

Basic Leak Detection

#include <stdlib.h>

int main() {
    // This allocation will be reported as a leak
    char* leaked = malloc(100);
    
    // This allocation is properly freed
    char* not_leaked = malloc(200);
    free(not_leaked);
    
    return 0;
}

Run with:

LD_PRELOAD=./lib/libmemrogue.so ./basic_example

Python Analysis Script

MemRogue includes a Python script for analyzing CSV exports:

# Generate CSV report
MEMROGUE_OUTPUT=leaks.csv LD_PRELOAD=./lib/libmemrogue.so ./your_app

# Analyze with Python
python3 examples/analyze_leaks.py leaks.csv

See examples/analyze_leaks.py for the full script.


Documentation

Document Description
USAGE.md Detailed usage guide and tutorials
API.md Complete API reference
CONFIGURATION.md Configuration reference
TROUBLESHOOTING.md Common issues and solutions
Examples README Example applications guide and usage

Project Structure

memrogue/
├── include/              # Public header files
│   ├── memrogue_tracker.h
│   ├── memrogue_leak_detector.h
│   ├── memrogue_config.h
│   ├── memrogue_report.h
│   ├── memrogue_json.h
│   ├── memrogue_csv.h
│   └── ...
├── src/                  # Source implementation
├── tests/                # Unit and integration tests
├── examples/             # Example applications
├── tools/                # CLI tools
└── docs/                 # Documentation

Testing

MemRogue includes comprehensive tests:

cd build

# Run all tests
make test

# Run specific test
./bin/test_leak_detector

# Run with verbose output
ctest -V

Performance

MemRogue is designed for minimal overhead:

Mode Overhead Use Case
Full tracking ~5-15% Development, debugging
Sampling (10%) ~1-3% Testing, staging
Disabled 0% Production fallback

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Quick steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes and add tests
  4. Run tests: ctest --output-on-failure
  5. Submit a pull request

For bugs and feature requests, please open an issue.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

  • Inspired by Valgrind, ASan, and other memory debugging tools
  • Built with modern C best practices
  • Project management powered by Plane

MemRogue - Making Memory Bugs Visible

About

A Lightweight, Production-Ready Memory Debugging Library for C/C++.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages