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?
A conditional breakpoint insertion can be done by using one single command by using the breakpoint command followed by the word if followed by a condition. In the previous example, the breakpoint can be inserted with the line b 29 if (y == 999).
To set a conditional breakpointOn the Home tab, in the Breakpoints group, choose Set/Clear Condition. In the Debugger Breakpoint Condition window, enter a condition. On the Home tab, in the Breakpoints group, choose List. In the Debugger Breakpoint List window, enter a condition in the Condition column.
Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.
Here [CONDITION] is a boolean expression, which, in GDB is true if the result is nonzero, otherwise it is false. The condition can include a function call, the value of a variable or the result of any GDB expression. Type help condition at the GDB prompt for more.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With