import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import ghidra.app.script.GhidraScript; import ghidra.program.model.address.Address; import ghidra.program.model.listing.; import ghidra.program.model.symbol.; import ghidra.program.model.data.DataType; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator;
public class FunctionAll extends GhidraScript {
@Override
public void run() throws Exception {
// 创建JSON对象
StringBuilder result = new StringBuilder();
// 获取当前程序的函数列表
FunctionManager functionManager = currentProgram.getFunctionManager();
// 获取函数管理器中的所有函数
Iterator<Function> functionIterator = functionManager.getFunctions(true);
// 遍历所有全局函数
while (functionIterator.hasNext()) {
Function function = functionIterator.next();
Symbol primarySymbol = function.getSymbol(); // 获取函数的主要符号
String functionName = primarySymbol.getName(); // 获取函数名称
result.append("(" + functionName + ",hasVariable,变量名;\\n)");
// 获取函数返回类型
DataType returnType = function.getReturnType();
if (returnType != null) {
result.append("(" + functionName + ",ReturnType," + returnType.getName() + ";\\n)");
}
// 获取函数参数
for (Parameter parameter : function.getParameters()) {
result.append("(" + functionName + ",hasParameters," +
parameter.getName() + ";\\n)");
result.append("(" + parameter.getName() + ",ParametersType," +
parameter.getDataType().getName() + ";\\n)");
}
result.append("(" + functionName + ",Call," + functionName + ";\\n)");
}
// 定义输出文件路径
String outputFileName = currentProgram.getName() + "_All.txt";
String outputFilePath = "output/" + outputFileName;
// 将结果写入文件
try (FileWriter writer = new FileWriter(outputFilePath)) {
writer.write(result.toString());
println("函数信息已保存到: " + outputFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容由零声教学AI助手提供,问题来源于学员提问