且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是EBP帧指针寄存器的目的是什么?

更新时间:2022-06-12 04:31:19

框指针是引用指针,允许一个调试器知道在哪里局部变量或参数是用一个恒定的偏移。虽然ESP的在执行过程中值的变化,EBP保持不变,从而可以在相同的偏移量(如第一个参数将永远在EBP-4,而ESP偏移量可以显著改变,因为你会推到同一个变量/弹出的东西)

Frame pointer is a reference pointer allowing a debugger to know where local variable or an argument is at with a single constant offset. Although ESP's value changes over the course of execution, EBP remains the same making it possible to reach the same variable at the same offset (such as first parameter will always be at EBP-4 while ESP offsets can change significantly since you'll be pushing/popping things)

为什么不编译器扔掉帧指针?因为帧指针,调试器可以揣摩出局部变量和参数使用符号表,因为他们保证是在一个恒定的偏移EBP。否则,没有计算出一个局部变量是在code的任何一点的简便方法。

Why don't compilers throw away frame pointer? Because with frame pointer, the debugger can figure out where local variables and arguments are using the symbol table since they are guaranteed to be at a constant offset to EBP. Otherwise there isn't an easy way to figure where a local variable is at any point in code.

正如格雷格提到的,它也有助于堆栈展开为一个调试因为EBP提供因此让调试堆栈帧的反向链表弄清楚的函数的堆栈帧的大小(本地变量+参数)。

As Greg mentioned, it also helps stack unwinding for a debugger since EBP provides a reverse linked list of stack frames therefore letting the debugger to figure out size of stack frame (local variables + arguments) of the function.

大多数编译器提供了一个选项,以忽略帧指针虽然它使调试真的很难。这种选择不应该被全球使用,即使在发布code。你不知道什么时候你需要调试用户的崩溃。

Most compilers provide an option to omit frame pointers although it makes debugging really hard. That option should never be used globally, even in release code. You don't know when you'll need to debug a user's crash.