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

前端 未结 2 1027
旧时难觅i
旧时难觅i 2021-01-12 03:13

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 Wi

2条回答
  •  死守一世寂寞
    2021-01-12 03:46

    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});
    

提交回复
热议问题