首页 技术 正文
技术 2022年11月22日
0 收藏 901 点赞 4,605 浏览 2086 个字

GDB调试汇编堆栈

分析过程

  • C语言源代码
int g(int x)
{
return x+6;
}
int f(int x)
{
return g(x+1);
}
int main(void)
{
return f(5)+1;
}
  • 使用gcc -g exp1.c -o exp1 -m32指令在64位机器上产生32位汇编。

  • 进入gdb调试器。

  • 在main函数处设置一个断点。

  • 使用run指令开始运行程序,返回main函数运行的结果。

  • 使用disassemble指令获取汇编代码。

  • 使用info registers命令查看各寄存器的值。

  • 由上图可以看出,帧指针%ebp在0xbffff2e8,栈指针%esp在0xbffff2e4。

  • x+地址来查看%esp和%ebp的值,此时都为0。

  • display,设置程序中断后欲显示的数据及其格式。例如,如果希望每次程序中断后可以看到即将被执行的下一条汇编指令,可以使用命令

    display /i $pc

    将5压入栈中,传递参数

  • call指令将返回地址压入栈中。

  • f函数汇编代码。

  • 将栈指针的值压入栈中。

  • 帧指针指向栈指针所在位置,以此为基址。

  • 在%eax中计算

  • 将g函数返回值放入%esp所指地址中

  • 将6压入栈中,传递参数

  • 调用g函数,调用call指令将f函数的返回地址压入栈中

  • g函数汇编代码

  • 将栈指针的值压入栈中。

  • 在%eax中计算

  • pop %ebp指令将栈顶弹到%ebp中,同时%esp增加4字节

  • ret将栈顶给%eip

  • leave使栈做好返回的准备,等价于
movl %ebp,%esp //将%ebp的值给%esp
popl %ebp //将栈顶弹到%ebp中
  • ret指令,返回主函数

  • 主函数汇编代码

  • 执行leave后,各寄存器值

序号 指令 %eip %ebp %esp %eax 栈顶->栈底
001 movl $0x5,(%esp) 0x80483e2 0xbffff2e8 0xbffff2e4 0x1 0x5 0x0
002 call 0x80483bf 0x80483bf 0xbffff2e8 0xbffff2e0 0x1 0x80483e7 0x5 0x0
003 push %ebp 0x80483c0 0xbffff2e8 0xbffff2dc 0x1 0xbffff2e8 0x80483e7 0x5 0x0
004 mov %esp,%ebp 0x80483c2 0xbffff2dc 0xbffff2dc 0x1 0xbffff2e8 0x80483e7 0x5 0x0
005 sub $0x4,%esp 0x80483c5 0xbffff2dc 0xbffff2d8 0x1 0xbffff2e8 0x80483e7 0x5 0x0
006 mov 0x8(%ebp),%eax 0x80483c8 0xbffff2dc 0xbffff2d8 0x5 0xbffff2e8 0x80483e7 0x5 0x0
007 add $0x1,%eax 0x80483cb 0xbffff2dc 0xbffff2d8 0x6 0xbffff2e8 0x80483e7 0x5 0x0
008 mov %eax,(%esp) 0x80483ce 0xbffff2dc 0xbffff2d8 0x6 0x6 0xbffff2e8 0x80483e7 0x5 0x0
009 Call 0x80483b4 0x80483b4 0xbffff2dc 0xbffff2d4 0x6 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
010 push %ebp 0x80483b5 0xbffff2dc 0xbffff2d0 0x6 0xbffff2dc 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
011 Mov %esp,%ebp 0x80483b7 0xbffff2d0 Oxbffff2d0 0x6 0xbffff2dc 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
012 mov 0x8(%ebp),%eax 0x80483ba 0xbffff2d0 0xbffff2d0 0x6 0xbffff2dc 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
013 add $0x6,%eax 0x80483bd 0xbffff2d0 0xbffff2d0 0xc 0xbffff2dc 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
014 Pop %ebp 0x80483be 0xbffff2dc 0xbffff2d4 0xc 0x80483d3 0x6 0xbffff2e8 0x80483e7 0x5 0x0
015 Ret 0x80483d3 0xbffff2dc 0xbffff2d8 0xc 0x6 0xbffff2e8 0x80483e7 0x5 0x0
016 leave 0x80483d4 0xbffff2e8 0xbffff2e0 0xc 0x80483e7 0x5 0x0
017 Ret 0x80483e7 0xbffff2e8 0xbffff2e4 0xc 0x5 0x0
018 add $0x1,%eax 0x80483ea 0xbffff2e8 0xbffff2e4 0xd 0x5 0x0
019 leave 0x80483eb 0x0 0xbffff2ec 0xd 0x0
020 ret 0xb7e3a533 0x0 0xbffff2f0 0xd
上一篇: three.js立方体
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896