假设我有:
#include <stdlib.h>
int main()
{
int a = 2, b = 3;
if (a!=b)
abort();
}
编译:
gcc -g c.c
运行此程序,我将得到一个核心转储(由于 abort()
引发的 SIGABRT
),我可以使用它进行调试:
gdb a.out core
我怎样才能让 gdb
从上下文中打印出 a
和 b
的值?
最佳答案
这是另一种专门获取 a
的方法和 b
通过移动到感兴趣的 frame 然后 info locals
来获取值会给你值(value)。
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
一旦第 2 帧:
(gdb) print a
$1 = 2
(gdb) print b
$2 = 3
关于c - 使用 gdb 检查调用者帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47570384/