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

kmbjn2e3  于 2022-12-27  发布在  .NET
关注(0)|答案(2)|浏览(125)
    • 此问题在此处已有答案**:

10年前关闭了。

    • 可能重复:**

How to tell if a .NET application was compiled in DEBUG or RELEASE mode?
我敢肯定,这已经问过,但谷歌和搜索等失败了我。
如何识别DLL是发布版本还是调试版本?

zu0ti5jz

zu0ti5jz1#

恕我直言,上述应用程序确实具有误导性;它只查找IsJITTrackingEnabled,IsJITTrackingEnabled完全独立于代码是否被编译用于优化和JIT优化。
如果在发布模式下编译并将DebugableOutput选择为"none"以外的任何值,则会出现DebuggableAttribute。
您还需要 * 确切地 * 定义"调试"与"发布"的含义...
您的意思是应用配置了代码优化吗?您的意思是您可以将VS/JIT调试器附加到它吗?您的意思是它生成DebugOutput吗?您的意思是它定义了DEBUG常量吗?请记住,您可以使用System.Diagnostics. Conditional()属性有条件地编译方法。
恕我直言,当有人问一个程序集是"调试"还是"发布"时,他们的真正意思是代码是否经过优化...
Sooo,您想手动还是编程地完成此操作?

    • 手动**:您需要查看程序集元数据的DebuggableAttribute位掩码的值。以下是具体操作方法:

1.在ILDASM中打开组件
1.打开清单
1.查看DebuggableAttribute位掩码。如果DebuggableAttribute不存在,则它肯定是优化的程序集。
1.如果它存在,请查看第4个字节-如果它是'0',则表示JIT优化-其他任何内容都不是:
//元数据版本:v4.0.30319 ....//.自定义示例void [mscorlib]系统.诊断.可调试属性::. ctor(值类型[mscorlib]系统.诊断.可调试属性/调试模式)=(01 00 020000 00 00)

    • 编程方式**:假设您想通过编程方式知道代码是否经过JIT优化,下面是正确的实现(在一个简单的控制台应用程序中):
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}");
}

我已经在我的博客上提供了这个实现:

    • 一个
htrmnn0y

htrmnn0y2#

唯一最好的方法是检查编译的程序集本身。有一个非常有用的工具叫做“.NET程序集信息”,它是Rotem Bloom在here上找到的。安装这个工具之后,它将自己与.dll文件相关联,以便自己打开。安装后,您只需双击程序集即可打开,它将为您提供程序集的详细信息,如下面的屏幕截图所示。在那里,您可以确定它是否经过调试编译。

相关问题