20145337 GDB调试汇编堆栈过程分析

测试代码

    #include<stdio.h>
short addend1 = 1;
static int addend2 = 2;
const static long addend3 = 3;
static int g(int x) return x + addend1;
}
static const int f(int x)
{
return g(x + addend2);
}
int main(void)
{
return f(8) + addend3;
}

分析过程

  • 使用gcc -g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器

  • 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指令获取汇编代码,用i(info) r(registers)指令查看各寄存器的值:



  • 可见此时主函数的栈基址为 0xbffff2d4,用x(examine)指令查看内存地址中的值,但目前%esp所指堆栈内容为0,%ebp所指内容也为0

  • 首先,结合display命令和寄存器或pc内部变量,做如下设置:display /i $pc,这样在每次执行下一条汇编语句时,都会显示出当前执行的语句。下面展示每一步时%esp%ebp和堆栈内容的变化:





  • call指令将下一条指令的地址入栈,此时%esp,%ebp和堆栈的值为:





  • 将上一个函数的基址入栈,从当前%esp开始作为新基址:





  • 先为传参做准备:





  • 实参的计算在%eax中进行:

  • f函数的汇编代码:

  • 实参入栈:

















  • call指令将下一条指令的地址入栈:











  • 计算short+int:

















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











  • ret指令将栈顶弹给%eip:





  • 因为函数f修改了%esp,所以用leave指令恢复。leave指令先将%esp对其到%ebp,然后把栈顶弹给%ebp:











  • 主函数汇编代码:









序号 汇编代码 %eip %ebp %esp %eax
0001 movl $0x8,(%esp) 0x80483e2 0xbffff2d8 0xbffff2d4 0x1
0002 call 0x80483c4 0x80483e9 0xbffff2d8 0xbffff2d4 0x1
0003 push %ebp 0x80483c4 0xbffff2d8 0xbffff2d0 0x1
0004 move %esp,%ebp 0x80483c5 0xbffff2d8 0xbffff2cc 0x1
0005 sub $0x4,%esp 0x80483c7 0xbffff2cc 0xbffff2cc 0x1
0006 mov 0x804a014%eax 0x80483c4 0xbffff2cc 0xbffff2c8 0x1
0007 add 0x8(%ebp),%eax 0x80483cf 0xbffff2cc 0xbffff2cc 0x2
0008 mov %eax,(%esp) 0x80483d2 0xbffff2cc 0xbffff2c8 0xa
0009 call 0x80483b4 0x80483d5 0xbffff2cc 0xbffff2c8 0xa
0010 push %ebp 0x80483b4 0xbffff2cc 0xbffff2c4 0xa
0011 mov %esp,%ebp 0x80483b5 0xbffff2cc 0xbffff2c0 0xa
0012 movzwl 0x8048010,%eax 0x80483b7 0xbffff2c0 0xbffff2c0 0xa
0013 cwtl 0x80483be 0xbffff2c0 0xbffff2c0 0x1
0014 add 0x8(%ebp),%eax 0x80483bf 0xbffff2c0 0xbffff2c0 0x1
0015 pop %ebp 0x80483c2 0xbffff2c0 0xbffff2c0 0x1
0016 ret 0x80483c3 0xbffff2cc 0xbffff2c4 0xb
0017 leave 0x80483da 0xbffff2cc 0xbffff2c8 0xb
0018 ret 0x80483db 0xbffff2d8 0xbffff2d0 0xb
0019 mov 0x80484d0,%edx 0x80483ee 0xbffff2d8 0xbffff2d4 0xb
0020 add %edx,%eax 0x80483f4 0xbffff2d8 0xbffff2d4 0xb
0021 leave 0x80483f6 0xbffff2d8 0xbffff2d4 0xe
序号 汇编代码 堆栈
0001 movl $0x8,(%esp) 0x0
0002 call 0x80483c4 0x8 0x0
0003 push %ebp 0x80483ee 0x8 0x0
0004 move %esp,%ebp 0xbffff2d8 0x80483ee 0x8
0005 sub $0x4,%esp 0xbffff2d8 0x80483ee 0x8
0006 mov 0x804a014%eax 0xbffff2d8 0x80483ee 0x8
0007 add 0x8(%ebp),%eax 0x8048409 0xbffff2d8 0x8 0x0
0008 mov %eax,(%esp) 0x8048409 0xbffff2d8 0x8 0x0 0x14c4d3
0009 call 0x80483b4 0xa 0xbffff2d8 0x80483ee 0x0 0x14c4d3
0010 push %ebp 0x80483da 0xa 0xbffff2d8 0x8 0x0
0011 mov %esp,%ebp 0xbffff2cc 0x80483da 0xa 0x80483ee 0x8 0x0
0012 movzwl 0x8048010,%eax 0xbffff2cc 0x80483da 0xa 0x80483ee 0x8 0x0
0013 cwtl 0xbffff2cc 0x80483da 0xa 0x80483ee 0x8 0x0
0014 add 0x8(%ebp),%eax 0xbffff2cc 0x80483da 0xa 0x80483ee 0x8 0x0
0015 pop %ebp 0xbffff2cc 0x80483da 0xa 0x80483ee 0x8 0x0
0016 ret 0x80483da 0xa 0xbffff2d8 0x8 0x0
0017 leave 0xa 0xbffff2d8 0x80483ee 0x0
0018 ret 0x80483ee 0x8 0x0
0019 mov 0x80484d0,%edx 0x8 0x0
0020 add %edx,%eax 0x0
0021 leave 0x0

20145337 GDB调试汇编堆栈过程分析的更多相关文章

  1. GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 分析过程 这是我的C源文件:click here 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb ...

  2. 20145212——GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 测试代码 #include <stdio.h> short val = 1; int vv = 2; int g(int xxx) { return xxx + ...

  3. 20145223《信息安全系统设计基础》 GDB调试汇编堆栈过程分析

    20145223<信息安全系统设计基础> GDB调试汇编堆栈过程分析 分析的c语言源码 生成汇编代码--命令:gcc -g example.c -o example -m32 进入gdb调 ...

  4. 赵文豪 GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器: 使用gdb调 ...

  5. 20145208 GDB调试汇编堆栈过程分析

    20145208 GDB调试汇编堆栈过程分析 测试代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const ...

  6. 20145218 GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 虚拟机中分析过程 输入gcc - g example.c -o example -m32指令在64位机器上产生32位汇编,但出现以下错误: 这时需要使用sudo apt-g ...

  7. 20145236 GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 首先需要输入sudo apt-get install libc6-dev-i386安装一个库才能产生汇编代码,然后输入gcc - g example.c -o exampl ...

  8. 20145312 GDB调试汇编堆栈过程分析

    20145312 GDB调试汇编堆栈过程分析 参考资料 卢肖明同学的博客:<GDB调试汇编堆栈过程分析>: http://www.cnblogs.com/lxm20145215----/p ...

  9. 20145240 GDB调试汇编堆栈过程分析

    20145240 GDB调试汇编堆栈过程分析 测试代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const ...

随机推荐

  1. MFC 按钮如何改变颜色

    我们发现想改变对话框的背景颜色是很简单的,但是对话框的背景颜色改变了后,我们发现按钮的颜色没有改变,如下图. 这样做出来的对话框看起来,不是很自然,我们也想把按钮的颜色改变一下.这就用到了按钮的重绘. ...

  2. c++中4个与类型转换相关的关键字分析

    static_cast reinterpret_cast dynamic_cast const_cast 1.**static_cast------运算符完成相关类型之间的转换** 使用场景:如在同一 ...

  3. jquery的live转on的办法

    <!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...

  4. PHP中的一些常用正则表达式

    匹配表单验证 验证账号,字母开头,允许 5-16 字节,允许字母数字下划线:^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 验证账号,不能为空,不能有空格,只能是英文字母:^\S+[a-z ...

  5. 动画系统(Mecanim补充)

      设置状态机部分等在实践中总结. 状态机基础: 动画层 Animation Layers Unity 使用"动画层"来管理身体不同部分的复杂状态机. 动画状态机  Animati ...

  6. pdf2htmlEx安装及测试

    pdf2htmEx转换效果优秀,可以将pdf转换为html文件,转换速度很快 有两种输出形式, 1.一个pdf对应一个html文件,转换出来的html文件较大 2.一个pdf对应多个html页面,且可 ...

  7. maven权威指南学习笔记(一)——简介

    maven是什么?有什么用? Maven是一个项目管理工具,它包含了     一个项目对象模型 (Project Object Model),     一组标准集合,     一个项目生命周期(Pro ...

  8. hive学习笔记

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  9. byobu相关操作

    http://lingbjxm.iteye.com/blog/2155833 重命名窗口:Fn F8

  10. Difference between WCF and Web API and WCF REST and Web Service

    The .Net framework has a number of technologies that allow you to create HTTP services such as Web S ...