How to identify if the DLL is Debug or Release build (in .NET)

79,751

Solution 1

The only best way to do this is to check the compiled assemblies itself. There is this very useful tool called '.NET Assembly Information' found here by Rotem Bloom. After you install this, it associates itself with .dll files to open with itself. After installing you can just double-click on the Assembly to open and it will give you the assembly details as displayed in the screenshots below. There you can identify if it's debug compiled or not.

Hope this helps..

Solution 2

IMHO, The above application is really misleading; it only looks for the IsJITTrackingEnabled which is completely independent of whether or not the code is compiled for optimization and JIT Optimization.

The DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none".

You also 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 (in a simple console app):

void Main()
{
    var HasDebuggableAttribute = false;
    var IsJITOptimized = false;
    var IsJITTrackingEnabled = false;
    var BuildType = "";
    var DebugOutput = "";
    
    var ReflectedAssembly = Assembly.LoadFile(@"path to the dll you are testing");
    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;
            
            // IsJITTrackingEnabled - Gets a value that indicates whether the runtime will track information during code generation for the debugger.
            IsJITTrackingEnabled = debuggableAttribute.IsJITTrackingEnabled;
            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";
    }

    Console.WriteLine($"{nameof(HasDebuggableAttribute)}: {HasDebuggableAttribute}");
    Console.WriteLine($"{nameof(IsJITOptimized)}: {IsJITOptimized}");
    Console.WriteLine($"{nameof(IsJITTrackingEnabled)}: {IsJITTrackingEnabled}");
    Console.WriteLine($"{nameof(BuildType)}: {BuildType}");
    Console.WriteLine($"{nameof(DebugOutput)}: {DebugOutput}");
}

I've provided this implementation on my blog at:

How to Tell if an Assembly is Debug or Release

Share:
79,751

Related videos on Youtube

Djonatas Tenfen
Author by

Djonatas Tenfen

SO Domination is so close! Ni hahahahahaha

Updated on July 08, 2022

Comments

  • Djonatas Tenfen
    Djonatas Tenfen almost 2 years

    Possible Duplicate:
    How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

    I'm sure this has been asked before, but google and SO search failed me.

    How can I identify if a DLL is a release build or debug build?

  • Rob Allen
    Rob Allen about 15 years
    Nice find. Thanks for the link.
  • this. __curious_geek
    this. __curious_geek about 15 years
    It has proven to be an exceptionally great tool in our development team. It's amazing!
  • Matt
    Matt over 12 years
    the tool at assemblyinformation.codeplex.com has been updated, you may wish to revise your answer
  • SUN Jiangong
    SUN Jiangong over 12 years
    It supports .net frameworks < 4 ?
  • David Yates
    David Yates about 11 years
    I would also note that now that you know what you are looking for (thanks to Mr. Black) there are tools like Dot Peek from JetBrains that will give you this information. With Dot Peek you double click on the assembly itself in the Assembly Explorer (not any files under the assembly) and it will show you all the Assembly attributes that he is checking for with his tool. The key two being the Debuggable attribute and IsJITOptimizerDisabled missing.
  • santosh kumar patro
    santosh kumar patro about 10 years
    Great tool. It resolve my issue. Thanks a lot for the help :)
  • h0r53
    h0r53 about 8 years
    Could you explain how you can use the programmatic approach on your DLLs? I don't see how I can get that code to open/find my DLL, unless I'm missing something. How does object[] attribs know which DLL to reference?
  • Dave Black
    Dave Black about 8 years
    @CaitLANJenner - this is a snippet from a tool I wrote that does WAY more than this. I'm considering placing on Git. Anyways, in the code above, replace "ReflectedAssembly" with an instance of the Assembly you want to examine. You can do this with Assembly.LoadFrom(...) or the older (deprecated) usages - Assembly.Load(..), Assembly.LoadFile(..) or Assembly.LoadWithPartialName(..)
  • DJ van Wyk
    DJ van Wyk about 8 years
    This tool must be up there with Lutz Roeder's .Net Reflector. Now I just need to find someone to convert it into a plugin for Total Commander's Lister tool.
  • user908645
    user908645 almost 8 years
    not working for win 10, failed to load "Microsoft.Owin.*"
  • Marc.2377
    Marc.2377 about 5 years
    @user908645 a more up-to-date fork can be found at github.com/tebjan/AssemblyInformation. I used the .msi installer. Working fine on my Win10 machine.
  • Sasha Bond
    Sasha Bond almost 4 years
    does not work in dotnet core... both release and debug result same output: HasDebuggableAttribute True; IsJITOptimized False; BuildType Debug; DebugOutput Full
  • Dave Black
    Dave Black over 3 years
    @SashaBond what doesn't work? The Manual method or the Programmatic method? Try the manual method on the assembly in question - it works for .NET Core. The code works for me on a .NET Core 3.1 console app compiled in 'Release' mode: HasDebuggableAttribute: True, IsJITOptimized: True, BuildType: Release, DebugOutput: pdb-only
  • Rodri
    Rodri over 3 years
    Nice tool!!! working.
  • Joe Huang
    Joe Huang about 3 years
    How to use ILSpy to see DebuggableAttribute bitmask?
  • Dave Black
    Dave Black about 3 years
    @JokeHuang - I'm not specifically familiar with ILSpy as I use ILDASM that comes with .NET. But the information is stored in the assembly's metadata. So wherever you are able to view Assembly Metadata in ILSpy is where you would look.
  • Randy Burden
    Randy Burden almost 3 years
    For a .NET Core 3.1 app, I loaded the DLL in JetBrains dotPeek (free decompiler tool), double-clicked on the assembly, and looked for the AssemblyConfiguration attribute. For Debug it displays [assembly: AssemblyConfiguration("Debug")] and for Release it displays [assembly: AssemblyConfiguration("Release")].