Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

linux

gdb

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?

like image 663
Golam Kawsar Avatar asked May 29 '12 14:05

Golam Kawsar


People also ask

How do I add a conditional breakpoint in GDB?

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).

How do you do a conditional breakpoint?

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.

What is conditional breakpoint in debugger?

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.

How do I add a condition in GDB?

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.


2 Answers

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
like image 191
ks1322 Avatar answered Sep 28 '22 07:09

ks1322


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
like image 36
HostileFork says dont trust SE Avatar answered Sep 28 '22 07:09

HostileFork says dont trust SE