Using PDB (Debug Symbols) When Compiling in Release Mode

Bit by yet another not-so-well documented "feature" in Microsoft .Net.  We were compiling in release mode for QA, since we've seen other behavioral differences between debug and release.  We also wanted to emit the line numbers in the stack trace so the engineers would have a better idea of what caused the error.  We added the /degub:pdbonly option, but left the /optimize+.  We noticed that some of the errors when compared to the line numbers didn't make sense.  It turned out that most of the stack trace was wrong... or at least wrong when compared to the original source code.

If you want to emit debug symbols, ie. pdb files, when compiling in release mode, you have to disable optimizations.  Compiler optimizations will often reorganize instructions for the most efficient execution.  As a result, any stack trace will produce unexpected line numbers... you'll get the line number in the newly optimized code, not the line in your original source code.

Disabling Optimizations and Enabling pdb only

If compiling at the command line, or with nant:

/debug:pdbonly /optimize-
or simply
/debug:pdbonly

In Visual Studio:

Under project properties:
  1. Uncheck "Enable Optimization"
  2. In the "Advanced ..." settings, choose "pdb-only" under Output->Debug Info.




The statement in the Microsoft docs: "It is possible to combine the /optimize and /debug options." -- is not entirely accurate.

Based on Microsoft's documentation, I'm placing this in the "Just because you can, doesn't mean you should." category.

References:

Comments

Popular posts from this blog

Adding New Microsoft Extensions to Legacy WCF and ASMX Web Services

Using NHibernate in Asp.Net Core

Code Coverage for Multiple Projects in a Single Build using Dotnet Test and Coverlet