LLVM如何获取呼叫站点文件名和行号 [英] LLVM how to get callsite file name and line number

查看:170
本文介绍了LLVM如何获取呼叫站点文件名和行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对LLVM非常陌生,这是我第一次编写C ++

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

我需要找到一些与LLVM CallSite相关的功能信息,但是,我已经在此处检查了源代码: LLVM CallSite源代码

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

仍然不知道从何处获得呼叫站点文件名(例如,CallSite在example.c文件中),呼叫站点行号(例如,在第整个程序中18个)

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?

推荐答案

您可以通过从调用的函数中获取调试信息来获取此信息.算法如下:

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

  1. 您需要获取称为函数的基础值.
  2. 然后,您需要获取附加到该功能的调试信息.
  3. 调试信息应包含您需要的所有内容.

这是应该完成此工作的代码(虽然我没有运行它):

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.

这篇关于LLVM如何获取呼叫站点文件名和行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆