是否可以在Windows上使用Visual Studio调试使用Mono/XBuild编译的程序集?

Dre*_*sel 8 debugging mono xbuild visual-studio

我正在使用XBuild为Mono编译Visual Studio解决方案.这会生成程序集+ mdb文件.是否有可能在Windows上使用Visual Studio调试此程序集?使用"附加到进程"时,我无法调试,因为显示错误,表明未加载符号.

我尝试通过Mono.Cecil(AssemblyDefinition,MdbReaderProvider,PdbWriterProvider)为此程序集生成pdb文件,并通过Debug/Windows/Modules和"Load Symbol From/Symbol Path"手动加载它,它实际上加载了符号(显示在模块中) Windows),但也没有启用调试.

Dre*_*sel 6

在比较VS2012构建和XBuild构建之间的程序集定义时,我注意到XBuild没有生成DebuggableAttribute.如果缺少此属性,则无法使用Visual Studio 2012进行调试,即使手动加载符号也是如此.需要以下步骤来调试使用VS2012的Mono/XBuild编译的程序集:

  1. 使用XBuild编译解决方案
  2. 对要调试的每个程序集使用Mono.Cecil生成pdb文件并注入DebuggableAttribute(请参阅下面的代码)
  3. 启动XBuild编译程序
  4. 使用VS2012中的"Debug/Attach to process ..."来调试正在运行的程序

生成pdb和注入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});
Run Code Online (Sandbox Code Playgroud)