Variable name from metadata

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable name from metadata

Pankaj Gode
Hi All,
 
Can we extract name of variable name from "MDNode" ?
 
1. Neither temp_MDNode->getName() nor temp_MDNode->getValueName() give me "global_int" which is name of a variable.
 
2. I tried below ways as well.
DIVariable DV(mdnode1);
Value *v = mdnode1->getOperand(0);//gives add 0x69
 
3. I have written below code to reach till variable name.
 
     LLVMIname is
 
              const CallInst *CI = dyn_cast<CallInst>(Insn);
              const Value* LLVMIValue  = cast<CallInst>(Insn)->getCalledValue();
              const Function *LLVMIFunc= dyn_cast<Function>(LLVMIValue);
             
              if( LLVMIname.compare("llvm.dbg.declare") == 0 )
                            {
                               int x = 0;
                int numoperands = CI->getNumArgOperands();
                for( unsigned i = 0; i != numoperands; ++i)
                {
                  Value *v = CI->getOperand(i);
                  if( i == 1) //points to metadata for variable name
                  {
                    MDNode *mdnode = (MDNode *)CI->getOperand(1);
                     int numoperds= mdnode->getNumOperands();//4 operands
                     for( unsigned x = 0; x != numoperds; ++x)
                     {
                        Value *v = mdnode->getOperand(x);
                        if( x == 2)//name of variable
                        {
                           MDNode *mdnode1 = (MDNode *)mdnode->getOperand(2);//mdnode1->dump() gives variable name.
                           std::pair<unsigned, MDNode *> mynode( Insn->getDebugLoc().getLine(), mdnode1);
                           linenum_varmdnode.push_back( mynode);
                       
                           //DIVariable DV(mdnode1);//not working
                           //Value *v = mdnode1->getOperand(0);//, not working. gives add 0x69
                                               }
                     }//for operands mdnode
                  }                  
                }//for operands mdnode
3. When I give "temp_MDNode->dump()" on gdb, it displays below output ==> 
metadata !"global_int"
If I can collect this in a string, that too is OK for me.
 
 
Thanks & Regards,
Pankaj
 

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Variable name from metadata

Pankaj Gode
Closing this, found below links to get variable name.
 
 
 
        I could do following, to get the required information.
                const CallInst *CI = dyn_cast<CallInst>(Insn);
                int numoperands = CI->getNumArgOperands();
                for( unsigned i = 0; i != numoperands; ++i)
                {
                  Value *v = CI->getOperand(i);
                  MDNode *temp = (MDNode *)CI->getOperand(1) ;
                  DIVariable DV(temp);//TO CHECK. remove later
                  StringRef varName = DV.getName();
                  const char *tmpvarname = varName.data();
                  if( i == 1) //points to metadata for variable name
                  {
 
Sorry for posting trivial question.
 
Regards,
Pankaj
 

From: Pankaj Gode <[hidden email]>
To: llvm Developers <[hidden email]>
Sent: Monday, October 17, 2011 5:38 PM
Subject: [LLVMdev] Variable name from metadata

Hi All,
 
Can we extract name of variable name from "MDNode" ?
 
1. Neither temp_MDNode->getName() nor temp_MDNode->getValueName() give me "global_int" which is name of a variable.
 
2. I tried below ways as well.
DIVariable DV(mdnode1);
Value *v = mdnode1->getOperand(0);//gives add 0x69
 
3. I have written below code to reach till variable name.
 
     LLVMIname is
 
              const CallInst *CI = dyn_cast<CallInst>(Insn);
              const Value* LLVMIValue  = cast<CallInst>(Insn)->getCalledValue();
              const Function *LLVMIFunc= dyn_cast<Function>(LLVMIValue);
             
              if( LLVMIname.compare("llvm.dbg.declare") == 0 )
                            {
                               int x = 0;
                int numoperands = CI->getNumArgOperands();
                for( unsigned i = 0; i != numoperands; ++i)
                {
                  Value *v = CI->getOperand(i);
                  if( i == 1) //points to metadata for variable name
                  {
                    MDNode *mdnode = (MDNode *)CI->getOperand(1);
                     int numoperds= mdnode->getNumOperands();//4 operands
                     for( unsigned x = 0; x != numoperds; ++x)
                     {
                        Value *v = mdnode->getOperand(x);
                        if( x == 2)//name of variable
                        {
                           MDNode *mdnode1 = (MDNode *)mdnode->getOperand(2);//mdnode1->dump() gives variable name.
                           std::pair<unsigned, MDNode *> mynode( Insn->getDebugLoc().getLine(), mdnode1);
                           linenum_varmdnode.push_back( mynode);
                       
                           //DIVariable DV(mdnode1);//not working
                           //Value *v = mdnode1->getOperand(0);//, not working. gives add 0x69
                                               }
                     }//for operands mdnode
                  }                  
                }//for operands mdnode
3. When I give "temp_MDNode->dump()" on gdb, it displays below output ==> 
metadata !"global_int"
If I can collect this in a string, that too is OK for me.
 
 
Thanks & Regards,
Pankaj
 

_______________________________________________
LLVM Developers mailing list
[hidden email]        http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Variable name from metadata

Devang Patel
Pankaj,

You could simply do

if (const CallInst *CI = dyn_cast<CallInst>(Insn))
if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(Insn)) {
DIVariable DV = DDI->getVariable();
StringRef VarName = DV.getName();
}
Now, StringRef has various operators so you could most likely use it directly.
-
Devang

On Oct 17, 2011, at 5:47 AM, Pankaj Gode wrote:

Closing this, found below links to get variable name.
 
 
 
        I could do following, to get the required information.
                const CallInst *CI = dyn_cast<CallInst>(Insn);
                int numoperands = CI->getNumArgOperands();
                for( unsigned i = 0; i != numoperands; ++i)
                {
                  Value *v = CI->getOperand(i);
                  MDNode *temp = (MDNode *)CI->getOperand(1) ;
                  DIVariable DV(temp);//TO CHECK. remove later
                  StringRef varName = DV.getName();
                  const char *tmpvarname = varName.data();
                  if( i == 1) //points to metadata for variable name
                  {
 
Sorry for posting trivial question.
 
Regards,
Pankaj
 

From: Pankaj Gode <[hidden email]>
To: llvm Developers <[hidden email]>
Sent: Monday, October 17, 2011 5:38 PM
Subject: [LLVMdev] Variable name from metadata

Hi All,
 
Can we extract name of variable name from "MDNode" ?
 
1. Neither temp_MDNode->getName() nor temp_MDNode->getValueName() give me "global_int" which is name of a variable.
 
2. I tried below ways as well.
DIVariable DV(mdnode1);
Value *v = mdnode1->getOperand(0);//gives add 0x69
 
3. I have written below code to reach till variable name.
 
     LLVMIname is
 
              const CallInst *CI = dyn_cast<CallInst>(Insn);
              const Value* LLVMIValue  = cast<CallInst>(Insn)->getCalledValue();
              const Function *LLVMIFunc= dyn_cast<Function>(LLVMIValue);
             
              if( LLVMIname.compare("llvm.dbg.declare") == 0 )
                            {
                               int x = 0;
                int numoperands = CI->getNumArgOperands();
                for( unsigned i = 0; i != numoperands; ++i)
                {
                  Value *v = CI->getOperand(i);
                  if( i == 1) //points to metadata for variable name
                  {
                    MDNode *mdnode = (MDNode *)CI->getOperand(1);
                     int numoperds= mdnode->getNumOperands();//4 operands
                     for( unsigned x = 0; x != numoperds; ++x)
                     {
                        Value *v = mdnode->getOperand(x);
                        if( x == 2)//name of variable
                        {
                           MDNode *mdnode1 = (MDNode *)mdnode->getOperand(2);//mdnode1->dump() gives variable name.
                           std::pair<unsigned, MDNode *> mynode( Insn->getDebugLoc().getLine(), mdnode1);
                           linenum_varmdnode.push_back( mynode);
                       
                           //DIVariable DV(mdnode1);//not working
                           //Value *v = mdnode1->getOperand(0);//, not working. gives add 0x69
                                               }
                     }//for operands mdnode
                  }                  
                }//for operands mdnode
3. When I give "temp_MDNode->dump()" on gdb, it displays below output ==> 
metadata !"global_int"
If I can collect this in a string, that too is OK for me.
 
 
Thanks & Regards,
Pankaj
 

_______________________________________________
LLVM Developers mailing list
[hidden email]        http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev