gpt4 book ai didi

c++ - 从函数获取 char* 后的条件 gdb 断点

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:32 31 4
gpt4 key购买 nike

骨架代码:

假设你有这样的东西(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 包含“hell”,如何在 gdb 中放置断点。这个 substr 'hell' 可以出现在 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 作为子字符串的字符串。

关于c++ - 从函数获取 char* 后的条件 gdb 断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56985746/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com