简体   繁体   English

如何在 Visual C++ 中添加条件断点

[英]How to add a conditional breakpoint in Visual C++

I want to add a breakpoint condition to my code in VC++ Express 2005, so that the breakpoint only triggers if a local variable meets a specified criteria.我想在 VC++ Express 2005 中为我的代码添加断点条件,以便只有在局部变量满足指定条件时才会触发断点。 eg例如

bool my_test(UIDList test_list) {
    foo(test_list);
    bar(test_list); // I have a breakpoint here, but only want it to trigger if test_list.Length() > 0
    print(test_list);
}

Having right-clicked on my breakpoint and selected "Condition..." I have found a dialog that appears to do what I want, however anything I try typing into the text field results in the following error:右键单击我的断点并选择“条件...”后,我发现一个对话框似乎可以执行我想要的操作,但是我尝试在文本字段中输入的任何内容都会导致以下错误:

Unable to evaluate the breakpoint condition: CX0052: Error: member function not present无法评估断点条件:CX0052:错误:成员 function 不存在

I tried the help documentation, but I couldn't find my answer.我尝试了帮助文档,但找不到答案。 I'm hoping someone experienced in VC++ might be able to point me in the right direction...我希望有 VC++ 经验的人能够指出我正确的方向......

I have previously tried upgrading to a more recent version of VC++ Express, but the project did not import cleanly.我之前曾尝试升级到更新版本的 VC++ Express,但该项目没有干净地导入。 Due to the complexity of the project and my current time scales I can't consider upgrading as a solution at this point.由于项目的复杂性和我目前的时间尺度,我现在不能考虑将升级作为解决方案。

VS does have some micro-evaluation engines- in variable watch windows, immediate window, break point conditions etc. I could never find decent documentation on them. VS 确实有一些微观评估引擎——在变量观察 windows、立即 window、断点条件等中。我永远找不到像样的文档。 As far as i can tell they are picky about methods they're willing to call, but they're also insensitive to access limitations.据我所知,他们对愿意调用的方法很挑剔,他们对访问限制也不敏感。
So, you can probably rephrase your condition from因此,您可能可以从

test_list.Length() > 0  

to something like类似于

test_list.m_nLength > 0

(or whatever your private length var is). (或任何您的私人长度变量)。

(EDIT) Just found this msdn page explaining what expressions the debugger can and can't handle. (编辑)刚刚发现这个 msdn 页面解释了调试器可以处理和不能处理的表达式。 So first, indeed -所以首先,确实——

'The debugger can access all class members regardless of access control. '无论访问控制如何,调试器都可以访问所有 class 成员。 You can examine any class object member, including base classes and embedded member objects.'您可以检查任何 class object 成员,包括基类和嵌入的成员对象。

And second, my guess regarding the failure to evaluate 'Length()' - it was probably inlined:其次,我对未能评估 'Length()' 的猜测 - 它可能是内联的:

'A debugger expression cannot call an intrinsic or inlined function unless the function appears at least once as a normal function.' '除非 function 作为正常的 function 出现至少一次,否则调试器表达式不能调用内部或内联 function。

use the DebugBreak();使用 DebugBreak(); function: function:

bool my_test(UIDList test_list) {
    foo(test_list);
    if (bar(test_list) /* or whatever check :) */) // I have a breakpoint here, but only want it to trigger if test_list.Length() > 0
        DebugBreak();
    }
    print(test_list);
}

Or you can always use assert(expression)或者你总是可以使用 assert(expression)

bool my_test(UIDList test_list) {
    foo(test_list);
    bar(test_list);
    assert(test_list.Length() > 0); // will break here
    print(test_list);
}

The conditions in a breakpoint can't call methods as far as I know.据我所知,断点中的条件不能调用方法。 So, what you need to do is to calculate the length before hand.所以,你需要做的是事先计算好长度。 Something like this.像这样的东西。


bool my_test(UIDList test_list) {
 foo(test_list);
 int i = test_list.Length();
 bar(test_list); // I have a breakpoint here, but only want it to trigger if test_list.Length() > 0
 print(test_list);
}

Put a conditional breakpoint on the value of i here and you should be fine.在 i 的值上放一个条件断点,你应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM