【发布时间】:2015-08-27 11:26:02
【问题描述】:
我正在尝试更好地掌握 LLDB,目前在调试某些代码时被困在尝试调用本地定义的函数(使用 LLDB 的 expr)。为了简单起见,让我们考虑这个玩具代码:
testing_lldb.c:
unsigned long factorial(unsigned input) {
unsigned long ret_val = 0;
if (input == 0)
ret_val = 1;
else
ret_val = input * (factorial(input-1));
return ret_val;
}
我是这样编译的:
$ clang -g -Weverything -c lldb_test.c
然后键入以下命令运行 LLDB:
$ lldb testing_lldb.o
在我的 LLDB 会话中,我希望能够致电 factorial()。我的第一次尝试:
(lldb) expr unsigned long i = factorial(i);
error: 'factorial' has unknown return type;
cast the call to its declared return type
错误信息包含明确的提示,所以我再试一次:
(lldb) expr unsigned long i = (unsigned long)factorial(i);
error: warning: function 'factorial' has internal linkage but is not defined
note: used here
error: Can't run the expression locally: Interpreter doesn't handle one of the expression's opcodes
好的,我尝试按照SO question 的答案手动定义factorial():
(lldb) expr typedef unsigned long (*$factorial_type)(unsigned)
(lldb) expr $factorial_type $factorial_function = ($factorial_type)0x0000000000000000
(lldb) expr unsigned long i = (unsigned long)factorial(i);
error: warning: function 'factorial' has internal linkage but is not defined
note: used here
error: Can't run the expression locally: Interpreter doesn't handle one of the expression's opcodes
这给了我与上面完全相同的错误。我通过运行再次检查了factorial() 的起始地址:
(lldb) image lookup -Avi -n factorial
问题
鉴于 testing_lldb.c,需要什么才能在 LLDB 的表达式中使用 factorial()?
关于环境的一些细节:
$ uname -r
3.16.0-4-amd64
$ lldb -v
lldb version 3.4.2 ( revision )
$ clang -v
Debian clang version 3.4.2-14 (tags/RELEASE_34/dot2-final) (based on LLVM 3.4.2)
Target: x86_64-pc-linux-gnu
【问题讨论】: