Skip to content

Debugging

What's Debugging?

Debugging is a process that allows you to detect and fix the errors on your code.

In .NET world debugging means running your .NET code in debug mode, attaching a .NET debugger on it, setting breakpoints, and following the code execution flow.

A .NET program can run in two different modes:

  • Release: This is the mode which you will use in production environment, it enables all kinds of compiler and JIT optimization that makes your code run faster with less memory footprint
  • Debug: The debug mode is used in development/testing environment, it disables the compiler and JIT optimizations and allows you to attach .NET debuggers to your program

What's a .NET debugger?

.NET debuggers are programs that uses runtime API that allows controlling the execution flow of another .NET process, they can pause execution for a short period of time, and you can check all kinds of data related to the program while it's paused like call stack, variable values, and much more.

How to build a cBots/Indicators in Debug mode?

To debug a cBot or an indicator, first you have to build it in debug mode, by default when you click on a cTrader cBot/Indicator "Build" button it builds that cBot/Indicator in release mode not debug mode.

Building in release mode is good default option because it allows the compiler to optimize your code.

For building your cBot/Indicator in debug mode you can use Dotnet CLI or IDEs like Visual Studio, for now let's focus on using Visual Studio for debugging but you can replicate the same process on other IDEs.

Install latest Visual Studio version on your system if you haven't already, then follow:

1. Open your indicator/cBot with Visual Studio by selecting "Edit in Visual Studio" option from context menu:

Image title

2. Then select the Debug mode for building your indicator/cBot in Visual studio:

Image title

3. Now build your indicator/cBot by using Visual studio:

Image title

Your cBot/Indicator is now built in debug mode.

Debugging a cBot/Indicator

Now that you know how to build a cBot/Indicator in debug mode let's focus on how to debug it.

First change your indicator/cBot access rights to FullAccess.

Then for debugging you have to attach a debugger to your indicator/cBot running process, you can do it 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");
    }    
}

You have to call it from cBot OnStart method or indicator Initialize method.

After Calling the System.Diagnostics.Debugger Launch method you will see a dialog pop-up that allows you to select the .NET debugger you want to use for debugging.

If your Indicator/cBot is already open on Visual Studio then select the instance that belongs to your Indicator/cBot, otherwise select new instance and it will open a new Visual Studio instance.

Once debugging started you can set breakpoints, step into lines of code, 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 DEBUG Preprocessor Directive

You can use the preprocessor directive in your cBot/Indicator code to control the compilation of your code.

For debugging the #if DEBUG is very useful, if you want to run a block or line of code only in debug mode you can use it, example:

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 above example the "Not Debugging" code line will not be compiled and no IL will be emitted for it if you build it in Debug mode.

For more please check C# preprocessor directives documentation.


Last update: July 1, 2022

Comments