Skip to content

Latest commit

 

History

History
309 lines (234 loc) · 6.41 KB

File metadata and controls

309 lines (234 loc) · 6.41 KB
name cpp-debugger
description AI-powered C/C++ debugger with breakpoint management, variable inspection, and step execution. Invoke when user needs to debug C/C++ code or asks for debugging assistance.

C/C++ AI Debugger

This skill provides AI-assisted debugging capabilities for C/C++ projects, supporting both GDB and LLDB debuggers.

Features

  • Breakpoint Management: Set, remove, enable/disable breakpoints with support for conditional breakpoints
  • Execution Control: Run, step, continue, and control program execution
  • Variable Inspection: View local and global variables, evaluate expressions
  • Call Stack Navigation: Navigate and inspect the call stack
  • Multi-Session Support: Manage multiple debugging sessions simultaneously
  • Cross-Platform: Works with GDB (Linux/macOS/Windows MinGW) and LLDB (macOS/Windows)

When to Invoke

Invoke this skill when:

  • User asks to debug C/C++ code
  • User mentions breakpoints, stepping through code, or inspecting variables
  • User encounters a crash or segmentation fault
  • User wants to understand program flow
  • User asks for help with debugging

Quick Start

Basic Usage

from src.debugger import AIDebugger

# Initialize debugger
debugger = AIDebugger()

# Create a debug session
result = debugger.execute_command('''{
    "action": "create_session",
    "params": {
        "program_path": "./my_program"
    }
}''')

# Set a breakpoint
debugger.execute_command('''{
    "action": "set_breakpoint",
    "params": {
        "session_id": "SESSION_ID",
        "location": "main.cpp:42"
    }
}''')

# Run the program
debugger.execute_command('''{
    "action": "run",
    "params": {
        "session_id": "SESSION_ID"
    }
}''')

Supported Actions

Session Management

  • create_session: Create a new debugging session
  • close_session: Close and cleanup a debugging session
  • list_sessions: List all active sessions

Breakpoint Management

  • set_breakpoint: Set a breakpoint at a location
  • remove_breakpoint: Remove a breakpoint
  • enable_breakpoint: Enable a disabled breakpoint
  • disable_breakpoint: Disable a breakpoint
  • list_breakpoints: List all breakpoints

Execution Control

  • run: Start or restart program execution
  • continue: Continue execution after breakpoint
  • step: Step into function calls
  • next: Step over function calls
  • finish: Execute until function returns
  • pause: Pause program execution
  • terminate: Terminate the debugged program

Inspection

  • get_variables: Get variable values
  • get_backtrace: Get call stack
  • evaluate_expression: Evaluate an expression
  • get_registers: Get CPU registers
  • read_memory: Read memory at address

Location Formats

Breakpoints can be set using various location formats:

  • File and line: "main.cpp:42"
  • Function name: "main" or "MyClass::myMethod"
  • File and function: "main.cpp:main"
  • Address: "*0x4005a0"

Conditional Breakpoints

{
    "action": "set_breakpoint",
    "params": {
        "session_id": "SESSION_ID",
        "location": "main.cpp:42",
        "condition": "x > 10"
    }
}

Response Format

All commands return JSON responses:

Success:

{
    "success": true,
    "result": {
        // Action-specific result
    }
}

Error:

{
    "success": false,
    "error": "Error description"
}

Requirements

  • Python 3.8 or higher
  • GDB 9.0+ or LLDB 12.0+
  • Required Python packages: pygdbmi, lldb (optional)

Installation

Install required dependencies:

pip install pygdbmi
pip install lldb  # Optional, for LLDB support

Examples

Example 1: Debug a Segmentation Fault

debugger = AIDebugger()

# Create session
session = debugger.execute_command('''{
    "action": "create_session",
    "params": {"program_path": "./my_program"}
}''')
session_id = session['result']['session_id']

# Run until crash
debugger.execute_command(f'''{{
    "action": "run",
    "params": {{"session_id": "{session_id}"}}
}}''')

# Get backtrace to find the crash location
bt = debugger.execute_command(f'''{{
    "action": "get_backtrace",
    "params": {{"session_id": "{session_id}"}}
}}''')

Example 2: Step Through Code

# Set breakpoint
debugger.execute_command(f'''{{
    "action": "set_breakpoint",
    "params": {{
        "session_id": "{session_id}",
        "location": "main.cpp:10"
    }}
}}''')

# Run to breakpoint
debugger.execute_command(f'''{{
    "action": "run",
    "params": {{"session_id": "{session_id}"}}
}}''')

# Step through code
for i in range(5):
    debugger.execute_command(f'''{{
        "action": "next",
        "params": {{"session_id": "{session_id}"}}
    }}''')
    
    # Check variables
    vars = debugger.execute_command(f'''{{
        "action": "get_variables",
        "params": {{
            "session_id": "{session_id}",
            "scope": "local"
        }}
    }}''')

Troubleshooting

GDB Not Found

If GDB is not found, ensure it's installed and in your PATH:

# Linux
sudo apt-get install gdb

# macOS
brew install gdb

# Windows (MinGW)
pacman -S mingw-w64-x86_64-gdb

LLDB Not Found

For LLDB support:

# macOS (comes with Xcode)
xcode-select --install

# Linux
sudo apt-get install lldb

# Windows
# Install LLVM from https://releases.llvm.org/

Permission Denied

On some systems, debugging requires elevated permissions. Run with appropriate permissions or configure ptrace scope:

# Linux temporary fix
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Advanced Features

Watchpoints

Monitor variable changes:

debugger.execute_command(f'''{{
    "action": "set_watchpoint",
    "params": {{
        "session_id": "{session_id}",
        "variable": "my_var"
    }}
}}''')

Thread Management

Switch between threads:

# List threads
threads = debugger.execute_command(f'''{{
    "action": "list_threads",
    "params": {{"session_id": "{session_id}"}}
}}''')

# Switch to thread 2
debugger.execute_command(f'''{{
    "action": "switch_thread",
    "params": {{
        "session_id": "{session_id}",
        "thread_id": 2
    }}
}}''')

Limitations

  • Remote debugging is not yet supported
  • Visual Studio debugger is not supported (use GDB/LLDB on Windows)
  • Core dump analysis is not implemented yet

Contributing

Contributions are welcome! Please see the project repository for guidelines.

License

MIT License