Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show lambda functions on backtraces?

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

like image 690
Luca Marturana Avatar asked Apr 10 '15 10:04

Luca Marturana


1 Answers

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.

like image 92
Jonathan Wakely Avatar answered Oct 20 '22 17:10

Jonathan Wakely