Determining if a .NET Assembly is compiled debug or not
Often times, in debugging various ASP.NET memory related issues, I find numerous assemblies that have been compiled in debug mode. One of the main problems, however, is the person I'm working with is almost always the one responsible for getting the server back up and running, and not the developer that created the debug assembly that's actually deployed to the server in question.
So I whipped up a little console application that returns the details on an assembly that gets passed on the command line.
using System;
using System.Reflection;
using System.Diagnostics;
namespace IsModDebug
{
public class IsDbg
// Errorlevels, for batch reasons:
// 0 = Non-Debug Assembly
// 1 = Debug Assembly
// 2 = No assembly name passed on command line
// 4 = Exception thrown
{
public static int Main(String[] args)
{
if (args.Length != 1)
{
Console.WriteLine("\nError: Not enough command line options specified.");
Console.WriteLine("Usage: \"IsModDebug <path to assembly>\"");
return (2);
}
try
{
Assembly asm = Assembly.LoadFile(args[0], null);
DebuggableAttribute objDebuggable =
(DebuggableAttribute)DebuggableAttribute.GetCustomAttribute(asm, typeof(DebuggableAttribute));
if (objDebuggable == null)
{
Console.WriteLine("Non-Debug Assembly");
return (0);
}
if (objDebuggable.IsJITOptimizerDisabled || objDebuggable.IsJITTrackingEnabled)
{
Console.WriteLine("Debug Assembly");
Console.WriteLine("JITOptimizerDisabled = {0}", objDebuggable.IsJITOptimizerDisabled);
Console.WriteLine("IsJITTrackingEnabled = {0}", objDebuggable.IsJITTrackingEnabled);
}
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}!!!", e.Message);
return (4);
}
return (1); // Debug Module Found
}
}
}
Output looks something like this, when run against various assemblies compiled in various ways:
C:\>ismoddebug d:\rel.exe
Non-Debug Assembly
C:\>ismoddebug d:\deb.exe
Debug Assembly
JITOptimizerDisabled = True
IsJITTrackingEnabled = True
C:\>ismoddebug d:\deb2.exe
Debug Assembly
JITOptimizerDisabled = False
IsJITTrackingEnabled = True
Have Fun,
Comments
Anonymous
June 14, 2006
excellent post!Anonymous
June 14, 2006
Good post dude!Anonymous
November 09, 2007
PingBack from http://seregaborzov.wordpress.com/2007/11/08/assembly-release-or-debug/Anonymous
September 10, 2008
Thanks for the example :) I would suggest to put Console.WriteLine("Debug Assembly"); outside of "if". e.g.: if (objDebuggable == null) { Console.WriteLine("Non-Debug Assembly"); return (0); } else { Console.WriteLine("Debug Assembly"); Console.WriteLine("JITOptimizerDisabled = {0}", objDebuggable.IsJITOptimizerDisabled.ToString()); Console.WriteLine("IsJITTrackingEnabled = {0}", objDebuggable.IsJITTrackingEnabled.ToString()); Console.WriteLine("DebuggingFlags = {0}", objDebuggable.DebuggingFlags.ToString()); } }