Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#

How to Tell if an Assembly is Debug or Release

Rate me:
Please Sign up or sign in to vote.
4.92/5 (7 votes)
22 Jan 2016CPOL1 min read 18.9K   9  
How to tell if an assembly is optimized

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, do you want to do this manually or programmatically?

Manually

You need to view the value of the DebuggableAttribute bitmask for the assembly's metadata. Here's how to do it:

  1. Open the assembly in ILDASM
  2. Open the Manifest
  3. Look at the DebuggableAttribute bitmask. If the DebuggableAttribute is not present, it is definitely an Optimized assembly.
  4. If it is present, look at the 4th byte - if it is a '0', it is JIT Optimized - anything else, it is not:

// Metadata version: v4.0.30319

....

// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype // [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )

Programmatically

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

C#
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) !=
                            DebuggableAttribute.DebuggingModes.None
                            ? "Full" : "pdb-only";
    }
}
else
{
    IsJITOptimized = true;
    BuildType = "Release";
}

This article was originally posted at http://dave-black.blogspot.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Black Box Solutions, Inc.
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --