118

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?

Community
  • 1
  • 1
dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • similars questions in Stackoverflow, one question, and many, many different answers: http://stackoverflow.com/questions/654450/programatically-detecting-release-debug-mode-net http://stackoverflow.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net http://stackoverflow.com/questions/194616/how-to-tell-if-net-app-was-compiled-in-debug-or-release-mode http://stackoverflow.com/questions/50900/best-way-to-detect-a-release-build-from-a-debug-build-net http://stackoverflow.com/questions/890459/asp-net-release-build-vs-debug-build – Kiquenet Feb 03 '11 at 19:52
  • To add my 2 cents as well - I blogged about this previously and include the various compile options: http://completedevelopment.blogspot.com/2009/07/determining-if-assembly-is-compiled-in.html – Adam Tuliper May 08 '11 at 19:17
  • [This blog post](http://jamesewelch.com/2007/08/30/how-to-tell-if-a-net-assembly-is-debug-or-release/) has the programmatic approach. – Promit Apr 28 '09 at 17:17
  • A [Link](http://stackoverflow.com/questions/629674/how-to-find-out-if-a-net-assembly-was-compiled-with-the-trace-or-debug-flag/629813#629813) to another SO question on the same topic. – Graeme Bradbury Apr 28 '09 at 17:18
  • One way that could work for most people is to simply open the DLL/EXE file with Notepad, and look for a path, for example search for "C:\" and you might find a path such as "C:\Source\myapp\obj\x64\Release\myapp.pdb", the "Release" shows that the build was done with Release configuration. – Shahin Dohan Sep 03 '20 at 12:32

2 Answers2

122

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

Dave Black
  • 7,305
  • 2
  • 52
  • 41
  • 2
    the tool at http://assemblyinformation.codeplex.com/ has been updated, you may wish to revise your answer – Tim Abell Dec 13 '11 at 11:31
  • 3
    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. – David Yates Apr 18 '13 at 16:00
  • 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? – h0r53 Apr 14 '16 at 02:08
  • 3
    @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(..) – Dave Black Apr 14 '16 at 16:59
  • 1
    does not work in dotnet core... both release and debug result same output: HasDebuggableAttribute True; IsJITOptimized False; BuildType Debug; DebugOutput Full – Sasha Bond May 27 '20 at 15:03
  • @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 – Dave Black Oct 30 '20 at 17:10
  • How to use ILSpy to see DebuggableAttribute bitmask? – huang Apr 23 '21 at 00:25
  • @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. – Dave Black Apr 23 '21 at 16:48
  • 1
    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")]`. – Randy Burden May 25 '21 at 16:31
95

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.

starball
  • 20,030
  • 7
  • 43
  • 238
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137