使用 gdb 检查调用者帧
Inspecting caller frames with gdb
假设我有:
#include <stdlib.h>
int main()
{
int a = 2, b = 3;
if (a!=b)
abort();
}
编译:
gcc -g c.c
运行 这个,我会得到一个核心转储(由于 SIGABRT
由 abort()
引发),我可以用它来调试:
gdb a.out core
如何让 gdb
从这个上下文中打印 a
和 b
的值?
您是否使用调试符号进行编译 -g
?命令应该是 bt
用于回溯,你也可以使用 bt full
来进行完整的回溯。
更多信息:https://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html
这是另一种专门获取 a
和 b
值的方法,方法是移动到感兴趣的 帧 然后 info locals
会给你价值。
a.out
是用您的代码编译的。 (第 2 帧是您感兴趣的内容,即 main()
)。
$ gdb ./a.out core
[ removed some not-so-interesting info here ]
Reading symbols from ./a.out...done.
[New LWP 14732]
Core was generated by `./a.out'.
Program terminated with signal SIGABRT, Aborted.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007fac16269f5d in __GI_abort () at abort.c:90
#2 0x00005592862f266d in main () at f.c:7
(gdb) frame 2
#2 0x00005592862f266d in main () at f.c:7
7 abort();
(gdb) info locals
a = 2
b = 3
(gdb) q
你也可以使用print
once frame 2:
(gdb) print a
= 2
(gdb) print b
= 3
假设我有:
#include <stdlib.h>
int main()
{
int a = 2, b = 3;
if (a!=b)
abort();
}
编译:
gcc -g c.c
运行 这个,我会得到一个核心转储(由于 SIGABRT
由 abort()
引发),我可以用它来调试:
gdb a.out core
如何让 gdb
从这个上下文中打印 a
和 b
的值?
您是否使用调试符号进行编译 -g
?命令应该是 bt
用于回溯,你也可以使用 bt full
来进行完整的回溯。
更多信息:https://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html
这是另一种专门获取 a
和 b
值的方法,方法是移动到感兴趣的 帧 然后 info locals
会给你价值。
a.out
是用您的代码编译的。 (第 2 帧是您感兴趣的内容,即 main()
)。
$ gdb ./a.out core
[ removed some not-so-interesting info here ]
Reading symbols from ./a.out...done.
[New LWP 14732]
Core was generated by `./a.out'.
Program terminated with signal SIGABRT, Aborted.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007fac16269f5d in __GI_abort () at abort.c:90
#2 0x00005592862f266d in main () at f.c:7
(gdb) frame 2
#2 0x00005592862f266d in main () at f.c:7
7 abort();
(gdb) info locals
a = 2
b = 3
(gdb) q
你也可以使用print
once frame 2:
(gdb) print a
= 2
(gdb) print b
= 3