Skip to content

Commit

Permalink
Locating and injecting externally linked function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel-Durov committed Dec 7, 2023
1 parent 3f61988 commit 7865b74
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions llvm/lib/Transforms/Yk/SoftwareTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,37 @@
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

#define DEBUG_TYPE "yk-software-tracer"
#define DEBUG_TYPE "yk-software-tracer-pass"

using namespace llvm;

namespace {
// void my_function() { printf("Hello from your_c_function!\n"); }

struct SoftwareTracerPass : public ModulePass {
static char ID;
Function *monitor;
Function *externalFunc = NULL;

SoftwareTracerPass() : ModulePass(ID) {}

bool runOnModule(Module &M) override {
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
llvm::StringRef ref = std::string("record_trace");
Function *calleeFunction =
M.getFunction(ref); // Find the function by name

LLVMContext &Context = M.getContext();
if (externalFunc == NULL) {
FunctionType *FType =
FunctionType::get(Type::getVoidTy(Context), {}, false);
externalFunc = Function::Create(FType, GlobalVariable::ExternalLinkage,
YK_TRACE_FUNCTION, M);
}
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
++BI) {
if (calleeFunction) {
Argument *TempArg = F->getArg(0); // temp argument
Value *args[] = {TempArg}; // BI->getName()

Instruction *newInst = CallInst::Create(calleeFunction, args);

BasicBlock *BB = BI->getParent();
// TODO: understand how to insert new instruction
// Instruction::insertInto(BB, newInst); // works with iterator
// BB->getInstList().insert(BI, newInst); // deal with getInstList
// being private errs() << "Inserted the function!\n";
}
// Insert function call instruction
IRBuilder<> builder(Context);
builder.SetInsertPoint(&(*BI));
builder.CreateCall(externalFunc);
}
}
}

return true;
}
};
Expand Down

0 comments on commit 7865b74

Please sign in to comment.