Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GDB debug C++ lambdas?

Tags:

I use C++ 11 features actively. I have program created in Visual Studio 2013 that relies on lambdas to run multiple threads (lambda represents task, and thread receives lambda instance that it has to run). Lambda is defined in static library and linked in executable file that calls it from thread created by this executable file. When I try to debug Linux version of this application with GDB, it looks like GDB can not step into method that contains lambda. It can not set breakpoints in this function, and when I try to step into, it even steps in methods that lambda calls from its body, but after return from these methods it doesn't go to lambda body, it goes to next method that lambda calls, etc. Are there any way to debug lambdas body with GDB?

like image 548
Vitalii Avatar asked Feb 14 '14 10:02

Vitalii


People also ask

Are functors the same as lambdas?

std::function<int(int,int)>; std::function<void(double)> ... Functors allow to write functional programs in C++. Lambdas are syntactic sugar to simplify this. With functors/lambdas classic patters from functional programming (e.g. map / filter /reduce) can be applied in C++.

Are lambdas copyable?

Copying a lambda will copy its state.

Can you use GDB with C++?

You can use GDB as a C++ debugger if a program is written with the GNU compiler and the -g flag. By debugging with GDB, you can catch errors and solve them before they cause severe issues.

Are lambdas fast C++?

See here and Join C++ Stories Premium: Lambda can be 6.6x faster to compile than std::bind!. You can also read it in the book: C++ Lambda Story @Leanpub.


Video Answer


1 Answers

I've seen them in stack traces before, so it does at least know about them. I've never tried setting a normal breakpoint in one. It's sort of a hack, but you can set a breakpoint in one (or anywhere) by using asm volatile("int $3"); on x86(-64).

Here's an example program:

int main(){     auto f = [](){         asm volatile("int $3");     };     f();     return 0; } 

Here's it's backtrace when it hits that breakpoint:

#0  0x0000000000400577 in main::{lambda()#1}::operator()() const () #1  0x000000000040058d in main () 
like image 75
Tyler Avatar answered Oct 09 '22 12:10

Tyler