Debugging Rust Code
Using an IDE Debugger
The recommended approach for debugging Rust code is using your IDE’s built-in debugger.- VS Code
- IntelliJ IDEA / CLion
- LLDB (Command Line)
Setup:Usage:
- Install the rust-analyzer extension
- Install the CodeLLDB extension
.vscode/launch.json:- Set breakpoints in Rust code by clicking in the gutter
- Press F5 to start debugging
- Use standard debugging controls (step, continue, etc.)
Debug Print Statements
Quick debugging with print statements:Use
eprintln! instead of println! to avoid interfering with program output.Verbose Logging
Enable detailed logging output:error- Errors onlywarn- Warnings and errorsinfo- Informational messagesdebug- Debug informationtrace- Very verbose output
Using the log Crate
Add logging to your code:Debugging JavaScript Runtime
Chrome DevTools Inspector
Deno supports the Chrome DevTools Protocol for debugging JavaScript:Connect DevTools
- Open Chrome browser
- Navigate to
chrome://inspect - Click “inspect” under your Deno process
- DevTools will open
The
--inspect-brk flag breaks on the first line of code, while --inspect allows the program to run until you set a breakpoint.Console Debugging
JavaScript console methods:Debugging Tests
Running Tests in Debug Mode
Debugging Spec Tests
Debugging Test in IDE
VS Code:- Click the “Debug” link above
#[test]function (requires rust-analyzer) - Or add to
.vscode/launch.json:
Performance Debugging
Backtraces
Get detailed stack traces:Profiling
- perf (Linux)
- Instruments (macOS)
- cargo-flamegraph
Memory Debugging
Common Debugging Scenarios
Debugging a crash or panic
Debugging a crash or panic
Steps:
-
Enable backtrace:
- Look for the panic message and location
- Set a breakpoint before the panic in debugger
- Examine variable state at crash point
Debugging slow performance
Debugging slow performance
Steps:
-
Profile the code:
- Look for hot functions in the profile
-
Add timing logs:
- Optimize hot paths
Debugging module resolution
Debugging module resolution
Steps:
-
Enable debug logging:
- Look for “Resolving” and “Loading” messages
-
Check
module_loader.rswith breakpoints - Verify import map and resolution logic
Debugging permission errors
Debugging permission errors
Steps:
-
Check permission flags:
-
Add debug logging in permission checks:
-
Set breakpoint in
runtime/permissions/ - Verify permission state
Debugging op failures
Debugging op failures
Steps:
-
Add logging to the op:
- Check JavaScript console for errors
- Use inspector to see JS stack trace
- Set breakpoint in op implementation
Advanced Debugging
Debugging V8
Debug V8-specific issues:Core Dumps
Generate and analyze core dumps:Remote Debugging
Debug on remote machines:Debugging Tools Summary
IDE Debugger
Best for step-by-step debuggingUse when: Tracing logic flow, inspecting variables
Print Debugging
Quick and simpleUse when: Quick checks, production debugging
Chrome DevTools
JavaScript debuggingUse when: Debugging JS runtime issues
Logging
Runtime diagnosticsUse when: Tracking execution flow, production issues
Profiler
Performance analysisUse when: Finding bottlenecks, optimizing
Backtrace
Crash analysisUse when: Diagnosing panics, segfaults
Troubleshooting Tips
Gather information
- What’s the error message?
- What’s the stack trace?
- What are the inputs?
- What’s the expected vs actual behavior?
Narrow down the cause
- Use print debugging to find where it fails
- Use bisection: comment out code to isolate the issue
- Check recent changes with
git log
Use appropriate tools
- Debugger for complex logic
- Profiler for performance
- Inspector for JavaScript
- Logging for understanding flow
Getting Help
If you’re stuck:- Search existing issues on GitHub
- Ask in Discord - Deno Discord
- Open a discussion on GitHub Discussions
- File an issue with reproduction steps
Next Steps
Testing
Learn about testing Deno
Code Structure
Understand the codebase structure