如果已命中断点 A,则启用断点 B

发布于 2024-12-15 09:49:27 字数 887 浏览 1 评论 0原文

我经常发现自己在代码中的某处设置断点 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 技术交流群。

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

发布评论

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

评论(2

尽揽少女心 2024-12-22 09:49:28

这是我认为你能做的最好的事情,但它似乎太大了,甚至无法尝试,因为它涉及添加一个变量......

string breakpointToStopOn = string.Empty;
Console.WriteLine("HERE"); // You can set breakpoint A here, 
                           // with a condition (right click on the breakpoint, then selectCondition),
                           // that sets breakpointToStopOn = "A"
Console.WriteLine("B"); // and you can set your breakpoint here with this condition
                        // (breakpointToStopOn == "A");  

你实际上无法在 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...

string breakpointToStopOn = string.Empty;
Console.WriteLine("HERE"); // You can set breakpoint A here, 
                           // with a condition (right click on the breakpoint, then selectCondition),
                           // that sets breakpointToStopOn = "A"
Console.WriteLine("B"); // and you can set your breakpoint here with this condition
                        // (breakpointToStopOn == "A");  

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.

紫瑟鸿黎 2024-12-22 09:49:27

即使不更改代码,您也可以通过使用某些全局存储来保存启用依赖断点的标记来获取依赖断点。

我发现的最易于访问的存储之一是应用程序域自定义属性。可以通过 System.AppDomain.CurrentDomain.GetData 和 SetData 方法访问它们。

因此,在第一个断点上,您可以使用以下命令定义“命中时”设置:

{System.AppDomain.CurrentDomain.SetData("break",true)}

breakpoint condition

在依赖断点上,设置命中条件到:

System.AppDomain.CurrentDomain.GetData("break") != null

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 :

{System.AppDomain.CurrentDomain.SetData("break",true)}

breakpoint condition

On the dependent breakpoint, set hit condition to:

System.AppDomain.CurrentDomain.GetData("break") != null

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