Debugging¶
What Is Debugging in cTrader Automate¶
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 to 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.
- Open your indicator/cBot in Visual Studio by selecting the 'Edit in Visual Studio' option that appears after right-clicking on an extension.
- In the menu above the code editor window, select 'Debug'.
- 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.
-
Change your indicator/cBot access rights to
FullAccess
. -
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 |
|
1 2 3 4 5 6 7 8 9 |
|
Please, note that the System.Diagnostics.Debugger.Launch()
method has to be called from the cBot OnStart()
method or the indicator Initialize()
method.
- 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'.
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, 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 |
|
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.