[LLVMdev] How to get line number of a function in a bitcode file?

Lu Zhao luzhao at cs.utah.edu
Thu Jul 21 11:15:58 PDT 2011


Sorry, I directly copied the code from my file without editing it.

"km->module" in the code is a pointer to a Module, which contains all 
named metadata.


I'm using the following code to get source information about functions 
without using an instruction of the function, and it seems to work well.

     // Get metadata about functions (subprograms)
     if (NamedMDNode *namedMD = 
km->module->getNamedMetadata("llvm.dbg.sp")) {
       for (unsigned i = 0, e = namedMD->getNumOperands(); i != e; ++i) {
         MDNode *mdnode = namedMD->getOperand(i);
         DIDescriptor diDesc(mdnode);
         if (!diDesc.isSubprogram())
           continue;

         DISubprogram subProg(mdnode);
         unsigned line = subProg.getLineNumber();
         StringRef dir = subProg.getDirectory();
         StringRef file = subProg.getFilename();
         std::cout << "Function name: "  << subProg.getName().str()
                   << " at line "
                   << line << " in " << dir.str() << file.str() << "\n";
       }
     }


The way that elements of a metadata node is called "operands" confused 
me very much. After I figured it out that it actually means a referred 
(sub-)node or (sub-)element of a node, it's really straightforward to 
use metadata information.

Lu

On 07/08/2011 02:09 PM, Devang Patel wrote:
> Hi Chen,
>
> On Jul 8, 2011, at 11:56 AM, kobe James wrote:
>
>> Hi All,
>>
>> I hope to get some information about functions in a bitcode file. Say,
>> if we have a function foo(), and the bitcode file is generated by a
>> single source file, then I want to get the line number foo() locates
>> in that source file.
>> If the bitcode file is linked by multiple bitcode files, is it
>> possible to also get which file foo() locates in?
>
>
> If bitcocd file has debug info, it is possible however there is not any
> LLVM IR ready made function to get this info. It is easy to write one.
>
> -
> Devang
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



More information about the llvm-dev mailing list