如果已命中断点 A,则启用断点 B
我经常发现自己在代码中的某处设置断点 A,并在命中断点时手动启用一个或多个断点。一个典型的情况是当我正在调试单元测试并且不关心前面的测试时。
void testAddZeros()
{
Number a(0);
Number b(0);
Number result = a.add(b);
assert((a + b) == Number(0))
}
void testAddOnes()
{
Number a(1);
Number b(1);
Number result = a.add(b);
assert((a + b) == Number(2));
}
void testAddNegativeNumber()
{
Number a(1);
Number b(-1)
Number result = a.add(b);
assert((a + b) == Number(0));
}
想象一下,如果 testAddZeros()
和 testAddOnes()
运行良好,但 testAddNegativeNumber()
运行良好。在这种情况下,在 Number result = a.add(b);
设置断点将是开始调试的自然位置。现在想象一下错误位于 Number::add
内部深处的某个地方,因此我们对 Numbers::add
早期发生的内容并不真正感兴趣。我想要做的是在 Numbers::add
内部某处设置一个断点,只有在 testAddNegativeNumber()
-test 内部时才会触发。
有没有办法在断点A
被命中时自动启用断点B
?
I'm often find myself setting a breakpoint A somewhere in the code and manually enabling one or more breakpoints when this breakpoint is hit. A typical case is when I'm debugging an unittest and don't care about the preceding tests.
void testAddZeros()
{
Number a(0);
Number b(0);
Number result = a.add(b);
assert((a + b) == Number(0))
}
void testAddOnes()
{
Number a(1);
Number b(1);
Number result = a.add(b);
assert((a + b) == Number(2));
}
void testAddNegativeNumber()
{
Number a(1);
Number b(-1)
Number result = a.add(b);
assert((a + b) == Number(0));
}
Imagine if testAddZeros()
and testAddOnes()
runs fine, but testAddNegativeNumber()
. In this case setting a breakpoint at Number result = a.add(b);
would be a natural place to start debugging. Now imagine that the error is located somewhere deep inside Number::add
, so we're not really interrested in the stuff that occurs early in Numbers::add
. What I want to do is to set a breakpoint somewhere inside Numbers::add
that only triggers if I'm inside the testAddNegativeNumber()
-test.
Is there any way to automatically enable breakpoint B
when breakpoint A
is hit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我认为你能做的最好的事情,但它似乎太大了,甚至无法尝试,因为它涉及添加一个变量......
你实际上无法在 Console.WriteLine("HERE" 上停止) 行,但您可以启用或禁用该断点,这实际上会启用另一个断点。
但请注意,条件断点语句会在调试时严重降低应用程序的性能。
This is about the best I think you could do, but it seems too big of a hack to even try, because it involves adding a variable...
You won't actually be able to stop on the Console.WriteLine("HERE") line, but you could enable or disable the breakpoint, which would in effect enable the other breakpoint.
Beware though, conditional breakpoint statements will seriously degrade performance of your app while debugging.
即使不更改代码,您也可以通过使用某些全局存储来保存启用依赖断点的标记来获取依赖断点。
我发现的最易于访问的存储之一是应用程序域自定义属性。可以通过 System.AppDomain.CurrentDomain.GetData 和 SetData 方法访问它们。
因此,在第一个断点上,您可以使用以下命令定义“命中时”设置:
在依赖断点上,设置命中条件到:
You can get the dependent breakpoints even without changing the code, by using some global storage to hold the marker that will enable dependent breakpoint.
One of the most accessible storages that I've found is app domain custom properties. They can be accessed by System.AppDomain.CurrentDomain.GetData and SetData methods.
So on first breakpoint you define a "when hit" setting with :
On the dependent breakpoint, set hit condition to: