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. TFS API : 四、工作项查询

    TFS API : 四.工作项查询 本节将讲述如何查询工作项,将用户统计数据. 使用WorkItemStore.Query方法进行查询工作项,其使用的语法和SQL语法类似: Select [标题] f ...

  2. TextView属性android:ellipsize="marquee"不生效的解决办法

    最近自己在写自己的第一个app,过程中遇到了这个问题,查了不少帖子,经过尝试发现,这种问题一般分为两类: 1. TextView的Text值赋值后不更改,很多帖子上说如下写法就可以生效: <Te ...

  3. 【Java EE 学习 77 下】【数据采集系统第九天】【使用spring实现答案水平分库】【未解决问题:分库查询问题】

    之前说过,如果一个数据库中要存储的数据量整体比较小,但是其中一个表存储的数据比较多,比如日志表,这时候就要考虑分表存储了:但是如果一个数据库整体存储的容量就比较大,该怎么办呢?这时候就需要考虑分库了, ...

  4. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  5. Xocde一次版本升级遇到的问题 (Code Sign Error)

    因为Xcode对ios版本的支持问题,我对XCode进行了一次升级,导致原来还好的项目代码出现了编译时错误. Code Sign Error failed with exit code 1 问题就在于 ...

  6. Qt基本框架介绍

    #include <QApplication>#include <QWidget> int main(int argc, char *argv[]){ QApplication ...

  7. UWP学习记录1-开端

    UWP学习记录1-开端 1.背景 针对不同基础的人,学习的路线自然是不同的.这篇文章记录的是我个人的学习路线,或者说笔记.我对自己的技术状态的定义是: A.有很好的windows平台编程基础: B.有 ...

  8. parted LVM划分4T磁盘,在线扩展1.5T

      磁盘分区: parted /dev/emcpowera (parted) print Model: Unknown (unknown) Disk /dev/emcpowera: 4398GB Se ...

  9. ABP集合贴(转)

    ABP集合贴 本文背景 公司最近规划的新框架准备基于ABP来搭建,自从在阳铭博客看到ABP框架的介绍后,就一直持续关注着,但还没真正在实际项目中直接使用ABP,只是自己做了一些学习和Demo.ABP所 ...

  10. 【github问题】error: src refspec master does not match any解决方法|please tell me who you are

    http://www.open-open.com/lib/view/open1366080269265.html这个先记录一下省得以后再找 我这里要解决的问题根本是:please tell me wh ...