生成包含IR的CFG-小例子

IR:(Intermediaterepresentation)LLVM 中间表示,是一种LLVM的中间语言。
IR 的三种表现形式:二进制文件.bc、人类可阅读文件.ll、内存的表达;
CFG:(Control flow graph)控制流图

我们先来写一个小例子:test.c:

#linux下code
vim test.c
#include <stdio.h>
int main(void)
{
        int i,j,n;
        j=1;
        n=3;
        for(i=0;i<n;i++) {
        	printf("%d",j);
            j++;
            if(j>9)
            	j-=10;
        }
        printf("\n");
        return 0;
}

这是一个循坏,为了增加复杂度,我加了一个 if 语句。

#用gcc(或clang)来编译一下,看看运行结果

$ gcc -o test test.c
$ clang -o test test.c
#生成 test 可执行文件
$ ./test
#运行结果:
123

可用llvm来生成IR

clang -c -emit-llvm test1.c -o test1.bc 编译产生字节码
clang -S -emit-llvm test.c -o test.ll 编译产生可视化字节码

生成“.ll”文件

#linux下code
clang -S -emit-llvm test.c -o test.ll

生成的’.ll’文件部分截图
部分'.ll'截图

使用opt调用pass
LLVM-PASS官网地址:

opt -dot-dom test.ll

生成 dom.main.dot 文件

根据 dom.main.dot 文件生成CFG

dot -Tpng -o test.png dom.main.dot

就出现了
test图

打开test.png
test.png

你可以选择将linux下的.png图片传到windows上打开
这里我提供一个python脚本

# -*- coding:utf-8 -*-
#初始化参数:
host = "10.3.0.207"
port = 22
timeout = 30
user = "root"
password = "******"

#Paramiko下载文件
import paramiko
def sftp_down_file(server_path, local_path):
    try:
        t = paramiko.Transport((host, 22))
        t.connect(username=user, password=password)
        sftp = paramiko.SFTPClient.from_transport(t)
        sftp.get(server_path, local_path)
        t.close()
    #python2.*版本
    except Exception, e:
        print e
    '''
    #python3.*版本
    except Exception as error:
        print (error)
    '''
if __name__ == '__main__':
    sftp_down_file("/root/test.png", "D:/test.png")
    
#linux下图片路径可用 "pwd" 命令来查看

在windows下 D盘就可以找到这个图片了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值