假设我有一个方法 foo
,当遍历对象的层次结构时,它会被不同的方法调用。
是否有可能在方法 foo
内部中断,只有当它被方法 bar
调用时(所以 bar
存在于调用堆栈中)?
LLDB 或 GDB 是否支持这样的用例?
最佳答案
最新版本的 gdb 附带了一些用 Python 编写的便利函数,就是为了这种情况。看看 $_caller_is
和 friend 。 (FWIW 这个确切的用例是促使我致力于将 Python 添加到 gdb 的原因......)
一个简单的用法是:
(gdb) break foo if $_any_caller_matches("bar")
如果调用堆栈在 foo
和 bar
调用之间包含更多函数,则堆栈如下所示,
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/