get function local debug info?

Hi,

If I have an instance of DISubprogram, can I get the debug info of local variables of the function, including parameters?

I tried to use the getVariables() function defined in DISubprogram, but it seemed to return an empty DIArray node when I ran my pass alone using opt. Do I need to enable any other analysis passes in order to populate the data?

My related snippet of code is like the following:

NamedMDNode *M_Nodes = M.getNamedMetadata(“llvm.dbg.cu”);
for (unsigned i = 0, e = M_Nodes->getNumOperands(); i != e; ++i) {
DIArray SPs = CU.getSubprograms();
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++ i) {
DISubprogram SP(SPs.getElement(i));
DIArray Vars = SP.getVariables();
for (unsigned i2 = 0, e2 = Vars.getNumElements(); i2 != e2; ++i2) {
DIVariable DV(Vars.getElement(i));
DV.print(errs()); errs() << " : "; DV.getType().dump();
}
}
}

In addition, can I use DebegInfo to get the list of parameters (var names and types) of a subprogram? or I have to parse the underlying metadata and build the relationship?

Thanks,
Lu

Hi,

If I have an instance of DISubprogram, can I get the debug info of local
variables of the function, including parameters?

I tried to use the getVariables() function defined in DISubprogram, but it
seemed to return an empty DIArray node when I ran my pass alone using opt.
Do I need to enable any other analysis passes in order to populate the data?

My related snippet of code is like the following:

    NamedMDNode *M_Nodes = M.getNamedMetadata("llvm.dbg.cu");
    for (unsigned i = 0, e = M_Nodes->getNumOperands(); i != e; ++i) {
      DIArray SPs = CU.getSubprograms();
      for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++ i) {
        DISubprogram SP(SPs.getElement(i));
        DIArray Vars = SP.getVariables();
        for (unsigned i2 = 0, e2 = Vars.getNumElements(); i2 != e2; ++i2) {
          DIVariable DV(Vars.getElement(i));
          DV.print(errs()); errs() << " : "; DV.getType().dump();
        }
      }
    }

In addition, can I use DebegInfo to get the list of parameters (var names
and types) of a subprogram? or I have to parse the underlying metadata and
build the relationship?

Basically this /\. We use the variables list (the getVariables function you
mentioned) to persist the variables in optimized (above -O0) builds, but at
-O0 we save metadata space by not emitting the list and relying on the
dbg_value/declare intrinsics to keep the variable metadata alive and the
variables to refer to their scope (lexical blocks (that refer to
subprograms) or subprograms).

So you'd have to walk all the instructions looking for dbg_declare/value (I
forget which, perhaps both) and trace those back to the DIVariables, etc...

Hi David,

Thank you! Your suggested method works.

I think that you or someone should write what you said in the comments for the getVariable() function. :slight_smile:

Best,
Lu

+llvmdev because I accidentally dropped it

As far as I can tell, the web page only has an example saying

;;
;; Define the anchor for subprograms.
;;
!6 = metadata !{
  ...
  null,              ;; List of function variables (emitted when optimizing)
  1                  ;; Line number of the opening '{' of the function
}

I didn’t fully get what that meant when reading it for the first time and thought that I should use it with an optimization pass. I understand now why it says that. It’s not very instrumental in terms of how to use it, but it makes sense after I understand it.

Thanks,
Lu