Overview
Debugging AOT-compiled code differs from debugging interpreted JavaScript. Porffor provides several tools and techniques to help diagnose issues in compiled WebAssembly and native binaries.Debug Mode
The-d flag enables debug mode, which includes additional information in compiled output:
- Function and variable names in Wasm
- Debug logging during compilation
- Source location information
- Preserved symbol names
- Better error messages
Using Debug Mode with Different Targets
- Run
- Wasm
- Native
- C
Disassembling WebAssembly
View Generated Instructions
Use the-f flag to print disassembled Wasm for your functions:
View Specific Function
To view a specific function’s Wasm:Example Output
example.js
Using —disassemble Flag
For detailed disassembly:- Full instruction listing
- Local variable information
- Type information
- Call graph details
The Debug Command
Porffor includes an experimental source-level debugger:Debug Command Features
Stepping:- Step Over: Execute current line, skip function calls
- Step In: Enter function calls
- Step Out: Execute until current function returns
- Resume: Continue execution
- Set breakpoints on specific lines
- Execution pauses when breakpoint is hit
- View current line
- See call stack
- Console output in dedicated panel
Debug Command Limitations
- No variable inspection: Cannot view variable values
- Limited expression evaluation: Cannot evaluate expressions at breakpoints
- Function-level granularity: Stepping is per-line, not per-expression
- No watch expressions: Cannot track specific variables
The debugger modifies your source code by injecting profiling calls, so debugging output may differ from normal execution.
Debugging Strategies
Adding Console Logging
The most reliable debugging technique:Using performance.now() for Timing
Profile sections of code:Bisecting Problems
Isolate issues by progressively commenting out code:Comparing with Node.js
Test behavior in Node.js to verify correctness:- Porffor bugs
- Unsupported features
- Different runtime behavior
Debugging Native Binaries
Using GDB (Linux/macOS)
Compile with debug symbols:Using LLDB (macOS)
Inspecting Generated C Code
Generate C code to understand what’s happening:output.c to see:
- How functions are compiled
- Memory management
- Type conversions
- Control flow
Debugging WebAssembly
Using wasm2wat
Convert Wasm to text format:output.wat to see:
- Function structure
- Local variables
- Instruction sequence
- Imports and exports
Browser DevTools
Some browsers support WebAssembly debugging:- Load Wasm module in browser
- Open DevTools
- Find Wasm in Sources panel
- Set breakpoints
- Inspect stack and memory
Porffor Wasm doesn’t use WASI, so running in browser requires custom import setup.
Common Issues and Solutions
Undefined or null reference errors
Undefined or null reference errors
Check variable initialization:Or add guards:
Type-related errors
Type-related errors
Scope errors
Scope errors
Porffor has limited scope support:
Memory/performance issues
Memory/performance issues
Profile with the profile command:Look for:
- Functions called most frequently
- Functions taking the most time
- Unexpected memory allocations
Compilation fails
Compilation fails
Enable debug mode to see detailed error:Common causes:
- Unsupported syntax (eval, Function constructor)
- Complex async/await patterns
- Scope issues
- Parser errors
The Profile Command
Theprofile command provides detailed performance information:
Profile Output
Shows for each function:- Call count: How many times called
- Total time: Cumulative execution time
- Average time: Mean time per call
- Percentage: Portion of total execution time
Example Profile Session
example.js
maincalled once, moderate timefibonaccicalled thousands of times (recursive), most total time- Identifies performance bottleneck
Using Profile Data
Use profiling to:- Find hot functions: Which functions consume most time
- Identify call patterns: How many times functions are called
- Guide optimization: Focus on expensive functions
- Measure improvements: Before/after optimization comparison
Debugging Checklist
Getting Help
If you’re stuck:- Check documentation: Review guides and API reference
- Simplify code: Create minimal reproduction
- Try different flags: Test with
-O0, different parsers, etc. - Ask for help: Join the Porffor Discord
Next Steps
Profiling
Detailed performance analysis
Optimization
Make your code faster
TypeScript
Better type safety and debugging
Contributing
Report bugs and contribute fixes