如何识别 DLL 是调试版本还是发布版本(在 .NET 中)

发布于 2024-07-17 10:53:12 字数 320 浏览 6 评论 0原文

可能的重复:
如何判断是否.NET 应用程序是在 DEBUG 或 RELEASE 模式下编译的吗?

我确信之前已经有人问过这个问题,但是 google 和 SO 搜索让我失败了。

如何识别 DLL 是发布版本还是调试版本?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-07-24 10:53:12

恕我直言,上述应用程序确实具有误导性; 它只查找 IsJITTrackingEnabled,它完全独立于代码是否经过编译以进行优化和 JIT 优化。

如果您在发布模式下编译并选择 DebugOutput 为“none”以外的任何值,则 DebuggableAttribute 会出现。

您还需要准确定义“调试”与“发布”的含义...

您的意思是应用程序配置了代码优化吗?
您的意思是可以将 VS/JIT 调试器附加到它吗?
你的意思是它生成 DebugOutput 吗?
您的意思是它定义了 DEBUG 常量吗? 请记住,您可以使用 System.Diagnostics.Conditional() 属性有条件地编译方法。

恕我直言,当有人问程序集是否是“调试”或“发布”时,他们的真正意思是代码是否经过优化……

那么,您想手动还是以编程方式执行此操作?

手动
您需要查看程序集元数据的 DebuggableAttribute 位掩码的值。 操作方法如下:

  1. 在 ILDASM 中打开程序集
  2. 打开清单
  3. 查看 DebuggableAttribute 位掩码。 如果 DebuggableAttribute 不存在,则它肯定是一个优化的程序集。
  4. 如果存在,请查看第 4 个字节 - 如果它是“0”,则它是 JIT 优化的 - 其他任何字节都不是:

// 元数据版本:v4.0.30319 .... // .custom 实例 void
[mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype
[mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = (
01 00 02 00 00 00 00 00 )

以编程方式:假设您想以编程方式了解代码是否经过 JITOptimized,这是正确的实现(在简单的控制台应用程序中) :

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}");
}

我在我的博客上提供了此实现:

如何判断程序集是调试还是发布

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(
quot;{nameof(HasDebuggableAttribute)}: {HasDebuggableAttribute}");
    Console.WriteLine(
quot;{nameof(IsJITOptimized)}: {IsJITOptimized}");
    Console.WriteLine(
quot;{nameof(IsJITTrackingEnabled)}: {IsJITTrackingEnabled}");
    Console.WriteLine(
quot;{nameof(BuildType)}: {BuildType}");
    Console.WriteLine(
quot;{nameof(DebugOutput)}: {DebugOutput}");
}

I've provided this implementation on my blog at:

How to Tell if an Assembly is Debug or Release

以酷 2024-07-24 10:53:12

执行此操作的唯一最佳方法是检查已编译的程序集本身。 Rotem Bloom 在此处找到了一个非常有用的工具,名为“.NET Assembly Information”。 安装后,它会将自身与 .dll 文件关联起来以自行打开。 安装后,您只需双击程序集即可打开,它将为您提供程序集详细信息,如下面的屏幕截图所示。 在那里您可以确定它是否经过调试编译。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文