Skip to content

Source code debugging

TiberiumFusion edited this page Jul 27, 2020 · 3 revisions

Back to the Developing with Visual Studio overview article.


This section explains how to configure your plugin to break Terraria so that you can debug your code line-by-line with Visual Studio. These steps are IDE-agnostic, however, and will work with any IDE that can debug .NET applications.


Checklist

  1. Modify your plugin's code to start a debugger.
  2. Build your plugin in the Debug configuration and copy the DLL + PDB to your Plugins folder.
  3. Switch from Release to Debug mode in Terraria Tweaker 2.

Launching the debugger

To break Terraria, simply call System.Diagnostics.Debugger.Launch() somewhere in your plugin, such as before a section of code that you would like to step through.

Example:

public override void Initialize()
{
	System.Diagnostics.Debugger.Launch();
	/* code */
}

TIP: Ensure your plugin project is open in Visual Studio before you launch Terraria to test your plugin. That way, when the "pick a debugger" prompt appears, you can select the existing Visual Studio instance that already has your project loaded.

Configuring for a debug launch

In order for source code debugging to be available to your debugger, you must do the following:

  1. In Visual Studio, compile your plugin in debug mode.
  2. Copy the built DLL and PDB file to Terraria Tweaker 2's Plugins folder.
  3. In Terraria Tweaker 2, edit a Tweak List that has your plugin enabled. Open the Plugin Manager and change the Plugin Compile Configuration option to Debug.
    TIP: It might be helpful to create a special Tweak List that you only use for debugging plugins.
  4. Ensure your plugin is enabled, then save the Tweak List.
  5. When you launch the Tweak List, the plugin's PDB will be included & loaded by the plugin framework, thus allowing you to debug your plugin's source code in Visual Studio.

Troubleshooting

  • You might need to disable Just My Code (Tools -> Options -> Debugging), depending on what source code files are actually included in your Visual Studio project.
  • If Visual Studio fails to find your plugin's symbols, you may need to manually add the PDB file to a symbol cache folder.
    • You can view the loaded .NET modules and their symbol load status with the Modules window (Debug -> Windows -> Modules) when Visual Studio is in debugging mode.

Cleaning up

  • When you have resolved all issues in your plugin, don't forget to remove all calls to System.Diagnostics.Debugger.Launch(), especially if you plan to distribute your plugin. You will scare the user if Terraria suddenly freezes and the debugger prompt appears on their screen.
  • If you aren't using a special Tweak List that's only for debugging, make sure you switch the Plugin Compile Configuration setting for your Tweak List back to Release mode in Terraria Tweaker 2.