c++ 单步查看汇编代码【转】
用gdb 查看汇编代码, 采用disassemble 和 x 命令。 nexti, stepi 可以单步指令执行
如下例:
------------------------------------------------------------
源代码:
------------------------------------------------------------
[root@hjj ~]# cat 1.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int size=sizeof("hjj");
printf("size is %d\n",size);
return 0;
}
------------------------------------------------------------
编译
------------------------------------------------------------
ot@hjj ~]# gcc -g3 -o 1 1.c
------------------------------------------------------------
调试
------------------------------------------------------------
[root@hjj ~]# gdb 1
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/1...done.
(gdb) b main
Breakpoint 1 at 0x4004d3: file 1.c, line 5.
(gdb) r
Starting program: /root/1
Breakpoint 1, main (argc=1, argv=0x7fffffffe5c8) at 1.c:5
5 int size=sizeof("hjj");
------------------------------------------------------------
观察. 用disasseble.
/m 源码和汇编一起排列
/r 还可以看到16进制代码
------------------------------------------------------------
(gdb) disassemble /m main
Dump of assembler code for function main:
4 {
0x00000000004004c4 <+0>: push %rbp
0x00000000004004c5 <+1>: mov %rsp,%rbp
0x00000000004004c8 <+4>: sub $0x20,%rsp
0x00000000004004cc <+8>: mov %edi,-0x14(%rbp)
0x00000000004004cf <+11>: mov %rsi,-0x20(%rbp)
5 int size=sizeof("hjj");
=> 0x00000000004004d3 <+15>: movl $0x4,-0x4(%rbp)
6 printf("size is %d\n",size);
0x00000000004004da <+22>: mov $0x4005f8,%eax
0x00000000004004df <+27>: mov -0x4(%rbp),%edx
0x00000000004004e2 <+30>: mov %edx,%esi
0x00000000004004e4 <+32>: mov %rax,%rdi
0x00000000004004e7 <+35>: mov $0x0,%eax
0x00000000004004ec <+40>: callq 0x4003b8 <printf@plt>
7 return 0;
0x00000000004004f1 <+45>: mov $0x0,%eax
8 }
0x00000000004004f6 <+50>: leaveq
0x00000000004004f7 <+51>: retq
End of assembler dump.
------------------------------------------------------------
用 x/i 可以查看指令
------------------------------------------------------------
(gdb) x/15i main
0x4004c4 <main>: push %rbp
0x4004c5 <main+1>: mov %rsp,%rbp
0x4004c8 <main+4>: sub $0x20,%rsp
0x4004cc <main+8>: mov %edi,-0x14(%rbp)
0x4004cf <main+11>: mov %rsi,-0x20(%rbp)
=> 0x4004d3 <main+15>: movl $0x4,-0x4(%rbp)
0x4004da <main+22>: mov $0x4005f8,%eax
0x4004df <main+27>: mov -0x4(%rbp),%edx
0x4004e2 <main+30>: mov %edx,%esi
0x4004e4 <main+32>: mov %rax,%rdi
0x4004e7 <main+35>: mov $0x0,%eax
0x4004ec <main+40>: callq 0x4003b8 <printf@plt>
0x4004f1 <main+45>: mov $0x0,%eax
0x4004f6 <main+50>: leaveq
0x4004f7 <main+51>: retq
------------------------------------------------------------
$pc 指向当前程序运行地址
------------------------------------------------------------
(gdb) x/5i $pc
=> 0x4004d3 <main+15>: movl $0x4,-0x4(%rbp)
0x4004da <main+22>: mov $0x4005f8,%eax
0x4004df <main+27>: mov -0x4(%rbp),%edx
0x4004e2 <main+30>: mov %edx,%esi
0x4004e4 <main+32>: mov %rax,%rdi
(gdb)
--------------------------------------------------------------------------------
用gdb 调试汇编代码(二进制代码).
查看:
disassembler $pc
display/i $pc
x/i $pc
执行: 单指令。
ni;
si:
用 p $eax
p $edi 等可以查看寄存器.
在gdb中 敲入help layout
(gdb) help layout
Change the layout of windows.
Usage: layout prev | next | <layout_name>
Layout names are:
src : Displays source and command windows.
asm : Displays disassembly and command windows.
split : Displays source, disassembly and command windows.
regs : Displays register window. If existing layout
is source/command or assembly/command, the
register window is displayed. If the
source/assembly/command (split) is displayed,
the register window is displayed with
the window that has current logical focus.
然后你可以用layout 去调试,很方便, 例如 layout asm.
c++ 单步查看汇编代码【转】的更多相关文章
- 【JVM】-NO.110.JVM.1 -【hsdis jitwatch 生成查看汇编代码】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- 在VS如何查看汇编代码
由于最近不常用,结果导致今天用的时候忘记了,╮(╯▽╰)╭.现在标记一下: 方法如下,先创建一个C++ Project,然后加入上面的代码,在main函数或者其他地方设置断点,注意是Debug版本,否 ...
- 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码
浅析VS2010反汇编 2015年07月25日 21:53:11 阅读数:4374 第一篇 1. 如何进行反汇编 在调试的环境下,我们可以很方便地通过反汇编窗口查看程序生成的反汇编信息.如下图所示. ...
- 使用WinDbg获得托管方法的汇编代码
概述:有时候,我们需要查看一个托管方法的汇编指令是怎么样的.记得在大学的时候,我们使用gcc -s和objdump来获得一个c程序代码的汇编指令.但是对于.NET程序来说,我们肯定无法轻松地获得这些内 ...
- 使用hsdis查看jit生成的汇编代码
http://blog.csdn.net/unei66/article/details/26477629 JVM 有 HotSpot引擎可以对热代码路径进行有效的 JIT优化,大幅度提升计算密集代码 ...
- 查看Java代码对应的汇编指令又一利器,JITWatch 转
http://www.tuicool.com/articles/IRrIRb3 时间 2015-05-13 08:00:00 Liuxinglanyue's Blog 原文 http://java ...
- 20145311利用gdb调试汇编代码
利用GDB调试汇编代码 首先编写c语言原代码,我使用的是同学分析过的代码 #include<stdio.h>short addend1 = 1;static int addend2 = 2 ...
- 在汇编代码中调用C函数
对于ARM体系来说,不同语言撰写的函数之间相互调用(mix calls)遵循的是 ATPCS(ARM-Thumb Procedure Call Standard),ATPCS主要是定义了函数呼叫时参数 ...
- 程序编码(机器级代码+汇编代码+C代码+反汇编)
[-1]相关声明 本文总结于csapp: 了解详情,或有兴趣,建议看原版书籍: [0]程序编码 GCC调用了一系列程序,将源代码转化成可执行代码的流程如下: (1)C预处理器扩展源代码,插入所有用#i ...
随机推荐
- 初步了解学习将传统单机应用改造成Dubbo服务的过程
Dubbo作为RPC框架,实现的效果就是调用远程的方法就像在本地调用一样.如何做到呢?就是本地有对远程方法的描述,包括方法名.参数.返回值,在Dubbo中是远程和本地使用同样的接口:然后呢,要有对网络 ...
- 详解Paste deploy
原创作品,转载注明本文出处:http://www.cnblogs.com/Security-Darren/p/4087587.html 谈到WSGI,就免不了要了解paste,其中paste depl ...
- ant学习笔记-taskdef
1.声明task jar包中指定的task <taskdef name="xmltask“ classname="com.oopsconsultancy.xmltask.an ...
- Java 注解 (Annotation)
Java 注解用于为 Java 代码提供元数据.作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的.Java 注解是从 Java5 开始添加到 Java 的. 注解语法 ...
- springboot+shiro+redis(单机redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+shiro+redis(单机red ...
- Jenkins入门知识
1 修改jenkins的根目录,默认地在C:\Documents and Settings\AAA\.jenkins . .jenkins ├─jobs│ └─JavaHelloWorld│ ...
- Ubuntu下使用dialog制作菜单执行简单脚本
新建test5 #!/bin/bash #using select in the menu temp=$(mktemp -t test.XXXXXX) temp2=$(mktemp -t test2. ...
- 最简单安装laravel
http://laravelacademy.org/resources-download
- nginx配置设置,使部分页面访问跳转到404页面
location ~* /(ask|hospital|wenda|regsearch|user|doctor) { return ; } error_page /.html;
- php单元测试断言方法
1.assertArrayHasKey() 用法:$this->assertArrayHasKey('foo', ['bar' => 'baz']); 等同于array_key_exist ...