如何查看GDB中自动变量的内存地址?
跟进
嗯,我不确定我做的是否正确。 感谢迄今为止的所有帮助。
我之前的主题: 这真的是地址
我正在创建新线程因为这确实是一个单独的问题,也是核心问题。
请多多包涵,谢谢。
让我重申一下我的目标:
我希望能够查看每个变量的内存地址(我们知道程序的入口地址,并且我们知道在读取汇编代码时预留了多少字节)。假设我们给出以下源代码:
源代码
int main()
{
int a = 15;
int b;
int c;
b = c;
c = c+1;
return 0;
}
我们应该能够找出变量a和c的地址,以及这些内存地址中的值。
使用 gdb 布局 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 的值。这怎么能做到呢?
非常感谢。
不是真正的作业,而是课堂讨论。
Follow-up
Hmmm I am not sure if I am doing the right thing. Thanks for all the helps thus far.
My previous thread:
Is this really the address
I am making new thread because this is really a separate problem, and the core problem.
Please bear with me, thank you.
Let me restate my goal:
I want to be able to look into the memory address of each variable (we know the entry address of the program, and we know how many bytes are set aside from reading the assembly code). Suppose we are given the following source code:
Source Code
int main()
{
int a = 15;
int b;
int c;
b = c;
c = c+1;
return 0;
}
We should be able to find out the address of variable a and c, and the values in these memory addresses.
Using gdb layout asm I get this
│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
But it doesn't make sense to me because from what I recalled, the variables are supposed to be in ebp - 0x10 right?
// 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
I don't think I know what I am doing...? On one hand, the automatic variables are destroyed as soon as the program terminates...
PS: I really can't use cout, or printf, or setting breakpoints or watcher while debugging.
So doing print $ebp will not work because there is no active register (remember the program terminates - no breakpoint!). So commands like info locals, info registers aren't available.
I have been spending the whole day trying to figure out what is going on. I really appreciate all the helps and I am looking forward to getting more. Thanks.
What should I do?? I need to look at the value of variable a, b, c. How can this be done?
Thank you very much.
Not really a homework, but a class discussion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些变量没有特定的存储位置。它们是堆栈变量。因此,在程序终止后,您不能依赖它们位于内存中,因为在创建它们的函数返回后,它们被视为超出范围,从而允许它们所在的地址被重用于存储其他内容。
想象一下,您有一个函数,其源代码如下所示:
如果您调用
foo(1)
,则变量y
将存在于两个不同的内存地址,一个对应两个不同的内存地址为foo
的两个嵌套调用(foo(1)
和foo(0)
)创建的堆栈帧。如果您调用foo(10)
,将会有 11 个y
实例,每个实例持有不同的值并驻留在不同的内存地址。如果不使用断点,则所有意图和目的的变量都不存在。它们仅在程序运行时分配存储空间,并且当前堆栈包含它们所在函数的帧。您无法事后获取它们(除了核心转储,这实际上是断点的一种形式)。
总结:如果您在程序运行时不分析程序,无论是通过中断调试器还是通过添加一些将打印/保留值的代码,您都无法检查堆栈变量。这些是堆栈变量。如果必须让它们成为单实例,则应该通过将它们移到函数作用域之外来使它们成为堆分配的全局变量。
These variables do not have one particular memory location. They are stack variables. So you cannot rely on them being in memory after the program terminates, because they are considered out of scope after the function in which they are created returns, allowing the address at which they resided to be reused for storing other content.
Imagine you have a function whose source looks like this:
If you call
foo(1)
, the variabley
will exist at two different memory addresses, one for each of the two stack frames created for the two nested invocations offoo
(foo(1)
andfoo(0)
). If you callfoo(10)
, there will be eleven instances ofy
, each one holding a different value and residing at a different memory address.If you do not use a breakpoint, then the variables for all intents and purposes do not exist. They only have storage allocated when the program is running and the current stack contains a frame from the function in which they reside. You cannot grab them postmortem (except from a core dump, which is a form of breakpoint really).
Sum-up: if you do not analyze the program while it is running, either via breaking to a debugger or via adding some code that will print/set aside values, you can not inspect stack variables. These are stack variables. If you must have them be single-instance, you should make them heap-allocated global variables by moving them outside of function scope.