查找局部变量的所有用途ghidra 脚本

问题描述

有没有办法在给定的局部变量的给定函数获取所有 PcodeOps?到目前为止,我可以根据函数名称找到 HighSymbol,但我想然后获取该变量的所有用途?

DecompileResults res = decomplib.decompileFunction(f,200,monitor);
if (res.decompileCompleted())
{
    HighFunction highFunc = res.getHighFunction();
    LocalSymbolMap localmap = highFunc.getLocalSymbolMap();
    Iterator<HighSymbol> localSymbols = localmap.getSymbols();
                        
    HighSymbol localSymbol = null;
    while (localSymbols.hasNext())
    {
      HighSymbol current = localSymbols.next();
      if (current.getName().equals(theName)) { 
       localSymbol = current;
        break;
      }
  }
}

解决方法

如果您从 HighSymbol 开始,您首先通过访问 HighVariable 获得 highSymbol.highVariable

然后您可以通过访问类型为 instancesHighVariableVarnodeAST 获取使用该变量的所有 PCodeOP,然后获取它们的 def(定义)和descendants

hvar = currentLocation.token.highVariable # get the high variable that is currently selected highlighted in the decompiler window
for varnode in hvar.instances:
    print(varnode.def) # the PCodeOp that defines this instance of the variable
    for dsc in varnode.descendants:
        print(dsc) # every PCodeOp that uses this variable