Ez栈帧回溯

Ez栈帧回溯

HuAmI Lv3

Ez栈帧回溯

之前原理听了个七八十,也动手操作试着回溯的几次,当时效果不错,但一段时间之后还是犯难生疏,特此Record一下.

实验情境(C++):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>

// 无参无返
void fun1()
{

}

int main()
{
fun1();
return 0;
}

以上是C++代码,一个简单的主函数调用fun1函数 (无参无返).

我们直接在fun1打断点 (断电用×表示):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>

// 无参无返
void fun1()
×{

}

int main()
{
fun1();
return 0;
}

在反汇编中单步debug到图示位置:

img

此时的栈帧状态如图:

1
2
3
4
5
6
7
8
9
10
11
高地址
┌──────────────────────┐
│ main 的参数(如果有)│
├──────────────────────┤
│ 返回地址 │ ← [EBP + 4]
├──────────────────────┤
│ main 的 EBP │ ← [EBP]
├──────────────────────┤
│ fun1 的局部变量 │ ← [EBP - 4] ...
└──────────────────────┘
低地址

此时的[EBP+4]这个存储的就是返回到main函数的返回地址.

img

查询004118e6h地址即为main的返回地址:

img

那么此时就到达了main函数中.

三层及以上的嵌套调用(含main):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>
void fun2()
{

}
// 无参无返
void fun1()
{
fun2();
}

int main()
{
fun1();
return 0;
}

此时我们在void fun2()下面一行打断点.Debug:

img

此时我们进入到fun2()函数中,此时的ESP寄存器中存储的是调用fun2()函数的函数的返回地址(即返回到fun1()的地址).

img

  • Title: Ez栈帧回溯
  • Author: HuAmI
  • Created at : 2026-02-01 18:12:56
  • Updated at : 2026-03-01 12:47:40
  • Link: https://redefine.ohevan.com/2026/02/01/Ez嵌套函数调用的栈帧回溯/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments