使用LLVM传递加载和存储变量

我正在尝试这样做:

1-插入一个变量t1。

2-向chrono功能插入呼叫(以获得时间)。

3-在t1中存储计时器的返回值

4-将一个调用插入到“函数”(我写的一个函数)并传递t1作为参数。 函数对t1进行一些计算。

在代码中它是:

float t1 = std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count());
function(t1);

不过,我想在我想修改的程序中插入LLVM pass。 我不确定如何去做,但我的想法是:

Constant *TFunc = M.getOrInsertFunction("std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count())", Type::getFloatTy(M.getContext()),NULL);
tfunc = cast<Function>(TFunc);
for (*certain type of instructions Inst*){
    CallInst *CurrInst = dyn_cast<CallInst>(Inst);
    AllocaInst *Talloc = new AllocaInst(Type::getFloatTy((*Inst).getContext()),"t1");
    Instruction *Tcall = CallInst::Create(tfunc,"");
    StoreInst* store_t = new StoreInst(Tcall,Talloc,(Instruction*)CurrInst);
    if(storeT != NULL) {
        LoadInst* loadT = new LoadInst(storeT,"",false);
        Value* t1 = loadT;
        Instruction *newInst = CallInst::Create(hook,loadT, "");
        Inst->getInstList().insert((Instruction*)CurrInst, newInst);
    }
}

我得到的错误是:

0 libLLVM-3.4.so.1 0x40f8150f llvm :: sys :: PrintStackTrace(_IO_FILE *)+ 47 1 libLLVM-3.4.so.1 0x40f8177f 2 libLLVM-3.4.so.1 0x40f812ec 3
0x40022400 __kernel_sigreturn + 0 4 libLLVM-3.4.so.1 0x40864ee7 llvm :: LoadInst :: LoadInst(llvm :: Value *,char const *,bool,llvm :: Instruction *)+ 71 5 pass.so 0x400265b2堆栈转储:0程序参数:/ usr / bin / clang -cc1 -triple i386-pc-linux-gnu -emit-obj -disable-free -disable -llvm-verifier -main-file-name mtd.cc -mrelocation-model pic - pic-level 2 -fmath-errno -masm -verbose -mconstructor-aliases -fuse-init-array -target -cpu pentium4 -target-linker-version 2.24 -momit-leaf -frame-pointer -g -coverage-file /。 ./mtd.o -resource-dir /usr/bin/../lib/clang/3.4 -dependency-file .deps / mtd.d -MT mtd.o -sys-header-deps -MP -include config.h -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I / USR / lib / llvm-3.4 / include -I /usr/lib/llvm-3.4/include -I / usr /lib/llvm-3.4/include -internal-isystem /usr/include//c++/4.8 -internal-isystem /usr/include//c++/4.8/i386-linux-gnu -internal-isystem / usr / include // c ++ / 4.8 / backward -internal-isystem /usr/include//i386-linux-gnu/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/ .. /../../../include/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../ .. /include/c++/4.8/i686-linux-gnu -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/ c ++ / 4.8 / backward -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/i686-linux-gnu/c++ /4.8 -internal-isystem / usr / local / include -internal-isystem /usr/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/bin/../lib/gcc/i686 -linux-gnu / 4.8 / include -internal-externc-isystem / usr / include / i386-linux-gnu -internal-externc-isystem / include -internal-externc-isystem / usr / include -O3 -Woverloaded-virtual -Wcast -qual -W -Wall -w -std = c ++ 11 -fdeprecated-macro -fdebug-compilation-dir /.../ma sstree-beta-master -ferror-limit 19 -fmessage-length 80 -fvisibility-inlines-hidden -mstackrealign -fobjc-runtime = gcc -fdiagnostics -show -option -fcolor-diagnostics -vectorize-loops -vectorize -slp -load / ../pass.so -o mtd.o -x c ++ mtd.cc 1.文件末尾的解析器2.每个模块的优化通过3.在模块“mtd.cc”上运行pass'Synchronization profiler'。 铛:错误:无法执行命令:分段错误(核心转储)铛:错误:铛前端命令由于信号失败(使用-v来查看调用)Ubuntu铿锵声3.4-1ubuntu3版(标签/ RELEASE_34 / final) LLVM 3.4)目标:i386-pc-linux-gnu线程模型:posix clang:注意:诊断信息:请向http://bugs.debian.org/提交错误报告,并包括崩溃回溯,预处理源和关联运行脚本。 铛声:注意:诊断味精:


请将以下文件附加到BUG报告中:预处理的源代码和相关的运行脚本位于:clang:note:diagnostic msg:/tmp/mtd-7d9a20.cpp clang:note:diagnostic msg:/ tmp /mtd-7d9a20.sh clang:注意:诊断信息:

******************** make:*** [mtd.o]错误254

除了制作上面的代码外,阅读文档对我来说帮不了多少忙。 我有以下问题:

1-我的密码有什么问题? 换句话说,如何添加我想要的代码?

2-这个错误是什么意思? 我看不到任何有意义的消息,除了出现分段错误并且命令无法执行(可能是因为我是新手?)。

3-我知道如何插入调用函数,我在另一个C ++文件(如“函数”)中写入函数,但不是函数,而是函数库中定义的函数,例如chrono函数,这就是为什么我写了“std :: chrono: :duration_cast

如果我的问题太基本,请抱歉。 帮助和指导将不胜感激!


当LLVM中的指令从Value继承时,在将函数作为调用指令插入后,您可以将该指令传递给下一个函数。 无需您正在尝试执行的加载存储操作。 所以调用你的时间函数,并将你用来调用它的指令对象传给“函数”。

链接地址: http://www.djcxy.com/p/68699.html

上一篇: Loading and storing variables using an LLVM pass

下一篇: Printf float with LLVM segfaults