LLVM how to get callsite file name and line number

我只是一个虾纸丫 提交于 2019-12-22 18:43:00

问题


I am very very new to LLVM, and it's my first time to write C++

I need to find several function info related to LLVM CallSite, however, I have checked the source code here: LLVM CallSite Source Code

Still don't know where to get call site file name (eg. CallSite is in example.c file), call site line number (eg. at line 18 in the whole program)

Do you know how can I get call site file name and line number?


回答1:


You can get this information by retrieving debug information from the called function. The algorithm is the following:

  1. You need to get underlying called value, which is a function.
  2. Then you need to get debug information attached to that function.
  3. The debug information should contain everything you need.

Here is a code that should do the job (I didn't run it though):

CallSite cs = ...;
if (!cs.isCall() && !cs.isInvoke()) {
  break;
}

Function *calledFunction = dyn_cast<Function>(cs.getCalledValue());
if (!calledFunction) {
  break;
}

MDNode *metadata = calledFunction->getMetadata(0);
if (!metadata) {
  break;
}

DILocation *debugLocation = dyn_cast<DILocation>(metadata);
if (debugLocation) {
  debugLocation->getFilename();
  debugLocation->getLine();
}

Please note the breaks. They are here to show that every step may not succeed, so you should be ready to handle all such cases.



来源:https://stackoverflow.com/questions/41714172/llvm-how-to-get-callsite-file-name-and-line-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!