c++ - 是否只有当调用堆栈中存在某个方法时才可以在断点处中断?

标签 c++ debugging gdb lldb

假设我有一个方法 foo,当遍历对象的层次结构时,它会被不同的方法调用。

是否有可能在方法 foo 内部中断,只有当它被方法 bar 调用时(所以 bar 存在于调用堆栈中)?

LLDB 或 GDB 是否支持这样的用例?

最佳答案

最新版本的 gdb 附带了一些用 Python 编写的便利函数,就是为了这种情况。看看 $_caller_is 和 friend 。 (FWIW 这个确切的用例是促使我致力于将 Python 添加到 gdb 的原因......)

一个简单的用法是:

(gdb) break foo if $_any_caller_matches("bar")

如果调用堆栈在 foobar 调用之间包含更多函数,则堆栈如下所示,

foo()
...
...
...
bar()
...
...
...
main()

您可以将一个额外的参数传递给 _any_caller_matches,它指示要检查 bar 出现的帧数

(gdb) break foo if $_any_caller_matches("bar", 10)

引用:https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Funs.html

$_any_caller_matches(regexp[, number_of_frames])

Returns one if any calling function’s name matches the regular expression regexp. Otherwise it returns zero.

If the optional argument number_of_frames is provided, it is the number of frames up in the stack to look. The default is 1.

This function differs from $_caller_matches in that this function checks all stack frames from the immediate caller to the frame specified by number_of_frames, whereas $_caller_matches only checks the frame specified by number_of_frames.

关于c++ - 是否只有当调用堆栈中存在某个方法时才可以在断点处中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392313/

相关文章:

c++ - SDL2 - 垂直同步不工作

python - 跟踪python解释器的执行路径

debugging - 在打包的 EXE 文件中查找 OEP

c - 不同的 gdb 寄存器名称

c++ - GDB 不断产生 "No line xx in file"错误,即使文件中包含以下行

c++ - 什么是 "= delete"?

c++ - 加密存储密码

linux - 内核在启动时无限期挂起

c++ - 为什么gdb找不到源文件

c++ - 如何让 C++ 模板类调用另一个类的方法?