在调试器下运行时更改程序流

OrE*_*lse 16 .net debugging cracking in-memory

有没有办法检测调试器是否在内存中运行?

这里是Form Load伪代码.

if debugger.IsRunning then
Application.exit
end if
Run Code Online (Sandbox Code Playgroud)

编辑:原始标题是"检测内存调试器"

Jar*_*Par 33

请尝试以下方法

if ( System.Diagnostics.Debugger.IsAttached ) {
  ...
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*ell 5

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

  1. 我使用调试器从商业.NET应用程序中提取崩溃跟踪并将其发送到随后修复的公司,感谢您使其变得简单并且
  2. 这项检查可以轻而易举地失败.

现在,为了更有用,下面是如何使用此检测来保持调试器中的函数更改程序状态,如果由于性能原因而有缓存的延迟评估属性.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有时也使用这种变体来确保我的调试器步骤不会跳过评估:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)