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

发布于 2024-09-16 11:55:07 字数 169 浏览 16 评论 0原文

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

这是表单加载伪代码。

if debugger.IsRunning then
Application.exit
end if

编辑:原来的标题是“检测内存调试器”

Is there any way of detecting that a debugger is running in memory?

and here comes the on Form Load pseudocode.

if debugger.IsRunning then
Application.exit
end if

Edit: The original title was "Detecting an in memory debugger"

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

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

发布评论

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

评论(2

失退 2024-09-23 11:55:07

尝试以下操作

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

Try the following

if ( System.Diagnostics.Debugger.IsAttached ) {
  ...
}
甜点 2024-09-23 11:55:07

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

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

Two things to keep in mind before using this to close an application running in the debugger:

  1. I've used a debugger to pull a crash trace from a commercial .NET application and send it to the company where it was subsequently fixed with a thank you for making it easy and
  2. That check can be trivially defeated.

Now, to be of more use, here's how to use this detection to keep func eval in the debugger from changing your program state if you have a cache a lazily evaluated property for performance reasons.

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

I've also used this variant at times to ensure my debugger step-through doesn't skip the evaluation:

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