Overview
Debugging recompiled code presents unique challenges since the generated C++ code doesn’t directly correspond to the original PowerPC assembly. This guide covers tools, techniques, and common issues when debugging ReXGlue applications.Build Configuration
Debug vs Release Builds
ReXGlue supports multiple build configurations via CMake:-g: Generate debug symbols-O0: No optimization for easier stepping
-O3: Maximum optimization-DNDEBUG: Disables assertions
Runtime Assertions
Assertion System
ReXGlue uses a comprehensive assertion system defined inrex/assert.h. All assertions are compiled out in Release builds when NDEBUG is defined.
Available assertions:
Static Assertions
/home/daytona/workspace/source/include/rex/assert.h:25-26
Sanitizers
Undefined Behavior Sanitizer (UBSan)
Enable UBSan to detect undefined behavior at runtime:- Signed integer overflow
- Null pointer dereferences
- Misaligned memory access
- Division by zero
- Invalid enum values
- Out-of-bounds array access
0x100000000. Only UBSan is enabled.
Running with Sanitizers
Debugging Recompiled Functions
Function Naming
Recompiled functions follow the naming convention:Setting Breakpoints
In GDB:Inspecting PPC State
Recompiled functions use aPPCContext parameter that holds the emulated PowerPC CPU state:
Common Issues
Memory Access Violations
Symptom: Segmentation fault when accessing guest memory Cause: Guest memory not mapped or accessed outside valid range Debug steps:- Enable exception handlers to catch the fault address
- Check if address is in valid guest range (typically
0x80000000 - 0x9FFFFFFF) - Verify memory was initialized before access
Invalid Instruction Exceptions
Symptom:SIGILL or illegal instruction crash
Cause:
- Unimplemented PowerPC instruction
- Invalid opcode in source binary
- Code region executed as data
- Check the faulting address in exception handler
- Disassemble the PowerPC code at that address
- Add instruction to
invalidInstructionHintsif it’s data:
Assertion Failures
Symptom: Assertion failed messages during execution Debug steps:- Read the assertion message and location
- Set a breakpoint at the assertion line
- Inspect variables when assertion fires
- Run in Debug build for maximum information
Performance Issues in Debug Builds
Symptom: Recompiled code runs 10-100x slower than expected Cause: Debug builds use-O0 and enable expensive assertions
Solutions:
- Use
RelWithDebInfofor moderate optimization with debug symbols - Profile in Release build, debug specific issues in Debug
- Disable specific hot-path assertions if necessary
Logging and Tracing
Kernel Import Tracing
Enable kernel import tracing to debug Xbox 360 kernel calls:/home/daytona/workspace/source/include/rex/system/kernel_state.h:42-52
Custom Debug Output
Add debug prints in recompiled code:Debugging Tools
GDB (Linux)
Recommended GDB settings for ReXGlue:LLDB (macOS/Linux)
Visual Studio (Windows)
- Open the generated Visual Studio solution
- Set the recompiled executable as the startup project
- F5 to debug with full IDE support
- Use Exception Settings to break on C++ exceptions
Advanced Debugging
Debugging the Recompiler Itself
If you suspect a code generation bug:- Inspect generated code: Check the output
.cppfiles in your output directory - Compare with disassembly: Use a PowerPC disassembler on the source binary
- Add code comments: Enable verbose output in the recompiler
- File a bug report: Include the problematic function address and generated code
Memory Corruption
Use Valgrind (Linux) or Dr. Memory (Windows) to detect memory errors:Debugging Race Conditions
ReXGlue is multi-threaded. Use thread sanitizer carefully:Tips and Best Practices
- Start with Debug builds: Get your code working first, optimize later
- Use assertions liberally: They’re free in Release builds
- Enable exception handlers: Catch crashes early with detailed context
- Test incrementally: Recompile one function at a time when debugging
- Compare with emulation: Run the same code in Xenia to verify behavior
- Read the generated code: Understanding the output helps debug issues
- Use sanitizers in CI: Catch bugs before they reach production
Related Topics
- Exception Handlers - Catch and handle runtime exceptions
- Optimization - Performance tuning and optimization flags
- Platform Support - Platform-specific debugging notes