Background: I'm trying to simplify my answer to the question c++ - Calling a lambda function in GDB - Stack Overflow.
Consider this simple program
int main(){
auto a=[&](int x){return x+1;};
}
When it's compiled (for example to a.out
), I can see
$ nm --demangle a.out |grep lambda
000000000000113a t main::{lambda(int)#1}::operator()(int) const
$ nm a.out |grep 113a
000000000000113a t _ZZ4mainENKUliE_clEi
So there's a symbol _ZZ4mainENKUliE_clEi
for the lambda operator()
method in the text (code) section.
Given the exact name, I can print its value in gdb, and tab-completion also lists the symbol:
$ gdb -q ./a.out
(gdb) print _ZZ4mainENKUliE_clEi
$1 = {int (const struct {...} * const, int)} 0x55555555513a <operator()(int) const>
(gdb) print 'main::{lambda(int)#1}::operator()(int) const'
$2 = {int (const struct {...} * const, int)} 0x55555555513a <operator()(int) const>
But how can I find the symbol without using the external executable nm
(inside gdb itself)?
Answers using the Python API is preferred.
Failed attempts
As suggested in c - Ask GDB to list all functions in a program - Stack Overflow:
(gdb) info functions .*lambda.*
All functions matching regular expression ".*lambda.*":
File /build/gcc/src/gcc/libstdc++-v3/src/c++11/compatibility-thread-c++0x.cc:
void std::once_flag::_Prepare_execution::_Prepare_execution<std::call_once<void (std::thread::*)(), std::
thread*>(std::once_flag&, void (std::thread::*&&)(), std::thread*&&)::{lambda()#1}>(void (std::thread::*&)())::{l
ambda()#1}::_FUN();
which is not the one I want, and
(gdb) info functions .*ZZ4.*
All functions matching regular expression ".*ZZ4.*":
doesn't match anything at all.
Also tried other things in Debugging with GDB - Examining the Symbol Table,
maintenance print symbols
doesn't have it either.
In Symbols In Python (Debugging with GDB),
gdb.lookup_global_symbol("_ZZ4mainENKUliE_clEi")
and gdb.lookup_static_symbol("_ZZ4mainENKUliE_clEi")
both returns None.
file ./a.out
in gdb does not help (suggested by GDB does not see symbols )