Debugging on embedded Mono

Following up on a previous post that showed how to embed Mono in a C++ application, we will see how it is possible to debug C# code that runs on the host application, using Xamarin Studio (or MonoDevelop).

Enabling debugging

The following snippet shows how to enable debugging on the Mono runtime:

In essence, the line  mono_debug_init(MONO_DEBUG_FORMAT_MONO); is what enables debugging. It must be inserted before the mono_jit_init call. If you run your application using the above code you will get:

That is because the debugger is not listening to the aforementioned address and port. You have to start the debugger prior to running your application.

Starting the Debugger

First of all, add an environment variable named MONODEVELOP_SDB_TEST with value Y. This is needed in order to enable the menu entry Custom Command Mono Soft Debugger, which we will use to launch the debugger.

Next, we have to generate the .mdb file for our assembly, which contains the debug symbols that the debugger uses to map a method to a position in a source file, and to get line numbers in stack traces. This step is needed only on Windows, since .mdb files are generated automatically on Linux systems. When you do a debug build of a C# project, a .pdb file is generated for the assembly. We have to convert this .pdb file to a .mdb one. Mono provides a tool for this, naturally called pdb2mdb, located in the lib\mono\4.5 folder of the Mono installation. For our convenience, we will add a custom build on for our project, that will execute pdb2mdb on every debug build:

custom build step
Adding a custom build step

We are basically executing pdb2mdb on the target of the project and storing the .mdb file on the target’s path.

pdb2mdb has a dependency to the Mono.Cecil assembly which is not included in the Mono installation.

The next step is to actually launch the debugger. This is done from the menu item:

Run → Run With → Custom Command Mono Soft Debugger

launch debugger
Launching the Debugger

Use the same address and port that you used in the C++ code and hit Listen.

If your assembly is compiled to a library, the Run menu item is not available in Xamarin Studio. To overcome this, add a Custom Command in Project Options → Run → Custom Commands and the debugger menu items will show up.

Now that the debugger is listening to the provided address and port, we can start our C++ application. The program’s execution will now be paused on any breakpoints you set.

4 Replies to “Debugging on embedded Mono”

  1. First of all, thanks for the great article !
    But I have some problems with Xamarin Studio. There is no “Soft Debugger” menu item. I have already followed up your post about adding custom commands. “Run with” menu item showed up, But there is only sub menu items for “Custom Parameters…” and “Edit Custom Modes…”.
    Do I have to get MonoDevelop instead of Xamarin Studio ?

    1. Hi juhl,

      Thanks for your feedback. The environment variable MONODEVELOP_SDB_TEST is probably missing from your system. You have to add it with a value of ‘1’ or ‘Y’ (without quotes). This works fine for both MonoDevelop and Xamaring Studio.

  2. Hi,

    Thanks, It’s great, I can do this by Xamarin Studio, but is there any way i can do this by VS2015 with Xamarin , need some VSExtension?

  3. Hi,

    Is it possible to debug both managed and native code ?

    I am developping with a Visual Studio 2015 extension that remotely compile and run C++ code, I have configured it to also upload my assemblies, application runs fine and native debugger runs fine too (I can debug the native part), but I would like to debug managed part, without having to do more than “F5” to run the debugger. So I wonder if it is possible to tell mono to “use the already attached debugger”. The VS extension uses GDB for remote debugging.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *