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. WPF 动画显示控件

    当我们要显示一个控件的时候,不仅仅要显示这个控件,还要有动画的效果. 主要用到了DoubleAnimation类. public static void ShowAnimation(object co ...

  2. 【转】Java面试宝典2015版(绝对值得收藏超长版)(一)

    (转自:http://mp.weixin.qq.com/s?__biz=MjM5MTM0NjQ2MQ==&mid=206619070&idx=1&sn=fcb21001d442 ...

  3. android studio使用部分报错处理

    1.android studio 导入项目时Error:SSL peer shut down incorrectly 今天导入一个项目到studio,显示在下载一个一个1.1.0-rc4的东西. 过了 ...

  4. ***Linux下使用git命令及github项目

    在linux下搭建git环境1.创建Github账号,https://github.com2.Linux创建SSH密钥: ssh-keygen  ##一直默认就可以了 3.将公钥加入到Github账户 ...

  5. BI项目记笔记索引

    这个笔记系列主要记录了在BI项目中,如何搭建环境进行源代码管理以及文档管理. 用到的产品包括: TFS Express Sharepoint Visual Studio SQL Server   配置 ...

  6. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  7. 7 Container With Most Water_Leetcode

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  8. Aspx页面模拟WebService功能

    在后台引入 using System.Web.Services 命名空间 然后在编写web服务方法: [WebMethod] public static string GetData(string t ...

  9. linux下安装apache与php;Apache+PHP+MySQL配置攻略

    1.apache   在如下页面下载apache的for Linux 的源码包    http://www.apache.org/dist/httpd/;   存至/home/xx目录,xx是自建文件 ...

  10. 百度地图API试用--(初次尝试)

    2016-03-17: 百度地图API申请key的步骤相对简单,不做过多阐述. 初次使用百度地图API感觉有点神奇,有些功能加进来以后有点问题,注释掉等有空再解决. 代码如下: <%@ page ...