简体   繁体   English

GDB可以调试C ++ lambdas吗?

[英]Can GDB debug C++ lambdas?

I use C++ 11 features actively. 我主动使用C ++ 11功能。 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). 我在Visual Studio 2013中创建的程序依赖于lambdas来运行多个线程(lambda表示任务,而线程接收它必须运行的lambda实例)。 Lambda is defined in static library and linked in executable file that calls it from thread created by this executable file. Lambda在静态库中定义,并链接在可执行文件中,该文件从此可执行文件创建的线程调用它。 When I try to debug Linux version of this application with GDB, it looks like GDB can not step into method that contains lambda. 当我尝试使用GDB调试此应用程序的Linux版本时,看起来GDB无法进入包含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? 它不能在这个函数中设置断点,当我尝试进入时,它甚至会进入lambda从其体调用的方法,但是从这些方法return之后它不会转到lambda体,它会转到下一个方法lambda调用等。有没有办法用GDB调试lambdas体?

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"); 这是一种破解,但你可以通过使用asm volatile("int $3");在一个(或任何地方)设置一个断点asm volatile("int $3"); on x86(-64). 在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 ()

From my experience, gdb cannot step into lambdas -- it just skips over them. 根据我的经验,gdb无法进入lambda - 它只是跳过它们。 Not only that, stepping into a lambda definition seems to confuse gdb and it proceeds to the end of the current function. 不仅如此,单步执行lambda定义似乎会混淆gdb,并且它会继续到当前函数的末尾。 You can, however, place a breakpoint explicitly inside a lambda, and if you hit that point, you will stop. 但是,您可以在lambda中明确放置断点,如果点击该点,则会停止。 This is obviously far from ideal. 这显然远非理想。

Step into Lamba's .run() go next/step (jump initializations) until the following call: 进入Lamba的.run()转到下一步/步骤(跳转初始化)直到以下调用:

std::forward<>(args)(...)

Step into this one. 走进这一步。 It will lead you to you lambda body code. 它会引导你到lambda身体代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM