首页 > 解决方案 > 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:

  1. 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.
  2. 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?

标签: c++templatesgdbgdb-python

解决方案


这个答案显示了如何迭代程序中的所有全局符号。

对于您的测试程序,它产生:

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>'也有效。


推荐阅读