简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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