C语言 如何使用 GDB 看到堆栈的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7848771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can one see content of stack with GDB?
提问by DipSwitch
I am new to GDB, so I have some questions:
我是 GDB 的新手,所以我有一些问题:
How can I look at content of the stack? Example: to see content of register, I type
info registers. For the stack, what should it be?How can I see the content of
$0x4(%esp)? When I typeprint /d $0x4(%esp), GDB gives an error.
如何查看堆栈的内容?示例:要查看寄存器的内容,我输入
info registers. 对于堆栈,它应该是什么?我怎样才能看到 的内容
$0x4(%esp)?当我输入时print /d $0x4(%esp),GDB 给出错误。
Platform: Linux and GDB
平台:Linux 和 GDB
回答by DipSwitch
info frameto show the stack frame info
info frame显示堆栈帧信息
To read the memory at given addresses you should take a look at x
要读取给定地址的内存,您应该查看 x
x/x $espfor hex x/d $espfor signed x/u $espfor unsigned etc. x uses the format syntax, you could also take a look at the current instruction via x/i $eipetc.
x/x $espfor hex x/d $espfor signed x/u $espfor unsigned etc. x 使用格式语法,您还可以通过x/i $eip等查看当前指令。
回答by gaoithe
Use:
用:
bt- backtrace: show stack functions and argsinfo frame- show stack start/end/args/locals pointersx/100x $sp- show stack memory
bt- 回溯:显示堆栈函数和参数info frame- 显示堆栈开始/结束/args/locals 指针x/100x $sp- 显示堆栈内存
(gdb) bt
#0 zzz () at zzz.c:96
#1 0xf7d39cba in yyy (arg=arg@entry=0x0) at yyy.c:542
#2 0xf7d3a4f6 in yyyinit () at yyy.c:590
#3 0x0804ac0c in gnninit () at gnn.c:374
#4 main (argc=1, argv=0xffffd5e4) at gnn.c:389
(gdb) info frame
Stack level 0, frame at 0xffeac770:
eip = 0x8049047 in main (goo.c:291); saved eip 0xf7f1fea1
source language c.
Arglist at 0xffeac768, args: argc=1, argv=0xffffd5e4
Locals at 0xffeac768, Previous frame's sp is 0xffeac770
Saved registers:
ebx at 0xffeac75c, ebp at 0xffeac768, esi at 0xffeac760, edi at 0xffeac764, eip at 0xffeac76c
(gdb) x/10x $sp
0xffeac63c: 0xf7d39cba 0xf7d3c0d8 0xf7d3c21b 0x00000001
0xffeac64c: 0xf78d133f 0xffeac6f4 0xf7a14450 0xffeac678
0xffeac65c: 0x00000000 0xf7d3790e
回答by unwind
You need to use gdb's memory-display commands. The basic one is x, for examine. There's an example on the linked-to page that uses
您需要使用 gdb 的内存显示命令。基本的一种是x,用于检查。链接到页面上有一个示例,该示例使用
gdb> x/4xw $sp
to print "four words (w) of memory above the stack pointer (here, $sp) in hexadecimal (x)". The quotation is slightly paraphrased.
以十六进制 ( )打印“w堆栈指针上方的四个内存字 ( )(此处为)”。引文略有解释。$spx

