简体   繁体   English

任意类型上的 GDB 条件断点,例如 C++ std::string 相等

[英]GDB conditional breakpoint on arbitrary types such as C++ std::string equality

Is it possible to set a conditional breakpoint in GDB where the the condition expression contains objects of arbitrary class types?是否可以在 GDB 中设置条件断点,其中条件表达式包含任意类类型的对象?

I need to set a breakpoint inside a function where the condition will check whether a member string variable of an object equals to say "foo".我需要在函数内设置一个断点,其中条件将检查对象的成员字符串变量是否等于“foo”。 So, something like:所以,像这样:

condition 1 myObject->myStringVar == "foo"

But it's not working.但它不起作用。 Does GDB only allow conditional breakpoints on primitive and char* types? GDB 是否只允许原始类型和 char* 类型的条件断点? Is there any way I could set a conditional breakpoint on non-primitive types?有什么办法可以在非原始类型上设置条件断点?

Is there any way I could set a conditional breakpoint on non-primitive types?有什么办法可以在非原始类型上设置条件断点?

Yes, one way to do it is to convert non-primitive type to primitive one, in your case to char* , and use strcmp to compare strings.是的,一种方法是将非原始类型转换为原始类型,在您的情况下转换为char* ,并使用strcmp来比较字符串。

condition 1 strcmp(myObject->myStringVar.c_str(),"foo") == 0

The answer to your question you asked is yes...in the general case it works for arbitrary classes and functions, and class member functions.您提出的问题的答案是肯定的……在一般情况下,它适用于任意类和函数以及类成员函数。 You aren't stuck with testing primitive types.您不会被测试原始类型所困。 Class member overloads, like operator== , should work.类成员重载,如operator== ,应该可以工作。

But I'd guess the problem with this case has to do with the operator== for std::string being a global templated operator overload :但我猜这个案例的问题与 std::string 的operator==是一个全局模板化运算符重载有关

http://www.cplusplus.com/reference/string/operators/ http://www.cplusplus.com/reference/string/operators/

So the declarations are like:所以声明是这样的:

template<class charT, class traits, class Allocator>
    bool operator==(const basic_string<charT,traits,Allocator>& rhs,
                const charT* lhs );

I wouldn't be surprised if gdb wouldn't know how to connect the dots on that for you.如果 gdb 不知道如何为您连接这些点,我不会感到惊讶。

Note that in addition to what @ks1322 said, you could stay in the C++ realm and more simply use std::string::compare() :请注意,除了@ks1322 所说的之外,您还可以留在 C++ 领域,更简单地使用std::string::compare()

condition 1 myObject->myStringVar.compare("foo") == 0

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

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