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. iOS开发CoreGraphics核心图形框架之一——CGPath的应用

    一.引言    CoreGraphics核心图形框架相较于UIKit框架更加偏于底层.在Objective-C工程中,CoreGraphics其中方法都是采用C语言风格进行编写的,同时其并不支持Obj ...

  2. [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析

    reference to : http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法( ...

  3. [LeetCode] Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  4. Problem with "AnyConnect was not able to establish connection to the specified secure gateway."

    Cisco的VPN客户端最近报"AnyConnect was not able to establish connection to the specified secure gateway ...

  5. list for循环中删除元素

    Iterator.remove() is safe, you can use it like this: List<String> list = new ArrayList<> ...

  6. 把office文档转换为html过程中的一些坑

    之前和我们项目的团队一起酝酿了一个项目,公司的业务文档技术文档比较多,但都比较分散,虽然通过FTP或其他方式聚合起来了,但感觉还是不够方便. 另外公司每次都来新员工,新员工都需要一些培训,比较耗时,比 ...

  7. string,stringbuilder,stringbuffer用法

    总结:1.如果要操作少量的数据用 = String   ==================================>字符串常量2.单线程操作字符串缓冲区 下操作大量数据 = Strin ...

  8. webform简单、复合控件

    简单控件: 1.Label 会被编译成span标签 属性: Text:文本内容 CssClass:CSS样式 Enlabled:是否可用 Visible:是否可见 2.Literal 空的,C#会把里 ...

  9. OGNL的使用

    访问Action中的普通属性: <s:property value="loginname"/><br/> 访问Action中的对象属性: <s:pro ...

  10. Json格式示意图

    json视图工具:http://www.bejson.com/jsonviewernew/ 一.Json格式化,(看到数组里面又有数组一下子疑问不是合格json):尾门地址查询: =>=> ...