How can I show lambda functions on backtraces?
Asked Answered
P

2

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

Plafond answered 10/4, 2015 at 10:6 Comment(2)
It's probably useful to mention in which context (debugger? toolchain?) you want to see this.Switchman
Yes, what compiler and debugger are you using?Heathen
E
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.

Endmost answered 11/4, 2015 at 19:13 Comment(0)
C
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.

Crystallize answered 11/4, 2015 at 18:39 Comment(1)
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.Endmost

© 2022 - 2024 — McMap. All rights reserved.