views:

2179

answers:

5

I have an application installed on my computer and I need to find out if it was compiled in DEBUG mode or not?

I've tried to use Reflector, but it does not show anything specific. Here is what I see: // Assembly APPLICATION_NAME, Version 8.0.0.15072 Location: C:\APPLICATION_FOLDER\APPLICATION_NAME.exe Name: APPLICATION_NAME, Version=8.0.0.15072, Culture=neutral, PublicKeyToken=null Type: Windows Application

+12  A: 

I blogged this a long time ago, and I don't know if it still valid or not, but the code is something like...

private void testfile(string file)
{
    if(isAssemblyDebugBuild(file))
    {
     MessageBox.Show(String.Format("{0} seems to be a debug build",file));
    }
    else
    {
     MessageBox.Show(String.Format("{0} seems to be a release build",file));
    }
}    

private bool isAssemblyDebugBuild(string filename)
{
    return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));    
}    

private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
    bool retVal = false;
    foreach(object att in assemb.GetCustomAttributes(false))
    {
     if(att.GetType() == System.Type.GetType("System.Diagnostics.DebuggableAttribute"))
     {
      retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
     }
    }
    return retVal;
}
ZombieSheep
This appears to work; though it does seem rather fragile.
Robert Taylor
As I said, it's a couple of years ago when I did the investigation work... Who knows what's changed in the framework, or even how poor a job I did originally. ;)
ZombieSheep
+5  A: 

You're on the right path actually. If you look in the Disassembler window in reflector you will see the following line if it was built in debug mode:

[assembly: Debuggable(...)]
Joe Basirico
Not true; i just checked an assembly that i built in Release mode. It still had this attribute: [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
Robert Taylor
I compiled in release mode and i saw this in reflector:[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
chikak
When I disassemble an assembly in Debug Mode, I see this: [assembly: Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.Default)] When I build it in Release Mode, the debuggable attribute only has the IgnoreSymbolStoreSequencePoints flag, as you all mention.
Alric
A: 

thanks guys. it helped!

If zombiesheeps answer helped and is correct, you should perhaps accept it as the answer, that then pins it to the top (helps other people to quickly find the correct answer)
Tim Jarvis
Which one is the accepted answer? Did you figure it out based on all the answers? How did you solve it?Thanks
Rismo
Please mark the correct answer as accepted.
Zaagmans
+1  A: 

How about using Jeff Key's IsDebug utility? It is a little dated, but since you have Reflector you can decompile it and recompile it in any version of the framework. I did.

flipdoubt
+1, but there's no need to decompile the utility - the author provides the source code:http://www.sliver.com/Downloads/IsDebugSource.zip
Luke Girvin
+1  A: 

i just looked at the MainForm.cs from the utility.. because i would need sth like that:

... Cursor = Cursors.WaitCursor;

   Assembly assm = Assembly.LoadFrom(filename);
   bool found = assm.GetCustomAttributes(typeof(DebuggableAttribute), false).Length > 0;
   buildType = found ? "Debug" : "Release";

...

-> this would only depend on the existence of the DebuggableAttribute.. thus it don´t work correct.

regards, k.