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’文件部分截图
使用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.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盘就可以找到这个图片了