SVF-tools / SVF

Static Value-Flow Analysis Framework for Source Code
http://svf-tools.github.io/SVF/
Other
1.42k stars 436 forks source link

debug info problem #129

Open luojiaqs opened 5 years ago

luojiaqs commented 5 years ago

Hello, I'm new in LLVM, and I want to use svf to do some static analysis. I want to find where a struct is defined, Loc and source file. just like function's dbginfo.

<func1> Source Loc: in line: 24 file: test.c 

but I cannot find any interface to get such information. How can I get such information with svf ? Apologize for my little stupid problem. ?

yuleisui commented 5 years ago

Hello,

First, no question is stupid:)

Second, please refer to https://github.com/SVF-tools/SVF/blob/206493694931caa8ff3133191e7a4dbb7832fd89/lib/Util/AnalysisUtil.cpp#L301

to understand printing the debug information of LLVM IR.

luojiaqs commented 5 years ago

Hello,

First, no question is stupid:)

Second, please refer to https://github.com/SVF-tools/SVF/blob/206493694931caa8ff3133191e7a4dbb7832fd89/lib/Util/AnalysisUtil.cpp#L301

to understand printing the debug information of LLVM IR.

Thanks for your help. I had read analysisUtil::getSourceLoc before but give little attention ><

Basically, a struct type always a DICompositeType type, and I just didn't know how to access to it.

Use FindDbgAddrUses function I can get all @llvm.dbg.declare inst. and I find DICompositeType in DIVar->getOperand(3)

sample code

 bool instAnalysis (Instruction *inst){
       if (isa<AllocaInst>(inst)) {
           for (DbgInfoIntrinsic *DII : FindDbgAddrUses(const_cast<Instruction*>(inst))) {
                if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(DII)) {
                    DIVariable *DIVar = cast<DIVariable>(DDI->getVariable());
                    errs() << "num operand : " << DIVar->getNumOperands() << "\n";
                    if(DICompositeType *pp =dyn_cast<DICompositeType>(DIVar->getOperand(3))){

                        errs() << pp->getLine() << " : " << pp->getFilename() << "\n";
                        errs() << pp->getElements().size() << "\n";
                        pp->getElements()[0]->dump();

                    }

                }
           }    
         return false;
}