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:
- Use XBuild to compile the solution
- Use Mono.Cecil for each assembly you want to debug to generate the pdb file and to inject the DebuggableAttribute (see code below)
- Start your with XBuild compiled program
- 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});
.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. – Siviapdb2mdb some.dll
to convert the pdb to the mono format – Equality