The Black Box of .NET Headline Animator

The Black Box of .NET

Thursday, December 8, 2011

How to Tell if an Assembly is Debug or Release

The DebuggableAttribute is present if you compile in any setting for 'Debug' mode and when Release mode is selected and Debug Info set to anything other than "none". So, just looking for the presence of DebuggableAttribute is not sufficient and could be misleading. So, you could still have an assembly that is JIT optimized where the DebuggableAttribute is present in the Assembly Manifest.

First, you need to define exactly what is meant by "Debug" vs. "Release"...
  • Do you mean that the app is configured with code optimization?
  • Do you mean that you can attach the VS/JIT Debugger to it?
  • Do you mean that it generates DebugOutput?
  • Do you mean that it defines the DEBUG constant?  Remember that you can conditionally compile Methods with the System.Diagnostics.Conditional() attribute.
IMHO, when someone asks whether or not an assembly is "Debug" or "Release", they really mean if the code is optimized...

Sooo, assuming that you want to know programmatically if the code is JITOptimized, here is the correct implementation:

object[] attribs = ReflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

// If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
if (attribs.Length > 0)
    // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
    // it's a DEBUG build; we have to check the JIT Optimization flag
    // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
    DebuggableAttribute debuggableAttribute = attribs[0] as DebuggableAttribute;
    if (debuggableAttribute != null)
        HasDebuggableAttribute = true;
        IsJITOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
        BuildType = debuggableAttribute.IsJITOptimizerDisabled ? "Debug" : "Release";

        // check for Debug Output "full" or "pdb-only"
            DebugOutput = (debuggableAttribute.DebuggingFlags &
                            DebuggableAttribute.DebuggingModes.Default) !=
                            ? "Full" : "pdb-only";
    IsJITOptimized = true;
    BuildType = "Release";


1 comment: