Is it possible to debug assemblies compiled with Mono / XBuild with Visual Studio on Windows?
Asked Answered
S

2

8

I'm using XBuild to compile Visual Studio solutions for Mono. This generates the assembly + mdb file. Is there a possibility to debug this assembly with Visual Studio on Windows? When using "Attach to process" i can't debug because an error is shown that the symbols aren't loaded.

I tried generating the pdb file for this assembly via Mono.Cecil (AssemblyDefinition, MdbReaderProvider, PdbWriterProvider) and loading it manually via Debug / Windows / Modules and "Load Symbol From / Symbol Path", which actually loads the symbols (shown in the Modules windows) but that doesn't enable debugging either.

Stodge answered 21/11, 2012 at 18:6 Comment(3)
I do not believe this is possible (but I'd love to be proven wrong).Cloddish
When I created Mono's .mdb file format many years ago, PDB was undocumented and proprietary, so we couldn't just simply use that. And AFAIK, this is still true today (at least Wikipedia says so). There may be some tools out there to write these files, but they can't generate fully "correct" PDBs unless they're either written by Microsoft or they publish the file format. However, I'd also love to be proven wrong about this.Sivia
FYI if you are doing the opposite, use pdb2mdb some.dll to convert the pdb to the mono formatEquality
S
6

When comparing assembly definitions between VS2012 builds and XBuild builds, i noticed that XBuild is not generating the DebuggableAttribute. If this attribute is missing, debugging with Visual Studio 2012 isn't possible, even if you load the symbols manually. Following steps are needed to debug assemblies compiled with Mono / XBuild with VS2012:

  1. Use XBuild to compile the solution
  2. Use Mono.Cecil for each assembly you want to debug to generate the pdb file and to inject the DebuggableAttribute (see code below)
  3. Start your with XBuild compiled program
  4. Use "Debug / Attach to process..." from VS2012 to debug the running program

Code for generating pdb and injecting DebuggableAttribute:

string assemblyPath = @"HelloWorld.exe";

var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyPath,
    new ReaderParameters() { SymbolReaderProvider = new MdbReaderProvider(), ReadSymbols = true});

CustomAttribute debuggableAttribute = newCustomAttribute(
assemblyDefinition.MainModule.Import(
    typeof(DebuggableAttribute).GetConstructor(new[] { typeof(bool), typeof(bool) })));

debuggableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(
    assemblyDefinition.MainModule.Import(typeof(bool)), true));

debuggableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(
    assemblyDefinition.MainModule.Import(typeof(bool)), true));

assemblyDefinition.CustomAttributes.Add(debuggableAttribute);

assemblyDefinition.Write(assemblyPath,
    new WriterParameters() { SymbolWriterProvider = new PdbWriterProvider(), WriteSymbols = true});
Stodge answered 22/11, 2012 at 18:41 Comment(0)
J
4

This is possible with a little one-time effort.

You need to convert the mono mdb files to pdb files. After that VS should be able to step through the code with you (if you have the sources too) - see below.

http://johnhmarks.wordpress.com/2011/01/19/getting-mono-cecil-to-rewrite-pdb-files-to-enable-debugging/

Mono.Cecil does change quite frequently so you may find the API has changed a little for this.

Joanniejoao answered 22/11, 2012 at 7:45 Comment(2)
I also found this post before asking and tried it (see second part of question - via AssemblyDefinition, MdbReaderProvider, PdbWriterProvider), maybe i did something wrong - but VS didn't load the symbols automatically and when i load them manually via Debug / Windows / Modules and "Load Symbol From / Symbol Path" nothing changed. I will post my Code snippet when i'm @ home.Stodge
You also have to add a DebuggableAttribute, see my answer below.Stodge

© 2022 - 2024 — McMap. All rights reserved.