我正在尝试了解 GDB 和 LLDB,以便我可以随时有效地使用它来调试我的程序。
但似乎我被卡住了我不知道如何打印 C 库函数的输出,例如pow,strnlen等等。如果我想探索那里的输出。
以下是 LLDB 和 GDB 输出。
3 int main(int argc,char *argv[]) {
4 int a = pow(3,2);
-> 5 printf("the value of a is %d",a);
6 return 0;
7 }
(lldb) print pow(3,1)
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
error: 'pow' has unknown return type; cast the call to its declared return type
(lldb) print strlen("abc")
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
error: 'strlen' has unknown return type; cast the call to its declared return type
(lldb) expr int a = strlen("abc");
error: 'strlen' has unknown return type; cast the call to its declared return type
(lldb) expr int a = strlen("abc");
Run Code Online (Sandbox Code Playgroud)
GDB 输出
Starting program: /Users/noobie/workspaces/myWork/pow
[New Thread 0x1903 of process 35243]
warning: unhandled dyld version (15)
Thread 2 hit Breakpoint 1, main (argc=1, argv=0x7fff5fbffb10) at pow.c:5
5 int a = pow(3,2);
(gdb) print pow(3,2)
No symbol "pow" in current context.
(gdb) set pow(3,2)
No symbol "pow" in current context.
(gdb) set pow(3,2);
No symbol "pow" in current context.
(gdb) print pow(3,2);
No symbol "pow" in current context.
(gdb) call pow(3,2)
No symbol "pow" in current context.
(gdb)
Run Code Online (Sandbox Code Playgroud)
我已经使用带有 -g3 标志的 gcc 编译了程序
IE
gcc -g3 pow.c -o pow
你从 lldb 得到的错误,例如:
error: 'strlen' has unknown return type; cast the call to its declared return type
Run Code Online (Sandbox Code Playgroud)
就是它所说的。您需要将调用转换为正确的返回类型:
(lldb) print (size_t) strlen("abc")
(size_t) $0 = 3
Run Code Online (Sandbox Code Playgroud)
类型信息是从失踪的原因strlen和printf等是为了节省空间,当它看到函数的定义,而不是在每次使用网站上的编译器只写的函数的签名到调试信息。由于您没有标准 C 库的调试信息,因此您没有这些信息。
调试器在调用函数之前需要此信息的原因是,如果调用返回结构的函数,但生成的代码就像函数返回标量值一样,调用该函数将破坏线程堆栈函数被调用,破坏了您的调试会话。所以 lldb 不会猜测这个。
请注意,在 macOS 上,系统为大多数系统库提供了“模块映射”,这允许 lldb 从模块重建类型。要在调试纯 C 程序时告诉 lldb 加载模块,请运行以下命令:
(lldb) expr -l objective-c -- @import Darwin
Run Code Online (Sandbox Code Playgroud)
如果您正在调试 ObjC 程序,则可以省略语言规范。该表达式运行后,lldb 将加载模块映射,您可以调用标准 C 库中的大多数函数而无需强制转换。