How to identify if the DLL is Debug or Release build (in .NET) [duplicate]
Asked Answered
K

2

120

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?

Kieger answered 28/4, 2009 at 17:13 Comment(5)
This blog post has the programmatic approach.Hilliard
A Link to another SO question on the same topic.Capitulation
similars questions in Stackoverflow, one question, and many, many different answers: #654950 #799471 #195116 #51400 #890959Apron
To add my 2 cents as well - I blogged about this previously and include the various compile options: completedevelopment.blogspot.com/2009/07/…Ellingston
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.Sumac
C
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.

Consueloconsuetude answered 28/4, 2009 at 17:15 Comment(4)
It supports .net frameworks < 4 ?Prognosticate
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.Olsewski
not working for win 10, failed to load "Microsoft.Owin.*"Labrador
@Labrador 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.Inexplicable
D
125

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

Derryberry answered 15/3, 2011 at 18:55 Comment(9)
the tool at assemblyinformation.codeplex.com has been updated, you may wish to revise your answerDelimitate
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.Middleclass
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?Margarine
@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(..)Derryberry
does not work in dotnet core... both release and debug result same output: HasDebuggableAttribute True; IsJITOptimized False; BuildType Debug; DebugOutput FullAlto
@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-onlyDerryberry
How to use ILSpy to see DebuggableAttribute bitmask?Babble
@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.Derryberry
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")].Pallas
C
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.

Consueloconsuetude answered 28/4, 2009 at 17:15 Comment(4)
It supports .net frameworks < 4 ?Prognosticate
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.Olsewski
not working for win 10, failed to load "Microsoft.Owin.*"Labrador
@Labrador 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.Inexplicable

© 2022 - 2024 — McMap. All rights reserved.