visual-studio 使用 Visual Studio 调试器更改值时中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break when a value changes using the Visual Studio debugger
提问by FlySwat
Is there a way to place a watch on variable and only have Visual Studio break when that value changes?
有没有办法将监视放在变量上,并且只有在该值更改时才让 Visual Studio 中断?
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.
断点条件仍然需要设置断点,我宁愿设置监视并让 Visual Studio 在状态更改时设置断点。
采纳答案by AShelly
In the Visual Studio 2005 menu:
在 Visual Studio 2005 菜单中:
Debug-> New Breakpoint-> New Data Breakpoint
调试->新建断点->新建数据断点
Enter:
进入:
&myVariable
回答by Michael Petrotta
You can also choose to break explicitly in code:
您还可以选择在代码中显式中断:
// Assuming C#
if (condition)
{
System.Diagnostics.Debugger.Break();
}
From MSDN:
来自 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.
Debugger.Break:如果没有附加调试器,系统会询问用户是否要附加调试器。如果是,则启动调试器。如果附加了调试器,调试器会收到用户断点事件的信号,并且调试器会暂停进程的执行,就像命中了调试器断点一样。
This is only a fallback, though. Setting a conditional breakpoint in Visual Studio, as described in other comments, is a better choice.
不过,这只是一个后备。如其他注释中所述,在 Visual Studio 中设置条件断点是更好的选择。
回答by Craig
Really old post but in case someone is unaware...
真的很旧的帖子,但万一有人不知道......
In Visual Studio 2015, you can place a breakpoint on the setaccessor of an Auto-Implemented Property and the debugger will break when the property is updated
在Visual Studio 2015 中,您可以在setAuto-Implemented Property 的访问器上放置断点,并且在更新该属性时调试器将中断
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 F9and a breakpoint will be placed accordingly. Nice!
或者; @AbdulRaufMujahid 在评论中指出,如果自动实现的属性在一行上,您可以将光标定位在get;or 上set;并点击,F9并相应地放置一个断点。好的!
public bool IsUpdated { get; set; }
回答by momboco
Imagine you have a class called A with the following declaration.
假设您有一个名为 A 的类,并带有以下声明。
class A
{
public:
A();
private:
int m_value;
};
You want the program to stop when someone modifies the value of "m_value".
您希望程序在有人修改“m_value”的值时停止。
Go to the class definition and put a breakpoint in the constructor of A.
转到类定义并在 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)
地址:&(this->m_value)
字节数:4(因为int有4个字节)
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)
地址:&(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.
如果您不知道要检查的变量的字节数,则可以使用 sizeof 运算符。
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.
如果您查看“调用堆栈”,您可以看到更改变量值的函数。
回答by Marcello
Change the variable into a property and add a breakpoint in the set method. Example:
把变量改成属性,在set方法中加断点。例子:
private bool m_Var = false;
protected bool var
{
get {
return m_var;
}
set {
m_var = value;
}
}
回答by Gulzar Nazim
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.
我记得您使用Visual Basic 6.0描述它的方式。在 Visual Studio 中,到目前为止我发现的唯一方法是指定断点条件。
回答by Julien N
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.
如果您使用 WPF,有一个很棒的工具:WPF Inspector。
它将自身附加到 WPF 应用程序并显示具有所有属性的完整控件树,它允许您(除其他外)中断任何属性更改。
But sadly I didn't find any tool that would allow you to do the same with ANY property or variable.
但遗憾的是,我没有找到任何工具可以让您对任何属性或变量执行相同操作。
回答by Oskar
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).
右键单击断点对我来说很好用(尽管大多数情况下我将它用于特定变量值的条件断点。即使中断涉及线程名称的表达式也可以工作,如果您试图发现线程问题,这非常有用)。
回答by R Risack
As Peter Mortensen wrote:
正如彼得·莫特森 (Peter Mortensen) 所写:
In the Visual Studio 2005 menu:
Debug -> New Breakpoint -> New Data Breakpoint
Enter: &myVariable
在 Visual Studio 2005 菜单中:
调试 -> 新建断点 -> 新建数据断点
输入:&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.
显然,系统必须知道要监视内存中的哪个地址。所以 - 设置一个正常的断点来初始化myVariable(或myClass.m_Variable) - 运行系统并等待它在该断点处停止。- 现在菜单条目已启用,您可以通过输入查看变量&myVariable,或通过输入查看实例&myClass.m_Variable。现在地址定义明确。
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.
抱歉,我通过解释已经给出的解决方案做错了事情。但是我无法添加评论,并且已经有一些关于此的评论。
回答by Matt
Update in 2019:
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.
现在,适用于 .Net Core 3.0 或更高版本的 Visual Studio 2019 Preview 2 正式支持此功能。当然,您可能必须考虑使用预览版 IDE 的潜在风险。我想在不久的将来这将包含在官方的 Visual Studio 中。
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!
幸运的是,数据断点不再是 C++ 独有的,因为它们现在可用于 Visual Studio 2019 预览版 2 中的 .NET Core(3.0 或更高版本)!

