骨架代码:
假设你有这样的东西(x.cpp(:
int main() {
char* str = <some_function_which_returns_char*>; // Such as hello, hell, hellow and it could be anything.
// Do some work here.
}
如果 str 包含"地狱",如何在 gdb 中放置断点。这个子"地狱"可以出现在str的任何位置。说啊,你好等。我写过:
b x.cpp:3 if $_regex(str, "hell") // At line number 3 of above snapshot. Right after getting the char*
这是正确的方式吗?或还有其他方法可以处理吗?
让我们暂时不要担心泄漏和其他任何事情。
可以使用
cond
命令使断点成为条件:
-
cond x.cpp:3 strcmp(str,"hell") == 0
- 确切地说hell
。 -
cond x.cpp:3 strncmp (str,"hell",4)
- 对于以hell
开头的所有字符串。 -
cond x.cpp:3 strstr(str, "hell") != NULL
- 用于包含hell
作为子字符串的所有字符串。