[英]How to get the referencing function pointer in LLVM IR?
我正在編寫一個 LLVM 模塊傳遞,它將獲得pthread_create
的第三個參數。 根據手冊頁pthread_create
的函數原型是
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
例如我的目標 C 源代碼如下。
void *pthread_task(void *args) {
sleep(4);
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t tid;
pthread_create(&tid, NULL, pthread_task, NULL);
pthread_join(tid, NULL);
return 0;
}
我想要輸出“pthread_task”。 我能夠訪問正確的函數並列出所有參數。 但是,由於第三個參數是函數指針,我不知道如何訪問它指向的函數。
我還附上了我如何實現我的模塊傳遞如下。
namespace {
~ int32_t indexOfPthreadCreate(CallGraphNode *node) {
~ for (int32_t i = 0; i < node -> size(); i++) {
~ Function *f = (*node)[i] -> getFunction();
~ if (f && f -> getName().compare("pthread_create") == 0) {
+ for (auto &A: f->args()) {
// I would like to access the third parameter of pthread_create here.
+ A.print(errs());
+ }
+ return i;
+ }
}
~ return -1;
}
struct PthreadScopeDetectPass : public ModulePass {
static char ID;
PthreadScopeDetectPass() : ModulePass(ID) { }
bool runOnModule(Module &M) override {
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
uint32_t nSCC = 0;
for (scc_iterator<CallGraph *> iterSCC = scc_begin(&CG); !iterSCC.isAtEnd(); ++iterSCC) {
auto nodes = *iterSCC;
for (CallGraphNode *node: nodes) {
Function *currFunc = node -> getFunction();
~ int32_t target_i = indexOfPthreadCreate(node);
~ if (target_i > 0 && currFunc) {
+ insertTimer(currFunc);
}
}
}
return true;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<CallGraphWrapperPass>();
}
};
}
感謝@arnt 的評論。 我已經解決了這個問題。 這是我解決它的方法。
~ int32_t indexOfPthreadCreate(CallGraphNode *node) {
~ for (int32_t i = 0; i < node -> size(); i++) {
~ Function *f = (*node)[i] -> getFunction();
~ if (f && f -> getName().compare("pthread_create") == 0) {
+ for (auto &inst: instructions(f)) {
~ if (inst.getOpcode() == 56) {
~ CallInst *ci = dyn_cast<CallInst>(&inst);
~ if (ci && ci->getCalledFunction()->getName().compare("pthread_create") == 0) {
~ Function *pthread_task = dyn_cast<Function>(ci->getArgOperand(2));
~_ errs() << "pthread is going to execute " << pthread_task->getName() << "function \n";
}
+ }
+ A.print(errs());
+ }
+ return i;
+ }
}
~ return -1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.