Debugging
Techniques and best practices for debugging plugins and handling errors in your language module development process.
Debugging Lua plugins that are embedded in C++ applications, such as Plugify, requires using the EmmyLua plugin and compatible debuggers to inspect and troubleshoot your Lua code. This guide provides an overview of how to debug Lua plugins in this context and directs you to external resources for detailed instructions.
Prerequisites
Before debugging your Lua plugins, ensure you have the following:
- Lua Environment (version
5.1
,5.2
,5.3
, or5.4
) installed and configured. - EmmyLua Plugin installed in your IDE (e.g., IntelliJ IDEA, VS Code).
- Plugify Core Library (built and available).
- Lua Language Module (`plugify-module-lua`) installed and configured.
- A Lua plugin to debug.
Debugging Lua Plugins with EmmyLua
The EmmyLua plugin provides robust debugging capabilities for Lua code. Below are the general steps to debug Lua plugins embedded in C++ applications:
Enable EmmyLua in Plugify
- Ensure the Lua Language Module is configured to support debugging.
- Launch Plugify with your Lua plugin loaded.
Connect to the EmmyLua Debugger
- Open your IDE (e.g., IntelliJ IDEA or VS Code) with the EmmyLua plugin installed.
- Configure the EmmyLua debugger to connect to your Lua environment (e.g., via a debug adapter or Lua’s debug library).
- Your Lua plugin should appear in the debugger interface. Start a debugging session to inspect the code.
Set Breakpoints
- Open the Lua source files for your plugin in the IDE.
- Set breakpoints in your code by clicking in the left margin next to the line numbers.
Debug Your Plugin
- Trigger the functionality in your plugin that you want to debug.
- The debugger will pause execution at your breakpoints, allowing you to inspect variables, step through code, and analyze the call stack.
Detailed Debugging Guide
For a comprehensive step-by-step guide on using EmmyLua, refer to the official EmmyLua documentation:
Common Debugging Scenarios
1. Plugin Crashes
- Check the Plugify logs for Lua error messages.
- Use the EmmyLua debugger to identify the exact line of code causing the crash.
- Inspect variables and stack traces to diagnose the issue.
2. Plugin Not Loading
- Verify that the plugin is placed in the correct directory.
- Check for missing Lua dependencies or syntax errors.
- Use the EmmyLua debugger to step through the plugin initialization code.
3. Unexpected Behavior
- Set breakpoints in the relevant functions to trace the flow of execution.
- Inspect variable values to identify discrepancies.
- Use conditional breakpoints to debug specific scenarios.
Advanced Debugging Tips
1. Use Logging
- Add print() statements to your Lua plugin code to track execution flow and variable values.
- Use Plugify’s built-in logging system to output messages to the console or log files.
2. Debugging Coroutines
- Use EmmyLua’s coroutine debugging features to trace coroutine execution.
- Inspect coroutine states (e.g., running, suspended) to identify issues.
3. Debugging Memory Issues
- Use Lua’s collectgarbage("count") to monitor memory usage.
- Take heap snapshots with EmmyLua or other Lua tools to analyze memory consumption.
Troubleshooting
1. Debugger Not Connecting
- Ensure Plugify is running and the EmmyLua debugger is enabled.
- Verify that the correct Lua environment is configured in the IDE.
2. Breakpoints Not Hit
- Ensure the Lua code matches the version being executed.
- Rebuild the plugin and restart Plugify.
3. Debugger Crashes
- Update Lua to the latest compatible version.
- Ensure all dependencies are compatible with your development environment.
For more advanced debugging techniques and tools, refer to the EmmyLua Documentation.