EBP帧指针寄存器的用途是什么? [英] What is the purpose of the EBP frame pointer register?

查看:35
本文介绍了EBP帧指针寄存器的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是汇编语言的初学者,并且注意到编译器发出的 x86 代码通常会保留帧指针,即使在发布/优化模式下,当它可以将 EBP 寄存器用于其他用途时.

I'm a beginner in assembly language and have noticed that the x86 code emitted by compilers usually keeps the frame pointer around even in release/optimized mode when it could use the EBP register for something else.

我理解为什么帧指针可以使代码更容易调试,如果在函数中调用 alloca() 可能是必要的.然而,x86 的寄存器非常少,如果一个就足够了,使用其中的两个来保存堆栈帧的位置对我来说没有意义.为什么即使在优化/发布版本中省略帧指针也被认为是一个坏主意?

I understand why the frame pointer might make code easier to debug, and might be necessary if alloca() is called within a function. However, x86 has very few registers and using two of them to hold the location of the stack frame when one would suffice just doesn't make sense to me. Why is omitting the frame pointer considered a bad idea even in optimized/release builds?

推荐答案

帧指针是一个引用指针,允许调试器通过单个常量偏移量知道局部变量或参数的位置.尽管 ESP 的值在执行过程中会发生变化,但 EBP 保持不变,因此可以在相同的偏移量处到达相同的变量(例如第一个参数将始终为 EBP+8,而 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+8 while ESP offsets can change significantly since you'll be pushing/popping things)

为什么编译器不丢弃帧指针?因为使用帧指针,调试器可以找出局部变量和参数使用符号表的位置,因为它们保证与 EBP 保持恒定的偏移量.否则没有一种简单的方法来确定局部变量在代码中的任何位置.

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.

正如 Greg 所提到的,它也有助于调试器的堆栈展开,因为 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.

大多数编译器都提供了省略帧指针的选项,尽管这会使调试变得非常困难.即使在发布代码中,也不应该全局使用该选项.您不知道什么时候需要调试用户的崩溃.

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.

这篇关于EBP帧指针寄存器的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆