LLVM how to get callsite file name and line number

淺唱寂寞╮ 提交于 2019-12-06 07:10:49

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.

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