Debugging

Guide on diagnosing issues, debugging plugins, and fixing common errors when running Plugify in a Metamod environment.

Debugging Plugify-related issues can be challenging, especially since our packages (modules and plugins) are published in Release mode without symbols by default. This page provides a comprehensive guide to help you debug effectively, whether you're troubleshooting Plugify itself, a plugin, or a game like CS2.

Building with Symbols

To debug effectively, you need access to debugging symbols. Since our published packages do not include symbols, you will need to build the modules or plugins yourself. Here’s how:

  1. Clone the Repository: Clone the Plugify repository or the repository of the module/plugin you want to debug.
  2. Build in Debug or RelWithDebInfo Mode: Configure the build to include debugging symbols.
    • For CMake projects, use the -DCMAKE_BUILD_TYPE=Debug flag.
    cmake -DCMAKE_BUILD_TYPE=Debug ..
    cmake --build .
    
  3. Use the Debug Build: Replace the Release version of the module/plugin with the Debug version you just built.

Debugging CS2 or Other Source2 Games

Plugify is often used with games like CS2. Debugging these games requires specific tools and configurations. Below are instructions for Windows and Linux/macOS.

Debugging on Windows

The most efficient approach on Windows is to use Visual Studio 2022 as a debugger.

Steps to Debug CS2 with Visual Studio

  1. Open the Executable:
    • In Visual Studio, go to File > Open > Project/Solution.
    • Select the game executable (e.g., cs2.exe).

Open Project Or Solution

  1. Set Command-Line Arguments:
    • Go to Project > Properties > Debugging.
    • In the Command Arguments field, add everything you usually use to run your server.

Set Properties

  1. Configure Debugger Type:
    • For C++ code, leave the debugger type as Auto.
    • For debugging C# scripts, set the debugger type to Mixed (.NET Core + 5).
  2. Disable I/O Redirection:
    • CS2 has a custom console that can crash Visual Studio or corrupt output if I/O redirection is enabled.
    • Disable I/O redirection by unchecking Redirect Output in the debugging settings.
  3. Run the Debugger:
    • Start debugging by pressing F5.
    • If a crash occurs, Visual Studio will provide detailed information, including stack traces and variable states.

Run Exe

Debugging on Linux/macOS

On Linux and macOS, you can use GDB or LLDB for debugging. While these tools can be used externally, integrating them with an IDE provides a better debugging experience.

Using GDB/LLDB Externally

  1. Start the Game with GDB/LLDB:
    gdb ./cs2
    

    or
    lldb ./cs2
    
  2. Set Breakpoints and Run:
    • Set breakpoints using break <function_name>.
    • Run the game using run.
  3. Inspect Crashes:
    • If the game crashes, use bt (backtrace) to inspect the stack trace.

Using an IDE

  • Visual Studio (via Wine): Run Visual Studio using Wine for a familiar debugging experience.
  • CLion: Use CLion with native Linux debugging support. CLion integrates with GDB/LLDB for a seamless debugging experience.

Debugging Plugify and Plugins

Most issues you encounter will likely be related to plugins or language modules rather than Plugify itself. Here’s how to debug these components:

  1. Build with Debug Symbols: As mentioned earlier, build the plugin or language module in Debug mode.
  2. Set Breakpoints: Use your debugger to set breakpoints in the plugin or module code.
  3. Inspect Logs: Plugify provides detailed logs that can help identify issues. Enable verbose logging if necessary.
  4. Reproduce the Issue: Run the game or application with the debugger attached to reproduce the issue.

Common Debugging Scenarios

1. Crashes in Plugins

  • Cause: Invalid memory access, null pointers, or unhandled exceptions.
  • Solution: Use the debugger to inspect the stack trace and identify the problematic code.

2. Performance Issues

  • Cause: Inefficient code or resource leaks.
  • Solution: Use profiling tools (e.g., Valgrind on Linux) to identify bottlenecks.

3. Inter-Language Communication Issues

  • Cause: Incorrect type conversion or marshalling.
  • Solution: Verify that the exported functions and their parameters are correctly defined and handled.

Tips for Effective Debugging

  • Enable Verbose Logging: Plugify’s logs can provide valuable insights into what’s happening under the hood.
  • Use Breakpoints Strategically: Set breakpoints in critical areas of your code to inspect variable states and execution flow.
  • Reproduce the Issue: Try to isolate the issue by creating a minimal reproducible example.
  • Ask for Help: If you’re stuck, reach out to the community on Discord or open an issue on GitHub.

Conclusion

Debugging Plugify-related issues requires building modules and plugins with debugging symbols and using the right tools for your platform. On Windows, Visual Studio is the most efficient debugger, while on Linux/macOS, GDB or LLDB are the tools of choice. By following the steps and tips outlined in this guide, you can effectively diagnose and resolve issues in Plugify, plugins, or games like CS2.