【问题标题】:Changing the program flow when running under a debugger在调试器下运行时更改程序流程
【发布时间】:2010-11-22 18:15:13
【问题描述】:

有什么方法可以检测到调试器是否在内存中运行?

这里是表单加载伪代码。

if debugger.IsRunning then
Application.exit
end if

编辑:原标题是“Detecting an in memory debugger”

【问题讨论】:

  • 大多数调试器可以在运行时附加到进程。在这种情况下,在 statrup 上检查调试器不会有太大帮助。

标签: .net debugging in-memory cracking


【解决方案1】:

试试下面的

if ( System.Diagnostics.Debugger.IsAttached ) {
  ...
}

【讨论】:

    【解决方案2】:

    在使用它关闭在调试器中运行的应用程序之前要记住两件事:

    1. 我使用调试器从商业 .NET 应用程序中提取崩溃跟踪,并将其发送给公司,随后该公司得到了修复,并感谢您的帮助和
    2. 该检查可以简单地被击败。

    现在,为了更有用,这里是如何使用此检测来防止调试器中的func eval 更改您的程序状态,如果您有缓存,则出于性能原因延迟评估的属性。

    private object _calculatedProperty;
    
    public object SomeCalculatedProperty
    {
        get
        {
            if (_calculatedProperty == null)
            {
                object property = /*calculate property*/;
                if (System.Diagnostics.Debugger.IsAttached)
                    return property;
    
                _calculatedProperty = property;
            }
    
            return _calculatedProperty;
        }
    }
    

    我有时也使用此变体来确保我的调试器逐步执行不会跳过评估:

    private object _calculatedProperty;
    
    public object SomeCalculatedProperty
    {
        get
        {
            bool debuggerAttached = System.Diagnostics.Debugger.IsAttached;
    
            if (_calculatedProperty == null || debuggerAttached)
            {
                object property = /*calculate property*/;
                if (debuggerAttached)
                    return property;
    
                _calculatedProperty = property;
            }
    
            return _calculatedProperty;
        }
    }
    

    【讨论】:

    • 这是一个很酷的想法 - 但它会在调试器下运行时改变程序的流程,因此您不再调试在发布中使用的代码。恕我直言,在大多数情况下,最好提供一个非缓存的属性变体(在 #if DEBUG 内部,因此它不是内置于发行版中),您可以在调试器中使用它来检查值,让“真实”属性工作在调试和发布版本中使用相同的方式。
    • @Jason:是和不是。在这种情况下,为评估属性而调用的所有方法都是纯方法(无论何时调用都没有副作用),因此我实际上确保从应用程序的角度来看,这也适用于属性。
    • 如果您想创建一个仅在 Visual Studio 的调试模式下工作的库,您认为这种方法是否有效?我想创建一个库,可以从 Visual Studio 中免费测试,但不能包含在基于发布模式构建的应用程序中
    猜你喜欢
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多