为什么这些内存值在 GDB 中永远不会改变?
再次感谢您迄今为止所提供的巨大帮助。
源代码:
int main()
{
int a = 20;
int b = 10;
int c;
c = a + b;
return 0;
}
Reading symbols from /home/jwxie/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x80483fa: file demoo.cpp, line 3.
Starting program: /home/jwxie/a.out
Temporary breakpoint 1, main () at demoo.cpp:3
3 int a = 20;
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000000
(gdb) info locals
a = 0
b = 134513696
c = 3903476
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-12
0xbffff39c: 0x003b8ff4
-- Now execute int a = 20;
(gdb) stepi
4 int b = 10;
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000014
(gdb) info locals
a = 20
b = 134513696
c = 3903476
(1) 我注意到,无论我重新启动调试或重新引导多少次,任何分配之前的 a、b 和 c 的值都保持不变。
我什至禁用了优化:g++ -g -O0 demo.cpp
这是为什么?
(2) 另一个奇怪的是,每次stepi之后,esp都没有改变,不像在Visual Studio中,我们可以观察esp和ebp的变化... 日志可以在这里找到: 信息寄存器
这里有什么问题? 非常感谢。
编辑 是的。谢谢。这是缺点
(gdb) disas /m main
Dump of assembler code for function main():
2 {
0x080483f4 <+0>: push %ebp
0x080483f5 <+1>: mov %esp,%ebp
0x080483f7 <+3>: sub $0x10,%esp
3 int a = 20;
0x080483fa <+6>: movl $0x14,-0x4(%ebp)
4 int b = 10;
0x08048401 <+13>: movl $0xa,-0x8(%ebp)
5 int c;
6 c = a + b;
0x08048408 <+20>: mov -0x8(%ebp),%eax
0x0804840b <+23>: mov -0x4(%ebp),%edx
0x0804840e <+26>: lea (%edx,%eax,1),%eax
0x08048411 <+29>: mov %eax,-0xc(%ebp)
7 return 0;
0x08048414 <+32>: mov $0x0,%eax
8 }
0x08048419 <+37>: leave
0x0804841a <+38>: ret
End of assembler dump.
Once again, thank you for the great help so far.
The source code:
int main()
{
int a = 20;
int b = 10;
int c;
c = a + b;
return 0;
}
Reading symbols from /home/jwxie/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x80483fa: file demoo.cpp, line 3.
Starting program: /home/jwxie/a.out
Temporary breakpoint 1, main () at demoo.cpp:3
3 int a = 20;
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000000
(gdb) info locals
a = 0
b = 134513696
c = 3903476
(gdb) x/wx $ebp-8
0xbffff3a0: 0x08048420
(gdb) x/wx $ebp-12
0xbffff39c: 0x003b8ff4
-- Now execute int a = 20;
(gdb) stepi
4 int b = 10;
(gdb) x/wx $ebp-4
0xbffff3a4: 0x00000014
(gdb) info locals
a = 20
b = 134513696
c = 3903476
(1)
I noticed that the values of a, b and c prior to any of the assignment remain the same no matter how many times I restart the debug or reboot.
I even disabled optimization: g++ -g -O0 demo.cpp
Why is that?
(2)
Another strange thing is that, after each stepi, esp never changed, unlike in Visual Studio, we can observe the change of esp and ebp...
The log is can be found in here: info registers
What is the problem here?
Thank you very much.
EDIT
Yes. Thank you. Here is the disas
(gdb) disas /m main
Dump of assembler code for function main():
2 {
0x080483f4 <+0>: push %ebp
0x080483f5 <+1>: mov %esp,%ebp
0x080483f7 <+3>: sub $0x10,%esp
3 int a = 20;
0x080483fa <+6>: movl $0x14,-0x4(%ebp)
4 int b = 10;
0x08048401 <+13>: movl $0xa,-0x8(%ebp)
5 int c;
6 c = a + b;
0x08048408 <+20>: mov -0x8(%ebp),%eax
0x0804840b <+23>: mov -0x4(%ebp),%edx
0x0804840e <+26>: lea (%edx,%eax,1),%eax
0x08048411 <+29>: mov %eax,-0xc(%ebp)
7 return 0;
0x08048414 <+32>: mov $0x0,%eax
8 }
0x08048419 <+37>: leave
0x0804841a <+38>: ret
End of assembler dump.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法保证变量值在初始化之前始终相同。一些调试器会将内存初始化为固定值,例如 0xDEADBEEF,一些调试器将清除为零,另一些调试器不会执行任何操作,您将获得内存中的内容。
There is no guarantee that variable values, before initialization, will always be the same. Some debuggers will initialize memory to fixed values, e.g. 0xDEADBEEF, some will clear to zero, others will not do anything and you get what was in memory.