Skip to content

Debugging

What Is Debugging?

In brief, debugging is a process during which you can detect and fix the errors present in your code.

Any .NET program can run in two different modes.

  • Release mode. This is the mode typically used in production environments. When using it, .NET enables various compiler and just-in-time optimizations that improve the speed with which your code executes and reduce its memory footprint.
  • Debug mode. This mode is primarily used in development/testing environments. All optimizations are disabled, and you can attach .NET debuggers to your code.

When working with .NET libraries and applications, debugging entails the following.

  • Running yoir code in debug mode.
  • Attaching a .NET debugger to your code.
  • Setting code breakpoints to indicate whether the execution flow should pause.
  • Following the code execution flow.

What Is a .NET Debugger?

.NET debuggers are programs that use runtime APIs. These APIs, in turn, allow for controlling the execution flow of .NET processes. In other words, they can pause execution at given breakpoints and store various states (including variable values).

How Do I Build cBots/Indicators in Debug Mode?

As we have discussed above, debugging a cBot or an indicator entails building this extension in debug mode. By default, when you click on the 'Build' button in the cTrader UI, your extension is run in release mode to enable code optimizations.

To build your cBot/indicator in debug mode, you need to use either the .NET CLI or external IDEs such as Visual Studio. In this documentation, we show the debug process as it occurs in Visual Studio. However, this process can also be replicated in other IDEs with minor changes.

Install the latest version of Visual Studio on your machine and perform the following actions.

  1. Open your indicator/cBot in Visual Studio by selecting the 'Edit in Visual Studio' option that appears after right-clicking on an extension.

Image title

  1. In the menu above the code editor window, select 'Debug'.

Image title

  1. Build your cBot/indicator in debug mode.

Debugging a cBot/Indicator

Now that you know how to build a cBot/indicator in debug mode, we will focus on how to actually debug it.

  1. Change your indicator/cBot access rights to FullAccess.

  2. Attach a debugger to your indicator/cBot running process. You can do so by calling the System.Diagnostics.Debugger.Launch() method.

1
2
3
4
5
6
7
8
9
protected override void OnStart()
{
    var result = System.Diagnostics.Debugger.Launch();

    if (result is false)
    {
        Print("Debugger launch failed");
    }    
}
1
2
3
4
5
6
7
8
9
protected override void Initialize()
{
    var result = System.Diagnostics.Debugger.Launch();

    if (result is false)
    {
        Print("Debugger launch failed");
    }    
}

Please, note that the System.Diagnostics.Debugger.Launch() method has to be called from the cBot OnStart() method or the indicator Initialize() method.

  1. Run a new instance of your cBot/indicator. If an instance is already running, select it in the UI. You should be redirected to Visual Studio with a prompt to select a debugger. Do so and click on 'OK'.

Image title

Once debugging starts, you can set breakpoints, 'step into' lines of code, and see the call stack and variable values.

For a complete guide on how to debug in Visual Studio, please, check the Visual Studio debugging documentation.

Using the 'DEBUG' Preprocessor Directive

You can also use the DEBUG preprocessor directive in your cBot/indicator code to control the execution flow.

For instance, the #if DEBUG is useful when you want to run only a block or a line of code in debug mode.

1
2
3
4
5
6
7
8
9
protected override void OnStart()
{
#if DEBUG
    System.Diagnostics.Debugger.Launch();
    Print("Debugging");
#else
    Print("Not Debugging");
#endif
}

In the above example, the "Not Debugging" code line will not be compiled if you build your cBot in debug mode. Additional information about C# processor directives is available in this documentation.


Last update: November 25, 2022

Comments