20145208 GDB调试汇编堆栈过程分析
20145208 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)
指令查看各寄存器的值:
- 可见此时主函数的栈基址为 0xffffcf98,用
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
:
- 主函数汇编代码:
指令 | %esp | %ebp | %eip | %eax | 堆栈 |
---|---|---|---|---|---|
push $0x8 | 0xffffcf98 | 0xffffcf98 | 0x804840b | -134500932 | 0x0 |
call 0x80483ef | 0xffffcf94 | 0xffffcf98 | 0x804840b | -134500932 | 0x8 0x0 |
push %ebp | 0xffffcf90 | 0xffffcf98 | 0x80483ef | -134500932 | 0x8048412 0x8 0x0 |
mov %esp,%ebp | 0xffffcf8c | 0xffffcf98 | 0x80483f0 | -134500932 | 0xffffcf98 0x8048412 0x8 0x0 |
mov 0x804a01c,%edx | 0xffffcf8c | 0xffffcf8c | 0x80483f2 | -134500932 | 0xffffcf98 0x8048412 0x8 0x0 |
mov 0x8(%ebp),%eax | 0xffffcf8c | 0xffffcf8c | 0x80483f8 | -134500932 | 0xffffcf98 0x8048412 0x8 0x0 |
add %edx,%eax | 0xffffcf8c | 0xffffcf8c | 0x80483fb | 8 | 0xffffcf98 0x8048412 0x8 0x0 |
push %eax | 0xffffcf8c | 0xffffcf8c | 0x80483fd | 10 | 0xffffcf98 0x8048412 0x8 0x0 |
call 0x80483db | 0xffffcf88 | 0xffffcf8c | 0x80483fe | 10 | 0xa 0xffffcf98 0x8048412 0x8 0x0 |
push %ebp | 0xffffcf84 | 0xffffcf8c | 0x80483db | 10 | 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
mov %esp,%ebp | 0xffffcf80 | 0xffffcf8c | 0x80483dc | 10 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
movzwl 0x804a018,%eax | 0xffffcf80 | 0xffffcf80 | 0x80483de | 10 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
movswl %ax,%edx | 0xffffcf80 | 0xffffcf80 | 0x80483e5 | 1 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
mov 0x8(%ebp),%eax | 0xffffcf80 | 0xffffcf80 | 0x80483e8 | 1 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
add %edx,%eax | 0xffffcf80 | 0xffffcf80 | 0x80483eb | 10 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
pop %ebp | 0xffffcf80 | 0xffffcf80 | 0x80483ed | 11 | 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
ret | 0xffffcf84 | 0xffffcf8c | 0x80483ee | 11 | 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
add $0x4,%esp | 0xffffcf88 | 0xffffcf8c | 0x8048403 | 11 | 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0 |
leave | 0xffffcf8c | 0xffffcf8c | 0x8048406 | 11 | 0xffffcf98 0x8048412 0x8 0x0 |
ret | 0xffffcf90 | 0xffffcf98 | 0x8048407 | 11 | 0x8048412 0x8 0x0 |
add $0x4,%esp | 0xffffcf94 | 0xffffcf98 | 0x8048412 | 11 | 0x8 0x0 |
mov $0x3,%edx | 0xffffcf98 | 0xffffcf98 | 0x8048415 | 11 | 0x0 |
add %edx,%eax | 0xffffcf98 | 0xffffcf98 | 0x804841a | 11 | 0x0 |
leave | 0xffffcf98 | 0xffffcf98 | 0x804841c | 14 | 0x0 |
ret | 0xffffcf9c | 0x0 | 0x804841d | 14 |
20145208 GDB调试汇编堆栈过程分析的更多相关文章
- GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 分析过程 这是我的C源文件:click here 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb ...
- 20145212——GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 测试代码 #include <stdio.h> short val = 1; int vv = 2; int g(int xxx) { return xxx + ...
- 20145223《信息安全系统设计基础》 GDB调试汇编堆栈过程分析
20145223<信息安全系统设计基础> GDB调试汇编堆栈过程分析 分析的c语言源码 生成汇编代码--命令:gcc -g example.c -o example -m32 进入gdb调 ...
- 赵文豪 GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器: 使用gdb调 ...
- 20145337 GDB调试汇编堆栈过程分析
20145337 GDB调试汇编堆栈过程分析 测试代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const ...
- 20145218 GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 虚拟机中分析过程 输入gcc - g example.c -o example -m32指令在64位机器上产生32位汇编,但出现以下错误: 这时需要使用sudo apt-g ...
- 20145236 GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 首先需要输入sudo apt-get install libc6-dev-i386安装一个库才能产生汇编代码,然后输入gcc - g example.c -o exampl ...
- 20145312 GDB调试汇编堆栈过程分析
20145312 GDB调试汇编堆栈过程分析 参考资料 卢肖明同学的博客:<GDB调试汇编堆栈过程分析>: http://www.cnblogs.com/lxm20145215----/p ...
- 20145240 GDB调试汇编堆栈过程分析
20145240 GDB调试汇编堆栈过程分析 测试代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const ...
随机推荐
- WAMPServer安装和配置
1. 下载地址: www.wampserver.com www.php100.com 本机下载在 安装在 2. 自定义网站根目录 设置到这里 访问localhost就会访问到自定义的目录了假设 ...
- Promise和$.Deferred总结
语法对比: Promise .then(f).catch(f)是.then(f,f)的语法糖 .all([A,B,C])等最慢的 .race([A,B,C])最快的 $.Deferred .d ...
- 【单页应用之通信机制】view之间应该如何通信
前言 在单页应用中,view与view之间的通信机制一直是一个重点,因为单页应用的所有操作以及状态管理全部发生在一个页面上 没有很好的组织的话很容易就乱了,就算表面上看起来没有问题,事实上会有各种隐忧 ...
- css实现图片切换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- bootstrap无限级分类 jq拓展 之前的无限级分类的封装版~
git地址:https://github.com/zhangjiahao93/jQ.select HTML部分 <!DOCTYPE html> <html> <head ...
- 轻松掌握:JavaScript观察者模式
观察者模式 观察者模式也叫"订阅者/发布者"模式,定义对象间的一种一对多的依赖关系,发布者可以向所有订阅者发布消息. 观察者模式被广泛地应用于JavaScript客户端编程中.所有 ...
- Linux安全基础:grep命令的使用
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.Unix的 ...
- C# 生成字符串的 CheckSum
C# 生成字符串的 CheckSum private static string CheckSum(string message) { char[] chars = message.ToCharArr ...
- Android 四大组件之再论BroadCast
BroadCast 是android提供的跨进程通讯的有一利器. 1.异步执行onReceiver @Nullable public abstract Intent registerReceiver( ...
- 我对uml类图关系的理解
uml类图的关系: 泛化关系也就是继承. 实现关系就是一个类实现另外一个接口. 依赖关系就是一个类使用了另外一个类,是一种使用关系,在这个类的某个服务中需要另外一个类来协助. 关联关系就是一类拥有另外 ...