Determining if a .NET Assembly is compiled debug or not

Published 14 June 06 10:30 PM | jamesbl 

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,

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# hasani said on June 14, 2006 6:47 PM:
excellent post!
# Toddca said on June 14, 2006 8:25 PM:
Good post dude!
# ????????????: debug ?????? release? « ???????? ???????????? ?????????????? said on November 9, 2007 1:07 PM:

PingBack from http://seregaborzov.wordpress.com/2007/11/08/assembly-release-or-debug/

# ANY said on September 11, 2008 4:17 AM:

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());

           }

    }

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker