使用 Visual Studio 调试器在值更改时中断

发布于 2024-07-05 19:34:11 字数 160 浏览 17 评论 0原文

有没有办法在变量上放置监视,并且仅在该值发生变化时让 Visual Studio 中断?

这将使发现棘手的国家问题变得更加容易。

这可以做到吗?

断点条件仍然需要设置断点,我宁愿设置一个监视并让 Visual Studio 在状态更改时设置断点。

Is there a way to place a watch on variable and only have Visual Studio break when that value changes?

It would make it so much easier to find tricky state issues.

Can this be done?

Breakpoint conditions still need a breakpoint set, and I'd rather set a watch and let Visual Studio set the breakpoints at state changes.

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

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

发布评论

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

评论(14

时光与爱终年不遇 2024-07-12 19:34:12

您可以选择重载变量的 = 运算符,并可以在特定条件下将断点放置在重载函数内。

You can optionally overload the = operator for the variable and can put the breakpoint inside the overloaded function on specific condition.

冷月断魂刀 2024-07-12 19:34:12

您可以在想要的位置放置一个断点,然后将鼠标悬停在其上,单击“设置”在此处输入图像描述

您可以在其中添加条件,包括窗帘值更改:
输入图片description here

设置条件后,断点图标上会出现“+”。 您还可以通过拖动来移动此条件

You can place a breakpoint where you want than hover a mouse over it, click "settings"enter image description here

There you can add conditions, including curtain value changes:
enter image description here

After conditions are set, the breakpoint will get "+" in icon. You also can move this conditions by dragging it

醉态萌生 2024-07-12 19:34:12

右键单击断点对我来说效果很好(尽管我主要将其用于特定变量值的条件断点。甚至在涉及线程名称的表达式上进行中断也可以,如果您试图发现线程问题,这非常有用)。

Right click on the breakpoint works fine for me (though mostly I am using it for conditional breakpoints on specific variable values. Even breaking on expressions involving a thread name works which is very useful if you're trying to spot threading issues).

西瑶 2024-07-12 19:34:12

正如彼得·莫滕森所写:

在 Visual Studio 2005 菜单中:

调试-> 新断点 -> 新数据断点

输入:&myVariable

附加信息:

显然,系统必须知道要监视内存中的哪个地址。
所以
- 在myVariable(或myClass.m_Variable)初始化时设置一个普通断点
- 运行系统并等待它在该断点处停止。
- 现在菜单项已启用,您可以通过输入 &myVariable 来查看变量,
或通过输入 &myClass.m_Variable 来获取实例。 现在地址已经明确定义了。

抱歉,当我解释已经给出的解决方案时做错了事情。 但我无法添加评论,并且已经有一些对此的评论。

As Peter Mortensen wrote:

In the Visual Studio 2005 menu:

Debug -> New Breakpoint -> New Data Breakpoint

Enter: &myVariable

Additional information:

Obviously, the system must know which address in memory to watch.
So
- set a normal breakpoint to the initialisation of myVariable (or myClass.m_Variable)
- run the system and wait till it stops at that breakpoint.
- Now the Menu entry is enabled, and you can watch the variable by entering &myVariable,
or the instance by entering &myClass.m_Variable. Now the addresses are well defined.

Sorry when I did things wrong by explaining an already given solution. But I could not add a comment, and there has been some comments regarding this.

梦醒时光 2024-07-12 19:34:12

您可以在非托管代码中使用内存观察点。 但不确定这些是否在托管代码中可用。

You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.

吾性傲以野 2024-07-12 19:34:12

您可能可以巧妙地利用 DebugBreak( ) 函数。

You can probably make a clever use of the DebugBreak() function.

树深时见影 2024-07-12 19:34:11

2019 年更新

现在,.Net Core 3.0 或更高版本的 Visual Studio 2019 Preview 2 正式支持此功能。 当然,您可能必须考虑使用 IDE 预览版的潜在风险。 我想在不久的将来这将包含在官方 Visual Studio 中。

https://blogs.msdn.microsoft.com/visualstudio/2019/02/12/break-when-value-changes-data-breakpoints-for-net-core-in-visual-工作室-2019/

幸运的是,数据断点不再是 C++ 独有的,因为它们现在可用于 Visual Studio 2019 Preview 2 中的 .NET Core(3.0 或更高版本)!

Update in 2019:

This is now officially supported in Visual Studio 2019 Preview 2 for .Net Core 3.0 or higher. Of course, you may have to put some thoughts in potential risks of using a Preview version of IDE. I imagine in the near future this will be included in the official Visual Studio.

https://blogs.msdn.microsoft.com/visualstudio/2019/02/12/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/

Fortunately, data breakpoints are no longer a C++ exclusive because they are now available for .NET Core (3.0 or higher) in Visual Studio 2019 Preview 2!

(り薆情海 2024-07-12 19:34:11

将变量更改为属性,并在 set 方法中添加断点。 例子:

private bool m_Var = false;
protected bool var
{
    get { 
        return m_var;
    }

    set { 
        m_var = value;
    }
}

Change the variable into a property and add a breakpoint in the set method. Example:

private bool m_Var = false;
protected bool var
{
    get { 
        return m_var;
    }

    set { 
        m_var = value;
    }
}
小兔几 2024-07-12 19:34:11

假设您有一个名为 A 的类,其声明如下。

class A  
{  
    public:  
        A();

    private:
        int m_value;
};

您希望程序在有人修改“m_value”的值时停止。

转到类定义并在 A 的构造函数中放置一个断点。

A::A()
{
    ... // set breakpoint here
}

一旦我们停止了程序:

Debug -> > 新断点 -> 新建数据断点...

地址:&(this->m_value)
字节数:4(因为 int 有 4 个字节)

现在,我们可以恢复程序。 当值更改时,调试器将停止。

您可以对继承类或复合类执行相同的操作。

class B
{
   private:
       A m_a;
};

地址:&(this->m_a.m_value)

如果您不知道要检查的变量的字节数,可以使用 sizeof 运算符。

例如:

// to know the size of the word processor,  
// if you want to inspect a pointer.
int wordTam = sizeof (void* ); 

如果您查看“调用堆栈”,您可以看到更改变量值的函数。

Imagine you have a class called A with the following declaration.

class A  
{  
    public:  
        A();

    private:
        int m_value;
};

You want the program to stop when someone modifies the value of "m_value".

Go to the class definition and put a breakpoint in the constructor of A.

A::A()
{
    ... // set breakpoint here
}

Once we stopped the program:

Debug -> New Breakpoint -> New Data Breakpoint ...

Address: &(this->m_value)
Byte Count: 4 (Because int has 4 bytes)

Now, we can resume the program. The debugger will stop when the value is changed.

You can do the same with inherited classes or compound classes.

class B
{
   private:
       A m_a;
};

Address: &(this->m_a.m_value)

If you don't know the number of bytes of the variable you want to inspect, you can use the sizeof operator.

For example:

// to know the size of the word processor,  
// if you want to inspect a pointer.
int wordTam = sizeof (void* ); 

If you look at the "Call stack" you can see the function that changed the value of the variable.

美胚控场 2024-07-12 19:34:11

Visual Studio 2015 中,您可以在自动实现的属性的 set 访问器上放置断点,并且当属性更新

public bool IsUpdated
{
    get;
    set;    //set breakpoint on this line
}

更新

或者; @AbdulRaufMujahid 在评论中指出,如果自动实现的属性在一行上,您可以将光标定位在 get;set; 上,然后点击 F9 并相应地放置一个断点。 好的!

public bool IsUpdated { get; set; }

In Visual Studio 2015, you can place a breakpoint on the set accessor of an Auto-Implemented Property and the debugger will break when the property is updated

public bool IsUpdated
{
    get;
    set;    //set breakpoint on this line
}

Update

Alternatively; @AbdulRaufMujahid has pointed out in the comments that if the auto implemented property is on a single line, you can position your cursor at the get; or set; and hit F9 and a breakpoint will be placed accordingly. Nice!

public bool IsUpdated { get; set; }
三生池水覆流年 2024-07-12 19:34:11

在 Visual Studio 2005 菜单中:

调试 -> 新断点 -> 新数据断点

输入:

&myVariable

In the Visual Studio 2005 menu:

Debug -> New Breakpoint -> New Data Breakpoint

Enter:

&myVariable
ゃ人海孤独症 2024-07-12 19:34:11

您还可以选择在代码中显式中断:

// Assuming C#
if (condition)
{
    System.Diagnostics.Debugger.Break();
}

来自 MSDN:

调试器.Break:
如果没有附加调试器,用户可以
询问他们是否想要附上
调试器。 如果是,则调试器是
开始了。 如果连接了调试器,
调试器收到用户信号
断点事件和调试器
只是暂停进程的执行
就像调试器断点已经
命中。

但这只是一个后备方案。 正如其他评论中所述,在 Visual Studio 中设置条件断点是更好的选择。

You can also choose to break explicitly in code:

// Assuming C#
if (condition)
{
    System.Diagnostics.Debugger.Break();
}

From MSDN:

Debugger.Break:
If no debugger is attached, users are
asked if they want to attach a
debugger. If yes, the debugger is
started. If a debugger is attached,
the debugger is signaled with a user
breakpoint event, and the debugger
suspends execution of the process just
as if a debugger breakpoint had been
hit.

This is only a fallback, though. Setting a conditional breakpoint in Visual Studio, as described in other comments, is a better choice.

岁月蹉跎了容颜 2024-07-12 19:34:11

我记得您使用 Visual Basic 6.0 描述它的方式。 在 Visual Studio 中,到目前为止我发现的唯一方法是指定 断点条件

I remember the way you described it using Visual Basic 6.0. In Visual Studio, the only way I have found so far is by specifying a breakpoint condition.

秋日私语 2024-07-12 19:34:11

如果您使用 WPF,有一个很棒的工具:WPF Inspector
它将自身附加到 WPF 应用程序并显示包含所有属性的完整控件树,它允许您(除其他外)在任何属性更改时中断。

但遗憾的是,我没有找到任何工具可以让您对任何属性或变量执行相同的操作。

If you are using WPF, there is an awesome tool : WPF Inspector.
It attaches itself to a WPF app and display the full tree of controls with the all properties, an it allows you (amongst other things) to break on any property change.

But sadly I didn't find any tool that would allow you to do the same with ANY property or variable.

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