//gdb-call-lambda.cpp
#include <iostream>
void do_something(void) {
std::cout << "blah blah" << std::endl;
auto lambda_func = [](void){
std::cout << "in lambda" << std::endl;
return;
};
lambda_func();
std::cout << "..." << std::endl;
return;
}
int main(int argc, char **argv) {
do_something();
return 0;
}
在这个示例程序中,如果你编译(
g++ gdb-call-lambda.cpp --std=c++11 -g
)并在gdb中运行它(gdb ./a.out
),你可以让GDB调用任何“正常”的函数。例如:(gdb) break main
Breakpoint 1 at 0x4008e7: file gdb-call-lambda.cpp, line 20.
(gdb) r
Starting program: /home/keithb/dev/mytest/gdb-call-lambda/a.out
Breakpoint 1, main (argc=1, argv=0x7fffffffdfb8) at gdb-call-lambda.cpp:20
20 do_something();
(gdb) call do_something()
blah blah
in lambda
...
然而,如果您尝试调用lambda函数:
(gdb) break do_something
Breakpoint 2 at 0x400891: file gdb-call-lambda.cpp, line 5.
(gdb) c
Continuing.
Breakpoint 2, do_something () at gdb-call-lambda.cpp:5
5 std::cout << "blah blah" << std::endl;
(gdb) n
blah blah
12 lambda_func();
(gdb) n
in lambda
14 std::cout << "..." << std::endl;
(gdb) call lambda_func()
Invalid data type for function to be called
GDB 有些反应异常。因此我的问题是:如何在 GDB 中调用 lambda?与普通函数相比,询问 GDB 所期望的并没有什么有趣的内容:
(gdb) whatis lambda_func
type = __lambda0
(gdb) whatis do_something
type = void (void)
我查看了lambda_func是否拥有任何特殊成员,例如指向函数的指针,类似于std::function和/或std::bind:
(gdb) print lambda_func
$1 = {<No data fields>}
没有特殊成员?那么这可能只是一个大肚子的函数指针?
(gdb) call ((void (void)) lambda_func)()
Program received signal SIGSEGV, Segmentation fault.
0x00007fffffffdeaf in ?? ()
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(at 0x0x7fffffffdeaf) will be abandoned.
When the function is done executing, GDB will silently stop.
所以我甚至不确定传递任何参数或特别捕获类型的顺序。
我尝试了额外的call lambda_func.operator()()
,call lambda_func::operator()
,call lambda_func::operator()()
,call __lambda0
,call __lambda0()
,call __lambda0::operator()
,call __lambda0::operator()()
,但都没有成功。
在Google上搜索发现有关在lambda中设置断点的内容,但没有关于如何从调试器中调用这些lambda的信息。
值得一提的是,这是在Ubuntu 14.04 64位系统上使用g++ 4.8.2-19ubuntu1和gdb7.7-0ubuntu3.1。
lambda_func.operator()
会起作用吗? - Johncall lambda_func.operator()()
,call lambda_func::operator()
,call lambda_func::operator()()
,call __lambda0
,call __lambda0()
,调用__lambda0::operator()
,call __lambda0::operator()()
,但都没有成功。 - inetknghterror: call to a function '$_0::operator()() const' ('_ZNK3$_0clEv') that is not present in the target error: warning: function '<anonymous class>::operator()' has internal linkage but is not defined error: The expression could not be prepared to run in the target
。 - tclamb