问题
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:
- You need to get underlying called value, which is a function.
- Then you need to get debug information attached to that function.
- 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