任意类型的GDB条件断点

Gol*_*sar 12 c++ linux gdb

是否可以在GDB中设置条件断点,其中条件表达式包含任意类类型的对象?

我需要在函数内设置断点,条件将检查对象的成员字符串变量是否等于"foo".所以,像:

condition 1 myObject->myStringVar == "foo"
Run Code Online (Sandbox Code Playgroud)

但它不起作用.GDB是否只允许基本和char*类型的条件断点?有什么办法可以在非原始类型上设置条件断点吗?

ks1*_*322 12

有什么办法可以在非原始类型上设置条件断点吗?

是的,一种方法是将非基本类型转换为原始类型,在您的情况下转换为char*,并用于strcmp比较字符串.

condition 1 strcmp(myObject->myStringVar.c_str(),"foo") == 0
Run Code Online (Sandbox Code Playgroud)


Hos*_*ork 8

您提出的问题的答案是肯定的......在一般情况下,它适用于任意类和函数以及类成员函数.您不会受到测试原始类型的困扰.类成员重载operator==,应该工作.

但标准图书馆非常疯狂.我打赌这个案例的问题与operator==for std :: string是一个全局模板化运算符重载:

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

所以声明如下:

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

我经常遇到gdb的问题......如果它不知道该怎么做,我也不会感到惊讶!

请注意,除了@ ks1322所说的,你可以留在C++领域,更简单地使用std :: string :: compare():

condition 1 myObject->myStringVar.compare("foo") == 0
Run Code Online (Sandbox Code Playgroud)