使用 Visual Studio 调试器在值更改时中断
有没有办法在变量上放置监视,并且仅在该值发生变化时让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以选择重载变量的 = 运算符,并可以在特定条件下将断点放置在重载函数内。
You can optionally overload the = operator for the variable and can put the breakpoint inside the overloaded function on specific condition.
您可以在想要的位置放置一个断点,然后将鼠标悬停在其上,单击“设置”
您可以在其中添加条件,包括窗帘值更改:
设置条件后,断点图标上会出现“+”。 您还可以通过拖动来移动此条件
You can place a breakpoint where you want than hover a mouse over it, click "settings"
There you can add conditions, including curtain value changes:
After conditions are set, the breakpoint will get "+" in icon. You also can move this conditions by dragging it
右键单击断点对我来说效果很好(尽管我主要将其用于特定变量值的条件断点。甚至在涉及线程名称的表达式上进行中断也可以,如果您试图发现线程问题,这非常有用)。
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).
正如彼得·莫滕森所写:
附加信息:
显然,系统必须知道要监视内存中的哪个地址。
所以
- 在
myVariable
(或myClass.m_Variable
)初始化时设置一个普通断点- 运行系统并等待它在该断点处停止。
- 现在菜单项已启用,您可以通过输入
&myVariable
来查看变量,或通过输入
&myClass.m_Variable
来获取实例。 现在地址已经明确定义了。抱歉,当我解释已经给出的解决方案时做错了事情。 但我无法添加评论,并且已经有一些对此的评论。
As Peter Mortensen wrote:
Additional information:
Obviously, the system must know which address in memory to watch.
So
- set a normal breakpoint to the initialisation of
myVariable
(ormyClass.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.
您可以在非托管代码中使用内存观察点。 但不确定这些是否在托管代码中可用。
You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.
您可能可以巧妙地利用 DebugBreak( ) 函数。
You can probably make a clever use of the DebugBreak() function.
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/
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/
将变量更改为属性,并在 set 方法中添加断点。 例子:
Change the variable into a property and add a breakpoint in the set method. Example:
假设您有一个名为 A 的类,其声明如下。
您希望程序在有人修改“m_value”的值时停止。
转到类定义并在 A 的构造函数中放置一个断点。
一旦我们停止了程序:
Debug -> > 新断点 -> 新建数据断点...
地址:&(this->m_value)
字节数:4(因为 int 有 4 个字节)
现在,我们可以恢复程序。 当值更改时,调试器将停止。
您可以对继承类或复合类执行相同的操作。
地址:&(this->m_a.m_value)
如果您不知道要检查的变量的字节数,可以使用 sizeof 运算符。
例如:
如果您查看“调用堆栈”,您可以看到更改变量值的函数。
Imagine you have a class called A with the following declaration.
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.
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.
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:
If you look at the "Call stack" you can see the function that changed the value of the variable.
在 Visual Studio 2015 中,您可以在自动实现的属性的
set
访问器上放置断点,并且当属性更新更新
或者; @AbdulRaufMujahid 在评论中指出,如果自动实现的属性在一行上,您可以将光标定位在
get;
或set;
上,然后点击F9
并相应地放置一个断点。 好的!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 updatedUpdate
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;
orset;
and hitF9
and a breakpoint will be placed accordingly. Nice!在 Visual Studio 2005 菜单中:
调试 -> 新断点 -> 新数据断点
输入:
In the Visual Studio 2005 menu:
Debug -> New Breakpoint -> New Data Breakpoint
Enter:
您还可以选择在代码中显式中断:
来自 MSDN:
但这只是一个后备方案。 正如其他评论中所述,在 Visual Studio 中设置条件断点是更好的选择。
You can also choose to break explicitly in code:
From MSDN:
This is only a fallback, though. Setting a conditional breakpoint in Visual Studio, as described in other comments, is a better choice.
我记得您使用 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.
如果您使用 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.