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?
This answer shows how to iterate over all global symbols in a program.
For your test program, it yields:
From here, you can easily find all instantiations of
bar<>()
, and set breakpoints on them using Python breakpoints API.Using
function='bar<2>'
also works.