【发布时间】:2011-10-02 02:28:49
【问题描述】:
跟进
嗯,我不确定我是否做对了。 感谢迄今为止提供的所有帮助。
我之前的帖子: Is this really the address
我正在制作新线程,因为这确实是一个单独的问题,也是核心问题。
请多多包涵,谢谢。
让我重申我的目标:
我希望能够查看每个变量的内存地址(我们知道程序的入口地址,并且我们知道在读取汇编代码时预留了多少字节)。假设我们得到以下源代码:
源代码
int main()
{
int a = 15;
int b;
int c;
b = c;
c = c+1;
return 0;
}
我们应该能够找出变量a和c的地址,以及这些内存地址中的值。
使用 gdb layout asm 我明白了
│0x80483f4 <main()> push %ebp │
│0x80483f5 <main()+1> mov %esp,%ebp │
│0x80483f7 <main()+3> sub $0x10,%esp │
│0x80483fa <main()+6> movl $0xf,-0x4(%ebp) │
│0x8048401 <main()+13> mov -0x8(%ebp),%eax │
│0x8048404 <main()+16> mov %eax,-0xc(%ebp) │
│0x8048407 <main()+19> addl $0x1,-0x8(%ebp) │
│0x804840b <main()+23> mov $0x0,%eax │
│0x8048410 <main()+28> leave │
│0x8048411 <main()+29> ret │
│0x8048412 nop
// the statement int a = 15 is in the address 0x80483fa
// I want to get the value 15
x/w 0x80483fd <== this will print 15
但这对我来说没有意义,因为据我回忆,变量应该在 ebp - 0x10 中,对吗?
// the starting address of the program is 0x80483f4
// minus 0x10 we get 0x80483E4
x/w 0x80483E4 <== will print a big number
// Since b = c, I should be able to get that as I decrement, but no luck
我不认为我知道我在做什么......?一方面,程序一终止,自动变量就被销毁了……
PS:我真的不能在调试时使用 cout 或 printf 或设置断点或观察器。
所以 print $ebp 将不起作用,因为没有活动寄存器(请记住程序终止 - 没有断点!)。因此,诸如 info locals、info registers 之类的命令不可用。
我一整天都在试图弄清楚发生了什么。我非常感谢所有的帮助,我期待得到更多帮助。谢谢。
我该怎么办??我需要查看变量 a、b、c 的值。如何才能做到这一点?
非常感谢。
不是真正的家庭作业,而是课堂讨论。
【问题讨论】: