12

I'm writing a C++11 software and I'm using lambdas. When I print the backtrace with backtrace_symbols_fd all functions are demangled except of lambda. It's a bit obvious because they are anonymous functions, but there is a way to get more insight instead of a raw pointer?

I'm using GCC 4.8 on Linux

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Luca Marturana
  • 428
  • 2
  • 10

2 Answers2

2

Some kind of useful information does exist in the binary, because GDB is able to show more useful names for lambda functions e.g.

(gdb) bt
#0  <lambda()>::operator()(void) const (__closure=0x7fffffffd5ef) at ll.cc:3
#1  0x00000000004005e7 in main () at ll.cc:3

(ALthough maybe the debug info just says it's a closure type, as GDB shows all such functions as <lambda()>::operator())

The mangled name of a template instantiated with a closure type includes a unique name e.g.

#3  0x0000000000400712 in func<main()::<lambda()> >(<lambda()>) (t=...) at l.cc:4

but maybe the name is only used when it's needed in other mangled names.

With GCC you can also print out the name of the closure's operator() by printing the pre-defined variable __PRETTY_FUNCTION__, which shows something like main()::<lambda()>

Using GDB's Python API I can get yet another name for the same closure, e.g.

(gdb) python import gdb; print gdb.block_for_pc(0x8048591).function.name
__lambda0::operator()() const

So that's at least three different names! So I think it's maybe a limitation of backtrace_symbols_fd that it can't find names for the lambda functions.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
0

According to the C++ Standard:

ยง5.1.2/3 states:

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type.

I don't think there is a way to get more useful information. Basically, lambdas are just instances of anonymous classes.

szulak
  • 703
  • 7
  • 16
  • They still need some kind of name, for linkage and name mangling purposes, even if that name cannot be spoken by users. e.g. if you instantiate a template with a closure type the compiler needs to encode the closure type in the mangled name of the template specialization. โ€“ Jonathan Wakely Apr 11 '15 at 18:48