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. List提取相同元素

    List<int> currentList = Cls_Data.SoruceDataIntses[key]; preList = currentList.Intersect(preLis ...

  2. js 字符串操作函数

    concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定 ...

  3. JAVA程序中SQL语句无法传递中文参数

    vi /etc/my.cnf [mysqld]# The default character set that will be used when a new schema or table is# ...

  4. C#事件的理解应用

    之前对C#的事件理解的不够透彻,总是感觉在实际应用上差一些火候.最近写character类的相关内容,有了一些理解,在这里分享一下. &感觉大神没必要往下看了 下面开始正式内容: 比如说,角色 ...

  5. iOS自动检测版本更新

    虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的. 首先说一下原理: 每个上架的苹果应用程序,都会有一个应用程序的 ...

  6. ***IT程序员自我修养和情商提升文章

    低情商的13个表现 --------------------------------------------------------------------- — THE END —

  7. Git使用笔记

    Ubuntu下安装步骤 sudo apt-get install git完成git的安装 安装完成后进行配置 git config –global user.name “Your Name” git ...

  8. asterisk 通话噪音,自动挂断,回声等情况

    打开配置文件:cd /etc/asterisk/ vim chan_dahdi.conf 1: busydetect:忙音检测,如果开启,Asterisk会拨号尝试或通话中分析在线的音频,从而尝试识别 ...

  9. Qt经典出错信息之undefined reference to `vtable for classname

    原文链接:Qt经典出错信息之undefined reference to `vtable for classname 这个出错信息太常见了,用过Qt两个月以上的朋友基本上都能自己解决了,因为太经典了, ...

  10. MyEclipse创建Maven工程

    先要在MyEclipse中对Maven进行设置: