Hi, I was looking at how to use debug info to find out original source:line for a certain function/variable in the llvm bytecode. I read http://llvm.org/docs/SourceLevelDebugging.html, and then I have looked in lib/Debugger/ProgramInfo.cpp. getFunction() looks useful, however I am not sure how to call it. I have a reference to a Function, however getFunction() takes a GlobalVariable. I tried creating a GlobalVariable that points to my Function, but that didn't work, since getFunction() is looking at uses of the GlobalVariable, which in my case was none. Am I missing something obvious here? How do I find out source:line for an LLVM Function? (assuming original file was compiled with debug info, and bitcode has debug info) Or is it just easier to iterate on the Function's BasicBlocks, and look for llvm.dbg.* instrinsics? As you might have guessed from the subject, I want to use this in a static analysis tool (that works on llvm bitcode), and it would be useful if it could provide the user with nice messages, that include references to the original source file:line, possibly with original types and variable names. All this seems to be available in the debug info (as generated by llvm-gcc -O0 -g), but I couldn't find functions to query for it. I plan on implementing some utility classes for my project, to make it easier to query for such information, and I'd like it to design it in a way that could be useful for LLVM too. Suggestions? Best regards, --Edwin
Prabhat Kumar Saraswat
2007-Dec-25 15:13 UTC
[LLVMdev] Using debug info in static analysis
Hi edwin, I am working on a similar project and some of my suggestions regarding your approach are inlined with this email.. :) read below. On Dec 25, 2007 2:56 PM, Török Edwin <edwintorok at gmail.com> wrote:> Hi, > > I was looking at how to use debug info to find out original source:line > for a certain function/variable in the llvm bytecode. > > I read http://llvm.org/docs/SourceLevelDebugging.html, and then I have > looked in lib/Debugger/ProgramInfo.cpp. > getFunction() looks useful, however I am not sure how to call it. I have > a reference to a Function, however getFunction() takes > a GlobalVariable. I tried creating a GlobalVariable that points to my > Function, but that didn't work, since getFunction() is looking > at uses of the GlobalVariable, which in my case was none.It could be better to just iterate over the functions using the runOnFunction pass on the bitcode file and then use the getName() method on the returned objects to find the name of the function (as in the high level language)> > > Am I missing something obvious here? How do I find out source:line for > an LLVM Function? (assuming original file was compiled with debug info, > and bitcode has debug info)It could be possible to do so using the debug info in the file, i have tried it but without any luck. Another approach i have tried and has worked out can be found at my earlier posting at this link http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-December/011892.html> > Or is it just easier to iterate on the Function's BasicBlocks, and look > for llvm.dbg.* instrinsics? > > As you might have guessed from the subject, I want to use this in a > static analysis tool (that works on llvm bitcode), > and it would be useful if it could provide the user with nice messages, > that include references to the original source file:line, possibly with > original types and variable names.> > > All this seems to be available in the debug info (as generated by > llvm-gcc -O0 -g), but I couldn't find functions to query for it. > I plan on implementing some utility classes for my project, to make it > easier to query for such information, and I'd like it to design it in a > way that could be useful for LLVM too. Suggestions? > > Best regards, > --EdwinGetting function level information seems to be easier, but it would be quite interesting to see how one can map the information in the bitcode file (along with the debug info) to the source lines in the high level language. Best of luck and best regards Prabhat> > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20071225/dc256b13/attachment.html>
On Dec 25, 2007, at 5:56 AM, Török Edwin wrote:> I was looking at how to use debug info to find out original > source:line > for a certain function/variable in the llvm bytecode. > > I read http://llvm.org/docs/SourceLevelDebugging.html, and then I have > looked in lib/Debugger/ProgramInfo.cpp. > getFunction() looks useful, however I am not sure how to call it. I > have > a reference to a Function, however getFunction() takes > a GlobalVariable. I tried creating a GlobalVariable that points to my > Function, but that didn't work, since getFunction() is looking > at uses of the GlobalVariable, which in my case was none. > > Am I missing something obvious here? How do I find out source:line for > an LLVM Function? (assuming original file was compiled with debug > info, > and bitcode has debug info)You can only get the location from a stoppoint intrinsic. Just walk the function looking for the first stoppoint in it. From that you can get the descriptor containing the file/line#. See the llvm programmer's manual for info on how to walk the IR. -Chris
Chris Lattner wrote:> You can only get the location from a stoppoint intrinsic. Just walk > the function looking for the first stoppoint in it. From that you can > get the descriptor containing the file/line#. See the llvm > programmer's manual for info on how to walk the IR. > >Thanks, it works perfectly. I have created a FunctionPass that just extracts file/line info, and I can easily use this from my other passes, via getAnalysis<>(func). Prabhat Kumar Saraswat wrote:> Hi edwin, > I am working on a similar project and some of my suggestions > regarding your approach are inlined with this email.. :) read below. > > It could be possible to do so using the debug info in the file, i have > tried it but without any luck. Another approach i have tried and has > worked out can be found at my earlier posting at this link > http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-December/011892.htmlI figured out how to use debug info to recover original variable name/type. You have to iterate on the variable's def-use chain looking for bitcast instructions, iterate its def-use chain till you find a DbgDeclareInst, which has a getVariable() method. You can extract debug info from it using DIDeserializer::Deserialize from MachineModuleInfo.h. You'll get a VariableDesc that has everything you need about location/name/type etc. I can get output like: function:main (at /tmp//x.c:4) variable x (at /tmp//x.c:5) of type oint (at /tmp//x.c:2) variable x (at /tmp//x.c:7) of type _Bool (in /tmp//x.c) variable tmp (Unknown location) of type i32 * (Unknown location) variable retval (at /tmp//x.c:4) of type int (in /tmp//x.c) for this simple test: #include <stdbool.h> typedef int oint; int main() { oint x = 15; { bool x = false; } return 0; } Best regards and thanks for all the help, Edwin