c++ - How to set a gdb breakpoint on a label in all template function instantiations
问题描述
Suppose I have the following C++:
template <int I> int bar(int i) {
++i;
label1:
return I * i;
}
int main(int argc, char **) { return bar<2>(argc); }
Is it possible to set a gdb breakpoint on label1
for all instantiations of bar? In the above, it is easy -- there is just one instantiation. In my actual use case there are a large number spread all over the code base.
Said another way, in my gdb commandfile, is there a way to avoid the need to know a priori about every template instantiation of bar? In my actual use case, my aim is to emit some information about program state at the point of the labels (worry not, no programs were harmed by goto
).
I know I can set a breakpoint on the label for a specific instantiation of bar
as follows:
break -function bar<2> -label label1
I also know about rbreak
which can be used to break on the entry of all template functions, but apparently does not have the -label
option. I don't want to break on the entry point -- just the the label(s). I also looked to see if I could combine rbreak
with until label1
but that didn't work.
Two other approaches I've considered are:
- grep for the labels (or could even just be specially formed comments), emit line numbers and use this information to populate/generate my gdb command file with source file and line number break points. This will definitely work, but was sort of hoping to avoid the need to "generate" my command file.
- Since I intend to implement some gdb Python pretty printers, perhaps I can parse the source from within python to get the line numbers and subsequently set the breakpoints. I'm just learning the Python API and it wasn't immediately obvious how to get the equivalent of
list
from within the gdb python package.
Any suggestions?
解决方案
这个答案显示了如何迭代程序中的所有全局符号。
对于您的测试程序,它产生:
bar<2>(int) True
main(int, char**) True
从这里,您可以轻松找到 的所有实例bar<>()
,并使用Python 断点 API在它们上设置断点。
(gdb) py
>gdb.Breakpoint(function='bar<2>(int)', label='label1')
Breakpoint 1 at 0x1158: file t.cc, line 3.
使用function='bar<2>'
也有效。
推荐阅读
- javascript - 未捕获的类型错误:无法读取带有偏移量的未定义的属性“顶部”
- wordpress - 我可以在 Google 网站上托管 wordpress 吗?
- python - 使用 findall 在字符串中找不到三个连续的单词
- javascript - 类型 'string' 不可分配给类型 '(url: string) => string'.ts(2322)
- android - Android TransactionTooLargeException 选择图片库
- linux - jitsi 无法共享某些应用程序(屏幕共享)
- ios - iOS IAP 恢复需要几次尝试
- javascript - 默认滚动条始终完全滚动的库
- reactjs - 卸载 React Native 组件时如何清除 Image.getSize 请求?
- python - Python yield 关键字的意义和关于生成器的困惑?