Golam Kawsar
Golam Kawsar

Reputation: 780

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?

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". So, something like:

condition 1 myObject->myStringVar == "foo"

But it's not working. Does GDB only allow conditional breakpoints on primitive and char* types? Is there any way I could set a conditional breakpoint on non-primitive types?

Upvotes: 18

Views: 7352

Answers (2)

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.

But I'd guess the problem with this case has to do with the operator== for std::string being a global templated operator overload:

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.

Note that in addition to what @ks1322 said, you could stay in the C++ realm and more simply use std::string::compare():

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

Upvotes: 15

ks1322
ks1322

Reputation: 35825

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.

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

Upvotes: 19

Related Questions